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