Skip to content

Commit c651ed8

Browse files
committed
QUICKSTART.md updates
1 parent 9762de1 commit c651ed8

File tree

4 files changed

+62
-12
lines changed

4 files changed

+62
-12
lines changed

QUICKSTART.md

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ rustup update
1919

2020
If this does not solve the problem, open an issue. The project is only compatible with a narrow set of `rustc` versions, and tries to always use the newest nigthly build.
2121
## Install the .NET runtime
22-
22+
**Note: This is only relevant if you want to use this project in its .NET mode**
2323
This project requires the .NET runtime to be used. You can find [instalation instructions here](https://dotnet.microsoft.com/en-us/download).
2424

2525
After installing .NET, run ` dotnet --info` to confoirm it is installed propely.
2626

2727
## Install `ilasm`
28-
28+
**Note: This is only relevant if you want to use this project in its .NET mode**
2929
### Windows
3030

3131
`ilasm` comes [installed with Visual Studio](https://learn.microsoft.com/en-us/dotnet/framework/tools/ilasm-exe-il-assembler). So, you propably already have it. If not, install visual stuido.
@@ -36,7 +36,11 @@ This tool supports both the "Core" and "Mono" flavours of ILASM. While you *can*
3636

3737
### Checking the dependencies
3838

39-
After you installed `dotnet` and `ilasm`, run `./bin/rustflags.rs` to check if you installed `ilasm` and `dotnet` correctly.
39+
After you installed `dotnet` and `ilasm`(, run `./bin/rustflags.rs` to check if you installed `ilasm` and `dotnet` correctly.
40+
If you want to use the project to generate C code, set `C_MODE` to `1`.
41+
```
42+
export C_MODE=1
43+
```
4044

4145
This script uses the experimental `cargo-script` feaure to will check your enviroment, build the project, and print the flags you need to use it.
4246

@@ -57,9 +61,9 @@ target = "x86_64-unknown-linux-gnu" # Change to the host target.
5761
build-std = ["core","alloc","std","panic_abort"]
5862
```
5963

60-
Then, run the codegen utility `rustflags` again, by running `cargo --bin rustflags --release` **in the directory `rustc_codegen_clr`**.
64+
Then, run the codegen utility `rustflags` again, by running `./bin/rustflags.rs` **in the directory `rustc_codegen_clr`**.
6165

62-
It should provide you with the commands necesary for enabling the codegen. They should look something like this:
66+
It should provide you with the commands necesarry for enabling the codegen. They should look something like this:
6367

6468
On Linux:
6569
```bash
@@ -75,4 +79,26 @@ The project makes **no pernament changes** to your instaltation, and simply clos
7579

7680
After this, simply run `cargo run` to compile & run your app inside the .NET runtime. Other cargo commands should work to. There may be quite a few error / warning messages dispalyed, but they will not stop compilation.
7781

78-
NOTE: the project currently does not support any `proc-macros`, due to techincal liminations. Supporting them is possible, but it is currently more effort than it is worth.
82+
NOTE: the project currently does not properly support `proc-macros`, due to technical liminations. Fully supporting them is possible, but it is currently more effort than it is worth.
83+
# Additional information on C support
84+
85+
## Required features
86+
Currently, the generated code will, by default, depend on some features of C that may not always be present.
87+
88+
Those features are:
89+
1. malloc and aligned allocators - certain static/constant variables require alignments higher than the ones guaranteed by the C standard. As a workaround, `cg_clr` will use malloc & aligned allocators
90+
to ensure sufficient alignment of static variables. This is something I am actively working on resolving, and this should not be an issue in the near future.
91+
2. POSIX threading APIs. The header files generated by `cg_clr` will override certain POSIX APIs in order to track new threads. This is needed to ensure proper initialization of thread-local data.
92+
93+
## Odd function names
94+
Additionally, you might notice that the generated code sometimes calls "wierd" functions, like `System_Numerics_BitOperations_LeadingZeroCountu64i32`.
95+
This is a limitation of the current abstractions over .NET and C. Most of the time, those functions simply call / emulate a C builtin. This is also something that will not be an issue in the future:
96+
I am actively working on better abstractions.
97+
98+
## Non-alphabetic identifiers.
99+
100+
By default, `cg_clr` will keep symbol(function) names more-or-less intact. However, those names may contain characters accepted by *most*, but not *all* compilers.
101+
```
102+
_ZN54_$LT$$LP$A$C$B$RP$$u20$as$u20$fuzz100__PrintFDebug$GT$12printf_debug17h769fc7ecbdc54ca9E
103+
```
104+
In those cases, simply set `ASCI_IDENT` enviroment variable to ensure correct symbol names.

bin/build_backend.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ pub static ILASM_PATH: std::sync::LazyLock<String> = std::sync::LazyLock::new(||
9393
pub fn absolute_backend_path() -> PathBuf {
9494
if cfg!(debug_assertions) {
9595
if cfg!(target_os = "linux") {
96-
std::fs::canonicalize("../target/debug/librustc_codegen_clr.so").unwrap()
96+
std::fs::canonicalize("target/debug/librustc_codegen_clr.so").unwrap()
9797
} else if cfg!(target_os = "windows") {
98-
std::fs::canonicalize("../target/debug/rustc_codegen_clr.dll").unwrap()
98+
std::fs::canonicalize("target/debug/rustc_codegen_clr.dll").unwrap()
9999
} else if cfg!(target_os = "macos") {
100100
let current_dir = std::env::current_dir().unwrap();
101101
let dylib_path = current_dir.join("target/debug/librustc_codegen_clr.dylib");

bin/rustflags.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ fn main() {
1919
println!("Welcome to the `rustc_codegen_clr` environment setup helper!");
2020
println!("This tool will help you use the codegen to compile Rust projects.");
2121
println!();
22+
println!("NOTE: if you are only interested in the C side of the project, set the enviroment variable C_MODE to 1, and run this tool again.");
2223
println!("Doing dependency checks...");
23-
ilasm_check();
24+
if !std::env::var("C_MODE").is_ok() {
25+
ilasm_check();
26+
}
2427
println!("Dependency checks succeeded.");
2528
println!();
2629
println!("WARNING: Please note, the project is currently in the early stages of development. Bugs, crashes and miscompilations will occur.");
@@ -31,6 +34,9 @@ fn main() {
3134
println!();
3235
println!("\"{build_env}\"");
3336
println!();
37+
if std::env::var("C_MODE").is_ok() {
38+
println!("Additonally, set `C_MODE` to 1");
39+
}
3440
println!();
3541
#[cfg(target_family = "unix")]
3642
{
@@ -40,6 +46,9 @@ fn main() {
4046
println!();
4147
println!();
4248
println!("export RUSTFLAGS=\"{build_env}\"");
49+
if std::env::var("C_MODE").is_ok() {
50+
println!("export C_MODE=1");
51+
}
4352
println!();
4453
println!();
4554
}
@@ -78,5 +87,19 @@ fn main() {
7887
" In 99.999% of the cases, the bug is within this project and not the Rust compiler."
7988
);
8089
println!();
81-
// std::env::set_var("RUSTFLAGS", build_env);
90+
#[cfg(target_family = "unix")]
91+
{
92+
println!("############# Testing with the Rust compiler #####################");
93+
println!("Testing with the Rust compiler requires:");
94+
println!("1. running `cargo test ::stable`(builds & tests the backend)");
95+
println!("2. running ./setup_rustc_fork.sh to download rustc.");
96+
println!("3. Setting the following environment variables:");
97+
println!("# For .NET");
98+
println!("export RUSTFLAGS_NOT_BOOTSTRAP=\"{build_env} -C link-args=--cargo-support\"");
99+
println!("# For C");
100+
println!("export C_MODE=1");
101+
println!("export RUSTFLAGS_NOT_BOOTSTRAP=\"{build_env} -Cpanic=abort -Zpanic_abort_tests -C link-args=--cargo-support\"");
102+
println!("Go to the `rustc` directory, run `./configure` to setup the compiler.");
103+
println!("./x test core --stage 0 -j 10");
104+
}
82105
}

src/assembly.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,9 +731,10 @@ pub fn add_allocation(alloc_id: u64, asm: &mut cilly::Assembly, tcx: TyCtxt<'_>)
731731
CILNode::AddressOfStaticField(Box::new(field_desc))
732732
}
733733
_ => {
734+
let tl = if thread_local { "t" } else { "g" };
734735
let alloc_name: IString = if let Some(krate) = krate {
735736
format!(
736-
"al_{}_{}_{}_{thread_local}_{}",
737+
"al_{}_{}_{}_{tl}_{}",
737738
encode(alloc_id),
738739
encode(byte_hash),
739740
const_allocation.len(),
@@ -742,7 +743,7 @@ pub fn add_allocation(alloc_id: u64, asm: &mut cilly::Assembly, tcx: TyCtxt<'_>)
742743
.into()
743744
} else {
744745
format!(
745-
"al_{}_{}_{}_{thread_local}",
746+
"al_{}_{}_{}_{tl}",
746747
encode(alloc_id),
747748
encode(byte_hash),
748749
const_allocation.len()

0 commit comments

Comments
 (0)