gdscript_disassembler.cpp 31 KB

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