Re: inline asm clobbers

2015-03-12 Thread Paul_Koning

 On Mar 11, 2015, at 8:53 PM, David Wohlferd d...@limegreensocks.com wrote:
 
 ...
 I would agree that one should avoid it.  I'd be wary of removing it
 from GCC at this point since it might break working code.
 It certainly would.  It’s not all that common, but I have seen this done in 
 production code.  Come to think of it, this certainly makes sense in 
 machines where some instructions act on fixed registers.
 
 Really?  While I've seen much code that uses clobbers, I have never (until 
 this week) see anyone attempt to clobber by index.  Since I'm basically an 
 i386 guy, maybe this is a platform thing?  Do you have some examples/links?

The example I remember was not in open code.  It may have been cleaned up by 
now, but as supplied to us by the vendor, there were some bits of assembly code 
that needed a scratch register and used a fixed register (t0 == %8) for that 
purpose rather than having GCC deal with temporaries.  So there was a clobber 
with “8” in it.  Obviously there’s a better way in that instance, but if GCC 
had removed the feature before we found and cleaned up that code, we would have 
had a failure on our hands.

An example of hardwired registers I remember is some VAX instructions (string 
instructions).  You could write those by name, of course, but if you didn’t 
know that GCC supports names, you might just use numbers.  On machines like VAX 
where the register names are really just numbers (R0, R1, etc.) that isn’t such 
a strange thing to do.

 Register names would be nice as an additional capability.
 
 Every example I've ever seen uses register names.  Perhaps that what you've 
 seen before?

No; I didn’t know that gcc supports register names.  The examples I had seen 
all use numbers.  Or more often, preprocessor symbols.  It may be that’s 
because the most common asm code I run into is MIPS coprocessor references, and 
while general registers may be known by name, coprocessor registers may not be. 
 Or it may just be a case of lack of awareness.

paul


Re: inline asm clobbers

2015-03-12 Thread David Wohlferd

Resending due to bounced email.

On 3/11/2015 6:19 PM, Ian Lance Taylor wrote:

On Wed, Mar 11, 2015 at 5:51 PM, David Wohlferd d...@limegreensocks.com wrote:

The reason I believe the order can change is this comment from i386.h:

/* Order in which to allocate registers.  Each register must be
listed once, even those in FIXED_REGISTERS.  List frame pointer
late and fixed registers last.  Note that, in general, we prefer
registers listed in CALL_USED_REGISTERS, keeping the others
available for storage of persistent values.

The ADJUST_REG_ALLOC_ORDER actually overwrite the order,
so this is just empty initializer for array.  */

That is REG_ALLOC_ORDER.  The index that appears in an asm statement
is the hard register number.  REG_ALLOC_ORDER is an array holding hard
register numbers.  The hard register numbers can change in principle,
by changing the source code, but I actually can't recall that ever
happening.

To wrap this up:

Like Ian said, the order of registers here apparently never changes.  I 
read more into that comment than I should have.  For good luck, I 
experimented with -fomit-frame-pointer, -ffixed-, etc, and nothing has 
any impact here.  The list is the list.


In fact, it turns out you can use this same format with register variables:

register int x asm(3); // i386: ebx

So while I find it ugly, unnecessarily complex, and lacking in 
self-documenting-ness, it is not inherently buggy the way I feared it 
was, so I can't think of any good arguments for pulling it out.


Thanks to Ian and Paul for straightening me out.

dw


Re: inline asm clobbers

2015-03-12 Thread Jakub Jelinek
On Thu, Mar 12, 2015 at 02:02:37PM -0700, David Wohlferd wrote:
 To wrap this up:
 
 Like Ian said, the order of registers here apparently never changes.  I read
 more into that comment than I should have.  For good luck, I experimented
 with -fomit-frame-pointer, -ffixed-, etc, and nothing has any impact here.
 The list is the list.
 
 In fact, it turns out you can use this same format with register variables:
 
 register int x asm(3); // i386: ebx

On some architectures, like e.g. powerpc, people often just use numbers
instead of say rnumber at least for the general purpose registers and
usually the internal register numbering matches it.
So, while using asm (3) on x86 is something very unusual, on other targets
it can be quite common.

Jakub


Re: inline asm clobbers

2015-03-12 Thread Jeff Law

On 03/12/15 15:26, Jakub Jelinek wrote:

On Thu, Mar 12, 2015 at 02:02:37PM -0700, David Wohlferd wrote:

To wrap this up:

Like Ian said, the order of registers here apparently never changes.  I read
more into that comment than I should have.  For good luck, I experimented
with -fomit-frame-pointer, -ffixed-, etc, and nothing has any impact here.
The list is the list.

In fact, it turns out you can use this same format with register variables:

 register int x asm(3); // i386: ebx


On some architectures, like e.g. powerpc, people often just use numbers
instead of say rnumber at least for the general purpose registers and
usually the internal register numbering matches it.
So, while using asm (3) on x86 is something very unusual, on other targets
it can be quite common.
Yup.  And as common as it may be, I find it makes reading the resulting 
assembly code far more difficult than it ought to be :(


But I won't start a rant on that issue right now ;-)

Jeff


Re: inline asm clobbers

2015-03-12 Thread David Wohlferd



On 3/12/2015 7:24 AM, paul_kon...@dell.com wrote:

On Mar 11, 2015, at 8:53 PM, David Wohlferd d...@limegreensocks.com wrote:


...
I would agree that one should avoid it.  I'd be wary of removing it
from GCC at this point since it might break working code.

It certainly would.  It’s not all that common, but I have seen this done in 
production code.  Come to think of it, this certainly makes sense in machines 
where some instructions act on fixed registers.

Really?  While I've seen much code that uses clobbers, I have never (until this 
week) see anyone attempt to clobber by index.  Since I'm basically an i386 guy, 
maybe this is a platform thing?  Do you have some examples/links?

The example I remember was not in open code.  It may have been cleaned up by 
now, but as supplied to us by the vendor, there were some bits of assembly code 
that needed a scratch register and used a fixed register (t0 == %8) for that 
purpose rather than having GCC deal with temporaries.  So there was a clobber 
with “8” in it.  Obviously there’s a better way in that instance, but if GCC 
had removed the feature before we found and cleaned up that code, we would have 
had a failure on our hands.

An example of hardwired registers I remember is some VAX instructions (string 
instructions).  You could write those by name, of course, but if you didn’t 
know that GCC supports names, you might just use numbers.  On machines like VAX 
where the register names are really just numbers (R0, R1, etc.) that isn’t such 
a strange thing to do.


Register names would be nice as an additional capability.

Every example I've ever seen uses register names.  Perhaps that what you've 
seen before?

No; I didn’t know that gcc supports register names.  The examples I had seen 
all use numbers.  Or more often, preprocessor symbols.  It may be that’s 
because the most common asm code I run into is MIPS coprocessor references, and 
while general registers may be known by name, coprocessor registers may not be. 
 Or it may just be a case of lack of awareness.

paul


Ahh.  So perhaps as I suspected: this is more commonly used on non-i386 
platforms.  So clearly removing this is out of the question.


This brings us to the question of documentation.  Right now the docs 
only refer to register names.  I expect it would be helpful for people 
to understand what it means when they come across code that uses 
indices.  A few words in the 'clobbers' section and the two Reg Vars 
sections would probably cover it.


Perhaps some variation of:


In addition to specifying registers by name, it is also possible to use 
a register index (ie 3 to refer to the 3rd register).  The list of 
registers and their order is platform specific.  See the REGISTER_NAMES 
defined for your platform in the gcc source.



I'm not excited about pointing people vaguely toward the source, but 
that's the only place I know to find this info.


I realize this may seem a bit redundant for people who are used to 
registers named R0,R1,R2..., but on the i386, the order is: 
ax,dx,cx,bx,si...


dw


Re: inline asm clobbers

2015-03-12 Thread Segher Boessenkool
On Thu, Mar 12, 2015 at 03:09:52PM -0700, David Wohlferd wrote:
 Ahh.  So perhaps as I suspected: this is more commonly used on non-i386 
 platforms.  So clearly removing this is out of the question.

glibc uses it for PowerPC and s390 at least (I only grepped for 3,
quotes included -- there may be more archs that use it).

This feature was added to GCC very long ago, in 1992, SVN commit r334.
The code comment (varasm.c:decode_reg_name_and_count nowadays) makes
clear it is very much deliberate (not an implementation accident).

 This brings us to the question of documentation.  Right now the docs 
 only refer to register names.  I expect it would be helpful for people 
 to understand what it means when they come across code that uses 
 indices.  A few words in the 'clobbers' section and the two Reg Vars 
 sections would probably cover it.

I wouldn't.  Numbers *are* the register names for all platforms where
you are likely to see this used.  Expecting numbers in a clobber to
work as a backref to an output constraint is a) strange, and b) fatal,
as so many errors with asm are.  I think the only way to teach people
to not hurt themselves so much with asm is to let them hurt themselves
a lot, so they'll be much more careful in the future :-/

 I realize this may seem a bit redundant for people who are used to 
 registers named R0,R1,R2..., but on the i386, the order is: 
 ax,dx,cx,bx,si...

And that isn't even the same as the hardware numbering.  No one will
or should use this on x86, and if they do, well, hurt ;-)


Segher


Re: inline asm clobbers

2015-03-11 Thread Ian Lance Taylor
On Wed, Mar 11, 2015 at 5:51 PM, David Wohlferd d...@limegreensocks.com wrote:

 The reason I believe the order can change is this comment from i386.h:

 /* Order in which to allocate registers.  Each register must be
listed once, even those in FIXED_REGISTERS.  List frame pointer
late and fixed registers last.  Note that, in general, we prefer
registers listed in CALL_USED_REGISTERS, keeping the others
available for storage of persistent values.

The ADJUST_REG_ALLOC_ORDER actually overwrite the order,
so this is just empty initializer for array.  */

That is REG_ALLOC_ORDER.  The index that appears in an asm statement
is the hard register number.  REG_ALLOC_ORDER is an array holding hard
register numbers.  The hard register numbers can change in principle,
by changing the source code, but I actually can't recall that ever
happening.

Ian


inline asm clobbers

2015-03-11 Thread David Wohlferd

Why does gcc allow you to specify clobbers using numbers:

   asm ( : : r (var) : 0); // i386: clobbers eax

How is this better than using register names?

This makes even less sense when you realize that (apparently) the 
indices of registers aren't fixed.  Which means there is no way to know 
which register you have clobbered in order to use it in the template.


Having just seen someone trying (unsuccessfully) to use this, it seems 
like there is no practical way you can.


Which makes me wonder why it's there.  And whether it still should be.

dw


Re: inline asm clobbers

2015-03-11 Thread Ian Lance Taylor
On Wed, Mar 11, 2015 at 4:41 PM,  paul_kon...@dell.com wrote:

 On Mar 11, 2015, at 7:19 PM, Ian Lance Taylor i...@google.com wrote:

 On Wed, Mar 11, 2015 at 3:58 PM, David Wohlferd d...@limegreensocks.com 
 wrote:

 Why does gcc allow you to specify clobbers using numbers:

   asm ( : : r (var) : 0); // i386: clobbers eax

 How is this better than using register names?

 This makes even less sense when you realize that (apparently) the indices of
 registers aren't fixed.  Which means there is no way to know which register
 you have clobbered in order to use it in the template.

 Having just seen someone trying (unsuccessfully) to use this, it seems like
 there is no practical way you can.

 Which makes me wonder why it's there.  And whether it still should be.

 I don't know why it works.  It should be consistent, though.  It's
 simply GCC's internal hard register number, which doesn't normally
 change.

 I would agree that one should avoid it.  I'd be wary of removing it
 from GCC at this point since it might break working code.

 It certainly would.  It’s not all that common, but I have seen this done in 
 production code.  Come to think of it, this certainly makes sense in machines 
 where some instructions act on fixed registers.

 Register names would be nice as an additional capability.

Register names are supported.

Ian


Re: inline asm clobbers

2015-03-11 Thread David Wohlferd


On 3/11/2015 4:19 PM, Ian Lance Taylor wrote:

On Wed, Mar 11, 2015 at 3:58 PM, David Wohlferd d...@limegreensocks.com wrote:

Why does gcc allow you to specify clobbers using numbers:

asm ( : : r (var) : 0); // i386: clobbers eax

How is this better than using register names?

This makes even less sense when you realize that (apparently) the indices of
registers aren't fixed.  Which means there is no way to know which register
you have clobbered in order to use it in the template.

Having just seen someone trying (unsuccessfully) to use this, it seems like
there is no practical way you can.

Which makes me wonder why it's there.  And whether it still should be.

I don't know why it works.  It should be consistent, though.  It's
simply GCC's internal hard register number, which doesn't normally
change.


The reason I believe the order can change is this comment from i386.h:

/* Order in which to allocate registers.  Each register must be
   listed once, even those in FIXED_REGISTERS.  List frame pointer
   late and fixed registers last.  Note that, in general, we prefer
   registers listed in CALL_USED_REGISTERS, keeping the others
   available for storage of persistent values.

   The ADJUST_REG_ALLOC_ORDER actually overwrite the order,
   so this is just empty initializer for array.  */

My attempts to follow ADJUST_REG_ALLOC_ORDER were not particularly 
successful, but it did take me to this comment in ira.c:


/* This is called every time when register related information is
   changed.  */


I would agree that one should avoid it.  I'd be wary of removing it
from GCC at this point since it might break working code.


I hear you on this.  Removing existing functionality is definitely 
risky, so I agree with your caution.  And of course changing anything is 
much less important if the register order here really is fixed.


On the other hand, what if my fear about register order changing is 
correct?  In that case people who assume (as you have) that they don't 
change are clobbering random registers.  Also, the fellow I saw trying 
to use this (incorrectly) assumed that 0 was referring to the same 
thing as the template's %0.


If people don't (or if in fact it is impossible to) use this safely, 
breaking their code by forcing them to use register names might be the 
best way to fix it.


dw


Re: inline asm clobbers

2015-03-11 Thread Paul_Koning

 On Mar 11, 2015, at 7:19 PM, Ian Lance Taylor i...@google.com wrote:
 
 On Wed, Mar 11, 2015 at 3:58 PM, David Wohlferd d...@limegreensocks.com 
 wrote:
 
 Why does gcc allow you to specify clobbers using numbers:
 
   asm ( : : r (var) : 0); // i386: clobbers eax
 
 How is this better than using register names?
 
 This makes even less sense when you realize that (apparently) the indices of
 registers aren't fixed.  Which means there is no way to know which register
 you have clobbered in order to use it in the template.
 
 Having just seen someone trying (unsuccessfully) to use this, it seems like
 there is no practical way you can.
 
 Which makes me wonder why it's there.  And whether it still should be.
 
 I don't know why it works.  It should be consistent, though.  It's
 simply GCC's internal hard register number, which doesn't normally
 change.
 
 I would agree that one should avoid it.  I'd be wary of removing it
 from GCC at this point since it might break working code.

It certainly would.  It’s not all that common, but I have seen this done in 
production code.  Come to think of it, this certainly makes sense in machines 
where some instructions act on fixed registers.

Register names would be nice as an additional capability.

paul



Re: inline asm clobbers

2015-03-11 Thread Ian Lance Taylor
On Wed, Mar 11, 2015 at 3:58 PM, David Wohlferd d...@limegreensocks.com wrote:

 Why does gcc allow you to specify clobbers using numbers:

asm ( : : r (var) : 0); // i386: clobbers eax

 How is this better than using register names?

 This makes even less sense when you realize that (apparently) the indices of
 registers aren't fixed.  Which means there is no way to know which register
 you have clobbered in order to use it in the template.

 Having just seen someone trying (unsuccessfully) to use this, it seems like
 there is no practical way you can.

 Which makes me wonder why it's there.  And whether it still should be.

I don't know why it works.  It should be consistent, though.  It's
simply GCC's internal hard register number, which doesn't normally
change.

I would agree that one should avoid it.  I'd be wary of removing it
from GCC at this point since it might break working code.

Ian


Re: inline asm clobbers

2015-03-11 Thread David Wohlferd



On 3/11/2015 4:41 PM, paul_kon...@dell.com wrote:

On Mar 11, 2015, at 7:19 PM, Ian Lance Taylor i...@google.com wrote:

On Wed, Mar 11, 2015 at 3:58 PM, David Wohlferd d...@limegreensocks.com wrote:

Why does gcc allow you to specify clobbers using numbers:

   asm ( : : r (var) : 0); // i386: clobbers eax

How is this better than using register names?

This makes even less sense when you realize that (apparently) the indices of
registers aren't fixed.  Which means there is no way to know which register
you have clobbered in order to use it in the template.

Having just seen someone trying (unsuccessfully) to use this, it seems like
there is no practical way you can.

Which makes me wonder why it's there.  And whether it still should be.

I don't know why it works.  It should be consistent, though.  It's
simply GCC's internal hard register number, which doesn't normally
change.

I would agree that one should avoid it.  I'd be wary of removing it
from GCC at this point since it might break working code.

It certainly would.  It’s not all that common, but I have seen this done in 
production code.  Come to think of it, this certainly makes sense in machines 
where some instructions act on fixed registers.


Really?  While I've seen much code that uses clobbers, I have never 
(until this week) see anyone attempt to clobber by index.  Since I'm 
basically an i386 guy, maybe this is a platform thing?  Do you have some 
examples/links?



Register names would be nice as an additional capability.


Every example I've ever seen uses register names.  Perhaps that what 
you've seen before?


dw