[m-rev.] for review: fix C foreign_type

Fergus Henderson fjh at cs.mu.OZ.AU
Thu May 30 17:58:11 AEST 2002


On 17-May-2002, Peter Ross <peter.ross at miscrit.be> wrote:
> On Fri, May 17, 2002 at 12:43:49AM +1000, Fergus Henderson wrote:
> > Implement C `pragma foreign_type' properly.
> 
> This looks fine modulo adding some comments.  Feel free to check in.

Unfortunately it didn't pass bootchecking in grade hlc.gc,
due to problems with #line directives inside macro invocations.

So I needed the following additional patch.

compiler/mlds_to_c.m:
	Don't output #line directives for target_code_components
	other than user_target_code.  This is nedded because #line
	directives are not permitted inside invocations of macros
	(such as MR_MAYBE_(UN)BOX_FOREIGN_TYPE).

Index: mlds_to_c.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_c.m,v
retrieving revision 1.128
diff -u -d -u -r1.128 mlds_to_c.m
--- mlds_to_c.m	7 May 2002 11:03:05 -0000	1.128
+++ mlds_to_c.m	30 May 2002 06:26:08 -0000
@@ -2701,6 +2808,17 @@
 		io__state, io__state).
 :- mode mlds_output_target_code_component(in, in, di, uo) is det.
 
+	% Note: `name(Name)' target_code_components are used to
+	% generate the #define for `MR_PROC_LABEL'.
+	% The fact that they're used in a #define means that we can't do
+	% an mlds_to_c__output_context(Context) here, since #line directives
+	% aren't allowed inside #defines.
+	% Similarly, all the target_code_components except user_target_code
+	% can get emitted inside calls to the MR_BOX_FOREIGN_TYPE
+	% or MR_UNBOX_FOREIGN_TYPE macros, which means that we can't output
+	% the contexts for those either, since #line directives aren't
+	% allowed inside macro invocations in standard C
+	% (although some compilers, e.g. gcc 3.2, do allow it).
 mlds_output_target_code_component(Context,
 		user_target_code(CodeString, MaybeUserContext, _Attrs)) -->
 	( { MaybeUserContext = yes(UserContext) } ->
@@ -2709,25 +2827,18 @@
 		mlds_to_c__output_context(Context)
 	),
 	io__write_string(CodeString),
-	io__write_string("\n").
-mlds_output_target_code_component(Context, raw_target_code(CodeString,
+	io__write_string("\n"),
+	mlds_to_c__reset_context.
+mlds_output_target_code_component(_Context, raw_target_code(CodeString,
 		_Attrs)) -->
-	mlds_to_c__output_context(Context),
 	io__write_string(CodeString).
-mlds_output_target_code_component(Context, target_code_input(Rval)) -->
-	mlds_to_c__output_context(Context),
+mlds_output_target_code_component(_Context, target_code_input(Rval)) -->
 	mlds_output_rval(Rval),
 	io__write_string("\n").
-mlds_output_target_code_component(Context, target_code_output(Lval)) -->
-	mlds_to_c__output_context(Context),
+mlds_output_target_code_component(_Context, target_code_output(Lval)) -->
 	mlds_output_lval(Lval),
 	io__write_string("\n").
 mlds_output_target_code_component(_Context, name(Name)) -->
-	% Note: `name(Name)' target_code_components are used to
-	% generate the #define for `MR_PROC_LABEL'.
-	% The fact that they're used in a #define means that we can't do
-	% an mlds_to_c__output_context(Context) here, since #line directives
-	% aren't allowed inside #defines.
 	mlds_output_fully_qualified_name(Name),
 	io__write_string("\n").
 
@@ -3290,6 +3401,12 @@
 	{ term__context_file(ProgContext, FileName) },
 	{ term__context_line(ProgContext, LineNumber) },
 	c_util__set_line_num(FileName, LineNumber).
+
+:- pred mlds_to_c__reset_context(io__state, io__state).
+:- mode mlds_to_c__reset_context(di, uo) is det.
+
+mlds_to_c__reset_context -->
+	c_util__reset_line_num.
 
 :- pred mlds_indent(mlds__context, indent, io__state, io__state).
 :- mode mlds_indent(in, in, di, uo) is det.


I also added the comments you suggested:


--- old2/export.m	Thu May 30 17:48:52 2002
+++ ./export.m	Thu May 30 17:57:28 2002
@@ -344,6 +344,8 @@
 			string__append_list(["\t", C_RetType,
 					" return_value;\n"],
 						MaybeDeclareRetval),
+			% We need to unbox non-word-sized foreign types
+			% before returning them to C code
 			( foreign__is_foreign_type(Export_RetType) = yes ->
 				string__append_list(
 					["\tMR_MAYBE_UNBOX_FOREIGN_TYPE(",
@@ -471,6 +473,8 @@
 		convert_type_to_mercury(ArgName0, Type, ArgName),
 		argloc_to_string(ArgLoc, ArgLocString),
 		Export_Type = foreign__to_exported_type(ModuleInfo, Type),
+		% We need to box non-word-sized foreign types
+		% before passing them to Mercury code
 		( foreign__is_foreign_type(Export_Type) = yes ->
 			C_Type = foreign__to_type_string(c, Export_Type),
 			string__append_list(
@@ -510,6 +514,8 @@
 		argloc_to_string(ArgLoc, ArgLocString0),
 		convert_type_from_mercury(ArgLocString0, Type, ArgLocString),
 		Export_Type = foreign__to_exported_type(ModuleInfo, Type),
+		% We need to unbox non-word-sized foreign types
+		% before returning them to C code
 		( foreign__is_foreign_type(Export_Type) = yes ->
 			C_Type = foreign__to_type_string(c, Export_Type),
 			string__append_list(
--- old2/mlds_to_c.m	Thu May 30 17:53:24 2002
+++ ./mlds_to_c.m	Thu May 30 17:56:15 2002
@@ -740,7 +740,9 @@
 		[]
 	),
 
-	% Generate code to box any foreign_type input parameters
+	% Generate code to box any non-word-sized foreign_type input parameters;
+	% these need to be converted to a uniform size before passing them
+	% to Mercury code.
 	io__write_list(CForeignTypeInputs, "",
 		(pred(Arg::in, di, uo) is det -->
 			{ Arg = mlds__argument(Name, Type, _GC_TraceCode) },
@@ -780,7 +782,8 @@
 		io__write_string(") ")
 	),
 
-	% Generate code to unbox any foreign_type output parameters
+	% Generate code to unbox any foreign_type output parameters,
+	% since we are returning those parameters to C code.
 	io__write_list(CForeignTypeOutputs, "",
 		(pred(Arg::in, di, uo) is det -->
 			{ Arg = mlds__argument(Name, Type, _GC_TraceCode) },
-- 
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