Files
mercury/compiler/mode_top_functor.m
Zoltan Somogyi d76c7bf617 Break up inst_util.m and mode_util.m.
This step significantly improves module cohesion.

compiler/inst_abstract_unify.m:
    New module carved out of inst_util.m, which does abstract unifications
    on insts.

compiler/inst_merge.m:
    New module carved out of inst_util.m, which merges insts.

compiler/inst_lookup.m:
    New module carved partly out of inst_util.m and partly out of mode_util.m,
    which looks up insts in the module_info, and then possibly expands out
    the result.

compiler/mode_test.m:
    New module carved out of mode_util.m, whose predicates
    perform tests on modes.

compiler/mode_top_functor.m:
    New module carved out of mode_util.m, which computes top_functor_modes
    from modes.

compiler/inst_mode_type_prop.m:
    New module carved out of mode_util.m, which propagates type information
    into both insts and modes.

compiler/recompute_instmap_deltas.m:
    New module carved out of mode_util.m, which recomputes goals'
    instmap_deltas.

compiler/inst_test.m:
    Move here the predicates in inst_util.m that perform tests on insts.

compiler/inst_util.m:
compiler/mode_util.m:
    Delete the code that this diff moves to other modules.

compiler/check_hlds.m:
    Add the new modules to the check_hlds package, the package that also
    contains inst_util.m and mode_util.m. (Some of these modules could
    be argued to fit better in the hlds package, but moving them there
    would not be desirable while they depend on code that is still in the
    check_hlds package.)

compiler/notes/compiler_design.html:
    Document the new modules.

compiler/add_pragma_tabling.m:
compiler/arg_info.m:
compiler/bytecode_gen.m:
compiler/closure_analysis.m:
compiler/complexity.m:
compiler/deep_profiling.m:
compiler/deforest.m:
compiler/dep_par_conj.m:
compiler/det_report.m:
compiler/direct_arg_in_out.m:
compiler/distance_granularity.m:
compiler/equiv_type_hlds.m:
compiler/error_msg_inst.m:
compiler/fact_table.m:
compiler/float_regs.m:
compiler/follow_code.m:
compiler/goal_util.m:
compiler/higher_order.m:
compiler/hlds_pred.m:
compiler/hlds_rtti.m:
compiler/inlining.m:
compiler/inst_match.m:
compiler/inst_user.m:
compiler/instmap.m:
compiler/intermod.m:
compiler/interval.m:
compiler/introduce_exists_casts.m:
compiler/lambda.m:
compiler/lco.m:
compiler/liveness.m:
compiler/lookup_util.m:
compiler/loop_inv.m:
compiler/mark_tail_calls.m:
compiler/ml_args_util.m:
compiler/ml_code_util.m:
compiler/ml_foreign_proc_gen.m:
compiler/ml_unify_gen_construct.m:
compiler/ml_unify_gen_util.m:
compiler/mode_constraints.m:
compiler/mode_errors.m:
compiler/modecheck_call.m:
compiler/modecheck_coerce.m:
compiler/modecheck_goal.m:
compiler/modecheck_unify.m:
compiler/modecheck_util.m:
compiler/modes.m:
compiler/oisu_check.m:
compiler/par_conj_gen.m:
compiler/pd_util.m:
compiler/post_typecheck.m:
compiler/pragma_c_gen.m:
compiler/proc_requests.m:
compiler/prog_rep.m:
compiler/push_goals_together.m:
compiler/rbmm.region_transformation.m:
compiler/saved_vars.m:
compiler/simplify_goal_switch.m:
compiler/simplify_proc.m:
compiler/size_prof.m:
compiler/ssdebug.m:
compiler/stack_opt.m:
compiler/stm_expand.m:
compiler/stratify.m:
compiler/structure_reuse.versions.m:
compiler/structure_sharing.domain.m:
compiler/superhomogeneous.m:
compiler/table_gen.m:
compiler/term_constr_build.m:
compiler/term_pass2.m:
compiler/term_util.m:
compiler/tupling.m:
compiler/unify_gen_construct.m:
compiler/unify_gen_util.m:
compiler/unique_modes.m:
compiler/unneeded_code.m:
compiler/untupling.m:
compiler/unused_args.m:
    Conform to the changes above by importing the required new modules,
    sometimes in addition to inst_util.m or mode_util.m, but more usually
    instead of them.
2021-12-16 01:31:35 +11:00

198 lines
7.5 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%---------------------------------------------------------------------------%
% Copyright (C) 1994-2012 The University of Melbourne.
% Copyright (C) 2015 The Mercury team.
% This file may only be copied under the terms of the GNU General
% Public License - see the file COPYING in the Mercury distribution.
%---------------------------------------------------------------------------%
%
% File: mode_top_functor.m.
%
% This module computes top_functor_modes.
%
%---------------------------------------------------------------------------%
:- module check_hlds.mode_top_functor.
:- interface.
:- import_module hlds.
:- import_module hlds.hlds_module.
:- import_module hlds.hlds_pred.
:- import_module parse_tree.
:- import_module parse_tree.prog_data.
:- import_module list.
%---------------------------------------------------------------------------%
%
% Computing top_functor_modes.
%
% mode_to_top_functor_mode converts a mode (and corresponding type)
% to a top_functor_mode.
%
% A mode is a high-level notion, the normal Mercury language mode.
% A top_functor_mode is a low-level notion used for code generation,
% which indicates the argument passing convention (top_in, top_out, or
% top_unused) that corresponds to that mode. We need to know the type,
% not just the mode, because the argument passing convention can depend
% on the type's representation.
%
:- pred mode_to_top_functor_mode(module_info::in, mer_mode::in,
mer_type::in, top_functor_mode::out) is det.
:- pred init_final_insts_to_top_functor_mode(module_info::in,
mer_inst::in, mer_inst::in, mer_type::in, top_functor_mode::out) is det.
% Zip together the given lists of modes and types, and return
% the top_functor_mode computed by mode_to_top_functor_mode
% for each pair.
%
:- pred modes_to_top_functor_modes(module_info::in,
list(mer_mode)::in, list(mer_type)::in, list(top_functor_mode)::out)
is det.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module check_hlds.inst_lookup.
:- import_module check_hlds.inst_test.
:- import_module check_hlds.mode_util.
:- import_module check_hlds.type_util.
:- import_module mdbcomp.
:- import_module mdbcomp.sym_name.
:- import_module parse_tree.prog_type.
:- import_module require.
:- import_module term.
%---------------------------------------------------------------------------%
mode_to_top_functor_mode(ModuleInfo, Mode, Type, TopFunctorMode) :-
mode_get_insts(ModuleInfo, Mode, InitialInst, FinalInst),
find_top_functor_mode_loop_over_notags(ModuleInfo, Type, [],
InitialInst, FinalInst, TopFunctorMode).
init_final_insts_to_top_functor_mode(ModuleInfo, InitialInst, FinalInst, Type,
TopFunctorMode) :-
find_top_functor_mode_loop_over_notags(ModuleInfo, Type, [],
InitialInst, FinalInst, TopFunctorMode).
:- pred find_top_functor_mode_loop_over_notags(module_info::in,
mer_type::in, list(type_ctor)::in, mer_inst::in, mer_inst::in,
top_functor_mode::out) is det.
find_top_functor_mode_loop_over_notags(ModuleInfo, Type, ContainingTypes,
InitialInst, FinalInst, TopFunctorMode) :-
% We need to handle no_tag types (types which have exactly one constructor,
% and whose one constructor has exactly one argument) specially here,
% since for them an inst of bound(f(free)) is not really bound as far as
% code generation is concerned, since the f/1 will get optimized away.
( if
% Is this a no_tag type?
type_is_no_tag_type(ModuleInfo, Type, FunctorName, ArgType),
% Avoid infinite recursion.
type_to_ctor(Type, TypeCtor),
not list.member(TypeCtor, ContainingTypes)
then
% The top_functor_mode will be determined by the mode and type of the
% functor's argument, so we figure out the mode and type of the
% argument, and then recurse.
ConsId = cons(FunctorName, 1, TypeCtor),
get_single_arg_inst(ModuleInfo, InitialInst, ConsId, InitialArgInst),
get_single_arg_inst(ModuleInfo, FinalInst, ConsId, FinalArgInst),
find_top_functor_mode_loop_over_notags(ModuleInfo,
ArgType, [TypeCtor | ContainingTypes],
InitialArgInst, FinalArgInst, TopFunctorMode)
else
( if inst_is_bound(ModuleInfo, InitialInst) then
TopFunctorMode = top_in
else if inst_is_bound(ModuleInfo, FinalInst) then
TopFunctorMode = top_out
else
TopFunctorMode = top_unused
)
).
% get_single_arg_inst(ModuleInfo, Inst, ConsId, ArgInsts):
%
% Given an inst `Inst', figure out what the inst of the argument would be,
% assuming that the functor is the one given by the specified ConsId,
% whose arity is 1.
%
:- pred get_single_arg_inst(module_info::in, mer_inst::in, cons_id::in,
mer_inst::out) is det.
get_single_arg_inst(ModuleInfo, Inst, ConsId, ArgInst) :-
% XXX This is very similar to get_arg_insts in prog_mode.
(
Inst = defined_inst(InstName),
inst_lookup(ModuleInfo, InstName, NamedInst),
get_single_arg_inst(ModuleInfo, NamedInst, ConsId, ArgInst)
;
Inst = not_reached,
ArgInst = not_reached
;
Inst = ground(Uniq, _PredInst),
ArgInst = ground(Uniq, none_or_default_func)
;
Inst = bound(_Uniq, _InstResult, List),
( if get_single_arg_inst_in_bound_insts(List, ConsId, ArgInst0) then
ArgInst = ArgInst0
else
% The code is unreachable.
ArgInst = not_reached
)
;
Inst = free,
ArgInst = free
;
Inst = free(_Type),
ArgInst = free % XXX loses type info
;
Inst = any(Uniq, _),
ArgInst = any(Uniq, none_or_default_func)
;
Inst = abstract_inst(_, _),
unexpected($pred, "abstract insts not supported")
;
Inst = inst_var(_),
unexpected($pred, "inst_var")
;
Inst = constrained_inst_vars(_, InsideInst),
get_single_arg_inst(ModuleInfo, InsideInst, ConsId, ArgInst)
).
:- pred get_single_arg_inst_in_bound_insts(list(bound_inst)::in, cons_id::in,
mer_inst::out) is semidet.
get_single_arg_inst_in_bound_insts([BoundInst | BoundInsts], ConsId,
ArgInst) :-
( if
BoundInst = bound_functor(InstConsId, [ArgInst0]),
% The cons_ids for types and insts can differ in the type_ctor field
% so we must ignore them.
equivalent_cons_ids(ConsId, InstConsId)
then
ArgInst = ArgInst0
else
get_single_arg_inst_in_bound_insts(BoundInsts, ConsId, ArgInst)
).
modes_to_top_functor_modes(_ModuleInfo, [], [], []).
modes_to_top_functor_modes(_ModuleInfo, [], [_ | _], _) :-
unexpected($pred, "length mismatch").
modes_to_top_functor_modes(_ModuleInfo, [_ | _], [], _) :-
unexpected($pred, "length mismatch").
modes_to_top_functor_modes(ModuleInfo, [Mode | Modes], [Type | Types],
[TopFunctorMode | TopFunctorModes]) :-
mode_to_top_functor_mode(ModuleInfo, Mode, Type, TopFunctorMode),
modes_to_top_functor_modes(ModuleInfo, Modes, Types, TopFunctorModes).
%---------------------------------------------------------------------------%
:- end_module check_hlds.mode_top_functor.
%---------------------------------------------------------------------------%