-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
209 lines (196 loc) · 5.19 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
#include "stdafx.h"
bool flg_lexonly = false;
bool flg_syxonly = false;
bool flg_tbl = false;
bool flg_opt = false;
bool flg_no_dag = false;
bool flg_no_reduce = false;
bool flg_debug = false;
bool flg_glbreg = false;
istream *src_input = nullptr;
ostream *lex_output = nullptr;
ostream *syx_output = nullptr;
ostream *tbl_output = nullptr;
ostream *asm_output = nullptr;
void usage()
{
cout << "Usage: hcc [-c FILENAME|-l FILENAME|-s FILENAME|" << endl
<< " -t FILENAME] [-o] [-D] [-R] [-r] [-d] SOURCEFILE" << endl
<< endl
<< " Read extended C0 source code from SOURCEFILE file and compile" << endl
<< " to MASM source code. The generated code will be output into" << endl
<< " std-out by default." << endl
<< endl
<< " -c FILENAME" <<endl
<< " Output the generated code to FILENAME. Assign FILENAME as - to" << endl
<< " indicate std-out." << endl
<< endl
<< " -l FILENAME" <<endl
<< " Only do lexical analysis and output the result to FILENAME." << endl
<< " Assign FILENAME as - to indicate std-out." << endl
<< endl
<< " -s FILENAME" <<endl
<< " Only do syntax analysis and output the result to FILENAME." << endl
<< " Assign FILENAME as - to indicate std-out." << endl
<< endl
<< " -t FILENAME" <<endl
<< " Do semantic analysis and dump the relevant tables to FILENAME." << endl
<< " Assign FILENAME as - to indicate std-out." << endl
<< endl
<< " -o Perform optimization." << endl
<< " -D Do NOT use DAG to simplify repeated expression." << endl
<< " -R Do NOT use reduce code using data flow, redundant deletion, copy-." << endl
<< " spread, or constant combination." << endl
<< " -r Use global register." << endl
<< " -d Output verbose debug information." << endl
<< endl;
exit(EXIT_FAILURE);
}
istream *open_input(string fname)
{
//from std-in
if(fname == "-")
return &cin;
//from file
istream *inputfs = new ifstream(fname);
if(*inputfs)
return inputfs;
else
{
cerr << "ERROR: Could not open input file." << endl;
exit(EXIT_FAILURE);
}
}
ostream *open_output(string fname)
{
//to std-out
if(fname == "-")
return &cout;
//to file
ostream *outputfs = new ofstream(fname);
if(*outputfs)
return outputfs;
else
{
cerr << "ERROR: Could not open output file." << endl;
exit(EXIT_FAILURE);
}
}
int main(int argc, char **argv)
{
set_terminate(handler);
//start parsing argument
int n=1;
if(argc < 2)
usage();
while(n<argc)
{
string arg(argv[n++]);
if(arg == "-c")
{
if(flg_syxonly || flg_lexonly || asm_output!=nullptr || n>=argc)
usage();
arg = string(argv[n++]);
asm_output = open_output(arg);
}
else if(arg == "-l")
{
if(lex_output!=nullptr || n>=argc)
usage();
arg = string(argv[n++]);
lex_output = open_output(arg);
flg_lexonly = true;
}
else if(arg == "-s")
{
if(flg_lexonly || syx_output!=nullptr || n>=argc)
usage();
arg = string(argv[n++]);
syx_output = open_output(arg);
flg_syxonly = true;
}
else if(arg == "-t")
{
if(flg_syxonly || flg_lexonly || tbl_output!=nullptr || n>=argc)
usage();
arg = string(argv[n++]);
tbl_output = open_output(arg);
flg_tbl = true;
}
else if(arg == "-o")
{
if(flg_opt)
usage();
flg_opt = true;
}
else if(arg == "-D")
{
if(flg_no_dag)
usage();
flg_no_dag = true;
}
else if(arg == "-R")
{
if(flg_no_reduce)
usage();
flg_no_reduce = true;
}
else if(arg == "-r")
{
if(flg_glbreg)
usage();
flg_glbreg = true;
}
else if(arg == "-d")
{
if(flg_debug)
usage();
flg_debug = true;
}
else
{
if(src_input != nullptr)
usage();
src_input = open_input(arg);
}
}
if(src_input==nullptr)
src_input = open_input("-");
if(asm_output==nullptr)
asm_output = open_output("-");
//start working
//lexical
lex_init();
if(flg_lexonly)
{
lex_dump();
return 0;
}
//syntax and semantics
syx_init();
program();
if(flg_syxonly)
return 0;
//optimization
if(flg_opt)
{
optimize();
//TODO
if(flg_tbl)
{
tbl_dump();
return 0;
}
genasm();
}
else
{
if(flg_tbl)
{
tbl_dump();
return 0;
}
genasm();
}
return 0;
}