Skip to content

Commit 8a80f7f

Browse files
committed
Cont Lambda Ltail (flambda2)
1 parent d80026c commit 8a80f7f

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

middle_end/flambda2/from_lambda/lambda_to_flambda.ml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ module Env : sig
133133
continuation_after_closing_region:Continuation.t ->
134134
t
135135

136+
val leaving_region : t -> t
137+
136138
val entering_try_region : t -> Ident.t -> t
137139

138140
val leaving_try_region : t -> t
@@ -364,6 +366,14 @@ end = struct
364366
t.region_closure_continuations
365367
}
366368

369+
let leaving_region t =
370+
match t.region_stack with
371+
| [] -> Misc.fatal_error "Cannot pop region, region stack is empty"
372+
| Regular _ :: region_stack -> { t with region_stack }
373+
| Try_with region :: _ ->
374+
Misc.fatal_errorf "Attempted to pop region but found try region %a"
375+
Ident.print region
376+
367377
let entering_try_region t region =
368378
{ t with region_stack = Try_with region :: t.region_stack }
369379

@@ -1430,6 +1440,13 @@ let rec cps acc env ccenv (lam : L.lambda) (k : cps_continuation)
14301440
"[Lifused] should have been removed by [Simplif.simplify_lets]"
14311441
| Lregion (body, _) when not (Flambda_features.stack_allocation_enabled ()) ->
14321442
cps acc env ccenv body k k_exn
1443+
| Ltail body ->
1444+
let region = Env.current_region env in
1445+
CC.close_let acc ccenv (Ident.create_local "unit")
1446+
Not_user_visible Flambda_kind.With_subkind.tagged_immediate
1447+
(End_region region) ~body:(fun acc ccenv ->
1448+
let env = Env.leaving_region env in
1449+
cps acc env ccenv body k k_exn)
14331450
| Lregion (body, layout) ->
14341451
(* Here we need to build the region closure continuation (see long comment
14351452
above). Since we're not in tail position, we also need to have a new
@@ -1720,7 +1737,8 @@ and cps_switch acc env ccenv (switch : L.lambda_switch) ~condition_dbg
17201737
| Lmutvar _ | Lapply _ | Lfunction _ | Llet _ | Lmutlet _ | Lletrec _
17211738
| Lprim _ | Lswitch _ | Lstringswitch _ | Lstaticraise _
17221739
| Lstaticcatch _ | Ltrywith _ | Lifthenelse _ | Lsequence _ | Lwhile _
1723-
| Lfor _ | Lassign _ | Lsend _ | Levent _ | Lifused _ | Lregion _ ->
1740+
| Lfor _ | Lassign _ | Lsend _ | Levent _ | Lifused _ | Lregion _
1741+
| Ltail _ ->
17241742
(* The continuations created here (and for failactions) are local. The
17251743
bodies of the let_conts will not modify mutable variables. Hence,
17261744
it is safe to exclude them from passing along the extra arguments

0 commit comments

Comments
 (0)