Skip to content

Commit

Permalink
fix parameter bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
xensik committed Nov 11, 2024
1 parent 048746f commit 8bc7c9b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 39 deletions.
1 change: 0 additions & 1 deletion include/xsk/gsc/compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
37 changes: 4 additions & 33 deletions src/gsc/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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++)
Expand Down
20 changes: 15 additions & 5 deletions src/gsc/decompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<expr_identifier>().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<expr_identifier>().value));
break;
}
case opcode::OP_EvalLocalVariableRefCached0:
Expand Down Expand Up @@ -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<u8>(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<u8>(scp_body->create_count), true });
scp_body->create_count++;
}
}

process_stmt_comp(*func.body, *scp_body);
Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit 8bc7c9b

Please sign in to comment.