2004-10-04 17:19:23 +04:00
|
|
|
#!/usr/bin/awk -f
|
|
|
|
#
|
2004-10-26 00:33:44 +04:00
|
|
|
# Generate the file opcodes.h.
|
|
|
|
#
|
2004-10-04 17:19:23 +04:00
|
|
|
# This AWK script scans a concatenation of the parse.h output file from the
|
|
|
|
# parser and the vdbe.c source file in order to generate the opcodes numbers
|
|
|
|
# for all opcodes.
|
|
|
|
#
|
|
|
|
# The lines of the vdbe.c that we are interested in are of the form:
|
|
|
|
#
|
|
|
|
# case OP_aaaa: /* same as TK_bbbbb */
|
|
|
|
#
|
|
|
|
# The TK_ comment is optional. If it is present, then the value assigned to
|
|
|
|
# the OP_ is the same as the TK_ value. If missing, the OP_ value is assigned
|
|
|
|
# a small integer that is different from every other OP_ value.
|
|
|
|
#
|
2006-01-26 17:29:58 +03:00
|
|
|
# We go to the trouble of making some OP_ values the same as TK_ values
|
2004-10-26 00:33:44 +04:00
|
|
|
# as an optimization. During parsing, things like expression operators
|
|
|
|
# are coded with TK_ values such as TK_ADD, TK_DIVIDE, and so forth. Later
|
|
|
|
# during code generation, we need to generate corresponding opcodes like
|
|
|
|
# OP_Add and OP_Divide. By making TK_ADD==OP_Add and TK_DIVIDE==OP_Divide,
|
2006-01-26 17:29:58 +03:00
|
|
|
# code to translate from one to the other is avoided. This makes the
|
2004-10-26 00:33:44 +04:00
|
|
|
# code generator run (infinitesimally) faster and more importantly it makes
|
2006-01-26 17:29:58 +03:00
|
|
|
# the library footprint smaller.
|
2004-10-26 00:33:44 +04:00
|
|
|
#
|
2005-11-25 01:22:29 +03:00
|
|
|
# This script also scans for lines of the form:
|
|
|
|
#
|
2009-11-02 21:14:50 +03:00
|
|
|
# case OP_aaaa: /* jump, in1, in2, in3, out2-prerelease, out3 */
|
2005-11-25 01:22:29 +03:00
|
|
|
#
|
2009-11-02 21:14:50 +03:00
|
|
|
# When such comments are found on an opcode, it means that certain
|
|
|
|
# properties apply to that opcode. Set corresponding flags using the
|
|
|
|
# OPFLG_INITIALIZER macro.
|
2005-11-25 01:22:29 +03:00
|
|
|
#
|
2004-10-04 17:19:23 +04:00
|
|
|
|
2005-03-29 12:26:13 +04:00
|
|
|
|
2004-10-04 17:19:23 +04:00
|
|
|
# Remember the TK_ values from the parse.h file
|
|
|
|
/^#define TK_/ {
|
2013-08-06 11:45:08 +04:00
|
|
|
tk[$2] = 0+$3 # tk[x] holds the numeric value for TK symbol X
|
2004-10-04 17:19:23 +04:00
|
|
|
}
|
|
|
|
|
2013-10-30 00:40:47 +04:00
|
|
|
# Find "/* Opcode: " lines in the vdbe.c file. Each one introduces
|
|
|
|
# a new opcode. Remember which parameters are used.
|
|
|
|
/^.. Opcode: / {
|
|
|
|
currentOp = "OP_" $3
|
|
|
|
m = 0
|
|
|
|
for(i=4; i<=NF; i++){
|
|
|
|
x = $i
|
|
|
|
if( x=="P1" ) m += 1
|
|
|
|
if( x=="P2" ) m += 2
|
|
|
|
if( x=="P3" ) m += 4
|
|
|
|
if( x=="P4" ) m += 8
|
|
|
|
if( x=="P5" ) m += 16
|
|
|
|
}
|
|
|
|
paramused[currentOp] = m
|
|
|
|
}
|
|
|
|
|
|
|
|
# Find "** Synopsis: " lines that follow Opcode:
|
|
|
|
/^.. Synopsis: / {
|
|
|
|
if( currentOp ){
|
|
|
|
x = $3
|
|
|
|
for(i=4; i<=NF; i++){
|
|
|
|
x = x " " $i
|
|
|
|
}
|
|
|
|
synopsis[currentOp] = x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-04 17:19:23 +04:00
|
|
|
# Scan for "case OP_aaaa:" lines in the vdbe.c file
|
|
|
|
/^case OP_/ {
|
|
|
|
name = $2
|
2005-09-06 00:35:25 +04:00
|
|
|
sub(/:/,"",name)
|
|
|
|
sub("\r","",name)
|
2013-08-06 11:45:08 +04:00
|
|
|
op[name] = -1 # op[x] holds the numeric value for OP symbol x
|
2008-01-04 19:50:09 +03:00
|
|
|
jump[name] = 0
|
2008-01-05 07:06:03 +03:00
|
|
|
in1[name] = 0
|
|
|
|
in2[name] = 0
|
|
|
|
in3[name] = 0
|
2009-11-13 22:43:43 +03:00
|
|
|
out2[name] = 0
|
2008-01-05 07:06:03 +03:00
|
|
|
out3[name] = 0
|
2005-03-29 12:26:13 +04:00
|
|
|
for(i=3; i<NF; i++){
|
2004-10-04 17:19:23 +04:00
|
|
|
if($i=="same" && $(i+1)=="as"){
|
2005-03-29 12:26:13 +04:00
|
|
|
sym = $(i+2)
|
|
|
|
sub(/,/,"",sym)
|
2013-08-06 11:45:08 +04:00
|
|
|
val = tk[sym]
|
|
|
|
op[name] = val
|
|
|
|
used[val] = 1
|
|
|
|
sameas[val] = sym
|
|
|
|
def[val] = name
|
2005-03-29 12:26:13 +04:00
|
|
|
}
|
2008-01-04 22:12:35 +03:00
|
|
|
x = $i
|
|
|
|
sub(",","",x)
|
2008-01-17 19:22:13 +03:00
|
|
|
if(x=="jump"){
|
2008-01-04 19:50:09 +03:00
|
|
|
jump[name] = 1
|
2008-01-05 07:06:03 +03:00
|
|
|
}else if(x=="in1"){
|
|
|
|
in1[name] = 1
|
|
|
|
}else if(x=="in2"){
|
|
|
|
in2[name] = 1
|
|
|
|
}else if(x=="in3"){
|
|
|
|
in3[name] = 1
|
2009-11-13 22:43:43 +03:00
|
|
|
}else if(x=="out2"){
|
|
|
|
out2[name] = 1
|
2008-01-05 07:06:03 +03:00
|
|
|
}else if(x=="out3"){
|
|
|
|
out3[name] = 1
|
2004-10-04 17:19:23 +04:00
|
|
|
}
|
|
|
|
}
|
2009-11-02 21:14:50 +03:00
|
|
|
order[n_op++] = name;
|
2004-10-04 17:19:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
# Assign numbers to all opcodes and output the result.
|
|
|
|
END {
|
|
|
|
cnt = 0
|
2004-12-10 20:17:18 +03:00
|
|
|
max = 0
|
2004-10-06 19:03:57 +04:00
|
|
|
print "/* Automatically generated. Do not edit */"
|
|
|
|
print "/* See the mkopcodeh.awk script for details */"
|
2008-01-19 23:11:25 +03:00
|
|
|
op["OP_Noop"] = -1;
|
2009-11-02 21:44:58 +03:00
|
|
|
order[n_op++] = "OP_Noop";
|
2008-01-19 23:11:25 +03:00
|
|
|
op["OP_Explain"] = -1;
|
2009-11-02 21:44:58 +03:00
|
|
|
order[n_op++] = "OP_Explain";
|
2013-08-06 11:45:08 +04:00
|
|
|
|
|
|
|
# Assign small values to opcodes that are processed by resolveP2Values()
|
|
|
|
# to make code generation for the switch() statement smaller and faster.
|
2009-11-02 21:14:50 +03:00
|
|
|
for(i=0; i<n_op; i++){
|
|
|
|
name = order[i];
|
2013-08-06 11:45:08 +04:00
|
|
|
if( op[name]>=0 ) continue;
|
2015-06-26 21:16:52 +03:00
|
|
|
if( name=="OP_Transaction" \
|
2013-08-06 11:45:08 +04:00
|
|
|
|| name=="OP_AutoCommit" \
|
|
|
|
|| name=="OP_Savepoint" \
|
|
|
|
|| name=="OP_Checkpoint" \
|
|
|
|
|| name=="OP_Vacuum" \
|
|
|
|
|| name=="OP_JournalMode" \
|
|
|
|
|| name=="OP_VUpdate" \
|
|
|
|
|| name=="OP_VFilter" \
|
|
|
|
|| name=="OP_Next" \
|
2013-11-21 21:24:18 +04:00
|
|
|
|| name=="OP_NextIfOpen" \
|
2013-08-06 11:45:08 +04:00
|
|
|
|| name=="OP_SorterNext" \
|
|
|
|
|| name=="OP_Prev" \
|
2013-11-21 21:24:18 +04:00
|
|
|
|| name=="OP_PrevIfOpen" \
|
2013-08-06 11:45:08 +04:00
|
|
|
){
|
2004-10-04 17:19:23 +04:00
|
|
|
cnt++
|
|
|
|
while( used[cnt] ) cnt++
|
|
|
|
op[name] = cnt
|
2013-08-06 11:45:08 +04:00
|
|
|
used[cnt] = 1
|
|
|
|
def[cnt] = name
|
2004-10-04 17:19:23 +04:00
|
|
|
}
|
2013-08-06 11:45:08 +04:00
|
|
|
}
|
2005-01-21 20:07:22 +03:00
|
|
|
|
2013-08-06 11:45:08 +04:00
|
|
|
# Generate the numeric values for opcodes
|
|
|
|
for(i=0; i<n_op; i++){
|
|
|
|
name = order[i];
|
|
|
|
if( op[name]<0 ){
|
|
|
|
cnt++
|
|
|
|
while( used[cnt] ) cnt++
|
|
|
|
op[name] = cnt
|
|
|
|
used[cnt] = 1
|
|
|
|
def[cnt] = name
|
|
|
|
}
|
2004-10-04 17:19:23 +04:00
|
|
|
}
|
2013-08-06 11:45:08 +04:00
|
|
|
max = cnt
|
|
|
|
for(i=1; i<=max; i++){
|
2004-12-10 20:17:18 +03:00
|
|
|
if( !used[i] ){
|
2013-08-06 11:45:08 +04:00
|
|
|
def[i] = "OP_NotUsed_" i
|
2004-12-10 20:17:18 +03:00
|
|
|
}
|
2013-10-30 00:40:47 +04:00
|
|
|
printf "#define %-16s %3d", def[i], i
|
|
|
|
com = ""
|
2013-08-06 11:45:08 +04:00
|
|
|
if( sameas[i] ){
|
2013-10-30 00:40:47 +04:00
|
|
|
com = "same as " sameas[i]
|
|
|
|
}
|
|
|
|
x = synopsis[def[i]]
|
|
|
|
if( x ){
|
|
|
|
if( com=="" ){
|
|
|
|
com = "synopsis: " x
|
|
|
|
} else {
|
|
|
|
com = com ", synopsis: " x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( com!="" ){
|
|
|
|
printf " /* %-42s */", com
|
|
|
|
}
|
2013-08-06 11:45:08 +04:00
|
|
|
printf "\n"
|
2004-12-10 20:17:18 +03:00
|
|
|
}
|
2005-03-29 12:26:13 +04:00
|
|
|
|
2008-01-04 19:50:09 +03:00
|
|
|
# Generate the bitvectors:
|
|
|
|
#
|
|
|
|
# bit 0: jump
|
2008-01-05 01:01:03 +03:00
|
|
|
# bit 1: pushes a result onto stack
|
|
|
|
# bit 2: output to p1. release p1 before opcode runs
|
2008-01-04 19:50:09 +03:00
|
|
|
#
|
2013-08-06 11:45:08 +04:00
|
|
|
for(i=0; i<=max; i++){
|
|
|
|
name = def[i]
|
2008-01-04 22:12:35 +03:00
|
|
|
a0 = a1 = a2 = a3 = a4 = a5 = a6 = a7 = 0
|
|
|
|
if( jump[name] ) a0 = 1;
|
2015-04-13 22:14:06 +03:00
|
|
|
if( in1[name] ) a2 = 2;
|
|
|
|
if( in2[name] ) a3 = 4;
|
|
|
|
if( in3[name] ) a4 = 8;
|
|
|
|
if( out2[name] ) a5 = 16;
|
|
|
|
if( out3[name] ) a6 = 32;
|
|
|
|
bv[i] = a0+a1+a2+a3+a4+a5+a6;
|
2005-03-29 12:26:13 +04:00
|
|
|
}
|
2008-01-04 19:50:09 +03:00
|
|
|
print "\n"
|
|
|
|
print "/* Properties such as \"out2\" or \"jump\" that are specified in"
|
2008-01-10 03:08:43 +03:00
|
|
|
print "** comments following the \"case\" for each opcode in the vdbe.c"
|
2008-01-04 19:50:09 +03:00
|
|
|
print "** are encoded into bitvectors as follows:"
|
|
|
|
print "*/"
|
2008-01-05 19:29:28 +03:00
|
|
|
print "#define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */"
|
2015-04-13 22:14:06 +03:00
|
|
|
print "#define OPFLG_IN1 0x0002 /* in1: P1 is an input */"
|
|
|
|
print "#define OPFLG_IN2 0x0004 /* in2: P2 is an input */"
|
|
|
|
print "#define OPFLG_IN3 0x0008 /* in3: P3 is an input */"
|
|
|
|
print "#define OPFLG_OUT2 0x0010 /* out2: P2 is an output */"
|
|
|
|
print "#define OPFLG_OUT3 0x0020 /* out3: P3 is an output */"
|
2008-01-04 19:50:09 +03:00
|
|
|
print "#define OPFLG_INITIALIZER {\\"
|
|
|
|
for(i=0; i<=max; i++){
|
2008-01-05 19:29:28 +03:00
|
|
|
if( i%8==0 ) printf("/* %3d */",i)
|
2008-01-17 19:22:13 +03:00
|
|
|
printf " 0x%02x,", bv[i]
|
2008-01-05 01:01:03 +03:00
|
|
|
if( i%8==7 ) printf("\\\n");
|
2005-03-29 12:26:13 +04:00
|
|
|
}
|
2008-01-04 19:50:09 +03:00
|
|
|
print "}"
|
2013-10-30 00:40:47 +04:00
|
|
|
if( 0 ){
|
|
|
|
print "\n/* Bitmask to indicate which fields (P1..P5) of each opcode are"
|
|
|
|
print "** actually used.\n*/"
|
|
|
|
print "#define OP_PARAM_USED_INITIALIZER {\\"
|
|
|
|
for(i=0; i<=max; i++){
|
|
|
|
if( i%8==0 ) printf("/* %3d */",i)
|
|
|
|
printf " 0x%02x,", paramused[def[i]]
|
|
|
|
if( i%8==7 ) printf("\\\n");
|
|
|
|
}
|
|
|
|
print "}"
|
|
|
|
}
|
2004-10-04 17:19:23 +04:00
|
|
|
}
|