forked from haileys/rustboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zero.rs
305 lines (246 loc) · 6.61 KB
/
zero.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright (c) 2006-2009 Graydon Hoare
// Copyright (c) 2009-2013 Mozilla Foundation
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the
// Software without restriction, including without
// limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions
// of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// zero.rs
//
// Minimal definitions of the core primitives in Rust. Include this file with
// your project to create a freestanding Rust program that can run on bare
// metal.
//
#[allow(ctypes)];
// Built-in traits
#[lang="copy"]
pub trait Copy {}
#[lang="owned"]
pub trait Owned {}
#[lang="const"]
pub trait Const {}
#[lang="drop"]
pub trait Drop {
fn finalize(&self);
}
// Operator overloading
#[lang="eq"]
pub trait Eq {
fn eq(&self, other: &Self) -> bool;
fn ne(&self, other: &Self) -> bool;
}
#[lang="ord"]
pub trait Ord {
fn lt(&self, other: &Self) -> bool;
fn le(&self, other: &Self) -> bool;
fn ge(&self, other: &Self) -> bool;
fn gt(&self, other: &Self) -> bool;
}
#[lang="add"]
pub trait Add<Rhs,Result> {
fn add(&self, rhs: &Rhs) -> Result;
}
#[lang="sub"]
pub trait Sub<Rhs,Result> {
fn sub(&self, rhs: &Rhs) -> Result;
}
#[lang="mul"]
pub trait Mul<Rhs,Result> {
fn mul(&self, rhs: &Rhs) -> Result;
}
#[lang="div"]
pub trait Div<Rhs,Result> {
fn div(&self, rhs: &Rhs) -> Result;
}
#[lang="rem"]
pub trait Rem<Rhs,Result> {
fn rem(&self, rhs: &Rhs) -> Result;
}
#[lang="neg"]
pub trait Neg<Rhs,Result> {
fn neg(&self) -> Result;
}
#[lang="not"]
pub trait Not<Rhs,Result> {
fn not(&self) -> Result;
}
#[lang="bitand"]
pub trait BitAnd<Rhs,Result> {
fn bitand(&self, rhs: &Rhs) -> Result;
}
#[lang="bitor"]
pub trait BitOr<Rhs,Result> {
fn bitor(&self, rhs: &Rhs) -> Result;
}
#[lang="bitxor"]
pub trait BitXor<Rhs,Result> {
fn bitxor(&self, rhs: &Rhs) -> Result;
}
#[lang="shl"]
pub trait Shl<Rhs,Result> {
fn shl(&self, rhs: &Rhs) -> Result;
}
#[lang="shr"]
pub trait Shr<Rhs,Result> {
fn shr(&self, rhs: &Rhs) -> Result;
}
#[lang="index"]
pub trait Index<Index,Result> {
fn index(&self, rhs: &Index) -> Result;
}
// String utilities
#[lang="str_eq"]
pub fn str_eq(a: &str, b: &str) -> bool {
unsafe {
let (aptr, alen): (*u8, uint) = transmute(a);
let (bptr, blen): (*u8, uint) = transmute(b);
if alen != blen {
return false
}
memcmp(aptr, bptr, alen - 1) == 0
}
}
// FIXME(pcwalton): This function is legacy junk.
#[lang="uniq_str_eq"]
pub fn uniq_str_eq(a: &~str, b: &~str) -> bool {
str_eq(*a, *b)
}
struct StringRepr {
fill: uint,
alloc: uint,
}
// FIXME(pcwalton): This function should not be necessary, I don't think.
#[lang="strdup_uniq"]
pub unsafe fn strdup_uniq(ptr: *u8, len: uint) -> ~str {
let size = size_of::<StringRepr>() + len + 1;
let string: *mut StringRepr = transmute(exchange_malloc(transmute(0),
size));
(*string).fill = len + 1;
(*string).alloc = len + 1;
let mut data_ptr: uint = transmute(string);
data_ptr += size_of::<StringRepr>();
let data_ptr: *mut u8 = transmute(data_ptr);
memcpy(data_ptr, ptr, len + 1);
transmute(string)
}
// Legacy junk
#[lang="log_type"]
pub fn log_type<T>(_: u32, _: &T) {
// FIXME: This function should not be a lang item.
}
#[lang="annihilate"]
pub unsafe fn annihilate() {}
// Failure
#[lang="fail_"]
pub fn fail(_: *i8, _: *i8, _: uint) -> ! {
unsafe {
abort()
}
}
#[lang="fail_bounds_check"]
pub fn fail_bounds_check(_: *i8, _: uint, _: uint, _: uint) {
unsafe {
abort()
}
}
// Memory allocation
// FIXME: So grotesquely inefficient.
struct Header {
minus_one: uint, // Must be -1.
type_desc: *i8,
null_0: uint, // Must be null.
null_1: uint, // Must be null.
}
// FIXME: This is horrendously inefficient.
#[lang="exchange_malloc"]
pub unsafe fn exchange_malloc(type_desc: *i8, size: uint) -> *i8 {
let alloc: *mut Header = transmute(malloc(size_of::<Header>() + size));
(*alloc).minus_one = -1;
(*alloc).type_desc = type_desc;
(*alloc).null_0 = 0;
(*alloc).null_1 = 0;
transmute(alloc)
}
#[lang="exchange_free"]
pub unsafe fn exchange_free(alloc: *i8) {
free(transmute(alloc))
}
// Entry point
// TODO(pcwalton): Stash argc and argv somewhere. Probably needs to wait on
// global variables.
#[lang="start"]
pub fn start(main: *u8, _: int, _: **i8, _: *u8) -> int {
unsafe {
let main: extern "Rust" fn() = transmute(main);
main();
0
}
}
// The nonexistent garbage collector
#[lang="malloc"]
pub unsafe fn gc_malloc(_: *i8, _: uint) -> *i8 {
abort()
}
#[lang="free"]
pub unsafe fn gc_free(_: *i8) {
abort()
}
#[lang="borrow_as_imm"]
pub unsafe fn borrow_as_imm(_: *u8, _: *i8, _: uint) -> uint {
abort()
}
#[lang="borrow_as_mut"]
pub unsafe fn borrow_as_mut(_: *u8, _: *i8, _: uint) -> uint {
abort()
}
#[lang="record_borrow"]
pub unsafe fn record_borrow(_: *u8, _: uint, _: *i8, _: uint) {
abort()
}
#[lang="unrecord_borrow"]
pub unsafe fn unrecord_borrow(_: *u8, _: uint, _: *i8, _: uint) {
abort()
}
#[lang="return_to_mut"]
pub unsafe fn return_to_mut(_: *u8, _: uint, _: *i8, _: uint) {
abort()
}
#[lang="check_not_borrowed"]
pub unsafe fn check_not_borrowed(_: *u8, _: *i8, _: uint) {
abort()
}
// libc dependencies
extern {
#[fast_ffi]
pub fn malloc(size: uint) -> *u8;
#[fast_ffi]
pub fn free(ptr: *u8);
#[fast_ffi]
pub fn abort() -> !;
#[fast_ffi]
pub fn memcpy(dest: *mut u8, src: *u8, size: uint) -> *u8;
#[fast_ffi]
pub fn memcmp(a: *u8, b: *u8, size: uint) -> i32;
}
// Rust intrinsic dependencies
extern "rust-intrinsic" {
pub fn transmute<T,U>(val: T) -> U;
pub fn size_of<T>() -> uint;
}