From 0cd70c649a631219422fbd097649820a69e6a86e Mon Sep 17 00:00:00 2001 From: Sergey Pepyakin Date: Tue, 11 Dec 2018 20:13:20 +0100 Subject: [PATCH] Implement returns. --- src/backend.rs | 11 ++++++++++- src/function_body.rs | 6 ++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/backend.rs b/src/backend.rs index 3e90722c00..7829b7bf1f 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -361,11 +361,20 @@ pub fn pass_outgoing_args(ctx: &mut Context, arity: u32) { } } -pub fn call_direct(ctx: &mut Context, index: u32) { +pub fn call_direct(ctx: &mut Context, index: u32, return_arity: u32) { + assert!(return_arity == 0 || return_arity == 1); + let label = &ctx.func_starts[index as usize].1; dynasm!(ctx.asm ; call =>*label ); + + if return_arity == 1 { + dynasm!(ctx.asm + ; push rax + ); + ctx.sp_depth.reserve(1); + } } pub fn prologue(ctx: &mut Context, stack_slots: u32) { diff --git a/src/function_body.rs b/src/function_body.rs index fca2a7e666..0983973f9f 100644 --- a/src/function_body.rs +++ b/src/function_body.rs @@ -222,13 +222,11 @@ pub fn translate( } Operator::Call { function_index } => { let callee_ty = translation_ctx.func_type(function_index); - assert!(callee_ty.returns.len() == 0, "is not supported"); - // TODO: ensure that this function is locally defined - // We would like to support imported functions at some point + // TODO: this implementation assumes that this function is locally defined. pass_outgoing_args(&mut ctx, callee_ty.params.len() as u32); - call_direct(&mut ctx, function_index); + call_direct(&mut ctx, function_index, callee_ty.returns.len() as u32); } _ => { trap(&mut ctx);