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

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

Pablo printer uses ostream, python Pablo syntax

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