wasm: delete CallConv, since now Zig==C (hopefully)

This commit is contained in:
Brian Carroll 2022-09-09 18:47:36 +01:00 committed by Brendan Hansknecht
parent cc2b8b5d19
commit 4c4344b46c
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
4 changed files with 44 additions and 99 deletions

View file

@ -105,7 +105,7 @@ impl WasmLayout {
/// The `ValueType`s to use for this layout when calling a Wasm function
/// One Roc argument can become 0, 1, or 2 Wasm arguments
pub fn arg_types(&self, conv: CallConv) -> &'static [ValueType] {
pub fn arg_types(&self) -> &'static [ValueType] {
use ValueType::*;
match self {
@ -116,87 +116,49 @@ impl WasmLayout {
Self::Primitive(F64, _) => &[F64],
// 1 Roc argument => 0-2 Wasm arguments (depending on size and calling convention)
Self::StackMemory { size, format, .. } => conv.stack_memory_arg_types(*size, *format),
Self::StackMemory { size, format, .. } => stack_memory_arg_types(*size, *format),
}
}
pub fn return_method(&self, conv: CallConv) -> ReturnMethod {
pub fn return_method(&self) -> ReturnMethod {
match self {
Self::Primitive(ty, size) => ReturnMethod::Primitive(*ty, *size),
Self::StackMemory { size, format, .. } => {
conv.stack_memory_return_method(*size, *format)
Self::StackMemory { size, format, .. } => stack_memory_return_method(*size, *format),
}
}
}
/// The Wasm argument types to use when passing structs or 128-bit numbers
pub fn stack_memory_arg_types(size: u32, format: StackMemoryFormat) -> &'static [ValueType] {
use StackMemoryFormat::*;
use ValueType::*;
match format {
Int128 | Decimal => &[I64, I64],
DataStructure => {
if size == 0 {
// Zero-size Roc values like `{}` => no Wasm arguments
&[]
} else {
&[I32] // Always pass structs by reference (pointer to stack memory)
}
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum CallConv {
/// The C calling convention, as defined here:
/// https://github.com/WebAssembly/tool-conventions/blob/main/BasicCABI.md
C,
/// The calling convention that Zig 0.9 generates for Wasm when we *ask* it
/// for the .C calling convention, due to bugs in the Zig compiler.
Zig,
}
pub fn stack_memory_return_method(size: u32, format: StackMemoryFormat) -> ReturnMethod {
use ReturnMethod::*;
use StackMemoryFormat::*;
impl CallConv {
/// The Wasm argument types to use when passing structs or 128-bit numbers
pub fn stack_memory_arg_types(
&self,
size: u32,
format: StackMemoryFormat,
) -> &'static [ValueType] {
use StackMemoryFormat::*;
use ValueType::*;
match format {
Int128 | Decimal => WriteToPointerArg,
match format {
Int128 | Decimal => &[I64, I64],
DataStructure => {
if size == 0 {
// Zero-size Roc values like `{}` => no Wasm arguments
return &[];
}
match self {
CallConv::C => {
&[I32] // Always pass structs by reference (pointer to stack memory)
}
CallConv::Zig => {
if size <= 4 {
&[I32] // Small struct: pass by value
} else if size <= 8 {
&[I64] // Small struct: pass by value
} else if size <= 12 {
&[I64, I32] // Medium struct: pass by value, as two Wasm arguments
} else if size <= 16 {
&[I64, I64] // Medium struct: pass by value, as two Wasm arguments
} else {
&[I32] // Large struct: pass by reference
}
}
}
}
}
}
pub fn stack_memory_return_method(&self, size: u32, format: StackMemoryFormat) -> ReturnMethod {
use ReturnMethod::*;
use StackMemoryFormat::*;
match format {
Int128 | Decimal => WriteToPointerArg,
DataStructure => {
if size == 0 {
return NoReturnValue;
}
match self {
CallConv::C => WriteToPointerArg,
CallConv::Zig => WriteToPointerArg,
}
DataStructure => {
if size == 0 {
NoReturnValue
} else {
WriteToPointerArg
}
}
}