Skip to content

Commit beaf35a

Browse files
committed
CommonWriteMethods are not static any more
1 parent 614a6c1 commit beaf35a

File tree

10 files changed

+179
-85
lines changed

10 files changed

+179
-85
lines changed

src/librustc_codegen_llvm/back/lto.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use rustc::util::common::time_ext;
2525
use rustc_data_structures::fx::FxHashMap;
2626
use time_graph::Timeline;
2727
use {ModuleCodegen, ModuleLlvm, ModuleKind};
28+
use std::marker::PhantomData;
2829

2930
use libc;
3031

@@ -761,6 +762,7 @@ impl ThinModule {
761762
llmod_raw,
762763
llcx,
763764
tm,
765+
phantom: PhantomData
764766
},
765767
name: self.name().to_string(),
766768
kind: ModuleKind::Regular,

src/librustc_codegen_llvm/back/write.rs

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc::session::config::{self, OutputFilenames, OutputType, Passes, Sanitize
2626
use rustc::session::Session;
2727
use rustc::util::nodemap::FxHashMap;
2828
use time_graph::{self, TimeGraph, Timeline};
29-
use llvm::{self, DiagnosticInfo, PassManager, SMDiagnostic};
29+
use llvm::{self, DiagnosticInfo, PassManager, SMDiagnostic, BasicBlock, True};
3030
use llvm_util;
3131
use {CodegenResults, ModuleCodegen, CompiledModule, ModuleKind, // ModuleLlvm,
3232
CachedModuleCodegen};
@@ -44,10 +44,12 @@ use syntax::ext::hygiene::Mark;
4444
use syntax_pos::MultiSpan;
4545
use syntax_pos::symbol::Symbol;
4646
use type_::Type;
47-
use context::{is_pie_binary, get_reloc_model, CodegenCx};
48-
use interfaces::CommonWriteMethods;
47+
use context::{is_pie_binary, get_reloc_model};
48+
use interfaces::{Backend, CommonWriteMethods};
4949
use jobserver::{Client, Acquired};
5050
use rustc_demangle;
51+
use value::Value;
52+
use std::marker::PhantomData;
5153

5254
use std::any::Any;
5355
use std::ffi::{CString, CStr};
@@ -344,7 +346,7 @@ struct AssemblerCommand {
344346

345347
/// Additional resources used by optimize_and_codegen (not module specific)
346348
#[derive(Clone)]
347-
pub struct CodegenContext {
349+
pub struct CodegenContext<'ll> {
348350
// Resources needed when running LTO
349351
pub time_passes: bool,
350352
pub lto: Lto,
@@ -384,9 +386,12 @@ pub struct CodegenContext {
384386
time_graph: Option<TimeGraph>,
385387
// The assembler command if no_integrated_as option is enabled, None otherwise
386388
assembler_cmd: Option<Arc<AssemblerCommand>>,
389+
// This field is used to give a lifetime parameter to the struct so that it can implement
390+
// the Backend trait.
391+
phantom: PhantomData<&'ll ()>
387392
}
388393

389-
impl CodegenContext {
394+
impl CodegenContext<'ll> {
390395
pub fn create_diag_handler(&self) -> Handler {
391396
Handler::with_emitter(true, false, Box::new(self.diag_emitter.clone()))
392397
}
@@ -414,13 +419,49 @@ impl CodegenContext {
414419
}
415420
}
416421

422+
impl<'ll> Backend for CodegenContext<'ll> {
423+
type Value = &'ll Value;
424+
type BasicBlock = &'ll BasicBlock;
425+
type Type = &'ll Type;
426+
type Context = &'ll llvm::Context;
427+
}
428+
429+
impl CommonWriteMethods for CodegenContext<'ll> {
430+
fn val_ty(&self, v: &'ll Value) -> &'ll Type {
431+
unsafe {
432+
llvm::LLVMTypeOf(v)
433+
}
434+
}
435+
436+
fn c_bytes_in_context(&self, llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
437+
unsafe {
438+
let ptr = bytes.as_ptr() as *const c_char;
439+
return llvm::LLVMConstStringInContext(llcx, ptr, bytes.len() as c_uint, True);
440+
}
441+
}
442+
443+
fn c_struct_in_context(
444+
&self,
445+
llcx: &'a llvm::Context,
446+
elts: &[&'a Value],
447+
packed: bool,
448+
) -> &'a Value {
449+
unsafe {
450+
llvm::LLVMConstStructInContext(llcx,
451+
elts.as_ptr(), elts.len() as c_uint,
452+
packed as llvm::Bool)
453+
}
454+
}
455+
}
456+
457+
417458
pub struct DiagnosticHandlers<'a> {
418-
data: *mut (&'a CodegenContext, &'a Handler),
459+
data: *mut (&'a CodegenContext<'a>, &'a Handler),
419460
llcx: &'a llvm::Context,
420461
}
421462

422463
impl<'a> DiagnosticHandlers<'a> {
423-
pub fn new(cgcx: &'a CodegenContext,
464+
pub fn new(cgcx: &'a CodegenContext<'a>,
424465
handler: &'a Handler,
425466
llcx: &'a llvm::Context) -> Self {
426467
let data = Box::into_raw(Box::new((cgcx, handler)));
@@ -884,10 +925,10 @@ unsafe fn embed_bitcode(cgcx: &CodegenContext,
884925
llcx: &llvm::Context,
885926
llmod: &llvm::Module,
886927
bitcode: Option<&[u8]>) {
887-
let llconst = CodegenCx::c_bytes_in_context(llcx, bitcode.unwrap_or(&[]));
928+
let llconst = cgcx.c_bytes_in_context(llcx, bitcode.unwrap_or(&[]));
888929
let llglobal = llvm::LLVMAddGlobal(
889930
llmod,
890-
CodegenCx::val_ty(llconst),
931+
cgcx.val_ty(llconst),
891932
"rustc.embedded.module\0".as_ptr() as *const _,
892933
);
893934
llvm::LLVMSetInitializer(llglobal, llconst);
@@ -904,10 +945,10 @@ unsafe fn embed_bitcode(cgcx: &CodegenContext,
904945
llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage);
905946
llvm::LLVMSetGlobalConstant(llglobal, llvm::True);
906947

907-
let llconst = CodegenCx::c_bytes_in_context(llcx, &[]);
948+
let llconst = cgcx.c_bytes_in_context(llcx, &[]);
908949
let llglobal = llvm::LLVMAddGlobal(
909950
llmod,
910-
CodegenCx::val_ty(llconst),
951+
cgcx.val_ty(llconst),
911952
"rustc.embedded.cmdline\0".as_ptr() as *const _,
912953
);
913954
llvm::LLVMSetInitializer(llglobal, llconst);
@@ -1621,6 +1662,7 @@ fn start_executing_work(tcx: TyCtxt,
16211662
target_pointer_width: tcx.sess.target.target.target_pointer_width.clone(),
16221663
debuginfo: tcx.sess.opts.debuginfo,
16231664
assembler_cmd,
1665+
phantom: PhantomData
16241666
};
16251667

16261668
// This is the "main loop" of parallel work happening for parallel codegen.
@@ -2091,7 +2133,7 @@ pub const CODEGEN_WORK_PACKAGE_KIND: time_graph::WorkPackageKind =
20912133
const LLVM_WORK_PACKAGE_KIND: time_graph::WorkPackageKind =
20922134
time_graph::WorkPackageKind(&["#7DB67A", "#C6EEC4", "#ACDAAA", "#579354", "#3E6F3C"]);
20932135

2094-
fn spawn_work(cgcx: CodegenContext, work: WorkItem) {
2136+
fn spawn_work(cgcx: CodegenContext<'static>, work: WorkItem) {
20952137
let depth = time_depth();
20962138

20972139
thread::spawn(move || {

src/librustc_codegen_llvm/base.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,13 @@ pub fn coerce_unsized_into(
334334
}
335335

336336
pub fn cast_shift_expr_rhs(
337-
cx: &Builder<'_, 'll, '_, &'ll Value>, op: hir::BinOpKind, lhs: &'ll Value, rhs: &'ll Value
337+
bx: &Builder<'_, 'll, '_, &'ll Value>, op: hir::BinOpKind, lhs: &'ll Value, rhs: &'ll Value
338338
) -> &'ll Value {
339-
cast_shift_rhs(op, lhs, rhs, |a, b| cx.trunc(a, b), |a, b| cx.zext(a, b))
339+
cast_shift_rhs(bx, op, lhs, rhs, |a, b| bx.trunc(a, b), |a, b| bx.zext(a, b))
340340
}
341341

342-
fn cast_shift_rhs<'ll, F, G>(op: hir::BinOpKind,
342+
fn cast_shift_rhs<'ll, F, G>(bx: &Builder<'_, 'll, '_, &'ll Value>,
343+
op: hir::BinOpKind,
343344
lhs: &'ll Value,
344345
rhs: &'ll Value,
345346
trunc: F,
@@ -350,8 +351,8 @@ fn cast_shift_rhs<'ll, F, G>(op: hir::BinOpKind,
350351
{
351352
// Shifts may have any size int on the rhs
352353
if op.is_shift() {
353-
let mut rhs_llty = CodegenCx::val_ty(rhs);
354-
let mut lhs_llty = CodegenCx::val_ty(lhs);
354+
let mut rhs_llty = bx.cx().val_ty(rhs);
355+
let mut lhs_llty = bx.cx().val_ty(lhs);
355356
if rhs_llty.kind() == TypeKind::Vector {
356357
rhs_llty = rhs_llty.element_type()
357358
}
@@ -392,7 +393,7 @@ pub fn from_immediate<'a, 'll: 'a, 'tcx: 'll>(
392393
bx: &Builder<'_ ,'ll, '_, &'ll Value>,
393394
val: &'ll Value
394395
) -> &'ll Value {
395-
if CodegenCx::val_ty(val) == Type::i1(bx.cx()) {
396+
if bx.cx().val_ty(val) == Type::i1(bx.cx()) {
396397
bx.zext(val, Type::i8(bx.cx()))
397398
} else {
398399
val
@@ -432,7 +433,7 @@ pub fn call_memcpy<'a, 'll: 'a, 'tcx: 'll>(
432433
if flags.contains(MemFlags::NONTEMPORAL) {
433434
// HACK(nox): This is inefficient but there is no nontemporal memcpy.
434435
let val = bx.load(src, align);
435-
let ptr = bx.pointercast(dst, CodegenCx::val_ty(val).ptr_to());
436+
let ptr = bx.pointercast(dst, bx.cx().val_ty(val).ptr_to());
436437
bx.store_with_flags(val, ptr, align, flags);
437438
return;
438439
}
@@ -653,12 +654,12 @@ fn write_metadata<'a, 'gcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>,
653654
DeflateEncoder::new(&mut compressed, Compression::fast())
654655
.write_all(&metadata.raw_data).unwrap();
655656

656-
let llmeta = CodegenCx::c_bytes_in_context(metadata_llcx, &compressed);
657-
let llconst = CodegenCx::c_struct_in_context(metadata_llcx, &[llmeta], false);
657+
let llmeta = llvm_module.c_bytes_in_context(metadata_llcx, &compressed);
658+
let llconst = llvm_module.c_struct_in_context(metadata_llcx, &[llmeta], false);
658659
let name = exported_symbols::metadata_symbol_name(tcx);
659660
let buf = CString::new(name).unwrap();
660661
let llglobal = unsafe {
661-
llvm::LLVMAddGlobal(metadata_llmod, CodegenCx::val_ty(llconst), buf.as_ptr())
662+
llvm::LLVMAddGlobal(metadata_llmod, llvm_module.val_ty(llconst), buf.as_ptr())
662663
};
663664
unsafe {
664665
llvm::LLVMSetInitializer(llglobal, llconst);
@@ -1246,7 +1247,7 @@ fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12461247
// Run replace-all-uses-with for statics that need it
12471248
for &(old_g, new_g) in cx.statics_to_rauw.borrow().iter() {
12481249
unsafe {
1249-
let bitcast = llvm::LLVMConstPointerCast(new_g, CodegenCx::val_ty(old_g));
1250+
let bitcast = llvm::LLVMConstPointerCast(new_g, cx.val_ty(old_g));
12501251
llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
12511252
llvm::LLVMDeleteGlobal(old_g);
12521253
}
@@ -1261,7 +1262,7 @@ fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12611262

12621263
unsafe {
12631264
let g = llvm::LLVMAddGlobal(cx.llmod,
1264-
CodegenCx::val_ty(array),
1265+
cx.val_ty(array),
12651266
name.as_ptr());
12661267
llvm::LLVMSetInitializer(g, array);
12671268
llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);

src/librustc_codegen_llvm/builder.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ impl BuilderMethods<'a, 'll, 'tcx>
539539

540540
fn range_metadata(&self, load: &'ll Value, range: Range<u128>) {
541541
unsafe {
542-
let llty = CodegenCx::val_ty(load);
542+
let llty = self.cx.val_ty(load);
543543
let v = [
544544
self.cx.c_uint_big(llty, range.start),
545545
self.cx.c_uint_big(llty, range.end)
@@ -773,7 +773,7 @@ impl BuilderMethods<'a, 'll, 'tcx>
773773

774774
let argtys = inputs.iter().map(|v| {
775775
debug!("Asm Input Type: {:?}", *v);
776-
CodegenCx::val_ty(*v)
776+
self.cx.val_ty(*v)
777777
}).collect::<Vec<_>>();
778778

779779
debug!("Asm Output Type: {:?}", output);
@@ -846,7 +846,7 @@ impl BuilderMethods<'a, 'll, 'tcx>
846846

847847
fn vector_splat(&self, num_elts: usize, elt: &'ll Value) -> &'ll Value {
848848
unsafe {
849-
let elt_ty = CodegenCx::val_ty(elt);
849+
let elt_ty = self.cx.val_ty(elt);
850850
let undef = llvm::LLVMGetUndef(type_::Type::vector(elt_ty, num_elts as u64));
851851
let vec = self.insert_element(undef, elt, CodegenCx::c_i32(self.cx, 0));
852852
let vec_i32_ty = type_::Type::vector(type_::Type::i32(self.cx), num_elts as u64);
@@ -1157,8 +1157,8 @@ impl BuilderMethods<'a, 'll, 'tcx>
11571157
fn check_store<'b>(&self,
11581158
val: &'ll Value,
11591159
ptr: &'ll Value) -> &'ll Value {
1160-
let dest_ptr_ty = CodegenCx::val_ty(ptr);
1161-
let stored_ty = CodegenCx::val_ty(val);
1160+
let dest_ptr_ty = self.cx.val_ty(ptr);
1161+
let stored_ty = self.cx.val_ty(val);
11621162
let stored_ptr_ty = stored_ty.ptr_to();
11631163

11641164
assert_eq!(dest_ptr_ty.kind(), llvm::TypeKind::Pointer);
@@ -1178,7 +1178,7 @@ impl BuilderMethods<'a, 'll, 'tcx>
11781178
typ: &str,
11791179
llfn: &'ll Value,
11801180
args: &'b [&'ll Value]) -> Cow<'b, [&'ll Value]> {
1181-
let mut fn_ty = CodegenCx::val_ty(llfn);
1181+
let mut fn_ty = self.cx.val_ty(llfn);
11821182
// Strip off pointers
11831183
while fn_ty.kind() == llvm::TypeKind::Pointer {
11841184
fn_ty = fn_ty.element_type();
@@ -1190,7 +1190,7 @@ impl BuilderMethods<'a, 'll, 'tcx>
11901190
let param_tys = fn_ty.func_params();
11911191

11921192
let all_args_match = param_tys.iter()
1193-
.zip(args.iter().map(|&v| CodegenCx::val_ty(v)))
1193+
.zip(args.iter().map(|&v| self.cx().val_ty(v)))
11941194
.all(|(expected_ty, actual_ty)| *expected_ty == actual_ty);
11951195

11961196
if all_args_match {
@@ -1201,7 +1201,7 @@ impl BuilderMethods<'a, 'll, 'tcx>
12011201
.zip(args.iter())
12021202
.enumerate()
12031203
.map(|(i, (expected_ty, &actual_val))| {
1204-
let actual_ty = CodegenCx::val_ty(actual_val);
1204+
let actual_ty = self.cx().val_ty(actual_val);
12051205
if expected_ty != actual_ty {
12061206
debug!("Type mismatch in function call of {:?}. \
12071207
Expected {:?} for param {}, got {:?}; injecting bitcast",

src/librustc_codegen_llvm/callee.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub fn get_fn(
8484
// This can occur on either a crate-local or crate-external
8585
// reference. It also occurs when testing libcore and in some
8686
// other weird situations. Annoying.
87-
if CodegenCx::val_ty(llfn) != llptrty {
87+
if cx.val_ty(llfn) != llptrty {
8888
debug!("get_fn: casting {:?} to {:?}", llfn, llptrty);
8989
consts::ptrcast(llfn, llptrty)
9090
} else {
@@ -93,7 +93,7 @@ pub fn get_fn(
9393
}
9494
} else {
9595
let llfn = declare::declare_fn(cx, &sym, fn_ty);
96-
assert_eq!(CodegenCx::val_ty(llfn), llptrty);
96+
assert_eq!(cx.val_ty(llfn), llptrty);
9797
debug!("get_fn: not casting pointer!");
9898

9999
if instance.def.is_inline(tcx) {

0 commit comments

Comments
 (0)