Beispiel #1
0
def getopd(obj,mode,reg,data,CGR=False):
    r = env.R[reg]
    size = 8 if obj.BW else 16
    if CGR and reg==2:
        r = [r,0,env.cst(0x4,16),env.cst(0x8,16)][mode]
        return r[0:size],data
    if CGR and reg==3:
        r = env.cst([0,1,2,-1][mode],16)
        return r[0:size],data
    if mode==0: # register mode
        return r[0:size],data
    if mode==1: # indexed/symbolic/absolute modes
        addr,data = data[0:16],data[16:]
        imm = env.cst(addr.int(-1),16)
        obj.bytes += pack(addr)
        if r is env.sr:
            imm.sf = False
            return env.mem(imm,size),data
        if r is env.pc:
            return env.mem(env.pc+imm,size),data
        return env.mem(r+imm,size),data
    if mode==2: # indirect register mode
        return env.mem(r,size),data
    if mode==3: # immediate & indirect autoincrement
        if r is env.pc:
            addr,data = data[0:16],data[16:]
            imm = env.cst(addr.int(),16)
            obj.bytes += pack(addr)
            return imm[0:size],data
        else:
            obj.misc['autoinc'] = r
            return env.mem(r,size),data
Beispiel #2
0
class msp430_syntax:

    divide = False
    noprefix = False

    comment = pp.Regex(r"\#.*")
    symbol = pp.Regex(r"[A-Za-z_.$][A-Za-z0-9_.$]*").setParseAction(
        lambda r: env.ext(r[0], size=32))
    mnemo = pp.LineStart() + symbol + pp.Optional(pp.Literal(",a"))
    mnemo.setParseAction(lambda r: r[0].ref.lower() + "".join(r[1:]))
    integer = pp.Regex(r"[1-9][0-9]*").setParseAction(lambda r: int(r[0], 10))
    hexa = pp.Regex(r"0[xX][0-9a-fA-F]+").setParseAction(
        lambda r: int(r[0], 16))
    octa = pp.Regex(r"0[0-7]*").setParseAction(lambda r: int(r[0], 8))
    bina = pp.Regex(r"0[bB][01]+").setParseAction(lambda r: int(r[0], 2))
    char = pp.Regex(r"('.)|('\\\\)").setParseAction(lambda r: ord(r[0]))
    number = integer | hexa | octa | bina | char
    number.setParseAction(lambda r: env.cst(r[0], 32))

    term = symbol | number

    exp = pp.Forward()

    op_one = pp.oneOf("- ~")
    op_sig = pp.oneOf("+ -")
    op_mul = pp.oneOf("* /")
    op_cmp = pp.oneOf("== != <= >= < > <>")
    op_bit = pp.oneOf("^ && || & |")

    operators = [
        (op_one, 1, pp.opAssoc.RIGHT),
        (op_sig, 2, pp.opAssoc.LEFT),
        (op_mul, 2, pp.opAssoc.LEFT),
        (op_cmp, 2, pp.opAssoc.LEFT),
        (op_bit, 2, pp.opAssoc.LEFT),
    ]
    reg = pp.Suppress("%") + pp.NotAny(pp.oneOf("hi lo")) + symbol
    hilo = pp.oneOf("%hi %lo") + pp.Suppress("(") + exp + pp.Suppress(")")
    exp << pp.operatorPrecedence(term | reg | hilo, operators)

    adr = pp.Suppress("[") + exp + pp.Suppress("]")
    mem = adr  # +pp.Optional(symbol|imm)
    mem.setParseAction(lambda r: env.mem(r[0]))

    opd = exp | mem | reg
    opds = pp.Group(pp.delimitedList(opd))

    instr = mnemo + pp.Optional(opds) + pp.Optional(comment)

    def action_reg(toks):
        rname = toks[0]
        return env.reg(rname.ref)

    def action_hilo(toks):
        v = toks[1]
        return env.hi(v) if toks[0] == "%hi" else env.lo(v).zeroextend(32)

    def action_exp(toks):
        tok = toks[0]
        if isinstance(tok, env.exp):
            return tok
        if len(tok) == 2:
            op = tok[0]
            r = tok[1]
            if isinstance(r, list):
                r = action_exp(r)
            return env.oper(op, r)
        elif len(tok) == 3:
            op = tok[1]
            l = tok[0]
            r = tok[2]
            if isinstance(l, list):
                l = action_exp(l)
            if isinstance(r, list):
                r = action_exp(r)
            return env.oper(op, l, r)
        else:
            return tok

    def action_instr(toks):
        i = instruction(b"")
        i.mnemonic = toks[0]
        if len(toks) > 1:
            i.operands = toks[1][0:]
        return asmhelper(i)

    # actions:
    reg.setParseAction(action_reg)
    hilo.setParseAction(action_hilo)
    exp.setParseAction(action_exp)
    instr.setParseAction(action_instr)