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