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