Skip to content

Commit 584035c

Browse files
committed
implement va_arg for x86_64 systemv
Turns out LLVM's `va_arg` is also unreliable for this target.
1 parent 6eef33b commit 584035c

File tree

3 files changed

+187
-3
lines changed

3 files changed

+187
-3
lines changed

compiler/rustc_codegen_llvm/src/va_arg.rs

Lines changed: 175 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_abi::{Align, Endian, HasDataLayout, Size};
1+
use rustc_abi::{Align, BackendRepr, Endian, HasDataLayout, Primitive, Size};
22
use rustc_codegen_ssa::common::IntPredicate;
33
use rustc_codegen_ssa::mir::operand::OperandRef;
44
use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods, BuilderMethods, ConstCodegenMethods};
@@ -278,6 +278,179 @@ fn emit_s390x_va_arg<'ll, 'tcx>(
278278
bx.load(val_type, val_addr, layout.align.abi)
279279
}
280280

281+
fn emit_x86_64_sysv64_va_arg<'ll, 'tcx>(
282+
bx: &mut Builder<'_, 'll, 'tcx>,
283+
list: OperandRef<'tcx, &'ll Value>,
284+
target_ty: Ty<'tcx>,
285+
) -> &'ll Value {
286+
let dl = bx.cx.data_layout();
287+
288+
// Implementation of the systemv x86_64 ABI calling convention for va_args, see
289+
// https://gitlab.com/x86-psABIs/x86-64-ABI (section 3.5.7). This implementation is heavily
290+
// based on the one in clang.
291+
292+
// We're able to take some shortcuts because the return type of `va_arg` must implement the
293+
// `VaArgSafe` trait. Currently, only pointers, f64, i32, u32, i64 and u64 implement this trait.
294+
295+
// typedef struct __va_list_tag {
296+
// unsigned int gp_offset;
297+
// unsigned int fp_offset;
298+
// void *overflow_arg_area;
299+
// void *reg_save_area;
300+
// } va_list[1];
301+
let va_list_addr = list.immediate();
302+
303+
let unsigned_int_offset = 4;
304+
let ptr_offset = 8;
305+
let gp_offset_ptr = va_list_addr;
306+
let fp_offset_ptr = bx.inbounds_ptradd(va_list_addr, bx.cx.const_usize(unsigned_int_offset));
307+
308+
let layout = bx.cx.layout_of(target_ty);
309+
310+
// AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
311+
// in the registers. If not go to step 7.
312+
313+
// AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
314+
// general purpose registers needed to pass type and num_fp to hold
315+
// the number of floating point registers needed.
316+
317+
let mut num_gp_registers = 0;
318+
let mut num_fp_registers = 0;
319+
320+
match layout.layout.backend_repr() {
321+
BackendRepr::Scalar(scalar) => match scalar.primitive() {
322+
Primitive::Int(integer, _is_signed) => {
323+
num_gp_registers += integer.size().bytes().div_ceil(8) as u32;
324+
}
325+
Primitive::Float(float) => {
326+
num_fp_registers += float.size().bytes().div_ceil(16) as u32;
327+
}
328+
Primitive::Pointer(_) => {
329+
num_gp_registers += 1;
330+
}
331+
},
332+
BackendRepr::ScalarPair(..)
333+
| BackendRepr::SimdVector { .. }
334+
| BackendRepr::Memory { .. } => {
335+
// Because no instance of VaArgSafe uses a non-scalar `BackendRepr`.
336+
unreachable!(
337+
"No x86-64 SysV va_arg implementation for {:?}",
338+
layout.layout.backend_repr()
339+
)
340+
}
341+
};
342+
343+
if num_gp_registers == 0 && num_fp_registers == 0 {
344+
unreachable!("VaArgSafe is not implemented for ZSTs")
345+
}
346+
347+
// AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
348+
// registers. In the case: l->gp_offset > 48 - num_gp * 8 or
349+
// l->fp_offset > 176 - num_fp * 16 go to step 7.
350+
351+
let gp_offset_v = bx.load(bx.type_i32(), gp_offset_ptr, Align::from_bytes(8).unwrap());
352+
let fp_offset_v = bx.load(bx.type_i32(), fp_offset_ptr, Align::from_bytes(4).unwrap());
353+
354+
let mut use_regs = bx.const_bool(false);
355+
356+
if num_gp_registers > 0 {
357+
let max_offset_val = 48u32 - num_gp_registers * 8;
358+
let fits_in_gp = bx.icmp(IntPredicate::IntULE, gp_offset_v, bx.const_u32(max_offset_val));
359+
use_regs = fits_in_gp;
360+
}
361+
362+
if num_fp_registers > 0 {
363+
let max_offset_val = 176u32 - num_fp_registers * 16;
364+
let fits_in_fp = bx.icmp(IntPredicate::IntULE, fp_offset_v, bx.const_u32(max_offset_val));
365+
use_regs = if num_gp_registers > 0 { bx.and(use_regs, fits_in_fp) } else { fits_in_fp };
366+
}
367+
368+
let in_reg = bx.append_sibling_block("va_arg.in_reg");
369+
let in_mem = bx.append_sibling_block("va_arg.in_mem");
370+
let end = bx.append_sibling_block("va_arg.end");
371+
372+
bx.cond_br(use_regs, in_reg, in_mem);
373+
374+
// Emit code to load the value if it was passed in a register.
375+
bx.switch_to_block(in_reg);
376+
377+
// AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
378+
// an offset of l->gp_offset and/or l->fp_offset. This may require
379+
// copying to a temporary location in case the parameter is passed
380+
// in different register classes or requires an alignment greater
381+
// than 8 for general purpose registers and 16 for XMM registers.
382+
//
383+
// FIXME(llvm): This really results in shameful code when we end up needing to
384+
// collect arguments from different places; often what should result in a
385+
// simple assembling of a structure from scattered addresses has many more
386+
// loads than necessary. Can we clean this up?
387+
let reg_save_area_ptr =
388+
bx.inbounds_ptradd(va_list_addr, bx.cx.const_usize(2 * unsigned_int_offset + ptr_offset));
389+
let reg_save_area_v = bx.load(bx.type_ptr(), reg_save_area_ptr, dl.pointer_align.abi);
390+
391+
let reg_addr = if num_gp_registers > 0 && num_fp_registers > 0 {
392+
unreachable!("instances of VaArgSafe cannot use both int and sse registers");
393+
} else if num_gp_registers > 0 || num_fp_registers == 1 {
394+
let gp_or_fp_offset = if num_gp_registers > 0 { gp_offset_v } else { fp_offset_v };
395+
bx.gep(bx.type_i8(), reg_save_area_v, &[gp_or_fp_offset])
396+
} else {
397+
// assert_eq!(num_sse_registers, 2);
398+
unreachable!("all instances of VaArgSafe have an alignment <= 8");
399+
};
400+
401+
// AMD64-ABI 3.5.7p5: Step 5. Set:
402+
// l->gp_offset = l->gp_offset + num_gp * 8
403+
if num_gp_registers > 0 {
404+
let offset = bx.const_u32(num_gp_registers * 8);
405+
let sum = bx.add(gp_offset_v, offset);
406+
bx.store(sum, gp_offset_ptr, Align::from_bytes(8).unwrap());
407+
}
408+
409+
// l->fp_offset = l->fp_offset + num_fp * 16.
410+
if num_fp_registers > 0 {
411+
let offset = bx.const_u32(num_fp_registers * 16);
412+
let sum = bx.add(fp_offset_v, offset);
413+
bx.store(sum, fp_offset_ptr, Align::from_bytes(4).unwrap());
414+
}
415+
416+
bx.br(end);
417+
418+
bx.switch_to_block(in_mem);
419+
420+
let overflow_arg_area_ptr =
421+
bx.inbounds_ptradd(va_list_addr, bx.cx.const_usize(2 * unsigned_int_offset));
422+
423+
let overflow_arg_area_v = bx.load(bx.type_ptr(), overflow_arg_area_ptr, dl.pointer_align.abi);
424+
// AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
425+
// byte boundary if alignment needed by type exceeds 8 byte boundary.
426+
// It isn't stated explicitly in the standard, but in practice we use
427+
// alignment greater than 16 where necessary.
428+
if layout.layout.align.abi.bytes() > 8 {
429+
unreachable!("all instances of VaArgSafe have an alignment <= 8");
430+
}
431+
432+
// AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
433+
let mem_addr = overflow_arg_area_v;
434+
435+
// AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
436+
// l->overflow_arg_area + sizeof(type).
437+
// AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
438+
// an 8 byte boundary.
439+
let size_in_bytes = layout.layout.size().bytes();
440+
let offset = bx.const_i32(size_in_bytes.next_multiple_of(8) as i32);
441+
let overflow_arg_area = bx.gep(bx.type_i8(), overflow_arg_area_v, &[offset]);
442+
bx.store(overflow_arg_area, overflow_arg_area_ptr, dl.pointer_align.abi);
443+
444+
bx.br(end);
445+
446+
bx.switch_to_block(end);
447+
448+
let val_type = layout.llvm_type(bx);
449+
let val_addr = bx.phi(bx.type_ptr(), &[reg_addr, mem_addr], &[in_reg, in_mem]);
450+
451+
bx.load(val_type, val_addr, layout.align.abi)
452+
}
453+
281454
fn emit_xtensa_va_arg<'ll, 'tcx>(
282455
bx: &mut Builder<'_, 'll, 'tcx>,
283456
list: OperandRef<'tcx, &'ll Value>,
@@ -410,6 +583,7 @@ pub(super) fn emit_va_arg<'ll, 'tcx>(
410583
let indirect: bool = target_ty_size > 8 || !target_ty_size.is_power_of_two();
411584
emit_ptr_va_arg(bx, addr, target_ty, indirect, Align::from_bytes(8).unwrap(), false)
412585
}
586+
"x86_64" if !target.is_like_darwin => emit_x86_64_sysv64_va_arg(bx, addr, target_ty),
413587
"xtensa" => emit_xtensa_va_arg(bx, addr, target_ty),
414588
// For all other architecture/OS combinations fall back to using
415589
// the LLVM va_arg instruction.

tests/run-make/c-link-to-rust-va-list-fn/checkrust.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ pub unsafe extern "C" fn check_varargs_4(_: c_double, mut ap: ...) -> usize {
112112
continue_if!(ap.arg::<c_double>() == 8.0);
113113
continue_if!(ap.arg::<c_double>() == 9.0);
114114
continue_if!(ap.arg::<c_double>() == 10.0);
115+
continue_if!(ap.arg::<c_double>() == 11.0);
116+
continue_if!(ap.arg::<c_double>() == 12.0);
117+
continue_if!(ap.arg::<c_double>() == 13.0);
115118
0
116119
}
117120

@@ -137,5 +140,11 @@ pub unsafe extern "C" fn check_varargs_5(_: c_int, mut ap: ...) -> usize {
137140
continue_if!(ap.arg::<c_double>() == 9.0);
138141
continue_if!(ap.arg::<c_int>() == 10);
139142
continue_if!(ap.arg::<c_double>() == 10.0);
143+
continue_if!(ap.arg::<c_int>() == 11);
144+
continue_if!(ap.arg::<c_double>() == 11.0);
145+
continue_if!(ap.arg::<c_int>() == 12);
146+
continue_if!(ap.arg::<c_double>() == 12.0);
147+
continue_if!(ap.arg::<c_int>() == 13);
148+
continue_if!(ap.arg::<c_double>() == 13.0);
140149
0
141150
}

tests/run-make/c-link-to-rust-va-list-fn/test.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ int main(int argc, char* argv[]) {
4141

4242
assert(check_varargs_3(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) == 0);
4343

44-
assert(check_varargs_4(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0) == 0);
44+
assert(check_varargs_4(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0,
45+
13.0) == 0);
4546

4647
assert(check_varargs_5(0, 1.0, 1, 2.0, 2, 3.0, 3, 4.0, 4, 5, 5.0, 6, 6.0, 7, 7.0, 8, 8.0,
47-
9, 9.0, 10, 10.0) == 0);
48+
9, 9.0, 10, 10.0, 11, 11.0, 12, 12.0, 13, 13.0) == 0);
4849

4950
return 0;
5051
}

0 commit comments

Comments
 (0)