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 | # |
| 28 | # case OP_aaaa: /* no-push */ |
| 29 | # |
| 30 | # When the no-push comment is found on an opcode, it means that that |
drh | 0ed4fcd | 2006-01-26 14:29:58 +0000 | [diff] [blame] | 31 | # opcode does not leave a result on the stack. By identifying which |
drh | fa3b19e | 2005-11-24 22:22:29 +0000 | [diff] [blame] | 32 | # opcodes leave results on the stack it is possible to determine a |
| 33 | # much smaller upper bound on the size of the stack. This allows |
| 34 | # a smaller stack to be allocated, which is important to embedded |
| 35 | # systems with limited memory space. This script generates a series |
| 36 | # of "NOPUSH_MASK" defines that contain bitmaps of opcodes that leave |
| 37 | # results on the stack. The NOPUSH_MASK defines are used in vdbeaux.c |
| 38 | # to help determine the maximum stack size. |
| 39 | # |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 40 | |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 41 | |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 42 | # Remember the TK_ values from the parse.h file |
| 43 | /^#define TK_/ { |
drh | 0a99ded | 2007-10-12 18:36:26 +0000 | [diff] [blame] | 44 | tk[$2] = 0+$3 |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | # Scan for "case OP_aaaa:" lines in the vdbe.c file |
| 48 | /^case OP_/ { |
| 49 | name = $2 |
drh | b726ee6 | 2005-09-05 20:35:25 +0000 | [diff] [blame] | 50 | sub(/:/,"",name) |
| 51 | sub("\r","",name) |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 52 | op[name] = -1 |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 53 | out1[name] = 0 |
| 54 | out2[name] = 0 |
| 55 | out3[name] = 0 |
| 56 | jump[name] = 0 |
| 57 | in1[name] = 0 |
| 58 | in2[name] = 0 |
| 59 | in3[name] = 0 |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 +0000 | [diff] [blame] | 60 | nopush[name] = 0 |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 61 | for(i=3; i<NF; i++){ |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 62 | if($i=="same" && $(i+1)=="as"){ |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 63 | sym = $(i+2) |
| 64 | sub(/,/,"",sym) |
| 65 | op[name] = tk[sym] |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 66 | used[op[name]] = 1 |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 67 | sameas[op[name]] = sym |
| 68 | } |
drh | 4aeb7bf | 2008-01-04 19:12:35 +0000 | [diff] [blame^] | 69 | x = $i |
| 70 | sub(",","",x) |
| 71 | if(x=="no-push"){ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 72 | nopush[name] = 1 |
drh | 4aeb7bf | 2008-01-04 19:12:35 +0000 | [diff] [blame^] | 73 | }else if(x=="out1"){ |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 74 | out1[name] = 1 |
drh | 4aeb7bf | 2008-01-04 19:12:35 +0000 | [diff] [blame^] | 75 | }else if(x=="out2"){ |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 76 | out2[name] = 2 |
drh | 4aeb7bf | 2008-01-04 19:12:35 +0000 | [diff] [blame^] | 77 | }else if(x=="out3"){ |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 78 | out3[name] = 3 |
drh | 4aeb7bf | 2008-01-04 19:12:35 +0000 | [diff] [blame^] | 79 | }else if(x=="in1"){ |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 80 | in1[name] = 1 |
drh | 4aeb7bf | 2008-01-04 19:12:35 +0000 | [diff] [blame^] | 81 | }else if(x=="in2"){ |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 82 | in2[name] = 1 |
drh | 4aeb7bf | 2008-01-04 19:12:35 +0000 | [diff] [blame^] | 83 | }else if(x=="in3"){ |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 84 | in3[name] = 1 |
drh | 4aeb7bf | 2008-01-04 19:12:35 +0000 | [diff] [blame^] | 85 | }else if(x=="jump"){ |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 86 | jump[name] = 1 |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | # Assign numbers to all opcodes and output the result. |
| 92 | END { |
| 93 | cnt = 0 |
drh | daa28ff | 2004-12-10 17:17:18 +0000 | [diff] [blame] | 94 | max = 0 |
drh | b327f77 | 2004-10-06 15:03:57 +0000 | [diff] [blame] | 95 | print "/* Automatically generated. Do not edit */" |
| 96 | print "/* See the mkopcodeh.awk script for details */" |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 97 | for(name in op){ |
| 98 | if( op[name]<0 ){ |
| 99 | cnt++ |
| 100 | while( used[cnt] ) cnt++ |
| 101 | op[name] = cnt |
| 102 | } |
drh | daa28ff | 2004-12-10 17:17:18 +0000 | [diff] [blame] | 103 | used[op[name]] = 1; |
| 104 | if( op[name]>max ) max = op[name] |
drh | 0602c2e | 2005-01-21 17:07:22 +0000 | [diff] [blame] | 105 | printf "#define %-25s %15d", name, op[name] |
| 106 | if( sameas[op[name]] ) { |
danielk1977 | 24c8ab8 | 2005-02-09 01:40:23 +0000 | [diff] [blame] | 107 | printf " /* same as %-12s*/", sameas[op[name]] |
drh | 0602c2e | 2005-01-21 17:07:22 +0000 | [diff] [blame] | 108 | } |
| 109 | printf "\n" |
| 110 | |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 111 | } |
drh | daa28ff | 2004-12-10 17:17:18 +0000 | [diff] [blame] | 112 | seenUnused = 0; |
| 113 | for(i=1; i<max; i++){ |
| 114 | if( !used[i] ){ |
| 115 | if( !seenUnused ){ |
| 116 | printf "\n/* The following opcode values are never used */\n" |
| 117 | seenUnused = 1 |
| 118 | } |
drh | 0602c2e | 2005-01-21 17:07:22 +0000 | [diff] [blame] | 119 | printf "#define %-25s %15d\n", sprintf( "OP_NotUsed_%-3d", i ), i |
drh | daa28ff | 2004-12-10 17:17:18 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 122 | |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 123 | # Generate the bitvectors: |
| 124 | # |
| 125 | # bit 0: jump |
| 126 | # bit 1: output on P1 |
| 127 | # bit 2: output on P2 |
| 128 | # bit 3: output on P3 |
| 129 | # bit 4: input on P1 |
| 130 | # bit 5: input on P2 |
| 131 | # bit 6: input on P3 |
| 132 | # bit 7: pushes a result onto stack |
| 133 | # |
| 134 | for(i=0; i<=max; i++) bv[i] = 0; |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 135 | for(name in op){ |
drh | 4aeb7bf | 2008-01-04 19:12:35 +0000 | [diff] [blame^] | 136 | x = op[name] |
| 137 | a0 = a1 = a2 = a3 = a4 = a5 = a6 = a7 = 0 |
| 138 | if( jump[name] ) a0 = 1; |
| 139 | if( out1[name] ) a1 = 2; |
| 140 | if( out2[name] ) a2 = 4; |
| 141 | if( out3[name] ) a3 = 8; |
| 142 | if( in1[name] ) a4 = 16; |
| 143 | if( in2[name] ) a5 = 32; |
| 144 | if( in3[name] ) a6 = 64; |
| 145 | if( nopush[name]==0 ) a7 = 128; |
| 146 | bv[x] = a0+a1+a2+a3+a4+a5+a6+a7; |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 147 | } |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 148 | print "\n" |
| 149 | print "/* Properties such as \"out2\" or \"jump\" that are specified in" |
| 150 | print "** comments following the "case" for each opcode in the vdbe.c" |
| 151 | print "** are encoded into bitvectors as follows:" |
| 152 | print "*/" |
| 153 | print "#define OPFLG_JUMP 0x01 /* jump: P2 holds a jump target */" |
| 154 | print "#define OPFLG_OUT1 0x02 /* out1: P1 specifies output reg */" |
| 155 | print "#define OPFLG_OUT2 0x04 /* out2: P2 specifies output reg */" |
| 156 | print "#define OPFLG_OUT3 0x08 /* out3: P3 specifies output reg */" |
| 157 | print "#define OPFLG_IN1 0x10 /* in1: P1 is an input reg */" |
| 158 | print "#define OPFLG_IN2 0x20 /* in2: P2 is an input reg */" |
| 159 | print "#define OPFLG_IN3 0x40 /* in3: P3 is an input reg */" |
| 160 | print "#define OPFLG_PUSH 0x80 /* omits no-push: Does not push */" |
| 161 | print "#define OPFLG_INITIALIZER {\\" |
| 162 | for(i=0; i<=max; i++){ |
| 163 | printf " 0x%02x,", bv[i] |
| 164 | if( i%10==9 ) printf("\\\n"); |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 165 | } |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 166 | print "}" |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 167 | } |