xref: /sqlite-3.40.0/tool/mkopcodec.tcl (revision 20de9f6c)
17651e0a4Sdrh#!/usr/bin/tclsh
27651e0a4Sdrh#
37651e0a4Sdrh# This TCL script scans the opcodes.h file (which is itself generated by
47651e0a4Sdrh# another TCL script) and uses the information gleaned to create the
57651e0a4Sdrh# opcodes.c source file.
67651e0a4Sdrh#
77651e0a4Sdrh# Opcodes.c contains strings which are the symbolic names for the various
87651e0a4Sdrh# opcodes used by the VDBE.  These strings are used when disassembling a
97651e0a4Sdrh# VDBE program during tracing or as a result of the EXPLAIN keyword.
107651e0a4Sdrh#
117651e0a4Sdrhputs "/* Automatically generated.  Do not edit */"
127651e0a4Sdrhputs "/* See the tool/mkopcodec.tcl script for details. */"
137651e0a4Sdrhputs "#if !defined(SQLITE_OMIT_EXPLAIN) \\"
147651e0a4Sdrhputs " || defined(VDBE_PROFILE) \\"
157651e0a4Sdrhputs " || defined(SQLITE_DEBUG)"
167651e0a4Sdrhputs "#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG)"
177651e0a4Sdrhputs "# define OpHelp(X) \"\\0\" X"
187651e0a4Sdrhputs "#else"
197651e0a4Sdrhputs "# define OpHelp(X)"
207651e0a4Sdrhputs "#endif"
217651e0a4Sdrhputs "const char *sqlite3OpcodeName(int i)\173"
22ed94af5eSdrhputs " static const char *const azName\[\] = \173"
237651e0a4Sdrhset mx 0
247651e0a4Sdrh
25*20de9f6cSdrhset in [open [lindex $argv 0]]
26*20de9f6cSdrhfconfigure $in -translation binary
277651e0a4Sdrhwhile {![eof $in]} {
287651e0a4Sdrh  set line [gets $in]
297651e0a4Sdrh  if {[regexp {^#define OP_} $line]} {
307651e0a4Sdrh    set name [lindex $line 1]
317651e0a4Sdrh    regsub {^OP_} $name {} name
327651e0a4Sdrh    set i [lindex $line 2]
337651e0a4Sdrh    set label($i) $name
347651e0a4Sdrh    if {$mx<$i} {set mx $i}
357651e0a4Sdrh    if {[regexp {synopsis: (.*) \*/} $line all x]} {
367651e0a4Sdrh      set synopsis($i) [string trim $x]
377651e0a4Sdrh    } else {
387651e0a4Sdrh      set synopsis($i) {}
397651e0a4Sdrh    }
407651e0a4Sdrh  }
417651e0a4Sdrh}
427651e0a4Sdrhclose $in
437651e0a4Sdrh
44ed94af5eSdrhfor {set i 0} {$i<=$mx} {incr i} {
457651e0a4Sdrh  puts [format "    /* %3d */ %-18s OpHelp(\"%s\")," \
467651e0a4Sdrh         $i \"$label($i)\" $synopsis($i)]
477651e0a4Sdrh}
487651e0a4Sdrhputs "  \175;"
497651e0a4Sdrhputs "  return azName\[i\];"
507651e0a4Sdrhputs "\175"
517651e0a4Sdrhputs "#endif"
52