gdscript_disassembler.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. /**************************************************************************/
  2. /* gdscript_disassembler.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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.h"
  32. #include "gdscript_function.h"
  33. #include "core/string/string_builder.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 = "class(" + cls->get_name() + ")";
  50. } else {
  51. Script *script = Object::cast_to<Script>(obj);
  52. if (script) {
  53. txt = "script(" + GDScript::debug_get_script_name(script) + ")";
  54. } else {
  55. txt = "object(" + obj->get_class();
  56. if (obj->get_script_instance()) {
  57. txt += ", " + GDScript::debug_get_script_name(obj->get_script_instance()->get_script());
  58. }
  59. txt += ")";
  60. }
  61. }
  62. }
  63. } else {
  64. txt = p_variant;
  65. }
  66. return txt;
  67. }
  68. static String _disassemble_address(const GDScript *p_script, const GDScriptFunction &p_function, int p_address) {
  69. int addr = p_address & GDScriptFunction::ADDR_MASK;
  70. switch (p_address >> GDScriptFunction::ADDR_BITS) {
  71. case GDScriptFunction::ADDR_TYPE_STACK: {
  72. switch (addr) {
  73. case GDScriptFunction::ADDR_STACK_SELF:
  74. return "self";
  75. case GDScriptFunction::ADDR_STACK_CLASS:
  76. return "class";
  77. case GDScriptFunction::ADDR_STACK_NIL:
  78. return "nil";
  79. default:
  80. return "stack(" + itos(addr) + ")";
  81. }
  82. } break;
  83. case GDScriptFunction::ADDR_TYPE_CONSTANT: {
  84. return "const(" + _get_variant_string(p_function.get_constant(addr)) + ")";
  85. } break;
  86. case GDScriptFunction::ADDR_TYPE_MEMBER: {
  87. return "member(" + p_script->debug_get_member_by_index(addr) + ")";
  88. } break;
  89. }
  90. return "<err>";
  91. }
  92. void GDScriptFunction::disassemble(const Vector<String> &p_code_lines) const {
  93. #define DADDR(m_ip) (_disassemble_address(_script, *this, _code_ptr[ip + m_ip]))
  94. for (int ip = 0; ip < _code_size;) {
  95. StringBuilder text;
  96. int incr = 0;
  97. text += " ";
  98. text += itos(ip);
  99. text += ": ";
  100. // This makes the compiler complain if some opcode is unchecked in the switch.
  101. Opcode opcode = Opcode(_code_ptr[ip]);
  102. switch (opcode) {
  103. case OPCODE_OPERATOR: {
  104. constexpr int _pointer_size = sizeof(Variant::ValidatedOperatorEvaluator) / sizeof(*_code_ptr);
  105. int operation = _code_ptr[ip + 4];
  106. text += "operator ";
  107. text += DADDR(3);
  108. text += " = ";
  109. text += DADDR(1);
  110. text += " ";
  111. text += Variant::get_operator_name(Variant::Operator(operation));
  112. text += " ";
  113. text += DADDR(2);
  114. incr += 7 + _pointer_size;
  115. } break;
  116. case OPCODE_OPERATOR_VALIDATED: {
  117. text += "validated operator ";
  118. text += DADDR(3);
  119. text += " = ";
  120. text += DADDR(1);
  121. text += " ";
  122. text += operator_names[_code_ptr[ip + 4]];
  123. text += " ";
  124. text += DADDR(2);
  125. incr += 5;
  126. } break;
  127. case OPCODE_TYPE_TEST_BUILTIN: {
  128. text += "type test ";
  129. text += DADDR(1);
  130. text += " = ";
  131. text += DADDR(2);
  132. text += " is ";
  133. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 3]));
  134. incr += 4;
  135. } break;
  136. case OPCODE_TYPE_TEST_ARRAY: {
  137. text += "type test ";
  138. text += DADDR(1);
  139. text += " = ";
  140. text += DADDR(2);
  141. text += " is Array[";
  142. Ref<Script> script_type = get_constant(_code_ptr[ip + 3] & ADDR_MASK);
  143. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 4];
  144. StringName native_type = get_global_name(_code_ptr[ip + 5]);
  145. if (script_type.is_valid() && script_type->is_valid()) {
  146. text += "script(";
  147. text += GDScript::debug_get_script_name(script_type);
  148. text += ")";
  149. } else if (native_type != StringName()) {
  150. text += native_type;
  151. } else {
  152. text += Variant::get_type_name(builtin_type);
  153. }
  154. text += "]";
  155. incr += 6;
  156. } break;
  157. case OPCODE_TYPE_TEST_DICTIONARY: {
  158. text += "type test ";
  159. text += DADDR(1);
  160. text += " = ";
  161. text += DADDR(2);
  162. text += " is Dictionary[";
  163. Ref<Script> key_script_type = get_constant(_code_ptr[ip + 3] & ADDR_MASK);
  164. Variant::Type key_builtin_type = (Variant::Type)_code_ptr[ip + 5];
  165. StringName key_native_type = get_global_name(_code_ptr[ip + 6]);
  166. if (key_script_type.is_valid() && key_script_type->is_valid()) {
  167. text += "script(";
  168. text += GDScript::debug_get_script_name(key_script_type);
  169. text += ")";
  170. } else if (key_native_type != StringName()) {
  171. text += key_native_type;
  172. } else {
  173. text += Variant::get_type_name(key_builtin_type);
  174. }
  175. text += ", ";
  176. Ref<Script> value_script_type = get_constant(_code_ptr[ip + 4] & ADDR_MASK);
  177. Variant::Type value_builtin_type = (Variant::Type)_code_ptr[ip + 7];
  178. StringName value_native_type = get_global_name(_code_ptr[ip + 8]);
  179. if (value_script_type.is_valid() && value_script_type->is_valid()) {
  180. text += "script(";
  181. text += GDScript::debug_get_script_name(value_script_type);
  182. text += ")";
  183. } else if (value_native_type != StringName()) {
  184. text += value_native_type;
  185. } else {
  186. text += Variant::get_type_name(value_builtin_type);
  187. }
  188. text += "]";
  189. incr += 9;
  190. } break;
  191. case OPCODE_TYPE_TEST_NATIVE: {
  192. text += "type test ";
  193. text += DADDR(1);
  194. text += " = ";
  195. text += DADDR(2);
  196. text += " is ";
  197. text += get_global_name(_code_ptr[ip + 3]);
  198. incr += 4;
  199. } break;
  200. case OPCODE_TYPE_TEST_SCRIPT: {
  201. text += "type test ";
  202. text += DADDR(1);
  203. text += " = ";
  204. text += DADDR(2);
  205. text += " is ";
  206. text += DADDR(3);
  207. incr += 4;
  208. } break;
  209. case OPCODE_SET_KEYED: {
  210. text += "set keyed ";
  211. text += DADDR(1);
  212. text += "[";
  213. text += DADDR(2);
  214. text += "] = ";
  215. text += DADDR(3);
  216. incr += 4;
  217. } break;
  218. case OPCODE_SET_KEYED_VALIDATED: {
  219. text += "set keyed validated ";
  220. text += DADDR(1);
  221. text += "[";
  222. text += DADDR(2);
  223. text += "] = ";
  224. text += DADDR(3);
  225. incr += 5;
  226. } break;
  227. case OPCODE_SET_INDEXED_VALIDATED: {
  228. text += "set indexed validated ";
  229. text += DADDR(1);
  230. text += "[";
  231. text += DADDR(2);
  232. text += "] = ";
  233. text += DADDR(3);
  234. incr += 5;
  235. } break;
  236. case OPCODE_GET_KEYED: {
  237. text += "get keyed ";
  238. text += DADDR(3);
  239. text += " = ";
  240. text += DADDR(1);
  241. text += "[";
  242. text += DADDR(2);
  243. text += "]";
  244. incr += 4;
  245. } break;
  246. case OPCODE_GET_KEYED_VALIDATED: {
  247. text += "get keyed validated ";
  248. text += DADDR(3);
  249. text += " = ";
  250. text += DADDR(1);
  251. text += "[";
  252. text += DADDR(2);
  253. text += "]";
  254. incr += 5;
  255. } break;
  256. case OPCODE_GET_INDEXED_VALIDATED: {
  257. text += "get indexed validated ";
  258. text += DADDR(3);
  259. text += " = ";
  260. text += DADDR(1);
  261. text += "[";
  262. text += DADDR(2);
  263. text += "]";
  264. incr += 5;
  265. } break;
  266. case OPCODE_SET_NAMED: {
  267. text += "set_named ";
  268. text += DADDR(1);
  269. text += "[\"";
  270. text += _global_names_ptr[_code_ptr[ip + 3]];
  271. text += "\"] = ";
  272. text += DADDR(2);
  273. incr += 4;
  274. } break;
  275. case OPCODE_SET_NAMED_VALIDATED: {
  276. text += "set_named validated ";
  277. text += DADDR(1);
  278. text += "[\"";
  279. text += setter_names[_code_ptr[ip + 3]];
  280. text += "\"] = ";
  281. text += DADDR(2);
  282. incr += 4;
  283. } break;
  284. case OPCODE_GET_NAMED: {
  285. text += "get_named ";
  286. text += DADDR(2);
  287. text += " = ";
  288. text += DADDR(1);
  289. text += "[\"";
  290. text += _global_names_ptr[_code_ptr[ip + 3]];
  291. text += "\"]";
  292. incr += 4;
  293. } break;
  294. case OPCODE_GET_NAMED_VALIDATED: {
  295. text += "get_named validated ";
  296. text += DADDR(2);
  297. text += " = ";
  298. text += DADDR(1);
  299. text += "[\"";
  300. text += getter_names[_code_ptr[ip + 3]];
  301. text += "\"]";
  302. incr += 4;
  303. } break;
  304. case OPCODE_SET_MEMBER: {
  305. text += "set_member ";
  306. text += "[\"";
  307. text += _global_names_ptr[_code_ptr[ip + 2]];
  308. text += "\"] = ";
  309. text += DADDR(1);
  310. incr += 3;
  311. } break;
  312. case OPCODE_GET_MEMBER: {
  313. text += "get_member ";
  314. text += DADDR(1);
  315. text += " = ";
  316. text += "[\"";
  317. text += _global_names_ptr[_code_ptr[ip + 2]];
  318. text += "\"]";
  319. incr += 3;
  320. } break;
  321. case OPCODE_SET_STATIC_VARIABLE: {
  322. Ref<GDScript> gdscript = get_constant(_code_ptr[ip + 2] & ADDR_MASK);
  323. text += "set_static_variable script(";
  324. text += GDScript::debug_get_script_name(gdscript);
  325. text += ")";
  326. if (gdscript.is_valid()) {
  327. text += "[\"" + gdscript->debug_get_static_var_by_index(_code_ptr[ip + 3]) + "\"]";
  328. } else {
  329. text += "[<index " + itos(_code_ptr[ip + 3]) + ">]";
  330. }
  331. text += " = ";
  332. text += DADDR(1);
  333. incr += 4;
  334. } break;
  335. case OPCODE_GET_STATIC_VARIABLE: {
  336. Ref<GDScript> gdscript = get_constant(_code_ptr[ip + 2] & ADDR_MASK);
  337. text += "get_static_variable ";
  338. text += DADDR(1);
  339. text += " = script(";
  340. text += GDScript::debug_get_script_name(gdscript);
  341. text += ")";
  342. if (gdscript.is_valid()) {
  343. text += "[\"" + gdscript->debug_get_static_var_by_index(_code_ptr[ip + 3]) + "\"]";
  344. } else {
  345. text += "[<index " + itos(_code_ptr[ip + 3]) + ">]";
  346. }
  347. incr += 4;
  348. } break;
  349. case OPCODE_ASSIGN: {
  350. text += "assign ";
  351. text += DADDR(1);
  352. text += " = ";
  353. text += DADDR(2);
  354. incr += 3;
  355. } break;
  356. case OPCODE_ASSIGN_NULL: {
  357. text += "assign ";
  358. text += DADDR(1);
  359. text += " = null";
  360. incr += 2;
  361. } break;
  362. case OPCODE_ASSIGN_TRUE: {
  363. text += "assign ";
  364. text += DADDR(1);
  365. text += " = true";
  366. incr += 2;
  367. } break;
  368. case OPCODE_ASSIGN_FALSE: {
  369. text += "assign ";
  370. text += DADDR(1);
  371. text += " = false";
  372. incr += 2;
  373. } break;
  374. case OPCODE_ASSIGN_TYPED_BUILTIN: {
  375. text += "assign typed builtin (";
  376. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 3]);
  377. text += ") ";
  378. text += DADDR(1);
  379. text += " = ";
  380. text += DADDR(2);
  381. incr += 4;
  382. } break;
  383. case OPCODE_ASSIGN_TYPED_ARRAY: {
  384. text += "assign typed array ";
  385. text += DADDR(1);
  386. text += " = ";
  387. text += DADDR(2);
  388. incr += 6;
  389. } break;
  390. case OPCODE_ASSIGN_TYPED_DICTIONARY: {
  391. text += "assign typed dictionary ";
  392. text += DADDR(1);
  393. text += " = ";
  394. text += DADDR(2);
  395. incr += 9;
  396. } break;
  397. case OPCODE_ASSIGN_TYPED_NATIVE: {
  398. text += "assign typed native (";
  399. text += DADDR(3);
  400. text += ") ";
  401. text += DADDR(1);
  402. text += " = ";
  403. text += DADDR(2);
  404. incr += 4;
  405. } break;
  406. case OPCODE_ASSIGN_TYPED_SCRIPT: {
  407. Ref<Script> script = get_constant(_code_ptr[ip + 3] & ADDR_MASK);
  408. text += "assign typed script (";
  409. text += GDScript::debug_get_script_name(script);
  410. text += ") ";
  411. text += DADDR(1);
  412. text += " = ";
  413. text += DADDR(2);
  414. incr += 4;
  415. } break;
  416. case OPCODE_CAST_TO_BUILTIN: {
  417. text += "cast builtin ";
  418. text += DADDR(2);
  419. text += " = ";
  420. text += DADDR(1);
  421. text += " as ";
  422. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 1]));
  423. incr += 4;
  424. } break;
  425. case OPCODE_CAST_TO_NATIVE: {
  426. text += "cast native ";
  427. text += DADDR(2);
  428. text += " = ";
  429. text += DADDR(1);
  430. text += " as ";
  431. text += DADDR(3);
  432. incr += 4;
  433. } break;
  434. case OPCODE_CAST_TO_SCRIPT: {
  435. text += "cast ";
  436. text += DADDR(2);
  437. text += " = ";
  438. text += DADDR(1);
  439. text += " as ";
  440. text += DADDR(3);
  441. incr += 4;
  442. } break;
  443. case OPCODE_CONSTRUCT: {
  444. int instr_var_args = _code_ptr[++ip];
  445. Variant::Type t = Variant::Type(_code_ptr[ip + 3 + instr_var_args]);
  446. int argc = _code_ptr[ip + 1 + instr_var_args];
  447. text += "construct ";
  448. text += DADDR(1 + argc);
  449. text += " = ";
  450. text += Variant::get_type_name(t) + "(";
  451. for (int i = 0; i < argc; i++) {
  452. if (i > 0) {
  453. text += ", ";
  454. }
  455. text += DADDR(i + 1);
  456. }
  457. text += ")";
  458. incr = 3 + instr_var_args;
  459. } break;
  460. case OPCODE_CONSTRUCT_VALIDATED: {
  461. int instr_var_args = _code_ptr[++ip];
  462. int argc = _code_ptr[ip + 1 + instr_var_args];
  463. text += "construct validated ";
  464. text += DADDR(1 + argc);
  465. text += " = ";
  466. text += constructors_names[_code_ptr[ip + 3 + argc]];
  467. text += "(";
  468. for (int i = 0; i < argc; i++) {
  469. if (i > 0) {
  470. text += ", ";
  471. }
  472. text += DADDR(i + 1);
  473. }
  474. text += ")";
  475. incr = 3 + instr_var_args;
  476. } break;
  477. case OPCODE_CONSTRUCT_ARRAY: {
  478. int instr_var_args = _code_ptr[++ip];
  479. int argc = _code_ptr[ip + 1 + instr_var_args];
  480. text += " make_array ";
  481. text += DADDR(1 + argc);
  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 += 3 + argc;
  491. } break;
  492. case OPCODE_CONSTRUCT_TYPED_ARRAY: {
  493. int instr_var_args = _code_ptr[++ip];
  494. int argc = _code_ptr[ip + 1 + instr_var_args];
  495. Ref<Script> script_type = get_constant(_code_ptr[ip + argc + 2] & ADDR_MASK);
  496. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + argc + 4];
  497. StringName native_type = get_global_name(_code_ptr[ip + argc + 5]);
  498. String type_name;
  499. if (script_type.is_valid() && script_type->is_valid()) {
  500. type_name = "script(" + GDScript::debug_get_script_name(script_type) + ")";
  501. } else if (native_type != StringName()) {
  502. type_name = native_type;
  503. } else {
  504. type_name = Variant::get_type_name(builtin_type);
  505. }
  506. text += " make_typed_array (";
  507. text += type_name;
  508. text += ") ";
  509. text += DADDR(1 + argc);
  510. text += " = [";
  511. for (int i = 0; i < argc; i++) {
  512. if (i > 0) {
  513. text += ", ";
  514. }
  515. text += DADDR(1 + i);
  516. }
  517. text += "]";
  518. incr += 6 + argc;
  519. } break;
  520. case OPCODE_CONSTRUCT_DICTIONARY: {
  521. int instr_var_args = _code_ptr[++ip];
  522. int argc = _code_ptr[ip + 1 + instr_var_args];
  523. text += "make_dict ";
  524. text += DADDR(1 + argc * 2);
  525. text += " = {";
  526. for (int i = 0; i < argc; i++) {
  527. if (i > 0) {
  528. text += ", ";
  529. }
  530. text += DADDR(1 + i * 2 + 0);
  531. text += ": ";
  532. text += DADDR(1 + i * 2 + 1);
  533. }
  534. text += "}";
  535. incr += 3 + argc * 2;
  536. } break;
  537. case OPCODE_CONSTRUCT_TYPED_DICTIONARY: {
  538. int instr_var_args = _code_ptr[++ip];
  539. int argc = _code_ptr[ip + 1 + instr_var_args];
  540. Ref<Script> key_script_type = get_constant(_code_ptr[ip + argc * 2 + 2] & ADDR_MASK);
  541. Variant::Type key_builtin_type = (Variant::Type)_code_ptr[ip + argc * 2 + 5];
  542. StringName key_native_type = get_global_name(_code_ptr[ip + argc * 2 + 6]);
  543. String key_type_name;
  544. if (key_script_type.is_valid() && key_script_type->is_valid()) {
  545. key_type_name = "script(" + GDScript::debug_get_script_name(key_script_type) + ")";
  546. } else if (key_native_type != StringName()) {
  547. key_type_name = key_native_type;
  548. } else {
  549. key_type_name = Variant::get_type_name(key_builtin_type);
  550. }
  551. Ref<Script> value_script_type = get_constant(_code_ptr[ip + argc * 2 + 3] & ADDR_MASK);
  552. Variant::Type value_builtin_type = (Variant::Type)_code_ptr[ip + argc * 2 + 7];
  553. StringName value_native_type = get_global_name(_code_ptr[ip + argc * 2 + 8]);
  554. String value_type_name;
  555. if (value_script_type.is_valid() && value_script_type->is_valid()) {
  556. value_type_name = "script(" + GDScript::debug_get_script_name(value_script_type) + ")";
  557. } else if (value_native_type != StringName()) {
  558. value_type_name = value_native_type;
  559. } else {
  560. value_type_name = Variant::get_type_name(value_builtin_type);
  561. }
  562. text += "make_typed_dict (";
  563. text += key_type_name;
  564. text += ", ";
  565. text += value_type_name;
  566. text += ") ";
  567. text += DADDR(1 + argc * 2);
  568. text += " = {";
  569. for (int i = 0; i < argc; i++) {
  570. if (i > 0) {
  571. text += ", ";
  572. }
  573. text += DADDR(1 + i * 2 + 0);
  574. text += ": ";
  575. text += DADDR(1 + i * 2 + 1);
  576. }
  577. text += "}";
  578. incr += 9 + argc * 2;
  579. } break;
  580. case OPCODE_CALL:
  581. case OPCODE_CALL_RETURN:
  582. case OPCODE_CALL_ASYNC: {
  583. bool ret = (_code_ptr[ip]) == OPCODE_CALL_RETURN;
  584. bool async = (_code_ptr[ip]) == OPCODE_CALL_ASYNC;
  585. int instr_var_args = _code_ptr[++ip];
  586. if (ret) {
  587. text += "call-ret ";
  588. } else if (async) {
  589. text += "call-async ";
  590. } else {
  591. text += "call ";
  592. }
  593. int argc = _code_ptr[ip + 1 + instr_var_args];
  594. if (ret || async) {
  595. text += DADDR(2 + argc) + " = ";
  596. }
  597. text += DADDR(1 + argc) + ".";
  598. text += String(_global_names_ptr[_code_ptr[ip + 2 + instr_var_args]]);
  599. text += "(";
  600. for (int i = 0; i < argc; i++) {
  601. if (i > 0) {
  602. text += ", ";
  603. }
  604. text += DADDR(1 + i);
  605. }
  606. text += ")";
  607. incr = 5 + argc;
  608. } break;
  609. case OPCODE_CALL_METHOD_BIND:
  610. case OPCODE_CALL_METHOD_BIND_RET: {
  611. bool ret = (_code_ptr[ip]) == OPCODE_CALL_METHOD_BIND_RET;
  612. int instr_var_args = _code_ptr[++ip];
  613. if (ret) {
  614. text += "call-method_bind-ret ";
  615. } else {
  616. text += "call-method_bind ";
  617. }
  618. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  619. int argc = _code_ptr[ip + 1 + instr_var_args];
  620. if (ret) {
  621. text += DADDR(2 + argc) + " = ";
  622. }
  623. text += DADDR(1 + argc) + ".";
  624. text += method->get_name();
  625. text += "(";
  626. for (int i = 0; i < argc; i++) {
  627. if (i > 0) {
  628. text += ", ";
  629. }
  630. text += DADDR(1 + i);
  631. }
  632. text += ")";
  633. incr = 5 + argc;
  634. } break;
  635. case OPCODE_CALL_BUILTIN_STATIC: {
  636. int instr_var_args = _code_ptr[++ip];
  637. Variant::Type type = (Variant::Type)_code_ptr[ip + 1 + instr_var_args];
  638. int argc = _code_ptr[ip + 3 + instr_var_args];
  639. text += "call built-in method static ";
  640. text += DADDR(1 + argc);
  641. text += " = ";
  642. text += Variant::get_type_name(type);
  643. text += ".";
  644. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]].operator String();
  645. text += "(";
  646. for (int i = 0; i < argc; i++) {
  647. if (i > 0) {
  648. text += ", ";
  649. }
  650. text += DADDR(1 + i);
  651. }
  652. text += ")";
  653. incr += 5 + argc;
  654. } break;
  655. case OPCODE_CALL_NATIVE_STATIC: {
  656. int instr_var_args = _code_ptr[++ip];
  657. MethodBind *method = _methods_ptr[_code_ptr[ip + 1 + instr_var_args]];
  658. int argc = _code_ptr[ip + 2 + instr_var_args];
  659. text += "call native method static ";
  660. text += DADDR(1 + argc);
  661. text += " = ";
  662. text += method->get_instance_class();
  663. text += ".";
  664. text += method->get_name();
  665. text += "(";
  666. for (int i = 0; i < argc; i++) {
  667. if (i > 0) {
  668. text += ", ";
  669. }
  670. text += DADDR(1 + i);
  671. }
  672. text += ")";
  673. incr += 4 + argc;
  674. } break;
  675. case OPCODE_CALL_NATIVE_STATIC_VALIDATED_RETURN: {
  676. int instr_var_args = _code_ptr[++ip];
  677. text += "call native static method validated (return) ";
  678. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  679. int argc = _code_ptr[ip + 1 + instr_var_args];
  680. text += DADDR(1 + argc) + " = ";
  681. text += method->get_instance_class();
  682. text += ".";
  683. text += method->get_name();
  684. text += "(";
  685. for (int i = 0; i < argc; i++) {
  686. if (i > 0) {
  687. text += ", ";
  688. }
  689. text += DADDR(1 + i);
  690. }
  691. text += ")";
  692. incr = 4 + argc;
  693. } break;
  694. case OPCODE_CALL_NATIVE_STATIC_VALIDATED_NO_RETURN: {
  695. int instr_var_args = _code_ptr[++ip];
  696. text += "call native static method validated (no return) ";
  697. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  698. int argc = _code_ptr[ip + 1 + instr_var_args];
  699. text += method->get_instance_class();
  700. text += ".";
  701. text += method->get_name();
  702. text += "(";
  703. for (int i = 0; i < argc; i++) {
  704. if (i > 0) {
  705. text += ", ";
  706. }
  707. text += DADDR(1 + i);
  708. }
  709. text += ")";
  710. incr = 4 + argc;
  711. } break;
  712. case OPCODE_CALL_METHOD_BIND_VALIDATED_RETURN: {
  713. int instr_var_args = _code_ptr[++ip];
  714. text += "call method-bind validated (return) ";
  715. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  716. int argc = _code_ptr[ip + 1 + instr_var_args];
  717. text += DADDR(2 + argc) + " = ";
  718. text += DADDR(1 + argc) + ".";
  719. text += method->get_name();
  720. text += "(";
  721. for (int i = 0; i < argc; i++) {
  722. if (i > 0) {
  723. text += ", ";
  724. }
  725. text += DADDR(1 + i);
  726. }
  727. text += ")";
  728. incr = 5 + argc;
  729. } break;
  730. case OPCODE_CALL_METHOD_BIND_VALIDATED_NO_RETURN: {
  731. int instr_var_args = _code_ptr[++ip];
  732. text += "call method-bind validated (no return) ";
  733. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  734. int argc = _code_ptr[ip + 1 + instr_var_args];
  735. text += DADDR(1 + argc) + ".";
  736. text += method->get_name();
  737. text += "(";
  738. for (int i = 0; i < argc; i++) {
  739. if (i > 0) {
  740. text += ", ";
  741. }
  742. text += DADDR(1 + i);
  743. }
  744. text += ")";
  745. incr = 5 + argc;
  746. } break;
  747. case OPCODE_CALL_BUILTIN_TYPE_VALIDATED: {
  748. int instr_var_args = _code_ptr[++ip];
  749. int argc = _code_ptr[ip + 1 + instr_var_args];
  750. text += "call-builtin-method validated ";
  751. text += DADDR(2 + argc) + " = ";
  752. text += DADDR(1) + ".";
  753. text += builtin_methods_names[_code_ptr[ip + 4 + argc]];
  754. text += "(";
  755. for (int i = 0; i < argc; i++) {
  756. if (i > 0) {
  757. text += ", ";
  758. }
  759. text += DADDR(1 + i);
  760. }
  761. text += ")";
  762. incr = 5 + argc;
  763. } break;
  764. case OPCODE_CALL_UTILITY: {
  765. int instr_var_args = _code_ptr[++ip];
  766. text += "call-utility ";
  767. int argc = _code_ptr[ip + 1 + instr_var_args];
  768. text += DADDR(1 + argc) + " = ";
  769. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  770. text += "(";
  771. for (int i = 0; i < argc; i++) {
  772. if (i > 0) {
  773. text += ", ";
  774. }
  775. text += DADDR(1 + i);
  776. }
  777. text += ")";
  778. incr = 4 + argc;
  779. } break;
  780. case OPCODE_CALL_UTILITY_VALIDATED: {
  781. int instr_var_args = _code_ptr[++ip];
  782. text += "call-utility validated ";
  783. int argc = _code_ptr[ip + 1 + instr_var_args];
  784. text += DADDR(1 + argc) + " = ";
  785. text += utilities_names[_code_ptr[ip + 3 + argc]];
  786. text += "(";
  787. for (int i = 0; i < argc; i++) {
  788. if (i > 0) {
  789. text += ", ";
  790. }
  791. text += DADDR(1 + i);
  792. }
  793. text += ")";
  794. incr = 4 + argc;
  795. } break;
  796. case OPCODE_CALL_GDSCRIPT_UTILITY: {
  797. int instr_var_args = _code_ptr[++ip];
  798. text += "call-gdscript-utility ";
  799. int argc = _code_ptr[ip + 1 + instr_var_args];
  800. text += DADDR(1 + argc) + " = ";
  801. text += gds_utilities_names[_code_ptr[ip + 3 + argc]];
  802. text += "(";
  803. for (int i = 0; i < argc; i++) {
  804. if (i > 0) {
  805. text += ", ";
  806. }
  807. text += DADDR(1 + i);
  808. }
  809. text += ")";
  810. incr = 4 + argc;
  811. } break;
  812. case OPCODE_CALL_SELF_BASE: {
  813. int instr_var_args = _code_ptr[++ip];
  814. text += "call-self-base ";
  815. int argc = _code_ptr[ip + 1 + instr_var_args];
  816. text += DADDR(2 + argc) + " = ";
  817. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  818. text += "(";
  819. for (int i = 0; i < argc; i++) {
  820. if (i > 0) {
  821. text += ", ";
  822. }
  823. text += DADDR(1 + i);
  824. }
  825. text += ")";
  826. incr = 4 + argc;
  827. } break;
  828. case OPCODE_AWAIT: {
  829. text += "await ";
  830. text += DADDR(1);
  831. incr = 2;
  832. } break;
  833. case OPCODE_AWAIT_RESUME: {
  834. text += "await resume ";
  835. text += DADDR(1);
  836. incr = 2;
  837. } break;
  838. case OPCODE_CREATE_LAMBDA: {
  839. int instr_var_args = _code_ptr[++ip];
  840. int captures_count = _code_ptr[ip + 1 + instr_var_args];
  841. GDScriptFunction *lambda = _lambdas_ptr[_code_ptr[ip + 2 + instr_var_args]];
  842. text += DADDR(1 + captures_count);
  843. text += "create lambda from ";
  844. text += lambda->name.operator String();
  845. text += "function, captures (";
  846. for (int i = 0; i < captures_count; i++) {
  847. if (i > 0) {
  848. text += ", ";
  849. }
  850. text += DADDR(1 + i);
  851. }
  852. text += ")";
  853. incr = 4 + captures_count;
  854. } break;
  855. case OPCODE_CREATE_SELF_LAMBDA: {
  856. int instr_var_args = _code_ptr[++ip];
  857. int captures_count = _code_ptr[ip + 1 + instr_var_args];
  858. GDScriptFunction *lambda = _lambdas_ptr[_code_ptr[ip + 2 + instr_var_args]];
  859. text += DADDR(1 + captures_count);
  860. text += "create self lambda from ";
  861. text += lambda->name.operator String();
  862. text += "function, captures (";
  863. for (int i = 0; i < captures_count; i++) {
  864. if (i > 0) {
  865. text += ", ";
  866. }
  867. text += DADDR(1 + i);
  868. }
  869. text += ")";
  870. incr = 4 + captures_count;
  871. } break;
  872. case OPCODE_JUMP: {
  873. text += "jump ";
  874. text += itos(_code_ptr[ip + 1]);
  875. incr = 2;
  876. } break;
  877. case OPCODE_JUMP_IF: {
  878. text += "jump-if ";
  879. text += DADDR(1);
  880. text += " to ";
  881. text += itos(_code_ptr[ip + 2]);
  882. incr = 3;
  883. } break;
  884. case OPCODE_JUMP_IF_NOT: {
  885. text += "jump-if-not ";
  886. text += DADDR(1);
  887. text += " to ";
  888. text += itos(_code_ptr[ip + 2]);
  889. incr = 3;
  890. } break;
  891. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  892. text += "jump-to-default-argument ";
  893. incr = 1;
  894. } break;
  895. case OPCODE_JUMP_IF_SHARED: {
  896. text += "jump-if-shared ";
  897. text += DADDR(1);
  898. text += " to ";
  899. text += itos(_code_ptr[ip + 2]);
  900. incr = 3;
  901. } break;
  902. case OPCODE_RETURN: {
  903. text += "return ";
  904. text += DADDR(1);
  905. incr = 2;
  906. } break;
  907. case OPCODE_RETURN_TYPED_BUILTIN: {
  908. text += "return typed builtin (";
  909. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 2]);
  910. text += ") ";
  911. text += DADDR(1);
  912. incr += 3;
  913. } break;
  914. case OPCODE_RETURN_TYPED_ARRAY: {
  915. text += "return typed array ";
  916. text += DADDR(1);
  917. incr += 5;
  918. } break;
  919. case OPCODE_RETURN_TYPED_DICTIONARY: {
  920. text += "return typed dictionary ";
  921. text += DADDR(1);
  922. incr += 8;
  923. } break;
  924. case OPCODE_RETURN_TYPED_NATIVE: {
  925. text += "return typed native (";
  926. text += DADDR(2);
  927. text += ") ";
  928. text += DADDR(1);
  929. incr += 3;
  930. } break;
  931. case OPCODE_RETURN_TYPED_SCRIPT: {
  932. Ref<Script> script = get_constant(_code_ptr[ip + 2] & ADDR_MASK);
  933. text += "return typed script (";
  934. text += GDScript::debug_get_script_name(script);
  935. text += ") ";
  936. text += DADDR(1);
  937. incr += 3;
  938. } break;
  939. #define DISASSEMBLE_ITERATE(m_type) \
  940. case OPCODE_ITERATE_##m_type: { \
  941. text += "for-loop (typed "; \
  942. text += #m_type; \
  943. text += ") "; \
  944. text += DADDR(3); \
  945. text += " in "; \
  946. text += DADDR(2); \
  947. text += " counter "; \
  948. text += DADDR(1); \
  949. text += " end "; \
  950. text += itos(_code_ptr[ip + 4]); \
  951. incr += 5; \
  952. } break
  953. #define DISASSEMBLE_ITERATE_BEGIN(m_type) \
  954. case OPCODE_ITERATE_BEGIN_##m_type: { \
  955. text += "for-init (typed "; \
  956. text += #m_type; \
  957. text += ") "; \
  958. text += DADDR(3); \
  959. text += " in "; \
  960. text += DADDR(2); \
  961. text += " counter "; \
  962. text += DADDR(1); \
  963. text += " end "; \
  964. text += itos(_code_ptr[ip + 4]); \
  965. incr += 5; \
  966. } break
  967. #define DISASSEMBLE_ITERATE_TYPES(m_macro) \
  968. m_macro(INT); \
  969. m_macro(FLOAT); \
  970. m_macro(VECTOR2); \
  971. m_macro(VECTOR2I); \
  972. m_macro(VECTOR3); \
  973. m_macro(VECTOR3I); \
  974. m_macro(STRING); \
  975. m_macro(DICTIONARY); \
  976. m_macro(ARRAY); \
  977. m_macro(PACKED_BYTE_ARRAY); \
  978. m_macro(PACKED_INT32_ARRAY); \
  979. m_macro(PACKED_INT64_ARRAY); \
  980. m_macro(PACKED_FLOAT32_ARRAY); \
  981. m_macro(PACKED_FLOAT64_ARRAY); \
  982. m_macro(PACKED_STRING_ARRAY); \
  983. m_macro(PACKED_VECTOR2_ARRAY); \
  984. m_macro(PACKED_VECTOR3_ARRAY); \
  985. m_macro(PACKED_COLOR_ARRAY); \
  986. m_macro(PACKED_VECTOR4_ARRAY); \
  987. m_macro(OBJECT)
  988. case OPCODE_ITERATE_BEGIN: {
  989. text += "for-init ";
  990. text += DADDR(3);
  991. text += " in ";
  992. text += DADDR(2);
  993. text += " counter ";
  994. text += DADDR(1);
  995. text += " end ";
  996. text += itos(_code_ptr[ip + 4]);
  997. incr += 5;
  998. } break;
  999. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE_BEGIN);
  1000. case OPCODE_ITERATE: {
  1001. text += "for-loop ";
  1002. text += DADDR(2);
  1003. text += " in ";
  1004. text += DADDR(2);
  1005. text += " counter ";
  1006. text += DADDR(1);
  1007. text += " end ";
  1008. text += itos(_code_ptr[ip + 4]);
  1009. incr += 5;
  1010. } break;
  1011. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE);
  1012. case OPCODE_STORE_GLOBAL: {
  1013. text += "store global ";
  1014. text += DADDR(1);
  1015. text += " = ";
  1016. text += String::num_int64(_code_ptr[ip + 2]);
  1017. incr += 3;
  1018. } break;
  1019. case OPCODE_STORE_NAMED_GLOBAL: {
  1020. text += "store named global ";
  1021. text += DADDR(1);
  1022. text += " = ";
  1023. text += String(_global_names_ptr[_code_ptr[ip + 2]]);
  1024. incr += 3;
  1025. } break;
  1026. case OPCODE_LINE: {
  1027. int line = _code_ptr[ip + 1] - 1;
  1028. if (line >= 0 && line < p_code_lines.size()) {
  1029. text += "line ";
  1030. text += itos(line + 1);
  1031. text += ": ";
  1032. text += p_code_lines[line];
  1033. } else {
  1034. text += "";
  1035. }
  1036. incr += 2;
  1037. } break;
  1038. #define DISASSEMBLE_TYPE_ADJUST(m_v_type) \
  1039. case OPCODE_TYPE_ADJUST_##m_v_type: { \
  1040. text += "type adjust ("; \
  1041. text += #m_v_type; \
  1042. text += ") "; \
  1043. text += DADDR(1); \
  1044. incr += 2; \
  1045. } break
  1046. DISASSEMBLE_TYPE_ADJUST(BOOL);
  1047. DISASSEMBLE_TYPE_ADJUST(INT);
  1048. DISASSEMBLE_TYPE_ADJUST(FLOAT);
  1049. DISASSEMBLE_TYPE_ADJUST(STRING);
  1050. DISASSEMBLE_TYPE_ADJUST(VECTOR2);
  1051. DISASSEMBLE_TYPE_ADJUST(VECTOR2I);
  1052. DISASSEMBLE_TYPE_ADJUST(RECT2);
  1053. DISASSEMBLE_TYPE_ADJUST(RECT2I);
  1054. DISASSEMBLE_TYPE_ADJUST(VECTOR3);
  1055. DISASSEMBLE_TYPE_ADJUST(VECTOR3I);
  1056. DISASSEMBLE_TYPE_ADJUST(TRANSFORM2D);
  1057. DISASSEMBLE_TYPE_ADJUST(VECTOR4);
  1058. DISASSEMBLE_TYPE_ADJUST(VECTOR4I);
  1059. DISASSEMBLE_TYPE_ADJUST(PLANE);
  1060. DISASSEMBLE_TYPE_ADJUST(QUATERNION);
  1061. DISASSEMBLE_TYPE_ADJUST(AABB);
  1062. DISASSEMBLE_TYPE_ADJUST(BASIS);
  1063. DISASSEMBLE_TYPE_ADJUST(TRANSFORM3D);
  1064. DISASSEMBLE_TYPE_ADJUST(PROJECTION);
  1065. DISASSEMBLE_TYPE_ADJUST(COLOR);
  1066. DISASSEMBLE_TYPE_ADJUST(STRING_NAME);
  1067. DISASSEMBLE_TYPE_ADJUST(NODE_PATH);
  1068. DISASSEMBLE_TYPE_ADJUST(RID);
  1069. DISASSEMBLE_TYPE_ADJUST(OBJECT);
  1070. DISASSEMBLE_TYPE_ADJUST(CALLABLE);
  1071. DISASSEMBLE_TYPE_ADJUST(SIGNAL);
  1072. DISASSEMBLE_TYPE_ADJUST(DICTIONARY);
  1073. DISASSEMBLE_TYPE_ADJUST(ARRAY);
  1074. DISASSEMBLE_TYPE_ADJUST(PACKED_BYTE_ARRAY);
  1075. DISASSEMBLE_TYPE_ADJUST(PACKED_INT32_ARRAY);
  1076. DISASSEMBLE_TYPE_ADJUST(PACKED_INT64_ARRAY);
  1077. DISASSEMBLE_TYPE_ADJUST(PACKED_FLOAT32_ARRAY);
  1078. DISASSEMBLE_TYPE_ADJUST(PACKED_FLOAT64_ARRAY);
  1079. DISASSEMBLE_TYPE_ADJUST(PACKED_STRING_ARRAY);
  1080. DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR2_ARRAY);
  1081. DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR3_ARRAY);
  1082. DISASSEMBLE_TYPE_ADJUST(PACKED_COLOR_ARRAY);
  1083. DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR4_ARRAY);
  1084. case OPCODE_ASSERT: {
  1085. text += "assert (";
  1086. text += DADDR(1);
  1087. text += ", ";
  1088. text += DADDR(2);
  1089. text += ")";
  1090. incr += 3;
  1091. } break;
  1092. case OPCODE_BREAKPOINT: {
  1093. text += "breakpoint";
  1094. incr += 1;
  1095. } break;
  1096. case OPCODE_END: {
  1097. text += "== END ==";
  1098. incr += 1;
  1099. } break;
  1100. }
  1101. ip += incr;
  1102. if (text.get_string_length() > 0) {
  1103. print_line(text.as_string());
  1104. }
  1105. }
  1106. }
  1107. #endif // DEBUG_ENABLED