2
0

gdscript_byte_codegen.cpp 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  1. /**************************************************************************/
  2. /* gdscript_byte_codegen.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. #include "gdscript_byte_codegen.h"
  31. #include "core/debugger/engine_debugger.h"
  32. #include "gdscript.h"
  33. uint32_t GDScriptByteCodeGenerator::add_parameter(const StringName &p_name, bool p_is_optional, const GDScriptDataType &p_type) {
  34. #ifdef TOOLS_ENABLED
  35. function->arg_names.push_back(p_name);
  36. #endif
  37. function->_argument_count++;
  38. function->argument_types.push_back(p_type);
  39. if (p_is_optional) {
  40. function->_default_arg_count++;
  41. }
  42. return add_local(p_name, p_type);
  43. }
  44. uint32_t GDScriptByteCodeGenerator::add_local(const StringName &p_name, const GDScriptDataType &p_type) {
  45. int stack_pos = locals.size() + RESERVED_STACK;
  46. locals.push_back(StackSlot(p_type.builtin_type));
  47. add_stack_identifier(p_name, stack_pos);
  48. return stack_pos;
  49. }
  50. uint32_t GDScriptByteCodeGenerator::add_local_constant(const StringName &p_name, const Variant &p_constant) {
  51. int index = add_or_get_constant(p_constant);
  52. local_constants[p_name] = index;
  53. return index;
  54. }
  55. uint32_t GDScriptByteCodeGenerator::add_or_get_constant(const Variant &p_constant) {
  56. return get_constant_pos(p_constant);
  57. }
  58. uint32_t GDScriptByteCodeGenerator::add_or_get_name(const StringName &p_name) {
  59. return get_name_map_pos(p_name);
  60. }
  61. uint32_t GDScriptByteCodeGenerator::add_temporary(const GDScriptDataType &p_type) {
  62. Variant::Type temp_type = Variant::NIL;
  63. if (p_type.has_type) {
  64. if (p_type.kind == GDScriptDataType::BUILTIN) {
  65. switch (p_type.builtin_type) {
  66. case Variant::NIL:
  67. case Variant::BOOL:
  68. case Variant::INT:
  69. case Variant::FLOAT:
  70. case Variant::STRING:
  71. case Variant::VECTOR2:
  72. case Variant::VECTOR2I:
  73. case Variant::RECT2:
  74. case Variant::RECT2I:
  75. case Variant::VECTOR3:
  76. case Variant::VECTOR3I:
  77. case Variant::TRANSFORM2D:
  78. case Variant::VECTOR4:
  79. case Variant::VECTOR4I:
  80. case Variant::PLANE:
  81. case Variant::QUATERNION:
  82. case Variant::AABB:
  83. case Variant::BASIS:
  84. case Variant::TRANSFORM3D:
  85. case Variant::PROJECTION:
  86. case Variant::COLOR:
  87. case Variant::STRING_NAME:
  88. case Variant::NODE_PATH:
  89. case Variant::RID:
  90. case Variant::OBJECT:
  91. case Variant::CALLABLE:
  92. case Variant::SIGNAL:
  93. case Variant::DICTIONARY:
  94. case Variant::ARRAY:
  95. temp_type = p_type.builtin_type;
  96. break;
  97. case Variant::PACKED_BYTE_ARRAY:
  98. case Variant::PACKED_INT32_ARRAY:
  99. case Variant::PACKED_INT64_ARRAY:
  100. case Variant::PACKED_FLOAT32_ARRAY:
  101. case Variant::PACKED_FLOAT64_ARRAY:
  102. case Variant::PACKED_STRING_ARRAY:
  103. case Variant::PACKED_VECTOR2_ARRAY:
  104. case Variant::PACKED_VECTOR3_ARRAY:
  105. case Variant::PACKED_COLOR_ARRAY:
  106. case Variant::VARIANT_MAX:
  107. // Packed arrays are reference counted, so we don't use the pool for them.
  108. temp_type = Variant::NIL;
  109. break;
  110. }
  111. } else {
  112. temp_type = Variant::OBJECT;
  113. }
  114. }
  115. if (!temporaries_pool.has(temp_type)) {
  116. temporaries_pool[temp_type] = List<int>();
  117. }
  118. List<int> &pool = temporaries_pool[temp_type];
  119. if (pool.is_empty()) {
  120. StackSlot new_temp(temp_type);
  121. int idx = temporaries.size();
  122. pool.push_back(idx);
  123. temporaries.push_back(new_temp);
  124. }
  125. int slot = pool.front()->get();
  126. pool.pop_front();
  127. used_temporaries.push_back(slot);
  128. return slot;
  129. }
  130. void GDScriptByteCodeGenerator::pop_temporary() {
  131. ERR_FAIL_COND(used_temporaries.is_empty());
  132. int slot_idx = used_temporaries.back()->get();
  133. const StackSlot &slot = temporaries[slot_idx];
  134. if (slot.type == Variant::OBJECT) {
  135. // Avoid keeping in the stack long-lived references to objects,
  136. // which may prevent RefCounted objects from being freed.
  137. write_assign_false(Address(Address::TEMPORARY, slot_idx));
  138. }
  139. temporaries_pool[slot.type].push_back(slot_idx);
  140. used_temporaries.pop_back();
  141. }
  142. void GDScriptByteCodeGenerator::start_parameters() {
  143. if (function->_default_arg_count > 0) {
  144. append(GDScriptFunction::OPCODE_JUMP_TO_DEF_ARGUMENT);
  145. function->default_arguments.push_back(opcodes.size());
  146. }
  147. }
  148. void GDScriptByteCodeGenerator::end_parameters() {
  149. function->default_arguments.reverse();
  150. }
  151. void GDScriptByteCodeGenerator::write_start(GDScript *p_script, const StringName &p_function_name, bool p_static, Variant p_rpc_config, const GDScriptDataType &p_return_type) {
  152. function = memnew(GDScriptFunction);
  153. debug_stack = EngineDebugger::is_active();
  154. function->name = p_function_name;
  155. function->_script = p_script;
  156. function->source = p_script->get_script_path();
  157. #ifdef DEBUG_ENABLED
  158. function->func_cname = (String(function->source) + " - " + String(p_function_name)).utf8();
  159. function->_func_cname = function->func_cname.get_data();
  160. #endif
  161. function->_static = p_static;
  162. function->return_type = p_return_type;
  163. function->rpc_config = p_rpc_config;
  164. function->_argument_count = 0;
  165. }
  166. GDScriptFunction *GDScriptByteCodeGenerator::write_end() {
  167. #ifdef DEBUG_ENABLED
  168. if (!used_temporaries.is_empty()) {
  169. ERR_PRINT("Non-zero temporary variables at end of function: " + itos(used_temporaries.size()));
  170. }
  171. #endif
  172. append_opcode(GDScriptFunction::OPCODE_END);
  173. for (int i = 0; i < temporaries.size(); i++) {
  174. int stack_index = i + max_locals + RESERVED_STACK;
  175. for (int j = 0; j < temporaries[i].bytecode_indices.size(); j++) {
  176. opcodes.write[temporaries[i].bytecode_indices[j]] = stack_index | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  177. }
  178. if (temporaries[i].type != Variant::NIL) {
  179. function->temporary_slots[stack_index] = temporaries[i].type;
  180. }
  181. }
  182. if (constant_map.size()) {
  183. function->_constant_count = constant_map.size();
  184. function->constants.resize(constant_map.size());
  185. function->_constants_ptr = function->constants.ptrw();
  186. for (const KeyValue<Variant, int> &K : constant_map) {
  187. function->constants.write[K.value] = K.key;
  188. }
  189. } else {
  190. function->_constants_ptr = nullptr;
  191. function->_constant_count = 0;
  192. }
  193. if (name_map.size()) {
  194. function->global_names.resize(name_map.size());
  195. function->_global_names_ptr = &function->global_names[0];
  196. for (const KeyValue<StringName, int> &E : name_map) {
  197. function->global_names.write[E.value] = E.key;
  198. }
  199. function->_global_names_count = function->global_names.size();
  200. } else {
  201. function->_global_names_ptr = nullptr;
  202. function->_global_names_count = 0;
  203. }
  204. if (opcodes.size()) {
  205. function->code = opcodes;
  206. function->_code_ptr = &function->code[0];
  207. function->_code_size = opcodes.size();
  208. } else {
  209. function->_code_ptr = nullptr;
  210. function->_code_size = 0;
  211. }
  212. if (function->default_arguments.size()) {
  213. function->_default_arg_count = function->default_arguments.size() - 1;
  214. function->_default_arg_ptr = &function->default_arguments[0];
  215. } else {
  216. function->_default_arg_count = 0;
  217. function->_default_arg_ptr = nullptr;
  218. }
  219. if (operator_func_map.size()) {
  220. function->operator_funcs.resize(operator_func_map.size());
  221. function->_operator_funcs_count = function->operator_funcs.size();
  222. function->_operator_funcs_ptr = function->operator_funcs.ptr();
  223. for (const KeyValue<Variant::ValidatedOperatorEvaluator, int> &E : operator_func_map) {
  224. function->operator_funcs.write[E.value] = E.key;
  225. }
  226. } else {
  227. function->_operator_funcs_count = 0;
  228. function->_operator_funcs_ptr = nullptr;
  229. }
  230. if (setters_map.size()) {
  231. function->setters.resize(setters_map.size());
  232. function->_setters_count = function->setters.size();
  233. function->_setters_ptr = function->setters.ptr();
  234. for (const KeyValue<Variant::ValidatedSetter, int> &E : setters_map) {
  235. function->setters.write[E.value] = E.key;
  236. }
  237. } else {
  238. function->_setters_count = 0;
  239. function->_setters_ptr = nullptr;
  240. }
  241. if (getters_map.size()) {
  242. function->getters.resize(getters_map.size());
  243. function->_getters_count = function->getters.size();
  244. function->_getters_ptr = function->getters.ptr();
  245. for (const KeyValue<Variant::ValidatedGetter, int> &E : getters_map) {
  246. function->getters.write[E.value] = E.key;
  247. }
  248. } else {
  249. function->_getters_count = 0;
  250. function->_getters_ptr = nullptr;
  251. }
  252. if (keyed_setters_map.size()) {
  253. function->keyed_setters.resize(keyed_setters_map.size());
  254. function->_keyed_setters_count = function->keyed_setters.size();
  255. function->_keyed_setters_ptr = function->keyed_setters.ptr();
  256. for (const KeyValue<Variant::ValidatedKeyedSetter, int> &E : keyed_setters_map) {
  257. function->keyed_setters.write[E.value] = E.key;
  258. }
  259. } else {
  260. function->_keyed_setters_count = 0;
  261. function->_keyed_setters_ptr = nullptr;
  262. }
  263. if (keyed_getters_map.size()) {
  264. function->keyed_getters.resize(keyed_getters_map.size());
  265. function->_keyed_getters_count = function->keyed_getters.size();
  266. function->_keyed_getters_ptr = function->keyed_getters.ptr();
  267. for (const KeyValue<Variant::ValidatedKeyedGetter, int> &E : keyed_getters_map) {
  268. function->keyed_getters.write[E.value] = E.key;
  269. }
  270. } else {
  271. function->_keyed_getters_count = 0;
  272. function->_keyed_getters_ptr = nullptr;
  273. }
  274. if (indexed_setters_map.size()) {
  275. function->indexed_setters.resize(indexed_setters_map.size());
  276. function->_indexed_setters_count = function->indexed_setters.size();
  277. function->_indexed_setters_ptr = function->indexed_setters.ptr();
  278. for (const KeyValue<Variant::ValidatedIndexedSetter, int> &E : indexed_setters_map) {
  279. function->indexed_setters.write[E.value] = E.key;
  280. }
  281. } else {
  282. function->_indexed_setters_count = 0;
  283. function->_indexed_setters_ptr = nullptr;
  284. }
  285. if (indexed_getters_map.size()) {
  286. function->indexed_getters.resize(indexed_getters_map.size());
  287. function->_indexed_getters_count = function->indexed_getters.size();
  288. function->_indexed_getters_ptr = function->indexed_getters.ptr();
  289. for (const KeyValue<Variant::ValidatedIndexedGetter, int> &E : indexed_getters_map) {
  290. function->indexed_getters.write[E.value] = E.key;
  291. }
  292. } else {
  293. function->_indexed_getters_count = 0;
  294. function->_indexed_getters_ptr = nullptr;
  295. }
  296. if (builtin_method_map.size()) {
  297. function->builtin_methods.resize(builtin_method_map.size());
  298. function->_builtin_methods_ptr = function->builtin_methods.ptr();
  299. function->_builtin_methods_count = builtin_method_map.size();
  300. for (const KeyValue<Variant::ValidatedBuiltInMethod, int> &E : builtin_method_map) {
  301. function->builtin_methods.write[E.value] = E.key;
  302. }
  303. } else {
  304. function->_builtin_methods_ptr = nullptr;
  305. function->_builtin_methods_count = 0;
  306. }
  307. if (constructors_map.size()) {
  308. function->constructors.resize(constructors_map.size());
  309. function->_constructors_ptr = function->constructors.ptr();
  310. function->_constructors_count = constructors_map.size();
  311. for (const KeyValue<Variant::ValidatedConstructor, int> &E : constructors_map) {
  312. function->constructors.write[E.value] = E.key;
  313. }
  314. } else {
  315. function->_constructors_ptr = nullptr;
  316. function->_constructors_count = 0;
  317. }
  318. if (utilities_map.size()) {
  319. function->utilities.resize(utilities_map.size());
  320. function->_utilities_ptr = function->utilities.ptr();
  321. function->_utilities_count = utilities_map.size();
  322. for (const KeyValue<Variant::ValidatedUtilityFunction, int> &E : utilities_map) {
  323. function->utilities.write[E.value] = E.key;
  324. }
  325. } else {
  326. function->_utilities_ptr = nullptr;
  327. function->_utilities_count = 0;
  328. }
  329. if (gds_utilities_map.size()) {
  330. function->gds_utilities.resize(gds_utilities_map.size());
  331. function->_gds_utilities_ptr = function->gds_utilities.ptr();
  332. function->_gds_utilities_count = gds_utilities_map.size();
  333. for (const KeyValue<GDScriptUtilityFunctions::FunctionPtr, int> &E : gds_utilities_map) {
  334. function->gds_utilities.write[E.value] = E.key;
  335. }
  336. } else {
  337. function->_gds_utilities_ptr = nullptr;
  338. function->_gds_utilities_count = 0;
  339. }
  340. if (method_bind_map.size()) {
  341. function->methods.resize(method_bind_map.size());
  342. function->_methods_ptr = function->methods.ptrw();
  343. function->_methods_count = method_bind_map.size();
  344. for (const KeyValue<MethodBind *, int> &E : method_bind_map) {
  345. function->methods.write[E.value] = E.key;
  346. }
  347. } else {
  348. function->_methods_ptr = nullptr;
  349. function->_methods_count = 0;
  350. }
  351. if (lambdas_map.size()) {
  352. function->lambdas.resize(lambdas_map.size());
  353. function->_lambdas_ptr = function->lambdas.ptrw();
  354. function->_lambdas_count = lambdas_map.size();
  355. for (const KeyValue<GDScriptFunction *, int> &E : lambdas_map) {
  356. function->lambdas.write[E.value] = E.key;
  357. }
  358. } else {
  359. function->_lambdas_ptr = nullptr;
  360. function->_lambdas_count = 0;
  361. }
  362. if (debug_stack) {
  363. function->stack_debug = stack_debug;
  364. }
  365. function->_stack_size = RESERVED_STACK + max_locals + temporaries.size();
  366. function->_instruction_args_size = instr_args_max;
  367. function->_ptrcall_args_size = ptrcall_max;
  368. #ifdef DEBUG_ENABLED
  369. function->operator_names = operator_names;
  370. function->setter_names = setter_names;
  371. function->getter_names = getter_names;
  372. function->builtin_methods_names = builtin_methods_names;
  373. function->constructors_names = constructors_names;
  374. function->utilities_names = utilities_names;
  375. function->gds_utilities_names = gds_utilities_names;
  376. #endif
  377. ended = true;
  378. return function;
  379. }
  380. #ifdef DEBUG_ENABLED
  381. void GDScriptByteCodeGenerator::set_signature(const String &p_signature) {
  382. function->profile.signature = p_signature;
  383. }
  384. #endif
  385. void GDScriptByteCodeGenerator::set_initial_line(int p_line) {
  386. function->_initial_line = p_line;
  387. }
  388. #define HAS_BUILTIN_TYPE(m_var) \
  389. (m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN)
  390. #define IS_BUILTIN_TYPE(m_var, m_type) \
  391. (m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN && m_var.type.builtin_type == m_type)
  392. void GDScriptByteCodeGenerator::write_type_adjust(const Address &p_target, Variant::Type p_new_type) {
  393. switch (p_new_type) {
  394. case Variant::BOOL:
  395. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_BOOL);
  396. break;
  397. case Variant::INT:
  398. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_INT);
  399. break;
  400. case Variant::FLOAT:
  401. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_FLOAT);
  402. break;
  403. case Variant::STRING:
  404. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_STRING);
  405. break;
  406. case Variant::VECTOR2:
  407. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR2);
  408. break;
  409. case Variant::VECTOR2I:
  410. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR2I);
  411. break;
  412. case Variant::RECT2:
  413. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_RECT2);
  414. break;
  415. case Variant::RECT2I:
  416. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_RECT2I);
  417. break;
  418. case Variant::VECTOR3:
  419. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR3);
  420. break;
  421. case Variant::VECTOR3I:
  422. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR3I);
  423. break;
  424. case Variant::TRANSFORM2D:
  425. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_TRANSFORM2D);
  426. break;
  427. case Variant::VECTOR4:
  428. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR3);
  429. break;
  430. case Variant::VECTOR4I:
  431. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR3I);
  432. break;
  433. case Variant::PLANE:
  434. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PLANE);
  435. break;
  436. case Variant::QUATERNION:
  437. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_QUATERNION);
  438. break;
  439. case Variant::AABB:
  440. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_AABB);
  441. break;
  442. case Variant::BASIS:
  443. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_BASIS);
  444. break;
  445. case Variant::TRANSFORM3D:
  446. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_TRANSFORM3D);
  447. break;
  448. case Variant::PROJECTION:
  449. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PROJECTION);
  450. break;
  451. case Variant::COLOR:
  452. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_COLOR);
  453. break;
  454. case Variant::STRING_NAME:
  455. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_STRING_NAME);
  456. break;
  457. case Variant::NODE_PATH:
  458. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_NODE_PATH);
  459. break;
  460. case Variant::RID:
  461. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_RID);
  462. break;
  463. case Variant::OBJECT:
  464. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_OBJECT);
  465. break;
  466. case Variant::CALLABLE:
  467. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_CALLABLE);
  468. break;
  469. case Variant::SIGNAL:
  470. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_SIGNAL);
  471. break;
  472. case Variant::DICTIONARY:
  473. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_DICTIONARY);
  474. break;
  475. case Variant::ARRAY:
  476. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_ARRAY);
  477. break;
  478. case Variant::PACKED_BYTE_ARRAY:
  479. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_BYTE_ARRAY);
  480. break;
  481. case Variant::PACKED_INT32_ARRAY:
  482. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_INT32_ARRAY);
  483. break;
  484. case Variant::PACKED_INT64_ARRAY:
  485. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_INT64_ARRAY);
  486. break;
  487. case Variant::PACKED_FLOAT32_ARRAY:
  488. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_FLOAT32_ARRAY);
  489. break;
  490. case Variant::PACKED_FLOAT64_ARRAY:
  491. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_FLOAT64_ARRAY);
  492. break;
  493. case Variant::PACKED_STRING_ARRAY:
  494. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_STRING_ARRAY);
  495. break;
  496. case Variant::PACKED_VECTOR2_ARRAY:
  497. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_VECTOR2_ARRAY);
  498. break;
  499. case Variant::PACKED_VECTOR3_ARRAY:
  500. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_VECTOR3_ARRAY);
  501. break;
  502. case Variant::PACKED_COLOR_ARRAY:
  503. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_COLOR_ARRAY);
  504. break;
  505. case Variant::NIL:
  506. case Variant::VARIANT_MAX:
  507. return;
  508. }
  509. append(p_target);
  510. }
  511. void GDScriptByteCodeGenerator::write_unary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand) {
  512. if (HAS_BUILTIN_TYPE(p_left_operand)) {
  513. // Gather specific operator.
  514. Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(p_operator, p_left_operand.type.builtin_type, Variant::NIL);
  515. append_opcode(GDScriptFunction::OPCODE_OPERATOR_VALIDATED);
  516. append(p_left_operand);
  517. append(Address());
  518. append(p_target);
  519. append(op_func);
  520. #ifdef DEBUG_ENABLED
  521. add_debug_name(operator_names, get_operation_pos(op_func), Variant::get_operator_name(p_operator));
  522. #endif
  523. return;
  524. }
  525. // No specific types, perform variant evaluation.
  526. append_opcode(GDScriptFunction::OPCODE_OPERATOR);
  527. append(p_left_operand);
  528. append(Address());
  529. append(p_target);
  530. append(p_operator);
  531. }
  532. void GDScriptByteCodeGenerator::write_binary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) {
  533. if (HAS_BUILTIN_TYPE(p_left_operand) && HAS_BUILTIN_TYPE(p_right_operand)) {
  534. if (p_target.mode == Address::TEMPORARY) {
  535. Variant::Type result_type = Variant::get_operator_return_type(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type);
  536. Variant::Type temp_type = temporaries[p_target.address].type;
  537. if (result_type != temp_type) {
  538. write_type_adjust(p_target, result_type);
  539. }
  540. }
  541. // Gather specific operator.
  542. Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type);
  543. append_opcode(GDScriptFunction::OPCODE_OPERATOR_VALIDATED);
  544. append(p_left_operand);
  545. append(p_right_operand);
  546. append(p_target);
  547. append(op_func);
  548. #ifdef DEBUG_ENABLED
  549. add_debug_name(operator_names, get_operation_pos(op_func), Variant::get_operator_name(p_operator));
  550. #endif
  551. return;
  552. }
  553. // No specific types, perform variant evaluation.
  554. append_opcode(GDScriptFunction::OPCODE_OPERATOR);
  555. append(p_left_operand);
  556. append(p_right_operand);
  557. append(p_target);
  558. append(p_operator);
  559. }
  560. void GDScriptByteCodeGenerator::write_type_test(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) {
  561. switch (p_type.kind) {
  562. case GDScriptDataType::BUILTIN: {
  563. if (p_type.builtin_type == Variant::ARRAY && p_type.has_container_element_type()) {
  564. const GDScriptDataType &element_type = p_type.get_container_element_type();
  565. append_opcode(GDScriptFunction::OPCODE_TYPE_TEST_ARRAY);
  566. append(p_target);
  567. append(p_source);
  568. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  569. append(element_type.builtin_type);
  570. append(element_type.native_type);
  571. } else {
  572. append_opcode(GDScriptFunction::OPCODE_TYPE_TEST_BUILTIN);
  573. append(p_target);
  574. append(p_source);
  575. append(p_type.builtin_type);
  576. }
  577. } break;
  578. case GDScriptDataType::NATIVE: {
  579. append_opcode(GDScriptFunction::OPCODE_TYPE_TEST_NATIVE);
  580. append(p_target);
  581. append(p_source);
  582. append(p_type.native_type);
  583. } break;
  584. case GDScriptDataType::SCRIPT:
  585. case GDScriptDataType::GDSCRIPT: {
  586. const Variant &script = p_type.script_type;
  587. append_opcode(GDScriptFunction::OPCODE_TYPE_TEST_SCRIPT);
  588. append(p_target);
  589. append(p_source);
  590. append(get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  591. } break;
  592. default: {
  593. ERR_PRINT("Compiler bug: unresolved type in type test.");
  594. append_opcode(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  595. append(p_target);
  596. }
  597. }
  598. }
  599. void GDScriptByteCodeGenerator::write_and_left_operand(const Address &p_left_operand) {
  600. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  601. append(p_left_operand);
  602. logic_op_jump_pos1.push_back(opcodes.size());
  603. append(0); // Jump target, will be patched.
  604. }
  605. void GDScriptByteCodeGenerator::write_and_right_operand(const Address &p_right_operand) {
  606. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  607. append(p_right_operand);
  608. logic_op_jump_pos2.push_back(opcodes.size());
  609. append(0); // Jump target, will be patched.
  610. }
  611. void GDScriptByteCodeGenerator::write_end_and(const Address &p_target) {
  612. // If here means both operands are true.
  613. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  614. append(p_target);
  615. // Jump away from the fail condition.
  616. append_opcode(GDScriptFunction::OPCODE_JUMP);
  617. append(opcodes.size() + 3);
  618. // Here it means one of operands is false.
  619. patch_jump(logic_op_jump_pos1.back()->get());
  620. patch_jump(logic_op_jump_pos2.back()->get());
  621. logic_op_jump_pos1.pop_back();
  622. logic_op_jump_pos2.pop_back();
  623. append_opcode(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  624. append(p_target);
  625. }
  626. void GDScriptByteCodeGenerator::write_or_left_operand(const Address &p_left_operand) {
  627. append_opcode(GDScriptFunction::OPCODE_JUMP_IF);
  628. append(p_left_operand);
  629. logic_op_jump_pos1.push_back(opcodes.size());
  630. append(0); // Jump target, will be patched.
  631. }
  632. void GDScriptByteCodeGenerator::write_or_right_operand(const Address &p_right_operand) {
  633. append_opcode(GDScriptFunction::OPCODE_JUMP_IF);
  634. append(p_right_operand);
  635. logic_op_jump_pos2.push_back(opcodes.size());
  636. append(0); // Jump target, will be patched.
  637. }
  638. void GDScriptByteCodeGenerator::write_end_or(const Address &p_target) {
  639. // If here means both operands are false.
  640. append_opcode(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  641. append(p_target);
  642. // Jump away from the success condition.
  643. append_opcode(GDScriptFunction::OPCODE_JUMP);
  644. append(opcodes.size() + 3);
  645. // Here it means one of operands is true.
  646. patch_jump(logic_op_jump_pos1.back()->get());
  647. patch_jump(logic_op_jump_pos2.back()->get());
  648. logic_op_jump_pos1.pop_back();
  649. logic_op_jump_pos2.pop_back();
  650. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  651. append(p_target);
  652. }
  653. void GDScriptByteCodeGenerator::write_start_ternary(const Address &p_target) {
  654. ternary_result.push_back(p_target);
  655. }
  656. void GDScriptByteCodeGenerator::write_ternary_condition(const Address &p_condition) {
  657. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  658. append(p_condition);
  659. ternary_jump_fail_pos.push_back(opcodes.size());
  660. append(0); // Jump target, will be patched.
  661. }
  662. void GDScriptByteCodeGenerator::write_ternary_true_expr(const Address &p_expr) {
  663. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  664. append(ternary_result.back()->get());
  665. append(p_expr);
  666. // Jump away from the false path.
  667. append_opcode(GDScriptFunction::OPCODE_JUMP);
  668. ternary_jump_skip_pos.push_back(opcodes.size());
  669. append(0);
  670. // Fail must jump here.
  671. patch_jump(ternary_jump_fail_pos.back()->get());
  672. ternary_jump_fail_pos.pop_back();
  673. }
  674. void GDScriptByteCodeGenerator::write_ternary_false_expr(const Address &p_expr) {
  675. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  676. append(ternary_result.back()->get());
  677. append(p_expr);
  678. }
  679. void GDScriptByteCodeGenerator::write_end_ternary() {
  680. patch_jump(ternary_jump_skip_pos.back()->get());
  681. ternary_jump_skip_pos.pop_back();
  682. ternary_result.pop_back();
  683. }
  684. void GDScriptByteCodeGenerator::write_set(const Address &p_target, const Address &p_index, const Address &p_source) {
  685. if (HAS_BUILTIN_TYPE(p_target)) {
  686. if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_setter(p_target.type.builtin_type) &&
  687. IS_BUILTIN_TYPE(p_source, Variant::get_indexed_element_type(p_target.type.builtin_type))) {
  688. // Use indexed setter instead.
  689. Variant::ValidatedIndexedSetter setter = Variant::get_member_validated_indexed_setter(p_target.type.builtin_type);
  690. append_opcode(GDScriptFunction::OPCODE_SET_INDEXED_VALIDATED);
  691. append(p_target);
  692. append(p_index);
  693. append(p_source);
  694. append(setter);
  695. return;
  696. } else if (Variant::get_member_validated_keyed_setter(p_target.type.builtin_type)) {
  697. Variant::ValidatedKeyedSetter setter = Variant::get_member_validated_keyed_setter(p_target.type.builtin_type);
  698. append_opcode(GDScriptFunction::OPCODE_SET_KEYED_VALIDATED);
  699. append(p_target);
  700. append(p_index);
  701. append(p_source);
  702. append(setter);
  703. return;
  704. }
  705. }
  706. append_opcode(GDScriptFunction::OPCODE_SET_KEYED);
  707. append(p_target);
  708. append(p_index);
  709. append(p_source);
  710. }
  711. void GDScriptByteCodeGenerator::write_get(const Address &p_target, const Address &p_index, const Address &p_source) {
  712. if (HAS_BUILTIN_TYPE(p_source)) {
  713. if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_getter(p_source.type.builtin_type)) {
  714. // Use indexed getter instead.
  715. Variant::ValidatedIndexedGetter getter = Variant::get_member_validated_indexed_getter(p_source.type.builtin_type);
  716. append_opcode(GDScriptFunction::OPCODE_GET_INDEXED_VALIDATED);
  717. append(p_source);
  718. append(p_index);
  719. append(p_target);
  720. append(getter);
  721. return;
  722. } else if (Variant::get_member_validated_keyed_getter(p_source.type.builtin_type)) {
  723. Variant::ValidatedKeyedGetter getter = Variant::get_member_validated_keyed_getter(p_source.type.builtin_type);
  724. append_opcode(GDScriptFunction::OPCODE_GET_KEYED_VALIDATED);
  725. append(p_source);
  726. append(p_index);
  727. append(p_target);
  728. append(getter);
  729. return;
  730. }
  731. }
  732. append_opcode(GDScriptFunction::OPCODE_GET_KEYED);
  733. append(p_source);
  734. append(p_index);
  735. append(p_target);
  736. }
  737. void GDScriptByteCodeGenerator::write_set_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  738. if (HAS_BUILTIN_TYPE(p_target) && Variant::get_member_validated_setter(p_target.type.builtin_type, p_name) &&
  739. IS_BUILTIN_TYPE(p_source, Variant::get_member_type(p_target.type.builtin_type, p_name))) {
  740. Variant::ValidatedSetter setter = Variant::get_member_validated_setter(p_target.type.builtin_type, p_name);
  741. append_opcode(GDScriptFunction::OPCODE_SET_NAMED_VALIDATED);
  742. append(p_target);
  743. append(p_source);
  744. append(setter);
  745. #ifdef DEBUG_ENABLED
  746. add_debug_name(setter_names, get_setter_pos(setter), p_name);
  747. #endif
  748. return;
  749. }
  750. append_opcode(GDScriptFunction::OPCODE_SET_NAMED);
  751. append(p_target);
  752. append(p_source);
  753. append(p_name);
  754. }
  755. void GDScriptByteCodeGenerator::write_get_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  756. if (HAS_BUILTIN_TYPE(p_source) && Variant::get_member_validated_getter(p_source.type.builtin_type, p_name)) {
  757. Variant::ValidatedGetter getter = Variant::get_member_validated_getter(p_source.type.builtin_type, p_name);
  758. append_opcode(GDScriptFunction::OPCODE_GET_NAMED_VALIDATED);
  759. append(p_source);
  760. append(p_target);
  761. append(getter);
  762. #ifdef DEBUG_ENABLED
  763. add_debug_name(getter_names, get_getter_pos(getter), p_name);
  764. #endif
  765. return;
  766. }
  767. append_opcode(GDScriptFunction::OPCODE_GET_NAMED);
  768. append(p_source);
  769. append(p_target);
  770. append(p_name);
  771. }
  772. void GDScriptByteCodeGenerator::write_set_member(const Address &p_value, const StringName &p_name) {
  773. append_opcode(GDScriptFunction::OPCODE_SET_MEMBER);
  774. append(p_value);
  775. append(p_name);
  776. }
  777. void GDScriptByteCodeGenerator::write_get_member(const Address &p_target, const StringName &p_name) {
  778. append_opcode(GDScriptFunction::OPCODE_GET_MEMBER);
  779. append(p_target);
  780. append(p_name);
  781. }
  782. void GDScriptByteCodeGenerator::write_assign_with_conversion(const Address &p_target, const Address &p_source) {
  783. switch (p_target.type.kind) {
  784. case GDScriptDataType::BUILTIN: {
  785. if (p_target.type.builtin_type == Variant::ARRAY && p_target.type.has_container_element_type()) {
  786. const GDScriptDataType &element_type = p_target.type.get_container_element_type();
  787. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_ARRAY);
  788. append(p_target);
  789. append(p_source);
  790. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  791. append(element_type.builtin_type);
  792. append(element_type.native_type);
  793. } else {
  794. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN);
  795. append(p_target);
  796. append(p_source);
  797. append(p_target.type.builtin_type);
  798. }
  799. } break;
  800. case GDScriptDataType::NATIVE: {
  801. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_target.type.native_type];
  802. Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
  803. class_idx = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  804. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE);
  805. append(p_target);
  806. append(p_source);
  807. append(class_idx);
  808. } break;
  809. case GDScriptDataType::SCRIPT:
  810. case GDScriptDataType::GDSCRIPT: {
  811. Variant script = p_target.type.script_type;
  812. int idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  813. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT);
  814. append(p_target);
  815. append(p_source);
  816. append(idx);
  817. } break;
  818. default: {
  819. ERR_PRINT("Compiler bug: unresolved assign.");
  820. // Shouldn't get here, but fail-safe to a regular assignment
  821. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  822. append(p_target);
  823. append(p_source);
  824. }
  825. }
  826. }
  827. void GDScriptByteCodeGenerator::write_assign(const Address &p_target, const Address &p_source) {
  828. if (p_target.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type == Variant::ARRAY && p_target.type.has_container_element_type()) {
  829. const GDScriptDataType &element_type = p_target.type.get_container_element_type();
  830. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_ARRAY);
  831. append(p_target);
  832. append(p_source);
  833. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  834. append(element_type.builtin_type);
  835. append(element_type.native_type);
  836. } else if (p_target.type.kind == GDScriptDataType::BUILTIN && p_source.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type != p_source.type.builtin_type) {
  837. // Need conversion.
  838. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN);
  839. append(p_target);
  840. append(p_source);
  841. append(p_target.type.builtin_type);
  842. } else {
  843. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  844. append(p_target);
  845. append(p_source);
  846. }
  847. }
  848. void GDScriptByteCodeGenerator::write_assign_true(const Address &p_target) {
  849. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  850. append(p_target);
  851. }
  852. void GDScriptByteCodeGenerator::write_assign_false(const Address &p_target) {
  853. append_opcode(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  854. append(p_target);
  855. }
  856. void GDScriptByteCodeGenerator::write_assign_default_parameter(const Address &p_dst, const Address &p_src, bool p_use_conversion) {
  857. if (p_use_conversion) {
  858. write_assign_with_conversion(p_dst, p_src);
  859. } else {
  860. write_assign(p_dst, p_src);
  861. }
  862. function->default_arguments.push_back(opcodes.size());
  863. }
  864. void GDScriptByteCodeGenerator::write_store_global(const Address &p_dst, int p_global_index) {
  865. append_opcode(GDScriptFunction::OPCODE_STORE_GLOBAL);
  866. append(p_dst);
  867. append(p_global_index);
  868. }
  869. void GDScriptByteCodeGenerator::write_store_named_global(const Address &p_dst, const StringName &p_global) {
  870. append_opcode(GDScriptFunction::OPCODE_STORE_NAMED_GLOBAL);
  871. append(p_dst);
  872. append(p_global);
  873. }
  874. void GDScriptByteCodeGenerator::write_cast(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) {
  875. int index = 0;
  876. switch (p_type.kind) {
  877. case GDScriptDataType::BUILTIN: {
  878. append_opcode(GDScriptFunction::OPCODE_CAST_TO_BUILTIN);
  879. index = p_type.builtin_type;
  880. } break;
  881. case GDScriptDataType::NATIVE: {
  882. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_type.native_type];
  883. Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
  884. append_opcode(GDScriptFunction::OPCODE_CAST_TO_NATIVE);
  885. index = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  886. } break;
  887. case GDScriptDataType::SCRIPT:
  888. case GDScriptDataType::GDSCRIPT: {
  889. Variant script = p_type.script_type;
  890. int idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  891. append_opcode(GDScriptFunction::OPCODE_CAST_TO_SCRIPT);
  892. index = idx;
  893. } break;
  894. default: {
  895. return;
  896. }
  897. }
  898. append(p_source);
  899. append(p_target);
  900. append(index);
  901. }
  902. GDScriptByteCodeGenerator::CallTarget GDScriptByteCodeGenerator::get_call_target(const GDScriptCodeGenerator::Address &p_target, Variant::Type p_type) {
  903. if (p_target.mode == Address::NIL) {
  904. GDScriptDataType type;
  905. if (p_type != Variant::NIL) {
  906. type.has_type = true;
  907. type.kind = GDScriptDataType::BUILTIN;
  908. type.builtin_type = p_type;
  909. }
  910. uint32_t addr = add_temporary(type);
  911. return CallTarget(Address(Address::TEMPORARY, addr, type), true, this);
  912. } else {
  913. return CallTarget(p_target, false, this);
  914. }
  915. }
  916. void GDScriptByteCodeGenerator::write_call(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  917. append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  918. for (int i = 0; i < p_arguments.size(); i++) {
  919. append(p_arguments[i]);
  920. }
  921. append(p_base);
  922. CallTarget ct = get_call_target(p_target);
  923. append(ct.target);
  924. append(p_arguments.size());
  925. append(p_function_name);
  926. ct.cleanup();
  927. }
  928. void GDScriptByteCodeGenerator::write_super_call(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  929. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_SELF_BASE, 1 + p_arguments.size());
  930. for (int i = 0; i < p_arguments.size(); i++) {
  931. append(p_arguments[i]);
  932. }
  933. CallTarget ct = get_call_target(p_target);
  934. append(ct.target);
  935. append(p_arguments.size());
  936. append(p_function_name);
  937. ct.cleanup();
  938. }
  939. void GDScriptByteCodeGenerator::write_call_async(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  940. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
  941. for (int i = 0; i < p_arguments.size(); i++) {
  942. append(p_arguments[i]);
  943. }
  944. append(p_base);
  945. CallTarget ct = get_call_target(p_target);
  946. append(ct.target);
  947. append(p_arguments.size());
  948. append(p_function_name);
  949. ct.cleanup();
  950. }
  951. void GDScriptByteCodeGenerator::write_call_gdscript_utility(const Address &p_target, const StringName &p_function, const Vector<Address> &p_arguments) {
  952. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_GDSCRIPT_UTILITY, 1 + p_arguments.size());
  953. GDScriptUtilityFunctions::FunctionPtr gds_function = GDScriptUtilityFunctions::get_function(p_function);
  954. for (int i = 0; i < p_arguments.size(); i++) {
  955. append(p_arguments[i]);
  956. }
  957. CallTarget ct = get_call_target(p_target);
  958. append(ct.target);
  959. append(p_arguments.size());
  960. append(gds_function);
  961. ct.cleanup();
  962. #ifdef DEBUG_ENABLED
  963. add_debug_name(gds_utilities_names, get_gds_utility_pos(gds_function), p_function);
  964. #endif
  965. }
  966. void GDScriptByteCodeGenerator::write_call_utility(const Address &p_target, const StringName &p_function, const Vector<Address> &p_arguments) {
  967. bool is_validated = true;
  968. if (Variant::is_utility_function_vararg(p_function)) {
  969. is_validated = true; // Vararg works fine with any argument, since they can be any type.
  970. } else if (p_arguments.size() == Variant::get_utility_function_argument_count(p_function)) {
  971. bool all_types_exact = true;
  972. for (int i = 0; i < p_arguments.size(); i++) {
  973. if (!IS_BUILTIN_TYPE(p_arguments[i], Variant::get_utility_function_argument_type(p_function, i))) {
  974. all_types_exact = false;
  975. break;
  976. }
  977. }
  978. is_validated = all_types_exact;
  979. }
  980. if (is_validated) {
  981. Variant::Type result_type = Variant::has_utility_function_return_value(p_function) ? Variant::get_utility_function_return_type(p_function) : Variant::NIL;
  982. CallTarget ct = get_call_target(p_target, result_type);
  983. Variant::Type temp_type = temporaries[ct.target.address].type;
  984. if (result_type != temp_type) {
  985. write_type_adjust(ct.target, result_type);
  986. }
  987. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_UTILITY_VALIDATED, 1 + p_arguments.size());
  988. for (int i = 0; i < p_arguments.size(); i++) {
  989. append(p_arguments[i]);
  990. }
  991. append(ct.target);
  992. append(p_arguments.size());
  993. append(Variant::get_validated_utility_function(p_function));
  994. ct.cleanup();
  995. #ifdef DEBUG_ENABLED
  996. add_debug_name(utilities_names, get_utility_pos(Variant::get_validated_utility_function(p_function)), p_function);
  997. #endif
  998. } else {
  999. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_UTILITY, 1 + p_arguments.size());
  1000. for (int i = 0; i < p_arguments.size(); i++) {
  1001. append(p_arguments[i]);
  1002. }
  1003. CallTarget ct = get_call_target(p_target);
  1004. append(ct.target);
  1005. append(p_arguments.size());
  1006. append(p_function);
  1007. ct.cleanup();
  1008. }
  1009. }
  1010. void GDScriptByteCodeGenerator::write_call_builtin_type(const Address &p_target, const Address &p_base, Variant::Type p_type, const StringName &p_method, bool p_is_static, const Vector<Address> &p_arguments) {
  1011. bool is_validated = false;
  1012. // Check if all types are correct.
  1013. if (Variant::is_builtin_method_vararg(p_type, p_method)) {
  1014. is_validated = true; // Vararg works fine with any argument, since they can be any type.
  1015. } else if (p_arguments.size() == Variant::get_builtin_method_argument_count(p_type, p_method)) {
  1016. bool all_types_exact = true;
  1017. for (int i = 0; i < p_arguments.size(); i++) {
  1018. if (!IS_BUILTIN_TYPE(p_arguments[i], Variant::get_builtin_method_argument_type(p_type, p_method, i))) {
  1019. all_types_exact = false;
  1020. break;
  1021. }
  1022. }
  1023. is_validated = all_types_exact;
  1024. }
  1025. if (!is_validated) {
  1026. // Perform regular call.
  1027. if (p_is_static) {
  1028. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_BUILTIN_STATIC, p_arguments.size() + 1);
  1029. for (int i = 0; i < p_arguments.size(); i++) {
  1030. append(p_arguments[i]);
  1031. }
  1032. CallTarget ct = get_call_target(p_target);
  1033. append(ct.target);
  1034. append(p_type);
  1035. append(p_method);
  1036. append(p_arguments.size());
  1037. ct.cleanup();
  1038. } else {
  1039. write_call(p_target, p_base, p_method, p_arguments);
  1040. }
  1041. return;
  1042. }
  1043. Variant::Type result_type = Variant::get_builtin_method_return_type(p_type, p_method);
  1044. CallTarget ct = get_call_target(p_target, result_type);
  1045. Variant::Type temp_type = temporaries[ct.target.address].type;
  1046. if (result_type != temp_type) {
  1047. write_type_adjust(ct.target, result_type);
  1048. }
  1049. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_BUILTIN_TYPE_VALIDATED, 2 + p_arguments.size());
  1050. for (int i = 0; i < p_arguments.size(); i++) {
  1051. append(p_arguments[i]);
  1052. }
  1053. append(p_base);
  1054. append(ct.target);
  1055. append(p_arguments.size());
  1056. append(Variant::get_validated_builtin_method(p_type, p_method));
  1057. ct.cleanup();
  1058. #ifdef DEBUG_ENABLED
  1059. add_debug_name(builtin_methods_names, get_builtin_method_pos(Variant::get_validated_builtin_method(p_type, p_method)), p_method);
  1060. #endif
  1061. }
  1062. void GDScriptByteCodeGenerator::write_call_builtin_type(const Address &p_target, const Address &p_base, Variant::Type p_type, const StringName &p_method, const Vector<Address> &p_arguments) {
  1063. write_call_builtin_type(p_target, p_base, p_type, p_method, false, p_arguments);
  1064. }
  1065. void GDScriptByteCodeGenerator::write_call_builtin_type_static(const Address &p_target, Variant::Type p_type, const StringName &p_method, const Vector<Address> &p_arguments) {
  1066. write_call_builtin_type(p_target, Address(), p_type, p_method, true, p_arguments);
  1067. }
  1068. void GDScriptByteCodeGenerator::write_call_native_static(const Address &p_target, const StringName &p_class, const StringName &p_method, const Vector<Address> &p_arguments) {
  1069. bool is_validated = false;
  1070. MethodBind *method = ClassDB::get_method(p_class, p_method);
  1071. if (!is_validated) {
  1072. // Perform regular call.
  1073. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_NATIVE_STATIC, p_arguments.size() + 1);
  1074. for (int i = 0; i < p_arguments.size(); i++) {
  1075. append(p_arguments[i]);
  1076. }
  1077. CallTarget ct = get_call_target(p_target);
  1078. append(ct.target);
  1079. append(method);
  1080. append(p_arguments.size());
  1081. ct.cleanup();
  1082. return;
  1083. }
  1084. }
  1085. void GDScriptByteCodeGenerator::write_call_method_bind(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) {
  1086. append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL_METHOD_BIND : GDScriptFunction::OPCODE_CALL_METHOD_BIND_RET, 2 + p_arguments.size());
  1087. for (int i = 0; i < p_arguments.size(); i++) {
  1088. append(p_arguments[i]);
  1089. }
  1090. CallTarget ct = get_call_target(p_target);
  1091. append(p_base);
  1092. append(ct.target);
  1093. append(p_arguments.size());
  1094. append(p_method);
  1095. ct.cleanup();
  1096. }
  1097. void GDScriptByteCodeGenerator::write_call_ptrcall(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) {
  1098. #define CASE_TYPE(m_type) \
  1099. case Variant::m_type: \
  1100. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_PTRCALL_##m_type, 2 + p_arguments.size()); \
  1101. break
  1102. bool is_ptrcall = true;
  1103. if (p_method->has_return()) {
  1104. MethodInfo info;
  1105. ClassDB::get_method_info(p_method->get_instance_class(), p_method->get_name(), &info);
  1106. switch (info.return_val.type) {
  1107. CASE_TYPE(BOOL);
  1108. CASE_TYPE(INT);
  1109. CASE_TYPE(FLOAT);
  1110. CASE_TYPE(STRING);
  1111. CASE_TYPE(VECTOR2);
  1112. CASE_TYPE(VECTOR2I);
  1113. CASE_TYPE(RECT2);
  1114. CASE_TYPE(RECT2I);
  1115. CASE_TYPE(VECTOR3);
  1116. CASE_TYPE(VECTOR3I);
  1117. CASE_TYPE(TRANSFORM2D);
  1118. CASE_TYPE(PLANE);
  1119. CASE_TYPE(AABB);
  1120. CASE_TYPE(BASIS);
  1121. CASE_TYPE(TRANSFORM3D);
  1122. CASE_TYPE(COLOR);
  1123. CASE_TYPE(STRING_NAME);
  1124. CASE_TYPE(NODE_PATH);
  1125. CASE_TYPE(RID);
  1126. CASE_TYPE(QUATERNION);
  1127. CASE_TYPE(OBJECT);
  1128. CASE_TYPE(CALLABLE);
  1129. CASE_TYPE(SIGNAL);
  1130. CASE_TYPE(DICTIONARY);
  1131. CASE_TYPE(ARRAY);
  1132. CASE_TYPE(PACKED_BYTE_ARRAY);
  1133. CASE_TYPE(PACKED_INT32_ARRAY);
  1134. CASE_TYPE(PACKED_INT64_ARRAY);
  1135. CASE_TYPE(PACKED_FLOAT32_ARRAY);
  1136. CASE_TYPE(PACKED_FLOAT64_ARRAY);
  1137. CASE_TYPE(PACKED_STRING_ARRAY);
  1138. CASE_TYPE(PACKED_VECTOR2_ARRAY);
  1139. CASE_TYPE(PACKED_VECTOR3_ARRAY);
  1140. CASE_TYPE(PACKED_COLOR_ARRAY);
  1141. default:
  1142. append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL_METHOD_BIND : GDScriptFunction::OPCODE_CALL_METHOD_BIND_RET, 2 + p_arguments.size());
  1143. is_ptrcall = false;
  1144. break;
  1145. }
  1146. } else {
  1147. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_PTRCALL_NO_RETURN, 2 + p_arguments.size());
  1148. }
  1149. for (int i = 0; i < p_arguments.size(); i++) {
  1150. append(p_arguments[i]);
  1151. }
  1152. append(p_base);
  1153. CallTarget ct = get_call_target(p_target);
  1154. append(ct.target);
  1155. append(p_arguments.size());
  1156. append(p_method);
  1157. ct.cleanup();
  1158. if (is_ptrcall) {
  1159. alloc_ptrcall(p_arguments.size());
  1160. }
  1161. #undef CASE_TYPE
  1162. }
  1163. void GDScriptByteCodeGenerator::write_call_self(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  1164. append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  1165. for (int i = 0; i < p_arguments.size(); i++) {
  1166. append(p_arguments[i]);
  1167. }
  1168. append(GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  1169. CallTarget ct = get_call_target(p_target);
  1170. append(ct.target);
  1171. append(p_arguments.size());
  1172. append(p_function_name);
  1173. ct.cleanup();
  1174. }
  1175. void GDScriptByteCodeGenerator::write_call_self_async(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  1176. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
  1177. for (int i = 0; i < p_arguments.size(); i++) {
  1178. append(p_arguments[i]);
  1179. }
  1180. append(GDScriptFunction::ADDR_SELF);
  1181. CallTarget ct = get_call_target(p_target);
  1182. append(ct.target);
  1183. append(p_arguments.size());
  1184. append(p_function_name);
  1185. ct.cleanup();
  1186. }
  1187. void GDScriptByteCodeGenerator::write_call_script_function(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  1188. append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  1189. for (int i = 0; i < p_arguments.size(); i++) {
  1190. append(p_arguments[i]);
  1191. }
  1192. append(p_base);
  1193. CallTarget ct = get_call_target(p_target);
  1194. append(ct.target);
  1195. append(p_arguments.size());
  1196. append(p_function_name);
  1197. ct.cleanup();
  1198. }
  1199. void GDScriptByteCodeGenerator::write_lambda(const Address &p_target, GDScriptFunction *p_function, const Vector<Address> &p_captures, bool p_use_self) {
  1200. append_opcode_and_argcount(p_use_self ? GDScriptFunction::OPCODE_CREATE_SELF_LAMBDA : GDScriptFunction::OPCODE_CREATE_LAMBDA, 1 + p_captures.size());
  1201. for (int i = 0; i < p_captures.size(); i++) {
  1202. append(p_captures[i]);
  1203. }
  1204. CallTarget ct = get_call_target(p_target);
  1205. append(ct.target);
  1206. append(p_captures.size());
  1207. append(p_function);
  1208. ct.cleanup();
  1209. }
  1210. void GDScriptByteCodeGenerator::write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) {
  1211. // Try to find an appropriate constructor.
  1212. bool all_have_type = true;
  1213. Vector<Variant::Type> arg_types;
  1214. for (int i = 0; i < p_arguments.size(); i++) {
  1215. if (!HAS_BUILTIN_TYPE(p_arguments[i])) {
  1216. all_have_type = false;
  1217. break;
  1218. }
  1219. arg_types.push_back(p_arguments[i].type.builtin_type);
  1220. }
  1221. if (all_have_type) {
  1222. int valid_constructor = -1;
  1223. for (int i = 0; i < Variant::get_constructor_count(p_type); i++) {
  1224. if (Variant::get_constructor_argument_count(p_type, i) != p_arguments.size()) {
  1225. continue;
  1226. }
  1227. int types_correct = true;
  1228. for (int j = 0; j < arg_types.size(); j++) {
  1229. if (arg_types[j] != Variant::get_constructor_argument_type(p_type, i, j)) {
  1230. types_correct = false;
  1231. break;
  1232. }
  1233. }
  1234. if (types_correct) {
  1235. valid_constructor = i;
  1236. break;
  1237. }
  1238. }
  1239. if (valid_constructor >= 0) {
  1240. append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_VALIDATED, 1 + p_arguments.size());
  1241. for (int i = 0; i < p_arguments.size(); i++) {
  1242. append(p_arguments[i]);
  1243. }
  1244. CallTarget ct = get_call_target(p_target);
  1245. append(ct.target);
  1246. append(p_arguments.size());
  1247. append(Variant::get_validated_constructor(p_type, valid_constructor));
  1248. ct.cleanup();
  1249. #ifdef DEBUG_ENABLED
  1250. add_debug_name(constructors_names, get_constructor_pos(Variant::get_validated_constructor(p_type, valid_constructor)), Variant::get_type_name(p_type));
  1251. #endif
  1252. return;
  1253. }
  1254. }
  1255. append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT, 1 + p_arguments.size());
  1256. for (int i = 0; i < p_arguments.size(); i++) {
  1257. append(p_arguments[i]);
  1258. }
  1259. CallTarget ct = get_call_target(p_target);
  1260. append(ct.target);
  1261. append(p_arguments.size());
  1262. append(p_type);
  1263. ct.cleanup();
  1264. }
  1265. void GDScriptByteCodeGenerator::write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) {
  1266. append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_ARRAY, 1 + p_arguments.size());
  1267. for (int i = 0; i < p_arguments.size(); i++) {
  1268. append(p_arguments[i]);
  1269. }
  1270. CallTarget ct = get_call_target(p_target);
  1271. append(ct.target);
  1272. append(p_arguments.size());
  1273. ct.cleanup();
  1274. }
  1275. void GDScriptByteCodeGenerator::write_construct_typed_array(const Address &p_target, const GDScriptDataType &p_element_type, const Vector<Address> &p_arguments) {
  1276. append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_TYPED_ARRAY, 2 + p_arguments.size());
  1277. for (int i = 0; i < p_arguments.size(); i++) {
  1278. append(p_arguments[i]);
  1279. }
  1280. CallTarget ct = get_call_target(p_target);
  1281. append(ct.target);
  1282. append(get_constant_pos(p_element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1283. append(p_arguments.size());
  1284. append(p_element_type.builtin_type);
  1285. append(p_element_type.native_type);
  1286. ct.cleanup();
  1287. }
  1288. void GDScriptByteCodeGenerator::write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) {
  1289. append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY, 1 + p_arguments.size());
  1290. for (int i = 0; i < p_arguments.size(); i++) {
  1291. append(p_arguments[i]);
  1292. }
  1293. CallTarget ct = get_call_target(p_target);
  1294. append(ct.target);
  1295. append(p_arguments.size() / 2); // This is number of key-value pairs, so only half of actual arguments.
  1296. ct.cleanup();
  1297. }
  1298. void GDScriptByteCodeGenerator::write_await(const Address &p_target, const Address &p_operand) {
  1299. append_opcode(GDScriptFunction::OPCODE_AWAIT);
  1300. append(p_operand);
  1301. append_opcode(GDScriptFunction::OPCODE_AWAIT_RESUME);
  1302. append(p_target);
  1303. }
  1304. void GDScriptByteCodeGenerator::write_if(const Address &p_condition) {
  1305. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1306. append(p_condition);
  1307. if_jmp_addrs.push_back(opcodes.size());
  1308. append(0); // Jump destination, will be patched.
  1309. }
  1310. void GDScriptByteCodeGenerator::write_else() {
  1311. append_opcode(GDScriptFunction::OPCODE_JUMP); // Jump from true if block;
  1312. int else_jmp_addr = opcodes.size();
  1313. append(0); // Jump destination, will be patched.
  1314. patch_jump(if_jmp_addrs.back()->get());
  1315. if_jmp_addrs.pop_back();
  1316. if_jmp_addrs.push_back(else_jmp_addr);
  1317. }
  1318. void GDScriptByteCodeGenerator::write_endif() {
  1319. patch_jump(if_jmp_addrs.back()->get());
  1320. if_jmp_addrs.pop_back();
  1321. }
  1322. void GDScriptByteCodeGenerator::write_jump_if_shared(const Address &p_value) {
  1323. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_SHARED);
  1324. append(p_value);
  1325. if_jmp_addrs.push_back(opcodes.size());
  1326. append(0); // Jump destination, will be patched.
  1327. }
  1328. void GDScriptByteCodeGenerator::write_end_jump_if_shared() {
  1329. patch_jump(if_jmp_addrs.back()->get());
  1330. if_jmp_addrs.pop_back();
  1331. }
  1332. void GDScriptByteCodeGenerator::start_for(const GDScriptDataType &p_iterator_type, const GDScriptDataType &p_list_type) {
  1333. Address counter(Address::LOCAL_VARIABLE, add_local("@counter_pos", p_iterator_type), p_iterator_type);
  1334. Address container(Address::LOCAL_VARIABLE, add_local("@container_pos", p_list_type), p_list_type);
  1335. // Store state.
  1336. for_counter_variables.push_back(counter);
  1337. for_container_variables.push_back(container);
  1338. }
  1339. void GDScriptByteCodeGenerator::write_for_assignment(const Address &p_variable, const Address &p_list) {
  1340. const Address &container = for_container_variables.back()->get();
  1341. // Assign container.
  1342. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  1343. append(container);
  1344. append(p_list);
  1345. for_iterator_variables.push_back(p_variable);
  1346. }
  1347. void GDScriptByteCodeGenerator::write_for() {
  1348. const Address &iterator = for_iterator_variables.back()->get();
  1349. const Address &counter = for_counter_variables.back()->get();
  1350. const Address &container = for_container_variables.back()->get();
  1351. current_breaks_to_patch.push_back(List<int>());
  1352. GDScriptFunction::Opcode begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN;
  1353. GDScriptFunction::Opcode iterate_opcode = GDScriptFunction::OPCODE_ITERATE;
  1354. if (container.type.has_type) {
  1355. if (container.type.kind == GDScriptDataType::BUILTIN) {
  1356. switch (container.type.builtin_type) {
  1357. case Variant::INT:
  1358. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_INT;
  1359. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_INT;
  1360. break;
  1361. case Variant::FLOAT:
  1362. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_FLOAT;
  1363. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_FLOAT;
  1364. break;
  1365. case Variant::VECTOR2:
  1366. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2;
  1367. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2;
  1368. break;
  1369. case Variant::VECTOR2I:
  1370. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2I;
  1371. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2I;
  1372. break;
  1373. case Variant::VECTOR3:
  1374. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3;
  1375. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3;
  1376. break;
  1377. case Variant::VECTOR3I:
  1378. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3I;
  1379. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3I;
  1380. break;
  1381. case Variant::STRING:
  1382. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_STRING;
  1383. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_STRING;
  1384. break;
  1385. case Variant::DICTIONARY:
  1386. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_DICTIONARY;
  1387. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_DICTIONARY;
  1388. break;
  1389. case Variant::ARRAY:
  1390. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_ARRAY;
  1391. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_ARRAY;
  1392. break;
  1393. case Variant::PACKED_BYTE_ARRAY:
  1394. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_BYTE_ARRAY;
  1395. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_BYTE_ARRAY;
  1396. break;
  1397. case Variant::PACKED_INT32_ARRAY:
  1398. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT32_ARRAY;
  1399. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT32_ARRAY;
  1400. break;
  1401. case Variant::PACKED_INT64_ARRAY:
  1402. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT64_ARRAY;
  1403. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT64_ARRAY;
  1404. break;
  1405. case Variant::PACKED_FLOAT32_ARRAY:
  1406. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT32_ARRAY;
  1407. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT32_ARRAY;
  1408. break;
  1409. case Variant::PACKED_FLOAT64_ARRAY:
  1410. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT64_ARRAY;
  1411. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT64_ARRAY;
  1412. break;
  1413. case Variant::PACKED_STRING_ARRAY:
  1414. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_STRING_ARRAY;
  1415. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_STRING_ARRAY;
  1416. break;
  1417. case Variant::PACKED_VECTOR2_ARRAY:
  1418. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR2_ARRAY;
  1419. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR2_ARRAY;
  1420. break;
  1421. case Variant::PACKED_VECTOR3_ARRAY:
  1422. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR3_ARRAY;
  1423. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR3_ARRAY;
  1424. break;
  1425. case Variant::PACKED_COLOR_ARRAY:
  1426. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_COLOR_ARRAY;
  1427. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_COLOR_ARRAY;
  1428. break;
  1429. default:
  1430. break;
  1431. }
  1432. } else {
  1433. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_OBJECT;
  1434. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_OBJECT;
  1435. }
  1436. }
  1437. // Begin loop.
  1438. append_opcode(begin_opcode);
  1439. append(counter);
  1440. append(container);
  1441. append(iterator);
  1442. for_jmp_addrs.push_back(opcodes.size());
  1443. append(0); // End of loop address, will be patched.
  1444. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1445. append(opcodes.size() + 6); // Skip over 'continue' code.
  1446. // Next iteration.
  1447. int continue_addr = opcodes.size();
  1448. continue_addrs.push_back(continue_addr);
  1449. append_opcode(iterate_opcode);
  1450. append(counter);
  1451. append(container);
  1452. append(iterator);
  1453. for_jmp_addrs.push_back(opcodes.size());
  1454. append(0); // Jump destination, will be patched.
  1455. }
  1456. void GDScriptByteCodeGenerator::write_endfor() {
  1457. // Jump back to loop check.
  1458. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1459. append(continue_addrs.back()->get());
  1460. continue_addrs.pop_back();
  1461. // Patch end jumps (two of them).
  1462. for (int i = 0; i < 2; i++) {
  1463. patch_jump(for_jmp_addrs.back()->get());
  1464. for_jmp_addrs.pop_back();
  1465. }
  1466. // Patch break statements.
  1467. for (const int &E : current_breaks_to_patch.back()->get()) {
  1468. patch_jump(E);
  1469. }
  1470. current_breaks_to_patch.pop_back();
  1471. // Pop state.
  1472. for_iterator_variables.pop_back();
  1473. for_counter_variables.pop_back();
  1474. for_container_variables.pop_back();
  1475. }
  1476. void GDScriptByteCodeGenerator::start_while_condition() {
  1477. current_breaks_to_patch.push_back(List<int>());
  1478. continue_addrs.push_back(opcodes.size());
  1479. }
  1480. void GDScriptByteCodeGenerator::write_while(const Address &p_condition) {
  1481. // Condition check.
  1482. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1483. append(p_condition);
  1484. while_jmp_addrs.push_back(opcodes.size());
  1485. append(0); // End of loop address, will be patched.
  1486. }
  1487. void GDScriptByteCodeGenerator::write_endwhile() {
  1488. // Jump back to loop check.
  1489. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1490. append(continue_addrs.back()->get());
  1491. continue_addrs.pop_back();
  1492. // Patch end jump.
  1493. patch_jump(while_jmp_addrs.back()->get());
  1494. while_jmp_addrs.pop_back();
  1495. // Patch break statements.
  1496. for (const int &E : current_breaks_to_patch.back()->get()) {
  1497. patch_jump(E);
  1498. }
  1499. current_breaks_to_patch.pop_back();
  1500. }
  1501. void GDScriptByteCodeGenerator::write_break() {
  1502. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1503. current_breaks_to_patch.back()->get().push_back(opcodes.size());
  1504. append(0);
  1505. }
  1506. void GDScriptByteCodeGenerator::write_continue() {
  1507. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1508. append(continue_addrs.back()->get());
  1509. }
  1510. void GDScriptByteCodeGenerator::write_breakpoint() {
  1511. append_opcode(GDScriptFunction::OPCODE_BREAKPOINT);
  1512. }
  1513. void GDScriptByteCodeGenerator::write_newline(int p_line) {
  1514. append_opcode(GDScriptFunction::OPCODE_LINE);
  1515. append(p_line);
  1516. current_line = p_line;
  1517. }
  1518. void GDScriptByteCodeGenerator::write_return(const Address &p_return_value) {
  1519. if (!function->return_type.has_type || p_return_value.type.has_type) {
  1520. // Either the function is untyped or the return value is also typed.
  1521. // If this is a typed function, then we need to check for potential conversions.
  1522. if (function->return_type.has_type) {
  1523. if (function->return_type.kind == GDScriptDataType::BUILTIN && function->return_type.builtin_type == Variant::ARRAY && function->return_type.has_container_element_type()) {
  1524. // Typed array.
  1525. const GDScriptDataType &element_type = function->return_type.get_container_element_type();
  1526. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_ARRAY);
  1527. append(p_return_value);
  1528. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1529. append(element_type.builtin_type);
  1530. append(element_type.native_type);
  1531. } else if (function->return_type.kind == GDScriptDataType::BUILTIN && p_return_value.type.kind == GDScriptDataType::BUILTIN && function->return_type.builtin_type != p_return_value.type.builtin_type) {
  1532. // Add conversion.
  1533. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_BUILTIN);
  1534. append(p_return_value);
  1535. append(function->return_type.builtin_type);
  1536. } else {
  1537. // Just assign.
  1538. append_opcode(GDScriptFunction::OPCODE_RETURN);
  1539. append(p_return_value);
  1540. }
  1541. } else {
  1542. append_opcode(GDScriptFunction::OPCODE_RETURN);
  1543. append(p_return_value);
  1544. }
  1545. } else {
  1546. switch (function->return_type.kind) {
  1547. case GDScriptDataType::BUILTIN: {
  1548. if (function->return_type.builtin_type == Variant::ARRAY && function->return_type.has_container_element_type()) {
  1549. const GDScriptDataType &element_type = function->return_type.get_container_element_type();
  1550. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_ARRAY);
  1551. append(p_return_value);
  1552. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1553. append(element_type.builtin_type);
  1554. append(element_type.native_type);
  1555. } else {
  1556. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_BUILTIN);
  1557. append(p_return_value);
  1558. append(function->return_type.builtin_type);
  1559. }
  1560. } break;
  1561. case GDScriptDataType::NATIVE: {
  1562. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_NATIVE);
  1563. append(p_return_value);
  1564. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[function->return_type.native_type];
  1565. Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
  1566. class_idx = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  1567. append(class_idx);
  1568. } break;
  1569. case GDScriptDataType::GDSCRIPT:
  1570. case GDScriptDataType::SCRIPT: {
  1571. Variant script = function->return_type.script_type;
  1572. int script_idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  1573. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_SCRIPT);
  1574. append(p_return_value);
  1575. append(script_idx);
  1576. } break;
  1577. default: {
  1578. ERR_PRINT("Compiler bug: unresolved return.");
  1579. // Shouldn't get here, but fail-safe to a regular return;
  1580. append_opcode(GDScriptFunction::OPCODE_RETURN);
  1581. append(p_return_value);
  1582. } break;
  1583. }
  1584. }
  1585. }
  1586. void GDScriptByteCodeGenerator::write_assert(const Address &p_test, const Address &p_message) {
  1587. append_opcode(GDScriptFunction::OPCODE_ASSERT);
  1588. append(p_test);
  1589. append(p_message);
  1590. }
  1591. void GDScriptByteCodeGenerator::start_block() {
  1592. push_stack_identifiers();
  1593. }
  1594. void GDScriptByteCodeGenerator::end_block() {
  1595. pop_stack_identifiers();
  1596. }
  1597. GDScriptByteCodeGenerator::~GDScriptByteCodeGenerator() {
  1598. if (!ended && function != nullptr) {
  1599. memdelete(function);
  1600. }
  1601. }