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 assert_emit(self, op: Op, expected: str) -> None: self.emitter.fragments = [] self.declarations.fragments = [] self.env.temp_index = 0 if isinstance(op, RegisterOp): self.env.add_op(op) op.accept(self.visitor) frags = self.declarations.fragments + self.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 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 assert_emit(self, op: Op, expected: str) -> None: if isinstance(op, RegisterOp): self.env.add_op(op) value_names = generate_names_for_env(self.env) emitter = Emitter(self.context, self.env, value_names) declarations = Emitter(self.context, self.env, 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 non_trivial_sources(op: Op) -> Set[Value]: result = set() for source in op.sources(): if not isinstance(source, Integer): result.add(source) return result