Skip to content

Commit

Permalink
refactor(codegen): use word_size is type is array
Browse files Browse the repository at this point in the history
  • Loading branch information
MilkeeyCat committed Oct 13, 2024
1 parent d3b7b9e commit 73a4b87
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 2 additions & 0 deletions programs/arrays.mk
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
fn main() -> u8 {
let arr: i32[4];
let arr_ptr: *i32 = arr as *i32;
let double_cast: *void = arr as *i32 as *void;
let void_ptr: *void = arr as *void;

if arr_ptr == NULL {
return 1;
Expand Down
10 changes: 6 additions & 4 deletions src/codegen/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions src/types/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down

0 comments on commit 73a4b87

Please sign in to comment.