[v4] LoongArch: Fixed a compilation failure with '%c' in inline assembly [PR107731].

Message ID 20221227064246.2149251-1-chenglulu@loongson.cn
State New
Headers
Series [v4] LoongArch: Fixed a compilation failure with '%c' in inline assembly [PR107731]. |

Commit Message

Lulu Cheng Dec. 27, 2022, 6:42 a.m. UTC
  Co-authored-by: Yang Yujie <yangyujie@loongson.cn>

gcc/ChangeLog:

	* config/loongarch/loongarch.cc (loongarch_classify_address):
	Add precessint for CONST_INT.
	(loongarch_print_operand_reloc): Operand modifier 'c' is supported.
	(loongarch_print_operand): Increase the processing of '%c'.
	* doc/extend.texi: Adds documents for LoongArch operand modifiers.

gcc/testsuite/ChangeLog:

	* gcc.target/loongarch/tst-asm-const.c: Moved to...
	* gcc.target/loongarch/pr107731.c: ...here.

---
V2 -> v3:
1. Correct a clerical error.
2. Adding document for loongarch operand modifiers.

v3 -> v4:
Copy the description of "%c" "%n" "%a" "%l" from gccint.pdf to gcc.pdf.


---
 gcc/config/loongarch/loongarch.cc             | 14 ++++++++
 gcc/doc/extend.texi                           | 33 +++++++++++++++++++
 .../loongarch/{tst-asm-const.c => pr107731.c} |  6 ++--
 3 files changed, 50 insertions(+), 3 deletions(-)
 rename gcc/testsuite/gcc.target/loongarch/{tst-asm-const.c => pr107731.c} (78%)
  

Comments

Lulu Cheng Dec. 30, 2022, 6:12 a.m. UTC | #1
Hi, Richard:

  Could you please help me look at this document? Is there any problem 
with my modification?

Thanks!


在 2022/12/27 下午2:42, Lulu Cheng 写道:
> Co-authored-by: Yang Yujie <yangyujie@loongson.cn>
>
> gcc/ChangeLog:
>
> 	* config/loongarch/loongarch.cc (loongarch_classify_address):
> 	Add precessint for CONST_INT.
> 	(loongarch_print_operand_reloc): Operand modifier 'c' is supported.
> 	(loongarch_print_operand): Increase the processing of '%c'.
> 	* doc/extend.texi: Adds documents for LoongArch operand modifiers.
>
> gcc/testsuite/ChangeLog:
>
> 	* gcc.target/loongarch/tst-asm-const.c: Moved to...
> 	* gcc.target/loongarch/pr107731.c: ...here.
>
> ---
> V2 -> v3:
> 1. Correct a clerical error.
> 2. Adding document for loongarch operand modifiers.
>
> v3 -> v4:
> Copy the description of "%c" "%n" "%a" "%l" from gccint.pdf to gcc.pdf.
>
>
> ---
>   gcc/config/loongarch/loongarch.cc             | 14 ++++++++
>   gcc/doc/extend.texi                           | 33 +++++++++++++++++++
>   .../loongarch/{tst-asm-const.c => pr107731.c} |  6 ++--
>   3 files changed, 50 insertions(+), 3 deletions(-)
>   rename gcc/testsuite/gcc.target/loongarch/{tst-asm-const.c => pr107731.c} (78%)
>
> diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc
> index c6b03fcf2f9..cdf190b985e 100644
> --- a/gcc/config/loongarch/loongarch.cc
> +++ b/gcc/config/loongarch/loongarch.cc
> @@ -2075,6 +2075,11 @@ loongarch_classify_address (struct loongarch_address_info *info, rtx x,
>         return (loongarch_valid_base_register_p (info->reg, mode, strict_p)
>   	      && loongarch_valid_lo_sum_p (info->symbol_type, mode,
>   					   info->offset));
> +    case CONST_INT:
> +      /* Small-integer addresses don't occur very often, but they
> +	 are legitimate if $r0 is a valid base register.  */
> +      info->type = ADDRESS_CONST_INT;
> +      return IMM12_OPERAND (INTVAL (x));
>   
>       default:
>         return false;
> @@ -4933,6 +4938,7 @@ loongarch_print_operand_reloc (FILE *file, rtx op, bool hi64_part,
>   
>      'A'	Print a _DB suffix if the memory model requires a release.
>      'b'	Print the address of a memory operand, without offset.
> +   'c'  Print an integer.
>      'C'	Print the integer branch condition for comparison OP.
>      'd'	Print CONST_INT OP in decimal.
>      'F'	Print the FPU branch condition for comparison OP.
> @@ -4979,6 +4985,14 @@ loongarch_print_operand (FILE *file, rtx op, int letter)
>          fputs ("_db", file);
>         break;
>   
> +    case 'c':
> +      if (CONST_INT_P (op))
> +	fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
> +      else
> +	output_operand_lossage ("unsupported operand for code '%c'", letter);
> +
> +      break;
> +
>       case 'C':
>         loongarch_print_int_branch_condition (file, code, letter);
>         break;
> diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
> index adba057c190..1bb960266ed 100644
> --- a/gcc/doc/extend.texi
> +++ b/gcc/doc/extend.texi
> @@ -10234,6 +10234,24 @@ generates multiple assembler instructions.
>   Outputs @samp{@{}, @samp{|}, and @samp{@}} characters (respectively)
>   into the assembler code.  When unescaped, these characters have special
>   meaning to indicate multiple assembler dialects, as described below.
> +
> +@item %c@var{digit}
> +It can be used to substitute an operand that is a constant value without
> +the syntax that normally indicates an immediate operand.
> +
> +@item %n@var{digit}
> +It's like @samp{%c@var{digit}} except that the value of the constant is
> +negated before printing.
> +
> +@item %a@var{digit}
> +It Can be used to substitute an operand as if it were a memory reference,
> +with the actual operand treated as the address.  This may be useful when
> +outputting a ``load address'' instruction, because often the assembler
> +syntax for such an instruction requires you to write the operand as if it
> +were a memory reference.
> +
> +@item %l@var{digit}
> +It's used to substitute a @code{label_ref} into a jump instruction.
>   @end table
>   
>   @subsubheading Multiple assembler dialects in @code{asm} templates
> @@ -11374,6 +11392,21 @@ constant.  Used to select the specified bit position.
>   @item @code{x} @tab Equivialent to @code{X}, but only for pointers.
>   @end multitable
>   
> +@anchor{loongarchOperandmodifiers}
> +@subsubsection LoongArch Operand Modifiers
> +
> +The list below describes the supported modifiers and their effects for LoongArch.
> +
> +@multitable @columnfractions .10 .90
> +@headitem Modifier @tab Description
> +@item @code{d} @tab Same as @code{c}.
> +@item @code{i} @tab Print the character ''@code{i}'' if the operand is not a register.
> +@item @code{m} @tab Same as @code{c}, but the printed value is @code{operand - 1}.
> +@item @code{X} @tab Print a constant integer operand in hexadecimal.
> +@item @code{z} @tab Print the operand in its unmodified form, followed by a comma.
> +@end multitable
> +
> +
>   @lowersections
>   @include md.texi
>   @raisesections
> diff --git a/gcc/testsuite/gcc.target/loongarch/tst-asm-const.c b/gcc/testsuite/gcc.target/loongarch/pr107731.c
> similarity index 78%
> rename from gcc/testsuite/gcc.target/loongarch/tst-asm-const.c
> rename to gcc/testsuite/gcc.target/loongarch/pr107731.c
> index 2e04b99e301..80d84c48c6e 100644
> --- a/gcc/testsuite/gcc.target/loongarch/tst-asm-const.c
> +++ b/gcc/testsuite/gcc.target/loongarch/pr107731.c
> @@ -1,13 +1,13 @@
> -/* Test asm const. */
>   /* { dg-do compile } */
>   /* { dg-final { scan-assembler-times "foo:.*\\.long 1061109567.*\\.long 52" 1 } } */
> +
>   int foo ()
>   {
>     __asm__ volatile (
>             "foo:"
>             "\n\t"
> -	  ".long %a0\n\t"
> -	  ".long %a1\n\t"
> +	  ".long %c0\n\t"
> +	  ".long %c1\n\t"
>   	  :
>   	  :"i"(0x3f3f3f3f), "i"(52)
>   	  :
  
Richard Sandiford Jan. 12, 2023, 4:27 p.m. UTC | #2
Thanks for doing this and sorry for the slow review.

Lulu Cheng <chenglulu@loongson.cn> writes:
> Hi, Richard:
>
>   Could you please help me look at this document? Is there any problem 
> with my modification?
>
> Thanks!
>
>
> 在 2022/12/27 下午2:42, Lulu Cheng 写道:
>> Co-authored-by: Yang Yujie <yangyujie@loongson.cn>
>>
>> gcc/ChangeLog:
>>
>> 	* config/loongarch/loongarch.cc (loongarch_classify_address):
>> 	Add precessint for CONST_INT.
>> 	(loongarch_print_operand_reloc): Operand modifier 'c' is supported.
>> 	(loongarch_print_operand): Increase the processing of '%c'.
>> 	* doc/extend.texi: Adds documents for LoongArch operand modifiers.
>>
>> gcc/testsuite/ChangeLog:
>>
>> 	* gcc.target/loongarch/tst-asm-const.c: Moved to...
>> 	* gcc.target/loongarch/pr107731.c: ...here.
>>
>> ---
>> V2 -> v3:
>> 1. Correct a clerical error.
>> 2. Adding document for loongarch operand modifiers.
>>
>> v3 -> v4:
>> Copy the description of "%c" "%n" "%a" "%l" from gccint.pdf to gcc.pdf.
>>
>>
>> ---
>>   gcc/config/loongarch/loongarch.cc             | 14 ++++++++
>>   gcc/doc/extend.texi                           | 33 +++++++++++++++++++
>>   .../loongarch/{tst-asm-const.c => pr107731.c} |  6 ++--
>>   3 files changed, 50 insertions(+), 3 deletions(-)
>>   rename gcc/testsuite/gcc.target/loongarch/{tst-asm-const.c => pr107731.c} (78%)
>>
>> diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc
>> index c6b03fcf2f9..cdf190b985e 100644
>> --- a/gcc/config/loongarch/loongarch.cc
>> +++ b/gcc/config/loongarch/loongarch.cc
>> @@ -2075,6 +2075,11 @@ loongarch_classify_address (struct loongarch_address_info *info, rtx x,
>>         return (loongarch_valid_base_register_p (info->reg, mode, strict_p)
>>   	      && loongarch_valid_lo_sum_p (info->symbol_type, mode,
>>   					   info->offset));
>> +    case CONST_INT:
>> +      /* Small-integer addresses don't occur very often, but they
>> +	 are legitimate if $r0 is a valid base register.  */
>> +      info->type = ADDRESS_CONST_INT;
>> +      return IMM12_OPERAND (INTVAL (x));
>>   
>>       default:
>>         return false;
>> @@ -4933,6 +4938,7 @@ loongarch_print_operand_reloc (FILE *file, rtx op, bool hi64_part,
>>   
>>      'A'	Print a _DB suffix if the memory model requires a release.
>>      'b'	Print the address of a memory operand, without offset.
>> +   'c'  Print an integer.
>>      'C'	Print the integer branch condition for comparison OP.
>>      'd'	Print CONST_INT OP in decimal.
>>      'F'	Print the FPU branch condition for comparison OP.
>> @@ -4979,6 +4985,14 @@ loongarch_print_operand (FILE *file, rtx op, int letter)
>>          fputs ("_db", file);
>>         break;
>>   
>> +    case 'c':
>> +      if (CONST_INT_P (op))
>> +	fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
>> +      else
>> +	output_operand_lossage ("unsupported operand for code '%c'", letter);
>> +
>> +      break;
>> +
>>       case 'C':
>>         loongarch_print_int_branch_condition (file, code, letter);
>>         break;
>> diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
>> index adba057c190..1bb960266ed 100644
>> --- a/gcc/doc/extend.texi
>> +++ b/gcc/doc/extend.texi
>> @@ -10234,6 +10234,24 @@ generates multiple assembler instructions.
>>   Outputs @samp{@{}, @samp{|}, and @samp{@}} characters (respectively)
>>   into the assembler code.  When unescaped, these characters have special
>>   meaning to indicate multiple assembler dialects, as described below.
>> +
>> +@item %c@var{digit}
>> +It can be used to substitute an operand that is a constant value without
>> +the syntax that normally indicates an immediate operand.

It would probably more idiomatic not to have the "it" for this kind of list,
so maybe:

    Substitutes a constant value without the syntax that normally indicates
    an immediate operand.

Similarly for %a and %l.

>> +
>> +@item %n@var{digit}
>> +It's like @samp{%c@var{digit}} except that the value of the constant is
>> +negated before printing.

Similarly here, I think just "Like ...", without the "It's".

>> +@item %a@var{digit}
>> +It Can be used to substitute an operand as if it were a memory reference,
>> +with the actual operand treated as the address.  This may be useful when
>> +outputting a ``load address'' instruction, because often the assembler
>> +syntax for such an instruction requires you to write the operand as if it
>> +were a memory reference.
>> +
>> +@item %l@var{digit}
>> +It's used to substitute a @code{label_ref} into a jump instruction.
>>   @end table

This list is describing uses of % that don't relate to operands.
The idea of using % with operand numbers is only introduced later, in:

    @code{asm} supports operand modifiers on operands (for example @samp{%k2} 
    instead of simply @samp{%2}). Typically these qualifiers are hardware 
    dependent. The list of supported modifiers for x86 is found at 
    @ref{x86Operandmodifiers,x86 Operand modifiers}.

There are two copies of this, one for outputs and one for inputs.

So I think we should instead extend that paragraph to refer to a section
that contains target-independent modifiers.  Maybe something like:

    @code{asm} supports operand modifiers on operands (for example @samp{%k2} 
    instead of simply @samp{%2}). @ref{genericOperandmodifiers,
    Generic Operand modifiers} lists the modifiers that are available
    on all targets.  Other modifiers are hardware dependent.
    For example, the list of supported modifiers for x86 is found at 
    @ref{x86Operandmodifiers,x86 Operand modifiers}.

Thanks,
Richard

>>   @subsubheading Multiple assembler dialects in @code{asm} templates
>> @@ -11374,6 +11392,21 @@ constant.  Used to select the specified bit position.
>>   @item @code{x} @tab Equivialent to @code{X}, but only for pointers.
>>   @end multitable
>>   
>> +@anchor{loongarchOperandmodifiers}
>> +@subsubsection LoongArch Operand Modifiers
>> +
>> +The list below describes the supported modifiers and their effects for LoongArch.
>> +
>> +@multitable @columnfractions .10 .90
>> +@headitem Modifier @tab Description
>> +@item @code{d} @tab Same as @code{c}.
>> +@item @code{i} @tab Print the character ''@code{i}'' if the operand is not a register.
>> +@item @code{m} @tab Same as @code{c}, but the printed value is @code{operand - 1}.
>> +@item @code{X} @tab Print a constant integer operand in hexadecimal.
>> +@item @code{z} @tab Print the operand in its unmodified form, followed by a comma.
>> +@end multitable
>> +
>> +
>>   @lowersections
>>   @include md.texi
>>   @raisesections
>> diff --git a/gcc/testsuite/gcc.target/loongarch/tst-asm-const.c b/gcc/testsuite/gcc.target/loongarch/pr107731.c
>> similarity index 78%
>> rename from gcc/testsuite/gcc.target/loongarch/tst-asm-const.c
>> rename to gcc/testsuite/gcc.target/loongarch/pr107731.c
>> index 2e04b99e301..80d84c48c6e 100644
>> --- a/gcc/testsuite/gcc.target/loongarch/tst-asm-const.c
>> +++ b/gcc/testsuite/gcc.target/loongarch/pr107731.c
>> @@ -1,13 +1,13 @@
>> -/* Test asm const. */
>>   /* { dg-do compile } */
>>   /* { dg-final { scan-assembler-times "foo:.*\\.long 1061109567.*\\.long 52" 1 } } */
>> +
>>   int foo ()
>>   {
>>     __asm__ volatile (
>>             "foo:"
>>             "\n\t"
>> -	  ".long %a0\n\t"
>> -	  ".long %a1\n\t"
>> +	  ".long %c0\n\t"
>> +	  ".long %c1\n\t"
>>   	  :
>>   	  :"i"(0x3f3f3f3f), "i"(52)
>>   	  :
  

Patch

diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc
index c6b03fcf2f9..cdf190b985e 100644
--- a/gcc/config/loongarch/loongarch.cc
+++ b/gcc/config/loongarch/loongarch.cc
@@ -2075,6 +2075,11 @@  loongarch_classify_address (struct loongarch_address_info *info, rtx x,
       return (loongarch_valid_base_register_p (info->reg, mode, strict_p)
 	      && loongarch_valid_lo_sum_p (info->symbol_type, mode,
 					   info->offset));
+    case CONST_INT:
+      /* Small-integer addresses don't occur very often, but they
+	 are legitimate if $r0 is a valid base register.  */
+      info->type = ADDRESS_CONST_INT;
+      return IMM12_OPERAND (INTVAL (x));
 
     default:
       return false;
@@ -4933,6 +4938,7 @@  loongarch_print_operand_reloc (FILE *file, rtx op, bool hi64_part,
 
    'A'	Print a _DB suffix if the memory model requires a release.
    'b'	Print the address of a memory operand, without offset.
+   'c'  Print an integer.
    'C'	Print the integer branch condition for comparison OP.
    'd'	Print CONST_INT OP in decimal.
    'F'	Print the FPU branch condition for comparison OP.
@@ -4979,6 +4985,14 @@  loongarch_print_operand (FILE *file, rtx op, int letter)
        fputs ("_db", file);
       break;
 
+    case 'c':
+      if (CONST_INT_P (op))
+	fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
+      else
+	output_operand_lossage ("unsupported operand for code '%c'", letter);
+
+      break;
+
     case 'C':
       loongarch_print_int_branch_condition (file, code, letter);
       break;
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index adba057c190..1bb960266ed 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -10234,6 +10234,24 @@  generates multiple assembler instructions.
 Outputs @samp{@{}, @samp{|}, and @samp{@}} characters (respectively)
 into the assembler code.  When unescaped, these characters have special
 meaning to indicate multiple assembler dialects, as described below.
+
+@item %c@var{digit}
+It can be used to substitute an operand that is a constant value without
+the syntax that normally indicates an immediate operand.
+
+@item %n@var{digit}
+It's like @samp{%c@var{digit}} except that the value of the constant is
+negated before printing.
+
+@item %a@var{digit}
+It Can be used to substitute an operand as if it were a memory reference,
+with the actual operand treated as the address.  This may be useful when
+outputting a ``load address'' instruction, because often the assembler
+syntax for such an instruction requires you to write the operand as if it
+were a memory reference.
+
+@item %l@var{digit}
+It's used to substitute a @code{label_ref} into a jump instruction.
 @end table
 
 @subsubheading Multiple assembler dialects in @code{asm} templates
@@ -11374,6 +11392,21 @@  constant.  Used to select the specified bit position.
 @item @code{x} @tab Equivialent to @code{X}, but only for pointers.
 @end multitable
 
+@anchor{loongarchOperandmodifiers}
+@subsubsection LoongArch Operand Modifiers
+
+The list below describes the supported modifiers and their effects for LoongArch.
+
+@multitable @columnfractions .10 .90
+@headitem Modifier @tab Description
+@item @code{d} @tab Same as @code{c}.
+@item @code{i} @tab Print the character ''@code{i}'' if the operand is not a register.
+@item @code{m} @tab Same as @code{c}, but the printed value is @code{operand - 1}.
+@item @code{X} @tab Print a constant integer operand in hexadecimal.
+@item @code{z} @tab Print the operand in its unmodified form, followed by a comma.
+@end multitable
+
+
 @lowersections
 @include md.texi
 @raisesections
diff --git a/gcc/testsuite/gcc.target/loongarch/tst-asm-const.c b/gcc/testsuite/gcc.target/loongarch/pr107731.c
similarity index 78%
rename from gcc/testsuite/gcc.target/loongarch/tst-asm-const.c
rename to gcc/testsuite/gcc.target/loongarch/pr107731.c
index 2e04b99e301..80d84c48c6e 100644
--- a/gcc/testsuite/gcc.target/loongarch/tst-asm-const.c
+++ b/gcc/testsuite/gcc.target/loongarch/pr107731.c
@@ -1,13 +1,13 @@ 
-/* Test asm const. */
 /* { dg-do compile } */
 /* { dg-final { scan-assembler-times "foo:.*\\.long 1061109567.*\\.long 52" 1 } } */
+
 int foo ()
 {
   __asm__ volatile (
           "foo:"
           "\n\t"
-	  ".long %a0\n\t"
-	  ".long %a1\n\t"
+	  ".long %c0\n\t"
+	  ".long %c1\n\t"
 	  :
 	  :"i"(0x3f3f3f3f), "i"(52)
 	  :