X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fcc65%2Fcodegen.c;h=81f4ecfb3dcb8ffb5cfa350187e65466dcdbfffc;hb=32164ea8e5222e3c9f40980dca307ac96c84a922;hp=88cebe9a4b734ead469354eb1fb8d466a06a88f9;hpb=c76e14f9f5341df20f92980caae5f41460303aa4;p=cc65 diff --git a/src/cc65/codegen.c b/src/cc65/codegen.c index 88cebe9a4..81f4ecfb3 100644 --- a/src/cc65/codegen.c +++ b/src/cc65/codegen.c @@ -1,8 +1,8 @@ /*****************************************************************************/ /* */ -/* codegen.c */ +/* codegen.c */ /* */ -/* 6502 code generator */ +/* 6502 code generator */ /* */ /* */ /* */ @@ -62,7 +62,7 @@ /*****************************************************************************/ -/* Helpers */ +/* Helpers */ /*****************************************************************************/ @@ -79,8 +79,8 @@ static void CheckLocalOffs (unsigned Offs) /* Check the offset into the stack for 8bit range */ { if (Offs >= 256) { - /* Too many local vars */ - Error ("Too many local variables"); + /* Too many local vars */ + Error ("Too many local variables"); } } @@ -88,41 +88,41 @@ static void CheckLocalOffs (unsigned Offs) static const char* GetLabelName (unsigned Flags, unsigned long Label, long Offs) { - static char Buf [256]; /* Label name */ + static char Buf [256]; /* Label name */ /* Create the correct label name */ switch (Flags & CF_ADDRMASK) { - case CF_STATIC: - /* Static memory cell */ - 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 */ - 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 */ - xsprintf (Buf, sizeof (Buf), "$%04X", (int)((Label+Offs) & 0xFFFF)); - break; - - case CF_REGVAR: - /* Variable in register bank */ - xsprintf (Buf, sizeof (Buf), "regbank+%u", (unsigned)((Label+Offs) & 0xFFFF)); - break; - - default: - Internal ("Invalid address flags: %04X", Flags); + case CF_STATIC: + /* Static memory cell */ + 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 */ + 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 */ + xsprintf (Buf, sizeof (Buf), "$%04X", (int)((Label+Offs) & 0xFFFF)); + break; + + case CF_REGVAR: + /* Variable in register bank */ + xsprintf (Buf, sizeof (Buf), "regbank+%u", (unsigned)((Label+Offs) & 0xFFFF)); + break; + + default: + Internal ("Invalid address flags: %04X", Flags); } /* Return a pointer to the static buffer */ @@ -132,7 +132,7 @@ static const char* GetLabelName (unsigned Flags, unsigned long Label, long Offs) /*****************************************************************************/ -/* Pre- and postamble */ +/* Pre- and postamble */ /*****************************************************************************/ @@ -147,12 +147,12 @@ void g_preamble (void) /* Identify the compiler version */ AddTextLine (";"); AddTextLine ("; File generated by cc65 v %u.%u.%u", - VER_MAJOR, VER_MINOR, VER_PATCH); + VER_MAJOR, VER_MINOR, VER_PATCH); AddTextLine (";"); /* Insert some object file options */ AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"", - VER_MAJOR, VER_MINOR, VER_PATCH); + VER_MAJOR, VER_MINOR, VER_PATCH); /* If we're producing code for some other CPU, switch the command set */ switch (CPU) { @@ -161,6 +161,7 @@ void g_preamble (void) case CPU_65SC02: AddTextLine ("\t.setcpu\t\t\"65SC02\""); break; case CPU_65C02: AddTextLine ("\t.setcpu\t\t\"65C02\""); break; case CPU_65816: AddTextLine ("\t.setcpu\t\t\"65816\""); break; + case CPU_HUC6280: AddTextLine ("\t.setcpu\t\t\"HUC6280\""); break; default: Internal ("Unknown CPU: %d", CPU); } @@ -189,17 +190,17 @@ void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime) /* If debug info is enabled, place a file info into the source */ { if (DebugInfo) { - /* We have to place this into the global text segment, so it will - * appear before all .dbg line statements. - */ - TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime); + /* 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); } } /*****************************************************************************/ -/* Segment support */ +/* Segment support */ /*****************************************************************************/ @@ -234,20 +235,20 @@ void g_segname (segment_t Seg) /* Emit a segment directive for the data style segments */ DataSeg* S; 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; + 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\"", GetSegName (Seg)); + DS_AddLine (S, ".segment\t\"%s\"", GetSegName (Seg)); } } /*****************************************************************************/ -/* Code */ +/* Code */ /*****************************************************************************/ @@ -257,22 +258,22 @@ unsigned sizeofarg (unsigned flags) { switch (flags & CF_TYPE) { - case CF_CHAR: - return (flags & CF_FORCECHAR)? 1 : 2; + case CF_CHAR: + return (flags & CF_FORCECHAR)? 1 : 2; - case CF_INT: - return 2; + case CF_INT: + return 2; - case CF_LONG: - return 4; + case CF_LONG: + return 4; case CF_FLOAT: return 4; - default: - typeerror (flags); - /* NOTREACHED */ - return 2; + default: + typeerror (flags); + /* NOTREACHED */ + return 2; } } @@ -307,14 +308,14 @@ static unsigned MakeByteOffs (unsigned Flags, unsigned Offs) */ unsigned O = Offs & ~0xFFU; if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) { - /* We need to add the low byte also */ - O += Offs & 0xFF; + /* We need to add the low byte also */ + O += Offs & 0xFF; } /* Do the correction if we need one */ if (O != 0) { - g_inc (CF_INT | CF_CONST, O); - Offs -= O; + g_inc (CF_INT | CF_CONST, O); + Offs -= O; } /* Return the new offset */ @@ -324,7 +325,7 @@ static unsigned MakeByteOffs (unsigned Flags, unsigned Offs) /*****************************************************************************/ -/* Functions handling local labels */ +/* Functions handling local labels */ /*****************************************************************************/ @@ -346,7 +347,7 @@ void g_defdatalabel (unsigned label) /*****************************************************************************/ -/* Functions handling global labels */ +/* Functions handling global labels */ /*****************************************************************************/ @@ -364,9 +365,9 @@ void g_defexport (const char* Name, int ZP) /* Export the given label */ { if (ZP) { - AddTextLine ("\t.exportzp\t_%s", Name); + AddTextLine ("\t.exportzp\t_%s", Name); } else { - AddTextLine ("\t.export\t\t_%s", Name); + AddTextLine ("\t.export\t\t_%s", Name); } } @@ -376,9 +377,9 @@ void g_defimport (const char* Name, int ZP) /* Import the given label */ { if (ZP) { - AddTextLine ("\t.importzp\t_%s", Name); + AddTextLine ("\t.importzp\t_%s", Name); } else { - AddTextLine ("\t.import\t\t_%s", Name); + AddTextLine ("\t.import\t\t_%s", Name); } } @@ -393,7 +394,7 @@ void g_importmainargs (void) /*****************************************************************************/ -/* Load functions for various registers */ +/* Load functions for various registers */ /*****************************************************************************/ @@ -423,7 +424,7 @@ static void ldyconst (unsigned val) /*****************************************************************************/ -/* Function entry and exit */ +/* Function entry and exit */ /*****************************************************************************/ @@ -440,11 +441,11 @@ void g_enter (unsigned flags, unsigned argsize) /* Function prologue */ { if ((flags & CF_FIXARGC) != 0) { - /* Just remember the argument size for the leave */ - funcargs = argsize; + /* Just remember the argument size for the leave */ + funcargs = argsize; } else { - funcargs = -1; - AddCodeLine ("jsr enter"); + funcargs = -1; + AddCodeLine ("jsr enter"); } } @@ -459,34 +460,34 @@ void g_leave (void) /* 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) { + /* 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"); - } + if (ToDrop <= 8) { + AddCodeLine ("jsr incsp%d", k); + } else { + ldyconst (ToDrop); + AddCodeLine ("jsr addysp"); + } k -= ToDrop; - } + } } else { - if (k == 0) { - /* Nothing to drop */ - AddCodeLine ("jsr leave"); - } else { - /* We've a stack frame to drop */ + if (k == 0) { + /* Nothing to drop */ + AddCodeLine ("jsr leave"); + } else { + /* We've a stack frame to drop */ while (k > 255) { ldyconst (255); AddCodeLine ("jsr addysp"); k -= 255; } - ldyconst (k); - AddCodeLine ("jsr leavey"); - } + ldyconst (k); + AddCodeLine ("jsr leavey"); + } } /* Add the final rts */ @@ -496,7 +497,7 @@ void g_leave (void) /*****************************************************************************/ -/* Register variables */ +/* Register variables */ /*****************************************************************************/ @@ -511,7 +512,7 @@ void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes) /* Generate code */ if (Bytes == 1) { - if (CodeSizeFactor < 165) { + if (IS_Get (&CodeSizeFactor) < 165) { ldyconst (StackOffs); ldxconst (RegOffs); AddCodeLine ("jsr regswap1"); @@ -547,28 +548,28 @@ void g_save_regvars (int RegOffs, unsigned Bytes) /* Don't loop for up to two bytes */ if (Bytes == 1) { - AddCodeLine ("lda regbank%+d", RegOffs); - AddCodeLine ("jsr pusha"); + AddCodeLine ("lda regbank%+d", RegOffs); + AddCodeLine ("jsr pusha"); } else if (Bytes == 2) { - AddCodeLine ("lda regbank%+d", RegOffs); - AddCodeLine ("ldx regbank%+d", RegOffs+1); - AddCodeLine ("jsr pushax"); + AddCodeLine ("lda regbank%+d", RegOffs); + AddCodeLine ("ldx regbank%+d", RegOffs+1); + AddCodeLine ("jsr pushax"); } else { - /* More than two bytes - loop */ - unsigned Label = GetLocalLabel (); - g_space (Bytes); - ldyconst (Bytes - 1); - ldxconst (Bytes); - g_defcodelabel (Label); - AddCodeLine ("lda regbank%+d,x", RegOffs-1); - AddCodeLine ("sta (sp),y"); - AddCodeLine ("dey"); - AddCodeLine ("dex"); - AddCodeLine ("bne %s", LocalLabelName (Label)); + /* More than two bytes - loop */ + unsigned Label = GetLocalLabel (); + g_space (Bytes); + ldyconst (Bytes - 1); + ldxconst (Bytes); + g_defcodelabel (Label); + AddCodeLine ("lda regbank%+d,x", RegOffs-1); + AddCodeLine ("sta (sp),y"); + AddCodeLine ("dey"); + AddCodeLine ("dex"); + AddCodeLine ("bne %s", LocalLabelName (Label)); } @@ -588,30 +589,30 @@ void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes) /* Don't loop for up to two bytes */ if (Bytes == 1) { - ldyconst (StackOffs); - AddCodeLine ("lda (sp),y"); - AddCodeLine ("sta regbank%+d", RegOffs); + ldyconst (StackOffs); + AddCodeLine ("lda (sp),y"); + AddCodeLine ("sta regbank%+d", RegOffs); } else if (Bytes == 2) { - ldyconst (StackOffs); - AddCodeLine ("lda (sp),y"); - AddCodeLine ("sta regbank%+d", RegOffs); - AddCodeLine ("iny"); - AddCodeLine ("lda (sp),y"); - AddCodeLine ("sta regbank%+d", RegOffs+1); - - } else if (Bytes == 3 && CodeSizeFactor >= 133) { - - ldyconst (StackOffs); - AddCodeLine ("lda (sp),y"); - AddCodeLine ("sta regbank%+d", RegOffs); - AddCodeLine ("iny"); - AddCodeLine ("lda (sp),y"); - AddCodeLine ("sta regbank%+d", RegOffs+1); - AddCodeLine ("iny"); - AddCodeLine ("lda (sp),y"); - AddCodeLine ("sta regbank%+d", RegOffs+2); + ldyconst (StackOffs); + AddCodeLine ("lda (sp),y"); + AddCodeLine ("sta regbank%+d", RegOffs); + AddCodeLine ("iny"); + AddCodeLine ("lda (sp),y"); + AddCodeLine ("sta regbank%+d", RegOffs+1); + + } else if (Bytes == 3 && IS_Get (&CodeSizeFactor) >= 133) { + + ldyconst (StackOffs); + AddCodeLine ("lda (sp),y"); + AddCodeLine ("sta regbank%+d", RegOffs); + AddCodeLine ("iny"); + AddCodeLine ("lda (sp),y"); + AddCodeLine ("sta regbank%+d", RegOffs+1); + AddCodeLine ("iny"); + AddCodeLine ("lda (sp),y"); + AddCodeLine ("sta regbank%+d", RegOffs+2); } else if (StackOffs <= RegOffs) { @@ -619,30 +620,30 @@ void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes) * register bank and the stack offset allows us to generate short * code that uses just one index register. */ - unsigned Label = GetLocalLabel (); - ldyconst (StackOffs); - g_defcodelabel (Label); - AddCodeLine ("lda (sp),y"); - AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs); - AddCodeLine ("iny"); - AddCodeLine ("cpy #$%02X", StackOffs + Bytes); - AddCodeLine ("bne %s", LocalLabelName (Label)); + unsigned Label = GetLocalLabel (); + ldyconst (StackOffs); + g_defcodelabel (Label); + AddCodeLine ("lda (sp),y"); + AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs); + AddCodeLine ("iny"); + AddCodeLine ("cpy #$%02X", StackOffs + Bytes); + AddCodeLine ("bne %s", LocalLabelName (Label)); } else { /* Ok, this is the generic code. We need to save X because the * caller will only save A. */ - unsigned Label = GetLocalLabel (); + unsigned Label = GetLocalLabel (); AddCodeLine ("stx tmp1"); - ldyconst (StackOffs + Bytes - 1); + ldyconst (StackOffs + Bytes - 1); ldxconst (Bytes - 1); - g_defcodelabel (Label); - AddCodeLine ("lda (sp),y"); - AddCodeLine ("sta regbank%+d,x", RegOffs); - AddCodeLine ("dey"); + g_defcodelabel (Label); + AddCodeLine ("lda (sp),y"); + AddCodeLine ("sta regbank%+d,x", RegOffs); + AddCodeLine ("dey"); AddCodeLine ("dex"); - AddCodeLine ("bpl %s", LocalLabelName (Label)); + AddCodeLine ("bpl %s", LocalLabelName (Label)); AddCodeLine ("ldx tmp1"); } @@ -651,7 +652,7 @@ void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes) /*****************************************************************************/ -/* Fetching memory cells */ +/* Fetching memory cells */ /*****************************************************************************/ @@ -665,77 +666,77 @@ void g_getimmed (unsigned Flags, unsigned long Val, long Offs) if ((Flags & CF_CONST) != 0) { - /* Numeric constant */ - switch (Flags & CF_TYPE) { - - case CF_CHAR: - if ((Flags & CF_FORCECHAR) != 0) { - ldaconst (Val); - break; - } - /* FALL THROUGH */ - case CF_INT: - ldxconst ((Val >> 8) & 0xFF); - ldaconst (Val & 0xFF); - break; - - case CF_LONG: - /* 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); - break; - - } + /* Numeric constant */ + switch (Flags & CF_TYPE) { + + case CF_CHAR: + if ((Flags & CF_FORCECHAR) != 0) { + ldaconst (Val); + break; + } + /* FALL THROUGH */ + case CF_INT: + ldxconst ((Val >> 8) & 0xFF); + ldaconst (Val & 0xFF); + break; + + case CF_LONG: + /* 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); + break; + + } } else { - /* Some sort of label */ - const char* Label = GetLabelName (Flags, Val, Offs); + /* Some sort of label */ + const char* Label = GetLabelName (Flags, Val, Offs); - /* Load the address into the primary */ - AddCodeLine ("lda #<(%s)", Label); - AddCodeLine ("ldx #>(%s)", Label); + /* Load the address into the primary */ + AddCodeLine ("lda #<(%s)", Label); + AddCodeLine ("ldx #>(%s)", Label); } } @@ -751,49 +752,49 @@ void g_getstatic (unsigned flags, unsigned long label, long offs) /* Check the size and generate the correct load operation */ switch (flags & CF_TYPE) { - case CF_CHAR: - if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) { - AddCodeLine ("lda %s", lbuf); /* load A from the label */ - } else { - ldxconst (0); - AddCodeLine ("lda %s", lbuf); /* load A from the label */ - if (!(flags & CF_UNSIGNED)) { - /* Must sign extend */ - unsigned L = GetLocalLabel (); - AddCodeLine ("bpl %s", LocalLabelName (L)); - AddCodeLine ("dex"); - g_defcodelabel (L); - } - } - break; - - case CF_INT: - AddCodeLine ("lda %s", lbuf); - if (flags & CF_TEST) { - AddCodeLine ("ora %s+1", lbuf); - } else { - AddCodeLine ("ldx %s+1", lbuf); - } - break; - - case CF_LONG: - if (flags & CF_TEST) { - AddCodeLine ("lda %s+3", lbuf); - AddCodeLine ("ora %s+2", lbuf); - AddCodeLine ("ora %s+1", lbuf); - AddCodeLine ("ora %s+0", lbuf); - } else { - 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; - - default: - typeerror (flags); + case CF_CHAR: + if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) { + AddCodeLine ("lda %s", lbuf); /* load A from the label */ + } else { + ldxconst (0); + AddCodeLine ("lda %s", lbuf); /* load A from the label */ + if (!(flags & CF_UNSIGNED)) { + /* Must sign extend */ + unsigned L = GetLocalLabel (); + AddCodeLine ("bpl %s", LocalLabelName (L)); + AddCodeLine ("dex"); + g_defcodelabel (L); + } + } + break; + + case CF_INT: + AddCodeLine ("lda %s", lbuf); + if (flags & CF_TEST) { + AddCodeLine ("ora %s+1", lbuf); + } else { + AddCodeLine ("ldx %s+1", lbuf); + } + break; + + case CF_LONG: + if (flags & CF_TEST) { + AddCodeLine ("lda %s+3", lbuf); + AddCodeLine ("ora %s+2", lbuf); + AddCodeLine ("ora %s+1", lbuf); + AddCodeLine ("ora %s+0", lbuf); + } else { + 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; + + default: + typeerror (flags); } } @@ -807,43 +808,46 @@ void g_getlocal (unsigned flags, int offs) CheckLocalOffs (offs); switch (flags & CF_TYPE) { - case CF_CHAR: - if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) { - ldyconst (offs); - AddCodeLine ("lda (sp),y"); - } else { - ldyconst (offs); - AddCodeLine ("ldx #$00"); - AddCodeLine ("lda (sp),y"); - if ((flags & CF_UNSIGNED) == 0) { - unsigned L = GetLocalLabel(); - AddCodeLine ("bpl %s", LocalLabelName (L)); - AddCodeLine ("dex"); - g_defcodelabel (L); - } - } - break; - - case CF_INT: - CheckLocalOffs (offs + 1); - if (flags & CF_TEST) { - ldyconst (offs + 1); - AddCodeLine ("lda (sp),y"); - AddCodeLine ("dey"); - AddCodeLine ("ora (sp),y"); - } else { - ldyconst (offs+1); - AddCodeLine ("jsr ldaxysp"); - } - break; - - case CF_LONG: - ldyconst (offs+3); - AddCodeLine ("jsr ldeaxysp"); - break; - - default: - typeerror (flags); + case CF_CHAR: + if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) { + ldyconst (offs); + AddCodeLine ("lda (sp),y"); + } else { + ldyconst (offs); + AddCodeLine ("ldx #$00"); + AddCodeLine ("lda (sp),y"); + if ((flags & CF_UNSIGNED) == 0) { + unsigned L = GetLocalLabel(); + AddCodeLine ("bpl %s", LocalLabelName (L)); + AddCodeLine ("dex"); + g_defcodelabel (L); + } + } + break; + + case CF_INT: + CheckLocalOffs (offs + 1); + if (flags & CF_TEST) { + ldyconst (offs + 1); + AddCodeLine ("lda (sp),y"); + AddCodeLine ("dey"); + AddCodeLine ("ora (sp),y"); + } else { + ldyconst (offs+1); + AddCodeLine ("jsr ldaxysp"); + } + break; + + case CF_LONG: + ldyconst (offs+3); + AddCodeLine ("jsr ldeaxysp"); + if (flags & CF_TEST) { + g_test (flags); + } + break; + + default: + typeerror (flags); } } @@ -863,41 +867,41 @@ void g_getind (unsigned flags, unsigned offs) /* Handle the indirect fetch */ switch (flags & CF_TYPE) { - case CF_CHAR: - /* Character sized */ - 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 ("sta ptr1"); - AddCodeLine ("stx ptr1+1"); - AddCodeLine ("lda (ptr1),y"); - AddCodeLine ("iny"); - AddCodeLine ("ora (ptr1),y"); - } else { - ldyconst (offs+1); - AddCodeLine ("jsr ldaxidx"); - } - break; - - case CF_LONG: - ldyconst (offs+3); - AddCodeLine ("jsr ldeaxidx"); - if (flags & CF_TEST) { - AddCodeLine ("jsr tsteax"); - } - break; - - default: - typeerror (flags); + case CF_CHAR: + /* Character sized */ + 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 ("sta ptr1"); + AddCodeLine ("stx ptr1+1"); + AddCodeLine ("lda (ptr1),y"); + AddCodeLine ("iny"); + AddCodeLine ("ora (ptr1),y"); + } else { + ldyconst (offs+1); + AddCodeLine ("jsr ldaxidx"); + } + break; + + case CF_LONG: + ldyconst (offs+3); + AddCodeLine ("jsr ldeaxidx"); + if (flags & CF_TEST) { + g_test (flags); + } + break; + + default: + typeerror (flags); } } @@ -912,30 +916,30 @@ void g_leasp (int offs) /* For value 0 we do direct code */ if (offs == 0) { - AddCodeLine ("lda sp"); - AddCodeLine ("ldx sp+1"); + AddCodeLine ("lda sp"); + AddCodeLine ("ldx sp+1"); } else { - if (CodeSizeFactor < 300) { - ldaconst (offs); /* Load A with offset value */ - AddCodeLine ("jsr leaasp"); /* Load effective address */ - } else { - unsigned L = GetLocalLabel (); - if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && offs == 1) { - AddCodeLine ("lda sp"); - AddCodeLine ("ldx sp+1"); - AddCodeLine ("ina"); - AddCodeLine ("bne %s", LocalLabelName (L)); - AddCodeLine ("inx"); - } else { - ldaconst (offs); - AddCodeLine ("clc"); - AddCodeLine ("ldx sp+1"); - AddCodeLine ("adc sp"); - AddCodeLine ("bcc %s", LocalLabelName (L)); - AddCodeLine ("inx"); - } - g_defcodelabel (L); - } + if (IS_Get (&CodeSizeFactor) < 300) { + ldaconst (offs); /* Load A with offset value */ + AddCodeLine ("jsr leaasp"); /* Load effective address */ + } else { + unsigned L = GetLocalLabel (); + if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && offs == 1) { + AddCodeLine ("lda sp"); + AddCodeLine ("ldx sp+1"); + AddCodeLine ("ina"); + AddCodeLine ("bne %s", LocalLabelName (L)); + AddCodeLine ("inx"); + } else { + ldaconst (offs); + AddCodeLine ("clc"); + AddCodeLine ("ldx sp+1"); + AddCodeLine ("adc sp"); + AddCodeLine ("bcc %s", LocalLabelName (L)); + AddCodeLine ("inx"); + } + g_defcodelabel (L); + } } } @@ -963,30 +967,30 @@ void g_leavariadic (int Offs) AddCodeLine ("lda (sp),y"); /* Add the value of the stackpointer */ - if (CodeSizeFactor > 250) { - unsigned L = GetLocalLabel(); - AddCodeLine ("ldx sp+1"); - AddCodeLine ("clc"); - AddCodeLine ("adc sp"); - AddCodeLine ("bcc %s", LocalLabelName (L)); - AddCodeLine ("inx"); - g_defcodelabel (L); + if (IS_Get (&CodeSizeFactor) > 250) { + unsigned L = GetLocalLabel(); + AddCodeLine ("ldx sp+1"); + AddCodeLine ("clc"); + AddCodeLine ("adc sp"); + AddCodeLine ("bcc %s", LocalLabelName (L)); + AddCodeLine ("inx"); + g_defcodelabel (L); } else { - AddCodeLine ("jsr leaasp"); + AddCodeLine ("jsr leaasp"); } /* Add the offset to the primary */ if (Offs > 0) { - g_inc (CF_INT | CF_CONST, Offs); + g_inc (CF_INT | CF_CONST, Offs); } else if (Offs < 0) { - g_dec (CF_INT | CF_CONST, -Offs); + g_dec (CF_INT | CF_CONST, -Offs); } } /*****************************************************************************/ -/* Store into memory */ +/* Store into memory */ /*****************************************************************************/ @@ -1000,26 +1004,26 @@ void g_putstatic (unsigned flags, unsigned long label, long offs) /* Check the size and generate the correct store operation */ switch (flags & CF_TYPE) { - case CF_CHAR: - AddCodeLine ("sta %s", lbuf); - break; + case CF_CHAR: + AddCodeLine ("sta %s", lbuf); + break; - case CF_INT: - AddCodeLine ("sta %s", lbuf); - AddCodeLine ("stx %s+1", lbuf); - break; + case CF_INT: + AddCodeLine ("sta %s", lbuf); + AddCodeLine ("stx %s+1", lbuf); + break; - case CF_LONG: - 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; + case CF_LONG: + 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: - typeerror (flags); + default: + typeerror (flags); } } @@ -1033,56 +1037,56 @@ void g_putlocal (unsigned Flags, int Offs, long Val) CheckLocalOffs (Offs); switch (Flags & CF_TYPE) { - case CF_CHAR: - if (Flags & CF_CONST) { - AddCodeLine ("lda #$%02X", (unsigned char) Val); - } - ldyconst (Offs); - AddCodeLine ("sta (sp),y"); - break; - - case CF_INT: - if (Flags & CF_CONST) { - ldyconst (Offs+1); - AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8)); - AddCodeLine ("sta (sp),y"); - if ((Flags & CF_NOKEEP) == 0) { - /* Place high byte into X */ - AddCodeLine ("tax"); - } - if ((Val & 0xFF) == Offs+1) { - /* The value we need is already in Y */ - AddCodeLine ("tya"); - AddCodeLine ("dey"); - } else { - AddCodeLine ("dey"); - AddCodeLine ("lda #$%02X", (unsigned char) Val); - } - AddCodeLine ("sta (sp),y"); - } else { - if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) { - ldyconst (Offs); - AddCodeLine ("jsr staxysp"); - } else { - ldyconst (Offs); - AddCodeLine ("sta (sp),y"); - AddCodeLine ("iny"); - AddCodeLine ("txa"); - AddCodeLine ("sta (sp),y"); - } - } - break; - - case CF_LONG: - if (Flags & CF_CONST) { - g_getimmed (Flags, Val, 0); - } - ldyconst (Offs); - AddCodeLine ("jsr steaxysp"); - break; - - default: - typeerror (Flags); + case CF_CHAR: + if (Flags & CF_CONST) { + AddCodeLine ("lda #$%02X", (unsigned char) Val); + } + ldyconst (Offs); + AddCodeLine ("sta (sp),y"); + break; + + case CF_INT: + if (Flags & CF_CONST) { + ldyconst (Offs+1); + AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8)); + AddCodeLine ("sta (sp),y"); + if ((Flags & CF_NOKEEP) == 0) { + /* Place high byte into X */ + AddCodeLine ("tax"); + } + if ((Val & 0xFF) == Offs+1) { + /* The value we need is already in Y */ + AddCodeLine ("tya"); + AddCodeLine ("dey"); + } else { + AddCodeLine ("dey"); + AddCodeLine ("lda #$%02X", (unsigned char) Val); + } + AddCodeLine ("sta (sp),y"); + } else { + 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"); + AddCodeLine ("sta (sp),y"); + } + } + break; + + case CF_LONG: + if (Flags & CF_CONST) { + g_getimmed (Flags, Val, 0); + } + ldyconst (Offs); + AddCodeLine ("jsr steaxysp"); + break; + + default: + typeerror (Flags); } } @@ -1101,57 +1105,57 @@ void g_putind (unsigned Flags, unsigned Offs) */ if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) { - /* Overflow - we need to add the low byte also */ - 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; + /* Overflow - we need to add the low byte also */ + 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; } else if ((Offs & 0xFF00) != 0) { - /* We can just add the high byte */ - 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; + /* We can just add the high byte */ + 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; } /* Check the size and determine operation */ switch (Flags & CF_TYPE) { - case CF_CHAR: - ldyconst (Offs); - AddCodeLine ("jsr staspidx"); - break; + case CF_CHAR: + ldyconst (Offs); + AddCodeLine ("jsr staspidx"); + break; - case CF_INT: - ldyconst (Offs); - AddCodeLine ("jsr staxspidx"); - break; + case CF_INT: + ldyconst (Offs); + AddCodeLine ("jsr staxspidx"); + break; - case CF_LONG: - ldyconst (Offs); - AddCodeLine ("jsr steaxspidx"); - break; + case CF_LONG: + ldyconst (Offs); + AddCodeLine ("jsr steaxspidx"); + break; - default: - typeerror (Flags); + default: + typeerror (Flags); } @@ -1162,7 +1166,7 @@ void g_putind (unsigned Flags, unsigned Offs) /*****************************************************************************/ -/* type conversion and similiar stuff */ +/* type conversion and similiar stuff */ /*****************************************************************************/ @@ -1172,21 +1176,21 @@ void g_toslong (unsigned flags) { switch (flags & CF_TYPE) { - case CF_CHAR: - case CF_INT: - if (flags & CF_UNSIGNED) { - AddCodeLine ("jsr tosulong"); - } else { - AddCodeLine ("jsr toslong"); - } - push (CF_INT); - break; - - case CF_LONG: - break; - - default: - typeerror (flags); + case CF_CHAR: + case CF_INT: + if (flags & CF_UNSIGNED) { + AddCodeLine ("jsr tosulong"); + } else { + AddCodeLine ("jsr toslong"); + } + push (CF_INT); + break; + + case CF_LONG: + break; + + default: + typeerror (flags); } } @@ -1197,17 +1201,17 @@ void g_tosint (unsigned flags) { switch (flags & CF_TYPE) { - case CF_CHAR: - case CF_INT: - break; + case CF_CHAR: + case CF_INT: + break; - case CF_LONG: - AddCodeLine ("jsr tosint"); - pop (CF_INT); - break; + case CF_LONG: + AddCodeLine ("jsr tosint"); + pop (CF_INT); + break; - default: - typeerror (flags); + default: + typeerror (flags); } } @@ -1220,7 +1224,7 @@ void g_regint (unsigned Flags) switch (Flags & CF_TYPE) { - case CF_CHAR: + case CF_CHAR: if (Flags & CF_FORCECHAR) { /* Conversion is from char */ if (Flags & CF_UNSIGNED) { @@ -1236,12 +1240,12 @@ void g_regint (unsigned Flags) } /* FALLTHROUGH */ - case CF_INT: - case CF_LONG: - break; + case CF_INT: + case CF_LONG: + break; - default: - typeerror (Flags); + default: + typeerror (Flags); } } @@ -1254,11 +1258,11 @@ void g_reglong (unsigned Flags) switch (Flags & CF_TYPE) { - case CF_CHAR: + case CF_CHAR: if (Flags & CF_FORCECHAR) { /* Conversion is from char */ if (Flags & CF_UNSIGNED) { - if (CodeSizeFactor >= 200) { + if (IS_Get (&CodeSizeFactor) >= 200) { AddCodeLine ("ldx #$00"); AddCodeLine ("stx sreg"); AddCodeLine ("stx sreg+1"); @@ -1266,7 +1270,7 @@ void g_reglong (unsigned Flags) AddCodeLine ("jsr aulong"); } } else { - if (CodeSizeFactor >= 366) { + if (IS_Get (&CodeSizeFactor) >= 366) { L = GetLocalLabel(); AddCodeLine ("ldx #$00"); AddCodeLine ("cmp #$80"); @@ -1282,25 +1286,25 @@ void g_reglong (unsigned Flags) } /* FALLTHROUGH */ - case CF_INT: - if (Flags & CF_UNSIGNED) { - if (CodeSizeFactor >= 200) { - ldyconst (0); - AddCodeLine ("sty sreg"); - AddCodeLine ("sty sreg+1"); - } else { - AddCodeLine ("jsr axulong"); - } - } else { - AddCodeLine ("jsr axlong"); - } - break; - - case CF_LONG: - break; - - default: - typeerror (Flags); + case CF_INT: + if (Flags & CF_UNSIGNED) { + if (IS_Get (&CodeSizeFactor) >= 200) { + ldyconst (0); + AddCodeLine ("sty sreg"); + AddCodeLine ("sty sreg+1"); + } else { + AddCodeLine ("jsr axulong"); + } + } else { + AddCodeLine ("jsr axlong"); + } + break; + + case CF_LONG: + break; + + default: + typeerror (Flags); } } @@ -1321,35 +1325,35 @@ unsigned g_typeadjust (unsigned lhs, unsigned rhs) /* Check if a conversion is needed */ if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) { - /* We must promote the primary register to long */ - g_reglong (rhs); - /* Get the new rhs type */ - rhs = (rhs & ~CF_TYPE) | CF_LONG; - rtype = CF_LONG; + /* We must promote the primary register to long */ + g_reglong (rhs); + /* Get the new rhs type */ + rhs = (rhs & ~CF_TYPE) | CF_LONG; + rtype = CF_LONG; } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) { - /* We must promote the lhs to long */ - if (lhs & CF_REG) { - g_reglong (lhs); - } else { - g_toslong (lhs); - } - /* Get the new rhs type */ - lhs = (lhs & ~CF_TYPE) | CF_LONG; - ltype = CF_LONG; + /* We must promote the lhs to long */ + if (lhs & CF_REG) { + g_reglong (lhs); + } else { + g_toslong (lhs); + } + /* Get the new rhs type */ + lhs = (lhs & ~CF_TYPE) | CF_LONG; + ltype = CF_LONG; } /* Determine the result type for the operation: - * - The result is const if both operands are const. - * - The result is unsigned if one of the operands is unsigned. - * - The result is long if one of the operands is long. - * - Otherwise the result is int sized. + * - The result is const if both operands are const. + * - The result is unsigned if one of the operands is unsigned. + * - The result is long if one of the operands is long. + * - Otherwise the result is int sized. */ result = (lhs & CF_CONST) & (rhs & CF_CONST); result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED); if (rtype == CF_LONG || ltype == CF_LONG) { - result |= CF_LONG; + result |= CF_LONG; } else { - result |= CF_INT; + result |= CF_INT; } return result; } @@ -1406,138 +1410,138 @@ void g_scale (unsigned flags, long val) /* Value may not be zero */ if (val == 0) { - Internal ("Data type has no size"); + Internal ("Data type has no size"); } else if (val > 0) { - /* Scale up */ - if ((p2 = powerof2 (val)) > 0 && p2 <= 4) { - - /* Factor is 2, 4, 8 and 16, use special function */ - switch (flags & CF_TYPE) { - - case CF_CHAR: - if (flags & CF_FORCECHAR) { - while (p2--) { - AddCodeLine ("asl a"); - } - break; - } - /* FALLTHROUGH */ - - case CF_INT: - if (CodeSizeFactor >= (p2+1)*130U) { - AddCodeLine ("stx tmp1"); - while (p2--) { - AddCodeLine ("asl a"); - AddCodeLine ("rol tmp1"); - } - AddCodeLine ("ldx tmp1"); - } else { - if (flags & CF_UNSIGNED) { - AddCodeLine ("jsr shlax%d", p2); - } else { - AddCodeLine ("jsr aslax%d", p2); - } - } - break; - - case CF_LONG: - if (flags & CF_UNSIGNED) { - AddCodeLine ("jsr shleax%d", p2); - } else { - AddCodeLine ("jsr asleax%d", p2); - } - break; - - default: - typeerror (flags); - - } - - } else if (val != 1) { - - /* Use a multiplication instead */ - g_mul (flags | CF_CONST, val); - - } + /* Scale up */ + if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) { + + /* Factor is 2, 4, 8 and 16, use special function */ + switch (flags & CF_TYPE) { + + case CF_CHAR: + if (flags & CF_FORCECHAR) { + while (p2--) { + AddCodeLine ("asl a"); + } + break; + } + /* FALLTHROUGH */ + + case CF_INT: + if (IS_Get (&CodeSizeFactor) >= (p2+1)*130) { + AddCodeLine ("stx tmp1"); + while (p2--) { + AddCodeLine ("asl a"); + AddCodeLine ("rol tmp1"); + } + AddCodeLine ("ldx tmp1"); + } else { + if (flags & CF_UNSIGNED) { + AddCodeLine ("jsr shlax%d", p2); + } else { + AddCodeLine ("jsr aslax%d", p2); + } + } + break; + + case CF_LONG: + if (flags & CF_UNSIGNED) { + AddCodeLine ("jsr shleax%d", p2); + } else { + AddCodeLine ("jsr asleax%d", p2); + } + break; + + default: + typeerror (flags); + + } + + } else if (val != 1) { + + /* Use a multiplication instead */ + g_mul (flags | CF_CONST, val); + + } } else { - /* Scale down */ - val = -val; - if ((p2 = powerof2 (val)) > 0 && p2 <= 4) { - - /* 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--) { - AddCodeLine ("lsr a"); - } - break; - } else if (p2 <= 2) { - AddCodeLine ("cmp #$80"); - AddCodeLine ("ror a"); - break; - } - } - /* FALLTHROUGH */ - - case CF_INT: - if (flags & CF_UNSIGNED) { - if (CodeSizeFactor >= (p2+1)*130U) { - AddCodeLine ("stx tmp1"); - while (p2--) { - AddCodeLine ("lsr tmp1"); - AddCodeLine ("ror a"); - } - AddCodeLine ("ldx tmp1"); - } else { - AddCodeLine ("jsr lsrax%d", p2); - } - } else { - if (CodeSizeFactor >= (p2+1)*150U) { - AddCodeLine ("stx tmp1"); - while (p2--) { - AddCodeLine ("cpx #$80"); - AddCodeLine ("ror tmp1"); - AddCodeLine ("ror a"); - } - AddCodeLine ("ldx tmp1"); - } else { - AddCodeLine ("jsr asrax%d", p2); - } - } - break; - - case CF_LONG: - if (flags & CF_UNSIGNED) { - AddCodeLine ("jsr lsreax%d", p2); - } else { - AddCodeLine ("jsr asreax%d", p2); - } - break; - - default: - typeerror (flags); - - } - - } else if (val != 1) { - - /* Use a division instead */ - g_div (flags | CF_CONST, val); - - } + /* Scale down */ + val = -val; + if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) { + + /* 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--) { + AddCodeLine ("lsr a"); + } + break; + } else if (p2 <= 2) { + AddCodeLine ("cmp #$80"); + AddCodeLine ("ror a"); + break; + } + } + /* FALLTHROUGH */ + + case CF_INT: + if (flags & CF_UNSIGNED) { + if (IS_Get (&CodeSizeFactor) >= (p2+1)*130) { + AddCodeLine ("stx tmp1"); + while (p2--) { + AddCodeLine ("lsr tmp1"); + AddCodeLine ("ror a"); + } + AddCodeLine ("ldx tmp1"); + } else { + AddCodeLine ("jsr lsrax%d", p2); + } + } else { + if (IS_Get (&CodeSizeFactor) >= (p2+1)*150) { + AddCodeLine ("stx tmp1"); + while (p2--) { + AddCodeLine ("cpx #$80"); + AddCodeLine ("ror tmp1"); + AddCodeLine ("ror a"); + } + AddCodeLine ("ldx tmp1"); + } else { + AddCodeLine ("jsr asrax%d", p2); + } + } + break; + + case CF_LONG: + if (flags & CF_UNSIGNED) { + AddCodeLine ("jsr lsreax%d", p2); + } else { + AddCodeLine ("jsr asreax%d", p2); + } + break; + + default: + typeerror (flags); + + } + + } else if (val != 1) { + + /* Use a division instead */ + g_div (flags | CF_CONST, val); + + } } } /*****************************************************************************/ -/* Adds and subs of variables fix a fixed address */ +/* Adds and subs of variables fix a fixed address */ /*****************************************************************************/ @@ -1553,37 +1557,37 @@ void g_addlocal (unsigned flags, int offs) switch (flags & CF_TYPE) { - case CF_CHAR: - L = GetLocalLabel(); - AddCodeLine ("ldy #$%02X", offs & 0xFF); - AddCodeLine ("clc"); - AddCodeLine ("adc (sp),y"); - AddCodeLine ("bcc %s", LocalLabelName (L)); - AddCodeLine ("inx"); - g_defcodelabel (L); - break; - - case CF_INT: - 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: - /* Do it the old way */ - g_push (flags, 0); - g_getlocal (flags, offs); - g_add (flags, 0); - break; - - default: - typeerror (flags); + case CF_CHAR: + 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: + 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: + /* Do it the old way */ + g_push (flags, 0); + g_getlocal (flags, offs); + g_add (flags, 0); + break; + + default: + typeerror (flags); } } @@ -1600,34 +1604,34 @@ void g_addstatic (unsigned flags, unsigned long label, long offs) switch (flags & CF_TYPE) { - case CF_CHAR: - L = GetLocalLabel(); - AddCodeLine ("clc"); - AddCodeLine ("adc %s", lbuf); - AddCodeLine ("bcc %s", LocalLabelName (L)); - AddCodeLine ("inx"); - g_defcodelabel (L); - break; - - case CF_INT: - AddCodeLine ("clc"); - AddCodeLine ("adc %s", lbuf); - AddCodeLine ("tay"); - AddCodeLine ("txa"); - AddCodeLine ("adc %s+1", lbuf); - AddCodeLine ("tax"); - AddCodeLine ("tya"); - break; - - case CF_LONG: - /* Do it the old way */ - g_push (flags, 0); - g_getstatic (flags, label, offs); - g_add (flags, 0); - break; - - default: - typeerror (flags); + case CF_CHAR: + L = GetLocalLabel(); + AddCodeLine ("clc"); + AddCodeLine ("adc %s", lbuf); + AddCodeLine ("bcc %s", LocalLabelName (L)); + AddCodeLine ("inx"); + g_defcodelabel (L); + break; + + case CF_INT: + AddCodeLine ("clc"); + AddCodeLine ("adc %s", lbuf); + AddCodeLine ("tay"); + AddCodeLine ("txa"); + AddCodeLine ("adc %s+1", lbuf); + AddCodeLine ("tax"); + AddCodeLine ("tya"); + break; + + case CF_LONG: + /* Do it the old way */ + g_push (flags, 0); + g_getstatic (flags, label, offs); + g_add (flags, 0); + break; + + default: + typeerror (flags); } } @@ -1635,13 +1639,13 @@ void g_addstatic (unsigned flags, unsigned long label, long offs) /*****************************************************************************/ -/* Special op= functions */ +/* Special op= functions */ /*****************************************************************************/ void g_addeqstatic (unsigned flags, unsigned long label, long offs, - unsigned long val) + unsigned long val) /* Emit += for a static variable */ { /* Create the correct label name */ @@ -1650,102 +1654,102 @@ void g_addeqstatic (unsigned flags, unsigned long label, long offs, /* Check the size and determine operation */ switch (flags & CF_TYPE) { - case CF_CHAR: - if (flags & CF_FORCECHAR) { - AddCodeLine ("ldx #$00"); - if (flags & CF_CONST) { - if (val == 1) { - AddCodeLine ("inc %s", lbuf); - AddCodeLine ("lda %s", lbuf); - } else { - AddCodeLine ("lda #$%02X", (int)(val & 0xFF)); - AddCodeLine ("clc"); - AddCodeLine ("adc %s", lbuf); - AddCodeLine ("sta %s", lbuf); - } - } else { - AddCodeLine ("clc"); - AddCodeLine ("adc %s", lbuf); - AddCodeLine ("sta %s", lbuf); - } - if ((flags & CF_UNSIGNED) == 0) { - unsigned L = GetLocalLabel(); - AddCodeLine ("bpl %s", LocalLabelName (L)); - AddCodeLine ("dex"); - g_defcodelabel (L); - } - break; - } - /* FALLTHROUGH */ - - case CF_INT: - if (flags & CF_CONST) { - if (val == 1) { - unsigned L = GetLocalLabel (); - AddCodeLine ("inc %s", lbuf); - AddCodeLine ("bne %s", LocalLabelName (L)); - AddCodeLine ("inc %s+1", lbuf); - g_defcodelabel (L); - AddCodeLine ("lda %s", lbuf); /* Hmmm... */ - AddCodeLine ("ldx %s+1", lbuf); - } else { - AddCodeLine ("lda #$%02X", (int)(val & 0xFF)); - AddCodeLine ("clc"); - AddCodeLine ("adc %s", lbuf); - AddCodeLine ("sta %s", lbuf); - if (val < 0x100) { - unsigned L = GetLocalLabel (); - AddCodeLine ("bcc %s", LocalLabelName (L)); - AddCodeLine ("inc %s+1", lbuf); - g_defcodelabel (L); - AddCodeLine ("ldx %s+1", lbuf); - } else { - AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8)); - AddCodeLine ("adc %s+1", lbuf); - AddCodeLine ("sta %s+1", lbuf); - AddCodeLine ("tax"); - AddCodeLine ("lda %s", lbuf); - } - } - } else { - 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) { - AddCodeLine ("ldy #<(%s)", lbuf); - AddCodeLine ("sty ptr1"); - AddCodeLine ("ldy #>(%s)", lbuf); - if (val == 1) { - AddCodeLine ("jsr laddeq1"); - } else { - AddCodeLine ("lda #$%02X", (int)(val & 0xFF)); - AddCodeLine ("jsr laddeqa"); - } - } else { - g_getstatic (flags, label, offs); - g_inc (flags, val); - g_putstatic (flags, label, offs); - } - } else { - AddCodeLine ("ldy #<(%s)", lbuf); - AddCodeLine ("sty ptr1"); - AddCodeLine ("ldy #>(%s)", lbuf); - AddCodeLine ("jsr laddeq"); - } - break; - - default: - typeerror (flags); + case CF_CHAR: + if (flags & CF_FORCECHAR) { + AddCodeLine ("ldx #$00"); + if (flags & CF_CONST) { + if (val == 1) { + AddCodeLine ("inc %s", lbuf); + AddCodeLine ("lda %s", lbuf); + } else { + AddCodeLine ("lda #$%02X", (int)(val & 0xFF)); + AddCodeLine ("clc"); + AddCodeLine ("adc %s", lbuf); + AddCodeLine ("sta %s", lbuf); + } + } else { + AddCodeLine ("clc"); + AddCodeLine ("adc %s", lbuf); + AddCodeLine ("sta %s", lbuf); + } + if ((flags & CF_UNSIGNED) == 0) { + unsigned L = GetLocalLabel(); + AddCodeLine ("bpl %s", LocalLabelName (L)); + AddCodeLine ("dex"); + g_defcodelabel (L); + } + break; + } + /* FALLTHROUGH */ + + case CF_INT: + if (flags & CF_CONST) { + if (val == 1) { + unsigned L = GetLocalLabel (); + AddCodeLine ("inc %s", lbuf); + AddCodeLine ("bne %s", LocalLabelName (L)); + AddCodeLine ("inc %s+1", lbuf); + g_defcodelabel (L); + AddCodeLine ("lda %s", lbuf); /* Hmmm... */ + AddCodeLine ("ldx %s+1", lbuf); + } else { + AddCodeLine ("lda #$%02X", (int)(val & 0xFF)); + AddCodeLine ("clc"); + AddCodeLine ("adc %s", lbuf); + AddCodeLine ("sta %s", lbuf); + if (val < 0x100) { + unsigned L = GetLocalLabel (); + AddCodeLine ("bcc %s", LocalLabelName (L)); + AddCodeLine ("inc %s+1", lbuf); + g_defcodelabel (L); + AddCodeLine ("ldx %s+1", lbuf); + } else { + AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8)); + AddCodeLine ("adc %s+1", lbuf); + AddCodeLine ("sta %s+1", lbuf); + AddCodeLine ("tax"); + AddCodeLine ("lda %s", lbuf); + } + } + } else { + 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) { + AddCodeLine ("ldy #<(%s)", lbuf); + AddCodeLine ("sty ptr1"); + AddCodeLine ("ldy #>(%s)", lbuf); + if (val == 1) { + AddCodeLine ("jsr laddeq1"); + } else { + AddCodeLine ("lda #$%02X", (int)(val & 0xFF)); + AddCodeLine ("jsr laddeqa"); + } + } else { + g_getstatic (flags, label, offs); + g_inc (flags, val); + g_putstatic (flags, label, offs); + } + } else { + AddCodeLine ("ldy #<(%s)", lbuf); + AddCodeLine ("sty ptr1"); + AddCodeLine ("ldy #>(%s)", lbuf); + AddCodeLine ("jsr laddeq"); + } + break; + + default: + typeerror (flags); } } @@ -1761,64 +1765,64 @@ void g_addeqlocal (unsigned flags, int offs, unsigned long val) /* Check the size and determine operation */ switch (flags & CF_TYPE) { - case CF_CHAR: - if (flags & CF_FORCECHAR) { - 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) { - unsigned L = GetLocalLabel(); - AddCodeLine ("bpl %s", LocalLabelName (L)); - AddCodeLine ("dex"); - g_defcodelabel (L); - } - break; - } - /* FALLTHROUGH */ - - case CF_INT: - ldyconst (offs); - if (flags & CF_CONST) { - 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 { - AddCodeLine ("jsr addeqysp"); - } - break; - - case CF_LONG: - if (flags & CF_CONST) { - g_getimmed (flags, val, 0); - } - ldyconst (offs); - AddCodeLine ("jsr laddeqysp"); - break; - - default: - typeerror (flags); + case CF_CHAR: + if (flags & CF_FORCECHAR) { + 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) { + unsigned L = GetLocalLabel(); + AddCodeLine ("bpl %s", LocalLabelName (L)); + AddCodeLine ("dex"); + g_defcodelabel (L); + } + break; + } + /* FALLTHROUGH */ + + case CF_INT: + ldyconst (offs); + if (flags & CF_CONST) { + if (IS_Get (&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 { + AddCodeLine ("jsr addeqysp"); + } + break; + + case CF_LONG: + if (flags & CF_CONST) { + g_getimmed (flags, val, 0); + } + ldyconst (offs); + AddCodeLine ("jsr laddeqysp"); + break; + + default: + typeerror (flags); } } @@ -1836,55 +1840,55 @@ void g_addeqind (unsigned flags, unsigned offs, unsigned long val) /* Check the size and determine operation */ switch (flags & CF_TYPE) { - case CF_CHAR: - 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 */ - 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: - 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 */ - break; - - default: - typeerror (flags); + case CF_CHAR: + 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 (IS_Get (&CodeSizeFactor) >= 200) { + /* Lots of code, use only if size is not important */ + 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: + 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 */ + break; + + default: + typeerror (flags); } } void g_subeqstatic (unsigned flags, unsigned long label, long offs, - unsigned long val) + unsigned long val) /* Emit -= for a static variable */ { /* Create the correct label name */ @@ -1893,90 +1897,90 @@ void g_subeqstatic (unsigned flags, unsigned long label, long offs, /* Check the size and determine operation */ switch (flags & CF_TYPE) { - case CF_CHAR: - if (flags & CF_FORCECHAR) { - AddCodeLine ("ldx #$00"); - if (flags & CF_CONST) { - if (val == 1) { - AddCodeLine ("dec %s", lbuf); - AddCodeLine ("lda %s", lbuf); - } else { - AddCodeLine ("sec"); - AddCodeLine ("lda %s", lbuf); - AddCodeLine ("sbc #$%02X", (int)(val & 0xFF)); - AddCodeLine ("sta %s", lbuf); - } - } else { - AddCodeLine ("eor #$FF"); - AddCodeLine ("sec"); - AddCodeLine ("adc %s", lbuf); - AddCodeLine ("sta %s", lbuf); - } - if ((flags & CF_UNSIGNED) == 0) { - unsigned L = GetLocalLabel(); - AddCodeLine ("bpl %s", LocalLabelName (L)); - AddCodeLine ("dex"); - g_defcodelabel (L); - } - break; - } - /* FALLTHROUGH */ - - case CF_INT: - AddCodeLine ("sec"); - if (flags & CF_CONST) { - AddCodeLine ("lda %s", lbuf); - AddCodeLine ("sbc #$%02X", (unsigned char)val); - AddCodeLine ("sta %s", lbuf); - if (val < 0x100) { - unsigned L = GetLocalLabel (); - AddCodeLine ("bcs %s", LocalLabelName (L)); - AddCodeLine ("dec %s+1", lbuf); - g_defcodelabel (L); - AddCodeLine ("ldx %s+1", lbuf); - } else { - AddCodeLine ("lda %s+1", lbuf); - AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8)); - AddCodeLine ("sta %s+1", lbuf); - AddCodeLine ("tax"); - AddCodeLine ("lda %s", lbuf); - } - } else { - 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) { - 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 { - AddCodeLine ("ldy #<(%s)", lbuf); - AddCodeLine ("sty ptr1"); - AddCodeLine ("ldy #>(%s)", lbuf); - AddCodeLine ("jsr lsubeq"); - } - break; - - default: - typeerror (flags); + case CF_CHAR: + if (flags & CF_FORCECHAR) { + AddCodeLine ("ldx #$00"); + if (flags & CF_CONST) { + if (val == 1) { + AddCodeLine ("dec %s", lbuf); + AddCodeLine ("lda %s", lbuf); + } else { + AddCodeLine ("sec"); + AddCodeLine ("lda %s", lbuf); + AddCodeLine ("sbc #$%02X", (int)(val & 0xFF)); + AddCodeLine ("sta %s", lbuf); + } + } else { + AddCodeLine ("eor #$FF"); + AddCodeLine ("sec"); + AddCodeLine ("adc %s", lbuf); + AddCodeLine ("sta %s", lbuf); + } + if ((flags & CF_UNSIGNED) == 0) { + unsigned L = GetLocalLabel(); + AddCodeLine ("bpl %s", LocalLabelName (L)); + AddCodeLine ("dex"); + g_defcodelabel (L); + } + break; + } + /* FALLTHROUGH */ + + case CF_INT: + AddCodeLine ("sec"); + if (flags & CF_CONST) { + AddCodeLine ("lda %s", lbuf); + AddCodeLine ("sbc #$%02X", (unsigned char)val); + AddCodeLine ("sta %s", lbuf); + if (val < 0x100) { + unsigned L = GetLocalLabel (); + AddCodeLine ("bcs %s", LocalLabelName (L)); + AddCodeLine ("dec %s+1", lbuf); + g_defcodelabel (L); + AddCodeLine ("ldx %s+1", lbuf); + } else { + AddCodeLine ("lda %s+1", lbuf); + AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8)); + AddCodeLine ("sta %s+1", lbuf); + AddCodeLine ("tax"); + AddCodeLine ("lda %s", lbuf); + } + } else { + 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) { + 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 { + AddCodeLine ("ldy #<(%s)", lbuf); + AddCodeLine ("sty ptr1"); + AddCodeLine ("ldy #>(%s)", lbuf); + AddCodeLine ("jsr lsubeq"); + } + break; + + default: + typeerror (flags); } } @@ -1992,47 +1996,47 @@ void g_subeqlocal (unsigned flags, int offs, unsigned long val) /* Check the size and determine operation */ switch (flags & CF_TYPE) { - case CF_CHAR: - if (flags & CF_FORCECHAR) { - ldyconst (offs); - AddCodeLine ("ldx #$00"); - AddCodeLine ("sec"); - if (flags & CF_CONST) { - AddCodeLine ("lda (sp),y"); - AddCodeLine ("sbc #$%02X", (unsigned char)val); - } else { - AddCodeLine ("eor #$FF"); - AddCodeLine ("adc (sp),y"); - } - AddCodeLine ("sta (sp),y"); - if ((flags & CF_UNSIGNED) == 0) { - unsigned L = GetLocalLabel(); - AddCodeLine ("bpl %s", LocalLabelName (L)); - AddCodeLine ("dex"); - g_defcodelabel (L); - } - break; - } - /* FALLTHROUGH */ - - case CF_INT: - if (flags & CF_CONST) { - g_getimmed (flags, val, 0); - } - ldyconst (offs); - AddCodeLine ("jsr subeqysp"); - break; - - case CF_LONG: - if (flags & CF_CONST) { - g_getimmed (flags, val, 0); - } - ldyconst (offs); - AddCodeLine ("jsr lsubeqysp"); - break; - - default: - typeerror (flags); + case CF_CHAR: + if (flags & CF_FORCECHAR) { + ldyconst (offs); + AddCodeLine ("ldx #$00"); + AddCodeLine ("sec"); + if (flags & CF_CONST) { + AddCodeLine ("lda (sp),y"); + AddCodeLine ("sbc #$%02X", (unsigned char)val); + } else { + AddCodeLine ("eor #$FF"); + AddCodeLine ("adc (sp),y"); + } + AddCodeLine ("sta (sp),y"); + if ((flags & CF_UNSIGNED) == 0) { + unsigned L = GetLocalLabel(); + AddCodeLine ("bpl %s", LocalLabelName (L)); + AddCodeLine ("dex"); + g_defcodelabel (L); + } + break; + } + /* FALLTHROUGH */ + + case CF_INT: + if (flags & CF_CONST) { + g_getimmed (flags, val, 0); + } + ldyconst (offs); + AddCodeLine ("jsr subeqysp"); + break; + + case CF_LONG: + if (flags & CF_CONST) { + g_getimmed (flags, val, 0); + } + ldyconst (offs); + AddCodeLine ("jsr lsubeqysp"); + break; + + default: + typeerror (flags); } } @@ -2050,55 +2054,55 @@ void g_subeqind (unsigned flags, unsigned offs, unsigned long val) /* Check the size and determine operation */ switch (flags & CF_TYPE) { - case CF_CHAR: - 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 */ - 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: - 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 */ - break; - - default: - typeerror (flags); + case CF_CHAR: + 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 (IS_Get (&CodeSizeFactor) >= 200) { + /* Lots of code, use only if size is not important */ + 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: + 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 */ + break; + + default: + typeerror (flags); } } /*****************************************************************************/ -/* Add a variable address to the value in ax */ +/* Add a variable address to the value in ax */ /*****************************************************************************/ @@ -2111,21 +2115,21 @@ void g_addaddr_local (unsigned flags attribute ((unused)), int offs) /* Add the offset */ offs -= StackPtr; if (offs != 0) { - /* We cannot address more then 256 bytes of locals anyway */ - L = GetLocalLabel(); - CheckLocalOffs (offs); - AddCodeLine ("clc"); - AddCodeLine ("adc #$%02X", offs & 0xFF); - /* Do also skip the CLC insn below */ - AddCodeLine ("bcc %s", LocalLabelName (L)); - AddCodeLine ("inx"); + /* We cannot address more then 256 bytes of locals anyway */ + L = GetLocalLabel(); + CheckLocalOffs (offs); + 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 */ AddCodeLine ("clc"); if (L != 0) { - /* Label was used above */ - g_defcodelabel (L); + /* Label was used above */ + g_defcodelabel (L); } AddCodeLine ("adc sp"); AddCodeLine ("tay"); @@ -2156,7 +2160,7 @@ void g_addaddr_static (unsigned flags, unsigned long label, long offs) /*****************************************************************************/ -/* */ +/* */ /*****************************************************************************/ @@ -2167,24 +2171,24 @@ void g_save (unsigned flags) /* Check the size and determine operation */ switch (flags & CF_TYPE) { - case CF_CHAR: - if (flags & CF_FORCECHAR) { - AddCodeLine ("pha"); - break; - } - /* FALLTHROUGH */ + case CF_CHAR: + if (flags & CF_FORCECHAR) { + AddCodeLine ("pha"); + break; + } + /* FALLTHROUGH */ - case CF_INT: - AddCodeLine ("sta regsave"); - AddCodeLine ("stx regsave+1"); - break; + case CF_INT: + AddCodeLine ("sta regsave"); + AddCodeLine ("stx regsave+1"); + break; - case CF_LONG: - AddCodeLine ("jsr saveeax"); - break; + case CF_LONG: + AddCodeLine ("jsr saveeax"); + break; - default: - typeerror (flags); + default: + typeerror (flags); } } @@ -2196,24 +2200,24 @@ void g_restore (unsigned flags) /* Check the size and determine operation */ switch (flags & CF_TYPE) { - case CF_CHAR: - if (flags & CF_FORCECHAR) { - AddCodeLine ("pla"); - break; - } - /* FALLTHROUGH */ + case CF_CHAR: + if (flags & CF_FORCECHAR) { + AddCodeLine ("pla"); + break; + } + /* FALLTHROUGH */ - case CF_INT: - AddCodeLine ("lda regsave"); - AddCodeLine ("ldx regsave+1"); - break; + case CF_INT: + AddCodeLine ("lda regsave"); + AddCodeLine ("ldx regsave+1"); + break; - case CF_LONG: - AddCodeLine ("jsr resteax"); - break; + case CF_LONG: + AddCodeLine ("jsr resteax"); + break; - default: - typeerror (flags); + default: + typeerror (flags); } } @@ -2229,27 +2233,27 @@ void g_cmp (unsigned flags, unsigned long val) /* Check the size and determine operation */ switch (flags & CF_TYPE) { - case CF_CHAR: - if (flags & CF_FORCECHAR) { - AddCodeLine ("cmp #$%02X", (unsigned char)val); - break; - } - /* FALLTHROUGH */ - - case CF_INT: - 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_CHAR: + if (flags & CF_FORCECHAR) { + AddCodeLine ("cmp #$%02X", (unsigned char)val); + break; + } + /* FALLTHROUGH */ + + case CF_INT: + 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: - Internal ("g_cmp: Long compares not implemented"); - break; + Internal ("g_cmp: Long compares not implemented"); + break; - default: - typeerror (flags); + default: + typeerror (flags); } } @@ -2258,10 +2262,10 @@ void g_cmp (unsigned flags, unsigned long val) static void oper (unsigned flags, unsigned long val, char** subs) /* Encode a binary operation. subs is a pointer to four groups of three * strings: - * 0-2 --> Operate on ints - * 3-5 --> Operate on unsigneds - * 6-8 --> Operate on longs - * 9-11 --> Operate on unsigned longs + * 0-2 --> Operate on ints + * 3-5 --> Operate on unsigneds + * 6-8 --> Operate on longs + * 9-11 --> Operate on unsigned longs * * The first subroutine names in each string group is used to encode an * operation with a zero constant, the second to encode an operation with @@ -2273,36 +2277,36 @@ static void oper (unsigned flags, unsigned long val, char** subs) /* Determine the offset into the array */ offs = (flags & CF_UNSIGNED)? 3 : 0; switch (flags & CF_TYPE) { - case CF_CHAR: - case CF_INT: - break; + case CF_CHAR: + case CF_INT: + break; - case CF_LONG: - offs += 6; - break; + case CF_LONG: + offs += 6; + break; - default: - typeerror (flags); + default: + typeerror (flags); } /* Encode the operation */ if (flags & CF_CONST) { - /* Constant value given */ - if (val == 0 && subs [offs+0]) { - /* Special case: constant with value zero */ - 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 */ - AddCodeLine ("jsr %s", subs [offs+1]); - } else { - /* Others: arbitrary constant value */ - g_getimmed (flags, val, 0); /* Load value */ - AddCodeLine ("jsr %s", subs [offs+2]); - } + /* Constant value given */ + if (val == 0 && subs [offs+0]) { + /* Special case: constant with value zero */ + 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 */ + AddCodeLine ("jsr %s", subs [offs+1]); + } else { + /* Others: arbitrary constant value */ + g_getimmed (flags, val, 0); /* Load value */ + AddCodeLine ("jsr %s", subs [offs+2]); + } } else { - /* Value not constant (is already in (e)ax) */ - AddCodeLine ("jsr %s", subs [offs+2]); + /* Value not constant (is already in (e)ax) */ + AddCodeLine ("jsr %s", subs [offs+2]); } /* The operation will pop it's argument */ @@ -2316,28 +2320,28 @@ void g_test (unsigned flags) { switch (flags & CF_TYPE) { - case CF_CHAR: - if (flags & CF_FORCECHAR) { - AddCodeLine ("tax"); - break; - } - /* FALLTHROUGH */ - - case CF_INT: - AddCodeLine ("stx tmp1"); - AddCodeLine ("ora tmp1"); - break; - - case CF_LONG: - if (flags & CF_UNSIGNED) { - AddCodeLine ("jsr utsteax"); - } else { - AddCodeLine ("jsr tsteax"); - } - break; - - default: - typeerror (flags); + case CF_CHAR: + if (flags & CF_FORCECHAR) { + AddCodeLine ("tax"); + break; + } + /* FALLTHROUGH */ + + case CF_INT: + AddCodeLine ("stx tmp1"); + AddCodeLine ("ora tmp1"); + break; + + case CF_LONG: + if (flags & CF_UNSIGNED) { + AddCodeLine ("jsr utsteax"); + } else { + AddCodeLine ("jsr tsteax"); + } + break; + + default: + typeerror (flags); } } @@ -2349,50 +2353,50 @@ void g_push (unsigned flags, unsigned long val) { 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)) { + /* We have a constant 8 or 16 bit value */ + if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) { - /* Handle as 8 bit value */ - ldaconst (val); - AddCodeLine ("jsr pusha"); + /* Handle as 8 bit value */ + ldaconst (val); + AddCodeLine ("jsr pusha"); - } else { + } else { - /* Handle as 16 bit value */ - g_getimmed (flags, val, 0); - AddCodeLine ("jsr pushax"); - } + /* Handle as 16 bit value */ + g_getimmed (flags, val, 0); + AddCodeLine ("jsr pushax"); + } } else { - /* Value is not 16 bit or not constant */ - if (flags & CF_CONST) { - /* Constant 32 bit value, load into eax */ - g_getimmed (flags, val, 0); - } + /* Value is not 16 bit or not constant */ + if (flags & CF_CONST) { + /* Constant 32 bit value, load into eax */ + g_getimmed (flags, val, 0); + } - /* Push the primary register */ - switch (flags & CF_TYPE) { + /* Push the primary register */ + switch (flags & CF_TYPE) { - case CF_CHAR: - if (flags & CF_FORCECHAR) { - /* Handle as char */ - AddCodeLine ("jsr pusha"); - break; - } - /* FALL THROUGH */ - case CF_INT: - AddCodeLine ("jsr pushax"); - break; + case CF_CHAR: + if (flags & CF_FORCECHAR) { + /* Handle as char */ + AddCodeLine ("jsr pusha"); + break; + } + /* FALL THROUGH */ + case CF_INT: + AddCodeLine ("jsr pushax"); + break; - case CF_LONG: - AddCodeLine ("jsr pusheax"); - break; + case CF_LONG: + AddCodeLine ("jsr pusheax"); + break; - default: - typeerror (flags); + default: + typeerror (flags); - } + } } @@ -2409,17 +2413,17 @@ void g_swap (unsigned flags) { switch (flags & CF_TYPE) { - case CF_CHAR: - case CF_INT: - AddCodeLine ("jsr swapstk"); - break; + case CF_CHAR: + case CF_INT: + AddCodeLine ("jsr swapstk"); + break; - case CF_LONG: - AddCodeLine ("jsr swapestk"); - break; + case CF_LONG: + AddCodeLine ("jsr swapestk"); + break; - default: - typeerror (flags); + default: + typeerror (flags); } } @@ -2430,11 +2434,11 @@ void g_call (unsigned Flags, const char* Label, unsigned ArgSize) /* Call the specified subroutine name */ { if ((Flags & CF_FIXARGC) == 0) { - /* Pass the argument count */ - ldyconst (ArgSize); + /* Pass the argument count */ + ldyconst (ArgSize); } AddCodeLine ("jsr _%s", Label); - StackPtr += ArgSize; /* callee pops args */ + StackPtr += ArgSize; /* callee pops args */ } @@ -2443,25 +2447,25 @@ void g_callind (unsigned Flags, unsigned ArgSize, int Offs) /* Call subroutine indirect */ { if ((Flags & CF_LOCAL) == 0) { - /* Address is in a/x */ - if ((Flags & CF_FIXARGC) == 0) { - /* Pass arg count */ - ldyconst (ArgSize); - } - AddCodeLine ("jsr callax"); + /* 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 -= StackPtr; - 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"); + /* The address is on stack, offset is on Val */ + Offs -= StackPtr; + 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"); } /* Callee pops args */ @@ -2497,11 +2501,11 @@ void g_falsejump (unsigned flags attribute ((unused)), unsigned label) static void mod_internal (int k, char* verb1, char* verb2) { if (k <= 8) { - AddCodeLine ("jsr %ssp%c", verb1, k + '0'); + AddCodeLine ("jsr %ssp%c", verb1, k + '0'); } else { - CheckLocalOffs (k); - ldyconst (k); - AddCodeLine ("jsr %ssp", verb2); + CheckLocalOffs (k); + ldyconst (k); + AddCodeLine ("jsr %ssp", verb2); } } @@ -2511,9 +2515,9 @@ 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"); } } @@ -2539,15 +2543,15 @@ void g_add (unsigned flags, unsigned long val) /* Primary = TOS + Primary */ { static char* ops [12] = { - 0, "tosadda0", "tosaddax", - 0, "tosadda0", "tosaddax", - 0, 0, "tosaddeax", - 0, 0, "tosaddeax", + 0, "tosadda0", "tosaddax", + 0, "tosadda0", "tosaddax", + 0, 0, "tosaddeax", + 0, 0, "tosaddeax", }; if (flags & CF_CONST) { - flags &= ~CF_FORCECHAR; /* Handle chars as ints */ - g_push (flags & ~CF_CONST, 0); + flags &= ~CF_FORCECHAR; /* Handle chars as ints */ + g_push (flags & ~CF_CONST, 0); } oper (flags, val, ops); } @@ -2558,15 +2562,15 @@ void g_sub (unsigned flags, unsigned long val) /* Primary = TOS - Primary */ { static char* ops [12] = { - 0, "tossuba0", "tossubax", - 0, "tossuba0", "tossubax", - 0, 0, "tossubeax", - 0, 0, "tossubeax", + 0, "tossuba0", "tossubax", + 0, "tossuba0", "tossubax", + 0, 0, "tossubeax", + 0, 0, "tossubeax", }; if (flags & CF_CONST) { - flags &= ~CF_FORCECHAR; /* Handle chars as ints */ - g_push (flags & ~CF_CONST, 0); + flags &= ~CF_FORCECHAR; /* Handle chars as ints */ + g_push (flags & ~CF_CONST, 0); } oper (flags, val, ops); } @@ -2577,10 +2581,10 @@ void g_rsub (unsigned flags, unsigned long val) /* Primary = Primary - TOS */ { static char* ops [12] = { - 0, "tosrsuba0", "tosrsubax", - 0, "tosrsuba0", "tosrsubax", - 0, 0, "tosrsubeax", - 0, 0, "tosrsubeax", + 0, "tosrsuba0", "tosrsubax", + 0, "tosrsuba0", "tosrsubax", + 0, 0, "tosrsubeax", + 0, 0, "tosrsubeax", }; oper (flags, val, ops); } @@ -2591,19 +2595,19 @@ void g_mul (unsigned flags, unsigned long val) /* Primary = TOS * Primary */ { static char* ops [12] = { - 0, "tosmula0", "tosmulax", - 0, "tosumula0", "tosumulax", - 0, 0, "tosmuleax", - 0, 0, "tosumuleax", + 0, "tosmula0", "tosmulax", + 0, "tosumula0", "tosumulax", + 0, 0, "tosmuleax", + 0, 0, "tosumuleax", }; int p2; /* Do strength reduction if the value is constant and a power of two */ - if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) { - /* Generate a shift instead */ - g_asl (flags, p2); - return; + if (flags & CF_CONST && (p2 = PowerOf2 (val)) >= 0) { + /* Generate a shift instead */ + g_asl (flags, p2); + return; } /* If the right hand side is const, the lhs is not on stack but still @@ -2611,83 +2615,83 @@ void g_mul (unsigned flags, unsigned long val) */ if (flags & CF_CONST) { - switch (flags & CF_TYPE) { - - case CF_CHAR: - if (flags & CF_FORCECHAR) { - /* Handle some special cases */ - switch (val) { - - case 3: - AddCodeLine ("sta tmp1"); - AddCodeLine ("asl a"); - AddCodeLine ("clc"); - AddCodeLine ("adc tmp1"); - return; - - case 5: - AddCodeLine ("sta tmp1"); - AddCodeLine ("asl a"); - AddCodeLine ("asl a"); - AddCodeLine ("clc"); - 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: - switch (val) { - case 3: - AddCodeLine ("jsr mulax3"); - return; - case 5: - AddCodeLine ("jsr mulax5"); - return; - case 6: - AddCodeLine ("jsr mulax6"); - return; - case 7: - AddCodeLine ("jsr mulax7"); - return; - case 9: - AddCodeLine ("jsr mulax9"); - return; - case 10: - AddCodeLine ("jsr mulax10"); - return; - } - break; - - case CF_LONG: - break; - - default: - typeerror (flags); - } - - /* If we go here, we didn't emit code. Push the lhs on stack and fall - * into the normal, non-optimized stuff. - */ - flags &= ~CF_FORCECHAR; /* Handle chars as ints */ - g_push (flags & ~CF_CONST, 0); + switch (flags & CF_TYPE) { + + case CF_CHAR: + if (flags & CF_FORCECHAR) { + /* Handle some special cases */ + switch (val) { + + case 3: + AddCodeLine ("sta tmp1"); + AddCodeLine ("asl a"); + AddCodeLine ("clc"); + AddCodeLine ("adc tmp1"); + return; + + case 5: + AddCodeLine ("sta tmp1"); + AddCodeLine ("asl a"); + AddCodeLine ("asl a"); + AddCodeLine ("clc"); + 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: + switch (val) { + case 3: + AddCodeLine ("jsr mulax3"); + return; + case 5: + AddCodeLine ("jsr mulax5"); + return; + case 6: + AddCodeLine ("jsr mulax6"); + return; + case 7: + AddCodeLine ("jsr mulax7"); + return; + case 9: + AddCodeLine ("jsr mulax9"); + return; + case 10: + AddCodeLine ("jsr mulax10"); + return; + } + break; + + case CF_LONG: + break; + + default: + typeerror (flags); + } + + /* If we go here, we didn't emit code. Push the lhs on stack and fall + * into the normal, non-optimized stuff. + */ + flags &= ~CF_FORCECHAR; /* Handle chars as ints */ + g_push (flags & ~CF_CONST, 0); } @@ -2701,25 +2705,25 @@ void g_div (unsigned flags, unsigned long val) /* Primary = TOS / Primary */ { static char* ops [12] = { - 0, "tosdiva0", "tosdivax", - 0, "tosudiva0", "tosudivax", - 0, 0, "tosdiveax", - 0, 0, "tosudiveax", + 0, "tosdiva0", "tosdivax", + 0, "tosudiva0", "tosudivax", + 0, 0, "tosdiveax", + 0, 0, "tosudiveax", }; /* Do strength reduction if the value is constant and a power of two */ int p2; - if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) { - /* Generate a shift instead */ - g_asr (flags, p2); + if ((flags & CF_CONST) && (p2 = PowerOf2 (val)) >= 0) { + /* Generate a shift instead */ + g_asr (flags, p2); } else { - /* Generate a division */ - if (flags & CF_CONST) { - /* lhs is not on stack */ - flags &= ~CF_FORCECHAR; /* Handle chars as ints */ - g_push (flags & ~CF_CONST, 0); - } - oper (flags, val, ops); + /* Generate a division */ + if (flags & CF_CONST) { + /* lhs is not on stack */ + flags &= ~CF_FORCECHAR; /* Handle chars as ints */ + g_push (flags & ~CF_CONST, 0); + } + oper (flags, val, ops); } } @@ -2729,25 +2733,25 @@ void g_mod (unsigned flags, unsigned long val) /* Primary = TOS % Primary */ { static char* ops [12] = { - 0, "tosmoda0", "tosmodax", - 0, "tosumoda0", "tosumodax", - 0, 0, "tosmodeax", - 0, 0, "tosumodeax", + 0, "tosmoda0", "tosmodax", + 0, "tosumoda0", "tosumodax", + 0, 0, "tosmodeax", + 0, 0, "tosumodeax", }; int p2; /* Check if we can do some cost reduction */ - if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) { - /* We can do that with an AND operation */ - g_and (flags, val - 1); + if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = PowerOf2 (val)) >= 0) { + /* We can do that with an AND operation */ + g_and (flags, val - 1); } else { - /* Do it the hard way... */ - if (flags & CF_CONST) { - /* lhs is not on stack */ - flags &= ~CF_FORCECHAR; /* Handle chars as ints */ - g_push (flags & ~CF_CONST, 0); - } - oper (flags, val, ops); + /* Do it the hard way... */ + if (flags & CF_CONST) { + /* lhs is not on stack */ + flags &= ~CF_FORCECHAR; /* Handle chars as ints */ + g_push (flags & ~CF_CONST, 0); + } + oper (flags, val, ops); } } @@ -2757,10 +2761,10 @@ void g_or (unsigned flags, unsigned long val) /* Primary = TOS | Primary */ { static char* ops [12] = { - 0, "tosora0", "tosorax", - 0, "tosora0", "tosorax", - 0, 0, "tosoreax", - 0, 0, "tosoreax", + 0, "tosora0", "tosorax", + 0, "tosora0", "tosorax", + 0, 0, "tosoreax", + 0, 0, "tosoreax", }; /* If the right hand side is const, the lhs is not on stack but still @@ -2768,25 +2772,25 @@ void g_or (unsigned flags, unsigned long val) */ if (flags & CF_CONST) { - switch (flags & CF_TYPE) { + switch (flags & CF_TYPE) { - case CF_CHAR: - if (flags & CF_FORCECHAR) { - if ((val & 0xFF) != 0) { - AddCodeLine ("ora #$%02X", (unsigned char)val); - } - return; - } - /* FALLTHROUGH */ + case CF_CHAR: + if (flags & CF_FORCECHAR) { + if ((val & 0xFF) != 0) { + AddCodeLine ("ora #$%02X", (unsigned char)val); + } + return; + } + /* FALLTHROUGH */ - case CF_INT: - if (val <= 0xFF) { + case CF_INT: + if (val <= 0xFF) { if ((val & 0xFF) != 0) { - AddCodeLine ("ora #$%02X", (unsigned char)val); + AddCodeLine ("ora #$%02X", (unsigned char)val); } - } else if ((val & 0xFF00) == 0xFF00) { + } else if ((val & 0xFF00) == 0xFF00) { if ((val & 0xFF) != 0) { - AddCodeLine ("ora #$%02X", (unsigned char)val); + AddCodeLine ("ora #$%02X", (unsigned char)val); } ldxconst (0xFF); } else if (val != 0) { @@ -2797,27 +2801,27 @@ void g_or (unsigned flags, unsigned long val) AddCodeLine ("tax"); AddCodeLine ("pla"); } - return; + return; - case CF_LONG: - if (val <= 0xFF) { + case CF_LONG: + if (val <= 0xFF) { if ((val & 0xFF) != 0) { AddCodeLine ("ora #$%02X", (unsigned char)val); } - return; - } - break; + return; + } + 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. Note: The standard stuff will + /* If we go here, we didn't emit code. Push the lhs on stack and fall + * into the normal, non-optimized stuff. Note: The standard stuff will * always work with ints. - */ + */ flags &= ~CF_FORCECHAR; - g_push (flags & ~CF_CONST, 0); + g_push (flags & ~CF_CONST, 0); } /* Use long way over the stack */ @@ -2830,10 +2834,10 @@ void g_xor (unsigned flags, unsigned long val) /* Primary = TOS ^ Primary */ { static char* ops [12] = { - 0, "tosxora0", "tosxorax", - 0, "tosxora0", "tosxorax", - 0, 0, "tosxoreax", - 0, 0, "tosxoreax", + 0, "tosxora0", "tosxorax", + 0, "tosxora0", "tosxorax", + 0, 0, "tosxoreax", + 0, 0, "tosxoreax", }; @@ -2842,53 +2846,53 @@ void g_xor (unsigned flags, unsigned long val) */ if (flags & CF_CONST) { - switch (flags & CF_TYPE) { - - case CF_CHAR: - if (flags & CF_FORCECHAR) { - if ((val & 0xFF) != 0) { - AddCodeLine ("eor #$%02X", (unsigned char)val); - } - return; - } - /* FALLTHROUGH */ - - case CF_INT: - if (val <= 0xFF) { - if (val != 0) { - AddCodeLine ("eor #$%02X", (unsigned char)val); - } - } else if (val != 0) { + switch (flags & CF_TYPE) { + + case CF_CHAR: + if (flags & CF_FORCECHAR) { + if ((val & 0xFF) != 0) { + AddCodeLine ("eor #$%02X", (unsigned char)val); + } + return; + } + /* FALLTHROUGH */ + + case CF_INT: + if (val <= 0xFF) { + if (val != 0) { + AddCodeLine ("eor #$%02X", (unsigned char)val); + } + } else if (val != 0) { if ((val & 0xFF) != 0) { AddCodeLine ("eor #$%02X", (unsigned char)val); } - AddCodeLine ("pha"); - AddCodeLine ("txa"); - AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8)); - AddCodeLine ("tax"); - AddCodeLine ("pla"); - } - return; - - case CF_LONG: - if (val <= 0xFF) { - if (val != 0) { - AddCodeLine ("eor #$%02X", (unsigned char)val); - } - return; - } - break; - - default: - typeerror (flags); - } - - /* If we go here, we didn't emit code. Push the lhs on stack and fall - * into the normal, non-optimized stuff. Note: The standard stuff will + AddCodeLine ("pha"); + AddCodeLine ("txa"); + AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8)); + AddCodeLine ("tax"); + AddCodeLine ("pla"); + } + return; + + case CF_LONG: + if (val <= 0xFF) { + if (val != 0) { + AddCodeLine ("eor #$%02X", (unsigned char)val); + } + return; + } + break; + + default: + typeerror (flags); + } + + /* If we go here, we didn't emit code. Push the lhs on stack and fall + * into the normal, non-optimized stuff. Note: The standard stuff will * always work with ints. - */ + */ flags &= ~CF_FORCECHAR; - g_push (flags & ~CF_CONST, 0); + g_push (flags & ~CF_CONST, 0); } /* Use long way over the stack */ @@ -2901,10 +2905,10 @@ void g_and (unsigned Flags, unsigned long Val) /* Primary = TOS & Primary */ { static char* ops [12] = { - 0, "tosanda0", "tosandax", - 0, "tosanda0", "tosandax", - 0, 0, "tosandeax", - 0, 0, "tosandeax", + 0, "tosanda0", "tosandax", + 0, "tosanda0", "tosandax", + 0, 0, "tosandeax", + 0, 0, "tosandeax", }; /* If the right hand side is const, the lhs is not on stack but still @@ -2912,72 +2916,72 @@ void g_and (unsigned Flags, unsigned long Val) */ if (Flags & CF_CONST) { - switch (Flags & CF_TYPE) { + switch (Flags & CF_TYPE) { - case CF_CHAR: - if (Flags & CF_FORCECHAR) { + case CF_CHAR: + if (Flags & CF_FORCECHAR) { if ((Val & 0xFF) != 0xFF) { AddCodeLine ("and #$%02X", (unsigned char)Val); } - return; - } - /* FALLTHROUGH */ - case CF_INT: - if ((Val & 0xFFFF) != 0xFFFF) { - if (Val <= 0xFF) { - ldxconst (0); - if (Val == 0) { - ldaconst (0); - } else if (Val != 0xFF) { - AddCodeLine ("and #$%02X", (unsigned char)Val); - } - } else if ((Val & 0xFF00) == 0xFF00) { - AddCodeLine ("and #$%02X", (unsigned char)Val); - } else if ((Val & 0x00FF) == 0x0000) { - AddCodeLine ("txa"); - AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8)); - AddCodeLine ("tax"); - ldaconst (0); - } else { - AddCodeLine ("tay"); - AddCodeLine ("txa"); - AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8)); - AddCodeLine ("tax"); - AddCodeLine ("tya"); - if ((Val & 0x00FF) != 0x00FF) { - AddCodeLine ("and #$%02X", (unsigned char)Val); - } - } - } - return; - - case CF_LONG: - if (Val <= 0xFF) { - ldxconst (0); - AddCodeLine ("stx sreg+1"); - AddCodeLine ("stx sreg"); - if ((Val & 0xFF) != 0xFF) { - AddCodeLine ("and #$%02X", (unsigned char)Val); - } - return; - } else if (Val == 0xFF00) { - ldaconst (0); - AddCodeLine ("sta sreg+1"); - AddCodeLine ("sta sreg"); - return; - } - break; - - default: - typeerror (Flags); - } - - /* If we go here, we didn't emit code. Push the lhs on stack and fall - * into the normal, non-optimized stuff. Note: The standard stuff will + return; + } + /* FALLTHROUGH */ + case CF_INT: + if ((Val & 0xFFFF) != 0xFFFF) { + if (Val <= 0xFF) { + ldxconst (0); + if (Val == 0) { + ldaconst (0); + } else if (Val != 0xFF) { + AddCodeLine ("and #$%02X", (unsigned char)Val); + } + } else if ((Val & 0xFF00) == 0xFF00) { + AddCodeLine ("and #$%02X", (unsigned char)Val); + } else if ((Val & 0x00FF) == 0x0000) { + AddCodeLine ("txa"); + AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8)); + AddCodeLine ("tax"); + ldaconst (0); + } else { + AddCodeLine ("tay"); + AddCodeLine ("txa"); + AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8)); + AddCodeLine ("tax"); + AddCodeLine ("tya"); + if ((Val & 0x00FF) != 0x00FF) { + AddCodeLine ("and #$%02X", (unsigned char)Val); + } + } + } + return; + + case CF_LONG: + if (Val <= 0xFF) { + ldxconst (0); + AddCodeLine ("stx sreg+1"); + AddCodeLine ("stx sreg"); + if ((Val & 0xFF) != 0xFF) { + AddCodeLine ("and #$%02X", (unsigned char)Val); + } + return; + } else if (Val == 0xFF00) { + ldaconst (0); + AddCodeLine ("sta sreg+1"); + AddCodeLine ("sta sreg"); + return; + } + break; + + default: + typeerror (Flags); + } + + /* If we go here, we didn't emit code. Push the lhs on stack and fall + * into the normal, non-optimized stuff. Note: The standard stuff will * always work with ints. - */ + */ Flags &= ~CF_FORCECHAR; - g_push (Flags & ~CF_CONST, 0); + g_push (Flags & ~CF_CONST, 0); } /* Use long way over the stack */ @@ -2990,10 +2994,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, 0, "tosasrax", + 0, 0, "tosshrax", + 0, 0, "tosasreax", + 0, 0, "tosshreax", }; /* If the right hand side is const, the lhs is not on stack but still @@ -3001,73 +3005,73 @@ void g_asr (unsigned flags, unsigned long val) */ if (flags & CF_CONST) { - switch (flags & CF_TYPE) { - - case CF_CHAR: - case CF_INT: - if (val >= 8 && (flags & CF_UNSIGNED)) { - AddCodeLine ("txa"); - ldxconst (0); - val -= 8; - } - if (val == 0) { - /* Done */ - return; - } 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 == 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) { - 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); - } - - /* If we go here, we didn't emit code. Push the lhs on stack and fall - * into the normal, non-optimized stuff. Note: The standard stuff will + switch (flags & CF_TYPE) { + + case CF_CHAR: + case CF_INT: + if (val >= 8 && (flags & CF_UNSIGNED)) { + AddCodeLine ("txa"); + ldxconst (0); + val -= 8; + } + if (val == 0) { + /* Done */ + return; + } 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 == 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) { + 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); + } + + /* If we go here, we didn't emit code. Push the lhs on stack and fall + * into the normal, non-optimized stuff. Note: The standard stuff will * always work with ints. - */ + */ flags &= ~CF_FORCECHAR; - g_push (flags & ~CF_CONST, 0); + g_push (flags & ~CF_CONST, 0); } /* Use long way over the stack */ @@ -3080,10 +3084,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, 0, "tosaslax", + 0, 0, "tosshlax", + 0, 0, "tosasleax", + 0, 0, "tosshleax", }; @@ -3092,65 +3096,65 @@ void g_asl (unsigned flags, unsigned long val) */ if (flags & CF_CONST) { - switch (flags & CF_TYPE) { - - case CF_CHAR: - case CF_INT: - if (val >= 8) { - AddCodeLine ("tax"); - AddCodeLine ("lda #$00"); - val -= 8; - } - if (val == 0) { - /* Done */ - return; - } else if (val >= 1 && val <= 4) { - if (flags & CF_UNSIGNED) { - AddCodeLine ("jsr shlax%ld", val); - } else { - AddCodeLine ("jsr aslax%ld", val); - } - return; - } - break; - - case CF_LONG: - if (val == 0) { - /* Nothing to do */ - return; - } else if (val >= 1 && val <= 4) { - if (flags & CF_UNSIGNED) { - AddCodeLine ("jsr shleax%ld", val); - } else { - AddCodeLine ("jsr asleax%ld", val); - } - return; - } else if (val == 8) { - AddCodeLine ("ldy sreg"); - AddCodeLine ("sty sreg+1"); - AddCodeLine ("stx sreg"); - AddCodeLine ("tax"); - AddCodeLine ("lda #$00"); - return; - } else if (val == 16) { - AddCodeLine ("stx sreg+1"); - AddCodeLine ("sta sreg"); - AddCodeLine ("lda #$00"); - AddCodeLine ("tax"); - return; - } - break; - - default: - typeerror (flags); - } - - /* If we go here, we didn't emit code. Push the lhs on stack and fall - * into the normal, non-optimized stuff. Note: The standard stuff will + switch (flags & CF_TYPE) { + + case CF_CHAR: + case CF_INT: + if (val >= 8) { + AddCodeLine ("tax"); + AddCodeLine ("lda #$00"); + val -= 8; + } + if (val == 0) { + /* Done */ + return; + } else if (val >= 1 && val <= 4) { + if (flags & CF_UNSIGNED) { + AddCodeLine ("jsr shlax%ld", val); + } else { + AddCodeLine ("jsr aslax%ld", val); + } + return; + } + break; + + case CF_LONG: + if (val == 0) { + /* Nothing to do */ + return; + } else if (val >= 1 && val <= 4) { + if (flags & CF_UNSIGNED) { + AddCodeLine ("jsr shleax%ld", val); + } else { + AddCodeLine ("jsr asleax%ld", val); + } + return; + } else if (val == 8) { + AddCodeLine ("ldy sreg"); + AddCodeLine ("sty sreg+1"); + AddCodeLine ("stx sreg"); + AddCodeLine ("tax"); + AddCodeLine ("lda #$00"); + return; + } else if (val == 16) { + AddCodeLine ("stx sreg+1"); + AddCodeLine ("sta sreg"); + AddCodeLine ("lda #$00"); + AddCodeLine ("tax"); + return; + } + break; + + default: + typeerror (flags); + } + + /* If we go here, we didn't emit code. Push the lhs on stack and fall + * into the normal, non-optimized stuff. Note: The standard stuff will * always work with ints. - */ + */ flags &= ~CF_FORCECHAR; - g_push (flags & ~CF_CONST, 0); + g_push (flags & ~CF_CONST, 0); } /* Use long way over the stack */ @@ -3164,7 +3168,7 @@ void g_neg (unsigned Flags) { switch (Flags & CF_TYPE) { - case CF_CHAR: + case CF_CHAR: if (Flags & CF_FORCECHAR) { AddCodeLine ("eor #$FF"); AddCodeLine ("clc"); @@ -3173,16 +3177,16 @@ void g_neg (unsigned Flags) } /* FALLTHROUGH */ - case CF_INT: - AddCodeLine ("jsr negax"); - break; + case CF_INT: + AddCodeLine ("jsr negax"); + break; - case CF_LONG: - AddCodeLine ("jsr negeax"); - break; + case CF_LONG: + AddCodeLine ("jsr negeax"); + break; - default: - typeerror (Flags); + default: + typeerror (Flags); } } @@ -3193,20 +3197,20 @@ void g_bneg (unsigned flags) { switch (flags & CF_TYPE) { - case CF_CHAR: - AddCodeLine ("jsr bnega"); - break; + case CF_CHAR: + AddCodeLine ("jsr bnega"); + break; - case CF_INT: - AddCodeLine ("jsr bnegax"); - break; + case CF_INT: + AddCodeLine ("jsr bnegax"); + break; - case CF_LONG: - AddCodeLine ("jsr bnegeax"); - break; + case CF_LONG: + AddCodeLine ("jsr bnegeax"); + break; - default: - typeerror (flags); + default: + typeerror (flags); } } @@ -3217,23 +3221,23 @@ void g_com (unsigned Flags) { switch (Flags & CF_TYPE) { - case CF_CHAR: + case CF_CHAR: if (Flags & CF_FORCECHAR) { AddCodeLine ("eor #$FF"); return; } /* FALLTHROUGH */ - case CF_INT: - AddCodeLine ("jsr complax"); - break; + case CF_INT: + AddCodeLine ("jsr complax"); + break; - case CF_LONG: - AddCodeLine ("jsr compleax"); - break; + case CF_LONG: + AddCodeLine ("jsr compleax"); + break; - default: - typeerror (Flags); + default: + typeerror (Flags); } } @@ -3244,89 +3248,89 @@ void g_inc (unsigned flags, unsigned long val) { /* Don't inc by zero */ if (val == 0) { - return; + return; } /* Generate code for the supported types */ flags &= ~CF_CONST; switch (flags & CF_TYPE) { - case CF_CHAR: - if (flags & CF_FORCECHAR) { - if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) { - while (val--) { - AddCodeLine ("ina"); - } - } else { - AddCodeLine ("clc"); - AddCodeLine ("adc #$%02X", (unsigned char)val); - } - break; - } - /* FALLTHROUGH */ - - case CF_INT: - if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) { - 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) { - AddCodeLine ("jsr incax%lu", val); - } else if (val <= 255) { - ldyconst (val); - AddCodeLine ("jsr incaxy"); - } else { - g_add (flags | CF_CONST, val); - } - } else { - /* Inline the code */ - if (val <= 0x300) { - if ((val & 0xFF) != 0) { - unsigned L = GetLocalLabel(); - AddCodeLine ("clc"); - AddCodeLine ("adc #$%02X", (unsigned char) val); - AddCodeLine ("bcc %s", LocalLabelName (L)); - AddCodeLine ("inx"); - g_defcodelabel (L); - } - if (val >= 0x100) { - AddCodeLine ("inx"); - } - if (val >= 0x200) { - AddCodeLine ("inx"); - } - if (val >= 0x300) { - AddCodeLine ("inx"); - } - } else { - AddCodeLine ("clc"); - if ((val & 0xFF) != 0) { - AddCodeLine ("adc #$%02X", (unsigned char) val); - } - AddCodeLine ("pha"); - AddCodeLine ("txa"); - AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8)); - AddCodeLine ("tax"); - AddCodeLine ("pla"); - } - } - break; - - case CF_LONG: - if (val <= 255) { - ldyconst (val); - AddCodeLine ("jsr inceaxy"); - } else { - g_add (flags | CF_CONST, val); - } - break; - - default: - typeerror (flags); + case CF_CHAR: + if (flags & CF_FORCECHAR) { + if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) { + while (val--) { + AddCodeLine ("ina"); + } + } else { + AddCodeLine ("clc"); + AddCodeLine ("adc #$%02X", (unsigned char)val); + } + break; + } + /* FALLTHROUGH */ + + case CF_INT: + if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) { + unsigned L = GetLocalLabel(); + AddCodeLine ("ina"); + AddCodeLine ("bne %s", LocalLabelName (L)); + AddCodeLine ("inx"); + g_defcodelabel (L); + } else if (IS_Get (&CodeSizeFactor) < 200) { + /* Use jsr calls */ + if (val <= 8) { + AddCodeLine ("jsr incax%lu", val); + } else if (val <= 255) { + ldyconst (val); + AddCodeLine ("jsr incaxy"); + } else { + g_add (flags | CF_CONST, val); + } + } else { + /* Inline the code */ + if (val <= 0x300) { + if ((val & 0xFF) != 0) { + unsigned L = GetLocalLabel(); + AddCodeLine ("clc"); + AddCodeLine ("adc #$%02X", (unsigned char) val); + AddCodeLine ("bcc %s", LocalLabelName (L)); + AddCodeLine ("inx"); + g_defcodelabel (L); + } + if (val >= 0x100) { + AddCodeLine ("inx"); + } + if (val >= 0x200) { + AddCodeLine ("inx"); + } + if (val >= 0x300) { + AddCodeLine ("inx"); + } + } else { + AddCodeLine ("clc"); + if ((val & 0xFF) != 0) { + AddCodeLine ("adc #$%02X", (unsigned char) val); + } + AddCodeLine ("pha"); + AddCodeLine ("txa"); + AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8)); + AddCodeLine ("tax"); + AddCodeLine ("pla"); + } + } + break; + + case CF_LONG: + if (val <= 255) { + ldyconst (val); + AddCodeLine ("jsr inceaxy"); + } else { + g_add (flags | CF_CONST, val); + } + break; + + default: + typeerror (flags); } } @@ -3338,80 +3342,80 @@ void g_dec (unsigned flags, unsigned long val) { /* Don't dec by zero */ if (val == 0) { - return; + return; } /* Generate code for the supported types */ flags &= ~CF_CONST; switch (flags & CF_TYPE) { - case CF_CHAR: - if (flags & CF_FORCECHAR) { - if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) { - while (val--) { - AddCodeLine ("dea"); - } - } else { - AddCodeLine ("sec"); - AddCodeLine ("sbc #$%02X", (unsigned char)val); - } - break; - } - /* FALLTHROUGH */ - - case CF_INT: - if (CodeSizeFactor < 200) { - /* Use subroutines */ - if (val <= 8) { - AddCodeLine ("jsr decax%d", (int) val); - } else if (val <= 255) { - ldyconst (val); - AddCodeLine ("jsr decaxy"); - } else { - g_sub (flags | CF_CONST, val); - } - } else { - /* Inline the code */ - if (val < 0x300) { - if ((val & 0xFF) != 0) { - unsigned L = GetLocalLabel(); - AddCodeLine ("sec"); - AddCodeLine ("sbc #$%02X", (unsigned char) val); - AddCodeLine ("bcs %s", LocalLabelName (L)); - AddCodeLine ("dex"); - g_defcodelabel (L); - } - if (val >= 0x100) { - AddCodeLine ("dex"); - } - if (val >= 0x200) { - AddCodeLine ("dex"); - } - } else { - AddCodeLine ("sec"); - if ((val & 0xFF) != 0) { - AddCodeLine ("sbc #$%02X", (unsigned char) val); - } - AddCodeLine ("pha"); - AddCodeLine ("txa"); - AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8)); - AddCodeLine ("tax"); - AddCodeLine ("pla"); - } - } - break; - - case CF_LONG: - if (val <= 255) { - ldyconst (val); - AddCodeLine ("jsr deceaxy"); - } else { - g_sub (flags | CF_CONST, val); - } - break; - - default: - typeerror (flags); + case CF_CHAR: + if (flags & CF_FORCECHAR) { + if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) { + while (val--) { + AddCodeLine ("dea"); + } + } else { + AddCodeLine ("sec"); + AddCodeLine ("sbc #$%02X", (unsigned char)val); + } + break; + } + /* FALLTHROUGH */ + + case CF_INT: + if (IS_Get (&CodeSizeFactor) < 200) { + /* Use subroutines */ + if (val <= 8) { + AddCodeLine ("jsr decax%d", (int) val); + } else if (val <= 255) { + ldyconst (val); + AddCodeLine ("jsr decaxy"); + } else { + g_sub (flags | CF_CONST, val); + } + } else { + /* Inline the code */ + if (val < 0x300) { + if ((val & 0xFF) != 0) { + unsigned L = GetLocalLabel(); + AddCodeLine ("sec"); + AddCodeLine ("sbc #$%02X", (unsigned char) val); + AddCodeLine ("bcs %s", LocalLabelName (L)); + AddCodeLine ("dex"); + g_defcodelabel (L); + } + if (val >= 0x100) { + AddCodeLine ("dex"); + } + if (val >= 0x200) { + AddCodeLine ("dex"); + } + } else { + AddCodeLine ("sec"); + if ((val & 0xFF) != 0) { + AddCodeLine ("sbc #$%02X", (unsigned char) val); + } + AddCodeLine ("pha"); + AddCodeLine ("txa"); + AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8)); + AddCodeLine ("tax"); + AddCodeLine ("pla"); + } + } + break; + + case CF_LONG: + if (val <= 255) { + ldyconst (val); + AddCodeLine ("jsr deceaxy"); + } else { + g_sub (flags | CF_CONST, val); + } + break; + + default: + typeerror (flags); } } @@ -3430,10 +3434,10 @@ void g_eq (unsigned flags, unsigned long val) /* Test for equal */ { static char* ops [12] = { - "toseq00", "toseqa0", "toseqax", - "toseq00", "toseqa0", "toseqax", - 0, 0, "toseqeax", - 0, 0, "toseqeax", + "toseq00", "toseqa0", "toseqax", + "toseq00", "toseqa0", "toseqax", + 0, 0, "toseqeax", + 0, 0, "toseqeax", }; unsigned L; @@ -3443,38 +3447,38 @@ void g_eq (unsigned flags, unsigned long val) */ if (flags & CF_CONST) { - switch (flags & CF_TYPE) { - - case CF_CHAR: - if (flags & CF_FORCECHAR) { - AddCodeLine ("cmp #$%02X", (unsigned char)val); - AddCodeLine ("jsr booleq"); - return; - } - /* FALLTHROUGH */ - - case CF_INT: - 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: - break; - - default: - typeerror (flags); - } - - /* If we go here, we didn't emit code. Push the lhs on stack and fall - * into the normal, non-optimized stuff. Note: The standard stuff will + switch (flags & CF_TYPE) { + + case CF_CHAR: + if (flags & CF_FORCECHAR) { + AddCodeLine ("cmp #$%02X", (unsigned char)val); + AddCodeLine ("jsr booleq"); + return; + } + /* FALLTHROUGH */ + + case CF_INT: + 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: + break; + + default: + typeerror (flags); + } + + /* If we go here, we didn't emit code. Push the lhs on stack and fall + * into the normal, non-optimized stuff. Note: The standard stuff will * always work with ints. - */ + */ flags &= ~CF_FORCECHAR; - g_push (flags & ~CF_CONST, 0); + g_push (flags & ~CF_CONST, 0); } /* Use long way over the stack */ @@ -3487,10 +3491,10 @@ void g_ne (unsigned flags, unsigned long val) /* Test for not equal */ { static char* ops [12] = { - "tosne00", "tosnea0", "tosneax", - "tosne00", "tosnea0", "tosneax", - 0, 0, "tosneeax", - 0, 0, "tosneeax", + "tosne00", "tosnea0", "tosneax", + "tosne00", "tosnea0", "tosneax", + 0, 0, "tosneeax", + 0, 0, "tosneeax", }; unsigned L; @@ -3500,38 +3504,38 @@ void g_ne (unsigned flags, unsigned long val) */ if (flags & CF_CONST) { - switch (flags & CF_TYPE) { - - case CF_CHAR: - if (flags & CF_FORCECHAR) { - AddCodeLine ("cmp #$%02X", (unsigned char)val); - AddCodeLine ("jsr boolne"); - return; - } - /* FALLTHROUGH */ - - case CF_INT: - 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: - break; - - default: - typeerror (flags); - } - - /* If we go here, we didn't emit code. Push the lhs on stack and fall - * into the normal, non-optimized stuff. Note: The standard stuff will + switch (flags & CF_TYPE) { + + case CF_CHAR: + if (flags & CF_FORCECHAR) { + AddCodeLine ("cmp #$%02X", (unsigned char)val); + AddCodeLine ("jsr boolne"); + return; + } + /* FALLTHROUGH */ + + case CF_INT: + 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: + break; + + default: + typeerror (flags); + } + + /* If we go here, we didn't emit code. Push the lhs on stack and fall + * into the normal, non-optimized stuff. Note: The standard stuff will * always work with ints. - */ + */ flags &= ~CF_FORCECHAR; - g_push (flags & ~CF_CONST, 0); + g_push (flags & ~CF_CONST, 0); } /* Use long way over the stack */ @@ -3544,10 +3548,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 @@ -3637,12 +3641,12 @@ void g_lt (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. Note: The standard stuff will + /* If we go here, we didn't emit code. Push the lhs on stack and fall + * into the normal, non-optimized stuff. Note: The standard stuff will * always work with ints. - */ + */ flags &= ~CF_FORCECHAR; - g_push (flags & ~CF_CONST, 0); + g_push (flags & ~CF_CONST, 0); } /* Use long way over the stack */ @@ -3655,10 +3659,10 @@ void g_le (unsigned flags, unsigned long val) /* Test for less than or equal to */ { static char* ops [12] = { - "tosle00", "toslea0", "tosleax", - "tosule00", "tosulea0", "tosuleax", - 0, 0, "tosleeax", - 0, 0, "tosuleeax", + "tosle00", "toslea0", "tosleax", + "tosule00", "tosulea0", "tosuleax", + 0, 0, "tosleeax", + 0, 0, "tosuleeax", }; @@ -3667,100 +3671,100 @@ void g_le (unsigned flags, unsigned long val) */ if (flags & CF_CONST) { - /* Look at the type */ - switch (flags & CF_TYPE) { - - 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; - } - /* FALLTHROUGH */ - - case CF_INT: - if (flags & CF_UNSIGNED) { - /* 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"); - } - } - return; - - case CF_LONG: - 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); - } - - /* If we go here, we didn't emit code. Push the lhs on stack and fall - * into the normal, non-optimized stuff. Note: The standard stuff will + /* Look at the type */ + switch (flags & CF_TYPE) { + + 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; + } + /* FALLTHROUGH */ + + case CF_INT: + if (flags & CF_UNSIGNED) { + /* 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"); + } + } + return; + + case CF_LONG: + 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); + } + + /* If we go here, we didn't emit code. Push the lhs on stack and fall + * into the normal, non-optimized stuff. Note: The standard stuff will * always work with ints. - */ + */ flags &= ~CF_FORCECHAR; - g_push (flags & ~CF_CONST, 0); + g_push (flags & ~CF_CONST, 0); } /* Use long way over the stack */ @@ -3773,10 +3777,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", }; @@ -3785,116 +3789,116 @@ void g_gt (unsigned flags, unsigned long val) */ if (flags & CF_CONST) { - /* Look at the type */ - switch (flags & CF_TYPE) { - - 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) { - /* 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 { - /* 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: - 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); - } - - /* If we go here, we didn't emit code. Push the lhs on stack and fall - * into the normal, non-optimized stuff. Note: The standard stuff will + /* Look at the type */ + switch (flags & CF_TYPE) { + + 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) { + /* 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 { + /* 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: + 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); + } + + /* If we go here, we didn't emit code. Push the lhs on stack and fall + * into the normal, non-optimized stuff. Note: The standard stuff will * always work with ints. - */ + */ flags &= ~CF_FORCECHAR; - g_push (flags & ~CF_CONST, 0); + g_push (flags & ~CF_CONST, 0); } /* Use long way over the stack */ @@ -3907,10 +3911,10 @@ void g_ge (unsigned flags, unsigned long val) /* Test for greater than or equal to */ { static char* ops [12] = { - "tosge00", "tosgea0", "tosgeax", - "tosuge00", "tosugea0", "tosugeax", - 0, 0, "tosgeeax", - 0, 0, "tosugeeax", + "tosge00", "tosgea0", "tosgeax", + "tosuge00", "tosugea0", "tosugeax", + 0, 0, "tosgeeax", + 0, 0, "tosugeeax", }; @@ -4002,12 +4006,12 @@ void g_ge (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. Note: The standard stuff will + /* If we go here, we didn't emit code. Push the lhs on stack and fall + * into the normal, non-optimized stuff. Note: The standard stuff will * always work with ints. - */ + */ flags &= ~CF_FORCECHAR; - g_push (flags & ~CF_CONST, 0); + g_push (flags & ~CF_CONST, 0); } /* Use long way over the stack */ @@ -4017,7 +4021,7 @@ void g_ge (unsigned flags, unsigned long val) /*****************************************************************************/ -/* Allocating static storage */ +/* Allocating static storage */ /*****************************************************************************/ @@ -4035,34 +4039,34 @@ void g_defdata (unsigned flags, unsigned long val, long offs) { if (flags & CF_CONST) { - /* Numeric constant */ - switch (flags & CF_TYPE) { + /* Numeric constant */ + switch (flags & CF_TYPE) { - case CF_CHAR: - AddDataLine ("\t.byte\t$%02lX", val & 0xFF); - break; + case CF_CHAR: + AddDataLine ("\t.byte\t$%02lX", val & 0xFF); + break; - case CF_INT: - AddDataLine ("\t.word\t$%04lX", val & 0xFFFF); - break; + case CF_INT: + AddDataLine ("\t.word\t$%04lX", val & 0xFFFF); + break; - case CF_LONG: - AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF); - break; + case CF_LONG: + AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF); + break; - default: - typeerror (flags); - break; + default: + typeerror (flags); + break; - } + } } else { - /* Create the correct label name */ - const char* Label = GetLabelName (flags, val, offs); + /* Create the correct label name */ + const char* Label = GetLabelName (flags, val, offs); - /* Labels are always 16 bit */ - AddDataLine ("\t.addr\t%s", Label); + /* Labels are always 16 bit */ + AddDataLine ("\t.addr\t%s", Label); } } @@ -4082,24 +4086,24 @@ void g_defbytes (const void* Bytes, unsigned Count) /* Output the stuff */ while (Count) { - /* How many go into this line? */ - if ((Chunk = Count) > 16) { - Chunk = 16; - } - Count -= Chunk; - - /* Output one line */ - strcpy (Buf, "\t.byte\t"); - B = Buf + 7; - do { - B += sprintf (B, "$%02X", *Data++); - if (--Chunk) { - *B++ = ','; - } - } while (Chunk); - - /* Output the line */ - AddDataLine (Buf); + /* How many go into this line? */ + if ((Chunk = Count) > 16) { + Chunk = 16; + } + Count -= Chunk; + + /* Output one line */ + strcpy (Buf, "\t.byte\t"); + B = Buf + 7; + do { + B += sprintf (B, "$%02X", *Data++); + if (--Chunk) { + *B++ = ','; + } + } while (Chunk); + + /* Output the line */ + AddDataLine (Buf); } } @@ -4138,14 +4142,14 @@ void g_initauto (unsigned Label, unsigned Size) CheckLocalOffs (Size); if (Size <= 128) { ldyconst (Size-1); - g_defcodelabel (CodeLabel); + 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); + g_defcodelabel (CodeLabel); AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0)); AddCodeLine ("sta (sp),y"); AddCodeLine ("iny"); @@ -4170,7 +4174,7 @@ void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size) } else if (Size <= 256) { unsigned CodeLabel = GetLocalLabel (); ldyconst (0); - g_defcodelabel (CodeLabel); + g_defcodelabel (CodeLabel); AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0)); AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0)); AddCodeLine ("iny"); @@ -4190,7 +4194,7 @@ void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size) /*****************************************************************************/ -/* Switch statement */ +/* Switch statement */ /*****************************************************************************/ @@ -4204,61 +4208,61 @@ void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth) /* 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); + 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); + /* 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; - } + /* If we have a next label, define it */ + if (NextLabel) { + g_defcodelabel (NextLabel); + NextLabel = 0; + } - /* Do the compare */ - AddCodeLine (Compare, CN_GetValue (N)); + /* 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) { + /* 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)); + /* Branch if equal */ + g_falsejump (0, CN_GetLabel (N)); - } else { + } 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); - } + /* 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); + /* Check the next level */ + g_switch (N->Nodes, DefaultLabel, Depth-1); - } + } } /* If we go here, we haven't found the label */ @@ -4268,7 +4272,7 @@ void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth) /*****************************************************************************/ -/* User supplied assembler code */ +/* User supplied assembler code */ /*****************************************************************************/