source: icGREP/icgrep-devel/icgrep/re/re_toolchain.cpp @ 5617

Last change on this file since 5617 was 5617, checked in by xuedongx, 20 months ago

new RE compiler pipeline for local language(enlightened by Glushkov automaton)

File size: 4.9 KB
Line 
1/*
2 *  Copyright (c) 2015 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#include <re/re_toolchain.h>
8#include <cc/cc_compiler.h>            // for CC_Compiler
9#include <llvm/Support/CommandLine.h>  // for clEnumVal, clEnumValEnd, Optio...
10#include <re/re_compiler.h>            // for RE_Compiler
11#include <re/re_nullable.h>            // for RE_Nullable
12#include <re/re_star_normal.h>         // for RE_Star_Normal
13#include <re/re_simplifier.h>          // for RE_Simplifier
14#include <re/re_local.h>
15#include <re/printer_re.h>
16#include <re/re_analysis.h>
17#include <iostream>
18
19using namespace pablo;
20using namespace llvm;
21
22namespace re {
23
24static cl::OptionCategory RegexOptions("Regex Toolchain Options",
25                                              "These options control the regular expression transformation and compilation.");
26const cl::OptionCategory * re_toolchain_flags() {
27    return &RegexOptions;
28}
29
30static cl::bits<RE_PrintFlags> 
31    PrintOptions(cl::values(clEnumVal(ShowREs, "Print parsed or generated regular expressions"),
32                            clEnumVal(ShowAllREs, "Print all regular expression passes"),
33                            clEnumVal(ShowStrippedREs, "Print REs with nullable prefixes/suffixes removed"),
34                            clEnumVal(ShowSimplifiedREs, "Print final simplified REs"),
35                            clEnumValEnd), cl::cat(RegexOptions));
36
37static cl::bits<RE_AlgorithmFlags>
38    AlgorithmOptions(cl::values(clEnumVal(DisableLog2BoundedRepetition, "disable log2 optimizations for bounded repetition of bytes"),
39                              clEnumVal(DisableIfHierarchy, "disable nested if hierarchy for generated Unicode classes (not recommended)"), 
40                              clEnumVal(DisableMatchStar, "disable MatchStar optimization"), 
41                              clEnumVal(DisableUnicodeMatchStar, "disable Unicode MatchStar optimization"),
42                              clEnumVal(DisableUnicodeLineBreak, "disable Unicode line breaks - use LF only"),
43                              clEnumValEnd), 
44                   cl::cat(RegexOptions));
45
46bool AlgorithmOptionIsSet(RE_AlgorithmFlags flag) {
47    return AlgorithmOptions.isSet(flag);
48}
49
50int IfInsertionGap;
51static cl::opt<int, true> 
52    IfInsertionGapOption("if-insertion-gap",  cl::location(IfInsertionGap), cl::init(3),
53                         cl::desc("minimum number of nonempty elements between inserted if short-circuit tests"), 
54                         cl::cat(RegexOptions));
55
56
57
58RE * regular_expression_passes(RE * re_ast)  {
59    if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowREs)) {
60        std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
61    }
62
63    //Optimization passes to simplify the AST.
64    re_ast = re::RE_Nullable::removeNullablePrefix(re_ast);
65    if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowStrippedREs)) {
66        std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
67    }
68    re_ast = re::RE_Nullable::removeNullableSuffix(re_ast);
69    if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowStrippedREs)) {
70        std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
71    }
72    re_ast = re::RE_Nullable::removeNullableAssertion(re_ast);
73    if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowStrippedREs)) {
74        std::cerr << "RemoveNullableAssertion:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
75    }
76    //re_ast = re::RE_Nullable::removeNullableAfterAssertion(re_ast);
77    //if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowStrippedREs)) {
78    //    std::cerr << "RemoveNullableAfterAssertion" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
79    //}
80   
81    re_ast = re::RE_Simplifier::simplify(re_ast);
82    if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowSimplifiedREs)) {
83        //Print to the terminal the AST that was generated by the simplifier.
84        std::cerr << "Simplifier:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
85    }
86
87    re_ast = re::RE_Star_Normal::star_normal(re_ast);
88    if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowSimplifiedREs)) {
89        //Print to the terminal the AST that was transformed to the star normal form.
90        std::cerr << "Star_Normal_Form:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
91    }   
92    return re_ast;
93}
94   
95void re2pablo_compiler(PabloKernel * kernel, RE * re_ast) {
96    Var * const basis = kernel->getInputStreamVar("basis");
97    bool local = RE_Local::isLocalLanguage(re_ast) && isTypeForLocal(re_ast);
98    cc::CC_Compiler cc_compiler(kernel, basis);
99    re::RE_Compiler re_compiler(kernel, cc_compiler, local);
100    re_compiler.compileUnicodeNames(re_ast);
101    re_compiler.compile(re_ast);
102}
103
104}
Note: See TracBrowser for help on using the repository browser.