Skip to content

Commit 3a23d5b

Browse files
celinvaloli-obk
andauthored
Move the book to book/ folder and add more content (#84)
Started a "Getting Started" guide, and added a few tips. Related to #76 Co-authored-by: Oli Scherer <[email protected]>
1 parent ea28c92 commit 3a23d5b

File tree

16 files changed

+223
-43
lines changed

16 files changed

+223
-43
lines changed

.github/workflows/deploy_mdbook.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ jobs:
1919
run: cargo install mdbook --version "^0.4" --locked
2020

2121
- name: Run mdbook
22-
run: mdbook build
22+
run: mdbook build book
2323

2424
- name: Upload book
2525
uses: actions/upload-pages-artifact@v2
2626
with:
27-
path: book/
27+
path: book/build
2828
retention-days: "3"
2929

3030
- name: Deploy Book
3131
uses: JamesIves/github-pages-deploy-action@v4
3232
if: ${{ github.event_name == 'push' && startsWith('refs/heads/main', github.ref) }}
3333
with:
3434
branch: gh-pages
35-
folder: book/
35+
folder: book/build
3636
single-commit: true

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/book
1+
/book/build
22
**/target
33

44
.idea

README.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,16 @@ API based on the rust compiler mid-level intermediate representation (MIR) that
2020
for development of tools that want to perform sophisticated analyses and make stronger guarantees about the
2121
behavior of Rust programs.
2222

23+
This is the repository we use to organise and document our work.
2324

24-
This is the repository we use to organise our work. Please refer to our [charter] as well
25-
as our [github pages website][gh-pages] for more information on our goals and
26-
current scope.
25+
If you are wondering how to use Stable MIR in your project, please check out the [Getting Started][tutorial] chapter.
2726

28-
If you are wondering how to use Stable MIR in your project, also see the [rustc_smir crate][rustc_smir].
29-
30-
[charter]: ./CHARTER.md
3127
[gh-pages]: https://rust-lang.github.io/project-stable-mir
32-
[rustc_smir]: https://github.com/rust-lang/rust/tree/master/compiler/rustc_smir
3328

29+
[tutorial]: https://rust-lang.github.io/project-stable-mir/getting-started.html
3430

3531
## How Can I Get Involved?
3632

37-
3833
[You can find a list of the current members available
3934
on `rust-lang/team`.][team-toml]
4035

@@ -46,5 +41,7 @@ yourself over there and ask us any questions you have.
4641

4742

4843
[open issues]: https://github.com/rust-lang/project-stable-mir/issues
44+
4945
[chat-link]: https://rust-lang.zulipchat.com/#narrow/stream/320896-project-stable-mir
46+
5047
[team-toml]: https://github.com/rust-lang/team/blob/master/teams/project-stable-mir.toml

SUMMARY.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

book.toml

Lines changed: 0 additions & 10 deletions
This file was deleted.

book/book.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[book]
2+
title = "Stable MIR Librarification Project"
3+
language = "en"
4+
5+
[output.html]
6+
curly-quotes = true
7+
git-repository-url = "https://github.com/rust-lang/project-stable-mir"
8+
site-url = "/project-stable-mir/"
9+
10+
[output.html.playground]
11+
runnable = false
12+
13+
[rust]
14+
edition = "2021"
15+
16+
[build]
17+
build-dir = "build"

book/src/SUMMARY.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Project StableMIR
2+
3+
- [Welcome](./welcome.md)
4+
- [Getting Started](./getting-started.md)
5+
- [Initial Integration](./initial.md)
6+
- [Migrating to StableMIR](./migrating.md)
7+
- [Tool Development: Tips and Tricks](./tricks.md)
8+
- [Contributing](./contributing.md)
File renamed without changes.

book/src/getting-started.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Getting Started
2+
3+
The StableMIR APIs are currently exposed as a crate in the compiler named `stable_mir`[^release].
4+
This crate includes the definition of structures and methods to be stabilized,
5+
which are expected to become the stable APIs in the compiler.
6+
7+
These APIs were designed to provide information about a Rust crate, including the body of functions, as well as type
8+
and layout information.
9+
10+
This chapter has two sections directed at different use cases.
11+
12+
1. If you already have a crate that uses some of the Rust compiler libraries,
13+
and you are interested in migrating them to the StableMIR APIs,
14+
you can find more details about your use case at the [Migrating to StableMIR](./migrating.md) section.
15+
2. If you are starting your integration with the Rust compiler via StableMIR, we recommend reading through the
16+
[Initial Integration](./initial.md) chapter.
17+
18+
We also include a [Tips and Tricks](./tricks.md) section that is related to a few common obstacles tool writers
19+
encounter,
20+
that is not directly related to the `stable_mir` crate and APIs.
21+
22+
Our repository also includes a little [demo crate](https://github.com/rust-lang/project-stable-mir/tree/main/demo) that
23+
demonstrate how `stable_mir` crate can be used to analyze the main function of a crate.
24+
25+
The latest crate documentation can be found in the
26+
[nightly documentation here](https://doc.rust-lang.org/nightly/nightly-rustc/stable_mir/index.html)
27+
28+
[^release]: We are planning to release the `stable_mir` crate into crates.io in the near future.
29+
See issue [#0030](https://github.com/rust-lang/project-stable-mir/issues/30) for the current release status.

book/src/initial.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Initial Integration
2+
3+
In order to use `stable_mir` in your crate, you will need to do the following:
4+
5+
1. Use a nightly toolchain that includes the `stable_mir` crate.
6+
2. Install at least the following rustup components: "rustc-dev" and "llvm-tools"
7+
3. Declare `stable_mir` as an external crate at the top of your crate:
8+
9+
```rust
10+
extern crate stable_mir;
11+
```
12+
13+
For 1 and 2, we highly recommend adding a "rust-toolchain.toml" file to your project.
14+
We also recommend to pin down a specific nightly version, to ensure all users and tests are using the same compiler
15+
version.
16+
Therefore, the same `stable_mir` crate version. E.g.:
17+
18+
```toml
19+
# Example of a rust-toolchain.toml
20+
[toolchain]
21+
# Update the date to change the toolchain version.
22+
channel = "nightly-2024-06-17"
23+
components = ["llvm-tools", "rustc-dev", "rust-src"]
24+
```
25+
26+
## Initializing StableMIR
27+
28+
There's currently no stable way to initialize the Rust compiler and StableMIR.
29+
See [#0069](https://github.com/rust-lang/project-stable-mir/issues/69) for more details.
30+
31+
Instead, StableMIR includes two unstable workarounds to give you a quick start.
32+
The `run` and `run_with_tcx` macros, both from present in the `rustc_smir` crate.
33+
34+
In order to use the `run` macro, you first need to declare the following external crates:
35+
36+
```rust
37+
extern crate rustc_driver;
38+
extern crate rustc_interface;
39+
#[macro_use]
40+
extern crate rustc_smir;
41+
// This one you should know already. =)
42+
extern crate stable_mir;
43+
```
44+
45+
Then start the driver using the `run!()` macro:
46+
47+
```rust
48+
let result = run!(rustc_args, callback_fn);
49+
```
50+
51+
This macro takes two arguments:
52+
53+
1. A vector with the command arguments to the compiler.
54+
2. A callback function, which can either be a closure expression or a function ident.
55+
- This callback function shouldn't take any argument, and it should return a `ControlFlow<B,C>`.
56+
57+
It will trigger the compilation in the current process, with the provided arguments, and invoke the callback after the
58+
compiler ran all its analyses, but before code generation.
59+
60+
The macro will return a `Result<C, CompilerError<B>>` representing the compilation result and the callback return value.
61+
62+
The second option is the `run_with_tcx!()` macro, which is very similar to the `run` macro.
63+
The main difference is that this macro passes a copy of the Rust compiler context (`TyCtxt`) to the callback,
64+
which allows the user to also make calls to internal compiler APIs.
65+
66+
Note that this option also requires the declaration of the `rustc_middle` external crate, i.e., you should now have the
67+
following declarations:
68+
69+
```rust
70+
extern crate rustc_driver;
71+
extern crate rustc_interface;
72+
extern crate rustc_middle; // This one is new!
73+
#[macro_use]
74+
extern crate rustc_smir;
75+
extern crate stable_mir;
76+
```
77+
78+
## Scope of StableMIR objects
79+
80+
StableMIR objects should not be used outside the scope of the callback function.
81+
Any usage outside this scope can panic or return an incorrect value.
82+
83+
For example, the following code is valid, since the logic we are storing is only used while the callback function
84+
is running:
85+
86+
```rust
87+
fn print_items(rustc_args: Vec<String>) {
88+
let _result = run!(rustc_args, || {
89+
for item in stable_mir::all_local_items() {
90+
// Using items inside the callback!
91+
println!(" - {}", item.name())
92+
}
93+
});
94+
}
95+
```
96+
97+
However, the following usage isn't valid, and `stable_mir` will panic when we invoke the `name()` function.
98+
99+
```rust
100+
fn broken_print_items(rustc_args: Vec<String>) {
101+
let result = run!(rustc_args, || { ControlFlow::Continue(stable_mir::all_local_items())});
102+
if let ControlFlow::Continue(items) = result {
103+
for item in items {
104+
// Using item outside the callback function is wrong!
105+
println!(" - {}", item.name())
106+
}
107+
}
108+
}
109+
```
110+
111+
StableMIR objects should also not be shared across different threads.
112+
113+
## Analyzing crate definitions
114+
115+
TODO
116+
117+
## Analyzing monomorphic instances
118+
119+
TODO

0 commit comments

Comments
 (0)