gdscript_disassembler.cpp 22 KB

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