]> git.lizzy.rs Git - rust.git/blobdiff - example/mini_core.rs
Pass command-line arguments to JITed function
[rust.git] / example / mini_core.rs
index 455661bd77e159a2dda878663beb17d858db90ab..b7277f5ee9f489867bedf15f32be363c47ccbd5a 100644 (file)
@@ -1,4 +1,4 @@
-#![feature(no_core, lang_items, intrinsics, unboxed_closures)]
+#![feature(no_core, lang_items, intrinsics, unboxed_closures, type_ascription)]
 #![no_core]
 #![allow(dead_code)]
 
@@ -12,9 +12,29 @@ pub trait Unsize<T: ?Sized> {}
 pub trait CoerceUnsized<T> {}
 
 impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
+impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {}
 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
 
+#[lang = "dispatch_from_dyn"]
+pub trait DispatchFromDyn<T> {}
+
+// &T -> &U
+impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a U> for &'a T {}
+// &mut T -> &mut U
+impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a mut U> for &'a mut T {}
+// *const T -> *const U
+impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*const U> for *const T {}
+// *mut T -> *mut U
+impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*mut U> for *mut T {}
+
+#[lang = "receiver"]
+pub trait Receiver {}
+
+impl<T: ?Sized> Receiver for &T {}
+impl<T: ?Sized> Receiver for &mut T {}
+impl<T: ?Sized> Receiver for Box<T> {}
+
 #[lang = "copy"]
 pub unsafe trait Copy {}
 
@@ -31,6 +51,7 @@ unsafe impl Copy for isize {}
 unsafe impl Copy for char {}
 unsafe impl<'a, T: ?Sized> Copy for &'a T {}
 unsafe impl<T: ?Sized> Copy for *const T {}
+unsafe impl<T: ?Sized> Copy for *mut T {}
 
 #[lang = "sync"]
 pub unsafe trait Sync {}
@@ -83,6 +104,14 @@ fn mul(self, rhs: Self) -> Self::Output {
     }
 }
 
+impl Mul for usize {
+    type Output = Self;
+
+    fn mul(self, rhs: Self) -> Self::Output {
+        self * rhs
+    }
+}
+
 #[lang = "add"]
 pub trait Add<RHS = Self> {
     type Output;
@@ -98,6 +127,14 @@ fn add(self, rhs: Self) -> Self {
     }
 }
 
+impl Add for usize {
+    type Output = Self;
+
+    fn add(self, rhs: Self) -> Self {
+        self + rhs
+    }
+}
+
 #[lang = "sub"]
 pub trait Sub<RHS = Self> {
     type Output;
@@ -152,6 +189,42 @@ fn ne(&self, other: &u8) -> bool {
     }
 }
 
+impl PartialEq for u16 {
+    fn eq(&self, other: &u16) -> bool {
+        (*self) == (*other)
+    }
+    fn ne(&self, other: &u16) -> bool {
+        (*self) != (*other)
+    }
+}
+
+impl PartialEq for u32 {
+    fn eq(&self, other: &u32) -> bool {
+        (*self) == (*other)
+    }
+    fn ne(&self, other: &u32) -> bool {
+        (*self) != (*other)
+    }
+}
+
+impl PartialEq for usize {
+    fn eq(&self, other: &usize) -> bool {
+        (*self) == (*other)
+    }
+    fn ne(&self, other: &usize) -> bool {
+        (*self) != (*other)
+    }
+}
+
+impl PartialEq for isize {
+    fn eq(&self, other: &isize) -> bool {
+        (*self) == (*other)
+    }
+    fn ne(&self, other: &isize) -> bool {
+        (*self) != (*other)
+    }
+}
+
 impl PartialEq for char {
     fn eq(&self, other: &char) -> bool {
         (*self) == (*other)
@@ -210,8 +283,12 @@ pub trait FnMut<Args>: FnOnce<Args> {
 }
 
 #[lang = "panic"]
-pub fn panic(_expr_file_line_col: &(&'static str, &'static str, u32, u32)) -> ! {
+// Make it available to jited mini_core_hello_world
+// FIXME remove next line when jit supports linking rlibs
+#[inline(always)]
+pub fn panic(&(_msg, _file, _line, _col): &(&'static str, &'static str, u32, u32)) -> ! {
     unsafe {
+        libc::puts("Panicking\0" as *const str as *const u8);
         intrinsics::abort();
     }
 }
@@ -234,11 +311,24 @@ pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
 
 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
 
-static mut MY_TINY_HEAP: [u8; 16] = [0; 16];
+impl<T: ?Sized> Drop for Box<T> {
+    fn drop(&mut self) {
+        // drop is currently performed by compiler.
+    }
+}
 
 #[lang = "exchange_malloc"]
+// Make it available to jited mini_core_hello_world
+// FIXME remove next line when jit supports linking rlibs
+#[inline(always)]
 unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
-    &mut MY_TINY_HEAP as *mut [u8; 16] as *mut u8
+    libc::malloc(size)
+}
+
+#[lang = "box_free"]
+#[inline(always)]
+unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
+    libc::free(ptr as *mut u8);
 }
 
 #[lang = "drop"]
@@ -261,6 +351,19 @@ pub mod intrinsics {
     }
 }
 
+pub mod libc {
+    #[link(name = "c")]
+    extern "C" {
+        pub fn puts(s: *const u8);
+        pub fn printf(format: *const i8, ...) -> i32;
+        pub fn malloc(size: usize) -> *mut u8;
+        pub fn free(ptr: *mut u8);
+        pub fn memcpy(dst: *mut u8, src: *const u8, size: usize);
+        pub fn memmove(dst: *mut u8, src: *const u8, size: usize);
+        pub fn strncpy(dst: *mut u8, src: *const u8, size: usize);
+    }
+}
+
 #[lang = "index"]
 pub trait Index<Idx: ?Sized> {
     type Output: ?Sized;