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;
21 use rustc::ty::layout::{self, Align, Size};
22 use rustc_data_structures::small_c_str::SmallCStr;
28 impl PartialEq for Type {
29 fn eq(&self, other: &Self) -> bool {
30 self as *const _ == other as *const _
34 impl fmt::Debug for Type {
35 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36 f.write_str(&llvm::build_string(|s| unsafe {
37 llvm::LLVMRustWriteTypeToString(self, s);
38 }).expect("non-UTF8 type description from LLVM"))
43 pub fn void(cx: &CodegenCx<'ll, '_>) -> &'ll Type {
45 llvm::LLVMVoidTypeInContext(cx.llcx)
49 pub fn metadata(cx: &CodegenCx<'ll, '_>) -> &'ll Type {
51 llvm::LLVMRustMetadataTypeInContext(cx.llcx)
55 pub fn i1(cx: &CodegenCx<'ll, '_>) -> &'ll Type {
57 llvm::LLVMInt1TypeInContext(cx.llcx)
61 pub fn i8(cx: &CodegenCx<'ll, '_>) -> &'ll Type {
63 llvm::LLVMInt8TypeInContext(cx.llcx)
67 pub fn i8_llcx(llcx: &llvm::Context) -> &Type {
69 llvm::LLVMInt8TypeInContext(llcx)
73 pub fn i16(cx: &CodegenCx<'ll, '_>) -> &'ll Type {
75 llvm::LLVMInt16TypeInContext(cx.llcx)
79 pub fn i32(cx: &CodegenCx<'ll, '_>) -> &'ll Type {
81 llvm::LLVMInt32TypeInContext(cx.llcx)
85 pub fn i64(cx: &CodegenCx<'ll, '_>) -> &'ll Type {
87 llvm::LLVMInt64TypeInContext(cx.llcx)
91 pub fn i128(cx: &CodegenCx<'ll, '_>) -> &'ll Type {
93 llvm::LLVMIntTypeInContext(cx.llcx, 128)
97 // Creates an integer type with the given number of bits, e.g. i24
98 pub fn ix(cx: &CodegenCx<'ll, '_>, num_bits: u64) -> &'ll Type {
100 llvm::LLVMIntTypeInContext(cx.llcx, num_bits as c_uint)
104 // Creates an integer type with the given number of bits, e.g. i24
105 pub fn ix_llcx(llcx: &llvm::Context, num_bits: u64) -> &Type {
107 llvm::LLVMIntTypeInContext(llcx, num_bits as c_uint)
111 pub fn f32(cx: &CodegenCx<'ll, '_>) -> &'ll Type {
113 llvm::LLVMFloatTypeInContext(cx.llcx)
117 pub fn f64(cx: &CodegenCx<'ll, '_>) -> &'ll Type {
119 llvm::LLVMDoubleTypeInContext(cx.llcx)
123 pub fn bool(cx: &CodegenCx<'ll, '_>) -> &'ll Type {
127 pub fn char(cx: &CodegenCx<'ll, '_>) -> &'ll Type {
131 pub fn i8p(cx: &CodegenCx<'ll, '_>) -> &'ll Type {
132 Type::i8(cx).ptr_to()
135 pub fn i8p_llcx(llcx: &llvm::Context) -> &Type {
136 Type::i8_llcx(llcx).ptr_to()
139 pub fn isize(cx: &CodegenCx<'ll, '_>) -> &'ll Type {
143 pub fn c_int(cx: &CodegenCx<'ll, '_>) -> &'ll Type {
144 match &cx.tcx.sess.target.target.target_c_int_width[..] {
145 "16" => Type::i16(cx),
146 "32" => Type::i32(cx),
147 "64" => Type::i64(cx),
148 width => bug!("Unsupported target_c_int_width: {}", width),
152 pub fn int_from_ty(cx: &CodegenCx<'ll, '_>, t: ast::IntTy) -> &'ll Type {
154 ast::IntTy::Isize => cx.isize_ty,
155 ast::IntTy::I8 => Type::i8(cx),
156 ast::IntTy::I16 => Type::i16(cx),
157 ast::IntTy::I32 => Type::i32(cx),
158 ast::IntTy::I64 => Type::i64(cx),
159 ast::IntTy::I128 => Type::i128(cx),
163 pub fn uint_from_ty(cx: &CodegenCx<'ll, '_>, t: ast::UintTy) -> &'ll Type {
165 ast::UintTy::Usize => cx.isize_ty,
166 ast::UintTy::U8 => Type::i8(cx),
167 ast::UintTy::U16 => Type::i16(cx),
168 ast::UintTy::U32 => Type::i32(cx),
169 ast::UintTy::U64 => Type::i64(cx),
170 ast::UintTy::U128 => Type::i128(cx),
174 pub fn float_from_ty(cx: &CodegenCx<'ll, '_>, t: ast::FloatTy) -> &'ll Type {
176 ast::FloatTy::F32 => Type::f32(cx),
177 ast::FloatTy::F64 => Type::f64(cx),
181 pub fn func(args: &[&'ll Type], ret: &'ll Type) -> &'ll Type {
183 llvm::LLVMFunctionType(ret, args.as_ptr(),
184 args.len() as c_uint, False)
188 pub fn variadic_func(args: &[&'ll Type], ret: &'ll Type) -> &'ll Type {
190 llvm::LLVMFunctionType(ret, args.as_ptr(),
191 args.len() as c_uint, True)
195 pub fn struct_(cx: &CodegenCx<'ll, '_>, els: &[&'ll Type], packed: bool) -> &'ll Type {
197 llvm::LLVMStructTypeInContext(cx.llcx, els.as_ptr(),
203 pub fn named_struct(cx: &CodegenCx<'ll, '_>, name: &str) -> &'ll Type {
204 let name = SmallCStr::new(name);
206 llvm::LLVMStructCreateNamed(cx.llcx, name.as_ptr())
211 pub fn array(ty: &Type, len: u64) -> &Type {
213 llvm::LLVMRustArrayType(ty, len)
217 pub fn vector(ty: &Type, len: u64) -> &Type {
219 llvm::LLVMVectorType(ty, len as c_uint)
223 pub fn kind(&self) -> TypeKind {
225 llvm::LLVMRustGetTypeKind(self)
229 pub fn set_struct_body(&'ll self, els: &[&'ll Type], packed: bool) {
231 llvm::LLVMStructSetBody(self, els.as_ptr(),
232 els.len() as c_uint, packed as Bool)
236 pub fn ptr_to(&self) -> &Type {
237 assert_ne!(self.kind(), TypeKind::Function,
238 "don't call ptr_to on function types, use ptr_to_llvm_type on FnType instead");
240 llvm::LLVMPointerType(self, 0)
244 pub fn element_type(&self) -> &Type {
246 llvm::LLVMGetElementType(self)
250 /// Return the number of elements in `self` if it is a LLVM vector type.
251 pub fn vector_length(&self) -> usize {
253 llvm::LLVMGetVectorSize(self) as usize
257 pub fn func_params(&self) -> Vec<&Type> {
259 let n_args = llvm::LLVMCountParamTypes(self) as usize;
260 let mut args = Vec::with_capacity(n_args);
261 llvm::LLVMGetParamTypes(self, args.as_mut_ptr());
262 args.set_len(n_args);
267 pub fn float_width(&self) -> usize {
269 TypeKind::Float => 32,
270 TypeKind::Double => 64,
271 TypeKind::X86_FP80 => 80,
272 TypeKind::FP128 | TypeKind::PPC_FP128 => 128,
273 _ => bug!("llvm_float_width called on a non-float type")
277 /// Retrieve the bit width of the integer type `self`.
278 pub fn int_width(&self) -> u64 {
280 llvm::LLVMGetIntTypeWidth(self) as u64
284 pub fn from_integer(cx: &CodegenCx<'ll, '_>, i: layout::Integer) -> &'ll Type {
285 use rustc::ty::layout::Integer::*;
288 I16 => Type::i16(cx),
289 I32 => Type::i32(cx),
290 I64 => Type::i64(cx),
291 I128 => Type::i128(cx),
295 /// Return a LLVM type that has at most the required alignment,
296 /// as a conservative approximation for unknown pointee types.
297 pub fn pointee_for_abi_align(cx: &CodegenCx<'ll, '_>, align: Align) -> &'ll Type {
298 // FIXME(eddyb) We could find a better approximation if ity.align < align.
299 let ity = layout::Integer::approximate_abi_align(cx, align);
300 Type::from_integer(cx, ity)
303 /// Return a LLVM type that has at most the required alignment,
304 /// and exactly the required size, as a best-effort padding array.
305 pub fn padding_filler(cx: &CodegenCx<'ll, '_>, size: Size, align: Align) -> &'ll Type {
306 let unit = layout::Integer::approximate_abi_align(cx, align);
307 let size = size.bytes();
308 let unit_size = unit.size().bytes();
309 assert_eq!(size % unit_size, 0);
310 Type::array(Type::from_integer(cx, unit), size / unit_size)
313 pub fn x86_mmx(cx: &CodegenCx<'ll, '_>) -> &'ll Type {
315 llvm::LLVMX86MMXTypeInContext(cx.llcx)