-
Notifications
You must be signed in to change notification settings - Fork 18
/
jacs_opwriter.ts
686 lines (607 loc) · 20.4 KB
/
jacs_opwriter.ts
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
namespace jacs {
export function assert(cond: boolean, msg?: string) {
if (!cond) {
if (msg == null) msg = "Assertion failed"
throw msg
}
}
export function write32(buf: Buffer, pos: number, v: number) {
buf.setNumber(NumberFormat.UInt32LE, pos, v)
}
export function read32(buf: Buffer, pos: number) {
return buf.getNumber(NumberFormat.UInt32LE, pos)
}
export function write16(buf: Buffer, pos: number, v: number) {
buf.setNumber(NumberFormat.UInt16LE, pos, v)
}
export function assertRange(
min: number,
v: number,
max: number,
desc?: string
) {
if (min <= v && v <= max) return
throw `${desc || "value"}=${v} out of range [${min}, ${max}]`
}
export function oops(msg: string): never {
throw msg
}
export interface TopOpWriter extends InstrArgResolver {
addString(s: string): number
addFloat(f: number): number
writer: OpWriter
hasErrors: boolean
}
export class Label {
uses: number[]
offset = -1
constructor(public name: string) {}
}
const VF_DEPTH_MASK = 0xff
const VF_USES_STATE = 0x100
const VF_HAS_PARENT = 0x200
const VF_IS_LITERAL = 0x400
const VF_IS_MEMREF = 0x800
const VF_IS_STRING = 0x1000
const VF_IS_WRITTEN = 0x2000
export class Value {
op: number
flags: number
args: Value[]
numValue: number
_userdata: {}
_cachedValue: CachedValue
constructor() {}
get maxstack() {
return (this.flags & VF_DEPTH_MASK) + 1
}
get usesState() {
return !!(this.flags & VF_USES_STATE)
}
get hasParent() {
return !!(this.flags & VF_HAS_PARENT)
}
get isLiteral() {
return !!(this.flags & VF_IS_LITERAL)
}
get isMemRef() {
return !!(this.flags & VF_IS_MEMREF)
}
adopt() {
assert(!(this.flags & VF_HAS_PARENT))
this.flags |= VF_HAS_PARENT
}
_set(src: Value) {
if (!this._userdata) this._userdata = src._userdata
this.op = src.op
this.flags = src.flags
this.args = src.args
this.numValue = src.numValue
this._cachedValue = src._cachedValue
}
}
export class CachedValue {
numrefs = 1
constructor(public parent: OpWriter, public index: number) {}
emit() {
assert(this.numrefs > 0)
const r = new Value()
r.numValue = this.index
r.op = Op.EXPRx_LOAD_LOCAL
r.flags = VF_IS_MEMREF // not "using state" - it's temporary
r._cachedValue = this
this.numrefs++
return r
}
store(v: Value) {
assert(this.numrefs > 0)
this.parent.emitStmt(Op.STMTx1_STORE_LOCAL, [
literal(this.index),
v,
])
}
_decr() {
assert(this.numrefs > 0)
if (--this.numrefs == 0) {
assert(this.parent.cachedValues[this.index] == this)
this.parent.cachedValues[this.index] = null
this.index = null
}
}
free() {
this._decr()
}
}
export function literal(v: number, op = Op.EXPRx_LITERAL) {
const r = new Value()
r.numValue = v
r.op = op
r.flags = VF_IS_LITERAL
return r
}
export function nonEmittable(k: number) {
const r = new Value()
assert(k >= 0x100)
r.op = k
return r
}
class Comment {
constructor(public offset: number, public comment: string) {}
}
export const LOCAL_OFFSET = 100
export class OpWriter {
private binary: Buffer
private binPtr: number = 0
private labels: Label[] = []
private comments: Comment[] = []
private bufferAllocated = false
pendingStatefulValues: Value[] = []
localOffsets: number[] = []
cachedValues: CachedValue[] = []
top: Label
ret: Label
private lineNo = -1
private lineNoStart = -1
desc = Buffer.create(BinFmt.FUNCTION_HEADER_SIZE)
offsetInFuncs = -1
srcmap: number[] = []
private nameIdx: number
external = false
constructor(
private prog: TopOpWriter,
public name: string,
public fnidx: number
) {
this.top = this.mkLabel("top")
this.emitLabel(this.top)
this.binary = Buffer.create(128)
this.nameIdx = this.prog.addString(this.name)
}
assertCurrent() {
assert(this.prog.writer == this)
}
serialize() {
while (this.location() & 3) this.writeByte(0)
this.binary = this.binary.slice(0, this.binPtr)
return this.binary
}
setExternal(fullbody: Buffer) {
const body = fullbody.slice(BinFmt.FUNCTION_HEADER_SIZE)
this.desc.write(0, fullbody)
this.external = true
this.binary = body
this.binPtr = this.binary.length
return body
}
finalizeDesc(off: number, numlocals: number, numargs: number) {
const flags = 0
const buf = this.desc
write32(buf, 0, off)
write32(buf, 4, this.location())
if (!this.external) {
write16(buf, 8, numlocals + this.cachedValues.length)
buf[10] = numargs
buf[11] = flags
}
write16(buf, 12, this.nameIdx)
}
emitDbg(msg: string) {
this.emitComment(msg)
}
_forceFinStmt() {
if (this.lineNo < 0) return
const len = this.location() - this.lineNoStart
if (len) {
this.srcmap.push(this.lineNo)
this.srcmap.push(this.lineNoStart)
this.srcmap.push(len)
}
}
stmtStart(lineNo: number) {
if (this.lineNo == lineNo) return
this._forceFinStmt()
this.lineNo = lineNo
this.lineNoStart = this.location()
}
allocTmpLocals(num: number) {
let run = 0
let runStart = 0
for (let i = 0; i < this.cachedValues.length; ++i) {
if (this.cachedValues[i] == null) run++
else {
run = 0
runStart = i + 1
}
if (run >= num) break
}
while (run < num) {
this.cachedValues.push(null)
run++
}
for (let i = 0; i < num; ++i) {
assert(this.cachedValues[runStart + i] === null) // not undefined
this.cachedValues[runStart + i] = new CachedValue(
this,
runStart + i
)
}
return this.cachedValues.slice(runStart, runStart + num)
}
allocTmpLocal() {
// TODO optimize?
return this.allocTmpLocals(1)[0]
}
cacheValue(v: Value) {
const t = this.allocTmpLocal()
t.store(v)
return t
}
stmtEnd() {
this.assertNoTemps()
}
allocBuf(): Value {
assert(!this.bufferAllocated, "allocBuf() not free")
this.bufferAllocated = true
return nonEmittable(CellKind.JD_CURR_BUFFER)
}
freeBuf(): void {
assert(this.bufferAllocated, "freeBuf() already free")
this.bufferAllocated = false
}
emitString(s: string) {
const v = literal(this.prog.addString(s))
v.flags |= VF_IS_STRING
return v
}
emitCall(procIdx: number, args: CachedValue[], op = OpCall.SYNC) {
const proc = this.emitExpr(Op.EXPRx_STATIC_FUNCTION, [
literal(procIdx),
])
const localidx = literal(args[0] ? args[0].index : 0)
const numargs = literal(args.length)
if (op == OpCall.SYNC)
this.emitStmt(Op.STMTx2_CALL, [localidx, numargs, proc])
else
this.emitStmt(Op.STMTx3_CALL_BG, [
localidx,
numargs,
proc,
literal(op),
])
for (const c of args) c.free()
}
emitBufLoad(fmt: NumFmt, off: number, buf?: Value) {
if (!buf) buf = this.emitExpr(Op.EXPR0_PKT_BUFFER, [])
return this.emitExpr(Op.EXPR3_LOAD_BUFFER, [
buf,
literal(fmt),
literal(off),
])
}
emitBufStore(src: Value, fmt: NumFmt, off: number, buf?: Value) {
if (!buf) buf = this.emitExpr(Op.EXPR0_PKT_BUFFER, [])
this.emitStmt(Op.STMT4_STORE_BUFFER, [
buf,
literal(fmt),
literal(off),
src,
])
}
getAssembly() {
let res = `proc ${this.name}_F${this.fnidx}:\n`
let ptr = 0
let commentPtr = 0
const getbyte = () => {
if (ptr < this.binPtr) return this.binary[ptr++]
return 0
}
while (ptr < this.binPtr) {
while (commentPtr < this.comments.length) {
const c = this.comments[commentPtr]
if (c.offset > ptr) break
commentPtr++
res += "; " + c.comment.replaceAll("\n", "\n; ") + "\n"
}
this.prog.resolverPC = ptr
res += stringifyInstr(getbyte, this.prog) + "\n"
}
if (ptr > this.binPtr) res += "!!! binary mis-alignment\n"
return res
}
emitComment(msg: string) {
this.comments.push(new Comment(this.location(), msg))
}
mkLabel(name: string) {
const l = new Label(name)
this.labels.push(l)
return l
}
_setLabelOffset(l: Label, off: number) {
l.offset = off
if (l.uses) {
for (const u of l.uses) {
const v = l.offset - u
assert(v >= 0)
assert(v <= 0xffff)
this.binary[u + 2] = v >> 8
this.binary[u + 3] = v & 0xff
}
l.uses = undefined
}
}
emitLabel(l: Label) {
assert(l.offset == -1)
this._setLabelOffset(l, this.location())
}
emitIf(reg: Value, thenBody: () => void, elseBody?: () => void) {
if (elseBody) {
const endIf = this.mkLabel("endif")
const elseIf = this.mkLabel("elseif")
this.emitJump(elseIf, reg)
thenBody()
this.emitJump(endIf)
this.emitLabel(elseIf)
elseBody()
this.emitLabel(endIf)
} else {
const skipIf = this.mkLabel("skipif")
this.emitJump(skipIf, reg)
thenBody()
this.emitLabel(skipIf)
}
}
emitJumpIfTrue(label: Label, cond: Value) {
return this.emitJump(label, this.emitExpr(Op.EXPR1_NOT, [cond]))
}
emitJump(label: Label, cond?: Value) {
if (cond) cond.adopt()
this.spillAllStateful()
if (cond) this.writeValue(cond)
const off0 = this.location()
this.writeByte(cond ? Op.STMTx1_JMP_Z : Op.STMTx_JMP)
if (label.offset != -1) {
this.writeInt(label.offset - off0)
} else {
if (!label.uses) label.uses = []
label.uses.push(off0)
this.writeInt(0x1000)
}
}
private oops(msg: string) {
try {
// console.log(this.getAssembly())
} catch {}
oops(msg)
}
assertNoTemps() {
if (this.prog.hasErrors) return
for (const c of this.cachedValues) {
if (c !== null) {
this.oops(`local ${c.index} still has ${c.numrefs} refs`)
}
}
for (const e of this.pendingStatefulValues) {
if (e.usesState && !e.hasParent)
this.oops("pending stateful values")
}
}
patchLabels() {
// we now patch at emit
for (const l of this.labels) {
if (l.uses) this.oops(`label ${l.name} not resolved`)
}
this.assertNoTemps()
// patch local indices
for (const off of this.localOffsets) {
assert(
LOCAL_OFFSET <= this.binary[off] && this.binary[off] < 0xf8
)
this.binary[off] =
this.binary[off] - LOCAL_OFFSET + this.cachedValues.length
}
this.localOffsets = []
}
private spillValue(v: Value) {
const l = this.allocTmpLocal()
l.store(v)
v._set(l.emit())
l.free()
}
private spillAllStateful() {
for (const e of this.pendingStatefulValues) {
if (e.usesState && !e.hasParent) this.spillValue(e)
}
this.pendingStatefulValues = []
}
emitMemRef(op: Op, idx: number) {
const r = new Value()
r.numValue = idx
r.op = op
r.flags = VF_IS_MEMREF | VF_USES_STATE
this.pendingStatefulValues.push(r)
return r
}
emitExpr(op: Op, args: Value[]) {
const n = opNumArgs(op)
if (n != args.length)
oops(`expr ${op} requires ${n}; got ${args.length}`)
let stack = 0
let maxstack = 1
let usesState = exprIsStateful(op)
// TODO constant folding
for (const a of args) {
if (stack + a.maxstack >= BinFmt.MAX_STACK_DEPTH)
this.spillValue(a)
maxstack = Math.max(maxstack, stack + a.maxstack)
stack++
if (a.usesState) usesState = true
assert(!(a.flags & VF_HAS_PARENT))
a.flags |= VF_HAS_PARENT
}
const r = new Value()
r.args = args
r.op = op
r.flags = maxstack - 1 // so that r.maxstack == maxstack
if (usesState) {
r.flags |= VF_USES_STATE
this.pendingStatefulValues.push(r)
}
return r
}
location() {
return this.binPtr
}
private writeByte(v: number) {
assert(0 <= v && v <= 0xff && (v | 0) == v)
if (this.binPtr >= this.binary.length) {
const copy = Buffer.create(this.binary.length * 2)
copy.write(0, this.binary)
this.binary = copy
}
this.binary[this.binPtr++] = v
}
private writeInt(v: number) {
assert((v | 0) == v)
if (0 <= v && v < 0xf8) this.writeByte(v)
else {
let b = 0xf8
if (v < 0) {
b |= 4
v = -v
}
let hddone = false
for (let shift = 3; shift >= 0; shift--) {
const q = (v >> (8 * shift)) & 0xff
if (q && !hddone) {
this.writeByte(b | shift)
hddone = true
}
if (hddone) this.writeByte(q)
}
}
}
private writeArgs(op: Op, args: Value[]) {
let i = 0
if (opTakesNumber(op)) i = 1
while (i < args.length) {
this.writeValue(args[i])
i++
}
this.writeByte(op)
if (opTakesNumber(op)) {
assert(args[0].isLiteral)
const nval = args[0].numValue
if (op == Op.STMTx1_STORE_LOCAL && nval >= LOCAL_OFFSET)
this.localOffsets.push(this.location())
this.writeInt(nval)
}
}
private writeValue(v: Value) {
assert(!(v.flags & VF_IS_WRITTEN))
v.flags |= VF_IS_WRITTEN
if (v.isLiteral) {
const q = v.numValue
if ((q | 0) == q) {
const qq =
q + BinFmt.DIRECT_CONST_OFFSET + BinFmt.DIRECT_CONST_OP
if (BinFmt.DIRECT_CONST_OFFSET <= qq && qq <= 0xff)
this.writeByte(qq)
else {
this.writeByte(Op.EXPRx_LITERAL)
this.writeInt(q)
}
} else if (isNaN(q)) {
this.writeByte(Op.EXPR0_NAN)
} else if (q == null) {
this.writeByte(Op.EXPR0_NULL)
} else {
const idx = this.prog.addFloat(q)
this.writeByte(Op.EXPRx_LITERAL_F64)
this.writeInt(idx)
}
} else if (v.isMemRef) {
assert(opTakesNumber(v.op))
this.writeByte(v.op)
if (v.op == Op.EXPRx_LOAD_LOCAL && v.numValue >= LOCAL_OFFSET)
this.localOffsets.push(this.location())
this.writeInt(v.numValue)
if (v._cachedValue) v._cachedValue._decr()
} else if (v.op >= 0x100) {
oops("this value cannot be emitted: " + v.op.toString())
} else {
this.writeArgs(v.op, v.args)
}
}
emitStmt(op: Op, args: Value[]) {
assert(opNumArgs(op) == args.length)
assert(opIsStmt(op))
for (const a of args) a.adopt()
this.spillAllStateful() // this doesn't spill adopt()'ed Value's (our arguments)
this.writeArgs(op, args)
}
}
export class SectionWriter {
offset = -1
currSize = 0
data: Buffer[] = []
desc = Buffer.create(BinFmt.SECTION_HEADER_SIZE)
constructor(public size?: number) {
if (this.size == null) this.size = -1
}
finalize(off: number) {
assert(this.offset == -1 || this.offset == off)
this.offset = off
if (this.size == -1) this.size = this.currSize
assert(this.size == this.currSize)
assert((this.offset & 3) == 0)
assert((this.size & 3) == 0)
write32(this.desc, 0, this.offset)
write32(this.desc, 4, this.size)
}
align() {
while (this.currSize & 3) this.append(Buffer.create(1))
}
append(buf: Buffer) {
this.data.push(buf)
this.currSize += buf.length
if (this.size >= 0) assertRange(0, this.currSize, this.size)
}
}
export class DelayedCodeSection {
startLabel: Label
returnLabel: Label
body: ((wr: OpWriter) => void)[] = []
constructor(public name: string, public parent: OpWriter) {}
empty() {
return this.body.length == 0
}
emit(f: (wr: OpWriter) => void) {
this.body.push(f)
}
callHere() {
const wr = this.parent
this.startLabel = wr.mkLabel(this.name + "Start")
this.returnLabel = wr.mkLabel(this.name + "Ret")
wr.emitJump(this.startLabel)
wr.emitLabel(this.returnLabel)
}
finalizeRaw() {
this.parent.assertCurrent()
const wr = this.parent
for (const b of this.body) b(wr)
}
finalize() {
if (this.empty()) {
this.parent._setLabelOffset(
this.startLabel,
this.returnLabel.offset
)
return
}
const wr = this.parent
wr.emitLabel(this.startLabel)
this.finalizeRaw()
wr.emitJump(this.returnLabel)
}
}
}