blob: 97c291de77b6d71cef0bc750c22c9b08ca2a15c5 [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#
drh0ed4fcd2006-01-26 14:29:58 +000017# We go to the trouble of making some OP_ values the same as TK_ values
drh722e95a2004-10-25 20:33:44 +000018# 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,
drh0ed4fcd2006-01-26 14:29:58 +000022# code to translate from one to the other is avoided. This makes the
drh722e95a2004-10-25 20:33:44 +000023# code generator run (infinitesimally) faster and more importantly it makes
drh0ed4fcd2006-01-26 14:29:58 +000024# the library footprint smaller.
drh722e95a2004-10-25 20:33:44 +000025#
drhfa3b19e2005-11-24 22:22:29 +000026# This script also scans for lines of the form:
27#
28# case OP_aaaa: /* no-push */
29#
30# When the no-push comment is found on an opcode, it means that that
drh0ed4fcd2006-01-26 14:29:58 +000031# opcode does not leave a result on the stack. By identifying which
drhfa3b19e2005-11-24 22:22:29 +000032# opcodes leave results on the stack it is possible to determine a
33# much smaller upper bound on the size of the stack. This allows
34# a smaller stack to be allocated, which is important to embedded
35# systems with limited memory space. This script generates a series
36# of "NOPUSH_MASK" defines that contain bitmaps of opcodes that leave
37# results on the stack. The NOPUSH_MASK defines are used in vdbeaux.c
38# to help determine the maximum stack size.
39#
drhf2bc0132004-10-04 13:19:23 +000040
danielk1977bc04f852005-03-29 08:26:13 +000041
drhf2bc0132004-10-04 13:19:23 +000042# Remember the TK_ values from the parse.h file
43/^#define TK_/ {
drh0a99ded2007-10-12 18:36:26 +000044 tk[$2] = 0+$3
drhf2bc0132004-10-04 13:19:23 +000045}
46
47# Scan for "case OP_aaaa:" lines in the vdbe.c file
48/^case OP_/ {
49 name = $2
drhb726ee62005-09-05 20:35:25 +000050 sub(/:/,"",name)
51 sub("\r","",name)
drhf2bc0132004-10-04 13:19:23 +000052 op[name] = -1
drh3a40f692008-01-04 16:50:09 +000053 out1[name] = 0
54 out2[name] = 0
55 out3[name] = 0
56 jump[name] = 0
57 in1[name] = 0
58 in2[name] = 0
59 in3[name] = 0
danielk1977bc04f852005-03-29 08:26:13 +000060 for(i=3; i<NF; i++){
drhf2bc0132004-10-04 13:19:23 +000061 if($i=="same" && $(i+1)=="as"){
danielk1977bc04f852005-03-29 08:26:13 +000062 sym = $(i+2)
63 sub(/,/,"",sym)
64 op[name] = tk[sym]
drhf2bc0132004-10-04 13:19:23 +000065 used[op[name]] = 1
danielk1977bc04f852005-03-29 08:26:13 +000066 sameas[op[name]] = sym
67 }
drh3a40f692008-01-04 16:50:09 +000068 sub(",","",$i)
danielk19777a5147c2005-03-29 13:07:00 +000069 if($i=="no-push"){
70 nopush[name] = 1
drh3a40f692008-01-04 16:50:09 +000071 }else if($i=="out1"){
72 out1[name] = 1
73 }else if($i=="out2"){
74 out2[name] = 2
75 }else if($i=="out3"){
76 out3[name] = 3
77 }else if($i=="in1"){
78 in1[name] = 1
79 }else if($i=="in2"){
80 in2[name] = 1
81 }else if($i=="in3"){
82 in3[name] = 1
83 }else if($i=="jump"){
84 jump[name] = 1
drhf2bc0132004-10-04 13:19:23 +000085 }
86 }
87}
88
89# Assign numbers to all opcodes and output the result.
90END {
91 cnt = 0
drhdaa28ff2004-12-10 17:17:18 +000092 max = 0
drhb327f772004-10-06 15:03:57 +000093 print "/* Automatically generated. Do not edit */"
94 print "/* See the mkopcodeh.awk script for details */"
drhf2bc0132004-10-04 13:19:23 +000095 for(name in op){
96 if( op[name]<0 ){
97 cnt++
98 while( used[cnt] ) cnt++
99 op[name] = cnt
100 }
drhdaa28ff2004-12-10 17:17:18 +0000101 used[op[name]] = 1;
102 if( op[name]>max ) max = op[name]
drh0602c2e2005-01-21 17:07:22 +0000103 printf "#define %-25s %15d", name, op[name]
104 if( sameas[op[name]] ) {
danielk197724c8ab82005-02-09 01:40:23 +0000105 printf " /* same as %-12s*/", sameas[op[name]]
drh0602c2e2005-01-21 17:07:22 +0000106 }
107 printf "\n"
108
drhf2bc0132004-10-04 13:19:23 +0000109 }
drhdaa28ff2004-12-10 17:17:18 +0000110 seenUnused = 0;
111 for(i=1; i<max; i++){
112 if( !used[i] ){
113 if( !seenUnused ){
114 printf "\n/* The following opcode values are never used */\n"
115 seenUnused = 1
116 }
drh0602c2e2005-01-21 17:07:22 +0000117 printf "#define %-25s %15d\n", sprintf( "OP_NotUsed_%-3d", i ), i
drhdaa28ff2004-12-10 17:17:18 +0000118 }
119 }
danielk1977bc04f852005-03-29 08:26:13 +0000120
drh3a40f692008-01-04 16:50:09 +0000121 # Generate the bitvectors:
122 #
123 # bit 0: jump
124 # bit 1: output on P1
125 # bit 2: output on P2
126 # bit 3: output on P3
127 # bit 4: input on P1
128 # bit 5: input on P2
129 # bit 6: input on P3
130 # bit 7: pushes a result onto stack
131 #
132 for(i=0; i<=max; i++) bv[i] = 0;
danielk1977bc04f852005-03-29 08:26:13 +0000133 for(name in op){
drh3a40f692008-01-04 16:50:09 +0000134 x = op[name]
135 if( jump[name] ) bv[x] += 0x01;
136 if( out1[name] ) bv[x] += 0x02;
137 if( out2[name] ) bv[x] += 0x04;
138 if( out3[name] ) bv[x] += 0x08;
139 if( in1[name] ) bv[x] += 0x10;
140 if( in2[name] ) bv[x] += 0x20;
141 if( in3[name] ) bv[x] += 0x40;
142 if( !nopush[name] ) bv[x] += 0x80;
danielk1977bc04f852005-03-29 08:26:13 +0000143 }
drh3a40f692008-01-04 16:50:09 +0000144 print "\n"
145 print "/* Properties such as \"out2\" or \"jump\" that are specified in"
146 print "** comments following the "case" for each opcode in the vdbe.c"
147 print "** are encoded into bitvectors as follows:"
148 print "*/"
149 print "#define OPFLG_JUMP 0x01 /* jump: P2 holds a jump target */"
150 print "#define OPFLG_OUT1 0x02 /* out1: P1 specifies output reg */"
151 print "#define OPFLG_OUT2 0x04 /* out2: P2 specifies output reg */"
152 print "#define OPFLG_OUT3 0x08 /* out3: P3 specifies output reg */"
153 print "#define OPFLG_IN1 0x10 /* in1: P1 is an input reg */"
154 print "#define OPFLG_IN2 0x20 /* in2: P2 is an input reg */"
155 print "#define OPFLG_IN3 0x40 /* in3: P3 is an input reg */"
156 print "#define OPFLG_PUSH 0x80 /* omits no-push: Does not push */"
157 print "#define OPFLG_INITIALIZER {\\"
158 for(i=0; i<=max; i++){
159 printf " 0x%02x,", bv[i]
160 if( i%10==9 ) printf("\\\n");
danielk1977bc04f852005-03-29 08:26:13 +0000161 }
drh3a40f692008-01-04 16:50:09 +0000162 print "}"
drhf2bc0132004-10-04 13:19:23 +0000163}