gdscript_byte_codegen.cpp 67 KB

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