瀏覽代碼

Refactor parts of bytecode_to_tree.py

jonathanvdc 8 年之前
父節點
當前提交
68a6dc7d25
共有 1 個文件被更改,包括 42 次插入37 次删除
  1. 42 37
      kernel/modelverse_jit/bytecode_to_tree.py

+ 42 - 37
kernel/modelverse_jit/bytecode_to_tree.py

@@ -31,6 +31,45 @@ def apply_intrinsic(intrinsic_function, named_args):
             tree_ir.create_block(*store_instructions),
             intrinsic_function(**arg_value_dict))
 
+def create_access(pointer):
+    """Creates a tree that loads the given pointer's value."""
+    # Accessing a variable is pretty easy. It really just boils
+    # down to reading the value corresponding to the 'value' key
+    # of the variable.
+    #
+    #     value, = yield [("RD", [returnvalue, "value"])]
+    #
+    return tree_ir.ReadDictionaryValueInstruction(
+        pointer,
+        tree_ir.LiteralInstruction('value'))
+
+def create_assign(pointer, value):
+    """Creates a tree that assigns the given value to the given pointer."""
+    # Assignments work like this:
+    #
+    #     value_link = yield [("RDE", [variable, "value"])]
+    #     _, _ =       yield [("CD", [variable, "value", value]),
+    #                         ("DE", [value_link])]
+    #
+    variable = tree_ir.StoreLocalInstruction(None, pointer)
+    value = tree_ir.StoreLocalInstruction(None, value)
+    value_link = tree_ir.StoreLocalInstruction(
+        'value_link',
+        tree_ir.ReadDictionaryEdgeInstruction(
+            variable.create_load(),
+            tree_ir.LiteralInstruction('value')))
+
+    return tree_ir.create_block(
+        variable,
+        value,
+        value_link,
+        tree_ir.CreateDictionaryEdgeInstruction(
+            variable.create_load(),
+            tree_ir.LiteralInstruction('value'),
+            value.create_load()),
+        tree_ir.DeleteEdgeInstruction(
+            value_link.create_load()))
+
 class LocalNameMap(object):
     """A map that converts local variable nodes to identifiers."""
     def __init__(self, local_mapping=None):
@@ -416,7 +455,7 @@ class AnalysisState(object):
 
     def analyze_global(self, instruction):
         """Tries to analyze the given 'global' (declaration) instruction."""
-        # To resolve a variable, we'll do something along the
+        # To declare a variable, we'll do something along the
         # lines of:
         #
         #     _globals, = yield [("RD", [task_root, "globals"])]
@@ -468,46 +507,12 @@ class AnalysisState(object):
         (var_r, value_r), = yield [
             ("CALL_ARGS", [self.analyze_all, ([instruction.pointer, instruction.value],)])]
 
-        # Assignments work like this:
-        #
-        #     value_link = yield [("RDE", [variable, "value"])]
-        #     _, _ =       yield [("CD", [variable, "value", value]),
-        #                         ("DE", [value_link])]
-
-        variable = tree_ir.StoreLocalInstruction(None, var_r)
-        value = tree_ir.StoreLocalInstruction(None, value_r)
-        value_link = tree_ir.StoreLocalInstruction(
-            'value_link',
-            tree_ir.ReadDictionaryEdgeInstruction(
-                variable.create_load(),
-                tree_ir.LiteralInstruction('value')))
-
-        raise primitive_functions.PrimitiveFinished(
-            tree_ir.create_block(
-                variable,
-                value,
-                value_link,
-                tree_ir.CreateDictionaryEdgeInstruction(
-                    variable.create_load(),
-                    tree_ir.LiteralInstruction('value'),
-                    value.create_load()),
-                tree_ir.DeleteEdgeInstruction(
-                    value_link.create_load())))
+        raise primitive_functions.PrimitiveFinished(create_assign(var_r, value_r))
 
     def analyze_access(self, instruction):
         """Tries to analyze the given 'access' instruction."""
         var_r, = yield [("CALL_ARGS", [self.analyze, (instruction.pointer,)])]
-
-        # Accessing a variable is pretty easy. It really just boils
-        # down to reading the value corresponding to the 'value' key
-        # of the variable.
-        #
-        #     value, = yield [("RD", [returnvalue, "value"])]
-
-        raise primitive_functions.PrimitiveFinished(
-            tree_ir.ReadDictionaryValueInstruction(
-                var_r,
-                tree_ir.LiteralInstruction('value')))
+        raise primitive_functions.PrimitiveFinished(create_access(var_r))
 
     def analyze_direct_call(self, callee_id, callee_name, argument_list):
         """Tries to analyze a direct 'call' instruction."""