Files
mercury/compiler/rbmm.execution_path.m
Zoltan Somogyi d69ba1a1f0 Include the type_ctor in cons_ids for user-defined types.
Estimated hours taken: 32
Branches: main

Include the type_ctor in cons_ids for user-defined types. The intention is
two-fold:

- It prepares for a future in which we allow more than one function symbol to
  with the same name to be defined in a module.

- It makes the HLDS code more self-contained. In many places, processing
  construction and deconstruction unifications required knowing which type
  the cons_id belongs to, but until now, code couldn't know that unless it
  kept track of the type of the variable unified with the cons_id.

With this diff, user-defined cons_ids are represented as

	cons(SymName, Arity, TypeCtor)

The last field is filled in during post-typecheck. After that time, any module
qualification in the SymName (which may initially be partial) is redundant,
since it is also available in the TypeCtor.

In the future, we could make all those SymNames be just unqualified(_) at that
time. We could also replace the current maps in HLDS type definitions with
full cons_id keys with just name/arity keys (since the module qualifier is a
given for any given type definition), we could also support partially
qualified cons_ids in source code using a map from name/arity pairs to a list
of all the type_ctors that have function symbols with that name/arity, instead
of our current practice of inserting all possible partially module qualified
version of every cons_id into a single giant table, and we could do the same
thing with the field names table.

This diff also separates tuples out from user-defined types, since in many
respects they are different (they don't have a single type_ctor, for starters).
It also separates out character constants, since they were alreay treated
specially in most places, though not in some places where they *ought* to
have been treated specially. Take the opportunity to give some other cons_ids
better names.

compiler/prog_data.m:
	Make the change described above, and document it.

	Put the implementations of the predicates declared in each part
	of this module next to the declarations, instead of keeping all the
	code until the very end (where it was usually far from their
	declarations).

	Remove three predicates with identical definitions from inst_match.m,
	inst_util.m and mode_constraints.m, and put the common definition
	in prog_data.m.

library/term_io.m:
	Add a new predicate that is basically a reversible version of
	the existing function espaced_char, since the definition of char_consts
	needs reversibilty.

compiler/post_typecheck.m:
	For functors of user-defined types, record their type_ctor. For tuples
	and char constants, record them as such.

compiler/builtin_lib_types.m:
compiler/parse_tree.m:
compiler/notes/compiler_design.html:
	New module to centralize knowledge about builtin types, specially
	handled library types, and their function symbols. Previously,
	the stuff now in this module used to be in several different places,
	including prog_type.m and stm_expand.m, and some of it was duplicated.

mdbcomp/prim_data.m:
	Add some predicates now needed by builtin_lib_types.m.

compiler/builtin_ops.m:
	Factor out some duplicated code.

compiler/add_type.m:
	Include the relevant type_ctors in the cons_ids generated in type
	definitions.

compiler/hlds_data.m:
	Document an existing type better.

	Rename a cons_tag in sync with its corresponding cons_id.

	Put some declarations into logical order.

compiler/hlds_out.m:
	Rename a misleadingly-named predicate.

compiler/prog_ctgc.m:
compiler/term_constr_build.m:
	Add XXXs for questionable existing code.

compiler/add_clause.m:
compiler/add_heap_ops.m:
compiler/add_pragma.m:
compiler/add_pred.m:
compiler/add_trail_ops.m:
compiler/assertion.m:
compiler/bytecode_gen.m:
compiler/closure_analysis.m:
compiler/code_info.m:
compiler/complexity.m:
compiler/ctgc_selector.m:
compiler/dead_proc_elim.m:
compiler/deep_profiling.m:
compiler/delay_partial_inst.m:
compiler/dependency_graph.m:
compiler/det_analysis.m:
compiler/det_report.m:
compiler/distance_granularity.m:
compiler/erl_rtti.m:
compiler/erl_unify_gen.m:
compiler/export.m:
compiler/field_access.m:
compiler/foreign.m:
compiler/format_call.m:
compiler/hhf.m:
compiler/higher_order.m:
compiler/hlds_code_util.m:
compiler/hlds_desc.m:
compiler/hlds_goal.m:
compiler/implementation_defined_literals.m:
compiler/inst_check.m:
compiler/inst_graph.m:
compiler/inst_match.m:
compiler/inst_util.m:
compiler/instmap.m:
compiler/intermod.m:
compiler/interval.m:
compiler/lambda.m:
compiler/lco.m:
compiler/make_tags.m:
compiler/mercury_compile.m:
compiler/mercury_to_mercury.m:
compiler/middle_rec.m:
compiler/ml_closure_gen.m:
compiler/ml_code_gen.m:
compiler/ml_code_util.m:
compiler/ml_switch_gen.m:
compiler/ml_type_gen.m:
compiler/ml_unify_gen.m:
compiler/ml_util.m:
compiler/mlds_to_c.m:
compiler/mlds_to_java.m:
compiler/mode_constraints.m:
compiler/mode_errors.m:
compiler/mode_ordering.m:
compiler/mode_util.m:
compiler/modecheck_unify.m:
compiler/modes.m:
compiler/module_qual.m:
compiler/polymorphism.m:
compiler/prog_ctgc.m:
compiler/prog_event.m:
compiler/prog_io_util.m:
compiler/prog_mode.m:
compiler/prog_mutable.m:
compiler/prog_out.m:
compiler/prog_type.m:
compiler/prog_util.m:
compiler/purity.m:
compiler/qual_info.m:
compiler/rbmm.add_rbmm_goal_infos.m:
compiler/rbmm.execution_path.m:
compiler/rbmm.points_to_analysis.m:
compiler/rbmm.region_transformation.m:
compiler/recompilation.usage.m:
compiler/rtti.m:
compiler/rtti_out.m:
compiler/rtti_to_mlds.m:
compiler/simplify.m:
compiler/simplify.m:
compiler/special_pred.m:
compiler/ssdebug.m:
compiler/stack_opt.m:
compiler/stm_expand.m:
compiler/stratify.m:
compiler/structure_reuse.direct.detect_garbagem:
compiler/superhomoegenous.m:
compiler/switch_detection.m:
compiler/switch_gen.m:
compiler/switch_util.m:
compiler/table_gen.m:
compiler/term_constr_build.m:
compiler/term_norm.m:
compiler/try_expand.m:
compiler/type_constraints.m:
compiler/type_ctor_info.m:
compiler/type_util.m:
compiler/typecheck.m:
compiler/typecheck_errors.m:
compiler/unify_gen.m:
compiler/unify_proc.m:
compiler/unify_modes.m:
compiler/untupling.m:
compiler/unused_imports.m:
compiler/xml_documentation.m:
	Minor changes, mostly to ignore the type_ctor in cons_ids in places
	where it is not needed, take the type_ctor from the cons_id in places
	where it is more convenient, conform to the new names of some cons_ids,
	conform to the changes in hlds_out.m, and/or add now-needed imports
	of builtin_lib_types.m.

	In some places, the handling previously applied to cons/2 (which
	included tuples and character constants as well as user-defined
	function symbols) is now applied only to user-defined function symbols
	or to user-defined function symbols and tuples, as appropriate,
	with character constants being handled more like the other kinds of
	constants.

	In inst_match.m, rename a whole bunch of predicates to avoid
	ambiguities.

	In prog_util.m, remove two predicates that did almost nothing yet were
	far too easy to misuse.
2009-06-11 07:00:38 +00:00

302 lines
12 KiB
Mathematica

%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 2005-2009 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 rbmm.execution_path.m.
% Main author: Quan Phan.
%
% This module collects all execution paths (ExecPath) of procedures.
%
%-----------------------------------------------------------------------------%
:- module transform_hlds.rbmm.execution_path.
:- interface.
:- import_module hlds.
:- import_module hlds.hlds_module.
:- import_module transform_hlds.rbmm.region_liveness_info.
% Collects execution paths for each procedure.
%
:- pred execution_path_analysis(module_info::in, execution_path_table::out)
is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module check_hlds.
:- import_module check_hlds.goal_path.
:- import_module hlds.hlds_data.
:- import_module hlds.hlds_goal.
:- import_module hlds.hlds_pred.
:- import_module libs.
:- import_module libs.compiler_util.
:- import_module parse_tree.
:- import_module parse_tree.prog_data.
:- import_module transform_hlds.smm_common.
:- import_module pair.
:- import_module string.
:- import_module svmap.
:- import_module list.
:- import_module map.
%-----------------------------------------------------------------------------%
%
% Execution path analysis
%
execution_path_analysis(ModuleInfo, ExecPathTable) :-
module_info_predids(PredIds, ModuleInfo, _),
map.init(ExecPathTable0),
list.foldl(execution_path_analysis_pred(ModuleInfo), PredIds,
ExecPathTable0, ExecPathTable).
:- pred execution_path_analysis_pred(module_info::in, pred_id::in,
execution_path_table::in, execution_path_table::out)
is det.
execution_path_analysis_pred(ModuleInfo, PredId, !ExecPathTable) :-
module_info_pred_info(ModuleInfo, PredId, PredInfo),
ProcIds = pred_info_non_imported_procids(PredInfo),
list.foldl(execution_path_analysis_proc(ModuleInfo, PredId), ProcIds,
!ExecPathTable).
:- pred execution_path_analysis_proc(module_info::in, pred_id::in,
proc_id::in, execution_path_table::in, execution_path_table::out) is det.
execution_path_analysis_proc(ModuleInfo, PredId, ProcId, !ExecPathTable) :-
PPId = proc(PredId, ProcId),
( some_are_special_preds([PPId], ModuleInfo) ->
true
;
module_info_proc_info(ModuleInfo, PPId, ProcInfo),
compute_execution_paths(ProcInfo, ModuleInfo, ExecPaths),
svmap.set(PPId, ExecPaths, !ExecPathTable)
).
% Compute all execution paths in the procedure.
%
:- pred compute_execution_paths(proc_info::in, module_info::in,
list(execution_path)::out) is det.
compute_execution_paths(ProcInfo0, ModuleInfo, ExecPaths) :-
% Fill the goals with program point information
fill_goal_path_slots(ModuleInfo, ProcInfo0, ProcInfo),
proc_info_get_goal(ProcInfo, Goal),
ExecPaths0 = [[]],
execution_paths_covered_goal(ProcInfo, Goal, ExecPaths0, ExecPaths).
% Extend the given execution paths to cover this goal.
%
:- pred execution_paths_covered_goal(proc_info::in, hlds_goal::in,
list(execution_path)::in, list(execution_path)::out) is det.
execution_paths_covered_goal(ProcInfo, Goal, !ExecPaths) :-
Goal = hlds_goal(GoalExpr, GoalInfo),
HasSubGoals = goal_expr_has_subgoals(GoalExpr),
(
HasSubGoals = does_not_have_subgoals,
(
( GoalExpr = unify(_, _, _, _, _)
; GoalExpr = plain_call(_, _, _, _, _, _)
; GoalExpr = conj(_ConjType, [])
; GoalExpr = disj([])
)
->
% Retrieve the program point of this goal.
ProgPoint = program_point_init(GoalInfo),
append_to_each_execution_path(!.ExecPaths,
[[pair(ProgPoint, Goal)]], !:ExecPaths)
;
% XXX: other kinds of atomic calls (generic_call,
% foreign_proc), TEMPORARILY ignored their corresponding pps.
% XXX: handle event_call and unsafe_cast generic_calls
append_to_each_execution_path(!.ExecPaths, [[]], !:ExecPaths)
)
;
HasSubGoals = has_subgoals,
execution_paths_covered_compound_goal(ProcInfo, Goal, !ExecPaths)
).
% Extend current execution paths to cover this compound goal.
%
:- pred execution_paths_covered_compound_goal(proc_info::in, hlds_goal::in,
list(execution_path)::in, list(execution_path)::out) is det.
execution_paths_covered_compound_goal(ProcInfo, CompoundGoal, !ExecPaths) :-
CompoundGoal = hlds_goal(Expr, _),
(
Expr = conj(_ConjType, [Conj | Conjs]),
execution_paths_covered_conj(ProcInfo, [Conj | Conjs], !ExecPaths)
;
Expr = switch(_, _, Cases),
execution_paths_covered_cases(ProcInfo, CompoundGoal, Cases,
!ExecPaths)
;
Expr = disj([Disj | Disjs]),
execution_paths_covered_disj(ProcInfo, [Disj | Disjs],
!ExecPaths)
;
Expr = negation(Goal),
execution_paths_covered_goal(ProcInfo, Goal, !ExecPaths)
;
Expr = scope(_, Goal),
% XXX We should special-case the handling of from_ground_term_construct
% scopes.
execution_paths_covered_goal(ProcInfo, Goal, !ExecPaths)
;
Expr = if_then_else(_V, Cond, Then, Else),
execution_paths_covered_goal(ProcInfo, Cond,
!.ExecPaths, ExecPathsCond),
execution_paths_covered_goal(ProcInfo, Then,
ExecPathsCond, ExecPathsCondThen),
execution_paths_covered_goal(ProcInfo, Else,
!.ExecPaths, ExecPathsElse),
!:ExecPaths = ExecPathsCondThen ++ ExecPathsElse
;
( Expr = unify(_, _, _, _, _)
; Expr = plain_call(_, _, _, _, _, _)
; Expr = conj(_, [])
; Expr = disj([])
; Expr = call_foreign_proc(_, _, _, _, _, _, _)
; Expr = generic_call(_, _, _, _)
; Expr = shorthand(_)
),
unexpected(this_file,
"collect_execution_path_in_compound_goal: encountered atomic or"
++ " unsupported goal")
).
% Extend execution paths to cover the goals in this conjunction.
%
:- pred execution_paths_covered_conj(proc_info::in, list(hlds_goal)::in,
list(execution_path)::in, list(execution_path)::out) is det.
execution_paths_covered_conj(_, [], !ExecPaths).
execution_paths_covered_conj(ProcInfo, [Conj | Conjs], !ExecPaths) :-
execution_paths_covered_goal(ProcInfo, Conj, !ExecPaths),
execution_paths_covered_conj(ProcInfo, Conjs, !ExecPaths).
% Extend execution paths to cover a disjunction.
% To do this we extend the execution paths from the beginning of the
% disjunction for each disjunct to obtain a set of execution paths
% for each disjuct. At the end of the disjunction we combine these.
%
:- pred execution_paths_covered_disj(proc_info::in, list(hlds_goal)::in,
list(execution_path)::in, list(execution_path)::out) is det.
execution_paths_covered_disj(_, [], _, []).
execution_paths_covered_disj(ProcInfo, [Disj | Disjs], !ExecPaths) :-
execution_paths_covered_goal(ProcInfo, Disj, !.ExecPaths,
ExecPathsDisj),
execution_paths_covered_disj(ProcInfo, Disjs, !.ExecPaths,
ExecPathsDisjs),
!:ExecPaths = ExecPathsDisj ++ ExecPathsDisjs.
% Extend execution paths to cover a switch.
% Switches are handled like disjunctions except that unifications
% involving the switch var sometimes need special handling.
% Unifications between the switch vars and a constant or a functor
% of arity zero are not explicitly present in the goal. This causes
% the execution paths to have fewer program points than they should.
% If this happens we need to add a program point for the removed
% unification. The goal corresponding to this introduced program
% point is the switch goal itself. This is so that we can get
% information about the switch var in the live variable analysis.
%
:- pred execution_paths_covered_cases(proc_info::in, hlds_goal::in,
list(case)::in, list(execution_path)::in, list(execution_path)::out)
is det.
execution_paths_covered_cases(_, _, [], _, []).
execution_paths_covered_cases(ProcInfo, Switch, [Case | Cases], !ExecPaths) :-
Case = case(MainConsId, OtherConsIds, CaseGoal),
expect(unify(OtherConsIds, []), this_file,
"NYI: execution_paths_covered_cases for multi-cons-id cases"),
Switch = hlds_goal(_SwitchExpr, Info),
ProgPoint = program_point_init(Info),
% Handle the unification on the switch var if it has been removed.
% We add a dummy program point for this unification.
(
MainConsId = cons(_SymName, Arity, _),
( Arity = 0 ->
append_to_each_execution_path(!.ExecPaths,
[[pair(ProgPoint, Switch)]], ExecPathsBeforeCase)
;
ExecPathsBeforeCase = !.ExecPaths
)
;
( MainConsId = int_const(_Int)
; MainConsId = float_const(_Float)
; MainConsId = char_const(_Char)
; MainConsId = string_const(_String)
),
% need to add a dummy pp
append_to_each_execution_path(!.ExecPaths,
[[pair(ProgPoint, Switch)]], ExecPathsBeforeCase)
;
( MainConsId = tuple_cons(_)
; MainConsId = closure_cons(_, _)
; MainConsId = impl_defined_const(_)
; MainConsId = type_ctor_info_const(_, _, _)
; MainConsId = base_typeclass_info_const(_, _, _, _)
; MainConsId = type_info_cell_constructor(_)
; MainConsId = typeclass_info_cell_constructor
; MainConsId = tabling_info_const(_)
; MainConsId = table_io_decl(_)
; MainConsId = deep_profiling_proc_layout(_)
),
unexpected(this_file,
"execution_paths_covered_cases: new cons_id encountered")
),
execution_paths_covered_goal(ProcInfo, CaseGoal,
ExecPathsBeforeCase, ExecPathsCase),
execution_paths_covered_cases(ProcInfo, Switch, Cases,
!.ExecPaths, ExecPathsCases),
!:ExecPaths = ExecPathsCase ++ ExecPathsCases.
% Extend each execution path in the first list with each in the
% second list, all the extended execution paths are put in the third list.
%
:- pred append_to_each_execution_path(list(execution_path)::in,
list(execution_path)::in, list(execution_path)::out) is det.
append_to_each_execution_path([], _, []).
append_to_each_execution_path([ExecPath | ExecPaths], Extensions,
ExtendedExecPaths) :-
extend_exectution_path(ExecPath, Extensions, ExtendedExecPaths0),
append_to_each_execution_path(ExecPaths, Extensions,
ExtendedExecPaths1),
ExtendedExecPaths = ExtendedExecPaths0 ++ ExtendedExecPaths1.
% extend_exectution_path(ExecPath, Extensions, ExtendedExecPaths):
%
% ExtendedExecPaths is the list created by appending each extension
% in Extensions to ExecPath.
%
:- pred extend_exectution_path(execution_path::in, list(execution_path)::in,
list(execution_path)::out) is det.
extend_exectution_path(_, [], []).
extend_exectution_path(ExecPath, [Extension | Extensions],
ExtendedExecPaths) :-
ExtendedExecPath = ExecPath ++ Extension,
extend_exectution_path(ExecPath, Extensions, ExtendedExecPaths0),
ExtendedExecPaths = [ExtendedExecPath | ExtendedExecPaths0].
%----------------------------------------------------------------------------%
:- func this_file = string.
this_file = "rbmm.execution_path.m".
%----------------------------------------------------------------------------%