Skip to content

[MIR] Handle call return values that need to be casted properly. #34054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions src/librustc_trans/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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));
Expand Down
24 changes: 24 additions & 0 deletions src/test/run-pass/mir_cast_fn_ret.rs
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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);
}