Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3e2fbac

Browse files
author
whitequark
committedAug 17, 2016
Add a Rust component in the runtime.
1 parent 6c2ca69 commit 3e2fbac

File tree

13 files changed

+239
-6
lines changed

13 files changed

+239
-6
lines changed
 

Diff for: ‎artiq/runtime.rs/Cargo.lock

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

Diff for: ‎artiq/runtime.rs/Cargo.toml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
authors = ["The ARTIQ Project Developers"]
3+
name = "runtime"
4+
version = "0.0.0"
5+
6+
[lib]
7+
name = "artiq_rust"
8+
crate-type = ["staticlib"]
9+
path = "src/lib.rs"
10+
11+
[dependencies]
12+
std_artiq = { path = "libstd_artiq" }
13+
14+
[profile.dev]
15+
panic = 'abort'
16+
opt-level = 2

Diff for: ‎artiq/runtime.rs/liballoc_artiq/Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
authors = ["The ARTIQ Project Developers"]
3+
name = "alloc_artiq"
4+
version = "0.0.0"
5+
6+
[lib]
7+
name = "alloc_artiq"
8+
path = "lib.rs"

Diff for: ‎artiq/runtime.rs/liballoc_artiq/lib.rs

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#![feature(allocator, libc)]
2+
#![no_std]
3+
#![allocator]
4+
5+
// The minimum alignment guaranteed by the architecture.
6+
const MIN_ALIGN: usize = 8;
7+
8+
#[no_mangle]
9+
pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
10+
unsafe { imp::allocate(size, align) }
11+
}
12+
13+
#[no_mangle]
14+
pub extern "C" fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) {
15+
unsafe { imp::deallocate(ptr, old_size, align) }
16+
}
17+
18+
#[no_mangle]
19+
pub extern "C" fn __rust_reallocate(ptr: *mut u8,
20+
old_size: usize,
21+
size: usize,
22+
align: usize)
23+
-> *mut u8 {
24+
unsafe { imp::reallocate(ptr, old_size, size, align) }
25+
}
26+
27+
#[no_mangle]
28+
pub extern "C" fn __rust_reallocate_inplace(ptr: *mut u8,
29+
old_size: usize,
30+
size: usize,
31+
align: usize)
32+
-> usize {
33+
unsafe { imp::reallocate_inplace(ptr, old_size, size, align) }
34+
}
35+
36+
#[no_mangle]
37+
pub extern "C" fn __rust_usable_size(size: usize, align: usize) -> usize {
38+
imp::usable_size(size, align)
39+
}
40+
41+
mod imp {
42+
extern crate libc;
43+
44+
use core::cmp;
45+
use core::ptr;
46+
use MIN_ALIGN;
47+
48+
pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
49+
if align <= MIN_ALIGN {
50+
libc::malloc(size as libc::size_t) as *mut u8
51+
} else {
52+
aligned_malloc(size, align)
53+
}
54+
}
55+
56+
unsafe fn aligned_malloc(_size: usize, _align: usize) -> *mut u8 {
57+
panic!("aligned_malloc not implemented")
58+
}
59+
60+
pub unsafe fn reallocate(ptr: *mut u8, old_size: usize,
61+
size: usize, align: usize) -> *mut u8 {
62+
if align <= MIN_ALIGN {
63+
libc::realloc(ptr as *mut libc::c_void, size as libc::size_t) as *mut u8
64+
} else {
65+
let new_ptr = allocate(size, align);
66+
if !new_ptr.is_null() {
67+
ptr::copy(ptr, new_ptr, cmp::min(size, old_size));
68+
deallocate(ptr, old_size, align);
69+
}
70+
new_ptr
71+
}
72+
}
73+
74+
pub unsafe fn reallocate_inplace(_ptr: *mut u8, old_size: usize,
75+
_size: usize, _align: usize) -> usize {
76+
old_size
77+
}
78+
79+
pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, _align: usize) {
80+
libc::free(ptr as *mut libc::c_void)
81+
}
82+
83+
pub fn usable_size(size: usize, _align: usize) -> usize {
84+
size
85+
}
86+
}

Diff for: ‎artiq/runtime.rs/libstd_artiq/Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
authors = ["The ARTIQ Project Developers"]
3+
name = "std_artiq"
4+
version = "0.0.0"
5+
6+
[lib]
7+
name = "std_artiq"
8+
path = "lib.rs"
9+
10+
[dependencies]
11+
alloc_artiq = { path = "../liballoc_artiq" }

Diff for: ‎artiq/runtime.rs/libstd_artiq/lib.rs

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#![feature(lang_items, asm, collections, libc, needs_panic_runtime)]
2+
#![no_std]
3+
#![needs_panic_runtime]
4+
5+
extern crate alloc_artiq;
6+
extern crate collections;
7+
extern crate libc;
8+
9+
pub mod prelude {
10+
pub mod v1 {
11+
pub use core::prelude::v1::*;
12+
pub use collections::*;
13+
}
14+
}
15+
16+
use core::fmt::Write;
17+
18+
#[macro_export]
19+
macro_rules! print {
20+
($($arg:tt)*) => ($crate::print_fmt(format_args!($($arg)*)));
21+
}
22+
23+
#[macro_export]
24+
macro_rules! println {
25+
($fmt:expr) => (print!(concat!($fmt, "\n")));
26+
($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*));
27+
}
28+
29+
extern {
30+
fn putchar(c: libc::c_int) -> libc::c_int;
31+
fn readchar() -> libc::c_char;
32+
}
33+
34+
pub struct Console;
35+
36+
impl core::fmt::Write for Console {
37+
fn write_str(&mut self, s: &str) -> Result<(), core::fmt::Error> {
38+
for c in s.bytes() { unsafe { putchar(c as i32); } }
39+
Ok(())
40+
}
41+
}
42+
43+
pub fn print_fmt(args: self::core::fmt::Arguments) {
44+
let _ = Console.write_fmt(args);
45+
}
46+
47+
#[lang = "panic_fmt"]
48+
extern fn panic_fmt(args: self::core::fmt::Arguments, file: &'static str, line: u32) -> ! {
49+
let _ = write!(Console, "panic at {}:{}: ", file, line);
50+
let _ = Console.write_fmt(args);
51+
let _ = write!(Console, "\nwaiting for debugger...\n");
52+
unsafe {
53+
let _ = readchar();
54+
loop { asm!("l.trap 0") }
55+
}
56+
}
57+
58+
// Allow linking with crates that are built as -Cpanic=unwind even when the root crate
59+
// is built with -Cpanic=abort.
60+
#[allow(non_snake_case)]
61+
#[no_mangle]
62+
pub extern "C" fn _Unwind_Resume() -> ! {
63+
loop {}
64+
}

Diff for: ‎artiq/runtime.rs/src/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![no_std]
2+
3+
#[macro_use]
4+
extern crate std_artiq as std;
5+
6+
use std::prelude::v1::*;
7+
8+
#[no_mangle]
9+
pub extern "C" fn rust_main() {
10+
println!("hello from rust!");
11+
}

Diff for: ‎artiq/runtime/Makefile

+12-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ all: runtime.bin runtime.fbi
2727
%.fbi: %.bin
2828
@echo " MSCIMG " $@ && $(PYTHON) -m misoc.tools.mkmscimg -f -o $@ $<
2929

30-
runtime.elf: $(OBJECTS)
30+
runtime.elf: $(OBJECTS) libartiq_rust.a
3131
$(LD) $(LDFLAGS) \
32+
--gc-sections \
3233
-T $(RUNTIME_DIRECTORY)/runtime.ld \
3334
-N -o $@ \
3435
../libbase/crt0-$(CPU).o \
@@ -38,7 +39,8 @@ runtime.elf: $(OBJECTS)
3839
-L../libm \
3940
-L../liballoc \
4041
-L../liblwip \
41-
-lbase -lm -lcompiler-rt -lalloc -llwip
42+
-Lcargo/or1k-unknown-none/debug/ \
43+
-lartiq_rust -lbase -lm -lcompiler-rt -lalloc -llwip
4244
@chmod -x $@
4345

4446
ksupport.elf: $(OBJECTS_KSUPPORT)
@@ -58,6 +60,14 @@ ksupport.elf: $(OBJECTS_KSUPPORT)
5860
ksupport_data.o: ksupport.elf
5961
$(LD) -r -b binary -o $@ $<
6062

63+
libartiq_rust.a:
64+
CARGO_TARGET_DIR="./cargo" \
65+
cargo rustc --verbose \
66+
--manifest-path $(RUNTIME_DIRECTORY)/../runtime.rs/Cargo.toml \
67+
--target=or1k-unknown-none -- \
68+
-C target-feature=+mul,+div,+ffl1,+cmov,+addc -C opt-level=s \
69+
-L../libcompiler-rt
70+
6171
%.o: $(RUNTIME_DIRECTORY)/%.c
6272
$(compile)
6373

Diff for: ‎artiq/runtime/main.c

+5
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ static int check_test_mode(void)
262262

263263
extern void _fheap, _eheap;
264264

265+
extern void rust_main();
266+
265267
int main(void)
266268
{
267269
irq_setmask(0);
@@ -276,6 +278,9 @@ int main(void)
276278
puts("Press 't' to enter test mode...");
277279
blink_led();
278280

281+
puts("Calling Rust...");
282+
rust_main();
283+
279284
if(check_test_mode()) {
280285
puts("Entering test mode.");
281286
test_main();

Diff for: ‎conda/artiq-kc705-nist_clock/meta.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ requirements:
1515
- migen 0.2
1616
- misoc 0.2
1717
- llvm-or1k
18-
- binutils-or1k-linux
18+
- binutils-or1k-linux >=2.27
19+
- rust-core-or1k
1920
run:
2021
- artiq {{ "{tag} py_{number}+git{hash}".format(tag=environ.get("GIT_DESCRIBE_TAG"), number=environ.get("GIT_DESCRIBE_NUMBER"), hash=environ.get("GIT_DESCRIBE_HASH")[1:]) if "GIT_DESCRIBE_TAG" in environ else "" }}
2122

Diff for: ‎conda/artiq-kc705-nist_qc1/meta.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ requirements:
1515
- migen 0.2
1616
- misoc 0.2
1717
- llvm-or1k
18-
- binutils-or1k-linux
18+
- binutils-or1k-linux >=2.27
19+
- rust-core-or1k
1920
run:
2021
- artiq {{ "{tag} py_{number}+git{hash}".format(tag=environ.get("GIT_DESCRIBE_TAG"), number=environ.get("GIT_DESCRIBE_NUMBER"), hash=environ.get("GIT_DESCRIBE_HASH")[1:]) if "GIT_DESCRIBE_TAG" in environ else "" }}
2122

Diff for: ‎conda/artiq-kc705-nist_qc2/meta.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ requirements:
1515
- migen 0.2
1616
- misoc 0.2
1717
- llvm-or1k
18-
- binutils-or1k-linux
18+
- binutils-or1k-linux >=2.27
19+
- rust-core-or1k
1920
run:
2021
- artiq {{ "{tag} py_{number}+git{hash}".format(tag=environ.get("GIT_DESCRIBE_TAG"), number=environ.get("GIT_DESCRIBE_NUMBER"), hash=environ.get("GIT_DESCRIBE_HASH")[1:]) if "GIT_DESCRIBE_TAG" in environ else "" }}
2122

Diff for: ‎conda/artiq-pipistrello-nist_qc1/meta.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ requirements:
1515
- migen 0.2
1616
- misoc 0.2
1717
- llvm-or1k
18-
- binutils-or1k-linux
18+
- binutils-or1k-linux >=2.27
19+
- rust-core-or1k
1920
run:
2021
- artiq {{ "{tag} py_{number}+git{hash}".format(tag=environ.get("GIT_DESCRIBE_TAG"), number=environ.get("GIT_DESCRIBE_NUMBER"), hash=environ.get("GIT_DESCRIBE_HASH")[1:]) if "GIT_DESCRIBE_TAG" in environ else "" }}
2122

0 commit comments

Comments
 (0)
Please sign in to comment.