Initial project import
This commit is contained in:
commit
23f2d29e32
11957 files changed
+1754577
No files matched your search
@@ -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())
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
import os
|
||||
import wasm
|
||||
|
||||
const runtime_page = 1024 * 64 // 64 KiBs
|
||||
|
||||
fn generate_ciovec(mut start wasm.Function, sp wasm.LocalIndex) {
|
||||
// construct struct __wasi_ciovec_t
|
||||
//
|
||||
// field, `const uint8_t *buf`
|
||||
start.i32_const(runtime_page)
|
||||
start.local_get(sp)
|
||||
start.store(.i32_t, 2, 0)
|
||||
// field, `__wasi_size_t buf_len`
|
||||
start.i32_const(runtime_page)
|
||||
start.i32_const(1) // len
|
||||
start.store(.i32_t, 2, 4)
|
||||
}
|
||||
|
||||
fn generate_code(mut start wasm.Function, bf_expr string) {
|
||||
// locals are initialised to zero, by spec
|
||||
sp := start.new_local_named(.i32_t, 'sp')
|
||||
|
||||
mut loop_labels := []wasm.LabelIndex{}
|
||||
mut block_labels := []wasm.LabelIndex{}
|
||||
|
||||
// our page, the second one
|
||||
|
||||
for ch in bf_expr {
|
||||
match ch {
|
||||
`>` {
|
||||
start.local_get(sp)
|
||||
start.i32_const(1)
|
||||
start.add(.i32_t)
|
||||
start.local_set(sp)
|
||||
}
|
||||
`<` {
|
||||
start.local_get(sp)
|
||||
start.i32_const(1)
|
||||
start.sub(.i32_t)
|
||||
start.local_set(sp)
|
||||
}
|
||||
`+` {
|
||||
start.local_get(sp)
|
||||
{
|
||||
start.local_get(sp)
|
||||
start.load8(.i32_t, false, 0, 0)
|
||||
start.i32_const(1)
|
||||
start.add(.i32_t)
|
||||
}
|
||||
start.store8(.i32_t, 0, 0)
|
||||
}
|
||||
`-` {
|
||||
start.local_get(sp)
|
||||
{
|
||||
start.local_get(sp)
|
||||
start.load8(.i32_t, false, 0, 0)
|
||||
start.i32_const(1)
|
||||
start.sub(.i32_t)
|
||||
}
|
||||
start.store8(.i32_t, 0, 0)
|
||||
}
|
||||
`.` {
|
||||
generate_ciovec(mut start, sp)
|
||||
|
||||
start.i32_const(1) // stdout
|
||||
start.i32_const(runtime_page) // *iovs
|
||||
start.i32_const(1) // iovs_len
|
||||
start.i32_const(runtime_page + 1024) // *nwritten
|
||||
start.call_import('wasi_unstable', 'fd_write')
|
||||
start.drop() // ignore errno
|
||||
}
|
||||
`,` {
|
||||
generate_ciovec(mut start, sp)
|
||||
|
||||
start.i32_const(0) // stdin
|
||||
start.i32_const(runtime_page) // *iovs
|
||||
start.i32_const(1) // iovs_len
|
||||
start.i32_const(runtime_page + 1024) // *nwritten
|
||||
start.call_import('wasi_unstable', 'fd_read')
|
||||
start.drop() // ignore errno
|
||||
}
|
||||
`[` {
|
||||
block_lbl := start.c_block([], [])
|
||||
loop_lbl := start.c_loop([], [])
|
||||
{
|
||||
start.local_get(sp)
|
||||
start.load8(.i32_t, false, 0, 0)
|
||||
start.eqz(.i32_t)
|
||||
start.c_br_if(block_lbl)
|
||||
}
|
||||
loop_labels << loop_lbl
|
||||
block_labels << block_lbl
|
||||
}
|
||||
`]` {
|
||||
loop_lbl := loop_labels.pop()
|
||||
start.c_br(loop_lbl) // jump back to top
|
||||
start.c_end(loop_lbl)
|
||||
start.c_end(block_labels.pop())
|
||||
}
|
||||
else {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@[noreturn]
|
||||
fn usage() {
|
||||
eprintln('Usage: bf <expr> <outfile.wasm>')
|
||||
eprintln(' or: bf <path/input.b> <outfile.wasm> (note the `.b` extension)')
|
||||
exit(1)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut bf_expr := os.args[1] or { usage() }
|
||||
if bf_expr.ends_with('.b') {
|
||||
bf_expr = os.read_file(bf_expr) or {
|
||||
eprintln('file ${bf_expr} could not be read, error: ${err}')
|
||||
usage()
|
||||
}
|
||||
}
|
||||
outfile := os.args[2] or { usage() }
|
||||
|
||||
mut m := wasm.Module{}
|
||||
m.enable_debug('wasm bf')
|
||||
m.new_function_import('wasi_unstable', 'fd_write', [.i32_t, .i32_t, .i32_t, .i32_t], [
|
||||
.i32_t,
|
||||
])
|
||||
m.new_function_import('wasi_unstable', 'fd_read', [.i32_t, .i32_t, .i32_t, .i32_t], [
|
||||
.i32_t,
|
||||
])
|
||||
m.assign_memory('memory', true, 2, none)
|
||||
|
||||
mut start := m.new_function('_start', [], [])
|
||||
{
|
||||
generate_code(mut start, bf_expr)
|
||||
}
|
||||
m.commit(start, true)
|
||||
|
||||
bytes := m.compile()
|
||||
os.write_file_array(outfile, bytes)!
|
||||
}
|
||||
@@ -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)
|
||||
if_blk := ifexpr.c_if([], [.i64_t])
|
||||
{
|
||||
ifexpr.i64_const(5000)
|
||||
}
|
||||
ifexpr.c_else(if_blk)
|
||||
{
|
||||
ifexpr.i64_const(-5000)
|
||||
}
|
||||
ifexpr.c_end(if_blk)
|
||||
}
|
||||
m.commit(ifexpr, true)
|
||||
print(m.compile().bytestr())
|
||||
}
|
||||
@@ -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)
|
||||
bif := fac.c_if([], [.i64_t])
|
||||
{
|
||||
fac.i64_const(1)
|
||||
}
|
||||
fac.c_else(bif)
|
||||
{
|
||||
{
|
||||
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(bif)
|
||||
}
|
||||
m.commit(fac, true)
|
||||
print(m.compile().bytestr())
|
||||
|
||||
// v run factorial.v > a.wasm
|
||||
// wasmer a.wasm -i fac 5
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import wasm
|
||||
|
||||
fn main() {
|
||||
mut m := wasm.Module{}
|
||||
m.enable_debug('vlang')
|
||||
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.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,71 @@
|
||||
import os
|
||||
import wasm
|
||||
|
||||
// Install Wasmer:
|
||||
// $ curl https://get.wasmer.io -sSfL | sh
|
||||
// $ wasmer --version
|
||||
//
|
||||
// Create `examples/wasm_codegen/math.wasm`:
|
||||
// $ v run examples/wasm_codegen/main.v
|
||||
//
|
||||
// Call entrypoints with the current Wasmer CLI:
|
||||
// $ wasmer run examples/wasm_codegen/math.wasm --invoke add 3 5
|
||||
// 8
|
||||
// $ wasmer run examples/wasm_codegen/math.wasm --invoke factorial 10
|
||||
// 3628800
|
||||
// $ wasmer run examples/wasm_codegen/math.wasm --invoke pythagoras 30 40
|
||||
// 50
|
||||
fn main() {
|
||||
mut math := Math{}
|
||||
math.function_add('add')
|
||||
math.function_factorial('factorial')
|
||||
math.function_pythagoras('pythagoras')
|
||||
math.save(os.join_path(@DIR, 'math.wasm'))!
|
||||
}
|
||||
|
||||
struct Math {
|
||||
wasm.Module
|
||||
}
|
||||
|
||||
fn (mut math Math) function_add(name string) {
|
||||
mut func := math.new_function(name, [.i32_t, .i32_t], [.i32_t])
|
||||
func.local_get(0)
|
||||
func.local_get(1)
|
||||
func.add(.i32_t)
|
||||
math.commit(func, true)
|
||||
}
|
||||
|
||||
fn (mut math Math) function_factorial(name string) {
|
||||
mut func := math.new_function(name, [.i64_t], [.i64_t])
|
||||
func.local_get(0)
|
||||
func.eqz(.i64_t)
|
||||
block := func.c_if([], [.i64_t])
|
||||
func.i64_const(1)
|
||||
func.c_else(block)
|
||||
func.local_get(0)
|
||||
func.local_get(0)
|
||||
func.i64_const(1)
|
||||
func.sub(.i64_t)
|
||||
func.call(name)
|
||||
func.mul(.i64_t)
|
||||
func.c_end(block)
|
||||
math.commit(func, true)
|
||||
}
|
||||
|
||||
fn (mut math Math) function_pythagoras(name string) {
|
||||
mut func := math.new_function(name, [.f32_t, .f32_t], [.f64_t])
|
||||
func.local_get(0)
|
||||
func.local_get(0)
|
||||
func.mul(.f32_t)
|
||||
func.local_get(1)
|
||||
func.local_get(1)
|
||||
func.mul(.f32_t)
|
||||
func.add(.f32_t)
|
||||
func.sqrt(.f32_t)
|
||||
func.cast(.f32_t, true, .f64_t)
|
||||
math.commit(func, true)
|
||||
}
|
||||
|
||||
fn (mut math Math) save(path string) ! {
|
||||
os.write_file_array(path, math.compile())!
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
Reference in new issue
Block a user