def test_assign(self) -> None: reg = register('foo') op1 = LoadInt(2) op2 = Assign(reg, op1) op3 = Assign(reg, op1) block = make_block([op1, op2, op3]) assert generate_names_for_ir([reg], [block]) == {op1: 'i0', reg: 'foo'}
def test_register(self) -> None: op = LoadInt(5) self.block.ops.append(op) fn = FuncIR( FuncDecl('myfunc', None, 'mod', FuncSignature([self.arg], list_rprimitive)), [self.reg], [self.block]) value_names = generate_names_for_ir(fn.arg_regs, fn.blocks) emitter = Emitter(EmitterContext(NameGenerator([['mod']])), value_names) generate_native_function(fn, emitter, 'prog.py', 'prog', optimize_int=False) result = emitter.fragments assert_string_arrays_equal([ 'PyObject *CPyDef_myfunc(CPyTagged cpy_r_arg) {\n', ' CPyTagged cpy_r_i0;\n', 'CPyL0: ;\n', ' cpy_r_i0 = 10;\n', '}\n', ], result, msg='Generated code invalid')
def test_assign(self) -> None: reg = register('foo') n = Integer(2) op1 = Assign(reg, n) op2 = Assign(reg, n) block = make_block([op1, op2]) assert generate_names_for_ir([reg], [block]) == {reg: 'foo'}
def test_int_op(self) -> None: n1 = Integer(2) n2 = Integer(4) op1 = IntOp(int_rprimitive, n1, n2, IntOp.ADD) op2 = IntOp(int_rprimitive, op1, n2, IntOp.ADD) block = make_block([op1, op2, Unreachable()]) assert generate_names_for_ir([], [block]) == {op1: 'r0', op2: 'r1'}
def assert_emit(self, op: Op, expected: str, next_block: Optional[BasicBlock] = None) -> None: block = BasicBlock(0) block.ops.append(op) value_names = generate_names_for_ir(self.registers, [block]) emitter = Emitter(self.context, value_names) declarations = Emitter(self.context, value_names) emitter.fragments = [] declarations.fragments = [] visitor = FunctionEmitterVisitor(emitter, declarations, 'prog.py', 'prog') visitor.next_block = next_block op.accept(visitor) frags = declarations.fragments + emitter.fragments actual_lines = [line.strip(' ') for line in frags] assert all(line.endswith('\n') for line in actual_lines) actual_lines = [line.rstrip('\n') for line in actual_lines] if not expected.strip(): expected_lines = [] else: expected_lines = expected.rstrip().split('\n') expected_lines = [line.strip(' ') for line in expected_lines] assert_string_arrays_equal(expected_lines, actual_lines, msg='Generated code unexpected')
def run_case(self, testcase: DataDrivenTestCase) -> None: """Perform a data-flow analysis test case.""" with use_custom_builtins( os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase): testcase.output = replace_native_int(testcase.output) try: ir = build_ir_for_single_file(testcase.input) except CompileError as e: actual = e.messages else: actual = [] for fn in ir: if (fn.name == TOP_LEVEL_NAME and not testcase.name.endswith('_toplevel')): continue exceptions.insert_exception_handling(fn) actual.extend(format_func(fn)) cfg = dataflow.get_cfg(fn.blocks) args = set(fn.arg_regs) # type: Set[Value] name = testcase.name if name.endswith('_MaybeDefined'): # Forward, maybe analysis_result = dataflow.analyze_maybe_defined_regs( fn.blocks, cfg, args) elif name.endswith('_Liveness'): # Backward, maybe analysis_result = dataflow.analyze_live_regs( fn.blocks, cfg) elif name.endswith('_MustDefined'): # Forward, must analysis_result = dataflow.analyze_must_defined_regs( fn.blocks, cfg, args, regs=all_values(fn.arg_regs, fn.blocks)) elif name.endswith('_BorrowedArgument'): # Forward, must analysis_result = dataflow.analyze_borrowed_arguments( fn.blocks, cfg, args) else: assert False, 'No recognized _AnalysisName suffix in test case' names = generate_names_for_ir(fn.arg_regs, fn.blocks) for key in sorted(analysis_result.before.keys(), key=lambda x: (x[0].label, x[1])): pre = ', '.join( sorted(names[reg] for reg in analysis_result.before[key])) post = ', '.join( sorted(names[reg] for reg in analysis_result.after[key])) actual.append('%-8s %-23s %s' % ((key[0].label, key[1]), '{%s}' % pre, '{%s}' % post)) assert_test_output(testcase, actual, 'Invalid source code output')
def generate_native_function(fn: FuncIR, emitter: Emitter, source_path: str, module_name: str) -> None: declarations = Emitter(emitter.context) names = generate_names_for_ir(fn.arg_regs, fn.blocks) body = Emitter(emitter.context, names) visitor = FunctionEmitterVisitor(body, declarations, source_path, module_name) declarations.emit_line(f'{native_function_header(fn.decl, emitter)} {{') body.indent() for r in all_values(fn.arg_regs, fn.blocks): if isinstance(r.type, RTuple): emitter.declare_tuple_struct(r.type) if isinstance(r.type, RArray): continue # Special: declared on first assignment if r in fn.arg_regs: continue # Skip the arguments ctype = emitter.ctype_spaced(r.type) init = '' declarations.emit_line('{ctype}{prefix}{name}{init};'.format( ctype=ctype, prefix=REG_PREFIX, name=names[r], init=init)) # Before we emit the blocks, give them all labels blocks = fn.blocks for i, block in enumerate(blocks): block.label = i common = frequently_executed_blocks(fn.blocks[0]) for i in range(len(blocks)): block = blocks[i] visitor.rare = block not in common next_block = None if i + 1 < len(blocks): next_block = blocks[i + 1] body.emit_label(block) visitor.next_block = next_block ops = block.ops visitor.ops = ops visitor.op_index = 0 while visitor.op_index < len(ops): ops[visitor.op_index].accept(visitor) visitor.op_index += 1 body.emit_line('}') emitter.emit_from_emitter(declarations) emitter.emit_from_emitter(body)
def generate_native_function(fn: FuncIR, emitter: Emitter, source_path: str, module_name: str, optimize_int: bool = True) -> None: if optimize_int: const_int_regs = find_constant_integer_registers(fn.blocks) else: const_int_regs = {} declarations = Emitter(emitter.context) names = generate_names_for_ir(fn.arg_regs, fn.blocks) body = Emitter(emitter.context, names) visitor = FunctionEmitterVisitor(body, declarations, source_path, module_name, const_int_regs) declarations.emit_line('{} {{'.format( native_function_header(fn.decl, emitter))) body.indent() for r in all_values(fn.arg_regs, fn.blocks): if isinstance(r.type, RTuple): emitter.declare_tuple_struct(r.type) if r in fn.arg_regs: continue # Skip the arguments ctype = emitter.ctype_spaced(r.type) init = '' if r not in const_int_regs: declarations.emit_line('{ctype}{prefix}{name}{init};'.format( ctype=ctype, prefix=REG_PREFIX, name=names[r], init=init)) # Before we emit the blocks, give them all labels for i, block in enumerate(fn.blocks): block.label = i for block in fn.blocks: body.emit_label(block) for op in block.ops: op.accept(visitor) body.emit_line('}') emitter.emit_from_emitter(declarations) emitter.emit_from_emitter(body)
def test_simple(self) -> None: self.block.ops.append(Return(self.reg)) fn = FuncIR( FuncDecl('myfunc', None, 'mod', FuncSignature([self.arg], int_rprimitive)), [self.reg], [self.block]) value_names = generate_names_for_ir(fn.arg_regs, fn.blocks) emitter = Emitter(EmitterContext(NameGenerator([['mod']])), value_names) generate_native_function(fn, emitter, 'prog.py', 'prog') result = emitter.fragments assert_string_arrays_equal([ 'CPyTagged CPyDef_myfunc(CPyTagged cpy_r_arg) {\n', 'CPyL0: ;\n', ' return cpy_r_arg;\n', '}\n', ], result, msg='Generated code invalid')
def assert_emit(self, op: Op, expected: str) -> None: block = BasicBlock(0) block.ops.append(op) value_names = generate_names_for_ir(self.registers, [block]) emitter = Emitter(self.context, value_names) declarations = Emitter(self.context, value_names) emitter.fragments = [] declarations.fragments = [] const_int_regs = {} # type: Dict[LoadInt, int] visitor = FunctionEmitterVisitor(emitter, declarations, 'prog.py', 'prog', const_int_regs) op.accept(visitor) frags = declarations.fragments + emitter.fragments actual_lines = [line.strip(' ') for line in frags] assert all(line.endswith('\n') for line in actual_lines) actual_lines = [line.rstrip('\n') for line in actual_lines] expected_lines = expected.rstrip().split('\n') expected_lines = [line.strip(' ') for line in expected_lines] assert_string_arrays_equal(expected_lines, actual_lines, msg='Generated code unexpected')
def test_int_op(self) -> None: op1 = LoadInt(2) op2 = LoadInt(4) op3 = IntOp(int_rprimitive, op1, op2, IntOp.ADD) block = make_block([op1, op2, op3, Unreachable()]) assert generate_names_for_ir([], [block]) == {op1: 'i0', op2: 'i1', op3: 'r0'}
def test_arg(self) -> None: reg = register('foo') assert generate_names_for_ir([reg], []) == {reg: 'foo'}
def test_empty(self) -> None: assert generate_names_for_ir([], []) == {}