def test_register_allocator_leave_special_regs(): ''' Test whether the register allocator does not allocate regs for special registers. ''' from blip.code.BlipCompiler import RegisterAllocator from blip.simulator.opcodes import SPECIAL_REGS code = [ ('mov', None, 'a', 'e'), ('add', None, 'b', 'a', 'b'), ('mov', None, 'out', 'b'), ('mov', None, 'd', 'west'), ('cmp', None, 'e', 'a'), ('memw_imm', None, 34, 'd'), ('imm', None, 'f', 12) ] liveness = RegisterAllocator.liveness_analysis(code) test_code, test_mapping = RegisterAllocator.register_allocation(code, liveness, 8) # test if no special reg is in the mapping of the regalloc assert len(set(test_mapping.keys()).intersection(SPECIAL_REGS)) == 0
def test_liveness_analysis(): ''' Test the liveness analysis, note that this analysis only accepts SSA code. ''' from blip.code.BlipCompiler import RegisterAllocator code = [ ('mov', None, 'a', 'e'), ('add', None, 'b', 'a', 'b'), ('mov', None, 'out', 'b'), ('mov', None, 'd', 'west'), ('cmp', None, 'e', 'a'), ('memw_imm', None, 34, 'd'), ('imm', None, 'f', 12) ] test_liveness = RegisterAllocator.liveness_analysis(code) expect_liveness = { 'a':(0, [1,4]), 'b':(1, [1, 2]), 'd':(3, [5]), 'e':(0, [0,4]), 'f':(6, [6]) } assert expect_liveness == test_liveness
args = {} block_size = (4, 4) block_len = 100 convertor = CodegenToSSAConvertor(block_len, True) try: all_code = [] all_usage = [] for i, current_ref_code in enumerate(convertor.gen_ssa_fragments(test_codegen, Code(), block_size, args)): test_code = convert_compact_repr_to_obj(current_ref_code) for instr, reg_usage in current_ref_code: all_code.append(instr) all_usage.append(reg_usage) print '\n'.join(str(x) for x in test_code) liveness = RegisterAllocator.liveness_analysis(all_code) RegisterAllocator.print_liveness(all_code, liveness) print '\n'.join(' '.join('x' if x else '.' for x in y) for y in all_usage) original_reg_ranges = get_allocation_ranges(all_usage) print 'original register ranges:' print original_reg_ranges new_code, reg_mapping = RegisterAllocator.register_allocation(all_code, liveness, 30) print '\n'.join(str(x) for x in new_code) print reg_mapping except Exception, e: print str(e) import pdb, traceback, sys traceback.print_tb(sys.exc_traceback)