1 // Copyright 2017 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 use std::ffi::CString;
15 use rustc::middle::allocator::AllocatorKind;
16 use rustc::ty::TyCtxt;
17 use rustc_allocator::{ALLOCATOR_METHODS, AllocatorTy};
20 use llvm::{self, False, True};
22 pub(crate) unsafe fn trans(tcx: TyCtxt, mods: &ModuleLlvm, kind: AllocatorKind) {
24 let llmod = mods.llmod;
25 let usize = match &tcx.sess.target.target.target_pointer_width[..] {
26 "16" => llvm::LLVMInt16TypeInContext(llcx),
27 "32" => llvm::LLVMInt32TypeInContext(llcx),
28 "64" => llvm::LLVMInt64TypeInContext(llcx),
29 tws => bug!("Unsupported target word size for int: {}", tws),
31 let i8 = llvm::LLVMInt8TypeInContext(llcx);
32 let i8p = llvm::LLVMPointerType(i8, 0);
33 let usizep = llvm::LLVMPointerType(usize, 0);
34 let void = llvm::LLVMVoidTypeInContext(llcx);
36 for method in ALLOCATOR_METHODS {
37 let mut args = Vec::new();
38 for ty in method.inputs.iter() {
40 AllocatorTy::Layout => {
41 args.push(usize); // size
42 args.push(usize); // align
44 AllocatorTy::LayoutRef => args.push(i8p),
45 AllocatorTy::Ptr => args.push(i8p),
46 AllocatorTy::AllocErr => args.push(i8p),
49 AllocatorTy::ResultExcess |
50 AllocatorTy::ResultPtr |
51 AllocatorTy::ResultUnit |
52 AllocatorTy::UsizePair |
53 AllocatorTy::Unit => panic!("invalid allocator arg"),
56 let output = match method.output {
57 AllocatorTy::UsizePair => {
58 args.push(usizep); // min
59 args.push(usizep); // max
62 AllocatorTy::Bang => None,
63 AllocatorTy::ResultExcess => {
64 args.push(i8p); // excess_ptr
65 args.push(i8p); // err_ptr
68 AllocatorTy::ResultPtr => {
69 args.push(i8p); // err_ptr
72 AllocatorTy::ResultUnit => Some(i8),
73 AllocatorTy::Unit => None,
75 AllocatorTy::AllocErr |
77 AllocatorTy::LayoutRef |
78 AllocatorTy::Ptr => panic!("invalid allocator output"),
80 let ty = llvm::LLVMFunctionType(output.unwrap_or(void),
84 let name = CString::new(format!("__rust_{}", method.name)).unwrap();
85 let llfn = llvm::LLVMRustGetOrInsertFunction(llmod,
89 let callee = CString::new(kind.fn_name(method.name)).unwrap();
90 let callee = llvm::LLVMRustGetOrInsertFunction(llmod,
94 let llbb = llvm::LLVMAppendBasicBlockInContext(llcx,
96 "entry\0".as_ptr() as *const _);
98 let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);
99 llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb);
100 let args = args.iter().enumerate().map(|(i, _)| {
101 llvm::LLVMGetParam(llfn, i as c_uint)
102 }).collect::<Vec<_>>();
103 let ret = llvm::LLVMRustBuildCall(llbuilder,
106 args.len() as c_uint,
108 "\0".as_ptr() as *const _);
109 llvm::LLVMSetTailCall(ret, True);
110 if output.is_some() {
111 llvm::LLVMBuildRet(llbuilder, ret);
113 llvm::LLVMBuildRetVoid(llbuilder);
115 llvm::LLVMDisposeBuilder(llbuilder);