-
Notifications
You must be signed in to change notification settings - Fork 0
/
Manager.hpp
101 lines (86 loc) · 2.51 KB
/
Manager.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <string>
#include <unistd.h>
#include "Perser.hpp"
#include "Lexer.hpp"
#include "CodeGen.hpp"
#include "ast/TranslationUnitAST.hpp"
class Manager {
int debug;
std::string input_file;
std::string output_file;
TranslationUnitAST translationUnit;
public:
Manager(int argc,char* argv[]): debug(0){
if(argc == 2){
input_file = argv[1];
} else if(argc > 2){
option(argc, argv);
} else{
std::cerr << "cook: no input files"<< std::endl;
}
LLVMInitializeNativeTarget();
llvm::sys::PrintStackTraceOnErrorSignal();
}
void option(int argc,char* argv[]){
int res = 0;
while((res=getopt(argc, argv,"o:i:d")) != -1){
switch(res){
case 'd':
debug = 1;
break;
case 'i':
input_file = optarg;
break;
case 'o':
output_file = optarg;
break;
}
}
}
void run(){
Lexer* lexer;
Perser* perser;
if(debug){
std::cout<<"Debug option\n";
}
lexer = new Lexer(debug);
lexer->load(input_file);
if(debug){
lexer->put_result();
}
perser = new Perser(lexer->getTokens(), debug);
if(!perser->perse()){
std::cout<<"Perse Error!!\n";
RELEASE(lexer);
RELEASE(perser);
return;
}
TranslationUnitAST* ast = perser->getAST();
CodeGen *codeGen = new CodeGen(debug);
if(!codeGen->codeGen( ast, input_file)){
std::cout<<"CodeGen Error!!\n";
RELEASE(lexer);
RELEASE(perser);
return;
}
llvm::Module &module = codeGen->getModule();
if(module.empty()){
std::cout<<" Module is empty!!\n";
RELEASE(lexer);
RELEASE(perser);
return;
}
llvm::PassManager pm;
pm.add(llvm::createPromoteMemoryToRegisterPass());
std::error_code error;
llvm::StringRef out_filename("do.out");
llvm::raw_fd_ostream raw_stream( out_filename, error, llvm::sys::fs::OpenFlags::F_None );
pm.add(createPrintModulePass( raw_stream));
pm.run(module);
raw_stream.close();
std::cout<<"Complate!!!!\n";
RELEASE(lexer);
RELEASE(perser);
}
};