Debian Codegraph @

generic/tclDisassemble.c

raw
1 /*
2 * tclDisassemble.c --
3 *
4 * This file contains procedures that disassemble bytecode into either
5 * human-readable or Tcl-processable forms.
6 *
7 * Copyright (c) 1996-1998 Sun Microsystems, Inc.
8 * Copyright (c) 2001 Kevin B. Kenny. All rights reserved.
9 * Copyright (c) 2013-2016 Donal K. Fellows.
10 *
11 * See the file "license.terms" for information on usage and redistribution of
12 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 */
14
15 #include "tclInt.h"
16 #include "tclCompile.h"
17 #include "tclOOInt.h"
18 #include <assert.h>
19
20 /*
21 * Prototypes for procedures defined later in this file:
22 */
23
24 static Tcl_Obj * DisassembleByteCodeAsDicts(Tcl_Obj *objPtr);
25 static Tcl_Obj * DisassembleByteCodeObj(Tcl_Obj *objPtr);
26 static int FormatInstruction(ByteCode *codePtr,
27 const unsigned char *pc, Tcl_Obj *bufferObj);
28 static void GetLocationInformation(Proc *procPtr,
29 Tcl_Obj **fileObjPtr, int *linePtr);
30 static void PrintSourceToObj(Tcl_Obj *appendObj,
31 const char *stringPtr, int maxChars);
32 static void UpdateStringOfInstName(Tcl_Obj *objPtr);
33
34 /*
35 * The structure below defines an instruction name Tcl object to allow
36 * reporting of inner contexts in errorstack without string allocation.
37 */
38
39 static const Tcl_ObjType tclInstNameType = {
40 "instname", /* name */
41 NULL, /* freeIntRepProc */
42 NULL, /* dupIntRepProc */
43 UpdateStringOfInstName, /* updateStringProc */
44 NULL, /* setFromAnyProc */
45 };
46
47 /*
48 * How to get the bytecode out of a Tcl_Obj.
49 */
50
51 #define BYTECODE(objPtr) \
52 ((ByteCode *) (objPtr)->internalRep.twoPtrValue.ptr1)
53
54 /*
55 *----------------------------------------------------------------------
56 *
57 * GetLocationInformation --
58 *
59 * This procedure looks up the information about where a procedure was
60 * originally declared.
61 *
62 * Results:
63 * Writes to the variables pointed at by fileObjPtr and linePtr.
64 *
65 * Side effects:
66 * None.
67 *
68 *----------------------------------------------------------------------
69 */
70
71 static void
72 GetLocationInformation(
73 Proc *procPtr, /* What to look up the information for. */
74 Tcl_Obj **fileObjPtr, /* Where to write the information about what
75 * file the code came from. Will be written
76 * to, either with the object (assume shared!)
77 * that describes what the file was, or with
78 * NULL if the information is not
79 * available. */
80 int *linePtr) /* Where to write the information about what
81 * line number represented the start of the
82 * code in question. Will be written to,
83 * either with the line number or with -1 if
84 * the information is not available. */
85 {
86 CmdFrame *cfPtr = TclGetCmdFrameForProcedure(procPtr);
87
88 *fileObjPtr = NULL;
89 *linePtr = -1;
90 if (cfPtr == NULL) {
91 return;
92 }
93
94 /*
95 * Get the source location data out of the CmdFrame.
96 */
97
98 *linePtr = cfPtr->line[0];
99 if (cfPtr->type == TCL_LOCATION_SOURCE) {
100 *fileObjPtr = cfPtr->data.eval.path;
101 }
102 }
103
104 #ifdef TCL_COMPILE_DEBUG
105 /*
106 *----------------------------------------------------------------------
107 *
108 * TclDebugPrintByteCodeObj --
109 *
110 * This procedure prints ("disassembles") the instructions of a bytecode
111 * object to stdout.
112 *
113 * Results:
114 * None.
115 *
116 * Side effects:
117 * None.
118 *
119 *----------------------------------------------------------------------
120 */
121
122 void
123 TclDebugPrintByteCodeObj(
124 Tcl_Obj *objPtr) /* The bytecode object to disassemble. */
125 {
126 if (tclTraceCompile == 2) {
127 Tcl_Obj *bufPtr = DisassembleByteCodeObj(objPtr);
128
129 fprintf(stdout, "\n%s", TclGetString(bufPtr));
130 Tcl_DecrRefCount(bufPtr);
131 fflush(stdout);
132 }
133 }
134
135 /*
136 *----------------------------------------------------------------------
137 *
138 * TclPrintInstruction --
139 *
140 * This procedure prints ("disassembles") one instruction from a bytecode
141 * object to stdout.
142 *
143 * Results:
144 * Returns the length in bytes of the current instruiction.
145 *
146 * Side effects:
147 * None.
148 *
149 *----------------------------------------------------------------------
150 */
151
152 int
153 TclPrintInstruction(
154 ByteCode *codePtr, /* Bytecode containing the instruction. */
155 const unsigned char *pc) /* Points to first byte of instruction. */
156 {
157 Tcl_Obj *bufferObj;
158 int numBytes;
159
160 TclNewObj(bufferObj);
161 numBytes = FormatInstruction(codePtr, pc, bufferObj);
162 fprintf(stdout, "%s", TclGetString(bufferObj));
163 Tcl_DecrRefCount(bufferObj);
164 return numBytes;
165 }
166
167 /*
168 *----------------------------------------------------------------------
169 *
170 * TclPrintObject --
171 *
172 * This procedure prints up to a specified number of characters from the
173 * argument Tcl object's string representation to a specified file.
174 *
175 * Results:
176 * None.
177 *
178 * Side effects:
179 * Outputs characters to the specified file.
180 *
181 *----------------------------------------------------------------------
182 */
183
184 void
185 TclPrintObject(
186 FILE *outFile, /* The file to print the source to. */
187 Tcl_Obj *objPtr, /* Points to the Tcl object whose string
188 * representation should be printed. */
189 int maxChars) /* Maximum number of chars to print. */
190 {
191 char *bytes;
192 int length;
193
194 bytes = TclGetStringFromObj(objPtr, &length);
195 TclPrintSource(outFile, bytes, TclMin(length, maxChars));
196 }
197
198 /*
199 *----------------------------------------------------------------------
200 *
201 * TclPrintSource --
202 *
203 * This procedure prints up to a specified number of characters from the
204 * argument string to a specified file. It tries to produce legible
205 * output by adding backslashes as necessary.
206 *
207 * Results:
208 * None.
209 *
210 * Side effects:
211 * Outputs characters to the specified file.
212 *
213 *----------------------------------------------------------------------
214 */
215
216 void
217 TclPrintSource(
218 FILE *outFile, /* The file to print the source to. */
219 const char *stringPtr, /* The string to print. */
220 int maxChars) /* Maximum number of chars to print. */
221 {
222 Tcl_Obj *bufferObj;
223
224 TclNewObj(bufferObj);
225 PrintSourceToObj(bufferObj, stringPtr, maxChars);
226 fprintf(outFile, "%s", TclGetString(bufferObj));
227 Tcl_DecrRefCount(bufferObj);
228 }
229 #endif /* TCL_COMPILE_DEBUG */
230
231 /*
232 *----------------------------------------------------------------------
233 *
234 * DisassembleByteCodeObj --
235 *
236 * Given an object which is of bytecode type, return a disassembled
237 * version of the bytecode (in a new refcount 0 object). No guarantees
238 * are made about the details of the contents of the result.
239 *
240 *----------------------------------------------------------------------
241 */
242
243 static Tcl_Obj *
244 DisassembleByteCodeObj(
245 Tcl_Obj *objPtr) /* The bytecode object to disassemble. */
246 {
247 ByteCode *codePtr = BYTECODE(objPtr);
248 unsigned char *codeStart, *codeLimit, *pc;
249 unsigned char *codeDeltaNext, *codeLengthNext;
250 unsigned char *srcDeltaNext, *srcLengthNext;
251 int codeOffset, codeLen, srcOffset, srcLen, numCmds, delta, line;
252 int i;
253 Interp *iPtr = (Interp *) *codePtr->interpHandle;
254 Tcl_Obj *bufferObj, *fileObj;
255 char ptrBuf1[20], ptrBuf2[20];
256
257 TclNewObj(bufferObj);
258 if (codePtr->refCount <= 0) {
259 return bufferObj; /* Already freed. */
260 }
261
262 codeStart = codePtr->codeStart;
263 codeLimit = codeStart + codePtr->numCodeBytes;
264 numCmds = codePtr->numCommands;
265
266 /*
267 * Print header lines describing the ByteCode.
268 */
269
270 snprintf(ptrBuf1, sizeof(ptrBuf1), "%p", codePtr);
271 snprintf(ptrBuf2, sizeof(ptrBuf1), "%p", iPtr);
272 Tcl_AppendPrintfToObj(bufferObj,
273 "ByteCode 0x%s, refCt %u, epoch %u, interp 0x%s (epoch %u)\n",
274 ptrBuf1, codePtr->refCount, codePtr->compileEpoch, ptrBuf2,
275 iPtr->compileEpoch);
276 Tcl_AppendToObj(bufferObj, " Source ", -1);
277 PrintSourceToObj(bufferObj, codePtr->source,
278 TclMin(codePtr->numSrcBytes, 55));
279 GetLocationInformation(codePtr->procPtr, &fileObj, &line);
280 if (line >= 0 && fileObj != NULL) {
281 Tcl_AppendPrintfToObj(bufferObj, "\n File \"%s\" Line %d",
282 TclGetString(fileObj), line);
283 }
284 Tcl_AppendPrintfToObj(bufferObj,
285 "\n Cmds %d, src %d, inst %d, litObjs %u, aux %d, stkDepth %u, code/src %.2f\n",
286 numCmds, codePtr->numSrcBytes, codePtr->numCodeBytes,
287 codePtr->numLitObjects, codePtr->numAuxDataItems,
288 codePtr->maxStackDepth,
289 #ifdef TCL_COMPILE_STATS
290 codePtr->numSrcBytes?
291 codePtr->structureSize/(float)codePtr->numSrcBytes :
292 #endif
293 0.0);
294
295 #ifdef TCL_COMPILE_STATS
296 Tcl_AppendPrintfToObj(bufferObj,
297 " Code %lu = header %lu+inst %d+litObj %lu+exc %lu+aux %lu+cmdMap %d\n",
298 (unsigned long) codePtr->structureSize,
299 (unsigned long) (TclOffset(ByteCode, localCachePtr)),
300 codePtr->numCodeBytes,
301 (unsigned long) (codePtr->numLitObjects * sizeof(Tcl_Obj *)),
302 (unsigned long) (codePtr->numExceptRanges*sizeof(ExceptionRange)),
303 (unsigned long) (codePtr->numAuxDataItems * sizeof(AuxData)),
304 codePtr->numCmdLocBytes);
305 #endif /* TCL_COMPILE_STATS */
306
307 /*
308 * If the ByteCode is the compiled body of a Tcl procedure, print
309 * information about that procedure. Note that we don't know the
310 * procedure's name since ByteCode's can be shared among procedures.
311 */
312
313 if (codePtr->procPtr != NULL) {
314 Proc *procPtr = codePtr->procPtr;
315 int numCompiledLocals = procPtr->numCompiledLocals;
316
317 snprintf(ptrBuf1, sizeof(ptrBuf1), "%p", procPtr);
318 Tcl_AppendPrintfToObj(bufferObj,
319 " Proc 0x%s, refCt %d, args %d, compiled locals %d\n",
320 ptrBuf1, procPtr->refCount, procPtr->numArgs,
321 numCompiledLocals);
322 if (numCompiledLocals > 0) {
323 CompiledLocal *localPtr = procPtr->firstLocalPtr;
324
325 for (i = 0; i < numCompiledLocals; i++) {
326 Tcl_AppendPrintfToObj(bufferObj,
327 " slot %d%s%s%s%s%s%s", i,
328 (localPtr->flags & (VAR_ARRAY|VAR_LINK)) ? "" : ", scalar",
329 (localPtr->flags & VAR_ARRAY) ? ", array" : "",
330 (localPtr->flags & VAR_LINK) ? ", link" : "",
331 (localPtr->flags & VAR_ARGUMENT) ? ", arg" : "",
332 (localPtr->flags & VAR_TEMPORARY) ? ", temp" : "",
333 (localPtr->flags & VAR_RESOLVED) ? ", resolved" : "");
334 if (TclIsVarTemporary(localPtr)) {
335 Tcl_AppendToObj(bufferObj, "\n", -1);
336 } else {
337 Tcl_AppendPrintfToObj(bufferObj, ", \"%s\"\n",
338 localPtr->name);
339 }
340 localPtr = localPtr->nextPtr;
341 }
342 }
343 }
344
345 /*
346 * Print the ExceptionRange array.
347 */
348
349 if (codePtr->numExceptRanges > 0) {
350 Tcl_AppendPrintfToObj(bufferObj, " Exception ranges %d, depth %d:\n",
351 codePtr->numExceptRanges, codePtr->maxExceptDepth);
352 for (i = 0; i < codePtr->numExceptRanges; i++) {
353 ExceptionRange *rangePtr = &codePtr->exceptArrayPtr[i];
354
355 Tcl_AppendPrintfToObj(bufferObj,
356 " %d: level %d, %s, pc %d-%d, ",
357 i, rangePtr->nestingLevel,
358 (rangePtr->type==LOOP_EXCEPTION_RANGE ? "loop" : "catch"),
359 rangePtr->codeOffset,
360 (rangePtr->codeOffset + rangePtr->numCodeBytes - 1));
361 switch (rangePtr->type) {
362 case LOOP_EXCEPTION_RANGE:
363 Tcl_AppendPrintfToObj(bufferObj, "continue %d, break %d\n",
364 rangePtr->continueOffset, rangePtr->breakOffset);
365 break;
366 case CATCH_EXCEPTION_RANGE:
367 Tcl_AppendPrintfToObj(bufferObj, "catch %d\n",
368 rangePtr->catchOffset);
369 break;
370 default:
371 Tcl_Panic("DisassembleByteCodeObj: bad ExceptionRange type %d",
372 rangePtr->type);
373 }
374 }
375 }
376
377 /*
378 * If there were no commands (e.g., an expression or an empty string was
379 * compiled), just print all instructions and return.
380 */
381
382 if (numCmds == 0) {
383 pc = codeStart;
384 while (pc < codeLimit) {
385 Tcl_AppendToObj(bufferObj, " ", -1);
386 pc += FormatInstruction(codePtr, pc, bufferObj);
387 }
388 return bufferObj;
389 }
390
391 /*
392 * Print table showing the code offset, source offset, and source length
393 * for each command. These are encoded as a sequence of bytes.
394 */
395
396 Tcl_AppendPrintfToObj(bufferObj, " Commands %d:", numCmds);
397 codeDeltaNext = codePtr->codeDeltaStart;
398 codeLengthNext = codePtr->codeLengthStart;
399 srcDeltaNext = codePtr->srcDeltaStart;
400 srcLengthNext = codePtr->srcLengthStart;
401 codeOffset = srcOffset = 0;
402 for (i = 0; i < numCmds; i++) {
403 if ((unsigned) *codeDeltaNext == (unsigned) 0xFF) {
404 codeDeltaNext++;
405 delta = TclGetInt4AtPtr(codeDeltaNext);
406 codeDeltaNext += 4;
407 } else {
408 delta = TclGetInt1AtPtr(codeDeltaNext);
409 codeDeltaNext++;
410 }
411 codeOffset += delta;
412
413 if ((unsigned) *codeLengthNext == (unsigned) 0xFF) {
414 codeLengthNext++;
415 codeLen = TclGetInt4AtPtr(codeLengthNext);
416 codeLengthNext += 4;
417 } else {
418 codeLen = TclGetInt1AtPtr(codeLengthNext);
419 codeLengthNext++;
420 }
421
422 if ((unsigned) *srcDeltaNext == (unsigned) 0xFF) {
423 srcDeltaNext++;
424 delta = TclGetInt4AtPtr(srcDeltaNext);
425 srcDeltaNext += 4;
426 } else {
427 delta = TclGetInt1AtPtr(srcDeltaNext);
428 srcDeltaNext++;
429 }
430 srcOffset += delta;
431
432 if ((unsigned) *srcLengthNext == (unsigned) 0xFF) {
433 srcLengthNext++;
434 srcLen = TclGetInt4AtPtr(srcLengthNext);
435 srcLengthNext += 4;
436 } else {
437 srcLen = TclGetInt1AtPtr(srcLengthNext);
438 srcLengthNext++;
439 }
440
441 Tcl_AppendPrintfToObj(bufferObj, "%s%4d: pc %d-%d, src %d-%d",
442 ((i % 2)? " " : "\n "),
443 (i+1), codeOffset, (codeOffset + codeLen - 1),
444 srcOffset, (srcOffset + srcLen - 1));
445 }
446 if (numCmds > 0) {
447 Tcl_AppendToObj(bufferObj, "\n", -1);
448 }
449
450 /*
451 * Print each instruction. If the instruction corresponds to the start of
452 * a command, print the command's source. Note that we don't need the code
453 * length here.
454 */
455
456 codeDeltaNext = codePtr->codeDeltaStart;
457 srcDeltaNext = codePtr->srcDeltaStart;
458 srcLengthNext = codePtr->srcLengthStart;
459 codeOffset = srcOffset = 0;
460 pc = codeStart;
461 for (i = 0; i < numCmds; i++) {
462 if ((unsigned) *codeDeltaNext == (unsigned) 0xFF) {
463 codeDeltaNext++;
464 delta = TclGetInt4AtPtr(codeDeltaNext);
465 codeDeltaNext += 4;
466 } else {
467 delta = TclGetInt1AtPtr(codeDeltaNext);
468 codeDeltaNext++;
469 }
470 codeOffset += delta;
471
472 if ((unsigned) *srcDeltaNext == (unsigned) 0xFF) {
473 srcDeltaNext++;
474 delta = TclGetInt4AtPtr(srcDeltaNext);
475 srcDeltaNext += 4;
476 } else {
477 delta = TclGetInt1AtPtr(srcDeltaNext);
478 srcDeltaNext++;
479 }
480 srcOffset += delta;
481
482 if ((unsigned) *srcLengthNext == (unsigned) 0xFF) {
483 srcLengthNext++;
484 srcLen = TclGetInt4AtPtr(srcLengthNext);
485 srcLengthNext += 4;
486 } else {
487 srcLen = TclGetInt1AtPtr(srcLengthNext);
488 srcLengthNext++;
489 }
490
491 /*
492 * Print instructions before command i.
493 */
494
495 while ((pc-codeStart) < codeOffset) {
496 Tcl_AppendToObj(bufferObj, " ", -1);
497 pc += FormatInstruction(codePtr, pc, bufferObj);
498 }
499
500 Tcl_AppendPrintfToObj(bufferObj, " Command %d: ", i+1);
501 PrintSourceToObj(bufferObj, (codePtr->source + srcOffset),
502 TclMin(srcLen, 55));
503 Tcl_AppendToObj(bufferObj, "\n", -1);
504 }
505 if (pc < codeLimit) {
506 /*
507 * Print instructions after the last command.
508 */
509
510 while (pc < codeLimit) {
511 Tcl_AppendToObj(bufferObj, " ", -1);
512 pc += FormatInstruction(codePtr, pc, bufferObj);
513 }
514 }
515 return bufferObj;
516 }
517
518 /*
519 *----------------------------------------------------------------------
520 *
521 * FormatInstruction --
522 *
523 * Appends a representation of a bytecode instruction to a Tcl_Obj.
524 *
525 *----------------------------------------------------------------------
526 */
527
528 static int
529 FormatInstruction(
530 ByteCode *codePtr, /* Bytecode containing the instruction. */
531 const unsigned char *pc, /* Points to first byte of instruction. */
532 Tcl_Obj *bufferObj) /* Object to append instruction info to. */
533 {
534 Proc *procPtr = codePtr->procPtr;
535 unsigned char opCode = *pc;
536 const InstructionDesc *instDesc = &tclInstructionTable[opCode];
537 unsigned char *codeStart = codePtr->codeStart;
538 unsigned pcOffset = pc - codeStart;
539 int opnd = 0, i, j, numBytes = 1;
540 int localCt = procPtr ? procPtr->numCompiledLocals : 0;
541 CompiledLocal *localPtr = procPtr ? procPtr->firstLocalPtr : NULL;
542 char suffixBuffer[128]; /* Additional info to print after main opcode
543 * and immediates. */
544 char *suffixSrc = NULL;
545 Tcl_Obj *suffixObj = NULL;
546 AuxData *auxPtr = NULL;
547
548 suffixBuffer[0] = '\0';
549 Tcl_AppendPrintfToObj(bufferObj, "(%u) %s ", pcOffset, instDesc->name);
550 for (i = 0; i < instDesc->numOperands; i++) {
551 switch (instDesc->opTypes[i]) {
552 case OPERAND_INT1:
553 opnd = TclGetInt1AtPtr(pc+numBytes); numBytes++;
554 Tcl_AppendPrintfToObj(bufferObj, "%+d ", opnd);
555 break;
556 case OPERAND_INT4:
557 opnd = TclGetInt4AtPtr(pc+numBytes); numBytes += 4;
558 Tcl_AppendPrintfToObj(bufferObj, "%+d ", opnd);
559 break;
560 case OPERAND_UINT1:
561 opnd = TclGetUInt1AtPtr(pc+numBytes); numBytes++;
562 Tcl_AppendPrintfToObj(bufferObj, "%u ", (unsigned) opnd);
563 break;
564 case OPERAND_UINT4:
565 opnd = TclGetUInt4AtPtr(pc+numBytes); numBytes += 4;
566 if (opCode == INST_START_CMD) {
567 snprintf(suffixBuffer+strlen(suffixBuffer), sizeof(suffixBuffer) - strlen(suffixBuffer),
568 ", %u cmds start here", opnd);
569 }
570 Tcl_AppendPrintfToObj(bufferObj, "%u ", (unsigned) opnd);
571 break;
572 case OPERAND_OFFSET1:
573 opnd = TclGetInt1AtPtr(pc+numBytes); numBytes++;
574 snprintf(suffixBuffer, sizeof(suffixBuffer), "pc %u", pcOffset+opnd);
575 Tcl_AppendPrintfToObj(bufferObj, "%+d ", opnd);
576 break;
577 case OPERAND_OFFSET4:
578 opnd = TclGetInt4AtPtr(pc+numBytes); numBytes += 4;
579 if (opCode == INST_START_CMD) {
580 snprintf(suffixBuffer, sizeof(suffixBuffer), "next cmd at pc %u", pcOffset+opnd);
581 } else {
582 snprintf(suffixBuffer, sizeof(suffixBuffer), "pc %u", pcOffset+opnd);
583 }
584 Tcl_AppendPrintfToObj(bufferObj, "%+d ", opnd);
585 break;
586 case OPERAND_LIT1:
587 opnd = TclGetUInt1AtPtr(pc+numBytes); numBytes++;
588 suffixObj = codePtr->objArrayPtr[opnd];
589 Tcl_AppendPrintfToObj(bufferObj, "%u ", (unsigned) opnd);
590 break;
591 case OPERAND_LIT4:
592 opnd = TclGetUInt4AtPtr(pc+numBytes); numBytes += 4;
593 suffixObj = codePtr->objArrayPtr[opnd];
594 Tcl_AppendPrintfToObj(bufferObj, "%u ", (unsigned) opnd);
595 break;
596 case OPERAND_AUX4:
597 opnd = TclGetUInt4AtPtr(pc+numBytes); numBytes += 4;
598 Tcl_AppendPrintfToObj(bufferObj, "%u ", (unsigned) opnd);
599 auxPtr = &codePtr->auxDataArrayPtr[opnd];
600 break;
601 case OPERAND_IDX4:
602 opnd = TclGetInt4AtPtr(pc+numBytes); numBytes += 4;
603 if (opnd >= -1) {
604 Tcl_AppendPrintfToObj(bufferObj, "%d ", opnd);
605 } else if (opnd == -2) {
606 Tcl_AppendPrintfToObj(bufferObj, "end ");
607 } else {
608 Tcl_AppendPrintfToObj(bufferObj, "end-%d ", -2-opnd);
609 }
610 break;
611 case OPERAND_LVT1:
612 opnd = TclGetUInt1AtPtr(pc+numBytes);
613 numBytes++;
614 goto printLVTindex;
615 case OPERAND_LVT4:
616 opnd = TclGetUInt4AtPtr(pc+numBytes);
617 numBytes += 4;
618 printLVTindex:
619 if (localPtr != NULL) {
620 if (opnd >= localCt) {
621 Tcl_Panic("FormatInstruction: bad local var index %u (%u locals)",
622 (unsigned) opnd, localCt);
623 }
624 for (j = 0; j < opnd; j++) {
625 localPtr = localPtr->nextPtr;
626 }
627 if (TclIsVarTemporary(localPtr)) {
628 snprintf(suffixBuffer, sizeof(suffixBuffer), "temp var %u", (unsigned) opnd);
629 } else {
630 snprintf(suffixBuffer, sizeof(suffixBuffer), "var ");
631 suffixSrc = localPtr->name;
632 }
633 }
634 Tcl_AppendPrintfToObj(bufferObj, "%%v%u ", (unsigned) opnd);
635 break;
636 case OPERAND_SCLS1:
637 opnd = TclGetUInt1AtPtr(pc+numBytes); numBytes++;
638 Tcl_AppendPrintfToObj(bufferObj, "%s ",
639 tclStringClassTable[opnd].name);
640 break;
641 case OPERAND_NONE:
642 default:
643 break;
644 }
645 }
646 if (suffixObj) {
647 const char *bytes;
648 int length;
649
650 Tcl_AppendToObj(bufferObj, "\t# ", -1);
651 bytes = TclGetStringFromObj(codePtr->objArrayPtr[opnd], &length);
652 PrintSourceToObj(bufferObj, bytes, TclMin(length, 40));
653 } else if (suffixBuffer[0]) {
654 Tcl_AppendPrintfToObj(bufferObj, "\t# %s", suffixBuffer);
655 if (suffixSrc) {
656 PrintSourceToObj(bufferObj, suffixSrc, 40);
657 }
658 }
659 Tcl_AppendToObj(bufferObj, "\n", -1);
660 if (auxPtr && auxPtr->type->printProc) {
661 Tcl_AppendToObj(bufferObj, "\t\t[", -1);
662 auxPtr->type->printProc(auxPtr->clientData, bufferObj, codePtr,
663 pcOffset);
664 Tcl_AppendToObj(bufferObj, "]\n", -1);
665 }
666 return numBytes;
667 }
668
669 /*
670 *----------------------------------------------------------------------
671 *
672 * TclGetInnerContext --
673 *
674 * If possible, returns a list capturing the inner context. Otherwise
675 * return NULL.
676 *
677 *----------------------------------------------------------------------
678 */
679
680 Tcl_Obj *
681 TclGetInnerContext(
682 Tcl_Interp *interp,
683 const unsigned char *pc,
684 Tcl_Obj **tosPtr)
685 {
686 int objc = 0;
687 Tcl_Obj *result;
688 Interp *iPtr = (Interp *) interp;
689
690 switch (*pc) {
691 case INST_STR_LEN:
692 case INST_LNOT:
693 case INST_BITNOT:
694 case INST_UMINUS:
695 case INST_UPLUS:
696 case INST_TRY_CVT_TO_NUMERIC:
697 case INST_EXPAND_STKTOP:
698 case INST_EXPR_STK:
699 objc = 1;
700 break;
701
702 case INST_LIST_IN:
703 case INST_LIST_NOT_IN: /* Basic list containment operators. */
704 case INST_STR_EQ:
705 case INST_STR_NEQ: /* String (in)equality check */
706 case INST_STR_CMP: /* String compare. */
707 case INST_STR_INDEX:
708 case INST_STR_MATCH:
709 case INST_REGEXP:
710 case INST_EQ:
711 case INST_NEQ:
712 case INST_LT:
713 case INST_GT:
714 case INST_LE:
715 case INST_GE:
716 case INST_MOD:
717 case INST_LSHIFT:
718 case INST_RSHIFT:
719 case INST_BITOR:
720 case INST_BITXOR:
721 case INST_BITAND:
722 case INST_EXPON:
723 case INST_ADD:
724 case INST_SUB:
725 case INST_DIV:
726 case INST_MULT:
727 objc = 2;
728 break;
729
730 case INST_RETURN_STK:
731 /* early pop. TODO: dig out opt dict too :/ */
732 objc = 1;
733 break;
734
735 case INST_SYNTAX:
736 case INST_RETURN_IMM:
737 objc = 2;
738 break;
739
740 case INST_INVOKE_STK4:
741 objc = TclGetUInt4AtPtr(pc+1);
742 break;
743
744 case INST_INVOKE_STK1:
745 objc = TclGetUInt1AtPtr(pc+1);
746 break;
747 }
748
749 result = iPtr->innerContext;
750 if (Tcl_IsShared(result)) {
751 Tcl_DecrRefCount(result);
752 iPtr->innerContext = result = Tcl_NewListObj(objc + 1, NULL);
753 Tcl_IncrRefCount(result);
754 } else {
755 int len;
756
757 /*
758 * Reset while keeping the list internalrep as much as possible.
759 */
760
761 TclListObjLength(interp, result, &len);
762 Tcl_ListObjReplace(interp, result, 0, len, 0, NULL);
763 }
764 Tcl_ListObjAppendElement(NULL, result, TclNewInstNameObj(*pc));
765
766 for (; objc>0 ; objc--) {
767 Tcl_Obj *objPtr;
768
769 objPtr = tosPtr[1 - objc];
770 if (!objPtr) {
771 Tcl_Panic("InnerContext: bad tos -- appending null object");
772 }
773 if ((objPtr->refCount<=0)
774 #ifdef TCL_MEM_DEBUG
775 || (objPtr->refCount==0x61616161)
776 #endif
777 ) {
778 Tcl_Panic("InnerContext: bad tos -- appending freed object %p",
779 objPtr);
780 }
781 Tcl_ListObjAppendElement(NULL, result, objPtr);
782 }
783
784 return result;
785 }
786
787 /*
788 *----------------------------------------------------------------------
789 *
790 * TclNewInstNameObj --
791 *
792 * Creates a new InstName Tcl_Obj based on the given instruction
793 *
794 *----------------------------------------------------------------------
795 */
796
797 Tcl_Obj *
798 TclNewInstNameObj(
799 unsigned char inst)
800 {
801 Tcl_Obj *objPtr;
802
803 TclNewObj(objPtr);
804 objPtr->typePtr = &tclInstNameType;
805 objPtr->internalRep.longValue = (long) inst;
806 objPtr->bytes = NULL;
807
808 return objPtr;
809 }
810
811 /*
812 *----------------------------------------------------------------------
813 *
814 * UpdateStringOfInstName --
815 *
816 * Update the string representation for an instruction name object.
817 *
818 *----------------------------------------------------------------------
819 */
820
821 static void
822 UpdateStringOfInstName(
823 Tcl_Obj *objPtr)
824 {
825 int inst = objPtr->internalRep.longValue;
826 char *s, buf[20];
827 int len;
828
829 if ((inst < 0) || (inst > LAST_INST_OPCODE)) {
830 snprintf(buf, sizeof(buf), "inst_%d", inst);
831 s = buf;
832 } else {
833 s = (char *) tclInstructionTable[objPtr->internalRep.longValue].name;
834 }
835 len = strlen(s);
836 objPtr->bytes = (char *)ckalloc(len + 1);
837 memcpy(objPtr->bytes, s, len + 1);
838 objPtr->length = len;
839 }
840
841 /*
842 *----------------------------------------------------------------------
843 *
844 * PrintSourceToObj --
845 *
846 * Appends a quoted representation of a string to a Tcl_Obj.
847 *
848 *----------------------------------------------------------------------
849 */
850
851 static void
852 PrintSourceToObj(
853 Tcl_Obj *appendObj, /* The object to print the source to. */
854 const char *stringPtr, /* The string to print. */
855 int maxChars) /* Maximum number of chars to print. */
856 {
857 const char *p;
858 int i = 0, len;
859
860 if (stringPtr == NULL) {
861 Tcl_AppendToObj(appendObj, "\"\"", -1);
862 return;
863 }
864
865 Tcl_AppendToObj(appendObj, "\"", -1);
866 p = stringPtr;
867 for (; (*p != '\0') && (i < maxChars); p+=len) {
868 int ucs4;
869
870 len = TclUtfToUCS4(p, &ucs4);
871 switch (ucs4) {
872 case '"':
873 Tcl_AppendToObj(appendObj, "\\\"", -1);
874 i += 2;
875 continue;
876 case '\f':
877 Tcl_AppendToObj(appendObj, "\\f", -1);
878 i += 2;
879 continue;
880 case '\n':
881 Tcl_AppendToObj(appendObj, "\\n", -1);
882 i += 2;
883 continue;
884 case '\r':
885 Tcl_AppendToObj(appendObj, "\\r", -1);
886 i += 2;
887 continue;
888 case '\t':
889 Tcl_AppendToObj(appendObj, "\\t", -1);
890 i += 2;
891 continue;
892 case '\v':
893 Tcl_AppendToObj(appendObj, "\\v", -1);
894 i += 2;
895 continue;
896 default:
897 if (ucs4 > 0xFFFF) {
898 Tcl_AppendPrintfToObj(appendObj, "\\U%08x", ucs4);
899 i += 10;
900 } else if (ucs4 < 0x20 || ucs4 >= 0x7F) {
901 Tcl_AppendPrintfToObj(appendObj, "\\u%04x", ucs4);
902 i += 6;
903 } else {
904 Tcl_AppendPrintfToObj(appendObj, "%c", ucs4);
905 i++;
906 }
907 continue;
908 }
909 }
910 if (*p != '\0') {
911 Tcl_AppendToObj(appendObj, "...", -1);
912 }
913 Tcl_AppendToObj(appendObj, "\"", -1);
914 }
915
916 /*
917 *----------------------------------------------------------------------
918 *
919 * DisassembleByteCodeAsDicts --
920 *
921 * Given an object which is of bytecode type, return a disassembled
922 * version of the bytecode (in a new refcount 0 object) in a dictionary.
923 * No guarantees are made about the details of the contents of the
924 * result, but it is intended to be more readable than the old output
925 * format.
926 *
927 *----------------------------------------------------------------------
928 */
929
930 static Tcl_Obj *
931 DisassembleByteCodeAsDicts(
932 Tcl_Obj *objPtr) /* The bytecode-holding value to take apart */
933 {
934 ByteCode *codePtr = BYTECODE(objPtr);
935 Tcl_Obj *description, *literals, *variables, *instructions, *inst;
936 Tcl_Obj *aux, *exn, *commands, *file;
937 unsigned char *pc, *opnd, *codeOffPtr, *codeLenPtr, *srcOffPtr, *srcLenPtr;
938 int codeOffset, codeLength, sourceOffset, sourceLength;
939 int i, val, line;
940
941 /*
942 * Get the literals from the bytecode.
943 */
944
945 TclNewObj(literals);
946 for (i=0 ; i<codePtr->numLitObjects ; i++) {
947 Tcl_ListObjAppendElement(NULL, literals, codePtr->objArrayPtr[i]);
948 }
949
950 /*
951 * Get the variables from the bytecode.
952 */
953
954 TclNewObj(variables);
955 if (codePtr->procPtr) {
956 int localCount = codePtr->procPtr->numCompiledLocals;
957 CompiledLocal *localPtr = codePtr->procPtr->firstLocalPtr;
958
959 for (i=0 ; i<localCount ; i++,localPtr=localPtr->nextPtr) {
960 Tcl_Obj *descriptor[2];
961
962 TclNewObj(descriptor[0]);
963 if (!(localPtr->flags & (VAR_ARRAY|VAR_LINK))) {
964 Tcl_ListObjAppendElement(NULL, descriptor[0],
965 Tcl_NewStringObj("scalar", -1));
966 }
967 if (localPtr->flags & VAR_ARRAY) {
968 Tcl_ListObjAppendElement(NULL, descriptor[0],
969 Tcl_NewStringObj("array", -1));
970 }
971 if (localPtr->flags & VAR_LINK) {
972 Tcl_ListObjAppendElement(NULL, descriptor[0],
973 Tcl_NewStringObj("link", -1));
974 }
975 if (localPtr->flags & VAR_ARGUMENT) {
976 Tcl_ListObjAppendElement(NULL, descriptor[0],
977 Tcl_NewStringObj("arg", -1));
978 }
979 if (localPtr->flags & VAR_TEMPORARY) {
980 Tcl_ListObjAppendElement(NULL, descriptor[0],
981 Tcl_NewStringObj("temp", -1));
982 }
983 if (localPtr->flags & VAR_RESOLVED) {
984 Tcl_ListObjAppendElement(NULL, descriptor[0],
985 Tcl_NewStringObj("resolved", -1));
986 }
987 if (localPtr->flags & VAR_TEMPORARY) {
988 Tcl_ListObjAppendElement(NULL, variables,
989 Tcl_NewListObj(1, descriptor));
990 } else {
991 descriptor[1] = Tcl_NewStringObj(localPtr->name, -1);
992 Tcl_ListObjAppendElement(NULL, variables,
993 Tcl_NewListObj(2, descriptor));
994 }
995 }
996 }
997
998 /*
999 * Get the instructions from the bytecode.
1000 */
1001
1002 TclNewObj(instructions);
1003 for (pc=codePtr->codeStart; pc<codePtr->codeStart+codePtr->numCodeBytes;){
1004 const InstructionDesc *instDesc = &tclInstructionTable[*pc];
1005 int address = pc - codePtr->codeStart;
1006
1007 TclNewObj(inst);
1008 Tcl_ListObjAppendElement(NULL, inst, Tcl_NewStringObj(
1009 instDesc->name, -1));
1010 opnd = pc + 1;
1011 for (i=0 ; i<instDesc->numOperands ; i++) {
1012 switch (instDesc->opTypes[i]) {
1013 case OPERAND_INT1:
1014 val = TclGetInt1AtPtr(opnd);
1015 opnd += 1;
1016 goto formatNumber;
1017 case OPERAND_UINT1:
1018 val = TclGetUInt1AtPtr(opnd);
1019 opnd += 1;
1020 goto formatNumber;
1021 case OPERAND_INT4:
1022 val = TclGetInt4AtPtr(opnd);
1023 opnd += 4;
1024 goto formatNumber;
1025 case OPERAND_UINT4:
1026 val = TclGetUInt4AtPtr(opnd);
1027 opnd += 4;
1028 formatNumber:
1029 Tcl_ListObjAppendElement(NULL, inst, Tcl_NewIntObj(val));
1030 break;
1031
1032 case OPERAND_OFFSET1:
1033 val = TclGetInt1AtPtr(opnd);
1034 opnd += 1;
1035 goto formatAddress;
1036 case OPERAND_OFFSET4:
1037 val = TclGetInt4AtPtr(opnd);
1038 opnd += 4;
1039 formatAddress:
1040 Tcl_ListObjAppendElement(NULL, inst, Tcl_ObjPrintf(
1041 "pc %d", address + val));
1042 break;
1043
1044 case OPERAND_LIT1:
1045 val = TclGetUInt1AtPtr(opnd);
1046 opnd += 1;
1047 goto formatLiteral;
1048 case OPERAND_LIT4:
1049 val = TclGetUInt4AtPtr(opnd);
1050 opnd += 4;
1051 formatLiteral:
1052 Tcl_ListObjAppendElement(NULL, inst, Tcl_ObjPrintf(
1053 "@%d", val));
1054 break;
1055
1056 case OPERAND_LVT1:
1057 val = TclGetUInt1AtPtr(opnd);
1058 opnd += 1;
1059 goto formatVariable;
1060 case OPERAND_LVT4:
1061 val = TclGetUInt4AtPtr(opnd);
1062 opnd += 4;
1063 formatVariable:
1064 Tcl_ListObjAppendElement(NULL, inst, Tcl_ObjPrintf(
1065 "%%%d", val));
1066 break;
1067 case OPERAND_IDX4:
1068 val = TclGetInt4AtPtr(opnd);
1069 opnd += 4;
1070 if (val >= -1) {
1071 Tcl_ListObjAppendElement(NULL, inst, Tcl_ObjPrintf(
1072 ".%d", val));
1073 } else if (val == -2) {
1074 Tcl_ListObjAppendElement(NULL, inst, Tcl_NewStringObj(
1075 ".end", -1));
1076 } else {
1077 Tcl_ListObjAppendElement(NULL, inst, Tcl_ObjPrintf(
1078 ".end-%d", -2-val));
1079 }
1080 break;
1081 case OPERAND_AUX4:
1082 val = TclGetInt4AtPtr(opnd);
1083 opnd += 4;
1084 Tcl_ListObjAppendElement(NULL, inst, Tcl_ObjPrintf(
1085 "?%d", val));
1086 break;
1087 case OPERAND_SCLS1:
1088 val = TclGetUInt1AtPtr(opnd);
1089 opnd++;
1090 Tcl_ListObjAppendElement(NULL, inst, Tcl_ObjPrintf(
1091 "=%s", tclStringClassTable[val].name));
1092 break;
1093 case OPERAND_NONE:
1094 Tcl_Panic("opcode %d with more than zero 'no' operands", *pc);
1095 }
1096 }
1097 Tcl_DictObjPut(NULL, instructions, Tcl_NewIntObj(address), inst);
1098 pc += instDesc->numBytes;
1099 }
1100
1101 /*
1102 * Get the auxiliary data from the bytecode.
1103 */
1104
1105 TclNewObj(aux);
1106 for (i=0 ; i<codePtr->numAuxDataItems ; i++) {
1107 AuxData *auxData = &codePtr->auxDataArrayPtr[i];
1108 Tcl_Obj *auxDesc = Tcl_NewStringObj(auxData->type->name, -1);
1109
1110 if (auxData->type->disassembleProc) {
1111 Tcl_Obj *desc;
1112
1113 TclNewObj(desc);
1114 TclDictPut(NULL, desc, "name", auxDesc);
1115 auxDesc = desc;
1116 auxData->type->disassembleProc(auxData->clientData, auxDesc,
1117 codePtr, 0);
1118 } else if (auxData->type->printProc) {
1119 Tcl_Obj *desc;
1120
1121 TclNewObj(desc);
1122 auxData->type->printProc(auxData->clientData, desc, codePtr, 0);
1123 Tcl_ListObjAppendElement(NULL, auxDesc, desc);
1124 }
1125 Tcl_ListObjAppendElement(NULL, aux, auxDesc);
1126 }
1127
1128 /*
1129 * Get the exception ranges from the bytecode.
1130 */
1131
1132 TclNewObj(exn);
1133 for (i=0 ; i<codePtr->numExceptRanges ; i++) {
1134 ExceptionRange *rangePtr = &codePtr->exceptArrayPtr[i];
1135
1136 switch (rangePtr->type) {
1137 case LOOP_EXCEPTION_RANGE:
1138 Tcl_ListObjAppendElement(NULL, exn, Tcl_ObjPrintf(
1139 "type %s level %d from %d to %d break %d continue %d",
1140 "loop", rangePtr->nestingLevel, rangePtr->codeOffset,
1141 rangePtr->codeOffset + rangePtr->numCodeBytes - 1,
1142 rangePtr->breakOffset, rangePtr->continueOffset));
1143 break;
1144 case CATCH_EXCEPTION_RANGE:
1145 Tcl_ListObjAppendElement(NULL, exn, Tcl_ObjPrintf(
1146 "type %s level %d from %d to %d catch %d",
1147 "catch", rangePtr->nestingLevel, rangePtr->codeOffset,
1148 rangePtr->codeOffset + rangePtr->numCodeBytes - 1,
1149 rangePtr->catchOffset));
1150 break;
1151 }
1152 }
1153
1154 /*
1155 * Get the command information from the bytecode.
1156 *
1157 * The way these are encoded in the bytecode is non-trivial; the Decode
1158 * macro (which updates its argument and returns the next decoded value)
1159 * handles this so that the rest of the code does not.
1160 */
1161
1162 #define Decode(ptr) \
1163 ((TclGetUInt1AtPtr(ptr) == 0xFF) \
1164 ? ((ptr)+=5 , TclGetInt4AtPtr((ptr)-4)) \
1165 : ((ptr)+=1 , TclGetInt1AtPtr((ptr)-1)))
1166
1167 TclNewObj(commands);
1168 codeOffPtr = codePtr->codeDeltaStart;
1169 codeLenPtr = codePtr->codeLengthStart;
1170 srcOffPtr = codePtr->srcDeltaStart;
1171 srcLenPtr = codePtr->srcLengthStart;
1172 codeOffset = sourceOffset = 0;
1173 for (i=0 ; i<codePtr->numCommands ; i++) {
1174 Tcl_Obj *cmd;
1175
1176 codeOffset += Decode(codeOffPtr);
1177 codeLength = Decode(codeLenPtr);
1178 sourceOffset += Decode(srcOffPtr);
1179 sourceLength = Decode(srcLenPtr);
1180 TclNewObj(cmd);
1181 TclDictPut(NULL, cmd, "codefrom", Tcl_NewIntObj(codeOffset));
1182 TclDictPut(NULL, cmd, "codeto", Tcl_NewIntObj(
1183 codeOffset + codeLength - 1));
1184
1185 /*
1186 * Convert byte offsets to character offsets; important if multibyte
1187 * characters are present in the source!
1188 */
1189
1190 TclDictPut(NULL, cmd, "scriptfrom", Tcl_NewIntObj(
1191 Tcl_NumUtfChars(codePtr->source, sourceOffset)));
1192 TclDictPut(NULL, cmd, "scriptto", Tcl_NewIntObj(
1193 Tcl_NumUtfChars(codePtr->source,
1194 sourceOffset + sourceLength - 1)));
1195 TclDictPut(NULL, cmd, "script",
1196 Tcl_NewStringObj(codePtr->source+sourceOffset, sourceLength));
1197 Tcl_ListObjAppendElement(NULL, commands, cmd);
1198 }
1199
1200 #undef Decode
1201
1202 /*
1203 * Get the source file and line number information from the CmdFrame
1204 * system if it is available.
1205 */
1206
1207 GetLocationInformation(codePtr->procPtr, &file, &line);
1208
1209 /*
1210 * Build the overall result.
1211 */
1212
1213 TclNewObj(description);
1214 TclDictPut(NULL, description, "literals", literals);
1215 TclDictPut(NULL, description, "variables", variables);
1216 TclDictPut(NULL, description, "exception", exn);
1217 TclDictPut(NULL, description, "instructions", instructions);
1218 TclDictPut(NULL, description, "auxiliary", aux);
1219 TclDictPut(NULL, description, "commands", commands);
1220 TclDictPut(NULL, description, "script",
1221 Tcl_NewStringObj(codePtr->source, codePtr->numSrcBytes));
1222 TclDictPut(NULL, description, "namespace",
1223 Tcl_NewStringObj(codePtr->nsPtr->fullName, -1));
1224 TclDictPut(NULL, description, "stackdepth",
1225 Tcl_NewIntObj(codePtr->maxStackDepth));
1226 TclDictPut(NULL, description, "exceptdepth",
1227 Tcl_NewIntObj(codePtr->maxExceptDepth));
1228 if (line > -1) {
1229 TclDictPut(NULL, description, "initiallinenumber",
1230 Tcl_NewIntObj(line));
1231 }
1232 if (file) {
1233 TclDictPut(NULL, description, "sourcefile", file);
1234 }
1235 return description;
1236 }
1237
1238 /*
1239 *----------------------------------------------------------------------
1240 *
1241 * Tcl_DisassembleObjCmd --
1242 *
1243 * Implementation of the "::tcl::unsupported::disassemble" command. This
1244 * command is not documented, but will disassemble procedures, lambda
1245 * terms and general scripts. Note that will compile terms if necessary
1246 * in order to disassemble them.
1247 *
1248 *----------------------------------------------------------------------
1249 */
1250
1251 int
1252 Tcl_DisassembleObjCmd(
1253 void *clientData, /* What type of operation. */
1254 Tcl_Interp *interp, /* Current interpreter. */
1255 int objc, /* Number of arguments. */
1256 Tcl_Obj *const objv[]) /* Argument objects. */
1257 {
1258 static const char *const types[] = {
1259 "constructor", "destructor",
1260 "lambda", "method", "objmethod", "proc", "script", NULL
1261 };
1262 enum Types {
1263 DISAS_CLASS_CONSTRUCTOR, DISAS_CLASS_DESTRUCTOR,
1264 DISAS_LAMBDA, DISAS_CLASS_METHOD, DISAS_OBJECT_METHOD, DISAS_PROC,
1265 DISAS_SCRIPT
1266 };
1267 int idx, result;
1268 Tcl_Obj *codeObjPtr = NULL;
1269 Proc *procPtr = NULL;
1270 Tcl_HashEntry *hPtr;
1271 Object *oPtr;
1272 Method *methodPtr;
1273
1274 if (objc < 2) {
1275 Tcl_WrongNumArgs(interp, 1, objv, "type ...");
1276 return TCL_ERROR;
1277 }
1278 if (Tcl_GetIndexFromObj(interp, objv[1], types, "type", 0, &idx)!=TCL_OK){
1279 return TCL_ERROR;
1280 }
1281
1282 switch ((enum Types) idx) {
1283 case DISAS_LAMBDA: {
1284 Command cmd;
1285 Tcl_Obj *nsObjPtr;
1286 Tcl_Namespace *nsPtr;
1287
1288 /*
1289 * Compile (if uncompiled) and disassemble a lambda term.
1290 *
1291 * WARNING! Pokes inside the lambda objtype.
1292 */
1293
1294 if (objc != 3) {
1295 Tcl_WrongNumArgs(interp, 2, objv, "lambdaTerm");
1296 return TCL_ERROR;
1297 }
1298 if (objv[2]->typePtr == &tclLambdaType) {
1299 procPtr = (Proc *)objv[2]->internalRep.twoPtrValue.ptr1;
1300 }
1301 if (procPtr == NULL || procPtr->iPtr != (Interp *) interp) {
1302 result = tclLambdaType.setFromAnyProc(interp, objv[2]);
1303 if (result != TCL_OK) {
1304 return result;
1305 }
1306 procPtr = (Proc *)objv[2]->internalRep.twoPtrValue.ptr1;
1307 }
1308
1309 memset(&cmd, 0, sizeof(Command));
1310 nsObjPtr = (Tcl_Obj *)objv[2]->internalRep.twoPtrValue.ptr2;
1311 result = TclGetNamespaceFromObj(interp, nsObjPtr, &nsPtr);
1312 if (result != TCL_OK) {
1313 return result;
1314 }
1315 cmd.nsPtr = (Namespace *) nsPtr;
1316 procPtr->cmdPtr = &cmd;
1317 result = TclPushProcCallFrame(procPtr, interp, objc, objv, 1);
1318 if (result != TCL_OK) {
1319 return result;
1320 }
1321 TclPopStackFrame(interp);
1322 codeObjPtr = procPtr->bodyPtr;
1323 break;
1324 }
1325 case DISAS_PROC:
1326 if (objc != 3) {
1327 Tcl_WrongNumArgs(interp, 2, objv, "procName");
1328 return TCL_ERROR;
1329 }
1330
1331 procPtr = TclFindProc((Interp *) interp, TclGetString(objv[2]));
1332 if (procPtr == NULL) {
1333 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
1334 "\"%s\" isn't a procedure", TclGetString(objv[2])));
1335 Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "PROC",
1336 TclGetString(objv[2]), (char *)NULL);
1337 return TCL_ERROR;
1338 }
1339
1340 /*
1341 * Compile (if uncompiled) and disassemble a procedure.
1342 */
1343
1344 result = TclPushProcCallFrame(procPtr, interp, 2, objv+1, 1);
1345 if (result != TCL_OK) {
1346 return result;
1347 }
1348 TclPopStackFrame(interp);
1349 codeObjPtr = procPtr->bodyPtr;
1350 break;
1351 case DISAS_SCRIPT:
1352 /*
1353 * Compile and disassemble a script.
1354 */
1355
1356 if (objc != 3) {
1357 Tcl_WrongNumArgs(interp, 2, objv, "script");
1358 return TCL_ERROR;
1359 }
1360 if ((objv[2]->typePtr != &tclByteCodeType)
1361 && (TclSetByteCodeFromAny(interp, objv[2], NULL, NULL) != TCL_OK)) {
1362 return TCL_ERROR;
1363 }
1364 codeObjPtr = objv[2];
1365 break;
1366
1367 case DISAS_CLASS_CONSTRUCTOR:
1368 if (objc != 3) {
1369 Tcl_WrongNumArgs(interp, 2, objv, "className");
1370 return TCL_ERROR;
1371 }
1372
1373 /*
1374 * Look up the body of a constructor.
1375 */
1376
1377 oPtr = (Object *) Tcl_GetObjectFromObj(interp, objv[2]);
1378 if (oPtr == NULL) {
1379 return TCL_ERROR;
1380 }
1381 if (oPtr->classPtr == NULL) {
1382 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
1383 "\"%s\" is not a class", TclGetString(objv[2])));
1384 Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "CLASS",
1385 TclGetString(objv[2]), (char *)NULL);
1386 return TCL_ERROR;
1387 }
1388
1389 methodPtr = oPtr->classPtr->constructorPtr;
1390 if (methodPtr == NULL) {
1391 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
1392 "\"%s\" has no defined constructor",
1393 TclGetString(objv[2])));
1394 Tcl_SetErrorCode(interp, "TCL", "OPERATION", "DISASSEMBLE",
1395 "CONSRUCTOR", (char *)NULL);
1396 return TCL_ERROR;
1397 }
1398 procPtr = TclOOGetProcFromMethod(methodPtr);
1399 if (procPtr == NULL) {
1400 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1401 "body not available for this kind of constructor", -1));
1402 Tcl_SetErrorCode(interp, "TCL", "OPERATION", "DISASSEMBLE",
1403 "METHODTYPE", (char *)NULL);
1404 return TCL_ERROR;
1405 }
1406
1407 /*
1408 * Compile if necessary.
1409 */
1410
1411 if (procPtr->bodyPtr->typePtr != &tclByteCodeType) {
1412 Command cmd;
1413
1414 /*
1415 * Yes, this is ugly, but we need to pass the namespace in to the
1416 * compiler in two places.
1417 */
1418
1419 cmd.nsPtr = (Namespace *) oPtr->namespacePtr;
1420 procPtr->cmdPtr = &cmd;
1421 result = TclProcCompileProc(interp, procPtr, procPtr->bodyPtr,
1422 (Namespace *) oPtr->namespacePtr, "body of constructor",
1423 TclGetString(objv[2]));
1424 procPtr->cmdPtr = NULL;
1425 if (result != TCL_OK) {
1426 return result;
1427 }
1428 }
1429 codeObjPtr = procPtr->bodyPtr;
1430 break;
1431
1432 case DISAS_CLASS_DESTRUCTOR:
1433 if (objc != 3) {
1434 Tcl_WrongNumArgs(interp, 2, objv, "className");
1435 return TCL_ERROR;
1436 }
1437
1438 /*
1439 * Look up the body of a destructor.
1440 */
1441
1442 oPtr = (Object *) Tcl_GetObjectFromObj(interp, objv[2]);
1443 if (oPtr == NULL) {
1444 return TCL_ERROR;
1445 }
1446 if (oPtr->classPtr == NULL) {
1447 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
1448 "\"%s\" is not a class", TclGetString(objv[2])));
1449 Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "CLASS",
1450 TclGetString(objv[2]), (char *)NULL);
1451 return TCL_ERROR;
1452 }
1453
1454 methodPtr = oPtr->classPtr->destructorPtr;
1455 if (methodPtr == NULL) {
1456 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
1457 "\"%s\" has no defined destructor",
1458 TclGetString(objv[2])));
1459 Tcl_SetErrorCode(interp, "TCL", "OPERATION", "DISASSEMBLE",
1460 "DESRUCTOR", (char *)NULL);
1461 return TCL_ERROR;
1462 }
1463 procPtr = TclOOGetProcFromMethod(methodPtr);
1464 if (procPtr == NULL) {
1465 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1466 "body not available for this kind of destructor", -1));
1467 Tcl_SetErrorCode(interp, "TCL", "OPERATION", "DISASSEMBLE",
1468 "METHODTYPE", (char *)NULL);
1469 return TCL_ERROR;
1470 }
1471
1472 /*
1473 * Compile if necessary.
1474 */
1475
1476 if (procPtr->bodyPtr->typePtr != &tclByteCodeType) {
1477 Command cmd;
1478
1479 /*
1480 * Yes, this is ugly, but we need to pass the namespace in to the
1481 * compiler in two places.
1482 */
1483
1484 cmd.nsPtr = (Namespace *) oPtr->namespacePtr;
1485 procPtr->cmdPtr = &cmd;
1486 result = TclProcCompileProc(interp, procPtr, procPtr->bodyPtr,
1487 (Namespace *) oPtr->namespacePtr, "body of destructor",
1488 TclGetString(objv[2]));
1489 procPtr->cmdPtr = NULL;
1490 if (result != TCL_OK) {
1491 return result;
1492 }
1493 }
1494 codeObjPtr = procPtr->bodyPtr;
1495 break;
1496
1497 case DISAS_CLASS_METHOD:
1498 if (objc != 4) {
1499 Tcl_WrongNumArgs(interp, 2, objv, "className methodName");
1500 return TCL_ERROR;
1501 }
1502
1503 /*
1504 * Look up the body of a class method.
1505 */
1506
1507 oPtr = (Object *) Tcl_GetObjectFromObj(interp, objv[2]);
1508 if (oPtr == NULL) {
1509 return TCL_ERROR;
1510 }
1511 if (oPtr->classPtr == NULL) {
1512 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
1513 "\"%s\" is not a class", TclGetString(objv[2])));
1514 Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "CLASS",
1515 TclGetString(objv[2]), (char *)NULL);
1516 return TCL_ERROR;
1517 }
1518 hPtr = Tcl_FindHashEntry(&oPtr->classPtr->classMethods,
1519 (char *)objv[3]);
1520 goto methodBody;
1521 case DISAS_OBJECT_METHOD:
1522 if (objc != 4) {
1523 Tcl_WrongNumArgs(interp, 2, objv, "objectName methodName");
1524 return TCL_ERROR;
1525 }
1526
1527 /*
1528 * Look up the body of an instance method.
1529 */
1530
1531 oPtr = (Object *) Tcl_GetObjectFromObj(interp, objv[2]);
1532 if (oPtr == NULL) {
1533 return TCL_ERROR;
1534 }
1535 if (oPtr->methodsPtr == NULL) {
1536 goto unknownMethod;
1537 }
1538 hPtr = Tcl_FindHashEntry(oPtr->methodsPtr, (char *)objv[3]);
1539
1540 /*
1541 * Compile (if necessary) and disassemble a method body.
1542 */
1543
1544 methodBody:
1545 if (hPtr == NULL) {
1546 unknownMethod:
1547 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
1548 "unknown method \"%s\"", TclGetString(objv[3])));
1549 Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "METHOD",
1550 TclGetString(objv[3]), (char *)NULL);
1551 return TCL_ERROR;
1552 }
1553 procPtr = TclOOGetProcFromMethod((Method *)Tcl_GetHashValue(hPtr));
1554 if (procPtr == NULL) {
1555 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1556 "body not available for this kind of method", -1));
1557 Tcl_SetErrorCode(interp, "TCL", "OPERATION", "DISASSEMBLE",
1558 "METHODTYPE", (char *)NULL);
1559 return TCL_ERROR;
1560 }
1561 if (procPtr->bodyPtr->typePtr != &tclByteCodeType) {
1562 Command cmd;
1563
1564 /*
1565 * Yes, this is ugly, but we need to pass the namespace in to the
1566 * compiler in two places.
1567 */
1568
1569 cmd.nsPtr = (Namespace *) oPtr->namespacePtr;
1570 procPtr->cmdPtr = &cmd;
1571 result = TclProcCompileProc(interp, procPtr, procPtr->bodyPtr,
1572 (Namespace *) oPtr->namespacePtr, "body of method",
1573 TclGetString(objv[3]));
1574 procPtr->cmdPtr = NULL;
1575 if (result != TCL_OK) {
1576 return result;
1577 }
1578 }
1579 codeObjPtr = procPtr->bodyPtr;
1580 break;
1581 default:
1582 CLANG_ASSERT(0);
1583 }
1584
1585 /*
1586 * Do the actual disassembly.
1587 */
1588
1589 if (BYTECODE(codeObjPtr)->flags & TCL_BYTECODE_PRECOMPILED) {
1590 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1591 "may not disassemble prebuilt bytecode", -1));
1592 Tcl_SetErrorCode(interp, "TCL", "OPERATION", "DISASSEMBLE",
1593 "BYTECODE", (char *)NULL);
1594 return TCL_ERROR;
1595 }
1596 if (clientData) {
1597 Tcl_SetObjResult(interp,
1598 DisassembleByteCodeAsDicts(codeObjPtr));
1599 } else {
1600 Tcl_SetObjResult(interp,
1601 DisassembleByteCodeObj(codeObjPtr));
1602 }
1603 return TCL_OK;
1604 }
1605
1606 /*
1607 * Local Variables:
1608 * mode: c
1609 * c-basic-offset: 4
1610 * fill-column: 78
1611 * tab-width: 8
1612 * End:
1613 */