/[dtapublic]/projs/trunk/shared_source/tcl_base/tclcompile.h
ViewVC logotype

Annotation of /projs/trunk/shared_source/tcl_base/tclcompile.h

Parent Directory Parent Directory | Revision Log Revision Log


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

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25