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

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Oct 23 16:39:47 AEST 2002


This patch should fix the last remaining lcc test failure.

Estimated hours taken: 0.75
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.

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.

Workspace: /home/ceres/fjh/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	23 Oct 2002 06:27:58 -0000
@@ -3215,10 +3215,13 @@
 		; Type = mlds__native_int_type
 		}
 	->
-		% We cast first to MR_Word, and then to MR_Box.
+		% For GCC, we cast first to MR_Word, and then to MR_Box.
 		% This is done to avoid spurious warnings about "cast from
 		% integer to pointer of different size" from gcc.
-		io__write_string("((MR_Box) (MR_Word) ("),
+		% But it's not safe to do this for non-GCC compilers,
+		% since ISO C does not allow casting from pointer types
+		% to ints in static initializers.  Hence we use a macro.
+		io__write_string("(MR_CAST_TO_BOX("),
 		mlds_output_rval(Exprn),
 		io__write_string("))")
 	;
Index: runtime/mercury.h
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury.h,v
retrieving revision 1.65
diff -u -d -r1.65 mercury.h
--- runtime/mercury.h	29 Sep 2002 09:38:41 -0000	1.65
+++ runtime/mercury.h	23 Oct 2002 06:31:46 -0000
@@ -297,6 +297,20 @@
 		}							\
 	} while (0)
 
+/*
+** For GCC, we sometimes cast first to MR_Word, and then to MR_Box.
+** This is done to avoid spurious warnings about "cast from
+** integer to pointer of different size" from GCC.
+** But it's not safe to do this for non-GCC C compilers,
+** since ISO C does not allow casting from pointer types
+** to ints in static initializers.  Hence we use this macro.
+*/
+#ifdef __GNUC__
+  #define MR_CAST_TO_BOX(e) ((MR_Box) (MR_Word) (e))
+#else
+  #define MR_CAST_TO_BOX(e) ((MR_Box) (e))
+#endif
+
 /*---------------------------------------------------------------------------*/
 
 /*

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