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

Last change on this file since 4380 was 4380, checked in by cameron, 5 years ago

UCD property resolution initial check in

File size: 6.4 KB
RevLine 
[3850]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
[4197]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
[4237]13#include <compiler.h>
[4337]14#include <re/re_cc.h>
[4237]15#include <re/re_nullable.h>
16#include <re/re_simplifier.h>
[4328]17#include <re/re_alt.h>
[4237]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>
[4249]23#include <cc/cc_namemap.hpp>
[4237]24#include <pablo/pablo_compiler.h>
[4280]25#include <pablo/analysis/useanalysis.h>
[3850]26
[4380]27#include "resolve_properties.cpp"
28
[4348]29#include "llvm/Support/CommandLine.h"
[4227]30#include <re/printer_re.h>
31#include <pablo/printer_pablos.h>
[4197]32
[4348]33
[4353]34cl::OptionCategory cRegexOutputOptions("Regex Dump Options",
35                                      "These options control printing of intermediate regular expression structures.");
[4348]36
[4353]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
[4197]52using namespace re;
[4198]53using namespace cc;
[4237]54using namespace pablo;
[4197]55
56namespace icgrep {
57
[4328]58LLVM_Gen_RetVal compile(const Encoding encoding, const std::vector<std::string> regexps, const bool enable_multiplexing) {
59    std::vector<RE *> REs;
[4197]60    RE * re_ast = nullptr;
[4328]61    for (int i = 0; i < regexps.size(); i++) {
62        try
63        {
64            re_ast = RE_Parser::parse(regexps[i]);
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);
[4197]73    }
[4328]74    if (REs.size() > 1) {
75        re_ast = makeAlt(REs.begin(), REs.end());
[4197]76    }
[3850]77
[4348]78    if (PrintAllREs || PrintParsedREs) {
79      std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
80    }
[3850]81
[4246]82    //Optimization passes to simplify the AST.
83    re_ast = RE_Nullable::removeNullablePrefix(re_ast);
[4348]84    if (PrintAllREs || PrintStrippedREs) {
85      std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
86    }
[4246]87    re_ast = RE_Nullable::removeNullableSuffix(re_ast);
[4348]88    if (PrintAllREs || PrintStrippedREs) {
89      std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
90    }
91   
[4380]92    resolveProperties(re_ast);
93   
94   
[4249]95    CC_NameMap nameMap;
[4337]96    re_ast = nameMap.process(re_ast, UnicodeClass);
[3850]97
[4348]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    }
[4197]102
[4249]103    //Add the UTF encoding.
104    if (encoding.getType() == Encoding::Type::UTF_8) {
105        re_ast = UTF8_Encoder::toUTF8(nameMap, re_ast);
[4348]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        }
[4249]111    }
[4331]112   
[4197]113    re_ast = RE_Simplifier::simplify(re_ast);
[4348]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    }
[4197]118
[4210]119    SymbolGenerator symgen;
[4220]120    PabloBlock main(symgen);
[4210]121
[4287]122    CC_Compiler cc_compiler(main, encoding, enable_multiplexing);
[4337]123   
124    cc_compiler.compileByteClasses(re_ast);
125   
126    auto basisBits = cc_compiler.getBasisBits(nameMap);
[4348]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    }
[4337]132   
[4249]133    RE_Compiler re_compiler(main, nameMap);
[4334]134    re_compiler.initializeRequiredStreams(cc_compiler);
[4330]135    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast));
[4348]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    }
[4197]141
[4280]142    // Scan through the pablo code and perform DCE and CSE
143    UseAnalysis::optimize(main);
144
[4348]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    }
[4280]150
[4270]151    PabloCompiler pablo_compiler(basisBits);
[4197]152
[4242]153    LLVM_Gen_RetVal retVal = pablo_compiler.compile(main);
[4197]154
[4335]155    RE::release_memory();
[4276]156    PabloAST::release_memory();
157
[4197]158    return retVal;
159}
160
161}
Note: See TracBrowser for help on using the repository browser.