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

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

Restructuring so compile is independent of initialization/finalization

File size: 4.5 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_nullable.h>
15#include <re/re_simplifier.h>
16#include <re/re_alt.h>
17#include <re/parsefailure.h>
18#include <re/re_parser.h>
19#include <re/re_compiler.h>
20#include <utf8_encoder.h>
21#include <cc/cc_compiler.h>
22#include <cc/cc_namemap.hpp>
23#include <pablo/pablo_compiler.h>
24#include <pablo/analysis/useanalysis.h>
25
26//#define DEBUG_PRINT_RE_AST
27//#define DEBUG_PRINT_PBIX_AST
28
29#ifdef DEBUG_PRINT_RE_AST
30#include <re/printer_re.h>
31#endif
32#ifdef DEBUG_PRINT_PBIX_AST
33#include <pablo/printer_pablos.h>
34#endif
35
36using namespace re;
37using namespace cc;
38using namespace pablo;
39
40namespace icgrep {
41
42LLVM_Gen_RetVal compile(const Encoding encoding, const std::vector<std::string> regexps, const bool enable_multiplexing) {
43    std::vector<RE *> REs;
44    RE * re_ast = nullptr;
45    for (int i = 0; i < regexps.size(); i++) {
46        try
47        {
48            re_ast = RE_Parser::parse(regexps[i]);
49        }
50        catch (ParseFailure failure)
51        {
52            std::cerr << "REGEX PARSING FAILURE: " << failure.what() << std::endl;
53            std::cerr << regexps[i] << std::endl;
54            exit(1);
55        }
56        REs.push_back(re_ast);
57    }
58    if (REs.size() > 1) {
59        re_ast = makeAlt(REs.begin(), REs.end());
60    }
61
62    #ifdef DEBUG_PRINT_RE_AST
63    //Print to the terminal the AST that was generated by the parser before adding the UTF encoding:
64    std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
65    #endif
66
67    //Optimization passes to simplify the AST.
68    re_ast = RE_Nullable::removeNullablePrefix(re_ast);
69    #ifdef DEBUG_PRINT_RE_AST
70    std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
71    #endif
72
73    re_ast = RE_Nullable::removeNullableSuffix(re_ast);
74    #ifdef DEBUG_PRINT_RE_AST
75    std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
76    #endif
77
78    CC_NameMap nameMap;
79    re_ast = nameMap.process(re_ast);
80
81    #ifdef DEBUG_PRINT_RE_AST
82    std::cerr << "Namer:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
83    #endif
84
85    //Add the UTF encoding.
86    if (encoding.getType() == Encoding::Type::UTF_8) {
87        re_ast = UTF8_Encoder::toUTF8(nameMap, re_ast);
88        #ifdef DEBUG_PRINT_RE_AST
89        //Print to the terminal the AST that was generated by the utf8 encoder.
90        std::cerr << "UTF8-encoder:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
91        #endif
92    }
93
94    // note: system is clumbersome at the moment; this ought to be done AFTER toUTF8.
95    nameMap.addPredefined("LineFeed", makeCC(0x0A));
96
97    re_ast = RE_Simplifier::simplify(re_ast);
98    #ifdef DEBUG_PRINT_RE_AST
99    //Print to the terminal the AST that was generated by the simplifier.
100    std::cerr << "Simplifier:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
101    #endif
102
103    SymbolGenerator symgen;
104    PabloBlock main(symgen);
105
106    CC_Compiler cc_compiler(main, encoding, enable_multiplexing);
107    auto basisBits = cc_compiler.compile(nameMap);
108    #ifdef DEBUG_PRINT_PBIX_AST
109    //Print to the terminal the AST that was generated by the character class compiler.
110    std::cerr << "CC AST:" << std::endl << PabloPrinter::print(main.statements()) << std::endl;
111    #endif
112
113    RE_Compiler re_compiler(main, nameMap);
114    re_compiler.initializeRequiredStreams();
115    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast));
116    #ifdef DEBUG_PRINT_PBIX_AST
117    //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
118    std::cerr << "Initial Pablo AST:" << PabloPrinter::print(main.statements()) << ")" << std::endl;
119    #endif
120
121    RE::release_memory();
122
123    // Scan through the pablo code and perform DCE and CSE
124    UseAnalysis::optimize(main);
125
126    #ifdef DEBUG_PRINT_PBIX_AST
127    //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
128    std::cerr << "Final Pablo AST:" << PabloPrinter::print(main.statements()) << ")" << std::endl;
129    #endif
130
131    PabloCompiler pablo_compiler(basisBits);
132
133    LLVM_Gen_RetVal retVal = pablo_compiler.compile(main);
134
135    PabloAST::release_memory();
136
137    return retVal;
138}
139
140}
Note: See TracBrowser for help on using the repository browser.