/* */
/* */
/* */
-/* (C) 1998-2004 Ullrich von Bassewitz */
-/* Römerstraße 52 */
-/* D-70794 Filderstadt */
-/* EMail: uz@cc65.org */
+/* (C) 1998-2009, Ullrich von Bassewitz */
+/* Roemerstrasse 52 */
+/* D-70794 Filderstadt */
+/* EMail: uz@cc65.org */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
#include "check.h"
#include "cpu.h"
#include "strbuf.h"
-#include "version.h"
#include "xmalloc.h"
#include "xsprintf.h"
+#include "version.h"
/* cc65 */
#include "asmcode.h"
static void typeerror (unsigned type)
/* Print an error message about an invalid operand type */
{
- Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
+ /* Special handling for floats here: */
+ if ((type & CF_TYPE) == CF_FLOAT) {
+ Fatal ("Floating point type is currently unsupported");
+ } else {
+ Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
+ }
}
void g_preamble (void)
/* Generate the assembler code preamble */
{
- /* Create a new (global) segment list and remember it */
- PushSegments (0);
- GS = CS;
-
/* Identify the compiler version */
AddTextLine (";");
- AddTextLine ("; File generated by cc65 v %u.%u.%u",
- VER_MAJOR, VER_MINOR, VER_PATCH);
+ AddTextLine ("; File generated by cc65 v %s", GetVersionAsString ());
AddTextLine (";");
/* Insert some object file options */
- AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
- VER_MAJOR, VER_MINOR, VER_PATCH);
+ AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %s\"",
+ GetVersionAsString ());
/* If we're producing code for some other CPU, switch the command set */
switch (CPU) {
+void g_aliasdatalabel (unsigned label, unsigned baselabel, long offs)
+/* Define label as a local alias for baselabel+offs */
+{
+ /* We need an intermediate buffer here since LocalLabelName uses a
+ * static buffer which changes with each call.
+ */
+ StrBuf L = AUTO_STRBUF_INITIALIZER;
+ SB_AppendStr (&L, LocalLabelName (label));
+ SB_Terminate (&L);
+ AddDataLine ("%s\t:=\t%s+%ld",
+ SB_GetConstBuf (&L),
+ LocalLabelName (baselabel),
+ offs);
+ SB_Done (&L);
+}
+
+
+
/*****************************************************************************/
/* Functions handling global labels */
/*****************************************************************************/
void g_importstartup (void)
-/* Forced import of the startup segment */
-{
-#if 0
- AddTextLine ("\t.forceimport\t__STARTUP_RUN__");
-#endif
+/* Forced import of the startup module */
+{
+ AddTextLine ("\t.forceimport\t__STARTUP__");
}
-/*****************************************************************************/
-/* Load functions for various registers */
-/*****************************************************************************/
-
-
-
-static void ldaconst (unsigned val)
-/* Load a with a constant */
-{
- AddCodeLine ("lda #$%02X", val & 0xFF);
-}
-
-
-
-static void ldxconst (unsigned val)
-/* Load x with a constant */
-{
- AddCodeLine ("ldx #$%02X", val & 0xFF);
-}
-
-
-
-static void ldyconst (unsigned val)
-/* Load y with a constant */
-{
- AddCodeLine ("ldy #$%02X", val & 0xFF);
-}
-
-
-
/*****************************************************************************/
/* Function entry and exit */
/*****************************************************************************/
/* Function epilogue */
{
/* How many bytes of locals do we have to drop? */
- unsigned k = (unsigned) -StackPtr;
+ unsigned ToDrop = (unsigned) -StackPtr;
/* If we didn't have a variable argument list, don't call leave */
if (funcargs >= 0) {
- /* Drop stackframe if needed. We can only drop 255 bytes at a time. */
- k += funcargs;
- while (k > 0) {
- unsigned ToDrop = (k > 255)? 255 : k;
- if (ToDrop <= 8) {
- AddCodeLine ("jsr incsp%d", k);
- } else {
- ldyconst (ToDrop);
- AddCodeLine ("jsr addysp");
- }
- k -= ToDrop;
- }
+ /* Drop stackframe if needed */
+ g_drop (ToDrop + funcargs);
- } else {
+ } else if (StackPtr != 0) {
- if (k == 0) {
- /* Nothing to drop */
+ /* We've a stack frame to drop */
+ if (ToDrop > 255) {
+ g_drop (ToDrop); /* Inlines the code */
AddCodeLine ("jsr leave");
} else {
- /* We've a stack frame to drop */
- while (k > 255) {
- ldyconst (255);
- AddCodeLine ("jsr addysp");
- k -= 255;
- }
- ldyconst (k);
+ AddCodeLine ("ldy #$%02X", ToDrop);
AddCodeLine ("jsr leavey");
}
+
+ } else {
+
+ /* Nothing to drop */
+ AddCodeLine ("jsr leave");
+
}
/* Add the final rts */
CheckLocalOffs (StackOffs);
/* Generate code */
+ AddCodeLine ("ldy #$%02X", StackOffs & 0xFF);
if (Bytes == 1) {
if (IS_Get (&CodeSizeFactor) < 165) {
- ldyconst (StackOffs);
- ldxconst (RegOffs);
+ AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
AddCodeLine ("jsr regswap1");
} else {
- ldyconst (StackOffs);
AddCodeLine ("lda (sp),y");
AddCodeLine ("ldx regbank%+d", RegOffs);
AddCodeLine ("sta regbank%+d", RegOffs);
} else if (Bytes == 2) {
- ldyconst (StackOffs);
- ldxconst (RegOffs);
+ AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
AddCodeLine ("jsr regswap2");
} else {
- ldyconst (StackOffs);
- ldxconst (RegOffs);
- ldaconst (Bytes);
+ AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
+ AddCodeLine ("lda #$%02X", Bytes & 0xFF);
AddCodeLine ("jsr regswap");
}
}
/* More than two bytes - loop */
unsigned Label = GetLocalLabel ();
g_space (Bytes);
- ldyconst (Bytes - 1);
- ldxconst (Bytes);
+ AddCodeLine ("ldy #$%02X", (unsigned char) (Bytes - 1));
+ AddCodeLine ("ldx #$%02X", (unsigned char) Bytes);
g_defcodelabel (Label);
AddCodeLine ("lda regbank%+d,x", RegOffs-1);
AddCodeLine ("sta (sp),y");
/* Don't loop for up to two bytes */
if (Bytes == 1) {
- ldyconst (StackOffs);
+ AddCodeLine ("ldy #$%02X", StackOffs);
AddCodeLine ("lda (sp),y");
AddCodeLine ("sta regbank%+d", RegOffs);
} else if (Bytes == 2) {
- ldyconst (StackOffs);
+ AddCodeLine ("ldy #$%02X", StackOffs);
AddCodeLine ("lda (sp),y");
AddCodeLine ("sta regbank%+d", RegOffs);
AddCodeLine ("iny");
} else if (Bytes == 3 && IS_Get (&CodeSizeFactor) >= 133) {
- ldyconst (StackOffs);
+ AddCodeLine ("ldy #$%02X", StackOffs);
AddCodeLine ("lda (sp),y");
AddCodeLine ("sta regbank%+d", RegOffs);
AddCodeLine ("iny");
* code that uses just one index register.
*/
unsigned Label = GetLocalLabel ();
- ldyconst (StackOffs);
+ AddCodeLine ("ldy #$%02X", StackOffs);
g_defcodelabel (Label);
AddCodeLine ("lda (sp),y");
AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs);
*/
unsigned Label = GetLocalLabel ();
AddCodeLine ("stx tmp1");
- ldyconst (StackOffs + Bytes - 1);
- ldxconst (Bytes - 1);
+ AddCodeLine ("ldy #$%02X", (unsigned char) (StackOffs + Bytes - 1));
+ AddCodeLine ("ldx #$%02X", (unsigned char) (Bytes - 1));
g_defcodelabel (Label);
AddCodeLine ("lda (sp),y");
AddCodeLine ("sta regbank%+d,x", RegOffs);
case CF_CHAR:
if ((Flags & CF_FORCECHAR) != 0) {
- ldaconst (Val);
+ AddCodeLine ("lda #$%02X", (unsigned char) Val);
break;
}
/* FALL THROUGH */
case CF_INT:
- ldxconst ((Val >> 8) & 0xFF);
- ldaconst (Val & 0xFF);
+ AddCodeLine ("ldx #$%02X", (unsigned char) (Val >> 8));
+ AddCodeLine ("lda #$%02X", (unsigned char) Val);
break;
case CF_LONG:
if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
AddCodeLine ("lda %s", lbuf); /* load A from the label */
} else {
- ldxconst (0);
+ AddCodeLine ("ldx #$00");
AddCodeLine ("lda %s", lbuf); /* load A from the label */
if (!(flags & CF_UNSIGNED)) {
/* Must sign extend */
-void g_getlocal (unsigned flags, int offs)
+void g_getlocal (unsigned Flags, int Offs)
/* Fetch specified local object (local var). */
{
- offs -= StackPtr;
- CheckLocalOffs (offs);
- switch (flags & CF_TYPE) {
+ Offs -= StackPtr;
+ switch (Flags & CF_TYPE) {
case CF_CHAR:
- if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
- ldyconst (offs);
+ CheckLocalOffs (Offs);
+ if ((Flags & CF_FORCECHAR) || (Flags & CF_TEST)) {
+ AddCodeLine ("ldy #$%02X", Offs);
AddCodeLine ("lda (sp),y");
} else {
- ldyconst (offs);
+ AddCodeLine ("ldy #$%02X", Offs);
AddCodeLine ("ldx #$00");
AddCodeLine ("lda (sp),y");
- if ((flags & CF_UNSIGNED) == 0) {
+ if ((Flags & CF_UNSIGNED) == 0) {
unsigned L = GetLocalLabel();
AddCodeLine ("bpl %s", LocalLabelName (L));
AddCodeLine ("dex");
break;
case CF_INT:
- CheckLocalOffs (offs + 1);
- if (flags & CF_TEST) {
- ldyconst (offs + 1);
+ CheckLocalOffs (Offs + 1);
+ AddCodeLine ("ldy #$%02X", (unsigned char) (Offs+1));
+ if (Flags & CF_TEST) {
AddCodeLine ("lda (sp),y");
AddCodeLine ("dey");
AddCodeLine ("ora (sp),y");
} else {
- ldyconst (offs+1);
AddCodeLine ("jsr ldaxysp");
}
break;
case CF_LONG:
- ldyconst (offs+3);
+ CheckLocalOffs (Offs + 3);
+ AddCodeLine ("ldy #$%02X", (unsigned char) (Offs+3));
AddCodeLine ("jsr ldeaxysp");
- if (flags & CF_TEST) {
- g_test (flags);
+ if (Flags & CF_TEST) {
+ g_test (Flags);
}
break;
default:
- typeerror (flags);
+ typeerror (Flags);
}
}
-void g_getind (unsigned flags, unsigned offs)
+void g_getind (unsigned Flags, unsigned Offs)
/* Fetch the specified object type indirect through the primary register
* into the primary register
*/
* the primary. This way we get an easy addition and use the low byte
* as the offset
*/
- offs = MakeByteOffs (flags, offs);
+ Offs = MakeByteOffs (Flags, Offs);
/* Handle the indirect fetch */
- switch (flags & CF_TYPE) {
+ switch (Flags & CF_TYPE) {
case CF_CHAR:
/* Character sized */
- if (flags & CF_UNSIGNED) {
- ldyconst (offs);
+ AddCodeLine ("ldy #$%02X", Offs);
+ if (Flags & CF_UNSIGNED) {
AddCodeLine ("jsr ldauidx");
} else {
- ldyconst (offs);
AddCodeLine ("jsr ldaidx");
}
break;
case CF_INT:
- if (flags & CF_TEST) {
- ldyconst (offs);
+ if (Flags & CF_TEST) {
+ AddCodeLine ("ldy #$%02X", Offs);
AddCodeLine ("sta ptr1");
AddCodeLine ("stx ptr1+1");
AddCodeLine ("lda (ptr1),y");
AddCodeLine ("iny");
AddCodeLine ("ora (ptr1),y");
} else {
- ldyconst (offs+1);
+ AddCodeLine ("ldy #$%02X", Offs+1);
AddCodeLine ("jsr ldaxidx");
}
break;
case CF_LONG:
- ldyconst (offs+3);
+ AddCodeLine ("ldy #$%02X", Offs+3);
AddCodeLine ("jsr ldeaxidx");
- if (flags & CF_TEST) {
- g_test (flags);
+ if (Flags & CF_TEST) {
+ g_test (Flags);
}
break;
default:
- typeerror (flags);
+ typeerror (Flags);
}
}
-void g_leasp (int offs)
+void g_leasp (int Offs)
/* Fetch the address of the specified symbol into the primary register */
{
+ unsigned char Lo, Hi;
+
/* Calculate the offset relative to sp */
- offs -= StackPtr;
+ Offs -= StackPtr;
- /* For value 0 we do direct code */
- if (offs == 0) {
- AddCodeLine ("lda sp");
- AddCodeLine ("ldx sp+1");
- } else {
- if (IS_Get (&CodeSizeFactor) < 300) {
- ldaconst (offs); /* Load A with offset value */
- AddCodeLine ("jsr leaasp"); /* Load effective address */
+ /* Get low and high byte */
+ Lo = (unsigned char) Offs;
+ Hi = (unsigned char) (Offs >> 8);
+
+ /* Generate code */
+ if (Lo == 0) {
+ if (Hi <= 3) {
+ AddCodeLine ("lda sp");
+ AddCodeLine ("ldx sp+1");
+ while (Hi--) {
+ AddCodeLine ("inx");
+ }
} else {
+ AddCodeLine ("lda sp+1");
+ AddCodeLine ("clc");
+ AddCodeLine ("adc #$%02X", Hi);
+ AddCodeLine ("tax");
+ AddCodeLine ("lda sp");
+ }
+ } else if (Hi == 0) {
+ /* 8 bit offset */
+ if (IS_Get (&CodeSizeFactor) < 200) {
+ /* 8 bit offset with subroutine call */
+ AddCodeLine ("lda #$%02X", Lo);
+ AddCodeLine ("jsr leaa0sp");
+ } else {
+ /* 8 bit offset inlined */
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");
- }
+ AddCodeLine ("lda sp");
+ AddCodeLine ("ldx sp+1");
+ AddCodeLine ("clc");
+ AddCodeLine ("adc #$%02X", Lo);
+ AddCodeLine ("bcc %s", LocalLabelName (L));
+ AddCodeLine ("inx");
g_defcodelabel (L);
}
+ } else if (IS_Get (&CodeSizeFactor) < 170) {
+ /* Full 16 bit offset with subroutine call */
+ AddCodeLine ("lda #$%02X", Lo);
+ AddCodeLine ("ldx #$%02X", Hi);
+ AddCodeLine ("jsr leaaxsp");
+ } else {
+ /* Full 16 bit offset inlined */
+ AddCodeLine ("lda sp");
+ AddCodeLine ("clc");
+ AddCodeLine ("adc #$%02X", Lo);
+ AddCodeLine ("pha");
+ AddCodeLine ("lda sp+1");
+ AddCodeLine ("adc #$%02X", Hi);
+ AddCodeLine ("tax");
+ AddCodeLine ("pla");
}
}
CheckLocalOffs (ArgSizeOffs);
/* Get the size of all parameters. */
- ldyconst (ArgSizeOffs);
+ AddCodeLine ("ldy #$%02X", ArgSizeOffs);
AddCodeLine ("lda (sp),y");
/* Add the value of the stackpointer */
AddCodeLine ("inx");
g_defcodelabel (L);
} else {
- AddCodeLine ("jsr leaasp");
+ AddCodeLine ("ldx #$00");
+ AddCodeLine ("jsr leaaxsp");
}
/* Add the offset to the primary */
if (Flags & CF_CONST) {
AddCodeLine ("lda #$%02X", (unsigned char) Val);
}
- ldyconst (Offs);
+ AddCodeLine ("ldy #$%02X", Offs);
AddCodeLine ("sta (sp),y");
break;
case CF_INT:
if (Flags & CF_CONST) {
- ldyconst (Offs+1);
+ AddCodeLine ("ldy #$%02X", Offs+1);
AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
AddCodeLine ("sta (sp),y");
if ((Flags & CF_NOKEEP) == 0) {
}
AddCodeLine ("sta (sp),y");
} else {
+ AddCodeLine ("ldy #$%02X", Offs);
if ((Flags & CF_NOKEEP) == 0 || IS_Get (&CodeSizeFactor) < 160) {
- ldyconst (Offs);
AddCodeLine ("jsr staxysp");
} else {
- ldyconst (Offs);
AddCodeLine ("sta (sp),y");
AddCodeLine ("iny");
AddCodeLine ("txa");
if (Flags & CF_CONST) {
g_getimmed (Flags, Val, 0);
}
- ldyconst (Offs);
+ AddCodeLine ("ldy #$%02X", Offs);
AddCodeLine ("jsr steaxysp");
break;
}
/* Check the size and determine operation */
+ AddCodeLine ("ldy #$%02X", Offs);
switch (Flags & CF_TYPE) {
case CF_CHAR:
- ldyconst (Offs);
AddCodeLine ("jsr staspidx");
break;
case CF_INT:
- ldyconst (Offs);
AddCodeLine ("jsr staxspidx");
break;
case CF_LONG:
- ldyconst (Offs);
AddCodeLine ("jsr steaxspidx");
break;
case CF_INT:
if (Flags & CF_UNSIGNED) {
if (IS_Get (&CodeSizeFactor) >= 200) {
- ldyconst (0);
+ AddCodeLine ("ldy #$00");
AddCodeLine ("sty sreg");
AddCodeLine ("sty sreg+1");
} else {
/* 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);
- }
- }
+ if (flags & CF_UNSIGNED) {
+ AddCodeLine ("jsr shlax%d", p2);
+ } else {
+ AddCodeLine ("jsr aslax%d", p2);
+ }
break;
case CF_LONG:
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);
- }
+ 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);
- }
+ AddCodeLine ("jsr asrax%d", p2);
}
break;
-void g_addeqlocal (unsigned flags, int offs, unsigned long val)
+void g_addeqlocal (unsigned flags, int Offs, unsigned long val)
/* Emit += for a local variable */
{
/* Calculate the true offset, check it, load it into Y */
- offs -= StackPtr;
- CheckLocalOffs (offs);
+ Offs -= StackPtr;
+ CheckLocalOffs (Offs);
/* Check the size and determine operation */
switch (flags & CF_TYPE) {
case CF_CHAR:
if (flags & CF_FORCECHAR) {
- ldyconst (offs);
+ AddCodeLine ("ldy #$%02X", Offs);
AddCodeLine ("ldx #$00");
if (flags & CF_CONST) {
AddCodeLine ("clc");
/* FALLTHROUGH */
case CF_INT:
- ldyconst (offs);
+ AddCodeLine ("ldy #$%02X", Offs);
if (flags & CF_CONST) {
if (IS_Get (&CodeSizeFactor) >= 400) {
AddCodeLine ("clc");
if (flags & CF_CONST) {
g_getimmed (flags, val, 0);
}
- ldyconst (offs);
+ AddCodeLine ("ldy #$%02X", Offs);
AddCodeLine ("jsr laddeqysp");
break;
AddCodeLine ("dec %s", lbuf);
AddCodeLine ("lda %s", lbuf);
} else {
- AddCodeLine ("sec");
AddCodeLine ("lda %s", lbuf);
+ AddCodeLine ("sec");
AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
AddCodeLine ("sta %s", lbuf);
}
/* FALLTHROUGH */
case CF_INT:
- AddCodeLine ("sec");
if (flags & CF_CONST) {
AddCodeLine ("lda %s", lbuf);
+ AddCodeLine ("sec");
AddCodeLine ("sbc #$%02X", (unsigned char)val);
AddCodeLine ("sta %s", lbuf);
if (val < 0x100) {
}
} else {
AddCodeLine ("eor #$FF");
+ AddCodeLine ("sec");
AddCodeLine ("adc %s", lbuf);
AddCodeLine ("sta %s", lbuf);
AddCodeLine ("txa");
-void g_subeqlocal (unsigned flags, int offs, unsigned long val)
+void g_subeqlocal (unsigned flags, int Offs, unsigned long val)
/* Emit -= for a local variable */
{
/* Calculate the true offset, check it, load it into Y */
- offs -= StackPtr;
- CheckLocalOffs (offs);
+ Offs -= StackPtr;
+ CheckLocalOffs (Offs);
/* Check the size and determine operation */
switch (flags & CF_TYPE) {
case CF_CHAR:
if (flags & CF_FORCECHAR) {
- ldyconst (offs);
+ AddCodeLine ("ldy #$%02X", Offs);
AddCodeLine ("ldx #$00");
- AddCodeLine ("sec");
if (flags & CF_CONST) {
AddCodeLine ("lda (sp),y");
+ AddCodeLine ("sec");
AddCodeLine ("sbc #$%02X", (unsigned char)val);
} else {
AddCodeLine ("eor #$FF");
+ AddCodeLine ("sec");
AddCodeLine ("adc (sp),y");
}
AddCodeLine ("sta (sp),y");
if (flags & CF_CONST) {
g_getimmed (flags, val, 0);
}
- ldyconst (offs);
+ AddCodeLine ("ldy #$%02X", Offs);
AddCodeLine ("jsr subeqysp");
break;
if (flags & CF_CONST) {
g_getimmed (flags, val, 0);
}
- ldyconst (offs);
+ AddCodeLine ("ldy #$%02X", Offs);
AddCodeLine ("jsr lsubeqysp");
break;
-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
- *
- * 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
- * a 8 bit constant, and the third is used in all other cases.
+static void oper (unsigned Flags, unsigned long Val, const char** Subs)
+/* Encode a binary operation. subs is a pointer to four strings:
+ * 0 --> Operate on ints
+ * 1 --> Operate on unsigneds
+ * 2 --> Operate on longs
+ * 3 --> Operate on unsigned longs
*/
{
- unsigned offs;
-
/* 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_LONG:
- offs += 6;
- break;
-
- default:
- typeerror (flags);
+ if (Flags & CF_UNSIGNED) {
+ ++Subs;
+ }
+ if ((Flags & CF_TYPE) == CF_LONG) {
+ Subs += 2;
}
- /* 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]);
- }
- } else {
- /* Value not constant (is already in (e)ax) */
- AddCodeLine ("jsr %s", subs [offs+2]);
+ /* Load the value if it is not already in the primary */
+ if (Flags & CF_CONST) {
+ /* Load value */
+ g_getimmed (Flags, Val, 0);
}
+ /* Output the operation */
+ AddCodeLine ("jsr %s", *Subs);
+
/* The operation will pop it's argument */
- pop (flags);
+ pop (Flags);
}
if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
/* Handle as 8 bit value */
- ldaconst (val);
+ AddCodeLine ("lda #$%02X", (unsigned char) val);
AddCodeLine ("jsr pusha");
} else {
{
if ((Flags & CF_FIXARGC) == 0) {
/* Pass the argument count */
- ldyconst (ArgSize);
+ AddCodeLine ("ldy #$%02X", ArgSize);
}
AddCodeLine ("jsr _%s", Label);
StackPtr += ArgSize; /* callee pops args */
/* Address is in a/x */
if ((Flags & CF_FIXARGC) == 0) {
/* Pass arg count */
- ldyconst (ArgSize);
+ AddCodeLine ("ldy #$%02X", ArgSize);
}
AddCodeLine ("jsr callax");
} else {
-static void mod_internal (int k, char* verb1, char* verb2)
+void g_drop (unsigned Space)
+/* Drop space allocated on the stack */
{
- if (k <= 8) {
- AddCodeLine ("jsr %ssp%c", verb1, k + '0');
- } else {
- CheckLocalOffs (k);
- ldyconst (k);
- AddCodeLine ("jsr %ssp", verb2);
+ if (Space > 255) {
+ /* Inline the code since calling addysp repeatedly is quite some
+ * overhead.
+ */
+ AddCodeLine ("pha");
+ AddCodeLine ("lda #$%02X", (unsigned char) Space);
+ AddCodeLine ("clc");
+ AddCodeLine ("adc sp");
+ AddCodeLine ("sta sp");
+ AddCodeLine ("lda #$%02X", (unsigned char) (Space >> 8));
+ AddCodeLine ("adc sp+1");
+ AddCodeLine ("sta sp+1");
+ AddCodeLine ("pla");
+ } else if (Space > 8) {
+ AddCodeLine ("ldy #$%02X", Space);
+ AddCodeLine ("jsr addysp");
+ } else if (Space != 0) {
+ AddCodeLine ("jsr incsp%u", Space);
}
}
-void g_space (int space)
+void g_space (int Space)
/* Create or drop space on the stack */
{
- if (space < 0) {
- mod_internal (-space, "inc", "addy");
- } else if (space > 0) {
- mod_internal (space, "dec", "suby");
+ if (Space < 0) {
+ /* This is actually a drop operation */
+ g_drop (-Space);
+ } else if (Space > 255) {
+ /* Inline the code since calling subysp repeatedly is quite some
+ * overhead.
+ */
+ AddCodeLine ("pha");
+ AddCodeLine ("lda sp");
+ AddCodeLine ("sec");
+ AddCodeLine ("sbc #$%02X", (unsigned char) Space);
+ AddCodeLine ("sta sp");
+ AddCodeLine ("lda sp+1");
+ AddCodeLine ("sbc #$%02X", (unsigned char) (Space >> 8));
+ AddCodeLine ("sta sp+1");
+ AddCodeLine ("pla");
+ } else if (Space > 8) {
+ AddCodeLine ("ldy #$%02X", Space);
+ AddCodeLine ("jsr subysp");
+ } else if (Space != 0) {
+ AddCodeLine ("jsr decsp%u", Space);
}
}
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",
+ static const char* ops[12] = {
+ "tosaddax", "tosaddax", "tosaddeax", "tosaddeax"
};
if (flags & CF_CONST) {
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",
+ static const char* ops[12] = {
+ "tossubax", "tossubax", "tossubeax", "tossubeax",
};
if (flags & CF_CONST) {
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",
+ static const char* ops[12] = {
+ "tosrsubax", "tosrsubax", "tosrsubeax", "tosrsubeax",
};
oper (flags, val, ops);
}
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",
+ static const char* ops[12] = {
+ "tosmulax", "tosumulax", "tosmuleax", "tosumuleax",
};
int p2;
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",
+ static const char* ops[12] = {
+ "tosdivax", "tosudivax", "tosdiveax", "tosudiveax",
};
/* Do strength reduction if the value is constant and a power of two */
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",
+ static const char* ops[12] = {
+ "tosmodax", "tosumodax", "tosmodeax", "tosumodeax",
};
int p2;
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",
+ static const char* ops[12] = {
+ "tosorax", "tosorax", "tosoreax", "tosoreax",
};
/* If the right hand side is const, the lhs is not on stack but still
if ((val & 0xFF) != 0) {
AddCodeLine ("ora #$%02X", (unsigned char)val);
}
- ldxconst (0xFF);
+ AddCodeLine ("ldx #$FF");
} else if (val != 0) {
AddCodeLine ("ora #$%02X", (unsigned char)val);
AddCodeLine ("pha");
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",
+ static const char* ops[12] = {
+ "tosxorax", "tosxorax", "tosxoreax", "tosxoreax",
};
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",
+ static const char* ops[12] = {
+ "tosandax", "tosandax", "tosandeax", "tosandeax",
};
/* If the right hand side is const, the lhs is not on stack but still
case CF_CHAR:
if (Flags & CF_FORCECHAR) {
- if ((Val & 0xFF) != 0xFF) {
+ if ((Val & 0xFF) == 0x00) {
+ AddCodeLine ("lda #$00");
+ } else if ((Val & 0xFF) != 0xFF) {
AddCodeLine ("and #$%02X", (unsigned char)Val);
}
return;
case CF_INT:
if ((Val & 0xFFFF) != 0xFFFF) {
if (Val <= 0xFF) {
- ldxconst (0);
+ AddCodeLine ("ldx #$00");
if (Val == 0) {
- ldaconst (0);
+ AddCodeLine ("lda #$00");
} else if (Val != 0xFF) {
AddCodeLine ("and #$%02X", (unsigned char)Val);
}
- } else if ((Val & 0xFF00) == 0xFF00) {
+ } else if ((Val & 0xFFFF) == 0xFF00) {
+ AddCodeLine ("lda #$00");
+ } 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);
+ AddCodeLine ("lda #$00");
} else {
AddCodeLine ("tay");
AddCodeLine ("txa");
AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
AddCodeLine ("tax");
AddCodeLine ("tya");
- if ((Val & 0x00FF) != 0x00FF) {
+ if ((Val & 0x00FF) == 0x0000) {
+ AddCodeLine ("lda #$00");
+ } else if ((Val & 0x00FF) != 0x00FF) {
AddCodeLine ("and #$%02X", (unsigned char)Val);
- }
+ }
}
}
return;
case CF_LONG:
if (Val <= 0xFF) {
- ldxconst (0);
+ AddCodeLine ("ldx #$00");
AddCodeLine ("stx sreg+1");
AddCodeLine ("stx sreg");
if ((Val & 0xFF) != 0xFF) {
}
return;
} else if (Val == 0xFF00) {
- ldaconst (0);
+ AddCodeLine ("lda #$00");
AddCodeLine ("sta sreg+1");
AddCodeLine ("sta sreg");
return;
void g_asr (unsigned flags, unsigned long val)
/* Primary = TOS >> Primary */
{
- static char* ops [12] = {
- 0, 0, "tosasrax",
- 0, 0, "tosshrax",
- 0, 0, "tosasreax",
- 0, 0, "tosshreax",
+ static const char* ops[12] = {
+ "tosasrax", "tosshrax", "tosasreax", "tosshreax",
};
/* If the right hand side is const, the lhs is not on stack but still
case CF_CHAR:
case CF_INT:
- if (val >= 8 && (flags & CF_UNSIGNED)) {
- AddCodeLine ("txa");
- ldxconst (0);
+ val &= 0x0F;
+ if (val >= 8) {
+ if (flags & CF_UNSIGNED) {
+ AddCodeLine ("txa");
+ AddCodeLine ("ldx #$00");
+ } else {
+ unsigned L = GetLocalLabel();
+ AddCodeLine ("cpx #$80"); /* Sign bit into carry */
+ AddCodeLine ("txa");
+ AddCodeLine ("ldx #$00");
+ AddCodeLine ("bcc %s", LocalLabelName (L));
+ AddCodeLine ("dex"); /* Make $FF */
+ g_defcodelabel (L);
+ }
val -= 8;
}
- if (val == 0) {
- /* Done */
- return;
- } else if (val >= 1 && val <= 4) {
+ if (val >= 4) {
+ if (flags & CF_UNSIGNED) {
+ AddCodeLine ("jsr shrax4");
+ } else {
+ AddCodeLine ("jsr asrax4");
+ }
+ val -= 4;
+ }
+ if (val > 0) {
if (flags & CF_UNSIGNED) {
AddCodeLine ("jsr shrax%ld", val);
} else {
AddCodeLine ("jsr asrax%ld", val);
}
- return;
}
- break;
+ return;
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);
+ val &= 0x1F;
+ if (val >= 24) {
+ AddCodeLine ("ldx #$00");
+ AddCodeLine ("lda sreg+1");
+ if ((flags & CF_UNSIGNED) == 0) {
+ unsigned L = GetLocalLabel();
+ AddCodeLine ("bpl %s", LocalLabelName (L));
+ AddCodeLine ("dex");
+ g_defcodelabel (L);
}
- 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 ("stx sreg");
+ AddCodeLine ("stx sreg+1");
+ val -= 24;
+ }
+ if (val >= 16) {
AddCodeLine ("ldy #$00");
AddCodeLine ("ldx sreg+1");
if ((flags & CF_UNSIGNED) == 0) {
AddCodeLine ("lda sreg");
AddCodeLine ("sty sreg+1");
AddCodeLine ("sty sreg");
- return;
+ val -= 16;
}
- break;
+ if (val >= 8) {
+ AddCodeLine ("txa");
+ AddCodeLine ("ldx sreg");
+ AddCodeLine ("ldy sreg+1");
+ AddCodeLine ("sty sreg");
+ if ((flags & CF_UNSIGNED) == 0) {
+ unsigned L = GetLocalLabel();
+ AddCodeLine ("cpy #$80");
+ AddCodeLine ("ldy #$00");
+ AddCodeLine ("bcc %s", LocalLabelName (L));
+ AddCodeLine ("dey");
+ g_defcodelabel (L);
+ } else {
+ AddCodeLine ("ldy #$00");
+ }
+ AddCodeLine ("sty sreg+1");
+ val -= 8;
+ }
+ if (val >= 4) {
+ if (flags & CF_UNSIGNED) {
+ AddCodeLine ("jsr shreax4");
+ } else {
+ AddCodeLine ("jsr asreax4");
+ }
+ val -= 4;
+ }
+ if (val > 0) {
+ if (flags & CF_UNSIGNED) {
+ AddCodeLine ("jsr shreax%ld", val);
+ } else {
+ AddCodeLine ("jsr asreax%ld", val);
+ }
+ }
+ return;
default:
typeerror (flags);
void g_asl (unsigned flags, unsigned long val)
/* Primary = TOS << Primary */
{
- static char* ops [12] = {
- 0, 0, "tosaslax",
- 0, 0, "tosshlax",
- 0, 0, "tosasleax",
- 0, 0, "tosshleax",
+ static const char* ops[12] = {
+ "tosaslax", "tosshlax", "tosasleax", "tosshleax",
};
case CF_CHAR:
case CF_INT:
+ val &= 0x0F;
if (val >= 8) {
AddCodeLine ("tax");
AddCodeLine ("lda #$00");
val -= 8;
}
- if (val == 0) {
- /* Done */
- return;
- } else if (val >= 1 && val <= 4) {
+ if (val >= 4) {
if (flags & CF_UNSIGNED) {
- AddCodeLine ("jsr shlax%ld", val);
+ AddCodeLine ("jsr shlax4");
} else {
- AddCodeLine ("jsr aslax%ld", val);
+ AddCodeLine ("jsr aslax4");
}
- return;
+ val -= 4;
}
- break;
-
- case CF_LONG:
- if (val == 0) {
- /* Nothing to do */
- return;
- } else if (val >= 1 && val <= 4) {
+ if (val > 0) {
if (flags & CF_UNSIGNED) {
- AddCodeLine ("jsr shleax%ld", val);
+ AddCodeLine ("jsr shlax%ld", val);
} else {
- AddCodeLine ("jsr asleax%ld", val);
+ AddCodeLine ("jsr aslax%ld", val);
}
- return;
- } else if (val == 8) {
- AddCodeLine ("ldy sreg");
- AddCodeLine ("sty sreg+1");
- AddCodeLine ("stx sreg");
- AddCodeLine ("tax");
+ }
+ return;
+
+ case CF_LONG:
+ val &= 0x1F;
+ if (val >= 24) {
+ AddCodeLine ("sta sreg+1");
AddCodeLine ("lda #$00");
- return;
- } else if (val == 16) {
+ AddCodeLine ("tax");
+ AddCodeLine ("sta sreg");
+ val -= 24;
+ }
+ if (val >= 16) {
AddCodeLine ("stx sreg+1");
AddCodeLine ("sta sreg");
AddCodeLine ("lda #$00");
AddCodeLine ("tax");
- return;
+ val -= 16;
}
- break;
+ if (val >= 8) {
+ AddCodeLine ("ldy sreg");
+ AddCodeLine ("sty sreg+1");
+ AddCodeLine ("stx sreg");
+ AddCodeLine ("tax");
+ AddCodeLine ("lda #$00");
+ val -= 8;
+ }
+ if (val > 4) {
+ if (flags & CF_UNSIGNED) {
+ AddCodeLine ("jsr shleax4");
+ } else {
+ AddCodeLine ("jsr asleax4");
+ }
+ val -= 4;
+ }
+ if (val > 0) {
+ if (flags & CF_UNSIGNED) {
+ AddCodeLine ("jsr shleax%ld", val);
+ } else {
+ AddCodeLine ("jsr asleax%ld", val);
+ }
+ }
+ return;
default:
typeerror (flags);
if (val <= 8) {
AddCodeLine ("jsr incax%lu", val);
} else if (val <= 255) {
- ldyconst (val);
+ AddCodeLine ("ldy #$%02X", (unsigned char) val);
AddCodeLine ("jsr incaxy");
} else {
g_add (flags | CF_CONST, val);
if (val >= 0x300) {
AddCodeLine ("inx");
}
- } else {
+ } else if ((val & 0xFF) != 0) {
AddCodeLine ("clc");
- if ((val & 0xFF) != 0) {
- AddCodeLine ("adc #$%02X", (unsigned char) val);
- }
+ AddCodeLine ("adc #$%02X", (unsigned char) val);
AddCodeLine ("pha");
AddCodeLine ("txa");
AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
AddCodeLine ("tax");
AddCodeLine ("pla");
+ } else {
+ AddCodeLine ("pha");
+ AddCodeLine ("txa");
+ AddCodeLine ("clc");
+ AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
+ AddCodeLine ("tax");
+ AddCodeLine ("pla");
}
}
break;
case CF_LONG:
if (val <= 255) {
- ldyconst (val);
+ AddCodeLine ("ldy #$%02X", (unsigned char) val);
AddCodeLine ("jsr inceaxy");
} else {
g_add (flags | CF_CONST, val);
if (val <= 8) {
AddCodeLine ("jsr decax%d", (int) val);
} else if (val <= 255) {
- ldyconst (val);
+ AddCodeLine ("ldy #$%02X", (unsigned char) val);
AddCodeLine ("jsr decaxy");
} else {
g_sub (flags | CF_CONST, val);
AddCodeLine ("dex");
}
} else {
- AddCodeLine ("sec");
if ((val & 0xFF) != 0) {
+ AddCodeLine ("sec");
AddCodeLine ("sbc #$%02X", (unsigned char) val);
+ AddCodeLine ("pha");
+ AddCodeLine ("txa");
+ AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
+ AddCodeLine ("tax");
+ AddCodeLine ("pla");
+ } else {
+ AddCodeLine ("pha");
+ AddCodeLine ("txa");
+ AddCodeLine ("sec");
+ AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
+ AddCodeLine ("tax");
+ AddCodeLine ("pla");
}
- 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 ("ldy #$%02X", (unsigned char) val);
AddCodeLine ("jsr deceaxy");
} else {
g_sub (flags | CF_CONST, val);
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",
+ static const char* ops[12] = {
+ "toseqax", "toseqax", "toseqeax", "toseqeax",
};
unsigned L;
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",
+ static const char* ops[12] = {
+ "tosneax", "tosneax", "tosneeax", "tosneeax",
};
unsigned L;
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",
+ static const char* ops[12] = {
+ "tosltax", "tosultax", "toslteax", "tosulteax",
};
+ unsigned Label;
+
/* If the right hand side is const, the lhs is not on stack but still
* in the primary register.
*/
} else if (val == 0) {
- /* Look at the type */
+ /* A signed compare against zero must only look at the sign bit */
switch (flags & CF_TYPE) {
case CF_CHAR:
if (flags & CF_FORCECHAR) {
- AddCodeLine ("tax");
- AddCodeLine ("jsr boollt");
+ AddCodeLine ("asl a"); /* Bit 7 -> carry */
+ AddCodeLine ("lda #$00");
+ AddCodeLine ("ldx #$00");
+ AddCodeLine ("rol a");
return;
}
/* FALLTHROUGH */
case CF_INT:
/* Just check the high byte */
- AddCodeLine ("txa");
- AddCodeLine ("jsr boollt");
+ AddCodeLine ("cpx #$80"); /* Bit 7 -> carry */
+ AddCodeLine ("lda #$00");
+ AddCodeLine ("ldx #$00");
+ AddCodeLine ("rol a");
return;
case CF_LONG:
/* Just check the high byte */
AddCodeLine ("lda sreg+1");
- AddCodeLine ("jsr boollt");
+ AddCodeLine ("asl a"); /* Bit 7 -> carry */
+ AddCodeLine ("lda #$00");
+ AddCodeLine ("ldx #$00");
+ AddCodeLine ("rol a");
+ return;
+
+ default:
+ typeerror (flags);
+ }
+
+ } else {
+
+ /* Signed compare against a constant != zero */
+ switch (flags & CF_TYPE) {
+
+ case CF_CHAR:
+ if (flags & CF_FORCECHAR) {
+ Label = GetLocalLabel ();
+ AddCodeLine ("sec");
+ AddCodeLine ("sbc #$%02X", (unsigned char)val);
+ AddCodeLine ("bvc %s", LocalLabelName (Label));
+ AddCodeLine ("eor #$80");
+ g_defcodelabel (Label);
+ AddCodeLine ("asl a"); /* Bit 7 -> carry */
+ AddCodeLine ("lda #$00");
+ AddCodeLine ("ldx #$00");
+ AddCodeLine ("rol a");
+ return;
+ }
+ /* FALLTHROUGH */
+
+ case CF_INT:
+ /* Do a subtraction */
+ Label = GetLocalLabel ();
+ AddCodeLine ("cmp #$%02X", (unsigned char)val);
+ AddCodeLine ("txa");
+ AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
+ AddCodeLine ("bvc %s", LocalLabelName (Label));
+ AddCodeLine ("eor #$80");
+ g_defcodelabel (Label);
+ AddCodeLine ("asl a"); /* Bit 7 -> carry */
+ AddCodeLine ("lda #$00");
+ AddCodeLine ("ldx #$00");
+ AddCodeLine ("rol a");
return;
+ case CF_LONG:
+ /* This one is too costly */
+ break;
+
default:
typeerror (flags);
}
+
}
/* If we go here, we didn't emit code. Push the lhs on stack and fall
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",
+ static const char* ops[12] = {
+ "tosleax", "tosuleax", "tosleeax", "tosuleeax",
};
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",
+ static const char* ops[12] = {
+ "tosgtax", "tosugtax", "tosgteax", "tosugteax",
};
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",
+ static const char* ops[12] = {
+ "tosgeax", "tosugeax", "tosgeeax", "tosugeeax",
};
+ unsigned Label;
+
/* If the right hand side is const, the lhs is not on stack but still
* in the primary register.
case CF_CHAR:
if (flags & CF_FORCECHAR) {
+ /* Do a subtraction. Condition is true if carry set */
AddCodeLine ("cmp #$%02X", (unsigned char)val);
- AddCodeLine ("jsr booluge");
+ AddCodeLine ("lda #$00");
+ AddCodeLine ("ldx #$00");
+ AddCodeLine ("rol a");
return;
}
/* FALLTHROUGH */
case CF_INT:
- /* If the low byte is zero, we must only test the high byte */
- AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
- if ((val & 0xFF) != 0) {
- unsigned L = GetLocalLabel();
- AddCodeLine ("bne %s", LocalLabelName (L));
- AddCodeLine ("cmp #$%02X", (unsigned char)val);
- g_defcodelabel (L);
- }
- AddCodeLine ("jsr booluge");
+ /* Do a subtraction. Condition is true if carry set */
+ AddCodeLine ("cmp #$%02X", (unsigned char)val);
+ AddCodeLine ("txa");
+ AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
+ AddCodeLine ("lda #$00");
+ AddCodeLine ("ldx #$00");
+ AddCodeLine ("rol a");
return;
case CF_LONG:
- /* Do a subtraction */
+ /* Do a subtraction. Condition is true if carry set */
AddCodeLine ("cmp #$%02X", (unsigned char)val);
AddCodeLine ("txa");
AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
AddCodeLine ("lda sreg+1");
AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
- AddCodeLine ("jsr booluge");
+ AddCodeLine ("lda #$00");
+ AddCodeLine ("ldx #$00");
+ AddCodeLine ("rol a");
return;
default:
} else if (val == 0) {
- /* Look at the type */
+ /* A signed compare against zero must only look at the sign bit */
switch (flags & CF_TYPE) {
case CF_CHAR:
default:
typeerror (flags);
}
- }
+ } else {
+
+ /* Signed compare against a constant != zero */
+ switch (flags & CF_TYPE) {
+
+ case CF_CHAR:
+ if (flags & CF_FORCECHAR) {
+ Label = GetLocalLabel ();
+ AddCodeLine ("sec");
+ AddCodeLine ("sbc #$%02X", (unsigned char)val);
+ AddCodeLine ("bvs %s", LocalLabelName (Label));
+ AddCodeLine ("eor #$80");
+ g_defcodelabel (Label);
+ AddCodeLine ("asl a"); /* Bit 7 -> carry */
+ AddCodeLine ("lda #$00");
+ AddCodeLine ("ldx #$00");
+ AddCodeLine ("rol a");
+ return;
+ }
+ /* FALLTHROUGH */
+
+ case CF_INT:
+ /* Do a subtraction */
+ Label = GetLocalLabel ();
+ AddCodeLine ("cmp #$%02X", (unsigned char)val);
+ AddCodeLine ("txa");
+ AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
+ AddCodeLine ("bvs %s", LocalLabelName (Label));
+ AddCodeLine ("eor #$80");
+ g_defcodelabel (Label);
+ AddCodeLine ("asl a"); /* Bit 7 -> carry */
+ AddCodeLine ("lda #$00");
+ AddCodeLine ("ldx #$00");
+ AddCodeLine ("rol a");
+ return;
+
+ case CF_LONG:
+ /* This one is too costly */
+ 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
} while (Chunk);
/* Output the line */
- AddDataLine (Buf);
+ AddDataLine ("%s", Buf);
}
}
{
/* Register variables do always have less than 128 bytes */
unsigned CodeLabel = GetLocalLabel ();
- ldxconst (Size-1);
+ AddCodeLine ("ldx #$%02X", (unsigned char) (Size - 1));
g_defcodelabel (CodeLabel);
AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
CheckLocalOffs (Size);
if (Size <= 128) {
- ldyconst (Size-1);
+ AddCodeLine ("ldy #$%02X", Size-1);
g_defcodelabel (CodeLabel);
AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
AddCodeLine ("sta (sp),y");
AddCodeLine ("dey");
AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
} else if (Size <= 256) {
- ldyconst (0);
+ AddCodeLine ("ldy #$00");
g_defcodelabel (CodeLabel);
AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
AddCodeLine ("sta (sp),y");
{
if (Size <= 128) {
unsigned CodeLabel = GetLocalLabel ();
- ldyconst (Size-1);
+ AddCodeLine ("ldy #$%02X", Size-1);
g_defcodelabel (CodeLabel);
AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
} else if (Size <= 256) {
unsigned CodeLabel = GetLocalLabel ();
- ldyconst (0);
+ AddCodeLine ("ldy #$00");
g_defcodelabel (CodeLabel);
AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
void g_asmcode (struct StrBuf* B)
/* Output one line of assembler code. */
{
- AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));
+ AddCodeLine ("%.*s", (int) SB_GetLen (B), SB_GetConstBuf (B));
}