]> git.sur5r.net Git - cc65/blobdiff - src/cc65/codegen.c
Small but significant shift optimization
[cc65] / src / cc65 / codegen.c
index ce7ef5d2c5dda83b18fca451ae313013b0c014fa..40dcd46ab3a48b63f8e390835d233d0fb7727d0c 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                                                 */
 
 /* common */
 #include "check.h"
+#include "strbuf.h"
 #include "version.h"
 #include "xmalloc.h"
 #include "xsprintf.h"
 
 /* cc65 */
 #include "asmcode.h"
-#include "asmlabel.h"                        
+#include "asmlabel.h"
+#include "casenode.h"
 #include "codeseg.h"
 #include "cpu.h"
 #include "dataseg.h"
 #include "error.h"
 #include "global.h"
-#include "segname.h"
+#include "segments.h"
+#include "textseg.h"
 #include "util.h"
 #include "codegen.h"
 
 /* Compiler relative stack pointer */
 int oursp      = 0;
 
-/* Current segment */
-segment_t CurSeg = SEG_INV;
-
 
 
 /*****************************************************************************/
-/*                                 Helpers                                  */
+/*                                         Helpers                                  */
 /*****************************************************************************/
 
 
@@ -96,31 +96,39 @@ 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:
@@ -128,13 +136,13 @@ static char* GetLabelName (unsigned flags, unsigned long label, unsigned offs)
     }
 
     /* Return a pointer to the static buffer */
-    return lbuf;
+    return Buf;
 }
 
 
 
 /*****************************************************************************/
-/*                           Pre- and postamble                             */
+/*                           Pre- and postamble                             */
 /*****************************************************************************/
 
 
@@ -142,84 +150,66 @@ static char* GetLabelName (unsigned flags, unsigned long label, unsigned offs)
 void g_preamble (void)
 /* Generate the assembler code preamble */
 {
-    /* Generate the global segments and push them */
-    PushCodeSeg (NewCodeSeg (SegmentNames[SEG_CODE], ""));
-    PushDataSeg (NewDataSeg (""));
+    /* Create a new (global) segment list and remember it */
+    PushSegments (0);
+    GS = CS;
 
     /* Identify the compiler version */
-    AddDataSegLine (DS, "; File generated by cc65 v %u.%u.%u",
-                   VER_MAJOR, VER_MINOR, VER_PATCH);
+    AddTextLine (";");
+    AddTextLine ("; File generated by cc65 v %u.%u.%u",
+                VER_MAJOR, VER_MINOR, VER_PATCH);
+    AddTextLine (";");
 
     /* Insert some object file options */
-    AddDataSegLine (DS, ".fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
-                   VER_MAJOR, VER_MINOR, VER_PATCH);
+    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) {
-       AddDataSegLine (DS, ".pc02");
+       AddTextLine ("\t.pc02");
     }
 
     /* Allow auto import for runtime library routines */
-    AddDataSegLine (DS, ".autoimport\ton");
+    AddTextLine ("\t.autoimport\ton");
 
     /* Switch the assembler into case sensitive mode */
-    AddDataSegLine (DS, ".case\t\ton");
+    AddTextLine ("\t.case\t\ton");
 
     /* Tell the assembler if we want to generate debug info */
-    AddDataSegLine (DS, ".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 */
-    AddDataSegLine (DS, ".importzp\tsp, sreg, regsave, regbank, tmp1, ptr1");
+    AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
 
     /* Define long branch macros */
-    AddDataSegLine (DS, ".macpack\tlongbranch");
+    AddTextLine ("\t.macpack\tlongbranch");
 }
 
 
 
-/*****************************************************************************/
-/*                             Segment support                              */
-/*****************************************************************************/
-
-
-
-static void UseSeg (int NewSeg)
-/* Switch to a specific segment */
+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 (CurSeg != NewSeg) {
-       CurSeg = (segment_t) NewSeg;
-       if (CurSeg != SEG_CODE) {
-           AddDataSegLine (DS, ".segment\t\"%s\"", SegmentNames [CurSeg]);
-       }
+    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);
     }
 }
 
 
 
-void g_pushseg (struct CodeSeg** FCS, struct DataSeg** FDS, const char* FuncName)
-/* Push the current segments and generate new ones for the given function */
-{
-    PushCodeSeg (NewCodeSeg (SegmentNames[SEG_CODE], FuncName));
-    *FCS = CS;
-    PushDataSeg (NewDataSeg (FuncName));
-    *FDS = DS;
-}
-
-
-
-void g_popseg (void)
-/* Restore the old segments */
-{
-    PopCodeSeg ();
-    PopDataSeg ();
-}
+/*****************************************************************************/
+/*                             Segment support                              */
+/*****************************************************************************/
 
 
 
 void g_userodata (void)
 /* Switch to the read only data segment */
 {
-    UseSeg (SEG_RODATA);
+    UseDataSeg (SEG_RODATA);
 }
 
 
@@ -227,7 +217,7 @@ void g_userodata (void)
 void g_usedata (void)
 /* Switch to the data segment */
 {
-    UseSeg (SEG_DATA);
+    UseDataSeg (SEG_DATA);
 }
 
 
@@ -235,62 +225,35 @@ void g_usedata (void)
 void g_usebss (void)
 /* Switch to the bss segment */
 {
-    UseSeg (SEG_BSS);
+    UseDataSeg (SEG_BSS);
 }
 
 
 
-static void SegName (segment_t Seg, const char* Name)
+void g_segname (segment_t Seg, const char* Name)
 /* Set the name of a segment */
 {
-    /* Free the old name and set a new one */
+    DataSeg* S;
+
+    /* Remember the new name */
     NewSegName (Seg, Name);
 
-    /* If the new segment is the current segment, emit a segment directive
-     * with the new name.
-     */
-    if (Seg == CurSeg) {
-               CurSeg = SEG_INV;       /* Invalidate */
-       UseSeg (Seg);
+    /* 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);
     }
-}
-
-
-
-void g_codename (const char* Name)
-/* Set the name of the CODE segment */
-{
-    SegName (SEG_CODE, Name);
-}
-
-
-
-void g_rodataname (const char* Name)
-/* Set the name of the RODATA segment */
-{
-    SegName (SEG_RODATA, Name);
-}
-
-
-
-void g_dataname (const char* Name)
-/* Set the name of the DATA segment */
-{
-    SegName (SEG_DATA, Name);
-}
-
-
-
-void g_bssname (const char* Name)
-/* Set the name of the BSS segment */
-{
-    SegName (SEG_BSS, Name);
 }
 
 
 
 /*****************************************************************************/
-/*                                  Code                                    */
+/*                                          Code                                    */
 /*****************************************************************************/
 
 
@@ -372,7 +335,7 @@ static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
 void g_defcodelabel (unsigned label)
 /* Define a local code label */
 {
-    AddCodeLabel (CS, LocalLabelName (label));
+    CS_AddLabel (CS->Code, LocalLabelName (label));
 }
 
 
@@ -380,7 +343,7 @@ void g_defcodelabel (unsigned label)
 void g_defdatalabel (unsigned label)
 /* Define a local data label */
 {
-    AddDataSegLine (DS, "%s:", LocalLabelName (label));
+    AddDataLine ("%s:", LocalLabelName (label));
 }
 
 
@@ -395,7 +358,7 @@ void g_defgloblabel (const char* Name)
 /* Define a global label with the given name */
 {
     /* Global labels are always data labels */
-    AddDataSegLine (DS, "_%s:", Name);
+    AddDataLine ("_%s:", Name);
 }
 
 
@@ -404,9 +367,9 @@ void g_defexport (const char* Name, int ZP)
 /* Export the given label */
 {
     if (ZP) {
-               AddDataSegLine (DS, "\t.exportzp\t_%s", Name);
+               AddTextLine ("\t.exportzp\t_%s", Name);
     } else {
-       AddDataSegLine (DS, "\t.export\t\t_%s", Name);
+       AddTextLine ("\t.export\t\t_%s", Name);
     }
 }
 
@@ -416,9 +379,9 @@ void g_defimport (const char* Name, int ZP)
 /* Import the given label */
 {
     if (ZP) {
-               AddDataSegLine (DS, "\t.importzp\t_%s", Name);
+               AddTextLine ("\t.importzp\t_%s", Name);
     } else {
-       AddDataSegLine (DS, "\t.import\t\t_%s", Name);
+       AddTextLine ("\t.import\t\t_%s", Name);
     }
 }
 
@@ -433,7 +396,7 @@ void g_defimport (const char* Name, int ZP)
 static void ldaconst (unsigned val)
 /* Load a with a constant */
 {
-    AddCodeSegLine (CS, "lda #$%02X", val & 0xFF);
+    AddCodeLine ("lda #$%02X", val & 0xFF);
 }
 
 
@@ -441,7 +404,7 @@ static void ldaconst (unsigned val)
 static void ldxconst (unsigned val)
 /* Load x with a constant */
 {
-    AddCodeSegLine (CS, "ldx #$%02X", val & 0xFF);
+    AddCodeLine ("ldx #$%02X", val & 0xFF);
 }
 
 
@@ -449,7 +412,7 @@ static void ldxconst (unsigned val)
 static void ldyconst (unsigned val)
 /* Load y with a constant */
 {
-    AddCodeSegLine (CS, "ldy #$%02X", val & 0xFF);
+    AddCodeLine ("ldy #$%02X", val & 0xFF);
 }
 
 
@@ -476,83 +439,47 @@ void g_enter (unsigned flags, unsigned argsize)
        funcargs = argsize;
     } else {
                funcargs = -1;
-               AddCodeSegLine (CS, "jsr enter");
+               AddCodeLine ("jsr enter");
     }
 }
 
 
 
-void g_leave (int flags, int val)
+void g_leave (void)
 /* Function epilogue */
 {
-    int k;
-    char buf [40];
-
-    /* CF_REG is set if we're returning a value from the function */
-    if ((flags & CF_REG) == 0) {
-       AddCodeHint ("x:-");
-       AddCodeHint ("a:-");
-    }
-
     /* How many bytes of locals do we have to drop? */
-    k = -oursp;
-
+    int k = -oursp;
+                                  
     /* If we didn't have a variable argument list, don't call leave */
     if (funcargs >= 0) {
 
-       /* Load a function return code if needed */
-       if ((flags & CF_CONST) != 0) {
-           g_getimmed (flags, val, 0);
-       }
-
-       /* Drop stackframe or leave with rts */
+       /* Drop stackframe if needed */
        k += funcargs;
-       if (k == 0) {
-           AddCodeHint ("y:-");        /* Y register no longer used */
-           AddCodeSegLine (CS, "rts");
-       } else if (k <= 8) {
-           AddCodeHint ("y:-");        /* Y register no longer used */
-           AddCodeSegLine (CS, "jmp incsp%d", k);
-       } else {
-           CheckLocalOffs (k);
-           ldyconst (k);
-           AddCodeSegLine (CS, "jmp addysp");
+               if (k > 0) {
+           if (k <= 8) {
+           AddCodeLine ("jsr incsp%d", k);
+           } else {
+               CheckLocalOffs (k);
+               ldyconst (k);
+               AddCodeLine ("jsr addysp");
+           }
        }
 
     } else {
 
-       strcpy (buf, "\tjmp\tleave");
-       if (k) {
+       if (k == 0) {
+           /* Nothing to drop */
+           AddCodeLine ("jsr leave");
+       } else {
            /* We've a stack frame to drop */
            ldyconst (k);
-           strcat (buf, "y");
-       } else {
-           /* Y register no longer used */
-           AddCodeHint ("y:-");
+           AddCodeLine ("jsr leavey");
        }
-       if (flags & CF_CONST) {
-           if ((flags & CF_TYPE) != CF_LONG) {
-               /* Constant int sized value given for return code */
-               if (val == 0) {
-                   /* Special case: return 0 */
-                   strcat (buf, "00");
-                       } else if (((val >> 8) & 0xFF) == 0) {
-                   /* Special case: constant with high byte zero */
-                   ldaconst (val);             /* Load low byte */
-                   strcat (buf, "0");
-               } else {
-                   /* Others: arbitrary constant value */
-                   g_getimmed (flags, val, 0); /* Load value */
-               }
-           } else {
-               /* Constant long value: No shortcut possible */
-               g_getimmed (flags, val, 0);
-           }
-       }
-
-       /* Output the jump */
-       AddCodeSegLine (CS, buf);
     }
+
+    /* Add the final rts */
+    AddCodeLine ("rts");
 }
 
 
@@ -569,14 +496,14 @@ void g_save_regvars (int RegOffs, unsigned Bytes)
     /* Don't loop for up to two bytes */
     if (Bytes == 1) {
 
-       AddCodeSegLine (CS, "lda regbank%+d", RegOffs);
-               AddCodeSegLine (CS, "jsr pusha");
+       AddCodeLine ("lda regbank%+d", RegOffs);
+               AddCodeLine ("jsr pusha");
 
     } else if (Bytes == 2) {
 
-               AddCodeSegLine (CS, "lda regbank%+d", RegOffs);
-       AddCodeSegLine (CS, "ldx regbank%+d", RegOffs+1);
-               AddCodeSegLine (CS, "jsr pushax");
+               AddCodeLine ("lda regbank%+d", RegOffs);
+       AddCodeLine ("ldx regbank%+d", RegOffs+1);
+               AddCodeLine ("jsr pushax");
 
     } else {
 
@@ -586,11 +513,11 @@ void g_save_regvars (int RegOffs, unsigned Bytes)
        ldyconst (Bytes - 1);
        ldxconst (Bytes);
        g_defcodelabel (Label);
-       AddCodeSegLine (CS, "lda regbank%+d,x", RegOffs-1);
-       AddCodeSegLine (CS, "sta (sp),y");
-       AddCodeSegLine (CS, "dey");
-       AddCodeSegLine (CS, "dex");
-       AddCodeSegLine (CS, "bne %s", LocalLabelName (Label));
+       AddCodeLine ("lda regbank%+d,x", RegOffs-1);
+       AddCodeLine ("sta (sp),y");
+       AddCodeLine ("dey");
+       AddCodeLine ("dex");
+       AddCodeLine ("bne %s", LocalLabelName (Label));
 
     }
 
@@ -611,17 +538,17 @@ void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
     if (Bytes == 1) {
 
        ldyconst (StackOffs);
-       AddCodeSegLine (CS, "lda (sp),y");
-       AddCodeSegLine (CS, "sta regbank%+d", RegOffs);
+       AddCodeLine ("lda (sp),y");
+       AddCodeLine ("sta regbank%+d", RegOffs);
 
     } else if (Bytes == 2) {
 
        ldyconst (StackOffs);
-       AddCodeSegLine (CS, "lda (sp),y");
-       AddCodeSegLine (CS, "sta regbank%+d", RegOffs);
-       AddCodeSegLine (CS, "iny");
-       AddCodeSegLine (CS, "lda (sp),y");
-       AddCodeSegLine (CS, "sta regbank%+d", RegOffs+1);
+       AddCodeLine ("lda (sp),y");
+       AddCodeLine ("sta regbank%+d", RegOffs);
+       AddCodeLine ("iny");
+       AddCodeLine ("lda (sp),y");
+       AddCodeLine ("sta regbank%+d", RegOffs+1);
 
     } else {
 
@@ -630,11 +557,11 @@ void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
        ldyconst (StackOffs+Bytes-1);
        ldxconst (Bytes);
        g_defcodelabel (Label);
-       AddCodeSegLine (CS, "lda (sp),y");
-       AddCodeSegLine (CS, "sta regbank%+d,x", RegOffs-1);
-       AddCodeSegLine (CS, "dey");
-       AddCodeSegLine (CS, "dex");
-       AddCodeSegLine (CS, "bne %s", LocalLabelName (Label));
+       AddCodeLine ("lda (sp),y");
+       AddCodeLine ("sta regbank%+d,x", RegOffs-1);
+       AddCodeLine ("dey");
+       AddCodeLine ("dex");
+       AddCodeLine ("bne %s", LocalLabelName (Label));
 
     }
 }
@@ -647,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) {
-                   AddCodeSegLine (CS, "ldx #$00");
-                   AddCodeSegLine (CS, "stx sreg+1");
-                   AddCodeSegLine (CS, "stx sreg");
-                   AddCodeSegLine (CS, "lda #$%02X", (unsigned char) val);
-               } else if ((val & 0xFFFF00FF) == 0) {
-                   AddCodeSegLine (CS, "lda #$00");
-                   AddCodeSegLine (CS, "sta sreg+1");
-                   AddCodeSegLine (CS, "sta sreg");
-                   AddCodeSegLine (CS, "ldx #$%02X", (unsigned char) (val >> 8));
-               } else if ((val & 0xFFFF0000) == 0 && CodeSizeFactor > 140) {
-                   AddCodeSegLine (CS, "lda #$00");
-                   AddCodeSegLine (CS, "sta sreg+1");
-                   AddCodeSegLine (CS, "sta sreg");
-                   AddCodeSegLine (CS, "lda #$%02X", (unsigned char) val);
-                   AddCodeSegLine (CS, "ldx #$%02X", (unsigned char) (val >> 8));
-               } else if ((val & 0xFFFFFF00) == 0xFFFFFF00) {
-                   AddCodeSegLine (CS, "ldx #$FF");
-                   AddCodeSegLine (CS, "stx sreg+1");
-                   AddCodeSegLine (CS, "stx sreg");
-                   if ((val & 0xFF) == 0xFF) {
-                       AddCodeSegLine (CS, "txa");
-                   } else {
-                       AddCodeSegLine (CS, "lda #$%02X", (unsigned char) val);
-                   }
-               } else if ((val & 0xFFFF00FF) == 0xFFFF00FF) {
-                   AddCodeSegLine (CS, "lda #$FF");
-                   AddCodeSegLine (CS, "sta sreg+1");
-                   AddCodeSegLine (CS, "sta sreg");
-                   AddCodeSegLine (CS, "ldx #$%02X", (unsigned char) (val >> 8));
-               } else {
-                   /* Call a subroutine that will load following value */
-                   AddCodeSegLine (CS, "jsr ldeax");
-                   AddCodeSegLine (CS, ".dword $%08lX", val & 0xFFFFFFFF);
+               /* 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");
+                   Done |= 0x04;
+               }
+               if (B2 == B4) {
+                   AddCodeLine ("stx sreg+1");
+                   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;
 
        }
@@ -713,63 +649,64 @@ 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 */
-       AddCodeSegLine (CS, "lda #<(%s)", Label);
-       AddCodeSegLine (CS, "ldx #>(%s)", Label);
+       AddCodeLine ("lda #<(%s)", Label);
+       AddCodeLine ("ldx #>(%s)", Label);
 
     }
 }
 
 
 
-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) {
 
        case CF_CHAR:
            if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
-               AddCodeSegLine (CS, "lda %s", lbuf);    /* load A from the label */
+               AddCodeLine ("lda %s", lbuf);   /* load A from the label */
                    } else {
                ldxconst (0);
-               AddCodeSegLine (CS, "lda %s", lbuf);    /* load A from the label */
+               AddCodeLine ("lda %s", lbuf);   /* load A from the label */
                if (!(flags & CF_UNSIGNED)) {
                    /* Must sign extend */
-                   AddCodeSegLine (CS, "bpl *+3");
-                   AddCodeSegLine (CS, "dex");
-                   AddCodeHint ("x:!");                /* X is invalid now */
+                   unsigned L = GetLocalLabel ();
+                   AddCodeLine ("bpl %s", LocalLabelName (L));
+                   AddCodeLine ("dex");
+                   g_defcodelabel (L);
                }
            }
            break;
 
        case CF_INT:
-           AddCodeSegLine (CS, "lda %s", lbuf);
+           AddCodeLine ("lda %s", lbuf);
            if (flags & CF_TEST) {
-               AddCodeSegLine (CS, "ora %s+1", lbuf);
+               AddCodeLine ("ora %s+1", lbuf);
            } else {
-               AddCodeSegLine (CS, "ldx %s+1", lbuf);
+               AddCodeLine ("ldx %s+1", lbuf);
            }
            break;
 
        case CF_LONG:
            if (flags & CF_TEST) {
-               AddCodeSegLine (CS, "lda %s+3", lbuf);
-               AddCodeSegLine (CS, "ora %s+2", lbuf);
-               AddCodeSegLine (CS, "ora %s+1", lbuf);
-               AddCodeSegLine (CS, "ora %s+0", lbuf);
+               AddCodeLine ("lda %s+3", lbuf);
+               AddCodeLine ("ora %s+2", lbuf);
+               AddCodeLine ("ora %s+1", lbuf);
+               AddCodeLine ("ora %s+0", lbuf);
            } else {
-               AddCodeSegLine (CS, "lda %s+3", lbuf);
-               AddCodeSegLine (CS, "sta sreg+1");
-               AddCodeSegLine (CS, "lda %s+2", lbuf);
-               AddCodeSegLine (CS, "sta sreg");
-               AddCodeSegLine (CS, "ldx %s+1", lbuf);
-               AddCodeSegLine (CS, "lda %s", lbuf);
+               AddCodeLine ("lda %s+3", lbuf);
+               AddCodeLine ("sta sreg+1");
+               AddCodeLine ("lda %s+2", lbuf);
+               AddCodeLine ("sta sreg");
+               AddCodeLine ("ldx %s+1", lbuf);
+               AddCodeLine ("lda %s", lbuf);
            }
            break;
 
@@ -790,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) {
-                   AddCodeSegLine (CS, "lda (sp)");
-               } else {
-                   ldyconst (offs);
-                   AddCodeSegLine (CS, "lda (sp),y");
-               }
+               ldyconst (offs);
+               AddCodeLine ("lda (sp),y");
            } else {
-               if (offs == 0) {
-                   AddCodeSegLine (CS, "ldx #$00");
-                   AddCodeSegLine (CS, "lda (sp,x)");
-               } else {
-                   ldyconst (offs);
-                   AddCodeSegLine (CS, "ldx #$00");
-                   AddCodeSegLine (CS, "lda (sp),y");
-               }
+               ldyconst (offs);
+               AddCodeLine ("ldx #$00");
+               AddCodeLine ("lda (sp),y");
                if ((flags & CF_UNSIGNED) == 0) {
-                   AddCodeSegLine (CS, "bpl *+3");
-                   AddCodeSegLine (CS, "dex");
-                   AddCodeHint ("x:!");        /* X is invalid now */
+                   unsigned L = GetLocalLabel();
+                   AddCodeLine ("bpl %s", LocalLabelName (L));
+                   AddCodeLine ("dex");
+                   g_defcodelabel (L);
                }
            }
            break;
@@ -817,34 +746,18 @@ void g_getlocal (unsigned flags, int offs)
            CheckLocalOffs (offs + 1);
                    if (flags & CF_TEST) {
                ldyconst (offs + 1);
-               AddCodeSegLine (CS, "lda (sp),y");
-               AddCodeSegLine (CS, "dey");
-               AddCodeSegLine (CS, "ora (sp),y");
+               AddCodeLine ("lda (sp),y");
+               AddCodeLine ("dey");
+               AddCodeLine ("ora (sp),y");
            } else {
-               if (CodeSizeFactor > 180) {
-                   ldyconst (offs + 1);
-                   AddCodeSegLine (CS, "lda (sp),y");
-                   AddCodeSegLine (CS, "tax");
-                   AddCodeSegLine (CS, "dey");
-                   AddCodeSegLine (CS, "lda (sp),y");
-               } else {
-                   if (offs) {
-                       ldyconst (offs+1);
-                               AddCodeSegLine (CS, "jsr ldaxysp");
-                   } else {
-                       AddCodeSegLine (CS, "jsr ldax0sp");
-                   }
-               }
+               ldyconst (offs+1);
+               AddCodeLine ("jsr ldaxysp");
            }
            break;
 
        case CF_LONG:
-           if (offs) {
-               ldyconst (offs+3);
-               AddCodeSegLine (CS, "jsr ldeaxysp");
-           } else {
-               AddCodeSegLine (CS, "jsr ldeax0sp");
-           }
+           ldyconst (offs+3);
+           AddCodeLine ("jsr ldeaxysp");
                    break;
 
                default:
@@ -870,56 +783,34 @@ void g_getind (unsigned flags, unsigned offs)
 
        case CF_CHAR:
                    /* Character sized */
-           if (offs) {
-               ldyconst (offs);
-               if (flags & CF_UNSIGNED) {
-                   AddCodeSegLine (CS, "jsr ldauidx");
-                       } else {
-                   AddCodeSegLine (CS, "jsr ldaidx");
-               }
-           } else {
-               if (flags & CF_UNSIGNED) {
-                   if (CodeSizeFactor > 250) {
-                       AddCodeSegLine (CS, "sta ptr1");
-                       AddCodeSegLine (CS, "stx ptr1+1");
-                       AddCodeSegLine (CS, "ldx #$00");
-                       AddCodeSegLine (CS, "lda (ptr1,x)");
-                   } else {
-                       AddCodeSegLine (CS, "jsr ldaui");
-                   }
-               } else {
-                           AddCodeSegLine (CS, "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);
-               AddCodeSegLine (CS, "sta ptr1");
-               AddCodeSegLine (CS, "stx ptr1+1");
-               AddCodeSegLine (CS, "lda (ptr1),y");
-               AddCodeSegLine (CS, "iny");
-               AddCodeSegLine (CS, "ora (ptr1),y");
+               ldyconst (offs);
+               AddCodeLine ("sta ptr1");
+               AddCodeLine ("stx ptr1+1");
+               AddCodeLine ("lda (ptr1),y");
+               AddCodeLine ("iny");
+               AddCodeLine ("ora (ptr1),y");
            } else {
-               if (offs == 0) {
-                   AddCodeSegLine (CS, "jsr ldaxi");
-               } else {
-                   ldyconst (offs+1);
-                   AddCodeSegLine (CS, "jsr ldaxidx");
-               }
+               ldyconst (offs+1);
+               AddCodeLine ("jsr ldaxidx");
            }
            break;
 
                case CF_LONG:
-           if (offs == 0) {
-               AddCodeSegLine (CS, "jsr ldeaxi");
-           } else {
-               ldyconst (offs+3);
-               AddCodeSegLine (CS, "jsr ldeaxidx");
-           }
+           ldyconst (offs+3);
+           AddCodeLine ("jsr ldeaxidx");
            if (flags & CF_TEST) {
-                       AddCodeSegLine (CS, "jsr tsteax");
+                       AddCodeLine ("jsr tsteax");
            }
            break;
 
@@ -939,29 +830,29 @@ void g_leasp (int offs)
 
     /* For value 0 we do direct code */
     if (offs == 0) {
-               AddCodeSegLine (CS, "lda sp");
-               AddCodeSegLine (CS, "ldx sp+1");
+               AddCodeLine ("lda sp");
+               AddCodeLine ("ldx sp+1");
     } else {
                if (CodeSizeFactor < 300) {
                    ldaconst (offs);                    /* Load A with offset value */
-                   AddCodeSegLine (CS, "jsr leaasp");  /* Load effective address */
+                   AddCodeLine ("jsr leaasp"); /* Load effective address */
                } else {
+           unsigned L = GetLocalLabel ();
                    if (CPU == CPU_65C02 && offs == 1) {
-                       AddCodeSegLine (CS, "lda sp");
-                       AddCodeSegLine (CS, "ldx sp+1");
-                       AddCodeSegLine (CS, "ina");
-                       AddCodeSegLine (CS, "bne *+3");
-                       AddCodeSegLine (CS, "inx");
-                       AddCodeHint ("x:!");            /* Invalidate X */
+                       AddCodeLine ("lda sp");
+                       AddCodeLine ("ldx sp+1");
+                       AddCodeLine ("ina");
+                       AddCodeLine ("bne %s", LocalLabelName (L));
+                       AddCodeLine ("inx");
                    } else {
                        ldaconst (offs);
-                       AddCodeSegLine (CS, "clc");
-                       AddCodeSegLine (CS, "ldx sp+1");
-                       AddCodeSegLine (CS, "adc sp");
-                       AddCodeSegLine (CS, "bcc *+3");
-                       AddCodeSegLine (CS, "inx");
-                       AddCodeHint ("x:!");            /* Invalidate X */
+                       AddCodeLine ("clc");
+                       AddCodeLine ("ldx sp+1");
+                       AddCodeLine ("adc sp");
+                       AddCodeLine ("bcc %s", LocalLabelName (L));
+                       AddCodeLine ("inx");
                    }
+           g_defcodelabel (L);
                }
     }
 }
@@ -986,23 +877,20 @@ void g_leavariadic (int Offs)
     CheckLocalOffs (ArgSizeOffs);
 
     /* Get the size of all parameters. */
-    if (ArgSizeOffs == 0 && CPU == CPU_65C02) {
-               AddCodeSegLine (CS, "lda (sp)");
-    } else {
-               ldyconst (ArgSizeOffs);
-               AddCodeSegLine (CS, "lda (sp),y");
-    }
+    ldyconst (ArgSizeOffs);
+    AddCodeLine ("lda (sp),y");
 
     /* Add the value of the stackpointer */
     if (CodeSizeFactor > 250) {
-               AddCodeSegLine (CS, "ldx sp+1");
-               AddCodeSegLine (CS, "clc");
-               AddCodeSegLine (CS, "adc sp");
-               AddCodeSegLine (CS, "bcc *+3");
-               AddCodeSegLine (CS, "inx");
-               AddCodeHint ("x:!");            /* Invalidate X */
+       unsigned L = GetLocalLabel();
+               AddCodeLine ("ldx sp+1");
+               AddCodeLine ("clc");
+               AddCodeLine ("adc sp");
+               AddCodeLine ("bcc %s", LocalLabelName (L));
+               AddCodeLine ("inx");
+       g_defcodelabel (L);
     } else {
-               AddCodeSegLine (CS, "jsr leaasp");
+               AddCodeLine ("jsr leaasp");
     }
 
     /* Add the offset to the primary */
@@ -1021,31 +909,31 @@ 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) {
 
        case CF_CHAR:
-           AddCodeSegLine (CS, "sta %s", lbuf);
+           AddCodeLine ("sta %s", lbuf);
            break;
 
        case CF_INT:
-           AddCodeSegLine (CS, "sta %s", lbuf);
-           AddCodeSegLine (CS, "stx %s+1", lbuf);
+           AddCodeLine ("sta %s", lbuf);
+           AddCodeLine ("stx %s+1", lbuf);
            break;
 
        case CF_LONG:
-           AddCodeSegLine (CS, "sta %s", lbuf);
-           AddCodeSegLine (CS, "stx %s+1", lbuf);
-           AddCodeSegLine (CS, "ldy sreg");
-           AddCodeSegLine (CS, "sty %s+2", lbuf);
-           AddCodeSegLine (CS, "ldy sreg+1");
-           AddCodeSegLine (CS, "sty %s+3", lbuf);
+           AddCodeLine ("sta %s", lbuf);
+           AddCodeLine ("stx %s+1", lbuf);
+           AddCodeLine ("ldy sreg");
+           AddCodeLine ("sty %s+2", lbuf);
+           AddCodeLine ("ldy sreg+1");
+           AddCodeLine ("sty %s+3", lbuf);
            break;
 
                default:
@@ -1065,60 +953,40 @@ void g_putlocal (unsigned Flags, int Offs, long Val)
 
        case CF_CHAR:
            if (Flags & CF_CONST) {
-               AddCodeSegLine (CS, "lda #$%02X", (unsigned char) Val);
+               AddCodeLine ("lda #$%02X", (unsigned char) Val);
            }
-           if (CPU == CPU_65C02 && Offs == 0) {
-               AddCodeSegLine (CS, "sta (sp)");
-           } else {
-               ldyconst (Offs);
-               AddCodeSegLine (CS, "sta (sp),y");
-           }
+           ldyconst (Offs);
+           AddCodeLine ("sta (sp),y");
            break;
 
        case CF_INT:
            if (Flags & CF_CONST) {
                ldyconst (Offs+1);
-               AddCodeSegLine (CS, "lda #$%02X", (unsigned char) (Val >> 8));
-               AddCodeSegLine (CS, "sta (sp),y");
+               AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
+               AddCodeLine ("sta (sp),y");
                if ((Flags & CF_NOKEEP) == 0) {
                    /* Place high byte into X */
-                   AddCodeSegLine (CS, "tax");
+                   AddCodeLine ("tax");
                }
-               if (CPU == CPU_65C02 && Offs == 0) {
-                   AddCodeSegLine (CS, "lda #$%02X", (unsigned char) Val);
-                   AddCodeSegLine (CS, "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 */
-                       AddCodeSegLine (CS, "tya");
-                       AddCodeSegLine (CS, "dey");
-                   } else {
-                       AddCodeSegLine (CS, "dey");
-                       AddCodeSegLine (CS, "lda #$%02X", (unsigned char) Val);
-                   }
-                   AddCodeSegLine (CS, "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);
-                       AddCodeSegLine (CS, "jsr staxysp");
-                   } else {
-                       AddCodeSegLine (CS, "jsr stax0sp");
-                   }
+                   ldyconst (Offs);
+                   AddCodeLine ("jsr staxysp");
                } else {
-                   if (CPU == CPU_65C02 && Offs == 0) {
-                       AddCodeSegLine (CS, "sta (sp)");
-                       ldyconst (1);
-                       AddCodeSegLine (CS, "txa");
-                       AddCodeSegLine (CS, "sta (sp),y");
-                   } else {
-                       ldyconst (Offs);
-                       AddCodeSegLine (CS, "sta (sp),y");
-                       AddCodeSegLine (CS, "iny");
-                       AddCodeSegLine (CS, "txa");
-                       AddCodeSegLine (CS, "sta (sp),y");
-                   }
+                   ldyconst (Offs);
+                   AddCodeLine ("sta (sp),y");
+                   AddCodeLine ("iny");
+                   AddCodeLine ("txa");
+                   AddCodeLine ("sta (sp),y");
                }
            }
            break;
@@ -1127,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);
-               AddCodeSegLine (CS, "jsr steaxysp");
-           } else {
-               AddCodeSegLine (CS, "jsr steax0sp");
-           }
+           ldyconst (Offs);
+           AddCodeLine ("jsr steaxysp");
            break;
 
                default:
@@ -1156,17 +1020,17 @@ void g_putind (unsigned Flags, unsigned Offs)
     if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
 
        /* Overflow - we need to add the low byte also */
-       AddCodeSegLine (CS, "ldy #$00");
-       AddCodeSegLine (CS, "clc");
-       AddCodeSegLine (CS, "pha");
-       AddCodeSegLine (CS, "lda #$%02X", Offs & 0xFF);
-       AddCodeSegLine (CS, "adc (sp),y");
-       AddCodeSegLine (CS, "sta (sp),y");
-       AddCodeSegLine (CS, "iny");
-               AddCodeSegLine (CS, "lda #$%02X", (Offs >> 8) & 0xFF);
-       AddCodeSegLine (CS, "adc (sp),y");
-       AddCodeSegLine (CS, "sta (sp),y");
-       AddCodeSegLine (CS, "pla");
+       AddCodeLine ("ldy #$00");
+       AddCodeLine ("clc");
+       AddCodeLine ("pha");
+       AddCodeLine ("lda #$%02X", Offs & 0xFF);
+       AddCodeLine ("adc (sp),y");
+       AddCodeLine ("sta (sp),y");
+       AddCodeLine ("iny");
+               AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
+       AddCodeLine ("adc (sp),y");
+       AddCodeLine ("sta (sp),y");
+       AddCodeLine ("pla");
 
        /* Complete address is on stack, new offset is zero */
        Offs = 0;
@@ -1174,13 +1038,13 @@ void g_putind (unsigned Flags, unsigned Offs)
     } else if ((Offs & 0xFF00) != 0) {
 
        /* We can just add the high byte */
-       AddCodeSegLine (CS, "ldy #$01");
-       AddCodeSegLine (CS, "clc");
-       AddCodeSegLine (CS, "pha");
-       AddCodeSegLine (CS, "lda #$%02X", (Offs >> 8) & 0xFF);
-       AddCodeSegLine (CS, "adc (sp),y");
-       AddCodeSegLine (CS, "sta (sp),y");
-       AddCodeSegLine (CS, "pla");
+       AddCodeLine ("ldy #$01");
+       AddCodeLine ("clc");
+       AddCodeLine ("pha");
+       AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
+       AddCodeLine ("adc (sp),y");
+       AddCodeLine ("sta (sp),y");
+       AddCodeLine ("pla");
 
        /* Offset is now just the low byte */
        Offs &= 0x00FF;
@@ -1190,30 +1054,18 @@ void g_putind (unsigned Flags, unsigned Offs)
     switch (Flags & CF_TYPE) {
 
        case CF_CHAR:
-           if (Offs) {
-               ldyconst (Offs);
-               AddCodeSegLine (CS, "jsr staspidx");
-           } else {
-               AddCodeSegLine (CS, "jsr staspp");
-           }
+           ldyconst (Offs);
+           AddCodeLine ("jsr staspidx");
            break;
 
        case CF_INT:
-           if (Offs) {
-               ldyconst (Offs);
-               AddCodeSegLine (CS, "jsr staxspidx");
-           } else {
-               AddCodeSegLine (CS, "jsr staxspp");
-           }
+           ldyconst (Offs);
+           AddCodeLine ("jsr staxspidx");
            break;
 
        case CF_LONG:
-           if (Offs) {
-               ldyconst (Offs);
-               AddCodeSegLine (CS, "jsr steaxspidx");
-           } else {
-               AddCodeSegLine (CS, "jsr steaxspp");
-           }
+           ldyconst (Offs);
+           AddCodeLine ("jsr steaxspidx");
            break;
 
        default:
@@ -1241,9 +1093,9 @@ void g_toslong (unsigned flags)
        case CF_CHAR:
        case CF_INT:
            if (flags & CF_UNSIGNED) {
-               AddCodeSegLine (CS, "jsr tosulong");
+               AddCodeLine ("jsr tosulong");
            } else {
-               AddCodeSegLine (CS, "jsr toslong");
+               AddCodeLine ("jsr toslong");
            }
            push (CF_INT);
            break;
@@ -1268,7 +1120,7 @@ void g_tosint (unsigned flags)
            break;
 
        case CF_LONG:
-           AddCodeSegLine (CS, "jsr tosint");
+           AddCodeLine ("jsr tosint");
            pop (CF_INT);
            break;
 
@@ -1289,13 +1141,13 @@ void g_reglong (unsigned flags)
            if (flags & CF_UNSIGNED) {
                if (CodeSizeFactor >= 200) {
                    ldyconst (0);
-                   AddCodeSegLine (CS, "sty sreg");
-                   AddCodeSegLine (CS, "sty sreg+1");
+                   AddCodeLine ("sty sreg");
+                   AddCodeLine ("sty sreg+1");
                } else {
-                   AddCodeSegLine (CS, "jsr axulong");
+                   AddCodeLine ("jsr axulong");
                }
            } else {
-               AddCodeSegLine (CS, "jsr axlong");
+               AddCodeLine ("jsr axlong");
            }
            break;
 
@@ -1408,42 +1260,42 @@ 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:
                    if (flags & CF_FORCECHAR) {
                        while (p2--) {
-                           AddCodeSegLine (CS, "asl a");
+                           AddCodeLine ("asl a");
                        }
                        break;
                    }
                    /* FALLTHROUGH */
 
                case CF_INT:
-                   if (CodeSizeFactor >= (p2+1)*130U) {
-                       AddCodeSegLine (CS, "stx tmp1");
+                   if (CodeSizeFactor >= (p2+1)*130U) {
+                       AddCodeLine ("stx tmp1");
                        while (p2--) {
-                           AddCodeSegLine (CS, "asl a");
-                           AddCodeSegLine (CS, "rol tmp1");
+                           AddCodeLine ("asl a");
+                           AddCodeLine ("rol tmp1");
                        }
-                       AddCodeSegLine (CS, "ldx tmp1");
+                       AddCodeLine ("ldx tmp1");
                    } else {
                        if (flags & CF_UNSIGNED) {
-                           AddCodeSegLine (CS, "jsr shlax%d", p2);
+                           AddCodeLine ("jsr shlax%d", p2);
                        } else {
-                           AddCodeSegLine (CS, "jsr aslax%d", p2);
+                           AddCodeLine ("jsr aslax%d", p2);
                        }
                    }
                    break;
 
                case CF_LONG:
                    if (flags & CF_UNSIGNED) {
-                       AddCodeSegLine (CS, "jsr shleax%d", p2);
+                       AddCodeLine ("jsr shleax%d", p2);
                    } else {
-                       AddCodeSegLine (CS, "jsr asleax%d", p2);
+                       AddCodeLine ("jsr asleax%d", p2);
                    }
                    break;
 
@@ -1463,21 +1315,21 @@ 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:
                    if (flags & CF_FORCECHAR) {
                        if (flags & CF_UNSIGNED) {
                            while (p2--) {
-                               AddCodeSegLine (CS, "lsr a");
+                               AddCodeLine ("lsr a");
                            }
                            break;
                        } else if (p2 <= 2) {
-                           AddCodeSegLine (CS, "cmp #$80");
-                           AddCodeSegLine (CS, "ror a");
+                           AddCodeLine ("cmp #$80");
+                           AddCodeLine ("ror a");
                            break;
                        }
                    }
@@ -1486,35 +1338,35 @@ void g_scale (unsigned flags, long val)
                case CF_INT:
                    if (flags & CF_UNSIGNED) {
                        if (CodeSizeFactor >= (p2+1)*130U) {
-                           AddCodeSegLine (CS, "stx tmp1");
+                           AddCodeLine ("stx tmp1");
                            while (p2--) {
-                               AddCodeSegLine (CS, "lsr tmp1");
-                               AddCodeSegLine (CS, "ror a");
+                               AddCodeLine ("lsr tmp1");
+                               AddCodeLine ("ror a");
                            }
-                           AddCodeSegLine (CS, "ldx tmp1");
+                           AddCodeLine ("ldx tmp1");
                        } else {
-                           AddCodeSegLine (CS, "jsr lsrax%d", p2);
+                           AddCodeLine ("jsr lsrax%d", p2);
                        }
                    } else {
                        if (CodeSizeFactor >= (p2+1)*150U) {
-                           AddCodeSegLine (CS, "stx tmp1");
+                           AddCodeLine ("stx tmp1");
                            while (p2--) {
-                               AddCodeSegLine (CS, "cpx #$80");
-                               AddCodeSegLine (CS, "ror tmp1");
-                               AddCodeSegLine (CS, "ror a");
+                               AddCodeLine ("cpx #$80");
+                               AddCodeLine ("ror tmp1");
+                               AddCodeLine ("ror a");
                            }
-                           AddCodeSegLine (CS, "ldx tmp1");
+                           AddCodeLine ("ldx tmp1");
                        } else {
-                           AddCodeSegLine (CS, "jsr asrax%d", p2);
+                           AddCodeLine ("jsr asrax%d", p2);
                        }
                    }
                    break;
 
                case CF_LONG:
                    if (flags & CF_UNSIGNED) {
-                       AddCodeSegLine (CS, "jsr lsreax%d", p2);
+                       AddCodeLine ("jsr lsreax%d", p2);
                    } else {
-                       AddCodeSegLine (CS, "jsr asreax%d", p2);
+                       AddCodeLine ("jsr asreax%d", p2);
                    }
                    break;
 
@@ -1543,6 +1395,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);
@@ -1550,24 +1404,25 @@ void g_addlocal (unsigned flags, int offs)
     switch (flags & CF_TYPE) {
 
        case CF_CHAR:
-           AddCodeSegLine (CS, "ldy #$%02X", offs & 0xFF);
-           AddCodeSegLine (CS, "clc");
-           AddCodeSegLine (CS, "adc (sp),y");
-           AddCodeSegLine (CS, "bcc *+3");
-           AddCodeSegLine (CS, "inx");
-           AddCodeHint ("x:!");
+           L = GetLocalLabel();
+           AddCodeLine ("ldy #$%02X", offs & 0xFF);
+           AddCodeLine ("clc");
+           AddCodeLine ("adc (sp),y");
+           AddCodeLine ("bcc %s", LocalLabelName (L));
+           AddCodeLine ("inx");
+           g_defcodelabel (L);
            break;
 
        case CF_INT:
-           AddCodeSegLine (CS, "ldy #$%02X", offs & 0xFF);
-           AddCodeSegLine (CS, "clc");
-           AddCodeSegLine (CS, "adc (sp),y");
-           AddCodeSegLine (CS, "pha");
-           AddCodeSegLine (CS, "txa");
-           AddCodeSegLine (CS, "iny");
-           AddCodeSegLine (CS, "adc (sp),y");
-           AddCodeSegLine (CS, "tax");
-           AddCodeSegLine (CS, "pla");
+           AddCodeLine ("ldy #$%02X", offs & 0xFF);
+           AddCodeLine ("clc");
+           AddCodeLine ("adc (sp),y");
+           AddCodeLine ("pha");
+           AddCodeLine ("txa");
+           AddCodeLine ("iny");
+           AddCodeLine ("adc (sp),y");
+           AddCodeLine ("tax");
+           AddCodeLine ("pla");
            break;
 
        case CF_LONG:
@@ -1585,30 +1440,33 @@ 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:
-           AddCodeSegLine (CS, "clc");
-           AddCodeSegLine (CS, "adc %s", lbuf);
-           AddCodeSegLine (CS, "bcc *+3");
-           AddCodeSegLine (CS, "inx");
-           AddCodeHint ("x:!");
+           L = GetLocalLabel();
+           AddCodeLine ("clc");
+           AddCodeLine ("adc %s", lbuf);
+           AddCodeLine ("bcc %s", LocalLabelName (L));
+           AddCodeLine ("inx");
+           g_defcodelabel (L);
            break;
 
        case CF_INT:
-           AddCodeSegLine (CS, "clc");
-           AddCodeSegLine (CS, "adc %s", lbuf);
-           AddCodeSegLine (CS, "tay");
-           AddCodeSegLine (CS, "txa");
-           AddCodeSegLine (CS, "adc %s+1", lbuf);
-           AddCodeSegLine (CS, "tax");
-           AddCodeSegLine (CS, "tya");
+           AddCodeLine ("clc");
+           AddCodeLine ("adc %s", lbuf);
+           AddCodeLine ("tay");
+           AddCodeLine ("txa");
+           AddCodeLine ("adc %s+1", lbuf);
+           AddCodeLine ("tax");
+           AddCodeLine ("tya");
            break;
 
        case CF_LONG:
@@ -1626,66 +1484,45 @@ 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) {
 
                case CF_CHAR:
                    if (flags & CF_FORCECHAR) {
-               AddCodeSegLine (CS, "ldx #$00");
+               AddCodeLine ("ldx #$00");
                        if (flags & CF_CONST) {
                    if (val == 1) {
-                       AddCodeSegLine (CS, "inc %s", lbuf);
-                       AddCodeSegLine (CS, "lda %s", lbuf);
+                       AddCodeLine ("inc %s", lbuf);
+                       AddCodeLine ("lda %s", lbuf);
                    } else {
-                               AddCodeSegLine (CS, "lda #$%02X", (int)(val & 0xFF));
-                       AddCodeSegLine (CS, "clc");
-                       AddCodeSegLine (CS, "adc %s", lbuf);
-                       AddCodeSegLine (CS, "sta %s", lbuf);
+                               AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
+                       AddCodeLine ("clc");
+                       AddCodeLine ("adc %s", lbuf);
+                       AddCodeLine ("sta %s", lbuf);
                    }
                        } else {
-                   AddCodeSegLine (CS, "clc");
-                           AddCodeSegLine (CS, "adc %s", lbuf);
-                   AddCodeSegLine (CS, "sta %s", lbuf);
+                   AddCodeLine ("clc");
+                           AddCodeLine ("adc %s", lbuf);
+                   AddCodeLine ("sta %s", lbuf);
                        }
                if ((flags & CF_UNSIGNED) == 0) {
-                   AddCodeSegLine (CS, "bpl *+3");
-                   AddCodeSegLine (CS, "dex");
-                   AddCodeHint ("x:!");                /* Invalidate X */
+                   unsigned L = GetLocalLabel();
+                   AddCodeLine ("bpl %s", LocalLabelName (L));
+                   AddCodeLine ("dex");
+                   g_defcodelabel (L);
                }
                        break;
                    }
@@ -1695,54 +1532,54 @@ void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
                    if (flags & CF_CONST) {
                if (val == 1) {
                    unsigned L = GetLocalLabel ();
-                   AddCodeSegLine (CS, "inc %s", lbuf);
-                   AddCodeSegLine (CS, "bne %s", LocalLabelName (L));
-                   AddCodeSegLine (CS, "inc %s+1", lbuf);
+                   AddCodeLine ("inc %s", lbuf);
+                   AddCodeLine ("bne %s", LocalLabelName (L));
+                   AddCodeLine ("inc %s+1", lbuf);
                    g_defcodelabel (L);
-                   AddCodeSegLine (CS, "lda %s", lbuf);                /* Hmmm... */
-                   AddCodeSegLine (CS, "ldx %s+1", lbuf);
+                   AddCodeLine ("lda %s", lbuf);               /* Hmmm... */
+                   AddCodeLine ("ldx %s+1", lbuf);
                } else {
-                           AddCodeSegLine (CS, "lda #$%02X", (int)(val & 0xFF));
-                   AddCodeSegLine (CS, "clc");
-                   AddCodeSegLine (CS, "adc %s", lbuf);
-                   AddCodeSegLine (CS, "sta %s", lbuf);
+                           AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
+                   AddCodeLine ("clc");
+                   AddCodeLine ("adc %s", lbuf);
+                   AddCodeLine ("sta %s", lbuf);
                    if (val < 0x100) {
                        unsigned L = GetLocalLabel ();
-                       AddCodeSegLine (CS, "bcc %s", LocalLabelName (L));
-                       AddCodeSegLine (CS, "inc %s+1", lbuf);
+                       AddCodeLine ("bcc %s", LocalLabelName (L));
+                       AddCodeLine ("inc %s+1", lbuf);
                                g_defcodelabel (L);
-                       AddCodeSegLine (CS, "ldx %s+1", lbuf);
+                       AddCodeLine ("ldx %s+1", lbuf);
                    } else {
-                               AddCodeSegLine (CS, "lda #$%02X", (unsigned char)(val >> 8));
-                       AddCodeSegLine (CS, "adc %s+1", lbuf);
-                       AddCodeSegLine (CS, "sta %s+1", lbuf);
-                       AddCodeSegLine (CS, "tax");
-                       AddCodeSegLine (CS, "lda %s", lbuf);
+                               AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
+                       AddCodeLine ("adc %s+1", lbuf);
+                       AddCodeLine ("sta %s+1", lbuf);
+                       AddCodeLine ("tax");
+                       AddCodeLine ("lda %s", lbuf);
                    }
                }
                    } else {
-               AddCodeSegLine (CS, "clc");
-                       AddCodeSegLine (CS, "adc %s", lbuf);
-                       AddCodeSegLine (CS, "sta %s", lbuf);
-                       AddCodeSegLine (CS, "txa");
-                       AddCodeSegLine (CS, "adc %s+1", lbuf);
-                       AddCodeSegLine (CS, "sta %s+1", lbuf);
-                       AddCodeSegLine (CS, "tax");
-               AddCodeSegLine (CS, "lda %s", lbuf);
+               AddCodeLine ("clc");
+                       AddCodeLine ("adc %s", lbuf);
+                       AddCodeLine ("sta %s", lbuf);
+                       AddCodeLine ("txa");
+                       AddCodeLine ("adc %s+1", lbuf);
+                       AddCodeLine ("sta %s+1", lbuf);
+                       AddCodeLine ("tax");
+               AddCodeLine ("lda %s", lbuf);
            }
                    break;
 
                case CF_LONG:
            if (flags & CF_CONST) {
                if (val < 0x100) {
-                   AddCodeSegLine (CS, "ldy #<(%s)", lbuf);
-                   AddCodeSegLine (CS, "sty ptr1");
-                   AddCodeSegLine (CS, "ldy #>(%s+1)", lbuf);
+                   AddCodeLine ("ldy #<(%s)", lbuf);
+                   AddCodeLine ("sty ptr1");
+                   AddCodeLine ("ldy #>(%s)", lbuf);
                    if (val == 1) {
-                       AddCodeSegLine (CS, "jsr laddeq1");
+                       AddCodeLine ("jsr laddeq1");
                    } else {
-                       AddCodeSegLine (CS, "lda #$%02X", (int)(val & 0xFF));
-                       AddCodeSegLine (CS, "jsr laddeqa");
+                       AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
+                       AddCodeLine ("jsr laddeqa");
                    }
                } else {
                    g_getstatic (flags, label, offs);
@@ -1750,10 +1587,10 @@ void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
                    g_putstatic (flags, label, offs);
                }
            } else {
-               AddCodeSegLine (CS, "ldy #<(%s)", lbuf);
-               AddCodeSegLine (CS, "sty ptr1");
-               AddCodeSegLine (CS, "ldy #>(%s+1)", lbuf);
-               AddCodeSegLine (CS, "jsr laddeq");
+               AddCodeLine ("ldy #<(%s)", lbuf);
+               AddCodeLine ("sty ptr1");
+               AddCodeLine ("ldy #>(%s)", lbuf);
+               AddCodeLine ("jsr laddeq");
            }
                    break;
 
@@ -1776,63 +1613,58 @@ void g_addeqlocal (unsigned flags, int offs, unsigned long val)
 
                case CF_CHAR:
                    if (flags & CF_FORCECHAR) {
-                       if (offs == 0) {
-                           AddCodeSegLine (CS, "ldx #$00");
-                           if (flags & CF_CONST) {
-                               AddCodeSegLine (CS, "clc");
-                               AddCodeSegLine (CS, "lda #$%02X", (int)(val & 0xFF));
-                               AddCodeSegLine (CS, "adc (sp,x)");
-                               AddCodeSegLine (CS, "sta (sp,x)");
-                           } else {
-                               AddCodeSegLine (CS, "clc");
-                               AddCodeSegLine (CS, "adc (sp,x)");
-                               AddCodeSegLine (CS, "sta (sp,x)");
-                           }
-                       } else {
-                           ldyconst (offs);
-                   AddCodeSegLine (CS, "ldx #$00");
-                   if (flags & CF_CONST) {
-                       AddCodeSegLine (CS, "clc");
-                               AddCodeSegLine (CS, "lda #$%02X", (int)(val & 0xFF));
-                       AddCodeSegLine (CS, "adc (sp),y");
-                       AddCodeSegLine (CS, "sta (sp),y");
-                   } else {
-                       AddCodeSegLine (CS, "clc");
-                       AddCodeSegLine (CS, "adc (sp),y");
-                       AddCodeSegLine (CS, "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) {
-                   AddCodeSegLine (CS, "bpl *+3");
-                   AddCodeSegLine (CS, "dex");
-                   AddCodeHint ("x:!");        /* Invalidate X */
+                   unsigned L = GetLocalLabel();
+                   AddCodeLine ("bpl %s", LocalLabelName (L));
+                   AddCodeLine ("dex");
+                   g_defcodelabel (L);
                }
                        break;
                    }
                    /* FALLTHROUGH */
 
                case CF_INT:
+           ldyconst (offs);
            if (flags & CF_CONST) {
-               g_getimmed (flags, val, 0);
-           }
-           if (offs == 0) {
-               AddCodeSegLine (CS, "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);
-               AddCodeSegLine (CS, "jsr addeqysp");
-           }
+               AddCodeLine ("jsr addeqysp");
+           }
                    break;
 
                case CF_LONG:
            if (flags & CF_CONST) {
                g_getimmed (flags, val, 0);
            }
-           if (offs == 0) {
-               AddCodeSegLine (CS, "jsr laddeq0sp");
-           } else {
-               ldyconst (offs);
-               AddCodeSegLine (CS, "jsr laddeqysp");
-           }
+           ldyconst (offs);
+           AddCodeLine ("jsr laddeqysp");
                    break;
 
                default:
@@ -1855,48 +1687,40 @@ void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
     switch (flags & CF_TYPE) {
 
                case CF_CHAR:
-           AddCodeSegLine (CS, "sta ptr1");
-           AddCodeSegLine (CS, "stx ptr1+1");
-           if (offs == 0) {
-               AddCodeSegLine (CS, "ldx #$00");
-               AddCodeSegLine (CS, "lda #$%02X", (int)(val & 0xFF));
-               AddCodeSegLine (CS, "clc");
-               AddCodeSegLine (CS, "adc (ptr1,x)");
-               AddCodeSegLine (CS, "sta (ptr1,x)");
-           } else {
-               AddCodeSegLine (CS, "ldy #$%02X", offs);
-                       AddCodeSegLine (CS, "ldx #$00");
-                       AddCodeSegLine (CS, "lda #$%02X", (int)(val & 0xFF));
-                       AddCodeSegLine (CS, "clc");
-                       AddCodeSegLine (CS, "adc (ptr1),y");
-                       AddCodeSegLine (CS, "sta (ptr1),y");
-           }
+           AddCodeLine ("sta ptr1");
+           AddCodeLine ("stx ptr1+1");
+           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:
            if (CodeSizeFactor >= 200) {
                /* Lots of code, use only if size is not important */
-                       AddCodeSegLine (CS, "sta ptr1");
-               AddCodeSegLine (CS, "stx ptr1+1");
-               AddCodeSegLine (CS, "ldy #$%02X", offs);
-               AddCodeSegLine (CS, "lda #$%02X", (int)(val & 0xFF));
-               AddCodeSegLine (CS, "clc");
-               AddCodeSegLine (CS, "adc (ptr1),y");
-               AddCodeSegLine (CS, "sta (ptr1),y");
-               AddCodeSegLine (CS, "pha");
-               AddCodeSegLine (CS, "iny");
-               AddCodeSegLine (CS, "lda #$%02X", (unsigned char)(val >> 8));
-               AddCodeSegLine (CS, "adc (ptr1),y");
-               AddCodeSegLine (CS, "sta (ptr1),y");
-               AddCodeSegLine (CS, "tax");
-               AddCodeSegLine (CS, "pla");
+                       AddCodeLine ("sta ptr1");
+               AddCodeLine ("stx ptr1+1");
+               AddCodeLine ("ldy #$%02X", offs);
+               AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
+               AddCodeLine ("clc");
+               AddCodeLine ("adc (ptr1),y");
+               AddCodeLine ("sta (ptr1),y");
+               AddCodeLine ("pha");
+               AddCodeLine ("iny");
+               AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
+               AddCodeLine ("adc (ptr1),y");
+               AddCodeLine ("sta (ptr1),y");
+               AddCodeLine ("tax");
+               AddCodeLine ("pla");
                break;
            }
            /* FALL THROUGH */
 
                case CF_LONG:
-                   AddCodeSegLine (CS, "jsr pushax");          /* Push the address */
-           push (flags);                       /* Correct the internal sp */
+                   AddCodeLine ("jsr pushax");         /* Push the address */
+                   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 */
@@ -1909,100 +1733,95 @@ 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) {
 
                case CF_CHAR:
                    if (flags & CF_FORCECHAR) {
-                       AddCodeSegLine (CS, "ldx #$00");
+                       AddCodeLine ("ldx #$00");
                        if (flags & CF_CONST) {
                            if (val == 1) {
-                               AddCodeSegLine (CS, "dec %s", lbuf);
-                               AddCodeSegLine (CS, "lda %s", lbuf);
+                               AddCodeLine ("dec %s", lbuf);
+                               AddCodeLine ("lda %s", lbuf);
                            } else {
-                               AddCodeSegLine (CS, "sec");
-                               AddCodeSegLine (CS, "lda %s", lbuf);
-                               AddCodeSegLine (CS, "sbc #$%02X", (int)(val & 0xFF));
-                               AddCodeSegLine (CS, "sta %s", lbuf);
+                               AddCodeLine ("sec");
+                               AddCodeLine ("lda %s", lbuf);
+                               AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
+                               AddCodeLine ("sta %s", lbuf);
                            }
                        } else {
-                           AddCodeSegLine (CS, "sec");
-                           AddCodeSegLine (CS, "sta tmp1");
-                           AddCodeSegLine (CS, "lda %s", lbuf);
-                           AddCodeSegLine (CS, "sbc tmp1");
-                           AddCodeSegLine (CS, "sta %s", lbuf);
+                   AddCodeLine ("eor #$FF");
+                           AddCodeLine ("sec");
+                           AddCodeLine ("adc %s", lbuf);
+                           AddCodeLine ("sta %s", lbuf);
                        }
                        if ((flags & CF_UNSIGNED) == 0) {
-                           AddCodeSegLine (CS, "bpl *+3");
-                           AddCodeSegLine (CS, "dex");
-                           AddCodeHint ("x:!");                /* Invalidate X */
+                   unsigned L = GetLocalLabel();
+                           AddCodeLine ("bpl %s", LocalLabelName (L));
+                           AddCodeLine ("dex");
+                   g_defcodelabel (L);
                        }
                        break;
                    }
                    /* FALLTHROUGH */
 
                case CF_INT:
-           AddCodeSegLine (CS, "sec");
+           AddCodeLine ("sec");
            if (flags & CF_CONST) {
-               AddCodeSegLine (CS, "lda %s", lbuf);
-               AddCodeSegLine (CS, "sbc #$%02X", (unsigned char)val);
-               AddCodeSegLine (CS, "sta %s", lbuf);
+               AddCodeLine ("lda %s", lbuf);
+               AddCodeLine ("sbc #$%02X", (unsigned char)val);
+               AddCodeLine ("sta %s", lbuf);
                if (val < 0x100) {
                    unsigned L = GetLocalLabel ();
-                   AddCodeSegLine (CS, "bcs %s", LocalLabelName (L));
-                   AddCodeSegLine (CS, "dec %s+1", lbuf);
+                   AddCodeLine ("bcs %s", LocalLabelName (L));
+                   AddCodeLine ("dec %s+1", lbuf);
                    g_defcodelabel (L);
-                   AddCodeSegLine (CS, "ldx %s+1", lbuf);
+                   AddCodeLine ("ldx %s+1", lbuf);
                } else {
-                   AddCodeSegLine (CS, "lda %s+1", lbuf);
-                   AddCodeSegLine (CS, "sbc #$%02X", (unsigned char)(val >> 8));
-                   AddCodeSegLine (CS, "sta %s+1", lbuf);
-                   AddCodeSegLine (CS, "tax");
-                   AddCodeSegLine (CS, "lda %s", lbuf);
+                   AddCodeLine ("lda %s+1", lbuf);
+                   AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
+                   AddCodeLine ("sta %s+1", lbuf);
+                   AddCodeLine ("tax");
+                   AddCodeLine ("lda %s", lbuf);
                }
            } else {
-               AddCodeSegLine (CS, "sta tmp1");
-               AddCodeSegLine (CS, "lda %s", lbuf);
-               AddCodeSegLine (CS, "sbc tmp1");
-               AddCodeSegLine (CS, "sta %s", lbuf);
-                       AddCodeSegLine (CS, "stx tmp1");
-               AddCodeSegLine (CS, "lda %s+1", lbuf);
-               AddCodeSegLine (CS, "sbc tmp1");
-               AddCodeSegLine (CS, "sta %s+1", lbuf);
-               AddCodeSegLine (CS, "tax");
-               AddCodeSegLine (CS, "lda %s", lbuf);
+               AddCodeLine ("eor #$FF");
+                       AddCodeLine ("adc %s", lbuf);
+               AddCodeLine ("sta %s", lbuf);
+               AddCodeLine ("txa");
+               AddCodeLine ("eor #$FF");
+                       AddCodeLine ("adc %s+1", lbuf);
+               AddCodeLine ("sta %s+1", lbuf);
+               AddCodeLine ("tax");
+               AddCodeLine ("lda %s", lbuf);
            }
                    break;
 
                case CF_LONG:
            if (flags & CF_CONST) {
                if (val < 0x100) {
-                   AddCodeSegLine (CS, "ldy #<(%s)", lbuf);
-                   AddCodeSegLine (CS, "sty ptr1");
-                   AddCodeSegLine (CS, "ldy #>(%s+1)", lbuf);
-                   if (val == 1) {
-                       AddCodeSegLine (CS, "jsr lsubeq1");
-                   } else {
-                       AddCodeSegLine (CS, "lda #$%02X", (unsigned char)val);
-                       AddCodeSegLine (CS, "jsr lsubeqa");
-                   }
+                   AddCodeLine ("ldy #<(%s)", lbuf);
+                   AddCodeLine ("sty ptr1");
+                   AddCodeLine ("ldy #>(%s)", lbuf);
+                   AddCodeLine ("lda #$%02X", (unsigned char)val);
+                   AddCodeLine ("jsr lsubeqa");
                } else {
                    g_getstatic (flags, label, offs);
                    g_dec (flags, val);
                    g_putstatic (flags, label, offs);
                }
            } else {
-               AddCodeSegLine (CS, "ldy #<(%s)", lbuf);
-               AddCodeSegLine (CS, "sty ptr1");
-               AddCodeSegLine (CS, "ldy #>(%s+1)", lbuf);
-               AddCodeSegLine (CS, "jsr lsubeq");
+               AddCodeLine ("ldy #<(%s)", lbuf);
+               AddCodeLine ("sty ptr1");
+               AddCodeLine ("ldy #>(%s)", lbuf);
+               AddCodeLine ("jsr lsubeq");
                    }
                    break;
 
@@ -2026,23 +1845,23 @@ void g_subeqlocal (unsigned flags, int offs, unsigned long val)
                case CF_CHAR:
                    if (flags & CF_FORCECHAR) {
                ldyconst (offs);
-               AddCodeSegLine (CS, "ldx #$00");
-                       AddCodeSegLine (CS, "sec");
+               AddCodeLine ("ldx #$00");
+                       AddCodeLine ("sec");
                if (flags & CF_CONST) {
-                   AddCodeSegLine (CS, "lda (sp),y");
-                   AddCodeSegLine (CS, "sbc #$%02X", (unsigned char)val);
+                   AddCodeLine ("lda (sp),y");
+                   AddCodeLine ("sbc #$%02X", (unsigned char)val);
                } else {
-                   AddCodeSegLine (CS, "sta tmp1");
-                   AddCodeSegLine (CS, "lda (sp),y");
-                   AddCodeSegLine (CS, "sbc tmp1");
+                   AddCodeLine ("eor #$FF");
+                   AddCodeLine ("adc (sp),y");
                }
-                       AddCodeSegLine (CS, "sta (sp),y");
+                       AddCodeLine ("sta (sp),y");
                if ((flags & CF_UNSIGNED) == 0) {
-                   AddCodeSegLine (CS, "bpl *+3");
-                   AddCodeSegLine (CS, "dex");
-                   AddCodeHint ("x:!");                /* Invalidate X */
-               }
-                       break;
+                   unsigned L = GetLocalLabel();
+                   AddCodeLine ("bpl %s", LocalLabelName (L));
+                   AddCodeLine ("dex");
+                   g_defcodelabel (L);
+               }
+                       break;
                    }
                    /* FALLTHROUGH */
 
@@ -2050,24 +1869,16 @@ void g_subeqlocal (unsigned flags, int offs, unsigned long val)
            if (flags & CF_CONST) {
                g_getimmed (flags, val, 0);
            }
-           if (offs == 0) {
-               AddCodeSegLine (CS, "jsr subeq0sp");
-           } else {
-               ldyconst (offs);
-               AddCodeSegLine (CS, "jsr subeqysp");
-           }
+           ldyconst (offs);
+           AddCodeLine ("jsr subeqysp");
                    break;
 
                case CF_LONG:
            if (flags & CF_CONST) {
                g_getimmed (flags, val, 0);
            }
-           if (offs == 0) {
-               AddCodeSegLine (CS, "jsr lsubeq0sp");
-           } else {
-               ldyconst (offs);
-               AddCodeSegLine (CS, "jsr lsubeqysp");
-           }
+           ldyconst (offs);
+           AddCodeLine ("jsr lsubeqysp");
                    break;
 
                default:
@@ -2090,48 +1901,40 @@ void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
     switch (flags & CF_TYPE) {
 
                case CF_CHAR:
-           AddCodeSegLine (CS, "sta ptr1");
-           AddCodeSegLine (CS, "stx ptr1+1");
-           if (offs == 0) {
-               AddCodeSegLine (CS, "ldx #$00");
-               AddCodeSegLine (CS, "lda (ptr1,x)");
-                       AddCodeSegLine (CS, "sec");
-               AddCodeSegLine (CS, "sbc #$%02X", (unsigned char)val);
-               AddCodeSegLine (CS, "sta (ptr1,x)");
-           } else {
-                       AddCodeSegLine (CS, "ldy #$%02X", offs);
-               AddCodeSegLine (CS, "ldx #$00");
-               AddCodeSegLine (CS, "lda (ptr1),y");
-               AddCodeSegLine (CS, "sec");
-               AddCodeSegLine (CS, "sbc #$%02X", (unsigned char)val);
-               AddCodeSegLine (CS, "sta (ptr1),y");
-           }
+           AddCodeLine ("sta ptr1");
+           AddCodeLine ("stx ptr1+1");
+           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:
            if (CodeSizeFactor >= 200) {
                /* Lots of code, use only if size is not important */
-               AddCodeSegLine (CS, "sta ptr1");
-                       AddCodeSegLine (CS, "stx ptr1+1");
-               AddCodeSegLine (CS, "ldy #$%02X", offs);
-               AddCodeSegLine (CS, "lda (ptr1),y");
-               AddCodeSegLine (CS, "sec");
-               AddCodeSegLine (CS, "sbc #$%02X", (unsigned char)val);
-               AddCodeSegLine (CS, "sta (ptr1),y");
-               AddCodeSegLine (CS, "pha");
-               AddCodeSegLine (CS, "iny");
-               AddCodeSegLine (CS, "lda (ptr1),y");
-               AddCodeSegLine (CS, "sbc #$%02X", (unsigned char)(val >> 8));
-               AddCodeSegLine (CS, "sta (ptr1),y");
-               AddCodeSegLine (CS, "tax");
-               AddCodeSegLine (CS, "pla");
+               AddCodeLine ("sta ptr1");
+                       AddCodeLine ("stx ptr1+1");
+               AddCodeLine ("ldy #$%02X", offs);
+               AddCodeLine ("lda (ptr1),y");
+               AddCodeLine ("sec");
+               AddCodeLine ("sbc #$%02X", (unsigned char)val);
+               AddCodeLine ("sta (ptr1),y");
+               AddCodeLine ("pha");
+               AddCodeLine ("iny");
+               AddCodeLine ("lda (ptr1),y");
+               AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
+               AddCodeLine ("sta (ptr1),y");
+               AddCodeLine ("tax");
+               AddCodeLine ("pla");
                break;
            }
            /* FALL THROUGH */
 
                case CF_LONG:
-                   AddCodeSegLine (CS, "jsr pushax");          /* Push the address */
-           push (flags);                       /* Correct the internal sp */
+                   AddCodeLine ("jsr pushax");         /* Push the address */
+           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 */
@@ -2150,47 +1953,54 @@ 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);
-       AddCodeSegLine (CS, "clc");
-       AddCodeSegLine (CS, "adc #$%02X", offs & 0xFF);
-               AddCodeSegLine (CS, "bcc *+4"); /* Do also skip the CLC insn below */
-       AddCodeSegLine (CS, "inx");
-       AddCodeHint ("x:!");                    /* Invalidate X */
+       AddCodeLine ("clc");
+       AddCodeLine ("adc #$%02X", offs & 0xFF);
+       /* Do also skip the CLC insn below */
+               AddCodeLine ("bcc %s", LocalLabelName (L));
+       AddCodeLine ("inx");
     }
 
     /* Add the current stackpointer value */
-    AddCodeSegLine (CS, "clc");
-    AddCodeSegLine (CS, "adc sp");
-    AddCodeSegLine (CS, "tay");
-    AddCodeSegLine (CS, "txa");
-    AddCodeSegLine (CS, "adc sp+1");
-    AddCodeSegLine (CS, "tax");
-    AddCodeSegLine (CS, "tya");
+    AddCodeLine ("clc");
+    if (L != 0) {
+       /* Label was used above */
+       g_defcodelabel (L);
+    }
+    AddCodeLine ("adc sp");
+    AddCodeLine ("tay");
+    AddCodeLine ("txa");
+    AddCodeLine ("adc sp+1");
+    AddCodeLine ("tax");
+    AddCodeLine ("tya");
 }
 
 
 
-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 */
-    AddCodeSegLine (CS, "clc");
-    AddCodeSegLine (CS, "adc #<(%s)", lbuf);
-    AddCodeSegLine (CS, "tay");
-    AddCodeSegLine (CS, "txa");
-    AddCodeSegLine (CS, "adc #>(%s)", lbuf);
-    AddCodeSegLine (CS, "tax");
-    AddCodeSegLine (CS, "tya");
+    AddCodeLine ("clc");
+    AddCodeLine ("adc #<(%s)", lbuf);
+    AddCodeLine ("tay");
+    AddCodeLine ("txa");
+    AddCodeLine ("adc #>(%s)", lbuf);
+    AddCodeLine ("tax");
+    AddCodeLine ("tya");
 }
 
 
@@ -2209,18 +2019,18 @@ void g_save (unsigned flags)
 
        case CF_CHAR:
            if (flags & CF_FORCECHAR) {
-               AddCodeSegLine (CS, "pha");
+               AddCodeLine ("pha");
                break;
            }
            /* FALLTHROUGH */
 
        case CF_INT:
-           AddCodeSegLine (CS, "sta regsave");
-           AddCodeSegLine (CS, "stx regsave+1");
+           AddCodeLine ("sta regsave");
+           AddCodeLine ("stx regsave+1");
            break;
 
        case CF_LONG:
-           AddCodeSegLine (CS, "jsr saveeax");
+           AddCodeLine ("jsr saveeax");
            break;
 
        default:
@@ -2238,18 +2048,18 @@ void g_restore (unsigned flags)
 
        case CF_CHAR:
            if (flags & CF_FORCECHAR) {
-               AddCodeSegLine (CS, "pla");
+               AddCodeLine ("pla");
                break;
            }
            /* FALLTHROUGH */
 
        case CF_INT:
-           AddCodeSegLine (CS, "lda regsave");
-           AddCodeSegLine (CS, "ldx regsave+1");
+           AddCodeLine ("lda regsave");
+           AddCodeLine ("ldx regsave+1");
            break;
 
        case CF_LONG:
-           AddCodeSegLine (CS, "jsr resteax");
+           AddCodeLine ("jsr resteax");
            break;
 
        default:
@@ -2264,20 +2074,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) {
-               AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
-               break;
+               AddCodeLine ("cmp #$%02X", (unsigned char)val);
+               break;
            }
            /* FALLTHROUGH */
 
        case CF_INT:
-           AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
-                   AddCodeSegLine (CS, "bne *+4");
-           AddCodeSegLine (CS, "cpx #$%02X", (unsigned char)(val >> 8));
+           L = GetLocalLabel();
+           AddCodeLine ("cmp #$%02X", (unsigned char)val);
+                   AddCodeLine ("bne %s", LocalLabelName (L));
+           AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
+           g_defcodelabel (L);
            break;
 
         case CF_LONG:
@@ -2326,19 +2140,19 @@ static void oper (unsigned flags, unsigned long val, char** subs)
        /* Constant value given */
        if (val == 0 && subs [offs+0]) {
            /* Special case: constant with value zero */
-           AddCodeSegLine (CS, "jsr %s", subs [offs+0]);
+           AddCodeLine ("jsr %s", subs [offs+0]);
        } else if (val < 0x100 && subs [offs+1]) {
            /* Special case: constant with high byte zero */
            ldaconst (val);             /* Load low byte */
-           AddCodeSegLine (CS, "jsr %s", subs [offs+1]);
+           AddCodeLine ("jsr %s", subs [offs+1]);
        } else {
            /* Others: arbitrary constant value */
            g_getimmed (flags, val, 0);                 /* Load value */
-           AddCodeSegLine (CS, "jsr %s", subs [offs+2]);
+           AddCodeLine ("jsr %s", subs [offs+2]);
        }
     } else {
        /* Value not constant (is already in (e)ax) */
-       AddCodeSegLine (CS, "jsr %s", subs [offs+2]);
+       AddCodeLine ("jsr %s", subs [offs+2]);
     }
 
     /* The operation will pop it's argument */
@@ -2354,21 +2168,21 @@ void g_test (unsigned flags)
 
        case CF_CHAR:
            if (flags & CF_FORCECHAR) {
-               AddCodeSegLine (CS, "tax");
+               AddCodeLine ("tax");
                break;
            }
            /* FALLTHROUGH */
 
        case CF_INT:
-           AddCodeSegLine (CS, "stx tmp1");
-           AddCodeSegLine (CS, "ora tmp1");
+           AddCodeLine ("stx tmp1");
+           AddCodeLine ("ora tmp1");
            break;
 
        case CF_LONG:
            if (flags & CF_UNSIGNED) {
-               AddCodeSegLine (CS, "jsr utsteax");
+               AddCodeLine ("jsr utsteax");
            } else {
-               AddCodeSegLine (CS, "jsr tsteax");
+               AddCodeLine ("jsr tsteax");
            }
            break;
 
@@ -2383,36 +2197,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);
-               AddCodeSegLine (CS, "jsr pusha");
-           } else {
-               AddCodeSegLine (CS, "jsr pushc%d", (int) val);
-           }
+           ldaconst (val);
+           AddCodeLine ("jsr pusha");
 
        } else {
 
            /* Handle as 16 bit value */
-           hi = (unsigned char) (val >> 8);
-           if (val <= 7) {
-               AddCodeSegLine (CS, "jsr push%u", (unsigned) val);
-           } else if (hi == 0 || hi == 0xFF) {
-               /* Use special function */
-               ldaconst (val);
-                       AddCodeSegLine (CS, "jsr %s", (hi == 0)? "pusha0" : "pushaFF");
-           } else {
-               /* Long way ... */
-               g_getimmed (flags, val, 0);
-               AddCodeSegLine (CS, "jsr pushax");
-           }
+           g_getimmed (flags, val, 0);
+           AddCodeLine ("jsr pushax");
        }
 
     } else {
@@ -2429,16 +2227,16 @@ void g_push (unsigned flags, unsigned long val)
            case CF_CHAR:
                if (flags & CF_FORCECHAR) {
                    /* Handle as char */
-                   AddCodeSegLine (CS, "jsr pusha");
+                   AddCodeLine ("jsr pusha");
                    break;
                }
                /* FALL THROUGH */
            case CF_INT:
-               AddCodeSegLine (CS, "jsr pushax");
+               AddCodeLine ("jsr pushax");
                break;
 
            case CF_LONG:
-               AddCodeSegLine (CS, "jsr pusheax");
+               AddCodeLine ("jsr pusheax");
                break;
 
            default:
@@ -2463,11 +2261,11 @@ void g_swap (unsigned flags)
 
        case CF_CHAR:
        case CF_INT:
-           AddCodeSegLine (CS, "jsr swapstk");
+           AddCodeLine ("jsr swapstk");
            break;
 
        case CF_LONG:
-           AddCodeSegLine (CS, "jsr swapestk");
+           AddCodeLine ("jsr swapestk");
            break;
 
        default:
@@ -2485,21 +2283,39 @@ void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
        /* Pass the argument count */
        ldyconst (ArgSize);
     }
-    AddCodeSegLine (CS, "jsr _%s", Label);
+    AddCodeLine ("jsr _%s", Label);
     oursp += ArgSize;          /* callee pops args */
 }
 
 
 
-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");
     }
-    AddCodeSegLine (CS, "jsr callax"); /* do the call */
-    oursp += ArgSize;                  /* callee pops args */
+
+    /* Callee pops args */
+    oursp += ArgSize;
 }
 
 
@@ -2507,70 +2323,23 @@ void g_callind (unsigned Flags, unsigned ArgSize)
 void g_jump (unsigned Label)
 /* Jump to specified internal label number */
 {
-    AddCodeSegLine (CS, "jmp %s", LocalLabelName (Label));
+    AddCodeLine ("jmp %s", LocalLabelName (Label));
 }
 
 
 
-void g_switch (unsigned Flags)
-/* Output switch statement preamble */
-{
-    switch (Flags & CF_TYPE) {
-
-       case CF_CHAR:
-       case CF_INT:
-           AddCodeSegLine (CS, "jsr switch");
-           break;
-
-       case CF_LONG:
-           AddCodeSegLine (CS, "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:
-           AddCodeSegLine (CS, ".word $%04X, %s",
-                        (unsigned)(val & 0xFFFF),
-                        LocalLabelName (label));
-                   break;
-
-       case CF_LONG:
-           AddCodeSegLine (CS, ".dword $%08lX", val);
-           AddCodeSegLine (CS, ".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 */
 {
-    AddCodeSegLine (CS, "jne %s", LocalLabelName (label));
+    AddCodeLine ("jne %s", LocalLabelName (label));
 }
 
 
 
-void g_falsejump (unsigned flags, unsigned label)
+void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
 /* Jump to label if zero flag set */
 {
-    AddCodeSegLine (CS, "jeq %s", LocalLabelName (label));
+    AddCodeLine ("jeq %s", LocalLabelName (label));
 }
 
 
@@ -2578,11 +2347,11 @@ void g_falsejump (unsigned flags, unsigned label)
 static void mod_internal (int k, char* verb1, char* verb2)
 {
     if (k <= 8) {
-       AddCodeSegLine (CS, "jsr %ssp%c", verb1, k + '0');
+       AddCodeLine ("jsr %ssp%c", verb1, k + '0');
     } else {
        CheckLocalOffs (k);
        ldyconst (k);
-       AddCodeSegLine (CS, "jsr %ssp", verb2);
+       AddCodeLine ("jsr %ssp", verb2);
     }
 }
 
@@ -2603,7 +2372,7 @@ void g_space (int space)
 void g_cstackcheck (void)
 /* Check for a C stack overflow */
 {
-    AddCodeSegLine (CS, "jsr cstkchk");
+    AddCodeLine ("jsr cstkchk");
 }
 
 
@@ -2611,7 +2380,7 @@ void g_cstackcheck (void)
 void g_stackcheck (void)
 /* Check for a stack overflow */
 {
-    AddCodeSegLine (CS, "jsr stkchk");
+    AddCodeLine ("jsr stkchk");
 }
 
 
@@ -2684,7 +2453,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
@@ -2696,50 +2465,78 @@ void g_mul (unsigned flags, unsigned long val)
 
            case CF_CHAR:
                if (flags & CF_FORCECHAR) {
-                   /* Handle some special cases */
-                   switch (val) {
-
-                       case 3:
-                           AddCodeSegLine (CS, "sta tmp1");
-                           AddCodeSegLine (CS, "asl a");
-                           AddCodeSegLine (CS, "clc");
-                           AddCodeSegLine (CS, "adc tmp1");
-                           return;
-
-                       case 5:
-                           AddCodeSegLine (CS, "sta tmp1");
-                           AddCodeSegLine (CS, "asl a");
-                           AddCodeSegLine (CS, "asl a");
-                           AddCodeSegLine (CS, "clc");
-                           AddCodeSegLine (CS, "adc tmp1");
-                           return;
-
-                       case 10:
-                           AddCodeSegLine (CS, "sta tmp1");
-                           AddCodeSegLine (CS, "asl a");
-                           AddCodeSegLine (CS, "asl a");
-                           AddCodeSegLine (CS, "clc");
-                           AddCodeSegLine (CS, "adc tmp1");
-                           AddCodeSegLine (CS, "asl a");
-                           return;
-                   }
+                   /* 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");
+                           AddCodeLine ("adc tmp1");
+                           return;
+
+                       case 6:
+                           AddCodeLine ("sta tmp1");
+                           AddCodeLine ("asl a");
+                           AddCodeLine ("clc");
+                           AddCodeLine ("adc tmp1");
+                           AddCodeLine ("asl a");
+                           return;
+
+                       case 10:
+                           AddCodeLine ("sta tmp1");
+                           AddCodeLine ("asl a");
+                           AddCodeLine ("asl a");
+                           AddCodeLine ("clc");
+                           AddCodeLine ("adc tmp1");
+                           AddCodeLine ("asl a");
+                           return;
+                   }
                }
                /* FALLTHROUGH */
 
-           case CF_INT:
-               break;
+           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;
+           case CF_LONG:
+               break;
 
-           default:
-               typeerror (flags);
-       }
+           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 */
+       /* 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);
 
     }
@@ -2826,7 +2623,7 @@ void g_or (unsigned flags, unsigned long val)
            case CF_CHAR:
                if (flags & CF_FORCECHAR) {
                    if ((val & 0xFF) != 0xFF) {
-                               AddCodeSegLine (CS, "ora #$%02X", (unsigned char)val);
+                               AddCodeLine ("ora #$%02X", (unsigned char)val);
                    }
                    return;
                }
@@ -2834,14 +2631,14 @@ void g_or (unsigned flags, unsigned long val)
 
            case CF_INT:
                if (val <= 0xFF) {
-                   AddCodeSegLine (CS, "ora #$%02X", (unsigned char)val);
+                   AddCodeLine ("ora #$%02X", (unsigned char)val);
                    return;
                }
                break;
 
            case CF_LONG:
                if (val <= 0xFF) {
-                   AddCodeSegLine (CS, "ora #$%02X", (unsigned char)val);
+                   AddCodeLine ("ora #$%02X", (unsigned char)val);
                    return;
                }
                break;
@@ -2884,7 +2681,7 @@ void g_xor (unsigned flags, unsigned long val)
            case CF_CHAR:
                if (flags & CF_FORCECHAR) {
                    if ((val & 0xFF) != 0) {
-                               AddCodeSegLine (CS, "eor #$%02X", (unsigned char)val);
+                               AddCodeLine ("eor #$%02X", (unsigned char)val);
                    }
                    return;
                }
@@ -2893,15 +2690,15 @@ void g_xor (unsigned flags, unsigned long val)
            case CF_INT:
                if (val <= 0xFF) {
                    if (val != 0) {
-                       AddCodeSegLine (CS, "eor #$%02X", (unsigned char)val);
+                       AddCodeLine ("eor #$%02X", (unsigned char)val);
                    }
                    return;
                } else if ((val & 0xFF) == 0) {
-                   AddCodeSegLine (CS, "pha");
-                   AddCodeSegLine (CS, "txa");
-                   AddCodeSegLine (CS, "eor #$%02X", (unsigned char)(val >> 8));
-                   AddCodeSegLine (CS, "tax");
-                   AddCodeSegLine (CS, "pla");
+                   AddCodeLine ("pha");
+                   AddCodeLine ("txa");
+                   AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
+                   AddCodeLine ("tax");
+                   AddCodeLine ("pla");
                    return;
                }
                break;
@@ -2909,7 +2706,7 @@ void g_xor (unsigned flags, unsigned long val)
            case CF_LONG:
                if (val <= 0xFF) {
                    if (val != 0) {
-                               AddCodeSegLine (CS, "eor #$%02X", (unsigned char)val);
+                               AddCodeLine ("eor #$%02X", (unsigned char)val);
                    }
                    return;
                }
@@ -2951,7 +2748,7 @@ void g_and (unsigned flags, unsigned long val)
 
            case CF_CHAR:
                if (flags & CF_FORCECHAR) {
-                   AddCodeSegLine (CS, "and #$%02X", (unsigned char)val);
+                   AddCodeLine ("and #$%02X", (unsigned char)val);
                    return;
                }
                /* FALLTHROUGH */
@@ -2962,23 +2759,23 @@ void g_and (unsigned flags, unsigned long val)
                        if (val == 0) {
                            ldaconst (0);
                        } else if (val != 0xFF) {
-                           AddCodeSegLine (CS, "and #$%02X", (unsigned char)val);
+                           AddCodeLine ("and #$%02X", (unsigned char)val);
                        }
                    } else if ((val & 0xFF00) == 0xFF00) {
-                       AddCodeSegLine (CS, "and #$%02X", (unsigned char)val);
+                       AddCodeLine ("and #$%02X", (unsigned char)val);
                    } else if ((val & 0x00FF) == 0x0000) {
-                       AddCodeSegLine (CS, "txa");
-                       AddCodeSegLine (CS, "and #$%02X", (unsigned char)(val >> 8));
-                       AddCodeSegLine (CS, "tax");
+                       AddCodeLine ("txa");
+                       AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
+                       AddCodeLine ("tax");
                        ldaconst (0);
-                   } else {
-                       AddCodeSegLine (CS, "tay");
-                       AddCodeSegLine (CS, "txa");
-                       AddCodeSegLine (CS, "and #$%02X", (unsigned char)(val >> 8));
-                       AddCodeSegLine (CS, "tax");
-                       AddCodeSegLine (CS, "tya");
+                   } else {
+                       AddCodeLine ("tay");
+                       AddCodeLine ("txa");
+                       AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
+                       AddCodeLine ("tax");
+                       AddCodeLine ("tya");
                        if ((val & 0x00FF) != 0x00FF) {
-                           AddCodeSegLine (CS, "and #$%02X", (unsigned char)val);
+                           AddCodeLine ("and #$%02X", (unsigned char)val);
                        }
                    }
                }
@@ -2987,16 +2784,16 @@ void g_and (unsigned flags, unsigned long val)
            case CF_LONG:
                if (val <= 0xFF) {
                    ldxconst (0);
-                   AddCodeSegLine (CS, "stx sreg+1");
-                   AddCodeSegLine (CS, "stx sreg");
+                   AddCodeLine ("stx sreg+1");
+                   AddCodeLine ("stx sreg");
                    if ((val & 0xFF) != 0xFF) {
-                        AddCodeSegLine (CS, "and #$%02X", (unsigned char)val);
+                        AddCodeLine ("and #$%02X", (unsigned char)val);
                    }
                    return;
                } else if (val == 0xFF00) {
                    ldaconst (0);
-                   AddCodeSegLine (CS, "sta sreg+1");
-                   AddCodeSegLine (CS, "sta sreg");
+                   AddCodeLine ("sta sreg+1");
+                   AddCodeLine ("sta sreg");
                    return;
                }
                break;
@@ -3022,10 +2819,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
@@ -3037,59 +2834,67 @@ void g_asr (unsigned flags, unsigned long val)
 
            case CF_CHAR:
            case CF_INT:
-               if (val >= 1 && val <= 3) {
-                   if (flags & CF_UNSIGNED) {
-                       AddCodeSegLine (CS, "jsr shrax%ld", val);
-                   } else {
-                       AddCodeSegLine (CS, "jsr asrax%ld", val);
-                   }
-                   return;
-               } else if (val == 8 && (flags & CF_UNSIGNED)) {
-                   AddCodeSegLine (CS, "txa");
+                       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) {
-                       AddCodeSegLine (CS, "jsr shreax%ld", val);
-                   } else {
-                       AddCodeSegLine (CS, "jsr asreax%ld", val);
-                   }
-                   return;
-               } else if (val == 8 && (flags & CF_UNSIGNED)) {
-                   AddCodeSegLine (CS, "txa");
-                   AddCodeSegLine (CS, "ldx sreg");
-                   AddCodeSegLine (CS, "ldy sreg+1");
-                   AddCodeSegLine (CS, "sty sreg");
-                   AddCodeSegLine (CS, "ldy #$00");
-                   AddCodeSegLine (CS, "sty sreg+1");
+           case CF_LONG:
+               if (val == 0) {
+                   /* Nothing to do */
                    return;
+               } 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;
                } else if (val == 16) {
-                   AddCodeSegLine (CS, "ldy #$00");
-                   AddCodeSegLine (CS, "ldx sreg+1");
-                   if ((flags & CF_UNSIGNED) == 0) {
-                       AddCodeSegLine (CS, "bpl *+3");
-                       AddCodeSegLine (CS, "dey");
-                       AddCodeHint ("y:!");
-                   }
-                   AddCodeSegLine (CS, "lda sreg");
-                   AddCodeSegLine (CS, "sty sreg+1");
-                   AddCodeSegLine (CS, "sty sreg");
-                   return;
-               }
-               break;
+                   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;
 
-           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);
 
     }
 
@@ -3103,10 +2908,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",
     };
 
 
@@ -3119,40 +2924,47 @@ 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) {
-                       AddCodeSegLine (CS, "jsr shlax%ld", val);
+                       AddCodeLine ("jsr shlax%ld", val);
                    } else {
-                       AddCodeSegLine (CS, "jsr aslax%ld", val);
+                       AddCodeLine ("jsr aslax%ld", val);
                    }
                    return;
-               } else if (val == 8) {
-                   AddCodeSegLine (CS, "tax");
-                   AddCodeSegLine (CS, "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) {
-                       AddCodeSegLine (CS, "jsr shleax%ld", val);
+                       AddCodeLine ("jsr shleax%ld", val);
                    } else {
-                       AddCodeSegLine (CS, "jsr asleax%ld", val);
+                       AddCodeLine ("jsr asleax%ld", val);
                    }
                    return;
                } else if (val == 8) {
-                   AddCodeSegLine (CS, "ldy sreg");
-                   AddCodeSegLine (CS, "sty sreg+1");
-                   AddCodeSegLine (CS, "stx sreg");
-                   AddCodeSegLine (CS, "tax");
-                   AddCodeSegLine (CS, "lda #$00");
+                   AddCodeLine ("ldy sreg");
+                   AddCodeLine ("sty sreg+1");
+                   AddCodeLine ("stx sreg");
+                   AddCodeLine ("tax");
+                   AddCodeLine ("lda #$00");
                    return;
                } else if (val == 16) {
-                   AddCodeSegLine (CS, "stx sreg+1");
-                   AddCodeSegLine (CS, "sta sreg");
-                   AddCodeSegLine (CS, "lda #$00");
-                   AddCodeSegLine (CS, "tax");
+                   AddCodeLine ("stx sreg+1");
+                   AddCodeLine ("sta sreg");
+                   AddCodeLine ("lda #$00");
+                   AddCodeLine ("tax");
                    return;
                }
                break;
@@ -3181,11 +2993,11 @@ void g_neg (unsigned flags)
 
        case CF_CHAR:
        case CF_INT:
-           AddCodeSegLine (CS, "jsr negax");
+           AddCodeLine ("jsr negax");
            break;
 
        case CF_LONG:
-           AddCodeSegLine (CS, "jsr negeax");
+           AddCodeLine ("jsr negeax");
            break;
 
        default:
@@ -3201,15 +3013,15 @@ void g_bneg (unsigned flags)
     switch (flags & CF_TYPE) {
 
        case CF_CHAR:
-           AddCodeSegLine (CS, "jsr bnega");
+           AddCodeLine ("jsr bnega");
            break;
 
        case CF_INT:
-           AddCodeSegLine (CS, "jsr bnegax");
+           AddCodeLine ("jsr bnegax");
            break;
 
        case CF_LONG:
-           AddCodeSegLine (CS, "jsr bnegeax");
+           AddCodeLine ("jsr bnegeax");
            break;
 
        default:
@@ -3226,11 +3038,11 @@ void g_com (unsigned flags)
 
        case CF_CHAR:
        case CF_INT:
-           AddCodeSegLine (CS, "jsr complax");
+           AddCodeLine ("jsr complax");
            break;
 
        case CF_LONG:
-           AddCodeSegLine (CS, "jsr compleax");
+           AddCodeLine ("jsr compleax");
            break;
 
        default:
@@ -3256,11 +3068,11 @@ void g_inc (unsigned flags, unsigned long val)
            if (flags & CF_FORCECHAR) {
                if (CPU == CPU_65C02 && val <= 2) {
                    while (val--) {
-                       AddCodeSegLine (CS, "ina");
+                       AddCodeLine ("ina");
                    }
                } else {
-                   AddCodeSegLine (CS, "clc");
-                   AddCodeSegLine (CS, "adc #$%02X", (unsigned char)val);
+                   AddCodeLine ("clc");
+                   AddCodeLine ("adc #$%02X", (unsigned char)val);
                }
                break;
            }
@@ -3268,50 +3080,51 @@ void g_inc (unsigned flags, unsigned long val)
 
        case CF_INT:
            if (CPU == CPU_65C02 && val == 1) {
-               AddCodeSegLine (CS, "ina");
-               AddCodeSegLine (CS, "bne *+3");
-               AddCodeSegLine (CS, "inx");
-               /* Tell the optimizer that the X register may be invalid */
-               AddCodeHint ("x:!");
+               unsigned L = GetLocalLabel();
+               AddCodeLine ("ina");
+               AddCodeLine ("bne %s", LocalLabelName (L));
+               AddCodeLine ("inx");
+               g_defcodelabel (L);
            } else if (CodeSizeFactor < 200) {
                /* Use jsr calls */
                if (val <= 8) {
-                   AddCodeSegLine (CS, "jsr incax%lu", val);
+                   AddCodeLine ("jsr incax%lu", val);
                } else if (val <= 255) {
                    ldyconst (val);
-                   AddCodeSegLine (CS, "jsr incaxy");
+                   AddCodeLine ("jsr incaxy");
                } else {
                    g_add (flags | CF_CONST, val);
                }
            } else {
                /* Inline the code */
-               if (val < 0x300) {
+               if (val <= 0x300) {
                    if ((val & 0xFF) != 0) {
-                       AddCodeSegLine (CS, "clc");
-                       AddCodeSegLine (CS, "adc #$%02X", (unsigned char) val);
-                       AddCodeSegLine (CS, "bcc *+3");
-                       AddCodeSegLine (CS, "inx");
-                       /* Tell the optimizer that the X register may be invalid */
-                               AddCodeHint ("x:!");
+                       unsigned L = GetLocalLabel();
+                       AddCodeLine ("clc");
+                       AddCodeLine ("adc #$%02X", (unsigned char) val);
+                       AddCodeLine ("bcc %s", LocalLabelName (L));
+                       AddCodeLine ("inx");
+                       g_defcodelabel (L);
                    }
                    if (val >= 0x100) {
-                       AddCodeSegLine (CS, "inx");
+                       AddCodeLine ("inx");
                    }
                    if (val >= 0x200) {
-                       AddCodeSegLine (CS, "inx");
+                       AddCodeLine ("inx");
+                   }
+                   if (val >= 0x300) {
+                       AddCodeLine ("inx");
                    }
                } else {
-                   AddCodeSegLine (CS, "clc");
+                   AddCodeLine ("clc");
                    if ((val & 0xFF) != 0) {
-                       AddCodeSegLine (CS, "adc #$%02X", (unsigned char) val);
-                       /* Tell the optimizer that the X register may be invalid */
-                       AddCodeHint ("x:!");
+                       AddCodeLine ("adc #$%02X", (unsigned char) val);
                    }
-                   AddCodeSegLine (CS, "pha");
-                   AddCodeSegLine (CS, "txa");
-                   AddCodeSegLine (CS, "adc #$%02X", (unsigned char) (val >> 8));
-                   AddCodeSegLine (CS, "tax");
-                   AddCodeSegLine (CS, "pla");
+                   AddCodeLine ("pha");
+                   AddCodeLine ("txa");
+                   AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
+                   AddCodeLine ("tax");
+                   AddCodeLine ("pla");
                }
            }
            break;
@@ -3319,7 +3132,7 @@ void g_inc (unsigned flags, unsigned long val)
                case CF_LONG:
            if (val <= 255) {
                ldyconst (val);
-               AddCodeSegLine (CS, "jsr inceaxy");
+               AddCodeLine ("jsr inceaxy");
            } else {
                g_add (flags | CF_CONST, val);
            }
@@ -3349,11 +3162,11 @@ void g_dec (unsigned flags, unsigned long val)
            if (flags & CF_FORCECHAR) {
                if (CPU == CPU_65C02 && val <= 2) {
                    while (val--) {
-                       AddCodeSegLine (CS, "dea");
+                       AddCodeLine ("dea");
                    }
                } else {
-                   AddCodeSegLine (CS, "sec");
-                   AddCodeSegLine (CS, "sbc #$%02X", (unsigned char)val);
+                   AddCodeLine ("sec");
+                   AddCodeLine ("sbc #$%02X", (unsigned char)val);
                }
                break;
            }
@@ -3363,10 +3176,10 @@ void g_dec (unsigned flags, unsigned long val)
            if (CodeSizeFactor < 200) {
                /* Use subroutines */
                if (val <= 8) {
-                   AddCodeSegLine (CS, "jsr decax%d", (int) val);
+                   AddCodeLine ("jsr decax%d", (int) val);
                } else if (val <= 255) {
                    ldyconst (val);
-                   AddCodeSegLine (CS, "jsr decaxy");
+                   AddCodeLine ("jsr decaxy");
                } else {
                    g_sub (flags | CF_CONST, val);
                }
@@ -3374,31 +3187,29 @@ void g_dec (unsigned flags, unsigned long val)
                /* Inline the code */
                if (val < 0x300) {
                    if ((val & 0xFF) != 0) {
-                       AddCodeSegLine (CS, "sec");
-                       AddCodeSegLine (CS, "sbc #$%02X", (unsigned char) val);
-                       AddCodeSegLine (CS, "bcs *+3");
-                       AddCodeSegLine (CS, "dex");
-                       /* Tell the optimizer that the X register may be invalid */
-                               AddCodeHint ("x:!");
+                       unsigned L = GetLocalLabel();
+                       AddCodeLine ("sec");
+                       AddCodeLine ("sbc #$%02X", (unsigned char) val);
+                       AddCodeLine ("bcs %s", LocalLabelName (L));
+                       AddCodeLine ("dex");
+                       g_defcodelabel (L);
                    }
                    if (val >= 0x100) {
-                       AddCodeSegLine (CS, "dex");
+                       AddCodeLine ("dex");
                    }
                    if (val >= 0x200) {
-                       AddCodeSegLine (CS, "dex");
+                       AddCodeLine ("dex");
                    }
                } else {
-                   AddCodeSegLine (CS, "sec");
+                   AddCodeLine ("sec");
                    if ((val & 0xFF) != 0) {
-                       AddCodeSegLine (CS, "sbc #$%02X", (unsigned char) val);
-                       /* Tell the optimizer that the X register may be invalid */
-                       AddCodeHint ("x:!");
+                       AddCodeLine ("sbc #$%02X", (unsigned char) val);
                    }
-                   AddCodeSegLine (CS, "pha");
-                   AddCodeSegLine (CS, "txa");
-                   AddCodeSegLine (CS, "sbc #$%02X", (unsigned char) (val >> 8));
-                   AddCodeSegLine (CS, "tax");
-                   AddCodeSegLine (CS, "pla");
+                   AddCodeLine ("pha");
+                   AddCodeLine ("txa");
+                   AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
+                   AddCodeLine ("tax");
+                   AddCodeLine ("pla");
                }
            }
            break;
@@ -3406,7 +3217,7 @@ void g_dec (unsigned flags, unsigned long val)
        case CF_LONG:
            if (val <= 255) {
                ldyconst (val);
-               AddCodeSegLine (CS, "jsr deceaxy");
+               AddCodeLine ("jsr deceaxy");
            } else {
                g_sub (flags | CF_CONST, val);
            }
@@ -3433,11 +3244,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.
      */
@@ -3447,17 +3260,19 @@ void g_eq (unsigned flags, unsigned long val)
 
            case CF_CHAR:
                if (flags & CF_FORCECHAR) {
-                   AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
-                   AddCodeSegLine (CS, "jsr booleq");
+                   AddCodeLine ("cmp #$%02X", (unsigned char)val);
+                   AddCodeLine ("jsr booleq");
                    return;
                }
                /* FALLTHROUGH */
 
            case CF_INT:
-               AddCodeSegLine (CS, "cpx #$%02X", (unsigned char)(val >> 8));
-                       AddCodeSegLine (CS, "bne *+4");
-               AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
-               AddCodeSegLine (CS, "jsr booleq");
+               L = GetLocalLabel();
+               AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
+                       AddCodeLine ("bne %s", LocalLabelName (L));
+               AddCodeLine ("cmp #$%02X", (unsigned char)val);
+               g_defcodelabel (L);
+               AddCodeLine ("jsr booleq");
                return;
 
            case CF_LONG:
@@ -3490,6 +3305,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.
@@ -3500,17 +3316,19 @@ void g_ne (unsigned flags, unsigned long val)
 
            case CF_CHAR:
                if (flags & CF_FORCECHAR) {
-                   AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
-                   AddCodeSegLine (CS, "jsr boolne");
+                   AddCodeLine ("cmp #$%02X", (unsigned char)val);
+                   AddCodeLine ("jsr boolne");
                    return;
                }
                /* FALLTHROUGH */
 
            case CF_INT:
-               AddCodeSegLine (CS, "cpx #$%02X", (unsigned char)(val >> 8));
-               AddCodeSegLine (CS, "bne *+4");
-               AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
-               AddCodeSegLine (CS, "jsr boolne");
+               L = GetLocalLabel();
+               AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
+               AddCodeLine ("bne %s", LocalLabelName (L));
+               AddCodeLine ("cmp #$%02X", (unsigned char)val);
+               g_defcodelabel (L);
+               AddCodeLine ("jsr boolne");
                return;
 
            case CF_LONG:
@@ -3537,10 +3355,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
@@ -3551,44 +3369,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) {
-                   AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
-                   if (flags & CF_UNSIGNED) {
-                       AddCodeSegLine (CS, "jsr boolult");
-                   } else {
-                       AddCodeSegLine (CS, "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.
                     */
-                   AddCodeSegLine (CS, "txa");
-                   AddCodeSegLine (CS, "jsr boollt");
-                   return;
+                   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));
                }
-               /* Direct code only for unsigned data types */
-               if (flags & CF_UNSIGNED) {
-                   AddCodeSegLine (CS, "cpx #$%02X", (unsigned char)(val >> 8));
-                           AddCodeSegLine (CS, "bne *+4");
-                   AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
-                   AddCodeSegLine (CS, "jsr boolult");
-                   return;
-               }
-               break;
-
-           case CF_LONG:
-               break;
+               /* Emit the proper makebool routine */
+               if (flags & CF_UNSIGNED) {
+                   /* Unsigned compare */
+                   AddCodeLine ("jsr boolult");
+               } else {
+                   /* Signed compare */
+                   AddCodeLine ("jsr boollt");
+               }
+               return;
 
            default:
                typeerror (flags);
@@ -3626,13 +3477,32 @@ void g_le (unsigned flags, unsigned long val)
        /* Look at the type */
        switch (flags & CF_TYPE) {
 
-           case CF_CHAR:
-               if (flags & CF_FORCECHAR) {
-                   AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
-                   if (flags & CF_UNSIGNED) {
-                       AddCodeSegLine (CS, "jsr boolule");
-                   } else {
-                       AddCodeSegLine (CS, "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;
                }
@@ -3640,16 +3510,53 @@ void g_le (unsigned flags, unsigned long val)
 
            case CF_INT:
                if (flags & CF_UNSIGNED) {
-                   AddCodeSegLine (CS, "cpx #$%02X", (unsigned char)(val >> 8));
-                           AddCodeSegLine (CS, "bne *+4");
-                   AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
-                   AddCodeSegLine (CS, "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);
@@ -3672,10 +3579,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",
     };
 
 
@@ -3687,51 +3594,105 @@ void g_gt (unsigned flags, unsigned long val)
        /* Look at the type */
        switch (flags & CF_TYPE) {
 
-           case CF_CHAR:
-               if (flags & CF_FORCECHAR) {
-                   AddCodeSegLine (CS, "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) {
-                           AddCodeSegLine (CS, "jsr boolugt");
-                       } else {
-                           AddCodeSegLine (CS, "jsr boolne");
-                       }
-                   } else {
-                       AddCodeSegLine (CS, "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) {
-                       AddCodeSegLine (CS, "stx tmp1");
-                       AddCodeSegLine (CS, "ora tmp1");
-                       AddCodeSegLine (CS, "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 {
-                               AddCodeSegLine (CS, "cpx #$%02X", (unsigned char)(val >> 8));
-                       AddCodeSegLine (CS, "bne *+4");
-                       AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
-                               AddCodeSegLine (CS, "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
@@ -3766,6 +3727,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 */
@@ -3773,11 +3736,11 @@ void g_ge (unsigned flags, unsigned long val)
 
            case CF_CHAR:
                if (flags & CF_FORCECHAR) {
-                   AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
+                   AddCodeLine ("cmp #$%02X", (unsigned char)val);
                    if (flags & CF_UNSIGNED) {
-                       AddCodeSegLine (CS, "jsr booluge");
+                       AddCodeLine ("jsr booluge");
                    } else {
-                       AddCodeSegLine (CS, "jsr boolge");
+                       AddCodeLine ("jsr boolge");
                    }
                    return;
                }
@@ -3785,19 +3748,59 @@ void g_ge (unsigned flags, unsigned long val)
 
            case CF_INT:
                if (flags & CF_UNSIGNED) {
-                           AddCodeSegLine (CS, "cpx #$%02X", (unsigned char)(val >> 8));
-                           AddCodeSegLine (CS, "bne *+4");
-                   AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
-                   AddCodeSegLine (CS, "jsr booluge");
-                   return;
+                   /* 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");
+               } 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
@@ -3822,12 +3825,12 @@ void g_ge (unsigned flags, unsigned long val)
 void g_res (unsigned n)
 /* Reserve static storage, n bytes */
 {
-    AddDataSegLine (DS, "\t.res\t%u,$00", n);
+    AddDataLine ("\t.res\t%u,$00", 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) {
@@ -3836,15 +3839,15 @@ void g_defdata (unsigned flags, unsigned long val, unsigned offs)
        switch (flags & CF_TYPE) {
 
            case CF_CHAR:
-               AddDataSegLine (DS, "\t.byte\t$%02lX", val & 0xFF);
+               AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
                break;
 
            case CF_INT:
-               AddDataSegLine (DS, "\t.word\t$%04lX", val & 0xFFFF);
+               AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
                break;
 
            case CF_LONG:
-               AddDataSegLine (DS, "\t.dword\t$%08lX", val & 0xFFFFFFFF);
+               AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
                break;
 
            default:
@@ -3859,7 +3862,7 @@ void g_defdata (unsigned flags, unsigned long val, unsigned offs)
        const char* Label = GetLabelName (flags, val, offs);
 
        /* Labels are always 16 bit */
-       AddDataSegLine (DS, "\t.word\t%s", Label);
+               AddDataLine ("\t.addr\t%s", Label);
 
     }
 }
@@ -3896,7 +3899,7 @@ void g_defbytes (const void* Bytes, unsigned Count)
        } while (Chunk);
 
        /* Output the line */
-               AddDataSegLine (DS, Buf);
+               AddDataLine (Buf);
     }
 }
 
@@ -3905,38 +3908,169 @@ void g_defbytes (const void* Bytes, unsigned Count)
 void g_zerobytes (unsigned n)
 /* Output n bytes of data initialized with zero */
 {
-    AddDataSegLine (DS, "\t.res\t%u,$00", n);
+    AddDataLine ("\t.res\t%u,$00", n);
+}
+
+
+
+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) {
-       AddCodeSegLine (CS, "%.*s", Len, Line);
-    } else {
-       AddCodeSegLine (CS, "%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 */
@@ -3946,34 +4080,34 @@ 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 */
-       AddCodeSegLine (CS, "ldy #$FF");
+       AddCodeLine ("ldy #$FF");
        g_defcodelabel (label);
-       AddCodeSegLine (CS, "iny");
-       AddCodeSegLine (CS, "lda %s,y", lbuf);
-       AddCodeSegLine (CS, "bne %s", LocalLabelName (label));
-               AddCodeSegLine (CS, "tax");
-       AddCodeSegLine (CS, "tya");
+       AddCodeLine ("iny");
+       AddCodeLine ("lda %s,y", lbuf);
+       AddCodeLine ("bne %s", LocalLabelName (label));
+               AddCodeLine ("tax");
+       AddCodeLine ("tya");
 
     } else {
 
                /* Address not constant but in primary */
        if (CodeSizeFactor < 400) {
            /* This is too much code, so call strlen instead of inlining */
-           AddCodeSegLine (CS, "jsr _strlen");
+           AddCodeLine ("jsr _strlen");
        } else {
            /* Inline the function */
-           AddCodeSegLine (CS, "sta ptr1");
-           AddCodeSegLine (CS, "stx ptr1+1");
-           AddCodeSegLine (CS, "ldy #$FF");
+           AddCodeLine ("sta ptr1");
+           AddCodeLine ("stx ptr1+1");
+           AddCodeLine ("ldy #$FF");
            g_defcodelabel (label);
-           AddCodeSegLine (CS, "iny");
-           AddCodeSegLine (CS, "lda (ptr1),y");
-           AddCodeSegLine (CS, "bne %s", LocalLabelName (label));
-                   AddCodeSegLine (CS, "tax");
-           AddCodeSegLine (CS, "tya");
+           AddCodeLine ("iny");
+           AddCodeLine ("lda (ptr1),y");
+           AddCodeLine ("bne %s", LocalLabelName (label));
+                   AddCodeLine ("tax");
+           AddCodeLine ("tya");
        }
     }
 }