1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Runtime calls emitted by the compiler.
14 use libc::{c_char, c_uchar, c_void, size_t, uintptr_t, c_int};
17 use rt::{context, OldTaskContext};
22 #[allow(non_camel_case_types)]
23 pub type rust_task = c_void;
26 use unstable::lang::rust_task;
27 use libc::{c_char, uintptr_t};
31 pub unsafe fn rust_upcall_malloc(td: *c_char, size: uintptr_t)
35 pub unsafe fn rust_upcall_free(ptr: *c_char);
38 pub unsafe fn rust_upcall_malloc_noswitch(td: *c_char,
43 pub fn rust_try_get_task() -> *rust_task;
45 pub fn rust_dbg_breakpoint();
50 pub fn fail_(expr: *c_char, file: *c_char, line: size_t) -> ! {
51 sys::begin_unwind_(expr, file, line);
54 #[lang="fail_bounds_check"]
55 pub fn fail_bounds_check(file: *c_char, line: size_t,
56 index: size_t, len: size_t) {
57 let msg = fmt!("index out of bounds: the len is %d but the index is %d",
58 len as int, index as int);
59 do msg.as_buf |p, _len| {
60 fail_(p as *c_char, file, line);
65 pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
68 return rustrt::rust_upcall_malloc_noswitch(td, size);
71 let mut alloc = ::ptr::null();
72 do Local::borrow::<Task,()> |task| {
73 rtdebug!("task pointer: %x, heap pointer: %x",
76 alloc = task.heap.alloc(td as *c_void, size as uint) as *c_char;
83 // NB: Calls to free CANNOT be allowed to fail, as throwing an exception from
84 // inside a landing pad may corrupt the state of the exception handler. If a
85 // problem occurs, call exit instead.
87 pub unsafe fn local_free(ptr: *c_char) {
88 ::rt::local_heap::local_free(ptr);
91 #[lang="borrow_as_imm"]
93 pub unsafe fn borrow_as_imm(a: *u8, file: *c_char, line: size_t) -> uint {
94 borrowck::borrow_as_imm(a, file, line)
97 #[lang="borrow_as_mut"]
99 pub unsafe fn borrow_as_mut(a: *u8, file: *c_char, line: size_t) -> uint {
100 borrowck::borrow_as_mut(a, file, line)
103 #[lang="record_borrow"]
104 pub unsafe fn record_borrow(a: *u8, old_ref_count: uint,
105 file: *c_char, line: size_t) {
106 borrowck::record_borrow(a, old_ref_count, file, line)
109 #[lang="unrecord_borrow"]
110 pub unsafe fn unrecord_borrow(a: *u8, old_ref_count: uint,
111 file: *c_char, line: size_t) {
112 borrowck::unrecord_borrow(a, old_ref_count, file, line)
115 #[lang="return_to_mut"]
117 pub unsafe fn return_to_mut(a: *u8, orig_ref_count: uint,
118 file: *c_char, line: size_t) {
119 borrowck::return_to_mut(a, orig_ref_count, file, line)
122 #[lang="check_not_borrowed"]
124 pub unsafe fn check_not_borrowed(a: *u8,
127 borrowck::check_not_borrowed(a, file, line)
130 #[lang="strdup_uniq"]
132 pub unsafe fn strdup_uniq(ptr: *c_uchar, len: uint) -> ~str {
133 str::raw::from_buf_len(ptr, len)
137 pub unsafe fn annihilate() {
138 ::cleanup::annihilate()
142 pub fn start(main: *u8, argc: int, argv: **c_char,
143 crate_map: *u8) -> int {
148 let use_old_rt = os::getenv("RUST_NEWRT").is_none();
150 return rust_start(main as *c_void, argc as c_int, argv,
151 crate_map as *c_void) as int;
153 return do rt::start(argc, argv as **u8, crate_map) {
154 let main: extern "Rust" fn() = transmute(main);
161 fn rust_start(main: *c_void, argc: c_int, argv: **c_char,
162 crate_map: *c_void) -> c_int;