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