diff --git a/zjit/src/lib.rs b/zjit/src/lib.rs index 655a85aca6..c93145cb7d 100644 --- a/zjit/src/lib.rs +++ b/zjit/src/lib.rs @@ -1,5 +1,3 @@ -mod zjit; - extern "C" fn zjit_init() { println!("zjit_init"); } @@ -8,3 +6,109 @@ extern "C" fn zjit_init() { pub extern "C" fn rb_zjit_parse_option() -> bool { false } + +#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] +pub struct InsnId(usize); +#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] +pub struct BlockId(usize); + +// TODO: replace with VALUE +#[derive(Debug)] +enum RubyValue { + Nil, +} + +#[derive(Debug)] +enum Insn { + Param { idx: usize }, + Const { val: RubyValue }, + Return { val: InsnId }, +} + +#[derive(Debug)] +struct Block { + params: Vec, + insns: Vec, +} + +impl Block { + fn new() -> Block { + Block { params: vec![], insns: vec![] } + } +} + +#[derive(Debug)] +struct Function { + entry_block: BlockId, + insns: Vec, + blocks: Vec, +} + +impl Function { + fn new() -> Function { + Function { blocks: vec![Block::new()], insns: vec![], entry_block: BlockId(0) } + } + + fn push_insn(&mut self, block: BlockId, insn: Insn) -> InsnId { + let id = InsnId(self.insns.len()); + self.insns.push(insn); + // Add the insn to the block + self.blocks[block.0].insns.push(id); + id + } +} + +enum RubyOpcode { + Putnil, + Leave, +} + +struct FrameState { + stack: Vec, +} + +impl FrameState { + fn new() -> FrameState { + FrameState { stack: vec![] } + } + + fn push(&mut self, val: InsnId) { + self.stack.push(val); + } + + fn pop(&mut self) -> InsnId { + self.stack.pop().expect("Bytecode stack mismatch") + } +} + +fn to_ssa(opcodes: &Vec) -> Function { + let mut result = Function::new(); + let mut state = FrameState::new(); + let block = result.entry_block; + for opcode in opcodes { + match opcode { + RubyOpcode::Putnil => { + state.push(result.push_insn(block, Insn::Const { val: RubyValue::Nil })); + }, + RubyOpcode::Leave => { + result.push_insn(block, Insn::Return { val: state.pop() }); + }, + } + } + result +} + +#[cfg(test)] +mod tests { + use crate::*; + + #[test] + fn test() { + let opcodes = vec![ + RubyOpcode::Putnil, + RubyOpcode::Leave, + ]; + let function = to_ssa(&opcodes); + println!("zjit {function:?}"); + } +} diff --git a/zjit/src/main.rs b/zjit/src/main.rs deleted file mode 100644 index 89774abe85..0000000000 --- a/zjit/src/main.rs +++ /dev/null @@ -1,26 +0,0 @@ -mod cruby; - -#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] -pub struct InsnId(usize); -#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] -pub struct BlockId(usize); - -enum Insn { - Param { idx: usize }, -} - -#[derive(Debug)] -struct Block { - params: Vec, - insns: Vec, -} - -#[derive(Debug)] -struct Function { - name: String, - entry_block: BlockId, -} - -fn main() { - println!("zjit"); -}