Pārlūkot izejas kodu

Make 'string_startswith' an intrinsic

jonathanvdc 8 gadi atpakaļ
vecāks
revīzija
6a13cd7bc2

+ 8 - 0
kernel/modelverse_jit/intrinsics.py

@@ -68,6 +68,14 @@ MISC_INTRINSICS = {
                 tree_ir.CallInstruction(
                     tree_ir.LoadGlobalInstruction('str'),
                     [tree_ir.ReadValueInstruction(b)]))),
+    'string_startswith' :
+        lambda a, b:
+        tree_ir.CreateNodeWithValueInstruction(
+            tree_ir.CallInstruction(
+                tree_ir.LoadMemberInstruction(
+                    tree_ir.ReadValueInstruction(a),
+                    'startswith'),
+                [tree_ir.ReadValueInstruction(b)])),
 
     # State creation
     'create_node' : tree_ir.CreateNodeInstruction,

+ 29 - 0
kernel/modelverse_jit/tree_ir.py

@@ -614,6 +614,10 @@ class LoadIndexInstruction(Instruction):
         """Tells if this instruction requires a definition."""
         return False
 
+    def simplify(self):
+        return LoadIndexInstruction(
+            self.indexed.simplify(), self.key.simplify())
+
     def generate_python_use(self, code_generator):
         """Generates a Python expression that retrieves this instruction's
            result. The expression is returned as a string."""
@@ -627,6 +631,31 @@ class LoadIndexInstruction(Instruction):
             self.indexed.generate_python_use(code_generator),
             self.key.generate_python_use(code_generator))
 
+class LoadMemberInstruction(Instruction):
+    """An instruction that produces a value by loading a member from a container."""
+    def __init__(self, container, member_name):
+        Instruction.__init__(self)
+        self.container = container
+        self.member_name = member_name
+
+    def has_definition(self):
+        """Tells if this instruction requires a definition."""
+        return False
+
+    def simplify(self):
+        return LoadMemberInstruction(
+            self.container.simplify(), self.member_name)
+
+    def generate_python_use(self, code_generator):
+        """Generates a Python expression that retrieves this instruction's
+           result. The expression is returned as a string."""
+        if self.container.has_definition():
+            self.container.generate_python_def(code_generator)
+
+        return "%s.%s" % (
+            self.container.generate_python_use(code_generator),
+            self.member_name)
+
 class NopInstruction(Instruction):
     """A nop instruction, which allows for the kernel's thread of execution to be interrupted."""