-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
225 lines (200 loc) · 6.5 KB
/
main.cpp
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "include/Timestamp.h"
using namespace std;
#ifndef COMMON_TYPES_AND_CONSTANTS
#include "include/CommonTypesAndConstants.h"
#endif
#ifndef IRGRAPH_H_
#include "include/IRGraph.h"
#endif
#ifndef PASSES_H_
#include "include/Passes.h"
#endif
#ifndef BUILD_H_
#include "include/build.h"
#endif
LLVMContext GlobalContext;
IRBuilder<> Builder(GlobalContext);
Module * GlobalModule;
std::map<std::string, AllocaInst *> NamedValues;
StructType * burst_struct_type;
extern FILE *yyin;
extern FILE *yyout;
extern int yyparse(IR_Graph& gr);
int main(int argc, char *argv[]) {
if( argc < 3 ) {
cout << "You need to specify at least input and output files." << endl;
exit(0);
}
// Considering non-obligatory arguments
int basis_opt_adr = 3;
// Visualization options
visualization_option option_vis = VIS_NONE;
if( argc > basis_opt_adr) {
if(strcmp(argv[basis_opt_adr],"-v") == 0) {
basis_opt_adr++;
if(argc > basis_opt_adr) {
char * option_params = argv[basis_opt_adr];
if(strcmp(option_params,"ias") == 0)
option_vis = VIS_ALL;
else if(strcmp(option_params,"i--") == 0)
option_vis = VIS_INITIAL;
else if (strcmp(option_params,"-a-") == 0)
option_vis = VIS_ARITH;
else if (strcmp(option_params,"--s") == 0)
option_vis = VIS_STRUCT;
else if (strcmp(option_params,"ia-") == 0)
option_vis = VIS_INITIAL_ARITH;
else if (strcmp(option_params,"i-s") == 0)
option_vis = VIS_INITIAL_STRUCT;
else if (strcmp(option_params,"-as") == 0)
option_vis = VIS_ARITH_STRUCT;
else if (strcmp(option_params,"---") == 0)
option_vis = VIS_NONE;
else
cout << "Error: Non-existent arguments for \'-v\'." << endl;
basis_opt_adr++;
} else {
cout << "Error: No arguments for \'-v\'." << endl;
}
}
}
// Considering output of AST IRs of CPU code and SPU code
ast_IR_option option_ast_IR = AST_IR_NONE;
std::string ast_filename;
if(argc > basis_opt_adr) {
if(strcmp(argv[basis_opt_adr],"-ast") == 0) {
basis_opt_adr++;
if(argc > basis_opt_adr) {
char * option_params = argv[basis_opt_adr];
if(strlen(option_params) == 3) {
char * option_params_main = (char*)malloc(2);
char * option_params_file = (char*)malloc(1);
strncpy(option_params_main,option_params,2);
option_params_main[2]='\0';
strncpy(option_params_file,option_params + 2,1);
option_params_file[1]='\0';
if(strcmp(option_params_main,"as") == 0)
option_ast_IR = AST_IR_ALL;
else if(strcmp(option_params_main,"a-") == 0)
option_ast_IR = AST_IR_CPU;
else if (strcmp(option_params_main,"-s") == 0)
option_ast_IR = AST_IR_SPU;
else if (strcmp(option_params_main,"--") == 0)
option_ast_IR = AST_IR_NONE;
else
cout << "Error: Non-existent arguments for AST printing settings in \'-ast\'." << endl;
size_t flag_pos = 2;
basis_opt_adr++;
if(argc > basis_opt_adr) {
if(strcmp(option_params_file,"f") == 0) {
ast_filename.assign(argv[basis_opt_adr]);
} else if(strcmp(option_params_file,"-") != 0) {
cout << "Error: Non-existent argument for AST file print option in \'-ast\'." << endl;
}
basis_opt_adr++;
}
}
} else {
cout << "Error: No arguments for \'-ast\'." << endl;
}
}
}
// Considering output of asm IRs of CPU code and SPU code
asm_IR_option option_asm_IR = ASM_IR_NONE;
std::string asm_filename;
if(argc > basis_opt_adr) {
if(strcmp(argv[basis_opt_adr],"-asm") == 0) {
basis_opt_adr++;
if(argc > basis_opt_adr) {
char * option_params = argv[basis_opt_adr];
if(strlen(option_params) == 3) {
char * option_params_main = (char*)malloc(2);
char * option_params_file = (char*)malloc(1);
strncpy(option_params_main,option_params,2);
option_params_main[2]='\0';
strncpy(option_params_file,option_params + 2,1);
option_params_file[1]='\0';
if(strcmp(option_params_main,"as") == 0)
option_asm_IR = ASM_IR_ALL;
else if(strcmp(option_params_main,"a-") == 0)
option_asm_IR = ASM_IR_CPU;
else if (strcmp(option_params_main,"-s") == 0)
option_asm_IR = ASM_IR_SPU;
else if (strcmp(option_params_main,"--") == 0)
option_asm_IR = ASM_IR_NONE;
else
cout << "Error: Non-existent arguments for asm IR printing settings in \'-asm\'." << endl;
size_t flag_pos = 2;
basis_opt_adr++;
if(argc > basis_opt_adr) {
if(strcmp(option_params_file,"f") == 0) {
asm_filename.assign(argv[basis_opt_adr]);
} else if(strcmp(option_params_file,"-") != 0) {
cout << "Error: Non-existent argument for asm IR file print option in \'-asm\'." << endl;
}
basis_opt_adr++;
}
}
} else {
cout << "Error: No arguments for \'-asm\'." << endl;
}
}
}
// Considering requirement to create timestamps
int compile_times = 1;
std::string timestamps_filename;
if(argc > basis_opt_adr) {
if(strcmp(argv[basis_opt_adr],"-t") == 0) {
basis_opt_adr++;
if(argc > basis_opt_adr) {
compile_times = atoi(argv[basis_opt_adr]);
basis_opt_adr++;
if(argc > basis_opt_adr) {
timestamps_filename.assign(argv[basis_opt_adr]);
basis_opt_adr++;
}
} else {
cout << "Error: No arguments for \'-t\'." << endl;
}
}
}
TimeStamp * global_timer = new TimeStamp(timestamps_filename);
for(int l = 1; l <= compile_times; l++) {
FILE *fp = fopen(argv[1],"r");
if( !fp ) {
cout << "Could not open the file for reading." << endl;
exit(0);
}
FILE *out_fp = fopen(argv[2],"w");
if( ! out_fp ) {
cout << "Could not open the file for writing." << endl;
exit(0);
}
yyin = fp;
yyout = out_fp;
std::string out_filename(argv[2]);
IR_Graph result_graph;
initialize_for_repeat();
global_timer->startTime(std::string(argv[1]),l,std::string("1 - Parsing"));
int status = yyparse(result_graph);
global_timer->endTime(std::string(argv[1]),l,std::string("1 - Parsing"));
finish_for_repeat();
fclose(fp);
fclose(out_fp);
remove(argv[2]);
if(status == 0) {
status = Compile(option_vis, option_ast_IR, ast_filename, option_asm_IR, asm_filename, &result_graph, out_filename, l, std::string(argv[1]), global_timer);
}
//
// if(status == 0)
// cout << endl << "*************************" << endl << "Compilation completed successfully." << endl;
// else
// cout << endl << "Some errors occured during compilation. Exiting (" << status << ")" << endl;
}
global_timer->printDurations();
return 0;
}