gdscript_disassembler.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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.h"
  32. #include "gdscript_function.h"
  33. #include "core/string/string_builder.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_SET_STATIC_VARIABLE: {
  280. text += "set_static_variable script(";
  281. Ref<GDScript> gdscript = get_constant(_code_ptr[ip + 2] & GDScriptFunction::ADDR_MASK);
  282. text += gdscript.is_valid() ? gdscript->get_fully_qualified_name().get_file() : "<unknown script>";
  283. text += ")";
  284. if (gdscript.is_valid()) {
  285. text += "[\"" + gdscript->debug_get_static_var_by_index(_code_ptr[ip + 3]) + "\"]";
  286. } else {
  287. text += "[<index " + itos(_code_ptr[ip + 3]) + ">]";
  288. }
  289. text += " = ";
  290. text += DADDR(1);
  291. incr += 4;
  292. } break;
  293. case OPCODE_GET_STATIC_VARIABLE: {
  294. text += "get_static_variable ";
  295. text += DADDR(1);
  296. text += " = script(";
  297. Ref<GDScript> gdscript = get_constant(_code_ptr[ip + 2] & GDScriptFunction::ADDR_MASK);
  298. text += gdscript.is_valid() ? gdscript->get_fully_qualified_name().get_file() : "<unknown script>";
  299. text += ")";
  300. if (gdscript.is_valid()) {
  301. text += "[\"" + gdscript->debug_get_static_var_by_index(_code_ptr[ip + 3]) + "\"]";
  302. } else {
  303. text += "[<index " + itos(_code_ptr[ip + 3]) + ">]";
  304. }
  305. incr += 4;
  306. } break;
  307. case OPCODE_ASSIGN: {
  308. text += "assign ";
  309. text += DADDR(1);
  310. text += " = ";
  311. text += DADDR(2);
  312. incr += 3;
  313. } break;
  314. case OPCODE_ASSIGN_TRUE: {
  315. text += "assign ";
  316. text += DADDR(1);
  317. text += " = true";
  318. incr += 2;
  319. } break;
  320. case OPCODE_ASSIGN_FALSE: {
  321. text += "assign ";
  322. text += DADDR(1);
  323. text += " = false";
  324. incr += 2;
  325. } break;
  326. case OPCODE_ASSIGN_TYPED_BUILTIN: {
  327. text += "assign typed builtin (";
  328. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 3]);
  329. text += ") ";
  330. text += DADDR(1);
  331. text += " = ";
  332. text += DADDR(2);
  333. incr += 4;
  334. } break;
  335. case OPCODE_ASSIGN_TYPED_ARRAY: {
  336. text += "assign typed array ";
  337. text += DADDR(1);
  338. text += " = ";
  339. text += DADDR(2);
  340. incr += 6;
  341. } break;
  342. case OPCODE_ASSIGN_TYPED_NATIVE: {
  343. text += "assign typed native (";
  344. text += DADDR(3);
  345. text += ") ";
  346. text += DADDR(1);
  347. text += " = ";
  348. text += DADDR(2);
  349. incr += 4;
  350. } break;
  351. case OPCODE_ASSIGN_TYPED_SCRIPT: {
  352. Variant script = _constants_ptr[_code_ptr[ip + 3]];
  353. Script *sc = Object::cast_to<Script>(script.operator Object *());
  354. text += "assign typed script (";
  355. text += sc->get_path();
  356. text += ") ";
  357. text += DADDR(1);
  358. text += " = ";
  359. text += DADDR(2);
  360. incr += 4;
  361. } break;
  362. case OPCODE_CAST_TO_BUILTIN: {
  363. text += "cast builtin ";
  364. text += DADDR(2);
  365. text += " = ";
  366. text += DADDR(1);
  367. text += " as ";
  368. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 1]));
  369. incr += 4;
  370. } break;
  371. case OPCODE_CAST_TO_NATIVE: {
  372. text += "cast native ";
  373. text += DADDR(2);
  374. text += " = ";
  375. text += DADDR(1);
  376. text += " as ";
  377. text += DADDR(3);
  378. incr += 4;
  379. } break;
  380. case OPCODE_CAST_TO_SCRIPT: {
  381. text += "cast ";
  382. text += DADDR(2);
  383. text += " = ";
  384. text += DADDR(1);
  385. text += " as ";
  386. text += DADDR(3);
  387. incr += 4;
  388. } break;
  389. case OPCODE_CONSTRUCT: {
  390. int instr_var_args = _code_ptr[++ip];
  391. Variant::Type t = Variant::Type(_code_ptr[ip + 3 + instr_var_args]);
  392. int argc = _code_ptr[ip + 1 + instr_var_args];
  393. text += "construct ";
  394. text += DADDR(1 + argc);
  395. text += " = ";
  396. text += Variant::get_type_name(t) + "(";
  397. for (int i = 0; i < argc; i++) {
  398. if (i > 0) {
  399. text += ", ";
  400. }
  401. text += DADDR(i + 1);
  402. }
  403. text += ")";
  404. incr = 3 + instr_var_args;
  405. } break;
  406. case OPCODE_CONSTRUCT_VALIDATED: {
  407. int instr_var_args = _code_ptr[++ip];
  408. int argc = _code_ptr[ip + 1 + instr_var_args];
  409. text += "construct validated ";
  410. text += DADDR(1 + argc);
  411. text += " = ";
  412. text += constructors_names[_code_ptr[ip + 3 + argc]];
  413. text += "(";
  414. for (int i = 0; i < argc; i++) {
  415. if (i > 0) {
  416. text += ", ";
  417. }
  418. text += DADDR(i + 1);
  419. }
  420. text += ")";
  421. incr = 3 + instr_var_args;
  422. } break;
  423. case OPCODE_CONSTRUCT_ARRAY: {
  424. int instr_var_args = _code_ptr[++ip];
  425. int argc = _code_ptr[ip + 1 + instr_var_args];
  426. text += " make_array ";
  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 += 3 + argc;
  437. } break;
  438. case OPCODE_CONSTRUCT_TYPED_ARRAY: {
  439. int instr_var_args = _code_ptr[++ip];
  440. int argc = _code_ptr[ip + 1 + instr_var_args];
  441. Ref<Script> script_type = get_constant(_code_ptr[ip + argc + 2] & GDScriptFunction::ADDR_MASK);
  442. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + argc + 4];
  443. StringName native_type = get_global_name(_code_ptr[ip + argc + 5]);
  444. String type_name;
  445. if (script_type.is_valid() && script_type->is_valid()) {
  446. type_name = script_type->get_path();
  447. } else if (native_type != StringName()) {
  448. type_name = native_type;
  449. } else {
  450. type_name = Variant::get_type_name(builtin_type);
  451. }
  452. text += " make_typed_array (";
  453. text += type_name;
  454. text += ") ";
  455. text += DADDR(1 + argc);
  456. text += " = [";
  457. for (int i = 0; i < argc; i++) {
  458. if (i > 0) {
  459. text += ", ";
  460. }
  461. text += DADDR(1 + i);
  462. }
  463. text += "]";
  464. incr += 6 + argc;
  465. } break;
  466. case OPCODE_CONSTRUCT_DICTIONARY: {
  467. int instr_var_args = _code_ptr[++ip];
  468. int argc = _code_ptr[ip + 1 + instr_var_args];
  469. text += "make_dict ";
  470. text += DADDR(1 + argc * 2);
  471. text += " = {";
  472. for (int i = 0; i < argc; i++) {
  473. if (i > 0) {
  474. text += ", ";
  475. }
  476. text += DADDR(1 + i * 2 + 0);
  477. text += ": ";
  478. text += DADDR(1 + i * 2 + 1);
  479. }
  480. text += "}";
  481. incr += 3 + argc * 2;
  482. } break;
  483. case OPCODE_CALL:
  484. case OPCODE_CALL_RETURN:
  485. case OPCODE_CALL_ASYNC: {
  486. bool ret = (_code_ptr[ip]) == OPCODE_CALL_RETURN;
  487. bool async = (_code_ptr[ip]) == OPCODE_CALL_ASYNC;
  488. int instr_var_args = _code_ptr[++ip];
  489. if (ret) {
  490. text += "call-ret ";
  491. } else if (async) {
  492. text += "call-async ";
  493. } else {
  494. text += "call ";
  495. }
  496. int argc = _code_ptr[ip + 1 + instr_var_args];
  497. if (ret || async) {
  498. text += DADDR(2 + argc) + " = ";
  499. }
  500. text += DADDR(1 + argc) + ".";
  501. text += String(_global_names_ptr[_code_ptr[ip + 2 + instr_var_args]]);
  502. text += "(";
  503. for (int i = 0; i < argc; i++) {
  504. if (i > 0) {
  505. text += ", ";
  506. }
  507. text += DADDR(1 + i);
  508. }
  509. text += ")";
  510. incr = 5 + argc;
  511. } break;
  512. case OPCODE_CALL_METHOD_BIND:
  513. case OPCODE_CALL_METHOD_BIND_RET: {
  514. bool ret = (_code_ptr[ip]) == OPCODE_CALL_METHOD_BIND_RET;
  515. int instr_var_args = _code_ptr[++ip];
  516. if (ret) {
  517. text += "call-method_bind-ret ";
  518. } else {
  519. text += "call-method_bind ";
  520. }
  521. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  522. int argc = _code_ptr[ip + 1 + instr_var_args];
  523. if (ret) {
  524. text += DADDR(2 + argc) + " = ";
  525. }
  526. text += DADDR(1 + argc) + ".";
  527. text += method->get_name();
  528. text += "(";
  529. for (int i = 0; i < argc; i++) {
  530. if (i > 0) {
  531. text += ", ";
  532. }
  533. text += DADDR(1 + i);
  534. }
  535. text += ")";
  536. incr = 5 + argc;
  537. } break;
  538. case OPCODE_CALL_BUILTIN_STATIC: {
  539. int instr_var_args = _code_ptr[++ip];
  540. Variant::Type type = (Variant::Type)_code_ptr[ip + 1 + instr_var_args];
  541. int argc = _code_ptr[ip + 3 + instr_var_args];
  542. text += "call built-in method static ";
  543. text += DADDR(1 + argc);
  544. text += " = ";
  545. text += Variant::get_type_name(type);
  546. text += ".";
  547. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]].operator String();
  548. text += "(";
  549. for (int i = 0; i < argc; i++) {
  550. if (i > 0) {
  551. text += ", ";
  552. }
  553. text += DADDR(1 + i);
  554. }
  555. text += ")";
  556. incr += 5 + argc;
  557. } break;
  558. case OPCODE_CALL_NATIVE_STATIC: {
  559. int instr_var_args = _code_ptr[++ip];
  560. MethodBind *method = _methods_ptr[_code_ptr[ip + 1 + instr_var_args]];
  561. int argc = _code_ptr[ip + 2 + instr_var_args];
  562. text += "call native method static ";
  563. text += DADDR(1 + argc);
  564. text += " = ";
  565. text += method->get_instance_class();
  566. text += ".";
  567. text += method->get_name();
  568. text += "(";
  569. for (int i = 0; i < argc; i++) {
  570. if (i > 0) {
  571. text += ", ";
  572. }
  573. text += DADDR(1 + i);
  574. }
  575. text += ")";
  576. incr += 4 + argc;
  577. } break;
  578. case OPCODE_CALL_PTRCALL_NO_RETURN: {
  579. int instr_var_args = _code_ptr[++ip];
  580. text += "call-ptrcall (no return) ";
  581. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  582. int argc = _code_ptr[ip + 1 + instr_var_args];
  583. text += DADDR(1 + argc) + ".";
  584. text += method->get_name();
  585. text += "(";
  586. for (int i = 0; i < argc; i++) {
  587. if (i > 0) {
  588. text += ", ";
  589. }
  590. text += DADDR(1 + i);
  591. }
  592. text += ")";
  593. incr = 5 + argc;
  594. } break;
  595. #define DISASSEMBLE_PTRCALL(m_type) \
  596. case OPCODE_CALL_PTRCALL_##m_type: { \
  597. int instr_var_args = _code_ptr[++ip]; \
  598. text += "call-ptrcall (return "; \
  599. text += #m_type; \
  600. text += ") "; \
  601. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]]; \
  602. int argc = _code_ptr[ip + 1 + instr_var_args]; \
  603. text += DADDR(2 + argc) + " = "; \
  604. text += DADDR(1 + argc) + "."; \
  605. text += method->get_name(); \
  606. text += "("; \
  607. for (int i = 0; i < argc; i++) { \
  608. if (i > 0) \
  609. text += ", "; \
  610. text += DADDR(1 + i); \
  611. } \
  612. text += ")"; \
  613. incr = 5 + argc; \
  614. } break
  615. DISASSEMBLE_PTRCALL(BOOL);
  616. DISASSEMBLE_PTRCALL(INT);
  617. DISASSEMBLE_PTRCALL(FLOAT);
  618. DISASSEMBLE_PTRCALL(STRING);
  619. DISASSEMBLE_PTRCALL(VECTOR2);
  620. DISASSEMBLE_PTRCALL(VECTOR2I);
  621. DISASSEMBLE_PTRCALL(RECT2);
  622. DISASSEMBLE_PTRCALL(RECT2I);
  623. DISASSEMBLE_PTRCALL(VECTOR3);
  624. DISASSEMBLE_PTRCALL(VECTOR3I);
  625. DISASSEMBLE_PTRCALL(TRANSFORM2D);
  626. DISASSEMBLE_PTRCALL(VECTOR4);
  627. DISASSEMBLE_PTRCALL(VECTOR4I);
  628. DISASSEMBLE_PTRCALL(PLANE);
  629. DISASSEMBLE_PTRCALL(AABB);
  630. DISASSEMBLE_PTRCALL(BASIS);
  631. DISASSEMBLE_PTRCALL(TRANSFORM3D);
  632. DISASSEMBLE_PTRCALL(PROJECTION);
  633. DISASSEMBLE_PTRCALL(COLOR);
  634. DISASSEMBLE_PTRCALL(STRING_NAME);
  635. DISASSEMBLE_PTRCALL(NODE_PATH);
  636. DISASSEMBLE_PTRCALL(RID);
  637. DISASSEMBLE_PTRCALL(QUATERNION);
  638. DISASSEMBLE_PTRCALL(OBJECT);
  639. DISASSEMBLE_PTRCALL(CALLABLE);
  640. DISASSEMBLE_PTRCALL(SIGNAL);
  641. DISASSEMBLE_PTRCALL(DICTIONARY);
  642. DISASSEMBLE_PTRCALL(ARRAY);
  643. DISASSEMBLE_PTRCALL(PACKED_BYTE_ARRAY);
  644. DISASSEMBLE_PTRCALL(PACKED_INT32_ARRAY);
  645. DISASSEMBLE_PTRCALL(PACKED_INT64_ARRAY);
  646. DISASSEMBLE_PTRCALL(PACKED_FLOAT32_ARRAY);
  647. DISASSEMBLE_PTRCALL(PACKED_FLOAT64_ARRAY);
  648. DISASSEMBLE_PTRCALL(PACKED_STRING_ARRAY);
  649. DISASSEMBLE_PTRCALL(PACKED_VECTOR2_ARRAY);
  650. DISASSEMBLE_PTRCALL(PACKED_VECTOR3_ARRAY);
  651. DISASSEMBLE_PTRCALL(PACKED_COLOR_ARRAY);
  652. case OPCODE_CALL_BUILTIN_TYPE_VALIDATED: {
  653. int instr_var_args = _code_ptr[++ip];
  654. int argc = _code_ptr[ip + 1 + instr_var_args];
  655. text += "call-builtin-method validated ";
  656. text += DADDR(2 + argc) + " = ";
  657. text += DADDR(1) + ".";
  658. text += builtin_methods_names[_code_ptr[ip + 4 + argc]];
  659. text += "(";
  660. for (int i = 0; i < argc; i++) {
  661. if (i > 0) {
  662. text += ", ";
  663. }
  664. text += DADDR(1 + i);
  665. }
  666. text += ")";
  667. incr = 5 + argc;
  668. } break;
  669. case OPCODE_CALL_UTILITY: {
  670. int instr_var_args = _code_ptr[++ip];
  671. text += "call-utility ";
  672. int argc = _code_ptr[ip + 1 + instr_var_args];
  673. text += DADDR(1 + argc) + " = ";
  674. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  675. text += "(";
  676. for (int i = 0; i < argc; i++) {
  677. if (i > 0) {
  678. text += ", ";
  679. }
  680. text += DADDR(1 + i);
  681. }
  682. text += ")";
  683. incr = 4 + argc;
  684. } break;
  685. case OPCODE_CALL_UTILITY_VALIDATED: {
  686. int instr_var_args = _code_ptr[++ip];
  687. text += "call-utility validated ";
  688. int argc = _code_ptr[ip + 1 + instr_var_args];
  689. text += DADDR(1 + argc) + " = ";
  690. text += utilities_names[_code_ptr[ip + 3 + argc]];
  691. text += "(";
  692. for (int i = 0; i < argc; i++) {
  693. if (i > 0) {
  694. text += ", ";
  695. }
  696. text += DADDR(1 + i);
  697. }
  698. text += ")";
  699. incr = 4 + argc;
  700. } break;
  701. case OPCODE_CALL_GDSCRIPT_UTILITY: {
  702. int instr_var_args = _code_ptr[++ip];
  703. text += "call-gdscript-utility ";
  704. int argc = _code_ptr[ip + 1 + instr_var_args];
  705. text += DADDR(1 + argc) + " = ";
  706. text += gds_utilities_names[_code_ptr[ip + 3 + argc]];
  707. text += "(";
  708. for (int i = 0; i < argc; i++) {
  709. if (i > 0) {
  710. text += ", ";
  711. }
  712. text += DADDR(1 + i);
  713. }
  714. text += ")";
  715. incr = 4 + argc;
  716. } break;
  717. case OPCODE_CALL_SELF_BASE: {
  718. int instr_var_args = _code_ptr[++ip];
  719. text += "call-self-base ";
  720. int argc = _code_ptr[ip + 1 + instr_var_args];
  721. text += DADDR(2 + argc) + " = ";
  722. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  723. text += "(";
  724. for (int i = 0; i < argc; i++) {
  725. if (i > 0) {
  726. text += ", ";
  727. }
  728. text += DADDR(1 + i);
  729. }
  730. text += ")";
  731. incr = 4 + argc;
  732. } break;
  733. case OPCODE_AWAIT: {
  734. text += "await ";
  735. text += DADDR(1);
  736. incr = 2;
  737. } break;
  738. case OPCODE_AWAIT_RESUME: {
  739. text += "await resume ";
  740. text += DADDR(1);
  741. incr = 2;
  742. } break;
  743. case OPCODE_CREATE_LAMBDA: {
  744. int instr_var_args = _code_ptr[++ip];
  745. int captures_count = _code_ptr[ip + 1 + instr_var_args];
  746. GDScriptFunction *lambda = _lambdas_ptr[_code_ptr[ip + 2 + instr_var_args]];
  747. text += DADDR(1 + captures_count);
  748. text += "create lambda from ";
  749. text += lambda->name.operator String();
  750. text += "function, captures (";
  751. for (int i = 0; i < captures_count; i++) {
  752. if (i > 0) {
  753. text += ", ";
  754. }
  755. text += DADDR(1 + i);
  756. }
  757. text += ")";
  758. incr = 4 + captures_count;
  759. } break;
  760. case OPCODE_CREATE_SELF_LAMBDA: {
  761. int instr_var_args = _code_ptr[++ip];
  762. int captures_count = _code_ptr[ip + 1 + instr_var_args];
  763. GDScriptFunction *lambda = _lambdas_ptr[_code_ptr[ip + 2 + instr_var_args]];
  764. text += DADDR(1 + captures_count);
  765. text += "create self lambda from ";
  766. text += lambda->name.operator String();
  767. text += "function, captures (";
  768. for (int i = 0; i < captures_count; i++) {
  769. if (i > 0) {
  770. text += ", ";
  771. }
  772. text += DADDR(1 + i);
  773. }
  774. text += ")";
  775. incr = 4 + captures_count;
  776. } break;
  777. case OPCODE_JUMP: {
  778. text += "jump ";
  779. text += itos(_code_ptr[ip + 1]);
  780. incr = 2;
  781. } break;
  782. case OPCODE_JUMP_IF: {
  783. text += "jump-if ";
  784. text += DADDR(1);
  785. text += " to ";
  786. text += itos(_code_ptr[ip + 2]);
  787. incr = 3;
  788. } break;
  789. case OPCODE_JUMP_IF_NOT: {
  790. text += "jump-if-not ";
  791. text += DADDR(1);
  792. text += " to ";
  793. text += itos(_code_ptr[ip + 2]);
  794. incr = 3;
  795. } break;
  796. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  797. text += "jump-to-default-argument ";
  798. incr = 1;
  799. } break;
  800. case OPCODE_JUMP_IF_SHARED: {
  801. text += "jump-if-shared ";
  802. text += DADDR(1);
  803. text += " to ";
  804. text += itos(_code_ptr[ip + 2]);
  805. incr = 3;
  806. } break;
  807. case OPCODE_RETURN: {
  808. text += "return ";
  809. text += DADDR(1);
  810. incr = 2;
  811. } break;
  812. case OPCODE_RETURN_TYPED_BUILTIN: {
  813. text += "return typed builtin (";
  814. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 2]);
  815. text += ") ";
  816. text += DADDR(1);
  817. incr += 3;
  818. } break;
  819. case OPCODE_RETURN_TYPED_ARRAY: {
  820. text += "return typed array ";
  821. text += DADDR(1);
  822. incr += 5;
  823. } break;
  824. case OPCODE_RETURN_TYPED_NATIVE: {
  825. text += "return typed native (";
  826. text += DADDR(2);
  827. text += ") ";
  828. text += DADDR(1);
  829. incr += 3;
  830. } break;
  831. case OPCODE_RETURN_TYPED_SCRIPT: {
  832. Variant script = _constants_ptr[_code_ptr[ip + 2]];
  833. Script *sc = Object::cast_to<Script>(script.operator Object *());
  834. text += "return typed script (";
  835. text += sc->get_path();
  836. text += ") ";
  837. text += DADDR(1);
  838. incr += 3;
  839. } break;
  840. #define DISASSEMBLE_ITERATE(m_type) \
  841. case OPCODE_ITERATE_##m_type: { \
  842. text += "for-loop (typed "; \
  843. text += #m_type; \
  844. text += ") "; \
  845. text += DADDR(3); \
  846. text += " in "; \
  847. text += DADDR(2); \
  848. text += " counter "; \
  849. text += DADDR(1); \
  850. text += " end "; \
  851. text += itos(_code_ptr[ip + 4]); \
  852. incr += 5; \
  853. } break
  854. #define DISASSEMBLE_ITERATE_BEGIN(m_type) \
  855. case OPCODE_ITERATE_BEGIN_##m_type: { \
  856. text += "for-init (typed "; \
  857. text += #m_type; \
  858. text += ") "; \
  859. text += DADDR(3); \
  860. text += " in "; \
  861. text += DADDR(2); \
  862. text += " counter "; \
  863. text += DADDR(1); \
  864. text += " end "; \
  865. text += itos(_code_ptr[ip + 4]); \
  866. incr += 5; \
  867. } break
  868. #define DISASSEMBLE_ITERATE_TYPES(m_macro) \
  869. m_macro(INT); \
  870. m_macro(FLOAT); \
  871. m_macro(VECTOR2); \
  872. m_macro(VECTOR2I); \
  873. m_macro(VECTOR3); \
  874. m_macro(VECTOR3I); \
  875. m_macro(STRING); \
  876. m_macro(DICTIONARY); \
  877. m_macro(ARRAY); \
  878. m_macro(PACKED_BYTE_ARRAY); \
  879. m_macro(PACKED_INT32_ARRAY); \
  880. m_macro(PACKED_INT64_ARRAY); \
  881. m_macro(PACKED_FLOAT32_ARRAY); \
  882. m_macro(PACKED_FLOAT64_ARRAY); \
  883. m_macro(PACKED_STRING_ARRAY); \
  884. m_macro(PACKED_VECTOR2_ARRAY); \
  885. m_macro(PACKED_VECTOR3_ARRAY); \
  886. m_macro(PACKED_COLOR_ARRAY); \
  887. m_macro(OBJECT)
  888. case OPCODE_ITERATE_BEGIN: {
  889. text += "for-init ";
  890. text += DADDR(3);
  891. text += " in ";
  892. text += DADDR(2);
  893. text += " counter ";
  894. text += DADDR(1);
  895. text += " end ";
  896. text += itos(_code_ptr[ip + 4]);
  897. incr += 5;
  898. } break;
  899. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE_BEGIN);
  900. case OPCODE_ITERATE: {
  901. text += "for-loop ";
  902. text += DADDR(2);
  903. text += " in ";
  904. text += DADDR(2);
  905. text += " counter ";
  906. text += DADDR(1);
  907. text += " end ";
  908. text += itos(_code_ptr[ip + 4]);
  909. incr += 5;
  910. } break;
  911. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE);
  912. case OPCODE_STORE_GLOBAL: {
  913. text += "store global ";
  914. text += DADDR(1);
  915. text += " = ";
  916. text += String::num_int64(_code_ptr[ip + 2]);
  917. incr += 3;
  918. } break;
  919. case OPCODE_STORE_NAMED_GLOBAL: {
  920. text += "store named global ";
  921. text += DADDR(1);
  922. text += " = ";
  923. text += String(_global_names_ptr[_code_ptr[ip + 2]]);
  924. incr += 3;
  925. } break;
  926. case OPCODE_LINE: {
  927. int line = _code_ptr[ip + 1] - 1;
  928. if (line >= 0 && line < p_code_lines.size()) {
  929. text += "line ";
  930. text += itos(line + 1);
  931. text += ": ";
  932. text += p_code_lines[line];
  933. } else {
  934. text += "";
  935. }
  936. incr += 2;
  937. } break;
  938. #define DISASSEMBLE_TYPE_ADJUST(m_v_type) \
  939. case OPCODE_TYPE_ADJUST_##m_v_type: { \
  940. text += "type adjust ("; \
  941. text += #m_v_type; \
  942. text += ") "; \
  943. text += DADDR(1); \
  944. incr += 2; \
  945. } break
  946. DISASSEMBLE_TYPE_ADJUST(BOOL);
  947. DISASSEMBLE_TYPE_ADJUST(INT);
  948. DISASSEMBLE_TYPE_ADJUST(FLOAT);
  949. DISASSEMBLE_TYPE_ADJUST(STRING);
  950. DISASSEMBLE_TYPE_ADJUST(VECTOR2);
  951. DISASSEMBLE_TYPE_ADJUST(VECTOR2I);
  952. DISASSEMBLE_TYPE_ADJUST(RECT2);
  953. DISASSEMBLE_TYPE_ADJUST(RECT2I);
  954. DISASSEMBLE_TYPE_ADJUST(VECTOR3);
  955. DISASSEMBLE_TYPE_ADJUST(VECTOR3I);
  956. DISASSEMBLE_TYPE_ADJUST(TRANSFORM2D);
  957. DISASSEMBLE_TYPE_ADJUST(VECTOR4);
  958. DISASSEMBLE_TYPE_ADJUST(VECTOR4I);
  959. DISASSEMBLE_TYPE_ADJUST(PLANE);
  960. DISASSEMBLE_TYPE_ADJUST(QUATERNION);
  961. DISASSEMBLE_TYPE_ADJUST(AABB);
  962. DISASSEMBLE_TYPE_ADJUST(BASIS);
  963. DISASSEMBLE_TYPE_ADJUST(TRANSFORM3D);
  964. DISASSEMBLE_TYPE_ADJUST(PROJECTION);
  965. DISASSEMBLE_TYPE_ADJUST(COLOR);
  966. DISASSEMBLE_TYPE_ADJUST(STRING_NAME);
  967. DISASSEMBLE_TYPE_ADJUST(NODE_PATH);
  968. DISASSEMBLE_TYPE_ADJUST(RID);
  969. DISASSEMBLE_TYPE_ADJUST(OBJECT);
  970. DISASSEMBLE_TYPE_ADJUST(CALLABLE);
  971. DISASSEMBLE_TYPE_ADJUST(SIGNAL);
  972. DISASSEMBLE_TYPE_ADJUST(DICTIONARY);
  973. DISASSEMBLE_TYPE_ADJUST(ARRAY);
  974. DISASSEMBLE_TYPE_ADJUST(PACKED_BYTE_ARRAY);
  975. DISASSEMBLE_TYPE_ADJUST(PACKED_INT32_ARRAY);
  976. DISASSEMBLE_TYPE_ADJUST(PACKED_INT64_ARRAY);
  977. DISASSEMBLE_TYPE_ADJUST(PACKED_FLOAT32_ARRAY);
  978. DISASSEMBLE_TYPE_ADJUST(PACKED_FLOAT64_ARRAY);
  979. DISASSEMBLE_TYPE_ADJUST(PACKED_STRING_ARRAY);
  980. DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR2_ARRAY);
  981. DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR3_ARRAY);
  982. DISASSEMBLE_TYPE_ADJUST(PACKED_COLOR_ARRAY);
  983. case OPCODE_ASSERT: {
  984. text += "assert (";
  985. text += DADDR(1);
  986. text += ", ";
  987. text += DADDR(2);
  988. text += ")";
  989. incr += 3;
  990. } break;
  991. case OPCODE_BREAKPOINT: {
  992. text += "breakpoint";
  993. incr += 1;
  994. } break;
  995. case OPCODE_END: {
  996. text += "== END ==";
  997. incr += 1;
  998. } break;
  999. }
  1000. ip += incr;
  1001. if (text.get_string_length() > 0) {
  1002. print_line(text.as_string());
  1003. }
  1004. }
  1005. }
  1006. #endif // DEBUG_ENABLED