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

Last change on this file since 4380 was 4380, checked in by cameron, 5 years ago

UCD property resolution initial check in

File size: 6.4 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 "resolve_properties.cpp"
28
29#include "llvm/Support/CommandLine.h"
30#include <re/printer_re.h>
31#include <pablo/printer_pablos.h>
32
33
34cl::OptionCategory cRegexOutputOptions("Regex Dump Options",
35                                      "These options control printing of intermediate regular expression structures.");
36
37cl::OptionCategory dPabloDumpOptions("Pablo Dump Options",
38                                      "These options control printing of intermediate Pablo code.");
39
40static cl::opt<bool> PrintAllREs("print-REs", cl::init(false), cl::desc("print regular expression passes"), cl::cat(cRegexOutputOptions));
41static cl::opt<bool> PrintParsedREs("print-parsed-REs", cl::init(false), cl::desc("print out parsed regular expressions"), cl::cat(cRegexOutputOptions));
42static cl::opt<bool> PrintStrippedREs("print-stripped-REs", cl::init(false), cl::desc("print out REs with nullable prefixes/suffixes removed"), cl::cat(cRegexOutputOptions));
43static cl::opt<bool> PrintNamedREs("print-named-REs", cl::init(false), cl::desc("print out named REs"), cl::cat(cRegexOutputOptions));
44static cl::opt<bool> PrintUTF8REs("print-utf8-REs", cl::init(false), cl::desc("print out UTF-8 REs"), cl::cat(cRegexOutputOptions));
45static cl::opt<bool> PrintSimplifiedREs("print-simplified-REs", cl::init(false), cl::desc("print out final simplified REs"), cl::cat(cRegexOutputOptions));
46static cl::opt<bool> PrintAllPablo("print-pablo", cl::init(false), cl::desc("print Pablo code passes"), cl::cat(dPabloDumpOptions));
47static cl::opt<bool> PrintCompiledCCcode("print-CC-pablo", cl::init(false), cl::desc("print Pablo output from character class compiler"), cl::cat(dPabloDumpOptions));
48static cl::opt<bool> PrintCompiledREcode("print-RE-pablo", cl::init(false), cl::desc("print Pablo output from the regular expression compiler"), cl::cat(dPabloDumpOptions));
49static cl::opt<bool> PrintOptimizedREcode("print-optimized-pablo", cl::init(false), cl::desc("print final optimized Pablo code"), cl::cat(dPabloDumpOptions));
50
51
52using namespace re;
53using namespace cc;
54using namespace pablo;
55
56namespace icgrep {
57
58LLVM_Gen_RetVal compile(const Encoding encoding, const std::vector<std::string> regexps, const bool enable_multiplexing) {
59    std::vector<RE *> REs;
60    RE * re_ast = nullptr;
61    for (int i = 0; i < regexps.size(); i++) {
62        try
63        {
64            re_ast = RE_Parser::parse(regexps[i]);
65        }
66        catch (ParseFailure failure)
67        {
68            std::cerr << "REGEX PARSING FAILURE: " << failure.what() << std::endl;
69            std::cerr << regexps[i] << std::endl;
70            exit(1);
71        }
72        REs.push_back(re_ast);
73    }
74    if (REs.size() > 1) {
75        re_ast = makeAlt(REs.begin(), REs.end());
76    }
77
78    if (PrintAllREs || PrintParsedREs) {
79      std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
80    }
81
82    //Optimization passes to simplify the AST.
83    re_ast = RE_Nullable::removeNullablePrefix(re_ast);
84    if (PrintAllREs || PrintStrippedREs) {
85      std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
86    }
87    re_ast = RE_Nullable::removeNullableSuffix(re_ast);
88    if (PrintAllREs || PrintStrippedREs) {
89      std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
90    }
91   
92    resolveProperties(re_ast);
93   
94   
95    CC_NameMap nameMap;
96    re_ast = nameMap.process(re_ast, UnicodeClass);
97
98    if (PrintAllREs || PrintNamedREs) {
99      std::cerr << "Namer:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
100      std::cerr << "NameMap:\n" << nameMap.printMap() << std::endl;
101    }
102
103    //Add the UTF encoding.
104    if (encoding.getType() == Encoding::Type::UTF_8) {
105        re_ast = UTF8_Encoder::toUTF8(nameMap, re_ast);
106        if (PrintAllREs || PrintUTF8REs) {
107          //Print to the terminal the AST that was generated by the utf8 encoder.
108          std::cerr << "UTF8-encoder:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
109          std::cerr << "NameMap:\n" << nameMap.printMap() << std::endl;
110        }
111    }
112   
113    re_ast = RE_Simplifier::simplify(re_ast);
114    if (PrintAllREs || PrintSimplifiedREs) {
115      //Print to the terminal the AST that was generated by the simplifier.
116      std::cerr << "Simplifier:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
117    }
118
119    SymbolGenerator symgen;
120    PabloBlock main(symgen);
121
122    CC_Compiler cc_compiler(main, encoding, enable_multiplexing);
123   
124    cc_compiler.compileByteClasses(re_ast);
125   
126    auto basisBits = cc_compiler.getBasisBits(nameMap);
127    if (PrintAllPablo || PrintCompiledCCcode) {
128      //Print to the terminal the AST that was generated by the character class compiler.
129      std::cerr << "CC AST:" << std::endl;
130      PabloPrinter::print(main.statements(), std::cerr);
131    }
132   
133    RE_Compiler re_compiler(main, nameMap);
134    re_compiler.initializeRequiredStreams(cc_compiler);
135    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast));
136    if (PrintAllPablo || PrintCompiledREcode) {
137      //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
138      std::cerr << "Initial Pablo AST:\n";
139      PabloPrinter::print(main.statements(), std::cerr);
140    }
141
142    // Scan through the pablo code and perform DCE and CSE
143    UseAnalysis::optimize(main);
144
145    if (PrintAllPablo || PrintOptimizedREcode) {
146      //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
147      std::cerr << "Final Pablo AST:\n";
148      PabloPrinter::print(main.statements(), std::cerr);
149    }
150
151    PabloCompiler pablo_compiler(basisBits);
152
153    LLVM_Gen_RetVal retVal = pablo_compiler.compile(main);
154
155    RE::release_memory();
156    PabloAST::release_memory();
157
158    return retVal;
159}
160
161}
Note: See TracBrowser for help on using the repository browser.