changeset 29:b833746d9d92

modify jitgrep.py and change linking method.
author Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
date Fri, 09 Jul 2010 14:08:20 +0900
parents 0e90ae1a2d9b
children ef2928cdbdb6
files src/grep_bench.sh src/grep_translator.py src/jitgrep.py src/template/grep.c src/template/grep.template
diffstat 5 files changed, 40 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
--- a/src/grep_bench.sh	Thu Jul 08 20:02:42 2010 +0900
+++ b/src/grep_bench.sh	Fri Jul 09 14:08:20 2010 +0900
@@ -6,7 +6,7 @@
 cgrepout="/tmp/cgrep.out"
 
 echo "[jitgrep]"
-time ./jitgrep.py -O3 $@ > $jitgrepout
+time ./jitgrep.py $@ > $jitgrepout
 #time /tmp/jitgrep $@ > $jitgrepout
 
 echo "\n[cgrep]"
--- a/src/grep_translator.py	Thu Jul 08 20:02:42 2010 +0900
+++ b/src/grep_translator.py	Fri Jul 09 14:08:20 2010 +0900
@@ -8,6 +8,9 @@
 
 class GREPTranslator(CTranslator):
     """GREPTranslator
+    This Class can translate form DFA into grep source-code.
+    which based on (beautiful) mini-grep introduced  \"The Practice of Programming\"
+    written by Rob Pike & Brian W. Kernighan. (see template/grep.c)
     >>> string = \"(build|fndecl|gcc)\"
     >>> reg = Regexp(string)
     >>> dfacg = CallGraph(reg.dfa)
@@ -35,18 +38,32 @@
 }\n""" % self.funType)
 
     def emit_initialization(self):
-        self.emit("#define NULL s\n\n")
-        self.emit("int DFA(char* s);\n")
+        self.emit("%sFILTER(char* s);\n" % (self.funType))
+        self.emit("%sDFA(char* s);\n" % (self.funType))
+        self.emit("extern int bmhfilter(char* text, char *key);\n")
+        self.emit("extern int grepmain(int argc, char* argv[]);\n")
         for state in self.cg.map.iterkeys():
             self.emit(self.funType + self.modify_state_name(state) + "(char* s);\n")
         self.emit(self.funType + 'accept(char* s);\n')
         self.emit(self.funType + 'reject(char* s);\n')
 
+    def emit_filter(self):
+        self.emit("""
+int FILTER(char *text) {
+""")
+        self.emit("\treturn 1;\n}\n")
+
     def emit_driver(self):
         self.emit("""
-int DFA(char *s) {
+int main(int argc, char* argv[]) {
+\treturn grepmain(argc, argv);
+}
+""")
+        self.emit_filter()
+        self.emit("""
+%sDFA(char *s) {
   return %s(s);
-}\n\n""" % (self.modify_state_name(self.cg.start)))
+}\n\n""" % (self.funType, self.modify_state_name(self.cg.start)))
 
     def emit_state(self, cur_state, transition):
         self.emit(self.funType + self.modify_state_name(cur_state) + "(char* s) {\n")
--- a/src/jitgrep.py	Thu Jul 08 20:02:42 2010 +0900
+++ b/src/jitgrep.py	Fri Jul 09 14:08:20 2010 +0900
@@ -12,8 +12,8 @@
     myusage = "%prog [--time] [--debug] [--cc=compiler] [-Olevel] regexp [file ..]"
     psr = OptionParser(usage=myusage)
 
-    optimize = ""
     redirect = ""
+    optimize = "-O3"
     srcpath = "/tmp/jitgrep_dfa.c"
     binpath = "/tmp/jitgrep"
     libgrep = "template/libgrep.so"
@@ -27,12 +27,14 @@
 
     psr.add_option("--cc", action="store", type="string", dest="compiler", default="gcc", metavar="FILE",
                    help="Choose compiler (default is gcc).")
+    psr.add_option("--CFLAGS", action="store", type="string", dest="cflags", default="-O3", help="Print compile/matching time.")
     psr.add_option("--time", action="store_true", dest="time", default=False, help="Print compile/matching time.")
     psr.add_option("--debug", action="store_true", dest="debug", default=False, help="Dump commands, not evalute matching (except interactive mode).")
     psr.add_option("--out", action="store", type="string", dest="out", default="", metavar="FILE", help="Output file.")
-    psr.add_option("-O", action="store_true", dest="_", help="Optimizing compilation takes somewhat more time, level be recognized a digits [0-9].")
+    psr.add_option("-O", action="store_true", dest="_", help="Optimizing compilation takes somewhat more time, level be recognized a digits [0-9]. (default: -O3)")
 
     (opts, args) = psr.parse_args(argv)
+    libgrep = ".".join([libgrep, opts.compiler])
 
     if len(args) < 2:
         psr.print_usage()
@@ -61,7 +63,7 @@
         print("Translation: " + str(end_time - start_time) + " Sec.")
 
     if not os.path.exists(libgrep):
-        cmd = "gcc -O3 -c -fPIC -shared template/grep.c -o " + libgrep
+        cmd = " ".join([opts.compiler, optimize, opts.cflags, "-c -fPIC -shared template/grep.c -o", libgrep])
         if opts.debug:
             print cmd
         else:
--- a/src/template/grep.c	Thu Jul 08 20:02:42 2010 +0900
+++ b/src/template/grep.c	Fri Jul 09 14:08:20 2010 +0900
@@ -8,39 +8,31 @@
 #define BUFSIZE 1024
 
 extern int DFA(char *text);
+extern int FILTER(char *text);
+int bmhfilter(char *text, char *key);
 
 int match(char *regexp, char *text) {
   if (regexp[0] == '^')
     return DFA(text);
   do {
-    if  (DFA(text)) //(matchhere(regexp+1, text))
+    if  (FILTER(text) && DFA(text))
       return 1;
   } while (*text++ != '\0');
   return 0;
 }
 
-/*
-int matchhere(char *regexp, char *text) {
-  if (regexp[0] == '\0')
-    return 1;
-  if (regexp[1] == '*')
-    return matchstar(regexp[0], regexp+2, text);
-  if (regexp[0] == '$' && regexp[1] == '\0')
-    return *text == '\0';
-  if (*text != '\0' && (regexp[0] == '.' || regexp[0] == *text))
-    return matchhere(regexp+1, text+1);
+int bmhfilter(char *text, char *key) {
+  int i, j;
+  i = j = 0;
+  while (text[i] != '\0' && key[j] != '\0') {
+    if (text[i] == key[j]) { ++i; ++j; }
+    else { i = i - j + 1; j = 0; }
+  }
+  if (key[j] == '\0')
+    return 1; /* position = k - j; */
   return 0;
 }
 
-int matchstar(int c, char *regexp, char *text) {
-  do {
-    if (matchhere(regexp, text))
-      return 1;
-  } while (*text != '\0' && (*text++ == c || c == '.'));
-  return 0;
-}
-*/
-
 int grep(char * regexp, FILE *f, char *name) {
   int n, nmatch;
   char buf[BUFSIZE];
@@ -59,7 +51,7 @@
   return nmatch;
 }
 
-int main(int argc, char* argv[]) {
+int grepmain(int argc, char* argv[]) {
   int i, nmatch;
   FILE *f;
 
--- a/src/template/grep.template	Thu Jul 08 20:02:42 2010 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,43 +0,0 @@
-int grep(FILE *f, char *name) {
-  int n, nmatch;
-  char buf[BUFSIZE];
-  nmatch = 0;
-  while (fgets(buf, sizeof buf, f) != NULL) {
-    n = strlen(buf);
-    if (n > 0 && buf[n-1] == '\n')
-      buf[n-1] = '\0';
-    if (match(buf)) {
-      nmatch++;
-      if (name != NULL)
-        printf("%s:", name);
-      printf("%s\n", buf);
-    }
-  }
-  return nmatch;
-}
-
-int main(int argc, char* argv[]) {
-  int i, nmatch;
-  FILE *f;
-  nmatch = 0;
-  /* for (i = 0; i < argc; printf("%s\n", argv[i++])); */
-  if (argc == 2) {
-    if (match(argv[1])) {
-      printf("%s\n", argv[1]);
-      nmatch++;
-    }
-  } else {
-    for (i = 2; i < argc; i++) {
-      f = fopen(argv[i], "r");
-      if (f == NULL) {
-        fprintf(stderr, "can't open: %s\n", argv[i]);
-        continue;
-      }
-      if (grep(f, argc > 3 ? argv[i] : NULL) > 0)
-        nmatch++;
-      fclose(f);
-    }
-  }
-
-  return nmatch;
-}