gdscript_byte_codegen.cpp 67 KB

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