Skip to content

Commit 9f3b12b

Browse files
committed
Change llsize_real to use LLVMSizeOfTypeInBits. Add comments.
1 parent b751996 commit 9f3b12b

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

src/rustc/lib/llvm.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,9 +783,16 @@ native mod llvm {
783783
/** Adds the target data to the given pass manager. The pass manager
784784
references the target data only weakly. */
785785
fn LLVMAddTargetData(TD: TargetDataRef, PM: PassManagerRef);
786-
/** Returns the size of a type. */
786+
/** Number of bytes clobbered when doing a Store to *T. */
787787
fn LLVMStoreSizeOfType(TD: TargetDataRef, Ty: TypeRef) -> c_ulonglong;
788+
789+
/** Number of bytes clobbered when doing a Store to *T. */
790+
fn LLVMSizeOfTypeInBits(TD: TargetDataRef, Ty: TypeRef) -> c_ulonglong;
791+
792+
/** Distance between successive elements in an array of T.
793+
Includes ABI padding. */
788794
fn LLVMABISizeOfType(TD: TargetDataRef, Ty: TypeRef) -> c_uint;
795+
789796
/** Returns the preferred alignment of a type. */
790797
fn LLVMPreferredAlignmentOfType(TD: TargetDataRef,
791798
Ty: TypeRef) -> c_uint;

src/rustc/middle/trans/reflect.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ impl methods for reflector {
3131
do_spill_noroot(self.bcx, ss)
3232
}
3333

34+
fn c_size_and_align(t: ty::t) -> [ValueRef] {
35+
let tr = type_of::type_of(self.bcx.ccx(), t);
36+
let s = shape::llsize_of_real(self.bcx.ccx(), tr);
37+
let a = shape::llalign_of_min(self.bcx.ccx(), tr);
38+
ret [self.c_uint(s),
39+
self.c_uint(a)];
40+
}
41+
3442
fn visit(ty_name: str, args: [ValueRef]) {
3543
let tcx = self.bcx.tcx();
3644
let mth_idx = option::get(ty::method_idx("visit_" + ty_name,

src/rustc/middle/trans/shape.rs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -635,33 +635,60 @@ type tag_metrics = {
635635
payload_align: ValueRef
636636
};
637637

638-
// Returns the real size of the given type for the current target.
639-
fn llsize_of_real(cx: @crate_ctxt, t: TypeRef) -> uint {
638+
// Returns the number of bytes clobbered by a Store to this type.
639+
fn llsize_of_store(cx: @crate_ctxt, t: TypeRef) -> uint {
640640
ret llvm::LLVMStoreSizeOfType(cx.td.lltd, t) as uint;
641641
}
642642

643+
// Returns the number of bytes between successive elements of type T in an
644+
// array of T. This is the "ABI" size. It includes any ABI-mandated padding.
643645
fn llsize_of_alloc(cx: @crate_ctxt, t: TypeRef) -> uint {
644646
ret llvm::LLVMABISizeOfType(cx.td.lltd, t) as uint;
645647
}
646648

649+
// Returns, as near as we can figure, the "real" size of a type. As in, the
650+
// bits in this number of bytes actually carry data related to the datum
651+
// with the type. Not junk, padding, accidentally-damaged words, or
652+
// whatever. Rounds up to the nearest byte though, so if you have a 1-bit
653+
// value, we return 1 here, not 0. Most of rustc works in bytes.
654+
fn llsize_of_real(cx: @crate_ctxt, t: TypeRef) -> uint {
655+
let nbits = llvm::LLVMSizeOfTypeInBits(cx.td.lltd, t) as uint;
656+
if nbits & 7u != 0u {
657+
// Not an even number of bytes, spills into "next" byte.
658+
1u + (nbits >> 3)
659+
} else {
660+
nbits >> 3
661+
}
662+
}
663+
664+
// Returns the "default" size of t, which is calculated by casting null to a
665+
// *T and then doing gep(1) on it and measuring the result. Really, look in
666+
// the LLVM sources. It does that. So this is likely similar to the ABI size
667+
// (i.e. including alignment-padding), but goodness knows which alignment it
668+
// winds up using. Probably the ABI one? Not recommended.
669+
fn llsize_of(cx: @crate_ctxt, t: TypeRef) -> ValueRef {
670+
ret llvm::LLVMConstIntCast(lib::llvm::llvm::LLVMSizeOf(t), cx.int_type,
671+
False);
672+
}
673+
647674
// Returns the preferred alignment of the given type for the current target.
648675
// The preffered alignment may be larger than the alignment used when
649-
// packing the type into structs
676+
// packing the type into structs. This will be used for things like
677+
// allocations inside a stack frame, which LLVM has a free hand in.
650678
fn llalign_of_pref(cx: @crate_ctxt, t: TypeRef) -> uint {
651679
ret llvm::LLVMPreferredAlignmentOfType(cx.td.lltd, t) as uint;
652680
}
653681

654682
// Returns the minimum alignment of a type required by the plattform.
655-
// This is the alignment that will be used for struct fields.
683+
// This is the alignment that will be used for struct fields, arrays,
684+
// and similar ABI-mandated things.
656685
fn llalign_of_min(cx: @crate_ctxt, t: TypeRef) -> uint {
657686
ret llvm::LLVMABIAlignmentOfType(cx.td.lltd, t) as uint;
658687
}
659688

660-
fn llsize_of(cx: @crate_ctxt, t: TypeRef) -> ValueRef {
661-
ret llvm::LLVMConstIntCast(lib::llvm::llvm::LLVMSizeOf(t), cx.int_type,
662-
False);
663-
}
664-
689+
// Returns the "default" alignment of t, which is calculated by casting
690+
// null to a record containing a single-bit followed by a t value, then
691+
// doing gep(0,1) to get at the trailing (and presumably padded) t cell.
665692
fn llalign_of(cx: @crate_ctxt, t: TypeRef) -> ValueRef {
666693
ret llvm::LLVMConstIntCast(lib::llvm::llvm::LLVMAlignOf(t), cx.int_type,
667694
False);

0 commit comments

Comments
 (0)