gmByteCode.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. _____ __ ___ __ ____ _ __
  3. / ___/__ ___ _ ___ / |/ /__ ___ / /_____ __ __/ __/_______(_)__ / /_
  4. / (_ / _ `/ ' \/ -_) /|_/ / _ \/ _ \/ '_/ -_) // /\ \/ __/ __/ / _ \/ __/
  5. \___/\_,_/_/_/_/\__/_/ /_/\___/_//_/_/\_\\__/\_, /___/\__/_/ /_/ .__/\__/
  6. /___/ /_/
  7. See Copyright Notice in gmMachine.h
  8. */
  9. #include "gmConfig.h"
  10. #include "gmByteCode.h"
  11. #if GM_COMPILE_DEBUG
  12. void gmByteCodePrint(FILE * a_fp, const void * a_byteCode, int a_byteCodeLength)
  13. {
  14. union
  15. {
  16. const gmuint32 * instruction32;
  17. const gmuint8 * instruction;
  18. };
  19. instruction = (const gmuint8 *) a_byteCode;
  20. const gmuint8 * end = instruction + a_byteCodeLength;
  21. const gmuint8 * start = instruction;
  22. const char * cp;
  23. bool opiptr, opf32, opi32;
  24. while(instruction < end)
  25. {
  26. opiptr = false;
  27. opf32 = false;
  28. opi32 = false;
  29. int addr = (int)(instruction - start);
  30. switch(*instruction)
  31. {
  32. case BC_NOP : cp = "nop"; break;
  33. case BC_LINE : cp = "line"; break;
  34. case BC_GETDOT : cp = "get dot"; opiptr = true; break;
  35. case BC_SETDOT : cp = "set dot"; opiptr = true; break;
  36. case BC_GETIND : cp = "get index"; break;
  37. case BC_SETIND : cp = "set index"; break;
  38. case BC_BRA : cp = "bra"; opiptr = true; break;
  39. case BC_BRZ : cp = "brz"; opiptr = true; break;
  40. case BC_BRNZ : cp = "brnz"; opiptr = true; break;
  41. case BC_BRZK : cp = "brzk"; opiptr = true; break;
  42. case BC_BRNZK : cp = "brnzk"; opiptr = true; break;
  43. case BC_CALL : cp = "call"; opiptr = true; break;
  44. case BC_RET : cp = "ret"; break;
  45. case BC_RETV : cp = "retv"; break;
  46. case BC_FOREACH : cp = "foreach"; opiptr = true; break;
  47. case BC_POP : cp = "pop"; break;
  48. case BC_POP2 : cp = "pop2"; break;
  49. case BC_DUP : cp = "dup"; break;
  50. case BC_DUP2 : cp = "dup2"; break;
  51. case BC_SWAP : cp = "swap"; break;
  52. case BC_PUSHNULL : cp = "push null"; break;
  53. case BC_PUSHINT : cp = "push int"; opi32 = true; break;
  54. case BC_PUSHINT0 : cp = "push int 0"; break;
  55. case BC_PUSHINT1 : cp = "push int 1"; break;
  56. case BC_PUSHFP : cp = "push fp"; opf32 = true; break;
  57. case BC_PUSHSTR : cp = "push str"; opiptr = true; break;
  58. case BC_PUSHTBL : cp = "push tbl"; break;
  59. case BC_PUSHFN : cp = "push fn"; opiptr = true; break;
  60. case BC_PUSHTHIS : cp = "push this"; break;
  61. case BC_GETLOCAL : cp = "get local"; opi32 = true; break;
  62. case BC_SETLOCAL : cp = "set local"; opi32 = true; break;
  63. case BC_GETGLOBAL : cp = "get global"; opiptr = true; break;
  64. case BC_SETGLOBAL : cp = "set global"; opiptr = true; break;
  65. case BC_GETTHIS : cp = "get this"; opiptr = true; break;
  66. case BC_SETTHIS : cp = "set this"; opiptr = true; break;
  67. case BC_OP_ADD : cp = "add"; break;
  68. case BC_OP_SUB : cp = "sub"; break;
  69. case BC_OP_MUL : cp = "mul"; break;
  70. case BC_OP_DIV : cp = "div"; break;
  71. case BC_OP_REM : cp = "rem"; break;
  72. case BC_BIT_OR : cp = "bor"; break;
  73. case BC_BIT_XOR : cp = "bxor"; break;
  74. case BC_BIT_AND : cp = "band"; break;
  75. case BC_BIT_INV : cp = "binv"; break;
  76. case BC_BIT_SHL : cp = "bshl"; break;
  77. case BC_BIT_SHR : cp = "bshr"; break;
  78. case BC_OP_NEG : cp = "neg"; break;
  79. case BC_OP_POS : cp = "pos"; break;
  80. case BC_OP_NOT : cp = "not"; break;
  81. case BC_OP_LT : cp = "lt"; break;
  82. case BC_OP_GT : cp = "gt"; break;
  83. case BC_OP_LTE : cp = "lte"; break;
  84. case BC_OP_GTE : cp = "gte"; break;
  85. case BC_OP_EQ : cp = "eq"; break;
  86. case BC_OP_NEQ : cp = "neq"; break;
  87. #if GM_USE_FORK
  88. case BC_FORK : cp = "fork"; opiptr = true; break;
  89. #endif //GM_USE_FORK
  90. default : cp = "ERROR"; break;
  91. }
  92. ++instruction32;
  93. if(opf32)
  94. {
  95. float fval = *((float *) instruction);
  96. instruction += sizeof(gmint32);
  97. fprintf(a_fp, " %04d %s %f" GM_NL, addr, cp, fval);
  98. }
  99. if(opi32)
  100. {
  101. gmint32 ival = *((gmint32 *) instruction);
  102. instruction += sizeof(gmint32);
  103. fprintf(a_fp, " %04d %s %d" GM_NL, addr, cp, ival);
  104. }
  105. else if (opiptr)
  106. {
  107. gmptr ival = *((gmptr *) instruction);
  108. instruction += sizeof(gmptr);
  109. fprintf(a_fp, " %04d %s %lld" GM_NL, addr, cp, (gmint64)ival);
  110. }
  111. else
  112. {
  113. fprintf(a_fp, " %04d %s" GM_NL, addr, cp);
  114. }
  115. }
  116. }
  117. #endif // GM_COMPILE_DEBUG