gdscript_byte_codegen.cpp 46 KB

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