gdscript_byte_codegen.cpp 58 KB

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