def sparc_arith_(obj,rd,a,rs1,i,rs2,simm13): obj.misc['icc'] = (a==1) src1 = env.r[rs1] src2 = env.r[rs2] if i==0 else env.cst(simm13,13).signextend(32) dst = env.r[rd] obj.operands = [src1,src2,dst] obj.type = type_data_processing
def sparc_wr_(obj,rd,rs1,i,rs2,simm13,_dst): if _dst==env.y: if rs1!=0: _dst = env.asr[rs1] src1 = env.r[rs1] src2 = env.r[rs2] if i==0 else env.cst(simm13,13).signextend(32) obj.operands = [src1,src2,_dst] obj.type = type_other
def sparc_sethi(obj,rd,imm22): if rd==0 and imm22==0: obj.mnemonic = 'nop' else: src = env.cst(imm22,22) dst = env.r[rd] obj.operands = [src,dst] obj.type = type_data_processing
def sparc_jmpl(obj,rd,rs1,i,rs2,simm13): src1 = env.r[rs1] src2 = env.r[rs2] if i==0 else env.cst(simm13,13).signextend(32) adr = src1+src2 dst = env.r[rd] obj.operands = [adr,dst] obj.misc['delayed'] = True obj.type = type_control_flow
def sparc_ldf_ldc(obj,rd,a,op3,rs1,i,unused,rs2,simm13): adr = env.r[rs1] if i==0: adr += env.r[rs2] else: adr += env.cst(simm13,13).signextend(32) src = env.ptr(adr) dst = env.f[rd] if a==0 else env.c[rd] if op3&0xf==0b0001: dst = env.fsr if a==0 else env.csr obj.operands = [src,dst] obj.type = type_data_processing
def sparc_ld_(obj, rd, a, op3, rs1, i, asi, rs2, simm13): adr = env.r[rs1] if i == 0: adr += env.r[rs2] src = env.ptr(adr, seg=asi) else: adr += env.cst(simm13, 13).signextend(32) src = env.ptr(adr) dst = env.r[rd] if op3 & 0xF == 0b0011 and rd % 1 == 1: raise InstructionError(obj) obj.operands = [src, dst] obj.type = type_data_processing
def sparc_st_(obj, rd, a, op3, rs1, i, asi, rs2, simm13): adr = env.r[rs1] if i == 0: adr += env.r[rs2] dst = env.ptr(adr, asi) else: adr += env.cst(simm13, 13).signextend(32) dst = env.ptr(adr) src = env.r[rd] if obj.mnemonic == "std" and rd % 1 == 1: raise InstructionError(obj) obj.operands = [src, dst] obj.type = type_data_processing
def sparc_stf_stc(obj, rd, a, op3, rs1, i, unused, rs2, simm13): adr = env.r[rs1] if i == 0: adr += env.r[rs2] else: adr += env.cst(simm13, 13).signextend(32) dst = env.ptr(adr) src = env.f[rd] if a == 0 else env.c[rd] if op3 & 0xF == 0b0101: src = env.fsr if a == 0 else env.csr elif op3 & 0xF == 0b0110: src = env.fq if a == 0 else env.cq obj.operands = [src, dst] obj.type = type_data_processing
def sparc_st_(obj,rd,a,op3,rs1,i,asi,rs2,simm13): adr = env.r[rs1] if i==0: adr += env.r[rs2] if a==1: obj.mnemonic += 'a' dst = env.ptr(adr,asi) else: adr += env.cst(simm13,13).signextend(32) dst = env.ptr(adr) if a==1: raise InstructionError(obj) src = env.r[rd] if obj.mnemonic=='std' and rd%1==1: raise InstructionError(obj) obj.operands = [src,dst] obj.type = type_data_processing
def sparc_rett(obj,rs1,i,rs2,simm13): src1 = env.r[rs1] src2 = env.r[rs2] if i==0 else env.cst(simm13,13).signextend(32) adr = src1+src2 obj.operands = [adr] obj.type = type_control_flow
def sparc_call(obj,disp30): obj.operands = [env.cst(disp30,30).signextend(32)] obj.misc['delayed'] = True obj.type = type_control_flow
def sparc_unimp(obj,const22): obj.operands = [env.cst(const22,22)] obj.type = type_undefined
def sparc_Bicc(obj,a,disp22): obj.operands = [env.cst(disp22,22).signextend(32)] obj.misc['delayed'] = True obj.misc['annul'] = (a==1) obj.type = type_control_flow
def sparc_sethi(obj, rd, imm22): src = env.cst(imm22, 22) dst = env.r[rd] obj.operands = [src, dst] obj.type = type_data_processing
def sparc_flush(obj,rs1,i,rs2,simm13): src1 = env.r[rs1] src2 = env.r[rs2] if i==0 else env.cst(simm13,13).signextend(32) obj.operands = [(src1+src2)] obj.type = type_cpu_state
def sparc_Bicc(obj, a, disp22): obj.operands = [env.cst(disp22, 22).signextend(32)] obj.misc["delayed"] = True obj.misc["annul"] = a == 1 obj.type = type_control_flow
def sparc_shift_(obj,rd,rs1,i,rs2): src1 = env.r[rs1] src2 = env.r[rs2] if i==0 else env.cst(rs2,5) dst = env.r[rd] obj.operands = [src1,src2,dst] obj.type = type_data_processing
def sparc_tagged_(obj,rd,rs1,i,rs2,simm13): src1 = env.r[rs1] src2 = env.r[rs2] if i==0 else env.cst(simm13,13).signextend(32) dst = env.r[rd] obj.operands = [src1,src2,dst] obj.type = type_data_processing
class sparc_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] if rname.ref.startswith('asr'): return env.reg(rname.ref) return env.__dict__[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('') 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)
def sparc_Ticc(obj,rs1,i,rs2,imm7): src1 = env.r[rs1] src2 = env.r[rs2] if i==0 else env.cst(imm7,7).signextend(32) obj.src = (src1+src2)[0:7] obj.operands = [obj.src] obj.type = type_control_flow