]> git.lizzy.rs Git - rust.git/blob - src/libstd/unstable/lang.rs
956ffb82902de0addac7b55c8b0a33f9e22b2e7f
[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 c_str::ToCStr;
14 use cast::transmute;
15 use libc::{c_char, c_void, size_t, uintptr_t};
16 use option::{Some, None};
17 use sys;
18 use rt::task::Task;
19 use rt::local::Local;
20 use rt::borrowck;
21
22 #[lang="fail_"]
23 pub fn fail_(expr: *c_char, file: *c_char, line: size_t) -> ! {
24     sys::begin_unwind_(expr, file, line);
25 }
26
27 #[lang="fail_bounds_check"]
28 pub fn fail_bounds_check(file: *c_char, line: size_t,
29                          index: size_t, len: size_t) {
30     let msg = fmt!("index out of bounds: the len is %d but the index is %d",
31                     len as int, index as int);
32     do msg.to_c_str().with_ref |buf| {
33         fail_(buf, file, line);
34     }
35 }
36
37 #[lang="malloc"]
38 pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
39     // XXX: Unsafe borrow for speed. Lame.
40     match Local::try_unsafe_borrow::<Task>() {
41         Some(task) => {
42             (*task).heap.alloc(td as *c_void, size as uint) as *c_char
43         }
44         None => rtabort!("local malloc outside of task")
45     }
46 }
47
48 // NB: Calls to free CANNOT be allowed to fail, as throwing an exception from
49 // inside a landing pad may corrupt the state of the exception handler. If a
50 // problem occurs, call exit instead.
51 #[lang="free"]
52 pub unsafe fn local_free(ptr: *c_char) {
53     ::rt::local_heap::local_free(ptr);
54 }
55
56 #[lang="borrow_as_imm"]
57 #[inline]
58 pub unsafe fn borrow_as_imm(a: *u8, file: *c_char, line: size_t) -> uint {
59     borrowck::borrow_as_imm(a, file, line)
60 }
61
62 #[lang="borrow_as_mut"]
63 #[inline]
64 pub unsafe fn borrow_as_mut(a: *u8, file: *c_char, line: size_t) -> uint {
65     borrowck::borrow_as_mut(a, file, line)
66 }
67
68 #[lang="record_borrow"]
69 pub unsafe fn record_borrow(a: *u8, old_ref_count: uint,
70                             file: *c_char, line: size_t) {
71     borrowck::record_borrow(a, old_ref_count, file, line)
72 }
73
74 #[lang="unrecord_borrow"]
75 pub unsafe fn unrecord_borrow(a: *u8, old_ref_count: uint,
76                               file: *c_char, line: size_t) {
77     borrowck::unrecord_borrow(a, old_ref_count, file, line)
78 }
79
80 #[lang="return_to_mut"]
81 #[inline]
82 pub unsafe fn return_to_mut(a: *u8, orig_ref_count: uint,
83                             file: *c_char, line: size_t) {
84     borrowck::return_to_mut(a, orig_ref_count, file, line)
85 }
86
87 #[lang="check_not_borrowed"]
88 #[inline]
89 pub unsafe fn check_not_borrowed(a: *u8,
90                                  file: *c_char,
91                                  line: size_t) {
92     borrowck::check_not_borrowed(a, file, line)
93 }
94
95 #[lang="annihilate"]
96 pub unsafe fn annihilate() {
97     ::cleanup::annihilate()
98 }
99
100 #[lang="start"]
101 pub fn start(main: *u8, argc: int, argv: **c_char,
102              crate_map: *u8) -> int {
103     use rt;
104
105     unsafe {
106         return do rt::start(argc, argv as **u8, crate_map) {
107             let main: extern "Rust" fn() = transmute(main);
108             main();
109         };
110     }
111 }