]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_codegen_llvm/src/type_.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / compiler / rustc_codegen_llvm / src / 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 as 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::TyAndLayout;
16 use rustc_middle::ty::Ty;
17 use rustc_target::abi::call::{CastTarget, FnAbi, Reg};
18 use rustc_target::abi::{AddressSpace, Align, Integer, Size};
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_vector(&self, ty: &'ll Type, len: u64) -> &'ll Type {
66 unsafe { llvm::LLVMVectorType(ty, len as c_uint) }
67 }
68
69 crate fn func_params_types(&self, ty: &'ll Type) -> Vec<&'ll Type> {
70 unsafe {
71 let n_args = llvm::LLVMCountParamTypes(ty) as usize;
72 let mut args = Vec::with_capacity(n_args);
73 llvm::LLVMGetParamTypes(ty, args.as_mut_ptr());
74 args.set_len(n_args);
75 args
76 }
77 }
78
79 crate fn type_bool(&self) -> &'ll Type {
80 self.type_i8()
81 }
82
83 crate fn type_int_from_ty(&self, t: ast::IntTy) -> &'ll Type {
84 match t {
85 ast::IntTy::Isize => self.type_isize(),
86 ast::IntTy::I8 => self.type_i8(),
87 ast::IntTy::I16 => self.type_i16(),
88 ast::IntTy::I32 => self.type_i32(),
89 ast::IntTy::I64 => self.type_i64(),
90 ast::IntTy::I128 => self.type_i128(),
91 }
92 }
93
94 crate fn type_uint_from_ty(&self, t: ast::UintTy) -> &'ll Type {
95 match t {
96 ast::UintTy::Usize => self.type_isize(),
97 ast::UintTy::U8 => self.type_i8(),
98 ast::UintTy::U16 => self.type_i16(),
99 ast::UintTy::U32 => self.type_i32(),
100 ast::UintTy::U64 => self.type_i64(),
101 ast::UintTy::U128 => self.type_i128(),
102 }
103 }
104
105 crate fn type_float_from_ty(&self, t: ast::FloatTy) -> &'ll Type {
106 match t {
107 ast::FloatTy::F32 => self.type_f32(),
108 ast::FloatTy::F64 => self.type_f64(),
109 }
110 }
111
112 crate fn type_pointee_for_align(&self, align: Align) -> &'ll Type {
113 // FIXME(eddyb) We could find a better approximation if ity.align < align.
114 let ity = Integer::approximate_align(self, align);
115 self.type_from_integer(ity)
116 }
117
118 /// Return a LLVM type that has at most the required alignment,
119 /// and exactly the required size, as a best-effort padding array.
120 crate fn type_padding_filler(&self, size: Size, align: Align) -> &'ll Type {
121 let unit = Integer::approximate_align(self, align);
122 let size = size.bytes();
123 let unit_size = unit.size().bytes();
124 assert_eq!(size % unit_size, 0);
125 self.type_array(self.type_from_integer(unit), size / unit_size)
126 }
127
128 crate fn type_variadic_func(&self, args: &[&'ll Type], ret: &'ll Type) -> &'ll Type {
129 unsafe { llvm::LLVMFunctionType(ret, args.as_ptr(), args.len() as c_uint, True) }
130 }
131
132 crate fn type_array(&self, ty: &'ll Type, len: u64) -> &'ll Type {
133 unsafe { llvm::LLVMRustArrayType(ty, len) }
134 }
135 }
136
137 impl BaseTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
138 fn type_i1(&self) -> &'ll Type {
139 unsafe { llvm::LLVMInt1TypeInContext(self.llcx) }
140 }
141
142 fn type_i8(&self) -> &'ll Type {
143 unsafe { llvm::LLVMInt8TypeInContext(self.llcx) }
144 }
145
146 fn type_i16(&self) -> &'ll Type {
147 unsafe { llvm::LLVMInt16TypeInContext(self.llcx) }
148 }
149
150 fn type_i32(&self) -> &'ll Type {
151 unsafe { llvm::LLVMInt32TypeInContext(self.llcx) }
152 }
153
154 fn type_i64(&self) -> &'ll Type {
155 unsafe { llvm::LLVMInt64TypeInContext(self.llcx) }
156 }
157
158 fn type_i128(&self) -> &'ll Type {
159 unsafe { llvm::LLVMIntTypeInContext(self.llcx, 128) }
160 }
161
162 fn type_isize(&self) -> &'ll Type {
163 self.isize_ty
164 }
165
166 fn type_f32(&self) -> &'ll Type {
167 unsafe { llvm::LLVMFloatTypeInContext(self.llcx) }
168 }
169
170 fn type_f64(&self) -> &'ll Type {
171 unsafe { llvm::LLVMDoubleTypeInContext(self.llcx) }
172 }
173
174 fn type_func(&self, args: &[&'ll Type], ret: &'ll Type) -> &'ll Type {
175 unsafe { llvm::LLVMFunctionType(ret, args.as_ptr(), args.len() as c_uint, False) }
176 }
177
178 fn type_struct(&self, els: &[&'ll Type], packed: bool) -> &'ll Type {
179 unsafe {
180 llvm::LLVMStructTypeInContext(
181 self.llcx,
182 els.as_ptr(),
183 els.len() as c_uint,
184 packed as Bool,
185 )
186 }
187 }
188
189 fn type_kind(&self, ty: &'ll Type) -> TypeKind {
190 unsafe { llvm::LLVMRustGetTypeKind(ty).to_generic() }
191 }
192
193 fn type_ptr_to(&self, ty: &'ll Type) -> &'ll Type {
194 assert_ne!(
195 self.type_kind(ty),
196 TypeKind::Function,
197 "don't call ptr_to on function types, use ptr_to_llvm_type on FnAbi instead or explicitly specify an address space if it makes sense"
198 );
199 ty.ptr_to(AddressSpace::DATA)
200 }
201
202 fn type_ptr_to_ext(&self, ty: &'ll Type, address_space: AddressSpace) -> &'ll Type {
203 ty.ptr_to(address_space)
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: &llvm::Context) -> &Type {
244 Type::i8_llcx(llcx).ptr_to(AddressSpace::DATA)
245 }
246
247 fn ptr_to(&self, address_space: AddressSpace) -> &Type {
248 unsafe { llvm::LLVMPointerType(&self, address_space.0) }
249 }
250 }
251
252 impl LayoutTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
253 fn backend_type(&self, layout: TyAndLayout<'tcx>) -> &'ll Type {
254 layout.llvm_type(self)
255 }
256 fn immediate_backend_type(&self, layout: TyAndLayout<'tcx>) -> &'ll Type {
257 layout.immediate_llvm_type(self)
258 }
259 fn is_backend_immediate(&self, layout: TyAndLayout<'tcx>) -> bool {
260 layout.is_llvm_immediate()
261 }
262 fn is_backend_scalar_pair(&self, layout: TyAndLayout<'tcx>) -> bool {
263 layout.is_llvm_scalar_pair()
264 }
265 fn backend_field_index(&self, layout: TyAndLayout<'tcx>, index: usize) -> u64 {
266 layout.llvm_field_index(index)
267 }
268 fn scalar_pair_element_backend_type(
269 &self,
270 layout: TyAndLayout<'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 }