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 |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | # Assign numbers to all opcodes and output the result. |
| 47 | END { |
| 48 | cnt = 0 |
drh | b327f77 | 2004-10-06 15:03:57 +0000 | [diff] [blame] | 49 | print "/* Automatically generated. Do not edit */" |
| 50 | print "/* See the mkopcodeh.awk script for details */" |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 51 | for(name in op){ |
| 52 | if( op[name]<0 ){ |
| 53 | cnt++ |
| 54 | while( used[cnt] ) cnt++ |
| 55 | op[name] = cnt |
| 56 | } |
| 57 | printf "#define %-30s %d\n", name, op[name] |
| 58 | } |
| 59 | } |