[cfe-dev] static initialization of global namespace scope constexpr objects

Richard Smith richard at metafoo.co.uk
Mon Jun 11 22:19:17 PDT 2012


On Mon, Jun 11, 2012 at 6:27 PM, Suman Kar <skarpio at gmail.com> wrote:

> Hello,
>
> I am perusing through N2235 (Generalized Constant Expressions) and was
> trying
> out the example on page 8, namely:
>
> struct complex {
>        constexpr complex(double r, double i) : re(r), im(i) { }
>        constexpr double real() { return re; }
>        constexpr double imag() { return im; }
> private:
>        double re;
>        double im;
> };
> constexpr complex I(0, 1); // OK -- literal complex
>
> constexpr double d = I.real(); // added by me
>
> I generated the assembler output with my clang++ build:
>
> $ /d/llvm_workspace/build/Debug+Asserts/bin/clang++ -v
> clang version 3.2 (trunk 157115) (llvm/trunk 157155)
> Target: i686-pc-mingw32
> Thread model: posix
>
> and it churned out the following:
>
>        .data
>        .align  8
> __ZL1I:
>        .quad   0
>        .quad   4607182418800017408
>
>        .align  8
> __ZL1d:
>        .quad   0
>

This looks like a backend issue: it appears that we don't support .rodata
when targeting i686-pc-mingw32. For i686-linux-gnu, we produce this:

        .file   "constexpr.cpp"
        .type   _ZL1I, at object           # @_ZL1I
        .section        .rodata,"a", at progbits
        .align  4
_ZL1I:
        .quad   0                       # double 0.000000e+00
                                        #  (0x0)
        .quad   4607182418800017408     # double 1.000000e+00
                                        #  (0x3ff0000000000000)
        .size   _ZL1I, 16

        .type   _ZL1d, at object           # @_ZL1d
        .align  8
_ZL1d:
        .quad   0                       # double 0.000000e+00
                                        #  (0x0)
        .size   _ZL1d, 8


        .section        ".note.GNU-stack","", at progbits


> Also, does 3.6.2/3 mandate that static initialization will not occur for
> objects
> with extern linkage?


3.6.2/3 doesn't mandate anything. Instead, it gives the implementation
permission to transform dynamic initialization into static initialization
in some circumstances.


> IOW, the following:
>
> extern constexpr complex I(0, 1); // OK -- literal complex
>
> will always require dynamic initialization and can thus never be in a
> ro-section?


No, static initialization (in particular, constant initialization) is
required here, because the variable is constexpr. Clang (well, LLVM) emits
this variable into an RO section on targets where it knows how to do so.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120611/f9915ca2/attachment.html>


More information about the cfe-dev mailing list