1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vlib: add a wasm module, implementing a pure V webassembly seralisation library (#17909)

This commit is contained in:
l-m 2023-04-09 14:55:02 +10:00 committed by GitHub
parent 9658d20f03
commit d2f69472b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 1966 additions and 0 deletions

View File

@ -0,0 +1,13 @@
import wasm
fn main() {
mut m := wasm.Module{}
mut func := m.new_function('add', [.i32_t, .i32_t], [.i32_t])
{
func.local_get(0)
func.local_get(1)
func.add(.i32_t)
}
m.commit(func, true) // `export: true`
print(m.compile().bytestr())
}

View File

@ -0,0 +1,43 @@
import wasm
fn main() {
mut m := wasm.Module{}
mut bif := m.new_function('block_if', [.i32_t], [.i32_t])
{
loc := bif.new_local(.i32_t)
// loc = i32.const 10
bif.i32_const(10)
bif.local_set(loc)
blk := bif.c_block([], [])
{
// if argument[0], break
bif.local_get(0)
bif.c_br_if(blk)
// loc = i32.const 11
bif.i32_const(11)
bif.local_set(loc)
}
bif.c_end(blk)
// return loc
bif.local_get(loc)
}
m.commit(bif, true)
mut ifexpr := m.new_function('if_expr', [.i32_t], [.i64_t])
{
ifexpr.local_get(0)
ifexpr.c_if([], [.i64_t])
{
ifexpr.i64_const(5000)
}
ifexpr.c_else()
{
ifexpr.i64_const(-5000)
}
ifexpr.c_end_if()
}
m.commit(ifexpr, true)
print(m.compile().bytestr())
}

View File

@ -0,0 +1,33 @@
import wasm
fn main() {
mut m := wasm.Module{}
mut fac := m.new_function('fac', [.i64_t], [.i64_t])
{
fac.local_get(0)
fac.eqz(.i64_t)
fac.c_if([], [.i64_t])
{
fac.i64_const(1)
}
fac.c_else()
{
{
fac.local_get(0)
}
{
fac.local_get(0)
fac.i64_const(1)
fac.sub(.i64_t)
fac.call('fac')
}
fac.mul(.i64_t)
}
fac.c_end_if()
}
m.commit(fac, true)
print(m.compile().bytestr())
// v run factorial.v > a.wasm
// wasmer a.wasm -i fac 5
}

View File

@ -0,0 +1,29 @@
import wasm
fn main() {
mut m := wasm.Module{}
mut pyth := m.new_function('pythagoras', [.f32_t, .f32_t], [
.f32_t,
])
{
pyth.local_get(0)
pyth.local_get(0)
pyth.mul(.f32_t)
pyth.local_get(1)
pyth.local_get(1)
pyth.mul(.f32_t)
pyth.add(.f32_t)
pyth.sqrt(.f32_t)
pyth.cast(.f32_t, true, .f64_t)
}
m.commit(pyth, true)
mut test := m.new_function('test', [.f32_t], [.f64_t])
{
test.local_get(0)
test.f32_const(10.0)
test.call('pythagoras')
test.cast(.f32_t, true, .f64_t)
}
m.commit(test, true)
print(m.compile().bytestr())
}

View File

@ -0,0 +1,28 @@
import wasm
fn main() {
mut m := wasm.Module{}
m.new_function_import('wasi_unstable', 'proc_exit', [.i32_t], [])
m.new_function_import('wasi_unstable', 'fd_write', [.i32_t, .i32_t, .i32_t, .i32_t],
[.i32_t])
m.assign_memory('memory', true, 1, none)
m.new_data_segment(0, [u8(8), 0, 0, 0]) // pointer to string
m.new_data_segment(4, [u8(13), 0, 0, 0]) // length of string
m.new_data_segment(8, 'Hello, WASI!\n'.bytes())
mut func := m.new_function('_start', [], [])
{
func.i32_const(1) // stdout
func.i32_const(0) // *iovs
func.i32_const(1) // 1 iov
func.i32_const(-1) // *retptrs
func.call_import('wasi_unstable', 'fd_write')
func.drop()
func.i32_const(0)
func.call_import('wasi_unstable', 'proc_exit')
}
m.commit(func, true)
print(m.compile().bytestr())
}

View File

@ -0,0 +1,12 @@
import wasm
fn main() {
mut m := wasm.Module{}
mut mtest := m.new_function('mload', [.i32_t], [.i32_t])
{
mtest.local_get(0)
mtest.load(.i32_t, 2, 0)
}
m.commit(mtest, false)
print(m.compile().bytestr())
}

37
vlib/wasm/README.md Normal file
View File

@ -0,0 +1,37 @@
## Description:
The `wasm` module is a pure V implementation of the WebAssembly bytecode module format,
available in the form of a builder.
It allows users to generate WebAssembly modules in memory.
With the V wasm module, users can create functions, opcodes, and utilize the entire wasm
specification without the need for a large dependency like binaryen. All of this
functionality is available within V itself, making the module a valuable resource for
V developers seeking to build high-performance web applications.
The module is designed to generate a `[]u8`, which can be written to a `.wasm` file
or executed in memory.
```v
import wasm
import os
fn main() {
mut m := wasm.Module{}
mut func := m.new_function('add', [.i32_t, .i32_t], [.i32_t])
{
func.local_get(0) // | local.get 0
func.local_get(1) // | local.get 1
func.add(.i32_t) // | i32.add
}
m.commit(func, true) // `export: true`
mod := m.compile() // []u8
os.write_file_array('add.wasm', mod)!
}
```
This module does not perform verification of the WebAssembly output.
Use a tool like `wasm-validate` to validate, and `wasm-dis` to show a decompiled form.

244
vlib/wasm/encoding.v Normal file
View File

@ -0,0 +1,244 @@
module wasm
import encoding.leb128
fn (mut mod Module) u32(v u32) {
mod.buf << leb128.encode_u32(v)
}
fn (mut mod Module) patch_start() int {
return mod.buf.len
}
fn (mut mod Module) patch_len(pos int) {
len := mod.buf.len - pos
data := leb128.encode_u32(u32(len))
mod.buf.insert(pos, data)
}
fn (mut mod Module) patch_u32(pos int, val u32) {
data := leb128.encode_u32(val)
mod.buf.insert(pos, data)
}
fn (mut mod Module) result_type(results []ValType) {
mod.u32(u32(results.len))
for r in results {
mod.buf << u8(r)
}
}
fn (mut mod Module) function_type(ft FuncType) {
mod.buf << 0x60 // function type indicator
mod.result_type(ft.parameters)
mod.result_type(ft.results)
}
fn (mut mod Module) patch_calls(ft Function) {
mut ptr := 0
for patch in ft.call_patches {
mut idx := -1
match patch {
FunctionCallPatch {
ftt := mod.functions[patch.name] or {
panic('called function ${patch.name} does not exist')
}
idx = ftt.idx + mod.fn_imports.len
}
ImportCallPatch {
for fnidx, c in mod.fn_imports {
if c.mod == patch.mod && c.name == patch.name {
idx = fnidx
break
}
}
if idx == -1 {
panic('called imported function ${patch.mod}.${patch.name} does not exist')
}
}
}
mod.buf << ft.code[ptr..patch.pos]
mod.buf << 0x10 // call
mod.u32(u32(idx))
ptr = patch.pos
}
mod.buf << ft.code[ptr..]
}
// compile serialises the WebAssembly module into a byte array.
// The returned byte array can be written out into a `.wasm`, or executed in memory.
pub fn (mut mod Module) compile() []u8 {
mod.buf = []u8{cap: 128}
// WASM_BINARY_MAGIC, WASM_BINARY_VERSION
mod.buf << [u8(0x00), 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]
// https://webassembly.github.io/spec/core/binary/modules.html#type-section
//
if mod.functypes.len > 0 {
// Types
mod.buf << u8(Section.type_section)
tpatch := mod.patch_start()
{
mod.u32(u32(mod.functypes.len))
for ft in mod.functypes {
mod.function_type(ft)
}
}
mod.patch_len(tpatch)
}
// https://webassembly.github.io/spec/core/binary/modules.html#import-section
//
if mod.fn_imports.len > 0 {
// Types
mod.buf << u8(Section.import_section)
tpatch := mod.patch_start()
{
mod.u32(u32(mod.fn_imports.len))
for ft in mod.fn_imports {
mod.u32(u32(ft.mod.len))
mod.buf << ft.mod.bytes()
mod.u32(u32(ft.name.len))
mod.buf << ft.name.bytes()
mod.buf << 0x00 // function
mod.u32(u32(ft.tidx))
}
}
mod.patch_len(tpatch)
}
// https://webassembly.github.io/spec/core/binary/modules.html#binary-funcsec
//
if mod.functions.len > 0 {
mod.buf << u8(Section.function_section)
tpatch := mod.patch_start()
{
mod.u32(u32(mod.functions.len))
for _, ft in mod.functions {
mod.u32(u32(ft.tidx))
}
}
mod.patch_len(tpatch)
}
// https://webassembly.github.io/spec/core/binary/modules.html#binary-memsec
//
if memory := mod.memory {
mod.buf << u8(Section.memory_section)
tpatch := mod.patch_start()
{
mod.u32(1)
if max := memory.max {
mod.buf << 0x01 // limit, max present
mod.u32(memory.min)
mod.u32(max)
} else {
mod.buf << 0x00 // limit, max not present
mod.u32(memory.min)
}
}
mod.patch_len(tpatch)
}
// https://webassembly.github.io/spec/core/binary/modules.html#export-section
//
if mod.functions.len > 0 {
mod.buf << u8(Section.export_section)
tpatch := mod.patch_start()
{
lpatch := mod.patch_start()
mut lsz := 0
for _, ft in mod.functions {
if !ft.export {
continue
}
lsz++
mod.u32(u32(ft.name.len))
mod.buf << ft.name.bytes()
mod.buf << 0x00 // function
mod.u32(u32(ft.idx + mod.fn_imports.len))
}
if memory := mod.memory {
if memory.export {
lsz++
mod.u32(u32(memory.name.len))
mod.buf << memory.name.bytes()
mod.buf << 0x02 // function
mod.u32(0)
}
}
mod.patch_u32(lpatch, u32(lsz))
}
mod.patch_len(tpatch)
}
// https://webassembly.github.io/spec/core/binary/modules.html#binary-startsec
//
if start := mod.start {
ftt := mod.functions[start] or { panic('start function ${start} does not exist') }
mod.buf << u8(Section.start_section)
tpatch := mod.patch_start()
{
mod.u32(u32(ftt.idx + mod.fn_imports.len))
}
mod.patch_len(tpatch)
}
// https://webassembly.github.io/spec/core/binary/modules.html#data-count-section
//
if mod.segments.len > 0 {
mod.buf << u8(Section.data_count_section)
tpatch := mod.patch_start()
{
mod.u32(u32(mod.segments.len))
}
mod.patch_len(tpatch)
}
// https://webassembly.github.io/spec/core/binary/modules.html#binary-codesec
//
if mod.functions.len > 0 {
mod.buf << u8(Section.code_section)
tpatch := mod.patch_start()
{
mod.u32(u32(mod.functions.len))
for _, ft in mod.functions {
fpatch := mod.patch_start()
{
mod.u32(u32(ft.locals.len))
for lt in ft.locals {
mod.u32(1)
mod.buf << u8(lt)
}
mod.patch_calls(ft)
mod.buf << 0x0B // END expression opcode
}
mod.patch_len(fpatch)
}
}
mod.patch_len(tpatch)
}
// https://webassembly.github.io/spec/core/binary/modules.html#data-section
//
if mod.segments.len > 0 {
mod.buf << u8(Section.data_section)
tpatch := mod.patch_start()
{
mod.u32(u32(mod.segments.len))
for _, seg in mod.segments {
if idx := seg.idx {
mod.buf << 0x00 // active
// constant expr
mod.buf << 0x41 // i32.const
mod.buf << leb128.encode_i32(idx)
mod.buf << 0x0B // END expression opcode
} else {
mod.buf << 0x01 // passive
}
mod.u32(u32(seg.data.len))
mod.buf << seg.data
}
}
mod.patch_len(tpatch)
}
return mod.buf
}

28
vlib/wasm/functions.v Normal file
View File

@ -0,0 +1,28 @@
module wasm
struct ImportCallPatch {
mod string
name string
pos int
}
struct FunctionCallPatch {
name string
pos int
}
type CallPatch = FunctionCallPatch | ImportCallPatch
struct Function {
tidx int
idx int
mut:
call_patches []CallPatch
label int
export bool
mod &Module
code []u8
locals []ValType
pub:
name string
}

1039
vlib/wasm/instructions.v Normal file

File diff suppressed because it is too large Load Diff

155
vlib/wasm/module.v Normal file
View File

@ -0,0 +1,155 @@
module wasm
enum Section as u8 {
custom_section
type_section
import_section
function_section
table_section
memory_section
global_section
export_section
start_section
element_section
code_section
data_section
data_count_section
}
pub enum NumType as u8 {
i32_t = 0x7f
i64_t = 0x7e
f32_t = 0x7d
f64_t = 0x7c
}
pub enum ValType as u8 {
i32_t = 0x7f
i64_t = 0x7e
f32_t = 0x7d
f64_t = 0x7c
v128_t = 0x7b
funcref_t = 0x70
externref_t = 0x6f
}
// Module contains the WebAssembly module.
// Use the `compile` method to compile the module into a pure byte array.
[heap]
pub struct Module {
mut:
buf []u8
functypes []FuncType
functions map[string]Function
memory ?Memory
start ?string
fn_imports []FunctionImport
segments []DataSegment
}
struct FunctionImport {
mod string
name string
tidx int
}
struct Memory {
name string
export bool
min u32
max ?u32
}
struct DataSegment {
idx ?int
data []u8
}
[params]
pub struct FuncType {
pub:
parameters []ValType
results []ValType
}
fn (mut mod Module) new_functype(ft FuncType) int {
// interns existing types
mut idx := mod.functypes.index(ft)
if idx == -1 {
idx = mod.functypes.len
mod.functypes << ft
}
return idx
}
// new_function creates a function struct.
pub fn (mut mod Module) new_function(name string, parameters []ValType, results []ValType) Function {
assert name !in mod.functions.keys()
idx := mod.functions.len
tidx := mod.new_functype(FuncType{parameters, results})
return Function{
name: name
tidx: tidx
idx: idx
mod: mod
}
}
// assign_memory assigns memory to the current module.
pub fn (mut mod Module) assign_memory(name string, export bool, min u32, max ?u32) {
mod.memory = Memory{
name: name
export: export
min: min
max: max
}
}
// assign_start assigns the start function to the current module.
pub fn (mut mod Module) assign_start(name string) {
mod.start = name
}
// new_function_import imports a new function into the current module.
pub fn (mut mod Module) new_function_import(modn string, name string, parameters []ValType, results []ValType) {
assert !mod.fn_imports.any(it.mod == modn && it.name == name)
tidx := mod.new_functype(FuncType{parameters, results})
mod.fn_imports << FunctionImport{
mod: modn
name: name
tidx: tidx
}
}
// commit commits a function to the module, use `export` to export the function.
pub fn (mut mod Module) commit(func Function, export bool) {
assert func.name !in mod.functions.keys()
mod.functions[func.name] = Function{
...func
export: export
}
}
// new_data_segment inserts a new data segment at the memory index `pos`.
pub fn (mut mod Module) new_data_segment(pos int, data []u8) int {
len := mod.segments.len
mod.segments << DataSegment{
idx: pos
data: data
}
return len
}
// new_passive_data_segment inserts a new passive data segment.
pub fn (mut mod Module) new_passive_data_segment(data []u8) {
mod.segments << DataSegment{
data: data
}
}

View File

@ -0,0 +1,56 @@
import wasm
import os
const exe = os.find_abs_path_of_executable('wasm-validate') or { exit(0) }
fn validate(mod []u8) ! {
mut proc := os.new_process(exe)
proc.set_args(['-'])
proc.set_redirect_stdio()
proc.run()
{
os.fd_write(proc.stdio_fd[0], mod.bytestr())
os.fd_close(proc.stdio_fd[0])
}
proc.wait()
if proc.status != .exited {
return error('wasm-validate exited abormally')
}
if proc.code != 0 {
return error('wasm-validate exited with a non zero exit code')
}
proc.close()
}
fn test_add() {
mut m := wasm.Module{}
mut a1 := m.new_function('add', [.i32_t, .i32_t], [.i32_t])
{
a1.local_get(0)
a1.local_get(1)
a1.add(.i32_t)
}
m.commit(a1, true)
mut a2 := m.new_function('sub', [.i32_t, .i32_t], [.i32_t])
{
a2.local_get(0)
a2.local_get(1)
a2.sub(.i32_t)
}
m.commit(a2, true)
mut a3 := m.new_function('mul', [.i32_t, .i32_t], [.i32_t])
{
a3.local_get(0)
a3.local_get(1)
a3.mul(.i32_t)
}
m.commit(a3, true)
mut a4 := m.new_function('div', [.i32_t, .i32_t], [.i32_t])
{
a4.local_get(0)
a4.local_get(1)
a4.div(.i32_t, true)
}
m.commit(a4, true)
validate(m.compile()) or { panic(err) }
}

View File

@ -0,0 +1,143 @@
import wasm
import os
const exe = os.find_abs_path_of_executable('wasm-validate') or { exit(0) }
fn validate(mod []u8) ! {
mut proc := os.new_process(exe)
proc.set_args(['-'])
proc.set_redirect_stdio()
proc.run()
{
os.fd_write(proc.stdio_fd[0], mod.bytestr())
os.fd_close(proc.stdio_fd[0])
}
proc.wait()
if proc.status != .exited {
return error('wasm-validate exited abormally')
}
if proc.code != 0 {
return error('wasm-validate exited with a non zero exit code')
}
proc.close()
}
fn test_add() {
mut m := wasm.Module{}
mut a1 := m.new_function('param', [], [.i32_t])
{
a1.i32_const(1)
blk := a1.c_block([.i32_t], [.i32_t])
{
a1.i32_const(2)
a1.add(.i32_t)
}
a1.c_end(blk)
}
m.commit(a1, true)
mut a2 := m.new_function('params', [], [.i32_t])
{
a2.i32_const(1)
a2.i32_const(2)
blk := a2.c_block([.i32_t, .i32_t], [.i32_t])
{
a2.add(.i32_t)
}
a2.c_end(blk)
}
m.commit(a2, true)
mut a3 := m.new_function('params-id', [], [.i32_t])
{
a3.i32_const(1)
a3.i32_const(2)
blk := a3.c_block([.i32_t, .i32_t], [.i32_t, .i32_t])
{
}
a3.c_end(blk)
a3.add(.i32_t)
}
m.commit(a3, true)
mut b1 := m.new_function('param-break', [], [.i32_t])
{
b1.i32_const(1)
blk := b1.c_block([.i32_t], [.i32_t])
{
b1.i32_const(2)
b1.add(.i32_t)
b1.c_br(blk)
}
b1.c_end(blk)
}
m.commit(b1, true)
mut b2 := m.new_function('params-break', [], [.i32_t])
{
b2.i32_const(1)
b2.i32_const(2)
blk := b2.c_block([.i32_t, .i32_t], [.i32_t])
{
b2.add(.i32_t)
b2.c_br(blk)
}
b2.c_end(blk)
}
m.commit(b2, true)
mut b3 := m.new_function('params-id-break', [], [.i32_t])
{
b3.i32_const(1)
b3.i32_const(2)
blk := b3.c_block([.i32_t, .i32_t], [.i32_t, .i32_t])
{
b3.c_br(blk)
}
b3.c_end(blk)
b3.add(.i32_t)
}
m.commit(b3, true)
mut dummy := m.new_function('dummy', [], [])
{
}
m.commit(dummy, false)
mut c1 := m.new_function('singular', [], [.i32_t])
{
blk1 := c1.c_block([], [])
{
c1.nop()
}
c1.c_end(blk1)
blk2 := c1.c_block([], [.i32_t])
{
c1.i32_const(7)
}
c1.c_end(blk2)
}
m.commit(c1, true)
mut c2 := m.new_function('nested', [], [.i32_t])
{
blk := c2.c_block([], [.i32_t])
{
blk1 := c2.c_block([], [])
{
c2.call('dummy')
blk2 := c2.c_block([], [])
{
}
c2.c_end(blk2)
c2.nop()
}
c2.c_end(blk1)
blk2 := c2.c_block([], [.i32_t])
{
c2.call('dummy')
c2.i32_const(9)
}
c2.c_end(blk2)
}
c2.c_end(blk)
}
m.commit(c2, true)
validate(m.compile()) or { panic(err) }
}

106
vlib/wasm/tests/call_test.v Normal file
View File

@ -0,0 +1,106 @@
import wasm
import os
const exe = os.find_abs_path_of_executable('wasm-validate') or { exit(0) }
fn validate(mod []u8) ! {
mut proc := os.new_process(exe)
proc.set_args(['-'])
proc.set_redirect_stdio()
proc.run()
{
os.fd_write(proc.stdio_fd[0], mod.bytestr())
os.fd_close(proc.stdio_fd[0])
}
proc.wait()
if proc.status != .exited {
return error('wasm-validate exited abormally')
}
if proc.code != 0 {
return error('wasm-validate exited with a non zero exit code')
}
proc.close()
}
fn test_add() {
mut m := wasm.Module{}
mut a1 := m.new_function('const-i32', [], [.i32_t])
{
a1.i32_const(0x132)
}
m.commit(a1, false)
mut a2 := m.new_function('const-i64', [], [.i64_t])
{
a2.i64_const(0x164)
}
m.commit(a2, false)
mut a3 := m.new_function('const-f32', [], [.f32_t])
{
a3.f32_const(0.2)
}
m.commit(a3, false)
mut a4 := m.new_function('const-f64', [], [.f64_t])
{
a4.f64_const(0.4)
}
m.commit(a4, false)
mut a5 := m.new_function('const-i32-i64', [], [.i32_t, .i64_t])
{
a5.i32_const(0x132)
a5.i64_const(0x164)
}
m.commit(a5, false)
mut b1 := m.new_function('type-i32', [], [.i32_t])
{
b1.call('const-i32')
}
m.commit(b1, true)
mut b2 := m.new_function('type-i64', [], [.i64_t])
{
b2.call('const-i64')
}
m.commit(b2, true)
mut b3 := m.new_function('type-f32', [], [.f32_t])
{
b3.call('const-f32')
}
m.commit(b3, true)
mut b4 := m.new_function('type-f64', [], [.f64_t])
{
b4.call('const-f64')
}
m.commit(b4, true)
mut b5 := m.new_function('type-i32-i64', [], [.i32_t, .i64_t])
{
b5.call('const-i32-i64')
}
m.commit(b5, true)
mut fac := m.new_function('fac', [.i64_t], [.i64_t])
{
fac.local_get(0)
fac.eqz(.i64_t)
fac.c_if([], [.i64_t])
{
fac.i64_const(1)
}
fac.c_else()
{
{
fac.local_get(0)
}
{
fac.local_get(0)
fac.i64_const(1)
fac.sub(.i64_t)
fac.call('fac')
}
fac.mul(.i64_t)
}
fac.c_end_if()
}
m.commit(fac, true)
validate(m.compile()) or { panic(err) }
}