-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmainCompiler.js
More file actions
executable file
·334 lines (287 loc) · 11.1 KB
/
mainCompiler.js
File metadata and controls
executable file
·334 lines (287 loc) · 11.1 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
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
#!/usr/bin/env node
// Main Compiler Version 2.0
// January 2023
const fs = require("fs"),
child_process = require("child_process"),
crypto = require("crypto"),
path = require("path"),
jam = require("./lib/ohm/jamscript/jam"),
os = require('os'),
JSZip = require("jszip");
const USAGE = `
jamc [options] <inputs>
JAMScript compiler toolchain for creating JAMScript executable (.jxe) from
source files. It takes the C-side and J-side source files and any data files
(given as a directory) that have to be packaged with the executable.
Options:
-h (--help) shows the usage of the compiler (this information).
-d (--debug) sets the debug mode in which address sanitizer is linked to the C side
-p (--preprocess) launch the preprocessor only
-o (--output) sets the output filename
-V (--version) prints version information
-v (--verbose) turns on verbose mode
-vv (--extrav) turns on extra verbosity mode
-a (--analyze) analyzes the source and outputs call graph
Inputs:
Exactly one C side file (with .c extension) and exactly one J side file (with .js extension).
Optionally a directory with data files.
`
// global parameters
let tmpDir = "/tmp/jam-" + randomValueHex(20);
let homeDir = os.homedir();
// Core of the compiler
let args = processArgs();
validateArgs(args);
runMain(args);
// End of the core
function ansiiGreen(text) {
return `\x1b[32m${text}\x1b[0m`;
}
function ansiiYellow(text) {
return `\x1b[33m${text}\x1b[0m`;
}
function ansiiRed(text) {
return `\x1b[31m${text}\x1b[0m`;
}
// Process command arguments and return a structure
function processArgs() {
let args = process.argv.slice(2);
let conf = {
cPath: undefined,
jsPath: undefined,
outPath: undefined,
supFiles: [],
debug: false,
preprocessOnly: false,
verbosity: 0,
callGraphFlag: false
};
for (var i = 0; i < args.length; i++) {
if (args[i].charAt(0) === "-") {
if (args[i] === "-d" || args[i] === "--debug") {
conf.debug = true;
} else if (args[i] === "-h" || args[i] === "--help") {
console.log(USAGE);
process.exit(0);
} else if (args[i] === "-o" || args[i] === "--output") {
conf.outPath = args[i + 1];
i = i + 1;
} else if (args[i] === "-p" || args[i] === "--preprocess") {
conf.preprocessOnly = true;
} else if (args[i] === "-V" || args[i] === "--version") {
console.log(require("./package.json").version);
process.exit(0);
} else if (args[i] === "-v" || args[i] === "--verbose") {
conf.verbosity = 1;
} else if (args[i] === "-vv" || args[i] === "--extrav") {
conf.verbosity = 2;
} else if (args[i] === "-a" || args[i] === "--analyze") {
// Generate call graph files
conf.callGraphFlag = true;
}
} else {
let inputPath = args[i];
let extension = path.extname(inputPath);
if (extension === ".js") {
if (conf.jsPath === undefined) {
conf.jsPath = inputPath;
if (conf.outPath === undefined) {
conf.outPath = path.basename(inputPath, ".js");
}
} else {
conf.supFiles.push(inputPath);
}
} else if (extension === ".c") {
conf.cPath = inputPath;
} else {
if (path.extname(inputPath) !== ".jxe") conf.supFiles.push(inputPath);
}
}
}
return conf;
}
function validateArgs(cargs) {
let inputError = false;
if (cargs.cPath === undefined) {
console.log("\n");
console.error(`${ansiiRed("Error:")} C-side input file missing`);
inputError = true;
} else if (!fs.existsSync(cargs.cPath)) {
if (!inputError) console.log("\n");
console.error(`${ansiiRed("File not found: ")}` + cargs.cPath);
inputError = true;
}
if (cargs.jsPath === undefined) {
if (!inputError) console.log("\n");
console.error(`${ansiiRed("Error:")} J-side input file missing`);
inputError = true;
} else if (!fs.existsSync(cargs.jsPath)) {
if (!inputError) console.log("\n");
console.error(`${ansiiRed("File not found: ")}` + cargs.jsPath);
inputError = true;
}
if (inputError) {
console.log("\n");
process.exit(1);
}
}
function runMain(cargs) {
let preprocessed;
let lineNumber;
try {
fs.mkdirSync(tmpDir);
let jsTree = jam.preprocessJs(fs.readFileSync(cargs.jsPath).toString(), args.verbosity);
try {
var preprocessedOutput = preprocess(cargs.cPath, cargs, jam.getCIncludes(jsTree));
preprocessed = preprocessedOutput.program;
lineNumber = preprocessedOutput.lineNumber;
} catch (e) {
printAndExit("Exiting with preprocessor error");
}
if (cargs.preprocessOnly)
printAndExit(preprocessed);
let results = jam.compile(preprocessed, jsTree, lineNumber, args.verbosity);
cargs.cSideEffectTable = results.C_SideEffectTable;
cargs.jsSideEffectTable = results.JS_SideEffectTable;
if (!cargs.noCompile) {
let task = nativeCompile(results.C, cargs, jam.getCIncludes(jsTree), jam.getCLinkerFlags(jsTree));
task.then(function (value) {
results.manifest = createManifest(cargs.outPath, results.hasJdata);
createZip(results.JS, results.C, results.manifest, results.jstart, tmpDir, cargs);
if (!cargs.debug) {
console.log(value);
deleteFolderRecursive(tmpDir);
}
}).catch(function (error) {
console.log(error);
});
}
} catch (e) {
console.log("ERROR:");
console.log(e);
}
}
function nativeCompile(code, cargs, userIncludes, userLinkerFlags) {
return new Promise(function (resolve, reject) {
// Set platform options
let options = "";
if (process.platform === "darwin") {
// Mac
options = "-framework CoreFoundation";
} else {
// Linux
options = "-lm";
}
if (cargs.debug) {
options += " -fno-omit-frame-pointer -fsanitize=address -Wall";
}
const requiredIncludes = [`jam.h`];
let includes = requiredIncludes.concat(userIncludes).map(f => `-include "${f}"`).join(" ");
fs.writeFileSync(`${tmpDir}/jamout.c`, code);
try {
var command = `clang -g ${tmpDir}/jamout.c -o ${tmpDir}/a.out -I/usr/local/include -I${homeDir}/.jamruns/clib/include -I${homeDir}/.jamruns/clib/src ${options} -pthread -ltinycbor -lmosquitto -lhiredis -levent ${userLinkerFlags.join(" ")} ${homeDir}/.jamruns/clib/libjam.a ${homeDir}/.jamruns/jamhome/deps/mujs2/build/release/libmujs.a ${homeDir}/.jamruns/jamhome/deps/tinycbor/lib/libtinycbor.a ${includes}`;
if (args.verbosity) console.log("[C] Compiling code...");
if (cargs.verbosity == 2) {
console.log(command);
}
// This prints any errors to stderr automatically, so no need to show the error again
child_process.execSync(command, {
stdio: [0, 1, 2],
});
} catch (e) {
reject("Compilation failed");
}
resolve("Compilation finished");
});
}
function printAndExit(output) {
console.log(output);
process.exit();
}
function preprocess(file, cargs, userIncludes) {
if (args.verbosity) console.log("[C] Preprocessing...");
let imacros = userIncludes.map(f => `-imacros "${f}"`).join(" ");
let contents = fs.readFileSync(file).toString();
fs.writeFileSync(`${tmpDir}/pre.c`, contents);
let command = `clang -E -P -I/usr/local/include -I${homeDir}/.jamruns/clib/include ${imacros} ${tmpDir}/pre.c`;
if (cargs.verbosity == 2)
console.log(command);
let preprocessedProg = child_process.execSync(command).toString();
if (cargs.debug)
fs.writeFileSync(`${tmpDir}/pre2.c`, preprocessedProg);
let index = preprocessedProg.indexOf("int main();\n");
let tmp = preprocessedProg.substring(0, index);
let lineNumber = tmp.split("\n").length;
return {
program: preprocessedProg,
lineNumber: lineNumber,
};
}
function createZip(jsout, cout, mout, jstart, tmpDir, cargs) {
let zip = new JSZip();
zip.file("MANIFEST.txt", mout);
zip.file("jamout.js", jsout);
zip.file("jamout.c", cout); // TODO this should be removed eventually but is very useful for debugging
zip.file("jstart.js", jstart);
// Include debug symbols if they are there.
// if(fs.existsSync(`${tmpDir}/a.out.dSYM`)) {
// fs.cpSync(`${tmpDir}/a.out.dSYM`, `${cargs.outPath}.dSYM`, {recursive: true});
// zip.file("a.out.dSYM", fs.readFileSync(`${tmpDir}/a.out.dSYM`))
// }
zip.file("a.out", fs.readFileSync(`${tmpDir}/a.out`));
if (cargs.supFiles !== undefined && cargs.supFiles.length > 0)
cargs.supFiles.forEach(function (e) {
var st = fs.statSync(e);
if (st.isDirectory()) {
var dir = fs.readdirSync(e);
process.chdir(e);
dir.forEach(function (f) {
if (args.verbosity) console.log("Copying file: ", e + "/" + f);
zip
.folder(path.basename(e))
.file(path.basename(f), fs.readFileSync(f));
});
process.chdir("..");
} else {
if (args.verbosity) console.log("Copying file: ", e);
zip.file(path.basename(e), fs.readFileSync(e));
}
});
zip
.generateNodeStream({
type: "nodebuffer",
streamFiles: true,
})
.pipe(fs.createWriteStream(`${cargs.outPath}.jxe`));
}
function createManifest(cargs, hasJ) {
let mout;
let ctime = new Date().getTime();
mout = "VERSION = 2.0\n";
mout += "DESCRIPTION = JAMScript executable file\n";
mout += `NAME = ${cargs.outPath}\n`;
mout += `CREATE-TIME = ${ctime}\n`;
mout += `JDATA = ${hasJ}\n`;
return mout;
}
function randomValueHex(len) {
return crypto
.randomBytes(Math.ceil(len / 2))
.toString("hex") // convert to hexadecimal format
.slice(0, len); // return required number of characters
}
function deleteFolderRecursive(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function (file) {
let curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) {
// recurse
deleteFolderRecursive(curPath);
} else {
// delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}