Browse Source

Turn some state-manipulation primitives into intrinsics

jonathanvdc 8 years ago
parent
commit
0607096020
2 changed files with 65 additions and 5 deletions
  1. 25 4
      kernel/modelverse_jit/intrinsics.py
  2. 40 1
      kernel/modelverse_jit/tree_ir.py

+ 25 - 4
kernel/modelverse_jit/intrinsics.py

@@ -34,14 +34,35 @@ UNARY_INTRINSICS = {
 }
 
 MISC_INTRINSICS = {
-    'element_eq' : (
+    # Reference equality
+    'element_eq' :
         lambda lhs, rhs:
         tree_ir.CreateNodeWithValueInstruction(
-            tree_ir.BinaryInstruction(lhs, '==', rhs))),
-    'element_neq' : (
+            tree_ir.BinaryInstruction(lhs, '==', rhs)),
+    'element_neq' :
         lambda lhs, rhs:
         tree_ir.CreateNodeWithValueInstruction(
-            tree_ir.BinaryInstruction(lhs, '!=', rhs)))
+            tree_ir.BinaryInstruction(lhs, '!=', rhs)),
+
+    # State creation
+    'create_node' : tree_ir.CreateNodeInstruction,
+    'create_edge' : tree_ir.CreateEdgeInstruction,
+    'create_value' :
+        lambda val:
+        tree_ir.CreateNodeWithValueInstruction(
+            tree_ir.ReadValueInstruction(val)),
+
+    # State reads
+    'read_edge_src' :
+        lambda e:
+        tree_ir.LoadIndexInstruction(
+            tree_ir.ReadEdgeInstruction(e),
+            tree_ir.LiteralInstruction(0)),
+    'read_edge_dst' :
+        lambda e:
+        tree_ir.LoadIndexInstruction(
+            tree_ir.ReadEdgeInstruction(e),
+            tree_ir.LiteralInstruction(1))
 }
 
 def register_intrinsics(target_jit):

+ 40 - 1
kernel/modelverse_jit/tree_ir.py

@@ -691,6 +691,25 @@ class ReadDictionaryEdgeInstruction(StateInstruction):
         """Gets this state instruction's argument list."""
         return [self.node_id, self.key]
 
+class ReadEdgeInstruction(StateInstruction):
+    """An instruction that reads an edge."""
+    def __init__(self, node_id):
+        StateInstruction.__init__(self)
+        self.node_id = node_id
+
+    def simplify(self):
+        """Applies basic simplification to this instruction and its children."""
+        return ReadEdgeInstruction(
+            self.node_id.simplify())
+
+    def get_opcode(self):
+        """Gets the opcode for this state instruction."""
+        return "RE"
+
+    def get_arguments(self):
+        """Gets this state instruction's argument list."""
+        return [self.node_id]
+
 class CreateNodeInstruction(StateInstruction):
     """An instruction that creates an empty node."""
 
@@ -720,9 +739,29 @@ class CreateNodeWithValueInstruction(StateInstruction):
         """Gets this state instruction's argument list."""
         return [self.value]
 
+class CreateEdgeInstruction(StateInstruction):
+    """An instruction that creates an edge."""
+    def __init__(self, source_id, target_id):
+        StateInstruction.__init__(self)
+        self.source_id = source_id
+        self.target_id = target_id
+
+    def simplify(self):
+        """Applies basic simplification to this instruction and its children."""
+        return CreateEdgeInstruction(
+            self.source_id.simplify(),
+            self.target_id.simplify())
+
+    def get_opcode(self):
+        """Gets the opcode for this state instruction."""
+        return "CE"
+
+    def get_arguments(self):
+        """Gets this state instruction's argument list."""
+        return [self.source_id, self.target_id]
+
 class CreateDictionaryEdgeInstruction(StateInstruction):
     """An instruction that creates a dictionary edge."""
-
     def __init__(self, source_id, key, target_id):
         StateInstruction.__init__(self)
         self.source_id = source_id