Skip to content

Commit 8ae2a01

Browse files
committed
moved the gotoc functionality out of mod.rs
1 parent 258f52c commit 8ae2a01

File tree

4 files changed

+54
-49
lines changed

4 files changed

+54
-49
lines changed

compiler/rustc_codegen_llvm/src/gotoc/backend.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

compiler/rustc_codegen_llvm/src/gotoc/debug.rs

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33

44
//! This file contains functionality that makes RMC easier to debug
55
6+
use super::metadata::*;
7+
use rustc_middle::mir::Body;
8+
use rustc_middle::ty::print::with_no_trimmed_paths;
9+
use rustc_middle::ty::Instance;
10+
use std::cell::RefCell;
11+
use std::lazy::SyncLazy;
12+
use std::panic;
13+
use tracing::debug;
14+
615
// Use a thread-local global variable to track the current codegen item for debugging.
716
// If RMC panics during codegen, we can grab this item to include the problematic
817
// codegen item in the panic trace.
@@ -12,43 +21,43 @@ thread_local!(static CURRENT_CODEGEN_ITEM: RefCell<Option<String>> = RefCell::ne
1221
const BUG_REPORT_URL: &str =
1322
"https://github.com/model-checking/rmc/issues/new?labels=bug&template=bug_report.md";
1423

15-
// Custom panic hook.
16-
static DEFAULT_HOOK: SyncLazy<Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static>> =
17-
SyncLazy::new(|| {
18-
let hook = panic::take_hook();
19-
panic::set_hook(Box::new(|info| {
20-
// Invoke the default handler, which prints the actual panic message and
21-
// optionally a backtrace. This also prints Rustc's "file a bug here" message:
22-
// it seems like the only way to remove that is to use rustc_driver::report_ice;
23-
// however, adding that dependency to this crate causes a circular dependency.
24-
// For now, just print our message after the Rust one and explicitly point to
25-
// our bug template form.
26-
(*DEFAULT_HOOK)(info);
24+
// Custom panic hook.
25+
pub static DEFAULT_HOOK: SyncLazy<Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static>> =
26+
SyncLazy::new(|| {
27+
let hook = panic::take_hook();
28+
panic::set_hook(Box::new(|info| {
29+
// Invoke the default handler, which prints the actual panic message and
30+
// optionally a backtrace. This also prints Rustc's "file a bug here" message:
31+
// it seems like the only way to remove that is to use rustc_driver::report_ice;
32+
// however, adding that dependency to this crate causes a circular dependency.
33+
// For now, just print our message after the Rust one and explicitly point to
34+
// our bug template form.
35+
(*DEFAULT_HOOK)(info);
2736

28-
// Separate the output with an empty line
29-
eprintln!();
37+
// Separate the output with an empty line
38+
eprintln!();
3039

31-
// Print the current function if available
32-
CURRENT_CODEGEN_ITEM.with(|cell| {
33-
if let Some(current_item) = cell.borrow().clone() {
34-
eprintln!("[RMC] current codegen item: {}", current_item);
35-
} else {
36-
eprintln!("[RMC] no current codegen item.");
37-
}
38-
});
40+
// Print the current function if available
41+
CURRENT_CODEGEN_ITEM.with(|cell| {
42+
if let Some(current_item) = cell.borrow().clone() {
43+
eprintln!("[RMC] current codegen item: {}", current_item);
44+
} else {
45+
eprintln!("[RMC] no current codegen item.");
46+
}
47+
});
3948

40-
// Separate the output with an empty line
41-
eprintln!();
49+
// Separate the output with an empty line
50+
eprintln!();
4251

43-
// Print the RMC message
44-
eprintln!("RMC unexpectedly panicked during code generation.\n");
45-
eprintln!(
46-
"If you are seeing this message, please file an issue here instead of on the Rust compiler: {}",
47-
BUG_REPORT_URL
48-
);
49-
}));
50-
hook
51-
});
52+
// Print the RMC message
53+
eprintln!("RMC unexpectedly panicked during code generation.\n");
54+
eprintln!(
55+
"If you are seeing this message, please file an issue here instead of on the Rust compiler: {}",
56+
BUG_REPORT_URL
57+
);
58+
}));
59+
hook
60+
});
5261

5362
impl<'tcx> GotocCtx<'tcx> {
5463
// Calls the closure while updating the tracked global variable marking the
@@ -65,7 +74,7 @@ impl<'tcx> GotocCtx<'tcx> {
6574
});
6675
}
6776

68-
fn print_instance(&self, instance: Instance<'_>, mir: &'tcx Body<'tcx>) {
77+
pub fn print_instance(&self, instance: Instance<'_>, mir: &'tcx Body<'tcx>) {
6978
if cfg!(debug_assertions) {
7079
debug!(
7180
"handling {}, {}",
@@ -85,5 +94,4 @@ impl<'tcx> GotocCtx<'tcx> {
8594
}
8695
}
8796
}
88-
8997
}

compiler/rustc_codegen_llvm/src/gotoc/mod.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,29 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33
use bitflags::_core::any::Any;
44
use cbmc::goto_program::symtab_transformer;
5-
use cbmc::goto_program::{Stmt, Symbol, SymbolTable};
5+
use cbmc::goto_program::{Stmt, SymbolTable};
66
use cbmc::{MachineModel, RoundingMode};
77
use metadata::*;
88
use rustc_codegen_ssa::traits::CodegenBackend;
99
use rustc_data_structures::fx::FxHashMap;
1010
use rustc_errors::ErrorReported;
11-
use rustc_hir::def_id::DefId;
1211
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
1312
use rustc_middle::middle::cstore::{EncodedMetadata, MetadataLoaderDyn};
1413
use rustc_middle::mir::mono::{CodegenUnit, MonoItem};
15-
use rustc_middle::mir::Body;
16-
use rustc_middle::ty::print::with_no_trimmed_paths;
1714
use rustc_middle::ty::query::Providers;
18-
use rustc_middle::ty::{self, Instance, TyCtxt};
15+
use rustc_middle::ty::{self, TyCtxt};
1916
use rustc_serialize::json::ToJson;
2017
use rustc_session::config::{OutputFilenames, OutputType};
2118
use rustc_session::Session;
2219
use rustc_target::abi::Endian;
23-
use std::cell::RefCell;
2420
use std::lazy::SyncLazy;
25-
use std::panic;
2621
use tracing::{debug, warn};
2722

2823
mod assumptions;
29-
mod backend;
3024
mod block;
3125
pub mod cbmc;
3226
mod current_fn;
27+
mod debug;
3328
mod function;
3429
mod hooks;
3530
mod intrinsic;
@@ -50,8 +45,6 @@ pub struct GotocCodegenResult {
5045
pub crate_name: rustc_span::Symbol,
5146
}
5247

53-
54-
5548
#[derive(Clone)]
5649
pub struct GotocCodegenBackend();
5750

@@ -73,7 +66,6 @@ impl<'tcx> GotocCtx<'tcx> {
7366
_ => false,
7467
}
7568
}
76-
7769
}
7870

7971
impl GotocCodegenBackend {
@@ -155,7 +147,7 @@ impl CodegenBackend for GotocCodegenBackend {
155147
use rustc_hir::def_id::LOCAL_CRATE;
156148

157149
// Install panic hook
158-
SyncLazy::force(&DEFAULT_HOOK); // Install ice hook
150+
SyncLazy::force(&debug::DEFAULT_HOOK); // Install ice hook
159151

160152
let codegen_units: &'tcx [CodegenUnit<'_>] = tcx.collect_and_partition_mono_items(()).1;
161153
let mm = machine_model_from_session(&tcx.sess);

compiler/rustc_codegen_llvm/src/gotoc/static_var.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
//! This file contains functions related to codegenning MIR static variables into gotoc
55
6+
use super::cbmc::goto_program::Symbol;
7+
use super::metadata::*;
8+
use rustc_hir::def_id::DefId;
9+
use rustc_middle::mir::mono::MonoItem;
10+
use tracing::debug;
11+
612
impl<'tcx> GotocCtx<'tcx> {
713
pub fn codegen_static(&mut self, def_id: DefId, item: MonoItem<'tcx>) {
814
debug!("codegen_static");
@@ -11,7 +17,7 @@ impl<'tcx> GotocCtx<'tcx> {
1117
self.codegen_allocation(alloc, |_| symbol_name.clone(), Some(symbol_name.clone()));
1218
}
1319

14-
fn declare_static(&mut self, def_id: DefId, item: MonoItem<'tcx>) {
20+
pub fn declare_static(&mut self, def_id: DefId, item: MonoItem<'tcx>) {
1521
debug!("declare_static {:?}", def_id);
1622
let symbol_name = item.symbol_name(self.tcx).to_string();
1723
let typ = self.codegen_ty(self.tcx.type_of(def_id));

0 commit comments

Comments
 (0)