mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-17 06:47:17 +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.
204 lines
6.4 KiB
Mathematica
204 lines
6.4 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 1997-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.
|
|
%-----------------------------------------------------------------------------%
|
|
%
|
|
% Main author: zs.
|
|
%
|
|
% This module defines a representation for basic blocks, sequences of
|
|
% instructions with one entry and one exit, and provides predicates
|
|
% that convert a list of instructions into a list of basic blocks
|
|
% and vice versa.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module basic_block.
|
|
|
|
:- interface.
|
|
|
|
:- import_module llds.
|
|
:- import_module list, map, std_util.
|
|
|
|
:- type block_map == map(label, block_info).
|
|
|
|
:- type block_info
|
|
---> block_info(
|
|
label,
|
|
% The label starting the block.
|
|
instruction,
|
|
% The instruction containing the label.
|
|
list(instruction),
|
|
% The code of the block without the initial
|
|
% label.
|
|
list(label),
|
|
% The labels we can jump to
|
|
% (not falling through).
|
|
maybe(label)
|
|
% The label we fall through to
|
|
% (if there is one).
|
|
).
|
|
|
|
:- pred create_basic_blocks(list(instruction)::in, list(instruction)::out,
|
|
proc_label::out, int::out, list(label)::out, block_map::out) is det.
|
|
|
|
:- pred flatten_basic_blocks(list(label)::in, block_map::in,
|
|
list(instruction)::out) is det.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module opt_util.
|
|
:- import_module bool, int, require.
|
|
|
|
create_basic_blocks(Instrs0, Comments, ProcLabel, N,
|
|
LabelSeq, BlockMap) :-
|
|
opt_util__get_prologue(Instrs0, ProcLabel, LabelInstr,
|
|
Comments, AfterLabelInstrs),
|
|
Instrs1 = [LabelInstr | AfterLabelInstrs],
|
|
opt_util__new_label_no(Instrs0, 1000, N0),
|
|
map__init(BlockMap0),
|
|
build_block_map(Instrs1, LabelSeq, BlockMap0, BlockMap,
|
|
ProcLabel, N0, N).
|
|
|
|
% Add labels to the given instruction sequence so that
|
|
% every basic block has labels around it.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- pred build_block_map(list(instruction)::in, list(label)::out,
|
|
block_map::in, block_map::out, proc_label::in, int::in, int::out)
|
|
is det.
|
|
|
|
build_block_map([], [], BlockMap, BlockMap, _, N, N).
|
|
build_block_map([OrigInstr0 | OrigInstrs0], LabelSeq, BlockMap0, BlockMap,
|
|
ProcLabel, N0, N) :-
|
|
( OrigInstr0 = label(OrigLabel) - _ ->
|
|
Label = OrigLabel,
|
|
LabelInstr = OrigInstr0,
|
|
RestInstrs = OrigInstrs0,
|
|
N1 = N0
|
|
;
|
|
N1 is N0 + 1,
|
|
Label = local(ProcLabel, N0),
|
|
LabelInstr = label(Label) - "",
|
|
RestInstrs = [OrigInstr0 | OrigInstrs0]
|
|
),
|
|
(
|
|
take_until_end_of_block(RestInstrs, BlockInstrs, Instrs1),
|
|
build_block_map(Instrs1, LabelSeq0,
|
|
BlockMap0, BlockMap1, ProcLabel, N1, N),
|
|
( list__last(BlockInstrs, LastInstr) ->
|
|
LastInstr = LastUinstr - _,
|
|
possible_targets(LastUinstr, SideLabels),
|
|
opt_util__can_instr_fall_through(LastUinstr,
|
|
CanFallThrough),
|
|
( CanFallThrough = yes ->
|
|
get_fallthrough_from_seq(LabelSeq0,
|
|
MaybeFallThrough)
|
|
;
|
|
MaybeFallThrough = no
|
|
)
|
|
;
|
|
SideLabels = [],
|
|
get_fallthrough_from_seq(LabelSeq0,
|
|
MaybeFallThrough)
|
|
),
|
|
BlockInfo = block_info(Label, LabelInstr, BlockInstrs,
|
|
SideLabels, MaybeFallThrough),
|
|
map__det_insert(BlockMap1, Label, BlockInfo, BlockMap),
|
|
LabelSeq = [Label | LabelSeq0]
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- pred take_until_end_of_block(list(instruction)::in,
|
|
list(instruction)::out, list(instruction)::out) is det.
|
|
|
|
take_until_end_of_block([], [], []).
|
|
take_until_end_of_block([Instr0 | Instrs0], BlockInstrs, Rest) :-
|
|
Instr0 = Uinstr0 - _Comment,
|
|
( Uinstr0 = label(_) ->
|
|
BlockInstrs = [],
|
|
Rest = [Instr0 | Instrs0]
|
|
; opt_util__can_instr_branch_away(Uinstr0, yes) ->
|
|
BlockInstrs = [Instr0],
|
|
Rest = Instrs0
|
|
;
|
|
take_until_end_of_block(Instrs0, BlockInstrs1, Rest),
|
|
BlockInstrs = [Instr0 | BlockInstrs1]
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- pred get_fallthrough_from_seq(list(label)::in, maybe(label)::out) is det.
|
|
|
|
get_fallthrough_from_seq(LabelSeq, MaybeFallThrough) :-
|
|
( LabelSeq = [NextLabel | _] ->
|
|
MaybeFallThrough = yes(NextLabel)
|
|
;
|
|
MaybeFallThrough = no
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% Given an instruction, find the set of labels to which it can cause
|
|
% control to transfer. In the case of calls, this includes transfer
|
|
% via return from the called procedure.
|
|
|
|
:- pred possible_targets(instr::in, list(label)::out) is det.
|
|
|
|
possible_targets(comment(_), []).
|
|
possible_targets(livevals(_), []).
|
|
possible_targets(block(_, _, _), _) :-
|
|
error("block in possible_targets").
|
|
possible_targets(assign(_, _), []).
|
|
possible_targets(call(_, ReturnAddr, _, _), Labels) :-
|
|
( ReturnAddr = label(Label) ->
|
|
Labels = [Label]
|
|
;
|
|
Labels = []
|
|
).
|
|
possible_targets(mkframe(_, _, _, _), []).
|
|
possible_targets(modframe(_), []).
|
|
possible_targets(label(_), []).
|
|
possible_targets(goto(CodeAddr), Targets) :-
|
|
( CodeAddr = label(Label) ->
|
|
Targets = [Label]
|
|
;
|
|
Targets = []
|
|
).
|
|
possible_targets(computed_goto(_, Targets), Targets).
|
|
possible_targets(c_code(_), []).
|
|
possible_targets(if_val(_, CodeAddr), Targets) :-
|
|
( CodeAddr = label(Label) ->
|
|
Targets = [Label]
|
|
;
|
|
Targets = []
|
|
).
|
|
possible_targets(incr_hp(_, _, _, _), []).
|
|
possible_targets(mark_hp(_), []).
|
|
possible_targets(restore_hp(_), []).
|
|
possible_targets(store_ticket(_), []).
|
|
possible_targets(reset_ticket(_, _), []).
|
|
possible_targets(discard_ticket, []).
|
|
possible_targets(mark_ticket_stack(_), []).
|
|
possible_targets(discard_tickets_to(_), []).
|
|
possible_targets(incr_sp(_, _), []).
|
|
possible_targets(decr_sp(_), []).
|
|
possible_targets(pragma_c(_, _, _, _, _), []).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
flatten_basic_blocks([], _, []).
|
|
flatten_basic_blocks([Label | Labels], BlockMap, Instrs) :-
|
|
flatten_basic_blocks(Labels, BlockMap, RestInstrs),
|
|
map__lookup(BlockMap, Label, BlockInfo),
|
|
BlockInfo = block_info(_, BlockLabelInstr, BlockInstrs, _, _),
|
|
list__append([BlockLabelInstr | BlockInstrs], RestInstrs, Instrs).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|