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

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

Multi-threading support for PabloAST / PabloCompiler?. Requires unique LLVM Context / Module for each thread.

File size: 4.7 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#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   
97void re2pablo_compiler(PabloKernel * kernel, const unsigned encodingBits, RE * re_ast, const bool CountOnly) {
98    cc::CC_Compiler cc_compiler(kernel, encodingBits);
99    re::RE_Compiler re_compiler(kernel, cc_compiler, CountOnly);
100    re_compiler.initializeRequiredStreams(encodingBits);
101    re_compiler.compileUnicodeNames(re_ast);
102    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast), AlgorithmOptions.isSet(InvertMatches));
103}
104
105}
Note: See TracBrowser for help on using the repository browser.