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:
|
|
|
|
#
|
|
|
|
# case OP_aaaa: /* no-push */
|
|
|
|
#
|
|
|
|
# When the no-push comment is found on an opcode, it means that that
|
2006-01-26 17:29:58 +03:00
|
|
|
# opcode does not leave a result on the stack. By identifying which
|
2005-11-25 01:22:29 +03:00
|
|
|
# opcodes leave results on the stack it is possible to determine a
|
|
|
|
# much smaller upper bound on the size of the stack. This allows
|
|
|
|
# a smaller stack to be allocated, which is important to embedded
|
|
|
|
# systems with limited memory space. This script generates a series
|
|
|
|
# of "NOPUSH_MASK" defines that contain bitmaps of opcodes that leave
|
|
|
|
# results on the stack. The NOPUSH_MASK defines are used in vdbeaux.c
|
|
|
|
# to help determine the maximum stack size.
|
|
|
|
#
|
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_/ {
|
2007-10-12 22:36:26 +04:00
|
|
|
tk[$2] = 0+$3
|
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)
|
2004-10-04 17:19:23 +04:00
|
|
|
op[name] = -1
|
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)
|
|
|
|
op[name] = tk[sym]
|
2004-10-04 17:19:23 +04:00
|
|
|
used[op[name]] = 1
|
2005-03-29 12:26:13 +04:00
|
|
|
sameas[op[name]] = sym
|
|
|
|
}
|
2005-03-29 17:07:00 +04:00
|
|
|
if($i=="no-push"){
|
|
|
|
nopush[name] = 1
|
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 */"
|
2004-10-04 17:19:23 +04:00
|
|
|
for(name in op){
|
|
|
|
if( op[name]<0 ){
|
|
|
|
cnt++
|
|
|
|
while( used[cnt] ) cnt++
|
|
|
|
op[name] = cnt
|
|
|
|
}
|
2004-12-10 20:17:18 +03:00
|
|
|
used[op[name]] = 1;
|
|
|
|
if( op[name]>max ) max = op[name]
|
2005-01-21 20:07:22 +03:00
|
|
|
printf "#define %-25s %15d", name, op[name]
|
|
|
|
if( sameas[op[name]] ) {
|
2005-02-09 04:40:23 +03:00
|
|
|
printf " /* same as %-12s*/", sameas[op[name]]
|
2005-01-21 20:07:22 +03:00
|
|
|
}
|
|
|
|
printf "\n"
|
|
|
|
|
2004-10-04 17:19:23 +04:00
|
|
|
}
|
2004-12-10 20:17:18 +03:00
|
|
|
seenUnused = 0;
|
|
|
|
for(i=1; i<max; i++){
|
|
|
|
if( !used[i] ){
|
|
|
|
if( !seenUnused ){
|
|
|
|
printf "\n/* The following opcode values are never used */\n"
|
|
|
|
seenUnused = 1
|
|
|
|
}
|
2005-01-21 20:07:22 +03:00
|
|
|
printf "#define %-25s %15d\n", sprintf( "OP_NotUsed_%-3d", i ), i
|
2004-12-10 20:17:18 +03:00
|
|
|
}
|
|
|
|
}
|
2005-03-29 12:26:13 +04:00
|
|
|
|
|
|
|
# Generate the 10 16-bit bitmasks used by function opcodeUsesStack()
|
|
|
|
# in vdbeaux.c. See comments in that function for details.
|
|
|
|
#
|
2005-03-29 17:07:00 +04:00
|
|
|
nopush[0] = 0 # 0..15
|
|
|
|
nopush[1] = 0 # 16..31
|
|
|
|
nopush[2] = 0 # 32..47
|
|
|
|
nopush[3] = 0 # 48..63
|
|
|
|
nopush[4] = 0 # 64..79
|
|
|
|
nopush[5] = 0 # 80..95
|
|
|
|
nopush[6] = 0 # 96..111
|
|
|
|
nopush[7] = 0 # 112..127
|
|
|
|
nopush[8] = 0 # 128..143
|
|
|
|
nopush[9] = 0 # 144..159
|
2005-03-29 12:26:13 +04:00
|
|
|
for(name in op){
|
2005-03-29 17:07:00 +04:00
|
|
|
if( nopush[name] ){
|
2005-03-29 12:26:13 +04:00
|
|
|
n = op[name]
|
|
|
|
j = n%16
|
|
|
|
i = ((n - j)/16)
|
2005-03-29 17:07:00 +04:00
|
|
|
nopush[i] = nopush[i] + (2^j)
|
2005-03-29 12:26:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printf "\n"
|
2006-01-26 17:29:58 +03:00
|
|
|
print "/* Opcodes that are guaranteed to never push a value onto the stack"
|
|
|
|
print "** contain a 1 their corresponding position of the following mask"
|
|
|
|
print "** set. See the opcodeNoPush() function in vdbeaux.c */"
|
2005-03-29 12:26:13 +04:00
|
|
|
for(i=0; i<10; i++){
|
2006-01-26 17:29:58 +03:00
|
|
|
printf "#define NOPUSH_MASK_%d 0x%04x\n", i, nopush[i]
|
2005-03-29 12:26:13 +04:00
|
|
|
}
|
2004-10-04 17:19:23 +04:00
|
|
|
}
|