2
0

gdscript_disassembler.cpp 31 KB

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