Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VC6sp5 ignoring frame-pointer omission?

23 views
Skip to first unread message

Trevor Perrin

unread,
Aug 18, 2004, 4:15:36 PM8/18/04
to
I can't seem to make VC6sp5 omit the frame pointer. When I compile
the below file in a Console App, under Release Builds, or Debug Builds
with /Oy, I can look at the disassembly and see use of the bp
register.

I discovered this when trying to use the bp register within some
inline assembly code, compiled under a Release Build with /O2, and
noticing that it crashed the program.

Does anyone know why this happens?

#pragma optimize("y", on)
int f(int x)
{
int a = 8;
int b = 9;
return x + 7 + a + b;
}

int main()
{
f(78);
}


Trevor

Trevor Perrin

unread,
Aug 18, 2004, 4:15:52 PM8/18/04
to

Doug Harrison [MVP]

unread,
Aug 18, 2004, 4:45:17 PM8/18/04
to
Trevor Perrin wrote:

It appears you need to include the "g" option in your #pragma.

--
Doug Harrison
Microsoft MVP - Visual C++

Trevor Perrin

unread,
Aug 19, 2004, 3:07:14 AM8/19/04
to
"Doug Harrison [MVP]" <d...@mvps.org> wrote in message news:<nof7i05rt7tea17jg...@4ax.com>...
> Trevor Perrin wrote:
> >I can't seem to make VC6sp5 omit the frame pointer. [...]
> It appears you need to include the "g" option in your #pragma.

Doug,

Thanks for the response, but I can't make it work. I've tried setting
the /Gy and /G2 options. I've tried different combinations of #pragma
optimize with the "g" and "y" options. The below program crashes in
every case (create a console app with a single .c file, run with
VC6sp5, to reproduce).

Could you point out exactly what steps will make the compiler not use
the frame pointer, so code like below will work?

int f(int x)
{
int a = 8;
int b = 9;

_asm { mov bp, 0 }
return a + b + x;
}

int main()
{
f(78);
}


Trevor

Bo Persson

unread,
Aug 19, 2004, 5:50:31 AM8/19/04
to

"Trevor Perrin" <tr...@trevp.net> skrev i meddelandet
news:47e891f1.04081...@posting.google.com...

> "Doug Harrison [MVP]" <d...@mvps.org> wrote in message
news:<nof7i05rt7tea17jg...@4ax.com>...
> > Trevor Perrin wrote:
> > >I can't seem to make VC6sp5 omit the frame pointer. [...]
> > It appears you need to include the "g" option in your #pragma.
>
> Doug,
>
> Thanks for the response, but I can't make it work. I've tried setting
> the /Gy and /G2 options. I've tried different combinations of #pragma
> optimize with the "g" and "y" options. The below program crashes in
> every case (create a console app with a single .c file, run with
> VC6sp5, to reproduce).
>
> Could you point out exactly what steps will make the compiler not use
> the frame pointer, so code like below will work?

I think you can do this!

The "Omit frame pointer" option is just a hint to the compiler that it
should avoid setting up frame pointers for functions that don't need
one. That doesn't free up the BP register, but only saves some code in
the affected functions.

Some functions (main(), perhaps?) actually *need* a frame pointer, and
will get one regardless of the options chosen.


Bo Persson


Bo Persson

unread,
Aug 19, 2004, 7:12:41 AM8/19/04
to

"Bo Persson" <b...@gmb.dk> skrev i meddelandet
news:urL82Hdh...@TK2MSFTNGP09.phx.gbl...

>
> "Trevor Perrin" <tr...@trevp.net> skrev i meddelandet
> news:47e891f1.04081...@posting.google.com...
> > "Doug Harrison [MVP]" <d...@mvps.org> wrote in message
> news:<nof7i05rt7tea17jg...@4ax.com>...
> > > Trevor Perrin wrote:
> > > >I can't seem to make VC6sp5 omit the frame pointer. [...]
> > > It appears you need to include the "g" option in your #pragma.
> >
> > Doug,
> >
> > Thanks for the response, but I can't make it work. I've tried
setting
> > the /Gy and /G2 options. I've tried different combinations of
#pragma
> > optimize with the "g" and "y" options. The below program crashes in
> > every case (create a console app with a single .c file, run with
> > VC6sp5, to reproduce).
> >
> > Could you point out exactly what steps will make the compiler not
use
> > the frame pointer, so code like below will work?
>
> I think you can do this!

Should of course be: I think you can NOT do this!

Doh!


Bo Persson


Doug Harrison [MVP]

unread,
Aug 19, 2004, 11:46:35 AM8/19/04
to
Trevor Perrin wrote:

Well, that's interesting. The __asm statement actually causes it to use the
frame pointer. Be that as it may, according to the documentation here, you
should preserve EBP around __asm blocks:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_core_Using_and_Preserving_Registers_in_Inline_Assembly.asp?frame=true
<quote>
You should preserve other registers you use (such as DS, SS, SP, BP, and
flags registers) for the scope of the __asm block. You should preserve the
ESP and EBP registers unless you have some reason to change them (to switch
stacks, for example).
</quote>

I dunno, maybe tagging the function with __declspec(naked) could help you.

Trevor Perrin

unread,
Aug 19, 2004, 3:01:36 PM8/19/04
to
"Bo Persson" <b...@gmb.dk> wrote in message news:<urL82Hdh...@TK2MSFTNGP09.phx.gbl>...

> "Trevor Perrin" <tr...@trevp.net> skrev i meddelandet
> news:47e891f1.04081...@posting.google.com...
> > "Doug Harrison [MVP]" <d...@mvps.org> wrote in message
> news:<nof7i05rt7tea17jg...@4ax.com>...
> > > Trevor Perrin wrote:
> > > >I can't seem to make VC6sp5 omit the frame pointer. [...]
> > > It appears you need to include the "g" option in your #pragma.
> >
> > Doug,
> >
> > Thanks for the response, but I can't make it work. I've tried setting
> > the /Gy and /G2 options. I've tried different combinations of #pragma
> > optimize with the "g" and "y" options. The below program crashes in
> > every case (create a console app with a single .c file, run with
> > VC6sp5, to reproduce).
> >
> > Could you point out exactly what steps will make the compiler not use
> > the frame pointer, so code like below will work?
>
> I think you can [not] do this!

>
> The "Omit frame pointer" option is just a hint to the compiler that it
> should avoid setting up frame pointers for functions that don't need
> one.[...]

Mm, Okay. I guess it only does that for functions that don't access
locals? gcc's -fomit-frame-pointer will compile my f() function
accessing locals by offsets from SP. It will still crash, cause
main() uses BP, but I can work around that... I assumed VC would do
the same (access locals from SP), and it's a little disappointing it
doesn't. But I can live with it.

Thanks for the help!

Trevor

0 new messages