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

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

Use llvm:raw_ostream in Pablo printer

File size: 7.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 <compiler.h>
8#include <re/re_cc.h>
9#include <re/re_nullable.h>
10#include <re/re_simplifier.h>
11#include <re/re_alt.h>
12#include <re/parsefailure.h>
13#include <re/re_parser.h>
14#include <re/re_compiler.h>
15#include <utf8_encoder.h>
16#include <cc/cc_compiler.h>
17#include <cc/cc_namemap.hpp>
18#include <pablo/pablo_compiler.h>
19#include <pablo/optimizers/pablo_simplifier.hpp>
20#include <pablo/optimizers/pablo_codesinking.hpp>
21#include "UCD/precompiled_gc.h"
22#include "UCD/precompiled_sc.h"
23#include "UCD/precompiled_scx.h"
24#include "UCD/precompiled_blk.h"
25#include "UCD/precompiled_derivedcoreproperties.h"
26#include "UCD/precompiled_proplist.h"
27
28#include "resolve_properties.cpp"
29
30#include "llvm/Support/CommandLine.h"
31#include <re/printer_re.h>
32#include <pablo/printer_pablos.h>
33
34
35cl::OptionCategory cRegexOutputOptions("Regex Dump Options",
36                                      "These options control printing of intermediate regular expression structures.");
37
38cl::OptionCategory dPabloDumpOptions("Pablo Dump Options",
39                                      "These options control printing of intermediate Pablo code.");
40
41static cl::opt<bool> PrintAllREs("print-REs", cl::init(false), cl::desc("print regular expression passes"), cl::cat(cRegexOutputOptions));
42static cl::opt<bool> PrintParsedREs("print-parsed-REs", cl::init(false), cl::desc("print out parsed regular expressions"), cl::cat(cRegexOutputOptions));
43static cl::opt<bool> PrintStrippedREs("print-stripped-REs", cl::init(false), cl::desc("print out REs with nullable prefixes/suffixes removed"), cl::cat(cRegexOutputOptions));
44static cl::opt<bool> PrintNamedREs("print-named-REs", cl::init(false), cl::desc("print out named REs"), cl::cat(cRegexOutputOptions));
45static cl::opt<bool> PrintUTF8REs("print-utf8-REs", cl::init(false), cl::desc("print out UTF-8 REs"), cl::cat(cRegexOutputOptions));
46static cl::opt<bool> PrintSimplifiedREs("print-simplified-REs", cl::init(false), cl::desc("print out final simplified REs"), cl::cat(cRegexOutputOptions));
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-pablo", cl::init(false), cl::desc("print final optimized Pablo code"), cl::cat(dPabloDumpOptions));
50
51
52cl::OptionCategory cPabloOptimizationsOptions("Pablo Optimizations",
53                                              "These options control Pablo optimization passes.");
54
55static cl::opt<bool> DisablePabloCSE("disable-CSE", cl::init(false),
56                                      cl::desc("Disable Pablo common subexpression elimination/dead code elimination"),
57                                      cl::cat(cPabloOptimizationsOptions));
58static cl::opt<bool> PabloSinkingPass("sinking", cl::init(false),
59                                      cl::desc("Moves all instructions into the innermost legal If-scope so that they are only executed when needed."),
60                                      cl::cat(cPabloOptimizationsOptions));
61
62using namespace re;
63using namespace cc;
64using namespace pablo;
65
66namespace icgrep {
67
68CompiledPabloFunction compile(const Encoding encoding, const std::vector<std::string> regexps, const ModeFlagSet initialFlags) {
69    std::vector<RE *> REs;
70    RE * re_ast = nullptr;
71    for (int i = 0; i < regexps.size(); i++) {
72        try
73        {
74            re_ast = RE_Parser::parse(regexps[i], initialFlags);
75        }
76        catch (ParseFailure failure)
77        {
78            std::cerr << "Regex parsing failure: " << failure.what() << std::endl;
79            std::cerr << regexps[i] << std::endl;
80            exit(1);
81        }
82        REs.push_back(re_ast);
83    }
84    if (REs.size() > 1) {
85        re_ast = makeAlt(REs.begin(), REs.end());
86    }
87
88    if (PrintAllREs || PrintParsedREs) {
89      std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
90    }
91
92    //Optimization passes to simplify the AST.
93    re_ast = RE_Nullable::removeNullablePrefix(re_ast);
94    if (PrintAllREs || PrintStrippedREs) {
95      std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
96    }
97    re_ast = RE_Nullable::removeNullableSuffix(re_ast);
98    if (PrintAllREs || PrintStrippedREs) {
99      std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
100    }
101   
102    resolveProperties(re_ast);
103   
104   
105    CC_NameMap nameMap;
106    re_ast = nameMap.process(re_ast, UnicodeClass);
107
108    if (PrintAllREs || PrintNamedREs) {
109      std::cerr << "Namer:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
110      std::cerr << "NameMap:\n" << nameMap.printMap() << std::endl;
111    }
112
113    //Add the UTF encoding.
114    if (encoding.getType() == Encoding::Type::UTF_8) {
115        re_ast = UTF8_Encoder::toUTF8(nameMap, re_ast);
116        if (PrintAllREs || PrintUTF8REs) {
117          //Print to the terminal the AST that was generated by the utf8 encoder.
118          std::cerr << "UTF8-encoder:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
119          std::cerr << "NameMap:\n" << nameMap.printMap() << std::endl;
120        }
121    }
122   
123    re_ast = RE_Simplifier::simplify(re_ast);
124    if (PrintAllREs || PrintSimplifiedREs) {
125      //Print to the terminal the AST that was generated by the simplifier.
126      std::cerr << "Simplifier:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
127    }
128
129    SymbolGenerator symbolGenerator;
130    PabloBlock & main = PabloBlock::Create(symbolGenerator);
131
132    CC_Compiler cc_compiler(main, encoding);
133   
134    cc_compiler.compileByteClasses(re_ast);
135   
136    auto basisBits = cc_compiler.getBasisBits(nameMap);
137    if (PrintCompiledCCcode) {
138      //Print to the terminal the AST that was generated by the character class compiler.
139      llvm::raw_os_ostream cerr(std::cerr);
140      cerr << "CC AST:" << "\n";
141      PabloPrinter::print(main.statements(), cerr);
142    }
143   
144    RE_Compiler re_compiler(main);
145    re_compiler.initializeRequiredStreams(cc_compiler);
146    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast));
147    if (PrintCompiledREcode) {
148      //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
149      llvm::raw_os_ostream cerr(std::cerr);
150      cerr << "Initial Pablo AST:\n";
151      PabloPrinter::print(main.statements(), cerr);
152    }
153
154    // Scan through the pablo code and perform DCE and CSE
155    if (!DisablePabloCSE) {
156        Simplifier::optimize(main);
157    }
158    if (PabloSinkingPass) {
159        CodeSinking::optimize(main);
160    }
161    if (PrintOptimizedREcode) {
162      //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
163      llvm::raw_os_ostream cerr(std::cerr);
164      cerr << "Final Pablo AST:\n";
165      PabloPrinter::print(main.statements(), cerr);
166    }
167
168    PabloCompiler pablo_compiler(basisBits);
169   
170    install_property_gc_fn_ptrs(pablo_compiler);
171    install_property_sc_fn_ptrs(pablo_compiler);
172    install_property_scx_fn_ptrs(pablo_compiler);
173    install_property_blk_fn_ptrs(pablo_compiler);
174    install_property_DerivedCoreProperties_fn_ptrs(pablo_compiler);
175    install_property_PropList_fn_ptrs(pablo_compiler);
176
177    try {
178        CompiledPabloFunction retVal = pablo_compiler.compile(main);
179        releaseSlabAllocatorMemory();
180        return retVal;
181    }
182    catch (std::runtime_error e) {
183        releaseSlabAllocatorMemory();
184        std::cerr << "Runtime error: " << e.what() << std::endl;
185        exit(1);
186    }
187}
188
189}
Note: See TracBrowser for help on using the repository browser.