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 | 0a99ded | 2007-10-12 18:36:26 +0000 | [diff] [blame] | 38 | tk[$2] = 0+$3 |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | # Scan for "case OP_aaaa:" lines in the vdbe.c file |
| 42 | /^case OP_/ { |
| 43 | name = $2 |
drh | b726ee6 | 2005-09-05 20:35:25 +0000 | [diff] [blame] | 44 | sub(/:/,"",name) |
| 45 | sub("\r","",name) |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 46 | op[name] = -1 |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 47 | jump[name] = 0 |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 48 | out2_prerelease[name] = 0 |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 49 | in1[name] = 0 |
| 50 | in2[name] = 0 |
| 51 | in3[name] = 0 |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 52 | out3[name] = 0 |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 53 | for(i=3; i<NF; i++){ |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 54 | if($i=="same" && $(i+1)=="as"){ |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 55 | sym = $(i+2) |
| 56 | sub(/,/,"",sym) |
| 57 | op[name] = tk[sym] |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 58 | used[op[name]] = 1 |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 59 | sameas[op[name]] = sym |
| 60 | } |
drh | 4aeb7bf | 2008-01-04 19:12:35 +0000 | [diff] [blame] | 61 | x = $i |
| 62 | sub(",","",x) |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 63 | if(x=="jump"){ |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 64 | jump[name] = 1 |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 65 | }else if(x=="out2-prerelease"){ |
| 66 | out2_prerelease[name] = 1 |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 67 | }else if(x=="in1"){ |
| 68 | in1[name] = 1 |
| 69 | }else if(x=="in2"){ |
| 70 | in2[name] = 1 |
| 71 | }else if(x=="in3"){ |
| 72 | in3[name] = 1 |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 73 | }else if(x=="out3"){ |
| 74 | out3[name] = 1 |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
drh | b24a200 | 2009-11-02 18:14:50 +0000 | [diff] [blame^] | 77 | order[n_op++] = name; |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | # Assign numbers to all opcodes and output the result. |
| 81 | END { |
| 82 | cnt = 0 |
drh | daa28ff | 2004-12-10 17:17:18 +0000 | [diff] [blame] | 83 | max = 0 |
drh | b327f77 | 2004-10-06 15:03:57 +0000 | [diff] [blame] | 84 | print "/* Automatically generated. Do not edit */" |
| 85 | print "/* See the mkopcodeh.awk script for details */" |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 86 | op["OP_Noop"] = -1; |
| 87 | op["OP_Explain"] = -1; |
drh | b24a200 | 2009-11-02 18:14:50 +0000 | [diff] [blame^] | 88 | for(i=0; i<n_op; i++){ |
| 89 | name = order[i]; |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 90 | if( op[name]<0 ){ |
| 91 | cnt++ |
| 92 | while( used[cnt] ) cnt++ |
| 93 | op[name] = cnt |
| 94 | } |
drh | daa28ff | 2004-12-10 17:17:18 +0000 | [diff] [blame] | 95 | used[op[name]] = 1; |
| 96 | if( op[name]>max ) max = op[name] |
drh | 0602c2e | 2005-01-21 17:07:22 +0000 | [diff] [blame] | 97 | printf "#define %-25s %15d", name, op[name] |
| 98 | if( sameas[op[name]] ) { |
danielk1977 | 24c8ab8 | 2005-02-09 01:40:23 +0000 | [diff] [blame] | 99 | printf " /* same as %-12s*/", sameas[op[name]] |
drh | 0602c2e | 2005-01-21 17:07:22 +0000 | [diff] [blame] | 100 | } |
| 101 | printf "\n" |
| 102 | |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 103 | } |
drh | daa28ff | 2004-12-10 17:17:18 +0000 | [diff] [blame] | 104 | seenUnused = 0; |
| 105 | for(i=1; i<max; i++){ |
| 106 | if( !used[i] ){ |
| 107 | if( !seenUnused ){ |
| 108 | printf "\n/* The following opcode values are never used */\n" |
| 109 | seenUnused = 1 |
| 110 | } |
drh | 0602c2e | 2005-01-21 17:07:22 +0000 | [diff] [blame] | 111 | printf "#define %-25s %15d\n", sprintf( "OP_NotUsed_%-3d", i ), i |
drh | daa28ff | 2004-12-10 17:17:18 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 114 | |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 115 | # Generate the bitvectors: |
| 116 | # |
| 117 | # bit 0: jump |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 118 | # bit 1: pushes a result onto stack |
| 119 | # bit 2: output to p1. release p1 before opcode runs |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 120 | # |
| 121 | for(i=0; i<=max; i++) bv[i] = 0; |
drh | b24a200 | 2009-11-02 18:14:50 +0000 | [diff] [blame^] | 122 | for(i=0; i<n_op; i++){ |
| 123 | name = order[i]; |
drh | 4aeb7bf | 2008-01-04 19:12:35 +0000 | [diff] [blame] | 124 | x = op[name] |
| 125 | a0 = a1 = a2 = a3 = a4 = a5 = a6 = a7 = 0 |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 126 | # a8 = a9 = a10 = a11 = a12 = a13 = a14 = a15 = 0 |
drh | 4aeb7bf | 2008-01-04 19:12:35 +0000 | [diff] [blame] | 127 | if( jump[name] ) a0 = 1; |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 128 | if( out2_prerelease[name] ) a1 = 2; |
| 129 | if( in1[name] ) a2 = 4; |
| 130 | if( in2[name] ) a3 = 8; |
| 131 | if( in3[name] ) a4 = 16; |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 132 | if( out3[name] ) a5 = 32; |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 133 | # bv[x] = a0+a1+a2+a3+a4+a5+a6+a7+a8+a9+a10+a11+a12+a13+a14+a15; |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 134 | bv[x] = a0+a1+a2+a3+a4+a5+a6+a7; |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 135 | } |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 136 | print "\n" |
| 137 | print "/* Properties such as \"out2\" or \"jump\" that are specified in" |
drh | 3de120b | 2008-01-10 00:08:43 +0000 | [diff] [blame] | 138 | print "** comments following the \"case\" for each opcode in the vdbe.c" |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 139 | print "** are encoded into bitvectors as follows:" |
| 140 | print "*/" |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 141 | print "#define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */" |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 142 | print "#define OPFLG_OUT2_PRERELEASE 0x0002 /* out2-prerelease: */" |
| 143 | print "#define OPFLG_IN1 0x0004 /* in1: P1 is an input */" |
| 144 | print "#define OPFLG_IN2 0x0008 /* in2: P2 is an input */" |
| 145 | print "#define OPFLG_IN3 0x0010 /* in3: P3 is an input */" |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 146 | print "#define OPFLG_OUT3 0x0020 /* out3: P3 is an output */" |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 147 | print "#define OPFLG_INITIALIZER {\\" |
| 148 | for(i=0; i<=max; i++){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 149 | if( i%8==0 ) printf("/* %3d */",i) |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 150 | printf " 0x%02x,", bv[i] |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 151 | if( i%8==7 ) printf("\\\n"); |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 152 | } |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 153 | print "}" |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 154 | } |