def test_assemble_basic_reloc(self): txt = r''' .segment text rip1: nop call rip1 jr $r29 li $r11, rip2 .alloc 256 call rip3 sw $r5, 0($r5) rip2: nop rip3: nop ''' obj = self.assemble(txt) # export and import tables should be empty self.assertEqual(obj.export_table, []) self.assertEqual(obj.import_table, []) # reloc table self.assertEqual(obj.reloc_table[0], ('text', RelocType.CALL, ('text', 4))) self.assertEqual(obj.reloc_table[1], ('text', RelocType.LI, ('text', 12))) self.assertEqual(obj.reloc_table[2], ('text', RelocType.CALL, ('text', 276))) # make sure that the assembled instructions are correct. # text_seg = obj.seg_data['text'] # the first part of LI is the LUI, which gets nothing from # the offset, since it's too small # the second part is the ORI, which gets the offset in its # constant field # self.assertEqual(bytes2word(text_seg[12:16]), 0x6 << 26 | 11 << 21) self.assertEqual(bytes2word(text_seg[16:20]), 0x2A << 26 | 11 << 21 | 11 << 16 | 284) # check call's instruction too self.assertEqual(bytes2word(text_seg[276:280]), 0x1D << 26 | (288 // 4))
def test_assemble_basic_import(self): txt = r''' .segment text call georgia jr $r29 li $r11, california .alloc 256 call california sw $r5, 0($r5) ''' obj = self.assemble(txt) # export and reloc tables should be empty self.assertEqual(obj.export_table, []) self.assertEqual(obj.reloc_table, []) # import table self.assertEqual(obj.import_table[0], ('georgia', ImportType.CALL, ('text', 0))) self.assertEqual(obj.import_table[1], ('california', ImportType.LI, ('text', 8))) self.assertEqual(obj.import_table[2], ('california', ImportType.CALL, ('text', 16 + 256))) # see what was actually assembled into the first CALL # since the constant is imported, 0 is placed in the # off26 field # text_seg = obj.seg_data['text'] self.assertEqual(bytes2word(text_seg[0:4]), 0x1D << 26)
def test_assemble_basic_export_and_segment(self): txt = r''' .segment text .global jj .global nb and $r2, $r0, $r2 # clear r2 jj: lw $r17, 20($v1) .segment data .byte 0x14, 0x18, 0x01, 8, 9 nb: .word 0x56899001 ''' obj = self.assemble(txt) # export table self.assertEqual(obj.export_table[0], ('jj', ('text', 4))) self.assertEqual(obj.export_table[1], ('nb', ('data', 8))) # import and reloc tables should be empty self.assertEqual(obj.import_table, []) self.assertEqual(obj.reloc_table, []) self.assertEqual(len(obj.seg_data), 2) text_seg = obj.seg_data['text'] data_seg = obj.seg_data['data'] # check the correct encoding of instructions in the text # segment # self.assertEqual(bytes2word(text_seg[0:4]), 9 << 26 | 2 << 21 | 2 << 11) self.assertEqual(bytes2word(text_seg[4:8]), 0xF << 26 | 17 << 21 | 3 << 16 | 20) # check the correct placement of data in the data segment # self.assertEqual(data_seg[0:5], list(unpack_bytes(b'\x14\x18\x01\x08\x09'))) self.assertEqual(data_seg[8:12], list(unpack_bytes(b'\x01\x90\x89\x56')))
def test_assemble_memref_define(self): txt = r''' .segment text .define DEF, 0x20 lw $r3, DEF($r4) ''' obj = self.assemble(txt) text_seg = obj.seg_data['text'] self.assertEqual(bytes2word(text_seg[0:4]), 0xF << 26 | 3 << 21 | 4 << 16 | 0x20)