source: icGREP/icgrep-devel/icgrep/compiler.cpp @ 4383

Last change on this file since 4383 was 4383, checked in by cameron, 4 years ago

Bug fix to resolve issue with empty maps

File size: 7.7 KB
Line 
1/*
2 *  Copyright (c) 2014 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/*
8 *  Copyright (c) 2014 International Characters.
9 *  This software is licensed to the public under the Open Software License 3.0.
10 *  icgrep is a trademark of International Characters.
11 */
12
13#include <compiler.h>
14#include <re/re_cc.h>
15#include <re/re_nullable.h>
16#include <re/re_simplifier.h>
17#include <re/re_alt.h>
18#include <re/parsefailure.h>
19#include <re/re_parser.h>
20#include <re/re_compiler.h>
21#include <utf8_encoder.h>
22#include <cc/cc_compiler.h>
23#include <cc/cc_namemap.hpp>
24#include <pablo/pablo_compiler.h>
25#include <pablo/analysis/useanalysis.h>
26
27#include "unicode_categories.h"
28
29#include "resolve_properties.cpp"
30
31#include "llvm/Support/CommandLine.h"
32#include <re/printer_re.h>
33#include <pablo/printer_pablos.h>
34
35
36cl::OptionCategory cRegexOutputOptions("Regex Dump Options",
37                                      "These options control printing of intermediate regular expression structures.");
38
39cl::OptionCategory dPabloDumpOptions("Pablo Dump Options",
40                                      "These options control printing of intermediate Pablo code.");
41
42static cl::opt<bool> PrintAllREs("print-REs", cl::init(false), cl::desc("print regular expression passes"), cl::cat(cRegexOutputOptions));
43static cl::opt<bool> PrintParsedREs("print-parsed-REs", cl::init(false), cl::desc("print out parsed regular expressions"), cl::cat(cRegexOutputOptions));
44static cl::opt<bool> PrintStrippedREs("print-stripped-REs", cl::init(false), cl::desc("print out REs with nullable prefixes/suffixes removed"), cl::cat(cRegexOutputOptions));
45static cl::opt<bool> PrintNamedREs("print-named-REs", cl::init(false), cl::desc("print out named REs"), cl::cat(cRegexOutputOptions));
46static cl::opt<bool> PrintUTF8REs("print-utf8-REs", cl::init(false), cl::desc("print out UTF-8 REs"), cl::cat(cRegexOutputOptions));
47static cl::opt<bool> PrintSimplifiedREs("print-simplified-REs", cl::init(false), cl::desc("print out final simplified REs"), cl::cat(cRegexOutputOptions));
48static cl::opt<bool> PrintAllPablo("print-pablo", cl::init(false), cl::desc("print Pablo code passes"), cl::cat(dPabloDumpOptions));
49static cl::opt<bool> PrintCompiledCCcode("print-CC-pablo", cl::init(false), cl::desc("print Pablo output from character class compiler"), cl::cat(dPabloDumpOptions));
50static cl::opt<bool> PrintCompiledREcode("print-RE-pablo", cl::init(false), cl::desc("print Pablo output from the regular expression compiler"), cl::cat(dPabloDumpOptions));
51static cl::opt<bool> PrintOptimizedREcode("print-optimized-pablo", cl::init(false), cl::desc("print final optimized Pablo code"), cl::cat(dPabloDumpOptions));
52
53
54using namespace re;
55using namespace cc;
56using namespace pablo;
57
58namespace icgrep {
59
60LLVM_Gen_RetVal compile(const Encoding encoding, const std::vector<std::string> regexps, const bool enable_multiplexing) {
61    std::vector<RE *> REs;
62    RE * re_ast = nullptr;
63    for (int i = 0; i < regexps.size(); i++) {
64        try
65        {
66            re_ast = RE_Parser::parse(regexps[i]);
67        }
68        catch (ParseFailure failure)
69        {
70            std::cerr << "REGEX PARSING FAILURE: " << failure.what() << std::endl;
71            std::cerr << regexps[i] << std::endl;
72            exit(1);
73        }
74        REs.push_back(re_ast);
75    }
76    if (REs.size() > 1) {
77        re_ast = makeAlt(REs.begin(), REs.end());
78    }
79
80    if (PrintAllREs || PrintParsedREs) {
81      std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
82    }
83
84    //Optimization passes to simplify the AST.
85    re_ast = RE_Nullable::removeNullablePrefix(re_ast);
86    if (PrintAllREs || PrintStrippedREs) {
87      std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
88    }
89    re_ast = RE_Nullable::removeNullableSuffix(re_ast);
90    if (PrintAllREs || PrintStrippedREs) {
91      std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
92    }
93   
94    resolveProperties(re_ast);
95   
96   
97    CC_NameMap nameMap;
98    re_ast = nameMap.process(re_ast, UnicodeClass);
99
100    if (PrintAllREs || PrintNamedREs) {
101      std::cerr << "Namer:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
102      std::cerr << "NameMap:\n" << nameMap.printMap() << std::endl;
103    }
104
105    //Add the UTF encoding.
106    if (encoding.getType() == Encoding::Type::UTF_8) {
107        re_ast = UTF8_Encoder::toUTF8(nameMap, re_ast);
108        if (PrintAllREs || PrintUTF8REs) {
109          //Print to the terminal the AST that was generated by the utf8 encoder.
110          std::cerr << "UTF8-encoder:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
111          std::cerr << "NameMap:\n" << nameMap.printMap() << std::endl;
112        }
113    }
114   
115    re_ast = RE_Simplifier::simplify(re_ast);
116    if (PrintAllREs || PrintSimplifiedREs) {
117      //Print to the terminal the AST that was generated by the simplifier.
118      std::cerr << "Simplifier:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
119    }
120
121    SymbolGenerator symgen;
122    PabloBlock main(symgen);
123
124    CC_Compiler cc_compiler(main, encoding, enable_multiplexing);
125   
126    cc_compiler.compileByteClasses(re_ast);
127   
128    auto basisBits = cc_compiler.getBasisBits(nameMap);
129    if (PrintAllPablo || PrintCompiledCCcode) {
130      //Print to the terminal the AST that was generated by the character class compiler.
131      std::cerr << "CC AST:" << std::endl;
132      PabloPrinter::print(main.statements(), std::cerr);
133    }
134   
135    RE_Compiler re_compiler(main, nameMap);
136    re_compiler.initializeRequiredStreams(cc_compiler);
137    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast));
138    if (PrintAllPablo || PrintCompiledREcode) {
139      //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
140      std::cerr << "Initial Pablo AST:\n";
141      PabloPrinter::print(main.statements(), std::cerr);
142    }
143
144    // Scan through the pablo code and perform DCE and CSE
145    UseAnalysis::optimize(main);
146
147    if (PrintAllPablo || PrintOptimizedREcode) {
148      //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
149      std::cerr << "Final Pablo AST:\n";
150      PabloPrinter::print(main.statements(), std::cerr);
151    }
152
153    PabloCompiler pablo_compiler(basisBits);
154   
155#define CHECK_GENERAL_CODE_CATEGORY(SUFFIX) \
156    pablo_compiler.InstallExternalFunction(std::string("__get_gc_") + std::string(#SUFFIX), (void*)&__get_gc_##SUFFIX);
157   
158    CHECK_GENERAL_CODE_CATEGORY(Cc)
159    CHECK_GENERAL_CODE_CATEGORY(Cf)
160    CHECK_GENERAL_CODE_CATEGORY(Cn)
161    CHECK_GENERAL_CODE_CATEGORY(Co)
162    CHECK_GENERAL_CODE_CATEGORY(Cs)
163    CHECK_GENERAL_CODE_CATEGORY(Ll)
164    CHECK_GENERAL_CODE_CATEGORY(Lm)
165    CHECK_GENERAL_CODE_CATEGORY(Lo)
166    CHECK_GENERAL_CODE_CATEGORY(Lt)
167    CHECK_GENERAL_CODE_CATEGORY(Lu)
168    CHECK_GENERAL_CODE_CATEGORY(Mc)
169    CHECK_GENERAL_CODE_CATEGORY(Me)
170    CHECK_GENERAL_CODE_CATEGORY(Mn)
171    CHECK_GENERAL_CODE_CATEGORY(Nd)
172    CHECK_GENERAL_CODE_CATEGORY(Nl)
173    CHECK_GENERAL_CODE_CATEGORY(No)
174    CHECK_GENERAL_CODE_CATEGORY(Pc)
175    CHECK_GENERAL_CODE_CATEGORY(Pd)
176    CHECK_GENERAL_CODE_CATEGORY(Pe)
177    CHECK_GENERAL_CODE_CATEGORY(Pf)
178    CHECK_GENERAL_CODE_CATEGORY(Pi)
179    CHECK_GENERAL_CODE_CATEGORY(Po)
180    CHECK_GENERAL_CODE_CATEGORY(Ps)
181    CHECK_GENERAL_CODE_CATEGORY(Sc)
182    CHECK_GENERAL_CODE_CATEGORY(Sk)
183    CHECK_GENERAL_CODE_CATEGORY(Sm)
184    CHECK_GENERAL_CODE_CATEGORY(So)
185    CHECK_GENERAL_CODE_CATEGORY(Zl)
186    CHECK_GENERAL_CODE_CATEGORY(Zp)
187    CHECK_GENERAL_CODE_CATEGORY(Zs)
188   
189
190    LLVM_Gen_RetVal retVal = pablo_compiler.compile(main);
191
192    RE::release_memory();
193    PabloAST::release_memory();
194
195    return retVal;
196}
197
198}
Note: See TracBrowser for help on using the repository browser.