]> git.sur5r.net Git - cc65/blobdiff - src/cc65/codegen.c
Changed TgtTranslateBuf
[cc65] / src / cc65 / codegen.c
index dfc986d09c9431edeafe7b06a3320c27fb669cfe..58595df904c4296c6c74d45d4c6380a839a89cb6 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998     Ullrich von Bassewitz                                        */
-/*              Wacholderweg 14                                              */
-/*              D-70597 Stuttgart                                            */
-/* EMail:       uz@musoftware.de                                             */
+/* (C) 1998-2000 Ullrich von Bassewitz                                       */
+/*               Wacholderweg 14                                             */
+/*               D-70597 Stuttgart                                           */
+/* EMail:        uz@musoftware.de                                            */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
 #include <stdio.h>
 #include <string.h>
 
-#include "../common/version.h"
-#include "../common/xmalloc.h"
+/* common */
+#include "check.h"
+#include "version.h"
+#include "xmalloc.h"
 
+/* cc65 */
 #include "asmcode.h"
 #include "asmlabel.h"
-#include "check.h"
 #include "cpu.h"
 #include "error.h"
 #include "global.h"
 #include "litpool.h"
 #include "optimize.h"
+#include "segname.h"
 #include "util.h"
 #include "codegen.h"
 
 
 
 /*****************************************************************************/
-/*                                  Data                                    */
+/*                                  Data                                    */
 /*****************************************************************************/
 
 
 
 /* Compiler relative stk ptr */
-int oursp      = 0;
+int oursp      = 0;
 
 /* Current segment */
-static enum {
-    SEG_INV = -1,      /* Invalid segment */
-    SEG_CODE,
-    SEG_RODATA,
-    SEG_DATA,
-    SEG_BSS
-} CurSeg = SEG_CODE;
+segment_t CurSeg = SEG_INV;
 
 /* Segment names */
-static char* SegmentNames [4];
 static char* SegmentHints [4] = {
     "seg:code", "seg:rodata", "seg:data", "seg:bss"
 };
@@ -98,7 +94,7 @@ static void CheckLocalOffs (unsigned Offs)
     if (Offs >= 256) {
        /* Too many local vars */
                AddCodeLine (";*** Too many locals");
-       Error (ERR_TOO_MANY_LOCALS);
+       Error ("Too many local variables");
     }
 }
 
@@ -165,7 +161,7 @@ void g_preamble (void)
     /* Allow auto import for runtime library routines */
     AddCodeLine (".autoimport\ton");
 
-    /* Switch the assembler into case sensible mode */
+    /* Switch the assembler into case sensitive mode */
     AddCodeLine (".case\t\ton");
 
     /* Tell the assembler if we want to generate debug info */
@@ -185,12 +181,6 @@ void g_preamble (void)
     AddCodeLine (".endmacro");
     AddEmptyLine ();
 
-    /* Define the default names for the segments */
-    SegmentNames [SEG_CODE]    = xstrdup ("CODE");
-    SegmentNames [SEG_RODATA]  = xstrdup ("RODATA");
-    SegmentNames [SEG_DATA]    = xstrdup ("DATA");
-    SegmentNames [SEG_BSS]     = xstrdup ("BSS");
-
     /* Tell the optimizer that this is the end of the preamble */
     AddCodeHint ("end_of_preamble");
 }
@@ -256,12 +246,11 @@ void g_usebss (void)
 
 
 
-static void SegName (int Seg, const char* Name)
+static void SegName (segment_t Seg, const char* Name)
 /* Set the name of a segment */
 {
     /* Free the old name and set a new one */
-    xfree (SegmentNames [Seg]);
-    SegmentNames [Seg] = xstrdup (Name);
+    NewSegName (Seg, Name);
 
     /* If the new segment is the current segment, emit a segment directive
      * with the new name.
@@ -496,6 +485,12 @@ void g_leave (int flags, int val)
     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;
 
@@ -510,8 +505,10 @@ void g_leave (int flags, int val)
        /* Drop stackframe or leave with rts */
        k += funcargs;
        if (k == 0) {
+           AddCodeHint ("y:-");        /* Y register no longer used */
            AddCodeLine ("\trts");
        } else if (k <= 8) {
+           AddCodeHint ("y:-");        /* Y register no longer used */
            AddCodeLine ("\tjmp\tincsp%d", k);
        } else {
            CheckLocalOffs (k);
@@ -526,7 +523,10 @@ void g_leave (int flags, int val)
            /* We've a stack frame to drop */
            ldyconst (k);
            strcat (buf, "y");
-       }
+       } else {
+           /* Y register no longer used */
+           AddCodeHint ("y:-");
+       }
        if (flags & CF_CONST) {
            if ((flags & CF_TYPE) != CF_LONG) {
                /* Constant int sized value given for return code */
@@ -1050,48 +1050,81 @@ void g_putlocal (unsigned flags, int offs)
 
 
 
-void g_putind (unsigned flags, unsigned offs)
+void g_putind (unsigned Flags, unsigned Offs)
 /* Store the specified object type in the primary register at the address
  * on the top of the stack
  */
 {
-    /* We cannot currently handle more than byte sized offsets */
-    if (offs > 256 - sizeofarg (flags)) {
-       Internal ("g_putind: Large offsets not implemented");
+    /* We can handle offsets below $100 directly, larger offsets must be added
+     * to the address. Since a/x is in use, best code is achieved by adding
+     * just the high byte. Be sure to check if the low byte will overflow while
+     * while storing.
+     */
+    if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
+
+       /* Overflow - we need to add the low byte also */
+       AddCodeLine ("\tldy\t#$00");
+       AddCodeLine ("\tclc");
+       AddCodeLine ("\tpha");
+       AddCodeLine ("\tlda\t#$%02X", Offs & 0xFF);
+       AddCodeLine ("\tadc\t(sp),y");
+       AddCodeLine ("\tsta\t(sp),y");
+       AddCodeLine ("\tiny");
+               AddCodeLine ("\tlda\t#$%02X", (Offs >> 8) & 0xFF);
+       AddCodeLine ("\tadc\t(sp),y");
+       AddCodeLine ("\tsta\t(sp),y");
+       AddCodeLine ("\tpla");
+
+       /* Complete address is on stack, new offset is zero */
+       Offs = 0;
+
+    } else if ((Offs & 0xFF00) != 0) {
+
+       /* We can just add the high byte */
+       AddCodeLine ("\tldy\t#$01");
+       AddCodeLine ("\tclc");
+       AddCodeLine ("\tpha");
+       AddCodeLine ("\tlda\t#$%02X", (Offs >> 8) & 0xFF);
+       AddCodeLine ("\tadc\t(sp),y");
+       AddCodeLine ("\tsta\t(sp),y");
+       AddCodeLine ("\tpla");
+
+       /* Offset is now just the low byte */
+       Offs &= 0x00FF;
     }
 
     /* Check the size and determine operation */
-    switch (flags & CF_TYPE) {
+    switch (Flags & CF_TYPE) {
 
        case CF_CHAR:
-           if (offs) {
-               ldyconst (offs);
+           if (Offs) {
+               ldyconst (Offs);
                AddCodeLine ("\tjsr\tstaspidx");
            } else {
-               AddCodeLine ("\tjsr\tstaspp");
+               AddCodeLine ("\tjsr\tstaspp");
            }
            break;
 
        case CF_INT:
-           if (offs) {
-               ldyconst (offs);
-               AddCodeLine ("\tjsr\tstaxspidx");
+           if (Offs) {
+               ldyconst (Offs);
+               AddCodeLine ("\tjsr\tstaxspidx");
            } else {
-               AddCodeLine ("\tjsr\tstaxspp");
+               AddCodeLine ("\tjsr\tstaxspp");
            }
            break;
 
        case CF_LONG:
-           if (offs) {
-               ldyconst (offs);
-               AddCodeLine ("\tjsr\tsteaxspidx");
+           if (Offs) {
+               ldyconst (Offs);
+               AddCodeLine ("\tjsr\tsteaxspidx");
            } else {
-               AddCodeLine ("\tjsr\tsteaxspp");
+               AddCodeLine ("\tjsr\tsteaxspp");
            }
            break;
 
        default:
-           typeerror (flags);
+           typeerror (Flags);
 
     }
 
@@ -1546,7 +1579,7 @@ void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
                        AddCodeLine ("\tinc\t%s", lbuf);
                        AddCodeLine ("\tlda\t%s", lbuf);
                    } else {
-                               AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
+                               AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
                        AddCodeLine ("\tclc");
                        AddCodeLine ("\tadc\t%s", lbuf);
                        AddCodeLine ("\tsta\t%s", lbuf);
@@ -1570,24 +1603,24 @@ void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
                if (val == 1) {
                    label = GetLabel ();
                    AddCodeLine ("\tinc\t%s", lbuf);
-                   AddCodeLine ("\tbne\tL%04X", label);
+                   AddCodeLine ("\tbne\tL%04X", (int)label);
                    AddCodeLine ("\tinc\t%s+1", lbuf);
                    g_defloclabel (label);
-                   AddCodeLine ("\tlda\t%s", lbuf);            /* Hmmm... */
+                   AddCodeLine ("\tlda\t%s", lbuf);            /* Hmmm... */
                    AddCodeLine ("\tldx\t%s+1", lbuf);
                } else {
-                           AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
+                           AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
                    AddCodeLine ("\tclc");
                    AddCodeLine ("\tadc\t%s", lbuf);
                    AddCodeLine ("\tsta\t%s", lbuf);
                    if (val < 0x100) {
                        label = GetLabel ();
-                       AddCodeLine ("\tbcc\tL%04X", label);
+                       AddCodeLine ("\tbcc\tL%04X", (int)label);
                        AddCodeLine ("\tinc\t%s+1", lbuf);
                                g_defloclabel (label);
                        AddCodeLine ("\tldx\t%s+1", lbuf);
                    } else {
-                               AddCodeLine ("\tlda\t#$%02X", (val >> 8) & 0xFF);
+                               AddCodeLine ("\tlda\t#$%02X", (unsigned char)(val >> 8));
                        AddCodeLine ("\tadc\t%s+1", lbuf);
                        AddCodeLine ("\tsta\t%s+1", lbuf);
                        AddCodeLine ("\ttax");
@@ -1615,7 +1648,7 @@ void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
                    if (val == 1) {
                        AddCodeLine ("\tjsr\tladdeq1");
                    } else {
-                       AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
+                       AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
                        AddCodeLine ("\tjsr\tladdeqa");
                    }
                } else {
@@ -1654,7 +1687,7 @@ void g_addeqlocal (unsigned flags, int offs, unsigned long val)
                            AddCodeLine ("\tldx\t#$00");
                            if (flags & CF_CONST) {
                                AddCodeLine ("\tclc");
-                               AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
+                               AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
                                AddCodeLine ("\tadc\t(sp,x)");
                                AddCodeLine ("\tsta\t(sp,x)");
                            } else {
@@ -1667,7 +1700,7 @@ void g_addeqlocal (unsigned flags, int offs, unsigned long val)
                    AddCodeLine ("\tldx\t#$00");
                    if (flags & CF_CONST) {
                        AddCodeLine ("\tclc");
-                               AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
+                               AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
                        AddCodeLine ("\tadc\t(sp),y");
                        AddCodeLine ("\tsta\t(sp),y");
                    } else {
@@ -1733,14 +1766,14 @@ void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
            AddCodeLine ("\tstx\tptr1+1");
            if (offs == 0) {
                AddCodeLine ("\tldx\t#$00");
-               AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
+               AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
                AddCodeLine ("\tclc");
                AddCodeLine ("\tadc\t(ptr1,x)");
                AddCodeLine ("\tsta\t(ptr1,x)");
            } else {
                AddCodeLine ("\tldy\t#$%02X", offs);
                        AddCodeLine ("\tldx\t#$00");
-                       AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
+                       AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
                        AddCodeLine ("\tclc");
                        AddCodeLine ("\tadc\t(ptr1),y");
                        AddCodeLine ("\tsta\t(ptr1),y");
@@ -1753,13 +1786,13 @@ void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
                        AddCodeLine ("\tsta\tptr1");
                AddCodeLine ("\tstx\tptr1+1");
                AddCodeLine ("\tldy\t#$%02X", offs);
-               AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
+               AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
                AddCodeLine ("\tclc");
                AddCodeLine ("\tadc\t(ptr1),y");
                AddCodeLine ("\tsta\t(ptr1),y");
                AddCodeLine ("\tpha");
                AddCodeLine ("\tiny");
-               AddCodeLine ("\tlda\t#$%02X", (val >> 8) & 0xFF);
+               AddCodeLine ("\tlda\t#$%02X", (unsigned char)(val >> 8));
                AddCodeLine ("\tadc\t(ptr1),y");
                AddCodeLine ("\tsta\t(ptr1),y");
                AddCodeLine ("\ttax");
@@ -1803,7 +1836,7 @@ void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
                            } else {
                                AddCodeLine ("\tsec");
                                AddCodeLine ("\tlda\t%s", lbuf);
-                               AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
+                               AddCodeLine ("\tsbc\t#$%02X", (int)(val & 0xFF));
                                AddCodeLine ("\tsta\t%s", lbuf);
                            }
                        } else {
@@ -1826,17 +1859,17 @@ void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
            AddCodeLine ("\tsec");
            if (flags & CF_CONST) {
                AddCodeLine ("\tlda\t%s", lbuf);
-               AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
+               AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
                AddCodeLine ("\tsta\t%s", lbuf);
                if (val < 0x100) {
                    label = GetLabel ();
-                   AddCodeLine ("\tbcs\tL%04X", label);
+                   AddCodeLine ("\tbcs\tL%04X", (unsigned)label);
                    AddCodeLine ("\tdec\t%s+1", lbuf);
                    g_defloclabel (label);
                    AddCodeLine ("\tldx\t%s+1", lbuf);
                } else {
                    AddCodeLine ("\tlda\t%s+1", lbuf);
-                   AddCodeLine ("\tsbc\t#$%02X", (val >> 8) & 0xFF);
+                   AddCodeLine ("\tsbc\t#$%02X", (unsigned char)(val >> 8));
                    AddCodeLine ("\tsta\t%s+1", lbuf);
                    AddCodeLine ("\ttax");
                    AddCodeLine ("\tlda\t%s", lbuf);
@@ -1864,7 +1897,7 @@ void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
                    if (val == 1) {
                        AddCodeLine ("\tjsr\tlsubeq1");
                    } else {
-                       AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
+                       AddCodeLine ("\tlda\t#$%02X", (unsigned char)val);
                        AddCodeLine ("\tjsr\tlsubeqa");
                    }
                } else {
@@ -1904,7 +1937,7 @@ void g_subeqlocal (unsigned flags, int offs, unsigned long val)
                        AddCodeLine ("\tsec");
                if (flags & CF_CONST) {
                    AddCodeLine ("\tlda\t(sp),y");
-                   AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
+                   AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
                } else {
                    AddCodeLine ("\tsta\ttmp1");
                    AddCodeLine ("\tlda\t(sp),y");
@@ -1970,14 +2003,14 @@ void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
                AddCodeLine ("\tldx\t#$00");
                AddCodeLine ("\tlda\t(ptr1,x)");
                        AddCodeLine ("\tsec");
-               AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
+               AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
                AddCodeLine ("\tsta\t(ptr1,x)");
            } else {
                        AddCodeLine ("\tldy\t#$%02X", offs);
                AddCodeLine ("\tldx\t#$00");
                AddCodeLine ("\tlda\t(ptr1),y");
                AddCodeLine ("\tsec");
-               AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
+               AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
                AddCodeLine ("\tsta\t(ptr1),y");
            }
            break;
@@ -1990,12 +2023,12 @@ void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
                AddCodeLine ("\tldy\t#$%02X", offs);
                AddCodeLine ("\tlda\t(ptr1),y");
                AddCodeLine ("\tsec");
-               AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
+               AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
                AddCodeLine ("\tsta\t(ptr1),y");
                AddCodeLine ("\tpha");
                AddCodeLine ("\tiny");
                AddCodeLine ("\tlda\t(ptr1),y");
-               AddCodeLine ("\tsbc\t#$%02X", (val >> 8) & 0xFF);
+               AddCodeLine ("\tsbc\t#$%02X", (unsigned char)(val >> 8));
                AddCodeLine ("\tsta\t(ptr1),y");
                AddCodeLine ("\ttax");
                AddCodeLine ("\tpla");
@@ -2143,15 +2176,15 @@ void g_cmp (unsigned flags, unsigned long val)
 
        case CF_CHAR:
            if (flags & CF_FORCECHAR) {
-               AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
+               AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
                break;
            }
            /* FALLTHROUGH */
 
        case CF_INT:
-           AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
+           AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
                    AddCodeLine ("\tbne\t*+4");
-           AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
+           AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
            break;
 
         case CF_LONG:
@@ -2415,11 +2448,13 @@ void g_case (unsigned flags, unsigned label, unsigned long val)
 
        case CF_CHAR:
        case CF_INT:
-           AddCodeLine ("\t.word\t$%04X, L%04X", val & 0xFFFF, label & 0xFFFF);
+           AddCodeLine ("\t.word\t$%04X, L%04X",
+                        (unsigned)(val & 0xFFFF),
+                        (unsigned)(label & 0xFFFF));
                    break;
 
        case CF_LONG:
-           AddCodeLine ("\t.dword\t$%08X", val);
+           AddCodeLine ("\t.dword\t$%08lX", val);
            AddCodeLine ("\t.word\tL%04X", label & 0xFFFF);
            break;
 
@@ -2472,14 +2507,30 @@ void g_space (int space)
 /* Create or drop space on the stack */
 {
     if (space < 0) {
-       mod_internal (-space, "inc", "addy");
+       mod_internal (-space, "inc", "addy");
     } else if (space > 0) {
-       mod_internal (space, "dec", "suby");
+       mod_internal (space, "dec", "suby");
     }
 }
 
 
 
+void g_cstackcheck (void)
+/* Check for a C stack overflow */
+{
+    AddCodeLine ("\tjsr\tcstkchk");
+}
+
+
+
+void g_stackcheck (void)
+/* Check for a stack overflow */
+{
+    AddCodeLine ("\tjsr\tstkchk");
+}
+
+
+
 void g_add (unsigned flags, unsigned long val)
 /* Primary = TOS + Primary */
 {
@@ -2491,7 +2542,7 @@ void g_add (unsigned flags, unsigned long val)
     };
 
     if (flags & CF_CONST) {
-       flags &= ~CF_FORCECHAR; // Handle chars as ints
+       flags &= ~CF_FORCECHAR; /* Handle chars as ints */
        g_push (flags & ~CF_CONST, 0);
     }
     oper (flags, val, ops);
@@ -2510,7 +2561,7 @@ void g_sub (unsigned flags, unsigned long val)
     };
 
     if (flags & CF_CONST) {
-       flags &= ~CF_FORCECHAR; // Handle chars as ints
+       flags &= ~CF_FORCECHAR; /* Handle chars as ints */
        g_push (flags & ~CF_CONST, 0);
     }
     oper (flags, val, ops);
@@ -2603,7 +2654,7 @@ void g_mul (unsigned flags, unsigned long val)
        /* 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
+       flags &= ~CF_FORCECHAR; /* Handle chars as ints */
        g_push (flags & ~CF_CONST, 0);
 
     }
@@ -2633,7 +2684,7 @@ void g_div (unsigned flags, unsigned long val)
        /* Generate a division */
        if (flags & CF_CONST) {
            /* lhs is not on stack */
-           flags &= ~CF_FORCECHAR;     // Handle chars as ints
+           flags &= ~CF_FORCECHAR;     /* Handle chars as ints */
            g_push (flags & ~CF_CONST, 0);
        }
        oper (flags, val, ops);
@@ -2661,7 +2712,7 @@ void g_mod (unsigned flags, unsigned long val)
        /* Do it the hard way... */
        if (flags & CF_CONST) {
            /* lhs is not on stack */
-           flags &= ~CF_FORCECHAR;     // Handle chars as ints
+           flags &= ~CF_FORCECHAR;     /* Handle chars as ints */
            g_push (flags & ~CF_CONST, 0);
        }
        oper (flags, val, ops);
@@ -2690,7 +2741,7 @@ void g_or (unsigned flags, unsigned long val)
            case CF_CHAR:
                if (flags & CF_FORCECHAR) {
                    if ((val & 0xFF) != 0xFF) {
-                               AddCodeLine ("\tora\t#$%02X", val & 0xFF);
+                               AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
                    }
                    return;
                }
@@ -2698,14 +2749,14 @@ void g_or (unsigned flags, unsigned long val)
 
            case CF_INT:
                if (val <= 0xFF) {
-                   AddCodeLine ("\tora\t#$%02X", val & 0xFF);
+                   AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
                    return;
                }
                break;
 
            case CF_LONG:
                if (val <= 0xFF) {
-                   AddCodeLine ("\tora\t#$%02X", val & 0xFF);
+                   AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
                    return;
                }
                break;
@@ -2748,7 +2799,7 @@ void g_xor (unsigned flags, unsigned long val)
            case CF_CHAR:
                if (flags & CF_FORCECHAR) {
                    if ((val & 0xFF) != 0) {
-                               AddCodeLine ("\teor\t#$%02X", val & 0xFF);
+                               AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
                    }
                    return;
                }
@@ -2757,13 +2808,13 @@ void g_xor (unsigned flags, unsigned long val)
            case CF_INT:
                if (val <= 0xFF) {
                    if (val != 0) {
-                       AddCodeLine ("\teor\t#$%02X", val);
+                       AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
                    }
                    return;
                } else if ((val & 0xFF) == 0) {
                    AddCodeLine ("\tpha");
                    AddCodeLine ("\ttxa");
-                   AddCodeLine ("\teor\t#$%02X", (val >> 8) & 0xFF);
+                   AddCodeLine ("\teor\t#$%02X", (unsigned char)(val >> 8));
                    AddCodeLine ("\ttax");
                    AddCodeLine ("\tpla");
                    return;
@@ -2773,7 +2824,7 @@ void g_xor (unsigned flags, unsigned long val)
            case CF_LONG:
                if (val <= 0xFF) {
                    if (val != 0) {
-                               AddCodeLine ("\teor\t#$%02X", val & 0xFF);
+                               AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
                    }
                    return;
                }
@@ -2815,7 +2866,7 @@ void g_and (unsigned flags, unsigned long val)
 
            case CF_CHAR:
                if (flags & CF_FORCECHAR) {
-                   AddCodeLine ("\tand\t#$%02X", val & 0xFF);
+                   AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
                    return;
                }
                /* FALLTHROUGH */
@@ -2826,23 +2877,23 @@ void g_and (unsigned flags, unsigned long val)
                        if (val == 0) {
                            ldaconst (0);
                        } else if (val != 0xFF) {
-                           AddCodeLine ("\tand\t#$%02X", val & 0xFF);
+                           AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
                        }
                    } else if ((val & 0xFF00) == 0xFF00) {
-                       AddCodeLine ("\tand\t#$%02X", val & 0xFF);
+                       AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
                    } else if ((val & 0x00FF) == 0x0000) {
                        AddCodeLine ("\ttxa");
-                       AddCodeLine ("\tand\t#$%02X", (val >> 8) & 0xFF);
+                       AddCodeLine ("\tand\t#$%02X", (unsigned char)(val >> 8));
                        AddCodeLine ("\ttax");
                        ldaconst (0);
                    } else {
                        AddCodeLine ("\ttay");
                        AddCodeLine ("\ttxa");
-                       AddCodeLine ("\tand\t#$%02X", (val >> 8) & 0xFF);
+                       AddCodeLine ("\tand\t#$%02X", (unsigned char)(val >> 8));
                        AddCodeLine ("\ttax");
                        AddCodeLine ("\ttya");
                        if ((val & 0x00FF) != 0x00FF) {
-                           AddCodeLine ("\tand\t#$%02X", val & 0xFF);
+                           AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
                        }
                    }
                }
@@ -2854,7 +2905,7 @@ void g_and (unsigned flags, unsigned long val)
                    AddCodeLine ("\tstx\tsreg+1");
                    AddCodeLine ("\tstx\tsreg");
                    if ((val & 0xFF) != 0xFF) {
-                        AddCodeLine ("\tand\t#$%02X", val & 0xFF);
+                        AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
                    }
                    return;
                } else if (val == 0xFF00) {
@@ -3124,7 +3175,7 @@ void g_inc (unsigned flags, unsigned long val)
                    }
                } else {
                    AddCodeLine ("\tclc");
-                   AddCodeLine ("\tadc\t#$%02X", val & 0xFF);
+                   AddCodeLine ("\tadc\t#$%02X", (unsigned char)val);
                }
                break;
            }
@@ -3140,7 +3191,7 @@ void g_inc (unsigned flags, unsigned long val)
            } else if (FavourSize) {
                /* Use jsr calls */
                if (val <= 8) {
-                   AddCodeLine ("\tjsr\tincax%u", val);
+                   AddCodeLine ("\tjsr\tincax%lu", val);
                } else if (val <= 255) {
                    ldyconst (val);
                    AddCodeLine ("\tjsr\tincaxy");
@@ -3200,6 +3251,11 @@ void g_inc (unsigned flags, unsigned long val)
 void g_dec (unsigned flags, unsigned long val)
 /* Decrement the primary register by a given number */
 {
+    /* Don't dec by zero */
+    if (val == 0) {
+       return;
+    }
+
     /* Generate code for the supported types */
     flags &= ~CF_CONST;
     switch (flags & CF_TYPE) {
@@ -3212,7 +3268,7 @@ void g_dec (unsigned flags, unsigned long val)
                    }
                } else {
                    AddCodeLine ("\tsec");
-                   AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
+                   AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
                }
                break;
            }
@@ -3273,16 +3329,16 @@ void g_eq (unsigned flags, unsigned long val)
 
            case CF_CHAR:
                if (flags & CF_FORCECHAR) {
-                   AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
+                   AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
                    AddCodeLine ("\tjsr\tbooleq");
                    return;
                }
                /* FALLTHROUGH */
 
            case CF_INT:
-               AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
+               AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
                        AddCodeLine ("\tbne\t*+4");
-               AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
+               AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
                AddCodeLine ("\tjsr\tbooleq");
                return;
 
@@ -3326,16 +3382,16 @@ void g_ne (unsigned flags, unsigned long val)
 
            case CF_CHAR:
                if (flags & CF_FORCECHAR) {
-                   AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
+                   AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
                    AddCodeLine ("\tjsr\tboolne");
                    return;
                }
                /* FALLTHROUGH */
 
            case CF_INT:
-               AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
+               AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
                AddCodeLine ("\tbne\t*+4");
-               AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
+               AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
                AddCodeLine ("\tjsr\tboolne");
                return;
 
@@ -3376,7 +3432,7 @@ void g_lt (unsigned flags, unsigned long val)
 
        /* Give a warning in some special cases */
        if ((flags & CF_UNSIGNED) && val == 0) {
-           Warning (WARN_COND_NEVER_TRUE);
+           Warning ("Condition is never true");
        }
 
        /* Look at the type */
@@ -3384,7 +3440,7 @@ void g_lt (unsigned flags, unsigned long val)
 
            case CF_CHAR:
                if (flags & CF_FORCECHAR) {
-                   AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
+                   AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
                    if (flags & CF_UNSIGNED) {
                        AddCodeLine ("\tjsr\tboolult");
                    } else {
@@ -3405,9 +3461,9 @@ void g_lt (unsigned flags, unsigned long val)
                }
                /* Direct code only for unsigned data types */
                if (flags & CF_UNSIGNED) {
-                   AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
+                   AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
                            AddCodeLine ("\tbne\t*+4");
-                   AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
+                   AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
                    AddCodeLine ("\tjsr\tboolult");
                    return;
                }
@@ -3454,7 +3510,7 @@ void g_le (unsigned flags, unsigned long val)
 
            case CF_CHAR:
                if (flags & CF_FORCECHAR) {
-                   AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
+                   AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
                    if (flags & CF_UNSIGNED) {
                        AddCodeLine ("\tjsr\tboolule");
                    } else {
@@ -3466,9 +3522,9 @@ void g_le (unsigned flags, unsigned long val)
 
            case CF_INT:
                if (flags & CF_UNSIGNED) {
-                   AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
+                   AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
                            AddCodeLine ("\tbne\t*+4");
-                   AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
+                   AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
                    AddCodeLine ("\tjsr\tboolule");
                    return;
                }
@@ -3515,7 +3571,7 @@ void g_gt (unsigned flags, unsigned long val)
 
            case CF_CHAR:
                if (flags & CF_FORCECHAR) {
-                   AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
+                   AddCodeLine ("\tcmp\t#$%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
@@ -3544,9 +3600,9 @@ void g_gt (unsigned flags, unsigned long val)
                        AddCodeLine ("\tora\ttmp1");
                        AddCodeLine ("\tjsr\tboolne");
                    } else {
-                               AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
+                               AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
                        AddCodeLine ("\tbne\t*+4");
-                       AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
+                       AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
                                AddCodeLine ("\tjsr\tboolugt");
                    }
                    return;
@@ -3591,7 +3647,7 @@ void g_ge (unsigned flags, unsigned long val)
 
        /* Give a warning in some special cases */
        if ((flags & CF_UNSIGNED) && val == 0) {
-           Warning (WARN_COND_ALWAYS_TRUE);
+           Warning ("Condition is always true");
        }
 
        /* Look at the type */
@@ -3599,7 +3655,7 @@ void g_ge (unsigned flags, unsigned long val)
 
            case CF_CHAR:
                if (flags & CF_FORCECHAR) {
-                   AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
+                   AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
                    if (flags & CF_UNSIGNED) {
                        AddCodeLine ("\tjsr\tbooluge");
                    } else {
@@ -3611,9 +3667,9 @@ void g_ge (unsigned flags, unsigned long val)
 
            case CF_INT:
                if (flags & CF_UNSIGNED) {
-                           AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
+                           AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
                            AddCodeLine ("\tbne\t*+4");
-                   AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
+                   AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
                    AddCodeLine ("\tjsr\tbooluge");
                    return;
                }
@@ -3640,7 +3696,7 @@ void g_ge (unsigned flags, unsigned long val)
 
 
 /*****************************************************************************/
-/*                        Allocating static storage                         */
+/*                        Allocating static storage                         */
 /*****************************************************************************/