public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* need volatile for asm?
@ 2011-02-15 23:58 kevin diggs
  2011-02-16  5:25 ` Andi Hellmund
  0 siblings, 1 reply; 6+ messages in thread
From: kevin diggs @ 2011-02-15 23:58 UTC (permalink / raw)
  To: gcc-help

Hi,

Does the asm in:

#include <stdio.h>

int main(int argc, char *argv[])
{
unsigned int pc;

	asm("\n\t"
		"call 1f\n\t"
		"1: pop %0\n"
		:"=g"(pc)
	);

	printf(__FILE__"`%s()-%d:  %%pc is %p\n",__func__,__LINE__,pc);

	return 0;
}

need volatile?

Thanks!

kevin

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: need volatile for asm?
  2011-02-15 23:58 need volatile for asm? kevin diggs
@ 2011-02-16  5:25 ` Andi Hellmund
  2011-02-16 10:33   ` Andrew Haley
  2011-02-16 10:59   ` Drasko DRASKOVIC
  0 siblings, 2 replies; 6+ messages in thread
From: Andi Hellmund @ 2011-02-16  5:25 UTC (permalink / raw)
  To: kevin diggs; +Cc: gcc-help

On 02/16/2011 12:11 AM, kevin diggs wrote:
> Hi,
>
> Does the asm in:
>
> #include<stdio.h>
>
> int main(int argc, char *argv[])
> {
> unsigned int pc;
>
> 	asm("\n\t"
> 		"call 1f\n\t"
> 		"1: pop %0\n"
> 		:"=g"(pc)
> 	);
>
> 	printf(__FILE__"`%s()-%d:  %%pc is %p\n",__func__,__LINE__,pc);
>
> 	return 0;
> }
>
> need volatile?
>
> Thanks!
>
> kevin
>

Hey Kevin

no, you don't need the volatile keyword since the output operand (pc) is 
live after the asm statement which puts a side-effect on the asm statement.

A possible way to verify the need of volatile would be to compile the 
code at a high optimization level, e.g. -O3 and check the assembly code 
(-S option) if the inline assembly was removed or NOT.

Best regards,
Andi

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: need volatile for asm?
  2011-02-16  5:25 ` Andi Hellmund
@ 2011-02-16 10:33   ` Andrew Haley
  2011-02-16 10:59   ` Drasko DRASKOVIC
  1 sibling, 0 replies; 6+ messages in thread
From: Andrew Haley @ 2011-02-16 10:33 UTC (permalink / raw)
  To: gcc-help

On 02/15/2011 11:58 PM, Andi Hellmund wrote:
> On 02/16/2011 12:11 AM, kevin diggs wrote:
>> Hi,
>>
>> Does the asm in:
>>
>> #include<stdio.h>
>>
>> int main(int argc, char *argv[])
>> {
>> unsigned int pc;
>>
>> asm("\n\t"
>> "call 1f\n\t"
>> "1: pop %0\n"
>> :"=g"(pc)
>> );
>>
>> printf(__FILE__"`%s()-%d: %%pc is %p\n",__func__,__LINE__,pc);
>>
>> return 0;
>> }
>>
>> need volatile?

>
> no, you don't need the volatile keyword since the output operand
> (pc) is live after the asm statement which puts a side-effect on the
> asm statement.

I'm not sure about that.  Because there is no input operand, gcc is
free to move the asm.  Volatile will prevent that from happening.

I'd do this:

#include <stdio.h>

int main(int argc, char *argv[])
{
   unsigned int pc;

   pc = (unsigned int)&&label;

  label:
   printf(__FILE__"`%s()-%d: %%pc is %p\n",__func__,__LINE__,pc);

   return 0;
}

Andrew.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: need volatile for asm?
  2011-02-16  5:25 ` Andi Hellmund
  2011-02-16 10:33   ` Andrew Haley
@ 2011-02-16 10:59   ` Drasko DRASKOVIC
  2011-02-16 11:18     ` Andi Hellmund
  1 sibling, 1 reply; 6+ messages in thread
From: Drasko DRASKOVIC @ 2011-02-16 10:59 UTC (permalink / raw)
  To: Andi Hellmund; +Cc: kevin diggs, gcc-help

On Wed, Feb 16, 2011 at 12:58 AM, Andi Hellmund <mail@andihellmund.com> wrote:
> On 02/16/2011 12:11 AM, kevin diggs wrote:

> A possible way to verify the need of volatile would be to compile the code
> at a high optimization level, e.g. -O3 and check the assembly code (-S
> option) if the inline assembly was removed or NOT.

Would this be portable check, guaranteeing that compilation on other
machine with other gcc version will not do opposite ?

And anyway, why not always putting it to volatile ? Once you started
embedding asm code, I guess you had a good reason for it to appear in
the output code...

BR,
Drasko

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: need volatile for asm?
  2011-02-16 10:59   ` Drasko DRASKOVIC
@ 2011-02-16 11:18     ` Andi Hellmund
  2011-02-16 12:12       ` David Brown
  0 siblings, 1 reply; 6+ messages in thread
From: Andi Hellmund @ 2011-02-16 11:18 UTC (permalink / raw)
  To: Drasko DRASKOVIC; +Cc: kevin diggs, gcc-help

 > Would this be portable check, guaranteeing that compilation on other
 > machine with other gcc version will not do opposite ?

You are right, this is definitely NOT portable

> And anyway, why not always putting it to volatile ? Once you started
> embedding asm code, I guess you had a good reason for it to appear in
> the output code...

Good point.

Andi

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: need volatile for asm?
  2011-02-16 11:18     ` Andi Hellmund
@ 2011-02-16 12:12       ` David Brown
  0 siblings, 0 replies; 6+ messages in thread
From: David Brown @ 2011-02-16 12:12 UTC (permalink / raw)
  To: gcc-help

On 16/02/2011 11:59, Andi Hellmund wrote:
>  > Would this be portable check, guaranteeing that compilation on other
>  > machine with other gcc version will not do opposite ?
>
> You are right, this is definitely NOT portable
>
>> And anyway, why not always putting it to volatile ? Once you started
>> embedding asm code, I guess you had a good reason for it to appear in
>> the output code...
>
> Good point.
>

There are certainly times when you want to use embedded assembly, but 
don't always want it to appear on the output code - then gcc's ability 
to optimise and rearrange inline assembly is very nice.  But this isn't 
one of the cases :-)



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2011-02-16 11:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-15 23:58 need volatile for asm? kevin diggs
2011-02-16  5:25 ` Andi Hellmund
2011-02-16 10:33   ` Andrew Haley
2011-02-16 10:59   ` Drasko DRASKOVIC
2011-02-16 11:18     ` Andi Hellmund
2011-02-16 12:12       ` David Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).