summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_codegen_gcc/src/callee.rs
blob: 9e3a22ee05d7ce1c51874636c0b121dc37fe8ec6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use gccjit::{FunctionType, RValue};
use rustc_codegen_ssa::traits::BaseTypeMethods;
use rustc_middle::ty::{self, Instance, TypeVisitableExt};
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt};

use crate::abi::FnAbiGccExt;
use crate::context::CodegenCx;

/// Codegens a reference to a fn/method item, monomorphizing and
/// inlining as it goes.
///
/// # Parameters
///
/// - `cx`: the crate context
/// - `instance`: the instance to be instantiated
pub fn get_fn<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, instance: Instance<'tcx>) -> RValue<'gcc> {
    let tcx = cx.tcx();

    assert!(!instance.substs.needs_infer());
    assert!(!instance.substs.has_escaping_bound_vars());

    if let Some(&func) = cx.function_instances.borrow().get(&instance) {
        return func;
    }

    let sym = tcx.symbol_name(instance).name;

    let fn_abi = cx.fn_abi_of_instance(instance, ty::List::empty());

    let func =
        if let Some(func) = cx.get_declared_value(&sym) {
            // Create a fn pointer with the new signature.
            let ptrty = fn_abi.ptr_to_gcc_type(cx);

            // This is subtle and surprising, but sometimes we have to bitcast
            // the resulting fn pointer.  The reason has to do with external
            // functions.  If you have two crates that both bind the same C
            // library, they may not use precisely the same types: for
            // example, they will probably each declare their own structs,
            // which are distinct types from LLVM's point of view (nominal
            // types).
            //
            // Now, if those two crates are linked into an application, and
            // they contain inlined code, you can wind up with a situation
            // where both of those functions wind up being loaded into this
            // application simultaneously. In that case, the same function
            // (from LLVM's point of view) requires two types. But of course
            // LLVM won't allow one function to have two types.
            //
            // What we currently do, therefore, is declare the function with
            // one of the two types (whichever happens to come first) and then
            // bitcast as needed when the function is referenced to make sure
            // it has the type we expect.
            //
            // This can occur on either a crate-local or crate-external
            // reference. It also occurs when testing libcore and in some
            // other weird situations. Annoying.
            if cx.val_ty(func) != ptrty {
                // TODO(antoyo): cast the pointer.
                func
            }
            else {
                func
            }
        }
        else {
            cx.linkage.set(FunctionType::Extern);
            let func = cx.declare_fn(&sym, &fn_abi);

            // TODO(antoyo): set linkage and attributes.
            func
        };

    cx.function_instances.borrow_mut().insert(instance, func);

    func
}