gdscript_byte_codegen.cpp 65 KB

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