gdscript_disassembler.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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]);
  110. switch (code) {
  111. case OPCODE_OPERATOR: {
  112. int operation = _code_ptr[ip + 1];
  113. text += "operator ";
  114. text += DADDR(4);
  115. text += " = ";
  116. text += DADDR(2);
  117. text += " ";
  118. text += Variant::get_operator_name(Variant::Operator(operation));
  119. text += " ";
  120. text += DADDR(3);
  121. incr += 5;
  122. } break;
  123. case OPCODE_EXTENDS_TEST: {
  124. text += "is object ";
  125. text += DADDR(3);
  126. text += " = ";
  127. text += DADDR(1);
  128. text += " is ";
  129. text += DADDR(2);
  130. incr += 4;
  131. } break;
  132. case OPCODE_IS_BUILTIN: {
  133. text += "is builtin ";
  134. text += DADDR(3);
  135. text += " = ";
  136. text += DADDR(1);
  137. text += " is ";
  138. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 2]));
  139. incr += 4;
  140. } break;
  141. case OPCODE_SET: {
  142. text += "set ";
  143. text += DADDR(1);
  144. text += "[";
  145. text += DADDR(2);
  146. text += "] = ";
  147. text += DADDR(3);
  148. incr += 4;
  149. } break;
  150. case OPCODE_GET: {
  151. text += "get ";
  152. text += DADDR(3);
  153. text += " = ";
  154. text += DADDR(1);
  155. text += "[";
  156. text += DADDR(2);
  157. text += "]";
  158. incr += 4;
  159. } break;
  160. case OPCODE_SET_NAMED: {
  161. text += "set_named ";
  162. text += DADDR(1);
  163. text += "[\"";
  164. text += _global_names_ptr[_code_ptr[ip + 2]];
  165. text += "\"] = ";
  166. text += DADDR(3);
  167. incr += 4;
  168. } break;
  169. case OPCODE_GET_NAMED: {
  170. text += "get_named ";
  171. text += DADDR(3);
  172. text += " = ";
  173. text += DADDR(1);
  174. text += "[\"";
  175. text += _global_names_ptr[_code_ptr[ip + 2]];
  176. text += "\"]";
  177. incr += 4;
  178. } break;
  179. case OPCODE_SET_MEMBER: {
  180. text += "set_member ";
  181. text += "[\"";
  182. text += _global_names_ptr[_code_ptr[ip + 1]];
  183. text += "\"] = ";
  184. text += DADDR(2);
  185. incr += 3;
  186. } break;
  187. case OPCODE_GET_MEMBER: {
  188. text += "get_member ";
  189. text += DADDR(2);
  190. text += " = ";
  191. text += "[\"";
  192. text += _global_names_ptr[_code_ptr[ip + 1]];
  193. text += "\"]";
  194. incr += 3;
  195. } break;
  196. case OPCODE_ASSIGN: {
  197. text += "assign ";
  198. text += DADDR(1);
  199. text += " = ";
  200. text += DADDR(2);
  201. incr += 3;
  202. } break;
  203. case OPCODE_ASSIGN_TRUE: {
  204. text += "assign ";
  205. text += DADDR(1);
  206. text += " = true";
  207. incr += 2;
  208. } break;
  209. case OPCODE_ASSIGN_FALSE: {
  210. text += "assign ";
  211. text += DADDR(1);
  212. text += " = false";
  213. incr += 2;
  214. } break;
  215. case OPCODE_ASSIGN_TYPED_BUILTIN: {
  216. text += "assign typed builtin (";
  217. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 1]);
  218. text += ") ";
  219. text += DADDR(2);
  220. text += " = ";
  221. text += DADDR(3);
  222. incr += 4;
  223. } break;
  224. case OPCODE_ASSIGN_TYPED_NATIVE: {
  225. Variant class_name = _constants_ptr[_code_ptr[ip + 1]];
  226. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(class_name.operator Object *());
  227. text += "assign typed native (";
  228. text += nc->get_name().operator String();
  229. text += ") ";
  230. text += DADDR(2);
  231. text += " = ";
  232. text += DADDR(3);
  233. incr += 4;
  234. } break;
  235. case OPCODE_ASSIGN_TYPED_SCRIPT: {
  236. Variant script = _constants_ptr[_code_ptr[ip + 1]];
  237. Script *sc = Object::cast_to<Script>(script.operator Object *());
  238. text += "assign typed script (";
  239. text += sc->get_path();
  240. text += ") ";
  241. text += DADDR(2);
  242. text += " = ";
  243. text += DADDR(3);
  244. incr += 4;
  245. } break;
  246. case OPCODE_CAST_TO_BUILTIN: {
  247. text += "cast builtin ";
  248. text += DADDR(3);
  249. text += " = ";
  250. text += DADDR(2);
  251. text += " as ";
  252. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 1]));
  253. incr += 4;
  254. } break;
  255. case OPCODE_CAST_TO_NATIVE: {
  256. Variant class_name = _constants_ptr[_code_ptr[ip + 1]];
  257. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(class_name.operator Object *());
  258. text += "cast native ";
  259. text += DADDR(3);
  260. text += " = ";
  261. text += DADDR(2);
  262. text += " as ";
  263. text += nc->get_name();
  264. incr += 4;
  265. } break;
  266. case OPCODE_CAST_TO_SCRIPT: {
  267. text += "cast ";
  268. text += DADDR(3);
  269. text += " = ";
  270. text += DADDR(2);
  271. text += " as ";
  272. text += DADDR(1);
  273. incr += 4;
  274. } break;
  275. case OPCODE_CONSTRUCT: {
  276. Variant::Type t = Variant::Type(_code_ptr[ip + 1]);
  277. int argc = _code_ptr[ip + 2];
  278. text += "construct ";
  279. text += DADDR(3 + argc);
  280. text += " = ";
  281. text += Variant::get_type_name(t) + "(";
  282. for (int i = 0; i < argc; i++) {
  283. if (i > 0)
  284. text += ", ";
  285. text += DADDR(i + 3);
  286. }
  287. text += ")";
  288. incr = 4 + argc;
  289. } break;
  290. case OPCODE_CONSTRUCT_ARRAY: {
  291. int argc = _code_ptr[ip + 1];
  292. text += " make_array ";
  293. text += DADDR(2 + argc);
  294. text += " = [";
  295. for (int i = 0; i < argc; i++) {
  296. if (i > 0)
  297. text += ", ";
  298. text += DADDR(2 + i);
  299. }
  300. text += "]";
  301. incr += 3 + argc;
  302. } break;
  303. case OPCODE_CONSTRUCT_DICTIONARY: {
  304. int argc = _code_ptr[ip + 1];
  305. text += "make_dict ";
  306. text += DADDR(2 + argc * 2);
  307. text += " = {";
  308. for (int i = 0; i < argc; i++) {
  309. if (i > 0)
  310. text += ", ";
  311. text += DADDR(2 + i * 2 + 0);
  312. text += ": ";
  313. text += DADDR(2 + i * 2 + 1);
  314. }
  315. text += "}";
  316. incr += 3 + argc * 2;
  317. } break;
  318. case OPCODE_CALL:
  319. case OPCODE_CALL_RETURN:
  320. case OPCODE_CALL_ASYNC: {
  321. bool ret = _code_ptr[ip] == OPCODE_CALL_RETURN;
  322. bool async = _code_ptr[ip] == OPCODE_CALL_ASYNC;
  323. if (ret) {
  324. text += "call-ret ";
  325. } else if (async) {
  326. text += "call-async ";
  327. } else {
  328. text += "call ";
  329. }
  330. int argc = _code_ptr[ip + 1];
  331. if (ret || async) {
  332. text += DADDR(4 + argc) + " = ";
  333. }
  334. text += DADDR(2) + ".";
  335. text += String(_global_names_ptr[_code_ptr[ip + 3]]);
  336. text += "(";
  337. for (int i = 0; i < argc; i++) {
  338. if (i > 0)
  339. text += ", ";
  340. text += DADDR(4 + i);
  341. }
  342. text += ")";
  343. incr = 5 + argc;
  344. } break;
  345. case OPCODE_CALL_BUILT_IN: {
  346. text += "call-built-in ";
  347. int argc = _code_ptr[ip + 2];
  348. text += DADDR(3 + argc) + " = ";
  349. text += GDScriptFunctions::get_func_name(GDScriptFunctions::Function(_code_ptr[ip + 1]));
  350. text += "(";
  351. for (int i = 0; i < argc; i++) {
  352. if (i > 0)
  353. text += ", ";
  354. text += DADDR(3 + i);
  355. }
  356. text += ")";
  357. incr = 4 + argc;
  358. } break;
  359. case OPCODE_CALL_SELF_BASE: {
  360. text += "call-self-base ";
  361. int argc = _code_ptr[ip + 2];
  362. text += DADDR(3 + argc) + " = ";
  363. text += _global_names_ptr[_code_ptr[ip + 1]];
  364. text += "(";
  365. for (int i = 0; i < argc; i++) {
  366. if (i > 0)
  367. text += ", ";
  368. text += DADDR(3 + i);
  369. }
  370. text += ")";
  371. incr = 4 + argc;
  372. } break;
  373. case OPCODE_AWAIT: {
  374. text += "await ";
  375. text += DADDR(1);
  376. incr += 2;
  377. } break;
  378. case OPCODE_AWAIT_RESUME: {
  379. text += "await resume ";
  380. text += DADDR(1);
  381. incr = 2;
  382. } break;
  383. case OPCODE_JUMP: {
  384. text += "jump ";
  385. text += itos(_code_ptr[ip + 1]);
  386. incr = 2;
  387. } break;
  388. case OPCODE_JUMP_IF: {
  389. text += "jump-if ";
  390. text += DADDR(1);
  391. text += " to ";
  392. text += itos(_code_ptr[ip + 2]);
  393. incr = 3;
  394. } break;
  395. case OPCODE_JUMP_IF_NOT: {
  396. text += "jump-if-not ";
  397. text += DADDR(1);
  398. text += " to ";
  399. text += itos(_code_ptr[ip + 2]);
  400. incr = 3;
  401. } break;
  402. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  403. text += "jump-to-default-argument ";
  404. incr = 1;
  405. } break;
  406. case OPCODE_RETURN: {
  407. text += "return ";
  408. text += DADDR(1);
  409. incr = 2;
  410. } break;
  411. case OPCODE_ITERATE_BEGIN: {
  412. text += "for-init ";
  413. text += DADDR(4);
  414. text += " in ";
  415. text += DADDR(2);
  416. text += " counter ";
  417. text += DADDR(1);
  418. text += " end ";
  419. text += itos(_code_ptr[ip + 3]);
  420. incr += 5;
  421. } break;
  422. case OPCODE_ITERATE: {
  423. text += "for-loop ";
  424. text += DADDR(4);
  425. text += " in ";
  426. text += DADDR(2);
  427. text += " counter ";
  428. text += DADDR(1);
  429. text += " end ";
  430. text += itos(_code_ptr[ip + 3]);
  431. incr += 5;
  432. } break;
  433. case OPCODE_LINE: {
  434. int line = _code_ptr[ip + 1] - 1;
  435. if (line >= 0 && line < p_code_lines.size()) {
  436. text += "line ";
  437. text += itos(line + 1);
  438. text += ": ";
  439. text += p_code_lines[line];
  440. } else {
  441. text += "";
  442. }
  443. incr += 2;
  444. } break;
  445. case OPCODE_ASSERT: {
  446. text += "assert (";
  447. text += DADDR(1);
  448. text += ", ";
  449. text += DADDR(2);
  450. text += ")";
  451. incr += 3;
  452. } break;
  453. case OPCODE_BREAKPOINT: {
  454. text += "breakpoint";
  455. incr += 1;
  456. } break;
  457. case OPCODE_END: {
  458. text += "== END ==";
  459. incr += 1;
  460. } break;
  461. }
  462. ip += incr;
  463. if (text.get_string_length() > 0) {
  464. print_line(text.as_string());
  465. }
  466. }
  467. }
  468. #endif