gdscript_byte_codegen.cpp 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805
  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.write[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. append(0); // Signature storage.
  530. append(0); // Return type storage.
  531. constexpr int _pointer_size = sizeof(Variant::ValidatedOperatorEvaluator) / sizeof(*(opcodes.ptr()));
  532. for (int i = 0; i < _pointer_size; i++) {
  533. append(0); // Space for function pointer.
  534. }
  535. }
  536. void GDScriptByteCodeGenerator::write_binary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) {
  537. // Avoid validated evaluator for modulo and division when operands are int, since there's no check for division by zero.
  538. 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)) {
  539. if (p_target.mode == Address::TEMPORARY) {
  540. Variant::Type result_type = Variant::get_operator_return_type(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type);
  541. Variant::Type temp_type = temporaries[p_target.address].type;
  542. if (result_type != temp_type) {
  543. write_type_adjust(p_target, result_type);
  544. }
  545. }
  546. // Gather specific operator.
  547. Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type);
  548. append_opcode(GDScriptFunction::OPCODE_OPERATOR_VALIDATED);
  549. append(p_left_operand);
  550. append(p_right_operand);
  551. append(p_target);
  552. append(op_func);
  553. #ifdef DEBUG_ENABLED
  554. add_debug_name(operator_names, get_operation_pos(op_func), Variant::get_operator_name(p_operator));
  555. #endif
  556. return;
  557. }
  558. // No specific types, perform variant evaluation.
  559. append_opcode(GDScriptFunction::OPCODE_OPERATOR);
  560. append(p_left_operand);
  561. append(p_right_operand);
  562. append(p_target);
  563. append(p_operator);
  564. append(0); // Signature storage.
  565. append(0); // Return type storage.
  566. constexpr int _pointer_size = sizeof(Variant::ValidatedOperatorEvaluator) / sizeof(*(opcodes.ptr()));
  567. for (int i = 0; i < _pointer_size; i++) {
  568. append(0); // Space for function pointer.
  569. }
  570. }
  571. void GDScriptByteCodeGenerator::write_type_test(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) {
  572. switch (p_type.kind) {
  573. case GDScriptDataType::BUILTIN: {
  574. if (p_type.builtin_type == Variant::ARRAY && p_type.has_container_element_type()) {
  575. const GDScriptDataType &element_type = p_type.get_container_element_type();
  576. append_opcode(GDScriptFunction::OPCODE_TYPE_TEST_ARRAY);
  577. append(p_target);
  578. append(p_source);
  579. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  580. append(element_type.builtin_type);
  581. append(element_type.native_type);
  582. } else {
  583. append_opcode(GDScriptFunction::OPCODE_TYPE_TEST_BUILTIN);
  584. append(p_target);
  585. append(p_source);
  586. append(p_type.builtin_type);
  587. }
  588. } break;
  589. case GDScriptDataType::NATIVE: {
  590. append_opcode(GDScriptFunction::OPCODE_TYPE_TEST_NATIVE);
  591. append(p_target);
  592. append(p_source);
  593. append(p_type.native_type);
  594. } break;
  595. case GDScriptDataType::SCRIPT:
  596. case GDScriptDataType::GDSCRIPT: {
  597. const Variant &script = p_type.script_type;
  598. append_opcode(GDScriptFunction::OPCODE_TYPE_TEST_SCRIPT);
  599. append(p_target);
  600. append(p_source);
  601. append(get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  602. } break;
  603. default: {
  604. ERR_PRINT("Compiler bug: unresolved type in type test.");
  605. append_opcode(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  606. append(p_target);
  607. }
  608. }
  609. }
  610. void GDScriptByteCodeGenerator::write_and_left_operand(const Address &p_left_operand) {
  611. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  612. append(p_left_operand);
  613. logic_op_jump_pos1.push_back(opcodes.size());
  614. append(0); // Jump target, will be patched.
  615. }
  616. void GDScriptByteCodeGenerator::write_and_right_operand(const Address &p_right_operand) {
  617. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  618. append(p_right_operand);
  619. logic_op_jump_pos2.push_back(opcodes.size());
  620. append(0); // Jump target, will be patched.
  621. }
  622. void GDScriptByteCodeGenerator::write_end_and(const Address &p_target) {
  623. // If here means both operands are true.
  624. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  625. append(p_target);
  626. // Jump away from the fail condition.
  627. append_opcode(GDScriptFunction::OPCODE_JUMP);
  628. append(opcodes.size() + 3);
  629. // Here it means one of operands is false.
  630. patch_jump(logic_op_jump_pos1.back()->get());
  631. patch_jump(logic_op_jump_pos2.back()->get());
  632. logic_op_jump_pos1.pop_back();
  633. logic_op_jump_pos2.pop_back();
  634. append_opcode(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  635. append(p_target);
  636. }
  637. void GDScriptByteCodeGenerator::write_or_left_operand(const Address &p_left_operand) {
  638. append_opcode(GDScriptFunction::OPCODE_JUMP_IF);
  639. append(p_left_operand);
  640. logic_op_jump_pos1.push_back(opcodes.size());
  641. append(0); // Jump target, will be patched.
  642. }
  643. void GDScriptByteCodeGenerator::write_or_right_operand(const Address &p_right_operand) {
  644. append_opcode(GDScriptFunction::OPCODE_JUMP_IF);
  645. append(p_right_operand);
  646. logic_op_jump_pos2.push_back(opcodes.size());
  647. append(0); // Jump target, will be patched.
  648. }
  649. void GDScriptByteCodeGenerator::write_end_or(const Address &p_target) {
  650. // If here means both operands are false.
  651. append_opcode(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  652. append(p_target);
  653. // Jump away from the success condition.
  654. append_opcode(GDScriptFunction::OPCODE_JUMP);
  655. append(opcodes.size() + 3);
  656. // Here it means one of operands is true.
  657. patch_jump(logic_op_jump_pos1.back()->get());
  658. patch_jump(logic_op_jump_pos2.back()->get());
  659. logic_op_jump_pos1.pop_back();
  660. logic_op_jump_pos2.pop_back();
  661. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  662. append(p_target);
  663. }
  664. void GDScriptByteCodeGenerator::write_start_ternary(const Address &p_target) {
  665. ternary_result.push_back(p_target);
  666. }
  667. void GDScriptByteCodeGenerator::write_ternary_condition(const Address &p_condition) {
  668. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  669. append(p_condition);
  670. ternary_jump_fail_pos.push_back(opcodes.size());
  671. append(0); // Jump target, will be patched.
  672. }
  673. void GDScriptByteCodeGenerator::write_ternary_true_expr(const Address &p_expr) {
  674. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  675. append(ternary_result.back()->get());
  676. append(p_expr);
  677. // Jump away from the false path.
  678. append_opcode(GDScriptFunction::OPCODE_JUMP);
  679. ternary_jump_skip_pos.push_back(opcodes.size());
  680. append(0);
  681. // Fail must jump here.
  682. patch_jump(ternary_jump_fail_pos.back()->get());
  683. ternary_jump_fail_pos.pop_back();
  684. }
  685. void GDScriptByteCodeGenerator::write_ternary_false_expr(const Address &p_expr) {
  686. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  687. append(ternary_result.back()->get());
  688. append(p_expr);
  689. }
  690. void GDScriptByteCodeGenerator::write_end_ternary() {
  691. patch_jump(ternary_jump_skip_pos.back()->get());
  692. ternary_jump_skip_pos.pop_back();
  693. ternary_result.pop_back();
  694. }
  695. void GDScriptByteCodeGenerator::write_set(const Address &p_target, const Address &p_index, const Address &p_source) {
  696. if (HAS_BUILTIN_TYPE(p_target)) {
  697. if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_setter(p_target.type.builtin_type) &&
  698. IS_BUILTIN_TYPE(p_source, Variant::get_indexed_element_type(p_target.type.builtin_type))) {
  699. // Use indexed setter instead.
  700. Variant::ValidatedIndexedSetter setter = Variant::get_member_validated_indexed_setter(p_target.type.builtin_type);
  701. append_opcode(GDScriptFunction::OPCODE_SET_INDEXED_VALIDATED);
  702. append(p_target);
  703. append(p_index);
  704. append(p_source);
  705. append(setter);
  706. return;
  707. } else if (Variant::get_member_validated_keyed_setter(p_target.type.builtin_type)) {
  708. Variant::ValidatedKeyedSetter setter = Variant::get_member_validated_keyed_setter(p_target.type.builtin_type);
  709. append_opcode(GDScriptFunction::OPCODE_SET_KEYED_VALIDATED);
  710. append(p_target);
  711. append(p_index);
  712. append(p_source);
  713. append(setter);
  714. return;
  715. }
  716. }
  717. append_opcode(GDScriptFunction::OPCODE_SET_KEYED);
  718. append(p_target);
  719. append(p_index);
  720. append(p_source);
  721. }
  722. void GDScriptByteCodeGenerator::write_get(const Address &p_target, const Address &p_index, const Address &p_source) {
  723. if (HAS_BUILTIN_TYPE(p_source)) {
  724. if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_getter(p_source.type.builtin_type)) {
  725. // Use indexed getter instead.
  726. Variant::ValidatedIndexedGetter getter = Variant::get_member_validated_indexed_getter(p_source.type.builtin_type);
  727. append_opcode(GDScriptFunction::OPCODE_GET_INDEXED_VALIDATED);
  728. append(p_source);
  729. append(p_index);
  730. append(p_target);
  731. append(getter);
  732. return;
  733. } else if (Variant::get_member_validated_keyed_getter(p_source.type.builtin_type)) {
  734. Variant::ValidatedKeyedGetter getter = Variant::get_member_validated_keyed_getter(p_source.type.builtin_type);
  735. append_opcode(GDScriptFunction::OPCODE_GET_KEYED_VALIDATED);
  736. append(p_source);
  737. append(p_index);
  738. append(p_target);
  739. append(getter);
  740. return;
  741. }
  742. }
  743. append_opcode(GDScriptFunction::OPCODE_GET_KEYED);
  744. append(p_source);
  745. append(p_index);
  746. append(p_target);
  747. }
  748. void GDScriptByteCodeGenerator::write_set_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  749. if (HAS_BUILTIN_TYPE(p_target) && Variant::get_member_validated_setter(p_target.type.builtin_type, p_name) &&
  750. IS_BUILTIN_TYPE(p_source, Variant::get_member_type(p_target.type.builtin_type, p_name))) {
  751. Variant::ValidatedSetter setter = Variant::get_member_validated_setter(p_target.type.builtin_type, p_name);
  752. append_opcode(GDScriptFunction::OPCODE_SET_NAMED_VALIDATED);
  753. append(p_target);
  754. append(p_source);
  755. append(setter);
  756. #ifdef DEBUG_ENABLED
  757. add_debug_name(setter_names, get_setter_pos(setter), p_name);
  758. #endif
  759. return;
  760. }
  761. append_opcode(GDScriptFunction::OPCODE_SET_NAMED);
  762. append(p_target);
  763. append(p_source);
  764. append(p_name);
  765. }
  766. void GDScriptByteCodeGenerator::write_get_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  767. if (HAS_BUILTIN_TYPE(p_source) && Variant::get_member_validated_getter(p_source.type.builtin_type, p_name)) {
  768. Variant::ValidatedGetter getter = Variant::get_member_validated_getter(p_source.type.builtin_type, p_name);
  769. append_opcode(GDScriptFunction::OPCODE_GET_NAMED_VALIDATED);
  770. append(p_source);
  771. append(p_target);
  772. append(getter);
  773. #ifdef DEBUG_ENABLED
  774. add_debug_name(getter_names, get_getter_pos(getter), p_name);
  775. #endif
  776. return;
  777. }
  778. append_opcode(GDScriptFunction::OPCODE_GET_NAMED);
  779. append(p_source);
  780. append(p_target);
  781. append(p_name);
  782. }
  783. void GDScriptByteCodeGenerator::write_set_member(const Address &p_value, const StringName &p_name) {
  784. append_opcode(GDScriptFunction::OPCODE_SET_MEMBER);
  785. append(p_value);
  786. append(p_name);
  787. }
  788. void GDScriptByteCodeGenerator::write_get_member(const Address &p_target, const StringName &p_name) {
  789. append_opcode(GDScriptFunction::OPCODE_GET_MEMBER);
  790. append(p_target);
  791. append(p_name);
  792. }
  793. void GDScriptByteCodeGenerator::write_set_static_variable(const Address &p_value, const Address &p_class, int p_index) {
  794. append_opcode(GDScriptFunction::OPCODE_SET_STATIC_VARIABLE);
  795. append(p_value);
  796. append(p_class);
  797. append(p_index);
  798. }
  799. void GDScriptByteCodeGenerator::write_get_static_variable(const Address &p_target, const Address &p_class, int p_index) {
  800. append_opcode(GDScriptFunction::OPCODE_GET_STATIC_VARIABLE);
  801. append(p_target);
  802. append(p_class);
  803. append(p_index);
  804. }
  805. void GDScriptByteCodeGenerator::write_assign_with_conversion(const Address &p_target, const Address &p_source) {
  806. switch (p_target.type.kind) {
  807. case GDScriptDataType::BUILTIN: {
  808. if (p_target.type.builtin_type == Variant::ARRAY && p_target.type.has_container_element_type()) {
  809. const GDScriptDataType &element_type = p_target.type.get_container_element_type();
  810. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_ARRAY);
  811. append(p_target);
  812. append(p_source);
  813. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  814. append(element_type.builtin_type);
  815. append(element_type.native_type);
  816. } else {
  817. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN);
  818. append(p_target);
  819. append(p_source);
  820. append(p_target.type.builtin_type);
  821. }
  822. } break;
  823. case GDScriptDataType::NATIVE: {
  824. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_target.type.native_type];
  825. Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
  826. class_idx = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  827. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE);
  828. append(p_target);
  829. append(p_source);
  830. append(class_idx);
  831. } break;
  832. case GDScriptDataType::SCRIPT:
  833. case GDScriptDataType::GDSCRIPT: {
  834. Variant script = p_target.type.script_type;
  835. int idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  836. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT);
  837. append(p_target);
  838. append(p_source);
  839. append(idx);
  840. } break;
  841. default: {
  842. ERR_PRINT("Compiler bug: unresolved assign.");
  843. // Shouldn't get here, but fail-safe to a regular assignment
  844. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  845. append(p_target);
  846. append(p_source);
  847. }
  848. }
  849. }
  850. void GDScriptByteCodeGenerator::write_assign(const Address &p_target, const Address &p_source) {
  851. if (p_target.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type == Variant::ARRAY && p_target.type.has_container_element_type()) {
  852. const GDScriptDataType &element_type = p_target.type.get_container_element_type();
  853. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_ARRAY);
  854. append(p_target);
  855. append(p_source);
  856. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  857. append(element_type.builtin_type);
  858. append(element_type.native_type);
  859. } else if (p_target.type.kind == GDScriptDataType::BUILTIN && p_source.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type != p_source.type.builtin_type) {
  860. // Need conversion.
  861. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN);
  862. append(p_target);
  863. append(p_source);
  864. append(p_target.type.builtin_type);
  865. } else {
  866. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  867. append(p_target);
  868. append(p_source);
  869. }
  870. }
  871. void GDScriptByteCodeGenerator::write_assign_true(const Address &p_target) {
  872. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  873. append(p_target);
  874. }
  875. void GDScriptByteCodeGenerator::write_assign_false(const Address &p_target) {
  876. append_opcode(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  877. append(p_target);
  878. }
  879. void GDScriptByteCodeGenerator::write_assign_default_parameter(const Address &p_dst, const Address &p_src, bool p_use_conversion) {
  880. if (p_use_conversion) {
  881. write_assign_with_conversion(p_dst, p_src);
  882. } else {
  883. write_assign(p_dst, p_src);
  884. }
  885. function->default_arguments.push_back(opcodes.size());
  886. }
  887. void GDScriptByteCodeGenerator::write_store_global(const Address &p_dst, int p_global_index) {
  888. append_opcode(GDScriptFunction::OPCODE_STORE_GLOBAL);
  889. append(p_dst);
  890. append(p_global_index);
  891. }
  892. void GDScriptByteCodeGenerator::write_store_named_global(const Address &p_dst, const StringName &p_global) {
  893. append_opcode(GDScriptFunction::OPCODE_STORE_NAMED_GLOBAL);
  894. append(p_dst);
  895. append(p_global);
  896. }
  897. void GDScriptByteCodeGenerator::write_cast(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) {
  898. int index = 0;
  899. switch (p_type.kind) {
  900. case GDScriptDataType::BUILTIN: {
  901. append_opcode(GDScriptFunction::OPCODE_CAST_TO_BUILTIN);
  902. index = p_type.builtin_type;
  903. } break;
  904. case GDScriptDataType::NATIVE: {
  905. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_type.native_type];
  906. Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
  907. append_opcode(GDScriptFunction::OPCODE_CAST_TO_NATIVE);
  908. index = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  909. } break;
  910. case GDScriptDataType::SCRIPT:
  911. case GDScriptDataType::GDSCRIPT: {
  912. Variant script = p_type.script_type;
  913. int idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  914. append_opcode(GDScriptFunction::OPCODE_CAST_TO_SCRIPT);
  915. index = idx;
  916. } break;
  917. default: {
  918. return;
  919. }
  920. }
  921. append(p_source);
  922. append(p_target);
  923. append(index);
  924. }
  925. GDScriptByteCodeGenerator::CallTarget GDScriptByteCodeGenerator::get_call_target(const GDScriptCodeGenerator::Address &p_target, Variant::Type p_type) {
  926. if (p_target.mode == Address::NIL) {
  927. GDScriptDataType type;
  928. if (p_type != Variant::NIL) {
  929. type.has_type = true;
  930. type.kind = GDScriptDataType::BUILTIN;
  931. type.builtin_type = p_type;
  932. }
  933. uint32_t addr = add_temporary(type);
  934. return CallTarget(Address(Address::TEMPORARY, addr, type), true, this);
  935. } else {
  936. return CallTarget(p_target, false, this);
  937. }
  938. }
  939. void GDScriptByteCodeGenerator::write_call(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  940. append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  941. for (int i = 0; i < p_arguments.size(); i++) {
  942. append(p_arguments[i]);
  943. }
  944. append(p_base);
  945. CallTarget ct = get_call_target(p_target);
  946. append(ct.target);
  947. append(p_arguments.size());
  948. append(p_function_name);
  949. ct.cleanup();
  950. }
  951. void GDScriptByteCodeGenerator::write_super_call(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  952. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_SELF_BASE, 1 + p_arguments.size());
  953. for (int i = 0; i < p_arguments.size(); i++) {
  954. append(p_arguments[i]);
  955. }
  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_async(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  963. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
  964. for (int i = 0; i < p_arguments.size(); i++) {
  965. append(p_arguments[i]);
  966. }
  967. append(p_base);
  968. CallTarget ct = get_call_target(p_target);
  969. append(ct.target);
  970. append(p_arguments.size());
  971. append(p_function_name);
  972. ct.cleanup();
  973. }
  974. void GDScriptByteCodeGenerator::write_call_gdscript_utility(const Address &p_target, const StringName &p_function, const Vector<Address> &p_arguments) {
  975. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_GDSCRIPT_UTILITY, 1 + p_arguments.size());
  976. GDScriptUtilityFunctions::FunctionPtr gds_function = GDScriptUtilityFunctions::get_function(p_function);
  977. for (int i = 0; i < p_arguments.size(); i++) {
  978. append(p_arguments[i]);
  979. }
  980. CallTarget ct = get_call_target(p_target);
  981. append(ct.target);
  982. append(p_arguments.size());
  983. append(gds_function);
  984. ct.cleanup();
  985. #ifdef DEBUG_ENABLED
  986. add_debug_name(gds_utilities_names, get_gds_utility_pos(gds_function), p_function);
  987. #endif
  988. }
  989. void GDScriptByteCodeGenerator::write_call_utility(const Address &p_target, const StringName &p_function, const Vector<Address> &p_arguments) {
  990. bool is_validated = true;
  991. if (Variant::is_utility_function_vararg(p_function)) {
  992. is_validated = false; // Vararg needs runtime checks, can't use validated call.
  993. } else if (p_arguments.size() == Variant::get_utility_function_argument_count(p_function)) {
  994. bool all_types_exact = true;
  995. for (int i = 0; i < p_arguments.size(); i++) {
  996. if (!IS_BUILTIN_TYPE(p_arguments[i], Variant::get_utility_function_argument_type(p_function, i))) {
  997. all_types_exact = false;
  998. break;
  999. }
  1000. }
  1001. is_validated = all_types_exact;
  1002. }
  1003. if (is_validated) {
  1004. Variant::Type result_type = Variant::has_utility_function_return_value(p_function) ? Variant::get_utility_function_return_type(p_function) : Variant::NIL;
  1005. CallTarget ct = get_call_target(p_target, result_type);
  1006. Variant::Type temp_type = temporaries[ct.target.address].type;
  1007. if (result_type != temp_type) {
  1008. write_type_adjust(ct.target, result_type);
  1009. }
  1010. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_UTILITY_VALIDATED, 1 + p_arguments.size());
  1011. for (int i = 0; i < p_arguments.size(); i++) {
  1012. append(p_arguments[i]);
  1013. }
  1014. append(ct.target);
  1015. append(p_arguments.size());
  1016. append(Variant::get_validated_utility_function(p_function));
  1017. ct.cleanup();
  1018. #ifdef DEBUG_ENABLED
  1019. add_debug_name(utilities_names, get_utility_pos(Variant::get_validated_utility_function(p_function)), p_function);
  1020. #endif
  1021. } else {
  1022. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_UTILITY, 1 + p_arguments.size());
  1023. for (int i = 0; i < p_arguments.size(); i++) {
  1024. append(p_arguments[i]);
  1025. }
  1026. CallTarget ct = get_call_target(p_target);
  1027. append(ct.target);
  1028. append(p_arguments.size());
  1029. append(p_function);
  1030. ct.cleanup();
  1031. }
  1032. }
  1033. 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) {
  1034. bool is_validated = false;
  1035. // Check if all types are correct.
  1036. if (Variant::is_builtin_method_vararg(p_type, p_method)) {
  1037. is_validated = false; // Vararg needs runtime checks, can't use validated call.
  1038. } else if (p_arguments.size() == Variant::get_builtin_method_argument_count(p_type, p_method)) {
  1039. bool all_types_exact = true;
  1040. for (int i = 0; i < p_arguments.size(); i++) {
  1041. if (!IS_BUILTIN_TYPE(p_arguments[i], Variant::get_builtin_method_argument_type(p_type, p_method, i))) {
  1042. all_types_exact = false;
  1043. break;
  1044. }
  1045. }
  1046. is_validated = all_types_exact;
  1047. }
  1048. if (!is_validated) {
  1049. // Perform regular call.
  1050. if (p_is_static) {
  1051. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_BUILTIN_STATIC, p_arguments.size() + 1);
  1052. for (int i = 0; i < p_arguments.size(); i++) {
  1053. append(p_arguments[i]);
  1054. }
  1055. CallTarget ct = get_call_target(p_target);
  1056. append(ct.target);
  1057. append(p_type);
  1058. append(p_method);
  1059. append(p_arguments.size());
  1060. ct.cleanup();
  1061. } else {
  1062. write_call(p_target, p_base, p_method, p_arguments);
  1063. }
  1064. return;
  1065. }
  1066. Variant::Type result_type = Variant::get_builtin_method_return_type(p_type, p_method);
  1067. CallTarget ct = get_call_target(p_target, result_type);
  1068. Variant::Type temp_type = temporaries[ct.target.address].type;
  1069. if (result_type != temp_type) {
  1070. write_type_adjust(ct.target, result_type);
  1071. }
  1072. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_BUILTIN_TYPE_VALIDATED, 2 + p_arguments.size());
  1073. for (int i = 0; i < p_arguments.size(); i++) {
  1074. append(p_arguments[i]);
  1075. }
  1076. append(p_base);
  1077. append(ct.target);
  1078. append(p_arguments.size());
  1079. append(Variant::get_validated_builtin_method(p_type, p_method));
  1080. ct.cleanup();
  1081. #ifdef DEBUG_ENABLED
  1082. add_debug_name(builtin_methods_names, get_builtin_method_pos(Variant::get_validated_builtin_method(p_type, p_method)), p_method);
  1083. #endif
  1084. }
  1085. 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) {
  1086. write_call_builtin_type(p_target, p_base, p_type, p_method, false, p_arguments);
  1087. }
  1088. void GDScriptByteCodeGenerator::write_call_builtin_type_static(const Address &p_target, Variant::Type p_type, const StringName &p_method, const Vector<Address> &p_arguments) {
  1089. write_call_builtin_type(p_target, Address(), p_type, p_method, true, p_arguments);
  1090. }
  1091. void GDScriptByteCodeGenerator::write_call_native_static(const Address &p_target, const StringName &p_class, const StringName &p_method, const Vector<Address> &p_arguments) {
  1092. bool is_validated = false;
  1093. MethodBind *method = ClassDB::get_method(p_class, p_method);
  1094. if (!is_validated) {
  1095. // Perform regular call.
  1096. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_NATIVE_STATIC, p_arguments.size() + 1);
  1097. for (int i = 0; i < p_arguments.size(); i++) {
  1098. append(p_arguments[i]);
  1099. }
  1100. CallTarget ct = get_call_target(p_target);
  1101. append(ct.target);
  1102. append(method);
  1103. append(p_arguments.size());
  1104. ct.cleanup();
  1105. return;
  1106. }
  1107. }
  1108. void GDScriptByteCodeGenerator::write_call_method_bind(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) {
  1109. append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL_METHOD_BIND : GDScriptFunction::OPCODE_CALL_METHOD_BIND_RET, 2 + p_arguments.size());
  1110. for (int i = 0; i < p_arguments.size(); i++) {
  1111. append(p_arguments[i]);
  1112. }
  1113. CallTarget ct = get_call_target(p_target);
  1114. append(p_base);
  1115. append(ct.target);
  1116. append(p_arguments.size());
  1117. append(p_method);
  1118. ct.cleanup();
  1119. }
  1120. void GDScriptByteCodeGenerator::write_call_ptrcall(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) {
  1121. #define CASE_TYPE(m_type) \
  1122. case Variant::m_type: \
  1123. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_PTRCALL_##m_type, 2 + p_arguments.size()); \
  1124. break
  1125. bool is_ptrcall = true;
  1126. if (p_method->has_return()) {
  1127. MethodInfo info;
  1128. ClassDB::get_method_info(p_method->get_instance_class(), p_method->get_name(), &info);
  1129. switch (info.return_val.type) {
  1130. CASE_TYPE(BOOL);
  1131. CASE_TYPE(INT);
  1132. CASE_TYPE(FLOAT);
  1133. CASE_TYPE(STRING);
  1134. CASE_TYPE(VECTOR2);
  1135. CASE_TYPE(VECTOR2I);
  1136. CASE_TYPE(RECT2);
  1137. CASE_TYPE(RECT2I);
  1138. CASE_TYPE(VECTOR3);
  1139. CASE_TYPE(VECTOR3I);
  1140. CASE_TYPE(TRANSFORM2D);
  1141. CASE_TYPE(PLANE);
  1142. CASE_TYPE(AABB);
  1143. CASE_TYPE(BASIS);
  1144. CASE_TYPE(TRANSFORM3D);
  1145. CASE_TYPE(COLOR);
  1146. CASE_TYPE(STRING_NAME);
  1147. CASE_TYPE(NODE_PATH);
  1148. CASE_TYPE(RID);
  1149. CASE_TYPE(QUATERNION);
  1150. CASE_TYPE(OBJECT);
  1151. CASE_TYPE(CALLABLE);
  1152. CASE_TYPE(SIGNAL);
  1153. CASE_TYPE(DICTIONARY);
  1154. CASE_TYPE(ARRAY);
  1155. CASE_TYPE(PACKED_BYTE_ARRAY);
  1156. CASE_TYPE(PACKED_INT32_ARRAY);
  1157. CASE_TYPE(PACKED_INT64_ARRAY);
  1158. CASE_TYPE(PACKED_FLOAT32_ARRAY);
  1159. CASE_TYPE(PACKED_FLOAT64_ARRAY);
  1160. CASE_TYPE(PACKED_STRING_ARRAY);
  1161. CASE_TYPE(PACKED_VECTOR2_ARRAY);
  1162. CASE_TYPE(PACKED_VECTOR3_ARRAY);
  1163. CASE_TYPE(PACKED_COLOR_ARRAY);
  1164. default:
  1165. append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL_METHOD_BIND : GDScriptFunction::OPCODE_CALL_METHOD_BIND_RET, 2 + p_arguments.size());
  1166. is_ptrcall = false;
  1167. break;
  1168. }
  1169. } else {
  1170. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_PTRCALL_NO_RETURN, 2 + p_arguments.size());
  1171. }
  1172. for (int i = 0; i < p_arguments.size(); i++) {
  1173. append(p_arguments[i]);
  1174. }
  1175. append(p_base);
  1176. CallTarget ct = get_call_target(p_target);
  1177. append(ct.target);
  1178. append(p_arguments.size());
  1179. append(p_method);
  1180. ct.cleanup();
  1181. if (is_ptrcall) {
  1182. alloc_ptrcall(p_arguments.size());
  1183. }
  1184. #undef CASE_TYPE
  1185. }
  1186. void GDScriptByteCodeGenerator::write_call_self(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  1187. append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  1188. for (int i = 0; i < p_arguments.size(); i++) {
  1189. append(p_arguments[i]);
  1190. }
  1191. append(GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  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_self_async(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  1199. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
  1200. for (int i = 0; i < p_arguments.size(); i++) {
  1201. append(p_arguments[i]);
  1202. }
  1203. append(GDScriptFunction::ADDR_SELF);
  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_call_script_function(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  1211. append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  1212. for (int i = 0; i < p_arguments.size(); i++) {
  1213. append(p_arguments[i]);
  1214. }
  1215. append(p_base);
  1216. CallTarget ct = get_call_target(p_target);
  1217. append(ct.target);
  1218. append(p_arguments.size());
  1219. append(p_function_name);
  1220. ct.cleanup();
  1221. }
  1222. void GDScriptByteCodeGenerator::write_lambda(const Address &p_target, GDScriptFunction *p_function, const Vector<Address> &p_captures, bool p_use_self) {
  1223. append_opcode_and_argcount(p_use_self ? GDScriptFunction::OPCODE_CREATE_SELF_LAMBDA : GDScriptFunction::OPCODE_CREATE_LAMBDA, 1 + p_captures.size());
  1224. for (int i = 0; i < p_captures.size(); i++) {
  1225. append(p_captures[i]);
  1226. }
  1227. CallTarget ct = get_call_target(p_target);
  1228. append(ct.target);
  1229. append(p_captures.size());
  1230. append(p_function);
  1231. ct.cleanup();
  1232. }
  1233. void GDScriptByteCodeGenerator::write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) {
  1234. // Try to find an appropriate constructor.
  1235. bool all_have_type = true;
  1236. Vector<Variant::Type> arg_types;
  1237. for (int i = 0; i < p_arguments.size(); i++) {
  1238. if (!HAS_BUILTIN_TYPE(p_arguments[i])) {
  1239. all_have_type = false;
  1240. break;
  1241. }
  1242. arg_types.push_back(p_arguments[i].type.builtin_type);
  1243. }
  1244. if (all_have_type) {
  1245. int valid_constructor = -1;
  1246. for (int i = 0; i < Variant::get_constructor_count(p_type); i++) {
  1247. if (Variant::get_constructor_argument_count(p_type, i) != p_arguments.size()) {
  1248. continue;
  1249. }
  1250. int types_correct = true;
  1251. for (int j = 0; j < arg_types.size(); j++) {
  1252. if (arg_types[j] != Variant::get_constructor_argument_type(p_type, i, j)) {
  1253. types_correct = false;
  1254. break;
  1255. }
  1256. }
  1257. if (types_correct) {
  1258. valid_constructor = i;
  1259. break;
  1260. }
  1261. }
  1262. if (valid_constructor >= 0) {
  1263. append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_VALIDATED, 1 + p_arguments.size());
  1264. for (int i = 0; i < p_arguments.size(); i++) {
  1265. append(p_arguments[i]);
  1266. }
  1267. CallTarget ct = get_call_target(p_target);
  1268. append(ct.target);
  1269. append(p_arguments.size());
  1270. append(Variant::get_validated_constructor(p_type, valid_constructor));
  1271. ct.cleanup();
  1272. #ifdef DEBUG_ENABLED
  1273. add_debug_name(constructors_names, get_constructor_pos(Variant::get_validated_constructor(p_type, valid_constructor)), Variant::get_type_name(p_type));
  1274. #endif
  1275. return;
  1276. }
  1277. }
  1278. append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT, 1 + p_arguments.size());
  1279. for (int i = 0; i < p_arguments.size(); i++) {
  1280. append(p_arguments[i]);
  1281. }
  1282. CallTarget ct = get_call_target(p_target);
  1283. append(ct.target);
  1284. append(p_arguments.size());
  1285. append(p_type);
  1286. ct.cleanup();
  1287. }
  1288. void GDScriptByteCodeGenerator::write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) {
  1289. append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_ARRAY, 1 + p_arguments.size());
  1290. for (int i = 0; i < p_arguments.size(); i++) {
  1291. append(p_arguments[i]);
  1292. }
  1293. CallTarget ct = get_call_target(p_target);
  1294. append(ct.target);
  1295. append(p_arguments.size());
  1296. ct.cleanup();
  1297. }
  1298. void GDScriptByteCodeGenerator::write_construct_typed_array(const Address &p_target, const GDScriptDataType &p_element_type, const Vector<Address> &p_arguments) {
  1299. append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_TYPED_ARRAY, 2 + p_arguments.size());
  1300. for (int i = 0; i < p_arguments.size(); i++) {
  1301. append(p_arguments[i]);
  1302. }
  1303. CallTarget ct = get_call_target(p_target);
  1304. append(ct.target);
  1305. append(get_constant_pos(p_element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1306. append(p_arguments.size());
  1307. append(p_element_type.builtin_type);
  1308. append(p_element_type.native_type);
  1309. ct.cleanup();
  1310. }
  1311. void GDScriptByteCodeGenerator::write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) {
  1312. append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY, 1 + p_arguments.size());
  1313. for (int i = 0; i < p_arguments.size(); i++) {
  1314. append(p_arguments[i]);
  1315. }
  1316. CallTarget ct = get_call_target(p_target);
  1317. append(ct.target);
  1318. append(p_arguments.size() / 2); // This is number of key-value pairs, so only half of actual arguments.
  1319. ct.cleanup();
  1320. }
  1321. void GDScriptByteCodeGenerator::write_await(const Address &p_target, const Address &p_operand) {
  1322. append_opcode(GDScriptFunction::OPCODE_AWAIT);
  1323. append(p_operand);
  1324. append_opcode(GDScriptFunction::OPCODE_AWAIT_RESUME);
  1325. append(p_target);
  1326. }
  1327. void GDScriptByteCodeGenerator::write_if(const Address &p_condition) {
  1328. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1329. append(p_condition);
  1330. if_jmp_addrs.push_back(opcodes.size());
  1331. append(0); // Jump destination, will be patched.
  1332. }
  1333. void GDScriptByteCodeGenerator::write_else() {
  1334. append_opcode(GDScriptFunction::OPCODE_JUMP); // Jump from true if block;
  1335. int else_jmp_addr = opcodes.size();
  1336. append(0); // Jump destination, will be patched.
  1337. patch_jump(if_jmp_addrs.back()->get());
  1338. if_jmp_addrs.pop_back();
  1339. if_jmp_addrs.push_back(else_jmp_addr);
  1340. }
  1341. void GDScriptByteCodeGenerator::write_endif() {
  1342. patch_jump(if_jmp_addrs.back()->get());
  1343. if_jmp_addrs.pop_back();
  1344. }
  1345. void GDScriptByteCodeGenerator::write_jump_if_shared(const Address &p_value) {
  1346. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_SHARED);
  1347. append(p_value);
  1348. if_jmp_addrs.push_back(opcodes.size());
  1349. append(0); // Jump destination, will be patched.
  1350. }
  1351. void GDScriptByteCodeGenerator::write_end_jump_if_shared() {
  1352. patch_jump(if_jmp_addrs.back()->get());
  1353. if_jmp_addrs.pop_back();
  1354. }
  1355. void GDScriptByteCodeGenerator::start_for(const GDScriptDataType &p_iterator_type, const GDScriptDataType &p_list_type) {
  1356. Address counter(Address::LOCAL_VARIABLE, add_local("@counter_pos", p_iterator_type), p_iterator_type);
  1357. Address container(Address::LOCAL_VARIABLE, add_local("@container_pos", p_list_type), p_list_type);
  1358. // Store state.
  1359. for_counter_variables.push_back(counter);
  1360. for_container_variables.push_back(container);
  1361. }
  1362. void GDScriptByteCodeGenerator::write_for_assignment(const Address &p_variable, const Address &p_list) {
  1363. const Address &container = for_container_variables.back()->get();
  1364. // Assign container.
  1365. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  1366. append(container);
  1367. append(p_list);
  1368. for_iterator_variables.push_back(p_variable);
  1369. }
  1370. void GDScriptByteCodeGenerator::write_for() {
  1371. const Address &iterator = for_iterator_variables.back()->get();
  1372. const Address &counter = for_counter_variables.back()->get();
  1373. const Address &container = for_container_variables.back()->get();
  1374. current_breaks_to_patch.push_back(List<int>());
  1375. GDScriptFunction::Opcode begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN;
  1376. GDScriptFunction::Opcode iterate_opcode = GDScriptFunction::OPCODE_ITERATE;
  1377. if (container.type.has_type) {
  1378. if (container.type.kind == GDScriptDataType::BUILTIN) {
  1379. switch (container.type.builtin_type) {
  1380. case Variant::INT:
  1381. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_INT;
  1382. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_INT;
  1383. break;
  1384. case Variant::FLOAT:
  1385. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_FLOAT;
  1386. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_FLOAT;
  1387. break;
  1388. case Variant::VECTOR2:
  1389. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2;
  1390. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2;
  1391. break;
  1392. case Variant::VECTOR2I:
  1393. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2I;
  1394. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2I;
  1395. break;
  1396. case Variant::VECTOR3:
  1397. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3;
  1398. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3;
  1399. break;
  1400. case Variant::VECTOR3I:
  1401. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3I;
  1402. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3I;
  1403. break;
  1404. case Variant::STRING:
  1405. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_STRING;
  1406. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_STRING;
  1407. break;
  1408. case Variant::DICTIONARY:
  1409. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_DICTIONARY;
  1410. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_DICTIONARY;
  1411. break;
  1412. case Variant::ARRAY:
  1413. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_ARRAY;
  1414. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_ARRAY;
  1415. break;
  1416. case Variant::PACKED_BYTE_ARRAY:
  1417. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_BYTE_ARRAY;
  1418. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_BYTE_ARRAY;
  1419. break;
  1420. case Variant::PACKED_INT32_ARRAY:
  1421. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT32_ARRAY;
  1422. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT32_ARRAY;
  1423. break;
  1424. case Variant::PACKED_INT64_ARRAY:
  1425. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT64_ARRAY;
  1426. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT64_ARRAY;
  1427. break;
  1428. case Variant::PACKED_FLOAT32_ARRAY:
  1429. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT32_ARRAY;
  1430. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT32_ARRAY;
  1431. break;
  1432. case Variant::PACKED_FLOAT64_ARRAY:
  1433. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT64_ARRAY;
  1434. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT64_ARRAY;
  1435. break;
  1436. case Variant::PACKED_STRING_ARRAY:
  1437. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_STRING_ARRAY;
  1438. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_STRING_ARRAY;
  1439. break;
  1440. case Variant::PACKED_VECTOR2_ARRAY:
  1441. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR2_ARRAY;
  1442. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR2_ARRAY;
  1443. break;
  1444. case Variant::PACKED_VECTOR3_ARRAY:
  1445. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR3_ARRAY;
  1446. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR3_ARRAY;
  1447. break;
  1448. case Variant::PACKED_COLOR_ARRAY:
  1449. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_COLOR_ARRAY;
  1450. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_COLOR_ARRAY;
  1451. break;
  1452. default:
  1453. break;
  1454. }
  1455. } else {
  1456. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_OBJECT;
  1457. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_OBJECT;
  1458. }
  1459. }
  1460. // Begin loop.
  1461. append_opcode(begin_opcode);
  1462. append(counter);
  1463. append(container);
  1464. append(iterator);
  1465. for_jmp_addrs.push_back(opcodes.size());
  1466. append(0); // End of loop address, will be patched.
  1467. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1468. append(opcodes.size() + 6); // Skip over 'continue' code.
  1469. // Next iteration.
  1470. int continue_addr = opcodes.size();
  1471. continue_addrs.push_back(continue_addr);
  1472. append_opcode(iterate_opcode);
  1473. append(counter);
  1474. append(container);
  1475. append(iterator);
  1476. for_jmp_addrs.push_back(opcodes.size());
  1477. append(0); // Jump destination, will be patched.
  1478. }
  1479. void GDScriptByteCodeGenerator::write_endfor() {
  1480. // Jump back to loop check.
  1481. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1482. append(continue_addrs.back()->get());
  1483. continue_addrs.pop_back();
  1484. // Patch end jumps (two of them).
  1485. for (int i = 0; i < 2; i++) {
  1486. patch_jump(for_jmp_addrs.back()->get());
  1487. for_jmp_addrs.pop_back();
  1488. }
  1489. // Patch break statements.
  1490. for (const int &E : current_breaks_to_patch.back()->get()) {
  1491. patch_jump(E);
  1492. }
  1493. current_breaks_to_patch.pop_back();
  1494. // Pop state.
  1495. for_iterator_variables.pop_back();
  1496. for_counter_variables.pop_back();
  1497. for_container_variables.pop_back();
  1498. }
  1499. void GDScriptByteCodeGenerator::start_while_condition() {
  1500. current_breaks_to_patch.push_back(List<int>());
  1501. continue_addrs.push_back(opcodes.size());
  1502. }
  1503. void GDScriptByteCodeGenerator::write_while(const Address &p_condition) {
  1504. // Condition check.
  1505. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1506. append(p_condition);
  1507. while_jmp_addrs.push_back(opcodes.size());
  1508. append(0); // End of loop address, will be patched.
  1509. }
  1510. void GDScriptByteCodeGenerator::write_endwhile() {
  1511. // Jump back to loop check.
  1512. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1513. append(continue_addrs.back()->get());
  1514. continue_addrs.pop_back();
  1515. // Patch end jump.
  1516. patch_jump(while_jmp_addrs.back()->get());
  1517. while_jmp_addrs.pop_back();
  1518. // Patch break statements.
  1519. for (const int &E : current_breaks_to_patch.back()->get()) {
  1520. patch_jump(E);
  1521. }
  1522. current_breaks_to_patch.pop_back();
  1523. }
  1524. void GDScriptByteCodeGenerator::write_break() {
  1525. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1526. current_breaks_to_patch.back()->get().push_back(opcodes.size());
  1527. append(0);
  1528. }
  1529. void GDScriptByteCodeGenerator::write_continue() {
  1530. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1531. append(continue_addrs.back()->get());
  1532. }
  1533. void GDScriptByteCodeGenerator::write_breakpoint() {
  1534. append_opcode(GDScriptFunction::OPCODE_BREAKPOINT);
  1535. }
  1536. void GDScriptByteCodeGenerator::write_newline(int p_line) {
  1537. append_opcode(GDScriptFunction::OPCODE_LINE);
  1538. append(p_line);
  1539. current_line = p_line;
  1540. }
  1541. void GDScriptByteCodeGenerator::write_return(const Address &p_return_value) {
  1542. if (!function->return_type.has_type || p_return_value.type.has_type) {
  1543. // Either the function is untyped or the return value is also typed.
  1544. // If this is a typed function, then we need to check for potential conversions.
  1545. if (function->return_type.has_type) {
  1546. if (function->return_type.kind == GDScriptDataType::BUILTIN && function->return_type.builtin_type == Variant::ARRAY && function->return_type.has_container_element_type()) {
  1547. // Typed array.
  1548. const GDScriptDataType &element_type = function->return_type.get_container_element_type();
  1549. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_ARRAY);
  1550. append(p_return_value);
  1551. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1552. append(element_type.builtin_type);
  1553. append(element_type.native_type);
  1554. } 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) {
  1555. // Add conversion.
  1556. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_BUILTIN);
  1557. append(p_return_value);
  1558. append(function->return_type.builtin_type);
  1559. } else {
  1560. // Just assign.
  1561. append_opcode(GDScriptFunction::OPCODE_RETURN);
  1562. append(p_return_value);
  1563. }
  1564. } else {
  1565. append_opcode(GDScriptFunction::OPCODE_RETURN);
  1566. append(p_return_value);
  1567. }
  1568. } else {
  1569. switch (function->return_type.kind) {
  1570. case GDScriptDataType::BUILTIN: {
  1571. if (function->return_type.builtin_type == Variant::ARRAY && function->return_type.has_container_element_type()) {
  1572. const GDScriptDataType &element_type = function->return_type.get_container_element_type();
  1573. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_ARRAY);
  1574. append(p_return_value);
  1575. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1576. append(element_type.builtin_type);
  1577. append(element_type.native_type);
  1578. } else {
  1579. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_BUILTIN);
  1580. append(p_return_value);
  1581. append(function->return_type.builtin_type);
  1582. }
  1583. } break;
  1584. case GDScriptDataType::NATIVE: {
  1585. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_NATIVE);
  1586. append(p_return_value);
  1587. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[function->return_type.native_type];
  1588. Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
  1589. class_idx = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  1590. append(class_idx);
  1591. } break;
  1592. case GDScriptDataType::GDSCRIPT:
  1593. case GDScriptDataType::SCRIPT: {
  1594. Variant script = function->return_type.script_type;
  1595. int script_idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  1596. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_SCRIPT);
  1597. append(p_return_value);
  1598. append(script_idx);
  1599. } break;
  1600. default: {
  1601. ERR_PRINT("Compiler bug: unresolved return.");
  1602. // Shouldn't get here, but fail-safe to a regular return;
  1603. append_opcode(GDScriptFunction::OPCODE_RETURN);
  1604. append(p_return_value);
  1605. } break;
  1606. }
  1607. }
  1608. }
  1609. void GDScriptByteCodeGenerator::write_assert(const Address &p_test, const Address &p_message) {
  1610. append_opcode(GDScriptFunction::OPCODE_ASSERT);
  1611. append(p_test);
  1612. append(p_message);
  1613. }
  1614. void GDScriptByteCodeGenerator::start_block() {
  1615. push_stack_identifiers();
  1616. }
  1617. void GDScriptByteCodeGenerator::end_block() {
  1618. pop_stack_identifiers();
  1619. }
  1620. void GDScriptByteCodeGenerator::clean_temporaries() {
  1621. List<int>::Element *E = temporaries_pending_clear.front();
  1622. while (E) {
  1623. // The temporary may have been re-used as something else than an object
  1624. // since it was added to the list. In that case, there's no need to clear it.
  1625. int slot_idx = E->get();
  1626. const StackSlot &slot = temporaries[slot_idx];
  1627. if (slot.type == Variant::NIL) {
  1628. write_assign_false(Address(Address::TEMPORARY, slot_idx));
  1629. }
  1630. List<int>::Element *next = E->next();
  1631. E->erase();
  1632. E = next;
  1633. }
  1634. }
  1635. GDScriptByteCodeGenerator::~GDScriptByteCodeGenerator() {
  1636. if (!ended && function != nullptr) {
  1637. memdelete(function);
  1638. }
  1639. }