-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Expand file tree
/
Copy pathtrans.rs
More file actions
7874 lines (6687 loc) · 268 KB
/
trans.rs
File metadata and controls
7874 lines (6687 loc) · 268 KB
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import std._int;
import std._str;
import std._uint;
import std._vec;
import std._str.rustrt.sbuf;
import std._vec.rustrt.vbuf;
import std.map;
import std.map.hashmap;
import std.option;
import std.option.some;
import std.option.none;
import front.ast;
import front.creader;
import pretty.pprust;
import driver.session;
import middle.ty;
import back.x86;
import back.abi;
import pretty.pprust;
import middle.ty.pat_ty;
import util.common;
import util.common.istr;
import util.common.new_def_hash;
import util.common.new_str_hash;
import lib.llvm.llvm;
import lib.llvm.builder;
import lib.llvm.target_data;
import lib.llvm.type_handle;
import lib.llvm.type_names;
import lib.llvm.mk_pass_manager;
import lib.llvm.mk_target_data;
import lib.llvm.mk_type_handle;
import lib.llvm.mk_type_names;
import lib.llvm.llvm.ModuleRef;
import lib.llvm.llvm.ValueRef;
import lib.llvm.llvm.TypeRef;
import lib.llvm.llvm.TypeHandleRef;
import lib.llvm.llvm.BuilderRef;
import lib.llvm.llvm.BasicBlockRef;
import lib.llvm.False;
import lib.llvm.True;
state obj namegen(mutable int i) {
fn next(str prefix) -> str {
i += 1;
ret prefix + istr(i);
}
}
type glue_fns = rec(ValueRef activate_glue,
ValueRef yield_glue,
ValueRef exit_task_glue,
vec[ValueRef] native_glues_rust,
vec[ValueRef] native_glues_pure_rust,
vec[ValueRef] native_glues_cdecl,
ValueRef no_op_type_glue,
ValueRef memcpy_glue,
ValueRef bzero_glue,
ValueRef vec_append_glue);
type tydesc_info = rec(ValueRef tydesc,
ValueRef take_glue,
ValueRef drop_glue,
ValueRef cmp_glue);
/*
* A note on nomenclature of linking: "upcall", "extern" and "native".
*
* An "extern" is an LLVM symbol we wind up emitting an undefined external
* reference to. This means "we don't have the thing in this compilation unit,
* please make sure you link it in at runtime". This could be a reference to
* C code found in a C library, or rust code found in a rust crate.
*
* A "native" is a combination of an extern that references C code, plus a
* glue-code stub that "looks like" a rust function, emitted here, plus a
* generic N-ary bit of asm glue (found over in back/x86.rs) that performs a
* control transfer into C from rust. Natives may be normal C library code.
*
* An upcall is a native call generated by the compiler (not corresponding to
* any user-written call in the code) into librustrt, to perform some helper
* task such as bringing a task to life, allocating memory, etc.
*
*/
state type crate_ctxt = rec(session.session sess,
ModuleRef llmod,
target_data td,
type_names tn,
ValueRef crate_ptr,
hashmap[str, ValueRef] externs,
hashmap[str, ValueRef] intrinsics,
hashmap[ast.def_id, ValueRef] item_ids,
hashmap[ast.def_id, @ast.item] items,
hashmap[ast.def_id,
@ast.native_item] native_items,
ty.type_cache type_cache,
hashmap[ast.def_id, str] item_symbols,
// TODO: hashmap[tup(tag_id,subtys), @tag_info]
hashmap[ty.t, uint] tag_sizes,
hashmap[ast.def_id, ValueRef] discrims,
hashmap[ast.def_id, str] discrim_symbols,
hashmap[ast.def_id, ValueRef] fn_pairs,
hashmap[ast.def_id, ValueRef] consts,
hashmap[ast.def_id,()] obj_methods,
hashmap[ty.t, @tydesc_info] tydescs,
hashmap[str, ValueRef] module_data,
hashmap[ty.t, TypeRef] lltypes,
@glue_fns glues,
namegen names,
std.sha1.sha1 sha,
hashmap[ty.t, str] type_sha1s,
hashmap[ty.t, metadata.ty_abbrev] type_abbrevs,
ty.ctxt tcx);
type local_ctxt = rec(vec[str] path,
vec[str] module_path,
vec[ast.ty_param] obj_typarams,
vec[ast.obj_field] obj_fields,
@crate_ctxt ccx);
type self_vt = rec(ValueRef v, ty.t t);
state type fn_ctxt = rec(ValueRef llfn,
ValueRef lltaskptr,
ValueRef llenv,
ValueRef llretptr,
mutable BasicBlockRef llallocas,
mutable option.t[self_vt] llself,
mutable option.t[ValueRef] lliterbody,
hashmap[ast.def_id, ValueRef] llargs,
hashmap[ast.def_id, ValueRef] llobjfields,
hashmap[ast.def_id, ValueRef] lllocals,
hashmap[ast.def_id, ValueRef] llupvars,
mutable vec[ValueRef] lltydescs,
@local_ctxt lcx);
tag cleanup {
clean(fn(@block_ctxt cx) -> result);
}
tag block_kind {
SCOPE_BLOCK;
LOOP_SCOPE_BLOCK(option.t[@block_ctxt], @block_ctxt);
NON_SCOPE_BLOCK;
}
state type block_ctxt = rec(BasicBlockRef llbb,
builder build,
block_parent parent,
block_kind kind,
mutable vec[cleanup] cleanups,
@fn_ctxt fcx);
// FIXME: we should be able to use option.t[@block_parent] here but
// the infinite-tag check in rustboot gets upset.
tag block_parent {
parent_none;
parent_some(@block_ctxt);
}
state type result = rec(mutable @block_ctxt bcx,
mutable ValueRef val);
fn sep() -> str {
ret "_";
}
fn extend_path(@local_ctxt cx, str name) -> @local_ctxt {
ret @rec(path = cx.path + vec(name) with *cx);
}
fn path_name(vec[str] path) -> str {
ret _str.connect(path, sep());
}
fn get_type_sha1(@crate_ctxt ccx, ty.t t) -> str {
auto hash = "";
alt (ccx.type_sha1s.find(t)) {
case (some[str](?h)) { hash = h; }
case (none[str]) {
ccx.sha.reset();
auto f = metadata.def_to_str;
// NB: do *not* use abbrevs here as we want the symbol names
// to be independent of one another in the crate.
auto cx = @rec(ds=f, tcx=ccx.tcx, abbrevs=metadata.ac_no_abbrevs);
ccx.sha.input_str(metadata.Encode.ty_str(cx, t));
hash = _str.substr(ccx.sha.result_str(), 0u, 16u);
ccx.type_sha1s.insert(t, hash);
}
}
ret hash;
}
fn mangle_name_by_type(@crate_ctxt ccx, vec[str] path, ty.t t) -> str {
auto hash = get_type_sha1(ccx, t);
ret sep() + "rust" + sep() + hash + sep() + path_name(path);
}
fn mangle_name_by_type_only(@crate_ctxt ccx, ty.t t, str name) -> str {
auto f = metadata.def_to_str;
auto cx = @rec(ds=f, tcx=ccx.tcx, abbrevs=metadata.ac_no_abbrevs);
auto s = metadata.Encode.ty_str(cx, t);
auto hash = get_type_sha1(ccx, t);
ret sep() + "rust" + sep() + hash + sep() + name + "_" + s;
}
fn mangle_name_by_seq(@crate_ctxt ccx, vec[str] path, str flav) -> str {
ret sep() + "rust" + sep()
+ ccx.names.next(flav) + sep()
+ path_name(path);
}
fn res(@block_ctxt bcx, ValueRef val) -> result {
ret rec(mutable bcx = bcx,
mutable val = val);
}
fn ty_str(type_names tn, TypeRef t) -> str {
ret lib.llvm.type_to_str(tn, t);
}
fn val_ty(ValueRef v) -> TypeRef {
ret llvm.LLVMTypeOf(v);
}
fn val_str(type_names tn, ValueRef v) -> str {
ret ty_str(tn, val_ty(v));
}
// LLVM type constructors.
fn T_void() -> TypeRef {
// Note: For the time being llvm is kinda busted here, it has the notion
// of a 'void' type that can only occur as part of the signature of a
// function, but no general unit type of 0-sized value. This is, afaict,
// vestigial from its C heritage, and we'll be attempting to submit a
// patch upstream to fix it. In the mean time we only model function
// outputs (Rust functions and C functions) using T_void, and model the
// Rust general purpose nil type you can construct as 1-bit (always
// zero). This makes the result incorrect for now -- things like a tuple
// of 10 nil values will have 10-bit size -- but it doesn't seem like we
// have any other options until it's fixed upstream.
ret llvm.LLVMVoidType();
}
fn T_nil() -> TypeRef {
// NB: See above in T_void().
ret llvm.LLVMInt1Type();
}
fn T_i1() -> TypeRef {
ret llvm.LLVMInt1Type();
}
fn T_i8() -> TypeRef {
ret llvm.LLVMInt8Type();
}
fn T_i16() -> TypeRef {
ret llvm.LLVMInt16Type();
}
fn T_i32() -> TypeRef {
ret llvm.LLVMInt32Type();
}
fn T_i64() -> TypeRef {
ret llvm.LLVMInt64Type();
}
fn T_f32() -> TypeRef {
ret llvm.LLVMFloatType();
}
fn T_f64() -> TypeRef {
ret llvm.LLVMDoubleType();
}
fn T_bool() -> TypeRef {
ret T_i1();
}
fn T_int() -> TypeRef {
// FIXME: switch on target type.
ret T_i32();
}
fn T_float() -> TypeRef {
// FIXME: switch on target type.
ret T_f64();
}
fn T_char() -> TypeRef {
ret T_i32();
}
fn T_fn(vec[TypeRef] inputs, TypeRef output) -> TypeRef {
ret llvm.LLVMFunctionType(output,
_vec.buf[TypeRef](inputs),
_vec.len[TypeRef](inputs),
False);
}
fn T_fn_pair(type_names tn, TypeRef tfn) -> TypeRef {
ret T_struct(vec(T_ptr(tfn),
T_opaque_closure_ptr(tn)));
}
fn T_ptr(TypeRef t) -> TypeRef {
ret llvm.LLVMPointerType(t, 0u);
}
fn T_struct(vec[TypeRef] elts) -> TypeRef {
ret llvm.LLVMStructType(_vec.buf[TypeRef](elts),
_vec.len[TypeRef](elts),
False);
}
fn T_opaque() -> TypeRef {
ret llvm.LLVMOpaqueType();
}
fn T_task(type_names tn) -> TypeRef {
auto s = "task";
if (tn.name_has_type(s)) {
ret tn.get_type(s);
}
auto t = T_struct(vec(T_int(), // Refcount
T_int(), // Delegate pointer
T_int(), // Stack segment pointer
T_int(), // Runtime SP
T_int(), // Rust SP
T_int(), // GC chain
T_int(), // Domain pointer
T_int() // Crate cache pointer
));
tn.associate(s, t);
ret t;
}
fn T_tydesc_field(type_names tn, int field) -> TypeRef {
// Bit of a kludge: pick the fn typeref out of the tydesc..
let vec[TypeRef] tydesc_elts =
_vec.init_elt[TypeRef](T_nil(), abi.n_tydesc_fields as uint);
llvm.LLVMGetStructElementTypes(T_tydesc(tn),
_vec.buf[TypeRef](tydesc_elts));
auto t = llvm.LLVMGetElementType(tydesc_elts.(field));
ret t;
}
fn T_glue_fn(type_names tn) -> TypeRef {
auto s = "glue_fn";
if (tn.name_has_type(s)) {
ret tn.get_type(s);
}
auto t = T_tydesc_field(tn, abi.tydesc_field_drop_glue);
tn.associate(s, t);
ret t;
}
fn T_dtor(@crate_ctxt ccx, TypeRef llself_ty) -> TypeRef {
ret type_of_fn_full(ccx, ast.proto_fn, some[TypeRef](llself_ty),
_vec.empty[ty.arg](), ty.mk_nil(ccx.tcx), 0u);
}
fn T_cmp_glue_fn(type_names tn) -> TypeRef {
auto s = "cmp_glue_fn";
if (tn.name_has_type(s)) {
ret tn.get_type(s);
}
auto t = T_tydesc_field(tn, abi.tydesc_field_cmp_glue);
tn.associate(s, t);
ret t;
}
fn T_tydesc(type_names tn) -> TypeRef {
auto s = "tydesc";
if (tn.name_has_type(s)) {
ret tn.get_type(s);
}
auto th = mk_type_handle();
auto abs_tydesc = llvm.LLVMResolveTypeHandle(th.llth);
auto tydescpp = T_ptr(T_ptr(abs_tydesc));
auto pvoid = T_ptr(T_i8());
auto glue_fn_ty = T_ptr(T_fn(vec(T_ptr(T_nil()),
T_taskptr(tn),
T_ptr(T_nil()),
tydescpp,
pvoid), T_void()));
auto cmp_glue_fn_ty = T_ptr(T_fn(vec(T_ptr(T_i1()),
T_taskptr(tn),
T_ptr(T_nil()),
tydescpp,
pvoid,
pvoid,
T_i8()), T_void()));
auto tydesc = T_struct(vec(tydescpp, // first_param
T_int(), // size
T_int(), // align
glue_fn_ty, // take_glue
glue_fn_ty, // drop_glue
glue_fn_ty, // free_glue
glue_fn_ty, // sever_glue
glue_fn_ty, // mark_glue
glue_fn_ty, // obj_drop_glue
glue_fn_ty, // is_stateful
cmp_glue_fn_ty)); // cmp_glue
llvm.LLVMRefineType(abs_tydesc, tydesc);
auto t = llvm.LLVMResolveTypeHandle(th.llth);
tn.associate(s, t);
ret t;
}
fn T_array(TypeRef t, uint n) -> TypeRef {
ret llvm.LLVMArrayType(t, n);
}
fn T_vec(TypeRef t) -> TypeRef {
ret T_struct(vec(T_int(), // Refcount
T_int(), // Alloc
T_int(), // Fill
T_int(), // Pad
T_array(t, 0u) // Body elements
));
}
fn T_opaque_vec_ptr() -> TypeRef {
ret T_ptr(T_vec(T_int()));
}
fn T_str() -> TypeRef {
ret T_vec(T_i8());
}
fn T_box(TypeRef t) -> TypeRef {
ret T_struct(vec(T_int(), t));
}
fn T_port(TypeRef t) -> TypeRef {
ret T_struct(vec(T_int())); // Refcount
}
fn T_chan(TypeRef t) -> TypeRef {
ret T_struct(vec(T_int())); // Refcount
}
fn T_crate(type_names tn) -> TypeRef {
auto s = "crate";
if (tn.name_has_type(s)) {
ret tn.get_type(s);
}
auto t = T_struct(vec(T_int(), // ptrdiff_t image_base_off
T_int(), // uintptr_t self_addr
T_int(), // ptrdiff_t debug_abbrev_off
T_int(), // size_t debug_abbrev_sz
T_int(), // ptrdiff_t debug_info_off
T_int(), // size_t debug_info_sz
T_int(), // size_t activate_glue
T_int(), // size_t yield_glue
T_int(), // size_t unwind_glue
T_int(), // size_t gc_glue
T_int(), // size_t main_exit_task_glue
T_int(), // int n_rust_syms
T_int(), // int n_c_syms
T_int(), // int n_libs
T_int() // uintptr_t abi_tag
));
tn.associate(s, t);
ret t;
}
fn T_taskptr(type_names tn) -> TypeRef {
ret T_ptr(T_task(tn));
}
// This type must never be used directly; it must always be cast away.
fn T_typaram(type_names tn) -> TypeRef {
auto s = "typaram";
if (tn.name_has_type(s)) {
ret tn.get_type(s);
}
auto t = T_i8();
tn.associate(s, t);
ret t;
}
fn T_typaram_ptr(type_names tn) -> TypeRef {
ret T_ptr(T_typaram(tn));
}
fn T_closure_ptr(type_names tn,
TypeRef lltarget_ty,
TypeRef llbindings_ty,
uint n_ty_params) -> TypeRef {
// NB: keep this in sync with code in trans_bind; we're making
// an LLVM typeref structure that has the same "shape" as the ty.t
// it constructs.
ret T_ptr(T_box(T_struct(vec(T_ptr(T_tydesc(tn)),
lltarget_ty,
llbindings_ty,
T_captured_tydescs(tn, n_ty_params))
)));
}
fn T_opaque_closure_ptr(type_names tn) -> TypeRef {
auto s = "*closure";
if (tn.name_has_type(s)) {
ret tn.get_type(s);
}
auto t = T_closure_ptr(tn, T_struct(vec(T_ptr(T_nil()),
T_ptr(T_nil()))),
T_nil(),
0u);
tn.associate(s, t);
ret t;
}
fn T_tag(type_names tn, uint size) -> TypeRef {
auto s = "tag_" + _uint.to_str(size, 10u);
if (tn.name_has_type(s)) {
ret tn.get_type(s);
}
auto t = T_struct(vec(T_int(), T_array(T_i8(), size)));
tn.associate(s, t);
ret t;
}
fn T_opaque_tag(type_names tn) -> TypeRef {
auto s = "opaque_tag";
if (tn.name_has_type(s)) {
ret tn.get_type(s);
}
auto t = T_struct(vec(T_int(), T_i8()));
tn.associate(s, t);
ret t;
}
fn T_opaque_tag_ptr(type_names tn) -> TypeRef {
ret T_ptr(T_opaque_tag(tn));
}
fn T_captured_tydescs(type_names tn, uint n) -> TypeRef {
ret T_struct(_vec.init_elt[TypeRef](T_ptr(T_tydesc(tn)), n));
}
fn T_obj_ptr(type_names tn, uint n_captured_tydescs) -> TypeRef {
// This function is not publicly exposed because it returns an incomplete
// type. The dynamically-sized fields follow the captured tydescs.
fn T_obj(type_names tn, uint n_captured_tydescs) -> TypeRef {
ret T_struct(vec(T_ptr(T_tydesc(tn)),
T_captured_tydescs(tn, n_captured_tydescs)));
}
ret T_ptr(T_box(T_obj(tn, n_captured_tydescs)));
}
fn T_opaque_obj_ptr(type_names tn) -> TypeRef {
ret T_obj_ptr(tn, 0u);
}
// This function now fails if called on a type with dynamic size (as its
// return value was always meaningless in that case anyhow). Beware!
//
// TODO: Enforce via a predicate.
fn type_of(@crate_ctxt cx, ty.t t) -> TypeRef {
if (ty.type_has_dynamic_size(cx.tcx, t)) {
log_err "type_of() called on a type with dynamic size: " +
ty.ty_to_str(cx.tcx, t);
fail;
}
ret type_of_inner(cx, t);
}
fn type_of_explicit_args(@crate_ctxt cx, vec[ty.arg] inputs) -> vec[TypeRef] {
let vec[TypeRef] atys = vec();
for (ty.arg arg in inputs) {
if (ty.type_has_dynamic_size(cx.tcx, arg.ty)) {
assert (arg.mode == ast.alias);
atys += vec(T_typaram_ptr(cx.tn));
} else {
let TypeRef t;
alt (arg.mode) {
case (ast.alias) {
t = T_ptr(type_of_inner(cx, arg.ty));
}
case (_) {
t = type_of_inner(cx, arg.ty);
}
}
atys += vec(t);
}
}
ret atys;
}
// NB: must keep 4 fns in sync:
//
// - type_of_fn_full
// - create_llargs_for_fn_args.
// - new_fn_ctxt
// - trans_args
fn type_of_fn_full(@crate_ctxt cx,
ast.proto proto,
option.t[TypeRef] obj_self,
vec[ty.arg] inputs,
ty.t output,
uint ty_param_count) -> TypeRef {
let vec[TypeRef] atys = vec();
// Arg 0: Output pointer.
if (ty.type_has_dynamic_size(cx.tcx, output)) {
atys += vec(T_typaram_ptr(cx.tn));
} else {
atys += vec(T_ptr(type_of_inner(cx, output)));
}
// Arg 1: Task pointer.
atys += vec(T_taskptr(cx.tn));
// Arg 2: Env (closure-bindings / self-obj)
alt (obj_self) {
case (some[TypeRef](?t)) {
assert (t as int != 0);
atys += vec(t);
}
case (_) {
atys += vec(T_opaque_closure_ptr(cx.tn));
}
}
// Args >3: ty params, if not acquired via capture...
if (obj_self == none[TypeRef]) {
auto i = 0u;
while (i < ty_param_count) {
atys += vec(T_ptr(T_tydesc(cx.tn)));
i += 1u;
}
}
if (proto == ast.proto_iter) {
// If it's an iter, the 'output' type of the iter is actually the
// *input* type of the function we're given as our iter-block
// argument.
atys +=
vec(T_fn_pair(cx.tn,
type_of_fn_full(cx, ast.proto_fn, none[TypeRef],
vec(rec(mode=ast.alias, ty=output)),
ty.mk_nil(cx.tcx), 0u)));
}
// ... then explicit args.
atys += type_of_explicit_args(cx, inputs);
ret T_fn(atys, llvm.LLVMVoidType());
}
fn type_of_fn(@crate_ctxt cx,
ast.proto proto,
vec[ty.arg] inputs,
ty.t output,
uint ty_param_count) -> TypeRef {
ret type_of_fn_full(cx, proto, none[TypeRef], inputs, output,
ty_param_count);
}
fn type_of_native_fn(@crate_ctxt cx, ast.native_abi abi,
vec[ty.arg] inputs,
ty.t output,
uint ty_param_count) -> TypeRef {
let vec[TypeRef] atys = vec();
if (abi == ast.native_abi_rust) {
atys += vec(T_taskptr(cx.tn));
auto t = ty.ty_native_fn(abi, inputs, output);
auto i = 0u;
while (i < ty_param_count) {
atys += vec(T_ptr(T_tydesc(cx.tn)));
i += 1u;
}
}
atys += type_of_explicit_args(cx, inputs);
ret T_fn(atys, type_of_inner(cx, output));
}
fn type_of_inner(@crate_ctxt cx, ty.t t) -> TypeRef {
// Check the cache.
if (cx.lltypes.contains_key(t)) {
ret cx.lltypes.get(t);
}
let TypeRef llty = 0 as TypeRef;
alt (ty.struct(cx.tcx, t)) {
case (ty.ty_native) { llty = T_ptr(T_i8()); }
case (ty.ty_nil) { llty = T_nil(); }
case (ty.ty_bool) { llty = T_bool(); }
case (ty.ty_int) { llty = T_int(); }
case (ty.ty_float) { llty = T_float(); }
case (ty.ty_uint) { llty = T_int(); }
case (ty.ty_machine(?tm)) {
alt (tm) {
case (common.ty_i8) { llty = T_i8(); }
case (common.ty_u8) { llty = T_i8(); }
case (common.ty_i16) { llty = T_i16(); }
case (common.ty_u16) { llty = T_i16(); }
case (common.ty_i32) { llty = T_i32(); }
case (common.ty_u32) { llty = T_i32(); }
case (common.ty_i64) { llty = T_i64(); }
case (common.ty_u64) { llty = T_i64(); }
case (common.ty_f32) { llty = T_f32(); }
case (common.ty_f64) { llty = T_f64(); }
}
}
case (ty.ty_char) { llty = T_char(); }
case (ty.ty_str) { llty = T_ptr(T_str()); }
case (ty.ty_tag(_, _)) {
if (ty.type_has_dynamic_size(cx.tcx, t)) {
llty = T_opaque_tag(cx.tn);
} else {
auto size = static_size_of_tag(cx, t);
llty = T_tag(cx.tn, size);
}
}
case (ty.ty_box(?mt)) {
llty = T_ptr(T_box(type_of_inner(cx, mt.ty)));
}
case (ty.ty_vec(?mt)) {
llty = T_ptr(T_vec(type_of_inner(cx, mt.ty)));
}
case (ty.ty_port(?t)) {
llty = T_ptr(T_port(type_of_inner(cx, t)));
}
case (ty.ty_chan(?t)) {
llty = T_ptr(T_chan(type_of_inner(cx, t)));
}
case (ty.ty_tup(?elts)) {
let vec[TypeRef] tys = vec();
for (ty.mt elt in elts) {
tys += vec(type_of_inner(cx, elt.ty));
}
llty = T_struct(tys);
}
case (ty.ty_rec(?fields)) {
let vec[TypeRef] tys = vec();
for (ty.field f in fields) {
tys += vec(type_of_inner(cx, f.mt.ty));
}
llty = T_struct(tys);
}
case (ty.ty_fn(?proto, ?args, ?out)) {
llty = T_fn_pair(cx.tn, type_of_fn(cx, proto, args, out, 0u));
}
case (ty.ty_native_fn(?abi, ?args, ?out)) {
auto nft = native_fn_wrapper_type(cx, 0u, t);
llty = T_fn_pair(cx.tn, nft);
}
case (ty.ty_obj(?meths)) {
auto th = mk_type_handle();
auto self_ty = llvm.LLVMResolveTypeHandle(th.llth);
let vec[TypeRef] mtys = vec(T_ptr(T_i8()));
for (ty.method m in meths) {
let TypeRef mty =
type_of_fn_full(cx, m.proto,
some[TypeRef](self_ty),
m.inputs, m.output, 0u);
mtys += vec(T_ptr(mty));
}
let TypeRef vtbl = T_struct(mtys);
let TypeRef pair = T_struct(vec(T_ptr(vtbl),
T_opaque_obj_ptr(cx.tn)));
auto abs_pair = llvm.LLVMResolveTypeHandle(th.llth);
llvm.LLVMRefineType(abs_pair, pair);
abs_pair = llvm.LLVMResolveTypeHandle(th.llth);
llty = abs_pair;
}
case (ty.ty_var(_)) {
log_err "ty_var in trans.type_of";
fail;
}
case (ty.ty_param(_)) {
llty = T_i8();
}
case (ty.ty_bound_param(_)) {
log_err "ty_bound_param in trans.type_of";
fail;
}
case (ty.ty_type) { llty = T_ptr(T_tydesc(cx.tn)); }
}
assert (llty as int != 0);
llvm.LLVMAddTypeName(cx.llmod, _str.buf(ty.ty_to_short_str(cx.tcx, t)),
llty);
cx.lltypes.insert(t, llty);
ret llty;
}
fn type_of_arg(@local_ctxt cx, &ty.arg arg) -> TypeRef {
alt (ty.struct(cx.ccx.tcx, arg.ty)) {
case (ty.ty_param(_)) {
if (arg.mode == ast.alias) {
ret T_typaram_ptr(cx.ccx.tn);
}
}
case (_) {
// fall through
}
}
auto typ;
if (arg.mode == ast.alias) {
typ = T_ptr(type_of_inner(cx.ccx, arg.ty));
} else {
typ = type_of_inner(cx.ccx, arg.ty);
}
ret typ;
}
fn type_of_ty_param_count_and_ty(@local_ctxt lcx,
ty.ty_param_count_and_ty tpt) -> TypeRef {
alt (ty.struct(lcx.ccx.tcx, tpt._1)) {
case (ty.ty_fn(?proto, ?inputs, ?output)) {
auto llfnty = type_of_fn(lcx.ccx, proto, inputs, output, tpt._0);
ret T_fn_pair(lcx.ccx.tn, llfnty);
}
case (_) {
// fall through
}
}
ret type_of(lcx.ccx, tpt._1);
}
// Name sanitation. LLVM will happily accept identifiers with weird names, but
// gas doesn't!
fn sanitize(str s) -> str {
auto result = "";
for (u8 c in s) {
if (c == ('@' as u8)) {
result += "boxed_";
} else {
if (c == (',' as u8)) {
result += "_";
} else {
if (c == ('{' as u8) || c == ('(' as u8)) {
result += "_of_";
} else {
if (c != 10u8 && c != ('}' as u8) && c != (')' as u8) &&
c != (' ' as u8) && c != ('\t' as u8) &&
c != (';' as u8)) {
auto v = vec(c);
result += _str.from_bytes(v);
}
}
}
}
}
ret result;
}
// LLVM constant constructors.
fn C_null(TypeRef t) -> ValueRef {
ret llvm.LLVMConstNull(t);
}
fn C_integral(int i, TypeRef t) -> ValueRef {
// FIXME. We can't use LLVM.ULongLong with our existing minimal native
// API, which only knows word-sized args. Lucky for us LLVM has a "take a
// string encoding" version. Hilarious. Please fix to handle:
//
// ret llvm.LLVMConstInt(T_int(), t as LLVM.ULongLong, False);
//
ret llvm.LLVMConstIntOfString(t, _str.buf(istr(i)), 10);
}
fn C_float(str s) -> ValueRef {
ret llvm.LLVMConstRealOfString(T_float(), _str.buf(s));
}
fn C_floating(str s, TypeRef t) -> ValueRef {
ret llvm.LLVMConstRealOfString(t, _str.buf(s));
}
fn C_nil() -> ValueRef {
// NB: See comment above in T_void().
ret C_integral(0, T_i1());
}
fn C_bool(bool b) -> ValueRef {
if (b) {
ret C_integral(1, T_bool());
} else {
ret C_integral(0, T_bool());
}
}
fn C_int(int i) -> ValueRef {
ret C_integral(i, T_int());
}
fn C_i8(uint i) -> ValueRef {
ret C_integral(i as int, T_i8());
}
// This is a 'c-like' raw string, which differs from
// our boxed-and-length-annotated strings.
fn C_cstr(@crate_ctxt cx, str s) -> ValueRef {
auto sc = llvm.LLVMConstString(_str.buf(s), _str.byte_len(s), False);
auto g = llvm.LLVMAddGlobal(cx.llmod, val_ty(sc),
_str.buf(cx.names.next("str")));
llvm.LLVMSetInitializer(g, sc);
llvm.LLVMSetGlobalConstant(g, True);
llvm.LLVMSetLinkage(g, lib.llvm.LLVMInternalLinkage
as llvm.Linkage);
ret g;
}
// A rust boxed-and-length-annotated string.
fn C_str(@crate_ctxt cx, str s) -> ValueRef {
auto len = _str.byte_len(s);
auto box = C_struct(vec(C_int(abi.const_refcount as int),
C_int(len + 1u as int), // 'alloc'
C_int(len + 1u as int), // 'fill'
C_int(0), // 'pad'
llvm.LLVMConstString(_str.buf(s),
len, False)));
auto g = llvm.LLVMAddGlobal(cx.llmod, val_ty(box),
_str.buf(cx.names.next("str")));
llvm.LLVMSetInitializer(g, box);
llvm.LLVMSetGlobalConstant(g, True);
llvm.LLVMSetLinkage(g, lib.llvm.LLVMInternalLinkage
as llvm.Linkage);
ret llvm.LLVMConstPointerCast(g, T_ptr(T_str()));
}
fn C_zero_byte_arr(uint size) -> ValueRef {
auto i = 0u;
let vec[ValueRef] elts = vec();
while (i < size) {
elts += vec(C_integral(0, T_i8()));
i += 1u;
}
ret llvm.LLVMConstArray(T_i8(), _vec.buf[ValueRef](elts),
_vec.len[ValueRef](elts));
}
fn C_struct(vec[ValueRef] elts) -> ValueRef {
ret llvm.LLVMConstStruct(_vec.buf[ValueRef](elts),
_vec.len[ValueRef](elts),
False);
}
fn C_array(TypeRef ty, vec[ValueRef] elts) -> ValueRef {
ret llvm.LLVMConstArray(ty, _vec.buf[ValueRef](elts),
_vec.len[ValueRef](elts));
}
fn decl_fn(ModuleRef llmod, str name, uint cc, TypeRef llty) -> ValueRef {
let ValueRef llfn =
llvm.LLVMAddFunction(llmod, _str.buf(name), llty);
llvm.LLVMSetFunctionCallConv(llfn, cc);
ret llfn;
}
fn decl_cdecl_fn(ModuleRef llmod, str name, TypeRef llty) -> ValueRef {
ret decl_fn(llmod, name, lib.llvm.LLVMCCallConv, llty);
}
fn decl_fastcall_fn(ModuleRef llmod, str name, TypeRef llty) -> ValueRef {
ret decl_fn(llmod, name, lib.llvm.LLVMFastCallConv, llty);
}
fn decl_internal_fastcall_fn(ModuleRef llmod,