@@ -412,20 +412,15 @@ fn llalign_of(cx: @crate_ctxt, t: TypeRef) -> ValueRef {
412
412
}
413
413
414
414
fn size_of ( cx : @block_ctxt , t : ty:: t ) -> result {
415
- size_of_ ( cx, t, align_total )
415
+ size_of_ ( cx, t)
416
416
}
417
417
418
- tag align_mode {
419
- align_total;
420
- align_next ( ty:: t) ;
421
- }
422
-
423
- fn size_of_ ( cx : @block_ctxt , t : ty:: t , mode : align_mode ) -> result {
418
+ fn size_of_ ( cx : @block_ctxt , t : ty:: t ) -> result {
424
419
let ccx = bcx_ccx ( cx) ;
425
420
if check type_has_static_size ( ccx, t) {
426
421
let sp = cx. sp ;
427
422
rslt ( cx, llsize_of ( bcx_ccx ( cx) , type_of ( ccx, sp, t) ) )
428
- } else { dynamic_size_of ( cx, t, mode ) }
423
+ } else { dynamic_size_of ( cx, t) }
429
424
}
430
425
431
426
fn align_of ( cx : @block_ctxt , t : ty:: t ) -> result {
@@ -536,9 +531,8 @@ fn static_size_of_tag(cx: @crate_ctxt, sp: span, t: ty::t)
536
531
}
537
532
}
538
533
539
- fn dynamic_size_of ( cx : @block_ctxt , t : ty:: t , mode : align_mode ) -> result {
540
- fn align_elements ( cx : @block_ctxt , elts : [ ty:: t ] ,
541
- mode : align_mode ) -> result {
534
+ fn dynamic_size_of ( cx : @block_ctxt , t : ty:: t ) -> result {
535
+ fn align_elements ( cx : @block_ctxt , elts : [ ty:: t ] ) -> result {
542
536
//
543
537
// C padding rules:
544
538
//
@@ -560,15 +554,16 @@ fn dynamic_size_of(cx: @block_ctxt, t: ty::t, mode: align_mode) -> result {
560
554
off = Add ( bcx, aligned_off, elt_size. val ) ;
561
555
max_align = umax ( bcx, max_align, elt_align. val ) ;
562
556
}
563
- off = alt mode {
564
- align_total. {
565
- align_to( bcx, off, max_align)
566
- }
567
- align_next ( t) {
568
- let { bcx, val: align } = align_of ( bcx, t) ;
569
- align_to ( bcx, off, align)
570
- }
571
- } ;
557
+ off = align_to ( bcx, off, max_align) ;
558
+ //off = alt mode {
559
+ // align_total. {
560
+ // align_to(bcx, off, max_align)
561
+ // }
562
+ // align_next(t) {
563
+ // let {bcx, val: align} = align_of(bcx, t);
564
+ // align_to(bcx, off, align)
565
+ // }
566
+ //};
572
567
ret rslt( bcx, off) ;
573
568
}
574
569
alt ty:: struct ( bcx_tcx ( cx) , t) {
@@ -579,12 +574,12 @@ fn dynamic_size_of(cx: @block_ctxt, t: ty::t, mode: align_mode) -> result {
579
574
ty:: ty_rec ( flds) {
580
575
let tys: [ ty:: t ] = [ ] ;
581
576
for f: ty:: field in flds { tys += [ f. mt . ty ] ; }
582
- ret align_elements ( cx, tys, mode ) ;
577
+ ret align_elements ( cx, tys) ;
583
578
}
584
579
ty:: ty_tup ( elts) {
585
580
let tys = [ ] ;
586
581
for tp in elts { tys += [ tp] ; }
587
- ret align_elements ( cx, tys, mode ) ;
582
+ ret align_elements ( cx, tys) ;
588
583
}
589
584
ty:: ty_tag ( tid, tps) {
590
585
let bcx = cx;
@@ -603,7 +598,7 @@ fn dynamic_size_of(cx: @block_ctxt, t: ty::t, mode: align_mode) -> result {
603
598
let t = ty:: substitute_type_params ( bcx_tcx ( cx) , tps, raw_ty) ;
604
599
tys += [ t] ;
605
600
}
606
- let rslt = align_elements ( bcx, tys, mode ) ;
601
+ let rslt = align_elements ( bcx, tys) ;
607
602
bcx = rslt. bcx ;
608
603
let this_size = rslt. val ;
609
604
let old_max_size = Load ( bcx, max_size) ;
@@ -689,84 +684,55 @@ fn GEP_tup_like_1(cx: @block_ctxt, t: ty::t, base: ValueRef, ixs: [int])
689
684
// above.
690
685
fn GEP_tup_like ( bcx : @block_ctxt , t : ty:: t , base : ValueRef , ixs : [ int ] )
691
686
: type_is_tup_like ( bcx , t ) -> result {
692
- // It might be a static-known type. Handle this.
693
- if !ty:: type_has_dynamic_size ( bcx_tcx ( bcx) , t) {
694
- #debug[ "GEP_tup_like t=%? ixs=%? -> static" ,
695
- ty_to_str ( bcx_tcx ( bcx) , t) , ixs] ;
696
687
697
- ret rslt ( bcx, GEPi ( bcx, base, ixs) ) ;
698
- }
699
- // It is a dynamic-containing type that, if we convert directly to an LLVM
700
- // TypeRef, will be all wrong; there's no proper LLVM type to represent
701
- // it, and the lowering function will stick in i8* values for each
702
- // ty_param, which is not right; the ty_params are all of some dynamic
703
- // size.
704
- //
705
- // What we must do instead is sadder. We must look through the indices
706
- // manually and split the input type into a prefix and a target. We then
707
- // measure the prefix size, bump the input pointer by that amount, and
708
- // cast to a pointer-to-target type.
709
-
710
- // Given a type, an index vector and an element number N in that vector,
711
- // calculate index X and the type that results by taking the first X-1
712
- // elements of the type and splitting the Xth off. Return the prefix as
713
- // well as the innermost Xth type.
714
-
715
- fn split_type ( ccx : @crate_ctxt , t : ty:: t , ixs : [ int ] , n : uint ) ->
716
- { prefix : [ ty:: t ] , target : ty:: t } {
717
- let len: uint = vec:: len :: < int > ( ixs) ;
718
- // We don't support 0-index or 1-index GEPs: The former is nonsense
719
- // and the latter would only be meaningful if we supported non-0
720
- // values for the 0th index (we don't).
721
-
722
- assert ( len > 1 u) ;
723
- if n == 0 u {
724
- // Since we're starting from a value that's a pointer to a
725
- // *single* structure, the first index (in GEP-ese) should just be
726
- // 0, to yield the pointee.
727
-
728
- assert ( ixs[ n] == 0 ) ;
729
- ret split_type( ccx, t, ixs, n + 1 u) ;
688
+ fn compute_off ( bcx : @block_ctxt ,
689
+ off : ValueRef ,
690
+ t : ty:: t ,
691
+ ixs : [ int ] ,
692
+ n : uint ) -> ( @block_ctxt , ValueRef , ty:: t ) {
693
+ if n == vec:: len ( ixs) {
694
+ ret ( bcx, off, t) ;
730
695
}
731
- assert ( n < len) ;
732
- let ix: int = ixs[ n] ;
733
- let prefix: [ ty:: t ] = [ ] ;
734
- let i: int = 0 ;
735
- while i < ix {
736
- prefix += [ ty:: get_element_type ( ccx. tcx , t, i as uint ) ] ;
737
- i += 1 ;
696
+
697
+ let tcx = bcx_tcx ( bcx) ;
698
+ let ix = ixs[ n] ;
699
+ let bcx = bcx, off = off;
700
+ int:: range ( 0 , ix) { |i|
701
+ let comp_t = ty:: get_element_type ( tcx, t, i as uint ) ;
702
+ let align = align_of ( bcx, comp_t) ;
703
+ bcx = align. bcx ;
704
+ off = align_to ( bcx, off, align. val ) ;
705
+ let sz = size_of ( bcx, comp_t) ;
706
+ bcx = sz. bcx ;
707
+ off = Add ( bcx, off, sz. val ) ;
738
708
}
739
- let selected = ty:: get_element_type ( ccx. tcx , t, i as uint ) ;
740
- if n == len - 1 u {
741
- // We are at the innermost index.
742
709
743
- ret { prefix : prefix, target : selected} ;
744
- } else {
745
- // Not the innermost index; call self recursively to dig deeper.
746
- // Once we get an inner result, append it current prefix and
747
- // return to caller.
710
+ let comp_t = ty:: get_element_type ( tcx, t, ix as uint ) ;
711
+ let align = align_of ( bcx, comp_t) ;
712
+ bcx = align. bcx ;
713
+ off = align_to ( bcx, off, align. val ) ;
748
714
749
- let inner = split_type ( ccx, selected, ixs, n + 1 u) ;
750
- prefix += inner. prefix ;
751
- ret { prefix : prefix with inner} ;
752
- }
715
+ be compute_off ( bcx, off, comp_t, ixs, n+1 u) ;
753
716
}
754
- // We make a fake prefix tuple-type here; luckily for measuring sizes
755
- // the tuple parens are associative so it doesn't matter that we've
756
- // flattened the incoming structure.
757
717
758
- let s = split_type ( bcx_ccx ( bcx) , t, ixs, 0 u) ;
718
+ if !ty:: type_has_dynamic_size ( bcx_tcx ( bcx) , t) {
719
+ ret rslt ( bcx, GEPi ( bcx, base, ixs) ) ;
720
+ }
759
721
760
- let args = [ ] ;
761
- for typ: ty:: t in s. prefix { args += [ typ] ; }
762
- let prefix_ty = ty:: mk_tup ( bcx_tcx ( bcx) , args) ;
722
+ #debug[ "GEP_tup_like(t=%s,base=%s,ixs=%?)" ,
723
+ ty_to_str ( bcx_tcx ( bcx) , t) ,
724
+ val_str ( bcx_ccx ( bcx) . tn , base) ,
725
+ ixs] ;
763
726
764
- #debug[ "GEP_tup_like t=%? ixs=%? prefix_ty=%?" ,
765
- ty_to_str ( bcx_tcx ( bcx) , t) , ixs,
766
- ty_to_str ( bcx_tcx ( bcx) , prefix_ty) ] ;
727
+ // We require that ixs start with 0 and we expect the input to be a
728
+ // pointer to an instance of type t, so we can safely ignore ixs[0],
729
+ // basically.
730
+ assert ixs[ 0 ] == 0 ;
767
731
768
- let sz = size_of_ ( bcx, prefix_ty, align_next ( s. target ) ) ;
769
- ret rslt( sz. bcx , bump_ptr ( sz. bcx , s. target , base, sz. val ) ) ;
732
+ let ( bcx, off, tar_t) = {
733
+ compute_off ( bcx, C_int ( bcx_ccx ( bcx) , 0 ) , t, ixs, 1 u)
734
+ } ;
735
+ ret rslt( bcx, bump_ptr ( bcx, tar_t, base, off) ) ;
770
736
}
771
737
772
738
0 commit comments