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

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

Compile only byteclasses with cc_compiler; resolve CC/BC ambiguity

File size: 4.6 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//#define DEBUG_PRINT_RE_AST
28//#define DEBUG_PRINT_PBIX_AST
29
30#ifdef DEBUG_PRINT_RE_AST
31#include <re/printer_re.h>
32#endif
33#ifdef DEBUG_PRINT_PBIX_AST
34#include <pablo/printer_pablos.h>
35#endif
36
37using namespace re;
38using namespace cc;
39using namespace pablo;
40
41namespace icgrep {
42
43LLVM_Gen_RetVal compile(const Encoding encoding, const std::vector<std::string> regexps, const bool enable_multiplexing) {
44    std::vector<RE *> REs;
45    RE * re_ast = nullptr;
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);
58    }
59    if (REs.size() > 1) {
60        re_ast = makeAlt(REs.begin(), REs.end());
61    }
62
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
67
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
79    CC_NameMap nameMap;
80    re_ast = nameMap.process(re_ast, UnicodeClass);
81
82    #ifdef DEBUG_PRINT_RE_AST
83    std::cerr << "Namer:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
84    std::cerr << "NameMap:\n" << nameMap.printMap() << std::endl;
85    #endif
86
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    }
95    #ifdef DEBUG_PRINT_RE_AST
96    std::cerr << "NameMap:\n" << nameMap.printMap() << std::endl;
97    #endif
98   
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
105    SymbolGenerator symgen;
106    PabloBlock main(symgen);
107
108    CC_Compiler cc_compiler(main, encoding, enable_multiplexing);
109   
110    cc_compiler.compileByteClasses(re_ast);
111   
112    auto basisBits = cc_compiler.getBasisBits(nameMap);
113    #ifdef DEBUG_PRINT_PBIX_AST
114    //Print to the terminal the AST that was generated by the character class compiler.
115    std::cerr << "CC AST:" << std::endl << PabloPrinter::print(main.statements()) << std::endl;
116    #endif
117
118   
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    #ifdef DEBUG_PRINT_PBIX_AST
125    //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
126    std::cerr << "Initial Pablo AST:" << PabloPrinter::print(main.statements()) << ")" << std::endl;
127    #endif
128
129    // Scan through the pablo code and perform DCE and CSE
130    UseAnalysis::optimize(main);
131
132    #ifdef DEBUG_PRINT_PBIX_AST
133    //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
134    std::cerr << "Final Pablo AST:" << PabloPrinter::print(main.statements()) << ")" << std::endl;
135    #endif
136
137    PabloCompiler pablo_compiler(basisBits);
138
139    LLVM_Gen_RetVal retVal = pablo_compiler.compile(main);
140
141    RE::release_memory();
142    PabloAST::release_memory();
143
144    return retVal;
145}
146
147}
Note: See TracBrowser for help on using the repository browser.