-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add experimental function wrapper macro for linking
- Loading branch information
Showing
6 changed files
with
84 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
//! Public macros | ||
#[macro_export] | ||
macro_rules! make_func_wrapper { | ||
// ptype is an ident because we still want to match on it later -- \/ rtype too -- \/ | ||
( $wrapper_name:ident: $original:ident( $( $pname:ident: $ptype:ident ),* $( , )? ) $( -> $rtype:ident )?) => { | ||
unsafe extern "C" fn $wrapper_name( | ||
_rt: ffi::IM3Runtime, | ||
_sp: *mut u64, | ||
_mem: *mut std::ffi::c_void, | ||
) -> *const std::ffi::c_void { | ||
let ssp = _sp; | ||
$( | ||
let $pname = $crate::read_stack_param!(_sp -> $ptype); | ||
let _sp = _sp.add(1); | ||
)* | ||
let ret = $original( $( $pname ),* ); | ||
$( | ||
$crate::put_stack_return!(ssp <- ret as $rtype); | ||
)? | ||
ffi::m3Err_none as _ | ||
} | ||
}; | ||
} | ||
|
||
#[doc(hidden)] | ||
#[macro_export] | ||
macro_rules! read_stack_param { | ||
($sp:ident -> u64) => { | ||
*$sp | ||
}; | ||
($sp:ident -> u32) => { | ||
(*$sp & 0xFFFF_FFFF) as u32; | ||
}; | ||
($sp:ident -> f64) => { | ||
f64::from_ne_bytes((*$sp).to_ne_bytes()) | ||
}; | ||
($sp:ident -> f32) => { | ||
f64::from_ne_bytes($crate::read_stack_param!($sp -> u32).to_ne_bytes()) | ||
}; | ||
($sp:ident -> $type:ty) => { | ||
compile_error!(concat!("unknown function argument type ", stringify!($type))) | ||
}; | ||
} | ||
|
||
#[doc(hidden)] | ||
#[macro_export] | ||
macro_rules! put_stack_return { | ||
($sp:ident <- $ident:ident as u64) => { | ||
*$sp = $ident; | ||
}; | ||
($sp:ident -> u32) => { | ||
*$sp = $ident as u64; | ||
}; | ||
($sp:ident -> f64) => { | ||
*$sp = u64::from_ne_bytes($ident.to_ne_bytes()); | ||
}; | ||
($sp:ident -> f32) => { | ||
f64::from_ne_bytes($crate::read_stack_param!($sp -> u32).to_ne_bytes()) | ||
*$sp = u32::from_ne_bytes($ident.to_ne_bytes()) as u64; | ||
}; | ||
($sp:ident -> ()) => {}; | ||
($sp:ident -> $type:ty) => { | ||
compile_error!(concat!("unknown function argument type ", stringify!($type))) | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters