blob: a029a99fa105bed3ca29c25a1f40c09b76364fdb [file] [log] [blame]
drhf2bc0132004-10-04 13:19:23 +00001#!/usr/bin/awk -f
2#
drh722e95a2004-10-25 20:33:44 +00003# Generate the file opcodes.h.
4#
drhf2bc0132004-10-04 13:19:23 +00005# 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#
drh722e95a2004-10-25 20:33:44 +000017# 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#
drhf2bc0132004-10-04 13:19:23 +000026
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)
drha1d65d02004-10-10 19:11:35 +000036 gsub("\r","",name)
drhf2bc0132004-10-04 13:19:23 +000037 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.
47END {
48 cnt = 0
drhdaa28ff2004-12-10 17:17:18 +000049 max = 0
drhb327f772004-10-06 15:03:57 +000050 print "/* Automatically generated. Do not edit */"
51 print "/* See the mkopcodeh.awk script for details */"
drhf2bc0132004-10-04 13:19:23 +000052 for(name in op){
53 if( op[name]<0 ){
54 cnt++
55 while( used[cnt] ) cnt++
56 op[name] = cnt
57 }
drhdaa28ff2004-12-10 17:17:18 +000058 used[op[name]] = 1;
59 if( op[name]>max ) max = op[name]
drhf2bc0132004-10-04 13:19:23 +000060 printf "#define %-30s %d\n", name, op[name]
61 }
drhdaa28ff2004-12-10 17:17:18 +000062 seenUnused = 0;
63 for(i=1; i<max; i++){
64 if( !used[i] ){
65 if( !seenUnused ){
66 printf "\n/* The following opcode values are never used */\n"
67 seenUnused = 1
68 }
69 printf "/*#define OP_? %d -- Not Used */\n", i
70 }
71 }
drhf2bc0132004-10-04 13:19:23 +000072}