Re: [cc65] Optimizing code fails - Found the error!

Date view Thread view Subject view

From: Geoffrey Wossum (
Date: 2003-11-22 21:13:07

On Saturday 22 November 2003 05:54 am, Ullrich von Bassewitz wrote:

> I have currently no idea how to change that without removing functionality.
> Not the best idea, but at least a workaround to prevent people from falling
> into this problem would be to remove the RTI instruction from the inline
> assembler, so one will have to use the external assembler for interrupt
> handlers. Better ideas anyone?

An immediate work-around to the problem for the person having the problem 
would be to separate the ISR into a separate source file, and compile it 
without optimization.  The rest of the code could be compiled with 
optimizations on.

For gcc on the AVR microcontrollers, there's an __interrupt__ attribute that 
is used for ISR's, which insures that proper prologue and epilogue code is 
generated.  When writing ISR's for avr-gcc, two macros are provided, 
INTERRUPT and SIGNAL.  They're virtually identical, except INTERRUPT 
re-enables global interrupts right after the prologue code.  There's also 
names for every possible interrupt vector, such as SIG_UART0_RECV, which 
translates to something like __vector11.  So to write an ISR in C using 
avr-gcc, you write:

     /* handle interrupt */

which would expand to something like:

void __vector11(void) __attribute__ ((interrupt));
void __vector11(void)
    /* handle interrupt */

Since all the ISR's have well known names due to macros, the linker can then 
also go ahead and build the interrupt vector table for you.  Since GNU ld 
supports weak references, the linker will fill in a do nothing ISR in the IVT 
for any vectors your code does not define.  

Probably not trivial to add functionality like this into cc65, but it is ever 
so nice to use.  Not sure how many people write ISR's for cc65 programs 
either, although I for one will almost always be writing ISR's when using 

Another possibility is to define prologue and epilogues macros that have to be 
used in ISR's.  gcc for the Palm did something similar to this as a 
workaround measure when writing PalmOS callbacks.  I guess the macro would 
have to something to trick the compiler optimizer, like having a couple of 
returns at the end or something.  It'd probably waste a few more instructions 
than adding an attribute to the compiler, but it'd be fast to implement, 
assuming you could trick the optimizer.

Geoffrey Wossum
Software Engineer
Long Range Systems -  
To unsubscribe from the list send mail to with
the string "unsubscribe cc65" in the body(!) of the mail.

Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2003-11-22 21:22:33 CET