From d14c5cd0b345dfffb0b089d49e489920c773b177 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Thu, 2 Jun 2016 21:32:07 -0400 Subject: [PATCH] [MIR] Handle call return values that need to be casted properly. --- src/librustc_trans/mir/block.rs | 44 ++++++++++++++++++++++++++-- src/test/run-pass/mir_cast_fn_ret.rs | 24 +++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 src/test/run-pass/mir_cast_fn_ret.rs diff --git a/src/librustc_trans/mir/block.rs b/src/librustc_trans/mir/block.rs index 4e3386bc73677..20e802f28a6bf 100644 --- a/src/librustc_trans/mir/block.rs +++ b/src/librustc_trans/mir/block.rs @@ -16,10 +16,10 @@ use adt; use base; use build; use callee::{Callee, CalleeData, Fn, Intrinsic, NamedTupleConstructor, Virtual}; -use common::{self, type_is_fat_ptr, Block, BlockAndBuilder, C_undef}; +use common::{self, type_is_fat_ptr, Block, BlockAndBuilder, C_uint, C_undef}; use debuginfo::DebugLoc; use Disr; -use machine::{llalign_of_min, llbitsize_of_real}; +use machine::{llalign_of_min, llbitsize_of_real, llsize_of_store}; use meth; use type_of; use glue; @@ -32,6 +32,8 @@ use super::lvalue::{LvalueRef, load_fat_ptr}; use super::operand::OperandRef; use super::operand::OperandValue::{self, FatPtr, Immediate, Ref}; +use std::cmp; + impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> { pub fn trans_block(&mut self, bb: mir::BasicBlock) { debug!("trans_block({:?})", bb); @@ -685,7 +687,43 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> { match dest { Nothing => (), - Store(dst) => ret_ty.store(bcx, op.immediate(), dst), + Store(dst) => { + if let Some(llcast_ty) = ret_ty.cast { + let ccx = bcx.ccx(); + // The actual return type is a struct, but the ABI + // adaptation code has cast it into some scalar type. The + // code that follows is the only reliable way I have + // found to do a transform like i64 -> {i32,i32}. + // Basically we dump the data onto the stack then memcpy it. + // + // Other approaches I tried: + // - Casting rust ret pointer to the foreign type and using Store + // is (a) unsafe if size of foreign type > size of rust type and + // (b) runs afoul of strict aliasing rules, yielding invalid + // assembly under -O (specifically, the store gets removed). + // - Truncating foreign type to correct integral type and then + // bitcasting to the struct type yields invalid cast errors. + + // We instead thus allocate some scratch space... + let llscratch = bcx.alloca(llcast_ty, "fn_ret_cast"); + bcx.with_block(|bcx| base::call_lifetime_start(bcx, llscratch)); + + // ...where we first store the value... + bcx.store(op.immediate(), llscratch); + + // ...and then memcpy it to the intended destination. + base::call_memcpy(bcx, + bcx.pointercast(dst, Type::i8p(ccx)), + bcx.pointercast(llscratch, Type::i8p(ccx)), + C_uint(ccx, llsize_of_store(ccx, ret_ty.original_ty)), + cmp::min(llalign_of_min(ccx, ret_ty.original_ty), + llalign_of_min(ccx, llcast_ty)) as u32); + + bcx.with_block(|bcx| base::call_lifetime_end(bcx, llscratch)); + } else { + ret_ty.store(bcx, op.immediate(), dst); + } + } IndirectOperand(tmp, idx) => { let op = self.trans_load(bcx, tmp, op.ty); self.temps[idx as usize] = TempRef::Operand(Some(op)); diff --git a/src/test/run-pass/mir_cast_fn_ret.rs b/src/test/run-pass/mir_cast_fn_ret.rs new file mode 100644 index 0000000000000..5bdc14f659cd5 --- /dev/null +++ b/src/test/run-pass/mir_cast_fn_ret.rs @@ -0,0 +1,24 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(rustc_attrs)] + +pub extern "C" fn foo() -> (u8, u8, u8) { + (1, 2, 3) +} + +#[rustc_mir] +pub fn bar() -> u8 { + foo().2 +} + +fn main() { + assert_eq!(bar(), 3); +}