-
Notifications
You must be signed in to change notification settings - Fork 7
/
winxed.winxed
441 lines (409 loc) · 12 KB
/
winxed.winxed
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#! winxed
// Winxed compiler driver.
// Parses command line options, calls the compiler backend
// and eventually runs the generated program.
$load "Getopt/Obj.pbc";
$include_const "iglobals.pasm";
$include_const "libpaths.pasm";
$include_const "except_severity.pasm";
function extname(string filename, string ext)
{
// Strip a possible .winxed extension from filename,
// add ext, and return the result.
int l = length(filename);
return (l > 7 && substr(filename, -7) == ".winxed") ?
substr(filename, 0, l - 7) + ext :
filename + ext;
}
// Use the OS pmc for file operations.
function getOS()
{
var os;
try {
loadlib("os");
os = new ["OS"];
}
catch() { }
return os;
}
class WinxedDriverOptions : ["Getopt", "Obj"]
{
var name;
var options;
var opts;
function WinxedDriverOptions(var argv, int unused)
{
var options = [
[ "stage=s", "Compiler stage to use" ],
[ "c", "Compile only. Same as --target=pir" ],
[ "e=s", "Evaluate" ],
[ "o=s", "Object name" ],
[ "L=s", "Add to parrot library search path" ],
[ "I=s", "Add to parrot include search path" ],
[ "X=s", "Add to parrot dynext search path" ],
[ "debug", "Set debug mode" ],
[ "noan", "No code annotations" ],
[ "nowarn", "No warnings" ],
[ "target=s", "Set target type: pir, pbc or run. Default is run." ],
[ "help", "Show this help" ],
[ "version", "Show version and exit" ]
];
self.options = options;
for (var o in options)
self.push_string(o[0]);
self.notOptStop(1);
self.name = argv.shift();
self.opts = self.get_options(argv);
}
function getbool(string option)
{
var opts = self.opts;
var value = opts[option];
return value != null;
}
function getstring(string option, string default[optional])
{
var opts = self.opts;
var value = opts[option];
return value != null ? string(value) : default;
}
function showhelp()
{
say("Usage: ", self.name, " [options] [program] [args]");
say(" Available options:");
int l= 0;
int i;
var o;
for (o in self.options) {
i = length(o[0]);
if (i > l)
l = i;
}
l = l + 4;
string filler = " ";
for (o in self.options) {
string s= o[0];
if (length(s) > 1 && substr(s, 1, 1) != "=")
s = "--" + s;
else
s = "-" + s;
say(" ", s, substr(filler, 0, l - length(s)), "-> ", o[1]);
}
}
}
function driver_main(var argv)
{
const int
DEFAULT_STAGE = 3,
CUSTOM_STAGE = -1;
var options = new WinxedDriverOptions(argv, 0);
int help = options.getbool("help");
int version = options.getbool("version");
string opt_stage = options.getstring("stage");
int compileonly = options.getbool("c");
int debug = options.getbool("debug");
int noan = options.getbool("noan");
int nowarn = options.getbool("nowarn");
string eval = options.getstring("e");
string libs = options.getstring("L");
string incs = options.getstring("I");
string dyns = options.getstring("X");
string obj = options.getstring("o", "");
string target = options.getstring("target");
if (help) {
options.showhelp();
return;
}
var env = new [ "Env" ];
// Path for stage pbc files
string winxedpath = env["WINXED_PATH"];
int stage = DEFAULT_STAGE;
if (opt_stage == null) {
string envstage = env["WINXED_STAGE"];
if (envstage != "")
opt_stage = envstage;
}
if (opt_stage != null) {
if (opt_stage.is_integer(opt_stage))
stage = opt_stage;
else
stage = CUSTOM_STAGE;
}
if (version && (stage == 0 || stage == 1)) {
cry("--version not available in stages 0 and 1");
exit(1);
}
const int
TargetDefault = 0,
TargetRun = 1,
TargetPir = 2,
TargetPbc = 3,
TargetConst = 4;
int targettype = TargetDefault;
if (target != null) {
switch (target) {
case "pir":
targettype = TargetPir;
break;
case "pbc":
targettype = TargetPbc;
break;
case "run":
targettype = TargetRun;
break;
case "include":
targettype = TargetConst;
break;
default:
throw "Invalid target: " + target;
}
}
if (compileonly) {
if (targettype != TargetDefault)
throw "Conflicting options";
targettype = TargetPir;
}
if (targettype == TargetDefault)
targettype = TargetRun;
string srcfile;
if (eval == null && ! version) {
int argc= argv;
if (argc < 1) {
say("ERROR: No program specified");
options.showhelp();
return;
}
srcfile = argv.shift();
}
string pirfile;
if (obj != "" && targettype == TargetPir || targettype == TargetConst)
pirfile = obj;
else {
if (eval != null)
pirfile = "__eval__.pir";
else
pirfile = extname(srcfile, ".pir");
}
var os = getOS();
// Call backend compiler
string winxedbin;
switch (stage) {
case 0:
winxedbin = "winxedst0";
break;
case 1:
winxedbin = "winxedst1";
break;
case 2:
winxedbin = "winxedst2";
break;
case 3:
winxedbin = "winxedst3";
break;
case CUSTOM_STAGE:
winxedbin = opt_stage;
break;
default:
throw "Invalid stage " + stage + ". Must be 0, 1, 2 or 3 .";
}
int retval = 0;
int use_pbc = stage > 1 || stage == CUSTOM_STAGE;
if (use_pbc) {
// Set the -I value now so the $include directive can use it.
if (incs != null) {
var interp = getinterp();
var lpaths = interp[IGLOBALS_LIB_PATHS];
var pathlib = lpaths[PARROT_LIB_PATH_INCLUDE];
pathlib.push(string(incs));
}
try {
load_bytecode(winxedbin + ".pbc");
} catch () { }
var compiler = compreg("winxed");
if (compiler == null) {
if (winxedpath != null && winxedpath != "") {
winxedbin = winxedpath + "/" + winxedbin;
try {
load_bytecode(winxedbin + ".pbc");
}
catch (e) {
if (__DEBUG__)
cry("Cannot load ", winxedbin, ": ", e["message"]);
}
compiler = compreg("winxed");
}
if (compiler == null)
throw "Cannot load stage " + winxedbin;
}
if (version) {
var ver = compiler.version_string();
if (ver == null)
ver = "CANNOT GET VERSION";
say(ver);
exit(0);
}
string ctarget = targettype == TargetConst ? "include" : "pir";
var opts = {
"target": ctarget,
"debug": debug,
"noan": noan,
"nowarn": nowarn
};
string pircode;
if (eval == null) {
var output;
var file;
if (pirfile == "-" &&
(targettype == TargetPir || targettype == TargetConst))
output = getstdout();
else {
file = open(pirfile, "w");
output = file;
}
opts["output"] = output;
compiler.compile_from_file(srcfile, opts:[named,flat]);
if (file != null)
file.close();
}
else {
eval = "function main[main](var argv){" + eval + ";}";
pircode = compiler.compile(eval, opts:[named,flat]);
if (pirfile == "-" &&
(targettype == TargetPir || targettype == TargetConst))
print(pircode);
else {
var file = open(pirfile, "w");
file.print(pircode);
file.close();
}
}
}
else {
string cmd[]; // Command to execute
if (stage == 1) {
cmd.push("parrot");
cmd.push("winxedst1.pbc");
}
else {
// First look in WINXED_PATH.
// If not, simple heuristic: if the compiler is in the current
// directory and has no extension, use it.
// If not, assume it's in the PATH or we are on Windows.
var check = null;
if (winxedpath != null && winxedpath != "" &&os != null) {
string checkbin = winxedpath + "/" + winxedbin;
check = os.stat(checkbin);
if (check == null) {
checkbin = winxedpath + "\\" + winxedbin + ".exe";
check = os.stat(checkbin);
}
if (check != null)
winxedbin = checkbin;
}
if (check == null) {
if (os != null)
check = os.stat(winxedbin);
if (check != null)
winxedbin = "./" + winxedbin;
}
cmd.push(winxedbin);
}
if (targettype == TargetConst)
throw "target include unavailable in stage 0";
if (debug)
cmd.push("--debug");
if (noan)
cmd.push("--noan");
if (nowarn && stage != 0)
cmd.push("--nowarn");
if (eval != null) {
cmd.push("-e");
cmd.push(eval);
}
cmd.push("-o");
cmd.push(pirfile);
if (eval == null)
cmd.push(srcfile);
retval = spawnw(cmd);
retval = (retval >> 8) & 0xFF;
}
if (retval)
exit(retval);
string runit[] = [ "parrot" ];
switch (targettype) {
case TargetRun:
// Execute result
if (libs != null) {
runit.push("-L");
runit.push(libs);
}
if (incs != null) {
runit.push("-I");
runit.push(incs);
}
if (dyns != null) {
runit.push("-X");
runit.push(dyns);
}
runit.push(pirfile);
for (string a in argv)
runit.push(a);
retval = spawnw(runit);
// Delete the generated pir file.
try {
os.rm(pirfile);
}
catch (e)
{
cry("WARNING: cannot delete temporary PIR file: ", e["message"]);
}
break;
case TargetPbc:
// Call parrot to ceate the pbc
string objfile;
if (obj != "")
objfile = obj;
else
objfile = extname(srcfile, ".pbc");
runit.push("-o");
runit.push(objfile);
runit.push(pirfile);
retval = spawnw(runit);
// Delete the generated pir file.
try {
os.rm(pirfile);
}
catch (e)
{
cry("WARNING: cannot delete temporary PIR file: ", e["message"]);
}
break;
default:
; // Otherwise, nothing to do.
}
retval = (retval >> 8) & 0xFF;
exit(retval);
}
function main [main] (var argv)
{
int r;
try {
driver_main(argv);
}
catch (e) {
int severity = e["severity"];
if (severity == EXCEPT_EXIT) {
int exit_code = e["exit_code"];
exit(exit_code);
}
int type = e["type"];
var payload = e["payload"];
if (type == __WINXED_ERROR__ && payload != null)
cry(payload.filename, ":", payload.line, ": ", payload.message);
else
cry(e["message"]);
r = 1;
}
exit(r);
}
// End