-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrc-preprocessor.js
More file actions
535 lines (416 loc) · 15.1 KB
/
rc-preprocessor.js
File metadata and controls
535 lines (416 loc) · 15.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
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
class Preprocessor {
constructor() {
this.warnings = null
this.errors = null
}
strip_lines(lines) {
// strip all non-code lines
var i = 0
var original_line = 0
while (i < lines.length) {
original_line++
var line = lines[i]
// strip all comments
if (line.includes(';')) {
line = line.substr(0, line.indexOf(';'))
}
// skip everything after 'END'
if (line.trim() == 'END') {
var l = lines.length - i - 1
if (l) {
this.warnings.push(`W002: ${l} line(s) after END token`);
}
lines.splice(i + 1, l - 1)
line = ''
}
if (line.trim().length) {
// keep all good lines
lines[i] = {
original_line: original_line,
line: 0,
text: line.trimRight()
}
i++
} else {
lines.splice(i, 1)
}
}
}
split_instructions(instructions, errors, warnings) {
for (var i = 0; i < instructions.length; i++) {
var o_l = instructions[i].original_line
var ins = instructions[i].text
var idx = instructions[i].line
// fix label:
ins = ins.replace(/^([^\s]+):/g, '$1 ')
// fix end as label
ins = ins.replace(/^END\s/g, '\tEND\t')
// remove multiple spaces
ins = ins.replace(/\s+|\t+/g, ' ')
// try fixing "address mode space value"-issues like "dat # 3"
const modes = Array.from(addr_names.values()).map(e => e.display)
modes.forEach(mode => {
var re = new RegExp(` \\${mode} `, 'g')
ins = ins.replace(re, ` ${mode}`)
})
// there's a few options here. If there is no comma separating the
// operands, there cannot be any spaces in the command (spaces
// displayed as _ in the examples below):
// mov_(1+label)_0 works (space as divider, no comma)
// mov_(1_+_label),_0 works (comma as divider)
// mov_(1_+_label)_0 fails (no comma but spaces)
if (ins.includes(',')) {
// comma separated -> X_Y_A,B
var x = ins.indexOf(' ')
ins = ins.substr(0, x) + ',' + ins.substr(x + 1)
x = ins.indexOf(' ')
ins = ins.substr(0, x) + ',' + ins.substr(x + 1)
ins = ins.replace(/\s+/g, '')
var items = ins.split(',')
}
else {
// no comma -> split on space
var items = ins.split(' ')
}
for (var q = items.length - 1; q >= 0; q--) {
if (typeof items[q] == 'undefined') {
items.splice(q, 1)
}
}
if (items.length > 1) {
var op = null
// find opcode
for (var o = 0; o < opcodes.length; o++) {
if (opcodes[o].name == items[1]) {
op = opcodes[o]
}
}
if (op) {
if (items.length < 2 + op.num_params) {
this.errors.push(`E006: Too few tokens for opcode '${items[1]}' on line ${o_l} '${ins}'`)
}
// single operand? right align?
if (items.length == 3 && op.b_align) {
items.splice(2, 0, '0')
}
}
else {
// pseudo-op?
if (!kPSEUDO_OPCODES.includes(items[1])) {
this.errors.push(`E003: Unknown opcode '${items[1]}' on line ${o_l} '${ins}'`)
}
}
}
items[0] = this.make_valid_label(items[0])
instructions[i] = {
line: idx,
original_line: o_l,
instruction: items
}
}
}
join_instructions(instructions) {
const fixed = []
var last = 0
for (var i = 0; i < instructions.length; i++) {
var line = instructions[i].instruction.join('\t')
while (instructions[i].original_line > last + 1) {
fixed.push('')
last++
}
fixed.push(line)
instructions[i] = line
last++
}
return fixed
}
make_valid_label(text) {
// labels can only be upper-case letters and numbers
return text.replace(/[^\x41-\x5A_\x30-\x39]/g, '')
}
resolve_equates() {
const self = this
this.equates.forEach((value, key) => {
self.equates.set(key, self.resolve_label(0, value))
})
}
resolve_label(offset, text) {
var all = new Map()
this.labels.forEach((value, key) => {
all.set(key, value - offset)
})
this.equates.forEach((value, key) => {
all.set(key, value)
})
all.set('CURLINE', offset)
var idents = Array.from(all.keys()).sort((a, b) => {
return b.length - a.length
})
idents.forEach(key => {
const value = '' + all.get(key)
if (!value.includes(key)) {
while (text.includes(key)) {
text = text.replace(key, `(${value})`)
}
}
})
return text
}
replace_labels(instructions) {
// everything in column 1 is a label
for (var i = 0; i < instructions.length; i++) {
var ins = instructions[i]
var components = ins.instruction
var label = this.make_valid_label(components[0])
if (label.length) {
// two kinds of labels - actual markers and EQU
if (ins.instruction[1] == 'EQU') {
instructions[i] = null
if (!this.equates.has(label)) {
this.equates.set(label, components[2])
} else {
this.warnings.push(`W004: Duplicate constant '${label}' in line ${ins.original_line}`)
}
}
else {
if (!this.labels.has(label)) {
this.labels.set(label, ins.line)
} else {
this.warnings.push(`W003: Duplicate label '${label}' in line ${ins.original_line}`)
}
}
}
}
for (var n = instructions.length - 1; n >= 0; n--) {
const ins = instructions[n]
if (ins == null || ins.instruction.length == 1) {
instructions.splice(n, 1)
}
}
this.resolve_equates()
// now find components using these labels
for (var i = 0; i < instructions.length; i++) {
var ins = instructions[i]
var comp = ins.instruction
if (comp.length > 2) {
comp[2] = this.resolve_label(ins.line, comp[2])
}
else {
comp.push(default_data)
}
if (comp.length > 3) {
comp[3] = this.resolve_label(ins.line, comp[3])
}
else {
comp.push(default_data)
}
// strip label
comp.splice(0, 1)
}
}
solve_loop(lines, offset) {
var for_op = lines.splice(0, 1)[0]
lines.pop()
const variable = for_op.instruction[0]
var value = for_op.instruction[2]
try {
value = eval(this.resolve_label(offset, value))
}
catch (e) {
this.errors.push(`E009: Unresolvable address '${value}' in loop`)
return []
}
const regex = new RegExp(variable, 'gi');
const output = []
for (var i = 0; i < value; i++) {
for (var r = 0; r < lines.length; r++) {
var source = lines[r]
var line = {
line: source.line,
original_line: source.original_line,
instruction: source.instruction.slice()
}
var ins = line.instruction.slice()
var val = (i + 1) + ''
// replace in label
ins[0] = ins[0].replace(regex, val)
// replace in values
for (var x = 2; x < ins.length; x++) {
ins[x] = ins[x].replace(regex, val)
}
line.instruction = ins
output.push(line);
}
}
return output
}
evalulate_loop(lines, index) {
var loop_end = index + 1
var found = false
// find matching ROF
while (loop_end < lines.length) {
const op = lines[loop_end].instruction[1]
if (op == 'FOR') {
loop_end = this.evalulate_loop(lines, loop_end)
}
else if (op == 'ROF') {
found = true
break
}
loop_end++
}
if (found) {
// we have a matching for-rof block with no further for/rof blocks
// inside
const loop = lines.splice(index, loop_end - index + 1)
const resolved = this.solve_loop(loop, index)
lines.splice.apply(lines, [index, 0].concat(resolved))
// return next index to check
return index + resolved.length
}
this.errors.push(`E001: Missing ROF for FOR in line ${lines[index].original_line}`)
lines.splice(index, 1)
}
parse_loops(lines) {
for (var i = 0; i < lines.length; i++) {
var line = lines[i]
if (line.instruction[1] == 'FOR') {
this.evalulate_loop(lines, i)
i--
}
}
}
number_lines(lines) {
var counter = 0
for (var i = 0; i < lines.length; i++) {
const line = lines[i]
var pseudo = false
if (line.instruction.length == 1) {
// label only, count, but do not increase counter
line.line = counter
continue
}
// check for pseudo op codes
if (kPSEUDO_OPCODES.includes(line.instruction[1])) {
line.line = 0
continue
}
line.line = counter;
counter++
}
}
evaluate_address(address) {
const valid_chars = address_mode_names + '()0123456789+-/*%'
// check if the only contents is now numberical + operand
for (var i = 0; i < address.length; i++) {
if (!valid_chars.includes(address.charAt(i))) {
this.errors.push(`E002: Unknown label in address '${address}'`)
return default_data;
}
}
// evaluate math in address
var prefix = address[0]
var value = address
if (address_mode_names.includes(prefix)) {
value = address.substr(1, address.length)
} else {
prefix = addr_names.get(default_address_mode).display
}
// trim leading zeros (cause we allow them for some reason)
value = value.replace(/^[-]?0+(?=\d)/, '')
try {
var v = eval(value)
}
catch (error) {
this.errors.push(`E008: Unresolvable address '${value}'`)
}
if (isNaN(v)) {
this.errors.push(`E010: Value '${value}' is not a number`)
}
// normalize the value
v %= this.environment.core_size
return prefix + v
}
evaluate_addresses(instructions) {
for (var i = 0; i < instructions.length; i++) {
var ins = instructions[i]
var components = ins.instruction
if (components.length > 1) {
components[1] = this.evaluate_address(components[1])
}
if (components.length > 2) {
components[2] = this.evaluate_address(components[2])
}
}
}
strip_to_ascii(code) {
var len = code.length
var ascii = code.replace(/[^\x00-\x7F]/g, '')
var diff = len - ascii.length
if (len != ascii.length) {
this.warnings.push(`W001: ${diff} non-ASCII characters in input.`)
}
return ascii
}
extract_metadata(lines) {
const metadata = new Map()
for (var idx in lines) {
const line = lines[idx].trim()
if (line.length > 1 && line[0] === ';' && line[1] !== ' ' &&
line.includes(' ') && (line.indexOf(' ') < line.length - 1)) {
const items = line.split(' ')
const key = items[0].substr(1)
const value = items.splice(1).join(' ')
metadata.set(key.toUpperCase(), value)
}
}
return metadata
}
fill_environment_equates() {
this.equates.set('CORESIZE', this.environment.core_size + '')
this.equates.set('MAXLENGTH', this.environment.max_instructions + '')
// TODO: missing constants
}
preprocess(code, environment) {
this.environment = environment
this.warnings = new Array()
this.errors = new Array()
this.labels = new Map()
this.equates = new Map()
var instructions = new Array()
// get all environment constants
this.fill_environment_equates()
// strip all non-ascii characters
code = this.strip_to_ascii(code)
// split into array
var components = code.toUpperCase().split('\n')
// extract all comment-value meta data liens
const metadata = this.extract_metadata(code.split('\n'))
// strip all non-code lines and turn the strings into objects
this.strip_lines(components)
// split into quads
this.split_instructions(components)
// unroll for/rof
this.parse_loops(components)
// set correct addresses
this.number_lines(components)
// parse labels
this.replace_labels(components)
// evaluate address arithmetic
this.evaluate_addresses(components)
// put the components back together
const fixed = this.join_instructions(components)
const verbose = fixed.join('\n')
// join the lines
const output = components.join('\n')
// todo add warnings for missing "obligatory" metadata (name, author,
// redode version)
return {
verbose_code: verbose,
code: output,
errors: this.errors,
warnings: this.warnings,
metadata: metadata,
}
}
}