mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
wasm: add a webassembly compiler backend, based on using binaryen (#17368)
This commit is contained in:
81
vlib/builtin/wasm/alloc.v
Normal file
81
vlib/builtin/wasm/alloc.v
Normal file
@@ -0,0 +1,81 @@
|
||||
[has_globals]
|
||||
module builtin
|
||||
|
||||
// Shitty `sbrk` basic `malloc` and `free` impl
|
||||
// TODO: implement pure V `walloc` later
|
||||
|
||||
const wasm_page_size = 64 * 1024
|
||||
|
||||
__global g_heap_base = isize(0)
|
||||
|
||||
fn init() {
|
||||
g_heap_base = __memory_grow(3)
|
||||
if g_heap_base == -1 {
|
||||
panic('g_heap_base: malloc() == nil')
|
||||
}
|
||||
g_heap_base *= wasm_page_size
|
||||
}
|
||||
|
||||
// malloc dynamically allocates a `n` bytes block of memory on the heap.
|
||||
// malloc returns a `byteptr` pointing to the memory address of the allocated space.
|
||||
// unlike the `calloc` family of functions - malloc will not zero the memory block.
|
||||
[unsafe]
|
||||
pub fn malloc(n isize) &u8 {
|
||||
if n <= 0 {
|
||||
panic('malloc(n <= 0)')
|
||||
}
|
||||
|
||||
res := g_heap_base
|
||||
g_heap_base += n
|
||||
|
||||
return &u8(res)
|
||||
}
|
||||
|
||||
// free allows for manually freeing memory allocated at the address `ptr`.
|
||||
// currently does not free any memory.
|
||||
[unsafe]
|
||||
pub fn free(ptr voidptr) {
|
||||
_ := ptr
|
||||
}
|
||||
|
||||
// vcalloc dynamically allocates a zeroed `n` bytes block of memory on the heap.
|
||||
// vcalloc returns a `byteptr` pointing to the memory address of the allocated space.
|
||||
// Unlike `v_calloc` vcalloc checks for negative values given in `n`.
|
||||
[unsafe]
|
||||
pub fn vcalloc(n isize) &u8 {
|
||||
if n <= 0 {
|
||||
panic('vcalloc(n <= 0)')
|
||||
} else if n == 0 {
|
||||
return &u8(0)
|
||||
}
|
||||
|
||||
res := unsafe { malloc(n) }
|
||||
|
||||
__memory_fill(res, 0, n)
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
// vmemcpy copies n bytes from memory area src to memory area dest.
|
||||
// The memory areas **CAN** overlap. vmemcpy returns a pointer to `dest`.
|
||||
[unsafe]
|
||||
pub fn vmemcpy(dest voidptr, const_src voidptr, n isize) voidptr {
|
||||
__memory_copy(dest, const_src, n)
|
||||
return dest
|
||||
}
|
||||
|
||||
// vmemmove copies n bytes from memory area src to memory area dest.
|
||||
// The memory areas **CAN** overlap. vmemmove returns a pointer to `dest`.
|
||||
[unsafe]
|
||||
pub fn vmemmove(dest voidptr, const_src voidptr, n isize) voidptr {
|
||||
__memory_copy(dest, const_src, n)
|
||||
return dest
|
||||
}
|
||||
|
||||
// vmemset fills the first `n` bytes of the memory area pointed to by `s`,
|
||||
// with the constant byte `c`. It returns a pointer to the memory area `s`.
|
||||
[unsafe]
|
||||
pub fn vmemset(s voidptr, c int, n isize) voidptr {
|
||||
__memory_fill(s, c, n)
|
||||
return s
|
||||
}
|
||||
16
vlib/builtin/wasm/browser/builtin.v
Normal file
16
vlib/builtin/wasm/browser/builtin.v
Normal file
@@ -0,0 +1,16 @@
|
||||
module builtin
|
||||
|
||||
fn JS.__panic_abort(&u8, int)
|
||||
fn JS.__writeln(&u8, int)
|
||||
|
||||
// panic calls the `__panic_abort` JS panic handler.
|
||||
[noreturn]
|
||||
pub fn panic(s string) {
|
||||
JS.__panic_abort(s.str, s.len)
|
||||
for {}
|
||||
}
|
||||
|
||||
// println prints a message with a line end, to stdout. stdout is flushed.
|
||||
pub fn println(s string) {
|
||||
JS.__writeln(s.str, s.len)
|
||||
}
|
||||
5
vlib/builtin/wasm/builtin.v
Normal file
5
vlib/builtin/wasm/builtin.v
Normal file
@@ -0,0 +1,5 @@
|
||||
module builtin
|
||||
|
||||
fn __memory_grow(size isize) isize
|
||||
fn __memory_fill(dest &u8, value isize, size isize)
|
||||
fn __memory_copy(dest &u8, src &u8, size isize)
|
||||
7
vlib/builtin/wasm/string.v
Normal file
7
vlib/builtin/wasm/string.v
Normal file
@@ -0,0 +1,7 @@
|
||||
module builtin
|
||||
|
||||
pub struct string {
|
||||
pub:
|
||||
str &u8
|
||||
len int
|
||||
}
|
||||
61
vlib/builtin/wasm/wasi/builtin.v
Normal file
61
vlib/builtin/wasm/wasi/builtin.v
Normal file
@@ -0,0 +1,61 @@
|
||||
module builtin
|
||||
|
||||
// print prints a message to stdout. Unlike `println` stdout is not automatically flushed.
|
||||
pub fn print(s string) {
|
||||
elm := CIOVec{
|
||||
buf: s.str
|
||||
len: usize(s.len)
|
||||
}
|
||||
|
||||
WASM.fd_write(1, &elm, 1, -1)
|
||||
}
|
||||
|
||||
// println prints a message with a line end, to stdout.
|
||||
pub fn println(s string) {
|
||||
elm := [CIOVec{
|
||||
buf: s.str
|
||||
len: usize(s.len)
|
||||
}, CIOVec{
|
||||
buf: c'\n'
|
||||
len: 1
|
||||
}]!
|
||||
|
||||
WASM.fd_write(1, &elm[0], 2, -1)
|
||||
}
|
||||
|
||||
// eprint prints a message to stderr.
|
||||
pub fn eprint(s string) {
|
||||
elm := CIOVec{
|
||||
buf: s.str
|
||||
len: usize(s.len)
|
||||
}
|
||||
|
||||
WASM.fd_write(2, &elm, 1, -1)
|
||||
}
|
||||
|
||||
// eprintln prints a message with a line end, to stderr.
|
||||
pub fn eprintln(s string) {
|
||||
elm := [CIOVec{
|
||||
buf: s.str
|
||||
len: usize(s.len)
|
||||
}, CIOVec{
|
||||
buf: c'\n'
|
||||
len: 1
|
||||
}]!
|
||||
|
||||
WASM.fd_write(2, &elm[0], 2, -1)
|
||||
}
|
||||
|
||||
// exit terminates execution immediately and returns exit `code` to the shell.
|
||||
[noreturn]
|
||||
pub fn exit(code int) {
|
||||
WASM.proc_exit(code)
|
||||
}
|
||||
|
||||
// panic prints a nice error message, then exits the process with exit code of 1.
|
||||
[noreturn]
|
||||
pub fn panic(s string) {
|
||||
eprint('V panic: ')
|
||||
eprintln(s)
|
||||
exit(1)
|
||||
}
|
||||
232
vlib/builtin/wasm/wasi/int.v
Normal file
232
vlib/builtin/wasm/wasi/int.v
Normal file
@@ -0,0 +1,232 @@
|
||||
module builtin
|
||||
|
||||
type byte = u8
|
||||
type i32 = int
|
||||
|
||||
const (
|
||||
// digit pairs in reverse order
|
||||
digit_pairs = '00102030405060708090011121314151617181910212223242526272829203132333435363738393041424344454647484940515253545556575859506162636465666768696071727374757677787970818283848586878889809192939495969798999'
|
||||
)
|
||||
|
||||
// This implementation is the quickest with gcc -O2
|
||||
// str_l returns the string representation of the integer nn with max chars.
|
||||
[direct_array_access; inline]
|
||||
fn (nn int) str_l(max int) string {
|
||||
unsafe {
|
||||
mut n := i64(nn)
|
||||
mut d := 0
|
||||
if n == 0 {
|
||||
return '0'
|
||||
}
|
||||
|
||||
mut is_neg := false
|
||||
if n < 0 {
|
||||
n = -n
|
||||
is_neg = true
|
||||
}
|
||||
mut index := max
|
||||
mut buf := malloc(max + 1)
|
||||
buf[index] = 0
|
||||
index--
|
||||
|
||||
for n > 0 {
|
||||
n1 := int(n / 100)
|
||||
// calculate the digit_pairs start index
|
||||
d = int(u32(int(n) - (n1 * 100)) << 1)
|
||||
n = n1
|
||||
buf[index] = digit_pairs.str[d]
|
||||
index--
|
||||
d++
|
||||
buf[index] = digit_pairs.str[d]
|
||||
index--
|
||||
}
|
||||
index++
|
||||
// remove head zero
|
||||
if d < 20 {
|
||||
index++
|
||||
}
|
||||
// Prepend - if it's negative
|
||||
if is_neg {
|
||||
index--
|
||||
buf[index] = `-`
|
||||
}
|
||||
diff := max - index
|
||||
vmemmove(buf, voidptr(buf + index), diff + 1)
|
||||
/*
|
||||
// === manual memory move for bare metal ===
|
||||
mut c:= 0
|
||||
for c < diff {
|
||||
buf[c] = buf[c+index]
|
||||
c++
|
||||
}
|
||||
buf[c] = 0
|
||||
*/
|
||||
return tos(buf, diff)
|
||||
|
||||
// return tos(memdup(&buf[0] + index, (max - index)), (max - index))
|
||||
}
|
||||
}
|
||||
|
||||
// str returns the value of the `i8` as a `string`.
|
||||
// Example: assert i8(-2).str() == '-2'
|
||||
pub fn (n i8) str() string {
|
||||
return int(n).str_l(5)
|
||||
}
|
||||
|
||||
// str returns the value of the `i16` as a `string`.
|
||||
// Example: assert i16(-20).str() == '-20'
|
||||
pub fn (n i16) str() string {
|
||||
return int(n).str_l(7)
|
||||
}
|
||||
|
||||
// str returns the value of the `u16` as a `string`.
|
||||
// Example: assert u16(20).str() == '20'
|
||||
pub fn (n u16) str() string {
|
||||
return int(n).str_l(7)
|
||||
}
|
||||
|
||||
// str returns the value of the `int` as a `string`.
|
||||
// Example: assert int(-2020).str() == '-2020'
|
||||
pub fn (n int) str() string {
|
||||
return n.str_l(12)
|
||||
}
|
||||
|
||||
// str returns the value of the `u32` as a `string`.
|
||||
// Example: assert u32(20000).str() == '20000'
|
||||
[direct_array_access; inline]
|
||||
pub fn (nn u32) str() string {
|
||||
unsafe {
|
||||
mut n := nn
|
||||
mut d := u32(0)
|
||||
if n == 0 {
|
||||
return '0'
|
||||
}
|
||||
max := 12
|
||||
mut buf := malloc(max + 1)
|
||||
mut index := max
|
||||
buf[index] = 0
|
||||
index--
|
||||
for n > 0 {
|
||||
n1 := n / u32(100)
|
||||
d = ((n - (n1 * u32(100))) << u32(1))
|
||||
n = n1
|
||||
buf[index] = digit_pairs[d]
|
||||
index--
|
||||
d++
|
||||
buf[index] = digit_pairs[d]
|
||||
index--
|
||||
}
|
||||
index++
|
||||
// remove head zero
|
||||
if d < u32(20) {
|
||||
index++
|
||||
}
|
||||
diff := max - index
|
||||
vmemmove(buf, voidptr(buf + index), diff + 1)
|
||||
return tos(buf, diff)
|
||||
|
||||
// return tos(memdup(&buf[0] + index, (max - index)), (max - index))
|
||||
}
|
||||
}
|
||||
|
||||
// str returns the value of the `int_literal` as a `string`.
|
||||
[inline]
|
||||
pub fn (n int_literal) str() string {
|
||||
return i64(n).str()
|
||||
}
|
||||
|
||||
// str returns the value of the `i64` as a `string`.
|
||||
// Example: assert i64(-200000).str() == '-200000'
|
||||
[direct_array_access; inline]
|
||||
pub fn (nn i64) str() string {
|
||||
unsafe {
|
||||
mut n := nn
|
||||
mut d := i64(0)
|
||||
if n == 0 {
|
||||
return '0'
|
||||
} else if n == i64(-9223372036854775807 - 1) {
|
||||
// math.min_i64
|
||||
return '-9223372036854775808'
|
||||
}
|
||||
max := 20
|
||||
mut buf := malloc(max + 1)
|
||||
mut is_neg := false
|
||||
if n < 0 {
|
||||
n = -n
|
||||
is_neg = true
|
||||
}
|
||||
mut index := max
|
||||
buf[index] = 0
|
||||
index--
|
||||
for n > 0 {
|
||||
n1 := n / i64(100)
|
||||
d = (u32(n - (n1 * i64(100))) << i64(1))
|
||||
n = n1
|
||||
buf[index] = digit_pairs[d]
|
||||
index--
|
||||
d++
|
||||
buf[index] = digit_pairs[d]
|
||||
index--
|
||||
}
|
||||
index++
|
||||
// remove head zero
|
||||
if d < i64(20) {
|
||||
index++
|
||||
}
|
||||
// Prepend - if it's negative
|
||||
if is_neg {
|
||||
index--
|
||||
buf[index] = `-`
|
||||
}
|
||||
diff := max - index
|
||||
vmemmove(buf, voidptr(buf + index), diff + 1)
|
||||
return tos(buf, diff)
|
||||
// return tos(memdup(&buf[0] + index, (max - index)), (max - index))
|
||||
}
|
||||
}
|
||||
|
||||
// str returns the value of the `u64` as a `string`.
|
||||
// Example: assert u64(2000000).str() == '2000000'
|
||||
[direct_array_access; inline]
|
||||
pub fn (nn u64) str() string {
|
||||
unsafe {
|
||||
mut n := nn
|
||||
mut d := u64(0)
|
||||
if n == 0 {
|
||||
return '0'
|
||||
}
|
||||
max := 20
|
||||
mut buf := malloc(max + 1)
|
||||
mut index := max
|
||||
buf[index] = 0
|
||||
index--
|
||||
for n > 0 {
|
||||
n1 := n / 100
|
||||
d = ((n - (n1 * 100)) << 1)
|
||||
n = n1
|
||||
buf[index] = digit_pairs[d]
|
||||
index--
|
||||
d++
|
||||
buf[index] = digit_pairs[d]
|
||||
index--
|
||||
}
|
||||
index++
|
||||
// remove head zero
|
||||
if d < 20 {
|
||||
index++
|
||||
}
|
||||
diff := max - index
|
||||
vmemmove(buf, voidptr(buf + index), diff + 1)
|
||||
return tos(buf, diff)
|
||||
// return tos(memdup(&buf[0] + index, (max - index)), (max - index))
|
||||
}
|
||||
}
|
||||
|
||||
// str returns the value of the `bool` as a `string`.
|
||||
// Example: assert (2 > 1).str() == 'true'
|
||||
pub fn (b bool) str() string {
|
||||
if b {
|
||||
return 'true'
|
||||
}
|
||||
return 'false'
|
||||
}
|
||||
16
vlib/builtin/wasm/wasi/string.v
Normal file
16
vlib/builtin/wasm/wasi/string.v
Normal file
@@ -0,0 +1,16 @@
|
||||
module builtin
|
||||
|
||||
// tos creates a V string, given a C style pointer to a 0 terminated block.
|
||||
// Note: the memory block pointed by s is *reused, not copied*!
|
||||
// It will panic, when the pointer `s` is 0.
|
||||
// See also `tos_clone`.
|
||||
[unsafe]
|
||||
pub fn tos(s &u8, len int) string {
|
||||
if s == 0 {
|
||||
panic('tos(): nil string')
|
||||
}
|
||||
return string{
|
||||
str: unsafe { s }
|
||||
len: len
|
||||
}
|
||||
}
|
||||
14
vlib/builtin/wasm/wasi/wasi.v
Normal file
14
vlib/builtin/wasm/wasi/wasi.v
Normal file
@@ -0,0 +1,14 @@
|
||||
[wasm_import_namespace: 'wasi_snapshot_preview1']
|
||||
module builtin
|
||||
|
||||
struct CIOVec {
|
||||
buf &u8
|
||||
len usize
|
||||
}
|
||||
|
||||
type Errno = u16
|
||||
type FileDesc = int
|
||||
|
||||
fn WASM.fd_write(fd FileDesc, iovs &CIOVec, iovs_len usize, retptr &usize) Errno
|
||||
[noreturn]
|
||||
fn WASM.proc_exit(rval int)
|
||||
Reference in New Issue
Block a user