Codegen & compile char, change *str to *char

This commit is contained in:
Sofia 2025-07-22 15:07:33 +03:00
parent 108cf6efa5
commit 1224c612c7
9 changed files with 29 additions and 76 deletions

View File

@ -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);
}

View File

@ -31,7 +31,6 @@ pub enum TypeKind {
F128,
F80,
F128PPC,
Str,
Char,
Array(Box<TypeKind>, u64),
Custom(String),

View File

@ -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),
}

View File

@ -311,7 +311,6 @@ impl From<ast::TypeKind> 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<ast::TypeKind> 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,
}
}
}

View File

@ -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!"),

View File

@ -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)?;

View File

@ -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,

View File

@ -98,7 +98,7 @@ pub enum TypeKind {
F128,
F80,
F128PPC,
Str,
Char,
Array(Box<TypeKind>, u64),
CustomType(String),
Borrow(Box<TypeKind>, 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,
}
}
}

View File

@ -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),