18 """Adds a register @arg3 and a sign-extended immediate value @arg2 and
19 stores the result in a register @arg1"""
24 "A word is loaded into a register @arg1 from the specified address @arg2."
29 "The contents of @arg2 is stored at the specified address @arg1."
34 "Jumps to the calculated address @arg1 and stores the return address in $RA"
37 RA = ExprId(ir.get_next_break_label(instr))
41 """Jump to an address stored in a register @arg1, and store the return
42 address in another register @arg2"""
45 arg2 = ExprId(ir.get_next_break_label(instr))
51 RA = ExprId(ir.get_next_break_label(instr))
60 """A byte is loaded (unsigned extended) into a register @arg1 from the
61 specified address @arg2."""
62 arg1 = mem8[arg2.arg].zeroExtend(32)
66 """A word is loaded (unsigned extended) into a register @arg1 from the
67 specified address @arg2."""
68 arg1 = mem16[arg2.arg].zeroExtend(32)
72 "A byte is loaded into a register @arg1 from the specified address @arg2."
73 arg1 = mem8[arg2.arg].signExtend(32)
76 def beq(arg1, arg2, arg3):
77 "Branches on @arg3 if the quantities of two registers @arg1, @arg2 are eq"
78 dst = ExprId(ir.get_next_break_label(instr))
if arg1 - arg2
else arg3
84 """Branches on @arg2 if the quantities of register @arg1 is greater than or
86 dst = ExprId(ir.get_next_break_label(instr))
if arg1.msb()
else arg2
91 def bne(arg1, arg2, arg3):
92 """Branches on @arg3 if the quantities of two registers @arg1, @arg2 are NOT
94 dst = arg3
if arg1 - arg2
else ExprId(ir.get_next_break_label(instr))
100 """The immediate value @arg2 is shifted left 16 bits and stored in the
101 register @arg1. The lower 16 bits are zeroes."""
102 arg1 = ExprCompose([(i16(0), 0, 16), (arg2[:16], 16, 32)])
110 """Jump to an address @arg1"""
116 """Bitwise logical ors two registers @arg2, @arg3 and stores the result in a
121 def nor(arg1, arg2, arg3):
122 """Bitwise logical Nors two registers @arg2, @arg3 and stores the result in
124 arg1 = (arg2 | arg3) ^ i32(-1)
128 """Bitwise logical ands two registers @arg2, @arg3 and stores the result in
133 def ext(arg1, arg2, arg3, arg4):
136 arg1 = arg2[pos:pos + size].zeroExtend(32)
139 def mul(arg1, arg2, arg3):
140 """Multiplies @arg2 by $arg3 and stores the result in @arg1."""
141 arg1 =
'imul'(arg2, arg3)
145 """If @arg3 is less than @arg2 (unsigned), @arg1 is set to one. It gets zero
147 arg1 = (((arg2 - arg3) ^ ((arg2 ^ arg3) & ((arg2 - arg3) ^ arg2))) ^ arg2 ^ arg3).msb().zeroExtend(32)
150 def slt(arg1, arg2, arg3):
151 """If @arg3 is less than @arg2 (signed), @arg1 is set to one. It gets zero
153 arg1 = ((arg2 - arg3) ^ ((arg2 ^ arg3) & ((arg2 - arg3) ^ arg2))).msb().zeroExtend(32)
161 """The least significant byte of @arg1 is stored at the specified address
163 mem8[arg2.arg] = arg1[:8]
167 mem16[arg2.arg] = arg1[:16]
180 def srl(arg1, arg2, arg3):
181 """Shifts arg1 register value @arg2 right by the shift amount @arg3 and
182 places the value in the destination register @arg1.
183 Zeroes are shifted in."""
187 def sra(arg1, arg2, arg3):
188 """Shifts arg1 register value @arg2 right by the shift amount @arg3 and
189 places the value in the destination register @arg1. The sign bit is shifted
191 arg1 =
'a>>'(arg2, arg3)
195 arg1 =
'a>>'(arg2, arg3 & i32(0x1F))
198 def sll(arg1, arg2, arg3):
203 """Shifts a register value @arg2 right by the amount specified in @arg3 and
204 places the value in the destination register @arg1.
205 Zeroes are shifted in."""
206 arg1 = arg2 >> (arg3 & i32(0x1F))
210 """Shifts a register value @arg2 left by the amount specified in @arg3 and
211 places the value in the destination register @arg1.
212 Zeroes are shifted in."""
213 arg1 = arg2 << (arg3 & i32(0x1F))
217 """Exclusive ors two registers @arg2, @arg3 and stores the result in a
223 arg1 = arg2[:8].signExtend(32)
227 arg1 = arg2[:16].signExtend(32)
231 """Branches on @arg2 if the register @arg1 is less than zero"""
232 dst_o = arg2
if arg1.msb()
else ExprId(ir.get_next_break_label(instr))
238 """Branches on @arg2 if the register @arg1 is less than or equal to zero"""
239 cond = (i1(1)
if arg1
else i1(0)) | arg1.msb()
240 dst_o = arg2
if cond
else ExprId(ir.get_next_break_label(instr))
246 """Branches on @arg2 if the register @arg1 is greater than zero"""
247 cond = (i1(1)
if arg1
else i1(0)) | arg1.msb()
248 dst_o = ExprId(ir.get_next_break_label(instr))
if cond
else arg2
254 arg1 = ExprCompose([(arg2[8:16], 0, 8),
256 (arg2[24:32], 16, 24),
257 (arg2[16:24], 24, 32)])
261 arg1 =
'>>>'(arg2, arg3)
266 arg1 =
'fadd'(arg2, arg3)
271 arg1 =
'fsub'(arg2, arg3)
276 arg1 =
'fdiv'(arg2, arg3)
281 arg1 =
'fmul'(arg2, arg3)
312 def ins(ir, instr, a, b, c, d):
319 my_slices.append((a[:pos], 0, pos))
321 my_slices.append((b[:l], pos, pos+l))
323 my_slices.append((a[pos+l:], pos+l, 32))
324 r = m2_expr.ExprCompose(my_slices)
325 e.append(m2_expr.ExprAff(a, r))
331 arg1 = (
'mem_%.2d_to_single' % arg2.size)(arg2)
335 arg2 = (
'single_to_mem_%.2d' % arg1.size)(arg1)
339 arg1 =
'fcomp_lt'(arg2, arg3)
343 arg1 =
'fcomp_eq'(arg2, arg3)
347 arg1 =
'fcomp_le'(arg2, arg3)
351 dst_o = arg2
if arg1
else ExprId(ir.get_next_break_label(instr))
357 dst_o = ExprId(ir.get_next_break_label(instr))
if arg1
else arg2
364 arg1 =
'flt_d_w'(arg2)
368 """Multiplies (signed) @arg1 by @arg2 and stores the result in $R_HI:$R_LO"""
370 result = arg1.signExtend(size * 2) * arg2.signExtend(size * 2)
376 """Multiplies (unsigned) @arg1 by @arg2 and stores the result in $R_HI:$R_LO"""
378 result = arg1.zeroExtend(size * 2) * arg2.zeroExtend(size * 2)
384 "The contents of register $R_HI are moved to the specified register @arg1."
389 "The contents of register R_LO are moved to the specified register @arg1."
404 mnemo_func = sbuild.functions
431 instr, extra_ir = mnemo_func[instr.name.lower()](ir, instr, *args)
432 return instr, extra_ir
437 ir.__init__(self, mn_mips32,
'l', symbol_pool)
438 self.
pc = mn_mips32.getpc()
439 self.
sp = mn_mips32.getsp()
440 self.
IRDst = m2_expr.ExprId(
'IRDst', 32)
446 for i, x
in enumerate(instr_ir):
447 x = m2_expr.ExprAff(x.dst, x.src.replace_expr(
448 {self.
pc: m2_expr.ExprInt32(instr.offset + 4)}))
452 for i, x
in enumerate(irs):
453 x = m2_expr.ExprAff(x.dst, x.src.replace_expr(
454 {self.
pc: m2_expr.ExprInt32(instr.offset + 4)}))
456 return instr_ir, extra_ir
459 l = self.symbol_pool.getby_offset_create(instr.offset + 4)
463 l = self.symbol_pool.getby_offset_create(instr.offset + 8)
466 def add_bloc(self, bloc, gen_pc_updt = False):
472 label = self.get_label(l)
473 c = irbloc(label, [], [])
474 ir_blocs_all.append(c)
476 # print 'Translate', l
477 dst, ir_bloc_cur, ir_blocs_extra = self.instr2ir(l)
479 # for xxx in ir_bloc_cur:
481 assert((dst is None) or (bloc_dst is None))
483 #if bloc_dst is not None:
486 ir_bloc_cur.append(m2_expr.ExprAff(PC_FETCH, dst))
488 if gen_pc_updt is not False:
489 self.gen_pc_update(c, l)
491 c.irs.append(ir_bloc_cur)
495 for b in ir_blocs_extra:
496 b.lines = [l] * len(b.irs)
497 ir_blocs_all += ir_blocs_extra
499 self.post_add_bloc(bloc, ir_blocs_all)
505 ir.__init__(self, mn_mips32,
'b', symbol_pool)
506 self.
pc = mn_mips32.getpc()
507 self.
sp = mn_mips32.getsp()
508 self.
IRDst = m2_expr.ExprId(
'IRDst', 32)