]> git.lizzy.rs Git - rust.git/blob - src/libcore/unstable/lang.rs
Merge remote-tracking branch 'brson/io'
[rust.git] / src / libcore / unstable / lang.rs
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.
4 //
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.
10
11 //! Runtime calls emitted by the compiler.
12
13 use cast::transmute;
14 use libc::{c_char, c_uchar, c_void, size_t, uintptr_t, c_int};
15 use managed::raw::BoxRepr;
16 use str;
17 use sys;
18 use unstable::exchange_alloc;
19 use cast::transmute;
20 use rt::{context, OldTaskContext};
21 use rt::local_services::borrow_local_services;
22
23 #[allow(non_camel_case_types)]
24 pub type rust_task = c_void;
25
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;
30
31 pub mod rustrt {
32     use libc::{c_char, uintptr_t};
33
34     pub extern {
35         #[rust_stack]
36         unsafe fn rust_upcall_malloc(td: *c_char, size: uintptr_t) -> *c_char;
37
38         #[rust_stack]
39         unsafe fn rust_upcall_free(ptr: *c_char);
40
41         #[fast_ffi]
42         unsafe fn rust_upcall_malloc_noswitch(td: *c_char,
43                                               size: uintptr_t)
44                                            -> *c_char;
45
46         #[fast_ffi]
47         unsafe fn rust_upcall_free_noswitch(ptr: *c_char);
48     }
49 }
50
51 #[lang="fail_"]
52 pub fn fail_(expr: *c_char, file: *c_char, line: size_t) -> ! {
53     sys::begin_unwind_(expr, file, line);
54 }
55
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);
63     }
64 }
65
66 pub fn fail_borrowed() {
67     let msg = "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);
71         }
72     }
73 }
74
75 // FIXME #4942: Make these signatures agree with exchange_alloc's signatures
76 #[lang="exchange_malloc"]
77 #[inline(always)]
78 pub unsafe fn exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char {
79     transmute(exchange_alloc::malloc(transmute(td), transmute(size)))
80 }
81
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"]
86 #[inline(always)]
87 pub unsafe fn exchange_free(ptr: *c_char) {
88     exchange_alloc::free(transmute(ptr))
89 }
90
91 #[lang="malloc"]
92 #[inline(always)]
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);
96 }
97
98 #[lang="malloc"]
99 #[inline(always)]
100 #[cfg(not(stage0))]
101 pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
102     match context() {
103         OldTaskContext => {
104             return rustrt::rust_upcall_malloc_noswitch(td, size);
105         }
106         _ => {
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;
110             }
111             return alloc;
112         }
113     }
114 }
115
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.
119 #[lang="free"]
120 #[inline(always)]
121 #[cfg(stage0)]
122 pub unsafe fn local_free(ptr: *c_char) {
123     rustrt::rust_upcall_free_noswitch(ptr);
124 }
125
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.
129 #[lang="free"]
130 #[inline(always)]
131 #[cfg(not(stage0))]
132 pub unsafe fn local_free(ptr: *c_char) {
133     match context() {
134         OldTaskContext => {
135             rustrt::rust_upcall_free_noswitch(ptr);
136         }
137         _ => {
138             do borrow_local_services |srv| {
139                 srv.heap.free(ptr as *c_void);
140             }
141         }
142     }
143 }
144
145 #[lang="borrow_as_imm"]
146 #[inline(always)]
147 pub unsafe fn borrow_as_imm(a: *u8) {
148     let a: *mut BoxRepr = transmute(a);
149     (*a).header.ref_count |= FROZEN_BIT;
150 }
151
152 #[lang="return_to_mut"]
153 #[inline(always)]
154 pub unsafe fn return_to_mut(a: *u8) {
155     // Sometimes the box is null, if it is conditionally frozen.
156     // See e.g. #4904.
157     if !a.is_null() {
158         let a: *mut BoxRepr = transmute(a);
159         (*a).header.ref_count &= !FROZEN_BIT;
160     }
161 }
162
163 #[lang="check_not_borrowed"]
164 #[inline(always)]
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 {
168         fail_borrowed();
169     }
170 }
171
172 #[lang="strdup_uniq"]
173 #[inline(always)]
174 pub unsafe fn strdup_uniq(ptr: *c_uchar, len: uint) -> ~str {
175     str::raw::from_buf_len(ptr, len)
176 }
177
178 #[lang="start"]
179 #[cfg(stage0)]
180 pub fn start(main: *u8, argc: int, argv: *c_char,
181              crate_map: *u8) -> int {
182     use libc::getenv;
183     use rt::start;
184
185     unsafe {
186         let use_old_rt = do str::as_c_str("RUST_NEWRT") |s| {
187             getenv(s).is_null()
188         };
189         if use_old_rt {
190             return rust_start(main as *c_void, argc as c_int, argv,
191                               crate_map as *c_void) as int;
192         } else {
193             return start(main, argc, argv, crate_map);
194         }
195     }
196
197     extern {
198         fn rust_start(main: *c_void, argc: c_int, argv: *c_char,
199                       crate_map: *c_void) -> c_int;
200     }
201 }
202
203 #[lang="start"]
204 #[cfg(not(stage0))]
205 pub fn start(main: *u8, argc: int, argv: **c_char,
206              crate_map: *u8) -> int {
207     use libc::getenv;
208     use rt::start;
209
210     unsafe {
211         let use_old_rt = do str::as_c_str("RUST_NEWRT") |s| {
212             getenv(s).is_null()
213         };
214         if use_old_rt {
215             return rust_start(main as *c_void, argc as c_int, argv,
216                               crate_map as *c_void) as int;
217         } else {
218             return start(main, argc, argv, crate_map);
219         }
220     }
221
222     extern {
223         fn rust_start(main: *c_void, argc: c_int, argv: **c_char,
224                       crate_map: *c_void) -> c_int;
225     }
226 }
227
228 // Local Variables:
229 // mode: rust;
230 // fill-column: 78;
231 // indent-tabs-mode: nil
232 // c-basic-offset: 4
233 // buffer-file-coding-system: utf-8-unix
234 // End: