diff --git a/reid/lib/std.reid b/reid/lib/std.reid index f55b9f9..d53a99e 100644 --- a/reid/lib/std.reid +++ b/reid/lib/std.reid @@ -1,13 +1,13 @@ extern fn puts(message: *str) -> i32; +extern fn malloc(size: u64) -> *u8; +extern fn div(numerator: i32, denominator: i32) -> div_t; struct div_t { quotient: i32, remainder: i32, } -extern fn div(numerator: i32, denominator: i32) -> div_t; - pub fn print(message: *str) { puts(message); } @@ -16,6 +16,10 @@ pub fn int_div(numerator: i32, denominator: i32) -> div_t { return div(numerator, denominator); } +pub fn allocate(size: u64) -> *u8 { + malloc(size) +} + fn main() -> u16 { return 0; } diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index d7cb604..0c6a9b2 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -1030,18 +1030,22 @@ impl mir::Expression { } mir::ExprKind::CastTo(expression, type_kind) => { let val = expression.codegen(scope, state)?; - let cast_instr = val - .1 - .get_type(scope.type_values, scope.types) - .cast_instruction( - val.instr(), - &type_kind.get_type(scope.type_values, scope.types), - ) - .unwrap(); - Some(StackValue( - val.0.derive(scope.block.build(cast_instr).unwrap()), - type_kind.clone(), - )) + let instr = + if let (TypeKind::UserPtr(_), TypeKind::UserPtr(_)) = (&val.1, type_kind) { + val.0.instr() + } else { + let cast_instr = val + .1 + .get_type(scope.type_values, scope.types) + .cast_instruction( + val.instr(), + &type_kind.get_type(scope.type_values, scope.types), + ) + .unwrap(); + scope.block.build(cast_instr).unwrap() + }; + + Some(StackValue(val.0.derive(instr), type_kind.clone())) } }; if let Some(value) = &value { diff --git a/reid_src/cast.reid b/reid_src/cast.reid index 816575b..120d0e5 100644 --- a/reid_src/cast.reid +++ b/reid_src/cast.reid @@ -1,5 +1,7 @@ // Arithmetic, function calls and imports! +import std::allocate; + fn other() -> i16 { return 6; } @@ -7,5 +9,7 @@ fn other() -> i16 { fn main() -> u32 { let value = other() as u32; - return value; + let v = (allocate(4) as *u32); + + return v[0]; } diff --git a/reid_src/ptr.reid b/reid_src/ptr.reid index 1b4536f..d694349 100644 --- a/reid_src/ptr.reid +++ b/reid_src/ptr.reid @@ -1,8 +1,6 @@ // Arithmetic, function calls and imports! -extern fn malloc(size: u64) -> *u8; - fn main() -> u8 { let mut ptr = malloc(4);