forked from tanishiking/scala-wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWasmTextWriter.scala
460 lines (408 loc) · 12.8 KB
/
WasmTextWriter.scala
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
package wasm
package converters
import wasm.wasm4s._
import wasm.wasm4s.Names._
import wasm.wasm4s.Types._
import wasm.wasm4s.WasmInstr._
class WasmTextWriter {
import WasmTextWriter._
def write(module: WasmModule): String = {
implicit val b = new WatBuilder()
writeModule(module)
b.toString()
}
private def writeModule(module: WasmModule)(implicit b: WatBuilder): Unit = {
b.newLineList(
"module", {
b.newLineList(
"rec", {
module.recGroupTypes.foreach(writeGCTypeDefinition)
module.functionTypes.foreach(writeFunctionType)
module.arrayTypes.foreach(writeGCTypeDefinition)
}
)
module.imports.foreach(writeImport)
module.definedFunctions.foreach(writeFunction)
module.tags.foreach(writeTag)
module.globals.foreach(writeGlobal)
module.exports.foreach(writeExport)
module.startFunction.foreach(writeStart)
module.elements.foreach(writeElement)
module.data.foreach(writeData)
}
)
}
private def writeGCTypeDefinition(
t: WasmGCTypeDefinition
)(implicit b: WatBuilder): Unit = {
def writeField(field: WasmStructField): Unit = {
b.sameLineList(
"field", {
b.appendElement(field.name.show)
if (field.isMutable)
b.sameLineList(
"mut", {
writeType(field.typ)
}
)
else writeType(field.typ)
}
)
}
b.newLineList(
"type", {
b.appendElement(t.name.show)
t match {
case WasmArrayType(name, field) =>
b.sameLineList(
"array", {
if (field.isMutable)
b.sameLineList(
"mut", {
writeType(field.typ)
}
)
else writeType(field.typ)
}
)
// (type $kotlin.Any___type_13 (struct (field (ref $kotlin.Any.vtable___type_12)) (field (ref null struct)) (field (mut i32)) (field (mut i32))))
case WasmStructType(name, fields, superType) =>
b.sameLineList(
"sub", {
superType.foreach(s => b.appendElement(s.show))
b.sameLineList(
"struct", {
fields.foreach(writeField)
}
)
}
)
}
}
)
}
private def writeFunctionType(
functionType: WasmFunctionType
)(implicit b: WatBuilder): Unit =
// (type type-name (func (param ty) (param ty) (result ty)))
b.newLineList(
"type", {
b.appendElement(functionType.name.show)
b.sameLineList(
"func", {
functionType.params.foreach { ty =>
b.sameLineList("param", writeType(ty))
}
if (functionType.results.nonEmpty)
functionType.results.foreach { ty =>
b.sameLineList("result", writeType(ty))
}
}
)
}
)
private def writeImport(i: WasmImport)(implicit b: WatBuilder): Unit = {
b.newLineList(
"import", {
b.appendElement("\"" + i.module + "\"")
b.appendElement("\"" + i.name + "\"")
i.desc match {
case WasmImportDesc.Func(id, typ) =>
b.sameLineList(
"func", {
b.appendElement(id.show)
writeSig(typ.params, typ.results)
}
)
case WasmImportDesc.Global(id, typ, isMutable) =>
b.sameLineList(
"global", {
b.appendElement(id.show)
if (isMutable)
b.sameLineList("mut", writeType(typ))
else
writeType(typ)
}
)
case WasmImportDesc.Tag(id, typ) =>
b.sameLineList(
"tag", {
b.appendElement(id.show)
writeSig(typ.params, typ.results)
}
)
}
}
)
}
private def writeSig(params: List[WasmType], results: List[WasmType])(implicit
b: WatBuilder
): Unit = {
params.foreach(typ => b.sameLineList("param", writeType(typ)))
results.foreach(typ => b.sameLineList("result", writeType(typ)))
}
private def writeFunction(f: WasmFunction)(implicit b: WatBuilder): Unit = {
def writeParam(l: WasmLocal)(implicit b: WatBuilder): Unit = {
b.sameLineList(
"param", {
b.appendElement(l.name.show)
writeType(l.typ)
}
)
}
def writeLocal(l: WasmLocal)(implicit b: WatBuilder): Unit = {
b.sameLineList(
"local", {
b.appendElement(l.name.show)
writeType(l.typ)
}
)
}
b.newLineList(
"func", {
val (params, nonParams) = f.locals.partition(_.isParameter)
b.appendElement(f.name.show)
b.sameLineListOne("type", f.typ.name.show)
b.newLine()
params.foreach(writeParam)
f.typ.results.foreach(r => { b.sameLineList("result", writeType(r)) })
b.newLine()
if (nonParams.nonEmpty) {
nonParams.foreach(writeLocal)
}
f.body.instr.foreach(writeInstr)
}
)
}
private def writeTag(tag: WasmTag)(implicit b: WatBuilder): Unit = {
b.newLineList(
"tag", {
b.appendElement(tag.name.show)
b.sameLineListOne("type", tag.typ.show)
}
)
}
private def writeGlobal(g: WasmGlobal)(implicit b: WatBuilder) =
b.newLineList(
"global", {
b.appendElement(g.name.show)
if (g.isMutable)
b.sameLineList("mut", writeType(g.typ))
else writeType(g.typ)
g.init.instr.foreach(writeInstr)
}
)
private def writeExport(e: WasmExport)(implicit
b: WatBuilder
) = b.newLineList(
"export", {
b.appendElement("\"" + e.exportName + "\"")
e match {
case WasmExport.Function(_, funcName) =>
b.sameLineList(
"func",
{ b.appendElement(funcName.show) }
)
case WasmExport.Global(_, globalName) =>
b.sameLineList(
"global",
{ b.appendElement(globalName.show) }
)
}
}
)
private def writeStart(startFunction: WasmFunctionName)(implicit b: WatBuilder): Unit = {
b.newLineList(
"start", {
b.appendElement(startFunction.show)
}
)
}
private def writeElement(element: WasmElement)(implicit b: WatBuilder): Unit = {
b.newLineList(
"elem", {
element.mode match {
case WasmElement.Mode.Passive => ()
case WasmElement.Mode.Declarative => b.appendElement("declare")
}
writeType(element.typ)
element.init.foreach { item =>
b.newLineList(
"item",
item.instr.foreach(writeInstr(_))
)
}
}
)
}
private def writeData(data: WasmData)(implicit b: WatBuilder): Unit = {
b.newLineList(
"data", {
b.appendElement(data.name.show)
data.mode match {
case WasmData.Mode.Passive => ()
}
b.appendElement("\"" + data.bytes.map("\\%02x".format(_)).mkString + "\"")
}
)
}
private def writeType(typ: WasmStorageType)(implicit b: WatBuilder): Unit = {
typ match {
case typ: WasmSimpleType => b.appendElement(typ.textName)
case typ: WasmPackedType => b.appendElement(typ.textName)
case WasmRefType(true, heapType: WasmHeapType.AbsHeapType) =>
b.appendElement(heapType.nullableRefTextName)
case WasmRefType(nullable, heapType) =>
b.sameLineList(
"ref", {
if (nullable)
b.appendElement("null")
writeHeapType(heapType)
}
)
}
}
private def writeHeapType(heapType: WasmHeapType)(implicit b: WatBuilder): Unit = {
heapType match {
case WasmHeapType.Type(typeName) => b.appendElement(typeName.show)
case heapType: WasmHeapType.AbsHeapType => b.appendElement(heapType.textName)
}
}
private def floatString(v: Double): String = {
if (v.isNaN()) "nan"
else if (v == Double.PositiveInfinity) "inf"
else if (v == Double.NegativeInfinity) "-inf"
else if (v.equals(-0.0)) "-0.0"
else v.toString()
}
private def writeBlockType(blockType: BlockType)(implicit b: WatBuilder): Unit = {
blockType match {
case BlockType.FunctionType(name) =>
b.appendElement(s"(type ${name.show})")
case BlockType.ValueType(optTy) =>
for (ty <- optTy)
b.sameLineList("result", writeType(ty))
}
}
private def writeLabelIdx(labelIdx: WasmLabelName)(implicit b: WatBuilder): Unit =
b.appendElement(labelIdx.show)
private def writeInstr(instr: WasmInstr)(implicit b: WatBuilder): Unit = {
instr match {
case END | ELSE | _: CATCH | CATCH_ALL => b.deindent()
case _ => ()
}
b.newLine()
b.appendElement(instr.mnemonic)
instr match {
case instr: StructuredLabeledInstr =>
instr.label.foreach(writeLabelIdx(_))
case _ =>
()
}
writeInstrImmediates(instr)
instr match {
case _: StructuredLabeledInstr | ELSE | _: CATCH | CATCH_ALL => b.indent()
case _ => ()
}
}
private def writeInstrImmediates(instr: WasmInstr)(implicit b: WatBuilder): Unit = {
instr match {
// Convenience categories
case instr: WasmSimpleInstr =>
()
case instr: WasmBlockTypeLabeledInstr =>
writeBlockType(instr.blockTypeArgument)
case instr: WasmLabelInstr =>
writeLabelIdx(instr.labelArgument)
case instr: WasmFuncInstr =>
b.appendElement(instr.funcArgument.show)
case instr: WasmTypeInstr =>
b.appendElement(instr.typeArgument.show)
case instr: WasmTagInstr =>
b.appendElement(instr.tagArgument.show)
case instr: WasmLocalInstr =>
b.appendElement(instr.localArgument.show)
case instr: WasmGlobalInstr =>
b.appendElement(instr.globalArgument.show)
case instr: WasmHeapTypeInstr =>
writeHeapType(instr.heapTypeArgument)
case instr: WasmRefTypeInstr =>
writeType(instr.refTypeArgument)
case instr: WasmStructFieldInstr =>
b.appendElement(instr.structTypeName.show)
b.appendElement(instr.fieldIdx.value.toString())
// Specific instructions with unique-ish shapes
case I32_CONST(v) => b.appendElement(v.toString())
case I64_CONST(v) => b.appendElement(v.toString())
case F32_CONST(v) => b.appendElement(floatString(v.toDouble))
case F64_CONST(v) => b.appendElement(floatString(v))
case BR_TABLE(labelIdxVector, defaultLabelIdx) =>
labelIdxVector.foreach(writeLabelIdx(_))
writeLabelIdx(defaultLabelIdx)
case TRY_TABLE(blockType, clauses, _) =>
writeBlockType(blockType)
for (clause <- clauses) {
b.sameLineList(
clause.mnemonic, {
clause.tag.foreach(tag => b.appendElement(tag.show))
writeLabelIdx(clause.label)
}
)
}
case ARRAY_NEW_DATA(typeIdx, dataIdx) =>
b.appendElement(typeIdx.show)
b.appendElement(dataIdx.show)
case ARRAY_NEW_FIXED(typeIdx, length) =>
b.appendElement(typeIdx.show)
b.appendElement(Integer.toUnsignedString(length))
case ARRAY_COPY(destType, srcType) =>
b.appendElement(destType.show)
b.appendElement(srcType.show)
case BR_ON_CAST(labelIdx, from, to) =>
writeLabelIdx(labelIdx)
writeType(from)
writeType(to)
case BR_ON_CAST_FAIL(labelIdx, from, to) =>
writeLabelIdx(labelIdx)
writeType(from)
writeType(to)
}
}
}
object WasmTextWriter {
private class WatBuilder {
private val builder = new StringBuilder
private var level = 0
private val indentStr = " "
private def indented(body: => Unit): Unit = {
level += 1
body
level -= 1
}
def indent(): Unit = level += 1
def deindent(): Unit = level -= 1
def newLine(): Unit = {
builder.append("\n")
builder.append(indentStr * level)
}
def newLineList(name: String, body: => Unit): Unit = {
newLine()
builder.append(s"($name")
indented(body)
builder.append(")")
}
def sameLineList(name: String, body: => Unit): Unit = {
builder.append(s" ($name")
body
builder.append(")")
}
def sameLineListOne(name: String, value: String) =
sameLineList(name, { appendElement(value) })
def appendElement(value: String): Unit = {
builder.append(" ")
builder.append(value)
}
override def toString: String =
builder.toString()
}
}