adapted from rustc_codegen_llvm
(
_tcx: TyCtxt<'_>,
mods: &mut LlvmMod,
kind: AllocatorKind,
has_alloc_error_handler: bool,
)
| 8 | |
| 9 | // adapted from rustc_codegen_llvm |
| 10 | pub(crate) unsafe fn codegen( |
| 11 | _tcx: TyCtxt<'_>, |
| 12 | mods: &mut LlvmMod, |
| 13 | kind: AllocatorKind, |
| 14 | has_alloc_error_handler: bool, |
| 15 | ) { |
| 16 | let llcx = &*mods.llcx; |
| 17 | let llmod = mods.llmod.as_ref().unwrap(); |
| 18 | let usize = target::usize_ty(llcx); |
| 19 | let i8 = llvm::LLVMInt8TypeInContext(llcx); |
| 20 | let i8p = llvm::LLVMPointerType(i8, 0); |
| 21 | let void = llvm::LLVMVoidTypeInContext(llcx); |
| 22 | |
| 23 | let mut used = Vec::new(); |
| 24 | |
| 25 | for method in ALLOCATOR_METHODS { |
| 26 | let mut args = Vec::with_capacity(method.inputs.len()); |
| 27 | for ty in method.inputs.iter() { |
| 28 | match *ty { |
| 29 | AllocatorTy::Layout => { |
| 30 | args.push(usize); // size |
| 31 | args.push(usize); // align |
| 32 | } |
| 33 | AllocatorTy::Ptr => args.push(i8p), |
| 34 | AllocatorTy::Usize => args.push(usize), |
| 35 | |
| 36 | AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"), |
| 37 | } |
| 38 | } |
| 39 | let output = match method.output { |
| 40 | AllocatorTy::ResultPtr => Some(i8p), |
| 41 | AllocatorTy::Unit => None, |
| 42 | |
| 43 | AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => { |
| 44 | panic!("invalid allocator output") |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | let ty = llvm::LLVMFunctionType( |
| 49 | output.unwrap_or(void), |
| 50 | args.as_ptr(), |
| 51 | args.len() as c_uint, |
| 52 | False, |
| 53 | ); |
| 54 | let name = format!("__rust_{}", method.name); |
| 55 | let llfn = llvm::LLVMRustGetOrInsertFunction(llmod, name.as_ptr().cast(), name.len(), ty); |
| 56 | |
| 57 | used.push(llfn); |
| 58 | // nvvm doesnt support uwtable so dont try to generate it |
| 59 | |
| 60 | let callee = kind.fn_name(method.name); |
| 61 | let callee = |
| 62 | llvm::LLVMRustGetOrInsertFunction(llmod, callee.as_ptr().cast(), callee.len(), ty); |
| 63 | llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden); |
| 64 | |
| 65 | let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, "entry\0".as_ptr().cast()); |
| 66 | |
| 67 | let llbuilder = llvm::LLVMCreateBuilderInContext(llcx); |
nothing calls this directly
no test coverage detected