gdscript_disassembler.cpp 29 KB

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