]> git.lizzy.rs Git - rust.git/blob - example/mini_core.rs
Implement bswap intrinsic
[rust.git] / example / mini_core.rs
1 #![feature(no_core, lang_items, intrinsics, unboxed_closures, type_ascription, extern_types)]
2 #![no_core]
3 #![allow(dead_code)]
4
5 #[lang = "sized"]
6 pub trait Sized {}
7
8 #[lang = "unsize"]
9 pub trait Unsize<T: ?Sized> {}
10
11 #[lang = "coerce_unsized"]
12 pub trait CoerceUnsized<T> {}
13
14 impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
15 impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {}
16 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
17 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
18
19 #[lang = "dispatch_from_dyn"]
20 pub trait DispatchFromDyn<T> {}
21
22 // &T -> &U
23 impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a U> for &'a T {}
24 // &mut T -> &mut U
25 impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a mut U> for &'a mut T {}
26 // *const T -> *const U
27 impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*const U> for *const T {}
28 // *mut T -> *mut U
29 impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*mut U> for *mut T {}
30 impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Box<U>> for Box<T> {}
31
32 #[lang = "receiver"]
33 pub trait Receiver {}
34
35 impl<T: ?Sized> Receiver for &T {}
36 impl<T: ?Sized> Receiver for &mut T {}
37 impl<T: ?Sized> Receiver for Box<T> {}
38
39 #[lang = "copy"]
40 pub unsafe trait Copy {}
41
42 unsafe impl Copy for bool {}
43 unsafe impl Copy for u8 {}
44 unsafe impl Copy for u16 {}
45 unsafe impl Copy for u32 {}
46 unsafe impl Copy for u64 {}
47 unsafe impl Copy for usize {}
48 unsafe impl Copy for i8 {}
49 unsafe impl Copy for i16 {}
50 unsafe impl Copy for i32 {}
51 unsafe impl Copy for isize {}
52 unsafe impl Copy for char {}
53 unsafe impl<'a, T: ?Sized> Copy for &'a T {}
54 unsafe impl<T: ?Sized> Copy for *const T {}
55 unsafe impl<T: ?Sized> Copy for *mut T {}
56
57 #[lang = "sync"]
58 pub unsafe trait Sync {}
59
60 unsafe impl Sync for bool {}
61 unsafe impl Sync for u8 {}
62 unsafe impl Sync for u16 {}
63 unsafe impl Sync for u32 {}
64 unsafe impl Sync for u64 {}
65 unsafe impl Sync for usize {}
66 unsafe impl Sync for i8 {}
67 unsafe impl Sync for i16 {}
68 unsafe impl Sync for i32 {}
69 unsafe impl Sync for isize {}
70 unsafe impl Sync for char {}
71 unsafe impl<'a, T: ?Sized> Sync for &'a T {}
72 unsafe impl Sync for [u8; 16] {}
73
74 #[lang = "freeze"]
75 trait Freeze {}
76
77 #[lang = "not"]
78 pub trait Not {
79     type Output;
80
81     fn not(self) -> Self::Output;
82 }
83
84 impl Not for bool {
85     type Output = bool;
86
87     fn not(self) -> bool {
88         !self
89     }
90 }
91
92 #[lang = "mul"]
93 pub trait Mul<RHS = Self> {
94     type Output;
95
96     #[must_use]
97     fn mul(self, rhs: RHS) -> Self::Output;
98 }
99
100 impl Mul for u8 {
101     type Output = Self;
102
103     fn mul(self, rhs: Self) -> Self::Output {
104         self * rhs
105     }
106 }
107
108 impl Mul for usize {
109     type Output = Self;
110
111     fn mul(self, rhs: Self) -> Self::Output {
112         self * rhs
113     }
114 }
115
116 #[lang = "add"]
117 pub trait Add<RHS = Self> {
118     type Output;
119
120     fn add(self, rhs: RHS) -> Self::Output;
121 }
122
123 impl Add for u8 {
124     type Output = Self;
125
126     fn add(self, rhs: Self) -> Self {
127         self + rhs
128     }
129 }
130
131 impl Add for usize {
132     type Output = Self;
133
134     fn add(self, rhs: Self) -> Self {
135         self + rhs
136     }
137 }
138
139 #[lang = "sub"]
140 pub trait Sub<RHS = Self> {
141     type Output;
142
143     fn sub(self, rhs: RHS) -> Self::Output;
144 }
145
146 impl Sub for usize {
147     type Output = Self;
148
149     fn sub(self, rhs: Self) -> Self {
150         self - rhs
151     }
152 }
153
154 #[lang = "bitor"]
155 pub trait BitOr<RHS = Self> {
156     type Output;
157
158     #[must_use]
159     fn bitor(self, rhs: RHS) -> Self::Output;
160 }
161
162 impl BitOr for bool {
163     type Output = bool;
164
165     fn bitor(self, rhs: bool) -> bool {
166         self | rhs
167     }
168 }
169
170 impl<'a> BitOr<bool> for &'a bool {
171     type Output = bool;
172
173     fn bitor(self, rhs: bool) -> bool {
174         *self | rhs
175     }
176 }
177
178 #[lang = "eq"]
179 pub trait PartialEq<Rhs: ?Sized = Self> {
180     fn eq(&self, other: &Rhs) -> bool;
181     fn ne(&self, other: &Rhs) -> bool;
182 }
183
184 impl PartialEq for u8 {
185     fn eq(&self, other: &u8) -> bool {
186         (*self) == (*other)
187     }
188     fn ne(&self, other: &u8) -> bool {
189         (*self) != (*other)
190     }
191 }
192
193 impl PartialEq for u16 {
194     fn eq(&self, other: &u16) -> bool {
195         (*self) == (*other)
196     }
197     fn ne(&self, other: &u16) -> bool {
198         (*self) != (*other)
199     }
200 }
201
202 impl PartialEq for u32 {
203     fn eq(&self, other: &u32) -> bool {
204         (*self) == (*other)
205     }
206     fn ne(&self, other: &u32) -> bool {
207         (*self) != (*other)
208     }
209 }
210
211
212 impl PartialEq for u64 {
213     fn eq(&self, other: &u64) -> bool {
214         (*self) == (*other)
215     }
216     fn ne(&self, other: &u64) -> bool {
217         (*self) != (*other)
218     }
219 }
220
221 impl PartialEq for usize {
222     fn eq(&self, other: &usize) -> bool {
223         (*self) == (*other)
224     }
225     fn ne(&self, other: &usize) -> bool {
226         (*self) != (*other)
227     }
228 }
229
230 impl PartialEq for i32 {
231     fn eq(&self, other: &i32) -> bool {
232         (*self) == (*other)
233     }
234     fn ne(&self, other: &i32) -> bool {
235         (*self) != (*other)
236     }
237 }
238
239 impl PartialEq for isize {
240     fn eq(&self, other: &isize) -> bool {
241         (*self) == (*other)
242     }
243     fn ne(&self, other: &isize) -> bool {
244         (*self) != (*other)
245     }
246 }
247
248 impl PartialEq for char {
249     fn eq(&self, other: &char) -> bool {
250         (*self) == (*other)
251     }
252     fn ne(&self, other: &char) -> bool {
253         (*self) != (*other)
254     }
255 }
256
257 impl<T: ?Sized> PartialEq for *const T {
258     fn eq(&self, other: &*const T) -> bool {
259         *self == *other
260     }
261     fn ne(&self, other: &*const T) -> bool {
262         *self != *other
263     }
264 }
265
266 #[lang = "neg"]
267 pub trait Neg {
268     type Output;
269
270     fn neg(self) -> Self::Output;
271 }
272
273 impl Neg for isize {
274     type Output = isize;
275
276     fn neg(self) -> isize {
277         -self
278     }
279 }
280
281 pub enum Option<T> {
282     Some(T),
283     None,
284 }
285
286 pub use Option::*;
287
288 #[lang = "phantom_data"]
289 pub struct PhantomData<T: ?Sized>;
290
291 #[lang = "fn_once"]
292 #[rustc_paren_sugar]
293 pub trait FnOnce<Args> {
294     type Output;
295
296     extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
297 }
298
299 #[lang = "fn_mut"]
300 #[rustc_paren_sugar]
301 pub trait FnMut<Args>: FnOnce<Args> {
302     extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
303 }
304
305 #[lang = "panic"]
306 // Make it available to jited mini_core_hello_world
307 // FIXME remove next line when jit supports linking rlibs
308 #[inline(always)]
309 pub fn panic(&(_msg, _file, _line, _col): &(&'static str, &'static str, u32, u32)) -> ! {
310     unsafe {
311         libc::puts("Panicking\0" as *const str as *const u8);
312         intrinsics::abort();
313     }
314 }
315
316 #[lang = "eh_personality"]
317 fn eh_personality() -> ! {
318     loop {}
319 }
320
321 #[lang = "drop_in_place"]
322 #[allow(unconditional_recursion)]
323 pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
324     // Code here does not matter - this is replaced by the
325     // real drop glue by the compiler.
326     drop_in_place(to_drop);
327 }
328
329 #[lang = "deref"]
330 pub trait Deref {
331     type Target: ?Sized;
332
333     fn deref(&self) -> &Self::Target;
334 }
335
336 #[lang = "owned_box"]
337 pub struct Box<T: ?Sized>(*mut T);
338
339 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
340
341 impl<T: ?Sized> Drop for Box<T> {
342     fn drop(&mut self) {
343         // drop is currently performed by compiler.
344     }
345 }
346
347 impl<T> Deref for Box<T> {
348     type Target = T;
349
350     fn deref(&self) -> &Self::Target {
351         &**self
352     }
353 }
354
355 #[lang = "exchange_malloc"]
356 // Make it available to jited mini_core_hello_world
357 // FIXME remove next line when jit supports linking rlibs
358 #[inline(always)]
359 unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
360     libc::malloc(size)
361 }
362
363 #[lang = "box_free"]
364 #[inline(always)]
365 unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
366     libc::free(ptr as *mut u8);
367 }
368
369 #[lang = "drop"]
370 pub trait Drop {
371     fn drop(&mut self);
372 }
373
374 pub mod intrinsics {
375     extern "rust-intrinsic" {
376         pub fn abort() -> !;
377         pub fn size_of<T>() -> usize;
378         pub fn size_of_val<T: ?::Sized>(val: &T) -> usize;
379         pub fn min_align_of<T>() -> usize;
380         pub fn min_align_of_val<T: ?::Sized>(val: &T) -> usize;
381         pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
382         pub fn transmute<T, U>(e: T) -> U;
383         pub fn uninit<T>() -> T;
384         pub fn init<T>() -> T;
385         pub fn ctlz_nonzero<T>(x: T) -> T;
386         pub fn needs_drop<T>() -> bool;
387         pub fn bitreverse<T>(x: T) -> T;
388         pub fn bswap<T>(x: T) -> T;
389     }
390 }
391
392 pub mod libc {
393     #[link(name = "c")]
394     extern "C" {
395         pub fn puts(s: *const u8);
396         pub fn printf(format: *const i8, ...) -> i32;
397         pub fn malloc(size: usize) -> *mut u8;
398         pub fn free(ptr: *mut u8);
399         pub fn memcpy(dst: *mut u8, src: *const u8, size: usize);
400         pub fn memmove(dst: *mut u8, src: *const u8, size: usize);
401         pub fn strncpy(dst: *mut u8, src: *const u8, size: usize);
402     }
403 }
404
405 #[lang = "index"]
406 pub trait Index<Idx: ?Sized> {
407     type Output: ?Sized;
408     fn index(&self, index: Idx) -> &Self::Output;
409 }
410
411 impl<T> Index<usize> for [T; 3] {
412     type Output = T;
413
414     fn index(&self, index: usize) -> &Self::Output {
415         &self[index]
416     }
417 }
418
419 impl<T> Index<usize> for [T] {
420     type Output = T;
421
422     fn index(&self, index: usize) -> &Self::Output {
423         &self[index]
424     }
425 }
426
427 extern {
428     type VaListImpl;
429 }
430
431 #[lang = "va_list"]
432 #[repr(transparent)]
433 pub struct VaList<'a>(&'a mut VaListImpl);