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;
18 use rustc_codegen_ssa::traits::*;
21 use rustc::util::nodemap::FxHashMap;
23 use rustc::ty::layout::TyLayout;
24 use rustc_target::abi::call::{CastTarget, FnType, Reg};
25 use rustc_data_structures::small_c_str::SmallCStr;
27 use rustc_codegen_ssa::common::TypeKind;
28 use type_of::LayoutLlvmExt;
29 use abi::{LlvmType, FnTypeExt};
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 fn type_ix(&self, num_bits: u64) -> &'ll Type {
103 llvm::LLVMIntTypeInContext(self.llcx, num_bits as c_uint)
107 fn type_isize(&self) -> &'ll Type {
111 fn type_f32(&self) -> &'ll Type {
113 llvm::LLVMFloatTypeInContext(self.llcx)
117 fn type_f64(&self) -> &'ll Type {
119 llvm::LLVMDoubleTypeInContext(self.llcx)
123 fn type_x86_mmx(&self) -> &'ll Type {
125 llvm::LLVMX86MMXTypeInContext(self.llcx)
135 llvm::LLVMFunctionType(ret, args.as_ptr(),
136 args.len() as c_uint, False)
140 fn type_variadic_func(
146 llvm::LLVMFunctionType(ret, args.as_ptr(),
147 args.len() as c_uint, True)
157 llvm::LLVMStructTypeInContext(self.llcx, els.as_ptr(),
163 fn type_named_struct(&self, name: &str) -> &'ll Type {
164 let name = SmallCStr::new(name);
166 llvm::LLVMStructCreateNamed(self.llcx, name.as_ptr())
171 fn type_array(&self, ty: &'ll Type, len: u64) -> &'ll Type {
173 llvm::LLVMRustArrayType(ty, len)
177 fn type_vector(&self, ty: &'ll Type, len: u64) -> &'ll Type {
179 llvm::LLVMVectorType(ty, len as c_uint)
183 fn type_kind(&self, ty: &'ll Type) -> TypeKind {
185 llvm::LLVMRustGetTypeKind(ty).to_generic()
189 fn set_struct_body(&self, ty: &'ll Type, els: &[&'ll Type], packed: bool) {
191 llvm::LLVMStructSetBody(ty, els.as_ptr(),
192 els.len() as c_uint, packed as Bool)
196 fn type_ptr_to(&self, ty: &'ll Type) -> &'ll Type {
197 assert_ne!(self.type_kind(ty), TypeKind::Function,
198 "don't call ptr_to on function types, use ptr_to_llvm_type on FnType instead");
202 fn element_type(&self, ty: &'ll Type) -> &'ll Type {
204 llvm::LLVMGetElementType(ty)
208 fn vector_length(&self, ty: &'ll Type) -> usize {
210 llvm::LLVMGetVectorSize(ty) as usize
214 fn func_params_types(&self, ty: &'ll Type) -> Vec<&'ll Type> {
216 let n_args = llvm::LLVMCountParamTypes(ty) as usize;
217 let mut args = Vec::with_capacity(n_args);
218 llvm::LLVMGetParamTypes(ty, args.as_mut_ptr());
219 args.set_len(n_args);
224 fn float_width(&self, ty: &'ll Type) -> usize {
225 match self.type_kind(ty) {
226 TypeKind::Float => 32,
227 TypeKind::Double => 64,
228 TypeKind::X86_FP80 => 80,
229 TypeKind::FP128 | TypeKind::PPC_FP128 => 128,
230 _ => bug!("llvm_float_width called on a non-float type")
234 fn int_width(&self, ty: &'ll Type) -> u64 {
236 llvm::LLVMGetIntTypeWidth(ty) as u64
240 fn val_ty(&self, v: &'ll Value) -> &'ll Type {
244 fn scalar_lltypes(&self) -> &RefCell<FxHashMap<Ty<'tcx>, Self::Type>> {
250 pub fn i8_llcx(llcx: &llvm::Context) -> &Type {
252 llvm::LLVMInt8TypeInContext(llcx)
256 // Creates an integer type with the given number of bits, e.g. i24
258 llcx: &llvm::Context,
262 llvm::LLVMIntTypeInContext(llcx, num_bits as c_uint)
266 pub fn i8p_llcx(llcx: &'ll llvm::Context) -> &'ll Type {
267 Type::i8_llcx(llcx).ptr_to()
270 fn ptr_to(&self) -> &Type {
272 llvm::LLVMPointerType(&self, 0)
278 impl LayoutTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
279 fn backend_type(&self, layout: TyLayout<'tcx>) -> &'ll Type {
280 layout.llvm_type(self)
282 fn immediate_backend_type(&self, layout: TyLayout<'tcx>) -> &'ll Type {
283 layout.immediate_llvm_type(self)
285 fn is_backend_immediate(&self, layout: TyLayout<'tcx>) -> bool {
286 layout.is_llvm_immediate()
288 fn is_backend_scalar_pair(&self, layout: TyLayout<'tcx>) -> bool {
289 layout.is_llvm_scalar_pair()
291 fn backend_field_index(&self, layout: TyLayout<'tcx>, index: usize) -> u64 {
292 layout.llvm_field_index(index)
294 fn scalar_pair_element_backend_type<'a>(
296 layout: TyLayout<'tcx>,
300 layout.scalar_pair_element_llvm_type(self, index, immediate)
302 fn cast_backend_type(&self, ty: &CastTarget) -> &'ll Type {
305 fn fn_backend_type(&self, ty: &FnType<'tcx, Ty<'tcx>>) -> &'ll Type {
308 fn fn_ptr_backend_type(&self, ty: &FnType<'tcx, Ty<'tcx>>) -> &'ll Type {
309 ty.ptr_to_llvm_type(self)
311 fn reg_backend_type(&self, ty: &Reg) -> &'ll Type {