Skip to content

Commit 952cb05

Browse files
authored
Compiler: prepare parse_bytecode for OCaml 4.12 (#1024)
1 parent f908775 commit 952cb05

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

compiler/lib/parse_bytecode.ml

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,12 @@ let compiled_blocks = ref Addr.Map.empty
761761

762762
let method_cache_id = ref 1
763763

764+
let new_closure_repr =
765+
(* true starting with 4.12 *)
766+
match Ocaml_version.v with
767+
| `V4_02 | `V4_03 | `V4_04 | `V4_06 | `V4_07 | `V4_08 | `V4_09 | `V4_10 | `V4_11 ->
768+
false
769+
764770
type compile_info =
765771
{ blocks : Blocks.u
766772
; code : string
@@ -883,7 +889,9 @@ and compile infos pc state instrs =
883889
infos
884890
(pc + 2)
885891
{ state with
886-
State.stack = State.Dummy :: State.Dummy :: State.Dummy :: state.State.stack
892+
State.stack =
893+
(* See interp.c *)
894+
State.Dummy :: State.Dummy :: State.Dummy :: state.State.stack
887895
}
888896
instrs
889897
| APPLY ->
@@ -1021,9 +1029,13 @@ and compile infos pc state instrs =
10211029
let state = if nvars > 0 then State.push state else state in
10221030
let vals, state = State.grab nvars state in
10231031
let x, state = State.fresh_var state in
1032+
let env = List.map vals ~f:(fun x -> State.Var x) in
10241033
let env =
1025-
Array.of_list (State.Dummy :: List.map vals ~f:(fun x -> State.Var x))
1034+
let code = State.Dummy in
1035+
let closure_info = State.Dummy in
1036+
if new_closure_repr then code :: closure_info :: env else code :: env
10261037
in
1038+
let env = Array.of_list env in
10271039
if debug_parser () then Format.printf "fun %a (" Var.print x;
10281040
let nparams =
10291041
match (get_instr_exn code addr).Instr.code with
@@ -1058,8 +1070,15 @@ and compile infos pc state instrs =
10581070
done;
10591071
let env = ref (List.map vals ~f:(fun x -> State.Var x)) in
10601072
List.iter !vars ~f:(fun (i, x) ->
1061-
env := State.Var x :: !env;
1062-
if i > 0 then env := State.Dummy :: !env);
1073+
let code = State.Var x in
1074+
let closure_info = State.Dummy in
1075+
if new_closure_repr
1076+
then env := code :: closure_info :: !env
1077+
else env := code :: !env;
1078+
if i > 0
1079+
then
1080+
let infix_tag = State.Dummy in
1081+
env := infix_tag :: !env);
10631082
let env = Array.of_list !env in
10641083
let state = !state in
10651084
let instrs =
@@ -1071,7 +1090,8 @@ and compile infos pc state instrs =
10711090
| GRAB -> getu code (addr + 1) + 1
10721091
| _ -> 1
10731092
in
1074-
let state' = State.start_function state env (i * 2) in
1093+
let offset = if new_closure_repr then i * 3 else i * 2 in
1094+
let state' = State.start_function state env offset in
10751095
let params, state' = State.make_stack nparams state' in
10761096
if debug_parser () then Format.printf ") {@.";
10771097
let state' = State.clear_accu state' in
@@ -1463,6 +1483,7 @@ and compile infos pc state instrs =
14631483
(pc + 2)
14641484
{ (State.push_handler state x addr) with
14651485
State.stack =
1486+
(* See interp.c *)
14661487
State.Dummy
14671488
:: State.Dummy
14681489
:: State.Dummy

0 commit comments

Comments
 (0)