Mangle functions
Ron Kreymborg
ron@jennaron.com.au
Mon Jan 28 10:22:00 GMT 2008
Is there a way the C++ pre-processor can access the mangled name of a class
method? This would greatly simplify implementing interrupt handlers as
private class methods in embedded systems where the processor uses an
interrupt vector table. Currently one must manually build the mangled name
and pass it to the gcc "alias" attribute.
My idea is a macro like:
#define CLASS_ISR(theclassname, themethodname) \
ISR(theclassname) ISR_ALIASOF(gcc_mangle(theclassname, themethodname)); \
void theclassname::themethodname(void)
where the "gcc_mangle" function is what I am after. If this existed it
would allow automating the creation of classes for embedded systems where
the interrupt handler is a private class method and its "friend" status
allows it access to the class data of the owning device manager class. The
abbreviated example class definitions below show the timer overflow
interrupt handler for the Timer0 peripheral of an Atmel AVR microprocessor:
//-------------------------------------------
class CTimer0Interrupt
{
public:
CTimer0Interrupt();
~CTimer0Interrupt();
private:
// Overflow interrupt handler.
void TIMER0_OVF_vect(void) __attribute__ ((signal, __INTR_ATTRS));
};
//-------------------------------------------
class CTimer0
{
friend class CTimer0Interrupt;
public:
CTimer0();
~CTimer0();
int GetTimer0Flag(void); // true if overflow occurred
private:
void SetOverflowFlag(void); // called from int handler
private:
volatile bool mTimer0Flag;
CTimer0Interrupt mTimer0Interrupt;
};
And in the cpp class implementation file, the method is:
//---------------------------------------------------------------
// Timer0 overflow interrupt handler.
//
CLASS_ISR(CTimer0Interrupt, TIMER0_OVF_vect)
{
TCNT0 = TIMER0_TIMEOUT; // restart the timeout
Timer0.SetOverflowFlag(); // tell our friend
}
Inserting the mangled name by hand in the macro above produces the vector
alias that allows gcc to link the external C vector name while the C++ code
remains unaware of its existence outside the owning class:
extern "C" void __vector_16 (void) __attribute__ ((signal,used,
externally_visible)) ;
void __vector_16 (void)
__attribute__((alias("_ZN16CTimer0Interrupt11__vector_16Ev")));
void CTimer0Interrupt::__vector_16(void)
{
(*(volatile uint8_t *)((0x32) + 0x20)) = 100;
Timer0.SetOverflowFlag();
}
Ron Kreymborg
More information about the Gcc-help
mailing list