Skip to content

Commit 1ebdfbb

Browse files
denismerigouxeddyb
authored andcommitted
Added some docs + start to &mut self builder methods
1 parent 015e444 commit 1ebdfbb

File tree

12 files changed

+82
-60
lines changed

12 files changed

+82
-60
lines changed

src/librustc_codegen_llvm/builder.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
9595
llfn: &'ll Value,
9696
name: &'b str
9797
) -> Self {
98-
let bx = Builder::with_cx(cx);
98+
let mut bx = Builder::with_cx(cx);
9999
let llbb = unsafe {
100100
let name = SmallCStr::new(name);
101101
llvm::LLVMAppendBasicBlockInContext(
@@ -148,48 +148,48 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
148148
}
149149
}
150150

151-
fn set_value_name(&self, value: &'ll Value, name: &str) {
151+
fn set_value_name(&mut self, value: &'ll Value, name: &str) {
152152
let cname = SmallCStr::new(name);
153153
unsafe {
154154
llvm::LLVMSetValueName(value, cname.as_ptr());
155155
}
156156
}
157157

158-
fn position_at_end(&self, llbb: &'ll BasicBlock) {
158+
fn position_at_end(&mut self, llbb: &'ll BasicBlock) {
159159
unsafe {
160160
llvm::LLVMPositionBuilderAtEnd(self.llbuilder, llbb);
161161
}
162162
}
163163

164-
fn position_at_start(&self, llbb: &'ll BasicBlock) {
164+
fn position_at_start(&mut self, llbb: &'ll BasicBlock) {
165165
unsafe {
166166
llvm::LLVMRustPositionBuilderAtStart(self.llbuilder, llbb);
167167
}
168168
}
169169

170-
fn ret_void(&self) {
170+
fn ret_void(&mut self) {
171171
self.count_insn("retvoid");
172172
unsafe {
173173
llvm::LLVMBuildRetVoid(self.llbuilder);
174174
}
175175
}
176176

177-
fn ret(&self, v: &'ll Value) {
177+
fn ret(&mut self, v: &'ll Value) {
178178
self.count_insn("ret");
179179
unsafe {
180180
llvm::LLVMBuildRet(self.llbuilder, v);
181181
}
182182
}
183183

184-
fn br(&self, dest: &'ll BasicBlock) {
184+
fn br(&mut self, dest: &'ll BasicBlock) {
185185
self.count_insn("br");
186186
unsafe {
187187
llvm::LLVMBuildBr(self.llbuilder, dest);
188188
}
189189
}
190190

191191
fn cond_br(
192-
&self,
192+
&mut self,
193193
cond: &'ll Value,
194194
then_llbb: &'ll BasicBlock,
195195
else_llbb: &'ll BasicBlock,
@@ -457,7 +457,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
457457
}
458458

459459
fn alloca(&self, ty: &'ll Type, name: &str, align: Align) -> &'ll Value {
460-
let bx = Builder::with_cx(self.cx);
460+
let mut bx = Builder::with_cx(self.cx);
461461
bx.position_at_start(unsafe {
462462
llvm::LLVMGetFirstBasicBlock(self.llfn())
463463
});

src/librustc_codegen_llvm/intrinsic.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -832,10 +832,10 @@ fn codegen_msvc_try(
832832

833833
bx.set_personality_fn(bx.cx().eh_personality());
834834

835-
let normal = bx.build_sibling_block("normal");
835+
let mut normal = bx.build_sibling_block("normal");
836836
let catchswitch = bx.build_sibling_block("catchswitch");
837837
let catchpad = bx.build_sibling_block("catchpad");
838-
let caught = bx.build_sibling_block("caught");
838+
let mut caught = bx.build_sibling_block("caught");
839839

840840
let func = llvm::get_param(bx.llfn(), 0);
841841
let data = llvm::get_param(bx.llfn(), 1);
@@ -956,8 +956,8 @@ fn codegen_gnu_try(
956956
// expected to be `*mut *mut u8` for this to actually work, but that's
957957
// managed by the standard library.
958958

959-
let then = bx.build_sibling_block("then");
960-
let catch = bx.build_sibling_block("catch");
959+
let mut then = bx.build_sibling_block("then");
960+
let mut catch = bx.build_sibling_block("catch");
961961

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

src/librustc_codegen_llvm/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ mod value;
129129

130130
pub struct LlvmCodegenBackend(());
131131

132-
impl BackendMethods for LlvmCodegenBackend {
132+
impl ExtraBackendMethods for LlvmCodegenBackend {
133133
type Module = ModuleLlvm;
134134
type OngoingCodegen = OngoingCodegen;
135135

src/librustc_codegen_ssa/base.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
524524
cx.set_frame_pointer_elimination(llfn);
525525
cx.apply_target_cpu_attr(llfn);
526526

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

529529
bx.insert_reference_to_gdb_debug_scripts_section_global();
530530

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

562562

563-
pub fn codegen_crate<B: BackendMethods>(
563+
pub fn codegen_crate<B: ExtraBackendMethods>(
564564
backend: B,
565565
tcx: TyCtxt<'a, 'tcx, 'tcx>,
566566
rx: mpsc::Receiver<Box<dyn Any + Send>>
@@ -785,29 +785,29 @@ pub fn codegen_crate<B: BackendMethods>(
785785
/// If you see this comment in the code, then it means that this workaround
786786
/// worked! We may yet one day track down the mysterious cause of that
787787
/// segfault...
788-
struct AbortCodegenOnDrop<B: BackendMethods>(Option<B::OngoingCodegen>);
788+
struct AbortCodegenOnDrop<B: ExtraBackendMethods>(Option<B::OngoingCodegen>);
789789

790-
impl<B: BackendMethods> AbortCodegenOnDrop<B> {
790+
impl<B: ExtraBackendMethods> AbortCodegenOnDrop<B> {
791791
fn into_inner(mut self) -> B::OngoingCodegen {
792792
self.0.take().unwrap()
793793
}
794794
}
795795

796-
impl<B: BackendMethods> Deref for AbortCodegenOnDrop<B> {
796+
impl<B: ExtraBackendMethods> Deref for AbortCodegenOnDrop<B> {
797797
type Target = B::OngoingCodegen;
798798

799799
fn deref(&self) -> &B::OngoingCodegen {
800800
self.0.as_ref().unwrap()
801801
}
802802
}
803803

804-
impl<B: BackendMethods> DerefMut for AbortCodegenOnDrop<B> {
804+
impl<B: ExtraBackendMethods> DerefMut for AbortCodegenOnDrop<B> {
805805
fn deref_mut(&mut self) -> &mut B::OngoingCodegen {
806806
self.0.as_mut().unwrap()
807807
}
808808
}
809809

810-
impl<B: BackendMethods> Drop for AbortCodegenOnDrop<B> {
810+
impl<B: ExtraBackendMethods> Drop for AbortCodegenOnDrop<B> {
811811
fn drop(&mut self) {
812812
if let Some(codegen) = self.0.take() {
813813
B::codegen_aborted(codegen);

src/librustc_codegen_ssa/interfaces/backend.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc::mir::mono::Stats;
1818
use rustc::session::Session;
1919
use rustc::ty::TyCtxt;
2020
use rustc::util::time_graph::TimeGraph;
21+
use rustc_codegen_utils::codegen_backend::CodegenBackend;
2122
use std::any::Any;
2223
use std::sync::mpsc::Receiver;
2324
use syntax_pos::symbol::InternedString;
@@ -42,7 +43,7 @@ impl<'tcx, T> Backend<'tcx> for T where
4243
Self: BackendTypes + HasTyCtxt<'tcx> + LayoutOf<Ty = Ty<'tcx>, TyLayout = TyLayout<'tcx>>
4344
{}
4445

45-
pub trait BackendMethods {
46+
pub trait ExtraBackendMethods: CodegenBackend {
4647
type Module;
4748
type OngoingCodegen;
4849

src/librustc_codegen_ssa/interfaces/builder.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,18 @@ pub trait BuilderMethods<'a, 'tcx: 'a>:
4141
fn llbb(&self) -> Self::BasicBlock;
4242
fn count_insn(&self, category: &str);
4343

44-
fn set_value_name(&self, value: Self::Value, name: &str);
45-
fn position_at_end(&self, llbb: Self::BasicBlock);
46-
fn position_at_start(&self, llbb: Self::BasicBlock);
47-
fn ret_void(&self);
48-
fn ret(&self, v: Self::Value);
49-
fn br(&self, dest: Self::BasicBlock);
50-
fn cond_br(&self, cond: Self::Value, then_llbb: Self::BasicBlock, else_llbb: Self::BasicBlock);
44+
fn set_value_name(&mut self, value: Self::Value, name: &str);
45+
fn position_at_end(&mut self, llbb: Self::BasicBlock);
46+
fn position_at_start(&mut self, llbb: Self::BasicBlock);
47+
fn ret_void(&mut self);
48+
fn ret(&mut self, v: Self::Value);
49+
fn br(&mut self, dest: Self::BasicBlock);
50+
fn cond_br(
51+
&mut self,
52+
cond: Self::Value,
53+
then_llbb: Self::BasicBlock,
54+
else_llbb: Self::BasicBlock,
55+
);
5156
fn switch(&self, v: Self::Value, else_llbb: Self::BasicBlock, num_cases: usize) -> Self::Value;
5257
fn invoke(
5358
&self,

src/librustc_codegen_ssa/interfaces/mod.rs

Lines changed: 17 additions & 1 deletion
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
mod abi;
1228
mod asm;
1329
mod backend;
@@ -22,7 +38,7 @@ mod type_;
2238

2339
pub use self::abi::{AbiBuilderMethods, AbiMethods};
2440
pub use self::asm::{AsmBuilderMethods, AsmMethods};
25-
pub use self::backend::{Backend, BackendMethods, BackendTypes};
41+
pub use self::backend::{Backend, BackendTypes, ExtraBackendMethods};
2642
pub use self::builder::BuilderMethods;
2743
pub use self::consts::ConstMethods;
2844
pub use self::debuginfo::{DebugInfoBuilderMethods, DebugInfoMethods};

src/librustc_codegen_ssa/interfaces/type_.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc::ty::layout::TyLayout;
1616
use rustc::ty::layout::{self, Align, Size};
1717
use rustc::ty::Ty;
1818
use rustc::util::nodemap::FxHashMap;
19-
use rustc_target::abi::call::{ArgType, CastTarget, FnType, Reg};
19+
use rustc_target::abi::call::{ArgType, CastTarget, FnType, Reg};
2020
use std::cell::RefCell;
2121
use syntax::ast;
2222

src/librustc_codegen_ssa/lib.rs

Lines changed: 4 additions & 4 deletions
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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
111111
};
112112

113113
let funclet_br =
114-
|this: &mut Self, bx: &Bx, target: mir::BasicBlock| {
114+
|this: &mut Self, bx: &mut Bx, target: mir::BasicBlock| {
115115
let (lltarget, is_cleanupret) = lltarget(this, target);
116116
if is_cleanupret {
117117
// micro-optimization: generate a `ret` rather than a jump
@@ -124,7 +124,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
124124

125125
let do_call = |
126126
this: &mut Self,
127-
bx: &Bx,
127+
bx: &mut Bx,
128128
fn_ty: FnType<'tcx, Ty<'tcx>>,
129129
fn_ptr: Bx::Value,
130130
llargs: &[Bx::Value],
@@ -200,7 +200,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
200200
}
201201

202202
mir::TerminatorKind::Goto { target } => {
203-
funclet_br(self, &bx, target);
203+
funclet_br(self, &mut bx, target);
204204
}
205205

206206
mir::TerminatorKind::SwitchInt { ref discr, switch_ty, ref values, ref targets } => {
@@ -302,7 +302,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
302302

303303
if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def {
304304
// we don't actually need to drop anything.
305-
funclet_br(self, &bx, target);
305+
funclet_br(self, &mut bx, target);
306306
return
307307
}
308308

@@ -332,7 +332,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
332332
bx.cx().fn_type_of_instance(&drop_fn))
333333
}
334334
};
335-
do_call(self, &bx, fn_ty, drop_fn, args,
335+
do_call(self, &mut bx, fn_ty, drop_fn, args,
336336
Some((ReturnDest::Nothing, target)),
337337
unwind);
338338
}
@@ -356,7 +356,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
356356

357357
// Don't codegen the panic block if success if known.
358358
if const_cond == Some(expected) {
359-
funclet_br(self, &bx, target);
359+
funclet_br(self, &mut bx, target);
360360
return;
361361
}
362362

@@ -427,7 +427,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
427427
let llfn = bx.cx().get_fn(instance);
428428

429429
// Codegen the actual panic invoke/call.
430-
do_call(self, &bx, fn_ty, llfn, &args, None, cleanup);
430+
do_call(self, &mut bx, fn_ty, llfn, &args, None, cleanup);
431431
}
432432

433433
mir::TerminatorKind::DropAndReplace { .. } => {
@@ -477,7 +477,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
477477
if let Some(destination_ref) = destination.as_ref() {
478478
let &(ref dest, target) = destination_ref;
479479
self.codegen_transmute(&bx, &args[0], dest);
480-
funclet_br(self, &bx, target);
480+
funclet_br(self, &mut bx, target);
481481
} else {
482482
// If we are trying to transmute to an uninhabited type,
483483
// it is likely there is no allotted destination. In fact,
@@ -504,7 +504,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
504504
Some(ty::InstanceDef::DropGlue(_, None)) => {
505505
// empty drop glue - a nop.
506506
let &(_, target) = destination.as_ref().unwrap();
507-
funclet_br(self, &bx, target);
507+
funclet_br(self, &mut bx, target);
508508
return;
509509
}
510510
_ => bx.cx().new_fn_type(sig, &extra_args)
@@ -550,7 +550,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
550550
// Codegen the actual panic invoke/call.
551551
do_call(
552552
self,
553-
&bx,
553+
&mut bx,
554554
fn_ty,
555555
llfn,
556556
&[msg_file_line_col],
@@ -648,7 +648,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
648648
}
649649

650650
if let Some((_, target)) = *destination {
651-
funclet_br(self, &bx, target);
651+
funclet_br(self, &mut bx, target);
652652
} else {
653653
bx.unreachable();
654654
}
@@ -740,7 +740,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
740740
_ => span_bug!(span, "no llfn for call"),
741741
};
742742

743-
do_call(self, &bx, fn_ty, fn_ptr, &llargs,
743+
do_call(self, &mut bx, fn_ty, fn_ptr, &llargs,
744744
destination.as_ref().map(|&(_, target)| (ret_dest, target)),
745745
cleanup);
746746
}
@@ -913,7 +913,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
913913
span_bug!(self.mir.span, "landing pad was not inserted?")
914914
}
915915

916-
let bx = self.new_block("cleanup");
916+
let mut bx = self.new_block("cleanup");
917917

918918
let llpersonality = self.cx.eh_personality();
919919
let llretty = self.landing_pad_type();
@@ -952,7 +952,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
952952
&self,
953953
bb: mir::BasicBlock
954954
) -> Bx {
955-
let bx = Bx::with_cx(self.cx);
955+
let mut bx = Bx::with_cx(self.cx);
956956
bx.position_at_end(self.blocks[bb]);
957957
bx
958958
}

0 commit comments

Comments
 (0)