Skip to content

Commit 720bcd8

Browse files
committed
auto merge of #10652 : jld/rust/enum-unstruct, r=thestinger
This is needed so that the FFI works as expected on platforms that don't flatten aggregates the way the AMD64 ABI does, especially for `#[repr(C)]`. This moves more of `type_of` into `trans::adt`, because the type might or might not be an LLVM struct. Closes #10308.
2 parents ffaee0f + 0c04a26 commit 720bcd8

File tree

3 files changed

+99
-40
lines changed

3 files changed

+99
-40
lines changed

src/librustc/middle/trans/adt.rs

+46-20
Original file line numberDiff line numberDiff line change
@@ -366,22 +366,41 @@ pub fn ty_of_inttype(ity: IntType) -> ty::t {
366366

367367

368368
/**
369-
* Returns the fields of a struct for the given representation.
370-
* All nominal types are LLVM structs, in order to be able to use
371-
* forward-declared opaque types to prevent circularity in `type_of`.
369+
* LLVM-level types are a little complicated.
370+
*
371+
* C-like enums need to be actual ints, not wrapped in a struct,
372+
* because that changes the ABI on some platforms (see issue #10308).
373+
*
374+
* For nominal types, in some cases, we need to use LLVM named structs
375+
* and fill in the actual contents in a second pass to prevent
376+
* unbounded recursion; see also the comments in `trans::type_of`.
372377
*/
373-
pub fn fields_of(cx: &mut CrateContext, r: &Repr) -> ~[Type] {
374-
generic_fields_of(cx, r, false)
378+
pub fn type_of(cx: &mut CrateContext, r: &Repr) -> Type {
379+
generic_type_of(cx, r, None, false)
380+
}
381+
pub fn sizing_type_of(cx: &mut CrateContext, r: &Repr) -> Type {
382+
generic_type_of(cx, r, None, true)
375383
}
376-
/// Like `fields_of`, but for `type_of::sizing_type_of` (q.v.).
377-
pub fn sizing_fields_of(cx: &mut CrateContext, r: &Repr) -> ~[Type] {
378-
generic_fields_of(cx, r, true)
384+
pub fn incomplete_type_of(cx: &mut CrateContext, r: &Repr, name: &str) -> Type {
385+
generic_type_of(cx, r, Some(name), false)
386+
}
387+
pub fn finish_type_of(cx: &mut CrateContext, r: &Repr, llty: &mut Type) {
388+
match *r {
389+
CEnum(*) | General(*) => { }
390+
Univariant(ref st, _) | NullablePointer{ nonnull: ref st, _ } =>
391+
llty.set_struct_body(struct_llfields(cx, st, false), st.packed)
392+
}
379393
}
380-
fn generic_fields_of(cx: &mut CrateContext, r: &Repr, sizing: bool) -> ~[Type] {
394+
395+
fn generic_type_of(cx: &mut CrateContext, r: &Repr, name: Option<&str>, sizing: bool) -> Type {
381396
match *r {
382-
CEnum(ity, _, _) => ~[ll_inttype(cx, ity)],
383-
Univariant(ref st, _dtor) => struct_llfields(cx, st, sizing),
384-
NullablePointer{ nonnull: ref st, _ } => struct_llfields(cx, st, sizing),
397+
CEnum(ity, _, _) => ll_inttype(cx, ity),
398+
Univariant(ref st, _) | NullablePointer{ nonnull: ref st, _ } => {
399+
match name {
400+
None => Type::struct_(struct_llfields(cx, st, sizing), st.packed),
401+
Some(name) => { assert_eq!(sizing, false); Type::named_struct(name) }
402+
}
403+
}
385404
General(ity, ref sts) => {
386405
// We need a representation that has:
387406
// * The alignment of the most-aligned field
@@ -394,8 +413,7 @@ fn generic_fields_of(cx: &mut CrateContext, r: &Repr, sizing: bool) -> ~[Type] {
394413
// more of its own type, then use alignment-sized ints to get the rest
395414
// of the size.
396415
//
397-
// Note: if/when we start exposing SIMD vector types (or f80, on some
398-
// platforms that have it), this will need some adjustment.
416+
// FIXME #10604: this breaks when vector types are present.
399417
let size = sts.iter().map(|st| st.size).max().unwrap();
400418
let most_aligned = sts.iter().max_by(|st| st.align).unwrap();
401419
let align = most_aligned.align;
@@ -411,9 +429,17 @@ fn generic_fields_of(cx: &mut CrateContext, r: &Repr, sizing: bool) -> ~[Type] {
411429
assert_eq!(machine::llalign_of_min(cx, pad_ty) as u64, align);
412430
let align_units = (size + align - 1) / align;
413431
assert_eq!(align % discr_size, 0);
414-
~[discr_ty,
432+
let fields = ~[discr_ty,
415433
Type::array(&discr_ty, align / discr_size - 1),
416-
Type::array(&pad_ty, align_units - 1)]
434+
Type::array(&pad_ty, align_units - 1)];
435+
match name {
436+
None => Type::struct_(fields, false),
437+
Some(name) => {
438+
let mut llty = Type::named_struct(name);
439+
llty.set_struct_body(fields, false);
440+
llty
441+
}
442+
}
417443
}
418444
}
419445
}
@@ -460,7 +486,8 @@ pub fn trans_get_discr(bcx: @mut Block, r: &Repr, scrutinee: ValueRef, cast_to:
460486
signed = ity.is_signed();
461487
}
462488
General(ity, ref cases) => {
463-
val = load_discr(bcx, ity, scrutinee, 0, (cases.len() - 1) as Disr);
489+
let ptr = GEPi(bcx, scrutinee, [0, 0]);
490+
val = load_discr(bcx, ity, ptr, 0, (cases.len() - 1) as Disr);
464491
signed = ity.is_signed();
465492
}
466493
Univariant(*) => {
@@ -487,9 +514,8 @@ fn nullable_bitdiscr(bcx: @mut Block, nonnull: &Struct, nndiscr: Disr, ptrfield:
487514
}
488515

489516
/// Helper for cases where the discriminant is simply loaded.
490-
fn load_discr(bcx: @mut Block, ity: IntType, scrutinee: ValueRef, min: Disr, max: Disr)
517+
fn load_discr(bcx: @mut Block, ity: IntType, ptr: ValueRef, min: Disr, max: Disr)
491518
-> ValueRef {
492-
let ptr = GEPi(bcx, scrutinee, [0, 0]);
493519
let llty = ll_inttype(bcx.ccx(), ity);
494520
assert_eq!(val_ty(ptr), llty.ptr_to());
495521
let bits = machine::llbitsize_of_real(bcx.ccx(), llty);
@@ -546,7 +572,7 @@ pub fn trans_start_init(bcx: @mut Block, r: &Repr, val: ValueRef, discr: Disr) {
546572
CEnum(ity, min, max) => {
547573
assert_discr_in_range(ity, min, max, discr);
548574
Store(bcx, C_integral(ll_inttype(bcx.ccx(), ity), discr as u64, true),
549-
GEPi(bcx, val, [0, 0]))
575+
val)
550576
}
551577
General(ity, _) => {
552578
Store(bcx, C_integral(ll_inttype(bcx.ccx(), ity), discr as u64, true),

src/librustc/middle/trans/type_of.rs

+14-20
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,17 @@ pub fn sizing_type_of(cx: &mut CrateContext, t: ty::t) -> Type {
147147

148148
ty::ty_tup(*) | ty::ty_enum(*) => {
149149
let repr = adt::represent_type(cx, t);
150-
Type::struct_(adt::sizing_fields_of(cx, repr), false)
150+
adt::sizing_type_of(cx, repr)
151151
}
152152

153-
ty::ty_struct(did, _) => {
153+
ty::ty_struct(*) => {
154154
if ty::type_is_simd(cx.tcx, t) {
155155
let et = ty::simd_type(cx.tcx, t);
156156
let n = ty::simd_size(cx.tcx, t);
157157
Type::vector(&type_of(cx, et), n as u64)
158158
} else {
159159
let repr = adt::represent_type(cx, t);
160-
let packed = ty::lookup_packed(cx.tcx, did);
161-
Type::struct_(adt::sizing_fields_of(cx, repr), packed)
160+
adt::sizing_type_of(cx, repr)
162161
}
163162
}
164163

@@ -217,8 +216,9 @@ pub fn type_of(cx: &mut CrateContext, t: ty::t) -> Type {
217216
// fill it in *after* placing it into the type cache. This
218217
// avoids creating more than one copy of the enum when one
219218
// of the enum's variants refers to the enum itself.
220-
221-
Type::named_struct(llvm_type_name(cx, an_enum, did, substs.tps))
219+
let repr = adt::represent_type(cx, t);
220+
let name = llvm_type_name(cx, an_enum, did, substs.tps);
221+
adt::incomplete_type_of(cx, repr, name)
222222
}
223223
ty::ty_estr(ty::vstore_box) => {
224224
Type::box(cx, &Type::vec(cx.sess.targ_cfg.arch, &Type::i8())).ptr_to()
@@ -287,7 +287,7 @@ pub fn type_of(cx: &mut CrateContext, t: ty::t) -> Type {
287287
ty::ty_type => cx.tydesc_type.ptr_to(),
288288
ty::ty_tup(*) => {
289289
let repr = adt::represent_type(cx, t);
290-
Type::struct_(adt::fields_of(cx, repr), false)
290+
adt::type_of(cx, repr)
291291
}
292292
ty::ty_opaque_closure_ptr(_) => Type::opaque_box(cx).ptr_to(),
293293
ty::ty_struct(did, ref substs) => {
@@ -299,7 +299,9 @@ pub fn type_of(cx: &mut CrateContext, t: ty::t) -> Type {
299299
// Only create the named struct, but don't fill it in. We fill it
300300
// in *after* placing it into the type cache. This prevents
301301
// infinite recursion with recursive struct types.
302-
Type::named_struct(llvm_type_name(cx, a_struct, did, substs.tps))
302+
let repr = adt::represent_type(cx, t);
303+
let name = llvm_type_name(cx, a_struct, did, substs.tps);
304+
adt::incomplete_type_of(cx, repr, name)
303305
}
304306
}
305307
ty::ty_self(*) => cx.tcx.sess.unimpl("type_of: ty_self"),
@@ -316,19 +318,11 @@ pub fn type_of(cx: &mut CrateContext, t: ty::t) -> Type {
316318

317319
// If this was an enum or struct, fill in the type now.
318320
match ty::get(t).sty {
319-
ty::ty_enum(*) => {
320-
let repr = adt::represent_type(cx, t);
321-
llty.set_struct_body(adt::fields_of(cx, repr), false);
322-
}
323-
324-
ty::ty_struct(did, _) => {
325-
if !ty::type_is_simd(cx.tcx, t) {
326-
let repr = adt::represent_type(cx, t);
327-
let packed = ty::lookup_packed(cx.tcx, did);
328-
llty.set_struct_body(adt::fields_of(cx, repr), packed);
321+
ty::ty_enum(*) | ty::ty_struct(*) if !ty::type_is_simd(cx.tcx, t) => {
322+
let repr = adt::represent_type(cx, t);
323+
adt::finish_type_of(cx, repr, &mut llty);
329324
}
330-
}
331-
_ => ()
325+
_ => ()
332326
}
333327

334328
return llty;
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
/*!
12+
* C-like enums have to be represented as LLVM ints, not wrapped in a
13+
* struct, because it's important for the FFI that they interoperate
14+
* with C integers/enums, and the ABI can treat structs differently.
15+
* For example, on i686-linux-gnu, a struct return value is passed by
16+
* storing to a hidden out parameter, whereas an integer would be
17+
* returned in a register.
18+
*
19+
* This test just checks that the ABIs for the enum and the plain
20+
* integer are compatible, rather than actually calling C code.
21+
* The unused parameter to `foo` is to increase the likelihood of
22+
* crashing if something goes wrong here.
23+
*/
24+
25+
#[repr(u32)]
26+
enum Foo {
27+
A = 0,
28+
B = 23
29+
}
30+
31+
#[inline(never)]
32+
extern "C" fn foo(_x: uint) -> Foo { B }
33+
34+
pub fn main() {
35+
unsafe {
36+
let f: extern "C" fn(uint) -> u32 = ::std::cast::transmute(foo);
37+
assert_eq!(f(0xDEADBEEF), B as u32);
38+
}
39+
}

0 commit comments

Comments
 (0)