tclcompile.h
来自「tcl是工具命令语言」· C头文件 代码 · 共 1,047 行 · 第 1/3 页
H
1,047 行
/* * tclCompile.h -- * * Copyright (c) 1996-1998 Sun Microsystems, Inc. * Copyright (c) 1998-2000 by Scriptics Corporation. * Copyright (c) 2001 by Kevin B. Kenny. All rights reserved. * * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * * RCS: @(#) $Id: tclCompile.h,v 1.33 2002/10/09 11:54:05 das Exp $ */#ifndef _TCLCOMPILATION#define _TCLCOMPILATION 1#ifndef _TCLINT#include "tclInt.h"#endif /* _TCLINT */#ifdef BUILD_tcl# undef TCL_STORAGE_CLASS# define TCL_STORAGE_CLASS DLLEXPORT#endif/* *------------------------------------------------------------------------ * Variables related to compilation. These are used in tclCompile.c, * tclExecute.c, tclBasic.c, and their clients. *------------------------------------------------------------------------ */#ifdef TCL_COMPILE_DEBUG/* * Variable that controls whether compilation tracing is enabled and, if so, * what level of tracing is desired: * 0: no compilation tracing * 1: summarize compilation of top level cmds and proc bodies * 2: display all instructions of each ByteCode compiled * This variable is linked to the Tcl variable "tcl_traceCompile". */extern int tclTraceCompile;#endif#ifdef TCL_COMPILE_DEBUG/* * Variable that controls whether execution tracing is enabled and, if so, * what level of tracing is desired: * 0: no execution tracing * 1: trace invocations of Tcl procs only * 2: trace invocations of all (not compiled away) commands * 3: display each instruction executed * This variable is linked to the Tcl variable "tcl_traceExec". */extern int tclTraceExec;#endif/* *------------------------------------------------------------------------ * Data structures related to compilation. *------------------------------------------------------------------------ *//* * The structure used to implement Tcl "exceptions" (exceptional returns): * for example, those generated in loops by the break and continue commands, * and those generated by scripts and caught by the catch command. This * ExceptionRange structure describes a range of code (e.g., a loop body), * the kind of exceptions (e.g., a break or continue) that might occur, and * the PC offsets to jump to if a matching exception does occur. Exception * ranges can nest so this structure includes a nesting level that is used * at runtime to find the closest exception range surrounding a PC. For * example, when a break command is executed, the ExceptionRange structure * for the most deeply nested loop, if any, is found and used. These * structures are also generated for the "next" subcommands of for loops * since a break there terminates the for command. This means a for command * actually generates two LoopInfo structures. */typedef enum { LOOP_EXCEPTION_RANGE, /* Exception's range is part of a loop. * Break and continue "exceptions" cause * jumps to appropriate PC offsets. */ CATCH_EXCEPTION_RANGE /* Exception's range is controlled by a * catch command. Errors in the range cause * a jump to a catch PC offset. */} ExceptionRangeType;typedef struct ExceptionRange { ExceptionRangeType type; /* The kind of ExceptionRange. */ int nestingLevel; /* Static depth of the exception range. * Used to find the most deeply-nested * range surrounding a PC at runtime. */ int codeOffset; /* Offset of the first instruction byte of * the code range. */ int numCodeBytes; /* Number of bytes in the code range. */ int breakOffset; /* If LOOP_EXCEPTION_RANGE, the target PC * offset for a break command in the range. */ int continueOffset; /* If LOOP_EXCEPTION_RANGE and not -1, the * target PC offset for a continue command in * the code range. Otherwise, ignore this range * when processing a continue command. */ int catchOffset; /* If a CATCH_EXCEPTION_RANGE, the target PC * offset for any "exception" in range. */} ExceptionRange;/* * Structure used to map between instruction pc and source locations. It * defines for each compiled Tcl command its code's starting offset and * its source's starting offset and length. Note that the code offset * increases monotonically: that is, the table is sorted in code offset * order. The source offset is not monotonic. */typedef struct CmdLocation { int codeOffset; /* Offset of first byte of command code. */ int numCodeBytes; /* Number of bytes for command's code. */ int srcOffset; /* Offset of first char of the command. */ int numSrcBytes; /* Number of command source chars. */} CmdLocation;/* * CompileProcs need the ability to record information during compilation * that can be used by bytecode instructions during execution. The AuxData * structure provides this "auxiliary data" mechanism. An arbitrary number * of these structures can be stored in the ByteCode record (during * compilation they are stored in a CompileEnv structure). Each AuxData * record holds one word of client-specified data (often a pointer) and is * given an index that instructions can later use to look up the structure * and its data. * * The following definitions declare the types of procedures that are called * to duplicate or free this auxiliary data when the containing ByteCode * objects are duplicated and freed. Pointers to these procedures are kept * in the AuxData structure. */typedef ClientData (AuxDataDupProc) _ANSI_ARGS_((ClientData clientData));typedef void (AuxDataFreeProc) _ANSI_ARGS_((ClientData clientData));/* * We define a separate AuxDataType struct to hold type-related information * for the AuxData structure. This separation makes it possible for clients * outside of the TCL core to manipulate (in a limited fashion!) AuxData; * for example, it makes it possible to pickle and unpickle AuxData structs. */typedef struct AuxDataType { char *name; /* the name of the type. Types can be * registered and found by name */ AuxDataDupProc *dupProc; /* Callback procedure to invoke when the * aux data is duplicated (e.g., when the * ByteCode structure containing the aux * data is duplicated). NULL means just * copy the source clientData bits; no * proc need be called. */ AuxDataFreeProc *freeProc; /* Callback procedure to invoke when the * aux data is freed. NULL means no * proc need be called. */} AuxDataType;/* * The definition of the AuxData structure that holds information created * during compilation by CompileProcs and used by instructions during * execution. */typedef struct AuxData { AuxDataType *type; /* pointer to the AuxData type associated with * this ClientData. */ ClientData clientData; /* The compilation data itself. */} AuxData;/* * Structure defining the compilation environment. After compilation, fields * describing bytecode instructions are copied out into the more compact * ByteCode structure defined below. */#define COMPILEENV_INIT_CODE_BYTES 250#define COMPILEENV_INIT_NUM_OBJECTS 60#define COMPILEENV_INIT_EXCEPT_RANGES 5#define COMPILEENV_INIT_CMD_MAP_SIZE 40#define COMPILEENV_INIT_AUX_DATA_SIZE 5typedef struct CompileEnv { Interp *iPtr; /* Interpreter containing the code being * compiled. Commands and their compile * procs are specific to an interpreter so * the code emitted will depend on the * interpreter. */ char *source; /* The source string being compiled by * SetByteCodeFromAny. This pointer is not * owned by the CompileEnv and must not be * freed or changed by it. */ int numSrcBytes; /* Number of bytes in source. */ Proc *procPtr; /* If a procedure is being compiled, a * pointer to its Proc structure; otherwise * NULL. Used to compile local variables. * Set from information provided by * ObjInterpProc in tclProc.c. */ int numCommands; /* Number of commands compiled. */ int exceptDepth; /* Current exception range nesting level; * -1 if not in any range currently. */ int maxExceptDepth; /* Max nesting level of exception ranges; * -1 if no ranges have been compiled. */ int maxStackDepth; /* Maximum number of stack elements needed * to execute the code. Set by compilation * procedures before returning. */ int currStackDepth; /* Current stack depth. */ LiteralTable localLitTable; /* Contains LiteralEntry's describing * all Tcl objects referenced by this * compiled code. Indexed by the string * representations of the literals. Used to * avoid creating duplicate objects. */ unsigned char *codeStart; /* Points to the first byte of the code. */ unsigned char *codeNext; /* Points to next code array byte to use. */ unsigned char *codeEnd; /* Points just after the last allocated * code array byte. */ int mallocedCodeArray; /* Set 1 if code array was expanded * and codeStart points into the heap.*/ LiteralEntry *literalArrayPtr; /* Points to start of LiteralEntry array. */ int literalArrayNext; /* Index of next free object array entry. */ int literalArrayEnd; /* Index just after last obj array entry. */ int mallocedLiteralArray; /* 1 if object array was expanded and * objArray points into the heap, else 0. */ ExceptionRange *exceptArrayPtr; /* Points to start of the ExceptionRange * array. */ int exceptArrayNext; /* Next free ExceptionRange array index. * exceptArrayNext is the number of ranges * and (exceptArrayNext-1) is the index of * the current range's array entry. */ int exceptArrayEnd; /* Index after the last ExceptionRange * array entry. */ int mallocedExceptArray; /* 1 if ExceptionRange array was expanded * and exceptArrayPtr points in heap, * else 0. */ CmdLocation *cmdMapPtr; /* Points to start of CmdLocation array. * numCommands is the index of the next * entry to use; (numCommands-1) is the * entry index for the last command. */ int cmdMapEnd; /* Index after last CmdLocation entry. */ int mallocedCmdMap; /* 1 if command map array was expanded and * cmdMapPtr points in the heap, else 0. */ AuxData *auxDataArrayPtr; /* Points to auxiliary data array start. */ int auxDataArrayNext; /* Next free compile aux data array index. * auxDataArrayNext is the number of aux * data items and (auxDataArrayNext-1) is * index of current aux data array entry. */ int auxDataArrayEnd; /* Index after last aux data array entry. */ int mallocedAuxDataArray; /* 1 if aux data array was expanded and * auxDataArrayPtr points in heap else 0. */ unsigned char staticCodeSpace[COMPILEENV_INIT_CODE_BYTES]; /* Initial storage for code. */ LiteralEntry staticLiteralSpace[COMPILEENV_INIT_NUM_OBJECTS]; /* Initial storage of LiteralEntry array. */ ExceptionRange staticExceptArraySpace[COMPILEENV_INIT_EXCEPT_RANGES]; /* Initial ExceptionRange array storage. */ CmdLocation staticCmdMapSpace[COMPILEENV_INIT_CMD_MAP_SIZE]; /* Initial storage for cmd location map. */ AuxData staticAuxDataArraySpace[COMPILEENV_INIT_AUX_DATA_SIZE]; /* Initial storage for aux data array. */} CompileEnv;/* * The structure defining the bytecode instructions resulting from compiling * a Tcl script. Note that this structure is variable length: a single heap * object is allocated to hold the ByteCode structure immediately followed * by the code bytes, the literal object array, the ExceptionRange array, * the CmdLocation map, and the compilation AuxData array. *//* * A PRECOMPILED bytecode struct is one that was generated from a compiled * image rather than implicitly compiled from source */#define TCL_BYTECODE_PRECOMPILED 0x0001typedef struct ByteCode { TclHandle interpHandle; /* Handle for interpreter containing the * compiled code. Commands and their compile * procs are specific to an interpreter so the * code emitted will depend on the * interpreter. */ int compileEpoch; /* Value of iPtr->compileEpoch when this * ByteCode was compiled. Used to invalidate * code when, e.g., commands with compile * procs are redefined. */ Namespace *nsPtr; /* Namespace context in which this code * was compiled. If the code is executed * if a different namespace, it must be * recompiled. */ int nsEpoch; /* Value of nsPtr->resolverEpoch when this * ByteCode was compiled. Used to invalidate * code when new namespace resolution rules * are put into effect. */ int refCount; /* Reference count: set 1 when created * plus 1 for each execution of the code * currently active. This structure can be * freed when refCount becomes zero. */ unsigned int flags; /* flags describing state for the codebyte. * this variable holds ORed values from the * TCL_BYTECODE_ masks defined above */ char *source; /* The source string from which this * ByteCode was compiled. Note that this * pointer is not owned by the ByteCode and * must not be freed or modified by it. */ Proc *procPtr; /* If the ByteCode was compiled from a * procedure body, this is a pointer to its * Proc structure; otherwise NULL. This * pointer is also not owned by the ByteCode * and must not be freed by it. */ size_t structureSize; /* Number of bytes in the ByteCode structure * itself. Does not include heap space for * literal Tcl objects or storage referenced * by AuxData entries. */ int numCommands; /* Number of commands compiled. */ int numSrcBytes; /* Number of source bytes compiled. */ int numCodeBytes; /* Number of code bytes. */ int numLitObjects; /* Number of objects in literal array. */ int numExceptRanges; /* Number of ExceptionRange array elems. */ int numAuxDataItems; /* Number of AuxData items. */ int numCmdLocBytes; /* Number of bytes needed for encoded * command location information. */ int maxExceptDepth; /* Maximum nesting level of ExceptionRanges; * -1 if no ranges were compiled. */ int maxStackDepth; /* Maximum number of stack elements needed * to execute the code. */ unsigned char *codeStart; /* Points to the first byte of the code. * This is just after the final ByteCode * member cmdMapPtr. */ Tcl_Obj **objArrayPtr; /* Points to the start of the literal * object array. This is just after the * last code byte. */ ExceptionRange *exceptArrayPtr; /* Points to the start of the ExceptionRange * array. This is just after the last * object in the object array. */ AuxData *auxDataArrayPtr; /* Points to the start of the auxiliary data * array. This is just after the last entry * in the ExceptionRange array. */ unsigned char *codeDeltaStart; /* Points to the first of a sequence of * bytes that encode the change in the * starting offset of each command's code.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?