gdscript_disassembler.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. /*************************************************************************/
  2. /* gdscript_disassembler.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. 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 += cls->get_name();
  50. txt += " (class)";
  51. } else {
  52. txt = obj->get_class();
  53. if (obj->get_script_instance()) {
  54. txt += "(" + obj->get_script_instance()->get_script()->get_path() + ")";
  55. }
  56. }
  57. }
  58. } else {
  59. txt = p_variant;
  60. }
  61. return txt;
  62. }
  63. static String _disassemble_address(const GDScript *p_script, const GDScriptFunction &p_function, int p_address) {
  64. int addr = p_address & GDScriptFunction::ADDR_MASK;
  65. switch (p_address >> GDScriptFunction::ADDR_BITS) {
  66. case GDScriptFunction::ADDR_TYPE_MEMBER: {
  67. return "member(" + p_script->debug_get_member_by_index(addr) + ")";
  68. } break;
  69. case GDScriptFunction::ADDR_TYPE_CONSTANT: {
  70. return "const(" + _get_variant_string(p_function.get_constant(addr)) + ")";
  71. } break;
  72. case GDScriptFunction::ADDR_TYPE_STACK: {
  73. switch (addr) {
  74. case GDScriptFunction::ADDR_STACK_SELF:
  75. return "self";
  76. case GDScriptFunction::ADDR_STACK_CLASS:
  77. return "class";
  78. case GDScriptFunction::ADDR_STACK_NIL:
  79. return "nil";
  80. default:
  81. return "stack(" + itos(addr) + ")";
  82. }
  83. } break;
  84. }
  85. return "<err>";
  86. }
  87. void GDScriptFunction::disassemble(const Vector<String> &p_code_lines) const {
  88. #define DADDR(m_ip) (_disassemble_address(_script, *this, _code_ptr[ip + m_ip]))
  89. for (int ip = 0; ip < _code_size;) {
  90. StringBuilder text;
  91. int incr = 0;
  92. text += " ";
  93. text += itos(ip);
  94. text += ": ";
  95. // This makes the compiler complain if some opcode is unchecked in the switch.
  96. Opcode opcode = Opcode(_code_ptr[ip]);
  97. switch (opcode) {
  98. case OPCODE_OPERATOR: {
  99. int operation = _code_ptr[ip + 4];
  100. text += "operator ";
  101. text += DADDR(3);
  102. text += " = ";
  103. text += DADDR(1);
  104. text += " ";
  105. text += Variant::get_operator_name(Variant::Operator(operation));
  106. text += " ";
  107. text += DADDR(2);
  108. incr += 5;
  109. } break;
  110. case OPCODE_OPERATOR_VALIDATED: {
  111. text += "validated operator ";
  112. text += DADDR(3);
  113. text += " = ";
  114. text += DADDR(1);
  115. text += " <operator function> ";
  116. text += DADDR(2);
  117. incr += 5;
  118. } break;
  119. case OPCODE_EXTENDS_TEST: {
  120. text += "is object ";
  121. text += DADDR(3);
  122. text += " = ";
  123. text += DADDR(1);
  124. text += " is ";
  125. text += DADDR(2);
  126. incr += 4;
  127. } break;
  128. case OPCODE_IS_BUILTIN: {
  129. text += "is builtin ";
  130. text += DADDR(2);
  131. text += " = ";
  132. text += DADDR(1);
  133. text += " is ";
  134. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 3]));
  135. incr += 4;
  136. } break;
  137. case OPCODE_SET_KEYED: {
  138. text += "set keyed ";
  139. text += DADDR(1);
  140. text += "[";
  141. text += DADDR(2);
  142. text += "] = ";
  143. text += DADDR(3);
  144. incr += 4;
  145. } break;
  146. case OPCODE_SET_KEYED_VALIDATED: {
  147. text += "set keyed validated ";
  148. text += DADDR(1);
  149. text += "[";
  150. text += DADDR(2);
  151. text += "] = ";
  152. text += DADDR(3);
  153. incr += 5;
  154. } break;
  155. case OPCODE_SET_INDEXED_VALIDATED: {
  156. text += "set indexed validated ";
  157. text += DADDR(1);
  158. text += "[";
  159. text += DADDR(2);
  160. text += "] = ";
  161. text += DADDR(3);
  162. incr += 5;
  163. } break;
  164. case OPCODE_GET_KEYED: {
  165. text += "get keyed ";
  166. text += DADDR(3);
  167. text += " = ";
  168. text += DADDR(1);
  169. text += "[";
  170. text += DADDR(2);
  171. text += "]";
  172. incr += 4;
  173. } break;
  174. case OPCODE_GET_KEYED_VALIDATED: {
  175. text += "get keyed validated ";
  176. text += DADDR(3);
  177. text += " = ";
  178. text += DADDR(1);
  179. text += "[";
  180. text += DADDR(2);
  181. text += "]";
  182. incr += 5;
  183. } break;
  184. case OPCODE_GET_INDEXED_VALIDATED: {
  185. text += "get indexed validated ";
  186. text += DADDR(3);
  187. text += " = ";
  188. text += DADDR(1);
  189. text += "[";
  190. text += DADDR(2);
  191. text += "]";
  192. incr += 5;
  193. } break;
  194. case OPCODE_SET_NAMED: {
  195. text += "set_named ";
  196. text += DADDR(1);
  197. text += "[\"";
  198. text += _global_names_ptr[_code_ptr[ip + 3]];
  199. text += "\"] = ";
  200. text += DADDR(2);
  201. incr += 4;
  202. } break;
  203. case OPCODE_SET_NAMED_VALIDATED: {
  204. text += "set_named validated ";
  205. text += DADDR(1);
  206. text += "[\"";
  207. text += "<unknown name>";
  208. text += "\"] = ";
  209. text += DADDR(2);
  210. incr += 4;
  211. } break;
  212. case OPCODE_GET_NAMED: {
  213. text += "get_named ";
  214. text += DADDR(2);
  215. text += " = ";
  216. text += DADDR(1);
  217. text += "[\"";
  218. text += _global_names_ptr[_code_ptr[ip + 3]];
  219. text += "\"]";
  220. incr += 4;
  221. } break;
  222. case OPCODE_GET_NAMED_VALIDATED: {
  223. text += "get_named validated ";
  224. text += DADDR(2);
  225. text += " = ";
  226. text += DADDR(1);
  227. text += "[\"";
  228. text += "<unknown name>";
  229. text += "\"]";
  230. incr += 4;
  231. } break;
  232. case OPCODE_SET_MEMBER: {
  233. text += "set_member ";
  234. text += "[\"";
  235. text += _global_names_ptr[_code_ptr[ip + 2]];
  236. text += "\"] = ";
  237. text += DADDR(1);
  238. incr += 3;
  239. } break;
  240. case OPCODE_GET_MEMBER: {
  241. text += "get_member ";
  242. text += DADDR(1);
  243. text += " = ";
  244. text += "[\"";
  245. text += _global_names_ptr[_code_ptr[ip + 2]];
  246. text += "\"]";
  247. incr += 3;
  248. } break;
  249. case OPCODE_ASSIGN: {
  250. text += "assign ";
  251. text += DADDR(1);
  252. text += " = ";
  253. text += DADDR(2);
  254. incr += 3;
  255. } break;
  256. case OPCODE_ASSIGN_TRUE: {
  257. text += "assign ";
  258. text += DADDR(1);
  259. text += " = true";
  260. incr += 2;
  261. } break;
  262. case OPCODE_ASSIGN_FALSE: {
  263. text += "assign ";
  264. text += DADDR(1);
  265. text += " = false";
  266. incr += 2;
  267. } break;
  268. case OPCODE_ASSIGN_TYPED_BUILTIN: {
  269. text += "assign typed builtin (";
  270. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 3]);
  271. text += ") ";
  272. text += DADDR(1);
  273. text += " = ";
  274. text += DADDR(2);
  275. incr += 4;
  276. } break;
  277. case OPCODE_ASSIGN_TYPED_ARRAY: {
  278. text += "assign typed array ";
  279. text += DADDR(1);
  280. text += " = ";
  281. text += DADDR(2);
  282. incr += 3;
  283. } break;
  284. case OPCODE_ASSIGN_TYPED_NATIVE: {
  285. text += "assign typed native (";
  286. text += DADDR(3);
  287. text += ") ";
  288. text += DADDR(1);
  289. text += " = ";
  290. text += DADDR(2);
  291. incr += 4;
  292. } break;
  293. case OPCODE_ASSIGN_TYPED_SCRIPT: {
  294. Variant script = _constants_ptr[_code_ptr[ip + 3]];
  295. Script *sc = Object::cast_to<Script>(script.operator Object *());
  296. text += "assign typed script (";
  297. text += sc->get_path();
  298. text += ") ";
  299. text += DADDR(1);
  300. text += " = ";
  301. text += DADDR(2);
  302. incr += 4;
  303. } break;
  304. case OPCODE_CAST_TO_BUILTIN: {
  305. text += "cast builtin ";
  306. text += DADDR(2);
  307. text += " = ";
  308. text += DADDR(1);
  309. text += " as ";
  310. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 1]));
  311. incr += 4;
  312. } break;
  313. case OPCODE_CAST_TO_NATIVE: {
  314. text += "cast native ";
  315. text += DADDR(2);
  316. text += " = ";
  317. text += DADDR(1);
  318. text += " as ";
  319. text += DADDR(3);
  320. incr += 4;
  321. } break;
  322. case OPCODE_CAST_TO_SCRIPT: {
  323. text += "cast ";
  324. text += DADDR(2);
  325. text += " = ";
  326. text += DADDR(1);
  327. text += " as ";
  328. text += DADDR(3);
  329. incr += 4;
  330. } break;
  331. case OPCODE_CONSTRUCT: {
  332. int instr_var_args = _code_ptr[++ip];
  333. Variant::Type t = Variant::Type(_code_ptr[ip + 3 + instr_var_args]);
  334. int argc = _code_ptr[ip + 1 + instr_var_args];
  335. text += "construct ";
  336. text += DADDR(1 + argc);
  337. text += " = ";
  338. text += Variant::get_type_name(t) + "(";
  339. for (int i = 0; i < argc; i++) {
  340. if (i > 0) {
  341. text += ", ";
  342. }
  343. text += DADDR(i + 1);
  344. }
  345. text += ")";
  346. incr = 3 + instr_var_args;
  347. } break;
  348. case OPCODE_CONSTRUCT_VALIDATED: {
  349. int instr_var_args = _code_ptr[++ip];
  350. int argc = _code_ptr[ip + 1 + instr_var_args];
  351. text += "construct validated ";
  352. text += DADDR(1 + argc);
  353. text += " = ";
  354. text += "<unknown type>(";
  355. for (int i = 0; i < argc; i++) {
  356. if (i > 0) {
  357. text += ", ";
  358. }
  359. text += DADDR(i + 1);
  360. }
  361. text += ")";
  362. incr = 3 + instr_var_args;
  363. } break;
  364. case OPCODE_CONSTRUCT_ARRAY: {
  365. int instr_var_args = _code_ptr[++ip];
  366. int argc = _code_ptr[ip + 1 + instr_var_args];
  367. text += " make_array ";
  368. text += DADDR(1 + argc);
  369. text += " = [";
  370. for (int i = 0; i < argc; i++) {
  371. if (i > 0) {
  372. text += ", ";
  373. }
  374. text += DADDR(1 + i);
  375. }
  376. text += "]";
  377. incr += 3 + argc;
  378. } break;
  379. case OPCODE_CONSTRUCT_TYPED_ARRAY: {
  380. int instr_var_args = _code_ptr[++ip];
  381. int argc = _code_ptr[ip + 1 + instr_var_args];
  382. Ref<Script> script_type = get_constant(_code_ptr[ip + argc + 2]);
  383. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + argc + 4];
  384. StringName native_type = get_global_name(_code_ptr[ip + argc + 5]);
  385. String type_name;
  386. if (script_type.is_valid() && script_type->is_valid()) {
  387. type_name = script_type->get_path();
  388. } else if (native_type != StringName()) {
  389. type_name = native_type;
  390. } else {
  391. type_name = Variant::get_type_name(builtin_type);
  392. }
  393. text += " make_typed_array (";
  394. text += type_name;
  395. text += ") ";
  396. text += DADDR(1 + argc);
  397. text += " = [";
  398. for (int i = 0; i < argc; i++) {
  399. if (i > 0) {
  400. text += ", ";
  401. }
  402. text += DADDR(1 + i);
  403. }
  404. text += "]";
  405. incr += 3 + argc;
  406. } break;
  407. case OPCODE_CONSTRUCT_DICTIONARY: {
  408. int instr_var_args = _code_ptr[++ip];
  409. int argc = _code_ptr[ip + 1 + instr_var_args];
  410. text += "make_dict ";
  411. text += DADDR(1 + argc * 2);
  412. text += " = {";
  413. for (int i = 0; i < argc; i++) {
  414. if (i > 0) {
  415. text += ", ";
  416. }
  417. text += DADDR(1 + i * 2 + 0);
  418. text += ": ";
  419. text += DADDR(1 + i * 2 + 1);
  420. }
  421. text += "}";
  422. incr += 3 + argc * 2;
  423. } break;
  424. case OPCODE_CALL:
  425. case OPCODE_CALL_RETURN:
  426. case OPCODE_CALL_ASYNC: {
  427. bool ret = (_code_ptr[ip]) == OPCODE_CALL_RETURN;
  428. bool async = (_code_ptr[ip]) == OPCODE_CALL_ASYNC;
  429. int instr_var_args = _code_ptr[++ip];
  430. if (ret) {
  431. text += "call-ret ";
  432. } else if (async) {
  433. text += "call-async ";
  434. } else {
  435. text += "call ";
  436. }
  437. int argc = _code_ptr[ip + 1 + instr_var_args];
  438. if (ret || async) {
  439. text += DADDR(2 + argc) + " = ";
  440. }
  441. text += DADDR(1 + argc) + ".";
  442. text += String(_global_names_ptr[_code_ptr[ip + 2 + instr_var_args]]);
  443. text += "(";
  444. for (int i = 0; i < argc; i++) {
  445. if (i > 0) {
  446. text += ", ";
  447. }
  448. text += DADDR(1 + i);
  449. }
  450. text += ")";
  451. incr = 5 + argc;
  452. } break;
  453. case OPCODE_CALL_METHOD_BIND:
  454. case OPCODE_CALL_METHOD_BIND_RET: {
  455. bool ret = (_code_ptr[ip]) == OPCODE_CALL_METHOD_BIND_RET;
  456. int instr_var_args = _code_ptr[++ip];
  457. if (ret) {
  458. text += "call-method_bind-ret ";
  459. } else {
  460. text += "call-method_bind ";
  461. }
  462. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  463. int argc = _code_ptr[ip + 1 + instr_var_args];
  464. if (ret) {
  465. text += DADDR(2 + argc) + " = ";
  466. }
  467. text += DADDR(1 + argc) + ".";
  468. text += method->get_name();
  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 = 5 + argc;
  478. } break;
  479. case OPCODE_CALL_BUILTIN_STATIC: {
  480. int instr_var_args = _code_ptr[++ip];
  481. Variant::Type type = (Variant::Type)_code_ptr[ip + 1 + instr_var_args];
  482. int argc = _code_ptr[ip + 3 + instr_var_args];
  483. text += "call built-in method static ";
  484. text += DADDR(1 + argc);
  485. text += " = ";
  486. text += Variant::get_type_name(type);
  487. text += ".";
  488. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]].operator String();
  489. text += "(";
  490. for (int i = 0; i < argc; i++) {
  491. if (i > 0) {
  492. text += ", ";
  493. }
  494. text += DADDR(1 + i);
  495. }
  496. text += ")";
  497. incr += 5 + argc;
  498. } break;
  499. case OPCODE_CALL_NATIVE_STATIC: {
  500. int instr_var_args = _code_ptr[++ip];
  501. MethodBind *method = _methods_ptr[_code_ptr[ip + 1 + instr_var_args]];
  502. int argc = _code_ptr[ip + 2 + instr_var_args];
  503. text += "call native method static ";
  504. text += DADDR(1 + argc);
  505. text += " = ";
  506. text += method->get_instance_class();
  507. text += ".";
  508. text += method->get_name();
  509. text += "(";
  510. for (int i = 0; i < argc; i++) {
  511. if (i > 0) {
  512. text += ", ";
  513. }
  514. text += DADDR(1 + i);
  515. }
  516. text += ")";
  517. incr += 4 + argc;
  518. } break;
  519. case OPCODE_CALL_PTRCALL_NO_RETURN: {
  520. int instr_var_args = _code_ptr[++ip];
  521. text += "call-ptrcall (no return) ";
  522. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  523. int argc = _code_ptr[ip + 1 + instr_var_args];
  524. text += DADDR(1 + argc) + ".";
  525. text += method->get_name();
  526. text += "(";
  527. for (int i = 0; i < argc; i++) {
  528. if (i > 0) {
  529. text += ", ";
  530. }
  531. text += DADDR(1 + i);
  532. }
  533. text += ")";
  534. incr = 5 + argc;
  535. } break;
  536. #define DISASSEMBLE_PTRCALL(m_type) \
  537. case OPCODE_CALL_PTRCALL_##m_type: { \
  538. int instr_var_args = _code_ptr[++ip]; \
  539. text += "call-ptrcall (return "; \
  540. text += #m_type; \
  541. text += ") "; \
  542. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]]; \
  543. int argc = _code_ptr[ip + 1 + instr_var_args]; \
  544. text += DADDR(2 + argc) + " = "; \
  545. text += DADDR(1 + argc) + "."; \
  546. text += method->get_name(); \
  547. text += "("; \
  548. for (int i = 0; i < argc; i++) { \
  549. if (i > 0) \
  550. text += ", "; \
  551. text += DADDR(1 + i); \
  552. } \
  553. text += ")"; \
  554. incr = 5 + argc; \
  555. } break
  556. DISASSEMBLE_PTRCALL(BOOL);
  557. DISASSEMBLE_PTRCALL(INT);
  558. DISASSEMBLE_PTRCALL(FLOAT);
  559. DISASSEMBLE_PTRCALL(STRING);
  560. DISASSEMBLE_PTRCALL(VECTOR2);
  561. DISASSEMBLE_PTRCALL(VECTOR2I);
  562. DISASSEMBLE_PTRCALL(RECT2);
  563. DISASSEMBLE_PTRCALL(RECT2I);
  564. DISASSEMBLE_PTRCALL(VECTOR3);
  565. DISASSEMBLE_PTRCALL(VECTOR3I);
  566. DISASSEMBLE_PTRCALL(TRANSFORM2D);
  567. DISASSEMBLE_PTRCALL(VECTOR4);
  568. DISASSEMBLE_PTRCALL(VECTOR4I);
  569. DISASSEMBLE_PTRCALL(PLANE);
  570. DISASSEMBLE_PTRCALL(AABB);
  571. DISASSEMBLE_PTRCALL(BASIS);
  572. DISASSEMBLE_PTRCALL(TRANSFORM3D);
  573. DISASSEMBLE_PTRCALL(PROJECTION);
  574. DISASSEMBLE_PTRCALL(COLOR);
  575. DISASSEMBLE_PTRCALL(STRING_NAME);
  576. DISASSEMBLE_PTRCALL(NODE_PATH);
  577. DISASSEMBLE_PTRCALL(RID);
  578. DISASSEMBLE_PTRCALL(QUATERNION);
  579. DISASSEMBLE_PTRCALL(OBJECT);
  580. DISASSEMBLE_PTRCALL(CALLABLE);
  581. DISASSEMBLE_PTRCALL(SIGNAL);
  582. DISASSEMBLE_PTRCALL(DICTIONARY);
  583. DISASSEMBLE_PTRCALL(ARRAY);
  584. DISASSEMBLE_PTRCALL(PACKED_BYTE_ARRAY);
  585. DISASSEMBLE_PTRCALL(PACKED_INT32_ARRAY);
  586. DISASSEMBLE_PTRCALL(PACKED_INT64_ARRAY);
  587. DISASSEMBLE_PTRCALL(PACKED_FLOAT32_ARRAY);
  588. DISASSEMBLE_PTRCALL(PACKED_FLOAT64_ARRAY);
  589. DISASSEMBLE_PTRCALL(PACKED_STRING_ARRAY);
  590. DISASSEMBLE_PTRCALL(PACKED_VECTOR2_ARRAY);
  591. DISASSEMBLE_PTRCALL(PACKED_VECTOR3_ARRAY);
  592. DISASSEMBLE_PTRCALL(PACKED_COLOR_ARRAY);
  593. case OPCODE_CALL_BUILTIN_TYPE_VALIDATED: {
  594. int instr_var_args = _code_ptr[++ip];
  595. int argc = _code_ptr[ip + 1 + instr_var_args];
  596. text += "call-builtin-method validated ";
  597. text += DADDR(2 + argc) + " = ";
  598. text += DADDR(1) + ".";
  599. text += "<unknown method>";
  600. text += "(";
  601. for (int i = 0; i < argc; i++) {
  602. if (i > 0) {
  603. text += ", ";
  604. }
  605. text += DADDR(1 + i);
  606. }
  607. text += ")";
  608. incr = 5 + argc;
  609. } break;
  610. case OPCODE_CALL_UTILITY: {
  611. int instr_var_args = _code_ptr[++ip];
  612. text += "call-utility ";
  613. int argc = _code_ptr[ip + 1 + instr_var_args];
  614. text += DADDR(1 + argc) + " = ";
  615. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  616. text += "(";
  617. for (int i = 0; i < argc; i++) {
  618. if (i > 0) {
  619. text += ", ";
  620. }
  621. text += DADDR(1 + i);
  622. }
  623. text += ")";
  624. incr = 4 + argc;
  625. } break;
  626. case OPCODE_CALL_UTILITY_VALIDATED: {
  627. int instr_var_args = _code_ptr[++ip];
  628. text += "call-utility ";
  629. int argc = _code_ptr[ip + 1 + instr_var_args];
  630. text += DADDR(1 + argc) + " = ";
  631. text += "<unknown function>";
  632. text += "(";
  633. for (int i = 0; i < argc; i++) {
  634. if (i > 0) {
  635. text += ", ";
  636. }
  637. text += DADDR(1 + i);
  638. }
  639. text += ")";
  640. incr = 4 + argc;
  641. } break;
  642. case OPCODE_CALL_GDSCRIPT_UTILITY: {
  643. int instr_var_args = _code_ptr[++ip];
  644. text += "call-gscript-utility ";
  645. int argc = _code_ptr[ip + 1 + instr_var_args];
  646. text += DADDR(1 + argc) + " = ";
  647. text += "<unknown function>";
  648. text += "(";
  649. for (int i = 0; i < argc; i++) {
  650. if (i > 0) {
  651. text += ", ";
  652. }
  653. text += DADDR(1 + i);
  654. }
  655. text += ")";
  656. incr = 4 + argc;
  657. } break;
  658. case OPCODE_CALL_SELF_BASE: {
  659. int instr_var_args = _code_ptr[++ip];
  660. text += "call-self-base ";
  661. int argc = _code_ptr[ip + 1 + instr_var_args];
  662. text += DADDR(2 + argc) + " = ";
  663. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  664. text += "(";
  665. for (int i = 0; i < argc; i++) {
  666. if (i > 0) {
  667. text += ", ";
  668. }
  669. text += DADDR(1 + i);
  670. }
  671. text += ")";
  672. incr = 4 + argc;
  673. } break;
  674. case OPCODE_AWAIT: {
  675. text += "await ";
  676. text += DADDR(1);
  677. incr = 2;
  678. } break;
  679. case OPCODE_AWAIT_RESUME: {
  680. text += "await resume ";
  681. text += DADDR(1);
  682. incr = 2;
  683. } break;
  684. case OPCODE_CREATE_LAMBDA: {
  685. int instr_var_args = _code_ptr[++ip];
  686. int captures_count = _code_ptr[ip + 1 + instr_var_args];
  687. GDScriptFunction *lambda = _lambdas_ptr[_code_ptr[ip + 2 + instr_var_args]];
  688. text += DADDR(1 + captures_count);
  689. text += "create lambda from ";
  690. text += lambda->name.operator String();
  691. text += "function, captures (";
  692. for (int i = 0; i < captures_count; i++) {
  693. if (i > 0) {
  694. text += ", ";
  695. }
  696. text += DADDR(1 + i);
  697. }
  698. text += ")";
  699. incr = 3 + captures_count;
  700. } break;
  701. case OPCODE_CREATE_SELF_LAMBDA: {
  702. int instr_var_args = _code_ptr[++ip];
  703. int captures_count = _code_ptr[ip + 1 + instr_var_args];
  704. GDScriptFunction *lambda = _lambdas_ptr[_code_ptr[ip + 2 + instr_var_args]];
  705. text += DADDR(1 + captures_count);
  706. text += "create self lambda from ";
  707. text += lambda->name.operator String();
  708. text += "function, captures (";
  709. for (int i = 0; i < captures_count; i++) {
  710. if (i > 0) {
  711. text += ", ";
  712. }
  713. text += DADDR(1 + i);
  714. }
  715. text += ")";
  716. incr = 3 + captures_count;
  717. } break;
  718. case OPCODE_JUMP: {
  719. text += "jump ";
  720. text += itos(_code_ptr[ip + 1]);
  721. incr = 2;
  722. } break;
  723. case OPCODE_JUMP_IF: {
  724. text += "jump-if ";
  725. text += DADDR(1);
  726. text += " to ";
  727. text += itos(_code_ptr[ip + 2]);
  728. incr = 3;
  729. } break;
  730. case OPCODE_JUMP_IF_NOT: {
  731. text += "jump-if-not ";
  732. text += DADDR(1);
  733. text += " to ";
  734. text += itos(_code_ptr[ip + 2]);
  735. incr = 3;
  736. } break;
  737. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  738. text += "jump-to-default-argument ";
  739. incr = 1;
  740. } break;
  741. case OPCODE_JUMP_IF_SHARED: {
  742. text += "jump-if-shared ";
  743. text += DADDR(1);
  744. text += " to ";
  745. text += itos(_code_ptr[ip + 2]);
  746. incr = 3;
  747. } break;
  748. case OPCODE_RETURN: {
  749. text += "return ";
  750. text += DADDR(1);
  751. incr = 2;
  752. } break;
  753. case OPCODE_RETURN_TYPED_BUILTIN: {
  754. text += "return typed builtin (";
  755. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 2]);
  756. text += ") ";
  757. text += DADDR(1);
  758. incr += 3;
  759. } break;
  760. case OPCODE_RETURN_TYPED_ARRAY: {
  761. text += "return typed array ";
  762. text += DADDR(1);
  763. incr += 5;
  764. } break;
  765. case OPCODE_RETURN_TYPED_NATIVE: {
  766. text += "return typed native (";
  767. text += DADDR(2);
  768. text += ") ";
  769. text += DADDR(1);
  770. incr += 3;
  771. } break;
  772. case OPCODE_RETURN_TYPED_SCRIPT: {
  773. Variant script = _constants_ptr[_code_ptr[ip + 2]];
  774. Script *sc = Object::cast_to<Script>(script.operator Object *());
  775. text += "return typed script (";
  776. text += sc->get_path();
  777. text += ") ";
  778. text += DADDR(1);
  779. incr += 3;
  780. } break;
  781. #define DISASSEMBLE_ITERATE(m_type) \
  782. case OPCODE_ITERATE_##m_type: { \
  783. text += "for-loop (typed "; \
  784. text += #m_type; \
  785. text += ") "; \
  786. text += DADDR(3); \
  787. text += " in "; \
  788. text += DADDR(2); \
  789. text += " counter "; \
  790. text += DADDR(1); \
  791. text += " end "; \
  792. text += itos(_code_ptr[ip + 4]); \
  793. incr += 5; \
  794. } break
  795. #define DISASSEMBLE_ITERATE_BEGIN(m_type) \
  796. case OPCODE_ITERATE_BEGIN_##m_type: { \
  797. text += "for-init (typed "; \
  798. text += #m_type; \
  799. text += ") "; \
  800. text += DADDR(3); \
  801. text += " in "; \
  802. text += DADDR(2); \
  803. text += " counter "; \
  804. text += DADDR(1); \
  805. text += " end "; \
  806. text += itos(_code_ptr[ip + 4]); \
  807. incr += 5; \
  808. } break
  809. #define DISASSEMBLE_ITERATE_TYPES(m_macro) \
  810. m_macro(INT); \
  811. m_macro(FLOAT); \
  812. m_macro(VECTOR2); \
  813. m_macro(VECTOR2I); \
  814. m_macro(VECTOR3); \
  815. m_macro(VECTOR3I); \
  816. m_macro(STRING); \
  817. m_macro(DICTIONARY); \
  818. m_macro(ARRAY); \
  819. m_macro(PACKED_BYTE_ARRAY); \
  820. m_macro(PACKED_INT32_ARRAY); \
  821. m_macro(PACKED_INT64_ARRAY); \
  822. m_macro(PACKED_FLOAT32_ARRAY); \
  823. m_macro(PACKED_FLOAT64_ARRAY); \
  824. m_macro(PACKED_STRING_ARRAY); \
  825. m_macro(PACKED_VECTOR2_ARRAY); \
  826. m_macro(PACKED_VECTOR3_ARRAY); \
  827. m_macro(PACKED_COLOR_ARRAY); \
  828. m_macro(OBJECT)
  829. case OPCODE_ITERATE_BEGIN: {
  830. text += "for-init ";
  831. text += DADDR(3);
  832. text += " in ";
  833. text += DADDR(2);
  834. text += " counter ";
  835. text += DADDR(1);
  836. text += " end ";
  837. text += itos(_code_ptr[ip + 4]);
  838. incr += 5;
  839. } break;
  840. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE_BEGIN);
  841. case OPCODE_ITERATE: {
  842. text += "for-loop ";
  843. text += DADDR(2);
  844. text += " in ";
  845. text += DADDR(2);
  846. text += " counter ";
  847. text += DADDR(1);
  848. text += " end ";
  849. text += itos(_code_ptr[ip + 4]);
  850. incr += 5;
  851. } break;
  852. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE);
  853. case OPCODE_STORE_GLOBAL: {
  854. text += "store global ";
  855. text += DADDR(1);
  856. text += " = ";
  857. text += String::num_int64(_code_ptr[ip + 2]);
  858. incr += 3;
  859. } break;
  860. case OPCODE_STORE_NAMED_GLOBAL: {
  861. text += "store named global ";
  862. text += DADDR(1);
  863. text += " = ";
  864. text += String(_global_names_ptr[_code_ptr[ip + 2]]);
  865. incr += 3;
  866. } break;
  867. case OPCODE_LINE: {
  868. int line = _code_ptr[ip + 1] - 1;
  869. if (line >= 0 && line < p_code_lines.size()) {
  870. text += "line ";
  871. text += itos(line + 1);
  872. text += ": ";
  873. text += p_code_lines[line];
  874. } else {
  875. text += "";
  876. }
  877. incr += 2;
  878. } break;
  879. #define DISASSEMBLE_TYPE_ADJUST(m_v_type) \
  880. case OPCODE_TYPE_ADJUST_##m_v_type: { \
  881. text += "type adjust ("; \
  882. text += #m_v_type; \
  883. text += ") "; \
  884. text += DADDR(1); \
  885. incr += 2; \
  886. } break
  887. DISASSEMBLE_TYPE_ADJUST(BOOL);
  888. DISASSEMBLE_TYPE_ADJUST(INT);
  889. DISASSEMBLE_TYPE_ADJUST(FLOAT);
  890. DISASSEMBLE_TYPE_ADJUST(STRING);
  891. DISASSEMBLE_TYPE_ADJUST(VECTOR2);
  892. DISASSEMBLE_TYPE_ADJUST(VECTOR2I);
  893. DISASSEMBLE_TYPE_ADJUST(RECT2);
  894. DISASSEMBLE_TYPE_ADJUST(RECT2I);
  895. DISASSEMBLE_TYPE_ADJUST(VECTOR3);
  896. DISASSEMBLE_TYPE_ADJUST(VECTOR3I);
  897. DISASSEMBLE_TYPE_ADJUST(TRANSFORM2D);
  898. DISASSEMBLE_TYPE_ADJUST(VECTOR4);
  899. DISASSEMBLE_TYPE_ADJUST(VECTOR4I);
  900. DISASSEMBLE_TYPE_ADJUST(PLANE);
  901. DISASSEMBLE_TYPE_ADJUST(QUATERNION);
  902. DISASSEMBLE_TYPE_ADJUST(AABB);
  903. DISASSEMBLE_TYPE_ADJUST(BASIS);
  904. DISASSEMBLE_TYPE_ADJUST(TRANSFORM3D);
  905. DISASSEMBLE_TYPE_ADJUST(PROJECTION);
  906. DISASSEMBLE_TYPE_ADJUST(COLOR);
  907. DISASSEMBLE_TYPE_ADJUST(STRING_NAME);
  908. DISASSEMBLE_TYPE_ADJUST(NODE_PATH);
  909. DISASSEMBLE_TYPE_ADJUST(RID);
  910. DISASSEMBLE_TYPE_ADJUST(OBJECT);
  911. DISASSEMBLE_TYPE_ADJUST(CALLABLE);
  912. DISASSEMBLE_TYPE_ADJUST(SIGNAL);
  913. DISASSEMBLE_TYPE_ADJUST(DICTIONARY);
  914. DISASSEMBLE_TYPE_ADJUST(ARRAY);
  915. DISASSEMBLE_TYPE_ADJUST(PACKED_BYTE_ARRAY);
  916. DISASSEMBLE_TYPE_ADJUST(PACKED_INT32_ARRAY);
  917. DISASSEMBLE_TYPE_ADJUST(PACKED_INT64_ARRAY);
  918. DISASSEMBLE_TYPE_ADJUST(PACKED_FLOAT32_ARRAY);
  919. DISASSEMBLE_TYPE_ADJUST(PACKED_FLOAT64_ARRAY);
  920. DISASSEMBLE_TYPE_ADJUST(PACKED_STRING_ARRAY);
  921. DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR2_ARRAY);
  922. DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR3_ARRAY);
  923. DISASSEMBLE_TYPE_ADJUST(PACKED_COLOR_ARRAY);
  924. case OPCODE_ASSERT: {
  925. text += "assert (";
  926. text += DADDR(1);
  927. text += ", ";
  928. text += DADDR(2);
  929. text += ")";
  930. incr += 3;
  931. } break;
  932. case OPCODE_BREAKPOINT: {
  933. text += "breakpoint";
  934. incr += 1;
  935. } break;
  936. case OPCODE_END: {
  937. text += "== END ==";
  938. incr += 1;
  939. } break;
  940. }
  941. ip += incr;
  942. if (text.get_string_length() > 0) {
  943. print_line(text.as_string());
  944. }
  945. }
  946. }
  947. #endif