mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 06:14:46 +00:00
Merge branch 'trunk' into valgrind
This commit is contained in:
commit
f73a6c0da4
50 changed files with 1425 additions and 579 deletions
49
examples/shared-quicksort/platform/README.md
Normal file
49
examples/shared-quicksort/platform/README.md
Normal file
|
@ -0,0 +1,49 @@
|
|||
# Rebuilding the host from source
|
||||
|
||||
Here are the current steps to rebuild this host. These
|
||||
steps can likely be moved into a `build.rs` script after
|
||||
turning `host.rs` into a `cargo` project, but that hasn't
|
||||
been attempted yet.
|
||||
|
||||
## Compile the Rust and C sources
|
||||
|
||||
Currently this host has both a `host.rs` and a `host.c`.
|
||||
This is only because we haven't figured out a way to convince
|
||||
Rust to emit a `.o` file that doesn't define a `main` entrypoint,
|
||||
but which is capable of being linked into one later.
|
||||
|
||||
As a workaround, we have `host.rs` expose a function called
|
||||
`rust_main` instead of `main`, and all `host.c` does is provide
|
||||
an actual `main` which imports and then calls `rust_main` from
|
||||
the compiled `host.rs`. It's not the most elegant workaround,
|
||||
but [asking on `users.rust-lang.org`](https://users.rust-lang.org/t/error-when-compiling-linking-with-o-files/49635/4)
|
||||
didn't turn up any nicer approaches. Maybe they're out there though!
|
||||
|
||||
To make this workaround happen, we need to compile both `host.rs`
|
||||
and `host.c`. First, `cd` into `platform/host/src/` and then run:
|
||||
|
||||
```
|
||||
$ clang -c host.c -o c_host.o
|
||||
$ rustc host.rs -o rust_host.o
|
||||
```
|
||||
|
||||
Now we should have `c_host.o` and `rust_host.o` in the curent directory.
|
||||
|
||||
## Link together the `.o` files
|
||||
|
||||
Next, combine `c_host.o` and `rust_host.o` into `host.o` using `ld -r` like so:
|
||||
|
||||
```
|
||||
$ ld -r c_host.o rust_host.o -o host.o
|
||||
```
|
||||
|
||||
Move `host.o` into the appropriate `platform/` subdirectory
|
||||
based on your architecture and operating system. For example,
|
||||
on macOS, you'd move `host.o` into the `platform/host/x86_64-unknown-darwin10/` directory.
|
||||
|
||||
## All done!
|
||||
|
||||
Congratulations! You now have an updated host.
|
||||
|
||||
It's now fine to delete `c_host.o` and `rust_host.o`,
|
||||
since they were only needed to produce `host.o`.
|
12
examples/shared-quicksort/platform/build.sh
Executable file
12
examples/shared-quicksort/platform/build.sh
Executable file
|
@ -0,0 +1,12 @@
|
|||
#!/bin/bash
|
||||
|
||||
# compile c_host.o and rust_host.o
|
||||
clang -c host.c -o c_host.o
|
||||
rustc host.rs -o rust_host.o
|
||||
|
||||
# link them together into host.o
|
||||
ld -r c_host.o rust_host.o -o host.o
|
||||
|
||||
# clean up
|
||||
rm -f c_host.o
|
||||
rm -f rust_host.o
|
7
examples/shared-quicksort/platform/host.c
Normal file
7
examples/shared-quicksort/platform/host.c
Normal file
|
@ -0,0 +1,7 @@
|
|||
#include <stdio.h>
|
||||
|
||||
extern int rust_main();
|
||||
|
||||
int main() {
|
||||
return rust_main();
|
||||
}
|
|
@ -1,33 +1,31 @@
|
|||
#![crate_type = "staticlib"]
|
||||
|
||||
use std::time::SystemTime;
|
||||
|
||||
#[link(name = "roc_app", kind = "static")]
|
||||
extern "C" {
|
||||
#[allow(improper_ctypes)]
|
||||
#[link_name = "quicksort#1"]
|
||||
fn quicksort(list: &[i64]) -> Box<[i64]>;
|
||||
#[link_name = "quicksort_1"]
|
||||
fn quicksort(list: Box<[i64]>) -> Box<[i64]>;
|
||||
}
|
||||
|
||||
const NUM_NUMS: usize = 1_000_000;
|
||||
const NUM_NUMS: usize = 10_000;
|
||||
|
||||
pub fn main() {
|
||||
let nums = {
|
||||
let mut nums = Vec::with_capacity(NUM_NUMS + 1);
|
||||
#[no_mangle]
|
||||
pub fn rust_main() -> isize {
|
||||
let nums: Box<[i64]> = {
|
||||
let mut nums = Vec::with_capacity(NUM_NUMS);
|
||||
|
||||
// give this list refcount 1
|
||||
nums.push((std::usize::MAX - 1) as i64);
|
||||
|
||||
for index in 1..nums.capacity() {
|
||||
let num = index as i64 % 12345;
|
||||
for index in 0..nums.capacity() {
|
||||
let num = index as i64 % 123;
|
||||
|
||||
nums.push(num);
|
||||
}
|
||||
|
||||
nums
|
||||
nums.into()
|
||||
};
|
||||
|
||||
println!("Running Roc shared quicksort");
|
||||
println!("Running Roc quicksort on {} numbers...", nums.len());
|
||||
let start_time = SystemTime::now();
|
||||
let answer = unsafe { quicksort(&nums[1..]) };
|
||||
let answer = unsafe { quicksort(nums) };
|
||||
let end_time = SystemTime::now();
|
||||
let duration = end_time.duration_since(start_time).unwrap();
|
||||
|
||||
|
@ -35,8 +33,7 @@ pub fn main() {
|
|||
"Roc quicksort took {:.4} ms to compute this answer: {:?}",
|
||||
duration.as_secs_f64() * 1000.0,
|
||||
// truncate the answer, so stdout is not swamped
|
||||
// NOTE index 0 is the refcount!
|
||||
&answer[1..20]
|
||||
&answer[0..20]
|
||||
);
|
||||
|
||||
// the pointer is to the first _element_ of the list,
|
||||
|
@ -44,4 +41,7 @@ pub fn main() {
|
|||
// this pointer would segfault/cause badness. Therefore, we
|
||||
// leak it for now
|
||||
Box::leak(answer);
|
||||
|
||||
// Exit code
|
||||
0
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue