#!/usr/bin/tcl
#
# This script makes modifications to the vdbe.c source file which reduce
# the amount of stack space required by the sqlite3VdbeExec() routine.
#
# The modifications performed by this script are optional. The vdbe.c
# source file will compile correctly with and without the modifications
# performed by this script. And all routines within vdbe.c will compute
# the same result. The modifications made by this script merely help
# the C compiler to generate code for sqlite3VdbeExec() that uses less
# stack space.
#
# Script usage:
#
# mv vdbe.c vdbe.c.template
# tclsh vdbe-compress.tcl $CFLAGS <vdbe.c.template >vdbe.c
#
# Modifications made:
#
# All modifications are within the sqlite3VdbeExec() function. The
# modifications seek to reduce the amount of stack space allocated by
# this routine by moving local variable declarations out of individual
# opcode implementations and into a single large union. The union contains
# a separate structure for each opcode and that structure contains the
# local variables used by that opcode. In this way, the total amount
# of stack space required by sqlite3VdbeExec() is reduced from the
# sum of all local variables to the maximum of the local variable space
# required for any single opcode.
#
# In order to be recognized by this script, local variables must appear
# on the first line after the open curly-brace that begins a new opcode
# implementation. Local variables must not have initializers, though they
# may be commented.
#
# The union definition is inserted in place of a special marker comment
# in the preamble to the sqlite3VdbeExec() implementation.
#
#############################################################################
#
set beforeUnion ;# C code before union
set unionDef ;# C code of the union
set afterUnion ;# C code after the union
set sCtr 0 ;# Context counter
# If the SQLITE_SMALL_STACK compile-time option is missing, then
# this transformation becomes a no-op.
#
if
# Read program text up to the spot where the union should be
# inserted.
#
while
# Process the remaining text. Build up the union definition as we go.
#
set vlist
set seenDecl 0
set namechars
set nnc
while
# Output the resulting text.
#
puts -nonewline $beforeUnion
puts " /********************************************************************"
puts " ** Automatically generated code"
puts " **"
puts " ** The following union is automatically generated by the"
puts " ** vdbe-compress.tcl script. The purpose of this union is to"
puts " ** reduce the amount of stack space required by this function."
puts " ** See comments in the vdbe-compress.tcl script for details."
puts " */"
puts " union vdbeExecUnion \173"
puts -nonewline $unionDef
puts " \175 u;"
puts " /* End automatically generated code"
puts " ********************************************************************/"
puts -nonewline $afterUnion