gdscript_disassembler.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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(VECTOR4);
  558. DISASSEMBLE_PTRCALL(VECTOR4I);
  559. DISASSEMBLE_PTRCALL(PLANE);
  560. DISASSEMBLE_PTRCALL(AABB);
  561. DISASSEMBLE_PTRCALL(BASIS);
  562. DISASSEMBLE_PTRCALL(TRANSFORM3D);
  563. DISASSEMBLE_PTRCALL(PROJECTION);
  564. DISASSEMBLE_PTRCALL(COLOR);
  565. DISASSEMBLE_PTRCALL(STRING_NAME);
  566. DISASSEMBLE_PTRCALL(NODE_PATH);
  567. DISASSEMBLE_PTRCALL(RID);
  568. DISASSEMBLE_PTRCALL(QUATERNION);
  569. DISASSEMBLE_PTRCALL(OBJECT);
  570. DISASSEMBLE_PTRCALL(CALLABLE);
  571. DISASSEMBLE_PTRCALL(SIGNAL);
  572. DISASSEMBLE_PTRCALL(DICTIONARY);
  573. DISASSEMBLE_PTRCALL(ARRAY);
  574. DISASSEMBLE_PTRCALL(PACKED_BYTE_ARRAY);
  575. DISASSEMBLE_PTRCALL(PACKED_INT32_ARRAY);
  576. DISASSEMBLE_PTRCALL(PACKED_INT64_ARRAY);
  577. DISASSEMBLE_PTRCALL(PACKED_FLOAT32_ARRAY);
  578. DISASSEMBLE_PTRCALL(PACKED_FLOAT64_ARRAY);
  579. DISASSEMBLE_PTRCALL(PACKED_STRING_ARRAY);
  580. DISASSEMBLE_PTRCALL(PACKED_VECTOR2_ARRAY);
  581. DISASSEMBLE_PTRCALL(PACKED_VECTOR3_ARRAY);
  582. DISASSEMBLE_PTRCALL(PACKED_COLOR_ARRAY);
  583. case OPCODE_CALL_BUILTIN_TYPE_VALIDATED: {
  584. int argc = _code_ptr[ip + 1 + instr_var_args];
  585. text += "call-builtin-method validated ";
  586. text += DADDR(2 + argc) + " = ";
  587. text += DADDR(1) + ".";
  588. text += "<unknown method>";
  589. text += "(";
  590. for (int i = 0; i < argc; i++) {
  591. if (i > 0) {
  592. text += ", ";
  593. }
  594. text += DADDR(1 + i);
  595. }
  596. text += ")";
  597. incr = 5 + argc;
  598. } break;
  599. case OPCODE_CALL_UTILITY: {
  600. text += "call-utility ";
  601. int argc = _code_ptr[ip + 1 + instr_var_args];
  602. text += DADDR(1 + argc) + " = ";
  603. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  604. text += "(";
  605. for (int i = 0; i < argc; i++) {
  606. if (i > 0) {
  607. text += ", ";
  608. }
  609. text += DADDR(1 + i);
  610. }
  611. text += ")";
  612. incr = 4 + argc;
  613. } break;
  614. case OPCODE_CALL_UTILITY_VALIDATED: {
  615. text += "call-utility ";
  616. int argc = _code_ptr[ip + 1 + instr_var_args];
  617. text += DADDR(1 + argc) + " = ";
  618. text += "<unknown function>";
  619. text += "(";
  620. for (int i = 0; i < argc; i++) {
  621. if (i > 0) {
  622. text += ", ";
  623. }
  624. text += DADDR(1 + i);
  625. }
  626. text += ")";
  627. incr = 4 + argc;
  628. } break;
  629. case OPCODE_CALL_GDSCRIPT_UTILITY: {
  630. text += "call-gscript-utility ";
  631. int argc = _code_ptr[ip + 1 + instr_var_args];
  632. text += DADDR(1 + argc) + " = ";
  633. text += "<unknown function>";
  634. text += "(";
  635. for (int i = 0; i < argc; i++) {
  636. if (i > 0) {
  637. text += ", ";
  638. }
  639. text += DADDR(1 + i);
  640. }
  641. text += ")";
  642. incr = 4 + argc;
  643. } break;
  644. case OPCODE_CALL_SELF_BASE: {
  645. text += "call-self-base ";
  646. int argc = _code_ptr[ip + 1 + instr_var_args];
  647. text += DADDR(2 + argc) + " = ";
  648. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  649. text += "(";
  650. for (int i = 0; i < argc; i++) {
  651. if (i > 0) {
  652. text += ", ";
  653. }
  654. text += DADDR(1 + i);
  655. }
  656. text += ")";
  657. incr = 4 + argc;
  658. } break;
  659. case OPCODE_AWAIT: {
  660. text += "await ";
  661. text += DADDR(1);
  662. incr = 2;
  663. } break;
  664. case OPCODE_AWAIT_RESUME: {
  665. text += "await resume ";
  666. text += DADDR(1);
  667. incr = 2;
  668. } break;
  669. case OPCODE_CREATE_LAMBDA: {
  670. int captures_count = _code_ptr[ip + 1 + instr_var_args];
  671. GDScriptFunction *lambda = _lambdas_ptr[_code_ptr[ip + 2 + instr_var_args]];
  672. text += DADDR(1 + captures_count);
  673. text += "create lambda from ";
  674. text += lambda->name.operator String();
  675. text += "function, captures (";
  676. for (int i = 0; i < captures_count; i++) {
  677. if (i > 0) {
  678. text += ", ";
  679. }
  680. text += DADDR(1 + i);
  681. }
  682. text += ")";
  683. incr = 3 + captures_count;
  684. } break;
  685. case OPCODE_CREATE_SELF_LAMBDA: {
  686. int captures_count = _code_ptr[ip + 1 + instr_var_args];
  687. GDScriptFunction *lambda = _lambdas_ptr[_code_ptr[ip + 2 + instr_var_args]];
  688. text += DADDR(1 + captures_count);
  689. text += "create self lambda from ";
  690. text += lambda->name.operator String();
  691. text += "function, captures (";
  692. for (int i = 0; i < captures_count; i++) {
  693. if (i > 0) {
  694. text += ", ";
  695. }
  696. text += DADDR(1 + i);
  697. }
  698. text += ")";
  699. incr = 3 + captures_count;
  700. } break;
  701. case OPCODE_JUMP: {
  702. text += "jump ";
  703. text += itos(_code_ptr[ip + 1]);
  704. incr = 2;
  705. } break;
  706. case OPCODE_JUMP_IF: {
  707. text += "jump-if ";
  708. text += DADDR(1);
  709. text += " to ";
  710. text += itos(_code_ptr[ip + 2]);
  711. incr = 3;
  712. } break;
  713. case OPCODE_JUMP_IF_NOT: {
  714. text += "jump-if-not ";
  715. text += DADDR(1);
  716. text += " to ";
  717. text += itos(_code_ptr[ip + 2]);
  718. incr = 3;
  719. } break;
  720. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  721. text += "jump-to-default-argument ";
  722. incr = 1;
  723. } break;
  724. case OPCODE_JUMP_IF_SHARED: {
  725. text += "jump-if-shared ";
  726. text += DADDR(1);
  727. text += " to ";
  728. text += itos(_code_ptr[ip + 2]);
  729. incr = 3;
  730. } break;
  731. case OPCODE_RETURN: {
  732. text += "return ";
  733. text += DADDR(1);
  734. incr = 2;
  735. } break;
  736. case OPCODE_RETURN_TYPED_BUILTIN: {
  737. text += "return typed builtin (";
  738. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 2]);
  739. text += ") ";
  740. text += DADDR(1);
  741. incr += 3;
  742. } break;
  743. case OPCODE_RETURN_TYPED_ARRAY: {
  744. text += "return typed array ";
  745. text += DADDR(1);
  746. incr += 5;
  747. } break;
  748. case OPCODE_RETURN_TYPED_NATIVE: {
  749. text += "return typed native (";
  750. text += DADDR(2);
  751. text += ") ";
  752. text += DADDR(1);
  753. incr += 3;
  754. } break;
  755. case OPCODE_RETURN_TYPED_SCRIPT: {
  756. Variant script = _constants_ptr[_code_ptr[ip + 2]];
  757. Script *sc = Object::cast_to<Script>(script.operator Object *());
  758. text += "return typed script (";
  759. text += sc->get_path();
  760. text += ") ";
  761. text += DADDR(1);
  762. incr += 3;
  763. } break;
  764. #define DISASSEMBLE_ITERATE(m_type) \
  765. case OPCODE_ITERATE_##m_type: { \
  766. text += "for-loop (typed "; \
  767. text += #m_type; \
  768. text += ") "; \
  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. #define DISASSEMBLE_ITERATE_BEGIN(m_type) \
  779. case OPCODE_ITERATE_BEGIN_##m_type: { \
  780. text += "for-init (typed "; \
  781. text += #m_type; \
  782. text += ") "; \
  783. text += DADDR(3); \
  784. text += " in "; \
  785. text += DADDR(2); \
  786. text += " counter "; \
  787. text += DADDR(1); \
  788. text += " end "; \
  789. text += itos(_code_ptr[ip + 4]); \
  790. incr += 5; \
  791. } break
  792. #define DISASSEMBLE_ITERATE_TYPES(m_macro) \
  793. m_macro(INT); \
  794. m_macro(FLOAT); \
  795. m_macro(VECTOR2); \
  796. m_macro(VECTOR2I); \
  797. m_macro(VECTOR3); \
  798. m_macro(VECTOR3I); \
  799. m_macro(STRING); \
  800. m_macro(DICTIONARY); \
  801. m_macro(ARRAY); \
  802. m_macro(PACKED_BYTE_ARRAY); \
  803. m_macro(PACKED_INT32_ARRAY); \
  804. m_macro(PACKED_INT64_ARRAY); \
  805. m_macro(PACKED_FLOAT32_ARRAY); \
  806. m_macro(PACKED_FLOAT64_ARRAY); \
  807. m_macro(PACKED_STRING_ARRAY); \
  808. m_macro(PACKED_VECTOR2_ARRAY); \
  809. m_macro(PACKED_VECTOR3_ARRAY); \
  810. m_macro(PACKED_COLOR_ARRAY); \
  811. m_macro(OBJECT)
  812. case OPCODE_ITERATE_BEGIN: {
  813. text += "for-init ";
  814. text += DADDR(3);
  815. text += " in ";
  816. text += DADDR(2);
  817. text += " counter ";
  818. text += DADDR(1);
  819. text += " end ";
  820. text += itos(_code_ptr[ip + 4]);
  821. incr += 5;
  822. } break;
  823. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE_BEGIN);
  824. case OPCODE_ITERATE: {
  825. text += "for-loop ";
  826. text += DADDR(2);
  827. text += " in ";
  828. text += DADDR(2);
  829. text += " counter ";
  830. text += DADDR(1);
  831. text += " end ";
  832. text += itos(_code_ptr[ip + 4]);
  833. incr += 5;
  834. } break;
  835. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE);
  836. case OPCODE_STORE_GLOBAL: {
  837. text += "store global ";
  838. text += DADDR(1);
  839. text += " = ";
  840. text += String::num_int64(_code_ptr[ip + 2]);
  841. incr += 3;
  842. } break;
  843. case OPCODE_STORE_NAMED_GLOBAL: {
  844. text += "store named global ";
  845. text += DADDR(1);
  846. text += " = ";
  847. text += String(_global_names_ptr[_code_ptr[ip + 2]]);
  848. incr += 3;
  849. } break;
  850. case OPCODE_LINE: {
  851. int line = _code_ptr[ip + 1] - 1;
  852. if (line >= 0 && line < p_code_lines.size()) {
  853. text += "line ";
  854. text += itos(line + 1);
  855. text += ": ";
  856. text += p_code_lines[line];
  857. } else {
  858. text += "";
  859. }
  860. incr += 2;
  861. } break;
  862. #define DISASSEMBLE_TYPE_ADJUST(m_v_type) \
  863. case OPCODE_TYPE_ADJUST_##m_v_type: { \
  864. text += "type adjust ("; \
  865. text += #m_v_type; \
  866. text += ") "; \
  867. text += DADDR(1); \
  868. incr += 2; \
  869. } break
  870. DISASSEMBLE_TYPE_ADJUST(BOOL);
  871. DISASSEMBLE_TYPE_ADJUST(INT);
  872. DISASSEMBLE_TYPE_ADJUST(FLOAT);
  873. DISASSEMBLE_TYPE_ADJUST(STRING);
  874. DISASSEMBLE_TYPE_ADJUST(VECTOR2);
  875. DISASSEMBLE_TYPE_ADJUST(VECTOR2I);
  876. DISASSEMBLE_TYPE_ADJUST(RECT2);
  877. DISASSEMBLE_TYPE_ADJUST(RECT2I);
  878. DISASSEMBLE_TYPE_ADJUST(VECTOR3);
  879. DISASSEMBLE_TYPE_ADJUST(VECTOR3I);
  880. DISASSEMBLE_TYPE_ADJUST(TRANSFORM2D);
  881. DISASSEMBLE_TYPE_ADJUST(VECTOR4);
  882. DISASSEMBLE_TYPE_ADJUST(VECTOR4I);
  883. DISASSEMBLE_TYPE_ADJUST(PLANE);
  884. DISASSEMBLE_TYPE_ADJUST(QUATERNION);
  885. DISASSEMBLE_TYPE_ADJUST(AABB);
  886. DISASSEMBLE_TYPE_ADJUST(BASIS);
  887. DISASSEMBLE_TYPE_ADJUST(TRANSFORM3D);
  888. DISASSEMBLE_TYPE_ADJUST(PROJECTION);
  889. DISASSEMBLE_TYPE_ADJUST(COLOR);
  890. DISASSEMBLE_TYPE_ADJUST(STRING_NAME);
  891. DISASSEMBLE_TYPE_ADJUST(NODE_PATH);
  892. DISASSEMBLE_TYPE_ADJUST(RID);
  893. DISASSEMBLE_TYPE_ADJUST(OBJECT);
  894. DISASSEMBLE_TYPE_ADJUST(CALLABLE);
  895. DISASSEMBLE_TYPE_ADJUST(SIGNAL);
  896. DISASSEMBLE_TYPE_ADJUST(DICTIONARY);
  897. DISASSEMBLE_TYPE_ADJUST(ARRAY);
  898. DISASSEMBLE_TYPE_ADJUST(PACKED_BYTE_ARRAY);
  899. DISASSEMBLE_TYPE_ADJUST(PACKED_INT32_ARRAY);
  900. DISASSEMBLE_TYPE_ADJUST(PACKED_INT64_ARRAY);
  901. DISASSEMBLE_TYPE_ADJUST(PACKED_FLOAT32_ARRAY);
  902. DISASSEMBLE_TYPE_ADJUST(PACKED_FLOAT64_ARRAY);
  903. DISASSEMBLE_TYPE_ADJUST(PACKED_STRING_ARRAY);
  904. DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR2_ARRAY);
  905. DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR3_ARRAY);
  906. DISASSEMBLE_TYPE_ADJUST(PACKED_COLOR_ARRAY);
  907. case OPCODE_ASSERT: {
  908. text += "assert (";
  909. text += DADDR(1);
  910. text += ", ";
  911. text += DADDR(2);
  912. text += ")";
  913. incr += 3;
  914. } break;
  915. case OPCODE_BREAKPOINT: {
  916. text += "breakpoint";
  917. incr += 1;
  918. } break;
  919. case OPCODE_END: {
  920. text += "== END ==";
  921. incr += 1;
  922. } break;
  923. }
  924. ip += incr;
  925. if (text.get_string_length() > 0) {
  926. print_line(text.as_string());
  927. }
  928. }
  929. }
  930. #endif