@@ -12,7 +12,9 @@ import driver.session;
12
12
import back. x86 ;
13
13
import back. abi ;
14
14
15
+ import util. common ;
15
16
import util. common . istr ;
17
+ import util. common . new_def_hash ;
16
18
import util. common . new_str_hash ;
17
19
18
20
import lib. llvm . llvm ;
@@ -49,6 +51,7 @@ state type trans_ctxt = rec(session.session sess,
49
51
state type fn_ctxt = rec ( ValueRef llfn,
50
52
ValueRef lloutptr,
51
53
ValueRef lltaskptr ,
54
+ hashmap[ ast. def_id, ValueRef ] lllocals ,
52
55
@trans_ctxt tcx ) ;
53
56
54
57
type terminator = fn ( @fn_ctxt cx , builder build) ;
@@ -126,11 +129,23 @@ fn T_i64() -> TypeRef {
126
129
ret llvm. LLVMInt64Type ( ) ;
127
130
}
128
131
132
+ fn T_f32 ( ) -> TypeRef {
133
+ ret llvm. LLVMFloatType ( ) ;
134
+ }
135
+
136
+ fn T_f64 ( ) -> TypeRef {
137
+ ret llvm. LLVMDoubleType ( ) ;
138
+ }
139
+
129
140
fn T_int ( ) -> TypeRef {
130
141
// FIXME: switch on target type.
131
142
ret T_i32 ( ) ;
132
143
}
133
144
145
+ fn T_char ( ) -> TypeRef {
146
+ ret T_i32 ( ) ;
147
+ }
148
+
134
149
fn T_fn ( vec[ TypeRef ] inputs , TypeRef output) -> TypeRef {
135
150
ret llvm. LLVMFunctionType ( output,
136
151
_vec. buf [ TypeRef ] ( inputs) ,
@@ -180,6 +195,10 @@ fn T_str(uint n) -> TypeRef {
180
195
ret T_vec ( T_i8 ( ) , n) ;
181
196
}
182
197
198
+ fn T_box ( TypeRef t) -> TypeRef {
199
+ ret T_struct ( vec ( T_int ( ) , t) ) ;
200
+ }
201
+
183
202
fn T_crate ( ) -> TypeRef {
184
203
ret T_struct ( vec ( T_int ( ) , // ptrdiff_t image_base_off
185
204
T_int ( ) , // uintptr_t self_addr
@@ -206,6 +225,49 @@ fn T_taskptr() -> TypeRef {
206
225
ret T_ptr ( T_task ( ) ) ;
207
226
}
208
227
228
+ fn type_of ( @trans_ctxt cx , @ast. ty t ) -> TypeRef {
229
+ alt ( t. node ) {
230
+ case ( ast. ty_nil ) { ret T_nil ( ) ; }
231
+ case ( ast. ty_bool ) { ret T_i1 ( ) ; }
232
+ case ( ast. ty_int ) { ret T_int ( ) ; }
233
+ case ( ast. ty_uint ) { ret T_int ( ) ; }
234
+ case ( ast. ty_machine ( ?tm) ) {
235
+ alt ( tm) {
236
+ case ( common. ty_i8 ) { ret T_i8 ( ) ; }
237
+ case ( common. ty_u8 ) { ret T_i8 ( ) ; }
238
+ case ( common. ty_i16 ) { ret T_i16 ( ) ; }
239
+ case ( common. ty_u16 ) { ret T_i16 ( ) ; }
240
+ case ( common. ty_i32 ) { ret T_i32 ( ) ; }
241
+ case ( common. ty_u32 ) { ret T_i32 ( ) ; }
242
+ case ( common. ty_i64 ) { ret T_i64 ( ) ; }
243
+ case ( common. ty_u64 ) { ret T_i64 ( ) ; }
244
+ case ( common. ty_f32 ) { ret T_f32 ( ) ; }
245
+ case ( common. ty_f64 ) { ret T_f64 ( ) ; }
246
+ }
247
+ }
248
+ case ( ast. ty_char ) { ret T_char ( ) ; }
249
+ case ( ast. ty_str ) { ret T_str ( 0 u) ; }
250
+ case ( ast. ty_box ( ?t) ) {
251
+ ret T_ptr ( T_box ( type_of ( cx, t) ) ) ;
252
+ }
253
+ case ( ast. ty_vec ( ?t) ) {
254
+ ret T_ptr ( T_vec ( type_of ( cx, t) , 0 u) ) ;
255
+ }
256
+ case ( ast. ty_tup ( ?elts) ) {
257
+ let vec[ TypeRef ] tys = vec ( ) ;
258
+ for ( tup( bool, @ast. ty) elt in elts) {
259
+ tys += type_of ( cx, elt. _1 ) ;
260
+ }
261
+ ret T_struct ( tys) ;
262
+ }
263
+ case ( ast. ty_path ( ?pth, ?def) ) {
264
+ // FIXME: implement.
265
+ cx. sess . unimpl ( "ty_path in trans.type_of" ) ;
266
+ }
267
+ }
268
+ fail;
269
+ }
270
+
209
271
// LLVM constant constructors.
210
272
211
273
fn C_null ( TypeRef t) -> ValueRef {
@@ -356,7 +418,7 @@ fn trans_lit(@block_ctxt cx, &ast.lit lit) -> result {
356
418
ret res ( cx, C_int ( u as int ) ) ;
357
419
}
358
420
case ( ast. lit_char ( ?c) ) {
359
- ret res ( cx, C_integral ( c as int , T_i32 ( ) ) ) ;
421
+ ret res ( cx, C_integral ( c as int , T_char ( ) ) ) ;
360
422
}
361
423
case ( ast. lit_bool ( ?b) ) {
362
424
ret res ( cx, C_bool ( b) ) ;
@@ -572,6 +634,25 @@ fn trans_expr(@block_ctxt cx, &ast.expr e) -> result {
572
634
573
635
ret res( next_cx, sub. val ) ;
574
636
}
637
+
638
+ case ( ast. expr_name ( ?n, ?dopt, _) ) {
639
+ alt ( dopt) {
640
+ case ( some[ ast. def ] ( ?def) ) {
641
+ alt ( def) {
642
+ case ( ast. def_local ( ?did) ) {
643
+ auto llptr = cx. fcx . lllocals . get ( did) ;
644
+ ret res ( cx, cx. build . Load ( llptr) ) ;
645
+ }
646
+ case ( _) {
647
+ cx. fcx . tcx . sess . unimpl ( "def variant in trans" ) ;
648
+ }
649
+ }
650
+ }
651
+ case ( none[ ast. def ] ) {
652
+ cx. fcx . tcx . sess . err ( "unresolved expr_name in trans" ) ;
653
+ }
654
+ }
655
+ }
575
656
}
576
657
cx. fcx . tcx . sess . unimpl ( "expr variant in trans_expr" ) ;
577
658
fail;
@@ -616,6 +697,20 @@ fn trans_stmt(@block_ctxt cx, &ast.stmt s) -> result {
616
697
sub. bcx = trans_expr ( cx, * e) . bcx ;
617
698
}
618
699
700
+ case ( ast. stmt_decl ( ?d) ) {
701
+ alt ( d. node ) {
702
+ case ( ast. decl_local ( ?local) ) {
703
+ alt ( local. init ) {
704
+ case ( some[ @ast. expr ] ( ?e) ) {
705
+ log "storing init of local " + local. ident ;
706
+ auto llptr = cx. fcx . lllocals . get ( local. id ) ;
707
+ sub = trans_expr ( cx, * e) ;
708
+ sub. val = sub. bcx . build . Store ( sub. val , llptr) ;
709
+ }
710
+ }
711
+ }
712
+ }
713
+ }
619
714
case ( _) {
620
715
cx. fcx . tcx . sess . unimpl ( "stmt variant" ) ;
621
716
}
@@ -685,9 +780,41 @@ fn trans_block_cleanups(@block_ctxt cx) -> @block_ctxt {
685
780
ret bcx;
686
781
}
687
782
783
+ iter block_locals( & ast. block b) -> @ast. local {
784
+ // FIXME: putting from inside an iter block doesn't work, so we can't
785
+ // use the index here.
786
+ for ( @ast. stmt s in b. node. stmts) {
787
+ alt ( s. node) {
788
+ case ( ast. stmt_decl( ?d) ) {
789
+ alt ( d. node) {
790
+ case ( ast. decl_local( ?local) ) {
791
+ put local;
792
+ }
793
+ }
794
+ }
795
+ }
796
+ }
797
+ }
798
+
688
799
fn trans_block( @block_ctxt cx, & ast. block b) -> result {
689
800
auto bcx = cx;
690
801
802
+ for each ( @ast. local local in block_locals( b) ) {
803
+ log "declaring local " + local. ident;
804
+ auto ty = T_nil ( ) ;
805
+ alt ( local. ty) {
806
+ case ( some[ @ast. ty] ( ?t) ) {
807
+ ty = type_of( cx. fcx. tcx, t) ;
808
+ }
809
+ case ( none[ @ast. ty] ) {
810
+ cx. fcx. tcx. sess. err( "missing type for local " + local. ident) ;
811
+ }
812
+ }
813
+ auto val = bcx. build. Alloca ( ty) ;
814
+ log "built alloca: " + val_str( val) ;
815
+ cx. fcx. lllocals. insert( local. id, val) ;
816
+ }
817
+
691
818
for ( @ast. stmt s in b. node . stmts ) {
692
819
bcx = trans_stmt ( bcx, * s) . bcx ;
693
820
}
@@ -709,9 +836,11 @@ fn new_fn_ctxt(@trans_ctxt cx,
709
836
cx. fns . insert ( cx. path , llfn) ;
710
837
let ValueRef lloutptr = llvm. LLVMGetParam ( llfn, 0 u) ;
711
838
let ValueRef lltaskptr = llvm. LLVMGetParam ( llfn, 1 u) ;
839
+ let hashmap[ ast. def_id , ValueRef ] lllocals = new_def_hash[ ValueRef ] ( ) ;
712
840
ret @rec( llfn=llfn,
713
841
lloutptr=lloutptr,
714
842
lltaskptr=lltaskptr,
843
+ lllocals=lllocals,
715
844
tcx=cx) ;
716
845
}
717
846
@@ -758,6 +887,7 @@ fn trans_exit_task_glue(@trans_ctxt cx) {
758
887
auto fcx = @rec ( llfn=llfn,
759
888
lloutptr=lloutptr,
760
889
lltaskptr=lltaskptr,
890
+ lllocals=new_def_hash[ ValueRef ] ( ) ,
761
891
tcx=cx) ;
762
892
763
893
auto bcx = new_top_block_ctxt ( fcx) ;
0 commit comments