From 1224c612c7032abb09465b04575f400cbc24f888 Mon Sep 17 00:00:00 2001 From: sofia Date: Tue, 22 Jul 2025 15:07:33 +0300 Subject: [PATCH] Codegen & compile char, change *str to *char --- reid/lib/std.reid | 4 ++-- reid/src/ast/mod.rs | 1 - reid/src/ast/parse.rs | 1 - reid/src/ast/process.rs | 3 +-- reid/src/codegen.rs | 17 ++++++++------- reid/src/mir/fmt.rs | 2 +- reid/src/mir/implement.rs | 44 +++++++-------------------------------- reid/src/mir/mod.rs | 6 +++--- reid/src/mir/typecheck.rs | 27 +++++------------------- 9 files changed, 29 insertions(+), 76 deletions(-) diff --git a/reid/lib/std.reid b/reid/lib/std.reid index d53a99e..6a26b18 100644 --- a/reid/lib/std.reid +++ b/reid/lib/std.reid @@ -1,5 +1,5 @@ -extern fn puts(message: *str) -> i32; +extern fn puts(message: *char) -> i32; extern fn malloc(size: u64) -> *u8; extern fn div(numerator: i32, denominator: i32) -> div_t; @@ -8,7 +8,7 @@ struct div_t { remainder: i32, } -pub fn print(message: *str) { +pub fn print(message: *char) { puts(message); } diff --git a/reid/src/ast/mod.rs b/reid/src/ast/mod.rs index 42a8140..a594164 100644 --- a/reid/src/ast/mod.rs +++ b/reid/src/ast/mod.rs @@ -31,7 +31,6 @@ pub enum TypeKind { F128, F80, F128PPC, - Str, Char, Array(Box, u64), Custom(String), diff --git a/reid/src/ast/parse.rs b/reid/src/ast/parse.rs index 1b33b7b..21020dd 100644 --- a/reid/src/ast/parse.rs +++ b/reid/src/ast/parse.rs @@ -60,7 +60,6 @@ impl Parse for Type { "f80" => TypeKind::F80, "f128" => TypeKind::F128, "f128ppc" => TypeKind::F128PPC, - "str" => TypeKind::Str, "char" => TypeKind::Char, _ => TypeKind::Custom(ident), } diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index 4ff95a4..f7241b3 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -311,7 +311,6 @@ impl From for mir::TypeKind { ast::TypeKind::Array(type_kind, length) => { mir::TypeKind::Array(Box::new(mir::TypeKind::from(*type_kind.clone())), *length) } - ast::TypeKind::Str => mir::TypeKind::Str, ast::TypeKind::Custom(name) => mir::TypeKind::CustomType(name.clone()), ast::TypeKind::Borrow(type_kind, mutable) => { mir::TypeKind::Borrow(Box::new(mir::TypeKind::from(*type_kind.clone())), *mutable) @@ -326,7 +325,7 @@ impl From for mir::TypeKind { ast::TypeKind::F80 => mir::TypeKind::F80, ast::TypeKind::F128 => mir::TypeKind::F128, ast::TypeKind::F128PPC => mir::TypeKind::F128PPC, - ast::TypeKind::Char => todo!(), + ast::TypeKind::Char => mir::TypeKind::Char, } } } diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index 9abe3b8..7d01219 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -18,8 +18,8 @@ use crate::{ error_raporting::ModuleMap, lexer::{FullToken, Position}, mir::{ - self, Metadata, NamedVariableRef, StructField, StructType, TypeDefinition, - TypeDefinitionKind, TypeKind, VagueLiteral, + self, implement::TypeCategory, Metadata, NamedVariableRef, StructField, StructType, + TypeDefinition, TypeDefinitionKind, TypeKind, VagueLiteral, }, }; @@ -247,7 +247,7 @@ impl mir::Module { insert_debug!(&TypeKind::I64); insert_debug!(&TypeKind::I128); insert_debug!(&TypeKind::Void); - insert_debug!(&TypeKind::Str); + insert_debug!(&TypeKind::Char); for typedef in &self.typedefs { let type_value = match &typedef.kind { @@ -633,7 +633,11 @@ impl mir::Expression { .expect("rhs has no return value") .instr(); let lhs_type = lhs_exp.return_type(&Default::default()).unwrap().1; - let instr = match (binop, lhs_type.signed(), lhs_type.is_float()) { + let instr = match ( + binop, + lhs_type.signed(), + lhs_type.category() == TypeCategory::Real, + ) { (mir::BinaryOperator::Add, _, false) => Instr::Add(lhs, rhs), (mir::BinaryOperator::Add, _, true) => Instr::FAdd(lhs, rhs), (mir::BinaryOperator::Minus, _, false) => Instr::Sub(lhs, rhs), @@ -643,7 +647,6 @@ impl mir::Expression { (mir::BinaryOperator::And, _, _) => Instr::And(lhs, rhs), (mir::BinaryOperator::Cmp(i), _, false) => Instr::ICmp(i.predicate(), lhs, rhs), (mir::BinaryOperator::Cmp(i), _, true) => Instr::FCmp(i.predicate(), lhs, rhs), - _ => todo!(), }; Some(StackValue( StackValueKind::Immutable(scope.block.build(instr).unwrap()), @@ -1228,7 +1231,7 @@ impl TypeKind { TypeKind::F128 => Type::F128, TypeKind::F80 => Type::F80, TypeKind::F128PPC => Type::F128PPC, - TypeKind::Str => Type::I8, + TypeKind::Char => Type::I8, TypeKind::Array(elem_t, len) => { Type::Array(Box::new(elem_t.get_type(type_vals, typedefs)), *len) } @@ -1368,7 +1371,7 @@ impl TypeKind { | TypeKind::F128 | TypeKind::F128PPC => DwarfEncoding::Float, TypeKind::Void => DwarfEncoding::Address, - TypeKind::Str => DwarfEncoding::UnsignedChar, + TypeKind::Char => DwarfEncoding::UnsignedChar, TypeKind::Array(_, _) => DwarfEncoding::Address, TypeKind::CustomType(_) => DwarfEncoding::Address, _ => panic!("tried fetching debug-type for non-supported type!"), diff --git a/reid/src/mir/fmt.rs b/reid/src/mir/fmt.rs index c0d2bf5..74267da 100644 --- a/reid/src/mir/fmt.rs +++ b/reid/src/mir/fmt.rs @@ -337,7 +337,7 @@ impl Display for TypeKind { TypeKind::U64 => write!(f, "u64"), TypeKind::U128 => write!(f, "u128"), TypeKind::Void => write!(f, "void"), - TypeKind::Str => write!(f, "str"), + TypeKind::Char => write!(f, "char"), TypeKind::Array(type_kind, len) => { f.write_char('[')?; Display::fmt(type_kind, f)?; diff --git a/reid/src/mir/implement.rs b/reid/src/mir/implement.rs index 5b758b9..c4556b4 100644 --- a/reid/src/mir/implement.rs +++ b/reid/src/mir/implement.rs @@ -79,8 +79,8 @@ impl TypeKind { let other_cat = other.category(); match (self, other) { (TypeKind::UserPtr(_), TypeKind::UserPtr(_)) => Ok(other.clone()), - (TypeKind::Str, TypeKind::U8) => Ok(other.clone()), - (TypeKind::U8, TypeKind::Str) => Ok(other.clone()), + (TypeKind::Char, TypeKind::U8) => Ok(other.clone()), + (TypeKind::U8, TypeKind::Char) => Ok(other.clone()), _ => match (&self_cat, &other_cat) { (TypeCategory::Integer, TypeCategory::Integer) => Ok(other.clone()), (TypeCategory::Integer, TypeCategory::Real) => Ok(other.clone()), @@ -121,38 +121,7 @@ impl TypeKind { TypeKind::U64 => false, TypeKind::U128 => false, TypeKind::Void => false, - TypeKind::Str => false, - TypeKind::Array(_, _) => false, - TypeKind::CustomType(_) => false, - TypeKind::CodegenPtr(_) => false, - TypeKind::Vague(_) => false, - TypeKind::Borrow(_, _) => false, - TypeKind::UserPtr(_) => false, - TypeKind::F16 => true, - TypeKind::F32B => true, - TypeKind::F32 => true, - TypeKind::F64 => true, - TypeKind::F128 => true, - TypeKind::F80 => true, - TypeKind::F128PPC => true, - } - } - - pub fn is_float(&self) -> bool { - match self { - TypeKind::Bool => false, - TypeKind::I8 => false, - TypeKind::I16 => false, - TypeKind::I32 => false, - TypeKind::I64 => false, - TypeKind::I128 => false, - TypeKind::U8 => false, - TypeKind::U16 => false, - TypeKind::U32 => false, - TypeKind::U64 => false, - TypeKind::U128 => false, - TypeKind::Void => false, - TypeKind::Str => false, + TypeKind::Char => false, TypeKind::Array(_, _) => false, TypeKind::CustomType(_) => false, TypeKind::CodegenPtr(_) => false, @@ -183,7 +152,7 @@ impl TypeKind { TypeKind::I128 => 128, TypeKind::U128 => 128, TypeKind::Void => 0, - TypeKind::Str => 8, + TypeKind::Char => 8, TypeKind::Array(type_kind, len) => type_kind.size_of() * len, TypeKind::CustomType(_) => 32, TypeKind::CodegenPtr(_) => 64, @@ -214,7 +183,7 @@ impl TypeKind { TypeKind::I128 => 128, TypeKind::U128 => 128, TypeKind::Void => 0, - TypeKind::Str => 8, + TypeKind::Char => 8, TypeKind::Array(type_kind, _) => type_kind.alignment(), TypeKind::CustomType(_) => 32, TypeKind::CodegenPtr(_) => 64, @@ -250,7 +219,7 @@ impl TypeKind { | TypeKind::U32 | TypeKind::U64 | TypeKind::U128 - | TypeKind::Str => TypeCategory::Integer, + | TypeKind::Char => TypeCategory::Integer, TypeKind::F16 | TypeKind::F32B | TypeKind::F32 @@ -275,6 +244,7 @@ impl TypeKind { } } +#[derive(PartialEq, Eq, PartialOrd, Ord)] pub enum TypeCategory { Integer, Real, diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index b97a82f..e4e152b 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -98,7 +98,7 @@ pub enum TypeKind { F128, F80, F128PPC, - Str, + Char, Array(Box, u64), CustomType(String), Borrow(Box, bool), @@ -186,7 +186,7 @@ impl Literal { Literal::U64(_) => TypeKind::U64, Literal::U128(_) => TypeKind::U128, Literal::Bool(_) => TypeKind::Bool, - Literal::String(_) => TypeKind::UserPtr(Box::new(TypeKind::Str)), + Literal::String(_) => TypeKind::UserPtr(Box::new(TypeKind::Char)), Literal::Vague(VagueLiteral::Number(_)) => TypeKind::Vague(VagueType::Integer), Literal::Vague(VagueLiteral::Decimal(_)) => TypeKind::Vague(VagueType::Decimal), Literal::F16(_) => TypeKind::F16, @@ -196,7 +196,7 @@ impl Literal { Literal::F80(_) => TypeKind::F80, Literal::F128(_) => TypeKind::F128, Literal::F128PPC(_) => TypeKind::F128PPC, - Literal::Char(_) => todo!(), + Literal::Char(_) => TypeKind::Char, } } } diff --git a/reid/src/mir/typecheck.rs b/reid/src/mir/typecheck.rs index 7978b8e..811bbe5 100644 --- a/reid/src/mir/typecheck.rs +++ b/reid/src/mir/typecheck.rs @@ -728,29 +728,12 @@ impl Literal { if let Some(hint) = &hint { use Literal as L; use VagueLiteral as VagueL; + + if *hint == self.as_type() { + return Ok(self); + } + Ok(match (self.clone(), hint) { - (L::I8(_), TypeKind::I8) => self, - (L::I16(_), TypeKind::I16) => self, - (L::I32(_), TypeKind::I32) => self, - (L::I64(_), TypeKind::I64) => self, - (L::I128(_), TypeKind::I128) => self, - (L::U8(_), TypeKind::U8) => self, - (L::U16(_), TypeKind::U16) => self, - (L::U32(_), TypeKind::U32) => self, - (L::U64(_), TypeKind::U64) => self, - (L::U128(_), TypeKind::U128) => self, - (L::F16(_), TypeKind::F16) => self, - (L::F32(_), TypeKind::F32) => self, - (L::F32B(_), TypeKind::F32B) => self, - (L::F64(_), TypeKind::F64) => self, - (L::F80(_), TypeKind::F80) => self, - (L::F128(_), TypeKind::F128) => self, - (L::F128PPC(_), TypeKind::F128PPC) => self, - (L::Bool(_), TypeKind::Bool) => self, - (L::String(_), TypeKind::UserPtr(ptr)) => match *ptr.clone() { - TypeKind::Str => self, - _ => Err(ErrorKind::LiteralIncompatible(self, hint.clone()))?, - }, // TODO make sure that v is actually able to fit in the // requested type (L::Vague(VagueL::Number(v)), TypeKind::I8) => L::I8(v as i8),