Skip to content

Commit 2c3b12f

Browse files
committed
implement va_arg for x86_64 systemv
Turns out LLVM's `va_arg` is also unreliable for this target.
1 parent c583fa6 commit 2c3b12f

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};
@@ -303,6 +303,179 @@ fn emit_s390x_va_arg<'ll, 'tcx>(
303303
bx.load(val_type, val_addr, layout.align.abi)
304304
}
305305

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