Skip to content

Commit

Permalink
Cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
losfair authored and syrusakbary committed Jun 3, 2019
1 parent ae19e7f commit 7808c68
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/runtime-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ mod sig_registry;
pub mod structures;
mod sys;
pub mod table;
#[cfg(all(unix, target_arch = "x86_64"))]
pub mod trampoline_x64;
pub mod typed_func;
pub mod types;
pub mod units;
pub mod vm;
#[doc(hidden)]
pub mod vmcalls;
#[cfg(all(unix, target_arch = "x86_64"))]
pub mod trampoline_x64;
#[cfg(all(unix, target_arch = "x86_64"))]
pub use trampoline_x64 as trampoline;

use self::error::CompileResult;
Expand Down
28 changes: 15 additions & 13 deletions lib/runtime-core/src/trampoline_x64.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Trampoline generator for carrying context with function pointer.
//!
//!
//! This makes use of the `mm0` register to pass the context as an implicit "parameter" because `mm0` is
//! not used to pass parameters and is almost never used by modern compilers. It's still better to call
//! `get_context()` as early as possible in the callee function though, as a good practice.
//!
//!
//! Variadic functions are not supported because `rax` is used by the trampoline code.
use crate::loader::CodeMemory;
Expand Down Expand Up @@ -60,7 +60,11 @@ impl TrampolineBufferBuilder {
}
}

pub fn add_function(&mut self, target: *const CallTarget, context: *const CallContext) -> usize {
pub fn add_function(
&mut self,
target: *const CallTarget,
context: *const CallContext,
) -> usize {
let idx = self.offsets.len();
self.offsets.push(self.code.len());
self.code.extend_from_slice(&[
Expand Down Expand Up @@ -108,21 +112,19 @@ mod tests {
value: i32,
}
extern "C" fn do_add(a: i32, b: f32) -> f32 {
let ctx = unsafe {
&*(get_context() as *const TestContext)
};
let ctx = unsafe { &*(get_context() as *const TestContext) };
a as f32 + b + ctx.value as f32
}
let mut builder = TrampolineBufferBuilder::new();
let ctx = TestContext {
value: 3,
};
let idx = builder.add_function(do_add as usize as *const _, &ctx as *const TestContext as *const _);
let ctx = TestContext { value: 3 };
let idx = builder.add_function(
do_add as usize as *const _,
&ctx as *const TestContext as *const _,
);
let buf = builder.build();
let t = buf.get_trampoline(idx);
let ret = unsafe {
::std::mem::transmute::<_, extern "C" fn (i32, f32) -> f32>(t)(1, 2.0) as i32
};
let ret =
unsafe { ::std::mem::transmute::<_, extern "C" fn(i32, f32) -> f32>(t)(1, 2.0) as i32 };
assert_eq!(ret, 6);
}
}

0 comments on commit 7808c68

Please sign in to comment.