Skip to content

Commit

Permalink
fix(codegen): load address of a variable if it's of type array
Browse files Browse the repository at this point in the history
  • Loading branch information
MilkeeyCat committed Sep 11, 2024
1 parent edfd0be commit 4504814
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions src/codegen/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,30 +267,17 @@ impl CodeGen {
.ok_or(SymbolTableError::NotFound(ident.0.clone()))?;
let dst = symbol.dest(&self.arch, &self.scope)?;

// If the ident is of type pointer, the address of variable has to be moved, not the value
// If the ident is of type array, the address of variable has to be moved, not the value
if let Type::Array(_) = ident.type_(&self.scope)? {
let r = self.arch.alloc()?;
let r_op = operands::Register {
register: r,
size: self.arch.word_size(),
};

self.arch.lea(
&Destination::Register(operands::Register {
register: r,
size: self.arch.word_size(),
}),
&EffectiveAddress {
base: dst.into(),
index: None,
scale: None,
displacement: None,
},
);
self.arch.mov(
&Source::Register(operands::Register {
register: r,
size: self.arch.word_size(),
}),
&dest,
false,
)?;
self.arch
.lea(&Destination::Register(r_op.clone()), &dst.into());
self.arch.mov(&Source::Register(r_op), &dest, false)?;
self.arch.free(r)?;
} else {
self.arch.mov(&dst.into(), &dest, false)?;
Expand Down

0 comments on commit 4504814

Please sign in to comment.