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

Last change on this file since 5206 was 5202, checked in by nmedfort, 3 years ago

Initial work on adding types to PabloAST and mutable Var objects.

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() {
31    return &RegexOptions;
32}
33
34static cl::bits<RE_PrintFlags> 
35    PrintOptions(cl::values(clEnumVal(PrintAllREs, "print regular expression passes"),
36                            clEnumVal(PrintParsedREs, "print out parsed regular expressions"),
37                            clEnumVal(PrintStrippedREs, "print out REs with nullable prefixes/suffixes removed"),
38                            clEnumVal(PrintSimplifiedREs, "print out final simplified REs"),
39                            clEnumValEnd), cl::cat(RegexOptions));
40
41static cl::bits<RE_AlgorithmFlags>
42    AlgorithmOptions(cl::values(clEnumVal(DisableLog2BoundedRepetition, "disable log2 optimizations for bounded repetition of bytes"),
43                              clEnumVal(DisableIfHierarchy, "disable nested if hierarchy for generated Unicode classes (not recommended)"), 
44                              clEnumVal(DisableMatchStar, "disable MatchStar optimization"), 
45                              clEnumVal(DisableUnicodeMatchStar, "disable Unicode MatchStar optimization"),
46                              clEnumVal(DisableUnicodeLineBreak, "disable Unicode line breaks - use LF only"),
47                              clEnumValN(InvertMatches, "v", "select non-matching lines"),
48#ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
49                              clEnumVal(UsePregeneratedUnicode, "use fixed pregenerated Unicode character class sets instead"),
50#endif
51                              clEnumValEnd), 
52                   cl::cat(RegexOptions));
53
54bool 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
64
65
66RE * regular_expression_passes(RE * re_ast)  {
67    if (PrintOptions.isSet(PrintAllREs) || PrintOptions.isSet(PrintParsedREs)) {
68        std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
69    }
70
71    //Optimization passes to simplify the AST.
72    re_ast = re::RE_Nullable::removeNullablePrefix(re_ast);
73    if (PrintOptions.isSet(PrintAllREs) || PrintOptions.isSet(PrintStrippedREs)) {
74        std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
75    }
76    re_ast = re::RE_Nullable::removeNullableSuffix(re_ast);
77    if (PrintOptions.isSet(PrintAllREs) || PrintOptions.isSet(PrintStrippedREs)) {
78        std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
79    }
80    re_ast = re::RE_Nullable::removeNullableAssertion(re_ast);
81    if (PrintOptions.isSet(PrintAllREs) || PrintOptions.isSet(PrintStrippedREs)) {
82        std::cerr << "RemoveNullableAssertion:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
83    }
84    re_ast = re::RE_Nullable::removeNullableAfterAssertion(re_ast);
85    if (PrintOptions.isSet(PrintAllREs) || PrintOptions.isSet(PrintStrippedREs)) {
86        std::cerr << "RemoveNullableAfterAssertion" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
87    }
88   
89    re_ast = re::RE_Simplifier::simplify(re_ast);
90    if (PrintOptions.isSet(PrintAllREs) || PrintOptions.isSet(PrintSimplifiedREs)) {
91        //Print to the terminal the AST that was generated by the simplifier.
92        std::cerr << "Simplifier:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
93    }
94    return re_ast;
95}
96   
97PabloFunction * re2pablo_compiler(const unsigned encodingBits, RE * re_ast, const bool CountOnly) {
98    PabloFunction * function = PabloFunction::Create("process_block");
99    cc::CC_Compiler cc_compiler(*function, encodingBits);
100    re::RE_Compiler re_compiler(*function, cc_compiler, CountOnly);
101    re_compiler.initializeRequiredStreams(encodingBits);
102    re_compiler.compileUnicodeNames(re_ast);
103    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast), AlgorithmOptions.isSet(InvertMatches));
104    return function;
105}
106
107}
Note: See TracBrowser for help on using the repository browser.