]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/type_.rs
Various minor/cosmetic improvements to code
[rust.git] / src / librustc_codegen_llvm / type_.rs
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 #![allow(non_upper_case_globals)]
12
13 pub use llvm::Type;
14
15 use llvm;
16 use llvm::{Bool, False, True};
17 use context::CodegenCx;
18 use rustc_codegen_ssa::traits::*;
19 use value::Value;
20
21 use rustc::util::nodemap::FxHashMap;
22 use rustc::ty::Ty;
23 use rustc::ty::layout::TyLayout;
24 use rustc_target::abi::call::{CastTarget, FnType, Reg};
25 use rustc_data_structures::small_c_str::SmallCStr;
26 use common;
27 use rustc_codegen_ssa::common::TypeKind;
28 use type_of::LayoutLlvmExt;
29 use abi::{LlvmType, FnTypeExt};
30
31 use std::fmt;
32 use std::cell::RefCell;
33
34 use libc::c_uint;
35
36 impl PartialEq for Type {
37     fn eq(&self, other: &Self) -> bool {
38         self as *const _ == other as *const _
39     }
40 }
41
42 impl fmt::Debug for Type {
43     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44         f.write_str(&llvm::build_string(|s| unsafe {
45             llvm::LLVMRustWriteTypeToString(self, s);
46         }).expect("non-UTF8 type description from LLVM"))
47     }
48 }
49
50 impl CodegenCx<'ll, 'tcx> {
51     crate fn type_named_struct(&self, name: &str) -> &'ll Type {
52         let name = SmallCStr::new(name);
53         unsafe {
54             llvm::LLVMStructCreateNamed(self.llcx, name.as_ptr())
55         }
56     }
57
58     crate fn set_struct_body(&self, ty: &'ll Type, els: &[&'ll Type], packed: bool) {
59         unsafe {
60             llvm::LLVMStructSetBody(ty, els.as_ptr(),
61                                     els.len() as c_uint, packed as Bool)
62         }
63     }
64 }
65
66 impl BaseTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
67     fn type_void(&self) -> &'ll Type {
68         unsafe {
69             llvm::LLVMVoidTypeInContext(self.llcx)
70         }
71     }
72
73     fn type_metadata(&self) -> &'ll Type {
74         unsafe {
75             llvm::LLVMRustMetadataTypeInContext(self.llcx)
76         }
77     }
78
79     fn type_i1(&self) -> &'ll Type {
80         unsafe {
81             llvm::LLVMInt1TypeInContext(self.llcx)
82         }
83     }
84
85     fn type_i8(&self) -> &'ll Type {
86         unsafe {
87             llvm::LLVMInt8TypeInContext(self.llcx)
88         }
89     }
90
91
92     fn type_i16(&self) -> &'ll Type {
93         unsafe {
94
95             llvm::LLVMInt16TypeInContext(self.llcx)
96         }
97     }
98
99     fn type_i32(&self) -> &'ll Type {
100         unsafe {
101             llvm::LLVMInt32TypeInContext(self.llcx)
102         }
103     }
104
105     fn type_i64(&self) -> &'ll Type {
106         unsafe {
107             llvm::LLVMInt64TypeInContext(self.llcx)
108         }
109     }
110
111     fn type_i128(&self) -> &'ll Type {
112         unsafe {
113             llvm::LLVMIntTypeInContext(self.llcx, 128)
114         }
115     }
116
117     fn type_ix(&self, num_bits: u64) -> &'ll Type {
118         unsafe {
119             llvm::LLVMIntTypeInContext(self.llcx, num_bits as c_uint)
120         }
121     }
122
123     fn type_isize(&self) -> &'ll Type {
124         self.isize_ty
125     }
126
127     fn type_f32(&self) -> &'ll Type {
128         unsafe {
129             llvm::LLVMFloatTypeInContext(self.llcx)
130         }
131     }
132
133     fn type_f64(&self) -> &'ll Type {
134         unsafe {
135             llvm::LLVMDoubleTypeInContext(self.llcx)
136         }
137     }
138
139     fn type_x86_mmx(&self) -> &'ll Type {
140         unsafe {
141             llvm::LLVMX86MMXTypeInContext(self.llcx)
142         }
143     }
144
145     fn type_func(
146         &self,
147         args: &[&'ll Type],
148         ret: &'ll Type
149     ) -> &'ll Type {
150         unsafe {
151             llvm::LLVMFunctionType(ret, args.as_ptr(),
152                                    args.len() as c_uint, False)
153         }
154     }
155
156     fn type_variadic_func(
157         &self,
158         args: &[&'ll Type],
159         ret: &'ll Type
160     ) -> &'ll Type {
161         unsafe {
162             llvm::LLVMFunctionType(ret, args.as_ptr(),
163                                    args.len() as c_uint, True)
164         }
165     }
166
167     fn type_struct(
168         &self,
169         els: &[&'ll Type],
170         packed: bool
171     ) -> &'ll Type {
172         unsafe {
173             llvm::LLVMStructTypeInContext(self.llcx, els.as_ptr(),
174                                           els.len() as c_uint,
175                                           packed as Bool)
176         }
177     }
178
179
180     fn type_array(&self, ty: &'ll Type, len: u64) -> &'ll Type {
181         unsafe {
182             llvm::LLVMRustArrayType(ty, len)
183         }
184     }
185
186     fn type_vector(&self, ty: &'ll Type, len: u64) -> &'ll Type {
187         unsafe {
188             llvm::LLVMVectorType(ty, len as c_uint)
189         }
190     }
191
192     fn type_kind(&self, ty: &'ll Type) -> TypeKind {
193         unsafe {
194             llvm::LLVMRustGetTypeKind(ty).to_generic()
195         }
196     }
197
198     fn type_ptr_to(&self, ty: &'ll Type) -> &'ll Type {
199         assert_ne!(self.type_kind(ty), TypeKind::Function,
200                    "don't call ptr_to on function types, use ptr_to_llvm_type on FnType instead");
201         ty.ptr_to()
202     }
203
204     fn element_type(&self, ty: &'ll Type) -> &'ll Type {
205         unsafe {
206             llvm::LLVMGetElementType(ty)
207         }
208     }
209
210     fn vector_length(&self, ty: &'ll Type) -> usize {
211         unsafe {
212             llvm::LLVMGetVectorSize(ty) as usize
213         }
214     }
215
216     fn func_params_types(&self, ty: &'ll Type) -> Vec<&'ll Type> {
217         unsafe {
218             let n_args = llvm::LLVMCountParamTypes(ty) as usize;
219             let mut args = Vec::with_capacity(n_args);
220             llvm::LLVMGetParamTypes(ty, args.as_mut_ptr());
221             args.set_len(n_args);
222             args
223         }
224     }
225
226     fn float_width(&self, ty: &'ll Type) -> usize {
227         match self.type_kind(ty) {
228             TypeKind::Float => 32,
229             TypeKind::Double => 64,
230             TypeKind::X86_FP80 => 80,
231             TypeKind::FP128 | TypeKind::PPC_FP128 => 128,
232             _ => bug!("llvm_float_width called on a non-float type")
233         }
234     }
235
236     fn int_width(&self, ty: &'ll Type) -> u64 {
237         unsafe {
238             llvm::LLVMGetIntTypeWidth(ty) as u64
239         }
240     }
241
242     fn val_ty(&self, v: &'ll Value) -> &'ll Type {
243         common::val_ty(v)
244     }
245
246     fn scalar_lltypes(&self) -> &RefCell<FxHashMap<Ty<'tcx>, Self::Type>> {
247         &self.scalar_lltypes
248     }
249 }
250
251 impl Type {
252     pub fn i8_llcx(llcx: &llvm::Context) -> &Type {
253         unsafe {
254             llvm::LLVMInt8TypeInContext(llcx)
255         }
256     }
257
258     // Creates an integer type with the given number of bits, e.g., i24
259     pub fn ix_llcx(
260         llcx: &llvm::Context,
261         num_bits: u64
262     ) -> &Type {
263         unsafe {
264             llvm::LLVMIntTypeInContext(llcx, num_bits as c_uint)
265         }
266     }
267
268     pub fn i8p_llcx(llcx: &'ll llvm::Context) -> &'ll Type {
269         Type::i8_llcx(llcx).ptr_to()
270     }
271
272     fn ptr_to(&self) -> &Type {
273         unsafe {
274             llvm::LLVMPointerType(&self, 0)
275         }
276     }
277 }
278
279
280 impl LayoutTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
281     fn backend_type(&self, layout: TyLayout<'tcx>) -> &'ll Type {
282         layout.llvm_type(self)
283     }
284     fn immediate_backend_type(&self, layout: TyLayout<'tcx>) -> &'ll Type {
285         layout.immediate_llvm_type(self)
286     }
287     fn is_backend_immediate(&self, layout: TyLayout<'tcx>) -> bool {
288         layout.is_llvm_immediate()
289     }
290     fn is_backend_scalar_pair(&self, layout: TyLayout<'tcx>) -> bool {
291         layout.is_llvm_scalar_pair()
292     }
293     fn backend_field_index(&self, layout: TyLayout<'tcx>, index: usize) -> u64 {
294         layout.llvm_field_index(index)
295     }
296     fn scalar_pair_element_backend_type<'a>(
297         &self,
298         layout: TyLayout<'tcx>,
299         index: usize,
300         immediate: bool
301     ) -> &'ll Type {
302         layout.scalar_pair_element_llvm_type(self, index, immediate)
303     }
304     fn cast_backend_type(&self, ty: &CastTarget) -> &'ll Type {
305         ty.llvm_type(self)
306     }
307     fn fn_backend_type(&self, ty: &FnType<'tcx, Ty<'tcx>>) -> &'ll Type {
308         ty.llvm_type(self)
309     }
310     fn fn_ptr_backend_type(&self, ty: &FnType<'tcx, Ty<'tcx>>) -> &'ll Type {
311         ty.ptr_to_llvm_type(self)
312     }
313     fn reg_backend_type(&self, ty: &Reg) -> &'ll Type {
314         ty.llvm_type(self)
315     }
316 }