Add simple SSA
This commit is contained in:
parent
344ee211d6
commit
1388f92919
Notes:
git
2025-04-18 13:49:57 +00:00
108
zjit/src/lib.rs
108
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<InsnId>,
|
||||
insns: Vec<InsnId>,
|
||||
}
|
||||
|
||||
impl Block {
|
||||
fn new() -> Block {
|
||||
Block { params: vec![], insns: vec![] }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Function {
|
||||
entry_block: BlockId,
|
||||
insns: Vec<Insn>,
|
||||
blocks: Vec<Block>,
|
||||
}
|
||||
|
||||
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<InsnId>,
|
||||
}
|
||||
|
||||
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<RubyOpcode>) -> 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:?}");
|
||||
}
|
||||
}
|
||||
|
@ -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<InsnId>,
|
||||
insns: Vec<InsnId>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Function {
|
||||
name: String,
|
||||
entry_block: BlockId,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("zjit");
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user