diff --git a/include/xsk/gsc/compiler.hpp b/include/xsk/gsc/compiler.hpp index 66001b26..3522ca86 100644 --- a/include/xsk/gsc/compiler.hpp +++ b/include/xsk/gsc/compiler.hpp @@ -142,7 +142,6 @@ class compiler auto variable_register(expr_identifier const& exp, scope& scp) -> void; auto variable_initialized(expr_identifier const& exp, scope& scp) -> bool; auto variable_initialize(expr_identifier const& exp, scope& scp) -> u8; - auto variable_reinitialize(expr_identifier const& exp, scope& scp) -> u8; auto variable_create(expr_identifier const& exp, scope& scp) -> u8; auto variable_access(expr_identifier const& exp, scope& scp) -> u8; auto resolve_function_type(expr_function const& exp, std::string& path) -> call::type; diff --git a/src/gsc/compiler.cpp b/src/gsc/compiler.cpp index 398ccf24..c9593dab 100644 --- a/src/gsc/compiler.cpp +++ b/src/gsc/compiler.cpp @@ -1634,19 +1634,19 @@ auto compiler::emit_expr_parameters(expr_parameters const& exp, scope& scp) -> v { for (auto const& entry : exp.list) { - //if (!variable_initialized(*entry, scp)) + if (!variable_initialized(*entry, scp)) { emit_opcode(opcode::OP_SafeCreateVariableFieldCached, std::format("{}", variable_initialize(*entry, scp))); } - /*else + else { - auto index = variable_reinitialize(*entry, scp); + auto index = variable_access(*entry, scp); if (index == 0) emit_opcode(opcode::OP_SafeSetVariableFieldCached0); else emit_opcode(opcode::OP_SafeSetVariableFieldCached, std::format("{}", index)); - }*/ + } } emit_opcode(opcode::OP_checkclearparams); @@ -2780,35 +2780,6 @@ auto compiler::variable_initialize(expr_identifier const& exp, scope& scp) -> u8 throw comp_error(exp.loc(), std::format("local variable '{}' not found", exp.value)); } -auto compiler::variable_reinitialize(expr_identifier const& exp, scope& scp) -> u8 -{ - for (auto i = 0u; i < scp.vars.size(); i++) - { - if (scp.vars[i].name == exp.value) - { - if (scp.vars[i].init) - { - for (auto j = 0u; j < i; j++) - { - if (!scp.vars[j].init) - { - scp.vars[j].init = true; - emit_opcode(opcode::OP_CreateLocalVariable, (ctx_->props() & props::hash) ? scp.vars[j].name : std::format("{}", scp.vars[j].create)); - } - } - - scp.vars[i].init = true; - scp.create_count = i + 1; - return scp.vars[i].create; - } - - throw comp_error(exp.loc(), std::format("local variable '{}' not initialized", exp.value)); - } - } - - throw comp_error(exp.loc(), std::format("local variable '{}' not found", exp.value)); -} - auto compiler::variable_create(expr_identifier const& exp, scope& scp) -> u8 { for (auto i = 0u; i < scp.vars.size(); i++) diff --git a/src/gsc/decompiler.cpp b/src/gsc/decompiler.cpp index 34417e87..5e268ef4 100644 --- a/src/gsc/decompiler.cpp +++ b/src/gsc/decompiler.cpp @@ -1253,12 +1253,19 @@ auto decompiler::decompile_instruction(instruction const& inst) -> void } case opcode::OP_SafeSetVariableFieldCached0: { - func_->params->list.push_back(expr_identifier::make(loc, "var_0")); + if (func_->params->list.size() == 0) + func_->params->list.push_back(expr_identifier::make(loc, "¡ERROR!")); + else + func_->params->list.push_back(expr_identifier::make(loc, func_->params->list.at(func_->params->list.size() - 1)->as().value)); break; } case opcode::OP_SafeSetVariableFieldCached: { - func_->params->list.push_back(expr_identifier::make(loc, "var_" + inst.data[0])); + auto index = func_->params->list.size() - 1 - std::stoul(inst.data[0]); + if (index < 0 || index > func_->params->list.size()) + func_->params->list.push_back(expr_identifier::make(loc, "¡ERROR!")); + else + func_->params->list.push_back(expr_identifier::make(loc, func_->params->list.at(index)->as().value)); break; } case opcode::OP_EvalLocalVariableRefCached0: @@ -2396,8 +2403,11 @@ auto decompiler::process_function(decl_function& func) -> void for (auto const& entry : func.params->list) { - scp_body->vars.push_back({ entry->value, static_cast(scp_body->create_count), true }); - scp_body->create_count++; + if (scp_body->find(0, entry->value) == -1) + { + scp_body->vars.push_back({ entry->value, static_cast(scp_body->create_count), true }); + scp_body->create_count++; + } } process_stmt_comp(*func.body, *scp_body); @@ -3107,7 +3117,7 @@ auto decompiler::process_expr_var_access(expr::ptr& exp, scope& scp) -> void if (scp.vars.size() <= index) { - std::cout << std::format("[WRN]: bad local var access\n"); + std::cout << std::format("[WRN]: bad variable access {} at {} \n", index, func_->name->value); } else {