]> 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 24429555a5fd7470dd8d36dde7b43f19c88821ad..b7277f5ee9f489867bedf15f32be363c47ccbd5a 100644 (file)
@@ -12,6 +12,7 @@ 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 {}
 
@@ -50,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 {}
@@ -102,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;
@@ -117,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;
@@ -171,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)
@@ -229,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();
     }
 }
@@ -253,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] = [0xff; 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"]
@@ -280,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;