gdscript_disassembler.cpp 30 KB

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