-
Notifications
You must be signed in to change notification settings - Fork 49
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
Add the posibility to type saved registers #284
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -476,8 +476,11 @@ def get_stack_var(self, location: int, *, store: bool) -> Expression: | |
def maybe_get_register_var(self, reg: Register) -> Optional[Var]: | ||
return self.reg_vars.get(reg) | ||
|
||
def add_register_var(self, reg: Register, name: str) -> None: | ||
type = Type.floatish() if reg.is_float() else Type.intptr() | ||
def add_register_var(self, reg: Register, name: str, given_type: Type) -> None: | ||
if given_type is not None: | ||
type = given_type | ||
else: | ||
type = Type.floatish() if reg.is_float() else Type.intptr() | ||
var = Var(self, prefix=f"var_{name}", type=type) | ||
self.reg_vars[reg] = var | ||
self.temp_vars.append(var) | ||
|
@@ -4365,6 +4368,34 @@ def setup_planned_vars( | |
stack_info.planned_vars[key] = var | ||
|
||
|
||
def get_declared_reg_types( | ||
stack_info: StackInfo, regs: List[Register] | ||
) -> [Dict[Register, Type]]: | ||
# Use a struct where the type of the member var_x represents the type of the register | ||
reg_struct_name = f"_m2c_reg_{stack_info.function.name}" | ||
reg_struct = stack_info.global_info.typepool.get_struct_by_tag_name( | ||
reg_struct_name, stack_info.global_info.typemap | ||
) | ||
if reg_struct is None: | ||
return {} | ||
types = {} | ||
for reg_field in reg_struct.fields: | ||
corresponding_regs = [ | ||
r | ||
for r in regs | ||
if reg_field.name.endswith(str(r).removeprefix("$")) and not r.is_float() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reg_name = stack_info.function.reg_formatter.format(reg)
name == f"var_{reg_name}" I think. Why the Actually, hm, instead of hard-coding variable name, it might be cleaner if this function just parsed out a struct field name -> type mapping, and then |
||
] | ||
if len(corresponding_regs) > 2: | ||
raise DecompFailure( | ||
f"Function {stack_info.function.name} has a provided register type struct {stack_info.reg_struct_name} " | ||
f"where a register was declared multiple times." | ||
) | ||
elif len(corresponding_regs) == 1: | ||
types[corresponding_regs[0]] = reg_field.type | ||
|
||
return types | ||
|
||
|
||
def setup_reg_vars(stack_info: StackInfo, options: Options) -> None: | ||
"""Set up per-register planned vars based on command line flags.""" | ||
arch = stack_info.global_info.arch | ||
|
@@ -4379,9 +4410,10 @@ def setup_reg_vars(stack_info: StackInfo, options: Options) -> None: | |
reg_vars = [ | ||
stack_info.function.reg_formatter.parse(x, arch) for x in options.reg_vars | ||
] | ||
types = get_declared_reg_types(stack_info, reg_vars) | ||
for reg in reg_vars: | ||
reg_name = stack_info.function.reg_formatter.format(reg) | ||
stack_info.add_register_var(reg, reg_name) | ||
stack_info.add_register_var(reg, reg_name, types.get(reg)) | ||
|
||
|
||
def setup_initial_registers(state: NodeState, fn_sig: FunctionSignature) -> None: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is invalid type syntax -- the [] need to be removed. Can you run mypy?