2
0

gdscript_disassembler.cpp 24 KB

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