gdscript_disassembler.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  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_DICTIONARY: {
  158. text += "type test ";
  159. text += DADDR(1);
  160. text += " = ";
  161. text += DADDR(2);
  162. text += " is Dictionary[";
  163. Ref<Script> key_script_type = get_constant(_code_ptr[ip + 3] & ADDR_MASK);
  164. Variant::Type key_builtin_type = (Variant::Type)_code_ptr[ip + 5];
  165. StringName key_native_type = get_global_name(_code_ptr[ip + 6]);
  166. if (key_script_type.is_valid() && key_script_type->is_valid()) {
  167. text += "script(";
  168. text += GDScript::debug_get_script_name(key_script_type);
  169. text += ")";
  170. } else if (key_native_type != StringName()) {
  171. text += key_native_type;
  172. } else {
  173. text += Variant::get_type_name(key_builtin_type);
  174. }
  175. text += ", ";
  176. Ref<Script> value_script_type = get_constant(_code_ptr[ip + 4] & ADDR_MASK);
  177. Variant::Type value_builtin_type = (Variant::Type)_code_ptr[ip + 7];
  178. StringName value_native_type = get_global_name(_code_ptr[ip + 8]);
  179. if (value_script_type.is_valid() && value_script_type->is_valid()) {
  180. text += "script(";
  181. text += GDScript::debug_get_script_name(value_script_type);
  182. text += ")";
  183. } else if (value_native_type != StringName()) {
  184. text += value_native_type;
  185. } else {
  186. text += Variant::get_type_name(value_builtin_type);
  187. }
  188. text += "]";
  189. incr += 9;
  190. } break;
  191. case OPCODE_TYPE_TEST_NATIVE: {
  192. text += "type test ";
  193. text += DADDR(1);
  194. text += " = ";
  195. text += DADDR(2);
  196. text += " is ";
  197. text += get_global_name(_code_ptr[ip + 3]);
  198. incr += 4;
  199. } break;
  200. case OPCODE_TYPE_TEST_SCRIPT: {
  201. text += "type test ";
  202. text += DADDR(1);
  203. text += " = ";
  204. text += DADDR(2);
  205. text += " is ";
  206. text += DADDR(3);
  207. incr += 4;
  208. } break;
  209. case OPCODE_SET_KEYED: {
  210. text += "set keyed ";
  211. text += DADDR(1);
  212. text += "[";
  213. text += DADDR(2);
  214. text += "] = ";
  215. text += DADDR(3);
  216. incr += 4;
  217. } break;
  218. case OPCODE_SET_KEYED_VALIDATED: {
  219. text += "set keyed validated ";
  220. text += DADDR(1);
  221. text += "[";
  222. text += DADDR(2);
  223. text += "] = ";
  224. text += DADDR(3);
  225. incr += 5;
  226. } break;
  227. case OPCODE_SET_INDEXED_VALIDATED: {
  228. text += "set indexed validated ";
  229. text += DADDR(1);
  230. text += "[";
  231. text += DADDR(2);
  232. text += "] = ";
  233. text += DADDR(3);
  234. incr += 5;
  235. } break;
  236. case OPCODE_GET_KEYED: {
  237. text += "get keyed ";
  238. text += DADDR(3);
  239. text += " = ";
  240. text += DADDR(1);
  241. text += "[";
  242. text += DADDR(2);
  243. text += "]";
  244. incr += 4;
  245. } break;
  246. case OPCODE_GET_KEYED_VALIDATED: {
  247. text += "get keyed validated ";
  248. text += DADDR(3);
  249. text += " = ";
  250. text += DADDR(1);
  251. text += "[";
  252. text += DADDR(2);
  253. text += "]";
  254. incr += 5;
  255. } break;
  256. case OPCODE_GET_INDEXED_VALIDATED: {
  257. text += "get indexed validated ";
  258. text += DADDR(3);
  259. text += " = ";
  260. text += DADDR(1);
  261. text += "[";
  262. text += DADDR(2);
  263. text += "]";
  264. incr += 5;
  265. } break;
  266. case OPCODE_SET_NAMED: {
  267. text += "set_named ";
  268. text += DADDR(1);
  269. text += "[\"";
  270. text += _global_names_ptr[_code_ptr[ip + 3]];
  271. text += "\"] = ";
  272. text += DADDR(2);
  273. incr += 4;
  274. } break;
  275. case OPCODE_SET_NAMED_VALIDATED: {
  276. text += "set_named validated ";
  277. text += DADDR(1);
  278. text += "[\"";
  279. text += setter_names[_code_ptr[ip + 3]];
  280. text += "\"] = ";
  281. text += DADDR(2);
  282. incr += 4;
  283. } break;
  284. case OPCODE_GET_NAMED: {
  285. text += "get_named ";
  286. text += DADDR(2);
  287. text += " = ";
  288. text += DADDR(1);
  289. text += "[\"";
  290. text += _global_names_ptr[_code_ptr[ip + 3]];
  291. text += "\"]";
  292. incr += 4;
  293. } break;
  294. case OPCODE_GET_NAMED_VALIDATED: {
  295. text += "get_named validated ";
  296. text += DADDR(2);
  297. text += " = ";
  298. text += DADDR(1);
  299. text += "[\"";
  300. text += getter_names[_code_ptr[ip + 3]];
  301. text += "\"]";
  302. incr += 4;
  303. } break;
  304. case OPCODE_SET_MEMBER: {
  305. text += "set_member ";
  306. text += "[\"";
  307. text += _global_names_ptr[_code_ptr[ip + 2]];
  308. text += "\"] = ";
  309. text += DADDR(1);
  310. incr += 3;
  311. } break;
  312. case OPCODE_GET_MEMBER: {
  313. text += "get_member ";
  314. text += DADDR(1);
  315. text += " = ";
  316. text += "[\"";
  317. text += _global_names_ptr[_code_ptr[ip + 2]];
  318. text += "\"]";
  319. incr += 3;
  320. } break;
  321. case OPCODE_SET_STATIC_VARIABLE: {
  322. Ref<GDScript> gdscript;
  323. if (_code_ptr[ip + 2] == ADDR_CLASS) {
  324. gdscript = Ref<GDScript>(_script);
  325. } else {
  326. gdscript = get_constant(_code_ptr[ip + 2] & ADDR_MASK);
  327. }
  328. text += "set_static_variable script(";
  329. text += GDScript::debug_get_script_name(gdscript);
  330. text += ")";
  331. if (gdscript.is_valid()) {
  332. text += "[\"" + gdscript->debug_get_static_var_by_index(_code_ptr[ip + 3]) + "\"]";
  333. } else {
  334. text += "[<index " + itos(_code_ptr[ip + 3]) + ">]";
  335. }
  336. text += " = ";
  337. text += DADDR(1);
  338. incr += 4;
  339. } break;
  340. case OPCODE_GET_STATIC_VARIABLE: {
  341. Ref<GDScript> gdscript;
  342. if (_code_ptr[ip + 2] == ADDR_CLASS) {
  343. gdscript = Ref<GDScript>(_script);
  344. } else {
  345. gdscript = get_constant(_code_ptr[ip + 2] & ADDR_MASK);
  346. }
  347. text += "get_static_variable ";
  348. text += DADDR(1);
  349. text += " = script(";
  350. text += GDScript::debug_get_script_name(gdscript);
  351. text += ")";
  352. if (gdscript.is_valid()) {
  353. text += "[\"" + gdscript->debug_get_static_var_by_index(_code_ptr[ip + 3]) + "\"]";
  354. } else {
  355. text += "[<index " + itos(_code_ptr[ip + 3]) + ">]";
  356. }
  357. incr += 4;
  358. } break;
  359. case OPCODE_ASSIGN: {
  360. text += "assign ";
  361. text += DADDR(1);
  362. text += " = ";
  363. text += DADDR(2);
  364. incr += 3;
  365. } break;
  366. case OPCODE_ASSIGN_NULL: {
  367. text += "assign ";
  368. text += DADDR(1);
  369. text += " = null";
  370. incr += 2;
  371. } break;
  372. case OPCODE_ASSIGN_TRUE: {
  373. text += "assign ";
  374. text += DADDR(1);
  375. text += " = true";
  376. incr += 2;
  377. } break;
  378. case OPCODE_ASSIGN_FALSE: {
  379. text += "assign ";
  380. text += DADDR(1);
  381. text += " = false";
  382. incr += 2;
  383. } break;
  384. case OPCODE_ASSIGN_TYPED_BUILTIN: {
  385. text += "assign typed builtin (";
  386. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 3]);
  387. text += ") ";
  388. text += DADDR(1);
  389. text += " = ";
  390. text += DADDR(2);
  391. incr += 4;
  392. } break;
  393. case OPCODE_ASSIGN_TYPED_ARRAY: {
  394. text += "assign typed array ";
  395. text += DADDR(1);
  396. text += " = ";
  397. text += DADDR(2);
  398. incr += 6;
  399. } break;
  400. case OPCODE_ASSIGN_TYPED_DICTIONARY: {
  401. text += "assign typed dictionary ";
  402. text += DADDR(1);
  403. text += " = ";
  404. text += DADDR(2);
  405. incr += 9;
  406. } break;
  407. case OPCODE_ASSIGN_TYPED_NATIVE: {
  408. text += "assign typed native (";
  409. text += DADDR(3);
  410. text += ") ";
  411. text += DADDR(1);
  412. text += " = ";
  413. text += DADDR(2);
  414. incr += 4;
  415. } break;
  416. case OPCODE_ASSIGN_TYPED_SCRIPT: {
  417. Ref<Script> script = get_constant(_code_ptr[ip + 3] & ADDR_MASK);
  418. text += "assign typed script (";
  419. text += GDScript::debug_get_script_name(script);
  420. text += ") ";
  421. text += DADDR(1);
  422. text += " = ";
  423. text += DADDR(2);
  424. incr += 4;
  425. } break;
  426. case OPCODE_CAST_TO_BUILTIN: {
  427. text += "cast builtin ";
  428. text += DADDR(2);
  429. text += " = ";
  430. text += DADDR(1);
  431. text += " as ";
  432. text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 1]));
  433. incr += 4;
  434. } break;
  435. case OPCODE_CAST_TO_NATIVE: {
  436. text += "cast native ";
  437. text += DADDR(2);
  438. text += " = ";
  439. text += DADDR(1);
  440. text += " as ";
  441. text += DADDR(3);
  442. incr += 4;
  443. } break;
  444. case OPCODE_CAST_TO_SCRIPT: {
  445. text += "cast ";
  446. text += DADDR(2);
  447. text += " = ";
  448. text += DADDR(1);
  449. text += " as ";
  450. text += DADDR(3);
  451. incr += 4;
  452. } break;
  453. case OPCODE_CONSTRUCT: {
  454. int instr_var_args = _code_ptr[++ip];
  455. Variant::Type t = Variant::Type(_code_ptr[ip + 3 + instr_var_args]);
  456. int argc = _code_ptr[ip + 1 + instr_var_args];
  457. text += "construct ";
  458. text += DADDR(1 + argc);
  459. text += " = ";
  460. text += Variant::get_type_name(t) + "(";
  461. for (int i = 0; i < argc; i++) {
  462. if (i > 0) {
  463. text += ", ";
  464. }
  465. text += DADDR(i + 1);
  466. }
  467. text += ")";
  468. incr = 3 + instr_var_args;
  469. } break;
  470. case OPCODE_CONSTRUCT_VALIDATED: {
  471. int instr_var_args = _code_ptr[++ip];
  472. int argc = _code_ptr[ip + 1 + instr_var_args];
  473. text += "construct validated ";
  474. text += DADDR(1 + argc);
  475. text += " = ";
  476. text += constructors_names[_code_ptr[ip + 3 + argc]];
  477. text += "(";
  478. for (int i = 0; i < argc; i++) {
  479. if (i > 0) {
  480. text += ", ";
  481. }
  482. text += DADDR(i + 1);
  483. }
  484. text += ")";
  485. incr = 3 + instr_var_args;
  486. } break;
  487. case OPCODE_CONSTRUCT_ARRAY: {
  488. int instr_var_args = _code_ptr[++ip];
  489. int argc = _code_ptr[ip + 1 + instr_var_args];
  490. text += " make_array ";
  491. text += DADDR(1 + argc);
  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 += 3 + argc;
  501. } break;
  502. case OPCODE_CONSTRUCT_TYPED_ARRAY: {
  503. int instr_var_args = _code_ptr[++ip];
  504. int argc = _code_ptr[ip + 1 + instr_var_args];
  505. Ref<Script> script_type = get_constant(_code_ptr[ip + argc + 2] & ADDR_MASK);
  506. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + argc + 4];
  507. StringName native_type = get_global_name(_code_ptr[ip + argc + 5]);
  508. String type_name;
  509. if (script_type.is_valid() && script_type->is_valid()) {
  510. type_name = "script(" + GDScript::debug_get_script_name(script_type) + ")";
  511. } else if (native_type != StringName()) {
  512. type_name = native_type;
  513. } else {
  514. type_name = Variant::get_type_name(builtin_type);
  515. }
  516. text += " make_typed_array (";
  517. text += type_name;
  518. text += ") ";
  519. text += DADDR(1 + argc);
  520. text += " = [";
  521. for (int i = 0; i < argc; i++) {
  522. if (i > 0) {
  523. text += ", ";
  524. }
  525. text += DADDR(1 + i);
  526. }
  527. text += "]";
  528. incr += 6 + argc;
  529. } break;
  530. case OPCODE_CONSTRUCT_DICTIONARY: {
  531. int instr_var_args = _code_ptr[++ip];
  532. int argc = _code_ptr[ip + 1 + instr_var_args];
  533. text += "make_dict ";
  534. text += DADDR(1 + argc * 2);
  535. text += " = {";
  536. for (int i = 0; i < argc; i++) {
  537. if (i > 0) {
  538. text += ", ";
  539. }
  540. text += DADDR(1 + i * 2 + 0);
  541. text += ": ";
  542. text += DADDR(1 + i * 2 + 1);
  543. }
  544. text += "}";
  545. incr += 3 + argc * 2;
  546. } break;
  547. case OPCODE_CONSTRUCT_TYPED_DICTIONARY: {
  548. int instr_var_args = _code_ptr[++ip];
  549. int argc = _code_ptr[ip + 1 + instr_var_args];
  550. Ref<Script> key_script_type = get_constant(_code_ptr[ip + argc * 2 + 2] & ADDR_MASK);
  551. Variant::Type key_builtin_type = (Variant::Type)_code_ptr[ip + argc * 2 + 5];
  552. StringName key_native_type = get_global_name(_code_ptr[ip + argc * 2 + 6]);
  553. String key_type_name;
  554. if (key_script_type.is_valid() && key_script_type->is_valid()) {
  555. key_type_name = "script(" + GDScript::debug_get_script_name(key_script_type) + ")";
  556. } else if (key_native_type != StringName()) {
  557. key_type_name = key_native_type;
  558. } else {
  559. key_type_name = Variant::get_type_name(key_builtin_type);
  560. }
  561. Ref<Script> value_script_type = get_constant(_code_ptr[ip + argc * 2 + 3] & ADDR_MASK);
  562. Variant::Type value_builtin_type = (Variant::Type)_code_ptr[ip + argc * 2 + 7];
  563. StringName value_native_type = get_global_name(_code_ptr[ip + argc * 2 + 8]);
  564. String value_type_name;
  565. if (value_script_type.is_valid() && value_script_type->is_valid()) {
  566. value_type_name = "script(" + GDScript::debug_get_script_name(value_script_type) + ")";
  567. } else if (value_native_type != StringName()) {
  568. value_type_name = value_native_type;
  569. } else {
  570. value_type_name = Variant::get_type_name(value_builtin_type);
  571. }
  572. text += "make_typed_dict (";
  573. text += key_type_name;
  574. text += ", ";
  575. text += value_type_name;
  576. text += ") ";
  577. text += DADDR(1 + argc * 2);
  578. text += " = {";
  579. for (int i = 0; i < argc; i++) {
  580. if (i > 0) {
  581. text += ", ";
  582. }
  583. text += DADDR(1 + i * 2 + 0);
  584. text += ": ";
  585. text += DADDR(1 + i * 2 + 1);
  586. }
  587. text += "}";
  588. incr += 9 + argc * 2;
  589. } break;
  590. case OPCODE_CALL:
  591. case OPCODE_CALL_RETURN:
  592. case OPCODE_CALL_ASYNC: {
  593. bool ret = (_code_ptr[ip]) == OPCODE_CALL_RETURN;
  594. bool async = (_code_ptr[ip]) == OPCODE_CALL_ASYNC;
  595. int instr_var_args = _code_ptr[++ip];
  596. if (ret) {
  597. text += "call-ret ";
  598. } else if (async) {
  599. text += "call-async ";
  600. } else {
  601. text += "call ";
  602. }
  603. int argc = _code_ptr[ip + 1 + instr_var_args];
  604. if (ret || async) {
  605. text += DADDR(2 + argc) + " = ";
  606. }
  607. text += DADDR(1 + argc) + ".";
  608. text += String(_global_names_ptr[_code_ptr[ip + 2 + instr_var_args]]);
  609. text += "(";
  610. for (int i = 0; i < argc; i++) {
  611. if (i > 0) {
  612. text += ", ";
  613. }
  614. text += DADDR(1 + i);
  615. }
  616. text += ")";
  617. incr = 5 + argc;
  618. } break;
  619. case OPCODE_CALL_METHOD_BIND:
  620. case OPCODE_CALL_METHOD_BIND_RET: {
  621. bool ret = (_code_ptr[ip]) == OPCODE_CALL_METHOD_BIND_RET;
  622. int instr_var_args = _code_ptr[++ip];
  623. if (ret) {
  624. text += "call-method_bind-ret ";
  625. } else {
  626. text += "call-method_bind ";
  627. }
  628. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  629. int argc = _code_ptr[ip + 1 + instr_var_args];
  630. if (ret) {
  631. text += DADDR(2 + argc) + " = ";
  632. }
  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. }
  640. text += DADDR(1 + i);
  641. }
  642. text += ")";
  643. incr = 5 + argc;
  644. } break;
  645. case OPCODE_CALL_BUILTIN_STATIC: {
  646. int instr_var_args = _code_ptr[++ip];
  647. Variant::Type type = (Variant::Type)_code_ptr[ip + 1 + instr_var_args];
  648. int argc = _code_ptr[ip + 3 + instr_var_args];
  649. text += "call built-in method static ";
  650. text += DADDR(1 + argc);
  651. text += " = ";
  652. text += Variant::get_type_name(type);
  653. text += ".";
  654. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]].operator String();
  655. text += "(";
  656. for (int i = 0; i < argc; i++) {
  657. if (i > 0) {
  658. text += ", ";
  659. }
  660. text += DADDR(1 + i);
  661. }
  662. text += ")";
  663. incr += 5 + argc;
  664. } break;
  665. case OPCODE_CALL_NATIVE_STATIC: {
  666. int instr_var_args = _code_ptr[++ip];
  667. MethodBind *method = _methods_ptr[_code_ptr[ip + 1 + instr_var_args]];
  668. int argc = _code_ptr[ip + 2 + instr_var_args];
  669. text += "call native method static ";
  670. text += DADDR(1 + argc);
  671. text += " = ";
  672. text += method->get_instance_class();
  673. text += ".";
  674. text += method->get_name();
  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_NATIVE_STATIC_VALIDATED_RETURN: {
  686. int instr_var_args = _code_ptr[++ip];
  687. text += "call native static method validated (return) ";
  688. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  689. int argc = _code_ptr[ip + 1 + instr_var_args];
  690. text += DADDR(1 + argc) + " = ";
  691. text += method->get_instance_class();
  692. text += ".";
  693. text += method->get_name();
  694. text += "(";
  695. for (int i = 0; i < argc; i++) {
  696. if (i > 0) {
  697. text += ", ";
  698. }
  699. text += DADDR(1 + i);
  700. }
  701. text += ")";
  702. incr = 4 + argc;
  703. } break;
  704. case OPCODE_CALL_NATIVE_STATIC_VALIDATED_NO_RETURN: {
  705. int instr_var_args = _code_ptr[++ip];
  706. text += "call native static method validated (no return) ";
  707. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  708. int argc = _code_ptr[ip + 1 + instr_var_args];
  709. text += method->get_instance_class();
  710. text += ".";
  711. text += method->get_name();
  712. text += "(";
  713. for (int i = 0; i < argc; i++) {
  714. if (i > 0) {
  715. text += ", ";
  716. }
  717. text += DADDR(1 + i);
  718. }
  719. text += ")";
  720. incr = 4 + argc;
  721. } break;
  722. case OPCODE_CALL_METHOD_BIND_VALIDATED_RETURN: {
  723. int instr_var_args = _code_ptr[++ip];
  724. text += "call method-bind validated (return) ";
  725. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  726. int argc = _code_ptr[ip + 1 + instr_var_args];
  727. text += DADDR(2 + argc) + " = ";
  728. text += DADDR(1 + argc) + ".";
  729. text += method->get_name();
  730. text += "(";
  731. for (int i = 0; i < argc; i++) {
  732. if (i > 0) {
  733. text += ", ";
  734. }
  735. text += DADDR(1 + i);
  736. }
  737. text += ")";
  738. incr = 5 + argc;
  739. } break;
  740. case OPCODE_CALL_METHOD_BIND_VALIDATED_NO_RETURN: {
  741. int instr_var_args = _code_ptr[++ip];
  742. text += "call method-bind validated (no return) ";
  743. MethodBind *method = _methods_ptr[_code_ptr[ip + 2 + instr_var_args]];
  744. int argc = _code_ptr[ip + 1 + instr_var_args];
  745. text += DADDR(1 + argc) + ".";
  746. text += method->get_name();
  747. text += "(";
  748. for (int i = 0; i < argc; i++) {
  749. if (i > 0) {
  750. text += ", ";
  751. }
  752. text += DADDR(1 + i);
  753. }
  754. text += ")";
  755. incr = 5 + argc;
  756. } break;
  757. case OPCODE_CALL_BUILTIN_TYPE_VALIDATED: {
  758. int instr_var_args = _code_ptr[++ip];
  759. int argc = _code_ptr[ip + 1 + instr_var_args];
  760. text += "call-builtin-method validated ";
  761. text += DADDR(2 + argc) + " = ";
  762. text += DADDR(1) + ".";
  763. text += builtin_methods_names[_code_ptr[ip + 4 + argc]];
  764. text += "(";
  765. for (int i = 0; i < argc; i++) {
  766. if (i > 0) {
  767. text += ", ";
  768. }
  769. text += DADDR(1 + i);
  770. }
  771. text += ")";
  772. incr = 5 + argc;
  773. } break;
  774. case OPCODE_CALL_UTILITY: {
  775. int instr_var_args = _code_ptr[++ip];
  776. text += "call-utility ";
  777. int argc = _code_ptr[ip + 1 + instr_var_args];
  778. text += DADDR(1 + argc) + " = ";
  779. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  780. text += "(";
  781. for (int i = 0; i < argc; i++) {
  782. if (i > 0) {
  783. text += ", ";
  784. }
  785. text += DADDR(1 + i);
  786. }
  787. text += ")";
  788. incr = 4 + argc;
  789. } break;
  790. case OPCODE_CALL_UTILITY_VALIDATED: {
  791. int instr_var_args = _code_ptr[++ip];
  792. text += "call-utility validated ";
  793. int argc = _code_ptr[ip + 1 + instr_var_args];
  794. text += DADDR(1 + argc) + " = ";
  795. text += utilities_names[_code_ptr[ip + 3 + argc]];
  796. text += "(";
  797. for (int i = 0; i < argc; i++) {
  798. if (i > 0) {
  799. text += ", ";
  800. }
  801. text += DADDR(1 + i);
  802. }
  803. text += ")";
  804. incr = 4 + argc;
  805. } break;
  806. case OPCODE_CALL_GDSCRIPT_UTILITY: {
  807. int instr_var_args = _code_ptr[++ip];
  808. text += "call-gdscript-utility ";
  809. int argc = _code_ptr[ip + 1 + instr_var_args];
  810. text += DADDR(1 + argc) + " = ";
  811. text += gds_utilities_names[_code_ptr[ip + 3 + argc]];
  812. text += "(";
  813. for (int i = 0; i < argc; i++) {
  814. if (i > 0) {
  815. text += ", ";
  816. }
  817. text += DADDR(1 + i);
  818. }
  819. text += ")";
  820. incr = 4 + argc;
  821. } break;
  822. case OPCODE_CALL_SELF_BASE: {
  823. int instr_var_args = _code_ptr[++ip];
  824. text += "call-self-base ";
  825. int argc = _code_ptr[ip + 1 + instr_var_args];
  826. text += DADDR(2 + argc) + " = ";
  827. text += _global_names_ptr[_code_ptr[ip + 2 + instr_var_args]];
  828. text += "(";
  829. for (int i = 0; i < argc; i++) {
  830. if (i > 0) {
  831. text += ", ";
  832. }
  833. text += DADDR(1 + i);
  834. }
  835. text += ")";
  836. incr = 4 + argc;
  837. } break;
  838. case OPCODE_AWAIT: {
  839. text += "await ";
  840. text += DADDR(1);
  841. incr = 2;
  842. } break;
  843. case OPCODE_AWAIT_RESUME: {
  844. text += "await resume ";
  845. text += DADDR(1);
  846. incr = 2;
  847. } break;
  848. case OPCODE_CREATE_LAMBDA: {
  849. int instr_var_args = _code_ptr[++ip];
  850. int captures_count = _code_ptr[ip + 1 + instr_var_args];
  851. GDScriptFunction *lambda = _lambdas_ptr[_code_ptr[ip + 2 + instr_var_args]];
  852. text += DADDR(1 + captures_count);
  853. text += "create lambda from ";
  854. text += lambda->name.operator String();
  855. text += "function, captures (";
  856. for (int i = 0; i < captures_count; i++) {
  857. if (i > 0) {
  858. text += ", ";
  859. }
  860. text += DADDR(1 + i);
  861. }
  862. text += ")";
  863. incr = 4 + captures_count;
  864. } break;
  865. case OPCODE_CREATE_SELF_LAMBDA: {
  866. int instr_var_args = _code_ptr[++ip];
  867. int captures_count = _code_ptr[ip + 1 + instr_var_args];
  868. GDScriptFunction *lambda = _lambdas_ptr[_code_ptr[ip + 2 + instr_var_args]];
  869. text += DADDR(1 + captures_count);
  870. text += "create self lambda from ";
  871. text += lambda->name.operator String();
  872. text += "function, captures (";
  873. for (int i = 0; i < captures_count; i++) {
  874. if (i > 0) {
  875. text += ", ";
  876. }
  877. text += DADDR(1 + i);
  878. }
  879. text += ")";
  880. incr = 4 + captures_count;
  881. } break;
  882. case OPCODE_JUMP: {
  883. text += "jump ";
  884. text += itos(_code_ptr[ip + 1]);
  885. incr = 2;
  886. } break;
  887. case OPCODE_JUMP_IF: {
  888. text += "jump-if ";
  889. text += DADDR(1);
  890. text += " to ";
  891. text += itos(_code_ptr[ip + 2]);
  892. incr = 3;
  893. } break;
  894. case OPCODE_JUMP_IF_NOT: {
  895. text += "jump-if-not ";
  896. text += DADDR(1);
  897. text += " to ";
  898. text += itos(_code_ptr[ip + 2]);
  899. incr = 3;
  900. } break;
  901. case OPCODE_JUMP_TO_DEF_ARGUMENT: {
  902. text += "jump-to-default-argument ";
  903. incr = 1;
  904. } break;
  905. case OPCODE_JUMP_IF_SHARED: {
  906. text += "jump-if-shared ";
  907. text += DADDR(1);
  908. text += " to ";
  909. text += itos(_code_ptr[ip + 2]);
  910. incr = 3;
  911. } break;
  912. case OPCODE_RETURN: {
  913. text += "return ";
  914. text += DADDR(1);
  915. incr = 2;
  916. } break;
  917. case OPCODE_RETURN_TYPED_BUILTIN: {
  918. text += "return typed builtin (";
  919. text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 2]);
  920. text += ") ";
  921. text += DADDR(1);
  922. incr += 3;
  923. } break;
  924. case OPCODE_RETURN_TYPED_ARRAY: {
  925. text += "return typed array ";
  926. text += DADDR(1);
  927. incr += 5;
  928. } break;
  929. case OPCODE_RETURN_TYPED_DICTIONARY: {
  930. text += "return typed dictionary ";
  931. text += DADDR(1);
  932. incr += 8;
  933. } break;
  934. case OPCODE_RETURN_TYPED_NATIVE: {
  935. text += "return typed native (";
  936. text += DADDR(2);
  937. text += ") ";
  938. text += DADDR(1);
  939. incr += 3;
  940. } break;
  941. case OPCODE_RETURN_TYPED_SCRIPT: {
  942. Ref<Script> script = get_constant(_code_ptr[ip + 2] & ADDR_MASK);
  943. text += "return typed script (";
  944. text += GDScript::debug_get_script_name(script);
  945. text += ") ";
  946. text += DADDR(1);
  947. incr += 3;
  948. } break;
  949. #define DISASSEMBLE_ITERATE(m_type) \
  950. case OPCODE_ITERATE_##m_type: { \
  951. text += "for-loop (typed "; \
  952. text += #m_type; \
  953. text += ") "; \
  954. text += DADDR(3); \
  955. text += " in "; \
  956. text += DADDR(2); \
  957. text += " counter "; \
  958. text += DADDR(1); \
  959. text += " end "; \
  960. text += itos(_code_ptr[ip + 4]); \
  961. incr += 5; \
  962. } break
  963. #define DISASSEMBLE_ITERATE_BEGIN(m_type) \
  964. case OPCODE_ITERATE_BEGIN_##m_type: { \
  965. text += "for-init (typed "; \
  966. text += #m_type; \
  967. text += ") "; \
  968. text += DADDR(3); \
  969. text += " in "; \
  970. text += DADDR(2); \
  971. text += " counter "; \
  972. text += DADDR(1); \
  973. text += " end "; \
  974. text += itos(_code_ptr[ip + 4]); \
  975. incr += 5; \
  976. } break
  977. #define DISASSEMBLE_ITERATE_TYPES(m_macro) \
  978. m_macro(INT); \
  979. m_macro(FLOAT); \
  980. m_macro(VECTOR2); \
  981. m_macro(VECTOR2I); \
  982. m_macro(VECTOR3); \
  983. m_macro(VECTOR3I); \
  984. m_macro(STRING); \
  985. m_macro(DICTIONARY); \
  986. m_macro(ARRAY); \
  987. m_macro(PACKED_BYTE_ARRAY); \
  988. m_macro(PACKED_INT32_ARRAY); \
  989. m_macro(PACKED_INT64_ARRAY); \
  990. m_macro(PACKED_FLOAT32_ARRAY); \
  991. m_macro(PACKED_FLOAT64_ARRAY); \
  992. m_macro(PACKED_STRING_ARRAY); \
  993. m_macro(PACKED_VECTOR2_ARRAY); \
  994. m_macro(PACKED_VECTOR3_ARRAY); \
  995. m_macro(PACKED_COLOR_ARRAY); \
  996. m_macro(PACKED_VECTOR4_ARRAY); \
  997. m_macro(OBJECT)
  998. case OPCODE_ITERATE_BEGIN: {
  999. text += "for-init ";
  1000. text += DADDR(3);
  1001. text += " in ";
  1002. text += DADDR(2);
  1003. text += " counter ";
  1004. text += DADDR(1);
  1005. text += " end ";
  1006. text += itos(_code_ptr[ip + 4]);
  1007. incr += 5;
  1008. } break;
  1009. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE_BEGIN);
  1010. case OPCODE_ITERATE: {
  1011. text += "for-loop ";
  1012. text += DADDR(2);
  1013. text += " in ";
  1014. text += DADDR(2);
  1015. text += " counter ";
  1016. text += DADDR(1);
  1017. text += " end ";
  1018. text += itos(_code_ptr[ip + 4]);
  1019. incr += 5;
  1020. } break;
  1021. DISASSEMBLE_ITERATE_TYPES(DISASSEMBLE_ITERATE);
  1022. case OPCODE_STORE_GLOBAL: {
  1023. text += "store global ";
  1024. text += DADDR(1);
  1025. text += " = ";
  1026. text += String::num_int64(_code_ptr[ip + 2]);
  1027. incr += 3;
  1028. } break;
  1029. case OPCODE_STORE_NAMED_GLOBAL: {
  1030. text += "store named global ";
  1031. text += DADDR(1);
  1032. text += " = ";
  1033. text += String(_global_names_ptr[_code_ptr[ip + 2]]);
  1034. incr += 3;
  1035. } break;
  1036. case OPCODE_LINE: {
  1037. int line = _code_ptr[ip + 1] - 1;
  1038. if (line >= 0 && line < p_code_lines.size()) {
  1039. text += "line ";
  1040. text += itos(line + 1);
  1041. text += ": ";
  1042. text += p_code_lines[line];
  1043. } else {
  1044. text += "";
  1045. }
  1046. incr += 2;
  1047. } break;
  1048. #define DISASSEMBLE_TYPE_ADJUST(m_v_type) \
  1049. case OPCODE_TYPE_ADJUST_##m_v_type: { \
  1050. text += "type adjust ("; \
  1051. text += #m_v_type; \
  1052. text += ") "; \
  1053. text += DADDR(1); \
  1054. incr += 2; \
  1055. } break
  1056. DISASSEMBLE_TYPE_ADJUST(BOOL);
  1057. DISASSEMBLE_TYPE_ADJUST(INT);
  1058. DISASSEMBLE_TYPE_ADJUST(FLOAT);
  1059. DISASSEMBLE_TYPE_ADJUST(STRING);
  1060. DISASSEMBLE_TYPE_ADJUST(VECTOR2);
  1061. DISASSEMBLE_TYPE_ADJUST(VECTOR2I);
  1062. DISASSEMBLE_TYPE_ADJUST(RECT2);
  1063. DISASSEMBLE_TYPE_ADJUST(RECT2I);
  1064. DISASSEMBLE_TYPE_ADJUST(VECTOR3);
  1065. DISASSEMBLE_TYPE_ADJUST(VECTOR3I);
  1066. DISASSEMBLE_TYPE_ADJUST(TRANSFORM2D);
  1067. DISASSEMBLE_TYPE_ADJUST(VECTOR4);
  1068. DISASSEMBLE_TYPE_ADJUST(VECTOR4I);
  1069. DISASSEMBLE_TYPE_ADJUST(PLANE);
  1070. DISASSEMBLE_TYPE_ADJUST(QUATERNION);
  1071. DISASSEMBLE_TYPE_ADJUST(AABB);
  1072. DISASSEMBLE_TYPE_ADJUST(BASIS);
  1073. DISASSEMBLE_TYPE_ADJUST(TRANSFORM3D);
  1074. DISASSEMBLE_TYPE_ADJUST(PROJECTION);
  1075. DISASSEMBLE_TYPE_ADJUST(COLOR);
  1076. DISASSEMBLE_TYPE_ADJUST(STRING_NAME);
  1077. DISASSEMBLE_TYPE_ADJUST(NODE_PATH);
  1078. DISASSEMBLE_TYPE_ADJUST(RID);
  1079. DISASSEMBLE_TYPE_ADJUST(OBJECT);
  1080. DISASSEMBLE_TYPE_ADJUST(CALLABLE);
  1081. DISASSEMBLE_TYPE_ADJUST(SIGNAL);
  1082. DISASSEMBLE_TYPE_ADJUST(DICTIONARY);
  1083. DISASSEMBLE_TYPE_ADJUST(ARRAY);
  1084. DISASSEMBLE_TYPE_ADJUST(PACKED_BYTE_ARRAY);
  1085. DISASSEMBLE_TYPE_ADJUST(PACKED_INT32_ARRAY);
  1086. DISASSEMBLE_TYPE_ADJUST(PACKED_INT64_ARRAY);
  1087. DISASSEMBLE_TYPE_ADJUST(PACKED_FLOAT32_ARRAY);
  1088. DISASSEMBLE_TYPE_ADJUST(PACKED_FLOAT64_ARRAY);
  1089. DISASSEMBLE_TYPE_ADJUST(PACKED_STRING_ARRAY);
  1090. DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR2_ARRAY);
  1091. DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR3_ARRAY);
  1092. DISASSEMBLE_TYPE_ADJUST(PACKED_COLOR_ARRAY);
  1093. DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR4_ARRAY);
  1094. case OPCODE_ASSERT: {
  1095. text += "assert (";
  1096. text += DADDR(1);
  1097. text += ", ";
  1098. text += DADDR(2);
  1099. text += ")";
  1100. incr += 3;
  1101. } break;
  1102. case OPCODE_BREAKPOINT: {
  1103. text += "breakpoint";
  1104. incr += 1;
  1105. } break;
  1106. case OPCODE_END: {
  1107. text += "== END ==";
  1108. incr += 1;
  1109. } break;
  1110. }
  1111. ip += incr;
  1112. if (text.get_string_length() > 0) {
  1113. print_line(text.as_string());
  1114. }
  1115. }
  1116. }
  1117. #endif // DEBUG_ENABLED