| 1 | // Copyright (c) 2023 l-m.dev. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | module wasm |
| 5 | |
| 6 | import encoding.leb128 |
| 7 | import math.bits |
| 8 | |
| 9 | fn (mut mod Module) u32(v u32) { |
| 10 | mod.buf << leb128.encode_u32(v) |
| 11 | } |
| 12 | |
| 13 | fn (mut mod Module) patch_start() int { |
| 14 | return mod.buf.len |
| 15 | } |
| 16 | |
| 17 | fn (mut mod Module) patch_len(pos int) { |
| 18 | len := mod.buf.len - pos |
| 19 | data := leb128.encode_u32(u32(len)) |
| 20 | mod.buf.insert(pos, data) |
| 21 | } |
| 22 | |
| 23 | fn (mut mod Module) patch_u32(pos int, val u32) { |
| 24 | data := leb128.encode_u32(val) |
| 25 | mod.buf.insert(pos, data) |
| 26 | } |
| 27 | |
| 28 | fn (mut mod Module) result_type(results []ValType) { |
| 29 | mod.u32(u32(results.len)) |
| 30 | for r in results { |
| 31 | mod.buf << u8(r) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | fn (mut mod Module) function_type(ft FuncType) { |
| 36 | mod.buf << 0x60 // function type indicator |
| 37 | mod.result_type(ft.parameters) |
| 38 | mod.result_type(ft.results) |
| 39 | } |
| 40 | |
| 41 | fn (mut mod Module) global_type(vt ValType, is_mut bool) { |
| 42 | mod.buf << u8(vt) |
| 43 | mod.buf << u8(is_mut) |
| 44 | } |
| 45 | |
| 46 | fn push_f32(mut buf []u8, v f32) { |
| 47 | rv := bits.f32_bits(v) |
| 48 | buf << u8(rv >> u32(0)) |
| 49 | buf << u8(rv >> u32(8)) |
| 50 | buf << u8(rv >> u32(16)) |
| 51 | buf << u8(rv >> u32(24)) |
| 52 | } |
| 53 | |
| 54 | fn push_f64(mut buf []u8, v f64) { |
| 55 | rv := bits.f64_bits(v) |
| 56 | buf << u8(rv >> u32(0)) |
| 57 | buf << u8(rv >> u32(8)) |
| 58 | buf << u8(rv >> u32(16)) |
| 59 | buf << u8(rv >> u32(24)) |
| 60 | buf << u8(rv >> u32(32)) |
| 61 | buf << u8(rv >> u32(40)) |
| 62 | buf << u8(rv >> u32(48)) |
| 63 | buf << u8(rv >> u32(56)) |
| 64 | } |
| 65 | |
| 66 | fn (mod &Module) get_function_idx(patch CallPatch) int { |
| 67 | mut idx := -1 |
| 68 | |
| 69 | match patch { |
| 70 | FunctionCallPatch { |
| 71 | ftt := mod.functions[patch.name] or { |
| 72 | panic('called function ${patch.name} does not exist') |
| 73 | } |
| 74 | idx = ftt.idx + mod.fn_imports.len |
| 75 | } |
| 76 | ImportCallPatch { |
| 77 | for fnidx, c in mod.fn_imports { |
| 78 | if c.mod == patch.mod && c.name == patch.name { |
| 79 | idx = fnidx |
| 80 | break |
| 81 | } |
| 82 | } |
| 83 | if idx == -1 { |
| 84 | panic('called imported function ${patch.mod}.${patch.name} does not exist') |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return idx |
| 90 | } |
| 91 | |
| 92 | fn (mut mod Module) patch(ft Function) { |
| 93 | mut ptr := 0 |
| 94 | |
| 95 | for patch in ft.patches { |
| 96 | mut idx := 0 |
| 97 | match patch { |
| 98 | CallPatch { |
| 99 | idx = mod.get_function_idx(patch) |
| 100 | } |
| 101 | FunctionGlobalPatch { |
| 102 | idx = mod.global_imports.len + patch.idx |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | mod.buf << ft.code[ptr..patch.pos] |
| 107 | mod.u32(u32(idx)) |
| 108 | ptr = patch.pos |
| 109 | } |
| 110 | |
| 111 | mod.buf << ft.code[ptr..] |
| 112 | } |
| 113 | |
| 114 | fn (mut mod Module) name(name string) { |
| 115 | mod.u32(u32(name.len)) |
| 116 | mod.buf << name.bytes() |
| 117 | } |
| 118 | |
| 119 | fn (mut mod Module) start_subsection(sec Subsection) int { |
| 120 | mod.buf << u8(sec) |
| 121 | return mod.patch_start() |
| 122 | } |
| 123 | |
| 124 | fn (mut mod Module) start_section(sec Section) int { |
| 125 | mod.buf << u8(sec) |
| 126 | return mod.patch_start() |
| 127 | } |
| 128 | |
| 129 | fn (mut mod Module) end_section(tpatch int) { |
| 130 | mod.patch_len(tpatch) |
| 131 | } |
| 132 | |
| 133 | // compile serialises the WebAssembly module into a byte array. |
| 134 | // The returned byte array can be written out into a `.wasm`, or executed in memory. |
| 135 | pub fn (mut mod Module) compile() []u8 { |
| 136 | mod.buf = []u8{cap: 128} |
| 137 | |
| 138 | // WASM_BINARY_MAGIC, WASM_BINARY_VERSION |
| 139 | mod.buf << [u8(0x00), 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00] |
| 140 | |
| 141 | // https://webassembly.github.io/spec/core/binary/modules.html#type-section |
| 142 | // |
| 143 | if mod.functypes.len > 0 { |
| 144 | tpatch := mod.start_section(.type_section) |
| 145 | { |
| 146 | mod.u32(u32(mod.functypes.len)) |
| 147 | for ft in mod.functypes { |
| 148 | mod.function_type(ft) |
| 149 | } |
| 150 | } |
| 151 | mod.end_section(tpatch) |
| 152 | } |
| 153 | // https://webassembly.github.io/spec/core/binary/modules.html#import-section |
| 154 | // |
| 155 | if mod.fn_imports.len > 0 || mod.global_imports.len > 0 { |
| 156 | tpatch := mod.start_section(.import_section) |
| 157 | { |
| 158 | mod.u32(u32(mod.fn_imports.len + mod.global_imports.len)) |
| 159 | for ft in mod.fn_imports { |
| 160 | mod.name(ft.mod) |
| 161 | mod.name(ft.name) |
| 162 | mod.buf << 0x00 // function |
| 163 | mod.u32(u32(ft.tidx)) |
| 164 | } |
| 165 | for gt in mod.global_imports { |
| 166 | mod.name(gt.mod) |
| 167 | mod.name(gt.name) |
| 168 | mod.buf << 0x03 // global |
| 169 | mod.global_type(gt.typ, gt.is_mut) |
| 170 | } |
| 171 | } |
| 172 | mod.end_section(tpatch) |
| 173 | } |
| 174 | // https://webassembly.github.io/spec/core/binary/modules.html#binary-funcsec |
| 175 | // |
| 176 | if mod.functions.len > 0 { |
| 177 | tpatch := mod.start_section(.function_section) |
| 178 | { |
| 179 | mod.u32(u32(mod.functions.len)) |
| 180 | for _, ft in mod.functions { |
| 181 | mod.u32(u32(ft.tidx)) |
| 182 | } |
| 183 | } |
| 184 | mod.end_section(tpatch) |
| 185 | } |
| 186 | // https://webassembly.github.io/spec/core/binary/modules.html#binary-memsec |
| 187 | // |
| 188 | if memory := mod.memory { |
| 189 | tpatch := mod.start_section(.memory_section) |
| 190 | { |
| 191 | mod.u32(1) |
| 192 | if max := memory.max { |
| 193 | mod.buf << 0x01 // limit, max present |
| 194 | mod.u32(memory.min) |
| 195 | mod.u32(max) |
| 196 | } else { |
| 197 | mod.buf << 0x00 // limit, max not present |
| 198 | mod.u32(memory.min) |
| 199 | } |
| 200 | } |
| 201 | mod.end_section(tpatch) |
| 202 | } |
| 203 | // https://webassembly.github.io/spec/core/binary/modules.html#global-section |
| 204 | // |
| 205 | if mod.globals.len > 0 { |
| 206 | tpatch := mod.start_section(.global_section) |
| 207 | { |
| 208 | mod.u32(u32(mod.globals.len)) |
| 209 | for gt in mod.globals { |
| 210 | mod.global_type(gt.typ, gt.is_mut) |
| 211 | |
| 212 | { |
| 213 | mut ptr := 0 |
| 214 | for patch in gt.init.call_patches { |
| 215 | idx := mod.get_function_idx(patch) |
| 216 | |
| 217 | mod.buf << gt.init.code[ptr..patch.pos] |
| 218 | mod.u32(u32(idx)) |
| 219 | ptr = patch.pos |
| 220 | } |
| 221 | mod.buf << gt.init.code[ptr..] |
| 222 | } |
| 223 | mod.buf << 0x0B // END expression opcode |
| 224 | } |
| 225 | } |
| 226 | mod.end_section(tpatch) |
| 227 | } |
| 228 | // https://webassembly.github.io/spec/core/binary/modules.html#export-section |
| 229 | // |
| 230 | { |
| 231 | tpatch := mod.start_section(.export_section) |
| 232 | { |
| 233 | lpatch := mod.patch_start() |
| 234 | mut lsz := 0 |
| 235 | for _, ft in mod.functions { |
| 236 | if !ft.export { |
| 237 | continue |
| 238 | } |
| 239 | lsz++ |
| 240 | mod.name(ft.export_name or { ft.name }) |
| 241 | mod.buf << 0x00 // function |
| 242 | mod.u32(u32(ft.idx + mod.fn_imports.len)) |
| 243 | } |
| 244 | if memory := mod.memory { |
| 245 | if memory.export { |
| 246 | lsz++ |
| 247 | mod.name(memory.name) |
| 248 | mod.buf << 0x02 // function |
| 249 | mod.u32(0) |
| 250 | } |
| 251 | } |
| 252 | for idx, gbl in mod.globals { |
| 253 | if !gbl.export { |
| 254 | continue |
| 255 | } |
| 256 | lsz++ |
| 257 | mod.name(gbl.name) |
| 258 | mod.buf << 0x03 // global |
| 259 | mod.u32(u32(idx + mod.global_imports.len)) |
| 260 | } |
| 261 | mod.patch_u32(lpatch, u32(lsz)) |
| 262 | } |
| 263 | mod.end_section(tpatch) |
| 264 | } |
| 265 | // https://webassembly.github.io/spec/core/binary/modules.html#binary-startsec |
| 266 | // |
| 267 | if start := mod.start { |
| 268 | ftt := mod.functions[start] or { panic('start function ${start} does not exist') } |
| 269 | tpatch := mod.start_section(.start_section) |
| 270 | { |
| 271 | mod.u32(u32(ftt.idx + mod.fn_imports.len)) |
| 272 | } |
| 273 | mod.end_section(tpatch) |
| 274 | } |
| 275 | // https://webassembly.github.io/spec/core/binary/modules.html#data-count-section |
| 276 | // |
| 277 | if mod.segments.len > 0 { |
| 278 | tpatch := mod.start_section(.data_count_section) |
| 279 | { |
| 280 | mod.u32(u32(mod.segments.len)) |
| 281 | } |
| 282 | mod.end_section(tpatch) |
| 283 | } |
| 284 | // https://webassembly.github.io/spec/core/binary/modules.html#binary-codesec |
| 285 | // |
| 286 | if mod.functions.len > 0 { |
| 287 | tpatch := mod.start_section(.code_section) |
| 288 | { |
| 289 | mod.u32(u32(mod.functions.len)) |
| 290 | for _, ft in mod.functions { |
| 291 | fpatch := mod.patch_start() |
| 292 | rloc := ft.locals[mod.functypes[ft.tidx].parameters.len..] |
| 293 | { |
| 294 | mod.u32(u32(rloc.len)) |
| 295 | for lt in rloc { |
| 296 | mod.u32(1) |
| 297 | mod.buf << u8(lt.typ) |
| 298 | } |
| 299 | mod.patch(ft) |
| 300 | mod.buf << 0x0B // END expression opcode |
| 301 | } |
| 302 | mod.patch_len(fpatch) |
| 303 | } |
| 304 | } |
| 305 | mod.end_section(tpatch) |
| 306 | } |
| 307 | // https://webassembly.github.io/spec/core/binary/modules.html#data-section |
| 308 | // |
| 309 | if mod.segments.len > 0 { |
| 310 | tpatch := mod.start_section(.data_section) |
| 311 | { |
| 312 | mod.u32(u32(mod.segments.len)) |
| 313 | for _, seg in mod.segments { |
| 314 | if idx := seg.idx { |
| 315 | mod.buf << 0x00 // active |
| 316 | // constant expr |
| 317 | mod.buf << 0x41 // i32.const |
| 318 | mod.buf << leb128.encode_i32(i32(idx)) |
| 319 | mod.buf << 0x0B // END expression opcode |
| 320 | } else { |
| 321 | mod.buf << 0x01 // passive |
| 322 | } |
| 323 | mod.u32(u32(seg.data.len)) |
| 324 | mod.buf << seg.data |
| 325 | } |
| 326 | } |
| 327 | mod.end_section(tpatch) |
| 328 | } |
| 329 | // https://webassembly.github.io/spec/core/appendix/custom.html#name-section |
| 330 | // |
| 331 | if mod.debug { |
| 332 | tpatch := mod.start_section(.custom_section) |
| 333 | mod.name('name') |
| 334 | if mod_name := mod.mod_name { |
| 335 | mpatch := mod.start_subsection(.name_module) |
| 336 | { |
| 337 | mod.name(mod_name) |
| 338 | } |
| 339 | mod.end_section(mpatch) |
| 340 | } |
| 341 | { |
| 342 | mpatch := mod.start_subsection(.name_function) |
| 343 | { |
| 344 | mod.u32(u32(mod.functions.len + mod.fn_imports.len)) |
| 345 | mut idx := 0 |
| 346 | for f in mod.fn_imports { |
| 347 | mod.u32(u32(idx)) |
| 348 | mod.name('${f.mod}.${f.name}') |
| 349 | idx++ |
| 350 | } |
| 351 | for n, _ in mod.functions { |
| 352 | mod.u32(u32(idx)) |
| 353 | mod.name(n) |
| 354 | idx++ |
| 355 | } |
| 356 | } |
| 357 | mod.end_section(mpatch) |
| 358 | } |
| 359 | { |
| 360 | mpatch := mod.start_subsection(.name_local) |
| 361 | { |
| 362 | fpatch := mod.patch_start() |
| 363 | mut fcount := 0 |
| 364 | mut idx := mod.fn_imports.len // after imports |
| 365 | for _, ft in mod.functions { |
| 366 | // only add entry if it contains a local with an assigned name |
| 367 | if ft.locals.any(it.name != none) { |
| 368 | mod.u32(u32(idx)) // function idx |
| 369 | |
| 370 | mut lcount := 0 |
| 371 | lcpatch := mod.patch_start() |
| 372 | for lidx, loc in ft.locals { |
| 373 | if name := loc.name { |
| 374 | mod.u32(u32(lidx)) |
| 375 | mod.name(name) |
| 376 | lcount++ |
| 377 | } |
| 378 | } |
| 379 | mod.patch_u32(lcpatch, u32(lcount)) |
| 380 | fcount++ |
| 381 | } |
| 382 | idx++ |
| 383 | } |
| 384 | mod.patch_u32(fpatch, u32(fcount)) |
| 385 | } |
| 386 | mod.end_section(mpatch) |
| 387 | } |
| 388 | { |
| 389 | mpatch := mod.start_subsection(.name_type) |
| 390 | { |
| 391 | fpatch := mod.patch_start() |
| 392 | mut fcount := 0 |
| 393 | for idx, ft in mod.functypes { |
| 394 | if name := ft.name { |
| 395 | mod.u32(u32(idx)) |
| 396 | mod.name(name) |
| 397 | fcount++ |
| 398 | } |
| 399 | } |
| 400 | mod.patch_u32(fpatch, u32(fcount)) |
| 401 | } |
| 402 | mod.end_section(mpatch) |
| 403 | } |
| 404 | if memory := mod.memory { |
| 405 | mpatch := mod.start_subsection(.name_memory) |
| 406 | { |
| 407 | mod.u32(u32(1)) // one memory in vec |
| 408 | mod.u32(u32(0)) // 0 idx |
| 409 | mod.name(memory.name) // memory name |
| 410 | } |
| 411 | mod.end_section(mpatch) |
| 412 | } |
| 413 | if mod.globals.len != 0 || mod.global_imports.len != 0 { |
| 414 | mpatch := mod.start_subsection(.name_global) |
| 415 | { |
| 416 | fpatch := mod.patch_start() |
| 417 | mut fcount := 0 |
| 418 | for gbl in mod.global_imports { |
| 419 | mod.u32(u32(fcount)) |
| 420 | mod.name('${gbl.mod}.${gbl.name}') |
| 421 | fcount++ |
| 422 | } |
| 423 | for gbl in mod.globals { |
| 424 | mod.u32(u32(fcount)) |
| 425 | mod.name(gbl.name) |
| 426 | fcount++ |
| 427 | } |
| 428 | mod.patch_u32(fpatch, u32(fcount)) |
| 429 | } |
| 430 | mod.end_section(mpatch) |
| 431 | } |
| 432 | if mod.segments.any(it.name != none) { |
| 433 | mpatch := mod.start_subsection(.name_data) |
| 434 | { |
| 435 | fpatch := mod.patch_start() |
| 436 | mut fcount := 0 |
| 437 | for idx, ds in mod.segments { |
| 438 | if name := ds.name { |
| 439 | mod.u32(u32(idx)) |
| 440 | mod.name(name) |
| 441 | fcount++ |
| 442 | } |
| 443 | } |
| 444 | mod.patch_u32(fpatch, u32(fcount)) |
| 445 | } |
| 446 | mod.end_section(mpatch) |
| 447 | } |
| 448 | |
| 449 | mod.end_section(tpatch) |
| 450 | } |
| 451 | |
| 452 | return mod.buf |
| 453 | } |
| 454 | |