source: icGREP/icgrep-devel/icgrep/re_compiler.cpp @ 3914

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

Updates for icgrep-0.9: re simplifications, re names, replimit mods, debugged while loops

File size: 4.1 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#include "re_compiler.h"
8
9RE_Compiler::RE_Compiler(){}
10
11LLVM_Gen_RetVal RE_Compiler::compile(bool show_compile_time,
12                                     bool ascii_only,
13                                     std::string basis_pattern,
14                                     std::string gensym_pattern,
15                                     UTF_Encoding encoding,
16                                     std::string input_string)
17{
18
19    ParseResult* parse_result = RE_Parser::parse_re(input_string);
20
21    RE* re_ast = 0;
22    if (ParseSuccess* success = dynamic_cast<ParseSuccess*>(parse_result))
23    {
24        re_ast = success->getRE();
25    }
26    else if (ParseFailure* failure = dynamic_cast<ParseFailure*>(parse_result))
27    {
28        std::cout << failure->getErrorMsg() << std::endl;
29        exit(1);
30    }
31    else
32    {
33        std::cout << "An unexepected parser error has occured!" << std::endl;
34        exit(1);
35    }
36
37    //Print to the terminal the AST that was generated by the parser before adding the UTF encoding:
38    //std::cout << "\nParser:\n" + Printer_RE::PrintRE(re_ast) + "\n" << std::endl;
39
40    //Add the UTF encoding.
41    if (!ascii_only)
42    {
43        if (encoding.getName().compare("UTF-8") == 0)
44        {
45            re_ast = UTF8_Encoder::toUTF8(re_ast);
46        }
47        else
48        {
49            std::cout << "Invalid encoding!" << std::endl;
50            exit(1);
51        }
52    }
53
54    //Print to the terminal the AST that was generated by the utf8 encoder.
55    //std::cout << "\nUTF8-encoder:\n" + Printer_RE::PrintRE(re_ast) + "\n" << std::endl;
56
57    //Optimization passes to simplify the AST.
58    re_ast = RE_Simplifier::simplify(RE_Nullable::removeNullableSuffix(RE_Nullable::removeNullablePrefix(re_ast)));
59
60    //Print to the terminal the AST that was generated by the simplifier.
61    //std::cout << "\nSimplifier:\n" + Printer_RE::PrintRE(re_ast) + "\n" << std::endl;
62
63    //Map all of the unique character classes in order to reduce redundancy.
64    std::map<std::string, RE*> re_map;
65    re_ast = RE_Reducer::reduce(re_ast, re_map);
66
67    //Print to the terminal the AST with the reduced REs.
68    //std::cout << "\nReducer:\n" + Printer_RE::PrintRE(re_ast) + "\n" << std::endl;
69
70    //Build our list of predefined characters.
71    std::list<CC*> predefined_characters;
72    CC* cc_lf = new CC("lex.cclf", '\n');
73    std::string lf_ccname = cc_lf->getName();
74    re_map.insert(make_pair(lf_ccname, cc_lf));
75
76    CC_Compiler cc_compiler(encoding);
77    std::list<PabloS*> cc_stmtsl = cc_compiler.compile(basis_pattern, gensym_pattern, re_map, predefined_characters);
78
79    //Print to the terminal the AST that was generated by the character class compiler.
80    //std::cout << "\n" << "(" << StatementPrinter::Print_CC_PabloStmts(cc_stmtsl) << ")" << "\n" << std::endl;
81
82    Pbix_Compiler pbix_compiler(lf_ccname);
83    CodeGenState cg_state = pbix_compiler.compile(re_ast);
84
85    //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
86    //std::cout << "\n" << "(" << StatementPrinter::PrintStmts(cg_state) << ")" << "\n" << std::endl;
87
88    //Print a count of the Pablo statements and expressions that are contained in the AST from the pbix compiler.
89    //std::cout << "\nPablo Statement Count: " << Pbix_Counter::Count_PabloStatements(cg_state.stmtsl) <<  "\n" << std::endl;
90
91    LLVM_Generator irgen(basis_pattern, lf_ccname, encoding.getBits());
92
93    unsigned long long cycles = 0;
94    double timer = 0;
95    if (show_compile_time)
96    {
97        cycles = get_hrcycles();
98        timer = getElapsedTime();
99    }
100
101    LLVM_Gen_RetVal retVal = irgen.Generate_LLVMIR(cg_state, cc_stmtsl);
102    if (show_compile_time)
103    {
104        cycles = get_hrcycles() - cycles;
105        timer = getElapsedTime() - timer;
106        std::cout << "LLVM compile time -  cycles:       " << cycles  << std::endl;
107        std::cout << "LLVM compile time -  milliseconds: " << timer << std::endl;
108    }
109
110    return  retVal;  //irgen.Generate_LLVMIR(cg_state, cc_stmtsl);
111}
112
Note: See TracBrowser for help on using the repository browser.