-
Notifications
You must be signed in to change notification settings - Fork 18
/
jacs_stringify.ts
193 lines (163 loc) · 4.94 KB
/
jacs_stringify.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
namespace jacs {
export function bitSize(fmt: NumFmt) {
return 8 << (fmt & 0b11)
}
let resolver: InstrArgResolver
let jmpoff: number
class OpTree {
args: OpTree[]
arg: number
constructor(public opcode: number) {}
}
export function stringifyInstr(
getbyte0: () => number,
resolver0?: InstrArgResolver
) {
let off = 0
jmpoff = NaN
const getbyte = () => {
off++
return getbyte0()
}
const decodeInt = () => {
const v = getbyte()
if (v < 0xf8) return v
let r = 0
const n = !!(v & 4)
const len = (v & 3) + 1
for (let i = 0; i < len; ++i) {
const v = getbyte()
r = r << 8
r |= v
}
return n ? -r : r
}
resolver = resolver0
const stack: OpTree[] = []
for (;;) {
const op = getbyte()
if (op == 0 && off == 1) return " .fill 0x00"
const e = new OpTree(op)
if (opTakesNumber(op)) {
if (resolver && resolver.resolverPC)
jmpoff = resolver.resolverPC + off - 1
e.arg = decodeInt()
}
let n = opNumRealArgs(op)
if (n) {
if (stack.length < n) return "???oops stack underflow"
e.args = stack.slice(stack.length - n)
while (n--) stack.pop()
}
stack.push(e)
if (opIsStmt(op)) break
}
if (stack.length != 1) return "???oops bad stack: " + stack.length
let res = " " + stringifyExpr(stack[0])
if (resolver) {
const pc = resolver.resolverPC
res = (pc > 9999 ? pc : (" " + pc).slice(-4)) + ": " + res
resolver = null // unroot it
}
return res
}
function expandFmt(fmt: string, t: OpTree) {
let ptr = 0
let beg = 0
let r = ""
while (ptr < fmt.length) {
if (fmt.charCodeAt(ptr) != 37) {
ptr++
continue
}
r += fmt.slice(beg, ptr)
ptr++
beg = ptr + 1
let e: string
let eNum: number = null
if (t.arg != undefined) {
eNum = t.arg
e = eNum + ""
t.arg = undefined
} else {
if (!t.args || !t.args.length) e = "???oops"
else e = stringifyExpr(t.args.shift())
if (isNumber(e)) eNum = +e
}
const ff = fmt[ptr]
switch (ff) {
case "e":
break
case "n":
e = numfmt(e)
break
case "o":
e = callop(e)
break
case "j":
e = jmpOffset(eNum)
break
default:
e = "_" + ff + e
if (eNum != null && resolver) {
const pref = resolver.describeCell(ff, eNum)
if (pref) e = pref + e
}
break
}
r += e
ptr++
}
r += fmt.slice(beg)
return r
}
function jmpOffset(off: number) {
const offs = (off >= 0 ? "+" : "") + off
return isNaN(jmpoff) ? offs : jmpoff + off + (" (" + offs + ")")
}
function isNumber(s: string) {
for (let i = 0; i < s.length; ++i) {
if ("0123456789".indexOf(s[i]) < 0) return false
}
return true
}
function numfmt(vv: string) {
if (!isNumber(vv)) return vv
const v = +vv
const fmt = v & 0xf
const bitsz = bitSize(fmt)
const letter = ["u", "i", "f", "x"][fmt >> 2]
const shift = v >> 4
if (shift) return letter + (bitsz - shift) + "." + shift
else return letter + bitsz
}
function callop(op: string) {
if (isNumber(op))
switch (+op) {
case OpCall.SYNC:
return ""
case OpCall.BG:
return " bg"
case OpCall.BG_MAX1:
return " bg (max1)"
case OpCall.BG_MAX1_PEND1:
return " bg (max1 pend1)"
default:
return ` call ${op}`
}
else return ` callop=${op}`
}
function stringifyExpr(t: OpTree): string {
const op = t.opcode
if (op >= BinFmt.DIRECT_CONST_OP)
return (
"" + (op - BinFmt.DIRECT_CONST_OP - BinFmt.DIRECT_CONST_OFFSET)
)
/*
const fmt = OP_PRINT_FMTS[op]
if (!fmt) return `???oops op${op}`
return expandFmt(fmt, t)
*/
return "uncomment"
}
}