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