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

Last change on this file was 5646, checked in by nmedfort, 4 days ago

Minor clean up. Bug fix for object cache when the same cached kernel is used twice in a single run. Improvement to RE Minimizer.

File size: 5.0 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_minimizer.h>
15#include <re/re_local.h>
16#include <re/printer_re.h>
17#include <re/re_analysis.h>
18#include <llvm/Support/raw_ostream.h>
19
20using namespace pablo;
21using namespace llvm;
22
23namespace re {
24
25static cl::OptionCategory RegexOptions("Regex Toolchain Options",
26                                              "These options control the regular expression transformation and compilation.");
27const cl::OptionCategory * re_toolchain_flags() {
28    return &RegexOptions;
29}
30
31static cl::bits<RE_PrintFlags> 
32    PrintOptions(cl::values(clEnumVal(ShowREs, "Print parsed or generated regular expressions"),
33                            clEnumVal(ShowAllREs, "Print all regular expression passes"),
34                            clEnumVal(ShowStrippedREs, "Print REs with nullable prefixes/suffixes removed"),
35                            clEnumVal(ShowSimplifiedREs, "Print final simplified REs"),
36                            clEnumValEnd), cl::cat(RegexOptions));
37
38static cl::bits<RE_AlgorithmFlags>
39    AlgorithmOptions(cl::values(clEnumVal(DisableLog2BoundedRepetition, "disable log2 optimizations for bounded repetition of bytes"),
40                              clEnumVal(DisableIfHierarchy, "disable nested if hierarchy for generated Unicode classes (not recommended)"), 
41                              clEnumVal(DisableMatchStar, "disable MatchStar optimization"), 
42                              clEnumVal(DisableUnicodeMatchStar, "disable Unicode MatchStar optimization"),
43                              clEnumVal(DisableUnicodeLineBreak, "disable Unicode line breaks - use LF only"),
44                              clEnumValEnd), 
45                   cl::cat(RegexOptions));
46
47bool AlgorithmOptionIsSet(RE_AlgorithmFlags flag) {
48    return AlgorithmOptions.isSet(flag);
49}
50
51int IfInsertionGap;
52static cl::opt<int, true> 
53    IfInsertionGapOption("if-insertion-gap",  cl::location(IfInsertionGap), cl::init(3),
54                         cl::desc("minimum number of nonempty elements between inserted if short-circuit tests"), 
55                         cl::cat(RegexOptions));
56
57
58
59RE * regular_expression_passes(RE * re)  {
60
61    if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowREs)) {
62        errs() << "Parser:\n" << Printer_RE::PrintRE(re) << '\n';
63    }
64
65    //Optimization passes to simplify the AST.
66    re = RE_Nullable::removeNullablePrefix(re);
67    if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowStrippedREs)) {
68        errs() << "RemoveNullablePrefix:\n" << Printer_RE::PrintRE(re) << '\n';
69    }
70    re = RE_Nullable::removeNullableSuffix(re);
71    if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowStrippedREs)) {
72        errs() << "RemoveNullableSuffix:\n" << Printer_RE::PrintRE(re) << '\n';
73    }
74    re = RE_Nullable::removeNullableAssertion(re);
75    if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowStrippedREs)) {
76        errs() << "RemoveNullableAssertion:\n" << Printer_RE::PrintRE(re) << '\n';
77    }
78    //re = RE_Nullable::removeNullableAfterAssertion(re);
79    //if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowStrippedREs)) {
80    //    errs() << "RemoveNullableAfterAssertion\n" << Printer_RE::PrintRE(re) << '\n';
81    //}
82
83    re = RE_Simplifier::simplify(re);
84
85    if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowSimplifiedREs)) {
86        //Print to the terminal the AST that was generated by the simplifier.
87        errs() << "Simplifier:\n" << Printer_RE::PrintRE(re) << '\n';
88    }
89   
90//    re = RE_Minimizer::minimize(re);
91
92//    if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowSimplifiedREs)) {
93//        //Print to the terminal the AST that was generated by the simplifier.
94//        errs() << "Minimizer:\n" << Printer_RE::PrintRE(re) << '\n';
95//    }
96
97    re = RE_Star_Normal::star_normal(re);
98
99    if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowSimplifiedREs)) {
100        //Print to the terminal the AST that was transformed to the star normal form.
101        errs() << "Star_Normal_Form:\n" << Printer_RE::PrintRE(re) << '\n';
102    }
103
104    return re;
105}
106   
107PabloAST * re2pablo_compiler(PabloKernel * kernel, RE * re_ast) {
108    Var * const basis = kernel->getInputStreamVar("basis");
109    const bool local = RE_Local::isLocalLanguage(re_ast) && isTypeForLocal(re_ast);
110    cc::CC_Compiler cc_compiler(kernel, basis);
111    RE_Compiler re_compiler(kernel, cc_compiler, local);
112    re_ast = re_compiler.compileUnicodeNames(re_ast);
113    return re_compiler.compile(re_ast);
114}
115
116}
Note: See TracBrowser for help on using the repository browser.