Skip to content

Commit 2a6412b

Browse files
committed
Added some docs + start to &mut self builder methods
1 parent 58c8f32 commit 2a6412b

File tree

11 files changed

+70
-53
lines changed

11 files changed

+70
-53
lines changed

src/librustc_codegen_llvm/builder.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl BuilderMethods<'a, 'll, 'tcx>
6464
llfn: &'ll Value,
6565
name: &'b str
6666
) -> Self {
67-
let bx = Builder::with_cx(cx);
67+
let mut bx = Builder::with_cx(cx);
6868
let llbb = unsafe {
6969
let name = SmallCStr::new(name);
7070
llvm::LLVMAppendBasicBlockInContext(
@@ -121,48 +121,48 @@ impl BuilderMethods<'a, 'll, 'tcx>
121121
}
122122
}
123123

124-
fn set_value_name(&self, value: &'ll Value, name: &str) {
124+
fn set_value_name(&mut self, value: &'ll Value, name: &str) {
125125
let cname = SmallCStr::new(name);
126126
unsafe {
127127
llvm::LLVMSetValueName(value, cname.as_ptr());
128128
}
129129
}
130130

131-
fn position_at_end(&self, llbb: &'ll BasicBlock) {
131+
fn position_at_end(&mut self, llbb: &'ll BasicBlock) {
132132
unsafe {
133133
llvm::LLVMPositionBuilderAtEnd(self.llbuilder, llbb);
134134
}
135135
}
136136

137-
fn position_at_start(&self, llbb: &'ll BasicBlock) {
137+
fn position_at_start(&mut self, llbb: &'ll BasicBlock) {
138138
unsafe {
139139
llvm::LLVMRustPositionBuilderAtStart(self.llbuilder, llbb);
140140
}
141141
}
142142

143-
fn ret_void(&self) {
143+
fn ret_void(&mut self) {
144144
self.count_insn("retvoid");
145145
unsafe {
146146
llvm::LLVMBuildRetVoid(self.llbuilder);
147147
}
148148
}
149149

150-
fn ret(&self, v: &'ll Value) {
150+
fn ret(&mut self, v: &'ll Value) {
151151
self.count_insn("ret");
152152
unsafe {
153153
llvm::LLVMBuildRet(self.llbuilder, v);
154154
}
155155
}
156156

157-
fn br(&self, dest: &'ll BasicBlock) {
157+
fn br(&mut self, dest: &'ll BasicBlock) {
158158
self.count_insn("br");
159159
unsafe {
160160
llvm::LLVMBuildBr(self.llbuilder, dest);
161161
}
162162
}
163163

164164
fn cond_br(
165-
&self,
165+
&mut self,
166166
cond: &'ll Value,
167167
then_llbb: &'ll BasicBlock,
168168
else_llbb: &'ll BasicBlock,
@@ -446,7 +446,7 @@ impl BuilderMethods<'a, 'll, 'tcx>
446446
}
447447

448448
fn alloca(&self, ty: &'ll Type, name: &str, align: Align) -> &'ll Value {
449-
let bx = Builder::with_cx(self.cx);
449+
let mut bx = Builder::with_cx(self.cx);
450450
bx.position_at_start(unsafe {
451451
llvm::LLVMGetFirstBasicBlock(self.llfn())
452452
});

src/librustc_codegen_llvm/intrinsic.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -803,10 +803,10 @@ fn codegen_msvc_try(
803803

804804
bx.set_personality_fn(bx.cx().eh_personality());
805805

806-
let normal = bx.build_sibling_block("normal");
806+
let mut normal = bx.build_sibling_block("normal");
807807
let catchswitch = bx.build_sibling_block("catchswitch");
808808
let catchpad = bx.build_sibling_block("catchpad");
809-
let caught = bx.build_sibling_block("caught");
809+
let mut caught = bx.build_sibling_block("caught");
810810

811811
let func = llvm::get_param(bx.llfn(), 0);
812812
let data = llvm::get_param(bx.llfn(), 1);
@@ -927,8 +927,8 @@ fn codegen_gnu_try(
927927
// expected to be `*mut *mut u8` for this to actually work, but that's
928928
// managed by the standard library.
929929

930-
let then = bx.build_sibling_block("then");
931-
let catch = bx.build_sibling_block("catch");
930+
let mut then = bx.build_sibling_block("then");
931+
let mut catch = bx.build_sibling_block("catch");
932932

933933
let func = llvm::get_param(bx.llfn(), 0);
934934
let data = llvm::get_param(bx.llfn(), 1);

src/librustc_codegen_llvm/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ mod value;
133133

134134
pub struct LlvmCodegenBackend(());
135135

136-
impl BackendMethods for LlvmCodegenBackend {
136+
impl ExtraBackendMethods for LlvmCodegenBackend {
137137
type Metadata = ModuleLlvm;
138138
type OngoingCodegen = OngoingCodegen;
139139

src/librustc_codegen_ssa/base.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ pub fn maybe_create_entry_wrapper<'a, 'll: 'a, 'tcx: 'll, Bx: BuilderMethods<'a,
535535
cx.set_frame_pointer_elimination(llfn);
536536
cx.apply_target_cpu_attr(llfn);
537537

538-
let bx = Bx::new_block(&cx, llfn, "top");
538+
let mut bx = Bx::new_block(&cx, llfn, "top");
539539

540540
bx.insert_reference_to_gdb_debug_scripts_section_global();
541541

@@ -571,7 +571,7 @@ pub const CODEGEN_WORK_PACKAGE_KIND: time_graph::WorkPackageKind =
571571
time_graph::WorkPackageKind(&["#DE9597", "#FED1D3", "#FDC5C7", "#B46668", "#88494B"]);
572572

573573

574-
pub fn codegen_crate<B : BackendMethods>(
574+
pub fn codegen_crate<B : ExtraBackendMethods>(
575575
backend: B,
576576
tcx: TyCtxt<'ll, 'tcx, 'tcx>,
577577
rx: mpsc::Receiver<Box<dyn Any + Send>>

src/librustc_codegen_ssa/interfaces/backend.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use super::CodegenObject;
1212
use {ModuleCodegen, CachedModuleCodegen};
1313
use rustc::session::Session;
14+
use rustc_codegen_utils::codegen_backend::CodegenBackend;
1415
use rustc::middle::cstore::EncodedMetadata;
1516
use rustc::middle::allocator::AllocatorKind;
1617
use rustc::ty::TyCtxt;
@@ -27,7 +28,7 @@ pub trait Backend<'ll> {
2728
type Context;
2829
}
2930

30-
pub trait BackendMethods {
31+
pub trait ExtraBackendMethods : CodegenBackend {
3132
type Metadata;
3233
type OngoingCodegen;
3334

src/librustc_codegen_ssa/interfaces/builder.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ pub trait BuilderMethods<'a, 'll :'a, 'tcx: 'll> : HasCodegen<'a, 'll, 'tcx> +
5050
fn llbb(&self) -> <Self::CodegenCx as Backend<'ll>>::BasicBlock;
5151
fn count_insn(&self, category: &str);
5252

53-
fn set_value_name(&self, value: <Self::CodegenCx as Backend<'ll>>::Value, name: &str);
54-
fn position_at_end(&self, llbb: <Self::CodegenCx as Backend<'ll>>::BasicBlock);
55-
fn position_at_start(&self, llbb: <Self::CodegenCx as Backend<'ll>>::BasicBlock);
56-
fn ret_void(&self);
57-
fn ret(&self, v: <Self::CodegenCx as Backend<'ll>>::Value);
58-
fn br(&self, dest: <Self::CodegenCx as Backend<'ll>>::BasicBlock);
53+
fn set_value_name(&mut self, value: <Self::CodegenCx as Backend<'ll>>::Value, name: &str);
54+
fn position_at_end(&mut self, llbb: <Self::CodegenCx as Backend<'ll>>::BasicBlock);
55+
fn position_at_start(&mut self, llbb: <Self::CodegenCx as Backend<'ll>>::BasicBlock);
56+
fn ret_void(&mut self);
57+
fn ret(&mut self, v: <Self::CodegenCx as Backend<'ll>>::Value);
58+
fn br(&mut self, dest: <Self::CodegenCx as Backend<'ll>>::BasicBlock);
5959
fn cond_br(
60-
&self,
60+
&mut self,
6161
cond: <Self::CodegenCx as Backend<'ll>>::Value,
6262
then_llbb: <Self::CodegenCx as Backend<'ll>>::BasicBlock,
6363
else_llbb: <Self::CodegenCx as Backend<'ll>>::BasicBlock,

src/librustc_codegen_ssa/interfaces/mod.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
//! Interface of a Rust codegen backend
12+
//!
13+
//! This crate defines all the traits that have to be implemented by a codegen backend in order to
14+
//! use the backend-agnostic codegen code in `rustc_codegen_ssa`.
15+
//!
16+
//! The interface is designed around two backend-specific data structures, the codegen context and
17+
//! the builder. The codegen context is supposed to be read-only after its creation and during the
18+
//! actual codegen, while the builder stores the information about the function during codegen and
19+
//! is used to produce the instructions of the backend IR.
20+
//!
21+
//! Finaly, a third `Backend` structure has to implement methods related to how codegen information
22+
//! is passed to the backend, especially for asynchronous compilation.
23+
//!
24+
//! The traits contain associated types that are backend-specific, such as the backend's value or
25+
//! basic blocks.
26+
1127
use std::fmt;
1228
mod backend;
1329
mod misc;
@@ -21,7 +37,7 @@ mod debuginfo;
2137
mod abi;
2238
mod asm;
2339

24-
pub use self::backend::{Backend, BackendMethods};
40+
pub use self::backend::{Backend, ExtraBackendMethods};
2541
pub use self::misc::MiscMethods;
2642
pub use self::statics::StaticMethods;
2743
pub use self::declare::{DeclareMethods, PreDefineMethods};

src/librustc_codegen_ssa/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! # Note
12-
//!
13-
//! This API is completely unstable and subject to change.
14-
1511
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
1612
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
1713
html_root_url = "https://doc.rust-lang.org/nightly/")]
@@ -28,6 +24,10 @@
2824
#![allow(dead_code)]
2925
#![feature(quote)]
3026

27+
//! This crate contains codegen code that is used by all codegen backends (LLVM and others).
28+
//! The backend-agnostic functions of this crate use functions defined in various traits that
29+
//! have to be implemented by each backends.
30+
3131
#[macro_use] extern crate bitflags;
3232
#[macro_use] extern crate log;
3333
extern crate rustc_apfloat;

src/librustc_codegen_ssa/mir/block.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
102102
};
103103

104104
let funclet_br =
105-
|this: &mut Self, bx: &Bx, target: mir::BasicBlock| {
105+
|this: &mut Self, bx: &mut Bx, target: mir::BasicBlock| {
106106
let (lltarget, is_cleanupret) = lltarget(this, target);
107107
if is_cleanupret {
108108
// micro-optimization: generate a `ret` rather than a jump
@@ -115,7 +115,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
115115

116116
let do_call = |
117117
this: &mut Self,
118-
bx: &Bx,
118+
bx: &mut Bx,
119119
fn_ty: FnType<'tcx, Ty<'tcx>>,
120120
fn_ptr: Cx::Value,
121121
llargs: &[Cx::Value],
@@ -191,7 +191,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
191191
}
192192

193193
mir::TerminatorKind::Goto { target } => {
194-
funclet_br(self, &bx, target);
194+
funclet_br(self, &mut bx, target);
195195
}
196196

197197
mir::TerminatorKind::SwitchInt { ref discr, switch_ty, ref values, ref targets } => {
@@ -293,7 +293,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
293293

294294
if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def {
295295
// we don't actually need to drop anything.
296-
funclet_br(self, &bx, target);
296+
funclet_br(self, &mut bx, target);
297297
return
298298
}
299299

@@ -324,7 +324,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
324324
bx.cx().fn_type_of_instance(&drop_fn))
325325
}
326326
};
327-
do_call(self, &bx, fn_ty, drop_fn, args,
327+
do_call(self, &mut bx, fn_ty, drop_fn, args,
328328
Some((ReturnDest::Nothing, target)),
329329
unwind);
330330
}
@@ -348,7 +348,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
348348

349349
// Don't codegen the panic block if success if known.
350350
if const_cond == Some(expected) {
351-
funclet_br(self, &bx, target);
351+
funclet_br(self, &mut bx, target);
352352
return;
353353
}
354354

@@ -419,7 +419,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
419419
let llfn = bx.cx().get_fn(instance);
420420

421421
// Codegen the actual panic invoke/call.
422-
do_call(self, &bx, fn_ty, llfn, &args, None, cleanup);
422+
do_call(self, &mut bx, fn_ty, llfn, &args, None, cleanup);
423423
}
424424

425425
mir::TerminatorKind::DropAndReplace { .. } => {
@@ -469,7 +469,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
469469
if let Some(destination_ref) = destination.as_ref() {
470470
let &(ref dest, target) = destination_ref;
471471
self.codegen_transmute(&bx, &args[0], dest);
472-
funclet_br(self, &bx, target);
472+
funclet_br(self, &mut bx, target);
473473
} else {
474474
// If we are trying to transmute to an uninhabited type,
475475
// it is likely there is no allotted destination. In fact,
@@ -496,7 +496,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
496496
Some(ty::InstanceDef::DropGlue(_, None)) => {
497497
// empty drop glue - a nop.
498498
let &(_, target) = destination.as_ref().unwrap();
499-
funclet_br(self, &bx, target);
499+
funclet_br(self, &mut bx, target);
500500
return;
501501
}
502502
_ => bx.cx().new_fn_type(sig, &extra_args)
@@ -542,7 +542,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
542542
// Codegen the actual panic invoke/call.
543543
do_call(
544544
self,
545-
&bx,
545+
&mut bx,
546546
fn_ty,
547547
llfn,
548548
&[msg_file_line_col],
@@ -640,7 +640,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
640640
}
641641

642642
if let Some((_, target)) = *destination {
643-
funclet_br(self, &bx, target);
643+
funclet_br(self, &mut bx, target);
644644
} else {
645645
bx.unreachable();
646646
}
@@ -692,7 +692,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
692692
_ => span_bug!(span, "no llfn for call"),
693693
};
694694

695-
do_call(self, &bx, fn_ty, fn_ptr, &llargs,
695+
do_call(self, &mut bx, fn_ty, fn_ptr, &llargs,
696696
destination.as_ref().map(|&(_, target)| (ret_dest, target)),
697697
cleanup);
698698
}
@@ -864,7 +864,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
864864
span_bug!(self.mir.span, "landing pad was not inserted?")
865865
}
866866

867-
let bx : Bx = self.new_block("cleanup");
867+
let mut bx : Bx = self.new_block("cleanup");
868868

869869
let llpersonality = self.cx.eh_personality();
870870
let llretty = self.landing_pad_type();
@@ -903,7 +903,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
903903
&self,
904904
bb: mir::BasicBlock
905905
) -> Bx {
906-
let bx = Bx::with_cx(self.cx);
906+
let mut bx = Bx::with_cx(self.cx);
907907
bx.position_at_end(self.blocks[bb]);
908908
bx
909909
}

0 commit comments

Comments
 (0)