about summary refs log tree commit diff
path: root/src/librlibc
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2014-05-14 11:24:12 -0700
committerAlex Crichton <alex@alexcrichton.com>2014-05-15 13:50:37 -0700
commita7bee7b05d59467bc6304b32eb14e617c2efbfc9 (patch)
tree99c06196f44ecfcebfaace1827cea6e6befa119f /src/librlibc
parente043644cea119ea701a293a01b6d4c5c0f13f450 (diff)
downloadrust-a7bee7b05d59467bc6304b32eb14e617c2efbfc9.tar.gz
rust-a7bee7b05d59467bc6304b32eb14e617c2efbfc9.zip
Add a crate for missing stubs from libcore
The core library in theory has 0 dependencies, but in practice it has some in
order for it to be efficient. These dependencies are in the form of the basic
memory operations provided by libc traditionally, such as memset, memcmp, etc.
These functions are trivial to implement and themselves have 0 dependencies.

This commit adds a new crate, librlibc, which will serve the purpose of
providing these dependencies. The crate is never linked to by default, but is
available to be linked to by downstream consumers. Normally these functions are
provided by the system libc, but in other freestanding contexts a libc may not
be available. In these cases, librlibc will suffice for enabling execution with
libcore.

cc #10116
Diffstat (limited to 'src/librlibc')
-rw-r--r--src/librlibc/lib.rs99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/librlibc/lib.rs b/src/librlibc/lib.rs
new file mode 100644
index 00000000000..904fbe9be9b
--- /dev/null
+++ b/src/librlibc/lib.rs
@@ -0,0 +1,99 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! A bare-metal library supplying functions rustc may lower code to
+//!
+//! This library is not intended for general use, and is superseded by a system
+//! libc if one is available. In a freestanding context, however, common
+//! functions such as memset, memcpy, etc are not implemented. This library
+//! provides an implementation of these functions which are either required by
+//! libcore or called by rustc implicitly.
+//!
+//! This library is never included by default, and must be manually included if
+//! necessary. It is an error to include this library when also linking with
+//! the system libc library.
+
+#![crate_id = "rlibc#0.11.0-pre"]
+#![license = "MIT/ASL2"]
+#![crate_type = "rlib"]
+#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
+       html_favicon_url = "http://www.rust-lang.org/favicon.ico",
+       html_root_url = "http://static.rust-lang.org/doc/master")]
+
+#![no_std]
+#![experimental]
+
+// This library is definining the builtin functions, so it would be a shame for
+// LLVM to optimize these function calls to themselves!
+#![no_builtins]
+
+#[cfg(test)] extern crate std;
+#[cfg(test)] extern crate native;
+
+// Require the offset intrinsics for LLVM to properly optimize the
+// implementations below. If pointer arithmetic is done through integers the
+// optimizations start to break down.
+extern "rust-intrinsic" {
+    fn offset<T>(dst: *T, offset: int) -> *T;
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *u8, n: uint) -> *mut u8 {
+    let mut i = 0;
+    while i < n {
+        *(offset(dest as *u8, i as int) as *mut u8) = *offset(src, i as int);
+        i += 1;
+    }
+    return dest;
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn memmove(dest: *mut u8, src: *u8, n: uint) -> *mut u8 {
+    if src < dest as *u8 { // copy from end
+        let mut i = n;
+        while i != 0 {
+            i -= 1;
+            *(offset(dest as *u8, i as int) as *mut u8) = *offset(src, i as int);
+        }
+    } else { // copy from beginning
+        let mut i = 0;
+        while i < n {
+            *(offset(dest as *u8, i as int) as *mut u8) = *offset(src, i as int);
+            i += 1;
+        }
+    }
+    return dest;
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn memset(s: *mut u8, c: i32, n: uint) -> *mut u8 {
+    let mut i = 0;
+    while i < n {
+        *(offset(s as *u8, i as int) as *mut u8) = c as u8;
+        i += 1;
+    }
+    return s;
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn memcmp(s1: *u8, s2: *u8, n: uint) -> i32 {
+    let mut i = 0;
+    while i < n {
+        let a = *offset(s1, i as int);
+        let b = *offset(s2, i as int);
+        if a != b {
+            return (a - b) as i32
+        }
+        i += 1;
+    }
+    return 0;
+}
+
+#[test] fn work_on_windows() { } // FIXME #10872 needed for a happy windows