summaryrefslogtreecommitdiff
path: root/zjit/src
diff options
context:
space:
mode:
authorTakashi Kokubun <[email protected]>2025-02-06 14:21:13 -0500
committerTakashi Kokubun <[email protected]>2025-04-18 21:52:56 +0900
commitd7dbaf04fab43d75d6e77ee45a77567cfdc831ed (patch)
tree39977ab25b9e975d751d8d186f50527b31b7484b /zjit/src
parent9ddce45c7df7d198b5089042ea8be5717f3eef00 (diff)
Get CodeBlock
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/13131
Diffstat (limited to 'zjit/src')
-rw-r--r--zjit/src/codegen.rs32
-rw-r--r--zjit/src/lib.rs5
2 files changed, 34 insertions, 3 deletions
diff --git a/zjit/src/codegen.rs b/zjit/src/codegen.rs
index 7b9d1bf2d7..ec1385542d 100644
--- a/zjit/src/codegen.rs
+++ b/zjit/src/codegen.rs
@@ -8,19 +8,35 @@ use crate::{utils::IntoUsize};
pub struct CodeBlock {
// Memory for storing the encoded instructions
mem_block: Rc<RefCell<VirtualMem>>,
+
+ // Current writing position
+ write_pos: usize,
}
impl CodeBlock {
/// Make a new CodeBlock
pub fn new(mem_block: Rc<RefCell<VirtualMem>>) -> Self {
- Self { mem_block }
+ Self {
+ mem_block,
+ write_pos: 0,
+ }
+ }
+
+ /// Get a (possibly dangling) direct pointer to the current write position
+ pub fn get_write_ptr(&self) -> CodePtr {
+ self.get_ptr(self.write_pos)
+ }
+
+ /// Get a (possibly dangling) direct pointer into the executable memory block
+ pub fn get_ptr(&self, offset: usize) -> CodePtr {
+ self.mem_block.borrow().start_ptr().add_bytes(offset)
}
}
/// Global state needed for code generation
pub struct ZJITState {
/// Inline code block (fast path)
- inline_cb: CodeBlock,
+ code_block: CodeBlock,
}
/// Private singleton instance of the codegen globals
@@ -65,7 +81,7 @@ impl ZJITState {
#[cfg(not(test))]
let zjit_state = ZJITState {
- inline_cb: cb,
+ code_block: cb,
};
// Initialize the codegen globals instance
@@ -74,4 +90,14 @@ impl ZJITState {
ZJIT_STATE = Some(zjit_state);
}
}
+
+ /// Get a mutable reference to the codegen globals instance
+ fn get_instance() -> &'static mut ZJITState {
+ unsafe { ZJIT_STATE.as_mut().unwrap() }
+ }
+
+ /// Get a mutable reference to the inline code block
+ pub fn get_code_block() -> &'static mut CodeBlock {
+ &mut ZJITState::get_instance().code_block
+ }
}
diff --git a/zjit/src/lib.rs b/zjit/src/lib.rs
index 2a7dc40c75..12de55e9ca 100644
--- a/zjit/src/lib.rs
+++ b/zjit/src/lib.rs
@@ -1,4 +1,5 @@
#![allow(dead_code)]
+#![allow(static_mut_refs)]
mod codegen;
mod cruby;
@@ -76,5 +77,9 @@ pub extern "C" fn rb_zjit_parse_option() -> bool {
#[no_mangle]
pub extern "C" fn rb_zjit_iseq_gen_entry_point(iseq: IseqPtr, _ec: EcPtr) -> *const u8 {
ir::iseq_to_ssa(iseq);
+
+ let cb = ZJITState::get_code_block();
+ let _start_ptr = cb.get_write_ptr();
+
std::ptr::null()
}