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, HasTyCtxt};
24 use rustc::util::nodemap::FxHashMap;
25 use rustc::ty::{self, Ty};
26 use rustc::ty::layout::TyLayout;
27 use rustc_target::abi::call::{CastTarget, FnType, Reg};
28 use rustc_data_structures::small_c_str::SmallCStr;
30 use rustc_codegen_utils::common::TypeKind;
31 use type_of::LayoutLlvmExt;
32 use abi::{LlvmType, FnTypeExt};
35 use std::cell::RefCell;
39 impl PartialEq for Type {
40 fn eq(&self, other: &Self) -> bool {
41 self as *const _ == other as *const _
45 impl fmt::Debug for Type {
46 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47 f.write_str(&llvm::build_string(|s| unsafe {
48 llvm::LLVMRustWriteTypeToString(self, s);
49 }).expect("non-UTF8 type description from LLVM"))
53 impl BaseTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
54 fn type_void(&self) -> &'ll Type {
56 llvm::LLVMVoidTypeInContext(self.llcx)
60 fn type_metadata(&self) -> &'ll Type {
62 llvm::LLVMRustMetadataTypeInContext(self.llcx)
66 fn type_i1(&self) -> &'ll Type {
68 llvm::LLVMInt1TypeInContext(self.llcx)
72 fn type_i8(&self) -> &'ll Type {
74 llvm::LLVMInt8TypeInContext(self.llcx)
79 fn type_i16(&self) -> &'ll Type {
82 llvm::LLVMInt16TypeInContext(self.llcx)
86 fn type_i32(&self) -> &'ll Type {
88 llvm::LLVMInt32TypeInContext(self.llcx)
92 fn type_i64(&self) -> &'ll Type {
94 llvm::LLVMInt64TypeInContext(self.llcx)
98 fn type_i128(&self) -> &'ll Type {
100 llvm::LLVMIntTypeInContext(self.llcx, 128)
104 fn type_ix(&self, num_bits: u64) -> &'ll Type {
106 llvm::LLVMIntTypeInContext(self.llcx, num_bits as c_uint)
110 fn type_f32(&self) -> &'ll Type {
112 llvm::LLVMFloatTypeInContext(self.llcx)
116 fn type_f64(&self) -> &'ll Type {
118 llvm::LLVMDoubleTypeInContext(self.llcx)
122 fn type_x86_mmx(&self) -> &'ll Type {
124 llvm::LLVMX86MMXTypeInContext(self.llcx)
134 llvm::LLVMFunctionType(ret, args.as_ptr(),
135 args.len() as c_uint, False)
139 fn type_variadic_func(
145 llvm::LLVMFunctionType(ret, args.as_ptr(),
146 args.len() as c_uint, True)
156 llvm::LLVMStructTypeInContext(self.llcx, els.as_ptr(),
162 fn type_named_struct(&self, name: &str) -> &'ll Type {
163 let name = SmallCStr::new(name);
165 llvm::LLVMStructCreateNamed(self.llcx, name.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 set_struct_body(&self, ty: &'ll Type, els: &[&'ll Type], packed: bool) {
190 llvm::LLVMStructSetBody(ty, els.as_ptr(),
191 els.len() as c_uint, packed as Bool)
195 fn type_ptr_to(&self, ty: &'ll Type) -> &'ll Type {
196 assert_ne!(self.type_kind(ty), TypeKind::Function,
197 "don't call ptr_to on function types, use ptr_to_llvm_type on FnType instead");
201 fn element_type(&self, ty: &'ll Type) -> &'ll Type {
203 llvm::LLVMGetElementType(ty)
207 fn vector_length(&self, ty: &'ll Type) -> usize {
209 llvm::LLVMGetVectorSize(ty) as usize
213 fn func_params_types(&self, ty: &'ll Type) -> Vec<&'ll Type> {
215 let n_args = llvm::LLVMCountParamTypes(ty) as usize;
216 let mut args = Vec::with_capacity(n_args);
217 llvm::LLVMGetParamTypes(ty, args.as_mut_ptr());
218 args.set_len(n_args);
223 fn float_width(&self, ty: &'ll Type) -> usize {
224 match self.type_kind(ty) {
225 TypeKind::Float => 32,
226 TypeKind::Double => 64,
227 TypeKind::X86_FP80 => 80,
228 TypeKind::FP128 | TypeKind::PPC_FP128 => 128,
229 _ => bug!("llvm_float_width called on a non-float type")
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 fn type_pointee_for_abi_align(&self, align: Align) -> &'ll Type {
348 // FIXME(eddyb) We could find a better approximation if ity.align < align.
349 let ity = layout::Integer::approximate_abi_align(self, align);
350 self.type_from_integer(ity)
353 fn type_padding_filler(
358 let unit = layout::Integer::approximate_abi_align(self, align);
359 let size = size.bytes();
360 let unit_size = unit.size().bytes();
361 assert_eq!(size % unit_size, 0);
362 self.type_array(self.type_from_integer(unit), size / unit_size)
365 fn type_needs_drop(&self, ty: Ty<'tcx>) -> bool {
366 common::type_needs_drop(self.tcx(), ty)
369 fn type_is_sized(&self, ty: Ty<'tcx>) -> bool {
370 common::type_is_sized(self.tcx(), ty)
373 fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
374 common::type_is_freeze(self.tcx(), ty)
377 fn type_has_metadata(&self, ty: Ty<'tcx>) -> bool {
378 use syntax_pos::DUMMY_SP;
379 if ty.is_sized(self.tcx().at(DUMMY_SP), ty::ParamEnv::reveal_all()) {
383 let tail = self.tcx().struct_tail(ty);
385 ty::Foreign(..) => false,
386 ty::Str | ty::Slice(..) | ty::Dynamic(..) => true,
387 _ => bug!("unexpected unsized tail: {:?}", tail.sty),
392 impl LayoutTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
393 fn backend_type(&self, layout: TyLayout<'tcx>) -> &'ll Type {
394 layout.llvm_type(self)
396 fn immediate_backend_type(&self, layout: TyLayout<'tcx>) -> &'ll Type {
397 layout.immediate_llvm_type(self)
399 fn is_backend_immediate(&self, layout: TyLayout<'tcx>) -> bool {
400 layout.is_llvm_immediate()
402 fn scalar_pair_element_backend_type<'a>(
404 layout: TyLayout<'tcx>,
408 layout.scalar_pair_element_llvm_type(self, index, immediate)
410 fn cast_backend_type(&self, ty: &CastTarget) -> &'ll Type {
413 fn fn_backend_type(&self, ty: &FnType<'tcx, Ty<'tcx>>) -> &'ll Type {
416 fn fn_ptr_backend_type(&self, ty: &FnType<'tcx, Ty<'tcx>>) -> &'ll Type {
417 ty.ptr_to_llvm_type(self)
419 fn reg_backend_type(&self, ty: &Reg) -> &'ll Type {