Add length-intrinsic

This commit is contained in:
Sofia 2025-07-29 01:15:09 +03:00
parent f700c577f1
commit baa7bafafc
5 changed files with 44 additions and 5 deletions

View File

@ -1,5 +1,11 @@
import std::String;
import std::print;
fn main() -> u8 { fn main() -> u8 {
// let message = String::from(include_bytes!("./macro_easy_file.txt")); // - TODO make a type of global identifier for globls?
// - TODO figure out if it is possible to map the path to the path of the
// module and not the PWD.
let bytes = test_macro!("./examples/macro_easy_file.txt"); let bytes = test_macro!("./examples/macro_easy_file.txt");
return bytes[0]; print(String::new() + bytes.length());
return *bytes[0];
} }

View File

@ -58,6 +58,28 @@ pub fn form_intrinsics() -> Vec<FunctionDefinition> {
} }
pub fn get_intrinsic_assoc_func(ty: &TypeKind, name: &str) -> Option<FunctionDefinition> { pub fn get_intrinsic_assoc_func(ty: &TypeKind, name: &str) -> Option<FunctionDefinition> {
if let TypeKind::Array(_, len) = ty {
match name {
"length" => {
return Some(FunctionDefinition {
name: "length".to_owned(),
linkage_name: None,
is_pub: true,
is_imported: false,
return_type: TypeKind::U64,
parameters: vec![FunctionParam {
name: String::from("self"),
ty: TypeKind::Borrow(Box::new(ty.clone()), false),
meta: Default::default(),
}],
kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicConst(*len))),
source: None,
})
}
_ => {}
}
}
match name { match name {
"sizeof" => Some(FunctionDefinition { "sizeof" => Some(FunctionDefinition {
name: "sizeof".to_owned(), name: "sizeof".to_owned(),
@ -408,6 +430,14 @@ impl IntrinsicFunction for IntrinsicNullPtr {
)) ))
} }
} }
#[derive(Clone, Debug)]
pub struct IntrinsicConst(u64);
impl IntrinsicFunction for IntrinsicConst {
fn codegen<'ctx, 'a>(&self, scope: &mut Scope<'ctx, 'a>, _: &[StackValue]) -> Result<StackValue, ErrorKind> {
let zero = scope.block.build(Instr::Constant(ConstValueKind::U64(self.0))).unwrap();
Ok(StackValue(StackValueKind::Literal(zero), TypeKind::U64))
}
}
// impl IntrinsicFunction for IntrinsicIAdd { // impl IntrinsicFunction for IntrinsicIAdd {
// fn codegen<'ctx, 'a>( // fn codegen<'ctx, 'a>(

View File

@ -1360,7 +1360,7 @@ impl mir::Expression {
let global_value = scope.globals.get(global_name).unwrap(); let global_value = scope.globals.get(global_name).unwrap();
let a = Some(StackValue( let a = Some(StackValue(
StackValueKind::Literal(scope.block.build(Instr::GetGlobal(global_value.clone())).unwrap()), StackValueKind::Literal(scope.block.build(Instr::GetGlobal(global_value.clone())).unwrap()),
TypeKind::UserPtr(Box::new(ty.clone())), ty.clone(),
)); ));
a a
} }

View File

@ -429,7 +429,7 @@ impl Expression {
Err(_) => Ok((ReturnKind::Soft, type_kind.clone())), Err(_) => Ok((ReturnKind::Soft, type_kind.clone())),
}, },
AssociatedFunctionCall(_, fcall) => fcall.return_type(), AssociatedFunctionCall(_, fcall) => fcall.return_type(),
GlobalRef(_, type_kind) => Ok((ReturnKind::Soft, TypeKind::UserPtr(Box::new(type_kind.clone())))), GlobalRef(_, type_kind) => Ok((ReturnKind::Soft, type_kind.clone())),
} }
} }

View File

@ -222,7 +222,10 @@ impl MacroFunction for TestMacro {
Ok(( Ok((
vec![global.clone()], vec![global.clone()],
mir::ExprKind::GlobalRef(global_name, TypeKind::U8), mir::ExprKind::GlobalRef(
global_name,
TypeKind::Borrow(Box::new(TypeKind::Array(Box::new(TypeKind::U8), len as u64)), false),
),
)) ))
} }
} }