def ast(self, astree: AbstractSyntaxTree, iaddr: str, bytestring: str, xdata: InstrXData) -> List[ASTInstruction]: if self.is_call(xdata) and xdata.has_call_target(): calltarget = xdata.call_target(self.ixd) tgtname = calltarget.name tgtxpr = self.target_expr_ast(astree, xdata) (lhs, assigns) = self.lhs_ast(astree, iaddr, xdata) args = self.arguments(xdata) argregs = ["R0", "R1", "R2", "R3"] callargs = argregs[:len(args)] argxprs: List[ASTExpr] = [] for (reg, arg) in zip(callargs, args): if XU.is_struct_field_address(arg, astree): addr = XU.xxpr_to_struct_field_address_expr(arg, astree) argxprs.append(addr) elif arg.is_string_reference: regast = astree.mk_register_variable_expr(reg) cstr = arg.constant.string_reference() saddr = hex(arg.constant.value) argxprs.append(astree.mk_string_constant(regast, cstr, saddr)) elif arg.is_argument_value: argindex = arg.argument_index() funarg = astree.function_argument(argindex) if funarg: astxpr = astree.mk_register_variable_expr( funarg.name, vtype=funarg.typ, parameter=argindex) argxprs.append(astxpr) else: argxprs.append(astree.mk_register_variable_expr(reg)) else: argxprs.append(astree.mk_register_variable_expr(reg)) if len(args) > 4: for a in args[4:]: argxprs.append(XU.xxpr_to_ast_expr(a, astree)) if lhs.is_ignored: call: ASTInstruction = astree.mk_call(lhs, tgtxpr, argxprs) astree.add_instruction_span(call.id, iaddr, bytestring) return [call] else: call = cast(ASTInstruction, astree.mk_call(lhs, tgtxpr, argxprs)) astree.add_instruction_span(call.id, iaddr, bytestring) for assign in assigns: astree.add_instruction_span(assign.id, iaddr, bytestring) return [call] + assigns else: return self.assembly_ast(astree, iaddr, bytestring, xdata)
def ast(self, astree: AbstractSyntaxTree, iaddr: str, bytestring: str, xdata: InstrXData) -> List[ASTInstruction]: tgtx = str(xdata.xprs[0]) if tgtx == "$ra_in": return [] elif self.is_call(xdata) and xdata.has_call_target(): calltarget = xdata.call_target(self.ixd) tgtname = calltarget.name tgtxpr = self.target_expr_ast(astree, xdata) (lhs, assigns) = self.lhs_ast(astree, iaddr, xdata) args = self.arguments(xdata) argxprs: List[ASTExpr] = [] for arg in args: if XU.is_struct_field_address(arg, astree): addr = XU.xxpr_to_struct_field_address_expr(arg, astree) elif arg.is_string_reference: xpr = XU.xxpr_to_ast_expr(arg, astree) cstr = arg.constant.string_reference() saddr = hex(arg.constant.value) argxprs.append(astree.mk_string_constant(xpr, cstr, saddr)) elif arg.is_argument_value: argindex = arg.argument_index() funarg = astree.function_argument(argindex) if funarg: astxpr = astree.mk_register_variable_expr( funarg.name, vtype=funarg.typ, parameter=argindex) argxprs.append(astxpr) else: astxpr = XU.xxpr_to_ast_expr(arg, astree) argxprs.append(astxpr) else: astxpr = XU.xxpr_to_ast_expr(arg, astree) argxprs.append(astxpr) if lhs.is_ignored: call: ASTInstruction = astree.mk_call(lhs, tgtxpr, argxprs) astree.add_instruction_span(call.id, iaddr, bytestring) return [call] else: call = cast(ASTInstruction, astree.mk_call(lhs, tgtxpr, argxprs)) astree.add_instruction_span(call.id, iaddr, bytestring) for assign in assigns: astree.add_instruction_span(assign.id, iaddr, bytestring) return [call] + assigns else: # TODO: accomodate indirect jumps return []
def vinitregister_value_to_ast_lval(vconstvar: "VInitialRegisterValue", astree: AbstractSyntaxTree) -> AST.ASTLval: if vconstvar.is_argument_value: argindex = vconstvar.argument_index() funarg = astree.function_argument(argindex) if funarg: return astree.mk_register_variable_lval(funarg.name, vtype=funarg.typ, parameter=argindex) else: return astree.mk_register_variable_lval(str(vconstvar.register)) elif vconstvar.register.is_stack_pointer: return astree.mk_register_variable_lval("base_sp") else: print("Initial Register Value: " + str(vconstvar.register)) return astree.mk_register_variable_lval(str(vconstvar.register))