]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/va_arg.rs
Auto merge of #80182 - in42:stack_trace, r=tmandry
[rust.git] / compiler / rustc_codegen_llvm / src / va_arg.rs
1 use crate::builder::Builder;
2 use crate::type_::Type;
3 use crate::type_of::LayoutLlvmExt;
4 use crate::value::Value;
5 use rustc_codegen_ssa::mir::operand::OperandRef;
6 use rustc_codegen_ssa::{
7     common::IntPredicate,
8     traits::{BaseTypeMethods, BuilderMethods, ConstMethods, DerivedTypeMethods},
9 };
10 use rustc_middle::ty::layout::HasTyCtxt;
11 use rustc_middle::ty::Ty;
12 use rustc_target::abi::{Align, Endian, HasDataLayout, LayoutOf, Size};
13
14 fn round_pointer_up_to_alignment(
15     bx: &mut Builder<'a, 'll, 'tcx>,
16     addr: &'ll Value,
17     align: Align,
18     ptr_ty: &'ll Type,
19 ) -> &'ll Value {
20     let mut ptr_as_int = bx.ptrtoint(addr, bx.cx().type_isize());
21     ptr_as_int = bx.add(ptr_as_int, bx.cx().const_i32(align.bytes() as i32 - 1));
22     ptr_as_int = bx.and(ptr_as_int, bx.cx().const_i32(-(align.bytes() as i32)));
23     bx.inttoptr(ptr_as_int, ptr_ty)
24 }
25
26 fn emit_direct_ptr_va_arg(
27     bx: &mut Builder<'a, 'll, 'tcx>,
28     list: OperandRef<'tcx, &'ll Value>,
29     llty: &'ll Type,
30     size: Size,
31     align: Align,
32     slot_size: Align,
33     allow_higher_align: bool,
34 ) -> (&'ll Value, Align) {
35     let va_list_ptr_ty = bx.cx().type_ptr_to(bx.cx.type_i8p());
36     let va_list_addr = if list.layout.llvm_type(bx.cx) != va_list_ptr_ty {
37         bx.bitcast(list.immediate(), va_list_ptr_ty)
38     } else {
39         list.immediate()
40     };
41
42     let ptr = bx.load(va_list_addr, bx.tcx().data_layout.pointer_align.abi);
43
44     let (addr, addr_align) = if allow_higher_align && align > slot_size {
45         (round_pointer_up_to_alignment(bx, ptr, align, bx.cx().type_i8p()), align)
46     } else {
47         (ptr, slot_size)
48     };
49
50     let aligned_size = size.align_to(slot_size).bytes() as i32;
51     let full_direct_size = bx.cx().const_i32(aligned_size);
52     let next = bx.inbounds_gep(addr, &[full_direct_size]);
53     bx.store(next, va_list_addr, bx.tcx().data_layout.pointer_align.abi);
54
55     if size.bytes() < slot_size.bytes() && bx.tcx().sess.target.endian == Endian::Big {
56         let adjusted_size = bx.cx().const_i32((slot_size.bytes() - size.bytes()) as i32);
57         let adjusted = bx.inbounds_gep(addr, &[adjusted_size]);
58         (bx.bitcast(adjusted, bx.cx().type_ptr_to(llty)), addr_align)
59     } else {
60         (bx.bitcast(addr, bx.cx().type_ptr_to(llty)), addr_align)
61     }
62 }
63
64 fn emit_ptr_va_arg(
65     bx: &mut Builder<'a, 'll, 'tcx>,
66     list: OperandRef<'tcx, &'ll Value>,
67     target_ty: Ty<'tcx>,
68     indirect: bool,
69     slot_size: Align,
70     allow_higher_align: bool,
71 ) -> &'ll Value {
72     let layout = bx.cx.layout_of(target_ty);
73     let (llty, size, align) = if indirect {
74         (
75             bx.cx.layout_of(bx.cx.tcx.mk_imm_ptr(target_ty)).llvm_type(bx.cx),
76             bx.cx.data_layout().pointer_size,
77             bx.cx.data_layout().pointer_align,
78         )
79     } else {
80         (layout.llvm_type(bx.cx), layout.size, layout.align)
81     };
82     let (addr, addr_align) =
83         emit_direct_ptr_va_arg(bx, list, llty, size, align.abi, slot_size, allow_higher_align);
84     if indirect {
85         let tmp_ret = bx.load(addr, addr_align);
86         bx.load(tmp_ret, align.abi)
87     } else {
88         bx.load(addr, addr_align)
89     }
90 }
91
92 fn emit_aapcs_va_arg(
93     bx: &mut Builder<'a, 'll, 'tcx>,
94     list: OperandRef<'tcx, &'ll Value>,
95     target_ty: Ty<'tcx>,
96 ) -> &'ll Value {
97     // Implementation of the AAPCS64 calling convention for va_args see
98     // https://github.com/ARM-software/abi-aa/blob/master/aapcs64/aapcs64.rst
99     let va_list_addr = list.immediate();
100     let layout = bx.cx.layout_of(target_ty);
101
102     let mut maybe_reg = bx.build_sibling_block("va_arg.maybe_reg");
103     let mut in_reg = bx.build_sibling_block("va_arg.in_reg");
104     let mut on_stack = bx.build_sibling_block("va_arg.on_stack");
105     let mut end = bx.build_sibling_block("va_arg.end");
106     let zero = bx.const_i32(0);
107     let offset_align = Align::from_bytes(4).unwrap();
108
109     let gr_type = target_ty.is_any_ptr() || target_ty.is_integral();
110     let (reg_off, reg_top_index, slot_size) = if gr_type {
111         let gr_offs = bx.struct_gep(va_list_addr, 7);
112         let nreg = (layout.size.bytes() + 7) / 8;
113         (gr_offs, 3, nreg * 8)
114     } else {
115         let vr_off = bx.struct_gep(va_list_addr, 9);
116         let nreg = (layout.size.bytes() + 15) / 16;
117         (vr_off, 5, nreg * 16)
118     };
119
120     // if the offset >= 0 then the value will be on the stack
121     let mut reg_off_v = bx.load(reg_off, offset_align);
122     let use_stack = bx.icmp(IntPredicate::IntSGE, reg_off_v, zero);
123     bx.cond_br(use_stack, &on_stack.llbb(), &maybe_reg.llbb());
124
125     // The value at this point might be in a register, but there is a chance that
126     // it could be on the stack so we have to update the offset and then check
127     // the offset again.
128
129     if gr_type && layout.align.abi.bytes() > 8 {
130         reg_off_v = maybe_reg.add(reg_off_v, bx.const_i32(15));
131         reg_off_v = maybe_reg.and(reg_off_v, bx.const_i32(-16));
132     }
133     let new_reg_off_v = maybe_reg.add(reg_off_v, bx.const_i32(slot_size as i32));
134
135     maybe_reg.store(new_reg_off_v, reg_off, offset_align);
136
137     // Check to see if we have overflowed the registers as a result of this.
138     // If we have then we need to use the stack for this value
139     let use_stack = maybe_reg.icmp(IntPredicate::IntSGT, new_reg_off_v, zero);
140     maybe_reg.cond_br(use_stack, &on_stack.llbb(), &in_reg.llbb());
141
142     let top = in_reg.struct_gep(va_list_addr, reg_top_index);
143     let top = in_reg.load(top, bx.tcx().data_layout.pointer_align.abi);
144
145     // reg_value = *(@top + reg_off_v);
146     let mut reg_addr = in_reg.gep(top, &[reg_off_v]);
147     if bx.tcx().sess.target.endian == Endian::Big && layout.size.bytes() != slot_size {
148         // On big-endian systems the value is right-aligned in its slot.
149         let offset = bx.const_i32((slot_size - layout.size.bytes()) as i32);
150         reg_addr = in_reg.gep(reg_addr, &[offset]);
151     }
152     let reg_addr = in_reg.bitcast(reg_addr, bx.cx.type_ptr_to(layout.llvm_type(bx)));
153     let reg_value = in_reg.load(reg_addr, layout.align.abi);
154     in_reg.br(&end.llbb());
155
156     // On Stack block
157     let stack_value =
158         emit_ptr_va_arg(&mut on_stack, list, target_ty, false, Align::from_bytes(8).unwrap(), true);
159     on_stack.br(&end.llbb());
160
161     let val = end.phi(
162         layout.immediate_llvm_type(bx),
163         &[reg_value, stack_value],
164         &[&in_reg.llbb(), &on_stack.llbb()],
165     );
166
167     *bx = end;
168     val
169 }
170
171 pub(super) fn emit_va_arg(
172     bx: &mut Builder<'a, 'll, 'tcx>,
173     addr: OperandRef<'tcx, &'ll Value>,
174     target_ty: Ty<'tcx>,
175 ) -> &'ll Value {
176     // Determine the va_arg implementation to use. The LLVM va_arg instruction
177     // is lacking in some instances, so we should only use it as a fallback.
178     let target = &bx.cx.tcx.sess.target;
179     let arch = &bx.cx.tcx.sess.target.arch;
180     match &**arch {
181         // Windows x86
182         "x86" if target.is_like_windows => {
183             emit_ptr_va_arg(bx, addr, target_ty, false, Align::from_bytes(4).unwrap(), false)
184         }
185         // Generic x86
186         "x86" => emit_ptr_va_arg(bx, addr, target_ty, false, Align::from_bytes(4).unwrap(), true),
187         // Windows AArch64
188         "aarch64" if target.is_like_windows => {
189             emit_ptr_va_arg(bx, addr, target_ty, false, Align::from_bytes(8).unwrap(), false)
190         }
191         // macOS / iOS AArch64
192         "aarch64" if target.is_like_osx => {
193             emit_ptr_va_arg(bx, addr, target_ty, false, Align::from_bytes(8).unwrap(), true)
194         }
195         "aarch64" => emit_aapcs_va_arg(bx, addr, target_ty),
196         // Windows x86_64
197         "x86_64" if target.is_like_windows => {
198             let target_ty_size = bx.cx.size_of(target_ty).bytes();
199             let indirect: bool = target_ty_size > 8 || !target_ty_size.is_power_of_two();
200             emit_ptr_va_arg(bx, addr, target_ty, indirect, Align::from_bytes(8).unwrap(), false)
201         }
202         // For all other architecture/OS combinations fall back to using
203         // the LLVM va_arg instruction.
204         // https://llvm.org/docs/LangRef.html#va-arg-instruction
205         _ => bx.va_arg(addr.immediate(), bx.cx.layout_of(target_ty).llvm_type(bx.cx)),
206     }
207 }