Skip to content

Commit dfe8bd1

Browse files
Move ty_align and ty_size out of most C ABI code
s390x's C ABI ty_align and ty_size are not moved because the implementation of ty_align varies in an atypical pattern: it calls ty_size for the llvm::Vector type kind. ty_size then cannot be moved since it indirectly calls ty_align through align.
1 parent 05c2fdd commit dfe8bd1

9 files changed

+103
-395
lines changed

src/librustc_trans/abi.rs

+71-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use llvm::{self, ValueRef};
11+
use llvm::{self, ValueRef, Integer, Pointer, Float, Double, Struct, Array, Vector};
1212
use base;
1313
use build::AllocaFcx;
1414
use common::{type_is_fat_ptr, BlockAndBuilder, C_uint};
@@ -598,3 +598,73 @@ impl FnType {
598598
}
599599
}
600600
}
601+
602+
pub fn align_up_to(off: usize, a: usize) -> usize {
603+
return (off + a - 1) / a * a;
604+
}
605+
606+
fn align(off: usize, ty: Type, pointer: usize) -> usize {
607+
let a = ty_align(ty, pointer);
608+
return align_up_to(off, a);
609+
}
610+
611+
pub fn ty_align(ty: Type, pointer: usize) -> usize {
612+
match ty.kind() {
613+
Integer => ((ty.int_width() as usize) + 7) / 8,
614+
Pointer => pointer,
615+
Float => 4,
616+
Double => 8,
617+
Struct => {
618+
if ty.is_packed() {
619+
1
620+
} else {
621+
let str_tys = ty.field_types();
622+
str_tys.iter().fold(1, |a, t| cmp::max(a, ty_align(*t, pointer)))
623+
}
624+
}
625+
Array => {
626+
let elt = ty.element_type();
627+
ty_align(elt, pointer)
628+
}
629+
Vector => {
630+
let len = ty.vector_length();
631+
let elt = ty.element_type();
632+
ty_align(elt, pointer) * len
633+
}
634+
_ => bug!("ty_align: unhandled type")
635+
}
636+
}
637+
638+
pub fn ty_size(ty: Type, pointer: usize) -> usize {
639+
match ty.kind() {
640+
Integer => ((ty.int_width() as usize) + 7) / 8,
641+
Pointer => pointer,
642+
Float => 4,
643+
Double => 8,
644+
Struct => {
645+
if ty.is_packed() {
646+
let str_tys = ty.field_types();
647+
str_tys.iter().fold(0, |s, t| s + ty_size(*t, pointer))
648+
} else {
649+
let str_tys = ty.field_types();
650+
let size = str_tys.iter().fold(0, |s, t| {
651+
align(s, *t, pointer) + ty_size(*t, pointer)
652+
});
653+
align(size, ty, pointer)
654+
}
655+
}
656+
Array => {
657+
let len = ty.array_length();
658+
let elt = ty.element_type();
659+
let eltsz = ty_size(elt, pointer);
660+
len * eltsz
661+
}
662+
Vector => {
663+
let len = ty.vector_length();
664+
let elt = ty.element_type();
665+
let eltsz = ty_size(elt, pointer);
666+
len * eltsz
667+
},
668+
_ => bug!("ty_size: unhandled type")
669+
}
670+
}

src/librustc_trans/cabi_aarch64.rs

+2-68
Original file line numberDiff line numberDiff line change
@@ -11,78 +11,12 @@
1111
#![allow(non_upper_case_globals)]
1212

1313
use llvm::{Integer, Pointer, Float, Double, Struct, Array, Vector};
14-
use abi::{FnType, ArgType};
14+
use abi::{self, FnType, ArgType};
1515
use context::CrateContext;
1616
use type_::Type;
1717

18-
use std::cmp;
19-
20-
fn align_up_to(off: usize, a: usize) -> usize {
21-
return (off + a - 1) / a * a;
22-
}
23-
24-
fn align(off: usize, ty: Type) -> usize {
25-
let a = ty_align(ty);
26-
return align_up_to(off, a);
27-
}
28-
29-
fn ty_align(ty: Type) -> usize {
30-
match ty.kind() {
31-
Integer => ((ty.int_width() as usize) + 7) / 8,
32-
Pointer => 8,
33-
Float => 4,
34-
Double => 8,
35-
Struct => {
36-
if ty.is_packed() {
37-
1
38-
} else {
39-
let str_tys = ty.field_types();
40-
str_tys.iter().fold(1, |a, t| cmp::max(a, ty_align(*t)))
41-
}
42-
}
43-
Array => {
44-
let elt = ty.element_type();
45-
ty_align(elt)
46-
}
47-
Vector => {
48-
let len = ty.vector_length();
49-
let elt = ty.element_type();
50-
ty_align(elt) * len
51-
}
52-
_ => bug!("ty_align: unhandled type")
53-
}
54-
}
55-
5618
fn ty_size(ty: Type) -> usize {
57-
match ty.kind() {
58-
Integer => ((ty.int_width() as usize) + 7) / 8,
59-
Pointer => 8,
60-
Float => 4,
61-
Double => 8,
62-
Struct => {
63-
if ty.is_packed() {
64-
let str_tys = ty.field_types();
65-
str_tys.iter().fold(0, |s, t| s + ty_size(*t))
66-
} else {
67-
let str_tys = ty.field_types();
68-
let size = str_tys.iter().fold(0, |s, t| align(s, *t) + ty_size(*t));
69-
align(size, ty)
70-
}
71-
}
72-
Array => {
73-
let len = ty.array_length();
74-
let elt = ty.element_type();
75-
let eltsz = ty_size(elt);
76-
len * eltsz
77-
}
78-
Vector => {
79-
let len = ty.vector_length();
80-
let elt = ty.element_type();
81-
let eltsz = ty_size(elt);
82-
len * eltsz
83-
}
84-
_ => bug!("ty_size: unhandled type")
85-
}
19+
abi::ty_size(ty, 8)
8620
}
8721

8822
fn is_homogenous_aggregate_ty(ty: Type) -> Option<(Type, u64)> {

src/librustc_trans/cabi_arm.rs

+2-29
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#![allow(non_upper_case_globals)]
1212

1313
use llvm::{Integer, Pointer, Float, Double, Struct, Array, Vector};
14-
use abi::{FnType, ArgType};
14+
use abi::{self, align_up_to, FnType, ArgType};
1515
use context::CrateContext;
1616
use type_::Type;
1717

@@ -24,40 +24,13 @@ pub enum Flavor {
2424

2525
type TyAlignFn = fn(ty: Type) -> usize;
2626

27-
fn align_up_to(off: usize, a: usize) -> usize {
28-
return (off + a - 1) / a * a;
29-
}
30-
3127
fn align(off: usize, ty: Type, align_fn: TyAlignFn) -> usize {
3228
let a = align_fn(ty);
3329
return align_up_to(off, a);
3430
}
3531

3632
fn general_ty_align(ty: Type) -> usize {
37-
match ty.kind() {
38-
Integer => ((ty.int_width() as usize) + 7) / 8,
39-
Pointer => 4,
40-
Float => 4,
41-
Double => 8,
42-
Struct => {
43-
if ty.is_packed() {
44-
1
45-
} else {
46-
let str_tys = ty.field_types();
47-
str_tys.iter().fold(1, |a, t| cmp::max(a, general_ty_align(*t)))
48-
}
49-
}
50-
Array => {
51-
let elt = ty.element_type();
52-
general_ty_align(elt)
53-
}
54-
Vector => {
55-
let len = ty.vector_length();
56-
let elt = ty.element_type();
57-
general_ty_align(elt) * len
58-
}
59-
_ => bug!("ty_align: unhandled type")
60-
}
33+
abi::ty_align(ty, 4)
6134
}
6235

6336
// For more information see:

src/librustc_trans/cabi_mips.rs

+4-64
Original file line numberDiff line numberDiff line change
@@ -13,77 +13,17 @@
1313
use libc::c_uint;
1414
use std::cmp;
1515
use llvm;
16-
use llvm::{Integer, Pointer, Float, Double, Struct, Array, Vector};
17-
use abi::{ArgType, FnType};
16+
use llvm::{Integer, Pointer, Float, Double, Vector};
17+
use abi::{self, align_up_to, ArgType, FnType};
1818
use context::CrateContext;
1919
use type_::Type;
2020

21-
fn align_up_to(off: usize, a: usize) -> usize {
22-
return (off + a - 1) / a * a;
23-
}
24-
25-
fn align(off: usize, ty: Type) -> usize {
26-
let a = ty_align(ty);
27-
return align_up_to(off, a);
28-
}
29-
3021
fn ty_align(ty: Type) -> usize {
31-
match ty.kind() {
32-
Integer => ((ty.int_width() as usize) + 7) / 8,
33-
Pointer => 4,
34-
Float => 4,
35-
Double => 8,
36-
Struct => {
37-
if ty.is_packed() {
38-
1
39-
} else {
40-
let str_tys = ty.field_types();
41-
str_tys.iter().fold(1, |a, t| cmp::max(a, ty_align(*t)))
42-
}
43-
}
44-
Array => {
45-
let elt = ty.element_type();
46-
ty_align(elt)
47-
}
48-
Vector => {
49-
let len = ty.vector_length();
50-
let elt = ty.element_type();
51-
ty_align(elt) * len
52-
}
53-
_ => bug!("ty_align: unhandled type")
54-
}
22+
abi::ty_align(ty, 4)
5523
}
5624

5725
fn ty_size(ty: Type) -> usize {
58-
match ty.kind() {
59-
Integer => ((ty.int_width() as usize) + 7) / 8,
60-
Pointer => 4,
61-
Float => 4,
62-
Double => 8,
63-
Struct => {
64-
if ty.is_packed() {
65-
let str_tys = ty.field_types();
66-
str_tys.iter().fold(0, |s, t| s + ty_size(*t))
67-
} else {
68-
let str_tys = ty.field_types();
69-
let size = str_tys.iter().fold(0, |s, t| align(s, *t) + ty_size(*t));
70-
align(size, ty)
71-
}
72-
}
73-
Array => {
74-
let len = ty.array_length();
75-
let elt = ty.element_type();
76-
let eltsz = ty_size(elt);
77-
len * eltsz
78-
}
79-
Vector => {
80-
let len = ty.vector_length();
81-
let elt = ty.element_type();
82-
let eltsz = ty_size(elt);
83-
len * eltsz
84-
}
85-
_ => bug!("ty_size: unhandled type")
86-
}
26+
abi::ty_size(ty, 4)
8727
}
8828

8929
fn classify_ret_ty(ccx: &CrateContext, ret: &mut ArgType) {

src/librustc_trans/cabi_mips64.rs

+4-64
Original file line numberDiff line numberDiff line change
@@ -13,77 +13,17 @@
1313
use libc::c_uint;
1414
use std::cmp;
1515
use llvm;
16-
use llvm::{Integer, Pointer, Float, Double, Struct, Array, Vector};
17-
use abi::{ArgType, FnType};
16+
use llvm::{Integer, Pointer, Float, Double, Vector};
17+
use abi::{self, align_up_to, ArgType, FnType};
1818
use context::CrateContext;
1919
use type_::Type;
2020

21-
fn align_up_to(off: usize, a: usize) -> usize {
22-
return (off + a - 1) / a * a;
23-
}
24-
25-
fn align(off: usize, ty: Type) -> usize {
26-
let a = ty_align(ty);
27-
return align_up_to(off, a);
28-
}
29-
3021
fn ty_align(ty: Type) -> usize {
31-
match ty.kind() {
32-
Integer => ((ty.int_width() as usize) + 7) / 8,
33-
Pointer => 8,
34-
Float => 4,
35-
Double => 8,
36-
Struct => {
37-
if ty.is_packed() {
38-
1
39-
} else {
40-
let str_tys = ty.field_types();
41-
str_tys.iter().fold(1, |a, t| cmp::max(a, ty_align(*t)))
42-
}
43-
}
44-
Array => {
45-
let elt = ty.element_type();
46-
ty_align(elt)
47-
}
48-
Vector => {
49-
let len = ty.vector_length();
50-
let elt = ty.element_type();
51-
ty_align(elt) * len
52-
}
53-
_ => bug!("ty_align: unhandled type")
54-
}
22+
abi::ty_align(ty, 8)
5523
}
5624

5725
fn ty_size(ty: Type) -> usize {
58-
match ty.kind() {
59-
Integer => ((ty.int_width() as usize) + 7) / 8,
60-
Pointer => 8,
61-
Float => 4,
62-
Double => 8,
63-
Struct => {
64-
if ty.is_packed() {
65-
let str_tys = ty.field_types();
66-
str_tys.iter().fold(0, |s, t| s + ty_size(*t))
67-
} else {
68-
let str_tys = ty.field_types();
69-
let size = str_tys.iter().fold(0, |s, t| align(s, *t) + ty_size(*t));
70-
align(size, ty)
71-
}
72-
}
73-
Array => {
74-
let len = ty.array_length();
75-
let elt = ty.element_type();
76-
let eltsz = ty_size(elt);
77-
len * eltsz
78-
}
79-
Vector => {
80-
let len = ty.vector_length();
81-
let elt = ty.element_type();
82-
let eltsz = ty_size(elt);
83-
len * eltsz
84-
}
85-
_ => bug!("ty_size: unhandled type")
86-
}
26+
abi::ty_size(ty, 8)
8727
}
8828

8929
fn classify_ret_ty(ccx: &CrateContext, ret: &mut ArgType) {

0 commit comments

Comments
 (0)