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
RevLine 
[3850]1/*
[4388]2 *  Copyright (c) 2015 International Characters.
[3850]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
[4237]7#include <compiler.h>
[4337]8#include <re/re_cc.h>
[4237]9#include <re/re_nullable.h>
10#include <re/re_simplifier.h>
[4328]11#include <re/re_alt.h>
[4237]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>
[4249]17#include <cc/cc_namemap.hpp>
[4237]18#include <pablo/pablo_compiler.h>
[4416]19#include <pablo/optimizers/pablo_simplifier.hpp>
[4521]20#include <pablo/optimizers/pablo_codesinking.hpp>
[4385]21#include "UCD/precompiled_gc.h"
22#include "UCD/precompiled_sc.h"
[4386]23#include "UCD/precompiled_scx.h"
24#include "UCD/precompiled_blk.h"
[4388]25#include "UCD/precompiled_derivedcoreproperties.h"
[4392]26#include "UCD/precompiled_proplist.h"
[4383]27
[4380]28#include "resolve_properties.cpp"
29
[4348]30#include "llvm/Support/CommandLine.h"
[4227]31#include <re/printer_re.h>
32#include <pablo/printer_pablos.h>
[4197]33
[4348]34
[4353]35cl::OptionCategory cRegexOutputOptions("Regex Dump Options",
36                                      "These options control printing of intermediate regular expression structures.");
[4348]37
[4353]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));
[4513]49static cl::opt<bool> PrintOptimizedREcode("print-pablo", cl::init(false), cl::desc("print final optimized Pablo code"), cl::cat(dPabloDumpOptions));
[4353]50
51
[4521]52cl::OptionCategory cPabloOptimizationsOptions("Pablo Optimizations",
[4523]53                                              "These options control Pablo optimization passes.");
[4521]54
[4523]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));
[4521]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
[4197]62using namespace re;
[4198]63using namespace cc;
[4237]64using namespace pablo;
[4197]65
66namespace icgrep {
67
[4516]68CompiledPabloFunction compile(const Encoding encoding, const std::vector<std::string> regexps, const ModeFlagSet initialFlags) {
[4328]69    std::vector<RE *> REs;
[4197]70    RE * re_ast = nullptr;
[4328]71    for (int i = 0; i < regexps.size(); i++) {
72        try
73        {
[4412]74            re_ast = RE_Parser::parse(regexps[i], initialFlags);
[4328]75        }
76        catch (ParseFailure failure)
77        {
[4431]78            std::cerr << "Regex parsing failure: " << failure.what() << std::endl;
[4328]79            std::cerr << regexps[i] << std::endl;
80            exit(1);
81        }
82        REs.push_back(re_ast);
[4197]83    }
[4328]84    if (REs.size() > 1) {
85        re_ast = makeAlt(REs.begin(), REs.end());
[4197]86    }
[3850]87
[4348]88    if (PrintAllREs || PrintParsedREs) {
89      std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
90    }
[3850]91
[4246]92    //Optimization passes to simplify the AST.
93    re_ast = RE_Nullable::removeNullablePrefix(re_ast);
[4348]94    if (PrintAllREs || PrintStrippedREs) {
95      std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
96    }
[4246]97    re_ast = RE_Nullable::removeNullableSuffix(re_ast);
[4348]98    if (PrintAllREs || PrintStrippedREs) {
99      std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
100    }
101   
[4380]102    resolveProperties(re_ast);
103   
104   
[4249]105    CC_NameMap nameMap;
[4337]106    re_ast = nameMap.process(re_ast, UnicodeClass);
[3850]107
[4348]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    }
[4197]112
[4249]113    //Add the UTF encoding.
114    if (encoding.getType() == Encoding::Type::UTF_8) {
115        re_ast = UTF8_Encoder::toUTF8(nameMap, re_ast);
[4348]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        }
[4249]121    }
[4331]122   
[4197]123    re_ast = RE_Simplifier::simplify(re_ast);
[4348]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    }
[4197]128
[4510]129    SymbolGenerator symbolGenerator;
130    PabloBlock & main = PabloBlock::Create(symbolGenerator);
[4210]131
[4412]132    CC_Compiler cc_compiler(main, encoding);
[4337]133   
134    cc_compiler.compileByteClasses(re_ast);
135   
136    auto basisBits = cc_compiler.getBasisBits(nameMap);
[4513]137    if (PrintCompiledCCcode) {
[4348]138      //Print to the terminal the AST that was generated by the character class compiler.
[4567]139      llvm::raw_os_ostream cerr(std::cerr);
140      cerr << "CC AST:" << "\n";
141      PabloPrinter::print(main.statements(), cerr);
[4348]142    }
[4337]143   
[4438]144    RE_Compiler re_compiler(main);
[4334]145    re_compiler.initializeRequiredStreams(cc_compiler);
[4330]146    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast));
[4513]147    if (PrintCompiledREcode) {
[4348]148      //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
[4567]149      llvm::raw_os_ostream cerr(std::cerr);
150      cerr << "Initial Pablo AST:\n";
151      PabloPrinter::print(main.statements(), cerr);
[4348]152    }
[4197]153
[4280]154    // Scan through the pablo code and perform DCE and CSE
[4523]155    if (!DisablePabloCSE) {
156        Simplifier::optimize(main);
157    }
[4521]158    if (PabloSinkingPass) {
159        CodeSinking::optimize(main);
160    }
[4513]161    if (PrintOptimizedREcode) {
[4348]162      //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
[4567]163      llvm::raw_os_ostream cerr(std::cerr);
164      cerr << "Final Pablo AST:\n";
165      PabloPrinter::print(main.statements(), cerr);
[4348]166    }
[4280]167
[4270]168    PabloCompiler pablo_compiler(basisBits);
[4383]169   
[4385]170    install_property_gc_fn_ptrs(pablo_compiler);
171    install_property_sc_fn_ptrs(pablo_compiler);
[4386]172    install_property_scx_fn_ptrs(pablo_compiler);
173    install_property_blk_fn_ptrs(pablo_compiler);
[4388]174    install_property_DerivedCoreProperties_fn_ptrs(pablo_compiler);
[4392]175    install_property_PropList_fn_ptrs(pablo_compiler);
[4197]176
[4522]177    try {
178        CompiledPabloFunction retVal = pablo_compiler.compile(main);
[4526]179        releaseSlabAllocatorMemory();
[4522]180        return retVal;
181    }
[4526]182    catch (std::runtime_error e) {
183        releaseSlabAllocatorMemory();
[4522]184        std::cerr << "Runtime error: " << e.what() << std::endl;
185        exit(1);
186    }
[4197]187}
188
189}
Note: See TracBrowser for help on using the repository browser.