···149149 `CAMLprim`, or remove them entirely if no longer used.
150150 (Xavier Leroy, review by David Allsopp)
151151152152+- #12700, continuing #11763 and trying to address #12660:
153153+ Use the correct types for primitives when generating the table of primitives
154154+ used by ocamlrun.
155155+ (Xavier Leroy, motivation, review and improvements by Antonin Décimo)
152156153157### Code generation and optimizations:
154158
+8-37
Makefile
···12511251 $(V_GEN)echo "$(STUBLIBDIR)" > $@ && \
12521252 echo "$(LIBDIR)" >> $@
1253125312541254-# If primitives contain duplicated lines (e.g. because the code is defined
12551255-# like
12561256-# #ifdef X
12571257-# CAMLprim value caml_foo() ...
12581258-# #else
12591259-# CAMLprim value caml_foo() ...
12601260-# #endif), horrible things will happen: duplicated entries in Runtimedef ->
12611261-# double registration in Symtable -> empty entry in the PRIM table ->
12621262-# the bytecode interpreter is confused.
12631263-# We sort the primitive file and remove duplicates to avoid this problem.
12641264-12651265-# Warning: we use "sort | uniq" instead of "sort -u" because in the MSVC
12661266-# port, the "sort" program in the path is Microsoft's and not cygwin's
12671267-12681268-# Warning: POSIX sort is locale dependent, that's why we set LC_ALL explicitly.
12691269-# Sort is unstable for "is_directory" and "isatty"
12701270-# see http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sort.html:
12711271-# "using sort to process pathnames, it is recommended that LC_ALL .. set to C"
12721272-12731254# To speed up builds, we avoid changing "primitives" when files
12741255# containing primitives change but the primitives table does not
12751275-runtime/primitives: \
12761276- $(shell runtime/gen_primitives.sh > runtime/primitives.new; \
12561256+runtime/primitives: runtime/gen_primitives.sh \
12571257+ $(shell runtime/gen_primitives.sh $(runtime_BYTECODE_C_SOURCES) \
12581258+ > runtime/primitives.new; \
12771259 cmp -s runtime/primitives runtime/primitives.new || \
12781260 echo runtime/primitives.new)
12791279- $(V_GEN)cp $^ $@
12611261+ $(V_GEN)cp runtime/primitives.new $@
1280126212811281-runtime/prims.c : runtime/primitives
12821282- $(V_GEN)export LC_ALL=C; \
12831283- (echo '#include "caml/config.h"'; \
12841284- echo 'typedef intnat value;'; \
12851285- echo 'typedef value (*c_primitive)(void);'; \
12861286- echo; \
12871287- sed -e 's/.*/extern value &(void);/' $<; \
12881288- echo; \
12891289- echo 'const c_primitive caml_builtin_cprim[] = {'; \
12901290- sed -e 's/.*/ &,/' $<; \
12911291- echo ' 0 };'; \
12921292- echo; \
12931293- echo 'const char * const caml_names_of_builtin_cprim[] = {'; \
12941294- sed -e 's/.*/ "&",/' $<; \
12951295- echo ' 0 };') > $@
12631263+runtime/prims.c: runtime/gen_primsc.sh runtime/primitives
12641264+ $(V_GEN)runtime/gen_primsc.sh \
12651265+ runtime/primitives $(runtime_BYTECODE_C_SOURCES) \
12661266+ > $@
1296126712971268runtime/caml/opnames.h : runtime/caml/instruct.h
12981269 $(V_GEN)tr -d '\r' < $< | \
···11001100 return caml_signbit(Double_val(f));
11011101}
1102110211031103-CAMLprim value caml_neq_float(value f, value g)
11041104-{
11051105- return Val_bool(Double_val(f) != Double_val(g));
11061106-}
11071107-11081108-#define DEFINE_NAN_CMP(op) (value f, value g) \
11091109-{ \
11101110- return Val_bool(Double_val(f) op Double_val(g)); \
11111111-}
11121112-11131103intnat caml_float_compare_unboxed(double f, double g)
11141104{
11151105 /* If one or both of f and g is NaN, order according to the convention
···11241114 return res;
11251115}
1126111611271127-CAMLprim value caml_eq_float DEFINE_NAN_CMP(==)
11281128-CAMLprim value caml_le_float DEFINE_NAN_CMP(<=)
11291129-CAMLprim value caml_lt_float DEFINE_NAN_CMP(<)
11301130-CAMLprim value caml_ge_float DEFINE_NAN_CMP(>=)
11311131-CAMLprim value caml_gt_float DEFINE_NAN_CMP(>)
11171117+#define FLOAT_CMP(op, f, g) \
11181118+ return Val_bool(Double_val(f) op Double_val(g));
11191119+11201120+CAMLprim value caml_neq_float(value f, value g) { FLOAT_CMP(!=, f, g) }
11211121+CAMLprim value caml_eq_float(value f, value g) { FLOAT_CMP(==, f, g) }
11221122+CAMLprim value caml_le_float(value f, value g) { FLOAT_CMP(<=, f, g) }
11231123+CAMLprim value caml_lt_float(value f, value g) { FLOAT_CMP(<, f, g) }
11241124+CAMLprim value caml_ge_float(value f, value g) { FLOAT_CMP(>=, f, g) }
11251125+CAMLprim value caml_gt_float(value f, value g) { FLOAT_CMP(>, f, g) }
1132112611331127CAMLprim value caml_float_compare(value vf, value vg)
11341128{
+25-14
runtime/gen_primitives.sh
···1515#* *
1616#**************************************************************************
17171818-# #8985: the meaning of character range a-z depends on the locale, so force C
1919-# locale throughout.
1818+# If primitives contain duplicated lines (e.g. because the code is defined
1919+# like
2020+# #ifdef X
2121+# CAMLprim value caml_foo() ...
2222+# #else
2323+# CAMLprim value caml_foo() ...
2424+# #endif), horrible things will happen: duplicated entries in Runtimedef ->
2525+# double registration in Symtable -> empty entry in the PRIM table ->
2626+# the bytecode interpreter is confused.
2727+# We sort the primitive file and remove duplicates to avoid this problem.
2828+2929+# Warning: we use "sort | uniq" instead of "sort -u" because in the MSVC
3030+# port, the "sort" program in the path is Microsoft's and not cygwin's
3131+3232+# Warning: POSIX sort is locale dependent, that's why we set LC_ALL explicitly.
3333+# Sort is unstable for "is_directory" and "isatty"
3434+# see http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sort.html:
3535+# "using sort to process pathnames, it is recommended that LC_ALL .. set to C"
3636+3737+# #8985: in sed, the meaning of character range a-z depends on the locale,
3838+# so force C locale throughout.
3939+2040export LC_ALL=C
2121-(
2222- for prim in \
2323- alloc array compare extern floats gc_ctrl hash intern interp ints io \
2424- lexing md5 meta memprof obj parsing signals str sys callback weak \
2525- finalise domain platform fiber memory startup_aux runtime_events sync \
2626- dynlink backtrace_byt backtrace afl \
2727- bigarray prng
2828- do
2929- sed -n -e 's/^CAMLprim value \([a-z0-9_][a-z0-9_]*\).*/\1/p' \
3030- "runtime/$prim.c"
3131- done
3232-) | sort | uniq
4141+4242+sed -n -e 's/^CAMLprim value \([a-z][a-z0-9_]*\).*$/\1/p' "$@" | \
4343+sort | uniq
+63
runtime/gen_primsc.sh
···11+#!/bin/sh
22+33+#**************************************************************************
44+#* *
55+#* OCaml *
66+#* *
77+#* Xavier Leroy, Collège de France and Inria *
88+#* *
99+#* Copyright 2023 Institut National de Recherche en Informatique et *
1010+#* en Automatique. *
1111+#* *
1212+#* All rights reserved. This file is distributed under the terms of *
1313+#* the GNU Lesser General Public License version 2.1, with the *
1414+#* special exception on linking described in the file LICENSE. *
1515+#* *
1616+#**************************************************************************
1717+1818+# Build the runtime/prims.c file, with proper C declarations of the primitives
1919+2020+export LC_ALL=C
2121+2222+case $# in
2323+ 0) echo "Usage: gen_primsc.sh <primitives file> <.c files>" 1>&2
2424+ exit 2;;
2525+ *) primitives="$1"; shift;;
2626+esac
2727+2828+cat <<'EOF'
2929+/* Generated file, do not edit */
3030+3131+#define CAML_INTERNALS
3232+#include "caml/mlvalues.h"
3333+#include "caml/prims.h"
3434+3535+EOF
3636+3737+# Extract the beginning of primitive definitions:
3838+# from 'CAMLprim' at beginning of line to the first closing parenthesis.
3939+# The first pattern below matches single-line definitions such as
4040+# CAMLprim value foo(value x) {
4141+# The second pattern matches multi-line definitions such as
4242+# CAMLprim value foo(value x,
4343+# value y)
4444+sed -n \
4545+ -e '/^CAMLprim value .*)/p' \
4646+ -e '/^CAMLprim value [^)]*$/,/)/p' \
4747+ "$@" |
4848+# Transform these definitions into "CAMLextern" declarations
4949+sed \
5050+ -e 's/^CAMLprim /CAMLextern /' \
5151+ -e 's/).*$/);/'
5252+5353+# Generate the table of primitives
5454+echo
5555+echo 'const c_primitive caml_builtin_cprim[] = {'
5656+sed -e 's/.*/ (c_primitive) &,/' "$primitives"
5757+echo ' 0 };'
5858+5959+# Generate the table of primitive names
6060+echo
6161+echo 'const char * const caml_names_of_builtin_cprim[] = {'
6262+sed -e 's/.*/ "&",/' "$primitives"
6363+echo ' 0 };'