]> git.lizzy.rs Git - rust.git/blobdiff - src/allocator.rs
Rollup merge of #81618 - bjorn3:sync_cg_clif-2021-02-01, r=bjorn3
[rust.git] / src / allocator.rs
index 2474e0146700465874320899a233363f26af2aa7..6c5916550ff639f52a99c14bd8ce34c0321f4da0 100644 (file)
@@ -1,19 +1,36 @@
-// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
+//! Allocator shim
+// Adapted from rustc
 
 use crate::prelude::*;
 
-use rustc::middle::allocator::AllocatorKind;
-use rustc_allocator::{AllocatorTy, ALLOCATOR_METHODS};
+use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
+use rustc_span::symbol::sym;
 
-pub fn codegen(module: &mut Module<impl Backend + 'static>, kind: AllocatorKind) {
+/// Returns whether an allocator shim was created
+pub(crate) fn codegen(
+    tcx: TyCtxt<'_>,
+    module: &mut impl Module,
+    unwind_context: &mut UnwindContext<'_>,
+) -> bool {
+    let any_dynamic_crate = tcx.dependency_formats(LOCAL_CRATE).iter().any(|(_, list)| {
+        use rustc_middle::middle::dependency_format::Linkage;
+        list.iter().any(|&linkage| linkage == Linkage::Dynamic)
+    });
+    if any_dynamic_crate {
+        false
+    } else if let Some(kind) = tcx.allocator_kind() {
+        codegen_inner(module, unwind_context, kind);
+        true
+    } else {
+        false
+    }
+}
+
+fn codegen_inner(
+    module: &mut impl Module,
+    unwind_context: &mut UnwindContext<'_>,
+    kind: AllocatorKind,
+) {
     let usize_ty = module.target_config().pointer_type();
 
     for method in ALLOCATOR_METHODS {
@@ -39,8 +56,8 @@ pub fn codegen(module: &mut Module<impl Backend + 'static>, kind: AllocatorKind)
             }
         };
 
-        let mut sig = Signature {
-            call_conv: CallConv::Fast,
+        let sig = Signature {
+            call_conv: CallConv::triple_default(module.isa().triple()),
             params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
             returns: output.into_iter().map(AbiParam::new).collect(),
         };
@@ -49,12 +66,10 @@ pub fn codegen(module: &mut Module<impl Backend + 'static>, kind: AllocatorKind)
         let callee_name = kind.fn_name(method.name);
         //eprintln!("Codegen allocator shim {} -> {} ({:?} -> {:?})", caller_name, callee_name, sig.params, sig.returns);
 
-        sig.call_conv = CallConv::Fast; // "rust" abi
         let func_id = module
             .declare_function(&caller_name, Linkage::Export, &sig)
             .unwrap();
 
-        sig.call_conv = CallConv::SystemV; // "C" abi
         let callee_func_id = module
             .declare_function(&callee_name, Linkage::Import, &sig)
             .unwrap();
@@ -63,24 +78,76 @@ pub fn codegen(module: &mut Module<impl Backend + 'static>, kind: AllocatorKind)
         ctx.func = Function::with_name_signature(ExternalName::user(0, 0), sig.clone());
         {
             let mut func_ctx = FunctionBuilderContext::new();
-            let mut bcx: FunctionBuilder = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
+            let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
 
-            let ebb = bcx.create_ebb();
-            bcx.switch_to_block(ebb);
+            let block = bcx.create_block();
+            bcx.switch_to_block(block);
             let args = arg_tys
                 .into_iter()
-                .map(|ty| bcx.append_ebb_param(ebb, ty))
+                .map(|ty| bcx.append_block_param(block, ty))
                 .collect::<Vec<Value>>();
 
             let callee_func_ref = module.declare_func_in_func(callee_func_id, &mut bcx.func);
-
             let call_inst = bcx.ins().call(callee_func_ref, &args);
-
             let results = bcx.inst_results(call_inst).to_vec(); // Clone to prevent borrow error
+
             bcx.ins().return_(&results);
             bcx.seal_all_blocks();
             bcx.finalize();
         }
-        module.define_function(func_id, &mut ctx).unwrap();
+        module
+            .define_function(
+                func_id,
+                &mut ctx,
+                &mut cranelift_codegen::binemit::NullTrapSink {},
+            )
+            .unwrap();
+        unwind_context.add_function(func_id, &ctx, module.isa());
+    }
+
+    let sig = Signature {
+        call_conv: CallConv::triple_default(module.isa().triple()),
+        params: vec![AbiParam::new(usize_ty), AbiParam::new(usize_ty)],
+        returns: vec![],
+    };
+
+    let callee_name = kind.fn_name(sym::oom);
+    //eprintln!("Codegen allocator shim {} -> {} ({:?} -> {:?})", caller_name, callee_name, sig.params, sig.returns);
+
+    let func_id = module
+        .declare_function("__rust_alloc_error_handler", Linkage::Export, &sig)
+        .unwrap();
+
+    let callee_func_id = module
+        .declare_function(&callee_name, Linkage::Import, &sig)
+        .unwrap();
+
+    let mut ctx = Context::new();
+    ctx.func = Function::with_name_signature(ExternalName::user(0, 0), sig);
+    {
+        let mut func_ctx = FunctionBuilderContext::new();
+        let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
+
+        let block = bcx.create_block();
+        bcx.switch_to_block(block);
+        let args = (&[usize_ty, usize_ty])
+            .iter()
+            .map(|&ty| bcx.append_block_param(block, ty))
+            .collect::<Vec<Value>>();
+
+        let callee_func_ref = module.declare_func_in_func(callee_func_id, &mut bcx.func);
+        bcx.ins().call(callee_func_ref, &args);
+
+        bcx.ins().trap(TrapCode::UnreachableCodeReached);
+        bcx.seal_all_blocks();
+        bcx.finalize();
     }
+    module
+        .define_function(
+            func_id,
+            &mut ctx,
+            &mut cranelift_codegen::binemit::NullTrapSink {},
+        )
+        .unwrap();
+    unwind_context.add_function(func_id, &ctx, module.isa());
 }