]> git.lizzy.rs Git - rust.git/blob - src/libstd/unstable/lang.rs
std: move str::as_buf into StrSlice
[rust.git] / src / libstd / 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 str;
16 use sys;
17 use rt::{context, OldTaskContext};
18 use rt::task::Task;
19 use rt::local::Local;
20 use rt::borrowck;
21
22 #[allow(non_camel_case_types)]
23 pub type rust_task = c_void;
24
25 pub mod rustrt {
26     use unstable::lang::rust_task;
27     use libc::{c_char, uintptr_t};
28
29     extern {
30         #[rust_stack]
31         pub unsafe fn rust_upcall_malloc(td: *c_char, size: uintptr_t)
32                                          -> *c_char;
33
34         #[rust_stack]
35         pub unsafe fn rust_upcall_free(ptr: *c_char);
36
37         #[fast_ffi]
38         pub unsafe fn rust_upcall_malloc_noswitch(td: *c_char,
39                                                   size: uintptr_t)
40                                                   -> *c_char;
41
42         #[rust_stack]
43         pub fn rust_try_get_task() -> *rust_task;
44
45         pub fn rust_dbg_breakpoint();
46     }
47 }
48
49 #[lang="fail_"]
50 pub fn fail_(expr: *c_char, file: *c_char, line: size_t) -> ! {
51     sys::begin_unwind_(expr, file, line);
52 }
53
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);
61     }
62 }
63
64 #[lang="malloc"]
65 pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
66     match context() {
67         OldTaskContext => {
68             return rustrt::rust_upcall_malloc_noswitch(td, size);
69         }
70         _ => {
71             let mut alloc = ::ptr::null();
72             do Local::borrow::<Task,()> |task| {
73                 rtdebug!("task pointer: %x, heap pointer: %x",
74                          to_uint(task),
75                          to_uint(&task.heap));
76                 alloc = task.heap.alloc(td as *c_void, size as uint) as *c_char;
77             }
78             return alloc;
79         }
80     }
81 }
82
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.
86 #[lang="free"]
87 pub unsafe fn local_free(ptr: *c_char) {
88     ::rt::local_heap::local_free(ptr);
89 }
90
91 #[lang="borrow_as_imm"]
92 #[inline]
93 pub unsafe fn borrow_as_imm(a: *u8, file: *c_char, line: size_t) -> uint {
94     borrowck::borrow_as_imm(a, file, line)
95 }
96
97 #[lang="borrow_as_mut"]
98 #[inline]
99 pub unsafe fn borrow_as_mut(a: *u8, file: *c_char, line: size_t) -> uint {
100     borrowck::borrow_as_mut(a, file, line)
101 }
102
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)
107 }
108
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)
113 }
114
115 #[lang="return_to_mut"]
116 #[inline]
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)
120 }
121
122 #[lang="check_not_borrowed"]
123 #[inline]
124 pub unsafe fn check_not_borrowed(a: *u8,
125                                  file: *c_char,
126                                  line: size_t) {
127     borrowck::check_not_borrowed(a, file, line)
128 }
129
130 #[lang="strdup_uniq"]
131 #[inline]
132 pub unsafe fn strdup_uniq(ptr: *c_uchar, len: uint) -> ~str {
133     str::raw::from_buf_len(ptr, len)
134 }
135
136 #[lang="annihilate"]
137 pub unsafe fn annihilate() {
138     ::cleanup::annihilate()
139 }
140
141 #[lang="start"]
142 pub fn start(main: *u8, argc: int, argv: **c_char,
143              crate_map: *u8) -> int {
144     use rt;
145     use os;
146
147     unsafe {
148         let use_old_rt = os::getenv("RUST_NEWRT").is_none();
149         if use_old_rt {
150             return rust_start(main as *c_void, argc as c_int, argv,
151                               crate_map as *c_void) as int;
152         } else {
153             return do rt::start(argc, argv as **u8, crate_map) {
154                 let main: extern "Rust" fn() = transmute(main);
155                 main();
156             };
157         }
158     }
159
160     extern {
161         fn rust_start(main: *c_void, argc: c_int, argv: **c_char,
162                       crate_map: *c_void) -> c_int;
163     }
164 }