gdscript_disassembler.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*************************************************************************/
  2. /* gdscript_disassembler.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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_function.h"
  32. #include "core/string/string_builder.h"
  33. #include "gdscript.h"
  34. #include "gdscript_functions.h"
  35. static String _get_variant_string(const Variant &p_variant) {
  36. String txt;
  37. if (p_variant.get_type() == Variant::STRING) {
  38. txt = "\"" + String(p_variant) + "\"";
  39. } else if (p_variant.get_type() == Variant::STRING_NAME) {
  40. txt = "&\"" + String(p_variant) + "\"";
  41. } else if (p_variant.get_type() == Variant::NODE_PATH) {
  42. txt = "^\"" + String(p_variant) + "\"";
  43. } else if (p_variant.get_type() == Variant::OBJECT) {
  44. Object *obj = p_variant;
  45. if (!obj) {
  46. txt = "null";
  47. } else {
  48. GDScriptNativeClass *cls = Object::cast_to<GDScriptNativeClass>(obj);
  49. if (cls) {
  50. txt += cls->get_name();
  51. txt += " (class)";
  52. } else {
  53. txt = obj->get_class();
  54. if (obj->get_script_instance()) {
  55. txt += "(" + obj->get_script_instance()->get_script()->get_path() + ")";
  56. }
  57. }
  58. }
  59. } else {
  60. txt = p_variant;
  61. }
  62. return txt;
  63. }
  64. static String _disassemble_address(const GDScript *p_script, const GDScriptFunction &p_function, int p_address) {
  65. int addr = p_address & GDScriptFunction::ADDR_MASK;
  66. switch (p_address >> GDScriptFunction::ADDR_BITS) {
  67. case GDScriptFunction::ADDR_TYPE_SELF: {
  68. return "self";
  69. } break;
  70. case GDScriptFunction::ADDR_TYPE_CLASS: {
  71. return "class";
  72. } break;
  73. case GDScriptFunction::ADDR_TYPE_MEMBER: {
  74. return "member(" + p_script->debug_get_member_by_index(addr) + ")";
  75. } break;
  76. case GDScriptFunction::ADDR_TYPE_CLASS_CONSTANT: {
  77. return "class_const(" + p_function.get_global_name(addr) + ")";
  78. } break;
  79. case GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT: {
  80. return "const(" + _get_variant_string(p_function.get_constant(addr)) + ")";
  81. } break;
  82. case GDScriptFunction::ADDR_TYPE_STACK: {
  83. return "stack(" + itos(addr) + ")";
  84. } break;
  85. case GDScriptFunction::ADDR_TYPE_STACK_VARIABLE: {
  86. return "var_stack(" + itos(addr) + ")";
  87. } break;
  88. case GDScriptFunction::ADDR_TYPE_GLOBAL: {
  89. return "global(" + _get_variant_string(GDScriptLanguage::get_singleton()->get_global_array()[addr]) + ")";
  90. } break;
  91. case GDScriptFunction::ADDR_TYPE_NAMED_GLOBAL: {
  92. return "named_global(" + p_function.get_global_name(addr) + ")";
  93. } break;
  94. case GDScriptFunction::ADDR_TYPE_NIL: {
  95. return "nil";
  96. } break;
  97. }
  98. return "<err>";
  99. }
  100. void GDScriptFunction::disassemble(const Vector<String> &p_code_lines) const {
  101. #define DADDR(m_ip) (_disassemble_address(_script, *this, _code_ptr[ip + m_ip]))
  102. for (int ip = 0; ip < _code_size;) {
  103. StringBuilder text;
  104. int incr = 0;
  105. text += " ";
  106. text += itos(ip);
  107. text += ": ";
  108. // This makes the compiler complain if some opcode is unchecked in the switch.
  109. Opcode code = Opcode(_code_ptr[ip] & INSTR_MASK);
  110. int instr_var_args = (_code_ptr[ip] & INSTR_ARGS_MASK) >> INSTR_BITS;
  111. switch (code) {
  112. case OPCODE_OPERATOR: {
  113. int operation = _code_ptr[ip + 4];
  114. text += "operator ";
  115. text += DADDR(3);
  116. text += " = ";
  117. text += DADDR(1);
  118. text += " ";
  119. text += Variant::get_operator_name(Variant::Operator(operation));
  120. text += " ";
  121. text += DADDR(2);
  122. incr += 5;
  123. } break;
  124. case OPCODE_OPERATOR_VALIDATED: {
  125. text += "validated operator ";
  126. text += DADDR(3);
  127. text += " = ";
  128. text += DADDR(1);
  129. text += " <operator function> ";
  130. text += DADDR(2);
  131. incr += 5;
  132. } break;
  133. case OPCODE_EXTENDS_TEST: {
  134. text += "is object ";
  135. text += DADDR(3);
  136. text += " = ";
  137. text += DADDR(1);
  138. text += " is ";
  139. text += DADDR(2);
  140. incr += 4;
  141. } break;
  142. case OPCODE_IS_BUILTIN: {
  143. text += "is builtin ";
  144. text += DADDR(2);
  145. text += " = ";
  146. text += DADDR(1);
  147. text += " is ";
  148. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 3]));
  149. incr += 4;
  150. } break;
  151. case OPCODE_SET_KEYED: {
  152. text += "set keyed ";
  153. text += DADDR(1);
  154. text += "[";
  155. text += DADDR(2);
  156. text += "] = ";
  157. text += DADDR(3);
  158. incr += 4;
  159. } break;
  160. case OPCODE_SET_KEYED_VALIDATED: {
  161. text += "set keyed validated ";
  162. text += DADDR(1);
  163. text += "[";
  164. text += DADDR(2);
  165. text += "] = ";
  166. text += DADDR(3);
  167. incr += 5;
  168. } break;
  169. case OPCODE_SET_INDEXED_VALIDATED: {
  170. text += "set indexed validated ";
  171. text += DADDR(1);
  172. text += "[";
  173. text += DADDR(2);
  174. text += "] = ";
  175. text += DADDR(3);
  176. incr += 5;
  177. } break;
  178. case OPCODE_GET_KEYED: {
  179. text += "get keyed ";
  180. text += DADDR(3);
  181. text += " = ";
  182. text += DADDR(1);
  183. text += "[";
  184. text += DADDR(2);
  185. text += "]";
  186. incr += 4;
  187. } break;
  188. case OPCODE_GET_KEYED_VALIDATED: {
  189. text += "get keyed validated ";
  190. text += DADDR(3);
  191. text += " = ";
  192. text += DADDR(1);
  193. text += "[";
  194. text += DADDR(2);
  195. text += "]";
  196. incr += 5;
  197. } break;
  198. case OPCODE_GET_INDEXED_VALIDATED: {
  199. text += "get indexed validated ";
  200. text += DADDR(3);
  201. text += " = ";
  202. text += DADDR(1);
  203. text += "[";
  204. text += DADDR(2);
  205. text += "]";
  206. incr += 5;
  207. } break;
  208. case OPCODE_SET_NAMED: {
  209. text += "set_named ";
  210. text += DADDR(1);
  211. text += "[\"";
  212. text += _global_names_ptr[_code_ptr[ip + 3]];
  213. text += "\"] = ";
  214. text += DADDR(2);
  215. incr += 4;
  216. } break;
  217. case OPCODE_SET_NAMED_VALIDATED: {
  218. text += "set_named validated ";
  219. text += DADDR(1);
  220. text += "[\"";
  221. text += "<unknown name>";
  222. text += "\"] = ";
  223. text += DADDR(2);
  224. incr += 4;
  225. } break;
  226. case OPCODE_GET_NAMED: {
  227. text += "get_named ";
  228. text += DADDR(2);
  229. text += " = ";
  230. text += DADDR(1);
  231. text += "[\"";
  232. text += _global_names_ptr[_code_ptr[ip + 3]];
  233. text += "\"]";
  234. incr += 4;
  235. } break;
  236. case OPCODE_GET_NAMED_VALIDATED: {
  237. text += "get_named validated ";
  238. text += DADDR(2);
  239. text += " = ";
  240. text += DADDR(1);
  241. text += "[\"";
  242. text += "<unknown name>";
  243. text += "\"]";
  244. incr += 4;
  245. } break;
  246. case OPCODE_SET_MEMBER: {
  247. text += "set_member ";
  248. text += "[\"";
  249. text += _global_names_ptr[_code_ptr[ip + 2]];
  250. text += "\"] = ";
  251. text += DADDR(1);
  252. incr += 3;
  253. } break;
  254. case OPCODE_GET_MEMBER: {
  255. text += "get_member ";
  256. text += DADDR(1);
  257. text += " = ";
  258. text += "[\"";
  259. text += _global_names_ptr[_code_ptr[ip + 2]];
  260. text += "\"]";
  261. incr += 3;
  262. } break;
  263. case OPCODE_ASSIGN: {
  264. text += "assign ";
  265. text += DADDR(1);
  266. text += " = ";
  267. text += DADDR(2);
  268. incr += 3;
  269. } break;
  270. case OPCODE_ASSIGN_TRUE: {
  271. text += "assign ";
  272. text += DADDR(1);
  273. text += " = true";
  274. incr += 2;
  275. } break;
  276. case OPCODE_ASSIGN_FALSE: {
  277. text += "assign ";
  278. text += DADDR(1);
  279. text += " = false";
  280. incr += 2;
  281. } break;
  282. case OPCODE_ASSIGN_TYPED_BUILTIN: {
  283. text += "assign typed builtin (";
  284. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 3]);
  285. text += ") ";
  286. text += DADDR(1);
  287. text += " = ";
  288. text += DADDR(2);
  289. incr += 4;
  290. } break;
  291. case OPCODE_ASSIGN_TYPED_NATIVE: {
  292. Variant class_name = _constants_ptr[_code_ptr[ip + 3]];
  293. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(class_name.operator Object *());
  294. text += "assign typed native (";
  295. text += nc->get_name().operator String();
  296. text += ") ";
  297. text += DADDR(1);
  298. text += " = ";
  299. text += DADDR(2);
  300. incr += 4;
  301. } break;
  302. case OPCODE_ASSIGN_TYPED_SCRIPT: {
  303. Variant script = _constants_ptr[_code_ptr[ip + 3]];
  304. Script *sc = Object::cast_to<Script>(script.operator Object *());
  305. text += "assign typed script (";
  306. text += sc->get_path();
  307. text += ") ";
  308. text += DADDR(1);
  309. text += " = ";
  310. text += DADDR(2);
  311. incr += 4;
  312. } break;
  313. case OPCODE_CAST_TO_BUILTIN: {
  314. text += "cast builtin ";
  315. text += DADDR(2);
  316. text += " = ";
  317. text += DADDR(1);
  318. text += " as ";
  319. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 1]));
  320. incr += 4;
  321. } break;
  322. case OPCODE_CAST_TO_NATIVE: {
  323. Variant class_name = _constants_ptr[_code_ptr[ip + 1]];
  324. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(class_name.operator Object *());
  325. text += "cast native ";
  326. text += DADDR(2);
  327. text += " = ";
  328. text += DADDR(1);
  329. text += " as ";
  330. text += nc->get_name();
  331. incr += 4;
  332. } break;
  333. case OPCODE_CAST_TO_SCRIPT: {
  334. text += "cast ";
  335. text += DADDR(2);
  336. text += " = ";
  337. text += DADDR(1);
  338. text += " as ";
  339. text += DADDR(3);
  340. incr += 4;
  341. } break;
  342. case OPCODE_CONSTRUCT: {
  343. Variant::Type t = Variant::Type(_code_ptr[ip + 3 + instr_var_args]);
  344. int argc = _code_ptr[ip + 2 + instr_var_args];
  345. text += "construct ";
  346. text += DADDR(1 + argc);
  347. text += " = ";
  348. text += Variant::get_type_name(t) + "(";
  349. for (int i = 0; i < argc; i++) {
  350. if (i > 0)
  351. text += ", ";
  352. text += DADDR(i + 1);
  353. }
  354. text += ")";
  355. incr = 3 + instr_var_args;
  356. } break;
  357. case OPCODE_CONSTRUCT_ARRAY: {
  358. int argc = _code_ptr[ip + 1 + instr_var_args];
  359. text += " make_array ";
  360. text += DADDR(1 + argc);
  361. text += " = [";
  362. for (int i = 0; i < argc; i++) {
  363. if (i > 0)
  364. text += ", ";
  365. text += DADDR(1 + i);
  366. }
  367. text += "]";
  368. incr += 3 + argc;
  369. } break;
  370. case OPCODE_CONSTRUCT_DICTIONARY: {
  371. int argc = _code_ptr[ip + 1 + instr_var_args];
  372. text += "make_dict ";
  373. text += DADDR(1 + argc * 2);
  374. text += " = {";
  375. for (int i = 0; i < argc; i++) {
  376. if (i > 0)
  377. text += ", ";
  378. text += DADDR(1 + i * 2 + 0);
  379. text += ": ";
  380. text += DADDR(1 + i * 2 + 1);
  381. }
  382. text += "}";
  383. incr += 3 + argc * 2;
  384. } break;
  385. case OPCODE_CALL:
  386. case OPCODE_CALL_RETURN:
  387. case OPCODE_CALL_ASYNC: {
  388. bool ret = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_RETURN;
  389. bool async = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_ASYNC;
  390. if (ret) {
  391. text += "call-ret ";
  392. } else if (async) {
  393. text += "call-async ";
  394. } else {
  395. text += "call ";
  396. }
  397. int argc = _code_ptr[ip + 1 + instr_var_args];
  398. if (ret || async) {
  399. text += DADDR(2 + argc) + " = ";
  400. }
  401. text += DADDR(1 + argc) + ".";
  402. text += String(_global_names_ptr[_code_ptr[ip + 2 + instr_var_args]]);
  403. text += "(";
  404. for (int i = 0; i < argc; i++) {
  405. if (i > 0)
  406. text += ", ";
  407. text += DADDR(1 + i);
  408. }
  409. text += ")";
  410. incr = 5 + argc;
  411. } break;
  412. case OPCODE_CALL_BUILT_IN: {
  413. text += "call-built-in ";
  414. int argc = _code_ptr[ip + 1 + instr_var_args];
  415. text += DADDR(1 + argc) + " = ";
  416. text += GDScriptFunctions::get_func_name(GDScriptFunctions::Function(_code_ptr[ip + 2 + instr_var_args]));
  417. text += "(";
  418. for (int i = 0; i < argc; i++) {
  419. if (i > 0)
  420. text += ", ";
  421. text += DADDR(1 + i);
  422. }
  423. text += ")";
  424. incr = 4 + argc;
  425. } break;
  426. case OPCODE_CALL_SELF_BASE: {
  427. text += "call-self-base ";
  428. int argc = _code_ptr[ip + 1 + instr_var_args];
  429. text += DADDR(2 + argc) + " = ";
  430. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  431. text += "(";
  432. for (int i = 0; i < argc; i++) {
  433. if (i > 0)
  434. text += ", ";
  435. text += DADDR(1 + i);
  436. }
  437. text += ")";
  438. incr = 4 + argc;
  439. } break;
  440. case OPCODE_AWAIT: {
  441. text += "await ";
  442. text += DADDR(1);
  443. incr += 2;
  444. } break;
  445. case OPCODE_AWAIT_RESUME: {
  446. text += "await resume ";
  447. text += DADDR(1);
  448. incr = 2;
  449. } break;
  450. case OPCODE_JUMP: {
  451. text += "jump ";
  452. text += itos(_code_ptr[ip + 1]);
  453. incr = 2;
  454. } break;
  455. case OPCODE_JUMP_IF: {
  456. text += "jump-if ";
  457. text += DADDR(1);
  458. text += " to ";
  459. text += itos(_code_ptr[ip + 2]);
  460. incr = 3;
  461. } break;
  462. case OPCODE_JUMP_IF_NOT: {
  463. text += "jump-if-not ";
  464. text += DADDR(1);
  465. text += " to ";
  466. text += itos(_code_ptr[ip + 2]);
  467. incr = 3;
  468. } break;
  469. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  470. text += "jump-to-default-argument ";
  471. incr = 1;
  472. } break;
  473. case OPCODE_RETURN: {
  474. text += "return ";
  475. text += DADDR(1);
  476. incr = 2;
  477. } break;
  478. case OPCODE_ITERATE_BEGIN: {
  479. text += "for-init ";
  480. text += DADDR(3);
  481. text += " in ";
  482. text += DADDR(2);
  483. text += " counter ";
  484. text += DADDR(1);
  485. text += " end ";
  486. text += itos(_code_ptr[ip + 4]);
  487. incr += 5;
  488. } break;
  489. case OPCODE_ITERATE: {
  490. text += "for-loop ";
  491. text += DADDR(2);
  492. text += " in ";
  493. text += DADDR(2);
  494. text += " counter ";
  495. text += DADDR(1);
  496. text += " end ";
  497. text += itos(_code_ptr[ip + 4]);
  498. incr += 5;
  499. } break;
  500. case OPCODE_LINE: {
  501. int line = _code_ptr[ip + 1] - 1;
  502. if (line >= 0 && line < p_code_lines.size()) {
  503. text += "line ";
  504. text += itos(line + 1);
  505. text += ": ";
  506. text += p_code_lines[line];
  507. } else {
  508. text += "";
  509. }
  510. incr += 2;
  511. } break;
  512. case OPCODE_ASSERT: {
  513. text += "assert (";
  514. text += DADDR(1);
  515. text += ", ";
  516. text += DADDR(2);
  517. text += ")";
  518. incr += 3;
  519. } break;
  520. case OPCODE_BREAKPOINT: {
  521. text += "breakpoint";
  522. incr += 1;
  523. } break;
  524. case OPCODE_END: {
  525. text += "== END ==";
  526. incr += 1;
  527. } break;
  528. }
  529. ip += incr;
  530. if (text.get_string_length() > 0) {
  531. print_line(text.as_string());
  532. }
  533. }
  534. }
  535. #endif