mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-14 21:35:49 +00:00
Estimated hours taken: 10 Branches: main This diff changes a few types from being defined as equivalent to a pair to being discriminated union types with their own function symbol. This was motivated by an error message (one of many, but the one that broke the camel's back) about "-" being used in an ambiguous manner. It will reduce the number of such messages in the future, and will make compiler data structures easier to inspect in the debugger. The most important type changed by far is hlds_goal, whose function symbol is now "hlds_goal". Second and third in importance are llds.instruction (function symbol "llds_instr") and prog_item.m's item_and_context (function symbol "item_and_context"). There are some others as well. In several places, I rearranged predicates to factor the deconstruction of goals into hlds_goal_expr and hlds_goal_into out of each clause into a single point. In many places, I changed variable names that used "Goal" to refer to just hlds_goal_exprs to use "GoalExpr" instead. I also changed variable names that used "Item" to refer to item_and_contexts to use "ItemAndContext" instead. This should make reading such code less confusing. I renamed some function symbols and predicates to avoid ambiguities. I only made one algorithmic change (at least intentionally). In assertion.m, comparing two goals for equality now ignores goal_infos for all kinds of goals, whereas previously it ignored them for most kinds of goals, but for shorthand goals it was insisting on them being equal. This seemed to me to be a bug. Pete, can you confirm this?
93 lines
3.4 KiB
Mathematica
93 lines
3.4 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 1997-1998, 2003-2007 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.
|
|
%-----------------------------------------------------------------------------%
|
|
%
|
|
% File: delay_slot.m.
|
|
% Main author: zs.
|
|
%
|
|
% This module attempts to fill the delay slots of branch instructions. Since
|
|
% it assumes the existence of one delay slot after every branch, this module
|
|
% should not be invoked if the architecture we are targeting has no delay
|
|
% slots on its branch instructions.
|
|
%
|
|
% Since we are generating C code, not assembler, we cannot fill slots
|
|
% directly. However, we can let the C compiler know that the instruction
|
|
% immediately after a condition branch instruction can be safely executed even
|
|
% if the branch is not taken. We can do so by moving the instruction before
|
|
% the conditional branch. This actually requires the instruction to be
|
|
% executed in both continuations, therefore we want to do this only if there
|
|
% is no chance that the C compiler would be able to fill the delay slot any
|
|
% other way.
|
|
%
|
|
% The only code pattern that we recognize as being both safe and advantageous
|
|
% to transform the code in order to fill a delay slot is this pattern,
|
|
% produced by frameopt:
|
|
%
|
|
% L1:
|
|
% if (cond) goto L2
|
|
% incr_sp(N)
|
|
% stackvar(N) = succip
|
|
% ...
|
|
%
|
|
% We transform this code into:
|
|
%
|
|
% L1:
|
|
% stackvar(0) = succip
|
|
% if (cond) goto L2
|
|
% incr_sp(N)
|
|
% ...
|
|
%
|
|
% The initial store into stackvar(0) is into the first word above the
|
|
% current detstack top. After the incr_sp is executed (if it ever is), this
|
|
% word will become the bottom stack slot of the new frame, i.e. stackvar(N).
|
|
%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module ll_backend.delay_slot.
|
|
:- interface.
|
|
|
|
:- import_module ll_backend.llds.
|
|
|
|
:- import_module list.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- pred fill_branch_delay_slot(list(instruction)::in, list(instruction)::out)
|
|
is det.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module pair.
|
|
:- import_module string.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
fill_branch_delay_slot([], []).
|
|
fill_branch_delay_slot([Instr0 | Instrs0], Instrs) :-
|
|
(
|
|
Instr0 = llds_instr(label(_), _),
|
|
Instrs0 = [Instr1, Instr2, Instr3 | Tail0],
|
|
Instr1 = llds_instr(if_val(_, _), _),
|
|
Instr2 = llds_instr(incr_sp(Size, _, _), _),
|
|
Instr3 = llds_instr(assign(stackvar(Size), lval(succip)), C2)
|
|
->
|
|
fill_branch_delay_slot(Tail0, Tail1),
|
|
string.append(C2, " (early save in delay slot)", NewC2),
|
|
EarlySave = llds_instr(assign(stackvar(0), lval(succip)), NewC2),
|
|
Instrs = [Instr0, EarlySave, Instr1, Instr2 | Tail1]
|
|
;
|
|
fill_branch_delay_slot(Instrs0, Instrs1),
|
|
Instrs = [Instr0 | Instrs1]
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
:- end_module delay_slot.
|
|
%-----------------------------------------------------------------------------%
|