Skip to content

Commit e81cc30

Browse files
committed
initial commit of smir-pretty
0 parents  commit e81cc30

10 files changed

+533
-0
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/

Diff for: Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "smir-pretty"
3+
version = "0.1.0"
4+
edition = "2021"
5+
# rust-version = "1.78.0" # I think we get latest available rust by unsetting this and setting rust-toolchain.toml.toolchain.channel = nightly
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
# serde = { version = "1.0.104", features = ["derive"] }
11+
# serde_cbor = "0.11"
12+
# serde_json = "1.0"
13+
# tar = "0.4"
14+
15+
[package.metadata.rust-analyzer]
16+
# This package uses rustc crates.
17+
rustc_private=true

Diff for: rust-toolchain.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[toolchain]
2+
channel = "nightly"

Diff for: src/driver.rs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//! This module provides a compiler driver such that:
2+
//!
3+
//! 1. the rustc compiler context is available
4+
//! 2. the rustc `stable_mir` APIs are available
5+
//!
6+
//! It exports a single function:
7+
//!
8+
//! ```rust
9+
//! stable_mir_driver(args: &Vec<String>, callback_fn: fn (TyCtxt) -> () )
10+
//! ```
11+
//!
12+
//! Calling this function is essentially equivalent to the following macro call:
13+
//!
14+
//! ```rust
15+
//! rustc_internal::run_with_tcx!( args, callback_fn );
16+
//! ```
17+
//!
18+
//! However, we prefer a non-macro version for clarity and build simplicity.
19+
20+
extern crate rustc_middle;
21+
extern crate rustc_driver;
22+
extern crate rustc_interface;
23+
extern crate rustc_smir;
24+
use rustc_middle::ty::TyCtxt;
25+
use rustc_driver::Compilation;
26+
use rustc_interface::{interface::Compiler, Queries};
27+
use rustc_smir::rustc_internal;
28+
29+
struct StableMirCallbacks {
30+
callback_fn: fn (TyCtxt) -> (),
31+
}
32+
33+
impl rustc_driver::Callbacks for StableMirCallbacks {
34+
fn after_analysis<'tcx>(
35+
&mut self,
36+
_compiler: &Compiler,
37+
queries: &'tcx Queries<'tcx>,
38+
) -> Compilation {
39+
40+
let _q = queries
41+
.global_ctxt()
42+
.unwrap()
43+
.get_mut()
44+
.enter(|tcx| {
45+
let _ = rustc_internal::run(tcx, || (self.callback_fn)(tcx));
46+
});
47+
Compilation::Stop
48+
}
49+
}
50+
51+
pub fn stable_mir_driver(args_outer: &Vec<String>, callback_fn: fn (TyCtxt) -> ()) {
52+
let mut callbacks = StableMirCallbacks { callback_fn };
53+
rustc_driver::RunCompiler::new(args_outer, &mut callbacks).run().unwrap()
54+
}

Diff for: src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![feature(rustc_private)]
2+
pub mod driver;
3+
pub mod printer;
4+
pub mod pretty;
5+
pub use driver::stable_mir_driver;
6+
pub use printer::*;
7+
pub use pretty::*;

Diff for: src/main.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(rustc_private)]
2+
use std::env;
3+
pub mod driver;
4+
pub mod printer;
5+
use driver::stable_mir_driver;
6+
use printer::print_all_items;
7+
8+
fn main() {
9+
let args: Vec<_> = env::args().collect();
10+
stable_mir_driver(&args, print_all_items)
11+
}

0 commit comments

Comments
 (0)