🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Urs Hoelzle Wed, 07 Oct 1992 21:03
g++ generates superfluous global constructor
#451
Author: Urs Hoelzle
Date: Wed, 07 Oct 1992 21:03
59 lines
2259 bytes
The following code segment produces a global initializer when compiled
with g++, even though it's just a simple constant initialization:

    typedef void (*fntype)();
    extern void fn();

    void* f = &fn;

Code produced by g++ (2.2.2, Sparc):

gcc2_compiled.:
.text
        .align 4
        .global __GLOBAL_$I$x_c
        .proc   020
__GLOBAL_$I$x_c:
        !#PROLOGUE# 0
        save %sp,-112,%sp
        !#PROLOGUE# 1
        sethi %hi(_fn__Fv),%g2
        or %g2,%lo(_fn__Fv),%g2
        sethi %hi(_f),%g3
        st %g2,[%g3+%lo(_f)]
        ret
        restore
        .global _f
        .common _f,8,"bss"

Code produced by gcc (i.e C mode instead of C++ mode):

gcc2_compiled.:
        .global _f
.data
        .align 4
_f:
        .word _fn__Fv

Interestingly, g++ generates the correct code (i.e, none :-) if the & is
dropped (i.e.  void* f = fn;).  But unfortunately the & can't be dropped
when initializing f with a member function.

The problem is that in my application I have a large array of structs
looking roughly like

      struct s { fntype f; int flag1, flag2, flag3, flag4, flag5; }

and g++ generates many kilobytes of code to initialize each of the
values at startup (even the flags!!).  The other problem is that if
you put such code in a dynamic link library to be called by C, you'll
be screwed because the initializer isn't run.  There shouldn't be any
global initializers if there aren't any global objects.

Any fix or workaround?

-Urs

----------------------------------------------------------------------------
Urs Hoelzle                                              urs@cs.stanford.EDU
Computer Systems Laboratory, CIS 57, Stanford University, Stanford, CA 94305
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads