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

Last change on this file was 5835, checked in by nmedfort, 6 weeks ago

Revised RE_Minimizer to use alphabets + minor optimizations to RE functions

File size: 5.2 KB
Line 
1/*
2 *  Copyright (c) 2017 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 <toolchain/toolchain.h>
8#include <grep_interface.h>
9#include <re/re_toolchain.h>
10#include <cc/cc_compiler.h>            // for CC_Compiler
11#include <llvm/Support/CommandLine.h>  // for clEnumVal, clEnumValEnd, Optio...
12#include <re/re_compiler.h>            // for RE_Compiler
13#include <re/re_nullable.h>            // for RE_Nullable
14#include <re/re_star_normal.h>         // for RE_Star_Normal
15#include <re/re_simplifier.h>          // for RE_Simplifier
16#include <re/re_minimizer.h>
17#include <re/re_local.h>
18#include <re/printer_re.h>
19#include <re/re_analysis.h>
20#include <re/re_cc.h>
21#include <re/casing.h>
22#include <re/exclude_CC.h>
23#include <re/re_name_resolve.h>
24#include <re/grapheme_clusters.h>
25#include <llvm/Support/raw_ostream.h>
26#include <toolchain/toolchain.h>
27
28using namespace pablo;
29using namespace llvm;
30
31namespace re {
32
33static cl::OptionCategory RegexOptions("Regex Toolchain Options",
34                                              "These options control the regular expression transformation and compilation.");
35const cl::OptionCategory * LLVM_READONLY re_toolchain_flags() {
36    return &RegexOptions;
37}
38
39static cl::bits<RE_PrintFlags> 
40    PrintOptions(cl::values(clEnumVal(ShowREs, "Print parsed or generated regular expressions"),
41                            clEnumVal(ShowAllREs, "Print all regular expression passes"),
42                            clEnumVal(ShowStrippedREs, "Print REs with nullable prefixes/suffixes removed"),
43                            clEnumVal(ShowSimplifiedREs, "Print final simplified REs")
44                            CL_ENUM_VAL_SENTINEL), cl::cat(RegexOptions));
45
46static cl::bits<RE_AlgorithmFlags>
47    AlgorithmOptions(cl::values(clEnumVal(DisableLog2BoundedRepetition, "disable log2 optimizations for bounded repetition of bytes"),
48                              clEnumVal(DisableIfHierarchy, "disable nested if hierarchy for generated Unicode classes (not recommended)"), 
49                              clEnumVal(DisableMatchStar, "disable MatchStar optimization"), 
50                              clEnumVal(DisableUnicodeMatchStar, "disable Unicode MatchStar optimization"),
51                              clEnumVal(DisableUnicodeLineBreak, "disable Unicode line breaks - use LF only")
52                              CL_ENUM_VAL_SENTINEL), cl::cat(RegexOptions));
53
54bool LLVM_READONLY AlgorithmOptionIsSet(RE_AlgorithmFlags flag) {
55    return AlgorithmOptions.isSet(flag);
56}
57
58int IfInsertionGap;
59static cl::opt<int, true> 
60    IfInsertionGapOption("if-insertion-gap",  cl::location(IfInsertionGap), cl::init(3),
61                         cl::desc("minimum number of nonempty elements between inserted if short-circuit tests"), 
62                         cl::cat(RegexOptions));
63
64RE * resolveModesAndExternalSymbols(RE * r) {
65    if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowREs)) {
66        errs() << "Parser:\n" << Printer_RE::PrintRE(r) << '\n';
67    }
68    r = resolveGraphemeMode(r, false /* not in grapheme mode at top level*/);
69    if (PrintOptions.isSet(ShowAllREs)) {
70        errs() << "resolveGraphemeMode:\n" << Printer_RE::PrintRE(r) << '\n';
71    }
72    r = re::resolveUnicodeProperties(r);
73    if (PrintOptions.isSet(ShowAllREs)) {
74        errs() << "resolveUnicodeProperties:\n" << Printer_RE::PrintRE(r) << '\n';
75    }
76    r = resolveCaseInsensitiveMode(r, grep::IgnoreCaseFlag);
77    if (PrintOptions.isSet(ShowAllREs)) {
78        errs() << "resolveCaseInsensitiveMode:\n" << Printer_RE::PrintRE(r) << '\n';
79    }
80    return r;
81}
82
83RE * excludeUnicodeLineBreak(RE * r) {
84    r = exclude_CC(r, re::makeCC(re::makeCC(0x0A, 0x0D), re::makeCC(re::makeCC(0x85), re::makeCC(0x2028, 0x2029))));
85    if (PrintOptions.isSet(ShowAllREs)) {
86        errs() << "excludeUnicodeLineBreak:\n" << Printer_RE::PrintRE(r) << '\n';
87    }
88    return r;
89}
90
91RE * regular_expression_passes(RE * r) {
92
93    //Optimization passes to simplify the AST.
94    r = RE_Nullable::removeNullablePrefix(r);
95    if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowStrippedREs)) {
96        errs() << "RemoveNullablePrefix:\n" << Printer_RE::PrintRE(r) << '\n';
97    }
98    r = RE_Nullable::removeNullableSuffix(r);
99    if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowStrippedREs)) {
100        errs() << "RemoveNullableSuffix:\n" << Printer_RE::PrintRE(r) << '\n';
101    }
102    r = RE_Star_Normal::star_normal(r);
103    if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowSimplifiedREs)) {
104        //Print to the terminal the AST that was transformed to the star normal form.
105        errs() << "Star_Normal_Form:\n" << Printer_RE::PrintRE(r) << '\n';
106    }
107    r = re::resolveNames(r);
108    if (PrintOptions.isSet(ShowAllREs)) {
109        errs() << "Resolve Names:\n" << Printer_RE::PrintRE(r) << '\n';
110    }
111    if (codegen::OptLevel > 1) {
112        r = RE_Minimizer::minimize(r);
113    } else {
114        r = RE_Simplifier::simplify(r);
115    }
116    if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowSimplifiedREs)) {
117        //Print to the terminal the AST that was generated by the simplifier.
118        errs() << "Simplifier:\n" << Printer_RE::PrintRE(r) << '\n';
119    }
120
121    return r;
122}
123
124}
Note: See TracBrowser for help on using the repository browser.