Skip to content

Commit

Permalink
fix(codegen): handle cases of casting from smol to big type and vice …
Browse files Browse the repository at this point in the history
…versa

fixes #81
  • Loading branch information
MilkeeyCat committed Oct 13, 2024
1 parent 7502d61 commit 1d69544
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/codegen/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,16 +314,25 @@ impl CodeGen {
Expr::Cast(cast_expr) => {
if let Some(dest) = dest {
let type_ = cast_expr.expr.type_(&self.scope)?;
let og_size = self.arch.size(&type_, &self.scope);
let casted_size = self.arch.size(&cast_expr.type_, &self.scope);

if casted_size < self.arch.size(&type_, &self.scope) {
if casted_size != og_size {
let (r, new) = match dest {
Destination::Memory(_) => (self.arch.alloc()?, true),
Destination::Register(register) => (register.register, false),
};

self.expr(*cast_expr.expr, Some(&r.dest(self.arch.word_size())), state)?;
self.expr(*cast_expr.expr, Some(&r.dest(og_size)), state)?;

if new {
if casted_size > og_size {
self.arch.mov(
&r.source(og_size),
&r.dest(casted_size),
type_.signed(),
)?;
}
self.arch
.mov(&r.source(casted_size), dest, type_.signed())?;
self.arch.free(r)?;
Expand Down

0 comments on commit 1d69544

Please sign in to comment.