gdscript_disassembler.cpp 29 KB

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