(index<- ) ./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, size_t, uintptr_t};
16 use rt::task;
17 use rt::borrowck;
18
19 #[lang="fail_"]
20 pub fn fail_(expr: *c_char, file: *c_char, line: size_t) -> ! {
21 task::begin_unwind(expr, file, line);
22 }
23
24 #[lang="fail_bounds_check"]
25 pub fn fail_bounds_check(file: *c_char, line: size_t,
26 index: size_t, len: size_t) {
27 let msg = format!("index out of bounds: the len is {} but the index is {}",
28 len as uint, index as uint);
29 do msg.with_c_str |buf| {
30 fail_(buf, file, line);
31 }
32 }
33
34 #[lang="malloc"]
35 pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
36 ::rt::local_heap::local_malloc(td, size)
37 }
38
39 // NB: Calls to free CANNOT be allowed to fail, as throwing an exception from
40 // inside a landing pad may corrupt the state of the exception handler. If a
41 // problem occurs, call exit instead.
42 #[lang="free"]
43 pub unsafe fn local_free(ptr: *c_char) {
44 ::rt::local_heap::local_free(ptr);
45 }
46
47 #[lang="borrow_as_imm"]
48 #[inline]
49 pub unsafe fn borrow_as_imm(a: *u8, file: *c_char, line: size_t) -> uint {
50 borrowck::borrow_as_imm(a, file, line)
51 }
52
53 #[lang="borrow_as_mut"]
54 #[inline]
55 pub unsafe fn borrow_as_mut(a: *u8, file: *c_char, line: size_t) -> uint {
56 borrowck::borrow_as_mut(a, file, line)
57 }
58
59 #[lang="record_borrow"]
60 pub unsafe fn record_borrow(a: *u8, old_ref_count: uint,
61 file: *c_char, line: size_t) {
62 borrowck::record_borrow(a, old_ref_count, file, line)
63 }
64
65 #[lang="unrecord_borrow"]
66 pub unsafe fn unrecord_borrow(a: *u8, old_ref_count: uint,
67 file: *c_char, line: size_t) {
68 borrowck::unrecord_borrow(a, old_ref_count, file, line)
69 }
70
71 #[lang="return_to_mut"]
72 #[inline]
73 pub unsafe fn return_to_mut(a: *u8, orig_ref_count: uint,
74 file: *c_char, line: size_t) {
75 borrowck::return_to_mut(a, orig_ref_count, file, line)
76 }
77
78 #[lang="check_not_borrowed"]
79 #[inline]
80 pub unsafe fn check_not_borrowed(a: *u8,
81 file: *c_char,
82 line: size_t) {
83 borrowck::check_not_borrowed(a, file, line)
84 }
85
86 #[lang="start"]
87 pub fn start(main: *u8, argc: int, argv: **c_char) -> int {
88 use rt;
89
90 unsafe {
91 return do rt::start(argc, argv as **u8) {
92 let main: extern "Rust" fn() = transmute(main);
93 main();
94 };
95 }
96 }