2
0

gdscript_disassembler.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. text += "cast native ";
  324. text += DADDR(2);
  325. text += " = ";
  326. text += DADDR(1);
  327. text += " as ";
  328. text += DADDR(3);
  329. incr += 4;
  330. } break;
  331. case OPCODE_CAST_TO_SCRIPT: {
  332. text += "cast ";
  333. text += DADDR(2);
  334. text += " = ";
  335. text += DADDR(1);
  336. text += " as ";
  337. text += DADDR(3);
  338. incr += 4;
  339. } break;
  340. case OPCODE_CONSTRUCT: {
  341. Variant::Type t = Variant::Type(_code_ptr[ip + 3 + instr_var_args]);
  342. int argc = _code_ptr[ip + 1 + instr_var_args];
  343. text += "construct ";
  344. text += DADDR(1 + argc);
  345. text += " = ";
  346. text += Variant::get_type_name(t) + "(";
  347. for (int i = 0; i < argc; i++) {
  348. if (i > 0)
  349. text += ", ";
  350. text += DADDR(i + 1);
  351. }
  352. text += ")";
  353. incr = 3 + instr_var_args;
  354. } break;
  355. case OPCODE_CONSTRUCT_VALIDATED: {
  356. int argc = _code_ptr[ip + 1 + instr_var_args];
  357. text += "construct validated ";
  358. text += DADDR(1 + argc);
  359. text += " = ";
  360. text += "<unkown type>(";
  361. for (int i = 0; i < argc; i++) {
  362. if (i > 0)
  363. text += ", ";
  364. text += DADDR(i + 1);
  365. }
  366. text += ")";
  367. incr = 3 + instr_var_args;
  368. } break;
  369. case OPCODE_CONSTRUCT_ARRAY: {
  370. int argc = _code_ptr[ip + 1 + instr_var_args];
  371. text += " make_array ";
  372. text += DADDR(1 + argc);
  373. text += " = [";
  374. for (int i = 0; i < argc; i++) {
  375. if (i > 0)
  376. text += ", ";
  377. text += DADDR(1 + i);
  378. }
  379. text += "]";
  380. incr += 3 + argc;
  381. } break;
  382. case OPCODE_CONSTRUCT_DICTIONARY: {
  383. int argc = _code_ptr[ip + 1 + instr_var_args];
  384. text += "make_dict ";
  385. text += DADDR(1 + argc * 2);
  386. text += " = {";
  387. for (int i = 0; i < argc; i++) {
  388. if (i > 0)
  389. text += ", ";
  390. text += DADDR(1 + i * 2 + 0);
  391. text += ": ";
  392. text += DADDR(1 + i * 2 + 1);
  393. }
  394. text += "}";
  395. incr += 3 + argc * 2;
  396. } break;
  397. case OPCODE_CALL:
  398. case OPCODE_CALL_RETURN:
  399. case OPCODE_CALL_ASYNC: {
  400. bool ret = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_RETURN;
  401. bool async = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_ASYNC;
  402. if (ret) {
  403. text += "call-ret ";
  404. } else if (async) {
  405. text += "call-async ";
  406. } else {
  407. text += "call ";
  408. }
  409. int argc = _code_ptr[ip + 1 + instr_var_args];
  410. if (ret || async) {
  411. text += DADDR(2 + argc) + " = ";
  412. }
  413. text += DADDR(1 + argc) + ".";
  414. text += String(_global_names_ptr[_code_ptr[ip + 2 + instr_var_args]]);
  415. text += "(";
  416. for (int i = 0; i < argc; i++) {
  417. if (i > 0)
  418. text += ", ";
  419. text += DADDR(1 + i);
  420. }
  421. text += ")";
  422. incr = 5 + argc;
  423. } break;
  424. case OPCODE_CALL_METHOD_BIND:
  425. case OPCODE_CALL_METHOD_BIND_RET: {
  426. bool ret = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_METHOD_BIND_RET;
  427. if (ret) {
  428. text += "call-method_bind-ret ";
  429. } else {
  430. text += "call-method_bind ";
  431. }
  432. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  433. int argc = _code_ptr[ip + 1 + instr_var_args];
  434. if (ret) {
  435. text += DADDR(2 + argc) + " = ";
  436. }
  437. text += DADDR(1 + argc) + ".";
  438. text += method->get_name();
  439. text += "(";
  440. for (int i = 0; i < argc; i++) {
  441. if (i > 0)
  442. text += ", ";
  443. text += DADDR(1 + i);
  444. }
  445. text += ")";
  446. incr = 5 + argc;
  447. } break;
  448. case OPCODE_CALL_PTRCALL_NO_RETURN: {
  449. text += "call-ptrcall (no return) ";
  450. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  451. int argc = _code_ptr[ip + 1 + instr_var_args];
  452. text += DADDR(1 + argc) + ".";
  453. text += method->get_name();
  454. text += "(";
  455. for (int i = 0; i < argc; i++) {
  456. if (i > 0)
  457. text += ", ";
  458. text += DADDR(1 + i);
  459. }
  460. text += ")";
  461. incr = 5 + argc;
  462. } break;
  463. #define DISASSEMBLE_PTRCALL(m_type) \
  464. case OPCODE_CALL_PTRCALL_##m_type: { \
  465. text += "call-ptrcall (return "; \
  466. text += #m_type; \
  467. text += ") "; \
  468. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]]; \
  469. int argc = _code_ptr[ip + 1 + instr_var_args]; \
  470. text += DADDR(2 + argc) + " = "; \
  471. text += DADDR(1 + argc) + "."; \
  472. text += method->get_name(); \
  473. text += "("; \
  474. for (int i = 0; i < argc; i++) { \
  475. if (i > 0) \
  476. text += ", "; \
  477. text += DADDR(1 + i); \
  478. } \
  479. text += ")"; \
  480. incr = 5 + argc; \
  481. } break
  482. DISASSEMBLE_PTRCALL(BOOL);
  483. DISASSEMBLE_PTRCALL(INT);
  484. DISASSEMBLE_PTRCALL(FLOAT);
  485. DISASSEMBLE_PTRCALL(STRING);
  486. DISASSEMBLE_PTRCALL(VECTOR2);
  487. DISASSEMBLE_PTRCALL(VECTOR2I);
  488. DISASSEMBLE_PTRCALL(RECT2);
  489. DISASSEMBLE_PTRCALL(RECT2I);
  490. DISASSEMBLE_PTRCALL(VECTOR3);
  491. DISASSEMBLE_PTRCALL(VECTOR3I);
  492. DISASSEMBLE_PTRCALL(TRANSFORM2D);
  493. DISASSEMBLE_PTRCALL(PLANE);
  494. DISASSEMBLE_PTRCALL(AABB);
  495. DISASSEMBLE_PTRCALL(BASIS);
  496. DISASSEMBLE_PTRCALL(TRANSFORM);
  497. DISASSEMBLE_PTRCALL(COLOR);
  498. DISASSEMBLE_PTRCALL(STRING_NAME);
  499. DISASSEMBLE_PTRCALL(NODE_PATH);
  500. DISASSEMBLE_PTRCALL(RID);
  501. DISASSEMBLE_PTRCALL(QUAT);
  502. DISASSEMBLE_PTRCALL(OBJECT);
  503. DISASSEMBLE_PTRCALL(CALLABLE);
  504. DISASSEMBLE_PTRCALL(SIGNAL);
  505. DISASSEMBLE_PTRCALL(DICTIONARY);
  506. DISASSEMBLE_PTRCALL(ARRAY);
  507. DISASSEMBLE_PTRCALL(PACKED_BYTE_ARRAY);
  508. DISASSEMBLE_PTRCALL(PACKED_INT32_ARRAY);
  509. DISASSEMBLE_PTRCALL(PACKED_INT64_ARRAY);
  510. DISASSEMBLE_PTRCALL(PACKED_FLOAT32_ARRAY);
  511. DISASSEMBLE_PTRCALL(PACKED_FLOAT64_ARRAY);
  512. DISASSEMBLE_PTRCALL(PACKED_STRING_ARRAY);
  513. DISASSEMBLE_PTRCALL(PACKED_VECTOR2_ARRAY);
  514. DISASSEMBLE_PTRCALL(PACKED_VECTOR3_ARRAY);
  515. DISASSEMBLE_PTRCALL(PACKED_COLOR_ARRAY);
  516. case OPCODE_CALL_BUILTIN_TYPE_VALIDATED: {
  517. int argc = _code_ptr[ip + 1 + instr_var_args];
  518. text += "call-builtin-method validated ";
  519. text += DADDR(2 + argc) + " = ";
  520. text += DADDR(1) + ".";
  521. text += "<unknown method>";
  522. text += "(";
  523. for (int i = 0; i < argc; i++) {
  524. if (i > 0)
  525. text += ", ";
  526. text += DADDR(1 + i);
  527. }
  528. text += ")";
  529. incr = 5 + argc;
  530. } break;
  531. case OPCODE_CALL_BUILT_IN: {
  532. text += "call-built-in ";
  533. int argc = _code_ptr[ip + 1 + instr_var_args];
  534. text += DADDR(1 + argc) + " = ";
  535. text += GDScriptFunctions::get_func_name(GDScriptFunctions::Function(_code_ptr[ip + 2 + instr_var_args]));
  536. text += "(";
  537. for (int i = 0; i < argc; i++) {
  538. if (i > 0)
  539. text += ", ";
  540. text += DADDR(1 + i);
  541. }
  542. text += ")";
  543. incr = 4 + argc;
  544. } break;
  545. case OPCODE_CALL_SELF_BASE: {
  546. text += "call-self-base ";
  547. int argc = _code_ptr[ip + 1 + instr_var_args];
  548. text += DADDR(2 + argc) + " = ";
  549. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  550. text += "(";
  551. for (int i = 0; i < argc; i++) {
  552. if (i > 0)
  553. text += ", ";
  554. text += DADDR(1 + i);
  555. }
  556. text += ")";
  557. incr = 4 + argc;
  558. } break;
  559. case OPCODE_AWAIT: {
  560. text += "await ";
  561. text += DADDR(1);
  562. incr += 2;
  563. } break;
  564. case OPCODE_AWAIT_RESUME: {
  565. text += "await resume ";
  566. text += DADDR(1);
  567. incr = 2;
  568. } break;
  569. case OPCODE_JUMP: {
  570. text += "jump ";
  571. text += itos(_code_ptr[ip + 1]);
  572. incr = 2;
  573. } break;
  574. case OPCODE_JUMP_IF: {
  575. text += "jump-if ";
  576. text += DADDR(1);
  577. text += " to ";
  578. text += itos(_code_ptr[ip + 2]);
  579. incr = 3;
  580. } break;
  581. case OPCODE_JUMP_IF_NOT: {
  582. text += "jump-if-not ";
  583. text += DADDR(1);
  584. text += " to ";
  585. text += itos(_code_ptr[ip + 2]);
  586. incr = 3;
  587. } break;
  588. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  589. text += "jump-to-default-argument ";
  590. incr = 1;
  591. } break;
  592. case OPCODE_RETURN: {
  593. text += "return ";
  594. text += DADDR(1);
  595. incr = 2;
  596. } break;
  597. #define DISASSEMBLE_ITERATE(m_type) \
  598. case OPCODE_ITERATE_##m_type: { \
  599. text += "for-loop (typed "; \
  600. text += #m_type; \
  601. text += ") "; \
  602. text += DADDR(3); \
  603. text += " in "; \
  604. text += DADDR(2); \
  605. text += " counter "; \
  606. text += DADDR(1); \
  607. text += " end "; \
  608. text += itos(_code_ptr[ip + 4]); \
  609. incr += 5; \
  610. } break
  611. #define DISASSEMBLE_ITERATE_BEGIN(m_type) \
  612. case OPCODE_ITERATE_BEGIN_##m_type: { \
  613. text += "for-init (typed "; \
  614. text += #m_type; \
  615. text += ") "; \
  616. text += DADDR(3); \
  617. text += " in "; \
  618. text += DADDR(2); \
  619. text += " counter "; \
  620. text += DADDR(1); \
  621. text += " end "; \
  622. text += itos(_code_ptr[ip + 4]); \
  623. incr += 5; \
  624. } break
  625. #define DISASSEMBLE_ITERATE_TYPES(m_macro) \
  626. m_macro(INT); \
  627. m_macro(FLOAT); \
  628. m_macro(VECTOR2); \
  629. m_macro(VECTOR2I); \
  630. m_macro(VECTOR3); \
  631. m_macro(VECTOR3I); \
  632. m_macro(STRING); \
  633. m_macro(DICTIONARY); \
  634. m_macro(ARRAY); \
  635. m_macro(PACKED_BYTE_ARRAY); \
  636. m_macro(PACKED_INT32_ARRAY); \
  637. m_macro(PACKED_INT64_ARRAY); \
  638. m_macro(PACKED_FLOAT32_ARRAY); \
  639. m_macro(PACKED_FLOAT64_ARRAY); \
  640. m_macro(PACKED_STRING_ARRAY); \
  641. m_macro(PACKED_VECTOR2_ARRAY); \
  642. m_macro(PACKED_VECTOR3_ARRAY); \
  643. m_macro(PACKED_COLOR_ARRAY); \
  644. m_macro(OBJECT)
  645. case OPCODE_ITERATE_BEGIN: {
  646. text += "for-init ";
  647. text += DADDR(3);
  648. text += " in ";
  649. text += DADDR(2);
  650. text += " counter ";
  651. text += DADDR(1);
  652. text += " end ";
  653. text += itos(_code_ptr[ip + 4]);
  654. incr += 5;
  655. } break;
  656. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE_BEGIN);
  657. case OPCODE_ITERATE: {
  658. text += "for-loop ";
  659. text += DADDR(2);
  660. text += " in ";
  661. text += DADDR(2);
  662. text += " counter ";
  663. text += DADDR(1);
  664. text += " end ";
  665. text += itos(_code_ptr[ip + 4]);
  666. incr += 5;
  667. } break;
  668. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE);
  669. case OPCODE_LINE: {
  670. int line = _code_ptr[ip + 1] - 1;
  671. if (line >= 0 && line < p_code_lines.size()) {
  672. text += "line ";
  673. text += itos(line + 1);
  674. text += ": ";
  675. text += p_code_lines[line];
  676. } else {
  677. text += "";
  678. }
  679. incr += 2;
  680. } break;
  681. case OPCODE_ASSERT: {
  682. text += "assert (";
  683. text += DADDR(1);
  684. text += ", ";
  685. text += DADDR(2);
  686. text += ")";
  687. incr += 3;
  688. } break;
  689. case OPCODE_BREAKPOINT: {
  690. text += "breakpoint";
  691. incr += 1;
  692. } break;
  693. case OPCODE_END: {
  694. text += "== END ==";
  695. incr += 1;
  696. } break;
  697. }
  698. ip += incr;
  699. if (text.get_string_length() > 0) {
  700. print_line(text.as_string());
  701. }
  702. }
  703. }
  704. #endif