123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735 |
- /*************************************************************************/
- /* gdscript_byte_codegen.cpp */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
- /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /*************************************************************************/
- #include "gdscript_byte_codegen.h"
- #include "core/debugger/engine_debugger.h"
- #include "gdscript.h"
- uint32_t GDScriptByteCodeGenerator::add_parameter(const StringName &p_name, bool p_is_optional, const GDScriptDataType &p_type) {
- #ifdef TOOLS_ENABLED
- function->arg_names.push_back(p_name);
- #endif
- function->_argument_count++;
- function->argument_types.push_back(p_type);
- if (p_is_optional) {
- if (function->_default_arg_count == 0) {
- append(GDScriptFunction::OPCODE_JUMP_TO_DEF_ARGUMENT);
- }
- function->default_arguments.push_back(opcodes.size());
- function->_default_arg_count++;
- }
- return add_local(p_name, p_type);
- }
- uint32_t GDScriptByteCodeGenerator::add_local(const StringName &p_name, const GDScriptDataType &p_type) {
- int stack_pos = increase_stack();
- add_stack_identifier(p_name, stack_pos);
- return stack_pos;
- }
- uint32_t GDScriptByteCodeGenerator::add_local_constant(const StringName &p_name, const Variant &p_constant) {
- int index = add_or_get_constant(p_constant);
- local_constants[p_name] = index;
- return index;
- }
- uint32_t GDScriptByteCodeGenerator::add_or_get_constant(const Variant &p_constant) {
- if (constant_map.has(p_constant)) {
- return constant_map[p_constant];
- }
- int index = constant_map.size();
- constant_map[p_constant] = index;
- return index;
- }
- uint32_t GDScriptByteCodeGenerator::add_or_get_name(const StringName &p_name) {
- return get_name_map_pos(p_name);
- }
- uint32_t GDScriptByteCodeGenerator::add_temporary() {
- current_temporaries++;
- return increase_stack();
- }
- void GDScriptByteCodeGenerator::pop_temporary() {
- current_stack_size--;
- current_temporaries--;
- }
- void GDScriptByteCodeGenerator::start_parameters() {}
- void GDScriptByteCodeGenerator::end_parameters() {
- function->default_arguments.invert();
- }
- 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) {
- function = memnew(GDScriptFunction);
- debug_stack = EngineDebugger::is_active();
- function->name = p_function_name;
- function->_script = p_script;
- function->source = p_script->get_path();
- #ifdef DEBUG_ENABLED
- function->func_cname = (String(function->source) + " - " + String(p_function_name)).utf8();
- function->_func_cname = function->func_cname.get_data();
- #endif
- function->_static = p_static;
- function->return_type = p_return_type;
- function->rpc_mode = p_rpc_mode;
- function->_argument_count = 0;
- }
- GDScriptFunction *GDScriptByteCodeGenerator::write_end() {
- append(GDScriptFunction::OPCODE_END, 0);
- if (constant_map.size()) {
- function->_constant_count = constant_map.size();
- function->constants.resize(constant_map.size());
- function->_constants_ptr = function->constants.ptrw();
- const Variant *K = nullptr;
- while ((K = constant_map.next(K))) {
- int idx = constant_map[*K];
- function->constants.write[idx] = *K;
- }
- } else {
- function->_constants_ptr = nullptr;
- function->_constant_count = 0;
- }
- if (name_map.size()) {
- function->global_names.resize(name_map.size());
- function->_global_names_ptr = &function->global_names[0];
- for (Map<StringName, int>::Element *E = name_map.front(); E; E = E->next()) {
- function->global_names.write[E->get()] = E->key();
- }
- function->_global_names_count = function->global_names.size();
- } else {
- function->_global_names_ptr = nullptr;
- function->_global_names_count = 0;
- }
- if (opcodes.size()) {
- function->code = opcodes;
- function->_code_ptr = &function->code[0];
- function->_code_size = opcodes.size();
- } else {
- function->_code_ptr = nullptr;
- function->_code_size = 0;
- }
- if (function->default_arguments.size()) {
- function->_default_arg_count = function->default_arguments.size();
- function->_default_arg_ptr = &function->default_arguments[0];
- } else {
- function->_default_arg_count = 0;
- function->_default_arg_ptr = nullptr;
- }
- if (debug_stack) {
- function->stack_debug = stack_debug;
- }
- function->_stack_size = stack_max;
- function->_instruction_args_size = instr_args_max;
- ended = true;
- return function;
- }
- #ifdef DEBUG_ENABLED
- void GDScriptByteCodeGenerator::set_signature(const String &p_signature) {
- function->profile.signature = p_signature;
- }
- #endif
- void GDScriptByteCodeGenerator::set_initial_line(int p_line) {
- function->_initial_line = p_line;
- }
- void GDScriptByteCodeGenerator::write_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) {
- append(GDScriptFunction::OPCODE_OPERATOR, 3);
- append(p_left_operand);
- append(p_right_operand);
- append(p_target);
- append(p_operator);
- }
- void GDScriptByteCodeGenerator::write_type_test(const Address &p_target, const Address &p_source, const Address &p_type) {
- append(GDScriptFunction::OPCODE_EXTENDS_TEST, 3);
- append(p_source);
- append(p_type);
- append(p_target);
- }
- void GDScriptByteCodeGenerator::write_type_test_builtin(const Address &p_target, const Address &p_source, Variant::Type p_type) {
- append(GDScriptFunction::OPCODE_IS_BUILTIN, 3);
- append(p_source);
- append(p_target);
- append(p_type);
- }
- void GDScriptByteCodeGenerator::write_and_left_operand(const Address &p_left_operand) {
- append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
- append(p_left_operand);
- logic_op_jump_pos1.push_back(opcodes.size());
- append(0); // Jump target, will be patched.
- }
- void GDScriptByteCodeGenerator::write_and_right_operand(const Address &p_right_operand) {
- append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
- append(p_right_operand);
- logic_op_jump_pos2.push_back(opcodes.size());
- append(0); // Jump target, will be patched.
- }
- void GDScriptByteCodeGenerator::write_end_and(const Address &p_target) {
- // If here means both operands are true.
- append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
- append(p_target);
- // Jump away from the fail condition.
- append(GDScriptFunction::OPCODE_JUMP, 0);
- append(opcodes.size() + 3);
- // Here it means one of operands is false.
- patch_jump(logic_op_jump_pos1.back()->get());
- patch_jump(logic_op_jump_pos2.back()->get());
- logic_op_jump_pos1.pop_back();
- logic_op_jump_pos2.pop_back();
- append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 0);
- append(p_target);
- }
- void GDScriptByteCodeGenerator::write_or_left_operand(const Address &p_left_operand) {
- append(GDScriptFunction::OPCODE_JUMP_IF, 1);
- append(p_left_operand);
- logic_op_jump_pos1.push_back(opcodes.size());
- append(0); // Jump target, will be patched.
- }
- void GDScriptByteCodeGenerator::write_or_right_operand(const Address &p_right_operand) {
- append(GDScriptFunction::OPCODE_JUMP_IF, 1);
- append(p_right_operand);
- logic_op_jump_pos2.push_back(opcodes.size());
- append(0); // Jump target, will be patched.
- }
- void GDScriptByteCodeGenerator::write_end_or(const Address &p_target) {
- // If here means both operands are false.
- append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 1);
- append(p_target);
- // Jump away from the success condition.
- append(GDScriptFunction::OPCODE_JUMP, 0);
- append(opcodes.size() + 3);
- // Here it means one of operands is false.
- patch_jump(logic_op_jump_pos1.back()->get());
- patch_jump(logic_op_jump_pos2.back()->get());
- logic_op_jump_pos1.pop_back();
- logic_op_jump_pos2.pop_back();
- append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
- append(p_target);
- }
- void GDScriptByteCodeGenerator::write_start_ternary(const Address &p_target) {
- ternary_result.push_back(p_target);
- }
- void GDScriptByteCodeGenerator::write_ternary_condition(const Address &p_condition) {
- append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
- append(p_condition);
- ternary_jump_fail_pos.push_back(opcodes.size());
- append(0); // Jump target, will be patched.
- }
- void GDScriptByteCodeGenerator::write_ternary_true_expr(const Address &p_expr) {
- append(GDScriptFunction::OPCODE_ASSIGN, 2);
- append(ternary_result.back()->get());
- append(p_expr);
- // Jump away from the false path.
- append(GDScriptFunction::OPCODE_JUMP, 0);
- ternary_jump_skip_pos.push_back(opcodes.size());
- append(0);
- // Fail must jump here.
- patch_jump(ternary_jump_fail_pos.back()->get());
- ternary_jump_fail_pos.pop_back();
- }
- void GDScriptByteCodeGenerator::write_ternary_false_expr(const Address &p_expr) {
- append(GDScriptFunction::OPCODE_ASSIGN, 2);
- append(ternary_result.back()->get());
- append(p_expr);
- }
- void GDScriptByteCodeGenerator::write_end_ternary() {
- patch_jump(ternary_jump_skip_pos.back()->get());
- ternary_jump_skip_pos.pop_back();
- }
- void GDScriptByteCodeGenerator::write_set(const Address &p_target, const Address &p_index, const Address &p_source) {
- append(GDScriptFunction::OPCODE_SET, 3);
- append(p_target);
- append(p_index);
- append(p_source);
- }
- void GDScriptByteCodeGenerator::write_get(const Address &p_target, const Address &p_index, const Address &p_source) {
- append(GDScriptFunction::OPCODE_GET, 3);
- append(p_source);
- append(p_index);
- append(p_target);
- }
- void GDScriptByteCodeGenerator::write_set_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
- append(GDScriptFunction::OPCODE_SET_NAMED, 2);
- append(p_target);
- append(p_source);
- append(p_name);
- }
- void GDScriptByteCodeGenerator::write_get_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
- append(GDScriptFunction::OPCODE_GET_NAMED, 2);
- append(p_source);
- append(p_target);
- append(p_name);
- }
- void GDScriptByteCodeGenerator::write_set_member(const Address &p_value, const StringName &p_name) {
- append(GDScriptFunction::OPCODE_SET_MEMBER, 1);
- append(p_value);
- append(p_name);
- }
- void GDScriptByteCodeGenerator::write_get_member(const Address &p_target, const StringName &p_name) {
- append(GDScriptFunction::OPCODE_GET_MEMBER, 1);
- append(p_target);
- append(p_name);
- }
- void GDScriptByteCodeGenerator::write_assign(const Address &p_target, const Address &p_source) {
- if (p_target.type.has_type && !p_source.type.has_type) {
- // Typed assignment.
- switch (p_target.type.kind) {
- case GDScriptDataType::BUILTIN: {
- append(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN, 2);
- append(p_target);
- append(p_source);
- append(p_target.type.builtin_type);
- } break;
- case GDScriptDataType::NATIVE: {
- int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_target.type.native_type];
- class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS);
- append(GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE, 3);
- append(p_target);
- append(p_source);
- append(class_idx);
- } break;
- case GDScriptDataType::SCRIPT:
- case GDScriptDataType::GDSCRIPT: {
- Variant script = p_target.type.script_type;
- int idx = get_constant_pos(script);
- idx |= (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
- append(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT, 3);
- append(p_target);
- append(p_source);
- append(idx);
- } break;
- default: {
- ERR_PRINT("Compiler bug: unresolved assign.");
- // Shouldn't get here, but fail-safe to a regular assignment
- append(GDScriptFunction::OPCODE_ASSIGN, 2);
- append(p_target);
- append(p_source);
- }
- }
- } else {
- if (p_target.type.kind == GDScriptDataType::BUILTIN && p_source.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type != p_source.type.builtin_type) {
- // Need conversion..
- append(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN, 2);
- append(p_target);
- append(p_source);
- append(p_target.type.builtin_type);
- } else {
- // Either untyped assignment or already type-checked by the parser
- append(GDScriptFunction::OPCODE_ASSIGN, 2);
- append(p_target);
- append(p_source);
- }
- }
- }
- void GDScriptByteCodeGenerator::write_assign_true(const Address &p_target) {
- append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
- append(p_target);
- }
- void GDScriptByteCodeGenerator::write_assign_false(const Address &p_target) {
- append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 1);
- append(p_target);
- }
- void GDScriptByteCodeGenerator::write_cast(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) {
- int index = 0;
- switch (p_type.kind) {
- case GDScriptDataType::BUILTIN: {
- append(GDScriptFunction::OPCODE_CAST_TO_BUILTIN, 2);
- index = p_type.builtin_type;
- } break;
- case GDScriptDataType::NATIVE: {
- int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_type.native_type];
- class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS);
- append(GDScriptFunction::OPCODE_CAST_TO_NATIVE, 3);
- index = class_idx;
- } break;
- case GDScriptDataType::SCRIPT:
- case GDScriptDataType::GDSCRIPT: {
- Variant script = p_type.script_type;
- int idx = get_constant_pos(script);
- idx |= (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
- append(GDScriptFunction::OPCODE_CAST_TO_SCRIPT, 3);
- index = idx;
- } break;
- default: {
- return;
- }
- }
- append(p_source);
- append(p_target);
- append(index);
- }
- void GDScriptByteCodeGenerator::write_call(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
- append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
- for (int i = 0; i < p_arguments.size(); i++) {
- append(p_arguments[i]);
- }
- append(p_base);
- append(p_target);
- append(p_arguments.size());
- append(p_function_name);
- }
- void GDScriptByteCodeGenerator::write_super_call(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
- append(GDScriptFunction::OPCODE_CALL_SELF_BASE, 1 + p_arguments.size());
- for (int i = 0; i < p_arguments.size(); i++) {
- append(p_arguments[i]);
- }
- append(p_target);
- append(p_arguments.size());
- append(p_function_name);
- }
- void GDScriptByteCodeGenerator::write_call_async(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
- append(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
- for (int i = 0; i < p_arguments.size(); i++) {
- append(p_arguments[i]);
- }
- append(p_base);
- append(p_target);
- append(p_arguments.size());
- append(p_function_name);
- }
- void GDScriptByteCodeGenerator::write_call_builtin(const Address &p_target, GDScriptFunctions::Function p_function, const Vector<Address> &p_arguments) {
- append(GDScriptFunction::OPCODE_CALL_BUILT_IN, 1 + p_arguments.size());
- for (int i = 0; i < p_arguments.size(); i++) {
- append(p_arguments[i]);
- }
- append(p_target);
- append(p_arguments.size());
- append(p_function);
- }
- void GDScriptByteCodeGenerator::write_call_method_bind(const Address &p_target, const Address &p_base, const MethodBind *p_method, const Vector<Address> &p_arguments) {
- append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
- for (int i = 0; i < p_arguments.size(); i++) {
- append(p_arguments[i]);
- }
- append(p_base);
- append(p_target);
- append(p_arguments.size());
- append(p_method->get_name());
- }
- void GDScriptByteCodeGenerator::write_call_ptrcall(const Address &p_target, const Address &p_base, const MethodBind *p_method, const Vector<Address> &p_arguments) {
- append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
- for (int i = 0; i < p_arguments.size(); i++) {
- append(p_arguments[i]);
- }
- append(p_base);
- append(p_target);
- append(p_arguments.size());
- append(p_method->get_name());
- }
- void GDScriptByteCodeGenerator::write_call_self(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
- append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
- for (int i = 0; i < p_arguments.size(); i++) {
- append(p_arguments[i]);
- }
- append(GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS);
- append(p_target);
- append(p_arguments.size());
- append(p_function_name);
- }
- void GDScriptByteCodeGenerator::write_call_script_function(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
- append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
- for (int i = 0; i < p_arguments.size(); i++) {
- append(p_arguments[i]);
- }
- append(p_base);
- append(p_target);
- append(p_arguments.size());
- append(p_function_name);
- }
- void GDScriptByteCodeGenerator::write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) {
- append(GDScriptFunction::OPCODE_CONSTRUCT, 1 + p_arguments.size());
- for (int i = 0; i < p_arguments.size(); i++) {
- append(p_arguments[i]);
- }
- append(p_target);
- append(p_arguments.size());
- append(p_type);
- }
- void GDScriptByteCodeGenerator::write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) {
- append(GDScriptFunction::OPCODE_CONSTRUCT_ARRAY, 1 + p_arguments.size());
- for (int i = 0; i < p_arguments.size(); i++) {
- append(p_arguments[i]);
- }
- append(p_target);
- append(p_arguments.size());
- }
- void GDScriptByteCodeGenerator::write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) {
- append(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY, 1 + p_arguments.size());
- for (int i = 0; i < p_arguments.size(); i++) {
- append(p_arguments[i]);
- }
- append(p_target);
- append(p_arguments.size() / 2); // This is number of key-value pairs, so only half of actual arguments.
- }
- void GDScriptByteCodeGenerator::write_await(const Address &p_target, const Address &p_operand) {
- append(GDScriptFunction::OPCODE_AWAIT, 1);
- append(p_operand);
- append(GDScriptFunction::OPCODE_AWAIT_RESUME, 1);
- append(p_target);
- }
- void GDScriptByteCodeGenerator::write_if(const Address &p_condition) {
- append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
- append(p_condition);
- if_jmp_addrs.push_back(opcodes.size());
- append(0); // Jump destination, will be patched.
- }
- void GDScriptByteCodeGenerator::write_else() {
- append(GDScriptFunction::OPCODE_JUMP, 0); // Jump from true if block;
- int else_jmp_addr = opcodes.size();
- append(0); // Jump destination, will be patched.
- patch_jump(if_jmp_addrs.back()->get());
- if_jmp_addrs.pop_back();
- if_jmp_addrs.push_back(else_jmp_addr);
- }
- void GDScriptByteCodeGenerator::write_endif() {
- patch_jump(if_jmp_addrs.back()->get());
- if_jmp_addrs.pop_back();
- }
- void GDScriptByteCodeGenerator::write_for(const Address &p_variable, const Address &p_list) {
- int counter_pos = add_temporary() | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
- int container_pos = add_temporary() | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
- current_breaks_to_patch.push_back(List<int>());
- // Assign container.
- append(GDScriptFunction::OPCODE_ASSIGN, 2);
- append(container_pos);
- append(p_list);
- // Begin loop.
- append(GDScriptFunction::OPCODE_ITERATE_BEGIN, 3);
- append(counter_pos);
- append(container_pos);
- append(p_variable);
- for_jmp_addrs.push_back(opcodes.size());
- append(0); // End of loop address, will be patched.
- append(GDScriptFunction::OPCODE_JUMP, 0);
- append(opcodes.size() + 6); // Skip over 'continue' code.
- // Next iteration.
- int continue_addr = opcodes.size();
- continue_addrs.push_back(continue_addr);
- append(GDScriptFunction::OPCODE_ITERATE, 3);
- append(counter_pos);
- append(container_pos);
- append(p_variable);
- for_jmp_addrs.push_back(opcodes.size());
- append(0); // Jump destination, will be patched.
- }
- void GDScriptByteCodeGenerator::write_endfor() {
- // Jump back to loop check.
- append(GDScriptFunction::OPCODE_JUMP, 0);
- append(continue_addrs.back()->get());
- continue_addrs.pop_back();
- // Patch end jumps (two of them).
- for (int i = 0; i < 2; i++) {
- patch_jump(for_jmp_addrs.back()->get());
- for_jmp_addrs.pop_back();
- }
- // Patch break statements.
- for (const List<int>::Element *E = current_breaks_to_patch.back()->get().front(); E; E = E->next()) {
- patch_jump(E->get());
- }
- current_breaks_to_patch.pop_back();
- // Remove loop temporaries.
- pop_temporary();
- pop_temporary();
- }
- void GDScriptByteCodeGenerator::start_while_condition() {
- current_breaks_to_patch.push_back(List<int>());
- continue_addrs.push_back(opcodes.size());
- }
- void GDScriptByteCodeGenerator::write_while(const Address &p_condition) {
- // Condition check.
- append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
- append(p_condition);
- while_jmp_addrs.push_back(opcodes.size());
- append(0); // End of loop address, will be patched.
- }
- void GDScriptByteCodeGenerator::write_endwhile() {
- // Jump back to loop check.
- append(GDScriptFunction::OPCODE_JUMP, 0);
- append(continue_addrs.back()->get());
- continue_addrs.pop_back();
- // Patch end jump.
- patch_jump(while_jmp_addrs.back()->get());
- while_jmp_addrs.pop_back();
- // Patch break statements.
- for (const List<int>::Element *E = current_breaks_to_patch.back()->get().front(); E; E = E->next()) {
- patch_jump(E->get());
- }
- current_breaks_to_patch.pop_back();
- }
- void GDScriptByteCodeGenerator::start_match() {
- match_continues_to_patch.push_back(List<int>());
- }
- void GDScriptByteCodeGenerator::start_match_branch() {
- // Patch continue statements.
- for (const List<int>::Element *E = match_continues_to_patch.back()->get().front(); E; E = E->next()) {
- patch_jump(E->get());
- }
- match_continues_to_patch.pop_back();
- // Start a new list for next branch.
- match_continues_to_patch.push_back(List<int>());
- }
- void GDScriptByteCodeGenerator::end_match() {
- // Patch continue statements.
- for (const List<int>::Element *E = match_continues_to_patch.back()->get().front(); E; E = E->next()) {
- patch_jump(E->get());
- }
- match_continues_to_patch.pop_back();
- }
- void GDScriptByteCodeGenerator::write_break() {
- append(GDScriptFunction::OPCODE_JUMP, 0);
- current_breaks_to_patch.back()->get().push_back(opcodes.size());
- append(0);
- }
- void GDScriptByteCodeGenerator::write_continue() {
- append(GDScriptFunction::OPCODE_JUMP, 0);
- append(continue_addrs.back()->get());
- }
- void GDScriptByteCodeGenerator::write_continue_match() {
- append(GDScriptFunction::OPCODE_JUMP, 0);
- match_continues_to_patch.back()->get().push_back(opcodes.size());
- append(0);
- }
- void GDScriptByteCodeGenerator::write_breakpoint() {
- append(GDScriptFunction::OPCODE_BREAKPOINT, 0);
- }
- void GDScriptByteCodeGenerator::write_newline(int p_line) {
- append(GDScriptFunction::OPCODE_LINE, 0);
- append(p_line);
- current_line = p_line;
- }
- void GDScriptByteCodeGenerator::write_return(const Address &p_return_value) {
- append(GDScriptFunction::OPCODE_RETURN, 1);
- append(p_return_value);
- }
- void GDScriptByteCodeGenerator::write_assert(const Address &p_test, const Address &p_message) {
- append(GDScriptFunction::OPCODE_ASSERT, 2);
- append(p_test);
- append(p_message);
- }
- void GDScriptByteCodeGenerator::start_block() {
- push_stack_identifiers();
- }
- void GDScriptByteCodeGenerator::end_block() {
- pop_stack_identifiers();
- }
- GDScriptByteCodeGenerator::~GDScriptByteCodeGenerator() {
- if (!ended && function != nullptr) {
- memdelete(function);
- }
- }
|