Fork me on GitHub

source: git/external/tcl/tclCompile.h@ 537d24f

Last change on this file since 537d24f was d4f8a8c, checked in by Pavel Demin <pavel-demin@…>, 5 years ago

add TclPrintSource to tclCompile.h

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