add debugger
This commit is contained in:
@@ -11,6 +11,7 @@ pub enum LogicalFrameEndingReason {
|
||||
BudgetExhausted,
|
||||
Halted,
|
||||
EndOfRom,
|
||||
Breakpoint,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -29,6 +30,7 @@ pub struct VirtualMachine {
|
||||
pub heap: Vec<Value>, // Simplificado para demo, futuramente RAM/Heap real
|
||||
pub cycles: u64,
|
||||
pub halted: bool,
|
||||
pub breakpoints: std::collections::HashSet<usize>,
|
||||
}
|
||||
|
||||
impl VirtualMachine {
|
||||
@@ -42,13 +44,21 @@ impl VirtualMachine {
|
||||
heap: Vec::new(),
|
||||
cycles: 0,
|
||||
halted: false,
|
||||
breakpoints: std::collections::HashSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn initialize(&mut self, program_bytes: Vec<u8>, entrypoint: &str) {
|
||||
// Por enquanto, tratamos os bytes como a ROM diretamente.
|
||||
// TODO: Implementar parser de .pbc para extrair constant_pool e rom reais.
|
||||
self.program = Program::new(program_bytes, vec![]);
|
||||
if program_bytes.starts_with(b"PPBC") {
|
||||
if let Ok((rom, cp)) = self.parse_pbc(&program_bytes) {
|
||||
self.program = Program::new(rom, cp);
|
||||
} else {
|
||||
self.program = Program::new(program_bytes, vec![]);
|
||||
}
|
||||
} else {
|
||||
// Por enquanto, tratamos os bytes como a ROM diretamente.
|
||||
self.program = Program::new(program_bytes, vec![]);
|
||||
}
|
||||
|
||||
// Se o entrypoint for numérico, podemos tentar usá-lo como PC inicial.
|
||||
// Se não, por enquanto ignoramos ou começamos do 0.
|
||||
@@ -65,6 +75,73 @@ impl VirtualMachine {
|
||||
self.cycles = 0;
|
||||
self.halted = false;
|
||||
}
|
||||
|
||||
fn parse_pbc(&self, bytes: &[u8]) -> Result<(Vec<u8>, Vec<Value>), String> {
|
||||
let mut cursor = 4; // Skip "PPBC"
|
||||
|
||||
let cp_count = self.read_u32_at(bytes, &mut cursor)? as usize;
|
||||
let mut cp = Vec::with_capacity(cp_count);
|
||||
|
||||
for _ in 0..cp_count {
|
||||
if cursor >= bytes.len() { return Err("Unexpected end of PBC".into()); }
|
||||
let tag = bytes[cursor];
|
||||
cursor += 1;
|
||||
|
||||
match tag {
|
||||
1 => { // Integer
|
||||
let val = self.read_i64_at(bytes, &mut cursor)?;
|
||||
cp.push(Value::Integer(val));
|
||||
}
|
||||
2 => { // Float
|
||||
let val = self.read_f64_at(bytes, &mut cursor)?;
|
||||
cp.push(Value::Float(val));
|
||||
}
|
||||
3 => { // Boolean
|
||||
if cursor >= bytes.len() { return Err("Unexpected end of PBC".into()); }
|
||||
let val = bytes[cursor] != 0;
|
||||
cursor += 1;
|
||||
cp.push(Value::Boolean(val));
|
||||
}
|
||||
4 => { // String
|
||||
let len = self.read_u32_at(bytes, &mut cursor)? as usize;
|
||||
if cursor + len > bytes.len() { return Err("Unexpected end of PBC".into()); }
|
||||
let s = String::from_utf8_lossy(&bytes[cursor..cursor + len]).into_owned();
|
||||
cursor += len;
|
||||
cp.push(Value::String(s));
|
||||
}
|
||||
_ => cp.push(Value::Null),
|
||||
}
|
||||
}
|
||||
|
||||
let rom_size = self.read_u32_at(bytes, &mut cursor)? as usize;
|
||||
if cursor + rom_size > bytes.len() {
|
||||
return Err("Invalid ROM size in PBC".into());
|
||||
}
|
||||
let rom = bytes[cursor..cursor + rom_size].to_vec();
|
||||
|
||||
Ok((rom, cp))
|
||||
}
|
||||
|
||||
fn read_u32_at(&self, bytes: &[u8], cursor: &mut usize) -> Result<u32, String> {
|
||||
if *cursor + 4 > bytes.len() { return Err("Unexpected end of PBC".into()); }
|
||||
let val = u32::from_le_bytes(bytes[*cursor..*cursor + 4].try_into().unwrap());
|
||||
*cursor += 4;
|
||||
Ok(val)
|
||||
}
|
||||
|
||||
fn read_i64_at(&self, bytes: &[u8], cursor: &mut usize) -> Result<i64, String> {
|
||||
if *cursor + 8 > bytes.len() { return Err("Unexpected end of PBC".into()); }
|
||||
let val = i64::from_le_bytes(bytes[*cursor..*cursor + 8].try_into().unwrap());
|
||||
*cursor += 8;
|
||||
Ok(val)
|
||||
}
|
||||
|
||||
fn read_f64_at(&self, bytes: &[u8], cursor: &mut usize) -> Result<f64, String> {
|
||||
if *cursor + 8 > bytes.len() { return Err("Unexpected end of PBC".into()); }
|
||||
let val = f64::from_le_bytes(bytes[*cursor..*cursor + 8].try_into().unwrap());
|
||||
*cursor += 8;
|
||||
Ok(val)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for VirtualMachine {
|
||||
@@ -88,6 +165,11 @@ impl VirtualMachine {
|
||||
&& !self.halted
|
||||
&& self.pc < self.program.rom.len()
|
||||
{
|
||||
if steps_executed > 0 && self.breakpoints.contains(&self.pc) {
|
||||
ending_reason = Some(LogicalFrameEndingReason::Breakpoint);
|
||||
break;
|
||||
}
|
||||
|
||||
let pc_before = self.pc;
|
||||
let cycles_before = self.cycles;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user