tclCompile.h 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932
  1. /*
  2. * tclCompile.h --
  3. *
  4. * Copyright (c) 1996-1998 Sun Microsystems, Inc.
  5. * Copyright (c) 1998-2000 by Scriptics Corporation.
  6. * Copyright (c) 2001 by Kevin B. Kenny. All rights reserved.
  7. * Copyright (c) 2007 Daniel A. Steffen <das@users.sourceforge.net>
  8. *
  9. * See the file "license.terms" for information on usage and redistribution of
  10. * this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11. */
  12. #ifndef _TCLCOMPILATION
  13. #define _TCLCOMPILATION 1
  14. #include "tclInt.h"
  15. struct ByteCode; /* Forward declaration. */
  16. /*
  17. *------------------------------------------------------------------------
  18. * Variables related to compilation. These are used in tclCompile.c,
  19. * tclExecute.c, tclBasic.c, and their clients.
  20. *------------------------------------------------------------------------
  21. */
  22. #ifdef TCL_COMPILE_DEBUG
  23. /*
  24. * Variable that controls whether compilation tracing is enabled and, if so,
  25. * what level of tracing is desired:
  26. * 0: no compilation tracing
  27. * 1: summarize compilation of top level cmds and proc bodies
  28. * 2: display all instructions of each ByteCode compiled
  29. * This variable is linked to the Tcl variable "tcl_traceCompile".
  30. */
  31. MODULE_SCOPE int tclTraceCompile;
  32. /*
  33. * Variable that controls whether execution tracing is enabled and, if so,
  34. * what level of tracing is desired:
  35. * 0: no execution tracing
  36. * 1: trace invocations of Tcl procs only
  37. * 2: trace invocations of all (not compiled away) commands
  38. * 3: display each instruction executed
  39. * This variable is linked to the Tcl variable "tcl_traceExec".
  40. */
  41. MODULE_SCOPE int tclTraceExec;
  42. #endif
  43. /*
  44. * The type of lambda expressions. Note that every lambda will *always* have a
  45. * string representation.
  46. */
  47. MODULE_SCOPE const Tcl_ObjType tclLambdaType;
  48. /*
  49. *------------------------------------------------------------------------
  50. * Data structures related to compilation.
  51. *------------------------------------------------------------------------
  52. */
  53. /*
  54. * The structure used to implement Tcl "exceptions" (exceptional returns): for
  55. * example, those generated in loops by the break and continue commands, and
  56. * those generated by scripts and caught by the catch command. This
  57. * ExceptionRange structure describes a range of code (e.g., a loop body), the
  58. * kind of exceptions (e.g., a break or continue) that might occur, and the PC
  59. * offsets to jump to if a matching exception does occur. Exception ranges can
  60. * nest so this structure includes a nesting level that is used at runtime to
  61. * find the closest exception range surrounding a PC. For example, when a
  62. * break command is executed, the ExceptionRange structure for the most deeply
  63. * nested loop, if any, is found and used. These structures are also generated
  64. * for the "next" subcommands of for loops since a break there terminates the
  65. * for command. This means a for command actually generates two LoopInfo
  66. * structures.
  67. */
  68. typedef enum {
  69. LOOP_EXCEPTION_RANGE, /* Exception's range is part of a loop. Break
  70. * and continue "exceptions" cause jumps to
  71. * appropriate PC offsets. */
  72. CATCH_EXCEPTION_RANGE /* Exception's range is controlled by a catch
  73. * command. Errors in the range cause a jump
  74. * to a catch PC offset. */
  75. } ExceptionRangeType;
  76. typedef struct ExceptionRange {
  77. ExceptionRangeType type; /* The kind of ExceptionRange. */
  78. int nestingLevel; /* Static depth of the exception range. Used
  79. * to find the most deeply-nested range
  80. * surrounding a PC at runtime. */
  81. int codeOffset; /* Offset of the first instruction byte of the
  82. * code range. */
  83. int numCodeBytes; /* Number of bytes in the code range. */
  84. int breakOffset; /* If LOOP_EXCEPTION_RANGE, the target PC
  85. * offset for a break command in the range. */
  86. int continueOffset; /* If LOOP_EXCEPTION_RANGE and not -1, the
  87. * target PC offset for a continue command in
  88. * the code range. Otherwise, ignore this
  89. * range when processing a continue
  90. * command. */
  91. int catchOffset; /* If a CATCH_EXCEPTION_RANGE, the target PC
  92. * offset for any "exception" in range. */
  93. } ExceptionRange;
  94. /*
  95. * Auxiliary data used when issuing (currently just loop) exception ranges,
  96. * but which is not required during execution.
  97. */
  98. typedef struct ExceptionAux {
  99. int supportsContinue; /* Whether this exception range will have a
  100. * continueOffset created for it; if it is a
  101. * loop exception range that *doesn't* have
  102. * one (see [for] next-clause) then we must
  103. * not pick up the range when scanning for a
  104. * target to continue to. */
  105. int stackDepth; /* The stack depth at the point where the
  106. * exception range was created. This is used
  107. * to calculate the number of POPs required to
  108. * restore the stack to its prior state. */
  109. int expandTarget; /* The number of expansions expected on the
  110. * auxData stack at the time the loop starts;
  111. * we can't currently discard them except by
  112. * doing INST_INVOKE_EXPANDED; this is a known
  113. * problem. */
  114. int expandTargetDepth; /* The stack depth expected at the outermost
  115. * expansion within the loop. Not meaningful
  116. * if there are no open expansions between the
  117. * looping level and the point of jump
  118. * issue. */
  119. int numBreakTargets; /* The number of [break]s that want to be
  120. * targeted to the place where this loop
  121. * exception will be bound to. */
  122. unsigned int *breakTargets; /* The offsets of the INST_JUMP4 instructions
  123. * issued by the [break]s that we must
  124. * update. Note that resizing a jump (via
  125. * TclFixupForwardJump) can cause the contents
  126. * of this array to be updated. When
  127. * numBreakTargets==0, this is NULL. */
  128. int allocBreakTargets; /* The size of the breakTargets array. */
  129. int numContinueTargets; /* The number of [continue]s that want to be
  130. * targeted to the place where this loop
  131. * exception will be bound to. */
  132. unsigned int *continueTargets; /* The offsets of the INST_JUMP4 instructions
  133. * issued by the [continue]s that we must
  134. * update. Note that resizing a jump (via
  135. * TclFixupForwardJump) can cause the contents
  136. * of this array to be updated. When
  137. * numContinueTargets==0, this is NULL. */
  138. int allocContinueTargets; /* The size of the continueTargets array. */
  139. } ExceptionAux;
  140. /*
  141. * Structure used to map between instruction pc and source locations. It
  142. * defines for each compiled Tcl command its code's starting offset and its
  143. * source's starting offset and length. Note that the code offset increases
  144. * monotonically: that is, the table is sorted in code offset order. The
  145. * source offset is not monotonic.
  146. */
  147. typedef 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 numSrcBytes; /* Number of command source chars. */
  152. } CmdLocation;
  153. /*
  154. * TIP #280
  155. * Structure to record additional location information for byte code. This
  156. * information is internal and not saved. i.e. tbcload'ed code will not have
  157. * this information. It records the lines for all words of all commands found
  158. * in the byte code. The association with a ByteCode structure BC is done
  159. * through the 'lineBCPtr' HashTable in Interp, keyed by the address of BC.
  160. * Also recorded is information coming from the context, i.e. type of the
  161. * frame and associated information, like the path of a sourced file.
  162. */
  163. typedef struct ECL {
  164. int srcOffset; /* Command location to find the entry. */
  165. int nline; /* Number of words in the command */
  166. int *line; /* Line information for all words in the
  167. * command. */
  168. int **next; /* Transient information used by the compiler
  169. * for tracking of hidden continuation
  170. * lines. */
  171. } ECL;
  172. typedef struct ExtCmdLoc {
  173. int type; /* Context type. */
  174. int start; /* Starting line for compiled script. Needed
  175. * for the extended recompile check in
  176. * tclCompileObj. */
  177. Tcl_Obj *path; /* Path of the sourced file the command is
  178. * in. */
  179. ECL *loc; /* Command word locations (lines). */
  180. int nloc; /* Number of allocated entries in 'loc'. */
  181. int nuloc; /* Number of used entries in 'loc'. */
  182. } ExtCmdLoc;
  183. /*
  184. * CompileProcs need the ability to record information during compilation that
  185. * can be used by bytecode instructions during execution. The AuxData
  186. * structure provides this "auxiliary data" mechanism. An arbitrary number of
  187. * these structures can be stored in the ByteCode record (during compilation
  188. * they are stored in a CompileEnv structure). Each AuxData record holds one
  189. * word of client-specified data (often a pointer) and is given an index that
  190. * instructions can later use to look up the structure and its data.
  191. *
  192. * The following definitions declare the types of procedures that are called
  193. * to duplicate or free this auxiliary data when the containing ByteCode
  194. * objects are duplicated and freed. Pointers to these procedures are kept in
  195. * the AuxData structure.
  196. */
  197. typedef ClientData (AuxDataDupProc) (ClientData clientData);
  198. typedef void (AuxDataFreeProc) (ClientData clientData);
  199. typedef void (AuxDataPrintProc)(ClientData clientData,
  200. Tcl_Obj *appendObj, struct ByteCode *codePtr,
  201. unsigned int pcOffset);
  202. /*
  203. * We define a separate AuxDataType struct to hold type-related information
  204. * for the AuxData structure. This separation makes it possible for clients
  205. * outside of the TCL core to manipulate (in a limited fashion!) AuxData; for
  206. * example, it makes it possible to pickle and unpickle AuxData structs.
  207. */
  208. typedef struct AuxDataType {
  209. const char *name; /* The name of the type. Types can be
  210. * registered and found by name */
  211. AuxDataDupProc *dupProc; /* Callback procedure to invoke when the aux
  212. * data is duplicated (e.g., when the ByteCode
  213. * structure containing the aux data is
  214. * duplicated). NULL means just copy the
  215. * source clientData bits; no proc need be
  216. * called. */
  217. AuxDataFreeProc *freeProc; /* Callback procedure to invoke when the aux
  218. * data is freed. NULL means no proc need be
  219. * called. */
  220. AuxDataPrintProc *printProc;/* Callback function to invoke when printing
  221. * the aux data as part of debugging. NULL
  222. * means that the data can't be printed. */
  223. AuxDataPrintProc *disassembleProc;
  224. /* Callback function to invoke when doing a
  225. * disassembly of the aux data (like the
  226. * printProc, except that the output is
  227. * intended to be script-readable). The
  228. * appendObj argument should be filled in with
  229. * a descriptive dictionary; it will start out
  230. * with "name" mapped to the content of the
  231. * name field. NULL means that the printProc
  232. * should be used instead. */
  233. } AuxDataType;
  234. /*
  235. * The definition of the AuxData structure that holds information created
  236. * during compilation by CompileProcs and used by instructions during
  237. * execution.
  238. */
  239. typedef struct AuxData {
  240. const AuxDataType *type; /* Pointer to the AuxData type associated with
  241. * this ClientData. */
  242. ClientData clientData; /* The compilation data itself. */
  243. } AuxData;
  244. /*
  245. * Structure defining the compilation environment. After compilation, fields
  246. * describing bytecode instructions are copied out into the more compact
  247. * ByteCode structure defined below.
  248. */
  249. #define COMPILEENV_INIT_CODE_BYTES 250
  250. #define COMPILEENV_INIT_NUM_OBJECTS 60
  251. #define COMPILEENV_INIT_EXCEPT_RANGES 5
  252. #define COMPILEENV_INIT_CMD_MAP_SIZE 40
  253. #define COMPILEENV_INIT_AUX_DATA_SIZE 5
  254. typedef struct CompileEnv {
  255. Interp *iPtr; /* Interpreter containing the code being
  256. * compiled. Commands and their compile procs
  257. * are specific to an interpreter so the code
  258. * emitted will depend on the interpreter. */
  259. const char *source; /* The source string being compiled by
  260. * SetByteCodeFromAny. This pointer is not
  261. * owned by the CompileEnv and must not be
  262. * freed or changed by it. */
  263. int numSrcBytes; /* Number of bytes in source. */
  264. Proc *procPtr; /* If a procedure is being compiled, a pointer
  265. * to its Proc structure; otherwise NULL. Used
  266. * to compile local variables. Set from
  267. * information provided by ObjInterpProc in
  268. * tclProc.c. */
  269. int numCommands; /* Number of commands compiled. */
  270. int exceptDepth; /* Current exception range nesting level; -1
  271. * if not in any range currently. */
  272. int maxExceptDepth; /* Max nesting level of exception ranges; -1
  273. * if no ranges have been compiled. */
  274. int maxStackDepth; /* Maximum number of stack elements needed to
  275. * execute the code. Set by compilation
  276. * procedures before returning. */
  277. int currStackDepth; /* Current stack depth. */
  278. LiteralTable localLitTable; /* Contains LiteralEntry's describing all Tcl
  279. * objects referenced by this compiled code.
  280. * Indexed by the string representations of
  281. * the literals. Used to avoid creating
  282. * duplicate objects. */
  283. unsigned char *codeStart; /* Points to the first byte of the code. */
  284. unsigned char *codeNext; /* Points to next code array byte to use. */
  285. unsigned char *codeEnd; /* Points just after the last allocated code
  286. * array byte. */
  287. int mallocedCodeArray; /* Set 1 if code array was expanded and
  288. * codeStart points into the heap.*/
  289. LiteralEntry *literalArrayPtr;
  290. /* Points to start of LiteralEntry array. */
  291. int literalArrayNext; /* Index of next free object array entry. */
  292. int literalArrayEnd; /* Index just after last obj array entry. */
  293. int mallocedLiteralArray; /* 1 if object array was expanded and objArray
  294. * points into the heap, else 0. */
  295. ExceptionRange *exceptArrayPtr;
  296. /* Points to start of the ExceptionRange
  297. * array. */
  298. int exceptArrayNext; /* Next free ExceptionRange array index.
  299. * exceptArrayNext is the number of ranges and
  300. * (exceptArrayNext-1) is the index of the
  301. * current range's array entry. */
  302. int exceptArrayEnd; /* Index after the last ExceptionRange array
  303. * entry. */
  304. int mallocedExceptArray; /* 1 if ExceptionRange array was expanded and
  305. * exceptArrayPtr points in heap, else 0. */
  306. ExceptionAux *exceptAuxArrayPtr;
  307. /* Array of information used to restore the
  308. * state when processing BREAK/CONTINUE
  309. * exceptions. Must be the same size as the
  310. * exceptArrayPtr. */
  311. CmdLocation *cmdMapPtr; /* Points to start of CmdLocation array.
  312. * numCommands is the index of the next entry
  313. * to use; (numCommands-1) is the entry index
  314. * for the last command. */
  315. int cmdMapEnd; /* Index after last CmdLocation entry. */
  316. int mallocedCmdMap; /* 1 if command map array was expanded and
  317. * cmdMapPtr points in the heap, else 0. */
  318. AuxData *auxDataArrayPtr; /* Points to auxiliary data array start. */
  319. int auxDataArrayNext; /* Next free compile aux data array index.
  320. * auxDataArrayNext is the number of aux data
  321. * items and (auxDataArrayNext-1) is index of
  322. * current aux data array entry. */
  323. int auxDataArrayEnd; /* Index after last aux data array entry. */
  324. int mallocedAuxDataArray; /* 1 if aux data array was expanded and
  325. * auxDataArrayPtr points in heap else 0. */
  326. unsigned char staticCodeSpace[COMPILEENV_INIT_CODE_BYTES];
  327. /* Initial storage for code. */
  328. LiteralEntry staticLiteralSpace[COMPILEENV_INIT_NUM_OBJECTS];
  329. /* Initial storage of LiteralEntry array. */
  330. ExceptionRange staticExceptArraySpace[COMPILEENV_INIT_EXCEPT_RANGES];
  331. /* Initial ExceptionRange array storage. */
  332. ExceptionAux staticExAuxArraySpace[COMPILEENV_INIT_EXCEPT_RANGES];
  333. /* Initial static except auxiliary info array
  334. * storage. */
  335. CmdLocation staticCmdMapSpace[COMPILEENV_INIT_CMD_MAP_SIZE];
  336. /* Initial storage for cmd location map. */
  337. AuxData staticAuxDataArraySpace[COMPILEENV_INIT_AUX_DATA_SIZE];
  338. /* Initial storage for aux data array. */
  339. /* TIP #280 */
  340. ExtCmdLoc *extCmdMapPtr; /* Extended command location information for
  341. * 'info frame'. */
  342. int line; /* First line of the script, based on the
  343. * invoking context, then the line of the
  344. * command currently compiled. */
  345. int atCmdStart; /* Flag to say whether an INST_START_CMD
  346. * should be issued; they should never be
  347. * issued repeatedly, as that is significantly
  348. * inefficient. If set to 2, that instruction
  349. * should not be issued at all (by the generic
  350. * part of the command compiler). */
  351. int expandCount; /* Number of INST_EXPAND_START instructions
  352. * encountered that have not yet been paired
  353. * with a corresponding
  354. * INST_INVOKE_EXPANDED. */
  355. int *clNext; /* If not NULL, it refers to the next slot in
  356. * clLoc to check for an invisible
  357. * continuation line. */
  358. } CompileEnv;
  359. /*
  360. * The structure defining the bytecode instructions resulting from compiling a
  361. * Tcl script. Note that this structure is variable length: a single heap
  362. * object is allocated to hold the ByteCode structure immediately followed by
  363. * the code bytes, the literal object array, the ExceptionRange array, the
  364. * CmdLocation map, and the compilation AuxData array.
  365. */
  366. /*
  367. * A PRECOMPILED bytecode struct is one that was generated from a compiled
  368. * image rather than implicitly compiled from source
  369. */
  370. #define TCL_BYTECODE_PRECOMPILED 0x0001
  371. /*
  372. * When a bytecode is compiled, interp or namespace resolvers have not been
  373. * applied yet: this is indicated by the TCL_BYTECODE_RESOLVE_VARS flag.
  374. */
  375. #define TCL_BYTECODE_RESOLVE_VARS 0x0002
  376. #define TCL_BYTECODE_RECOMPILE 0x0004
  377. typedef struct ByteCode {
  378. TclHandle interpHandle; /* Handle for interpreter containing the
  379. * compiled code. Commands and their compile
  380. * procs are specific to an interpreter so the
  381. * code emitted will depend on the
  382. * interpreter. */
  383. int compileEpoch; /* Value of iPtr->compileEpoch when this
  384. * ByteCode was compiled. Used to invalidate
  385. * code when, e.g., commands with compile
  386. * procs are redefined. */
  387. Namespace *nsPtr; /* Namespace context in which this code was
  388. * compiled. If the code is executed if a
  389. * different namespace, it must be
  390. * recompiled. */
  391. int nsEpoch; /* Value of nsPtr->resolverEpoch when this
  392. * ByteCode was compiled. Used to invalidate
  393. * code when new namespace resolution rules
  394. * are put into effect. */
  395. int refCount; /* Reference count: set 1 when created plus 1
  396. * for each execution of the code currently
  397. * active. This structure can be freed when
  398. * refCount becomes zero. */
  399. unsigned int flags; /* flags describing state for the codebyte.
  400. * this variable holds ORed values from the
  401. * TCL_BYTECODE_ masks defined above */
  402. const char *source; /* The source string from which this ByteCode
  403. * was compiled. Note that this pointer is not
  404. * owned by the ByteCode and must not be freed
  405. * or modified by it. */
  406. Proc *procPtr; /* If the ByteCode was compiled from a
  407. * procedure body, this is a pointer to its
  408. * Proc structure; otherwise NULL. This
  409. * pointer is also not owned by the ByteCode
  410. * and must not be freed by it. */
  411. size_t structureSize; /* Number of bytes in the ByteCode structure
  412. * itself. Does not include heap space for
  413. * literal Tcl objects or storage referenced
  414. * by AuxData entries. */
  415. int numCommands; /* Number of commands compiled. */
  416. int numSrcBytes; /* Number of source bytes compiled. */
  417. int numCodeBytes; /* Number of code bytes. */
  418. int numLitObjects; /* Number of objects in literal array. */
  419. int numExceptRanges; /* Number of ExceptionRange array elems. */
  420. int numAuxDataItems; /* Number of AuxData items. */
  421. int numCmdLocBytes; /* Number of bytes needed for encoded command
  422. * location information. */
  423. int maxExceptDepth; /* Maximum nesting level of ExceptionRanges;
  424. * -1 if no ranges were compiled. */
  425. int maxStackDepth; /* Maximum number of stack elements needed to
  426. * execute the code. */
  427. unsigned char *codeStart; /* Points to the first byte of the code. This
  428. * is just after the final ByteCode member
  429. * cmdMapPtr. */
  430. Tcl_Obj **objArrayPtr; /* Points to the start of the literal object
  431. * array. This is just after the last code
  432. * byte. */
  433. ExceptionRange *exceptArrayPtr;
  434. /* Points to the start of the ExceptionRange
  435. * array. This is just after the last object
  436. * in the object array. */
  437. AuxData *auxDataArrayPtr; /* Points to the start of the auxiliary data
  438. * array. This is just after the last entry in
  439. * the ExceptionRange array. */
  440. unsigned char *codeDeltaStart;
  441. /* Points to the first of a sequence of bytes
  442. * that encode the change in the starting
  443. * offset of each command's code. If -127 <=
  444. * delta <= 127, it is encoded as 1 byte,
  445. * otherwise 0xFF (128) appears and the delta
  446. * is encoded by the next 4 bytes. Code deltas
  447. * are always positive. This sequence is just
  448. * after the last entry in the AuxData
  449. * array. */
  450. unsigned char *codeLengthStart;
  451. /* Points to the first of a sequence of bytes
  452. * that encode the length of each command's
  453. * code. The encoding is the same as for code
  454. * deltas. Code lengths are always positive.
  455. * This sequence is just after the last entry
  456. * in the code delta sequence. */
  457. unsigned char *srcDeltaStart;
  458. /* Points to the first of a sequence of bytes
  459. * that encode the change in the starting
  460. * offset of each command's source. The
  461. * encoding is the same as for code deltas.
  462. * Source deltas can be negative. This
  463. * sequence is just after the last byte in the
  464. * code length sequence. */
  465. unsigned char *srcLengthStart;
  466. /* Points to the first of a sequence of bytes
  467. * that encode the length of each command's
  468. * source. The encoding is the same as for
  469. * code deltas. Source lengths are always
  470. * positive. This sequence is just after the
  471. * last byte in the source delta sequence. */
  472. LocalCache *localCachePtr; /* Pointer to the start of the cached variable
  473. * names and initialisation data for local
  474. * variables. */
  475. #ifdef TCL_COMPILE_STATS
  476. Tcl_Time createTime; /* Absolute time when the ByteCode was
  477. * created. */
  478. #endif /* TCL_COMPILE_STATS */
  479. } ByteCode;
  480. /*
  481. * Opcodes for the Tcl bytecode instructions. These must correspond to the
  482. * entries in the table of instruction descriptions, tclInstructionTable, in
  483. * tclCompile.c. Also, the order and number of the expression opcodes (e.g.,
  484. * INST_LOR) must match the entries in the array operatorStrings in
  485. * tclExecute.c.
  486. */
  487. /* Opcodes 0 to 9 */
  488. #define INST_DONE 0
  489. #define INST_PUSH1 1
  490. #define INST_PUSH4 2
  491. #define INST_POP 3
  492. #define INST_DUP 4
  493. #define INST_STR_CONCAT1 5
  494. #define INST_INVOKE_STK1 6
  495. #define INST_INVOKE_STK4 7
  496. #define INST_EVAL_STK 8
  497. #define INST_EXPR_STK 9
  498. /* Opcodes 10 to 23 */
  499. #define INST_LOAD_SCALAR1 10
  500. #define INST_LOAD_SCALAR4 11
  501. #define INST_LOAD_SCALAR_STK 12
  502. #define INST_LOAD_ARRAY1 13
  503. #define INST_LOAD_ARRAY4 14
  504. #define INST_LOAD_ARRAY_STK 15
  505. #define INST_LOAD_STK 16
  506. #define INST_STORE_SCALAR1 17
  507. #define INST_STORE_SCALAR4 18
  508. #define INST_STORE_SCALAR_STK 19
  509. #define INST_STORE_ARRAY1 20
  510. #define INST_STORE_ARRAY4 21
  511. #define INST_STORE_ARRAY_STK 22
  512. #define INST_STORE_STK 23
  513. /* Opcodes 24 to 33 */
  514. #define INST_INCR_SCALAR1 24
  515. #define INST_INCR_SCALAR_STK 25
  516. #define INST_INCR_ARRAY1 26
  517. #define INST_INCR_ARRAY_STK 27
  518. #define INST_INCR_STK 28
  519. #define INST_INCR_SCALAR1_IMM 29
  520. #define INST_INCR_SCALAR_STK_IMM 30
  521. #define INST_INCR_ARRAY1_IMM 31
  522. #define INST_INCR_ARRAY_STK_IMM 32
  523. #define INST_INCR_STK_IMM 33
  524. /* Opcodes 34 to 39 */
  525. #define INST_JUMP1 34
  526. #define INST_JUMP4 35
  527. #define INST_JUMP_TRUE1 36
  528. #define INST_JUMP_TRUE4 37
  529. #define INST_JUMP_FALSE1 38
  530. #define INST_JUMP_FALSE4 39
  531. /* Opcodes 40 to 64 */
  532. #define INST_LOR 40
  533. #define INST_LAND 41
  534. #define INST_BITOR 42
  535. #define INST_BITXOR 43
  536. #define INST_BITAND 44
  537. #define INST_EQ 45
  538. #define INST_NEQ 46
  539. #define INST_LT 47
  540. #define INST_GT 48
  541. #define INST_LE 49
  542. #define INST_GE 50
  543. #define INST_LSHIFT 51
  544. #define INST_RSHIFT 52
  545. #define INST_ADD 53
  546. #define INST_SUB 54
  547. #define INST_MULT 55
  548. #define INST_DIV 56
  549. #define INST_MOD 57
  550. #define INST_UPLUS 58
  551. #define INST_UMINUS 59
  552. #define INST_BITNOT 60
  553. #define INST_LNOT 61
  554. #define INST_CALL_BUILTIN_FUNC1 62
  555. #define INST_CALL_FUNC1 63
  556. #define INST_TRY_CVT_TO_NUMERIC 64
  557. /* Opcodes 65 to 66 */
  558. #define INST_BREAK 65
  559. #define INST_CONTINUE 66
  560. /* Opcodes 67 to 68 */
  561. #define INST_FOREACH_START4 67 /* DEPRECATED */
  562. #define INST_FOREACH_STEP4 68 /* DEPRECATED */
  563. /* Opcodes 69 to 72 */
  564. #define INST_BEGIN_CATCH4 69
  565. #define INST_END_CATCH 70
  566. #define INST_PUSH_RESULT 71
  567. #define INST_PUSH_RETURN_CODE 72
  568. /* Opcodes 73 to 78 */
  569. #define INST_STR_EQ 73
  570. #define INST_STR_NEQ 74
  571. #define INST_STR_CMP 75
  572. #define INST_STR_LEN 76
  573. #define INST_STR_INDEX 77
  574. #define INST_STR_MATCH 78
  575. /* Opcodes 78 to 81 */
  576. #define INST_LIST 79
  577. #define INST_LIST_INDEX 80
  578. #define INST_LIST_LENGTH 81
  579. /* Opcodes 82 to 87 */
  580. #define INST_APPEND_SCALAR1 82
  581. #define INST_APPEND_SCALAR4 83
  582. #define INST_APPEND_ARRAY1 84
  583. #define INST_APPEND_ARRAY4 85
  584. #define INST_APPEND_ARRAY_STK 86
  585. #define INST_APPEND_STK 87
  586. /* Opcodes 88 to 93 */
  587. #define INST_LAPPEND_SCALAR1 88
  588. #define INST_LAPPEND_SCALAR4 89
  589. #define INST_LAPPEND_ARRAY1 90
  590. #define INST_LAPPEND_ARRAY4 91
  591. #define INST_LAPPEND_ARRAY_STK 92
  592. #define INST_LAPPEND_STK 93
  593. /* TIP #22 - LINDEX operator with flat arg list */
  594. #define INST_LIST_INDEX_MULTI 94
  595. /*
  596. * TIP #33 - 'lset' command. Code gen also required a Forth-like
  597. * OVER operation.
  598. */
  599. #define INST_OVER 95
  600. #define INST_LSET_LIST 96
  601. #define INST_LSET_FLAT 97
  602. /* TIP#90 - 'return' command. */
  603. #define INST_RETURN_IMM 98
  604. /* TIP#123 - exponentiation operator. */
  605. #define INST_EXPON 99
  606. /* TIP #157 - {*}... (word expansion) language syntax support. */
  607. #define INST_EXPAND_START 100
  608. #define INST_EXPAND_STKTOP 101
  609. #define INST_INVOKE_EXPANDED 102
  610. /*
  611. * TIP #57 - 'lassign' command. Code generation requires immediate
  612. * LINDEX and LRANGE operators.
  613. */
  614. #define INST_LIST_INDEX_IMM 103
  615. #define INST_LIST_RANGE_IMM 104
  616. #define INST_START_CMD 105
  617. #define INST_LIST_IN 106
  618. #define INST_LIST_NOT_IN 107
  619. #define INST_PUSH_RETURN_OPTIONS 108
  620. #define INST_RETURN_STK 109
  621. /*
  622. * Dictionary (TIP#111) related commands.
  623. */
  624. #define INST_DICT_GET 110
  625. #define INST_DICT_SET 111
  626. #define INST_DICT_UNSET 112
  627. #define INST_DICT_INCR_IMM 113
  628. #define INST_DICT_APPEND 114
  629. #define INST_DICT_LAPPEND 115
  630. #define INST_DICT_FIRST 116
  631. #define INST_DICT_NEXT 117
  632. #define INST_DICT_DONE 118
  633. #define INST_DICT_UPDATE_START 119
  634. #define INST_DICT_UPDATE_END 120
  635. /*
  636. * Instruction to support jumps defined by tables (instead of the classic
  637. * [switch] technique of chained comparisons).
  638. */
  639. #define INST_JUMP_TABLE 121
  640. /*
  641. * Instructions to support compilation of global, variable, upvar and
  642. * [namespace upvar].
  643. */
  644. #define INST_UPVAR 122
  645. #define INST_NSUPVAR 123
  646. #define INST_VARIABLE 124
  647. /* Instruction to support compiling syntax error to bytecode */
  648. #define INST_SYNTAX 125
  649. /* Instruction to reverse N items on top of stack */
  650. #define INST_REVERSE 126
  651. /* regexp instruction */
  652. #define INST_REGEXP 127
  653. /* For [info exists] compilation */
  654. #define INST_EXIST_SCALAR 128
  655. #define INST_EXIST_ARRAY 129
  656. #define INST_EXIST_ARRAY_STK 130
  657. #define INST_EXIST_STK 131
  658. /* For [subst] compilation */
  659. #define INST_NOP 132
  660. #define INST_RETURN_CODE_BRANCH 133
  661. /* For [unset] compilation */
  662. #define INST_UNSET_SCALAR 134
  663. #define INST_UNSET_ARRAY 135
  664. #define INST_UNSET_ARRAY_STK 136
  665. #define INST_UNSET_STK 137
  666. /* For [dict with], [dict exists], [dict create] and [dict merge] */
  667. #define INST_DICT_EXPAND 138
  668. #define INST_DICT_RECOMBINE_STK 139
  669. #define INST_DICT_RECOMBINE_IMM 140
  670. #define INST_DICT_EXISTS 141
  671. #define INST_DICT_VERIFY 142
  672. /* For [string map] and [regsub] compilation */
  673. #define INST_STR_MAP 143
  674. #define INST_STR_FIND 144
  675. #define INST_STR_FIND_LAST 145
  676. #define INST_STR_RANGE_IMM 146
  677. #define INST_STR_RANGE 147
  678. /* For operations to do with coroutines and other NRE-manipulators */
  679. #define INST_YIELD 148
  680. #define INST_COROUTINE_NAME 149
  681. #define INST_TAILCALL 150
  682. /* For compilation of basic information operations */
  683. #define INST_NS_CURRENT 151
  684. #define INST_INFO_LEVEL_NUM 152
  685. #define INST_INFO_LEVEL_ARGS 153
  686. #define INST_RESOLVE_COMMAND 154
  687. /* For compilation relating to TclOO */
  688. #define INST_TCLOO_SELF 155
  689. #define INST_TCLOO_CLASS 156
  690. #define INST_TCLOO_NS 157
  691. #define INST_TCLOO_IS_OBJECT 158
  692. /* For compilation of [array] subcommands */
  693. #define INST_ARRAY_EXISTS_STK 159
  694. #define INST_ARRAY_EXISTS_IMM 160
  695. #define INST_ARRAY_MAKE_STK 161
  696. #define INST_ARRAY_MAKE_IMM 162
  697. #define INST_INVOKE_REPLACE 163
  698. #define INST_LIST_CONCAT 164
  699. #define INST_EXPAND_DROP 165
  700. /* New foreach implementation */
  701. #define INST_FOREACH_START 166
  702. #define INST_FOREACH_STEP 167
  703. #define INST_FOREACH_END 168
  704. #define INST_LMAP_COLLECT 169
  705. /* For compilation of [string trim] and related */
  706. #define INST_STR_TRIM 170
  707. #define INST_STR_TRIM_LEFT 171
  708. #define INST_STR_TRIM_RIGHT 172
  709. #define INST_CONCAT_STK 173
  710. #define INST_STR_UPPER 174
  711. #define INST_STR_LOWER 175
  712. #define INST_STR_TITLE 176
  713. #define INST_STR_REPLACE 177
  714. #define INST_ORIGIN_COMMAND 178
  715. #define INST_TCLOO_NEXT 179
  716. #define INST_TCLOO_NEXT_CLASS 180
  717. #define INST_YIELD_TO_INVOKE 181
  718. #define INST_NUM_TYPE 182
  719. #define INST_TRY_CVT_TO_BOOLEAN 183
  720. #define INST_STR_CLASS 184
  721. #define INST_LAPPEND_LIST 185
  722. #define INST_LAPPEND_LIST_ARRAY 186
  723. #define INST_LAPPEND_LIST_ARRAY_STK 187
  724. #define INST_LAPPEND_LIST_STK 188
  725. #define INST_CLOCK_READ 189
  726. /* The last opcode */
  727. #define LAST_INST_OPCODE 189
  728. /*
  729. * Table describing the Tcl bytecode instructions: their name (for displaying
  730. * code), total number of code bytes required (including operand bytes), and a
  731. * description of the type of each operand. These operand types include signed
  732. * and unsigned integers of length one and four bytes. The unsigned integers
  733. * are used for indexes or for, e.g., the count of objects to push in a "push"
  734. * instruction.
  735. */
  736. #define MAX_INSTRUCTION_OPERANDS 2
  737. typedef enum InstOperandType {
  738. OPERAND_NONE,
  739. OPERAND_INT1, /* One byte signed integer. */
  740. OPERAND_INT4, /* Four byte signed integer. */
  741. OPERAND_UINT1, /* One byte unsigned integer. */
  742. OPERAND_UINT4, /* Four byte unsigned integer. */
  743. OPERAND_IDX4, /* Four byte signed index (actually an
  744. * integer, but displayed differently.) */
  745. OPERAND_LVT1, /* One byte unsigned index into the local
  746. * variable table. */
  747. OPERAND_LVT4, /* Four byte unsigned index into the local
  748. * variable table. */
  749. OPERAND_AUX4, /* Four byte unsigned index into the aux data
  750. * table. */
  751. OPERAND_OFFSET1, /* One byte signed jump offset. */
  752. OPERAND_OFFSET4, /* Four byte signed jump offset. */
  753. OPERAND_LIT1, /* One byte unsigned index into table of
  754. * literals. */
  755. OPERAND_LIT4, /* Four byte unsigned index into table of
  756. * literals. */
  757. OPERAND_SCLS1 /* Index into tclStringClassTable. */
  758. } InstOperandType;
  759. typedef struct InstructionDesc {
  760. const char *name; /* Name of instruction. */
  761. int numBytes; /* Total number of bytes for instruction. */
  762. int stackEffect; /* The worst-case balance stack effect of the
  763. * instruction, used for stack requirements
  764. * computations. The value INT_MIN signals
  765. * that the instruction's worst case effect is
  766. * (1-opnd1). */
  767. int numOperands; /* Number of operands. */
  768. InstOperandType opTypes[MAX_INSTRUCTION_OPERANDS];
  769. /* The type of each operand. */
  770. } InstructionDesc;
  771. MODULE_SCOPE InstructionDesc const tclInstructionTable[];
  772. /*
  773. * Constants used by INST_STRING_CLASS to indicate character classes. These
  774. * correspond closely by name with what [string is] can support, but there is
  775. * no requirement to keep the values the same.
  776. */
  777. typedef enum InstStringClassType {
  778. STR_CLASS_ALNUM, /* Unicode alphabet or digit characters. */
  779. STR_CLASS_ALPHA, /* Unicode alphabet characters. */
  780. STR_CLASS_ASCII, /* Characters in range U+000000..U+00007F. */
  781. STR_CLASS_CONTROL, /* Unicode control characters. */
  782. STR_CLASS_DIGIT, /* Unicode digit characters. */
  783. STR_CLASS_GRAPH, /* Unicode printing characters, excluding
  784. * space. */
  785. STR_CLASS_LOWER, /* Unicode lower-case alphabet characters. */
  786. STR_CLASS_PRINT, /* Unicode printing characters, including
  787. * spaces. */
  788. STR_CLASS_PUNCT, /* Unicode punctuation characters. */
  789. STR_CLASS_SPACE, /* Unicode space characters. */
  790. STR_CLASS_UPPER, /* Unicode upper-case alphabet characters. */
  791. STR_CLASS_WORD, /* Unicode word (alphabetic, digit, connector
  792. * punctuation) characters. */
  793. STR_CLASS_XDIGIT /* Characters that can be used as digits in
  794. * hexadecimal numbers ([0-9A-Fa-f]). */
  795. } InstStringClassType;
  796. typedef struct StringClassDesc {
  797. const char *name; /* Name of the class. */
  798. int (*comparator)(int); /* Function to test if a single unicode
  799. * character is a member of the class. */
  800. } StringClassDesc;
  801. MODULE_SCOPE StringClassDesc const tclStringClassTable[];
  802. /*
  803. * Compilation of some Tcl constructs such as if commands and the logical or
  804. * (||) and logical and (&&) operators in expressions requires the generation
  805. * of forward jumps. Since the PC target of these jumps isn't known when the
  806. * jumps are emitted, we record the offset of each jump in an array of
  807. * JumpFixup structures. There is one array for each sequence of jumps to one
  808. * target PC. When we learn the target PC, we update the jumps with the
  809. * correct distance. Also, if the distance is too great (> 127 bytes), we
  810. * replace the single-byte jump with a four byte jump instruction, move the
  811. * instructions after the jump down, and update the code offsets for any
  812. * commands between the jump and the target.
  813. */
  814. typedef enum {
  815. TCL_UNCONDITIONAL_JUMP,
  816. TCL_TRUE_JUMP,
  817. TCL_FALSE_JUMP
  818. } TclJumpType;
  819. typedef struct JumpFixup {
  820. TclJumpType jumpType; /* Indicates the kind of jump. */
  821. unsigned int codeOffset; /* Offset of the first byte of the one-byte
  822. * forward jump's code. */
  823. int cmdIndex; /* Index of the first command after the one
  824. * for which the jump was emitted. Used to
  825. * update the code offsets for subsequent
  826. * commands if the two-byte jump at jumpPc
  827. * must be replaced with a five-byte one. */
  828. int exceptIndex; /* Index of the first range entry in the
  829. * ExceptionRange array after the current one.
  830. * This field is used to adjust the code
  831. * offsets in subsequent ExceptionRange
  832. * records when a jump is grown from 2 bytes
  833. * to 5 bytes. */
  834. } JumpFixup;
  835. #define JUMPFIXUP_INIT_ENTRIES 10
  836. typedef struct JumpFixupArray {
  837. JumpFixup *fixup; /* Points to start of jump fixup array. */
  838. int next; /* Index of next free array entry. */
  839. int end; /* Index of last usable entry in array. */
  840. int mallocedArray; /* 1 if array was expanded and fixups points
  841. * into the heap, else 0. */
  842. JumpFixup staticFixupSpace[JUMPFIXUP_INIT_ENTRIES];
  843. /* Initial storage for jump fixup array. */
  844. } JumpFixupArray;
  845. /*
  846. * The structure describing one variable list of a foreach command. Note that
  847. * only foreach commands inside procedure bodies are compiled inline so a
  848. * ForeachVarList structure always describes local variables. Furthermore,
  849. * only scalar variables are supported for inline-compiled foreach loops.
  850. */
  851. typedef struct ForeachVarList {
  852. int numVars; /* The number of variables in the list. */
  853. int varIndexes[TCLFLEXARRAY];/* An array of the indexes ("slot numbers")
  854. * for each variable in the procedure's array
  855. * of local variables. Only scalar variables
  856. * are supported. The actual size of this
  857. * field will be large enough to numVars
  858. * indexes. THIS MUST BE THE LAST FIELD IN THE
  859. * STRUCTURE! */
  860. } ForeachVarList;
  861. /*
  862. * Structure used to hold information about a foreach command that is needed
  863. * during program execution. These structures are stored in CompileEnv and
  864. * ByteCode structures as auxiliary data.
  865. */
  866. typedef struct ForeachInfo {
  867. int numLists; /* The number of both the variable and value
  868. * lists of the foreach command. */
  869. int firstValueTemp; /* Index of the first temp var in a proc frame
  870. * used to point to a value list. */
  871. int loopCtTemp; /* Index of temp var in a proc frame holding
  872. * the loop's iteration count. Used to
  873. * determine next value list element to assign
  874. * each loop var. */
  875. ForeachVarList *varLists[TCLFLEXARRAY];/* An array of pointers to ForeachVarList
  876. * structures describing each var list. The
  877. * actual size of this field will be large
  878. * enough to numVars indexes. THIS MUST BE THE
  879. * LAST FIELD IN THE STRUCTURE! */
  880. } ForeachInfo;
  881. /*
  882. * Structure used to hold information about a switch command that is needed
  883. * during program execution. These structures are stored in CompileEnv and
  884. * ByteCode structures as auxiliary data.
  885. */
  886. typedef struct JumptableInfo {
  887. Tcl_HashTable hashTable; /* Hash that maps strings to signed ints (PC
  888. * offsets). */
  889. } JumptableInfo;
  890. MODULE_SCOPE const AuxDataType tclJumptableInfoType;
  891. #define JUMPTABLEINFO(envPtr, index) \
  892. ((JumptableInfo*)((envPtr)->auxDataArrayPtr[TclGetUInt4AtPtr(index)].clientData))
  893. /*
  894. * Structure used to hold information about a [dict update] command that is
  895. * needed during program execution. These structures are stored in CompileEnv
  896. * and ByteCode structures as auxiliary data.
  897. */
  898. typedef struct {
  899. int length; /* Size of array */
  900. int varIndices[TCLFLEXARRAY]; /* Array of variable indices to manage when
  901. * processing the start and end of a [dict
  902. * update]. There is really more than one
  903. * entry, and the structure is allocated to
  904. * take account of this. MUST BE LAST FIELD IN
  905. * STRUCTURE. */
  906. } DictUpdateInfo;
  907. /*
  908. * ClientData type used by the math operator commands.
  909. */
  910. typedef struct {
  911. const char *op; /* Do not call it 'operator': C++ reserved */
  912. const char *expected;
  913. union {
  914. int numArgs;
  915. int identity;
  916. } i;
  917. } TclOpCmdClientData;
  918. /*
  919. *----------------------------------------------------------------
  920. * Procedures exported by tclBasic.c to be used within the engine.
  921. *----------------------------------------------------------------
  922. */
  923. MODULE_SCOPE Tcl_ObjCmdProc TclNRInterpCoroutine;
  924. /*
  925. *----------------------------------------------------------------
  926. * Procedures exported by the engine to be used by tclBasic.c
  927. *----------------------------------------------------------------
  928. */
  929. MODULE_SCOPE ByteCode * TclCompileObj(Tcl_Interp *interp, Tcl_Obj *objPtr,
  930. const CmdFrame *invoker, int word);
  931. /*
  932. *----------------------------------------------------------------
  933. * Procedures shared among Tcl bytecode compilation and execution modules but
  934. * not used outside:
  935. *----------------------------------------------------------------
  936. */
  937. MODULE_SCOPE int TclAttemptCompileProc(Tcl_Interp *interp,
  938. Tcl_Parse *parsePtr, int depth, Command *cmdPtr,
  939. CompileEnv *envPtr);
  940. MODULE_SCOPE void TclCleanupByteCode(ByteCode *codePtr);
  941. MODULE_SCOPE void TclCleanupStackForBreakContinue(CompileEnv *envPtr,
  942. ExceptionAux *auxPtr);
  943. MODULE_SCOPE void TclCompileCmdWord(Tcl_Interp *interp,
  944. Tcl_Token *tokenPtr, int count,
  945. CompileEnv *envPtr);
  946. MODULE_SCOPE void TclCompileExpr(Tcl_Interp *interp, const char *script,
  947. int numBytes, CompileEnv *envPtr, int optimize);
  948. MODULE_SCOPE void TclCompileExprWords(Tcl_Interp *interp,
  949. Tcl_Token *tokenPtr, int numWords,
  950. CompileEnv *envPtr);
  951. MODULE_SCOPE void TclCompileInvocation(Tcl_Interp *interp,
  952. Tcl_Token *tokenPtr, Tcl_Obj *cmdObj, int numWords,
  953. CompileEnv *envPtr);
  954. MODULE_SCOPE void TclCompileScript(Tcl_Interp *interp,
  955. const char *script, int numBytes,
  956. CompileEnv *envPtr);
  957. MODULE_SCOPE void TclCompileSyntaxError(Tcl_Interp *interp,
  958. CompileEnv *envPtr);
  959. MODULE_SCOPE void TclCompileTokens(Tcl_Interp *interp,
  960. Tcl_Token *tokenPtr, int count,
  961. CompileEnv *envPtr);
  962. MODULE_SCOPE void TclCompileVarSubst(Tcl_Interp *interp,
  963. Tcl_Token *tokenPtr, CompileEnv *envPtr);
  964. MODULE_SCOPE int TclCreateAuxData(ClientData clientData,
  965. const AuxDataType *typePtr, CompileEnv *envPtr);
  966. MODULE_SCOPE int TclCreateExceptRange(ExceptionRangeType type,
  967. CompileEnv *envPtr);
  968. MODULE_SCOPE ExecEnv * TclCreateExecEnv(Tcl_Interp *interp, int size);
  969. MODULE_SCOPE Tcl_Obj * TclCreateLiteral(Interp *iPtr, char *bytes,
  970. int length, unsigned int hash, int *newPtr,
  971. Namespace *nsPtr, int flags,
  972. LiteralEntry **globalPtrPtr);
  973. MODULE_SCOPE void TclDeleteExecEnv(ExecEnv *eePtr);
  974. MODULE_SCOPE void TclDeleteLiteralTable(Tcl_Interp *interp,
  975. LiteralTable *tablePtr);
  976. MODULE_SCOPE void TclEmitForwardJump(CompileEnv *envPtr,
  977. TclJumpType jumpType, JumpFixup *jumpFixupPtr);
  978. MODULE_SCOPE void TclEmitInvoke(CompileEnv *envPtr, int opcode, ...);
  979. MODULE_SCOPE ExceptionRange * TclGetExceptionRangeForPc(unsigned char *pc,
  980. int catchOnly, ByteCode *codePtr);
  981. MODULE_SCOPE void TclExpandJumpFixupArray(JumpFixupArray *fixupArrayPtr);
  982. MODULE_SCOPE int TclNRExecuteByteCode(Tcl_Interp *interp,
  983. ByteCode *codePtr);
  984. MODULE_SCOPE Tcl_Obj * TclFetchLiteral(CompileEnv *envPtr, unsigned int index);
  985. MODULE_SCOPE int TclFindCompiledLocal(const char *name, int nameChars,
  986. int create, CompileEnv *envPtr);
  987. MODULE_SCOPE int TclFixupForwardJump(CompileEnv *envPtr,
  988. JumpFixup *jumpFixupPtr, int jumpDist,
  989. int distThreshold);
  990. MODULE_SCOPE void TclFreeCompileEnv(CompileEnv *envPtr);
  991. MODULE_SCOPE void TclFreeJumpFixupArray(JumpFixupArray *fixupArrayPtr);
  992. MODULE_SCOPE int TclGetIndexFromToken(Tcl_Token *tokenPtr,
  993. int before, int after, int *indexPtr);
  994. MODULE_SCOPE void TclInitByteCodeObj(Tcl_Obj *objPtr,
  995. CompileEnv *envPtr);
  996. MODULE_SCOPE void TclInitCompileEnv(Tcl_Interp *interp,
  997. CompileEnv *envPtr, const char *string,
  998. int numBytes, const CmdFrame *invoker, int word);
  999. MODULE_SCOPE void TclInitJumpFixupArray(JumpFixupArray *fixupArrayPtr);
  1000. MODULE_SCOPE void TclInitLiteralTable(LiteralTable *tablePtr);
  1001. MODULE_SCOPE ExceptionRange *TclGetInnermostExceptionRange(CompileEnv *envPtr,
  1002. int returnCode, ExceptionAux **auxPtrPtr);
  1003. MODULE_SCOPE void TclAddLoopBreakFixup(CompileEnv *envPtr,
  1004. ExceptionAux *auxPtr);
  1005. MODULE_SCOPE void TclAddLoopContinueFixup(CompileEnv *envPtr,
  1006. ExceptionAux *auxPtr);
  1007. MODULE_SCOPE void TclFinalizeLoopExceptionRange(CompileEnv *envPtr,
  1008. int range);
  1009. #ifdef TCL_COMPILE_STATS
  1010. MODULE_SCOPE char * TclLiteralStats(LiteralTable *tablePtr);
  1011. MODULE_SCOPE int TclLog2(int value);
  1012. #endif
  1013. MODULE_SCOPE int TclLocalScalar(const char *bytes, int numBytes,
  1014. CompileEnv *envPtr);
  1015. MODULE_SCOPE int TclLocalScalarFromToken(Tcl_Token *tokenPtr,
  1016. CompileEnv *envPtr);
  1017. MODULE_SCOPE void TclOptimizeBytecode(void *envPtr);
  1018. #ifdef TCL_COMPILE_DEBUG
  1019. MODULE_SCOPE void TclPrintByteCodeObj(Tcl_Interp *interp,
  1020. Tcl_Obj *objPtr);
  1021. #endif
  1022. MODULE_SCOPE int TclPrintInstruction(ByteCode *codePtr,
  1023. const unsigned char *pc);
  1024. MODULE_SCOPE void TclPrintObject(FILE *outFile,
  1025. Tcl_Obj *objPtr, int maxChars);
  1026. MODULE_SCOPE void TclPrintSource(FILE *outFile,
  1027. const char *string, int maxChars);
  1028. MODULE_SCOPE void TclPushVarName(Tcl_Interp *interp,
  1029. Tcl_Token *varTokenPtr, CompileEnv *envPtr,
  1030. int flags, int *localIndexPtr,
  1031. int *isScalarPtr);
  1032. static inline void
  1033. TclPreserveByteCode(
  1034. ByteCode *codePtr)
  1035. {
  1036. codePtr->refCount++;
  1037. }
  1038. static inline void
  1039. TclReleaseByteCode(
  1040. ByteCode *codePtr)
  1041. {
  1042. if (codePtr->refCount-- > 1) {
  1043. return;
  1044. }
  1045. /* Just dropped to refcount==0. Clean up. */
  1046. TclCleanupByteCode(codePtr);
  1047. }
  1048. MODULE_SCOPE void TclReleaseLiteral(Tcl_Interp *interp, Tcl_Obj *objPtr);
  1049. MODULE_SCOPE void TclInvalidateCmdLiteral(Tcl_Interp *interp,
  1050. const char *name, Namespace *nsPtr);
  1051. MODULE_SCOPE int TclSingleOpCmd(ClientData clientData,
  1052. Tcl_Interp *interp, int objc,
  1053. Tcl_Obj *const objv[]);
  1054. MODULE_SCOPE int TclSortingOpCmd(ClientData clientData,
  1055. Tcl_Interp *interp, int objc,
  1056. Tcl_Obj *const objv[]);
  1057. MODULE_SCOPE int TclVariadicOpCmd(ClientData clientData,
  1058. Tcl_Interp *interp, int objc,
  1059. Tcl_Obj *const objv[]);
  1060. MODULE_SCOPE int TclNoIdentOpCmd(ClientData clientData,
  1061. Tcl_Interp *interp, int objc,
  1062. Tcl_Obj *const objv[]);
  1063. #ifdef TCL_COMPILE_DEBUG
  1064. MODULE_SCOPE void TclVerifyGlobalLiteralTable(Interp *iPtr);
  1065. MODULE_SCOPE void TclVerifyLocalLiteralTable(CompileEnv *envPtr);
  1066. #endif
  1067. MODULE_SCOPE int TclWordKnownAtCompileTime(Tcl_Token *tokenPtr,
  1068. Tcl_Obj *valuePtr);
  1069. MODULE_SCOPE void TclLogCommandInfo(Tcl_Interp *interp,
  1070. const char *script, const char *command,
  1071. int length, const unsigned char *pc,
  1072. Tcl_Obj **tosPtr);
  1073. MODULE_SCOPE Tcl_Obj *TclGetInnerContext(Tcl_Interp *interp,
  1074. const unsigned char *pc, Tcl_Obj **tosPtr);
  1075. MODULE_SCOPE Tcl_Obj *TclNewInstNameObj(unsigned char inst);
  1076. MODULE_SCOPE int TclPushProcCallFrame(ClientData clientData,
  1077. Tcl_Interp *interp, int objc,
  1078. Tcl_Obj *const objv[], int isLambda);
  1079. /*
  1080. *----------------------------------------------------------------
  1081. * Macros and flag values used by Tcl bytecode compilation and execution
  1082. * modules inside the Tcl core but not used outside.
  1083. *----------------------------------------------------------------
  1084. */
  1085. /*
  1086. * Simplified form to access AuxData.
  1087. *
  1088. * ClientData TclFetchAuxData(CompileEng *envPtr, int index);
  1089. */
  1090. #define TclFetchAuxData(envPtr, index) \
  1091. (envPtr)->auxDataArrayPtr[(index)].clientData
  1092. #define LITERAL_ON_HEAP 0x01
  1093. #define LITERAL_CMD_NAME 0x02
  1094. #define LITERAL_UNSHARED 0x04
  1095. /*
  1096. * Form of TclRegisterLiteral with flags == 0. In that case, it is safe to
  1097. * cast away constness, and it is cleanest to do that here, all in one place.
  1098. *
  1099. * int TclRegisterNewLiteral(CompileEnv *envPtr, const char *bytes,
  1100. * int length);
  1101. */
  1102. #define TclRegisterNewLiteral(envPtr, bytes, length) \
  1103. TclRegisterLiteral(envPtr, (char *)(bytes), length, /*flags*/ 0)
  1104. /*
  1105. * Form of TclRegisterLiteral with flags == LITERAL_CMD_NAME. In that case, it
  1106. * is safe to cast away constness, and it is cleanest to do that here, all in
  1107. * one place.
  1108. *
  1109. * int TclRegisterNewNSLiteral(CompileEnv *envPtr, const char *bytes,
  1110. * int length);
  1111. */
  1112. #define TclRegisterNewCmdLiteral(envPtr, bytes, length) \
  1113. TclRegisterLiteral(envPtr, (char *)(bytes), length, LITERAL_CMD_NAME)
  1114. /*
  1115. * Macro used to manually adjust the stack requirements; used in cases where
  1116. * the stack effect cannot be computed from the opcode and its operands, but
  1117. * is still known at compile time.
  1118. *
  1119. * void TclAdjustStackDepth(int delta, CompileEnv *envPtr);
  1120. */
  1121. #define TclAdjustStackDepth(delta, envPtr) \
  1122. do { \
  1123. if ((delta) < 0) { \
  1124. if ((envPtr)->maxStackDepth < (envPtr)->currStackDepth) { \
  1125. (envPtr)->maxStackDepth = (envPtr)->currStackDepth; \
  1126. } \
  1127. } \
  1128. (envPtr)->currStackDepth += (delta); \
  1129. } while (0)
  1130. #define TclGetStackDepth(envPtr) \
  1131. ((envPtr)->currStackDepth)
  1132. #define TclSetStackDepth(depth, envPtr) \
  1133. (envPtr)->currStackDepth = (depth)
  1134. #define TclCheckStackDepth(depth, envPtr) \
  1135. do { \
  1136. int _dd = (depth); \
  1137. if (_dd != (envPtr)->currStackDepth) { \
  1138. Tcl_Panic("bad stack depth computations: is %i, should be %i", \
  1139. (envPtr)->currStackDepth, _dd); \
  1140. } \
  1141. } while (0)
  1142. /*
  1143. * Macro used to update the stack requirements. It is called by the macros
  1144. * TclEmitOpCode, TclEmitInst1 and TclEmitInst4.
  1145. * Remark that the very last instruction of a bytecode always reduces the
  1146. * stack level: INST_DONE or INST_POP, so that the maxStackdepth is always
  1147. * updated.
  1148. *
  1149. * void TclUpdateStackReqs(unsigned char op, int i, CompileEnv *envPtr);
  1150. */
  1151. #define TclUpdateStackReqs(op, i, envPtr) \
  1152. do { \
  1153. int _delta = tclInstructionTable[(op)].stackEffect; \
  1154. if (_delta) { \
  1155. if (_delta == INT_MIN) { \
  1156. _delta = 1 - (i); \
  1157. } \
  1158. TclAdjustStackDepth(_delta, envPtr); \
  1159. } \
  1160. } while (0)
  1161. /*
  1162. * Macros used to update the flag that indicates if we are at the start of a
  1163. * command, based on whether the opcode is INST_START_COMMAND.
  1164. *
  1165. * void TclUpdateAtCmdStart(unsigned char op, CompileEnv *envPtr);
  1166. */
  1167. #define TclUpdateAtCmdStart(op, envPtr) \
  1168. if ((envPtr)->atCmdStart < 2) { \
  1169. (envPtr)->atCmdStart = ((op) == INST_START_CMD ? 1 : 0); \
  1170. }
  1171. /*
  1172. * Macro to emit an opcode byte into a CompileEnv's code array. The ANSI C
  1173. * "prototype" for this macro is:
  1174. *
  1175. * void TclEmitOpcode(unsigned char op, CompileEnv *envPtr);
  1176. */
  1177. #define TclEmitOpcode(op, envPtr) \
  1178. do { \
  1179. if ((envPtr)->codeNext == (envPtr)->codeEnd) { \
  1180. TclExpandCodeArray(envPtr); \
  1181. } \
  1182. *(envPtr)->codeNext++ = (unsigned char) (op); \
  1183. TclUpdateAtCmdStart(op, envPtr); \
  1184. TclUpdateStackReqs(op, 0, envPtr); \
  1185. } while (0)
  1186. /*
  1187. * Macros to emit an integer operand. The ANSI C "prototype" for these macros
  1188. * are:
  1189. *
  1190. * void TclEmitInt1(int i, CompileEnv *envPtr);
  1191. * void TclEmitInt4(int i, CompileEnv *envPtr);
  1192. */
  1193. #define TclEmitInt1(i, envPtr) \
  1194. do { \
  1195. if ((envPtr)->codeNext == (envPtr)->codeEnd) { \
  1196. TclExpandCodeArray(envPtr); \
  1197. } \
  1198. *(envPtr)->codeNext++ = (unsigned char) ((unsigned int) (i)); \
  1199. } while (0)
  1200. #define TclEmitInt4(i, envPtr) \
  1201. do { \
  1202. if (((envPtr)->codeNext + 4) > (envPtr)->codeEnd) { \
  1203. TclExpandCodeArray(envPtr); \
  1204. } \
  1205. *(envPtr)->codeNext++ = \
  1206. (unsigned char) ((unsigned int) (i) >> 24); \
  1207. *(envPtr)->codeNext++ = \
  1208. (unsigned char) ((unsigned int) (i) >> 16); \
  1209. *(envPtr)->codeNext++ = \
  1210. (unsigned char) ((unsigned int) (i) >> 8); \
  1211. *(envPtr)->codeNext++ = \
  1212. (unsigned char) ((unsigned int) (i) ); \
  1213. } while (0)
  1214. /*
  1215. * Macros to emit an instruction with signed or unsigned integer operands.
  1216. * Four byte integers are stored in "big-endian" order with the high order
  1217. * byte stored at the lowest address. The ANSI C "prototypes" for these macros
  1218. * are:
  1219. *
  1220. * void TclEmitInstInt1(unsigned char op, int i, CompileEnv *envPtr);
  1221. * void TclEmitInstInt4(unsigned char op, int i, CompileEnv *envPtr);
  1222. */
  1223. #define TclEmitInstInt1(op, i, envPtr) \
  1224. do { \
  1225. if (((envPtr)->codeNext + 2) > (envPtr)->codeEnd) { \
  1226. TclExpandCodeArray(envPtr); \
  1227. } \
  1228. *(envPtr)->codeNext++ = (unsigned char) (op); \
  1229. *(envPtr)->codeNext++ = (unsigned char) ((unsigned int) (i)); \
  1230. TclUpdateAtCmdStart(op, envPtr); \
  1231. TclUpdateStackReqs(op, i, envPtr); \
  1232. } while (0)
  1233. #define TclEmitInstInt4(op, i, envPtr) \
  1234. do { \
  1235. if (((envPtr)->codeNext + 5) > (envPtr)->codeEnd) { \
  1236. TclExpandCodeArray(envPtr); \
  1237. } \
  1238. *(envPtr)->codeNext++ = (unsigned char) (op); \
  1239. *(envPtr)->codeNext++ = \
  1240. (unsigned char) ((unsigned int) (i) >> 24); \
  1241. *(envPtr)->codeNext++ = \
  1242. (unsigned char) ((unsigned int) (i) >> 16); \
  1243. *(envPtr)->codeNext++ = \
  1244. (unsigned char) ((unsigned int) (i) >> 8); \
  1245. *(envPtr)->codeNext++ = \
  1246. (unsigned char) ((unsigned int) (i) ); \
  1247. TclUpdateAtCmdStart(op, envPtr); \
  1248. TclUpdateStackReqs(op, i, envPtr); \
  1249. } while (0)
  1250. /*
  1251. * Macro to push a Tcl object onto the Tcl evaluation stack. It emits the
  1252. * object's one or four byte array index into the CompileEnv's code array.
  1253. * These support, respectively, a maximum of 256 (2**8) and 2**32 objects in a
  1254. * CompileEnv. The ANSI C "prototype" for this macro is:
  1255. *
  1256. * void TclEmitPush(int objIndex, CompileEnv *envPtr);
  1257. */
  1258. #define TclEmitPush(objIndex, envPtr) \
  1259. do { \
  1260. int _objIndexCopy = (objIndex); \
  1261. if (_objIndexCopy <= 255) { \
  1262. TclEmitInstInt1(INST_PUSH1, _objIndexCopy, (envPtr)); \
  1263. } else { \
  1264. TclEmitInstInt4(INST_PUSH4, _objIndexCopy, (envPtr)); \
  1265. } \
  1266. } while (0)
  1267. /*
  1268. * Macros to update a (signed or unsigned) integer starting at a pointer. The
  1269. * two variants depend on the number of bytes. The ANSI C "prototypes" for
  1270. * these macros are:
  1271. *
  1272. * void TclStoreInt1AtPtr(int i, unsigned char *p);
  1273. * void TclStoreInt4AtPtr(int i, unsigned char *p);
  1274. */
  1275. #define TclStoreInt1AtPtr(i, p) \
  1276. *(p) = (unsigned char) ((unsigned int) (i))
  1277. #define TclStoreInt4AtPtr(i, p) \
  1278. do { \
  1279. *(p) = (unsigned char) ((unsigned int) (i) >> 24); \
  1280. *(p+1) = (unsigned char) ((unsigned int) (i) >> 16); \
  1281. *(p+2) = (unsigned char) ((unsigned int) (i) >> 8); \
  1282. *(p+3) = (unsigned char) ((unsigned int) (i) ); \
  1283. } while (0)
  1284. /*
  1285. * Macros to update instructions at a particular pc with a new op code and a
  1286. * (signed or unsigned) int operand. The ANSI C "prototypes" for these macros
  1287. * are:
  1288. *
  1289. * void TclUpdateInstInt1AtPc(unsigned char op, int i, unsigned char *pc);
  1290. * void TclUpdateInstInt4AtPc(unsigned char op, int i, unsigned char *pc);
  1291. */
  1292. #define TclUpdateInstInt1AtPc(op, i, pc) \
  1293. do { \
  1294. *(pc) = (unsigned char) (op); \
  1295. TclStoreInt1AtPtr((i), ((pc)+1)); \
  1296. } while (0)
  1297. #define TclUpdateInstInt4AtPc(op, i, pc) \
  1298. do { \
  1299. *(pc) = (unsigned char) (op); \
  1300. TclStoreInt4AtPtr((i), ((pc)+1)); \
  1301. } while (0)
  1302. /*
  1303. * Macro to fix up a forward jump to point to the current code-generation
  1304. * position in the bytecode being created (the most common case). The ANSI C
  1305. * "prototypes" for this macro is:
  1306. *
  1307. * int TclFixupForwardJumpToHere(CompileEnv *envPtr, JumpFixup *fixupPtr,
  1308. * int threshold);
  1309. */
  1310. #define TclFixupForwardJumpToHere(envPtr, fixupPtr, threshold) \
  1311. TclFixupForwardJump((envPtr), (fixupPtr), \
  1312. (envPtr)->codeNext-(envPtr)->codeStart-(fixupPtr)->codeOffset, \
  1313. (threshold))
  1314. /*
  1315. * Macros to get a signed integer (GET_INT{1,2}) or an unsigned int
  1316. * (GET_UINT{1,2}) from a pointer. There are two variants for each return type
  1317. * that depend on the number of bytes fetched. The ANSI C "prototypes" for
  1318. * these macros are:
  1319. *
  1320. * int TclGetInt1AtPtr(unsigned char *p);
  1321. * int TclGetInt4AtPtr(unsigned char *p);
  1322. * unsigned int TclGetUInt1AtPtr(unsigned char *p);
  1323. * unsigned int TclGetUInt4AtPtr(unsigned char *p);
  1324. */
  1325. /*
  1326. * The TclGetInt1AtPtr macro is tricky because we want to do sign extension on
  1327. * the 1-byte value. Unfortunately the "char" type isn't signed on all
  1328. * platforms so sign-extension doesn't always happen automatically. Sometimes
  1329. * we can explicitly declare the pointer to be signed, but other times we have
  1330. * to explicitly sign-extend the value in software.
  1331. */
  1332. #ifndef __CHAR_UNSIGNED__
  1333. # define TclGetInt1AtPtr(p) ((int) *((char *) p))
  1334. #elif defined(HAVE_SIGNED_CHAR)
  1335. # define TclGetInt1AtPtr(p) ((int) *((signed char *) p))
  1336. #else
  1337. # define TclGetInt1AtPtr(p) \
  1338. (((int) *((char *) p)) | ((*(p) & 0200) ? (-256) : 0))
  1339. #endif
  1340. #define TclGetInt4AtPtr(p) \
  1341. (((int) (TclGetUInt1AtPtr(p) << 24)) | \
  1342. (*((p)+1) << 16) | \
  1343. (*((p)+2) << 8) | \
  1344. (*((p)+3)))
  1345. #define TclGetUInt1AtPtr(p) \
  1346. ((unsigned int) *(p))
  1347. #define TclGetUInt4AtPtr(p) \
  1348. ((unsigned int) (*(p) << 24) | \
  1349. (*((p)+1) << 16) | \
  1350. (*((p)+2) << 8) | \
  1351. (*((p)+3)))
  1352. /*
  1353. * Macros used to compute the minimum and maximum of two integers. The ANSI C
  1354. * "prototypes" for these macros are:
  1355. *
  1356. * int TclMin(int i, int j);
  1357. * int TclMax(int i, int j);
  1358. */
  1359. #define TclMin(i, j) ((((int) i) < ((int) j))? (i) : (j))
  1360. #define TclMax(i, j) ((((int) i) > ((int) j))? (i) : (j))
  1361. /*
  1362. * Convenience macros for use when compiling bodies of commands. The ANSI C
  1363. * "prototype" for these macros are:
  1364. *
  1365. * static void BODY(Tcl_Token *tokenPtr, int word);
  1366. */
  1367. #define BODY(tokenPtr, word) \
  1368. SetLineInformation((word)); \
  1369. TclCompileCmdWord(interp, (tokenPtr)+1, (tokenPtr)->numComponents, \
  1370. envPtr)
  1371. /*
  1372. * Convenience macro for use when compiling tokens to be pushed. The ANSI C
  1373. * "prototype" for this macro is:
  1374. *
  1375. * static void CompileTokens(CompileEnv *envPtr, Tcl_Token *tokenPtr,
  1376. * Tcl_Interp *interp);
  1377. */
  1378. #define CompileTokens(envPtr, tokenPtr, interp) \
  1379. TclCompileTokens((interp), (tokenPtr)+1, (tokenPtr)->numComponents, \
  1380. (envPtr));
  1381. /*
  1382. * Convenience macros for use when pushing literals. The ANSI C "prototype" for
  1383. * these macros are:
  1384. *
  1385. * static void PushLiteral(CompileEnv *envPtr,
  1386. * const char *string, int length);
  1387. * static void PushStringLiteral(CompileEnv *envPtr,
  1388. * const char *string);
  1389. */
  1390. #define PushLiteral(envPtr, string, length) \
  1391. TclEmitPush(TclRegisterNewLiteral((envPtr), (string), (length)), (envPtr))
  1392. #define PushStringLiteral(envPtr, string) \
  1393. PushLiteral((envPtr), (string), (int) (sizeof(string "") - 1))
  1394. /*
  1395. * Macro to advance to the next token; it is more mnemonic than the address
  1396. * arithmetic that it replaces. The ANSI C "prototype" for this macro is:
  1397. *
  1398. * static Tcl_Token * TokenAfter(Tcl_Token *tokenPtr);
  1399. */
  1400. #define TokenAfter(tokenPtr) \
  1401. ((tokenPtr) + ((tokenPtr)->numComponents + 1))
  1402. /*
  1403. * Macro to get the offset to the next instruction to be issued. The ANSI C
  1404. * "prototype" for this macro is:
  1405. *
  1406. * static int CurrentOffset(CompileEnv *envPtr);
  1407. */
  1408. #define CurrentOffset(envPtr) \
  1409. ((envPtr)->codeNext - (envPtr)->codeStart)
  1410. /*
  1411. * Note: the exceptDepth is a bit of a misnomer: TEBC only needs the
  1412. * maximal depth of nested CATCH ranges in order to alloc runtime
  1413. * memory. These macros should compute precisely that? OTOH, the nesting depth
  1414. * of LOOP ranges is an interesting datum for debugging purposes, and that is
  1415. * what we compute now.
  1416. *
  1417. * static int ExceptionRangeStarts(CompileEnv *envPtr, int index);
  1418. * static void ExceptionRangeEnds(CompileEnv *envPtr, int index);
  1419. * static void ExceptionRangeTarget(CompileEnv *envPtr, int index, LABEL);
  1420. */
  1421. #define ExceptionRangeStarts(envPtr, index) \
  1422. (((envPtr)->exceptDepth++), \
  1423. ((envPtr)->maxExceptDepth = \
  1424. TclMax((envPtr)->exceptDepth, (envPtr)->maxExceptDepth)), \
  1425. ((envPtr)->exceptArrayPtr[(index)].codeOffset = CurrentOffset(envPtr)))
  1426. #define ExceptionRangeEnds(envPtr, index) \
  1427. (((envPtr)->exceptDepth--), \
  1428. ((envPtr)->exceptArrayPtr[(index)].numCodeBytes = \
  1429. CurrentOffset(envPtr) - (envPtr)->exceptArrayPtr[(index)].codeOffset))
  1430. #define ExceptionRangeTarget(envPtr, index, targetType) \
  1431. ((envPtr)->exceptArrayPtr[(index)].targetType = CurrentOffset(envPtr))
  1432. /*
  1433. * Check if there is an LVT for compiled locals
  1434. */
  1435. #define EnvHasLVT(envPtr) \
  1436. (envPtr->procPtr || envPtr->iPtr->varFramePtr->localCachePtr)
  1437. /*
  1438. * Macros for making it easier to deal with tokens and DStrings.
  1439. */
  1440. #define TclDStringAppendToken(dsPtr, tokenPtr) \
  1441. Tcl_DStringAppend((dsPtr), (tokenPtr)->start, (tokenPtr)->size)
  1442. #define TclRegisterDStringLiteral(envPtr, dsPtr) \
  1443. TclRegisterLiteral(envPtr, Tcl_DStringValue(dsPtr), \
  1444. Tcl_DStringLength(dsPtr), /*flags*/ 0)
  1445. /*
  1446. * Macro that encapsulates an efficiency trick that avoids a function call for
  1447. * the simplest of compiles. The ANSI C "prototype" for this macro is:
  1448. *
  1449. * static void CompileWord(CompileEnv *envPtr, Tcl_Token *tokenPtr,
  1450. * Tcl_Interp *interp, int word);
  1451. */
  1452. #define CompileWord(envPtr, tokenPtr, interp, word) \
  1453. if ((tokenPtr)->type == TCL_TOKEN_SIMPLE_WORD) { \
  1454. PushLiteral((envPtr), (tokenPtr)[1].start, (tokenPtr)[1].size); \
  1455. } else { \
  1456. SetLineInformation((word)); \
  1457. CompileTokens((envPtr), (tokenPtr), (interp)); \
  1458. }
  1459. /*
  1460. * TIP #280: Remember the per-word line information of the current command. An
  1461. * index is used instead of a pointer as recursive compilation may reallocate,
  1462. * i.e. move, the array. This is also the reason to save the nuloc now, it may
  1463. * change during the course of the function.
  1464. *
  1465. * Macro to encapsulate the variable definition and setup.
  1466. */
  1467. #define DefineLineInformation \
  1468. ExtCmdLoc *mapPtr = envPtr->extCmdMapPtr; \
  1469. int eclIndex = mapPtr->nuloc - 1
  1470. #define SetLineInformation(word) \
  1471. envPtr->line = mapPtr->loc[eclIndex].line[(word)]; \
  1472. envPtr->clNext = mapPtr->loc[eclIndex].next[(word)]
  1473. #define PushVarNameWord(i,v,e,f,l,sc,word) \
  1474. SetLineInformation(word); \
  1475. TclPushVarName(i,v,e,f,l,sc)
  1476. /*
  1477. * Often want to issue one of two versions of an instruction based on whether
  1478. * the argument will fit in a single byte or not. This makes it much clearer.
  1479. */
  1480. #define Emit14Inst(nm,idx,envPtr) \
  1481. if (idx <= 255) { \
  1482. TclEmitInstInt1(nm##1,idx,envPtr); \
  1483. } else { \
  1484. TclEmitInstInt4(nm##4,idx,envPtr); \
  1485. }
  1486. /*
  1487. * How to get an anonymous local variable (used for holding temporary values
  1488. * off the stack) or a local simple scalar.
  1489. */
  1490. #define AnonymousLocal(envPtr) \
  1491. (TclFindCompiledLocal(NULL, /*nameChars*/ 0, /*create*/ 1, (envPtr)))
  1492. #define LocalScalar(chars,len,envPtr) \
  1493. TclLocalScalar(chars, len, envPtr)
  1494. #define LocalScalarFromToken(tokenPtr,envPtr) \
  1495. TclLocalScalarFromToken(tokenPtr, envPtr)
  1496. /*
  1497. * Flags bits used by TclPushVarName.
  1498. */
  1499. #define TCL_NO_LARGE_INDEX 1 /* Do not return localIndex value > 255 */
  1500. #define TCL_NO_ELEMENT 2 /* Do not push the array element. */
  1501. /*
  1502. * DTrace probe macros (NOPs if DTrace support is not enabled).
  1503. */
  1504. /*
  1505. * Define the following macros to enable debug logging of the DTrace proc,
  1506. * cmd, and inst probes. Note that this does _not_ require a platform with
  1507. * DTrace, it simply logs all probe output to /tmp/tclDTraceDebug-[pid].log.
  1508. *
  1509. * If the second macro is defined, logging to file starts immediately,
  1510. * otherwise only after the first call to [tcl::dtrace]. Note that the debug
  1511. * probe data is always computed, even when it is not logged to file.
  1512. *
  1513. * Defining the third macro enables debug logging of inst probes (disabled
  1514. * by default due to the significant performance impact).
  1515. */
  1516. /*
  1517. #define TCL_DTRACE_DEBUG 1
  1518. #define TCL_DTRACE_DEBUG_LOG_ENABLED 1
  1519. #define TCL_DTRACE_DEBUG_INST_PROBES 1
  1520. */
  1521. #if !(defined(TCL_DTRACE_DEBUG) && defined(__GNUC__))
  1522. #ifdef USE_DTRACE
  1523. #if defined(__GNUC__) && __GNUC__ > 2
  1524. /*
  1525. * Use gcc branch prediction hint to minimize cost of DTrace ENABLED checks.
  1526. */
  1527. #define unlikely(x) (__builtin_expect((x), 0))
  1528. #else
  1529. #define unlikely(x) (x)
  1530. #endif
  1531. #define TCL_DTRACE_PROC_ENTRY_ENABLED() unlikely(TCL_PROC_ENTRY_ENABLED())
  1532. #define TCL_DTRACE_PROC_RETURN_ENABLED() unlikely(TCL_PROC_RETURN_ENABLED())
  1533. #define TCL_DTRACE_PROC_RESULT_ENABLED() unlikely(TCL_PROC_RESULT_ENABLED())
  1534. #define TCL_DTRACE_PROC_ARGS_ENABLED() unlikely(TCL_PROC_ARGS_ENABLED())
  1535. #define TCL_DTRACE_PROC_INFO_ENABLED() unlikely(TCL_PROC_INFO_ENABLED())
  1536. #define TCL_DTRACE_PROC_ENTRY(a0, a1, a2) TCL_PROC_ENTRY(a0, a1, a2)
  1537. #define TCL_DTRACE_PROC_RETURN(a0, a1) TCL_PROC_RETURN(a0, a1)
  1538. #define TCL_DTRACE_PROC_RESULT(a0, a1, a2, a3) TCL_PROC_RESULT(a0, a1, a2, a3)
  1539. #define TCL_DTRACE_PROC_ARGS(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) \
  1540. TCL_PROC_ARGS(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
  1541. #define TCL_DTRACE_PROC_INFO(a0, a1, a2, a3, a4, a5, a6, a7) \
  1542. TCL_PROC_INFO(a0, a1, a2, a3, a4, a5, a6, a7)
  1543. #define TCL_DTRACE_CMD_ENTRY_ENABLED() unlikely(TCL_CMD_ENTRY_ENABLED())
  1544. #define TCL_DTRACE_CMD_RETURN_ENABLED() unlikely(TCL_CMD_RETURN_ENABLED())
  1545. #define TCL_DTRACE_CMD_RESULT_ENABLED() unlikely(TCL_CMD_RESULT_ENABLED())
  1546. #define TCL_DTRACE_CMD_ARGS_ENABLED() unlikely(TCL_CMD_ARGS_ENABLED())
  1547. #define TCL_DTRACE_CMD_INFO_ENABLED() unlikely(TCL_CMD_INFO_ENABLED())
  1548. #define TCL_DTRACE_CMD_ENTRY(a0, a1, a2) TCL_CMD_ENTRY(a0, a1, a2)
  1549. #define TCL_DTRACE_CMD_RETURN(a0, a1) TCL_CMD_RETURN(a0, a1)
  1550. #define TCL_DTRACE_CMD_RESULT(a0, a1, a2, a3) TCL_CMD_RESULT(a0, a1, a2, a3)
  1551. #define TCL_DTRACE_CMD_ARGS(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) \
  1552. TCL_CMD_ARGS(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
  1553. #define TCL_DTRACE_CMD_INFO(a0, a1, a2, a3, a4, a5, a6, a7) \
  1554. TCL_CMD_INFO(a0, a1, a2, a3, a4, a5, a6, a7)
  1555. #define TCL_DTRACE_INST_START_ENABLED() unlikely(TCL_INST_START_ENABLED())
  1556. #define TCL_DTRACE_INST_DONE_ENABLED() unlikely(TCL_INST_DONE_ENABLED())
  1557. #define TCL_DTRACE_INST_START(a0, a1, a2) TCL_INST_START(a0, a1, a2)
  1558. #define TCL_DTRACE_INST_DONE(a0, a1, a2) TCL_INST_DONE(a0, a1, a2)
  1559. #define TCL_DTRACE_TCL_PROBE_ENABLED() unlikely(TCL_TCL_PROBE_ENABLED())
  1560. #define TCL_DTRACE_TCL_PROBE(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) \
  1561. TCL_TCL_PROBE(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
  1562. #define TCL_DTRACE_DEBUG_LOG()
  1563. MODULE_SCOPE void TclDTraceInfo(Tcl_Obj *info, const char **args,
  1564. int *argsi);
  1565. #else /* USE_DTRACE */
  1566. #define TCL_DTRACE_PROC_ENTRY_ENABLED() 0
  1567. #define TCL_DTRACE_PROC_RETURN_ENABLED() 0
  1568. #define TCL_DTRACE_PROC_RESULT_ENABLED() 0
  1569. #define TCL_DTRACE_PROC_ARGS_ENABLED() 0
  1570. #define TCL_DTRACE_PROC_INFO_ENABLED() 0
  1571. #define TCL_DTRACE_PROC_ENTRY(a0, a1, a2) {if (a0) {}}
  1572. #define TCL_DTRACE_PROC_RETURN(a0, a1) {if (a0) {}}
  1573. #define TCL_DTRACE_PROC_RESULT(a0, a1, a2, a3) {if (a0) {}; if (a3) {}}
  1574. #define TCL_DTRACE_PROC_ARGS(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {}
  1575. #define TCL_DTRACE_PROC_INFO(a0, a1, a2, a3, a4, a5, a6, a7) {}
  1576. #define TCL_DTRACE_CMD_ENTRY_ENABLED() 0
  1577. #define TCL_DTRACE_CMD_RETURN_ENABLED() 0
  1578. #define TCL_DTRACE_CMD_RESULT_ENABLED() 0
  1579. #define TCL_DTRACE_CMD_ARGS_ENABLED() 0
  1580. #define TCL_DTRACE_CMD_INFO_ENABLED() 0
  1581. #define TCL_DTRACE_CMD_ENTRY(a0, a1, a2) {}
  1582. #define TCL_DTRACE_CMD_RETURN(a0, a1) {}
  1583. #define TCL_DTRACE_CMD_RESULT(a0, a1, a2, a3) {}
  1584. #define TCL_DTRACE_CMD_ARGS(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {}
  1585. #define TCL_DTRACE_CMD_INFO(a0, a1, a2, a3, a4, a5, a6, a7) {}
  1586. #define TCL_DTRACE_INST_START_ENABLED() 0
  1587. #define TCL_DTRACE_INST_DONE_ENABLED() 0
  1588. #define TCL_DTRACE_INST_START(a0, a1, a2) {}
  1589. #define TCL_DTRACE_INST_DONE(a0, a1, a2) {}
  1590. #define TCL_DTRACE_TCL_PROBE_ENABLED() 0
  1591. #define TCL_DTRACE_TCL_PROBE(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {}
  1592. #define TclDTraceInfo(info, args, argsi) {*args = ""; *argsi = 0;}
  1593. #endif /* USE_DTRACE */
  1594. #else /* TCL_DTRACE_DEBUG */
  1595. #define USE_DTRACE 1
  1596. #if !defined(TCL_DTRACE_DEBUG_LOG_ENABLED) || !(TCL_DTRACE_DEBUG_LOG_ENABLED)
  1597. #undef TCL_DTRACE_DEBUG_LOG_ENABLED
  1598. #define TCL_DTRACE_DEBUG_LOG_ENABLED 0
  1599. #endif
  1600. #if !defined(TCL_DTRACE_DEBUG_INST_PROBES) || !(TCL_DTRACE_DEBUG_INST_PROBES)
  1601. #undef TCL_DTRACE_DEBUG_INST_PROBES
  1602. #define TCL_DTRACE_DEBUG_INST_PROBES 0
  1603. #endif
  1604. MODULE_SCOPE int tclDTraceDebugEnabled, tclDTraceDebugIndent;
  1605. MODULE_SCOPE FILE *tclDTraceDebugLog;
  1606. MODULE_SCOPE void TclDTraceOpenDebugLog(void);
  1607. MODULE_SCOPE void TclDTraceInfo(Tcl_Obj *info, const char **args, int *argsi);
  1608. #define TCL_DTRACE_DEBUG_LOG() \
  1609. int tclDTraceDebugEnabled = TCL_DTRACE_DEBUG_LOG_ENABLED; \
  1610. int tclDTraceDebugIndent = 0; \
  1611. FILE *tclDTraceDebugLog = NULL; \
  1612. void TclDTraceOpenDebugLog(void) { \
  1613. char n[35]; \
  1614. sprintf(n, "/tmp/tclDTraceDebug-%lu.log", \
  1615. (unsigned long) getpid()); \
  1616. tclDTraceDebugLog = fopen(n, "a"); \
  1617. }
  1618. #define TclDTraceDbgMsg(p, m, ...) \
  1619. do { \
  1620. if (tclDTraceDebugEnabled) { \
  1621. int _l, _t = 0; \
  1622. if (!tclDTraceDebugLog) { TclDTraceOpenDebugLog(); } \
  1623. fprintf(tclDTraceDebugLog, "%.12s:%.4d:%n", \
  1624. strrchr(__FILE__, '/')+1, __LINE__, &_l); _t += _l; \
  1625. fprintf(tclDTraceDebugLog, " %.*s():%n", \
  1626. (_t < 18 ? 18 - _t : 0) + 18, __func__, &_l); _t += _l; \
  1627. fprintf(tclDTraceDebugLog, "%*s" p "%n", \
  1628. (_t < 40 ? 40 - _t : 0) + 2 * tclDTraceDebugIndent, \
  1629. "", &_l); _t += _l; \
  1630. fprintf(tclDTraceDebugLog, "%*s" m "\n", \
  1631. (_t < 64 ? 64 - _t : 1), "", ##__VA_ARGS__); \
  1632. fflush(tclDTraceDebugLog); \
  1633. } \
  1634. } while (0)
  1635. #define TCL_DTRACE_PROC_ENTRY_ENABLED() 1
  1636. #define TCL_DTRACE_PROC_RETURN_ENABLED() 1
  1637. #define TCL_DTRACE_PROC_RESULT_ENABLED() 1
  1638. #define TCL_DTRACE_PROC_ARGS_ENABLED() 1
  1639. #define TCL_DTRACE_PROC_INFO_ENABLED() 1
  1640. #define TCL_DTRACE_PROC_ENTRY(a0, a1, a2) \
  1641. tclDTraceDebugIndent++; \
  1642. TclDTraceDbgMsg("-> proc-entry", "%s %d %p", a0, a1, a2)
  1643. #define TCL_DTRACE_PROC_RETURN(a0, a1) \
  1644. TclDTraceDbgMsg("<- proc-return", "%s %d", a0, a1); \
  1645. tclDTraceDebugIndent--
  1646. #define TCL_DTRACE_PROC_RESULT(a0, a1, a2, a3) \
  1647. TclDTraceDbgMsg(" | proc-result", "%s %d %s %p", a0, a1, a2, a3)
  1648. #define TCL_DTRACE_PROC_ARGS(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) \
  1649. TclDTraceDbgMsg(" | proc-args", "%s %s %s %s %s %s %s %s %s %s", a0, \
  1650. a1, a2, a3, a4, a5, a6, a7, a8, a9)
  1651. #define TCL_DTRACE_PROC_INFO(a0, a1, a2, a3, a4, a5, a6, a7) \
  1652. TclDTraceDbgMsg(" | proc-info", "%s %s %s %s %d %d %s %s", a0, a1, \
  1653. a2, a3, a4, a5, a6, a7)
  1654. #define TCL_DTRACE_CMD_ENTRY_ENABLED() 1
  1655. #define TCL_DTRACE_CMD_RETURN_ENABLED() 1
  1656. #define TCL_DTRACE_CMD_RESULT_ENABLED() 1
  1657. #define TCL_DTRACE_CMD_ARGS_ENABLED() 1
  1658. #define TCL_DTRACE_CMD_INFO_ENABLED() 1
  1659. #define TCL_DTRACE_CMD_ENTRY(a0, a1, a2) \
  1660. tclDTraceDebugIndent++; \
  1661. TclDTraceDbgMsg("-> cmd-entry", "%s %d %p", a0, a1, a2)
  1662. #define TCL_DTRACE_CMD_RETURN(a0, a1) \
  1663. TclDTraceDbgMsg("<- cmd-return", "%s %d", a0, a1); \
  1664. tclDTraceDebugIndent--
  1665. #define TCL_DTRACE_CMD_RESULT(a0, a1, a2, a3) \
  1666. TclDTraceDbgMsg(" | cmd-result", "%s %d %s %p", a0, a1, a2, a3)
  1667. #define TCL_DTRACE_CMD_ARGS(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) \
  1668. TclDTraceDbgMsg(" | cmd-args", "%s %s %s %s %s %s %s %s %s %s", a0, \
  1669. a1, a2, a3, a4, a5, a6, a7, a8, a9)
  1670. #define TCL_DTRACE_CMD_INFO(a0, a1, a2, a3, a4, a5, a6, a7) \
  1671. TclDTraceDbgMsg(" | cmd-info", "%s %s %s %s %d %d %s %s", a0, a1, \
  1672. a2, a3, a4, a5, a6, a7)
  1673. #define TCL_DTRACE_INST_START_ENABLED() TCL_DTRACE_DEBUG_INST_PROBES
  1674. #define TCL_DTRACE_INST_DONE_ENABLED() TCL_DTRACE_DEBUG_INST_PROBES
  1675. #define TCL_DTRACE_INST_START(a0, a1, a2) \
  1676. TclDTraceDbgMsg(" | inst-start", "%s %d %p", a0, a1, a2)
  1677. #define TCL_DTRACE_INST_DONE(a0, a1, a2) \
  1678. TclDTraceDbgMsg(" | inst-end", "%s %d %p", a0, a1, a2)
  1679. #define TCL_DTRACE_TCL_PROBE_ENABLED() 1
  1680. #define TCL_DTRACE_TCL_PROBE(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) \
  1681. do { \
  1682. tclDTraceDebugEnabled = 1; \
  1683. TclDTraceDbgMsg(" | tcl-probe", "%s %s %s %s %s %s %s %s %s %s", a0, \
  1684. a1, a2, a3, a4, a5, a6, a7, a8, a9); \
  1685. } while (0)
  1686. #endif /* TCL_DTRACE_DEBUG */
  1687. #endif /* _TCLCOMPILATION */
  1688. /*
  1689. * Local Variables:
  1690. * mode: c
  1691. * c-basic-offset: 4
  1692. * fill-column: 78
  1693. * End:
  1694. */