Excluded functions with cc65 inline assembler from optimization.

The cc65 documentation: "Note: Inline assembler statements are subject to all optimizations done by the compiler. There is currently no way to protect an inline assembler statement from being moved or removed completely by the optimizer. If in doubt, check the generated assembler output, or disable optimizations."

Unfortunately we learned recently the hard way that this is by no means no theoretical concern. In fact the cc65 optimizer removed several asm statements from contiki-cc65/uip/uip_arch.c.

Fortunately the cc65 compiler (starting with mid-July 2004 snapshots) now understands an optimization pragma which allows to control optimization on a per-function level.

The pragma was inserted into the cc65 specific codebase without macro encapsulation or #ifdef because:

a) Older cc65 versions just issue an warning on the pragma - which makes perfectly sense as one might want to check the generated code in question manually.

b) The current cc65 version needs the pragma as a hint to generate correct code.

c) Future cc65 versions will probably exclude functions containing inline assembler from optimization automatically thus turning the pragma unnecessary - but still it doesn't hurt.
diff --git a/contiki-plus4/apps/vnc-draw.c b/contiki-plus4/apps/vnc-draw.c
index 816cd9b..4ee96db 100644
--- a/contiki-plus4/apps/vnc-draw.c
+++ b/contiki-plus4/apps/vnc-draw.c
@@ -32,7 +32,7 @@
  *
  * This file is part of the Contiki VNC client
  *
- * $Id: vnc-draw.c,v 1.1 2003/04/11 20:30:15 adamdunkels Exp $
+ * $Id: vnc-draw.c,v 1.2 2004/07/18 13:23:28 oliverschmidt Exp $
  *
  */
 
@@ -246,6 +246,7 @@
 
 #endif /* 0 */
 #if 0
+#pragma optimize(push, off)
 void
 c64_scroll_up(unsigned char c)
 {
@@ -343,7 +344,9 @@
   asm("pla");
   asm("sta $f7");
 }
+#pragma optimize(pop)
 /*-----------------------------------------------------------------------------------*/
+#pragma optimize(push, off)
 void
 c64_scroll_down(unsigned char c)
 {
@@ -443,7 +446,9 @@
   asm("pla");
   asm("sta $f7");
 }
+#pragma optimize(pop)
 /*-----------------------------------------------------------------------------------*/
+#pragma optimize(push, off)
 void
 c64_scroll_right(unsigned char c)
 {
@@ -562,7 +567,9 @@
   asm("pla");
   asm("sta $f7");
 }
+#pragma optimize(pop)
 /*-----------------------------------------------------------------------------------*/
+#pragma optimize(push, off)
 void
 c64_scroll_left(unsigned char c)
 {
@@ -677,6 +684,7 @@
   asm("pla");
   asm("sta $f7");
 }
+#pragma optimize(pop)
 /*-----------------------------------------------------------------------------------*/
 
 
diff --git a/contiki-plus4/uip/uip_arch.c b/contiki-plus4/uip/uip_arch.c
index 2bb2898..b43fc26 100644
--- a/contiki-plus4/uip/uip_arch.c
+++ b/contiki-plus4/uip/uip_arch.c
@@ -28,7 +28,7 @@
  *
  * This file is part of the uIP TCP/IP stack.
  *
- * $Id: uip_arch.c,v 1.2 2004/07/04 20:01:31 adamdunkels Exp $
+ * $Id: uip_arch.c,v 1.3 2004/07/18 13:23:48 oliverschmidt Exp $
  *
  */
 
@@ -42,6 +42,7 @@
 /*-----------------------------------------------------------------------------------*/
 #if UIP_BUFSIZE > 255
 /*-----------------------------------------------------------------------------------*/
+#pragma optimize(push, off)
 void
 uip_add32(u8_t *op32, u16_t op16)
 {
@@ -69,9 +70,11 @@
   asm("adc #0");
   asm("sta _uip_acc32+0");  
 }
+#pragma optimize(pop)
 /*-----------------------------------------------------------------------------------*/
 #else /* UIP_BUFSIZE > 255 */
 /*-----------------------------------------------------------------------------------*/
+#pragma optimize(push, off)
 void
 uip_add32(u8_t *op32, u8_t op8)
 {
@@ -98,12 +101,14 @@
   asm("adc #0");
   asm("sta _uip_acc32+0");  
 }
+#pragma optimize(pop)
 /*-----------------------------------------------------------------------------------*/
 #endif /* UIP_BUFSIZE > 255 */
 
 static u16_t chksum_ptr, chksum_len, chksum_tmp;
 static u16_t chksum(void);
 /*-----------------------------------------------------------------------------------*/
+#pragma optimize(push, off)
 u16_t
 chksum(void) {
 
@@ -198,6 +203,7 @@
   asm("lda tmp1");
   asm("ldx tmp1+1");
 }
+#pragma optimize(pop)
 /*-----------------------------------------------------------------------------------*/
 u16_t
 uip_chksum(u16_t *buf, u16_t len)
@@ -238,6 +244,7 @@
   return chksum();
 }
 /*-----------------------------------------------------------------------------------*/
+#pragma optimize(push, off)
 u16_t
 uip_tcpchksum(void)
 {  
@@ -359,4 +366,5 @@
 
   return chksum_tmp;
 }
+#pragma optimize(pop)
 /*-----------------------------------------------------------------------------------*/