changeset 108:2632b963e441

modify llvm*.
author Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
date Thu, 30 Dec 2010 17:18:40 +0900
parents 492daa4d7fa5
children d591da6e2988
files pyrect/llgrep.py pyrect/regexp/nfa.py pyrect/translator/llvm_grep_translator.py pyrect/translator/llvm_translator.py
diffstat 4 files changed, 23 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/pyrect/llgrep.py	Tue Dec 21 00:44:53 2010 +0900
+++ b/pyrect/llgrep.py	Thu Dec 30 17:18:40 2010 +0900
@@ -5,8 +5,8 @@
 import re
 import time
 from optparse import OptionParser
-from llvm_grep_translator import LLVMGREPTranslator
-from regexp import Regexp
+from pyrect.translator import LLVMGREPTranslator
+from pyrect.regexp import Regexp
 
 def main(argv):
     myusage = """%prog [--time] [--dump]
--- a/pyrect/regexp/nfa.py	Tue Dec 21 00:44:53 2010 +0900
+++ b/pyrect/regexp/nfa.py	Thu Dec 30 17:18:40 2010 +0900
@@ -77,16 +77,16 @@
         self.states = copy.deepcopy(nfa.states)
         self.transition = self.nfa_trans(nfa)
         start = set(self.states)-set([self.start])
-        if self.map.has_key((self.start, '')):
-            self.map[(self.start, '')].add(start)
+        if  self.transition.has_key(self.start):
+            self.transition[(self.start, '')].add(start)
         else:
-            self.map[(self.start, '')] = start
+            self.transition[(self.start, '')] = start
 
     def nfa_trans(self, fa):
         if type(fa) == NFA:
             return copy.deepcopy(fa.transition)
         ntrans = dict()
-        for k, v in fa.map.itertrans():
+        for k, v in fa.transition.itertrans():
             ntrans[k] = set([v])
         return ntrans
 
@@ -111,7 +111,7 @@
                 if n in self.pdfa.accepts:
                     self.accepts.add(nind)
                     self.acceptable[nind].add(s)
-                self.map[(sind, i)] = set((nind,))
+                self.transition[(sind, i)] = set((nind,))
 
         starts = set()
         for i in range(1, len(states)):
--- a/pyrect/translator/llvm_grep_translator.py	Tue Dec 21 00:44:53 2010 +0900
+++ b/pyrect/translator/llvm_grep_translator.py	Thu Dec 30 17:18:40 2010 +0900
@@ -1,5 +1,6 @@
 #!/usr/bin/env python
 
+import os
 from llvm.core import *
 from llvm.passes import *
 from llvm.ee import *
@@ -19,16 +20,18 @@
     True
     """
 
+    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
+
     def __init__(self, regexp):
         LLVMTranslator.__init__(self, regexp)
-        llfile = file("template/grep.ll")
+        llfile = file(self.BASE_DIR + "/template/grep.ll")
         self.llvm_module = Module.from_assembly(llfile)
         self.compiled = False
         self.string = regexp.regexp
         self.args = []
 
     def state_name(self, state_name):
-        return state_name
+        return str(state_name)
 
     def emit_driver(self):
         self.regexp_str = self.new_str_const(self.string)
@@ -36,7 +39,7 @@
             Type.function(self.int_t, (self.charptr_t,)), "DFA")
         dfa_entry = dfa.append_basic_block("entry")
         emit = Builder.new(dfa_entry)
-        ret = emit.call(self.llvm_module.get_function_named(self.cg.start)
+        ret = emit.call(self.llvm_module.get_function_named(self.fa.start)
                         ,(dfa.args[0],))
         emit.ret(ret)
 
@@ -82,9 +85,9 @@
         state_ref["reject"] = reject_state
 
         # add state to module, (as function or label).
-        for state in self.cg.map.iterkeys():
+        for state in self.fa.transition.iterkeys():
             fun = self.llvm_module.add_function(
-                Type.function(self.int_t, (self.charptr_t,)), state)
+                Type.function(self.int_t, (self.charptr_t,)), self.state_name(state))
             optional_func_decl(fun)
             state_ref[state] = fun
 
@@ -95,12 +98,12 @@
         emit = Builder.new(reject_state.append_basic_block("entry"))
         emit.ret(self.const_zero)
 
-        for state, transition in self.cg.map.iteritems():
+        for state, transition in self.fa.transition.iteritems():
             cases = dict()
             state_fun = state_ref[state]
             emit = Builder.new(state_fun.append_basic_block("entry"))
 
-            if state in self.cg.accepts:
+            if state in self.fa.accepts:
                 ret = emit.call(accept_state, (state_fun.args[0],))
                 emit.ret(ret)
                 continue
--- a/pyrect/translator/llvm_translator.py	Tue Dec 21 00:44:53 2010 +0900
+++ b/pyrect/translator/llvm_translator.py	Thu Dec 30 17:18:40 2010 +0900
@@ -32,7 +32,8 @@
         self.optimize = False
         self.debug = False
         self.string = "ABC"
-        self.llvm_module = Module.new(self.cg.type)
+        self.fa = regexp.dfa
+        self.llvm_module = Module.new("DFA")
         self.compiled = False
 
     def emit_driver(self):
@@ -42,7 +43,7 @@
         main_entry = main.append_basic_block("entry")
 
         emit = Builder.new(main_entry)
-        start = self.llvm_module.get_function_named(self.cg.start)
+        start = self.llvm_module.get_function_named(self.fa.start)
         ret = emit.call(start, (main.args[0],))
         emit.ret(ret)
         self.main = main
@@ -72,7 +73,7 @@
         state_ref["reject"] = reject_state
 
         # add state to module, (as function or label).
-        for state in self.cg.map.iterkeys():
+        for state in self.fa.transition.iterkeys():
             fun = self.llvm_module.add_function(
                 Type.function(self.int_t, (self.int_t,)), state)
             optional_func_decl(fun)
@@ -87,9 +88,9 @@
         if self.debug: self.emit_call_printf(emit, "%s does not match regexp\n", self.gep_first(emit, self.matchp_str))
         emit.ret(self.const_zero)
 
-        for state, transition in self.cg.map.iteritems():
+        for state, transition in self.fa.transition.iteritems():
             cases = dict()
-            if state in self.cg.accepts:
+            if state in self.fa.accepts:
                 transition['\\0'] = ["accept"]
             for case, next_states in transition.iteritems():
                 cases[self.char_const(case)] = state_ref[next_states[0]]