home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tcl8.0 / generic / tclCompile.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-15  |  39.8 KB  |  1,013 lines  |  [TEXT/CWIE]

  1. /*
  2.  * tclCompile.h --
  3.  *
  4.  * Copyright (c) 1996-1997 Sun Microsystems, Inc.
  5.  *
  6.  * See the file "license.terms" for information on usage and redistribution
  7.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  8.  *
  9.  * SCCS: @(#) tclCompile.h 1.37 97/08/07 19:11:50
  10.  */
  11.  
  12. #ifndef _TCLCOMPILATION
  13. #define _TCLCOMPILATION 1
  14.  
  15. #ifndef _TCLINT
  16. #include "tclInt.h"
  17. #endif /* _TCLINT */
  18.  
  19. /*
  20.  *------------------------------------------------------------------------
  21.  * Variables related to compilation. These are used in tclCompile.c,
  22.  * tclExecute.c, tclBasic.c, and their clients.
  23.  *------------------------------------------------------------------------
  24.  */
  25.  
  26. /*
  27.  * Variable that denotes the command name Tcl object type. Objects of this
  28.  * type cache the Command pointer that results from looking up command names
  29.  * in the command hashtable.
  30.  */
  31.  
  32. extern Tcl_ObjType    tclCmdNameType;
  33.  
  34. /*
  35.  * Variable that controls whether compilation tracing is enabled and, if so,
  36.  * what level of tracing is desired:
  37.  *    0: no compilation tracing
  38.  *    1: summarize compilation of top level cmds and proc bodies
  39.  *    2: display all instructions of each ByteCode compiled
  40.  * This variable is linked to the Tcl variable "tcl_traceCompile".
  41.  */
  42.  
  43. extern int         tclTraceCompile;
  44.  
  45. /*
  46.  * Variable that controls whether execution tracing is enabled and, if so,
  47.  * what level of tracing is desired:
  48.  *    0: no execution tracing
  49.  *    1: trace invocations of Tcl procs only
  50.  *    2: trace invocations of all (not compiled away) commands
  51.  *    3: display each instruction executed
  52.  * This variable is linked to the Tcl variable "tcl_traceExec".
  53.  */
  54.  
  55. extern int         tclTraceExec;
  56.  
  57. /*
  58.  * The number of bytecode compilations and various other compilation-related
  59.  * statistics. The tclByteCodeCount and tclSourceCount arrays are used to
  60.  * hold the count of ByteCodes and sources whose sizes fall into various
  61.  * binary decades; e.g., tclByteCodeCount[5] is a count of the ByteCodes
  62.  * with size larger than 2**4 and less than or equal to 2**5.
  63.  */
  64.  
  65. #ifdef TCL_COMPILE_STATS
  66. extern long        tclNumCompilations;
  67. extern double        tclTotalSourceBytes;
  68. extern double        tclTotalCodeBytes;
  69.  
  70. extern double        tclTotalInstBytes;
  71. extern double        tclTotalObjBytes;
  72. extern double        tclTotalExceptBytes;
  73. extern double        tclTotalAuxBytes;
  74. extern double        tclTotalCmdMapBytes;
  75.  
  76. extern double        tclCurrentSourceBytes;
  77. extern double        tclCurrentCodeBytes;
  78.  
  79. extern int        tclSourceCount[32];
  80. extern int        tclByteCodeCount[32];
  81. #endif /* TCL_COMPILE_STATS */
  82.  
  83. /*
  84.  *------------------------------------------------------------------------
  85.  * Data structures related to compilation.
  86.  *------------------------------------------------------------------------
  87.  */
  88.  
  89. /*
  90.  * The structure used to implement Tcl "exceptions" (exceptional returns):
  91.  * for example, those generated in loops by the break and continue commands,
  92.  * and those generated by scripts and caught by the catch command. This
  93.  * ExceptionRange structure describes a range of code (e.g., a loop body),
  94.  * the kind of exceptions (e.g., a break or continue) that might occur, and
  95.  * the PC offsets to jump to if a matching exception does occur. Exception
  96.  * ranges can nest so this structure includes a nesting level that is used
  97.  * at runtime to find the closest exception range surrounding a PC. For
  98.  * example, when a break command is executed, the ExceptionRange structure
  99.  * for the most deeply nested loop, if any, is found and used. These
  100.  * structures are also generated for the "next" subcommands of for loops
  101.  * since a break there terminates the for command. This means a for command
  102.  * actually generates two LoopInfo structures.
  103.  */
  104.  
  105. typedef enum {
  106.     LOOP_EXCEPTION_RANGE,    /* Code range is part of a loop command.
  107.                  * break and continue "exceptions" cause
  108.                  * jumps to appropriate PC offsets. */
  109.     CATCH_EXCEPTION_RANGE    /* Code range is controlled by a catch
  110.                  * command. Errors in the range cause a
  111.                  * jump to a particular PC offset. */
  112. } ExceptionRangeType;
  113.  
  114. typedef struct ExceptionRange {
  115.     ExceptionRangeType type;    /* The kind of ExceptionRange. */
  116.     int nestingLevel;        /* Static depth of the exception range.
  117.                  * Used to find the most deeply-nested
  118.                  * range surrounding a PC at runtime. */
  119.     int codeOffset;        /* Offset of the first instruction byte of
  120.                  * the code range. */
  121.     int numCodeBytes;        /* Number of bytes in the code range. */
  122.     int breakOffset;        /* If a LOOP_EXCEPTION_RANGE, the target
  123.                  * PC offset for a break command in the
  124.                  * range. */
  125.     int continueOffset;        /* If a LOOP_EXCEPTION_RANGE and not -1,
  126.                  * the target PC offset for a continue
  127.                  * command in the code range. Otherwise,
  128.                  * ignore this range when processing a
  129.                  * continue command. */
  130.     int catchOffset;        /* If a CATCH_EXCEPTION_RANGE, the target PC
  131.                  * offset for an "exception" in range. */
  132. } ExceptionRange;
  133.  
  134. /*
  135.  * Structure used to map between instruction pc and source locations. It
  136.  * defines for each compiled Tcl command its code's starting offset and 
  137.  * its source's starting offset and length. Note that the code offset
  138.  * increases monotonically: that is, the table is sorted in code offset
  139.  * order. The source offset is not monotonic.
  140.  */
  141.  
  142. typedef struct CmdLocation {
  143.     int codeOffset;        /* Offset of first byte of command code. */
  144.     int numCodeBytes;        /* Number of bytes for command's code. */
  145.     int srcOffset;        /* Offset of first char of the command. */
  146.     int numSrcChars;        /* Number of command source chars. */
  147. } CmdLocation;
  148.  
  149. /*
  150.  * CompileProcs need the ability to record information during compilation
  151.  * that can be used by bytecode instructions during execution. The AuxData
  152.  * structure provides this "auxiliary data" mechanism. An arbitrary number
  153.  * of these structures can be stored in the ByteCode record (during
  154.  * compilation they are stored in a CompileEnv structure). Each AuxData
  155.  * record holds one word of client-specified data (often a pointer) and is
  156.  * given an index that instructions can later use to look up the structure
  157.  * and its data.
  158.  *
  159.  * The following definitions declare the types of procedures that are called
  160.  * to duplicate or free this auxiliary data when the containing ByteCode
  161.  * objects are duplicated and freed. Pointers to these procedures are kept
  162.  * in the AuxData structure.
  163.  */
  164.  
  165. typedef ClientData (AuxDataDupProc)  _ANSI_ARGS_((ClientData clientData));
  166. typedef void       (AuxDataFreeProc) _ANSI_ARGS_((ClientData clientData));
  167.  
  168. /*
  169.  * The definition of the AuxData structure that holds information created
  170.  * during compilation by CompileProcs and used by instructions during
  171.  * execution.
  172.  */
  173.  
  174. typedef struct AuxData {
  175.     ClientData clientData;    /* The compilation data itself. */
  176.     AuxDataDupProc *dupProc;    /* Callback procedure to invoke when the
  177.                  * aux data is duplicated (e.g., when the
  178.                  * ByteCode structure containing the aux
  179.                  * data is duplicated). NULL means just
  180.                  * copy the source clientData bits; no
  181.                  * proc need be called. */
  182.     AuxDataFreeProc *freeProc;    /* Callback procedure to invoke when the
  183.                  * aux data is freed. NULL means no
  184.                  * proc need be called. */
  185. } AuxData;
  186.  
  187. /*
  188.  * Structure defining the compilation environment. After compilation, fields
  189.  * describing bytecode instructions are copied out into the more compact
  190.  * ByteCode structure defined below.
  191.  */
  192.  
  193. #define COMPILEENV_INIT_CODE_BYTES    250
  194. #define COMPILEENV_INIT_NUM_OBJECTS    40
  195. #define COMPILEENV_INIT_EXCEPT_RANGES   5
  196. #define COMPILEENV_INIT_CMD_MAP_SIZE   40
  197. #define COMPILEENV_INIT_AUX_DATA_SIZE   5
  198.  
  199. typedef struct CompileEnv {
  200.     Interp *iPtr;        /* Interpreter containing the code being
  201.                  * compiled. Commands and their compile
  202.                  * procs are specific to an interpreter so
  203.                  * the code emitted will depend on the
  204.                  * interpreter. */
  205.     char *source;        /* The source string being compiled by
  206.                  * SetByteCodeFromAny. This pointer is not
  207.                  * owned by the CompileEnv and must not be
  208.                  * freed or changed by it. */
  209.     Proc *procPtr;        /* If a procedure is being compiled, a
  210.                  * pointer to its Proc structure; otherwise
  211.                  * NULL. Used to compile local variables.
  212.                  * Set from information provided by
  213.                  * ObjInterpProc in tclProc.c. */
  214.     int numCommands;        /* Number of commands compiled. */
  215.     int excRangeDepth;        /* Current exception range nesting level;
  216.                  * -1 if not in any range currently. */
  217.     int maxExcRangeDepth;    /* Max nesting level of exception ranges;
  218.                  * -1 if no ranges have been compiled. */
  219.     int maxStackDepth;        /* Maximum number of stack elements needed
  220.                  * to execute the code. Set by compilation
  221.                  * procedures before returning. */
  222.     Tcl_HashTable objTable;    /* Contains all Tcl objects referenced by
  223.                  * the compiled code. Indexed by the string
  224.                  * representations of the objects. Used to
  225.                  * avoid creating duplicate objects. */
  226.     int pushSimpleWords;    /* Set 1 by callers of compilation routines
  227.                  * if they should emit instructions to push
  228.                  * "simple" command words (those that are
  229.                  * just a sequence of characters). If 0, the
  230.                  * callers are responsible for compiling
  231.                  * simple words. */
  232.     int wordIsSimple;        /* Set 1 by compilation procedures before
  233.                  * returning if the previous command word
  234.                  * was just a sequence of characters,
  235.                  * otherwise 0. Used to help determine the
  236.                  * command being compiled. */
  237.     int numSimpleWordChars;    /* If wordIsSimple is 1 then the number of
  238.                  * characters in the simple word, else 0. */
  239.     int exprIsJustVarRef;    /* Set 1 if the expression last compiled by
  240.                  * TclCompileExpr consisted of just a
  241.                  * variable reference as in the expression
  242.                  * of "if $b then...". Otherwise 0. Used
  243.                  * to implement expr's 2 level substitution
  244.                  * semantics properly. */
  245.     int exprIsComparison;    /* Set 1 if the top-level operator in the
  246.                  * expression last compiled is a comparison.
  247.                  * Otherwise 0. If 1, since the operands
  248.                  * might be strings, the expr is compiled
  249.                  * out-of-line to implement expr's 2 level
  250.                  * substitution semantics properly. */
  251.     int termOffset;        /* Offset of character just after the last
  252.                  * one compiled. Set by compilation
  253.                  * procedures before returning. */
  254.     unsigned char *codeStart;    /* Points to the first byte of the code. */
  255.     unsigned char *codeNext;    /* Points to next code array byte to use. */
  256.     unsigned char *codeEnd;    /* Points just after the last allocated
  257.                  * code array byte. */
  258.     int mallocedCodeArray;      /* Set 1 if code array was expanded 
  259.                  * and codeStart points into the heap.*/
  260.     Tcl_Obj **objArrayPtr;    /* Points to start of object array. */
  261.     int objArrayNext;        /* Index of next free object array entry. */
  262.     int objArrayEnd;        /* Index just after last obj array entry. */
  263.     int mallocedObjArray;       /* 1 if object array was expanded and
  264.                                  * objArray points into the heap, else 0. */
  265.     ExceptionRange *excRangeArrayPtr;
  266.                     /* Points to start of the ExceptionRange
  267.                  * array. */
  268.     int excRangeArrayNext;    /* Next free ExceptionRange array index.
  269.                  * excRangeArrayNext is the number of ranges
  270.                  * and (excRangeArrayNext-1) is the index of
  271.                  * the current range's array entry. */
  272.     int excRangeArrayEnd;    /* Index after the last ExceptionRange
  273.                  * array entry. */
  274.     int mallocedExcRangeArray;    /* 1 if ExceptionRange array was expanded
  275.                  * and excRangeArrayPtr points in heap,
  276.                  * else 0. */
  277.     CmdLocation *cmdMapPtr;    /* Points to start of CmdLocation array.
  278.                  * numCommands is the index of the next
  279.                  * entry to use; (numCommands-1) is the
  280.                  * entry index for the last command. */
  281.     int cmdMapEnd;        /* Index after last CmdLocation entry. */
  282.     int mallocedCmdMap;        /* 1 if command map array was expanded and
  283.                  * cmdMapPtr points in the heap, else 0. */
  284.     AuxData *auxDataArrayPtr;   /* Points to auxiliary data array start. */
  285.     int auxDataArrayNext;    /* Next free compile aux data array index.
  286.                  * auxDataArrayNext is the number of aux
  287.                  * data items and (auxDataArrayNext-1) is
  288.                  * index of current aux data array entry. */
  289.     int auxDataArrayEnd;    /* Index after last aux data array entry. */
  290.     int mallocedAuxDataArray;    /* 1 if aux data array was expanded and
  291.                  * auxDataArrayPtr points in heap else 0. */
  292.     unsigned char staticCodeSpace[COMPILEENV_INIT_CODE_BYTES];
  293.                                 /* Initial storage for code. */
  294.     Tcl_Obj *staticObjArraySpace[COMPILEENV_INIT_NUM_OBJECTS];
  295.                                 /* Initial storage for object array. */
  296.     ExceptionRange staticExcRangeArraySpace[COMPILEENV_INIT_EXCEPT_RANGES];
  297.                                 /* Initial ExceptionRange array storage. */
  298.     CmdLocation staticCmdMapSpace[COMPILEENV_INIT_CMD_MAP_SIZE];
  299.                                 /* Initial storage for cmd location map. */
  300.     AuxData staticAuxDataArraySpace[COMPILEENV_INIT_AUX_DATA_SIZE];
  301.                                 /* Initial storage for aux data array. */
  302. } CompileEnv;
  303.  
  304. /*
  305.  * The structure defining the bytecode instructions resulting from compiling
  306.  * a Tcl script. Note that this structure is variable length: a single heap
  307.  * object is allocated to hold the ByteCode structure immediately followed
  308.  * by the code bytes, the object array, the ExceptionRange array, the
  309.  * CmdLocation map, and the compilation AuxData array.
  310.  */
  311.  
  312. typedef struct ByteCode {
  313.     Interp *iPtr;        /* Interpreter containing the code being
  314.                  * compiled. Commands and their compile
  315.                  * procs are specific to an interpreter so
  316.                  * the code emitted will depend on the
  317.                  * interpreter. */
  318.     int compileEpoch;        /* Value of iPtr->compileEpoch when this
  319.                  * ByteCode was compiled. Used to invalidate
  320.                  * code when, e.g., commands with compile
  321.                  * procs are redefined. */
  322.     int refCount;        /* Reference count: set 1 when created
  323.                  * plus 1 for each execution of the code
  324.                  * currently active. This structure can be
  325.                  * freed when refCount becomes zero. */
  326.     char *source;        /* The source string from which this
  327.                  * ByteCode was compiled. Note that this
  328.                  * pointer is not owned by the ByteCode and
  329.                  * must not be freed or modified by it. */
  330.     Proc *procPtr;        /* If the ByteCode was compiled from a
  331.                  * procedure body, this is a pointer to its
  332.                  * Proc structure; otherwise NULL. This
  333.                  * pointer is also not owned by the ByteCode
  334.                  * and must not be freed by it. Used for
  335.                  * debugging. */
  336.     size_t totalSize;        /* Total number of bytes required for this
  337.                  * ByteCode structure including the storage
  338.                  * for Tcl objects in its object array. */
  339.     int numCommands;        /* Number of commands compiled. */
  340.     int numSrcChars;        /* Number of source chars compiled. */
  341.     int numCodeBytes;        /* Number of code bytes. */
  342.     int numObjects;        /* Number of Tcl objects in object array. */
  343.     int numExcRanges;        /* Number of ExceptionRange array elems. */
  344.     int numAuxDataItems;    /* Number of AuxData items. */
  345.     int numCmdLocBytes;        /* Number of bytes needed for encoded
  346.                  * command location information. */
  347.     int maxExcRangeDepth;    /* Maximum nesting level of ExceptionRanges;
  348.                  * -1 if no ranges were compiled. */
  349.     int maxStackDepth;        /* Maximum number of stack elements needed
  350.                  * to execute the code. */
  351.     unsigned char *codeStart;    /* Points to the first byte of the code.
  352.                  * This is just after the final ByteCode
  353.                  * member cmdMapPtr. */
  354.     Tcl_Obj **objArrayPtr;    /* Points to the start of the object array.
  355.                  * This is just after the last code byte. */
  356.     ExceptionRange *excRangeArrayPtr;
  357.                     /* Points to the start of the ExceptionRange
  358.                  * array. This is just after the last
  359.                  * object in the object array. */
  360.     AuxData *auxDataArrayPtr;   /* Points to the start of the auxiliary data
  361.                  * array. This is just after the last entry
  362.                  * in the ExceptionRange array. */
  363.     unsigned char *codeDeltaStart;
  364.                 /* Points to the first of a sequence of
  365.                  * bytes that encode the change in the
  366.                  * starting offset of each command's code.
  367.                  * If -127<=delta<=127, it is encoded as 1
  368.                  * byte, otherwise 0xFF (128) appears and
  369.                  * the delta is encoded by the next 4 bytes.
  370.                  * Code deltas are always positive. This
  371.                  * sequence is just after the last entry in
  372.                  * the AuxData array. */
  373.     unsigned char *codeLengthStart;
  374.                 /* Points to the first of a sequence of
  375.                  * bytes that encode the length of each
  376.                  * command's code. The encoding is the same
  377.                  * as for code deltas. Code lengths are
  378.                  * always positive. This sequence is just
  379.                  * after the last entry in the code delta
  380.                  * sequence. */
  381.     unsigned char *srcDeltaStart;
  382.                 /* Points to the first of a sequence of
  383.                  * bytes that encode the change in the
  384.                  * starting offset of each command's source.
  385.                  * The encoding is the same as for code
  386.                  * deltas. Source deltas can be negative.
  387.                  * This sequence is just after the last byte
  388.                  * in the code length sequence. */
  389.     unsigned char *srcLengthStart;
  390.                 /* Points to the first of a sequence of
  391.                  * bytes that encode the length of each
  392.                  * command's source. The encoding is the
  393.                  * same as for code deltas. Source lengths
  394.                  * are always positive. This sequence is
  395.                  * just after the last byte in the source
  396.                  * delta sequence. */
  397. } ByteCode;
  398.  
  399. /*
  400.  * Opcodes for the Tcl bytecode instructions. These opcodes must correspond
  401.  * to the entries in the table of instruction descriptions in tclCompile.c.
  402.  * Also, the order and number of the expression opcodes (e.g., INST_LOR)
  403.  * must match the entries in the array operatorStrings in tclExecute.c.
  404.  */
  405.  
  406. /* Opcodes 0 to 9 */
  407. #define INST_DONE            0
  408. #define INST_PUSH1            (INST_DONE + 1)
  409. #define INST_PUSH4            (INST_DONE + 2)
  410. #define INST_POP            (INST_DONE + 3)
  411. #define INST_DUP            (INST_DONE + 4)
  412. #define INST_CONCAT1            (INST_DONE + 5)
  413. #define INST_INVOKE_STK1        (INST_DONE + 6)
  414. #define INST_INVOKE_STK4        (INST_DONE + 7)
  415. #define INST_EVAL_STK            (INST_DONE + 8)
  416. #define INST_EXPR_STK            (INST_DONE + 9)
  417.  
  418. /* Opcodes 10 to 23 */
  419. #define INST_LOAD_SCALAR1        (INST_EXPR_STK + 1)
  420. #define INST_LOAD_SCALAR4        (INST_LOAD_SCALAR1 + 1)
  421. #define INST_LOAD_SCALAR_STK        (INST_LOAD_SCALAR1 + 2)
  422. #define INST_LOAD_ARRAY1        (INST_LOAD_SCALAR1 + 3)
  423. #define INST_LOAD_ARRAY4        (INST_LOAD_SCALAR1 + 4)
  424. #define INST_LOAD_ARRAY_STK        (INST_LOAD_SCALAR1 + 5)
  425. #define INST_LOAD_STK            (INST_LOAD_SCALAR1 + 6)
  426. #define INST_STORE_SCALAR1        (INST_LOAD_SCALAR1 + 7)
  427. #define INST_STORE_SCALAR4        (INST_LOAD_SCALAR1 + 8)
  428. #define INST_STORE_SCALAR_STK        (INST_LOAD_SCALAR1 + 9)
  429. #define INST_STORE_ARRAY1        (INST_LOAD_SCALAR1 + 10)
  430. #define INST_STORE_ARRAY4        (INST_LOAD_SCALAR1 + 11)
  431. #define INST_STORE_ARRAY_STK        (INST_LOAD_SCALAR1 + 12)
  432. #define INST_STORE_STK            (INST_LOAD_SCALAR1 + 13)
  433.  
  434. /* Opcodes 24 to 33 */
  435. #define INST_INCR_SCALAR1        (INST_STORE_STK + 1)
  436. #define INST_INCR_SCALAR_STK        (INST_INCR_SCALAR1 + 1)
  437. #define INST_INCR_ARRAY1        (INST_INCR_SCALAR1 + 2)
  438. #define INST_INCR_ARRAY_STK        (INST_INCR_SCALAR1 + 3)
  439. #define INST_INCR_STK            (INST_INCR_SCALAR1 + 4)
  440. #define INST_INCR_SCALAR1_IMM        (INST_INCR_SCALAR1 + 5)
  441. #define INST_INCR_SCALAR_STK_IMM    (INST_INCR_SCALAR1 + 6)
  442. #define INST_INCR_ARRAY1_IMM        (INST_INCR_SCALAR1 + 7)
  443. #define INST_INCR_ARRAY_STK_IMM        (INST_INCR_SCALAR1 + 8)
  444. #define INST_INCR_STK_IMM        (INST_INCR_SCALAR1 + 9)
  445.  
  446. /* Opcodes 34 to 39 */
  447. #define INST_JUMP1            (INST_INCR_STK_IMM + 1)
  448. #define INST_JUMP4            (INST_JUMP1 + 1)
  449. #define INST_JUMP_TRUE1            (INST_JUMP1 + 2)
  450. #define INST_JUMP_TRUE4            (INST_JUMP1 + 3)
  451. #define INST_JUMP_FALSE1        (INST_JUMP1 + 4)
  452. #define INST_JUMP_FALSE4            (INST_JUMP1 + 5)
  453.  
  454. /* Opcodes 40 to 64 */
  455. #define INST_LOR            (INST_JUMP_FALSE4 + 1)
  456. #define INST_LAND            (INST_LOR + 1)
  457. #define INST_BITOR            (INST_LOR + 2)
  458. #define INST_BITXOR            (INST_LOR + 3)
  459. #define INST_BITAND            (INST_LOR + 4)
  460. #define INST_EQ                (INST_LOR + 5)
  461. #define INST_NEQ            (INST_LOR + 6)
  462. #define INST_LT                (INST_LOR + 7)
  463. #define INST_GT                (INST_LOR + 8)
  464. #define INST_LE                (INST_LOR + 9)
  465. #define INST_GE                (INST_LOR + 10)
  466. #define INST_LSHIFT            (INST_LOR + 11)
  467. #define INST_RSHIFT            (INST_LOR + 12)
  468. #define INST_ADD            (INST_LOR + 13)
  469. #define INST_SUB            (INST_LOR + 14)
  470. #define INST_MULT            (INST_LOR + 15)
  471. #define INST_DIV            (INST_LOR + 16)
  472. #define INST_MOD            (INST_LOR + 17)
  473. #define INST_UPLUS            (INST_LOR + 18)
  474. #define INST_UMINUS            (INST_LOR + 19)
  475. #define INST_BITNOT            (INST_LOR + 20)
  476. #define INST_LNOT            (INST_LOR + 21)
  477. #define INST_CALL_BUILTIN_FUNC1        (INST_LOR + 22)
  478. #define INST_CALL_FUNC1            (INST_LOR + 23)
  479. #define INST_TRY_CVT_TO_NUMERIC        (INST_LOR + 24)
  480.  
  481. /* Opcodes 65 to 66 */
  482. #define INST_BREAK            (INST_TRY_CVT_TO_NUMERIC + 1)
  483. #define INST_CONTINUE            (INST_BREAK + 1)
  484.  
  485. /* Opcodes 67 to 68 */
  486. #define INST_FOREACH_START4        (INST_CONTINUE + 1)
  487. #define INST_FOREACH_STEP4        (INST_FOREACH_START4 + 1)
  488.  
  489. /* Opcodes 69 to 72 */
  490. #define INST_BEGIN_CATCH4        (INST_FOREACH_STEP4 + 1)
  491. #define INST_END_CATCH            (INST_BEGIN_CATCH4 + 1)
  492. #define INST_PUSH_RESULT        (INST_BEGIN_CATCH4 + 2)
  493. #define INST_PUSH_RETURN_CODE        (INST_BEGIN_CATCH4 + 3)
  494.  
  495. /* The last opcode */
  496. #define LAST_INST_OPCODE            INST_PUSH_RETURN_CODE
  497.  
  498. /*
  499.  * Table describing the Tcl bytecode instructions: their name (for
  500.  * displaying code), total number of code bytes required (including
  501.  * operand bytes), and a description of the type of each operand.
  502.  * These operand types include signed and unsigned integers of length
  503.  * one and four bytes. The unsigned integers are used for indexes or
  504.  * for, e.g., the count of objects to push in a "push" instruction.
  505.  */
  506.  
  507. #define MAX_INSTRUCTION_OPERANDS 2
  508.  
  509. typedef enum InstOperandType {
  510.     OPERAND_NONE,
  511.     OPERAND_INT1,        /* One byte signed integer. */
  512.     OPERAND_INT4,        /* Four byte signed integer. */
  513.     OPERAND_UINT1,        /* One byte unsigned integer. */
  514.     OPERAND_UINT4        /* Four byte unsigned integer. */
  515. } InstOperandType;
  516.  
  517. typedef struct InstructionDesc {
  518.     char *name;            /* Name of instruction. */
  519.     int numBytes;        /* Total number of bytes for instruction. */
  520.     int numOperands;        /* Number of operands. */
  521.     InstOperandType opTypes[MAX_INSTRUCTION_OPERANDS];
  522.                 /* The type of each operand. */
  523. } InstructionDesc;
  524.  
  525. extern InstructionDesc instructionTable[];
  526.  
  527. /*
  528.  * Definitions of the values of the INST_CALL_BUILTIN_FUNC instruction's
  529.  * operand byte. Each value denotes a builtin Tcl math function. These
  530.  * values must correspond to the entries in the builtinFuncTable array
  531.  * below and to the values stored in the tclInt.h MathFunc structure's
  532.  * builtinFuncIndex field.
  533.  */
  534.  
  535. #define BUILTIN_FUNC_ACOS        0
  536. #define BUILTIN_FUNC_ASIN        1
  537. #define BUILTIN_FUNC_ATAN        2
  538. #define BUILTIN_FUNC_ATAN2        3
  539. #define BUILTIN_FUNC_CEIL        4
  540. #define BUILTIN_FUNC_COS        5
  541. #define BUILTIN_FUNC_COSH        6
  542. #define BUILTIN_FUNC_EXP        7
  543. #define BUILTIN_FUNC_FLOOR        8
  544. #define BUILTIN_FUNC_FMOD        9
  545. #define BUILTIN_FUNC_HYPOT        10
  546. #define BUILTIN_FUNC_LOG        11
  547. #define BUILTIN_FUNC_LOG10        12
  548. #define BUILTIN_FUNC_POW        13
  549. #define BUILTIN_FUNC_SIN        14
  550. #define BUILTIN_FUNC_SINH        15
  551. #define BUILTIN_FUNC_SQRT        16
  552. #define BUILTIN_FUNC_TAN        17
  553. #define BUILTIN_FUNC_TANH        18
  554. #define BUILTIN_FUNC_ABS        19
  555. #define BUILTIN_FUNC_DOUBLE        20
  556. #define BUILTIN_FUNC_INT        21
  557. #define BUILTIN_FUNC_RAND        22
  558. #define BUILTIN_FUNC_ROUND        23
  559. #define BUILTIN_FUNC_SRAND        24
  560.  
  561. #define LAST_BUILTIN_FUNC            BUILTIN_FUNC_SRAND
  562.  
  563. /*
  564.  * Table describing the built-in math functions. Entries in this table are
  565.  * indexed by the values of the INST_CALL_BUILTIN_FUNC instruction's
  566.  * operand byte.
  567.  */
  568.  
  569. typedef int (CallBuiltinFuncProc) _ANSI_ARGS_((Tcl_Interp *interp,
  570.         ExecEnv *eePtr, ClientData clientData));
  571.  
  572. typedef struct {
  573.     char *name;            /* Name of function. */
  574.     int numArgs;        /* Number of arguments for function. */
  575.     Tcl_ValueType argTypes[MAX_MATH_ARGS];
  576.                 /* Acceptable types for each argument. */
  577.     CallBuiltinFuncProc *proc;    /* Procedure implementing this function. */
  578.     ClientData clientData;    /* Additional argument to pass to the
  579.                  * function when invoking it. */
  580. } BuiltinFunc;
  581.  
  582. extern BuiltinFunc builtinFuncTable[];
  583.  
  584. /*
  585.  * The structure used to hold information about the start and end of each
  586.  * argument word in a command. 
  587.  */
  588.  
  589. #define ARGINFO_INIT_ENTRIES 5
  590.  
  591. typedef struct ArgInfo {
  592.     int numArgs;        /* Number of argument words in command. */
  593.     char **startArray;        /* Array of pointers to the first character
  594.                  * of each argument word. */
  595.     char **endArray;        /* Array of pointers to the last character
  596.                  * of each argument word. */
  597.     int allocArgs;        /* Number of array entries currently
  598.                  * allocated. */
  599.     int mallocedArrays;        /* 1 if the arrays were expanded and
  600.                  * wordStartArray/wordEndArray point into
  601.                  * the heap, else 0. */
  602.     char *staticStartSpace[ARGINFO_INIT_ENTRIES];
  603.                                 /* Initial storage for word start array. */
  604.     char *staticEndSpace[ARGINFO_INIT_ENTRIES];
  605.                                 /* Initial storage for word end array. */
  606. } ArgInfo;
  607.  
  608. /*
  609.  * Compilation of some Tcl constructs such as if commands and the logical or
  610.  * (||) and logical and (&&) operators in expressions requires the
  611.  * generation of forward jumps. Since the PC target of these jumps isn't
  612.  * known when the jumps are emitted, we record the offset of each jump in an
  613.  * array of JumpFixup structures. There is one array for each sequence of
  614.  * jumps to one target PC. When we learn the target PC, we update the jumps
  615.  * with the correct distance. Also, if the distance is too great (> 127
  616.  * bytes), we replace the single-byte jump with a four byte jump
  617.  * instruction, move the instructions after the jump down, and update the
  618.  * code offsets for any commands between the jump and the target.
  619.  */
  620.  
  621. typedef enum {
  622.     TCL_UNCONDITIONAL_JUMP,
  623.     TCL_TRUE_JUMP,
  624.     TCL_FALSE_JUMP
  625. } TclJumpType;
  626.  
  627. typedef struct JumpFixup {
  628.     TclJumpType jumpType;    /* Indicates the kind of jump. */
  629.     int codeOffset;        /* Offset of the first byte of the one-byte
  630.                  * forward jump's code. */
  631.     int cmdIndex;        /* Index of the first command after the one
  632.                  * for which the jump was emitted. Used to
  633.                  * update the code offsets for subsequent
  634.                  * commands if the two-byte jump at jumpPc
  635.                  * must be replaced with a five-byte one. */
  636.     int excRangeIndex;        /* Index of the first range entry in the
  637.                  * ExceptionRange array after the current
  638.                  * one. This field is used to adjust the
  639.                  * code offsets in subsequent ExceptionRange
  640.                  * records when a jump is grown from 2 bytes
  641.                  * to 5 bytes. */
  642. } JumpFixup;
  643.  
  644. #define JUMPFIXUP_INIT_ENTRIES    10
  645.  
  646. typedef struct JumpFixupArray {
  647.     JumpFixup *fixup;        /* Points to start of jump fixup array. */
  648.     int next;            /* Index of next free array entry. */
  649.     int end;            /* Index of last usable entry in array. */
  650.     int mallocedArray;        /* 1 if array was expanded and fixups points
  651.                  * into the heap, else 0. */
  652.     JumpFixup staticFixupSpace[JUMPFIXUP_INIT_ENTRIES];
  653.                 /* Initial storage for jump fixup array. */
  654. } JumpFixupArray;
  655.  
  656. /*
  657.  * The structure describing one variable list of a foreach command. Note
  658.  * that only foreach commands inside procedure bodies are compiled inline so
  659.  * a ForeachVarList structure always describes local variables. Furthermore,
  660.  * only scalar variables are supported for inline-compiled foreach loops.
  661.  */
  662.  
  663. typedef struct ForeachVarList {
  664.     int numVars;        /* The number of variables in the list. */
  665.     int varIndexes[1];        /* An array of the indexes ("slot numbers")
  666.                  * for each variable in the procedure's
  667.                  * array of local variables. Only scalar
  668.                  * variables are supported. The actual
  669.                  * size of this field will be large enough
  670.                  * to numVars indexes. THIS MUST BE THE
  671.                  * LAST FIELD IN THE STRUCTURE! */
  672. } ForeachVarList;
  673.  
  674. /*
  675.  * Structure used to hold information about a foreach command that is needed
  676.  * during program execution. These structures are stored in CompileEnv and
  677.  * ByteCode structures as auxiliary data.
  678.  */
  679.  
  680. typedef struct ForeachInfo {
  681.     int numLists;        /* The number of both the variable and value
  682.                  * lists of the foreach command. */
  683.     int firstListTmp;        /* The slot number of the first temporary
  684.                  * variable holding the lists themselves. */
  685.     int loopIterNumTmp;        /* The slot number of the temp var holding
  686.                  * the count of times the loop body has been
  687.                  * executed. This is used to determine which
  688.                  * list element to assign each loop var. */
  689.     ForeachVarList *varLists[1];/* An array of pointers to ForeachVarList
  690.                  * structures describing each var list. The
  691.                  * actual size of this field will be large
  692.                  * enough to numVars indexes. THIS MUST BE
  693.                  * THE LAST FIELD IN THE STRUCTURE! */
  694. } ForeachInfo;
  695.  
  696. /*
  697.  * Structure containing a cached pointer to a command that is the result
  698.  * of resolving the command's name in some namespace. It is the internal
  699.  * representation for a cmdName object. It contains the pointer along
  700.  * with some information that is used to check the pointer's validity.
  701.  */
  702.  
  703. typedef struct ResolvedCmdName {
  704.     Command *cmdPtr;        /* A cached Command pointer. */
  705.     Namespace *refNsPtr;    /* Points to the namespace containing the
  706.                  * reference (not the namespace that
  707.                  * contains the referenced command). */
  708.     long refNsId;        /* refNsPtr's unique namespace id. Used to
  709.                  * verify that refNsPtr is still valid
  710.                  * (e.g., it's possible that the cmd's
  711.                  * containing namespace was deleted and a
  712.                  * new one created at the same address). */
  713.     int refNsCmdEpoch;        /* Value of the referencing namespace's
  714.                  * cmdRefEpoch when the pointer was cached.
  715.                  * Before using the cached pointer, we check
  716.                  * if the namespace's epoch was incremented;
  717.                  * if so, this cached pointer is invalid. */
  718.     int cmdEpoch;        /* Value of the command's cmdEpoch when this
  719.                  * pointer was cached. Before using the
  720.                  * cached pointer, we check if the cmd's
  721.                  * epoch was incremented; if so, the cmd was
  722.                  * renamed, deleted, hidden, or exposed, and
  723.                  * so the pointer is invalid. */
  724.     int refCount;        /* Reference count: 1 for each cmdName
  725.                  * object that has a pointer to this
  726.                  * ResolvedCmdName structure as its internal
  727.                  * rep. This structure can be freed when
  728.                  * refCount becomes zero. */
  729. } ResolvedCmdName;
  730.  
  731. /*
  732.  *----------------------------------------------------------------
  733.  * Procedures shared among Tcl bytecode compilation and execution
  734.  * modules but not used outside:
  735.  *----------------------------------------------------------------
  736.  */
  737.  
  738. EXTERN void        TclCleanupByteCode _ANSI_ARGS_((ByteCode *codePtr));
  739. EXTERN int        TclCompileExpr _ANSI_ARGS_((Tcl_Interp *interp,
  740.                 char *string, char *lastChar, int flags,
  741.                 CompileEnv *envPtr));
  742. EXTERN int        TclCompileQuotes _ANSI_ARGS_((Tcl_Interp *interp,
  743.                 char *string, char *lastChar, int termChar,
  744.                 int flags, CompileEnv *envPtr));
  745. EXTERN int        TclCompileString _ANSI_ARGS_((Tcl_Interp *interp,
  746.                 char *string, char *lastChar, int flags,
  747.                 CompileEnv *envPtr));
  748. EXTERN int        TclCompileDollarVar _ANSI_ARGS_((Tcl_Interp *interp,
  749.                 char *string, char *lastChar, int flags,
  750.                 CompileEnv *envPtr));
  751. EXTERN int        TclCreateAuxData _ANSI_ARGS_((
  752.                 ClientData clientData, AuxDataDupProc *dupProc,
  753.                 AuxDataFreeProc *freeProc, CompileEnv *envPtr));
  754. EXTERN ExecEnv *    TclCreateExecEnv _ANSI_ARGS_((Tcl_Interp *interp));
  755. EXTERN void        TclDeleteExecEnv _ANSI_ARGS_((ExecEnv *eePtr));
  756. EXTERN void        TclEmitForwardJump _ANSI_ARGS_((CompileEnv *envPtr,
  757.                 TclJumpType jumpType, JumpFixup *jumpFixupPtr));
  758. EXTERN ExceptionRange *    TclGetExceptionRangeForPc _ANSI_ARGS_((
  759.                 unsigned char *pc, int catchOnly,
  760.                 ByteCode* codePtr));
  761. EXTERN int        TclExecuteByteCode _ANSI_ARGS_((Tcl_Interp *interp,
  762.                 ByteCode *codePtr));
  763. EXTERN void        TclExpandCodeArray _ANSI_ARGS_((
  764.                             CompileEnv *envPtr));
  765. EXTERN void        TclExpandJumpFixupArray _ANSI_ARGS_((
  766.                             JumpFixupArray *fixupArrayPtr));
  767. EXTERN int        TclFixupForwardJump _ANSI_ARGS_((
  768.                 CompileEnv *envPtr, JumpFixup *jumpFixupPtr,
  769.                 int jumpDist, int distThreshold));
  770. EXTERN void        TclFreeCompileEnv _ANSI_ARGS_((CompileEnv *envPtr));
  771. EXTERN void        TclFreeJumpFixupArray _ANSI_ARGS_((
  772.                   JumpFixupArray *fixupArrayPtr));
  773. EXTERN void        TclInitByteCodeObj _ANSI_ARGS_((Tcl_Obj *objPtr,
  774.                 CompileEnv *envPtr));
  775. EXTERN void        TclInitCompileEnv _ANSI_ARGS_((Tcl_Interp *interp,
  776.                 CompileEnv *envPtr, char *string));
  777. EXTERN void        TclInitJumpFixupArray _ANSI_ARGS_((
  778.                 JumpFixupArray *fixupArrayPtr));
  779. #ifdef TCL_COMPILE_STATS
  780. EXTERN int        TclLog2 _ANSI_ARGS_((int value));
  781. #endif /*TCL_COMPILE_STATS*/
  782. EXTERN int        TclObjIndexForString _ANSI_ARGS_((char *start,
  783.                 int length, int allocStrRep, int inHeap,
  784.                 CompileEnv *envPtr));
  785. EXTERN int        TclPrintInstruction _ANSI_ARGS_((ByteCode* codePtr,
  786.                 unsigned char *pc));
  787. EXTERN void        TclPrintSource _ANSI_ARGS_((FILE *outFile,
  788.                 char *string, int maxChars));
  789.  
  790. /*
  791.  *----------------------------------------------------------------
  792.  * Macros used by Tcl bytecode compilation and execution modules
  793.  * inside the Tcl core but not used outside.
  794.  *----------------------------------------------------------------
  795.  */
  796.  
  797. /*
  798.  * Macros to ensure there is enough room in a CompileEnv's code array.
  799.  * The ANSI C "prototypes" for these macros are:
  800.  *
  801.  * EXTERN void    TclEnsureCodeSpace1 _ANSI_ARGS_((CompileEnv *envPtr));
  802.  * EXTERN void    TclEnsureCodeSpace _ANSI_ARGS_((int nBytes,
  803.  *            CompileEnv *envPtr));
  804.  */
  805.  
  806. #define TclEnsureCodeSpace1(envPtr) \
  807.     if ((envPtr)->codeNext == (envPtr)->codeEnd) \
  808.         TclExpandCodeArray(envPtr)
  809.  
  810. #define TclEnsureCodeSpace(nBytes, envPtr) \
  811.     if (((envPtr)->codeNext + nBytes) > (envPtr)->codeEnd) \
  812.         TclExpandCodeArray(envPtr)
  813.  
  814. /*
  815.  * Macro to emit an opcode byte into a CompileEnv's code array.
  816.  * The ANSI C "prototype" for this macro is:
  817.  *
  818.  * EXTERN void    TclEmitOpcode _ANSI_ARGS_((unsigned char op,
  819.  *            CompileEnv *envPtr));
  820.  */
  821.  
  822. #define TclEmitOpcode(op, envPtr) \
  823.     TclEnsureCodeSpace1(envPtr); \
  824.     *(envPtr)->codeNext++ = (unsigned char) (op)
  825.  
  826. /*
  827.  * Macros to emit a (signed or unsigned) int operand. The two variants
  828.  * depend on the number of bytes needed for the int. Four byte integers
  829.  * are stored in "big-endian" order with the high order byte stored at
  830.  * the lowest address. The ANSI C "prototypes" for these macros are:
  831.  *
  832.  * EXTERN void    TclEmitInt1 _ANSI_ARGS_((int i, CompileEnv *envPtr));
  833.  * EXTERN void    TclEmitInt4 _ANSI_ARGS_((int i, CompileEnv *envPtr));
  834.  */
  835.  
  836. #define TclEmitInt1(i, envPtr) \
  837.     TclEnsureCodeSpace(1, (envPtr)); \
  838.     *(envPtr)->codeNext++ = (unsigned char) ((unsigned int) (i))
  839.  
  840. #define TclEmitInt4(i, envPtr) \
  841.     TclEnsureCodeSpace(4, (envPtr)); \
  842.     *(envPtr)->codeNext++ = \
  843.         (unsigned char) ((unsigned int) (i) >> 24); \
  844.     *(envPtr)->codeNext++ = \
  845.         (unsigned char) ((unsigned int) (i) >> 16); \
  846.     *(envPtr)->codeNext++ = \
  847.         (unsigned char) ((unsigned int) (i) >>  8); \
  848.     *(envPtr)->codeNext++ = \
  849.         (unsigned char) ((unsigned int) (i)      )
  850.  
  851. /*
  852.  * Macros to emit an instruction with signed or unsigned int operands.
  853.  * The ANSI C "prototypes" for these macros are:
  854.  *
  855.  * EXTERN void    TclEmitInstInt1 _ANSI_ARGS_((unsigned char op, int i, 
  856.  *            CompileEnv *envPtr));
  857.  * EXTERN void    TclEmitInstInt4 _ANSI_ARGS_((unsigned char op, int i, 
  858.  *            CompileEnv *envPtr));
  859.  * EXTERN void    TclEmitInstUInt1 _ANSI_ARGS_((unsigned char op,
  860.  *            unsigned int i, CompileEnv *envPtr));
  861.  * EXTERN void    TclEmitInstUInt4 _ANSI_ARGS_((unsigned char op,
  862.  *            unsigned int i, CompileEnv *envPtr));
  863.  */
  864.  
  865. #define TclEmitInstInt1(op, i, envPtr) \
  866.     TclEnsureCodeSpace(2, (envPtr)); \
  867.     *(envPtr)->codeNext++ = (unsigned char) (op); \
  868.     *(envPtr)->codeNext++ = (unsigned char) ((unsigned int) (i))
  869.  
  870. #define TclEmitInstInt4(op, i, envPtr) \
  871.     TclEnsureCodeSpace(5, (envPtr)); \
  872.     *(envPtr)->codeNext++ = (unsigned char) (op); \
  873.     *(envPtr)->codeNext++ = \
  874.         (unsigned char) ((unsigned int) (i) >> 24); \
  875.     *(envPtr)->codeNext++ = \
  876.         (unsigned char) ((unsigned int) (i) >> 16); \
  877.     *(envPtr)->codeNext++ = \
  878.         (unsigned char) ((unsigned int) (i) >>  8); \
  879.     *(envPtr)->codeNext++ = \
  880.         (unsigned char) ((unsigned int) (i)      )
  881.     
  882. #define TclEmitInstUInt1(op, i, envPtr) \
  883.     TclEmitInstInt1((op), (i), (envPtr))
  884.  
  885. #define TclEmitInstUInt4(op, i, envPtr) \
  886.     TclEmitInstInt4((op), (i), (envPtr))
  887.     
  888. /*
  889.  * Macro to push a Tcl object onto the Tcl evaluation stack. It emits the
  890.  * object's one or four byte array index into the CompileEnv's code
  891.  * array. These support, respectively, a maximum of 256 (2**8) and 2**32
  892.  * objects in a CompileEnv. The ANSI C "prototype" for this macro is:
  893.  *
  894.  * EXTERN void    TclEmitPush _ANSI_ARGS_((int objIndex, CompileEnv *envPtr));
  895.  */
  896.  
  897. #define TclEmitPush(objIndex, envPtr) \
  898.     if ((objIndex) <= 255) { \
  899.     TclEmitInstUInt1(INST_PUSH1, (objIndex), (envPtr)); \
  900.     } else { \
  901.     TclEmitInstUInt4(INST_PUSH4, (objIndex), (envPtr)); \
  902.     }
  903.  
  904. /*
  905.  * Macros to update a (signed or unsigned) integer starting at a pointer.
  906.  * The two variants depend on the number of bytes. The ANSI C "prototypes"
  907.  * for these macros are:
  908.  *
  909.  * EXTERN void    TclStoreInt1AtPtr _ANSI_ARGS_((int i, unsigned char *p));
  910.  * EXTERN void    TclStoreInt4AtPtr _ANSI_ARGS_((int i, unsigned char *p));
  911.  */
  912.     
  913. #define TclStoreInt1AtPtr(i, p) \
  914.     *(p)   = (unsigned char) ((unsigned int) (i))
  915.     
  916. #define TclStoreInt4AtPtr(i, p) \
  917.     *(p)   = (unsigned char) ((unsigned int) (i) >> 24); \
  918.     *(p+1) = (unsigned char) ((unsigned int) (i) >> 16); \
  919.     *(p+2) = (unsigned char) ((unsigned int) (i) >>  8); \
  920.     *(p+3) = (unsigned char) ((unsigned int) (i)      )
  921.  
  922. /*
  923.  * Macros to update instructions at a particular pc with a new op code
  924.  * and a (signed or unsigned) int operand. The ANSI C "prototypes" for
  925.  * these macros are:
  926.  *
  927.  * EXTERN void    TclUpdateInstInt1AtPc _ANSI_ARGS_((unsigned char op, int i,
  928.  *            unsigned char *pc));
  929.  * EXTERN void    TclUpdateInstInt4AtPc _ANSI_ARGS_((unsigned char op, int i,
  930.  *            unsigned char *pc));
  931.  */
  932.  
  933. #define TclUpdateInstInt1AtPc(op, i, pc) \
  934.     *(pc) = (unsigned char) (op); \
  935.     TclStoreInt1AtPtr((i), ((pc)+1))
  936.  
  937. #define TclUpdateInstInt4AtPc(op, i, pc) \
  938.     *(pc) = (unsigned char) (op); \
  939.     TclStoreInt4AtPtr((i), ((pc)+1))
  940.     
  941. /*
  942.  * Macros to get a signed integer (GET_INT{1,2}) or an unsigned int
  943.  * (GET_UINT{1,2}) from a pointer. There are two variants for each
  944.  * return type that depend on the number of bytes fetched.
  945.  * The ANSI C "prototypes" for these macros are:
  946.  *
  947.  * EXTERN int            TclGetInt1AtPtr  _ANSI_ARGS_((unsigned char *p));
  948.  * EXTERN int            TclGetInt4AtPtr  _ANSI_ARGS_((unsigned char *p));
  949.  * EXTERN unsigned int    TclGetUInt1AtPtr _ANSI_ARGS_((unsigned char *p));
  950.  * EXTERN unsigned int    TclGetUInt4AtPtr _ANSI_ARGS_((unsigned char *p));
  951.  */
  952.  
  953. /*
  954.  * The TclGetInt1AtPtr macro is tricky because we want to do sign
  955.  * extension on the 1-byte value. Unfortunately the "char" type isn't
  956.  * signed on all platforms so sign-extension doesn't always happen
  957.  * automatically. Sometimes we can explicitly declare the pointer to be
  958.  * signed, but other times we have to explicitly sign-extend the value
  959.  * in software.
  960.  */
  961.  
  962. #ifndef __CHAR_UNSIGNED__
  963. #   define TclGetInt1AtPtr(p) ((int) *((char *) p))
  964. #else
  965. #   ifdef HAVE_SIGNED_CHAR
  966. #    define TclGetInt1AtPtr(p) ((int) *((signed char *) p))
  967. #    else
  968. #    define TclGetInt1AtPtr(p) (((int) *((char *) p)) \
  969.         | ((*(p) & 0200) ? (-256) : 0))
  970. #    endif
  971. #endif
  972.  
  973. #define TclGetInt4AtPtr(p) (((int) TclGetInt1AtPtr(p) << 24) | \
  974.                                   (*((p)+1) << 16) | \
  975.                           (*((p)+2) <<  8) | \
  976.                           (*((p)+3)))
  977.  
  978. #define TclGetUInt1AtPtr(p) ((unsigned int) *(p))
  979. #define TclGetUInt4AtPtr(p) ((unsigned int) (*(p)     << 24) | \
  980.                                     (*((p)+1) << 16) | \
  981.                             (*((p)+2) <<  8) | \
  982.                             (*((p)+3)))
  983.  
  984. /*
  985.  * Macros used to compute the minimum and maximum of two integers.
  986.  * The ANSI C "prototypes" for these macros are:
  987.  *
  988.  * EXTERN int  TclMin _ANSI_ARGS_((int i, int j));
  989.  * EXTERN int  TclMax _ANSI_ARGS_((int i, int j));
  990.  */
  991.  
  992. #define TclMin(i, j)   ((((int) i) < ((int) j))? (i) : (j))
  993. #define TclMax(i, j)   ((((int) i) > ((int) j))? (i) : (j))
  994.  
  995. /*
  996.  * Macro used to compute the offset of the current instruction in the
  997.  * bytecode instruction stream. The ANSI C "prototypes" for this macro is:
  998.  *
  999.  * EXTERN int  TclCurrCodeOffset _ANSI_ARGS_((void));
  1000.  */
  1001.  
  1002. #define TclCurrCodeOffset()  ((envPtr)->codeNext - (envPtr)->codeStart)
  1003.  
  1004. /*
  1005.  * Upper bound for legal jump distances. Checked during compilation if
  1006.  * debugging.
  1007.  */
  1008.  
  1009. #define MAX_JUMP_DIST   5000
  1010.  
  1011. #endif /* _TCLCOMPILATION */
  1012.  
  1013.