Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 4 additions & 76 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 5 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@ keywords = ["jit", "assembler", "codegen"]
[dependencies]
cfg-if = "1.0.0"
tinyvec = { version = "1.6", features = ["alloc"] }
libc = "0.2"
jit-allocator = "0.2.8"
parking_lot = "0.12"
once_cell = "1.17"
num-traits = "0.2"
paste = "1.0"
errno = "0.3"
num = "0.4"
capstone = { version = "0.11", optional = true }
capstone = { version = "0.12", optional = true }

[target.'cfg(target_arch="x86_64")'.dependencies]
iced-x86 = { version = "1.13", features = ["decoder", "no_std", "gas"], default-features = false, optional = true }

[target.'cfg(target_arch="riscv64")'.dependencies]
paste = "1.0"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = [
"memoryapi",
Expand Down
14 changes: 6 additions & 8 deletions src/assembler/assembly_comments.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![allow(dead_code)]
use once_cell::sync::Lazy;
use parking_lot::Mutex;
use std::collections::HashMap;
use std::sync::{Mutex, OnceLock, PoisonError};

pub type CommentMap = HashMap<usize, String>;

Expand All @@ -17,13 +16,12 @@ impl AssemblyCommentsRegistry {
}

pub fn singleton() -> &'static Self {
static INSTANCE: Lazy<AssemblyCommentsRegistry> =
Lazy::new(|| AssemblyCommentsRegistry::new());
&INSTANCE
static INSTANCE: OnceLock<AssemblyCommentsRegistry> = OnceLock::new();
INSTANCE.get_or_init(|| AssemblyCommentsRegistry::new())
}

pub fn comment<'a>(&self, in_code: *const u8) -> Option<String> {
let comments = self.lock.lock();
let comments = self.lock.lock().unwrap_or_else(PoisonError::into_inner);

// search lower bound
/*let (_, (end, comment_map)) = match comments.range(..=(in_code as usize)).next_back() {
Expand Down Expand Up @@ -52,7 +50,7 @@ impl AssemblyCommentsRegistry {
let start = start as usize;
let end = end as usize;

let mut comments = self.lock.lock();
let mut comments = self.lock.lock().unwrap_or_else(PoisonError::into_inner);

/*let (found_end, _) = match comments.remove(&start) {
Some(v) => v,
Expand All @@ -75,7 +73,7 @@ impl AssemblyCommentsRegistry {
let start = start as usize;
let end = end as usize;

let mut comments = self.lock.lock();
let mut comments = self.lock.lock().unwrap_or_else(PoisonError::into_inner);
for (pos, comment) in new_comments.iter() {
println!("{:x}: {}", pos, comment);
}
Expand Down
14 changes: 7 additions & 7 deletions src/assembler/riscv64assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,30 +202,30 @@ macro_rules! decl_immediate {
impl $name {
pub const IMMEDIATE_SIZE: usize = $immediate_size;

pub fn immediate_mask<T: num_traits::WrappingSub + num::PrimInt + num::NumCast>() -> T {
pub fn immediate_mask<T: num_traits::WrappingSub + num_traits::PrimInt + num_traits::NumCast>() -> T {
if $immediate_size < std::mem::size_of::<u32>() * 8 {
T::from(1u64.wrapping_shl($immediate_size as u32)).unwrap().wrapping_sub(&T::one())
} else {
T::max_value()
}
}

pub fn is_valid<T: num::PrimInt + num::NumCast>(imm_value: T) -> bool {
pub fn is_valid<T: num_traits::PrimInt + num_traits::NumCast>(imm_value: T) -> bool {
let shift = std::mem::size_of::<T>() * 8 - $immediate_size;


imm_value == T::from((imm_value.to_i64().unwrap() << shift) >> shift).unwrap()
}

pub fn v32<T: num::NumCast>(imm_value: i32) -> T {
pub fn v32<T: num_traits::NumCast>(imm_value: i32) -> T {

assert!(Self::is_valid(imm_value), "Invalid immediate value: {}", imm_value);
let imm_value = imm_value as u32;
let mask: u32 = Self::immediate_mask::<u32>();
T::from(imm_value & mask).unwrap()
}

pub fn v64<T: num::NumCast>(imm_value: i64) -> T {
pub fn v64<T: num_traits::NumCast>(imm_value: i64) -> T {
assert!(Self::is_valid(imm_value));
let imm_value = imm_value as u64;
let mask: u64 = Self::immediate_mask::<u64>();
Expand All @@ -241,7 +241,7 @@ macro_rules! decl_immediate {
$ctor
}

impl num::ToPrimitive for $name {
impl num_traits::ToPrimitive for $name {
fn to_i64(&self) -> Option<i64> {
Some(self.value as i64)
}
Expand All @@ -259,9 +259,9 @@ macro_rules! decl_immediate {
}
}

impl num::NumCast for $name {
impl num_traits::NumCast for $name {
fn from<T>(n: T) -> Option<Self>
where T: num::ToPrimitive
where T: num_traits::ToPrimitive
{
let n = n.to_u32()?;
if Self::is_valid(n) {
Expand Down
7 changes: 4 additions & 3 deletions src/jit/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
//! Direct port of `JavaScriptCore/jit` module.

use jit_allocator::{JitAllocator, JitAllocatorOptions};
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use std::sync::{Mutex, OnceLock, PoisonError};

pub mod fpr_info;
pub mod gpr_info;
pub mod helpers;

pub static EXECUTABLE_ALLOCATOR: OnceCell<Mutex<Box<JitAllocator>>> = OnceCell::new();
pub static EXECUTABLE_ALLOCATOR: OnceLock<Mutex<Box<JitAllocator>>> = OnceLock::new();
pub fn init_executable_allocator_with(opts: JitAllocatorOptions) {
EXECUTABLE_ALLOCATOR.get_or_init(|| Mutex::new(JitAllocator::new(opts)));
}
Expand All @@ -29,6 +28,7 @@ pub fn allocate_executable_memory(
EXECUTABLE_ALLOCATOR
.get_or_init(|| Mutex::new(JitAllocator::new(JitAllocatorOptions::default())))
.lock()
.unwrap_or_else(PoisonError::into_inner)
.alloc(size)
}

Expand All @@ -45,6 +45,7 @@ pub unsafe fn free_executable_memory(rx: *const u8) -> Result<(), jit_allocator:
.get()
.expect("Executable Allocator must be initialized before freeing")
.lock()
.unwrap_or_else(PoisonError::into_inner)
.release(rx)
}
}