gdscript_byte_codegen.cpp 64 KB

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