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 | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 17 | # We go to the trouble of making some OP_ value the same as TK_ values |
| 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, |
| 22 | # code to translation from one to the other is avoided. This makes the |
| 23 | # code generator run (infinitesimally) faster and more importantly it makes |
| 24 | # the total library smaller. |
| 25 | # |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 26 | |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 27 | |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 28 | # Remember the TK_ values from the parse.h file |
| 29 | /^#define TK_/ { |
| 30 | tk[$2] = $3 |
| 31 | } |
| 32 | |
| 33 | # Scan for "case OP_aaaa:" lines in the vdbe.c file |
| 34 | /^case OP_/ { |
| 35 | name = $2 |
drh | b726ee6 | 2005-09-05 20:35:25 +0000 | [diff] [blame^] | 36 | sub(/:/,"",name) |
| 37 | sub("\r","",name) |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 38 | op[name] = -1 |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 39 | for(i=3; i<NF; i++){ |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 40 | if($i=="same" && $(i+1)=="as"){ |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 41 | sym = $(i+2) |
| 42 | sub(/,/,"",sym) |
| 43 | op[name] = tk[sym] |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 44 | used[op[name]] = 1 |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 45 | sameas[op[name]] = sym |
| 46 | } |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 47 | if($i=="no-push"){ |
| 48 | nopush[name] = 1 |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | # Assign numbers to all opcodes and output the result. |
| 54 | END { |
| 55 | cnt = 0 |
drh | daa28ff | 2004-12-10 17:17:18 +0000 | [diff] [blame] | 56 | max = 0 |
drh | b327f77 | 2004-10-06 15:03:57 +0000 | [diff] [blame] | 57 | print "/* Automatically generated. Do not edit */" |
| 58 | print "/* See the mkopcodeh.awk script for details */" |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 59 | for(name in op){ |
| 60 | if( op[name]<0 ){ |
| 61 | cnt++ |
| 62 | while( used[cnt] ) cnt++ |
| 63 | op[name] = cnt |
| 64 | } |
drh | daa28ff | 2004-12-10 17:17:18 +0000 | [diff] [blame] | 65 | used[op[name]] = 1; |
| 66 | if( op[name]>max ) max = op[name] |
drh | 0602c2e | 2005-01-21 17:07:22 +0000 | [diff] [blame] | 67 | printf "#define %-25s %15d", name, op[name] |
| 68 | if( sameas[op[name]] ) { |
danielk1977 | 24c8ab8 | 2005-02-09 01:40:23 +0000 | [diff] [blame] | 69 | printf " /* same as %-12s*/", sameas[op[name]] |
drh | 0602c2e | 2005-01-21 17:07:22 +0000 | [diff] [blame] | 70 | } |
| 71 | printf "\n" |
| 72 | |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 73 | } |
drh | daa28ff | 2004-12-10 17:17:18 +0000 | [diff] [blame] | 74 | seenUnused = 0; |
| 75 | for(i=1; i<max; i++){ |
| 76 | if( !used[i] ){ |
| 77 | if( !seenUnused ){ |
| 78 | printf "\n/* The following opcode values are never used */\n" |
| 79 | seenUnused = 1 |
| 80 | } |
drh | 0602c2e | 2005-01-21 17:07:22 +0000 | [diff] [blame] | 81 | printf "#define %-25s %15d\n", sprintf( "OP_NotUsed_%-3d", i ), i |
drh | daa28ff | 2004-12-10 17:17:18 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 84 | |
| 85 | # Generate the 10 16-bit bitmasks used by function opcodeUsesStack() |
| 86 | # in vdbeaux.c. See comments in that function for details. |
| 87 | # |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 88 | nopush[0] = 0 # 0..15 |
| 89 | nopush[1] = 0 # 16..31 |
| 90 | nopush[2] = 0 # 32..47 |
| 91 | nopush[3] = 0 # 48..63 |
| 92 | nopush[4] = 0 # 64..79 |
| 93 | nopush[5] = 0 # 80..95 |
| 94 | nopush[6] = 0 # 96..111 |
| 95 | nopush[7] = 0 # 112..127 |
| 96 | nopush[8] = 0 # 128..143 |
| 97 | nopush[9] = 0 # 144..159 |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 98 | for(name in op){ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 99 | if( nopush[name] ){ |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 100 | n = op[name] |
| 101 | j = n%16 |
| 102 | i = ((n - j)/16) |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 103 | nopush[i] = nopush[i] + (2^j) |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | printf "\n" |
| 107 | for(i=0; i<10; i++){ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 108 | printf "#define NOPUSH_MASK_%d %d\n", i, nopush[i] |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 109 | } |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 110 | } |