[PATCH] Target hook for rewriting inline asm constraints

Andreas Krebbel Andreas.Krebbel@de.ibm.com
Wed Oct 24 16:01:00 GMT 2007


Hello,

if a back end wants to support new memory operands it has to extend
the GO_IF_LEGITIMATE_ADDRESS target hook.  Unfortunately this also
changes the semantics of the 'm' constraint possibly breaking every
existing inline assembly out there.

The attached patch introduces a new target hook which can be defined
in the back end to rewrite constraints used in inline assemblies.  If
a back end supports new memory operands it usually also has to provide
a new constraint letter defining what the former 'm' constraint would
have accepted.  The intention of that target hook is to provide a
mechanism allowing the back end to replace the 'm' contraint with that
new constraint letter.

I know that this is not perfectly appropriate for stage 3.  But since
it is a nop for all targets not defining that target hook and since I
need it for S/390 back end patches I'm working on I hope that can
nevertheless go in.

Bootstrapping on s390x and x86_64.

OK for mainline given no testsuite regressions occur?

Bye,

-Andreas-

2007-10-24  Andreas Krebbel  <krebbel1@de.ibm.com>

	* target-def.h (TARGET_REPLACE_INLINE_ASM_CONSTRAINT): New
	macro.  Added to TARGET_INITIALIZER definition.
	* target.h (replace_inline_asm_constraint): Function pointer added.
	* targhooks.c (default_replace_inline_asm_constraint): New function.
	* targhooks.h (default_replace_inline_asm_constraint): Prototype added.
	* c-parser.c (c_parser_asm_operands): Invoke new target hook.
	* doc/tm.texi (TARGET_REPLACE_INLINE_ASM_CONSTRAINT):
	Documenation added.


Index: gcc/target-def.h
===================================================================
*** gcc/target-def.h.orig	2007-10-19 16:34:27.000000000 +0200
--- gcc/target-def.h	2007-10-19 16:56:47.000000000 +0200
***************
*** 603,608 ****
--- 603,612 ----
  #define TARGET_SECONDARY_RELOAD default_secondary_reload
  #endif
  
+ #ifndef TARGET_REPLACE_INLINE_ASM_CONSTRAINT
+ #define TARGET_REPLACE_INLINE_ASM_CONSTRAINT default_replace_inline_asm_constraint
+ #endif
+ 
  /* C specific.  */
  #ifndef TARGET_C_MODE_FOR_SUFFIX
  #define TARGET_C_MODE_FOR_SUFFIX default_mode_for_suffix
***************
*** 769,774 ****
--- 773,779 ----
    TARGET_INVALID_UNARY_OP,			\
    TARGET_INVALID_BINARY_OP,			\
    TARGET_SECONDARY_RELOAD,			\
+   TARGET_REPLACE_INLINE_ASM_CONSTRAINT,       \
    TARGET_C,					\
    TARGET_CXX,					\
    TARGET_EXTRA_LIVE_ON_ENTRY,			\
Index: gcc/target.h
===================================================================
*** gcc/target.h.orig	2007-10-19 16:34:27.000000000 +0200
--- gcc/target.h	2007-10-19 16:55:27.000000000 +0200
*************** struct gcc_target
*** 846,851 ****
--- 846,855 ----
  				      enum machine_mode,
  				      struct secondary_reload_info *);
  
+   /* Return a replacement for constraint c when used for an inline
+      assembly operand.  */
+   const char *(*replace_inline_asm_constraint) (const char *c);
+ 
    /* Functions specific to the C family of frontends.  */
    struct c {
      /* Return machine mode for non-standard suffix
Index: gcc/targhooks.c
===================================================================
*** gcc/targhooks.c.orig	2007-10-19 16:34:27.000000000 +0200
--- gcc/targhooks.c	2007-10-19 16:53:38.000000000 +0200
*************** default_secondary_reload (bool in_p ATTR
*** 661,666 ****
--- 661,672 ----
    return class;
  }
  
+ const char *
+ default_replace_inline_asm_constraint (const char *c)
+ {
+   return c;
+ }
+ 
  bool
  default_handle_c_option (size_t code ATTRIBUTE_UNUSED,
  			 const char *arg ATTRIBUTE_UNUSED,
Index: gcc/targhooks.h
===================================================================
*** gcc/targhooks.h.orig	2007-10-19 16:34:27.000000000 +0200
--- gcc/targhooks.h	2007-10-19 16:56:00.000000000 +0200
*************** extern rtx default_internal_arg_pointer 
*** 90,95 ****
--- 90,96 ----
  extern enum reg_class default_secondary_reload (bool, rtx, enum reg_class,
  						enum machine_mode,
  						secondary_reload_info *);
+ extern const char * default_replace_inline_asm_constraint (const char *c);
  extern void hook_void_bitmap (bitmap);
  extern bool default_handle_c_option (size_t, const char *, int);
  extern int default_reloc_rw_mask (void);
Index: gcc/c-parser.c
===================================================================
*** gcc/c-parser.c.orig	2007-10-19 14:36:57.000000000 +0200
--- gcc/c-parser.c	2007-10-24 16:37:43.000000000 +0200
*************** along with GCC; see the file COPYING3.  
*** 42,47 ****
--- 42,48 ----
  #include "tm.h"
  #include "tree.h"
  #include "rtl.h"
+ #include "tm_p.h"
  #include "langhooks.h"
  #include "input.h"
  #include "cpplib.h"
*************** c_parser_asm_operands (c_parser *parser,
*** 4262,4267 ****
--- 4263,4300 ----
        str = c_parser_asm_string_literal (parser);
        if (str == NULL_TREE)
  	return NULL_TREE;
+       {
+ 	const char *p = TREE_STRING_POINTER (str);
+ 	char result[255], tmp[255];
+ 	char c;
+ 	int len;
+ 
+ 	result[0] = '\0';
+ 
+ 	do
+ 	  switch (c = *p, len = CONSTRAINT_LEN (c, p), c)
+ 	    {
+ 	    case '\0':
+ 	      len = 0;
+ 	      break;
+ 	    case ',':
+ 	      c = '\0';
+ 	      break;
+ 	    case '=': case '+': case '*': case '%': case '?':
+ 	    case '!': case '#':
+ 	    case '0':  case '1':  case '2':  case '3':  case '4':
+ 	    case '5':  case '6':  case '7':  case '8':  case '9':
+ 	      result[strlen (result) + 1] = '\0';
+ 	      result[strlen (result)] = c;
+ 	      break;
+ 	    default:
+ 	      memcpy (tmp, p, len);
+ 	      tmp[len] = '\0';
+ 	      strcat (result, targetm.replace_inline_asm_constraint (tmp));
+ 	    }
+ 	while ((p += len), c);
+ 	str = build_string (strlen (result), result);
+       }
        parser->lex_untranslated_string = false;
        if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
  	{
Index: gcc/doc/tm.texi
===================================================================
*** gcc/doc/tm.texi.orig	2007-10-19 16:33:25.000000000 +0200
--- gcc/doc/tm.texi	2007-10-24 16:50:29.000000000 +0200
*************** to the functions in @file{libgcc} that p
*** 10355,10357 ****
--- 10355,10367 ----
  call stack unwinding.  It is used in declarations in @file{unwind-generic.h}
  and the associated definitions of those functions.
  @end defmac
+ 
+ @deftypefn {Target Hook} {const char *} TARGET_REPLACE_INLINE_ASM_CONSTRAINT (const char *@var{str})
+ Define this target hook to return a constraint string which should be
+ used instead of @var{str} in inline assembly operands.
+ 
+ One case where this target hook needs to be defined is when the back
+ end target hook @code{GO_IF_LEGITIMATE_ADDRESS} has to be extended to
+ support new memory operands while keeping the semantics of the ``m''
+ constraint.
+ @end deftypefn



More information about the Gcc-patches mailing list