gdscript_byte_codegen.cpp 65 KB

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