gdscript_byte_codegen.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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 (method_bind_map.size()) {
  215. function->methods.resize(method_bind_map.size());
  216. function->_methods_ptr = function->methods.ptrw();
  217. function->_methods_count = method_bind_map.size();
  218. for (const Map<MethodBind *, int>::Element *E = method_bind_map.front(); E; E = E->next()) {
  219. function->methods.write[E->get()] = E->key();
  220. }
  221. } else {
  222. function->_methods_ptr = nullptr;
  223. function->_methods_count = 0;
  224. }
  225. if (debug_stack) {
  226. function->stack_debug = stack_debug;
  227. }
  228. function->_stack_size = stack_max;
  229. function->_instruction_args_size = instr_args_max;
  230. function->_ptrcall_args_size = ptrcall_max;
  231. ended = true;
  232. return function;
  233. }
  234. #ifdef DEBUG_ENABLED
  235. void GDScriptByteCodeGenerator::set_signature(const String &p_signature) {
  236. function->profile.signature = p_signature;
  237. }
  238. #endif
  239. void GDScriptByteCodeGenerator::set_initial_line(int p_line) {
  240. function->_initial_line = p_line;
  241. }
  242. #define HAS_BUILTIN_TYPE(m_var) \
  243. (m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN)
  244. #define IS_BUILTIN_TYPE(m_var, m_type) \
  245. (m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN && m_var.type.builtin_type == m_type)
  246. void GDScriptByteCodeGenerator::write_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) {
  247. if (HAS_BUILTIN_TYPE(p_left_operand) && HAS_BUILTIN_TYPE(p_right_operand)) {
  248. // Gather specific operator.
  249. Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type);
  250. append(GDScriptFunction::OPCODE_OPERATOR_VALIDATED, 3);
  251. append(p_left_operand);
  252. append(p_right_operand);
  253. append(p_target);
  254. append(op_func);
  255. return;
  256. }
  257. // No specific types, perform variant evaluation.
  258. append(GDScriptFunction::OPCODE_OPERATOR, 3);
  259. append(p_left_operand);
  260. append(p_right_operand);
  261. append(p_target);
  262. append(p_operator);
  263. }
  264. void GDScriptByteCodeGenerator::write_type_test(const Address &p_target, const Address &p_source, const Address &p_type) {
  265. append(GDScriptFunction::OPCODE_EXTENDS_TEST, 3);
  266. append(p_source);
  267. append(p_type);
  268. append(p_target);
  269. }
  270. void GDScriptByteCodeGenerator::write_type_test_builtin(const Address &p_target, const Address &p_source, Variant::Type p_type) {
  271. append(GDScriptFunction::OPCODE_IS_BUILTIN, 3);
  272. append(p_source);
  273. append(p_target);
  274. append(p_type);
  275. }
  276. void GDScriptByteCodeGenerator::write_and_left_operand(const Address &p_left_operand) {
  277. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  278. append(p_left_operand);
  279. logic_op_jump_pos1.push_back(opcodes.size());
  280. append(0); // Jump target, will be patched.
  281. }
  282. void GDScriptByteCodeGenerator::write_and_right_operand(const Address &p_right_operand) {
  283. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  284. append(p_right_operand);
  285. logic_op_jump_pos2.push_back(opcodes.size());
  286. append(0); // Jump target, will be patched.
  287. }
  288. void GDScriptByteCodeGenerator::write_end_and(const Address &p_target) {
  289. // If here means both operands are true.
  290. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  291. append(p_target);
  292. // Jump away from the fail condition.
  293. append(GDScriptFunction::OPCODE_JUMP, 0);
  294. append(opcodes.size() + 3);
  295. // Here it means one of operands is false.
  296. patch_jump(logic_op_jump_pos1.back()->get());
  297. patch_jump(logic_op_jump_pos2.back()->get());
  298. logic_op_jump_pos1.pop_back();
  299. logic_op_jump_pos2.pop_back();
  300. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 0);
  301. append(p_target);
  302. }
  303. void GDScriptByteCodeGenerator::write_or_left_operand(const Address &p_left_operand) {
  304. append(GDScriptFunction::OPCODE_JUMP_IF, 1);
  305. append(p_left_operand);
  306. logic_op_jump_pos1.push_back(opcodes.size());
  307. append(0); // Jump target, will be patched.
  308. }
  309. void GDScriptByteCodeGenerator::write_or_right_operand(const Address &p_right_operand) {
  310. append(GDScriptFunction::OPCODE_JUMP_IF, 1);
  311. append(p_right_operand);
  312. logic_op_jump_pos2.push_back(opcodes.size());
  313. append(0); // Jump target, will be patched.
  314. }
  315. void GDScriptByteCodeGenerator::write_end_or(const Address &p_target) {
  316. // If here means both operands are false.
  317. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 1);
  318. append(p_target);
  319. // Jump away from the success condition.
  320. append(GDScriptFunction::OPCODE_JUMP, 0);
  321. append(opcodes.size() + 3);
  322. // Here it means one of operands is false.
  323. patch_jump(logic_op_jump_pos1.back()->get());
  324. patch_jump(logic_op_jump_pos2.back()->get());
  325. logic_op_jump_pos1.pop_back();
  326. logic_op_jump_pos2.pop_back();
  327. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  328. append(p_target);
  329. }
  330. void GDScriptByteCodeGenerator::write_start_ternary(const Address &p_target) {
  331. ternary_result.push_back(p_target);
  332. }
  333. void GDScriptByteCodeGenerator::write_ternary_condition(const Address &p_condition) {
  334. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  335. append(p_condition);
  336. ternary_jump_fail_pos.push_back(opcodes.size());
  337. append(0); // Jump target, will be patched.
  338. }
  339. void GDScriptByteCodeGenerator::write_ternary_true_expr(const Address &p_expr) {
  340. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  341. append(ternary_result.back()->get());
  342. append(p_expr);
  343. // Jump away from the false path.
  344. append(GDScriptFunction::OPCODE_JUMP, 0);
  345. ternary_jump_skip_pos.push_back(opcodes.size());
  346. append(0);
  347. // Fail must jump here.
  348. patch_jump(ternary_jump_fail_pos.back()->get());
  349. ternary_jump_fail_pos.pop_back();
  350. }
  351. void GDScriptByteCodeGenerator::write_ternary_false_expr(const Address &p_expr) {
  352. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  353. append(ternary_result.back()->get());
  354. append(p_expr);
  355. }
  356. void GDScriptByteCodeGenerator::write_end_ternary() {
  357. patch_jump(ternary_jump_skip_pos.back()->get());
  358. ternary_jump_skip_pos.pop_back();
  359. }
  360. void GDScriptByteCodeGenerator::write_set(const Address &p_target, const Address &p_index, const Address &p_source) {
  361. if (HAS_BUILTIN_TYPE(p_target)) {
  362. if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_setter(p_target.type.builtin_type)) {
  363. // Use indexed setter instead.
  364. Variant::ValidatedIndexedSetter setter = Variant::get_member_validated_indexed_setter(p_target.type.builtin_type);
  365. append(GDScriptFunction::OPCODE_SET_INDEXED_VALIDATED, 3);
  366. append(p_target);
  367. append(p_index);
  368. append(p_source);
  369. append(setter);
  370. return;
  371. } else if (Variant::get_member_validated_keyed_setter(p_target.type.builtin_type)) {
  372. Variant::ValidatedKeyedSetter setter = Variant::get_member_validated_keyed_setter(p_target.type.builtin_type);
  373. append(GDScriptFunction::OPCODE_SET_KEYED_VALIDATED, 3);
  374. append(p_target);
  375. append(p_index);
  376. append(p_source);
  377. append(setter);
  378. return;
  379. }
  380. }
  381. append(GDScriptFunction::OPCODE_SET_KEYED, 3);
  382. append(p_target);
  383. append(p_index);
  384. append(p_source);
  385. }
  386. void GDScriptByteCodeGenerator::write_get(const Address &p_target, const Address &p_index, const Address &p_source) {
  387. if (HAS_BUILTIN_TYPE(p_source)) {
  388. if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_getter(p_source.type.builtin_type)) {
  389. // Use indexed getter instead.
  390. Variant::ValidatedIndexedGetter getter = Variant::get_member_validated_indexed_getter(p_source.type.builtin_type);
  391. append(GDScriptFunction::OPCODE_GET_INDEXED_VALIDATED, 3);
  392. append(p_source);
  393. append(p_index);
  394. append(p_target);
  395. append(getter);
  396. return;
  397. } else if (Variant::get_member_validated_keyed_getter(p_source.type.builtin_type)) {
  398. Variant::ValidatedKeyedGetter getter = Variant::get_member_validated_keyed_getter(p_source.type.builtin_type);
  399. append(GDScriptFunction::OPCODE_GET_KEYED_VALIDATED, 3);
  400. append(p_source);
  401. append(p_index);
  402. append(p_target);
  403. append(getter);
  404. return;
  405. }
  406. }
  407. append(GDScriptFunction::OPCODE_GET_KEYED, 3);
  408. append(p_source);
  409. append(p_index);
  410. append(p_target);
  411. }
  412. void GDScriptByteCodeGenerator::write_set_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  413. if (HAS_BUILTIN_TYPE(p_target) && Variant::get_member_validated_setter(p_target.type.builtin_type, p_name)) {
  414. Variant::ValidatedSetter setter = Variant::get_member_validated_setter(p_target.type.builtin_type, p_name);
  415. append(GDScriptFunction::OPCODE_SET_NAMED_VALIDATED, 2);
  416. append(p_target);
  417. append(p_source);
  418. append(setter);
  419. return;
  420. }
  421. append(GDScriptFunction::OPCODE_SET_NAMED, 2);
  422. append(p_target);
  423. append(p_source);
  424. append(p_name);
  425. }
  426. void GDScriptByteCodeGenerator::write_get_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  427. if (HAS_BUILTIN_TYPE(p_source) && Variant::get_member_validated_getter(p_source.type.builtin_type, p_name)) {
  428. Variant::ValidatedGetter getter = Variant::get_member_validated_getter(p_source.type.builtin_type, p_name);
  429. append(GDScriptFunction::OPCODE_GET_NAMED_VALIDATED, 2);
  430. append(p_source);
  431. append(p_target);
  432. append(getter);
  433. return;
  434. }
  435. append(GDScriptFunction::OPCODE_GET_NAMED, 2);
  436. append(p_source);
  437. append(p_target);
  438. append(p_name);
  439. }
  440. void GDScriptByteCodeGenerator::write_set_member(const Address &p_value, const StringName &p_name) {
  441. append(GDScriptFunction::OPCODE_SET_MEMBER, 1);
  442. append(p_value);
  443. append(p_name);
  444. }
  445. void GDScriptByteCodeGenerator::write_get_member(const Address &p_target, const StringName &p_name) {
  446. append(GDScriptFunction::OPCODE_GET_MEMBER, 1);
  447. append(p_target);
  448. append(p_name);
  449. }
  450. void GDScriptByteCodeGenerator::write_assign(const Address &p_target, const Address &p_source) {
  451. if (p_target.type.has_type && !p_source.type.has_type) {
  452. // Typed assignment.
  453. switch (p_target.type.kind) {
  454. case GDScriptDataType::BUILTIN: {
  455. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN, 2);
  456. append(p_target);
  457. append(p_source);
  458. append(p_target.type.builtin_type);
  459. } break;
  460. case GDScriptDataType::NATIVE: {
  461. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_target.type.native_type];
  462. class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS);
  463. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE, 3);
  464. append(p_target);
  465. append(p_source);
  466. append(class_idx);
  467. } break;
  468. case GDScriptDataType::SCRIPT:
  469. case GDScriptDataType::GDSCRIPT: {
  470. Variant script = p_target.type.script_type;
  471. int idx = get_constant_pos(script);
  472. idx |= (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
  473. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT, 3);
  474. append(p_target);
  475. append(p_source);
  476. append(idx);
  477. } break;
  478. default: {
  479. ERR_PRINT("Compiler bug: unresolved assign.");
  480. // Shouldn't get here, but fail-safe to a regular assignment
  481. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  482. append(p_target);
  483. append(p_source);
  484. }
  485. }
  486. } else {
  487. if (p_target.type.kind == GDScriptDataType::BUILTIN && p_source.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type != p_source.type.builtin_type) {
  488. // Need conversion..
  489. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN, 2);
  490. append(p_target);
  491. append(p_source);
  492. append(p_target.type.builtin_type);
  493. } else {
  494. // Either untyped assignment or already type-checked by the parser
  495. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  496. append(p_target);
  497. append(p_source);
  498. }
  499. }
  500. }
  501. void GDScriptByteCodeGenerator::write_assign_true(const Address &p_target) {
  502. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  503. append(p_target);
  504. }
  505. void GDScriptByteCodeGenerator::write_assign_false(const Address &p_target) {
  506. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 1);
  507. append(p_target);
  508. }
  509. void GDScriptByteCodeGenerator::write_cast(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) {
  510. int index = 0;
  511. switch (p_type.kind) {
  512. case GDScriptDataType::BUILTIN: {
  513. append(GDScriptFunction::OPCODE_CAST_TO_BUILTIN, 2);
  514. index = p_type.builtin_type;
  515. } break;
  516. case GDScriptDataType::NATIVE: {
  517. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_type.native_type];
  518. class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS);
  519. append(GDScriptFunction::OPCODE_CAST_TO_NATIVE, 3);
  520. index = class_idx;
  521. } break;
  522. case GDScriptDataType::SCRIPT:
  523. case GDScriptDataType::GDSCRIPT: {
  524. Variant script = p_type.script_type;
  525. int idx = get_constant_pos(script);
  526. idx |= (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
  527. append(GDScriptFunction::OPCODE_CAST_TO_SCRIPT, 3);
  528. index = idx;
  529. } break;
  530. default: {
  531. return;
  532. }
  533. }
  534. append(p_source);
  535. append(p_target);
  536. append(index);
  537. }
  538. void GDScriptByteCodeGenerator::write_call(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  539. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  540. for (int i = 0; i < p_arguments.size(); i++) {
  541. append(p_arguments[i]);
  542. }
  543. append(p_base);
  544. append(p_target);
  545. append(p_arguments.size());
  546. append(p_function_name);
  547. }
  548. void GDScriptByteCodeGenerator::write_super_call(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  549. append(GDScriptFunction::OPCODE_CALL_SELF_BASE, 1 + p_arguments.size());
  550. for (int i = 0; i < p_arguments.size(); i++) {
  551. append(p_arguments[i]);
  552. }
  553. append(p_target);
  554. append(p_arguments.size());
  555. append(p_function_name);
  556. }
  557. void GDScriptByteCodeGenerator::write_call_async(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  558. append(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
  559. for (int i = 0; i < p_arguments.size(); i++) {
  560. append(p_arguments[i]);
  561. }
  562. append(p_base);
  563. append(p_target);
  564. append(p_arguments.size());
  565. append(p_function_name);
  566. }
  567. void GDScriptByteCodeGenerator::write_call_builtin(const Address &p_target, GDScriptFunctions::Function p_function, const Vector<Address> &p_arguments) {
  568. append(GDScriptFunction::OPCODE_CALL_BUILT_IN, 1 + p_arguments.size());
  569. for (int i = 0; i < p_arguments.size(); i++) {
  570. append(p_arguments[i]);
  571. }
  572. append(p_target);
  573. append(p_arguments.size());
  574. append(p_function);
  575. }
  576. void GDScriptByteCodeGenerator::write_call_method_bind(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) {
  577. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL_METHOD_BIND : GDScriptFunction::OPCODE_CALL_METHOD_BIND_RET, 2 + p_arguments.size());
  578. for (int i = 0; i < p_arguments.size(); i++) {
  579. append(p_arguments[i]);
  580. }
  581. append(p_base);
  582. append(p_target);
  583. append(p_arguments.size());
  584. append(p_method);
  585. }
  586. void GDScriptByteCodeGenerator::write_call_ptrcall(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) {
  587. #define CASE_TYPE(m_type) \
  588. case Variant::m_type: \
  589. append(GDScriptFunction::OPCODE_CALL_PTRCALL_##m_type, 2 + p_arguments.size()); \
  590. break
  591. bool is_ptrcall = true;
  592. if (p_method->has_return()) {
  593. MethodInfo info;
  594. ClassDB::get_method_info(p_method->get_instance_class(), p_method->get_name(), &info);
  595. switch (info.return_val.type) {
  596. CASE_TYPE(BOOL);
  597. CASE_TYPE(INT);
  598. CASE_TYPE(FLOAT);
  599. CASE_TYPE(STRING);
  600. CASE_TYPE(VECTOR2);
  601. CASE_TYPE(VECTOR2I);
  602. CASE_TYPE(RECT2);
  603. CASE_TYPE(RECT2I);
  604. CASE_TYPE(VECTOR3);
  605. CASE_TYPE(VECTOR3I);
  606. CASE_TYPE(TRANSFORM2D);
  607. CASE_TYPE(PLANE);
  608. CASE_TYPE(AABB);
  609. CASE_TYPE(BASIS);
  610. CASE_TYPE(TRANSFORM);
  611. CASE_TYPE(COLOR);
  612. CASE_TYPE(STRING_NAME);
  613. CASE_TYPE(NODE_PATH);
  614. CASE_TYPE(RID);
  615. CASE_TYPE(QUAT);
  616. CASE_TYPE(OBJECT);
  617. CASE_TYPE(CALLABLE);
  618. CASE_TYPE(SIGNAL);
  619. CASE_TYPE(DICTIONARY);
  620. CASE_TYPE(ARRAY);
  621. CASE_TYPE(PACKED_BYTE_ARRAY);
  622. CASE_TYPE(PACKED_INT32_ARRAY);
  623. CASE_TYPE(PACKED_INT64_ARRAY);
  624. CASE_TYPE(PACKED_FLOAT32_ARRAY);
  625. CASE_TYPE(PACKED_FLOAT64_ARRAY);
  626. CASE_TYPE(PACKED_STRING_ARRAY);
  627. CASE_TYPE(PACKED_VECTOR2_ARRAY);
  628. CASE_TYPE(PACKED_VECTOR3_ARRAY);
  629. CASE_TYPE(PACKED_COLOR_ARRAY);
  630. default:
  631. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL_METHOD_BIND : GDScriptFunction::OPCODE_CALL_METHOD_BIND_RET, 2 + p_arguments.size());
  632. is_ptrcall = false;
  633. break;
  634. }
  635. } else {
  636. append(GDScriptFunction::OPCODE_CALL_PTRCALL_NO_RETURN, 2 + p_arguments.size());
  637. }
  638. for (int i = 0; i < p_arguments.size(); i++) {
  639. append(p_arguments[i]);
  640. }
  641. append(p_base);
  642. append(p_target);
  643. append(p_arguments.size());
  644. append(p_method);
  645. if (is_ptrcall) {
  646. alloc_ptrcall(p_arguments.size());
  647. }
  648. #undef CASE_TYPE
  649. }
  650. void GDScriptByteCodeGenerator::write_call_self(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  651. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  652. for (int i = 0; i < p_arguments.size(); i++) {
  653. append(p_arguments[i]);
  654. }
  655. append(GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS);
  656. append(p_target);
  657. append(p_arguments.size());
  658. append(p_function_name);
  659. }
  660. void GDScriptByteCodeGenerator::write_call_script_function(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  661. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  662. for (int i = 0; i < p_arguments.size(); i++) {
  663. append(p_arguments[i]);
  664. }
  665. append(p_base);
  666. append(p_target);
  667. append(p_arguments.size());
  668. append(p_function_name);
  669. }
  670. void GDScriptByteCodeGenerator::write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) {
  671. append(GDScriptFunction::OPCODE_CONSTRUCT, 1 + p_arguments.size());
  672. for (int i = 0; i < p_arguments.size(); i++) {
  673. append(p_arguments[i]);
  674. }
  675. append(p_target);
  676. append(p_arguments.size());
  677. append(p_type);
  678. }
  679. void GDScriptByteCodeGenerator::write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) {
  680. append(GDScriptFunction::OPCODE_CONSTRUCT_ARRAY, 1 + p_arguments.size());
  681. for (int i = 0; i < p_arguments.size(); i++) {
  682. append(p_arguments[i]);
  683. }
  684. append(p_target);
  685. append(p_arguments.size());
  686. }
  687. void GDScriptByteCodeGenerator::write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) {
  688. append(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY, 1 + p_arguments.size());
  689. for (int i = 0; i < p_arguments.size(); i++) {
  690. append(p_arguments[i]);
  691. }
  692. append(p_target);
  693. append(p_arguments.size() / 2); // This is number of key-value pairs, so only half of actual arguments.
  694. }
  695. void GDScriptByteCodeGenerator::write_await(const Address &p_target, const Address &p_operand) {
  696. append(GDScriptFunction::OPCODE_AWAIT, 1);
  697. append(p_operand);
  698. append(GDScriptFunction::OPCODE_AWAIT_RESUME, 1);
  699. append(p_target);
  700. }
  701. void GDScriptByteCodeGenerator::write_if(const Address &p_condition) {
  702. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  703. append(p_condition);
  704. if_jmp_addrs.push_back(opcodes.size());
  705. append(0); // Jump destination, will be patched.
  706. }
  707. void GDScriptByteCodeGenerator::write_else() {
  708. append(GDScriptFunction::OPCODE_JUMP, 0); // Jump from true if block;
  709. int else_jmp_addr = opcodes.size();
  710. append(0); // Jump destination, will be patched.
  711. patch_jump(if_jmp_addrs.back()->get());
  712. if_jmp_addrs.pop_back();
  713. if_jmp_addrs.push_back(else_jmp_addr);
  714. }
  715. void GDScriptByteCodeGenerator::write_endif() {
  716. patch_jump(if_jmp_addrs.back()->get());
  717. if_jmp_addrs.pop_back();
  718. }
  719. void GDScriptByteCodeGenerator::write_for(const Address &p_variable, const Address &p_list) {
  720. int counter_pos = add_temporary() | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  721. int container_pos = add_temporary() | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  722. current_breaks_to_patch.push_back(List<int>());
  723. // Assign container.
  724. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  725. append(container_pos);
  726. append(p_list);
  727. // Begin loop.
  728. append(GDScriptFunction::OPCODE_ITERATE_BEGIN, 3);
  729. append(counter_pos);
  730. append(container_pos);
  731. append(p_variable);
  732. for_jmp_addrs.push_back(opcodes.size());
  733. append(0); // End of loop address, will be patched.
  734. append(GDScriptFunction::OPCODE_JUMP, 0);
  735. append(opcodes.size() + 6); // Skip over 'continue' code.
  736. // Next iteration.
  737. int continue_addr = opcodes.size();
  738. continue_addrs.push_back(continue_addr);
  739. append(GDScriptFunction::OPCODE_ITERATE, 3);
  740. append(counter_pos);
  741. append(container_pos);
  742. append(p_variable);
  743. for_jmp_addrs.push_back(opcodes.size());
  744. append(0); // Jump destination, will be patched.
  745. }
  746. void GDScriptByteCodeGenerator::write_endfor() {
  747. // Jump back to loop check.
  748. append(GDScriptFunction::OPCODE_JUMP, 0);
  749. append(continue_addrs.back()->get());
  750. continue_addrs.pop_back();
  751. // Patch end jumps (two of them).
  752. for (int i = 0; i < 2; i++) {
  753. patch_jump(for_jmp_addrs.back()->get());
  754. for_jmp_addrs.pop_back();
  755. }
  756. // Patch break statements.
  757. for (const List<int>::Element *E = current_breaks_to_patch.back()->get().front(); E; E = E->next()) {
  758. patch_jump(E->get());
  759. }
  760. current_breaks_to_patch.pop_back();
  761. // Remove loop temporaries.
  762. pop_temporary();
  763. pop_temporary();
  764. }
  765. void GDScriptByteCodeGenerator::start_while_condition() {
  766. current_breaks_to_patch.push_back(List<int>());
  767. continue_addrs.push_back(opcodes.size());
  768. }
  769. void GDScriptByteCodeGenerator::write_while(const Address &p_condition) {
  770. // Condition check.
  771. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  772. append(p_condition);
  773. while_jmp_addrs.push_back(opcodes.size());
  774. append(0); // End of loop address, will be patched.
  775. }
  776. void GDScriptByteCodeGenerator::write_endwhile() {
  777. // Jump back to loop check.
  778. append(GDScriptFunction::OPCODE_JUMP, 0);
  779. append(continue_addrs.back()->get());
  780. continue_addrs.pop_back();
  781. // Patch end jump.
  782. patch_jump(while_jmp_addrs.back()->get());
  783. while_jmp_addrs.pop_back();
  784. // Patch break statements.
  785. for (const List<int>::Element *E = current_breaks_to_patch.back()->get().front(); E; E = E->next()) {
  786. patch_jump(E->get());
  787. }
  788. current_breaks_to_patch.pop_back();
  789. }
  790. void GDScriptByteCodeGenerator::start_match() {
  791. match_continues_to_patch.push_back(List<int>());
  792. }
  793. void GDScriptByteCodeGenerator::start_match_branch() {
  794. // Patch continue statements.
  795. for (const List<int>::Element *E = match_continues_to_patch.back()->get().front(); E; E = E->next()) {
  796. patch_jump(E->get());
  797. }
  798. match_continues_to_patch.pop_back();
  799. // Start a new list for next branch.
  800. match_continues_to_patch.push_back(List<int>());
  801. }
  802. void GDScriptByteCodeGenerator::end_match() {
  803. // Patch continue statements.
  804. for (const List<int>::Element *E = match_continues_to_patch.back()->get().front(); E; E = E->next()) {
  805. patch_jump(E->get());
  806. }
  807. match_continues_to_patch.pop_back();
  808. }
  809. void GDScriptByteCodeGenerator::write_break() {
  810. append(GDScriptFunction::OPCODE_JUMP, 0);
  811. current_breaks_to_patch.back()->get().push_back(opcodes.size());
  812. append(0);
  813. }
  814. void GDScriptByteCodeGenerator::write_continue() {
  815. append(GDScriptFunction::OPCODE_JUMP, 0);
  816. append(continue_addrs.back()->get());
  817. }
  818. void GDScriptByteCodeGenerator::write_continue_match() {
  819. append(GDScriptFunction::OPCODE_JUMP, 0);
  820. match_continues_to_patch.back()->get().push_back(opcodes.size());
  821. append(0);
  822. }
  823. void GDScriptByteCodeGenerator::write_breakpoint() {
  824. append(GDScriptFunction::OPCODE_BREAKPOINT, 0);
  825. }
  826. void GDScriptByteCodeGenerator::write_newline(int p_line) {
  827. append(GDScriptFunction::OPCODE_LINE, 0);
  828. append(p_line);
  829. current_line = p_line;
  830. }
  831. void GDScriptByteCodeGenerator::write_return(const Address &p_return_value) {
  832. append(GDScriptFunction::OPCODE_RETURN, 1);
  833. append(p_return_value);
  834. }
  835. void GDScriptByteCodeGenerator::write_assert(const Address &p_test, const Address &p_message) {
  836. append(GDScriptFunction::OPCODE_ASSERT, 2);
  837. append(p_test);
  838. append(p_message);
  839. }
  840. void GDScriptByteCodeGenerator::start_block() {
  841. push_stack_identifiers();
  842. }
  843. void GDScriptByteCodeGenerator::end_block() {
  844. pop_stack_identifiers();
  845. }
  846. GDScriptByteCodeGenerator::~GDScriptByteCodeGenerator() {
  847. if (!ended && function != nullptr) {
  848. memdelete(function);
  849. }
  850. }