gdscript_byte_codegen.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. if (function->_default_arg_count == 0) {
  41. append(GDScriptFunction::OPCODE_JUMP_TO_DEF_ARGUMENT);
  42. }
  43. function->default_arguments.push_back(opcodes.size());
  44. function->_default_arg_count++;
  45. }
  46. return add_local(p_name, p_type);
  47. }
  48. uint32_t GDScriptByteCodeGenerator::add_local(const StringName &p_name, const GDScriptDataType &p_type) {
  49. int stack_pos = increase_stack();
  50. add_stack_identifier(p_name, stack_pos);
  51. return stack_pos;
  52. }
  53. uint32_t GDScriptByteCodeGenerator::add_local_constant(const StringName &p_name, const Variant &p_constant) {
  54. int index = add_or_get_constant(p_constant);
  55. local_constants[p_name] = index;
  56. return index;
  57. }
  58. uint32_t GDScriptByteCodeGenerator::add_or_get_constant(const Variant &p_constant) {
  59. if (constant_map.has(p_constant)) {
  60. return constant_map[p_constant];
  61. }
  62. int index = constant_map.size();
  63. constant_map[p_constant] = index;
  64. return index;
  65. }
  66. uint32_t GDScriptByteCodeGenerator::add_or_get_name(const StringName &p_name) {
  67. return get_name_map_pos(p_name);
  68. }
  69. uint32_t GDScriptByteCodeGenerator::add_temporary() {
  70. current_temporaries++;
  71. return increase_stack();
  72. }
  73. void GDScriptByteCodeGenerator::pop_temporary() {
  74. current_stack_size--;
  75. current_temporaries--;
  76. }
  77. void GDScriptByteCodeGenerator::start_parameters() {}
  78. void GDScriptByteCodeGenerator::end_parameters() {
  79. function->default_arguments.invert();
  80. }
  81. 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) {
  82. function = memnew(GDScriptFunction);
  83. debug_stack = EngineDebugger::is_active();
  84. function->name = p_function_name;
  85. function->_script = p_script;
  86. function->source = p_script->get_path();
  87. #ifdef DEBUG_ENABLED
  88. function->func_cname = (String(function->source) + " - " + String(p_function_name)).utf8();
  89. function->_func_cname = function->func_cname.get_data();
  90. #endif
  91. function->_static = p_static;
  92. function->return_type = p_return_type;
  93. function->rpc_mode = p_rpc_mode;
  94. function->_argument_count = 0;
  95. }
  96. GDScriptFunction *GDScriptByteCodeGenerator::write_end() {
  97. append(GDScriptFunction::OPCODE_END, 0);
  98. if (constant_map.size()) {
  99. function->_constant_count = constant_map.size();
  100. function->constants.resize(constant_map.size());
  101. function->_constants_ptr = function->constants.ptrw();
  102. const Variant *K = nullptr;
  103. while ((K = constant_map.next(K))) {
  104. int idx = constant_map[*K];
  105. function->constants.write[idx] = *K;
  106. }
  107. } else {
  108. function->_constants_ptr = nullptr;
  109. function->_constant_count = 0;
  110. }
  111. if (name_map.size()) {
  112. function->global_names.resize(name_map.size());
  113. function->_global_names_ptr = &function->global_names[0];
  114. for (Map<StringName, int>::Element *E = name_map.front(); E; E = E->next()) {
  115. function->global_names.write[E->get()] = E->key();
  116. }
  117. function->_global_names_count = function->global_names.size();
  118. } else {
  119. function->_global_names_ptr = nullptr;
  120. function->_global_names_count = 0;
  121. }
  122. if (opcodes.size()) {
  123. function->code = opcodes;
  124. function->_code_ptr = &function->code[0];
  125. function->_code_size = opcodes.size();
  126. } else {
  127. function->_code_ptr = nullptr;
  128. function->_code_size = 0;
  129. }
  130. if (function->default_arguments.size()) {
  131. function->_default_arg_count = function->default_arguments.size();
  132. function->_default_arg_ptr = &function->default_arguments[0];
  133. } else {
  134. function->_default_arg_count = 0;
  135. function->_default_arg_ptr = nullptr;
  136. }
  137. if (operator_func_map.size()) {
  138. function->operator_funcs.resize(operator_func_map.size());
  139. function->_operator_funcs_count = function->operator_funcs.size();
  140. function->_operator_funcs_ptr = function->operator_funcs.ptr();
  141. for (const Map<Variant::ValidatedOperatorEvaluator, int>::Element *E = operator_func_map.front(); E; E = E->next()) {
  142. function->operator_funcs.write[E->get()] = E->key();
  143. }
  144. } else {
  145. function->_operator_funcs_count = 0;
  146. function->_operator_funcs_ptr = nullptr;
  147. }
  148. if (setters_map.size()) {
  149. function->setters.resize(setters_map.size());
  150. function->_setters_count = function->setters.size();
  151. function->_setters_ptr = function->setters.ptr();
  152. for (const Map<Variant::ValidatedSetter, int>::Element *E = setters_map.front(); E; E = E->next()) {
  153. function->setters.write[E->get()] = E->key();
  154. }
  155. } else {
  156. function->_setters_count = 0;
  157. function->_setters_ptr = nullptr;
  158. }
  159. if (getters_map.size()) {
  160. function->getters.resize(getters_map.size());
  161. function->_getters_count = function->getters.size();
  162. function->_getters_ptr = function->getters.ptr();
  163. for (const Map<Variant::ValidatedGetter, int>::Element *E = getters_map.front(); E; E = E->next()) {
  164. function->getters.write[E->get()] = E->key();
  165. }
  166. } else {
  167. function->_getters_count = 0;
  168. function->_getters_ptr = nullptr;
  169. }
  170. if (keyed_setters_map.size()) {
  171. function->keyed_setters.resize(keyed_setters_map.size());
  172. function->_keyed_setters_count = function->keyed_setters.size();
  173. function->_keyed_setters_ptr = function->keyed_setters.ptr();
  174. for (const Map<Variant::ValidatedKeyedSetter, int>::Element *E = keyed_setters_map.front(); E; E = E->next()) {
  175. function->keyed_setters.write[E->get()] = E->key();
  176. }
  177. } else {
  178. function->_keyed_setters_count = 0;
  179. function->_keyed_setters_ptr = nullptr;
  180. }
  181. if (keyed_getters_map.size()) {
  182. function->keyed_getters.resize(keyed_getters_map.size());
  183. function->_keyed_getters_count = function->keyed_getters.size();
  184. function->_keyed_getters_ptr = function->keyed_getters.ptr();
  185. for (const Map<Variant::ValidatedKeyedGetter, int>::Element *E = keyed_getters_map.front(); E; E = E->next()) {
  186. function->keyed_getters.write[E->get()] = E->key();
  187. }
  188. } else {
  189. function->_keyed_getters_count = 0;
  190. function->_keyed_getters_ptr = nullptr;
  191. }
  192. if (indexed_setters_map.size()) {
  193. function->indexed_setters.resize(indexed_setters_map.size());
  194. function->_indexed_setters_count = function->indexed_setters.size();
  195. function->_indexed_setters_ptr = function->indexed_setters.ptr();
  196. for (const Map<Variant::ValidatedIndexedSetter, int>::Element *E = indexed_setters_map.front(); E; E = E->next()) {
  197. function->indexed_setters.write[E->get()] = E->key();
  198. }
  199. } else {
  200. function->_indexed_setters_count = 0;
  201. function->_indexed_setters_ptr = nullptr;
  202. }
  203. if (indexed_getters_map.size()) {
  204. function->indexed_getters.resize(indexed_getters_map.size());
  205. function->_indexed_getters_count = function->indexed_getters.size();
  206. function->_indexed_getters_ptr = function->indexed_getters.ptr();
  207. for (const Map<Variant::ValidatedIndexedGetter, int>::Element *E = indexed_getters_map.front(); E; E = E->next()) {
  208. function->indexed_getters.write[E->get()] = E->key();
  209. }
  210. } else {
  211. function->_indexed_getters_count = 0;
  212. function->_indexed_getters_ptr = nullptr;
  213. }
  214. if (debug_stack) {
  215. function->stack_debug = stack_debug;
  216. }
  217. function->_stack_size = stack_max;
  218. function->_instruction_args_size = instr_args_max;
  219. ended = true;
  220. return function;
  221. }
  222. #ifdef DEBUG_ENABLED
  223. void GDScriptByteCodeGenerator::set_signature(const String &p_signature) {
  224. function->profile.signature = p_signature;
  225. }
  226. #endif
  227. void GDScriptByteCodeGenerator::set_initial_line(int p_line) {
  228. function->_initial_line = p_line;
  229. }
  230. #define HAS_BUILTIN_TYPE(m_var) \
  231. (m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN)
  232. #define IS_BUILTIN_TYPE(m_var, m_type) \
  233. (m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN && m_var.type.builtin_type == m_type)
  234. void GDScriptByteCodeGenerator::write_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) {
  235. if (HAS_BUILTIN_TYPE(p_left_operand) && HAS_BUILTIN_TYPE(p_right_operand)) {
  236. // Gather specific operator.
  237. Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type);
  238. append(GDScriptFunction::OPCODE_OPERATOR_VALIDATED, 3);
  239. append(p_left_operand);
  240. append(p_right_operand);
  241. append(p_target);
  242. append(op_func);
  243. return;
  244. }
  245. // No specific types, perform variant evaluation.
  246. append(GDScriptFunction::OPCODE_OPERATOR, 3);
  247. append(p_left_operand);
  248. append(p_right_operand);
  249. append(p_target);
  250. append(p_operator);
  251. }
  252. void GDScriptByteCodeGenerator::write_type_test(const Address &p_target, const Address &p_source, const Address &p_type) {
  253. append(GDScriptFunction::OPCODE_EXTENDS_TEST, 3);
  254. append(p_source);
  255. append(p_type);
  256. append(p_target);
  257. }
  258. void GDScriptByteCodeGenerator::write_type_test_builtin(const Address &p_target, const Address &p_source, Variant::Type p_type) {
  259. append(GDScriptFunction::OPCODE_IS_BUILTIN, 3);
  260. append(p_source);
  261. append(p_target);
  262. append(p_type);
  263. }
  264. void GDScriptByteCodeGenerator::write_and_left_operand(const Address &p_left_operand) {
  265. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  266. append(p_left_operand);
  267. logic_op_jump_pos1.push_back(opcodes.size());
  268. append(0); // Jump target, will be patched.
  269. }
  270. void GDScriptByteCodeGenerator::write_and_right_operand(const Address &p_right_operand) {
  271. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  272. append(p_right_operand);
  273. logic_op_jump_pos2.push_back(opcodes.size());
  274. append(0); // Jump target, will be patched.
  275. }
  276. void GDScriptByteCodeGenerator::write_end_and(const Address &p_target) {
  277. // If here means both operands are true.
  278. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  279. append(p_target);
  280. // Jump away from the fail condition.
  281. append(GDScriptFunction::OPCODE_JUMP, 0);
  282. append(opcodes.size() + 3);
  283. // Here it means one of operands is false.
  284. patch_jump(logic_op_jump_pos1.back()->get());
  285. patch_jump(logic_op_jump_pos2.back()->get());
  286. logic_op_jump_pos1.pop_back();
  287. logic_op_jump_pos2.pop_back();
  288. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 0);
  289. append(p_target);
  290. }
  291. void GDScriptByteCodeGenerator::write_or_left_operand(const Address &p_left_operand) {
  292. append(GDScriptFunction::OPCODE_JUMP_IF, 1);
  293. append(p_left_operand);
  294. logic_op_jump_pos1.push_back(opcodes.size());
  295. append(0); // Jump target, will be patched.
  296. }
  297. void GDScriptByteCodeGenerator::write_or_right_operand(const Address &p_right_operand) {
  298. append(GDScriptFunction::OPCODE_JUMP_IF, 1);
  299. append(p_right_operand);
  300. logic_op_jump_pos2.push_back(opcodes.size());
  301. append(0); // Jump target, will be patched.
  302. }
  303. void GDScriptByteCodeGenerator::write_end_or(const Address &p_target) {
  304. // If here means both operands are false.
  305. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 1);
  306. append(p_target);
  307. // Jump away from the success condition.
  308. append(GDScriptFunction::OPCODE_JUMP, 0);
  309. append(opcodes.size() + 3);
  310. // Here it means one of operands is false.
  311. patch_jump(logic_op_jump_pos1.back()->get());
  312. patch_jump(logic_op_jump_pos2.back()->get());
  313. logic_op_jump_pos1.pop_back();
  314. logic_op_jump_pos2.pop_back();
  315. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  316. append(p_target);
  317. }
  318. void GDScriptByteCodeGenerator::write_start_ternary(const Address &p_target) {
  319. ternary_result.push_back(p_target);
  320. }
  321. void GDScriptByteCodeGenerator::write_ternary_condition(const Address &p_condition) {
  322. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  323. append(p_condition);
  324. ternary_jump_fail_pos.push_back(opcodes.size());
  325. append(0); // Jump target, will be patched.
  326. }
  327. void GDScriptByteCodeGenerator::write_ternary_true_expr(const Address &p_expr) {
  328. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  329. append(ternary_result.back()->get());
  330. append(p_expr);
  331. // Jump away from the false path.
  332. append(GDScriptFunction::OPCODE_JUMP, 0);
  333. ternary_jump_skip_pos.push_back(opcodes.size());
  334. append(0);
  335. // Fail must jump here.
  336. patch_jump(ternary_jump_fail_pos.back()->get());
  337. ternary_jump_fail_pos.pop_back();
  338. }
  339. void GDScriptByteCodeGenerator::write_ternary_false_expr(const Address &p_expr) {
  340. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  341. append(ternary_result.back()->get());
  342. append(p_expr);
  343. }
  344. void GDScriptByteCodeGenerator::write_end_ternary() {
  345. patch_jump(ternary_jump_skip_pos.back()->get());
  346. ternary_jump_skip_pos.pop_back();
  347. }
  348. void GDScriptByteCodeGenerator::write_set(const Address &p_target, const Address &p_index, const Address &p_source) {
  349. if (HAS_BUILTIN_TYPE(p_target)) {
  350. if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_setter(p_target.type.builtin_type)) {
  351. // Use indexed setter instead.
  352. Variant::ValidatedIndexedSetter setter = Variant::get_member_validated_indexed_setter(p_target.type.builtin_type);
  353. append(GDScriptFunction::OPCODE_SET_INDEXED_VALIDATED, 3);
  354. append(p_target);
  355. append(p_index);
  356. append(p_source);
  357. append(setter);
  358. return;
  359. } else if (Variant::get_member_validated_keyed_setter(p_target.type.builtin_type)) {
  360. Variant::ValidatedKeyedSetter setter = Variant::get_member_validated_keyed_setter(p_target.type.builtin_type);
  361. append(GDScriptFunction::OPCODE_SET_KEYED_VALIDATED, 3);
  362. append(p_target);
  363. append(p_index);
  364. append(p_source);
  365. append(setter);
  366. return;
  367. }
  368. }
  369. append(GDScriptFunction::OPCODE_SET_KEYED, 3);
  370. append(p_target);
  371. append(p_index);
  372. append(p_source);
  373. }
  374. void GDScriptByteCodeGenerator::write_get(const Address &p_target, const Address &p_index, const Address &p_source) {
  375. if (HAS_BUILTIN_TYPE(p_source)) {
  376. if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_getter(p_source.type.builtin_type)) {
  377. // Use indexed getter instead.
  378. Variant::ValidatedIndexedGetter getter = Variant::get_member_validated_indexed_getter(p_source.type.builtin_type);
  379. append(GDScriptFunction::OPCODE_GET_INDEXED_VALIDATED, 3);
  380. append(p_source);
  381. append(p_index);
  382. append(p_target);
  383. append(getter);
  384. return;
  385. } else if (Variant::get_member_validated_keyed_getter(p_source.type.builtin_type)) {
  386. Variant::ValidatedKeyedGetter getter = Variant::get_member_validated_keyed_getter(p_source.type.builtin_type);
  387. append(GDScriptFunction::OPCODE_GET_KEYED_VALIDATED, 3);
  388. append(p_source);
  389. append(p_index);
  390. append(p_target);
  391. append(getter);
  392. return;
  393. }
  394. }
  395. append(GDScriptFunction::OPCODE_GET_KEYED, 3);
  396. append(p_source);
  397. append(p_index);
  398. append(p_target);
  399. }
  400. void GDScriptByteCodeGenerator::write_set_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  401. if (HAS_BUILTIN_TYPE(p_target) && Variant::get_member_validated_setter(p_target.type.builtin_type, p_name)) {
  402. Variant::ValidatedSetter setter = Variant::get_member_validated_setter(p_target.type.builtin_type, p_name);
  403. append(GDScriptFunction::OPCODE_SET_NAMED_VALIDATED, 2);
  404. append(p_target);
  405. append(p_source);
  406. append(setter);
  407. return;
  408. }
  409. append(GDScriptFunction::OPCODE_SET_NAMED, 2);
  410. append(p_target);
  411. append(p_source);
  412. append(p_name);
  413. }
  414. void GDScriptByteCodeGenerator::write_get_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  415. if (HAS_BUILTIN_TYPE(p_source) && Variant::get_member_validated_getter(p_source.type.builtin_type, p_name)) {
  416. Variant::ValidatedGetter getter = Variant::get_member_validated_getter(p_source.type.builtin_type, p_name);
  417. append(GDScriptFunction::OPCODE_GET_NAMED_VALIDATED, 2);
  418. append(p_source);
  419. append(p_target);
  420. append(getter);
  421. return;
  422. }
  423. append(GDScriptFunction::OPCODE_GET_NAMED, 2);
  424. append(p_source);
  425. append(p_target);
  426. append(p_name);
  427. }
  428. void GDScriptByteCodeGenerator::write_set_member(const Address &p_value, const StringName &p_name) {
  429. append(GDScriptFunction::OPCODE_SET_MEMBER, 1);
  430. append(p_value);
  431. append(p_name);
  432. }
  433. void GDScriptByteCodeGenerator::write_get_member(const Address &p_target, const StringName &p_name) {
  434. append(GDScriptFunction::OPCODE_GET_MEMBER, 1);
  435. append(p_target);
  436. append(p_name);
  437. }
  438. void GDScriptByteCodeGenerator::write_assign(const Address &p_target, const Address &p_source) {
  439. if (p_target.type.has_type && !p_source.type.has_type) {
  440. // Typed assignment.
  441. switch (p_target.type.kind) {
  442. case GDScriptDataType::BUILTIN: {
  443. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN, 2);
  444. append(p_target);
  445. append(p_source);
  446. append(p_target.type.builtin_type);
  447. } break;
  448. case GDScriptDataType::NATIVE: {
  449. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_target.type.native_type];
  450. class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS);
  451. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE, 3);
  452. append(p_target);
  453. append(p_source);
  454. append(class_idx);
  455. } break;
  456. case GDScriptDataType::SCRIPT:
  457. case GDScriptDataType::GDSCRIPT: {
  458. Variant script = p_target.type.script_type;
  459. int idx = get_constant_pos(script);
  460. idx |= (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
  461. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT, 3);
  462. append(p_target);
  463. append(p_source);
  464. append(idx);
  465. } break;
  466. default: {
  467. ERR_PRINT("Compiler bug: unresolved assign.");
  468. // Shouldn't get here, but fail-safe to a regular assignment
  469. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  470. append(p_target);
  471. append(p_source);
  472. }
  473. }
  474. } else {
  475. if (p_target.type.kind == GDScriptDataType::BUILTIN && p_source.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type != p_source.type.builtin_type) {
  476. // Need conversion..
  477. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN, 2);
  478. append(p_target);
  479. append(p_source);
  480. append(p_target.type.builtin_type);
  481. } else {
  482. // Either untyped assignment or already type-checked by the parser
  483. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  484. append(p_target);
  485. append(p_source);
  486. }
  487. }
  488. }
  489. void GDScriptByteCodeGenerator::write_assign_true(const Address &p_target) {
  490. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  491. append(p_target);
  492. }
  493. void GDScriptByteCodeGenerator::write_assign_false(const Address &p_target) {
  494. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 1);
  495. append(p_target);
  496. }
  497. void GDScriptByteCodeGenerator::write_cast(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) {
  498. int index = 0;
  499. switch (p_type.kind) {
  500. case GDScriptDataType::BUILTIN: {
  501. append(GDScriptFunction::OPCODE_CAST_TO_BUILTIN, 2);
  502. index = p_type.builtin_type;
  503. } break;
  504. case GDScriptDataType::NATIVE: {
  505. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_type.native_type];
  506. class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS);
  507. append(GDScriptFunction::OPCODE_CAST_TO_NATIVE, 3);
  508. index = class_idx;
  509. } break;
  510. case GDScriptDataType::SCRIPT:
  511. case GDScriptDataType::GDSCRIPT: {
  512. Variant script = p_type.script_type;
  513. int idx = get_constant_pos(script);
  514. idx |= (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
  515. append(GDScriptFunction::OPCODE_CAST_TO_SCRIPT, 3);
  516. index = idx;
  517. } break;
  518. default: {
  519. return;
  520. }
  521. }
  522. append(p_source);
  523. append(p_target);
  524. append(index);
  525. }
  526. void GDScriptByteCodeGenerator::write_call(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  527. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  528. for (int i = 0; i < p_arguments.size(); i++) {
  529. append(p_arguments[i]);
  530. }
  531. append(p_base);
  532. append(p_target);
  533. append(p_arguments.size());
  534. append(p_function_name);
  535. }
  536. void GDScriptByteCodeGenerator::write_super_call(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  537. append(GDScriptFunction::OPCODE_CALL_SELF_BASE, 1 + p_arguments.size());
  538. for (int i = 0; i < p_arguments.size(); i++) {
  539. append(p_arguments[i]);
  540. }
  541. append(p_target);
  542. append(p_arguments.size());
  543. append(p_function_name);
  544. }
  545. void GDScriptByteCodeGenerator::write_call_async(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  546. append(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
  547. for (int i = 0; i < p_arguments.size(); i++) {
  548. append(p_arguments[i]);
  549. }
  550. append(p_base);
  551. append(p_target);
  552. append(p_arguments.size());
  553. append(p_function_name);
  554. }
  555. void GDScriptByteCodeGenerator::write_call_builtin(const Address &p_target, GDScriptFunctions::Function p_function, const Vector<Address> &p_arguments) {
  556. append(GDScriptFunction::OPCODE_CALL_BUILT_IN, 1 + p_arguments.size());
  557. for (int i = 0; i < p_arguments.size(); i++) {
  558. append(p_arguments[i]);
  559. }
  560. append(p_target);
  561. append(p_arguments.size());
  562. append(p_function);
  563. }
  564. void GDScriptByteCodeGenerator::write_call_method_bind(const Address &p_target, const Address &p_base, const MethodBind *p_method, const Vector<Address> &p_arguments) {
  565. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  566. for (int i = 0; i < p_arguments.size(); i++) {
  567. append(p_arguments[i]);
  568. }
  569. append(p_base);
  570. append(p_target);
  571. append(p_arguments.size());
  572. append(p_method->get_name());
  573. }
  574. void GDScriptByteCodeGenerator::write_call_ptrcall(const Address &p_target, const Address &p_base, const MethodBind *p_method, const Vector<Address> &p_arguments) {
  575. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  576. for (int i = 0; i < p_arguments.size(); i++) {
  577. append(p_arguments[i]);
  578. }
  579. append(p_base);
  580. append(p_target);
  581. append(p_arguments.size());
  582. append(p_method->get_name());
  583. }
  584. void GDScriptByteCodeGenerator::write_call_self(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  585. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  586. for (int i = 0; i < p_arguments.size(); i++) {
  587. append(p_arguments[i]);
  588. }
  589. append(GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS);
  590. append(p_target);
  591. append(p_arguments.size());
  592. append(p_function_name);
  593. }
  594. void GDScriptByteCodeGenerator::write_call_script_function(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  595. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  596. for (int i = 0; i < p_arguments.size(); i++) {
  597. append(p_arguments[i]);
  598. }
  599. append(p_base);
  600. append(p_target);
  601. append(p_arguments.size());
  602. append(p_function_name);
  603. }
  604. void GDScriptByteCodeGenerator::write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) {
  605. append(GDScriptFunction::OPCODE_CONSTRUCT, 1 + p_arguments.size());
  606. for (int i = 0; i < p_arguments.size(); i++) {
  607. append(p_arguments[i]);
  608. }
  609. append(p_target);
  610. append(p_arguments.size());
  611. append(p_type);
  612. }
  613. void GDScriptByteCodeGenerator::write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) {
  614. append(GDScriptFunction::OPCODE_CONSTRUCT_ARRAY, 1 + p_arguments.size());
  615. for (int i = 0; i < p_arguments.size(); i++) {
  616. append(p_arguments[i]);
  617. }
  618. append(p_target);
  619. append(p_arguments.size());
  620. }
  621. void GDScriptByteCodeGenerator::write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) {
  622. append(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY, 1 + p_arguments.size());
  623. for (int i = 0; i < p_arguments.size(); i++) {
  624. append(p_arguments[i]);
  625. }
  626. append(p_target);
  627. append(p_arguments.size() / 2); // This is number of key-value pairs, so only half of actual arguments.
  628. }
  629. void GDScriptByteCodeGenerator::write_await(const Address &p_target, const Address &p_operand) {
  630. append(GDScriptFunction::OPCODE_AWAIT, 1);
  631. append(p_operand);
  632. append(GDScriptFunction::OPCODE_AWAIT_RESUME, 1);
  633. append(p_target);
  634. }
  635. void GDScriptByteCodeGenerator::write_if(const Address &p_condition) {
  636. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  637. append(p_condition);
  638. if_jmp_addrs.push_back(opcodes.size());
  639. append(0); // Jump destination, will be patched.
  640. }
  641. void GDScriptByteCodeGenerator::write_else() {
  642. append(GDScriptFunction::OPCODE_JUMP, 0); // Jump from true if block;
  643. int else_jmp_addr = opcodes.size();
  644. append(0); // Jump destination, will be patched.
  645. patch_jump(if_jmp_addrs.back()->get());
  646. if_jmp_addrs.pop_back();
  647. if_jmp_addrs.push_back(else_jmp_addr);
  648. }
  649. void GDScriptByteCodeGenerator::write_endif() {
  650. patch_jump(if_jmp_addrs.back()->get());
  651. if_jmp_addrs.pop_back();
  652. }
  653. void GDScriptByteCodeGenerator::write_for(const Address &p_variable, const Address &p_list) {
  654. int counter_pos = add_temporary() | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  655. int container_pos = add_temporary() | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  656. current_breaks_to_patch.push_back(List<int>());
  657. // Assign container.
  658. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  659. append(container_pos);
  660. append(p_list);
  661. // Begin loop.
  662. append(GDScriptFunction::OPCODE_ITERATE_BEGIN, 3);
  663. append(counter_pos);
  664. append(container_pos);
  665. append(p_variable);
  666. for_jmp_addrs.push_back(opcodes.size());
  667. append(0); // End of loop address, will be patched.
  668. append(GDScriptFunction::OPCODE_JUMP, 0);
  669. append(opcodes.size() + 6); // Skip over 'continue' code.
  670. // Next iteration.
  671. int continue_addr = opcodes.size();
  672. continue_addrs.push_back(continue_addr);
  673. append(GDScriptFunction::OPCODE_ITERATE, 3);
  674. append(counter_pos);
  675. append(container_pos);
  676. append(p_variable);
  677. for_jmp_addrs.push_back(opcodes.size());
  678. append(0); // Jump destination, will be patched.
  679. }
  680. void GDScriptByteCodeGenerator::write_endfor() {
  681. // Jump back to loop check.
  682. append(GDScriptFunction::OPCODE_JUMP, 0);
  683. append(continue_addrs.back()->get());
  684. continue_addrs.pop_back();
  685. // Patch end jumps (two of them).
  686. for (int i = 0; i < 2; i++) {
  687. patch_jump(for_jmp_addrs.back()->get());
  688. for_jmp_addrs.pop_back();
  689. }
  690. // Patch break statements.
  691. for (const List<int>::Element *E = current_breaks_to_patch.back()->get().front(); E; E = E->next()) {
  692. patch_jump(E->get());
  693. }
  694. current_breaks_to_patch.pop_back();
  695. // Remove loop temporaries.
  696. pop_temporary();
  697. pop_temporary();
  698. }
  699. void GDScriptByteCodeGenerator::start_while_condition() {
  700. current_breaks_to_patch.push_back(List<int>());
  701. continue_addrs.push_back(opcodes.size());
  702. }
  703. void GDScriptByteCodeGenerator::write_while(const Address &p_condition) {
  704. // Condition check.
  705. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  706. append(p_condition);
  707. while_jmp_addrs.push_back(opcodes.size());
  708. append(0); // End of loop address, will be patched.
  709. }
  710. void GDScriptByteCodeGenerator::write_endwhile() {
  711. // Jump back to loop check.
  712. append(GDScriptFunction::OPCODE_JUMP, 0);
  713. append(continue_addrs.back()->get());
  714. continue_addrs.pop_back();
  715. // Patch end jump.
  716. patch_jump(while_jmp_addrs.back()->get());
  717. while_jmp_addrs.pop_back();
  718. // Patch break statements.
  719. for (const List<int>::Element *E = current_breaks_to_patch.back()->get().front(); E; E = E->next()) {
  720. patch_jump(E->get());
  721. }
  722. current_breaks_to_patch.pop_back();
  723. }
  724. void GDScriptByteCodeGenerator::start_match() {
  725. match_continues_to_patch.push_back(List<int>());
  726. }
  727. void GDScriptByteCodeGenerator::start_match_branch() {
  728. // Patch continue statements.
  729. for (const List<int>::Element *E = match_continues_to_patch.back()->get().front(); E; E = E->next()) {
  730. patch_jump(E->get());
  731. }
  732. match_continues_to_patch.pop_back();
  733. // Start a new list for next branch.
  734. match_continues_to_patch.push_back(List<int>());
  735. }
  736. void GDScriptByteCodeGenerator::end_match() {
  737. // Patch continue statements.
  738. for (const List<int>::Element *E = match_continues_to_patch.back()->get().front(); E; E = E->next()) {
  739. patch_jump(E->get());
  740. }
  741. match_continues_to_patch.pop_back();
  742. }
  743. void GDScriptByteCodeGenerator::write_break() {
  744. append(GDScriptFunction::OPCODE_JUMP, 0);
  745. current_breaks_to_patch.back()->get().push_back(opcodes.size());
  746. append(0);
  747. }
  748. void GDScriptByteCodeGenerator::write_continue() {
  749. append(GDScriptFunction::OPCODE_JUMP, 0);
  750. append(continue_addrs.back()->get());
  751. }
  752. void GDScriptByteCodeGenerator::write_continue_match() {
  753. append(GDScriptFunction::OPCODE_JUMP, 0);
  754. match_continues_to_patch.back()->get().push_back(opcodes.size());
  755. append(0);
  756. }
  757. void GDScriptByteCodeGenerator::write_breakpoint() {
  758. append(GDScriptFunction::OPCODE_BREAKPOINT, 0);
  759. }
  760. void GDScriptByteCodeGenerator::write_newline(int p_line) {
  761. append(GDScriptFunction::OPCODE_LINE, 0);
  762. append(p_line);
  763. current_line = p_line;
  764. }
  765. void GDScriptByteCodeGenerator::write_return(const Address &p_return_value) {
  766. append(GDScriptFunction::OPCODE_RETURN, 1);
  767. append(p_return_value);
  768. }
  769. void GDScriptByteCodeGenerator::write_assert(const Address &p_test, const Address &p_message) {
  770. append(GDScriptFunction::OPCODE_ASSERT, 2);
  771. append(p_test);
  772. append(p_message);
  773. }
  774. void GDScriptByteCodeGenerator::start_block() {
  775. push_stack_identifiers();
  776. }
  777. void GDScriptByteCodeGenerator::end_block() {
  778. pop_stack_identifiers();
  779. }
  780. GDScriptByteCodeGenerator::~GDScriptByteCodeGenerator() {
  781. if (!ended && function != nullptr) {
  782. memdelete(function);
  783. }
  784. }