From 1d69544add0e9dd4180fd562a8f2554bc4f3fb77 Mon Sep 17 00:00:00 2001 From: MilkeeyCat Date: Sun, 13 Oct 2024 09:56:40 +0300 Subject: [PATCH] fix(codegen): handle cases of casting from smol to big type and vice versa fixes #81 --- src/codegen/codegen.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/codegen/codegen.rs b/src/codegen/codegen.rs index ae2db71..6c03cab 100644 --- a/src/codegen/codegen.rs +++ b/src/codegen/codegen.rs @@ -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)?;