gdscript_byte_codegen.cpp 66 KB

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