[m-rev.] fix problem with GCC back-end & .mih files

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Sep 30 22:10:08 AEST 2002


Branches: main
Estimated hours taken: 4

Fix a problem where the tests of grade `hlc.gc' with `--target c' were failing
when run using a compiler built in grade `hlc.gc' with `--target asm'.

(XXX Is this the right fix?  Would it be better to just require people
to install libraries with `--target c' if they are going to use those
libraries to build other modules with `--target c'?  This approach is
easier for users, but worse for compilation time & disk usage, because
it creates the .mih files even in cases where they won't be needed.)

compiler/mlds_to_c.m:
	Split the code for output_mlds into two sub-procedures,
	output_c_file and output_header_file, and export these procedures
	for use by mlds_to_gcc.m.

compiler/mlds_to_gcc.m:
	Always call mlds_to_c__output_header_file, so that we always create
	the `.mih' files.  (The conditions for creating the `.c' file are the
	same as before, i.e. only if the module contains C foreign code.)

compiler/modules.m:
	Update the definition of the <module>.mihs Mmake variable to reflect
	the above change.

Workspace: /home/mars/fjh/ws-gcc/mercury
Index: compiler/mlds_to_c.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_c.m,v
retrieving revision 1.138
diff -u -d -r1.138 mlds_to_c.m
--- compiler/mlds_to_c.m	9 Sep 2002 07:42:29 -0000	1.138
+++ compiler/mlds_to_c.m	30 Sep 2002 11:52:32 -0000
@@ -27,15 +27,29 @@
 :- import_module io.
 
 	% output_mlds(MLDS, Suffix):
-	%	Output C code to the appropriate C file and header file.
+	%	Output C code the the appropriate C file and
+	%	C declarations to the appropriate header file.
 	%	The file names are determined by the module name,
 	%	with the specified Suffix appended at the end.
 	%	(The suffix is used for debugging dumps.  For normal
 	%	output, the suffix should be the empty string.)
-	%	
 :- pred mlds_to_c__output_mlds(mlds, string, io__state, io__state).
 :- mode mlds_to_c__output_mlds(in, in, di, uo) is det.
 
+	% output_header_file(MLDS, Suffix):
+	%	Output C declarations for the procedures (etc.) in the
+	%	specified MLDS module to the appropriate .mih header file.
+	%	See output_mlds for the meaning of Suffix.
+:- pred mlds_to_c__output_header_file(mlds, string, io__state, io__state).
+:- mode mlds_to_c__output_header_file(in, in, di, uo) is det.
+
+	% output_c_file(MLDS, Suffix):
+	%	Output C code for the specified MLDS module to the
+	%	appropriate C file.
+	%	See output_mlds for the meaning of Suffix.
+:- pred mlds_to_c__output_c_file(mlds, string, io__state, io__state).
+:- mode mlds_to_c__output_c_file(in, in, di, uo) is det.
+
 	% output an MLDS context in C #line format. 
 	% this is useful for other foreign language interfaces such as
 	% managed extensions for C++.
@@ -56,7 +70,8 @@
 :- import_module backend_libs__rtti.		% for rtti__addr_to_string.
 :- import_module ml_backend__rtti_to_mlds.	% for mlds_rtti_type_name.
 :- import_module hlds__hlds_pred.	% for pred_proc_id.
-:- import_module ml_backend__ml_code_util.	% for ml_gen_public_field_decl_flags, which is
+:- import_module ml_backend__ml_code_util.
+				% for ml_gen_public_field_decl_flags, which is
 				% used by the code that handles derived classes
 :- import_module ml_backend__ml_type_gen.	% for ml_gen_type_name
 :- import_module backend_libs__foreign.
@@ -78,36 +93,43 @@
 %-----------------------------------------------------------------------------%
 
 mlds_to_c__output_mlds(MLDS, Suffix) -->
-	%
-	% We write the header file out to <module>.h.tmp and then
-	% call `update_interface' to move the <module>.h.tmp file to
-	% <module>.h; this avoids updating the timestamp on the `.h'
-	% file if it hasn't changed.
-	% 
 	% We output the source file before outputting the header,
 	% since the Mmake dependencies say the header file depends
 	% on the source file, and so if we wrote them out in the
 	% other order this might lead to unnecessary recompilation
 	% next time Mmake is run.
 	%
+	% XXX at some point we should also handle output of any non-C
+	%     foreign code (Ada, Fortran, etc.) to appropriate files.
+	%
+	output_c_file(MLDS, Suffix),
+	output_header_file(MLDS, Suffix).
+
+mlds_to_c__output_c_file(MLDS, Suffix) -->
+	{ ModuleName = mlds__get_module_name(MLDS) },
+	module_name_to_file_name(ModuleName, ".c" ++ Suffix, yes, SourceFile),
+	{ Indent = 0 },
+	output_to_file(SourceFile, mlds_output_src_file(Indent, MLDS)).
+
+	%
+	% Generate the header file
+	%
+mlds_to_c__output_header_file(MLDS, Suffix) -->
+	%
+	% We write the header file out to <module>.mih.tmp and then
+	% call `update_interface' to move the <module>.mih.tmp file to
+	% <module>.mih; this avoids updating the timestamp on the `.mih'
+	% file if it hasn't changed.
+	% 
 	{ ModuleName = mlds__get_module_name(MLDS) },
-	module_name_to_file_name(ModuleName, ".c" ++ Suffix, yes,
-		SourceFile),
 	module_name_to_file_name(ModuleName, ".mih" ++ Suffix ++ ".tmp",
 		yes, TmpHeaderFile),
 	module_name_to_file_name(ModuleName, ".mih" ++ Suffix, yes,
 		HeaderFile),
 	{ Indent = 0 },
-	output_to_file(SourceFile, mlds_output_src_file(Indent, MLDS)),
 	output_to_file(TmpHeaderFile, mlds_output_hdr_file(Indent, MLDS)),
 	update_interface(HeaderFile).
-	%
-	% XXX at some point we should also handle output of any non-C
-	%     foreign code (Ada, Fortran, etc.) to appropriate files.
 
-	%
-	% Generate the header file
-	%
 :- pred mlds_output_hdr_file(indent, mlds, io__state, io__state).
 :- mode mlds_output_hdr_file(in, in, di, uo) is det.
 
Index: compiler/mlds_to_gcc.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_gcc.m,v
retrieving revision 1.76
diff -u -d -r1.76 mlds_to_gcc.m
--- compiler/mlds_to_gcc.m	16 Sep 2002 19:40:52 -0000	1.76
+++ compiler/mlds_to_gcc.m	30 Sep 2002 11:39:49 -0000
@@ -236,49 +236,57 @@
 		% We only handle C currently, so we just look up C
 	{ ForeignCode = map__lookup(AllForeignCode, c) },
 	(
-		% Check if there is any code from pragma foreign_code,
+		% Check if there is any C code from pragma foreign_code,
 		% pragma export, or pragma foreign_proc declarations.
+		% We only want to generate a `.c' file if there is C foreign
+		% code.
 		%
-		% We don't call mlds_to_c to generate `.c' and `.h' files
-		% if the module contains only `pragma foreign_decls'.
-		% This is needed to avoid calling mlds_to_c when intermodule
+		% We don't generate a `.c' file if the
+		% module contains only `pragma foreign_decls' . This
+		% is needed to avoid generating a `.c' file when intermodule
 		% optimization is enabled and `pragma foreign_decls'
 		% declarations have been read in from the `.opt' files
 		% and have propagated through to the MLDS.
-		% Calling mlds_to_c when the module itself doesn't contain
+		% Creating a C file when the module itself doesn't contain
 		% C code breaks things, since Mmake won't compile and link
 		% in the generated `.c' files, but those files contain the
 		% definition of the `*__init_type_tables()' functions that
 		% are referenced by `*_init.c'.
 		%
 		% XXX This is not quite right, since if the module itself
-		% contains `pragma foreign_decls', the `.h' file might
+		% contains `pragma foreign_decls', the `.c' file might
 		% be needed.  But the Mercury standard library needs
 		% intermodule optimization enabled for `make install'
 		% to work.  A better fix would be to ignore foreign_decls
-		% that were defined in other modules, but to call mlds_to_c
-		% for foreign_decls that were defined in the module that
-		% we're compiling.
+		% that were defined in other modules, but to create the `.c'
+		% file if there are foreign_decls that were defined in the
+		% module that we're compiling.
 		{ ForeignCode = mlds__foreign_code(_Decls, _Imports, [], []) },
 		{ ForeignDefns = [] }
 	->
 		{ ContainsCCode = no },
-		% there's no foreign code, so we don't need to
-		% do anything special
 		{ NeedInitFn = yes }
 	;
+		% XXX currently the only foreign code we handle is C;
+		%     see comments above (at the declaration for
+		%     mlds_to_c__compile_to_asm)
+		{ ContainsCCode = yes },
+		{ NeedInitFn = no },
 		% create a new MLDS containing just the foreign code
 		% (with all definitions made public, so we can use
 		% them from the asm file!) and pass that to mlds_to_c.m
+		% to create the .mih file, and if necessary the .c file.
 		{ ForeignMLDS = mlds(ModuleName, AllForeignCode, Imports,
 			list__map(make_public, ForeignDefns)) },
-		mlds_to_c__output_mlds(ForeignMLDS, ""),
-		% XXX currently the only foreign code we handle is C;
-		%     see comments above (at the declaration for
-		%     mlds_to_c__compile_to_asm)
-		{ ContainsCCode = yes },
-		{ NeedInitFn = no }
+		mlds_to_c__output_c_file(ForeignMLDS, "")
 	),
+	%
+	% Generate the .mih C header file for this module.
+	% We do this regardless of whether the module contains C code,
+	% because this is needed to allow interoperability between modules
+	% compiled with --target asm and --target c.
+	%
+	mlds_to_c__output_header_file(MLDS, ""),
 
 	%
 	% We generate things in this order:
Index: compiler/modules.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/modules.m,v
retrieving revision 1.246
diff -u -d -r1.246 modules.m
--- compiler/modules.m	22 Aug 2002 02:34:13 -0000	1.246
+++ compiler/modules.m	30 Sep 2002 11:47:36 -0000
@@ -3902,16 +3902,17 @@
 	io__write_string(DepStream, ".mihs = "),
 	globals__io_lookup_bool_option(highlevel_code, HighLevelCode),
 	( { HighLevelCode = yes } ->
-		( { Target = asm } ->
-			% For the `--target asm' back-end, we only
-			% generate `.mih' files for modules that
-			% contain C code
-			write_dependencies_list(
-				modules_that_need_headers(Modules, DepsMap),
-				".mih", DepStream)
-		; { Target = c } ->
+		( { Target = c ; Target = asm } ->
 			% For the `--target c' MLDS back-end, we
-			% generate `.mih' files for every module
+			% generate `.mih' files for every module.
+			% Likewise for the `--target asm' back-end.
+			% (For the `--target asm' back-end,
+			% we previously used to do that only for modules
+			% that contain C code, but this caused trouble
+			% when trying to interoperate between compiled
+			% with `--target c' and code compiled with
+			% `--target asm', so now we generate them
+			% unconditionally.)
 			write_compact_dependencies_list(Modules,
 					"$(mihs_subdir)", ".mih",
 					Basis, DepStream)

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