-
Notifications
You must be signed in to change notification settings - Fork 1
/
winxedxc.winxed
424 lines (377 loc) · 10.5 KB
/
winxedxc.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
#! winxed
// winxedxc.winxed
// Winxed extern compiler
/*
This is the frontend. It parses options, load winxed compiler
and modules, call external compilers and linkers and eventually
executes the result.
This version works only with g++.
*/
$include_const "iglobals.pasm";
$include_const "libpaths.pasm";
$include "winxedxx.winxhead";
namespace WinxedXC
{
//**************************************************************
// Helper functions
function splitExtension(string filename)
{
/*
Get the extension from file name.
Return null string if no dot, empty string if the dot is the last
character.
*/
for (int pos = length(filename); pos > 0; --pos) {
string c = filename[pos - 1];
switch (c) {
case "/":
return filename, string(null);
case ".":
if (pos > 0)
return substr(filename, 0, pos - 1), substr(filename, pos);
}
}
return filename, string(null);
}
function is_relative(string path)
{
int l = length(path);
if (l == 0)
return true;
string c = path[0];
switch (c) {
case "/":
return false;
case ".":
if (l == 1)
return false;
c = path[1];
return c != "." && c != "/";
default:
return true;
}
}
function load_config(string filename)
{
var json = load_language("data_json");
var file = open(filename);
file.encoding("utf8");
var code = json.compile(file.readall());
file.close();
return code();
}
inline check_exit_code(int code) return int
{
return (code >> 8) & 0xFF;
}
//**************************************************************
function get_winxed_compiler()
{
// Load language module and compreg it, exit with error if not found.
var winxed = null;
try
winxed = load_language("winxed");
catch () { }
if (winxed == null) {
cry("Cannot load language");
exit(1);
}
return winxed;
}
function load_winxed_stage(string stage)
{
// Load and compreg a winxed stage
var module;
try
module = load_packfile(stage);
catch () { }
if (module == null) {
cry("Cannot load stage '", stage, "'");
exit(1);
}
try {
for (var loadsub in module.subs_by_tag("load"))
loadsub();
}
catch (e) {
cry("Error loading stage '", stage, "':");
cry(e.message);
exit(1);
}
var winxed;
try
winxed = new ["Winxed", "Compiler", "WinxedHLL" ];
catch () { }
if (winxed == null) {
cry("Winxed compiler not found in '", stage, "'");
exit(1);
}
return winxed;
}
class WinxedCompiler
{
var winxed;
function WinxedCompiler(string stage)
{
self.winxed = stage == null ?
get_winxed_compiler() :
load_winxed_stage(stage);
}
function version_string()
{
return self.winxed.version_string();
}
function compile(string sourcefile, var outhandle)
{
// Compile a winxed source to pir.
var parsed = self.winxed.compile_from_file(sourcefile,
"parse":[named("target")]
);
:WinxedXX.Output output(outhandle);
output.emit(parsed);
}
}
//**************************************************************
/*
C++ compiler wrapper.
Methods:
compile - compile a C++ source file to an object file.
link - link one or more object files with the winxedxx runtime.
*/
class CompilerCxx
{
var includedir;
var dynlib;
function CompilerCxx(var config)
{
if (config != null) {
var includedir = config["I"];
if (includedir != null)
self.includedir = includedir;
var dynlib = config["dynlib"];
if (dynlib != null)
self.dynlib = dynlib;
}
}
function compile(string cxxfile, string outfile)
{
string command[];
push(command, "g++");
push(command, "-Wall");
push(command, "-O2");
var includedir = self.includedir;
if (includedir != null)
push(command, "-I" + string(includedir));
push(command, "-fPIC");
push(command, "-o");
push(command, outfile);
push(command, "-c");
push(command, cxxfile);
int r = spawnw(command);
return check_exit_code(r);
}
function link(string outfile, var objs)
{
string args[] = [
"g++",
"-g",
"-o", outfile
];
for (string obj in objs)
push(args, obj);
if (self.dynlib != null)
push(args, self.dynlib);
push(args, "-ldl");
int r = spawnw(args);
return check_exit_code(r);
}
function linkshared(string outfile, var objs)
{
string args[] = [
"g++",
"-g",
"-shared",
"-o", outfile
];
for (string obj in objs)
push(args, obj);
if (self.dynlib != null)
push(args, self.dynlib);
push(args, "-ldl");
int r = spawnw(args);
return check_exit_code(r);
}
}
//**************************************************************
$load "Getopt/Obj.pbc";
class Options : ["Getopt", "Obj" ]
{
var name;
var options;
var opts;
function Options(var argv)
{
self.options = [
[ "o=s", "Object name" ],
[ "target=s", "Set target type: cxx, exe or run. Default is cxx." ],
[ "stage=s", "Use a compiler stage instead of default." ],
[ "I=s", "Add to parrot include search path" ],
[ "config=s", "Configuration file" ],
[ "verbose=i", "Talk a lot" ]
];
for (var o in self.options)
self.push_string(o[0]);
self.notOptStop(1);
self.name = argv.shift();
self.opts = self.get_options(argv);
}
function getstring(string option, string default_value[optional])
{
var value = self.opts[option];
return value != null ? string(value) : default_value;
}
function getint(string option, int default_value[optional])
{
var value = self.opts[option];
return value != null ? int(value) : default_value;
}
}
} // namespace WinxedXC
//**************************************************************
function main [main](var argv)
{
using namespace WinxedXC;
:Options cloptions(argv);
string target = cloptions.getstring("target", "run");
string outfile = cloptions.getstring("o");
string stage = cloptions.getstring("stage");
string incs = cloptions.getstring("I");
int verbose = cloptions.getint("verbose");
string configfile = cloptions.getstring("config");
// Load config from json file or use defaults.
var config = configfile != null ?
load_config(configfile) :
{
"cxx" : {
"dynlib" : "winxedxx.so",
"I" : "."
}
};
var configwinxed = config["winxed"];
if (configwinxed != null) {
string libs = configwinxed["L"];
if (libs != null) {
var interp = getinterp();
var lpaths = interp[IGLOBALS_LIB_PATHS];
var pathlibs = lpaths[PARROT_LIB_PATH_LIBRARY];
pathlibs.push(libs);
}
}
switch (target) {
case "cxx":
case "obj":
case "shared":
case "exe":
case "run":
break;
default:
cry("Invalid target: ", target);
exit(1);
}
int argc = elements(argv);
if (argc < 1)
throw Error("No source files");
if (incs != null) {
var interp = getinterp();
var lpaths = interp[IGLOBALS_LIB_PATHS];
var pathinc = lpaths[PARROT_LIB_PATH_INCLUDE];
pathinc.push(string(incs));
}
string cxxfiles[];
string objfiles[];
var winxed = null;
while (elements(argv) > 0) {
string sourcefile = argv.shift();
if (sourcefile == "--")
break;
:(string barename, string ext) = splitExtension(sourcefile);
switch (ext) {
case "cxx":
push(cxxfiles, sourcefile);
break;
case "o":
push(objfiles, sourcefile);
break;
case "winxed":
default:
if (winxed == null) {
if (verbose)
say("Loading winxed");
winxed = new WinxedXC.WinxedCompiler(stage);
if (verbose)
say("Using winxed ", winxed.version_string());
if (verbose)
say("Loading winxedxx");
load_bytecode("winxedxx.pbc");
}
if (verbose)
say("Compiling winxed: ", sourcefile);
string cxxfile = barename + ".cxx";
var cxxout = open(cxxfile, "w");
winxed.compile(sourcefile, cxxout);
cxxout.close();
push(cxxfiles, cxxfile);
break;
}
}
if (target == "cxx")
exit(0);
int r; // Result code from external calls
//--------------------------------------------------
// Compile the generated C++ to obj.
var cxxconfig = config["cxx"];
:CompilerCxx cxx(cxxconfig);
for (string cxxfile in cxxfiles) {
if (verbose)
say("Compiling C++: ", cxxfile);
string objfile = substr(cxxfile, 0, length(cxxfile) - 4) + ".o";
r = cxx.compile(cxxfile, objfile);
if (r) {
cry("Compile ", cxxfile, " failed");
exit(r);
}
push(objfiles, objfile);
}
if (target == "obj")
exit(0);
//--------------------------------------------------
// Link executable
if (outfile == null) {
// Let's be traditional:
outfile = "a.out";
}
if (verbose)
say("Linking: ", outfile);
if (target == "exe" || target == "run")
r = cxx.link(outfile, objfiles);
else
r = cxx.linkshared(outfile, objfiles);
if (r) {
cry("Link '", outfile, "' failed");
exit(r);
}
if (target == "exe" || target == "shared")
exit(0);
//--------------------------------------------------
// Run
if (verbose)
say("Run: ", outfile, " ", join(" ", argv));
if (is_relative(outfile))
outfile = "./" + outfile;
string cmd[] = [ outfile ];
for (string arg in argv)
push(cmd, arg);
r = check_exit_code(spawnw(cmd));
exit(r);
}
// End