def get(name, value): if name in tab: return tab[name][value] elif name == "N": return Constant(value * 0x08) elif name == "I": return Constant(value)
def analyze(self): ctx = Context() romconfig = Config(self.filename, rom=True) rombank = romconfig.get(["Analysis", "FlowAnalysis-Rombank"]) if str(hex(self.addr.virtual())) in rombank: print "a" ctx.setValue('ROMBANK', Constant(rombank[str(hex(self.addr.virtual()))])) ''' if self.addr.virtual() == 0x0A90: ctx.setValue('ROMBANK', Constant(1)) if self.addr.virtual() == 0x3CCA: ctx.setValue('ROMBANK', Constant(0x15)) if self.addr.virtual() in (0x0B34, 0x0B37, 0x0B3A, 0x0B3D): ctx.setValue('ROMBANK', Constant(1)) if self.addr.virtual() in (0x0D68, 0x0E7F, 0x0EFC, 0x0EDB, 0x0C40, 0x149B, 0x15B3, 0x1732, 0x0C10): ctx.setValue('ROMBANK', Constant(2)) ''' if self.addr.inBankedSpace() and not self.addr.isAmbiguous(): ctx.setValue('ROMBANK', Constant(self.addr.bank())) content = self.process(self.graph.start(), None, False, False, True) content = content.optimizedWithContext(ctx) content = content.optimizeDependencies( set(ALL_REGS) - set(['FZ', 'FN', 'FC', 'FH'])) return content
def make(cls, left, right): if isConstant(right): if isinstance(left, Shr) and isConstant(left.right): right = Constant(right.value + left.right.value) left = left.left if isinstance(left, Shl) and isConstant(left.right): sh_a = left.right.value sh_b = right.value mask = Constant(((0xFF << sh_a) & 0xFF) >> sh_b) sum_shift = sh_b - sh_a if sum_shift < 0: return And.make(Shl.make(left.left, Constant(-sum_shift)), mask) else: return And.make(Shr.make(left.left, Constant(sum_shift)), mask) if isinstance(left, And): a = left.left b = left.right return And.make(Shr.make(a, right), Shr.make(b, right)) return super(Shr, cls).make(left, right)
def make(cls, left, right): if isConstant(left): left, right = right, left if right.value == 0: return left if left == right: return Constant(0) if isConstant(right) and isinstance(left, Sub) and isConstant( left.right): return Sub.make( left.left, Constant(Add.calculate(right.value, left.right.value))) return super(Sub, cls).make(left, right)
def make_context(params, argument, next_addr): ctx = Context() if next_addr.bank() > 0: ctx.setValue('ROMBANK', Constant(next_addr.bank())) ctx.setValue('v8', Constant(argument)) ctx.setValue('FF00_v8', Constant(0xFF00 + argument)) ctx.setValue('v16', Constant(argument)) offset = argument if offset & 0x80: # convert to signed offset offset -= 0x100 ctx.setValue('v8_rel', Constant(next_addr.offset(offset).virtual())) for p in params: value = placeholders.get(p, params[p]) ctx.setValue('#' + p, value) return ctx
def make(cls, left, right): if isConstant(left): left, right = right, left if isinstance(left, Sub) and isConstant( left.right) and isConstant(right): return Equals.make( left.left, Constant(Add.calculate(right.value, left.right.value))) return super(Equals, cls).make(left, right)
def make(cls, left, right): if isConstant(left): left, right = right, left if right.value == 0: return left if isinstance(left, Add16) and isConstant(right) and isConstant( left.right): return cls.make( left.left, Constant(cls.calculate(right.value, left.right.value))) return super(Add16, cls).make(left, right)
def analyze(self): ctx = Context() if self.addr.virtual() == 0x0A90: ctx.setValue('ROMBANK', Constant(1)) if self.addr.virtual() == 0x3CCA: ctx.setValue('ROMBANK', Constant(0x15)) if self.addr.virtual() in (0x0B34, 0x0B37, 0x0B3A, 0x0B3D): ctx.setValue('ROMBANK', Constant(1)) if self.addr.virtual() in (0x0D68, 0x0E7F, 0x0EFC, 0x0EDB, 0x0C40, 0x149B, 0x15B3, 0x1732, 0x0C10): ctx.setValue('ROMBANK', Constant(2)) if self.addr.inBankedSpace() and not self.addr.isAmbiguous(): ctx.setValue('ROMBANK', Constant(self.addr.bank())) content = self.process(self.graph.start(), None, False, False, True) content = content.optimizedWithContext(ctx) content = content.optimizeDependencies( set(ALL_REGS) - set(['FZ', 'FN', 'FC', 'FH'])) return content
def make(cls, hi, lo): # merge word if isinstance(hi, HighByte) and isinstance(lo, LowByte): if hi.childs[0] == lo.childs[0]: return hi.childs[0] # high zero if hi.value == 0: return lo if isinstance(hi, Shr) and isinstance( lo, Shl) and hi.left == lo.left and isConstant( hi.right) and isConstant(lo.right): hi_sh = hi.right.value lo_sh = lo.right.value if hi_sh + lo_sh == 8: return Shl16.make(lo.left, Constant(lo_sh)) return super(Word, cls).make(hi, lo)
def make(cls, left, right): if isConstant(left): left, right = right, left if isConstant(right): if (left.value_mask & right.value) == left.value_mask: return left if (left.value_mask & right.value) == 0: return Constant(0) if right == left: return left #if isinstance(left, And): # return And.make(left.left, And(left.right, right)) #if isinstance(right, And): # return And.make(right.left, And(right.right, left)) if isinstance(left, Or) and isConstant(right): return Or.make(And.make(left.left, right), And.make(left.right, right)) return super(And, cls).make(left, right)
def valueForBranch(self, i): return Constant(self.base_value + i)
def make(cls, left, right): if isConstant(left) and isConstant(right): return Constant(cls.calculate(left.value, right.value)) else: return super(BinOp, cls).make(left, right)
def make(cls, *args): if hasattr(cls, 'calculate') and all(isConstant(x) for x in args): values = [x.value for x in args] return Constant(cls.calculate(*values)) return super(FuncOperator, cls).make(*args)
def make(cls, left, right): if left == right: return Constant(0) return super(Xor, cls).make(left, right)
def constant(arg): if arg.startswith('0x'): value = int(arg, 16) else: value = int(arg) return Constant(value)