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