comparison python/target/arminstructions.py @ 335:582a1aaa3983

Added long branch format
author Windel Bouwman
date Mon, 17 Feb 2014 20:41:30 +0100
parents 6f4753202b9a
children d1ecc493384e
comparison
equal deleted inserted replaced
334:6f4753202b9a 335:582a1aaa3983
214 self.expr = expr 214 self.expr = expr
215 self.label = None 215 self.label = None
216 else: 216 else:
217 raise NotImplementedError() 217 raise NotImplementedError()
218 218
219 def resolve(self, f):
220 if self.label:
221 self.expr = f(self.label.name)
222
223 def encode(self): 219 def encode(self):
224 return u32(self.expr) 220 return u32(self.expr)
221
222 def relocations(self):
223 assert not isinstance(self.expr, LabelRef)
224 return []
225 225
226 def __repr__(self): 226 def __repr__(self):
227 return 'DCD 0x{0:X}'.format(self.expr) 227 return 'DCD 0x{0:X}'.format(self.expr)
228 228
229 229
323 323
324 @classmethod 324 @classmethod
325 def fromim(cls, im): 325 def fromim(cls, im):
326 return cls(im.dst[0], im.others[0]) 326 return cls(im.dst[0], im.others[0])
327 327
328 def resolve(self, f): 328 def relocations(self):
329 la = f(self.label.name) 329 return [(self.label.name, 'lit_add_8')]
330 sa = align(self.address + 2, 4)
331 self.offset = (la - sa)
332 if self.offset < 0:
333 self.offset = 0
334 330
335 def encode(self): 331 def encode(self):
336 rt = self.rt.num 332 rt = self.rt.num
337 assert rt < 8 333 assert rt < 8
338 assert self.offset % 4 == 0 334 assert self.offset % 4 == 0
486 opcode = 0b01000110 482 opcode = 0b01000110
487 return u16((opcode << 8) | (D << 7) |(Rm << 3) | Rd) 483 return u16((opcode << 8) | (D << 7) |(Rm << 3) | Rd)
488 484
489 def __repr__(self): 485 def __repr__(self):
490 return '{} {}, {}'.format(self.mnemonic, self.rd, self.rm) 486 return '{} {}, {}'.format(self.mnemonic, self.rd, self.rm)
491 487
492 488
493 @instruction 489 @instruction
494 class Mul(ArmInstruction): 490 class Mul(ArmInstruction):
495 """ mul Rn, Rdm """ 491 """ mul Rn, Rdm """
496 operands = (Reg8Op, Reg8Op) 492 operands = (Reg8Op, Reg8Op)
600 def __init__(self, target_label): 596 def __init__(self, target_label):
601 assert type(target_label) is LabelRef 597 assert type(target_label) is LabelRef
602 self.target = target_label 598 self.target = target_label
603 self.offset = 0 599 self.offset = 0
604 600
605 def resolve(self, f):
606 la = f(self.target.name)
607 sa = self.address + 4
608 self.offset = (la - sa)
609
610 def __repr__(self): 601 def __repr__(self):
611 return '{} {}'.format(self.mnemonic, self.target.name) 602 return '{} {}'.format(self.mnemonic, self.target.name)
603
604
605 class Imm11Reloc:
606 def apply(self, P, S):
607 pass
612 608
613 609
614 @instruction 610 @instruction
615 class B(jumpBase_ins): 611 class B(jumpBase_ins):
616 mnemonic = 'B' 612 mnemonic = 'B'
617 def encode(self): 613 def encode(self):
618 imm11 = wrap_negative(self.offset >> 1, 11) 614 imm11 = wrap_negative(self.offset >> 1, 11)
619 h = (0b11100 << 11) | imm11 # | 1 # 1 to enable thumb mode 615 h = (0b11100 << 11) | imm11 # | 1 # 1 to enable thumb mode
620 return u16(h) 616 return u16(h)
621 617
618 def relocations(self):
619 return [(self.target.name, 'wrap_new11')]
622 620
623 @instruction 621 @instruction
624 class Bl(jumpBase_ins): 622 class Bl(jumpBase_ins):
625 mnemonic = 'BL' 623 mnemonic = 'BL'
626 def encode(self): 624 def encode(self):
632 s = (imm32 >> 24) & 0x1 630 s = (imm32 >> 24) & 0x1
633 h1 = (0b11110 << 11) | (s << 10) | imm10 631 h1 = (0b11110 << 11) | (s << 10) | imm10
634 h2 = (0b1101 << 12) | (j1 << 13) | (j2 << 11) | imm11 632 h2 = (0b1101 << 12) | (j1 << 13) | (j2 << 11) | imm11
635 return u16(h1) + u16(h2) 633 return u16(h1) + u16(h2)
636 634
637 635 def relocations(self):
638 class cond_base_ins(jumpBase_ins): 636 return [(self.target.name, 'bl_imm11_imm10')]
637
638
639 class cond_base_ins_short(jumpBase_ins):
639 def encode(self): 640 def encode(self):
640 imm8 = wrap_negative(self.offset >> 1, 8) 641 imm8 = wrap_negative(self.offset >> 1, 8)
641 h = (0b1101 << 12) | (self.cond << 8) | imm8 642 h = (0b1101 << 12) | (self.cond << 8) | imm8
642 return u16(h) 643 return u16(h)
644
645 def relocations(self):
646 return [(self.target.name, 'rel8')]
647
648
649 class cond_base_ins(jumpBase_ins):
650 """ Encoding T3 """
651 def encode(self):
652 j1 = 1 # TODO: what do these mean?
653 j2 = 1
654 h1 = (0b11110 << 11) | (self.cond << 6)
655 h2 = (0b1101 << 12) | (j1 << 13) | (j2 << 11)
656 return u16(h1) + u16(h2)
657
658 def relocations(self):
659 return [(self.target.name, 'b_imm11_imm6')]
643 660
644 661
645 @instruction 662 @instruction
646 class Beq(cond_base_ins): 663 class Beq(cond_base_ins):
647 mnemonic = 'beq' 664 mnemonic = 'beq'