Skip to content

Commit a9ad16d

Browse files
committed
Added some docs + start to &mut self builder methods
1 parent a787b7f commit a9ad16d

File tree

11 files changed

+70
-53
lines changed

11 files changed

+70
-53
lines changed

src/librustc_codegen_llvm/builder.rs

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

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

791791
bx.set_personality_fn(bx.cx().eh_personality());
792792

793-
let normal = bx.build_sibling_block("normal");
793+
let mut normal = bx.build_sibling_block("normal");
794794
let catchswitch = bx.build_sibling_block("catchswitch");
795795
let catchpad = bx.build_sibling_block("catchpad");
796-
let caught = bx.build_sibling_block("caught");
796+
let mut caught = bx.build_sibling_block("caught");
797797

798798
let func = llvm::get_param(bx.llfn(), 0);
799799
let data = llvm::get_param(bx.llfn(), 1);
@@ -914,8 +914,8 @@ fn codegen_gnu_try(
914914
// expected to be `*mut *mut u8` for this to actually work, but that's
915915
// managed by the standard library.
916916

917-
let then = bx.build_sibling_block("then");
918-
let catch = bx.build_sibling_block("catch");
917+
let mut then = bx.build_sibling_block("then");
918+
let mut catch = bx.build_sibling_block("catch");
919919

920920
let func = llvm::get_param(bx.llfn(), 0);
921921
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 Metadata = ModuleLlvm;
134134
type OngoingCodegen = OngoingCodegen;
135135

src/librustc_codegen_ssa/base.rs

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

Lines changed: 2 additions & 1 deletion
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

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

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
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

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
@@ -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

@@ -323,7 +323,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
323323
bx.cx().fn_type_of_instance(&drop_fn))
324324
}
325325
};
326-
do_call(self, &bx, fn_ty, drop_fn, args,
326+
do_call(self, &mut bx, fn_ty, drop_fn, args,
327327
Some((ReturnDest::Nothing, target)),
328328
unwind);
329329
}
@@ -347,7 +347,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
347347

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

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

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

424424
mir::TerminatorKind::DropAndReplace { .. } => {
@@ -468,7 +468,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
468468
if let Some(destination_ref) = destination.as_ref() {
469469
let &(ref dest, target) = destination_ref;
470470
self.codegen_transmute(&bx, &args[0], dest);
471-
funclet_br(self, &bx, target);
471+
funclet_br(self, &mut bx, target);
472472
} else {
473473
// If we are trying to transmute to an uninhabited type,
474474
// it is likely there is no allotted destination. In fact,
@@ -495,7 +495,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
495495
Some(ty::InstanceDef::DropGlue(_, None)) => {
496496
// empty drop glue - a nop.
497497
let &(_, target) = destination.as_ref().unwrap();
498-
funclet_br(self, &bx, target);
498+
funclet_br(self, &mut bx, target);
499499
return;
500500
}
501501
_ => bx.cx().new_fn_type(sig, &extra_args)
@@ -541,7 +541,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
541541
// Codegen the actual panic invoke/call.
542542
do_call(
543543
self,
544-
&bx,
544+
&mut bx,
545545
fn_ty,
546546
llfn,
547547
&[msg_file_line_col],
@@ -639,7 +639,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
639639
}
640640

641641
if let Some((_, target)) = *destination {
642-
funclet_br(self, &bx, target);
642+
funclet_br(self, &mut bx, target);
643643
} else {
644644
bx.unreachable();
645645
}
@@ -731,7 +731,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
731731
_ => span_bug!(span, "no llfn for call"),
732732
};
733733

734-
do_call(self, &bx, fn_ty, fn_ptr, &llargs,
734+
do_call(self, &mut bx, fn_ty, fn_ptr, &llargs,
735735
destination.as_ref().map(|&(_, target)| (ret_dest, target)),
736736
cleanup);
737737
}
@@ -903,7 +903,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
903903
span_bug!(self.mir.span, "landing pad was not inserted?")
904904
}
905905

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

908908
let llpersonality = self.cx.eh_personality();
909909
let llretty = self.landing_pad_type();
@@ -942,7 +942,7 @@ impl<'a, 'f, 'll: 'a + 'f, 'tcx: 'll, Cx: CodegenMethods<'ll, 'tcx>>
942942
&self,
943943
bb: mir::BasicBlock
944944
) -> Bx {
945-
let bx = Bx::with_cx(self.cx);
945+
let mut bx = Bx::with_cx(self.cx);
946946
bx.position_at_end(self.blocks[bb]);
947947
bx
948948
}

0 commit comments

Comments
 (0)