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

Last change on this file since 4416 was 4416, checked in by nmedfort, 5 years ago

Many use def info changes; removed dependency on boost system library. More work still needed on CSE.

File size: 6.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 "UCD/precompiled_gc.h"
21#include "UCD/precompiled_sc.h"
22#include "UCD/precompiled_scx.h"
23#include "UCD/precompiled_blk.h"
24#include "UCD/precompiled_derivedcoreproperties.h"
25#include "UCD/precompiled_proplist.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 ModeFlagSet initialFlags) {
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], initialFlags);
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    PabloBlock & main = PabloBlock::Create();
120
121    CC_Compiler cc_compiler(main, encoding);
122   
123    cc_compiler.compileByteClasses(re_ast);
124   
125    auto basisBits = cc_compiler.getBasisBits(nameMap);
126    if (PrintAllPablo || PrintCompiledCCcode) {
127      //Print to the terminal the AST that was generated by the character class compiler.
128      std::cerr << "CC AST:" << std::endl;
129      PabloPrinter::print(main.statements(), std::cerr);
130    }
131   
132    RE_Compiler re_compiler(main, nameMap);
133    re_compiler.initializeRequiredStreams(cc_compiler);
134    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast));
135    if (PrintAllPablo || PrintCompiledREcode) {
136      //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
137      std::cerr << "Initial Pablo AST:\n";
138      PabloPrinter::print(main.statements(), std::cerr);
139    }
140
141    // Scan through the pablo code and perform DCE and CSE
142    Simplifier::optimize(main);
143
144    if (PrintAllPablo || PrintOptimizedREcode) {
145      //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
146      std::cerr << "Final Pablo AST:\n";
147      PabloPrinter::print(main.statements(), std::cerr);
148    }
149
150    PabloCompiler pablo_compiler(basisBits);
151   
152    install_property_gc_fn_ptrs(pablo_compiler);
153    install_property_sc_fn_ptrs(pablo_compiler);
154    install_property_scx_fn_ptrs(pablo_compiler);
155    install_property_blk_fn_ptrs(pablo_compiler);
156    install_property_DerivedCoreProperties_fn_ptrs(pablo_compiler);
157    install_property_PropList_fn_ptrs(pablo_compiler);
158
159    LLVM_Gen_RetVal retVal = pablo_compiler.compile(main);
160
161    RE::release_memory();
162    PabloAST::release_memory();
163
164    return retVal;
165}
166
167}
Note: See TracBrowser for help on using the repository browser.