]> git.sur5r.net Git - cc65/blobdiff - src/cc65/codegen.c
Change the OptStackOps function so that it adjusts the instruction pointer
[cc65] / src / cc65 / codegen.c
index 2f67a07fcce5bc338c8f05f6032543268a3ca470..577623aa6d941a7cb2d00f6ba234a33841c871f8 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2009 Ullrich von Bassewitz                                       */
-/*               Roemerstrasse 52                                            */
-/*               D-70794 Filderstadt                                         */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 1998-2009, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
@@ -41,9 +41,9 @@
 #include "check.h"
 #include "cpu.h"
 #include "strbuf.h"
-#include "version.h"
 #include "xmalloc.h"
 #include "xsprintf.h"
+#include "version.h"
 
 /* cc65 */
 #include "asmcode.h"
@@ -145,19 +145,14 @@ static const char* GetLabelName (unsigned Flags, unsigned long Label, long Offs)
 void g_preamble (void)
 /* Generate the assembler code preamble */
 {
-    /* Create a new (global) segment list and remember it */
-    PushSegments (0);
-    GS = CS;
-
     /* Identify the compiler version */
     AddTextLine (";");
-    AddTextLine ("; File generated by cc65 v %u.%u.%u",
-                VER_MAJOR, VER_MINOR, VER_PATCH);
+    AddTextLine ("; File generated by cc65 v %s", GetVersionAsString ());
     AddTextLine (";");
 
     /* Insert some object file options */
-    AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
-                   VER_MAJOR, VER_MINOR, VER_PATCH);
+    AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %s\"",
+                GetVersionAsString ());
 
     /* If we're producing code for some other CPU, switch the command set */
     switch (CPU) {
@@ -351,6 +346,24 @@ void g_defdatalabel (unsigned label)
 
 
 
+void g_aliasdatalabel (unsigned label, unsigned baselabel, long offs)
+/* Define label as a local alias for baselabel+offs */
+{
+    /* We need an intermediate buffer here since LocalLabelName uses a
+     * static buffer which changes with each call.
+     */
+    StrBuf L = AUTO_STRBUF_INITIALIZER;
+    SB_AppendStr (&L, LocalLabelName (label));
+    SB_Terminate (&L);
+    AddDataLine ("%s\t:=\t%s+%ld",
+                 SB_GetConstBuf (&L),
+                 LocalLabelName (baselabel),
+                 offs);
+    SB_Done (&L);
+}
+
+
+
 /*****************************************************************************/
 /*                    Functions handling global labels                      */
 /*****************************************************************************/
@@ -406,36 +419,6 @@ void g_importmainargs (void)
 
 
 
-/*****************************************************************************/
-/*                   Load functions for various registers                   */
-/*****************************************************************************/
-
-
-
-static void ldaconst (unsigned val)
-/* Load a with a constant */
-{
-    AddCodeLine ("lda #$%02X", val & 0xFF);
-}
-
-
-
-static void ldxconst (unsigned val)
-/* Load x with a constant */
-{
-    AddCodeLine ("ldx #$%02X", val & 0xFF);
-}
-
-
-
-static void ldyconst (unsigned val)
-/* Load y with a constant */
-{
-    AddCodeLine ("ldy #$%02X", val & 0xFF);
-}
-
-
-
 /*****************************************************************************/
 /*                          Function entry and exit                         */
 /*****************************************************************************/
@@ -468,39 +451,30 @@ void g_leave (void)
 /* Function epilogue */
 {
     /* How many bytes of locals do we have to drop? */
-    unsigned k = (unsigned) -StackPtr;
+    unsigned ToDrop = (unsigned) -StackPtr;
 
     /* If we didn't have a variable argument list, don't call leave */
     if (funcargs >= 0) {
 
-        /* Drop stackframe if needed. We can only drop 255 bytes at a time. */
-        k += funcargs;
-        while (k > 0) {
-            unsigned ToDrop = (k > 255)? 255 : k;
-            if (ToDrop <= 8) {
-                AddCodeLine ("jsr incsp%d", k);
-            } else {
-                       ldyconst (ToDrop);
-                       AddCodeLine ("jsr addysp");
-            }
-            k -= ToDrop;
-        }
+        /* Drop stackframe if needed */
+        g_drop (ToDrop + funcargs);
 
-    } else {
+    } else if (StackPtr != 0) {
 
-        if (k == 0) {
-            /* Nothing to drop */
+        /* We've a stack frame to drop */
+        if (ToDrop > 255) {
+            g_drop (ToDrop);            /* Inlines the code */
             AddCodeLine ("jsr leave");
         } else {
-            /* We've a stack frame to drop */
-            while (k > 255) {
-                ldyconst (255);
-                AddCodeLine ("jsr addysp");
-                k -= 255;
-            }
-            ldyconst (k);
+            AddCodeLine ("ldy #$%02X", ToDrop);
             AddCodeLine ("jsr leavey");
         }
+
+    } else {
+
+        /* Nothing to drop */
+        AddCodeLine ("jsr leave");
+
     }
 
     /* Add the final rts */
@@ -523,14 +497,13 @@ void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
     CheckLocalOffs (StackOffs);
 
     /* Generate code */
+    AddCodeLine ("ldy #$%02X", StackOffs & 0xFF);
     if (Bytes == 1) {
 
         if (IS_Get (&CodeSizeFactor) < 165) {
-            ldyconst (StackOffs);
-            ldxconst (RegOffs);
+            AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
             AddCodeLine ("jsr regswap1");
         } else {
-            ldyconst (StackOffs);
             AddCodeLine ("lda (sp),y");
             AddCodeLine ("ldx regbank%+d", RegOffs);
             AddCodeLine ("sta regbank%+d", RegOffs);
@@ -540,15 +513,13 @@ void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
 
     } else if (Bytes == 2) {
 
-        ldyconst (StackOffs);
-        ldxconst (RegOffs);
+        AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
         AddCodeLine ("jsr regswap2");
 
     } else {
 
-        ldyconst (StackOffs);
-        ldxconst (RegOffs);
-        ldaconst (Bytes);
+        AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
+        AddCodeLine ("lda #$%02X", Bytes & 0xFF);
         AddCodeLine ("jsr regswap");
     }
 }
@@ -575,8 +546,8 @@ void g_save_regvars (int RegOffs, unsigned Bytes)
         /* More than two bytes - loop */
         unsigned Label = GetLocalLabel ();
         g_space (Bytes);
-        ldyconst (Bytes - 1);
-        ldxconst (Bytes);
+        AddCodeLine ("ldy #$%02X", (unsigned char) (Bytes - 1));
+        AddCodeLine ("ldx #$%02X", (unsigned char) Bytes);
         g_defcodelabel (Label);
         AddCodeLine ("lda regbank%+d,x", RegOffs-1);
         AddCodeLine ("sta (sp),y");
@@ -602,13 +573,13 @@ void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
     /* Don't loop for up to two bytes */
     if (Bytes == 1) {
 
-        ldyconst (StackOffs);
+        AddCodeLine ("ldy #$%02X", StackOffs);
         AddCodeLine ("lda (sp),y");
         AddCodeLine ("sta regbank%+d", RegOffs);
 
     } else if (Bytes == 2) {
 
-        ldyconst (StackOffs);
+        AddCodeLine ("ldy #$%02X", StackOffs);
         AddCodeLine ("lda (sp),y");
         AddCodeLine ("sta regbank%+d", RegOffs);
         AddCodeLine ("iny");
@@ -617,7 +588,7 @@ void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
 
     } else if (Bytes == 3 && IS_Get (&CodeSizeFactor) >= 133) {
 
-        ldyconst (StackOffs);
+        AddCodeLine ("ldy #$%02X", StackOffs);
         AddCodeLine ("lda (sp),y");
         AddCodeLine ("sta regbank%+d", RegOffs);
         AddCodeLine ("iny");
@@ -634,7 +605,7 @@ void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
          * code that uses just one index register.
          */
         unsigned Label = GetLocalLabel ();
-        ldyconst (StackOffs);
+        AddCodeLine ("ldy #$%02X", StackOffs);
         g_defcodelabel (Label);
         AddCodeLine ("lda (sp),y");
         AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs);
@@ -649,8 +620,8 @@ void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
          */
         unsigned Label = GetLocalLabel ();
         AddCodeLine ("stx tmp1");
-        ldyconst (StackOffs + Bytes - 1);
-        ldxconst (Bytes - 1);
+        AddCodeLine ("ldy #$%02X", (unsigned char) (StackOffs + Bytes - 1));
+        AddCodeLine ("ldx #$%02X", (unsigned char) (Bytes - 1));
         g_defcodelabel (Label);
         AddCodeLine ("lda (sp),y");
         AddCodeLine ("sta regbank%+d,x", RegOffs);
@@ -684,13 +655,13 @@ void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
 
             case CF_CHAR:
                 if ((Flags & CF_FORCECHAR) != 0) {
-                    ldaconst (Val);
+                    AddCodeLine ("lda #$%02X", (unsigned char) Val);
                     break;
                 }
                 /* FALL THROUGH */
             case CF_INT:
-                ldxconst ((Val >> 8) & 0xFF);
-                ldaconst (Val & 0xFF);
+                AddCodeLine ("ldx #$%02X", (unsigned char) (Val >> 8));
+                AddCodeLine ("lda #$%02X", (unsigned char) Val);
                 break;
 
             case CF_LONG:
@@ -769,7 +740,7 @@ void g_getstatic (unsigned flags, unsigned long label, long offs)
             if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
                 AddCodeLine ("lda %s", lbuf);  /* load A from the label */
             } else {
-               ldxconst (0);
+                AddCodeLine ("ldx #$00");
                AddCodeLine ("lda %s", lbuf);   /* load A from the label */
                if (!(flags & CF_UNSIGNED)) {
                    /* Must sign extend */
@@ -823,10 +794,10 @@ void g_getlocal (unsigned Flags, int Offs)
         case CF_CHAR:
             CheckLocalOffs (Offs);
             if ((Flags & CF_FORCECHAR) || (Flags & CF_TEST)) {
-                ldyconst (Offs);
+                AddCodeLine ("ldy #$%02X", Offs);
                 AddCodeLine ("lda (sp),y");
             } else {
-                ldyconst (Offs);
+                AddCodeLine ("ldy #$%02X", Offs);
                 AddCodeLine ("ldx #$00");
                 AddCodeLine ("lda (sp),y");
                if ((Flags & CF_UNSIGNED) == 0) {
@@ -845,13 +816,8 @@ void g_getlocal (unsigned Flags, int Offs)
                AddCodeLine ("lda (sp),y");
                 AddCodeLine ("dey");
                 AddCodeLine ("ora (sp),y");
-            } else if (IS_Get (&CodeSizeFactor) < 165) {
-                AddCodeLine ("jsr ldaxysp");
             } else {
-               AddCodeLine ("lda (sp),y");
-                AddCodeLine ("tax");
-                AddCodeLine ("dey");
-                AddCodeLine ("lda (sp),y");
+                AddCodeLine ("jsr ldaxysp");
             }
             break;
 
@@ -887,31 +853,30 @@ void g_getind (unsigned Flags, unsigned Offs)
 
         case CF_CHAR:
             /* Character sized */
+            AddCodeLine ("ldy #$%02X", Offs);
             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);
+                AddCodeLine ("ldy #$%02X", Offs);
                 AddCodeLine ("sta ptr1");
                 AddCodeLine ("stx ptr1+1");
                 AddCodeLine ("lda (ptr1),y");
                 AddCodeLine ("iny");
                 AddCodeLine ("ora (ptr1),y");
             } else {
-                ldyconst (Offs+1);
+                AddCodeLine ("ldy #$%02X", Offs+1);
                 AddCodeLine ("jsr ldaxidx");
             }
             break;
 
         case CF_LONG:
-            ldyconst (Offs+3);
+            AddCodeLine ("ldy #$%02X", Offs+3);
             AddCodeLine ("jsr ldeaxidx");
             if (Flags & CF_TEST) {
                 g_test (Flags);
@@ -929,13 +894,15 @@ void g_getind (unsigned Flags, unsigned Offs)
 void g_leasp (int Offs)
 /* Fetch the address of the specified symbol into the primary register */
 {
-    /* Get low and high byte */
-    unsigned char Lo = (unsigned char) Offs;
-    unsigned char Hi = (unsigned char) (Offs >> 8);
+    unsigned char Lo, Hi;
 
     /* Calculate the offset relative to sp */
     Offs -= StackPtr;
 
+    /* Get low and high byte */
+    Lo = (unsigned char) Offs;
+    Hi = (unsigned char) (Offs >> 8);
+
     /* Generate code */
     if (Lo == 0) {
         if (Hi <= 3) {
@@ -977,10 +944,10 @@ void g_leasp (int Offs)
         /* Full 16 bit offset inlined */
         AddCodeLine ("lda sp");
         AddCodeLine ("clc");
-        AddCodeLine ("adc #$%02X", (unsigned char) Offs);
+        AddCodeLine ("adc #$%02X", Lo);
         AddCodeLine ("pha");
         AddCodeLine ("lda sp+1");
-        AddCodeLine ("adc #$%02X", (unsigned char) (Offs >> 8));
+        AddCodeLine ("adc #$%02X", Hi);
         AddCodeLine ("tax");
         AddCodeLine ("pla");
     }
@@ -1006,7 +973,7 @@ void g_leavariadic (int Offs)
     CheckLocalOffs (ArgSizeOffs);
 
     /* Get the size of all parameters. */
-    ldyconst (ArgSizeOffs);
+    AddCodeLine ("ldy #$%02X", ArgSizeOffs);
     AddCodeLine ("lda (sp),y");
 
     /* Add the value of the stackpointer */
@@ -1085,13 +1052,13 @@ void g_putlocal (unsigned Flags, int Offs, long Val)
             if (Flags & CF_CONST) {
                AddCodeLine ("lda #$%02X", (unsigned char) Val);
             }
-            ldyconst (Offs);
+            AddCodeLine ("ldy #$%02X", Offs);
             AddCodeLine ("sta (sp),y");
             break;
 
         case CF_INT:
             if (Flags & CF_CONST) {
-                ldyconst (Offs+1);
+                AddCodeLine ("ldy #$%02X", Offs+1);
                 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
                 AddCodeLine ("sta (sp),y");
                 if ((Flags & CF_NOKEEP) == 0) {
@@ -1108,11 +1075,10 @@ void g_putlocal (unsigned Flags, int Offs, long Val)
                 }
                 AddCodeLine ("sta (sp),y");
             } else {
+                AddCodeLine ("ldy #$%02X", Offs);
                 if ((Flags & CF_NOKEEP) == 0 || IS_Get (&CodeSizeFactor) < 160) {
-                    ldyconst (Offs);
                     AddCodeLine ("jsr staxysp");
                 } else {
-                    ldyconst (Offs);
                     AddCodeLine ("sta (sp),y");
                     AddCodeLine ("iny");
                     AddCodeLine ("txa");
@@ -1125,7 +1091,7 @@ void g_putlocal (unsigned Flags, int Offs, long Val)
             if (Flags & CF_CONST) {
                g_getimmed (Flags, Val, 0);
             }
-            ldyconst (Offs);
+            AddCodeLine ("ldy #$%02X", Offs);
             AddCodeLine ("jsr steaxysp");
             break;
 
@@ -1181,20 +1147,18 @@ void g_putind (unsigned Flags, unsigned Offs)
     }
 
     /* Check the size and determine operation */
+    AddCodeLine ("ldy #$%02X", Offs);
     switch (Flags & CF_TYPE) {
 
         case CF_CHAR:
-            ldyconst (Offs);
             AddCodeLine ("jsr staspidx");
             break;
 
         case CF_INT:
-            ldyconst (Offs);
             AddCodeLine ("jsr staxspidx");
             break;
 
         case CF_LONG:
-            ldyconst (Offs);
             AddCodeLine ("jsr steaxspidx");
             break;
 
@@ -1333,7 +1297,7 @@ void g_reglong (unsigned Flags)
         case CF_INT:
             if (Flags & CF_UNSIGNED) {
                if (IS_Get (&CodeSizeFactor) >= 200) {
-                   ldyconst (0);
+                    AddCodeLine ("ldy #$00");
                    AddCodeLine ("sty sreg");
                    AddCodeLine ("sty sreg+1");
                } else {
@@ -1771,19 +1735,19 @@ void g_addeqstatic (unsigned flags, unsigned long label, long offs,
 
 
 
-void g_addeqlocal (unsigned flags, int offs, unsigned long val)
+void g_addeqlocal (unsigned flags, int Offs, unsigned long val)
 /* Emit += for a local variable */
 {
     /* Calculate the true offset, check it, load it into Y */
-    offs -= StackPtr;
-    CheckLocalOffs (offs);
+    Offs -= StackPtr;
+    CheckLocalOffs (Offs);
 
     /* Check the size and determine operation */
     switch (flags & CF_TYPE) {
 
         case CF_CHAR:
             if (flags & CF_FORCECHAR) {
-                ldyconst (offs);
+                AddCodeLine ("ldy #$%02X", Offs);
                 AddCodeLine ("ldx #$00");
                 if (flags & CF_CONST) {
                     AddCodeLine ("clc");
@@ -1806,7 +1770,7 @@ void g_addeqlocal (unsigned flags, int offs, unsigned long val)
             /* FALLTHROUGH */
 
         case CF_INT:
-            ldyconst (offs);
+            AddCodeLine ("ldy #$%02X", Offs);
             if (flags & CF_CONST) {
                 if (IS_Get (&CodeSizeFactor) >= 400) {
                     AddCodeLine ("clc");
@@ -1833,7 +1797,7 @@ void g_addeqlocal (unsigned flags, int offs, unsigned long val)
             if (flags & CF_CONST) {
                g_getimmed (flags, val, 0);
             }
-            ldyconst (offs);
+            AddCodeLine ("ldy #$%02X", Offs);
             AddCodeLine ("jsr laddeqysp");
             break;
 
@@ -2003,19 +1967,19 @@ void g_subeqstatic (unsigned flags, unsigned long label, long offs,
 
 
 
-void g_subeqlocal (unsigned flags, int offs, unsigned long val)
+void g_subeqlocal (unsigned flags, int Offs, unsigned long val)
 /* Emit -= for a local variable */
 {
     /* Calculate the true offset, check it, load it into Y */
-    offs -= StackPtr;
-    CheckLocalOffs (offs);
+    Offs -= StackPtr;
+    CheckLocalOffs (Offs);
 
     /* Check the size and determine operation */
     switch (flags & CF_TYPE) {
 
         case CF_CHAR:
             if (flags & CF_FORCECHAR) {
-               ldyconst (offs);
+                AddCodeLine ("ldy #$%02X", Offs);
                 AddCodeLine ("ldx #$00");
                 if (flags & CF_CONST) {
                     AddCodeLine ("lda (sp),y");
@@ -2041,7 +2005,7 @@ void g_subeqlocal (unsigned flags, int offs, unsigned long val)
             if (flags & CF_CONST) {
                g_getimmed (flags, val, 0);
             }
-            ldyconst (offs);
+            AddCodeLine ("ldy #$%02X", Offs);
             AddCodeLine ("jsr subeqysp");
             break;
 
@@ -2049,7 +2013,7 @@ void g_subeqlocal (unsigned flags, int offs, unsigned long val)
             if (flags & CF_CONST) {
                g_getimmed (flags, val, 0);
             }
-            ldyconst (offs);
+            AddCodeLine ("ldy #$%02X", Offs);
             AddCodeLine ("jsr lsubeqysp");
             break;
 
@@ -2350,7 +2314,7 @@ void g_push (unsigned flags, unsigned long val)
         if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
 
             /* Handle as 8 bit value */
-            ldaconst (val);
+            AddCodeLine ("lda #$%02X", (unsigned char) val);
             AddCodeLine ("jsr pusha");
 
         } else {
@@ -2428,7 +2392,7 @@ void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
 {
     if ((Flags & CF_FIXARGC) == 0) {
         /* Pass the argument count */
-        ldyconst (ArgSize);
+        AddCodeLine ("ldy #$%02X", ArgSize);
     }
     AddCodeLine ("jsr _%s", Label);
     StackPtr += ArgSize;                /* callee pops args */
@@ -2443,7 +2407,7 @@ void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
         /* Address is in a/x */
         if ((Flags & CF_FIXARGC) == 0) {
             /* Pass arg count */
-            ldyconst (ArgSize);
+            AddCodeLine ("ldy #$%02X", ArgSize);
         }
         AddCodeLine ("jsr callax");
     } else {
@@ -2491,26 +2455,56 @@ void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
 
 
 
-static void mod_internal (int k, char* verb1, char* verb2)
+void g_drop (unsigned Space)
+/* Drop space allocated on the stack */
 {
-    if (k <= 8) {
-        AddCodeLine ("jsr %ssp%c", verb1, k + '0');
-    } else {
-        CheckLocalOffs (k);
-        ldyconst (k);
-        AddCodeLine ("jsr %ssp", verb2);
+    if (Space > 255) {
+        /* Inline the code since calling addysp repeatedly is quite some
+         * overhead.
+         */
+        AddCodeLine ("pha");
+        AddCodeLine ("lda #$%02X", (unsigned char) Space);
+        AddCodeLine ("clc");
+        AddCodeLine ("adc sp");
+        AddCodeLine ("sta sp");
+        AddCodeLine ("lda #$%02X", (unsigned char) (Space >> 8));
+        AddCodeLine ("adc sp+1");
+        AddCodeLine ("sta sp+1");
+        AddCodeLine ("pla");
+    } else if (Space > 8) {
+        AddCodeLine ("ldy #$%02X", Space);
+        AddCodeLine ("jsr addysp");
+    } else if (Space != 0) {
+        AddCodeLine ("jsr incsp%u", Space);
     }
 }
 
 
 
-void g_space (int space)
+void g_space (int Space)
 /* Create or drop space on the stack */
 {
-    if (space < 0) {
-        mod_internal (-space, "inc", "addy");
-    } else if (space > 0) {
-        mod_internal (space, "dec", "suby");
+    if (Space < 0) {
+        /* This is actually a drop operation */
+        g_drop (-Space);
+    } else if (Space > 255) {
+        /* Inline the code since calling subysp repeatedly is quite some
+         * overhead.
+         */
+        AddCodeLine ("pha");
+        AddCodeLine ("lda sp");
+        AddCodeLine ("sec");
+        AddCodeLine ("sbc #$%02X", (unsigned char) Space);
+        AddCodeLine ("sta sp");
+        AddCodeLine ("lda sp+1");
+        AddCodeLine ("sbc #$%02X", (unsigned char) (Space >> 8));
+        AddCodeLine ("sta sp+1");
+        AddCodeLine ("pla");
+    } else if (Space > 8) {
+        AddCodeLine ("ldy #$%02X", Space);
+        AddCodeLine ("jsr subysp");
+    } else if (Space != 0) {
+        AddCodeLine ("jsr decsp%u", Space);
     }
 }
 
@@ -2764,7 +2758,7 @@ void g_or (unsigned flags, unsigned long val)
                     if ((val & 0xFF) != 0) {
                         AddCodeLine ("ora #$%02X", (unsigned char)val);
                     }
-                    ldxconst (0xFF);
+                    AddCodeLine ("ldx #$FF");
                 } else if (val != 0) {
                     AddCodeLine ("ora #$%02X", (unsigned char)val);
                     AddCodeLine ("pha");
@@ -2897,7 +2891,7 @@ void g_and (unsigned Flags, unsigned long Val)
             case CF_INT:
                        if ((Val & 0xFFFF) != 0xFFFF) {
                            if (Val <= 0xFF) {
-                               ldxconst (0);
+                        AddCodeLine ("ldx #$00");
                                if (Val == 0) {
                             AddCodeLine ("lda #$00");
                                } else if (Val != 0xFF) {
@@ -2929,7 +2923,7 @@ void g_and (unsigned Flags, unsigned long Val)
 
             case CF_LONG:
                 if (Val <= 0xFF) {
-                    ldxconst (0);
+                    AddCodeLine ("ldx #$00");
                     AddCodeLine ("stx sreg+1");
                    AddCodeLine ("stx sreg");
                     if ((Val & 0xFF) != 0xFF) {
@@ -2937,7 +2931,7 @@ void g_and (unsigned Flags, unsigned long Val)
                     }
                     return;
                 } else if (Val == 0xFF00) {
-                    ldaconst (0);
+                    AddCodeLine ("lda #$00");
                     AddCodeLine ("sta sreg+1");
                     AddCodeLine ("sta sreg");
                     return;
@@ -2982,7 +2976,7 @@ void g_asr (unsigned flags, unsigned long val)
                        if (val >= 8) {
                     if (flags & CF_UNSIGNED) {
                         AddCodeLine ("txa");
-                        ldxconst (0);
+                        AddCodeLine ("ldx #$00");
                     } else {
                                unsigned L = GetLocalLabel();
                         AddCodeLine ("cpx #$80");   /* Sign bit into carry */
@@ -3310,7 +3304,7 @@ void g_inc (unsigned flags, unsigned long val)
                 if (val <= 8) {
                     AddCodeLine ("jsr incax%lu", val);
                 } else if (val <= 255) {
-                    ldyconst (val);
+                    AddCodeLine ("ldy #$%02X", (unsigned char) val);
                     AddCodeLine ("jsr incaxy");
                 } else {
                     g_add (flags | CF_CONST, val);
@@ -3356,7 +3350,7 @@ void g_inc (unsigned flags, unsigned long val)
 
         case CF_LONG:
             if (val <= 255) {
-                ldyconst (val);
+                AddCodeLine ("ldy #$%02X", (unsigned char) val);
                 AddCodeLine ("jsr inceaxy");
             } else {
                 g_add (flags | CF_CONST, val);
@@ -3403,7 +3397,7 @@ void g_dec (unsigned flags, unsigned long val)
                 if (val <= 8) {
                     AddCodeLine ("jsr decax%d", (int) val);
                 } else if (val <= 255) {
-                    ldyconst (val);
+                    AddCodeLine ("ldy #$%02X", (unsigned char) val);
                     AddCodeLine ("jsr decaxy");
                 } else {
                     g_sub (flags | CF_CONST, val);
@@ -3448,7 +3442,7 @@ void g_dec (unsigned flags, unsigned long val)
 
         case CF_LONG:
             if (val <= 255) {
-                ldyconst (val);
+                AddCodeLine ("ldy #$%02X", (unsigned char) val);
                 AddCodeLine ("jsr deceaxy");
             } else {
                 g_sub (flags | CF_CONST, val);
@@ -4228,7 +4222,7 @@ void g_defbytes (const void* Bytes, unsigned Count)
         } while (Chunk);
 
         /* Output the line */
-        AddDataLine (Buf);
+        AddDataLine ("%s", Buf);
     }
 }
 
@@ -4249,7 +4243,7 @@ void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
 {
     /* Register variables do always have less than 128 bytes */
     unsigned CodeLabel = GetLocalLabel ();
-    ldxconst (Size-1);
+    AddCodeLine ("ldx #$%02X", (unsigned char) (Size - 1));
     g_defcodelabel (CodeLabel);
     AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
     AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
@@ -4266,14 +4260,14 @@ void g_initauto (unsigned Label, unsigned Size)
 
     CheckLocalOffs (Size);
     if (Size <= 128) {
-        ldyconst (Size-1);
+        AddCodeLine ("ldy #$%02X", 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);
+        AddCodeLine ("ldy #$00");
         g_defcodelabel (CodeLabel);
         AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
         AddCodeLine ("sta (sp),y");
@@ -4290,7 +4284,7 @@ void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
 {
     if (Size <= 128) {
         unsigned CodeLabel = GetLocalLabel ();
-        ldyconst (Size-1);
+        AddCodeLine ("ldy #$%02X", Size-1);
         g_defcodelabel (CodeLabel);
         AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
         AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
@@ -4298,7 +4292,7 @@ void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
         AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
     } else if (Size <= 256) {
         unsigned CodeLabel = GetLocalLabel ();
-        ldyconst (0);
+        AddCodeLine ("ldy #$00");
         g_defcodelabel (CodeLabel);
         AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
         AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
@@ -4405,7 +4399,7 @@ void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
 void g_asmcode (struct StrBuf* B)
 /* Output one line of assembler code. */
 {
-    AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));
+    AddCodeLine ("%.*s", (int) SB_GetLen (B), SB_GetConstBuf (B));
 }