blob: f8da18e3061bb36ad7a61280158c33ca1af707cd [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#
drhb24a2002009-11-02 18:14:50 +000028# case OP_aaaa: /* jump, in1, in2, in3, out2-prerelease, out3 */
drhfa3b19e2005-11-24 22:22:29 +000029#
drhb24a2002009-11-02 18:14:50 +000030# When such comments are found on an opcode, it means that certain
31# properties apply to that opcode. Set corresponding flags using the
32# OPFLG_INITIALIZER macro.
drhfa3b19e2005-11-24 22:22:29 +000033#
drhf2bc0132004-10-04 13:19:23 +000034
danielk1977bc04f852005-03-29 08:26:13 +000035
drhf2bc0132004-10-04 13:19:23 +000036# Remember the TK_ values from the parse.h file
37/^#define TK_/ {
drh8c8a8c42013-08-06 07:45:08 +000038 tk[$2] = 0+$3 # tk[x] holds the numeric value for TK symbol X
drhf2bc0132004-10-04 13:19:23 +000039}
40
drh81316f82013-10-29 20:40:47 +000041# Find "/* Opcode: " lines in the vdbe.c file. Each one introduces
42# a new opcode. Remember which parameters are used.
43/^.. Opcode: / {
44 currentOp = "OP_" $3
45 m = 0
46 for(i=4; i<=NF; i++){
47 x = $i
48 if( x=="P1" ) m += 1
49 if( x=="P2" ) m += 2
50 if( x=="P3" ) m += 4
51 if( x=="P4" ) m += 8
52 if( x=="P5" ) m += 16
53 }
54 paramused[currentOp] = m
55}
56
57# Find "** Synopsis: " lines that follow Opcode:
58/^.. Synopsis: / {
59 if( currentOp ){
60 x = $3
61 for(i=4; i<=NF; i++){
62 x = x " " $i
63 }
64 synopsis[currentOp] = x
65 }
66}
67
drhf2bc0132004-10-04 13:19:23 +000068# Scan for "case OP_aaaa:" lines in the vdbe.c file
69/^case OP_/ {
70 name = $2
drhb726ee62005-09-05 20:35:25 +000071 sub(/:/,"",name)
72 sub("\r","",name)
drh8c8a8c42013-08-06 07:45:08 +000073 op[name] = -1 # op[x] holds the numeric value for OP symbol x
drh3a40f692008-01-04 16:50:09 +000074 jump[name] = 0
drh4c583122008-01-04 22:01:03 +000075 out2_prerelease[name] = 0
drhb1fdb2a2008-01-05 04:06:03 +000076 in1[name] = 0
77 in2[name] = 0
78 in3[name] = 0
drh93952eb2009-11-13 19:43:43 +000079 out2[name] = 0
drhb1fdb2a2008-01-05 04:06:03 +000080 out3[name] = 0
danielk1977bc04f852005-03-29 08:26:13 +000081 for(i=3; i<NF; i++){
drhf2bc0132004-10-04 13:19:23 +000082 if($i=="same" && $(i+1)=="as"){
danielk1977bc04f852005-03-29 08:26:13 +000083 sym = $(i+2)
84 sub(/,/,"",sym)
drh8c8a8c42013-08-06 07:45:08 +000085 val = tk[sym]
86 op[name] = val
87 used[val] = 1
88 sameas[val] = sym
89 def[val] = name
danielk1977bc04f852005-03-29 08:26:13 +000090 }
drh4aeb7bf2008-01-04 19:12:35 +000091 x = $i
92 sub(",","",x)
drh9cbf3422008-01-17 16:22:13 +000093 if(x=="jump"){
drh3a40f692008-01-04 16:50:09 +000094 jump[name] = 1
drh4c583122008-01-04 22:01:03 +000095 }else if(x=="out2-prerelease"){
96 out2_prerelease[name] = 1
drhb1fdb2a2008-01-05 04:06:03 +000097 }else if(x=="in1"){
98 in1[name] = 1
99 }else if(x=="in2"){
100 in2[name] = 1
101 }else if(x=="in3"){
102 in3[name] = 1
drh93952eb2009-11-13 19:43:43 +0000103 }else if(x=="out2"){
104 out2[name] = 1
drhb1fdb2a2008-01-05 04:06:03 +0000105 }else if(x=="out3"){
106 out3[name] = 1
drhf2bc0132004-10-04 13:19:23 +0000107 }
108 }
drhb24a2002009-11-02 18:14:50 +0000109 order[n_op++] = name;
drhf2bc0132004-10-04 13:19:23 +0000110}
111
112# Assign numbers to all opcodes and output the result.
113END {
114 cnt = 0
drhdaa28ff2004-12-10 17:17:18 +0000115 max = 0
drhb327f772004-10-06 15:03:57 +0000116 print "/* Automatically generated. Do not edit */"
117 print "/* See the mkopcodeh.awk script for details */"
drh91fd4d42008-01-19 20:11:25 +0000118 op["OP_Noop"] = -1;
drhccaf7732009-11-02 18:44:58 +0000119 order[n_op++] = "OP_Noop";
drh91fd4d42008-01-19 20:11:25 +0000120 op["OP_Explain"] = -1;
drhccaf7732009-11-02 18:44:58 +0000121 order[n_op++] = "OP_Explain";
drh8c8a8c42013-08-06 07:45:08 +0000122
123 # Assign small values to opcodes that are processed by resolveP2Values()
124 # to make code generation for the switch() statement smaller and faster.
125 for(i=0; i<n_op; i++){
126 name = order[i];
127 if( op[name]>=0 ) continue;
128 if( name=="OP_Function" \
129 || name=="OP_AggStep" \
130 || name=="OP_Transaction" \
131 || name=="OP_AutoCommit" \
132 || name=="OP_Savepoint" \
133 || name=="OP_Checkpoint" \
134 || name=="OP_Vacuum" \
135 || name=="OP_JournalMode" \
136 || name=="OP_VUpdate" \
137 || name=="OP_VFilter" \
138 || name=="OP_Next" \
139 || name=="OP_SorterNext" \
140 || name=="OP_Prev" \
141 ){
142 cnt++
143 while( used[cnt] ) cnt++
144 op[name] = cnt
145 used[cnt] = 1
146 def[cnt] = name
147 }
148 }
149
150 # Generate the numeric values for opcodes
drhb24a2002009-11-02 18:14:50 +0000151 for(i=0; i<n_op; i++){
152 name = order[i];
drhf2bc0132004-10-04 13:19:23 +0000153 if( op[name]<0 ){
154 cnt++
155 while( used[cnt] ) cnt++
156 op[name] = cnt
drh8c8a8c42013-08-06 07:45:08 +0000157 used[cnt] = 1
158 def[cnt] = name
drhf2bc0132004-10-04 13:19:23 +0000159 }
drh8c8a8c42013-08-06 07:45:08 +0000160 }
161 max = cnt
162 for(i=1; i<=max; i++){
163 if( !used[i] ){
164 def[i] = "OP_NotUsed_" i
165 }
drh81316f82013-10-29 20:40:47 +0000166 printf "#define %-16s %3d", def[i], i
167 com = ""
drh8c8a8c42013-08-06 07:45:08 +0000168 if( sameas[i] ){
drh81316f82013-10-29 20:40:47 +0000169 com = "same as " sameas[i]
170 }
171 x = synopsis[def[i]]
172 if( x ){
173 if( com=="" ){
174 com = "synopsis: " x
175 } else {
176 com = com ", synopsis: " x
177 }
178 }
179 if( com!="" ){
180 printf " /* %-42s */", com
181 }
drh0602c2e2005-01-21 17:07:22 +0000182 printf "\n"
drhdaa28ff2004-12-10 17:17:18 +0000183 }
danielk1977bc04f852005-03-29 08:26:13 +0000184
drh3a40f692008-01-04 16:50:09 +0000185 # Generate the bitvectors:
186 #
187 # bit 0: jump
drh4c583122008-01-04 22:01:03 +0000188 # bit 1: pushes a result onto stack
189 # bit 2: output to p1. release p1 before opcode runs
drh3a40f692008-01-04 16:50:09 +0000190 #
drh8c8a8c42013-08-06 07:45:08 +0000191 for(i=0; i<=max; i++){
192 name = def[i]
drh4aeb7bf2008-01-04 19:12:35 +0000193 a0 = a1 = a2 = a3 = a4 = a5 = a6 = a7 = 0
194 if( jump[name] ) a0 = 1;
drh9cbf3422008-01-17 16:22:13 +0000195 if( out2_prerelease[name] ) a1 = 2;
196 if( in1[name] ) a2 = 4;
197 if( in2[name] ) a3 = 8;
198 if( in3[name] ) a4 = 16;
drh93952eb2009-11-13 19:43:43 +0000199 if( out2[name] ) a5 = 32;
200 if( out3[name] ) a6 = 64;
drh8c8a8c42013-08-06 07:45:08 +0000201 bv[i] = a0+a1+a2+a3+a4+a5+a6+a7;
danielk1977bc04f852005-03-29 08:26:13 +0000202 }
drh3a40f692008-01-04 16:50:09 +0000203 print "\n"
204 print "/* Properties such as \"out2\" or \"jump\" that are specified in"
drh3de120b2008-01-10 00:08:43 +0000205 print "** comments following the \"case\" for each opcode in the vdbe.c"
drh3a40f692008-01-04 16:50:09 +0000206 print "** are encoded into bitvectors as follows:"
207 print "*/"
drh5b6afba2008-01-05 16:29:28 +0000208 print "#define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */"
drh9cbf3422008-01-17 16:22:13 +0000209 print "#define OPFLG_OUT2_PRERELEASE 0x0002 /* out2-prerelease: */"
210 print "#define OPFLG_IN1 0x0004 /* in1: P1 is an input */"
211 print "#define OPFLG_IN2 0x0008 /* in2: P2 is an input */"
212 print "#define OPFLG_IN3 0x0010 /* in3: P3 is an input */"
drh93952eb2009-11-13 19:43:43 +0000213 print "#define OPFLG_OUT2 0x0020 /* out2: P2 is an output */"
214 print "#define OPFLG_OUT3 0x0040 /* out3: P3 is an output */"
drh3a40f692008-01-04 16:50:09 +0000215 print "#define OPFLG_INITIALIZER {\\"
216 for(i=0; i<=max; i++){
drh5b6afba2008-01-05 16:29:28 +0000217 if( i%8==0 ) printf("/* %3d */",i)
drh9cbf3422008-01-17 16:22:13 +0000218 printf " 0x%02x,", bv[i]
drh4c583122008-01-04 22:01:03 +0000219 if( i%8==7 ) printf("\\\n");
danielk1977bc04f852005-03-29 08:26:13 +0000220 }
drh3a40f692008-01-04 16:50:09 +0000221 print "}"
drh81316f82013-10-29 20:40:47 +0000222 if( 0 ){
223 print "\n/* Bitmask to indicate which fields (P1..P5) of each opcode are"
224 print "** actually used.\n*/"
225 print "#define OP_PARAM_USED_INITIALIZER {\\"
226 for(i=0; i<=max; i++){
227 if( i%8==0 ) printf("/* %3d */",i)
228 printf " 0x%02x,", paramused[def[i]]
229 if( i%8==7 ) printf("\\\n");
230 }
231 print "}"
232 }
drhf2bc0132004-10-04 13:19:23 +0000233}