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 | |
| 27 | # Remember the TK_ values from the parse.h file |
| 28 | /^#define TK_/ { |
| 29 | tk[$2] = $3 |
| 30 | } |
| 31 | |
| 32 | # Scan for "case OP_aaaa:" lines in the vdbe.c file |
| 33 | /^case OP_/ { |
| 34 | name = $2 |
| 35 | gsub(/:/,"",name) |
drh | a1d65d0 | 2004-10-10 19:11:35 +0000 | [diff] [blame] | 36 | gsub("\r","",name) |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 37 | op[name] = -1 |
| 38 | for(i=3; i<NF-2; i++){ |
| 39 | if($i=="same" && $(i+1)=="as"){ |
| 40 | op[name] = tk[$(i+2)] |
| 41 | used[op[name]] = 1 |
drh | 0602c2e | 2005-01-21 17:07:22 +0000 | [diff] [blame] | 42 | sameas[op[name]] = $(i+2) |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | # Assign numbers to all opcodes and output the result. |
| 48 | END { |
| 49 | cnt = 0 |
drh | daa28ff | 2004-12-10 17:17:18 +0000 | [diff] [blame] | 50 | max = 0 |
drh | b327f77 | 2004-10-06 15:03:57 +0000 | [diff] [blame] | 51 | print "/* Automatically generated. Do not edit */" |
| 52 | print "/* See the mkopcodeh.awk script for details */" |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 53 | for(name in op){ |
| 54 | if( op[name]<0 ){ |
| 55 | cnt++ |
| 56 | while( used[cnt] ) cnt++ |
| 57 | op[name] = cnt |
| 58 | } |
drh | daa28ff | 2004-12-10 17:17:18 +0000 | [diff] [blame] | 59 | used[op[name]] = 1; |
| 60 | if( op[name]>max ) max = op[name] |
drh | 0602c2e | 2005-01-21 17:07:22 +0000 | [diff] [blame] | 61 | printf "#define %-25s %15d", name, op[name] |
| 62 | if( sameas[op[name]] ) { |
danielk1977 | 24c8ab8 | 2005-02-09 01:40:23 +0000 | [diff] [blame^] | 63 | printf " /* same as %-12s*/", sameas[op[name]] |
drh | 0602c2e | 2005-01-21 17:07:22 +0000 | [diff] [blame] | 64 | } |
| 65 | printf "\n" |
| 66 | |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 67 | } |
drh | daa28ff | 2004-12-10 17:17:18 +0000 | [diff] [blame] | 68 | seenUnused = 0; |
| 69 | for(i=1; i<max; i++){ |
| 70 | if( !used[i] ){ |
| 71 | if( !seenUnused ){ |
| 72 | printf "\n/* The following opcode values are never used */\n" |
| 73 | seenUnused = 1 |
| 74 | } |
drh | 0602c2e | 2005-01-21 17:07:22 +0000 | [diff] [blame] | 75 | printf "#define %-25s %15d\n", sprintf( "OP_NotUsed_%-3d", i ), i |
drh | daa28ff | 2004-12-10 17:17:18 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 78 | } |