-
Notifications
You must be signed in to change notification settings - Fork 19
/
ws_asm.js
548 lines (507 loc) · 16 KB
/
ws_asm.js
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
globalThis.ws_asm = (function() {
const builtinMacros = function () {
return {
"include": {
params: ["STRING"],
action: function (args, builder) {
const arg = args[1];
let fileName = arg.token;
fileName = fileName.slice(1, fileName.length - 1);
if (!(fileName in builder.includes)) {
const file = ws_fs.getFile(fileName);
if (!file) {
throw `File not found: '${fileName}'`;
}
builder.includes[fileName] = ws_fs.openFile(file);
if (builder.includes[fileName]) {
const srcArr = ws_util.StrArr(builder.includes[fileName]);
try {
const ext = ws_asm.compile(builder.includes[fileName], builder);
builder.externals.push(ext);
} catch (err) {
if (err.program) {
builder.externals.push(err.program);
console.warn(`Broken include '${fileName}': ${err.message}`);
} else {
throw `Error while compiling '${fileName}': ${err.message}`;
}
}
}
}
}
},
"macro": {
params: ["LABEL"],
action: function (args, builder) {
const metaTypes = {"$number": "NUMBER", "$label": "TOKEN", "$string": "STRING"};
const macroLabel = args[1].token;
let closed = false;
const newMacro = {
tokens: [],
params: [],
action: function (args, builder) {
const name = args[0].token;
if (typeof args[0].data === "bigint") {
throw `Defining a macro with a numeric identifier: ${name}`;
}
builder.macroCallCounts[name] = (builder.macroCallCounts[name] || 0) + 1;
const macroId = builder.macroCallCounts[name];
args[0].called = (args[0].called || 0) + 1
if (args[0].called > 16) {
throw "Circular reference of macros";
}
const toks = [];
let n = 1;
for (const t of this.tokens) {
const token = Object.assign({}, t);
if (token.token in metaTypes) {
toks.push(args[n++]);
} else {
if (/^\$\d+$/.test(token.token)) {
token.token = ".__" + name + token.token + "_" + macroId;
}
toks.push(token);
}
}
builder.tokens = toks.concat(builder.tokens);
}
};
while (true) {
const token = builder.tokens.shift();
if (!token) {
break;
}
if (token.type === "MACRO") {
if (token.token === "$$") {
closed = true;
break;
}
if (token.token === "include") {
// do nothing
} else if (token.token === "$redef") {
args[1].type = "MACRO";
newMacro.tokens.push(args[1]);
continue;
} else if (token.token in metaTypes) {
newMacro.params.push(metaTypes[token.token]);
}
}
newMacro.tokens.push(token);
}
if (!closed) {
throw "Macro not closed";
}
builder.macros[macroLabel] = newMacro;
}
},
"$$": {
params: [],
action: function (args, builder) {
throw "Unexpected end of macro";
}
},
"$label": {
params: [],
action: function (args, builder) {
throw "Label-pop called outside of a macro";
}
},
"$number": {
params: [],
action: function (args, builder) {
throw "Number-pop called outside of a macro";
}
},
"$string": {
params: [],
action: function (args, builder) {
throw "String-pop called outside of a macro";
}
},
"$redef": {
params: [],
action: function (args, builder) {
throw "Can't redefine macro outside of a macro"
}
}
};
};
const mnemo = (function () {
const mnemoCodes = {};
// Collect keywords
for (const keyword of ws.keywords) {
mnemoCodes[keyword.mnemo] = keyword;
}
return mnemoCodes;
})();
const parseWhitespace = function (strArr) {
let space = "";
while (strArr.hasNext() && /[ \t\n\r]/.test(strArr.peek())) {
space += strArr.getNext();
}
return {
type: "SPACE",
token: space
};
};
const parseLineComment = function (strArr) {
let comment = "";
do {
comment += strArr.getNext();
} while (strArr.hasNext() && strArr.peek() !== '\n');
return {
type: "COMMENT",
token: comment
};
};
const parseMultiLineComment = function (strArr) {
let depth = 0;
let comment = "";
while (true) {
if (!strArr.hasNext()) {
throw "Unterminated multiline comment";
}
const ch = strArr.getNext();
comment += ch;
if (ch === "{" && strArr.peek() === "-") {
depth += 1;
comment += strArr.getNext();
} else if (ch === "-" && strArr.peek() === "}") {
depth -= 1;
comment += strArr.getNext();
if (depth === 0) {
break;
}
}
}
return {
type: "COMMENT",
token: comment
};
};
const parseNumber = function (strArr) {
let numStr = "";
while (strArr.hasNext() && /^[+-]?\d*$/.test(numStr + strArr.peek())) {
numStr += strArr.getNext();
}
let type = "NUMBER";
if (strArr.peek() === ":") {
type = "LABEL";
strArr.getNext();
}
try {
const data = BigInt(numStr);
return {
type: type,
token: numStr,
data: data
};
} catch (err) {
throw "Illegal number";
}
};
const getStringArray = function (str) {
const result = [];
let escape = false;
let chCode = "";
for (const ch of str.slice(1, -1)) {
if (chCode) {
if (/[0-9]/.test(ch)) {
chCode += ch;
continue;
} else {
result.push(BigInt(chCode));
chCode = "";
}
}
if (escape) {
if (ch === 'n') {
result.push(BigInt('\n'.charCodeAt(0)));
} else if (ch === 't') {
result.push(BigInt('\t'.charCodeAt(0)));
} else if (/[0-9]/.test(ch)) {
chCode += ch;
} else {
result.push(BigInt(ch.codePointAt(0)));
}
escape = false;
} else if (ch === '\\') {
escape = true;
} else {
result.push(BigInt(ch.codePointAt(0)));
}
}
if (chCode) {
result.push(BigInt(chCode));
}
if (str[0] === '"') {
result.push(0n);
}
return result;
};
const parseString = function (strArr) {
const strEnd = strArr.getNext();
let str = strEnd;
while (true) {
if (!strArr.hasNext()) {
throw "Unterminated string";
}
const ch = strArr.getNext();
str += ch;
if (ch === strEnd) {
break;
} else if (ch === '\n') {
throw "Unexpected end of line";
} else if (ch === '\\' && strArr.hasNext()) {
str += strArr.getNext();
}
}
return {
type: "STRING",
token: str,
data: getStringArray(str)
};
};
const parseLabel = function (strArr, builder) {
let label = "";
while (strArr.hasNext() && /[0-9a-zA-Z_$.]/.test(strArr.peek())) {
label += strArr.getNext();
}
let type = "TOKEN";
let op = null;
if (strArr.peek() === ':') {
type = "LABEL";
strArr.getNext();
} else if (label in mnemo) {
type = "KEYWORD";
op = mnemo[label];
} else if (label in builder.macros) {
type = "MACRO";
}
return {
type: type,
token: label,
op: op
};
};
const getTokens = function (strArr, builder) {
let prevSpace = true;
while (strArr.hasNext()) {
if (parseWhitespace(strArr).token) {
prevSpace = true;
continue;
}
let token = null;
builder.pos = strArr.pos();
const next = strArr.peek();
if (next === ';' || next === '#' || (next === '-' && strArr.peek(1) === '-')) {
token = parseLineComment(strArr);
} else if (next === '{' && strArr.peek(1) === '-') {
token = parseMultiLineComment(strArr);
} else if (/["']/.test(next)) {
token = parseString(strArr);
} else if (/[-+\d]/.test(next)) {
token = parseNumber(strArr);
} else if (/[a-zA-Z_$.]/.test(next)) {
token = parseLabel(strArr, builder);
} else {
throw `Illegal character: '${next}'`;
}
token.pos = builder.pos;
if (token.type === "COMMENT") {
prevSpace = true;
continue;
}
if (!prevSpace) {
const prev = builder.tokens[builder.tokens.length-1];
throw `Missing space between ${prev.token} and ${token.token}`;
}
// Implicit space after :
prevSpace = token.type === "LABEL";
builder.tokens.push(token);
}
};
const pushInstruction = function (builder, constr, numberArg) {
const instruction = new constr();
if (numberArg != null) {
instruction.arg = {token: ws_util.getWsSignedNumber(numberArg), value: numberArg};
}
builder.pushInstruction(instruction);
};
const postProcess = function (builder) {
while (builder.externals.length > 0) {
const ext = builder.externals.shift();
for (const inst of ext.programStack) {
builder.pushInstruction(inst);
}
}
return builder.postProcess();
};
const checkMacroArgs = function (token, builder) {
const macro = builder.macros[token];
if (typeof macro.action === "function") {
let n = 0;
for (const paramType of macro.params) {
const arg = builder.tokens[n++];
if (!arg || (arg.type !== paramType &&
!(paramType === "NUMBER" && arg.type === "STRING" && arg.data.length === 1))) {
return false;
}
}
}
return true;
};
return {
compile: function (str, master) {
const strArr = ws_util.StrArr(str);
const builder = ws.programBuilder(str, master);
let tokenError;
builder.macros = builder.macros || builtinMacros();
builder.includes = builder.includes || {};
builder.externals = builder.externals || [];
builder.macroCallCounts = builder.macroCallCounts || {};
builder.tokens = [];
builder.pos = strArr.pos();
try {
getTokens(strArr, builder);
} catch (err) {
tokenError = {
tokens: builder.tokens,
pos: builder.pos,
message: `${err} at ${builder.pos.line}:${builder.pos.col}`,
line: builder.pos.line,
col: builder.pos.col
};
}
builder.asmLabeler = builder.asmLabeler || ws_util.labelTransformer(function(counter, label) {
return ws_util.getWsUnsignedNumber(counter);
});
const labeler = builder.asmLabeler;
let parentLabel = "";
while (builder.tokens.length > 0) {
const token = builder.tokens.shift();
const pos = token.pos;
try {
if (token.type === "LABEL" || (token.op && token.op.constr === ws.WsLabel)) {
let labelAsm;
if (token.type === "LABEL") {
// Colon-style label
labelAsm = token.token;
} else {
// Mnemonic-style label
const arg = builder.tokens.shift();
if (!arg || arg.type !== "TOKEN" && arg.type !== "MACRO" && arg.type !== "KEYWORD" && arg.type !== "NUMBER") {
throw "Expected label argument";
}
labelAsm = arg.token;
}
if (ws_util.isLocalLabel(labelAsm)) {
labelAsm = parentLabel + labelAsm;
} else {
parentLabel = labelAsm;
}
const label = labeler.getLabel(labelAsm);
if (typeof builder.labels[label] === "number") {
throw `Multiple definitions of label ${labelAsm}`;
}
builder.labels[label] = builder.programStack.length;
builder.asmLabels[label] = labelAsm;
} else if (token.token in builder.macros && checkMacroArgs(token.token, builder)) {
token.type = "MACRO"; // can be label in some cases
const macro = builder.macros[token.token];
if (typeof macro.action === "function") {
const args = [token];
for (const paramType of macro.params) {
const arg = builder.tokens.shift();
if (!arg || (arg.type !== paramType &&
!(paramType === "NUMBER" && arg.type === "STRING" && arg.data.length === 1))) {
throw `Expected ${paramType} argument`;
} else {
args.push(arg);
}
}
macro.action(args, builder);
} else {
throw `Unimplemented macro type ${typeof macro.action}`;
}
} else if (token.token in mnemo) {
if (token.type !== 'KEYWORD') { // A mnemonic overloaded as a macro
token.type = 'KEYWORD';
token.op = mnemo[token.token];
}
const op = token.op;
let instruction = new op.constr();
if (op.optParam === 'NUMBER' && builder.tokens[0] && builder.tokens[0].type === op.optParam) {
pushInstruction(builder, ws.WsPush, builder.tokens.shift().data);
}
if (op.param) {
const arg = builder.tokens.shift();
if (!arg) {
throw "Argument expected";
}
if (op.param === "NUMBER") {
if (arg.type === "NUMBER") {
pushInstruction(builder, op.constr, arg.data);
} else if (instruction instanceof ws.WsPush && arg.type === "STRING") {
for (let i = arg.data.length - 1; i >= 0; i--) {
pushInstruction(builder, op.constr, arg.data[i]);
}
} else {
throw `Unexpected token: ${arg.token}`;
}
} else if (op.param === "LABEL") {
if (arg.type === "TOKEN" || arg.type === "MACRO" || arg.type === "KEYWORD" || arg.type === "NUMBER") {
instruction = new op.constr();
let label = arg.token;
if (ws_util.isLocalLabel(label)) label = parentLabel + label;
instruction.arg = {
token: labeler.getLabel(label), value: null, label: label
};
builder.pushInstruction(instruction);
} else {
throw "Expected label argument";
}
} else {
throw `Unsupported argument type ${op.param} (should never happen)`
}
} else {
pushInstruction(builder, op.constr);
}
} else if (token.token in builder.macros) {
throw `Incorrect argument types for macro ${token.token}`;
} else {
throw `Unexpected token: ${token.token}`;
}
} catch (err) {
if (typeof err === "string") {
throw {
program: null,
line: pos.line,
message: `${err} at ${pos.line}:${pos.col}`
};
} else {
throw err;
}
}
}
if (tokenError) {
throw {
program: null,
line: tokenError.pos.line,
message: tokenError.message
};
}
try {
return postProcess(builder);
} catch (err) {
if (typeof err === "string") {
throw {
message: err
};
} else {
throw err;
}
}
}
};
})();