drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 1 | #!/usr/bin/awk -f |
| 2 | # |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 3 | # Generate the file opcodes.h. |
| 4 | # |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 5 | # This AWK script scans a concatenation of the parse.h output file from the |
| 6 | # parser and the vdbe.c source file in order to generate the opcodes numbers |
| 7 | # for all opcodes. |
| 8 | # |
| 9 | # The lines of the vdbe.c that we are interested in are of the form: |
| 10 | # |
| 11 | # case OP_aaaa: /* same as TK_bbbbb */ |
| 12 | # |
| 13 | # The TK_ comment is optional. If it is present, then the value assigned to |
| 14 | # the OP_ is the same as the TK_ value. If missing, the OP_ value is assigned |
| 15 | # a small integer that is different from every other OP_ value. |
| 16 | # |
drh | 0ed4fcd | 2006-01-26 14:29:58 +0000 | [diff] [blame] | 17 | # We go to the trouble of making some OP_ values the same as TK_ values |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 18 | # as an optimization. During parsing, things like expression operators |
| 19 | # are coded with TK_ values such as TK_ADD, TK_DIVIDE, and so forth. Later |
| 20 | # during code generation, we need to generate corresponding opcodes like |
| 21 | # OP_Add and OP_Divide. By making TK_ADD==OP_Add and TK_DIVIDE==OP_Divide, |
drh | 0ed4fcd | 2006-01-26 14:29:58 +0000 | [diff] [blame] | 22 | # code to translate from one to the other is avoided. This makes the |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 23 | # code generator run (infinitesimally) faster and more importantly it makes |
drh | 0ed4fcd | 2006-01-26 14:29:58 +0000 | [diff] [blame] | 24 | # the library footprint smaller. |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 25 | # |
drh | fa3b19e | 2005-11-24 22:22:29 +0000 | [diff] [blame] | 26 | # This script also scans for lines of the form: |
| 27 | # |
drh | b24a200 | 2009-11-02 18:14:50 +0000 | [diff] [blame] | 28 | # case OP_aaaa: /* jump, in1, in2, in3, out2-prerelease, out3 */ |
drh | fa3b19e | 2005-11-24 22:22:29 +0000 | [diff] [blame] | 29 | # |
drh | b24a200 | 2009-11-02 18:14:50 +0000 | [diff] [blame] | 30 | # When such comments are found on an opcode, it means that certain |
| 31 | # properties apply to that opcode. Set corresponding flags using the |
| 32 | # OPFLG_INITIALIZER macro. |
drh | fa3b19e | 2005-11-24 22:22:29 +0000 | [diff] [blame] | 33 | # |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 34 | |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 35 | |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 36 | # Remember the TK_ values from the parse.h file |
| 37 | /^#define TK_/ { |
drh | 8c8a8c4 | 2013-08-06 07:45:08 +0000 | [diff] [blame] | 38 | tk[$2] = 0+$3 # tk[x] holds the numeric value for TK symbol X |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 39 | } |
| 40 | |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 41 | # Find "/* Opcode: " lines in the vdbe.c file. Each one introduces |
| 42 | # a new opcode. Remember which parameters are used. |
| 43 | /^.. Opcode: / { |
| 44 | currentOp = "OP_" $3 |
| 45 | m = 0 |
| 46 | for(i=4; i<=NF; i++){ |
| 47 | x = $i |
| 48 | if( x=="P1" ) m += 1 |
| 49 | if( x=="P2" ) m += 2 |
| 50 | if( x=="P3" ) m += 4 |
| 51 | if( x=="P4" ) m += 8 |
| 52 | if( x=="P5" ) m += 16 |
| 53 | } |
| 54 | paramused[currentOp] = m |
| 55 | } |
| 56 | |
| 57 | # Find "** Synopsis: " lines that follow Opcode: |
| 58 | /^.. Synopsis: / { |
| 59 | if( currentOp ){ |
| 60 | x = $3 |
| 61 | for(i=4; i<=NF; i++){ |
| 62 | x = x " " $i |
| 63 | } |
| 64 | synopsis[currentOp] = x |
| 65 | } |
| 66 | } |
| 67 | |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 68 | # Scan for "case OP_aaaa:" lines in the vdbe.c file |
| 69 | /^case OP_/ { |
| 70 | name = $2 |
drh | b726ee6 | 2005-09-05 20:35:25 +0000 | [diff] [blame] | 71 | sub(/:/,"",name) |
| 72 | sub("\r","",name) |
drh | 8c8a8c4 | 2013-08-06 07:45:08 +0000 | [diff] [blame] | 73 | op[name] = -1 # op[x] holds the numeric value for OP symbol x |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 74 | jump[name] = 0 |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 75 | in1[name] = 0 |
| 76 | in2[name] = 0 |
| 77 | in3[name] = 0 |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 78 | out2[name] = 0 |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 79 | out3[name] = 0 |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 80 | for(i=3; i<NF; i++){ |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 81 | if($i=="same" && $(i+1)=="as"){ |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 82 | sym = $(i+2) |
| 83 | sub(/,/,"",sym) |
drh | 8c8a8c4 | 2013-08-06 07:45:08 +0000 | [diff] [blame] | 84 | val = tk[sym] |
| 85 | op[name] = val |
| 86 | used[val] = 1 |
| 87 | sameas[val] = sym |
| 88 | def[val] = name |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 89 | } |
drh | 4aeb7bf | 2008-01-04 19:12:35 +0000 | [diff] [blame] | 90 | x = $i |
| 91 | sub(",","",x) |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 92 | if(x=="jump"){ |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 93 | jump[name] = 1 |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 94 | }else if(x=="in1"){ |
| 95 | in1[name] = 1 |
| 96 | }else if(x=="in2"){ |
| 97 | in2[name] = 1 |
| 98 | }else if(x=="in3"){ |
| 99 | in3[name] = 1 |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 100 | }else if(x=="out2"){ |
| 101 | out2[name] = 1 |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 102 | }else if(x=="out3"){ |
| 103 | out3[name] = 1 |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
drh | b24a200 | 2009-11-02 18:14:50 +0000 | [diff] [blame] | 106 | order[n_op++] = name; |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | # Assign numbers to all opcodes and output the result. |
| 110 | END { |
| 111 | cnt = 0 |
drh | daa28ff | 2004-12-10 17:17:18 +0000 | [diff] [blame] | 112 | max = 0 |
drh | b327f77 | 2004-10-06 15:03:57 +0000 | [diff] [blame] | 113 | print "/* Automatically generated. Do not edit */" |
| 114 | print "/* See the mkopcodeh.awk script for details */" |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 115 | op["OP_Noop"] = -1; |
drh | ccaf773 | 2009-11-02 18:44:58 +0000 | [diff] [blame] | 116 | order[n_op++] = "OP_Noop"; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 117 | op["OP_Explain"] = -1; |
drh | ccaf773 | 2009-11-02 18:44:58 +0000 | [diff] [blame] | 118 | order[n_op++] = "OP_Explain"; |
drh | 8c8a8c4 | 2013-08-06 07:45:08 +0000 | [diff] [blame] | 119 | |
| 120 | # Assign small values to opcodes that are processed by resolveP2Values() |
| 121 | # to make code generation for the switch() statement smaller and faster. |
| 122 | for(i=0; i<n_op; i++){ |
| 123 | name = order[i]; |
| 124 | if( op[name]>=0 ) continue; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 125 | if( name=="OP_Transaction" \ |
drh | 8c8a8c4 | 2013-08-06 07:45:08 +0000 | [diff] [blame] | 126 | || name=="OP_AutoCommit" \ |
| 127 | || name=="OP_Savepoint" \ |
| 128 | || name=="OP_Checkpoint" \ |
| 129 | || name=="OP_Vacuum" \ |
| 130 | || name=="OP_JournalMode" \ |
| 131 | || name=="OP_VUpdate" \ |
| 132 | || name=="OP_VFilter" \ |
| 133 | || name=="OP_Next" \ |
drh | 66e8102 | 2013-11-21 17:24:18 +0000 | [diff] [blame] | 134 | || name=="OP_NextIfOpen" \ |
drh | 8c8a8c4 | 2013-08-06 07:45:08 +0000 | [diff] [blame] | 135 | || name=="OP_SorterNext" \ |
| 136 | || name=="OP_Prev" \ |
drh | 66e8102 | 2013-11-21 17:24:18 +0000 | [diff] [blame] | 137 | || name=="OP_PrevIfOpen" \ |
drh | 8c8a8c4 | 2013-08-06 07:45:08 +0000 | [diff] [blame] | 138 | ){ |
| 139 | cnt++ |
| 140 | while( used[cnt] ) cnt++ |
| 141 | op[name] = cnt |
| 142 | used[cnt] = 1 |
| 143 | def[cnt] = name |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | # Generate the numeric values for opcodes |
drh | b24a200 | 2009-11-02 18:14:50 +0000 | [diff] [blame] | 148 | for(i=0; i<n_op; i++){ |
| 149 | name = order[i]; |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 150 | if( op[name]<0 ){ |
| 151 | cnt++ |
| 152 | while( used[cnt] ) cnt++ |
| 153 | op[name] = cnt |
drh | 8c8a8c4 | 2013-08-06 07:45:08 +0000 | [diff] [blame] | 154 | used[cnt] = 1 |
| 155 | def[cnt] = name |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 156 | } |
drh | 8c8a8c4 | 2013-08-06 07:45:08 +0000 | [diff] [blame] | 157 | } |
| 158 | max = cnt |
| 159 | for(i=1; i<=max; i++){ |
| 160 | if( !used[i] ){ |
| 161 | def[i] = "OP_NotUsed_" i |
| 162 | } |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 163 | printf "#define %-16s %3d", def[i], i |
| 164 | com = "" |
drh | 8c8a8c4 | 2013-08-06 07:45:08 +0000 | [diff] [blame] | 165 | if( sameas[i] ){ |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 166 | com = "same as " sameas[i] |
| 167 | } |
| 168 | x = synopsis[def[i]] |
| 169 | if( x ){ |
| 170 | if( com=="" ){ |
| 171 | com = "synopsis: " x |
| 172 | } else { |
| 173 | com = com ", synopsis: " x |
| 174 | } |
| 175 | } |
| 176 | if( com!="" ){ |
| 177 | printf " /* %-42s */", com |
| 178 | } |
drh | 0602c2e | 2005-01-21 17:07:22 +0000 | [diff] [blame] | 179 | printf "\n" |
drh | daa28ff | 2004-12-10 17:17:18 +0000 | [diff] [blame] | 180 | } |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 181 | |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 182 | # Generate the bitvectors: |
| 183 | # |
| 184 | # bit 0: jump |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 185 | # bit 1: pushes a result onto stack |
| 186 | # bit 2: output to p1. release p1 before opcode runs |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 187 | # |
drh | 8c8a8c4 | 2013-08-06 07:45:08 +0000 | [diff] [blame] | 188 | for(i=0; i<=max; i++){ |
| 189 | name = def[i] |
drh | 4aeb7bf | 2008-01-04 19:12:35 +0000 | [diff] [blame] | 190 | a0 = a1 = a2 = a3 = a4 = a5 = a6 = a7 = 0 |
| 191 | if( jump[name] ) a0 = 1; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 192 | if( in1[name] ) a2 = 2; |
| 193 | if( in2[name] ) a3 = 4; |
| 194 | if( in3[name] ) a4 = 8; |
| 195 | if( out2[name] ) a5 = 16; |
| 196 | if( out3[name] ) a6 = 32; |
| 197 | bv[i] = a0+a1+a2+a3+a4+a5+a6; |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 198 | } |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 199 | print "\n" |
| 200 | print "/* Properties such as \"out2\" or \"jump\" that are specified in" |
drh | 3de120b | 2008-01-10 00:08:43 +0000 | [diff] [blame] | 201 | print "** comments following the \"case\" for each opcode in the vdbe.c" |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 202 | print "** are encoded into bitvectors as follows:" |
| 203 | print "*/" |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 204 | print "#define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */" |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 205 | print "#define OPFLG_IN1 0x0002 /* in1: P1 is an input */" |
| 206 | print "#define OPFLG_IN2 0x0004 /* in2: P2 is an input */" |
| 207 | print "#define OPFLG_IN3 0x0008 /* in3: P3 is an input */" |
| 208 | print "#define OPFLG_OUT2 0x0010 /* out2: P2 is an output */" |
| 209 | print "#define OPFLG_OUT3 0x0020 /* out3: P3 is an output */" |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 210 | print "#define OPFLG_INITIALIZER {\\" |
| 211 | for(i=0; i<=max; i++){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 212 | if( i%8==0 ) printf("/* %3d */",i) |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 213 | printf " 0x%02x,", bv[i] |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 214 | if( i%8==7 ) printf("\\\n"); |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 215 | } |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 216 | print "}" |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 217 | if( 0 ){ |
| 218 | print "\n/* Bitmask to indicate which fields (P1..P5) of each opcode are" |
| 219 | print "** actually used.\n*/" |
| 220 | print "#define OP_PARAM_USED_INITIALIZER {\\" |
| 221 | for(i=0; i<=max; i++){ |
| 222 | if( i%8==0 ) printf("/* %3d */",i) |
| 223 | printf " 0x%02x,", paramused[def[i]] |
| 224 | if( i%8==7 ) printf("\\\n"); |
| 225 | } |
| 226 | print "}" |
| 227 | } |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 228 | } |