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

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

Modified memory alignment mechanism for GetPropertyValueGrepString? + misc. changes.

File size: 4.6 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/pablo_kernel.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                              clEnumValEnd), 
49                   cl::cat(RegexOptions));
50
51bool AlgorithmOptionIsSet(RE_AlgorithmFlags flag) {
52    return AlgorithmOptions.isSet(flag);
53}
54
55int IfInsertionGap;
56static cl::opt<int, true> 
57    IfInsertionGapOption("if-insertion-gap",  cl::location(IfInsertionGap), cl::init(3),
58                         cl::desc("minimum number of nonempty elements between inserted if short-circuit tests"), 
59                         cl::cat(RegexOptions));
60
61
62
63RE * regular_expression_passes(RE * re_ast)  {
64    if (PrintOptions.isSet(PrintAllREs) || PrintOptions.isSet(PrintParsedREs)) {
65        std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
66    }
67
68    //Optimization passes to simplify the AST.
69    re_ast = re::RE_Nullable::removeNullablePrefix(re_ast);
70    if (PrintOptions.isSet(PrintAllREs) || PrintOptions.isSet(PrintStrippedREs)) {
71        std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
72    }
73    re_ast = re::RE_Nullable::removeNullableSuffix(re_ast);
74    if (PrintOptions.isSet(PrintAllREs) || PrintOptions.isSet(PrintStrippedREs)) {
75        std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
76    }
77    re_ast = re::RE_Nullable::removeNullableAssertion(re_ast);
78    if (PrintOptions.isSet(PrintAllREs) || PrintOptions.isSet(PrintStrippedREs)) {
79        std::cerr << "RemoveNullableAssertion:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
80    }
81    re_ast = re::RE_Nullable::removeNullableAfterAssertion(re_ast);
82    if (PrintOptions.isSet(PrintAllREs) || PrintOptions.isSet(PrintStrippedREs)) {
83        std::cerr << "RemoveNullableAfterAssertion" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
84    }
85   
86    re_ast = re::RE_Simplifier::simplify(re_ast);
87    if (PrintOptions.isSet(PrintAllREs) || PrintOptions.isSet(PrintSimplifiedREs)) {
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    return re_ast;
92}
93   
94void re2pablo_compiler(PabloKernel * kernel, const unsigned encodingBits, RE * re_ast, const bool CountOnly) {
95    cc::CC_Compiler cc_compiler(kernel, encodingBits);
96    re::RE_Compiler re_compiler(kernel, cc_compiler, CountOnly);
97    re_compiler.initializeRequiredStreams(encodingBits);
98    re_compiler.compileUnicodeNames(re_ast);
99    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast), AlgorithmOptions.isSet(InvertMatches));
100}
101
102}
Note: See TracBrowser for help on using the repository browser.