gdscript_disassembler.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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 += "<unknown 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_BUILTIN_STATIC: {
  474. Variant::Type type = (Variant::Type)_code_ptr[ip + 1 + instr_var_args];
  475. int argc = _code_ptr[ip + 3 + instr_var_args];
  476. text += "call built-in method static ";
  477. text += DADDR(1 + argc);
  478. text += " = ";
  479. text += Variant::get_type_name(type);
  480. text += ".";
  481. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]].operator String();
  482. text += "(";
  483. for (int i = 0; i < argc; i++) {
  484. if (i > 0) {
  485. text += ", ";
  486. }
  487. text += DADDR(1 + i);
  488. }
  489. text += ")";
  490. incr += 5 + argc;
  491. } break;
  492. case OPCODE_CALL_PTRCALL_NO_RETURN: {
  493. text += "call-ptrcall (no return) ";
  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(1 + argc) + ".";
  497. text += method->get_name();
  498. text += "(";
  499. for (int i = 0; i < argc; i++) {
  500. if (i > 0) {
  501. text += ", ";
  502. }
  503. text += DADDR(1 + i);
  504. }
  505. text += ")";
  506. incr = 5 + argc;
  507. } break;
  508. #define DISASSEMBLE_PTRCALL(m_type) \
  509. case OPCODE_CALL_PTRCALL_##m_type: { \
  510. text += "call-ptrcall (return "; \
  511. text += #m_type; \
  512. text += ") "; \
  513. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]]; \
  514. int argc = _code_ptr[ip + 1 + instr_var_args]; \
  515. text += DADDR(2 + argc) + " = "; \
  516. text += DADDR(1 + argc) + "."; \
  517. text += method->get_name(); \
  518. text += "("; \
  519. for (int i = 0; i < argc; i++) { \
  520. if (i > 0) \
  521. text += ", "; \
  522. text += DADDR(1 + i); \
  523. } \
  524. text += ")"; \
  525. incr = 5 + argc; \
  526. } break
  527. DISASSEMBLE_PTRCALL(BOOL);
  528. DISASSEMBLE_PTRCALL(INT);
  529. DISASSEMBLE_PTRCALL(FLOAT);
  530. DISASSEMBLE_PTRCALL(STRING);
  531. DISASSEMBLE_PTRCALL(VECTOR2);
  532. DISASSEMBLE_PTRCALL(VECTOR2I);
  533. DISASSEMBLE_PTRCALL(RECT2);
  534. DISASSEMBLE_PTRCALL(RECT2I);
  535. DISASSEMBLE_PTRCALL(VECTOR3);
  536. DISASSEMBLE_PTRCALL(VECTOR3I);
  537. DISASSEMBLE_PTRCALL(TRANSFORM2D);
  538. DISASSEMBLE_PTRCALL(PLANE);
  539. DISASSEMBLE_PTRCALL(AABB);
  540. DISASSEMBLE_PTRCALL(BASIS);
  541. DISASSEMBLE_PTRCALL(TRANSFORM3D);
  542. DISASSEMBLE_PTRCALL(COLOR);
  543. DISASSEMBLE_PTRCALL(STRING_NAME);
  544. DISASSEMBLE_PTRCALL(NODE_PATH);
  545. DISASSEMBLE_PTRCALL(RID);
  546. DISASSEMBLE_PTRCALL(QUAT);
  547. DISASSEMBLE_PTRCALL(OBJECT);
  548. DISASSEMBLE_PTRCALL(CALLABLE);
  549. DISASSEMBLE_PTRCALL(SIGNAL);
  550. DISASSEMBLE_PTRCALL(DICTIONARY);
  551. DISASSEMBLE_PTRCALL(ARRAY);
  552. DISASSEMBLE_PTRCALL(PACKED_BYTE_ARRAY);
  553. DISASSEMBLE_PTRCALL(PACKED_INT32_ARRAY);
  554. DISASSEMBLE_PTRCALL(PACKED_INT64_ARRAY);
  555. DISASSEMBLE_PTRCALL(PACKED_FLOAT32_ARRAY);
  556. DISASSEMBLE_PTRCALL(PACKED_FLOAT64_ARRAY);
  557. DISASSEMBLE_PTRCALL(PACKED_STRING_ARRAY);
  558. DISASSEMBLE_PTRCALL(PACKED_VECTOR2_ARRAY);
  559. DISASSEMBLE_PTRCALL(PACKED_VECTOR3_ARRAY);
  560. DISASSEMBLE_PTRCALL(PACKED_COLOR_ARRAY);
  561. case OPCODE_CALL_BUILTIN_TYPE_VALIDATED: {
  562. int argc = _code_ptr[ip + 1 + instr_var_args];
  563. text += "call-builtin-method validated ";
  564. text += DADDR(2 + argc) + " = ";
  565. text += DADDR(1) + ".";
  566. text += "<unknown method>";
  567. text += "(";
  568. for (int i = 0; i < argc; i++) {
  569. if (i > 0) {
  570. text += ", ";
  571. }
  572. text += DADDR(1 + i);
  573. }
  574. text += ")";
  575. incr = 5 + argc;
  576. } break;
  577. case OPCODE_CALL_UTILITY: {
  578. text += "call-utility ";
  579. int argc = _code_ptr[ip + 1 + instr_var_args];
  580. text += DADDR(1 + argc) + " = ";
  581. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  582. text += "(";
  583. for (int i = 0; i < argc; i++) {
  584. if (i > 0) {
  585. text += ", ";
  586. }
  587. text += DADDR(1 + i);
  588. }
  589. text += ")";
  590. incr = 4 + argc;
  591. } break;
  592. case OPCODE_CALL_UTILITY_VALIDATED: {
  593. text += "call-utility ";
  594. int argc = _code_ptr[ip + 1 + instr_var_args];
  595. text += DADDR(1 + argc) + " = ";
  596. text += "<unknown function>";
  597. text += "(";
  598. for (int i = 0; i < argc; i++) {
  599. if (i > 0) {
  600. text += ", ";
  601. }
  602. text += DADDR(1 + i);
  603. }
  604. text += ")";
  605. incr = 4 + argc;
  606. } break;
  607. case OPCODE_CALL_GDSCRIPT_UTILITY: {
  608. text += "call-gscript-utility ";
  609. int argc = _code_ptr[ip + 1 + instr_var_args];
  610. text += DADDR(1 + argc) + " = ";
  611. text += "<unknown function>";
  612. text += "(";
  613. for (int i = 0; i < argc; i++) {
  614. if (i > 0) {
  615. text += ", ";
  616. }
  617. text += DADDR(1 + i);
  618. }
  619. text += ")";
  620. incr = 4 + argc;
  621. } break;
  622. case OPCODE_CALL_SELF_BASE: {
  623. text += "call-self-base ";
  624. int argc = _code_ptr[ip + 1 + instr_var_args];
  625. text += DADDR(2 + argc) + " = ";
  626. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  627. text += "(";
  628. for (int i = 0; i < argc; i++) {
  629. if (i > 0) {
  630. text += ", ";
  631. }
  632. text += DADDR(1 + i);
  633. }
  634. text += ")";
  635. incr = 4 + argc;
  636. } break;
  637. case OPCODE_AWAIT: {
  638. text += "await ";
  639. text += DADDR(1);
  640. incr = 2;
  641. } break;
  642. case OPCODE_AWAIT_RESUME: {
  643. text += "await resume ";
  644. text += DADDR(1);
  645. incr = 2;
  646. } break;
  647. case OPCODE_CREATE_LAMBDA: {
  648. int captures_count = _code_ptr[ip + 1 + instr_var_args];
  649. GDScriptFunction *lambda = _lambdas_ptr[_code_ptr[ip + 2 + instr_var_args]];
  650. text += DADDR(1 + captures_count);
  651. text += "create lambda from ";
  652. text += lambda->name.operator String();
  653. text += "function, captures (";
  654. for (int i = 0; i < captures_count; i++) {
  655. if (i > 0) {
  656. text += ", ";
  657. }
  658. text += DADDR(1 + i);
  659. }
  660. text += ")";
  661. incr = 3 + captures_count;
  662. } break;
  663. case OPCODE_JUMP: {
  664. text += "jump ";
  665. text += itos(_code_ptr[ip + 1]);
  666. incr = 2;
  667. } break;
  668. case OPCODE_JUMP_IF: {
  669. text += "jump-if ";
  670. text += DADDR(1);
  671. text += " to ";
  672. text += itos(_code_ptr[ip + 2]);
  673. incr = 3;
  674. } break;
  675. case OPCODE_JUMP_IF_NOT: {
  676. text += "jump-if-not ";
  677. text += DADDR(1);
  678. text += " to ";
  679. text += itos(_code_ptr[ip + 2]);
  680. incr = 3;
  681. } break;
  682. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  683. text += "jump-to-default-argument ";
  684. incr = 1;
  685. } break;
  686. case OPCODE_RETURN: {
  687. text += "return ";
  688. text += DADDR(1);
  689. incr = 2;
  690. } break;
  691. case OPCODE_RETURN_TYPED_BUILTIN: {
  692. text += "return typed builtin (";
  693. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 2]);
  694. text += ") ";
  695. text += DADDR(1);
  696. incr += 3;
  697. } break;
  698. case OPCODE_RETURN_TYPED_ARRAY: {
  699. text += "return typed array ";
  700. text += DADDR(1);
  701. incr += 5;
  702. } break;
  703. case OPCODE_RETURN_TYPED_NATIVE: {
  704. text += "return typed native (";
  705. text += DADDR(2);
  706. text += ") ";
  707. text += DADDR(1);
  708. incr += 3;
  709. } break;
  710. case OPCODE_RETURN_TYPED_SCRIPT: {
  711. Variant script = _constants_ptr[_code_ptr[ip + 2]];
  712. Script *sc = Object::cast_to<Script>(script.operator Object *());
  713. text += "return typed script (";
  714. text += sc->get_path();
  715. text += ") ";
  716. text += DADDR(1);
  717. incr += 3;
  718. } break;
  719. #define DISASSEMBLE_ITERATE(m_type) \
  720. case OPCODE_ITERATE_##m_type: { \
  721. text += "for-loop (typed "; \
  722. text += #m_type; \
  723. text += ") "; \
  724. text += DADDR(3); \
  725. text += " in "; \
  726. text += DADDR(2); \
  727. text += " counter "; \
  728. text += DADDR(1); \
  729. text += " end "; \
  730. text += itos(_code_ptr[ip + 4]); \
  731. incr += 5; \
  732. } break
  733. #define DISASSEMBLE_ITERATE_BEGIN(m_type) \
  734. case OPCODE_ITERATE_BEGIN_##m_type: { \
  735. text += "for-init (typed "; \
  736. text += #m_type; \
  737. text += ") "; \
  738. text += DADDR(3); \
  739. text += " in "; \
  740. text += DADDR(2); \
  741. text += " counter "; \
  742. text += DADDR(1); \
  743. text += " end "; \
  744. text += itos(_code_ptr[ip + 4]); \
  745. incr += 5; \
  746. } break
  747. #define DISASSEMBLE_ITERATE_TYPES(m_macro) \
  748. m_macro(INT); \
  749. m_macro(FLOAT); \
  750. m_macro(VECTOR2); \
  751. m_macro(VECTOR2I); \
  752. m_macro(VECTOR3); \
  753. m_macro(VECTOR3I); \
  754. m_macro(STRING); \
  755. m_macro(DICTIONARY); \
  756. m_macro(ARRAY); \
  757. m_macro(PACKED_BYTE_ARRAY); \
  758. m_macro(PACKED_INT32_ARRAY); \
  759. m_macro(PACKED_INT64_ARRAY); \
  760. m_macro(PACKED_FLOAT32_ARRAY); \
  761. m_macro(PACKED_FLOAT64_ARRAY); \
  762. m_macro(PACKED_STRING_ARRAY); \
  763. m_macro(PACKED_VECTOR2_ARRAY); \
  764. m_macro(PACKED_VECTOR3_ARRAY); \
  765. m_macro(PACKED_COLOR_ARRAY); \
  766. m_macro(OBJECT)
  767. case OPCODE_ITERATE_BEGIN: {
  768. text += "for-init ";
  769. text += DADDR(3);
  770. text += " in ";
  771. text += DADDR(2);
  772. text += " counter ";
  773. text += DADDR(1);
  774. text += " end ";
  775. text += itos(_code_ptr[ip + 4]);
  776. incr += 5;
  777. } break;
  778. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE_BEGIN);
  779. case OPCODE_ITERATE: {
  780. text += "for-loop ";
  781. text += DADDR(2);
  782. text += " in ";
  783. text += DADDR(2);
  784. text += " counter ";
  785. text += DADDR(1);
  786. text += " end ";
  787. text += itos(_code_ptr[ip + 4]);
  788. incr += 5;
  789. } break;
  790. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE);
  791. case OPCODE_STORE_NAMED_GLOBAL: {
  792. text += "store named global ";
  793. text += DADDR(1);
  794. text += " = ";
  795. text += String(_global_names_ptr[_code_ptr[ip + 2]]);
  796. incr += 3;
  797. } break;
  798. case OPCODE_LINE: {
  799. int line = _code_ptr[ip + 1] - 1;
  800. if (line >= 0 && line < p_code_lines.size()) {
  801. text += "line ";
  802. text += itos(line + 1);
  803. text += ": ";
  804. text += p_code_lines[line];
  805. } else {
  806. text += "";
  807. }
  808. incr += 2;
  809. } break;
  810. #define DISASSEMBLE_TYPE_ADJUST(m_v_type) \
  811. case OPCODE_TYPE_ADJUST_##m_v_type: { \
  812. text += "type adjust ("; \
  813. text += #m_v_type; \
  814. text += ") "; \
  815. text += DADDR(1); \
  816. incr += 2; \
  817. } break
  818. DISASSEMBLE_TYPE_ADJUST(BOOL);
  819. DISASSEMBLE_TYPE_ADJUST(INT);
  820. DISASSEMBLE_TYPE_ADJUST(FLOAT);
  821. DISASSEMBLE_TYPE_ADJUST(STRING);
  822. DISASSEMBLE_TYPE_ADJUST(VECTOR2);
  823. DISASSEMBLE_TYPE_ADJUST(VECTOR2I);
  824. DISASSEMBLE_TYPE_ADJUST(RECT2);
  825. DISASSEMBLE_TYPE_ADJUST(RECT2I);
  826. DISASSEMBLE_TYPE_ADJUST(VECTOR3);
  827. DISASSEMBLE_TYPE_ADJUST(VECTOR3I);
  828. DISASSEMBLE_TYPE_ADJUST(TRANSFORM2D);
  829. DISASSEMBLE_TYPE_ADJUST(PLANE);
  830. DISASSEMBLE_TYPE_ADJUST(QUAT);
  831. DISASSEMBLE_TYPE_ADJUST(AABB);
  832. DISASSEMBLE_TYPE_ADJUST(BASIS);
  833. DISASSEMBLE_TYPE_ADJUST(TRANSFORM);
  834. DISASSEMBLE_TYPE_ADJUST(COLOR);
  835. DISASSEMBLE_TYPE_ADJUST(STRING_NAME);
  836. DISASSEMBLE_TYPE_ADJUST(NODE_PATH);
  837. DISASSEMBLE_TYPE_ADJUST(RID);
  838. DISASSEMBLE_TYPE_ADJUST(OBJECT);
  839. DISASSEMBLE_TYPE_ADJUST(CALLABLE);
  840. DISASSEMBLE_TYPE_ADJUST(SIGNAL);
  841. DISASSEMBLE_TYPE_ADJUST(DICTIONARY);
  842. DISASSEMBLE_TYPE_ADJUST(ARRAY);
  843. DISASSEMBLE_TYPE_ADJUST(PACKED_BYTE_ARRAY);
  844. DISASSEMBLE_TYPE_ADJUST(PACKED_INT32_ARRAY);
  845. DISASSEMBLE_TYPE_ADJUST(PACKED_INT64_ARRAY);
  846. DISASSEMBLE_TYPE_ADJUST(PACKED_FLOAT32_ARRAY);
  847. DISASSEMBLE_TYPE_ADJUST(PACKED_FLOAT64_ARRAY);
  848. DISASSEMBLE_TYPE_ADJUST(PACKED_STRING_ARRAY);
  849. DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR2_ARRAY);
  850. DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR3_ARRAY);
  851. DISASSEMBLE_TYPE_ADJUST(PACKED_COLOR_ARRAY);
  852. case OPCODE_ASSERT: {
  853. text += "assert (";
  854. text += DADDR(1);
  855. text += ", ";
  856. text += DADDR(2);
  857. text += ")";
  858. incr += 3;
  859. } break;
  860. case OPCODE_BREAKPOINT: {
  861. text += "breakpoint";
  862. incr += 1;
  863. } break;
  864. case OPCODE_END: {
  865. text += "== END ==";
  866. incr += 1;
  867. } break;
  868. }
  869. ip += incr;
  870. if (text.get_string_length() > 0) {
  871. print_line(text.as_string());
  872. }
  873. }
  874. }
  875. #endif