-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathrepr.ts
More file actions
405 lines (356 loc) · 11.5 KB
/
repr.ts
File metadata and controls
405 lines (356 loc) · 11.5 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
// repr(n :N, w :Writer, options? :Options) :void
// Represents a tree visually as plain text (with optional terminal colors.)
//
import { t, N, AnyOp, VarUint7 } from './ast'
import { utf8 } from './utf8'
import { uint8, uint16, uint32, float32, float64 } from './basic-types';
import { opcodes } from './info'
export type Writer = (s :string)=>void
export interface Options {
readonly colors :boolean // explicitly enable or disable terminal colors
readonly immSeparator :string // defaults to `:`
readonly detailedTypes? :boolean, // `vi32(9)` or just `9`
}
interface Ctx {
readonly write :(depth :number, s :string)=>void
readonly writeln :(depth :number, s :string)=>void
readonly writeinline :(s :string)=>void
readonly options :Options
}
function symname(y :Symbol) {
const s = y.toString()
return s.length > 8 ? s.substr(7,s.length-8) : 'Symbol(?)';
}
// const indln = (indent? :string) => (indent ? '\n' + indent : '')
const style = (str :string, style :string) => '\x1B[' + style + 'm' + str + '\x1B[0m'
const reprop = (op :uint8) => style(opcodes.get(op), '96')
const styleType = (s :string) => style(s, '33')
function readVarInt7(byte :uint8) {
return byte < 64 ? byte : -(128 - byte)
}
function visitAll(nodes :N[], c :Ctx, depth :number) {
for (let n of nodes) {
visit(n, c, depth)
}
}
function visitCell(n :N, tname :string, c :Ctx, depth :number) {
c.write(depth, '(' + tname)
visitAll(n.v, c, depth + 1)
c.write(depth, ')')
}
function visitValue(v :any, tname :string, c :Ctx, depth :number) {
const s = String(v)
return c.write(depth, c.options.detailedTypes ? `${tname}(${s})` : s)
}
function visit(n :N, c :Ctx, depth :number) {
const tname = style(symname(n.t), '92')
switch (n.t) {
// Atoms
case t.uint8:
case t.uint16:
case t.uint32:
case t.varuint1:
case t.varuint7:
case t.varuint32:
case t.varint32:
case t.varint64:
case t.float32:
case t.float64: {
return visitValue(n.v, tname, c, depth)
}
case t.varint7: {
return visitValue(readVarInt7(n.v), tname, c, depth)
}
case t.external_kind: {
switch (n.v) {
case 0: return c.write(depth, styleType('external_kind.function'))
case 1: return c.write(depth, styleType('external_kind.table'))
case 2: return c.write(depth, styleType('external_kind.memory'))
case 3: return c.write(depth, styleType('external_kind.global'))
default: return visitValue(readVarInt7(n.v), tname, c, depth)
}
}
// Instructions
case t.instr: {
return c.write(depth, reprop(n.v))
}
case t.instr_imm1: {
c.write(depth, '(' + reprop(n.v) + ' [')
visit((n as AnyOp).imm as N, c, depth + 1)
return c.write(depth, '])')
}
case t.instr_pre: {
c.write(depth, '(' + reprop(n.v))
visitAll((n as AnyOp).pre as N[], c, depth + 1)
return c.writeln(depth , ')')
}
case t.instr_pre1: {
c.write(depth, '(' + reprop(n.v))
visit((n as AnyOp).pre as N, c, depth + 1)
return c.writeln(depth, ')')
}
case t.instr_imm1_post: {
c.write(depth, '(' + reprop(n.v) + ' [')
visit((n as AnyOp).imm as N, c, depth + 1)
c.write(depth, ']')
visitAll((n as AnyOp).post as N[], c, depth + 1)
return c.write(depth, ')')
}
case t.instr_pre_imm: {
c.write(depth, '(' + reprop(n.v) + ' [')
visitAll((n as AnyOp).imm as N[], c, depth + 1)
c.write(depth, ']')
visitAll((n as AnyOp).pre as N[], c, depth + 1)
return c.write(depth, ')')
}
case t.instr_pre_imm_post: {
c.write(depth, '(' + reprop(n.v) + ' [')
visitAll((n as AnyOp).imm as N[], c, depth + 1)
c.write(depth, ']')
visitAll((n as AnyOp).pre as N[], c, depth + 1)
visitAll((n as AnyOp).post as N[], c, depth + 1)
return c.write(depth, ')')
}
// TODO: special case of "if"
// {
// let s = ind(indent) + '(' + reprop(this.v)
//
// if (this.v == 0x04/*if*/) {
// let cind = (indent || '') + ' '
//
// s += ' ' + this.imm[0].repr() + // type
// this.pre[0].repr(cind) + // cond
// ind(cind) + '(then';
//
// let i = 1, ci = (cind || '') + ' ', E = this.imm.length-1 // skip `end`
// for (; i < E; ++i) {
// let n = this.imm[i]
// if (n.v == 0x05/*else*/) {
// s += ')' // end of "then"
// break
// }
// s += ' ' + n.repr(ci)
// }
//
// if (i < E) {
// s += ind(cind) + '(else';
// ++i
// for (; i < E; ++i) {
// let n = this.imm[i]
// s += ' ' + n.repr(ci)
// }
// }
//
// return s + ') end)' // end of "then" or "else"
// }
//
// return s + `${reprv(indent, this.imm)}${reprv(indent, this.pre)})`
// }
case t.data: {
let v :string[] = []
for (let i = 0, L = n.v.length; i != L; ++i) {
if (v.length == 8) { v.push('...') ;break }
v.push((n.v[i] as number).toString(16))
}
return c.write(depth, '(' + tname + ' ' + v.join(' ') + ')')
}
case t.type: {
switch (n.v) {
case -1: return c.write(depth, styleType('i32'))
case -2: return c.write(depth, styleType('i64'))
case -3: return c.write(depth, styleType('f32'))
case -4: return c.write(depth, styleType('f64'))
case -0x10: return c.write(depth, styleType('anyfunc'))
case -0x20: return c.write(depth, styleType('func'))
case -0x40: return c.write(depth, styleType('void')) // aka empty_block
default: return c.write(depth, styleType('?'))
}
}
// —————————————————————————————————————————————————————————————————
// Cells
case t.module: {
c.write(depth, '(' + tname + ' ' + n.v[1].v)
visitAll(n.v.slice(2), c, depth + 1)
return c.write(depth, ')')
}
case t.section: {
const id = (n.v[0] as VarUint7).v
const name = [
'custom', // 0 Custom named section
'type', // 1 Function signature declarations
'import', // 2 Import declarations
'function', // 3 Function declarations
'table', // 4 Indirect function table and other tables
'memory', // 5 Memory attributes
'global', // 6 Global declarations
'export', // 7 Exports
'start', // 8 Start function declaration
'element', // 9 Elements section
'code', // 10 Function bodies (code)
'data', // 11 Data segments
][id] || ('0x' + id.toString(16))
c.write(depth, '(' + tname + ' ' + name)
visitAll(n.v.slice(1), c, depth + 1)
return c.write(depth, ')')
}
case t.import_entry:
case t.export_entry:
case t.local_entry:
case t.table_type:
case t.memory_type: {
return visitCell(n, tname, c, depth)
}
case t.func_type: {
// func, paramCount, paramT*, retCount, retT* -> "(" paramT* ")" retT
c.write(depth, '(' + tname + ' (')
const paramCount = n.v[1].v as number
const resCount = n.v[1 + paramCount + 1].v as number
visitAll(n.v.slice(2, 2+paramCount), c, depth)
c.writeinline(')')
if (resCount > 0) {
visitAll(n.v.slice(1 + paramCount + 2), c, depth)
}
return c.write(depth, ')')
}
case t.global_type: {
c.write(depth, '(' + tname)
visitAll(n.v.slice(0, n.v.length-1), c, depth + 1)
c.write(depth + 1, (n.v[1].v ? ' mutable' : 'immutable'))
return c.write(depth, ')')
}
case t.resizable_limits: {
return c.write(depth,
'(' + tname + ' ' + n.v[1].v + '..' + (n.v[0].v ? n.v[2].v : '') + ')'
)
}
case t.global_variable:
case t.init_expr:
case t.elem_segment:
case t.data_segment:
case t.function_body: {
return visitCell(n, tname, c, depth)
}
case t.str: {
return c.write(depth, '"' + utf8.decode(n.v) + '"')
}
default:
throw new Error('unexpected type ' + n.t.toString())
}
}
export function reprBuffer(
buffer :ArrayBuffer,
w :Writer,
limit? :number,
highlightRange? :number[],
options? :Options)
{
limit = Math.min(buffer.byteLength, limit || Infinity)
if (highlightRange && !highlightRange[1]) {
highlightRange[1] = highlightRange[0]+1
}
let s = [], i = 0, view = new DataView(buffer)
for (; i < limit; ++i) {
let b = view.getUint8(i)
const inHLRange = (highlightRange && i < highlightRange[1] && i >= highlightRange[0])
if (b == 0 && !inHLRange) {
s.push('\x1B[2m00\x1B[0m')
} else {
let str = b.toString(16)
s.push(
inHLRange ?
('\x1B[45;97m' + (str.length == 1 ? '0' : '') + str + '\x1B[0m' ) :
str.length == 1 ? '\x1B[2m0\x1B[0m' + str :
str
)
}
if (s.length % 20 == 19) {
w(s.join(' ') + '\n');
s = [];
} else if (s.length % 5 == 4) {
s.push('')
}
}
const tail = (highlightRange && highlightRange[0] >= i) ? '\x1B[45;97m..\x1B[0m' : ''
if (s.length) {
w(s.join(' ') + (tail ? ' ' + tail : '') + '\n');
} else if (tail) {
w(tail + '\n')
}
}
export function repr(n :N, w :Writer, options? :Options) {
const maxLineLength = 40
const SP = ' '
let currLineLen = 0
let lastCh = ''
let writer = w
if (!options || options.colors !== false) {
// writer that colors immediate brackets
let immOpen = style('[', '2'),
immClose = style(']', '2')
writer = s => {
w(s.replace(/([^\x1B]|^)[\[\]]/g, m =>
m.length == 2 ?
m[0] + (m[1] == '[' ? immOpen : immClose) :
(m[0] == '[' ? immOpen : immClose)
))
}
}
const ctx = {
options: Object.assign({
// detailedTypes: true,
immSeparator: ':',
}, options || {}) as Options,
writeln(depth :number, chunk :string) {
const line = SP.substr(0, depth * 2) + chunk
currLineLen = line.length
lastCh = chunk[chunk.length-1]
writer('\n' + line)
},
writeinline(chunk :string) {
currLineLen += chunk.length
lastCh = chunk[chunk.length-1]
writer(chunk)
},
write(depth :number, chunk :string) {
if (currLineLen > 0 && depth > 0) {
const ch0 = chunk[0]
if (ch0 == '(') {
return this.writeln(depth, chunk)
}
if (ch0 != ')' && ch0 != ']' &&
lastCh != '[' && lastCh != '(')
{
currLineLen += 1
chunk = ' ' + chunk
}
}
this.writeinline(chunk)
},
}
visit(n, ctx, 0)
}
// Simple Writer that buffers everything as a string that
export function BufferedWriter() {
const buf = []
const w = s => { buf.push(s) }
w.toString = () => buf.join('')
return w
}
// Convenience functions that returns strings.
// Will be slower and use more memory than `repr` but convenient for
// visualizing smaller structures.
export function strRepr(n :N, options? :Options) {
const w = BufferedWriter()
repr(n, w, options)
return w.toString()
}
export function strReprBuffer(
buffer :ArrayBuffer,
limit? :number,
highlightRange? :number[],
options? :Options)
{
const w = BufferedWriter()
reprBuffer(buffer, w, limit, highlightRange, options)
return w.toString()
}