[m-dev.] diff: GCC back-end: promote return values to `int'

Fergus Henderson fjh at cs.mu.OZ.AU
Tue Jan 16 14:14:41 AEDT 2001


Estimated hours taken: 1

gcc/mercury/mercury-gcc.c:
	Promote function return values to `int', to ensure binary
	compatibility with C.  Without this change, the generated code
	for functions with return values of type e.g. `bool' on e.g.
	x86 would use only the low 8 bits of the return register eax,
	and would leave the remaining bits uninitialized, whereas
	the C calling convention expects such values to be
	sign-extended to 32 bits.

cvs diff: Diffing .
Index: mercury-gcc.c
===================================================================
RCS file: /home/mercury1/repository/gcc/mercury/mercury-gcc.c,v
retrieving revision 1.20.2.2
diff -u -d -u -r1.20.2.2 mercury-gcc.c
--- mercury-gcc.c	2001/01/14 09:20:18	1.20.2.2
+++ mercury-gcc.c	2001/01/15 15:15:34
@@ -68,6 +68,7 @@
 /* Declarations of functions defined in this file.  */
 
 static tree merc_convert PARAMS((tree type, tree expr));
+static tree merc_promote_return_type PARAMS((tree type));
 static void merc_init_builtin_functions PARAMS((void));
 static void merc_handle_fatal_error PARAMS((const char *msg, va_list *args));
 static int merc_call_mercury_compiler PARAMS((void));
@@ -244,6 +245,24 @@
   return tree_cons (NULL_TREE, param_type, param_type_list);
 }
 
+/* For binary compatibility with C, we must promote
+   function return values that are smaller than `int' to `int'.  */
+
+static tree
+merc_promote_return_type (type)
+     tree type;
+{
+  enum tree_code code = TREE_CODE (type);
+  if ((code == INTEGER_TYPE
+       || code == ENUMERAL_TYPE
+       || code == CHAR_TYPE
+       || code == BOOLEAN_TYPE)
+      && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
+    return integer_type_node;
+  else
+    return type;
+}
+
 /* Make a FUNCTION_DECL for a function whose name is NAME,
    whose parameter types are given in the list PARAM_TYPE_LIST,
    and whose parameters are given in PARAM_LIST.  */
@@ -271,7 +290,8 @@
   TREE_STATIC (fndecl) = 1;
   DECL_ARGUMENTS (fndecl) = param_list;
   DECL_RESULT (fndecl)
-    = build_decl (RESULT_DECL, NULL_TREE, return_type);
+    = build_decl (RESULT_DECL, NULL_TREE,
+		  merc_promote_return_type (return_type));
   DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
 
   rest_of_decl_compilation (fndecl, NULL_PTR, 1, 0);
@@ -454,8 +474,9 @@
 merc_gen_return (exp)
      tree exp;
 {
+  tree result = DECL_RESULT (current_function_decl);
   expand_return (build (MODIFY_EXPR, void_type_node,
-			DECL_RESULT (current_function_decl), exp));
+			result, convert (TREE_TYPE (result), exp)));
 }
 
 /* Generate code for an assignment statement.  */

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list