瀏覽代碼

Support 'while' in the JIT

jonathanvdc 8 年之前
父節點
當前提交
39ae0c54ca
共有 1 個文件被更改,包括 23 次插入1 次删除
  1. 23 1
      kernel/modelverse_jit/jit.py

+ 23 - 1
kernel/modelverse_jit/jit.py

@@ -151,15 +151,37 @@ class AnalysisState(object):
                     true_r,
                     false_r))
 
+    def analyze_while(self, instruction_id):
+        """Tries to analyze the given 'while' instruction."""
+        cond, body = yield [
+            ("RD", [instruction_id, "cond"]),
+            ("RD", [instruction_id, "body"])]
+
+        gen = self.analyze_all([cond, body])
+        try:
+            inp = None
+            while True:
+                inp = yield gen.send(inp)
+        except primitive_functions.PrimitiveFinished as e:
+            cond_r, body_r = e.result
+            raise primitive_functions.PrimitiveFinished(
+                tree_ir.LoopInstruction(
+                    tree_ir.CompoundInstruction(
+                        tree_ir.SelectInstruction(
+                            tree_ir.ReadValueInstruction(cond_r),
+                            tree_ir.BreakInstruction(),
+                            tree_ir.EmptyInstruction()),
+                        body_r)))
+
     def analyze_constant(self, instruction_id):
         """Tries to analyze the given 'constant' (literal) instruction."""
-
         node_id, = yield [("RD", [instruction_id, "node"])]
         raise primitive_functions.PrimitiveFinished(
             tree_ir.LiteralInstruction(node_id))
 
     instruction_analyzers = {
         'if' : analyze_if,
+        'while' : analyze_while,
         'return' : analyze_return,
         'constant' : analyze_constant
     }