Skip to content

Commit 2c48c7d

Browse files
authored
Merge pull request #3773 from BuckleScript/fix_tail
Refine tail position property
2 parents 89fc213 + 817b5d3 commit 2c48c7d

File tree

8 files changed

+309
-207
lines changed

8 files changed

+309
-207
lines changed

jscomp/core/js_output.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ let output_of_expression
5454
(continuation : continuation)
5555
(exp : J.expression) ~(no_effects: bool Lazy.t) =
5656
match continuation with
57-
| EffectCall ReturnFalse ->
57+
| EffectCall Not_tail ->
5858
if Lazy.force no_effects
5959
then dummy
6060
else {block = []; value = Some exp ; output_finished = False}
6161
| Declare (kind, n)->
6262
make [ S.define_variable ~kind n exp]
6363
| Assign n ->
6464
make [S.assign n exp ]
65-
| EffectCall (ReturnTrue _) ->
65+
| EffectCall (Maybe_tail_is_return _) ->
6666
make [S.return_stmt exp] ~output_finished:True
6767
| NeedValue _ ->
6868
{block = []; value = Some exp; output_finished = False }
@@ -72,8 +72,8 @@ let output_of_block_and_expression
7272
(continuation : continuation)
7373
(block : J.block) exp : t =
7474
match continuation with
75-
| EffectCall ReturnFalse -> make block ~value:exp
76-
| EffectCall (ReturnTrue _) ->
75+
| EffectCall Not_tail -> make block ~value:exp
76+
| EffectCall (Maybe_tail_is_return _) ->
7777
make (Ext_list.append_one block (S.return_stmt exp)) ~output_finished:True
7878
| Declare (kind,n) ->
7979
make (Ext_list.append_one block (S.define_variable ~kind n exp))

jscomp/core/lam_compile.ml

Lines changed: 61 additions & 42 deletions
Large diffs are not rendered by default.

jscomp/core/lam_compile_context.ml

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@ type return_label = {
4646
mutable triggered : bool
4747
}
4848

49-
type return_type =
50-
| ReturnFalse
51-
| ReturnTrue of return_label option
49+
type maybe_tail =
50+
| Tail_in_try
51+
| Tail_with_name of return_label option
52+
53+
type tail_type =
54+
| Not_tail
55+
| Maybe_tail_is_return of maybe_tail
5256
(* Note [return] does indicate it is a tail position in most cases
5357
however, in an exception handler, return may not be in tail position
54-
to fix #1701 we play a trick that (ReturnTrue None)
58+
to fix #1701 we play a trick that (Maybe_tail_is_return None)
5559
would never trigger tailcall, however, it preserves [return]
5660
semantics
5761
*)
@@ -61,18 +65,18 @@ type return_type =
6165
type let_kind = Lam_compat.let_kind
6266

6367
type continuation =
64-
| EffectCall of return_type
65-
| NeedValue of return_type
68+
| EffectCall of tail_type
69+
| NeedValue of tail_type
6670
| Declare of let_kind * J.ident (* bound value *)
6771
| Assign of J.ident (* when use [Assign], var is not needed, since it's already declared *)
6872

6973
type jmp_table = value HandlerMap.t
7074

7175
let continuation_is_return ( x : continuation) =
7276
match x with
73-
| EffectCall (ReturnTrue _) | NeedValue (ReturnTrue _)
77+
| EffectCall (Maybe_tail_is_return _) | NeedValue (Maybe_tail_is_return _)
7478
-> true
75-
| EffectCall ReturnFalse | NeedValue ReturnFalse
79+
| EffectCall Not_tail | NeedValue Not_tail
7680
| Declare _ | Assign _
7781
-> false
7882

@@ -109,5 +113,5 @@ let add_jmps
109113
map, List.rev handlers
110114

111115

112-
let find_exn i cxt =
116+
let find_exn cxt i =
113117
Int_map.find_exn cxt.jmp_table i

jscomp/core/lam_compile_context.mli

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,22 @@ type value = {
5757

5858
type let_kind = Lam_compat.let_kind
5959

60-
type return_type =
61-
| ReturnFalse
62-
| ReturnTrue of return_label option (* anonoymous function does not have identifier *)
60+
type maybe_tail =
61+
| Tail_in_try
62+
| Tail_with_name of return_label option
63+
64+
type tail_type =
65+
| Not_tail
66+
| Maybe_tail_is_return of maybe_tail
67+
(* anonoymous function does not have identifier *)
6368

6469
(* delegate to the callee to generate expression
6570
Invariant: [output] should return a trailing expression
6671
*)
6772

6873
type continuation =
69-
| EffectCall of return_type
70-
| NeedValue of return_type
74+
| EffectCall of tail_type
75+
| NeedValue of tail_type
7176
| Declare of let_kind * J.ident (* bound value *)
7277
| Assign of J.ident
7378
(** when use [Assign], var is not needed, since it's already declared
@@ -81,6 +86,8 @@ type jmp_table
8186
val continuation_is_return:
8287
continuation ->
8388
bool
89+
90+
8491
type t = {
8592
continuation : continuation ;
8693
jmp_table : jmp_table;
@@ -96,9 +103,13 @@ type handler = {
96103
}
97104

98105
val add_jmps :
99-
jmp_table ->
100-
Ident.t ->
101-
handler list ->
102-
jmp_table * (jbl_label * Lam.t) list
106+
jmp_table ->
107+
Ident.t ->
108+
handler list ->
109+
jmp_table * (jbl_label * Lam.t) list
110+
103111

104-
val find_exn : jbl_label -> t -> value
112+
val find_exn :
113+
t ->
114+
jbl_label ->
115+
value

jscomp/core/lam_compile_main.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ let compile_group (meta : Lam_stats.t)
7777

7878
| Recursive id_lams ->
7979
Lam_compile.compile_recursive_lets
80-
{ continuation = EffectCall ReturnFalse;
80+
{ continuation = EffectCall Not_tail;
8181
jmp_table = Lam_compile_context.empty_handler_map;
8282
meta
8383
}
8484
id_lams
8585
| Nop lam -> (* TODO: Side effect callls, log and see statistics *)
86-
Lam_compile.compile_lambda {continuation = EffectCall ReturnFalse;
86+
Lam_compile.compile_lambda {continuation = EffectCall Not_tail;
8787
jmp_table = Lam_compile_context.empty_handler_map;
8888
meta
8989
} lam

jscomp/core/lam_compile_primitive.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ module E = Js_exp_make
3636
*)
3737
let ensure_value_unit (st : Lam_compile_context.continuation) e : E.t =
3838
match st with
39-
| EffectCall (ReturnTrue _ ) | NeedValue (ReturnTrue _)
39+
| EffectCall (Maybe_tail_is_return _ ) | NeedValue (Maybe_tail_is_return _)
4040
| Assign _ | Declare _ | NeedValue _ -> E.seq e E.unit
41-
| EffectCall ReturnFalse -> e
41+
| EffectCall Not_tail -> e
4242
(* NeedValue should return a meaningful expression*)
4343

4444
let translate loc

0 commit comments

Comments
 (0)