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

Last change on this file since 5630 was 5630, checked in by nmedfort, 2 years ago

Partial check-in for avoidance of compiling Pablo/LLVM code to determine the Kernel struct type when using a cached object. Inactive RE alternation minimization check in.

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