Skip to content

Commit

Permalink
fix(arch/amd64): use correct register sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
MilkeeyCat committed Aug 26, 2024
1 parent 99a1460 commit c3c6065
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
23 changes: 16 additions & 7 deletions src/archs/amd64/amd64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,19 +256,28 @@ impl Architecture for Amd64 {

//NOTE: if mafs doesn't works, probably because of this xd
fn div(&mut self, dest: &Destination, src: &Source, signed: bool) -> Result<(), ArchError> {
let register = operands::Register {
register: self.rax,
size: WORD_SIZE,
};

self.mov(src, &Destination::Register(register.clone()), signed)?;
self.mov(
src,
&Destination::Register(operands::Register {
register: self.rax,
size: WORD_SIZE,
}),
signed,
)?;
self.buf.push_str(&formatdoc!(
"
\tcqo
\tidiv {src}
",
));
self.mov(&Source::Register(register.clone()), dest, signed)?;
self.mov(
&Source::Register(operands::Register {
register: self.rax,
size: src.size().unwrap_or(WORD_SIZE),
}),
dest,
signed,
)?;

Ok(())
}
Expand Down
10 changes: 5 additions & 5 deletions src/codegen/operands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ impl std::fmt::Display for Offset {
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub enum Base {
Register(Register),
Label(String),
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Register {
pub register: register::Register,
pub size: usize,
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct EffectiveAddress {
pub base: Base,
pub index: Option<Register>,
Expand All @@ -73,7 +73,7 @@ pub enum Immediate {
Label(String),
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Memory {
pub effective_address: EffectiveAddress,
pub size: usize,
Expand Down Expand Up @@ -106,7 +106,7 @@ impl Source {
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub enum Destination {
Memory(Memory),
Register(Register),
Expand Down
6 changes: 3 additions & 3 deletions src/symbol_table/symbol_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Symbol {
scale: None,
displacement: Some(symbol.offset.clone()),
},
size: 1,
size: symbol.type_.size(arch, scope)?,
}),
Self::Global(symbol) => Destination::Memory(Memory {
effective_address: EffectiveAddress {
Expand All @@ -67,7 +67,7 @@ impl Symbol {
scale: None,
displacement: None,
},
size: 1,
size: symbol.type_.size(arch, scope)?,
}),
Self::Param(symbol) => Destination::Memory(Memory {
effective_address: EffectiveAddress {
Expand All @@ -79,7 +79,7 @@ impl Symbol {
scale: None,
displacement: Some(symbol.offset.clone()),
},
size: 1,
size: symbol.type_.size(arch, scope)?,
}),
Self::Function(_) => unreachable!(),
})
Expand Down

0 comments on commit c3c6065

Please sign in to comment.