2012-12-03 16:48:01 -08: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 15:52:05 -07:00
|
|
|
//! Runtime calls emitted by the compiler.
|
|
|
|
|
2013-08-03 17:13:14 -07:00
|
|
|
use c_str::ToCStr;
|
2013-01-11 21:01:42 -08:00
|
|
|
use cast::transmute;
|
2013-08-11 03:17:01 -04:00
|
|
|
use libc::{c_char, c_void, size_t, uintptr_t};
|
2013-08-08 11:38:10 -07:00
|
|
|
use option::{Option, None, Some};
|
2012-12-23 17:41:37 -05:00
|
|
|
use sys;
|
2013-05-19 16:50:21 -07:00
|
|
|
use rt::task::Task;
|
|
|
|
use rt::local::Local;
|
2013-06-22 16:52:40 -07:00
|
|
|
use rt::borrowck;
|
2012-07-10 15:52:05 -07:00
|
|
|
|
2013-01-07 12:21:34 -08:00
|
|
|
#[lang="fail_"]
|
2013-02-27 18:34:04 -08:00
|
|
|
pub fn fail_(expr: *c_char, file: *c_char, line: size_t) -> ! {
|
2012-12-10 17:22:10 -08:00
|
|
|
sys::begin_unwind_(expr, file, line);
|
2012-07-10 15:52:05 -07:00
|
|
|
}
|
|
|
|
|
2013-01-07 12:21:34 -08:00
|
|
|
#[lang="fail_bounds_check"]
|
2013-04-09 01:31:23 -04:00
|
|
|
pub fn fail_bounds_check(file: *c_char, line: size_t,
|
2013-05-01 09:14:47 -04:00
|
|
|
index: size_t, len: size_t) {
|
2012-09-29 12:34:11 +01:00
|
|
|
let msg = fmt!("index out of bounds: the len is %d but the index is %d",
|
|
|
|
len as int, index as int);
|
2013-08-14 19:21:59 -07:00
|
|
|
do msg.with_c_str |buf| {
|
2013-07-22 21:45:33 -07:00
|
|
|
fail_(buf, file, line);
|
2012-09-29 12:34:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-07 12:21:34 -08:00
|
|
|
#[lang="malloc"]
|
2013-02-27 18:34:04 -08:00
|
|
|
pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
|
2013-08-12 19:09:46 -07:00
|
|
|
// XXX: Unsafe borrow for speed. Lame.
|
2013-08-08 11:38:10 -07:00
|
|
|
let task: Option<*mut Task> = Local::try_unsafe_borrow();
|
|
|
|
match task {
|
2013-08-12 19:09:46 -07:00
|
|
|
Some(task) => {
|
|
|
|
(*task).heap.alloc(td as *c_void, size as uint) as *c_char
|
|
|
|
}
|
|
|
|
None => rtabort!("local malloc outside of task")
|
2013-04-21 19:03:52 -07:00
|
|
|
}
|
2012-07-17 10:48:19 -07:00
|
|
|
}
|
|
|
|
|
2012-07-23 16:00:19 -07: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 12:21:34 -08:00
|
|
|
#[lang="free"]
|
2013-02-27 18:34:04 -08:00
|
|
|
pub unsafe fn local_free(ptr: *c_char) {
|
2013-06-22 01:09:06 -07:00
|
|
|
::rt::local_heap::local_free(ptr);
|
2012-07-17 10:48:19 -07:00
|
|
|
}
|
|
|
|
|
2013-05-01 10:29:47 -04:00
|
|
|
#[lang="borrow_as_imm"]
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2013-05-01 13:48:00 -04:00
|
|
|
pub unsafe fn borrow_as_imm(a: *u8, file: *c_char, line: size_t) -> uint {
|
2013-06-22 16:52:40 -07:00
|
|
|
borrowck::borrow_as_imm(a, file, line)
|
2013-05-01 10:29:47 -04:00
|
|
|
}
|
|
|
|
|
2013-05-01 13:48:00 -04:00
|
|
|
#[lang="borrow_as_mut"]
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2013-05-01 13:48:00 -04:00
|
|
|
pub unsafe fn borrow_as_mut(a: *u8, file: *c_char, line: size_t) -> uint {
|
2013-06-22 16:52:40 -07:00
|
|
|
borrowck::borrow_as_mut(a, file, line)
|
2013-05-01 13:48:00 -04:00
|
|
|
}
|
|
|
|
|
2013-05-03 05:42:00 -04:00
|
|
|
#[lang="record_borrow"]
|
|
|
|
pub unsafe fn record_borrow(a: *u8, old_ref_count: uint,
|
|
|
|
file: *c_char, line: size_t) {
|
2013-06-22 16:52:40 -07:00
|
|
|
borrowck::record_borrow(a, old_ref_count, file, line)
|
2013-05-03 05:42:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[lang="unrecord_borrow"]
|
|
|
|
pub unsafe fn unrecord_borrow(a: *u8, old_ref_count: uint,
|
|
|
|
file: *c_char, line: size_t) {
|
2013-06-22 16:52:40 -07:00
|
|
|
borrowck::unrecord_borrow(a, old_ref_count, file, line)
|
2013-05-03 05:42:00 -04:00
|
|
|
}
|
|
|
|
|
2013-05-01 13:48:00 -04:00
|
|
|
#[lang="return_to_mut"]
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2013-05-04 14:25:15 -04:00
|
|
|
pub unsafe fn return_to_mut(a: *u8, orig_ref_count: uint,
|
2013-05-01 21:46:34 -04:00
|
|
|
file: *c_char, line: size_t) {
|
2013-06-22 16:52:40 -07:00
|
|
|
borrowck::return_to_mut(a, orig_ref_count, file, line)
|
2013-05-01 13:48:00 -04:00
|
|
|
}
|
|
|
|
|
2013-05-01 09:14:47 -04:00
|
|
|
#[lang="check_not_borrowed"]
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2013-05-01 09:14:47 -04:00
|
|
|
pub unsafe fn check_not_borrowed(a: *u8,
|
|
|
|
file: *c_char,
|
|
|
|
line: size_t) {
|
2013-06-22 16:52:40 -07:00
|
|
|
borrowck::check_not_borrowed(a, file, line)
|
2013-01-11 21:01:42 -08:00
|
|
|
}
|
|
|
|
|
2013-02-26 19:40:39 -08:00
|
|
|
#[lang="start"]
|
2013-03-30 19:59:21 -07:00
|
|
|
pub fn start(main: *u8, argc: int, argv: **c_char,
|
|
|
|
crate_map: *u8) -> int {
|
2013-05-08 14:52:30 -07:00
|
|
|
use rt;
|
2013-03-30 19:59:21 -07:00
|
|
|
|
|
|
|
unsafe {
|
2013-07-31 23:12:20 -07:00
|
|
|
return do rt::start(argc, argv as **u8, crate_map) {
|
|
|
|
let main: extern "Rust" fn() = transmute(main);
|
|
|
|
main();
|
|
|
|
};
|
2013-03-30 19:59:21 -07:00
|
|
|
}
|
|
|
|
}
|