From cced2d9617fcc2dd02f4a590aaee9378c10e4f44 Mon Sep 17 00:00:00 2001 From: MilkeeyCat Date: Sun, 13 Oct 2024 19:00:18 +0300 Subject: [PATCH] refactor(codegen): use `word_size` is type is array --- programs/arrays.mk | 2 ++ src/codegen/codegen.rs | 10 ++++++---- src/types/types.rs | 3 +++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/programs/arrays.mk b/programs/arrays.mk index 225f059..00dfef9 100644 --- a/programs/arrays.mk +++ b/programs/arrays.mk @@ -1,6 +1,8 @@ fn main() -> u8 { let arr: i32[4]; let arr_ptr: *i32 = arr as *i32; + let double_cast: *i32 = arr as *i32 as *void; + let void_ptr: *void = arr as *void; if arr_ptr == NULL { return 1; diff --git a/src/codegen/codegen.rs b/src/codegen/codegen.rs index 9888b33..d952cfd 100644 --- a/src/codegen/codegen.rs +++ b/src/codegen/codegen.rs @@ -314,10 +314,12 @@ 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) - .clamp(0, self.arch.word_size()); + let og_size = if let Type::Array(_) = &type_ { + self.arch.word_size() + } else { + self.arch.size(&type_, &self.scope) + }; + assert!(og_size <= 8); let casted_size = self.arch.size(&cast_expr.type_, &self.scope); if casted_size != og_size { diff --git a/src/types/types.rs b/src/types/types.rs index b0254c1..0529035 100644 --- a/src/types/types.rs +++ b/src/types/types.rs @@ -146,6 +146,9 @@ impl Type { { Ok(to) } + (Type::Array(_), Type::Ptr(pointee)) if pointee.as_ref() == &Type::Void => { + Ok(Type::Ptr(pointee)) + } (from, to) if from.ptr() && to.ptr() => Ok(to), (from, to) if from.ptr() && to.int() => Ok(to), (from, to) => Err(TypeError::Cast(from, to)),