Posted to tcl by dogeen at Tue Jun 29 18:39:44 GMT 2010view raw

  1. int
  2. Tcl_AssembleProc(
  3. ClientData dummy, /* Not used. */
  4. Tcl_Interp *interp, /* Current interpreter. */
  5. int objc, /* Number of arguments. */
  6. Tcl_Obj *const objv[]) /* Argument objects. */
  7. {
  8. Interp *iPtr = (Interp *) interp;
  9. ByteCode *byteCodePtr;
  10. unsigned char *p;
  11. int size;
  12. int numCommands, numSrcBytes, numCodeBytes, numLitObjects, numExceptRanges;
  13. int numAuxDataItems, numCmdLocBytes, maxExceptDepth, maxStackDepth;
  14. int objArrayBytes, exceptArrayBytes, auxDataArrayBytes;
  15. // LocMapSizes *locMapPtr = &envPtr->locMap;
  16. Namespace *namespacePtr;
  17.  
  18. objArrayBytes = (1 * sizeof(Tcl_Obj *)); /* numLitObjects = 1 */
  19. exceptArrayBytes = (0 * sizeof(ExceptionRange)); /* numExceptRanges = 0 */
  20. auxDataArrayBytes = (0 * sizeof(AuxData)); /* numAuxDataItems = 0 */
  21.  
  22. size = sizeof(ByteCode);
  23. size += TCL_ALIGN(3); /* align object array with numCodeBytes = 3 */
  24. size += TCL_ALIGN(objArrayBytes); /* align exception range array */
  25. size += TCL_ALIGN(exceptArrayBytes); /* align AuxData array */
  26. size += auxDataArrayBytes;
  27. size += 0; /* Since numCmdLocBytes is 0 */
  28.  
  29. p = (unsigned char *) ckalloc((size_t) size);
  30. byteCodePtr = (ByteCode *) p;
  31. memset(byteCodePtr, 0, (size_t) size);
  32.  
  33. byteCodePtr->interpHandle = TclHandlePreserve(iPtr->handle);
  34.  
  35. if (iPtr->varFramePtr != NULL) {
  36. namespacePtr = iPtr->varFramePtr->nsPtr;
  37. } else {
  38. namespacePtr = iPtr->globalNsPtr;
  39. }
  40.  
  41. byteCodePtr->compileEpoch = iPtr->compileEpoch;
  42. byteCodePtr->nsPtr = namespacePtr;
  43. byteCodePtr->nsEpoch = namespacePtr->resolverEpoch;
  44. byteCodePtr->refCount = 1;
  45. byteCodePtr->flags = TCL_BYTECODE_PRECOMPILED;
  46. byteCodePtr->procPtr = NULL;
  47. byteCodePtr->structureSize = 0; /* Not really 0 needs update */
  48. byteCodePtr->numCommands = 1; /* kbk says 1, disassemble says 0 */
  49.  
  50. char noSourceCode[] = "# Created by bytecode assembler";
  51. byteCodePtr->source = "# Created by bytecode assembler";
  52. byteCodePtr->numSrcBytes = sizeof(noSourceCode);
  53. byteCodePtr->maxStackDepth = 1;
  54. byteCodePtr->maxExceptDepth = 0;
  55. byteCodePtr->numAuxDataItems = 0;
  56. byteCodePtr->numCodeBytes = 3; /* Even the empty proc has a push1 and a done
  57. instruction, which sum up to 3 bytes */
  58. byteCodePtr->numLitObjects = 1; /* Not sure about this, but disassembling
  59. shows, empty proc has 1 */
  60. byteCodePtr->numCmdLocBytes = 0; /* Not sure about this, but disassembling
  61. shows, empty proc has 0 */
  62. byteCodePtr->numExceptRanges = 0; /* Not sure about this, but disassembling
  63. shows, empty proc has 0 */
  64.  
  65. /* This part is the same with InitByteCode, cmpRead.c of tbcload */
  66. p += sizeof(ByteCode);
  67. byteCodePtr->codeStart = p;
  68.  
  69. p += TCL_ALIGN(byteCodePtr->numCodeBytes);
  70. if (byteCodePtr->numLitObjects > 0) {
  71. byteCodePtr->objArrayPtr = (Tcl_Obj **) p;
  72. }
  73.  
  74. p += TCL_ALIGN(objArrayBytes);
  75. if (byteCodePtr->numExceptRanges > 0) {
  76. byteCodePtr->exceptArrayPtr = (ExceptionRange *) p;
  77. }
  78.  
  79. p += TCL_ALIGN(exceptArrayBytes);
  80. if (byteCodePtr->numAuxDataItems > 0) {
  81. byteCodePtr->auxDataArrayPtr = (AuxData *) p;
  82. }
  83.  
  84. p += auxDataArrayBytes;
  85. byteCodePtr->codeDeltaStart = p;
  86.  
  87. /* Have no idea, how to deal with what's below, codeDeltaSize and such
  88. *
  89. p += locMapPtr->codeDeltaSize;
  90. byteCodePtr->codeLengthStart = p;
  91. p += locMapPtr->codeLengthSize;
  92. byteCodePtr->srcDeltaStart = p;
  93. if (locMapPtr->srcDeltaSize < 0) {
  94. p += byteCodePtr->numCommands;
  95. } else {
  96. p += locMapPtr->srcDeltaSize;
  97. }
  98. */
  99. byteCodePtr->srcLengthStart = p;
  100.  
  101.  
  102.  
  103. char strbuf[50];
  104. int i = 0;
  105. for (;i < objc; i++) {
  106. sprintf(strbuf, " Argument %d, %s \n", i, TclGetString(objv[i]));
  107. Tcl_AppendResult(interp, strbuf,NULL);
  108. }
  109. return TCL_OK;
  110.  
  111. }