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-02-26 21:40:39 -06:00
|
|
|
use libc::{c_char, c_uchar, c_void, size_t, uintptr_t, c_int};
|
2013-01-11 23:01:42 -06:00
|
|
|
use managed::raw::BoxRepr;
|
2012-12-23 16:41:37 -06:00
|
|
|
use str;
|
|
|
|
use sys;
|
2013-01-13 18:53:13 -06:00
|
|
|
use private::exchange_alloc;
|
|
|
|
use cast::transmute;
|
2012-07-10 17:52:05 -05:00
|
|
|
|
2012-09-07 20:08:21 -05:00
|
|
|
use gc::{cleanup_stack_for_failure, gc, Word};
|
2012-07-16 14:28:15 -05:00
|
|
|
|
2012-09-02 17:39:37 -05:00
|
|
|
#[allow(non_camel_case_types)]
|
2012-10-02 18:31:52 -05:00
|
|
|
pub type rust_task = c_void;
|
2012-07-10 17:52:05 -05:00
|
|
|
|
2013-01-11 23:01:42 -06:00
|
|
|
#[cfg(target_word_size = "32")]
|
2013-01-29 13:47:18 -06:00
|
|
|
pub const FROZEN_BIT: uint = 0x80000000;
|
2013-01-11 23:01:42 -06:00
|
|
|
#[cfg(target_word_size = "64")]
|
2013-01-29 13:47:18 -06:00
|
|
|
pub const FROZEN_BIT: uint = 0x8000000000000000;
|
2013-01-11 23:01:42 -06:00
|
|
|
|
2013-01-29 13:47:18 -06:00
|
|
|
pub extern mod rustrt {
|
2012-07-17 12:48:19 -05:00
|
|
|
#[rust_stack]
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe fn rust_upcall_malloc(td: *c_char, size: uintptr_t) -> *c_char;
|
2012-07-17 12:48:19 -05:00
|
|
|
|
|
|
|
#[rust_stack]
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe fn rust_upcall_free(ptr: *c_char);
|
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-02-27 20:34:04 -06:00
|
|
|
pub unsafe fn fail_bounds_check(file: *c_char, line: size_t,
|
|
|
|
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);
|
|
|
|
do str::as_buf(msg) |p, _len| {
|
2013-02-27 20:34:04 -06:00
|
|
|
fail_(p as *c_char, file, line);
|
2012-09-29 06:34:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-27 20:34:04 -06:00
|
|
|
pub unsafe fn fail_borrowed() {
|
2013-01-11 23:01:42 -06:00
|
|
|
let msg = "borrowed";
|
|
|
|
do str::as_buf(msg) |msg_p, _| {
|
|
|
|
do str::as_buf("???") |file_p, _| {
|
2013-02-27 20:34:04 -06:00
|
|
|
fail_(msg_p as *c_char, file_p as *c_char, 0);
|
2013-01-11 23:01:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 20:17:56 -06:00
|
|
|
// FIXME #4942: Make these signatures agree with exchange_alloc's signatures
|
2013-01-07 14:21:34 -06:00
|
|
|
#[lang="exchange_malloc"]
|
2013-02-27 20:34:04 -06:00
|
|
|
pub unsafe fn exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char {
|
2013-01-13 18:53:13 -06:00
|
|
|
transmute(exchange_alloc::malloc(transmute(td), transmute(size)))
|
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="exchange_free"]
|
2013-02-27 20:34:04 -06:00
|
|
|
pub unsafe fn exchange_free(ptr: *c_char) {
|
2013-01-13 18:53:13 -06:00
|
|
|
exchange_alloc::free(transmute(ptr))
|
2012-07-17 12:48:19 -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 {
|
2012-08-01 19:30:05 -05:00
|
|
|
return rustrt::rust_upcall_malloc(td, size);
|
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) {
|
2012-07-17 12:48:19 -05:00
|
|
|
rustrt::rust_upcall_free(ptr);
|
|
|
|
}
|
|
|
|
|
2013-01-11 23:01:42 -06:00
|
|
|
#[lang="borrow_as_imm"]
|
|
|
|
#[inline(always)]
|
|
|
|
pub unsafe fn borrow_as_imm(a: *u8) {
|
|
|
|
let a: *mut BoxRepr = transmute(a);
|
|
|
|
(*a).header.ref_count |= FROZEN_BIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[lang="return_to_mut"]
|
|
|
|
#[inline(always)]
|
|
|
|
pub unsafe fn return_to_mut(a: *u8) {
|
|
|
|
let a: *mut BoxRepr = transmute(a);
|
|
|
|
(*a).header.ref_count &= !FROZEN_BIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[lang="check_not_borrowed"]
|
|
|
|
#[inline(always)]
|
|
|
|
pub unsafe fn check_not_borrowed(a: *u8) {
|
|
|
|
let a: *mut BoxRepr = transmute(a);
|
|
|
|
if ((*a).header.ref_count & FROZEN_BIT) != 0 {
|
2013-02-27 20:34:04 -06:00
|
|
|
fail_borrowed();
|
2013-01-11 23:01:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 21:59:52 -06:00
|
|
|
#[lang="strdup_uniq"]
|
|
|
|
pub unsafe fn strdup_uniq(ptr: *c_uchar, len: uint) -> ~str {
|
|
|
|
str::raw::from_buf_len(ptr, len)
|
|
|
|
}
|
|
|
|
|
2013-02-26 21:40:39 -06:00
|
|
|
#[lang="start"]
|
|
|
|
pub fn start(main: *u8, argc: int, argv: *c_char,
|
|
|
|
crate_map: *u8) -> int {
|
|
|
|
|
|
|
|
extern {
|
|
|
|
fn rust_start(main: *c_void, argc: c_int, argv: *c_char,
|
|
|
|
crate_map: *c_void) -> c_int;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
return rust_start(main as *c_void, argc as c_int, argv,
|
|
|
|
crate_map as *c_void) as int;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-10 17:52:05 -05:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|