gdscript_disassembler.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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_ARRAY: {
  291. text += "assign typed array ";
  292. text += DADDR(1);
  293. text += " = ";
  294. text += DADDR(2);
  295. incr += 3;
  296. } break;
  297. case OPCODE_ASSIGN_TYPED_NATIVE: {
  298. text += "assign typed native (";
  299. text += DADDR(3);
  300. text += ") ";
  301. text += DADDR(1);
  302. text += " = ";
  303. text += DADDR(2);
  304. incr += 4;
  305. } break;
  306. case OPCODE_ASSIGN_TYPED_SCRIPT: {
  307. Variant script = _constants_ptr[_code_ptr[ip + 3]];
  308. Script *sc = Object::cast_to<Script>(script.operator Object *());
  309. text += "assign typed script (";
  310. text += sc->get_path();
  311. text += ") ";
  312. text += DADDR(1);
  313. text += " = ";
  314. text += DADDR(2);
  315. incr += 4;
  316. } break;
  317. case OPCODE_CAST_TO_BUILTIN: {
  318. text += "cast builtin ";
  319. text += DADDR(2);
  320. text += " = ";
  321. text += DADDR(1);
  322. text += " as ";
  323. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 1]));
  324. incr += 4;
  325. } break;
  326. case OPCODE_CAST_TO_NATIVE: {
  327. text += "cast native ";
  328. text += DADDR(2);
  329. text += " = ";
  330. text += DADDR(1);
  331. text += " as ";
  332. text += DADDR(3);
  333. incr += 4;
  334. } break;
  335. case OPCODE_CAST_TO_SCRIPT: {
  336. text += "cast ";
  337. text += DADDR(2);
  338. text += " = ";
  339. text += DADDR(1);
  340. text += " as ";
  341. text += DADDR(3);
  342. incr += 4;
  343. } break;
  344. case OPCODE_CONSTRUCT: {
  345. Variant::Type t = Variant::Type(_code_ptr[ip + 3 + instr_var_args]);
  346. int argc = _code_ptr[ip + 1 + instr_var_args];
  347. text += "construct ";
  348. text += DADDR(1 + argc);
  349. text += " = ";
  350. text += Variant::get_type_name(t) + "(";
  351. for (int i = 0; i < argc; i++) {
  352. if (i > 0) {
  353. text += ", ";
  354. }
  355. text += DADDR(i + 1);
  356. }
  357. text += ")";
  358. incr = 3 + instr_var_args;
  359. } break;
  360. case OPCODE_CONSTRUCT_VALIDATED: {
  361. int argc = _code_ptr[ip + 1 + instr_var_args];
  362. text += "construct validated ";
  363. text += DADDR(1 + argc);
  364. text += " = ";
  365. text += "<unkown type>(";
  366. for (int i = 0; i < argc; i++) {
  367. if (i > 0) {
  368. text += ", ";
  369. }
  370. text += DADDR(i + 1);
  371. }
  372. text += ")";
  373. incr = 3 + instr_var_args;
  374. } break;
  375. case OPCODE_CONSTRUCT_ARRAY: {
  376. int argc = _code_ptr[ip + 1 + instr_var_args];
  377. text += " make_array ";
  378. text += DADDR(1 + argc);
  379. text += " = [";
  380. for (int i = 0; i < argc; i++) {
  381. if (i > 0) {
  382. text += ", ";
  383. }
  384. text += DADDR(1 + i);
  385. }
  386. text += "]";
  387. incr += 3 + argc;
  388. } break;
  389. case OPCODE_CONSTRUCT_TYPED_ARRAY: {
  390. int argc = _code_ptr[ip + 1 + instr_var_args];
  391. Ref<Script> script_type = get_constant(_code_ptr[ip + argc + 2]);
  392. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + argc + 4];
  393. StringName native_type = get_global_name(_code_ptr[ip + argc + 5]);
  394. String type_name;
  395. if (script_type.is_valid() && script_type->is_valid()) {
  396. type_name = script_type->get_path();
  397. } else if (native_type != StringName()) {
  398. type_name = native_type;
  399. } else {
  400. type_name = Variant::get_type_name(builtin_type);
  401. }
  402. text += " make_typed_array (";
  403. text += type_name;
  404. text += ") ";
  405. text += DADDR(1 + argc);
  406. text += " = [";
  407. for (int i = 0; i < argc; i++) {
  408. if (i > 0) {
  409. text += ", ";
  410. }
  411. text += DADDR(1 + i);
  412. }
  413. text += "]";
  414. incr += 3 + argc;
  415. } break;
  416. case OPCODE_CONSTRUCT_DICTIONARY: {
  417. int argc = _code_ptr[ip + 1 + instr_var_args];
  418. text += "make_dict ";
  419. text += DADDR(1 + argc * 2);
  420. text += " = {";
  421. for (int i = 0; i < argc; i++) {
  422. if (i > 0) {
  423. text += ", ";
  424. }
  425. text += DADDR(1 + i * 2 + 0);
  426. text += ": ";
  427. text += DADDR(1 + i * 2 + 1);
  428. }
  429. text += "}";
  430. incr += 3 + argc * 2;
  431. } break;
  432. case OPCODE_CALL:
  433. case OPCODE_CALL_RETURN:
  434. case OPCODE_CALL_ASYNC: {
  435. bool ret = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_RETURN;
  436. bool async = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_ASYNC;
  437. if (ret) {
  438. text += "call-ret ";
  439. } else if (async) {
  440. text += "call-async ";
  441. } else {
  442. text += "call ";
  443. }
  444. int argc = _code_ptr[ip + 1 + instr_var_args];
  445. if (ret || async) {
  446. text += DADDR(2 + argc) + " = ";
  447. }
  448. text += DADDR(1 + argc) + ".";
  449. text += String(_global_names_ptr[_code_ptr[ip + 2 + instr_var_args]]);
  450. text += "(";
  451. for (int i = 0; i < argc; i++) {
  452. if (i > 0) {
  453. text += ", ";
  454. }
  455. text += DADDR(1 + i);
  456. }
  457. text += ")";
  458. incr = 5 + argc;
  459. } break;
  460. case OPCODE_CALL_METHOD_BIND:
  461. case OPCODE_CALL_METHOD_BIND_RET: {
  462. bool ret = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_METHOD_BIND_RET;
  463. if (ret) {
  464. text += "call-method_bind-ret ";
  465. } else {
  466. text += "call-method_bind ";
  467. }
  468. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  469. int argc = _code_ptr[ip + 1 + instr_var_args];
  470. if (ret) {
  471. text += DADDR(2 + argc) + " = ";
  472. }
  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. }
  480. text += DADDR(1 + i);
  481. }
  482. text += ")";
  483. incr = 5 + argc;
  484. } break;
  485. case OPCODE_CALL_PTRCALL_NO_RETURN: {
  486. text += "call-ptrcall (no return) ";
  487. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  488. int argc = _code_ptr[ip + 1 + instr_var_args];
  489. text += DADDR(1 + argc) + ".";
  490. text += method->get_name();
  491. text += "(";
  492. for (int i = 0; i < argc; i++) {
  493. if (i > 0) {
  494. text += ", ";
  495. }
  496. text += DADDR(1 + i);
  497. }
  498. text += ")";
  499. incr = 5 + argc;
  500. } break;
  501. #define DISASSEMBLE_PTRCALL(m_type) \
  502. case OPCODE_CALL_PTRCALL_##m_type: { \
  503. text += "call-ptrcall (return "; \
  504. text += #m_type; \
  505. text += ") "; \
  506. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]]; \
  507. int argc = _code_ptr[ip + 1 + instr_var_args]; \
  508. text += DADDR(2 + argc) + " = "; \
  509. text += DADDR(1 + argc) + "."; \
  510. text += method->get_name(); \
  511. text += "("; \
  512. for (int i = 0; i < argc; i++) { \
  513. if (i > 0) \
  514. text += ", "; \
  515. text += DADDR(1 + i); \
  516. } \
  517. text += ")"; \
  518. incr = 5 + argc; \
  519. } break
  520. DISASSEMBLE_PTRCALL(BOOL);
  521. DISASSEMBLE_PTRCALL(INT);
  522. DISASSEMBLE_PTRCALL(FLOAT);
  523. DISASSEMBLE_PTRCALL(STRING);
  524. DISASSEMBLE_PTRCALL(VECTOR2);
  525. DISASSEMBLE_PTRCALL(VECTOR2I);
  526. DISASSEMBLE_PTRCALL(RECT2);
  527. DISASSEMBLE_PTRCALL(RECT2I);
  528. DISASSEMBLE_PTRCALL(VECTOR3);
  529. DISASSEMBLE_PTRCALL(VECTOR3I);
  530. DISASSEMBLE_PTRCALL(TRANSFORM2D);
  531. DISASSEMBLE_PTRCALL(PLANE);
  532. DISASSEMBLE_PTRCALL(AABB);
  533. DISASSEMBLE_PTRCALL(BASIS);
  534. DISASSEMBLE_PTRCALL(TRANSFORM);
  535. DISASSEMBLE_PTRCALL(COLOR);
  536. DISASSEMBLE_PTRCALL(STRING_NAME);
  537. DISASSEMBLE_PTRCALL(NODE_PATH);
  538. DISASSEMBLE_PTRCALL(RID);
  539. DISASSEMBLE_PTRCALL(QUAT);
  540. DISASSEMBLE_PTRCALL(OBJECT);
  541. DISASSEMBLE_PTRCALL(CALLABLE);
  542. DISASSEMBLE_PTRCALL(SIGNAL);
  543. DISASSEMBLE_PTRCALL(DICTIONARY);
  544. DISASSEMBLE_PTRCALL(ARRAY);
  545. DISASSEMBLE_PTRCALL(PACKED_BYTE_ARRAY);
  546. DISASSEMBLE_PTRCALL(PACKED_INT32_ARRAY);
  547. DISASSEMBLE_PTRCALL(PACKED_INT64_ARRAY);
  548. DISASSEMBLE_PTRCALL(PACKED_FLOAT32_ARRAY);
  549. DISASSEMBLE_PTRCALL(PACKED_FLOAT64_ARRAY);
  550. DISASSEMBLE_PTRCALL(PACKED_STRING_ARRAY);
  551. DISASSEMBLE_PTRCALL(PACKED_VECTOR2_ARRAY);
  552. DISASSEMBLE_PTRCALL(PACKED_VECTOR3_ARRAY);
  553. DISASSEMBLE_PTRCALL(PACKED_COLOR_ARRAY);
  554. case OPCODE_CALL_BUILTIN_TYPE_VALIDATED: {
  555. int argc = _code_ptr[ip + 1 + instr_var_args];
  556. text += "call-builtin-method validated ";
  557. text += DADDR(2 + argc) + " = ";
  558. text += DADDR(1) + ".";
  559. text += "<unknown method>";
  560. text += "(";
  561. for (int i = 0; i < argc; i++) {
  562. if (i > 0) {
  563. text += ", ";
  564. }
  565. text += DADDR(1 + i);
  566. }
  567. text += ")";
  568. incr = 5 + argc;
  569. } break;
  570. case OPCODE_CALL_UTILITY: {
  571. text += "call-utility ";
  572. int argc = _code_ptr[ip + 1 + instr_var_args];
  573. text += DADDR(1 + 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. }
  580. text += DADDR(1 + i);
  581. }
  582. text += ")";
  583. incr = 4 + argc;
  584. } break;
  585. case OPCODE_CALL_UTILITY_VALIDATED: {
  586. text += "call-utility ";
  587. int argc = _code_ptr[ip + 1 + instr_var_args];
  588. text += DADDR(1 + argc) + " = ";
  589. text += "<unkown function>";
  590. text += "(";
  591. for (int i = 0; i < argc; i++) {
  592. if (i > 0) {
  593. text += ", ";
  594. }
  595. text += DADDR(1 + i);
  596. }
  597. text += ")";
  598. incr = 4 + argc;
  599. } break;
  600. case OPCODE_CALL_GDSCRIPT_UTILITY: {
  601. text += "call-gscript-utility ";
  602. int argc = _code_ptr[ip + 1 + instr_var_args];
  603. text += DADDR(1 + argc) + " = ";
  604. text += "<unknown function>";
  605. text += "(";
  606. for (int i = 0; i < argc; i++) {
  607. if (i > 0) {
  608. text += ", ";
  609. }
  610. text += DADDR(1 + i);
  611. }
  612. text += ")";
  613. incr = 4 + argc;
  614. } break;
  615. case OPCODE_CALL_SELF_BASE: {
  616. text += "call-self-base ";
  617. int argc = _code_ptr[ip + 1 + instr_var_args];
  618. text += DADDR(2 + argc) + " = ";
  619. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  620. text += "(";
  621. for (int i = 0; i < argc; i++) {
  622. if (i > 0) {
  623. text += ", ";
  624. }
  625. text += DADDR(1 + i);
  626. }
  627. text += ")";
  628. incr = 4 + argc;
  629. } break;
  630. case OPCODE_AWAIT: {
  631. text += "await ";
  632. text += DADDR(1);
  633. incr += 2;
  634. } break;
  635. case OPCODE_AWAIT_RESUME: {
  636. text += "await resume ";
  637. text += DADDR(1);
  638. incr = 2;
  639. } break;
  640. case OPCODE_JUMP: {
  641. text += "jump ";
  642. text += itos(_code_ptr[ip + 1]);
  643. incr = 2;
  644. } break;
  645. case OPCODE_JUMP_IF: {
  646. text += "jump-if ";
  647. text += DADDR(1);
  648. text += " to ";
  649. text += itos(_code_ptr[ip + 2]);
  650. incr = 3;
  651. } break;
  652. case OPCODE_JUMP_IF_NOT: {
  653. text += "jump-if-not ";
  654. text += DADDR(1);
  655. text += " to ";
  656. text += itos(_code_ptr[ip + 2]);
  657. incr = 3;
  658. } break;
  659. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  660. text += "jump-to-default-argument ";
  661. incr = 1;
  662. } break;
  663. case OPCODE_RETURN: {
  664. text += "return ";
  665. text += DADDR(1);
  666. incr = 2;
  667. } break;
  668. #define DISASSEMBLE_ITERATE(m_type) \
  669. case OPCODE_ITERATE_##m_type: { \
  670. text += "for-loop (typed "; \
  671. text += #m_type; \
  672. text += ") "; \
  673. text += DADDR(3); \
  674. text += " in "; \
  675. text += DADDR(2); \
  676. text += " counter "; \
  677. text += DADDR(1); \
  678. text += " end "; \
  679. text += itos(_code_ptr[ip + 4]); \
  680. incr += 5; \
  681. } break
  682. #define DISASSEMBLE_ITERATE_BEGIN(m_type) \
  683. case OPCODE_ITERATE_BEGIN_##m_type: { \
  684. text += "for-init (typed "; \
  685. text += #m_type; \
  686. text += ") "; \
  687. text += DADDR(3); \
  688. text += " in "; \
  689. text += DADDR(2); \
  690. text += " counter "; \
  691. text += DADDR(1); \
  692. text += " end "; \
  693. text += itos(_code_ptr[ip + 4]); \
  694. incr += 5; \
  695. } break
  696. #define DISASSEMBLE_ITERATE_TYPES(m_macro) \
  697. m_macro(INT); \
  698. m_macro(FLOAT); \
  699. m_macro(VECTOR2); \
  700. m_macro(VECTOR2I); \
  701. m_macro(VECTOR3); \
  702. m_macro(VECTOR3I); \
  703. m_macro(STRING); \
  704. m_macro(DICTIONARY); \
  705. m_macro(ARRAY); \
  706. m_macro(PACKED_BYTE_ARRAY); \
  707. m_macro(PACKED_INT32_ARRAY); \
  708. m_macro(PACKED_INT64_ARRAY); \
  709. m_macro(PACKED_FLOAT32_ARRAY); \
  710. m_macro(PACKED_FLOAT64_ARRAY); \
  711. m_macro(PACKED_STRING_ARRAY); \
  712. m_macro(PACKED_VECTOR2_ARRAY); \
  713. m_macro(PACKED_VECTOR3_ARRAY); \
  714. m_macro(PACKED_COLOR_ARRAY); \
  715. m_macro(OBJECT)
  716. case OPCODE_ITERATE_BEGIN: {
  717. text += "for-init ";
  718. text += DADDR(3);
  719. text += " in ";
  720. text += DADDR(2);
  721. text += " counter ";
  722. text += DADDR(1);
  723. text += " end ";
  724. text += itos(_code_ptr[ip + 4]);
  725. incr += 5;
  726. } break;
  727. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE_BEGIN);
  728. case OPCODE_ITERATE: {
  729. text += "for-loop ";
  730. text += DADDR(2);
  731. text += " in ";
  732. text += DADDR(2);
  733. text += " counter ";
  734. text += DADDR(1);
  735. text += " end ";
  736. text += itos(_code_ptr[ip + 4]);
  737. incr += 5;
  738. } break;
  739. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE);
  740. case OPCODE_LINE: {
  741. int line = _code_ptr[ip + 1] - 1;
  742. if (line >= 0 && line < p_code_lines.size()) {
  743. text += "line ";
  744. text += itos(line + 1);
  745. text += ": ";
  746. text += p_code_lines[line];
  747. } else {
  748. text += "";
  749. }
  750. incr += 2;
  751. } break;
  752. case OPCODE_ASSERT: {
  753. text += "assert (";
  754. text += DADDR(1);
  755. text += ", ";
  756. text += DADDR(2);
  757. text += ")";
  758. incr += 3;
  759. } break;
  760. case OPCODE_BREAKPOINT: {
  761. text += "breakpoint";
  762. incr += 1;
  763. } break;
  764. case OPCODE_END: {
  765. text += "== END ==";
  766. incr += 1;
  767. } break;
  768. }
  769. ip += incr;
  770. if (text.get_string_length() > 0) {
  771. print_line(text.as_string());
  772. }
  773. }
  774. }
  775. #endif