]> git.lizzy.rs Git - rust.git/blob - src/libstd/unstable/lang.rs
Find the cratemap at runtime on windows.
[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::{Option, None, Some};
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.with_c_str |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     let task: Option<*mut Task> = Local::try_unsafe_borrow();
41     match task {
42         Some(task) => {
43             (*task).heap.alloc(td as *c_void, size as uint) as *c_char
44         }
45         None => rtabort!("local malloc outside of task")
46     }
47 }
48
49 // NB: Calls to free CANNOT be allowed to fail, as throwing an exception from
50 // inside a landing pad may corrupt the state of the exception handler. If a
51 // problem occurs, call exit instead.
52 #[lang="free"]
53 pub unsafe fn local_free(ptr: *c_char) {
54     ::rt::local_heap::local_free(ptr);
55 }
56
57 #[lang="borrow_as_imm"]
58 #[inline]
59 pub unsafe fn borrow_as_imm(a: *u8, file: *c_char, line: size_t) -> uint {
60     borrowck::borrow_as_imm(a, file, line)
61 }
62
63 #[lang="borrow_as_mut"]
64 #[inline]
65 pub unsafe fn borrow_as_mut(a: *u8, file: *c_char, line: size_t) -> uint {
66     borrowck::borrow_as_mut(a, file, line)
67 }
68
69 #[lang="record_borrow"]
70 pub unsafe fn record_borrow(a: *u8, old_ref_count: uint,
71                             file: *c_char, line: size_t) {
72     borrowck::record_borrow(a, old_ref_count, file, line)
73 }
74
75 #[lang="unrecord_borrow"]
76 pub unsafe fn unrecord_borrow(a: *u8, old_ref_count: uint,
77                               file: *c_char, line: size_t) {
78     borrowck::unrecord_borrow(a, old_ref_count, file, line)
79 }
80
81 #[lang="return_to_mut"]
82 #[inline]
83 pub unsafe fn return_to_mut(a: *u8, orig_ref_count: uint,
84                             file: *c_char, line: size_t) {
85     borrowck::return_to_mut(a, orig_ref_count, file, line)
86 }
87
88 #[lang="check_not_borrowed"]
89 #[inline]
90 pub unsafe fn check_not_borrowed(a: *u8,
91                                  file: *c_char,
92                                  line: size_t) {
93     borrowck::check_not_borrowed(a, file, line)
94 }
95
96 #[cfg(stage0)]
97 #[lang="start"]
98 pub fn start(main: *u8, argc: int, argv: **c_char,
99              crate_map: *u8) -> int {
100     use rt;
101
102     unsafe {
103         return do rt::start(argc, argv as **u8, crate_map) {
104             let main: extern "Rust" fn() = transmute(main);
105             main();
106         };
107     }
108 }
109
110 #[cfg(not(stage0))]
111 #[lang="start"]
112 pub fn start(main: *u8, argc: int, argv: **c_char) -> int {
113     use rt;
114
115     unsafe {
116         return do rt::start(argc, argv as **u8) {
117             let main: extern "Rust" fn() = transmute(main);
118             main();
119         };
120     }
121 }