Skip to content

Commit d6b868c

Browse files
committed
Build runtime/prims.c with accurate declarations of primitives
"Accurate" means "with their proper types instead of the generic type `value prim(void)`". Partial fix for #12660
1 parent 2f944c9 commit d6b868c

File tree

2 files changed

+69
-14
lines changed

2 files changed

+69
-14
lines changed

Makefile

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,20 +1261,9 @@ runtime/primitives: \
12611261
$(V_GEN)cp $^ $@
12621262

12631263
runtime/prims.c : runtime/primitives
1264-
$(V_GEN)export LC_ALL=C; \
1265-
(echo '#include "caml/config.h"'; \
1266-
echo 'typedef intnat value;'; \
1267-
echo 'typedef value (*c_primitive)(void);'; \
1268-
echo; \
1269-
sed -e 's/.*/extern value &(void);/' $<; \
1270-
echo; \
1271-
echo 'const c_primitive caml_builtin_cprim[] = {'; \
1272-
sed -e 's/.*/ &,/' $<; \
1273-
echo ' 0 };'; \
1274-
echo; \
1275-
echo 'const char * const caml_names_of_builtin_cprim[] = {'; \
1276-
sed -e 's/.*/ "&",/' $<; \
1277-
echo ' 0 };') > $@
1264+
$(V_GEN) runtime/gen_primsc.sh \
1265+
runtime/primitives $(runtime_BYTECODE_C_SOURCES) \
1266+
> $@
12781267

12791268
runtime/caml/opnames.h : runtime/caml/instruct.h
12801269
$(V_GEN)tr -d '\r' < $< | \

runtime/gen_primsc.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/sh
2+
3+
#**************************************************************************
4+
#* *
5+
#* OCaml *
6+
#* *
7+
#* Xavier Leroy, Collège de France and Inria *
8+
#* *
9+
#* Copyright 2023 Institut National de Recherche en Informatique et *
10+
#* en Automatique. *
11+
#* *
12+
#* All rights reserved. This file is distributed under the terms of *
13+
#* the GNU Lesser General Public License version 2.1, with the *
14+
#* special exception on linking described in the file LICENSE. *
15+
#* *
16+
#**************************************************************************
17+
18+
# Build the runtime/prims.c file, with proper C declarations of the primitives
19+
20+
export LC_ALL=C
21+
22+
case $# in
23+
0) echo "Usage: gen_primsc.sh <primitives file> <.c files>" 1>&2
24+
exit 2;;
25+
*) primitives="$1"; shift;;
26+
esac
27+
28+
cat <<'EOF'
29+
/* Generated file, do not edit */
30+
31+
#define CAML_INTERNALS
32+
#include "caml/mlvalues.h"
33+
#include "caml/prims.h"
34+
35+
EOF
36+
37+
# Extract the beginning of primitive definitions:
38+
# from 'CAMLprim' at beginning of line to the first closing parenthesis.
39+
# The first pattern below matches single-line definitions such as
40+
# CAMLprim value foo(value x) {
41+
# The second pattern matches multi-line definitions such as
42+
# CAMLprim value foo(value x,
43+
# value y)
44+
sed -n \
45+
-e '/^CAMLprim value .*)/p' \
46+
-e '/^CAMLprim value [^)]*$/,/)/p' \
47+
"$@" |
48+
# Transform these definitions into "CAMLextern" declarations
49+
# TODO: consider getting rid of the DEFINE_NAN_CMP macro in runtime/floats.c
50+
sed \
51+
-e 's/DEFINE_NAN_CMP(.*)/(value, value)/' \
52+
-e 's/^CAMLprim /CAMLextern /' \
53+
-e 's/).*$/);/'
54+
55+
# Generate the table of primitives
56+
echo
57+
echo 'const c_primitive caml_builtin_cprim[] = {'
58+
sed -e 's/.*/ (c_primitive) &,/' "$primitives"
59+
echo ' 0 };'
60+
61+
# Generate the table of primitive names
62+
echo
63+
echo 'const char * const caml_names_of_builtin_cprim[] = {'
64+
sed -e 's/.*/ "&",/' "$primitives"
65+
echo ' 0 };'
66+

0 commit comments

Comments
 (0)