gdscript_disassembler.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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_NULL: {
  323. text += "assign ";
  324. text += DADDR(1);
  325. text += " = null";
  326. incr += 2;
  327. } break;
  328. case OPCODE_ASSIGN_TRUE: {
  329. text += "assign ";
  330. text += DADDR(1);
  331. text += " = true";
  332. incr += 2;
  333. } break;
  334. case OPCODE_ASSIGN_FALSE: {
  335. text += "assign ";
  336. text += DADDR(1);
  337. text += " = false";
  338. incr += 2;
  339. } break;
  340. case OPCODE_ASSIGN_TYPED_BUILTIN: {
  341. text += "assign typed builtin (";
  342. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 3]);
  343. text += ") ";
  344. text += DADDR(1);
  345. text += " = ";
  346. text += DADDR(2);
  347. incr += 4;
  348. } break;
  349. case OPCODE_ASSIGN_TYPED_ARRAY: {
  350. text += "assign typed array ";
  351. text += DADDR(1);
  352. text += " = ";
  353. text += DADDR(2);
  354. incr += 6;
  355. } break;
  356. case OPCODE_ASSIGN_TYPED_NATIVE: {
  357. text += "assign typed native (";
  358. text += DADDR(3);
  359. text += ") ";
  360. text += DADDR(1);
  361. text += " = ";
  362. text += DADDR(2);
  363. incr += 4;
  364. } break;
  365. case OPCODE_ASSIGN_TYPED_SCRIPT: {
  366. Ref<Script> script = get_constant(_code_ptr[ip + 3] & ADDR_MASK);
  367. text += "assign typed script (";
  368. text += GDScript::debug_get_script_name(script);
  369. text += ") ";
  370. text += DADDR(1);
  371. text += " = ";
  372. text += DADDR(2);
  373. incr += 4;
  374. } break;
  375. case OPCODE_CAST_TO_BUILTIN: {
  376. text += "cast builtin ";
  377. text += DADDR(2);
  378. text += " = ";
  379. text += DADDR(1);
  380. text += " as ";
  381. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 1]));
  382. incr += 4;
  383. } break;
  384. case OPCODE_CAST_TO_NATIVE: {
  385. text += "cast native ";
  386. text += DADDR(2);
  387. text += " = ";
  388. text += DADDR(1);
  389. text += " as ";
  390. text += DADDR(3);
  391. incr += 4;
  392. } break;
  393. case OPCODE_CAST_TO_SCRIPT: {
  394. text += "cast ";
  395. text += DADDR(2);
  396. text += " = ";
  397. text += DADDR(1);
  398. text += " as ";
  399. text += DADDR(3);
  400. incr += 4;
  401. } break;
  402. case OPCODE_CONSTRUCT: {
  403. int instr_var_args = _code_ptr[++ip];
  404. Variant::Type t = Variant::Type(_code_ptr[ip + 3 + instr_var_args]);
  405. int argc = _code_ptr[ip + 1 + instr_var_args];
  406. text += "construct ";
  407. text += DADDR(1 + argc);
  408. text += " = ";
  409. text += Variant::get_type_name(t) + "(";
  410. for (int i = 0; i < argc; i++) {
  411. if (i > 0) {
  412. text += ", ";
  413. }
  414. text += DADDR(i + 1);
  415. }
  416. text += ")";
  417. incr = 3 + instr_var_args;
  418. } break;
  419. case OPCODE_CONSTRUCT_VALIDATED: {
  420. int instr_var_args = _code_ptr[++ip];
  421. int argc = _code_ptr[ip + 1 + instr_var_args];
  422. text += "construct validated ";
  423. text += DADDR(1 + argc);
  424. text += " = ";
  425. text += constructors_names[_code_ptr[ip + 3 + argc]];
  426. text += "(";
  427. for (int i = 0; i < argc; i++) {
  428. if (i > 0) {
  429. text += ", ";
  430. }
  431. text += DADDR(i + 1);
  432. }
  433. text += ")";
  434. incr = 3 + instr_var_args;
  435. } break;
  436. case OPCODE_CONSTRUCT_ARRAY: {
  437. int instr_var_args = _code_ptr[++ip];
  438. int argc = _code_ptr[ip + 1 + instr_var_args];
  439. text += " make_array ";
  440. text += DADDR(1 + argc);
  441. text += " = [";
  442. for (int i = 0; i < argc; i++) {
  443. if (i > 0) {
  444. text += ", ";
  445. }
  446. text += DADDR(1 + i);
  447. }
  448. text += "]";
  449. incr += 3 + argc;
  450. } break;
  451. case OPCODE_CONSTRUCT_TYPED_ARRAY: {
  452. int instr_var_args = _code_ptr[++ip];
  453. int argc = _code_ptr[ip + 1 + instr_var_args];
  454. Ref<Script> script_type = get_constant(_code_ptr[ip + argc + 2] & ADDR_MASK);
  455. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + argc + 4];
  456. StringName native_type = get_global_name(_code_ptr[ip + argc + 5]);
  457. String type_name;
  458. if (script_type.is_valid() && script_type->is_valid()) {
  459. type_name = "script(" + GDScript::debug_get_script_name(script_type) + ")";
  460. } else if (native_type != StringName()) {
  461. type_name = native_type;
  462. } else {
  463. type_name = Variant::get_type_name(builtin_type);
  464. }
  465. text += " make_typed_array (";
  466. text += type_name;
  467. text += ") ";
  468. text += DADDR(1 + argc);
  469. text += " = [";
  470. for (int i = 0; i < argc; i++) {
  471. if (i > 0) {
  472. text += ", ";
  473. }
  474. text += DADDR(1 + i);
  475. }
  476. text += "]";
  477. incr += 6 + argc;
  478. } break;
  479. case OPCODE_CONSTRUCT_DICTIONARY: {
  480. int instr_var_args = _code_ptr[++ip];
  481. int argc = _code_ptr[ip + 1 + instr_var_args];
  482. text += "make_dict ";
  483. text += DADDR(1 + argc * 2);
  484. text += " = {";
  485. for (int i = 0; i < argc; i++) {
  486. if (i > 0) {
  487. text += ", ";
  488. }
  489. text += DADDR(1 + i * 2 + 0);
  490. text += ": ";
  491. text += DADDR(1 + i * 2 + 1);
  492. }
  493. text += "}";
  494. incr += 3 + argc * 2;
  495. } break;
  496. case OPCODE_CALL:
  497. case OPCODE_CALL_RETURN:
  498. case OPCODE_CALL_ASYNC: {
  499. bool ret = (_code_ptr[ip]) == OPCODE_CALL_RETURN;
  500. bool async = (_code_ptr[ip]) == OPCODE_CALL_ASYNC;
  501. int instr_var_args = _code_ptr[++ip];
  502. if (ret) {
  503. text += "call-ret ";
  504. } else if (async) {
  505. text += "call-async ";
  506. } else {
  507. text += "call ";
  508. }
  509. int argc = _code_ptr[ip + 1 + instr_var_args];
  510. if (ret || async) {
  511. text += DADDR(2 + argc) + " = ";
  512. }
  513. text += DADDR(1 + argc) + ".";
  514. text += String(_global_names_ptr[_code_ptr[ip + 2 + instr_var_args]]);
  515. text += "(";
  516. for (int i = 0; i < argc; i++) {
  517. if (i > 0) {
  518. text += ", ";
  519. }
  520. text += DADDR(1 + i);
  521. }
  522. text += ")";
  523. incr = 5 + argc;
  524. } break;
  525. case OPCODE_CALL_METHOD_BIND:
  526. case OPCODE_CALL_METHOD_BIND_RET: {
  527. bool ret = (_code_ptr[ip]) == OPCODE_CALL_METHOD_BIND_RET;
  528. int instr_var_args = _code_ptr[++ip];
  529. if (ret) {
  530. text += "call-method_bind-ret ";
  531. } else {
  532. text += "call-method_bind ";
  533. }
  534. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  535. int argc = _code_ptr[ip + 1 + instr_var_args];
  536. if (ret) {
  537. text += DADDR(2 + argc) + " = ";
  538. }
  539. text += DADDR(1 + argc) + ".";
  540. text += method->get_name();
  541. text += "(";
  542. for (int i = 0; i < argc; i++) {
  543. if (i > 0) {
  544. text += ", ";
  545. }
  546. text += DADDR(1 + i);
  547. }
  548. text += ")";
  549. incr = 5 + argc;
  550. } break;
  551. case OPCODE_CALL_BUILTIN_STATIC: {
  552. int instr_var_args = _code_ptr[++ip];
  553. Variant::Type type = (Variant::Type)_code_ptr[ip + 1 + instr_var_args];
  554. int argc = _code_ptr[ip + 3 + instr_var_args];
  555. text += "call built-in method static ";
  556. text += DADDR(1 + argc);
  557. text += " = ";
  558. text += Variant::get_type_name(type);
  559. text += ".";
  560. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]].operator String();
  561. text += "(";
  562. for (int i = 0; i < argc; i++) {
  563. if (i > 0) {
  564. text += ", ";
  565. }
  566. text += DADDR(1 + i);
  567. }
  568. text += ")";
  569. incr += 5 + argc;
  570. } break;
  571. case OPCODE_CALL_NATIVE_STATIC: {
  572. int instr_var_args = _code_ptr[++ip];
  573. MethodBind *method = _methods_ptr[_code_ptr[ip + 1 + instr_var_args]];
  574. int argc = _code_ptr[ip + 2 + instr_var_args];
  575. text += "call native method static ";
  576. text += DADDR(1 + argc);
  577. text += " = ";
  578. text += method->get_instance_class();
  579. text += ".";
  580. text += method->get_name();
  581. text += "(";
  582. for (int i = 0; i < argc; i++) {
  583. if (i > 0) {
  584. text += ", ";
  585. }
  586. text += DADDR(1 + i);
  587. }
  588. text += ")";
  589. incr += 4 + argc;
  590. } break;
  591. case OPCODE_CALL_METHOD_BIND_VALIDATED_RETURN: {
  592. int instr_var_args = _code_ptr[++ip];
  593. text += "call method-bind validated (return) ";
  594. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  595. int argc = _code_ptr[ip + 1 + instr_var_args];
  596. text += DADDR(2 + argc) + " = ";
  597. text += DADDR(1 + argc) + ".";
  598. text += method->get_name();
  599. text += "(";
  600. for (int i = 0; i < argc; i++) {
  601. if (i > 0)
  602. text += ", ";
  603. text += DADDR(1 + i);
  604. }
  605. text += ")";
  606. incr = 5 + argc;
  607. } break;
  608. case OPCODE_CALL_METHOD_BIND_VALIDATED_NO_RETURN: {
  609. int instr_var_args = _code_ptr[++ip];
  610. text += "call method-bind validated (no return) ";
  611. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  612. int argc = _code_ptr[ip + 1 + instr_var_args];
  613. text += DADDR(1 + argc) + ".";
  614. text += method->get_name();
  615. text += "(";
  616. for (int i = 0; i < argc; i++) {
  617. if (i > 0) {
  618. text += ", ";
  619. }
  620. text += DADDR(1 + i);
  621. }
  622. text += ")";
  623. incr = 5 + argc;
  624. } break;
  625. case OPCODE_CALL_BUILTIN_TYPE_VALIDATED: {
  626. int instr_var_args = _code_ptr[++ip];
  627. int argc = _code_ptr[ip + 1 + instr_var_args];
  628. text += "call-builtin-method validated ";
  629. text += DADDR(2 + argc) + " = ";
  630. text += DADDR(1) + ".";
  631. text += builtin_methods_names[_code_ptr[ip + 4 + argc]];
  632. text += "(";
  633. for (int i = 0; i < argc; i++) {
  634. if (i > 0) {
  635. text += ", ";
  636. }
  637. text += DADDR(1 + i);
  638. }
  639. text += ")";
  640. incr = 5 + argc;
  641. } break;
  642. case OPCODE_CALL_UTILITY: {
  643. int instr_var_args = _code_ptr[++ip];
  644. text += "call-utility ";
  645. int argc = _code_ptr[ip + 1 + instr_var_args];
  646. text += DADDR(1 + argc) + " = ";
  647. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  648. text += "(";
  649. for (int i = 0; i < argc; i++) {
  650. if (i > 0) {
  651. text += ", ";
  652. }
  653. text += DADDR(1 + i);
  654. }
  655. text += ")";
  656. incr = 4 + argc;
  657. } break;
  658. case OPCODE_CALL_UTILITY_VALIDATED: {
  659. int instr_var_args = _code_ptr[++ip];
  660. text += "call-utility validated ";
  661. int argc = _code_ptr[ip + 1 + instr_var_args];
  662. text += DADDR(1 + argc) + " = ";
  663. text += utilities_names[_code_ptr[ip + 3 + argc]];
  664. text += "(";
  665. for (int i = 0; i < argc; i++) {
  666. if (i > 0) {
  667. text += ", ";
  668. }
  669. text += DADDR(1 + i);
  670. }
  671. text += ")";
  672. incr = 4 + argc;
  673. } break;
  674. case OPCODE_CALL_GDSCRIPT_UTILITY: {
  675. int instr_var_args = _code_ptr[++ip];
  676. text += "call-gdscript-utility ";
  677. int argc = _code_ptr[ip + 1 + instr_var_args];
  678. text += DADDR(1 + argc) + " = ";
  679. text += gds_utilities_names[_code_ptr[ip + 3 + argc]];
  680. text += "(";
  681. for (int i = 0; i < argc; i++) {
  682. if (i > 0) {
  683. text += ", ";
  684. }
  685. text += DADDR(1 + i);
  686. }
  687. text += ")";
  688. incr = 4 + argc;
  689. } break;
  690. case OPCODE_CALL_SELF_BASE: {
  691. int instr_var_args = _code_ptr[++ip];
  692. text += "call-self-base ";
  693. int argc = _code_ptr[ip + 1 + instr_var_args];
  694. text += DADDR(2 + argc) + " = ";
  695. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  696. text += "(";
  697. for (int i = 0; i < argc; i++) {
  698. if (i > 0) {
  699. text += ", ";
  700. }
  701. text += DADDR(1 + i);
  702. }
  703. text += ")";
  704. incr = 4 + argc;
  705. } break;
  706. case OPCODE_AWAIT: {
  707. text += "await ";
  708. text += DADDR(1);
  709. incr = 2;
  710. } break;
  711. case OPCODE_AWAIT_RESUME: {
  712. text += "await resume ";
  713. text += DADDR(1);
  714. incr = 2;
  715. } break;
  716. case OPCODE_CREATE_LAMBDA: {
  717. int instr_var_args = _code_ptr[++ip];
  718. int captures_count = _code_ptr[ip + 1 + instr_var_args];
  719. GDScriptFunction *lambda = _lambdas_ptr[_code_ptr[ip + 2 + instr_var_args]];
  720. text += DADDR(1 + captures_count);
  721. text += "create lambda from ";
  722. text += lambda->name.operator String();
  723. text += "function, captures (";
  724. for (int i = 0; i < captures_count; i++) {
  725. if (i > 0) {
  726. text += ", ";
  727. }
  728. text += DADDR(1 + i);
  729. }
  730. text += ")";
  731. incr = 4 + captures_count;
  732. } break;
  733. case OPCODE_CREATE_SELF_LAMBDA: {
  734. int instr_var_args = _code_ptr[++ip];
  735. int captures_count = _code_ptr[ip + 1 + instr_var_args];
  736. GDScriptFunction *lambda = _lambdas_ptr[_code_ptr[ip + 2 + instr_var_args]];
  737. text += DADDR(1 + captures_count);
  738. text += "create self lambda from ";
  739. text += lambda->name.operator String();
  740. text += "function, captures (";
  741. for (int i = 0; i < captures_count; i++) {
  742. if (i > 0) {
  743. text += ", ";
  744. }
  745. text += DADDR(1 + i);
  746. }
  747. text += ")";
  748. incr = 4 + captures_count;
  749. } break;
  750. case OPCODE_JUMP: {
  751. text += "jump ";
  752. text += itos(_code_ptr[ip + 1]);
  753. incr = 2;
  754. } break;
  755. case OPCODE_JUMP_IF: {
  756. text += "jump-if ";
  757. text += DADDR(1);
  758. text += " to ";
  759. text += itos(_code_ptr[ip + 2]);
  760. incr = 3;
  761. } break;
  762. case OPCODE_JUMP_IF_NOT: {
  763. text += "jump-if-not ";
  764. text += DADDR(1);
  765. text += " to ";
  766. text += itos(_code_ptr[ip + 2]);
  767. incr = 3;
  768. } break;
  769. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  770. text += "jump-to-default-argument ";
  771. incr = 1;
  772. } break;
  773. case OPCODE_JUMP_IF_SHARED: {
  774. text += "jump-if-shared ";
  775. text += DADDR(1);
  776. text += " to ";
  777. text += itos(_code_ptr[ip + 2]);
  778. incr = 3;
  779. } break;
  780. case OPCODE_RETURN: {
  781. text += "return ";
  782. text += DADDR(1);
  783. incr = 2;
  784. } break;
  785. case OPCODE_RETURN_TYPED_BUILTIN: {
  786. text += "return typed builtin (";
  787. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 2]);
  788. text += ") ";
  789. text += DADDR(1);
  790. incr += 3;
  791. } break;
  792. case OPCODE_RETURN_TYPED_ARRAY: {
  793. text += "return typed array ";
  794. text += DADDR(1);
  795. incr += 5;
  796. } break;
  797. case OPCODE_RETURN_TYPED_NATIVE: {
  798. text += "return typed native (";
  799. text += DADDR(2);
  800. text += ") ";
  801. text += DADDR(1);
  802. incr += 3;
  803. } break;
  804. case OPCODE_RETURN_TYPED_SCRIPT: {
  805. Ref<Script> script = get_constant(_code_ptr[ip + 2] & ADDR_MASK);
  806. text += "return typed script (";
  807. text += GDScript::debug_get_script_name(script);
  808. text += ") ";
  809. text += DADDR(1);
  810. incr += 3;
  811. } break;
  812. #define DISASSEMBLE_ITERATE(m_type) \
  813. case OPCODE_ITERATE_##m_type: { \
  814. text += "for-loop (typed "; \
  815. text += #m_type; \
  816. text += ") "; \
  817. text += DADDR(3); \
  818. text += " in "; \
  819. text += DADDR(2); \
  820. text += " counter "; \
  821. text += DADDR(1); \
  822. text += " end "; \
  823. text += itos(_code_ptr[ip + 4]); \
  824. incr += 5; \
  825. } break
  826. #define DISASSEMBLE_ITERATE_BEGIN(m_type) \
  827. case OPCODE_ITERATE_BEGIN_##m_type: { \
  828. text += "for-init (typed "; \
  829. text += #m_type; \
  830. text += ") "; \
  831. text += DADDR(3); \
  832. text += " in "; \
  833. text += DADDR(2); \
  834. text += " counter "; \
  835. text += DADDR(1); \
  836. text += " end "; \
  837. text += itos(_code_ptr[ip + 4]); \
  838. incr += 5; \
  839. } break
  840. #define DISASSEMBLE_ITERATE_TYPES(m_macro) \
  841. m_macro(INT); \
  842. m_macro(FLOAT); \
  843. m_macro(VECTOR2); \
  844. m_macro(VECTOR2I); \
  845. m_macro(VECTOR3); \
  846. m_macro(VECTOR3I); \
  847. m_macro(STRING); \
  848. m_macro(DICTIONARY); \
  849. m_macro(ARRAY); \
  850. m_macro(PACKED_BYTE_ARRAY); \
  851. m_macro(PACKED_INT32_ARRAY); \
  852. m_macro(PACKED_INT64_ARRAY); \
  853. m_macro(PACKED_FLOAT32_ARRAY); \
  854. m_macro(PACKED_FLOAT64_ARRAY); \
  855. m_macro(PACKED_STRING_ARRAY); \
  856. m_macro(PACKED_VECTOR2_ARRAY); \
  857. m_macro(PACKED_VECTOR3_ARRAY); \
  858. m_macro(PACKED_COLOR_ARRAY); \
  859. m_macro(OBJECT)
  860. case OPCODE_ITERATE_BEGIN: {
  861. text += "for-init ";
  862. text += DADDR(3);
  863. text += " in ";
  864. text += DADDR(2);
  865. text += " counter ";
  866. text += DADDR(1);
  867. text += " end ";
  868. text += itos(_code_ptr[ip + 4]);
  869. incr += 5;
  870. } break;
  871. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE_BEGIN);
  872. case OPCODE_ITERATE: {
  873. text += "for-loop ";
  874. text += DADDR(2);
  875. text += " in ";
  876. text += DADDR(2);
  877. text += " counter ";
  878. text += DADDR(1);
  879. text += " end ";
  880. text += itos(_code_ptr[ip + 4]);
  881. incr += 5;
  882. } break;
  883. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE);
  884. case OPCODE_STORE_GLOBAL: {
  885. text += "store global ";
  886. text += DADDR(1);
  887. text += " = ";
  888. text += String::num_int64(_code_ptr[ip + 2]);
  889. incr += 3;
  890. } break;
  891. case OPCODE_STORE_NAMED_GLOBAL: {
  892. text += "store named global ";
  893. text += DADDR(1);
  894. text += " = ";
  895. text += String(_global_names_ptr[_code_ptr[ip + 2]]);
  896. incr += 3;
  897. } break;
  898. case OPCODE_LINE: {
  899. int line = _code_ptr[ip + 1] - 1;
  900. if (line >= 0 && line < p_code_lines.size()) {
  901. text += "line ";
  902. text += itos(line + 1);
  903. text += ": ";
  904. text += p_code_lines[line];
  905. } else {
  906. text += "";
  907. }
  908. incr += 2;
  909. } break;
  910. #define DISASSEMBLE_TYPE_ADJUST(m_v_type) \
  911. case OPCODE_TYPE_ADJUST_##m_v_type: { \
  912. text += "type adjust ("; \
  913. text += #m_v_type; \
  914. text += ") "; \
  915. text += DADDR(1); \
  916. incr += 2; \
  917. } break
  918. DISASSEMBLE_TYPE_ADJUST(BOOL);
  919. DISASSEMBLE_TYPE_ADJUST(INT);
  920. DISASSEMBLE_TYPE_ADJUST(FLOAT);
  921. DISASSEMBLE_TYPE_ADJUST(STRING);
  922. DISASSEMBLE_TYPE_ADJUST(VECTOR2);
  923. DISASSEMBLE_TYPE_ADJUST(VECTOR2I);
  924. DISASSEMBLE_TYPE_ADJUST(RECT2);
  925. DISASSEMBLE_TYPE_ADJUST(RECT2I);
  926. DISASSEMBLE_TYPE_ADJUST(VECTOR3);
  927. DISASSEMBLE_TYPE_ADJUST(VECTOR3I);
  928. DISASSEMBLE_TYPE_ADJUST(TRANSFORM2D);
  929. DISASSEMBLE_TYPE_ADJUST(VECTOR4);
  930. DISASSEMBLE_TYPE_ADJUST(VECTOR4I);
  931. DISASSEMBLE_TYPE_ADJUST(PLANE);
  932. DISASSEMBLE_TYPE_ADJUST(QUATERNION);
  933. DISASSEMBLE_TYPE_ADJUST(AABB);
  934. DISASSEMBLE_TYPE_ADJUST(BASIS);
  935. DISASSEMBLE_TYPE_ADJUST(TRANSFORM3D);
  936. DISASSEMBLE_TYPE_ADJUST(PROJECTION);
  937. DISASSEMBLE_TYPE_ADJUST(COLOR);
  938. DISASSEMBLE_TYPE_ADJUST(STRING_NAME);
  939. DISASSEMBLE_TYPE_ADJUST(NODE_PATH);
  940. DISASSEMBLE_TYPE_ADJUST(RID);
  941. DISASSEMBLE_TYPE_ADJUST(OBJECT);
  942. DISASSEMBLE_TYPE_ADJUST(CALLABLE);
  943. DISASSEMBLE_TYPE_ADJUST(SIGNAL);
  944. DISASSEMBLE_TYPE_ADJUST(DICTIONARY);
  945. DISASSEMBLE_TYPE_ADJUST(ARRAY);
  946. DISASSEMBLE_TYPE_ADJUST(PACKED_BYTE_ARRAY);
  947. DISASSEMBLE_TYPE_ADJUST(PACKED_INT32_ARRAY);
  948. DISASSEMBLE_TYPE_ADJUST(PACKED_INT64_ARRAY);
  949. DISASSEMBLE_TYPE_ADJUST(PACKED_FLOAT32_ARRAY);
  950. DISASSEMBLE_TYPE_ADJUST(PACKED_FLOAT64_ARRAY);
  951. DISASSEMBLE_TYPE_ADJUST(PACKED_STRING_ARRAY);
  952. DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR2_ARRAY);
  953. DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR3_ARRAY);
  954. DISASSEMBLE_TYPE_ADJUST(PACKED_COLOR_ARRAY);
  955. case OPCODE_ASSERT: {
  956. text += "assert (";
  957. text += DADDR(1);
  958. text += ", ";
  959. text += DADDR(2);
  960. text += ")";
  961. incr += 3;
  962. } break;
  963. case OPCODE_BREAKPOINT: {
  964. text += "breakpoint";
  965. incr += 1;
  966. } break;
  967. case OPCODE_END: {
  968. text += "== END ==";
  969. incr += 1;
  970. } break;
  971. }
  972. ip += incr;
  973. if (text.get_string_length() > 0) {
  974. print_line(text.as_string());
  975. }
  976. }
  977. }
  978. #endif // DEBUG_ENABLED