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.
|
|
|
|
#
|
2004-10-26 00:33:44 +04:00
|
|
|
# We go to the trouble of making some OP_ value the same as TK_ values
|
|
|
|
# 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,
|
|
|
|
# code to translation from one to the other is avoided. This makes the
|
|
|
|
# code generator run (infinitesimally) faster and more importantly it makes
|
|
|
|
# the total library smaller.
|
|
|
|
#
|
2004-10-04 17:19:23 +04:00
|
|
|
|
|
|
|
# Remember the TK_ values from the parse.h file
|
|
|
|
/^#define TK_/ {
|
|
|
|
tk[$2] = $3
|
|
|
|
}
|
|
|
|
|
|
|
|
# Scan for "case OP_aaaa:" lines in the vdbe.c file
|
|
|
|
/^case OP_/ {
|
|
|
|
name = $2
|
|
|
|
gsub(/:/,"",name)
|
2004-10-10 23:11:35 +04:00
|
|
|
gsub("\r","",name)
|
2004-10-04 17:19:23 +04:00
|
|
|
op[name] = -1
|
|
|
|
for(i=3; i<NF-2; i++){
|
|
|
|
if($i=="same" && $(i+1)=="as"){
|
|
|
|
op[name] = tk[$(i+2)]
|
|
|
|
used[op[name]] = 1
|
2005-01-21 20:07:22 +03:00
|
|
|
sameas[op[name]] = $(i+2)
|
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]] ) {
|
|
|
|
printf " /*same as %-12s*/", sameas[op[name]]
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2004-10-04 17:19:23 +04:00
|
|
|
}
|