Files
mercury/tests/hard_coded/profdeep_seg_fault.m
Zoltan Somogyi 2bd7c5ee3e Rename X's aux modules as X_helper_N in hard_coded.
tests/hard_coded/*.m:
    Rename modules as mentioned above.

    In a few cases, where the main module's name itself had a suffix,
    such as "_mod_a" or "_main", remove that suffix. This entails
    renaming the .exp file as well. (In some cases, this meant that
    the name of a helper module was "taken over" by the main module
    of the test case.)

    Update all references to the moved modules.

    General updates to programming style, such as

    - replacing DCG notation with state var notation
    - replacing (C->T;E) with (if C then T else E)
    - moving pred/func declarations to just before their code
    - replacing io.write/io.nl sequences with io.write_line
    - replacing io.print/io.nl sequences with io.print_line
    - fixing too-long lines
    - fixing grammar errors in comments

tests/hard_coded/Mmakefile:
tests/hard_coded/Mercury.options:
    Update all references to the moved modules.

    Enable the constant_prop_int test case. The fact that it wasn't enabled
    before is probably an accident. (When constant_prop_int.m was created,
    the test case was added to a list in the Mmakefile, but that list
    was later removed due to never being referenced.)

tests/hard_coded/constant_prop_int.{m,exp}:
    Delete the calls to shift operations with negative shift amounts,
    since we have added a compile-time error for these since the test
    was originally created.
2023-06-16 08:33:22 +02:00

129 lines
3.5 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
%
% The following program used to expose a bug in the runtime's handling
% of deep profiled code. When compiled in a deep profiling grade at -O2
% or below it used to abort with a segmentation fault.
%
% The problem was caused by the code in builtin.{unify, compare} for du types
% having inappropriate references to the proc layout structures of the dummy
% unify and compare predicates for the dummy type builtin.user_by_rtti. If the
% first call to builtin.{unify, compare} at a given call site had the equality
% pretest of the two arguments succeed, this used to fill in the call site
% dynamic structure with a pointer to the proc dynamic structure of this dummy
% predicate, leading later calls through that call site to refer to the wrong
% data structure.
:- module profdeep_seg_fault.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- type list(T)
---> []
; [T | list(T)].
:- type t_type
---> t_bool
; t_set(t_type)
; t_list(t_type)
; t_enum(string)
; t_typevar(string).
:- type t_sig
---> t_sig(list(t_type), t_type).
:- type type_inst
---> ti_par_bool
; ti_par_typevar(string).
:- type ti_sig
---> ti_sig(list(type_inst), type_inst).
main(!IO) :-
A = [
ti_sig([ti_par_bool], ti_par_bool),
ti_sig([ti_par_bool], ti_par_bool)
],
B = [
ti_sig([ti_par_typevar("FOO")], ti_par_bool),
ti_sig([ti_par_typevar("BAR")], ti_par_bool)
],
AllBuiltinSigs = [A, B],
add_sigs_to_sym(AllBuiltinSigs, [], S),
io.write(S, !IO),
io.nl(!IO).
:- pred add_sigs_to_sym(list(list(ti_sig))::in,
list(t_sig)::in, list(t_sig)::out) is det.
add_sigs_to_sym([], S, S).
add_sigs_to_sym([TISigs | TISigs0], S0, S) :-
ti_sigs_to_t_sigs(TISigs, TSigs0),
insertion_sort(TSigs0, [], TSigs),
append_type_sigs(TSigs, S0, S1),
add_sigs_to_sym(TISigs0, S1, S).
:- pred ti_sigs_to_t_sigs(list(ti_sig)::in, list(t_sig)::out) is det.
ti_sigs_to_t_sigs([], []).
ti_sigs_to_t_sigs([X | Xs], [Y | Ys]) :-
X = ti_sig(ArgTIs, RetTI),
type_insts_to_t_types(ArgTIs, ArgTs),
(
RetTI = ti_par_bool,
RetT = t_bool
;
RetTI = ti_par_typevar(V),
RetT = t_typevar(V)
),
Y = t_sig(ArgTs, RetT),
ti_sigs_to_t_sigs(Xs, Ys).
:- pred type_insts_to_t_types(list(type_inst)::in, list(t_type)::out) is det.
type_insts_to_t_types([], []).
type_insts_to_t_types([X | Xs], [Y | Ys]) :-
(
X = ti_par_bool,
Y = t_bool
;
X = ti_par_typevar(V),
Y = t_typevar(V)
),
type_insts_to_t_types(Xs, Ys).
:- pred append_type_sigs(list(t_sig)::in, list(t_sig)::in,
list(t_sig)::out) is det.
append_type_sigs([], Zs, Zs).
append_type_sigs([X | Xs], Ys, [X | Zs]) :-
append_type_sigs(Xs, Ys, Zs).
:- pred insertion_sort(list(t_sig)::in, list(t_sig)::in, list(t_sig)::out)
is det.
insertion_sort([], Zs, Zs).
insertion_sort([X | Xs], Ys0, Zs) :-
insert(X, Ys0, Ys),
insertion_sort(Xs, Ys, Zs).
:- pred insert(t_sig::in, list(t_sig)::in, list(t_sig)::out) is det.
insert(X, [], [X]).
insert(X, [Y | Ys], Zs) :-
( if
compare(Res, X, Y),
Res = (>)
then
insert(X, Ys, Zs0),
Zs = [Y | Zs0]
else
Zs = [X, Y | Ys]
).