Skip to content

Commit 431601c

Browse files
committed
Greate separation of librsutc_codegen_llvm : librustc_codegen_ssa compiles
1 parent 26e18a1 commit 431601c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1760
-1475
lines changed

src/librustc_codegen_llvm/base.rs

Lines changed: 8 additions & 1029 deletions
Large diffs are not rendered by default.

src/librustc_codegen_llvm/builder.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,6 @@ fn noname() -> *const c_char {
5252
&CNULL
5353
}
5454

55-
bitflags! {
56-
pub struct MemFlags: u8 {
57-
const VOLATILE = 1 << 0;
58-
const NONTEMPORAL = 1 << 1;
59-
const UNALIGNED = 1 << 2;
60-
}
61-
}
62-
6355
impl HasCodegen<'a, 'll, 'tcx> for Builder<'a, 'll, 'tcx, &'ll Value> {
6456
type CodegenCx = CodegenCx<'ll, 'tcx, &'ll Value>;
6557
}

src/librustc_codegen_llvm/callee.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,3 @@ pub fn get_fn(
203203

204204
llfn
205205
}
206-
207-
pub fn resolve_and_get_fn<'ll, 'tcx: 'll, Cx : CodegenMethods<'ll, 'tcx>>(
208-
cx: &Cx,
209-
def_id: DefId,
210-
substs: &'tcx Substs<'tcx>,
211-
) -> Cx::Value {
212-
cx.get_fn(
213-
ty::Instance::resolve(
214-
*cx.tcx(),
215-
ty::ParamEnv::reveal_all(),
216-
def_id,
217-
substs
218-
).unwrap()
219-
)
220-
}

src/librustc_codegen_llvm/common.rs

Lines changed: 0 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -370,137 +370,3 @@ pub fn struct_in_context(
370370
fn hi_lo_to_u128(lo: u64, hi: u64) -> u128 {
371371
((hi as u128) << 64) | (lo as u128)
372372
}
373-
374-
pub fn langcall(tcx: TyCtxt,
375-
span: Option<Span>,
376-
msg: &str,
377-
li: LangItem)
378-
-> DefId {
379-
tcx.lang_items().require(li).unwrap_or_else(|s| {
380-
let msg = format!("{} {}", msg, s);
381-
match span {
382-
Some(span) => tcx.sess.span_fatal(span, &msg[..]),
383-
None => tcx.sess.fatal(&msg[..]),
384-
}
385-
})
386-
}
387-
388-
// To avoid UB from LLVM, these two functions mask RHS with an
389-
// appropriate mask unconditionally (i.e. the fallback behavior for
390-
// all shifts). For 32- and 64-bit types, this matches the semantics
391-
// of Java. (See related discussion on #1877 and #10183.)
392-
393-
pub fn build_unchecked_lshift<'a, 'll: 'a, 'tcx: 'll, Bx: BuilderMethods<'a, 'll, 'tcx>>(
394-
bx: &Bx,
395-
lhs: <Bx::CodegenCx as Backend<'ll>>::Value,
396-
rhs: <Bx::CodegenCx as Backend<'ll>>::Value
397-
) -> <Bx::CodegenCx as Backend<'ll>>::Value {
398-
let rhs = base::cast_shift_expr_rhs(bx, hir::BinOpKind::Shl, lhs, rhs);
399-
// #1877, #10183: Ensure that input is always valid
400-
let rhs = shift_mask_rhs(bx, rhs);
401-
bx.shl(lhs, rhs)
402-
}
403-
404-
pub fn build_unchecked_rshift<'a, 'll: 'a, 'tcx: 'll, Bx: BuilderMethods<'a, 'll, 'tcx>>(
405-
bx: &Bx,
406-
lhs_t: Ty<'tcx>,
407-
lhs: <Bx::CodegenCx as Backend<'ll>>::Value,
408-
rhs: <Bx::CodegenCx as Backend<'ll>>::Value
409-
) -> <Bx::CodegenCx as Backend<'ll>>::Value {
410-
let rhs = base::cast_shift_expr_rhs(bx, hir::BinOpKind::Shr, lhs, rhs);
411-
// #1877, #10183: Ensure that input is always valid
412-
let rhs = shift_mask_rhs(bx, rhs);
413-
let is_signed = lhs_t.is_signed();
414-
if is_signed {
415-
bx.ashr(lhs, rhs)
416-
} else {
417-
bx.lshr(lhs, rhs)
418-
}
419-
}
420-
421-
fn shift_mask_rhs<'a, 'll: 'a, 'tcx: 'll, Bx: BuilderMethods<'a, 'll, 'tcx>>(
422-
bx: &Bx,
423-
rhs: <Bx::CodegenCx as Backend<'ll>>::Value
424-
) -> <Bx::CodegenCx as Backend<'ll>>::Value {
425-
let rhs_llty = bx.cx().val_ty(rhs);
426-
bx.and(rhs, shift_mask_val(bx, rhs_llty, rhs_llty, false))
427-
}
428-
429-
pub fn shift_mask_val<'a, 'll: 'a, 'tcx: 'll, Bx: BuilderMethods<'a, 'll, 'tcx>>(
430-
bx: &Bx,
431-
llty: <Bx::CodegenCx as Backend<'ll>>::Type,
432-
mask_llty: <Bx::CodegenCx as Backend<'ll>>::Type,
433-
invert: bool
434-
) -> <Bx::CodegenCx as Backend<'ll>>::Value {
435-
let kind = bx.cx().type_kind(llty);
436-
match kind {
437-
TypeKind::Integer => {
438-
// i8/u8 can shift by at most 7, i16/u16 by at most 15, etc.
439-
let val = bx.cx().int_width(llty) - 1;
440-
if invert {
441-
bx.cx().const_int(mask_llty, !val as i64)
442-
} else {
443-
bx.cx().const_uint(mask_llty, val)
444-
}
445-
},
446-
TypeKind::Vector => {
447-
let mask = shift_mask_val(
448-
bx,
449-
bx.cx().element_type(llty),
450-
bx.cx().element_type(mask_llty),
451-
invert
452-
);
453-
bx.vector_splat(bx.cx().vector_length(mask_llty), mask)
454-
},
455-
_ => bug!("shift_mask_val: expected Integer or Vector, found {:?}", kind),
456-
}
457-
}
458-
459-
pub fn ty_fn_sig<'ll, 'tcx:'ll, Cx: CodegenMethods<'ll, 'tcx>>(
460-
cx: &Cx,
461-
ty: Ty<'tcx>
462-
) -> ty::PolyFnSig<'tcx> {
463-
match ty.sty {
464-
ty::FnDef(..) |
465-
// Shims currently have type FnPtr. Not sure this should remain.
466-
ty::FnPtr(_) => ty.fn_sig(*cx.tcx()),
467-
ty::Closure(def_id, substs) => {
468-
let tcx = *cx.tcx();
469-
let sig = substs.closure_sig(def_id, tcx);
470-
471-
let env_ty = tcx.closure_env_ty(def_id, substs).unwrap();
472-
sig.map_bound(|sig| tcx.mk_fn_sig(
473-
iter::once(*env_ty.skip_binder()).chain(sig.inputs().iter().cloned()),
474-
sig.output(),
475-
sig.variadic,
476-
sig.unsafety,
477-
sig.abi
478-
))
479-
}
480-
ty::Generator(def_id, substs, _) => {
481-
let tcx = *cx.tcx();
482-
let sig = substs.poly_sig(def_id, tcx);
483-
484-
let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
485-
let env_ty = tcx.mk_mut_ref(tcx.mk_region(env_region), ty);
486-
487-
sig.map_bound(|sig| {
488-
let state_did = tcx.lang_items().gen_state().unwrap();
489-
let state_adt_ref = tcx.adt_def(state_did);
490-
let state_substs = tcx.intern_substs(&[
491-
sig.yield_ty.into(),
492-
sig.return_ty.into(),
493-
]);
494-
let ret_ty = tcx.mk_adt(state_adt_ref, state_substs);
495-
496-
tcx.mk_fn_sig(iter::once(env_ty),
497-
ret_ty,
498-
false,
499-
hir::Unsafety::Normal,
500-
Abi::Rust
501-
)
502-
})
503-
}
504-
_ => bug!("unexpected type {:?} to ty_fn_sig", ty)
505-
}
506-
}

src/librustc_codegen_llvm/consts.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,61 @@ use rustc::hir::{self, CodegenFnAttrs, CodegenFnAttrFlags};
3232
use std::ffi::{CStr, CString};
3333

3434

35+
pub fn const_alloc_to_llvm(cx: &CodegenCx<'ll, '_, &'ll Value>, alloc: &Allocation) -> &'ll Value {
36+
let mut llvals = Vec::with_capacity(alloc.relocations.len() + 1);
37+
let layout = cx.data_layout();
38+
let pointer_size = layout.pointer_size.bytes() as usize;
39+
40+
let mut next_offset = 0;
41+
for &(offset, alloc_id) in alloc.relocations.iter() {
42+
let offset = offset.bytes();
43+
assert_eq!(offset as usize as u64, offset);
44+
let offset = offset as usize;
45+
if offset > next_offset {
46+
llvals.push(cx.const_bytes(&alloc.bytes[next_offset..offset]));
47+
}
48+
let ptr_offset = read_target_uint(
49+
layout.endian,
50+
&alloc.bytes[offset..(offset + pointer_size)],
51+
).expect("const_alloc_to_llvm: could not read relocation pointer") as u64;
52+
llvals.push(cx.scalar_to_backend(
53+
Pointer { alloc_id, offset: Size::from_bytes(ptr_offset) }.into(),
54+
&layout::Scalar {
55+
value: layout::Primitive::Pointer,
56+
valid_range: 0..=!0
57+
},
58+
cx.type_i8p()
59+
));
60+
next_offset = offset + pointer_size;
61+
}
62+
if alloc.bytes.len() >= next_offset {
63+
llvals.push(cx.const_bytes(&alloc.bytes[next_offset ..]));
64+
}
65+
66+
cx.const_struct(&llvals, true)
67+
}
68+
69+
pub fn codegen_static_initializer(
70+
cx: &CodegenCx<'ll, 'tcx, &'ll Value>,
71+
def_id: DefId,
72+
) -> Result<(&'ll Value, &'tcx Allocation), Lrc<ConstEvalErr<'tcx>>> {
73+
let instance = ty::Instance::mono(cx.tcx, def_id);
74+
let cid = GlobalId {
75+
instance,
76+
promoted: None,
77+
};
78+
let param_env = ty::ParamEnv::reveal_all();
79+
let static_ = cx.tcx.const_eval(param_env.and(cid))?;
80+
81+
let alloc = match static_.val {
82+
ConstValue::ByRef(_, alloc, n) if n.bytes() == 0 => alloc,
83+
_ => bug!("static const eval returned {:#?}", static_),
84+
};
85+
Ok((const_alloc_to_llvm(cx, alloc), alloc))
86+
}
87+
88+
89+
3590
fn set_global_alignment(cx: &CodegenCx<'ll, '_, &'ll Value>,
3691
gv: &'ll Value,
3792
mut align: Align) {

src/librustc_codegen_llvm/context.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,9 @@ impl MiscMethods<'ll, 'tcx> for CodegenCx<'ll, 'tcx, &'ll Value> {
446446
attributes::apply_target_cpu_attr(self, llfn)
447447
}
448448

449+
fn env_alloca_allowed(&self) {
450+
unsafe { llvm::LLVMRustVersionMajor() < 6 }
451+
}
449452

450453
fn create_used_variable(&self) {
451454
let name = const_cstr!("llvm.used");

src/librustc_codegen_llvm/debuginfo/create_scope_map.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,6 @@ use rustc_data_structures::indexed_vec::{Idx, IndexVec};
2727

2828
use syntax_pos::BytePos;
2929

30-
#[derive(Clone, Copy, Debug)]
31-
pub struct MirDebugScope<D> {
32-
pub scope_metadata: Option<D>,
33-
// Start and end offsets of the file to which this DIScope belongs.
34-
// These are used to quickly determine whether some span refers to the same file.
35-
pub file_start_pos: BytePos,
36-
pub file_end_pos: BytePos,
37-
}
38-
39-
impl<D> MirDebugScope<D> {
40-
pub fn is_valid(&self) -> bool {
41-
self.scope_metadata.is_some()
42-
}
43-
}
44-
4530
/// Produce DIScope DIEs for each MIR Scope which has variables defined in it.
4631
/// If debuginfo is disabled, the returned vector is empty.
4732
pub fn create_mir_scopes(

src/librustc_codegen_llvm/debuginfo/mod.rs

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -111,53 +111,6 @@ impl<'a, 'tcx> CrateDebugContext<'a, 'tcx> {
111111
}
112112
}
113113

114-
pub enum FunctionDebugContext<'ll> {
115-
RegularContext(FunctionDebugContextData<'ll>),
116-
DebugInfoDisabled,
117-
FunctionWithoutDebugInfo,
118-
}
119-
120-
impl FunctionDebugContext<'ll> {
121-
pub fn get_ref<'a>(&'a self, span: Span) -> &'a FunctionDebugContextData<'ll> {
122-
match *self {
123-
FunctionDebugContext::RegularContext(ref data) => data,
124-
FunctionDebugContext::DebugInfoDisabled => {
125-
span_bug!(span, "{}", FunctionDebugContext::debuginfo_disabled_message());
126-
}
127-
FunctionDebugContext::FunctionWithoutDebugInfo => {
128-
span_bug!(span, "{}", FunctionDebugContext::should_be_ignored_message());
129-
}
130-
}
131-
}
132-
133-
fn debuginfo_disabled_message() -> &'static str {
134-
"debuginfo: Error trying to access FunctionDebugContext although debug info is disabled!"
135-
}
136-
137-
fn should_be_ignored_message() -> &'static str {
138-
"debuginfo: Error trying to access FunctionDebugContext for function that should be \
139-
ignored by debug info!"
140-
}
141-
}
142-
143-
pub struct FunctionDebugContextData<'ll> {
144-
fn_metadata: &'ll DISubprogram,
145-
source_locations_enabled: Cell<bool>,
146-
pub defining_crate: CrateNum,
147-
}
148-
149-
pub enum VariableAccess<'a, V> {
150-
// The llptr given is an alloca containing the variable's value
151-
DirectVariable { alloca: V },
152-
// The llptr given is an alloca containing the start of some pointer chain
153-
// leading to the variable's content.
154-
IndirectVariable { alloca: V, address_operations: &'a [i64] }
155-
}
156-
157-
pub enum VariableKind {
158-
ArgumentVariable(usize /*index*/),
159-
LocalVariable,
160-
}
161114

162115
/// Create any deferred debug metadata nodes
163116
pub fn finalize(cx: &CodegenCx<'ll, '_, &'ll Value>) {
@@ -596,4 +549,13 @@ impl<'ll, 'tcx: 'll> DebugInfoMethods<'ll, 'tcx> for CodegenCx<'ll, 'tcx, &'ll V
596549
fn debuginfo_finalize(&self) {
597550
finalize(self)
598551
}
552+
553+
fn debuginfo_upvar_decls_ops_sequence(&self, byte_offset_of_var_in_env: u64) -> &[i64] {
554+
unsafe {
555+
[llvm::LLVMRustDIBuilderCreateOpDeref(),
556+
llvm::LLVMRustDIBuilderCreateOpPlusUconst(),
557+
byte_offset_of_var_in_env as i64,
558+
llvm::LLVMRustDIBuilderCreateOpDeref()]
559+
};
560+
}
599561
}

src/librustc_codegen_llvm/debuginfo/source_loc.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,6 @@ pub fn set_source_location(
5151
set_debug_location(bx, dbg_loc);
5252
}
5353

54-
/// Enables emitting source locations for the given functions.
55-
///
56-
/// Since we don't want source locations to be emitted for the function prelude,
57-
/// they are disabled when beginning to codegen a new function. This functions
58-
/// switches source location emitting on and must therefore be called before the
59-
/// first real statement/expression of the function is codegened.
60-
pub fn start_emitting_source_locations(dbg_context: &FunctionDebugContext<'ll>) {
61-
if let FunctionDebugContext::RegularContext(ref data) = *dbg_context {
62-
data.source_locations_enabled.set(true);
63-
}
64-
}
65-
6654

6755
#[derive(Copy, Clone, PartialEq)]
6856
pub enum InternalDebugLocation<'ll> {

src/librustc_codegen_llvm/interfaces/mod.rs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
mod builder;
12-
mod consts;
13-
mod type_;
14-
mod intrinsic;
15-
mod debuginfo;
16-
mod abi;
17-
mod asm;
18-
19-
pub use self::builder::{BuilderMethods, HasCodegen};
2011
pub use rustc_codegen_ssa::interfaces::{Backend, BackendMethods, CodegenObject, MiscMethods,
21-
StaticMethods, DeclareMethods, PreDefineMethods};
22-
pub use self::consts::ConstMethods;
23-
pub use self::type_::{TypeMethods, BaseTypeMethods, DerivedTypeMethods,
24-
LayoutTypeMethods, ArgTypeMethods};
25-
pub use self::intrinsic::{IntrinsicCallMethods, IntrinsicDeclarationMethods};
26-
pub use self::debuginfo::{DebugInfoMethods, DebugInfoBuilderMethods};
27-
pub use self::abi::{AbiMethods, AbiBuilderMethods};
28-
pub use self::asm::{AsmMethods, AsmBuilderMethods};
29-
30-
pub trait CodegenMethods<'ll, 'tcx: 'll> :
31-
Backend<'ll> + TypeMethods<'ll, 'tcx> + MiscMethods<'ll, 'tcx> + ConstMethods<'ll, 'tcx> +
32-
StaticMethods<'ll> + DebugInfoMethods<'ll, 'tcx> + AbiMethods<'tcx> +
33-
IntrinsicDeclarationMethods<'ll> + DeclareMethods<'ll, 'tcx> + AsmMethods +
34-
PreDefineMethods<'ll, 'tcx> {}
12+
StaticMethods, DeclareMethods, PreDefineMethods, BuilderMethods, HasCodegen, ConstMethods,
13+
TypeMethods, BaseTypeMethods, DerivedTypeMethods, LayoutTypeMethods, ArgTypeMethods,
14+
IntrinsicCallMethods, IntrinsicDeclarationMethods, DebugInfoMethods, DebugInfoBuilderMethods,
15+
AbiMethods, AbiBuilderMethods, AsmMethods, AsmBuilderMethods, CodegenMethods};

0 commit comments

Comments
 (0)