py2llvm: add break statement support

This commit is contained in:
Sebastien Bourdeauducq 2014-09-10 16:16:12 +08:00
parent d66448a486
commit 7e889b014d
2 changed files with 9 additions and 0 deletions

View File

@ -41,6 +41,7 @@ class Visitor:
self.env = env self.env = env
self.ns = ns self.ns = ns
self.builder = builder self.builder = builder
self._break_stack = []
# builder can be None for visit_expression # builder can be None for visit_expression
def visit_expression(self, node): def visit_expression(self, node):
@ -206,6 +207,7 @@ class Visitor:
body_block = function.append_basic_block("w_body") body_block = function.append_basic_block("w_body")
else_block = function.append_basic_block("w_else") else_block = function.append_basic_block("w_else")
merge_block = function.append_basic_block("w_merge") merge_block = function.append_basic_block("w_merge")
self._break_stack.append(merge_block)
condition = self.visit_expression(node.test).o_bool(self.builder) condition = self.visit_expression(node.test).o_bool(self.builder)
self.builder.cbranch( self.builder.cbranch(
@ -224,12 +226,14 @@ class Visitor:
self.builder.branch(merge_block) self.builder.branch(merge_block)
self.builder.position_at_end(merge_block) self.builder.position_at_end(merge_block)
self._break_stack.pop()
def _visit_stmt_For(self, node): def _visit_stmt_For(self, node):
function = self.builder.basic_block.function function = self.builder.basic_block.function
body_block = function.append_basic_block("f_body") body_block = function.append_basic_block("f_body")
else_block = function.append_basic_block("f_else") else_block = function.append_basic_block("f_else")
merge_block = function.append_basic_block("f_merge") merge_block = function.append_basic_block("f_merge")
self._break_stack.append(merge_block)
it = self.visit_expression(node.iter) it = self.visit_expression(node.iter)
target = self.visit_expression(node.target) target = self.visit_expression(node.target)
@ -253,6 +257,10 @@ class Visitor:
self.builder.branch(merge_block) self.builder.branch(merge_block)
self.builder.position_at_end(merge_block) self.builder.position_at_end(merge_block)
self._break_stack.pop()
def _visit_stmt_Break(self, node):
self.builder.branch(self._break_stack[-1])
def _visit_stmt_Return(self, node): def _visit_stmt_Return(self, node):
if node.value is None: if node.value is None:

View File

@ -21,6 +21,7 @@ class CompilerTest(AutoContext):
while d*d <= x: while d*d <= x:
if x % d == 0: if x % d == 0:
prime = False prime = False
break
d += 1 d += 1
if prime: if prime:
self.output(x) self.output(x)