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

Last change on this file since 4410 was 4410, checked in by nmedfort, 4 years ago

Changes to support 3-operand form for all instructions. CSE disabled but partially redundant now.

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/analysis/useanalysis.h>
20
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> PrintAllPablo("print-pablo", cl::init(false), cl::desc("print Pablo code passes"), cl::cat(dPabloDumpOptions));
48static cl::opt<bool> PrintCompiledCCcode("print-CC-pablo", cl::init(false), cl::desc("print Pablo output from character class compiler"), cl::cat(dPabloDumpOptions));
49static cl::opt<bool> PrintCompiledREcode("print-RE-pablo", cl::init(false), cl::desc("print Pablo output from the regular expression compiler"), cl::cat(dPabloDumpOptions));
50static cl::opt<bool> PrintOptimizedREcode("print-optimized-pablo", cl::init(false), cl::desc("print final optimized Pablo code"), cl::cat(dPabloDumpOptions));
51
52
53using namespace re;
54using namespace cc;
55using namespace pablo;
56
57namespace icgrep {
58
59LLVM_Gen_RetVal compile(const Encoding encoding, const std::vector<std::string> regexps, const bool enable_multiplexing) {
60    std::vector<RE *> REs;
61    RE * re_ast = nullptr;
62    for (int i = 0; i < regexps.size(); i++) {
63        try
64        {
65            re_ast = RE_Parser::parse(regexps[i]);
66        }
67        catch (ParseFailure failure)
68        {
69            std::cerr << "REGEX PARSING FAILURE: " << failure.what() << std::endl;
70            std::cerr << regexps[i] << std::endl;
71            exit(1);
72        }
73        REs.push_back(re_ast);
74    }
75    if (REs.size() > 1) {
76        re_ast = makeAlt(REs.begin(), REs.end());
77    }
78
79    if (PrintAllREs || PrintParsedREs) {
80      std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
81    }
82
83    //Optimization passes to simplify the AST.
84    re_ast = RE_Nullable::removeNullablePrefix(re_ast);
85    if (PrintAllREs || PrintStrippedREs) {
86      std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
87    }
88    re_ast = RE_Nullable::removeNullableSuffix(re_ast);
89    if (PrintAllREs || PrintStrippedREs) {
90      std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
91    }
92   
93    resolveProperties(re_ast);
94   
95   
96    CC_NameMap nameMap;
97    re_ast = nameMap.process(re_ast, UnicodeClass);
98
99    if (PrintAllREs || PrintNamedREs) {
100      std::cerr << "Namer:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
101      std::cerr << "NameMap:\n" << nameMap.printMap() << std::endl;
102    }
103
104    //Add the UTF encoding.
105    if (encoding.getType() == Encoding::Type::UTF_8) {
106        re_ast = UTF8_Encoder::toUTF8(nameMap, re_ast);
107        if (PrintAllREs || PrintUTF8REs) {
108          //Print to the terminal the AST that was generated by the utf8 encoder.
109          std::cerr << "UTF8-encoder:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
110          std::cerr << "NameMap:\n" << nameMap.printMap() << std::endl;
111        }
112    }
113   
114    re_ast = RE_Simplifier::simplify(re_ast);
115    if (PrintAllREs || PrintSimplifiedREs) {
116      //Print to the terminal the AST that was generated by the simplifier.
117      std::cerr << "Simplifier:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
118    }
119
120    PabloBlock & main = PabloBlock::Create();
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    install_property_gc_fn_ptrs(pablo_compiler);
154    install_property_sc_fn_ptrs(pablo_compiler);
155    install_property_scx_fn_ptrs(pablo_compiler);
156    install_property_blk_fn_ptrs(pablo_compiler);
157    install_property_DerivedCoreProperties_fn_ptrs(pablo_compiler);
158    install_property_PropList_fn_ptrs(pablo_compiler);
159
160    LLVM_Gen_RetVal retVal = pablo_compiler.compile(main);
161
162    RE::release_memory();
163    PabloAST::release_memory();
164
165    return retVal;
166}
167
168}
Note: See TracBrowser for help on using the repository browser.