Skip to content

Commit 93a7f23

Browse files
committed
auto merge of #5115 : sanxiyn/rust/arm-abi, r=brson
Type size and alignment code needs to be factored, but I didn't want to interfere with MIPS port. Can be done later. Fix #4797.
2 parents a8f07dc + 5098cf5 commit 93a7f23

File tree

3 files changed

+168
-1
lines changed

3 files changed

+168
-1
lines changed

src/librustc/middle/trans/cabi_arm.rs

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
// Copyright 2012-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+
use lib::llvm::{llvm, Integer, Pointer, Float, Double, Struct, Array};
12+
use lib::llvm::struct_tys;
13+
use lib::llvm::TypeRef;
14+
use lib::llvm::{Attribute, StructRetAttribute};
15+
use middle::trans::cabi::{ABIInfo, FnType, LLVMType};
16+
use middle::trans::common::{T_i8, T_i16, T_i32, T_i64};
17+
use middle::trans::common::{T_array, T_ptr, T_void};
18+
19+
use core::option::{Option, None, Some};
20+
use core::uint;
21+
use core::vec;
22+
23+
fn align_up_to(off: uint, a: uint) -> uint {
24+
return (off + a - 1u) / a * a;
25+
}
26+
27+
fn align(off: uint, ty: TypeRef) -> uint {
28+
let a = ty_align(ty);
29+
return align_up_to(off, a);
30+
}
31+
32+
fn ty_align(ty: TypeRef) -> uint {
33+
unsafe {
34+
return match llvm::LLVMGetTypeKind(ty) {
35+
Integer => {
36+
((llvm::LLVMGetIntTypeWidth(ty) as uint) + 7) / 8
37+
}
38+
Pointer => 4,
39+
Float => 4,
40+
Double => 8,
41+
Struct => {
42+
do vec::foldl(1, struct_tys(ty)) |a, t| {
43+
uint::max(a, ty_align(*t))
44+
}
45+
}
46+
Array => {
47+
let elt = llvm::LLVMGetElementType(ty);
48+
ty_align(elt)
49+
}
50+
_ => fail!(~"ty_align: unhandled type")
51+
};
52+
}
53+
}
54+
55+
fn ty_size(ty: TypeRef) -> uint {
56+
unsafe {
57+
return match llvm::LLVMGetTypeKind(ty) {
58+
Integer => {
59+
((llvm::LLVMGetIntTypeWidth(ty) as uint) + 7) / 8
60+
}
61+
Pointer => 4,
62+
Float => 4,
63+
Double => 8,
64+
Struct => {
65+
let size = do vec::foldl(0, struct_tys(ty)) |s, t| {
66+
align(s, *t) + ty_size(*t)
67+
};
68+
align(size, ty)
69+
}
70+
Array => {
71+
let len = llvm::LLVMGetArrayLength(ty) as uint;
72+
let elt = llvm::LLVMGetElementType(ty);
73+
let eltsz = ty_size(elt);
74+
len * eltsz
75+
}
76+
_ => fail!(~"ty_size: unhandled type")
77+
};
78+
}
79+
}
80+
81+
fn classify_ret_ty(ty: TypeRef) -> (LLVMType, Option<Attribute>) {
82+
if is_reg_ty(ty) {
83+
return (LLVMType { cast: false, ty: ty }, None);
84+
}
85+
let size = ty_size(ty);
86+
if size <= 4 {
87+
let llty = if size <= 1 {
88+
T_i8()
89+
} else if size <= 2 {
90+
T_i16()
91+
} else {
92+
T_i32()
93+
};
94+
return (LLVMType { cast: true, ty: llty }, None);
95+
}
96+
(LLVMType { cast: false, ty: T_ptr(ty) }, Some(StructRetAttribute))
97+
}
98+
99+
fn classify_arg_ty(ty: TypeRef) -> (LLVMType, Option<Attribute>) {
100+
if is_reg_ty(ty) {
101+
return (LLVMType { cast: false, ty: ty }, None);
102+
}
103+
let align = ty_align(ty);
104+
let size = ty_size(ty);
105+
let llty = if align <= 4 {
106+
T_array(T_i32(), (size + 3) / 4)
107+
} else {
108+
T_array(T_i64(), (size + 7) / 8)
109+
};
110+
(LLVMType { cast: true, ty: llty }, None)
111+
}
112+
113+
fn is_reg_ty(ty: TypeRef) -> bool {
114+
unsafe {
115+
return match llvm::LLVMGetTypeKind(ty) {
116+
Integer
117+
| Pointer
118+
| Float
119+
| Double => true,
120+
_ => false
121+
};
122+
}
123+
}
124+
125+
enum ARM_ABIInfo { ARM_ABIInfo }
126+
127+
impl ABIInfo for ARM_ABIInfo {
128+
fn compute_info(&self,
129+
atys: &[TypeRef],
130+
rty: TypeRef,
131+
ret_def: bool) -> FnType {
132+
let mut arg_tys = ~[];
133+
let mut attrs = ~[];
134+
for atys.each |&aty| {
135+
let (ty, attr) = classify_arg_ty(aty);
136+
arg_tys.push(ty);
137+
attrs.push(attr);
138+
}
139+
140+
let mut (ret_ty, ret_attr) = if ret_def {
141+
classify_ret_ty(rty)
142+
} else {
143+
(LLVMType { cast: false, ty: T_void() }, None)
144+
};
145+
146+
let sret = ret_attr.is_some();
147+
if sret {
148+
arg_tys.unshift(ret_ty);
149+
attrs.unshift(ret_attr);
150+
ret_ty = LLVMType { cast: false, ty: T_void() };
151+
}
152+
153+
return FnType {
154+
arg_tys: arg_tys,
155+
ret_ty: ret_ty,
156+
attrs: attrs,
157+
sret: sret
158+
};
159+
}
160+
}
161+
162+
pub fn abi_info() -> ABIInfo {
163+
return ARM_ABIInfo as ABIInfo;
164+
}

src/librustc/middle/trans/foreign.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use lib;
2121
use middle::trans::base::*;
2222
use middle::trans::cabi;
2323
use middle::trans::cabi_x86_64::*;
24+
use middle::trans::cabi_arm;
2425
use middle::trans::build::*;
2526
use middle::trans::callee::*;
2627
use middle::trans::common::*;
@@ -42,7 +43,8 @@ use syntax::parse::token::special_idents;
4243

4344
fn abi_info(arch: session::arch) -> cabi::ABIInfo {
4445
return match arch {
45-
arch_x86_64 | arch_arm => x86_64_abi_info(),
46+
arch_x86_64 => x86_64_abi_info(),
47+
arch_arm => cabi_arm::abi_info(),
4648
_ => cabi::llvm_abi_info()
4749
}
4850
}

src/librustc/rustc.rc

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub mod middle {
6868
pub mod meth;
6969
pub mod cabi;
7070
pub mod cabi_x86_64;
71+
pub mod cabi_arm;
7172
pub mod foreign;
7273
pub mod reflect;
7374
pub mod shape;

0 commit comments

Comments
 (0)