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

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

Group options; hide LLVM options

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