2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-07-10 17:52:05 -05:00
|
|
|
//! Runtime calls emitted by the compiler.
|
|
|
|
|
2013-01-11 23:01:42 -06:00
|
|
|
use cast::transmute;
|
2013-07-02 19:36:58 -05:00
|
|
|
use libc::{c_char, c_uchar, c_void, size_t, uintptr_t, c_int};
|
2012-12-23 16:41:37 -06:00
|
|
|
use str;
|
|
|
|
use sys;
|
2013-04-21 21:03:52 -05:00
|
|
|
use rt::{context, OldTaskContext};
|
2013-05-19 18:50:21 -05:00
|
|
|
use rt::task::Task;
|
|
|
|
use rt::local::Local;
|
2013-06-22 18:52:40 -05:00
|
|
|
use rt::borrowck;
|
2012-07-10 17:52:05 -05:00
|
|
|
|
2013-02-26 23:10:03 -06:00
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
pub type rust_task = c_void;
|
2012-07-16 14:28:15 -05:00
|
|
|
|
2013-03-05 13:57:50 -06:00
|
|
|
pub mod rustrt {
|
2013-05-01 09:29:47 -05:00
|
|
|
use unstable::lang::rust_task;
|
2013-07-02 19:36:58 -05:00
|
|
|
use libc::{c_char, uintptr_t};
|
2012-07-17 12:48:19 -05:00
|
|
|
|
2013-07-18 21:08:57 -05:00
|
|
|
extern {
|
2013-03-05 13:57:50 -06:00
|
|
|
#[rust_stack]
|
2013-07-18 21:08:57 -05:00
|
|
|
pub unsafe fn rust_upcall_malloc(td: *c_char, size: uintptr_t)
|
|
|
|
-> *c_char;
|
2013-03-05 13:57:50 -06:00
|
|
|
|
|
|
|
#[rust_stack]
|
2013-07-18 21:08:57 -05:00
|
|
|
pub unsafe fn rust_upcall_free(ptr: *c_char);
|
2013-03-29 18:55:04 -05:00
|
|
|
|
|
|
|
#[fast_ffi]
|
2013-07-18 21:08:57 -05:00
|
|
|
pub unsafe fn rust_upcall_malloc_noswitch(td: *c_char,
|
|
|
|
size: uintptr_t)
|
|
|
|
-> *c_char;
|
2013-03-29 18:55:04 -05:00
|
|
|
|
2013-05-06 10:16:17 -05:00
|
|
|
#[rust_stack]
|
2013-07-18 21:08:57 -05:00
|
|
|
pub fn rust_try_get_task() -> *rust_task;
|
2013-03-05 13:57:50 -06:00
|
|
|
}
|
2012-07-10 17:52:05 -05:00
|
|
|
}
|
|
|
|
|
2013-01-07 14:21:34 -06:00
|
|
|
#[lang="fail_"]
|
2013-02-27 20:34:04 -06:00
|
|
|
pub fn fail_(expr: *c_char, file: *c_char, line: size_t) -> ! {
|
2012-12-10 19:22:10 -06:00
|
|
|
sys::begin_unwind_(expr, file, line);
|
2012-07-10 17:52:05 -05:00
|
|
|
}
|
|
|
|
|
2013-01-07 14:21:34 -06:00
|
|
|
#[lang="fail_bounds_check"]
|
2013-04-09 00:31:23 -05:00
|
|
|
pub fn fail_bounds_check(file: *c_char, line: size_t,
|
2013-05-01 08:14:47 -05:00
|
|
|
index: size_t, len: size_t) {
|
2012-09-29 06:34:11 -05:00
|
|
|
let msg = fmt!("index out of bounds: the len is %d but the index is %d",
|
|
|
|
len as int, index as int);
|
2013-07-22 23:45:33 -05:00
|
|
|
do msg.as_c_str |buf| {
|
|
|
|
fail_(buf, file, line);
|
2012-09-29 06:34:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-07 14:21:34 -06:00
|
|
|
#[lang="malloc"]
|
2013-02-27 20:34:04 -06:00
|
|
|
pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
|
2013-04-21 21:03:52 -05:00
|
|
|
match context() {
|
|
|
|
OldTaskContext => {
|
2013-04-23 17:16:04 -05:00
|
|
|
return rustrt::rust_upcall_malloc_noswitch(td, size);
|
2013-04-21 21:03:52 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let mut alloc = ::ptr::null();
|
2013-06-10 17:29:02 -05:00
|
|
|
do Local::borrow::<Task,()> |task| {
|
2013-06-26 18:41:00 -05:00
|
|
|
rtdebug!("task pointer: %x, heap pointer: %x",
|
2013-07-25 04:46:48 -05:00
|
|
|
::borrow::to_uint(task),
|
|
|
|
::borrow::to_uint(&task.heap));
|
2013-05-19 03:04:01 -05:00
|
|
|
alloc = task.heap.alloc(td as *c_void, size as uint) as *c_char;
|
2013-04-21 21:03:52 -05:00
|
|
|
}
|
|
|
|
return alloc;
|
|
|
|
}
|
|
|
|
}
|
2012-07-17 12:48:19 -05:00
|
|
|
}
|
|
|
|
|
2012-07-23 18:00:19 -05:00
|
|
|
// NB: Calls to free CANNOT be allowed to fail, as throwing an exception from
|
|
|
|
// inside a landing pad may corrupt the state of the exception handler. If a
|
|
|
|
// problem occurs, call exit instead.
|
2013-01-07 14:21:34 -06:00
|
|
|
#[lang="free"]
|
2013-02-27 20:34:04 -06:00
|
|
|
pub unsafe fn local_free(ptr: *c_char) {
|
2013-06-22 03:09:06 -05:00
|
|
|
::rt::local_heap::local_free(ptr);
|
2012-07-17 12:48:19 -05:00
|
|
|
}
|
|
|
|
|
2013-05-01 09:29:47 -05:00
|
|
|
#[lang="borrow_as_imm"]
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-05-01 12:48:00 -05:00
|
|
|
pub unsafe fn borrow_as_imm(a: *u8, file: *c_char, line: size_t) -> uint {
|
2013-06-22 18:52:40 -05:00
|
|
|
borrowck::borrow_as_imm(a, file, line)
|
2013-05-01 09:29:47 -05:00
|
|
|
}
|
|
|
|
|
2013-05-01 12:48:00 -05:00
|
|
|
#[lang="borrow_as_mut"]
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-05-01 12:48:00 -05:00
|
|
|
pub unsafe fn borrow_as_mut(a: *u8, file: *c_char, line: size_t) -> uint {
|
2013-06-22 18:52:40 -05:00
|
|
|
borrowck::borrow_as_mut(a, file, line)
|
2013-05-01 12:48:00 -05:00
|
|
|
}
|
|
|
|
|
2013-05-03 04:42:00 -05:00
|
|
|
#[lang="record_borrow"]
|
|
|
|
pub unsafe fn record_borrow(a: *u8, old_ref_count: uint,
|
|
|
|
file: *c_char, line: size_t) {
|
2013-06-22 18:52:40 -05:00
|
|
|
borrowck::record_borrow(a, old_ref_count, file, line)
|
2013-05-03 04:42:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[lang="unrecord_borrow"]
|
|
|
|
pub unsafe fn unrecord_borrow(a: *u8, old_ref_count: uint,
|
|
|
|
file: *c_char, line: size_t) {
|
2013-06-22 18:52:40 -05:00
|
|
|
borrowck::unrecord_borrow(a, old_ref_count, file, line)
|
2013-05-03 04:42:00 -05:00
|
|
|
}
|
|
|
|
|
2013-05-01 12:48:00 -05:00
|
|
|
#[lang="return_to_mut"]
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-05-04 13:25:15 -05:00
|
|
|
pub unsafe fn return_to_mut(a: *u8, orig_ref_count: uint,
|
2013-05-01 20:46:34 -05:00
|
|
|
file: *c_char, line: size_t) {
|
2013-06-22 18:52:40 -05:00
|
|
|
borrowck::return_to_mut(a, orig_ref_count, file, line)
|
2013-05-01 12:48:00 -05:00
|
|
|
}
|
|
|
|
|
2013-05-01 08:14:47 -05:00
|
|
|
#[lang="check_not_borrowed"]
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-05-01 08:14:47 -05:00
|
|
|
pub unsafe fn check_not_borrowed(a: *u8,
|
|
|
|
file: *c_char,
|
|
|
|
line: size_t) {
|
2013-06-22 18:52:40 -05:00
|
|
|
borrowck::check_not_borrowed(a, file, line)
|
2013-01-11 23:01:42 -06:00
|
|
|
}
|
|
|
|
|
2013-01-29 21:59:52 -06:00
|
|
|
#[lang="strdup_uniq"]
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-01-29 21:59:52 -06:00
|
|
|
pub unsafe fn strdup_uniq(ptr: *c_uchar, len: uint) -> ~str {
|
|
|
|
str::raw::from_buf_len(ptr, len)
|
|
|
|
}
|
|
|
|
|
2013-06-22 03:09:06 -05:00
|
|
|
#[lang="annihilate"]
|
|
|
|
pub unsafe fn annihilate() {
|
|
|
|
::cleanup::annihilate()
|
|
|
|
}
|
|
|
|
|
2013-02-26 21:40:39 -06:00
|
|
|
#[lang="start"]
|
2013-03-30 21:59:21 -05:00
|
|
|
pub fn start(main: *u8, argc: int, argv: **c_char,
|
|
|
|
crate_map: *u8) -> int {
|
2013-05-08 16:52:30 -05:00
|
|
|
use rt;
|
2013-05-08 17:28:30 -05:00
|
|
|
use os;
|
2013-03-30 21:59:21 -05:00
|
|
|
|
|
|
|
unsafe {
|
2013-05-08 17:28:30 -05:00
|
|
|
let use_old_rt = os::getenv("RUST_NEWRT").is_none();
|
2013-03-30 22:00:19 -05:00
|
|
|
if use_old_rt {
|
2013-03-30 21:59:21 -05:00
|
|
|
return rust_start(main as *c_void, argc as c_int, argv,
|
|
|
|
crate_map as *c_void) as int;
|
|
|
|
} else {
|
2013-05-08 18:53:40 -05:00
|
|
|
return do rt::start(argc, argv as **u8, crate_map) {
|
2013-06-20 00:41:54 -05:00
|
|
|
let main: extern "Rust" fn() = transmute(main);
|
|
|
|
main();
|
2013-05-08 16:52:30 -05:00
|
|
|
};
|
2013-03-30 21:59:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern {
|
|
|
|
fn rust_start(main: *c_void, argc: c_int, argv: **c_char,
|
|
|
|
crate_map: *c_void) -> c_int;
|
|
|
|
}
|
|
|
|
}
|