os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/generic/tclCompile.h
First public contribution.
4 * Copyright (c) 1996-1998 Sun Microsystems, Inc.
5 * Copyright (c) 1998-2000 by Scriptics Corporation.
6 * Copyright (c) 2001 by Kevin B. Kenny. All rights reserved.
8 * See the file "license.terms" for information on usage and redistribution
9 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 * RCS: @(#) $Id: tclCompile.h,v 1.33.2.1 2006/11/28 22:20:00 andreas_kupries Exp $
14 #ifndef _TCLCOMPILATION
15 #define _TCLCOMPILATION 1
22 # undef TCL_STORAGE_CLASS
23 # define TCL_STORAGE_CLASS DLLEXPORT
27 *------------------------------------------------------------------------
28 * Variables related to compilation. These are used in tclCompile.c,
29 * tclExecute.c, tclBasic.c, and their clients.
30 *------------------------------------------------------------------------
33 #ifdef TCL_COMPILE_DEBUG
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".
43 extern int tclTraceCompile;
46 #ifdef TCL_COMPILE_DEBUG
48 * Variable that controls whether execution tracing is enabled and, if so,
49 * what level of tracing is desired:
50 * 0: no execution tracing
51 * 1: trace invocations of Tcl procs only
52 * 2: trace invocations of all (not compiled away) commands
53 * 3: display each instruction executed
54 * This variable is linked to the Tcl variable "tcl_traceExec".
57 extern int tclTraceExec;
61 *------------------------------------------------------------------------
62 * Data structures related to compilation.
63 *------------------------------------------------------------------------
67 * The structure used to implement Tcl "exceptions" (exceptional returns):
68 * for example, those generated in loops by the break and continue commands,
69 * and those generated by scripts and caught by the catch command. This
70 * ExceptionRange structure describes a range of code (e.g., a loop body),
71 * the kind of exceptions (e.g., a break or continue) that might occur, and
72 * the PC offsets to jump to if a matching exception does occur. Exception
73 * ranges can nest so this structure includes a nesting level that is used
74 * at runtime to find the closest exception range surrounding a PC. For
75 * example, when a break command is executed, the ExceptionRange structure
76 * for the most deeply nested loop, if any, is found and used. These
77 * structures are also generated for the "next" subcommands of for loops
78 * since a break there terminates the for command. This means a for command
79 * actually generates two LoopInfo structures.
83 LOOP_EXCEPTION_RANGE, /* Exception's range is part of a loop.
84 * Break and continue "exceptions" cause
85 * jumps to appropriate PC offsets. */
86 CATCH_EXCEPTION_RANGE /* Exception's range is controlled by a
87 * catch command. Errors in the range cause
88 * a jump to a catch PC offset. */
91 typedef struct ExceptionRange {
92 ExceptionRangeType type; /* The kind of ExceptionRange. */
93 int nestingLevel; /* Static depth of the exception range.
94 * Used to find the most deeply-nested
95 * range surrounding a PC at runtime. */
96 int codeOffset; /* Offset of the first instruction byte of
98 int numCodeBytes; /* Number of bytes in the code range. */
99 int breakOffset; /* If LOOP_EXCEPTION_RANGE, the target PC
100 * offset for a break command in the range. */
101 int continueOffset; /* If LOOP_EXCEPTION_RANGE and not -1, the
102 * target PC offset for a continue command in
103 * the code range. Otherwise, ignore this range
104 * when processing a continue command. */
105 int catchOffset; /* If a CATCH_EXCEPTION_RANGE, the target PC
106 * offset for any "exception" in range. */
110 * Structure used to map between instruction pc and source locations. It
111 * defines for each compiled Tcl command its code's starting offset and
112 * its source's starting offset and length. Note that the code offset
113 * increases monotonically: that is, the table is sorted in code offset
114 * order. The source offset is not monotonic.
117 typedef struct CmdLocation {
118 int codeOffset; /* Offset of first byte of command code. */
119 int numCodeBytes; /* Number of bytes for command's code. */
120 int srcOffset; /* Offset of first char of the command. */
121 int numSrcBytes; /* Number of command source chars. */
127 * Structure to record additional location information for byte code.
128 * This information is internal and not saved. I.e. tbcload'ed code
129 * will not have this information. It records the lines for all words
130 * of all commands found in the byte code. The association with a
131 * ByteCode structure BC is done through the 'lineBCPtr' HashTable in
132 * Interp, keyed by the address of BC. Also recorded is information
133 * coming from the context, i.e. type of the frame and associated
134 * information, like the path of a sourced file.
138 int srcOffset; /* cmd location to find the entry */
140 int* line; /* line information for all words in the command */
142 typedef struct ExtCmdLoc {
143 int type; /* Context type */
144 Tcl_Obj* path; /* Path of the sourced file the command is in */
145 ECL* loc; /* Command word locations (lines) */
146 int nloc; /* Number of allocated entries in 'loc' */
147 int nuloc; /* Number of used entries in 'loc' */
152 * CompileProcs need the ability to record information during compilation
153 * that can be used by bytecode instructions during execution. The AuxData
154 * structure provides this "auxiliary data" mechanism. An arbitrary number
155 * of these structures can be stored in the ByteCode record (during
156 * compilation they are stored in a CompileEnv structure). Each AuxData
157 * record holds one word of client-specified data (often a pointer) and is
158 * given an index that instructions can later use to look up the structure
161 * The following definitions declare the types of procedures that are called
162 * to duplicate or free this auxiliary data when the containing ByteCode
163 * objects are duplicated and freed. Pointers to these procedures are kept
164 * in the AuxData structure.
167 typedef ClientData (AuxDataDupProc) _ANSI_ARGS_((ClientData clientData));
168 typedef void (AuxDataFreeProc) _ANSI_ARGS_((ClientData clientData));
171 * We define a separate AuxDataType struct to hold type-related information
172 * for the AuxData structure. This separation makes it possible for clients
173 * outside of the TCL core to manipulate (in a limited fashion!) AuxData;
174 * for example, it makes it possible to pickle and unpickle AuxData structs.
177 typedef struct AuxDataType {
178 char *name; /* the name of the type. Types can be
179 * registered and found by name */
180 AuxDataDupProc *dupProc; /* Callback procedure to invoke when the
181 * aux data is duplicated (e.g., when the
182 * ByteCode structure containing the aux
183 * data is duplicated). NULL means just
184 * copy the source clientData bits; no
185 * proc need be called. */
186 AuxDataFreeProc *freeProc; /* Callback procedure to invoke when the
187 * aux data is freed. NULL means no
188 * proc need be called. */
192 * The definition of the AuxData structure that holds information created
193 * during compilation by CompileProcs and used by instructions during
197 typedef struct AuxData {
198 AuxDataType *type; /* pointer to the AuxData type associated with
199 * this ClientData. */
200 ClientData clientData; /* The compilation data itself. */
204 * Structure defining the compilation environment. After compilation, fields
205 * describing bytecode instructions are copied out into the more compact
206 * ByteCode structure defined below.
209 #define COMPILEENV_INIT_CODE_BYTES 250
210 #define COMPILEENV_INIT_NUM_OBJECTS 60
211 #define COMPILEENV_INIT_EXCEPT_RANGES 5
212 #define COMPILEENV_INIT_CMD_MAP_SIZE 40
213 #define COMPILEENV_INIT_AUX_DATA_SIZE 5
215 typedef struct CompileEnv {
216 Interp *iPtr; /* Interpreter containing the code being
217 * compiled. Commands and their compile
218 * procs are specific to an interpreter so
219 * the code emitted will depend on the
221 char *source; /* The source string being compiled by
222 * SetByteCodeFromAny. This pointer is not
223 * owned by the CompileEnv and must not be
224 * freed or changed by it. */
225 int numSrcBytes; /* Number of bytes in source. */
226 Proc *procPtr; /* If a procedure is being compiled, a
227 * pointer to its Proc structure; otherwise
228 * NULL. Used to compile local variables.
229 * Set from information provided by
230 * ObjInterpProc in tclProc.c. */
231 int numCommands; /* Number of commands compiled. */
232 int exceptDepth; /* Current exception range nesting level;
233 * -1 if not in any range currently. */
234 int maxExceptDepth; /* Max nesting level of exception ranges;
235 * -1 if no ranges have been compiled. */
236 int maxStackDepth; /* Maximum number of stack elements needed
237 * to execute the code. Set by compilation
238 * procedures before returning. */
239 int currStackDepth; /* Current stack depth. */
240 LiteralTable localLitTable; /* Contains LiteralEntry's describing
241 * all Tcl objects referenced by this
242 * compiled code. Indexed by the string
243 * representations of the literals. Used to
244 * avoid creating duplicate objects. */
245 unsigned char *codeStart; /* Points to the first byte of the code. */
246 unsigned char *codeNext; /* Points to next code array byte to use. */
247 unsigned char *codeEnd; /* Points just after the last allocated
248 * code array byte. */
249 int mallocedCodeArray; /* Set 1 if code array was expanded
250 * and codeStart points into the heap.*/
251 LiteralEntry *literalArrayPtr;
252 /* Points to start of LiteralEntry array. */
253 int literalArrayNext; /* Index of next free object array entry. */
254 int literalArrayEnd; /* Index just after last obj array entry. */
255 int mallocedLiteralArray; /* 1 if object array was expanded and
256 * objArray points into the heap, else 0. */
257 ExceptionRange *exceptArrayPtr;
258 /* Points to start of the ExceptionRange
260 int exceptArrayNext; /* Next free ExceptionRange array index.
261 * exceptArrayNext is the number of ranges
262 * and (exceptArrayNext-1) is the index of
263 * the current range's array entry. */
264 int exceptArrayEnd; /* Index after the last ExceptionRange
266 int mallocedExceptArray; /* 1 if ExceptionRange array was expanded
267 * and exceptArrayPtr points in heap,
269 CmdLocation *cmdMapPtr; /* Points to start of CmdLocation array.
270 * numCommands is the index of the next
271 * entry to use; (numCommands-1) is the
272 * entry index for the last command. */
273 int cmdMapEnd; /* Index after last CmdLocation entry. */
274 int mallocedCmdMap; /* 1 if command map array was expanded and
275 * cmdMapPtr points in the heap, else 0. */
276 AuxData *auxDataArrayPtr; /* Points to auxiliary data array start. */
277 int auxDataArrayNext; /* Next free compile aux data array index.
278 * auxDataArrayNext is the number of aux
279 * data items and (auxDataArrayNext-1) is
280 * index of current aux data array entry. */
281 int auxDataArrayEnd; /* Index after last aux data array entry. */
282 int mallocedAuxDataArray; /* 1 if aux data array was expanded and
283 * auxDataArrayPtr points in heap else 0. */
284 unsigned char staticCodeSpace[COMPILEENV_INIT_CODE_BYTES];
285 /* Initial storage for code. */
286 LiteralEntry staticLiteralSpace[COMPILEENV_INIT_NUM_OBJECTS];
287 /* Initial storage of LiteralEntry array. */
288 ExceptionRange staticExceptArraySpace[COMPILEENV_INIT_EXCEPT_RANGES];
289 /* Initial ExceptionRange array storage. */
290 CmdLocation staticCmdMapSpace[COMPILEENV_INIT_CMD_MAP_SIZE];
291 /* Initial storage for cmd location map. */
292 AuxData staticAuxDataArraySpace[COMPILEENV_INIT_AUX_DATA_SIZE];
293 /* Initial storage for aux data array. */
296 ExtCmdLoc* extCmdMapPtr; /* Extended command location information
297 * for 'info frame'. */
298 int line; /* First line of the script, based on the
299 * invoking context, then the line of the
300 * command currently compiled. */
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 literal object array, the ExceptionRange array,
309 * the CmdLocation map, and the compilation AuxData array.
313 * A PRECOMPILED bytecode struct is one that was generated from a compiled
314 * image rather than implicitly compiled from source
316 #define TCL_BYTECODE_PRECOMPILED 0x0001
318 typedef struct ByteCode {
319 TclHandle interpHandle; /* Handle for interpreter containing the
320 * compiled code. Commands and their compile
321 * procs are specific to an interpreter so the
322 * code emitted will depend on the
324 int compileEpoch; /* Value of iPtr->compileEpoch when this
325 * ByteCode was compiled. Used to invalidate
326 * code when, e.g., commands with compile
327 * procs are redefined. */
328 Namespace *nsPtr; /* Namespace context in which this code
329 * was compiled. If the code is executed
330 * if a different namespace, it must be
332 int nsEpoch; /* Value of nsPtr->resolverEpoch when this
333 * ByteCode was compiled. Used to invalidate
334 * code when new namespace resolution rules
335 * are put into effect. */
336 int refCount; /* Reference count: set 1 when created
337 * plus 1 for each execution of the code
338 * currently active. This structure can be
339 * freed when refCount becomes zero. */
340 unsigned int flags; /* flags describing state for the codebyte.
341 * this variable holds ORed values from the
342 * TCL_BYTECODE_ masks defined above */
343 char *source; /* The source string from which this
344 * ByteCode was compiled. Note that this
345 * pointer is not owned by the ByteCode and
346 * must not be freed or modified by it. */
347 Proc *procPtr; /* If the ByteCode was compiled from a
348 * procedure body, this is a pointer to its
349 * Proc structure; otherwise NULL. This
350 * pointer is also not owned by the ByteCode
351 * and must not be freed by it. */
352 size_t structureSize; /* Number of bytes in the ByteCode structure
353 * itself. Does not include heap space for
354 * literal Tcl objects or storage referenced
355 * by AuxData entries. */
356 int numCommands; /* Number of commands compiled. */
357 int numSrcBytes; /* Number of source bytes compiled. */
358 int numCodeBytes; /* Number of code bytes. */
359 int numLitObjects; /* Number of objects in literal array. */
360 int numExceptRanges; /* Number of ExceptionRange array elems. */
361 int numAuxDataItems; /* Number of AuxData items. */
362 int numCmdLocBytes; /* Number of bytes needed for encoded
363 * command location information. */
364 int maxExceptDepth; /* Maximum nesting level of ExceptionRanges;
365 * -1 if no ranges were compiled. */
366 int maxStackDepth; /* Maximum number of stack elements needed
367 * to execute the code. */
368 unsigned char *codeStart; /* Points to the first byte of the code.
369 * This is just after the final ByteCode
370 * member cmdMapPtr. */
371 Tcl_Obj **objArrayPtr; /* Points to the start of the literal
372 * object array. This is just after the
374 ExceptionRange *exceptArrayPtr;
375 /* Points to the start of the ExceptionRange
376 * array. This is just after the last
377 * object in the object array. */
378 AuxData *auxDataArrayPtr; /* Points to the start of the auxiliary data
379 * array. This is just after the last entry
380 * in the ExceptionRange array. */
381 unsigned char *codeDeltaStart;
382 /* Points to the first of a sequence of
383 * bytes that encode the change in the
384 * starting offset of each command's code.
385 * If -127<=delta<=127, it is encoded as 1
386 * byte, otherwise 0xFF (128) appears and
387 * the delta is encoded by the next 4 bytes.
388 * Code deltas are always positive. This
389 * sequence is just after the last entry in
390 * the AuxData array. */
391 unsigned char *codeLengthStart;
392 /* Points to the first of a sequence of
393 * bytes that encode the length of each
394 * command's code. The encoding is the same
395 * as for code deltas. Code lengths are
396 * always positive. This sequence is just
397 * after the last entry in the code delta
399 unsigned char *srcDeltaStart;
400 /* Points to the first of a sequence of
401 * bytes that encode the change in the
402 * starting offset of each command's source.
403 * The encoding is the same as for code
404 * deltas. Source deltas can be negative.
405 * This sequence is just after the last byte
406 * in the code length sequence. */
407 unsigned char *srcLengthStart;
408 /* Points to the first of a sequence of
409 * bytes that encode the length of each
410 * command's source. The encoding is the
411 * same as for code deltas. Source lengths
412 * are always positive. This sequence is
413 * just after the last byte in the source
415 #ifdef TCL_COMPILE_STATS
416 Tcl_Time createTime; /* Absolute time when the ByteCode was
418 #endif /* TCL_COMPILE_STATS */
422 * Opcodes for the Tcl bytecode instructions. These must correspond to
423 * the entries in the table of instruction descriptions,
424 * tclInstructionTable, in tclCompile.c. Also, the order and number of
425 * the expression opcodes (e.g., INST_LOR) must match the entries in
426 * the array operatorStrings in tclExecute.c.
435 #define INST_CONCAT1 5
436 #define INST_INVOKE_STK1 6
437 #define INST_INVOKE_STK4 7
438 #define INST_EVAL_STK 8
439 #define INST_EXPR_STK 9
441 /* Opcodes 10 to 23 */
442 #define INST_LOAD_SCALAR1 10
443 #define INST_LOAD_SCALAR4 11
444 #define INST_LOAD_SCALAR_STK 12
445 #define INST_LOAD_ARRAY1 13
446 #define INST_LOAD_ARRAY4 14
447 #define INST_LOAD_ARRAY_STK 15
448 #define INST_LOAD_STK 16
449 #define INST_STORE_SCALAR1 17
450 #define INST_STORE_SCALAR4 18
451 #define INST_STORE_SCALAR_STK 19
452 #define INST_STORE_ARRAY1 20
453 #define INST_STORE_ARRAY4 21
454 #define INST_STORE_ARRAY_STK 22
455 #define INST_STORE_STK 23
457 /* Opcodes 24 to 33 */
458 #define INST_INCR_SCALAR1 24
459 #define INST_INCR_SCALAR_STK 25
460 #define INST_INCR_ARRAY1 26
461 #define INST_INCR_ARRAY_STK 27
462 #define INST_INCR_STK 28
463 #define INST_INCR_SCALAR1_IMM 29
464 #define INST_INCR_SCALAR_STK_IMM 30
465 #define INST_INCR_ARRAY1_IMM 31
466 #define INST_INCR_ARRAY_STK_IMM 32
467 #define INST_INCR_STK_IMM 33
469 /* Opcodes 34 to 39 */
470 #define INST_JUMP1 34
471 #define INST_JUMP4 35
472 #define INST_JUMP_TRUE1 36
473 #define INST_JUMP_TRUE4 37
474 #define INST_JUMP_FALSE1 38
475 #define INST_JUMP_FALSE4 39
477 /* Opcodes 40 to 64 */
480 #define INST_BITOR 42
481 #define INST_BITXOR 43
482 #define INST_BITAND 44
489 #define INST_LSHIFT 51
490 #define INST_RSHIFT 52
496 #define INST_UPLUS 58
497 #define INST_UMINUS 59
498 #define INST_BITNOT 60
500 #define INST_CALL_BUILTIN_FUNC1 62
501 #define INST_CALL_FUNC1 63
502 #define INST_TRY_CVT_TO_NUMERIC 64
504 /* Opcodes 65 to 66 */
505 #define INST_BREAK 65
506 #define INST_CONTINUE 66
508 /* Opcodes 67 to 68 */
509 #define INST_FOREACH_START4 67
510 #define INST_FOREACH_STEP4 68
512 /* Opcodes 69 to 72 */
513 #define INST_BEGIN_CATCH4 69
514 #define INST_END_CATCH 70
515 #define INST_PUSH_RESULT 71
516 #define INST_PUSH_RETURN_CODE 72
518 /* Opcodes 73 to 78 */
519 #define INST_STR_EQ 73
520 #define INST_STR_NEQ 74
521 #define INST_STR_CMP 75
522 #define INST_STR_LEN 76
523 #define INST_STR_INDEX 77
524 #define INST_STR_MATCH 78
526 /* Opcodes 78 to 81 */
528 #define INST_LIST_INDEX 80
529 #define INST_LIST_LENGTH 81
531 /* Opcodes 82 to 87 */
532 #define INST_APPEND_SCALAR1 82
533 #define INST_APPEND_SCALAR4 83
534 #define INST_APPEND_ARRAY1 84
535 #define INST_APPEND_ARRAY4 85
536 #define INST_APPEND_ARRAY_STK 86
537 #define INST_APPEND_STK 87
539 /* Opcodes 88 to 93 */
540 #define INST_LAPPEND_SCALAR1 88
541 #define INST_LAPPEND_SCALAR4 89
542 #define INST_LAPPEND_ARRAY1 90
543 #define INST_LAPPEND_ARRAY4 91
544 #define INST_LAPPEND_ARRAY_STK 92
545 #define INST_LAPPEND_STK 93
547 /* TIP #22 - LINDEX operator with flat arg list */
549 #define INST_LIST_INDEX_MULTI 94
552 * TIP #33 - 'lset' command. Code gen also required a Forth-like
557 #define INST_LSET_LIST 96
558 #define INST_LSET_FLAT 97
560 /* The last opcode */
561 #define LAST_INST_OPCODE 97
564 * Table describing the Tcl bytecode instructions: their name (for
565 * displaying code), total number of code bytes required (including
566 * operand bytes), and a description of the type of each operand.
567 * These operand types include signed and unsigned integers of length
568 * one and four bytes. The unsigned integers are used for indexes or
569 * for, e.g., the count of objects to push in a "push" instruction.
572 #define MAX_INSTRUCTION_OPERANDS 2
574 typedef enum InstOperandType {
576 OPERAND_INT1, /* One byte signed integer. */
577 OPERAND_INT4, /* Four byte signed integer. */
578 OPERAND_UINT1, /* One byte unsigned integer. */
579 OPERAND_UINT4 /* Four byte unsigned integer. */
582 typedef struct InstructionDesc {
583 char *name; /* Name of instruction. */
584 int numBytes; /* Total number of bytes for instruction. */
585 int stackEffect; /* The worst-case balance stack effect of the
586 * instruction, used for stack requirements
587 * computations. The value INT_MIN signals
588 * that the instruction's worst case effect
591 int numOperands; /* Number of operands. */
592 InstOperandType opTypes[MAX_INSTRUCTION_OPERANDS];
593 /* The type of each operand. */
596 extern InstructionDesc tclInstructionTable[];
599 * Definitions of the values of the INST_CALL_BUILTIN_FUNC instruction's
600 * operand byte. Each value denotes a builtin Tcl math function. These
601 * values must correspond to the entries in the tclBuiltinFuncTable array
602 * below and to the values stored in the tclInt.h MathFunc structure's
603 * builtinFuncIndex field.
606 #define BUILTIN_FUNC_ACOS 0
607 #define BUILTIN_FUNC_ASIN 1
608 #define BUILTIN_FUNC_ATAN 2
609 #define BUILTIN_FUNC_ATAN2 3
610 #define BUILTIN_FUNC_CEIL 4
611 #define BUILTIN_FUNC_COS 5
612 #define BUILTIN_FUNC_COSH 6
613 #define BUILTIN_FUNC_EXP 7
614 #define BUILTIN_FUNC_FLOOR 8
615 #define BUILTIN_FUNC_FMOD 9
616 #define BUILTIN_FUNC_HYPOT 10
617 #define BUILTIN_FUNC_LOG 11
618 #define BUILTIN_FUNC_LOG10 12
619 #define BUILTIN_FUNC_POW 13
620 #define BUILTIN_FUNC_SIN 14
621 #define BUILTIN_FUNC_SINH 15
622 #define BUILTIN_FUNC_SQRT 16
623 #define BUILTIN_FUNC_TAN 17
624 #define BUILTIN_FUNC_TANH 18
625 #define BUILTIN_FUNC_ABS 19
626 #define BUILTIN_FUNC_DOUBLE 20
627 #define BUILTIN_FUNC_INT 21
628 #define BUILTIN_FUNC_RAND 22
629 #define BUILTIN_FUNC_ROUND 23
630 #define BUILTIN_FUNC_SRAND 24
631 #define BUILTIN_FUNC_WIDE 25
633 #define LAST_BUILTIN_FUNC 25
636 * Table describing the built-in math functions. Entries in this table are
637 * indexed by the values of the INST_CALL_BUILTIN_FUNC instruction's
641 typedef int (CallBuiltinFuncProc) _ANSI_ARGS_((Tcl_Interp *interp,
642 ExecEnv *eePtr, ClientData clientData));
645 char *name; /* Name of function. */
646 int numArgs; /* Number of arguments for function. */
647 Tcl_ValueType argTypes[MAX_MATH_ARGS];
648 /* Acceptable types for each argument. */
649 CallBuiltinFuncProc *proc; /* Procedure implementing this function. */
650 ClientData clientData; /* Additional argument to pass to the
651 * function when invoking it. */
654 extern BuiltinFunc tclBuiltinFuncTable[];
657 * Compilation of some Tcl constructs such as if commands and the logical or
658 * (||) and logical and (&&) operators in expressions requires the
659 * generation of forward jumps. Since the PC target of these jumps isn't
660 * known when the jumps are emitted, we record the offset of each jump in an
661 * array of JumpFixup structures. There is one array for each sequence of
662 * jumps to one target PC. When we learn the target PC, we update the jumps
663 * with the correct distance. Also, if the distance is too great (> 127
664 * bytes), we replace the single-byte jump with a four byte jump
665 * instruction, move the instructions after the jump down, and update the
666 * code offsets for any commands between the jump and the target.
670 TCL_UNCONDITIONAL_JUMP,
675 typedef struct JumpFixup {
676 TclJumpType jumpType; /* Indicates the kind of jump. */
677 int codeOffset; /* Offset of the first byte of the one-byte
678 * forward jump's code. */
679 int cmdIndex; /* Index of the first command after the one
680 * for which the jump was emitted. Used to
681 * update the code offsets for subsequent
682 * commands if the two-byte jump at jumpPc
683 * must be replaced with a five-byte one. */
684 int exceptIndex; /* Index of the first range entry in the
685 * ExceptionRange array after the current
686 * one. This field is used to adjust the
687 * code offsets in subsequent ExceptionRange
688 * records when a jump is grown from 2 bytes
692 #define JUMPFIXUP_INIT_ENTRIES 10
694 typedef struct JumpFixupArray {
695 JumpFixup *fixup; /* Points to start of jump fixup array. */
696 int next; /* Index of next free array entry. */
697 int end; /* Index of last usable entry in array. */
698 int mallocedArray; /* 1 if array was expanded and fixups points
699 * into the heap, else 0. */
700 JumpFixup staticFixupSpace[JUMPFIXUP_INIT_ENTRIES];
701 /* Initial storage for jump fixup array. */
705 * The structure describing one variable list of a foreach command. Note
706 * that only foreach commands inside procedure bodies are compiled inline so
707 * a ForeachVarList structure always describes local variables. Furthermore,
708 * only scalar variables are supported for inline-compiled foreach loops.
711 typedef struct ForeachVarList {
712 int numVars; /* The number of variables in the list. */
713 int varIndexes[1]; /* An array of the indexes ("slot numbers")
714 * for each variable in the procedure's
715 * array of local variables. Only scalar
716 * variables are supported. The actual
717 * size of this field will be large enough
718 * to numVars indexes. THIS MUST BE THE
719 * LAST FIELD IN THE STRUCTURE! */
723 * Structure used to hold information about a foreach command that is needed
724 * during program execution. These structures are stored in CompileEnv and
725 * ByteCode structures as auxiliary data.
728 typedef struct ForeachInfo {
729 int numLists; /* The number of both the variable and value
730 * lists of the foreach command. */
731 int firstValueTemp; /* Index of the first temp var in a proc
732 * frame used to point to a value list. */
733 int loopCtTemp; /* Index of temp var in a proc frame
734 * holding the loop's iteration count. Used
735 * to determine next value list element to
736 * assign each loop var. */
737 ForeachVarList *varLists[1];/* An array of pointers to ForeachVarList
738 * structures describing each var list. The
739 * actual size of this field will be large
740 * enough to numVars indexes. THIS MUST BE
741 * THE LAST FIELD IN THE STRUCTURE! */
744 extern AuxDataType tclForeachInfoType;
748 *----------------------------------------------------------------
749 * Procedures exported by tclBasic.c to be used within the engine.
750 *----------------------------------------------------------------
753 EXTERN int TclEvalObjvInternal _ANSI_ARGS_((Tcl_Interp *interp, int objc,
754 Tcl_Obj *CONST objv[], CONST char *command, int length,
756 EXTERN int TclInterpReady _ANSI_ARGS_((Tcl_Interp *interp));
760 *----------------------------------------------------------------
761 * Procedures exported by the engine to be used by tclBasic.c
762 *----------------------------------------------------------------
766 EXTERN int TclCompEvalObj _ANSI_ARGS_((Tcl_Interp *interp,
769 EXTERN int TclCompEvalObj _ANSI_ARGS_((Tcl_Interp *interp,
770 Tcl_Obj *objPtr, CONST CmdFrame* invoker,
775 *----------------------------------------------------------------
776 * Procedures shared among Tcl bytecode compilation and execution
777 * modules but not used outside:
778 *----------------------------------------------------------------
781 EXTERN void TclCleanupByteCode _ANSI_ARGS_((ByteCode *codePtr));
782 EXTERN int TclCompileCmdWord _ANSI_ARGS_((Tcl_Interp *interp,
783 Tcl_Token *tokenPtr, int count,
784 CompileEnv *envPtr));
785 EXTERN int TclCompileExpr _ANSI_ARGS_((Tcl_Interp *interp,
786 CONST char *script, int numBytes,
787 CompileEnv *envPtr));
788 EXTERN int TclCompileExprWords _ANSI_ARGS_((Tcl_Interp *interp,
789 Tcl_Token *tokenPtr, int numWords,
790 CompileEnv *envPtr));
791 EXTERN int TclCompileScript _ANSI_ARGS_((Tcl_Interp *interp,
792 CONST char *script, int numBytes, int nested,
793 CompileEnv *envPtr));
794 EXTERN int TclCompileTokens _ANSI_ARGS_((Tcl_Interp *interp,
795 Tcl_Token *tokenPtr, int count,
796 CompileEnv *envPtr));
797 EXTERN int TclCreateAuxData _ANSI_ARGS_((ClientData clientData,
798 AuxDataType *typePtr, CompileEnv *envPtr));
799 EXTERN int TclCreateExceptRange _ANSI_ARGS_((
800 ExceptionRangeType type, CompileEnv *envPtr));
801 EXTERN ExecEnv * TclCreateExecEnv _ANSI_ARGS_((Tcl_Interp *interp));
802 EXTERN void TclDeleteExecEnv _ANSI_ARGS_((ExecEnv *eePtr));
803 EXTERN void TclDeleteLiteralTable _ANSI_ARGS_((
804 Tcl_Interp *interp, LiteralTable *tablePtr));
805 EXTERN void TclEmitForwardJump _ANSI_ARGS_((CompileEnv *envPtr,
806 TclJumpType jumpType, JumpFixup *jumpFixupPtr));
807 EXTERN ExceptionRange * TclGetExceptionRangeForPc _ANSI_ARGS_((
808 unsigned char *pc, int catchOnly,
810 EXTERN void TclExpandJumpFixupArray _ANSI_ARGS_((
811 JumpFixupArray *fixupArrayPtr));
812 EXTERN void TclFinalizeAuxDataTypeTable _ANSI_ARGS_((void));
813 EXTERN int TclFindCompiledLocal _ANSI_ARGS_((CONST char *name,
814 int nameChars, int create, int flags,
816 EXTERN LiteralEntry * TclLookupLiteralEntry _ANSI_ARGS_((
817 Tcl_Interp *interp, Tcl_Obj *objPtr));
818 EXTERN int TclFixupForwardJump _ANSI_ARGS_((
819 CompileEnv *envPtr, JumpFixup *jumpFixupPtr,
820 int jumpDist, int distThreshold));
821 EXTERN void TclFreeCompileEnv _ANSI_ARGS_((CompileEnv *envPtr));
822 EXTERN void TclFreeJumpFixupArray _ANSI_ARGS_((
823 JumpFixupArray *fixupArrayPtr));
824 EXTERN void TclInitAuxDataTypeTable _ANSI_ARGS_((void));
825 EXTERN void TclInitByteCodeObj _ANSI_ARGS_((Tcl_Obj *objPtr,
826 CompileEnv *envPtr));
827 EXTERN void TclInitCompilation _ANSI_ARGS_((void));
829 EXTERN void TclInitCompileEnv _ANSI_ARGS_((Tcl_Interp *interp,
830 CompileEnv *envPtr, char *string,
833 EXTERN void TclInitCompileEnv _ANSI_ARGS_((Tcl_Interp *interp,
834 CompileEnv *envPtr, char *string,
835 int numBytes, CONST CmdFrame* invoker, int word));
837 EXTERN void TclInitJumpFixupArray _ANSI_ARGS_((
838 JumpFixupArray *fixupArrayPtr));
839 EXTERN void TclInitLiteralTable _ANSI_ARGS_((
840 LiteralTable *tablePtr));
841 #ifdef TCL_COMPILE_STATS
842 EXTERN char * TclLiteralStats _ANSI_ARGS_((
843 LiteralTable *tablePtr));
844 EXTERN int TclLog2 _ANSI_ARGS_((int value));
846 #ifdef TCL_COMPILE_DEBUG
847 EXTERN void TclPrintByteCodeObj _ANSI_ARGS_((Tcl_Interp *interp,
850 EXTERN int TclPrintInstruction _ANSI_ARGS_((ByteCode* codePtr,
852 EXTERN void TclPrintObject _ANSI_ARGS_((FILE *outFile,
853 Tcl_Obj *objPtr, int maxChars));
854 EXTERN void TclPrintSource _ANSI_ARGS_((FILE *outFile,
855 CONST char *string, int maxChars));
856 EXTERN void TclRegisterAuxDataType _ANSI_ARGS_((AuxDataType *typePtr));
857 EXTERN int TclRegisterLiteral _ANSI_ARGS_((CompileEnv *envPtr,
858 char *bytes, int length, int onHeap));
859 EXTERN void TclReleaseLiteral _ANSI_ARGS_((Tcl_Interp *interp,
861 EXTERN void TclSetCmdNameObj _ANSI_ARGS_((Tcl_Interp *interp,
862 Tcl_Obj *objPtr, Command *cmdPtr));
863 #ifdef TCL_COMPILE_DEBUG
864 EXTERN void TclVerifyGlobalLiteralTable _ANSI_ARGS_((
866 EXTERN void TclVerifyLocalLiteralTable _ANSI_ARGS_((
867 CompileEnv *envPtr));
869 EXTERN int TclCompileVariableCmd _ANSI_ARGS_((
870 Tcl_Interp *interp, Tcl_Parse *parsePtr, CompileEnv *envPtr));
873 *----------------------------------------------------------------
874 * Macros used by Tcl bytecode compilation and execution modules
875 * inside the Tcl core but not used outside.
876 *----------------------------------------------------------------
880 * Form of TclRegisterLiteral with onHeap == 0.
881 * In that case, it is safe to cast away CONSTness, and it
882 * is cleanest to do that here, all in one place.
885 #define TclRegisterNewLiteral(envPtr, bytes, length) \
886 TclRegisterLiteral(envPtr, (char *)(bytes), length, /*onHeap*/ 0)
889 * Macro used to update the stack requirements.
890 * It is called by the macros TclEmitOpCode, TclEmitInst1 and
892 * Remark that the very last instruction of a bytecode always
893 * reduces the stack level: INST_DONE or INST_POP, so that the
894 * maxStackdepth is always updated.
897 #define TclUpdateStackReqs(op, i, envPtr) \
899 int delta = tclInstructionTable[(op)].stackEffect;\
902 if((envPtr)->maxStackDepth < (envPtr)->currStackDepth) {\
903 (envPtr)->maxStackDepth = (envPtr)->currStackDepth;\
905 if (delta == INT_MIN) {\
909 (envPtr)->currStackDepth += delta;\
914 * Macro to emit an opcode byte into a CompileEnv's code array.
915 * The ANSI C "prototype" for this macro is:
917 * EXTERN void TclEmitOpcode _ANSI_ARGS_((unsigned char op,
918 * CompileEnv *envPtr));
921 #define TclEmitOpcode(op, envPtr) \
922 if ((envPtr)->codeNext == (envPtr)->codeEnd) \
923 TclExpandCodeArray(envPtr); \
924 *(envPtr)->codeNext++ = (unsigned char) (op);\
925 TclUpdateStackReqs(op, 0, envPtr)
928 * Macro to emit an integer operand.
929 * The ANSI C "prototype" for this macro is:
931 * EXTERN void TclEmitInt1 _ANSI_ARGS_((int i, CompileEnv *envPtr));
934 #define TclEmitInt1(i, envPtr) \
935 if ((envPtr)->codeNext == (envPtr)->codeEnd) \
936 TclExpandCodeArray(envPtr); \
937 *(envPtr)->codeNext++ = (unsigned char) ((unsigned int) (i))
940 * Macros to emit an instruction with signed or unsigned integer operands.
941 * Four byte integers are stored in "big-endian" order with the high order
942 * byte stored at the lowest address.
943 * The ANSI C "prototypes" for these macros are:
945 * EXTERN void TclEmitInstInt1 _ANSI_ARGS_((unsigned char op, int i,
946 * CompileEnv *envPtr));
947 * EXTERN void TclEmitInstInt4 _ANSI_ARGS_((unsigned char op, int i,
948 * CompileEnv *envPtr));
952 #define TclEmitInstInt1(op, i, envPtr) \
953 if (((envPtr)->codeNext + 2) > (envPtr)->codeEnd) { \
954 TclExpandCodeArray(envPtr); \
956 *(envPtr)->codeNext++ = (unsigned char) (op); \
957 *(envPtr)->codeNext++ = (unsigned char) ((unsigned int) (i));\
958 TclUpdateStackReqs(op, i, envPtr)
960 #define TclEmitInstInt4(op, i, envPtr) \
961 if (((envPtr)->codeNext + 5) > (envPtr)->codeEnd) { \
962 TclExpandCodeArray(envPtr); \
964 *(envPtr)->codeNext++ = (unsigned char) (op); \
965 *(envPtr)->codeNext++ = \
966 (unsigned char) ((unsigned int) (i) >> 24); \
967 *(envPtr)->codeNext++ = \
968 (unsigned char) ((unsigned int) (i) >> 16); \
969 *(envPtr)->codeNext++ = \
970 (unsigned char) ((unsigned int) (i) >> 8); \
971 *(envPtr)->codeNext++ = \
972 (unsigned char) ((unsigned int) (i) );\
973 TclUpdateStackReqs(op, i, envPtr)
976 * Macro to push a Tcl object onto the Tcl evaluation stack. It emits the
977 * object's one or four byte array index into the CompileEnv's code
978 * array. These support, respectively, a maximum of 256 (2**8) and 2**32
979 * objects in a CompileEnv. The ANSI C "prototype" for this macro is:
981 * EXTERN void TclEmitPush _ANSI_ARGS_((int objIndex, CompileEnv *envPtr));
984 #define TclEmitPush(objIndex, envPtr) \
986 register int objIndexCopy = (objIndex);\
987 if (objIndexCopy <= 255) { \
988 TclEmitInstInt1(INST_PUSH1, objIndexCopy, (envPtr)); \
990 TclEmitInstInt4(INST_PUSH4, objIndexCopy, (envPtr)); \
995 * Macros to update a (signed or unsigned) integer starting at a pointer.
996 * The two variants depend on the number of bytes. The ANSI C "prototypes"
997 * for these macros are:
999 * EXTERN void TclStoreInt1AtPtr _ANSI_ARGS_((int i, unsigned char *p));
1000 * EXTERN void TclStoreInt4AtPtr _ANSI_ARGS_((int i, unsigned char *p));
1003 #define TclStoreInt1AtPtr(i, p) \
1004 *(p) = (unsigned char) ((unsigned int) (i))
1006 #define TclStoreInt4AtPtr(i, p) \
1007 *(p) = (unsigned char) ((unsigned int) (i) >> 24); \
1008 *(p+1) = (unsigned char) ((unsigned int) (i) >> 16); \
1009 *(p+2) = (unsigned char) ((unsigned int) (i) >> 8); \
1010 *(p+3) = (unsigned char) ((unsigned int) (i) )
1013 * Macros to update instructions at a particular pc with a new op code
1014 * and a (signed or unsigned) int operand. The ANSI C "prototypes" for
1017 * EXTERN void TclUpdateInstInt1AtPc _ANSI_ARGS_((unsigned char op, int i,
1018 * unsigned char *pc));
1019 * EXTERN void TclUpdateInstInt4AtPc _ANSI_ARGS_((unsigned char op, int i,
1020 * unsigned char *pc));
1023 #define TclUpdateInstInt1AtPc(op, i, pc) \
1024 *(pc) = (unsigned char) (op); \
1025 TclStoreInt1AtPtr((i), ((pc)+1))
1027 #define TclUpdateInstInt4AtPc(op, i, pc) \
1028 *(pc) = (unsigned char) (op); \
1029 TclStoreInt4AtPtr((i), ((pc)+1))
1032 * Macros to get a signed integer (GET_INT{1,2}) or an unsigned int
1033 * (GET_UINT{1,2}) from a pointer. There are two variants for each
1034 * return type that depend on the number of bytes fetched.
1035 * The ANSI C "prototypes" for these macros are:
1037 * EXTERN int TclGetInt1AtPtr _ANSI_ARGS_((unsigned char *p));
1038 * EXTERN int TclGetInt4AtPtr _ANSI_ARGS_((unsigned char *p));
1039 * EXTERN unsigned int TclGetUInt1AtPtr _ANSI_ARGS_((unsigned char *p));
1040 * EXTERN unsigned int TclGetUInt4AtPtr _ANSI_ARGS_((unsigned char *p));
1044 * The TclGetInt1AtPtr macro is tricky because we want to do sign
1045 * extension on the 1-byte value. Unfortunately the "char" type isn't
1046 * signed on all platforms so sign-extension doesn't always happen
1047 * automatically. Sometimes we can explicitly declare the pointer to be
1048 * signed, but other times we have to explicitly sign-extend the value
1052 #ifndef __CHAR_UNSIGNED__
1053 # define TclGetInt1AtPtr(p) ((int) *((char *) p))
1055 # ifdef HAVE_SIGNED_CHAR
1056 # define TclGetInt1AtPtr(p) ((int) *((signed char *) p))
1058 # define TclGetInt1AtPtr(p) (((int) *((char *) p)) \
1059 | ((*(p) & 0200) ? (-256) : 0))
1063 #define TclGetInt4AtPtr(p) (((int) TclGetInt1AtPtr(p) << 24) | \
1064 (*((p)+1) << 16) | \
1068 #define TclGetUInt1AtPtr(p) ((unsigned int) *(p))
1069 #define TclGetUInt4AtPtr(p) ((unsigned int) (*(p) << 24) | \
1070 (*((p)+1) << 16) | \
1075 * Macros used to compute the minimum and maximum of two integers.
1076 * The ANSI C "prototypes" for these macros are:
1078 * EXTERN int TclMin _ANSI_ARGS_((int i, int j));
1079 * EXTERN int TclMax _ANSI_ARGS_((int i, int j));
1082 #define TclMin(i, j) ((((int) i) < ((int) j))? (i) : (j))
1083 #define TclMax(i, j) ((((int) i) > ((int) j))? (i) : (j))
1085 # undef TCL_STORAGE_CLASS
1086 # define TCL_STORAGE_CLASS DLLIMPORT
1088 #endif /* _TCLCOMPILATION */