diff --git a/reid/lib/std.reid b/reid/lib/std.reid index d5be29b..08ab852 100644 --- a/reid/lib/std.reid +++ b/reid/lib/std.reid @@ -2,8 +2,8 @@ extern fn puts(message: string) -> i32; struct DivT { - quot: i32, - rem: i32, + quotient: i32, + remainder: i32, } extern fn div(numerator: i32, denominator: i32) -> DivT; diff --git a/reid/src/ast/mod.rs b/reid/src/ast/mod.rs index e8c8aee..51a8e97 100644 --- a/reid/src/ast/mod.rs +++ b/reid/src/ast/mod.rs @@ -89,11 +89,7 @@ impl BinaryOperator { } #[derive(Debug, Clone)] -pub struct FunctionCallExpression( - pub String, - pub Vec, - #[allow(dead_code)] pub TokenRange, -); +pub struct FunctionCallExpression(pub String, pub Vec, pub TokenRange); #[derive(Debug, Clone)] pub struct IfExpression( diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index c83789d..41a7f34 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -188,6 +188,7 @@ impl ast::Expression { .iter() .map(|e| e.process(module_id)) .collect(), + meta: fn_call_expr.2.as_meta(module_id), }) } ast::ExpressionKind::BlockExpr(block) => { diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index 836a9d9..de7394e 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -648,11 +648,13 @@ impl mir::Expression { )) } mir::ExprKind::FunctionCall(call) => { - let ret_type = call + let ret_type_kind = call .return_type .known() .expect("function return type unknown"); + let ret_type = ret_type_kind.get_type(scope.type_values, scope.types); + let params = call .parameters .iter() @@ -663,18 +665,56 @@ impl mir::Expression { .get(&call.name) .expect("function not found!"); dbg!(&self, &callee.ir.value()); - Some(StackValue( - StackValueKind::Immutable( - scope - .block - .build( - call.name.clone(), - Instr::FunctionCall(callee.ir.value(), params), - ) - .unwrap(), - ), - ret_type, - )) + + let val = scope + .block + .build( + call.name.clone(), + Instr::FunctionCall(callee.ir.value(), params), + ) + .unwrap(); + + if let Some(debug) = &scope.debug { + let location = call.meta.into_debug(scope.tokens).unwrap(); + let location_val = debug.info.location(&debug.scope, location); + val.with_location(&mut scope.block, location_val); + } + + let ptr = if ret_type_kind != TypeKind::Void { + let ptr = scope + .block + .build(&call.name, Instr::Alloca(ret_type.clone())) + .unwrap(); + scope + .block + .build(format!("{}.store", call.name), Instr::Store(ptr, val)) + .unwrap(); + + Some(ptr) + } else { + None + }; + + if let Some(ptr) = ptr { + if state.should_load { + Some(StackValue( + StackValueKind::Immutable( + scope + .block + .build(call.name.clone(), Instr::Load(ptr, ret_type)) + .unwrap(), + ), + ret_type_kind, + )) + } else { + Some(StackValue( + StackValueKind::Immutable(ptr), + TypeKind::Ptr(Box::new(ret_type_kind)), + )) + } + } else { + None + } } mir::ExprKind::If(if_expression) => if_expression.codegen(scope, state), mir::ExprKind::Block(block) => { diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index b69b6b3..41694fb 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -259,6 +259,7 @@ pub struct FunctionCall { pub name: String, pub return_type: TypeKind, pub parameters: Vec, + pub meta: Metadata, } #[derive(Debug)] diff --git a/reid_src/std_test.reid b/reid_src/std_test.reid index 23c04d3..334ac09 100644 --- a/reid_src/std_test.reid +++ b/reid_src/std_test.reid @@ -5,8 +5,7 @@ import std::intdiv; fn main() -> i32 { let hello = "hello world"; - print(hello); - return intdiv(10, 5).quot; + return intdiv(15, 5).quotient; }