gdscript_disassembler.cpp 22 KB

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