delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2007/04/18/10:15:51

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
NNTP-Posting-Date: Mon, 16 Apr 2007 22:30:43 -0500
From: "Alexei A. Frounze" <alexfru AT chat DOT ru>
Newsgroups: comp.os.msdos.djgpp
Subject: gcc bug?
Date: Mon, 16 Apr 2007 20:27:07 -0700
MIME-Version: 1.0
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3028
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028
Message-ID: <LbadnVsa3_H-ornbnZ2dnUVZ_sudnZ2d@comcast.com>
Lines: 125
NNTP-Posting-Host: 67.170.72.236
X-Trace: sv3-QkB/SFwSDwLetF76dpO0GslJotE1jWJK+N80LKidDQ71C7MZsHMSYf19uGJaF2xBZm1YxKKlhDNFfHJ!QA8R4Fgmt2BP1ZFsTeUYSdlKqRsnYPtd8shdRmOqbn2V16QQhOpOOIE4bWdt1ZKH6temvOT8pQo5!HD/wNDrxlHCsI9eP5E5i8iM7nf0rlA==
X-Complaints-To: abuse AT comcast DOT net
X-DMCA-Complaints-To: dmca AT comcast DOT net
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.34
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

I was checking my understanding of volatile's effect and encountered an odd 
thing.

Consider the following code:
----8<----
typedef struct {char x;} T, *PT;

void f1 ()
{
  volatile T* pt = 0;
  while (pt->x != 0);
}

void f2 ()
{
  volatile PT pt = 0;
  while (pt->x != 0);
}

int main()
{
  return 0;
}

----8<----

Now, let's compile it to asm using: gcc -Wall -c -S -O ptr.c -o ptr.s:
----8<----
        .file   "ptr.c"
        .section .text
.globl _f1
_f1:
        pushl   %ebp
        movl    %esp, %ebp
        movl    $0, %edx
L2:
        movb    (%edx), %al
        testb   %al, %al
        jne     L2
        popl    %ebp
        ret
.globl _f2
_f2:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $4, %esp
        movl    $0, -4(%ebp)
L6:
        movl    -4(%ebp), %eax
        cmpb    $0, (%eax)
        jne     L6
        movl    %ebp, %esp
        popl    %ebp
        ret
.globl _main
_main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        movl    $0, %eax
        movl    %ebp, %esp
        popl    %ebp
        ret
        .ident  "GCC: (GNU) 3.3.4"
----8<----

Let's now compile it to asm using higher optimization level: 
gcc -Wall -c -S -O ptr.c -o ptr.s:
----8<----
        .file   "ptr.c"
        .section .text
        .p2align 4,,15
.globl _f1
_f1:
        pushl   %ebp
        movl    %esp, %ebp
        .p2align 4,,7
L2:
        movb    0, %al
        testb   %al, %al
        jne     L2
        popl    %ebp
        ret
        .p2align 4,,15
.globl _f2
_f2:
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %eax
        movl    $0, -4(%ebp)
        .p2align 4,,7
L6:
        movl    -4(%ebp), %eax
        cmpb    $0, (%eax)
        jne     L6
        movl    %ebp, %esp
        popl    %ebp
        ret
        .p2align 4,,15
.globl _main
_main:
        pushl   %ebp
        movl    %esp, %ebp
        xorl    %eax, %eax
        pushl   %edx
        pushl   %edx
        andl    $-16, %esp
        movl    %ebp, %esp
        popl    %ebp
        ret
        .ident  "GCC: (GNU) 3.3.4"
----8<----

Note the difference in f1:
- if the optimization is -O, then there's a pointer in the routine and it 
gets dereferenced
- if the optimization is -O2 (or -O3), then there's no pointer in the 
routine at all, instead there's a non-pointer variable that is initialized 
with the value that should've initialized the missing pointer

Am I missing something, or is this really a bug?

Alex

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019