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

Last change on this file since 5181 was 5162, checked in by cameron, 3 years ago

Eliminate unnecessary and broken include

File size: 4.8 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 <string>
8#include <iostream>
9#include <fstream>
10
11#include <re/re_toolchain.h>
12#include <re/re_cc.h>
13#include <re/re_nullable.h>
14#include <re/re_simplifier.h>
15#include <re/re_alt.h>
16#include <re/re_parser.h>
17#include <re/re_compiler.h>
18#include <utf8_encoder.h>
19#include <cc/cc_compiler.h>
20#include <pablo/function.h>
21#include <re/printer_re.h>
22#include <llvm/Support/CommandLine.h>
23
24
25using namespace pablo;
26namespace re {
27
28static cl::OptionCategory RegexOptions("Regex Toolchain Options",
29                                              "These options control the regular expression transformation and compilation.");
30const cl::OptionCategory * re_toolchain_flags() {return &RegexOptions;};
31
32static cl::bits<RE_PrintFlags> 
33    PrintOptions(cl::values(clEnumVal(PrintAllREs, "print regular expression passes"),
34                            clEnumVal(PrintParsedREs, "print out parsed regular expressions"),
35                            clEnumVal(PrintStrippedREs, "print out REs with nullable prefixes/suffixes removed"),
36                            clEnumVal(PrintSimplifiedREs, "print out 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                              clEnumValN(InvertMatches, "v", "select non-matching lines"),
46#ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
47                              clEnumVal(UsePregeneratedUnicode, "use fixed pregenerated Unicode character class sets instead"),
48#endif
49                              clEnumValEnd), 
50                   cl::cat(RegexOptions));
51
52bool AlgorithmOptionIsSet(RE_AlgorithmFlags flag) {
53    return AlgorithmOptions.isSet(flag);
54}
55
56int IfInsertionGap;
57static cl::opt<int, true> 
58    IfInsertionGapOption("if-insertion-gap",  cl::location(IfInsertionGap), cl::init(3),
59                         cl::desc("minimum number of nonempty elements between inserted if short-circuit tests"), 
60                         cl::cat(RegexOptions));
61
62
63
64RE * regular_expression_passes(RE * re_ast)  {
65    if (PrintOptions.isSet(PrintAllREs) || PrintOptions.isSet(PrintParsedREs)) {
66        std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
67    }
68
69    //Optimization passes to simplify the AST.
70    re_ast = re::RE_Nullable::removeNullablePrefix(re_ast);
71    if (PrintOptions.isSet(PrintAllREs) || PrintOptions.isSet(PrintStrippedREs)) {
72        std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
73    }
74    re_ast = re::RE_Nullable::removeNullableSuffix(re_ast);
75    if (PrintOptions.isSet(PrintAllREs) || PrintOptions.isSet(PrintStrippedREs)) {
76        std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
77    }
78    re_ast = re::RE_Nullable::removeNullableAssertion(re_ast);
79    if (PrintOptions.isSet(PrintAllREs) || PrintOptions.isSet(PrintStrippedREs)) {
80        std::cerr << "RemoveNullableAssertion:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
81    }
82    re_ast = re::RE_Nullable::removeNullableAfterAssertion(re_ast);
83    if (PrintOptions.isSet(PrintAllREs) || PrintOptions.isSet(PrintStrippedREs)) {
84        std::cerr << "RemoveNullableAfterAssertion" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
85    }
86   
87    re_ast = re::RE_Simplifier::simplify(re_ast);
88    if (PrintOptions.isSet(PrintAllREs) || PrintOptions.isSet(PrintSimplifiedREs)) {
89        //Print to the terminal the AST that was generated by the simplifier.
90        std::cerr << "Simplifier:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
91    }
92    return re_ast;
93}
94   
95PabloFunction * re2pablo_compiler(const unsigned encodingBits, RE * re_ast, bool CountOnly) {
96    PabloFunction * function = PabloFunction::Create("process_block", encodingBits, CountOnly ? 0 : 2);
97    cc::CC_Compiler cc_compiler(*function, encodingBits);
98    re::RE_Compiler re_compiler(*function, cc_compiler, CountOnly);
99    re_compiler.initializeRequiredStreams(encodingBits);
100    re_compiler.compileUnicodeNames(re_ast);
101    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast), AlgorithmOptions.isSet(InvertMatches));
102    return function;
103}
104}
Note: See TracBrowser for help on using the repository browser.