blob: d2c51dfa6b5873344637e56e09ac5a0e4202e041 [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
danielk1977bc04f852005-03-29 08:26:13 +000027
drhf2bc0132004-10-04 13:19:23 +000028# 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
36 gsub(/:/,"",name)
drha1d65d02004-10-10 19:11:35 +000037 gsub("\r","",name)
drhf2bc0132004-10-04 13:19:23 +000038 op[name] = -1
danielk1977bc04f852005-03-29 08:26:13 +000039 for(i=3; i<NF; i++){
drhf2bc0132004-10-04 13:19:23 +000040 if($i=="same" && $(i+1)=="as"){
danielk1977bc04f852005-03-29 08:26:13 +000041 sym = $(i+2)
42 sub(/,/,"",sym)
43 op[name] = tk[sym]
drhf2bc0132004-10-04 13:19:23 +000044 used[op[name]] = 1
danielk1977bc04f852005-03-29 08:26:13 +000045 sameas[op[name]] = sym
46 }
47 if($i=="stack"){
48 stack[name] = 1
drhf2bc0132004-10-04 13:19:23 +000049 }
50 }
51}
52
53# Assign numbers to all opcodes and output the result.
54END {
55 cnt = 0
drhdaa28ff2004-12-10 17:17:18 +000056 max = 0
drhb327f772004-10-06 15:03:57 +000057 print "/* Automatically generated. Do not edit */"
58 print "/* See the mkopcodeh.awk script for details */"
drhf2bc0132004-10-04 13:19:23 +000059 for(name in op){
60 if( op[name]<0 ){
61 cnt++
62 while( used[cnt] ) cnt++
63 op[name] = cnt
64 }
drhdaa28ff2004-12-10 17:17:18 +000065 used[op[name]] = 1;
66 if( op[name]>max ) max = op[name]
drh0602c2e2005-01-21 17:07:22 +000067 printf "#define %-25s %15d", name, op[name]
68 if( sameas[op[name]] ) {
danielk197724c8ab82005-02-09 01:40:23 +000069 printf " /* same as %-12s*/", sameas[op[name]]
drh0602c2e2005-01-21 17:07:22 +000070 }
71 printf "\n"
72
drhf2bc0132004-10-04 13:19:23 +000073 }
drhdaa28ff2004-12-10 17:17:18 +000074 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 }
drh0602c2e2005-01-21 17:07:22 +000081 printf "#define %-25s %15d\n", sprintf( "OP_NotUsed_%-3d", i ), i
drhdaa28ff2004-12-10 17:17:18 +000082 }
83 }
danielk1977bc04f852005-03-29 08:26:13 +000084
85 # Generate the 10 16-bit bitmasks used by function opcodeUsesStack()
86 # in vdbeaux.c. See comments in that function for details.
87 #
88 stack[0] = 0 # 0..15
89 stack[1] = 0 # 16..31
90 stack[2] = 0 # 32..47
91 stack[3] = 0 # 48..63
92 stack[4] = 0 # 64..79
93 stack[5] = 0 # 80..95
94 stack[6] = 0 # 96..111
95 stack[7] = 0 # 112..127
96 stack[8] = 0 # 128..143
97 stack[9] = 0 # 144..159
98 for(name in op){
99 if( stack[name] ){
100 n = op[name]
101 j = n%16
102 i = ((n - j)/16)
103 stack[i] = stack[i] + (2^j)
104 }
105 }
106 printf "\n"
107 for(i=0; i<10; i++){
108 printf "#define STACK_MASK_%d %d\n", i, stack[i]
109 }
110
drhf2bc0132004-10-04 13:19:23 +0000111}