mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-19 07:45:09 +00:00
Estimated hours taken: 1 Branches: main compiler/stdlabel.m: A new module that contains code to standardize labels in the LLDS. compiler/ll_backend.m: Include the new module in this package. compiler/options.m: Add an option that governs whether stdlabel.m is invoked or not. compiler/optimize.m: If the option is set, invoke stdlabel.m. compiler/opt_util.m: Add an option to opt_util.replace_labels_instruction_list to allow it to replace labels in label instructions themselves. compiler/dupelim.m: Conform to the changes in opt_util.m compiler/notes/compiler_design.html: Document the new module.
95 lines
3.4 KiB
Mathematica
95 lines
3.4 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 2006 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: stdlabel.m.
|
|
% Author: zs.
|
|
%
|
|
% Module to rename (actually renumber) all the labels in a procedure so that
|
|
% the label numbers are in a dense ascending sequence.
|
|
%
|
|
% This transformation leaves the performance of the program unaffected.
|
|
% However, one can use it make the effect of an optimization much more readily
|
|
% apparent. The idea is that enabling --standardize-labels when compiling
|
|
% a program both with and without a new or modified optimization cleans up
|
|
% the output of the diff between the two sets of resulting .c files, by
|
|
% eliminating incidental differences in labels that could otherwise overwhelm
|
|
% the real differences made by the optimization.
|
|
%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module ll_backend.stdlabel.
|
|
:- interface.
|
|
|
|
:- import_module ll_backend.llds.
|
|
|
|
:- import_module counter.
|
|
:- import_module list.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- pred standardize_labels(list(instruction)::in, list(instruction)::out,
|
|
counter::in, counter::out) is det.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module libs.compiler_util.
|
|
:- import_module ll_backend.opt_util.
|
|
:- import_module mdbcomp.prim_data.
|
|
|
|
:- import_module bool.
|
|
:- import_module int.
|
|
:- import_module map.
|
|
:- import_module pair.
|
|
:- import_module svmap.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
standardize_labels(Instrs0, Instrs, _, !:ProcCounter) :-
|
|
opt_util.get_prologue(Instrs0, LabelInstr, Comments, Instrs1),
|
|
(
|
|
LabelInstr = label(FirstLabel) - _,
|
|
FirstLabel = entry(_, ProcLabel)
|
|
->
|
|
build_std_map(Instrs1, ProcLabel, counter.init(1), !:ProcCounter,
|
|
map.init, Map),
|
|
replace_labels_instruction_list(Instrs1, Map, yes, yes, Instrs2),
|
|
Instrs = [LabelInstr | Comments] ++ Instrs2
|
|
;
|
|
unexpected(this_file, "standardize_labels: no proc_label")
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- pred build_std_map(list(instruction)::in, proc_label::in,
|
|
counter::in, counter::out,
|
|
map(label, label)::in, map(label, label)::out) is det.
|
|
|
|
build_std_map([], _, !Counter, !Map).
|
|
build_std_map([Instr | Instrs], ProcLabel, !Counter, !Map) :-
|
|
( Instr = label(Label) - _ ->
|
|
counter.allocate(LabelNum, !Counter),
|
|
StdLabel = internal(LabelNum, ProcLabel),
|
|
svmap.det_insert(Label, StdLabel, !Map)
|
|
;
|
|
true
|
|
),
|
|
build_std_map(Instrs, ProcLabel, !Counter, !Map).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- func this_file = string.
|
|
|
|
this_file = "stdlabel.m".
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
:- end_module stdlabel.
|
|
%-----------------------------------------------------------------------------%
|