Skip to content

Commit 6728ed8

Browse files
danielsntedinski
authored andcommitted
Refactor: Cleanly seperate CurrentFnCtx from GotocCtx. (rust-lang#195)
* Move `CurrentFnCtx` into its own file * Replace `GotocCtx` forwarding functions with a direct call to the `CurrentFnCtx` functions * Make all `CurrentFnCtx` fields private, with getters and setters.
1 parent c1e9ba5 commit 6728ed8

File tree

10 files changed

+182
-152
lines changed

10 files changed

+182
-152
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
use crate::gotoc::GotocCtx;
5+
use crate::gotoc::Stmt;
6+
use rustc_hir::def_id::DefId;
7+
use rustc_middle::mir::BasicBlock;
8+
use rustc_middle::mir::Body;
9+
use rustc_middle::ty::Instance;
10+
use rustc_middle::ty::PolyFnSig;
11+
use rustc_middle::ty::TyCtxt;
12+
13+
/// This structure represents useful data about the function we are currently compiling.
14+
pub struct CurrentFnCtx<'tcx> {
15+
/// The GOTO block we are compiling into
16+
block: Vec<Stmt>,
17+
/// The current MIR basic block
18+
current_bb: Option<BasicBlock>,
19+
/// The codegen instance for the current function
20+
instance: Instance<'tcx>,
21+
/// The goto labels for all blocks
22+
labels: Vec<String>,
23+
/// The mir for the current instance
24+
mir: &'tcx Body<'tcx>,
25+
/// The symbol name of the current function
26+
name: String,
27+
/// The signature of the current function
28+
sig: PolyFnSig<'tcx>,
29+
/// A counter to enable creating temporary variables
30+
temp_var_counter: u64,
31+
}
32+
33+
/// Constructor
34+
impl CurrentFnCtx<'tcx> {
35+
pub fn new(instance: Instance<'tcx>, gcx: &GotocCtx<'tcx>) -> Self {
36+
Self {
37+
block: vec![],
38+
current_bb: None,
39+
instance,
40+
labels: vec![],
41+
mir: gcx.tcx.instance_mir(instance.def),
42+
name: gcx.symbol_name(instance),
43+
sig: gcx.fn_sig_of_instance(instance),
44+
temp_var_counter: 0,
45+
}
46+
}
47+
}
48+
49+
/// Setters
50+
impl CurrentFnCtx<'tcx> {
51+
/// Returns the current block, replacing it with an empty vector.
52+
pub fn extract_block(&mut self) -> Vec<Stmt> {
53+
std::mem::replace(&mut self.block, vec![])
54+
}
55+
56+
pub fn get_and_incr_counter(&mut self) -> u64 {
57+
let rval = self.temp_var_counter;
58+
self.temp_var_counter += 1;
59+
rval
60+
}
61+
62+
pub fn push_onto_block(&mut self, s: Stmt) {
63+
self.block.push(s)
64+
}
65+
66+
pub fn reset_current_bb(&mut self) {
67+
self.current_bb = None;
68+
}
69+
70+
pub fn set_current_bb(&mut self, bb: BasicBlock) {
71+
self.current_bb = Some(bb);
72+
}
73+
74+
pub fn set_labels(&mut self, labels: Vec<String>) {
75+
assert!(self.labels.is_empty());
76+
self.labels = labels;
77+
}
78+
}
79+
80+
/// Getters
81+
impl CurrentFnCtx<'tcx> {
82+
/// The basic block we are currently compiling
83+
pub fn current_bb(&self) -> BasicBlock {
84+
self.current_bb.unwrap()
85+
}
86+
87+
/// The function we are currently compiling
88+
pub fn instance(&self) -> Instance<'tcx> {
89+
self.instance
90+
}
91+
92+
/// The labels in the function we are currently compiling
93+
pub fn labels(&self) -> &Vec<String> {
94+
&self.labels
95+
}
96+
97+
/// The MIR for the function we are currently compiling
98+
pub fn mir(&self) -> &'tcx Body<'tcx> {
99+
self.mir
100+
}
101+
102+
/// The name of the function we are currently compiling
103+
pub fn name(&self) -> String {
104+
self.name.clone()
105+
}
106+
107+
/// The signature of the function we are currently compiling
108+
pub fn sig(&self) -> PolyFnSig<'tcx> {
109+
self.sig
110+
}
111+
}
112+
113+
/// Utility functions
114+
impl CurrentFnCtx<'_> {
115+
pub fn find_label(&self, bb: &BasicBlock) -> String {
116+
self.labels[bb.index()].clone()
117+
}
118+
}

compiler/rustc_codegen_llvm/src/gotoc/hooks.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ impl<'tcx> GotocHook<'tcx> for Assume {
104104
let loc = tcx.codegen_span_option2(span);
105105

106106
Stmt::block(
107-
vec![Stmt::assume(cond, loc.clone()), Stmt::goto(tcx.find_label(&target), loc.clone())],
107+
vec![
108+
Stmt::assume(cond, loc.clone()),
109+
Stmt::goto(tcx.current_fn().find_label(&target), loc.clone()),
110+
],
108111
loc,
109112
)
110113
}
@@ -132,7 +135,7 @@ impl<'tcx> GotocHook<'tcx> for Nondet {
132135
let target = target.unwrap();
133136
let pt = tcx.place_ty(&p);
134137
if pt.is_unit() {
135-
Stmt::goto(tcx.find_label(&target), loc)
138+
Stmt::goto(tcx.current_fn().find_label(&target), loc)
136139
} else {
137140
let pe = tcx.codegen_place(&p).goto_expr;
138141
Stmt::block(
@@ -143,7 +146,7 @@ impl<'tcx> GotocHook<'tcx> for Nondet {
143146
None => Stmt::skip(loc.clone()),
144147
Some(f) => Stmt::assume(f.call(vec![pe.address_of()]), loc.clone()),
145148
},
146-
Stmt::goto(tcx.find_label(&target), loc.clone()),
149+
Stmt::goto(tcx.current_fn().find_label(&target), loc.clone()),
147150
],
148151
loc,
149152
)
@@ -229,7 +232,7 @@ impl<'tcx> GotocHook<'tcx> for Intrinsic {
229232
Stmt::block(
230233
vec![
231234
tcx.codegen_intrinsic(instance, fargs, &p, span),
232-
Stmt::goto(tcx.find_label(&target), loc.clone()),
235+
Stmt::goto(tcx.current_fn().find_label(&target), loc.clone()),
233236
],
234237
loc,
235238
)
@@ -263,7 +266,7 @@ impl<'tcx> GotocHook<'tcx> for MemReplace {
263266
let place_layout = tcx.layout_of(place_type);
264267
let place_is_zst = place_layout.is_zst();
265268
if place_is_zst {
266-
Stmt::block(vec![Stmt::goto(tcx.find_label(&target), loc.clone())], loc)
269+
Stmt::block(vec![Stmt::goto(tcx.current_fn().find_label(&target), loc.clone())], loc)
267270
} else {
268271
let dest = fargs.remove(0);
269272
let src = fargs.remove(0);
@@ -273,7 +276,7 @@ impl<'tcx> GotocHook<'tcx> for MemReplace {
273276
.goto_expr
274277
.assign(dest.clone().dereference().with_location(loc.clone()), loc.clone()),
275278
dest.dereference().assign(src, loc.clone()),
276-
Stmt::goto(tcx.find_label(&target), loc.clone()),
279+
Stmt::goto(tcx.current_fn().find_label(&target), loc.clone()),
277280
],
278281
loc,
279282
)
@@ -336,7 +339,7 @@ impl<'tcx> GotocHook<'tcx> for MemSwap {
336339
Stmt::block(
337340
vec![
338341
tcx.find_function(&func_name).unwrap().call(vec![x, y]).as_stmt(loc.clone()),
339-
Stmt::goto(tcx.find_label(&target), loc.clone()),
342+
Stmt::goto(tcx.current_fn().find_label(&target), loc.clone()),
340343
],
341344
loc,
342345
)
@@ -374,7 +377,7 @@ impl<'tcx> GotocHook<'tcx> for PtrRead {
374377
tcx.codegen_place(&p)
375378
.goto_expr
376379
.assign(src.dereference().with_location(loc.clone()), loc.clone()),
377-
Stmt::goto(tcx.find_label(&target), loc.clone()),
380+
Stmt::goto(tcx.current_fn().find_label(&target), loc.clone()),
378381
],
379382
loc,
380383
)
@@ -410,7 +413,7 @@ impl<'tcx> GotocHook<'tcx> for PtrWrite {
410413
Stmt::block(
411414
vec![
412415
dst.dereference().assign(src, loc.clone()).with_location(loc.clone()),
413-
Stmt::goto(tcx.find_label(&target), loc.clone()),
416+
Stmt::goto(tcx.current_fn().find_label(&target), loc.clone()),
414417
],
415418
loc,
416419
)
@@ -446,7 +449,7 @@ impl<'tcx> GotocHook<'tcx> for RustAlloc {
446449
.cast_to(Type::unsigned_int(8).to_pointer()),
447450
loc,
448451
),
449-
Stmt::goto(tcx.find_label(&target), Location::none()),
452+
Stmt::goto(tcx.current_fn().find_label(&target), Location::none()),
450453
],
451454
Location::none(),
452455
)
@@ -482,7 +485,7 @@ impl<'tcx> GotocHook<'tcx> for RustDealloc {
482485
BuiltinFn::Free
483486
.call(vec![ptr.cast_to(Type::void_pointer())], loc.clone())
484487
.as_stmt(loc.clone()),
485-
Stmt::goto(tcx.find_label(&target), Location::none()),
488+
Stmt::goto(tcx.current_fn().find_label(&target), Location::none()),
486489
],
487490
loc,
488491
)
@@ -524,7 +527,7 @@ impl<'tcx> GotocHook<'tcx> for RustRealloc {
524527
.cast_to(Type::unsigned_int(8).to_pointer()),
525528
loc.clone(),
526529
),
527-
Stmt::goto(tcx.find_label(&target), loc.clone()),
530+
Stmt::goto(tcx.current_fn().find_label(&target), loc.clone()),
528531
],
529532
loc,
530533
)
@@ -560,7 +563,7 @@ impl<'tcx> GotocHook<'tcx> for RustAllocZeroed {
560563
.cast_to(Type::unsigned_int(8).to_pointer()),
561564
loc.clone(),
562565
),
563-
Stmt::goto(tcx.find_label(&target), loc.clone()),
566+
Stmt::goto(tcx.current_fn().find_label(&target), loc.clone()),
564567
],
565568
loc,
566569
)
@@ -601,7 +604,7 @@ impl<'tcx> GotocHook<'tcx> for SliceFromRawPart {
601604
loc.clone(),
602605
)
603606
.with_location(loc.clone());
604-
Stmt::block(vec![code, Stmt::goto(tcx.find_label(&target), loc.clone())], loc)
607+
Stmt::block(vec![code, Stmt::goto(tcx.current_fn().find_label(&target), loc.clone())], loc)
605608
}
606609
}
607610

0 commit comments

Comments
 (0)