Files
mercury/compiler/term_errors.m
Simon Taylor 18430aaef1 Aditi compilation.
Estimated hours taken: 1200

Aditi compilation.

compiler/options.m:
	The documentation for these is commented out because the Aditi
	system is not currently useful to the general public.
	--aditi: enable Aditi compilation.
	--dump-rl: write the intermediate RL to `<module>.rl_dump'.
	--dump-rl-bytecode: write a text version of the bytecodes
		to `<module>.rla'
	--aditi-only: don't produce a `.c' file.
	--filenames-from-stdin: accept a list of filenames to compile
		from stdin. This is used by the query shell.
	--optimize-rl, --optimize-rl-cse, --optimize-rl-invariants,
	--optimize-rl-index, --detect-rl-streams:
		Options to control RL optimization passes.
	--aditi-user:
		Default owner of any Aditi procedures,
		defaults to $USER or "guest".
	--generate-schemas:
		write schemas for base relations to `<module>'.base_schema
		and schemas for derived relations to `<module>'.derived_schema.
		This is used by the query shell.

compiler/handle_options.m:
	Handle the default for --aditi-user.

compiler/hlds_pred.m:
compiler/prog_data.m:
compiler/prog_io_pragma.m:
compiler/make_hlds.m:
	Add some Aditi pragma declarations - `aditi', `supp_magic', `context',
	`naive', `psn' (predicate semi-naive), `aditi_memo', `aditi_no_memo',
	`base_relation', `owner' and `index'.
	Separate out code to parse a predicate name and arity.

compiler/hlds_pred.m:
	Add predicates to identify Aditi procedures.
	Added markers `generate_inline' and `aditi_interface', which
	are used internally for Aditi code generation.
	Add an `owner' field to pred_infos, which is used for database
	security checks.
	Add a field to pred_infos to hold the list of indexes for a base
	relation.

compiler/make_hlds.m:
	Some pragmas must be exported if the corresponding predicates
	are exported, check this.
	Make sure stratification of Aditi procedures is checked.
	Predicates with a mode declaration but no type declaration
	are no longer assumed to be local.
	Set the `do_aditi_compilation' field of the module_info if there
	are any local Aditi procedures or base relations.
	Check that `--aditi' is set if Aditi compilation is required.

compiler/post_typecheck.m:
	Check that every Aditi predicate has an `aditi__state' argument,
	which is used to ensure sequencing of updates and that Aditi
	procedures are only called within transactions.

compiler/dnf.m:
	Changed the definition of disjunctive normal form slightly
	so that a call followed by some atomic goals not including
	any database calls is considered atomic. magic.m can handle
	this kind of goal, and it results in more efficient RL code.

compiler/hlds_module.m:
compiler/dependency_graph.m:
	Added dependency_graph__get_scc_entry_points which finds
	the procedures in an SCC which could be called from outside.
	Added a new field to the dependency_info, the
	aditi_dependency_ordering. This contains all Aditi SCCs of
	the original program, with multiple SCCs merged where
	possible to improve the effectiveness of differential evaluation
	and the low level RL optimizations.

compiler/hlds_module.m:
	Add a field to record whether there are any local Aditi procedures
	in the current module.
	Added versions of module_info_pred_proc_info and
	module_info_set_pred_proc_info which take a pred_proc_id,
	not a separate pred_id and proc_id.

compiler/polymorphism.m:
compiler/lambda.m:
	Make sure that predicates created for closures in Aditi procedures
	have the correct markers.

compiler/goal_util.m:
	Added goal_util__switch_to_disjunction,
	goal_util__case_to_disjunct (factored out from simplify.m)
	and goal_util__if_then_else_to_disjunction. These are
	require because supplementary magic sets can't handle
	if-then-elses or switches.

compiler/type_util.m:
	Added type_is_aditi_state/1.

compiler/mode_util.m:
	Added partition_args/5 which partitions a list of arguments
	into inputs and others.

compiler/inlining.m:
	Don't inline memoed procedures.
	Don't inline Aditi procedures into non-Aditi procedures.

compiler/intermod.m:
	Handle Aditi markers.
	Clean up handling of markers which should not appear in `.opt' files.

compiler/simplify.m:
	Export a slightly different interface for use by magic.m.
	Remove explicit quantifications where possible.
	Merge multiple nested quantifications.
	Don't report infinite recursion warnings for Aditi procedures.

compiler/prog_out.m:
	Generalised the code to output a module list to write any list.

compiler/code_gen.m:
compiler/arg_info.m:
	Don't process Aditi procedures.

compiler/mercury_compile.m:
	Call magic.m and rl_gen.m.
	Don't perform the low-level annotation passes on Aditi procedures.
	Remove calls to constraint.m - sometime soon a rewritten version
	will be called directly from deforestation.

compiler/passes_aux.m:
	Add predicates to process only non-Aditi procedures.

compiler/llds.m:
compiler/llds_out.m:
	Added new `code_addr' enum members, do_{det,semidet,nondet}_aditi_call,
	which are defined in extras/aditi/aditi.m.

compiler/call_gen.m:
	Handle generation of do_*_aditi_call.

compiler/llds_out.m:
	Write the RL code for the module as a constant char array
	in the `.c' file.

compiler/term_errors.m:
compiler/error_util.m:
	Move code to describe predicates into error_util.m
	Allow the caller to explicitly add line breaks.
	Added error_util:list_to_pieces to format a list of
	strings.
	Reordered some arguments for currying.

compiler/hlds_out.m:
	Don't try to print clauses if there are none.

runtime/mercury_init.h:
util/mkinit.c:
scripts/c2init.in:
	Added a function `mercury__load_aditi_rl_code()' to the generated
	`<module>_init.c' file which throws all the RL code for the program
	at the database. This should be called at connection time by
	`aditi__connect'.
	Added an option `--aditi' which controls the output
	`mercury__load_aditi_rl_code()'.

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

Mmakefile:
bindist/Mmakefile:
	Don't distribute extras/aditi yet.

New files:

compiler/magic.m:
compiler/magic_util.m:
	Supplementary magic sets transformation. Report errors
	for constructs that Aditi can't handle.

compiler/context.m:
	Supplementary context transformation.

compiler/rl_gen.m:
compiler/rl_relops.m:
	Aditi code generation.

compiler/rl_info.m:
	Code generator state.

compiler/rl.m:
	Intermediate RL representation.

compiler/rl_util:
	Predicates to collect information about RL instructions.

compiler/rl_dump.m:
	Print out the representation in rl.m.

compiler/rl_opt.m:
	Control low-level RL optimizations.

compiler/rl_block.m:
	Break a procedure into basic blocks.

compiler/rl_analyse.m:
	Generic dataflow analysis for RL procedures.

compiler/rl_liveness.m:
	Make sure all relations are initialised before used, clear
	references to relations that are no longer required.

compiler/rl_loop.m:
	Loop invariant removal.

compiler/rl_block_opt.m:
	CSE and instruction merging on basic blocks.

compiler/rl_key.m:
	Detect upper/lower bounds for which a goal could succeed.

compiler/rl_sort.m:
	Use indexing for joins and projections.
	Optimize away unnecessary sorting and indexing.

compiler/rl_stream.m:
	Detect relations which don't need to be materialised.

compiler/rl_code.m:
	RL bytecode definitions. Automatically generated from the Aditi
	header files.

compiler/rl_out.m:
compiler/rl_file.m:
	Output the RL bytecodes in binary to <module>.rlo (for use by Aditi)
	and in text to <module>.rla (for use by the RL interpreter).
	Also output the schema information if --generate-schemas is set.

compiler/rl_exprn.m:
	Generate bytecodes for join conditions.

extras/aditi/Mmakefile:
extras/aditi/aditi.m:
	Definitions of some Aditi library predicates and the
	interfacing and transaction processing code.
1998-12-06 23:49:14 +00:00

470 lines
16 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.
%-----------------------------------------------------------------------------%
%
% term_errors.m
% Main author: crs.
%
% This module prints out the various error messages that are produced by
% the various modules of termination analysis.
%
%-----------------------------------------------------------------------------%
:- module term_errors.
:- interface.
:- import_module hlds_module, hlds_pred, prog_data.
:- import_module io, bag, std_util, list, assoc_list.
:- type termination_error
---> pragma_c_code
% The analysis result depends on the change constant
% of a piece of pragma C code, (which cannot be
% obtained without analyzing the C code, which is
% something we cannot do).
% Valid in both passes.
; imported_pred
% The SCC contains some imported procedures,
% whose code is not accessible.
; can_loop_proc_called(pred_proc_id, pred_proc_id)
% can_loop_proc_called(Caller, Callee, Context)
% The call from Caller to Callee at the associated
% context is to a procedure (Callee) whose termination
% info is set to can_loop.
% Although this error does not prevent us from
% producing argument size information, it would
% prevent us from proving termination.
% We look for this error in pass 1; if we find it,
% we do not perform pass 2.
; horder_args(pred_proc_id, pred_proc_id)
% horder_args(Caller, Callee, Context)
% The call from Caller to Callee at the associated
% context has some arguments of a higher order type.
% Valid in both passes.
; horder_call
% horder_call
% There is a higher order call at the associated
% context.
% Valid in both passes.
; inf_termination_const(pred_proc_id, pred_proc_id)
% inf_termination_const(Caller, Callee, Context)
% The call from Caller to Callee at the associated
% context is to a procedure (Callee) whose arg size
% info is set to infinite.
% Valid in both passes.
; not_subset(pred_proc_id, bag(prog_var), bag(prog_var))
% not_subset(Proc, SupplierVariables, InHeadVariables)
% This error occurs when the bag of active variables
% is not a subset of the input head variables.
% Valid error only in pass 1.
; inf_call(pred_proc_id, pred_proc_id)
% inf_call(Caller, Callee)
% The call from Caller to Callee at the associated
% context has infinite weight.
% Valid error only in pass 2.
; cycle(pred_proc_id, assoc_list(pred_proc_id, prog_context))
% cycle(StartPPId, CallSites)
% In the cycle of calls starting at StartPPId and
% going through the named call sites may be an
% infinite loop.
% Valid error only in pass 2.
; no_eqns
% There are no equations in this SCC.
% This has 2 possible causes. (1) If the predicate has
% no output arguments, no equations will be created
% for them. The change constant of the predicate is
% undefined, but it will also never be used.
% (2) If the procedure is a builtin predicate, with
% an empty body, traversal cannot create any equations.
% Valid error only in pass 1.
; too_many_paths
% There were too many distinct paths to be analyzed.
% Valid in both passes (which analyze different sets
% of paths).
; solver_failed
% The solver could not find finite termination
% constants for the procedures in the SCC.
% Valid only in pass 1.
; is_builtin(pred_id)
% The termination constant of the given builtin is
% set to infinity; this happens when the type of at
% least one output argument permits a norm greater
% than zero.
; does_not_term_pragma(pred_id).
% The given procedure has a does_not_terminate pragma.
:- type term_errors__error == pair(prog_context, termination_error).
:- pred term_errors__report_term_errors(list(pred_proc_id)::in,
list(term_errors__error)::in, module_info::in,
io__state::di, io__state::uo) is det.
% An error is considered an indirect error if it is due either to a
% language feature we cannot analyze or due to an error in another part
% of the code. By default, we do not issue warnings about indirect errors,
% since in the first case, the programmer cannot do anything about it,
% and in the second case, the piece of code that the programmer *can* do
% something about is not this piece.
:- pred indirect_error(term_errors__termination_error).
:- mode indirect_error(in) is semidet.
:- implementation.
:- import_module hlds_out, prog_out, passes_aux, error_util.
:- import_module term, varset.
:- import_module mercury_to_mercury, term_util, options, globals.
:- import_module bool, int, string, map, bag, require.
indirect_error(horder_call).
indirect_error(pragma_c_code).
indirect_error(imported_pred).
indirect_error(can_loop_proc_called(_, _)).
indirect_error(horder_args(_, _)).
indirect_error(does_not_term_pragma(_)).
term_errors__report_term_errors(SCC, Errors, Module) -->
{ get_context_from_scc(SCC, Module, Context) },
( { SCC = [PPId] } ->
{ Pieces0 = [words("Termination of")] },
{ error_util__describe_one_proc_name(Module, PPId, PredName) },
{ list__append(Pieces0, [fixed(PredName)], Pieces1) },
{ Single = yes(PPId) }
;
{ Pieces0 = [words("Termination of the mutually recursive procedures")] },
{ error_util__describe_several_proc_names(Module, SCC,
ProcNamePieces) },
{ list__append(Pieces0, ProcNamePieces, Pieces1) },
{ Single = no }
),
(
{ Errors = [] },
% XXX this should never happen
% XXX but for some reason, it often does
% { error("empty list of errors") }
{ Pieces2 = [words("not proven, for unknown reason(s).")] },
{ list__append(Pieces1, Pieces2, Pieces) },
write_error_pieces(Context, 0, Pieces)
;
{ Errors = [Error] },
{ Pieces2 = [words("not proven for the following reason:")] },
{ list__append(Pieces1, Pieces2, Pieces) },
write_error_pieces(Context, 0, Pieces),
term_errors__output_error(Error, Single, no, 0, Module)
;
{ Errors = [_, _ | _] },
{ Pieces2 = [words("not proven for the following reasons:")] },
{ list__append(Pieces1, Pieces2, Pieces) },
write_error_pieces(Context, 0, Pieces),
term_errors__output_errors(Errors, Single, 1, 0, Module)
).
:- pred term_errors__report_arg_size_errors(list(pred_proc_id)::in,
list(term_errors__error)::in, module_info::in,
io__state::di, io__state::uo) is det.
term_errors__report_arg_size_errors(SCC, Errors, Module) -->
{ get_context_from_scc(SCC, Module, Context) },
( { SCC = [PPId] } ->
{ Pieces0 = [words("Termination constant of")] },
{ error_util__describe_one_proc_name(Module, PPId, ProcName) },
{ list__append(Pieces0, [fixed(ProcName)], Pieces1) },
{ Single = yes(PPId) }
;
{ Pieces0 = [words("Termination constants"),
words("of the mutually recursive procedures")] },
{ error_util__describe_several_proc_names(Module, SCC,
ProcNamePieces) },
{ list__append(Pieces0, ProcNamePieces, Pieces1) },
{ Single = no }
),
{ Piece2 = words("set to infinity for the following") },
(
{ Errors = [] },
{ error("empty list of errors") }
;
{ Errors = [Error] },
{ Piece3 = words("reason:") },
{ list__append(Pieces1, [Piece2, Piece3], Pieces) },
write_error_pieces(Context, 0, Pieces),
term_errors__output_error(Error, Single, no, 0, Module)
;
{ Errors = [_, _ | _] },
{ Piece3 = words("reasons:") },
{ list__append(Pieces1, [Piece2, Piece3], Pieces) },
write_error_pieces(Context, 0, Pieces),
term_errors__output_errors(Errors, Single, 1, 0, Module)
).
:- pred term_errors__output_errors(list(term_errors__error)::in,
maybe(pred_proc_id)::in, int::in, int::in, module_info::in,
io__state::di, io__state::uo) is det.
term_errors__output_errors([], _, _, _, _) --> [].
term_errors__output_errors([Error | Errors], Single, ErrNum0, Indent, Module)
-->
term_errors__output_error(Error, Single, yes(ErrNum0), Indent, Module),
{ ErrNum1 is ErrNum0 + 1 },
term_errors__output_errors(Errors, Single, ErrNum1, Indent, Module).
:- pred term_errors__output_error(term_errors__error::in,
maybe(pred_proc_id)::in, maybe(int)::in, int::in, module_info::in,
io__state::di, io__state::uo) is det.
term_errors__output_error(Context - Error, Single, ErrorNum, Indent, Module) -->
{ term_errors__description(Error, Single, Module, Pieces0, Reason) },
{ ErrorNum = yes(N) ->
string__int_to_string(N, Nstr),
string__append_list(["Reason ", Nstr, ":"], Preamble),
Pieces = [fixed(Preamble) | Pieces0]
;
Pieces = Pieces0
},
write_error_pieces(Context, Indent, Pieces),
( { Reason = yes(InfArgSizePPId) } ->
{ lookup_proc_arg_size_info(Module, InfArgSizePPId, ArgSize) },
( { ArgSize = yes(infinite(ArgSizeErrors)) } ->
% XXX the next line is cheating
{ ArgSizePPIdSCC = [InfArgSizePPId] },
term_errors__report_arg_size_errors(ArgSizePPIdSCC,
ArgSizeErrors, Module)
;
{ error("inf arg size procedure does not have inf arg size") }
)
;
[]
).
:- pred term_errors__description(termination_error::in,
maybe(pred_proc_id)::in, module_info::in, list(format_component)::out,
maybe(pred_proc_id)::out) is det.
term_errors__description(horder_call, _, _, Pieces, no) :-
Pieces = [words("It contains a higher order call.")].
term_errors__description(pragma_c_code, _, _, Pieces, no) :-
Pieces = [words("It depends on the properties of"),
words("foreign language code included via a"),
fixed("`pragma c_code'"),
words("declaration.")].
term_errors__description(inf_call(CallerPPId, CalleePPId),
Single, Module, Pieces, no) :-
(
Single = yes(PPId),
require(unify(PPId, CallerPPId), "caller outside this SCC"),
Piece1 = words("It")
;
Single = no,
error_util__describe_one_proc_name(Module, CallerPPId,
ProcName),
Piece1 = fixed(ProcName)
),
Piece2 = words("calls"),
error_util__describe_one_proc_name(Module, CalleePPId, CalleePiece),
Pieces3 = [words("with an unbounded increase"),
words("in the size of the input arguments.")],
Pieces = [Piece1, Piece2, fixed(CalleePiece) | Pieces3].
term_errors__description(can_loop_proc_called(CallerPPId, CalleePPId),
Single, Module, Pieces, no) :-
(
Single = yes(PPId),
require(unify(PPId, CallerPPId), "caller outside this SCC"),
Piece1 = words("It")
;
Single = no,
error_util__describe_one_proc_name(Module, CallerPPId,
ProcName),
Piece1 = fixed(ProcName)
),
Piece2 = words("calls"),
error_util__describe_one_proc_name(Module, CalleePPId, CalleePiece),
Pieces3 = [words("which could not be proven to terminate.")],
Pieces = [Piece1, Piece2, fixed(CalleePiece) | Pieces3].
term_errors__description(imported_pred, _, _, Pieces, no) :-
Pieces = [words("It contains one or more"),
words("predicates and/or functions"),
words("imported from another module.")].
term_errors__description(horder_args(CallerPPId, CalleePPId), Single, Module,
Pieces, no) :-
(
Single = yes(PPId),
require(unify(PPId, CallerPPId), "caller outside this SCC"),
Piece1 = words("It")
;
Single = no,
error_util__describe_one_proc_name(Module, CallerPPId,
ProcName),
Piece1 = fixed(ProcName)
),
Piece2 = words("calls"),
error_util__describe_one_proc_name(Module, CalleePPId, CalleePiece),
Pieces3 = [words("with one or more higher order arguments.")],
Pieces = [Piece1, Piece2, fixed(CalleePiece) | Pieces3].
term_errors__description(inf_termination_const(CallerPPId, CalleePPId),
Single, Module, Pieces, yes(CalleePPId)) :-
(
Single = yes(PPId),
require(unify(PPId, CallerPPId), "caller outside this SCC"),
Piece1 = words("It")
;
Single = no,
error_util__describe_one_proc_name(Module, CallerPPId,
ProcName),
Piece1 = fixed(ProcName)
),
Piece2 = words("calls"),
error_util__describe_one_proc_name(Module, CalleePPId, CalleePiece),
Pieces3 = [words("which has a termination constant of infinity.")],
Pieces = [Piece1, Piece2, fixed(CalleePiece) | Pieces3].
term_errors__description(not_subset(ProcPPId, OutputSuppliers, HeadVars),
Single, Module, Pieces, no) :-
(
Single = yes(PPId),
( PPId = ProcPPId ->
Pieces1 = [words("The set of"),
words("its output supplier variables")]
;
% XXX this should never happen (but it does)
% error("not_subset outside this SCC"),
error_util__describe_one_proc_name(Module, ProcPPId,
PPIdPiece),
Pieces1 = [words("The set of"),
words("output supplier variables of"),
fixed(PPIdPiece)]
)
;
Single = no,
error_util__describe_one_proc_name(Module, ProcPPId,
PPIdPiece),
Pieces1 = [words("The set of output supplier variables of"),
fixed(PPIdPiece)]
),
ProcPPId = proc(PredId, ProcId),
module_info_pred_proc_info(Module, PredId, ProcId, _, ProcInfo),
proc_info_varset(ProcInfo, Varset),
term_errors_var_bag_description(OutputSuppliers, Varset,
OutputSuppliersNames),
list__map(lambda([OS::in, FOS::out] is det, (FOS = fixed(OS))),
OutputSuppliersNames, OutputSuppliersPieces),
Pieces3 = [words("was not a subset of the head variables")],
term_errors_var_bag_description(HeadVars, Varset, HeadVarsNames),
list__map(lambda([HV::in, FHV::out] is det, (FHV = fixed(HV))),
HeadVarsNames, HeadVarsPieces),
list__condense([Pieces1, OutputSuppliersPieces, Pieces3,
HeadVarsPieces], Pieces).
term_errors__description(cycle(_StartPPId, CallSites), _, Module, Pieces, no) :-
( CallSites = [DirectCall] ->
error_util__describe_one_call_site(Module, DirectCall, Site),
Pieces = [words("At the recursive call to"),
fixed(Site),
words("the arguments are"),
words("not guaranteed to decrease in size.")]
;
Pieces1 = [words("In the recursive cycle"),
words("through the calls to")],
error_util__describe_several_call_sites(Module, CallSites,
SitePieces),
Pieces2 = [words("the arguments are"),
words("not guaranteed to decrease in size.")],
list__condense([Pieces1, SitePieces, Pieces2], Pieces)
).
term_errors__description(too_many_paths, _, _, Pieces, no) :-
Pieces = [words("There were too many execution paths"),
words("for the analysis to process.")].
term_errors__description(no_eqns, _, _, Pieces, no) :-
Pieces = [words("The analysis was unable to form any constraints"),
words("between the arguments of this group of procedures.")].
term_errors__description(solver_failed, _, _, Pieces, no) :-
Pieces = [words("The solver found the constraints produced"),
words("by the analysis to be infeasible.")].
term_errors__description(is_builtin(_PredId), _Single, _, Pieces, no) :-
% XXX require(unify(Single, yes(_)), "builtin not alone in SCC"),
Pieces = [words("It is a builtin predicate.")].
term_errors__description(does_not_term_pragma(PredId), Single, Module,
Pieces, no) :-
Pieces1 = [words("There was a `does_not_terminate' pragma defined on")],
(
Single = yes(PPId),
PPId = proc(SCCPredId, _),
require(unify(PredId, SCCPredId), "does not terminate pragma outside this SCC"),
Piece2 = words("It")
;
Single = no,
error_util__describe_one_pred_name(Module, PredId,
Piece2Nodot),
string__append(Piece2Nodot, ".", Piece2Str),
Piece2 = fixed(Piece2Str)
),
list__append(Pieces1, [Piece2], Pieces).
%----------------------------------------------------------------------------%
:- pred term_errors_var_bag_description(bag(prog_var)::in, prog_varset::in,
list(string)::out) is det.
term_errors_var_bag_description(HeadVars, Varset, Pieces) :-
bag__to_assoc_list(HeadVars, HeadVarCountList),
term_errors_var_bag_description_2(HeadVarCountList, Varset, yes,
Pieces).
:- pred term_errors_var_bag_description_2(assoc_list(prog_var, int)::in,
prog_varset::in, bool::in, list(string)::out) is det.
term_errors_var_bag_description_2([], _, _, ["{}"]).
term_errors_var_bag_description_2([Var - Count | VarCounts], Varset, First,
[Piece | Pieces]) :-
varset__lookup_name(Varset, Var, VarName),
( Count > 1 ->
string__append(VarName, "*", VarCountPiece0),
string__int_to_string(Count, CountStr),
string__append(VarCountPiece0, CountStr, VarCountPiece)
;
VarCountPiece = VarName
),
( First = yes ->
string__append("{", VarCountPiece, Piece0)
;
Piece0 = VarCountPiece
),
( VarCounts = [] ->
string__append(Piece0, "}.", Piece),
Pieces = []
;
Piece = Piece0,
term_errors_var_bag_description_2(VarCounts, Varset, First,
Pieces)
).
%----------------------------------------------------------------------------%
%----------------------------------------------------------------------------%