[m-rev.] diff: fix hlc* grades non-portability

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Nov 4 17:24:40 AEDT 2002


On 23-Oct-2002, Fergus Henderson <fjh at cs.mu.OZ.AU> wrote:
> 
> Fix a bug where we were generating non-portable casts in static initializers.
> This bug broke tests/valid/recursive_no_tag_type when compiled in an hlc*
> grade using lcc.
> 
> runtime/mercury.h:
> 	Define a new macro MR_CAST_TO_BOX.
> 	For GCC, this does a double-cast (MR_Box) (MR_Word),
> 	which is used to avoid some spurious warnings from GCC,
> 	but for other compilers we just cast directly to (MR_Box).
> 
> compiler/mlds_to_c.m:
> 	Generate code which uses the new macro, rather than
> 	unconditionally emitting the double-cast.

Actually it turned out that that fix was wrong, so I didn't end up
committing it.  Instead, I will commit the following patch, presuming
it passes a boostrap test.

----------

Estimated hours taken: 2
Branches: main

Fix a bug where we were generating non-portable casts in static initializers.
This bug broke tests/valid/recursive_no_tag_type when compiled in an hlc*
grade using lcc.

compiler/mlds_to_c.m:
	Avoid casting addresses to other types before casting them to MR_Box;
	instead, cast them directly to MR_Box without any intermediate casts.

Workspace: /home/mars/fjh/ws1/mercury
Index: compiler/mlds_to_c.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_c.m,v
retrieving revision 1.139
diff -u -d -r1.139 mlds_to_c.m
--- compiler/mlds_to_c.m	4 Oct 2002 10:02:30 -0000	1.139
+++ compiler/mlds_to_c.m	4 Nov 2002 06:03:06 -0000
@@ -3194,9 +3194,13 @@
 		mlds_output_rval(Exprn)
 	;
 		{ Exprn = unop(cast(OtherType), InnerExprn) },
-		{ Type = OtherType }
+		( { Type = OtherType }
+		; { is_an_address(InnerExprn) }
+		)
 	->
 		% avoid unnecessary double-casting -- strip away the inner cast
+		% This is necessary for ANSI/ISO C conformance, to avoid
+		% casts from pointers to integers in static initializers.
 		mlds_output_boxed_rval(Type, InnerExprn)
 	;
 		{ Type = mlds__mercury_type(term__functor(term__atom("float"),
@@ -3226,6 +3230,18 @@
 		mlds_output_rval(Exprn),
 		io__write_string("))")
 	).
+
+% Succeed if the specified rval is an address
+% (possibly tagged and/or cast to a different type).
+:- pred is_an_address(mlds__rval::in) is semidet.
+is_an_address(mkword(_Tag, Expr)) :-
+	is_an_address(Expr).
+is_an_address(unop(cast(_), Expr)) :-
+	is_an_address(Expr).
+is_an_address(mem_addr(_)).
+is_an_address(const(null(_))).
+is_an_address(const(code_addr_const(_))).
+is_an_address(const(data_addr_const(_))).
 
 :- pred mlds_output_unboxed_rval(mlds__type, mlds__rval, io__state, io__state).
 :- mode mlds_output_unboxed_rval(in, in, di, uo) is det.

-- 
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