2
0

gdscript_disassembler.cpp 30 KB

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