Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look at stack struct for local variables #235

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/if_statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -1424,21 +1424,23 @@ def get_function_text(function_info: FunctionInfo, options: Options) -> str:
# Format the body first, because this can result in additional type inferencce
formatted_body = body.format(fmt)

local_vars = function_info.stack_info.local_vars
stack_struct = function_info.stack_info.stack_struct
assert stack_struct is not None
stack_struct_fields = stack_struct.fields

# GCC's stack is ordered low-to-high (e.g. `int sp10; int sp14;`)
# IDO's and MWCC's stack is ordered high-to-low (e.g. `int sp14; int sp10;`)
if options.target.compiler != Target.CompilerEnum.GCC:
local_vars = local_vars[::-1]
for local_var in local_vars:
type_decl = local_var.toplevel_decl(fmt)
if type_decl is not None:
comment = None
if local_var.value in function_info.stack_info.weak_stack_var_locations:
comment = "compiler-managed"
function_lines.append(
SimpleStatement(f"{type_decl};", comment=comment).format(fmt)
)
any_decl = True
stack_struct_fields = stack_struct_fields[::-1]
for field in stack_struct_fields:
type_decl = field.type.to_decl(field.name, fmt)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think LocalVar.toplevel_decl(...) can be deleted now?

comment = None
if field.offset in function_info.stack_info.weak_stack_var_locations:
comment = "compiler-managed"
function_lines.append(
SimpleStatement(f"{type_decl};", comment=comment).format(fmt)
)
any_decl = True

# With reused temps (no longer used), we can get duplicate declarations,
# hence the use of a set here.
Expand Down
18 changes: 3 additions & 15 deletions src/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ class StackInfo:
callee_save_regs: Set[Register] = field(default_factory=set)
callee_save_reg_region: Tuple[int, int] = (0, 0)
unique_type_map: Dict[Tuple[str, object], "Type"] = field(default_factory=dict)
local_vars: List["LocalVar"] = field(default_factory=list)
temp_vars: List["EvalOnceStmt"] = field(default_factory=list)
phi_vars: List["PhiExpr"] = field(default_factory=list)
reg_vars: Dict[Register, "RegisterVar"] = field(default_factory=dict)
Expand All @@ -208,6 +207,7 @@ class StackInfo:
nonzero_accesses: Set["Expression"] = field(default_factory=set)
param_names: Dict[int, str] = field(default_factory=dict)
stack_pointer_type: Optional[Type] = None
stack_struct: Optional[StructDeclaration] = None
replace_first_arg: Optional[Tuple[str, Type]] = None
weak_stack_var_types: Dict[int, Type] = field(default_factory=dict)
weak_stack_var_locations: Set[int] = field(default_factory=set)
Expand Down Expand Up @@ -283,13 +283,6 @@ def add_known_param(self, offset: int, name: Optional[str], type: Type) -> Type:
def get_param_name(self, offset: int) -> Optional[str]:
return self.param_names.get(offset)

def add_local_var(self, var: "LocalVar") -> None:
if any(v.value == var.value for v in self.local_vars):
return
self.local_vars.append(var)
# Make sure the local vars stay sorted in order on the stack.
self.local_vars.sort(key=lambda v: v.value)

def add_argument(self, arg: "PassedInArg") -> None:
if any(a.value == arg.value for a in self.arguments):
return
Expand Down Expand Up @@ -617,6 +610,8 @@ def get_stack_info(
stack_struct.is_stack = True
stack_struct.new_field_prefix = "sp"

info.stack_struct = stack_struct

# This acts as the type of the $sp register
info.stack_pointer_type = Type.ptr(Type.struct(stack_struct))

Expand Down Expand Up @@ -2727,10 +2722,7 @@ def handle_addi_real(
if stack_info.is_stack_reg(output_reg):
# Changing sp. Just ignore that.
return source
# Keep track of all local variables that we take addresses of.
var = stack_info.get_stack_var(imm.value, store=False)
if isinstance(var, LocalVar):
stack_info.add_local_var(var)
return AddressOf(var, type=var.type.reference())
else:
return add_imm(source, imm, stack_info)
Expand Down Expand Up @@ -4124,7 +4116,6 @@ def store_memory(
return

if isinstance(dest, LocalVar):
self.stack_info.add_local_var(dest)
raw_value = source
if isinstance(raw_value, Cast) and raw_value.reinterpret:
# When preserving values on the stack across function calls,
Expand Down Expand Up @@ -4966,9 +4957,6 @@ def make_arg(offset: int, type: Type) -> PassedInArg:

v: Dict[str, object] = {}
fmt = Formatter()
for local in stack_info.local_vars:
var_name = local.format(fmt)
v[var_name] = local
for temp in stack_info.temp_vars:
if temp.need_decl():
var_name = temp.expr.var.format(fmt)
Expand Down