gdscript_byte_codegen.cpp 63 KB

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