mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-17 18:33:58 +00:00
Estimated hours taken: 50
This change introduces interface tracing, and makes it possible to successfully
bootstrap the compiler with tracing (either interface or full).
compiler/options.m:
Change the bool options --generate-trace into a string option --trace
with three valid values: minimal, interface and full. The last two mean
what they say; the intention is that eventually minimal will mean
no tracing in non-tracing grades and interface tracing in tracing
grades.
compiler/globals.m:
Add a new global for the trace level.
compiler/handle_options.m:
Convert the argument of --trace to a trace level.
Use only consistent 4-space indentation in the deeply nested
if-then-else.
compiler/trace.m:
Implement interface tracing.
Rename trace__generate_depth_reset_code as trace__prepare_for_call,
since it does more than reset the depth if this module is compiled
with interface tracing.
Do not check whether tracing is enabled before calling MR_trace;
let MR_trace make the check. This trades increased non-tracing
execution time for a substantial code size reduction (which may
in turn benefit execution time).
compiler/call_gen.m:
Call trace__generate_depth_reset_code by its new name.
compiler/code_info.m:
Fix a bug in the handling of non/semi commits. When entering a commit,
we used to push a clone of whatever the top failure continuation was.
However, the resume setup for this continuation could have started
with a label that assumed that the resume vars were in their original
locations (which are often registers), whereas the method of
backtracking to that point only guarantees the survival of stack slots,
not registers.
(This bug caused two lines of incorrect code to be generated among
the approx 30 million lines of code in the stage 2 compiler when
compiled with tracing.)
Fix another bug (previously untriggered as far as I know) in the
handling of multi/det commits. This one was breaking the invariant
that the resume vars set of each entry on the failure continuation
stack included the resume vars set of every other entry below it,
which meant that the values of these resume vars were not guaranteed
to be preserved.
compiler/stack_layout.m:
Make layout structures local to their module. They are not (yet)
referred to by name from other modules, and by declaring them
to be global we caused their names to be included even in stripped
executables, adding several megabytes to the size of the binary.
(The names are not stripped because a dynamically linked library
may want to refer to them.)
Change the mercury_data__stack_layout__ prefix on the names of
generated globals vars to just mercury_data__layout__. It is now
merely too long instead of far too long.
Include the label number in the label layout structure and the number
of typeinfo variables in a var_info structure only with native gc.
Their only use is in debugging native gc.
Fix some documentation rot.
compiler/llds.m:
Add a new field to the pragma_c instruction that says whether the
compiler-generated C code fragments access any stack variables.
compiler/frameopt.m:
Use the new field in pragma_c's to avoid a bug. Because frameopt was
assuming that the pragma_c instruction that filled in the stack slots
containing the call sequence number and depth did not access the stack,
it moved the pragma_c before the incr_sp that allocates the frame
(it was trying to get it out of the loop).
compiler/*.m:
Minor changes to set or ignore the extra field in pragma_c, to refer
to layout structures via the new prefix, or to handle the --trace
option.
doc/user_guide.texi:
Update the documentation for --trace.
runtime/mercury_types.h:
Add the type Unsigned.
runtime/mercury_goto.h:
Use the shorter layout prefix.
runtime/mercury_stack_layout.h:
Use the shorter layout prefix, and include the label number only with
native gc.
runtime/mercury_trace.[ch]:
runtime/mercury_trace_internal.[ch]:
runtime/mercury_trace_external.[ch]:
runtime/mercury_trace_util.[ch]:
Divide the old mercury_trace.[ch] into several components, with one
module for the internal debugger, one for the interface to the
external debugger, one for utilities needed by both. Mercury_trace.c
now has only the top-level stuff that steers between the two
debuggers.
runtime/mercury_trace.[ch]:
Add the new global variable MR_trace_from_full. Before each call,
the calling procedure assigns TRUE to this variable if the caller
is fully traced, and FALSE otherwise. Interface traced procedures
generate trace events only if this variable is TRUE when they are
called (fully traced callee procedures ignore the initial value of
the variable).
Make MR_trace return immediately without doing anything unless
tracing is enabled and a new extra argument to MR_trace is TRUE.
This extra argument is always TRUE for trace events in fully traced
procedures, while for trace events from interface traced procedures,
its value is set from the value of MR_trace_from_full at the time
that the procedure was called (i.e. the event is ignored unless the
interface traced procedure was called from a fully traced procedure).
runtime/mercury_trace.[ch]:
runtime/mercury_trace_internal.[ch]:
For global variables that are stored in stack slots, make their type
Word rather than int.
Use a new function MR_trace_event_report instead of calling
MR_trace_event with a NULL command structure pointer to indicate
that the event is to be reported but there is to be no user
interaction.
Use %ld formats in printfs and casts to long for better portability.
runtime/mercury_trace_internal.c:
Save trace-related globals across calls to Mercury library code
in the debugger, since otherwise any trace events in this code
could screw up e.g. the event number or the call number sequence.
Create separate functions for printing port names and determinisms.
runtime/mercury_wrapper.h:
Disable the tracing of the initialization and finalization code
written in Mercury.
runtime/Mmakefile:
Update for the new source and header files.
tests/debugger/{debugger_regs,interpreter,queens}_lib.{m,inp,exp}:
One new copy of each existing test case. These ones are intended
to be used when the stage 2 library is compiled with tracing, which
affects the tests by adding events for the library procedures called
from the test programs.
The .m files are the same as before; one of the .inp files is a bit
different; the .exp files reflect the correct output when the library
is compiled with full tracing.
tests/debugger/Mmakefile:
Provide separate targets for the new set of test cases.
Use --trace full instead of --generate-trace.
tests/debugger/runtests:
Try both the new set of test cases if the old set fails, and report
failure only if both sets fail. This is simpler than trying to figure
out which set should be really tested, and the probability of a false
positive is negligible.
341 lines
11 KiB
Mathematica
341 lines
11 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 1996-1998 The University of Melbourne.
|
|
% This file may only be copied under the terms of the GNU General
|
|
% Public License - see the file COPYING in the Mercury distribution.
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% This module looks for static data structures in create rvals so that
|
|
% it can make them global, in the hope of replacing multiple local definitions
|
|
% by a single global one.
|
|
%
|
|
% Processes a list of procedures, and a list of c_modules, intended
|
|
% to contain any extra c_data structures the compiler generates.
|
|
% Any other contents of the c_modules list will be untouched.
|
|
|
|
% Main author: zs.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module llds_common.
|
|
|
|
:- interface.
|
|
|
|
:- import_module llds.
|
|
:- import_module prog_data. % for module_name
|
|
:- import_module list.
|
|
|
|
:- pred llds_common(list(c_procedure), list(c_module), module_name,
|
|
list(c_procedure), list(c_module), list(c_module)).
|
|
:- mode llds_common(in, in, in, out, out, out) is det.
|
|
|
|
:- implementation.
|
|
|
|
:- import_module llds_out.
|
|
|
|
:- import_module bool, int, assoc_list, map, std_util, require.
|
|
|
|
:- type cell_info
|
|
---> cell_info(
|
|
int % what is the number of the cell?
|
|
).
|
|
|
|
:- type common_info
|
|
---> common_info(
|
|
module_name, % base file name
|
|
int, % next cell number
|
|
map(list(maybe(rval)), cell_info)
|
|
% map cell contents to cell declaration
|
|
% information
|
|
).
|
|
|
|
llds_common(Procedures0, Data0, BaseName, Procedures, Data, DataModules) :-
|
|
map__init(CellMap0),
|
|
Info0 = common_info(BaseName, 0, CellMap0),
|
|
llds_common__process_procs(Procedures0, Info0, Info1, Procedures),
|
|
llds_common__process_modules(Data0, Info1, Info, Data),
|
|
Info = common_info(_, _, CellMap),
|
|
map__to_assoc_list(CellMap, CellPairs0),
|
|
list__sort(lambda([CellPairA::in, CellPairB::in, Compare::out] is det,
|
|
(
|
|
CellPairA = _ - cell_info(ANum),
|
|
CellPairB = _ - cell_info(BNum),
|
|
compare(Compare, ANum, BNum)
|
|
)), CellPairs0, CellPairs),
|
|
llds_common__cell_pairs_to_modules(CellPairs, BaseName, DataModules).
|
|
|
|
:- pred llds_common__cell_pairs_to_modules(
|
|
assoc_list(list(maybe(rval)), cell_info), module_name, list(c_module)).
|
|
:- mode llds_common__cell_pairs_to_modules(in, in, out) is det.
|
|
|
|
llds_common__cell_pairs_to_modules([], _, []).
|
|
llds_common__cell_pairs_to_modules([Args - CellInfo | CellPairs], BaseName,
|
|
[Module | Modules]) :-
|
|
CellInfo = cell_info(VarNum),
|
|
Module = c_data(BaseName, common(VarNum), no, Args, []),
|
|
llds_common__cell_pairs_to_modules(CellPairs, BaseName, Modules).
|
|
|
|
:- pred llds_common__process_create(tag, list(maybe(rval)),
|
|
common_info, common_info, rval).
|
|
:- mode llds_common__process_create(in, in, in, out, out) is det.
|
|
|
|
llds_common__process_create(Tag, Args0, Info0, Info, Rval) :-
|
|
llds_common__process_maybe_rvals(Args0, Info0, Info1, Args),
|
|
Info1 = common_info(BaseName, NextCell0, CellMap0),
|
|
( map__search(CellMap0, Args, CellInfo0) ->
|
|
CellInfo0 = cell_info(VarNum),
|
|
DataConst = data_addr_const(
|
|
data_addr(BaseName, common(VarNum))),
|
|
Rval = mkword(Tag, const(DataConst)),
|
|
Info = Info1
|
|
;
|
|
DataConst = data_addr_const(
|
|
data_addr(BaseName, common(NextCell0))),
|
|
Rval = mkword(Tag, const(DataConst)),
|
|
CellInfo = cell_info(NextCell0),
|
|
NextCell is NextCell0 + 1,
|
|
map__det_insert(CellMap0, Args, CellInfo, CellMap),
|
|
Info = common_info(BaseName, NextCell, CellMap)
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% The rest of the file is quite boring. Its only job is to traverse
|
|
% the various components of c_modules to arrive at the creates.
|
|
|
|
|
|
% Only c_data elements are processed, all other elements are ignored.
|
|
|
|
:- pred llds_common__process_modules(list(c_module), common_info, common_info,
|
|
list(c_module)).
|
|
:- mode llds_common__process_modules(in, in, out, out) is det.
|
|
|
|
llds_common__process_modules([], Info, Info, []).
|
|
llds_common__process_modules([Module0 | Modules0], Info0, Info,
|
|
[Module | Modules]) :-
|
|
llds_common__process_module(Module0, Info0, Info1, Module),
|
|
llds_common__process_modules(Modules0, Info1, Info, Modules).
|
|
|
|
:- pred llds_common__process_module(c_module, common_info, common_info,
|
|
c_module).
|
|
:- mode llds_common__process_module(in, in, out, out) is det.
|
|
|
|
llds_common__process_module(c_module(Name, Ps), Info, Info, c_module(Name, Ps)).
|
|
llds_common__process_module(c_code(Cde, Ctxt), Info, Info, c_code(Cde, Ctxt)).
|
|
llds_common__process_module(c_export(Exports), Info, Info, c_export(Exports)).
|
|
llds_common__process_module(
|
|
c_data(Name, DataName, Export, Args0, Refs), Info0,
|
|
Info, c_data(Name, DataName, Export, Args, Refs)) :-
|
|
llds_common__process_maybe_rvals(Args0, Info0, Info, Args).
|
|
|
|
:- pred llds_common__process_procs(list(c_procedure), common_info, common_info,
|
|
list(c_procedure)).
|
|
:- mode llds_common__process_procs(in, in, out, out) is det.
|
|
|
|
llds_common__process_procs([], Info, Info, []).
|
|
llds_common__process_procs([Proc0 | Procs0], Info0, Info, [Proc | Procs]) :-
|
|
llds_common__process_proc(Proc0, Info0, Info1, Proc),
|
|
llds_common__process_procs(Procs0, Info1, Info, Procs).
|
|
|
|
:- pred llds_common__process_proc(c_procedure, common_info, common_info,
|
|
c_procedure).
|
|
:- mode llds_common__process_proc(in, in, out, out) is det.
|
|
|
|
llds_common__process_proc(Proc0, Info0, Info, Proc) :-
|
|
Proc0 = c_procedure(Name, Arity, PredProcId, Instrs0),
|
|
llds_common__process_instrs(Instrs0, Info0, Info, Instrs),
|
|
Proc = c_procedure(Name, Arity, PredProcId, Instrs).
|
|
|
|
:- pred llds_common__process_instrs(list(instruction),
|
|
common_info, common_info, list(instruction)).
|
|
:- mode llds_common__process_instrs(in, in, out, out) is det.
|
|
|
|
llds_common__process_instrs([], Info, Info, []).
|
|
llds_common__process_instrs([Uinstr0 - Comment | Instrs0], Info0, Info,
|
|
[Uinstr - Comment | Instrs]) :-
|
|
llds_common__process_instr(Uinstr0, Info0, Info1, Uinstr),
|
|
llds_common__process_instrs(Instrs0, Info1, Info, Instrs).
|
|
|
|
:- pred llds_common__process_instr(instr, common_info, common_info, instr).
|
|
:- mode llds_common__process_instr(in, in, out, out) is det.
|
|
|
|
llds_common__process_instr(Instr0, Info0, Info, Instr) :-
|
|
(
|
|
Instr0 = comment(_),
|
|
Instr = Instr0,
|
|
Info = Info0
|
|
;
|
|
Instr0 = livevals(_),
|
|
Instr = Instr0,
|
|
Info = Info0
|
|
;
|
|
Instr0 = block(NR, NF, Instrs0),
|
|
llds_common__process_instrs(Instrs0, Info0, Info, Instrs),
|
|
Instr = block(NR, NF, Instrs)
|
|
;
|
|
Instr0 = assign(Lval, Rval0),
|
|
llds_common__process_rval(Rval0, Info0, Info, Rval),
|
|
Instr = assign(Lval, Rval)
|
|
;
|
|
Instr0 = call(_, _, _, _),
|
|
Instr = Instr0,
|
|
Info = Info0
|
|
;
|
|
Instr0 = mkframe(_, _, _, _),
|
|
Instr = Instr0,
|
|
Info = Info0
|
|
;
|
|
Instr0 = modframe(_),
|
|
Instr = Instr0,
|
|
Info = Info0
|
|
;
|
|
Instr0 = label(_),
|
|
Instr = Instr0,
|
|
Info = Info0
|
|
;
|
|
Instr0 = goto(_),
|
|
Instr = Instr0,
|
|
Info = Info0
|
|
;
|
|
% unlikely to find anything to share, but why not try?
|
|
Instr0 = computed_goto(Rval0, Labels),
|
|
llds_common__process_rval(Rval0, Info0, Info, Rval),
|
|
Instr = computed_goto(Rval, Labels)
|
|
;
|
|
Instr0 = c_code(_),
|
|
Instr = Instr0,
|
|
Info = Info0
|
|
;
|
|
% unlikely to find anything to share, but why not try?
|
|
Instr0 = if_val(Rval0, Target),
|
|
llds_common__process_rval(Rval0, Info0, Info, Rval),
|
|
Instr = if_val(Rval, Target)
|
|
;
|
|
% unlikely to find anything to share, but why not try?
|
|
Instr0 = incr_hp(Lval, MaybeTag, Rval0, Msg),
|
|
llds_common__process_rval(Rval0, Info0, Info, Rval),
|
|
Instr = incr_hp(Lval, MaybeTag, Rval, Msg)
|
|
;
|
|
Instr0 = mark_hp(_),
|
|
Instr = Instr0,
|
|
Info = Info0
|
|
;
|
|
% unlikely to find anything to share, but why not try?
|
|
Instr0 = restore_hp(Rval0),
|
|
llds_common__process_rval(Rval0, Info0, Info, Rval),
|
|
Instr = restore_hp(Rval)
|
|
;
|
|
Instr0 = store_ticket(_),
|
|
Instr = Instr0,
|
|
Info = Info0
|
|
;
|
|
% unlikely to find anything to share, but why not try?
|
|
Instr0 = reset_ticket(Rval0, Reason),
|
|
llds_common__process_rval(Rval0, Info0, Info, Rval),
|
|
Instr = reset_ticket(Rval, Reason)
|
|
;
|
|
Instr0 = discard_ticket,
|
|
Instr = Instr0,
|
|
Info = Info0
|
|
;
|
|
Instr0 = mark_ticket_stack(_),
|
|
Instr = Instr0,
|
|
Info = Info0
|
|
;
|
|
Instr0 = discard_tickets_to(_),
|
|
Instr = Instr0,
|
|
Info = Info0
|
|
;
|
|
Instr0 = incr_sp(_, _),
|
|
Instr = Instr0,
|
|
Info = Info0
|
|
;
|
|
Instr0 = decr_sp(_),
|
|
Instr = Instr0,
|
|
Info = Info0
|
|
;
|
|
Instr0 = pragma_c(_, _, _, _, _),
|
|
Instr = Instr0,
|
|
Info = Info0
|
|
).
|
|
|
|
:- pred llds_common__process_rval(rval, common_info, common_info, rval).
|
|
:- mode llds_common__process_rval(in, in, out, out) is det.
|
|
|
|
llds_common__process_rval(Rval0, Info0, Info, Rval) :-
|
|
(
|
|
Rval0 = lval(_),
|
|
Rval = Rval0,
|
|
Info = Info0
|
|
;
|
|
Rval0 = var(_),
|
|
error("var rval found in llds_common__process_rval")
|
|
;
|
|
Rval0 = create(Tag, Args, Unique, _LabelNo, _Msg),
|
|
( Unique = no ->
|
|
llds_common__process_create(Tag, Args, Info0,
|
|
Info, Rval)
|
|
;
|
|
Rval = Rval0,
|
|
Info = Info0
|
|
)
|
|
;
|
|
Rval0 = mkword(Tag, SubRval0),
|
|
llds_common__process_rval(SubRval0, Info0, Info, SubRval),
|
|
Rval = mkword(Tag, SubRval)
|
|
;
|
|
Rval0 = const(_),
|
|
Rval = Rval0,
|
|
Info = Info0
|
|
;
|
|
Rval0 = unop(Unop, SubRval0),
|
|
llds_common__process_rval(SubRval0, Info0, Info, SubRval),
|
|
Rval = unop(Unop, SubRval)
|
|
;
|
|
Rval0 = binop(Binop, Left0, Right0),
|
|
llds_common__process_rval(Left0, Info0, Info1, Left),
|
|
llds_common__process_rval(Right0, Info1, Info, Right),
|
|
Rval = binop(Binop, Left, Right)
|
|
;
|
|
Rval0 = mem_addr(MemRef0),
|
|
llds_common__process_mem_ref(MemRef0, Info0, Info, MemRef),
|
|
Rval = mem_addr(MemRef)
|
|
).
|
|
|
|
:- pred llds_common__process_mem_ref(mem_ref, common_info, common_info,
|
|
mem_ref).
|
|
:- mode llds_common__process_mem_ref(in, in, out, out) is det.
|
|
|
|
llds_common__process_mem_ref(stackvar_ref(N), Info, Info, stackvar_ref(N)).
|
|
llds_common__process_mem_ref(framevar_ref(N), Info, Info, framevar_ref(N)).
|
|
llds_common__process_mem_ref(heap_ref(Rval0, Tag, N), Info0, Info,
|
|
heap_ref(Rval, Tag, N)) :-
|
|
llds_common__process_rval(Rval0, Info0, Info, Rval).
|
|
|
|
:- pred llds_common__process_rvals(list(rval), common_info, common_info,
|
|
list(rval)).
|
|
:- mode llds_common__process_rvals(in, in, out, out) is det.
|
|
|
|
llds_common__process_rvals([], Info, Info, []).
|
|
llds_common__process_rvals([Rval0 | Rvals0], Info0, Info,
|
|
[Rval | Rvals]) :-
|
|
llds_common__process_rval(Rval0, Info0, Info1, Rval),
|
|
llds_common__process_rvals(Rvals0, Info1, Info, Rvals).
|
|
|
|
:- pred llds_common__process_maybe_rvals(list(maybe(rval)),
|
|
common_info, common_info, list(maybe(rval))).
|
|
:- mode llds_common__process_maybe_rvals(in, in, out, out) is det.
|
|
|
|
llds_common__process_maybe_rvals([], Info, Info, []).
|
|
llds_common__process_maybe_rvals([MaybeRval0 | MaybeRvals0], Info0, Info,
|
|
[MaybeRval | MaybeRvals]) :-
|
|
(
|
|
MaybeRval0 = yes(Rval0),
|
|
llds_common__process_rval(Rval0, Info0, Info1, Rval),
|
|
MaybeRval = yes(Rval)
|
|
;
|
|
MaybeRval0 = no,
|
|
MaybeRval = no,
|
|
Info1 = Info0
|
|
),
|
|
llds_common__process_maybe_rvals(MaybeRvals0, Info1, Info, MaybeRvals).
|