diff --git a/examples/macro_easy.reid b/examples/macro_easy.reid index 7026732..409d1f1 100644 --- a/examples/macro_easy.reid +++ b/examples/macro_easy.reid @@ -1,5 +1,11 @@ +import std::String; +import std::print; + 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"); - return bytes[0]; + print(String::new() + bytes.length()); + return *bytes[0]; } diff --git a/reid/src/codegen/intrinsics.rs b/reid/src/codegen/intrinsics.rs index d0cb64f..342bc24 100644 --- a/reid/src/codegen/intrinsics.rs +++ b/reid/src/codegen/intrinsics.rs @@ -58,6 +58,28 @@ pub fn form_intrinsics() -> Vec { } pub fn get_intrinsic_assoc_func(ty: &TypeKind, name: &str) -> Option { + 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 { "sizeof" => Some(FunctionDefinition { 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 { + let zero = scope.block.build(Instr::Constant(ConstValueKind::U64(self.0))).unwrap(); + Ok(StackValue(StackValueKind::Literal(zero), TypeKind::U64)) + } +} // impl IntrinsicFunction for IntrinsicIAdd { // fn codegen<'ctx, 'a>( diff --git a/reid/src/codegen/mod.rs b/reid/src/codegen/mod.rs index 41bdf76..173b663 100644 --- a/reid/src/codegen/mod.rs +++ b/reid/src/codegen/mod.rs @@ -1360,7 +1360,7 @@ impl mir::Expression { let global_value = scope.globals.get(global_name).unwrap(); let a = Some(StackValue( StackValueKind::Literal(scope.block.build(Instr::GetGlobal(global_value.clone())).unwrap()), - TypeKind::UserPtr(Box::new(ty.clone())), + ty.clone(), )); a } diff --git a/reid/src/mir/implement.rs b/reid/src/mir/implement.rs index fd24349..bcb8c43 100644 --- a/reid/src/mir/implement.rs +++ b/reid/src/mir/implement.rs @@ -429,7 +429,7 @@ impl Expression { Err(_) => Ok((ReturnKind::Soft, type_kind.clone())), }, 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())), } } diff --git a/reid/src/mir/macros.rs b/reid/src/mir/macros.rs index 869d517..49e7a28 100644 --- a/reid/src/mir/macros.rs +++ b/reid/src/mir/macros.rs @@ -222,7 +222,10 @@ impl MacroFunction for TestMacro { Ok(( 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), + ), )) } }