-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
132 lines (98 loc) · 4.47 KB
/
Copy pathmain.cpp
File metadata and controls
132 lines (98 loc) · 4.47 KB
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
#include <iostream>
#include <fstream>
#include <chrono>
#include "front/ast/node.h"
#include "middle/abstract_assembler/AbstractAssembler.h"
#include "middle/ast_optimizer/ASTOptimizer.h"
#include "middle/peephole/PeepholeOptimizer.h"
extern DeclarationList *declarations;
extern CommandList *commands;
extern ConstantList *constants;
extern int yyparse();
extern FILE *yyin;
bool warning = false;
int main(int argc, char **argv) {
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
if (argc < 2) {
std::cerr << "[i] Usage: compiler <source> [destination] [optimize=(0|1)]" << std::endl;
return 1;
}
FILE *source = fopen(argv[1], "r");
if (!source) {
std::cerr << "[e] Can't open " << argv[1] << std::endl;
return 1;
}
bool optimize = true, verbose = false;
for (int i = 0; i < argc; i++) {
if (argv[i][0] == '-' && argv[i][1] == 'v') verbose = true;
if (argv[i][0] == '-' && argv[i][1] == 'o') optimize = false;
}
std::cout << "[i] Compiling file " << argv[1] << (optimize ? " with optimization" : " without optimization") << std::endl;
std::cout << "[i] Parsing... " << std::endl;
yyin = source;
yyparse();
std::cout << " [i] done" << std::endl;
if (commands == nullptr) commands = new CommandList();
if (declarations == nullptr) declarations = new DeclarationList();
Program *program = new Program(*declarations, *commands, *constants);
if (verbose) std::cout << "-=- A S T -=-" << std::endl;
if (verbose) std::cout << program->toString() << std::endl;
if (optimize) {
std::cout << "[i] AST Optimization... " << std::endl;
if (verbose) std::cout << std::endl;
ASTOptimizer *astOptimizer = new ASTOptimizer(program);
astOptimizer->optimize(verbose);
if (verbose) std::cout << std::endl;
std::cout << " [i] done" << std::endl;
if (verbose) std::cout << "-=- OPTIMIZED A S T -=-" << std::endl;
if (verbose) std::cout << program->toString() << std::endl;
}
AbstractAssembler *assembler = new AbstractAssembler(*program);
std::cout << "[i] Compiling... " << std::endl;
if (verbose) std::cout << std::endl;
try {
InstructionList &assembled = assembler->assemble(verbose);
std::cout << " [i] done" << std::endl;
if (verbose) std::cout << std::endl << "-=- A S M -=-" << std::endl;
std::ofstream output;
output.open(argv[2] ? argv[2] : "a.out");
for (const auto &ins : assembled.getInstructions()) {
if (!ins->stub) {
if (verbose) std::cout << std::setbase(10) << ins->getAddress() << ": " << ins->toAssemblyCode(true) << std::endl;
if (!optimize) output << ins->toAssemblyCode(true) << std::endl;
}
}
if (optimize) {
std::cout << "[i] ASM Optimization... " << std::endl;
if (verbose) std::cout << std::endl;
PeepholeOptimizer *peepholeOptimizer = new PeepholeOptimizer(assembled);
peepholeOptimizer->optimize(verbose);
assembled.seal(false);
if (verbose) std::cout << std::endl;
std::cout << " [i] done" << std::endl;
if (verbose) std::cout << std::endl << "-=- OPTIMIZED A S M -=-" << std::endl;
for (const auto &ins : assembled.getInstructions()) {
if (!ins->stub) {
if (verbose) std::cout << std::setbase(10) << ins->getAddress() << ": " << ins->toAssemblyCode(true) << std::endl;
output << ins->toAssemblyCode(true) << std::endl;
}
}
output.close();
}
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
if (warning) {
std::cout << "[w] Compiled with warnings in " << std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() << " ms" << std::endl;
} else {
std::cout << "[i] Compiled successfully in " << std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() << " ms" << std::endl;
}
} catch (std::string errorMessage) {
std::cout << " [e] " << errorMessage << std::endl;
std::cout << "[e] Aborting" << std::endl;
return 1;
} catch (char const *errorMessage) {
std::cout << " [e] " << errorMessage << std::endl;
std::cout << "[e] Aborting" << std::endl;
return 1;
}
return 0;
}