[m-dev.] for review: user-friendly representation of streams

Zoltan Somogyi zs at cs.mu.OZ.AU
Wed Sep 25 18:22:14 AEST 2002


Three caveats on this diff.

First, I could in theory tease apart the three different changes below,
but I don't think that would be very productive.

Second, the new version of io.m generates a few warnings from gcc. These arise
in the calls to the MR_MAYBE_UNBOX_FOREIGN_TYPE macro in the C functions
generated by the Mercury compiler when Mercury predicates are exported to C.
Fergus should fix either this macro or the code that generates calls to it.

Third, I changed the implementation of io__streams for C backends, but not
for the .NET or Java backends. Tyson or Fergus should make the analogous
changes to those backends. I can't, since I can't test such changes.
I can either commit this change after review and let one of them change
the MC++ code later (maybe converting it to C# in the process), or I can
given them the diff to complete.

Zoltan.

----------------------------

This change bundles three modifications, one large, one medium and one small.
(I needed them all while working on fixing the deep profiler.)

The large change is the change in the representation of io__streams. The
change allows the debugger to print meaningful information about streams,
not just <<c_pointer>>. It also allows the library and the debugger to handle
streams much more safely internally.

The medium change is that the predicates to report stats now return the
statistics as strings, instead of printing them out. Amongst other things,
this allows the stats to be printed to streams other than the current one.

The small change is that the printing of the query message about EOF in mdb
can now be switched off.

runtime/mercury_library_types.h:
	Add an MR_ prefix to the name of the MercuryFile type. Redefine both
	versions of the type (with and without the new MercuryFile configure
	option) to contain a name and a unique identifier. Update the macros
	operating on both versions of the type accordingly. Define the type
	MR_MercuryFilePtr to point to these structures.

	Add prefixes to structure field names and to macros.

library/io.m:
	Change the definition of the io__stream type to be a foreign type,
	to wit, MR_MercuryFilePtr. This improves safety by allowing us to
	avoid a *large* number of casts.

	Delete the map from I/O streams to names, since it is not needed
	anymore.

	Add a new type, mercury_file_ptr, with the same definition as
	io__stream, but this one exported to the debugger. (The export is
	in the hidden, undocumented part of the interface.) Add predicates
	for use by the debugger to convert io__streams to mercury_file_ptrs
	and vice versa.

	Add predicates to report statistics to user-supplied streams.

	Standardize the formating of foreign_proc pragmas.

browser/browse.m:
browser/help.m:
browser/debugger_interface.m:
browser/declarative_debugger.m:
browser/declarative_execution.m:
browser/interactive_query.m:
browser/util.m:
	Instead of exporting to the trace directory predicates that expect
	stream arguments, define and export predicates that expect
	mercury_file_ptrs. Convert these to streams as necessary.

	Make Erwan's code conform to our style conventions.

library/benchmarking.m:
library/table_builtin.m:
runtime/mercury_tabling.[ch]:
	Change the predicates that report stats to return them as strings.

runtime/mercury_file.[ch]:
	Change the code that creates MR_MercuryFile structures accordingly.

runtime/mercury_init.h:
runtime/mercury_wrapper.[ch]:
	Update the prototypes of C functions exported by io.m to reflect that
	the C type of io__streams has changed.

runtime/mercury_layout_util.c:
	Conform to the changed prototypes.

runtime/mercury_trace_base.[ch]:
	Add a global for controlling whether we get a message from mdb about
	EOF.

runtime/mercury_builtin_types.[ch]:
	Declare and define the type_ctor_infos of the types used by the LLDS
	backend in all the grades used by the LLDS backend. They may not be
	needed in all such grades, but keeping track of which ones are defined
	under what conditions can be complex, and the payoff (a few dozen bytes
	less memory consumption) isn't worth it.
 
trace/mercury_trace_util.[ch]:
	Delete a now unnecessary utility function.

trace/mercury_trace_browse.c:
trace/mercury_trace_declarative.[ch]:
trace/mercury_trace_external.[ch]:
trace/mercury_trace_internal.[ch]:
	Call the mercury_file_ptr versions of the relevant predicates in the
	browser directory. This avoids many casts.

	Convert FILE *s to MR_MercuryFilePtrs just once, not once for each
	call that has a mercury_file_ptr argument.
 
trace/mercury_trace_vars.c:
	Convert arguments of whose types represents streams to the more
	user-friendly representation when collecting their values. This helps
	users of all flavours of the debugger, external, procedural and
	declarative.

	Convert the lists of references to type_ctor_infos to use higher level
	names, and remove the now unnecessary #ifdefs.

tests/debugger/stream.{m,inp,exp}:
tests/debugger/DATA.in:
tests/debugger/declarative/stream.{m,inp,exp}:
tests/debugger/declarative/DATA.in:
	Two new test cases to test the printing of I/O streams. Their code
	is the same, but the input scripts differ, checking the handling
	of streams by the procedural and declarative debuggers respectively.

tests/debugger/Mmakefile:
tests/debugger/Mmakefile:
	Enable the new test cases.

cvs diff: Diffing .
cvs diff: Diffing bench
cvs diff: Diffing bench/progs
cvs diff: Diffing bench/progs/compress
cvs diff: Diffing bench/progs/icfp2000
cvs diff: Diffing bench/progs/icfp2001
cvs diff: Diffing bench/progs/nuc
cvs diff: Diffing bench/progs/ray
cvs diff: Diffing bench/progs/tree234
cvs diff: Diffing bindist
cvs diff: Diffing boehm_gc
cvs diff: Diffing boehm_gc/Mac_files
cvs diff: Diffing boehm_gc/cord
cvs diff: Diffing boehm_gc/cord/private
cvs diff: Diffing boehm_gc/doc
cvs diff: Diffing boehm_gc/include
cvs diff: Diffing boehm_gc/include/private
cvs diff: Diffing boehm_gc/tests
cvs diff: Diffing browser
Index: browser/browse.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/browse.m,v
retrieving revision 1.29
diff -u -b -r1.29 browse.m
--- browser/browse.m	3 Jul 2002 08:25:02 -0000	1.29
+++ browser/browse.m	18 Sep 2002 11:50:02 -0000
@@ -127,24 +127,122 @@
 % they are used in trace/mercury_trace_browser.c.
 %
 
-:- pragma export(browse__browse(in, in, in, out, in, out, di, uo),
-	"ML_BROWSE_browse").
-:- pragma export(browse__browse_format(in, in, in, in, in, out, di, uo),
-	"ML_BROWSE_browse_format").
-:- pragma export(browse__browse_synthetic(in, in, in, in, in, out,
-	in, out, di, uo), "ML_BROWSE_browse_synthetic").
-:- pragma export(browse__browse_format_synthetic(in, in, in, in, in, in,
-	in, out, di, uo), "ML_BROWSE_browse_format_synthetic").
-:- pragma export(browse__browse_external(in, in, in, in, out, di, uo),
-	"ML_BROWSE_browse_external").
-:- pragma export(browse__print(in, in, in, in, di, uo),
-	"ML_BROWSE_print").
-:- pragma export(browse__print_format(in, in, in, in, in, di, uo),
-	"ML_BROWSE_print_format").
-:- pragma export(browse__print_synthetic(in, in, in, in, in, in, di, uo),
-	"ML_BROWSE_print_synthetic").
-:- pragma export(browse__print_format_synthetic(in, in, in, in, in, in, in,
-	di, uo), "ML_BROWSE_print_format_synthetic").
+:- pragma export(browse__browse_mfp(in, in, in, out, in, out, di, uo),
+	"ML_BROWSE_browse_mfp").
+:- pragma export(browse__browse_format_mfp(in, in, in, in, in, out, di, uo),
+	"ML_BROWSE_browse_format_mfp").
+:- pragma export(browse__browse_synthetic_mfp(in, in, in, in, in, out,
+	in, out, di, uo), "ML_BROWSE_browse_synthetic_mfp").
+:- pragma export(browse__browse_format_synthetic_mfp(in, in, in, in, in, in,
+	in, out, di, uo), "ML_BROWSE_browse_format_synthetic_mfp").
+:- pragma export(browse__browse_external_mfp(in, in, in, in, out, di, uo),
+	"ML_BROWSE_browse_external_mfp").
+:- pragma export(browse__print_mfp(in, in, in, in, di, uo),
+	"ML_BROWSE_print_mfp").
+:- pragma export(browse__print_format_mfp(in, in, in, in, in, di, uo),
+	"ML_BROWSE_print_format_mfp").
+:- pragma export(browse__print_synthetic_mfp(in, in, in, in, in, in, di, uo),
+	"ML_BROWSE_print_synthetic_mfp").
+:- pragma export(browse__print_format_synthetic_mfp(in, in, in, in, in, in, in,
+	di, uo), "ML_BROWSE_print_format_synthetic_mfp").
+
+:- pred browse__browse_mfp(T::in, mercury_file_ptr::in, mercury_file_ptr::in,
+	maybe(list(dir))::out,
+	browser_persistent_state::in, browser_persistent_state::out,
+	io__state::di, io__state::uo) is cc_multi.
+
+browse__browse_mfp(Object, InputMfp, OutputMfp, MaybeMark, State0, State) -->
+	{ io__input_stream_from_mfp(InputStream, InputMfp) },
+	{ io__output_stream_from_mfp(OutputStream, OutputMfp) },
+	browse__browse(Object, InputStream, OutputStream, MaybeMark,
+		State0, State).
+
+:- pred browse__browse_format_mfp(T::in, mercury_file_ptr::in,
+	mercury_file_ptr::in, portray_format::in,
+	browser_persistent_state::in, browser_persistent_state::out,
+	io__state::di, io__state::uo) is cc_multi.
+
+browse__browse_format_mfp(Object, InputMfp, OutputMfp, Format, State0, State)
+		-->
+	{ io__input_stream_from_mfp(InputStream, InputMfp) },
+	{ io__output_stream_from_mfp(OutputStream, OutputMfp) },
+	browse__browse_format(Object, InputStream, OutputStream, Format,
+		State0, State).
+
+:- pred browse__browse_synthetic_mfp(string::in, list(univ)::in, bool::in,
+	mercury_file_ptr::in, mercury_file_ptr::in, maybe(list(dir))::out,
+	browser_persistent_state::in, browser_persistent_state::out,
+	io__state::di, io__state::uo) is cc_multi.
+
+browse__browse_synthetic_mfp(FunctorString, Args, IsFunc,
+		InputMfp, OutputMfp, MaybeMark, State0, State) -->
+	{ io__input_stream_from_mfp(InputStream, InputMfp) },
+	{ io__output_stream_from_mfp(OutputStream, OutputMfp) },
+	browse__browse_synthetic(FunctorString, Args, IsFunc,
+		InputStream, OutputStream, MaybeMark, State0, State).
+
+:- pred browse__browse_format_synthetic_mfp(string::in, list(univ)::in,
+	bool::in, mercury_file_ptr::in, mercury_file_ptr::in,
+	portray_format::in,
+	browser_persistent_state::in, browser_persistent_state::out,
+	io__state::di, io__state::uo) is cc_multi.
+
+browse__browse_format_synthetic_mfp(FunctorString, Args, IsFunc,
+		InputMfp, OutputMfp, Format, State0, State) -->
+	{ io__input_stream_from_mfp(InputStream, InputMfp) },
+	{ io__output_stream_from_mfp(OutputStream, OutputMfp) },
+	browse__browse_format_synthetic(FunctorString, Args, IsFunc,
+		InputStream, OutputStream, Format, State0, State).
+
+:- pred browse__browse_external_mfp(T::in,
+	mercury_file_ptr::in, mercury_file_ptr::in,
+	browser_persistent_state::in, browser_persistent_state::out,
+	io__state::di, io__state::uo) is cc_multi.
+
+browse__browse_external_mfp(Object, InputMfp, OutputMfp, State0, State) -->
+	{ io__input_stream_from_mfp(InputStream, InputMfp) },
+	{ io__output_stream_from_mfp(OutputStream, OutputMfp) },
+	browse__browse_external(Object, InputStream, OutputStream,
+		State0, State).
+
+:- pred browse__print_mfp(T::in, mercury_file_ptr::in, browse_caller_type::in,
+	browser_persistent_state::in, io__state::di, io__state::uo)
+	is cc_multi.
+
+browse__print_mfp(Term, OutputMfp, Caller, State) -->
+	{ io__output_stream_from_mfp(OutputStream, OutputMfp) },
+	browse__print(Term, OutputStream, Caller, State).
+
+:- pred browse__print_synthetic_mfp(string::in, list(univ)::in, bool::in,
+	mercury_file_ptr::in, browse_caller_type::in,
+	browser_persistent_state::in, io__state::di, io__state::uo)
+	is cc_multi.
+
+browse__print_synthetic_mfp(FunctorString, Args, IsFunc, OutputMfp, Caller,
+		State) -->
+	{ io__output_stream_from_mfp(OutputStream, OutputMfp) },
+	browse__print_synthetic(FunctorString, Args, IsFunc,
+		OutputStream, Caller, State).
+
+:- pred browse__print_format_mfp(T::in, mercury_file_ptr::in,
+	browse_caller_type::in, portray_format::in,
+	browser_persistent_state::in, io__state::di, io__state::uo)
+	is cc_multi.
+
+browse__print_format_mfp(Term, OutputMfp, Caller, Format, State) -->
+	{ io__output_stream_from_mfp(OutputStream, OutputMfp) },
+	browse__print_format(Term, OutputStream, Caller, Format, State).
+
+:- pred browse__print_format_synthetic_mfp(string::in, list(univ)::in, bool::in,
+	mercury_file_ptr::in, browse_caller_type::in, portray_format::in,
+	browser_persistent_state::in, io__state::di, io__state::uo)
+	is cc_multi.
+
+browse__print_format_synthetic_mfp(FunctorString, Args, IsFunc, OutputMfp,
+		Caller, Format, State) -->
+	{ io__output_stream_from_mfp(OutputStream, OutputMfp) },
+	browse__print_format_synthetic(FunctorString, Args, IsFunc,
+		OutputStream, Caller, Format, State).
 
 %---------------------------------------------------------------------------%
 % If the term browser is called from the internal debugger, input is
Index: browser/debugger_interface.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/debugger_interface.m,v
retrieving revision 1.19
diff -u -b -r1.19 debugger_interface.m
--- browser/debugger_interface.m	22 Jun 2001 15:20:22 -0000	1.19
+++ browser/debugger_interface.m	18 Sep 2002 12:56:18 -0000
@@ -1,5 +1,5 @@
 %-----------------------------------------------------------------------------%
-% Copyright (C) 1998-2001 The University of Melbourne.
+% Copyright (C) 1998-2002 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.
 %-----------------------------------------------------------------------------%
@@ -44,7 +44,6 @@
 	% encoded as specified in ../runtime/mercury_stack_layout.h
 	% and ../compiler/stack_layout.m.
 
-
 % Depending whether the Opium side is requesting for a user defined procedure
 % or a compiler generated one, the event has not exactly the same structure.
 % The differences between the two types of event are gathered in a forward_move
@@ -159,7 +158,6 @@
 :- type call_number == int.
 :- type depth_number == int.
 
-
 % `match' is called "get status" in the Opium documentation.
 % This type defines a unary predicate which determines whether
 % or not a particular value will be selected.
@@ -171,7 +169,6 @@
 	;	interval(T,T)	% interval(Low, High): Low =< X, X =< High
 	.
 
-
 % The debugger_response type is used for response sent
 % to the debugger process from the Mercury program being debugged.
 :- type debugger_response
@@ -268,7 +265,6 @@
 	;	collect_arg_off_ok
 	.
 
-
 %-----------------------------------------------------------------------------%
 %	send to the debugger (e.g. Opium) the wanted features.
 
@@ -283,18 +279,17 @@
 	trace_port_type, pred_or_func, /* declarated module name */ string,
 	/* definition module name */ string, /* pred name */ string, arity, 
 	/* mode num */ int, determinism, goal_path_string, line_number,
-	io__output_stream, io__state, io__state).
+	mercury_file_ptr, io__state, io__state).
 :- mode output_current_slots_user(in, in, in, in, in, in, in, in, in, in, 
 	in, in, in, in, di, uo) is det.
 
-
 output_current_slots_user(EventNumber, CallNumber, DepthNumber, Port, 
-	PredOrFunc, DeclModuleName, DefModuleName, PredName, Arity, ModeNum, 
-	Determinism, Path, LineNo, OutputStream) -->
-	
+		PredOrFunc, DeclModuleName, DefModuleName, PredName, Arity,
+		ModeNum, Determinism, Path, LineNo, OutputMfp) -->
 	{ CurrentTraceInfo = current_slots_user(EventNumber, CallNumber, 
 		DepthNumber, Port, PredOrFunc, DeclModuleName, DefModuleName, 
 		PredName, Arity, ModeNum, Determinism, Path, LineNo) },
+	{ io__output_stream_from_mfp(OutputStream, OutputMfp) },
 	io__write(OutputStream, CurrentTraceInfo),
 	io__print(OutputStream, ".\n"),
 	io__flush_output(OutputStream).
@@ -310,18 +305,17 @@
 	trace_port_type, /* name type */ string, /* module type */ string,
 	/* definition module */ string, /* pred name */ string, arity, 
 	/* mode num */ int, determinism, goal_path_string, line_number,
-	io__output_stream, io__state, io__state).
+	mercury_file_ptr, io__state, io__state).
 :- mode output_current_slots_comp(in, in, in, in, in, in, in, in, in, in, 
 	in, in, in, in, di, uo) is det.
 
-
 output_current_slots_comp(EventNumber, CallNumber, DepthNumber, Port, 
 	NameType, ModuleType, DefModuleName, PredName, Arity, 
-	ModeNum, Determinism, Path, LineNo, OutputStream) -->
-	
+		ModeNum, Determinism, Path, LineNo, OutputMfp) -->
 	{ CurrentTraceInfo = current_slots_comp(EventNumber, CallNumber, 
 		DepthNumber, Port, NameType, ModuleType, DefModuleName, 
 		PredName, Arity, ModeNum, Determinism, Path, LineNo) },
+	{ io__output_stream_from_mfp(OutputStream, OutputMfp) },
 	io__write(OutputStream, CurrentTraceInfo),
 	io__print(OutputStream, ".\n"),
 	io__flush_output(OutputStream).
@@ -334,13 +328,12 @@
 	"ML_DI_output_current_vars").
 			
 :- pred output_current_vars(list(univ), list(string), 
-	io__output_stream, io__state, io__state).
+	mercury_file_ptr, io__state, io__state).
 :- mode output_current_vars(in, in, in, di, uo) is det.
 
-
-output_current_vars(VarList, StringList, OutputStream) -->
-		
+output_current_vars(VarList, StringList, OutputMfp) -->
 	{ CurrentTraceInfo = current_vars(VarList, StringList) },
+	{ io__output_stream_from_mfp(OutputStream, OutputMfp) },
 	io__write(OutputStream, CurrentTraceInfo),
 	io__print(OutputStream, ".\n"),
 	io__flush_output(OutputStream).
@@ -351,31 +344,27 @@
 :- pragma export(output_current_nth_var(in, in, di, uo), 
 	"ML_DI_output_current_nth_var").
 			
-:- pred output_current_nth_var(univ, io__output_stream, io__state, io__state).
+:- pred output_current_nth_var(univ, mercury_file_ptr, io__state, io__state).
 :- mode output_current_nth_var(in, in, di, uo) is det.
 
-
-output_current_nth_var(Var, OutputStream) -->
-		
+output_current_nth_var(Var, OutputMfp) -->
 	{ CurrentTraceInfo = current_nth_var(Var) },
+	{ io__output_stream_from_mfp(OutputStream, OutputMfp) },
 	io__write(OutputStream, CurrentTraceInfo),
 	io__print(OutputStream, ".\n"),
 	io__flush_output(OutputStream).
 
-
 :- pragma export(output_current_live_var_names(in, in, in, di, uo), 
 	"ML_DI_output_current_live_var_names").
 			
 :- pred output_current_live_var_names(list(string), list(string),
-	io__output_stream, io__state, io__state).
+	mercury_file_ptr, io__state, io__state).
 :- mode output_current_live_var_names(in, in, in, di, uo) is det.
 
-
-output_current_live_var_names(LiveVarNameList, LiveVarTypeList, 
-	OutputStream) -->
-		
-	{ CurrentTraceInfo = current_live_var_names(
-				LiveVarNameList, LiveVarTypeList) },
+output_current_live_var_names(LiveVarNameList, LiveVarTypeList, OutputMfp) -->
+	{ CurrentTraceInfo = current_live_var_names(LiveVarNameList,
+		LiveVarTypeList) },
+	{ io__output_stream_from_mfp(OutputStream, OutputMfp) },
 	io__write(OutputStream, CurrentTraceInfo),
 	io__print(OutputStream, ".\n"),
 	io__flush_output(OutputStream).
@@ -428,13 +417,12 @@
 		match(MatchDepthNumber, DepthNumber),
 		match(MatchPort, Port),
 		(
-		if
 			UserPredMatch = match_user_pred(
 				MatchPredOrFunc, MatchDeclModuleName)
-		then
+		->
 			match(MatchPredOrFunc, PredOrFunc),
 			match(MatchDeclModuleName, DeclModuleName)
-		else
+		;
 			UserPredMatch = match_any_pred
 		),
 		match(MatchDefModuleName, DefModuleName),
@@ -448,7 +436,6 @@
 		error("found_match: forward_move expected")
 	).
 
-
 % match(MatchPattern, Value) is true iff Value matches the specified pattern.
 :- pred match(match(T), T).
 :- mode match(in, in) is semidet.
@@ -464,7 +451,6 @@
 	compare(GE, X, Low), 
 	(GE = (>) ; GE = (=)).
 
-
 :- pragma export(found_match_comp(in, in, in, in, in, in, in, in, in, in, in,
 			in, in, in), "ML_DI_found_match_comp").
 			
@@ -493,14 +479,13 @@
 		match(MatchDepthNumber, DepthNumber),
 		match(MatchPort, Port),
 		(
-		if
 			CompilerGeneratedPredMatch = 
 				match_compiler_generated_pred(MatchNameType,
 			MatchModuleType)
-		then
+		->
 			match(MatchNameType, NameType),
 			match(MatchModuleType, ModuleType)
-		else
+		;
 			CompilerGeneratedPredMatch = match_any_pred
 		),
 		match(MatchDefModuleName, DefModuleName),
@@ -514,18 +499,16 @@
 		error("found_match: forward_move expected")
 	).
 
-
 %-----------------------------------------------------------------------------%
 
-:- pred read_request_from_socket(io__input_stream, debugger_request, int,
-		io__state, io__state).
-			
-:- mode read_request_from_socket(in, out, out, di, uo) is det.
+:- pred read_request_from_socket(mercury_file_ptr::in, debugger_request::out,
+	int::out, io__state::di, io__state::uo) is det.
 
 :- pragma export(read_request_from_socket(in, out, out, di, uo),
 		"ML_DI_read_request_from_socket").
 
-read_request_from_socket(SocketStream, Request, RequestType) -->
+read_request_from_socket(SocketMfp, Request, RequestType) -->
+	{ io__input_stream_from_mfp(SocketStream, SocketMfp) },
 	io__read(SocketStream, MaybeRequest),
 	( { MaybeRequest = ok(Request0) },
 		{ Request = Request0 }
@@ -546,7 +529,6 @@
 	io__print(StdErr, ".\n").
 	***********/
 
-
 %-----------------------------------------------------------------------------%
 
 :- pred get_list_modules_to_import(debugger_request, int, imports).
@@ -588,6 +570,7 @@
 	;
 		error("get_mmc_options: not a mmc_options request")
 	).
+
 %-----------------------------------------------------------------------------%
 
 :- pred get_object_file_name(debugger_request, string).
@@ -663,6 +646,5 @@
 classify_request(current_grade,20).
 classify_request(collect_arg_on,21).
 classify_request(collect_arg_off,22).
-
 
 %-----------------------------------------------------------------------------%
Index: browser/declarative_debugger.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/declarative_debugger.m,v
retrieving revision 1.32
diff -u -b -r1.32 declarative_debugger.m
--- browser/declarative_debugger.m	13 Sep 2002 04:17:39 -0000	1.32
+++ browser/declarative_debugger.m	18 Sep 2002 12:01:35 -0000
@@ -401,14 +401,16 @@
 	% Export a monomorphic version of diagnosis_state_init/4, to
 	% make it easier to call from C code.
 	%
-:- pred diagnoser_state_init_store(io__input_stream, io__output_stream,
+:- pred diagnoser_state_init_store(mercury_file_ptr, mercury_file_ptr,
 		diagnoser_state(trace_node_id)).
 :- mode diagnoser_state_init_store(in, in, out) is det.
 
 :- pragma export(diagnoser_state_init_store(in, in, out),
 		"MR_DD_decl_diagnosis_state_init").
 
-diagnoser_state_init_store(InStr, OutStr, Diagnoser) :-
+diagnoser_state_init_store(InMfp, OutMfp, Diagnoser) :-
+	io__input_stream_from_mfp(InStr, InMfp),
+	io__output_stream_from_mfp(OutStr, OutMfp),
 	diagnoser_state_init(map__init, InStr, OutStr, Diagnoser).
 
 	% Export a monomorphic version of diagnosis/10, to make it
Index: browser/declarative_execution.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/declarative_execution.m,v
retrieving revision 1.21
diff -u -b -r1.21 declarative_execution.m
--- browser/declarative_execution.m	13 Sep 2002 04:17:40 -0000	1.21
+++ browser/declarative_execution.m	18 Sep 2002 12:44:46 -0000
@@ -1063,8 +1063,15 @@
 		throw(io_error("load_trace_node_map", Msg))
 	}.
 
-:- pragma export(save_trace_node_store(in, in, in, di, uo),
-		"MR_DD_save_trace").
+:- pred save_trace_node_store_mfp(mercury_file_ptr::in, trace_node_store::in,
+	trace_node_id::in, io__state::di, io__state::uo) is det.
+
+:- pragma export(save_trace_node_store_mfp(in, in, in, di, uo),
+	"MR_DD_save_trace_mfp").
+
+save_trace_node_store_mfp(Mfp, Store, NodeId) -->
+	{ io__output_stream_from_mfp(Stream, Mfp) },
+	save_trace_node_store(Stream, Store, NodeId).
 
 save_trace_node_store(Stream, Store, NodeId) -->
 	{ map__init(Map0) },
Index: browser/help.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/help.m,v
retrieving revision 1.3
diff -u -b -r1.3 help.m
--- browser/help.m	24 Mar 2000 10:27:20 -0000	1.3
+++ browser/help.m	18 Sep 2002 11:55:13 -0000
@@ -99,11 +99,32 @@
 :- pragma export(help__init(out), "ML_HELP_init").
 :- pragma export(help__add_help_node(in, in, in, in, in, out, out),
 	"ML_HELP_add_help_node").
-:- pragma export(help__help(in, in, di, uo), "ML_HELP_help").
-:- pragma export(help__path(in, in, in, out, di, uo), "ML_HELP_path").
-:- pragma export(help__name(in, in, in, di, uo), "ML_HELP_name").
+:- pragma export(help__help_mfp(in, in, di, uo), "ML_HELP_help_mfp").
+:- pragma export(help__path_mfp(in, in, in, out, di, uo), "ML_HELP_path_mfp").
+:- pragma export(help__name_mfp(in, in, in, di, uo), "ML_HELP_name_mfp").
 :- pragma export(help__help_system_type(out), "ML_HELP_help_system_type").
 :- pragma export(help__result_is_error(in, out), "ML_HELP_result_is_error").
+
+:- pred help__help_mfp(help__system::in, mercury_file_ptr::in,
+	io__state::di, io__state::uo) is det.
+
+help__help_mfp(Sys, Mfp) -->
+	{ io__output_stream_from_mfp(Stream, Mfp) },
+	help__help(Sys, Stream).
+
+:- pred help__path_mfp(help__system::in, help__path::in, mercury_file_ptr::in,
+	help__res::out, io__state::di, io__state::uo) is det.
+
+help__path_mfp(Entries, Path, Mfp, Result) -->
+	{ io__output_stream_from_mfp(Stream, Mfp) },
+	help__path(Entries, Path, Stream, Result).
+
+:- pred help__name_mfp(help__system::in, string::in, mercury_file_ptr::in,
+	io__state::di, io__state::uo) is det.
+
+help__name_mfp(Sys, Name, Mfp) -->
+	{ io__output_stream_from_mfp(Stream, Mfp) },
+	help__name(Sys, Name, Stream).
 
 %-----------------------------------------------------------------------------%
 
Index: browser/interactive_query.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/interactive_query.m,v
retrieving revision 1.16
diff -u -b -r1.16 interactive_query.m
--- browser/interactive_query.m	1 Jul 2002 09:27:10 -0000	1.16
+++ browser/interactive_query.m	18 Sep 2002 12:49:03 -0000
@@ -21,13 +21,13 @@
 
 :- pred query(query_type::in, imports::in, options::in,
 		io__input_stream::in, io__output_stream::in,
-		state::di, state::uo) is det.
+	io__state::di, io__state::uo) is det.
 
 % query_external/7 is the same as query/7 but for the use of the external 
 % debugger.
 :- pred query_external(query_type::in, imports::in, options::in,
 		io__input_stream::in, io__output_stream::in,
-		state::di, state::uo) is det.
+	io__state::di, io__state::uo) is det.
 
 :- type query_type ---> normal_query ; cc_query ; io_query.
 :- type imports == list(string).
@@ -38,10 +38,19 @@
 :- import_module mdb__dl, mdb__name_mangle.
 :- import_module mdb__util.
 
-:- pragma export(query(in, in, in, in, in, di, uo), "ML_query").
-
 :- type prog ---> prog(query_type, imports, term, varset).
 
+:- pragma export(query_mfp(in, in, in, in, in, di, uo), "ML_query_mfp").
+
+:- pred query_mfp(query_type::in, imports::in, options::in,
+	mercury_file_ptr::in, mercury_file_ptr::in,
+	io__state::di, io__state::uo) is det.
+
+query_mfp(QueryType, Imports, Options, MDB_StdinMfp, MDB_StdoutMfp) -->
+	{ io__input_stream_from_mfp(MDB_Stdin, MDB_StdinMfp) },
+	{ io__output_stream_from_mfp(MDB_Stdout, MDB_StdoutMfp) },
+	query(QueryType, Imports, Options, MDB_Stdin, MDB_Stdout).
+
 query(QueryType, Imports, Options, MDB_Stdin, MDB_Stdout) -->
 	% write_import_list(Imports),
 	util__trace_getline(query_prompt(QueryType), Result,
@@ -61,7 +70,7 @@
 
 :- pred query_2(query_type::in, imports::in, options::in,
 		io__input_stream::in, io__output_stream::in,
-		read_term(generic)::in, state::di, state::uo) is det.
+	read_term(generic)::in, io__state::di, io__state::uo) is det.
 
 query_2(QueryType, Imports, Options, MDB_Stdin, MDB_Stdout, ReadTerm) -->
 	( { ReadTerm = eof },
@@ -108,8 +117,17 @@
 	;	iq_error(string)
 	.
 
-:- pragma export(query_external(in, in, in, in, in, di, uo), 
-	"ML_query_external").
+:- pred query_external_mfp(query_type::in, imports::in, options::in,
+	mercury_file_ptr::in, mercury_file_ptr::in,
+	io__state::di, io__state::uo) is det.
+
+:- pragma export(query_external_mfp(in, in, in, in, in, di, uo), 
+	"ML_query_external_mfp").
+
+query_external_mfp(QueryType, Imports, Options, SocketInMfp, SocketOutMfp) -->
+	{ io__input_stream_from_mfp(SocketIn, SocketInMfp) },
+	{ io__output_stream_from_mfp(SocketOut, SocketOutMfp) },
+	query_external(QueryType, Imports, Options, SocketIn, SocketOut).
 
 query_external(QueryType, Imports, Options, SocketIn, SocketOut) -->
 	io__set_input_stream(SocketIn, OldStdin),
Index: browser/util.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/util.m,v
retrieving revision 1.20
diff -u -b -r1.20 util.m
--- browser/util.m	16 Sep 2002 02:26:15 -0000	1.20
+++ browser/util.m	18 Sep 2002 03:57:27 -0000
@@ -96,8 +96,14 @@
 		Result = eof
 	}.
 
-:- pred call_trace_getline(input_stream::in, output_stream::in, string::in,
-	string::out, int::out, io__state::di, io__state::uo) is det.
+:- pred call_trace_getline(io__input_stream::in, io__output_stream::in,
+	string::in, string::out, int::out, io__state::di, io__state::uo)
+	is det.
+
+call_trace_getline(MdbIn, MdbOut, Prompt, Line, Success) -->
+	{ io__input_stream_to_mfp(MdbIn, MdbInMfp) },
+	{ io__output_stream_to_mfp(MdbOut, MdbOutMfp) },
+	call_trace_getline_mfp(MdbInMfp, MdbOutMfp, Prompt, Line, Success).
 
 :- pragma c_header_code("
 	#include ""mercury_wrapper.h""
@@ -107,8 +113,12 @@
 	#include ""mercury_library_types.h""
 ").
 
+:- pred call_trace_getline_mfp(mercury_file_ptr::in, mercury_file_ptr::in,
+	string::in, string::out, int::out, io__state::di, io__state::uo)
+	is det.
+
 :- pragma foreign_proc("C",
-	call_trace_getline(MdbIn::in, MdbOut::in, Prompt::in, Line::out,
+	call_trace_getline_mfp(MdbIn::in, MdbOut::in, Prompt::in, Line::out,
 		Success::out, IO0::di, IO::uo),
 	% We need to use will_not_call_mercury here,
 	% because MR_make_aligned_string_copy() references MR_hp,
@@ -116,12 +126,10 @@
 	[will_not_call_mercury, promise_pure, tabled_for_io],
 "
 	char		*line;
-	MercuryFile	*mdb_in = (MercuryFile *) MdbIn;
-	MercuryFile	*mdb_out = (MercuryFile *) MdbOut;
 
 	if (MR_address_of_trace_getline != NULL) {
 		line = (*MR_address_of_trace_getline)((char *) Prompt,
-				MR_file(*mdb_in), MR_file(*mdb_out));
+			MR_mf_file(*MdbIn), MR_mf_file(*MdbOut));
 	} else {
 		MR_tracing_not_enabled();
 		/* not reached */
@@ -145,19 +153,25 @@
 	io__output_stream(MdbOut),
 	util__trace_get_command(Prompt, Result, MdbIn, MdbOut).
 
+util__trace_get_command(Prompt, Result, In, Out) -->
+	{ io__input_stream_to_mfp(In, InMfp) },
+	{ io__output_stream_to_mfp(Out, OutMfp) },
+	util__trace_get_command_mfp(Prompt, Result, InMfp, OutMfp).
+
+:- pred util__trace_get_command_mfp(string::in, string::out,
+	mercury_file_ptr::in, mercury_file_ptr::in,
+	io__state::di, io__state::uo) is det.
+
 :- pragma foreign_proc("C",
-	util__trace_get_command(Prompt::in, Line::out, MdbIn::in,
-		MdbOut::in, State0::di, State::uo),
+	util__trace_get_command_mfp(Prompt::in, Line::out,
+		In::in, Out::in, State0::di, State::uo),
 	[will_not_call_mercury, promise_pure, tabled_for_io],
 "
 	char		*line;
-	MercuryFile	*mdb_in = (MercuryFile *) MdbIn;
-	MercuryFile	*mdb_out = (MercuryFile *) MdbOut;
 
 	if (MR_address_of_trace_getline != NULL) {
-		line = (*MR_address_of_trace_get_command)(
-				(char *) Prompt,
-				MR_file(*mdb_in), MR_file(*mdb_out));
+		line = (*MR_address_of_trace_get_command)((char *) Prompt,
+			MR_mf_file(*In), MR_mf_file(*Out));
 	} else {
 		MR_tracing_not_enabled();
 		/* not reached */
cvs diff: Diffing bytecode
cvs diff: Diffing compiler
cvs diff: Diffing compiler/notes
cvs diff: Diffing debian
cvs diff: Diffing deep_profiler
cvs diff: Diffing deep_profiler/notes
cvs diff: Diffing doc
cvs diff: Diffing extras
cvs diff: Diffing extras/aditi
cvs diff: Diffing extras/cgi
cvs diff: Diffing extras/complex_numbers
cvs diff: Diffing extras/complex_numbers/samples
cvs diff: Diffing extras/complex_numbers/tests
cvs diff: Diffing extras/concurrency
cvs diff: Diffing extras/curs
cvs diff: Diffing extras/curs/samples
cvs diff: Diffing extras/curses
cvs diff: Diffing extras/curses/sample
cvs diff: Diffing extras/dynamic_linking
cvs diff: Diffing extras/graphics
cvs diff: Diffing extras/graphics/mercury_opengl
cvs diff: Diffing extras/graphics/mercury_tcltk
cvs diff: Diffing extras/graphics/samples
cvs diff: Diffing extras/graphics/samples/calc
cvs diff: Diffing extras/graphics/samples/maze
cvs diff: Diffing extras/graphics/samples/pent
cvs diff: Diffing extras/lazy_evaluation
cvs diff: Diffing extras/lex
cvs diff: Diffing extras/lex/samples
cvs diff: Diffing extras/logged_output
cvs diff: Diffing extras/moose
cvs diff: Diffing extras/moose/samples
cvs diff: Diffing extras/morphine
cvs diff: Diffing extras/morphine/non-regression-tests
cvs diff: Diffing extras/morphine/scripts
cvs diff: Diffing extras/morphine/source
cvs diff: Diffing extras/odbc
cvs diff: Diffing extras/posix
cvs diff: Diffing extras/quickcheck
cvs diff: Diffing extras/quickcheck/tutes
cvs diff: Diffing extras/references
cvs diff: Diffing extras/references/samples
cvs diff: Diffing extras/references/tests
cvs diff: Diffing extras/stream
cvs diff: Diffing extras/trailed_update
cvs diff: Diffing extras/trailed_update/samples
cvs diff: Diffing extras/trailed_update/tests
cvs diff: Diffing extras/xml
cvs diff: Diffing extras/xml/samples
cvs diff: Diffing java
cvs diff: Diffing java/library
cvs diff: Diffing java/runtime
cvs diff: Diffing library
Index: library/benchmarking.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/benchmarking.m,v
retrieving revision 1.51
diff -u -b -r1.51 benchmarking.m
--- library/benchmarking.m	21 Aug 2002 11:27:32 -0000	1.51
+++ library/benchmarking.m	26 Aug 2002 08:03:34 -0000
@@ -17,18 +17,18 @@
 :- module benchmarking.
 :- interface.
 
-% `report_stats' is a non-logical procedure intended for use in profiling
+% `report_stats' is a non-logical function intended for use in profiling
 % the performance of a program.
-% It has the side-effect of reporting some memory and time usage statistics
-% about the time period since the last call to report_stats to stderr.
+% It returns a string reporting some memory and time usage statistics
+% about the time period since the last call.
 
-:- impure pred report_stats is det.
+:- impure pred report_stats(string::out) is det.
 
-% `report_full_memory_stats' is a non-logical procedure intended for use
-% in profiling the memory usage of a program.  It has the side-effect of
-% reporting a full memory profile to stderr.
+% `report_full_memory_stats' is a non-logical function intended for use
+% in profiling the memory usage of a program. It returns a string
+% reporting a full memory profile.
 
-:- impure pred report_full_memory_stats is det.
+:- impure pred report_full_memory_stats(string::out) is det.
 
 % benchmark_det(Pred, In, Out, Repeats, Time) is for benchmarking the
 % det predicate Pred. We call Pred with the input In and the output Out,
@@ -69,33 +69,43 @@
 #include ""mercury_timing.h""
 #include ""mercury_heap.h""
 
-extern void ML_report_stats(void);
+extern MR_String	ML_report_stats(void);
 
-extern void ML_report_full_memory_stats(void);
+extern MR_String	ML_report_full_memory_stats(void);
 
 "). % end pragma foreign_decl
 
-:- pragma foreign_proc("C", report_stats,
+:- pragma foreign_proc("C",
+	report_stats(Stats::out),
 	[will_not_call_mercury],
 "
-	ML_report_stats();
+	MR_String	Stats0;
+
+	Stats0 = ML_report_stats();
+	MR_make_aligned_string_copy(Stats, Stats0);
 ").
 
-:- pragma foreign_proc("C", report_full_memory_stats,
+:- pragma foreign_proc("C",
+	report_full_memory_stats(Stats::out),
 	[will_not_call_mercury],
 "
 #ifdef	MR_MPROF_PROFILE_MEMORY
-	ML_report_full_memory_stats();
+	MR_String	Stats0;
+
+	Stats0 = ML_report_full_memory_stats();
+	MR_make_aligned_string_copy(Stats, Stats0);
+#else
+	MR_make_aligned_string_copy(Stats, """");
 #endif
 ").
 
-report_stats :-
+report_stats(_) :-
 	% This version is only used for back-ends for which there is no
 	% matching foreign_proc version.
 	impure private_builtin__imp,
 	private_builtin__sorry("report_stats").
 
-report_full_memory_stats :-
+report_full_memory_stats(_) :-
 	% This version is only used for back-ends for which there is no
 	% matching foreign_proc version.
 	impure private_builtin__imp,
@@ -153,14 +163,17 @@
   static int  ML_memory_profile_fill_table(MR_memprof_record *node,
 				ML_memprof_report_entry *table, int next_slot);
 
-  static void ML_memory_profile_report(const ML_memprof_report_entry *,
+  static MR_String ML_memory_profile_report(const ML_memprof_report_entry *,
 				int num_entries, MR_bool complete);
 
   static int  ML_memory_profile_compare_final(const void *, const void *);
 
 #endif /* MR_MPROF_PROFILE_MEMORY */
 
-void
+#define	ML_BUF_SIZE	1024
+#define	ML_ARENA_INIT	1024
+
+MR_String
 ML_report_stats(void)
 {
 	int			time_at_prev_stat;
@@ -171,6 +184,17 @@
 	int			num_table_entries;
 	ML_memprof_report_entry	table[MEMORY_PROFILE_SIZE];
 #endif
+	char			buf1[ML_BUF_SIZE];
+	char			buf2[ML_BUF_SIZE];
+	char			buf3[ML_BUF_SIZE];
+	char			buf4[ML_BUF_SIZE];
+	char			buf5[ML_BUF_SIZE];
+	MR_String		profile_by_proc;
+	MR_String		profile_by_type;
+	char			*profile_buf;
+	int			profile_buf_size;
+	char			*buf;
+	int			buf_size;
   
 	/*
 	** Print timing and stack usage information
@@ -183,14 +207,15 @@
 	eng = MR_get_engine();
 #endif
 
-	fprintf(stderr, 
-		""[Time: +%.3fs, %.3fs,"",
+	snprintf(buf1, ML_BUF_SIZE,
+		""Time: +%.3fs, %.3fs,"",
 		(MR_time_at_last_stat - time_at_prev_stat) / 1000.0,
 		(MR_time_at_last_stat - MR_time_at_start) / 1000.0
 	);
 
 #ifndef MR_HIGHLEVEL_CODE
-	fprintf(stderr, "" D Stack: %.3fk, ND Stack: %.3fk,"",
+	snprintf(buf2, ML_BUF_SIZE,
+		"" D Stack: %.3fk, ND Stack: %.3fk,"",
 		((char *) MR_sp - (char *)
 			eng->MR_eng_context.MR_ctxt_detstack_zone->min)
 				/ 1024.0,
@@ -198,21 +223,27 @@
 			eng->MR_eng_context.MR_ctxt_nondetstack_zone->min)
 				/ 1024.0
 	);
+#else
+	sprintf(buf2, """");
 #endif
 
 #ifdef MR_CONSERVATIVE_GC
 	{ char local_var;
-	  fprintf(stderr, "" C Stack: %.3fk,"",
+	  snprintf(buf3, ML_BUF_SIZE, "" C Stack: %.3fk,"",
 		labs(&local_var - (char *) GC_stackbottom) / 1024.0);
 	}
+#else
+	snprintf(buf3, ML_BUF_SIZE, """");
 #endif
 
 #ifdef MR_USE_TRAIL
-	fprintf(stderr,
+	snprintf(buf4, ML_BUF_SIZE,
 		"" Trail: %.3fk,"",
 		((char *) MR_trail_ptr - (char *)
 			MR_trail_zone->min) / 1024.0
 	);
+#else
+	snprintf(buf4, ML_BUF_SIZE, """");
 #endif
 
 	/*
@@ -227,7 +258,7 @@
 		committed = mps_arena_committed(mercury_mps_arena);
 		spare = mps_arena_spare_committed(mercury_mps_arena);
 
-		fprintf(stderr, 
+		snprintf(buf5, ML_BUF_SIZE,
 			""\\nHeap in use: %.3fk, spare: %.3fk, total: %.3fk"",
 			(committed - spare) / 1024.0,
 			spare / 1024.0,
@@ -235,7 +266,7 @@
 	}
   #endif /* MR_MPS_GC */
   #ifdef MR_BOEHM_GC
-	fprintf(stderr, 
+	snprintf(buf5, ML_BUF_SIZE,
 		""\\n#GCs: %lu, ""
 		""Heap used since last GC: %.3fk, Total used: %.3fk"",
 		(unsigned long) GC_gc_no,
@@ -244,7 +275,7 @@
 	);
   #endif
 #else /* !MR_CONSERVATIVE_GC */
-	fprintf(stderr, 
+	snprintf(buf5, ML_BUF_SIZE,
 		""\\nHeap: %.3fk"",
 		((char *) MR_hp - (char *) eng->MR_eng_heap_zone->min) / 1024.0
 	);
@@ -259,46 +290,87 @@
 	ML_update_counter(&MR_memprof_overall, &ML_overall_counter);
 
 	/*
-	** Print out the per-procedure memory profile (top N entries)
+	** Format the per-procedure memory profile (top N entries)
 	*/
 	num_table_entries = ML_memory_profile_top_table(MR_memprof_procs.root,
 		table, MEMORY_PROFILE_SIZE, 0);
-	fprintf(stderr, ""\\nMemory profile by procedure\\n"");
-	ML_memory_profile_report(table, num_table_entries, MR_FALSE);
+	profile_by_proc = ML_memory_profile_report(table, num_table_entries,
+		MR_FALSE);
 
 	/*
-	** Print out the per-type memory profile (top N entries)
+	** Format the per-type memory profile (top N entries)
 	*/
 	num_table_entries = ML_memory_profile_top_table(MR_memprof_types.root,
 		table, MEMORY_PROFILE_SIZE, 0);
-	fprintf(stderr, ""\\nMemory profile by type\\n"");
-	ML_memory_profile_report(table, num_table_entries, MR_FALSE);
+	profile_by_type = ML_memory_profile_report(table, num_table_entries,
+		MR_FALSE);
 
-	/*
-	** Print out the overall memory usage.
-	*/
-	fprintf(stderr, 
+	profile_buf_size = strlen(profile_by_proc) + strlen(profile_by_type)
+		+ ML_BUF_SIZE;
+
+	profile_buf = MR_malloc(profile_buf_size);
+	if (profile_buf == NULL) {
+		MR_fatal_error(""ML_report_stats: out of memory"");
+	}
+
+	snprintf(profile_buf, profile_buf_size,
+		""\\nMemory profile by procedure\\n%s""
+		""\\nMemory profile by type\\n%s""
 		""Overall memory usage:""
 		""+%8.8g %8.8g cells, +%8.8g %8.8g words\\n"",
+		profile_by_proc, profile_by_type,
 		ML_overall_counter.cells_since_period_start,
 		ML_overall_counter.cells_at_period_end,
 		ML_overall_counter.words_since_period_start,
 		ML_overall_counter.words_at_period_end
 	);
 
+	MR_free(profile_by_proc);
+	MR_free(profile_by_type);
+
+#else
+
+	profile_buf = MR_malloc(profile_buf_size);
+	if (profile_buf == NULL) {
+		MR_fatal_error(""ML_report_stats: out of memory"");
+	}
+
+	strcpy(profile_buf, """");
+
 #endif /* MR_MPROF_PROFILE_MEMORY */
 
-	fprintf(stderr, ""]\\n"");
+	buf_size = strlen(buf1) + strlen(buf2) + strlen(buf3) + strlen(buf4)
+		+ strlen(buf5) + strlen(profile_buf) + ML_BUF_SIZE;
+
+	buf = MR_malloc(buf_size);
+	if (buf == NULL) {
+		MR_fatal_error(""ML_report_stats: out of memory"");
+	}
+
+	snprintf(buf, buf_size,
+		""[%s%s%s%s%s%s]\\n"",
+		buf1, buf2, buf3, buf4, buf5, profile_buf);
+
+	MR_free(profile_buf);
+	return buf;
 }
 
 #ifdef MR_MPROF_PROFILE_MEMORY
 
-void
+MR_String
 ML_report_full_memory_stats(void)
 {
 	int			num_table_entries;
 	int			table_size;
 	ML_memprof_report_entry	*table;
+	char			*reports = NULL;
+	int			report_max = 0;
+	int			len_so_far;
+	int			len_cur;
+	MR_String		profile_by_proc;
+	MR_String		profile_by_type;
+	char			*buf;
+	int			buf_size;
 
 	/*
 	** Update the overall counter (this needs to be done first,
@@ -324,10 +396,8 @@
 	qsort(table, MR_memprof_procs.num_entries,
 		sizeof(ML_memprof_report_entry),
 		ML_memory_profile_compare_final);
-	fprintf(stderr, ""\\nMemory profile by procedure\\n"");
-	fprintf(stderr, ""%14s %14s  %s\\n"",
-		""Cells"", ""Words"", ""Procedure label"");
-	ML_memory_profile_report(table, num_table_entries, MR_TRUE);
+	profile_by_proc = ML_memory_profile_report(table, num_table_entries,
+		MR_TRUE);
 
 	/*
 	** Print the by-type memory profile
@@ -337,24 +407,40 @@
 	qsort(table, MR_memprof_types.num_entries,
 		sizeof(ML_memprof_report_entry),
 		ML_memory_profile_compare_final);
-	fprintf(stderr, ""\\nMemory profile by type\\n"");
-	fprintf(stderr, ""%14s %14s  %s\\n"",
-		""Cells"", ""Words"", ""Procedure label"");
-	ML_memory_profile_report(table, num_table_entries, MR_TRUE);
+	profile_by_type = ML_memory_profile_report(table, num_table_entries,
+		MR_TRUE);
 
 	/*
 	** Deallocate space for the table
 	*/
 	MR_GC_free(table);
 
+	buf_size = strlen(profile_by_proc) + strlen(profile_by_type)
+		+ ML_BUF_SIZE;
+
 	/*
 	** Print the overall memory usage
 	*/
-	fprintf(stderr, 
+	buf = MR_malloc(buf_size);
+	if (buf == NULL) {
+		return """";
+	}
+
+	snprintf(buf, buf_size,
+		""\\nMemory profile by procedure\\n""
+		""%14s %14s  %s\\n%s""
+		""\\nMemory profile by type\\n""
+		""%14s %14s  %s\\n%s""
 		""\\nOverall memory usage: %8.8g cells, %8.8g words\\n"",
+		""Cells"", ""Words"", ""Procedure label"", profile_by_proc,
+		""Cells"", ""Words"", ""Procedure label"", profile_by_type,
 		ML_overall_counter.cells_at_period_end,
 		ML_overall_counter.words_at_period_end
 	);
+
+	MR_free(profile_by_proc);
+	MR_free(profile_by_type);
+	return buf;
 }
 
 /*
@@ -508,24 +594,27 @@
 ** ML_memory_profile_report(table, num_entries, complete):
 **	Print out a profiling report for the specified table.
 */
-static void
+static MR_String
 ML_memory_profile_report(const ML_memprof_report_entry *table, int num_entries,
 	MR_bool complete)
 {
 	int		i;
 	const char	*name;
+	char		*reports = NULL;
+	int		report_max = 0;
+	int		len_so_far;
+	int		len_cur;
+	char		buf[ML_BUF_SIZE];
 
 	if (complete) {
 		if (ML_overall_counter.cells_at_period_end < 1.0
 		||  ML_overall_counter.words_at_period_end < 1.0) {
-			fprintf(stderr, ""no allocations to report\\n"");
-			return;
+			return ""no allocations to report\\n"";
 		}
 	} else {
 		if (ML_overall_counter.cells_since_period_start < 1.0
 		||  ML_overall_counter.words_since_period_start < 1.0) {
-			fprintf(stderr, ""no allocations to report\\n"");
-			return;
+			return ""no allocations to report\\n"";
 		}
 	}
 
@@ -533,9 +622,10 @@
 		num_entries = MAX_REPORT_LINES;
 	}
 
+	len_so_far = 0;
 	for (i = 0; i < num_entries; i++) {
 		if (complete) {
-			fprintf(stderr,
+			snprintf(buf, ML_BUF_SIZE,
 				""%8.8g/%4.1f%% %8.8g/%4.1f%%  %s\\n"",
 				table[i].counter.cells_at_period_end,
 				100 * table[i].counter.cells_at_period_end /
@@ -546,7 +636,7 @@
 				table[i].name
 			);
 		} else {
-			fprintf(stderr,
+			snprintf(buf, ML_BUF_SIZE,
 				""%8.8g/%4.1f%% %8.8g/%4.1f%%  %s\\n"",
 				table[i].counter.cells_since_period_start,
 				100 *
@@ -559,7 +649,15 @@
 				table[i].name
 			);
 		}
+
+		len_cur = strlen(buf);
+		MR_ensure_big_enough(len_so_far + len_cur + 1, report,
+			char, ML_ARENA_INIT);
+		strcpy(reports + len_so_far, buf);
+		len_so_far += len_cur;
 	}
+
+	return reports;
 }
 
 /*
Index: library/io.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/io.m,v
retrieving revision 1.266
diff -u -b -r1.266 io.m
--- library/io.m	24 Sep 2002 06:55:17 -0000	1.266
+++ library/io.m	24 Sep 2002 10:14:10 -0000
@@ -317,8 +317,6 @@
 :- mode io__ignore_whitespace(in, out, di, uo) is det.
 %		Discards all the whitespace from the specified stream.
 
-
-
 %-----------------------------------------------------------------------------%
 
 % Text output predicates.
@@ -713,7 +711,6 @@
 :- mode io__set_output_line_number(in, in, di, uo) is det.
 %	Set the line number of the specified output stream.
 
-
 %-----------------------------------------------------------------------------%
 
 % Binary input predicates.
@@ -1138,6 +1135,11 @@
 :- pred io__report_stats(io__state, io__state).
 :- mode io__report_stats(di, uo) is det.
 
+	% Write memory/time usage statistics to the specified stream.
+
+:- pred io__report_standard_stats(io__output_stream, io__state, io__state).
+:- mode io__report_standard_stats(in, di, uo) is det.
+
 	% Write complete memory usage statistics to stderr,
 	% including information about all procedures and types.
 	% (You need to compile with memory profiling enabled.)
@@ -1149,10 +1151,10 @@
 :- pred io__report_full_memory_stats(io__state, io__state).
 :- mode io__report_full_memory_stats(di, uo) is det.
 
-	% Write statistics to stderr; what statistics will be written
-	% is controlled by the first argument, which acts a selector.
-	% What selector values cause what statistics to be printed
-	% is implementation defined.
+	% Write statistics to the specified stream (the default being stderr).
+	% What statistics will be written is controlled by the first argument,
+	% which acts a selector. What selector values cause what statistics
+	% to be printed is implementation defined.
 	%
 	% The Melbourne implementation supports the following selectors:
 	%
@@ -1168,6 +1170,9 @@
 	%			to have been compiled with the macro
 	%			MR_TABLE_STATISTICS defined.
 
+:- pred io__report_stats(io__output_stream, string, io__state, io__state).
+:- mode io__report_stats(in, in, di, uo) is det.
+
 :- pred io__report_stats(string, io__state, io__state).
 :- mode io__report_stats(in, di, uo) is det.
 
@@ -1270,6 +1275,37 @@
 :- func io__handle_system_command_exit_status(int) =
 		io__res(io__system_result).
 
+	% This type represents io__streams (input and output, text and binary)
+	% for the C code in the debugger implementation. It and the predicates
+	% that operate on it are exported to the debugger.
+
+	% The definition of io__stream should be kept the same as the
+	% definition of mercury_file_ptr.
+:- type mercury_file_ptr --->	mercury_file_ptr(c_pointer).
+:- pragma foreign_type("C", mercury_file_ptr, "MR_MercuryFilePtr").
+:- pragma foreign_type(il, mercury_file_ptr, "class [mscorlib]System.Object").
+
+:- pred io__input_stream_to_mfp(io__input_stream::in, mercury_file_ptr::out)
+	is det.
+:- pred io__output_stream_to_mfp(io__output_stream::in, mercury_file_ptr::out)
+	is det.
+
+:- pred io__input_stream_from_mfp(io__input_stream::out, mercury_file_ptr::in)
+	is det.
+:- pred io__output_stream_from_mfp(io__output_stream::out, mercury_file_ptr::in)
+	is det.
+
+	% This type represents io__streams (input and output, text and binary)
+	% for the users of the debugger. It and the predicates that operate
+	% on it are exported to the debugger.
+	%
+	% The arguments give the values of the id and name fields of
+	% MR_MercuryFile structures respectively.
+
+:- type io__stream_rep	--->	stream_rep(int, string).
+
+:- pred io__make_stream_rep(int::in, string::in, io__stream_rep::out) is det.
+
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
 
@@ -1279,15 +1315,58 @@
 :- use_module table_builtin.
 :- use_module rtti_implementation.
 
-:- type io__state ---> io__state(c_pointer).
 	% Values of type `io__state' are never really used:
 	% instead we store data in global variables.
 	% The reason this is not defined simply as `io__state == c_pointer'
 	% is so that `type_name' produces more informative results
 	% for cases such as `type_name(main)'.
 
+:- type io__state --->	io__state(c_pointer).
+
+	% We should have a foreign_type definition of the type io__stream for
+	% every backend. The Mercury definition of the io__stream type should 
+	% never be used.
+	%
+	% The definition of io__stream should be kept the same as the
+	% definition of mercury_file_ptr.
+
+:- type io__stream --->	io__stream(c_pointer).
+:- pragma foreign_type("C", io__stream, "MR_MercuryFilePtr").
+:- pragma foreign_type(il, io__stream, "class [mscorlib]System.Object").
+% The il definition is a placeholder only.
+
+:- pragma foreign_proc("C",
+	io__input_stream_to_mfp(Stream::in, Mfp:: out),
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
+"
+	Mfp = Stream;
+").
+
+:- pragma foreign_proc("C",
+	io__output_stream_to_mfp(Stream::in, Mfp:: out),
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
+"
+	Mfp = Stream;
+").
+
+:- pragma foreign_proc("C",
+	io__input_stream_from_mfp(Stream::out, Mfp:: in),
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
+"
+	Stream = Mfp;
+").
+
+:- pragma foreign_proc("C",
+	io__output_stream_from_mfp(Stream::out, Mfp:: in),
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
+"
+	Stream = Mfp;
+").
+
+:- pragma export(io__make_stream_rep(in, in, out), "ML_io_make_stream_rep").
+io__make_stream_rep(SeqNo, Name, stream_rep(SeqNo, Name)).
+
 :- pragma foreign_decl("C", "
-	extern MR_Word		ML_io_stream_names;
 	extern MR_Word		ML_io_user_globals;
 	#if 0
 	  extern MR_Word	ML_io_ops_table;
@@ -1295,7 +1374,6 @@
 ").
 
 :- pragma foreign_code("C", "
-	MR_Word			ML_io_stream_names;
 	MR_Word			ML_io_user_globals;
 	#if 0
 	  MR_Word		ML_io_ops_table;
@@ -1303,18 +1381,11 @@
 ").
 
 :- pragma foreign_code("MC++", "
-#ifdef MR_HIGHLEVEL_DATA
-	static mercury::tree234::tree234_2 __gc	*ML_io_stream_names;
-#else
-	static MR_Word		ML_io_stream_names;
-#endif
 	static MR_Univ		ML_io_user_globals;
 	static int next_id;
 	static System::Text::ASCIIEncoding *ascii_encoder;
 ").
 
-
-:- type io__stream_names ==	map(io__stream_id, string).
 :- type io__stream_putback ==	map(io__stream_id, list(char)).
 
 :- type io__input_stream ==	io__stream.
@@ -1322,24 +1393,11 @@
 
 :- type io__binary_stream ==	io__stream.
 
-:- type io__stream == c_pointer.
-
 	% a unique identifier for an IO stream
 :- type io__stream_id == int.
 
 :- func io__get_stream_id(io__stream) = io__stream_id.
 
-/*
- * In NU-Prolog: 
- *	io__stream	--->	stream(int, int)
- *			;	user_input
- *			;	user_output
- *			;	user_error.
- * In C:
- *	io__stream	==	pointer to MercuryFile (which is defined
- *				in runtime/mercury_library_types.h)
- */
-
 	% This inter-language stuff is tricky.
 	% We communicate via ints rather than via io__result_codes because
 	% we don't want the C code to depend on how Mercury stores its
@@ -1548,10 +1606,10 @@
 		io__state, io__state).
 :- mode io__read_line_as_string_2(in, out, out, di, uo) is det.
 
-:- pragma foreign_proc("C", io__read_line_as_string_2(File::in, Res :: out,
-			RetString::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure,
-			tabled_for_io, thread_safe],
+:- pragma foreign_proc("C",
+	io__read_line_as_string_2(File::in, Res :: out, RetString::out,
+		IO0::di, IO::uo),
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "
 #define ML_IO_READ_LINE_GROW(n)	((n) * 3 / 2)
 #define ML_IO_BYTES_TO_WORDS(n)	(((n) + sizeof(MR_Word) - 1) / sizeof(MR_Word))
@@ -1565,7 +1623,7 @@
 
 	Res = 0;
 	for (i = 0; char_code != '\\n'; ) {
-		char_code = mercury_getc((MercuryFile *) File);
+		char_code = mercury_getc(File);
 		if (char_code == EOF) {
 			if (i == 0) {
 				Res = -1;
@@ -1763,21 +1821,20 @@
 :- mode io__clear_err(in, di, uo) is det.
 % same as ANSI C's clearerr().
 
-:- pragma foreign_proc("C", io__clear_err(Stream::in, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure,
-			tabled_for_io, thread_safe],
+:- pragma foreign_proc("C",
+	io__clear_err(Stream::in, IO0::di, IO::uo),
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "{
-	MercuryFile *f = (MercuryFile *) Stream;
-
-	if (MR_IS_FILE_STREAM(*f)) {
-		clearerr(MR_file(*f));
+	if (MR_IS_FILE_STREAM(*Stream)) {
+		clearerr(MR_mf_file(*Stream));
 	} else {
 		/* Not a file stream so do nothing */
 	}
 	MR_update_io(IO0, IO);
 }").
 
-:- pragma foreign_proc("MC++", io__clear_err(_Stream::in, IO0::di, IO::uo),
+:- pragma foreign_proc("MC++",
+	io__clear_err(_Stream::in, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, thread_safe],
 "{
 	// XXX no error flag to reset as in MC++ an error throws directly an
@@ -1791,7 +1848,6 @@
 	% matching foreign_proc version.
 	{ private_builtin__sorry("io__clear_err") }.
 
-
 :- pred io__check_err(stream, io__res, io__state, io__state).
 :- mode io__check_err(in, out, di, uo) is det.
 
@@ -1807,15 +1863,12 @@
 :- mode io__ferror(in, out, out, di, uo) is det.
 % similar to ANSI C's ferror().
 
-:- pragma foreign_proc("C", ferror(Stream::in, RetVal::out, RetStr::out,
-		IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure,
-			tabled_for_io, thread_safe],
+:- pragma foreign_proc("C",
+	io__ferror(Stream::in, RetVal::out, RetStr::out, IO0::di, IO::uo),
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "{
-	MercuryFile *f = (MercuryFile *) Stream;
-
-	if (MR_IS_FILE_STREAM(*f)) {
-		RetVal = ferror(MR_file(*f));
+	if (MR_IS_FILE_STREAM(*Stream)) {
+		RetVal = ferror(MR_mf_file(*Stream));
 	} else {
 		RetVal = -1;
 	}
@@ -1825,8 +1878,8 @@
 	MR_update_io(IO0, IO);
 }").
 
-:- pragma foreign_proc("MC++", ferror(_Stream::in, RetVal::out, _RetStr::out,
-		IO0::di, IO::uo),
+:- pragma foreign_proc("MC++",
+	io__ferror(_Stream::in, RetVal::out, _RetStr::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, thread_safe],
 "{
 	// XXX see clearerr
@@ -1834,10 +1887,10 @@
 	MR_update_io(IO0, IO);
 }").
 
-ferror(_, _, _) -->
+io__ferror(_, _, _) -->
 	% This version is only used for back-ends for which there is no
 	% matching foreign_proc version.
-	{ private_builtin__sorry("ferror") }.
+	{ private_builtin__sorry("io__ferror") }.
 
 % io__make_err_msg(MessagePrefix, Message):
 %	`Message' is an error message obtained by looking up the
@@ -1866,7 +1919,6 @@
 	% matching foreign_proc version.
 	{ private_builtin__sorry("io__make_err_msg") }.
 
-
 %-----------------------------------------------------------------------------%
 
 :- pred io__stream_file_size(stream, int, io__state, io__state).
@@ -1884,18 +1936,16 @@
 #endif
 ").
 
-:- pragma foreign_proc("C", io__stream_file_size(Stream::in, Size::out,
-		IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io,
-			thread_safe],
+:- pragma foreign_proc("C",
+	io__stream_file_size(Stream::in, Size::out, IO0::di, IO::uo),
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "{
-	MercuryFile *f = (MercuryFile *) Stream;
 #if defined(MR_HAVE_FSTAT) && \
     (defined(MR_HAVE_FILENO) || defined(fileno)) && \
     defined(S_ISREG)
 	struct stat s;
-	if (MR_IS_FILE_STREAM(*f)) {
-		if (fstat(fileno(MR_file(*f)), &s) == 0 &&
+	if (MR_IS_FILE_STREAM(*Stream)) {
+		if (fstat(fileno(MR_mf_file(*Stream)), &s) == 0 &&
 				S_ISREG(s.st_mode))
 		{
 			Size = s.st_size;
@@ -1911,8 +1961,8 @@
 	MR_update_io(IO0, IO);
 }").
 
-:- pragma foreign_proc("MC++", io__stream_file_size(Stream::in, Size::out,
-		IO0::di, IO::uo),
+:- pragma foreign_proc("MC++",
+	io__stream_file_size(Stream::in, Size::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, thread_safe],
 "{
 	MR_MercuryFile mf = ML_DownCast(MR_MercuryFile, 
@@ -1930,7 +1980,6 @@
 	% matching foreign_proc version.
 	{ private_builtin__sorry("io__stream_file_size") }.
 	
-
 io__file_modification_time(File, Result) -->
 	io__file_modification_time_2(File, Status, Msg, Time),
 	{ Status = 1 ->
@@ -1997,6 +2046,7 @@
 	private_builtin__sorry("io__alloc_buffer").
 
 :- pred io__resize_buffer(buffer::di, int::in, int::in, buffer::uo) is det.
+
 :- pragma foreign_proc("C",
 	io__resize_buffer(Buffer0::di, OldSize::in,
 		NewSize::in, Buffer::uo),
@@ -2073,21 +2123,18 @@
 :- pragma foreign_proc("C",
 	io__read_into_buffer(Stream::in, Buffer0::di, Pos0::in, Size::in,
 		    Buffer::uo, Pos::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io,
-			thread_safe],
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "{
-	MercuryFile *f = (MercuryFile *) Stream;
 	char *buffer = (MR_Char *) Buffer0;
 	int items_read;
 
-	items_read = MR_READ(*f, buffer + Pos0, Size - Pos0);
+	items_read = MR_READ(*Stream, buffer + Pos0, Size - Pos0);
 
 	Buffer = (MR_Word) buffer;
 	Pos = Pos0 + items_read;
 	MR_update_io(IO0, IO);
 }").
 
-
 io__read_into_buffer(_, _, _, _, _, _) -->
 	% This version is only used for back-ends for which there is no
 	% matching foreign_proc version.
@@ -2785,8 +2832,7 @@
 io__open_input(FileName, Result) -->
 	io__do_open(FileName, "r", Result0, NewStream),
 	( { Result0 \= -1 } ->
-		{ Result = ok(NewStream) },
-		io__insert_stream_name(NewStream, FileName)
+		{ Result = ok(NewStream) }
 	;
 		io__make_err_msg("can't open input file: ", Msg),
 		{ Result = error(io_error(Msg)) }
@@ -2795,8 +2841,7 @@
 io__open_output(FileName, Result) -->
 	io__do_open(FileName, "w", Result0, NewStream),
 	( { Result0 \= -1 } ->
-		{ Result = ok(NewStream) },
-		io__insert_stream_name(NewStream, FileName)
+		{ Result = ok(NewStream) }
 	;
 		io__make_err_msg("can't open output file: ", Msg),
 		{ Result = error(io_error(Msg)) }
@@ -2805,8 +2850,7 @@
 io__open_append(FileName, Result) -->
 	io__do_open(FileName, "a", Result0, NewStream),
 	( { Result0 \= -1 } ->
-		{ Result = ok(NewStream) },
-		io__insert_stream_name(NewStream, FileName)
+		{ Result = ok(NewStream) }
 	;
 		io__make_err_msg("can't append to file: ", Msg),
 		{ Result = error(io_error(Msg)) }
@@ -2815,8 +2859,7 @@
 io__open_binary_input(FileName, Result) -->
 	io__do_open(FileName, "rb", Result0, NewStream),
 	( { Result0 \= -1 } ->
-		{ Result = ok(NewStream) },
-		io__insert_stream_name(NewStream, FileName)
+		{ Result = ok(NewStream) }
 	;
 		io__make_err_msg("can't open input file: ", Msg),
 		{ Result = error(io_error(Msg)) }
@@ -2825,8 +2868,7 @@
 io__open_binary_output(FileName, Result) -->
 	io__do_open(FileName, "wb", Result0, NewStream),
 	( { Result0 \= -1 } ->
-		{ Result = ok(NewStream) },
-		io__insert_stream_name(NewStream, FileName)
+		{ Result = ok(NewStream) }
 	;
 		io__make_err_msg("can't open output file: ", Msg),
 		{ Result = error(io_error(Msg)) }
@@ -2835,8 +2877,7 @@
 io__open_binary_append(FileName, Result) -->
 	io__do_open(FileName, "ab", Result0, NewStream),
 	( { Result0 \= -1 } ->
-		{ Result = ok(NewStream) },
-		io__insert_stream_name(NewStream, FileName)
+		{ Result = ok(NewStream) }
 	;
 		io__make_err_msg("can't append to file: ", Msg),
 		{ Result = error(io_error(Msg)) }
@@ -2952,79 +2993,14 @@
 :- pred io__stream_name(io__stream, string, io__state, io__state).
 :- mode io__stream_name(in, out, di, uo) is det.
 
-io__stream_name(Stream, Name) -->
-	io__get_stream_names(StreamNames),
-	{ map__search(StreamNames, get_stream_id(Stream), Name1) ->
-		Name = Name1
-	;
-		Name = "<stream name unavailable>"
-	},
-	io__set_stream_names(StreamNames).
-
-:- pred io__get_stream_names(io__stream_names, io__state, io__state).
-:- mode io__get_stream_names(out, di, uo) is det.
-
-:- pred io__set_stream_names(io__stream_names, io__state, io__state).
-:- mode io__set_stream_names(in, di, uo) is det.
-
 :- pragma foreign_proc("C", 
-		io__get_stream_names(StreamNames::out, IO0::di, IO::uo), 
-		[will_not_call_mercury, promise_pure, tabled_for_io],
-"
-	StreamNames = ML_io_stream_names;
-	MR_update_io(IO0, IO);
-").
-
-:- pragma foreign_proc("C", 
-		io__set_stream_names(StreamNames::in, IO0::di, IO::uo), 
-		[will_not_call_mercury, promise_pure, tabled_for_io],
-"
-	ML_io_stream_names = StreamNames;
-	MR_update_io(IO0, IO);
-").
-
-:- pragma foreign_proc("MC++", 
-		io__get_stream_names(StreamNames::out, IO0::di, IO::uo), 
-		[will_not_call_mercury, promise_pure, tabled_for_io],
-"
-	StreamNames = ML_io_stream_names;
-	MR_update_io(IO0, IO);
-").
-
-:- pragma foreign_proc("MC++", 
-		io__set_stream_names(StreamNames::in, IO0::di, IO::uo), 
-		[will_not_call_mercury, promise_pure, tabled_for_io],
+	io__stream_name(Stream::in, Name::out, IO0::di, IO::uo),
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "
-	ML_io_stream_names = StreamNames;
+	MR_make_aligned_string_copy(Name, MR_mf_name(*Stream));
 	MR_update_io(IO0, IO);
 ").
 
-io__get_stream_names(_) -->
-	% This version is only used for back-ends for which there is no
-	% matching foreign_proc version.
-	{ private_builtin__sorry("io__get_stream_names") }.
-
-io__set_stream_names(_) -->
-	% This version is only used for back-ends for which there is no
-	% matching foreign_proc version.
-	{ private_builtin__sorry("io__set_stream_names") }.
-
-:- pred io__delete_stream_name(io__stream, io__state, io__state).
-:- mode io__delete_stream_name(in, di, uo) is det.
-
-io__delete_stream_name(Stream) -->
-	io__get_stream_names(StreamNames0),
-	{ map__delete(StreamNames0, get_stream_id(Stream), StreamNames) },
-	io__set_stream_names(StreamNames).
-
-:- pred io__insert_stream_name(io__stream, string, io__state, io__state).
-:- mode io__insert_stream_name(in, in, di, uo) is det.
-
-io__insert_stream_name(Stream, Name) -->
-	io__get_stream_names(StreamNames0),
-	{ map__set(StreamNames0, get_stream_id(Stream), Name, StreamNames) },
-	io__set_stream_names(StreamNames).
-
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
 
@@ -3080,7 +3056,6 @@
 	io__progname(DefaultName, Progname),
 	{ dir__basename(Progname, PrognameBase) }.
 
-
 	% XXX we call a pred version of io__get_stream_id, which is a
 	% bit inelegant.  We should either fix the MC++ interface so you
 	% can implement functions, or implement everything in this
@@ -3092,26 +3067,15 @@
 
 :- pragma foreign_proc("C",
 	io__get_stream_id(Stream::in, Id::out), 
-		[will_not_call_mercury, promise_pure], "
-
-#ifndef MR_NATIVE_GC
-	/* 
-	** Most of the time, we can just use the pointer to the stream
-	** as a unique identifier.
-	*/
-	Id = (MR_Word) Stream;
-#else
-	/* 
-	** for accurate GC we embed an ID in the MercuryFile
-	** and retrieve it here.
-	*/
-	Id = ((MercuryFile *) Stream)->id;
-#endif
+	[will_not_call_mercury, promise_pure],
+"
+	Id = MR_mf_id(Stream);
 ").
 
 :- pragma foreign_proc("MC++",
 	io__get_stream_id(Stream::in, Id::out), 
-		[will_not_call_mercury, promise_pure], "
+	[will_not_call_mercury, promise_pure],
+"
 	MR_MercuryFile mf = ML_DownCast(MR_MercuryFile,
 		MR_word_to_c_pointer(Stream));
 	Id = mf->id;
@@ -3155,26 +3119,37 @@
 % statistics reporting predicates
 
 io__report_stats -->
-	io__report_stats("standard").
+	io__stderr_stream(StdErr),
+	io__report_standard_stats(StdErr).
+
+io__report_standard_stats(Stream) -->
+	io__report_stats(Stream, "standard").
 
 io__report_full_memory_stats -->
 	io__report_stats("full_memory_stats").
 
-:- pragma promise_pure(io__report_stats/3).
-
 io__report_stats(Selector) -->
-	{ Selector = "standard" ->
-		impure report_stats
-	; Selector = "full_memory_stats" ->
-		impure report_full_memory_stats
-	; Selector = "tabling" ->
-		impure table_builtin__table_report_statistics
+	io__stderr_stream(StdErr),
+	io__report_stats(StdErr, Selector).
+
+:- pragma promise_pure(io__report_stats/4).
+
+io__report_stats(Stream, Selector) -->
+	( { Selector = "standard" } ->
+		{ impure report_stats(Stats) },
+		io__write_string(Stream, Stats)
+	; { Selector = "full_memory_stats" } ->
+		{ impure report_full_memory_stats(Stats) },
+		io__write_string(Stream, Stats)
+	; { Selector = "tabling" } ->
+		{ impure table_builtin__table_report_statistics(Stats) },
+		io__write_string(Stream, Stats)
 	;
-		string__format(
+		{ string__format(
 			"io__report_stats: selector `%s' not understood",
-			[s(Selector)], Message),
-		error(Message)
-	}.
+			[s(Selector)], Message) },
+		{ error(Message) }
+	).
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
@@ -3194,13 +3169,10 @@
 :- pragma export(io__init_state(di, uo), "ML_io_init_state").
 
 io__init_state -->
-	io__gc_init(type_of(StreamNames), type_of(Globals)),
-	{ map__init(StreamNames) },
+	io__gc_init(type_of(Globals)),
 	{ type_to_univ("<globals>", Globals) },
-	io__set_stream_names(StreamNames),
 	io__set_op_table(ops__init_mercury_op_table),
-	io__set_globals(Globals),
-	io__insert_std_stream_names.
+	io__set_globals(Globals).
 
 :- pred io__finalize_state(io__state, io__state).
 :- mode io__finalize_state(di, uo) is det.
@@ -3215,42 +3187,30 @@
 	% we don't bother.)
 	[].
 
-:- pred io__gc_init(type_desc, type_desc, io__state, io__state).
-:- mode io__gc_init(in, in, di, uo) is det.
+:- pred io__gc_init(type_desc, io__state, io__state).
+:- mode io__gc_init(in, di, uo) is det.
 
 :- pragma foreign_proc("C", 
-		io__gc_init(StreamNamesType::in, UserGlobalsType::in,
-		IO0::di, IO::uo),
+	io__gc_init(UserGlobalsType::in, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
 	/* for Windows DLLs, we need to call GC_INIT() from each DLL */
 #ifdef MR_CONSERVATIVE_GC
 	GC_INIT();
 #endif
-	MR_add_root(&ML_io_stream_names, (MR_TypeInfo) StreamNamesType);
 	MR_add_root(&ML_io_user_globals, (MR_TypeInfo) UserGlobalsType);
 	MR_update_io(IO0, IO);
 ").
 
 :- pragma foreign_proc("MC++", 
-		io__gc_init(_StreamNamesType::in, _UserGlobalsType::in,
-		IO0::di, IO::uo), [will_not_call_mercury, promise_pure], "
+	io__gc_init(_UserGlobalsType::in, IO0::di, IO::uo),
+	[will_not_call_mercury, promise_pure],
+"
 	MR_update_io(IO0, IO);
 	ascii_encoder =	new System::Text::ASCIIEncoding();
 ").
 
-io__gc_init(_, _) --> [].
-
-:- pred io__insert_std_stream_names(io__state, io__state).
-:- mode io__insert_std_stream_names(di, uo) is det.
-
-io__insert_std_stream_names -->
-	io__stdin_stream(Stdin),
-	io__insert_stream_name(Stdin, "<standard input>"),
-	io__stdout_stream(Stdout),
-	io__insert_stream_name(Stdout, "<standard output>"),
-	io__stderr_stream(Stderr),
-	io__insert_stream_name(Stderr, "<standard error>").
+io__gc_init(_) --> [].
 
 io__call_system(Command, Result) -->
 	io__call_system_return_signal(Command, Result0),
@@ -3346,32 +3306,35 @@
   #include <sys/wait.h>		/* for WIFEXITED, WEXITSTATUS, etc. */
 #endif
 
-extern MercuryFile mercury_stdin;
-extern MercuryFile mercury_stdout;
-extern MercuryFile mercury_stderr;
-extern MercuryFile mercury_stdin_binary;
-extern MercuryFile mercury_stdout_binary;
-extern MercuryFile *mercury_current_text_input;
-extern MercuryFile *mercury_current_text_output;
-extern MercuryFile *mercury_current_binary_input;
-extern MercuryFile *mercury_current_binary_output;
+extern	MR_MercuryFile		mercury_stdin;
+extern	MR_MercuryFile		mercury_stdout;
+extern	MR_MercuryFile		mercury_stderr;
+extern	MR_MercuryFile		mercury_stdin_binary;
+extern	MR_MercuryFile		mercury_stdout_binary;
+extern	MR_MercuryFilePtr	mercury_current_text_input;
+extern	MR_MercuryFilePtr	mercury_current_text_output;
+extern	MR_MercuryFilePtr	mercury_current_binary_input;
+extern	MR_MercuryFilePtr	mercury_current_binary_output;
 
 #define MR_initial_io_state()		0	/* some random number */
 #define MR_final_io_state(r)		((void)0)
 
 #define MR_update_io(r_src, r_dest)	((r_dest) = (r_src))
 
-void 		mercury_init_io(void);
-MercuryFile*	mercury_open(const char *filename, const char *openmode);
-void		mercury_io_error(MercuryFile* mf, const char *format, ...);
-void		mercury_output_error(MercuryFile* mf);
-void		mercury_print_string(MercuryFile* mf, const char *s);
-void		mercury_print_binary_string(MercuryFile* mf, const char *s);
-int		mercury_getc(MercuryFile* mf);
-void		mercury_close(MercuryFile* mf);
-int		ML_fprintf(MercuryFile* mf, const char *format, ...);
-").
+extern	void 	mercury_init_io(void);
+extern	MR_MercuryFilePtr
+		mercury_open(const char *filename, const char *openmode);
+extern	void	mercury_io_error(MR_MercuryFilePtr mfp, const char *format,
+			...);
+extern	void	mercury_output_error(MR_MercuryFilePtr mfp);
+extern	void	mercury_print_string(MR_MercuryFilePtr mfp, const char *s);
+extern	void	mercury_print_binary_string(MR_MercuryFilePtr mfp,
+			const char *s);
+extern	int	mercury_getc(MR_MercuryFilePtr mfp);
+extern	void	mercury_close(MR_MercuryFilePtr mfp);
+extern	int	ML_fprintf(MR_MercuryFilePtr mfp, const char *format, ...);
 
+").
 
 :- pragma foreign_decl("MC++", "
 
@@ -3407,42 +3370,43 @@
 #define MR_update_io(r_src, r_dest)	(0)
 #define MR_final_io_state(r)
 
-
 ").
 
 :- pragma foreign_code("C", "
 
-MercuryFile mercury_stdin;
-MercuryFile mercury_stdout;
-MercuryFile mercury_stderr;
-MercuryFile mercury_stdin_binary;
-MercuryFile mercury_stdout_binary;
-
-MercuryFile *mercury_current_text_input = &mercury_stdin;
-MercuryFile *mercury_current_text_output = &mercury_stdout;
-MercuryFile *mercury_current_binary_input = &mercury_stdin_binary;
-MercuryFile *mercury_current_binary_output = &mercury_stdout_binary;
+MR_MercuryFile		mercury_stdin;
+MR_MercuryFile		mercury_stdout;
+MR_MercuryFile		mercury_stderr;
+MR_MercuryFile		mercury_stdin_binary;
+MR_MercuryFile		mercury_stdout_binary;
+
+MR_MercuryFilePtr	mercury_current_text_input = &mercury_stdin;
+MR_MercuryFilePtr	mercury_current_text_output = &mercury_stdout;
+MR_MercuryFilePtr	mercury_current_binary_input = &mercury_stdin_binary;
+MR_MercuryFilePtr	mercury_current_binary_output = &mercury_stdout_binary;
 
 void
 mercury_init_io(void)
 {
-	MR_mercuryfile_init(stdin, 1, &mercury_stdin);
-	MR_mercuryfile_init(stdout, 1, &mercury_stdout);
-	MR_mercuryfile_init(stderr, 1, &mercury_stderr);
-
-	MR_mercuryfile_init(NULL, 1, &mercury_stdin_binary);
-	MR_mercuryfile_init(NULL, 1, &mercury_stdout_binary);
+	MR_mercuryfile_init(stdin,  ""<stdin>"",  1, &mercury_stdin);
+	MR_mercuryfile_init(stdout, ""<stdout>"", 1, &mercury_stdout);
+	MR_mercuryfile_init(stderr, ""<stderr>"", 1, &mercury_stderr);
+
+	MR_mercuryfile_init(NULL, ""<stdin_binary>"",  1,
+		&mercury_stdin_binary);
+	MR_mercuryfile_init(NULL, ""<stdout_binary>"", 1,
+		&mercury_stdout_binary);
 
 #if defined(MR_HAVE_FDOPEN) && (defined(MR_HAVE_FILENO) || defined(fileno)) && \
     defined(MR_HAVE_DUP)
-	MR_file(mercury_stdin_binary) = fdopen(dup(fileno(stdin)), ""rb"");
-	if (MR_file(mercury_stdin_binary) == NULL) {
+	MR_mf_file(mercury_stdin_binary) = fdopen(dup(fileno(stdin)), ""rb"");
+	if (MR_mf_file(mercury_stdin_binary) == NULL) {
 		MR_fatal_error(""error opening standard input stream in ""
 			""binary mode:\\n\\tfdopen() failed: %s"",
 			strerror(errno));
 	}
-	MR_file(mercury_stdout_binary) = fdopen(dup(fileno(stdout)), ""wb"");
-	if (MR_file(mercury_stdout_binary) == NULL) {
+	MR_mf_file(mercury_stdout_binary) = fdopen(dup(fileno(stdout)), ""wb"");
+	if (MR_mf_file(mercury_stdout_binary) == NULL) {
 		MR_fatal_error(""error opening standard output stream in ""
 			""binary mode:\\n\\tfdopen() failed: %s"",
 			strerror(errno));
@@ -3452,8 +3416,8 @@
 	** XXX Standard ANSI/ISO C provides no way to set stdin/stdout
 	** to binary mode.  I guess we just have to punt...
 	*/
-	MR_file(mercury_stdin_binary) = stdin;
-	MR_file(mercury_stdout_binary) = stdout;
+	MR_mf_file(mercury_stdin_binary) = stdin;
+	MR_mf_file(mercury_stdout_binary) = stdout;
 #endif
 }
 
@@ -3520,20 +3484,22 @@
 
 ").
 
-
 :- pragma foreign_code("C", "
 
-MercuryFile*
+MR_MercuryFilePtr
 mercury_open(const char *filename, const char *openmode)
 {
-	MercuryFile *mf;
-	FILE *f;
+	MR_MercuryFilePtr	mfp;
+	FILE			*fp;
 
-	f = fopen(filename, openmode);
-	if (!f) return NULL;
-	mf = MR_GC_NEW(MercuryFile);
-	MR_mercuryfile_init(f, 1, mf);
-	return mf;
+	fp = fopen(filename, openmode);
+	if (fp == NULL) {
+		return NULL;
+	}
+
+	mfp = MR_GC_NEW(MR_MercuryFile);
+	MR_mercuryfile_init(fp, filename, 1, mfp);
+	return mfp;
 }
 
 ").
@@ -3586,7 +3552,6 @@
 
 ").
 
-
 :- pred throw_io_error(string::in) is erroneous.
 :- pragma export(throw_io_error(in), "ML_throw_io_error").
 throw_io_error(Message) :- throw(io_error(Message)).
@@ -3594,19 +3559,21 @@
 :- pragma foreign_code("C", "
 
 void
-mercury_io_error(MercuryFile* mf, const char *format, ...)
+mercury_io_error(MR_MercuryFilePtr mfp, const char *format, ...)
 {
 	va_list args;
 	char message[5000];
+	char fdmsg[5000];
 	MR_ConstString message_as_mercury_string;
 
-	/* the `mf' parameter is currently not used */
-
 	/* format the error message using vsprintf() */
 	va_start(args, format);
 	vsprintf(message, format, args);
 	va_end(args);
 
+	snprintf(fdmsg, 5000, "" (fd %d)"", MR_fp_to_fileno(MR_mf_file(*mfp)));
+	strncat(message, fdmsg, 5000);
+
 	/* copy the error message to a Mercury string */
 	MR_restore_registers(); /* for MR_hp */
 	MR_make_aligned_string_copy(message_as_mercury_string, message);
@@ -3621,9 +3588,9 @@
 :- pragma foreign_code("C", "
 
 void
-mercury_output_error(MercuryFile *mf)
+mercury_output_error(MR_MercuryFilePtr mfp)
 {
-	mercury_io_error(mf, ""error writing to output file: %s"",
+	mercury_io_error(mfp, ""error writing to output file: %s"",
 		strerror(errno));
 }
 
@@ -3632,14 +3599,14 @@
 :- pragma foreign_code("C", "
 
 void
-mercury_print_string(MercuryFile* mf, const char *s)
+mercury_print_string(MR_MercuryFilePtr mfp, const char *s)
 {
-	if (ML_fprintf(mf, ""%s"", s) < 0) {
-		mercury_output_error(mf);
+	if (ML_fprintf(mfp, ""%s"", s) < 0) {
+		mercury_output_error(mfp);
 	}
 	while (*s) {
 		if (*s++ == '\\n') {
-			MR_line_number(*mf)++;
+			MR_mf_line_number(*mfp)++;
 		}
 	}
 }
@@ -3667,10 +3634,10 @@
 :- pragma foreign_code("C", "
 
 void
-mercury_print_binary_string(MercuryFile* mf, const char *s)
+mercury_print_binary_string(MR_MercuryFilePtr mfp, const char *s)
 {
-	if (ML_fprintf(mf, ""%s"", s) < 0) {
-		mercury_output_error(mf);
+	if (ML_fprintf(mfp, ""%s"", s) < 0) {
+		mercury_output_error(mfp);
 	}
 }
 
@@ -3679,18 +3646,17 @@
 :- pragma foreign_code("C", "
 
 int
-mercury_getc(MercuryFile* mf)
+mercury_getc(MR_MercuryFilePtr mfp)
 {
-	int c = MR_GETCH(*mf);
+	int c = MR_GETCH(*mfp);
 	if (c == '\\n') {
-		MR_line_number(*mf)++;
+		MR_mf_line_number(*mfp)++;
 	}
 	return c;
 }
 
 ").
 
-
 :- pragma foreign_code("MC++", "
 
 static void
@@ -3718,7 +3684,6 @@
 
 ").
 
-
 %------------------------------------------------------------------------------%
 %------------------------------------------------------------------------------%
 
@@ -3791,10 +3756,13 @@
 	return EOF;
 }
 
-static const MercuryFile MR_closed_stream = {
+static const MR_MercuryFile MR_closed_stream = {
 	/* stream_type	= */	MR_USER_STREAM,
 	/* stream_info	= */	{ NULL },
 	/* line_number	= */	0,
+	/* fd		= */	-1,
+	/* id		= */	-1,
+	/* name		= */	""<closed stream>"",
 
 	/* close	= */	ME_closed_stream_close,
 	/* read		= */	ME_closed_stream_read,
@@ -3810,19 +3778,19 @@
 #endif /* MR_NEW_MERCURYFILE_STRUCT */
 
 void
-mercury_close(MercuryFile* mf)
+mercury_close(MR_MercuryFilePtr mfp)
 {
-	if (mf == &mercury_stdin ||
-	    mf == &mercury_stdout ||
-	    mf == &mercury_stderr ||
-	    mf == &mercury_stdin_binary ||
-	    mf == &mercury_stdout_binary)
+	if (mfp == &mercury_stdin ||
+	    mfp == &mercury_stdout ||
+	    mfp == &mercury_stderr ||
+	    mfp == &mercury_stdin_binary ||
+	    mfp == &mercury_stdout_binary)
 	{
-		mercury_io_error(mf,
+		mercury_io_error(mfp,
 			""attempt to close stdin, stdout or stderr"");	
 	} else {
-		if (MR_CLOSE(*mf) < 0) {
-			mercury_io_error(mf, ""error closing file: %s"",
+		if (MR_CLOSE(*mfp) < 0) {
+			mercury_io_error(mfp, ""error closing file: %s"",
 				strerror(errno));
 		}
 
@@ -3839,7 +3807,7 @@
 		** gcc 2.95.2 barfs on `*mf = MR_closed_stream;'
 		** so we use MR_memcpy() instead.
 		*/
-		MR_memcpy(mf, &MR_closed_stream, sizeof(*mf));
+		MR_memcpy(mfp, &MR_closed_stream, sizeof(*mfp));
 
 /*
 ** XXX it would be nice to have an autoconf check
@@ -3850,7 +3818,7 @@
 
 /****
 #elif defined(HAVE_FOPENCOOKIE)
-		MR_file(*mf) = MR_closed_file;
+		MR_mf_file(*mfp) = MR_closed_file;
 ****/
 
 #else
@@ -3867,7 +3835,7 @@
 		** accessing closed files traps immediately rather than
 		** causing problems at some later point.
 		*/
-		MR_mercuryfile_init(NULL, 0, mf);
+		MR_mercuryfile_init(NULL, ""<closed file>"", 0, mfp);
 
 #endif /* ! MR_NEW_MERCURYFILE_STRUCT */
 
@@ -3880,7 +3848,7 @@
 		** io_streams, since the io__stream type is defined
 		** as a foreign_type.
 		*/
-  		MR_GC_free(mf);
+  		MR_GC_free(mfp);
 #endif /* !MR_CONSERVATIVE_GC */
 
 	}
@@ -3909,17 +3877,16 @@
 
 ").
 
-
 :- pragma foreign_code("C", "
 
 int
-ML_fprintf(MercuryFile* mf, const char *format, ...)
+ML_fprintf(MR_MercuryFilePtr mfp, const char *format, ...)
 {
 	int rc;
 	va_list args;
 
 	va_start(args, format);
-	rc = MR_VFPRINTF(*mf, format, args);
+	rc = MR_VFPRINTF(*mfp, format, args);
 	va_end(args);
 
 	return rc;
@@ -3933,40 +3900,39 @@
 	io__read_char_code(File::in, CharCode::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	CharCode = mercury_getc((MercuryFile *) File);
+	CharCode = mercury_getc(File);
 	MR_update_io(IO0, IO);
 ").
 
 :- pragma foreign_proc("C", 
-	io__putback_char(File::in, Character::in, IO0::di, IO::uo),
+	io__putback_char(Stream::in, Character::in, IO0::di, IO::uo),
 		[may_call_mercury, promise_pure, tabled_for_io],
 "{
-	MercuryFile* mf = (MercuryFile *) File;
 	if (Character == '\\n') {
-		MR_line_number(*mf)--;
+		MR_mf_line_number(*Stream)--;
 	}
 	/* XXX should work even if ungetc() fails */
-	if (MR_UNGETCH(*mf, Character) == EOF) {
-		mercury_io_error(mf, ""io__putback_char: ungetc failed"");
+	if (MR_UNGETCH(*Stream, Character) == EOF) {
+		mercury_io_error(Stream, ""io__putback_char: ungetc failed"");
 	}
 	MR_update_io(IO0, IO);
 }").
 
 :- pragma foreign_proc("C",
-	io__putback_byte(File::in, Character::in, IO0::di, IO::uo),
+	io__putback_byte(Stream::in, Character::in, IO0::di, IO::uo),
 		[may_call_mercury, promise_pure, tabled_for_io],
 "{
-	MercuryFile* mf = (MercuryFile *) File;
 	/* XXX should work even if ungetc() fails */
-	if (MR_UNGETCH(*mf, Character) == EOF) {
-		mercury_io_error(mf, ""io__putback_byte: ungetc failed"");
+	if (MR_UNGETCH(*Stream, Character) == EOF) {
+		mercury_io_error(Stream, ""io__putback_byte: ungetc failed"");
 	}
 	MR_update_io(IO0, IO);
 }").
 
 :- pragma foreign_proc("MC++", 
 	io__read_char_code(File::in, CharCode::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure], "
+	[will_not_call_mercury, promise_pure],
+"
 	MR_MercuryFile mf = ML_DownCast(MR_MercuryFile, 
 		MR_word_to_c_pointer(File));
 	CharCode = mercury_getc(mf);
@@ -3975,8 +3941,8 @@
 
 :- pragma foreign_proc("MC++", 
 	io__putback_char(File::in, Character::in, IO0::di, IO::uo),
-		[may_call_mercury, promise_pure], "{
-
+	[may_call_mercury, promise_pure],
+"{
 	MR_MercuryFile mf = ML_DownCast(MR_MercuryFile,
 		MR_word_to_c_pointer(File));
 	if (Character == '\\n') {
@@ -3988,8 +3954,8 @@
 
 :- pragma foreign_proc("MC++",
 	io__putback_byte(File::in, _Character::in, IO0::di, IO::uo),
-		[may_call_mercury, promise_pure], "{
-
+	[may_call_mercury, promise_pure],
+"{
 	MR_MercuryFile mf = ML_DownCast(MR_MercuryFile, 
 		MR_word_to_c_pointer(File));
 	mf->stream->Seek(-1, System::IO::SeekOrigin::Current);
@@ -4029,7 +3995,7 @@
 		mercury_output_error(mercury_current_text_output);
 	}
 	if (Character == '\\n') {
-		MR_line_number(*mercury_current_text_output)++;
+		MR_mf_line_number(*mercury_current_text_output)++;
 	}
 	MR_update_io(IO0, IO);
 ").
@@ -4227,17 +4193,15 @@
 :- pragma foreign_proc("C",
 	io__seek_binary_2(Stream::in, Flag::in, Off::in,
 		IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io,
-			thread_safe],
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "{
 	static const int seek_flags[] = { SEEK_SET, SEEK_CUR, SEEK_END };
-	MercuryFile *stream = (MercuryFile *) Stream;
 	/* XXX should check for failure */
 	/* XXX should also check if the stream is seekable */
-	if (MR_IS_FILE_STREAM(*stream)) {
-		fseek(MR_file(*stream), Off, seek_flags[Flag]);
+	if (MR_IS_FILE_STREAM(*Stream)) {
+		fseek(MR_mf_file(*Stream), Off, seek_flags[Flag]);
 	} else {
-		mercury_io_error(stream,
+		mercury_io_error(Stream,
 				""io__seek_binary_2: unseekable stream"");
 	}
 	MR_update_io(IO0, IO);
@@ -4246,16 +4210,14 @@
 :- pragma foreign_proc("C",
 	io__binary_stream_offset(Stream::in, Offset::out,
 		IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io,
-			thread_safe],
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "{
-	MercuryFile *stream = (MercuryFile *) Stream;
 	/* XXX should check for failure */
 	/* XXX should check if the stream is tellable */
-	if (MR_IS_FILE_STREAM(*stream)) {
-		Offset = ftell(MR_file(*stream));
+	if (MR_IS_FILE_STREAM(*Stream)) {
+		Offset = ftell(MR_mf_file(*Stream));
 	} else {
-		mercury_io_error(stream,
+		mercury_io_error(Stream,
 			""io__binary_stream_offset: untellable stream"");
 	}
 	MR_update_io(IO0, IO);
@@ -4277,8 +4239,7 @@
 	io__write_string(Stream::in, Message::in, IO0::di, IO::uo),
 		[may_call_mercury, promise_pure, tabled_for_io, thread_safe], 
 "{
-	MercuryFile *stream = (MercuryFile *) Stream;
-	mercury_print_string(stream, Message);
+	mercury_print_string(Stream, Message);
 	MR_update_io(IO0, IO);
 }").
 
@@ -4286,12 +4247,11 @@
 	io__write_char(Stream::in, Character::in, IO0::di, IO::uo),
 		[may_call_mercury, promise_pure, tabled_for_io, thread_safe], 
 "{
-	MercuryFile *stream = (MercuryFile *) Stream;
-	if (MR_PUTCH(*stream, Character) < 0) {
-		mercury_output_error(stream);
+	if (MR_PUTCH(*Stream, Character) < 0) {
+		mercury_output_error(Stream);
 	}
 	if (Character == '\\n') {
-		MR_line_number(*stream)++;
+		MR_mf_line_number(*Stream)++;
 	}
 	MR_update_io(IO0, IO);
 }").
@@ -4300,9 +4260,8 @@
 	io__write_int(Stream::in, Val::in, IO0::di, IO::uo),
 		[may_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "{
-	MercuryFile *stream = (MercuryFile *) Stream;
-	if (ML_fprintf(stream, ""%ld"", (long) Val) < 0) {
-		mercury_output_error(stream);
+	if (ML_fprintf(Stream, ""%ld"", (long) Val) < 0) {
+		mercury_output_error(Stream);
 	}
 	MR_update_io(IO0, IO);
 }").
@@ -4311,9 +4270,8 @@
 	io__write_float(Stream::in, Val::in, IO0::di, IO::uo),
 		[may_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "{
-	MercuryFile *stream = (MercuryFile *) Stream;
-	if (ML_fprintf(stream, ""%#.15g"", Val) < 0) {
-		mercury_output_error(stream);
+	if (ML_fprintf(Stream, ""%#.15g"", Val) < 0) {
+		mercury_output_error(Stream);
 	}
 	MR_update_io(IO0, IO);
 }").
@@ -4322,10 +4280,9 @@
 	io__write_byte(Stream::in, Byte::in, IO0::di, IO::uo),
 		[may_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "{
-	MercuryFile *stream = (MercuryFile *) Stream;
 	/* call putc with a strictly non-negative byte-sized integer */
-	if (MR_PUTCH(*stream, (int) ((unsigned char) Byte)) < 0) {
-		mercury_output_error(stream);
+	if (MR_PUTCH(*Stream, (int) ((unsigned char) Byte)) < 0) {
+		mercury_output_error(Stream);
 	}
 	MR_update_io(IO0, IO);
 }").
@@ -4334,8 +4291,7 @@
 	io__write_bytes(Stream::in, Message::in, IO0::di, IO::uo),
 		[may_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "{
-	MercuryFile *stream = (MercuryFile *) Stream;
-	mercury_print_binary_string(stream, Message);
+	mercury_print_binary_string(Stream, Message);
 	MR_update_io(IO0, IO);
 }").
 
@@ -4343,9 +4299,8 @@
 	io__flush_output(Stream::in, IO0::di, IO::uo),
 		[may_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "{
-	MercuryFile *stream = (MercuryFile *) Stream;
-	if (MR_FLUSH(*stream) < 0) {
-		mercury_output_error(stream);
+	if (MR_FLUSH(*Stream) < 0) {
+		mercury_output_error(Stream);
 	}
 	MR_update_io(IO0, IO);
 }").
@@ -4354,9 +4309,8 @@
 	io__flush_binary_output(Stream::in, IO0::di, IO::uo),
 		[may_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "{
-	MercuryFile *stream = (MercuryFile *) Stream;
-	if (MR_FLUSH(*stream) < 0) {
-		mercury_output_error(stream);
+	if (MR_FLUSH(*Stream) < 0) {
+		mercury_output_error(Stream);
 	}
 	MR_update_io(IO0, IO);
 }").
@@ -4503,46 +4457,41 @@
 
 :- pragma foreign_proc("C",
 	io__stdin_stream(Stream::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io,
-			thread_safe],
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "
-	Stream = (MR_Word) &mercury_stdin;
+	Stream = &mercury_stdin;
 	MR_update_io(IO0, IO);
 ").
 
 :- pragma foreign_proc("C",
 	io__stdout_stream(Stream::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io,
-			thread_safe],
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "
-	Stream = (MR_Word) &mercury_stdout;
+	Stream = &mercury_stdout;
 	MR_update_io(IO0, IO);
 ").
 
 :- pragma foreign_proc("C",
 	io__stderr_stream(Stream::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io,
-			thread_safe],
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "
-	Stream = (MR_Word) &mercury_stderr;
+	Stream = &mercury_stderr;
 	MR_update_io(IO0, IO);
 ").
 
 :- pragma foreign_proc("C",
 	io__stdin_binary_stream(Stream::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io,
-			thread_safe],
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "
-	Stream = (MR_Word) &mercury_stdin_binary;
+	Stream = &mercury_stdin_binary;
 	MR_update_io(IO0, IO);
 ").
 
 :- pragma foreign_proc("C",
 	io__stdout_binary_stream(Stream::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io,
-			thread_safe],
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "
-	Stream = (MR_Word) &mercury_stdout_binary;
+	Stream = &mercury_stdout_binary;
 	MR_update_io(IO0, IO);
 ").
 
@@ -4550,7 +4499,7 @@
 	io__input_stream(Stream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	Stream = (MR_Word) mercury_current_text_input;
+	Stream = mercury_current_text_input;
 	MR_update_io(IO0, IO);
 ").
 
@@ -4558,7 +4507,7 @@
 	io__output_stream(Stream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	Stream = (MR_Word) mercury_current_text_output;
+	Stream = mercury_current_text_output;
 	MR_update_io(IO0, IO);
 ").
 
@@ -4566,7 +4515,7 @@
 	io__binary_input_stream(Stream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	Stream = (MR_Word) mercury_current_binary_input;
+	Stream = mercury_current_binary_input;
 	MR_update_io(IO0, IO);
 ").
 
@@ -4574,7 +4523,7 @@
 	io__binary_output_stream(Stream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	Stream = (MR_Word) mercury_current_binary_output;
+	Stream = mercury_current_binary_output;
 	MR_update_io(IO0, IO);
 ").
 
@@ -4582,7 +4531,7 @@
 	io__get_line_number(LineNum::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	LineNum = MR_line_number(*mercury_current_text_input);
+	LineNum = MR_mf_line_number(*mercury_current_text_input);
 	MR_update_io(IO0, IO);
 ").
 
@@ -4590,8 +4539,7 @@
 	io__get_line_number(Stream::in, LineNum::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "{
-	MercuryFile *stream = (MercuryFile *) Stream;
-	LineNum = MR_line_number(*stream);
+	LineNum = MR_mf_line_number(*Stream);
 	MR_update_io(IO0, IO);
 }").
 
@@ -4599,7 +4547,7 @@
 	io__set_line_number(LineNum::in, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	MR_line_number(*mercury_current_text_input) = LineNum;
+	MR_mf_line_number(*mercury_current_text_input) = LineNum;
 	MR_update_io(IO0, IO);
 ").
 
@@ -4607,8 +4555,7 @@
 	io__set_line_number(Stream::in, LineNum::in, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "{
-	MercuryFile *stream = (MercuryFile *) Stream;
-	MR_line_number(*stream) = LineNum;
+	MR_mf_line_number(*Stream) = LineNum;
 	MR_update_io(IO0, IO);
 }").
 
@@ -4616,7 +4563,7 @@
 	io__get_output_line_number(LineNum::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	LineNum = MR_line_number(*mercury_current_text_output);
+	LineNum = MR_mf_line_number(*mercury_current_text_output);
 	MR_update_io(IO0, IO);
 ").
 	
@@ -4624,8 +4571,7 @@
 	io__get_output_line_number(Stream::in, LineNum::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "{
-	MercuryFile *stream = (MercuryFile *) Stream;
-	LineNum = MR_line_number(*stream);
+	LineNum = MR_mf_line_number(*Stream);
 	MR_update_io(IO0, IO);
 }").
 
@@ -4633,7 +4579,7 @@
 	io__set_output_line_number(LineNum::in, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	MR_line_number(*mercury_current_text_output) = LineNum;
+	MR_mf_line_number(*mercury_current_text_output) = LineNum;
 	MR_update_io(IO0, IO);
 ").
 
@@ -4642,8 +4588,7 @@
 		IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "{
-	MercuryFile *stream = (MercuryFile *) Stream;
-	MR_line_number(*stream) = LineNum;
+	MR_mf_line_number(*Stream) = LineNum;
 	MR_update_io(IO0, IO);
 }").
 
@@ -4651,7 +4596,7 @@
 	io__current_input_stream(OutStream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	OutStream = (MR_Word) mercury_current_text_input;
+	OutStream = mercury_current_text_input;
 	MR_update_io(IO0, IO);
 ").
 
@@ -4659,7 +4604,7 @@
 	io__current_output_stream(OutStream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	OutStream = (MR_Word) mercury_current_text_output;
+	OutStream = mercury_current_text_output;
 	MR_update_io(IO0, IO);
 ").
 
@@ -4667,7 +4612,7 @@
 	io__current_binary_input_stream(OutStream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	OutStream = (MR_Word) mercury_current_binary_input;
+	OutStream = mercury_current_binary_input;
 	MR_update_io(IO0, IO);
 ").
 
@@ -4675,7 +4620,7 @@
 	io__current_binary_output_stream(OutStream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	OutStream = (MR_Word) mercury_current_binary_output;
+	OutStream = mercury_current_binary_output;
 	MR_update_io(IO0, IO);
 ").
 
@@ -4687,8 +4632,8 @@
 		IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	OutStream = (MR_Word) mercury_current_text_input;
-	mercury_current_text_input = (MercuryFile *) NewStream;
+	OutStream = mercury_current_text_input;
+	mercury_current_text_input = NewStream;
 	MR_update_io(IO0, IO);
 ").
 
@@ -4697,8 +4642,8 @@
 		IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	OutStream = (MR_Word) mercury_current_text_output;
-	mercury_current_text_output = (MercuryFile *) NewStream;
+	OutStream = mercury_current_text_output;
+	mercury_current_text_output = NewStream;
 	MR_update_io(IO0, IO);
 ").
 
@@ -4707,8 +4652,8 @@
 		IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	OutStream = (MR_Word) mercury_current_binary_input;
-	mercury_current_binary_input = (MercuryFile *) NewStream;
+	OutStream = mercury_current_binary_input;
+	mercury_current_binary_input = NewStream;
 	MR_update_io(IO0, IO);
 ").
 
@@ -4717,15 +4662,14 @@
 		IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	OutStream = (MR_Word) mercury_current_binary_output;
-	mercury_current_binary_output = (MercuryFile *) NewStream;
+	OutStream = mercury_current_binary_output;
+	mercury_current_binary_output = NewStream;
 	MR_update_io(IO0, IO);
 ").
 
 :- pragma foreign_proc("MC++",
 	io__stdin_stream(Stream::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, thread_safe,
-			tabled_for_io],
+	[will_not_call_mercury, promise_pure, thread_safe, tabled_for_io],
 "
 	MR_c_pointer_to_word(Stream, mercury_stdin);
 	MR_update_io(IO0, IO);
@@ -4733,8 +4677,7 @@
 
 :- pragma foreign_proc("MC++",
 	io__stdout_stream(Stream::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, thread_safe,
-			tabled_for_io],
+	[will_not_call_mercury, promise_pure, thread_safe, tabled_for_io],
 "
 	MR_c_pointer_to_word(Stream, mercury_stdout);
 	MR_update_io(IO0, IO);
@@ -4742,8 +4685,7 @@
 
 :- pragma foreign_proc("MC++",
 	io__stderr_stream(Stream::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, thread_safe,
-			tabled_for_io],
+	[will_not_call_mercury, promise_pure, thread_safe, tabled_for_io],
 "
 	MR_c_pointer_to_word(Stream, mercury_stderr);
 	MR_update_io(IO0, IO);
@@ -4751,8 +4693,7 @@
 
 :- pragma foreign_proc("MC++",
 	io__stdin_binary_stream(Stream::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, thread_safe,
-			tabled_for_io],
+	[will_not_call_mercury, promise_pure, thread_safe, tabled_for_io],
 "
 	MR_c_pointer_to_word(Stream, mercury_stdin_binary);
 	MR_update_io(IO0, IO);
@@ -4760,8 +4701,7 @@
 
 :- pragma foreign_proc("MC++",
 	io__stdout_binary_stream(Stream::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, thread_safe,
-			tabled_for_io],
+	[will_not_call_mercury, promise_pure, thread_safe, tabled_for_io],
 "
 	MR_c_pointer_to_word(Stream, mercury_stdout_binary);
 	MR_update_io(IO0, IO);
@@ -4837,14 +4777,16 @@
 
 :- pragma foreign_proc("MC++",
 	io__get_output_line_number(LineNum::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io], "
+	[will_not_call_mercury, promise_pure, tabled_for_io],
+"
 	LineNum = mercury_current_text_output->line_number;
 	MR_update_io(IO0, IO);
 ").
 
 :- pragma foreign_proc("MC++",
 	io__get_output_line_number(Stream::in, LineNum::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io], "{
+	[will_not_call_mercury, promise_pure, tabled_for_io],
+"{
 	MR_MercuryFile stream = ML_DownCast(MR_MercuryFile, 
 		MR_word_to_c_pointer(Stream));
 	LineNum = stream->line_number;
@@ -4853,14 +4795,16 @@
 
 :- pragma foreign_proc("MC++",
 	io__set_output_line_number(LineNum::in, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io], "
+	[will_not_call_mercury, promise_pure, tabled_for_io],
+"
 	mercury_current_text_output->line_number = LineNum;
 	MR_update_io(IO0, IO);
 ").
 
 :- pragma foreign_proc("MC++",
 	io__set_output_line_number(Stream::in, LineNum::in, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io], "{
+	[will_not_call_mercury, promise_pure, tabled_for_io],
+"{
 	MR_MercuryFile stream = ML_DownCast(MR_MercuryFile, 
 		MR_word_to_c_pointer(Stream));
 	stream->line_number = LineNum;
@@ -4872,7 +4816,8 @@
 %	Returns the previous stream.
 :- pragma foreign_proc("MC++",
 	io__set_input_stream(NewStream::in, OutStream::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io], "
+	[will_not_call_mercury, promise_pure, tabled_for_io],
+"
 	MR_c_pointer_to_word(OutStream, mercury_current_text_input);
 	mercury_current_text_input = 
 		ML_DownCast(MR_MercuryFile, MR_word_to_c_pointer(NewStream));
@@ -4881,7 +4826,8 @@
 
 :- pragma foreign_proc("MC++",
 	io__set_output_stream(NewStream::in, OutStream::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io], "
+	[will_not_call_mercury, promise_pure, tabled_for_io],
+"
 	MR_c_pointer_to_word(OutStream, mercury_current_text_output);
 	mercury_current_text_output = 
 		ML_DownCast(MR_MercuryFile, MR_word_to_c_pointer(NewStream));
@@ -4891,7 +4837,8 @@
 :- pragma foreign_proc("MC++",
 	io__set_binary_input_stream(NewStream::in, OutStream::out,
 		IO0::di, IO::uo), 
-		[will_not_call_mercury, promise_pure, tabled_for_io], "
+	[will_not_call_mercury, promise_pure, tabled_for_io],
+"
 	MR_c_pointer_to_word(OutStream, mercury_current_binary_input);
 	mercury_current_binary_input = 
 		ML_DownCast(MR_MercuryFile, MR_word_to_c_pointer(NewStream));
@@ -4901,7 +4848,8 @@
 :- pragma foreign_proc("MC++",
 	io__set_binary_output_stream(NewStream::in, OutStream::out,
 		IO0::di, IO::uo), 
-		[will_not_call_mercury, promise_pure, tabled_for_io], "
+	[will_not_call_mercury, promise_pure, tabled_for_io],
+"
 	MR_c_pointer_to_word(OutStream, mercury_current_binary_output);
 	mercury_current_binary_output = 
 		ML_DownCast(MR_MercuryFile, MR_word_to_c_pointer(NewStream));
@@ -5041,19 +4989,17 @@
 :- pragma foreign_proc("C",
 	io__do_open(FileName::in, Mode::in, ResultCode::out,
 		Stream::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io,
-			thread_safe],
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "
-	Stream = (MR_Word) mercury_open(FileName, Mode);
-	ResultCode = (Stream ? 0 : -1);
+	Stream = mercury_open(FileName, Mode);
+	ResultCode = ((Stream != NULL) ? 0 : -1);
 	MR_update_io(IO0, IO);
 ").
 
 :- pragma foreign_proc("MC++",
 	io__do_open(FileName::in, Mode::in, ResultCode::out,
 		Stream::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io,
-			thread_safe],
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "
 	MR_MercuryFile mf = mercury_open(FileName, Mode);
 	MR_c_pointer_to_word(Stream, mf);
@@ -5067,31 +5013,31 @@
 	{ private_builtin__sorry("io__do_open") }.
 
 io__close_input(Stream) -->
-	io__delete_stream_name(Stream),
 	io__close_stream(Stream).
 
 io__close_output(Stream) -->
-	io__delete_stream_name(Stream),
 	io__close_stream(Stream).
 
 io__close_binary_input(Stream) -->
-	io__delete_stream_name(Stream),
 	io__close_stream(Stream).
 
 io__close_binary_output(Stream) -->
-	io__delete_stream_name(Stream),
 	io__close_stream(Stream).
 
 :- pred io__close_stream(stream::in, io__state::di, io__state::uo) is det.
 
-:- pragma foreign_proc("C", io__close_stream(Stream::in, IO0::di, IO::uo),
-		[may_call_mercury, promise_pure, tabled_for_io, thread_safe], "
-	mercury_close((MercuryFile *) Stream);
+:- pragma foreign_proc("C",
+	io__close_stream(Stream::in, IO0::di, IO::uo),
+	[may_call_mercury, promise_pure, tabled_for_io, thread_safe],
+"
+	mercury_close(Stream);
 	MR_update_io(IO0, IO);
 ").
 
-:- pragma foreign_proc("MC++", io__close_stream(Stream::in, IO0::di, IO::uo),
-		[may_call_mercury, promise_pure, tabled_for_io, thread_safe], "
+:- pragma foreign_proc("MC++",
+	io__close_stream(Stream::in, IO0::di, IO::uo),
+	[may_call_mercury, promise_pure, tabled_for_io, thread_safe],
+"
 	MR_MercuryFile mf = ML_DownCast(MR_MercuryFile, 
 		MR_word_to_c_pointer(Stream));
 	mercury_close(mf);
@@ -5107,8 +5053,7 @@
 
 :- pragma foreign_proc("C",
 	io__progname(DefaultProgname::in, PrognameOut::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io,
-			thread_safe],
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "
 	if (MR_progname) {
 		/*
@@ -5130,15 +5075,15 @@
 
 :- pragma foreign_proc("C",
 	io__command_line_arguments(Args::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io,
-			thread_safe],
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "
 	/* convert mercury_argv from a vector to a list */
-	{ int i = mercury_argc;
+	{
+		int i = mercury_argc;
 	  Args = MR_list_empty_msg(MR_PROC_LABEL);
 	  while (--i >= 0) {
-		Args = MR_list_cons_msg((MR_Word) mercury_argv[i], Args,
-			MR_PROC_LABEL);
+			Args = MR_list_cons_msg((MR_Word) mercury_argv[i],
+				Args, MR_PROC_LABEL);
 	  }
 	}
 	MR_update_io(IO0, IO);
@@ -5182,7 +5127,6 @@
 	MR_update_io(IO0, IO);
 ").
 
-
 io__progname(DefaultProgName::in, ProgName::out, IO::di, IO::uo) :-
 	% This is a fall-back for back-ends which don't support the
 	% C interface.
@@ -5241,8 +5185,8 @@
 
 :- pragma foreign_proc("MC++",
 	io__command_line_arguments(Args::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io,
-			thread_safe], "
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
+"
 #ifdef MR_HIGHLEVEL_DATA
 	mercury::runtime::Errors::SORRY(""io__command_line_arguments"");
 #else
@@ -5316,27 +5260,31 @@
 /* io__getenv and io__putenv, from io.m */
 
 :- pragma promise_semipure(io__getenv/2).
-:- pragma foreign_proc("C", io__getenv(Var::in, Value::out),
+:- pragma foreign_proc("C",
+	io__getenv(Var::in, Value::out),
 		[will_not_call_mercury, tabled_for_io],
 "{
 	Value = getenv(Var);
 	SUCCESS_INDICATOR = (Value != 0);
 }").
 
-:- pragma foreign_proc("C", io__putenv(VarAndValue::in),
+:- pragma foreign_proc("C",
+	io__putenv(VarAndValue::in),
 		[will_not_call_mercury, tabled_for_io],
 "
 	SUCCESS_INDICATOR = (putenv(VarAndValue) == 0);
 ").
 
-:- pragma foreign_proc("MC++", io__getenv(Var::in, Value::out),
+:- pragma foreign_proc("MC++",
+	io__getenv(Var::in, Value::out),
 		[will_not_call_mercury, tabled_for_io],
 "{
 	Value = System::Environment::GetEnvironmentVariable(Var);
 	SUCCESS_INDICATOR = (Value != 0);
 }").
 
-:- pragma foreign_proc("MC++", io__putenv(_VarAndValue::in),
+:- pragma foreign_proc("MC++",
+	io__putenv(_VarAndValue::in),
 		[will_not_call_mercury, tabled_for_io],
 "
 	mercury::runtime::Errors::SORRY(
@@ -5460,7 +5408,8 @@
 	} while (fd == -1 && errno == EEXIST &&
 		num_tries < MAX_TEMPNAME_TRIES);
 	if (fd == -1) {
-		ML_maybe_make_err_msg(MR_TRUE, ""error opening temporary file: "",
+		ML_maybe_make_err_msg(MR_TRUE,
+			""error opening temporary file: "",
 			MR_PROC_LABEL, ErrorMessage);
 		Error = -1;
 	}  else {
@@ -5537,8 +5486,7 @@
 :- pragma foreign_proc("C",
 	io__remove_file_2(FileName::in, RetVal::out, RetStr::out,
 		IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io,
-			thread_safe],
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "{
 	RetVal = remove(FileName);
 	ML_maybe_make_err_msg(RetVal != 0, ""remove failed: "",
@@ -5550,8 +5498,7 @@
 :- pragma foreign_proc("MC++",
 	io__remove_file_2(FileName::in, RetVal::out, RetStr::out,
 		IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io,
-			thread_safe],
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "{
 	mercury::runtime::Errors::SORRY(""foreign code for this function"");
 	System::IO::File::Delete(FileName);
@@ -5580,8 +5527,7 @@
 :- pragma foreign_proc("C",
 		io__rename_file_2(OldFileName::in, NewFileName::in,
 			RetVal::out, RetStr::out, IO0::di, IO::uo),
-		[will_not_call_mercury, promise_pure, tabled_for_io,
-			thread_safe],
+	[will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "{
 	RetVal = rename(OldFileName, NewFileName);
 	ML_maybe_make_err_msg(RetVal != 0, ""rename failed: "",
Index: library/table_builtin.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/table_builtin.m,v
retrieving revision 1.22
diff -u -b -r1.22 table_builtin.m
--- library/table_builtin.m	12 Sep 2002 10:00:51 -0000	1.22
+++ library/table_builtin.m	13 Sep 2002 03:44:11 -0000
@@ -1138,8 +1138,8 @@
 :- impure pred table_create_ans_block(ml_subgoal_table_node::in, int::in,
 	ml_answer_block::out) is det.
 
-	% Report statistics on the operation of the tabling system to stderr.
-:- impure pred table_report_statistics is det.
+	% Return statistics on the operation of the tabling system.
+:- impure pred table_report_statistics(string::out) is det.
 
 %-----------------------------------------------------------------------------%
 
@@ -1393,8 +1393,14 @@
 	error(Message).
 
 :- pragma foreign_proc("C",
-	table_report_statistics, [will_not_call_mercury], "
-	MR_table_report_statistics(stderr);
+	table_report_statistics(Stats::out),
+	[will_not_call_mercury],
+"
+	MR_String	Stats0;
+
+	Stats0 = MR_table_report_statistics();
+	MR_make_aligned_string_copy(Stats, Stats0);
+	MR_free(Stats0);
 ").
 
 
@@ -1530,7 +1536,7 @@
 	impure private_builtin__imp,
 	private_builtin__sorry("table_create_ans_block").
 
-table_report_statistics :-
+table_report_statistics(_) :-
 	% This version is only used for back-ends for which there is no
 	% matching foreign_proc version.
 	impure private_builtin__imp,
cvs diff: Diffing profiler
cvs diff: Diffing robdd
cvs diff: Diffing runtime
Index: runtime/RESERVED_MACRO_NAMES
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/RESERVED_MACRO_NAMES,v
retrieving revision 1.13
diff -u -b -r1.13 RESERVED_MACRO_NAMES
--- runtime/RESERVED_MACRO_NAMES	24 Sep 2002 06:55:26 -0000	1.13
+++ runtime/RESERVED_MACRO_NAMES	24 Sep 2002 12:07:31 -0000
@@ -42,3 +42,8 @@
 # headers, so this doesn't cause any name space polution.
 __GNU_LIBRARY__
 #-----------------------------------------------------------------------------#
+# These are defined by mercury_signal.h. The first is defined only temporarily,
+# and the second eliminates gratuitous differences between Linux versions.
+# Neither really pollutes the namespace.
+__KERNEL__
+sigcontext_struct
Index: runtime/mercury.h
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury.h,v
retrieving revision 1.64
diff -u -b -r1.64 mercury.h
--- runtime/mercury.h	12 Sep 2002 10:00:53 -0000	1.64
+++ runtime/mercury.h	24 Sep 2002 23:49:48 -0000
@@ -30,6 +30,7 @@
 #include "mercury_std.h"	/* for the MR_CALL macro (and others) */
 #include "mercury_type_info.h"
 #include "mercury_builtin_types.h" 
+#include "mercury_library_types.h" 
 #include "mercury_ho_call.h"	/* for the `MR_Closure' type */
 #include "mercury_bootstrap.h"
 #include "mercury_memory.h"	/* for memory allocation routines */
@@ -125,7 +126,7 @@
 
 /*
 ** Declare the TypeCtorInfos of the library types that are not already
-** declared in mercury_builtin_types.h
+** declared in mercury_builtin_types.h.
 */
 
 MR_DECLARE_TYPE_CTOR_INFO_STRUCT(
Index: runtime/mercury_builtin_types.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_builtin_types.c,v
retrieving revision 1.3
diff -u -b -r1.3 mercury_builtin_types.c
--- runtime/mercury_builtin_types.c	22 Aug 2002 06:51:18 -0000	1.3
+++ runtime/mercury_builtin_types.c	24 Sep 2002 12:12:53 -0000
@@ -40,6 +40,7 @@
 MR_DEFINE_PROC_STATICS(builtin, pred, 0);
 MR_DEFINE_PROC_STATICS(builtin, func, 0);
 MR_DEFINE_PROC_STATICS(builtin, tuple, 0);
+#ifndef MR_HIGHLEVEL_CODE
 MR_DEFINE_PROC_STATICS(builtin, succip, 0);
 MR_DEFINE_PROC_STATICS(builtin, hp, 0);
 MR_DEFINE_PROC_STATICS(builtin, curfr, 0);
@@ -48,6 +49,7 @@
 MR_DEFINE_PROC_STATICS(builtin, redoip, 0);
 MR_DEFINE_PROC_STATICS(builtin, trailptr, 0);
 MR_DEFINE_PROC_STATICS(builtin, ticket, 0);
+#endif
 MR_DEFINE_PROC_STATICS(private_builtin, heap_pointer, 0);
 MR_DEFINE_PROC_STATICS(private_builtin, type_ctor_info, 1);
 MR_DEFINE_PROC_STATICS(private_builtin, type_info, 1);
Index: runtime/mercury_builtin_types.h
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_builtin_types.h,v
retrieving revision 1.2
diff -u -b -r1.2 mercury_builtin_types.h
--- runtime/mercury_builtin_types.h	12 Sep 2002 10:00:54 -0000	1.2
+++ runtime/mercury_builtin_types.h	24 Sep 2002 12:37:28 -0000
@@ -42,6 +42,24 @@
 	MR_TYPE_CTOR_INFO_NAME(builtin, func, 0));
 MR_DECLARE_TYPE_CTOR_INFO_STRUCT(
 	MR_TYPE_CTOR_INFO_NAME(builtin, tuple, 0));
+#ifndef MR_HIGHLEVEL_CODE
+MR_DECLARE_TYPE_CTOR_INFO_STRUCT(
+	MR_TYPE_CTOR_INFO_NAME(builtin, succip, 0));
+MR_DECLARE_TYPE_CTOR_INFO_STRUCT(
+	MR_TYPE_CTOR_INFO_NAME(builtin, hp, 0));
+MR_DECLARE_TYPE_CTOR_INFO_STRUCT(
+	MR_TYPE_CTOR_INFO_NAME(builtin, curfr, 0));
+MR_DECLARE_TYPE_CTOR_INFO_STRUCT(
+	MR_TYPE_CTOR_INFO_NAME(builtin, maxfr, 0));
+MR_DECLARE_TYPE_CTOR_INFO_STRUCT(
+	MR_TYPE_CTOR_INFO_NAME(builtin, redofr, 0));
+MR_DECLARE_TYPE_CTOR_INFO_STRUCT(
+	MR_TYPE_CTOR_INFO_NAME(builtin, redoip, 0));
+MR_DECLARE_TYPE_CTOR_INFO_STRUCT(
+	MR_TYPE_CTOR_INFO_NAME(builtin, trailptr, 0));
+MR_DECLARE_TYPE_CTOR_INFO_STRUCT(
+	MR_TYPE_CTOR_INFO_NAME(builtin, ticket, 0));
+#endif
 MR_DECLARE_TYPE_CTOR_INFO_STRUCT(
 	MR_TYPE_CTOR_INFO_NAME(private_builtin, heap_pointer, 0));
 MR_DECLARE_TYPE_CTOR_INFO_STRUCT(
Index: runtime/mercury_file.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_file.c,v
retrieving revision 1.4
diff -u -b -r1.4 mercury_file.c
--- runtime/mercury_file.c	13 Feb 2002 09:56:40 -0000	1.4
+++ runtime/mercury_file.c	23 Sep 2002 07:00:38 -0000
@@ -12,113 +12,115 @@
 #include "mercury_file.h"
 #include "mercury_std.h"	/* for MR_assert */
 
-#ifdef MR_NATIVE_GC
-  static int next_id = 0;
-#endif
+static int next_id = 0;
 
 #ifndef MR_NEW_MERCURYFILE_STRUCT
-  void
-  MR_mercuryfile_init(FILE *file, int line_number, MercuryFile *mf)
-  {
-	MR_file(*mf)	    = file;
-	MR_line_number(*mf) = line_number;
-  #ifdef MR_NATIVE_GC
-	mf->id = ++next_id;
-  #endif
-  }
-
-#else /* MR_NEW_MERCURYFILE_STRUCT */
-
-  void
-  MR_mercuryfile_init(FILE *file, int line_number, MercuryFile *mf)
-  {
-	mf->stream_type	= MR_FILE_STREAM;
-	mf->stream_info.file	= file;
-	mf->line_number	= line_number;
-
-	mf->close		= MR_close;
-	mf->read		= MR_read;
-	mf->write		= MR_write;
-
-	mf->flush		= MR_flush;
-	mf->ungetc		= MR_ungetch;
-	mf->getc		= MR_getch;
-	mf->vprintf		= MR_vfprintf;
-	mf->putc		= MR_putch;
-  #ifdef MR_NATIVE_GC
-	mf->id = ++next_id;
-  #endif
-  }
-
-  int
-  MR_getch(MR_StreamInfo *info) 
-  {
-	MR_assert(info != NULL);
-	return getc(info->file);	  
-  }
 
-  int
-  MR_ungetch(MR_StreamInfo *info, int ch)
-  {
-	MR_assert(info != NULL);		
-	return ungetc(ch, info->file);
-  }
+void
+MR_mercuryfile_init(FILE *fp, const char *name, int line_number,
+	MR_MercuryFilePtr mfp)
+{
+	MR_mf_file(*mfp) = fp;
+	MR_mf_fd(*mfp) = MR_fp_to_fileno(fp);
+	MR_mf_line_number(*mfp) = line_number;
+	MR_mf_id(*mfp) = ++next_id;
+	MR_mf_name(*mfp) = name;
+}
 
-  int
-  MR_putch(MR_StreamInfo *info, int ch)
-  {
-	MR_assert(info != NULL);
-	return putc(ch, info->file);
-  }
-
-  int
-  MR_close(MR_StreamInfo *info)
-  {
-	MR_assert(info != NULL);				      
-	return fclose(info->file);
-  }
-  
-  int
-  MR_flush(MR_StreamInfo *info)
-  {
-	MR_assert(info != NULL);				       
-	return fflush(info->file);
-  }
+#else /* MR_NEW_MERCURYFILE_STRUCT */
   
-  int
-  MR_vfprintf(MR_StreamInfo *info, const char *format, va_list ap)
-  {
+void
+MR_mercuryfile_init(FILE *fp, const char *name, int line_number,
+	MR_MercuryFilePtr mfp)
+{
+	mfp->MR_mf_stream_type		= MR_FILE_STREAM;
+	mfp->MR_mf_stream_info.MR_si_file	= fp;
+	mfp->MR_mf_fd			= MR_fp_to_fileno(fp);
+	mfp->MR_mf_id			= ++next_id;
+	mfp->MR_mf_line_number		= line_number;
+	mfp->MR_mf_name			= name;
+
+	mfp->MR_mf_close		= MR_close;
+	mfp->MR_mf_read			= MR_read;
+	mfp->MR_mf_write		= MR_write;
+
+	mfp->MR_mf_flush		= MR_flush;
+	mfp->MR_mf_ungetc		= MR_ungetch;
+	mfp->MR_mf_getc			= MR_getch;
+	mfp->MR_mf_vprintf		= MR_vfprintf;
+	mfp->MR_mf_putc			= MR_putch;
+}
+
+int
+MR_getch(MR_StreamInfo *info) 
+{
+	MR_assert(info != NULL);
+	return getc(info->MR_si_file);	  
+}
+
+int
+MR_ungetch(MR_StreamInfo *info, int ch)
+{
+	MR_assert(info != NULL);		
+	return ungetc(ch, info->MR_si_file);
+}
+
+int
+MR_putch(MR_StreamInfo *info, int ch)
+{
+	MR_assert(info != NULL);
+	return putc(ch, info->MR_si_file);
+}
+
+int
+MR_close(MR_StreamInfo *info)
+{
+	MR_assert(info != NULL);				      
+	return fclose(info->MR_si_file);
+}
+
+int
+MR_flush(MR_StreamInfo *info)
+{
+	MR_assert(info != NULL);				       
+	return fflush(info->MR_si_file);
+}
+
+int
+MR_vfprintf(MR_StreamInfo *info, const char *format, va_list ap)
+{
 	MR_assert(info != NULL);
 	MR_assert(format != NULL);
 
-	return vfprintf(info->file, format, ap);
-  }
+	return vfprintf(info->MR_si_file, format, ap);
+}
   
-  int
-  MR_read(MR_StreamInfo *info, void *buffer, size_t size)
-  { 
+int
+MR_read(MR_StreamInfo *info, void *buffer, size_t size)
+{ 
 	int rc;							       
 	MR_assert(info != NULL);				
-	rc = fread(buffer, sizeof(unsigned char), size, info->file);
+	rc = fread(buffer, sizeof(unsigned char), size, info->MR_si_file);
 
 		/* Handle error/eof special cases */
-	if ( (rc < size) &&  feof(info->file) ) {
+	if ( (rc < size) &&  feof(info->MR_si_file) ) {
 		/* nothing to do */;
-	} else if ( ferror(info->file) ) {
+	} else if ( ferror(info->MR_si_file) ) {
 		rc = -1;
 	}
 
 	return rc;
-  }
+}
 
-  int
-  MR_write(MR_StreamInfo *info, const void *buffer, size_t size)
-  {
+int
+MR_write(MR_StreamInfo *info, const void *buffer, size_t size)
+{
 	int rc;							       
 
 	MR_assert(info != NULL);
-	rc = fwrite(buffer, sizeof(unsigned char), size, info->file);
+	rc = fwrite(buffer, sizeof(unsigned char), size, info->MR_si_file);
 
 	return (rc < size ? -1 : rc);
-  }
+}
+
 #endif /* MR_NEW_MERCURYFILE_STRUCT */
Index: runtime/mercury_file.h
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_file.h,v
retrieving revision 1.1
diff -u -b -r1.1 mercury_file.h
--- runtime/mercury_file.h	11 Aug 2000 16:50:21 -0000	1.1
+++ runtime/mercury_file.h	16 Sep 2002 22:08:10 -0000
@@ -9,13 +9,12 @@
 #include "mercury_library_types.h"
 
 /*
-** Initialise a MercuryFile structure to use the C stdlib FILE *type.
+** Initialise a MR_MercuryFile structure to use the C stdlib FILE *type.
 */
-void MR_mercuryfile_init(FILE *file, int line_number, MercuryFile *mf);
+extern	void	MR_mercuryfile_init(FILE *file, const char *name,
+			int line_number, MR_MercuryFilePtr mfp);
 
 #ifdef MR_NEW_MERCURYFILE_STRUCT
-  #define MR_IS_FILE_STREAM(mf)	( (mf).stream_type == MR_FILE_STREAM )
-
   int MR_getch(MR_StreamInfo *info);
   int MR_putch(MR_StreamInfo *info, int);
   int MR_ungetch(MR_StreamInfo *info, int);
Index: runtime/mercury_init.h
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_init.h,v
retrieving revision 1.39
diff -u -b -r1.39 mercury_init.h
--- runtime/mercury_init.h	21 Aug 2002 11:27:42 -0000	1.39
+++ runtime/mercury_init.h	18 Sep 2002 13:22:07 -0000
@@ -86,6 +86,7 @@
 				   etc. */
 #include "mercury_trace_base.h"	/* for MR_trace_port */
 #include "mercury_type_info.h"	/* for MR_TypeCtorInfo_Struct */
+#include "mercury_library_types.h"	/* for MR_MercuryFilePtr */
 
 #ifdef MR_CONSERVATIVE_GC
   #ifdef MR_MPS_GC
@@ -118,11 +119,11 @@
 extern	void	mercury_init_io(void);
 extern	void	ML_io_init_state(void);
 extern	void	ML_io_finalize_state(void);
-extern	void	ML_io_stderr_stream(MR_Word *);
-extern	void	ML_io_stdout_stream(MR_Word *);
-extern	void	ML_io_stdin_stream(MR_Word *);
+extern	void	ML_io_stderr_stream(MR_MercuryFilePtr *);
+extern	void	ML_io_stdout_stream(MR_MercuryFilePtr *);
+extern	void	ML_io_stdin_stream(MR_MercuryFilePtr *);
 
-extern	void	ML_io_print_to_stream(MR_Word, MR_Word, MR_Word);
+extern	void	ML_io_print_to_stream(MR_Word, MR_MercuryFilePtr, MR_Word);
 extern	void	ML_io_print_to_cur_stream(MR_Word, MR_Word);
 
 /* in trace/mercury_trace_internal.h */
@@ -139,11 +140,12 @@
 extern	void	MR_trace_final_external(void);
 
 /* in browser/debugger_interface.h */
-extern	void	ML_DI_output_current_vars(MR_Word, MR_Word, MR_Word);
+extern	void	ML_DI_output_current_vars(MR_Word, MR_Word, MR_MercuryFilePtr);
 		/* output_current_vars/4 */
-extern	void	ML_DI_output_current_nth_var(MR_Word, MR_Word);
+extern	void	ML_DI_output_current_nth_var(MR_Word, MR_MercuryFilePtr);
 		/* output_current_nth_var/3 */
-extern	void	ML_DI_output_current_live_var_names(MR_Word, MR_Word, MR_Word);
+extern	void	ML_DI_output_current_live_var_names(MR_Word, MR_Word,
+			MR_MercuryFilePtr);
 		/* output_current_live_var_names/5 */
 extern	void	ML_DI_output_current_slots(MR_Integer, MR_Integer, MR_Integer,
 			MR_Word, MR_String, MR_String, MR_Integer, MR_Integer,
@@ -153,7 +155,7 @@
 			MR_String, MR_String, MR_Integer, MR_Integer,
 			MR_Integer, MR_Word, MR_String, MR_Word);
 		/* found_match/12 */
-extern	void	ML_DI_read_request_from_socket(MR_Word, MR_Word *,
+extern	void	ML_DI_read_request_from_socket(MR_MercuryFilePtr, MR_Word *,
 			MR_Integer *);
 extern	MR_Integer	ML_DI_get_var_number(MR_Word);
 
Index: runtime/mercury_layout_util.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_layout_util.c,v
retrieving revision 1.29
diff -u -b -r1.29 mercury_layout_util.c
--- runtime/mercury_layout_util.c	15 May 2002 11:24:15 -0000	1.29
+++ runtime/mercury_layout_util.c	24 Sep 2002 08:45:59 -0000
@@ -701,10 +701,10 @@
 void
 MR_write_variable(MR_TypeInfo type_info, MR_Word value)
 {
-	MR_Word	stdout_stream;
+	MR_MercuryFilePtr	stdout_mfp;
 
-	(*MR_io_stdout_stream)(&stdout_stream);
-	(*MR_io_print_to_stream)((MR_Word) type_info, stdout_stream, value);
+	(*MR_io_stdout_stream)(&stdout_mfp);
+	(*MR_io_print_to_stream)((MR_Word) type_info, stdout_mfp, value);
 }
 
 void
Index: runtime/mercury_library_types.h
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_library_types.h,v
retrieving revision 1.9
diff -u -b -r1.9 mercury_library_types.h
--- runtime/mercury_library_types.h	4 Jun 2002 14:28:58 -0000	1.9
+++ runtime/mercury_library_types.h	23 Sep 2002 07:40:33 -0000
@@ -17,48 +17,57 @@
 #include "mercury_types.h"	/* for `MR_Word' and `MR_Integer' */
 #include "mercury_std.h"	/* for MR_VARIABLE_SIZED */
 
+#if defined(fileno) || defined(MR_HAVE_FILENO)
+  #define MR_fp_to_fileno(fp) ((fp != NULL) ? fileno(fp) : -1)
+#else
+  #define MR_fp_to_fileno(fp) -1
+#endif
+
 /*
-** The C `MercuryFile' type is used for the Mercury `io__stream' type
-** in library/io.m.
+** The C `MR_MercuryFile' and `MR_MercuryFilePtr' types are used to implement
+** the Mercury `io__stream' type in library/io.m.
 ** Mercury files are not quite the same as C stdio FILEs,
 ** because we keep track of a lot more information.
 */
 #ifndef MR_NEW_MERCURYFILE_STRUCT
   typedef struct mercury_file {
-	FILE *file1;
-	int line_number1;
-  #ifdef MR_NATIVE_GC
-	int id;
-  #endif
-  } MercuryFile;
-
-  #define MR_file(mf)		(mf).file1
-  #define MR_line_number(mf)	(mf).line_number1
+	FILE		*MR_mf_file;
+	int		MR_mf_fd;
+	int		MR_mf_line_number;
+	int		MR_mf_id;
+	const char	*MR_mf_name;
+  } MR_MercuryFile;
+
+  #define MR_mf_file(mf)	(mf).MR_mf_file
+  #define MR_mf_fd(mf)		(mf).MR_mf_fd
+  #define MR_mf_line_number(mf)	(mf).MR_mf_line_number
+  #define MR_mf_id(mf)		(mf).MR_mf_id
+  #define MR_mf_name(mf)	(mf).MR_mf_name
 
   #define MR_IS_FILE_STREAM(mf)	( MR_TRUE )
 
-  #define MR_MERCURYFILE_INIT(file, line_number)		\
-  		{ (file), (line_number) }
+  #define MR_MERCURYFILE_INIT(file, line_number, id, name)		\
+		{ (file), MR_fp_to_fileno(file), (line_number), (id), (name) }
 
-  #define MR_CLOSE(mf)		fclose(MR_file(mf))
-  #define MR_FLUSH(mf)		fflush(MR_file(mf))
+  #define MR_CLOSE(mf)		fclose(MR_mf_file(mf))
+  #define MR_FLUSH(mf)		fflush(MR_mf_file(mf))
 
   #define MR_READ(mf, ptr, size)	\
-  		fread((ptr), sizeof(unsigned char), (size), MR_file(mf))
+  		fread((ptr), sizeof(unsigned char), (size), MR_mf_file(mf))
   #define MR_WRITE(mf, ptr, size)	\
-  		fwrite((ptr), sizeof(unsigned char), (size), MR_file(mf))
+  		fwrite((ptr), sizeof(unsigned char), (size), MR_mf_file(mf))
 
-  #define MR_UNGETCH(mf, ch)	ungetc((ch), MR_file(mf))
-  #define MR_GETCH(mf)		getc(MR_file(mf))
+  #define MR_UNGETCH(mf, ch)	ungetc((ch), MR_mf_file(mf))
+  #define MR_GETCH(mf)		getc(MR_mf_file(mf))
 
   #define MR_VFPRINTF(mf, fmt, args)	\
-  		vfprintf(MR_file(mf), (fmt), (args))
+  		vfprintf(MR_mf_file(mf), (fmt), (args))
 
-  #define MR_PUTCH(mf, ch)	putc((ch), MR_file(mf))
+  #define MR_PUTCH(mf, ch)	putc((ch), MR_mf_file(mf))
 
 #else /* MR_NEW_MERCURYFILE_STRUCT */
 
-	/* Possible types of a MercuryFile */
+	/* Possible types of an MR_MercuryFile */
   typedef enum {
 	MR_FILE_STREAM		= 1,
 	MR_SOCKET_STREAM	= 2,
@@ -67,11 +76,11 @@
   } MR_StreamType;
 
   /*
-  ** A pointer to the data which can be used to access the MercuryFile.
+  ** A pointer to the data which can be used to access the MR_MercuryFile.
   */
   typedef union {
-	FILE *file;
-	void *data;
+	FILE *MR_si_file;
+	void *MR_si_data;
   } MR_StreamInfo;
 
   typedef int (MR_Stream_close)(MR_StreamInfo *);
@@ -85,7 +94,7 @@
   typedef int (MR_Stream_putc)(MR_StreamInfo *, int);
 
   /*
-  ** The MercuryFile structure records:
+  ** The MR_MercuryFile structure records:
   **	- the type of the stream
   **	- a pointer to the information which describes the stream
   **	- the line number we are up to in the stream
@@ -96,56 +105,66 @@
   **	- pointers to functions which provide the same functionality
   **	  as flush/ungetc/getc/vprintf/putc on stdio files.
   **
-  ** MercuryFiles record all this extra information so that users can use all
+  ** MR_MercuryFiles record all this extra information so that users can use all
   ** the functionality of io.m on their own streams.  For instance see
   ** extras/logged_output.
   */
   typedef struct mercury_file {
-	MR_StreamType	stream_type;
-	MR_StreamInfo	stream_info;
-	int		line_number;
-  #ifdef MR_NATIVE_GC
-	int id;
-  #endif
+	MR_StreamType	MR_mf_stream_type;
+	MR_StreamInfo	MR_mf_stream_info;
+	int		MR_mf_line_number;
+	int		MR_mf_fd;
+	int		MR_mf_id;
+	const char	*MR_mf_name;
 
 		/* UNBUFFERED FUNCTIONS */
-	MR_Stream_close	*close;    
-	MR_Stream_read	*read;
-	MR_Stream_write	*write;
+	MR_Stream_close	*MR_mf_close;    
+	MR_Stream_read	*MR_mf_read;
+	MR_Stream_write	*MR_mf_write;
 
 		/* BUFFERED FUNCTIONS */
-	MR_Stream_flush		*flush;
-	MR_Stream_ungetc	*ungetc;
-	MR_Stream_getc		*getc;
-	MR_Stream_vprintf	*vprintf;
-	MR_Stream_putc		*putc;
-  } MercuryFile;
+	MR_Stream_flush		*MR_mf_flush;
+	MR_Stream_ungetc	*MR_mf_ungetc;
+	MR_Stream_getc		*MR_mf_getc;
+	MR_Stream_vprintf	*MR_mf_vprintf;
+	MR_Stream_putc		*MR_mf_putc;
+  } MR_MercuryFile;
 
   /*
-  ** access the file and line number fields
+  ** access the file, fd, line number, id and name fields
   */
-  #define MR_file(mf)		(mf).stream_info.file
-  #define MR_line_number(mf)	(mf).line_number
+  #define MR_mf_file(mf)	(mf).MR_mf_stream_info.MR_si_file
+  #define MR_mf_fd(mf)		(mf).MR_mf_fd
+  #define MR_mf_line_number(mf)	(mf).MR_mf_line_number
+  #define MR_mf_id(mf)		(mf).MR_mf_id
+  #define MR_mf_name(mf)	(mf).MR_mf_name
+
+  #define MR_IS_FILE_STREAM(mf)	( (mf).MR_mf_stream_type == MR_FILE_STREAM )
 
   /*
-  ** Call the functions associated with the MercuryFile structure
+  ** Call the functions associated with the MR_MercuryFile structure
   */
-  #define MR_CLOSE(mf)	((mf).close)(&((mf).stream_info))
+  #define MR_CLOSE(mf)			\
+		((mf).MR_mf_close)(&((mf).MR_mf_stream_info))
   #define MR_READ(mf, ptr, size)	\
-  		((mf).read)(&((mf).stream_info), (ptr), (size))
+  		((mf).MR_mf_read)(&((mf).MR_mf_stream_info), (ptr), (size))
   #define MR_WRITE(mf, ptr, size)	\
-  		((mf).write)(&((mf).stream_info), (ptr), (size))
+  		((mf).MR_mf_write)(&((mf).MR_mf_stream_info), (ptr), (size))
 
-  #define MR_FLUSH(mf)	((mf).flush)(&((mf).stream_info))
+  #define MR_FLUSH(mf)			\
+		((mf).MR_mf_flush)(&((mf).MR_mf_stream_info))
   #define MR_UNGETCH(mf, ch)	\
-  		((mf).ungetc)(&((mf).stream_info), (ch))
-  #define MR_GETCH(mf)	((mf).getc)(&((mf).stream_info))
+  		((mf).MR_mf_ungetc)(&((mf).MR_mf_stream_info), (ch))
+  #define MR_GETCH(mf)			\
+		((mf).MR_mf_getc)(&((mf).MR_mf_stream_info))
   #define MR_VFPRINTF(mf, fmt, args)	\
-  		((mf).vprintf)(&((mf).stream_info), (fmt), (args))
+  		((mf).MR_mf_vprintf)(&((mf).MR_mf_stream_info), (fmt), (args))
   #define MR_PUTCH(mf, ch)	\
-  		((mf).putc)(&((mf).stream_info), (ch))
+  		((mf).MR_mf_putc)(&((mf).MR_mf_stream_info), (ch))
 
 #endif	/* MR_NEW_MERCURYFILE_STRUCT */
+
+typedef	MR_MercuryFile	*MR_MercuryFilePtr;
 
 /*
 ** Definitions for accessing the representation of the
Index: runtime/mercury_tabling.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_tabling.c,v
retrieving revision 1.52
diff -u -b -r1.52 mercury_tabling.c
--- runtime/mercury_tabling.c	1 Aug 2002 11:52:27 -0000	1.52
+++ runtime/mercury_tabling.c	26 Aug 2002 08:19:42 -0000
@@ -993,41 +993,68 @@
 
 /*---------------------------------------------------------------------------*/
 
-void
-MR_table_report_statistics(FILE *fp)
+#define MR_BUF_SIZE 1024
+
+MR_String
+MR_table_report_statistics(void)
 {
-        fprintf(fp, "hash table search statistics:\n");
+    char    buf1[MR_BUF_SIZE];
+    char    buf2[MR_BUF_SIZE];
+    char    buf3[MR_BUF_SIZE];
+    char    buf4[MR_BUF_SIZE];
+    char    buf5[MR_BUF_SIZE];
+    char    *buf;
+
+    buf = MR_malloc(MR_BUF_SIZE);
+    if (buf == NULL) {
+        MR_fatal_error("MR_table_report_statistics: out of memory");
+    }
 
 #ifdef  MR_TABLE_STATISTICS
+    snprintf(buf1, MR_BUF_SIZE,
+        "hash table search statistics:\n");
+
         if (MR_table_hash_lookups == 0) {
-                fprintf(fp, "no successful searches\n");
+        snprintf(buf2, MR_BUF_SIZE,
+            "no successful searches\n");
         } else {
-                fprintf(fp, "successful   %6d, "
-                                "with an average of %6.3f comparisons\n",
+        snprintf(buf2, MR_BUF_SIZE,
+            "successful   %6d, with an average of %6.3f comparisons\n",
                         MR_table_hash_lookups,
                         (float) MR_table_hash_lookup_probes /
                                 (float) MR_table_hash_lookups);
         }
 
         if (MR_table_hash_inserts == 0) {
-                fprintf(fp, "no unsuccessful searches\n");
+        snprintf(buf3, MR_BUF_SIZE,
+            "no unsuccessful searches\n");
         } else {
-                fprintf(fp, "unsuccessful %6d, "
-                                "with an average of %6.3f comparisons\n",
+        snprintf(buf3, MR_BUF_SIZE,
+            "unsuccessful %6d, with an average of %6.3f comparisons\n",
                         MR_table_hash_inserts,
                         (float) MR_table_hash_insert_probes /
                                 (float) MR_table_hash_inserts);
         }
 
-        fprintf(fp, "rehash operations: %d, per search: %6.3f%%\n",
+    snprintf(buf4, MR_BUF_SIZE,
+        "rehash operations: %d, per search: %6.3f%%\n",
                         MR_table_hash_resizes,
                         (float) (100 * MR_table_hash_resizes) /
                         (float) (MR_table_hash_lookups
                                  + MR_table_hash_inserts));
-        fprintf(fp, "chunk allocations: %d\n", MR_table_hash_allocs);
+    snprintf(buf5, MR_BUF_SIZE,
+        "chunk allocations: %d\n", MR_table_hash_allocs);
+
+    snprintf(buf, MR_BUF_SIZE,
+        "%s%s%s%s%s",
+        buf1, buf2, buf3, buf4, buf5);
+
 #else
-        fprintf(fp, "not enabled\n");
+    snprintf(buf, MR_BUF_SIZE,
+        "hash table search statistics not enabled\n");
 #endif
+
+    return buf;
 }
 
 /*---------------------------------------------------------------------------*/
Index: runtime/mercury_tabling.h
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_tabling.h,v
retrieving revision 1.28
diff -u -b -r1.28 mercury_tabling.h
--- runtime/mercury_tabling.h	18 Feb 2002 07:01:21 -0000	1.28
+++ runtime/mercury_tabling.h	26 Aug 2002 08:12:22 -0000
@@ -330,11 +330,12 @@
 				MR_TypeInfo type_info, MR_Word data_value);
 
 /*
-** This function prints statistics about the operation of tabling, if the
-** collection of such statistics is enabled, on the given stream.
+** This function returns statistics about the operation of tabling, if the
+** collection of such statistics is enabled. The string should be freed with
+** MR_free after use.
 */
 
-extern	void		MR_table_report_statistics(FILE *fp);
+extern	MR_String	MR_table_report_statistics(void);
 
 /*---------------------------------------------------------------------------*/
 
Index: runtime/mercury_trace_base.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_trace_base.c,v
retrieving revision 1.47
diff -u -b -r1.47 mercury_trace_base.c
--- runtime/mercury_trace_base.c	11 Sep 2002 07:20:28 -0000	1.47
+++ runtime/mercury_trace_base.c	13 Sep 2002 03:56:20 -0000
@@ -52,6 +52,8 @@
 MR_bool		MR_trace_unhide_events = MR_FALSE;
 MR_bool		MR_trace_have_unhid_events = MR_FALSE;
 
+MR_bool		MR_trace_ask_about_eof = MR_TRUE;
+
 /*
 ** I/O tabling is documented in library/table_builtin.m
 */
Index: runtime/mercury_trace_base.h
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_trace_base.h,v
retrieving revision 1.27
diff -u -b -r1.27 mercury_trace_base.h
--- runtime/mercury_trace_base.h	11 Sep 2002 07:20:28 -0000	1.27
+++ runtime/mercury_trace_base.h	13 Sep 2002 03:55:43 -0000
@@ -230,6 +230,14 @@
 extern	MR_bool		MR_trace_have_unhid_events;
 
 /*
+** MR_trace_ask_about_eof controls whether the debugger asks "are you sure"
+** before terminating the program on receipt of an EOF on MR_mdb_in. It it
+** normally set to true, but code it can be set to disable the query.
+*/
+
+extern	MR_bool		MR_trace_ask_about_eof;
+
+/*
 ** The details of I/O tabling are documented in library/table_builtin.m.
 */
 
Index: runtime/mercury_wrapper.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_wrapper.c,v
retrieving revision 1.111
diff -u -b -r1.111 mercury_wrapper.c
--- runtime/mercury_wrapper.c	25 Sep 2002 07:54:12 -0000	1.111
+++ runtime/mercury_wrapper.c	25 Sep 2002 08:08:26 -0000
@@ -294,11 +294,11 @@
 void	(*MR_library_finalizer)(void);
 		/* normally ML_io_finalize_state (io__finalize_state/2) */
 
-void	(*MR_io_stderr_stream)(MR_Word *);
-void	(*MR_io_stdout_stream)(MR_Word *);
-void	(*MR_io_stdin_stream)(MR_Word *);
+void	(*MR_io_stdin_stream)(MR_MercuryFilePtr *);
+void	(*MR_io_stdout_stream)(MR_MercuryFilePtr *);
+void	(*MR_io_stderr_stream)(MR_MercuryFilePtr *);
 void	(*MR_io_print_to_cur_stream)(MR_Word, MR_Word);
-void	(*MR_io_print_to_stream)(MR_Word, MR_Word, MR_Word);
+void	(*MR_io_print_to_stream)(MR_Word, MR_MercuryFilePtr, MR_Word);
 
 void	(*MR_DI_output_current_ptr)(MR_Integer, MR_Integer, MR_Integer,
 		MR_Word, MR_String, MR_String, MR_Integer, MR_Integer,
@@ -309,15 +309,6 @@
 		MR_Word, MR_String, MR_Word);
 		/* normally ML_DI_found_match (output_current/12) */
 void	(*MR_DI_read_request_from_socket)(MR_Word, MR_Word *, MR_Integer *);
-
-/*
-** This variable has been replaced by MR_io_print_to_*_stream,
-** but the installed mkinit executable may still generate references to it.
-** We must therefore keep it until all obsolete mkinit executables have
-** been retired.
-*/
-
-MR_Code	*MR_library_trace_browser;
 
 MR_Code	*(*volatile MR_trace_func_ptr)(const MR_Label_Layout *);
 
Index: runtime/mercury_wrapper.h
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_wrapper.h,v
retrieving revision 1.55
diff -u -b -r1.55 mercury_wrapper.h
--- runtime/mercury_wrapper.h	25 Sep 2002 07:54:13 -0000	1.55
+++ runtime/mercury_wrapper.h	25 Sep 2002 08:08:26 -0000
@@ -19,6 +19,7 @@
 #include "mercury_trace_base.h"		/* for `MR_trace_port' */
 #include "mercury_stacks.h"		/* for `MR_{Cut,Generator}StackFrame' */
 #include "mercury_type_info.h"		/* for `MR_TypeCtorInfo' */
+#include "mercury_library_types.h"	/* for `MR_MercuryFilePtr' */
 #include <stdio.h>			/* for `FILE' */
 
 /*
@@ -79,11 +80,12 @@
 extern	void		(*MR_library_initializer)(void);
 extern	void		(*MR_library_finalizer)(void);
 
-extern	void		(*MR_io_stderr_stream)(MR_Word *);
-extern	void		(*MR_io_stdout_stream)(MR_Word *);
-extern	void		(*MR_io_stdin_stream)(MR_Word *);
+extern	void		(*MR_io_stdin_stream)(MR_MercuryFilePtr *);
+extern	void		(*MR_io_stdout_stream)(MR_MercuryFilePtr *);
+extern	void		(*MR_io_stderr_stream)(MR_MercuryFilePtr *);
 extern	void		(*MR_io_print_to_cur_stream)(MR_Word, MR_Word);
-extern	void		(*MR_io_print_to_stream)(MR_Word, MR_Word, MR_Word);
+extern	void		(*MR_io_print_to_stream)(MR_Word, MR_MercuryFilePtr,
+				MR_Word);
 
 extern	void		(*MR_address_of_mercury_init_io)(void);
 extern	void		(*MR_address_of_init_modules)(void);
@@ -133,16 +135,6 @@
 
 extern	void		(*MR_address_of_trace_init_external)(void);
 extern	void		(*MR_address_of_trace_final_external)(void);
-
-/*
-** XXX This is obsolete too.
-** This variable has been replaced by MR_io_print_to_*_stream,
-** but the installed mkinit executable may still generate references to it.
-** We must therefore keep it until all obsolete mkinit executables have
-** been retired.
-*/
-
-extern	MR_Code		*MR_library_trace_browser;
 
 /*
 ** MR_trace_func_ptr is set to either MR_trace_real (trace/mercury_trace.c),
cvs diff: Diffing runtime/GETOPT
cvs diff: Diffing runtime/machdeps
cvs diff: Diffing samples
cvs diff: Diffing samples/c_interface
cvs diff: Diffing samples/c_interface/c_calls_mercury
cvs diff: Diffing samples/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/c_interface/mercury_calls_c
cvs diff: Diffing samples/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/diff
cvs diff: Diffing samples/muz
cvs diff: Diffing samples/rot13
cvs diff: Diffing samples/solutions
cvs diff: Diffing samples/tests
cvs diff: Diffing samples/tests/c_interface
cvs diff: Diffing samples/tests/c_interface/c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/tests/c_interface/mercury_calls_c
cvs diff: Diffing samples/tests/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/tests/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/tests/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/tests/diff
cvs diff: Diffing samples/tests/muz
cvs diff: Diffing samples/tests/rot13
cvs diff: Diffing samples/tests/solutions
cvs diff: Diffing samples/tests/toplevel
cvs diff: Diffing scripts
cvs diff: Diffing tests
cvs diff: Diffing tests/benchmarks
cvs diff: Diffing tests/debugger
Index: tests/debugger/DATA.in
===================================================================
RCS file: tests/debugger/DATA.in
diff -N tests/debugger/DATA.in
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/debugger/DATA.in	24 Sep 2002 15:32:11 -0000
@@ -0,0 +1 @@
+This is the contents of DATA.in.
Index: tests/debugger/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/debugger/Mmakefile,v
retrieving revision 1.89
diff -u -b -r1.89 Mmakefile
--- tests/debugger/Mmakefile	24 Sep 2002 06:37:56 -0000	1.89
+++ tests/debugger/Mmakefile	24 Sep 2002 15:33:03 -0000
@@ -34,6 +34,7 @@
 	print_goal			\
 	queens_rep			\
 	resume_typeinfos		\
+	stream				\
 	type_desc_test
 
 # This test is currently not useful.
@@ -327,6 +328,9 @@
 
 shallow.out: shallow shallow.inp
 	$(MDB) ./shallow < shallow.inp > shallow.out 2>&1
+
+stream.out: stream stream.inp DATA.in
+	$(MDB_STD) ./stream < stream.inp > stream.out 2>&1
 
 tabled_read.out: tabled_read tabled_read.inp tabled_read.data
 	$(MDB_STD) ./tabled_read < tabled_read.inp > tabled_read.out 2>&1
Index: tests/debugger/stream.exp
===================================================================
RCS file: tests/debugger/stream.exp
diff -N tests/debugger/stream.exp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/debugger/stream.exp	24 Sep 2002 15:42:22 -0000
@@ -0,0 +1,16 @@
+      E1:     C1  1 CALL pred stream:main/2-0 (det) stream.m:11
+mdb> echo on
+Command echo enabled.
+mdb> context none
+Contexts will not be printed.
+mdb> register --quiet
+mdb> b stream__test
+ 0: + stop  interface pred stream:test/4-0 (det)
+mdb> c
+      E2:     C2  2 CALL pred stream:test/4-0 (det)
+mdb> p *
+       InStream (arg 1)       	stream_rep(9, "DATA.in")
+       OutStream (arg 2)      	stream_rep(2, "<stdout>")
+       DCG_0 (arg 3)          	state('<<c_pointer>>')
+mdb> c -S
+This is the contents of DATA.in.
Index: tests/debugger/stream.inp
===================================================================
RCS file: tests/debugger/stream.inp
diff -N tests/debugger/stream.inp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/debugger/stream.inp	24 Sep 2002 15:37:02 -0000
@@ -0,0 +1,7 @@
+echo on
+context none
+register --quiet
+b stream__test
+c
+p *
+c -S
Index: tests/debugger/stream.m
===================================================================
RCS file: tests/debugger/stream.m
diff -N tests/debugger/stream.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/debugger/stream.m	24 Sep 2002 15:43:56 -0000
@@ -0,0 +1,29 @@
+:- module stream.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io__state::di, io__state::uo) is det.
+
+:- implementation.
+
+main -->
+	io__open_input("DATA.in", InRes),
+	( { InRes = ok(InStream) } ->
+		io__stdout_stream(OutStream),
+		test(InStream, OutStream)
+	;
+		io__write_string("cannot open DATA.in\n")
+	).
+
+:- pred test(io__input_stream::in, io__output_stream::in,
+	io__state::di, io__state::uo) is det.
+
+test(InStream, OutStream) -->
+	io__read_file_as_string(InStream, ReadRes),
+	( { ReadRes = ok(Str) } ->
+		io__write_string(OutStream, Str)
+	;
+		io__write_string("cannot read DATA.in\n")
+	).
cvs diff: Diffing tests/debugger/declarative
Index: tests/debugger/declarative/DATA.in
===================================================================
RCS file: tests/debugger/declarative/DATA.in
diff -N tests/debugger/declarative/DATA.in
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/debugger/declarative/DATA.in	24 Sep 2002 15:44:09 -0000
@@ -0,0 +1 @@
+This is the contents of DATA.in.
Index: tests/debugger/declarative/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/debugger/declarative/Mmakefile,v
retrieving revision 1.42
diff -u -b -r1.42 Mmakefile
--- tests/debugger/declarative/Mmakefile	13 Sep 2002 04:17:46 -0000	1.42
+++ tests/debugger/declarative/Mmakefile	24 Sep 2002 15:46:37 -0000
@@ -35,6 +35,7 @@
 	small			\
 	solutions		\
 	special_term_dep	\
+	stream			\
 	tabled_read_decl	\
 	throw
 
@@ -202,6 +203,9 @@
 special_term_dep.out: special_term_dep special_term_dep.inp
 	$(MDB) ./special_term_dep < special_term_dep.inp \
 			> special_term_dep.out 2>&1
+
+stream.out: stream stream.inp
+	$(MDB_STD) ./stream < stream.inp > stream.out 2>&1
 
 tabled_read_decl.out: tabled_read_decl tabled_read_decl.inp
 	$(MDB) ./tabled_read_decl < tabled_read_decl.inp \
Index: tests/debugger/declarative/stream.exp
===================================================================
RCS file: tests/debugger/declarative/stream.exp
diff -N tests/debugger/declarative/stream.exp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/debugger/declarative/stream.exp	24 Sep 2002 18:08:14 -0000
@@ -0,0 +1,19 @@
+      E1:     C1  1 CALL pred stream:main/2-0 (det) stream.m:11
+mdb> echo on
+Command echo enabled.
+mdb> context none
+Contexts will not be printed.
+mdb> register --quiet
+mdb> b stream__test
+ 0: + stop  interface pred stream:test/4-0 (det)
+mdb> c
+      E2:     C2  2 CALL pred stream:test/4-0 (det)
+mdb> finish
+This is the contents of DATA.in.
+      E3:     C2  2 EXIT pred stream:test/4-0 (det)
+mdb> dd
+test(stream_rep(9, "DATA.in"), stream_rep(2, "<stdout>"), _, state('<<c_pointer>>'))
+Valid? yes
+No bug found.
+      E3:     C2  2 EXIT pred stream:test/4-0 (det)
+mdb> c -S
Index: tests/debugger/declarative/stream.inp
===================================================================
RCS file: tests/debugger/declarative/stream.inp
diff -N tests/debugger/declarative/stream.inp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/debugger/declarative/stream.inp	24 Sep 2002 15:45:20 -0000
@@ -0,0 +1,9 @@
+echo on
+context none
+register --quiet
+b stream__test
+c
+finish
+dd
+yes
+c -S
Index: tests/debugger/declarative/stream.m
===================================================================
RCS file: tests/debugger/declarative/stream.m
diff -N tests/debugger/declarative/stream.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/debugger/declarative/stream.m	24 Sep 2002 15:43:58 -0000
@@ -0,0 +1,29 @@
+:- module stream.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io__state::di, io__state::uo) is det.
+
+:- implementation.
+
+main -->
+	io__open_input("DATA.in", InRes),
+	( { InRes = ok(InStream) } ->
+		io__stdout_stream(OutStream),
+		test(InStream, OutStream)
+	;
+		io__write_string("cannot open DATA.in\n")
+	).
+
+:- pred test(io__input_stream::in, io__output_stream::in,
+	io__state::di, io__state::uo) is det.
+
+test(InStream, OutStream) -->
+	io__read_file_as_string(InStream, ReadRes),
+	( { ReadRes = ok(Str) } ->
+		io__write_string(OutStream, Str)
+	;
+		io__write_string("cannot read DATA.in\n")
+	).
cvs diff: Diffing tests/dppd
cvs diff: Diffing tests/general
cvs diff: Diffing tests/general/accumulator
cvs diff: Diffing tests/general/structure_reuse
cvs diff: Diffing tests/hard_coded
cvs diff: Diffing tests/hard_coded/exceptions
cvs diff: Diffing tests/hard_coded/purity
cvs diff: Diffing tests/hard_coded/sub-modules
cvs diff: Diffing tests/hard_coded/typeclasses
cvs diff: Diffing tests/invalid
cvs diff: Diffing tests/invalid/purity
cvs diff: Diffing tests/misc_tests
cvs diff: Diffing tests/recompilation
cvs diff: Diffing tests/tabling
cvs diff: Diffing tests/term
cvs diff: Diffing tests/valid
cvs diff: Diffing tests/warnings
cvs diff: Diffing tools
cvs diff: Diffing trace
Index: trace/mercury_trace_browse.c
===================================================================
RCS file: /home/mercury1/repository/mercury/trace/mercury_trace_browse.c,v
retrieving revision 1.26
diff -u -b -r1.26 mercury_trace_browse.c
--- trace/mercury_trace_browse.c	5 Aug 2002 21:46:18 -0000	1.26
+++ trace/mercury_trace_browse.c	18 Sep 2002 12:40:33 -0000
@@ -33,6 +33,7 @@
 #include "mdb.browser_info.mh"
 #include "mdb.interactive_query.mh"
 
+#include "io.mh"
 #include "type_desc.mh"
 
 #include <stdio.h>
@@ -48,26 +49,22 @@
 void
 MR_trace_browse(MR_Word type_info, MR_Word value, MR_Browse_Format format)
 {
-	MercuryFile	mdb_in, mdb_out;
 	MR_Word		maybe_mark;
 
 	MR_trace_browse_ensure_init();
 
-	MR_c_file_to_mercury_file(MR_mdb_in, &mdb_in);
-	MR_c_file_to_mercury_file(MR_mdb_out, &mdb_out);
-
 	if (format != MR_BROWSE_DEFAULT_FORMAT) {
 		MR_TRACE_CALL_MERCURY(
-			ML_BROWSE_browse_format(type_info, value,
-				(MR_Word) &mdb_in, (MR_Word) &mdb_out,
+			ML_BROWSE_browse_format_mfp(type_info, value,
+				MR_mdb_in_mfp, MR_mdb_out_mfp,
 				(MR_Word) format,
 				MR_trace_browser_persistent_state,
 				&MR_trace_browser_persistent_state);
 		);
 	} else {
 		MR_TRACE_CALL_MERCURY(
-			ML_BROWSE_browse(type_info, value,
-				(MR_Word) &mdb_in, (MR_Word) &mdb_out,
+			ML_BROWSE_browse_mfp(type_info, value,
+				MR_mdb_in_mfp, MR_mdb_out_mfp,
 				&maybe_mark, MR_trace_browser_persistent_state,
 				&MR_trace_browser_persistent_state);
 		);
@@ -81,28 +78,24 @@
 MR_trace_browse_goal(MR_ConstString name, MR_Word arg_list, MR_Word is_func,
 	MR_Browse_Format format)
 {
-	MercuryFile	mdb_in, mdb_out;
 	MR_Word		maybe_mark;
 
 	MR_trace_browse_ensure_init();
 
-	MR_c_file_to_mercury_file(MR_mdb_in, &mdb_in);
-	MR_c_file_to_mercury_file(MR_mdb_out, &mdb_out);
-
 	if (format != MR_BROWSE_DEFAULT_FORMAT) {
 		MR_TRACE_CALL_MERCURY(
-			ML_BROWSE_browse_format_synthetic(
+			ML_BROWSE_browse_format_synthetic_mfp(
 				(MR_String) (MR_Word) name, arg_list, is_func,
-				(MR_Word) &mdb_in, (MR_Word) &mdb_out,
+				MR_mdb_in_mfp, MR_mdb_out_mfp,
 				(MR_Word) format,
 				MR_trace_browser_persistent_state,
 				&MR_trace_browser_persistent_state);
 		);
 	} else {
 		MR_TRACE_CALL_MERCURY(
-			ML_BROWSE_browse_synthetic(
+			ML_BROWSE_browse_synthetic_mfp(
 				(MR_String) (MR_Word) name, arg_list, is_func,
-				(MR_Word) &mdb_in, (MR_Word) &mdb_out,
+				MR_mdb_in_mfp, MR_mdb_out_mfp,
 				&maybe_mark, MR_trace_browser_persistent_state,
 				&MR_trace_browser_persistent_state);
 		);
@@ -128,9 +121,9 @@
 	MR_trace_browse_ensure_init();
 
 	MR_TRACE_CALL_MERCURY(
-		ML_BROWSE_browse_external(type_info, value,
-			(MR_Word) &MR_debugger_socket_in, 
-			(MR_Word) &MR_debugger_socket_out,
+		ML_BROWSE_browse_external_mfp(type_info, value,
+			&MR_debugger_socket_in_mf, 
+			&MR_debugger_socket_out_mf,
 			MR_trace_browser_persistent_state,
 			&MR_trace_browser_persistent_state);
 	);
@@ -145,23 +138,19 @@
 MR_trace_print(MR_Word type_info, MR_Word value, MR_Browse_Caller_Type caller,
 		MR_Browse_Format format)
 {
-	MercuryFile mdb_out;
-
 	MR_trace_browse_ensure_init();
 
-	MR_c_file_to_mercury_file(MR_mdb_out, &mdb_out);
-
 	if (format != MR_BROWSE_DEFAULT_FORMAT) {
 		MR_TRACE_CALL_MERCURY(
-			ML_BROWSE_print_format(type_info, value,
-				(MR_Word) &mdb_out, (MR_Word) caller,
+			ML_BROWSE_print_format_mfp(type_info, value,
+				MR_mdb_out_mfp, (MR_Word) caller,
 				(MR_Word) format,
 				MR_trace_browser_persistent_state);
 		);
 	} else {
 		MR_TRACE_CALL_MERCURY(
-			ML_BROWSE_print(type_info, value,
-				(MR_Word) &mdb_out, (MR_Word) caller,
+			ML_BROWSE_print_mfp(type_info, value,
+				MR_mdb_out_mfp, (MR_Word) caller,
 				MR_trace_browser_persistent_state);
 		);
 	}
@@ -171,25 +160,21 @@
 MR_trace_print_goal(MR_ConstString name, MR_Word arg_list, MR_Word is_func,
 	MR_Browse_Caller_Type caller, MR_Browse_Format format)
 {
-	MercuryFile mdb_out;
-
 	MR_trace_browse_ensure_init();
 
-	MR_c_file_to_mercury_file(MR_mdb_out, &mdb_out);
-
 	if (format != MR_BROWSE_DEFAULT_FORMAT) {
 		MR_TRACE_CALL_MERCURY(
-			ML_BROWSE_print_format_synthetic(
+			ML_BROWSE_print_format_synthetic_mfp(
 				(MR_String) (MR_Word) name, arg_list, is_func,
-				(MR_Word) &mdb_out, (MR_Word) caller,
+				MR_mdb_out_mfp, (MR_Word) caller,
 				(MR_Word) format,
 				MR_trace_browser_persistent_state);
 		);
 	} else {
 		MR_TRACE_CALL_MERCURY(
-			ML_BROWSE_print_synthetic(
+			ML_BROWSE_print_synthetic_mfp(
 				(MR_String) (MR_Word) name, arg_list, is_func,
-				(MR_Word) &mdb_out, (MR_Word) caller,
+				MR_mdb_out_mfp, (MR_Word) caller,
 				MR_trace_browser_persistent_state);
 		);
 	}
@@ -315,12 +300,8 @@
 {
 	MR_ConstString options_on_heap;
 	MR_Word imports_list;
-	MercuryFile mdb_in, mdb_out;
 	int i;
 
-	MR_c_file_to_mercury_file(MR_mdb_in, &mdb_in);
-	MR_c_file_to_mercury_file(MR_mdb_out, &mdb_out);
-
 	if (options == NULL) options = "";
 
         MR_TRACE_USE_HP(
@@ -336,8 +317,8 @@
 	);
 
 	MR_TRACE_CALL_MERCURY(
-		ML_query(type, imports_list, (MR_String) options_on_heap,
-			(MR_Word) &mdb_in, (MR_Word) &mdb_out);
+		ML_query_mfp(type, imports_list, (MR_String) options_on_heap,
+			MR_mdb_in_mfp, MR_mdb_out_mfp);
 	);
 }
 
@@ -348,9 +329,8 @@
 	MR_Word imports_list)
 {
 	MR_TRACE_CALL_MERCURY(
-		ML_query_external(type, imports_list,  options,
-			(MR_Word) &MR_debugger_socket_in, 
-			(MR_Word) &MR_debugger_socket_out);
+		ML_query_external_mfp(type, imports_list, options,
+			&MR_debugger_socket_in_mf, &MR_debugger_socket_out_mf);
 	);
 }
 
Index: trace/mercury_trace_declarative.c
===================================================================
RCS file: /home/mercury1/repository/mercury/trace/mercury_trace_declarative.c,v
retrieving revision 1.56
diff -u -b -r1.56 mercury_trace_declarative.c
--- trace/mercury_trace_declarative.c	5 Aug 2002 21:46:18 -0000	1.56
+++ trace/mercury_trace_declarative.c	24 Sep 2002 12:24:44 -0000
@@ -49,6 +49,7 @@
 #include "mdb.declarative_execution.mh"
 
 #include "std_util.mh"
+#include "io.mh"
 
 #include <errno.h>
 
@@ -157,9 +158,11 @@
 ** which the store should be written when built.  This global is
 ** set in MR_trace_start_decl_debug, and keeps the same value
 ** throughout the declarative debugging session.
+** XXX the last part is not true; it is closed once a trace is saved.
 */
 
 static	FILE		*MR_trace_store_file;
+static	MR_MercuryFile	MR_trace_store_file_mf;
 
 static	MR_Trace_Node	MR_trace_decl_call(MR_Event_Info *event_info,
 				MR_Trace_Node prev);
@@ -1029,26 +1032,19 @@
 MR_trace_decl_ensure_init(void)
 {
 	static MR_bool		done = MR_FALSE;
-	static MercuryFile	mdb_in;
-	static MercuryFile	mdb_out;
-
-	MR_mercuryfile_init(MR_mdb_in, 1, &mdb_in);
-	MR_mercuryfile_init(MR_mdb_out, 1, &mdb_out);
 
 	if (! done) {
 		MR_TRACE_CALL_MERCURY(
 			MR_trace_node_store = 0;
-			MR_DD_decl_diagnosis_state_init(
-					(MR_Word) &mdb_in,
-					(MR_Word) &mdb_out,
-					&MR_trace_front_end_state);
+			MR_DD_decl_diagnosis_state_init(MR_mdb_in_mfp,
+				MR_mdb_out_mfp, &MR_trace_front_end_state);
 		);
 		done = MR_TRUE;
 	}
 }
 
 MR_bool
-MR_trace_start_decl_debug(MR_Trace_Mode trace_mode, const char *outfile,
+MR_trace_start_decl_debug(MR_Trace_Mode trace_mode, char *outfile,
 		MR_Trace_Cmd_Info *cmd, MR_Event_Info *event_info,
 		MR_Event_Details *event_details, MR_Code **jumpaddr)
 {
@@ -1107,6 +1103,9 @@
 		}
 	}
 
+	MR_mercuryfile_init(MR_trace_store_file, "<trace store file>", 1,
+		&MR_trace_store_file_mf);
+
 	MR_trace_decl_mode = trace_mode;
 
 	MR_trace_decl_ensure_init();
@@ -1223,7 +1222,6 @@
 	MR_Unsigned		bug_event;
 	MR_Unsigned		final_event;
 	MR_Unsigned		topmost_seqno;
-	MercuryFile		stream;
 	MR_Integer		use_old_io_map;
 	MR_Unsigned		io_start;
 	MR_Unsigned		io_end;
@@ -1241,10 +1239,8 @@
 	}
 
 	if (MR_trace_decl_mode == MR_TRACE_DECL_DEBUG_DUMP) {
-		MR_mercuryfile_init(MR_trace_store_file, 1, &stream);
-
 		MR_TRACE_CALL_MERCURY(
-			MR_DD_save_trace((MR_Word) &stream,
+			MR_DD_save_trace_mfp(&MR_trace_store_file_mf,
 						MR_trace_node_store, root);
 		);
 
@@ -1476,13 +1472,9 @@
 static	void
 MR_decl_checkpoint_loc(const char *str, MR_Trace_Node node)
 {
-	MercuryFile mdb_out;
-
-	MR_mercuryfile_init(MR_mdb_out, 1, &mdb_out);
-
 	fprintf(MR_mdb_out, "DD %s: %ld ", str, (long) node);
 	MR_TRACE_CALL_MERCURY(
-		MR_DD_print_trace_node((MR_Word) &mdb_out, (MR_Word) node);
+		MR_DD_print_trace_node_mfp(MR_mdb_out_mfp, (MR_Word) node);
 	);
 	fprintf(MR_mdb_out, "\n");
 }
Index: trace/mercury_trace_declarative.h
===================================================================
RCS file: /home/mercury1/repository/mercury/trace/mercury_trace_declarative.h,v
retrieving revision 1.14
diff -u -b -r1.14 mercury_trace_declarative.h
--- trace/mercury_trace_declarative.h	18 Feb 2002 07:01:29 -0000	1.14
+++ trace/mercury_trace_declarative.h	9 Sep 2002 00:37:06 -0000
@@ -29,7 +29,7 @@
 */
 
 extern	MR_bool	MR_trace_start_decl_debug(MR_Trace_Mode trace_mode,
-			const char *out, MR_Trace_Cmd_Info *cmd,
+			char *out, MR_Trace_Cmd_Info *cmd,
 			MR_Event_Info *event_info,
 			MR_Event_Details *event_details, MR_Code **jumpaddr);
 
Index: trace/mercury_trace_external.c
===================================================================
RCS file: /home/mercury1/repository/mercury/trace/mercury_trace_external.c,v
retrieving revision 1.66
diff -u -b -r1.66 mercury_trace_external.c
--- trace/mercury_trace_external.c	5 Aug 2002 21:46:18 -0000	1.66
+++ trace/mercury_trace_external.c	18 Sep 2002 13:31:41 -0000
@@ -32,6 +32,7 @@
 #include "mdb.debugger_interface.mh"
 #include "mdb.collect_lib.mh"
 
+#include "io.mh"
 #include "type_desc.mh"
 
 #include "mercury_deep_copy.h"
@@ -97,8 +98,8 @@
 
 } MR_debugger_request_type;
 
-MercuryFile MR_debugger_socket_in;
-MercuryFile MR_debugger_socket_out;
+MR_MercuryFile	MR_debugger_socket_in_mf;
+MR_MercuryFile	MR_debugger_socket_out_mf;
 
 static MR_String	MR_mmc_options;
 
@@ -141,7 +142,7 @@
 ** debugger.
 */
 
-static	void	(*send_collect_result_ptr)(MR_Word, MR_Word);
+static	void	(*send_collect_result_ptr)(MR_Word, MR_MercuryFilePtr);
 
 /*
 ** Variable generated during the dynamic linking that is needed to close
@@ -409,8 +410,10 @@
 		fprintf(stderr, "Mercury runtime: fdopen(): ok\n");
 	}
 
-	MR_mercuryfile_init(file_in, 1, &MR_debugger_socket_in);
-	MR_mercuryfile_init(file_out, 1, &MR_debugger_socket_out);
+	MR_mercuryfile_init(file_in, "<morphine input socket>", 1,
+		&MR_debugger_socket_in_mf);
+	MR_mercuryfile_init(file_out, "<morphine output socket>", 1,
+		&MR_debugger_socket_out_mf);
 
 	/*
 	** Send hello
@@ -938,7 +941,7 @@
 			layout->MR_sll_entry->MR_sle_detism,
 			(MR_String) (MR_Word) path,
 			lineno,
-			(MR_Word) &MR_debugger_socket_out);
+			&MR_debugger_socket_out_mf);
 		    );
 	} else {
 		MR_TRACE_CALL_MERCURY(
@@ -959,7 +962,7 @@
 			layout->MR_sll_entry->MR_sle_detism,
 			(MR_String) (MR_Word) path,
 			lineno,
-			(MR_Word) &MR_debugger_socket_out);
+			&MR_debugger_socket_out_mf);
 		    );
 	}
 }
@@ -971,7 +974,7 @@
 	ML_DI_output_current_vars(
 		var_list,
 		string_list,
-		(MR_Word) &MR_debugger_socket_out);
+		&MR_debugger_socket_out_mf);
     );
 }
 
@@ -981,7 +984,7 @@
     MR_TRACE_CALL_MERCURY(
 	ML_DI_output_current_nth_var(
 		var,
-		(MR_Word) &MR_debugger_socket_out);
+		&MR_debugger_socket_out_mf);
     );
 }
 
@@ -992,7 +995,7 @@
 	ML_DI_output_current_live_var_names(
 		var_names_list,
 		type_list,
-		(MR_Word) &MR_debugger_socket_out);
+		&MR_debugger_socket_out_mf);
     );
 }
 
@@ -1001,11 +1004,11 @@
 			MR_Word *debugger_request_ptr, 
 			MR_Integer *debugger_request_type_ptr)
 {		
-	fflush(MR_file(MR_debugger_socket_in));
+	fflush(MR_mf_file(MR_debugger_socket_in_mf));
 
     MR_TRACE_CALL_MERCURY(
 	ML_DI_read_request_from_socket(
-		(MR_Word) &MR_debugger_socket_in, 
+		&MR_debugger_socket_in_mf,
 		debugger_request_ptr, 
 		debugger_request_type_ptr);
     );
@@ -1074,18 +1077,18 @@
 	va_list args;
 
 	va_start(args, format);
-	vfprintf(MR_file(MR_debugger_socket_out), format, args);
+	vfprintf(MR_mf_file(MR_debugger_socket_out_mf), format, args);
 	va_end(args);
-	fflush(MR_file(MR_debugger_socket_out));
-	MR_line_number(MR_debugger_socket_out)++;
+	fflush(MR_mf_file(MR_debugger_socket_out_mf));
+	MR_mf_line_number(MR_debugger_socket_out_mf)++;
 }
 
 static void
 MR_send_message_to_socket(const char *message)
 {
-	fprintf(MR_file(MR_debugger_socket_out), "%s.\n", message);
-	fflush(MR_file(MR_debugger_socket_out));
-	MR_line_number(MR_debugger_socket_out)++;
+	fprintf(MR_mf_file(MR_debugger_socket_out_mf), "%s.\n", message);
+	fflush(MR_mf_file(MR_debugger_socket_out_mf));
+	MR_mf_line_number(MR_debugger_socket_out_mf)++;
 }
 
 /*
@@ -1567,10 +1570,11 @@
 
 		(*send_collect_result_ptr)(
 			MR_collected_variable, 
-			(MR_Word) &MR_debugger_socket_out));
+			&MR_debugger_socket_out_mf));
 #if defined(MR_HAVE_DLFCN_H) && defined(MR_HAVE_DLCLOSE)
 	MR_TRACE_CALL_MERCURY(
        		ML_CL_unlink_collect(collect_lib_maybe_handle));
 #endif
 }
+
 #endif /* MR_USE_EXTERNAL_DEBUGGER */
Index: trace/mercury_trace_external.h
===================================================================
RCS file: /home/mercury1/repository/mercury/trace/mercury_trace_external.h,v
retrieving revision 1.12
diff -u -b -r1.12 mercury_trace_external.h
--- trace/mercury_trace_external.h	18 Feb 2002 07:01:30 -0000	1.12
+++ trace/mercury_trace_external.h	18 Sep 2002 12:39:31 -0000
@@ -10,7 +10,7 @@
 #include "mercury_trace.h"		/* for MR_Trace_Cmd_Info, etc.  */
 #include "mercury_conf.h"		/* for MR_USE_EXTERNAL_DEBUGGER */
 #include "mercury_types.h"		/* for MR_Code                  */
-#include "mercury_library_types.h"	/* for MercuryFile              */
+#include "mercury_library_types.h"	/* for MR_MercuryFile           */
 
 #ifdef	MR_USE_EXTERNAL_DEBUGGER
 
@@ -31,8 +31,8 @@
 ** External debugger socket streams.
 */
 
-extern MercuryFile MR_debugger_socket_in;
-extern MercuryFile MR_debugger_socket_out;
+extern MR_MercuryFile MR_debugger_socket_in_mf;
+extern MR_MercuryFile MR_debugger_socket_out_mf;
 
 #endif	/* MR_USE_EXTERNAL_DEBUGGER */
 
Index: trace/mercury_trace_help.c
===================================================================
RCS file: /home/mercury1/repository/mercury/trace/mercury_trace_help.c,v
retrieving revision 1.19
diff -u -b -r1.19 mercury_trace_help.c
--- trace/mercury_trace_help.c	5 Aug 2002 21:46:19 -0000	1.19
+++ trace/mercury_trace_help.c	18 Sep 2002 11:56:12 -0000
@@ -117,14 +117,10 @@
 void
 MR_trace_help(void)
 {
-	MercuryFile mdb_out;
-
 	MR_trace_help_ensure_init();
 
-	MR_c_file_to_mercury_file(MR_mdb_out, &mdb_out);
-
 	MR_TRACE_CALL_MERCURY(
-		ML_HELP_help(MR_trace_help_system, (MR_Word) &mdb_out);
+		ML_HELP_help_mfp(MR_trace_help_system, MR_mdb_out_mfp);
 	);
 }
 
@@ -132,7 +128,6 @@
 MR_trace_help_word(const char *word)
 {
 	char	*word_on_heap;
-	MercuryFile mdb_out;
 
 	MR_trace_help_ensure_init();
 
@@ -140,10 +135,9 @@
 		MR_make_aligned_string_copy(word_on_heap, word);
 	);
 
-	MR_c_file_to_mercury_file(MR_mdb_out, &mdb_out);
 	MR_TRACE_CALL_MERCURY(
-		ML_HELP_name(MR_trace_help_system, word_on_heap,
-			(MR_Word) &mdb_out);
+		ML_HELP_name_mfp(MR_trace_help_system, word_on_heap,
+			MR_mdb_out_mfp);
 	);
 }
 
@@ -156,7 +150,6 @@
 	char	*category_on_heap;
 	char	*item_on_heap;
 	MR_bool	error;
-	MercuryFile mdb_out;
 
 	MR_trace_help_ensure_init();
 
@@ -168,10 +161,9 @@
 		path = MR_list_cons((MR_Word) category_on_heap, path);
 	);
 
-	MR_c_file_to_mercury_file(MR_mdb_out, &mdb_out);
 	MR_TRACE_CALL_MERCURY(
-		ML_HELP_path(MR_trace_help_system, path,
-			(MR_Word) &mdb_out, &result);
+		ML_HELP_path_mfp(MR_trace_help_system, path, MR_mdb_out_mfp,
+			&result);
 		error = ML_HELP_result_is_error(result, &msg);
 	);
 
Index: trace/mercury_trace_internal.c
===================================================================
RCS file: /home/mercury1/repository/mercury/trace/mercury_trace_internal.c,v
retrieving revision 1.140
diff -u -b -r1.140 mercury_trace_internal.c
--- trace/mercury_trace_internal.c	15 Sep 2002 10:06:02 -0000	1.140
+++ trace/mercury_trace_internal.c	24 Sep 2002 08:27:13 -0000
@@ -29,6 +29,7 @@
 #include "mercury_trace_readline.h"
 #include "mercury_trace_source.h"
 
+#include "io.mh"
 #include "mdb.browse.mh"
 #include "mdb.browser_info.mh"
 #include "mdb.program_representation.mh"
@@ -111,6 +112,14 @@
 FILE	*MR_mdb_out;
 FILE	*MR_mdb_err;
 
+MR_MercuryFilePtr	MR_mdb_in_mfp;
+MR_MercuryFilePtr	MR_mdb_out_mfp;
+MR_MercuryFilePtr	MR_mdb_err_mfp;
+
+static MR_MercuryFile	MR_mdb_in_mf;
+static MR_MercuryFile	MR_mdb_out_mf;
+static MR_MercuryFile	MR_mdb_err_mf;
+
 static	MR_Trace_Print_Level	MR_default_print_level = MR_PRINT_LEVEL_SOME;
 
 /*
@@ -677,6 +686,17 @@
 		/* Ensure that MR_mdb_err is not buffered */
 		setvbuf(MR_mdb_err, NULL, _IONBF, 0);
 
+		MR_mercuryfile_init(MR_mdb_in, "<mdb input>", 1,
+			&MR_mdb_in_mf);
+		MR_mercuryfile_init(MR_mdb_out, "<mdb output>", 1,
+			&MR_mdb_out_mf);
+		MR_mercuryfile_init(MR_mdb_err, "<mdb errors>", 1,
+			&MR_mdb_err_mf);
+
+		MR_mdb_in_mfp  = &MR_mdb_in_mf;
+		MR_mdb_out_mfp = &MR_mdb_out_mf;
+		MR_mdb_err_mfp = &MR_mdb_err_mf;
+
 		if (getenv("MERCURY_SUPPRESS_MDB_BANNER") == NULL) {
 			fprintf(MR_mdb_out, MR_trace_banner, MR_VERSION);
 		}
@@ -919,6 +939,11 @@
 		    goto parent_error;
 		}
 
+		/*
+		** MR_mdb_in, MR_mdb_out and MR_mdb_err will be put into
+		** MR_MercuryFile structures by our caller.
+		*/
+
 		MR_have_mdb_window = MR_TRUE;
 		MR_mdb_window_pid = child_pid;
 		return MR_TRUE;
@@ -2162,8 +2187,7 @@
 			sprintf(buf, "\nWhich do you want to put "
 				"a breakpoint on (0-%d or *)? ",
 				matches.match_proc_next - 1);
-			line2 = MR_trace_getline(buf,
-				MR_mdb_in, MR_mdb_out);
+			line2 = MR_trace_getline(buf, MR_mdb_in, MR_mdb_out);
 			if (line2 == NULL) {
 				/* This means the user input EOF. */
 				fprintf(MR_mdb_out, "none of them\n");
@@ -3534,7 +3558,7 @@
 	MR_Code **jumpaddr)
 {
 	MR_Trace_Mode	trace_mode;
-	const char	*filename;
+	char		*filename;
 
 	if (word_count > 2) {
 		fflush(MR_mdb_out);
@@ -3543,10 +3567,10 @@
 	} else {
 		if (word_count == 2) {
 			trace_mode = MR_TRACE_DECL_DEBUG_DUMP;
-			filename = (const char *) words[1];
+			filename = words[1];
 		} else {
 			trace_mode = MR_TRACE_DECL_DEBUG_DEBUG;
-			filename = (const char *) NULL;
+			filename = NULL;
 		}
 
 		if (MR_trace_start_decl_debug(trace_mode, filename,
@@ -4666,7 +4690,13 @@
 		** We arrange things so we don't have to treat this case
 		** specially in the command interpreter.
 		*/
+		if (MR_trace_ask_about_eof) {
 		line = MR_copy_string("quit");
+		} else {
+			line = MR_copy_string("quit -y");
+			/* terminate the line containing the last prompt */
+			fprintf(mdb_out, "\n");
+		}
 		return line;
 	}
 
Index: trace/mercury_trace_internal.h
===================================================================
RCS file: /home/mercury1/repository/mercury/trace/mercury_trace_internal.h,v
retrieving revision 1.16
diff -u -b -r1.16 mercury_trace_internal.h
--- trace/mercury_trace_internal.h	6 Mar 2002 14:35:05 -0000	1.16
+++ trace/mercury_trace_internal.h	24 Sep 2002 15:09:43 -0000
@@ -8,7 +8,9 @@
 #define	MERCURY_TRACE_INTERNAL_H
 
 #include "mercury_conf.h"	/* for MR_USE_DECLARATIVE_DEBUGGER */
+#include "mercury_regs.h"		/* must be before most other headers */
 #include "mercury_types.h"	/* for MR_Code */
+#include "mercury_library_types.h"	/* for MR_MercuryFilePtr */
 #include "mercury_trace.h"	/* for MR_Event_Info, etc. */
 #include "mercury_std.h"	/* for MR_bool */
 #include "mercury_trace_completion.h"	/* for MR_Make_Completer */
@@ -55,14 +57,19 @@
 ** Debugger I/O streams.
 ** Replacements for stdin/stdout/stderr respectively.
 **
-** The distinction between MR_mdb_out and MR_mdb_err is analagous to
+** The distinction between MR_mdb_out and MR_mdb_err is analogous to
 ** the distinction between stdout and stderr: ordinary output, including
 ** information messages about conditions which are not errors, should
 ** go to MR_mdb_out, but error messages should go to MR_mdb_err.
 */
+
 extern FILE *MR_mdb_in;
 extern FILE *MR_mdb_out;
 extern FILE *MR_mdb_err;
+
+extern	MR_MercuryFilePtr	MR_mdb_in_mfp;
+extern	MR_MercuryFilePtr	MR_mdb_out_mfp;
+extern	MR_MercuryFilePtr	MR_mdb_err_mfp;
 
 /*
 ** This just prints to MR_mdb_out a message telling the user
Index: trace/mercury_trace_util.c
===================================================================
RCS file: /home/mercury1/repository/mercury/trace/mercury_trace_util.c,v
retrieving revision 1.8
diff -u -b -r1.8 mercury_trace_util.c
--- trace/mercury_trace_util.c	18 Feb 2002 07:01:32 -0000	1.8
+++ trace/mercury_trace_util.c	9 Sep 2002 00:23:36 -0000
@@ -17,12 +17,6 @@
 
 #include <ctype.h>
 
-void
-MR_c_file_to_mercury_file(FILE *c_file, MercuryFile *mercury_file)
-{
-	MR_mercuryfile_init(c_file, 1, mercury_file);
-}
-
 MR_bool
 MR_trace_is_number(const char *word, int *value)
 {
Index: trace/mercury_trace_util.h
===================================================================
RCS file: /home/mercury1/repository/mercury/trace/mercury_trace_util.h,v
retrieving revision 1.8
diff -u -b -r1.8 mercury_trace_util.h
--- trace/mercury_trace_util.h	22 Jul 2002 07:13:14 -0000	1.8
+++ trace/mercury_trace_util.h	9 Sep 2002 02:34:09 -0000
@@ -17,7 +17,6 @@
 
 #include "mercury_std.h"		/* for MR_bool        */
 #include "mercury_types.h"		/* for MR_Word etc */
-#include "mercury_library_types.h"	/* for MercuryFile */
 #include <stdio.h>			/* for FILE        */
 
 /*
@@ -49,13 +48,6 @@
 		MR_save_transient_registers();				\
 		MR_io_tabling_enabled = saved_io_enabled;		\
 	} while (0)
-
-/*
-** MR_c_file_to_mercury_file is used to convert MR_mdb_in and MR_mdb_out
-** into Mercury streams suitable for use by the browser.
-*/
-extern	void	MR_c_file_to_mercury_file(FILE *c_file,
-			MercuryFile *mercury_file);
 
 /*
 ** MR_trace_is_number checks whether the given word contains a natural number,
Index: trace/mercury_trace_vars.c
===================================================================
RCS file: /home/mercury1/repository/mercury/trace/mercury_trace_vars.c,v
retrieving revision 1.49
diff -u -b -r1.49 mercury_trace_vars.c
--- trace/mercury_trace_vars.c	9 Sep 2002 05:52:18 -0000	1.49
+++ trace/mercury_trace_vars.c	25 Sep 2002 00:16:10 -0000
@@ -18,11 +18,13 @@
 #include "mercury_imp.h"
 #include "mercury_array_macros.h"
 #include "mercury_memory.h"
+#include "mercury_builtin_types.h"
 #include "mercury_layout_util.h"
 #include "mercury_deconstruct.h"
 #include "mercury_stack_layout.h"
 #include "mercury_trace_util.h"
 #include "mercury_trace_vars.h"
+#include "io.mh"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -145,67 +147,27 @@
 ** do not export them. The types are a lie, but a safe lie.
 */
 
-#ifndef MR_HIGHLEVEL_CODE
-  extern struct MR_TypeCtorInfo_Struct
-    mercury_data_private_builtin__type_ctor_info_type_info_1;
-  extern struct MR_TypeCtorInfo_Struct
-    mercury_data_private_builtin__type_ctor_info_type_ctor_info_1;
-  extern struct MR_TypeCtorInfo_Struct
-    mercury_data_private_builtin__type_ctor_info_typeclass_info_1;
-  extern struct MR_TypeCtorInfo_Struct
-    mercury_data_private_builtin__type_ctor_info_base_typeclass_info_1;
-  extern struct MR_TypeCtorInfo_Struct
-    mercury_data_std_util__type_ctor_info_type_desc_0;
-  extern struct MR_TypeCtorInfo_Struct
-    mercury_data_std_util__type_ctor_info_type_ctor_desc_0;
-  extern struct MR_TypeCtorInfo_Struct
-    mercury_data_type_desc__type_ctor_info_type_desc_0;
-  extern struct MR_TypeCtorInfo_Struct
-    mercury_data_type_desc__type_ctor_info_type_ctor_desc_0;
-  extern struct MR_TypeCtorInfo_Struct
-    mercury_data_builtin__type_ctor_info_func_0;
-  extern struct MR_TypeCtorInfo_Struct
-    mercury_data_builtin__type_ctor_info_pred_0;
-  extern struct MR_TypeCtorInfo_Struct
-    mercury_data_builtin__type_ctor_info_void_0;
-
-  #ifdef MR_NATIVE_GC
-    extern struct MR_TypeCtorInfo_Struct
-    mercury_data_builtin__type_ctor_info_succip_0;
-    extern struct MR_TypeCtorInfo_Struct
-    mercury_data_builtin__type_ctor_info_hp_0;
-    extern struct MR_TypeCtorInfo_Struct
-    mercury_data_builtin__type_ctor_info_curfr_0;
-    extern struct MR_TypeCtorInfo_Struct
-    mercury_data_builtin__type_ctor_info_maxfr_0;
-    extern struct MR_TypeCtorInfo_Struct
-    mercury_data_builtin__type_ctor_info_redoip_0;
-    extern struct MR_TypeCtorInfo_Struct
-    mercury_data_builtin__type_ctor_info_redofr_0;
-  #endif
-#endif
-
 static  MR_TypeCtorInfo
 MR_trace_always_ignored_type_ctors[] =
 {
-#ifndef MR_HIGHLEVEL_CODE
     /* we ignore these until the browser can handle their varying arity, */
     /* or their definitions are updated. XXX */
-    &mercury_data_private_builtin__type_ctor_info_typeclass_info_1,
-    &mercury_data_private_builtin__type_ctor_info_base_typeclass_info_1,
+    &MR_TYPE_CTOR_INFO_NAME(private_builtin, typeclass_info, 1),
+    &MR_TYPE_CTOR_INFO_NAME(private_builtin, base_typeclass_info, 1),
 
     /* we ignore these because they should never be needed */
-    &mercury_data_builtin__type_ctor_info_void_0,
+    &MR_TYPE_CTOR_INFO_NAME(builtin, void, 0),
 
-  #ifdef MR_NATIVE_GC
+#ifndef MR_HIGHLEVEL_CODE
     /* we ignore these because they are not interesting */
-    &mercury_data_builtin__type_ctor_info_succip_0,
-    &mercury_data_builtin__type_ctor_info_hp_0,
-    &mercury_data_builtin__type_ctor_info_curfr_0,
-    &mercury_data_builtin__type_ctor_info_maxfr_0,
-    &mercury_data_builtin__type_ctor_info_redoip_0,
-    &mercury_data_builtin__type_ctor_info_redofr_0,
-  #endif
+    &MR_TYPE_CTOR_INFO_NAME(builtin, succip, 0),
+    &MR_TYPE_CTOR_INFO_NAME(builtin, hp, 0),
+    &MR_TYPE_CTOR_INFO_NAME(builtin, curfr, 0),
+    &MR_TYPE_CTOR_INFO_NAME(builtin, maxfr, 0),
+    &MR_TYPE_CTOR_INFO_NAME(builtin, redofr, 0),
+    &MR_TYPE_CTOR_INFO_NAME(builtin, redoip, 0),
+    &MR_TYPE_CTOR_INFO_NAME(builtin, trailptr, 0),
+    &MR_TYPE_CTOR_INFO_NAME(builtin, ticket, 0),
 #endif
     /* dummy member */
     NULL
@@ -214,14 +176,12 @@
 static  MR_TypeCtorInfo
 MR_trace_maybe_ignored_type_ctors[] =
 {
-#ifndef MR_HIGHLEVEL_CODE
     /*
     ** We can print values of these types (after a fashion),
     ** but users are usually not interested in their values.
     */
-    &mercury_data_private_builtin__type_ctor_info_type_info_1,
-    &mercury_data_private_builtin__type_ctor_info_type_ctor_info_1,
-#endif
+    &MR_TYPE_CTOR_INFO_NAME(private_builtin, type_info, 1),
+    &MR_TYPE_CTOR_INFO_NAME(private_builtin, type_ctor_info, 1),
     /* dummy member */
     NULL
 };
@@ -306,6 +266,11 @@
     }
 }
 
+MR_DECLARE_TYPE_CTOR_INFO_STRUCT(MR_TYPE_CTOR_INFO_NAME(io, input_stream, 0));
+MR_DECLARE_TYPE_CTOR_INFO_STRUCT(MR_TYPE_CTOR_INFO_NAME(io, output_stream, 0));
+MR_DECLARE_TYPE_CTOR_INFO_STRUCT(MR_TYPE_CTOR_INFO_NAME(io, stream, 0));
+MR_DECLARE_TYPE_CTOR_INFO_STRUCT(MR_TYPE_CTOR_INFO_NAME(io, stream_rep, 0));
+
 const char *
 MR_trace_set_level_from_layout(const MR_Label_Layout *level_layout,
     MR_Word *base_sp, MR_Word *base_curfr, int ancestor_level,
@@ -319,6 +284,7 @@
     MR_TypeInfo             *type_params;
     MR_Word                 value;
     MR_TypeInfo             type_info;
+    MR_TypeCtorInfo         type_ctor_info;
     MR_PseudoTypeInfo       pseudo_type_info;
     int                     i;
     int                     slot;
@@ -457,6 +423,26 @@
         {
             /* this value is not a variable */
             continue;
+        }
+
+        type_ctor_info = MR_TYPEINFO_GET_TYPE_CTOR_INFO(type_info);
+
+        if (type_ctor_info == &MR_TYPE_CTOR_INFO_NAME(io, input_stream, 0)
+        ||  type_ctor_info == &MR_TYPE_CTOR_INFO_NAME(io, output_stream, 0)
+        ||  type_ctor_info == &MR_TYPE_CTOR_INFO_NAME(io, stream, 0))
+        {
+            MR_MercuryFilePtr   mfp;
+            MR_Word             stream_rep;
+            MR_Integer          stream_id;
+            MR_String           stream_name;
+
+            mfp = (MR_MercuryFilePtr) value;
+            stream_id = MR_mf_id(*mfp);
+            MR_make_aligned_string_copy(stream_name, MR_mf_name(*mfp));
+            ML_io_make_stream_rep(stream_id, stream_name, &stream_rep);
+            type_info = (MR_TypeInfo)
+                &MR_TYPE_CTOR_INFO_NAME(io, stream_rep, 0);
+            value = stream_rep;
         }
 
         MR_point.MR_point_vars[slot].MR_var_hlds_number = var_num;
cvs diff: Diffing util
cvs diff: Diffing vim
cvs diff: Diffing vim/after
cvs diff: Diffing vim/ftplugin
cvs diff: Diffing vim/syntax
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list