Skip to content

Commit 894b746

Browse files
committed
Replace most uses of intrinsics in core with builtins
Issue #1981
1 parent b2aace2 commit 894b746

File tree

5 files changed

+44
-85
lines changed

5 files changed

+44
-85
lines changed

src/libcore/comm.rs

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ native mod rustrt {
5555
yield: *libc::uintptr_t);
5656
}
5757

58-
#[abi = "rust-intrinsic"]
58+
#[abi = "rust-builtin"]
5959
native mod rusti {
60-
fn call_with_retptr<T: send>(&&f: fn(*uint)) -> T;
60+
fn init<T>() -> T;
6161
}
6262

6363
type port_id = int;
@@ -137,18 +137,13 @@ fn recv<T: send>(p: port<T>) -> T { recv_(***p) }
137137

138138
#[doc = "Receive on a raw port pointer"]
139139
fn recv_<T: send>(p: *rust_port) -> T {
140-
// FIXME: Due to issue 1185 we can't use a return pointer when
141-
// calling C code, and since we can't create our own return
142-
// pointer on the stack, we're going to call a little intrinsic
143-
// that will grab the value of the return pointer, then call this
144-
// function, which we will then use to call the runtime.
145-
fn recv(dptr: *uint, port: *rust_port,
146-
yield: *libc::uintptr_t) unsafe {
147-
rustrt::port_recv(dptr, port, yield);
148-
}
149140
let yield = 0u;
150141
let yieldp = ptr::addr_of(yield);
151-
let res = rusti::call_with_retptr(bind recv(_, p, yieldp));
142+
let mut res;
143+
res = rusti::init::<T>();
144+
log(debug, ptr::addr_of(res));
145+
rustrt::port_recv(ptr::addr_of(res) as *uint, p, yieldp);
146+
152147
if yield != 0u {
153148
// Data isn't available yet, so res has not been initialized.
154149
task::yield();
@@ -161,26 +156,18 @@ fn recv_<T: send>(p: *rust_port) -> T {
161156
}
162157

163158
#[doc = "Receive on one of two ports"]
164-
fn select2<A: send, B: send>(
165-
p_a: port<A>, p_b: port<B>
166-
) -> either<A, B> unsafe {
167-
168-
fn select(dptr: **rust_port, ports: **rust_port,
169-
n_ports: libc::size_t, yield: *libc::uintptr_t) {
170-
rustrt::rust_port_select(dptr, ports, n_ports, yield)
171-
}
172-
173-
let mut ports = [];
174-
ports += [***p_a, ***p_b];
159+
fn select2<A: send, B: send>(p_a: port<A>, p_b: port<B>)
160+
-> either<A, B> unsafe {
161+
let ports = [***p_a, ***p_b];
175162
let n_ports = 2 as libc::size_t;
176-
let yield = 0u;
177-
let yieldp = ptr::addr_of(yield);
163+
let yield = 0u, yieldp = ptr::addr_of(yield);
178164

179-
let resport: *rust_port = vec::as_buf(ports) {|ports|
180-
rusti::call_with_retptr {|retptr|
181-
select(unsafe::reinterpret_cast(retptr), ports, n_ports, yieldp)
182-
}
183-
};
165+
let mut resport: *rust_port;
166+
resport = rusti::init::<*rust_port>();
167+
vec::as_buf(ports) {|ports|
168+
rustrt::rust_port_select(ptr::addr_of(resport), ports, n_ports,
169+
yieldp);
170+
}
184171

185172
if yield != 0u {
186173
// Wait for data

src/libcore/ptr.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,28 @@ export null;
88
export memcpy;
99
export memmove;
1010

11+
import libc::c_void;
1112

12-
#[abi = "rust-intrinsic"]
13+
#[nolink]
14+
#[abi = "cdecl"]
15+
native mod libc_ {
16+
fn memcpy(dest: *c_void, src: *c_void, n: libc::size_t) -> *c_void;
17+
fn memmove(dest: *c_void, src: *c_void, n: libc::size_t) -> *c_void;
18+
}
19+
20+
#[abi = "rust-builtin"]
1321
native mod rusti {
1422
fn addr_of<T>(val: T) -> *T;
15-
fn memcpy<T>(dst: *T, src: *T, count: libc::uintptr_t);
16-
fn memmove<T>(dst: *T, src: *T, count: libc::uintptr_t);
1723
}
1824

1925
#[doc = "Get an unsafe pointer to a value"]
2026
#[inline(always)]
21-
fn addr_of<T>(val: T) -> *T { ret rusti::addr_of(val); }
27+
fn addr_of<T>(val: T) -> *T { rusti::addr_of(val) }
2228

2329
#[doc = "Get an unsafe mutable pointer to a value"]
2430
#[inline(always)]
2531
fn mut_addr_of<T>(val: T) -> *mutable T unsafe {
26-
ret unsafe::reinterpret_cast(rusti::addr_of(val));
32+
unsafe::reinterpret_cast(rusti::addr_of(val))
2733
}
2834

2935
#[doc = "Calculate the offset from a pointer"]
@@ -51,7 +57,8 @@ and destination may not overlap.
5157
"]
5258
#[inline(always)]
5359
unsafe fn memcpy<T>(dst: *T, src: *T, count: uint) {
54-
rusti::memcpy(dst, src, count);
60+
let n = count * sys::size_of::<T>();
61+
libc_::memcpy(dst as *c_void, src as *c_void, n);
5562
}
5663

5764
#[doc = "
@@ -62,7 +69,8 @@ and destination may overlap.
6269
"]
6370
#[inline(always)]
6471
unsafe fn memmove<T>(dst: *T, src: *T, count: uint) {
65-
rusti::memmove(dst, src, count);
72+
let n = count * sys::size_of::<T>();
73+
libc_::memmove(dst as *c_void, src as *c_void, n);
6674
}
6775

6876
#[test]

src/libcore/sys.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ native mod rustrt {
2828
fn rust_set_exit_status(code: libc::intptr_t);
2929
}
3030

31-
#[abi = "rust-intrinsic"]
31+
#[abi = "rust-builtin"]
3232
native mod rusti {
33-
fn get_type_desc<T>() -> *type_desc;
34-
35-
// Invokes __builtin_frame_address().
36-
// See <http://gcc.gnu.org/onlinedocs/gcc/Return-Address.html>.
37-
fn frame_address(n: libc::c_uint) -> libc::uintptr_t;
33+
fn get_tydesc<T>() -> *();
34+
fn size_of<T>() -> uint;
35+
fn align_of<T>() -> uint;
3836
}
3937

4038
#[doc = "
@@ -44,7 +42,7 @@ Useful for calling certain function in the Rust runtime or otherwise
4442
performing dark magick.
4543
"]
4644
fn get_type_desc<T>() -> *type_desc {
47-
ret rusti::get_type_desc::<T>();
45+
rusti::get_tydesc::<T>() as *type_desc
4846
}
4947

5048
#[doc = "Get a string representing the platform-dependent last error"]
@@ -54,12 +52,12 @@ fn last_os_error() -> str {
5452

5553
#[doc = "Returns the size of a type"]
5654
fn size_of<T>() -> uint unsafe {
57-
ret (*get_type_desc::<T>()).size;
55+
rusti::size_of::<T>()
5856
}
5957

6058
#[doc = "Returns the alignment of a type"]
6159
fn align_of<T>() -> uint unsafe {
62-
ret (*get_type_desc::<T>()).align;
60+
rusti::align_of::<T>()
6361
}
6462

6563
#[doc = "Returns the refcount of a shared box"]

src/libcore/unsafe.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,18 @@
22

33
export reinterpret_cast, forget;
44

5-
#[abi = "rust-intrinsic"]
5+
#[abi = "rust-builtin"]
66
native mod rusti {
7-
fn cast<T, U>(src: T) -> U;
8-
fn leak<T>(-thing: T);
7+
fn forget<T>(-x: T);
8+
fn reinterpret_cast<T, U>(e: T) -> U;
99
}
1010

1111
#[doc = "
1212
Casts the value at `src` to U. The two types must have the same length.
1313
"]
1414
#[inline(always)]
1515
unsafe fn reinterpret_cast<T, U>(src: T) -> U {
16-
let t1 = sys::get_type_desc::<T>();
17-
let t2 = sys::get_type_desc::<U>();
18-
if (*t1).size != (*t2).size {
19-
fail "attempt to cast values of differing sizes";
20-
}
21-
ret rusti::cast(src);
16+
rusti::reinterpret_cast(src)
2217
}
2318

2419
#[doc ="
@@ -30,7 +25,7 @@ can be used for various acts of magick, particularly when using
3025
reinterpret_cast on managed pointer types.
3126
"]
3227
#[inline(always)]
33-
unsafe fn forget<T>(-thing: T) { rusti::leak(thing); }
28+
unsafe fn forget<T>(-thing: T) { rusti::forget(thing); }
3429

3530
#[cfg(test)]
3631
mod tests {

src/test/run-pass/interior-vec.rs

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)