gdscript_byte_codegen.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  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 = increase_stack();
  46. add_stack_identifier(p_name, stack_pos);
  47. return stack_pos;
  48. }
  49. uint32_t GDScriptByteCodeGenerator::add_local_constant(const StringName &p_name, const Variant &p_constant) {
  50. int index = add_or_get_constant(p_constant);
  51. local_constants[p_name] = index;
  52. return index;
  53. }
  54. uint32_t GDScriptByteCodeGenerator::add_or_get_constant(const Variant &p_constant) {
  55. return get_constant_pos(p_constant);
  56. }
  57. uint32_t GDScriptByteCodeGenerator::add_or_get_name(const StringName &p_name) {
  58. return get_name_map_pos(p_name);
  59. }
  60. uint32_t GDScriptByteCodeGenerator::add_temporary() {
  61. current_temporaries++;
  62. int idx = increase_stack();
  63. #ifdef DEBUG_ENABLED
  64. temp_stack.push_back(idx);
  65. #endif
  66. return idx;
  67. }
  68. void GDScriptByteCodeGenerator::pop_temporary() {
  69. ERR_FAIL_COND(current_temporaries == 0);
  70. current_stack_size--;
  71. #ifdef DEBUG_ENABLED
  72. if (temp_stack.back()->get() != current_stack_size) {
  73. ERR_PRINT("Mismatched popping of temporary value");
  74. }
  75. temp_stack.pop_back();
  76. #endif
  77. current_temporaries--;
  78. }
  79. void GDScriptByteCodeGenerator::start_parameters() {
  80. if (function->_default_arg_count > 0) {
  81. append(GDScriptFunction::OPCODE_JUMP_TO_DEF_ARGUMENT);
  82. function->default_arguments.push_back(opcodes.size());
  83. }
  84. }
  85. void GDScriptByteCodeGenerator::end_parameters() {
  86. function->default_arguments.reverse();
  87. }
  88. void GDScriptByteCodeGenerator::write_start(GDScript *p_script, const StringName &p_function_name, bool p_static, MultiplayerAPI::RPCMode p_rpc_mode, const GDScriptDataType &p_return_type) {
  89. function = memnew(GDScriptFunction);
  90. debug_stack = EngineDebugger::is_active();
  91. function->name = p_function_name;
  92. function->_script = p_script;
  93. function->source = p_script->get_path();
  94. #ifdef DEBUG_ENABLED
  95. function->func_cname = (String(function->source) + " - " + String(p_function_name)).utf8();
  96. function->_func_cname = function->func_cname.get_data();
  97. #endif
  98. function->_static = p_static;
  99. function->return_type = p_return_type;
  100. function->rpc_mode = p_rpc_mode;
  101. function->_argument_count = 0;
  102. }
  103. GDScriptFunction *GDScriptByteCodeGenerator::write_end() {
  104. #ifdef DEBUG_ENABLED
  105. if (current_temporaries != 0) {
  106. ERR_PRINT("Non-zero temporary variables at end of function: " + itos(current_temporaries));
  107. }
  108. #endif
  109. append(GDScriptFunction::OPCODE_END, 0);
  110. if (constant_map.size()) {
  111. function->_constant_count = constant_map.size();
  112. function->constants.resize(constant_map.size());
  113. function->_constants_ptr = function->constants.ptrw();
  114. const Variant *K = nullptr;
  115. while ((K = constant_map.next(K))) {
  116. int idx = constant_map[*K];
  117. function->constants.write[idx] = *K;
  118. }
  119. } else {
  120. function->_constants_ptr = nullptr;
  121. function->_constant_count = 0;
  122. }
  123. if (name_map.size()) {
  124. function->global_names.resize(name_map.size());
  125. function->_global_names_ptr = &function->global_names[0];
  126. for (Map<StringName, int>::Element *E = name_map.front(); E; E = E->next()) {
  127. function->global_names.write[E->get()] = E->key();
  128. }
  129. function->_global_names_count = function->global_names.size();
  130. } else {
  131. function->_global_names_ptr = nullptr;
  132. function->_global_names_count = 0;
  133. }
  134. if (opcodes.size()) {
  135. function->code = opcodes;
  136. function->_code_ptr = &function->code[0];
  137. function->_code_size = opcodes.size();
  138. } else {
  139. function->_code_ptr = nullptr;
  140. function->_code_size = 0;
  141. }
  142. if (function->default_arguments.size()) {
  143. function->_default_arg_count = function->default_arguments.size() - 1;
  144. function->_default_arg_ptr = &function->default_arguments[0];
  145. } else {
  146. function->_default_arg_count = 0;
  147. function->_default_arg_ptr = nullptr;
  148. }
  149. if (operator_func_map.size()) {
  150. function->operator_funcs.resize(operator_func_map.size());
  151. function->_operator_funcs_count = function->operator_funcs.size();
  152. function->_operator_funcs_ptr = function->operator_funcs.ptr();
  153. for (const Map<Variant::ValidatedOperatorEvaluator, int>::Element *E = operator_func_map.front(); E; E = E->next()) {
  154. function->operator_funcs.write[E->get()] = E->key();
  155. }
  156. } else {
  157. function->_operator_funcs_count = 0;
  158. function->_operator_funcs_ptr = nullptr;
  159. }
  160. if (setters_map.size()) {
  161. function->setters.resize(setters_map.size());
  162. function->_setters_count = function->setters.size();
  163. function->_setters_ptr = function->setters.ptr();
  164. for (const Map<Variant::ValidatedSetter, int>::Element *E = setters_map.front(); E; E = E->next()) {
  165. function->setters.write[E->get()] = E->key();
  166. }
  167. } else {
  168. function->_setters_count = 0;
  169. function->_setters_ptr = nullptr;
  170. }
  171. if (getters_map.size()) {
  172. function->getters.resize(getters_map.size());
  173. function->_getters_count = function->getters.size();
  174. function->_getters_ptr = function->getters.ptr();
  175. for (const Map<Variant::ValidatedGetter, int>::Element *E = getters_map.front(); E; E = E->next()) {
  176. function->getters.write[E->get()] = E->key();
  177. }
  178. } else {
  179. function->_getters_count = 0;
  180. function->_getters_ptr = nullptr;
  181. }
  182. if (keyed_setters_map.size()) {
  183. function->keyed_setters.resize(keyed_setters_map.size());
  184. function->_keyed_setters_count = function->keyed_setters.size();
  185. function->_keyed_setters_ptr = function->keyed_setters.ptr();
  186. for (const Map<Variant::ValidatedKeyedSetter, int>::Element *E = keyed_setters_map.front(); E; E = E->next()) {
  187. function->keyed_setters.write[E->get()] = E->key();
  188. }
  189. } else {
  190. function->_keyed_setters_count = 0;
  191. function->_keyed_setters_ptr = nullptr;
  192. }
  193. if (keyed_getters_map.size()) {
  194. function->keyed_getters.resize(keyed_getters_map.size());
  195. function->_keyed_getters_count = function->keyed_getters.size();
  196. function->_keyed_getters_ptr = function->keyed_getters.ptr();
  197. for (const Map<Variant::ValidatedKeyedGetter, int>::Element *E = keyed_getters_map.front(); E; E = E->next()) {
  198. function->keyed_getters.write[E->get()] = E->key();
  199. }
  200. } else {
  201. function->_keyed_getters_count = 0;
  202. function->_keyed_getters_ptr = nullptr;
  203. }
  204. if (indexed_setters_map.size()) {
  205. function->indexed_setters.resize(indexed_setters_map.size());
  206. function->_indexed_setters_count = function->indexed_setters.size();
  207. function->_indexed_setters_ptr = function->indexed_setters.ptr();
  208. for (const Map<Variant::ValidatedIndexedSetter, int>::Element *E = indexed_setters_map.front(); E; E = E->next()) {
  209. function->indexed_setters.write[E->get()] = E->key();
  210. }
  211. } else {
  212. function->_indexed_setters_count = 0;
  213. function->_indexed_setters_ptr = nullptr;
  214. }
  215. if (indexed_getters_map.size()) {
  216. function->indexed_getters.resize(indexed_getters_map.size());
  217. function->_indexed_getters_count = function->indexed_getters.size();
  218. function->_indexed_getters_ptr = function->indexed_getters.ptr();
  219. for (const Map<Variant::ValidatedIndexedGetter, int>::Element *E = indexed_getters_map.front(); E; E = E->next()) {
  220. function->indexed_getters.write[E->get()] = E->key();
  221. }
  222. } else {
  223. function->_indexed_getters_count = 0;
  224. function->_indexed_getters_ptr = nullptr;
  225. }
  226. if (builtin_method_map.size()) {
  227. function->builtin_methods.resize(builtin_method_map.size());
  228. function->_builtin_methods_ptr = function->builtin_methods.ptr();
  229. function->_builtin_methods_count = builtin_method_map.size();
  230. for (const Map<Variant::ValidatedBuiltInMethod, int>::Element *E = builtin_method_map.front(); E; E = E->next()) {
  231. function->builtin_methods.write[E->get()] = E->key();
  232. }
  233. } else {
  234. function->_builtin_methods_ptr = nullptr;
  235. function->_builtin_methods_count = 0;
  236. }
  237. if (constructors_map.size()) {
  238. function->constructors.resize(constructors_map.size());
  239. function->_constructors_ptr = function->constructors.ptr();
  240. function->_constructors_count = constructors_map.size();
  241. for (const Map<Variant::ValidatedConstructor, int>::Element *E = constructors_map.front(); E; E = E->next()) {
  242. function->constructors.write[E->get()] = E->key();
  243. }
  244. } else {
  245. function->_constructors_ptr = nullptr;
  246. function->_constructors_count = 0;
  247. }
  248. if (utilities_map.size()) {
  249. function->utilities.resize(utilities_map.size());
  250. function->_utilities_ptr = function->utilities.ptr();
  251. function->_utilities_count = utilities_map.size();
  252. for (const Map<Variant::ValidatedUtilityFunction, int>::Element *E = utilities_map.front(); E; E = E->next()) {
  253. function->utilities.write[E->get()] = E->key();
  254. }
  255. } else {
  256. function->_utilities_ptr = nullptr;
  257. function->_utilities_count = 0;
  258. }
  259. if (gds_utilities_map.size()) {
  260. function->gds_utilities.resize(gds_utilities_map.size());
  261. function->_gds_utilities_ptr = function->gds_utilities.ptr();
  262. function->_gds_utilities_count = gds_utilities_map.size();
  263. for (const Map<GDScriptUtilityFunctions::FunctionPtr, int>::Element *E = gds_utilities_map.front(); E; E = E->next()) {
  264. function->gds_utilities.write[E->get()] = E->key();
  265. }
  266. } else {
  267. function->_gds_utilities_ptr = nullptr;
  268. function->_gds_utilities_count = 0;
  269. }
  270. if (method_bind_map.size()) {
  271. function->methods.resize(method_bind_map.size());
  272. function->_methods_ptr = function->methods.ptrw();
  273. function->_methods_count = method_bind_map.size();
  274. for (const Map<MethodBind *, int>::Element *E = method_bind_map.front(); E; E = E->next()) {
  275. function->methods.write[E->get()] = E->key();
  276. }
  277. } else {
  278. function->_methods_ptr = nullptr;
  279. function->_methods_count = 0;
  280. }
  281. if (debug_stack) {
  282. function->stack_debug = stack_debug;
  283. }
  284. function->_stack_size = stack_max;
  285. function->_instruction_args_size = instr_args_max;
  286. function->_ptrcall_args_size = ptrcall_max;
  287. ended = true;
  288. return function;
  289. }
  290. #ifdef DEBUG_ENABLED
  291. void GDScriptByteCodeGenerator::set_signature(const String &p_signature) {
  292. function->profile.signature = p_signature;
  293. }
  294. #endif
  295. void GDScriptByteCodeGenerator::set_initial_line(int p_line) {
  296. function->_initial_line = p_line;
  297. }
  298. #define HAS_BUILTIN_TYPE(m_var) \
  299. (m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN)
  300. #define IS_BUILTIN_TYPE(m_var, m_type) \
  301. (m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN && m_var.type.builtin_type == m_type)
  302. void GDScriptByteCodeGenerator::write_unary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand) {
  303. if (HAS_BUILTIN_TYPE(p_left_operand)) {
  304. // Gather specific operator.
  305. Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(p_operator, p_left_operand.type.builtin_type, Variant::NIL);
  306. append(GDScriptFunction::OPCODE_OPERATOR_VALIDATED, 3);
  307. append(p_left_operand);
  308. append(Address());
  309. append(p_target);
  310. append(op_func);
  311. return;
  312. }
  313. // No specific types, perform variant evaluation.
  314. append(GDScriptFunction::OPCODE_OPERATOR, 3);
  315. append(p_left_operand);
  316. append(Address());
  317. append(p_target);
  318. append(p_operator);
  319. }
  320. void GDScriptByteCodeGenerator::write_binary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) {
  321. if (HAS_BUILTIN_TYPE(p_left_operand) && HAS_BUILTIN_TYPE(p_right_operand)) {
  322. // Gather specific operator.
  323. Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type);
  324. append(GDScriptFunction::OPCODE_OPERATOR_VALIDATED, 3);
  325. append(p_left_operand);
  326. append(p_right_operand);
  327. append(p_target);
  328. append(op_func);
  329. return;
  330. }
  331. // No specific types, perform variant evaluation.
  332. append(GDScriptFunction::OPCODE_OPERATOR, 3);
  333. append(p_left_operand);
  334. append(p_right_operand);
  335. append(p_target);
  336. append(p_operator);
  337. }
  338. void GDScriptByteCodeGenerator::write_type_test(const Address &p_target, const Address &p_source, const Address &p_type) {
  339. append(GDScriptFunction::OPCODE_EXTENDS_TEST, 3);
  340. append(p_source);
  341. append(p_type);
  342. append(p_target);
  343. }
  344. void GDScriptByteCodeGenerator::write_type_test_builtin(const Address &p_target, const Address &p_source, Variant::Type p_type) {
  345. append(GDScriptFunction::OPCODE_IS_BUILTIN, 2);
  346. append(p_source);
  347. append(p_target);
  348. append(p_type);
  349. }
  350. void GDScriptByteCodeGenerator::write_and_left_operand(const Address &p_left_operand) {
  351. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  352. append(p_left_operand);
  353. logic_op_jump_pos1.push_back(opcodes.size());
  354. append(0); // Jump target, will be patched.
  355. }
  356. void GDScriptByteCodeGenerator::write_and_right_operand(const Address &p_right_operand) {
  357. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  358. append(p_right_operand);
  359. logic_op_jump_pos2.push_back(opcodes.size());
  360. append(0); // Jump target, will be patched.
  361. }
  362. void GDScriptByteCodeGenerator::write_end_and(const Address &p_target) {
  363. // If here means both operands are true.
  364. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  365. append(p_target);
  366. // Jump away from the fail condition.
  367. append(GDScriptFunction::OPCODE_JUMP, 0);
  368. append(opcodes.size() + 3);
  369. // Here it means one of operands is false.
  370. patch_jump(logic_op_jump_pos1.back()->get());
  371. patch_jump(logic_op_jump_pos2.back()->get());
  372. logic_op_jump_pos1.pop_back();
  373. logic_op_jump_pos2.pop_back();
  374. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 1);
  375. append(p_target);
  376. }
  377. void GDScriptByteCodeGenerator::write_or_left_operand(const Address &p_left_operand) {
  378. append(GDScriptFunction::OPCODE_JUMP_IF, 1);
  379. append(p_left_operand);
  380. logic_op_jump_pos1.push_back(opcodes.size());
  381. append(0); // Jump target, will be patched.
  382. }
  383. void GDScriptByteCodeGenerator::write_or_right_operand(const Address &p_right_operand) {
  384. append(GDScriptFunction::OPCODE_JUMP_IF, 1);
  385. append(p_right_operand);
  386. logic_op_jump_pos2.push_back(opcodes.size());
  387. append(0); // Jump target, will be patched.
  388. }
  389. void GDScriptByteCodeGenerator::write_end_or(const Address &p_target) {
  390. // If here means both operands are false.
  391. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 1);
  392. append(p_target);
  393. // Jump away from the success condition.
  394. append(GDScriptFunction::OPCODE_JUMP, 0);
  395. append(opcodes.size() + 3);
  396. // Here it means one of operands is true.
  397. patch_jump(logic_op_jump_pos1.back()->get());
  398. patch_jump(logic_op_jump_pos2.back()->get());
  399. logic_op_jump_pos1.pop_back();
  400. logic_op_jump_pos2.pop_back();
  401. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  402. append(p_target);
  403. }
  404. void GDScriptByteCodeGenerator::write_start_ternary(const Address &p_target) {
  405. ternary_result.push_back(p_target);
  406. }
  407. void GDScriptByteCodeGenerator::write_ternary_condition(const Address &p_condition) {
  408. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  409. append(p_condition);
  410. ternary_jump_fail_pos.push_back(opcodes.size());
  411. append(0); // Jump target, will be patched.
  412. }
  413. void GDScriptByteCodeGenerator::write_ternary_true_expr(const Address &p_expr) {
  414. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  415. append(ternary_result.back()->get());
  416. append(p_expr);
  417. // Jump away from the false path.
  418. append(GDScriptFunction::OPCODE_JUMP, 0);
  419. ternary_jump_skip_pos.push_back(opcodes.size());
  420. append(0);
  421. // Fail must jump here.
  422. patch_jump(ternary_jump_fail_pos.back()->get());
  423. ternary_jump_fail_pos.pop_back();
  424. }
  425. void GDScriptByteCodeGenerator::write_ternary_false_expr(const Address &p_expr) {
  426. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  427. append(ternary_result.back()->get());
  428. append(p_expr);
  429. }
  430. void GDScriptByteCodeGenerator::write_end_ternary() {
  431. patch_jump(ternary_jump_skip_pos.back()->get());
  432. ternary_jump_skip_pos.pop_back();
  433. }
  434. void GDScriptByteCodeGenerator::write_set(const Address &p_target, const Address &p_index, const Address &p_source) {
  435. if (HAS_BUILTIN_TYPE(p_target)) {
  436. if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_setter(p_target.type.builtin_type)) {
  437. // Use indexed setter instead.
  438. Variant::ValidatedIndexedSetter setter = Variant::get_member_validated_indexed_setter(p_target.type.builtin_type);
  439. append(GDScriptFunction::OPCODE_SET_INDEXED_VALIDATED, 3);
  440. append(p_target);
  441. append(p_index);
  442. append(p_source);
  443. append(setter);
  444. return;
  445. } else if (Variant::get_member_validated_keyed_setter(p_target.type.builtin_type)) {
  446. Variant::ValidatedKeyedSetter setter = Variant::get_member_validated_keyed_setter(p_target.type.builtin_type);
  447. append(GDScriptFunction::OPCODE_SET_KEYED_VALIDATED, 3);
  448. append(p_target);
  449. append(p_index);
  450. append(p_source);
  451. append(setter);
  452. return;
  453. }
  454. }
  455. append(GDScriptFunction::OPCODE_SET_KEYED, 3);
  456. append(p_target);
  457. append(p_index);
  458. append(p_source);
  459. }
  460. void GDScriptByteCodeGenerator::write_get(const Address &p_target, const Address &p_index, const Address &p_source) {
  461. if (HAS_BUILTIN_TYPE(p_source)) {
  462. if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_getter(p_source.type.builtin_type)) {
  463. // Use indexed getter instead.
  464. Variant::ValidatedIndexedGetter getter = Variant::get_member_validated_indexed_getter(p_source.type.builtin_type);
  465. append(GDScriptFunction::OPCODE_GET_INDEXED_VALIDATED, 3);
  466. append(p_source);
  467. append(p_index);
  468. append(p_target);
  469. append(getter);
  470. return;
  471. } else if (Variant::get_member_validated_keyed_getter(p_source.type.builtin_type)) {
  472. Variant::ValidatedKeyedGetter getter = Variant::get_member_validated_keyed_getter(p_source.type.builtin_type);
  473. append(GDScriptFunction::OPCODE_GET_KEYED_VALIDATED, 3);
  474. append(p_source);
  475. append(p_index);
  476. append(p_target);
  477. append(getter);
  478. return;
  479. }
  480. }
  481. append(GDScriptFunction::OPCODE_GET_KEYED, 3);
  482. append(p_source);
  483. append(p_index);
  484. append(p_target);
  485. }
  486. void GDScriptByteCodeGenerator::write_set_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  487. if (HAS_BUILTIN_TYPE(p_target) && Variant::get_member_validated_setter(p_target.type.builtin_type, p_name)) {
  488. Variant::ValidatedSetter setter = Variant::get_member_validated_setter(p_target.type.builtin_type, p_name);
  489. append(GDScriptFunction::OPCODE_SET_NAMED_VALIDATED, 2);
  490. append(p_target);
  491. append(p_source);
  492. append(setter);
  493. return;
  494. }
  495. append(GDScriptFunction::OPCODE_SET_NAMED, 2);
  496. append(p_target);
  497. append(p_source);
  498. append(p_name);
  499. }
  500. void GDScriptByteCodeGenerator::write_get_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  501. if (HAS_BUILTIN_TYPE(p_source) && Variant::get_member_validated_getter(p_source.type.builtin_type, p_name)) {
  502. Variant::ValidatedGetter getter = Variant::get_member_validated_getter(p_source.type.builtin_type, p_name);
  503. append(GDScriptFunction::OPCODE_GET_NAMED_VALIDATED, 2);
  504. append(p_source);
  505. append(p_target);
  506. append(getter);
  507. return;
  508. }
  509. append(GDScriptFunction::OPCODE_GET_NAMED, 2);
  510. append(p_source);
  511. append(p_target);
  512. append(p_name);
  513. }
  514. void GDScriptByteCodeGenerator::write_set_member(const Address &p_value, const StringName &p_name) {
  515. append(GDScriptFunction::OPCODE_SET_MEMBER, 1);
  516. append(p_value);
  517. append(p_name);
  518. }
  519. void GDScriptByteCodeGenerator::write_get_member(const Address &p_target, const StringName &p_name) {
  520. append(GDScriptFunction::OPCODE_GET_MEMBER, 1);
  521. append(p_target);
  522. append(p_name);
  523. }
  524. void GDScriptByteCodeGenerator::write_assign(const Address &p_target, const Address &p_source) {
  525. if (p_target.type.has_type && !p_source.type.has_type) {
  526. // Typed assignment.
  527. switch (p_target.type.kind) {
  528. case GDScriptDataType::BUILTIN: {
  529. if (p_target.type.builtin_type == Variant::ARRAY && p_target.type.has_container_element_type()) {
  530. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_ARRAY, 2);
  531. append(p_target);
  532. append(p_source);
  533. } else {
  534. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN, 2);
  535. append(p_target);
  536. append(p_source);
  537. append(p_target.type.builtin_type);
  538. }
  539. } break;
  540. case GDScriptDataType::NATIVE: {
  541. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_target.type.native_type];
  542. Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
  543. class_idx = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  544. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE, 3);
  545. append(p_target);
  546. append(p_source);
  547. append(class_idx);
  548. } break;
  549. case GDScriptDataType::SCRIPT:
  550. case GDScriptDataType::GDSCRIPT: {
  551. Variant script = p_target.type.script_type;
  552. int idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  553. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT, 3);
  554. append(p_target);
  555. append(p_source);
  556. append(idx);
  557. } break;
  558. default: {
  559. ERR_PRINT("Compiler bug: unresolved assign.");
  560. // Shouldn't get here, but fail-safe to a regular assignment
  561. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  562. append(p_target);
  563. append(p_source);
  564. }
  565. }
  566. } else {
  567. if (p_target.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type == Variant::ARRAY && p_target.type.has_container_element_type()) {
  568. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_ARRAY, 2);
  569. append(p_target);
  570. append(p_source);
  571. } else if (p_target.type.kind == GDScriptDataType::BUILTIN && p_source.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type != p_source.type.builtin_type) {
  572. // Need conversion..
  573. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN, 2);
  574. append(p_target);
  575. append(p_source);
  576. append(p_target.type.builtin_type);
  577. } else {
  578. // Either untyped assignment or already type-checked by the parser
  579. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  580. append(p_target);
  581. append(p_source);
  582. }
  583. }
  584. }
  585. void GDScriptByteCodeGenerator::write_assign_true(const Address &p_target) {
  586. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  587. append(p_target);
  588. }
  589. void GDScriptByteCodeGenerator::write_assign_false(const Address &p_target) {
  590. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 1);
  591. append(p_target);
  592. }
  593. void GDScriptByteCodeGenerator::write_assign_default_parameter(const Address &p_dst, const Address &p_src) {
  594. write_assign(p_dst, p_src);
  595. function->default_arguments.push_back(opcodes.size());
  596. }
  597. void GDScriptByteCodeGenerator::write_store_named_global(const Address &p_dst, const StringName &p_global) {
  598. append(GDScriptFunction::OPCODE_STORE_NAMED_GLOBAL, 1);
  599. append(p_dst);
  600. append(p_global);
  601. }
  602. void GDScriptByteCodeGenerator::write_cast(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) {
  603. int index = 0;
  604. switch (p_type.kind) {
  605. case GDScriptDataType::BUILTIN: {
  606. append(GDScriptFunction::OPCODE_CAST_TO_BUILTIN, 2);
  607. index = p_type.builtin_type;
  608. } break;
  609. case GDScriptDataType::NATIVE: {
  610. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_type.native_type];
  611. Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
  612. append(GDScriptFunction::OPCODE_CAST_TO_NATIVE, 3);
  613. index = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  614. } break;
  615. case GDScriptDataType::SCRIPT:
  616. case GDScriptDataType::GDSCRIPT: {
  617. Variant script = p_type.script_type;
  618. int idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  619. append(GDScriptFunction::OPCODE_CAST_TO_SCRIPT, 3);
  620. index = idx;
  621. } break;
  622. default: {
  623. return;
  624. }
  625. }
  626. append(p_source);
  627. append(p_target);
  628. append(index);
  629. }
  630. void GDScriptByteCodeGenerator::write_call(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  631. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  632. for (int i = 0; i < p_arguments.size(); i++) {
  633. append(p_arguments[i]);
  634. }
  635. append(p_base);
  636. append(p_target);
  637. append(p_arguments.size());
  638. append(p_function_name);
  639. }
  640. void GDScriptByteCodeGenerator::write_super_call(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  641. append(GDScriptFunction::OPCODE_CALL_SELF_BASE, 1 + p_arguments.size());
  642. for (int i = 0; i < p_arguments.size(); i++) {
  643. append(p_arguments[i]);
  644. }
  645. append(p_target);
  646. append(p_arguments.size());
  647. append(p_function_name);
  648. }
  649. void GDScriptByteCodeGenerator::write_call_async(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  650. append(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
  651. for (int i = 0; i < p_arguments.size(); i++) {
  652. append(p_arguments[i]);
  653. }
  654. append(p_base);
  655. append(p_target);
  656. append(p_arguments.size());
  657. append(p_function_name);
  658. }
  659. void GDScriptByteCodeGenerator::write_call_gdscript_utility(const Address &p_target, GDScriptUtilityFunctions::FunctionPtr p_function, const Vector<Address> &p_arguments) {
  660. append(GDScriptFunction::OPCODE_CALL_GDSCRIPT_UTILITY, 1 + p_arguments.size());
  661. for (int i = 0; i < p_arguments.size(); i++) {
  662. append(p_arguments[i]);
  663. }
  664. append(p_target);
  665. append(p_arguments.size());
  666. append(p_function);
  667. }
  668. void GDScriptByteCodeGenerator::write_call_utility(const Address &p_target, const StringName &p_function, const Vector<Address> &p_arguments) {
  669. bool is_validated = true;
  670. if (Variant::is_utility_function_vararg(p_function)) {
  671. is_validated = true; // Vararg works fine with any argument, since they can be any type.
  672. } else if (p_arguments.size() == Variant::get_utility_function_argument_count(p_function)) {
  673. bool all_types_exact = true;
  674. for (int i = 0; i < p_arguments.size(); i++) {
  675. if (!IS_BUILTIN_TYPE(p_arguments[i], Variant::get_utility_function_argument_type(p_function, i))) {
  676. all_types_exact = false;
  677. break;
  678. }
  679. }
  680. is_validated = all_types_exact;
  681. }
  682. if (is_validated) {
  683. append(GDScriptFunction::OPCODE_CALL_UTILITY_VALIDATED, 1 + p_arguments.size());
  684. for (int i = 0; i < p_arguments.size(); i++) {
  685. append(p_arguments[i]);
  686. }
  687. append(p_target);
  688. append(p_arguments.size());
  689. append(Variant::get_validated_utility_function(p_function));
  690. } else {
  691. append(GDScriptFunction::OPCODE_CALL_UTILITY, 1 + p_arguments.size());
  692. for (int i = 0; i < p_arguments.size(); i++) {
  693. append(p_arguments[i]);
  694. }
  695. append(p_target);
  696. append(p_arguments.size());
  697. append(p_function);
  698. }
  699. }
  700. 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) {
  701. bool is_validated = false;
  702. // Check if all types are correct.
  703. if (Variant::is_builtin_method_vararg(p_type, p_method)) {
  704. is_validated = true; // Vararg works fine with any argument, since they can be any type.
  705. } else if (p_arguments.size() == Variant::get_builtin_method_argument_count(p_type, p_method)) {
  706. bool all_types_exact = true;
  707. for (int i = 0; i < p_arguments.size(); i++) {
  708. if (!IS_BUILTIN_TYPE(p_arguments[i], Variant::get_builtin_method_argument_type(p_type, p_method, i))) {
  709. all_types_exact = false;
  710. break;
  711. }
  712. }
  713. is_validated = all_types_exact;
  714. }
  715. if (!is_validated) {
  716. // Perform regular call.
  717. write_call(p_target, p_base, p_method, p_arguments);
  718. return;
  719. }
  720. append(GDScriptFunction::OPCODE_CALL_BUILTIN_TYPE_VALIDATED, 2 + p_arguments.size());
  721. for (int i = 0; i < p_arguments.size(); i++) {
  722. append(p_arguments[i]);
  723. }
  724. append(p_base);
  725. append(p_target);
  726. append(p_arguments.size());
  727. append(Variant::get_validated_builtin_method(p_type, p_method));
  728. }
  729. void GDScriptByteCodeGenerator::write_call_method_bind(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) {
  730. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL_METHOD_BIND : GDScriptFunction::OPCODE_CALL_METHOD_BIND_RET, 2 + p_arguments.size());
  731. for (int i = 0; i < p_arguments.size(); i++) {
  732. append(p_arguments[i]);
  733. }
  734. append(p_base);
  735. append(p_target);
  736. append(p_arguments.size());
  737. append(p_method);
  738. }
  739. void GDScriptByteCodeGenerator::write_call_ptrcall(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) {
  740. #define CASE_TYPE(m_type) \
  741. case Variant::m_type: \
  742. append(GDScriptFunction::OPCODE_CALL_PTRCALL_##m_type, 2 + p_arguments.size()); \
  743. break
  744. bool is_ptrcall = true;
  745. if (p_method->has_return()) {
  746. MethodInfo info;
  747. ClassDB::get_method_info(p_method->get_instance_class(), p_method->get_name(), &info);
  748. switch (info.return_val.type) {
  749. CASE_TYPE(BOOL);
  750. CASE_TYPE(INT);
  751. CASE_TYPE(FLOAT);
  752. CASE_TYPE(STRING);
  753. CASE_TYPE(VECTOR2);
  754. CASE_TYPE(VECTOR2I);
  755. CASE_TYPE(RECT2);
  756. CASE_TYPE(RECT2I);
  757. CASE_TYPE(VECTOR3);
  758. CASE_TYPE(VECTOR3I);
  759. CASE_TYPE(TRANSFORM2D);
  760. CASE_TYPE(PLANE);
  761. CASE_TYPE(AABB);
  762. CASE_TYPE(BASIS);
  763. CASE_TYPE(TRANSFORM);
  764. CASE_TYPE(COLOR);
  765. CASE_TYPE(STRING_NAME);
  766. CASE_TYPE(NODE_PATH);
  767. CASE_TYPE(RID);
  768. CASE_TYPE(QUAT);
  769. CASE_TYPE(OBJECT);
  770. CASE_TYPE(CALLABLE);
  771. CASE_TYPE(SIGNAL);
  772. CASE_TYPE(DICTIONARY);
  773. CASE_TYPE(ARRAY);
  774. CASE_TYPE(PACKED_BYTE_ARRAY);
  775. CASE_TYPE(PACKED_INT32_ARRAY);
  776. CASE_TYPE(PACKED_INT64_ARRAY);
  777. CASE_TYPE(PACKED_FLOAT32_ARRAY);
  778. CASE_TYPE(PACKED_FLOAT64_ARRAY);
  779. CASE_TYPE(PACKED_STRING_ARRAY);
  780. CASE_TYPE(PACKED_VECTOR2_ARRAY);
  781. CASE_TYPE(PACKED_VECTOR3_ARRAY);
  782. CASE_TYPE(PACKED_COLOR_ARRAY);
  783. default:
  784. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL_METHOD_BIND : GDScriptFunction::OPCODE_CALL_METHOD_BIND_RET, 2 + p_arguments.size());
  785. is_ptrcall = false;
  786. break;
  787. }
  788. } else {
  789. append(GDScriptFunction::OPCODE_CALL_PTRCALL_NO_RETURN, 2 + p_arguments.size());
  790. }
  791. for (int i = 0; i < p_arguments.size(); i++) {
  792. append(p_arguments[i]);
  793. }
  794. append(p_base);
  795. append(p_target);
  796. append(p_arguments.size());
  797. append(p_method);
  798. if (is_ptrcall) {
  799. alloc_ptrcall(p_arguments.size());
  800. }
  801. #undef CASE_TYPE
  802. }
  803. void GDScriptByteCodeGenerator::write_call_self(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  804. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  805. for (int i = 0; i < p_arguments.size(); i++) {
  806. append(p_arguments[i]);
  807. }
  808. append(GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  809. append(p_target);
  810. append(p_arguments.size());
  811. append(p_function_name);
  812. }
  813. void GDScriptByteCodeGenerator::write_call_self_async(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  814. append(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
  815. for (int i = 0; i < p_arguments.size(); i++) {
  816. append(p_arguments[i]);
  817. }
  818. append(GDScriptFunction::ADDR_SELF);
  819. append(p_target);
  820. append(p_arguments.size());
  821. append(p_function_name);
  822. }
  823. void GDScriptByteCodeGenerator::write_call_script_function(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  824. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  825. for (int i = 0; i < p_arguments.size(); i++) {
  826. append(p_arguments[i]);
  827. }
  828. append(p_base);
  829. append(p_target);
  830. append(p_arguments.size());
  831. append(p_function_name);
  832. }
  833. void GDScriptByteCodeGenerator::write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) {
  834. // Try to find an appropriate constructor.
  835. bool all_have_type = true;
  836. Vector<Variant::Type> arg_types;
  837. for (int i = 0; i < p_arguments.size(); i++) {
  838. if (!HAS_BUILTIN_TYPE(p_arguments[i])) {
  839. all_have_type = false;
  840. break;
  841. }
  842. arg_types.push_back(p_arguments[i].type.builtin_type);
  843. }
  844. if (all_have_type) {
  845. int valid_constructor = -1;
  846. for (int i = 0; i < Variant::get_constructor_count(p_type); i++) {
  847. if (Variant::get_constructor_argument_count(p_type, i) != p_arguments.size()) {
  848. continue;
  849. }
  850. int types_correct = true;
  851. for (int j = 0; j < arg_types.size(); j++) {
  852. if (arg_types[j] != Variant::get_constructor_argument_type(p_type, i, j)) {
  853. types_correct = false;
  854. break;
  855. }
  856. }
  857. if (types_correct) {
  858. valid_constructor = i;
  859. break;
  860. }
  861. }
  862. if (valid_constructor >= 0) {
  863. append(GDScriptFunction::OPCODE_CONSTRUCT_VALIDATED, 1 + p_arguments.size());
  864. for (int i = 0; i < p_arguments.size(); i++) {
  865. append(p_arguments[i]);
  866. }
  867. append(p_target);
  868. append(p_arguments.size());
  869. append(Variant::get_validated_constructor(p_type, valid_constructor));
  870. return;
  871. }
  872. }
  873. append(GDScriptFunction::OPCODE_CONSTRUCT, 1 + p_arguments.size());
  874. for (int i = 0; i < p_arguments.size(); i++) {
  875. append(p_arguments[i]);
  876. }
  877. append(p_target);
  878. append(p_arguments.size());
  879. append(p_type);
  880. }
  881. void GDScriptByteCodeGenerator::write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) {
  882. append(GDScriptFunction::OPCODE_CONSTRUCT_ARRAY, 1 + p_arguments.size());
  883. for (int i = 0; i < p_arguments.size(); i++) {
  884. append(p_arguments[i]);
  885. }
  886. append(p_target);
  887. append(p_arguments.size());
  888. }
  889. void GDScriptByteCodeGenerator::write_construct_typed_array(const Address &p_target, const GDScriptDataType &p_element_type, const Vector<Address> &p_arguments) {
  890. append(GDScriptFunction::OPCODE_CONSTRUCT_TYPED_ARRAY, 2 + p_arguments.size());
  891. for (int i = 0; i < p_arguments.size(); i++) {
  892. append(p_arguments[i]);
  893. }
  894. append(p_target);
  895. if (p_element_type.script_type) {
  896. Variant script_type = Ref<Script>(p_element_type.script_type);
  897. int addr = get_constant_pos(script_type);
  898. addr |= GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS;
  899. append(addr);
  900. } else {
  901. append(Address()); // null.
  902. }
  903. append(p_arguments.size());
  904. append(p_element_type.builtin_type);
  905. append(p_element_type.native_type);
  906. }
  907. void GDScriptByteCodeGenerator::write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) {
  908. append(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY, 1 + p_arguments.size());
  909. for (int i = 0; i < p_arguments.size(); i++) {
  910. append(p_arguments[i]);
  911. }
  912. append(p_target);
  913. append(p_arguments.size() / 2); // This is number of key-value pairs, so only half of actual arguments.
  914. }
  915. void GDScriptByteCodeGenerator::write_await(const Address &p_target, const Address &p_operand) {
  916. append(GDScriptFunction::OPCODE_AWAIT, 1);
  917. append(p_operand);
  918. append(GDScriptFunction::OPCODE_AWAIT_RESUME, 1);
  919. append(p_target);
  920. }
  921. void GDScriptByteCodeGenerator::write_if(const Address &p_condition) {
  922. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  923. append(p_condition);
  924. if_jmp_addrs.push_back(opcodes.size());
  925. append(0); // Jump destination, will be patched.
  926. }
  927. void GDScriptByteCodeGenerator::write_else() {
  928. append(GDScriptFunction::OPCODE_JUMP, 0); // Jump from true if block;
  929. int else_jmp_addr = opcodes.size();
  930. append(0); // Jump destination, will be patched.
  931. patch_jump(if_jmp_addrs.back()->get());
  932. if_jmp_addrs.pop_back();
  933. if_jmp_addrs.push_back(else_jmp_addr);
  934. }
  935. void GDScriptByteCodeGenerator::write_endif() {
  936. patch_jump(if_jmp_addrs.back()->get());
  937. if_jmp_addrs.pop_back();
  938. }
  939. void GDScriptByteCodeGenerator::start_for(const GDScriptDataType &p_iterator_type, const GDScriptDataType &p_list_type) {
  940. Address counter(Address::LOCAL_VARIABLE, add_local("@counter_pos", p_iterator_type), p_iterator_type);
  941. Address container(Address::LOCAL_VARIABLE, add_local("@container_pos", p_list_type), p_list_type);
  942. // Store state.
  943. for_counter_variables.push_back(counter);
  944. for_container_variables.push_back(container);
  945. }
  946. void GDScriptByteCodeGenerator::write_for_assignment(const Address &p_variable, const Address &p_list) {
  947. const Address &container = for_container_variables.back()->get();
  948. // Assign container.
  949. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  950. append(container);
  951. append(p_list);
  952. for_iterator_variables.push_back(p_variable);
  953. }
  954. void GDScriptByteCodeGenerator::write_for() {
  955. const Address &iterator = for_iterator_variables.back()->get();
  956. const Address &counter = for_counter_variables.back()->get();
  957. const Address &container = for_container_variables.back()->get();
  958. current_breaks_to_patch.push_back(List<int>());
  959. GDScriptFunction::Opcode begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN;
  960. GDScriptFunction::Opcode iterate_opcode = GDScriptFunction::OPCODE_ITERATE;
  961. if (container.type.has_type) {
  962. if (container.type.kind == GDScriptDataType::BUILTIN) {
  963. switch (container.type.builtin_type) {
  964. case Variant::INT:
  965. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_INT;
  966. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_INT;
  967. break;
  968. case Variant::FLOAT:
  969. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_FLOAT;
  970. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_FLOAT;
  971. break;
  972. case Variant::VECTOR2:
  973. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2;
  974. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2;
  975. break;
  976. case Variant::VECTOR2I:
  977. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2I;
  978. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2I;
  979. break;
  980. case Variant::VECTOR3:
  981. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3;
  982. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3;
  983. break;
  984. case Variant::VECTOR3I:
  985. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3I;
  986. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3I;
  987. break;
  988. case Variant::STRING:
  989. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_STRING;
  990. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_STRING;
  991. break;
  992. case Variant::DICTIONARY:
  993. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_DICTIONARY;
  994. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_DICTIONARY;
  995. break;
  996. case Variant::ARRAY:
  997. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_ARRAY;
  998. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_ARRAY;
  999. break;
  1000. case Variant::PACKED_BYTE_ARRAY:
  1001. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_BYTE_ARRAY;
  1002. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_BYTE_ARRAY;
  1003. break;
  1004. case Variant::PACKED_INT32_ARRAY:
  1005. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT32_ARRAY;
  1006. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT32_ARRAY;
  1007. break;
  1008. case Variant::PACKED_INT64_ARRAY:
  1009. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT64_ARRAY;
  1010. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT64_ARRAY;
  1011. break;
  1012. case Variant::PACKED_FLOAT32_ARRAY:
  1013. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT32_ARRAY;
  1014. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT32_ARRAY;
  1015. break;
  1016. case Variant::PACKED_FLOAT64_ARRAY:
  1017. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT64_ARRAY;
  1018. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT64_ARRAY;
  1019. break;
  1020. case Variant::PACKED_STRING_ARRAY:
  1021. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_STRING_ARRAY;
  1022. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_STRING_ARRAY;
  1023. break;
  1024. case Variant::PACKED_VECTOR2_ARRAY:
  1025. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR2_ARRAY;
  1026. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR2_ARRAY;
  1027. break;
  1028. case Variant::PACKED_VECTOR3_ARRAY:
  1029. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR3_ARRAY;
  1030. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR3_ARRAY;
  1031. break;
  1032. case Variant::PACKED_COLOR_ARRAY:
  1033. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_COLOR_ARRAY;
  1034. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_COLOR_ARRAY;
  1035. break;
  1036. default:
  1037. break;
  1038. }
  1039. } else {
  1040. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_OBJECT;
  1041. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_OBJECT;
  1042. }
  1043. }
  1044. // Begin loop.
  1045. append(begin_opcode, 3);
  1046. append(counter);
  1047. append(container);
  1048. append(iterator);
  1049. for_jmp_addrs.push_back(opcodes.size());
  1050. append(0); // End of loop address, will be patched.
  1051. append(GDScriptFunction::OPCODE_JUMP, 0);
  1052. append(opcodes.size() + 6); // Skip over 'continue' code.
  1053. // Next iteration.
  1054. int continue_addr = opcodes.size();
  1055. continue_addrs.push_back(continue_addr);
  1056. append(iterate_opcode, 3);
  1057. append(counter);
  1058. append(container);
  1059. append(iterator);
  1060. for_jmp_addrs.push_back(opcodes.size());
  1061. append(0); // Jump destination, will be patched.
  1062. }
  1063. void GDScriptByteCodeGenerator::write_endfor() {
  1064. // Jump back to loop check.
  1065. append(GDScriptFunction::OPCODE_JUMP, 0);
  1066. append(continue_addrs.back()->get());
  1067. continue_addrs.pop_back();
  1068. // Patch end jumps (two of them).
  1069. for (int i = 0; i < 2; i++) {
  1070. patch_jump(for_jmp_addrs.back()->get());
  1071. for_jmp_addrs.pop_back();
  1072. }
  1073. // Patch break statements.
  1074. for (const List<int>::Element *E = current_breaks_to_patch.back()->get().front(); E; E = E->next()) {
  1075. patch_jump(E->get());
  1076. }
  1077. current_breaks_to_patch.pop_back();
  1078. // Pop state.
  1079. for_iterator_variables.pop_back();
  1080. for_counter_variables.pop_back();
  1081. for_container_variables.pop_back();
  1082. }
  1083. void GDScriptByteCodeGenerator::start_while_condition() {
  1084. current_breaks_to_patch.push_back(List<int>());
  1085. continue_addrs.push_back(opcodes.size());
  1086. }
  1087. void GDScriptByteCodeGenerator::write_while(const Address &p_condition) {
  1088. // Condition check.
  1089. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  1090. append(p_condition);
  1091. while_jmp_addrs.push_back(opcodes.size());
  1092. append(0); // End of loop address, will be patched.
  1093. }
  1094. void GDScriptByteCodeGenerator::write_endwhile() {
  1095. // Jump back to loop check.
  1096. append(GDScriptFunction::OPCODE_JUMP, 0);
  1097. append(continue_addrs.back()->get());
  1098. continue_addrs.pop_back();
  1099. // Patch end jump.
  1100. patch_jump(while_jmp_addrs.back()->get());
  1101. while_jmp_addrs.pop_back();
  1102. // Patch break statements.
  1103. for (const List<int>::Element *E = current_breaks_to_patch.back()->get().front(); E; E = E->next()) {
  1104. patch_jump(E->get());
  1105. }
  1106. current_breaks_to_patch.pop_back();
  1107. }
  1108. void GDScriptByteCodeGenerator::start_match() {
  1109. match_continues_to_patch.push_back(List<int>());
  1110. }
  1111. void GDScriptByteCodeGenerator::start_match_branch() {
  1112. // Patch continue statements.
  1113. for (const List<int>::Element *E = match_continues_to_patch.back()->get().front(); E; E = E->next()) {
  1114. patch_jump(E->get());
  1115. }
  1116. match_continues_to_patch.pop_back();
  1117. // Start a new list for next branch.
  1118. match_continues_to_patch.push_back(List<int>());
  1119. }
  1120. void GDScriptByteCodeGenerator::end_match() {
  1121. // Patch continue statements.
  1122. for (const List<int>::Element *E = match_continues_to_patch.back()->get().front(); E; E = E->next()) {
  1123. patch_jump(E->get());
  1124. }
  1125. match_continues_to_patch.pop_back();
  1126. }
  1127. void GDScriptByteCodeGenerator::write_break() {
  1128. append(GDScriptFunction::OPCODE_JUMP, 0);
  1129. current_breaks_to_patch.back()->get().push_back(opcodes.size());
  1130. append(0);
  1131. }
  1132. void GDScriptByteCodeGenerator::write_continue() {
  1133. append(GDScriptFunction::OPCODE_JUMP, 0);
  1134. append(continue_addrs.back()->get());
  1135. }
  1136. void GDScriptByteCodeGenerator::write_continue_match() {
  1137. append(GDScriptFunction::OPCODE_JUMP, 0);
  1138. match_continues_to_patch.back()->get().push_back(opcodes.size());
  1139. append(0);
  1140. }
  1141. void GDScriptByteCodeGenerator::write_breakpoint() {
  1142. append(GDScriptFunction::OPCODE_BREAKPOINT, 0);
  1143. }
  1144. void GDScriptByteCodeGenerator::write_newline(int p_line) {
  1145. append(GDScriptFunction::OPCODE_LINE, 0);
  1146. append(p_line);
  1147. current_line = p_line;
  1148. }
  1149. void GDScriptByteCodeGenerator::write_return(const Address &p_return_value) {
  1150. if (!function->return_type.has_type || p_return_value.type.has_type) {
  1151. // Either the function is untyped or the return value is also typed.
  1152. // If this is a typed function, then we need to check for potential conversions.
  1153. if (function->return_type.has_type) {
  1154. if (function->return_type.kind == GDScriptDataType::BUILTIN && function->return_type.builtin_type == Variant::ARRAY && function->return_type.has_container_element_type()) {
  1155. // Typed array.
  1156. const GDScriptDataType &element_type = function->return_type.get_container_element_type();
  1157. Variant script = function->return_type.script_type;
  1158. int script_idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  1159. append(GDScriptFunction::OPCODE_RETURN_TYPED_ARRAY, 2);
  1160. append(p_return_value);
  1161. append(script_idx);
  1162. append(element_type.kind == GDScriptDataType::BUILTIN ? element_type.builtin_type : Variant::OBJECT);
  1163. append(element_type.native_type);
  1164. } 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) {
  1165. // Add conversion.
  1166. append(GDScriptFunction::OPCODE_RETURN_TYPED_BUILTIN, 1);
  1167. append(p_return_value);
  1168. append(function->return_type.builtin_type);
  1169. } else {
  1170. // Just assign.
  1171. append(GDScriptFunction::OPCODE_RETURN, 1);
  1172. append(p_return_value);
  1173. }
  1174. } else {
  1175. append(GDScriptFunction::OPCODE_RETURN, 1);
  1176. append(p_return_value);
  1177. }
  1178. } else {
  1179. switch (function->return_type.kind) {
  1180. case GDScriptDataType::BUILTIN: {
  1181. if (function->return_type.builtin_type == Variant::ARRAY && function->return_type.has_container_element_type()) {
  1182. const GDScriptDataType &element_type = function->return_type.get_container_element_type();
  1183. Variant script = function->return_type.script_type;
  1184. int script_idx = get_constant_pos(script);
  1185. script_idx |= (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  1186. append(GDScriptFunction::OPCODE_RETURN_TYPED_ARRAY, 2);
  1187. append(p_return_value);
  1188. append(script_idx);
  1189. append(element_type.kind == GDScriptDataType::BUILTIN ? element_type.builtin_type : Variant::OBJECT);
  1190. append(element_type.native_type);
  1191. } else {
  1192. append(GDScriptFunction::OPCODE_RETURN_TYPED_BUILTIN, 1);
  1193. append(p_return_value);
  1194. append(function->return_type.builtin_type);
  1195. }
  1196. } break;
  1197. case GDScriptDataType::NATIVE: {
  1198. append(GDScriptFunction::OPCODE_RETURN_TYPED_NATIVE, 2);
  1199. append(p_return_value);
  1200. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[function->return_type.native_type];
  1201. Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
  1202. class_idx = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  1203. append(class_idx);
  1204. } break;
  1205. case GDScriptDataType::GDSCRIPT:
  1206. case GDScriptDataType::SCRIPT: {
  1207. Variant script = function->return_type.script_type;
  1208. int script_idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  1209. append(GDScriptFunction::OPCODE_RETURN_TYPED_SCRIPT, 2);
  1210. append(p_return_value);
  1211. append(script_idx);
  1212. } break;
  1213. default: {
  1214. ERR_PRINT("Compiler bug: unresolved return.");
  1215. // Shouldn't get here, but fail-safe to a regular return;
  1216. append(GDScriptFunction::OPCODE_RETURN, 1);
  1217. append(p_return_value);
  1218. } break;
  1219. }
  1220. }
  1221. }
  1222. void GDScriptByteCodeGenerator::write_assert(const Address &p_test, const Address &p_message) {
  1223. append(GDScriptFunction::OPCODE_ASSERT, 2);
  1224. append(p_test);
  1225. append(p_message);
  1226. }
  1227. void GDScriptByteCodeGenerator::start_block() {
  1228. push_stack_identifiers();
  1229. }
  1230. void GDScriptByteCodeGenerator::end_block() {
  1231. pop_stack_identifiers();
  1232. }
  1233. GDScriptByteCodeGenerator::~GDScriptByteCodeGenerator() {
  1234. if (!ended && function != nullptr) {
  1235. memdelete(function);
  1236. }
  1237. }