@@ -10,15 +10,23 @@ use crate::type_::Type;
10
10
use crate :: type_of:: LayoutLlvmExt ;
11
11
use crate :: value:: Value ;
12
12
13
+ fn round_up_to_alignment < ' ll > (
14
+ bx : & mut Builder < ' _ , ' ll , ' _ > ,
15
+ mut value : & ' ll Value ,
16
+ align : Align ,
17
+ ) -> & ' ll Value {
18
+ value = bx. add ( value, bx. cx ( ) . const_i32 ( align. bytes ( ) as i32 - 1 ) ) ;
19
+ return bx. and ( value, bx. cx ( ) . const_i32 ( -( align. bytes ( ) as i32 ) ) ) ;
20
+ }
21
+
13
22
fn round_pointer_up_to_alignment < ' ll > (
14
23
bx : & mut Builder < ' _ , ' ll , ' _ > ,
15
24
addr : & ' ll Value ,
16
25
align : Align ,
17
26
ptr_ty : & ' ll Type ,
18
27
) -> & ' ll Value {
19
28
let mut ptr_as_int = bx. ptrtoint ( addr, bx. cx ( ) . type_isize ( ) ) ;
20
- ptr_as_int = bx. add ( ptr_as_int, bx. cx ( ) . const_i32 ( align. bytes ( ) as i32 - 1 ) ) ;
21
- ptr_as_int = bx. and ( ptr_as_int, bx. cx ( ) . const_i32 ( -( align. bytes ( ) as i32 ) ) ) ;
29
+ ptr_as_int = round_up_to_alignment ( bx, ptr_as_int, align) ;
22
30
bx. inttoptr ( ptr_as_int, ptr_ty)
23
31
}
24
32
@@ -270,6 +278,93 @@ fn emit_s390x_va_arg<'ll, 'tcx>(
270
278
bx. load ( val_type, val_addr, layout. align . abi )
271
279
}
272
280
281
+ fn emit_xtensa_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
+ // Implementation of va_arg for Xtensa. There doesn't seem to be an authoritative source for
287
+ // this, other than "what GCC does".
288
+ //
289
+ // The va_list type has three fields:
290
+ // struct __va_list_tag {
291
+ // int32_t *va_stk; // Arguments passed on the stack
292
+ // int32_t *va_reg; // Arguments passed in registers, saved to memory by the prologue.
293
+ // int32_t va_ndx; // Offset into the arguments, in bytes
294
+ // };
295
+ //
296
+ // The first 24 bytes (equivalent to 6 registers) come from va_reg, the rest from va_stk.
297
+ // Thus if va_ndx is less than 24, the next va_arg *may* read from va_reg,
298
+ // otherwise it must come from va_stk.
299
+ //
300
+ // Primitive arguments are never split between registers and the stack. For example, if loading an 8 byte
301
+ // primitive value and va_ndx = 20, we instead bump the offset and read everything from va_stk.
302
+ let va_list_addr = list. immediate ( ) ;
303
+ let layout = bx. cx . layout_of ( target_ty) ;
304
+ let from_stack = bx. append_sibling_block ( "va_arg.from_stack" ) ;
305
+ let from_regsave = bx. append_sibling_block ( "va_arg.from_regsave" ) ;
306
+ let end = bx. append_sibling_block ( "va_arg.end" ) ;
307
+
308
+ // The following code is equivalent to `(*va).va_ndx`
309
+ let va_reg_offset = 4 ;
310
+ let va_ndx_offset = va_reg_offset + 4 ;
311
+ let offset_ptr =
312
+ bx. inbounds_gep ( bx. type_i8 ( ) , va_list_addr, & [ bx. cx . const_usize ( va_ndx_offset) ] ) ;
313
+
314
+ let offset = bx. load ( bx. type_i32 ( ) , offset_ptr, bx. tcx ( ) . data_layout . i32_align . abi ) ;
315
+ let offset = round_up_to_alignment ( bx, offset, layout. align . abi ) ;
316
+
317
+ let slot_size = layout. size . align_to ( Align :: from_bytes ( 4 ) . unwrap ( ) ) . bytes ( ) as i32 ;
318
+
319
+ // Update the offset in va_list, by adding the slot's size.
320
+ let offset_next = bx. add ( offset, bx. const_i32 ( slot_size) ) ;
321
+
322
+ // Figure out where to look for our value. We do that by checking the end of our slot (offset_next).
323
+ // If that is within the regsave area, then load from there. Otherwise load from the stack area.
324
+ let regsave_size = bx. const_i32 ( 24 ) ;
325
+ let use_regsave = bx. icmp ( IntPredicate :: IntULE , offset_next, regsave_size) ;
326
+ bx. cond_br ( use_regsave, from_regsave, from_stack) ;
327
+
328
+ bx. switch_to_block ( from_regsave) ;
329
+ // update va_ndx
330
+ bx. store ( offset_next, offset_ptr, bx. tcx ( ) . data_layout . pointer_align . abi ) ;
331
+ // The following code is equivalent to `(*va).va_reg`
332
+ let regsave_area_ptr =
333
+ bx. inbounds_gep ( bx. type_i8 ( ) , va_list_addr, & [ bx. cx . const_usize ( va_reg_offset) ] ) ;
334
+ let regsave_area =
335
+ bx. load ( bx. type_ptr ( ) , regsave_area_ptr, bx. tcx ( ) . data_layout . pointer_align . abi ) ;
336
+ let regsave_value_ptr = bx. inbounds_gep ( bx. type_i8 ( ) , regsave_area, & [ offset] ) ;
337
+ bx. br ( end) ;
338
+
339
+ bx. switch_to_block ( from_stack) ;
340
+
341
+ // The first time we switch from regsave to stack we needs to adjust our offsets a bit.
342
+ // va_stk is set up such that the first stack argument is always at va_stk + 32.
343
+ // The corrected offset is written back into the va_list struct.
344
+ let needs_correction = bx. icmp ( IntPredicate :: IntULE , offset, regsave_size) ;
345
+ let offset_corrected = bx. select ( needs_correction, bx. const_i32 ( 32 ) , offset) ;
346
+ let offset_next_corrected =
347
+ bx. select ( needs_correction, bx. const_i32 ( 32 + slot_size) , offset_next) ;
348
+ // update va_ndx
349
+ bx. store ( offset_next_corrected, offset_ptr, bx. tcx ( ) . data_layout . pointer_align . abi ) ;
350
+
351
+ // The following code is equivalent to `(*va).va_stk`
352
+ let stack_area_ptr = bx. inbounds_gep ( bx. type_i8 ( ) , va_list_addr, & [ bx. cx . const_usize ( 0 ) ] ) ;
353
+ let stack_area = bx. load ( bx. type_ptr ( ) , stack_area_ptr, bx. tcx ( ) . data_layout . pointer_align . abi ) ;
354
+ let stack_value_ptr = bx. inbounds_gep ( bx. type_i8 ( ) , stack_area, & [ offset_corrected] ) ;
355
+ bx. br ( end) ;
356
+
357
+ bx. switch_to_block ( end) ;
358
+
359
+ // On big-endian, for values smaller than the slot size we'd have to align the read to the end
360
+ // of the slot rather than the start. While the ISA and GCC support big-endian, all the Xtensa
361
+ // targets supported by rustc are litte-endian so don't worry about it.
362
+ assert ! ( bx. tcx( ) . sess. target. endian == Endian :: Little ) ;
363
+ let value_ptr =
364
+ bx. phi ( bx. type_ptr ( ) , & [ regsave_value_ptr, stack_value_ptr] , & [ from_regsave, from_stack] ) ;
365
+ return bx. load ( layout. llvm_type ( bx) , value_ptr, layout. align . abi ) ;
366
+ }
367
+
273
368
pub ( super ) fn emit_va_arg < ' ll , ' tcx > (
274
369
bx : & mut Builder < ' _ , ' ll , ' tcx > ,
275
370
addr : OperandRef < ' tcx , & ' ll Value > ,
@@ -302,6 +397,7 @@ pub(super) fn emit_va_arg<'ll, 'tcx>(
302
397
let indirect: bool = target_ty_size > 8 || !target_ty_size. is_power_of_two ( ) ;
303
398
emit_ptr_va_arg ( bx, addr, target_ty, indirect, Align :: from_bytes ( 8 ) . unwrap ( ) , false )
304
399
}
400
+ "xtensa" => emit_xtensa_va_arg ( bx, addr, target_ty) ,
305
401
// For all other architecture/OS combinations fall back to using
306
402
// the LLVM va_arg instruction.
307
403
// https://llvm.org/docs/LangRef.html#va-arg-instruction
0 commit comments