Fork me on GitHub

source: git/external/tcl/tclCompile.h@ 5a697dde

ImprovedOutputFile Timing dual_readout llp
Last change on this file since 5a697dde was d7d2da3, checked in by pavel <pavel@…>, 12 years ago

move branches/ModularDelphes to trunk

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