gdscript_disassembler.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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_NATIVE: {
  158. text += "type test ";
  159. text += DADDR(1);
  160. text += " = ";
  161. text += DADDR(2);
  162. text += " is ";
  163. text += get_global_name(_code_ptr[ip + 3]);
  164. incr += 4;
  165. } break;
  166. case OPCODE_TYPE_TEST_SCRIPT: {
  167. text += "type test ";
  168. text += DADDR(1);
  169. text += " = ";
  170. text += DADDR(2);
  171. text += " is ";
  172. text += DADDR(3);
  173. incr += 4;
  174. } break;
  175. case OPCODE_SET_KEYED: {
  176. text += "set keyed ";
  177. text += DADDR(1);
  178. text += "[";
  179. text += DADDR(2);
  180. text += "] = ";
  181. text += DADDR(3);
  182. incr += 4;
  183. } break;
  184. case OPCODE_SET_KEYED_VALIDATED: {
  185. text += "set keyed validated ";
  186. text += DADDR(1);
  187. text += "[";
  188. text += DADDR(2);
  189. text += "] = ";
  190. text += DADDR(3);
  191. incr += 5;
  192. } break;
  193. case OPCODE_SET_INDEXED_VALIDATED: {
  194. text += "set indexed validated ";
  195. text += DADDR(1);
  196. text += "[";
  197. text += DADDR(2);
  198. text += "] = ";
  199. text += DADDR(3);
  200. incr += 5;
  201. } break;
  202. case OPCODE_GET_KEYED: {
  203. text += "get keyed ";
  204. text += DADDR(3);
  205. text += " = ";
  206. text += DADDR(1);
  207. text += "[";
  208. text += DADDR(2);
  209. text += "]";
  210. incr += 4;
  211. } break;
  212. case OPCODE_GET_KEYED_VALIDATED: {
  213. text += "get keyed validated ";
  214. text += DADDR(3);
  215. text += " = ";
  216. text += DADDR(1);
  217. text += "[";
  218. text += DADDR(2);
  219. text += "]";
  220. incr += 5;
  221. } break;
  222. case OPCODE_GET_INDEXED_VALIDATED: {
  223. text += "get indexed validated ";
  224. text += DADDR(3);
  225. text += " = ";
  226. text += DADDR(1);
  227. text += "[";
  228. text += DADDR(2);
  229. text += "]";
  230. incr += 5;
  231. } break;
  232. case OPCODE_SET_NAMED: {
  233. text += "set_named ";
  234. text += DADDR(1);
  235. text += "[\"";
  236. text += _global_names_ptr[_code_ptr[ip + 3]];
  237. text += "\"] = ";
  238. text += DADDR(2);
  239. incr += 4;
  240. } break;
  241. case OPCODE_SET_NAMED_VALIDATED: {
  242. text += "set_named validated ";
  243. text += DADDR(1);
  244. text += "[\"";
  245. text += setter_names[_code_ptr[ip + 3]];
  246. text += "\"] = ";
  247. text += DADDR(2);
  248. incr += 4;
  249. } break;
  250. case OPCODE_GET_NAMED: {
  251. text += "get_named ";
  252. text += DADDR(2);
  253. text += " = ";
  254. text += DADDR(1);
  255. text += "[\"";
  256. text += _global_names_ptr[_code_ptr[ip + 3]];
  257. text += "\"]";
  258. incr += 4;
  259. } break;
  260. case OPCODE_GET_NAMED_VALIDATED: {
  261. text += "get_named validated ";
  262. text += DADDR(2);
  263. text += " = ";
  264. text += DADDR(1);
  265. text += "[\"";
  266. text += getter_names[_code_ptr[ip + 3]];
  267. text += "\"]";
  268. incr += 4;
  269. } break;
  270. case OPCODE_SET_MEMBER: {
  271. text += "set_member ";
  272. text += "[\"";
  273. text += _global_names_ptr[_code_ptr[ip + 2]];
  274. text += "\"] = ";
  275. text += DADDR(1);
  276. incr += 3;
  277. } break;
  278. case OPCODE_GET_MEMBER: {
  279. text += "get_member ";
  280. text += DADDR(1);
  281. text += " = ";
  282. text += "[\"";
  283. text += _global_names_ptr[_code_ptr[ip + 2]];
  284. text += "\"]";
  285. incr += 3;
  286. } break;
  287. case OPCODE_SET_STATIC_VARIABLE: {
  288. Ref<GDScript> gdscript = get_constant(_code_ptr[ip + 2] & ADDR_MASK);
  289. text += "set_static_variable script(";
  290. text += GDScript::debug_get_script_name(gdscript);
  291. text += ")";
  292. if (gdscript.is_valid()) {
  293. text += "[\"" + gdscript->debug_get_static_var_by_index(_code_ptr[ip + 3]) + "\"]";
  294. } else {
  295. text += "[<index " + itos(_code_ptr[ip + 3]) + ">]";
  296. }
  297. text += " = ";
  298. text += DADDR(1);
  299. incr += 4;
  300. } break;
  301. case OPCODE_GET_STATIC_VARIABLE: {
  302. Ref<GDScript> gdscript = get_constant(_code_ptr[ip + 2] & ADDR_MASK);
  303. text += "get_static_variable ";
  304. text += DADDR(1);
  305. text += " = script(";
  306. text += GDScript::debug_get_script_name(gdscript);
  307. text += ")";
  308. if (gdscript.is_valid()) {
  309. text += "[\"" + gdscript->debug_get_static_var_by_index(_code_ptr[ip + 3]) + "\"]";
  310. } else {
  311. text += "[<index " + itos(_code_ptr[ip + 3]) + ">]";
  312. }
  313. incr += 4;
  314. } break;
  315. case OPCODE_ASSIGN: {
  316. text += "assign ";
  317. text += DADDR(1);
  318. text += " = ";
  319. text += DADDR(2);
  320. incr += 3;
  321. } break;
  322. case OPCODE_ASSIGN_TRUE: {
  323. text += "assign ";
  324. text += DADDR(1);
  325. text += " = true";
  326. incr += 2;
  327. } break;
  328. case OPCODE_ASSIGN_FALSE: {
  329. text += "assign ";
  330. text += DADDR(1);
  331. text += " = false";
  332. incr += 2;
  333. } break;
  334. case OPCODE_ASSIGN_TYPED_BUILTIN: {
  335. text += "assign typed builtin (";
  336. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 3]);
  337. text += ") ";
  338. text += DADDR(1);
  339. text += " = ";
  340. text += DADDR(2);
  341. incr += 4;
  342. } break;
  343. case OPCODE_ASSIGN_TYPED_ARRAY: {
  344. text += "assign typed array ";
  345. text += DADDR(1);
  346. text += " = ";
  347. text += DADDR(2);
  348. incr += 6;
  349. } break;
  350. case OPCODE_ASSIGN_TYPED_NATIVE: {
  351. text += "assign typed native (";
  352. text += DADDR(3);
  353. text += ") ";
  354. text += DADDR(1);
  355. text += " = ";
  356. text += DADDR(2);
  357. incr += 4;
  358. } break;
  359. case OPCODE_ASSIGN_TYPED_SCRIPT: {
  360. Ref<Script> script = get_constant(_code_ptr[ip + 3] & ADDR_MASK);
  361. text += "assign typed script (";
  362. text += GDScript::debug_get_script_name(script);
  363. text += ") ";
  364. text += DADDR(1);
  365. text += " = ";
  366. text += DADDR(2);
  367. incr += 4;
  368. } break;
  369. case OPCODE_CAST_TO_BUILTIN: {
  370. text += "cast builtin ";
  371. text += DADDR(2);
  372. text += " = ";
  373. text += DADDR(1);
  374. text += " as ";
  375. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 1]));
  376. incr += 4;
  377. } break;
  378. case OPCODE_CAST_TO_NATIVE: {
  379. text += "cast native ";
  380. text += DADDR(2);
  381. text += " = ";
  382. text += DADDR(1);
  383. text += " as ";
  384. text += DADDR(3);
  385. incr += 4;
  386. } break;
  387. case OPCODE_CAST_TO_SCRIPT: {
  388. text += "cast ";
  389. text += DADDR(2);
  390. text += " = ";
  391. text += DADDR(1);
  392. text += " as ";
  393. text += DADDR(3);
  394. incr += 4;
  395. } break;
  396. case OPCODE_CONSTRUCT: {
  397. int instr_var_args = _code_ptr[++ip];
  398. Variant::Type t = Variant::Type(_code_ptr[ip + 3 + instr_var_args]);
  399. int argc = _code_ptr[ip + 1 + instr_var_args];
  400. text += "construct ";
  401. text += DADDR(1 + argc);
  402. text += " = ";
  403. text += Variant::get_type_name(t) + "(";
  404. for (int i = 0; i < argc; i++) {
  405. if (i > 0) {
  406. text += ", ";
  407. }
  408. text += DADDR(i + 1);
  409. }
  410. text += ")";
  411. incr = 3 + instr_var_args;
  412. } break;
  413. case OPCODE_CONSTRUCT_VALIDATED: {
  414. int instr_var_args = _code_ptr[++ip];
  415. int argc = _code_ptr[ip + 1 + instr_var_args];
  416. text += "construct validated ";
  417. text += DADDR(1 + argc);
  418. text += " = ";
  419. text += constructors_names[_code_ptr[ip + 3 + argc]];
  420. text += "(";
  421. for (int i = 0; i < argc; i++) {
  422. if (i > 0) {
  423. text += ", ";
  424. }
  425. text += DADDR(i + 1);
  426. }
  427. text += ")";
  428. incr = 3 + instr_var_args;
  429. } break;
  430. case OPCODE_CONSTRUCT_ARRAY: {
  431. int instr_var_args = _code_ptr[++ip];
  432. int argc = _code_ptr[ip + 1 + instr_var_args];
  433. text += " make_array ";
  434. text += DADDR(1 + argc);
  435. text += " = [";
  436. for (int i = 0; i < argc; i++) {
  437. if (i > 0) {
  438. text += ", ";
  439. }
  440. text += DADDR(1 + i);
  441. }
  442. text += "]";
  443. incr += 3 + argc;
  444. } break;
  445. case OPCODE_CONSTRUCT_TYPED_ARRAY: {
  446. int instr_var_args = _code_ptr[++ip];
  447. int argc = _code_ptr[ip + 1 + instr_var_args];
  448. Ref<Script> script_type = get_constant(_code_ptr[ip + argc + 2] & ADDR_MASK);
  449. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + argc + 4];
  450. StringName native_type = get_global_name(_code_ptr[ip + argc + 5]);
  451. String type_name;
  452. if (script_type.is_valid() && script_type->is_valid()) {
  453. type_name = "script(" + GDScript::debug_get_script_name(script_type) + ")";
  454. } else if (native_type != StringName()) {
  455. type_name = native_type;
  456. } else {
  457. type_name = Variant::get_type_name(builtin_type);
  458. }
  459. text += " make_typed_array (";
  460. text += type_name;
  461. text += ") ";
  462. text += DADDR(1 + argc);
  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 += 6 + argc;
  472. } break;
  473. case OPCODE_CONSTRUCT_DICTIONARY: {
  474. int instr_var_args = _code_ptr[++ip];
  475. int argc = _code_ptr[ip + 1 + instr_var_args];
  476. text += "make_dict ";
  477. text += DADDR(1 + argc * 2);
  478. text += " = {";
  479. for (int i = 0; i < argc; i++) {
  480. if (i > 0) {
  481. text += ", ";
  482. }
  483. text += DADDR(1 + i * 2 + 0);
  484. text += ": ";
  485. text += DADDR(1 + i * 2 + 1);
  486. }
  487. text += "}";
  488. incr += 3 + argc * 2;
  489. } break;
  490. case OPCODE_CALL:
  491. case OPCODE_CALL_RETURN:
  492. case OPCODE_CALL_ASYNC: {
  493. bool ret = (_code_ptr[ip]) == OPCODE_CALL_RETURN;
  494. bool async = (_code_ptr[ip]) == OPCODE_CALL_ASYNC;
  495. int instr_var_args = _code_ptr[++ip];
  496. if (ret) {
  497. text += "call-ret ";
  498. } else if (async) {
  499. text += "call-async ";
  500. } else {
  501. text += "call ";
  502. }
  503. int argc = _code_ptr[ip + 1 + instr_var_args];
  504. if (ret || async) {
  505. text += DADDR(2 + argc) + " = ";
  506. }
  507. text += DADDR(1 + argc) + ".";
  508. text += String(_global_names_ptr[_code_ptr[ip + 2 + instr_var_args]]);
  509. text += "(";
  510. for (int i = 0; i < argc; i++) {
  511. if (i > 0) {
  512. text += ", ";
  513. }
  514. text += DADDR(1 + i);
  515. }
  516. text += ")";
  517. incr = 5 + argc;
  518. } break;
  519. case OPCODE_CALL_METHOD_BIND:
  520. case OPCODE_CALL_METHOD_BIND_RET: {
  521. bool ret = (_code_ptr[ip]) == OPCODE_CALL_METHOD_BIND_RET;
  522. int instr_var_args = _code_ptr[++ip];
  523. if (ret) {
  524. text += "call-method_bind-ret ";
  525. } else {
  526. text += "call-method_bind ";
  527. }
  528. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  529. int argc = _code_ptr[ip + 1 + instr_var_args];
  530. if (ret) {
  531. text += DADDR(2 + argc) + " = ";
  532. }
  533. text += DADDR(1 + argc) + ".";
  534. text += method->get_name();
  535. text += "(";
  536. for (int i = 0; i < argc; i++) {
  537. if (i > 0) {
  538. text += ", ";
  539. }
  540. text += DADDR(1 + i);
  541. }
  542. text += ")";
  543. incr = 5 + argc;
  544. } break;
  545. case OPCODE_CALL_BUILTIN_STATIC: {
  546. int instr_var_args = _code_ptr[++ip];
  547. Variant::Type type = (Variant::Type)_code_ptr[ip + 1 + instr_var_args];
  548. int argc = _code_ptr[ip + 3 + instr_var_args];
  549. text += "call built-in method static ";
  550. text += DADDR(1 + argc);
  551. text += " = ";
  552. text += Variant::get_type_name(type);
  553. text += ".";
  554. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]].operator String();
  555. text += "(";
  556. for (int i = 0; i < argc; i++) {
  557. if (i > 0) {
  558. text += ", ";
  559. }
  560. text += DADDR(1 + i);
  561. }
  562. text += ")";
  563. incr += 5 + argc;
  564. } break;
  565. case OPCODE_CALL_NATIVE_STATIC: {
  566. int instr_var_args = _code_ptr[++ip];
  567. MethodBind *method = _methods_ptr[_code_ptr[ip + 1 + instr_var_args]];
  568. int argc = _code_ptr[ip + 2 + instr_var_args];
  569. text += "call native method static ";
  570. text += DADDR(1 + argc);
  571. text += " = ";
  572. text += method->get_instance_class();
  573. text += ".";
  574. text += method->get_name();
  575. text += "(";
  576. for (int i = 0; i < argc; i++) {
  577. if (i > 0) {
  578. text += ", ";
  579. }
  580. text += DADDR(1 + i);
  581. }
  582. text += ")";
  583. incr += 4 + argc;
  584. } break;
  585. case OPCODE_CALL_PTRCALL_NO_RETURN: {
  586. int instr_var_args = _code_ptr[++ip];
  587. text += "call-ptrcall (no return) ";
  588. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  589. int argc = _code_ptr[ip + 1 + instr_var_args];
  590. text += DADDR(1 + argc) + ".";
  591. text += method->get_name();
  592. text += "(";
  593. for (int i = 0; i < argc; i++) {
  594. if (i > 0) {
  595. text += ", ";
  596. }
  597. text += DADDR(1 + i);
  598. }
  599. text += ")";
  600. incr = 5 + argc;
  601. } break;
  602. #define DISASSEMBLE_PTRCALL(m_type) \
  603. case OPCODE_CALL_PTRCALL_##m_type: { \
  604. int instr_var_args = _code_ptr[++ip]; \
  605. text += "call-ptrcall (return "; \
  606. text += #m_type; \
  607. text += ") "; \
  608. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]]; \
  609. int argc = _code_ptr[ip + 1 + instr_var_args]; \
  610. text += DADDR(2 + argc) + " = "; \
  611. text += DADDR(1 + argc) + "."; \
  612. text += method->get_name(); \
  613. text += "("; \
  614. for (int i = 0; i < argc; i++) { \
  615. if (i > 0) \
  616. text += ", "; \
  617. text += DADDR(1 + i); \
  618. } \
  619. text += ")"; \
  620. incr = 5 + argc; \
  621. } break
  622. DISASSEMBLE_PTRCALL(BOOL);
  623. DISASSEMBLE_PTRCALL(INT);
  624. DISASSEMBLE_PTRCALL(FLOAT);
  625. DISASSEMBLE_PTRCALL(STRING);
  626. DISASSEMBLE_PTRCALL(VECTOR2);
  627. DISASSEMBLE_PTRCALL(VECTOR2I);
  628. DISASSEMBLE_PTRCALL(RECT2);
  629. DISASSEMBLE_PTRCALL(RECT2I);
  630. DISASSEMBLE_PTRCALL(VECTOR3);
  631. DISASSEMBLE_PTRCALL(VECTOR3I);
  632. DISASSEMBLE_PTRCALL(TRANSFORM2D);
  633. DISASSEMBLE_PTRCALL(VECTOR4);
  634. DISASSEMBLE_PTRCALL(VECTOR4I);
  635. DISASSEMBLE_PTRCALL(PLANE);
  636. DISASSEMBLE_PTRCALL(AABB);
  637. DISASSEMBLE_PTRCALL(BASIS);
  638. DISASSEMBLE_PTRCALL(TRANSFORM3D);
  639. DISASSEMBLE_PTRCALL(PROJECTION);
  640. DISASSEMBLE_PTRCALL(COLOR);
  641. DISASSEMBLE_PTRCALL(STRING_NAME);
  642. DISASSEMBLE_PTRCALL(NODE_PATH);
  643. DISASSEMBLE_PTRCALL(RID);
  644. DISASSEMBLE_PTRCALL(QUATERNION);
  645. DISASSEMBLE_PTRCALL(OBJECT);
  646. DISASSEMBLE_PTRCALL(CALLABLE);
  647. DISASSEMBLE_PTRCALL(SIGNAL);
  648. DISASSEMBLE_PTRCALL(DICTIONARY);
  649. DISASSEMBLE_PTRCALL(ARRAY);
  650. DISASSEMBLE_PTRCALL(PACKED_BYTE_ARRAY);
  651. DISASSEMBLE_PTRCALL(PACKED_INT32_ARRAY);
  652. DISASSEMBLE_PTRCALL(PACKED_INT64_ARRAY);
  653. DISASSEMBLE_PTRCALL(PACKED_FLOAT32_ARRAY);
  654. DISASSEMBLE_PTRCALL(PACKED_FLOAT64_ARRAY);
  655. DISASSEMBLE_PTRCALL(PACKED_STRING_ARRAY);
  656. DISASSEMBLE_PTRCALL(PACKED_VECTOR2_ARRAY);
  657. DISASSEMBLE_PTRCALL(PACKED_VECTOR3_ARRAY);
  658. DISASSEMBLE_PTRCALL(PACKED_COLOR_ARRAY);
  659. case OPCODE_CALL_BUILTIN_TYPE_VALIDATED: {
  660. int instr_var_args = _code_ptr[++ip];
  661. int argc = _code_ptr[ip + 1 + instr_var_args];
  662. text += "call-builtin-method validated ";
  663. text += DADDR(2 + argc) + " = ";
  664. text += DADDR(1) + ".";
  665. text += builtin_methods_names[_code_ptr[ip + 4 + argc]];
  666. text += "(";
  667. for (int i = 0; i < argc; i++) {
  668. if (i > 0) {
  669. text += ", ";
  670. }
  671. text += DADDR(1 + i);
  672. }
  673. text += ")";
  674. incr = 5 + argc;
  675. } break;
  676. case OPCODE_CALL_UTILITY: {
  677. int instr_var_args = _code_ptr[++ip];
  678. text += "call-utility ";
  679. int argc = _code_ptr[ip + 1 + instr_var_args];
  680. text += DADDR(1 + argc) + " = ";
  681. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  682. text += "(";
  683. for (int i = 0; i < argc; i++) {
  684. if (i > 0) {
  685. text += ", ";
  686. }
  687. text += DADDR(1 + i);
  688. }
  689. text += ")";
  690. incr = 4 + argc;
  691. } break;
  692. case OPCODE_CALL_UTILITY_VALIDATED: {
  693. int instr_var_args = _code_ptr[++ip];
  694. text += "call-utility validated ";
  695. int argc = _code_ptr[ip + 1 + instr_var_args];
  696. text += DADDR(1 + argc) + " = ";
  697. text += utilities_names[_code_ptr[ip + 3 + argc]];
  698. text += "(";
  699. for (int i = 0; i < argc; i++) {
  700. if (i > 0) {
  701. text += ", ";
  702. }
  703. text += DADDR(1 + i);
  704. }
  705. text += ")";
  706. incr = 4 + argc;
  707. } break;
  708. case OPCODE_CALL_GDSCRIPT_UTILITY: {
  709. int instr_var_args = _code_ptr[++ip];
  710. text += "call-gdscript-utility ";
  711. int argc = _code_ptr[ip + 1 + instr_var_args];
  712. text += DADDR(1 + argc) + " = ";
  713. text += gds_utilities_names[_code_ptr[ip + 3 + argc]];
  714. text += "(";
  715. for (int i = 0; i < argc; i++) {
  716. if (i > 0) {
  717. text += ", ";
  718. }
  719. text += DADDR(1 + i);
  720. }
  721. text += ")";
  722. incr = 4 + argc;
  723. } break;
  724. case OPCODE_CALL_SELF_BASE: {
  725. int instr_var_args = _code_ptr[++ip];
  726. text += "call-self-base ";
  727. int argc = _code_ptr[ip + 1 + instr_var_args];
  728. text += DADDR(2 + argc) + " = ";
  729. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  730. text += "(";
  731. for (int i = 0; i < argc; i++) {
  732. if (i > 0) {
  733. text += ", ";
  734. }
  735. text += DADDR(1 + i);
  736. }
  737. text += ")";
  738. incr = 4 + argc;
  739. } break;
  740. case OPCODE_AWAIT: {
  741. text += "await ";
  742. text += DADDR(1);
  743. incr = 2;
  744. } break;
  745. case OPCODE_AWAIT_RESUME: {
  746. text += "await resume ";
  747. text += DADDR(1);
  748. incr = 2;
  749. } break;
  750. case OPCODE_CREATE_LAMBDA: {
  751. int instr_var_args = _code_ptr[++ip];
  752. int captures_count = _code_ptr[ip + 1 + instr_var_args];
  753. GDScriptFunction *lambda = _lambdas_ptr[_code_ptr[ip + 2 + instr_var_args]];
  754. text += DADDR(1 + captures_count);
  755. text += "create lambda from ";
  756. text += lambda->name.operator String();
  757. text += "function, captures (";
  758. for (int i = 0; i < captures_count; i++) {
  759. if (i > 0) {
  760. text += ", ";
  761. }
  762. text += DADDR(1 + i);
  763. }
  764. text += ")";
  765. incr = 4 + captures_count;
  766. } break;
  767. case OPCODE_CREATE_SELF_LAMBDA: {
  768. int instr_var_args = _code_ptr[++ip];
  769. int captures_count = _code_ptr[ip + 1 + instr_var_args];
  770. GDScriptFunction *lambda = _lambdas_ptr[_code_ptr[ip + 2 + instr_var_args]];
  771. text += DADDR(1 + captures_count);
  772. text += "create self lambda from ";
  773. text += lambda->name.operator String();
  774. text += "function, captures (";
  775. for (int i = 0; i < captures_count; i++) {
  776. if (i > 0) {
  777. text += ", ";
  778. }
  779. text += DADDR(1 + i);
  780. }
  781. text += ")";
  782. incr = 4 + captures_count;
  783. } break;
  784. case OPCODE_JUMP: {
  785. text += "jump ";
  786. text += itos(_code_ptr[ip + 1]);
  787. incr = 2;
  788. } break;
  789. case OPCODE_JUMP_IF: {
  790. text += "jump-if ";
  791. text += DADDR(1);
  792. text += " to ";
  793. text += itos(_code_ptr[ip + 2]);
  794. incr = 3;
  795. } break;
  796. case OPCODE_JUMP_IF_NOT: {
  797. text += "jump-if-not ";
  798. text += DADDR(1);
  799. text += " to ";
  800. text += itos(_code_ptr[ip + 2]);
  801. incr = 3;
  802. } break;
  803. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  804. text += "jump-to-default-argument ";
  805. incr = 1;
  806. } break;
  807. case OPCODE_JUMP_IF_SHARED: {
  808. text += "jump-if-shared ";
  809. text += DADDR(1);
  810. text += " to ";
  811. text += itos(_code_ptr[ip + 2]);
  812. incr = 3;
  813. } break;
  814. case OPCODE_RETURN: {
  815. text += "return ";
  816. text += DADDR(1);
  817. incr = 2;
  818. } break;
  819. case OPCODE_RETURN_TYPED_BUILTIN: {
  820. text += "return typed builtin (";
  821. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 2]);
  822. text += ") ";
  823. text += DADDR(1);
  824. incr += 3;
  825. } break;
  826. case OPCODE_RETURN_TYPED_ARRAY: {
  827. text += "return typed array ";
  828. text += DADDR(1);
  829. incr += 5;
  830. } break;
  831. case OPCODE_RETURN_TYPED_NATIVE: {
  832. text += "return typed native (";
  833. text += DADDR(2);
  834. text += ") ";
  835. text += DADDR(1);
  836. incr += 3;
  837. } break;
  838. case OPCODE_RETURN_TYPED_SCRIPT: {
  839. Ref<Script> script = get_constant(_code_ptr[ip + 2] & ADDR_MASK);
  840. text += "return typed script (";
  841. text += GDScript::debug_get_script_name(script);
  842. text += ") ";
  843. text += DADDR(1);
  844. incr += 3;
  845. } break;
  846. #define DISASSEMBLE_ITERATE(m_type) \
  847. case OPCODE_ITERATE_##m_type: { \
  848. text += "for-loop (typed "; \
  849. text += #m_type; \
  850. text += ") "; \
  851. text += DADDR(3); \
  852. text += " in "; \
  853. text += DADDR(2); \
  854. text += " counter "; \
  855. text += DADDR(1); \
  856. text += " end "; \
  857. text += itos(_code_ptr[ip + 4]); \
  858. incr += 5; \
  859. } break
  860. #define DISASSEMBLE_ITERATE_BEGIN(m_type) \
  861. case OPCODE_ITERATE_BEGIN_##m_type: { \
  862. text += "for-init (typed "; \
  863. text += #m_type; \
  864. text += ") "; \
  865. text += DADDR(3); \
  866. text += " in "; \
  867. text += DADDR(2); \
  868. text += " counter "; \
  869. text += DADDR(1); \
  870. text += " end "; \
  871. text += itos(_code_ptr[ip + 4]); \
  872. incr += 5; \
  873. } break
  874. #define DISASSEMBLE_ITERATE_TYPES(m_macro) \
  875. m_macro(INT); \
  876. m_macro(FLOAT); \
  877. m_macro(VECTOR2); \
  878. m_macro(VECTOR2I); \
  879. m_macro(VECTOR3); \
  880. m_macro(VECTOR3I); \
  881. m_macro(STRING); \
  882. m_macro(DICTIONARY); \
  883. m_macro(ARRAY); \
  884. m_macro(PACKED_BYTE_ARRAY); \
  885. m_macro(PACKED_INT32_ARRAY); \
  886. m_macro(PACKED_INT64_ARRAY); \
  887. m_macro(PACKED_FLOAT32_ARRAY); \
  888. m_macro(PACKED_FLOAT64_ARRAY); \
  889. m_macro(PACKED_STRING_ARRAY); \
  890. m_macro(PACKED_VECTOR2_ARRAY); \
  891. m_macro(PACKED_VECTOR3_ARRAY); \
  892. m_macro(PACKED_COLOR_ARRAY); \
  893. m_macro(OBJECT)
  894. case OPCODE_ITERATE_BEGIN: {
  895. text += "for-init ";
  896. text += DADDR(3);
  897. text += " in ";
  898. text += DADDR(2);
  899. text += " counter ";
  900. text += DADDR(1);
  901. text += " end ";
  902. text += itos(_code_ptr[ip + 4]);
  903. incr += 5;
  904. } break;
  905. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE_BEGIN);
  906. case OPCODE_ITERATE: {
  907. text += "for-loop ";
  908. text += DADDR(2);
  909. text += " in ";
  910. text += DADDR(2);
  911. text += " counter ";
  912. text += DADDR(1);
  913. text += " end ";
  914. text += itos(_code_ptr[ip + 4]);
  915. incr += 5;
  916. } break;
  917. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE);
  918. case OPCODE_STORE_GLOBAL: {
  919. text += "store global ";
  920. text += DADDR(1);
  921. text += " = ";
  922. text += String::num_int64(_code_ptr[ip + 2]);
  923. incr += 3;
  924. } break;
  925. case OPCODE_STORE_NAMED_GLOBAL: {
  926. text += "store named global ";
  927. text += DADDR(1);
  928. text += " = ";
  929. text += String(_global_names_ptr[_code_ptr[ip + 2]]);
  930. incr += 3;
  931. } break;
  932. case OPCODE_LINE: {
  933. int line = _code_ptr[ip + 1] - 1;
  934. if (line >= 0 && line < p_code_lines.size()) {
  935. text += "line ";
  936. text += itos(line + 1);
  937. text += ": ";
  938. text += p_code_lines[line];
  939. } else {
  940. text += "";
  941. }
  942. incr += 2;
  943. } break;
  944. #define DISASSEMBLE_TYPE_ADJUST(m_v_type) \
  945. case OPCODE_TYPE_ADJUST_##m_v_type: { \
  946. text += "type adjust ("; \
  947. text += #m_v_type; \
  948. text += ") "; \
  949. text += DADDR(1); \
  950. incr += 2; \
  951. } break
  952. DISASSEMBLE_TYPE_ADJUST(BOOL);
  953. DISASSEMBLE_TYPE_ADJUST(INT);
  954. DISASSEMBLE_TYPE_ADJUST(FLOAT);
  955. DISASSEMBLE_TYPE_ADJUST(STRING);
  956. DISASSEMBLE_TYPE_ADJUST(VECTOR2);
  957. DISASSEMBLE_TYPE_ADJUST(VECTOR2I);
  958. DISASSEMBLE_TYPE_ADJUST(RECT2);
  959. DISASSEMBLE_TYPE_ADJUST(RECT2I);
  960. DISASSEMBLE_TYPE_ADJUST(VECTOR3);
  961. DISASSEMBLE_TYPE_ADJUST(VECTOR3I);
  962. DISASSEMBLE_TYPE_ADJUST(TRANSFORM2D);
  963. DISASSEMBLE_TYPE_ADJUST(VECTOR4);
  964. DISASSEMBLE_TYPE_ADJUST(VECTOR4I);
  965. DISASSEMBLE_TYPE_ADJUST(PLANE);
  966. DISASSEMBLE_TYPE_ADJUST(QUATERNION);
  967. DISASSEMBLE_TYPE_ADJUST(AABB);
  968. DISASSEMBLE_TYPE_ADJUST(BASIS);
  969. DISASSEMBLE_TYPE_ADJUST(TRANSFORM3D);
  970. DISASSEMBLE_TYPE_ADJUST(PROJECTION);
  971. DISASSEMBLE_TYPE_ADJUST(COLOR);
  972. DISASSEMBLE_TYPE_ADJUST(STRING_NAME);
  973. DISASSEMBLE_TYPE_ADJUST(NODE_PATH);
  974. DISASSEMBLE_TYPE_ADJUST(RID);
  975. DISASSEMBLE_TYPE_ADJUST(OBJECT);
  976. DISASSEMBLE_TYPE_ADJUST(CALLABLE);
  977. DISASSEMBLE_TYPE_ADJUST(SIGNAL);
  978. DISASSEMBLE_TYPE_ADJUST(DICTIONARY);
  979. DISASSEMBLE_TYPE_ADJUST(ARRAY);
  980. DISASSEMBLE_TYPE_ADJUST(PACKED_BYTE_ARRAY);
  981. DISASSEMBLE_TYPE_ADJUST(PACKED_INT32_ARRAY);
  982. DISASSEMBLE_TYPE_ADJUST(PACKED_INT64_ARRAY);
  983. DISASSEMBLE_TYPE_ADJUST(PACKED_FLOAT32_ARRAY);
  984. DISASSEMBLE_TYPE_ADJUST(PACKED_FLOAT64_ARRAY);
  985. DISASSEMBLE_TYPE_ADJUST(PACKED_STRING_ARRAY);
  986. DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR2_ARRAY);
  987. DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR3_ARRAY);
  988. DISASSEMBLE_TYPE_ADJUST(PACKED_COLOR_ARRAY);
  989. case OPCODE_ASSERT: {
  990. text += "assert (";
  991. text += DADDR(1);
  992. text += ", ";
  993. text += DADDR(2);
  994. text += ")";
  995. incr += 3;
  996. } break;
  997. case OPCODE_BREAKPOINT: {
  998. text += "breakpoint";
  999. incr += 1;
  1000. } break;
  1001. case OPCODE_END: {
  1002. text += "== END ==";
  1003. incr += 1;
  1004. } break;
  1005. }
  1006. ip += incr;
  1007. if (text.get_string_length() > 0) {
  1008. print_line(text.as_string());
  1009. }
  1010. }
  1011. }
  1012. #endif // DEBUG_ENABLED