forked from M-Labs/artiq
1
0
Fork 0

transforms.ARTIQIRGenerator: IndexError loc should point to "[".

This commit is contained in:
whitequark 2015-08-08 16:18:57 +03:00
parent 96c770190c
commit acd8d6355f
1 changed files with 16 additions and 9 deletions

View File

@ -466,12 +466,16 @@ class ARTIQIRGenerator(algorithm.Visitor):
def visit_Continue(self, node): def visit_Continue(self, node):
self.append(ir.Branch(self.continue_target)) self.append(ir.Branch(self.continue_target))
def raise_exn(self, exn): def raise_exn(self, exn, loc=None):
if exn is not None: if exn is not None:
loc_file = ir.Constant(self.current_loc.source_buffer.name, builtins.TStr()) if loc is None:
loc_line = ir.Constant(self.current_loc.line(), builtins.TInt(types.TValue(32))) loc = self.current_loc
loc_column = ir.Constant(self.current_loc.column(), builtins.TInt(types.TValue(32)))
loc_file = ir.Constant(loc.source_buffer.name, builtins.TStr())
loc_line = ir.Constant(loc.line(), builtins.TInt(types.TValue(32)))
loc_column = ir.Constant(loc.column(), builtins.TInt(types.TValue(32)))
loc_function = ir.Constant(".".join(self.name), builtins.TStr()) loc_function = ir.Constant(".".join(self.name), builtins.TStr())
self.append(ir.SetAttr(exn, "__file__", loc_file)) self.append(ir.SetAttr(exn, "__file__", loc_file))
self.append(ir.SetAttr(exn, "__line__", loc_line)) self.append(ir.SetAttr(exn, "__line__", loc_line))
self.append(ir.SetAttr(exn, "__col__", loc_column)) self.append(ir.SetAttr(exn, "__col__", loc_column))
@ -686,7 +690,7 @@ class ARTIQIRGenerator(algorithm.Visitor):
else: else:
self.append(ir.SetAttr(obj, node.attr, self.current_assign)) self.append(ir.SetAttr(obj, node.attr, self.current_assign))
def _map_index(self, length, index, one_past_the_end=False): def _map_index(self, length, index, one_past_the_end=False, loc=None):
lt_0 = self.append(ir.Compare(ast.Lt(loc=None), lt_0 = self.append(ir.Compare(ast.Lt(loc=None),
index, ir.Constant(0, index.type))) index, ir.Constant(0, index.type)))
from_end = self.append(ir.Arith(ast.Add(loc=None), length, index)) from_end = self.append(ir.Arith(ast.Add(loc=None), length, index))
@ -703,7 +707,7 @@ class ARTIQIRGenerator(algorithm.Visitor):
exn = self.alloc_exn(builtins.TIndexError(), exn = self.alloc_exn(builtins.TIndexError(),
ir.Constant("index {0} out of bounds 0:{1}", builtins.TStr()), ir.Constant("index {0} out of bounds 0:{1}", builtins.TStr()),
index, length) index, length)
self.raise_exn(exn) self.raise_exn(exn, loc=loc)
self.current_block = in_bounds_block = self.add_block() self.current_block = in_bounds_block = self.add_block()
head.append(ir.BranchIf(in_bounds, in_bounds_block, out_of_bounds_block)) head.append(ir.BranchIf(in_bounds, in_bounds_block, out_of_bounds_block))
@ -754,7 +758,8 @@ class ARTIQIRGenerator(algorithm.Visitor):
if isinstance(node.slice, ast.Index): if isinstance(node.slice, ast.Index):
index = self.visit(node.slice.value) index = self.visit(node.slice.value)
length = self.iterable_len(value, index.type) length = self.iterable_len(value, index.type)
mapped_index = self._map_index(length, index) mapped_index = self._map_index(length, index,
loc=node.begin_loc)
if self.current_assign is None: if self.current_assign is None:
result = self.iterable_get(value, mapped_index) result = self.iterable_get(value, mapped_index)
result.set_name("{}.at.{}".format(value.name, _readable_name(index))) result.set_name("{}.at.{}".format(value.name, _readable_name(index)))
@ -769,13 +774,15 @@ class ARTIQIRGenerator(algorithm.Visitor):
start_index = self.visit(node.slice.lower) start_index = self.visit(node.slice.lower)
else: else:
start_index = ir.Constant(0, node.slice.type) start_index = ir.Constant(0, node.slice.type)
mapped_start_index = self._map_index(length, start_index) mapped_start_index = self._map_index(length, start_index,
loc=node.begin_loc)
if node.slice.upper is not None: if node.slice.upper is not None:
stop_index = self.visit(node.slice.upper) stop_index = self.visit(node.slice.upper)
else: else:
stop_index = length stop_index = length
mapped_stop_index = self._map_index(length, stop_index, one_past_the_end=True) mapped_stop_index = self._map_index(length, stop_index, one_past_the_end=True,
loc=node.begin_loc)
if node.slice.step is not None: if node.slice.step is not None:
step = self.visit(node.slice.step) step = self.visit(node.slice.step)