mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-19 11:23:46 +00:00
Right now, most parts of the compiler write to the "current output stream".
This was a pragmatic choice at the time, but has not aged well. The problem
is that the answer to the question "where is the current output stream going?"
is not obvious in *all* places in the compiler (although it is obvious in
most). When using such implicit streams, finding where the output is going
to in a given predicate requires inspecting not just the ancestors of that
predicate, but also all their older siblings (since any of them could have
changed the current stream), *including* their entire call trees. This is
usually an infeasible task. By constrast, if we explicitly pass streams
to all output operations, we need only follow the places where the variable
representing that stream is bound, which the mode system makes easy.
This diff switches large parts of the compiler over to doing output only
to explicitly passed streams, never to the implicit "current output stream".
The parts it switches over are the parts that rely to a significant degree
on the innermost change, which is to the "output" typeclass in
parse_tree_out_info.m. This is the part that has to be switched over to
explicit streams first, because (a) many modules such as mercury_to_mercury.m
rely on the output typeclass, and (b) most other modules that do output
call predicates in these modules. Starting anywhere else would be like
building a skyscraper starting at the top.
This typeclass, output(U), has two instances: output(io), and output(string),
so you could output either to the current output stream, or to a string.
To allow the specification of the destination stream in the first case,
this diff changes the typeclass to output(S, U) with a functional dependency
from U to S, with the two instances being output(io.text_output_stream, io)
and output(unit, string). (The unit arg is ignored in the second case.)
There is a complication with the output typeclass method, add_list, that
outputs a list of items. The complication is that each item is output
by a predicate supplied by the caller, but the separator between the items
(usually a comma) is output by add_list itself. We don't want to give
callers of this method the opportunity to screw up by specifying (possibly
implicitly) two different output streams for these two purposes, so we want
(a) the caller to tell add_list where to put the separators, and then
(b) for add_list, not its caller, tell the user-supplied predicate what
stream to write to. This works only if the stream argument is just before
the di,uo pair of I/O state arguments, which differs from our usual practice
of passing the stream at or near the left edge of the argument list,
not near the right. The result of this complication is that two categories
of predicates that are and are not used to print items in a list differ
in where they put the stream in their argument lists. This makes it easy
to pass the stream in the wrong argument position if you call a predicate
without looking up its signature, and may require *changing* the argument
order when a predicate is used to print an item in a list for the first time.
A complete switch over to always passing the stream just before !IO
would fix this inconsistency, but is far to big a change to make all at once.
compiler/parse_tree_out_info.m:
Make the changes described above.
Add write_out_list, which is a variant of io.write_list specifically
designed to address the "complication" described above. It also has
the arguments in an order that is better suited for higher-order use.
Make the same change to argument order in the class method add_list
as well.
Almost all of the following changes consist of passing an extra stream
argument to output predicates. In some places, where I thought this would
aid readability, I replaced sequences of calls to output predicates
with a single io.format.
compiler/prog_out.m:
This module had many predicates that wrote things to the current output
stream. This diff adds versions of these predicates that take an
explicit stream argument.
If the originals are still needed after the changes to the other modules,
keep them, but add "_to_cur_stream" to the end of their names.
Otherwise, delete them. (Many of the changes below replace
write_xyz(..., !IO) with io.write_string(Stream, xyz_to_string(...), !IO),
especially when write_xyz did nothing except call xyz_to_string
and wrote out the result.)
compiler/c_util.m:
Add either an explicit stream argument to the argument list, or a
"_current_stream" suffix to the name, of every predicate defined
in this module that does output.
Add a new predicate to print out the block comment containing
input for mkinit. This factors out common code in the LLDS and MLDS
backends.
compiler/name_mangle.m:
Delete all predicates that used to write to the current output stream,
after replacing them if necessary with functions that return a string,
which the caller can print to wherever it wants. (The "if necessary"
part is there because some of the "replacement" functions already
existed.)
When converting a proc_label to a string, *always* require the caller
to say whether the label prefix should be added to the string,
instead of silently assuming "yes, add it", as calls to one of the old,
now deleted predicates had it.
compiler/file_util.m:
Add output_to_file_stream, a version of output_to_file which
simply passes the output file stream it opens to the predicate
that is intended to define the contents of the newly created or
updated file. The existing output_to_file, which instead sets
and resets the current output stream around the equivalent
predicate call, is still needed e.g. by the MLDS backend,
but hopefully for not too long.
compiler/mercury_to_mercury.m:
compiler/parse_tree_out.m:
compiler/parse_tree_out_clause.m:
compiler/parse_tree_out_inst.m:
compiler/parse_tree_out_pragma.m:
compiler/parse_tree_out_pred_decl.m:
compiler/parse_tree_out_term.m:
compiler/parse_tree_out_type_repn.m:
Change the code writing out parse trees to explicitly pass a stream
to every predicate that does output.
In some places, this allows us to avoid changing the identity
of the current output stream.
compiler/hlds_out.m:
compiler/hlds_out_goal.m:
compiler/hlds_out_mode.m:
compiler/hlds_out_module.m:
compiler/hlds_out_pred.m:
compiler/hlds_out_util.m:
compiler/intermod.m:
Change the code writing out HLDS code to explicitly pass a stream
to every predicate that does output. (The changes to these modules
belong in this diff because these modules call many of the output
predicates in the parse tree package.)
In hlds_out_util.m, delete some write_to_xyz(...) predicates that wrote
the result of xyz_to_string(...) to the current output stream.
Replace calls to the deleted predicates with calls to io.write_string
with the string being written being computed by xyz_to_string.
Add a predicate to hlds_out_util.m that outputs a comment containing
the current context, if it is valid. This factors out code that used
to be common to several of the other modules.
In a few places in hlds_out_module.m, the new code generates a
slighly different set of blank lines, but this should not be a problem.
compiler/layout_out.m:
compiler/llds_out_code_addr.m:
compiler/llds_out_data.m:
compiler/llds_out_file.m:
compiler/llds_out_global.m:
compiler/llds_out_instr.m:
compiler/llds_out_util.m:
compiler/opt_debug.m:
compiler/rtti_out.m:
Change the code writing out the LLDS to explicitly pass a stream
to every predicate that does output. (The changes to these modules
belong in this diff because layout_out.m and rtti_out.m call
many of the output predicates in the parse tree package,
and through them, the rest of the LLDS backend is affected as well.)
compiler/make.module_dep_file.m:
compiler/mercury_compile_main.m:
compiler/mercury_compile_middle_passes.m:
Replace code that sets and resets the current output stream
with code that simply passes an explicit output stream to a
predicate that now *takes* an explicit stream as an argument.
compiler/accumulator.m:
compiler/add_clause.m:
compiler/code_gen.m:
compiler/code_loc_dep.m:
compiler/cse_detection.m:
compiler/delay_partial_inst.m:
compiler/dep_par_conj.m:
compiler/det_analysis.m:
compiler/error_msg_inst.m:
compiler/export.m:
compiler/format_call.m:
compiler/goal_expr_to_goal.m:
compiler/ite_gen.m:
compiler/lco.m:
compiler/liveness.m:
compiler/lp_rational.m:
compiler/mercury_compile_front_end.m:
compiler/mercury_compile_llds_back_end.m:
compiler/mlds_to_c_file.m:
compiler/mlds_to_c_global.m:
compiler/mode_debug.m:
compiler/mode_errors.m:
compiler/modes.m:
compiler/optimize.m:
compiler/passes_aux.m:
compiler/pd_debug.m:
compiler/pragma_c_gen.m:
compiler/proc_gen.m:
compiler/prog_ctgc.m:
compiler/push_goals_together.m:
compiler/rat.m:
compiler/recompilation.m:
compiler/recompilation.usage.m:
compiler/recompilation.version.m:
compiler/rtti.m:
compiler/saved_vars.m:
compiler/simplify_goal_conj.m:
compiler/stack_opt.m:
compiler/structure_reuse.analysis.m:
compiler/structure_reuse.domain.m:
compiler/structure_reuse.indirect.m:
compiler/structure_sharing.analysis.m:
compiler/superhomogeneous.m:
compiler/term_constr_build.m:
compiler/term_constr_data.m:
compiler/term_constr_fixpoint.m:
compiler/term_constr_pass2.m:
compiler/term_constr_util.m:
compiler/tupling.m:
compiler/type_assign.m:
compiler/unneeded_code.m:
compiler/write_deps_file.m:
Conform to the changes above, mostly by passing streams explicitly.
compiler/hlds_dependency_graph.m:
Conform to the changes above, mostly by passing streams explicitly.
Move a predicate's definition next it only use.
compiler/Mercury.options:
Specify --warn-implicit-stream-calls for all the modules in which
this diff has replaced all implicit streams with explicit streams.
(Unfortunately, debugging this diff has shown that --warn-implicit-
stream-calls detects only *some*, and not *all*, uses of implicit
streams.)
library/term_io.m:
Fix documentation.
231 lines
5.7 KiB
Mathematica
231 lines
5.7 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 1997-1998, 2003, 2005-2006, 2010-2011 The University of Melbourne.
|
|
% This file may only be copied under the terms of the GNU Library General
|
|
% Public License - see the file COPYING.LIB in the Mercury distribution.
|
|
%-----------------------------------------------------------------------------%
|
|
%
|
|
% File: rat.m.
|
|
% Authors: vjteag, juliensf.
|
|
%
|
|
% Implements a rational number type using fixed precision integers.
|
|
% The functionality here is limited to that which is used in the
|
|
% lp_rational module.
|
|
%
|
|
% NOTE: if you actually want a general purpose rational number type,
|
|
% then use the rational module in the standard library. This module
|
|
% is pretty heavily geared towards a few specific tasks that are part of
|
|
% the termination analysis.
|
|
%
|
|
% TODO:
|
|
% - overflow checking would be nice
|
|
%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module libs.rat.
|
|
:- interface.
|
|
|
|
:- import_module io.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- type rat.
|
|
|
|
:- func one = rat.
|
|
:- func zero = rat.
|
|
|
|
:- pred '<'(rat::in, rat::in) is semidet.
|
|
:- pred '>'(rat::in, rat::in) is semidet.
|
|
:- pred '=<'(rat::in, rat::in) is semidet.
|
|
:- pred '>='(rat::in, rat::in) is semidet.
|
|
|
|
:- func rat(int) = rat.
|
|
:- func rat(int, int) = rat.
|
|
|
|
:- func '+'(rat) = rat.
|
|
:- func '-'(rat) = rat.
|
|
:- func rat + rat = rat.
|
|
:- func rat - rat = rat.
|
|
:- func rat * rat = rat.
|
|
:- func rat / rat = rat.
|
|
|
|
:- func numer(rat) = int.
|
|
:- func denom(rat) = int.
|
|
|
|
:- func abs(rat) = rat.
|
|
|
|
% Convert a rational to a string of the form: "(<Num>/<Denom>)".
|
|
%
|
|
:- func to_string(rat) = string.
|
|
|
|
% Write a rat in the form: r(<Numerator>, <Denominator>).
|
|
%
|
|
:- pred write_rat(rat::in, io::di, io::uo) is det.
|
|
:- pred write_rat(io.text_output_stream::in, rat::in, io::di, io::uo) is det.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module int.
|
|
:- import_module list.
|
|
:- import_module require.
|
|
:- import_module string.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% The normal form of a rat number has the following properties:
|
|
%
|
|
% - numerator and denominator have no common factors.
|
|
% - denominator is positive.
|
|
% - denominator is not zero.
|
|
% - if numerator is zero, then denominator is one.
|
|
%
|
|
% These invariants must be preserved by any rat number
|
|
% constructed using this module since the equality predicate
|
|
% on rats is simply Mercury's default unification
|
|
% predicate =/2. If the invariants were not maintained,
|
|
% we would have pathologies like r(-1,2) \= r(1,-2).
|
|
%
|
|
% The rat_norm/2 function generates rationals in this normal form.
|
|
%
|
|
:- type rat
|
|
---> r(int, int).
|
|
|
|
one = r(1, 1).
|
|
|
|
zero = r(0, 1).
|
|
|
|
'<'(X, Y) :- cmp(X, Y) = (<).
|
|
|
|
'>'(X, Y) :- cmp(X, Y) = (>).
|
|
|
|
'=<'(X, Y) :- cmp(X, Y) \= (>).
|
|
|
|
'>='(X, Y) :- cmp(X, Y) \= (<).
|
|
|
|
rat(Int) = r(Int, 1).
|
|
|
|
rat(Num, Den) = rat_norm(Num, Den).
|
|
|
|
'+'(Rat) = Rat.
|
|
|
|
'-'(r(Num, Den)) = r(-Num, Den).
|
|
|
|
r(An, Ad) + r(Bn, Bd) = rat_norm(Numer, M) :-
|
|
M = lcm(Ad, Bd),
|
|
CA = M // Ad,
|
|
CB = M // Bd,
|
|
Numer = An * CA + Bn * CB.
|
|
|
|
X - Y = X + (-Y).
|
|
|
|
% XXX: need we call rat_norm here?
|
|
r(An, Ad) * r(Bn, Bd) = rat_norm(Numer, Denom) :-
|
|
G1 = gcd(An, Bd),
|
|
G2 = gcd(Ad, Bn),
|
|
Numer = (An // G1) * (Bn // G2),
|
|
Denom = (Ad // G2) * (Bd // G1).
|
|
|
|
X / Y = X * rat.reciprocal(Y).
|
|
|
|
:- func rat.reciprocal(rat) = rat.
|
|
|
|
reciprocal(r(Num, Den)) =
|
|
( if Num = 0 then
|
|
unexpected($pred, "division by zero")
|
|
else
|
|
r(signum(Num) * Den, int.abs(Num))
|
|
).
|
|
|
|
numer(r(Num, _)) = Num.
|
|
|
|
denom(r(_, Den)) = Den.
|
|
|
|
abs(r(Num, Den)) = r(int.abs(Num), Den).
|
|
|
|
:- func rat_norm(int, int) = rat.
|
|
|
|
rat_norm(Num, Den) = Rat :-
|
|
( if Den = 0 then
|
|
unexpected($pred, "division by zero")
|
|
else if Num = 0 then
|
|
Rat = r(0, 1)
|
|
else
|
|
G = gcd(Num, Den),
|
|
Num2 = Num * signum(Den),
|
|
Den2 = int.abs(Den),
|
|
Rat = r(Num2 // G, Den2 // G)
|
|
).
|
|
|
|
:- func gcd(int, int) = int.
|
|
|
|
gcd(A, B) = gcd_2(int.abs(A), int.abs(B)).
|
|
|
|
:- func gcd_2(int, int) = int.
|
|
|
|
gcd_2(A, B) = ( if B = 0 then A else gcd_2(B, A rem B) ).
|
|
|
|
:- func lcm(int, int) = int.
|
|
|
|
lcm(A, B) =
|
|
( if A = 0 then
|
|
0
|
|
else if B = 0 then
|
|
0
|
|
else
|
|
int.abs((A // gcd(A, B)) * B)
|
|
).
|
|
|
|
:- func signum(int) = int.
|
|
|
|
signum(N) = ( if N = 0 then 0 else if N < 0 then -1 else 1 ).
|
|
|
|
% Builtin comparison does not give a natural ordering on rats.
|
|
%
|
|
:- func cmp(rat, rat) = comparison_result.
|
|
|
|
cmp(X, Y) = Cmp :-
|
|
Diff = X - Y,
|
|
( if is_zero(Diff) then
|
|
Cmp = (=)
|
|
else if is_negative(Diff) then
|
|
Cmp = (<)
|
|
else
|
|
Cmp = (>)
|
|
).
|
|
|
|
:- pred is_zero(rat::in) is semidet.
|
|
|
|
is_zero(r(0, _)).
|
|
|
|
:- pred is_negative(rat::in) is semidet.
|
|
|
|
is_negative(r(Num, _)) :- Num < 0.
|
|
|
|
to_string(r(Num, Denom)) =
|
|
( if Num = 0 then
|
|
"0"
|
|
else
|
|
"(" ++ string.int_to_string(Num) ++
|
|
( if Denom = 1 then
|
|
""
|
|
else
|
|
"/" ++ string.int_to_string(Denom)
|
|
)
|
|
++ ")"
|
|
).
|
|
|
|
write_rat(Rat, !IO) :-
|
|
io.output_stream(Stream, !IO),
|
|
write_rat(Stream, Rat, !IO).
|
|
|
|
write_rat(Stream, r(Numerator, Denominator), !IO) :-
|
|
io.format(Stream, "r(%d, %d)", [i(Numerator), i(Denominator)], !IO).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
:- end_module libs.rat.
|
|
%-----------------------------------------------------------------------------%
|