Skip to content

Commit

Permalink
ARTIQIRGenerator: add semantic locs to all other implicitly raised ex…
Browse files Browse the repository at this point in the history
…ceptions.
  • Loading branch information
whitequark committed Aug 8, 2015
1 parent acd8d63 commit bdcf7f1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion artiq/compiler/testbench/inferencer.py
Expand Up @@ -66,7 +66,7 @@ def process_diagnostic(diag):
buf = source.Buffer("".join(fileinput.input()).expandtabs(),
os.path.basename(fileinput.filename()))
parsed, comments = parse_buffer(buf, engine=engine)
typed = ASTTypedRewriter(engine=engine, globals=prelude.globals()).visit(parsed)
typed = ASTTypedRewriter(engine=engine, prelude=prelude.globals()).visit(parsed)
Inferencer(engine=engine).visit(typed)
if monomorphize:
IntMonomorphizer(engine=engine).visit(typed)
Expand Down
19 changes: 12 additions & 7 deletions artiq/compiler/transforms/artiq_ir_generator.py
Expand Up @@ -714,13 +714,13 @@ def _map_index(self, length, index, one_past_the_end=False, loc=None):

return mapped_index

def _make_check(self, cond, exn_gen):
def _make_check(self, cond, exn_gen, loc=None):
# cond: bool Value, condition
# exn_gen: lambda()->exn Value, exception if condition not true
cond_block = self.current_block

self.current_block = body_block = self.add_block()
self.raise_exn(exn_gen())
self.raise_exn(exn_gen(), loc=loc)

self.current_block = tail_block = self.add_block()
cond_block.append(ir.BranchIf(cond, tail_block, body_block))
Expand Down Expand Up @@ -789,7 +789,8 @@ def visit_SubscriptT(self, node):
self._make_check(
self.append(ir.Compare(ast.NotEq(loc=None), step, ir.Constant(0, step.type))),
lambda: self.alloc_exn(builtins.TValueError(),
ir.Constant("step cannot be zero", builtins.TStr())))
ir.Constant("step cannot be zero", builtins.TStr())),
loc=node.slice.step.loc)
else:
step = ir.Constant(1, node.slice.type)
counting_up = self.append(ir.Compare(ast.Gt(loc=None), step,
Expand All @@ -811,7 +812,8 @@ def visit_SubscriptT(self, node):
lambda: self.alloc_exn(builtins.TValueError(),
ir.Constant("slice size {0} is larger than iterable length {1}",
builtins.TStr()),
slice_size, length))
slice_size, length),
loc=node.slice.loc)

if self.current_assign is None:
is_neg_size = self.append(ir.Compare(ast.Lt(loc=None),
Expand Down Expand Up @@ -992,20 +994,23 @@ def visit_CoerceT(self, node):

def visit_BinOpT(self, node):
if builtins.is_numeric(node.type):
lhs = self.visit(node.left)
rhs = self.visit(node.right)
if isinstance(node.op, (ast.LShift, ast.RShift)):
# Check for negative shift amount.
self._make_check(
self.append(ir.Compare(ast.GtE(loc=None), rhs, ir.Constant(0, rhs.type))),
lambda: self.alloc_exn(builtins.TValueError(),
ir.Constant("shift amount must be nonnegative", builtins.TStr())))
ir.Constant("shift amount must be nonnegative", builtins.TStr())),
loc=node.right.loc)
elif isinstance(node.op, (ast.Div, ast.FloorDiv)):
self._make_check(
self.append(ir.Compare(ast.NotEq(loc=None), rhs, ir.Constant(0, rhs.type))),
lambda: self.alloc_exn(builtins.TZeroDivisionError(),
ir.Constant("cannot divide by zero", builtins.TStr())))
ir.Constant("cannot divide by zero", builtins.TStr())),
loc=node.right.loc)

return self.append(ir.Arith(node.op, self.visit(node.left), rhs))
return self.append(ir.Arith(node.op, lhs, rhs))
elif isinstance(node.op, ast.Add): # list + list, tuple + tuple
lhs, rhs = self.visit(node.left), self.visit(node.right)
if types.is_tuple(node.left.type) and types.is_tuple(node.right.type):
Expand Down

0 comments on commit bdcf7f1

Please sign in to comment.