Initial project import
This commit is contained in:
commit
23f2d29e32
11957 files changed
+1754577
No files matched your search
@@ -0,0 +1,36 @@
|
||||
module main
|
||||
|
||||
import wasm
|
||||
|
||||
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) }
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
module main
|
||||
|
||||
import wasm
|
||||
|
||||
fn test_block() {
|
||||
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) }
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
module main
|
||||
|
||||
import wasm
|
||||
|
||||
fn test_call() {
|
||||
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)
|
||||
ifs := fac.c_if([], [.i64_t])
|
||||
{
|
||||
fac.i64_const(1)
|
||||
}
|
||||
fac.c_else(ifs)
|
||||
{
|
||||
{
|
||||
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(ifs)
|
||||
}
|
||||
m.commit(fac, true)
|
||||
|
||||
validate(m.compile()) or { panic(err) }
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
module main
|
||||
|
||||
import os
|
||||
|
||||
const pid = os.getpid()
|
||||
|
||||
const wasm_validate_exe = find_wasm_validate() or {
|
||||
println('>>> Skipping test, since wasm-validate could not be found, error: ${err}')
|
||||
exit(0)
|
||||
}
|
||||
|
||||
fn find_wasm_validate() !string {
|
||||
// Prefer to find our own version first, if it was installed already
|
||||
// through install_wabt.vsh, since it is more likely to be known, recent, and stable:
|
||||
thirdpart_wasm_validate_folder := os.join_path(@VEXEROOT, 'thirdparty', 'wabt', 'bin')
|
||||
extension := $if windows { '.exe' } $else { '' }
|
||||
wasm_validate_executable := os.join_path(thirdpart_wasm_validate_folder,
|
||||
'wasm-validate${extension}')
|
||||
if os.exists(wasm_validate_executable) {
|
||||
return wasm_validate_executable
|
||||
}
|
||||
if path := os.find_abs_path_of_executable('wasm-validate') {
|
||||
return path
|
||||
}
|
||||
return error('could not find wasm-validate executable in thirdparty/ as well, try first `v run cmd/tools/install_wabt.vsh`')
|
||||
}
|
||||
|
||||
// validate validates the given wasm code using `wasm-validate` executable.
|
||||
pub fn validate(code []u8) ! {
|
||||
println('validating using: ${wasm_validate_exe}')
|
||||
outfile := os.join_path(os.temp_dir(), 'code_${pid}.wasm')
|
||||
os.write_file(outfile, code.bytestr())!
|
||||
validation_cmd := '${os.quoted_path(wasm_validate_exe)} ${os.quoted_path(outfile)}'
|
||||
res := os.execute(validation_cmd)
|
||||
if res.exit_code != 0 {
|
||||
eprintln('failed exit code: ${res.exit_code} | command:\n${validation_cmd}')
|
||||
return error('wasm-validate exited with a non zero exit code: ${res.exit_code}')
|
||||
}
|
||||
os.rm(outfile)!
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
module main
|
||||
|
||||
import wasm
|
||||
|
||||
fn test_debug() {
|
||||
mut m := wasm.Module{}
|
||||
m.enable_debug('hello VLANG!')
|
||||
|
||||
// FuncType with debuginfo/name
|
||||
func_type := wasm.FuncType{[.i32_t, .i32_t], [.i32_t], 'fn_<int_int>_int'}
|
||||
|
||||
// []?string, named function parameters
|
||||
param_names := [?string('a'), 'b']
|
||||
|
||||
mut a1 := m.new_debug_function('add', func_type, param_names)
|
||||
{
|
||||
loc := a1.new_local_named(.i32_t, 'intermediary')
|
||||
a1.local_get(0)
|
||||
a1.local_get(1)
|
||||
a1.add(.i32_t)
|
||||
a1.local_set(loc)
|
||||
a1.local_get(loc)
|
||||
}
|
||||
m.commit(a1, false)
|
||||
validate(m.compile()) or { panic(err) }
|
||||
}
|
||||
|
||||
fn test_wasi_debug() {
|
||||
mut m := wasm.Module{}
|
||||
m.enable_debug('hello!')
|
||||
m.new_global('__vsp', true, .i32_t, true, wasm.constexpr_value(10))
|
||||
|
||||
m.new_global_import('env', 'glble', .i32_t, false)
|
||||
|
||||
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('CIOVec.str', 0, [u8(8), 0, 0, 0]) // pointer to string
|
||||
m.new_data_segment('CIOVec.len', 4, [u8(13), 0, 0, 0]) // length of string
|
||||
m.new_data_segment(none, 8, 'Hello, WASI!\n'.bytes())
|
||||
|
||||
mut func := m.new_function('_start', [], [])
|
||||
{
|
||||
func.new_local_named(.i32_t, 'loc')
|
||||
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())
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
module main
|
||||
|
||||
import wasm
|
||||
|
||||
fn test_patch() {
|
||||
mut m := wasm.Module{}
|
||||
vsp := m.new_global('__vsp', false, .i32_t, true, wasm.constexpr_value(10))
|
||||
mut at := m.new_function('add', [], [])
|
||||
{
|
||||
pp := at.patch_pos()
|
||||
at.drop()
|
||||
at.i32_const(10)
|
||||
at.i32_const(10)
|
||||
at.i32_const(10)
|
||||
at.drop()
|
||||
at.drop()
|
||||
at.drop()
|
||||
pps := at.patch_pos()
|
||||
{
|
||||
at.i32_const(10)
|
||||
at.i32_const(10)
|
||||
at.global_set(vsp)
|
||||
at.i32_const(10)
|
||||
at.global_set(vsp)
|
||||
}
|
||||
at.patch(pp, pps) // move to start
|
||||
}
|
||||
m.commit(at, true)
|
||||
validate(m.compile()) or { panic(err) }
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
module main
|
||||
|
||||
import wasm
|
||||
|
||||
fn test_globals() {
|
||||
mut m := wasm.Module{}
|
||||
m.enable_debug('vlang')
|
||||
|
||||
vsp := m.new_global('__vsp', false, .i32_t, true, wasm.constexpr_value(10))
|
||||
mut func := m.new_function('vsp', [], [.i32_t])
|
||||
{
|
||||
func.global_get(vsp)
|
||||
func.i32_const(20)
|
||||
func.add(.i32_t)
|
||||
func.global_set(vsp)
|
||||
func.global_get(vsp)
|
||||
}
|
||||
m.commit(func, true)
|
||||
|
||||
fref := m.new_global('__ref', true, .funcref_t, true, wasm.constexpr_ref_null(.funcref_t))
|
||||
mut func1 := m.new_function('ref', [], [])
|
||||
{
|
||||
func1.ref_func('vsp')
|
||||
func1.global_set(fref)
|
||||
}
|
||||
m.commit(func1, true)
|
||||
|
||||
gimport := m.new_global_import('env', '__import', .f64_t, false)
|
||||
mut func2 := m.new_function('import', [], [.f64_t])
|
||||
{
|
||||
func2.global_get(gimport)
|
||||
}
|
||||
m.commit(func2, true)
|
||||
|
||||
validate(m.compile()) or { panic(err) }
|
||||
}
|
||||
Reference in new issue
Block a user