Skip to content

Commit 1f4af51

Browse files
committed
librustc: Add an intrinsic to retrieve the return pointer of a function.
This is needed for some GC stuff in Servo.
1 parent 31590bd commit 1f4af51

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/librustc/middle/trans/intrinsic.rs

+6
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,12 @@ pub fn trans_intrinsic_call<'a>(mut bcx: &'a Block<'a>, node: ast::NodeId,
425425
with_overflow_intrinsic(bcx, "llvm.umul.with.overflow.i64", ret_ty,
426426
*llargs.get(0), *llargs.get(1)),
427427

428+
(_, "return_address") => {
429+
PointerCast(bcx,
430+
bcx.fcx.llretptr.get().unwrap(),
431+
Type::i8p(bcx.ccx()))
432+
}
433+
428434
// This requires that atomic intrinsics follow a specific naming pattern:
429435
// "atomic_<operation>[_<ordering>]", and no ordering means SeqCst
430436
(_, name) if name.starts_with("atomic_") => {

src/librustc/middle/typeck/check/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -4986,6 +4986,8 @@ pub fn check_intrinsic_type(ccx: &CrateCtxt, it: &ast::ForeignItem) {
49864986
(0, vec!(ty::mk_u64(), ty::mk_u64()),
49874987
ty::mk_tup(tcx, vec!(ty::mk_u64(), ty::mk_bool()))),
49884988

4989+
"return_address" => (0, vec![], ty::mk_imm_ptr(tcx, ty::mk_u8())),
4990+
49894991
ref other => {
49904992
span_err!(tcx.sess, it.span, E0093,
49914993
"unrecognized intrinsic function: `{}`", *other);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(intrinsics)];
12+
13+
use std::ptr;
14+
15+
struct Point {
16+
x: f32,
17+
y: f32,
18+
z: f32,
19+
}
20+
21+
extern "rust-intrinsic" {
22+
fn return_address() -> *const u8;
23+
}
24+
25+
fn f(result: &mut uint) -> Point {
26+
unsafe {
27+
*result = return_address() as uint;
28+
Point {
29+
x: 1.0,
30+
y: 2.0,
31+
z: 3.0,
32+
}
33+
}
34+
35+
}
36+
37+
fn main() {
38+
let mut intrinsic_reported_address = 0;
39+
let pt = f(&mut intrinsic_reported_address);
40+
let actual_address = &pt as *const Point as uint;
41+
assert_eq!(intrinsic_reported_address, actual_address);
42+
}
43+

0 commit comments

Comments
 (0)