]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/type_.rs
Moved common.rs enums
[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 interfaces::*;
19 use value::Value;
20
21
22 use syntax::ast;
23 use rustc::ty::layout::{self, Align, Size, HasTyCtxt};
24 use rustc::util::nodemap::FxHashMap;
25 use rustc::ty::{self, Ty};
26 use rustc::ty::layout::TyLayout;
27 use rustc_target::abi::call::{CastTarget, FnType, Reg};
28 use rustc_data_structures::small_c_str::SmallCStr;
29 use common;
30 use rustc_codegen_utils::common::TypeKind;
31 use type_of::LayoutLlvmExt;
32 use abi::{LlvmType, FnTypeExt};
33
34 use std::fmt;
35 use std::cell::RefCell;
36
37 use libc::c_uint;
38
39 impl PartialEq for Type {
40     fn eq(&self, other: &Self) -> bool {
41         self as *const _ == other as *const _
42     }
43 }
44
45 impl fmt::Debug for Type {
46     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47         f.write_str(&llvm::build_string(|s| unsafe {
48             llvm::LLVMRustWriteTypeToString(self, s);
49         }).expect("non-UTF8 type description from LLVM"))
50     }
51 }
52
53 impl BaseTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
54     fn type_void(&self) -> &'ll Type {
55         unsafe {
56             llvm::LLVMVoidTypeInContext(self.llcx)
57         }
58     }
59
60     fn type_metadata(&self) -> &'ll Type {
61         unsafe {
62             llvm::LLVMRustMetadataTypeInContext(self.llcx)
63         }
64     }
65
66     fn type_i1(&self) -> &'ll Type {
67         unsafe {
68             llvm::LLVMInt1TypeInContext(self.llcx)
69         }
70     }
71
72     fn type_i8(&self) -> &'ll Type {
73         unsafe {
74             llvm::LLVMInt8TypeInContext(self.llcx)
75         }
76     }
77
78
79     fn type_i16(&self) -> &'ll Type {
80         unsafe {
81
82             llvm::LLVMInt16TypeInContext(self.llcx)
83         }
84     }
85
86     fn type_i32(&self) -> &'ll Type {
87         unsafe {
88             llvm::LLVMInt32TypeInContext(self.llcx)
89         }
90     }
91
92     fn type_i64(&self) -> &'ll Type {
93         unsafe {
94             llvm::LLVMInt64TypeInContext(self.llcx)
95         }
96     }
97
98     fn type_i128(&self) -> &'ll Type {
99         unsafe {
100             llvm::LLVMIntTypeInContext(self.llcx, 128)
101         }
102     }
103
104     fn type_ix(&self, num_bits: u64) -> &'ll Type {
105         unsafe {
106             llvm::LLVMIntTypeInContext(self.llcx, num_bits as c_uint)
107         }
108     }
109
110     fn type_f32(&self) -> &'ll Type {
111         unsafe {
112             llvm::LLVMFloatTypeInContext(self.llcx)
113         }
114     }
115
116     fn type_f64(&self) -> &'ll Type {
117         unsafe {
118             llvm::LLVMDoubleTypeInContext(self.llcx)
119         }
120     }
121
122     fn type_x86_mmx(&self) -> &'ll Type {
123         unsafe {
124             llvm::LLVMX86MMXTypeInContext(self.llcx)
125         }
126     }
127
128     fn type_func(
129         &self,
130         args: &[&'ll Type],
131         ret: &'ll Type
132     ) -> &'ll Type {
133         unsafe {
134             llvm::LLVMFunctionType(ret, args.as_ptr(),
135                                    args.len() as c_uint, False)
136         }
137     }
138
139     fn type_variadic_func(
140         &self,
141         args: &[&'ll Type],
142         ret: &'ll Type
143     ) -> &'ll Type {
144         unsafe {
145             llvm::LLVMFunctionType(ret, args.as_ptr(),
146                                    args.len() as c_uint, True)
147         }
148     }
149
150     fn type_struct(
151         &self,
152         els: &[&'ll Type],
153         packed: bool
154     ) -> &'ll Type {
155         unsafe {
156             llvm::LLVMStructTypeInContext(self.llcx, els.as_ptr(),
157                                           els.len() as c_uint,
158                                           packed as Bool)
159         }
160     }
161
162     fn type_named_struct(&self, name: &str) -> &'ll Type {
163         let name = SmallCStr::new(name);
164         unsafe {
165             llvm::LLVMStructCreateNamed(self.llcx, name.as_ptr())
166         }
167     }
168
169
170     fn type_array(&self, ty: &'ll Type, len: u64) -> &'ll Type {
171         unsafe {
172             llvm::LLVMRustArrayType(ty, len)
173         }
174     }
175
176     fn type_vector(&self, ty: &'ll Type, len: u64) -> &'ll Type {
177         unsafe {
178             llvm::LLVMVectorType(ty, len as c_uint)
179         }
180     }
181
182     fn type_kind(&self, ty: &'ll Type) -> TypeKind {
183         unsafe {
184             llvm::LLVMRustGetTypeKind(ty).to_generic()
185         }
186     }
187
188     fn set_struct_body(&self, ty: &'ll Type, els: &[&'ll Type], packed: bool) {
189         unsafe {
190             llvm::LLVMStructSetBody(ty, els.as_ptr(),
191                                     els.len() as c_uint, packed as Bool)
192         }
193     }
194
195     fn type_ptr_to(&self, ty: &'ll Type) -> &'ll Type {
196         assert_ne!(self.type_kind(ty), TypeKind::Function,
197                    "don't call ptr_to on function types, use ptr_to_llvm_type on FnType instead");
198         ty.ptr_to()
199     }
200
201     fn element_type(&self, ty: &'ll Type) -> &'ll Type {
202         unsafe {
203             llvm::LLVMGetElementType(ty)
204         }
205     }
206
207     fn vector_length(&self, ty: &'ll Type) -> usize {
208         unsafe {
209             llvm::LLVMGetVectorSize(ty) as usize
210         }
211     }
212
213     fn func_params_types(&self, ty: &'ll Type) -> Vec<&'ll Type> {
214         unsafe {
215             let n_args = llvm::LLVMCountParamTypes(ty) as usize;
216             let mut args = Vec::with_capacity(n_args);
217             llvm::LLVMGetParamTypes(ty, args.as_mut_ptr());
218             args.set_len(n_args);
219             args
220         }
221     }
222
223     fn float_width(&self, ty: &'ll Type) -> usize {
224         match self.type_kind(ty) {
225             TypeKind::Float => 32,
226             TypeKind::Double => 64,
227             TypeKind::X86_FP80 => 80,
228             TypeKind::FP128 | TypeKind::PPC_FP128 => 128,
229             _ => bug!("llvm_float_width called on a non-float type")
230         }
231     }
232
233     fn int_width(&self, ty: &'ll Type) -> u64 {
234         unsafe {
235             llvm::LLVMGetIntTypeWidth(ty) as u64
236         }
237     }
238
239     fn val_ty(&self, v: &'ll Value) -> &'ll Type {
240         common::val_ty(v)
241     }
242
243     fn scalar_lltypes(&self) -> &RefCell<FxHashMap<Ty<'tcx>, Self::Type>> {
244         &self.scalar_lltypes
245     }
246 }
247
248 impl Type {
249     pub fn i8_llcx(llcx: &llvm::Context) -> &Type {
250         unsafe {
251             llvm::LLVMInt8TypeInContext(llcx)
252         }
253     }
254
255     // Creates an integer type with the given number of bits, e.g. i24
256     pub fn ix_llcx(
257         llcx: &llvm::Context,
258         num_bits: u64
259     ) -> &Type {
260         unsafe {
261             llvm::LLVMIntTypeInContext(llcx, num_bits as c_uint)
262         }
263     }
264
265     pub fn i8p_llcx(llcx: &'ll llvm::Context) -> &'ll Type {
266         Type::i8_llcx(llcx).ptr_to()
267     }
268
269     fn ptr_to(&self) -> &Type {
270         unsafe {
271             llvm::LLVMPointerType(&self, 0)
272         }
273     }
274 }
275
276 impl DerivedTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
277     fn type_bool(&self) -> &'ll Type {
278         self.type_i8()
279     }
280
281     fn type_i8p(&self) -> &'ll Type {
282         self.type_ptr_to(self.type_i8())
283     }
284
285     fn type_isize(&self) -> &'ll Type {
286         self.isize_ty
287     }
288
289     fn type_int(&self) -> &'ll Type {
290         match &self.sess().target.target.target_c_int_width[..] {
291             "16" => self.type_i16(),
292             "32" => self.type_i32(),
293             "64" => self.type_i64(),
294             width => bug!("Unsupported target_c_int_width: {}", width),
295         }
296     }
297
298     fn type_int_from_ty(
299         &self,
300         t: ast::IntTy
301     ) -> &'ll Type {
302         match t {
303             ast::IntTy::Isize => self.isize_ty,
304             ast::IntTy::I8 => self.type_i8(),
305             ast::IntTy::I16 => self.type_i16(),
306             ast::IntTy::I32 => self.type_i32(),
307             ast::IntTy::I64 => self.type_i64(),
308             ast::IntTy::I128 => self.type_i128(),
309         }
310     }
311
312     fn type_uint_from_ty(
313         &self,
314         t: ast::UintTy
315     ) -> &'ll Type {
316         match t {
317             ast::UintTy::Usize => self.isize_ty,
318             ast::UintTy::U8 => self.type_i8(),
319             ast::UintTy::U16 => self.type_i16(),
320             ast::UintTy::U32 => self.type_i32(),
321             ast::UintTy::U64 => self.type_i64(),
322             ast::UintTy::U128 => self.type_i128(),
323         }
324     }
325
326     fn type_float_from_ty(
327         &self,
328         t: ast::FloatTy
329     ) -> &'ll Type {
330         match t {
331             ast::FloatTy::F32 => self.type_f32(),
332             ast::FloatTy::F64 => self.type_f64(),
333         }
334     }
335
336     fn type_from_integer(&self, i: layout::Integer) -> &'ll Type {
337         use rustc::ty::layout::Integer::*;
338         match i {
339             I8 => self.type_i8(),
340             I16 => self.type_i16(),
341             I32 => self.type_i32(),
342             I64 => self.type_i64(),
343             I128 => self.type_i128(),
344         }
345     }
346
347     fn type_pointee_for_abi_align(&self, align: Align) -> &'ll Type {
348         // FIXME(eddyb) We could find a better approximation if ity.align < align.
349         let ity = layout::Integer::approximate_abi_align(self, align);
350         self.type_from_integer(ity)
351     }
352
353     fn type_padding_filler(
354         &self,
355         size: Size,
356         align: Align
357     ) -> &'ll Type {
358         let unit = layout::Integer::approximate_abi_align(self, align);
359         let size = size.bytes();
360         let unit_size = unit.size().bytes();
361         assert_eq!(size % unit_size, 0);
362         self.type_array(self.type_from_integer(unit), size / unit_size)
363     }
364
365     fn type_needs_drop(&self, ty: Ty<'tcx>) -> bool {
366         common::type_needs_drop(self.tcx(), ty)
367     }
368
369     fn type_is_sized(&self, ty: Ty<'tcx>) -> bool {
370         common::type_is_sized(self.tcx(), ty)
371     }
372
373     fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
374         common::type_is_freeze(self.tcx(), ty)
375     }
376
377     fn type_has_metadata(&self, ty: Ty<'tcx>) -> bool {
378         use syntax_pos::DUMMY_SP;
379         if ty.is_sized(self.tcx().at(DUMMY_SP), ty::ParamEnv::reveal_all()) {
380             return false;
381         }
382
383         let tail = self.tcx().struct_tail(ty);
384         match tail.sty {
385             ty::Foreign(..) => false,
386             ty::Str | ty::Slice(..) | ty::Dynamic(..) => true,
387             _ => bug!("unexpected unsized tail: {:?}", tail.sty),
388         }
389     }
390 }
391
392 impl LayoutTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
393     fn backend_type(&self, layout: TyLayout<'tcx>) -> &'ll Type {
394         layout.llvm_type(self)
395     }
396     fn immediate_backend_type(&self, layout: TyLayout<'tcx>) -> &'ll Type {
397         layout.immediate_llvm_type(self)
398     }
399     fn is_backend_immediate(&self, layout: TyLayout<'tcx>) -> bool {
400         layout.is_llvm_immediate()
401     }
402     fn scalar_pair_element_backend_type<'a>(
403         &self,
404         layout: TyLayout<'tcx>,
405         index: usize,
406         immediate: bool
407     ) -> &'ll Type {
408         layout.scalar_pair_element_llvm_type(self, index, immediate)
409     }
410     fn cast_backend_type(&self, ty: &CastTarget) -> &'ll Type {
411         ty.llvm_type(self)
412     }
413     fn fn_backend_type(&self, ty: &FnType<'tcx, Ty<'tcx>>) -> &'ll Type {
414         ty.llvm_type(self)
415     }
416     fn fn_ptr_backend_type(&self, ty: &FnType<'tcx, Ty<'tcx>>) -> &'ll Type {
417         ty.ptr_to_llvm_type(self)
418     }
419     fn reg_backend_type(&self, ty: &Reg) -> &'ll Type {
420         ty.llvm_type(self)
421     }
422 }