Skip to content

Commit c3334cf

Browse files
vouillonhhugo
authored andcommitted
Fix compilation of some mutually recursive functions
Implement caml_alloc_dummy_infix
1 parent 4d67af7 commit c3334cf

File tree

9 files changed

+118
-6
lines changed

9 files changed

+118
-6
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- Effects: fix Js.export and Js.export_all to work with functions
1717
- Sourcemap: fix incorrect sourcemap with separate compilation
1818
- Compiler: fix control flow analysis; some annotions were wrong in the runtime
19+
- Runtime: fix the compilation of some mutually recursive functions
1920

2021
# 5.0.1 (2022-12-20) - Lille
2122
## Features/Changes

compiler/tests-check-prim/main.output

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Missing
33

44
From main.bc:
55
caml_alloc_dummy_function
6-
caml_alloc_dummy_infix
76
caml_dynlink_add_primitive
87
caml_dynlink_close_lib
98
caml_dynlink_get_current_libs

compiler/tests-check-prim/main.output5

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Missing
33

44
From main.bc:
55
caml_alloc_dummy_function
6-
caml_alloc_dummy_infix
76
caml_continuation_use
87
caml_drop_continuation
98
caml_dynlink_add_primitive

compiler/tests-check-prim/unix-unix.output

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Missing
33

44
From unix.bc:
55
caml_alloc_dummy_function
6-
caml_alloc_dummy_infix
76
caml_dynlink_add_primitive
87
caml_dynlink_close_lib
98
caml_dynlink_get_current_libs

compiler/tests-check-prim/unix-unix.output5

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Missing
33

44
From unix.bc:
55
caml_alloc_dummy_function
6-
caml_alloc_dummy_infix
76
caml_continuation_use
87
caml_drop_continuation
98
caml_dynlink_add_primitive

compiler/tests-check-prim/unix-win32.output

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Missing
33

44
From unix.bc:
55
caml_alloc_dummy_function
6-
caml_alloc_dummy_infix
76
caml_dynlink_add_primitive
87
caml_dynlink_close_lib
98
caml_dynlink_get_current_libs

compiler/tests-check-prim/unix-win32.output5

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Missing
33

44
From unix.bc:
55
caml_alloc_dummy_function
6-
caml_alloc_dummy_infix
76
caml_continuation_use
87
caml_drop_continuation
98
caml_dynlink_add_primitive

compiler/tests-jsoo/test_rec_fun.ml

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
(**************************************************************************)
2+
(* *)
3+
(* OCaml *)
4+
(* *)
5+
(* Copyright 2019 Institut National de Recherche en Informatique et *)
6+
(* en Automatique. *)
7+
(* *)
8+
(* All rights reserved. This file is distributed under the terms of *)
9+
(* the GNU Lesser General Public License version 2.1, with the *)
10+
(* special exception on linking described in the file LICENSE. *)
11+
(* *)
12+
(**************************************************************************)
13+
14+
let rec h =
15+
let rec f n = if n >= 0 then g (n - 1)
16+
and g n =
17+
h n;
18+
f n
19+
in
20+
f
21+
22+
let () = ignore (h 10)
23+
24+
let mooo x =
25+
let rec h =
26+
ignore (Sys.opaque_identity x);
27+
let rec g n =
28+
h n;
29+
f n
30+
and f n = if n >= 0 then g (n - 1) in
31+
f
32+
in
33+
h
34+
35+
let h = mooo 3
36+
37+
let () = ignore (h 10)
38+
39+
let rec foo =
40+
let rec f = function
41+
| 0 -> 100
42+
| n -> foo (n - 1)
43+
and g = function
44+
| 0 -> 200
45+
| n -> f (n - 1)
46+
in
47+
g
48+
49+
let%expect_test _ =
50+
print_int (foo 2);
51+
print_newline ();
52+
[%expect {| 200 |}]
53+
54+
let%expect_test _ =
55+
print_int (foo 7);
56+
print_newline ();
57+
[%expect {| 100 |}]
58+
59+
let with_free_vars a b c =
60+
let rec foo =
61+
let rec f = function
62+
| 0 -> 100 + a + b + c
63+
| n -> foo (n - 1)
64+
and g = function
65+
| 0 -> 200 + a + b + c
66+
| n -> f (n - 1)
67+
in
68+
g
69+
in
70+
foo
71+
72+
let%expect_test _ =
73+
print_int (with_free_vars 1 2 3 2);
74+
print_newline ();
75+
[%expect {| 206 |}]
76+
77+
let%expect_test _ =
78+
print_int (with_free_vars 1 2 3 7);
79+
print_newline ();
80+
[%expect {| 106 |}]
81+
82+
let bar =
83+
let rec f = function
84+
| 0 -> 3
85+
| n -> g (n - 1)
86+
and g = function
87+
| 0 -> 10 + f 10
88+
| n -> f (n - 1)
89+
in
90+
let foof = f and goof = g in
91+
foof, goof
92+
93+
let%expect_test _ =
94+
print_int (snd bar 42);
95+
print_newline ();
96+
[%expect {| 13 |}]
97+
98+
let rec foobar =
99+
let rec f x = function
100+
| 0 -> 100
101+
| n -> foobar x (n - 1)
102+
and g x = function
103+
| 0 -> 200
104+
| n -> f x (n - 1)
105+
in
106+
g
107+
108+
let%expect_test _ =
109+
print_int (foobar 5 2);
110+
print_newline ();
111+
[%expect {| 200 |}]

runtime/obj.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ function caml_update_dummy (x, y) {
2222
var i = y.length; while (i--) x[i] = y[i]; return 0;
2323
}
2424

25+
//Provides: caml_alloc_dummy_infix
26+
//Requires: caml_call_gen
27+
function caml_alloc_dummy_infix () {
28+
return function f (x) { return caml_call_gen(f.fun, [x]) }
29+
}
30+
2531
//Provides: caml_obj_is_block const (const)
2632
function caml_obj_is_block (x) { return +(x instanceof Array); }
2733

0 commit comments

Comments
 (0)