gdscript_disassembler.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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_MEMBER: {
  67. return "member(" + p_script->debug_get_member_by_index(addr) + ")";
  68. } break;
  69. case GDScriptFunction::ADDR_TYPE_CONSTANT: {
  70. return "const(" + _get_variant_string(p_function.get_constant(addr)) + ")";
  71. } break;
  72. case GDScriptFunction::ADDR_TYPE_STACK: {
  73. switch (addr) {
  74. case GDScriptFunction::ADDR_STACK_SELF:
  75. return "self";
  76. case GDScriptFunction::ADDR_STACK_CLASS:
  77. return "class";
  78. case GDScriptFunction::ADDR_STACK_NIL:
  79. return "nil";
  80. default:
  81. return "stack(" + itos(addr) + ")";
  82. }
  83. } break;
  84. }
  85. return "<err>";
  86. }
  87. void GDScriptFunction::disassemble(const Vector<String> &p_code_lines) const {
  88. #define DADDR(m_ip) (_disassemble_address(_script, *this, _code_ptr[ip + m_ip]))
  89. for (int ip = 0; ip < _code_size;) {
  90. StringBuilder text;
  91. int incr = 0;
  92. text += " ";
  93. text += itos(ip);
  94. text += ": ";
  95. // This makes the compiler complain if some opcode is unchecked in the switch.
  96. Opcode code = Opcode(_code_ptr[ip] & INSTR_MASK);
  97. int instr_var_args = (_code_ptr[ip] & INSTR_ARGS_MASK) >> INSTR_BITS;
  98. switch (code) {
  99. case OPCODE_OPERATOR: {
  100. int operation = _code_ptr[ip + 4];
  101. text += "operator ";
  102. text += DADDR(3);
  103. text += " = ";
  104. text += DADDR(1);
  105. text += " ";
  106. text += Variant::get_operator_name(Variant::Operator(operation));
  107. text += " ";
  108. text += DADDR(2);
  109. incr += 5;
  110. } break;
  111. case OPCODE_OPERATOR_VALIDATED: {
  112. text += "validated operator ";
  113. text += DADDR(3);
  114. text += " = ";
  115. text += DADDR(1);
  116. text += " <operator function> ";
  117. text += DADDR(2);
  118. incr += 5;
  119. } break;
  120. case OPCODE_EXTENDS_TEST: {
  121. text += "is object ";
  122. text += DADDR(3);
  123. text += " = ";
  124. text += DADDR(1);
  125. text += " is ";
  126. text += DADDR(2);
  127. incr += 4;
  128. } break;
  129. case OPCODE_IS_BUILTIN: {
  130. text += "is builtin ";
  131. text += DADDR(2);
  132. text += " = ";
  133. text += DADDR(1);
  134. text += " is ";
  135. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 3]));
  136. incr += 4;
  137. } break;
  138. case OPCODE_SET_KEYED: {
  139. text += "set keyed ";
  140. text += DADDR(1);
  141. text += "[";
  142. text += DADDR(2);
  143. text += "] = ";
  144. text += DADDR(3);
  145. incr += 4;
  146. } break;
  147. case OPCODE_SET_KEYED_VALIDATED: {
  148. text += "set keyed validated ";
  149. text += DADDR(1);
  150. text += "[";
  151. text += DADDR(2);
  152. text += "] = ";
  153. text += DADDR(3);
  154. incr += 5;
  155. } break;
  156. case OPCODE_SET_INDEXED_VALIDATED: {
  157. text += "set indexed validated ";
  158. text += DADDR(1);
  159. text += "[";
  160. text += DADDR(2);
  161. text += "] = ";
  162. text += DADDR(3);
  163. incr += 5;
  164. } break;
  165. case OPCODE_GET_KEYED: {
  166. text += "get keyed ";
  167. text += DADDR(3);
  168. text += " = ";
  169. text += DADDR(1);
  170. text += "[";
  171. text += DADDR(2);
  172. text += "]";
  173. incr += 4;
  174. } break;
  175. case OPCODE_GET_KEYED_VALIDATED: {
  176. text += "get keyed validated ";
  177. text += DADDR(3);
  178. text += " = ";
  179. text += DADDR(1);
  180. text += "[";
  181. text += DADDR(2);
  182. text += "]";
  183. incr += 5;
  184. } break;
  185. case OPCODE_GET_INDEXED_VALIDATED: {
  186. text += "get indexed validated ";
  187. text += DADDR(3);
  188. text += " = ";
  189. text += DADDR(1);
  190. text += "[";
  191. text += DADDR(2);
  192. text += "]";
  193. incr += 5;
  194. } break;
  195. case OPCODE_SET_NAMED: {
  196. text += "set_named ";
  197. text += DADDR(1);
  198. text += "[\"";
  199. text += _global_names_ptr[_code_ptr[ip + 3]];
  200. text += "\"] = ";
  201. text += DADDR(2);
  202. incr += 4;
  203. } break;
  204. case OPCODE_SET_NAMED_VALIDATED: {
  205. text += "set_named validated ";
  206. text += DADDR(1);
  207. text += "[\"";
  208. text += "<unknown name>";
  209. text += "\"] = ";
  210. text += DADDR(2);
  211. incr += 4;
  212. } break;
  213. case OPCODE_GET_NAMED: {
  214. text += "get_named ";
  215. text += DADDR(2);
  216. text += " = ";
  217. text += DADDR(1);
  218. text += "[\"";
  219. text += _global_names_ptr[_code_ptr[ip + 3]];
  220. text += "\"]";
  221. incr += 4;
  222. } break;
  223. case OPCODE_GET_NAMED_VALIDATED: {
  224. text += "get_named validated ";
  225. text += DADDR(2);
  226. text += " = ";
  227. text += DADDR(1);
  228. text += "[\"";
  229. text += "<unknown name>";
  230. text += "\"]";
  231. incr += 4;
  232. } break;
  233. case OPCODE_SET_MEMBER: {
  234. text += "set_member ";
  235. text += "[\"";
  236. text += _global_names_ptr[_code_ptr[ip + 2]];
  237. text += "\"] = ";
  238. text += DADDR(1);
  239. incr += 3;
  240. } break;
  241. case OPCODE_GET_MEMBER: {
  242. text += "get_member ";
  243. text += DADDR(1);
  244. text += " = ";
  245. text += "[\"";
  246. text += _global_names_ptr[_code_ptr[ip + 2]];
  247. text += "\"]";
  248. incr += 3;
  249. } break;
  250. case OPCODE_ASSIGN: {
  251. text += "assign ";
  252. text += DADDR(1);
  253. text += " = ";
  254. text += DADDR(2);
  255. incr += 3;
  256. } break;
  257. case OPCODE_ASSIGN_TRUE: {
  258. text += "assign ";
  259. text += DADDR(1);
  260. text += " = true";
  261. incr += 2;
  262. } break;
  263. case OPCODE_ASSIGN_FALSE: {
  264. text += "assign ";
  265. text += DADDR(1);
  266. text += " = false";
  267. incr += 2;
  268. } break;
  269. case OPCODE_ASSIGN_TYPED_BUILTIN: {
  270. text += "assign typed builtin (";
  271. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 3]);
  272. text += ") ";
  273. text += DADDR(1);
  274. text += " = ";
  275. text += DADDR(2);
  276. incr += 4;
  277. } break;
  278. case OPCODE_ASSIGN_TYPED_ARRAY: {
  279. text += "assign typed array ";
  280. text += DADDR(1);
  281. text += " = ";
  282. text += DADDR(2);
  283. incr += 3;
  284. } break;
  285. case OPCODE_ASSIGN_TYPED_NATIVE: {
  286. text += "assign typed native (";
  287. text += DADDR(3);
  288. text += ") ";
  289. text += DADDR(1);
  290. text += " = ";
  291. text += DADDR(2);
  292. incr += 4;
  293. } break;
  294. case OPCODE_ASSIGN_TYPED_SCRIPT: {
  295. Variant script = _constants_ptr[_code_ptr[ip + 3]];
  296. Script *sc = Object::cast_to<Script>(script.operator Object *());
  297. text += "assign typed script (";
  298. text += sc->get_path();
  299. text += ") ";
  300. text += DADDR(1);
  301. text += " = ";
  302. text += DADDR(2);
  303. incr += 4;
  304. } break;
  305. case OPCODE_CAST_TO_BUILTIN: {
  306. text += "cast builtin ";
  307. text += DADDR(2);
  308. text += " = ";
  309. text += DADDR(1);
  310. text += " as ";
  311. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 1]));
  312. incr += 4;
  313. } break;
  314. case OPCODE_CAST_TO_NATIVE: {
  315. text += "cast native ";
  316. text += DADDR(2);
  317. text += " = ";
  318. text += DADDR(1);
  319. text += " as ";
  320. text += DADDR(3);
  321. incr += 4;
  322. } break;
  323. case OPCODE_CAST_TO_SCRIPT: {
  324. text += "cast ";
  325. text += DADDR(2);
  326. text += " = ";
  327. text += DADDR(1);
  328. text += " as ";
  329. text += DADDR(3);
  330. incr += 4;
  331. } break;
  332. case OPCODE_CONSTRUCT: {
  333. Variant::Type t = Variant::Type(_code_ptr[ip + 3 + instr_var_args]);
  334. int argc = _code_ptr[ip + 1 + instr_var_args];
  335. text += "construct ";
  336. text += DADDR(1 + argc);
  337. text += " = ";
  338. text += Variant::get_type_name(t) + "(";
  339. for (int i = 0; i < argc; i++) {
  340. if (i > 0) {
  341. text += ", ";
  342. }
  343. text += DADDR(i + 1);
  344. }
  345. text += ")";
  346. incr = 3 + instr_var_args;
  347. } break;
  348. case OPCODE_CONSTRUCT_VALIDATED: {
  349. int argc = _code_ptr[ip + 1 + instr_var_args];
  350. text += "construct validated ";
  351. text += DADDR(1 + argc);
  352. text += " = ";
  353. text += "<unkown type>(";
  354. for (int i = 0; i < argc; i++) {
  355. if (i > 0) {
  356. text += ", ";
  357. }
  358. text += DADDR(i + 1);
  359. }
  360. text += ")";
  361. incr = 3 + instr_var_args;
  362. } break;
  363. case OPCODE_CONSTRUCT_ARRAY: {
  364. int argc = _code_ptr[ip + 1 + instr_var_args];
  365. text += " make_array ";
  366. text += DADDR(1 + argc);
  367. text += " = [";
  368. for (int i = 0; i < argc; i++) {
  369. if (i > 0) {
  370. text += ", ";
  371. }
  372. text += DADDR(1 + i);
  373. }
  374. text += "]";
  375. incr += 3 + argc;
  376. } break;
  377. case OPCODE_CONSTRUCT_TYPED_ARRAY: {
  378. int argc = _code_ptr[ip + 1 + instr_var_args];
  379. Ref<Script> script_type = get_constant(_code_ptr[ip + argc + 2]);
  380. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + argc + 4];
  381. StringName native_type = get_global_name(_code_ptr[ip + argc + 5]);
  382. String type_name;
  383. if (script_type.is_valid() && script_type->is_valid()) {
  384. type_name = script_type->get_path();
  385. } else if (native_type != StringName()) {
  386. type_name = native_type;
  387. } else {
  388. type_name = Variant::get_type_name(builtin_type);
  389. }
  390. text += " make_typed_array (";
  391. text += type_name;
  392. text += ") ";
  393. text += DADDR(1 + argc);
  394. text += " = [";
  395. for (int i = 0; i < argc; i++) {
  396. if (i > 0) {
  397. text += ", ";
  398. }
  399. text += DADDR(1 + i);
  400. }
  401. text += "]";
  402. incr += 3 + argc;
  403. } break;
  404. case OPCODE_CONSTRUCT_DICTIONARY: {
  405. int argc = _code_ptr[ip + 1 + instr_var_args];
  406. text += "make_dict ";
  407. text += DADDR(1 + argc * 2);
  408. text += " = {";
  409. for (int i = 0; i < argc; i++) {
  410. if (i > 0) {
  411. text += ", ";
  412. }
  413. text += DADDR(1 + i * 2 + 0);
  414. text += ": ";
  415. text += DADDR(1 + i * 2 + 1);
  416. }
  417. text += "}";
  418. incr += 3 + argc * 2;
  419. } break;
  420. case OPCODE_CALL:
  421. case OPCODE_CALL_RETURN:
  422. case OPCODE_CALL_ASYNC: {
  423. bool ret = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_RETURN;
  424. bool async = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_ASYNC;
  425. if (ret) {
  426. text += "call-ret ";
  427. } else if (async) {
  428. text += "call-async ";
  429. } else {
  430. text += "call ";
  431. }
  432. int argc = _code_ptr[ip + 1 + instr_var_args];
  433. if (ret || async) {
  434. text += DADDR(2 + argc) + " = ";
  435. }
  436. text += DADDR(1 + argc) + ".";
  437. text += String(_global_names_ptr[_code_ptr[ip + 2 + instr_var_args]]);
  438. text += "(";
  439. for (int i = 0; i < argc; i++) {
  440. if (i > 0) {
  441. text += ", ";
  442. }
  443. text += DADDR(1 + i);
  444. }
  445. text += ")";
  446. incr = 5 + argc;
  447. } break;
  448. case OPCODE_CALL_METHOD_BIND:
  449. case OPCODE_CALL_METHOD_BIND_RET: {
  450. bool ret = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_METHOD_BIND_RET;
  451. if (ret) {
  452. text += "call-method_bind-ret ";
  453. } else {
  454. text += "call-method_bind ";
  455. }
  456. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  457. int argc = _code_ptr[ip + 1 + instr_var_args];
  458. if (ret) {
  459. text += DADDR(2 + argc) + " = ";
  460. }
  461. text += DADDR(1 + argc) + ".";
  462. text += method->get_name();
  463. text += "(";
  464. for (int i = 0; i < argc; i++) {
  465. if (i > 0) {
  466. text += ", ";
  467. }
  468. text += DADDR(1 + i);
  469. }
  470. text += ")";
  471. incr = 5 + argc;
  472. } break;
  473. case OPCODE_CALL_PTRCALL_NO_RETURN: {
  474. text += "call-ptrcall (no return) ";
  475. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  476. int argc = _code_ptr[ip + 1 + instr_var_args];
  477. text += DADDR(1 + argc) + ".";
  478. text += method->get_name();
  479. text += "(";
  480. for (int i = 0; i < argc; i++) {
  481. if (i > 0) {
  482. text += ", ";
  483. }
  484. text += DADDR(1 + i);
  485. }
  486. text += ")";
  487. incr = 5 + argc;
  488. } break;
  489. #define DISASSEMBLE_PTRCALL(m_type) \
  490. case OPCODE_CALL_PTRCALL_##m_type: { \
  491. text += "call-ptrcall (return "; \
  492. text += #m_type; \
  493. text += ") "; \
  494. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]]; \
  495. int argc = _code_ptr[ip + 1 + instr_var_args]; \
  496. text += DADDR(2 + argc) + " = "; \
  497. text += DADDR(1 + argc) + "."; \
  498. text += method->get_name(); \
  499. text += "("; \
  500. for (int i = 0; i < argc; i++) { \
  501. if (i > 0) \
  502. text += ", "; \
  503. text += DADDR(1 + i); \
  504. } \
  505. text += ")"; \
  506. incr = 5 + argc; \
  507. } break
  508. DISASSEMBLE_PTRCALL(BOOL);
  509. DISASSEMBLE_PTRCALL(INT);
  510. DISASSEMBLE_PTRCALL(FLOAT);
  511. DISASSEMBLE_PTRCALL(STRING);
  512. DISASSEMBLE_PTRCALL(VECTOR2);
  513. DISASSEMBLE_PTRCALL(VECTOR2I);
  514. DISASSEMBLE_PTRCALL(RECT2);
  515. DISASSEMBLE_PTRCALL(RECT2I);
  516. DISASSEMBLE_PTRCALL(VECTOR3);
  517. DISASSEMBLE_PTRCALL(VECTOR3I);
  518. DISASSEMBLE_PTRCALL(TRANSFORM2D);
  519. DISASSEMBLE_PTRCALL(PLANE);
  520. DISASSEMBLE_PTRCALL(AABB);
  521. DISASSEMBLE_PTRCALL(BASIS);
  522. DISASSEMBLE_PTRCALL(TRANSFORM);
  523. DISASSEMBLE_PTRCALL(COLOR);
  524. DISASSEMBLE_PTRCALL(STRING_NAME);
  525. DISASSEMBLE_PTRCALL(NODE_PATH);
  526. DISASSEMBLE_PTRCALL(RID);
  527. DISASSEMBLE_PTRCALL(QUAT);
  528. DISASSEMBLE_PTRCALL(OBJECT);
  529. DISASSEMBLE_PTRCALL(CALLABLE);
  530. DISASSEMBLE_PTRCALL(SIGNAL);
  531. DISASSEMBLE_PTRCALL(DICTIONARY);
  532. DISASSEMBLE_PTRCALL(ARRAY);
  533. DISASSEMBLE_PTRCALL(PACKED_BYTE_ARRAY);
  534. DISASSEMBLE_PTRCALL(PACKED_INT32_ARRAY);
  535. DISASSEMBLE_PTRCALL(PACKED_INT64_ARRAY);
  536. DISASSEMBLE_PTRCALL(PACKED_FLOAT32_ARRAY);
  537. DISASSEMBLE_PTRCALL(PACKED_FLOAT64_ARRAY);
  538. DISASSEMBLE_PTRCALL(PACKED_STRING_ARRAY);
  539. DISASSEMBLE_PTRCALL(PACKED_VECTOR2_ARRAY);
  540. DISASSEMBLE_PTRCALL(PACKED_VECTOR3_ARRAY);
  541. DISASSEMBLE_PTRCALL(PACKED_COLOR_ARRAY);
  542. case OPCODE_CALL_BUILTIN_TYPE_VALIDATED: {
  543. int argc = _code_ptr[ip + 1 + instr_var_args];
  544. text += "call-builtin-method validated ";
  545. text += DADDR(2 + argc) + " = ";
  546. text += DADDR(1) + ".";
  547. text += "<unknown method>";
  548. text += "(";
  549. for (int i = 0; i < argc; i++) {
  550. if (i > 0) {
  551. text += ", ";
  552. }
  553. text += DADDR(1 + i);
  554. }
  555. text += ")";
  556. incr = 5 + argc;
  557. } break;
  558. case OPCODE_CALL_UTILITY: {
  559. text += "call-utility ";
  560. int argc = _code_ptr[ip + 1 + instr_var_args];
  561. text += DADDR(1 + argc) + " = ";
  562. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  563. text += "(";
  564. for (int i = 0; i < argc; i++) {
  565. if (i > 0) {
  566. text += ", ";
  567. }
  568. text += DADDR(1 + i);
  569. }
  570. text += ")";
  571. incr = 4 + argc;
  572. } break;
  573. case OPCODE_CALL_UTILITY_VALIDATED: {
  574. text += "call-utility ";
  575. int argc = _code_ptr[ip + 1 + instr_var_args];
  576. text += DADDR(1 + argc) + " = ";
  577. text += "<unkown function>";
  578. text += "(";
  579. for (int i = 0; i < argc; i++) {
  580. if (i > 0) {
  581. text += ", ";
  582. }
  583. text += DADDR(1 + i);
  584. }
  585. text += ")";
  586. incr = 4 + argc;
  587. } break;
  588. case OPCODE_CALL_GDSCRIPT_UTILITY: {
  589. text += "call-gscript-utility ";
  590. int argc = _code_ptr[ip + 1 + instr_var_args];
  591. text += DADDR(1 + argc) + " = ";
  592. text += "<unknown function>";
  593. text += "(";
  594. for (int i = 0; i < argc; i++) {
  595. if (i > 0) {
  596. text += ", ";
  597. }
  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. }
  613. text += DADDR(1 + i);
  614. }
  615. text += ")";
  616. incr = 4 + argc;
  617. } break;
  618. case OPCODE_AWAIT: {
  619. text += "await ";
  620. text += DADDR(1);
  621. incr += 2;
  622. } break;
  623. case OPCODE_AWAIT_RESUME: {
  624. text += "await resume ";
  625. text += DADDR(1);
  626. incr = 2;
  627. } break;
  628. case OPCODE_JUMP: {
  629. text += "jump ";
  630. text += itos(_code_ptr[ip + 1]);
  631. incr = 2;
  632. } break;
  633. case OPCODE_JUMP_IF: {
  634. text += "jump-if ";
  635. text += DADDR(1);
  636. text += " to ";
  637. text += itos(_code_ptr[ip + 2]);
  638. incr = 3;
  639. } break;
  640. case OPCODE_JUMP_IF_NOT: {
  641. text += "jump-if-not ";
  642. text += DADDR(1);
  643. text += " to ";
  644. text += itos(_code_ptr[ip + 2]);
  645. incr = 3;
  646. } break;
  647. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  648. text += "jump-to-default-argument ";
  649. incr = 1;
  650. } break;
  651. case OPCODE_RETURN: {
  652. text += "return ";
  653. text += DADDR(1);
  654. incr = 2;
  655. } break;
  656. case OPCODE_RETURN_TYPED_BUILTIN: {
  657. text += "return typed builtin (";
  658. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 2]);
  659. text += ") ";
  660. text += DADDR(1);
  661. incr += 3;
  662. } break;
  663. case OPCODE_RETURN_TYPED_ARRAY: {
  664. text += "return typed array ";
  665. text += DADDR(1);
  666. incr += 5;
  667. } break;
  668. case OPCODE_RETURN_TYPED_NATIVE: {
  669. text += "return typed native (";
  670. text += DADDR(2);
  671. text += ") ";
  672. text += DADDR(1);
  673. incr += 3;
  674. } break;
  675. case OPCODE_RETURN_TYPED_SCRIPT: {
  676. Variant script = _constants_ptr[_code_ptr[ip + 2]];
  677. Script *sc = Object::cast_to<Script>(script.operator Object *());
  678. text += "return typed script (";
  679. text += sc->get_path();
  680. text += ") ";
  681. text += DADDR(1);
  682. incr += 3;
  683. } break;
  684. #define DISASSEMBLE_ITERATE(m_type) \
  685. case OPCODE_ITERATE_##m_type: { \
  686. text += "for-loop (typed "; \
  687. text += #m_type; \
  688. text += ") "; \
  689. text += DADDR(3); \
  690. text += " in "; \
  691. text += DADDR(2); \
  692. text += " counter "; \
  693. text += DADDR(1); \
  694. text += " end "; \
  695. text += itos(_code_ptr[ip + 4]); \
  696. incr += 5; \
  697. } break
  698. #define DISASSEMBLE_ITERATE_BEGIN(m_type) \
  699. case OPCODE_ITERATE_BEGIN_##m_type: { \
  700. text += "for-init (typed "; \
  701. text += #m_type; \
  702. text += ") "; \
  703. text += DADDR(3); \
  704. text += " in "; \
  705. text += DADDR(2); \
  706. text += " counter "; \
  707. text += DADDR(1); \
  708. text += " end "; \
  709. text += itos(_code_ptr[ip + 4]); \
  710. incr += 5; \
  711. } break
  712. #define DISASSEMBLE_ITERATE_TYPES(m_macro) \
  713. m_macro(INT); \
  714. m_macro(FLOAT); \
  715. m_macro(VECTOR2); \
  716. m_macro(VECTOR2I); \
  717. m_macro(VECTOR3); \
  718. m_macro(VECTOR3I); \
  719. m_macro(STRING); \
  720. m_macro(DICTIONARY); \
  721. m_macro(ARRAY); \
  722. m_macro(PACKED_BYTE_ARRAY); \
  723. m_macro(PACKED_INT32_ARRAY); \
  724. m_macro(PACKED_INT64_ARRAY); \
  725. m_macro(PACKED_FLOAT32_ARRAY); \
  726. m_macro(PACKED_FLOAT64_ARRAY); \
  727. m_macro(PACKED_STRING_ARRAY); \
  728. m_macro(PACKED_VECTOR2_ARRAY); \
  729. m_macro(PACKED_VECTOR3_ARRAY); \
  730. m_macro(PACKED_COLOR_ARRAY); \
  731. m_macro(OBJECT)
  732. case OPCODE_ITERATE_BEGIN: {
  733. text += "for-init ";
  734. text += DADDR(3);
  735. text += " in ";
  736. text += DADDR(2);
  737. text += " counter ";
  738. text += DADDR(1);
  739. text += " end ";
  740. text += itos(_code_ptr[ip + 4]);
  741. incr += 5;
  742. } break;
  743. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE_BEGIN);
  744. case OPCODE_ITERATE: {
  745. text += "for-loop ";
  746. text += DADDR(2);
  747. text += " in ";
  748. text += DADDR(2);
  749. text += " counter ";
  750. text += DADDR(1);
  751. text += " end ";
  752. text += itos(_code_ptr[ip + 4]);
  753. incr += 5;
  754. } break;
  755. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE);
  756. case OPCODE_STORE_NAMED_GLOBAL: {
  757. text += "store named global ";
  758. text += DADDR(1);
  759. text += " = ";
  760. text += String(_global_names_ptr[_code_ptr[ip + 2]]);
  761. incr += 3;
  762. } break;
  763. case OPCODE_LINE: {
  764. int line = _code_ptr[ip + 1] - 1;
  765. if (line >= 0 && line < p_code_lines.size()) {
  766. text += "line ";
  767. text += itos(line + 1);
  768. text += ": ";
  769. text += p_code_lines[line];
  770. } else {
  771. text += "";
  772. }
  773. incr += 2;
  774. } break;
  775. #define DISASSEMBLE_TYPE_ADJUST(m_v_type) \
  776. case OPCODE_TYPE_ADJUST_##m_v_type: { \
  777. text += "type adjust ("; \
  778. text += #m_v_type; \
  779. text += ") "; \
  780. text += DADDR(1); \
  781. incr += 2; \
  782. } break
  783. DISASSEMBLE_TYPE_ADJUST(BOOL);
  784. DISASSEMBLE_TYPE_ADJUST(INT);
  785. DISASSEMBLE_TYPE_ADJUST(FLOAT);
  786. DISASSEMBLE_TYPE_ADJUST(STRING);
  787. DISASSEMBLE_TYPE_ADJUST(VECTOR2);
  788. DISASSEMBLE_TYPE_ADJUST(VECTOR2I);
  789. DISASSEMBLE_TYPE_ADJUST(RECT2);
  790. DISASSEMBLE_TYPE_ADJUST(RECT2I);
  791. DISASSEMBLE_TYPE_ADJUST(VECTOR3);
  792. DISASSEMBLE_TYPE_ADJUST(VECTOR3I);
  793. DISASSEMBLE_TYPE_ADJUST(TRANSFORM2D);
  794. DISASSEMBLE_TYPE_ADJUST(PLANE);
  795. DISASSEMBLE_TYPE_ADJUST(QUAT);
  796. DISASSEMBLE_TYPE_ADJUST(AABB);
  797. DISASSEMBLE_TYPE_ADJUST(BASIS);
  798. DISASSEMBLE_TYPE_ADJUST(TRANSFORM);
  799. DISASSEMBLE_TYPE_ADJUST(COLOR);
  800. DISASSEMBLE_TYPE_ADJUST(STRING_NAME);
  801. DISASSEMBLE_TYPE_ADJUST(NODE_PATH);
  802. DISASSEMBLE_TYPE_ADJUST(RID);
  803. DISASSEMBLE_TYPE_ADJUST(OBJECT);
  804. DISASSEMBLE_TYPE_ADJUST(CALLABLE);
  805. DISASSEMBLE_TYPE_ADJUST(SIGNAL);
  806. DISASSEMBLE_TYPE_ADJUST(DICTIONARY);
  807. DISASSEMBLE_TYPE_ADJUST(ARRAY);
  808. DISASSEMBLE_TYPE_ADJUST(PACKED_BYTE_ARRAY);
  809. DISASSEMBLE_TYPE_ADJUST(PACKED_INT32_ARRAY);
  810. DISASSEMBLE_TYPE_ADJUST(PACKED_INT64_ARRAY);
  811. DISASSEMBLE_TYPE_ADJUST(PACKED_FLOAT32_ARRAY);
  812. DISASSEMBLE_TYPE_ADJUST(PACKED_FLOAT64_ARRAY);
  813. DISASSEMBLE_TYPE_ADJUST(PACKED_STRING_ARRAY);
  814. DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR2_ARRAY);
  815. DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR3_ARRAY);
  816. DISASSEMBLE_TYPE_ADJUST(PACKED_COLOR_ARRAY);
  817. case OPCODE_ASSERT: {
  818. text += "assert (";
  819. text += DADDR(1);
  820. text += ", ";
  821. text += DADDR(2);
  822. text += ")";
  823. incr += 3;
  824. } break;
  825. case OPCODE_BREAKPOINT: {
  826. text += "breakpoint";
  827. incr += 1;
  828. } break;
  829. case OPCODE_END: {
  830. text += "== END ==";
  831. incr += 1;
  832. } break;
  833. }
  834. ip += incr;
  835. if (text.get_string_length() > 0) {
  836. print_line(text.as_string());
  837. }
  838. }
  839. }
  840. #endif