[m-dev.] for review: add support for dumping the MLDS

Fergus Henderson fjh at cs.mu.OZ.AU
Tue May 9 23:06:04 AEST 2000


I wrote some code to add support for dumping the MLDS, using Ralph's
new pprint module.  Unfortunately, however, it only works for extremly
trivial examples (e.g. samples/hello.m), due to performance bugs in
library/pprint.m.  For larger examples (e.g. even samples/calculator.m),
it takes a very long time and overflows the det stack.
The problem is lots and lots of recursive calls to pprint:be.
It may be related to the following comment in pprint.m:

    % XXX We could do with a spot of laziness to avoid exponential
    % run-times in the worst case here.  The problem is that flatten/1
    % need only be evaluated to the point where it can be decided
    % (by better/4 and fits/2) whether a structure is going to fit on
    % the remainder of the line or not.  In practice, eagerness doesn't
    % seem to be a problem.

Should I go ahead and commit this now?
Or should I wait until we solve the performance problem in pprint?

----------

Estimated hours taken: 1

Add support for dumping the MLDS.

XXX Currently this only works for extremly trivial examples
(e.g. samples/hello.m, but not samples/calculator.m),
due to performance bugs in library/pprint.m.

compiler/options.m:
	Add a new option `--dump-mlds'.
	Also delete the unused `--verbose_hlds_dump' option,
	and allow `-H' as an alias for `--high-level-code'.

compiler/mercury_compile.m:
	Implement the new `--dump-mlds' option,
	using the pretty printer in library/pprint.m.

Workspace: /home/pgrad/fjh/ws/hg
Index: compiler/mercury_compile.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mercury_compile.m,v
retrieving revision 1.158
diff -u -d -r1.158 mercury_compile.m
--- compiler/mercury_compile.m	2000/05/06 06:40:29	1.158
+++ compiler/mercury_compile.m	2000/05/09 11:18:00
@@ -27,7 +27,7 @@
 	% library modules
 :- import_module int, list, map, set, std_util, dir, require, string, bool.
 :- import_module library, getopt, set_bbbtree, term, varset.
-:- import_module gc.
+:- import_module pprint, gc.
 
 	%
 	% the main compiler passes (mostly in order of execution)
@@ -2244,6 +2244,7 @@
 	ml_code_gen(HLDS, MLDS0),
 	maybe_write_string(Verbose, "% done.\n"),
 	maybe_report_stats(Stats),
+	mercury_compile__maybe_dump_mlds(MLDS0, "0", "initial"),
 
 	% XXX this pass should be conditional on a compilation option
 
@@ -2251,6 +2252,7 @@
 	ml_mark_tailcalls(MLDS0, MLDS1),
 	maybe_write_string(Verbose, "% done.\n"),
 	maybe_report_stats(Stats),
+	mercury_compile__maybe_dump_mlds(MLDS1, "1", "tailcalls"),
 
 	globals__io_lookup_bool_option(gcc_nested_functions, NestedFuncs),
 	( { NestedFuncs = no } ->
@@ -2262,11 +2264,13 @@
 	),
 	maybe_write_string(Verbose, "% done.\n"),
 	maybe_report_stats(Stats),
+	mercury_compile__maybe_dump_mlds(MLDS2, "2", "nested_funcs"),
 
 	maybe_write_string(Verbose, "% Generating RTTI data...\n"),
 	{ mercury_compile__mlds_gen_rtti_data(HLDS, MLDS2, MLDS) },
 	maybe_write_string(Verbose, "% done.\n"),
-	maybe_report_stats(Stats).
+	maybe_report_stats(Stats),
+	mercury_compile__maybe_dump_mlds(MLDS, "3", "rtti").
 
 :- pred mercury_compile__mlds_gen_rtti_data(module_info, mlds, mlds).
 :- mode mercury_compile__mlds_gen_rtti_data(in, in, out) is det.
@@ -2762,6 +2766,57 @@
 		report_error(ErrorMessage)
 	).
 
+:- pred mercury_compile__maybe_dump_mlds(mlds, string, string,
+	io__state, io__state).
+:- mode mercury_compile__maybe_dump_mlds(in, in, in, di, uo) is det.
+
+mercury_compile__maybe_dump_mlds(MLDS, StageNum, StageName) -->
+	globals__io_lookup_accumulating_option(dump_mlds, DumpStages),
+	(
+		{
+			list__member(StageNum, DumpStages)
+		;
+			list__member(StageName, DumpStages)
+		;
+			list__member("all", DumpStages)
+		;
+			string__append("0", StrippedStageNum, StageNum),
+			list__member(StrippedStageNum, DumpStages)
+		}
+	->
+		{ ModuleName = mlds__get_module_name(MLDS) },
+		module_name_to_file_name(ModuleName, ".mlds_dump", yes,
+			BaseFileName),
+		{ string__append_list(
+			[BaseFileName, ".", StageNum, "-", StageName],
+			DumpFile) },
+		mercury_compile__dump_mlds(DumpFile, MLDS)
+	;
+		[]
+	).
+
+:- pred mercury_compile__dump_mlds(string, mlds, io__state, io__state).
+:- mode mercury_compile__dump_mlds(in, in, di, uo) is det.
+
+mercury_compile__dump_mlds(DumpFile, MLDS) -->
+	globals__io_lookup_bool_option(verbose, Verbose),
+	globals__io_lookup_bool_option(statistics, Stats),
+	maybe_write_string(Verbose, "% Dumping out MLDS to `"),
+	maybe_write_string(Verbose, DumpFile),
+	maybe_write_string(Verbose, "'..."),
+	maybe_flush_output(Verbose),
+	io__tell(DumpFile, Res),
+	( { Res = ok } ->
+		pprint__write(80, pprint__to_doc(MLDS)),
+		io__told,
+		maybe_write_string(Verbose, " done.\n"),
+		maybe_report_stats(Stats)
+	;
+		maybe_write_string(Verbose, "\n"),
+		{ string__append_list(["can't open file `",
+			DumpFile, "' for output."], ErrorMessage) },
+		report_error(ErrorMessage)
+	).
 
 :- pred mercury_compile__maybe_dump_rl(list(rl_proc), module_info,
 		string, string, io__state, io__state).
Index: compiler/options.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/options.m,v
retrieving revision 1.279
diff -u -d -r1.279 options.m
--- compiler/options.m	2000/04/18 05:08:46	1.279
+++ compiler/options.m	2000/05/09 11:34:59
@@ -110,7 +110,7 @@
 		;	dump_hlds
 		;	dump_hlds_alias
 		;	dump_hlds_options
-		;	verbose_dump_hlds
+		;	dump_mlds
 		;	generate_schemas
 		;	dump_rl
 		;	dump_rl_bytecode
@@ -481,7 +481,7 @@
 	dump_hlds		-	accumulating([]),
 	dump_hlds_alias		-	string(""),
 	dump_hlds_options	-	string(""),
-	verbose_dump_hlds	-	string(""),
+	dump_mlds		-	accumulating([]),
 	dump_rl			-	bool(no),
 	dump_rl_bytecode	-	bool(no),
 	generate_schemas	-	bool(no)
@@ -744,6 +744,7 @@
 short_option('E', 			verbose_errors).
 short_option('G', 			convert_to_goedel).
 short_option('h', 			help).
+short_option('H', 			highlevel_code).
 short_option('i', 			make_interface).
 short_option('I', 			search_directories).
 short_option('l', 			link_libraries).
@@ -850,7 +851,7 @@
 long_option("dump-hlds",		dump_hlds).
 long_option("dump-hlds-alias",		dump_hlds_alias).
 long_option("dump-hlds-options",	dump_hlds_options).
-long_option("verbose-dump-hlds",	verbose_dump_hlds).
+long_option("dump-mlds",		dump_mlds).
 long_option("dump-rl",			dump_rl).
 long_option("dump-rl-bytecode",		dump_rl_bytecode).
 long_option("generate-schemas",		generate_schemas).
@@ -1626,7 +1627,7 @@
 		"--show-dependency-graph",
 		"\tWrite out the dependency graph to `<module>.dependency_graph'.",
 		"-d <n>, --dump-hlds <stage number or name>",
-		"\tDump the HLDS (intermediate representation) after",
+		"\tDump the HLDS (high level intermediate representation) after",
 		"\tthe specified stage to `<module>.hlds_dump.<num>-<name>'.",
 		"\tStage numbers range from 1-99.",
 		"\tMultiple dump options accumulate.",
@@ -1640,6 +1641,11 @@
 		"\tEach type of detail is included in the dump if its",
 		"\tcorresponding letter occurs in the option argument",
 		"\t(see the Mercury User's Guide for details).",
+		"--dump-mlds <stage number or name>",
+		"\tDump the MLDS (medium level intermediate representation) after",
+		"\tthe specified stage to `<module>.mlds_dump.<num>-<name>'.",
+		"\tStage numbers range from 1-99.",
+		"\tMultiple dump options accumulate.",
 		"--dump-rl",
 		"\tOutput a human readable form of the compiler's internal",
 		"\trepresentation of the generated Aditi-RL code to",

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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