mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-27 05:49:08 +00:00
wasm: delete CallConv, since now Zig==C (hopefully)
This commit is contained in:
parent
cc2b8b5d19
commit
4c4344b46c
4 changed files with 44 additions and 99 deletions
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue