Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Thread

Constructors

Constructors

2004-09-20 by Rodrigo Cesar da Silva Martins

How can I use class constructors with gnu arm? I'm using a LPC2129 and
Keil development system. 
For example:
//header file
class SPI
{
            public:
            SPI(char init);
            .
}
//source file
SPI::SPI(char init)
{
            .
}
//application file
 SPI ADSPI(0); //spi channel for AD converter
 
The constructor function should be called when I declare an object, but
it doesn't happen.
 
Thanks in advance,
Rodrigo Martins
Intermetric Instrumentos Ltda
(11) 4761-8009
rodrigo@...
 


[Non-text portions of this message have been removed]

Re: [lpc2000] Constructors

2004-09-21 by Pawel Sikora

> The constructor function should be called when I
> declare an object, but
> it doesn't happen.

Probably the startup code and linker script don't
support .ctors and .dtors sections.
The .ctors and .dtors sections are lists of function
pointers to global c++ object constructors
and destructors.


	
		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!
http://promotions.yahoo.com/new_mail

Re: Constructors

2004-09-22 by dave_albert

Here's a snippet of code I use to call C++ constructors in crt0.S
I call it after clearing .bss and initializing .data:


     /* Call global C++ object constructors */

     ldr     r4, =__CTOR_LIST__      @ Null terminated list of ctors
Ctor_Loop:      
     ldr     r5, [r4], #+4           @ Fetch constructor address
     cmp     r5, #0                  @ Test for end of list
     addne   lr, pc, #4              @ Save return address
     movne   pc, r5                  @ Call constructor
     bne     Ctor_Loop               @ Continue for all constructors

Good luck!

--- In lpc2000@yahoogroups.com, "Rodrigo Cesar da Silva Martins"
<rodrigo@i...> wrote:
Show quoted textHide quoted text
> How can I use class constructors with gnu arm? I'm using a LPC2129 and
> Keil development system. 
> For example:
> //header file
> class SPI
> {
>             public:
>             SPI(char init);
>             .
> }
> //source file
> SPI::SPI(char init)
> {
>             .
> }
> //application file
>  SPI ADSPI(0); //spi channel for AD converter
>  
> The constructor function should be called when I declare an object, but
> it doesn't happen.
>  
> Thanks in advance,
> Rodrigo Martins
> Intermetric Instrumentos Ltda
> (11) 4761-8009
> rodrigo@i...
>  
> 
> 
> [Non-text portions of this message have been removed]

Re: Constructors

2004-09-22 by Karl Olsen

--- In lpc2000@yahoogroups.com, "dave_albert" <david_albert@h...> 
wrote:
> Here's a snippet of code I use to call C++ constructors in crt0.S
> I call it after clearing .bss and initializing .data:
> 
> 
>      /* Call global C++ object constructors */
> 
>      ldr     r4, =__CTOR_LIST__      @ Null terminated list of ctors
> Ctor_Loop:      
>      ldr     r5, [r4], #+4           @ Fetch constructor address
>      cmp     r5, #0                  @ Test for end of list
>      addne   lr, pc, #4              @ Save return address
>      movne   pc, r5                  @ Call constructor
>      bne     Ctor_Loop               @ Continue for all constructors
> 

Won't the called constructor likely modify the zero flag so that 
the "bne Ctor_Loop" might terminate the loop prematurely?

Karl Olsen

Re: Constructors

2004-09-22 by dave_albert

You're absolutely right...thanks Karl!

Clearly my constructors aren't doing any comparisons that set the zero
flag (since this works for me)

per Karl's comment, the comparison of r5 to zero should be done again
after the constructor returns and before the bne... and then there are
probably more elegant solutions.

--- In lpc2000@yahoogroups.com, "Karl Olsen" <kro@p...> wrote:
Show quoted textHide quoted text
> --- In lpc2000@yahoogroups.com, "dave_albert" <david_albert@h...> 
> wrote:
> > Here's a snippet of code I use to call C++ constructors in crt0.S
> > I call it after clearing .bss and initializing .data:
> > 
> > 
> >      /* Call global C++ object constructors */
> > 
> >      ldr     r4, =__CTOR_LIST__      @ Null terminated list of ctors
> > Ctor_Loop:      
> >      ldr     r5, [r4], #+4           @ Fetch constructor address
> >      cmp     r5, #0                  @ Test for end of list
> >      addne   lr, pc, #4              @ Save return address
> >      movne   pc, r5                  @ Call constructor
> >      bne     Ctor_Loop               @ Continue for all constructors
> > 
> 
> Won't the called constructor likely modify the zero flag so that 
> the "bne Ctor_Loop" might terminate the loop prematurely?
> 
> Karl Olsen

Re: Constructors

2004-09-25 by dave_albert

Note that you will also need to modify your linker script.  Mine has
the following immediately after the .text segment:


                /* Build table of global constructors to call */
                __CTOR_LIST__ = .;
                *(.ctors)
                LONG(0)
                __CTOR_END__ = .;

                /* Build table of global destructors to call on exit*/
                __DTOR_LIST__ = .;
                *(.dtors)
                LONG(0)
                __DTOR_END__ = .;


--- In lpc2000@yahoogroups.com, "dave_albert" <david_albert@h...> wrote:
Show quoted textHide quoted text
> You're absolutely right...thanks Karl!
> 
> Clearly my constructors aren't doing any comparisons that set the zero
> flag (since this works for me)
> 
> per Karl's comment, the comparison of r5 to zero should be done again
> after the constructor returns and before the bne... and then there are
> probably more elegant solutions.
> 
> --- In lpc2000@yahoogroups.com, "Karl Olsen" <kro@p...> wrote:
> > --- In lpc2000@yahoogroups.com, "dave_albert" <david_albert@h...> 
> > wrote:
> > > Here's a snippet of code I use to call C++ constructors in crt0.S
> > > I call it after clearing .bss and initializing .data:
> > > 
> > > 
> > >      /* Call global C++ object constructors */
> > > 
> > >      ldr     r4, =__CTOR_LIST__      @ Null terminated list of ctors
> > > Ctor_Loop:      
> > >      ldr     r5, [r4], #+4           @ Fetch constructor address
> > >      cmp     r5, #0                  @ Test for end of list
> > >      addne   lr, pc, #4              @ Save return address
> > >      movne   pc, r5                  @ Call constructor
> > >      bne     Ctor_Loop               @ Continue for all constructors
> > > 
> > 
> > Won't the called constructor likely modify the zero flag so that 
> > the "bne Ctor_Loop" might terminate the loop prematurely?
> > 
> > Karl Olsen

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.