gdscript_disassembler.cpp 27 KB

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