]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_codegen_cranelift/example/mini_core.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / compiler / rustc_codegen_cranelift / example / mini_core.rs
CommitLineData
29967ef6 1#![feature(
5e7ed085
FG
2 no_core,
3 lang_items,
4 intrinsics,
5 unboxed_closures,
6 extern_types,
7 decl_macro,
8 rustc_attrs,
9 transparent_unions,
10 auto_traits,
11 thread_local
29967ef6
XL
12)]
13#![no_core]
14#![allow(dead_code)]
15
16#[lang = "sized"]
17pub trait Sized {}
18
04454e1e
FG
19#[lang = "destruct"]
20pub trait Destruct {}
21
9c376795
FG
22#[lang = "tuple_trait"]
23pub trait Tuple {}
24
29967ef6
XL
25#[lang = "unsize"]
26pub trait Unsize<T: ?Sized> {}
27
28#[lang = "coerce_unsized"]
29pub trait CoerceUnsized<T> {}
30
31impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
32impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {}
33impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
34impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
35
36#[lang = "dispatch_from_dyn"]
37pub trait DispatchFromDyn<T> {}
38
39// &T -> &U
40impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a U> for &'a T {}
41// &mut T -> &mut U
42impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a mut U> for &'a mut T {}
43// *const T -> *const U
44impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*const U> for *const T {}
45// *mut T -> *mut U
46impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*mut U> for *mut T {}
47impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Box<U>> for Box<T> {}
48
49#[lang = "receiver"]
50pub trait Receiver {}
51
52impl<T: ?Sized> Receiver for &T {}
53impl<T: ?Sized> Receiver for &mut T {}
54impl<T: ?Sized> Receiver for Box<T> {}
55
56#[lang = "copy"]
57pub unsafe trait Copy {}
58
59unsafe impl Copy for bool {}
60unsafe impl Copy for u8 {}
61unsafe impl Copy for u16 {}
62unsafe impl Copy for u32 {}
63unsafe impl Copy for u64 {}
64unsafe impl Copy for u128 {}
65unsafe impl Copy for usize {}
66unsafe impl Copy for i8 {}
67unsafe impl Copy for i16 {}
68unsafe impl Copy for i32 {}
69unsafe impl Copy for isize {}
70unsafe impl Copy for f32 {}
5e7ed085 71unsafe impl Copy for f64 {}
29967ef6
XL
72unsafe impl Copy for char {}
73unsafe impl<'a, T: ?Sized> Copy for &'a T {}
74unsafe impl<T: ?Sized> Copy for *const T {}
75unsafe impl<T: ?Sized> Copy for *mut T {}
76unsafe impl<T: Copy> Copy for Option<T> {}
77
78#[lang = "sync"]
79pub unsafe trait Sync {}
80
81unsafe impl Sync for bool {}
82unsafe impl Sync for u8 {}
83unsafe impl Sync for u16 {}
84unsafe impl Sync for u32 {}
85unsafe impl Sync for u64 {}
86unsafe impl Sync for usize {}
87unsafe impl Sync for i8 {}
88unsafe impl Sync for i16 {}
89unsafe impl Sync for i32 {}
90unsafe impl Sync for isize {}
91unsafe impl Sync for char {}
92unsafe impl<'a, T: ?Sized> Sync for &'a T {}
93unsafe impl Sync for [u8; 16] {}
94
95#[lang = "freeze"]
96unsafe auto trait Freeze {}
97
98unsafe impl<T: ?Sized> Freeze for PhantomData<T> {}
99unsafe impl<T: ?Sized> Freeze for *const T {}
100unsafe impl<T: ?Sized> Freeze for *mut T {}
101unsafe impl<T: ?Sized> Freeze for &T {}
102unsafe impl<T: ?Sized> Freeze for &mut T {}
103
104#[lang = "structural_peq"]
105pub trait StructuralPartialEq {}
106
107#[lang = "structural_teq"]
108pub trait StructuralEq {}
109
110#[lang = "not"]
111pub trait Not {
112 type Output;
113
114 fn not(self) -> Self::Output;
115}
116
117impl Not for bool {
118 type Output = bool;
119
120 fn not(self) -> bool {
121 !self
122 }
123}
124
125#[lang = "mul"]
126pub trait Mul<RHS = Self> {
127 type Output;
128
129 #[must_use]
130 fn mul(self, rhs: RHS) -> Self::Output;
131}
132
133impl Mul for u8 {
134 type Output = Self;
135
136 fn mul(self, rhs: Self) -> Self::Output {
137 self * rhs
138 }
139}
140
141impl Mul for usize {
142 type Output = Self;
143
144 fn mul(self, rhs: Self) -> Self::Output {
145 self * rhs
146 }
147}
148
149#[lang = "add"]
150pub trait Add<RHS = Self> {
151 type Output;
152
153 fn add(self, rhs: RHS) -> Self::Output;
154}
155
156impl Add for u8 {
157 type Output = Self;
158
159 fn add(self, rhs: Self) -> Self {
160 self + rhs
161 }
162}
163
164impl Add for i8 {
165 type Output = Self;
166
167 fn add(self, rhs: Self) -> Self {
168 self + rhs
169 }
170}
171
172impl Add for usize {
173 type Output = Self;
174
175 fn add(self, rhs: Self) -> Self {
176 self + rhs
177 }
178}
179
180#[lang = "sub"]
181pub trait Sub<RHS = Self> {
182 type Output;
183
184 fn sub(self, rhs: RHS) -> Self::Output;
185}
186
187impl Sub for usize {
188 type Output = Self;
189
190 fn sub(self, rhs: Self) -> Self {
191 self - rhs
192 }
193}
194
195impl Sub for u8 {
196 type Output = Self;
197
198 fn sub(self, rhs: Self) -> Self {
199 self - rhs
200 }
201}
202
203impl Sub for i8 {
204 type Output = Self;
205
206 fn sub(self, rhs: Self) -> Self {
207 self - rhs
208 }
209}
210
211impl Sub for i16 {
212 type Output = Self;
213
214 fn sub(self, rhs: Self) -> Self {
215 self - rhs
216 }
217}
218
219#[lang = "rem"]
220pub trait Rem<RHS = Self> {
221 type Output;
222
223 fn rem(self, rhs: RHS) -> Self::Output;
224}
225
226impl Rem for usize {
227 type Output = Self;
228
229 fn rem(self, rhs: Self) -> Self {
230 self % rhs
231 }
232}
233
234#[lang = "bitor"]
235pub trait BitOr<RHS = Self> {
236 type Output;
237
238 #[must_use]
239 fn bitor(self, rhs: RHS) -> Self::Output;
240}
241
242impl BitOr for bool {
243 type Output = bool;
244
245 fn bitor(self, rhs: bool) -> bool {
246 self | rhs
247 }
248}
249
250impl<'a> BitOr<bool> for &'a bool {
251 type Output = bool;
252
253 fn bitor(self, rhs: bool) -> bool {
254 *self | rhs
255 }
256}
257
258#[lang = "eq"]
259pub trait PartialEq<Rhs: ?Sized = Self> {
260 fn eq(&self, other: &Rhs) -> bool;
261 fn ne(&self, other: &Rhs) -> bool;
262}
263
264impl PartialEq for u8 {
265 fn eq(&self, other: &u8) -> bool {
266 (*self) == (*other)
267 }
268 fn ne(&self, other: &u8) -> bool {
269 (*self) != (*other)
270 }
271}
272
273impl PartialEq for u16 {
274 fn eq(&self, other: &u16) -> bool {
275 (*self) == (*other)
276 }
277 fn ne(&self, other: &u16) -> bool {
278 (*self) != (*other)
279 }
280}
281
282impl PartialEq for u32 {
283 fn eq(&self, other: &u32) -> bool {
284 (*self) == (*other)
285 }
286 fn ne(&self, other: &u32) -> bool {
287 (*self) != (*other)
288 }
289}
290
291
292impl PartialEq for u64 {
293 fn eq(&self, other: &u64) -> bool {
294 (*self) == (*other)
295 }
296 fn ne(&self, other: &u64) -> bool {
297 (*self) != (*other)
298 }
299}
300
301impl PartialEq for u128 {
302 fn eq(&self, other: &u128) -> bool {
303 (*self) == (*other)
304 }
305 fn ne(&self, other: &u128) -> bool {
306 (*self) != (*other)
307 }
308}
309
310impl PartialEq for usize {
311 fn eq(&self, other: &usize) -> bool {
312 (*self) == (*other)
313 }
314 fn ne(&self, other: &usize) -> bool {
315 (*self) != (*other)
316 }
317}
318
319impl PartialEq for i8 {
320 fn eq(&self, other: &i8) -> bool {
321 (*self) == (*other)
322 }
323 fn ne(&self, other: &i8) -> bool {
324 (*self) != (*other)
325 }
326}
327
328impl PartialEq for i32 {
329 fn eq(&self, other: &i32) -> bool {
330 (*self) == (*other)
331 }
332 fn ne(&self, other: &i32) -> bool {
333 (*self) != (*other)
334 }
335}
336
337impl PartialEq for isize {
338 fn eq(&self, other: &isize) -> bool {
339 (*self) == (*other)
340 }
341 fn ne(&self, other: &isize) -> bool {
342 (*self) != (*other)
343 }
344}
345
346impl PartialEq for char {
347 fn eq(&self, other: &char) -> bool {
348 (*self) == (*other)
349 }
350 fn ne(&self, other: &char) -> bool {
351 (*self) != (*other)
352 }
353}
354
355impl<T: ?Sized> PartialEq for *const T {
356 fn eq(&self, other: &*const T) -> bool {
357 *self == *other
358 }
359 fn ne(&self, other: &*const T) -> bool {
360 *self != *other
361 }
362}
363
364impl <T: PartialEq> PartialEq for Option<T> {
365 fn eq(&self, other: &Self) -> bool {
366 match (self, other) {
367 (Some(lhs), Some(rhs)) => *lhs == *rhs,
368 (None, None) => true,
369 _ => false,
370 }
371 }
372
373 fn ne(&self, other: &Self) -> bool {
374 match (self, other) {
375 (Some(lhs), Some(rhs)) => *lhs != *rhs,
376 (None, None) => false,
377 _ => true,
378 }
379 }
380}
381
6a06907d
XL
382#[lang = "shl"]
383pub trait Shl<RHS = Self> {
384 type Output;
385
386 #[must_use]
387 fn shl(self, rhs: RHS) -> Self::Output;
388}
389
390impl Shl for u128 {
391 type Output = u128;
392
393 fn shl(self, rhs: u128) -> u128 {
394 self << rhs
395 }
396}
397
29967ef6
XL
398#[lang = "neg"]
399pub trait Neg {
400 type Output;
401
402 fn neg(self) -> Self::Output;
403}
404
405impl Neg for i8 {
406 type Output = i8;
407
408 fn neg(self) -> i8 {
409 -self
410 }
411}
412
413impl Neg for i16 {
414 type Output = i16;
415
416 fn neg(self) -> i16 {
417 self
418 }
419}
420
421impl Neg for isize {
422 type Output = isize;
423
424 fn neg(self) -> isize {
425 -self
426 }
427}
428
429impl Neg for f32 {
430 type Output = f32;
431
432 fn neg(self) -> f32 {
433 -self
434 }
435}
436
437pub enum Option<T> {
438 Some(T),
439 None,
440}
441
442pub use Option::*;
443
444#[lang = "phantom_data"]
445pub struct PhantomData<T: ?Sized>;
446
447#[lang = "fn_once"]
448#[rustc_paren_sugar]
9c376795 449pub trait FnOnce<Args: Tuple> {
29967ef6
XL
450 #[lang = "fn_once_output"]
451 type Output;
452
453 extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
454}
455
456#[lang = "fn_mut"]
457#[rustc_paren_sugar]
9c376795 458pub trait FnMut<Args: Tuple>: FnOnce<Args> {
29967ef6
XL
459 extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
460}
461
462#[lang = "panic"]
463#[track_caller]
064997fb 464pub fn panic(_msg: &'static str) -> ! {
29967ef6
XL
465 unsafe {
466 libc::puts("Panicking\n\0" as *const str as *const i8);
467 intrinsics::abort();
468 }
469}
470
471#[lang = "panic_bounds_check"]
472#[track_caller]
473fn panic_bounds_check(index: usize, len: usize) -> ! {
474 unsafe {
475 libc::printf("index out of bounds: the len is %d but the index is %d\n\0" as *const str as *const i8, len, index);
476 intrinsics::abort();
477 }
478}
479
480#[lang = "eh_personality"]
481fn eh_personality() -> ! {
482 loop {}
483}
484
485#[lang = "drop_in_place"]
486#[allow(unconditional_recursion)]
487pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
488 // Code here does not matter - this is replaced by the
489 // real drop glue by the compiler.
490 drop_in_place(to_drop);
491}
492
493#[lang = "deref"]
494pub trait Deref {
495 type Target: ?Sized;
496
497 fn deref(&self) -> &Self::Target;
498}
499
04454e1e
FG
500#[repr(transparent)]
501#[rustc_layout_scalar_valid_range_start(1)]
502#[rustc_nonnull_optimization_guaranteed]
064997fb 503pub struct NonNull<T: ?Sized>(pub *const T);
04454e1e
FG
504
505impl<T: ?Sized, U: ?Sized> CoerceUnsized<NonNull<U>> for NonNull<T> where T: Unsize<U> {}
506impl<T: ?Sized, U: ?Sized> DispatchFromDyn<NonNull<U>> for NonNull<T> where T: Unsize<U> {}
507
5e7ed085 508pub struct Unique<T: ?Sized> {
04454e1e 509 pub pointer: NonNull<T>,
5e7ed085
FG
510 pub _marker: PhantomData<T>,
511}
512
513impl<T: ?Sized, U: ?Sized> CoerceUnsized<Unique<U>> for Unique<T> where T: Unsize<U> {}
5e7ed085
FG
514impl<T: ?Sized, U: ?Sized> DispatchFromDyn<Unique<U>> for Unique<T> where T: Unsize<U> {}
515
29967ef6 516#[lang = "owned_box"]
5e7ed085 517pub struct Box<T: ?Sized>(Unique<T>, ());
29967ef6
XL
518
519impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
520
521impl<T: ?Sized> Drop for Box<T> {
522 fn drop(&mut self) {
523 // drop is currently performed by compiler.
524 }
525}
526
064997fb 527impl<T: ?Sized> Deref for Box<T> {
29967ef6
XL
528 type Target = T;
529
530 fn deref(&self) -> &Self::Target {
531 &**self
532 }
533}
534
535#[lang = "exchange_malloc"]
536unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
537 libc::malloc(size)
538}
539
540#[lang = "box_free"]
f2b60f7d 541unsafe fn box_free<T: ?Sized>(ptr: Unique<T>, _alloc: ()) {
04454e1e 542 libc::free(ptr.pointer.0 as *mut u8);
29967ef6
XL
543}
544
545#[lang = "drop"]
546pub trait Drop {
547 fn drop(&mut self);
548}
549
550#[lang = "manually_drop"]
551#[repr(transparent)]
552pub struct ManuallyDrop<T: ?Sized> {
553 pub value: T,
554}
555
556#[lang = "maybe_uninit"]
557#[repr(transparent)]
558pub union MaybeUninit<T> {
559 pub uninit: (),
560 pub value: ManuallyDrop<T>,
561}
562
563pub mod intrinsics {
564 extern "rust-intrinsic" {
2b03887a 565 #[rustc_safe_intrinsic]
29967ef6 566 pub fn abort() -> !;
2b03887a 567 #[rustc_safe_intrinsic]
29967ef6
XL
568 pub fn size_of<T>() -> usize;
569 pub fn size_of_val<T: ?::Sized>(val: *const T) -> usize;
2b03887a 570 #[rustc_safe_intrinsic]
29967ef6
XL
571 pub fn min_align_of<T>() -> usize;
572 pub fn min_align_of_val<T: ?::Sized>(val: *const T) -> usize;
573 pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
574 pub fn transmute<T, U>(e: T) -> U;
575 pub fn ctlz_nonzero<T>(x: T) -> T;
2b03887a 576 #[rustc_safe_intrinsic]
923072b8 577 pub fn needs_drop<T: ?::Sized>() -> bool;
2b03887a 578 #[rustc_safe_intrinsic]
29967ef6 579 pub fn bitreverse<T>(x: T) -> T;
2b03887a 580 #[rustc_safe_intrinsic]
29967ef6
XL
581 pub fn bswap<T>(x: T) -> T;
582 pub fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
583 }
584}
585
586pub mod libc {
f2b60f7d
FG
587 // With the new Universal CRT, msvc has switched to all the printf functions being inline wrapper
588 // functions. legacy_stdio_definitions.lib which provides the printf wrapper functions as normal
589 // symbols to link against.
590 #[cfg_attr(unix, link(name = "c"))]
591 #[cfg_attr(target_env="msvc", link(name="legacy_stdio_definitions"))]
592 extern "C" {
593 pub fn printf(format: *const i8, ...) -> i32;
594 }
595
5869c6ff
XL
596 #[cfg_attr(unix, link(name = "c"))]
597 #[cfg_attr(target_env = "msvc", link(name = "msvcrt"))]
29967ef6
XL
598 extern "C" {
599 pub fn puts(s: *const i8) -> i32;
29967ef6
XL
600 pub fn malloc(size: usize) -> *mut u8;
601 pub fn free(ptr: *mut u8);
602 pub fn memcpy(dst: *mut u8, src: *const u8, size: usize);
603 pub fn memmove(dst: *mut u8, src: *const u8, size: usize);
604 pub fn strncpy(dst: *mut u8, src: *const u8, size: usize);
605 }
606}
607
608#[lang = "index"]
609pub trait Index<Idx: ?Sized> {
610 type Output: ?Sized;
611 fn index(&self, index: Idx) -> &Self::Output;
612}
613
614impl<T> Index<usize> for [T; 3] {
615 type Output = T;
616
617 fn index(&self, index: usize) -> &Self::Output {
618 &self[index]
619 }
620}
621
622impl<T> Index<usize> for [T] {
623 type Output = T;
624
625 fn index(&self, index: usize) -> &Self::Output {
626 &self[index]
627 }
628}
629
630extern {
631 type VaListImpl;
632}
633
634#[lang = "va_list"]
635#[repr(transparent)]
636pub struct VaList<'a>(&'a mut VaListImpl);
637
638#[rustc_builtin_macro]
639#[rustc_macro_transparency = "semitransparent"]
640pub macro stringify($($t:tt)*) { /* compiler built-in */ }
641
642#[rustc_builtin_macro]
643#[rustc_macro_transparency = "semitransparent"]
644pub macro file() { /* compiler built-in */ }
645
646#[rustc_builtin_macro]
647#[rustc_macro_transparency = "semitransparent"]
648pub macro line() { /* compiler built-in */ }
649
650#[rustc_builtin_macro]
651#[rustc_macro_transparency = "semitransparent"]
652pub macro cfg() { /* compiler built-in */ }
653
654#[rustc_builtin_macro]
655#[rustc_macro_transparency = "semitransparent"]
656pub macro global_asm() { /* compiler built-in */ }
657
658pub static A_STATIC: u8 = 42;
659
660#[lang = "panic_location"]
661struct PanicLocation {
662 file: &'static str,
663 line: u32,
664 column: u32,
665}
666
667#[no_mangle]
cdc7bbd5 668#[cfg(not(windows))]
29967ef6
XL
669pub fn get_tls() -> u8 {
670 #[thread_local]
671 static A: u8 = 42;
672
673 A
674}