2022-07-11 21:03:05 -04:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
use crate::NativeType;
|
|
|
|
use crate::{tcc::Compiler, Symbol};
|
|
|
|
use std::ffi::c_void;
|
|
|
|
use std::ffi::CString;
|
|
|
|
use std::fmt::Write as _;
|
2022-07-24 06:41:11 -04:00
|
|
|
use std::mem::size_of;
|
|
|
|
|
|
|
|
const _: () = assert!(size_of::<fn()>() == size_of::<usize>());
|
2022-07-11 21:03:05 -04:00
|
|
|
|
|
|
|
pub(crate) struct Allocation {
|
|
|
|
pub addr: *mut c_void,
|
|
|
|
_ctx: Compiler,
|
|
|
|
_sym: Box<Symbol>,
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! cstr {
|
|
|
|
($st:expr) => {
|
|
|
|
&CString::new($st).unwrap()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn native_arg_to_c(ty: &NativeType) -> &'static str {
|
|
|
|
match ty {
|
2022-09-04 23:26:52 -04:00
|
|
|
NativeType::Bool => "bool",
|
2022-07-11 21:03:05 -04:00
|
|
|
NativeType::U8 | NativeType::U16 | NativeType::U32 => "uint32_t",
|
|
|
|
NativeType::I8 | NativeType::I16 | NativeType::I32 => "int32_t",
|
|
|
|
NativeType::Void => "void",
|
|
|
|
NativeType::F32 => "float",
|
|
|
|
NativeType::F64 => "double",
|
2022-07-24 06:41:11 -04:00
|
|
|
NativeType::U64 => "uint64_t",
|
|
|
|
NativeType::I64 => "int64_t",
|
|
|
|
NativeType::ISize => "intptr_t",
|
|
|
|
NativeType::USize => "uintptr_t",
|
2022-08-22 23:46:43 -04:00
|
|
|
NativeType::Buffer => "struct FastApiTypedArray*",
|
|
|
|
NativeType::Function | NativeType::Pointer => "void*",
|
2022-07-11 21:03:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn native_to_c(ty: &NativeType) -> &'static str {
|
|
|
|
match ty {
|
2022-09-04 23:26:52 -04:00
|
|
|
NativeType::Bool => "bool",
|
2022-07-11 21:03:05 -04:00
|
|
|
NativeType::U8 => "uint8_t",
|
|
|
|
NativeType::U16 => "uint16_t",
|
|
|
|
NativeType::U32 => "uint32_t",
|
|
|
|
NativeType::I8 => "int8_t",
|
|
|
|
NativeType::I16 => "uint16_t",
|
|
|
|
NativeType::I32 => "int32_t",
|
|
|
|
NativeType::Void => "void",
|
|
|
|
NativeType::F32 => "float",
|
|
|
|
NativeType::F64 => "double",
|
2022-07-24 06:41:11 -04:00
|
|
|
NativeType::U64 => "uint64_t",
|
|
|
|
NativeType::I64 => "int64_t",
|
|
|
|
NativeType::ISize => "intptr_t",
|
|
|
|
NativeType::USize => "uintptr_t",
|
2022-08-22 23:46:43 -04:00
|
|
|
NativeType::Pointer | NativeType::Buffer | NativeType::Function => "void*",
|
2022-07-11 21:03:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn codegen(sym: &crate::Symbol) -> String {
|
2022-07-24 09:08:13 -04:00
|
|
|
let mut c = String::from(include_str!("prelude.h"));
|
2022-07-28 08:38:22 -04:00
|
|
|
let needs_unwrap = crate::needs_unwrap(sym.result_type);
|
|
|
|
|
|
|
|
// Return type of the FFI call.
|
|
|
|
let ffi_ret = native_to_c(&sym.result_type);
|
|
|
|
// Return type of the trampoline.
|
|
|
|
let ret = if needs_unwrap { "void" } else { ffi_ret };
|
2022-07-11 21:03:05 -04:00
|
|
|
|
|
|
|
// extern <return_type> func(
|
2022-07-28 08:38:22 -04:00
|
|
|
let _ = write!(c, "\nextern {ffi_ret} func(");
|
2022-07-11 21:03:05 -04:00
|
|
|
// <param_type> p0, <param_type> p1, ...);
|
|
|
|
for (i, ty) in sym.parameter_types.iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
c += ", ";
|
|
|
|
}
|
|
|
|
c += native_to_c(ty);
|
|
|
|
let _ = write!(c, " p{i}");
|
|
|
|
}
|
|
|
|
c += ");\n\n";
|
|
|
|
|
|
|
|
// void* recv, <param_type> p0, <param_type> p1, ...);
|
|
|
|
c += ret;
|
|
|
|
c += " func_trampoline(";
|
|
|
|
c += "void* recv";
|
|
|
|
for (i, ty) in sym.parameter_types.iter().enumerate() {
|
|
|
|
c += ", ";
|
|
|
|
c += native_arg_to_c(ty);
|
|
|
|
let _ = write!(c, " p{i}");
|
|
|
|
}
|
2022-07-28 08:38:22 -04:00
|
|
|
if needs_unwrap {
|
|
|
|
let _ = write!(c, ", struct FastApiTypedArray* const p_ret");
|
|
|
|
}
|
2022-07-11 21:03:05 -04:00
|
|
|
c += ") {\n";
|
2022-07-28 08:38:22 -04:00
|
|
|
// func(p0, p1, ...);
|
|
|
|
let mut call_s = String::from("func(");
|
|
|
|
{
|
|
|
|
for (i, ty) in sym.parameter_types.iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
call_s += ", ";
|
|
|
|
}
|
2022-08-22 23:46:43 -04:00
|
|
|
if matches!(ty, NativeType::Buffer) {
|
2022-07-28 08:38:22 -04:00
|
|
|
let _ = write!(call_s, "p{i}->data");
|
|
|
|
} else {
|
|
|
|
let _ = write!(call_s, "p{i}");
|
|
|
|
}
|
2022-07-27 10:02:21 -04:00
|
|
|
}
|
2022-07-28 08:38:22 -04:00
|
|
|
call_s += ");\n";
|
2022-07-11 21:03:05 -04:00
|
|
|
}
|
2022-07-28 08:38:22 -04:00
|
|
|
if needs_unwrap {
|
|
|
|
// <return_type> r = func(p0, p1, ...);
|
|
|
|
// ((<return_type>*)p_ret->data)[0] = r;
|
|
|
|
let _ = write!(c, " {ffi_ret} r = {call_s}");
|
|
|
|
let _ = writeln!(c, " (({ffi_ret}*)p_ret->data)[0] = r;");
|
|
|
|
} else {
|
|
|
|
// return func(p0, p1, ...);
|
|
|
|
let _ = write!(c, " return {call_s}");
|
|
|
|
}
|
|
|
|
c += "}\n\n";
|
2022-07-11 21:03:05 -04:00
|
|
|
c
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn gen_trampoline(
|
|
|
|
sym: Box<crate::Symbol>,
|
|
|
|
) -> Result<Box<Allocation>, ()> {
|
|
|
|
let mut ctx = Compiler::new()?;
|
|
|
|
ctx.set_options(cstr!("-nostdlib"));
|
|
|
|
// SAFETY: symbol satisfies ABI requirement.
|
|
|
|
unsafe { ctx.add_symbol(cstr!("func"), sym.ptr.0 as *const c_void) };
|
|
|
|
let c = codegen(&sym);
|
|
|
|
ctx.compile_string(cstr!(c))?;
|
|
|
|
let alloc = Allocation {
|
|
|
|
addr: ctx.relocate_and_get_symbol(cstr!("func_trampoline"))?,
|
|
|
|
_ctx: ctx,
|
|
|
|
_sym: sym,
|
|
|
|
};
|
|
|
|
Ok(Box::new(alloc))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use libffi::middle::Type;
|
|
|
|
use std::ptr::null_mut;
|
|
|
|
|
|
|
|
fn codegen(parameters: Vec<NativeType>, ret: NativeType) -> String {
|
|
|
|
let sym = Box::new(crate::Symbol {
|
|
|
|
cif: libffi::middle::Cif::new(vec![], Type::void()),
|
|
|
|
ptr: libffi::middle::CodePtr(null_mut()),
|
|
|
|
parameter_types: parameters,
|
|
|
|
result_type: ret,
|
|
|
|
can_callback: false,
|
|
|
|
});
|
|
|
|
super::codegen(&sym)
|
|
|
|
}
|
|
|
|
|
2022-07-24 09:08:13 -04:00
|
|
|
const PRELUDE: &str = include_str!("prelude.h");
|
|
|
|
fn assert_codegen(expected: String, actual: &str) {
|
|
|
|
assert_eq!(expected, format!("{PRELUDE}\n{}", actual))
|
|
|
|
}
|
|
|
|
|
2022-07-11 21:03:05 -04:00
|
|
|
#[test]
|
|
|
|
fn test_gen_trampoline() {
|
2022-07-24 09:08:13 -04:00
|
|
|
assert_codegen(
|
2022-07-11 21:03:05 -04:00
|
|
|
codegen(vec![], NativeType::Void),
|
2022-07-24 09:08:13 -04:00
|
|
|
"extern void func();\n\n\
|
2022-07-12 06:44:54 -04:00
|
|
|
void func_trampoline(void* recv) {\
|
|
|
|
\n return func();\n\
|
2022-07-24 09:08:13 -04:00
|
|
|
}\n\n",
|
2022-07-11 21:03:05 -04:00
|
|
|
);
|
2022-07-24 09:08:13 -04:00
|
|
|
assert_codegen(
|
2022-07-11 21:03:05 -04:00
|
|
|
codegen(vec![NativeType::U32, NativeType::U32], NativeType::U32),
|
2022-07-24 09:08:13 -04:00
|
|
|
"extern uint32_t func(uint32_t p0, uint32_t p1);\n\n\
|
2022-07-12 06:44:54 -04:00
|
|
|
uint32_t func_trampoline(void* recv, uint32_t p0, uint32_t p1) {\
|
|
|
|
\n return func(p0, p1);\n\
|
2022-07-24 09:08:13 -04:00
|
|
|
}\n\n",
|
2022-07-11 21:03:05 -04:00
|
|
|
);
|
2022-07-24 09:08:13 -04:00
|
|
|
assert_codegen(
|
2022-07-11 21:03:05 -04:00
|
|
|
codegen(vec![NativeType::I32, NativeType::I32], NativeType::I32),
|
2022-07-24 09:08:13 -04:00
|
|
|
"extern int32_t func(int32_t p0, int32_t p1);\n\n\
|
2022-07-12 06:44:54 -04:00
|
|
|
int32_t func_trampoline(void* recv, int32_t p0, int32_t p1) {\
|
|
|
|
\n return func(p0, p1);\n\
|
2022-07-24 09:08:13 -04:00
|
|
|
}\n\n",
|
2022-07-11 21:03:05 -04:00
|
|
|
);
|
2022-07-24 09:08:13 -04:00
|
|
|
assert_codegen(
|
2022-07-11 21:03:05 -04:00
|
|
|
codegen(vec![NativeType::F32, NativeType::F32], NativeType::F32),
|
2022-07-24 09:08:13 -04:00
|
|
|
"extern float func(float p0, float p1);\n\n\
|
2022-07-12 06:44:54 -04:00
|
|
|
float func_trampoline(void* recv, float p0, float p1) {\
|
|
|
|
\n return func(p0, p1);\n\
|
2022-07-24 09:08:13 -04:00
|
|
|
}\n\n",
|
2022-07-11 21:03:05 -04:00
|
|
|
);
|
2022-07-24 09:08:13 -04:00
|
|
|
assert_codegen(
|
2022-07-11 21:03:05 -04:00
|
|
|
codegen(vec![NativeType::F64, NativeType::F64], NativeType::F64),
|
2022-07-24 09:08:13 -04:00
|
|
|
"extern double func(double p0, double p1);\n\n\
|
2022-07-12 06:44:54 -04:00
|
|
|
double func_trampoline(void* recv, double p0, double p1) {\
|
|
|
|
\n return func(p0, p1);\n\
|
2022-07-24 09:08:13 -04:00
|
|
|
}\n\n",
|
2022-07-11 21:03:05 -04:00
|
|
|
);
|
2022-07-27 10:02:21 -04:00
|
|
|
assert_codegen(
|
2022-08-22 23:46:43 -04:00
|
|
|
codegen(vec![NativeType::Buffer, NativeType::U32], NativeType::U32),
|
2022-07-27 10:02:21 -04:00
|
|
|
"extern uint32_t func(void* p0, uint32_t p1);\n\n\
|
|
|
|
uint32_t func_trampoline(void* recv, struct FastApiTypedArray* p0, uint32_t p1) {\
|
|
|
|
\n return func(p0->data, p1);\n\
|
|
|
|
}\n\n",
|
|
|
|
);
|
|
|
|
assert_codegen(
|
2022-08-22 23:46:43 -04:00
|
|
|
codegen(vec![NativeType::Buffer, NativeType::Buffer], NativeType::U32),
|
2022-07-27 10:02:21 -04:00
|
|
|
"extern uint32_t func(void* p0, void* p1);\n\n\
|
|
|
|
uint32_t func_trampoline(void* recv, struct FastApiTypedArray* p0, struct FastApiTypedArray* p1) {\
|
|
|
|
\n return func(p0->data, p1->data);\n\
|
|
|
|
}\n\n",
|
|
|
|
);
|
2022-07-28 08:38:22 -04:00
|
|
|
assert_codegen(
|
|
|
|
codegen(vec![], NativeType::U64),
|
|
|
|
"extern uint64_t func();\n\n\
|
|
|
|
void func_trampoline(void* recv, struct FastApiTypedArray* const p_ret) {\
|
|
|
|
\n uint64_t r = func();\
|
|
|
|
\n ((uint64_t*)p_ret->data)[0] = r;\n\
|
|
|
|
}\n\n",
|
|
|
|
);
|
|
|
|
assert_codegen(
|
2022-08-22 23:46:43 -04:00
|
|
|
codegen(vec![NativeType::Buffer, NativeType::Buffer], NativeType::U64),
|
2022-07-28 08:38:22 -04:00
|
|
|
"extern uint64_t func(void* p0, void* p1);\n\n\
|
|
|
|
void func_trampoline(void* recv, struct FastApiTypedArray* p0, struct FastApiTypedArray* p1, struct FastApiTypedArray* const p_ret) {\
|
|
|
|
\n uint64_t r = func(p0->data, p1->data);\
|
|
|
|
\n ((uint64_t*)p_ret->data)[0] = r;\n\
|
|
|
|
}\n\n",
|
|
|
|
);
|
2022-08-22 23:46:43 -04:00
|
|
|
assert_codegen(
|
|
|
|
codegen(vec![NativeType::Pointer, NativeType::Pointer], NativeType::U64),
|
|
|
|
"extern uint64_t func(void* p0, void* p1);\n\n\
|
|
|
|
void func_trampoline(void* recv, void* p0, void* p1, struct FastApiTypedArray* const p_ret) {\
|
|
|
|
\n uint64_t r = func(p0, p1);\
|
|
|
|
\n ((uint64_t*)p_ret->data)[0] = r;\n\
|
|
|
|
}\n\n",
|
|
|
|
);
|
2022-07-11 21:03:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_gen_trampoline_implicit_cast() {
|
2022-07-24 09:08:13 -04:00
|
|
|
assert_codegen(
|
2022-07-11 21:03:05 -04:00
|
|
|
codegen(vec![NativeType::I8, NativeType::U8], NativeType::I8),
|
2022-07-24 09:08:13 -04:00
|
|
|
"extern int8_t func(int8_t p0, uint8_t p1);\n\n\
|
2022-07-12 06:44:54 -04:00
|
|
|
int8_t func_trampoline(void* recv, int32_t p0, uint32_t p1) {\
|
|
|
|
\n return func(p0, p1);\n\
|
2022-07-24 09:08:13 -04:00
|
|
|
}\n\n",
|
2022-07-11 22:50:20 -04:00
|
|
|
);
|
2022-07-24 09:08:13 -04:00
|
|
|
assert_codegen(
|
2022-07-11 22:50:20 -04:00
|
|
|
codegen(vec![NativeType::ISize, NativeType::U64], NativeType::Void),
|
2022-07-24 09:08:13 -04:00
|
|
|
"extern void func(intptr_t p0, uint64_t p1);\n\n\
|
2022-07-24 06:41:11 -04:00
|
|
|
void func_trampoline(void* recv, intptr_t p0, uint64_t p1) {\
|
2022-07-12 06:44:54 -04:00
|
|
|
\n return func(p0, p1);\n\
|
2022-07-24 09:08:13 -04:00
|
|
|
}\n\n",
|
2022-07-11 22:50:20 -04:00
|
|
|
);
|
2022-07-24 09:08:13 -04:00
|
|
|
assert_codegen(
|
2022-07-11 22:50:20 -04:00
|
|
|
codegen(vec![NativeType::USize, NativeType::USize], NativeType::U32),
|
2022-07-24 09:08:13 -04:00
|
|
|
"extern uint32_t func(uintptr_t p0, uintptr_t p1);\n\n\
|
2022-07-24 06:41:11 -04:00
|
|
|
uint32_t func_trampoline(void* recv, uintptr_t p0, uintptr_t p1) {\
|
2022-07-12 06:44:54 -04:00
|
|
|
\n return func(p0, p1);\n\
|
2022-07-24 09:08:13 -04:00
|
|
|
}\n\n",
|
2022-07-11 22:50:20 -04:00
|
|
|
);
|
2022-07-11 21:03:05 -04:00
|
|
|
}
|
|
|
|
}
|