gdscript_disassembler.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /*************************************************************************/
  2. /* gdscript_disassembler.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifdef DEBUG_ENABLED
  31. #include "gdscript_function.h"
  32. #include "core/string/string_builder.h"
  33. #include "gdscript.h"
  34. #include "gdscript_functions.h"
  35. static String _get_variant_string(const Variant &p_variant) {
  36. String txt;
  37. if (p_variant.get_type() == Variant::STRING) {
  38. txt = "\"" + String(p_variant) + "\"";
  39. } else if (p_variant.get_type() == Variant::STRING_NAME) {
  40. txt = "&\"" + String(p_variant) + "\"";
  41. } else if (p_variant.get_type() == Variant::NODE_PATH) {
  42. txt = "^\"" + String(p_variant) + "\"";
  43. } else if (p_variant.get_type() == Variant::OBJECT) {
  44. Object *obj = p_variant;
  45. if (!obj) {
  46. txt = "null";
  47. } else {
  48. GDScriptNativeClass *cls = Object::cast_to<GDScriptNativeClass>(obj);
  49. if (cls) {
  50. txt += cls->get_name();
  51. txt += " (class)";
  52. } else {
  53. txt = obj->get_class();
  54. if (obj->get_script_instance()) {
  55. txt += "(" + obj->get_script_instance()->get_script()->get_path() + ")";
  56. }
  57. }
  58. }
  59. } else {
  60. txt = p_variant;
  61. }
  62. return txt;
  63. }
  64. static String _disassemble_address(const GDScript *p_script, const GDScriptFunction &p_function, int p_address) {
  65. int addr = p_address & GDScriptFunction::ADDR_MASK;
  66. switch (p_address >> GDScriptFunction::ADDR_BITS) {
  67. case GDScriptFunction::ADDR_TYPE_SELF: {
  68. return "self";
  69. } break;
  70. case GDScriptFunction::ADDR_TYPE_CLASS: {
  71. return "class";
  72. } break;
  73. case GDScriptFunction::ADDR_TYPE_MEMBER: {
  74. return "member(" + p_script->debug_get_member_by_index(addr) + ")";
  75. } break;
  76. case GDScriptFunction::ADDR_TYPE_CLASS_CONSTANT: {
  77. return "class_const(" + p_function.get_global_name(addr) + ")";
  78. } break;
  79. case GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT: {
  80. return "const(" + _get_variant_string(p_function.get_constant(addr)) + ")";
  81. } break;
  82. case GDScriptFunction::ADDR_TYPE_STACK: {
  83. return "stack(" + itos(addr) + ")";
  84. } break;
  85. case GDScriptFunction::ADDR_TYPE_STACK_VARIABLE: {
  86. return "var_stack(" + itos(addr) + ")";
  87. } break;
  88. case GDScriptFunction::ADDR_TYPE_GLOBAL: {
  89. return "global(" + _get_variant_string(GDScriptLanguage::get_singleton()->get_global_array()[addr]) + ")";
  90. } break;
  91. case GDScriptFunction::ADDR_TYPE_NAMED_GLOBAL: {
  92. return "named_global(" + p_function.get_global_name(addr) + ")";
  93. } break;
  94. case GDScriptFunction::ADDR_TYPE_NIL: {
  95. return "nil";
  96. } break;
  97. }
  98. return "<err>";
  99. }
  100. void GDScriptFunction::disassemble(const Vector<String> &p_code_lines) const {
  101. #define DADDR(m_ip) (_disassemble_address(_script, *this, _code_ptr[ip + m_ip]))
  102. for (int ip = 0; ip < _code_size;) {
  103. StringBuilder text;
  104. int incr = 0;
  105. text += " ";
  106. text += itos(ip);
  107. text += ": ";
  108. // This makes the compiler complain if some opcode is unchecked in the switch.
  109. Opcode code = Opcode(_code_ptr[ip] & INSTR_MASK);
  110. int instr_var_args = (_code_ptr[ip] & INSTR_ARGS_MASK) >> INSTR_BITS;
  111. switch (code) {
  112. case OPCODE_OPERATOR: {
  113. int operation = _code_ptr[ip + 4];
  114. text += "operator ";
  115. text += DADDR(3);
  116. text += " = ";
  117. text += DADDR(1);
  118. text += " ";
  119. text += Variant::get_operator_name(Variant::Operator(operation));
  120. text += " ";
  121. text += DADDR(2);
  122. incr += 5;
  123. } break;
  124. case OPCODE_OPERATOR_VALIDATED: {
  125. text += "validated operator ";
  126. text += DADDR(3);
  127. text += " = ";
  128. text += DADDR(1);
  129. text += " <operator function> ";
  130. text += DADDR(2);
  131. incr += 5;
  132. } break;
  133. case OPCODE_EXTENDS_TEST: {
  134. text += "is object ";
  135. text += DADDR(3);
  136. text += " = ";
  137. text += DADDR(1);
  138. text += " is ";
  139. text += DADDR(2);
  140. incr += 4;
  141. } break;
  142. case OPCODE_IS_BUILTIN: {
  143. text += "is builtin ";
  144. text += DADDR(2);
  145. text += " = ";
  146. text += DADDR(1);
  147. text += " is ";
  148. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 3]));
  149. incr += 4;
  150. } break;
  151. case OPCODE_SET_KEYED: {
  152. text += "set keyed ";
  153. text += DADDR(1);
  154. text += "[";
  155. text += DADDR(2);
  156. text += "] = ";
  157. text += DADDR(3);
  158. incr += 4;
  159. } break;
  160. case OPCODE_SET_KEYED_VALIDATED: {
  161. text += "set keyed validated ";
  162. text += DADDR(1);
  163. text += "[";
  164. text += DADDR(2);
  165. text += "] = ";
  166. text += DADDR(3);
  167. incr += 5;
  168. } break;
  169. case OPCODE_SET_INDEXED_VALIDATED: {
  170. text += "set indexed validated ";
  171. text += DADDR(1);
  172. text += "[";
  173. text += DADDR(2);
  174. text += "] = ";
  175. text += DADDR(3);
  176. incr += 5;
  177. } break;
  178. case OPCODE_GET_KEYED: {
  179. text += "get keyed ";
  180. text += DADDR(3);
  181. text += " = ";
  182. text += DADDR(1);
  183. text += "[";
  184. text += DADDR(2);
  185. text += "]";
  186. incr += 4;
  187. } break;
  188. case OPCODE_GET_KEYED_VALIDATED: {
  189. text += "get keyed validated ";
  190. text += DADDR(3);
  191. text += " = ";
  192. text += DADDR(1);
  193. text += "[";
  194. text += DADDR(2);
  195. text += "]";
  196. incr += 5;
  197. } break;
  198. case OPCODE_GET_INDEXED_VALIDATED: {
  199. text += "get indexed validated ";
  200. text += DADDR(3);
  201. text += " = ";
  202. text += DADDR(1);
  203. text += "[";
  204. text += DADDR(2);
  205. text += "]";
  206. incr += 5;
  207. } break;
  208. case OPCODE_SET_NAMED: {
  209. text += "set_named ";
  210. text += DADDR(1);
  211. text += "[\"";
  212. text += _global_names_ptr[_code_ptr[ip + 3]];
  213. text += "\"] = ";
  214. text += DADDR(2);
  215. incr += 4;
  216. } break;
  217. case OPCODE_SET_NAMED_VALIDATED: {
  218. text += "set_named validated ";
  219. text += DADDR(1);
  220. text += "[\"";
  221. text += "<unknown name>";
  222. text += "\"] = ";
  223. text += DADDR(2);
  224. incr += 4;
  225. } break;
  226. case OPCODE_GET_NAMED: {
  227. text += "get_named ";
  228. text += DADDR(2);
  229. text += " = ";
  230. text += DADDR(1);
  231. text += "[\"";
  232. text += _global_names_ptr[_code_ptr[ip + 3]];
  233. text += "\"]";
  234. incr += 4;
  235. } break;
  236. case OPCODE_GET_NAMED_VALIDATED: {
  237. text += "get_named validated ";
  238. text += DADDR(2);
  239. text += " = ";
  240. text += DADDR(1);
  241. text += "[\"";
  242. text += "<unknown name>";
  243. text += "\"]";
  244. incr += 4;
  245. } break;
  246. case OPCODE_SET_MEMBER: {
  247. text += "set_member ";
  248. text += "[\"";
  249. text += _global_names_ptr[_code_ptr[ip + 2]];
  250. text += "\"] = ";
  251. text += DADDR(1);
  252. incr += 3;
  253. } break;
  254. case OPCODE_GET_MEMBER: {
  255. text += "get_member ";
  256. text += DADDR(1);
  257. text += " = ";
  258. text += "[\"";
  259. text += _global_names_ptr[_code_ptr[ip + 2]];
  260. text += "\"]";
  261. incr += 3;
  262. } break;
  263. case OPCODE_ASSIGN: {
  264. text += "assign ";
  265. text += DADDR(1);
  266. text += " = ";
  267. text += DADDR(2);
  268. incr += 3;
  269. } break;
  270. case OPCODE_ASSIGN_TRUE: {
  271. text += "assign ";
  272. text += DADDR(1);
  273. text += " = true";
  274. incr += 2;
  275. } break;
  276. case OPCODE_ASSIGN_FALSE: {
  277. text += "assign ";
  278. text += DADDR(1);
  279. text += " = false";
  280. incr += 2;
  281. } break;
  282. case OPCODE_ASSIGN_TYPED_BUILTIN: {
  283. text += "assign typed builtin (";
  284. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 3]);
  285. text += ") ";
  286. text += DADDR(1);
  287. text += " = ";
  288. text += DADDR(2);
  289. incr += 4;
  290. } break;
  291. case OPCODE_ASSIGN_TYPED_NATIVE: {
  292. Variant class_name = _constants_ptr[_code_ptr[ip + 3]];
  293. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(class_name.operator Object *());
  294. text += "assign typed native (";
  295. text += nc->get_name().operator String();
  296. text += ") ";
  297. text += DADDR(1);
  298. text += " = ";
  299. text += DADDR(2);
  300. incr += 4;
  301. } break;
  302. case OPCODE_ASSIGN_TYPED_SCRIPT: {
  303. Variant script = _constants_ptr[_code_ptr[ip + 3]];
  304. Script *sc = Object::cast_to<Script>(script.operator Object *());
  305. text += "assign typed script (";
  306. text += sc->get_path();
  307. text += ") ";
  308. text += DADDR(1);
  309. text += " = ";
  310. text += DADDR(2);
  311. incr += 4;
  312. } break;
  313. case OPCODE_CAST_TO_BUILTIN: {
  314. text += "cast builtin ";
  315. text += DADDR(2);
  316. text += " = ";
  317. text += DADDR(1);
  318. text += " as ";
  319. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 1]));
  320. incr += 4;
  321. } break;
  322. case OPCODE_CAST_TO_NATIVE: {
  323. Variant class_name = _constants_ptr[_code_ptr[ip + 1]];
  324. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(class_name.operator Object *());
  325. text += "cast native ";
  326. text += DADDR(2);
  327. text += " = ";
  328. text += DADDR(1);
  329. text += " as ";
  330. text += nc->get_name();
  331. incr += 4;
  332. } break;
  333. case OPCODE_CAST_TO_SCRIPT: {
  334. text += "cast ";
  335. text += DADDR(2);
  336. text += " = ";
  337. text += DADDR(1);
  338. text += " as ";
  339. text += DADDR(3);
  340. incr += 4;
  341. } break;
  342. case OPCODE_CONSTRUCT: {
  343. Variant::Type t = Variant::Type(_code_ptr[ip + 3 + instr_var_args]);
  344. int argc = _code_ptr[ip + 1 + instr_var_args];
  345. text += "construct ";
  346. text += DADDR(1 + argc);
  347. text += " = ";
  348. text += Variant::get_type_name(t) + "(";
  349. for (int i = 0; i < argc; i++) {
  350. if (i > 0)
  351. text += ", ";
  352. text += DADDR(i + 1);
  353. }
  354. text += ")";
  355. incr = 3 + instr_var_args;
  356. } break;
  357. case OPCODE_CONSTRUCT_VALIDATED: {
  358. int argc = _code_ptr[ip + 1 + instr_var_args];
  359. text += "construct validated ";
  360. text += DADDR(1 + argc);
  361. text += " = ";
  362. text += "<unkown type>(";
  363. for (int i = 0; i < argc; i++) {
  364. if (i > 0)
  365. text += ", ";
  366. text += DADDR(i + 1);
  367. }
  368. text += ")";
  369. incr = 3 + instr_var_args;
  370. } break;
  371. case OPCODE_CONSTRUCT_ARRAY: {
  372. int argc = _code_ptr[ip + 1 + instr_var_args];
  373. text += " make_array ";
  374. text += DADDR(1 + argc);
  375. text += " = [";
  376. for (int i = 0; i < argc; i++) {
  377. if (i > 0)
  378. text += ", ";
  379. text += DADDR(1 + i);
  380. }
  381. text += "]";
  382. incr += 3 + argc;
  383. } break;
  384. case OPCODE_CONSTRUCT_DICTIONARY: {
  385. int argc = _code_ptr[ip + 1 + instr_var_args];
  386. text += "make_dict ";
  387. text += DADDR(1 + argc * 2);
  388. text += " = {";
  389. for (int i = 0; i < argc; i++) {
  390. if (i > 0)
  391. text += ", ";
  392. text += DADDR(1 + i * 2 + 0);
  393. text += ": ";
  394. text += DADDR(1 + i * 2 + 1);
  395. }
  396. text += "}";
  397. incr += 3 + argc * 2;
  398. } break;
  399. case OPCODE_CALL:
  400. case OPCODE_CALL_RETURN:
  401. case OPCODE_CALL_ASYNC: {
  402. bool ret = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_RETURN;
  403. bool async = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_ASYNC;
  404. if (ret) {
  405. text += "call-ret ";
  406. } else if (async) {
  407. text += "call-async ";
  408. } else {
  409. text += "call ";
  410. }
  411. int argc = _code_ptr[ip + 1 + instr_var_args];
  412. if (ret || async) {
  413. text += DADDR(2 + argc) + " = ";
  414. }
  415. text += DADDR(1 + argc) + ".";
  416. text += String(_global_names_ptr[_code_ptr[ip + 2 + instr_var_args]]);
  417. text += "(";
  418. for (int i = 0; i < argc; i++) {
  419. if (i > 0)
  420. text += ", ";
  421. text += DADDR(1 + i);
  422. }
  423. text += ")";
  424. incr = 5 + argc;
  425. } break;
  426. case OPCODE_CALL_METHOD_BIND:
  427. case OPCODE_CALL_METHOD_BIND_RET: {
  428. bool ret = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_METHOD_BIND_RET;
  429. if (ret) {
  430. text += "call-method_bind-ret ";
  431. } else {
  432. text += "call-method_bind ";
  433. }
  434. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  435. int argc = _code_ptr[ip + 1 + instr_var_args];
  436. if (ret) {
  437. text += DADDR(2 + argc) + " = ";
  438. }
  439. text += DADDR(1 + argc) + ".";
  440. text += method->get_name();
  441. text += "(";
  442. for (int i = 0; i < argc; i++) {
  443. if (i > 0)
  444. text += ", ";
  445. text += DADDR(1 + i);
  446. }
  447. text += ")";
  448. incr = 5 + argc;
  449. } break;
  450. case OPCODE_CALL_PTRCALL_NO_RETURN: {
  451. text += "call-ptrcall (no return) ";
  452. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  453. int argc = _code_ptr[ip + 1 + instr_var_args];
  454. text += DADDR(1 + argc) + ".";
  455. text += method->get_name();
  456. text += "(";
  457. for (int i = 0; i < argc; i++) {
  458. if (i > 0)
  459. text += ", ";
  460. text += DADDR(1 + i);
  461. }
  462. text += ")";
  463. incr = 5 + argc;
  464. } break;
  465. #define DISASSEMBLE_PTRCALL(m_type) \
  466. case OPCODE_CALL_PTRCALL_##m_type: { \
  467. text += "call-ptrcall (return "; \
  468. text += #m_type; \
  469. text += ") "; \
  470. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]]; \
  471. int argc = _code_ptr[ip + 1 + instr_var_args]; \
  472. text += DADDR(2 + argc) + " = "; \
  473. text += DADDR(1 + argc) + "."; \
  474. text += method->get_name(); \
  475. text += "("; \
  476. for (int i = 0; i < argc; i++) { \
  477. if (i > 0) \
  478. text += ", "; \
  479. text += DADDR(1 + i); \
  480. } \
  481. text += ")"; \
  482. incr = 5 + argc; \
  483. } break
  484. DISASSEMBLE_PTRCALL(BOOL);
  485. DISASSEMBLE_PTRCALL(INT);
  486. DISASSEMBLE_PTRCALL(FLOAT);
  487. DISASSEMBLE_PTRCALL(STRING);
  488. DISASSEMBLE_PTRCALL(VECTOR2);
  489. DISASSEMBLE_PTRCALL(VECTOR2I);
  490. DISASSEMBLE_PTRCALL(RECT2);
  491. DISASSEMBLE_PTRCALL(RECT2I);
  492. DISASSEMBLE_PTRCALL(VECTOR3);
  493. DISASSEMBLE_PTRCALL(VECTOR3I);
  494. DISASSEMBLE_PTRCALL(TRANSFORM2D);
  495. DISASSEMBLE_PTRCALL(PLANE);
  496. DISASSEMBLE_PTRCALL(AABB);
  497. DISASSEMBLE_PTRCALL(BASIS);
  498. DISASSEMBLE_PTRCALL(TRANSFORM);
  499. DISASSEMBLE_PTRCALL(COLOR);
  500. DISASSEMBLE_PTRCALL(STRING_NAME);
  501. DISASSEMBLE_PTRCALL(NODE_PATH);
  502. DISASSEMBLE_PTRCALL(RID);
  503. DISASSEMBLE_PTRCALL(QUAT);
  504. DISASSEMBLE_PTRCALL(OBJECT);
  505. DISASSEMBLE_PTRCALL(CALLABLE);
  506. DISASSEMBLE_PTRCALL(SIGNAL);
  507. DISASSEMBLE_PTRCALL(DICTIONARY);
  508. DISASSEMBLE_PTRCALL(ARRAY);
  509. DISASSEMBLE_PTRCALL(PACKED_BYTE_ARRAY);
  510. DISASSEMBLE_PTRCALL(PACKED_INT32_ARRAY);
  511. DISASSEMBLE_PTRCALL(PACKED_INT64_ARRAY);
  512. DISASSEMBLE_PTRCALL(PACKED_FLOAT32_ARRAY);
  513. DISASSEMBLE_PTRCALL(PACKED_FLOAT64_ARRAY);
  514. DISASSEMBLE_PTRCALL(PACKED_STRING_ARRAY);
  515. DISASSEMBLE_PTRCALL(PACKED_VECTOR2_ARRAY);
  516. DISASSEMBLE_PTRCALL(PACKED_VECTOR3_ARRAY);
  517. DISASSEMBLE_PTRCALL(PACKED_COLOR_ARRAY);
  518. case OPCODE_CALL_BUILTIN_TYPE_VALIDATED: {
  519. int argc = _code_ptr[ip + 1 + instr_var_args];
  520. text += "call-builtin-method validated ";
  521. text += DADDR(2 + argc) + " = ";
  522. text += DADDR(1) + ".";
  523. text += "<unknown method>";
  524. text += "(";
  525. for (int i = 0; i < argc; i++) {
  526. if (i > 0)
  527. text += ", ";
  528. text += DADDR(1 + i);
  529. }
  530. text += ")";
  531. incr = 5 + argc;
  532. } break;
  533. case OPCODE_CALL_BUILT_IN: {
  534. text += "call-built-in ";
  535. int argc = _code_ptr[ip + 1 + instr_var_args];
  536. text += DADDR(1 + argc) + " = ";
  537. text += GDScriptFunctions::get_func_name(GDScriptFunctions::Function(_code_ptr[ip + 2 + instr_var_args]));
  538. text += "(";
  539. for (int i = 0; i < argc; i++) {
  540. if (i > 0)
  541. text += ", ";
  542. text += DADDR(1 + i);
  543. }
  544. text += ")";
  545. incr = 4 + argc;
  546. } break;
  547. case OPCODE_CALL_SELF_BASE: {
  548. text += "call-self-base ";
  549. int argc = _code_ptr[ip + 1 + instr_var_args];
  550. text += DADDR(2 + argc) + " = ";
  551. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  552. text += "(";
  553. for (int i = 0; i < argc; i++) {
  554. if (i > 0)
  555. text += ", ";
  556. text += DADDR(1 + i);
  557. }
  558. text += ")";
  559. incr = 4 + argc;
  560. } break;
  561. case OPCODE_AWAIT: {
  562. text += "await ";
  563. text += DADDR(1);
  564. incr += 2;
  565. } break;
  566. case OPCODE_AWAIT_RESUME: {
  567. text += "await resume ";
  568. text += DADDR(1);
  569. incr = 2;
  570. } break;
  571. case OPCODE_JUMP: {
  572. text += "jump ";
  573. text += itos(_code_ptr[ip + 1]);
  574. incr = 2;
  575. } break;
  576. case OPCODE_JUMP_IF: {
  577. text += "jump-if ";
  578. text += DADDR(1);
  579. text += " to ";
  580. text += itos(_code_ptr[ip + 2]);
  581. incr = 3;
  582. } break;
  583. case OPCODE_JUMP_IF_NOT: {
  584. text += "jump-if-not ";
  585. text += DADDR(1);
  586. text += " to ";
  587. text += itos(_code_ptr[ip + 2]);
  588. incr = 3;
  589. } break;
  590. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  591. text += "jump-to-default-argument ";
  592. incr = 1;
  593. } break;
  594. case OPCODE_RETURN: {
  595. text += "return ";
  596. text += DADDR(1);
  597. incr = 2;
  598. } break;
  599. #define DISASSEMBLE_ITERATE(m_type) \
  600. case OPCODE_ITERATE_##m_type: { \
  601. text += "for-loop (typed "; \
  602. text += #m_type; \
  603. text += ") "; \
  604. text += DADDR(3); \
  605. text += " in "; \
  606. text += DADDR(2); \
  607. text += " counter "; \
  608. text += DADDR(1); \
  609. text += " end "; \
  610. text += itos(_code_ptr[ip + 4]); \
  611. incr += 5; \
  612. } break
  613. #define DISASSEMBLE_ITERATE_BEGIN(m_type) \
  614. case OPCODE_ITERATE_BEGIN_##m_type: { \
  615. text += "for-init (typed "; \
  616. text += #m_type; \
  617. text += ") "; \
  618. text += DADDR(3); \
  619. text += " in "; \
  620. text += DADDR(2); \
  621. text += " counter "; \
  622. text += DADDR(1); \
  623. text += " end "; \
  624. text += itos(_code_ptr[ip + 4]); \
  625. incr += 5; \
  626. } break
  627. #define DISASSEMBLE_ITERATE_TYPES(m_macro) \
  628. m_macro(INT); \
  629. m_macro(FLOAT); \
  630. m_macro(VECTOR2); \
  631. m_macro(VECTOR2I); \
  632. m_macro(VECTOR3); \
  633. m_macro(VECTOR3I); \
  634. m_macro(STRING); \
  635. m_macro(DICTIONARY); \
  636. m_macro(ARRAY); \
  637. m_macro(PACKED_BYTE_ARRAY); \
  638. m_macro(PACKED_INT32_ARRAY); \
  639. m_macro(PACKED_INT64_ARRAY); \
  640. m_macro(PACKED_FLOAT32_ARRAY); \
  641. m_macro(PACKED_FLOAT64_ARRAY); \
  642. m_macro(PACKED_STRING_ARRAY); \
  643. m_macro(PACKED_VECTOR2_ARRAY); \
  644. m_macro(PACKED_VECTOR3_ARRAY); \
  645. m_macro(PACKED_COLOR_ARRAY); \
  646. m_macro(OBJECT)
  647. case OPCODE_ITERATE_BEGIN: {
  648. text += "for-init ";
  649. text += DADDR(3);
  650. text += " in ";
  651. text += DADDR(2);
  652. text += " counter ";
  653. text += DADDR(1);
  654. text += " end ";
  655. text += itos(_code_ptr[ip + 4]);
  656. incr += 5;
  657. } break;
  658. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE_BEGIN);
  659. case OPCODE_ITERATE: {
  660. text += "for-loop ";
  661. text += DADDR(2);
  662. text += " in ";
  663. text += DADDR(2);
  664. text += " counter ";
  665. text += DADDR(1);
  666. text += " end ";
  667. text += itos(_code_ptr[ip + 4]);
  668. incr += 5;
  669. } break;
  670. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE);
  671. case OPCODE_LINE: {
  672. int line = _code_ptr[ip + 1] - 1;
  673. if (line >= 0 && line < p_code_lines.size()) {
  674. text += "line ";
  675. text += itos(line + 1);
  676. text += ": ";
  677. text += p_code_lines[line];
  678. } else {
  679. text += "";
  680. }
  681. incr += 2;
  682. } break;
  683. case OPCODE_ASSERT: {
  684. text += "assert (";
  685. text += DADDR(1);
  686. text += ", ";
  687. text += DADDR(2);
  688. text += ")";
  689. incr += 3;
  690. } break;
  691. case OPCODE_BREAKPOINT: {
  692. text += "breakpoint";
  693. incr += 1;
  694. } break;
  695. case OPCODE_END: {
  696. text += "== END ==";
  697. incr += 1;
  698. } break;
  699. }
  700. ip += incr;
  701. if (text.get_string_length() > 0) {
  702. print_line(text.as_string());
  703. }
  704. }
  705. }
  706. #endif