This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: why the barrier() can not take effect?


On 12/24/2010 08:32 AM, Parmenides wrote:
I have modified the code as follows:

#include<stdio.h>

#define barrier() __asm__ __volatile__("": : :"memory")

int main()
{
      int i, n;
      int s = 0;
      int *p;

      scanf("%d",&n);
      for (i = 0; i<  n; i++)
           s += i;
      barrier();
      p =&s;<--- for s, the&  operator is used
      printf("%d %d %d\n", i, n, *p);<--- the s is referenced through a pointer

      return 0;
}

and the corresponding asm code is as follows:

         leal    -8(%ebp), %eax
         pushl   %eax
         pushl   $.LC0
         call    scanf
         movl    -8(%ebp), %edx
         addl    $16, %esp
         testl   %edx, %edx
         jg      .L2
         movl    $0, %eax
         movl    $0, %ecx
         jmp     .L4
.L2:
         movl    $0, %eax
         movl    $0, %ecx
.L5:
         addl    %eax, %ecx
         incl    %eax
         cmpl    %edx, %eax
         jne     .L5
.L4:
        <---  after the barrier(()
         pushl   %ecx<---  In spite of using the '&' operator, the
value of s is still gotten from the register rather than the
corresponding memory location.
         pushl   -8(%ebp)<--- the value of n is gotten from its
memory location indeed
         pushl   %eax
         pushl   $.LC1
         call    printf

Alough the '&' operator is used for both 's' and 'n', gcc deals with
them differently. The former's value is in a register, while the
latter's value is retrieved from the corresponding memory location. I
wonder the difference.

Please don't top-post.


I don't know why you'd expect a difference, really. Try this:

#include <stdio.h>

#define barrier() __asm__ __volatile__(";": : :"memory")

int s = 0;

int main()
{
     int i, n;
     int *p;

     scanf("%d", &n);
     for (i = 0; i < n; i++)
          s += i;
     p = &s;  // <--- for s, the & operator is used
     barrier();
     printf("%d %d %d\n", i, n, *p);  // <--- the s is referenced through a pointer

     return 0;
}

Andrew.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]