use super::super::PpcSectionMem;
use ppc::{PpcCpu, PpcMemory};
const GPR_ARGUMENTS: usize = 8;
const FPR_ARGUMENTS: usize = 13;
const LINKAGE_AREA_BYTES: u32 = 24;
pub struct PpcCallArgs<'a> {
cpu: &'a PpcCpu,
memory: &'a mut PpcSectionMem,
word: usize,
fpr: usize,
}
impl<'a> PpcCallArgs<'a> {
pub fn new(cpu: &'a PpcCpu, memory: &'a mut PpcSectionMem) -> Self {
Self {
cpu,
memory,
word: 0,
fpr: 0,
}
}
pub fn memory(&mut self) -> &mut PpcSectionMem {
self.memory
}
fn stack_word_address(&self, word: usize) -> u32 {
self.cpu.gpr[1]
.wrapping_add(LINKAGE_AREA_BYTES)
.wrapping_add((word as u32).wrapping_mul(4))
}
fn word_at(&mut self, word: usize) -> u32 {
if word < GPR_ARGUMENTS {
self.cpu.gpr[3 + word]
} else {
let address = self.stack_word_address(word);
self.memory.read_u32_be(address).unwrap_or(0)
}
}
pub fn u32(&mut self) -> u32 {
let value = self.word_at(self.word);
self.word += 1;
value
}
pub fn i32(&mut self) -> i32 {
self.u32() as i32
}
pub fn f32(&mut self) -> f32 {
let value = if self.fpr < FPR_ARGUMENTS {
f64::from_bits(self.cpu.fpr[1 + self.fpr]) as f32
} else {
f32::from_bits(self.word_at(self.word))
};
self.fpr += 1;
self.word += 1;
value
}
pub fn f64(&mut self) -> f64 {
let value = if self.fpr < FPR_ARGUMENTS {
f64::from_bits(self.cpu.fpr[1 + self.fpr])
} else {
let high = self.word_at(self.word);
let low = self.word_at(self.word + 1);
f64::from_bits((u64::from(high) << 32) | u64::from(low))
};
self.fpr += 1;
self.word += 2;
value
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn floats_shadow_general_registers_and_spill_to_the_parameter_area() {
let mut cpu = PpcCpu::new();
let mut memory = PpcSectionMem::new();
cpu.gpr[3] = 7;
cpu.gpr[5] = 9;
cpu.gpr[8] = 11;
cpu.fpr[1] = 1.5f64.to_bits();
cpu.fpr[2] = (-2.25f64).to_bits();
let mut args = PpcCallArgs::new(&cpu, &mut memory);
assert_eq!(args.u32(), 7);
assert_eq!(args.f32(), 1.5);
assert_eq!(args.u32(), 9);
assert_eq!(args.f64(), -2.25);
assert_eq!(args.u32(), 11);
}
#[test]
fn ninth_integer_argument_comes_from_the_caller_parameter_area() {
let mut cpu = PpcCpu::new();
let mut memory = PpcSectionMem::new();
cpu.gpr[1] = 0x1000;
memory.add_region(0x1000, vec![0; 64]);
memory
.write_u32_be(0x1000 + 24 + 8 * 4, 0xDEAD_BEEF)
.unwrap();
for index in 0..8 {
cpu.gpr[3 + index] = index as u32;
}
let mut args = PpcCallArgs::new(&cpu, &mut memory);
for index in 0..8 {
assert_eq!(args.u32(), index);
}
assert_eq!(args.u32(), 0xDEAD_BEEF);
}
}