gdscript_byte_codegen.cpp 62 KB

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