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, TypeKind};
18 use context::CodegenCx;
19 use interfaces::TypeMethods;
22 use rustc::ty::layout::{self, Align, Size};
23 use rustc_data_structures::small_c_str::SmallCStr;
30 impl PartialEq for Type {
31 fn eq(&self, other: &Self) -> bool {
32 self as *const _ == other as *const _
36 impl fmt::Debug for Type {
37 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38 f.write_str(&llvm::build_string(|s| unsafe {
39 llvm::LLVMRustWriteTypeToString(self, s);
40 }).expect("non-UTF8 type description from LLVM"))
44 impl TypeMethods for CodegenCx<'ll, 'tcx> {
46 fn type_void(&self) -> &'ll Type {
48 llvm::LLVMVoidTypeInContext(self.llcx)
52 fn type_metadata(&self) -> &'ll Type {
54 llvm::LLVMRustMetadataTypeInContext(self.llcx)
58 fn type_i1(&self) -> &'ll Type {
60 llvm::LLVMInt1TypeInContext(self.llcx)
64 fn type_i8(&self) -> &'ll Type {
66 llvm::LLVMInt8TypeInContext(self.llcx)
71 fn type_i16(&self) -> &'ll Type {
74 llvm::LLVMInt16TypeInContext(self.llcx)
78 fn type_i32(&self) -> &'ll Type {
80 llvm::LLVMInt32TypeInContext(self.llcx)
84 fn type_i64(&self) -> &'ll Type {
86 llvm::LLVMInt64TypeInContext(self.llcx)
90 fn type_i128(&self) -> &'ll Type {
92 llvm::LLVMIntTypeInContext(self.llcx, 128)
96 // Creates an integer type with the given number of bits, e.g. i24
97 fn type_ix(&self, num_bits: u64) -> &'ll Type {
99 llvm::LLVMIntTypeInContext(self.llcx, num_bits as c_uint)
103 fn type_f32(&self) -> &'ll Type {
105 llvm::LLVMFloatTypeInContext(self.llcx)
109 fn type_f64(&self) -> &'ll Type {
111 llvm::LLVMDoubleTypeInContext(self.llcx)
115 fn type_x86_mmx(&self) -> &'ll Type {
117 llvm::LLVMX86MMXTypeInContext(self.llcx)
127 llvm::LLVMFunctionType(ret, args.as_ptr(),
128 args.len() as c_uint, False)
132 fn type_variadic_func(
138 llvm::LLVMFunctionType(ret, args.as_ptr(),
139 args.len() as c_uint, True)
149 llvm::LLVMStructTypeInContext(self.llcx, els.as_ptr(),
155 fn type_named_struct(&self, name: &str) -> &'ll Type {
156 let name = SmallCStr::new(name);
158 llvm::LLVMStructCreateNamed(self.llcx, name.as_ptr())
163 fn type_array(&self, ty: &'ll Type, len: u64) -> &'ll Type {
165 llvm::LLVMRustArrayType(ty, len)
169 fn type_vector(&self, ty: &'ll Type, len: u64) -> &'ll Type {
171 llvm::LLVMVectorType(ty, len as c_uint)
175 fn type_kind(&self, ty: &'ll Type) -> TypeKind {
177 llvm::LLVMRustGetTypeKind(ty)
181 fn set_struct_body(&self, ty: &'ll Type, els: &[&'ll Type], packed: bool) {
183 llvm::LLVMStructSetBody(ty, els.as_ptr(),
184 els.len() as c_uint, packed as Bool)
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");
192 llvm::LLVMPointerType(ty, 0)
196 fn element_type(&self, ty: &'ll Type) -> &'ll Type {
198 llvm::LLVMGetElementType(ty)
202 /// Return the number of elements in `self` if it is a LLVM vector type.
203 fn vector_length(&self, ty: &'ll Type) -> usize {
205 llvm::LLVMGetVectorSize(ty) as usize
209 fn func_params_types(&self, ty: &'ll Type) -> Vec<&'ll Type> {
211 let n_args = llvm::LLVMCountParamTypes(ty) as usize;
212 let mut args = Vec::with_capacity(n_args);
213 llvm::LLVMGetParamTypes(ty, args.as_mut_ptr());
214 args.set_len(n_args);
219 fn float_width(&self, ty : &'ll Type) -> usize {
220 match self.type_kind(ty) {
221 TypeKind::Float => 32,
222 TypeKind::Double => 64,
223 TypeKind::X86_FP80 => 80,
224 TypeKind::FP128 | TypeKind::PPc_FP128 => 128,
225 _ => bug!("llvm_float_width called on a non-float type")
229 /// Retrieve the bit width of the integer type `self`.
230 fn int_width(&self, ty: &'ll Type) -> u64 {
232 llvm::LLVMGetIntTypeWidth(ty) as u64
238 pub fn i8_llcx(llcx: &llvm::Context) -> &Type {
240 llvm::LLVMInt8TypeInContext(llcx)
244 // Creates an integer type with the given number of bits, e.g. i24
246 llcx: &llvm::Context,
250 llvm::LLVMIntTypeInContext(llcx, num_bits as c_uint)
254 pub fn i8p_llcx(cx : &write::CodegenContext<'ll>, llcx: &'ll llvm::Context) -> &'ll Type {
255 cx.type_ptr_to(Type::i8_llcx(llcx))
259 impl CodegenCx<'ll, 'tcx> {
260 pub fn type_bool(&self) -> &'ll Type {
264 pub fn type_i8p(&self) -> &'ll Type {
265 self.type_ptr_to(self.type_i8())
268 pub fn type_isize(&self) -> &'ll Type {
272 pub fn type_int(&self) -> &'ll Type {
273 match &self.sess().target.target.target_c_int_width[..] {
274 "16" => self.type_i16(),
275 "32" => self.type_i32(),
276 "64" => self.type_i64(),
277 width => bug!("Unsupported target_c_int_width: {}", width),
281 pub fn type_int_from_ty(
286 ast::IntTy::Isize => self.isize_ty,
287 ast::IntTy::I8 => self.type_i8(),
288 ast::IntTy::I16 => self.type_i16(),
289 ast::IntTy::I32 => self.type_i32(),
290 ast::IntTy::I64 => self.type_i64(),
291 ast::IntTy::I128 => self.type_i128(),
295 pub fn type_uint_from_ty(
300 ast::UintTy::Usize => self.isize_ty,
301 ast::UintTy::U8 => self.type_i8(),
302 ast::UintTy::U16 => self.type_i16(),
303 ast::UintTy::U32 => self.type_i32(),
304 ast::UintTy::U64 => self.type_i64(),
305 ast::UintTy::U128 => self.type_i128(),
309 pub fn type_float_from_ty(
314 ast::FloatTy::F32 => self.type_f32(),
315 ast::FloatTy::F64 => self.type_f64(),
319 pub fn type_from_integer(&self, i: layout::Integer) -> &'ll Type {
320 use rustc::ty::layout::Integer::*;
322 I8 => self.type_i8(),
323 I16 => self.type_i16(),
324 I32 => self.type_i32(),
325 I64 => self.type_i64(),
326 I128 => self.type_i128(),
330 /// Return a LLVM type that has at most the required alignment,
331 /// as a conservative approximation for unknown pointee types.
332 pub fn type_pointee_for_abi_align(&self, align: Align) -> &'ll Type {
333 // FIXME(eddyb) We could find a better approximation if ity.align < align.
334 let ity = layout::Integer::approximate_abi_align(self, align);
335 self.type_from_integer(ity)
338 /// Return a LLVM type that has at most the required alignment,
339 /// and exactly the required size, as a best-effort padding array.
340 pub fn type_padding_filler(
345 let unit = layout::Integer::approximate_abi_align(self, align);
346 let size = size.bytes();
347 let unit_size = unit.size().bytes();
348 assert_eq!(size % unit_size, 0);
349 self.type_array(self.type_from_integer(unit), size / unit_size)