[m-rev.] for review: fix interactive queries

Simon Taylor stayl at cs.mu.OZ.AU
Wed Jun 12 18:04:13 AEST 2002


Estimated hours taken: 2.5
Branches: main

Recently I changed the interactive query facility in the debugger
to use `mmc --make' to build the shared library containing the query. 
There were two problems with this. The entire program was included in
the shared library, not just the query module. Also, invoking
`mmc --make query.realclean' cleaned up the user's files. 

compiler/options.m:
compiler/compile_target_code.m:
	Add an option `--compile-to-shared-lib', which causes
	mmc to package the modules on the command line into
	a shared library, not an executable. This option is
	intended only for use by browser/interactive_query.m,
	so it isn't documented.

browser/interactive_query.m:
	Use `mmc --compile-to-shared-lib', not `mmc --make'
	to build the query shared library.

	Name the query source file mdb_query.m rather than query.m
	to reduce the chance of overwriting a user's file.

tests/debugger/Mmakefile:
	Use `lmc' to compile the query if `WORKSPACE' is set,
	rather than using a Mercury.options file to set up
	for the workspace.

tests/debugger/interactive.exp:
	Update expected output.

Index: browser/interactive_query.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/interactive_query.m,v
retrieving revision 1.14
diff -u -u -r1.14 interactive_query.m
--- browser/interactive_query.m	29 Apr 2002 08:22:00 -0000	1.14
+++ browser/interactive_query.m	12 Jun 2002 07:57:19 -0000
@@ -8,11 +8,12 @@
 % author: fjh
 % A module to invoke interactive queries using dynamic linking.
 %
-% This module reads in a query, writes out Mercury code for it to `query.m',
-% invokes the Mercury compiler mmc to compile `query.m' to `libquery.so',
-% dynamically loads in the object code for the module `query'
-% from the file `libquery.so', looks up the address of the
-% procedure query/2 in that module, and then calls that procedure.
+% This module reads in a query, writes out Mercury code for it to
+% `mdb_query.m', invokes the Mercury compiler mmc to compile `query.m'
+% to `libmdb_query.so', dynamically loads in the object code for the module
+% `mdb_query' from the file `libmdb_query.so', looks up the address of the
+% procedure query/2 in that module, calls that procedure, and then
+% cleans up the generated files.
 
 :- module mdb__interactive_query.
 :- interface.
@@ -166,7 +167,7 @@
 :- pred run_query(options, prog, io__state, io__state).
 :- mode run_query(in, in, di, uo) is det.
 run_query(Options, Program) -->
-	{ SourceFile = "query.m" },
+	{ SourceFile = query_module_name ++ ".m" },
 	io__get_environment_var("MERCURY_OPTIONS", MAYBE_MERCURY_OPTIONS),
 	(if { MAYBE_MERCURY_OPTIONS = yes(MERCURY_OPTIONS) } then	
 		io__set_environment_var("MERCURY_OPTIONS", ""),
@@ -223,7 +224,7 @@
 
 write_prog_to_stream(prog(QueryType, Imports, Term, VarSet)) -->
 	io__write_string("
-			:- module query.
+			:- module mdb_query.
 			:- interface.
 			:- import_module io.
 			:- pred run(io__state::di, io__state::uo) is cc_multi.
@@ -431,18 +432,25 @@
 		"--no-warn-simple-code --no-warn-det-decls-too-lax ",
 		"--output-compile-error-lines 10000 ",
 		"--link-flags --allow-undefined ", Options,
-		" --make libquery.so"], Command) },
+		" --compile-to-shared-lib ", query_module_name],
+		Command) },
 	invoke_system_command(Command, Succeeded).
 
 :- pred cleanup_query(options, state, state).
 :- mode cleanup_query(in, di, uo) is det.
 
-cleanup_query(Options) -->
-	invoke_system_command(
-		string__append_list(["mmc --grade ", grade_option, " ",
-			Options, " --make query.realclean"]),
+cleanup_query(_Options) -->
+	io__remove_file(query_module_name ++ ".m", _),
+	io__remove_file(query_module_name ++ ".d", _),
+	io__remove_file("Mercury/ds/" ++ query_module_name ++ ".d", _),
+	io__remove_file(query_module_name ++ ".c", _),
+	io__remove_file("Mercury/cs/" ++ query_module_name ++ ".c", _),
+	io__remove_file(query_module_name ++ ".c_date", _),
+	io__remove_file("Mercury/c_dates/" ++ query_module_name ++ ".c_date",
 		_),
-	io__remove_file("query.m", _).
+	io__remove_file(query_module_name ++ ".o", _),
+	io__remove_file("Mercury/os/" ++ query_module_name ++ ".o", _),
+	io__remove_file("lib" ++ query_module_name ++ ".so", _).
 
 :- func grade_option = string.
 %
@@ -492,6 +500,10 @@
 % dynamically load the shared object and execute the query
 %
 
+:- func query_module_name = string.
+
+query_module_name = "mdb_query".
+
 :- pred dynamically_load_and_run(io__state::di, io__state::uo) is det.
 
 dynamically_load_and_run -->
@@ -499,7 +511,8 @@
 	% Load in the object code for the module `query' from
 	% the file `libquery.so'.
 	%
-	dl__open("./libquery.so", lazy, local, MaybeHandle),
+	dl__open("./lib" ++ query_module_name ++ ".so",
+		lazy, local, MaybeHandle),
 	(	
 		{ MaybeHandle = error(Msg) },
 		print("dlopen failed: "), print(Msg), nl
@@ -509,8 +522,8 @@
 		% Look up the address of the first mode (mode number 0)
 		% of the predicate run/2 in the module query.
 		%
-		{ QueryProc = mercury_proc(predicate, unqualified("query"),
-					"run", 2, 0) },
+		{ QueryProc = mercury_proc(predicate,
+				unqualified(query_module_name), "run", 2, 0) },
 		dl__mercury_sym(Handle, QueryProc, MaybeQuery),
 		(
 			{ MaybeQuery = error(Msg) },
Index: compiler/compile_target_code.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/compile_target_code.m,v
retrieving revision 1.11
diff -u -u -r1.11 compile_target_code.m
--- compiler/compile_target_code.m	1 Jun 2002 04:17:51 -0000	1.11
+++ compiler/compile_target_code.m	12 Jun 2002 07:58:10 -0000
@@ -737,22 +737,36 @@
 	( { MakeLibCmdOK = no } ->
     	    { Succeeded = no }
 	;
-	    { list__map(
-	    	(pred(ModuleStr::in, ModuleName::out) is det :-
+	    globals__io_lookup_bool_option(compile_to_shared_lib,
+			CompileToSharedLib),
+	    { TargetType =
+		(CompileToSharedLib = yes -> shared_library ; executable) },
+    	    ( { TargetType = executable } ->
+		{ list__map(
+		    (pred(ModuleStr::in, ModuleName::out) is det :-
 			dir__basename(ModuleStr, ModuleStrBase),
 			file_name_to_module_name(ModuleStrBase, ModuleName)
-		),
-		Modules, ModuleNames) },
-	    { MustCompile = yes },
-	    make_init_obj_file(OutputStream,
-	    	MustCompile, MainModuleName, ModuleNames, InitObjResult),
+		    ),
+		    Modules, ModuleNames) },
+		{ MustCompile = yes },
+		make_init_obj_file(OutputStream, MustCompile, MainModuleName,
+			ModuleNames, InitObjResult)
+	    ;
+	       	{ InitObjResult = yes("") }
+	    ),
 	    (
 	    	{ InitObjResult = yes(InitObjFileName) },
 		globals__io_lookup_accumulating_option(link_objects,
 			ExtraLinkObjectsList),
-	        link(OutputStream, executable, MainModuleName,
-	    	    [InitObjFileName | ObjectsList] ++ ExtraLinkObjectsList,
-		    Succeeded)
+		{ AllObjects0 = ObjectsList ++ ExtraLinkObjectsList },
+		{ AllObjects =
+			( InitObjFileName = "" ->
+				AllObjects0
+			;
+				[InitObjFileName | AllObjects0]
+			) },
+	        link(OutputStream, TargetType, MainModuleName,
+	    	    AllObjects, Succeeded)
 	    ;
 		{ InitObjResult = no },
 		{ Succeeded = no }
Index: compiler/options.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/options.m,v
retrieving revision 1.373
diff -u -u -r1.373 options.m
--- compiler/options.m	25 May 2002 13:25:06 -0000	1.373
+++ compiler/options.m	2 Jun 2002 15:08:20 -0000
@@ -108,6 +108,7 @@
 		;	errorcheck_only
 		;	target_code_only
 		;	compile_only
+		;	compile_to_shared_lib
 		;	aditi_only
 		;	output_grade_string
 	% Auxiliary output options
@@ -664,6 +665,7 @@
 	errorcheck_only		-	bool(no),
 	target_code_only	-	bool(no),
 	compile_only		-	bool(no),
+	compile_to_shared_lib	-	bool(no),
 	aditi_only		-	bool(no),
 	output_grade_string	-	bool(no)
 ]).
@@ -1213,6 +1215,7 @@
 long_option("errorcheck-only",		errorcheck_only).
 long_option("target-code-only",		target_code_only).
 long_option("compile-only",		compile_only).
+long_option("compile-to-shared-lib",	compile_to_shared_lib).
 long_option("aditi-only",		aditi_only).
 long_option("output-grade-string",	output_grade_string).
 
@@ -2244,6 +2247,9 @@
 		"-c, --compile-only",
 		"\tGenerate C code in `<module>.c' and object code in `<module>.o'",
 		"\tbut do not attempt to link the named modules.",
+		% --compile-to-shared-lib is intended only for use
+		% by the debugger's interactive query facility,
+		% so it isn't documented.
 		"--aditi-only",
 		"\tWrite Aditi-RL bytecode to `<module>.rlo' and",
 		"\tdo not compile to C.",
Index: tests/debugger/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/debugger/Mmakefile,v
retrieving revision 1.77
diff -u -u -r1.77 Mmakefile
--- tests/debugger/Mmakefile	8 Jun 2002 17:43:45 -0000	1.77
+++ tests/debugger/Mmakefile	12 Jun 2002 07:51:16 -0000
@@ -304,18 +304,24 @@
 # Note that interactive.out.orig depends on $(interactive.ints) because
 # interactive.inp contains interactive queries that require interactive.ints
 # to have been built.
-interactive.out.orig: Mercury.options.interactive interactive \
-			interactive.inp $(interactive.ints)
+# When WORKSPACE is set, use $(WORKSPACE)/tools/lmc to compile the query.
+ifneq ($(origin WORKSPACE), undefined)
+export WORKSPACE
+endif
+
+interactive.out.orig: interactive $(interactive.ints)
+ifneq ($(origin WORKSPACE),undefined)
+	rm -rf lmc
+	mkdir ./lmc
+	cp $(WORKSPACE)/tools/lmc lmc/mmc
+endif
 	echo "echo on" > interactive.inp.tmp
-	echo mmc_options --options-file Mercury.options.interactive \
-			--trace minimum >> interactive.inp.tmp
+	echo mmc_options $(ALL_MCFLAGS) --trace minimum >> interactive.inp.tmp
 	cat interactive.inp >> interactive.inp.tmp
-	$(MDB) ./interactive < interactive.inp.tmp > interactive.out.orig 2>&1
+	PATH="`pwd`/lmc:$$PATH" $(MDB) ./interactive \
+		< interactive.inp.tmp > interactive.out.orig 2>&1
 	rm -f interactive.inp.tmp
-
-.PHONY: Mercury.options.interactive
-Mercury.options.interactive:
-	$(ECHO_MERCURY_OPTIONS) > Mercury.options.interactive
+	rm -rf lmc
 
 # We pipe the output through sed to avoid differences for `--use-subdirs',
 # and to remove some spurious warnings that `gcc' and `ld' issue.
@@ -332,8 +338,8 @@
 		    -e 's/                 from \/home\/mercury\/public\/.a\/lib\/mercury\/inc\/mercury_wrapper.h:[0-9]*,.//' \
 		    -e '/                 from \/home\/mercury\/public\/.a\/lib\/mercury\/inc\/mercury_imp.h:[0-9]*,$$/N' \
 		    -e 's/                 from \/home\/mercury\/public\/.a\/lib\/mercury\/inc\/mercury_imp.h:[0-9]*,.//' \
-		    -e '/                 from query.c:[0-9]*:$$/N' \
-		    -e 's/                 from query.c:[0-9]*:.//' \
+		    -e '/                 from mdb_query.c:[0-9]*:$$/N' \
+		    -e 's/                 from mdb_query.c:[0-9]*:.//' \
 		    -e '/\/usr\/local\/apps\/gcc-2.95.3\/lib\/gcc-lib\/alpha-dec-osf5.1\/2.95.3\/include\/unistd.h:[0-9]*: warning: .cuserid. redefined$$/N' \
 		    -e 's/\/usr\/local\/apps\/gcc-2.95.3\/lib\/gcc-lib\/alpha-dec-osf5.1\/2.95.3\/include\/unistd.h:[0-9]*: warning: .cuserid. redefined.//' \
 		    -e '/\/usr\/local\/apps\/gcc-2.95.3\/lib\/gcc-lib\/alpha-dec-osf5.1\/2.95.3\/include\/stdio.h:[0-9]*: warning: this is the location of the previous definition$$/N' \
@@ -344,8 +350,8 @@
 		    -e 's/                 from \/home\/mercury\/public\/.a\/lib\/mercury\/inc\/mercury_library_types.h:[0-9]*,.//' \
 		    -e '/                 from \/home\/mercury\/public\/.a\/lib\/mercury\/inc\/mercury_imp.h:[0-9]*,$$/N' \
 		    -e 's/                 from \/home\/mercury\/public\/.a\/lib\/mercury\/inc\/mercury_imp.h:[0-9]*,.//' \
-		    -e '/                 from query.c:[0-9]*:$$/N' \
-		    -e 's/                 from query.c:[0-9]*:.//' \
+		    -e '/                 from mdb_query.c:[0-9]*:$$/N' \
+		    -e 's/                 from mdb_query.c:[0-9]*:.//' \
 		    -e '/\/usr\/local\/apps\/gcc-2.95.3\/lib\/gcc-lib\/alpha-dec-osf5.1\/2.95.3\/include\/va-alpha.h:[0-9]*: warning: redefinition of .va_list.$$/N' \
 		    -e 's/\/usr\/local\/apps\/gcc-2.95.3\/lib\/gcc-lib\/alpha-dec-osf5.1\/2.95.3\/include\/va-alpha.h:[0-9]*: warning: redefinition of .va_list..//' \
 		    -e '/\/usr\/local\/apps\/gcc-2.95.3\/lib\/gcc-lib\/alpha-dec-osf5.1\/2.95.3\/include\/va_list.h:[0-9]*: warning: .va_list. previously declared here$$/N' \
Index: tests/debugger/interactive.exp
===================================================================
RCS file: /home/mercury1/repository/tests/debugger/interactive.exp,v
retrieving revision 1.6
diff -u -u -r1.6 interactive.exp
--- tests/debugger/interactive.exp	5 Jun 2002 16:41:22 -0000	1.6
+++ tests/debugger/interactive.exp	12 Jun 2002 07:38:40 -0000
@@ -121,17 +121,17 @@
 List = [3, 2, 1], true ;
 fail.
 No (more) solutions.
-?- <stdin>:012: In clause for predicate `query:run/2':
+?- <stdin>:012: In clause for predicate `mdb_query:run/2':
 <stdin>:012:   warning: variable `_2' occurs more than once in this scope.
-<stdin>:014: In clause for predicate `query:run/2':
+<stdin>:014: In clause for predicate `mdb_query:run/2':
 <stdin>:014:   warning: variable `_2' occurs more than once in this scope.
-<stdin>:012: In clause for predicate `query:run/2':
+<stdin>:012: In clause for predicate `mdb_query:run/2':
 <stdin>:012:   warning: variable `_2' occurs more than once in this scope.
-<stdin>:015: In clause for predicate `query:run/2':
+<stdin>:015: In clause for predicate `mdb_query:run/2':
 <stdin>:015:   warning: variable `_2' occurs more than once in this scope.
-<stdin>:026: In clause for predicate `query:query/2':
+<stdin>:026: In clause for predicate `mdb_query:query/2':
 <stdin>:026:   warning: variable `_2' occurs more than once in this scope.
-<stdin>:001: In clause for predicate `query:query/2':
+<stdin>:001: In clause for predicate `mdb_query:query/2':
 <stdin>:001:   warning: variable `_2' occurs more than once in this scope.
 <stdin>:026: Inferred :- pred query((list:list(int)), (list:list(int))).
 <stdin>:026: Inferred :- mode query(out, out) is nondet.
@@ -140,24 +140,24 @@
 List = [2, 3, 1], _2 = [3, 1], true ;
 fail.
 No (more) solutions.
-?- <stdin>:012: In clause for predicate `query:run/2':
+?- <stdin>:012: In clause for predicate `mdb_query:run/2':
 <stdin>:012:   warning: variable `_2' occurs more than once in this scope.
-<stdin>:014: In clause for predicate `query:run/2':
+<stdin>:014: In clause for predicate `mdb_query:run/2':
 <stdin>:014:   warning: variable `_2' occurs more than once in this scope.
-<stdin>:012: In clause for predicate `query:run/2':
+<stdin>:012: In clause for predicate `mdb_query:run/2':
 <stdin>:012:   warning: variable `_2' occurs more than once in this scope.
-<stdin>:015: In clause for predicate `query:run/2':
+<stdin>:015: In clause for predicate `mdb_query:run/2':
 <stdin>:015:   warning: variable `_2' occurs more than once in this scope.
-<stdin>:026: In clause for predicate `query:query/2':
+<stdin>:026: In clause for predicate `mdb_query:query/2':
 <stdin>:026:   warning: variable `_2' occurs more than once in this scope.
-<stdin>:001: In clause for predicate `query:query/2':
+<stdin>:001: In clause for predicate `mdb_query:query/2':
 <stdin>:001:   warning: variable `_2' occurs more than once in this scope.
 <stdin>:026: Inferred :- pred query((list:list(int)), (list:list(int))).
 <stdin>:026: Inferred :- mode query(out, out) is nondet.
 qperm([1,2,3], List), List = [4 | _].
 fail.
 No (more) solutions.
-?- <stdin>:001: In clause for predicate `query:query/1':
+?- <stdin>:001: In clause for predicate `mdb_query:query/1':
 <stdin>:001:   in argument 1 of call to predicate `qperm/2':
 <stdin>:001:   in argument 2 of functor `[|]/2':
 <stdin>:001:   in argument 2 of functor `[|]/2':
@@ -167,12 +167,11 @@
 <stdin>:001:   argument has type `int',
 <stdin>:001:   constant `"foo"' has type `string'.
 For more information, try recompiling with `-E'.
-** Error making `query.c'.
 qperm([1,2,"foo"], List).
 Compilation error(s) occurred.
 ?- <stdin>:026: Inferred :- pred query((list:list(int))).
 <stdin>:014: In clause for `run(di, uo)':
-<stdin>:014:   in call to predicate `query:query/1':
+<stdin>:014:   in call to predicate `mdb_query:query/1':
 <stdin>:014:   mode error: arguments `List'
 <stdin>:014:   have insts `free',
 <stdin>:014:   which does not match any of the valid modes for
@@ -182,7 +181,6 @@
 <stdin>:026:   mode error: variable `HeadVar__2' has instantiatedness `free',
 <stdin>:026:   expected instantiatedness was `ground'.
 For more information, try recompiling with `-E'.
-** Error making `query.c'.
 qperm(List, [1]).
 Compilation error(s) occurred.
 ?- quit.
@@ -196,25 +194,25 @@
 <stdin>:017: Inferred :- mode query(out) is nondet.
 qperm([1,2,3], List).
 List = [1, 2, 3], true.
-?- <stdin>:011: In clause for predicate `query:run/2':
+?- <stdin>:011: In clause for predicate `mdb_query:run/2':
 <stdin>:011:   warning: variable `_2' occurs more than once in this scope.
-<stdin>:012: In clause for predicate `query:run/2':
+<stdin>:012: In clause for predicate `mdb_query:run/2':
 <stdin>:012:   warning: variable `_2' occurs more than once in this scope.
-<stdin>:017: In clause for predicate `query:query/2':
+<stdin>:017: In clause for predicate `mdb_query:query/2':
 <stdin>:017:   warning: variable `_2' occurs more than once in this scope.
-<stdin>:001: In clause for predicate `query:query/2':
+<stdin>:001: In clause for predicate `mdb_query:query/2':
 <stdin>:001:   warning: variable `_2' occurs more than once in this scope.
 <stdin>:017: Inferred :- pred query((list:list(int)), (list:list(int))).
 <stdin>:017: Inferred :- mode query(out, out) is nondet.
 qperm([1,2,3], List), List = [2 | _].
 List = [2, 1, 3], _2 = [1, 3], true.
-?- <stdin>:011: In clause for predicate `query:run/2':
+?- <stdin>:011: In clause for predicate `mdb_query:run/2':
 <stdin>:011:   warning: variable `_2' occurs more than once in this scope.
-<stdin>:012: In clause for predicate `query:run/2':
+<stdin>:012: In clause for predicate `mdb_query:run/2':
 <stdin>:012:   warning: variable `_2' occurs more than once in this scope.
-<stdin>:017: In clause for predicate `query:query/2':
+<stdin>:017: In clause for predicate `mdb_query:query/2':
 <stdin>:017:   warning: variable `_2' occurs more than once in this scope.
-<stdin>:001: In clause for predicate `query:query/2':
+<stdin>:001: In clause for predicate `mdb_query:query/2':
 <stdin>:001:   warning: variable `_2' occurs more than once in this scope.
 <stdin>:017: Inferred :- pred query((list:list(int)), (list:list(int))).
 <stdin>:017: Inferred :- mode query(out, out) is nondet.
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list