gdscript_byte_codegen.cpp 58 KB

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