Skip to content

Commit cdf67b1

Browse files
committed
Better backpointer logic.
1 parent 246e72b commit cdf67b1

File tree

4 files changed

+65
-19
lines changed

4 files changed

+65
-19
lines changed

src/boot/be/il.ml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,9 +903,20 @@ let get_element_ptr
903903
(string_of_cell fmt mem_cell)
904904
;;
905905

906-
let ptr_cast (cell:cell) (rty:referent_ty) : cell =
906+
let cell_cast (cell:cell) (rty:referent_ty) : cell =
907907
match cell with
908908
Mem (mem, _) -> Mem (mem, rty)
909+
| Reg (reg, _) ->
910+
begin
911+
match rty with
912+
ScalarTy st -> Reg (reg, st)
913+
| _ -> bug () "expected scalar type in Il.cell_cast on register"
914+
end
915+
916+
917+
let ptr_cast (cell:cell) (rty:referent_ty) : cell =
918+
match cell with
919+
Mem (mem, ScalarTy (AddrTy _)) -> Mem (mem, ScalarTy (AddrTy rty))
909920
| Reg (reg, AddrTy _) -> Reg (reg, AddrTy rty)
910921
| _ -> bug () "expected address cell in Il.ptr_cast"
911922
;;

src/boot/be/x86.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,17 +1078,17 @@ let rec calculate_sz
10781078

10791079
mov (rc eax) (Il.Cell closure_ptr);
10801080
let obj_body = word_n (h eax) Abi.box_rc_field_body in
1081-
let obj_body = Il.ptr_cast obj_body obj_box_rty in
1081+
let obj_body = Il.cell_cast obj_body obj_box_rty in
10821082
let tydesc_ptr = get_element_ptr obj_body Abi.obj_body_elt_tydesc in
10831083

10841084
mov (rc eax) (Il.Cell tydesc_ptr);
1085-
let tydesc = Il.ptr_cast (word_at (h eax)) tydesc_rty in
1085+
let tydesc = Il.cell_cast (word_at (h eax)) tydesc_rty in
10861086
let ty_params_ptr =
10871087
get_element_ptr tydesc Abi.tydesc_field_first_param
10881088
in
10891089

10901090
mov (rc eax) (Il.Cell ty_params_ptr);
1091-
let ty_params = Il.ptr_cast (word_at (h eax)) ty_params_rty in
1091+
let ty_params = Il.cell_cast (word_at (h eax)) ty_params_rty in
10921092
get_element_ptr ty_params i
10931093
in
10941094

src/boot/me/semant.ml

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,27 +2142,33 @@ and vec_sty (word_bits:Il.bits) : Il.scalar_ty =
21422142
let ptr = Il.ScalarTy (Il.AddrTy Il.OpaqueTy) in
21432143
Il.AddrTy (Il.StructTy [| word; word; word; ptr |])
21442144

2145-
and referent_type (cx:ctxt) (t:Ast.ty) : Il.referent_ty =
2145+
and referent_type
2146+
?parent_tags:parent_tags
2147+
?boxed:(boxed=false)
2148+
(cx:ctxt)
2149+
(t:Ast.ty)
2150+
: Il.referent_ty =
21462151
let s t = Il.ScalarTy t in
21472152
let v b = Il.ValTy b in
21482153
let p t = Il.AddrTy t in
21492154
let sv b = s (v b) in
21502155
let sp t = s (p t) in
2156+
let recur ty = referent_type ?parent_tags ~boxed cx ty in
21512157

21522158
let word_bits = cx.ctxt_abi.Abi.abi_word_bits in
21532159
let word = word_rty word_bits in
21542160
let ptr = sp Il.OpaqueTy in
21552161
let rc_ptr = sp (Il.StructTy [| word; Il.OpaqueTy |]) in
2156-
let tup ttup = Il.StructTy (Array.map (referent_type cx) ttup) in
2162+
let tup ttup = Il.StructTy (Array.map recur ttup) in
21572163
let tag ttag =
21582164
let n = get_n_tag_tups cx ttag in
21592165
let union =
2160-
let rty t =
2161-
match t with
2162-
Ast.TY_box (Ast.TY_tag dst_tag) when is_back_edge ttag dst_tag ->
2163-
sp (Il.StructTy [| word; Il.OpaqueTy |])
2164-
| _ -> referent_type cx t
2166+
let parent_tags =
2167+
match parent_tags with
2168+
None -> [ttag]
2169+
| Some pts -> ttag::pts
21652170
in
2171+
let rty t = referent_type ~parent_tags ~boxed cx t in
21662172
let tup ttup = Il.StructTy (Array.map rty ttup) in
21672173
Array.init n (fun i -> tup (get_nth_tag_tup cx ttag i))
21682174
in
@@ -2202,7 +2208,17 @@ and referent_type (cx:ctxt) (t:Ast.ty) : Il.referent_ty =
22022208
| Ast.TY_fn _ -> fn_rty cx false
22032209
| Ast.TY_obj _ -> obj_rty word_bits
22042210

2205-
| Ast.TY_tag ttag -> tag ttag
2211+
| Ast.TY_tag ttag ->
2212+
begin
2213+
match parent_tags with
2214+
Some parent_tags
2215+
when boxed
2216+
&& parent_tags <> []
2217+
&& List.mem ttag parent_tags
2218+
&& is_back_edge ttag (List.hd parent_tags) ->
2219+
Il.StructTy [| word; Il.OpaqueTy |]
2220+
| _ -> tag ttag
2221+
end
22062222

22072223
| Ast.TY_chan _
22082224
| Ast.TY_port _
@@ -2213,14 +2229,15 @@ and referent_type (cx:ctxt) (t:Ast.ty) : Il.referent_ty =
22132229
| Ast.TY_native _ -> ptr
22142230

22152231
| Ast.TY_box t ->
2216-
sp (Il.StructTy [| word; referent_type cx t |])
2232+
sp (Il.StructTy
2233+
[| word; referent_type ?parent_tags ~boxed:true cx t |])
22172234

2218-
| Ast.TY_mutable t -> referent_type cx t
2235+
| Ast.TY_mutable t -> recur t
22192236

22202237
| Ast.TY_param (i, _) -> Il.ParamTy i
22212238

22222239
| Ast.TY_named _ -> bug () "named type in referent_type"
2223-
| Ast.TY_constrained (t, _) -> referent_type cx t
2240+
| Ast.TY_constrained (t, _) -> recur t
22242241

22252242
and slot_referent_type (cx:ctxt) (sl:Ast.slot) : Il.referent_ty =
22262243
let s t = Il.ScalarTy t in

src/boot/me/trans.ml

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ let trans_visitor
337337

338338
let rec ptr_cast = Il.ptr_cast
339339

340+
and cell_cast = Il.cell_cast
341+
340342
and curr_crate_ptr _ : Il.cell =
341343
word_at (fp_imm frame_crate_ptr)
342344

@@ -465,7 +467,7 @@ let trans_visitor
465467
let indirect_args =
466468
get_element_ptr args_cell Abi.calltup_elt_indirect_args
467469
in
468-
deref (ptr_cast
470+
deref (cell_cast
469471
(get_element_ptr indirect_args Abi.indirect_args_elt_closure)
470472
(Il.ScalarTy (Il.AddrTy (obj_box_rty word_bits))))
471473
in
@@ -508,7 +510,7 @@ let trans_visitor
508510
get_element_ptr (deref tydesc) Abi.tydesc_field_first_param
509511
in
510512
let ty_params =
511-
ptr_cast ty_params (Il.ScalarTy (Il.AddrTy ty_params_rty))
513+
cell_cast ty_params (Il.ScalarTy (Il.AddrTy ty_params_rty))
512514
in
513515
deref ty_params
514516
else
@@ -550,7 +552,7 @@ let trans_visitor
550552
let blk_fn = get_element_ptr self_iterator_args
551553
Abi.iterator_args_elt_block_fn
552554
in
553-
ptr_cast blk_fn
555+
cell_cast blk_fn
554556
(Il.ScalarTy (Il.AddrTy Il.CodeTy))
555557
in
556558

@@ -3223,13 +3225,27 @@ let trans_visitor
32233225
iter_ty_parts_full ty_params dst src ty
32243226
(clone_ty ty_params clone_task)
32253227

3228+
and unfold_opaque_cell (c:Il.cell) (ty:Ast.ty) : Il.cell =
3229+
match Il.cell_referent_ty c with
3230+
Il.ScalarTy (Il.AddrTy _) ->
3231+
begin
3232+
match strip_mutable_or_constrained_ty ty with
3233+
Ast.TY_box boxed ->
3234+
Il.ptr_cast c
3235+
(Il.StructTy [| word_rty; referent_type cx boxed |])
3236+
| _ -> c
3237+
end
3238+
| _ -> c
3239+
32263240
and free_ty
32273241
(is_gc:bool)
32283242
(ty_params:Il.cell)
32293243
(ty:Ast.ty)
32303244
(cell:Il.cell)
32313245
: unit =
32323246
check_box_rty cell;
3247+
let cell = unfold_opaque_cell cell ty in
3248+
check_box_rty cell;
32333249
note_drop_step ty "in free-ty";
32343250
begin
32353251
match strip_mutable_or_constrained_ty ty with
@@ -3463,6 +3479,8 @@ let trans_visitor
34633479

34643480
| (Ast.TY_box ty', DEREF_one_box)
34653481
| (Ast.TY_box ty', DEREF_all_boxes) ->
3482+
check_box_rty cell;
3483+
let cell = unfold_opaque_cell cell ty in
34663484
check_box_rty cell;
34673485
if initializing
34683486
then init_box cell ty;
@@ -4092,7 +4110,7 @@ let trans_visitor
40924110
in
40934111
let pair_code_cell = get_element_ptr dst_cell Abi.fn_field_code in
40944112
let pair_box_cell =
4095-
ptr_cast
4113+
cell_cast
40964114
(get_element_ptr dst_cell Abi.fn_field_box)
40974115
(Il.ScalarTy (Il.AddrTy (closure_box_rty)))
40984116
in

0 commit comments

Comments
 (0)