Skip to content

Commit ee9ff6a

Browse files
committed
WIP
1 parent 6c9986c commit ee9ff6a

File tree

5 files changed

+32
-85
lines changed

5 files changed

+32
-85
lines changed

src/jit/assembler/basic_block.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -240,22 +240,17 @@ impl BasicBlock {
240240
}
241241
}
242242

243-
pub fn remove_dead_code(&mut self, asm: &BlockAsm) {
243+
pub fn remove_dead_code(&mut self, asm: &mut BlockAsm) {
244244
let mut current_node = self.insts_link.root;
245245
let mut i = 0;
246246
while !current_node.is_null() {
247247
let inst_i = BlockInstList::deref(current_node).value;
248-
let inst = &asm.buf.insts[inst_i];
248+
let inst = &mut asm.buf.insts[inst_i];
249249
if let BlockInstKind::RestoreReg { guest_reg, .. } = &inst.kind {
250250
if *guest_reg != Reg::CPSR {
251251
let (_, outputs) = inst.get_io();
252252
if (self.regs_live_ranges[i + 1] - outputs) == self.regs_live_ranges[i + 1] {
253-
let next_node = BlockInstList::deref(current_node).next;
254-
self.insts_link.remove_entry(current_node);
255-
current_node = next_node;
256-
self.regs_live_ranges.remove(i);
257-
self.used_regs.remove(i);
258-
continue;
253+
inst.skip = true;
259254
}
260255
}
261256
}
@@ -288,12 +283,15 @@ impl BasicBlock {
288283
let mut current_node = self.insts_link.root;
289284
while !current_node.is_null() {
290285
let inst_i = BlockInstList::deref(current_node).value;
291-
asm.buf.reg_allocator.inst_allocate(&mut asm.buf.insts[inst_i], &self.regs_live_ranges[i..], &self.used_regs[i..]);
292-
if !asm.buf.reg_allocator.pre_allocate_insts.is_empty() {
293-
for i in asm.buf.insts.len()..asm.buf.insts.len() + asm.buf.reg_allocator.pre_allocate_insts.len() {
294-
self.insts_link.insert_entry_begin(current_node, i);
286+
let inst = &mut asm.buf.insts[inst_i];
287+
if !inst.skip {
288+
asm.buf.reg_allocator.inst_allocate(inst, &self.regs_live_ranges[i..], &self.used_regs[i..]);
289+
if !asm.buf.reg_allocator.pre_allocate_insts.is_empty() {
290+
for i in asm.buf.insts.len()..asm.buf.insts.len() + asm.buf.reg_allocator.pre_allocate_insts.len() {
291+
self.insts_link.insert_entry_begin(current_node, i);
292+
}
293+
asm.buf.insts.extend_from_slice(&asm.buf.reg_allocator.pre_allocate_insts);
295294
}
296-
asm.buf.insts.extend_from_slice(&asm.buf.reg_allocator.pre_allocate_insts);
297295
}
298296
i += 1;
299297
current_node = BlockInstList::deref(current_node).next;
@@ -322,6 +320,9 @@ impl BasicBlock {
322320
let mut inst_opcodes = Vec::new();
323321
for entry in self.insts_link.iter() {
324322
let inst = &mut asm.buf.insts[entry.value];
323+
if inst.skip {
324+
continue;
325+
}
325326

326327
if IS_DEBUG && unsafe { BLOCK_LOG } {
327328
match &inst.kind {

src/jit/assembler/block_asm.rs

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::jit::{Cond, MemoryAmount, ShiftType};
1212
use crate::utils::{NoHashMap, NoHashSet};
1313
use crate::IS_DEBUG;
1414
use std::intrinsics::unlikely;
15-
use std::{ptr, slice};
15+
use std::slice;
1616

1717
pub static mut BLOCK_LOG: bool = false;
1818

@@ -58,7 +58,6 @@ pub struct BlockAsm<'a> {
5858

5959
cond_block_end_label_stack: Vec<(BlockLabel, Cond, usize)>,
6060

61-
guest_reg_init_offset: Option<usize>,
6261
is_common_fun: bool,
6362
host_sp_ptr: *mut usize,
6463
block_start: usize,
@@ -94,7 +93,6 @@ impl<'a> BlockAsm<'a> {
9493

9594
cond_block_end_label_stack: Vec::new(),
9695

97-
guest_reg_init_offset: None,
9896
is_common_fun,
9997
host_sp_ptr,
10098
block_start: 0,
@@ -117,7 +115,9 @@ impl<'a> BlockAsm<'a> {
117115

118116
if !is_common_fun {
119117
instance.mov(thread_regs_addr_reg, guest_regs_ptr as u32);
120-
instance.guest_reg_init_offset = Some(instance.buf.insts.len() - 1);
118+
for guest_reg in RegReserve::gp() + Reg::SP + Reg::LR {
119+
instance.restore_reg(guest_reg);
120+
}
121121
instance.restore_reg(Reg::CPSR);
122122
}
123123

@@ -917,72 +917,14 @@ impl<'a> BlockAsm<'a> {
917917
basic_block.remove_dead_code(self);
918918
}
919919

920-
if let Some(guest_regs_init_offset) = self.guest_reg_init_offset {
921-
let required_guest_regs = basic_blocks[0].get_required_inputs().get_guests();
922-
if !required_guest_regs.is_empty() {
923-
let mut basic_block_i = 0;
924-
let mut inst_i = 0;
925-
let mut inst_entry = ptr::null_mut();
926-
927-
'basic_blocks_loop: for (i, basic_block) in basic_blocks.iter_mut().enumerate() {
928-
if !reachable_blocks.contains(&i) {
929-
continue;
930-
}
931-
932-
for (j, entry) in basic_block.insts_link.iter().enumerate() {
933-
if entry.value == guest_regs_init_offset {
934-
basic_block_i = i;
935-
inst_i = j;
936-
inst_entry = entry as *const _ as *mut _;
937-
break 'basic_blocks_loop;
938-
}
939-
}
940-
}
941-
942-
let basic_block = &mut basic_blocks[basic_block_i];
943-
for i in 0..=inst_i {
944-
basic_block.regs_live_ranges[i].remove_guests(required_guest_regs);
945-
}
946-
let enter_blocks = unsafe { slice::from_raw_parts(basic_block.enter_blocks.as_ptr(), basic_block.enter_blocks.len()) };
947-
Self::remove_guest_input_regs(&mut basic_blocks, required_guest_regs, enter_blocks);
948-
let basic_block = &mut basic_blocks[basic_block_i];
949-
950-
let mut previous_guest_reg = None;
951-
for (j, guest_reg) in required_guest_regs.into_iter().enumerate() {
952-
inst_entry = basic_block.insts_link.insert_entry_end(inst_entry, self.buf.insts.len());
953-
self.buf.insts.push(
954-
BlockInstKind::RestoreReg {
955-
guest_reg,
956-
reg_mapped: guest_reg.into(),
957-
thread_regs_addr_reg: self.thread_regs_addr_reg,
958-
tmp_guest_cpsr_reg: self.tmp_guest_cpsr_reg,
959-
}
960-
.into(),
961-
);
962-
let inst_index = inst_i + j + 1;
963-
964-
let (inputs, outputs) = self.buf.insts.last().unwrap().get_io();
965-
basic_block.used_regs.insert(inst_index, inputs + outputs);
966-
967-
let mut previous_regs_live_range = basic_block.regs_live_ranges[inst_index - 1];
968-
if let Some(previous_guest_reg) = previous_guest_reg {
969-
previous_regs_live_range += BlockReg::from(previous_guest_reg);
970-
}
971-
previous_regs_live_range += inputs;
972-
basic_block.regs_live_ranges.insert(inst_index, previous_regs_live_range);
973-
974-
previous_guest_reg = Some(guest_reg);
975-
}
976-
}
977-
}
978-
979920
(basic_blocks, reachable_blocks)
980921
}
981922

982923
pub fn emit_opcodes(&mut self, block_start_pc: u32, thumb: bool) -> usize {
983924
let (mut basic_blocks, reachable_blocks) = self.assemble_basic_blocks(block_start_pc, thumb);
984925

985926
if !basic_blocks[0].get_required_inputs().get_guests().is_empty() {
927+
println!("inputs as requirement {:?}", basic_blocks[0].get_required_inputs().get_guests());
986928
unsafe { BLOCK_LOG = true };
987929
}
988930

src/jit/assembler/block_inst.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub struct BlockInst {
7878
pub cond: Cond,
7979
pub kind: BlockInstKind,
8080
io_cache: RefCell<Option<(BlockRegSet, BlockRegSet)>>,
81+
pub skip: bool,
8182
}
8283

8384
impl BlockInst {
@@ -86,6 +87,7 @@ impl BlockInst {
8687
cond,
8788
kind,
8889
io_cache: RefCell::new(None),
90+
skip: false,
8991
}
9092
}
9193

@@ -129,7 +131,11 @@ impl From<BlockInstKind> for BlockInst {
129131

130132
impl Debug for BlockInst {
131133
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
132-
write!(f, "{:?} {:?}", self.cond, self.kind)
134+
if self.skip {
135+
write!(f, "SKIPPED: {:?} {:?}", self.cond, self.kind)
136+
} else {
137+
write!(f, "{:?} {:?}", self.cond, self.kind)
138+
}
133139
}
134140
}
135141

src/jit/emitter/emit_transfer.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,6 @@ impl<const CPU: CpuType> JitAsm<'_, CPU> {
216216
}
217217
block_asm.restore_reg(Reg::CPSR);
218218

219-
block_asm.branch(continue_label, Cond::AL);
220-
221219
block_asm.label(continue_label);
222220

223221
block_asm.free_reg(fast_read_addr_masked_reg);

src/jit/jit_asm.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ fn emit_code_block_internal<const CPU: CpuType, const THUMB: bool>(asm: &mut Jit
186186
}
187187

188188
let jit_entry = {
189-
// unsafe { BLOCK_LOG = guest_pc == 0x20018bc };
189+
// unsafe { BLOCK_LOG = guest_pc == 0x2000800 };
190190

191191
let mut block_asm = asm.new_block_asm(false);
192192

@@ -210,11 +210,11 @@ fn emit_code_block_internal<const CPU: CpuType, const THUMB: bool>(asm: &mut Jit
210210
asm.emit(&mut block_asm);
211211
}
212212

213-
if DEBUG_LOG {
214-
block_asm.save_context();
215-
block_asm.call2(debug_after_exec_op::<CPU> as *const (), asm.jit_buf.current_pc, asm.jit_buf.current_inst().opcode);
216-
block_asm.restore_reg(Reg::CPSR);
217-
}
213+
// if DEBUG_LOG {
214+
// block_asm.save_context();
215+
// block_asm.call2(debug_after_exec_op::<CPU> as *const (), asm.jit_buf.current_pc, asm.jit_buf.current_inst().opcode);
216+
// block_asm.restore_reg(Reg::CPSR);
217+
// }
218218
}
219219

220220
let opcodes_len = block_asm.emit_opcodes(guest_pc, THUMB);

0 commit comments

Comments
 (0)