Skip to content

Commit

Permalink
Refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
sisshiki1969 committed Jan 30, 2025
1 parent 8d5c783 commit ba91477
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 145 deletions.
57 changes: 27 additions & 30 deletions monoruby/src/compiler/jitgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,6 @@ mod slot;
pub mod trace_ir;
mod variables;

struct ContinuationInfo {
from: BBContext,
to: BBContext,
pc: BytecodePtr,
}

impl ContinuationInfo {
fn new(from: BBContext, to: BBContext, pc: BytecodePtr) -> Self {
Self { from, to, pc }
}
}

///
/// Compile result of the current instruction.
///
Expand All @@ -61,24 +49,29 @@ enum CompileResult {
Recompile,
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq)]
struct JitLabel(usize);

#[derive(Debug, PartialEq)]
enum BranchMode {
///
/// continuation branch.
/// Continuation branch.
///
/// 'continuation' means the destination is adjacent to the source basic block on the bytecode.
///
Continue,
///
/// side branch. (conditional branch)
/// Side branch. (conditional branch)
///
Side,
/// The machine code for the branch is outlined.
///
/// branch. (unconditional branch)
Side { dest: JitLabel },
///
Branch,
/// Branch. (unconditional branch)
///
/// The machine code for the branch is inlined.
///
Branch { dest: JitLabel },
}

///
Expand All @@ -90,11 +83,9 @@ struct BranchEntry {
src_bb: BasicBlockId,
/// context of the source basic block.
bbctx: BBContext,
/// `DestLabel` for the destination basic block.
branch_dest: JitLabel,
/// true if the branch is a continuation branch.
/// 'continuation' means the destination is adjacent to the source basic block on the bytecode.
cont: BranchMode,
mode: BranchMode,
}

pub(crate) fn conv(reg: SlotId) -> i32 {
Expand Down Expand Up @@ -707,22 +698,28 @@ impl Codegen {
let pair = self.get_address_pair();

// generate machine code for a main context
for ir in std::mem::take(&mut ctx.ir).into_iter() {
self.gen_asm(ir.1, store, &mut ctx, None);
}

// generate machine code for bridges
for (ir, entry, exit) in std::mem::take(&mut ctx.bridges2) {
let entry = ctx.resolve_label(&mut self.jit, entry);
self.gen_asm(ir, store, &mut ctx, Some((entry, exit)));
for (bbid, ir) in std::mem::take(&mut ctx.ir).into_iter() {
self.gen_asm(ir, store, &mut ctx, None);
// generate machine code for bridges
if let Some(bbid) = bbid
&& let Some((ir, exit)) = ctx.continue_bridges.remove(&bbid)
{
self.gen_asm(ir, store, &mut ctx, None);
if let Some(exit) = exit {
let exit = ctx.get_bb_label(exit);
let exit = ctx.resolve_label(&mut self.jit, exit);
monoasm! { &mut self.jit,
jmp exit;
}
}
}
}

// generate machine code for bridges
for (ir, entry, exit) in std::mem::take(&mut ctx.bridges) {
let entry = ctx.resolve_label(&mut self.jit, entry);
self.gen_asm(ir, store, &mut ctx, Some((entry, exit)));
}
assert!(ctx.continuation_bridge.is_none());

self.jit.finalize();

Expand Down
4 changes: 0 additions & 4 deletions monoruby/src/compiler/jitgen/asmir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ impl AsmIr {
self.inst.push(inst);
}

pub(super) fn append(&mut self, ir: &mut AsmIr) {
self.inst.append(&mut ir.inst);
}

pub(super) fn save(&mut self) -> (usize, usize) {
(self.inst.len(), self.side_exit.len())
}
Expand Down
41 changes: 17 additions & 24 deletions monoruby/src/compiler/jitgen/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,12 @@ impl JitContext {
eprintln!(" new_branch_init: {}->{}", BcIndex(0), start_pos);

let bb_begin = func.bb_info.get_bb_id(start_pos);
let branch_dest = self.label();
self.branch_map.insert(
bb_begin,
vec![BranchEntry {
src_bb: BasicBlockId(0),
bbctx,
branch_dest,
cont: BranchMode::Continue,
mode: BranchMode::Continue,
}],
);

Expand Down Expand Up @@ -128,14 +126,12 @@ impl JitContext {
let mut liveness = Liveness::new(self.total_reg_num());

let bbctx = BBContext::new(&ctx);
let branch_dest = ctx.label();
ctx.branch_map.insert(
loop_start,
vec![BranchEntry {
src_bb: BasicBlockId(0),
bbctx,
branch_dest,
cont: BranchMode::Continue,
mode: BranchMode::Continue,
}],
);

Expand Down Expand Up @@ -203,18 +199,15 @@ impl JitContext {
) -> Option<BBContext> {
if let Some(bb) = self.target_ctx.remove(&bbid) {
Some(bb)
} else if let Some(bb) = self.incoming_context(ir, func, bbid) {
Some(bb)
} else {
None
self.incoming_context(ir, func, bbid)
}
}

fn prepare_next(&mut self, ir: &mut AsmIr, bbctx: BBContext, func: &ISeqInfo, end: BcIndex) {
let next_idx = end + 1;
if let Some(next_bbid) = func.bb_info.is_bb_head(next_idx) {
let label = self.label();
self.new_continue(func, end, next_bbid, bbctx, label);
self.new_continue(func, end, next_bbid, bbctx);
if let Some(target_ctx) = self.incoming_context(ir, func, next_bbid) {
assert!(self.target_ctx.insert(next_bbid, target_ctx).is_none());
}
Expand Down Expand Up @@ -503,43 +496,43 @@ impl JitContext {
TraceIr::FCmpBr {
kind,
info,
dest,
dest_bb,
brkind,
} => {
let index = bc_pos + 1;
let branch_dest = self.label();
let src_idx = bc_pos + 1;
let dest = self.label();
let mode = bbctx.fmode(ir, info);
bbctx.discard(info.dst);
bbctx.clear_above_next_sp();
ir.float_cmp_br(mode, kind, brkind, branch_dest);
self.new_side_branch(func, index, dest, bbctx.clone(), branch_dest);
ir.float_cmp_br(mode, kind, brkind, dest);
self.new_side_branch(func, src_idx, dest_bb, bbctx.clone(), dest);
}
TraceIr::ICmpBr {
kind,
dst: _,
mode,
dest,
dest_bb,
brkind,
} => {
let index = bc_pos + 1;
let branch_dest = self.label();
bbctx.gen_cmpbr_integer(ir, kind, mode, brkind, branch_dest);
self.new_side_branch(func, index, dest, bbctx.clone(), branch_dest);
let src_idx = bc_pos + 1;
let dest = self.label();
bbctx.gen_cmpbr_integer(ir, kind, mode, brkind, dest);
self.new_side_branch(func, src_idx, dest_bb, bbctx.clone(), dest);
}
TraceIr::GCmpBr {
kind,
info,
dest,
dest_bb,
brkind,
} => {
let recv_class = info.lhs_class;
let name = Self::cmpkind_to_id(kind);
if let Some(fid) = self.jit_check_method(store, recv_class, name) {
match self.compile_binop_call(bbctx, ir, store, fid, info) {
CompileResult::Continue => {
let index = bc_pos + 1;
let src_idx = bc_pos + 1;
bbctx.unset_class_version_guard();
self.gen_cond_br(bbctx, ir, func, index, dest, brkind);
self.gen_cond_br(bbctx, ir, func, src_idx, dest_bb, brkind);
//let branch_dest = self.label();
//ir.push(AsmInst::CondBr(brkind, branch_dest));
//self.new_branch(func, index, dest, bbctx.clone(), branch_dest);
Expand Down
73 changes: 36 additions & 37 deletions monoruby/src/compiler/jitgen/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,9 @@ pub struct JitContext {
///
pub(super) bridges: Vec<(AsmIr, JitLabel, BasicBlockId)>,
///
/// Information for bridges.
///
pub(super) bridges2: Vec<(AsmIr, JitLabel, BasicBlockId)>,
///
/// Information for continuation bridge.
/// Information for continue bridges.
///
pub(super) continuation_bridge: Option<(Option<ContinuationInfo>, JitLabel)>,
pub(super) continue_bridges: HashMap<BasicBlockId, (AsmIr, Option<BasicBlockId>)>,
///
/// Information for `JitLabel`s`.
///
Expand Down Expand Up @@ -167,8 +163,7 @@ impl JitContext {
self_class,
self_ty,
bridges: vec![],
bridges2: vec![],
continuation_bridge: None,
continue_bridges: HashMap::default(),
labels,
class_version,
ir: vec![],
Expand Down Expand Up @@ -200,8 +195,7 @@ impl JitContext {
self_class: NIL_CLASS,
self_ty: None,
bridges: vec![],
bridges2: vec![],
continuation_bridge: None,
continue_bridges: HashMap::default(),
labels: vec![],
class_version: 0,
ir: vec![],
Expand Down Expand Up @@ -320,20 +314,22 @@ impl JitContext {
&mut self,
func: &ISeqInfo,
src_idx: BcIndex,
dest: BasicBlockId,
dest_bb: BasicBlockId,
mut bbctx: BBContext,
branch_dest: JitLabel,
dest: JitLabel,
) {
bbctx.sp = func.get_sp(src_idx);
let src_bb = func.bb_info.get_bb_id(src_idx);
#[cfg(feature = "jit-debug")]
eprintln!(" new_branch: [{:?}]{src_idx}->{:?}", bbctx.sp, dest);
self.branch_map.entry(dest).or_default().push(BranchEntry {
src_bb,
bbctx,
branch_dest,
cont: BranchMode::Side,
});
eprintln!(" new_branch: [{:?}]{src_idx}->{:?}", bbctx.sp, dest_bb);
self.branch_map
.entry(dest_bb)
.or_default()
.push(BranchEntry {
src_bb,
bbctx,
mode: BranchMode::Side { dest },
});
}

///
Expand All @@ -343,20 +339,22 @@ impl JitContext {
&mut self,
func: &ISeqInfo,
src_idx: BcIndex,
dest: BasicBlockId,
dest_bb: BasicBlockId,
mut bbctx: BBContext,
branch_dest: JitLabel,
dest: JitLabel,
) {
bbctx.sp = func.get_sp(src_idx);
let src_bb = func.bb_info.get_bb_id(src_idx);
#[cfg(feature = "jit-debug")]
eprintln!(" new_branch: [{:?}]{src_idx}->{:?}", bbctx.sp, dest);
self.branch_map.entry(dest).or_default().push(BranchEntry {
src_bb,
bbctx,
branch_dest,
cont: BranchMode::Branch,
});
eprintln!(" new_branch: [{:?}]{src_idx}->{:?}", bbctx.sp, dest_bb);
self.branch_map
.entry(dest_bb)
.or_default()
.push(BranchEntry {
src_bb,
bbctx,
mode: BranchMode::Branch { dest },
});
}

///
Expand All @@ -366,20 +364,21 @@ impl JitContext {
&mut self,
func: &ISeqInfo,
src_idx: BcIndex,
dest: BasicBlockId,
dest_bb: BasicBlockId,
mut bbctx: BBContext,
branch_dest: JitLabel,
) {
bbctx.sp = func.get_sp(src_idx);
let src_bb = func.bb_info.get_bb_id(src_idx);
#[cfg(feature = "jit-debug")]
eprintln!(" new_continue:[{:?}] {src_idx}->{:?}", bbctx.sp, dest);
self.branch_map.entry(dest).or_default().push(BranchEntry {
src_bb,
bbctx,
branch_dest,
cont: BranchMode::Continue,
})
eprintln!(" new_continue:[{:?}] {src_idx}->{:?}", bbctx.sp, dest_bb);
self.branch_map
.entry(dest_bb)
.or_default()
.push(BranchEntry {
src_bb,
bbctx,
mode: BranchMode::Continue,
})
}

///
Expand Down
Loading

0 comments on commit ba91477

Please sign in to comment.