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