Skip to content

Commit 8047957

Browse files
committed
Construct the wrappers to native functions. Hello world now works :-)
1 parent 90f299e commit 8047957

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

src/comp/middle/trans.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4972,11 +4972,6 @@ fn decl_native_fn_and_pair(@crate_ctxt cx,
49724972
let str s = cx.names.next("_rust_wrapper") + sep() + name;
49734973
let ValueRef wrapper_fn = decl_fastcall_fn(cx.llmod, s, wrapper_type);
49744974

4975-
// Build the wrapper.
4976-
auto fcx = new_fn_ctxt(cx, wrapper_fn);
4977-
auto bcx = new_top_block_ctxt(fcx);
4978-
bcx.build.RetVoid();
4979-
49804975
// Declare the global constant pair that points to it.
49814976
auto wrapper_pair_type = T_fn_pair(cx.tn, wrapper_type);
49824977
let str ps = cx.names.next("_rust_wrapper_pair") + sep() + name;
@@ -4985,7 +4980,40 @@ fn decl_native_fn_and_pair(@crate_ctxt cx,
49854980

49864981
// Declare the function itself.
49874982
auto llfty = get_pair_fn_ty(node_type(cx, ann));
4988-
decl_cdecl_fn(cx.llmod, name, llfty);
4983+
auto function = decl_cdecl_fn(cx.llmod, name, llfty);
4984+
4985+
// Build the wrapper.
4986+
auto fcx = new_fn_ctxt(cx, wrapper_fn);
4987+
auto bcx = new_top_block_ctxt(fcx);
4988+
auto fn_type = node_ann_type(cx, ann);
4989+
4990+
let vec[ValueRef] call_args = vec();
4991+
auto abi = ty.ty_fn_abi(fn_type);
4992+
auto arg_n = 3u;
4993+
alt (abi) {
4994+
case (ast.native_abi_rust) {
4995+
call_args += vec(fcx.lltaskptr);
4996+
auto num_ty_param = ty.count_ty_params(plain_ty(fn_type.struct));
4997+
for each (uint i in _uint.range(0u, num_ty_param)) {
4998+
auto llarg = llvm.LLVMGetParam(fcx.llfn, arg_n);
4999+
check (llarg as int != 0);
5000+
call_args += vec(llarg);
5001+
arg_n += 1u;
5002+
}
5003+
}
5004+
case (ast.native_abi_cdecl) {
5005+
}
5006+
}
5007+
auto args = ty.ty_fn_args(fn_type);
5008+
for (ty.arg arg in args) {
5009+
auto llarg = llvm.LLVMGetParam(fcx.llfn, arg_n);
5010+
check (llarg as int != 0);
5011+
call_args += vec(llarg);
5012+
arg_n += 1u;
5013+
}
5014+
auto r = bcx.build.Call(function, call_args);
5015+
bcx.build.Store(r, fcx.llretptr);
5016+
bcx.build.RetVoid();
49895017
}
49905018

49915019
fn collect_native_item(&@crate_ctxt cx, @ast.native_item i) -> @crate_ctxt {

src/comp/middle/ty.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,13 @@ fn ty_fn_proto(@t fty) -> ast.proto {
645645
fail;
646646
}
647647

648+
fn ty_fn_abi(@t fty) -> ast.native_abi {
649+
alt (fty.struct) {
650+
case (ty.ty_native_fn(?a, _, _)) { ret a; }
651+
}
652+
fail;
653+
}
654+
648655
fn ty_fn_ret(@t fty) -> @t {
649656
alt (fty.struct) {
650657
case (ty.ty_fn(_, _, ?r)) { ret r; }

0 commit comments

Comments
 (0)