1use std::borrow::Borrow;
2use std::hash::{Hash, Hasher};
3use std::{fmt, ptr};
4
5use libc::c_uint;
6use rustc_abi::{AddressSpace, Align, Float, Integer, Reg, Size};
7use rustc_codegen_ssa::traits::*;
8use rustc_data_structures::small_c_str::SmallCStr;
9use rustc_middle::ty::layout::TyAndLayout;
10use rustc_middle::ty::{self, Ty};
11use rustc_span::bug;
12use rustc_target::callconv::{CastTarget, FnAbi};
13
14use crate::abi::{FnAbiLlvmExt, LlvmType};
15use crate::common;
16use crate::context::{CodegenCx, GenericCx, SCx};
17use crate::llvm::{self, FALSE, TRUE, ToLlvmBool, Type, TypeKind, Value};
18use crate::type_of::LayoutLlvmExt;
19
20impl PartialEq for Type {
21 fn eq(&self, other: &Self) -> bool {
22 ptr::eq(self, other)
23 }
24}
25
26impl Eq for Type {}
27
28impl Hash for Type {
29 fn hash<H: Hasher>(&self, state: &mut H) {
30 ptr::hash(self, state);
31 }
32}
33
34impl fmt::Debug for Type {
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 f.write_str(
37 &llvm::build_string(|s| unsafe {
38 llvm::LLVMRustWriteTypeToString(self, s);
39 })
40 .expect("non-UTF8 type description from LLVM"),
41 )
42 }
43}
44
45impl<'ll> CodegenCx<'ll, '_> {}
46impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
47 pub(crate) fn type_named_struct(&self, name: &str) -> &'ll Type {
48 let name = SmallCStr::new(name);
49 unsafe { llvm::LLVMStructCreateNamed(self.llcx(), name.as_ptr()) }
50 }
51
52 pub(crate) fn set_struct_body(&self, ty: &'ll Type, els: &[&'ll Type], packed: bool) {
53 unsafe {
54 llvm::LLVMStructSetBody(ty, els.as_ptr(), els.len() as c_uint, packed.to_llvm_bool())
55 }
56 }
57 pub(crate) fn type_void(&self) -> &'ll Type {
58 unsafe { llvm::LLVMVoidTypeInContext(self.llcx()) }
59 }
60
61 pub(crate) fn type_ix(&self, num_bits: u64) -> &'ll Type {
63 llvm::LLVMIntTypeInContext(self.llcx(), num_bits as c_uint)
64 }
65
66 pub(crate) fn type_vector(&self, ty: &'ll Type, count: u64) -> &'ll Type {
67 unsafe { llvm::LLVMVectorType(ty, count as c_uint) }
68 }
69
70 pub(crate) fn type_scalable_vector(&self, ty: &'ll Type, count: u64) -> &'ll Type {
71 unsafe { llvm::LLVMScalableVectorType(ty, count as c_uint) }
72 }
73
74 pub(crate) fn add_func(&self, name: &str, ty: &'ll Type) -> &'ll Value {
75 let name = SmallCStr::new(name);
76 unsafe { llvm::LLVMAddFunction(self.llmod(), name.as_ptr(), ty) }
77 }
78
79 pub(crate) fn get_return_type(&self, ty: &'ll Type) -> &'ll Type {
80 unsafe { llvm::LLVMGetReturnType(ty) }
81 }
82
83 pub(crate) fn func_params_types(&self, ty: &'ll Type) -> Vec<&'ll Type> {
84 unsafe {
85 let n_args = llvm::LLVMCountParamTypes(ty) as usize;
86 let mut args = Vec::with_capacity(n_args);
87 llvm::LLVMGetParamTypes(ty, args.as_mut_ptr());
88 args.set_len(n_args);
89 args
90 }
91 }
92
93 pub(crate) fn func_is_variadic(&self, ty: &'ll Type) -> bool {
94 unsafe { llvm::LLVMIsFunctionVarArg(ty).is_true() }
95 }
96
97 pub(crate) fn struct_element_types(&self, ty: &'ll Type) -> Vec<&'ll Type> {
98 unsafe {
99 let n_args = llvm::LLVMCountStructElementTypes(ty) as usize;
100 let mut args = Vec::with_capacity(n_args);
101 llvm::LLVMGetStructElementTypes(ty, args.as_mut_ptr());
102 args.set_len(n_args);
103 args
104 }
105 }
106}
107impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
108 pub(crate) fn type_int_from_ty(&self, t: ty::IntTy) -> &'ll Type {
109 match t {
110 ty::IntTy::Isize => self.type_isize(),
111 ty::IntTy::I8 => self.type_i8(),
112 ty::IntTy::I16 => self.type_i16(),
113 ty::IntTy::I32 => self.type_i32(),
114 ty::IntTy::I64 => self.type_i64(),
115 ty::IntTy::I128 => self.type_i128(),
116 }
117 }
118
119 pub(crate) fn type_uint_from_ty(&self, t: ty::UintTy) -> &'ll Type {
120 match t {
121 ty::UintTy::Usize => self.type_isize(),
122 ty::UintTy::U8 => self.type_i8(),
123 ty::UintTy::U16 => self.type_i16(),
124 ty::UintTy::U32 => self.type_i32(),
125 ty::UintTy::U64 => self.type_i64(),
126 ty::UintTy::U128 => self.type_i128(),
127 }
128 }
129
130 pub(crate) fn type_float_from_ty(&self, t: ty::FloatTy) -> &'ll Type {
131 match t {
132 ty::FloatTy::F16 => self.type_f16(),
133 ty::FloatTy::F32 => self.type_f32(),
134 ty::FloatTy::F64 => self.type_f64(),
135 ty::FloatTy::F128 => self.type_f128(),
136 }
137 }
138
139 pub(crate) fn type_padding_filler(&self, size: Size, align: Align) -> &'ll Type {
142 let unit = Integer::approximate_align(self, align);
143 let size = size.bytes();
144 let unit_size = unit.size().bytes();
145 {
match (&(size % unit_size), &0) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};assert_eq!(size % unit_size, 0);
146 self.type_array(self.type_from_integer(unit), size / unit_size)
147 }
148}
149
150impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
151 pub(crate) fn llcx(&self) -> llvm::Context<'ll> {
152 (**self).borrow().llcx
153 }
154
155 pub(crate) fn llmod(&self) -> llvm::Module<'ll> {
156 (**self).borrow().llmod
157 }
158
159 pub(crate) fn isize_ty(&self) -> &'ll Type {
160 (**self).borrow().isize_ty
161 }
162
163 pub(crate) fn type_variadic_func(&self, args: &[&'ll Type], ret: &'ll Type) -> &'ll Type {
164 unsafe { llvm::LLVMFunctionType(ret, args.as_ptr(), args.len() as c_uint, TRUE) }
165 }
166
167 pub(crate) fn type_i1(&self) -> &'ll Type {
168 unsafe { llvm::LLVMInt1TypeInContext(self.llcx()) }
169 }
170
171 pub(crate) fn type_struct(&self, els: &[&'ll Type]) -> &'ll Type {
172 unsafe {
173 llvm::LLVMStructTypeInContext(self.llcx(), els.as_ptr(), els.len() as c_uint, FALSE)
174 }
175 }
176
177 pub(crate) fn type_packed_struct(&self, els: &[&'ll Type]) -> &'ll Type {
178 unsafe {
179 llvm::LLVMStructTypeInContext(self.llcx(), els.as_ptr(), els.len() as c_uint, TRUE)
180 }
181 }
182
183 pub(crate) fn type_bf16(&self) -> &'ll Type {
184 unsafe { llvm::LLVMBFloatTypeInContext(self.llcx()) }
185 }
186
187 pub(crate) fn type_array(&self, ty: &'ll Type, len: u64) -> &'ll Type {
188 unsafe { llvm::LLVMArrayType2(ty, len) }
189 }
190
191 pub(crate) fn type_kind(&self, ty: &'ll Type) -> TypeKind {
194 llvm::LLVMGetTypeKind(ty).to_rust()
195 }
196}
197
198impl<'ll, CX: Borrow<SCx<'ll>>> BaseTypeCodegenMethods for GenericCx<'ll, CX> {
199 fn type_i8(&self) -> &'ll Type {
200 unsafe { llvm::LLVMInt8TypeInContext(self.llcx()) }
201 }
202
203 fn type_i16(&self) -> &'ll Type {
204 unsafe { llvm::LLVMInt16TypeInContext(self.llcx()) }
205 }
206
207 fn type_i32(&self) -> &'ll Type {
208 unsafe { llvm::LLVMInt32TypeInContext(self.llcx()) }
209 }
210
211 fn type_i64(&self) -> &'ll Type {
212 unsafe { llvm::LLVMInt64TypeInContext(self.llcx()) }
213 }
214
215 fn type_i128(&self) -> &'ll Type {
216 self.type_ix(128)
217 }
218
219 fn type_isize(&self) -> &'ll Type {
220 self.isize_ty()
221 }
222
223 fn type_f16(&self) -> &'ll Type {
224 unsafe { llvm::LLVMHalfTypeInContext(self.llcx()) }
225 }
226
227 fn type_f16b(&self) -> &'ll Type {
228 unsafe { llvm::LLVMBFloatTypeInContext(self.llcx()) }
229 }
230
231 fn type_f32(&self) -> &'ll Type {
232 unsafe { llvm::LLVMFloatTypeInContext(self.llcx()) }
233 }
234
235 fn type_f64(&self) -> &'ll Type {
236 unsafe { llvm::LLVMDoubleTypeInContext(self.llcx()) }
237 }
238
239 fn type_f128(&self) -> &'ll Type {
240 unsafe { llvm::LLVMFP128TypeInContext(self.llcx()) }
241 }
242
243 fn type_ppcf128(&self) -> &'ll Type {
244 unsafe { llvm::LLVMPPCFP128TypeInContext(self.llcx()) }
245 }
246
247 fn type_func(&self, args: &[&'ll Type], ret: &'ll Type) -> &'ll Type {
248 unsafe { llvm::LLVMFunctionType(ret, args.as_ptr(), args.len() as c_uint, FALSE) }
249 }
250
251 fn type_ptr(&self) -> &'ll Type {
252 llvm_type_ptr(self.llcx())
253 }
254
255 fn type_ptr_ext(&self, address_space: AddressSpace) -> &'ll Type {
256 llvm_type_ptr_in_address_space(self.llcx(), address_space)
257 }
258
259 fn vector_element_type_and_length(&self, ty: Self::Type) -> Option<(Self::Type, u64)> {
260 match self.type_kind(ty) {
261 TypeKind::Vector => {
262 Some(unsafe { (llvm::LLVMGetElementType(ty), llvm::LLVMGetVectorSize(ty).into()) })
263 }
264 _ => None,
265 }
266 }
267
268 fn float_type(&self, ty: &'ll Type) -> Option<Float> {
269 match self.type_kind(ty) {
270 TypeKind::BFloat => Some(Float::F16B),
271 TypeKind::Half => Some(Float::F16),
272 TypeKind::Float => Some(Float::F32),
273 TypeKind::Double => Some(Float::F64),
274 TypeKind::FP128 => Some(Float::F128),
275 TypeKind::X86_FP80 => ::rustc_span::macros::bug_impl(None,
format_args!("x87_f80 is not supported yet"), Location::caller())bug!("x87_f80 is not supported yet"),
276 TypeKind::PPC_FP128 => Some(Float::PpcF128),
277 _ => None,
278 }
279 }
280
281 fn int_width(&self, ty: &'ll Type) -> Option<u64> {
282 match self.type_kind(ty) {
283 TypeKind::Integer => Some(unsafe { llvm::LLVMGetIntTypeWidth(ty).into() }),
284 _ => None,
285 }
286 }
287
288 fn val_ty(&self, v: &'ll Value) -> &'ll Type {
289 common::val_ty(v)
290 }
291}
292
293pub(crate) fn llvm_type_ptr<'ll>(llcx: llvm::Context<'ll>) -> &'ll Type {
294 llvm_type_ptr_in_address_space(llcx, AddressSpace::ZERO)
295}
296
297pub(crate) fn llvm_type_ptr_in_address_space<'ll>(
298 llcx: llvm::Context<'ll>,
299 addr_space: AddressSpace,
300) -> &'ll Type {
301 llvm::LLVMPointerTypeInContext(llcx, addr_space.0)
302}
303
304impl<'ll, 'tcx> LayoutTypeCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
305 fn backend_type(&self, layout: TyAndLayout<'tcx>) -> &'ll Type {
306 layout.llvm_type(self)
307 }
308 fn immediate_backend_type(&self, layout: TyAndLayout<'tcx>) -> &'ll Type {
309 layout.immediate_llvm_type(self)
310 }
311 fn scalar_pair_element_backend_type(
312 &self,
313 layout: TyAndLayout<'tcx>,
314 index: usize,
315 immediate: bool,
316 ) -> &'ll Type {
317 layout.scalar_pair_element_llvm_type(self, index, immediate)
318 }
319 fn cast_backend_type(&self, ty: &CastTarget) -> &'ll Type {
320 ty.llvm_type(self)
321 }
322 fn fn_decl_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Type {
323 fn_abi.llvm_type(self)
324 }
325 fn fn_ptr_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Type {
326 fn_abi.ptr_to_llvm_type(self)
327 }
328 fn reg_backend_type(&self, ty: &Reg) -> &'ll Type {
329 ty.llvm_type(self)
330 }
331}
332
333impl<'ll, 'tcx> TypeMembershipCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
334 fn add_type_metadata(&self, function: &'ll Value, typeid: &[u8]) {
335 let typeid_metadata = self.create_metadata(typeid);
336 let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
337 self.global_add_metadata_node(function, llvm::MD_type, &v);
338 }
339
340 fn set_type_metadata(&self, function: &'ll Value, typeid: &[u8]) {
341 let typeid_metadata = self.create_metadata(typeid);
342 let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
343 self.global_set_metadata_node(function, llvm::MD_type, &v);
344 }
345
346 fn add_kcfi_type_metadata(&self, function: &'ll Value, kcfi_typeid: u32) {
347 let kcfi_type_metadata = [llvm::LLVMValueAsMetadata(self.const_u32(kcfi_typeid))];
348 self.global_add_metadata_node(function, llvm::MD_kcfi_type, &kcfi_type_metadata);
349 }
350
351 fn set_kcfi_type_metadata(&self, function: &'ll Value, kcfi_typeid: u32) {
352 let kcfi_type_metadata = [llvm::LLVMValueAsMetadata(self.const_u32(kcfi_typeid))];
353 self.global_set_metadata_node(function, llvm::MD_kcfi_type, &kcfi_type_metadata);
354 }
355}