1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![allow(non_upper_case_globals)]
16 use llvm::{Bool, False, True};
17 use context::CodegenCx;
23 use rustc::ty::layout::{self, Align, Size};
24 use rustc::util::nodemap::FxHashMap;
26 use rustc::ty::layout::TyLayout;
27 use rustc_data_structures::small_c_str::SmallCStr;
28 use common::{self, TypeKind};
29 use type_of::LayoutLlvmExt;
32 use std::cell::RefCell;
36 impl PartialEq for Type {
37 fn eq(&self, other: &Self) -> bool {
38 self as *const _ == other as *const _
42 impl fmt::Debug for Type {
43 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44 f.write_str(&llvm::build_string(|s| unsafe {
45 llvm::LLVMRustWriteTypeToString(self, s);
46 }).expect("non-UTF8 type description from LLVM"))
50 impl BaseTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
51 fn type_void(&self) -> &'ll Type {
53 llvm::LLVMVoidTypeInContext(self.llcx)
57 fn type_metadata(&self) -> &'ll Type {
59 llvm::LLVMRustMetadataTypeInContext(self.llcx)
63 fn type_i1(&self) -> &'ll Type {
65 llvm::LLVMInt1TypeInContext(self.llcx)
69 fn type_i8(&self) -> &'ll Type {
71 llvm::LLVMInt8TypeInContext(self.llcx)
76 fn type_i16(&self) -> &'ll Type {
79 llvm::LLVMInt16TypeInContext(self.llcx)
83 fn type_i32(&self) -> &'ll Type {
85 llvm::LLVMInt32TypeInContext(self.llcx)
89 fn type_i64(&self) -> &'ll Type {
91 llvm::LLVMInt64TypeInContext(self.llcx)
95 fn type_i128(&self) -> &'ll Type {
97 llvm::LLVMIntTypeInContext(self.llcx, 128)
101 // Creates an integer type with the given number of bits, e.g. i24
102 fn type_ix(&self, num_bits: u64) -> &'ll Type {
104 llvm::LLVMIntTypeInContext(self.llcx, num_bits as c_uint)
108 fn type_f32(&self) -> &'ll Type {
110 llvm::LLVMFloatTypeInContext(self.llcx)
114 fn type_f64(&self) -> &'ll Type {
116 llvm::LLVMDoubleTypeInContext(self.llcx)
120 fn type_x86_mmx(&self) -> &'ll Type {
122 llvm::LLVMX86MMXTypeInContext(self.llcx)
132 llvm::LLVMFunctionType(ret, args.as_ptr(),
133 args.len() as c_uint, False)
137 fn type_variadic_func(
143 llvm::LLVMFunctionType(ret, args.as_ptr(),
144 args.len() as c_uint, True)
154 llvm::LLVMStructTypeInContext(self.llcx, els.as_ptr(),
160 fn type_named_struct(&self, name: &str) -> &'ll Type {
161 let name = SmallCStr::new(name);
163 llvm::LLVMStructCreateNamed(self.llcx, name.as_ptr())
168 fn type_array(&self, ty: &'ll Type, len: u64) -> &'ll Type {
170 llvm::LLVMRustArrayType(ty, len)
174 fn type_vector(&self, ty: &'ll Type, len: u64) -> &'ll Type {
176 llvm::LLVMVectorType(ty, len as c_uint)
180 fn type_kind(&self, ty: &'ll Type) -> TypeKind {
182 llvm::LLVMRustGetTypeKind(ty).to_generic()
186 fn set_struct_body(&self, ty: &'ll Type, els: &[&'ll Type], packed: bool) {
188 llvm::LLVMStructSetBody(ty, els.as_ptr(),
189 els.len() as c_uint, packed as Bool)
193 fn type_ptr_to(&self, ty: &'ll Type) -> &'ll Type {
194 assert_ne!(self.type_kind(ty), TypeKind::Function,
195 "don't call ptr_to on function types, use ptr_to_llvm_type on FnType instead");
199 fn element_type(&self, ty: &'ll Type) -> &'ll Type {
201 llvm::LLVMGetElementType(ty)
205 /// Return the number of elements in `self` if it is a LLVM vector type.
206 fn vector_length(&self, ty: &'ll Type) -> usize {
208 llvm::LLVMGetVectorSize(ty) as usize
212 fn func_params_types(&self, ty: &'ll Type) -> Vec<&'ll Type> {
214 let n_args = llvm::LLVMCountParamTypes(ty) as usize;
215 let mut args = Vec::with_capacity(n_args);
216 llvm::LLVMGetParamTypes(ty, args.as_mut_ptr());
217 args.set_len(n_args);
222 fn float_width(&self, ty: &'ll Type) -> usize {
223 match self.type_kind(ty) {
224 TypeKind::Float => 32,
225 TypeKind::Double => 64,
226 TypeKind::X86_FP80 => 80,
227 TypeKind::FP128 | TypeKind::PPc_FP128 => 128,
228 _ => bug!("llvm_float_width called on a non-float type")
232 /// Retrieve the bit width of the integer type `self`.
233 fn int_width(&self, ty: &'ll Type) -> u64 {
235 llvm::LLVMGetIntTypeWidth(ty) as u64
239 fn val_ty(&self, v: &'ll Value) -> &'ll Type {
243 fn scalar_lltypes(&self) -> &RefCell<FxHashMap<Ty<'tcx>, Self::Type>> {
249 pub fn i8_llcx(llcx: &llvm::Context) -> &Type {
251 llvm::LLVMInt8TypeInContext(llcx)
255 // Creates an integer type with the given number of bits, e.g. i24
257 llcx: &llvm::Context,
261 llvm::LLVMIntTypeInContext(llcx, num_bits as c_uint)
265 pub fn i8p_llcx(llcx: &'ll llvm::Context) -> &'ll Type {
266 Type::i8_llcx(llcx).ptr_to()
269 fn ptr_to(&self) -> &Type {
271 llvm::LLVMPointerType(&self, 0)
276 impl DerivedTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
277 fn type_bool(&self) -> &'ll Type {
281 fn type_i8p(&self) -> &'ll Type {
282 self.type_ptr_to(self.type_i8())
285 fn type_isize(&self) -> &'ll Type {
289 fn type_int(&self) -> &'ll Type {
290 match &self.sess().target.target.target_c_int_width[..] {
291 "16" => self.type_i16(),
292 "32" => self.type_i32(),
293 "64" => self.type_i64(),
294 width => bug!("Unsupported target_c_int_width: {}", width),
303 ast::IntTy::Isize => self.isize_ty,
304 ast::IntTy::I8 => self.type_i8(),
305 ast::IntTy::I16 => self.type_i16(),
306 ast::IntTy::I32 => self.type_i32(),
307 ast::IntTy::I64 => self.type_i64(),
308 ast::IntTy::I128 => self.type_i128(),
312 fn type_uint_from_ty(
317 ast::UintTy::Usize => self.isize_ty,
318 ast::UintTy::U8 => self.type_i8(),
319 ast::UintTy::U16 => self.type_i16(),
320 ast::UintTy::U32 => self.type_i32(),
321 ast::UintTy::U64 => self.type_i64(),
322 ast::UintTy::U128 => self.type_i128(),
326 fn type_float_from_ty(
331 ast::FloatTy::F32 => self.type_f32(),
332 ast::FloatTy::F64 => self.type_f64(),
336 fn type_from_integer(&self, i: layout::Integer) -> &'ll Type {
337 use rustc::ty::layout::Integer::*;
339 I8 => self.type_i8(),
340 I16 => self.type_i16(),
341 I32 => self.type_i32(),
342 I64 => self.type_i64(),
343 I128 => self.type_i128(),
347 /// Return a LLVM type that has at most the required alignment,
348 /// as a conservative approximation for unknown pointee types.
349 fn type_pointee_for_abi_align(&self, align: Align) -> &'ll Type {
350 // FIXME(eddyb) We could find a better approximation if ity.align < align.
351 let ity = layout::Integer::approximate_abi_align(self, align);
352 self.type_from_integer(ity)
355 /// Return a LLVM type that has at most the required alignment,
356 /// and exactly the required size, as a best-effort padding array.
357 fn type_padding_filler(
362 let unit = layout::Integer::approximate_abi_align(self, align);
363 let size = size.bytes();
364 let unit_size = unit.size().bytes();
365 assert_eq!(size % unit_size, 0);
366 self.type_array(self.type_from_integer(unit), size / unit_size)
370 impl LayoutTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
371 fn backend_type(&self, ty: TyLayout<'tcx>) -> &'ll Type {