]> git.sur5r.net Git - cc65/blobdiff - src/cc65/codegen.c
Fixed a bug
[cc65] / src / cc65 / codegen.c
index 81273b01396196ef79d500396e21f7c545f98169..6e05351d0d22d1d335214ae46b601f774f0fde6c 100644 (file)
@@ -6,7 +6,7 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2001 Ullrich von Bassewitz                                       */
+/* (C) 1998-2002 Ullrich von Bassewitz                                       */
 /*               Wacholderweg 14                                             */
 /*               D-70597 Stuttgart                                           */
 /* EMail:        uz@cc65.org                                                 */
@@ -39,6 +39,7 @@
 
 /* common */
 #include "check.h"
+#include "strbuf.h"
 #include "version.h"
 #include "xmalloc.h"
 #include "xsprintf.h"
 /* cc65 */
 #include "asmcode.h"
 #include "asmlabel.h"
+#include "casenode.h"
 #include "codeseg.h"
 #include "cpu.h"
 #include "dataseg.h"
 #include "error.h"
 #include "global.h"
 #include "segments.h"
+#include "textseg.h"
 #include "util.h"
 #include "codegen.h"
 
@@ -93,45 +96,53 @@ static void CheckLocalOffs (unsigned Offs)
 
 
 
-static char* GetLabelName (unsigned flags, unsigned long label, unsigned offs)
+static const char* GetLabelName (unsigned Flags, unsigned long Label, long Offs)
 {
-    static char lbuf [128];            /* Label name */
+    static char Buf [256];             /* Label name */
 
     /* Create the correct label name */
-    switch (flags & CF_ADDRMASK) {
+    switch (Flags & CF_ADDRMASK) {
 
        case CF_STATIC:
                    /* Static memory cell */
-           sprintf (lbuf, "%s+%u", LocalLabelName (label), offs);
+           if (Offs) {
+               xsprintf (Buf, sizeof (Buf), "%s%+ld", LocalLabelName (Label), Offs);
+           } else {
+                       xsprintf (Buf, sizeof (Buf), "%s", LocalLabelName (Label));
+           }
            break;
 
        case CF_EXTERNAL:
            /* External label */
-           sprintf (lbuf, "_%s+%u", (char*) label, offs);
+           if (Offs) {
+               xsprintf (Buf, sizeof (Buf), "_%s%+ld", (char*) Label, Offs);
+           } else {
+               xsprintf (Buf, sizeof (Buf), "_%s", (char*) Label);
+           }
            break;
 
        case CF_ABSOLUTE:
            /* Absolute address */
-           sprintf (lbuf, "$%04X", (unsigned)((label+offs) & 0xFFFF));
+           xsprintf (Buf, sizeof (Buf), "$%04X", (int)((Label+Offs) & 0xFFFF));
            break;
 
        case CF_REGVAR:
            /* Variable in register bank */
-           sprintf (lbuf, "regbank+%u", (unsigned)((label+offs) & 0xFFFF));
+           xsprintf (Buf, sizeof (Buf), "regbank+%u", (unsigned)((Label+Offs) & 0xFFFF));
            break;
 
        default:
            Internal ("Invalid address flags");
     }
-
+                   
     /* Return a pointer to the static buffer */
-    return lbuf;
+    return Buf;
 }
 
 
 
 /*****************************************************************************/
-/*                           Pre- and postamble                             */
+/*                           Pre- and postamble                             */
 /*****************************************************************************/
 
 
@@ -139,36 +150,52 @@ static char* GetLabelName (unsigned flags, unsigned long label, unsigned offs)
 void g_preamble (void)
 /* Generate the assembler code preamble */
 {
-    /* Create a new segment list */
+    /* Create a new (global) segment list and remember it */
     PushSegments (0);
+    GS = CS;
 
     /* Identify the compiler version */
+    AddTextLine (";");
     AddTextLine ("; File generated by cc65 v %u.%u.%u",
                 VER_MAJOR, VER_MINOR, VER_PATCH);
+    AddTextLine (";");
 
     /* Insert some object file options */
-    AddTextLine (".fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
+    AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
                    VER_MAJOR, VER_MINOR, VER_PATCH);
 
     /* If we're producing code for some other CPU, switch the command set */
     if (CPU == CPU_65C02) {
-       AddTextLine (".pc02");
+       AddTextLine ("\t.pc02");
     }
 
     /* Allow auto import for runtime library routines */
-    AddTextLine (".autoimport\ton");
+    AddTextLine ("\t.autoimport\ton");
 
     /* Switch the assembler into case sensitive mode */
-    AddTextLine (".case\t\ton");
+    AddTextLine ("\t.case\t\ton");
 
     /* Tell the assembler if we want to generate debug info */
-    AddTextLine (".debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
+    AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
 
     /* Import the stack pointer for direct auto variable access */
-    AddTextLine (".importzp\tsp, sreg, regsave, regbank, tmp1, ptr1");
+    AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
 
     /* Define long branch macros */
-    AddTextLine (".macpack\tlongbranch");
+    AddTextLine ("\t.macpack\tlongbranch");
+}
+
+
+
+void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
+/* If debug info is enabled, place a file info into the source */
+{
+    if (DebugInfo) {
+       /* We have to place this into the global text segment, so it will
+        * appear before all .dbg line statements.
+        */
+               TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
+    }
 }
 
 
@@ -203,8 +230,30 @@ void g_usebss (void)
 
 
 
+void g_segname (segment_t Seg, const char* Name)
+/* Set the name of a segment */
+{
+    DataSeg* S;
+
+    /* Remember the new name */
+    NewSegName (Seg, Name);
+
+    /* Emit a segment directive for the data style segments */
+    switch (Seg) {
+       case SEG_RODATA: S = CS->ROData; break;
+       case SEG_DATA:   S = CS->Data;   break;
+       case SEG_BSS:    S = CS->BSS;    break;
+       default:         S = 0;          break;
+    }
+    if (S) {
+               DS_AddLine (S, ".segment\t\"%s\"", Name);
+    }
+}
+
+
+
 /*****************************************************************************/
-/*                                  Code                                    */
+/*                                          Code                                    */
 /*****************************************************************************/
 
 
@@ -286,7 +335,7 @@ static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
 void g_defcodelabel (unsigned label)
 /* Define a local code label */
 {
-    AddCodeLabel (CS->Code, LocalLabelName (label));
+    CS_AddLabel (CS->Code, LocalLabelName (label));
 }
 
 
@@ -525,65 +574,74 @@ void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
 
 
 
-void g_getimmed (unsigned flags, unsigned long val, unsigned offs)
+void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
 /* Load a constant into the primary register */
 {
-    if ((flags & CF_CONST) != 0) {
+    unsigned char B1, B2, B3, B4;
+    unsigned      Done;
+
+
+    if ((Flags & CF_CONST) != 0) {
 
        /* Numeric constant */
-       switch (flags & CF_TYPE) {
+       switch (Flags & CF_TYPE) {
 
            case CF_CHAR:
-               if ((flags & CF_FORCECHAR) != 0) {
-                   ldaconst (val);
+               if ((Flags & CF_FORCECHAR) != 0) {
+                   ldaconst (Val);
                    break;
                }
                /* FALL THROUGH */
            case CF_INT:
-               ldxconst ((val >> 8) & 0xFF);
-               ldaconst (val & 0xFF);
+               ldxconst ((Val >> 8) & 0xFF);
+               ldaconst (Val & 0xFF);
                break;
 
            case CF_LONG:
-               if (val < 0x100) {
-                   AddCodeLine ("ldx #$00");
-                   AddCodeLine ("stx sreg+1");
+               /* Split the value into 4 bytes */
+               B1 = (unsigned char) (Val >>  0);
+               B2 = (unsigned char) (Val >>  8);
+               B3 = (unsigned char) (Val >> 16);
+               B4 = (unsigned char) (Val >> 24);
+
+               /* Remember which bytes are done */
+               Done = 0;
+
+               /* Load the value */
+               AddCodeLine ("ldx #$%02X", B2);
+               Done |= 0x02;
+               if (B2 == B3) {
                    AddCodeLine ("stx sreg");
-                   AddCodeLine ("lda #$%02X", (unsigned char) val);
-               } else if ((val & 0xFFFF00FF) == 0) {
-                   AddCodeLine ("lda #$00");
-                   AddCodeLine ("sta sreg+1");
-                   AddCodeLine ("sta sreg");
-                   AddCodeLine ("ldx #$%02X", (unsigned char) (val >> 8));
-               } else if ((val & 0xFFFF0000) == 0 && CodeSizeFactor > 140) {
-                   AddCodeLine ("lda #$00");
-                   AddCodeLine ("sta sreg+1");
-                   AddCodeLine ("sta sreg");
-                   AddCodeLine ("lda #$%02X", (unsigned char) val);
-                   AddCodeLine ("ldx #$%02X", (unsigned char) (val >> 8));
-               } else if ((val & 0xFFFFFF00) == 0xFFFFFF00) {
-                   AddCodeLine ("ldx #$FF");
+                   Done |= 0x04;
+               }
+               if (B2 == B4) {
                    AddCodeLine ("stx sreg+1");
-                   AddCodeLine ("stx sreg");
-                   if ((val & 0xFF) == 0xFF) {
-                       AddCodeLine ("txa");
-                   } else {
-                       AddCodeLine ("lda #$%02X", (unsigned char) val);
-                   }
-               } else if ((val & 0xFFFF00FF) == 0xFFFF00FF) {
-                   AddCodeLine ("lda #$FF");
-                   AddCodeLine ("sta sreg+1");
-                   AddCodeLine ("sta sreg");
-                   AddCodeLine ("ldx #$%02X", (unsigned char) (val >> 8));
-               } else {
-                   /* Call a subroutine that will load following value */
-                   AddCodeLine ("jsr ldeax");
-                   AddCodeLine (".dword $%08lX", val & 0xFFFFFFFF);
+                   Done |= 0x08;
                }
+               if ((Done & 0x04) == 0 && B1 != B3) {
+                   AddCodeLine ("lda #$%02X", B3);
+                   AddCodeLine ("sta sreg");
+                   Done |= 0x04;
+               }
+               if ((Done & 0x08) == 0 && B1 != B4) {
+                   AddCodeLine ("lda #$%02X", B4);
+                   AddCodeLine ("sta sreg+1");
+                   Done |= 0x08;
+               }
+               AddCodeLine ("lda #$%02X", B1);
+               Done |= 0x01;
+               if ((Done & 0x04) == 0) {
+                   CHECK (B1 == B3);
+                   AddCodeLine ("sta sreg");
+               }
+               if ((Done & 0x08) == 0) {
+                   CHECK (B1 == B4);
+                   AddCodeLine ("sta sreg+1");
+               }
                break;
 
            default:
-               typeerror (flags);
+               typeerror (Flags);
                break;
 
        }
@@ -591,7 +649,7 @@ void g_getimmed (unsigned flags, unsigned long val, unsigned offs)
     } else {
 
        /* Some sort of label */
-       const char* Label = GetLabelName (flags, val, offs);
+       const char* Label = GetLabelName (Flags, Val, Offs);
 
        /* Load the address into the primary */
        AddCodeLine ("lda #<(%s)", Label);
@@ -602,11 +660,11 @@ void g_getimmed (unsigned flags, unsigned long val, unsigned offs)
 
 
 
-void g_getstatic (unsigned flags, unsigned long label, unsigned offs)
+void g_getstatic (unsigned flags, unsigned long label, long offs)
 /* Fetch an static memory cell into the primary register */
 {
     /* Create the correct label name */
-    char* lbuf = GetLabelName (flags, label, offs);
+    const char* lbuf = GetLabelName (flags, label, offs);
 
     /* Check the size and generate the correct load operation */
     switch (flags & CF_TYPE) {
@@ -619,9 +677,10 @@ void g_getstatic (unsigned flags, unsigned long label, unsigned offs)
                AddCodeLine ("lda %s", lbuf);   /* load A from the label */
                if (!(flags & CF_UNSIGNED)) {
                    /* Must sign extend */
-                   AddCodeLine ("bpl *+3");
+                   unsigned L = GetLocalLabel ();
+                   AddCodeLine ("bpl %s", LocalLabelName (L));
                    AddCodeLine ("dex");
-                   AddCodeHint ("x:!");                /* X is invalid now */
+                   g_defcodelabel (L);
                }
            }
            break;
@@ -668,25 +727,17 @@ void g_getlocal (unsigned flags, int offs)
 
        case CF_CHAR:
            if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
-               if (CPU == CPU_65C02 && offs == 0) {
-                   AddCodeLine ("lda (sp)");
-               } else {
-                   ldyconst (offs);
-                   AddCodeLine ("lda (sp),y");
-               }
+               ldyconst (offs);
+               AddCodeLine ("lda (sp),y");
            } else {
-               if (offs == 0) {
-                   AddCodeLine ("ldx #$00");
-                   AddCodeLine ("lda (sp,x)");
-               } else {
-                   ldyconst (offs);
-                   AddCodeLine ("ldx #$00");
-                   AddCodeLine ("lda (sp),y");
-               }
+               ldyconst (offs);
+               AddCodeLine ("ldx #$00");
+               AddCodeLine ("lda (sp),y");
                if ((flags & CF_UNSIGNED) == 0) {
-                   AddCodeLine ("bpl *+3");
+                   unsigned L = GetLocalLabel();
+                   AddCodeLine ("bpl %s", LocalLabelName (L));
                    AddCodeLine ("dex");
-                   AddCodeHint ("x:!");        /* X is invalid now */
+                   g_defcodelabel (L);
                }
            }
            break;
@@ -699,30 +750,14 @@ void g_getlocal (unsigned flags, int offs)
                AddCodeLine ("dey");
                AddCodeLine ("ora (sp),y");
            } else {
-               if (CodeSizeFactor > 180) {
-                   ldyconst (offs + 1);
-                   AddCodeLine ("lda (sp),y");
-                   AddCodeLine ("tax");
-                   AddCodeLine ("dey");
-                   AddCodeLine ("lda (sp),y");
-               } else {
-                   if (offs) {
-                       ldyconst (offs+1);
-                               AddCodeLine ("jsr ldaxysp");
-                   } else {
-                       AddCodeLine ("jsr ldax0sp");
-                   }
-               }
+               ldyconst (offs+1);
+               AddCodeLine ("jsr ldaxysp");
            }
            break;
 
        case CF_LONG:
-           if (offs) {
-               ldyconst (offs+3);
-               AddCodeLine ("jsr ldeaxysp");
-           } else {
-               AddCodeLine ("jsr ldeax0sp");
-           }
+           ldyconst (offs+3);
+           AddCodeLine ("jsr ldeaxysp");
                    break;
 
                default:
@@ -748,54 +783,32 @@ void g_getind (unsigned flags, unsigned offs)
 
        case CF_CHAR:
                    /* Character sized */
-           if (offs) {
-               ldyconst (offs);
-               if (flags & CF_UNSIGNED) {
-                   AddCodeLine ("jsr ldauidx");
-                       } else {
-                   AddCodeLine ("jsr ldaidx");
-               }
-           } else {
-               if (flags & CF_UNSIGNED) {
-                   if (CodeSizeFactor > 250) {
-                       AddCodeLine ("sta ptr1");
-                       AddCodeLine ("stx ptr1+1");
-                       AddCodeLine ("ldx #$00");
-                       AddCodeLine ("lda (ptr1,x)");
-                   } else {
-                       AddCodeLine ("jsr ldaui");
-                   }
-               } else {
-                           AddCodeLine ("jsr ldai");
-               }
-                   }
+           if (flags & CF_UNSIGNED) {
+               ldyconst (offs);
+               AddCodeLine ("jsr ldauidx");
+           } else {
+               ldyconst (offs);
+               AddCodeLine ("jsr ldaidx");
+           }
            break;
 
        case CF_INT:
            if (flags & CF_TEST) {
-               ldyconst (offs);
+               ldyconst (offs);
                AddCodeLine ("sta ptr1");
                AddCodeLine ("stx ptr1+1");
                AddCodeLine ("lda (ptr1),y");
                AddCodeLine ("iny");
                AddCodeLine ("ora (ptr1),y");
            } else {
-               if (offs == 0) {
-                   AddCodeLine ("jsr ldaxi");
-               } else {
-                   ldyconst (offs+1);
-                   AddCodeLine ("jsr ldaxidx");
-               }
+               ldyconst (offs+1);
+               AddCodeLine ("jsr ldaxidx");
            }
            break;
 
                case CF_LONG:
-           if (offs == 0) {
-               AddCodeLine ("jsr ldeaxi");
-           } else {
-               ldyconst (offs+3);
-               AddCodeLine ("jsr ldeaxidx");
-           }
+           ldyconst (offs+3);
+           AddCodeLine ("jsr ldeaxidx");
            if (flags & CF_TEST) {
                        AddCodeLine ("jsr tsteax");
            }
@@ -824,22 +837,22 @@ void g_leasp (int offs)
                    ldaconst (offs);                    /* Load A with offset value */
                    AddCodeLine ("jsr leaasp"); /* Load effective address */
                } else {
+           unsigned L = GetLocalLabel ();
                    if (CPU == CPU_65C02 && offs == 1) {
                        AddCodeLine ("lda sp");
                        AddCodeLine ("ldx sp+1");
                        AddCodeLine ("ina");
-                       AddCodeLine ("bne *+3");
+                       AddCodeLine ("bne %s", LocalLabelName (L));
                        AddCodeLine ("inx");
-                       AddCodeHint ("x:!");            /* Invalidate X */
                    } else {
                        ldaconst (offs);
                        AddCodeLine ("clc");
                        AddCodeLine ("ldx sp+1");
                        AddCodeLine ("adc sp");
-                       AddCodeLine ("bcc *+3");
+                       AddCodeLine ("bcc %s", LocalLabelName (L));
                        AddCodeLine ("inx");
-                       AddCodeHint ("x:!");            /* Invalidate X */
                    }
+           g_defcodelabel (L);
                }
     }
 }
@@ -864,21 +877,18 @@ void g_leavariadic (int Offs)
     CheckLocalOffs (ArgSizeOffs);
 
     /* Get the size of all parameters. */
-    if (ArgSizeOffs == 0 && CPU == CPU_65C02) {
-               AddCodeLine ("lda (sp)");
-    } else {
-               ldyconst (ArgSizeOffs);
-               AddCodeLine ("lda (sp),y");
-    }
+    ldyconst (ArgSizeOffs);
+    AddCodeLine ("lda (sp),y");
 
     /* Add the value of the stackpointer */
     if (CodeSizeFactor > 250) {
+       unsigned L = GetLocalLabel();
                AddCodeLine ("ldx sp+1");
                AddCodeLine ("clc");
                AddCodeLine ("adc sp");
-               AddCodeLine ("bcc *+3");
+               AddCodeLine ("bcc %s", LocalLabelName (L));
                AddCodeLine ("inx");
-               AddCodeHint ("x:!");            /* Invalidate X */
+       g_defcodelabel (L);
     } else {
                AddCodeLine ("jsr leaasp");
     }
@@ -899,11 +909,11 @@ void g_leavariadic (int Offs)
 
 
 
-void g_putstatic (unsigned flags, unsigned long label, unsigned offs)
+void g_putstatic (unsigned flags, unsigned long label, long offs)
 /* Store the primary register into the specified static memory cell */
 {
     /* Create the correct label name */
-    char* lbuf = GetLabelName (flags, label, offs);
+    const char* lbuf = GetLabelName (flags, label, offs);
 
     /* Check the size and generate the correct store operation */
     switch (flags & CF_TYPE) {
@@ -945,12 +955,8 @@ void g_putlocal (unsigned Flags, int Offs, long Val)
            if (Flags & CF_CONST) {
                AddCodeLine ("lda #$%02X", (unsigned char) Val);
            }
-           if (CPU == CPU_65C02 && Offs == 0) {
-               AddCodeLine ("sta (sp)");
-           } else {
-               ldyconst (Offs);
-               AddCodeLine ("sta (sp),y");
-           }
+           ldyconst (Offs);
+           AddCodeLine ("sta (sp),y");
            break;
 
        case CF_INT:
@@ -962,41 +968,25 @@ void g_putlocal (unsigned Flags, int Offs, long Val)
                    /* Place high byte into X */
                    AddCodeLine ("tax");
                }
-               if (CPU == CPU_65C02 && Offs == 0) {
-                   AddCodeLine ("lda #$%02X", (unsigned char) Val);
-                   AddCodeLine ("sta (sp)");
+               if ((Val & 0xFF) == Offs+1) {
+                   /* The value we need is already in Y */
+                   AddCodeLine ("tya");
+                   AddCodeLine ("dey");
                } else {
-                   if ((Val & 0xFF) == Offs+1) {
-                       /* The value we need is already in Y */
-                       AddCodeLine ("tya");
-                       AddCodeLine ("dey");
-                   } else {
-                       AddCodeLine ("dey");
-                       AddCodeLine ("lda #$%02X", (unsigned char) Val);
-                   }
-                   AddCodeLine ("sta (sp),y");
+                   AddCodeLine ("dey");
+                   AddCodeLine ("lda #$%02X", (unsigned char) Val);
                }
+               AddCodeLine ("sta (sp),y");
            } else {
                if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
-                   if (Offs) {
-                       ldyconst (Offs);
-                       AddCodeLine ("jsr staxysp");
-                   } else {
-                       AddCodeLine ("jsr stax0sp");
-                   }
+                   ldyconst (Offs);
+                   AddCodeLine ("jsr staxysp");
                } else {
-                   if (CPU == CPU_65C02 && Offs == 0) {
-                       AddCodeLine ("sta (sp)");
-                       ldyconst (1);
-                       AddCodeLine ("txa");
-                       AddCodeLine ("sta (sp),y");
-                   } else {
-                       ldyconst (Offs);
-                       AddCodeLine ("sta (sp),y");
-                       AddCodeLine ("iny");
-                       AddCodeLine ("txa");
-                       AddCodeLine ("sta (sp),y");
-                   }
+                   ldyconst (Offs);
+                   AddCodeLine ("sta (sp),y");
+                   AddCodeLine ("iny");
+                   AddCodeLine ("txa");
+                   AddCodeLine ("sta (sp),y");
                }
            }
            break;
@@ -1005,12 +995,8 @@ void g_putlocal (unsigned Flags, int Offs, long Val)
            if (Flags & CF_CONST) {
                g_getimmed (Flags, Val, 0);
            }
-           if (Offs) {
-               ldyconst (Offs);
-               AddCodeLine ("jsr steaxysp");
-           } else {
-               AddCodeLine ("jsr steax0sp");
-           }
+           ldyconst (Offs);
+           AddCodeLine ("jsr steaxysp");
            break;
 
                default:
@@ -1068,30 +1054,18 @@ void g_putind (unsigned Flags, unsigned Offs)
     switch (Flags & CF_TYPE) {
 
        case CF_CHAR:
-           if (Offs) {
-               ldyconst (Offs);
-               AddCodeLine ("jsr staspidx");
-           } else {
-               AddCodeLine ("jsr staspp");
-           }
+           ldyconst (Offs);
+           AddCodeLine ("jsr staspidx");
            break;
 
        case CF_INT:
-           if (Offs) {
-               ldyconst (Offs);
-               AddCodeLine ("jsr staxspidx");
-           } else {
-               AddCodeLine ("jsr staxspp");
-           }
+           ldyconst (Offs);
+           AddCodeLine ("jsr staxspidx");
            break;
 
        case CF_LONG:
-           if (Offs) {
-               ldyconst (Offs);
-               AddCodeLine ("jsr steaxspidx");
-           } else {
-               AddCodeLine ("jsr steaxspp");
-           }
+           ldyconst (Offs);
+           AddCodeLine ("jsr steaxspidx");
            break;
 
        default:
@@ -1157,18 +1131,81 @@ void g_tosint (unsigned flags)
 
 
 
-void g_reglong (unsigned flags)
+void g_regint (unsigned Flags)
+/* Make sure, the value in the primary register an int. Convert if necessary */
+{
+    unsigned L;
+
+    switch (Flags & CF_TYPE) {
+
+       case CF_CHAR:
+            if (Flags & CF_FORCECHAR) {
+                /* Conversion is from char */
+                if (Flags & CF_UNSIGNED) {
+                    AddCodeLine ("ldx #$00");
+                } else {
+                    L = GetLocalLabel();
+                    AddCodeLine ("ldx #$00");
+                    AddCodeLine ("cmp #$80");
+                    AddCodeLine ("bcc %s", LocalLabelName (L));
+                    AddCodeLine ("dex");
+                    g_defcodelabel (L);
+                }
+            }
+            /* FALLTHROUGH */
+
+       case CF_INT:
+       case CF_LONG:
+           break;
+
+       default:
+           typeerror (Flags);
+    }
+}
+
+
+
+void g_reglong (unsigned Flags)
 /* Make sure, the value in the primary register a long. Convert if necessary */
 {
-    switch (flags & CF_TYPE) {
+    unsigned L;
+
+    switch (Flags & CF_TYPE) {
 
        case CF_CHAR:
+            if (Flags & CF_FORCECHAR) {
+                /* Conversion is from char */
+                if (Flags & CF_UNSIGNED) {
+                    if (CodeSizeFactor >= 200) {
+                        AddCodeLine ("ldx #$00");
+                        AddCodeLine ("stx sreg");
+                        AddCodeLine ("stx sreg+1");
+                    } else {
+                        AddCodeLine ("jsr aulong");
+                    }
+                } else {
+                    if (CodeSizeFactor >= 366) {
+                        L = GetLocalLabel();
+                        AddCodeLine ("ldx #$00");
+                        AddCodeLine ("cmp #$80");
+                        AddCodeLine ("bcc %s", LocalLabelName (L));
+                        AddCodeLine ("dex");
+                        g_defcodelabel (L);
+                        AddCodeLine ("stx sreg");
+                        AddCodeLine ("stx sreg+1");
+                    } else {
+                        AddCodeLine ("jsr along");
+                    }
+                }
+            }
+            /* FALLTHROUGH */
+
        case CF_INT:
-           if (flags & CF_UNSIGNED) {
+           if (Flags & CF_UNSIGNED) {
                if (CodeSizeFactor >= 200) {
                    ldyconst (0);
                    AddCodeLine ("sty sreg");
-                   AddCodeLine ("sty sreg+1");
+                   AddCodeLine ("sty sreg+1");
                } else {
                    AddCodeLine ("jsr axulong");
                }
@@ -1181,7 +1218,7 @@ void g_reglong (unsigned flags)
            break;
 
        default:
-           typeerror (flags);
+           typeerror (Flags);
     }
 }
 
@@ -1249,9 +1286,14 @@ unsigned g_typecast (unsigned lhs, unsigned rhs)
     rtype = rhs & CF_TYPE;
 
     /* Check if a conversion is needed */
-    if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
-       /* We must promote the primary register to long */
-       g_reglong (rhs);
+    if ((rhs & CF_CONST) == 0) {
+        if (ltype == CF_LONG && rtype != CF_LONG) {
+            /* We must promote the primary register to long */
+            g_reglong (rhs);
+        } else if (ltype == CF_INT && rtype != CF_INT) {
+            /* We must promote the primary register to int */
+            g_regint (rhs);
+        }
     }
 
     /* Do not need any other action. If the left type is int, and the primary
@@ -1286,9 +1328,9 @@ void g_scale (unsigned flags, long val)
     } else if (val > 0) {
 
        /* Scale up */
-       if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
+       if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
 
-           /* Factor is 2, 4 or 8, use special function */
+                   /* Factor is 2, 4, 8 and 16, use special function */
            switch (flags & CF_TYPE) {
 
                case CF_CHAR:
@@ -1301,11 +1343,11 @@ void g_scale (unsigned flags, long val)
                    /* FALLTHROUGH */
 
                case CF_INT:
-                   if (CodeSizeFactor >= (p2+1)*130U) {
+                   if (CodeSizeFactor >= (p2+1)*130U) {
                        AddCodeLine ("stx tmp1");
                        while (p2--) {
                            AddCodeLine ("asl a");
-                           AddCodeLine ("rol tmp1");
+                           AddCodeLine ("rol tmp1");
                        }
                        AddCodeLine ("ldx tmp1");
                    } else {
@@ -1341,9 +1383,9 @@ void g_scale (unsigned flags, long val)
 
        /* Scale down */
        val = -val;
-       if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
+       if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
 
-           /* Factor is 2, 4 or 8, use special function */
+           /* Factor is 2, 4, 8 and 16 use special function */
            switch (flags & CF_TYPE) {
 
                case CF_CHAR:
@@ -1421,6 +1463,8 @@ void g_scale (unsigned flags, long val)
 void g_addlocal (unsigned flags, int offs)
 /* Add a local variable to ax */
 {
+    unsigned L;
+
     /* Correct the offset and check it */
     offs -= oursp;
     CheckLocalOffs (offs);
@@ -1428,12 +1472,13 @@ void g_addlocal (unsigned flags, int offs)
     switch (flags & CF_TYPE) {
 
        case CF_CHAR:
+           L = GetLocalLabel();
            AddCodeLine ("ldy #$%02X", offs & 0xFF);
            AddCodeLine ("clc");
            AddCodeLine ("adc (sp),y");
-           AddCodeLine ("bcc *+3");
+           AddCodeLine ("bcc %s", LocalLabelName (L));
            AddCodeLine ("inx");
-           AddCodeHint ("x:!");
+           g_defcodelabel (L);
            break;
 
        case CF_INT:
@@ -1463,20 +1508,23 @@ void g_addlocal (unsigned flags, int offs)
 
 
 
-void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
+void g_addstatic (unsigned flags, unsigned long label, long offs)
 /* Add a static variable to ax */
 {
+    unsigned L;
+
     /* Create the correct label name */
-    char* lbuf = GetLabelName (flags, label, offs);
+    const char* lbuf = GetLabelName (flags, label, offs);
 
     switch (flags & CF_TYPE) {
 
        case CF_CHAR:
+           L = GetLocalLabel();
            AddCodeLine ("clc");
            AddCodeLine ("adc %s", lbuf);
-           AddCodeLine ("bcc *+3");
+           AddCodeLine ("bcc %s", LocalLabelName (L));
            AddCodeLine ("inx");
-           AddCodeHint ("x:!");
+           g_defcodelabel (L);
            break;
 
        case CF_INT:
@@ -1504,40 +1552,18 @@ void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
 
 
 
-/*****************************************************************************/
-/*            Compares of ax with a variable with fixed address             */
-/*****************************************************************************/
-
-
-
-void g_cmplocal (unsigned flags, int offs)
-/* Compare a local variable to ax */
-{
-    Internal ("g_cmplocal not implemented");
-}
-
-
-
-void g_cmpstatic (unsigned flags, unsigned label, unsigned offs)
-/* Compare a static variable to ax */
-{
-    Internal ("g_cmpstatic not implemented");
-}
-
-
-
 /*****************************************************************************/
 /*                          Special op= functions                           */
 /*****************************************************************************/
 
 
 
-void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
+void g_addeqstatic (unsigned flags, unsigned long label, long offs,
                            unsigned long val)
 /* Emit += for a static variable */
 {
     /* Create the correct label name */
-    char* lbuf = GetLabelName (flags, label, offs);
+    const char* lbuf = GetLabelName (flags, label, offs);
 
     /* Check the size and determine operation */
     switch (flags & CF_TYPE) {
@@ -1561,9 +1587,10 @@ void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
                    AddCodeLine ("sta %s", lbuf);
                        }
                if ((flags & CF_UNSIGNED) == 0) {
-                   AddCodeLine ("bpl *+3");
+                   unsigned L = GetLocalLabel();
+                   AddCodeLine ("bpl %s", LocalLabelName (L));
                    AddCodeLine ("dex");
-                   AddCodeHint ("x:!");                /* Invalidate X */
+                   g_defcodelabel (L);
                }
                        break;
                    }
@@ -1615,7 +1642,7 @@ void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
                if (val < 0x100) {
                    AddCodeLine ("ldy #<(%s)", lbuf);
                    AddCodeLine ("sty ptr1");
-                   AddCodeLine ("ldy #>(%s+1)", lbuf);
+                   AddCodeLine ("ldy #>(%s)", lbuf);
                    if (val == 1) {
                        AddCodeLine ("jsr laddeq1");
                    } else {
@@ -1630,7 +1657,7 @@ void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
            } else {
                AddCodeLine ("ldy #<(%s)", lbuf);
                AddCodeLine ("sty ptr1");
-               AddCodeLine ("ldy #>(%s+1)", lbuf);
+               AddCodeLine ("ldy #>(%s)", lbuf);
                AddCodeLine ("jsr laddeq");
            }
                    break;
@@ -1654,63 +1681,58 @@ void g_addeqlocal (unsigned flags, int offs, unsigned long val)
 
                case CF_CHAR:
                    if (flags & CF_FORCECHAR) {
-                       if (offs == 0) {
-                           AddCodeLine ("ldx #$00");
-                           if (flags & CF_CONST) {
-                               AddCodeLine ("clc");
-                               AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
-                               AddCodeLine ("adc (sp,x)");
-                               AddCodeLine ("sta (sp,x)");
-                           } else {
-                               AddCodeLine ("clc");
-                               AddCodeLine ("adc (sp,x)");
-                               AddCodeLine ("sta (sp,x)");
-                           }
-                       } else {
-                           ldyconst (offs);
-                   AddCodeLine ("ldx #$00");
-                   if (flags & CF_CONST) {
-                       AddCodeLine ("clc");
-                               AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
-                       AddCodeLine ("adc (sp),y");
-                       AddCodeLine ("sta (sp),y");
-                   } else {
-                       AddCodeLine ("clc");
-                       AddCodeLine ("adc (sp),y");
-                       AddCodeLine ("sta (sp),y");
-                   }
-               }
+               ldyconst (offs);
+               AddCodeLine ("ldx #$00");
+               if (flags & CF_CONST) {
+                   AddCodeLine ("clc");
+                   AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
+                   AddCodeLine ("adc (sp),y");
+                   AddCodeLine ("sta (sp),y");
+               } else {
+                   AddCodeLine ("clc");
+                   AddCodeLine ("adc (sp),y");
+                   AddCodeLine ("sta (sp),y");
+               }
                if ((flags & CF_UNSIGNED) == 0) {
-                   AddCodeLine ("bpl *+3");
+                   unsigned L = GetLocalLabel();
+                   AddCodeLine ("bpl %s", LocalLabelName (L));
                    AddCodeLine ("dex");
-                   AddCodeHint ("x:!");        /* Invalidate X */
+                   g_defcodelabel (L);
                }
                        break;
                    }
                    /* FALLTHROUGH */
 
                case CF_INT:
+           ldyconst (offs);
            if (flags & CF_CONST) {
-               g_getimmed (flags, val, 0);
-           }
-           if (offs == 0) {
-               AddCodeLine ("jsr addeq0sp");
+               if (CodeSizeFactor >= 400) {
+                   AddCodeLine ("clc");
+                   AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
+                   AddCodeLine ("adc (sp),y");
+                   AddCodeLine ("sta (sp),y");
+                   AddCodeLine ("iny");
+                   AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
+                   AddCodeLine ("adc (sp),y");
+                   AddCodeLine ("sta (sp),y");
+                   AddCodeLine ("tax");
+                   AddCodeLine ("dey");
+                   AddCodeLine ("lda (sp),y");
+               } else {
+                   g_getimmed (flags, val, 0);
+                   AddCodeLine ("jsr addeqysp");
+               }
            } else {
-               ldyconst (offs);
-               AddCodeLine ("jsr addeqysp");
-           }
+               AddCodeLine ("jsr addeqysp");
+           }
                    break;
 
                case CF_LONG:
            if (flags & CF_CONST) {
                g_getimmed (flags, val, 0);
            }
-           if (offs == 0) {
-               AddCodeLine ("jsr laddeq0sp");
-           } else {
-               ldyconst (offs);
-               AddCodeLine ("jsr laddeqysp");
-           }
+           ldyconst (offs);
+           AddCodeLine ("jsr laddeqysp");
                    break;
 
                default:
@@ -1735,20 +1757,12 @@ void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
                case CF_CHAR:
            AddCodeLine ("sta ptr1");
            AddCodeLine ("stx ptr1+1");
-           if (offs == 0) {
-               AddCodeLine ("ldx #$00");
-               AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
-               AddCodeLine ("clc");
-               AddCodeLine ("adc (ptr1,x)");
-               AddCodeLine ("sta (ptr1,x)");
-           } else {
-               AddCodeLine ("ldy #$%02X", offs);
-                       AddCodeLine ("ldx #$00");
-                       AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
-                       AddCodeLine ("clc");
-                       AddCodeLine ("adc (ptr1),y");
-                       AddCodeLine ("sta (ptr1),y");
-           }
+           AddCodeLine ("ldy #$%02X", offs);
+           AddCodeLine ("ldx #$00");
+           AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
+           AddCodeLine ("clc");
+           AddCodeLine ("adc (ptr1),y");
+           AddCodeLine ("sta (ptr1),y");
            break;
 
                case CF_INT:
@@ -1774,7 +1788,7 @@ void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
 
                case CF_LONG:
                    AddCodeLine ("jsr pushax");         /* Push the address */
-           push (flags);                       /* Correct the internal sp */
+                   push (CF_PTR);                      /* Correct the internal sp */
            g_getind (flags, offs);             /* Fetch the value */
            g_inc (flags, val);                 /* Increment value in primary */
            g_putind (flags, offs);             /* Store the value back */
@@ -1787,12 +1801,12 @@ void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
 
 
 
-void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
+void g_subeqstatic (unsigned flags, unsigned long label, long offs,
                            unsigned long val)
 /* Emit -= for a static variable */
 {
     /* Create the correct label name */
-    char* lbuf = GetLabelName (flags, label, offs);
+    const char* lbuf = GetLabelName (flags, label, offs);
 
     /* Check the size and determine operation */
     switch (flags & CF_TYPE) {
@@ -1811,16 +1825,16 @@ void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
                                AddCodeLine ("sta %s", lbuf);
                            }
                        } else {
+                   AddCodeLine ("eor #$FF");
                            AddCodeLine ("sec");
-                           AddCodeLine ("sta tmp1");
-                           AddCodeLine ("lda %s", lbuf);
-                           AddCodeLine ("sbc tmp1");
+                           AddCodeLine ("adc %s", lbuf);
                            AddCodeLine ("sta %s", lbuf);
                        }
                        if ((flags & CF_UNSIGNED) == 0) {
-                           AddCodeLine ("bpl *+3");
+                   unsigned L = GetLocalLabel();
+                           AddCodeLine ("bpl %s", LocalLabelName (L));
                            AddCodeLine ("dex");
-                           AddCodeHint ("x:!");                /* Invalidate X */
+                   g_defcodelabel (L);
                        }
                        break;
                    }
@@ -1846,13 +1860,12 @@ void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
                    AddCodeLine ("lda %s", lbuf);
                }
            } else {
-               AddCodeLine ("sta tmp1");
-               AddCodeLine ("lda %s", lbuf);
-               AddCodeLine ("sbc tmp1");
+               AddCodeLine ("eor #$FF");
+                       AddCodeLine ("adc %s", lbuf);
                AddCodeLine ("sta %s", lbuf);
-                       AddCodeLine ("stx tmp1");
-               AddCodeLine ("lda %s+1", lbuf);
-               AddCodeLine ("sbc tmp1");
+               AddCodeLine ("txa");
+               AddCodeLine ("eor #$FF");
+                       AddCodeLine ("adc %s+1", lbuf);
                AddCodeLine ("sta %s+1", lbuf);
                AddCodeLine ("tax");
                AddCodeLine ("lda %s", lbuf);
@@ -1864,13 +1877,9 @@ void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
                if (val < 0x100) {
                    AddCodeLine ("ldy #<(%s)", lbuf);
                    AddCodeLine ("sty ptr1");
-                   AddCodeLine ("ldy #>(%s+1)", lbuf);
-                   if (val == 1) {
-                       AddCodeLine ("jsr lsubeq1");
-                   } else {
-                       AddCodeLine ("lda #$%02X", (unsigned char)val);
-                       AddCodeLine ("jsr lsubeqa");
-                   }
+                   AddCodeLine ("ldy #>(%s)", lbuf);
+                   AddCodeLine ("lda #$%02X", (unsigned char)val);
+                   AddCodeLine ("jsr lsubeqa");
                } else {
                    g_getstatic (flags, label, offs);
                    g_dec (flags, val);
@@ -1879,7 +1888,7 @@ void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
            } else {
                AddCodeLine ("ldy #<(%s)", lbuf);
                AddCodeLine ("sty ptr1");
-               AddCodeLine ("ldy #>(%s+1)", lbuf);
+               AddCodeLine ("ldy #>(%s)", lbuf);
                AddCodeLine ("jsr lsubeq");
                    }
                    break;
@@ -1910,17 +1919,17 @@ void g_subeqlocal (unsigned flags, int offs, unsigned long val)
                    AddCodeLine ("lda (sp),y");
                    AddCodeLine ("sbc #$%02X", (unsigned char)val);
                } else {
-                   AddCodeLine ("sta tmp1");
-                   AddCodeLine ("lda (sp),y");
-                   AddCodeLine ("sbc tmp1");
+                   AddCodeLine ("eor #$FF");
+                   AddCodeLine ("adc (sp),y");
                }
                        AddCodeLine ("sta (sp),y");
                if ((flags & CF_UNSIGNED) == 0) {
-                   AddCodeLine ("bpl *+3");
-                   AddCodeLine ("dex");
-                   AddCodeHint ("x:!");                /* Invalidate X */
-               }
-                       break;
+                   unsigned L = GetLocalLabel();
+                   AddCodeLine ("bpl %s", LocalLabelName (L));
+                   AddCodeLine ("dex");
+                   g_defcodelabel (L);
+               }
+                       break;
                    }
                    /* FALLTHROUGH */
 
@@ -1928,24 +1937,16 @@ void g_subeqlocal (unsigned flags, int offs, unsigned long val)
            if (flags & CF_CONST) {
                g_getimmed (flags, val, 0);
            }
-           if (offs == 0) {
-               AddCodeLine ("jsr subeq0sp");
-           } else {
-               ldyconst (offs);
-               AddCodeLine ("jsr subeqysp");
-           }
+           ldyconst (offs);
+           AddCodeLine ("jsr subeqysp");
                    break;
 
                case CF_LONG:
            if (flags & CF_CONST) {
                g_getimmed (flags, val, 0);
            }
-           if (offs == 0) {
-               AddCodeLine ("jsr lsubeq0sp");
-           } else {
-               ldyconst (offs);
-               AddCodeLine ("jsr lsubeqysp");
-           }
+           ldyconst (offs);
+           AddCodeLine ("jsr lsubeqysp");
                    break;
 
                default:
@@ -1970,20 +1971,12 @@ void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
                case CF_CHAR:
            AddCodeLine ("sta ptr1");
            AddCodeLine ("stx ptr1+1");
-           if (offs == 0) {
-               AddCodeLine ("ldx #$00");
-               AddCodeLine ("lda (ptr1,x)");
-                       AddCodeLine ("sec");
-               AddCodeLine ("sbc #$%02X", (unsigned char)val);
-               AddCodeLine ("sta (ptr1,x)");
-           } else {
-                       AddCodeLine ("ldy #$%02X", offs);
-               AddCodeLine ("ldx #$00");
-               AddCodeLine ("lda (ptr1),y");
-               AddCodeLine ("sec");
-               AddCodeLine ("sbc #$%02X", (unsigned char)val);
-               AddCodeLine ("sta (ptr1),y");
-           }
+           AddCodeLine ("ldy #$%02X", offs);
+           AddCodeLine ("ldx #$00");
+           AddCodeLine ("lda (ptr1),y");
+           AddCodeLine ("sec");
+           AddCodeLine ("sbc #$%02X", (unsigned char)val);
+           AddCodeLine ("sta (ptr1),y");
            break;
 
                case CF_INT:
@@ -2009,7 +2002,7 @@ void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
 
                case CF_LONG:
                    AddCodeLine ("jsr pushax");         /* Push the address */
-           push (flags);                       /* Correct the internal sp */
+           push (CF_PTR);                      /* Correct the internal sp */
            g_getind (flags, offs);             /* Fetch the value */
            g_dec (flags, val);                 /* Increment value in primary */
            g_putind (flags, offs);             /* Store the value back */
@@ -2028,23 +2021,30 @@ void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
 
 
 
-void g_addaddr_local (unsigned flags, int offs)
+void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
 /* Add the address of a local variable to ax */
 {
+    unsigned L = 0;
+
     /* Add the offset */
     offs -= oursp;
     if (offs != 0) {
        /* We cannot address more then 256 bytes of locals anyway */
+       L = GetLocalLabel();
        CheckLocalOffs (offs);
        AddCodeLine ("clc");
        AddCodeLine ("adc #$%02X", offs & 0xFF);
-               AddCodeLine ("bcc *+4");        /* Do also skip the CLC insn below */
+       /* Do also skip the CLC insn below */
+               AddCodeLine ("bcc %s", LocalLabelName (L));
        AddCodeLine ("inx");
-       AddCodeHint ("x:!");                    /* Invalidate X */
     }
 
     /* Add the current stackpointer value */
     AddCodeLine ("clc");
+    if (L != 0) {
+       /* Label was used above */
+       g_defcodelabel (L);
+    }
     AddCodeLine ("adc sp");
     AddCodeLine ("tay");
     AddCodeLine ("txa");
@@ -2055,11 +2055,11 @@ void g_addaddr_local (unsigned flags, int offs)
 
 
 
-void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
+void g_addaddr_static (unsigned flags, unsigned long label, long offs)
 /* Add the address of a static variable to ax */
 {
     /* Create the correct label name */
-    char* lbuf = GetLabelName (flags, label, offs);
+    const char* lbuf = GetLabelName (flags, label, offs);
 
     /* Add the address to the current ax value */
     AddCodeLine ("clc");
@@ -2142,20 +2142,24 @@ void g_cmp (unsigned flags, unsigned long val)
  * will be set.
  */
 {
+    unsigned L;
+
     /* Check the size and determine operation */
     switch (flags & CF_TYPE) {
 
        case CF_CHAR:
            if (flags & CF_FORCECHAR) {
                AddCodeLine ("cmp #$%02X", (unsigned char)val);
-               break;
+               break;
            }
            /* FALLTHROUGH */
 
        case CF_INT:
+           L = GetLocalLabel();
            AddCodeLine ("cmp #$%02X", (unsigned char)val);
-                   AddCodeLine ("bne *+4");
+                   AddCodeLine ("bne %s", LocalLabelName (L));
            AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
+           g_defcodelabel (L);
            break;
 
         case CF_LONG:
@@ -2261,36 +2265,20 @@ void g_test (unsigned flags)
 void g_push (unsigned flags, unsigned long val)
 /* Push the primary register or a constant value onto the stack */
 {
-    unsigned char hi;
-
     if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
 
        /* We have a constant 8 or 16 bit value */
        if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
 
            /* Handle as 8 bit value */
-           if (CodeSizeFactor >= 165 || val > 2) {
-               ldaconst (val);
-               AddCodeLine ("jsr pusha");
-           } else {
-               AddCodeLine ("jsr pushc%d", (int) val);
-           }
+           ldaconst (val);
+           AddCodeLine ("jsr pusha");
 
        } else {
 
            /* Handle as 16 bit value */
-           hi = (unsigned char) (val >> 8);
-           if (val <= 7) {
-               AddCodeLine ("jsr push%u", (unsigned) val);
-           } else if (hi == 0 || hi == 0xFF) {
-               /* Use special function */
-               ldaconst (val);
-                       AddCodeLine ("jsr %s", (hi == 0)? "pusha0" : "pushaFF");
-           } else {
-               /* Long way ... */
-               g_getimmed (flags, val, 0);
-               AddCodeLine ("jsr pushax");
-           }
+           g_getimmed (flags, val, 0);
+           AddCodeLine ("jsr pushax");
        }
 
     } else {
@@ -2369,15 +2357,33 @@ void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
 
 
 
-void g_callind (unsigned Flags, unsigned ArgSize)
-/* Call subroutine with address in AX */
+void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
+/* Call subroutine indirect */
 {
-    if ((Flags & CF_FIXARGC) == 0) {
-       /* Pass arg count */
-       ldyconst (ArgSize);
+    if ((Flags & CF_LOCAL) == 0) {
+       /* Address is in a/x */
+       if ((Flags & CF_FIXARGC) == 0) {
+           /* Pass arg count */
+           ldyconst (ArgSize);
+       }
+       AddCodeLine ("jsr callax");
+    } else {
+       /* The address is on stack, offset is on Val */
+       Offs -= oursp;
+       CheckLocalOffs (Offs);
+       AddCodeLine ("pha");
+       AddCodeLine ("ldy #$%02X", Offs);
+       AddCodeLine ("lda (sp),y");
+       AddCodeLine ("sta jmpvec+1");
+       AddCodeLine ("iny");
+       AddCodeLine ("lda (sp),y");
+       AddCodeLine ("sta jmpvec+2");
+       AddCodeLine ("pla");
+       AddCodeLine ("jsr jmpvec");
     }
-    AddCodeLine ("jsr callax");        /* do the call */
-    oursp += ArgSize;                  /* callee pops args */
+
+    /* Callee pops args */
+    oursp += ArgSize;
 }
 
 
@@ -2390,54 +2396,7 @@ void g_jump (unsigned Label)
 
 
 
-void g_switch (unsigned Flags)
-/* Output switch statement preamble */
-{
-    switch (Flags & CF_TYPE) {
-
-       case CF_CHAR:
-       case CF_INT:
-           AddCodeLine ("jsr switch");
-           break;
-
-       case CF_LONG:
-           AddCodeLine ("jsr lswitch");
-           break;
-
-       default:
-           typeerror (Flags);
-
-    }
-}
-
-
-
-void g_case (unsigned flags, unsigned label, unsigned long val)
-/* Create table code for one case selector */
-{
-    switch (flags & CF_TYPE) {
-
-       case CF_CHAR:
-       case CF_INT:
-           AddCodeLine (".word $%04X, %s",
-                        (unsigned)(val & 0xFFFF),
-                        LocalLabelName (label));
-                   break;
-
-       case CF_LONG:
-           AddCodeLine (".dword $%08lX", val);
-           AddCodeLine (".word %s", LocalLabelName (label));
-           break;
-
-       default:
-           typeerror (flags);
-
-    }
-}
-
-
-
-void g_truejump (unsigned flags, unsigned label)
+void g_truejump (unsigned flags attribute ((unused)), unsigned label)
 /* Jump to label if zero flag clear */
 {
     AddCodeLine ("jne %s", LocalLabelName (label));
@@ -2445,7 +2404,7 @@ void g_truejump (unsigned flags, unsigned label)
 
 
 
-void g_falsejump (unsigned flags, unsigned label)
+void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
 /* Jump to label if zero flag set */
 {
     AddCodeLine ("jeq %s", LocalLabelName (label));
@@ -2562,7 +2521,7 @@ void g_mul (unsigned flags, unsigned long val)
     if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
        /* Generate a shift instead */
        g_asl (flags, p2);
-       return;
+       return;
     }
 
     /* If the right hand side is const, the lhs is not on stack but still
@@ -2574,50 +2533,78 @@ void g_mul (unsigned flags, unsigned long val)
 
            case CF_CHAR:
                if (flags & CF_FORCECHAR) {
-                   /* Handle some special cases */
-                   switch (val) {
-
-                       case 3:
-                           AddCodeLine ("sta tmp1");
-                           AddCodeLine ("asl a");
-                           AddCodeLine ("clc");
-                           AddCodeLine ("adc tmp1");
-                           return;
-
-                       case 5:
-                           AddCodeLine ("sta tmp1");
-                           AddCodeLine ("asl a");
-                           AddCodeLine ("asl a");
-                           AddCodeLine ("clc");
+                   /* Handle some special cases */
+                   switch (val) {
+
+                       case 3:
+                           AddCodeLine ("sta tmp1");
+                           AddCodeLine ("asl a");
+                           AddCodeLine ("clc");
                            AddCodeLine ("adc tmp1");
-                           return;
-
-                       case 10:
-                           AddCodeLine ("sta tmp1");
-                           AddCodeLine ("asl a");
-                           AddCodeLine ("asl a");
-                           AddCodeLine ("clc");
-                           AddCodeLine ("adc tmp1");
-                           AddCodeLine ("asl a");
-                           return;
-                   }
-               }
-               /* FALLTHROUGH */
+                           return;
 
-           case CF_INT:
-               break;
+                       case 5:
+                           AddCodeLine ("sta tmp1");
+                           AddCodeLine ("asl a");
+                           AddCodeLine ("asl a");
+                           AddCodeLine ("clc");
+                           AddCodeLine ("adc tmp1");
+                           return;
 
-           case CF_LONG:
-               break;
+                       case 6:
+                           AddCodeLine ("sta tmp1");
+                           AddCodeLine ("asl a");
+                           AddCodeLine ("clc");
+                           AddCodeLine ("adc tmp1");
+                           AddCodeLine ("asl a");
+                           return;
 
-           default:
-               typeerror (flags);
-       }
+                       case 10:
+                           AddCodeLine ("sta tmp1");
+                           AddCodeLine ("asl a");
+                           AddCodeLine ("asl a");
+                           AddCodeLine ("clc");
+                           AddCodeLine ("adc tmp1");
+                           AddCodeLine ("asl a");
+                           return;
+                   }
+               }
+               /* FALLTHROUGH */
 
-       /* If we go here, we didn't emit code. Push the lhs on stack and fall
-        * into the normal, non-optimized stuff.
-        */
-       flags &= ~CF_FORCECHAR; /* Handle chars as ints */
+           case CF_INT:
+               switch (val) {
+                   case 3:
+                       AddCodeLine ("jsr mulax3");
+                       return;
+                   case 5:
+                       AddCodeLine ("jsr mulax5");
+                       return;
+                   case 6:
+                       AddCodeLine ("jsr mulax6");
+                       return;
+                   case 7:
+                       AddCodeLine ("jsr mulax7");
+                       return;
+                   case 9:
+                       AddCodeLine ("jsr mulax9");
+                       return;
+                   case 10:
+                       AddCodeLine ("jsr mulax10");
+                       return;
+               }
+               break;
+
+           case CF_LONG:
+               break;
+
+           default:
+               typeerror (flags);
+       }
+
+       /* If we go here, we didn't emit code. Push the lhs on stack and fall
+        * into the normal, non-optimized stuff.
+        */
+       flags &= ~CF_FORCECHAR; /* Handle chars as ints */
        g_push (flags & ~CF_CONST, 0);
 
     }
@@ -2849,14 +2836,14 @@ void g_and (unsigned flags, unsigned long val)
                        AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
                        AddCodeLine ("tax");
                        ldaconst (0);
-                   } else {
+                   } else {
                        AddCodeLine ("tay");
                        AddCodeLine ("txa");
                        AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
                        AddCodeLine ("tax");
                        AddCodeLine ("tya");
                        if ((val & 0x00FF) != 0x00FF) {
-                           AddCodeLine ("and #$%02X", (unsigned char)val);
+                           AddCodeLine ("and #$%02X", (unsigned char)val);
                        }
                    }
                }
@@ -2900,10 +2887,10 @@ void g_asr (unsigned flags, unsigned long val)
 /* Primary = TOS >> Primary */
 {
     static char* ops [12] = {
-       0,              "tosasra0",     "tosasrax",
-       0,              "tosshra0",     "tosshrax",
-       0,              0,              "tosasreax",
-       0,              0,              "tosshreax",
+       0,              "tosasra0",     "tosasrax",
+       0,              "tosshra0",     "tosshrax",
+       0,              0,              "tosasreax",
+       0,              0,              "tosshreax",
     };
 
     /* If the right hand side is const, the lhs is not on stack but still
@@ -2915,59 +2902,67 @@ void g_asr (unsigned flags, unsigned long val)
 
            case CF_CHAR:
            case CF_INT:
-               if (val >= 1 && val <= 3) {
-                   if (flags & CF_UNSIGNED) {
-                       AddCodeLine ("jsr shrax%ld", val);
-                   } else {
-                       AddCodeLine ("jsr asrax%ld", val);
-                   }
-                   return;
-               } else if (val == 8 && (flags & CF_UNSIGNED)) {
+                       if (val >= 8 && (flags & CF_UNSIGNED)) {
                    AddCodeLine ("txa");
                    ldxconst (0);
+                   val -= 8;
+               }
+               if (val == 0) {
+                   /* Done */
                    return;
-               }
-               break;
+               } else if (val >= 1 && val <= 4) {
+                   if (flags & CF_UNSIGNED) {
+                       AddCodeLine ("jsr shrax%ld", val);
+                   } else {
+                       AddCodeLine ("jsr asrax%ld", val);
+                   }
+                   return;
+                       }
+               break;
 
-           case CF_LONG:
-               if (val >= 1 && val <= 3) {
-                   if (flags & CF_UNSIGNED) {
-                       AddCodeLine ("jsr shreax%ld", val);
-                   } else {
-                       AddCodeLine ("jsr asreax%ld", val);
-                   }
+           case CF_LONG:
+               if (val == 0) {
+                   /* Nothing to do */
                    return;
-               } else if (val == 8 && (flags & CF_UNSIGNED)) {
-                   AddCodeLine ("txa");
-                   AddCodeLine ("ldx sreg");
-                   AddCodeLine ("ldy sreg+1");
-                   AddCodeLine ("sty sreg");
-                   AddCodeLine ("ldy #$00");
+               } else if (val >= 1 && val <= 4) {
+                   if (flags & CF_UNSIGNED) {
+                       AddCodeLine ("jsr shreax%ld", val);
+                   } else {
+                       AddCodeLine ("jsr asreax%ld", val);
+                   }
+                   return;
+               } else if (val == 8 && (flags & CF_UNSIGNED)) {
+                   AddCodeLine ("txa");
+                   AddCodeLine ("ldx sreg");
+                   AddCodeLine ("ldy sreg+1");
+                   AddCodeLine ("sty sreg");
+                   AddCodeLine ("ldy #$00");
                    AddCodeLine ("sty sreg+1");
-                   return;
+                   return;
                } else if (val == 16) {
-                   AddCodeLine ("ldy #$00");
-                   AddCodeLine ("ldx sreg+1");
-                   if ((flags & CF_UNSIGNED) == 0) {
-                       AddCodeLine ("bpl *+3");
-                       AddCodeLine ("dey");
-                       AddCodeHint ("y:!");
-                   }
+                   AddCodeLine ("ldy #$00");
+                   AddCodeLine ("ldx sreg+1");
+                   if ((flags & CF_UNSIGNED) == 0) {
+                       unsigned L = GetLocalLabel();
+                       AddCodeLine ("bpl %s", LocalLabelName (L));
+                       AddCodeLine ("dey");
+                       g_defcodelabel (L);
+                   }
                    AddCodeLine ("lda sreg");
-                   AddCodeLine ("sty sreg+1");
-                   AddCodeLine ("sty sreg");
-                   return;
-               }
-               break;
+                   AddCodeLine ("sty sreg+1");
+                   AddCodeLine ("sty sreg");
+                   return;
+               }
+               break;
 
-           default:
-               typeerror (flags);
-       }
+           default:
+               typeerror (flags);
+       }
 
-       /* If we go here, we didn't emit code. Push the lhs on stack and fall
+       /* If we go here, we didn't emit code. Push the lhs on stack and fall
         * into the normal, non-optimized stuff.
-        */
-       g_push (flags & ~CF_CONST, 0);
+        */
+       g_push (flags & ~CF_CONST, 0);
 
     }
 
@@ -2981,10 +2976,10 @@ void g_asl (unsigned flags, unsigned long val)
 /* Primary = TOS << Primary */
 {
     static char* ops [12] = {
-       0,              "tosasla0",     "tosaslax",
-       0,              "tosshla0",     "tosshlax",
-       0,              0,              "tosasleax",
-       0,              0,              "tosshleax",
+       0,              "tosasla0",     "tosaslax",
+       0,              "tosshla0",     "tosshlax",
+       0,              0,              "tosasleax",
+       0,              0,              "tosshleax",
     };
 
 
@@ -2997,22 +2992,29 @@ void g_asl (unsigned flags, unsigned long val)
 
            case CF_CHAR:
            case CF_INT:
-               if (val >= 1 && val <= 3) {
+               if (val >= 8) {
+                   AddCodeLine ("tax");
+                   AddCodeLine ("lda #$00");
+                   val -= 8;
+               }
+               if (val == 0) {
+                   /* Done */
+                   return;
+               } else if (val >= 1 && val <= 4) {
                    if (flags & CF_UNSIGNED) {
                        AddCodeLine ("jsr shlax%ld", val);
                    } else {
-                       AddCodeLine ("jsr aslax%ld", val);
+                       AddCodeLine ("jsr aslax%ld", val);
                    }
                    return;
-               } else if (val == 8) {
-                   AddCodeLine ("tax");
-                   AddCodeLine ("lda #$00");
-                   return;
-               }
+               }
                break;
 
            case CF_LONG:
-               if (val >= 1 && val <= 3) {
+               if (val == 0) {
+                   /* Nothing to do */
+                   return;
+               } else if (val >= 1 && val <= 4) {
                    if (flags & CF_UNSIGNED) {
                        AddCodeLine ("jsr shleax%ld", val);
                    } else {
@@ -3020,7 +3022,7 @@ void g_asl (unsigned flags, unsigned long val)
                    }
                    return;
                } else if (val == 8) {
-                   AddCodeLine ("ldy sreg");
+                   AddCodeLine ("ldy sreg");
                    AddCodeLine ("sty sreg+1");
                    AddCodeLine ("stx sreg");
                    AddCodeLine ("tax");
@@ -3063,7 +3065,7 @@ void g_neg (unsigned flags)
            break;
 
        case CF_LONG:
-           AddCodeLine ("jsr negeax");
+           AddCodeLine ("jsr negeax");
            break;
 
        default:
@@ -3134,7 +3136,7 @@ void g_inc (unsigned flags, unsigned long val)
            if (flags & CF_FORCECHAR) {
                if (CPU == CPU_65C02 && val <= 2) {
                    while (val--) {
-                       AddCodeLine ("ina");
+                       AddCodeLine ("ina");
                    }
                } else {
                    AddCodeLine ("clc");
@@ -3146,11 +3148,11 @@ void g_inc (unsigned flags, unsigned long val)
 
        case CF_INT:
            if (CPU == CPU_65C02 && val == 1) {
+               unsigned L = GetLocalLabel();
                AddCodeLine ("ina");
-               AddCodeLine ("bne *+3");
+               AddCodeLine ("bne %s", LocalLabelName (L));
                AddCodeLine ("inx");
-               /* Tell the optimizer that the X register may be invalid */
-               AddCodeHint ("x:!");
+               g_defcodelabel (L);
            } else if (CodeSizeFactor < 200) {
                /* Use jsr calls */
                if (val <= 8) {
@@ -3163,14 +3165,14 @@ void g_inc (unsigned flags, unsigned long val)
                }
            } else {
                /* Inline the code */
-               if (val < 0x300) {
+               if (val <= 0x300) {
                    if ((val & 0xFF) != 0) {
+                       unsigned L = GetLocalLabel();
                        AddCodeLine ("clc");
                        AddCodeLine ("adc #$%02X", (unsigned char) val);
-                       AddCodeLine ("bcc *+3");
+                       AddCodeLine ("bcc %s", LocalLabelName (L));
                        AddCodeLine ("inx");
-                       /* Tell the optimizer that the X register may be invalid */
-                               AddCodeHint ("x:!");
+                       g_defcodelabel (L);
                    }
                    if (val >= 0x100) {
                        AddCodeLine ("inx");
@@ -3178,12 +3180,13 @@ void g_inc (unsigned flags, unsigned long val)
                    if (val >= 0x200) {
                        AddCodeLine ("inx");
                    }
+                   if (val >= 0x300) {
+                       AddCodeLine ("inx");
+                   }
                } else {
                    AddCodeLine ("clc");
                    if ((val & 0xFF) != 0) {
                        AddCodeLine ("adc #$%02X", (unsigned char) val);
-                       /* Tell the optimizer that the X register may be invalid */
-                       AddCodeHint ("x:!");
                    }
                    AddCodeLine ("pha");
                    AddCodeLine ("txa");
@@ -3252,12 +3255,12 @@ void g_dec (unsigned flags, unsigned long val)
                /* Inline the code */
                if (val < 0x300) {
                    if ((val & 0xFF) != 0) {
+                       unsigned L = GetLocalLabel();
                        AddCodeLine ("sec");
                        AddCodeLine ("sbc #$%02X", (unsigned char) val);
-                       AddCodeLine ("bcs *+3");
+                       AddCodeLine ("bcs %s", LocalLabelName (L));
                        AddCodeLine ("dex");
-                       /* Tell the optimizer that the X register may be invalid */
-                               AddCodeHint ("x:!");
+                       g_defcodelabel (L);
                    }
                    if (val >= 0x100) {
                        AddCodeLine ("dex");
@@ -3269,8 +3272,6 @@ void g_dec (unsigned flags, unsigned long val)
                    AddCodeLine ("sec");
                    if ((val & 0xFF) != 0) {
                        AddCodeLine ("sbc #$%02X", (unsigned char) val);
-                       /* Tell the optimizer that the X register may be invalid */
-                       AddCodeHint ("x:!");
                    }
                    AddCodeLine ("pha");
                    AddCodeLine ("txa");
@@ -3311,11 +3312,13 @@ void g_eq (unsigned flags, unsigned long val)
 {
     static char* ops [12] = {
        "toseq00",      "toseqa0",      "toseqax",
-       "toseq00",      "toseqa0",      "toseqax",
-       0,              0,              "toseqeax",
-       0,              0,              "toseqeax",
+       "toseq00",      "toseqa0",      "toseqax",
+       0,              0,              "toseqeax",
+       0,              0,              "toseqeax",
     };
 
+    unsigned L;
+
     /* If the right hand side is const, the lhs is not on stack but still
      * in the primary register.
      */
@@ -3332,9 +3335,11 @@ void g_eq (unsigned flags, unsigned long val)
                /* FALLTHROUGH */
 
            case CF_INT:
+               L = GetLocalLabel();
                AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
-                       AddCodeLine ("bne *+4");
+                       AddCodeLine ("bne %s", LocalLabelName (L));
                AddCodeLine ("cmp #$%02X", (unsigned char)val);
+               g_defcodelabel (L);
                AddCodeLine ("jsr booleq");
                return;
 
@@ -3368,6 +3373,7 @@ void g_ne (unsigned flags, unsigned long val)
        0,              0,              "tosneeax",
     };
 
+    unsigned L;
 
     /* If the right hand side is const, the lhs is not on stack but still
      * in the primary register.
@@ -3385,9 +3391,11 @@ void g_ne (unsigned flags, unsigned long val)
                /* FALLTHROUGH */
 
            case CF_INT:
+               L = GetLocalLabel();
                AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
-               AddCodeLine ("bne *+4");
+               AddCodeLine ("bne %s", LocalLabelName (L));
                AddCodeLine ("cmp #$%02X", (unsigned char)val);
+               g_defcodelabel (L);
                AddCodeLine ("jsr boolne");
                return;
 
@@ -3415,10 +3423,10 @@ void g_lt (unsigned flags, unsigned long val)
 /* Test for less than */
 {
     static char* ops [12] = {
-       "toslt00",      "toslta0",      "tosltax",
-       "tosult00",     "tosulta0",     "tosultax",
-       0,              0,              "toslteax",
-       0,              0,              "tosulteax",
+       "toslt00",      "toslta0",      "tosltax",
+       "tosult00",     "tosulta0",     "tosultax",
+       0,              0,              "toslteax",
+       0,              0,              "tosulteax",
     };
 
     /* If the right hand side is const, the lhs is not on stack but still
@@ -3429,44 +3437,77 @@ void g_lt (unsigned flags, unsigned long val)
        /* Give a warning in some special cases */
        if ((flags & CF_UNSIGNED) && val == 0) {
            Warning ("Condition is never true");
+           AddCodeLine ("jsr return0");
+           return;
        }
 
        /* Look at the type */
        switch (flags & CF_TYPE) {
 
            case CF_CHAR:
-               if (flags & CF_FORCECHAR) {
-                   AddCodeLine ("cmp #$%02X", (unsigned char)val);
-                   if (flags & CF_UNSIGNED) {
-                       AddCodeLine ("jsr boolult");
-                   } else {
-                       AddCodeLine ("jsr boollt");
-                   }
-                   return;
-               }
-               /* FALLTHROUGH */
+               if (flags & CF_FORCECHAR) {
+                   AddCodeLine ("cmp #$%02X", (unsigned char)val);
+                   if (flags & CF_UNSIGNED) {
+                       AddCodeLine ("jsr boolult");
+                   } else {
+                       AddCodeLine ("jsr boollt");
+                   }
+                   return;
+               }
+               /* FALLTHROUGH */
 
            case CF_INT:
+               if (flags & CF_UNSIGNED) {
+                   /* Unsigned compare */
+                   /* If the low byte is zero, we must only test the high byte */
+                   AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
+                   if ((val & 0xFF) != 0) {
+                       unsigned L = GetLocalLabel();
+                       AddCodeLine ("bne %s", LocalLabelName (L));
+                       AddCodeLine ("cmp #$%02X", (unsigned char)val);
+                       g_defcodelabel (L);
+                   }
+                   AddCodeLine ("jsr boolult");
+               } else {
+                   /* Signed compare */
+                   if ((val & 0xFF) == 0) {
+                       /* Low byte is zero, just look at the high byte */
+                       AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
+                   } else {
+                       /* Subtract the two values */
+                       AddCodeLine ("cmp #$%02X", (unsigned char)val);
+                       AddCodeLine ("txa");
+                       AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
+                   }
+                   AddCodeLine ("jsr boollt");
+               }
+               return;
+
+           case CF_LONG:
                if ((flags & CF_UNSIGNED) == 0 && val == 0) {
                    /* If we have a signed compare against zero, we only need to
                     * test the high byte.
                     */
+                   AddCodeLine ("lda sreg+1");
+               } else {
+                   /* Do a subtraction */
+                   AddCodeLine ("cmp #$%02X", (unsigned char)val);
                    AddCodeLine ("txa");
+                   AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
+                   AddCodeLine ("lda sreg");
+                   AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
+                   AddCodeLine ("lda sreg+1");
+                   AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
+               }
+               /* Emit the proper makebool routine */
+               if (flags & CF_UNSIGNED) {
+                   /* Unsigned compare */
+                   AddCodeLine ("jsr boolult");
+               } else {
+                   /* Signed compare */
                    AddCodeLine ("jsr boollt");
-                   return;
                }
-               /* Direct code only for unsigned data types */
-               if (flags & CF_UNSIGNED) {
-                   AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
-                           AddCodeLine ("bne *+4");
-                   AddCodeLine ("cmp #$%02X", (unsigned char)val);
-                   AddCodeLine ("jsr boolult");
-                   return;
-               }
-               break;
-
-           case CF_LONG:
-               break;
+               return;
 
            default:
                typeerror (flags);
@@ -3504,13 +3545,32 @@ void g_le (unsigned flags, unsigned long val)
        /* Look at the type */
        switch (flags & CF_TYPE) {
 
-           case CF_CHAR:
-               if (flags & CF_FORCECHAR) {
-                   AddCodeLine ("cmp #$%02X", (unsigned char)val);
-                   if (flags & CF_UNSIGNED) {
-                       AddCodeLine ("jsr boolule");
-                   } else {
-                       AddCodeLine ("jsr boolle");
+           case CF_CHAR:
+               if (flags & CF_FORCECHAR) {
+                   if (flags & CF_UNSIGNED) {
+                       /* Unsigned compare */
+                       if (val < 0xFF) {
+                           /* Use < instead of <= because the former gives
+                            * better code on the 6502 than the latter.
+                            */
+                           g_lt (flags, val+1);
+                       } else {
+                           /* Always true */
+                           Warning ("Condition is always true");
+                           AddCodeLine ("jsr return1");
+                       }
+                   } else {
+                       /* Signed compare */
+                       if ((long) val < 0x7F) {
+                           /* Use < instead of <= because the former gives
+                            * better code on the 6502 than the latter.
+                            */
+                           g_lt (flags, val+1);
+                       } else {
+                           /* Always true */
+                           Warning ("Condition is always true");
+                           AddCodeLine ("jsr return1");
+                       }
                    }
                    return;
                }
@@ -3518,16 +3578,53 @@ void g_le (unsigned flags, unsigned long val)
 
            case CF_INT:
                if (flags & CF_UNSIGNED) {
-                   AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
-                           AddCodeLine ("bne *+4");
-                   AddCodeLine ("cmp #$%02X", (unsigned char)val);
-                   AddCodeLine ("jsr boolule");
-                   return;
+                   /* Unsigned compare */
+                   if (val < 0xFFFF) {
+                       /* Use < instead of <= because the former gives
+                        * better code on the 6502 than the latter.
+                        */
+                       g_lt (flags, val+1);
+                   } else {
+                       /* Always true */
+                       Warning ("Condition is always true");
+                       AddCodeLine ("jsr return1");
+                   }
+               } else {
+                   /* Signed compare */
+                   if ((long) val < 0x7FFF) {
+                       g_lt (flags, val+1);
+                   } else {
+                       /* Always true */
+                       Warning ("Condition is always true");
+                       AddCodeLine ("jsr return1");
+                   }
                }
-               break;
+               return;
 
            case CF_LONG:
-               break;
+               if (flags & CF_UNSIGNED) {
+                   /* Unsigned compare */
+                   if (val < 0xFFFFFFFF) {
+                       /* Use < instead of <= because the former gives
+                        * better code on the 6502 than the latter.
+                        */
+                       g_lt (flags, val+1);
+                   } else {
+                       /* Always true */
+                       Warning ("Condition is always true");
+                       AddCodeLine ("jsr return1");
+                   }
+               } else {
+                   /* Signed compare */
+                   if ((long) val < 0x7FFFFFFF) {
+                       g_lt (flags, val+1);
+                   } else {
+                       /* Always true */
+                       Warning ("Condition is always true");
+                       AddCodeLine ("jsr return1");
+                   }
+               }
+               return;
 
            default:
                typeerror (flags);
@@ -3550,10 +3647,10 @@ void g_gt (unsigned flags, unsigned long val)
 /* Test for greater than */
 {
     static char* ops [12] = {
-       "tosgt00",      "tosgta0",      "tosgtax",
-       "tosugt00",     "tosugta0",     "tosugtax",
-       0,              0,              "tosgteax",
-       0,              0,              "tosugteax",
+       "tosgt00",      "tosgta0",      "tosgtax",
+       "tosugt00",     "tosugta0",     "tosugtax",
+       0,              0,              "tosgteax",
+       0,              0,              "tosugteax",
     };
 
 
@@ -3565,51 +3662,105 @@ void g_gt (unsigned flags, unsigned long val)
        /* Look at the type */
        switch (flags & CF_TYPE) {
 
-           case CF_CHAR:
-               if (flags & CF_FORCECHAR) {
-                   AddCodeLine ("cmp #$%02X", (unsigned char)val);
-                   if (flags & CF_UNSIGNED) {
-                       /* If we have a compare > 0, we will replace it by
-                        * != 0 here, since both are identical but the latter
-                        * is easier to optimize.
-                        */
-                       if (val & 0xFF) {
-                           AddCodeLine ("jsr boolugt");
-                       } else {
-                           AddCodeLine ("jsr boolne");
-                       }
-                   } else {
-                       AddCodeLine ("jsr boolgt");
-                   }
-                   return;
-               }
-               /* FALLTHROUGH */
+           case CF_CHAR:
+               if (flags & CF_FORCECHAR) {
+                   if (flags & CF_UNSIGNED) {
+                               if (val == 0) {
+                           /* If we have a compare > 0, we will replace it by
+                            * != 0 here, since both are identical but the
+                            * latter is easier to optimize.
+                            */
+                           g_ne (flags, val);
+                       } else if (val < 0xFF) {
+                           /* Use >= instead of > because the former gives
+                            * better code on the 6502 than the latter.
+                            */
+                           g_ge (flags, val+1);
+                       } else {
+                           /* Never true */
+                           Warning ("Condition is never true");
+                           AddCodeLine ("jsr return0");
+                       }
+                   } else {
+                       if ((long) val < 0x7F) {
+                           /* Use >= instead of > because the former gives
+                            * better code on the 6502 than the latter.
+                            */
+                           g_ge (flags, val+1);
+                       } else {
+                           /* Never true */
+                           Warning ("Condition is never true");
+                           AddCodeLine ("jsr return0");
+                       }
+                   }
+                   return;
+               }
+               /* FALLTHROUGH */
 
-           case CF_INT:
-               if (flags & CF_UNSIGNED) {
-                   /* If we have a compare > 0, we will replace it by
-                    * != 0 here, since both are identical but the latter
-                    * is easier to optimize.
-                    */
-                   if ((val & 0xFFFF) == 0) {
-                       AddCodeLine ("stx tmp1");
-                       AddCodeLine ("ora tmp1");
-                       AddCodeLine ("jsr boolne");
+           case CF_INT:
+               if (flags & CF_UNSIGNED) {
+                   /* Unsigned compare */
+                   if (val == 0) {
+                       /* If we have a compare > 0, we will replace it by
+                        * != 0 here, since both are identical but the latter
+                        * is easier to optimize.
+                        */
+                       g_ne (flags, val);
+                   } else if (val < 0xFFFF) {
+                       /* Use >= instead of > because the former gives better
+                        * code on the 6502 than the latter.
+                        */
+                       g_ge (flags, val+1);
                    } else {
-                               AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
-                       AddCodeLine ("bne *+4");
-                       AddCodeLine ("cmp #$%02X", (unsigned char)val);
-                               AddCodeLine ("jsr boolugt");
-                   }
-                   return;
-                       }
-               break;
+                       /* Never true */
+                       Warning ("Condition is never true");
+                       AddCodeLine ("jsr return0");
+                   }
+                       } else {
+                   /* Signed compare */
+                   if ((long) val < 0x7FFF) {
+                       g_ge (flags, val+1);
+                   } else {
+                       /* Never true */
+                       Warning ("Condition is never true");
+                       AddCodeLine ("jsr return0");
+                   }
+               }
+               return;
 
            case CF_LONG:
-               break;
+               if (flags & CF_UNSIGNED) {
+                   /* Unsigned compare */
+                   if (val == 0) {
+                       /* If we have a compare > 0, we will replace it by
+                        * != 0 here, since both are identical but the latter
+                        * is easier to optimize.
+                        */
+                       g_ne (flags, val);
+                   } else if (val < 0xFFFFFFFF) {
+                       /* Use >= instead of > because the former gives better
+                        * code on the 6502 than the latter.
+                        */
+                       g_ge (flags, val+1);
+                   } else {
+                       /* Never true */
+                       Warning ("Condition is never true");
+                       AddCodeLine ("jsr return0");
+                   }
+                       } else {
+                   /* Signed compare */
+                   if ((long) val < 0x7FFFFFFF) {
+                       g_ge (flags, val+1);
+                   } else {
+                       /* Never true */
+                       Warning ("Condition is never true");
+                       AddCodeLine ("jsr return0");
+                   }
+               }
+               return;
 
            default:
-               typeerror (flags);
+               typeerror (flags);
        }
 
        /* If we go here, we didn't emit code. Push the lhs on stack and fall
@@ -3644,6 +3795,8 @@ void g_ge (unsigned flags, unsigned long val)
        /* Give a warning in some special cases */
        if ((flags & CF_UNSIGNED) && val == 0) {
            Warning ("Condition is always true");
+           AddCodeLine ("jsr return1");
+           return;
        }
 
        /* Look at the type */
@@ -3663,19 +3816,59 @@ void g_ge (unsigned flags, unsigned long val)
 
            case CF_INT:
                if (flags & CF_UNSIGNED) {
-                           AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
-                           AddCodeLine ("bne *+4");
-                   AddCodeLine ("cmp #$%02X", (unsigned char)val);
+                   /* Unsigned compare */
+                   /* If the low byte is zero, we must only test the high byte */
+                   AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
+                   if ((val & 0xFF) != 0) {
+                       unsigned L = GetLocalLabel();
+                       AddCodeLine ("bne %s", LocalLabelName (L));
+                       AddCodeLine ("cmp #$%02X", (unsigned char)val);
+                       g_defcodelabel (L);
+                   }
                    AddCodeLine ("jsr booluge");
-                   return;
+               } else {
+                   /* Signed compare */
+                   if ((val & 0xFF) == 0) {
+                       /* Low byte is zero, just look at the high byte */
+                       AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
+                   } else {
+                       /* Subtract the two values */
+                       AddCodeLine ("cmp #$%02X", (unsigned char)val);
+                       AddCodeLine ("txa");
+                       AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
+                   }
+                   AddCodeLine ("jsr boolge");
                }
-               break;
+               return;
 
            case CF_LONG:
-               break;
+               if ((flags & CF_UNSIGNED) == 0 && val == 0) {
+                   /* If we have a signed compare against zero, we only need to
+                    * test the high byte.
+                    */
+                   AddCodeLine ("lda sreg+1");
+               } else {
+                   /* Do a subtraction */
+                   AddCodeLine ("cmp #$%02X", (unsigned char)val);
+                   AddCodeLine ("txa");
+                   AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
+                   AddCodeLine ("lda sreg");
+                   AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
+                   AddCodeLine ("lda sreg+1");
+                   AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
+               }
+               /* Emit the proper makebool routine */
+               if (flags & CF_UNSIGNED) {
+                   /* Unsigned compare */
+                   AddCodeLine ("jsr booluge");
+               } else {
+                   /* Signed compare */
+                   AddCodeLine ("jsr boolge");
+               }
+               return;
 
            default:
-               typeerror (flags);
+               typeerror (flags);
        }
 
        /* If we go here, we didn't emit code. Push the lhs on stack and fall
@@ -3705,7 +3898,7 @@ void g_res (unsigned n)
 
 
 
-void g_defdata (unsigned flags, unsigned long val, unsigned offs)
+void g_defdata (unsigned flags, unsigned long val, long offs)
 /* Define data with the size given in flags */
 {
     if (flags & CF_CONST) {
@@ -3737,7 +3930,7 @@ void g_defdata (unsigned flags, unsigned long val, unsigned offs)
        const char* Label = GetLabelName (flags, val, offs);
 
        /* Labels are always 16 bit */
-       AddDataLine ("\t.word\t%s", Label);
+               AddDataLine ("\t.addr\t%s", Label);
 
     }
 }
@@ -3788,33 +3981,179 @@ void g_zerobytes (unsigned n)
 
 
 
+void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
+/* Initialize a register variable from static initialization data */
+{
+    /* Register variables do always have less than 128 bytes */
+    unsigned CodeLabel = GetLocalLabel ();
+    ldxconst (Size-1);
+    g_defcodelabel (CodeLabel);
+    AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
+    AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
+    AddCodeLine ("dex");
+    AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
+}
+
+
+
+void g_initauto (unsigned Label, unsigned Size)
+/* Initialize a local variable at stack offset zero from static data */
+{
+    unsigned CodeLabel = GetLocalLabel ();
+
+    CheckLocalOffs (Size);
+    if (Size <= 128) {
+        ldyconst (Size-1);
+       g_defcodelabel (CodeLabel);
+        AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
+        AddCodeLine ("sta (sp),y");
+        AddCodeLine ("dey");
+        AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
+    } else if (Size <= 256) {
+        ldyconst (0);
+       g_defcodelabel (CodeLabel);
+        AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
+        AddCodeLine ("sta (sp),y");
+        AddCodeLine ("iny");
+        AddCodeLine ("cpy #$%02X", (unsigned char) Size);
+        AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
+    }
+}
+
+
+
+void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
+/* Initialize a static local variable from static initialization data */
+{
+    if (Size <= 128) {
+        unsigned CodeLabel = GetLocalLabel ();
+        ldyconst (Size-1);
+        g_defcodelabel (CodeLabel);
+        AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
+        AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
+        AddCodeLine ("dey");
+        AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
+    } else if (Size <= 256) {
+        unsigned CodeLabel = GetLocalLabel ();
+        ldyconst (0);
+       g_defcodelabel (CodeLabel);
+        AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
+        AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
+        AddCodeLine ("iny");
+        AddCodeLine ("cpy #$%02X", (unsigned char) Size);
+        AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
+    } else {
+        /* Use the easy way here: memcpy */
+        g_getimmed (CF_STATIC, VarLabel, 0);
+        AddCodeLine ("jsr pushax");
+        g_getimmed (CF_STATIC, InitLabel, 0);
+        AddCodeLine ("jsr pushax");
+        g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
+        AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
+    }
+}
+
+
+
 /*****************************************************************************/
-/*                      User supplied assembler code                        */
+/*                                    Switch statement                              */
 /*****************************************************************************/
 
 
 
-void g_asmcode (const char* Line, int Len)
-/* Output one line of assembler code. If Len is greater than zero, it is used
- * as the maximum number of characters to use from Line.
- */
+void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
+/* Generate code for a switch statement */
 {
-    if (Len >= 0) {
-       AddCodeLine ("%.*s", Len, Line);
-    } else {
-       AddCodeLine ("%s", Line);
+    unsigned NextLabel = 0;
+    unsigned I;
+
+    /* Setup registers and determine which compare insn to use */
+    const char* Compare;
+    switch (Depth) {
+       case 1:
+           Compare = "cmp #$%02X";
+           break;
+       case 2:
+           Compare = "cpx #$%02X";
+           break;
+       case 3:
+           AddCodeLine ("ldy sreg");
+           Compare = "cpy #$%02X";
+           break;
+       case 4:
+           AddCodeLine ("ldy sreg+1");
+           Compare = "cpy #$%02X";
+           break;
+       default:
+           Internal ("Invalid depth in g_switch: %u", Depth);
     }
+
+    /* Walk over all nodes */
+    for (I = 0; I < CollCount (Nodes); ++I) {
+
+       /* Get the next case node */
+       CaseNode* N = CollAtUnchecked (Nodes, I);
+
+       /* If we have a next label, define it */
+       if (NextLabel) {
+           g_defcodelabel (NextLabel);
+           NextLabel = 0;
+       }
+
+       /* Do the compare */
+       AddCodeLine (Compare, CN_GetValue (N));
+
+       /* If this is the last level, jump directly to the case code if found */
+       if (Depth == 1) {
+
+           /* Branch if equal */
+           g_falsejump (0, CN_GetLabel (N));
+
+       } else {
+
+           /* Determine the next label */
+           if (I == CollCount (Nodes) - 1) {
+               /* Last node means not found */
+               g_truejump (0, DefaultLabel);
+           } else {
+               /* Jump to the next check */
+               NextLabel = GetLocalLabel ();
+               g_truejump (0, NextLabel);
+           }
+
+           /* Check the next level */
+           g_switch (N->Nodes, DefaultLabel, Depth-1);
+
+       }
+    }
+
+    /* If we go here, we haven't found the label */
+    g_jump (DefaultLabel);
+}
+
+
+
+/*****************************************************************************/
+/*                      User supplied assembler code                        */
+/*****************************************************************************/
+
+
+
+void g_asmcode (struct StrBuf* B)
+/* Output one line of assembler code. */
+{
+    AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));
 }
 
 
 
 /*****************************************************************************/
-/*                         Inlined known functions                          */
+/*                         Inlined known functions                          */
 /*****************************************************************************/
 
 
 
-void g_strlen (unsigned flags, unsigned long val, unsigned offs)
+void g_strlen (unsigned flags, unsigned long val, long offs)
 /* Inline the strlen() function */
 {
     /* We need a label in both cases */
@@ -3824,7 +4163,7 @@ void g_strlen (unsigned flags, unsigned long val, unsigned offs)
     if (flags & CF_CONST) {
 
        /* The address of the string is constant. Create the correct label name */
-       char* lbuf = GetLabelName (flags, val, offs);
+       const char* lbuf = GetLabelName (flags, val, offs);
 
        /* Generate the strlen code */
        AddCodeLine ("ldy #$FF");