Merge pull request #41338 from vnen/gdscript-compiler-abstraction
Add GDScript code generation abstraction
This commit is contained in:
commit
8e052e1a06
|
@ -505,6 +505,9 @@ void GDScriptAnalyzer::resolve_class_interface(GDScriptParser::ClassNode *p_clas
|
|||
member.variable->set_datatype(datatype); // Allow recursive usage.
|
||||
reduce_expression(member.variable->initializer);
|
||||
datatype = member.variable->initializer->get_datatype();
|
||||
if (datatype.type_source != GDScriptParser::DataType::UNDETECTED) {
|
||||
datatype.type_source = GDScriptParser::DataType::INFERRED;
|
||||
}
|
||||
}
|
||||
|
||||
if (member.variable->datatype_specifier != nullptr) {
|
||||
|
@ -540,6 +543,7 @@ void GDScriptAnalyzer::resolve_class_interface(GDScriptParser::ClassNode *p_clas
|
|||
} else if (datatype.builtin_type == Variant::NIL) {
|
||||
push_error(vformat(R"(Cannot infer the type of "%s" variable because the initial value is "null".)", member.variable->identifier->name), member.variable->initializer);
|
||||
}
|
||||
datatype.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
|
||||
}
|
||||
|
||||
datatype.is_constant = false;
|
||||
|
@ -914,6 +918,7 @@ void GDScriptAnalyzer::decide_suite_type(GDScriptParser::Node *p_suite, GDScript
|
|||
p_suite->datatype.type_source = GDScriptParser::DataType::UNDETECTED;
|
||||
} else {
|
||||
p_suite->set_datatype(p_statement->get_datatype());
|
||||
p_suite->datatype.type_source = GDScriptParser::DataType::INFERRED;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
|
736
modules/gdscript/gdscript_byte_codegen.cpp
Normal file
736
modules/gdscript/gdscript_byte_codegen.cpp
Normal file
|
@ -0,0 +1,736 @@
|
|||
/*************************************************************************/
|
||||
/* 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);
|
||||
|
||||
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->_call_size = call_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);
|
||||
append(p_operator);
|
||||
append(p_left_operand);
|
||||
append(p_right_operand);
|
||||
append(p_target);
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_type_test(const Address &p_target, const Address &p_source, const Address &p_type) {
|
||||
append(GDScriptFunction::OPCODE_EXTENDS_TEST);
|
||||
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);
|
||||
append(p_source);
|
||||
append(p_type);
|
||||
append(p_target);
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_and_left_operand(const Address &p_left_operand) {
|
||||
append(GDScriptFunction::OPCODE_JUMP_IF_NOT);
|
||||
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);
|
||||
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);
|
||||
append(p_target);
|
||||
// Jump away from the fail condition.
|
||||
append(GDScriptFunction::OPCODE_JUMP);
|
||||
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);
|
||||
append(p_target);
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_or_left_operand(const Address &p_left_operand) {
|
||||
append(GDScriptFunction::OPCODE_JUMP_IF);
|
||||
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);
|
||||
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);
|
||||
append(p_target);
|
||||
// Jump away from the success condition.
|
||||
append(GDScriptFunction::OPCODE_JUMP);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
append(ternary_result.back()->get());
|
||||
append(p_expr);
|
||||
// Jump away from the false path.
|
||||
append(GDScriptFunction::OPCODE_JUMP);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
append(p_target);
|
||||
append(p_name);
|
||||
append(p_source);
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_get_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
|
||||
append(GDScriptFunction::OPCODE_GET_NAMED);
|
||||
append(p_source);
|
||||
append(p_name);
|
||||
append(p_target);
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_set_member(const Address &p_value, const StringName &p_name) {
|
||||
append(GDScriptFunction::OPCODE_SET_MEMBER);
|
||||
append(p_name);
|
||||
append(p_value);
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_get_member(const Address &p_target, const StringName &p_name) {
|
||||
append(GDScriptFunction::OPCODE_GET_MEMBER);
|
||||
append(p_name);
|
||||
append(p_target);
|
||||
}
|
||||
|
||||
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);
|
||||
append(p_target.type.builtin_type);
|
||||
append(p_target);
|
||||
append(p_source);
|
||||
} 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);
|
||||
append(class_idx);
|
||||
append(p_target);
|
||||
append(p_source);
|
||||
} break;
|
||||
case GDScriptDataType::SCRIPT:
|
||||
case GDScriptDataType::GDSCRIPT: {
|
||||
Variant script = p_target.type.script_type;
|
||||
int idx = get_constant_pos(script);
|
||||
|
||||
append(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT);
|
||||
append(idx);
|
||||
append(p_target);
|
||||
append(p_source);
|
||||
} break;
|
||||
default: {
|
||||
ERR_PRINT("Compiler bug: unresolved assign.");
|
||||
|
||||
// Shouldn't get here, but fail-safe to a regular assignment
|
||||
append(GDScriptFunction::OPCODE_ASSIGN);
|
||||
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);
|
||||
append(p_target.type.builtin_type);
|
||||
append(p_target);
|
||||
append(p_source);
|
||||
} else {
|
||||
// Either untyped assignment or already type-checked by the parser
|
||||
append(GDScriptFunction::OPCODE_ASSIGN);
|
||||
append(p_target);
|
||||
append(p_source);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_assign_true(const Address &p_target) {
|
||||
append(GDScriptFunction::OPCODE_ASSIGN_TRUE);
|
||||
append(p_target);
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_assign_false(const Address &p_target) {
|
||||
append(GDScriptFunction::OPCODE_ASSIGN_FALSE);
|
||||
append(p_target);
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_cast(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) {
|
||||
switch (p_type.kind) {
|
||||
case GDScriptDataType::BUILTIN: {
|
||||
append(GDScriptFunction::OPCODE_CAST_TO_BUILTIN);
|
||||
append(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);
|
||||
append(class_idx);
|
||||
} break;
|
||||
case GDScriptDataType::SCRIPT:
|
||||
case GDScriptDataType::GDSCRIPT: {
|
||||
Variant script = p_type.script_type;
|
||||
int idx = get_constant_pos(script);
|
||||
|
||||
append(GDScriptFunction::OPCODE_CAST_TO_SCRIPT);
|
||||
append(idx);
|
||||
} break;
|
||||
default: {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
append(p_source);
|
||||
append(p_target);
|
||||
}
|
||||
|
||||
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);
|
||||
append(p_arguments.size());
|
||||
append(p_base);
|
||||
append(p_function_name);
|
||||
for (int i = 0; i < p_arguments.size(); i++) {
|
||||
append(p_arguments[i]);
|
||||
}
|
||||
append(p_target);
|
||||
alloc_call(p_arguments.size());
|
||||
}
|
||||
|
||||
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);
|
||||
append(p_function_name);
|
||||
append(p_arguments.size());
|
||||
for (int i = 0; i < p_arguments.size(); i++) {
|
||||
append(p_arguments[i]);
|
||||
}
|
||||
append(p_target);
|
||||
alloc_call(p_arguments.size());
|
||||
}
|
||||
|
||||
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);
|
||||
append(p_arguments.size());
|
||||
append(p_base);
|
||||
append(p_function_name);
|
||||
for (int i = 0; i < p_arguments.size(); i++) {
|
||||
append(p_arguments[i]);
|
||||
}
|
||||
append(p_target);
|
||||
alloc_call(p_arguments.size());
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_call_builtin(const Address &p_target, GDScriptFunctions::Function p_function, const Vector<Address> &p_arguments) {
|
||||
append(GDScriptFunction::OPCODE_CALL_BUILT_IN);
|
||||
append(p_function);
|
||||
append(p_arguments.size());
|
||||
for (int i = 0; i < p_arguments.size(); i++) {
|
||||
append(p_arguments[i]);
|
||||
}
|
||||
append(p_target);
|
||||
alloc_call(p_arguments.size());
|
||||
}
|
||||
|
||||
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);
|
||||
append(p_arguments.size());
|
||||
append(p_base);
|
||||
append(p_method->get_name());
|
||||
for (int i = 0; i < p_arguments.size(); i++) {
|
||||
append(p_arguments[i]);
|
||||
}
|
||||
append(p_target);
|
||||
alloc_call(p_arguments.size());
|
||||
}
|
||||
|
||||
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);
|
||||
append(p_arguments.size());
|
||||
append(p_base);
|
||||
append(p_method->get_name());
|
||||
for (int i = 0; i < p_arguments.size(); i++) {
|
||||
append(p_arguments[i]);
|
||||
}
|
||||
append(p_target);
|
||||
alloc_call(p_arguments.size());
|
||||
}
|
||||
|
||||
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);
|
||||
append(p_arguments.size());
|
||||
append(GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS);
|
||||
append(p_function_name);
|
||||
for (int i = 0; i < p_arguments.size(); i++) {
|
||||
append(p_arguments[i]);
|
||||
}
|
||||
append(p_target);
|
||||
alloc_call(p_arguments.size());
|
||||
}
|
||||
|
||||
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);
|
||||
append(p_arguments.size());
|
||||
append(p_base);
|
||||
append(p_function_name);
|
||||
for (int i = 0; i < p_arguments.size(); i++) {
|
||||
append(p_arguments[i]);
|
||||
}
|
||||
append(p_target);
|
||||
alloc_call(p_arguments.size());
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) {
|
||||
append(GDScriptFunction::OPCODE_CONSTRUCT);
|
||||
append(p_type);
|
||||
append(p_arguments.size());
|
||||
for (int i = 0; i < p_arguments.size(); i++) {
|
||||
append(p_arguments[i]);
|
||||
}
|
||||
append(p_target);
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) {
|
||||
append(GDScriptFunction::OPCODE_CONSTRUCT_ARRAY);
|
||||
append(p_arguments.size());
|
||||
for (int i = 0; i < p_arguments.size(); i++) {
|
||||
append(p_arguments[i]);
|
||||
}
|
||||
append(p_target);
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) {
|
||||
append(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY);
|
||||
append(p_arguments.size() / 2); // This is number of key-value pairs, so only half of actual arguments.
|
||||
for (int i = 0; i < p_arguments.size(); i++) {
|
||||
append(p_arguments[i]);
|
||||
}
|
||||
append(p_target);
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_await(const Address &p_target, const Address &p_operand) {
|
||||
append(GDScriptFunction::OPCODE_AWAIT);
|
||||
append(p_operand);
|
||||
append(GDScriptFunction::OPCODE_AWAIT_RESUME);
|
||||
append(p_target);
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_if(const Address &p_condition) {
|
||||
append(GDScriptFunction::OPCODE_JUMP_IF_NOT);
|
||||
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); // 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 = increase_stack() | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
|
||||
int container_pos = increase_stack() | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
|
||||
|
||||
current_breaks_to_patch.push_back(List<int>());
|
||||
|
||||
// Assign container.
|
||||
append(GDScriptFunction::OPCODE_ASSIGN);
|
||||
append(container_pos);
|
||||
append(p_list);
|
||||
|
||||
// Begin loop.
|
||||
append(GDScriptFunction::OPCODE_ITERATE_BEGIN);
|
||||
append(counter_pos);
|
||||
append(container_pos);
|
||||
for_jmp_addrs.push_back(opcodes.size());
|
||||
append(0); // End of loop address, will be patched.
|
||||
append(p_variable);
|
||||
append(GDScriptFunction::OPCODE_JUMP);
|
||||
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);
|
||||
append(counter_pos);
|
||||
append(container_pos);
|
||||
for_jmp_addrs.push_back(opcodes.size());
|
||||
append(0); // Jump destination, will be patched.
|
||||
append(p_variable);
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_endfor() {
|
||||
// Jump back to loop check.
|
||||
append(GDScriptFunction::OPCODE_JUMP);
|
||||
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();
|
||||
|
||||
current_stack_size -= 2; // Remove loop temporaries.
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
current_breaks_to_patch.back()->get().push_back(opcodes.size());
|
||||
append(0);
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_continue() {
|
||||
append(GDScriptFunction::OPCODE_JUMP);
|
||||
append(continue_addrs.back()->get());
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_continue_match() {
|
||||
append(GDScriptFunction::OPCODE_JUMP);
|
||||
match_continues_to_patch.back()->get().push_back(opcodes.size());
|
||||
append(0);
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_breakpoint() {
|
||||
append(GDScriptFunction::OPCODE_BREAKPOINT);
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_newline(int p_line) {
|
||||
append(GDScriptFunction::OPCODE_LINE);
|
||||
append(p_line);
|
||||
current_line = p_line;
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_return(const Address &p_return_value) {
|
||||
append(GDScriptFunction::OPCODE_RETURN);
|
||||
append(p_return_value);
|
||||
}
|
||||
|
||||
void GDScriptByteCodeGenerator::write_assert(const Address &p_test, const Address &p_message) {
|
||||
append(GDScriptFunction::OPCODE_ASSERT);
|
||||
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);
|
||||
}
|
||||
}
|
277
modules/gdscript/gdscript_byte_codegen.h
Normal file
277
modules/gdscript/gdscript_byte_codegen.h
Normal file
|
@ -0,0 +1,277 @@
|
|||
/*************************************************************************/
|
||||
/* gdscript_byte_codegen.h */
|
||||
/*************************************************************************/
|
||||
/* 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. */
|
||||
/*************************************************************************/
|
||||
|
||||
#ifndef GDSCRIPT_BYTE_CODEGEN
|
||||
#define GDSCRIPT_BYTE_CODEGEN
|
||||
|
||||
#include "gdscript_codegen.h"
|
||||
|
||||
class GDScriptByteCodeGenerator : public GDScriptCodeGenerator {
|
||||
bool ended = false;
|
||||
GDScriptFunction *function = nullptr;
|
||||
bool debug_stack = false;
|
||||
|
||||
Vector<int> opcodes;
|
||||
List<Map<StringName, int>> stack_id_stack;
|
||||
Map<StringName, int> stack_identifiers;
|
||||
Map<StringName, int> local_constants;
|
||||
|
||||
List<GDScriptFunction::StackDebug> stack_debug;
|
||||
List<Map<StringName, int>> block_identifier_stack;
|
||||
Map<StringName, int> block_identifiers;
|
||||
|
||||
int current_stack_size = 0;
|
||||
int current_temporaries = 0;
|
||||
|
||||
HashMap<Variant, int, VariantHasher, VariantComparator> constant_map;
|
||||
Map<StringName, int> name_map;
|
||||
#ifdef TOOLS_ENABLED
|
||||
Vector<StringName> named_globals;
|
||||
#endif
|
||||
int current_line = 0;
|
||||
int stack_max = 0;
|
||||
int call_max = 0;
|
||||
|
||||
List<int> if_jmp_addrs; // List since this can be nested.
|
||||
List<int> for_jmp_addrs;
|
||||
List<int> while_jmp_addrs;
|
||||
List<int> continue_addrs;
|
||||
|
||||
// Used to patch jumps with `and` and `or` operators with short-circuit.
|
||||
List<int> logic_op_jump_pos1;
|
||||
List<int> logic_op_jump_pos2;
|
||||
|
||||
List<Address> ternary_result;
|
||||
List<int> ternary_jump_fail_pos;
|
||||
List<int> ternary_jump_skip_pos;
|
||||
|
||||
List<List<int>> current_breaks_to_patch;
|
||||
List<List<int>> match_continues_to_patch;
|
||||
|
||||
void add_stack_identifier(const StringName &p_id, int p_stackpos) {
|
||||
stack_identifiers[p_id] = p_stackpos;
|
||||
if (debug_stack) {
|
||||
block_identifiers[p_id] = p_stackpos;
|
||||
GDScriptFunction::StackDebug sd;
|
||||
sd.added = true;
|
||||
sd.line = current_line;
|
||||
sd.identifier = p_id;
|
||||
sd.pos = p_stackpos;
|
||||
stack_debug.push_back(sd);
|
||||
}
|
||||
}
|
||||
|
||||
void push_stack_identifiers() {
|
||||
stack_id_stack.push_back(stack_identifiers);
|
||||
if (debug_stack) {
|
||||
block_identifier_stack.push_back(block_identifiers);
|
||||
block_identifiers.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void pop_stack_identifiers() {
|
||||
stack_identifiers = stack_id_stack.back()->get();
|
||||
current_stack_size = stack_identifiers.size() + current_temporaries;
|
||||
stack_id_stack.pop_back();
|
||||
|
||||
if (debug_stack) {
|
||||
for (Map<StringName, int>::Element *E = block_identifiers.front(); E; E = E->next()) {
|
||||
GDScriptFunction::StackDebug sd;
|
||||
sd.added = false;
|
||||
sd.identifier = E->key();
|
||||
sd.line = current_line;
|
||||
sd.pos = E->get();
|
||||
stack_debug.push_back(sd);
|
||||
}
|
||||
block_identifiers = block_identifier_stack.back()->get();
|
||||
block_identifier_stack.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
int get_name_map_pos(const StringName &p_identifier) {
|
||||
int ret;
|
||||
if (!name_map.has(p_identifier)) {
|
||||
ret = name_map.size();
|
||||
name_map[p_identifier] = ret;
|
||||
} else {
|
||||
ret = name_map[p_identifier];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int get_constant_pos(const Variant &p_constant) {
|
||||
if (constant_map.has(p_constant))
|
||||
return constant_map[p_constant];
|
||||
int pos = constant_map.size();
|
||||
constant_map[p_constant] = pos;
|
||||
return pos;
|
||||
}
|
||||
|
||||
void alloc_stack(int p_level) {
|
||||
if (p_level >= stack_max)
|
||||
stack_max = p_level + 1;
|
||||
}
|
||||
|
||||
void alloc_call(int p_params) {
|
||||
if (p_params >= call_max)
|
||||
call_max = p_params;
|
||||
}
|
||||
|
||||
int increase_stack() {
|
||||
int top = current_stack_size++;
|
||||
alloc_stack(current_stack_size);
|
||||
return top;
|
||||
}
|
||||
|
||||
int address_of(const Address &p_address) {
|
||||
switch (p_address.mode) {
|
||||
case Address::SELF:
|
||||
return GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS;
|
||||
case Address::CLASS:
|
||||
return GDScriptFunction::ADDR_TYPE_CLASS << GDScriptFunction::ADDR_BITS;
|
||||
case Address::MEMBER:
|
||||
return p_address.address | (GDScriptFunction::ADDR_TYPE_MEMBER << GDScriptFunction::ADDR_BITS);
|
||||
case Address::CLASS_CONSTANT:
|
||||
return p_address.address | (GDScriptFunction::ADDR_TYPE_CLASS_CONSTANT << GDScriptFunction::ADDR_BITS);
|
||||
case Address::LOCAL_CONSTANT:
|
||||
case Address::CONSTANT:
|
||||
return p_address.address | (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
|
||||
case Address::LOCAL_VARIABLE:
|
||||
case Address::TEMPORARY:
|
||||
case Address::FUNCTION_PARAMETER:
|
||||
return p_address.address | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
|
||||
case Address::GLOBAL:
|
||||
return p_address.address | (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS);
|
||||
case Address::NAMED_GLOBAL:
|
||||
return p_address.address | (GDScriptFunction::ADDR_TYPE_NAMED_GLOBAL << GDScriptFunction::ADDR_BITS);
|
||||
case Address::NIL:
|
||||
return GDScriptFunction::ADDR_TYPE_NIL << GDScriptFunction::ADDR_BITS;
|
||||
}
|
||||
return -1; // Unreachable.
|
||||
}
|
||||
|
||||
void append(int code) {
|
||||
opcodes.push_back(code);
|
||||
}
|
||||
|
||||
void append(const Address &p_address) {
|
||||
opcodes.push_back(address_of(p_address));
|
||||
}
|
||||
|
||||
void append(const StringName &p_name) {
|
||||
opcodes.push_back(get_name_map_pos(p_name));
|
||||
}
|
||||
|
||||
void patch_jump(int p_address) {
|
||||
opcodes.write[p_address] = opcodes.size();
|
||||
}
|
||||
|
||||
public:
|
||||
virtual uint32_t add_parameter(const StringName &p_name, bool p_is_optional, const GDScriptDataType &p_type) override;
|
||||
virtual uint32_t add_local(const StringName &p_name, const GDScriptDataType &p_type) override;
|
||||
virtual uint32_t add_local_constant(const StringName &p_name, const Variant &p_constant) override;
|
||||
virtual uint32_t add_or_get_constant(const Variant &p_constant) override;
|
||||
virtual uint32_t add_or_get_name(const StringName &p_name) override;
|
||||
virtual uint32_t add_temporary() override;
|
||||
virtual void pop_temporary() override;
|
||||
|
||||
virtual void start_parameters() override;
|
||||
virtual void end_parameters() override;
|
||||
|
||||
virtual void start_block() override;
|
||||
virtual void end_block() override;
|
||||
|
||||
virtual void write_start(GDScript *p_script, const StringName &p_function_name, bool p_static, MultiplayerAPI::RPCMode p_rpc_mode, const GDScriptDataType &p_return_type) override;
|
||||
virtual GDScriptFunction *write_end() override;
|
||||
|
||||
#ifdef DEBUG_ENABLED
|
||||
virtual void set_signature(const String &p_signature) override;
|
||||
#endif
|
||||
virtual void set_initial_line(int p_line) override;
|
||||
|
||||
virtual void write_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) override;
|
||||
virtual void write_type_test(const Address &p_target, const Address &p_source, const Address &p_type) override;
|
||||
virtual void write_type_test_builtin(const Address &p_target, const Address &p_source, Variant::Type p_type) override;
|
||||
virtual void write_and_left_operand(const Address &p_left_operand) override;
|
||||
virtual void write_and_right_operand(const Address &p_right_operand) override;
|
||||
virtual void write_end_and(const Address &p_target) override;
|
||||
virtual void write_or_left_operand(const Address &p_left_operand) override;
|
||||
virtual void write_or_right_operand(const Address &p_right_operand) override;
|
||||
virtual void write_end_or(const Address &p_target) override;
|
||||
virtual void write_start_ternary(const Address &p_target) override;
|
||||
virtual void write_ternary_condition(const Address &p_condition) override;
|
||||
virtual void write_ternary_true_expr(const Address &p_expr) override;
|
||||
virtual void write_ternary_false_expr(const Address &p_expr) override;
|
||||
virtual void write_end_ternary() override;
|
||||
virtual void write_set(const Address &p_target, const Address &p_index, const Address &p_source) override;
|
||||
virtual void write_get(const Address &p_target, const Address &p_index, const Address &p_source) override;
|
||||
virtual void write_set_named(const Address &p_target, const StringName &p_name, const Address &p_source) override;
|
||||
virtual void write_get_named(const Address &p_target, const StringName &p_name, const Address &p_source) override;
|
||||
virtual void write_set_member(const Address &p_value, const StringName &p_name) override;
|
||||
virtual void write_get_member(const Address &p_target, const StringName &p_name) override;
|
||||
virtual void write_assign(const Address &p_target, const Address &p_source) override;
|
||||
virtual void write_assign_true(const Address &p_target) override;
|
||||
virtual void write_assign_false(const Address &p_target) override;
|
||||
virtual void write_cast(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) override;
|
||||
virtual void write_call(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) override;
|
||||
virtual void write_super_call(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) override;
|
||||
virtual void write_call_async(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) override;
|
||||
virtual void write_call_builtin(const Address &p_target, GDScriptFunctions::Function p_function, const Vector<Address> &p_arguments) override;
|
||||
virtual void write_call_method_bind(const Address &p_target, const Address &p_base, const MethodBind *p_method, const Vector<Address> &p_arguments) override;
|
||||
virtual void write_call_ptrcall(const Address &p_target, const Address &p_base, const MethodBind *p_method, const Vector<Address> &p_arguments) override;
|
||||
virtual void write_call_self(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) override;
|
||||
virtual void write_call_script_function(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) override;
|
||||
virtual void write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) override;
|
||||
virtual void write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) override;
|
||||
virtual void write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) override;
|
||||
virtual void write_await(const Address &p_target, const Address &p_operand) override;
|
||||
virtual void write_if(const Address &p_condition) override;
|
||||
virtual void write_else() override;
|
||||
virtual void write_endif() override;
|
||||
virtual void write_for(const Address &p_variable, const Address &p_list) override;
|
||||
virtual void write_endfor() override;
|
||||
virtual void start_while_condition() override;
|
||||
virtual void write_while(const Address &p_condition) override;
|
||||
virtual void write_endwhile() override;
|
||||
virtual void start_match() override;
|
||||
virtual void start_match_branch() override;
|
||||
virtual void end_match() override;
|
||||
virtual void write_break() override;
|
||||
virtual void write_continue() override;
|
||||
virtual void write_continue_match() override;
|
||||
virtual void write_breakpoint() override;
|
||||
virtual void write_newline(int p_line) override;
|
||||
virtual void write_return(const Address &p_return_value) override;
|
||||
virtual void write_assert(const Address &p_test, const Address &p_message) override;
|
||||
|
||||
virtual ~GDScriptByteCodeGenerator();
|
||||
};
|
||||
|
||||
#endif // GDSCRIPT_BYTE_CODEGEN
|
160
modules/gdscript/gdscript_codegen.h
Normal file
160
modules/gdscript/gdscript_codegen.h
Normal file
|
@ -0,0 +1,160 @@
|
|||
/*************************************************************************/
|
||||
/* gdscript_codegen.h */
|
||||
/*************************************************************************/
|
||||
/* 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. */
|
||||
/*************************************************************************/
|
||||
|
||||
#ifndef GDSCRIPT_CODEGEN
|
||||
#define GDSCRIPT_CODEGEN
|
||||
|
||||
#include "core/io/multiplayer_api.h"
|
||||
#include "core/string_name.h"
|
||||
#include "core/variant.h"
|
||||
#include "gdscript_function.h"
|
||||
#include "gdscript_functions.h"
|
||||
|
||||
class GDScriptCodeGenerator {
|
||||
public:
|
||||
struct Address {
|
||||
enum AddressMode {
|
||||
SELF,
|
||||
CLASS,
|
||||
MEMBER,
|
||||
CONSTANT,
|
||||
CLASS_CONSTANT,
|
||||
LOCAL_CONSTANT,
|
||||
LOCAL_VARIABLE,
|
||||
FUNCTION_PARAMETER,
|
||||
TEMPORARY,
|
||||
GLOBAL,
|
||||
NAMED_GLOBAL,
|
||||
NIL,
|
||||
};
|
||||
AddressMode mode = NIL;
|
||||
uint32_t address = 0;
|
||||
GDScriptDataType type;
|
||||
|
||||
Address() {}
|
||||
Address(AddressMode p_mode, const GDScriptDataType &p_type = GDScriptDataType()) {
|
||||
mode = p_mode;
|
||||
type = p_type;
|
||||
}
|
||||
Address(AddressMode p_mode, uint32_t p_address, const GDScriptDataType &p_type = GDScriptDataType()) {
|
||||
mode = p_mode,
|
||||
address = p_address;
|
||||
type = p_type;
|
||||
}
|
||||
};
|
||||
|
||||
virtual uint32_t add_parameter(const StringName &p_name, bool p_is_optional, const GDScriptDataType &p_type) = 0;
|
||||
virtual uint32_t add_local(const StringName &p_name, const GDScriptDataType &p_type) = 0;
|
||||
virtual uint32_t add_local_constant(const StringName &p_name, const Variant &p_constant) = 0;
|
||||
virtual uint32_t add_or_get_constant(const Variant &p_constant) = 0;
|
||||
virtual uint32_t add_or_get_name(const StringName &p_name) = 0;
|
||||
virtual uint32_t add_temporary() = 0;
|
||||
virtual void pop_temporary() = 0;
|
||||
|
||||
virtual void start_parameters() = 0;
|
||||
virtual void end_parameters() = 0;
|
||||
|
||||
virtual void start_block() = 0;
|
||||
virtual void end_block() = 0;
|
||||
|
||||
// virtual int get_max_stack_level() = 0;
|
||||
// virtual int get_max_function_arguments() = 0;
|
||||
|
||||
virtual void write_start(GDScript *p_script, const StringName &p_function_name, bool p_static, MultiplayerAPI::RPCMode p_rpc_mode, const GDScriptDataType &p_return_type) = 0;
|
||||
virtual GDScriptFunction *write_end() = 0;
|
||||
|
||||
#ifdef DEBUG_ENABLED
|
||||
virtual void set_signature(const String &p_signature) = 0;
|
||||
#endif
|
||||
virtual void set_initial_line(int p_line) = 0;
|
||||
|
||||
// virtual void alloc_stack(int p_level) = 0; // Is this needed?
|
||||
// virtual void alloc_call(int p_arg_count) = 0; // This might be automatic from other functions.
|
||||
|
||||
virtual void write_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) = 0;
|
||||
virtual void write_type_test(const Address &p_target, const Address &p_source, const Address &p_type) = 0;
|
||||
virtual void write_type_test_builtin(const Address &p_target, const Address &p_source, Variant::Type p_type) = 0;
|
||||
virtual void write_and_left_operand(const Address &p_left_operand) = 0;
|
||||
virtual void write_and_right_operand(const Address &p_right_operand) = 0;
|
||||
virtual void write_end_and(const Address &p_target) = 0;
|
||||
virtual void write_or_left_operand(const Address &p_left_operand) = 0;
|
||||
virtual void write_or_right_operand(const Address &p_right_operand) = 0;
|
||||
virtual void write_end_or(const Address &p_target) = 0;
|
||||
virtual void write_start_ternary(const Address &p_target) = 0;
|
||||
virtual void write_ternary_condition(const Address &p_condition) = 0;
|
||||
virtual void write_ternary_true_expr(const Address &p_expr) = 0;
|
||||
virtual void write_ternary_false_expr(const Address &p_expr) = 0;
|
||||
virtual void write_end_ternary() = 0;
|
||||
virtual void write_set(const Address &p_target, const Address &p_index, const Address &p_source) = 0;
|
||||
virtual void write_get(const Address &p_target, const Address &p_index, const Address &p_source) = 0;
|
||||
virtual void write_set_named(const Address &p_target, const StringName &p_name, const Address &p_source) = 0;
|
||||
virtual void write_get_named(const Address &p_target, const StringName &p_name, const Address &p_source) = 0;
|
||||
virtual void write_set_member(const Address &p_value, const StringName &p_name) = 0;
|
||||
virtual void write_get_member(const Address &p_target, const StringName &p_name) = 0;
|
||||
virtual void write_assign(const Address &p_target, const Address &p_source) = 0;
|
||||
virtual void write_assign_true(const Address &p_target) = 0;
|
||||
virtual void write_assign_false(const Address &p_target) = 0;
|
||||
virtual void write_cast(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) = 0;
|
||||
virtual void write_call(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) = 0;
|
||||
virtual void write_super_call(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) = 0;
|
||||
virtual void write_call_async(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) = 0;
|
||||
virtual void write_call_builtin(const Address &p_target, GDScriptFunctions::Function p_function, const Vector<Address> &p_arguments) = 0;
|
||||
virtual void write_call_method_bind(const Address &p_target, const Address &p_base, const MethodBind *p_method, const Vector<Address> &p_arguments) = 0;
|
||||
virtual void write_call_ptrcall(const Address &p_target, const Address &p_base, const MethodBind *p_method, const Vector<Address> &p_arguments) = 0;
|
||||
virtual void write_call_self(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) = 0;
|
||||
virtual void write_call_script_function(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) = 0;
|
||||
virtual void write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) = 0;
|
||||
virtual void write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) = 0;
|
||||
virtual void write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) = 0;
|
||||
virtual void write_await(const Address &p_target, const Address &p_operand) = 0;
|
||||
virtual void write_if(const Address &p_condition) = 0;
|
||||
// virtual void write_elseif(const Address &p_condition) = 0; This kind of makes things more difficult for no real benefit.
|
||||
virtual void write_else() = 0;
|
||||
virtual void write_endif() = 0;
|
||||
virtual void write_for(const Address &p_variable, const Address &p_list) = 0;
|
||||
virtual void write_endfor() = 0;
|
||||
virtual void start_while_condition() = 0; // Used to allow a jump to the expression evaluation.
|
||||
virtual void write_while(const Address &p_condition) = 0;
|
||||
virtual void write_endwhile() = 0;
|
||||
virtual void start_match() = 0;
|
||||
virtual void start_match_branch() = 0;
|
||||
virtual void end_match() = 0;
|
||||
virtual void write_break() = 0;
|
||||
virtual void write_continue() = 0;
|
||||
virtual void write_continue_match() = 0;
|
||||
virtual void write_breakpoint() = 0;
|
||||
virtual void write_newline(int p_line) = 0;
|
||||
virtual void write_return(const Address &p_return_value) = 0;
|
||||
virtual void write_assert(const Address &p_test, const Address &p_message) = 0;
|
||||
|
||||
virtual ~GDScriptCodeGenerator() {}
|
||||
};
|
||||
|
||||
#endif // GDSCRIPT_CODEGEN
|
File diff suppressed because it is too large
Load diff
|
@ -33,109 +33,88 @@
|
|||
|
||||
#include "core/set.h"
|
||||
#include "gdscript.h"
|
||||
#include "gdscript_codegen.h"
|
||||
#include "gdscript_function.h"
|
||||
#include "gdscript_parser.h"
|
||||
|
||||
class GDScriptCompiler {
|
||||
const GDScriptParser *parser;
|
||||
const GDScriptParser *parser = nullptr;
|
||||
Set<GDScript *> parsed_classes;
|
||||
Set<GDScript *> parsing_classes;
|
||||
GDScript *main_script;
|
||||
GDScript *main_script = nullptr;
|
||||
|
||||
struct CodeGen {
|
||||
GDScript *script;
|
||||
const GDScriptParser::ClassNode *class_node;
|
||||
const GDScriptParser::FunctionNode *function_node;
|
||||
GDScript *script = nullptr;
|
||||
const GDScriptParser::ClassNode *class_node = nullptr;
|
||||
const GDScriptParser::FunctionNode *function_node = nullptr;
|
||||
StringName function_name;
|
||||
bool debug_stack;
|
||||
GDScriptCodeGenerator *generator = nullptr;
|
||||
Map<StringName, GDScriptCodeGenerator::Address> parameters;
|
||||
Map<StringName, GDScriptCodeGenerator::Address> locals;
|
||||
List<Set<StringName>> locals_in_scope;
|
||||
|
||||
List<Map<StringName, int>> stack_id_stack;
|
||||
Map<StringName, int> stack_identifiers;
|
||||
|
||||
List<GDScriptFunction::StackDebug> stack_debug;
|
||||
List<Map<StringName, int>> block_identifier_stack;
|
||||
Map<StringName, int> block_identifiers;
|
||||
Map<StringName, int> local_named_constants;
|
||||
|
||||
void add_stack_identifier(const StringName &p_id, int p_stackpos) {
|
||||
stack_identifiers[p_id] = p_stackpos;
|
||||
if (debug_stack) {
|
||||
block_identifiers[p_id] = p_stackpos;
|
||||
GDScriptFunction::StackDebug sd;
|
||||
sd.added = true;
|
||||
sd.line = current_line;
|
||||
sd.identifier = p_id;
|
||||
sd.pos = p_stackpos;
|
||||
stack_debug.push_back(sd);
|
||||
}
|
||||
GDScriptCodeGenerator::Address add_local(const StringName &p_name, const GDScriptDataType &p_type) {
|
||||
uint32_t addr = generator->add_local(p_name, p_type);
|
||||
locals[p_name] = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::LOCAL_VARIABLE, addr, p_type);
|
||||
locals_in_scope.back()->get().insert(p_name);
|
||||
return locals[p_name];
|
||||
}
|
||||
|
||||
void push_stack_identifiers() {
|
||||
stack_id_stack.push_back(stack_identifiers);
|
||||
if (debug_stack) {
|
||||
block_identifier_stack.push_back(block_identifiers);
|
||||
block_identifiers.clear();
|
||||
}
|
||||
GDScriptCodeGenerator::Address add_local_constant(const StringName &p_name, const Variant &p_value) {
|
||||
uint32_t addr = generator->add_local_constant(p_name, p_value);
|
||||
locals[p_name] = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::LOCAL_CONSTANT, addr);
|
||||
return locals[p_name];
|
||||
}
|
||||
|
||||
void pop_stack_identifiers() {
|
||||
stack_identifiers = stack_id_stack.back()->get();
|
||||
stack_id_stack.pop_back();
|
||||
GDScriptCodeGenerator::Address add_temporary(const GDScriptDataType &p_type = GDScriptDataType()) {
|
||||
uint32_t addr = generator->add_temporary();
|
||||
return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::TEMPORARY, addr, p_type);
|
||||
}
|
||||
|
||||
if (debug_stack) {
|
||||
for (Map<StringName, int>::Element *E = block_identifiers.front(); E; E = E->next()) {
|
||||
GDScriptFunction::StackDebug sd;
|
||||
sd.added = false;
|
||||
sd.identifier = E->key();
|
||||
sd.line = current_line;
|
||||
sd.pos = E->get();
|
||||
stack_debug.push_back(sd);
|
||||
GDScriptCodeGenerator::Address add_constant(const Variant &p_constant) {
|
||||
GDScriptDataType type;
|
||||
type.has_type = true;
|
||||
type.kind = GDScriptDataType::BUILTIN;
|
||||
type.builtin_type = p_constant.get_type();
|
||||
if (type.builtin_type == Variant::OBJECT) {
|
||||
Object *obj = p_constant;
|
||||
if (obj) {
|
||||
type.kind = GDScriptDataType::NATIVE;
|
||||
type.native_type = obj->get_class_name();
|
||||
|
||||
Ref<Script> script = obj->get_script();
|
||||
if (script.is_valid()) {
|
||||
type.script_type = script;
|
||||
Ref<GDScript> gdscript = script;
|
||||
if (gdscript.is_valid()) {
|
||||
type.kind = GDScriptDataType::GDSCRIPT;
|
||||
} else {
|
||||
type.kind = GDScriptDataType::SCRIPT;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
type.builtin_type = Variant::NIL;
|
||||
}
|
||||
block_identifiers = block_identifier_stack.back()->get();
|
||||
block_identifier_stack.pop_back();
|
||||
}
|
||||
|
||||
uint32_t addr = generator->add_or_get_constant(p_constant);
|
||||
return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::CONSTANT, addr, type);
|
||||
}
|
||||
|
||||
HashMap<Variant, int, VariantHasher, VariantComparator> constant_map;
|
||||
Map<StringName, int> name_map;
|
||||
#ifdef TOOLS_ENABLED
|
||||
Vector<StringName> named_globals;
|
||||
#endif
|
||||
|
||||
int get_name_map_pos(const StringName &p_identifier) {
|
||||
int ret;
|
||||
if (!name_map.has(p_identifier)) {
|
||||
ret = name_map.size();
|
||||
name_map[p_identifier] = ret;
|
||||
} else {
|
||||
ret = name_map[p_identifier];
|
||||
}
|
||||
return ret;
|
||||
void start_block() {
|
||||
Set<StringName> scope;
|
||||
locals_in_scope.push_back(scope);
|
||||
generator->start_block();
|
||||
}
|
||||
|
||||
int get_constant_pos(const Variant &p_constant) {
|
||||
if (constant_map.has(p_constant)) {
|
||||
return constant_map[p_constant] | (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
|
||||
void end_block() {
|
||||
Set<StringName> &scope = locals_in_scope.back()->get();
|
||||
for (Set<StringName>::Element *E = scope.front(); E; E = E->next()) {
|
||||
locals.erase(E->get());
|
||||
}
|
||||
int pos = constant_map.size();
|
||||
constant_map[p_constant] = pos;
|
||||
return pos | (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
|
||||
locals_in_scope.pop_back();
|
||||
generator->end_block();
|
||||
}
|
||||
|
||||
Vector<int> opcodes;
|
||||
void alloc_stack(int p_level) {
|
||||
if (p_level >= stack_max) {
|
||||
stack_max = p_level + 1;
|
||||
}
|
||||
}
|
||||
void alloc_call(int p_params) {
|
||||
if (p_params >= call_max) {
|
||||
call_max = p_params;
|
||||
}
|
||||
}
|
||||
|
||||
int current_line;
|
||||
int stack_max;
|
||||
int call_max;
|
||||
};
|
||||
|
||||
bool _is_class_member_property(CodeGen &codegen, const StringName &p_name);
|
||||
|
@ -143,17 +122,16 @@ class GDScriptCompiler {
|
|||
|
||||
void _set_error(const String &p_error, const GDScriptParser::Node *p_node);
|
||||
|
||||
bool _create_unary_operator(CodeGen &codegen, const GDScriptParser::UnaryOpNode *on, Variant::Operator op, int p_stack_level);
|
||||
bool _create_binary_operator(CodeGen &codegen, const GDScriptParser::BinaryOpNode *on, Variant::Operator op, int p_stack_level, bool p_initializer = false, int p_index_addr = 0);
|
||||
bool _create_binary_operator(CodeGen &codegen, const GDScriptParser::ExpressionNode *p_left_operand, const GDScriptParser::ExpressionNode *p_right_operand, Variant::Operator op, int p_stack_level, bool p_initializer = false, int p_index_addr = 0);
|
||||
bool _generate_typed_assign(CodeGen &codegen, int p_src_address, int p_dst_address, const GDScriptDataType &p_datatype, const GDScriptParser::DataType &p_value_type);
|
||||
Error _create_binary_operator(CodeGen &codegen, const GDScriptParser::BinaryOpNode *on, Variant::Operator op, bool p_initializer = false, const GDScriptCodeGenerator::Address &p_index_addr = GDScriptCodeGenerator::Address());
|
||||
Error _create_binary_operator(CodeGen &codegen, const GDScriptParser::ExpressionNode *p_left_operand, const GDScriptParser::ExpressionNode *p_right_operand, Variant::Operator op, bool p_initializer = false, const GDScriptCodeGenerator::Address &p_index_addr = GDScriptCodeGenerator::Address());
|
||||
|
||||
GDScriptDataType _gdtype_from_datatype(const GDScriptParser::DataType &p_datatype) const;
|
||||
|
||||
int _parse_assign_right_expression(CodeGen &codegen, const GDScriptParser::AssignmentNode *p_assignment, int p_stack_level, int p_index_addr = 0);
|
||||
int _parse_expression(CodeGen &codegen, const GDScriptParser::ExpressionNode *p_expression, int p_stack_level, bool p_root = false, bool p_initializer = false, int p_index_addr = 0);
|
||||
Error _parse_match_pattern(CodeGen &codegen, const GDScriptParser::PatternNode *p_pattern, int p_stack_level, int p_value_addr, int p_type_addr, int &r_bound_variables, Vector<int> &r_patch_addresses, Vector<int> &r_block_patch_address);
|
||||
Error _parse_block(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block, int p_stack_level = 0, int p_break_addr = -1, int p_continue_addr = -1);
|
||||
GDScriptCodeGenerator::Address _parse_assign_right_expression(CodeGen &codegen, Error &r_error, const GDScriptParser::AssignmentNode *p_assignmentint, const GDScriptCodeGenerator::Address &p_index_addr = GDScriptCodeGenerator::Address());
|
||||
GDScriptCodeGenerator::Address _parse_expression(CodeGen &codegen, Error &r_error, const GDScriptParser::ExpressionNode *p_expression, bool p_root = false, bool p_initializer = false, const GDScriptCodeGenerator::Address &p_index_addr = GDScriptCodeGenerator::Address());
|
||||
GDScriptCodeGenerator::Address _parse_match_pattern(CodeGen &codegen, Error &r_error, const GDScriptParser::PatternNode *p_pattern, const GDScriptCodeGenerator::Address &p_value_addr, const GDScriptCodeGenerator::Address &p_type_addr, const GDScriptCodeGenerator::Address &p_previous_test, bool p_is_first, bool p_is_nested);
|
||||
void _add_locals_in_block(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block);
|
||||
Error _parse_block(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block, bool p_add_locals = true);
|
||||
Error _parse_function(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::FunctionNode *p_func, bool p_for_ready = false);
|
||||
Error _parse_setter_getter(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::VariableNode *p_variable, bool p_is_setter);
|
||||
Error _parse_class_level(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
|
||||
|
|
|
@ -34,6 +34,10 @@
|
|||
#include "gdscript.h"
|
||||
#include "gdscript_functions.h"
|
||||
|
||||
#ifdef DEBUG_ENABLED
|
||||
#include "core/string_builder.h"
|
||||
#endif
|
||||
|
||||
Variant *GDScriptFunction::_get_variant(int p_address, GDScriptInstance *p_instance, GDScript *p_script, Variant &self, Variant &static_ref, Variant *p_stack, String &r_error) const {
|
||||
int address = p_address & ADDR_MASK;
|
||||
|
||||
|
@ -105,9 +109,9 @@ Variant *GDScriptFunction::_get_variant(int p_address, GDScriptInstance *p_insta
|
|||
#ifdef TOOLS_ENABLED
|
||||
case ADDR_TYPE_NAMED_GLOBAL: {
|
||||
#ifdef DEBUG_ENABLED
|
||||
ERR_FAIL_INDEX_V(address, _named_globals_count, nullptr);
|
||||
ERR_FAIL_INDEX_V(address, _global_names_count, nullptr);
|
||||
#endif
|
||||
StringName id = _named_globals_ptr[address];
|
||||
StringName id = _global_names_ptr[address];
|
||||
|
||||
if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(id)) {
|
||||
return (Variant *)&GDScriptLanguage::get_singleton()->get_named_globals_map()[id];
|
||||
|
@ -212,7 +216,6 @@ String GDScriptFunction::_get_call_error(const Callable::CallError &p_err, const
|
|||
&&OPCODE_CALL_RETURN, \
|
||||
&&OPCODE_CALL_ASYNC, \
|
||||
&&OPCODE_CALL_BUILT_IN, \
|
||||
&&OPCODE_CALL_SELF, \
|
||||
&&OPCODE_CALL_SELF_BASE, \
|
||||
&&OPCODE_AWAIT, \
|
||||
&&OPCODE_AWAIT_RESUME, \
|
||||
|
@ -1139,10 +1142,6 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
|
|||
}
|
||||
DISPATCH_OPCODE;
|
||||
|
||||
OPCODE(OPCODE_CALL_SELF) {
|
||||
OPCODE_BREAK;
|
||||
}
|
||||
|
||||
OPCODE(OPCODE_CALL_SELF_BASE) {
|
||||
CHECK_SPACE(2);
|
||||
int self_fun = _code_ptr[ip + 1];
|
||||
|
@ -1214,8 +1213,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
|
|||
DISPATCH_OPCODE;
|
||||
|
||||
OPCODE(OPCODE_AWAIT) {
|
||||
int ipofs = 2;
|
||||
CHECK_SPACE(3);
|
||||
CHECK_SPACE(2);
|
||||
|
||||
//do the oneshot connect
|
||||
GET_VARIANT_PTR(argobj, 1);
|
||||
|
@ -1265,7 +1263,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
|
|||
gdfs->state.stack_size = _stack_size;
|
||||
gdfs->state.self = self;
|
||||
gdfs->state.alloca_size = alloca_size;
|
||||
gdfs->state.ip = ip + ipofs;
|
||||
gdfs->state.ip = ip + 2;
|
||||
gdfs->state.line = line;
|
||||
gdfs->state.script = _script;
|
||||
{
|
||||
|
@ -1884,3 +1882,506 @@ GDScriptFunctionState::~GDScriptFunctionState() {
|
|||
instances_list.remove_from_list();
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef DEBUG_ENABLED
|
||||
static String _get_variant_string(const Variant &p_variant) {
|
||||
String txt;
|
||||
if (p_variant.get_type() == Variant::STRING) {
|
||||
txt = "\"" + String(p_variant) + "\"";
|
||||
} else if (p_variant.get_type() == Variant::STRING_NAME) {
|
||||
txt = "&\"" + String(p_variant) + "\"";
|
||||
} else if (p_variant.get_type() == Variant::NODE_PATH) {
|
||||
txt = "^\"" + String(p_variant) + "\"";
|
||||
} else if (p_variant.get_type() == Variant::OBJECT) {
|
||||
Object *obj = p_variant;
|
||||
if (!obj) {
|
||||
txt = "null";
|
||||
} else {
|
||||
GDScriptNativeClass *cls = Object::cast_to<GDScriptNativeClass>(obj);
|
||||
if (cls) {
|
||||
txt += cls->get_name();
|
||||
txt += " (class)";
|
||||
} else {
|
||||
txt = obj->get_class();
|
||||
if (obj->get_script_instance()) {
|
||||
txt += "(" + obj->get_script_instance()->get_script()->get_path() + ")";
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
txt = p_variant;
|
||||
}
|
||||
return txt;
|
||||
}
|
||||
|
||||
static String _disassemble_address(const GDScript *p_script, const GDScriptFunction &p_function, int p_address) {
|
||||
int addr = p_address & GDScriptFunction::ADDR_MASK;
|
||||
|
||||
switch (p_address >> GDScriptFunction::ADDR_BITS) {
|
||||
case GDScriptFunction::ADDR_TYPE_SELF: {
|
||||
return "self";
|
||||
} break;
|
||||
case GDScriptFunction::ADDR_TYPE_CLASS: {
|
||||
return "class";
|
||||
} break;
|
||||
case GDScriptFunction::ADDR_TYPE_MEMBER: {
|
||||
return "member(" + p_script->debug_get_member_by_index(addr) + ")";
|
||||
} break;
|
||||
case GDScriptFunction::ADDR_TYPE_CLASS_CONSTANT: {
|
||||
return "class_const(" + p_function.get_global_name(addr) + ")";
|
||||
} break;
|
||||
case GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT: {
|
||||
return "const(" + _get_variant_string(p_function.get_constant(addr)) + ")";
|
||||
} break;
|
||||
case GDScriptFunction::ADDR_TYPE_STACK: {
|
||||
return "stack(" + itos(addr) + ")";
|
||||
} break;
|
||||
case GDScriptFunction::ADDR_TYPE_STACK_VARIABLE: {
|
||||
return "var_stack(" + itos(addr) + ")";
|
||||
} break;
|
||||
case GDScriptFunction::ADDR_TYPE_GLOBAL: {
|
||||
return "global(" + _get_variant_string(GDScriptLanguage::get_singleton()->get_global_array()[addr]) + ")";
|
||||
} break;
|
||||
case GDScriptFunction::ADDR_TYPE_NAMED_GLOBAL: {
|
||||
return "named_global(" + p_function.get_global_name(addr) + ")";
|
||||
} break;
|
||||
case GDScriptFunction::ADDR_TYPE_NIL: {
|
||||
return "nil";
|
||||
} break;
|
||||
}
|
||||
|
||||
return "<err>";
|
||||
}
|
||||
|
||||
void GDScriptFunction::disassemble(const Vector<String> &p_code_lines) const {
|
||||
#define DADDR(m_ip) (_disassemble_address(_script, *this, _code_ptr[ip + m_ip]))
|
||||
|
||||
for (int ip = 0; ip < _code_size;) {
|
||||
StringBuilder text;
|
||||
int incr = 0;
|
||||
|
||||
text += " ";
|
||||
text += itos(ip);
|
||||
text += ": ";
|
||||
|
||||
// This makes the compiler complain if some opcode is unchecked in the switch.
|
||||
Opcode code = Opcode(_code_ptr[ip]);
|
||||
|
||||
switch (code) {
|
||||
case OPCODE_OPERATOR: {
|
||||
int operation = _code_ptr[ip + 1];
|
||||
|
||||
text += "operator ";
|
||||
|
||||
text += DADDR(4);
|
||||
text += " = ";
|
||||
text += DADDR(2);
|
||||
text += " ";
|
||||
text += Variant::get_operator_name(Variant::Operator(operation));
|
||||
text += " ";
|
||||
text += DADDR(3);
|
||||
|
||||
incr += 5;
|
||||
} break;
|
||||
case OPCODE_EXTENDS_TEST: {
|
||||
text += "is object ";
|
||||
text += DADDR(3);
|
||||
text += " = ";
|
||||
text += DADDR(1);
|
||||
text += " is ";
|
||||
text += DADDR(2);
|
||||
|
||||
incr += 4;
|
||||
} break;
|
||||
case OPCODE_IS_BUILTIN: {
|
||||
text += "is builtin ";
|
||||
text += DADDR(3);
|
||||
text += " = ";
|
||||
text += DADDR(1);
|
||||
text += " is ";
|
||||
text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 2]));
|
||||
|
||||
incr += 4;
|
||||
} break;
|
||||
case OPCODE_SET: {
|
||||
text += "set ";
|
||||
text += DADDR(1);
|
||||
text += "[";
|
||||
text += DADDR(2);
|
||||
text += "] = ";
|
||||
text += DADDR(3);
|
||||
|
||||
incr += 4;
|
||||
} break;
|
||||
case OPCODE_GET: {
|
||||
text += "get ";
|
||||
text += DADDR(3);
|
||||
text += " = ";
|
||||
text += DADDR(1);
|
||||
text += "[";
|
||||
text += DADDR(2);
|
||||
text += "]";
|
||||
|
||||
incr += 4;
|
||||
} break;
|
||||
case OPCODE_SET_NAMED: {
|
||||
text += "set_named ";
|
||||
text += DADDR(1);
|
||||
text += "[\"";
|
||||
text += _global_names_ptr[_code_ptr[ip + 2]];
|
||||
text += "\"] = ";
|
||||
text += DADDR(3);
|
||||
|
||||
incr += 4;
|
||||
} break;
|
||||
case OPCODE_GET_NAMED: {
|
||||
text += "get_named ";
|
||||
text += DADDR(3);
|
||||
text += " = ";
|
||||
text += DADDR(1);
|
||||
text += "[\"";
|
||||
text += _global_names_ptr[_code_ptr[ip + 2]];
|
||||
text += "\"]";
|
||||
|
||||
incr += 4;
|
||||
} break;
|
||||
case OPCODE_SET_MEMBER: {
|
||||
text += "set_member ";
|
||||
text += "[\"";
|
||||
text += _global_names_ptr[_code_ptr[ip + 1]];
|
||||
text += "\"] = ";
|
||||
text += DADDR(2);
|
||||
|
||||
incr += 3;
|
||||
} break;
|
||||
case OPCODE_GET_MEMBER: {
|
||||
text += "get_member ";
|
||||
text += DADDR(2);
|
||||
text += " = ";
|
||||
text += "[\"";
|
||||
text += _global_names_ptr[_code_ptr[ip + 1]];
|
||||
text += "\"]";
|
||||
|
||||
incr += 3;
|
||||
} break;
|
||||
case OPCODE_ASSIGN: {
|
||||
text += "assign ";
|
||||
text += DADDR(1);
|
||||
text += " = ";
|
||||
text += DADDR(2);
|
||||
|
||||
incr += 3;
|
||||
} break;
|
||||
case OPCODE_ASSIGN_TRUE: {
|
||||
text += "assign ";
|
||||
text += DADDR(1);
|
||||
text += " = true";
|
||||
|
||||
incr += 2;
|
||||
} break;
|
||||
case OPCODE_ASSIGN_FALSE: {
|
||||
text += "assign ";
|
||||
text += DADDR(1);
|
||||
text += " = false";
|
||||
|
||||
incr += 2;
|
||||
} break;
|
||||
case OPCODE_ASSIGN_TYPED_BUILTIN: {
|
||||
text += "assign typed builtin (";
|
||||
text += Variant::get_type_name((Variant::Type)_code_ptr[ip + 1]);
|
||||
text += ") ";
|
||||
text += DADDR(2);
|
||||
text += " = ";
|
||||
text += DADDR(3);
|
||||
|
||||
incr += 4;
|
||||
} break;
|
||||
case OPCODE_ASSIGN_TYPED_NATIVE: {
|
||||
Variant class_name = _constants_ptr[_code_ptr[ip + 1]];
|
||||
GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(class_name.operator Object *());
|
||||
|
||||
text += "assign typed native (";
|
||||
text += nc->get_name().operator String();
|
||||
text += ") ";
|
||||
text += DADDR(2);
|
||||
text += " = ";
|
||||
text += DADDR(3);
|
||||
|
||||
incr += 4;
|
||||
} break;
|
||||
case OPCODE_ASSIGN_TYPED_SCRIPT: {
|
||||
Variant script = _constants_ptr[_code_ptr[ip + 1]];
|
||||
Script *sc = Object::cast_to<Script>(script.operator Object *());
|
||||
|
||||
text += "assign typed script (";
|
||||
text += sc->get_path();
|
||||
text += ") ";
|
||||
text += DADDR(2);
|
||||
text += " = ";
|
||||
text += DADDR(3);
|
||||
|
||||
incr += 4;
|
||||
} break;
|
||||
case OPCODE_CAST_TO_BUILTIN: {
|
||||
text += "cast builtin ";
|
||||
text += DADDR(3);
|
||||
text += " = ";
|
||||
text += DADDR(2);
|
||||
text += " as ";
|
||||
text += Variant::get_type_name(Variant::Type(_code_ptr[ip + 1]));
|
||||
|
||||
incr += 4;
|
||||
} break;
|
||||
case OPCODE_CAST_TO_NATIVE: {
|
||||
Variant class_name = _constants_ptr[_code_ptr[ip + 1]];
|
||||
GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(class_name.operator Object *());
|
||||
|
||||
text += "cast native ";
|
||||
text += DADDR(3);
|
||||
text += " = ";
|
||||
text += DADDR(2);
|
||||
text += " as ";
|
||||
text += nc->get_name();
|
||||
|
||||
incr += 4;
|
||||
} break;
|
||||
case OPCODE_CAST_TO_SCRIPT: {
|
||||
text += "cast ";
|
||||
text += DADDR(3);
|
||||
text += " = ";
|
||||
text += DADDR(2);
|
||||
text += " as ";
|
||||
text += DADDR(1);
|
||||
|
||||
incr += 4;
|
||||
} break;
|
||||
case OPCODE_CONSTRUCT: {
|
||||
Variant::Type t = Variant::Type(_code_ptr[ip + 1]);
|
||||
int argc = _code_ptr[ip + 2];
|
||||
|
||||
text += "construct ";
|
||||
text += DADDR(3 + argc);
|
||||
text += " = ";
|
||||
|
||||
text += Variant::get_type_name(t) + "(";
|
||||
for (int i = 0; i < argc; i++) {
|
||||
if (i > 0)
|
||||
text += ", ";
|
||||
text += DADDR(i + 3);
|
||||
}
|
||||
text += ")";
|
||||
|
||||
incr = 4 + argc;
|
||||
} break;
|
||||
case OPCODE_CONSTRUCT_ARRAY: {
|
||||
int argc = _code_ptr[ip + 1];
|
||||
text += " make_array ";
|
||||
text += DADDR(2 + argc);
|
||||
text += " = [";
|
||||
|
||||
for (int i = 0; i < argc; i++) {
|
||||
if (i > 0)
|
||||
text += ", ";
|
||||
text += DADDR(2 + i);
|
||||
}
|
||||
|
||||
text += "]";
|
||||
|
||||
incr += 3 + argc;
|
||||
} break;
|
||||
case OPCODE_CONSTRUCT_DICTIONARY: {
|
||||
int argc = _code_ptr[ip + 1];
|
||||
text += "make_dict ";
|
||||
text += DADDR(2 + argc * 2);
|
||||
text += " = {";
|
||||
|
||||
for (int i = 0; i < argc; i++) {
|
||||
if (i > 0)
|
||||
text += ", ";
|
||||
text += DADDR(2 + i * 2 + 0);
|
||||
text += ": ";
|
||||
text += DADDR(2 + i * 2 + 1);
|
||||
}
|
||||
|
||||
text += "}";
|
||||
|
||||
incr += 3 + argc * 2;
|
||||
} break;
|
||||
case OPCODE_CALL:
|
||||
case OPCODE_CALL_RETURN:
|
||||
case OPCODE_CALL_ASYNC: {
|
||||
bool ret = _code_ptr[ip] == OPCODE_CALL_RETURN;
|
||||
bool async = _code_ptr[ip] == OPCODE_CALL_ASYNC;
|
||||
|
||||
if (ret) {
|
||||
text += "call-ret ";
|
||||
} else if (async) {
|
||||
text += "call-async ";
|
||||
} else {
|
||||
text += "call ";
|
||||
}
|
||||
|
||||
int argc = _code_ptr[ip + 1];
|
||||
if (ret || async) {
|
||||
text += DADDR(4 + argc) + " = ";
|
||||
}
|
||||
|
||||
text += DADDR(2) + ".";
|
||||
text += String(_global_names_ptr[_code_ptr[ip + 3]]);
|
||||
text += "(";
|
||||
|
||||
for (int i = 0; i < argc; i++) {
|
||||
if (i > 0)
|
||||
text += ", ";
|
||||
text += DADDR(4 + i);
|
||||
}
|
||||
text += ")";
|
||||
|
||||
incr = 5 + argc;
|
||||
} break;
|
||||
case OPCODE_CALL_BUILT_IN: {
|
||||
text += "call-built-in ";
|
||||
|
||||
int argc = _code_ptr[ip + 2];
|
||||
text += DADDR(3 + argc) + " = ";
|
||||
|
||||
text += GDScriptFunctions::get_func_name(GDScriptFunctions::Function(_code_ptr[ip + 1]));
|
||||
text += "(";
|
||||
|
||||
for (int i = 0; i < argc; i++) {
|
||||
if (i > 0)
|
||||
text += ", ";
|
||||
text += DADDR(3 + i);
|
||||
}
|
||||
text += ")";
|
||||
|
||||
incr = 4 + argc;
|
||||
} break;
|
||||
case OPCODE_CALL_SELF_BASE: {
|
||||
text += "call-self-base ";
|
||||
|
||||
int argc = _code_ptr[ip + 2];
|
||||
text += DADDR(3 + argc) + " = ";
|
||||
|
||||
text += _global_names_ptr[_code_ptr[ip + 1]];
|
||||
text += "(";
|
||||
|
||||
for (int i = 0; i < argc; i++) {
|
||||
if (i > 0)
|
||||
text += ", ";
|
||||
text += DADDR(3 + i);
|
||||
}
|
||||
text += ")";
|
||||
|
||||
incr = 4 + argc;
|
||||
} break;
|
||||
case OPCODE_AWAIT: {
|
||||
text += "await ";
|
||||
text += DADDR(1);
|
||||
|
||||
incr += 2;
|
||||
} break;
|
||||
case OPCODE_AWAIT_RESUME: {
|
||||
text += "await resume ";
|
||||
text += DADDR(1);
|
||||
|
||||
incr = 2;
|
||||
} break;
|
||||
case OPCODE_JUMP: {
|
||||
text += "jump ";
|
||||
text += itos(_code_ptr[ip + 1]);
|
||||
|
||||
incr = 2;
|
||||
} break;
|
||||
case OPCODE_JUMP_IF: {
|
||||
text += "jump-if ";
|
||||
text += DADDR(1);
|
||||
text += " to ";
|
||||
text += itos(_code_ptr[ip + 2]);
|
||||
|
||||
incr = 3;
|
||||
} break;
|
||||
case OPCODE_JUMP_IF_NOT: {
|
||||
text += "jump-if-not ";
|
||||
text += DADDR(1);
|
||||
text += " to ";
|
||||
text += itos(_code_ptr[ip + 2]);
|
||||
|
||||
incr = 3;
|
||||
} break;
|
||||
case OPCODE_JUMP_TO_DEF_ARGUMENT: {
|
||||
text += "jump-to-default-argument ";
|
||||
|
||||
incr = 1;
|
||||
} break;
|
||||
case OPCODE_RETURN: {
|
||||
text += "return ";
|
||||
text += DADDR(1);
|
||||
|
||||
incr = 2;
|
||||
} break;
|
||||
case OPCODE_ITERATE_BEGIN: {
|
||||
text += "for-init ";
|
||||
text += DADDR(4);
|
||||
text += " in ";
|
||||
text += DADDR(2);
|
||||
text += " counter ";
|
||||
text += DADDR(1);
|
||||
text += " end ";
|
||||
text += itos(_code_ptr[ip + 3]);
|
||||
|
||||
incr += 5;
|
||||
} break;
|
||||
case OPCODE_ITERATE: {
|
||||
text += "for-loop ";
|
||||
text += DADDR(4);
|
||||
text += " in ";
|
||||
text += DADDR(2);
|
||||
text += " counter ";
|
||||
text += DADDR(1);
|
||||
text += " end ";
|
||||
text += itos(_code_ptr[ip + 3]);
|
||||
|
||||
incr += 5;
|
||||
} break;
|
||||
case OPCODE_LINE: {
|
||||
int line = _code_ptr[ip + 1] - 1;
|
||||
if (line >= 0 && line < p_code_lines.size()) {
|
||||
text += "line ";
|
||||
text += itos(line + 1);
|
||||
text += ": ";
|
||||
text += p_code_lines[line];
|
||||
} else {
|
||||
text += "";
|
||||
}
|
||||
|
||||
incr += 2;
|
||||
} break;
|
||||
case OPCODE_ASSERT: {
|
||||
text += "assert (";
|
||||
text += DADDR(1);
|
||||
text += ", ";
|
||||
text += DADDR(2);
|
||||
text += ")";
|
||||
|
||||
incr += 3;
|
||||
} break;
|
||||
case OPCODE_BREAKPOINT: {
|
||||
text += "breakpoint";
|
||||
|
||||
incr += 1;
|
||||
} break;
|
||||
case OPCODE_END: {
|
||||
text += "== END ==";
|
||||
|
||||
incr += 1;
|
||||
} break;
|
||||
}
|
||||
|
||||
ip += incr;
|
||||
if (text.get_string_length() > 0) {
|
||||
print_line(text.as_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -182,7 +182,6 @@ public:
|
|||
OPCODE_CALL_RETURN,
|
||||
OPCODE_CALL_ASYNC,
|
||||
OPCODE_CALL_BUILT_IN,
|
||||
OPCODE_CALL_SELF,
|
||||
OPCODE_CALL_SELF_BASE,
|
||||
OPCODE_AWAIT,
|
||||
OPCODE_AWAIT_RESUME,
|
||||
|
@ -224,6 +223,7 @@ public:
|
|||
|
||||
private:
|
||||
friend class GDScriptCompiler;
|
||||
friend class GDScriptByteCodeGenerator;
|
||||
|
||||
StringName source;
|
||||
|
||||
|
@ -232,10 +232,6 @@ private:
|
|||
int _constant_count;
|
||||
const StringName *_global_names_ptr;
|
||||
int _global_names_count;
|
||||
#ifdef TOOLS_ENABLED
|
||||
const StringName *_named_globals_ptr;
|
||||
int _named_globals_count;
|
||||
#endif
|
||||
const int *_default_arg_ptr;
|
||||
int _default_arg_count;
|
||||
const int *_code_ptr;
|
||||
|
@ -252,9 +248,6 @@ private:
|
|||
StringName name;
|
||||
Vector<Variant> constants;
|
||||
Vector<StringName> global_names;
|
||||
#ifdef TOOLS_ENABLED
|
||||
Vector<StringName> named_globals;
|
||||
#endif
|
||||
Vector<int> default_arguments;
|
||||
Vector<int> code;
|
||||
Vector<GDScriptDataType> argument_types;
|
||||
|
@ -344,6 +337,10 @@ public:
|
|||
|
||||
Variant call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Callable::CallError &r_err, CallState *p_state = nullptr);
|
||||
|
||||
#ifdef DEBUG_ENABLED
|
||||
void disassemble(const Vector<String> &p_code_lines) const;
|
||||
#endif
|
||||
|
||||
_FORCE_INLINE_ MultiplayerAPI::RPCMode get_rpc_mode() const { return rpc_mode; }
|
||||
GDScriptFunction();
|
||||
~GDScriptFunction();
|
||||
|
|
|
@ -1476,7 +1476,9 @@ GDScriptParser::ContinueNode *GDScriptParser::parse_continue() {
|
|||
}
|
||||
current_suite->has_continue = true;
|
||||
end_statement(R"("continue")");
|
||||
return alloc_node<ContinueNode>();
|
||||
ContinueNode *cont = alloc_node<ContinueNode>();
|
||||
cont->is_for_match = is_continue_match;
|
||||
return cont;
|
||||
}
|
||||
|
||||
GDScriptParser::ForNode *GDScriptParser::parse_for() {
|
||||
|
@ -1495,10 +1497,12 @@ GDScriptParser::ForNode *GDScriptParser::parse_for() {
|
|||
// Save break/continue state.
|
||||
bool could_break = can_break;
|
||||
bool could_continue = can_continue;
|
||||
bool was_continue_match = is_continue_match;
|
||||
|
||||
// Allow break/continue.
|
||||
can_break = true;
|
||||
can_continue = true;
|
||||
is_continue_match = false;
|
||||
|
||||
SuiteNode *suite = alloc_node<SuiteNode>();
|
||||
if (n_for->variable) {
|
||||
|
@ -1511,6 +1515,7 @@ GDScriptParser::ForNode *GDScriptParser::parse_for() {
|
|||
// Reset break/continue state.
|
||||
can_break = could_break;
|
||||
can_continue = could_continue;
|
||||
is_continue_match = was_continue_match;
|
||||
|
||||
return n_for;
|
||||
}
|
||||
|
@ -1645,8 +1650,10 @@ GDScriptParser::MatchBranchNode *GDScriptParser::parse_match_branch() {
|
|||
|
||||
// Save continue state.
|
||||
bool could_continue = can_continue;
|
||||
bool was_continue_match = is_continue_match;
|
||||
// Allow continue for match.
|
||||
can_continue = true;
|
||||
is_continue_match = true;
|
||||
|
||||
SuiteNode *suite = alloc_node<SuiteNode>();
|
||||
if (branch->patterns.size() > 0) {
|
||||
|
@ -1663,6 +1670,7 @@ GDScriptParser::MatchBranchNode *GDScriptParser::parse_match_branch() {
|
|||
|
||||
// Restore continue state.
|
||||
can_continue = could_continue;
|
||||
is_continue_match = was_continue_match;
|
||||
|
||||
return branch;
|
||||
}
|
||||
|
@ -1820,16 +1828,19 @@ GDScriptParser::WhileNode *GDScriptParser::parse_while() {
|
|||
// Save break/continue state.
|
||||
bool could_break = can_break;
|
||||
bool could_continue = can_continue;
|
||||
bool was_continue_match = is_continue_match;
|
||||
|
||||
// Allow break/continue.
|
||||
can_break = true;
|
||||
can_continue = true;
|
||||
is_continue_match = false;
|
||||
|
||||
n_while->loop = parse_suite(R"("while" block)");
|
||||
|
||||
// Reset break/continue state.
|
||||
can_break = could_break;
|
||||
can_continue = could_continue;
|
||||
is_continue_match = was_continue_match;
|
||||
|
||||
return n_while;
|
||||
}
|
||||
|
|
|
@ -609,6 +609,7 @@ public:
|
|||
};
|
||||
|
||||
struct ContinueNode : public Node {
|
||||
bool is_for_match = false;
|
||||
ContinueNode() {
|
||||
type = CONTINUE;
|
||||
}
|
||||
|
@ -1079,6 +1080,7 @@ private:
|
|||
bool panic_mode = false;
|
||||
bool can_break = false;
|
||||
bool can_continue = false;
|
||||
bool is_continue_match = false; // Whether a `continue` will act on a `match`.
|
||||
bool is_ignoring_warnings = false;
|
||||
List<bool> multiline_stack;
|
||||
|
||||
|
|
|
@ -1039,7 +1039,7 @@ void GDScriptTokenizer::check_indent() {
|
|||
// First time indenting, choose character now.
|
||||
indent_char = current_indent_char;
|
||||
} else if (current_indent_char != indent_char) {
|
||||
Token error = make_error(vformat("Used \"%c\" for indentation instead \"%c\" as used before in the file.", String(¤t_indent_char, 1).c_escape(), String(&indent_char, 1).c_escape()));
|
||||
Token error = make_error(vformat("Used \"%s\" for indentation instead \"%s\" as used before in the file.", String(¤t_indent_char, 1).c_escape(), String(&indent_char, 1).c_escape()));
|
||||
error.start_line = line;
|
||||
error.start_column = 1;
|
||||
error.leftmost_column = 1;
|
||||
|
|
|
@ -36,8 +36,11 @@
|
|||
#include "core/string_builder.h"
|
||||
|
||||
#include "modules/modules_enabled.gen.h"
|
||||
|
||||
#ifdef MODULE_GDSCRIPT_ENABLED
|
||||
|
||||
#include "modules/gdscript/gdscript_analyzer.h"
|
||||
#include "modules/gdscript/gdscript_compiler.h"
|
||||
#include "modules/gdscript/gdscript_parser.h"
|
||||
#include "modules/gdscript/gdscript_tokenizer.h"
|
||||
|
||||
|
@ -122,6 +125,64 @@ static void test_parser(const String &p_code, const String &p_script_path, const
|
|||
printer.print_tree(parser);
|
||||
}
|
||||
|
||||
static void test_compiler(const String &p_code, const String &p_script_path, const Vector<String> &p_lines) {
|
||||
GDScriptParser parser;
|
||||
Error err = parser.parse(p_code, p_script_path, false);
|
||||
|
||||
if (err != OK) {
|
||||
print_line("Error in parser:");
|
||||
const List<GDScriptParser::ParserError> &errors = parser.get_errors();
|
||||
for (const List<GDScriptParser::ParserError>::Element *E = errors.front(); E != nullptr; E = E->next()) {
|
||||
const GDScriptParser::ParserError &error = E->get();
|
||||
print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
GDScriptAnalyzer analyzer(&parser);
|
||||
err = analyzer.analyze();
|
||||
|
||||
if (err != OK) {
|
||||
print_line("Error in analyzer:");
|
||||
const List<GDScriptParser::ParserError> &errors = parser.get_errors();
|
||||
for (const List<GDScriptParser::ParserError>::Element *E = errors.front(); E != nullptr; E = E->next()) {
|
||||
const GDScriptParser::ParserError &error = E->get();
|
||||
print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
GDScriptCompiler compiler;
|
||||
Ref<GDScript> script;
|
||||
script.instance();
|
||||
script->set_path(p_script_path);
|
||||
|
||||
err = compiler.compile(&parser, script.ptr(), false);
|
||||
|
||||
if (err) {
|
||||
print_line("Error in compiler:");
|
||||
print_line(vformat("%02d:%02d: %s", compiler.get_error_line(), compiler.get_error_column(), compiler.get_error()));
|
||||
return;
|
||||
}
|
||||
|
||||
for (const Map<StringName, GDScriptFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) {
|
||||
const GDScriptFunction *func = E->value();
|
||||
|
||||
String signature = "Disassembling " + func->get_name().operator String() + "(";
|
||||
for (int i = 0; i < func->get_argument_count(); i++) {
|
||||
if (i > 0) {
|
||||
signature += ", ";
|
||||
}
|
||||
signature += func->get_argument_name(i);
|
||||
}
|
||||
print_line(signature + ")");
|
||||
|
||||
func->disassemble(p_lines);
|
||||
print_line("");
|
||||
print_line("");
|
||||
}
|
||||
}
|
||||
|
||||
MainLoop *test(TestType p_type) {
|
||||
List<String> cmdlargs = OS::get_singleton()->get_cmdline_args();
|
||||
|
||||
|
@ -164,6 +225,8 @@ MainLoop *test(TestType p_type) {
|
|||
test_parser(code, test, lines);
|
||||
break;
|
||||
case TEST_COMPILER:
|
||||
test_compiler(code, test, lines);
|
||||
break;
|
||||
case TEST_BYTECODE:
|
||||
print_line("Not implemented.");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue