1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2009, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
64 /*****************************************************************************/
66 /*****************************************************************************/
70 static void typeerror (unsigned type)
71 /* Print an error message about an invalid operand type */
73 /* Special handling for floats here: */
74 if ((type & CF_TYPE) == CF_FLOAT) {
75 Fatal ("Floating point type is currently unsupported");
77 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
83 static void CheckLocalOffs (unsigned Offs)
84 /* Check the offset into the stack for 8bit range */
87 /* Too many local vars */
88 Error ("Too many local variables");
94 static const char* GetLabelName (unsigned Flags, unsigned long Label, long Offs)
96 static char Buf [256]; /* Label name */
98 /* Create the correct label name */
99 switch (Flags & CF_ADDRMASK) {
102 /* Static memory cell */
104 xsprintf (Buf, sizeof (Buf), "%s%+ld", LocalLabelName (Label), Offs);
106 xsprintf (Buf, sizeof (Buf), "%s", LocalLabelName (Label));
113 xsprintf (Buf, sizeof (Buf), "_%s%+ld", (char*) Label, Offs);
115 xsprintf (Buf, sizeof (Buf), "_%s", (char*) Label);
120 /* Absolute address */
121 xsprintf (Buf, sizeof (Buf), "$%04X", (int)((Label+Offs) & 0xFFFF));
125 /* Variable in register bank */
126 xsprintf (Buf, sizeof (Buf), "regbank+%u", (unsigned)((Label+Offs) & 0xFFFF));
130 Internal ("Invalid address flags: %04X", Flags);
133 /* Return a pointer to the static buffer */
139 /*****************************************************************************/
140 /* Pre- and postamble */
141 /*****************************************************************************/
145 void g_preamble (void)
146 /* Generate the assembler code preamble */
148 /* Create a new (global) segment list and remember it */
152 /* Identify the compiler version */
154 AddTextLine ("; File generated by cc65 v %s", GetVersionAsString ());
157 /* Insert some object file options */
158 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %s\"",
159 GetVersionAsString ());
161 /* If we're producing code for some other CPU, switch the command set */
163 case CPU_6502: AddTextLine ("\t.setcpu\t\t\"6502\""); break;
164 case CPU_6502X: AddTextLine ("\t.setcpu\t\t\"6502X\""); break;
165 case CPU_65SC02: AddTextLine ("\t.setcpu\t\t\"65SC02\""); break;
166 case CPU_65C02: AddTextLine ("\t.setcpu\t\t\"65C02\""); break;
167 case CPU_65816: AddTextLine ("\t.setcpu\t\t\"65816\""); break;
168 case CPU_HUC6280: AddTextLine ("\t.setcpu\t\t\"HUC6280\""); break;
169 default: Internal ("Unknown CPU: %d", CPU);
173 AddTextLine ("\t.smart\t\ton");
175 /* Allow auto import for runtime library routines */
176 AddTextLine ("\t.autoimport\ton");
178 /* Switch the assembler into case sensitive mode */
179 AddTextLine ("\t.case\t\ton");
181 /* Tell the assembler if we want to generate debug info */
182 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
184 /* Import the stack pointer for direct auto variable access */
185 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
187 /* Define long branch macros */
188 AddTextLine ("\t.macpack\tlongbranch");
193 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
194 /* If debug info is enabled, place a file info into the source */
197 /* We have to place this into the global text segment, so it will
198 * appear before all .dbg line statements.
200 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
206 /*****************************************************************************/
207 /* Segment support */
208 /*****************************************************************************/
212 void g_userodata (void)
213 /* Switch to the read only data segment */
215 UseDataSeg (SEG_RODATA);
220 void g_usedata (void)
221 /* Switch to the data segment */
223 UseDataSeg (SEG_DATA);
229 /* Switch to the bss segment */
231 UseDataSeg (SEG_BSS);
236 void g_segname (segment_t Seg)
237 /* Emit the name of a segment if necessary */
239 /* Emit a segment directive for the data style segments */
242 case SEG_RODATA: S = CS->ROData; break;
243 case SEG_DATA: S = CS->Data; break;
244 case SEG_BSS: S = CS->BSS; break;
245 default: S = 0; break;
248 DS_AddLine (S, ".segment\t\"%s\"", GetSegName (Seg));
254 /*****************************************************************************/
256 /*****************************************************************************/
260 unsigned sizeofarg (unsigned flags)
261 /* Return the size of a function argument type that is encoded in flags */
263 switch (flags & CF_TYPE) {
266 return (flags & CF_FORCECHAR)? 1 : 2;
286 int pop (unsigned flags)
287 /* Pop an argument of the given size */
289 return StackPtr += sizeofarg (flags);
294 int push (unsigned flags)
295 /* Push an argument of the given size */
297 return StackPtr -= sizeofarg (flags);
302 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
303 /* The value in Offs is an offset to an address in a/x. Make sure, an object
304 * of the type given in Flags can be loaded or stored into this address by
305 * adding part of the offset to the address in ax, so that the remaining
306 * offset fits into an index register. Return the remaining offset.
309 /* If the offset is too large for a byte register, add the high byte
310 * of the offset to the primary. Beware: We need a special correction
311 * if the offset in the low byte will overflow in the operation.
313 unsigned O = Offs & ~0xFFU;
314 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
315 /* We need to add the low byte also */
319 /* Do the correction if we need one */
321 g_inc (CF_INT | CF_CONST, O);
325 /* Return the new offset */
331 /*****************************************************************************/
332 /* Functions handling local labels */
333 /*****************************************************************************/
337 void g_defcodelabel (unsigned label)
338 /* Define a local code label */
340 CS_AddLabel (CS->Code, LocalLabelName (label));
345 void g_defdatalabel (unsigned label)
346 /* Define a local data label */
348 AddDataLine ("%s:", LocalLabelName (label));
353 /*****************************************************************************/
354 /* Functions handling global labels */
355 /*****************************************************************************/
359 void g_defgloblabel (const char* Name)
360 /* Define a global label with the given name */
362 /* Global labels are always data labels */
363 AddDataLine ("_%s:", Name);
368 void g_defexport (const char* Name, int ZP)
369 /* Export the given label */
372 AddTextLine ("\t.exportzp\t_%s", Name);
374 AddTextLine ("\t.export\t\t_%s", Name);
380 void g_defimport (const char* Name, int ZP)
381 /* Import the given label */
384 AddTextLine ("\t.importzp\t_%s", Name);
386 AddTextLine ("\t.import\t\t_%s", Name);
392 void g_importstartup (void)
393 /* Forced import of the startup module */
395 AddTextLine ("\t.forceimport\t__STARTUP__");
400 void g_importmainargs (void)
401 /* Forced import of a special symbol that handles arguments to main */
403 AddTextLine ("\t.forceimport\tinitmainargs");
408 /*****************************************************************************/
409 /* Function entry and exit */
410 /*****************************************************************************/
414 /* Remember the argument size of a function. The variable is set by g_enter
415 * and used by g_leave. If the functions gets its argument size by the caller
416 * (variable param list or function without prototype), g_enter will set the
422 void g_enter (unsigned flags, unsigned argsize)
423 /* Function prologue */
425 if ((flags & CF_FIXARGC) != 0) {
426 /* Just remember the argument size for the leave */
430 AddCodeLine ("jsr enter");
437 /* Function epilogue */
439 /* How many bytes of locals do we have to drop? */
440 unsigned ToDrop = (unsigned) -StackPtr;
442 /* If we didn't have a variable argument list, don't call leave */
445 /* Drop stackframe if needed */
446 g_drop (ToDrop + funcargs);
448 } else if (StackPtr != 0) {
450 /* We've a stack frame to drop */
452 g_drop (ToDrop); /* Inlines the code */
453 AddCodeLine ("jsr leave");
455 AddCodeLine ("ldy #$%02X", ToDrop);
456 AddCodeLine ("jsr leavey");
461 /* Nothing to drop */
462 AddCodeLine ("jsr leave");
466 /* Add the final rts */
472 /*****************************************************************************/
473 /* Register variables */
474 /*****************************************************************************/
478 void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
479 /* Swap a register variable with a location on the stack */
481 /* Calculate the actual stack offset and check it */
482 StackOffs -= StackPtr;
483 CheckLocalOffs (StackOffs);
486 AddCodeLine ("ldy #$%02X", StackOffs & 0xFF);
489 if (IS_Get (&CodeSizeFactor) < 165) {
490 AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
491 AddCodeLine ("jsr regswap1");
493 AddCodeLine ("lda (sp),y");
494 AddCodeLine ("ldx regbank%+d", RegOffs);
495 AddCodeLine ("sta regbank%+d", RegOffs);
497 AddCodeLine ("sta (sp),y");
500 } else if (Bytes == 2) {
502 AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
503 AddCodeLine ("jsr regswap2");
507 AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
508 AddCodeLine ("lda #$%02X", Bytes & 0xFF);
509 AddCodeLine ("jsr regswap");
515 void g_save_regvars (int RegOffs, unsigned Bytes)
516 /* Save register variables */
518 /* Don't loop for up to two bytes */
521 AddCodeLine ("lda regbank%+d", RegOffs);
522 AddCodeLine ("jsr pusha");
524 } else if (Bytes == 2) {
526 AddCodeLine ("lda regbank%+d", RegOffs);
527 AddCodeLine ("ldx regbank%+d", RegOffs+1);
528 AddCodeLine ("jsr pushax");
532 /* More than two bytes - loop */
533 unsigned Label = GetLocalLabel ();
535 AddCodeLine ("ldy #$%02X", (unsigned char) (Bytes - 1));
536 AddCodeLine ("ldx #$%02X", (unsigned char) Bytes);
537 g_defcodelabel (Label);
538 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
539 AddCodeLine ("sta (sp),y");
542 AddCodeLine ("bne %s", LocalLabelName (Label));
546 /* We pushed stuff, correct the stack pointer */
552 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
553 /* Restore register variables */
555 /* Calculate the actual stack offset and check it */
556 StackOffs -= StackPtr;
557 CheckLocalOffs (StackOffs);
559 /* Don't loop for up to two bytes */
562 AddCodeLine ("ldy #$%02X", StackOffs);
563 AddCodeLine ("lda (sp),y");
564 AddCodeLine ("sta regbank%+d", RegOffs);
566 } else if (Bytes == 2) {
568 AddCodeLine ("ldy #$%02X", StackOffs);
569 AddCodeLine ("lda (sp),y");
570 AddCodeLine ("sta regbank%+d", RegOffs);
572 AddCodeLine ("lda (sp),y");
573 AddCodeLine ("sta regbank%+d", RegOffs+1);
575 } else if (Bytes == 3 && IS_Get (&CodeSizeFactor) >= 133) {
577 AddCodeLine ("ldy #$%02X", StackOffs);
578 AddCodeLine ("lda (sp),y");
579 AddCodeLine ("sta regbank%+d", RegOffs);
581 AddCodeLine ("lda (sp),y");
582 AddCodeLine ("sta regbank%+d", RegOffs+1);
584 AddCodeLine ("lda (sp),y");
585 AddCodeLine ("sta regbank%+d", RegOffs+2);
587 } else if (StackOffs <= RegOffs) {
589 /* More bytes, but the relation between the register offset in the
590 * register bank and the stack offset allows us to generate short
591 * code that uses just one index register.
593 unsigned Label = GetLocalLabel ();
594 AddCodeLine ("ldy #$%02X", StackOffs);
595 g_defcodelabel (Label);
596 AddCodeLine ("lda (sp),y");
597 AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs);
599 AddCodeLine ("cpy #$%02X", StackOffs + Bytes);
600 AddCodeLine ("bne %s", LocalLabelName (Label));
604 /* Ok, this is the generic code. We need to save X because the
605 * caller will only save A.
607 unsigned Label = GetLocalLabel ();
608 AddCodeLine ("stx tmp1");
609 AddCodeLine ("ldy #$%02X", (unsigned char) (StackOffs + Bytes - 1));
610 AddCodeLine ("ldx #$%02X", (unsigned char) (Bytes - 1));
611 g_defcodelabel (Label);
612 AddCodeLine ("lda (sp),y");
613 AddCodeLine ("sta regbank%+d,x", RegOffs);
616 AddCodeLine ("bpl %s", LocalLabelName (Label));
617 AddCodeLine ("ldx tmp1");
624 /*****************************************************************************/
625 /* Fetching memory cells */
626 /*****************************************************************************/
630 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
631 /* Load a constant into the primary register */
633 unsigned char B1, B2, B3, B4;
637 if ((Flags & CF_CONST) != 0) {
639 /* Numeric constant */
640 switch (Flags & CF_TYPE) {
643 if ((Flags & CF_FORCECHAR) != 0) {
644 AddCodeLine ("lda #$%02X", (unsigned char) Val);
649 AddCodeLine ("ldx #$%02X", (unsigned char) (Val >> 8));
650 AddCodeLine ("lda #$%02X", (unsigned char) Val);
654 /* Split the value into 4 bytes */
655 B1 = (unsigned char) (Val >> 0);
656 B2 = (unsigned char) (Val >> 8);
657 B3 = (unsigned char) (Val >> 16);
658 B4 = (unsigned char) (Val >> 24);
660 /* Remember which bytes are done */
664 AddCodeLine ("ldx #$%02X", B2);
667 AddCodeLine ("stx sreg");
671 AddCodeLine ("stx sreg+1");
674 if ((Done & 0x04) == 0 && B1 != B3) {
675 AddCodeLine ("lda #$%02X", B3);
676 AddCodeLine ("sta sreg");
679 if ((Done & 0x08) == 0 && B1 != B4) {
680 AddCodeLine ("lda #$%02X", B4);
681 AddCodeLine ("sta sreg+1");
684 AddCodeLine ("lda #$%02X", B1);
686 if ((Done & 0x04) == 0) {
688 AddCodeLine ("sta sreg");
690 if ((Done & 0x08) == 0) {
692 AddCodeLine ("sta sreg+1");
704 /* Some sort of label */
705 const char* Label = GetLabelName (Flags, Val, Offs);
707 /* Load the address into the primary */
708 AddCodeLine ("lda #<(%s)", Label);
709 AddCodeLine ("ldx #>(%s)", Label);
716 void g_getstatic (unsigned flags, unsigned long label, long offs)
717 /* Fetch an static memory cell into the primary register */
719 /* Create the correct label name */
720 const char* lbuf = GetLabelName (flags, label, offs);
722 /* Check the size and generate the correct load operation */
723 switch (flags & CF_TYPE) {
726 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
727 AddCodeLine ("lda %s", lbuf); /* load A from the label */
729 AddCodeLine ("ldx #$00");
730 AddCodeLine ("lda %s", lbuf); /* load A from the label */
731 if (!(flags & CF_UNSIGNED)) {
732 /* Must sign extend */
733 unsigned L = GetLocalLabel ();
734 AddCodeLine ("bpl %s", LocalLabelName (L));
742 AddCodeLine ("lda %s", lbuf);
743 if (flags & CF_TEST) {
744 AddCodeLine ("ora %s+1", lbuf);
746 AddCodeLine ("ldx %s+1", lbuf);
751 if (flags & CF_TEST) {
752 AddCodeLine ("lda %s+3", lbuf);
753 AddCodeLine ("ora %s+2", lbuf);
754 AddCodeLine ("ora %s+1", lbuf);
755 AddCodeLine ("ora %s+0", lbuf);
757 AddCodeLine ("lda %s+3", lbuf);
758 AddCodeLine ("sta sreg+1");
759 AddCodeLine ("lda %s+2", lbuf);
760 AddCodeLine ("sta sreg");
761 AddCodeLine ("ldx %s+1", lbuf);
762 AddCodeLine ("lda %s", lbuf);
774 void g_getlocal (unsigned Flags, int Offs)
775 /* Fetch specified local object (local var). */
778 switch (Flags & CF_TYPE) {
781 CheckLocalOffs (Offs);
782 if ((Flags & CF_FORCECHAR) || (Flags & CF_TEST)) {
783 AddCodeLine ("ldy #$%02X", Offs);
784 AddCodeLine ("lda (sp),y");
786 AddCodeLine ("ldy #$%02X", Offs);
787 AddCodeLine ("ldx #$00");
788 AddCodeLine ("lda (sp),y");
789 if ((Flags & CF_UNSIGNED) == 0) {
790 unsigned L = GetLocalLabel();
791 AddCodeLine ("bpl %s", LocalLabelName (L));
799 CheckLocalOffs (Offs + 1);
800 AddCodeLine ("ldy #$%02X", (unsigned char) (Offs+1));
801 if (Flags & CF_TEST) {
802 AddCodeLine ("lda (sp),y");
804 AddCodeLine ("ora (sp),y");
805 } else if (IS_Get (&CodeSizeFactor) < 165) {
806 AddCodeLine ("jsr ldaxysp");
808 AddCodeLine ("lda (sp),y");
811 AddCodeLine ("lda (sp),y");
816 CheckLocalOffs (Offs + 3);
817 AddCodeLine ("ldy #$%02X", (unsigned char) (Offs+3));
818 AddCodeLine ("jsr ldeaxysp");
819 if (Flags & CF_TEST) {
831 void g_getind (unsigned Flags, unsigned Offs)
832 /* Fetch the specified object type indirect through the primary register
833 * into the primary register
836 /* If the offset is greater than 255, add the part that is > 255 to
837 * the primary. This way we get an easy addition and use the low byte
840 Offs = MakeByteOffs (Flags, Offs);
842 /* Handle the indirect fetch */
843 switch (Flags & CF_TYPE) {
846 /* Character sized */
847 AddCodeLine ("ldy #$%02X", Offs);
848 if (Flags & CF_UNSIGNED) {
849 AddCodeLine ("jsr ldauidx");
851 AddCodeLine ("jsr ldaidx");
856 if (Flags & CF_TEST) {
857 AddCodeLine ("ldy #$%02X", Offs);
858 AddCodeLine ("sta ptr1");
859 AddCodeLine ("stx ptr1+1");
860 AddCodeLine ("lda (ptr1),y");
862 AddCodeLine ("ora (ptr1),y");
864 AddCodeLine ("ldy #$%02X", Offs+1);
865 AddCodeLine ("jsr ldaxidx");
870 AddCodeLine ("ldy #$%02X", Offs+3);
871 AddCodeLine ("jsr ldeaxidx");
872 if (Flags & CF_TEST) {
885 void g_leasp (int Offs)
886 /* Fetch the address of the specified symbol into the primary register */
888 unsigned char Lo, Hi;
890 /* Calculate the offset relative to sp */
893 /* Get low and high byte */
894 Lo = (unsigned char) Offs;
895 Hi = (unsigned char) (Offs >> 8);
900 AddCodeLine ("lda sp");
901 AddCodeLine ("ldx sp+1");
906 AddCodeLine ("lda sp+1");
908 AddCodeLine ("adc #$%02X", Hi);
910 AddCodeLine ("lda sp");
912 } else if (Hi == 0) {
914 if (IS_Get (&CodeSizeFactor) < 200) {
915 /* 8 bit offset with subroutine call */
916 AddCodeLine ("lda #$%02X", Lo);
917 AddCodeLine ("jsr leaa0sp");
919 /* 8 bit offset inlined */
920 unsigned L = GetLocalLabel ();
921 AddCodeLine ("lda sp");
922 AddCodeLine ("ldx sp+1");
924 AddCodeLine ("adc #$%02X", Lo);
925 AddCodeLine ("bcc %s", LocalLabelName (L));
929 } else if (IS_Get (&CodeSizeFactor) < 170) {
930 /* Full 16 bit offset with subroutine call */
931 AddCodeLine ("lda #$%02X", Lo);
932 AddCodeLine ("ldx #$%02X", Hi);
933 AddCodeLine ("jsr leaaxsp");
935 /* Full 16 bit offset inlined */
936 AddCodeLine ("lda sp");
938 AddCodeLine ("adc #$%02X", Lo);
940 AddCodeLine ("lda sp+1");
941 AddCodeLine ("adc #$%02X", Hi);
949 void g_leavariadic (int Offs)
950 /* Fetch the address of a parameter in a variadic function into the primary
954 unsigned ArgSizeOffs;
956 /* Calculate the offset relative to sp */
959 /* Get the offset of the parameter which is stored at sp+0 on function
960 * entry and check if this offset is reachable with a byte offset.
962 CHECK (StackPtr <= 0);
963 ArgSizeOffs = -StackPtr;
964 CheckLocalOffs (ArgSizeOffs);
966 /* Get the size of all parameters. */
967 AddCodeLine ("ldy #$%02X", ArgSizeOffs);
968 AddCodeLine ("lda (sp),y");
970 /* Add the value of the stackpointer */
971 if (IS_Get (&CodeSizeFactor) > 250) {
972 unsigned L = GetLocalLabel();
973 AddCodeLine ("ldx sp+1");
975 AddCodeLine ("adc sp");
976 AddCodeLine ("bcc %s", LocalLabelName (L));
980 AddCodeLine ("ldx #$00");
981 AddCodeLine ("jsr leaaxsp");
984 /* Add the offset to the primary */
986 g_inc (CF_INT | CF_CONST, Offs);
987 } else if (Offs < 0) {
988 g_dec (CF_INT | CF_CONST, -Offs);
994 /*****************************************************************************/
995 /* Store into memory */
996 /*****************************************************************************/
1000 void g_putstatic (unsigned flags, unsigned long label, long offs)
1001 /* Store the primary register into the specified static memory cell */
1003 /* Create the correct label name */
1004 const char* lbuf = GetLabelName (flags, label, offs);
1006 /* Check the size and generate the correct store operation */
1007 switch (flags & CF_TYPE) {
1010 AddCodeLine ("sta %s", lbuf);
1014 AddCodeLine ("sta %s", lbuf);
1015 AddCodeLine ("stx %s+1", lbuf);
1019 AddCodeLine ("sta %s", lbuf);
1020 AddCodeLine ("stx %s+1", lbuf);
1021 AddCodeLine ("ldy sreg");
1022 AddCodeLine ("sty %s+2", lbuf);
1023 AddCodeLine ("ldy sreg+1");
1024 AddCodeLine ("sty %s+3", lbuf);
1035 void g_putlocal (unsigned Flags, int Offs, long Val)
1036 /* Put data into local object. */
1039 CheckLocalOffs (Offs);
1040 switch (Flags & CF_TYPE) {
1043 if (Flags & CF_CONST) {
1044 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1046 AddCodeLine ("ldy #$%02X", Offs);
1047 AddCodeLine ("sta (sp),y");
1051 if (Flags & CF_CONST) {
1052 AddCodeLine ("ldy #$%02X", Offs+1);
1053 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1054 AddCodeLine ("sta (sp),y");
1055 if ((Flags & CF_NOKEEP) == 0) {
1056 /* Place high byte into X */
1057 AddCodeLine ("tax");
1059 if ((Val & 0xFF) == Offs+1) {
1060 /* The value we need is already in Y */
1061 AddCodeLine ("tya");
1062 AddCodeLine ("dey");
1064 AddCodeLine ("dey");
1065 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1067 AddCodeLine ("sta (sp),y");
1069 AddCodeLine ("ldy #$%02X", Offs);
1070 if ((Flags & CF_NOKEEP) == 0 || IS_Get (&CodeSizeFactor) < 160) {
1071 AddCodeLine ("jsr staxysp");
1073 AddCodeLine ("sta (sp),y");
1074 AddCodeLine ("iny");
1075 AddCodeLine ("txa");
1076 AddCodeLine ("sta (sp),y");
1082 if (Flags & CF_CONST) {
1083 g_getimmed (Flags, Val, 0);
1085 AddCodeLine ("ldy #$%02X", Offs);
1086 AddCodeLine ("jsr steaxysp");
1097 void g_putind (unsigned Flags, unsigned Offs)
1098 /* Store the specified object type in the primary register at the address
1099 * on the top of the stack
1102 /* We can handle offsets below $100 directly, larger offsets must be added
1103 * to the address. Since a/x is in use, best code is achieved by adding
1104 * just the high byte. Be sure to check if the low byte will overflow while
1107 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1109 /* Overflow - we need to add the low byte also */
1110 AddCodeLine ("ldy #$00");
1111 AddCodeLine ("clc");
1112 AddCodeLine ("pha");
1113 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1114 AddCodeLine ("adc (sp),y");
1115 AddCodeLine ("sta (sp),y");
1116 AddCodeLine ("iny");
1117 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1118 AddCodeLine ("adc (sp),y");
1119 AddCodeLine ("sta (sp),y");
1120 AddCodeLine ("pla");
1122 /* Complete address is on stack, new offset is zero */
1125 } else if ((Offs & 0xFF00) != 0) {
1127 /* We can just add the high byte */
1128 AddCodeLine ("ldy #$01");
1129 AddCodeLine ("clc");
1130 AddCodeLine ("pha");
1131 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1132 AddCodeLine ("adc (sp),y");
1133 AddCodeLine ("sta (sp),y");
1134 AddCodeLine ("pla");
1136 /* Offset is now just the low byte */
1140 /* Check the size and determine operation */
1141 AddCodeLine ("ldy #$%02X", Offs);
1142 switch (Flags & CF_TYPE) {
1145 AddCodeLine ("jsr staspidx");
1149 AddCodeLine ("jsr staxspidx");
1153 AddCodeLine ("jsr steaxspidx");
1161 /* Pop the argument which is always a pointer */
1167 /*****************************************************************************/
1168 /* type conversion and similiar stuff */
1169 /*****************************************************************************/
1173 void g_toslong (unsigned flags)
1174 /* Make sure, the value on TOS is a long. Convert if necessary */
1176 switch (flags & CF_TYPE) {
1180 if (flags & CF_UNSIGNED) {
1181 AddCodeLine ("jsr tosulong");
1183 AddCodeLine ("jsr toslong");
1198 void g_tosint (unsigned flags)
1199 /* Make sure, the value on TOS is an int. Convert if necessary */
1201 switch (flags & CF_TYPE) {
1208 AddCodeLine ("jsr tosint");
1219 void g_regint (unsigned Flags)
1220 /* Make sure, the value in the primary register an int. Convert if necessary */
1224 switch (Flags & CF_TYPE) {
1227 if (Flags & CF_FORCECHAR) {
1228 /* Conversion is from char */
1229 if (Flags & CF_UNSIGNED) {
1230 AddCodeLine ("ldx #$00");
1232 L = GetLocalLabel();
1233 AddCodeLine ("ldx #$00");
1234 AddCodeLine ("cmp #$80");
1235 AddCodeLine ("bcc %s", LocalLabelName (L));
1236 AddCodeLine ("dex");
1253 void g_reglong (unsigned Flags)
1254 /* Make sure, the value in the primary register a long. Convert if necessary */
1258 switch (Flags & CF_TYPE) {
1261 if (Flags & CF_FORCECHAR) {
1262 /* Conversion is from char */
1263 if (Flags & CF_UNSIGNED) {
1264 if (IS_Get (&CodeSizeFactor) >= 200) {
1265 AddCodeLine ("ldx #$00");
1266 AddCodeLine ("stx sreg");
1267 AddCodeLine ("stx sreg+1");
1269 AddCodeLine ("jsr aulong");
1272 if (IS_Get (&CodeSizeFactor) >= 366) {
1273 L = GetLocalLabel();
1274 AddCodeLine ("ldx #$00");
1275 AddCodeLine ("cmp #$80");
1276 AddCodeLine ("bcc %s", LocalLabelName (L));
1277 AddCodeLine ("dex");
1279 AddCodeLine ("stx sreg");
1280 AddCodeLine ("stx sreg+1");
1282 AddCodeLine ("jsr along");
1289 if (Flags & CF_UNSIGNED) {
1290 if (IS_Get (&CodeSizeFactor) >= 200) {
1291 AddCodeLine ("ldy #$00");
1292 AddCodeLine ("sty sreg");
1293 AddCodeLine ("sty sreg+1");
1295 AddCodeLine ("jsr axulong");
1298 AddCodeLine ("jsr axlong");
1312 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1313 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1314 * value, that corresponds to the value on TOS, rhs corresponds to the value
1315 * in (e)ax. The return value is the the flags value for the resulting type.
1318 unsigned ltype, rtype;
1321 /* Get the type spec from the flags */
1322 ltype = lhs & CF_TYPE;
1323 rtype = rhs & CF_TYPE;
1325 /* Check if a conversion is needed */
1326 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1327 /* We must promote the primary register to long */
1329 /* Get the new rhs type */
1330 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1332 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1333 /* We must promote the lhs to long */
1339 /* Get the new rhs type */
1340 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1344 /* Determine the result type for the operation:
1345 * - The result is const if both operands are const.
1346 * - The result is unsigned if one of the operands is unsigned.
1347 * - The result is long if one of the operands is long.
1348 * - Otherwise the result is int sized.
1350 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1351 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1352 if (rtype == CF_LONG || ltype == CF_LONG) {
1362 unsigned g_typecast (unsigned lhs, unsigned rhs)
1363 /* Cast the value in the primary register to the operand size that is flagged
1364 * by the lhs value. Return the result value.
1367 unsigned ltype, rtype;
1369 /* Get the type spec from the flags */
1370 ltype = lhs & CF_TYPE;
1371 rtype = rhs & CF_TYPE;
1373 /* Check if a conversion is needed */
1374 if ((rhs & CF_CONST) == 0) {
1375 if (ltype == CF_LONG && rtype != CF_LONG) {
1376 /* We must promote the primary register to long */
1378 } else if (ltype == CF_INT && rtype != CF_INT) {
1379 /* We must promote the primary register to int */
1384 /* Do not need any other action. If the left type is int, and the primary
1385 * register is long, it will be automagically truncated. If the right hand
1386 * side is const, it is not located in the primary register and handled by
1387 * the expression parser code.
1390 /* Result is const if the right hand side was const */
1391 lhs |= (rhs & CF_CONST);
1393 /* The resulting type is that of the left hand side (that's why you called
1401 void g_scale (unsigned flags, long val)
1402 /* Scale the value in the primary register by the given value. If val is positive,
1403 * scale up, is val is negative, scale down. This function is used to scale
1404 * the operands or results of pointer arithmetic by the size of the type, the
1405 * pointer points to.
1410 /* Value may not be zero */
1412 Internal ("Data type has no size");
1413 } else if (val > 0) {
1416 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1418 /* Factor is 2, 4, 8 and 16, use special function */
1419 switch (flags & CF_TYPE) {
1422 if (flags & CF_FORCECHAR) {
1424 AddCodeLine ("asl a");
1431 if (flags & CF_UNSIGNED) {
1432 AddCodeLine ("jsr shlax%d", p2);
1434 AddCodeLine ("jsr aslax%d", p2);
1439 if (flags & CF_UNSIGNED) {
1440 AddCodeLine ("jsr shleax%d", p2);
1442 AddCodeLine ("jsr asleax%d", p2);
1451 } else if (val != 1) {
1453 /* Use a multiplication instead */
1454 g_mul (flags | CF_CONST, val);
1462 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1464 /* Factor is 2, 4, 8 and 16 use special function */
1465 switch (flags & CF_TYPE) {
1468 if (flags & CF_FORCECHAR) {
1469 if (flags & CF_UNSIGNED) {
1471 AddCodeLine ("lsr a");
1474 } else if (p2 <= 2) {
1475 AddCodeLine ("cmp #$80");
1476 AddCodeLine ("ror a");
1483 if (flags & CF_UNSIGNED) {
1484 AddCodeLine ("jsr lsrax%d", p2);
1486 AddCodeLine ("jsr asrax%d", p2);
1491 if (flags & CF_UNSIGNED) {
1492 AddCodeLine ("jsr lsreax%d", p2);
1494 AddCodeLine ("jsr asreax%d", p2);
1503 } else if (val != 1) {
1505 /* Use a division instead */
1506 g_div (flags | CF_CONST, val);
1514 /*****************************************************************************/
1515 /* Adds and subs of variables fix a fixed address */
1516 /*****************************************************************************/
1520 void g_addlocal (unsigned flags, int offs)
1521 /* Add a local variable to ax */
1525 /* Correct the offset and check it */
1527 CheckLocalOffs (offs);
1529 switch (flags & CF_TYPE) {
1532 L = GetLocalLabel();
1533 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1534 AddCodeLine ("clc");
1535 AddCodeLine ("adc (sp),y");
1536 AddCodeLine ("bcc %s", LocalLabelName (L));
1537 AddCodeLine ("inx");
1542 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1543 AddCodeLine ("clc");
1544 AddCodeLine ("adc (sp),y");
1545 AddCodeLine ("pha");
1546 AddCodeLine ("txa");
1547 AddCodeLine ("iny");
1548 AddCodeLine ("adc (sp),y");
1549 AddCodeLine ("tax");
1550 AddCodeLine ("pla");
1554 /* Do it the old way */
1556 g_getlocal (flags, offs);
1568 void g_addstatic (unsigned flags, unsigned long label, long offs)
1569 /* Add a static variable to ax */
1573 /* Create the correct label name */
1574 const char* lbuf = GetLabelName (flags, label, offs);
1576 switch (flags & CF_TYPE) {
1579 L = GetLocalLabel();
1580 AddCodeLine ("clc");
1581 AddCodeLine ("adc %s", lbuf);
1582 AddCodeLine ("bcc %s", LocalLabelName (L));
1583 AddCodeLine ("inx");
1588 AddCodeLine ("clc");
1589 AddCodeLine ("adc %s", lbuf);
1590 AddCodeLine ("tay");
1591 AddCodeLine ("txa");
1592 AddCodeLine ("adc %s+1", lbuf);
1593 AddCodeLine ("tax");
1594 AddCodeLine ("tya");
1598 /* Do it the old way */
1600 g_getstatic (flags, label, offs);
1612 /*****************************************************************************/
1613 /* Special op= functions */
1614 /*****************************************************************************/
1618 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1620 /* Emit += for a static variable */
1622 /* Create the correct label name */
1623 const char* lbuf = GetLabelName (flags, label, offs);
1625 /* Check the size and determine operation */
1626 switch (flags & CF_TYPE) {
1629 if (flags & CF_FORCECHAR) {
1630 AddCodeLine ("ldx #$00");
1631 if (flags & CF_CONST) {
1633 AddCodeLine ("inc %s", lbuf);
1634 AddCodeLine ("lda %s", lbuf);
1636 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1637 AddCodeLine ("clc");
1638 AddCodeLine ("adc %s", lbuf);
1639 AddCodeLine ("sta %s", lbuf);
1642 AddCodeLine ("clc");
1643 AddCodeLine ("adc %s", lbuf);
1644 AddCodeLine ("sta %s", lbuf);
1646 if ((flags & CF_UNSIGNED) == 0) {
1647 unsigned L = GetLocalLabel();
1648 AddCodeLine ("bpl %s", LocalLabelName (L));
1649 AddCodeLine ("dex");
1657 if (flags & CF_CONST) {
1659 unsigned L = GetLocalLabel ();
1660 AddCodeLine ("inc %s", lbuf);
1661 AddCodeLine ("bne %s", LocalLabelName (L));
1662 AddCodeLine ("inc %s+1", lbuf);
1664 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1665 AddCodeLine ("ldx %s+1", lbuf);
1667 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1668 AddCodeLine ("clc");
1669 AddCodeLine ("adc %s", lbuf);
1670 AddCodeLine ("sta %s", lbuf);
1672 unsigned L = GetLocalLabel ();
1673 AddCodeLine ("bcc %s", LocalLabelName (L));
1674 AddCodeLine ("inc %s+1", lbuf);
1676 AddCodeLine ("ldx %s+1", lbuf);
1678 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1679 AddCodeLine ("adc %s+1", lbuf);
1680 AddCodeLine ("sta %s+1", lbuf);
1681 AddCodeLine ("tax");
1682 AddCodeLine ("lda %s", lbuf);
1686 AddCodeLine ("clc");
1687 AddCodeLine ("adc %s", lbuf);
1688 AddCodeLine ("sta %s", lbuf);
1689 AddCodeLine ("txa");
1690 AddCodeLine ("adc %s+1", lbuf);
1691 AddCodeLine ("sta %s+1", lbuf);
1692 AddCodeLine ("tax");
1693 AddCodeLine ("lda %s", lbuf);
1698 if (flags & CF_CONST) {
1700 AddCodeLine ("ldy #<(%s)", lbuf);
1701 AddCodeLine ("sty ptr1");
1702 AddCodeLine ("ldy #>(%s)", lbuf);
1704 AddCodeLine ("jsr laddeq1");
1706 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1707 AddCodeLine ("jsr laddeqa");
1710 g_getstatic (flags, label, offs);
1712 g_putstatic (flags, label, offs);
1715 AddCodeLine ("ldy #<(%s)", lbuf);
1716 AddCodeLine ("sty ptr1");
1717 AddCodeLine ("ldy #>(%s)", lbuf);
1718 AddCodeLine ("jsr laddeq");
1729 void g_addeqlocal (unsigned flags, int Offs, unsigned long val)
1730 /* Emit += for a local variable */
1732 /* Calculate the true offset, check it, load it into Y */
1734 CheckLocalOffs (Offs);
1736 /* Check the size and determine operation */
1737 switch (flags & CF_TYPE) {
1740 if (flags & CF_FORCECHAR) {
1741 AddCodeLine ("ldy #$%02X", Offs);
1742 AddCodeLine ("ldx #$00");
1743 if (flags & CF_CONST) {
1744 AddCodeLine ("clc");
1745 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1746 AddCodeLine ("adc (sp),y");
1747 AddCodeLine ("sta (sp),y");
1749 AddCodeLine ("clc");
1750 AddCodeLine ("adc (sp),y");
1751 AddCodeLine ("sta (sp),y");
1753 if ((flags & CF_UNSIGNED) == 0) {
1754 unsigned L = GetLocalLabel();
1755 AddCodeLine ("bpl %s", LocalLabelName (L));
1756 AddCodeLine ("dex");
1764 AddCodeLine ("ldy #$%02X", Offs);
1765 if (flags & CF_CONST) {
1766 if (IS_Get (&CodeSizeFactor) >= 400) {
1767 AddCodeLine ("clc");
1768 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1769 AddCodeLine ("adc (sp),y");
1770 AddCodeLine ("sta (sp),y");
1771 AddCodeLine ("iny");
1772 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1773 AddCodeLine ("adc (sp),y");
1774 AddCodeLine ("sta (sp),y");
1775 AddCodeLine ("tax");
1776 AddCodeLine ("dey");
1777 AddCodeLine ("lda (sp),y");
1779 g_getimmed (flags, val, 0);
1780 AddCodeLine ("jsr addeqysp");
1783 AddCodeLine ("jsr addeqysp");
1788 if (flags & CF_CONST) {
1789 g_getimmed (flags, val, 0);
1791 AddCodeLine ("ldy #$%02X", Offs);
1792 AddCodeLine ("jsr laddeqysp");
1802 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1803 /* Emit += for the location with address in ax */
1805 /* If the offset is too large for a byte register, add the high byte
1806 * of the offset to the primary. Beware: We need a special correction
1807 * if the offset in the low byte will overflow in the operation.
1809 offs = MakeByteOffs (flags, offs);
1811 /* Check the size and determine operation */
1812 switch (flags & CF_TYPE) {
1815 AddCodeLine ("sta ptr1");
1816 AddCodeLine ("stx ptr1+1");
1817 AddCodeLine ("ldy #$%02X", offs);
1818 AddCodeLine ("ldx #$00");
1819 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1820 AddCodeLine ("clc");
1821 AddCodeLine ("adc (ptr1),y");
1822 AddCodeLine ("sta (ptr1),y");
1826 if (IS_Get (&CodeSizeFactor) >= 200) {
1827 /* Lots of code, use only if size is not important */
1828 AddCodeLine ("sta ptr1");
1829 AddCodeLine ("stx ptr1+1");
1830 AddCodeLine ("ldy #$%02X", offs);
1831 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1832 AddCodeLine ("clc");
1833 AddCodeLine ("adc (ptr1),y");
1834 AddCodeLine ("sta (ptr1),y");
1835 AddCodeLine ("pha");
1836 AddCodeLine ("iny");
1837 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1838 AddCodeLine ("adc (ptr1),y");
1839 AddCodeLine ("sta (ptr1),y");
1840 AddCodeLine ("tax");
1841 AddCodeLine ("pla");
1847 AddCodeLine ("jsr pushax"); /* Push the address */
1848 push (CF_PTR); /* Correct the internal sp */
1849 g_getind (flags, offs); /* Fetch the value */
1850 g_inc (flags, val); /* Increment value in primary */
1851 g_putind (flags, offs); /* Store the value back */
1861 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1863 /* Emit -= for a static variable */
1865 /* Create the correct label name */
1866 const char* lbuf = GetLabelName (flags, label, offs);
1868 /* Check the size and determine operation */
1869 switch (flags & CF_TYPE) {
1872 if (flags & CF_FORCECHAR) {
1873 AddCodeLine ("ldx #$00");
1874 if (flags & CF_CONST) {
1876 AddCodeLine ("dec %s", lbuf);
1877 AddCodeLine ("lda %s", lbuf);
1879 AddCodeLine ("lda %s", lbuf);
1880 AddCodeLine ("sec");
1881 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1882 AddCodeLine ("sta %s", lbuf);
1885 AddCodeLine ("eor #$FF");
1886 AddCodeLine ("sec");
1887 AddCodeLine ("adc %s", lbuf);
1888 AddCodeLine ("sta %s", lbuf);
1890 if ((flags & CF_UNSIGNED) == 0) {
1891 unsigned L = GetLocalLabel();
1892 AddCodeLine ("bpl %s", LocalLabelName (L));
1893 AddCodeLine ("dex");
1901 if (flags & CF_CONST) {
1902 AddCodeLine ("lda %s", lbuf);
1903 AddCodeLine ("sec");
1904 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1905 AddCodeLine ("sta %s", lbuf);
1907 unsigned L = GetLocalLabel ();
1908 AddCodeLine ("bcs %s", LocalLabelName (L));
1909 AddCodeLine ("dec %s+1", lbuf);
1911 AddCodeLine ("ldx %s+1", lbuf);
1913 AddCodeLine ("lda %s+1", lbuf);
1914 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1915 AddCodeLine ("sta %s+1", lbuf);
1916 AddCodeLine ("tax");
1917 AddCodeLine ("lda %s", lbuf);
1920 AddCodeLine ("eor #$FF");
1921 AddCodeLine ("sec");
1922 AddCodeLine ("adc %s", lbuf);
1923 AddCodeLine ("sta %s", lbuf);
1924 AddCodeLine ("txa");
1925 AddCodeLine ("eor #$FF");
1926 AddCodeLine ("adc %s+1", lbuf);
1927 AddCodeLine ("sta %s+1", lbuf);
1928 AddCodeLine ("tax");
1929 AddCodeLine ("lda %s", lbuf);
1934 if (flags & CF_CONST) {
1936 AddCodeLine ("ldy #<(%s)", lbuf);
1937 AddCodeLine ("sty ptr1");
1938 AddCodeLine ("ldy #>(%s)", lbuf);
1939 AddCodeLine ("lda #$%02X", (unsigned char)val);
1940 AddCodeLine ("jsr lsubeqa");
1942 g_getstatic (flags, label, offs);
1944 g_putstatic (flags, label, offs);
1947 AddCodeLine ("ldy #<(%s)", lbuf);
1948 AddCodeLine ("sty ptr1");
1949 AddCodeLine ("ldy #>(%s)", lbuf);
1950 AddCodeLine ("jsr lsubeq");
1961 void g_subeqlocal (unsigned flags, int Offs, unsigned long val)
1962 /* Emit -= for a local variable */
1964 /* Calculate the true offset, check it, load it into Y */
1966 CheckLocalOffs (Offs);
1968 /* Check the size and determine operation */
1969 switch (flags & CF_TYPE) {
1972 if (flags & CF_FORCECHAR) {
1973 AddCodeLine ("ldy #$%02X", Offs);
1974 AddCodeLine ("ldx #$00");
1975 if (flags & CF_CONST) {
1976 AddCodeLine ("lda (sp),y");
1977 AddCodeLine ("sec");
1978 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1980 AddCodeLine ("eor #$FF");
1981 AddCodeLine ("sec");
1982 AddCodeLine ("adc (sp),y");
1984 AddCodeLine ("sta (sp),y");
1985 if ((flags & CF_UNSIGNED) == 0) {
1986 unsigned L = GetLocalLabel();
1987 AddCodeLine ("bpl %s", LocalLabelName (L));
1988 AddCodeLine ("dex");
1996 if (flags & CF_CONST) {
1997 g_getimmed (flags, val, 0);
1999 AddCodeLine ("ldy #$%02X", Offs);
2000 AddCodeLine ("jsr subeqysp");
2004 if (flags & CF_CONST) {
2005 g_getimmed (flags, val, 0);
2007 AddCodeLine ("ldy #$%02X", Offs);
2008 AddCodeLine ("jsr lsubeqysp");
2018 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2019 /* Emit -= for the location with address in ax */
2021 /* If the offset is too large for a byte register, add the high byte
2022 * of the offset to the primary. Beware: We need a special correction
2023 * if the offset in the low byte will overflow in the operation.
2025 offs = MakeByteOffs (flags, offs);
2027 /* Check the size and determine operation */
2028 switch (flags & CF_TYPE) {
2031 AddCodeLine ("sta ptr1");
2032 AddCodeLine ("stx ptr1+1");
2033 AddCodeLine ("ldy #$%02X", offs);
2034 AddCodeLine ("ldx #$00");
2035 AddCodeLine ("lda (ptr1),y");
2036 AddCodeLine ("sec");
2037 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2038 AddCodeLine ("sta (ptr1),y");
2042 if (IS_Get (&CodeSizeFactor) >= 200) {
2043 /* Lots of code, use only if size is not important */
2044 AddCodeLine ("sta ptr1");
2045 AddCodeLine ("stx ptr1+1");
2046 AddCodeLine ("ldy #$%02X", offs);
2047 AddCodeLine ("lda (ptr1),y");
2048 AddCodeLine ("sec");
2049 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2050 AddCodeLine ("sta (ptr1),y");
2051 AddCodeLine ("pha");
2052 AddCodeLine ("iny");
2053 AddCodeLine ("lda (ptr1),y");
2054 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2055 AddCodeLine ("sta (ptr1),y");
2056 AddCodeLine ("tax");
2057 AddCodeLine ("pla");
2063 AddCodeLine ("jsr pushax"); /* Push the address */
2064 push (CF_PTR); /* Correct the internal sp */
2065 g_getind (flags, offs); /* Fetch the value */
2066 g_dec (flags, val); /* Increment value in primary */
2067 g_putind (flags, offs); /* Store the value back */
2077 /*****************************************************************************/
2078 /* Add a variable address to the value in ax */
2079 /*****************************************************************************/
2083 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2084 /* Add the address of a local variable to ax */
2088 /* Add the offset */
2091 /* We cannot address more then 256 bytes of locals anyway */
2092 L = GetLocalLabel();
2093 CheckLocalOffs (offs);
2094 AddCodeLine ("clc");
2095 AddCodeLine ("adc #$%02X", offs & 0xFF);
2096 /* Do also skip the CLC insn below */
2097 AddCodeLine ("bcc %s", LocalLabelName (L));
2098 AddCodeLine ("inx");
2101 /* Add the current stackpointer value */
2102 AddCodeLine ("clc");
2104 /* Label was used above */
2107 AddCodeLine ("adc sp");
2108 AddCodeLine ("tay");
2109 AddCodeLine ("txa");
2110 AddCodeLine ("adc sp+1");
2111 AddCodeLine ("tax");
2112 AddCodeLine ("tya");
2117 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2118 /* Add the address of a static variable to ax */
2120 /* Create the correct label name */
2121 const char* lbuf = GetLabelName (flags, label, offs);
2123 /* Add the address to the current ax value */
2124 AddCodeLine ("clc");
2125 AddCodeLine ("adc #<(%s)", lbuf);
2126 AddCodeLine ("tay");
2127 AddCodeLine ("txa");
2128 AddCodeLine ("adc #>(%s)", lbuf);
2129 AddCodeLine ("tax");
2130 AddCodeLine ("tya");
2135 /*****************************************************************************/
2137 /*****************************************************************************/
2141 void g_save (unsigned flags)
2142 /* Copy primary register to hold register. */
2144 /* Check the size and determine operation */
2145 switch (flags & CF_TYPE) {
2148 if (flags & CF_FORCECHAR) {
2149 AddCodeLine ("pha");
2155 AddCodeLine ("sta regsave");
2156 AddCodeLine ("stx regsave+1");
2160 AddCodeLine ("jsr saveeax");
2170 void g_restore (unsigned flags)
2171 /* Copy hold register to primary. */
2173 /* Check the size and determine operation */
2174 switch (flags & CF_TYPE) {
2177 if (flags & CF_FORCECHAR) {
2178 AddCodeLine ("pla");
2184 AddCodeLine ("lda regsave");
2185 AddCodeLine ("ldx regsave+1");
2189 AddCodeLine ("jsr resteax");
2199 void g_cmp (unsigned flags, unsigned long val)
2200 /* Immidiate compare. The primary register will not be changed, Z flag
2206 /* Check the size and determine operation */
2207 switch (flags & CF_TYPE) {
2210 if (flags & CF_FORCECHAR) {
2211 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2217 L = GetLocalLabel();
2218 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2219 AddCodeLine ("bne %s", LocalLabelName (L));
2220 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2225 Internal ("g_cmp: Long compares not implemented");
2235 static void oper (unsigned Flags, unsigned long Val, const char** Subs)
2236 /* Encode a binary operation. subs is a pointer to four strings:
2237 * 0 --> Operate on ints
2238 * 1 --> Operate on unsigneds
2239 * 2 --> Operate on longs
2240 * 3 --> Operate on unsigned longs
2243 /* Determine the offset into the array */
2244 if (Flags & CF_UNSIGNED) {
2247 if ((Flags & CF_TYPE) == CF_LONG) {
2251 /* Load the value if it is not already in the primary */
2252 if (Flags & CF_CONST) {
2254 g_getimmed (Flags, Val, 0);
2257 /* Output the operation */
2258 AddCodeLine ("jsr %s", *Subs);
2260 /* The operation will pop it's argument */
2266 void g_test (unsigned flags)
2267 /* Test the value in the primary and set the condition codes */
2269 switch (flags & CF_TYPE) {
2272 if (flags & CF_FORCECHAR) {
2273 AddCodeLine ("tax");
2279 AddCodeLine ("stx tmp1");
2280 AddCodeLine ("ora tmp1");
2284 if (flags & CF_UNSIGNED) {
2285 AddCodeLine ("jsr utsteax");
2287 AddCodeLine ("jsr tsteax");
2299 void g_push (unsigned flags, unsigned long val)
2300 /* Push the primary register or a constant value onto the stack */
2302 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2304 /* We have a constant 8 or 16 bit value */
2305 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2307 /* Handle as 8 bit value */
2308 AddCodeLine ("lda #$%02X", (unsigned char) val);
2309 AddCodeLine ("jsr pusha");
2313 /* Handle as 16 bit value */
2314 g_getimmed (flags, val, 0);
2315 AddCodeLine ("jsr pushax");
2320 /* Value is not 16 bit or not constant */
2321 if (flags & CF_CONST) {
2322 /* Constant 32 bit value, load into eax */
2323 g_getimmed (flags, val, 0);
2326 /* Push the primary register */
2327 switch (flags & CF_TYPE) {
2330 if (flags & CF_FORCECHAR) {
2331 /* Handle as char */
2332 AddCodeLine ("jsr pusha");
2337 AddCodeLine ("jsr pushax");
2341 AddCodeLine ("jsr pusheax");
2351 /* Adjust the stack offset */
2357 void g_swap (unsigned flags)
2358 /* Swap the primary register and the top of the stack. flags give the type
2359 * of *both* values (must have same size).
2362 switch (flags & CF_TYPE) {
2366 AddCodeLine ("jsr swapstk");
2370 AddCodeLine ("jsr swapestk");
2381 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2382 /* Call the specified subroutine name */
2384 if ((Flags & CF_FIXARGC) == 0) {
2385 /* Pass the argument count */
2386 AddCodeLine ("ldy #$%02X", ArgSize);
2388 AddCodeLine ("jsr _%s", Label);
2389 StackPtr += ArgSize; /* callee pops args */
2394 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2395 /* Call subroutine indirect */
2397 if ((Flags & CF_LOCAL) == 0) {
2398 /* Address is in a/x */
2399 if ((Flags & CF_FIXARGC) == 0) {
2400 /* Pass arg count */
2401 AddCodeLine ("ldy #$%02X", ArgSize);
2403 AddCodeLine ("jsr callax");
2405 /* The address is on stack, offset is on Val */
2407 CheckLocalOffs (Offs);
2408 AddCodeLine ("pha");
2409 AddCodeLine ("ldy #$%02X", Offs);
2410 AddCodeLine ("lda (sp),y");
2411 AddCodeLine ("sta jmpvec+1");
2412 AddCodeLine ("iny");
2413 AddCodeLine ("lda (sp),y");
2414 AddCodeLine ("sta jmpvec+2");
2415 AddCodeLine ("pla");
2416 AddCodeLine ("jsr jmpvec");
2419 /* Callee pops args */
2420 StackPtr += ArgSize;
2425 void g_jump (unsigned Label)
2426 /* Jump to specified internal label number */
2428 AddCodeLine ("jmp %s", LocalLabelName (Label));
2433 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2434 /* Jump to label if zero flag clear */
2436 AddCodeLine ("jne %s", LocalLabelName (label));
2441 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2442 /* Jump to label if zero flag set */
2444 AddCodeLine ("jeq %s", LocalLabelName (label));
2449 void g_drop (unsigned Space)
2450 /* Drop space allocated on the stack */
2453 /* Inline the code since calling addysp repeatedly is quite some
2456 AddCodeLine ("pha");
2457 AddCodeLine ("lda #$%02X", (unsigned char) Space);
2458 AddCodeLine ("clc");
2459 AddCodeLine ("adc sp");
2460 AddCodeLine ("sta sp");
2461 AddCodeLine ("lda #$%02X", (unsigned char) (Space >> 8));
2462 AddCodeLine ("adc sp+1");
2463 AddCodeLine ("sta sp+1");
2464 AddCodeLine ("pla");
2465 } else if (Space > 8) {
2466 AddCodeLine ("ldy #$%02X", Space);
2467 AddCodeLine ("jsr addysp");
2468 } else if (Space != 0) {
2469 AddCodeLine ("jsr incsp%u", Space);
2475 void g_space (int Space)
2476 /* Create or drop space on the stack */
2479 /* This is actually a drop operation */
2481 } else if (Space > 255) {
2482 /* Inline the code since calling subysp repeatedly is quite some
2485 AddCodeLine ("pha");
2486 AddCodeLine ("lda sp");
2487 AddCodeLine ("sec");
2488 AddCodeLine ("sbc #$%02X", (unsigned char) Space);
2489 AddCodeLine ("sta sp");
2490 AddCodeLine ("lda sp+1");
2491 AddCodeLine ("sbc #$%02X", (unsigned char) (Space >> 8));
2492 AddCodeLine ("sta sp+1");
2493 AddCodeLine ("pla");
2494 } else if (Space > 8) {
2495 AddCodeLine ("ldy #$%02X", Space);
2496 AddCodeLine ("jsr subysp");
2497 } else if (Space != 0) {
2498 AddCodeLine ("jsr decsp%u", Space);
2504 void g_cstackcheck (void)
2505 /* Check for a C stack overflow */
2507 AddCodeLine ("jsr cstkchk");
2512 void g_stackcheck (void)
2513 /* Check for a stack overflow */
2515 AddCodeLine ("jsr stkchk");
2520 void g_add (unsigned flags, unsigned long val)
2521 /* Primary = TOS + Primary */
2523 static const char* ops[12] = {
2524 "tosaddax", "tosaddax", "tosaddeax", "tosaddeax"
2527 if (flags & CF_CONST) {
2528 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2529 g_push (flags & ~CF_CONST, 0);
2531 oper (flags, val, ops);
2536 void g_sub (unsigned flags, unsigned long val)
2537 /* Primary = TOS - Primary */
2539 static const char* ops[12] = {
2540 "tossubax", "tossubax", "tossubeax", "tossubeax",
2543 if (flags & CF_CONST) {
2544 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2545 g_push (flags & ~CF_CONST, 0);
2547 oper (flags, val, ops);
2552 void g_rsub (unsigned flags, unsigned long val)
2553 /* Primary = Primary - TOS */
2555 static const char* ops[12] = {
2556 "tosrsubax", "tosrsubax", "tosrsubeax", "tosrsubeax",
2558 oper (flags, val, ops);
2563 void g_mul (unsigned flags, unsigned long val)
2564 /* Primary = TOS * Primary */
2566 static const char* ops[12] = {
2567 "tosmulax", "tosumulax", "tosmuleax", "tosumuleax",
2572 /* Do strength reduction if the value is constant and a power of two */
2573 if (flags & CF_CONST && (p2 = PowerOf2 (val)) >= 0) {
2574 /* Generate a shift instead */
2579 /* If the right hand side is const, the lhs is not on stack but still
2580 * in the primary register.
2582 if (flags & CF_CONST) {
2584 switch (flags & CF_TYPE) {
2587 if (flags & CF_FORCECHAR) {
2588 /* Handle some special cases */
2592 AddCodeLine ("sta tmp1");
2593 AddCodeLine ("asl a");
2594 AddCodeLine ("clc");
2595 AddCodeLine ("adc tmp1");
2599 AddCodeLine ("sta tmp1");
2600 AddCodeLine ("asl a");
2601 AddCodeLine ("asl a");
2602 AddCodeLine ("clc");
2603 AddCodeLine ("adc tmp1");
2607 AddCodeLine ("sta tmp1");
2608 AddCodeLine ("asl a");
2609 AddCodeLine ("clc");
2610 AddCodeLine ("adc tmp1");
2611 AddCodeLine ("asl a");
2615 AddCodeLine ("sta tmp1");
2616 AddCodeLine ("asl a");
2617 AddCodeLine ("asl a");
2618 AddCodeLine ("clc");
2619 AddCodeLine ("adc tmp1");
2620 AddCodeLine ("asl a");
2629 AddCodeLine ("jsr mulax3");
2632 AddCodeLine ("jsr mulax5");
2635 AddCodeLine ("jsr mulax6");
2638 AddCodeLine ("jsr mulax7");
2641 AddCodeLine ("jsr mulax9");
2644 AddCodeLine ("jsr mulax10");
2656 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2657 * into the normal, non-optimized stuff.
2659 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2660 g_push (flags & ~CF_CONST, 0);
2664 /* Use long way over the stack */
2665 oper (flags, val, ops);
2670 void g_div (unsigned flags, unsigned long val)
2671 /* Primary = TOS / Primary */
2673 static const char* ops[12] = {
2674 "tosdivax", "tosudivax", "tosdiveax", "tosudiveax",
2677 /* Do strength reduction if the value is constant and a power of two */
2679 if ((flags & CF_CONST) && (p2 = PowerOf2 (val)) >= 0) {
2680 /* Generate a shift instead */
2683 /* Generate a division */
2684 if (flags & CF_CONST) {
2685 /* lhs is not on stack */
2686 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2687 g_push (flags & ~CF_CONST, 0);
2689 oper (flags, val, ops);
2695 void g_mod (unsigned flags, unsigned long val)
2696 /* Primary = TOS % Primary */
2698 static const char* ops[12] = {
2699 "tosmodax", "tosumodax", "tosmodeax", "tosumodeax",
2703 /* Check if we can do some cost reduction */
2704 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = PowerOf2 (val)) >= 0) {
2705 /* We can do that with an AND operation */
2706 g_and (flags, val - 1);
2708 /* Do it the hard way... */
2709 if (flags & CF_CONST) {
2710 /* lhs is not on stack */
2711 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2712 g_push (flags & ~CF_CONST, 0);
2714 oper (flags, val, ops);
2720 void g_or (unsigned flags, unsigned long val)
2721 /* Primary = TOS | Primary */
2723 static const char* ops[12] = {
2724 "tosorax", "tosorax", "tosoreax", "tosoreax",
2727 /* If the right hand side is const, the lhs is not on stack but still
2728 * in the primary register.
2730 if (flags & CF_CONST) {
2732 switch (flags & CF_TYPE) {
2735 if (flags & CF_FORCECHAR) {
2736 if ((val & 0xFF) != 0) {
2737 AddCodeLine ("ora #$%02X", (unsigned char)val);
2745 if ((val & 0xFF) != 0) {
2746 AddCodeLine ("ora #$%02X", (unsigned char)val);
2748 } else if ((val & 0xFF00) == 0xFF00) {
2749 if ((val & 0xFF) != 0) {
2750 AddCodeLine ("ora #$%02X", (unsigned char)val);
2752 AddCodeLine ("ldx #$FF");
2753 } else if (val != 0) {
2754 AddCodeLine ("ora #$%02X", (unsigned char)val);
2755 AddCodeLine ("pha");
2756 AddCodeLine ("txa");
2757 AddCodeLine ("ora #$%02X", (unsigned char)(val >> 8));
2758 AddCodeLine ("tax");
2759 AddCodeLine ("pla");
2765 if ((val & 0xFF) != 0) {
2766 AddCodeLine ("ora #$%02X", (unsigned char)val);
2776 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2777 * into the normal, non-optimized stuff. Note: The standard stuff will
2778 * always work with ints.
2780 flags &= ~CF_FORCECHAR;
2781 g_push (flags & ~CF_CONST, 0);
2784 /* Use long way over the stack */
2785 oper (flags, val, ops);
2790 void g_xor (unsigned flags, unsigned long val)
2791 /* Primary = TOS ^ Primary */
2793 static const char* ops[12] = {
2794 "tosxorax", "tosxorax", "tosxoreax", "tosxoreax",
2798 /* If the right hand side is const, the lhs is not on stack but still
2799 * in the primary register.
2801 if (flags & CF_CONST) {
2803 switch (flags & CF_TYPE) {
2806 if (flags & CF_FORCECHAR) {
2807 if ((val & 0xFF) != 0) {
2808 AddCodeLine ("eor #$%02X", (unsigned char)val);
2817 AddCodeLine ("eor #$%02X", (unsigned char)val);
2819 } else if (val != 0) {
2820 if ((val & 0xFF) != 0) {
2821 AddCodeLine ("eor #$%02X", (unsigned char)val);
2823 AddCodeLine ("pha");
2824 AddCodeLine ("txa");
2825 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2826 AddCodeLine ("tax");
2827 AddCodeLine ("pla");
2834 AddCodeLine ("eor #$%02X", (unsigned char)val);
2844 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2845 * into the normal, non-optimized stuff. Note: The standard stuff will
2846 * always work with ints.
2848 flags &= ~CF_FORCECHAR;
2849 g_push (flags & ~CF_CONST, 0);
2852 /* Use long way over the stack */
2853 oper (flags, val, ops);
2858 void g_and (unsigned Flags, unsigned long Val)
2859 /* Primary = TOS & Primary */
2861 static const char* ops[12] = {
2862 "tosandax", "tosandax", "tosandeax", "tosandeax",
2865 /* If the right hand side is const, the lhs is not on stack but still
2866 * in the primary register.
2868 if (Flags & CF_CONST) {
2870 switch (Flags & CF_TYPE) {
2873 if (Flags & CF_FORCECHAR) {
2874 if ((Val & 0xFF) == 0x00) {
2875 AddCodeLine ("lda #$00");
2876 } else if ((Val & 0xFF) != 0xFF) {
2877 AddCodeLine ("and #$%02X", (unsigned char)Val);
2883 if ((Val & 0xFFFF) != 0xFFFF) {
2885 AddCodeLine ("ldx #$00");
2887 AddCodeLine ("lda #$00");
2888 } else if (Val != 0xFF) {
2889 AddCodeLine ("and #$%02X", (unsigned char)Val);
2891 } else if ((Val & 0xFFFF) == 0xFF00) {
2892 AddCodeLine ("lda #$00");
2893 } else if ((Val & 0xFF00) == 0xFF00) {
2894 AddCodeLine ("and #$%02X", (unsigned char)Val);
2895 } else if ((Val & 0x00FF) == 0x0000) {
2896 AddCodeLine ("txa");
2897 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2898 AddCodeLine ("tax");
2899 AddCodeLine ("lda #$00");
2901 AddCodeLine ("tay");
2902 AddCodeLine ("txa");
2903 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2904 AddCodeLine ("tax");
2905 AddCodeLine ("tya");
2906 if ((Val & 0x00FF) == 0x0000) {
2907 AddCodeLine ("lda #$00");
2908 } else if ((Val & 0x00FF) != 0x00FF) {
2909 AddCodeLine ("and #$%02X", (unsigned char)Val);
2917 AddCodeLine ("ldx #$00");
2918 AddCodeLine ("stx sreg+1");
2919 AddCodeLine ("stx sreg");
2920 if ((Val & 0xFF) != 0xFF) {
2921 AddCodeLine ("and #$%02X", (unsigned char)Val);
2924 } else if (Val == 0xFF00) {
2925 AddCodeLine ("lda #$00");
2926 AddCodeLine ("sta sreg+1");
2927 AddCodeLine ("sta sreg");
2936 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2937 * into the normal, non-optimized stuff. Note: The standard stuff will
2938 * always work with ints.
2940 Flags &= ~CF_FORCECHAR;
2941 g_push (Flags & ~CF_CONST, 0);
2944 /* Use long way over the stack */
2945 oper (Flags, Val, ops);
2950 void g_asr (unsigned flags, unsigned long val)
2951 /* Primary = TOS >> Primary */
2953 static const char* ops[12] = {
2954 "tosasrax", "tosshrax", "tosasreax", "tosshreax",
2957 /* If the right hand side is const, the lhs is not on stack but still
2958 * in the primary register.
2960 if (flags & CF_CONST) {
2962 switch (flags & CF_TYPE) {
2968 if (flags & CF_UNSIGNED) {
2969 AddCodeLine ("txa");
2970 AddCodeLine ("ldx #$00");
2972 unsigned L = GetLocalLabel();
2973 AddCodeLine ("cpx #$80"); /* Sign bit into carry */
2974 AddCodeLine ("txa");
2975 AddCodeLine ("ldx #$00");
2976 AddCodeLine ("bcc %s", LocalLabelName (L));
2977 AddCodeLine ("dex"); /* Make $FF */
2983 if (flags & CF_UNSIGNED) {
2984 AddCodeLine ("jsr shrax4");
2986 AddCodeLine ("jsr asrax4");
2991 if (flags & CF_UNSIGNED) {
2992 AddCodeLine ("jsr shrax%ld", val);
2994 AddCodeLine ("jsr asrax%ld", val);
3002 AddCodeLine ("ldx #$00");
3003 AddCodeLine ("lda sreg+1");
3004 if ((flags & CF_UNSIGNED) == 0) {
3005 unsigned L = GetLocalLabel();
3006 AddCodeLine ("bpl %s", LocalLabelName (L));
3007 AddCodeLine ("dex");
3010 AddCodeLine ("stx sreg");
3011 AddCodeLine ("stx sreg+1");
3015 AddCodeLine ("ldy #$00");
3016 AddCodeLine ("ldx sreg+1");
3017 if ((flags & CF_UNSIGNED) == 0) {
3018 unsigned L = GetLocalLabel();
3019 AddCodeLine ("bpl %s", LocalLabelName (L));
3020 AddCodeLine ("dey");
3023 AddCodeLine ("lda sreg");
3024 AddCodeLine ("sty sreg+1");
3025 AddCodeLine ("sty sreg");
3029 AddCodeLine ("txa");
3030 AddCodeLine ("ldx sreg");
3031 AddCodeLine ("ldy sreg+1");
3032 AddCodeLine ("sty sreg");
3033 if ((flags & CF_UNSIGNED) == 0) {
3034 unsigned L = GetLocalLabel();
3035 AddCodeLine ("cpy #$80");
3036 AddCodeLine ("ldy #$00");
3037 AddCodeLine ("bcc %s", LocalLabelName (L));
3038 AddCodeLine ("dey");
3041 AddCodeLine ("ldy #$00");
3043 AddCodeLine ("sty sreg+1");
3047 if (flags & CF_UNSIGNED) {
3048 AddCodeLine ("jsr shreax4");
3050 AddCodeLine ("jsr asreax4");
3055 if (flags & CF_UNSIGNED) {
3056 AddCodeLine ("jsr shreax%ld", val);
3058 AddCodeLine ("jsr asreax%ld", val);
3067 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3068 * into the normal, non-optimized stuff. Note: The standard stuff will
3069 * always work with ints.
3071 flags &= ~CF_FORCECHAR;
3072 g_push (flags & ~CF_CONST, 0);
3075 /* Use long way over the stack */
3076 oper (flags, val, ops);
3081 void g_asl (unsigned flags, unsigned long val)
3082 /* Primary = TOS << Primary */
3084 static const char* ops[12] = {
3085 "tosaslax", "tosshlax", "tosasleax", "tosshleax",
3089 /* If the right hand side is const, the lhs is not on stack but still
3090 * in the primary register.
3092 if (flags & CF_CONST) {
3094 switch (flags & CF_TYPE) {
3100 AddCodeLine ("tax");
3101 AddCodeLine ("lda #$00");
3105 if (flags & CF_UNSIGNED) {
3106 AddCodeLine ("jsr shlax4");
3108 AddCodeLine ("jsr aslax4");
3113 if (flags & CF_UNSIGNED) {
3114 AddCodeLine ("jsr shlax%ld", val);
3116 AddCodeLine ("jsr aslax%ld", val);
3124 AddCodeLine ("sta sreg+1");
3125 AddCodeLine ("lda #$00");
3126 AddCodeLine ("tax");
3127 AddCodeLine ("sta sreg");
3131 AddCodeLine ("stx sreg+1");
3132 AddCodeLine ("sta sreg");
3133 AddCodeLine ("lda #$00");
3134 AddCodeLine ("tax");
3138 AddCodeLine ("ldy sreg");
3139 AddCodeLine ("sty sreg+1");
3140 AddCodeLine ("stx sreg");
3141 AddCodeLine ("tax");
3142 AddCodeLine ("lda #$00");
3146 if (flags & CF_UNSIGNED) {
3147 AddCodeLine ("jsr shleax4");
3149 AddCodeLine ("jsr asleax4");
3154 if (flags & CF_UNSIGNED) {
3155 AddCodeLine ("jsr shleax%ld", val);
3157 AddCodeLine ("jsr asleax%ld", val);
3166 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3167 * into the normal, non-optimized stuff. Note: The standard stuff will
3168 * always work with ints.
3170 flags &= ~CF_FORCECHAR;
3171 g_push (flags & ~CF_CONST, 0);
3174 /* Use long way over the stack */
3175 oper (flags, val, ops);
3180 void g_neg (unsigned Flags)
3181 /* Primary = -Primary */
3183 switch (Flags & CF_TYPE) {
3186 if (Flags & CF_FORCECHAR) {
3187 AddCodeLine ("eor #$FF");
3188 AddCodeLine ("clc");
3189 AddCodeLine ("adc #$01");
3195 AddCodeLine ("jsr negax");
3199 AddCodeLine ("jsr negeax");
3209 void g_bneg (unsigned flags)
3210 /* Primary = !Primary */
3212 switch (flags & CF_TYPE) {
3215 AddCodeLine ("jsr bnega");
3219 AddCodeLine ("jsr bnegax");
3223 AddCodeLine ("jsr bnegeax");
3233 void g_com (unsigned Flags)
3234 /* Primary = ~Primary */
3236 switch (Flags & CF_TYPE) {
3239 if (Flags & CF_FORCECHAR) {
3240 AddCodeLine ("eor #$FF");
3246 AddCodeLine ("jsr complax");
3250 AddCodeLine ("jsr compleax");
3260 void g_inc (unsigned flags, unsigned long val)
3261 /* Increment the primary register by a given number */
3263 /* Don't inc by zero */
3268 /* Generate code for the supported types */
3270 switch (flags & CF_TYPE) {
3273 if (flags & CF_FORCECHAR) {
3274 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3276 AddCodeLine ("ina");
3279 AddCodeLine ("clc");
3280 AddCodeLine ("adc #$%02X", (unsigned char)val);
3287 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) {
3288 unsigned L = GetLocalLabel();
3289 AddCodeLine ("ina");
3290 AddCodeLine ("bne %s", LocalLabelName (L));
3291 AddCodeLine ("inx");
3293 } else if (IS_Get (&CodeSizeFactor) < 200) {
3296 AddCodeLine ("jsr incax%lu", val);
3297 } else if (val <= 255) {
3298 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3299 AddCodeLine ("jsr incaxy");
3301 g_add (flags | CF_CONST, val);
3304 /* Inline the code */
3306 if ((val & 0xFF) != 0) {
3307 unsigned L = GetLocalLabel();
3308 AddCodeLine ("clc");
3309 AddCodeLine ("adc #$%02X", (unsigned char) val);
3310 AddCodeLine ("bcc %s", LocalLabelName (L));
3311 AddCodeLine ("inx");
3315 AddCodeLine ("inx");
3318 AddCodeLine ("inx");
3321 AddCodeLine ("inx");
3323 } else if ((val & 0xFF) != 0) {
3324 AddCodeLine ("clc");
3325 AddCodeLine ("adc #$%02X", (unsigned char) val);
3326 AddCodeLine ("pha");
3327 AddCodeLine ("txa");
3328 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3329 AddCodeLine ("tax");
3330 AddCodeLine ("pla");
3332 AddCodeLine ("pha");
3333 AddCodeLine ("txa");
3334 AddCodeLine ("clc");
3335 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3336 AddCodeLine ("tax");
3337 AddCodeLine ("pla");
3344 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3345 AddCodeLine ("jsr inceaxy");
3347 g_add (flags | CF_CONST, val);
3359 void g_dec (unsigned flags, unsigned long val)
3360 /* Decrement the primary register by a given number */
3362 /* Don't dec by zero */
3367 /* Generate code for the supported types */
3369 switch (flags & CF_TYPE) {
3372 if (flags & CF_FORCECHAR) {
3373 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3375 AddCodeLine ("dea");
3378 AddCodeLine ("sec");
3379 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3386 if (IS_Get (&CodeSizeFactor) < 200) {
3387 /* Use subroutines */
3389 AddCodeLine ("jsr decax%d", (int) val);
3390 } else if (val <= 255) {
3391 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3392 AddCodeLine ("jsr decaxy");
3394 g_sub (flags | CF_CONST, val);
3397 /* Inline the code */
3399 if ((val & 0xFF) != 0) {
3400 unsigned L = GetLocalLabel();
3401 AddCodeLine ("sec");
3402 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3403 AddCodeLine ("bcs %s", LocalLabelName (L));
3404 AddCodeLine ("dex");
3408 AddCodeLine ("dex");
3411 AddCodeLine ("dex");
3414 if ((val & 0xFF) != 0) {
3415 AddCodeLine ("sec");
3416 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3417 AddCodeLine ("pha");
3418 AddCodeLine ("txa");
3419 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3420 AddCodeLine ("tax");
3421 AddCodeLine ("pla");
3423 AddCodeLine ("pha");
3424 AddCodeLine ("txa");
3425 AddCodeLine ("sec");
3426 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3427 AddCodeLine ("tax");
3428 AddCodeLine ("pla");
3436 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3437 AddCodeLine ("jsr deceaxy");
3439 g_sub (flags | CF_CONST, val);
3452 * Following are the conditional operators. They compare the TOS against
3453 * the primary and put a literal 1 in the primary if the condition is
3454 * true, otherwise they clear the primary register
3459 void g_eq (unsigned flags, unsigned long val)
3460 /* Test for equal */
3462 static const char* ops[12] = {
3463 "toseqax", "toseqax", "toseqeax", "toseqeax",
3468 /* If the right hand side is const, the lhs is not on stack but still
3469 * in the primary register.
3471 if (flags & CF_CONST) {
3473 switch (flags & CF_TYPE) {
3476 if (flags & CF_FORCECHAR) {
3477 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3478 AddCodeLine ("jsr booleq");
3484 L = GetLocalLabel();
3485 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3486 AddCodeLine ("bne %s", LocalLabelName (L));
3487 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3489 AddCodeLine ("jsr booleq");
3499 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3500 * into the normal, non-optimized stuff. Note: The standard stuff will
3501 * always work with ints.
3503 flags &= ~CF_FORCECHAR;
3504 g_push (flags & ~CF_CONST, 0);
3507 /* Use long way over the stack */
3508 oper (flags, val, ops);
3513 void g_ne (unsigned flags, unsigned long val)
3514 /* Test for not equal */
3516 static const char* ops[12] = {
3517 "tosneax", "tosneax", "tosneeax", "tosneeax",
3522 /* If the right hand side is const, the lhs is not on stack but still
3523 * in the primary register.
3525 if (flags & CF_CONST) {
3527 switch (flags & CF_TYPE) {
3530 if (flags & CF_FORCECHAR) {
3531 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3532 AddCodeLine ("jsr boolne");
3538 L = GetLocalLabel();
3539 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3540 AddCodeLine ("bne %s", LocalLabelName (L));
3541 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3543 AddCodeLine ("jsr boolne");
3553 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3554 * into the normal, non-optimized stuff. Note: The standard stuff will
3555 * always work with ints.
3557 flags &= ~CF_FORCECHAR;
3558 g_push (flags & ~CF_CONST, 0);
3561 /* Use long way over the stack */
3562 oper (flags, val, ops);
3567 void g_lt (unsigned flags, unsigned long val)
3568 /* Test for less than */
3570 static const char* ops[12] = {
3571 "tosltax", "tosultax", "toslteax", "tosulteax",
3576 /* If the right hand side is const, the lhs is not on stack but still
3577 * in the primary register.
3579 if (flags & CF_CONST) {
3581 /* Because the handling of the overflow flag is too complex for
3582 * inlining, we can handle only unsigned compares, and signed
3583 * compares against zero here.
3585 if (flags & CF_UNSIGNED) {
3587 /* Give a warning in some special cases */
3589 Warning ("Condition is never true");
3590 AddCodeLine ("jsr return0");
3594 /* Look at the type */
3595 switch (flags & CF_TYPE) {
3598 if (flags & CF_FORCECHAR) {
3599 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3600 AddCodeLine ("jsr boolult");
3606 /* If the low byte is zero, we must only test the high byte */
3607 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3608 if ((val & 0xFF) != 0) {
3609 unsigned L = GetLocalLabel();
3610 AddCodeLine ("bne %s", LocalLabelName (L));
3611 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3614 AddCodeLine ("jsr boolult");
3618 /* Do a subtraction */
3619 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3620 AddCodeLine ("txa");
3621 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3622 AddCodeLine ("lda sreg");
3623 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3624 AddCodeLine ("lda sreg+1");
3625 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3626 AddCodeLine ("jsr boolult");
3633 } else if (val == 0) {
3635 /* A signed compare against zero must only look at the sign bit */
3636 switch (flags & CF_TYPE) {
3639 if (flags & CF_FORCECHAR) {
3640 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3641 AddCodeLine ("lda #$00");
3642 AddCodeLine ("ldx #$00");
3643 AddCodeLine ("rol a");
3649 /* Just check the high byte */
3650 AddCodeLine ("cpx #$80"); /* Bit 7 -> carry */
3651 AddCodeLine ("lda #$00");
3652 AddCodeLine ("ldx #$00");
3653 AddCodeLine ("rol a");
3657 /* Just check the high byte */
3658 AddCodeLine ("lda sreg+1");
3659 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3660 AddCodeLine ("lda #$00");
3661 AddCodeLine ("ldx #$00");
3662 AddCodeLine ("rol a");
3671 /* Signed compare against a constant != zero */
3672 switch (flags & CF_TYPE) {
3675 if (flags & CF_FORCECHAR) {
3676 Label = GetLocalLabel ();
3677 AddCodeLine ("sec");
3678 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3679 AddCodeLine ("bvc %s", LocalLabelName (Label));
3680 AddCodeLine ("eor #$80");
3681 g_defcodelabel (Label);
3682 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3683 AddCodeLine ("lda #$00");
3684 AddCodeLine ("ldx #$00");
3685 AddCodeLine ("rol a");
3691 /* Do a subtraction */
3692 Label = GetLocalLabel ();
3693 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3694 AddCodeLine ("txa");
3695 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3696 AddCodeLine ("bvc %s", LocalLabelName (Label));
3697 AddCodeLine ("eor #$80");
3698 g_defcodelabel (Label);
3699 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3700 AddCodeLine ("lda #$00");
3701 AddCodeLine ("ldx #$00");
3702 AddCodeLine ("rol a");
3706 /* This one is too costly */
3715 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3716 * into the normal, non-optimized stuff. Note: The standard stuff will
3717 * always work with ints.
3719 flags &= ~CF_FORCECHAR;
3720 g_push (flags & ~CF_CONST, 0);
3723 /* Use long way over the stack */
3724 oper (flags, val, ops);
3729 void g_le (unsigned flags, unsigned long val)
3730 /* Test for less than or equal to */
3732 static const char* ops[12] = {
3733 "tosleax", "tosuleax", "tosleeax", "tosuleeax",
3737 /* If the right hand side is const, the lhs is not on stack but still
3738 * in the primary register.
3740 if (flags & CF_CONST) {
3742 /* Look at the type */
3743 switch (flags & CF_TYPE) {
3746 if (flags & CF_FORCECHAR) {
3747 if (flags & CF_UNSIGNED) {
3748 /* Unsigned compare */
3750 /* Use < instead of <= because the former gives
3751 * better code on the 6502 than the latter.
3753 g_lt (flags, val+1);
3756 Warning ("Condition is always true");
3757 AddCodeLine ("jsr return1");
3760 /* Signed compare */
3761 if ((long) val < 0x7F) {
3762 /* Use < instead of <= because the former gives
3763 * better code on the 6502 than the latter.
3765 g_lt (flags, val+1);
3768 Warning ("Condition is always true");
3769 AddCodeLine ("jsr return1");
3777 if (flags & CF_UNSIGNED) {
3778 /* Unsigned compare */
3780 /* Use < instead of <= because the former gives
3781 * better code on the 6502 than the latter.
3783 g_lt (flags, val+1);
3786 Warning ("Condition is always true");
3787 AddCodeLine ("jsr return1");
3790 /* Signed compare */
3791 if ((long) val < 0x7FFF) {
3792 g_lt (flags, val+1);
3795 Warning ("Condition is always true");
3796 AddCodeLine ("jsr return1");
3802 if (flags & CF_UNSIGNED) {
3803 /* Unsigned compare */
3804 if (val < 0xFFFFFFFF) {
3805 /* Use < instead of <= because the former gives
3806 * better code on the 6502 than the latter.
3808 g_lt (flags, val+1);
3811 Warning ("Condition is always true");
3812 AddCodeLine ("jsr return1");
3815 /* Signed compare */
3816 if ((long) val < 0x7FFFFFFF) {
3817 g_lt (flags, val+1);
3820 Warning ("Condition is always true");
3821 AddCodeLine ("jsr return1");
3830 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3831 * into the normal, non-optimized stuff. Note: The standard stuff will
3832 * always work with ints.
3834 flags &= ~CF_FORCECHAR;
3835 g_push (flags & ~CF_CONST, 0);
3838 /* Use long way over the stack */
3839 oper (flags, val, ops);
3844 void g_gt (unsigned flags, unsigned long val)
3845 /* Test for greater than */
3847 static const char* ops[12] = {
3848 "tosgtax", "tosugtax", "tosgteax", "tosugteax",
3852 /* If the right hand side is const, the lhs is not on stack but still
3853 * in the primary register.
3855 if (flags & CF_CONST) {
3857 /* Look at the type */
3858 switch (flags & CF_TYPE) {
3861 if (flags & CF_FORCECHAR) {
3862 if (flags & CF_UNSIGNED) {
3864 /* If we have a compare > 0, we will replace it by
3865 * != 0 here, since both are identical but the
3866 * latter is easier to optimize.
3869 } else if (val < 0xFF) {
3870 /* Use >= instead of > because the former gives
3871 * better code on the 6502 than the latter.
3873 g_ge (flags, val+1);
3876 Warning ("Condition is never true");
3877 AddCodeLine ("jsr return0");
3880 if ((long) val < 0x7F) {
3881 /* Use >= instead of > because the former gives
3882 * better code on the 6502 than the latter.
3884 g_ge (flags, val+1);
3887 Warning ("Condition is never true");
3888 AddCodeLine ("jsr return0");
3896 if (flags & CF_UNSIGNED) {
3897 /* Unsigned compare */
3899 /* If we have a compare > 0, we will replace it by
3900 * != 0 here, since both are identical but the latter
3901 * is easier to optimize.
3904 } else if (val < 0xFFFF) {
3905 /* Use >= instead of > because the former gives better
3906 * code on the 6502 than the latter.
3908 g_ge (flags, val+1);
3911 Warning ("Condition is never true");
3912 AddCodeLine ("jsr return0");
3915 /* Signed compare */
3916 if ((long) val < 0x7FFF) {
3917 g_ge (flags, val+1);
3920 Warning ("Condition is never true");
3921 AddCodeLine ("jsr return0");
3927 if (flags & CF_UNSIGNED) {
3928 /* Unsigned compare */
3930 /* If we have a compare > 0, we will replace it by
3931 * != 0 here, since both are identical but the latter
3932 * is easier to optimize.
3935 } else if (val < 0xFFFFFFFF) {
3936 /* Use >= instead of > because the former gives better
3937 * code on the 6502 than the latter.
3939 g_ge (flags, val+1);
3942 Warning ("Condition is never true");
3943 AddCodeLine ("jsr return0");
3946 /* Signed compare */
3947 if ((long) val < 0x7FFFFFFF) {
3948 g_ge (flags, val+1);
3951 Warning ("Condition is never true");
3952 AddCodeLine ("jsr return0");
3961 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3962 * into the normal, non-optimized stuff. Note: The standard stuff will
3963 * always work with ints.
3965 flags &= ~CF_FORCECHAR;
3966 g_push (flags & ~CF_CONST, 0);
3969 /* Use long way over the stack */
3970 oper (flags, val, ops);
3975 void g_ge (unsigned flags, unsigned long val)
3976 /* Test for greater than or equal to */
3978 static const char* ops[12] = {
3979 "tosgeax", "tosugeax", "tosgeeax", "tosugeeax",
3985 /* If the right hand side is const, the lhs is not on stack but still
3986 * in the primary register.
3988 if (flags & CF_CONST) {
3990 /* Because the handling of the overflow flag is too complex for
3991 * inlining, we can handle only unsigned compares, and signed
3992 * compares against zero here.
3994 if (flags & CF_UNSIGNED) {
3996 /* Give a warning in some special cases */
3998 Warning ("Condition is always true");
3999 AddCodeLine ("jsr return1");
4003 /* Look at the type */
4004 switch (flags & CF_TYPE) {
4007 if (flags & CF_FORCECHAR) {
4008 /* Do a subtraction. Condition is true if carry set */
4009 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4010 AddCodeLine ("lda #$00");
4011 AddCodeLine ("ldx #$00");
4012 AddCodeLine ("rol a");
4018 /* Do a subtraction. Condition is true if carry set */
4019 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4020 AddCodeLine ("txa");
4021 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4022 AddCodeLine ("lda #$00");
4023 AddCodeLine ("ldx #$00");
4024 AddCodeLine ("rol a");
4028 /* Do a subtraction. Condition is true if carry set */
4029 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4030 AddCodeLine ("txa");
4031 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4032 AddCodeLine ("lda sreg");
4033 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
4034 AddCodeLine ("lda sreg+1");
4035 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
4036 AddCodeLine ("lda #$00");
4037 AddCodeLine ("ldx #$00");
4038 AddCodeLine ("rol a");
4045 } else if (val == 0) {
4047 /* A signed compare against zero must only look at the sign bit */
4048 switch (flags & CF_TYPE) {
4051 if (flags & CF_FORCECHAR) {
4052 AddCodeLine ("tax");
4053 AddCodeLine ("jsr boolge");
4059 /* Just test the high byte */
4060 AddCodeLine ("txa");
4061 AddCodeLine ("jsr boolge");
4065 /* Just test the high byte */
4066 AddCodeLine ("lda sreg+1");
4067 AddCodeLine ("jsr boolge");
4076 /* Signed compare against a constant != zero */
4077 switch (flags & CF_TYPE) {
4080 if (flags & CF_FORCECHAR) {
4081 Label = GetLocalLabel ();
4082 AddCodeLine ("sec");
4083 AddCodeLine ("sbc #$%02X", (unsigned char)val);
4084 AddCodeLine ("bvs %s", LocalLabelName (Label));
4085 AddCodeLine ("eor #$80");
4086 g_defcodelabel (Label);
4087 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4088 AddCodeLine ("lda #$00");
4089 AddCodeLine ("ldx #$00");
4090 AddCodeLine ("rol a");
4096 /* Do a subtraction */
4097 Label = GetLocalLabel ();
4098 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4099 AddCodeLine ("txa");
4100 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4101 AddCodeLine ("bvs %s", LocalLabelName (Label));
4102 AddCodeLine ("eor #$80");
4103 g_defcodelabel (Label);
4104 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4105 AddCodeLine ("lda #$00");
4106 AddCodeLine ("ldx #$00");
4107 AddCodeLine ("rol a");
4111 /* This one is too costly */
4119 /* If we go here, we didn't emit code. Push the lhs on stack and fall
4120 * into the normal, non-optimized stuff. Note: The standard stuff will
4121 * always work with ints.
4123 flags &= ~CF_FORCECHAR;
4124 g_push (flags & ~CF_CONST, 0);
4127 /* Use long way over the stack */
4128 oper (flags, val, ops);
4133 /*****************************************************************************/
4134 /* Allocating static storage */
4135 /*****************************************************************************/
4139 void g_res (unsigned n)
4140 /* Reserve static storage, n bytes */
4142 AddDataLine ("\t.res\t%u,$00", n);
4147 void g_defdata (unsigned flags, unsigned long val, long offs)
4148 /* Define data with the size given in flags */
4150 if (flags & CF_CONST) {
4152 /* Numeric constant */
4153 switch (flags & CF_TYPE) {
4156 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
4160 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
4164 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
4175 /* Create the correct label name */
4176 const char* Label = GetLabelName (flags, val, offs);
4178 /* Labels are always 16 bit */
4179 AddDataLine ("\t.addr\t%s", Label);
4186 void g_defbytes (const void* Bytes, unsigned Count)
4187 /* Output a row of bytes as a constant */
4193 /* Cast the buffer pointer */
4194 const unsigned char* Data = (const unsigned char*) Bytes;
4196 /* Output the stuff */
4199 /* How many go into this line? */
4200 if ((Chunk = Count) > 16) {
4205 /* Output one line */
4206 strcpy (Buf, "\t.byte\t");
4209 B += sprintf (B, "$%02X", *Data++);
4215 /* Output the line */
4216 AddDataLine ("%s", Buf);
4222 void g_zerobytes (unsigned Count)
4223 /* Output Count bytes of data initialized with zero */
4226 AddDataLine ("\t.res\t%u,$00", Count);
4232 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4233 /* Initialize a register variable from static initialization data */
4235 /* Register variables do always have less than 128 bytes */
4236 unsigned CodeLabel = GetLocalLabel ();
4237 AddCodeLine ("ldx #$%02X", (unsigned char) (Size - 1));
4238 g_defcodelabel (CodeLabel);
4239 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4240 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4241 AddCodeLine ("dex");
4242 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4247 void g_initauto (unsigned Label, unsigned Size)
4248 /* Initialize a local variable at stack offset zero from static data */
4250 unsigned CodeLabel = GetLocalLabel ();
4252 CheckLocalOffs (Size);
4254 AddCodeLine ("ldy #$%02X", Size-1);
4255 g_defcodelabel (CodeLabel);
4256 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4257 AddCodeLine ("sta (sp),y");
4258 AddCodeLine ("dey");
4259 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4260 } else if (Size <= 256) {
4261 AddCodeLine ("ldy #$00");
4262 g_defcodelabel (CodeLabel);
4263 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4264 AddCodeLine ("sta (sp),y");
4265 AddCodeLine ("iny");
4266 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4267 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4273 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4274 /* Initialize a static local variable from static initialization data */
4277 unsigned CodeLabel = GetLocalLabel ();
4278 AddCodeLine ("ldy #$%02X", Size-1);
4279 g_defcodelabel (CodeLabel);
4280 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4281 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4282 AddCodeLine ("dey");
4283 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4284 } else if (Size <= 256) {
4285 unsigned CodeLabel = GetLocalLabel ();
4286 AddCodeLine ("ldy #$00");
4287 g_defcodelabel (CodeLabel);
4288 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4289 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4290 AddCodeLine ("iny");
4291 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4292 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4294 /* Use the easy way here: memcpy */
4295 g_getimmed (CF_STATIC, VarLabel, 0);
4296 AddCodeLine ("jsr pushax");
4297 g_getimmed (CF_STATIC, InitLabel, 0);
4298 AddCodeLine ("jsr pushax");
4299 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4300 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4306 /*****************************************************************************/
4307 /* Switch statement */
4308 /*****************************************************************************/
4312 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4313 /* Generate code for a switch statement */
4315 unsigned NextLabel = 0;
4318 /* Setup registers and determine which compare insn to use */
4319 const char* Compare;
4322 Compare = "cmp #$%02X";
4325 Compare = "cpx #$%02X";
4328 AddCodeLine ("ldy sreg");
4329 Compare = "cpy #$%02X";
4332 AddCodeLine ("ldy sreg+1");
4333 Compare = "cpy #$%02X";
4336 Internal ("Invalid depth in g_switch: %u", Depth);
4339 /* Walk over all nodes */
4340 for (I = 0; I < CollCount (Nodes); ++I) {
4342 /* Get the next case node */
4343 CaseNode* N = CollAtUnchecked (Nodes, I);
4345 /* If we have a next label, define it */
4347 g_defcodelabel (NextLabel);
4351 /* Do the compare */
4352 AddCodeLine (Compare, CN_GetValue (N));
4354 /* If this is the last level, jump directly to the case code if found */
4357 /* Branch if equal */
4358 g_falsejump (0, CN_GetLabel (N));
4362 /* Determine the next label */
4363 if (I == CollCount (Nodes) - 1) {
4364 /* Last node means not found */
4365 g_truejump (0, DefaultLabel);
4367 /* Jump to the next check */
4368 NextLabel = GetLocalLabel ();
4369 g_truejump (0, NextLabel);
4372 /* Check the next level */
4373 g_switch (N->Nodes, DefaultLabel, Depth-1);
4378 /* If we go here, we haven't found the label */
4379 g_jump (DefaultLabel);
4384 /*****************************************************************************/
4385 /* User supplied assembler code */
4386 /*****************************************************************************/
4390 void g_asmcode (struct StrBuf* B)
4391 /* Output one line of assembler code. */
4393 AddCodeLine ("%.*s", (int) SB_GetLen (B), SB_GetConstBuf (B));