From 9db508bd9c7dd91f235948b792fe7965ae91c7aa Mon Sep 17 00:00:00 2001 From: sofia Date: Mon, 21 Jul 2025 13:52:37 +0300 Subject: [PATCH] Change user-space pointer to different type --- reid/src/ast/process.rs | 2 +- reid/src/codegen.rs | 29 +++++++++++++++++------------ reid/src/mir/fmt.rs | 6 +++++- reid/src/mir/implement.rs | 10 ++++++---- reid/src/mir/mod.rs | 3 ++- reid/src/mir/typecheck.rs | 8 ++++---- reid/src/mir/typeinference.rs | 4 ++-- 7 files changed, 37 insertions(+), 25 deletions(-) diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index fdb06a9..8bc2d87 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -294,7 +294,7 @@ impl From for mir::TypeKind { mir::TypeKind::Borrow(Box::new(mir::TypeKind::from(*type_kind.clone())), *mutable) } ast::TypeKind::Ptr(type_kind) => { - mir::TypeKind::Ptr(Box::new(mir::TypeKind::from(*type_kind.clone()))) + mir::TypeKind::UserPtr(Box::new(mir::TypeKind::from(*type_kind.clone()))) } } } diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index 15a1edd..de125e0 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -368,7 +368,7 @@ impl mir::Module { p_name.clone(), StackValue( StackValueKind::mutable(p_ty.is_mutable(), alloca), - TypeKind::Ptr(Box::new(p_ty.clone())), + TypeKind::CodegenPtr(Box::new(p_ty.clone())), ), ); @@ -511,7 +511,7 @@ impl mir::Statement { scope.stack_values.insert( name.clone(), - StackValue(stack_value, TypeKind::Ptr(Box::new(value.clone().1))), + StackValue(stack_value, TypeKind::CodegenPtr(Box::new(value.clone().1))), ); if let Some(debug) = &scope.debug { let location = self.1.into_debug(scope.tokens).unwrap(); @@ -595,7 +595,7 @@ impl mir::Expression { .expect("Variable reference not found?!"); Some({ if state.should_load { - if let TypeKind::Ptr(inner) = &v.1 { + if let TypeKind::CodegenPtr(inner) = &v.1 { StackValue( v.0.derive( scope @@ -715,7 +715,7 @@ impl mir::Expression { } else { Some(StackValue( StackValueKind::Immutable(ptr), - TypeKind::Ptr(Box::new(ret_type_kind)), + TypeKind::CodegenPtr(Box::new(ret_type_kind)), )) } } else { @@ -758,7 +758,7 @@ impl mir::Expression { .unwrap() .maybe_location(&mut scope.block, location); - let TypeKind::Ptr(inner) = array_ty else { + let TypeKind::CodegenPtr(inner) = array_ty else { panic!(); }; let TypeKind::Array(elem_ty, _) = *inner else { @@ -783,7 +783,7 @@ impl mir::Expression { *elem_ty, )) } else { - Some(StackValue(kind.derive(ptr), TypeKind::Ptr(elem_ty))) + Some(StackValue(kind.derive(ptr), TypeKind::CodegenPtr(elem_ty))) } } mir::ExprKind::Array(expressions) => { @@ -856,7 +856,7 @@ impl mir::Expression { mir::ExprKind::Accessed(expression, type_kind, field) => { let struct_val = expression.codegen(scope, &state.load(false)).unwrap(); - let TypeKind::Ptr(inner) = &struct_val.1 else { + let TypeKind::CodegenPtr(inner) = &struct_val.1 else { panic!("tried accessing non-pointer"); }; let TypeKind::CustomType(name) = *inner.clone() else { @@ -898,7 +898,9 @@ impl mir::Expression { } else { Some(StackValue( struct_val.0.derive(value), - TypeKind::Ptr(Box::new(struct_ty.get_field_ty(&field).unwrap().clone())), + TypeKind::CodegenPtr(Box::new( + struct_ty.get_field_ty(&field).unwrap().clone(), + )), )) } } @@ -959,7 +961,7 @@ impl mir::Expression { .get(&varref.1) .expect("Variable reference not found?!"); - let TypeKind::Ptr(ptr_inner) = &v.1 else { + let TypeKind::CodegenPtr(ptr_inner) = &v.1 else { panic!(); }; @@ -976,7 +978,7 @@ impl mir::Expression { Some({ if state.should_load { - if let TypeKind::Ptr(inner) = *ptr_inner.clone() { + if let TypeKind::CodegenPtr(inner) = *ptr_inner.clone() { StackValue( v.0.derive( scope @@ -1173,7 +1175,10 @@ impl TypeKind { let type_val = type_vals.get(n).unwrap().clone(); Type::CustomType(type_val) } - TypeKind::Ptr(type_kind) => { + TypeKind::UserPtr(type_kind) => { + Type::Ptr(Box::new(type_kind.get_type(type_vals, typedefs))) + } + TypeKind::CodegenPtr(type_kind) => { Type::Ptr(Box::new(type_kind.get_type(type_vals, typedefs))) } TypeKind::Borrow(type_kind, _) => { @@ -1223,7 +1228,7 @@ impl TypeKind { ), size_bits: self.size_of(), }), - TypeKind::Ptr(inner) | TypeKind::Borrow(inner, _) => { + TypeKind::CodegenPtr(inner) | TypeKind::Borrow(inner, _) => { DebugTypeData::Pointer(DebugPointerType { name, pointee: inner.get_debug_type_hard( diff --git a/reid/src/mir/fmt.rs b/reid/src/mir/fmt.rs index b578b47..3048c90 100644 --- a/reid/src/mir/fmt.rs +++ b/reid/src/mir/fmt.rs @@ -345,10 +345,14 @@ impl Display for TypeKind { write!(f, "&mut ")?; Display::fmt(type_kind, f) } - TypeKind::Ptr(type_kind) => { + TypeKind::UserPtr(type_kind) => { write!(f, "*")?; Display::fmt(type_kind, f) } + TypeKind::CodegenPtr(type_kind) => { + write!(f, "CodegenPtr ")?; + Display::fmt(type_kind, f) + } TypeKind::Vague(vague_type) => Display::fmt(vague_type, f), } } diff --git a/reid/src/mir/implement.rs b/reid/src/mir/implement.rs index 8dddeff..5b3483b 100644 --- a/reid/src/mir/implement.rs +++ b/reid/src/mir/implement.rs @@ -46,9 +46,10 @@ impl TypeKind { TypeKind::StringPtr => 32, TypeKind::Array(type_kind, len) => type_kind.size_of() * len, TypeKind::CustomType(_) => 32, - TypeKind::Ptr(_) => 64, + TypeKind::CodegenPtr(_) => 64, TypeKind::Vague(_) => panic!("Tried to sizeof a vague type!"), TypeKind::Borrow(_, _) => 64, + TypeKind::UserPtr(_) => 64, } } @@ -69,9 +70,10 @@ impl TypeKind { TypeKind::StringPtr => 32, TypeKind::Array(type_kind, _) => type_kind.alignment(), TypeKind::CustomType(_) => 32, - TypeKind::Ptr(_) => 64, + TypeKind::CodegenPtr(_) => 64, TypeKind::Vague(_) => panic!("Tried to sizeof a vague type!"), TypeKind::Borrow(_, _) => 64, + TypeKind::UserPtr(_) => 64, } } @@ -397,8 +399,8 @@ impl Collapsable for TypeKind { )) } } - (TypeKind::Ptr(val1), TypeKind::Ptr(val2)) => { - Ok(TypeKind::Ptr(Box::new(val1.collapse_into(val2)?))) + (TypeKind::UserPtr(val1), TypeKind::UserPtr(val2)) => { + Ok(TypeKind::UserPtr(Box::new(val1.collapse_into(val2)?))) } _ => Err(ErrorKind::TypesIncompatible(self.clone(), other.clone())), } diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index 9024be5..0fe2b2b 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -95,7 +95,8 @@ pub enum TypeKind { Array(Box, u64), CustomType(String), Borrow(Box, bool), - Ptr(Box), + UserPtr(Box), + CodegenPtr(Box), Vague(VagueType), } diff --git a/reid/src/mir/typecheck.rs b/reid/src/mir/typecheck.rs index f89c232..248e1dd 100644 --- a/reid/src/mir/typecheck.rs +++ b/reid/src/mir/typecheck.rs @@ -39,8 +39,8 @@ pub enum ErrorKind { TypeNotInferrable(TypeKind), #[error("Expected branch type to be {0}, found {1} instead")] BranchTypesDiffer(TypeKind, TypeKind), - #[error("Attempted to index a non-array type of {0}")] - TriedIndexingNonArray(TypeKind), + #[error("Attempted to index a non-indexable type of {0}")] + TriedIndexingNonIndexable(TypeKind), #[error("Index {0} out of bounds ({1})")] IndexOutOfBounds(u64, u64), #[error("No such type {0} could be found")] @@ -535,7 +535,7 @@ impl Expression { let expr_t = expression.typecheck(state, typerefs, hint_t)?; match expr_t { - TypeKind::Array(inferred_ty, _) | TypeKind::Ptr(inferred_ty) => { + TypeKind::Array(inferred_ty, _) | TypeKind::UserPtr(inferred_ty) => { let ty = state.or_else( elem_ty.resolve_ref(typerefs).collapse_into(&inferred_ty), TypeKind::Vague(Vague::Unknown), @@ -544,7 +544,7 @@ impl Expression { *elem_ty = ty.clone(); Ok(ty) } - _ => Err(ErrorKind::TriedIndexingNonArray(expr_t)), + _ => Err(ErrorKind::TriedIndexingNonIndexable(expr_t)), } } ExprKind::Array(expressions) => { diff --git a/reid/src/mir/typeinference.rs b/reid/src/mir/typeinference.rs index 60eb1d8..efb79d3 100644 --- a/reid/src/mir/typeinference.rs +++ b/reid/src/mir/typeinference.rs @@ -267,12 +267,12 @@ impl Expression { // need for further resolution. let kind = expr_ty.resolve_weak().unwrap(); match kind { - Array(type_kind, _) | Ptr(type_kind) => { + Array(type_kind, _) | UserPtr(type_kind) => { let elem_ty = type_refs.from_type(&type_kind).unwrap(); *index_ty = elem_ty.as_type().clone(); Ok(elem_ty) } - _ => Err(ErrorKind::TriedIndexingNonArray(kind)), + _ => Err(ErrorKind::TriedIndexingNonIndexable(kind)), } } ExprKind::Array(expressions) => {