2019-02-17 12:58:58 -06:00
|
|
|
use crate::builder::Builder;
|
|
|
|
use crate::type_::Type;
|
|
|
|
use crate::type_of::LayoutLlvmExt;
|
|
|
|
use crate::value::Value;
|
2020-03-29 09:41:09 -05:00
|
|
|
use rustc_middle::ty::layout::{Align, HasDataLayout, HasTyCtxt, LayoutOf, Size};
|
|
|
|
use rustc_middle::ty::Ty;
|
2019-12-22 16:42:04 -06:00
|
|
|
use rustc_codegen_ssa::mir::operand::OperandRef;
|
|
|
|
use rustc_codegen_ssa::traits::{
|
|
|
|
BaseTypeMethods, BuilderMethods, ConstMethods, DerivedTypeMethods,
|
|
|
|
};
|
2018-10-23 18:13:33 -05:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn round_pointer_up_to_alignment(
|
|
|
|
bx: &mut Builder<'a, 'll, 'tcx>,
|
|
|
|
addr: &'ll Value,
|
|
|
|
align: Align,
|
2019-12-22 16:42:04 -06:00
|
|
|
ptr_ty: &'ll Type,
|
2018-10-23 18:13:33 -05:00
|
|
|
) -> &'ll Value {
|
|
|
|
let mut ptr_as_int = bx.ptrtoint(addr, bx.cx().type_isize());
|
|
|
|
ptr_as_int = bx.add(ptr_as_int, bx.cx().const_i32(align.bytes() as i32 - 1));
|
|
|
|
ptr_as_int = bx.and(ptr_as_int, bx.cx().const_i32(-(align.bytes() as i32)));
|
|
|
|
bx.inttoptr(ptr_as_int, ptr_ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_direct_ptr_va_arg(
|
|
|
|
bx: &mut Builder<'a, 'll, 'tcx>,
|
|
|
|
list: OperandRef<'tcx, &'ll Value>,
|
|
|
|
llty: &'ll Type,
|
|
|
|
size: Size,
|
|
|
|
align: Align,
|
|
|
|
slot_size: Align,
|
2019-12-22 16:42:04 -06:00
|
|
|
allow_higher_align: bool,
|
2018-10-23 18:13:33 -05:00
|
|
|
) -> (&'ll Value, Align) {
|
|
|
|
let va_list_ptr_ty = bx.cx().type_ptr_to(bx.cx.type_i8p());
|
|
|
|
let va_list_addr = if list.layout.llvm_type(bx.cx) != va_list_ptr_ty {
|
|
|
|
bx.bitcast(list.immediate(), va_list_ptr_ty)
|
|
|
|
} else {
|
|
|
|
list.immediate()
|
|
|
|
};
|
|
|
|
|
|
|
|
let ptr = bx.load(va_list_addr, bx.tcx().data_layout.pointer_align.abi);
|
|
|
|
|
|
|
|
let (addr, addr_align) = if allow_higher_align && align > slot_size {
|
|
|
|
(round_pointer_up_to_alignment(bx, ptr, align, bx.cx().type_i8p()), align)
|
|
|
|
} else {
|
|
|
|
(ptr, slot_size)
|
|
|
|
};
|
|
|
|
|
|
|
|
let aligned_size = size.align_to(slot_size).bytes() as i32;
|
|
|
|
let full_direct_size = bx.cx().const_i32(aligned_size);
|
|
|
|
let next = bx.inbounds_gep(addr, &[full_direct_size]);
|
|
|
|
bx.store(next, va_list_addr, bx.tcx().data_layout.pointer_align.abi);
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
if size.bytes() < slot_size.bytes() && &*bx.tcx().sess.target.target.target_endian == "big" {
|
2018-10-23 18:13:33 -05:00
|
|
|
let adjusted_size = bx.cx().const_i32((slot_size.bytes() - size.bytes()) as i32);
|
|
|
|
let adjusted = bx.inbounds_gep(addr, &[adjusted_size]);
|
|
|
|
(bx.bitcast(adjusted, bx.cx().type_ptr_to(llty)), addr_align)
|
|
|
|
} else {
|
|
|
|
(bx.bitcast(addr, bx.cx().type_ptr_to(llty)), addr_align)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_ptr_va_arg(
|
|
|
|
bx: &mut Builder<'a, 'll, 'tcx>,
|
|
|
|
list: OperandRef<'tcx, &'ll Value>,
|
|
|
|
target_ty: Ty<'tcx>,
|
|
|
|
indirect: bool,
|
|
|
|
slot_size: Align,
|
2019-12-22 16:42:04 -06:00
|
|
|
allow_higher_align: bool,
|
2018-10-23 18:13:33 -05:00
|
|
|
) -> &'ll Value {
|
|
|
|
let layout = bx.cx.layout_of(target_ty);
|
|
|
|
let (llty, size, align) = if indirect {
|
2019-12-22 16:42:04 -06:00
|
|
|
(
|
|
|
|
bx.cx.layout_of(bx.cx.tcx.mk_imm_ptr(target_ty)).llvm_type(bx.cx),
|
|
|
|
bx.cx.data_layout().pointer_size,
|
|
|
|
bx.cx.data_layout().pointer_align,
|
|
|
|
)
|
2018-10-23 18:13:33 -05:00
|
|
|
} else {
|
2019-12-22 16:42:04 -06:00
|
|
|
(layout.llvm_type(bx.cx), layout.size, layout.align)
|
2018-10-23 18:13:33 -05:00
|
|
|
};
|
2019-12-22 16:42:04 -06:00
|
|
|
let (addr, addr_align) =
|
|
|
|
emit_direct_ptr_va_arg(bx, list, llty, size, align.abi, slot_size, allow_higher_align);
|
2018-10-23 18:13:33 -05:00
|
|
|
if indirect {
|
|
|
|
let tmp_ret = bx.load(addr, addr_align);
|
|
|
|
bx.load(tmp_ret, align.abi)
|
|
|
|
} else {
|
|
|
|
bx.load(addr, addr_align)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn emit_va_arg(
|
|
|
|
bx: &mut Builder<'a, 'll, 'tcx>,
|
|
|
|
addr: OperandRef<'tcx, &'ll Value>,
|
|
|
|
target_ty: Ty<'tcx>,
|
|
|
|
) -> &'ll Value {
|
|
|
|
// Determine the va_arg implementation to use. The LLVM va_arg instruction
|
|
|
|
// is lacking in some instances, so we should only use it as a fallback.
|
2018-12-04 20:44:08 -06:00
|
|
|
let target = &bx.cx.tcx.sess.target.target;
|
2018-10-23 18:13:33 -05:00
|
|
|
let arch = &bx.cx.tcx.sess.target.target.arch;
|
2018-12-04 20:44:08 -06:00
|
|
|
match (&**arch, target.options.is_like_windows) {
|
|
|
|
// Windows x86
|
2018-10-23 18:13:33 -05:00
|
|
|
("x86", true) => {
|
2019-12-22 16:42:04 -06:00
|
|
|
emit_ptr_va_arg(bx, addr, target_ty, false, Align::from_bytes(4).unwrap(), false)
|
2018-10-23 18:13:33 -05:00
|
|
|
}
|
2018-12-04 20:44:08 -06:00
|
|
|
// Generic x86
|
|
|
|
("x86", _) => {
|
2019-12-22 16:42:04 -06:00
|
|
|
emit_ptr_va_arg(bx, addr, target_ty, false, Align::from_bytes(4).unwrap(), true)
|
2018-12-04 20:44:08 -06:00
|
|
|
}
|
2019-01-20 05:47:29 -06:00
|
|
|
// Windows AArch64
|
2018-11-30 09:53:44 -06:00
|
|
|
("aarch64", true) => {
|
2019-12-22 16:42:04 -06:00
|
|
|
emit_ptr_va_arg(bx, addr, target_ty, false, Align::from_bytes(8).unwrap(), false)
|
2018-12-04 20:44:08 -06:00
|
|
|
}
|
2019-01-20 05:47:29 -06:00
|
|
|
// iOS AArch64
|
2018-11-30 09:53:44 -06:00
|
|
|
("aarch64", _) if target.target_os == "ios" => {
|
2019-12-22 16:42:04 -06:00
|
|
|
emit_ptr_va_arg(bx, addr, target_ty, false, Align::from_bytes(8).unwrap(), true)
|
2018-12-04 20:44:08 -06:00
|
|
|
}
|
|
|
|
// Windows x86_64
|
2018-10-23 18:13:33 -05:00
|
|
|
("x86_64", true) => {
|
|
|
|
let target_ty_size = bx.cx.size_of(target_ty).bytes();
|
2020-03-02 19:07:15 -06:00
|
|
|
let indirect: bool = target_ty_size > 8 || !target_ty_size.is_power_of_two();
|
2019-12-22 16:42:04 -06:00
|
|
|
emit_ptr_va_arg(bx, addr, target_ty, indirect, Align::from_bytes(8).unwrap(), false)
|
2018-10-23 18:13:33 -05:00
|
|
|
}
|
2018-12-04 20:44:08 -06:00
|
|
|
// For all other architecture/OS combinations fall back to using
|
|
|
|
// the LLVM va_arg instruction.
|
|
|
|
// https://llvm.org/docs/LangRef.html#va-arg-instruction
|
2019-12-22 16:42:04 -06:00
|
|
|
_ => bx.va_arg(addr.immediate(), bx.cx.layout_of(target_ty).llvm_type(bx.cx)),
|
2018-10-23 18:13:33 -05:00
|
|
|
}
|
|
|
|
}
|