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

Last change on this file since 4251 was 4249, checked in by nmedfort, 5 years ago

Big update to use CC_NameMap; removed CharClass? and RE_Reducer.

File size: 4.2 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/parsefailure.h>
17#include <re/re_parser.h>
18#include <re/re_compiler.h>
19#include "hrtime.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
25//#define DEBUG_PRINT_RE_AST
26//#define DEBUG_PRINT_PBIX_AST
27
28#ifdef DEBUG_PRINT_RE_AST
29#include <re/printer_re.h>
30#endif
31#ifdef DEBUG_PRINT_PBIX_AST
32#include <pablo/printer_pablos.h>
33#endif
34
35using namespace re;
36using namespace cc;
37using namespace pablo;
38
39namespace icgrep {
40
41LLVM_Gen_RetVal compile(const Encoding encoding, const std::string input_string, const bool show_compile_time) {
42    RE * re_ast = nullptr;
43    try
44    {
45        re_ast = RE_Parser::parse(input_string);
46    }
47    catch (ParseFailure failure)
48    {
49        std::cerr << "REGEX PARSING FAILURE: " << failure.what() << std::endl;
50        std::cerr << input_string << std::endl;
51        exit(1);
52    }
53
54    #ifdef DEBUG_PRINT_RE_AST
55    //Print to the terminal the AST that was generated by the parser before adding the UTF encoding:
56    std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
57    #endif
58
59    //Optimization passes to simplify the AST.
60    re_ast = RE_Nullable::removeNullablePrefix(re_ast);
61    #ifdef DEBUG_PRINT_RE_AST
62    std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
63    #endif
64
65    re_ast = RE_Nullable::removeNullableSuffix(re_ast);
66    #ifdef DEBUG_PRINT_RE_AST
67    std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
68    #endif
69
70    CC_NameMap nameMap;
71    re_ast = nameMap.process(re_ast);
72
73    #ifdef DEBUG_PRINT_RE_AST
74    std::cerr << "Namer:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
75    #endif
76
77    //Add the UTF encoding.
78    if (encoding.getType() == Encoding::Type::UTF_8) {
79        re_ast = UTF8_Encoder::toUTF8(nameMap, re_ast);
80        #ifdef DEBUG_PRINT_RE_AST
81        //Print to the terminal the AST that was generated by the utf8 encoder.
82        std::cerr << "UTF8-encoder:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
83        #endif
84    }
85
86    // note: system is clumbersome at the moment; this needs to be done AFTER toUTF8.
87    nameMap.addPredefined("LineFeed", makeCC(0x0A));
88
89    re_ast = RE_Simplifier::simplify(re_ast);
90    #ifdef DEBUG_PRINT_RE_AST
91    //Print to the terminal the AST that was generated by the simplifier.
92    std::cerr << "Simplifier:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
93    #endif
94
95    SymbolGenerator symgen;
96    PabloBlock main(symgen);
97
98    CC_Compiler cc_compiler(main, encoding);
99    cc_compiler.compile(nameMap);
100    #ifdef DEBUG_PRINT_PBIX_AST
101    //Print to the terminal the AST that was generated by the character class compiler.
102    std::cerr << "Pablo CC AST:" << std::endl << StatementPrinter::Print_CC_PabloStmts(main.expressions()) << std::endl;
103    #endif
104
105    RE_Compiler re_compiler(main, nameMap);
106    re_compiler.compile(re_ast);
107    #ifdef DEBUG_PRINT_PBIX_AST
108    //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
109    std::cerr << "Final Pablo AST:" << StatementPrinter::Print_CC_PabloStmts(main.expressions()) << ")" << std::endl;
110    #endif
111
112    PabloCompiler pablo_compiler(nameMap, cc_compiler.getBasisBitVars(), encoding.getBits());
113    unsigned long long cycles = 0;
114    double timer = 0;
115    if (show_compile_time)
116    {
117        cycles = get_hrcycles();
118        timer = getElapsedTime();
119    }
120
121    LLVM_Gen_RetVal retVal = pablo_compiler.compile(main);
122    if (show_compile_time)
123    {
124        cycles = get_hrcycles() - cycles;
125        timer = getElapsedTime() - timer;
126        std::cout << "LLVM compile time -  cycles:       " << cycles  << std::endl;
127        std::cout << "LLVM compile time -  milliseconds: " << timer << std::endl;
128    }
129
130    return retVal;
131}
132
133}
Note: See TracBrowser for help on using the repository browser.