1 #![allow(non_upper_case_globals)]
3 pub use crate::llvm::Type;
6 use crate::llvm::{Bool, False, True};
7 use crate::context::CodegenCx;
8 use crate::value::Value;
9 use rustc_codegen_ssa::traits::*;
12 use crate::type_of::LayoutLlvmExt;
13 use crate::abi::{LlvmType, FnTypeExt};
14 use rustc::util::nodemap::FxHashMap;
16 use rustc::ty::layout::TyLayout;
17 use rustc_target::abi::call::{CastTarget, FnType, Reg};
18 use rustc_data_structures::small_c_str::SmallCStr;
19 use rustc_codegen_ssa::common::TypeKind;
22 use std::cell::RefCell;
27 impl PartialEq for Type {
28 fn eq(&self, other: &Self) -> bool {
33 impl fmt::Debug for Type {
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 f.write_str(&llvm::build_string(|s| unsafe {
36 llvm::LLVMRustWriteTypeToString(self, s);
37 }).expect("non-UTF8 type description from LLVM"))
41 impl CodegenCx<'ll, 'tcx> {
42 crate fn type_named_struct(&self, name: &str) -> &'ll Type {
43 let name = SmallCStr::new(name);
45 llvm::LLVMStructCreateNamed(self.llcx, name.as_ptr())
49 crate fn set_struct_body(&self, ty: &'ll Type, els: &[&'ll Type], packed: bool) {
51 llvm::LLVMStructSetBody(ty, els.as_ptr(),
52 els.len() as c_uint, packed as Bool)
57 impl BaseTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
58 fn type_void(&self) -> &'ll Type {
60 llvm::LLVMVoidTypeInContext(self.llcx)
64 fn type_metadata(&self) -> &'ll Type {
66 llvm::LLVMRustMetadataTypeInContext(self.llcx)
70 fn type_i1(&self) -> &'ll Type {
72 llvm::LLVMInt1TypeInContext(self.llcx)
76 fn type_i8(&self) -> &'ll Type {
78 llvm::LLVMInt8TypeInContext(self.llcx)
83 fn type_i16(&self) -> &'ll Type {
85 llvm::LLVMInt16TypeInContext(self.llcx)
89 fn type_i32(&self) -> &'ll Type {
91 llvm::LLVMInt32TypeInContext(self.llcx)
95 fn type_i64(&self) -> &'ll Type {
97 llvm::LLVMInt64TypeInContext(self.llcx)
101 fn type_i128(&self) -> &'ll Type {
103 llvm::LLVMIntTypeInContext(self.llcx, 128)
107 fn type_ix(&self, num_bits: u64) -> &'ll Type {
109 llvm::LLVMIntTypeInContext(self.llcx, num_bits as c_uint)
113 fn type_isize(&self) -> &'ll Type {
117 fn type_f32(&self) -> &'ll Type {
119 llvm::LLVMFloatTypeInContext(self.llcx)
123 fn type_f64(&self) -> &'ll Type {
125 llvm::LLVMDoubleTypeInContext(self.llcx)
129 fn type_x86_mmx(&self) -> &'ll Type {
131 llvm::LLVMX86MMXTypeInContext(self.llcx)
141 llvm::LLVMFunctionType(ret, args.as_ptr(),
142 args.len() as c_uint, False)
146 fn type_variadic_func(
152 llvm::LLVMFunctionType(ret, args.as_ptr(),
153 args.len() as c_uint, True)
163 llvm::LLVMStructTypeInContext(self.llcx, els.as_ptr(),
170 fn type_array(&self, ty: &'ll Type, len: u64) -> &'ll Type {
172 llvm::LLVMRustArrayType(ty, len)
176 fn type_vector(&self, ty: &'ll Type, len: u64) -> &'ll Type {
178 llvm::LLVMVectorType(ty, len as c_uint)
182 fn type_kind(&self, ty: &'ll Type) -> TypeKind {
184 llvm::LLVMRustGetTypeKind(ty).to_generic()
188 fn type_ptr_to(&self, ty: &'ll Type) -> &'ll Type {
189 assert_ne!(self.type_kind(ty), TypeKind::Function,
190 "don't call ptr_to on function types, use ptr_to_llvm_type on FnType instead");
194 fn element_type(&self, ty: &'ll Type) -> &'ll Type {
196 llvm::LLVMGetElementType(ty)
200 fn vector_length(&self, ty: &'ll Type) -> usize {
202 llvm::LLVMGetVectorSize(ty) as usize
206 fn func_params_types(&self, ty: &'ll Type) -> Vec<&'ll Type> {
208 let n_args = llvm::LLVMCountParamTypes(ty) as usize;
209 let mut args = Vec::with_capacity(n_args);
210 llvm::LLVMGetParamTypes(ty, args.as_mut_ptr());
211 args.set_len(n_args);
216 fn float_width(&self, ty: &'ll Type) -> usize {
217 match self.type_kind(ty) {
218 TypeKind::Float => 32,
219 TypeKind::Double => 64,
220 TypeKind::X86_FP80 => 80,
221 TypeKind::FP128 | TypeKind::PPC_FP128 => 128,
222 _ => bug!("llvm_float_width called on a non-float type")
226 fn int_width(&self, ty: &'ll Type) -> u64 {
228 llvm::LLVMGetIntTypeWidth(ty) as u64
232 fn val_ty(&self, v: &'ll Value) -> &'ll Type {
236 fn scalar_lltypes(&self) -> &RefCell<FxHashMap<Ty<'tcx>, Self::Type>> {
242 pub fn i8_llcx(llcx: &llvm::Context) -> &Type {
244 llvm::LLVMInt8TypeInContext(llcx)
248 // Creates an integer type with the given number of bits, e.g., i24
250 llcx: &llvm::Context,
254 llvm::LLVMIntTypeInContext(llcx, num_bits as c_uint)
258 pub fn i8p_llcx(llcx: &'ll llvm::Context) -> &'ll Type {
259 Type::i8_llcx(llcx).ptr_to()
262 fn ptr_to(&self) -> &Type {
264 llvm::LLVMPointerType(&self, 0)
270 impl LayoutTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
271 fn backend_type(&self, layout: TyLayout<'tcx>) -> &'ll Type {
272 layout.llvm_type(self)
274 fn immediate_backend_type(&self, layout: TyLayout<'tcx>) -> &'ll Type {
275 layout.immediate_llvm_type(self)
277 fn is_backend_immediate(&self, layout: TyLayout<'tcx>) -> bool {
278 layout.is_llvm_immediate()
280 fn is_backend_scalar_pair(&self, layout: TyLayout<'tcx>) -> bool {
281 layout.is_llvm_scalar_pair()
283 fn backend_field_index(&self, layout: TyLayout<'tcx>, index: usize) -> u64 {
284 layout.llvm_field_index(index)
286 fn scalar_pair_element_backend_type<'a>(
288 layout: TyLayout<'tcx>,
292 layout.scalar_pair_element_llvm_type(self, index, immediate)
294 fn cast_backend_type(&self, ty: &CastTarget) -> &'ll Type {
297 fn fn_backend_type(&self, ty: &FnType<'tcx, Ty<'tcx>>) -> &'ll Type {
300 fn fn_ptr_backend_type(&self, ty: &FnType<'tcx, Ty<'tcx>>) -> &'ll Type {
301 ty.ptr_to_llvm_type(self)
303 fn reg_backend_type(&self, ty: &Reg) -> &'ll Type {