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

wasm: remove dependency on thirdparty/binaryen, webassembly backend rewrite (#18120)

This commit is contained in:
l-m
2023-07-12 12:24:38 +00:00
committed by GitHub
parent 1c7df29bed
commit c422919481
40 changed files with 2895 additions and 6166 deletions

View File

@@ -110,26 +110,22 @@ fn (mut mod Module) patch(ft Function) {
mod.buf << ft.code[ptr..]
}
// name
pub fn (mut mod Module) name(name string) {
fn (mut mod Module) name(name string) {
mod.u32(u32(name.len))
mod.buf << name.bytes()
}
// start_subsection
pub fn (mut mod Module) start_subsection(sec Subsection) int {
fn (mut mod Module) start_subsection(sec Subsection) int {
mod.buf << u8(sec)
return mod.patch_start()
}
// start_section
pub fn (mut mod Module) start_section(sec Section) int {
fn (mut mod Module) start_section(sec Section) int {
mod.buf << u8(sec)
return mod.patch_start()
}
// end_section
pub fn (mut mod Module) end_section(tpatch int) {
fn (mut mod Module) end_section(tpatch int) {
mod.patch_len(tpatch)
}
@@ -160,18 +156,14 @@ pub fn (mut mod Module) compile() []u8 {
{
mod.u32(u32(mod.fn_imports.len + mod.global_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.name(ft.mod)
mod.name(ft.name)
mod.buf << 0x00 // function
mod.u32(u32(ft.tidx))
}
for gt in mod.global_imports {
mod.u32(u32(gt.mod.len))
mod.buf << gt.mod.bytes()
mod.u32(u32(gt.name.len))
mod.buf << gt.name.bytes()
mod.name(gt.mod)
mod.name(gt.name)
mod.buf << 0x03 // global
mod.global_type(gt.typ, gt.is_mut)
}
@@ -244,7 +236,7 @@ pub fn (mut mod Module) compile() []u8 {
continue
}
lsz++
mod.name(ft.name)
mod.name(ft.export_name or { ft.name })
mod.buf << 0x00 // function
mod.u32(u32(ft.idx + mod.fn_imports.len))
}

View File

@@ -43,4 +43,11 @@ mut:
locals []FunctionLocal
pub:
name string
pub mut:
export_name ?string
}
// export_name sets the export name of the function to `name`
pub fn (mut func Function) export_name(name string) {
func.export_name = name
}

View File

@@ -861,6 +861,17 @@ pub fn (mut func Function) cast_trapping(a NumType, is_signed bool, b NumType) {
func.cast(a, is_signed, b)
}
// reinterpret returns a value which has the same bit-pattern as its operand value, in its result type.
// WebAssembly instruction: `f32.reinterpret_i32`, `i32.reinterpret_f32`, `f64.reinterpret_i64`, `i64.reinterpret_f64`.
pub fn (mut func Function) reinterpret(a NumType) {
match a {
.f32_t { func.code << 0xBC } // i32.reinterpret_f32
.i32_t { func.code << 0xBE } // f32.reinterpret_i32
.f64_t { func.code << 0xBD } // i64.reinterpret_f64
.i64_t { func.code << 0xBF } // f64.reinterpret_i64
}
}
// unreachable denotes a point in code that should not be reachable, it is an unconditional trap.
// WebAssembly instruction: `unreachable`.
pub fn (mut func Function) unreachable() {
@@ -919,7 +930,7 @@ pub fn (mut func Function) c_return() {
func.code << 0x0F // return
}
// c_end ends the block or loop with the label passed in at `label`.
// c_end ends the block, loop or if expression with the label passed in at `label`.
pub fn (mut func Function) c_end(label LabelIndex) {
assert func.label == label, 'c_end: called with an invalid label ${label}'
func.label--
@@ -945,11 +956,6 @@ pub fn (mut func Function) c_br_if(label LabelIndex) {
func.u32(u32(v))
}
// c_end_if closes the current if expression.
pub fn (mut func Function) c_end_if() {
func.code << 0x0B // END expression opcode
}
// call calls a locally defined function.
// If this function does not exist when calling `compile` on the module, it will panic.
// WebAssembly instruction: `call`.