gdscript_disassembler.cpp 14 KB

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