Forráskód Böngészése

Define a tuple assignment tree IR node

jonathanvdc 8 éve
szülő
commit
2bfe0c2625
1 módosított fájl, 51 hozzáadás és 0 törlés
  1. 51 0
      kernel/modelverse_jit/tree_ir.py

+ 51 - 0
kernel/modelverse_jit/tree_ir.py

@@ -969,6 +969,57 @@ class StoreLocalInstruction(LocalInstruction):
     def __repr__(self):
         return 'StoreLocalInstruction(%r, %r)' % (self.name, self.value)
 
+class TupleStoreLocalInstruction(VoidInstruction):
+    """Assigns a number of values to a number of variables."""
+    def __init__(self, name_value_pairs):
+        VoidInstruction.__init__(self)
+        self.name_value_pairs = name_value_pairs
+
+    def get_children(self):
+        """Gets this instruction's sequence of child instructions."""
+        return [val for _, val in self.name_value_pairs]
+
+    def has_result_temporary(self):
+        """Tells if this instruction stores its result in a temporary."""
+        return False
+
+    def create(self, new_children):
+        """Creates a new instruction of this type from the given sequence of child instructions."""
+        new_name_value_pairs = [
+            (name, new_value) for (name, _), new_value in zip(self.name_value_pairs, new_children)]
+        return TupleStoreLocalInstruction(new_name_value_pairs)
+
+    def generate_python_def(self, code_generator):
+        """Generates a Python statement that executes this instruction.
+           The statement is appended immediately to the code generator."""
+        tuple_lhs = []
+        tuple_rhs = []
+        for name, value in self.name_value_pairs:
+            if isinstance(name, str) or isinstance(name, unicode) or name is None:
+                variable = VariableName(name)
+            else:
+                variable = name
+
+            # Retrieve the result name for the variable.
+            var_result_name = code_generator.get_result_name(variable)
+            # Encourage the value to take on the same result name as the variable.
+            value_result_name = code_generator.get_result_name(value, var_result_name)
+            if value.has_definition():
+                # Generate the value' definition.
+                value.generate_python_def(code_generator)
+
+            # Only perform an assignment if it's truly necessary.
+            if var_result_name != value_result_name or not value.has_result_temporary():
+                tuple_lhs.append(var_result_name)
+                tuple_rhs.append(value.generate_python_use(code_generator))
+
+        if len(tuple_lhs) > 0:
+            code_generator.append_line(
+                '%s = %s' % (', '.join(tuple_lhs), ', '.join(tuple_rhs)))
+
+    def __repr__(self):
+        return 'TupleStoreLocalInstruction(%r)' % (self.name_value_pairs)
+
 class LoadLocalInstruction(LocalInstruction):
     """An instruction that loads a value from a local variable."""
     def has_definition_impl(self):