mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
vlib: add a wasm module, implementing a pure V webassembly seralisation library (#17909)
This commit is contained in:
parent
9658d20f03
commit
d2f69472b2
14 changed files with 1966 additions and 0 deletions
13
examples/wasm_codegen/add.v
Normal file
13
examples/wasm_codegen/add.v
Normal 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())
|
||||
}
|
43
examples/wasm_codegen/control_flow.v
Normal file
43
examples/wasm_codegen/control_flow.v
Normal 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())
|
||||
}
|
33
examples/wasm_codegen/factorial.v
Normal file
33
examples/wasm_codegen/factorial.v
Normal 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
|
||||
}
|
29
examples/wasm_codegen/functions.v
Normal file
29
examples/wasm_codegen/functions.v
Normal 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())
|
||||
}
|
28
examples/wasm_codegen/hello_wasi.v
Normal file
28
examples/wasm_codegen/hello_wasi.v
Normal 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())
|
||||
}
|
12
examples/wasm_codegen/memory.v
Normal file
12
examples/wasm_codegen/memory.v
Normal 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())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue