-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcc.cpp
72 lines (68 loc) · 1.86 KB
/
rcc.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
/**
* ===================
* RCC - RC16 COMPILER
* ===================
*
* MAIN
* Davide Della Giustina
* 05/10/2019
*/
#include "src/main.hpp"
#include "src/microops.cpp"
#include "src/isa.cpp"
#include "src/parser.cpp"
// Prints usage help.
// @return String with usage help.
string help() {
oss os;
os << "Usage: rcc [options]" << nl <<
"Options:" << nl <<
" -i <arg> Input file to be compiled [REQUIRED]." << nl <<
" -o <arg> Output file name." << nl <<
" -h Print this help.";
return os.str();
}
// Compile a program into a binary file.
// @param src Source filename.
// @param dst Destination filename.
void compilePrg(const string &src, const string &dst) {
ofs bin(dst);
// Header
bin << "v2.0 raw" << nl;
// Init instructions
bin << SET(A, 0) << " " << EXC(NOT, false, false) << " " << MOVREG(OUT, SP) << nl; // SP = mem_estk (0xffff)
bin << SET(A, 32) << " " << SET(B, 9) << " " << EXC(LSL, false, false) << " " << MOVREG(OUT, LR) << nl; // LR = mem_iprg (0x4000)
bin << MOVREG(OUT, PC) << nl; // PC = mem_iprg (0x4000)
// Parse program
bin << parsePrg(src) << nl;
bin << HLT();
bin.close();
}
// Main.
int main(int argc, char* argv[]) {
string ifile = "", ofile = "";
// Parse command line options
int opt;
while ((opt = getopt(argc, argv, "i:o:h")) != -1) {
switch (opt) {
case 'i':
ifile = string(optarg);
break;
case 'o':
ofile = string(optarg);
break;
case 'h':
cout << help() << nl;
return 0;
default:
cerr << help() << nl;
return -1;
}
}
if (ifile.compare("") == 0) { cerr << "No input file given." << nl; return -1; }
if (!fexists(ifile)) { cerr << "Given file does not exist or is unaccessible." << nl; return -1; } // Check ifile existence and accessibility
if (ofile.compare("") == 0) ofile = "a.bin"; // If ofile not given
// Compile given program
compilePrg(ifile, ofile);
return 0;
}