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

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

Add support for ScriptExtensions? and Block properties

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