-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompile.cpp
More file actions
256 lines (252 loc) · 6.84 KB
/
compile.cpp
File metadata and controls
256 lines (252 loc) · 6.84 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//
// Created by Dan Evans on 11/15/24.
//
#include <string>
#include <cstring>
#define EXPORT_CS_GLOBAL
#include "cs_global.h"
#define EXPORT_CS_COMPILE
#include "cs_compile.h"
namespace Cstar
{
extern void dumpCode();
extern void dumpSymbols();
extern void dumpArrays();
extern void dumpBlocks();
extern void dumpReals();
extern void showConsoleList(bool);
extern void BLOCK(/*InterpLocal *il, */SYMSET FSYS, bool ISFUN, int LEVEL, int PRT);
extern void ERROR(int);
int MAINFUNC;
void compile()
{
INITCOMPILER();
// strcpy(PROGNAME, "PROGRAM ");
MAINFUNC = -1;
BLOCK(/*il, */DECLBEGSYS, false, 1, Tx);
if (SY != EOFSY)
ERROR(22);
if (BTAB[2].VSIZE + WORKSIZE > STMAX)
ERROR(49);
if (MAINFUNC < 0)
ERROR(100);
else
{
if (MPIMODE)
{
EMIT(9);
EMIT1(18, MAINFUNC);
EMIT1(19, BTAB[TAB[MAINFUNC].REF].PSIZE - 1);
EMIT(69);
}
EMIT1(18, MAINFUNC);
EMIT1(19, BTAB[TAB[MAINFUNC].REF].PSIZE - 1);
}
EMIT(31);
//il->ENDLOC = LC;
LOCATION[LNUM + 1] = LC;
//std::cout << std::endl;
}
}
static void usage(const char *pgm)
{
printf("Cstar (C*) compiler\n");
printf("usage: %s [-h] [-l] [-m] [-o] [-Xabcrs] [file]\n", pgm);
printf(" h display this help and exit\n");
printf(" l display listing on the console\n");
printf(" m set MPI ON\n");
printf(" o write an object file\n");
printf(" file compilation file\n");
printf(" X execution options, combined in any order after X (no spaces)\n");
printf(" a display the array table\n");
printf(" b display the block table\n");
printf(" c display the generated interpreter code\n");
printf(" r display the real constant table\n");
printf(" s display the symbol table\n");
std::exit(1);
}
static bool code_list = false;
static bool block_list = false;
static bool array_list = false;
static bool real_list = false;
static bool symbol_list = false;
static bool object_file = false;
static std::string sig = "CST*";
static std::string sigd = "DISP";
static std::string siga = "ATAB";
static std::string sigb = "BTAB";
static std::string sigc = "CTAB";
static std::string sigs = "SYMT";
static std::string sigk = "CONS";
static std::string sigi = "INIT";
static std::string fto = "csto";
static void doOption(const char *opt, const char *pgm)
{
int ix;
switch (*opt)
{
case 'X':
ix = 1;
while (opt[ix] != '\0')
{
switch (opt[ix])
{
case 'a':
// Cstar::showArrayList(true);
array_list = true;
break;
case 'b':
// Cstar::showBlockList(true);
block_list = true;
break;
case 'c':
// Cstar::showCodeList(true);
code_list = true;
break;
case 'r':
// Cstar::showRealList(true);
real_list = true;
break;
case 's':
// Cstar::showSymbolList(true);
symbol_list = true;
break;
// case 't':
// Cstar::showInstTrace(true);
// break;
default:
break;
}
ix += 1;
}
break;
case 'l':
Cstar::showConsoleList(true);
break;
case 'm':
Cstar::MPIMODE = true;
break;
case 'o':
object_file = true;
break;
default:
usage(pgm);
break;
}
}
static void write_obj(FILE *obj)
{
int obj_last, obj_len;
obj_last = Cstar::LC;
obj_len = sizeof Cstar::CODE[0];
fwrite(sig.c_str(), 1, 4, obj);
fwrite(Cstar::CODE, obj_len, obj_last, obj);
obj_last = 0;
while (obj_last <= LMAX && Cstar::DISPLAY[obj_last] > 0)
{
obj_last += 1;
}
obj_len = sizeof Cstar::DISPLAY[1];
fwrite(sigd.c_str(), 1, 4, obj);
fwrite(&Cstar::DISPLAY[1], obj_len, obj_last, obj);
// static std::string siga = "ATAB";
// static std::string sigb = "BTAB";
// static std::string sigc = "CTAB";
// static std::string sigs = "SYMT";
// static std::string sigk = "CONS";
// static std::string sigi = "INIT";
}
std::string obj_name(const char *fn)
{
int ln, ix;
std::string nm = fn;
ix = ln = (int)nm.length();
while (--ix > 0)
{
if (fn[ix] == '.')
{
nm.replace(ix + 1, ln, fto);
// objnm = (char *)std::malloc(ln + 1 + 5); // 4 byte file type plus null
break;
}
}
return nm ;
}
static void cs_error(const char *p, int ern = 0, const char *p2 = "")
{
//std::cerr << p << " " << p2 << "\n";
fprintf(stderr, "cs%03d %s %s\n", ern, p, p2);
std::exit(1);
}
int main(int argc, char *argv[])
{
int ix;
FILE *cfile, *lfile, *ofile;
char *from_file = nullptr;
std::string objnm;
const char *list_file = "LISTFILE.TXT";
#ifdef MAC
__sFILE *stdin_save = nullptr;
Cstar::STDOUT = __stdoutp;
#else
FILE *stdin_save = nullptr;
Cstar::STDOUT = stdout;
#endif
if (argc == 1)
usage(argv[0]);
for (ix = 1; ix < argc; ++ix)
{
if (argv[ix][0] == '-')
{
doOption(&argv[ix][1], argv[0]);
}
else if (from_file == nullptr)
{
from_file = argv[ix];
}
else
usage(argv[0]);
}
cfile = fopen(from_file, "r");
if (cfile == nullptr)
{
cs_error("cannot open input file", 1, from_file);
return 1;
}
Cstar::SRC = cfile;
lfile = fopen(list_file, "w");
if (lfile == nullptr)
{
cs_error("cannot open ", 1, list_file);
return 1;
}
fprintf(lfile, "%s\n", from_file);
Cstar::LIS = lfile;
Cstar::compile();
fclose(lfile);
fclose(cfile);
// SRCOPEN = true;
if (code_list)
Cstar::dumpCode();
if (block_list)
Cstar::dumpBlocks();
if (symbol_list)
Cstar::dumpSymbols();
if (array_list)
Cstar::dumpArrays();
if (real_list)
Cstar::dumpReals();
if (object_file)
{
objnm = obj_name(from_file);
ofile = fopen(objnm.c_str(), "w");
if (ofile == nullptr)
{
cs_error("cannot open object file ", 1, objnm.c_str());
return 1;
}
write_obj(ofile);
fclose(ofile);
}
return 0;
}