Skip to content

Commit c9357ac

Browse files
MisterDAdra27
authored andcommitted
Use strict prototypes on primitives in bytecomp
The bytecode compiler also extracts the list of primitives to a C file, similarly as the Makefile. The Makefile was fixed in 8db1817 to account for stricter C function prototypes, but the bytecomp problems were not discovered back then. As the startup code includes `caml/mlvalues.h`, two primitives are declared twice with conflicting types. We prevent this with preprocessor tricks before including the header. The generated C code now also uses the same names as in the Makefile.
1 parent 5a10c41 commit c9357ac

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

bytecomp/bytelink.ml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,13 @@ let emit_global_constant outchan (name, value) =
606606

607607
(* Output a bytecode executable as a C file *)
608608

609+
(* Primitives declared in the included headers but re-declared in the
610+
primitives table need to be guarded and not declared twice. *)
611+
let guarded_primitives = [
612+
"caml_get_public_method", "caml__get_public_method";
613+
"caml_set_oo_id", "caml__set_oo_id";
614+
]
615+
609616
let link_bytecode_as_c tolink outfile with_main =
610617
let outchan = open_out outfile in
611618
Misc.try_finally
@@ -619,12 +626,17 @@ let link_bytecode_as_c tolink outfile with_main =
619626
\n\
620627
\n#ifdef __cplusplus\
621628
\nextern \"C\" {\
622-
\n#endif\
629+
\n#endif";
630+
List.iter (fun (f, f') -> Printf.fprintf outchan "\n#define %s %s" f f')
631+
guarded_primitives;
632+
output_string outchan "\
623633
\n#include <caml/mlvalues.h>\
624634
\n#include <caml/startup.h>\
625635
\n#include <caml/sys.h>\
626636
\n#include <caml/misc.h>\n";
627-
output_string outchan "static int caml_code[] = {\n";
637+
List.iter (fun (f, _) -> Printf.fprintf outchan "\n#undef %s" f)
638+
guarded_primitives;
639+
output_string outchan "\nstatic int caml_code[] = {\n";
628640
Symtable.init();
629641
clear_crc_interfaces ();
630642
let currpos = ref 0 in

bytecomp/symtable.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,19 @@ open Printf
132132
let output_primitive_table outchan =
133133
let prim = all_primitives() in
134134
for i = 0 to Array.length prim - 1 do
135-
fprintf outchan "extern value %s();\n" prim.(i)
135+
fprintf outchan "extern value %s(void);\n" prim.(i)
136136
done;
137-
fprintf outchan "typedef value (*primitive)();\n";
138-
fprintf outchan "primitive caml_builtin_cprim[] = {\n";
137+
fprintf outchan "typedef value (*c_primitive)(void);\n";
138+
fprintf outchan "c_primitive caml_builtin_cprim[] = {\n";
139139
for i = 0 to Array.length prim - 1 do
140140
fprintf outchan " %s,\n" prim.(i)
141141
done;
142-
fprintf outchan " (primitive) 0 };\n";
142+
fprintf outchan " 0 };\n";
143143
fprintf outchan "const char * caml_names_of_builtin_cprim[] = {\n";
144144
for i = 0 to Array.length prim - 1 do
145145
fprintf outchan " \"%s\",\n" prim.(i)
146146
done;
147-
fprintf outchan " (char *) 0 };\n"
147+
fprintf outchan " 0 };\n"
148148

149149
(* Initialization for batch linking *)
150150

0 commit comments

Comments
 (0)