-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Rust component in the runtime.
whitequark
committed
Aug 17, 2016
1 parent
6c2ca69
commit 3e2fbac
Showing
13 changed files
with
239 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
authors = ["The ARTIQ Project Developers"] | ||
name = "runtime" | ||
version = "0.0.0" | ||
|
||
[lib] | ||
name = "artiq_rust" | ||
crate-type = ["staticlib"] | ||
path = "src/lib.rs" | ||
|
||
[dependencies] | ||
std_artiq = { path = "libstd_artiq" } | ||
|
||
[profile.dev] | ||
panic = 'abort' | ||
opt-level = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
authors = ["The ARTIQ Project Developers"] | ||
name = "alloc_artiq" | ||
version = "0.0.0" | ||
|
||
[lib] | ||
name = "alloc_artiq" | ||
path = "lib.rs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#![feature(allocator, libc)] | ||
#![no_std] | ||
#![allocator] | ||
|
||
// The minimum alignment guaranteed by the architecture. | ||
const MIN_ALIGN: usize = 8; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 { | ||
unsafe { imp::allocate(size, align) } | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) { | ||
unsafe { imp::deallocate(ptr, old_size, align) } | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn __rust_reallocate(ptr: *mut u8, | ||
old_size: usize, | ||
size: usize, | ||
align: usize) | ||
-> *mut u8 { | ||
unsafe { imp::reallocate(ptr, old_size, size, align) } | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn __rust_reallocate_inplace(ptr: *mut u8, | ||
old_size: usize, | ||
size: usize, | ||
align: usize) | ||
-> usize { | ||
unsafe { imp::reallocate_inplace(ptr, old_size, size, align) } | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn __rust_usable_size(size: usize, align: usize) -> usize { | ||
imp::usable_size(size, align) | ||
} | ||
|
||
mod imp { | ||
extern crate libc; | ||
|
||
use core::cmp; | ||
use core::ptr; | ||
use MIN_ALIGN; | ||
|
||
pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 { | ||
if align <= MIN_ALIGN { | ||
libc::malloc(size as libc::size_t) as *mut u8 | ||
} else { | ||
aligned_malloc(size, align) | ||
} | ||
} | ||
|
||
unsafe fn aligned_malloc(_size: usize, _align: usize) -> *mut u8 { | ||
panic!("aligned_malloc not implemented") | ||
} | ||
|
||
pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, | ||
size: usize, align: usize) -> *mut u8 { | ||
if align <= MIN_ALIGN { | ||
libc::realloc(ptr as *mut libc::c_void, size as libc::size_t) as *mut u8 | ||
} else { | ||
let new_ptr = allocate(size, align); | ||
if !new_ptr.is_null() { | ||
ptr::copy(ptr, new_ptr, cmp::min(size, old_size)); | ||
deallocate(ptr, old_size, align); | ||
} | ||
new_ptr | ||
} | ||
} | ||
|
||
pub unsafe fn reallocate_inplace(_ptr: *mut u8, old_size: usize, | ||
_size: usize, _align: usize) -> usize { | ||
old_size | ||
} | ||
|
||
pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, _align: usize) { | ||
libc::free(ptr as *mut libc::c_void) | ||
} | ||
|
||
pub fn usable_size(size: usize, _align: usize) -> usize { | ||
size | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
authors = ["The ARTIQ Project Developers"] | ||
name = "std_artiq" | ||
version = "0.0.0" | ||
|
||
[lib] | ||
name = "std_artiq" | ||
path = "lib.rs" | ||
|
||
[dependencies] | ||
alloc_artiq = { path = "../liballoc_artiq" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#![feature(lang_items, asm, collections, libc, needs_panic_runtime)] | ||
#![no_std] | ||
#![needs_panic_runtime] | ||
|
||
extern crate alloc_artiq; | ||
extern crate collections; | ||
extern crate libc; | ||
|
||
pub mod prelude { | ||
pub mod v1 { | ||
pub use core::prelude::v1::*; | ||
pub use collections::*; | ||
} | ||
} | ||
|
||
use core::fmt::Write; | ||
|
||
#[macro_export] | ||
macro_rules! print { | ||
($($arg:tt)*) => ($crate::print_fmt(format_args!($($arg)*))); | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! println { | ||
($fmt:expr) => (print!(concat!($fmt, "\n"))); | ||
($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*)); | ||
} | ||
|
||
extern { | ||
fn putchar(c: libc::c_int) -> libc::c_int; | ||
fn readchar() -> libc::c_char; | ||
} | ||
|
||
pub struct Console; | ||
|
||
impl core::fmt::Write for Console { | ||
fn write_str(&mut self, s: &str) -> Result<(), core::fmt::Error> { | ||
for c in s.bytes() { unsafe { putchar(c as i32); } } | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn print_fmt(args: self::core::fmt::Arguments) { | ||
let _ = Console.write_fmt(args); | ||
} | ||
|
||
#[lang = "panic_fmt"] | ||
extern fn panic_fmt(args: self::core::fmt::Arguments, file: &'static str, line: u32) -> ! { | ||
let _ = write!(Console, "panic at {}:{}: ", file, line); | ||
let _ = Console.write_fmt(args); | ||
let _ = write!(Console, "\nwaiting for debugger...\n"); | ||
unsafe { | ||
let _ = readchar(); | ||
loop { asm!("l.trap 0") } | ||
} | ||
} | ||
|
||
// Allow linking with crates that are built as -Cpanic=unwind even when the root crate | ||
// is built with -Cpanic=abort. | ||
#[allow(non_snake_case)] | ||
#[no_mangle] | ||
pub extern "C" fn _Unwind_Resume() -> ! { | ||
loop {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#![no_std] | ||
|
||
#[macro_use] | ||
extern crate std_artiq as std; | ||
|
||
use std::prelude::v1::*; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn rust_main() { | ||
println!("hello from rust!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters