X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fcc65%2Fcodegen.c;h=2b61e7a2acaba3fbf65c5c87f5ea795b52b86d45;hb=1366b6cbea1b374fa7502354cf2cb8a971f71c5f;hp=7732f7f3437f794049b623a23a099fd6633e4c84;hpb=b3af17126c3ac6178e45b090555bed5f34507eaf;p=cc65 diff --git a/src/cc65/codegen.c b/src/cc65/codegen.c index 7732f7f34..2b61e7a2a 100644 --- a/src/cc65/codegen.c +++ b/src/cc65/codegen.c @@ -39,6 +39,7 @@ /* common */ #include "check.h" +#include "strbuf.h" #include "version.h" #include "xmalloc.h" #include "xsprintf.h" @@ -46,6 +47,7 @@ /* cc65 */ #include "asmcode.h" #include "asmlabel.h" +#include "casenode.h" #include "codeseg.h" #include "cpu.h" #include "dataseg.h" @@ -169,7 +171,7 @@ void g_preamble (void) AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off"); /* Import the stack pointer for direct auto variable access */ - AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1"); + AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2"); /* Define long branch macros */ AddTextLine ("\t.macpack\tlongbranch"); @@ -607,7 +609,7 @@ void g_getimmed (unsigned Flags, unsigned long Val, unsigned Offs) if (B2 == B4) { AddCodeLine ("stx sreg+1"); Done |= 0x08; - } + } if ((Done & 0x04) == 0 && B1 != B3) { AddCodeLine ("lda #$%02X", B3); AddCodeLine ("sta sreg"); @@ -717,12 +719,8 @@ void g_getlocal (unsigned flags, int offs) case CF_CHAR: if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) { - if (CPU == CPU_65C02 && offs == 0) { - AddCodeLine ("lda (sp)"); - } else { - ldyconst (offs); - AddCodeLine ("lda (sp),y"); - } + ldyconst (offs); + AddCodeLine ("lda (sp),y"); } else { ldyconst (offs); AddCodeLine ("ldx #$00"); @@ -751,23 +749,15 @@ void g_getlocal (unsigned flags, int offs) AddCodeLine ("dey"); AddCodeLine ("lda (sp),y"); } else { - if (offs) { - ldyconst (offs+1); - AddCodeLine ("jsr ldaxysp"); - } else { - AddCodeLine ("jsr ldax0sp"); - } + ldyconst (offs+1); + AddCodeLine ("jsr ldaxysp"); } } break; case CF_LONG: - if (offs) { - ldyconst (offs+3); - AddCodeLine ("jsr ldeaxysp"); - } else { - AddCodeLine ("jsr ldeax0sp"); - } + ldyconst (offs+3); + AddCodeLine ("jsr ldeaxysp"); break; default: @@ -811,22 +801,14 @@ void g_getind (unsigned flags, unsigned offs) AddCodeLine ("iny"); AddCodeLine ("ora (ptr1),y"); } else { - if (offs == 0) { - AddCodeLine ("jsr ldaxi"); - } else { - ldyconst (offs+1); - AddCodeLine ("jsr ldaxidx"); - } + ldyconst (offs+1); + AddCodeLine ("jsr ldaxidx"); } break; case CF_LONG: - if (offs == 0) { - AddCodeLine ("jsr ldeaxi"); - } else { - ldyconst (offs+3); - AddCodeLine ("jsr ldeaxidx"); - } + ldyconst (offs+3); + AddCodeLine ("jsr ldeaxidx"); if (flags & CF_TEST) { AddCodeLine ("jsr tsteax"); } @@ -895,12 +877,8 @@ void g_leavariadic (int Offs) CheckLocalOffs (ArgSizeOffs); /* Get the size of all parameters. */ - if (ArgSizeOffs == 0 && CPU == CPU_65C02) { - AddCodeLine ("lda (sp)"); - } else { - ldyconst (ArgSizeOffs); - AddCodeLine ("lda (sp),y"); - } + ldyconst (ArgSizeOffs); + AddCodeLine ("lda (sp),y"); /* Add the value of the stackpointer */ if (CodeSizeFactor > 250) { @@ -977,12 +955,8 @@ void g_putlocal (unsigned Flags, int Offs, long Val) if (Flags & CF_CONST) { AddCodeLine ("lda #$%02X", (unsigned char) Val); } - if (CPU == CPU_65C02 && Offs == 0) { - AddCodeLine ("sta (sp)"); - } else { - ldyconst (Offs); - AddCodeLine ("sta (sp),y"); - } + ldyconst (Offs); + AddCodeLine ("sta (sp),y"); break; case CF_INT: @@ -994,41 +968,25 @@ void g_putlocal (unsigned Flags, int Offs, long Val) /* Place high byte into X */ AddCodeLine ("tax"); } - if (CPU == CPU_65C02 && Offs == 0) { - AddCodeLine ("lda #$%02X", (unsigned char) Val); - AddCodeLine ("sta (sp)"); + if ((Val & 0xFF) == Offs+1) { + /* The value we need is already in Y */ + AddCodeLine ("tya"); + AddCodeLine ("dey"); } else { - if ((Val & 0xFF) == Offs+1) { - /* The value we need is already in Y */ - AddCodeLine ("tya"); - AddCodeLine ("dey"); - } else { - AddCodeLine ("dey"); - AddCodeLine ("lda #$%02X", (unsigned char) Val); - } - AddCodeLine ("sta (sp),y"); + AddCodeLine ("dey"); + AddCodeLine ("lda #$%02X", (unsigned char) Val); } + AddCodeLine ("sta (sp),y"); } else { if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) { - if (Offs) { - ldyconst (Offs); - AddCodeLine ("jsr staxysp"); - } else { - AddCodeLine ("jsr stax0sp"); - } + ldyconst (Offs); + AddCodeLine ("jsr staxysp"); } else { - if (CPU == CPU_65C02 && Offs == 0) { - AddCodeLine ("sta (sp)"); - ldyconst (1); - AddCodeLine ("txa"); - AddCodeLine ("sta (sp),y"); - } else { - ldyconst (Offs); - AddCodeLine ("sta (sp),y"); - AddCodeLine ("iny"); - AddCodeLine ("txa"); - AddCodeLine ("sta (sp),y"); - } + ldyconst (Offs); + AddCodeLine ("sta (sp),y"); + AddCodeLine ("iny"); + AddCodeLine ("txa"); + AddCodeLine ("sta (sp),y"); } } break; @@ -1037,12 +995,8 @@ void g_putlocal (unsigned Flags, int Offs, long Val) if (Flags & CF_CONST) { g_getimmed (Flags, Val, 0); } - if (Offs) { - ldyconst (Offs); - AddCodeLine ("jsr steaxysp"); - } else { - AddCodeLine ("jsr steax0sp"); - } + ldyconst (Offs); + AddCodeLine ("jsr steaxysp"); break; default: @@ -1530,28 +1484,6 @@ void g_addstatic (unsigned flags, unsigned long label, unsigned offs) -/*****************************************************************************/ -/* Compares of ax with a variable with fixed address */ -/*****************************************************************************/ - - - -void g_cmplocal (unsigned flags, int offs) -/* Compare a local variable to ax */ -{ - Internal ("g_cmplocal not implemented"); -} - - - -void g_cmpstatic (unsigned flags, unsigned label, unsigned offs) -/* Compare a static variable to ax */ -{ - Internal ("g_cmpstatic not implemented"); -} - - - /*****************************************************************************/ /* Special op= functions */ /*****************************************************************************/ @@ -1704,23 +1636,35 @@ void g_addeqlocal (unsigned flags, int offs, unsigned long val) /* FALLTHROUGH */ case CF_INT: - if (flags & CF_CONST) { - g_getimmed (flags, val, 0); - } ldyconst (offs); - AddCodeLine ("jsr addeqysp"); + 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); } - if (offs == 0) { - AddCodeLine ("jsr laddeq0sp"); - } else { - ldyconst (offs); - AddCodeLine ("jsr laddeqysp"); - } + ldyconst (offs); + AddCodeLine ("jsr laddeqysp"); break; default: @@ -1868,12 +1812,8 @@ void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs, AddCodeLine ("ldy #<(%s)", lbuf); AddCodeLine ("sty ptr1"); AddCodeLine ("ldy #>(%s+1)", lbuf); - if (val == 1) { - AddCodeLine ("jsr lsubeq1"); - } else { - AddCodeLine ("lda #$%02X", (unsigned char)val); - AddCodeLine ("jsr lsubeqa"); - } + AddCodeLine ("lda #$%02X", (unsigned char)val); + AddCodeLine ("jsr lsubeqa"); } else { g_getstatic (flags, label, offs); g_dec (flags, val); @@ -1932,24 +1872,16 @@ void g_subeqlocal (unsigned flags, int offs, unsigned long val) if (flags & CF_CONST) { g_getimmed (flags, val, 0); } - if (offs == 0) { - AddCodeLine ("jsr subeq0sp"); - } else { - ldyconst (offs); - AddCodeLine ("jsr subeqysp"); - } + ldyconst (offs); + AddCodeLine ("jsr subeqysp"); break; case CF_LONG: if (flags & CF_CONST) { g_getimmed (flags, val, 0); } - if (offs == 0) { - AddCodeLine ("jsr lsubeq0sp"); - } else { - ldyconst (offs); - AddCodeLine ("jsr lsubeqysp"); - } + ldyconst (offs); + AddCodeLine ("jsr lsubeqysp"); break; default: @@ -2024,7 +1956,7 @@ void g_subeqind (unsigned flags, unsigned offs, unsigned long val) -void g_addaddr_local (unsigned flags, int offs) +void g_addaddr_local (unsigned flags attribute ((unused)), int offs) /* Add the address of a local variable to ax */ { unsigned L = 0; @@ -2268,36 +2200,20 @@ void g_test (unsigned flags) void g_push (unsigned flags, unsigned long val) /* Push the primary register or a constant value onto the stack */ { - unsigned char hi; - if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) { /* We have a constant 8 or 16 bit value */ if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) { /* Handle as 8 bit value */ - if (CodeSizeFactor >= 165 || val > 2) { - ldaconst (val); - AddCodeLine ("jsr pusha"); - } else { - AddCodeLine ("jsr pushc%d", (int) val); - } + ldaconst (val); + AddCodeLine ("jsr pusha"); } else { /* Handle as 16 bit value */ - hi = (unsigned char) (val >> 8); - if (val <= 7) { - AddCodeLine ("jsr push%u", (unsigned) val); - } else if (hi == 0 || hi == 0xFF) { - /* Use special function */ - ldaconst (val); - AddCodeLine ("jsr %s", (hi == 0)? "pusha0" : "pushaFF"); - } else { - /* Long way ... */ - g_getimmed (flags, val, 0); - AddCodeLine ("jsr pushax"); - } + g_getimmed (flags, val, 0); + AddCodeLine ("jsr pushax"); } } else { @@ -2376,15 +2292,33 @@ void g_call (unsigned Flags, const char* Label, unsigned ArgSize) -void g_callind (unsigned Flags, unsigned ArgSize) -/* Call subroutine with address in AX */ +void g_callind (unsigned Flags, unsigned ArgSize, int Offs) +/* Call subroutine indirect */ { - if ((Flags & CF_FIXARGC) == 0) { - /* Pass arg count */ - ldyconst (ArgSize); + if ((Flags & CF_LOCAL) == 0) { + /* Address is in a/x */ + if ((Flags & CF_FIXARGC) == 0) { + /* Pass arg count */ + ldyconst (ArgSize); + } + AddCodeLine ("jsr callax"); + } else { + /* The address is on stack, offset is on Val */ + Offs -= oursp; + CheckLocalOffs (Offs); + AddCodeLine ("pha"); + AddCodeLine ("ldy #$%02X", Offs); + AddCodeLine ("lda (sp),y"); + AddCodeLine ("sta jmpvec+1"); + AddCodeLine ("iny"); + AddCodeLine ("lda (sp),y"); + AddCodeLine ("sta jmpvec+2"); + AddCodeLine ("pla"); + AddCodeLine ("jsr jmpvec"); } - AddCodeLine ("jsr callax"); /* do the call */ - oursp += ArgSize; /* callee pops args */ + + /* Callee pops args */ + oursp += ArgSize; } @@ -2397,54 +2331,7 @@ void g_jump (unsigned Label) -void g_switch (unsigned Flags) -/* Output switch statement preamble */ -{ - switch (Flags & CF_TYPE) { - - case CF_CHAR: - case CF_INT: - AddCodeLine ("jsr switch"); - break; - - case CF_LONG: - AddCodeLine ("jsr lswitch"); - break; - - default: - typeerror (Flags); - - } -} - - - -void g_case (unsigned flags, unsigned label, unsigned long val) -/* Create table code for one case selector */ -{ - switch (flags & CF_TYPE) { - - case CF_CHAR: - case CF_INT: - AddCodeLine (".word $%04X, %s", - (unsigned)(val & 0xFFFF), - LocalLabelName (label)); - break; - - case CF_LONG: - AddCodeLine (".dword $%08lX", val); - AddCodeLine (".word %s", LocalLabelName (label)); - break; - - default: - typeerror (flags); - - } -} - - - -void g_truejump (unsigned flags, unsigned label) +void g_truejump (unsigned flags attribute ((unused)), unsigned label) /* Jump to label if zero flag clear */ { AddCodeLine ("jne %s", LocalLabelName (label)); @@ -2452,7 +2339,7 @@ void g_truejump (unsigned flags, unsigned label) -void g_falsejump (unsigned flags, unsigned label) +void g_falsejump (unsigned flags attribute ((unused)), unsigned label) /* Jump to label if zero flag set */ { AddCodeLine ("jeq %s", LocalLabelName (label)); @@ -2569,7 +2456,7 @@ void g_mul (unsigned flags, unsigned long val) if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) { /* Generate a shift instead */ g_asl (flags, p2); - return; + return; } /* If the right hand side is const, the lhs is not on stack but still @@ -2581,50 +2468,61 @@ void g_mul (unsigned flags, unsigned long val) case CF_CHAR: if (flags & CF_FORCECHAR) { - /* Handle some special cases */ - switch (val) { - - case 3: - AddCodeLine ("sta tmp1"); - AddCodeLine ("asl a"); - AddCodeLine ("clc"); - AddCodeLine ("adc tmp1"); - return; - - case 5: - AddCodeLine ("sta tmp1"); - AddCodeLine ("asl a"); - AddCodeLine ("asl a"); - AddCodeLine ("clc"); + /* Handle some special cases */ + switch (val) { + + case 3: + AddCodeLine ("sta tmp1"); + AddCodeLine ("asl a"); + AddCodeLine ("clc"); AddCodeLine ("adc tmp1"); - return; - - case 10: - AddCodeLine ("sta tmp1"); - AddCodeLine ("asl a"); - AddCodeLine ("asl a"); - AddCodeLine ("clc"); - AddCodeLine ("adc tmp1"); - AddCodeLine ("asl a"); - return; - } + return; + + case 5: + AddCodeLine ("sta tmp1"); + AddCodeLine ("asl a"); + AddCodeLine ("asl a"); + AddCodeLine ("clc"); + AddCodeLine ("adc tmp1"); + return; + + case 10: + AddCodeLine ("sta tmp1"); + AddCodeLine ("asl a"); + AddCodeLine ("asl a"); + AddCodeLine ("clc"); + AddCodeLine ("adc tmp1"); + AddCodeLine ("asl a"); + return; + } } /* FALLTHROUGH */ - case CF_INT: - break; + case CF_INT: + switch (val) { + case 3: + AddCodeLine ("jsr mulax3"); + return; + case 5: + AddCodeLine ("jsr mulax5"); + return; + case 10: + AddCodeLine ("jsr mulax10"); + return; + } + break; - case CF_LONG: - break; + case CF_LONG: + break; - default: - typeerror (flags); - } + default: + typeerror (flags); + } - /* If we go here, we didn't emit code. Push the lhs on stack and fall - * into the normal, non-optimized stuff. - */ - flags &= ~CF_FORCECHAR; /* Handle chars as ints */ + /* If we go here, we didn't emit code. Push the lhs on stack and fall + * into the normal, non-optimized stuff. + */ + flags &= ~CF_FORCECHAR; /* Handle chars as ints */ g_push (flags & ~CF_CONST, 0); } @@ -2856,14 +2754,14 @@ void g_and (unsigned flags, unsigned long val) AddCodeLine ("and #$%02X", (unsigned char)(val >> 8)); AddCodeLine ("tax"); ldaconst (0); - } else { + } else { AddCodeLine ("tay"); AddCodeLine ("txa"); AddCodeLine ("and #$%02X", (unsigned char)(val >> 8)); AddCodeLine ("tax"); AddCodeLine ("tya"); if ((val & 0x00FF) != 0x00FF) { - AddCodeLine ("and #$%02X", (unsigned char)val); + AddCodeLine ("and #$%02X", (unsigned char)val); } } } @@ -2907,10 +2805,10 @@ void g_asr (unsigned flags, unsigned long val) /* Primary = TOS >> Primary */ { static char* ops [12] = { - 0, "tosasra0", "tosasrax", - 0, "tosshra0", "tosshrax", - 0, 0, "tosasreax", - 0, 0, "tosshreax", + 0, "tosasra0", "tosasrax", + 0, "tosshra0", "tosshrax", + 0, 0, "tosasreax", + 0, 0, "tosshreax", }; /* If the right hand side is const, the lhs is not on stack but still @@ -2922,60 +2820,67 @@ void g_asr (unsigned flags, unsigned long val) case CF_CHAR: case CF_INT: - if (val >= 1 && val <= 4) { - if (flags & CF_UNSIGNED) { - AddCodeLine ("jsr shrax%ld", val); - } else { - AddCodeLine ("jsr asrax%ld", val); - } - return; - } else if (val == 8 && (flags & CF_UNSIGNED)) { + if (val >= 8 && (flags & CF_UNSIGNED)) { AddCodeLine ("txa"); ldxconst (0); + val -= 8; + } + if (val == 0) { + /* Done */ return; - } - break; + } else if (val >= 1 && val <= 4) { + if (flags & CF_UNSIGNED) { + AddCodeLine ("jsr shrax%ld", val); + } else { + AddCodeLine ("jsr asrax%ld", val); + } + return; + } + break; - case CF_LONG: - if (val >= 1 && val <= 4) { - if (flags & CF_UNSIGNED) { - AddCodeLine ("jsr shreax%ld", val); - } else { - AddCodeLine ("jsr asreax%ld", val); - } + case CF_LONG: + if (val == 0) { + /* Nothing to do */ return; - } else if (val == 8 && (flags & CF_UNSIGNED)) { - AddCodeLine ("txa"); - AddCodeLine ("ldx sreg"); - AddCodeLine ("ldy sreg+1"); - AddCodeLine ("sty sreg"); - AddCodeLine ("ldy #$00"); + } else if (val >= 1 && val <= 4) { + if (flags & CF_UNSIGNED) { + AddCodeLine ("jsr shreax%ld", val); + } else { + AddCodeLine ("jsr asreax%ld", val); + } + return; + } else if (val == 8 && (flags & CF_UNSIGNED)) { + AddCodeLine ("txa"); + AddCodeLine ("ldx sreg"); + AddCodeLine ("ldy sreg+1"); + AddCodeLine ("sty sreg"); + AddCodeLine ("ldy #$00"); AddCodeLine ("sty sreg+1"); - return; + return; } else if (val == 16) { - AddCodeLine ("ldy #$00"); - AddCodeLine ("ldx sreg+1"); - if ((flags & CF_UNSIGNED) == 0) { - unsigned L = GetLocalLabel(); - AddCodeLine ("bpl %s", LocalLabelName (L)); - AddCodeLine ("dey"); - g_defcodelabel (L); - } + AddCodeLine ("ldy #$00"); + AddCodeLine ("ldx sreg+1"); + if ((flags & CF_UNSIGNED) == 0) { + unsigned L = GetLocalLabel(); + AddCodeLine ("bpl %s", LocalLabelName (L)); + AddCodeLine ("dey"); + g_defcodelabel (L); + } AddCodeLine ("lda sreg"); - AddCodeLine ("sty sreg+1"); - AddCodeLine ("sty sreg"); - return; - } - break; + AddCodeLine ("sty sreg+1"); + AddCodeLine ("sty sreg"); + return; + } + break; - default: - typeerror (flags); - } + default: + typeerror (flags); + } - /* If we go here, we didn't emit code. Push the lhs on stack and fall + /* If we go here, we didn't emit code. Push the lhs on stack and fall * into the normal, non-optimized stuff. - */ - g_push (flags & ~CF_CONST, 0); + */ + g_push (flags & ~CF_CONST, 0); } @@ -2989,10 +2894,10 @@ void g_asl (unsigned flags, unsigned long val) /* Primary = TOS << Primary */ { static char* ops [12] = { - 0, "tosasla0", "tosaslax", - 0, "tosshla0", "tosshlax", - 0, 0, "tosasleax", - 0, 0, "tosshleax", + 0, "tosasla0", "tosaslax", + 0, "tosshla0", "tosshlax", + 0, 0, "tosasleax", + 0, 0, "tosshleax", }; @@ -3005,22 +2910,29 @@ void g_asl (unsigned flags, unsigned long val) case CF_CHAR: case CF_INT: - if (val >= 1 && val <= 4) { + if (val >= 8) { + AddCodeLine ("tax"); + AddCodeLine ("lda #$00"); + val -= 8; + } + if (val == 0) { + /* Done */ + return; + } else if (val >= 1 && val <= 4) { if (flags & CF_UNSIGNED) { AddCodeLine ("jsr shlax%ld", val); } else { - AddCodeLine ("jsr aslax%ld", val); + AddCodeLine ("jsr aslax%ld", val); } return; - } else if (val == 8) { - AddCodeLine ("tax"); - AddCodeLine ("lda #$00"); - return; - } + } break; case CF_LONG: - if (val >= 1 && val <= 4) { + if (val == 0) { + /* Nothing to do */ + return; + } else if (val >= 1 && val <= 4) { if (flags & CF_UNSIGNED) { AddCodeLine ("jsr shleax%ld", val); } else { @@ -3028,7 +2940,7 @@ void g_asl (unsigned flags, unsigned long val) } return; } else if (val == 8) { - AddCodeLine ("ldy sreg"); + AddCodeLine ("ldy sreg"); AddCodeLine ("sty sreg+1"); AddCodeLine ("stx sreg"); AddCodeLine ("tax"); @@ -3071,7 +2983,7 @@ void g_neg (unsigned flags) break; case CF_LONG: - AddCodeLine ("jsr negeax"); + AddCodeLine ("jsr negeax"); break; default: @@ -3142,7 +3054,7 @@ void g_inc (unsigned flags, unsigned long val) if (flags & CF_FORCECHAR) { if (CPU == CPU_65C02 && val <= 2) { while (val--) { - AddCodeLine ("ina"); + AddCodeLine ("ina"); } } else { AddCodeLine ("clc"); @@ -3171,14 +3083,14 @@ void g_inc (unsigned flags, unsigned long val) } } else { /* Inline the code */ - if (val < 0x300) { + if (val <= 0x300) { if ((val & 0xFF) != 0) { - unsigned L = GetLocalLabel(); + unsigned L = GetLocalLabel(); AddCodeLine ("clc"); AddCodeLine ("adc #$%02X", (unsigned char) val); AddCodeLine ("bcc %s", LocalLabelName (L)); AddCodeLine ("inx"); - g_defcodelabel (L); + g_defcodelabel (L); } if (val >= 0x100) { AddCodeLine ("inx"); @@ -3186,6 +3098,9 @@ void g_inc (unsigned flags, unsigned long val) if (val >= 0x200) { AddCodeLine ("inx"); } + if (val >= 0x300) { + AddCodeLine ("inx"); + } } else { AddCodeLine ("clc"); if ((val & 0xFF) != 0) { @@ -3820,7 +3735,7 @@ void g_defdata (unsigned flags, unsigned long val, unsigned offs) const char* Label = GetLabelName (flags, val, offs); /* Labels are always 16 bit */ - AddDataLine ("\t.word\t%s", Label); + AddDataLine ("\t.addr\t%s", Label); } } @@ -3872,27 +3787,99 @@ void g_zerobytes (unsigned n) /*****************************************************************************/ -/* User supplied assembler code */ +/* Switch statement */ /*****************************************************************************/ -void g_asmcode (const char* Line, int Len) -/* Output one line of assembler code. If Len is greater than zero, it is used - * as the maximum number of characters to use from Line. - */ +void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth) +/* Generate code for a switch statement */ { - if (Len >= 0) { - AddCodeLine ("%.*s", Len, Line); - } else { - AddCodeLine ("%s", Line); + unsigned NextLabel = 0; + unsigned I; + + /* Setup registers and determine which compare insn to use */ + const char* Compare; + switch (Depth) { + case 1: + Compare = "cmp #$%02X"; + break; + case 2: + Compare = "cpx #$%02X"; + break; + case 3: + AddCodeLine ("ldy sreg"); + Compare = "cpy #$%02X"; + break; + case 4: + AddCodeLine ("ldy sreg+1"); + Compare = "cpy #$%02X"; + break; + default: + Internal ("Invalid depth in g_switch: %u", Depth); } + + /* Walk over all nodes */ + for (I = 0; I < CollCount (Nodes); ++I) { + + /* Get the next case node */ + CaseNode* N = CollAtUnchecked (Nodes, I); + + /* If we have a next label, define it */ + if (NextLabel) { + g_defcodelabel (NextLabel); + NextLabel = 0; + } + + /* Do the compare */ + AddCodeLine (Compare, CN_GetValue (N)); + + /* If this is the last level, jump directly to the case code if found */ + if (Depth == 1) { + + /* Branch if equal */ + g_falsejump (0, CN_GetLabel (N)); + + } else { + + /* Determine the next label */ + if (I == CollCount (Nodes) - 1) { + /* Last node means not found */ + g_truejump (0, DefaultLabel); + } else { + /* Jump to the next check */ + NextLabel = GetLocalLabel (); + g_truejump (0, NextLabel); + } + + /* Check the next level */ + g_switch (N->Nodes, DefaultLabel, Depth-1); + + } + } + + /* If we go here, we haven't found the label */ + g_jump (DefaultLabel); +} + + + +/*****************************************************************************/ +/* User supplied assembler code */ +/*****************************************************************************/ + + + +void g_asmcode (struct StrBuf* B) +/* Output one line of assembler code. */ +{ + AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B)); } /*****************************************************************************/ -/* Inlined known functions */ +/* Inlined known functions */ /*****************************************************************************/