gdscript_disassembler.cpp 22 KB

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