Re: Are there any 8051 C++ compilers ?

From: Ed Beroset (beroset_at_mindspring.com)
Date: 08/08/04


Date: Sun, 08 Aug 2004 18:56:45 GMT

Bjarne Stroustrup wrote:
> Jonathan Kirwan <jkirwan@easystreet.com> wrote
>
>>This usage in C++, that of a 'const int' for example, could (if the C+ linker
>>would support it) remove the instance of storage if the address isn't taken. In
>>that sense, it could be very useful without incurring a price. But I don't know
>>of any C++ compiler/linker sets that are able to do this, so a const in C++ will
>>take up a memory instance, even if the value is known by all modules at compile
>>time and the address isn't ever needed, at all. Stroustrup, again, insists that
>>it's certainly within the spec to eliminate const's whose addresses aren't taken
>>by any module. But none I've used manage this.
>
>
> Odd. My impression was that with the exception of old GCCs, all C++
> compilers avoids laying down storage for simple consts, such as
>
> const int a = 7;
>
> Even the first C++ compiler, Cfront, did that 20 years ago.

That was my impression, too, but I haven't exhaustively tested. For
what it's worth, here's what g++ version 3.2.2 did. From this input file:

#include <iostream>

const int seven = 7;
const int eight = 8;
const int unused = 99;

int main()
{
     std::cout << "seven plus eight is " << seven+eight << std::endl;
     return 0;
}

I get an assembly file which includes the following:

        pushl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
        subl $12, %esp
        pushl $15 ; note that this is the constant 15
        subl $12, %esp
        pushl $.LC0 ; .LCO is the address of the string
        pushl $_ZSt4cout
.LCFI3:
        call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
        

When I look at the binary (running under Linux), I get:

                              7365 7665 6e20 706c ........seven pl
00007b0: 7573 2065 6967 6874 2069 7320 0000 0000 us eight is ....
00007c0: 0700 0000 0800 0000 011b 033b 2800 0000 ...........;(...
00007d0: 0400 0000 78fe ffff 4800 0000

As you can see, the constants 7 and 8 seem to be present, but the
constant 99 is not. I used no command line parameters for optimization,
so perhaps there is a way to tell it to suppress the creation of these
constants.

Ed



Relevant Pages

  • Re: Are there any 8051 C++ compilers ?
    ... > would support it) remove the instance of storage if the address isn't taken. ... > take up a memory instance, even if the value is known by all modules at compile ... -- Bjarne Stroustrup; http://www.research.att.com/~bs ...
    (comp.arch.embedded)
  • Re: declaration vs. defintion
    ... > identifier being defined ?, ... You shouldn't think of it in terms of storage being allocated. ... const int Five = 5; ... I believe the one-definition rule goes something like this: ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Const from IDL-File
    ... I have defined a "const int" in an idl file(const int ... There is no room to storage "const"s in TLB file. ...
    (microsoft.public.vc.atl)