gdscript_disassembler.cpp 14 KB

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