gdscript_disassembler.cpp 30 KB

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