1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
|
import syntax::ast;
import syntax::ast_util;
import lib::llvm::llvm;
import llvm::{ValueRef, TypeRef};
import trans_common::*;
import trans_build::*;
import trans::*;
import middle::freevars::get_freevars;
import option::{some, none};
import back::abi;
import syntax::codemap::span;
import back::link::{
mangle_internal_name_by_path,
mangle_internal_name_by_path_and_seq};
import trans::{
trans_shared_malloc,
type_of_inner,
size_of,
node_id_type,
INIT,
trans_shared_free,
drop_ty,
new_sub_block_ctxt,
load_if_immediate,
dest
};
// ___Good to know (tm)__________________________________________________
//
// The layout of a closure environment in memory is
// roughly as follows:
//
// struct closure_box {
// unsigned ref_count; // only used for sharid environments
// struct closure {
// type_desc *tydesc; // descriptor for the env type
// type_desc *bound_tdescs[]; // bound descriptors
// struct {
// upvar1_t upvar1;
// ...
// upvarN_t upvarN;
// } bound_data;
// };
// };
//
// NB: this is defined in the code in T_closure_ptr and
// closure_ty_to_tuple_ty (below).
//
// Note that the closure carries a type descriptor that describes
// itself. Trippy. This is needed because the precise types of the
// closed over data are lost in the closure type (`fn(T)->U`), so if
// we need to take/drop, we must know what data is in the upvars and
// so forth.
//
// The allocation strategy for this closure depends on the closure
// type. For a sendfn, the closure (and the referenced type
// descriptors) will be allocated in the exchange heap. For a fn, the
// closure is allocated in the task heap and is reference counted.
// For a block, the closure is allocated on the stack. Note that in
// all cases we allocate space for a ref count just to make our lives
// easier when upcasting to block(T)->U, in the shape code, and so
// forth.
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tag environment_value {
env_expr(@ast::expr);
env_direct(ValueRef, ty::t, bool);
}
// Given a closure ty, emits a corresponding tuple ty
fn mk_closure_ty(tcx: ty::ctxt,
ck: ty::closure_kind,
n_bound_tds: uint,
bound_data_ty: ty::t)
-> ty::t {
let tydesc_ty = alt ck {
ty::closure_block. | ty::closure_shared. { ty::mk_type(tcx) }
ty::closure_send. { ty::mk_send_type(tcx) }
};
ret ty::mk_tup(tcx, [
tydesc_ty,
ty::mk_tup(tcx, vec::init_elt(tydesc_ty, n_bound_tds)),
bound_data_ty]);
}
fn shared_opaque_closure_box_ty(tcx: ty::ctxt) -> ty::t {
let opaque_closure_ty = ty::mk_opaque_closure(tcx);
ret ty::mk_imm_box(tcx, opaque_closure_ty);
}
fn send_opaque_closure_box_ty(tcx: ty::ctxt) -> ty::t {
let opaque_closure_ty = ty::mk_opaque_closure(tcx);
let tup_ty = ty::mk_tup(tcx, [ty::mk_int(tcx), opaque_closure_ty]);
ret ty::mk_uniq(tcx, {ty: tup_ty, mut: ast::imm});
}
type closure_result = {
llbox: ValueRef, // llvalue of boxed environment
box_ty: ty::t, // type of boxed environment
bcx: @block_ctxt // final bcx
};
// Given a block context and a list of tydescs and values to bind
// construct a closure out of them. If copying is true, it is a
// heap allocated closure that copies the upvars into environment.
// Otherwise, it is stack allocated and copies pointers to the upvars.
fn store_environment(
bcx: @block_ctxt, lltydescs: [ValueRef],
bound_values: [environment_value],
ck: ty::closure_kind)
-> closure_result {
fn dummy_environment_box(bcx: @block_ctxt, r: result)
-> (@block_ctxt, ValueRef, ValueRef) {
// Prevent glue from trying to free this.
let ccx = bcx_ccx(bcx);
let ref_cnt = GEPi(bcx, r.val, [0, abi::box_rc_field_refcnt]);
Store(r.bcx, C_int(ccx, 2), ref_cnt);
let closure = GEPi(r.bcx, r.val, [0, abi::box_rc_field_body]);
(r.bcx, closure, r.val)
}
fn maybe_clone_tydesc(bcx: @block_ctxt,
ck: ty::closure_kind,
td: ValueRef) -> ValueRef {
ret alt ck {
ty::closure_block. | ty::closure_shared. {
td
}
ty::closure_send. {
Call(bcx, bcx_ccx(bcx).upcalls.create_shared_type_desc, [td])
}
};
}
//let ccx = bcx_ccx(bcx);
let tcx = bcx_tcx(bcx);
// First, synthesize a tuple type containing the types of all the
// bound expressions.
// bindings_ty = [bound_ty1, bound_ty2, ...]
let bound_tys = [];
for bv in bound_values {
bound_tys += [alt bv {
env_direct(_, t, _) { t }
env_expr(e) { ty::expr_ty(tcx, e) }
}];
}
let bound_data_ty = ty::mk_tup(tcx, bound_tys);
let closure_ty =
mk_closure_ty(tcx, ck, vec::len(lltydescs), bound_data_ty);
let temp_cleanups = [];
// Allocate a box that can hold something closure-sized.
//
// For now, no matter what kind of closure we have, we always allocate
// space for a ref cnt in the closure. If the closure is a block or
// unique closure, this ref count isn't really used: we initialize it to 2
// so that it will never drop to zero. This is a hack and could go away
// but then we'd have to modify the code to do the right thing when
// casting from a shared closure to a block.
let (bcx, closure, box) = alt ck {
ty::closure_shared. {
let r = trans::trans_malloc_boxed(bcx, closure_ty);
add_clean_free(bcx, r.box, false);
temp_cleanups += [r.box];
(r.bcx, r.body, r.box)
}
ty::closure_send. {
// Dummy up a box in the exchange heap.
let tup_ty = ty::mk_tup(tcx, [ty::mk_int(tcx), closure_ty]);
let box_ty = ty::mk_uniq(tcx, {ty: tup_ty, mut: ast::imm});
check trans_uniq::type_is_unique_box(bcx, box_ty);
let r = trans_uniq::alloc_uniq(bcx, box_ty);
add_clean_free(bcx, r.val, true);
temp_cleanups += [r.val];
dummy_environment_box(bcx, r)
}
ty::closure_block. {
// Dummy up a box on the stack,
let ty = ty::mk_tup(tcx, [ty::mk_int(tcx), closure_ty]);
let r = trans::alloc_ty(bcx, ty);
dummy_environment_box(bcx, r)
}
};
// Store bindings tydesc.
alt ck {
ty::closure_shared. | ty::closure_send. {
let bound_tydesc = GEPi(bcx, closure, [0, abi::closure_elt_tydesc]);
let ti = none;
// NDM I believe this is the correct value,
// but using it exposes bugs and limitations
// in the shape code. Therefore, I am using
// tps_normal, which is what we used before.
//
// let tps = tps_fn(vec::len(lltydescs));
let tps = tps_normal;
let {result:closure_td, _} =
trans::get_tydesc(bcx, closure_ty, true, tps, ti);
trans::lazily_emit_tydesc_glue(bcx, abi::tydesc_field_drop_glue, ti);
trans::lazily_emit_tydesc_glue(bcx, abi::tydesc_field_free_glue, ti);
bcx = closure_td.bcx;
let td = maybe_clone_tydesc(bcx, ck, closure_td.val);
Store(bcx, td, bound_tydesc);
}
ty::closure_block. { /* skip this for blocks, not really relevant */ }
}
check type_is_tup_like(bcx, closure_ty);
let box_ty = ty::mk_imm_box(bcx_tcx(bcx), closure_ty);
// If necessary, copy tydescs describing type parameters into the
// appropriate slot in the closure.
let {bcx:bcx, val:ty_params_slot} =
GEP_tup_like_1(bcx, closure_ty, closure,
[0, abi::closure_elt_ty_params]);
vec::iteri(lltydescs) { |i, td|
let ty_param_slot = GEPi(bcx, ty_params_slot, [0, i as int]);
let cloned_td = maybe_clone_tydesc(bcx, ck, td);
Store(bcx, cloned_td, ty_param_slot);
}
// Copy expr values into boxed bindings.
// Silly check
vec::iteri(bound_values) { |i, bv|
let bound = trans::GEP_tup_like_1(bcx, box_ty, box,
[0, abi::box_rc_field_body,
abi::closure_elt_bindings,
i as int]);
bcx = bound.bcx;
alt bv {
env_expr(e) {
bcx = trans::trans_expr_save_in(bcx, e, bound.val);
add_clean_temp_mem(bcx, bound.val, bound_tys[i]);
temp_cleanups += [bound.val];
}
env_direct(val, ty, is_mem) {
alt ck {
ty::closure_shared. | ty::closure_send. {
let val1 = is_mem ? load_if_immediate(bcx, val, ty) : val;
bcx = trans::copy_val(bcx, INIT, bound.val, val1, ty);
}
ty::closure_block. {
let addr = is_mem ? val : do_spill_noroot(bcx, val);
Store(bcx, addr, bound.val);
}
}
}
}
}
for cleanup in temp_cleanups { revoke_clean(bcx, cleanup); }
ret {llbox: box, box_ty: box_ty, bcx: bcx};
}
// Given a context and a list of upvars, build a closure. This just
// collects the upvars and packages them up for store_environment.
fn build_closure(cx: @block_ctxt,
upvars: @[ast::def],
ck: ty::closure_kind)
-> closure_result {
// If we need to, package up the iterator body to call
let env_vals = [];
let tcx = bcx_tcx(cx);
// Package up the upvars
vec::iter(*upvars) { |def|
let lv = trans_local_var(cx, def);
let nid = ast_util::def_id_of_def(def).node;
let ty = ty::node_id_to_monotype(tcx, nid);
alt ck {
ty::closure_block. { ty = ty::mk_mut_ptr(tcx, ty); }
ty::closure_send. | ty::closure_shared. {}
}
env_vals += [env_direct(lv.val, ty, lv.kind == owned)];
}
ret store_environment(cx, copy cx.fcx.lltydescs, env_vals, ck);
}
// Given an enclosing block context, a new function context, a closure type,
// and a list of upvars, generate code to load and populate the environment
// with the upvars and type descriptors.
fn load_environment(enclosing_cx: @block_ctxt,
fcx: @fn_ctxt,
boxed_closure_ty: ty::t,
upvars: @[ast::def],
ck: ty::closure_kind) {
let bcx = new_raw_block_ctxt(fcx, fcx.llloadenv);
let ccx = bcx_ccx(bcx);
let sp = bcx.sp;
check (type_has_static_size(ccx, boxed_closure_ty));
let llty = type_of(ccx, sp, boxed_closure_ty);
let llclosure = PointerCast(bcx, fcx.llenv, llty);
// Populate the type parameters from the environment. We need to
// do this first because the tydescs are needed to index into
// the bindings if they are dynamically sized.
let tydesc_count = vec::len(enclosing_cx.fcx.lltydescs);
let lltydescs = GEPi(bcx, llclosure,
[0, abi::box_rc_field_body,
abi::closure_elt_ty_params]);
uint::range(0u, tydesc_count) { |i|
let lltydescptr = GEPi(bcx, lltydescs, [0, i as int]);
fcx.lltydescs += [Load(bcx, lltydescptr)];
}
// Populate the upvars from the environment.
let path = [0, abi::box_rc_field_body, abi::closure_elt_bindings];
vec::iteri(*upvars) { |i, upvar_def|
check type_is_tup_like(bcx, boxed_closure_ty);
let upvarptr =
GEP_tup_like(bcx, boxed_closure_ty, llclosure, path + [i as int]);
bcx = upvarptr.bcx;
let llupvarptr = upvarptr.val;
alt ck {
ty::closure_block. { llupvarptr = Load(bcx, llupvarptr); }
ty::closure_send. | ty::closure_shared. { }
}
let def_id = ast_util::def_id_of_def(upvar_def);
fcx.llupvars.insert(def_id.node, llupvarptr);
}
}
fn trans_expr_fn(bcx: @block_ctxt, f: ast::_fn, sp: span,
id: ast::node_id, dest: dest) -> @block_ctxt {
if dest == ignore { ret bcx; }
let ccx = bcx_ccx(bcx), bcx = bcx;
let fty = node_id_type(ccx, id);
check returns_non_ty_var(ccx, fty);
let llfnty = type_of_fn_from_ty(ccx, sp, fty, 0u);
let sub_cx = extend_path(bcx.fcx.lcx, ccx.names.next("anon"));
let s = mangle_internal_name_by_path(ccx, sub_cx.path);
let llfn = decl_internal_cdecl_fn(ccx.llmod, s, llfnty);
let trans_closure_env = lambda(ck: ty::closure_kind) -> ValueRef {
let upvars = get_freevars(ccx.tcx, id);
let {llbox, box_ty, bcx} = build_closure(bcx, upvars, ck);
trans_closure(sub_cx, sp, f, llfn, no_self, [], id, {|fcx|
load_environment(bcx, fcx, box_ty, upvars, ck);
});
llbox
};
let closure = alt f.proto {
ast::proto_block. { trans_closure_env(ty::closure_block) }
ast::proto_shared(_) { trans_closure_env(ty::closure_shared) }
ast::proto_send. { trans_closure_env(ty::closure_send) }
ast::proto_bare. {
let closure = C_null(T_opaque_boxed_closure_ptr(ccx));
trans_closure(sub_cx, sp, f, llfn, no_self, [], id, {|_fcx|});
closure
}
};
fill_fn_pair(bcx, get_dest_addr(dest), llfn, closure);
ret bcx;
}
fn trans_bind(cx: @block_ctxt, f: @ast::expr, args: [option::t<@ast::expr>],
id: ast::node_id, dest: dest) -> @block_ctxt {
let f_res = trans_callee(cx, f);
ret trans_bind_1(cx, ty::expr_ty(bcx_tcx(cx), f), f_res, args,
ty::node_id_to_type(bcx_tcx(cx), id), dest);
}
fn trans_bind_1(cx: @block_ctxt, outgoing_fty: ty::t,
f_res: lval_maybe_callee,
args: [option::t<@ast::expr>], pair_ty: ty::t,
dest: dest) -> @block_ctxt {
let bound: [@ast::expr] = [];
for argopt: option::t<@ast::expr> in args {
alt argopt { none. { } some(e) { bound += [e]; } }
}
let bcx = f_res.bcx;
if dest == ignore {
for ex in bound { bcx = trans_expr(bcx, ex, ignore); }
ret bcx;
}
// Figure out which tydescs we need to pass, if any.
let outgoing_fty_real; // the type with typarams still in it
let lltydescs: [ValueRef];
alt f_res.generic {
none. { outgoing_fty_real = outgoing_fty; lltydescs = []; }
some(ginfo) {
lazily_emit_all_generic_info_tydesc_glues(cx, ginfo);
outgoing_fty_real = ginfo.item_type;
lltydescs = ginfo.tydescs;
}
}
let ty_param_count = vec::len(lltydescs);
if vec::len(bound) == 0u && ty_param_count == 0u {
// Trivial 'binding': just return the closure
let lv = lval_maybe_callee_to_lval(f_res, pair_ty);
bcx = lv.bcx;
ret memmove_ty(bcx, get_dest_addr(dest), lv.val, pair_ty);
}
let closure = alt f_res.env {
null_env. { none }
_ { let (_, cl) = maybe_add_env(cx, f_res); some(cl) }
};
// FIXME: should follow from a precondition on trans_bind_1
let ccx = bcx_ccx(cx);
check (type_has_static_size(ccx, outgoing_fty));
// Arrange for the bound function to live in the first binding spot
// if the function is not statically known.
let (env_vals, target_res) = alt closure {
some(cl) {
// Cast the function we are binding to be the type that the
// closure will expect it to have. The type the closure knows
// about has the type parameters substituted with the real types.
let sp = cx.sp;
let llclosurety = T_ptr(type_of(ccx, sp, outgoing_fty));
let src_loc = PointerCast(bcx, cl, llclosurety);
([env_direct(src_loc, pair_ty, true)], none)
}
none. { ([], some(f_res.val)) }
};
// Actually construct the closure
let {llbox, box_ty, bcx} = store_environment(
bcx, lltydescs,
env_vals + vec::map(bound, {|x| env_expr(x)}),
ty::closure_shared);
// Make thunk
let llthunk =
trans_bind_thunk(cx.fcx.lcx, cx.sp, pair_ty, outgoing_fty_real, args,
box_ty, ty_param_count, target_res);
// Fill the function pair
fill_fn_pair(bcx, get_dest_addr(dest), llthunk.val, llbox);
ret bcx;
}
fn make_fn_glue(
cx: @block_ctxt,
v: ValueRef,
t: ty::t,
glue_fn: fn(@block_ctxt, v: ValueRef, t: ty::t) -> @block_ctxt)
-> @block_ctxt {
let bcx = cx;
let tcx = bcx_tcx(cx);
let fn_env = lambda(blk: block(@block_ctxt, ValueRef) -> @block_ctxt)
-> @block_ctxt {
let box_cell_v = GEPi(cx, v, [0, abi::fn_field_box]);
let box_ptr_v = Load(cx, box_cell_v);
let inner_cx = new_sub_block_ctxt(cx, "iter box");
let next_cx = new_sub_block_ctxt(cx, "next");
let null_test = IsNull(cx, box_ptr_v);
CondBr(cx, null_test, next_cx.llbb, inner_cx.llbb);
inner_cx = blk(inner_cx, box_cell_v);
Br(inner_cx, next_cx.llbb);
ret next_cx;
};
ret alt ty::struct(tcx, t) {
ty::ty_native_fn(_, _) | ty::ty_fn(ast::proto_bare., _, _, _, _) {
bcx
}
ty::ty_fn(ast::proto_block., _, _, _, _) {
bcx
}
ty::ty_fn(ast::proto_send., _, _, _, _) {
fn_env({ |bcx, box_cell_v|
let box_ty = trans_closure::send_opaque_closure_box_ty(tcx);
glue_fn(bcx, box_cell_v, box_ty)
})
}
ty::ty_fn(ast::proto_shared(_), _, _, _, _) {
fn_env({ |bcx, box_cell_v|
let box_ty = trans_closure::shared_opaque_closure_box_ty(tcx);
glue_fn(bcx, box_cell_v, box_ty)
})
}
_ { fail "make_fn_glue invoked on non-function type" }
};
}
fn call_opaque_closure_glue(bcx: @block_ctxt,
v: ValueRef, // ptr to an opaque closure
field: int) -> @block_ctxt {
let ccx = bcx_ccx(bcx);
let v = PointerCast(bcx, v, T_ptr(T_opaque_closure(ccx)));
let tydescptr = GEPi(bcx, v, [0, abi::closure_elt_tydesc]);
let tydesc = Load(bcx, tydescptr);
let ti = none;
call_tydesc_glue_full(bcx, v, tydesc, field, ti);
ret bcx;
}
// pth is cx.path
fn trans_bind_thunk(cx: @local_ctxt,
sp: span,
incoming_fty: ty::t,
outgoing_fty: ty::t,
args: [option::t<@ast::expr>],
boxed_closure_ty: ty::t,
ty_param_count: uint,
target_fn: option::t<ValueRef>)
-> {val: ValueRef, ty: TypeRef} {
// If we supported constraints on record fields, we could make the
// constraints for this function:
/*
: returns_non_ty_var(ccx, outgoing_fty),
type_has_static_size(ccx, incoming_fty) ->
*/
// but since we don't, we have to do the checks at the beginning.
let ccx = cx.ccx;
check type_has_static_size(ccx, incoming_fty);
// Here we're not necessarily constructing a thunk in the sense of
// "function with no arguments". The result of compiling 'bind f(foo,
// bar, baz)' would be a thunk that, when called, applies f to those
// arguments and returns the result. But we're stretching the meaning of
// the word "thunk" here to also mean the result of compiling, say, 'bind
// f(foo, _, baz)', or any other bind expression that binds f and leaves
// some (or all) of the arguments unbound.
// Here, 'incoming_fty' is the type of the entire bind expression, while
// 'outgoing_fty' is the type of the function that is having some of its
// arguments bound. If f is a function that takes three arguments of type
// int and returns int, and we're translating, say, 'bind f(3, _, 5)',
// then outgoing_fty is the type of f, which is (int, int, int) -> int,
// and incoming_fty is the type of 'bind f(3, _, 5)', which is int -> int.
// Once translated, the entire bind expression will be the call f(foo,
// bar, baz) wrapped in a (so-called) thunk that takes 'bar' as its
// argument and that has bindings of 'foo' to 3 and 'baz' to 5 and a
// pointer to 'f' all saved in its environment. So, our job is to
// construct and return that thunk.
// Give the thunk a name, type, and value.
let s: str = mangle_internal_name_by_path_and_seq(ccx, cx.path, "thunk");
let llthunk_ty: TypeRef = get_pair_fn_ty(type_of(ccx, sp, incoming_fty));
let llthunk: ValueRef = decl_internal_cdecl_fn(ccx.llmod, s, llthunk_ty);
// Create a new function context and block context for the thunk, and hold
// onto a pointer to the first block in the function for later use.
let fcx = new_fn_ctxt(cx, sp, llthunk);
let bcx = new_top_block_ctxt(fcx);
let lltop = bcx.llbb;
// Since we might need to construct derived tydescs that depend on
// our bound tydescs, we need to load tydescs out of the environment
// before derived tydescs are constructed. To do this, we load them
// in the load_env block.
let load_env_bcx = new_raw_block_ctxt(fcx, fcx.llloadenv);
// The 'llenv' that will arrive in the thunk we're creating is an
// environment that will contain the values of its arguments and a pointer
// to the original function. So, let's create one of those:
// The llenv pointer needs to be the correct size. That size is
// 'boxed_closure_ty', which was determined by trans_bind.
check (type_has_static_size(ccx, boxed_closure_ty));
let llclosure_ptr_ty = type_of(ccx, sp, boxed_closure_ty);
let llclosure = PointerCast(load_env_bcx, fcx.llenv, llclosure_ptr_ty);
// "target", in this context, means the function that's having some of its
// arguments bound and that will be called inside the thunk we're
// creating. (In our running example, target is the function f.) Pick
// out the pointer to the target function from the environment. The
// target function lives in the first binding spot.
let (lltargetfn, lltargetenv, starting_idx) = alt target_fn {
some(fptr) {
(fptr, llvm::LLVMGetUndef(T_opaque_boxed_closure_ptr(ccx)), 0)
}
none. {
// Silly check
check type_is_tup_like(bcx, boxed_closure_ty);
let {bcx: cx, val: pair} =
GEP_tup_like(bcx, boxed_closure_ty, llclosure,
[0, abi::box_rc_field_body,
abi::closure_elt_bindings, 0]);
let lltargetenv =
Load(cx, GEPi(cx, pair, [0, abi::fn_field_box]));
let lltargetfn = Load
(cx, GEPi(cx, pair, [0, abi::fn_field_code]));
bcx = cx;
(lltargetfn, lltargetenv, 1)
}
};
// And then, pick out the target function's own environment. That's what
// we'll use as the environment the thunk gets.
// Get f's return type, which will also be the return type of the entire
// bind expression.
let outgoing_ret_ty = ty::ty_fn_ret(cx.ccx.tcx, outgoing_fty);
// Get the types of the arguments to f.
let outgoing_args = ty::ty_fn_args(cx.ccx.tcx, outgoing_fty);
// The 'llretptr' that will arrive in the thunk we're creating also needs
// to be the correct type. Cast it to f's return type, if necessary.
let llretptr = fcx.llretptr;
let ccx = cx.ccx;
if ty::type_contains_params(ccx.tcx, outgoing_ret_ty) {
check non_ty_var(ccx, outgoing_ret_ty);
let llretty = type_of_inner(ccx, sp, outgoing_ret_ty);
llretptr = PointerCast(bcx, llretptr, T_ptr(llretty));
}
// Set up the three implicit arguments to the thunk.
let llargs: [ValueRef] = [llretptr, lltargetenv];
// Copy in the type parameters.
let i: uint = 0u;
while i < ty_param_count {
// Silly check
check type_is_tup_like(load_env_bcx, boxed_closure_ty);
let lltyparam_ptr =
GEP_tup_like(load_env_bcx, boxed_closure_ty, llclosure,
[0, abi::box_rc_field_body,
abi::closure_elt_ty_params, i as int]);
load_env_bcx = lltyparam_ptr.bcx;
let td = Load(load_env_bcx, lltyparam_ptr.val);
llargs += [td];
fcx.lltydescs += [td];
i += 1u;
}
let a: uint = 2u; // retptr, env come first
let b: int = starting_idx;
let outgoing_arg_index: uint = 0u;
let llout_arg_tys: [TypeRef] =
type_of_explicit_args(cx.ccx, sp, outgoing_args);
for arg: option::t<@ast::expr> in args {
let out_arg = outgoing_args[outgoing_arg_index];
let llout_arg_ty = llout_arg_tys[outgoing_arg_index];
alt arg {
// Arg provided at binding time; thunk copies it from
// closure.
some(e) {
// Silly check
check type_is_tup_like(bcx, boxed_closure_ty);
let bound_arg =
GEP_tup_like(bcx, boxed_closure_ty, llclosure,
[0, abi::box_rc_field_body,
abi::closure_elt_bindings, b]);
bcx = bound_arg.bcx;
let val = bound_arg.val;
if out_arg.mode == ast::by_val { val = Load(bcx, val); }
if out_arg.mode == ast::by_copy {
let {bcx: cx, val: alloc} = alloc_ty(bcx, out_arg.ty);
bcx = memmove_ty(cx, alloc, val, out_arg.ty);
bcx = take_ty(bcx, alloc, out_arg.ty);
val = alloc;
}
// If the type is parameterized, then we need to cast the
// type we actually have to the parameterized out type.
if ty::type_contains_params(cx.ccx.tcx, out_arg.ty) {
val = PointerCast(bcx, val, llout_arg_ty);
}
llargs += [val];
b += 1;
}
// Arg will be provided when the thunk is invoked.
none. {
let arg: ValueRef = llvm::LLVMGetParam(llthunk, a);
if ty::type_contains_params(cx.ccx.tcx, out_arg.ty) {
arg = PointerCast(bcx, arg, llout_arg_ty);
}
llargs += [arg];
a += 1u;
}
}
outgoing_arg_index += 1u;
}
// Cast the outgoing function to the appropriate type.
// This is necessary because the type of the function that we have
// in the closure does not know how many type descriptors the function
// needs to take.
let ccx = bcx_ccx(bcx);
check returns_non_ty_var(ccx, outgoing_fty);
let lltargetty =
type_of_fn_from_ty(ccx, sp, outgoing_fty, ty_param_count);
lltargetfn = PointerCast(bcx, lltargetfn, T_ptr(lltargetty));
Call(bcx, lltargetfn, llargs);
build_return(bcx);
finish_fn(fcx, lltop);
ret {val: llthunk, ty: llthunk_ty};
}
|