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

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

-print-REs, -print-pablo, -dump-generated-IR command-line options

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