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};
15 use managed::raw::BoxRepr;
18 use unstable::exchange_alloc;
20 use rt::{context, OldTaskContext};
21 use rt::local_services::borrow_local_services;
23 #[allow(non_camel_case_types)]
24 pub type rust_task = c_void;
26 #[cfg(target_word_size = "32")]
27 pub static FROZEN_BIT: uint = 0x80000000;
28 #[cfg(target_word_size = "64")]
29 pub static FROZEN_BIT: uint = 0x8000000000000000;
32 use libc::{c_char, uintptr_t};
36 unsafe fn rust_upcall_malloc(td: *c_char, size: uintptr_t) -> *c_char;
39 unsafe fn rust_upcall_free(ptr: *c_char);
42 unsafe fn rust_upcall_malloc_noswitch(td: *c_char,
47 unsafe fn rust_upcall_free_noswitch(ptr: *c_char);
52 pub fn fail_(expr: *c_char, file: *c_char, line: size_t) -> ! {
53 sys::begin_unwind_(expr, file, line);
56 #[lang="fail_bounds_check"]
57 pub fn fail_bounds_check(file: *c_char, line: size_t,
58 index: size_t, len: size_t) {
59 let msg = fmt!("index out of bounds: the len is %d but the index is %d",
60 len as int, index as int);
61 do str::as_buf(msg) |p, _len| {
62 fail_(p as *c_char, file, line);
66 pub fn fail_borrowed() {
68 do str::as_buf(msg) |msg_p, _| {
69 do str::as_buf("???") |file_p, _| {
70 fail_(msg_p as *c_char, file_p as *c_char, 0);
75 // FIXME #4942: Make these signatures agree with exchange_alloc's signatures
76 #[lang="exchange_malloc"]
78 pub unsafe fn exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char {
79 transmute(exchange_alloc::malloc(transmute(td), transmute(size)))
82 // NB: Calls to free CANNOT be allowed to fail, as throwing an exception from
83 // inside a landing pad may corrupt the state of the exception handler. If a
84 // problem occurs, call exit instead.
85 #[lang="exchange_free"]
87 pub unsafe fn exchange_free(ptr: *c_char) {
88 exchange_alloc::free(transmute(ptr))
93 #[cfg(stage0)] // For some reason this isn't working on windows in stage0
94 pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
95 return rustrt::rust_upcall_malloc_noswitch(td, size);
101 pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
104 return rustrt::rust_upcall_malloc_noswitch(td, size);
107 let mut alloc = ::ptr::null();
108 do borrow_local_services |srv| {
109 alloc = srv.heap.alloc(td as *c_void, size as uint) as *c_char;
116 // NB: Calls to free CANNOT be allowed to fail, as throwing an exception from
117 // inside a landing pad may corrupt the state of the exception handler. If a
118 // problem occurs, call exit instead.
122 pub unsafe fn local_free(ptr: *c_char) {
123 rustrt::rust_upcall_free_noswitch(ptr);
126 // NB: Calls to free CANNOT be allowed to fail, as throwing an exception from
127 // inside a landing pad may corrupt the state of the exception handler. If a
128 // problem occurs, call exit instead.
132 pub unsafe fn local_free(ptr: *c_char) {
135 rustrt::rust_upcall_free_noswitch(ptr);
138 do borrow_local_services |srv| {
139 srv.heap.free(ptr as *c_void);
145 #[lang="borrow_as_imm"]
147 pub unsafe fn borrow_as_imm(a: *u8) {
148 let a: *mut BoxRepr = transmute(a);
149 (*a).header.ref_count |= FROZEN_BIT;
152 #[lang="return_to_mut"]
154 pub unsafe fn return_to_mut(a: *u8) {
155 // Sometimes the box is null, if it is conditionally frozen.
158 let a: *mut BoxRepr = transmute(a);
159 (*a).header.ref_count &= !FROZEN_BIT;
163 #[lang="check_not_borrowed"]
165 pub unsafe fn check_not_borrowed(a: *u8) {
166 let a: *mut BoxRepr = transmute(a);
167 if ((*a).header.ref_count & FROZEN_BIT) != 0 {
172 #[lang="strdup_uniq"]
174 pub unsafe fn strdup_uniq(ptr: *c_uchar, len: uint) -> ~str {
175 str::raw::from_buf_len(ptr, len)
180 pub fn start(main: *u8, argc: int, argv: *c_char,
181 crate_map: *u8) -> int {
186 let use_old_rt = do str::as_c_str("RUST_NEWRT") |s| {
190 return rust_start(main as *c_void, argc as c_int, argv,
191 crate_map as *c_void) as int;
193 return start(main, argc, argv, crate_map);
198 fn rust_start(main: *c_void, argc: c_int, argv: *c_char,
199 crate_map: *c_void) -> c_int;
205 pub fn start(main: *u8, argc: int, argv: **c_char,
206 crate_map: *u8) -> int {
211 let use_old_rt = do str::as_c_str("RUST_NEWRT") |s| {
215 return rust_start(main as *c_void, argc as c_int, argv,
216 crate_map as *c_void) as int;
218 return start(main, argc, argv, crate_map);
223 fn rust_start(main: *c_void, argc: c_int, argv: **c_char,
224 crate_map: *c_void) -> c_int;
231 // indent-tabs-mode: nil
233 // buffer-file-coding-system: utf-8-unix