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 %u.%u.%u",
155 VER_MAJOR, VER_MINOR, VER_PATCH);
158 /* Insert some object file options */
159 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
160 VER_MAJOR, VER_MINOR, VER_PATCH);
162 /* If we're producing code for some other CPU, switch the command set */
164 case CPU_6502: AddTextLine ("\t.setcpu\t\t\"6502\""); break;
165 case CPU_6502X: AddTextLine ("\t.setcpu\t\t\"6502X\""); break;
166 case CPU_65SC02: AddTextLine ("\t.setcpu\t\t\"65SC02\""); break;
167 case CPU_65C02: AddTextLine ("\t.setcpu\t\t\"65C02\""); break;
168 case CPU_65816: AddTextLine ("\t.setcpu\t\t\"65816\""); break;
169 case CPU_HUC6280: AddTextLine ("\t.setcpu\t\t\"HUC6280\""); break;
170 default: Internal ("Unknown CPU: %d", CPU);
174 AddTextLine ("\t.smart\t\ton");
176 /* Allow auto import for runtime library routines */
177 AddTextLine ("\t.autoimport\ton");
179 /* Switch the assembler into case sensitive mode */
180 AddTextLine ("\t.case\t\ton");
182 /* Tell the assembler if we want to generate debug info */
183 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
185 /* Import the stack pointer for direct auto variable access */
186 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
188 /* Define long branch macros */
189 AddTextLine ("\t.macpack\tlongbranch");
194 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
195 /* If debug info is enabled, place a file info into the source */
198 /* We have to place this into the global text segment, so it will
199 * appear before all .dbg line statements.
201 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
207 /*****************************************************************************/
208 /* Segment support */
209 /*****************************************************************************/
213 void g_userodata (void)
214 /* Switch to the read only data segment */
216 UseDataSeg (SEG_RODATA);
221 void g_usedata (void)
222 /* Switch to the data segment */
224 UseDataSeg (SEG_DATA);
230 /* Switch to the bss segment */
232 UseDataSeg (SEG_BSS);
237 void g_segname (segment_t Seg)
238 /* Emit the name of a segment if necessary */
240 /* Emit a segment directive for the data style segments */
243 case SEG_RODATA: S = CS->ROData; break;
244 case SEG_DATA: S = CS->Data; break;
245 case SEG_BSS: S = CS->BSS; break;
246 default: S = 0; break;
249 DS_AddLine (S, ".segment\t\"%s\"", GetSegName (Seg));
255 /*****************************************************************************/
257 /*****************************************************************************/
261 unsigned sizeofarg (unsigned flags)
262 /* Return the size of a function argument type that is encoded in flags */
264 switch (flags & CF_TYPE) {
267 return (flags & CF_FORCECHAR)? 1 : 2;
287 int pop (unsigned flags)
288 /* Pop an argument of the given size */
290 return StackPtr += sizeofarg (flags);
295 int push (unsigned flags)
296 /* Push an argument of the given size */
298 return StackPtr -= sizeofarg (flags);
303 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
304 /* The value in Offs is an offset to an address in a/x. Make sure, an object
305 * of the type given in Flags can be loaded or stored into this address by
306 * adding part of the offset to the address in ax, so that the remaining
307 * offset fits into an index register. Return the remaining offset.
310 /* If the offset is too large for a byte register, add the high byte
311 * of the offset to the primary. Beware: We need a special correction
312 * if the offset in the low byte will overflow in the operation.
314 unsigned O = Offs & ~0xFFU;
315 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
316 /* We need to add the low byte also */
320 /* Do the correction if we need one */
322 g_inc (CF_INT | CF_CONST, O);
326 /* Return the new offset */
332 /*****************************************************************************/
333 /* Functions handling local labels */
334 /*****************************************************************************/
338 void g_defcodelabel (unsigned label)
339 /* Define a local code label */
341 CS_AddLabel (CS->Code, LocalLabelName (label));
346 void g_defdatalabel (unsigned label)
347 /* Define a local data label */
349 AddDataLine ("%s:", LocalLabelName (label));
354 /*****************************************************************************/
355 /* Functions handling global labels */
356 /*****************************************************************************/
360 void g_defgloblabel (const char* Name)
361 /* Define a global label with the given name */
363 /* Global labels are always data labels */
364 AddDataLine ("_%s:", Name);
369 void g_defexport (const char* Name, int ZP)
370 /* Export the given label */
373 AddTextLine ("\t.exportzp\t_%s", Name);
375 AddTextLine ("\t.export\t\t_%s", Name);
381 void g_defimport (const char* Name, int ZP)
382 /* Import the given label */
385 AddTextLine ("\t.importzp\t_%s", Name);
387 AddTextLine ("\t.import\t\t_%s", Name);
393 void g_importstartup (void)
394 /* Forced import of the startup module */
396 AddTextLine ("\t.forceimport\t__STARTUP__");
401 void g_importmainargs (void)
402 /* Forced import of a special symbol that handles arguments to main */
404 AddTextLine ("\t.forceimport\tinitmainargs");
409 /*****************************************************************************/
410 /* Load functions for various registers */
411 /*****************************************************************************/
415 static void ldaconst (unsigned val)
416 /* Load a with a constant */
418 AddCodeLine ("lda #$%02X", val & 0xFF);
423 static void ldxconst (unsigned val)
424 /* Load x with a constant */
426 AddCodeLine ("ldx #$%02X", val & 0xFF);
431 static void ldyconst (unsigned val)
432 /* Load y with a constant */
434 AddCodeLine ("ldy #$%02X", val & 0xFF);
439 /*****************************************************************************/
440 /* Function entry and exit */
441 /*****************************************************************************/
445 /* Remember the argument size of a function. The variable is set by g_enter
446 * and used by g_leave. If the functions gets its argument size by the caller
447 * (variable param list or function without prototype), g_enter will set the
453 void g_enter (unsigned flags, unsigned argsize)
454 /* Function prologue */
456 if ((flags & CF_FIXARGC) != 0) {
457 /* Just remember the argument size for the leave */
461 AddCodeLine ("jsr enter");
468 /* Function epilogue */
470 /* How many bytes of locals do we have to drop? */
471 unsigned k = (unsigned) -StackPtr;
473 /* If we didn't have a variable argument list, don't call leave */
476 /* Drop stackframe if needed. We can only drop 255 bytes at a time. */
479 unsigned ToDrop = (k > 255)? 255 : k;
481 AddCodeLine ("jsr incsp%d", k);
484 AddCodeLine ("jsr addysp");
492 /* Nothing to drop */
493 AddCodeLine ("jsr leave");
495 /* We've a stack frame to drop */
498 AddCodeLine ("jsr addysp");
502 AddCodeLine ("jsr leavey");
506 /* Add the final rts */
512 /*****************************************************************************/
513 /* Register variables */
514 /*****************************************************************************/
518 void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
519 /* Swap a register variable with a location on the stack */
521 /* Calculate the actual stack offset and check it */
522 StackOffs -= StackPtr;
523 CheckLocalOffs (StackOffs);
528 if (IS_Get (&CodeSizeFactor) < 165) {
529 ldyconst (StackOffs);
531 AddCodeLine ("jsr regswap1");
533 ldyconst (StackOffs);
534 AddCodeLine ("lda (sp),y");
535 AddCodeLine ("ldx regbank%+d", RegOffs);
536 AddCodeLine ("sta regbank%+d", RegOffs);
538 AddCodeLine ("sta (sp),y");
541 } else if (Bytes == 2) {
543 ldyconst (StackOffs);
545 AddCodeLine ("jsr regswap2");
549 ldyconst (StackOffs);
552 AddCodeLine ("jsr regswap");
558 void g_save_regvars (int RegOffs, unsigned Bytes)
559 /* Save register variables */
561 /* Don't loop for up to two bytes */
564 AddCodeLine ("lda regbank%+d", RegOffs);
565 AddCodeLine ("jsr pusha");
567 } else if (Bytes == 2) {
569 AddCodeLine ("lda regbank%+d", RegOffs);
570 AddCodeLine ("ldx regbank%+d", RegOffs+1);
571 AddCodeLine ("jsr pushax");
575 /* More than two bytes - loop */
576 unsigned Label = GetLocalLabel ();
578 ldyconst (Bytes - 1);
580 g_defcodelabel (Label);
581 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
582 AddCodeLine ("sta (sp),y");
585 AddCodeLine ("bne %s", LocalLabelName (Label));
589 /* We pushed stuff, correct the stack pointer */
595 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
596 /* Restore register variables */
598 /* Calculate the actual stack offset and check it */
599 StackOffs -= StackPtr;
600 CheckLocalOffs (StackOffs);
602 /* Don't loop for up to two bytes */
605 ldyconst (StackOffs);
606 AddCodeLine ("lda (sp),y");
607 AddCodeLine ("sta regbank%+d", RegOffs);
609 } else if (Bytes == 2) {
611 ldyconst (StackOffs);
612 AddCodeLine ("lda (sp),y");
613 AddCodeLine ("sta regbank%+d", RegOffs);
615 AddCodeLine ("lda (sp),y");
616 AddCodeLine ("sta regbank%+d", RegOffs+1);
618 } else if (Bytes == 3 && IS_Get (&CodeSizeFactor) >= 133) {
620 ldyconst (StackOffs);
621 AddCodeLine ("lda (sp),y");
622 AddCodeLine ("sta regbank%+d", RegOffs);
624 AddCodeLine ("lda (sp),y");
625 AddCodeLine ("sta regbank%+d", RegOffs+1);
627 AddCodeLine ("lda (sp),y");
628 AddCodeLine ("sta regbank%+d", RegOffs+2);
630 } else if (StackOffs <= RegOffs) {
632 /* More bytes, but the relation between the register offset in the
633 * register bank and the stack offset allows us to generate short
634 * code that uses just one index register.
636 unsigned Label = GetLocalLabel ();
637 ldyconst (StackOffs);
638 g_defcodelabel (Label);
639 AddCodeLine ("lda (sp),y");
640 AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs);
642 AddCodeLine ("cpy #$%02X", StackOffs + Bytes);
643 AddCodeLine ("bne %s", LocalLabelName (Label));
647 /* Ok, this is the generic code. We need to save X because the
648 * caller will only save A.
650 unsigned Label = GetLocalLabel ();
651 AddCodeLine ("stx tmp1");
652 ldyconst (StackOffs + Bytes - 1);
653 ldxconst (Bytes - 1);
654 g_defcodelabel (Label);
655 AddCodeLine ("lda (sp),y");
656 AddCodeLine ("sta regbank%+d,x", RegOffs);
659 AddCodeLine ("bpl %s", LocalLabelName (Label));
660 AddCodeLine ("ldx tmp1");
667 /*****************************************************************************/
668 /* Fetching memory cells */
669 /*****************************************************************************/
673 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
674 /* Load a constant into the primary register */
676 unsigned char B1, B2, B3, B4;
680 if ((Flags & CF_CONST) != 0) {
682 /* Numeric constant */
683 switch (Flags & CF_TYPE) {
686 if ((Flags & CF_FORCECHAR) != 0) {
692 ldxconst ((Val >> 8) & 0xFF);
693 ldaconst (Val & 0xFF);
697 /* Split the value into 4 bytes */
698 B1 = (unsigned char) (Val >> 0);
699 B2 = (unsigned char) (Val >> 8);
700 B3 = (unsigned char) (Val >> 16);
701 B4 = (unsigned char) (Val >> 24);
703 /* Remember which bytes are done */
707 AddCodeLine ("ldx #$%02X", B2);
710 AddCodeLine ("stx sreg");
714 AddCodeLine ("stx sreg+1");
717 if ((Done & 0x04) == 0 && B1 != B3) {
718 AddCodeLine ("lda #$%02X", B3);
719 AddCodeLine ("sta sreg");
722 if ((Done & 0x08) == 0 && B1 != B4) {
723 AddCodeLine ("lda #$%02X", B4);
724 AddCodeLine ("sta sreg+1");
727 AddCodeLine ("lda #$%02X", B1);
729 if ((Done & 0x04) == 0) {
731 AddCodeLine ("sta sreg");
733 if ((Done & 0x08) == 0) {
735 AddCodeLine ("sta sreg+1");
747 /* Some sort of label */
748 const char* Label = GetLabelName (Flags, Val, Offs);
750 /* Load the address into the primary */
751 AddCodeLine ("lda #<(%s)", Label);
752 AddCodeLine ("ldx #>(%s)", Label);
759 void g_getstatic (unsigned flags, unsigned long label, long offs)
760 /* Fetch an static memory cell into the primary register */
762 /* Create the correct label name */
763 const char* lbuf = GetLabelName (flags, label, offs);
765 /* Check the size and generate the correct load operation */
766 switch (flags & CF_TYPE) {
769 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
770 AddCodeLine ("lda %s", lbuf); /* load A from the label */
773 AddCodeLine ("lda %s", lbuf); /* load A from the label */
774 if (!(flags & CF_UNSIGNED)) {
775 /* Must sign extend */
776 unsigned L = GetLocalLabel ();
777 AddCodeLine ("bpl %s", LocalLabelName (L));
785 AddCodeLine ("lda %s", lbuf);
786 if (flags & CF_TEST) {
787 AddCodeLine ("ora %s+1", lbuf);
789 AddCodeLine ("ldx %s+1", lbuf);
794 if (flags & CF_TEST) {
795 AddCodeLine ("lda %s+3", lbuf);
796 AddCodeLine ("ora %s+2", lbuf);
797 AddCodeLine ("ora %s+1", lbuf);
798 AddCodeLine ("ora %s+0", lbuf);
800 AddCodeLine ("lda %s+3", lbuf);
801 AddCodeLine ("sta sreg+1");
802 AddCodeLine ("lda %s+2", lbuf);
803 AddCodeLine ("sta sreg");
804 AddCodeLine ("ldx %s+1", lbuf);
805 AddCodeLine ("lda %s", lbuf);
817 void g_getlocal (unsigned flags, int offs)
818 /* Fetch specified local object (local var). */
821 CheckLocalOffs (offs);
822 switch (flags & CF_TYPE) {
825 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
827 AddCodeLine ("lda (sp),y");
830 AddCodeLine ("ldx #$00");
831 AddCodeLine ("lda (sp),y");
832 if ((flags & CF_UNSIGNED) == 0) {
833 unsigned L = GetLocalLabel();
834 AddCodeLine ("bpl %s", LocalLabelName (L));
842 CheckLocalOffs (offs + 1);
843 if (flags & CF_TEST) {
845 AddCodeLine ("lda (sp),y");
847 AddCodeLine ("ora (sp),y");
850 AddCodeLine ("jsr ldaxysp");
856 AddCodeLine ("jsr ldeaxysp");
857 if (flags & CF_TEST) {
869 void g_getind (unsigned flags, unsigned offs)
870 /* Fetch the specified object type indirect through the primary register
871 * into the primary register
874 /* If the offset is greater than 255, add the part that is > 255 to
875 * the primary. This way we get an easy addition and use the low byte
878 offs = MakeByteOffs (flags, offs);
880 /* Handle the indirect fetch */
881 switch (flags & CF_TYPE) {
884 /* Character sized */
885 if (flags & CF_UNSIGNED) {
887 AddCodeLine ("jsr ldauidx");
890 AddCodeLine ("jsr ldaidx");
895 if (flags & CF_TEST) {
897 AddCodeLine ("sta ptr1");
898 AddCodeLine ("stx ptr1+1");
899 AddCodeLine ("lda (ptr1),y");
901 AddCodeLine ("ora (ptr1),y");
904 AddCodeLine ("jsr ldaxidx");
910 AddCodeLine ("jsr ldeaxidx");
911 if (flags & CF_TEST) {
924 void g_leasp (int offs)
925 /* Fetch the address of the specified symbol into the primary register */
927 /* Calculate the offset relative to sp */
930 /* For value 0 we do direct code */
932 AddCodeLine ("lda sp");
933 AddCodeLine ("ldx sp+1");
935 if (IS_Get (&CodeSizeFactor) < 300) {
936 ldaconst (offs); /* Load A with offset value */
937 AddCodeLine ("jsr leaasp"); /* Load effective address */
939 unsigned L = GetLocalLabel ();
940 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && offs == 1) {
941 AddCodeLine ("lda sp");
942 AddCodeLine ("ldx sp+1");
944 AddCodeLine ("bne %s", LocalLabelName (L));
948 AddCodeLine ("ldx sp+1");
950 AddCodeLine ("adc sp");
951 AddCodeLine ("bcc %s", LocalLabelName (L));
961 void g_leavariadic (int Offs)
962 /* Fetch the address of a parameter in a variadic function into the primary
966 unsigned ArgSizeOffs;
968 /* Calculate the offset relative to sp */
971 /* Get the offset of the parameter which is stored at sp+0 on function
972 * entry and check if this offset is reachable with a byte offset.
974 CHECK (StackPtr <= 0);
975 ArgSizeOffs = -StackPtr;
976 CheckLocalOffs (ArgSizeOffs);
978 /* Get the size of all parameters. */
979 ldyconst (ArgSizeOffs);
980 AddCodeLine ("lda (sp),y");
982 /* Add the value of the stackpointer */
983 if (IS_Get (&CodeSizeFactor) > 250) {
984 unsigned L = GetLocalLabel();
985 AddCodeLine ("ldx sp+1");
987 AddCodeLine ("adc sp");
988 AddCodeLine ("bcc %s", LocalLabelName (L));
992 AddCodeLine ("jsr leaasp");
995 /* Add the offset to the primary */
997 g_inc (CF_INT | CF_CONST, Offs);
998 } else if (Offs < 0) {
999 g_dec (CF_INT | CF_CONST, -Offs);
1005 /*****************************************************************************/
1006 /* Store into memory */
1007 /*****************************************************************************/
1011 void g_putstatic (unsigned flags, unsigned long label, long offs)
1012 /* Store the primary register into the specified static memory cell */
1014 /* Create the correct label name */
1015 const char* lbuf = GetLabelName (flags, label, offs);
1017 /* Check the size and generate the correct store operation */
1018 switch (flags & CF_TYPE) {
1021 AddCodeLine ("sta %s", lbuf);
1025 AddCodeLine ("sta %s", lbuf);
1026 AddCodeLine ("stx %s+1", lbuf);
1030 AddCodeLine ("sta %s", lbuf);
1031 AddCodeLine ("stx %s+1", lbuf);
1032 AddCodeLine ("ldy sreg");
1033 AddCodeLine ("sty %s+2", lbuf);
1034 AddCodeLine ("ldy sreg+1");
1035 AddCodeLine ("sty %s+3", lbuf);
1046 void g_putlocal (unsigned Flags, int Offs, long Val)
1047 /* Put data into local object. */
1050 CheckLocalOffs (Offs);
1051 switch (Flags & CF_TYPE) {
1054 if (Flags & CF_CONST) {
1055 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1058 AddCodeLine ("sta (sp),y");
1062 if (Flags & CF_CONST) {
1064 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1065 AddCodeLine ("sta (sp),y");
1066 if ((Flags & CF_NOKEEP) == 0) {
1067 /* Place high byte into X */
1068 AddCodeLine ("tax");
1070 if ((Val & 0xFF) == Offs+1) {
1071 /* The value we need is already in Y */
1072 AddCodeLine ("tya");
1073 AddCodeLine ("dey");
1075 AddCodeLine ("dey");
1076 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1078 AddCodeLine ("sta (sp),y");
1080 if ((Flags & CF_NOKEEP) == 0 || IS_Get (&CodeSizeFactor) < 160) {
1082 AddCodeLine ("jsr staxysp");
1085 AddCodeLine ("sta (sp),y");
1086 AddCodeLine ("iny");
1087 AddCodeLine ("txa");
1088 AddCodeLine ("sta (sp),y");
1094 if (Flags & CF_CONST) {
1095 g_getimmed (Flags, Val, 0);
1098 AddCodeLine ("jsr steaxysp");
1109 void g_putind (unsigned Flags, unsigned Offs)
1110 /* Store the specified object type in the primary register at the address
1111 * on the top of the stack
1114 /* We can handle offsets below $100 directly, larger offsets must be added
1115 * to the address. Since a/x is in use, best code is achieved by adding
1116 * just the high byte. Be sure to check if the low byte will overflow while
1119 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1121 /* Overflow - we need to add the low byte also */
1122 AddCodeLine ("ldy #$00");
1123 AddCodeLine ("clc");
1124 AddCodeLine ("pha");
1125 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1126 AddCodeLine ("adc (sp),y");
1127 AddCodeLine ("sta (sp),y");
1128 AddCodeLine ("iny");
1129 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1130 AddCodeLine ("adc (sp),y");
1131 AddCodeLine ("sta (sp),y");
1132 AddCodeLine ("pla");
1134 /* Complete address is on stack, new offset is zero */
1137 } else if ((Offs & 0xFF00) != 0) {
1139 /* We can just add the high byte */
1140 AddCodeLine ("ldy #$01");
1141 AddCodeLine ("clc");
1142 AddCodeLine ("pha");
1143 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1144 AddCodeLine ("adc (sp),y");
1145 AddCodeLine ("sta (sp),y");
1146 AddCodeLine ("pla");
1148 /* Offset is now just the low byte */
1152 /* Check the size and determine operation */
1153 switch (Flags & CF_TYPE) {
1157 AddCodeLine ("jsr staspidx");
1162 AddCodeLine ("jsr staxspidx");
1167 AddCodeLine ("jsr steaxspidx");
1175 /* Pop the argument which is always a pointer */
1181 /*****************************************************************************/
1182 /* type conversion and similiar stuff */
1183 /*****************************************************************************/
1187 void g_toslong (unsigned flags)
1188 /* Make sure, the value on TOS is a long. Convert if necessary */
1190 switch (flags & CF_TYPE) {
1194 if (flags & CF_UNSIGNED) {
1195 AddCodeLine ("jsr tosulong");
1197 AddCodeLine ("jsr toslong");
1212 void g_tosint (unsigned flags)
1213 /* Make sure, the value on TOS is an int. Convert if necessary */
1215 switch (flags & CF_TYPE) {
1222 AddCodeLine ("jsr tosint");
1233 void g_regint (unsigned Flags)
1234 /* Make sure, the value in the primary register an int. Convert if necessary */
1238 switch (Flags & CF_TYPE) {
1241 if (Flags & CF_FORCECHAR) {
1242 /* Conversion is from char */
1243 if (Flags & CF_UNSIGNED) {
1244 AddCodeLine ("ldx #$00");
1246 L = GetLocalLabel();
1247 AddCodeLine ("ldx #$00");
1248 AddCodeLine ("cmp #$80");
1249 AddCodeLine ("bcc %s", LocalLabelName (L));
1250 AddCodeLine ("dex");
1267 void g_reglong (unsigned Flags)
1268 /* Make sure, the value in the primary register a long. Convert if necessary */
1272 switch (Flags & CF_TYPE) {
1275 if (Flags & CF_FORCECHAR) {
1276 /* Conversion is from char */
1277 if (Flags & CF_UNSIGNED) {
1278 if (IS_Get (&CodeSizeFactor) >= 200) {
1279 AddCodeLine ("ldx #$00");
1280 AddCodeLine ("stx sreg");
1281 AddCodeLine ("stx sreg+1");
1283 AddCodeLine ("jsr aulong");
1286 if (IS_Get (&CodeSizeFactor) >= 366) {
1287 L = GetLocalLabel();
1288 AddCodeLine ("ldx #$00");
1289 AddCodeLine ("cmp #$80");
1290 AddCodeLine ("bcc %s", LocalLabelName (L));
1291 AddCodeLine ("dex");
1293 AddCodeLine ("stx sreg");
1294 AddCodeLine ("stx sreg+1");
1296 AddCodeLine ("jsr along");
1303 if (Flags & CF_UNSIGNED) {
1304 if (IS_Get (&CodeSizeFactor) >= 200) {
1306 AddCodeLine ("sty sreg");
1307 AddCodeLine ("sty sreg+1");
1309 AddCodeLine ("jsr axulong");
1312 AddCodeLine ("jsr axlong");
1326 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1327 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1328 * value, that corresponds to the value on TOS, rhs corresponds to the value
1329 * in (e)ax. The return value is the the flags value for the resulting type.
1332 unsigned ltype, rtype;
1335 /* Get the type spec from the flags */
1336 ltype = lhs & CF_TYPE;
1337 rtype = rhs & CF_TYPE;
1339 /* Check if a conversion is needed */
1340 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1341 /* We must promote the primary register to long */
1343 /* Get the new rhs type */
1344 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1346 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1347 /* We must promote the lhs to long */
1353 /* Get the new rhs type */
1354 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1358 /* Determine the result type for the operation:
1359 * - The result is const if both operands are const.
1360 * - The result is unsigned if one of the operands is unsigned.
1361 * - The result is long if one of the operands is long.
1362 * - Otherwise the result is int sized.
1364 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1365 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1366 if (rtype == CF_LONG || ltype == CF_LONG) {
1376 unsigned g_typecast (unsigned lhs, unsigned rhs)
1377 /* Cast the value in the primary register to the operand size that is flagged
1378 * by the lhs value. Return the result value.
1381 unsigned ltype, rtype;
1383 /* Get the type spec from the flags */
1384 ltype = lhs & CF_TYPE;
1385 rtype = rhs & CF_TYPE;
1387 /* Check if a conversion is needed */
1388 if ((rhs & CF_CONST) == 0) {
1389 if (ltype == CF_LONG && rtype != CF_LONG) {
1390 /* We must promote the primary register to long */
1392 } else if (ltype == CF_INT && rtype != CF_INT) {
1393 /* We must promote the primary register to int */
1398 /* Do not need any other action. If the left type is int, and the primary
1399 * register is long, it will be automagically truncated. If the right hand
1400 * side is const, it is not located in the primary register and handled by
1401 * the expression parser code.
1404 /* Result is const if the right hand side was const */
1405 lhs |= (rhs & CF_CONST);
1407 /* The resulting type is that of the left hand side (that's why you called
1415 void g_scale (unsigned flags, long val)
1416 /* Scale the value in the primary register by the given value. If val is positive,
1417 * scale up, is val is negative, scale down. This function is used to scale
1418 * the operands or results of pointer arithmetic by the size of the type, the
1419 * pointer points to.
1424 /* Value may not be zero */
1426 Internal ("Data type has no size");
1427 } else if (val > 0) {
1430 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1432 /* Factor is 2, 4, 8 and 16, use special function */
1433 switch (flags & CF_TYPE) {
1436 if (flags & CF_FORCECHAR) {
1438 AddCodeLine ("asl a");
1445 if (flags & CF_UNSIGNED) {
1446 AddCodeLine ("jsr shlax%d", p2);
1448 AddCodeLine ("jsr aslax%d", p2);
1453 if (flags & CF_UNSIGNED) {
1454 AddCodeLine ("jsr shleax%d", p2);
1456 AddCodeLine ("jsr asleax%d", p2);
1465 } else if (val != 1) {
1467 /* Use a multiplication instead */
1468 g_mul (flags | CF_CONST, val);
1476 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1478 /* Factor is 2, 4, 8 and 16 use special function */
1479 switch (flags & CF_TYPE) {
1482 if (flags & CF_FORCECHAR) {
1483 if (flags & CF_UNSIGNED) {
1485 AddCodeLine ("lsr a");
1488 } else if (p2 <= 2) {
1489 AddCodeLine ("cmp #$80");
1490 AddCodeLine ("ror a");
1497 if (flags & CF_UNSIGNED) {
1498 AddCodeLine ("jsr lsrax%d", p2);
1500 AddCodeLine ("jsr asrax%d", p2);
1505 if (flags & CF_UNSIGNED) {
1506 AddCodeLine ("jsr lsreax%d", p2);
1508 AddCodeLine ("jsr asreax%d", p2);
1517 } else if (val != 1) {
1519 /* Use a division instead */
1520 g_div (flags | CF_CONST, val);
1528 /*****************************************************************************/
1529 /* Adds and subs of variables fix a fixed address */
1530 /*****************************************************************************/
1534 void g_addlocal (unsigned flags, int offs)
1535 /* Add a local variable to ax */
1539 /* Correct the offset and check it */
1541 CheckLocalOffs (offs);
1543 switch (flags & CF_TYPE) {
1546 L = GetLocalLabel();
1547 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1548 AddCodeLine ("clc");
1549 AddCodeLine ("adc (sp),y");
1550 AddCodeLine ("bcc %s", LocalLabelName (L));
1551 AddCodeLine ("inx");
1556 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1557 AddCodeLine ("clc");
1558 AddCodeLine ("adc (sp),y");
1559 AddCodeLine ("pha");
1560 AddCodeLine ("txa");
1561 AddCodeLine ("iny");
1562 AddCodeLine ("adc (sp),y");
1563 AddCodeLine ("tax");
1564 AddCodeLine ("pla");
1568 /* Do it the old way */
1570 g_getlocal (flags, offs);
1582 void g_addstatic (unsigned flags, unsigned long label, long offs)
1583 /* Add a static variable to ax */
1587 /* Create the correct label name */
1588 const char* lbuf = GetLabelName (flags, label, offs);
1590 switch (flags & CF_TYPE) {
1593 L = GetLocalLabel();
1594 AddCodeLine ("clc");
1595 AddCodeLine ("adc %s", lbuf);
1596 AddCodeLine ("bcc %s", LocalLabelName (L));
1597 AddCodeLine ("inx");
1602 AddCodeLine ("clc");
1603 AddCodeLine ("adc %s", lbuf);
1604 AddCodeLine ("tay");
1605 AddCodeLine ("txa");
1606 AddCodeLine ("adc %s+1", lbuf);
1607 AddCodeLine ("tax");
1608 AddCodeLine ("tya");
1612 /* Do it the old way */
1614 g_getstatic (flags, label, offs);
1626 /*****************************************************************************/
1627 /* Special op= functions */
1628 /*****************************************************************************/
1632 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1634 /* Emit += for a static variable */
1636 /* Create the correct label name */
1637 const char* lbuf = GetLabelName (flags, label, offs);
1639 /* Check the size and determine operation */
1640 switch (flags & CF_TYPE) {
1643 if (flags & CF_FORCECHAR) {
1644 AddCodeLine ("ldx #$00");
1645 if (flags & CF_CONST) {
1647 AddCodeLine ("inc %s", lbuf);
1648 AddCodeLine ("lda %s", lbuf);
1650 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1651 AddCodeLine ("clc");
1652 AddCodeLine ("adc %s", lbuf);
1653 AddCodeLine ("sta %s", lbuf);
1656 AddCodeLine ("clc");
1657 AddCodeLine ("adc %s", lbuf);
1658 AddCodeLine ("sta %s", lbuf);
1660 if ((flags & CF_UNSIGNED) == 0) {
1661 unsigned L = GetLocalLabel();
1662 AddCodeLine ("bpl %s", LocalLabelName (L));
1663 AddCodeLine ("dex");
1671 if (flags & CF_CONST) {
1673 unsigned L = GetLocalLabel ();
1674 AddCodeLine ("inc %s", lbuf);
1675 AddCodeLine ("bne %s", LocalLabelName (L));
1676 AddCodeLine ("inc %s+1", lbuf);
1678 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1679 AddCodeLine ("ldx %s+1", lbuf);
1681 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1682 AddCodeLine ("clc");
1683 AddCodeLine ("adc %s", lbuf);
1684 AddCodeLine ("sta %s", lbuf);
1686 unsigned L = GetLocalLabel ();
1687 AddCodeLine ("bcc %s", LocalLabelName (L));
1688 AddCodeLine ("inc %s+1", lbuf);
1690 AddCodeLine ("ldx %s+1", lbuf);
1692 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1693 AddCodeLine ("adc %s+1", lbuf);
1694 AddCodeLine ("sta %s+1", lbuf);
1695 AddCodeLine ("tax");
1696 AddCodeLine ("lda %s", lbuf);
1700 AddCodeLine ("clc");
1701 AddCodeLine ("adc %s", lbuf);
1702 AddCodeLine ("sta %s", lbuf);
1703 AddCodeLine ("txa");
1704 AddCodeLine ("adc %s+1", lbuf);
1705 AddCodeLine ("sta %s+1", lbuf);
1706 AddCodeLine ("tax");
1707 AddCodeLine ("lda %s", lbuf);
1712 if (flags & CF_CONST) {
1714 AddCodeLine ("ldy #<(%s)", lbuf);
1715 AddCodeLine ("sty ptr1");
1716 AddCodeLine ("ldy #>(%s)", lbuf);
1718 AddCodeLine ("jsr laddeq1");
1720 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1721 AddCodeLine ("jsr laddeqa");
1724 g_getstatic (flags, label, offs);
1726 g_putstatic (flags, label, offs);
1729 AddCodeLine ("ldy #<(%s)", lbuf);
1730 AddCodeLine ("sty ptr1");
1731 AddCodeLine ("ldy #>(%s)", lbuf);
1732 AddCodeLine ("jsr laddeq");
1743 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1744 /* Emit += for a local variable */
1746 /* Calculate the true offset, check it, load it into Y */
1748 CheckLocalOffs (offs);
1750 /* Check the size and determine operation */
1751 switch (flags & CF_TYPE) {
1754 if (flags & CF_FORCECHAR) {
1756 AddCodeLine ("ldx #$00");
1757 if (flags & CF_CONST) {
1758 AddCodeLine ("clc");
1759 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1760 AddCodeLine ("adc (sp),y");
1761 AddCodeLine ("sta (sp),y");
1763 AddCodeLine ("clc");
1764 AddCodeLine ("adc (sp),y");
1765 AddCodeLine ("sta (sp),y");
1767 if ((flags & CF_UNSIGNED) == 0) {
1768 unsigned L = GetLocalLabel();
1769 AddCodeLine ("bpl %s", LocalLabelName (L));
1770 AddCodeLine ("dex");
1779 if (flags & CF_CONST) {
1780 if (IS_Get (&CodeSizeFactor) >= 400) {
1781 AddCodeLine ("clc");
1782 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1783 AddCodeLine ("adc (sp),y");
1784 AddCodeLine ("sta (sp),y");
1785 AddCodeLine ("iny");
1786 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1787 AddCodeLine ("adc (sp),y");
1788 AddCodeLine ("sta (sp),y");
1789 AddCodeLine ("tax");
1790 AddCodeLine ("dey");
1791 AddCodeLine ("lda (sp),y");
1793 g_getimmed (flags, val, 0);
1794 AddCodeLine ("jsr addeqysp");
1797 AddCodeLine ("jsr addeqysp");
1802 if (flags & CF_CONST) {
1803 g_getimmed (flags, val, 0);
1806 AddCodeLine ("jsr laddeqysp");
1816 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1817 /* Emit += for the location with address in ax */
1819 /* If the offset is too large for a byte register, add the high byte
1820 * of the offset to the primary. Beware: We need a special correction
1821 * if the offset in the low byte will overflow in the operation.
1823 offs = MakeByteOffs (flags, offs);
1825 /* Check the size and determine operation */
1826 switch (flags & CF_TYPE) {
1829 AddCodeLine ("sta ptr1");
1830 AddCodeLine ("stx ptr1+1");
1831 AddCodeLine ("ldy #$%02X", offs);
1832 AddCodeLine ("ldx #$00");
1833 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1834 AddCodeLine ("clc");
1835 AddCodeLine ("adc (ptr1),y");
1836 AddCodeLine ("sta (ptr1),y");
1840 if (IS_Get (&CodeSizeFactor) >= 200) {
1841 /* Lots of code, use only if size is not important */
1842 AddCodeLine ("sta ptr1");
1843 AddCodeLine ("stx ptr1+1");
1844 AddCodeLine ("ldy #$%02X", offs);
1845 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1846 AddCodeLine ("clc");
1847 AddCodeLine ("adc (ptr1),y");
1848 AddCodeLine ("sta (ptr1),y");
1849 AddCodeLine ("pha");
1850 AddCodeLine ("iny");
1851 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1852 AddCodeLine ("adc (ptr1),y");
1853 AddCodeLine ("sta (ptr1),y");
1854 AddCodeLine ("tax");
1855 AddCodeLine ("pla");
1861 AddCodeLine ("jsr pushax"); /* Push the address */
1862 push (CF_PTR); /* Correct the internal sp */
1863 g_getind (flags, offs); /* Fetch the value */
1864 g_inc (flags, val); /* Increment value in primary */
1865 g_putind (flags, offs); /* Store the value back */
1875 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1877 /* Emit -= for a static variable */
1879 /* Create the correct label name */
1880 const char* lbuf = GetLabelName (flags, label, offs);
1882 /* Check the size and determine operation */
1883 switch (flags & CF_TYPE) {
1886 if (flags & CF_FORCECHAR) {
1887 AddCodeLine ("ldx #$00");
1888 if (flags & CF_CONST) {
1890 AddCodeLine ("dec %s", lbuf);
1891 AddCodeLine ("lda %s", lbuf);
1893 AddCodeLine ("lda %s", lbuf);
1894 AddCodeLine ("sec");
1895 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1896 AddCodeLine ("sta %s", lbuf);
1899 AddCodeLine ("eor #$FF");
1900 AddCodeLine ("sec");
1901 AddCodeLine ("adc %s", lbuf);
1902 AddCodeLine ("sta %s", lbuf);
1904 if ((flags & CF_UNSIGNED) == 0) {
1905 unsigned L = GetLocalLabel();
1906 AddCodeLine ("bpl %s", LocalLabelName (L));
1907 AddCodeLine ("dex");
1915 if (flags & CF_CONST) {
1916 AddCodeLine ("lda %s", lbuf);
1917 AddCodeLine ("sec");
1918 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1919 AddCodeLine ("sta %s", lbuf);
1921 unsigned L = GetLocalLabel ();
1922 AddCodeLine ("bcs %s", LocalLabelName (L));
1923 AddCodeLine ("dec %s+1", lbuf);
1925 AddCodeLine ("ldx %s+1", lbuf);
1927 AddCodeLine ("lda %s+1", lbuf);
1928 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1929 AddCodeLine ("sta %s+1", lbuf);
1930 AddCodeLine ("tax");
1931 AddCodeLine ("lda %s", lbuf);
1934 AddCodeLine ("eor #$FF");
1935 AddCodeLine ("sec");
1936 AddCodeLine ("adc %s", lbuf);
1937 AddCodeLine ("sta %s", lbuf);
1938 AddCodeLine ("txa");
1939 AddCodeLine ("eor #$FF");
1940 AddCodeLine ("adc %s+1", lbuf);
1941 AddCodeLine ("sta %s+1", lbuf);
1942 AddCodeLine ("tax");
1943 AddCodeLine ("lda %s", lbuf);
1948 if (flags & CF_CONST) {
1950 AddCodeLine ("ldy #<(%s)", lbuf);
1951 AddCodeLine ("sty ptr1");
1952 AddCodeLine ("ldy #>(%s)", lbuf);
1953 AddCodeLine ("lda #$%02X", (unsigned char)val);
1954 AddCodeLine ("jsr lsubeqa");
1956 g_getstatic (flags, label, offs);
1958 g_putstatic (flags, label, offs);
1961 AddCodeLine ("ldy #<(%s)", lbuf);
1962 AddCodeLine ("sty ptr1");
1963 AddCodeLine ("ldy #>(%s)", lbuf);
1964 AddCodeLine ("jsr lsubeq");
1975 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1976 /* Emit -= for a local variable */
1978 /* Calculate the true offset, check it, load it into Y */
1980 CheckLocalOffs (offs);
1982 /* Check the size and determine operation */
1983 switch (flags & CF_TYPE) {
1986 if (flags & CF_FORCECHAR) {
1988 AddCodeLine ("ldx #$00");
1989 if (flags & CF_CONST) {
1990 AddCodeLine ("lda (sp),y");
1991 AddCodeLine ("sec");
1992 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1994 AddCodeLine ("eor #$FF");
1995 AddCodeLine ("sec");
1996 AddCodeLine ("adc (sp),y");
1998 AddCodeLine ("sta (sp),y");
1999 if ((flags & CF_UNSIGNED) == 0) {
2000 unsigned L = GetLocalLabel();
2001 AddCodeLine ("bpl %s", LocalLabelName (L));
2002 AddCodeLine ("dex");
2010 if (flags & CF_CONST) {
2011 g_getimmed (flags, val, 0);
2014 AddCodeLine ("jsr subeqysp");
2018 if (flags & CF_CONST) {
2019 g_getimmed (flags, val, 0);
2022 AddCodeLine ("jsr lsubeqysp");
2032 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2033 /* Emit -= for the location with address in ax */
2035 /* If the offset is too large for a byte register, add the high byte
2036 * of the offset to the primary. Beware: We need a special correction
2037 * if the offset in the low byte will overflow in the operation.
2039 offs = MakeByteOffs (flags, offs);
2041 /* Check the size and determine operation */
2042 switch (flags & CF_TYPE) {
2045 AddCodeLine ("sta ptr1");
2046 AddCodeLine ("stx ptr1+1");
2047 AddCodeLine ("ldy #$%02X", offs);
2048 AddCodeLine ("ldx #$00");
2049 AddCodeLine ("lda (ptr1),y");
2050 AddCodeLine ("sec");
2051 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2052 AddCodeLine ("sta (ptr1),y");
2056 if (IS_Get (&CodeSizeFactor) >= 200) {
2057 /* Lots of code, use only if size is not important */
2058 AddCodeLine ("sta ptr1");
2059 AddCodeLine ("stx ptr1+1");
2060 AddCodeLine ("ldy #$%02X", offs);
2061 AddCodeLine ("lda (ptr1),y");
2062 AddCodeLine ("sec");
2063 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2064 AddCodeLine ("sta (ptr1),y");
2065 AddCodeLine ("pha");
2066 AddCodeLine ("iny");
2067 AddCodeLine ("lda (ptr1),y");
2068 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2069 AddCodeLine ("sta (ptr1),y");
2070 AddCodeLine ("tax");
2071 AddCodeLine ("pla");
2077 AddCodeLine ("jsr pushax"); /* Push the address */
2078 push (CF_PTR); /* Correct the internal sp */
2079 g_getind (flags, offs); /* Fetch the value */
2080 g_dec (flags, val); /* Increment value in primary */
2081 g_putind (flags, offs); /* Store the value back */
2091 /*****************************************************************************/
2092 /* Add a variable address to the value in ax */
2093 /*****************************************************************************/
2097 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2098 /* Add the address of a local variable to ax */
2102 /* Add the offset */
2105 /* We cannot address more then 256 bytes of locals anyway */
2106 L = GetLocalLabel();
2107 CheckLocalOffs (offs);
2108 AddCodeLine ("clc");
2109 AddCodeLine ("adc #$%02X", offs & 0xFF);
2110 /* Do also skip the CLC insn below */
2111 AddCodeLine ("bcc %s", LocalLabelName (L));
2112 AddCodeLine ("inx");
2115 /* Add the current stackpointer value */
2116 AddCodeLine ("clc");
2118 /* Label was used above */
2121 AddCodeLine ("adc sp");
2122 AddCodeLine ("tay");
2123 AddCodeLine ("txa");
2124 AddCodeLine ("adc sp+1");
2125 AddCodeLine ("tax");
2126 AddCodeLine ("tya");
2131 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2132 /* Add the address of a static variable to ax */
2134 /* Create the correct label name */
2135 const char* lbuf = GetLabelName (flags, label, offs);
2137 /* Add the address to the current ax value */
2138 AddCodeLine ("clc");
2139 AddCodeLine ("adc #<(%s)", lbuf);
2140 AddCodeLine ("tay");
2141 AddCodeLine ("txa");
2142 AddCodeLine ("adc #>(%s)", lbuf);
2143 AddCodeLine ("tax");
2144 AddCodeLine ("tya");
2149 /*****************************************************************************/
2151 /*****************************************************************************/
2155 void g_save (unsigned flags)
2156 /* Copy primary register to hold register. */
2158 /* Check the size and determine operation */
2159 switch (flags & CF_TYPE) {
2162 if (flags & CF_FORCECHAR) {
2163 AddCodeLine ("pha");
2169 AddCodeLine ("sta regsave");
2170 AddCodeLine ("stx regsave+1");
2174 AddCodeLine ("jsr saveeax");
2184 void g_restore (unsigned flags)
2185 /* Copy hold register to primary. */
2187 /* Check the size and determine operation */
2188 switch (flags & CF_TYPE) {
2191 if (flags & CF_FORCECHAR) {
2192 AddCodeLine ("pla");
2198 AddCodeLine ("lda regsave");
2199 AddCodeLine ("ldx regsave+1");
2203 AddCodeLine ("jsr resteax");
2213 void g_cmp (unsigned flags, unsigned long val)
2214 /* Immidiate compare. The primary register will not be changed, Z flag
2220 /* Check the size and determine operation */
2221 switch (flags & CF_TYPE) {
2224 if (flags & CF_FORCECHAR) {
2225 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2231 L = GetLocalLabel();
2232 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2233 AddCodeLine ("bne %s", LocalLabelName (L));
2234 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2239 Internal ("g_cmp: Long compares not implemented");
2249 static void oper (unsigned Flags, unsigned long Val, const char** Subs)
2250 /* Encode a binary operation. subs is a pointer to four strings:
2251 * 0 --> Operate on ints
2252 * 1 --> Operate on unsigneds
2253 * 2 --> Operate on longs
2254 * 3 --> Operate on unsigned longs
2257 /* Determine the offset into the array */
2258 if (Flags & CF_UNSIGNED) {
2261 if ((Flags & CF_TYPE) == CF_LONG) {
2265 /* Load the value if it is not already in the primary */
2266 if (Flags & CF_CONST) {
2268 g_getimmed (Flags, Val, 0);
2271 /* Output the operation */
2272 AddCodeLine ("jsr %s", *Subs);
2274 /* The operation will pop it's argument */
2280 void g_test (unsigned flags)
2281 /* Test the value in the primary and set the condition codes */
2283 switch (flags & CF_TYPE) {
2286 if (flags & CF_FORCECHAR) {
2287 AddCodeLine ("tax");
2293 AddCodeLine ("stx tmp1");
2294 AddCodeLine ("ora tmp1");
2298 if (flags & CF_UNSIGNED) {
2299 AddCodeLine ("jsr utsteax");
2301 AddCodeLine ("jsr tsteax");
2313 void g_push (unsigned flags, unsigned long val)
2314 /* Push the primary register or a constant value onto the stack */
2316 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2318 /* We have a constant 8 or 16 bit value */
2319 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2321 /* Handle as 8 bit value */
2323 AddCodeLine ("jsr pusha");
2327 /* Handle as 16 bit value */
2328 g_getimmed (flags, val, 0);
2329 AddCodeLine ("jsr pushax");
2334 /* Value is not 16 bit or not constant */
2335 if (flags & CF_CONST) {
2336 /* Constant 32 bit value, load into eax */
2337 g_getimmed (flags, val, 0);
2340 /* Push the primary register */
2341 switch (flags & CF_TYPE) {
2344 if (flags & CF_FORCECHAR) {
2345 /* Handle as char */
2346 AddCodeLine ("jsr pusha");
2351 AddCodeLine ("jsr pushax");
2355 AddCodeLine ("jsr pusheax");
2365 /* Adjust the stack offset */
2371 void g_swap (unsigned flags)
2372 /* Swap the primary register and the top of the stack. flags give the type
2373 * of *both* values (must have same size).
2376 switch (flags & CF_TYPE) {
2380 AddCodeLine ("jsr swapstk");
2384 AddCodeLine ("jsr swapestk");
2395 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2396 /* Call the specified subroutine name */
2398 if ((Flags & CF_FIXARGC) == 0) {
2399 /* Pass the argument count */
2402 AddCodeLine ("jsr _%s", Label);
2403 StackPtr += ArgSize; /* callee pops args */
2408 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2409 /* Call subroutine indirect */
2411 if ((Flags & CF_LOCAL) == 0) {
2412 /* Address is in a/x */
2413 if ((Flags & CF_FIXARGC) == 0) {
2414 /* Pass arg count */
2417 AddCodeLine ("jsr callax");
2419 /* The address is on stack, offset is on Val */
2421 CheckLocalOffs (Offs);
2422 AddCodeLine ("pha");
2423 AddCodeLine ("ldy #$%02X", Offs);
2424 AddCodeLine ("lda (sp),y");
2425 AddCodeLine ("sta jmpvec+1");
2426 AddCodeLine ("iny");
2427 AddCodeLine ("lda (sp),y");
2428 AddCodeLine ("sta jmpvec+2");
2429 AddCodeLine ("pla");
2430 AddCodeLine ("jsr jmpvec");
2433 /* Callee pops args */
2434 StackPtr += ArgSize;
2439 void g_jump (unsigned Label)
2440 /* Jump to specified internal label number */
2442 AddCodeLine ("jmp %s", LocalLabelName (Label));
2447 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2448 /* Jump to label if zero flag clear */
2450 AddCodeLine ("jne %s", LocalLabelName (label));
2455 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2456 /* Jump to label if zero flag set */
2458 AddCodeLine ("jeq %s", LocalLabelName (label));
2463 static void mod_internal (int k, char* verb1, char* verb2)
2466 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2470 AddCodeLine ("jsr %ssp", verb2);
2476 void g_space (int space)
2477 /* Create or drop space on the stack */
2480 mod_internal (-space, "inc", "addy");
2481 } else if (space > 0) {
2482 mod_internal (space, "dec", "suby");
2488 void g_cstackcheck (void)
2489 /* Check for a C stack overflow */
2491 AddCodeLine ("jsr cstkchk");
2496 void g_stackcheck (void)
2497 /* Check for a stack overflow */
2499 AddCodeLine ("jsr stkchk");
2504 void g_add (unsigned flags, unsigned long val)
2505 /* Primary = TOS + Primary */
2507 static const char* ops[12] = {
2508 "tosaddax", "tosaddax", "tosaddeax", "tosaddeax"
2511 if (flags & CF_CONST) {
2512 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2513 g_push (flags & ~CF_CONST, 0);
2515 oper (flags, val, ops);
2520 void g_sub (unsigned flags, unsigned long val)
2521 /* Primary = TOS - Primary */
2523 static const char* ops[12] = {
2524 "tossubax", "tossubax", "tossubeax", "tossubeax",
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_rsub (unsigned flags, unsigned long val)
2537 /* Primary = Primary - TOS */
2539 static const char* ops[12] = {
2540 "tosrsubax", "tosrsubax", "tosrsubeax", "tosrsubeax",
2542 oper (flags, val, ops);
2547 void g_mul (unsigned flags, unsigned long val)
2548 /* Primary = TOS * Primary */
2550 static const char* ops[12] = {
2551 "tosmulax", "tosumulax", "tosmuleax", "tosumuleax",
2556 /* Do strength reduction if the value is constant and a power of two */
2557 if (flags & CF_CONST && (p2 = PowerOf2 (val)) >= 0) {
2558 /* Generate a shift instead */
2563 /* If the right hand side is const, the lhs is not on stack but still
2564 * in the primary register.
2566 if (flags & CF_CONST) {
2568 switch (flags & CF_TYPE) {
2571 if (flags & CF_FORCECHAR) {
2572 /* Handle some special cases */
2576 AddCodeLine ("sta tmp1");
2577 AddCodeLine ("asl a");
2578 AddCodeLine ("clc");
2579 AddCodeLine ("adc tmp1");
2583 AddCodeLine ("sta tmp1");
2584 AddCodeLine ("asl a");
2585 AddCodeLine ("asl a");
2586 AddCodeLine ("clc");
2587 AddCodeLine ("adc tmp1");
2591 AddCodeLine ("sta tmp1");
2592 AddCodeLine ("asl a");
2593 AddCodeLine ("clc");
2594 AddCodeLine ("adc tmp1");
2595 AddCodeLine ("asl a");
2599 AddCodeLine ("sta tmp1");
2600 AddCodeLine ("asl a");
2601 AddCodeLine ("asl a");
2602 AddCodeLine ("clc");
2603 AddCodeLine ("adc tmp1");
2604 AddCodeLine ("asl a");
2613 AddCodeLine ("jsr mulax3");
2616 AddCodeLine ("jsr mulax5");
2619 AddCodeLine ("jsr mulax6");
2622 AddCodeLine ("jsr mulax7");
2625 AddCodeLine ("jsr mulax9");
2628 AddCodeLine ("jsr mulax10");
2640 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2641 * into the normal, non-optimized stuff.
2643 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2644 g_push (flags & ~CF_CONST, 0);
2648 /* Use long way over the stack */
2649 oper (flags, val, ops);
2654 void g_div (unsigned flags, unsigned long val)
2655 /* Primary = TOS / Primary */
2657 static const char* ops[12] = {
2658 "tosdivax", "tosudivax", "tosdiveax", "tosudiveax",
2661 /* Do strength reduction if the value is constant and a power of two */
2663 if ((flags & CF_CONST) && (p2 = PowerOf2 (val)) >= 0) {
2664 /* Generate a shift instead */
2667 /* Generate a division */
2668 if (flags & CF_CONST) {
2669 /* lhs is not on stack */
2670 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2671 g_push (flags & ~CF_CONST, 0);
2673 oper (flags, val, ops);
2679 void g_mod (unsigned flags, unsigned long val)
2680 /* Primary = TOS % Primary */
2682 static const char* ops[12] = {
2683 "tosmodax", "tosumodax", "tosmodeax", "tosumodeax",
2687 /* Check if we can do some cost reduction */
2688 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = PowerOf2 (val)) >= 0) {
2689 /* We can do that with an AND operation */
2690 g_and (flags, val - 1);
2692 /* Do it the hard way... */
2693 if (flags & CF_CONST) {
2694 /* lhs is not on stack */
2695 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2696 g_push (flags & ~CF_CONST, 0);
2698 oper (flags, val, ops);
2704 void g_or (unsigned flags, unsigned long val)
2705 /* Primary = TOS | Primary */
2707 static const char* ops[12] = {
2708 "tosorax", "tosorax", "tosoreax", "tosoreax",
2711 /* If the right hand side is const, the lhs is not on stack but still
2712 * in the primary register.
2714 if (flags & CF_CONST) {
2716 switch (flags & CF_TYPE) {
2719 if (flags & CF_FORCECHAR) {
2720 if ((val & 0xFF) != 0) {
2721 AddCodeLine ("ora #$%02X", (unsigned char)val);
2729 if ((val & 0xFF) != 0) {
2730 AddCodeLine ("ora #$%02X", (unsigned char)val);
2732 } else if ((val & 0xFF00) == 0xFF00) {
2733 if ((val & 0xFF) != 0) {
2734 AddCodeLine ("ora #$%02X", (unsigned char)val);
2737 } else if (val != 0) {
2738 AddCodeLine ("ora #$%02X", (unsigned char)val);
2739 AddCodeLine ("pha");
2740 AddCodeLine ("txa");
2741 AddCodeLine ("ora #$%02X", (unsigned char)(val >> 8));
2742 AddCodeLine ("tax");
2743 AddCodeLine ("pla");
2749 if ((val & 0xFF) != 0) {
2750 AddCodeLine ("ora #$%02X", (unsigned char)val);
2760 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2761 * into the normal, non-optimized stuff. Note: The standard stuff will
2762 * always work with ints.
2764 flags &= ~CF_FORCECHAR;
2765 g_push (flags & ~CF_CONST, 0);
2768 /* Use long way over the stack */
2769 oper (flags, val, ops);
2774 void g_xor (unsigned flags, unsigned long val)
2775 /* Primary = TOS ^ Primary */
2777 static const char* ops[12] = {
2778 "tosxorax", "tosxorax", "tosxoreax", "tosxoreax",
2782 /* If the right hand side is const, the lhs is not on stack but still
2783 * in the primary register.
2785 if (flags & CF_CONST) {
2787 switch (flags & CF_TYPE) {
2790 if (flags & CF_FORCECHAR) {
2791 if ((val & 0xFF) != 0) {
2792 AddCodeLine ("eor #$%02X", (unsigned char)val);
2801 AddCodeLine ("eor #$%02X", (unsigned char)val);
2803 } else if (val != 0) {
2804 if ((val & 0xFF) != 0) {
2805 AddCodeLine ("eor #$%02X", (unsigned char)val);
2807 AddCodeLine ("pha");
2808 AddCodeLine ("txa");
2809 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2810 AddCodeLine ("tax");
2811 AddCodeLine ("pla");
2818 AddCodeLine ("eor #$%02X", (unsigned char)val);
2828 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2829 * into the normal, non-optimized stuff. Note: The standard stuff will
2830 * always work with ints.
2832 flags &= ~CF_FORCECHAR;
2833 g_push (flags & ~CF_CONST, 0);
2836 /* Use long way over the stack */
2837 oper (flags, val, ops);
2842 void g_and (unsigned Flags, unsigned long Val)
2843 /* Primary = TOS & Primary */
2845 static const char* ops[12] = {
2846 "tosandax", "tosandax", "tosandeax", "tosandeax",
2849 /* If the right hand side is const, the lhs is not on stack but still
2850 * in the primary register.
2852 if (Flags & CF_CONST) {
2854 switch (Flags & CF_TYPE) {
2857 if (Flags & CF_FORCECHAR) {
2858 if ((Val & 0xFF) == 0x00) {
2859 AddCodeLine ("lda #$00");
2860 } else if ((Val & 0xFF) != 0xFF) {
2861 AddCodeLine ("and #$%02X", (unsigned char)Val);
2867 if ((Val & 0xFFFF) != 0xFFFF) {
2871 AddCodeLine ("lda #$00");
2872 } else if (Val != 0xFF) {
2873 AddCodeLine ("and #$%02X", (unsigned char)Val);
2875 } else if ((Val & 0xFFFF) == 0xFF00) {
2876 AddCodeLine ("lda #$00");
2877 } else if ((Val & 0xFF00) == 0xFF00) {
2878 AddCodeLine ("and #$%02X", (unsigned char)Val);
2879 } else if ((Val & 0x00FF) == 0x0000) {
2880 AddCodeLine ("txa");
2881 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2882 AddCodeLine ("tax");
2883 AddCodeLine ("lda #$00");
2885 AddCodeLine ("tay");
2886 AddCodeLine ("txa");
2887 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2888 AddCodeLine ("tax");
2889 AddCodeLine ("tya");
2890 if ((Val & 0x00FF) == 0x0000) {
2891 AddCodeLine ("lda #$00");
2892 } else if ((Val & 0x00FF) != 0x00FF) {
2893 AddCodeLine ("and #$%02X", (unsigned char)Val);
2902 AddCodeLine ("stx sreg+1");
2903 AddCodeLine ("stx sreg");
2904 if ((Val & 0xFF) != 0xFF) {
2905 AddCodeLine ("and #$%02X", (unsigned char)Val);
2908 } else if (Val == 0xFF00) {
2910 AddCodeLine ("sta sreg+1");
2911 AddCodeLine ("sta sreg");
2920 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2921 * into the normal, non-optimized stuff. Note: The standard stuff will
2922 * always work with ints.
2924 Flags &= ~CF_FORCECHAR;
2925 g_push (Flags & ~CF_CONST, 0);
2928 /* Use long way over the stack */
2929 oper (Flags, Val, ops);
2934 void g_asr (unsigned flags, unsigned long val)
2935 /* Primary = TOS >> Primary */
2937 static const char* ops[12] = {
2938 "tosasrax", "tosshrax", "tosasreax", "tosshreax",
2941 /* If the right hand side is const, the lhs is not on stack but still
2942 * in the primary register.
2944 if (flags & CF_CONST) {
2946 switch (flags & CF_TYPE) {
2952 if (flags & CF_UNSIGNED) {
2953 AddCodeLine ("txa");
2956 unsigned L = GetLocalLabel();
2957 AddCodeLine ("cpx #$80"); /* Sign bit into carry */
2958 AddCodeLine ("txa");
2959 AddCodeLine ("ldx #$00");
2960 AddCodeLine ("bcc %s", LocalLabelName (L));
2961 AddCodeLine ("dex"); /* Make $FF */
2967 if (flags & CF_UNSIGNED) {
2968 AddCodeLine ("jsr shrax4");
2970 AddCodeLine ("jsr asrax4");
2975 if (flags & CF_UNSIGNED) {
2976 AddCodeLine ("jsr shrax%ld", val);
2978 AddCodeLine ("jsr asrax%ld", val);
2986 AddCodeLine ("ldx #$00");
2987 AddCodeLine ("lda sreg+1");
2988 if ((flags & CF_UNSIGNED) == 0) {
2989 unsigned L = GetLocalLabel();
2990 AddCodeLine ("bpl %s", LocalLabelName (L));
2991 AddCodeLine ("dex");
2994 AddCodeLine ("stx sreg");
2995 AddCodeLine ("stx sreg+1");
2999 AddCodeLine ("ldy #$00");
3000 AddCodeLine ("ldx sreg+1");
3001 if ((flags & CF_UNSIGNED) == 0) {
3002 unsigned L = GetLocalLabel();
3003 AddCodeLine ("bpl %s", LocalLabelName (L));
3004 AddCodeLine ("dey");
3007 AddCodeLine ("lda sreg");
3008 AddCodeLine ("sty sreg+1");
3009 AddCodeLine ("sty sreg");
3013 AddCodeLine ("txa");
3014 AddCodeLine ("ldx sreg");
3015 AddCodeLine ("ldy sreg+1");
3016 AddCodeLine ("sty sreg");
3017 if ((flags & CF_UNSIGNED) == 0) {
3018 unsigned L = GetLocalLabel();
3019 AddCodeLine ("cpy #$80");
3020 AddCodeLine ("ldy #$00");
3021 AddCodeLine ("bcc %s", LocalLabelName (L));
3022 AddCodeLine ("dey");
3025 AddCodeLine ("ldy #$00");
3027 AddCodeLine ("sty sreg+1");
3031 if (flags & CF_UNSIGNED) {
3032 AddCodeLine ("jsr shreax4");
3034 AddCodeLine ("jsr asreax4");
3039 if (flags & CF_UNSIGNED) {
3040 AddCodeLine ("jsr shreax%ld", val);
3042 AddCodeLine ("jsr asreax%ld", val);
3051 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3052 * into the normal, non-optimized stuff. Note: The standard stuff will
3053 * always work with ints.
3055 flags &= ~CF_FORCECHAR;
3056 g_push (flags & ~CF_CONST, 0);
3059 /* Use long way over the stack */
3060 oper (flags, val, ops);
3065 void g_asl (unsigned flags, unsigned long val)
3066 /* Primary = TOS << Primary */
3068 static const char* ops[12] = {
3069 "tosaslax", "tosshlax", "tosasleax", "tosshleax",
3073 /* If the right hand side is const, the lhs is not on stack but still
3074 * in the primary register.
3076 if (flags & CF_CONST) {
3078 switch (flags & CF_TYPE) {
3084 AddCodeLine ("tax");
3085 AddCodeLine ("lda #$00");
3089 if (flags & CF_UNSIGNED) {
3090 AddCodeLine ("jsr shlax4");
3092 AddCodeLine ("jsr aslax4");
3097 if (flags & CF_UNSIGNED) {
3098 AddCodeLine ("jsr shlax%ld", val);
3100 AddCodeLine ("jsr aslax%ld", val);
3108 AddCodeLine ("sta sreg+1");
3109 AddCodeLine ("lda #$00");
3110 AddCodeLine ("tax");
3111 AddCodeLine ("sta sreg");
3115 AddCodeLine ("stx sreg+1");
3116 AddCodeLine ("sta sreg");
3117 AddCodeLine ("lda #$00");
3118 AddCodeLine ("tax");
3122 AddCodeLine ("ldy sreg");
3123 AddCodeLine ("sty sreg+1");
3124 AddCodeLine ("stx sreg");
3125 AddCodeLine ("tax");
3126 AddCodeLine ("lda #$00");
3130 if (flags & CF_UNSIGNED) {
3131 AddCodeLine ("jsr shleax4");
3133 AddCodeLine ("jsr asleax4");
3138 if (flags & CF_UNSIGNED) {
3139 AddCodeLine ("jsr shleax%ld", val);
3141 AddCodeLine ("jsr asleax%ld", val);
3150 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3151 * into the normal, non-optimized stuff. Note: The standard stuff will
3152 * always work with ints.
3154 flags &= ~CF_FORCECHAR;
3155 g_push (flags & ~CF_CONST, 0);
3158 /* Use long way over the stack */
3159 oper (flags, val, ops);
3164 void g_neg (unsigned Flags)
3165 /* Primary = -Primary */
3167 switch (Flags & CF_TYPE) {
3170 if (Flags & CF_FORCECHAR) {
3171 AddCodeLine ("eor #$FF");
3172 AddCodeLine ("clc");
3173 AddCodeLine ("adc #$01");
3179 AddCodeLine ("jsr negax");
3183 AddCodeLine ("jsr negeax");
3193 void g_bneg (unsigned flags)
3194 /* Primary = !Primary */
3196 switch (flags & CF_TYPE) {
3199 AddCodeLine ("jsr bnega");
3203 AddCodeLine ("jsr bnegax");
3207 AddCodeLine ("jsr bnegeax");
3217 void g_com (unsigned Flags)
3218 /* Primary = ~Primary */
3220 switch (Flags & CF_TYPE) {
3223 if (Flags & CF_FORCECHAR) {
3224 AddCodeLine ("eor #$FF");
3230 AddCodeLine ("jsr complax");
3234 AddCodeLine ("jsr compleax");
3244 void g_inc (unsigned flags, unsigned long val)
3245 /* Increment the primary register by a given number */
3247 /* Don't inc by zero */
3252 /* Generate code for the supported types */
3254 switch (flags & CF_TYPE) {
3257 if (flags & CF_FORCECHAR) {
3258 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3260 AddCodeLine ("ina");
3263 AddCodeLine ("clc");
3264 AddCodeLine ("adc #$%02X", (unsigned char)val);
3271 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) {
3272 unsigned L = GetLocalLabel();
3273 AddCodeLine ("ina");
3274 AddCodeLine ("bne %s", LocalLabelName (L));
3275 AddCodeLine ("inx");
3277 } else if (IS_Get (&CodeSizeFactor) < 200) {
3280 AddCodeLine ("jsr incax%lu", val);
3281 } else if (val <= 255) {
3283 AddCodeLine ("jsr incaxy");
3285 g_add (flags | CF_CONST, val);
3288 /* Inline the code */
3290 if ((val & 0xFF) != 0) {
3291 unsigned L = GetLocalLabel();
3292 AddCodeLine ("clc");
3293 AddCodeLine ("adc #$%02X", (unsigned char) val);
3294 AddCodeLine ("bcc %s", LocalLabelName (L));
3295 AddCodeLine ("inx");
3299 AddCodeLine ("inx");
3302 AddCodeLine ("inx");
3305 AddCodeLine ("inx");
3307 } else if ((val & 0xFF) != 0) {
3308 AddCodeLine ("clc");
3309 AddCodeLine ("adc #$%02X", (unsigned char) val);
3310 AddCodeLine ("pha");
3311 AddCodeLine ("txa");
3312 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3313 AddCodeLine ("tax");
3314 AddCodeLine ("pla");
3316 AddCodeLine ("pha");
3317 AddCodeLine ("txa");
3318 AddCodeLine ("clc");
3319 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3320 AddCodeLine ("tax");
3321 AddCodeLine ("pla");
3329 AddCodeLine ("jsr inceaxy");
3331 g_add (flags | CF_CONST, val);
3343 void g_dec (unsigned flags, unsigned long val)
3344 /* Decrement the primary register by a given number */
3346 /* Don't dec by zero */
3351 /* Generate code for the supported types */
3353 switch (flags & CF_TYPE) {
3356 if (flags & CF_FORCECHAR) {
3357 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3359 AddCodeLine ("dea");
3362 AddCodeLine ("sec");
3363 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3370 if (IS_Get (&CodeSizeFactor) < 200) {
3371 /* Use subroutines */
3373 AddCodeLine ("jsr decax%d", (int) val);
3374 } else if (val <= 255) {
3376 AddCodeLine ("jsr decaxy");
3378 g_sub (flags | CF_CONST, val);
3381 /* Inline the code */
3383 if ((val & 0xFF) != 0) {
3384 unsigned L = GetLocalLabel();
3385 AddCodeLine ("sec");
3386 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3387 AddCodeLine ("bcs %s", LocalLabelName (L));
3388 AddCodeLine ("dex");
3392 AddCodeLine ("dex");
3395 AddCodeLine ("dex");
3398 if ((val & 0xFF) != 0) {
3399 AddCodeLine ("sec");
3400 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3401 AddCodeLine ("pha");
3402 AddCodeLine ("txa");
3403 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3404 AddCodeLine ("tax");
3405 AddCodeLine ("pla");
3407 AddCodeLine ("pha");
3408 AddCodeLine ("txa");
3409 AddCodeLine ("sec");
3410 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3411 AddCodeLine ("tax");
3412 AddCodeLine ("pla");
3421 AddCodeLine ("jsr deceaxy");
3423 g_sub (flags | CF_CONST, val);
3436 * Following are the conditional operators. They compare the TOS against
3437 * the primary and put a literal 1 in the primary if the condition is
3438 * true, otherwise they clear the primary register
3443 void g_eq (unsigned flags, unsigned long val)
3444 /* Test for equal */
3446 static const char* ops[12] = {
3447 "toseqax", "toseqax", "toseqeax", "toseqeax",
3452 /* If the right hand side is const, the lhs is not on stack but still
3453 * in the primary register.
3455 if (flags & CF_CONST) {
3457 switch (flags & CF_TYPE) {
3460 if (flags & CF_FORCECHAR) {
3461 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3462 AddCodeLine ("jsr booleq");
3468 L = GetLocalLabel();
3469 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3470 AddCodeLine ("bne %s", LocalLabelName (L));
3471 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3473 AddCodeLine ("jsr booleq");
3483 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3484 * into the normal, non-optimized stuff. Note: The standard stuff will
3485 * always work with ints.
3487 flags &= ~CF_FORCECHAR;
3488 g_push (flags & ~CF_CONST, 0);
3491 /* Use long way over the stack */
3492 oper (flags, val, ops);
3497 void g_ne (unsigned flags, unsigned long val)
3498 /* Test for not equal */
3500 static const char* ops[12] = {
3501 "tosneax", "tosneax", "tosneeax", "tosneeax",
3506 /* If the right hand side is const, the lhs is not on stack but still
3507 * in the primary register.
3509 if (flags & CF_CONST) {
3511 switch (flags & CF_TYPE) {
3514 if (flags & CF_FORCECHAR) {
3515 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3516 AddCodeLine ("jsr boolne");
3522 L = GetLocalLabel();
3523 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3524 AddCodeLine ("bne %s", LocalLabelName (L));
3525 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3527 AddCodeLine ("jsr boolne");
3537 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3538 * into the normal, non-optimized stuff. Note: The standard stuff will
3539 * always work with ints.
3541 flags &= ~CF_FORCECHAR;
3542 g_push (flags & ~CF_CONST, 0);
3545 /* Use long way over the stack */
3546 oper (flags, val, ops);
3551 void g_lt (unsigned flags, unsigned long val)
3552 /* Test for less than */
3554 static const char* ops[12] = {
3555 "tosltax", "tosultax", "toslteax", "tosulteax",
3560 /* If the right hand side is const, the lhs is not on stack but still
3561 * in the primary register.
3563 if (flags & CF_CONST) {
3565 /* Because the handling of the overflow flag is too complex for
3566 * inlining, we can handle only unsigned compares, and signed
3567 * compares against zero here.
3569 if (flags & CF_UNSIGNED) {
3571 /* Give a warning in some special cases */
3573 Warning ("Condition is never true");
3574 AddCodeLine ("jsr return0");
3578 /* Look at the type */
3579 switch (flags & CF_TYPE) {
3582 if (flags & CF_FORCECHAR) {
3583 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3584 AddCodeLine ("jsr boolult");
3590 /* If the low byte is zero, we must only test the high byte */
3591 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3592 if ((val & 0xFF) != 0) {
3593 unsigned L = GetLocalLabel();
3594 AddCodeLine ("bne %s", LocalLabelName (L));
3595 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3598 AddCodeLine ("jsr boolult");
3602 /* Do a subtraction */
3603 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3604 AddCodeLine ("txa");
3605 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3606 AddCodeLine ("lda sreg");
3607 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3608 AddCodeLine ("lda sreg+1");
3609 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3610 AddCodeLine ("jsr boolult");
3617 } else if (val == 0) {
3619 /* A signed compare against zero must only look at the sign bit */
3620 switch (flags & CF_TYPE) {
3623 if (flags & CF_FORCECHAR) {
3624 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3625 AddCodeLine ("lda #$00");
3626 AddCodeLine ("ldx #$00");
3627 AddCodeLine ("rol a");
3633 /* Just check the high byte */
3634 AddCodeLine ("cpx #$80"); /* Bit 7 -> carry */
3635 AddCodeLine ("lda #$00");
3636 AddCodeLine ("ldx #$00");
3637 AddCodeLine ("rol a");
3641 /* Just check the high byte */
3642 AddCodeLine ("lda sreg+1");
3643 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3644 AddCodeLine ("lda #$00");
3645 AddCodeLine ("ldx #$00");
3646 AddCodeLine ("rol a");
3655 /* Signed compare against a constant != zero */
3656 switch (flags & CF_TYPE) {
3659 if (flags & CF_FORCECHAR) {
3660 Label = GetLocalLabel ();
3661 AddCodeLine ("sec");
3662 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3663 AddCodeLine ("bvc %s", LocalLabelName (Label));
3664 AddCodeLine ("eor #$80");
3665 g_defcodelabel (Label);
3666 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3667 AddCodeLine ("lda #$00");
3668 AddCodeLine ("ldx #$00");
3669 AddCodeLine ("rol a");
3675 /* Do a subtraction */
3676 Label = GetLocalLabel ();
3677 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3678 AddCodeLine ("txa");
3679 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3680 AddCodeLine ("bvc %s", LocalLabelName (Label));
3681 AddCodeLine ("eor #$80");
3682 g_defcodelabel (Label);
3683 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3684 AddCodeLine ("lda #$00");
3685 AddCodeLine ("ldx #$00");
3686 AddCodeLine ("rol a");
3690 /* This one is too costly */
3699 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3700 * into the normal, non-optimized stuff. Note: The standard stuff will
3701 * always work with ints.
3703 flags &= ~CF_FORCECHAR;
3704 g_push (flags & ~CF_CONST, 0);
3707 /* Use long way over the stack */
3708 oper (flags, val, ops);
3713 void g_le (unsigned flags, unsigned long val)
3714 /* Test for less than or equal to */
3716 static const char* ops[12] = {
3717 "tosleax", "tosuleax", "tosleeax", "tosuleeax",
3721 /* If the right hand side is const, the lhs is not on stack but still
3722 * in the primary register.
3724 if (flags & CF_CONST) {
3726 /* Look at the type */
3727 switch (flags & CF_TYPE) {
3730 if (flags & CF_FORCECHAR) {
3731 if (flags & CF_UNSIGNED) {
3732 /* Unsigned compare */
3734 /* Use < instead of <= because the former gives
3735 * better code on the 6502 than the latter.
3737 g_lt (flags, val+1);
3740 Warning ("Condition is always true");
3741 AddCodeLine ("jsr return1");
3744 /* Signed compare */
3745 if ((long) val < 0x7F) {
3746 /* Use < instead of <= because the former gives
3747 * better code on the 6502 than the latter.
3749 g_lt (flags, val+1);
3752 Warning ("Condition is always true");
3753 AddCodeLine ("jsr return1");
3761 if (flags & CF_UNSIGNED) {
3762 /* Unsigned compare */
3764 /* Use < instead of <= because the former gives
3765 * better code on the 6502 than the latter.
3767 g_lt (flags, val+1);
3770 Warning ("Condition is always true");
3771 AddCodeLine ("jsr return1");
3774 /* Signed compare */
3775 if ((long) val < 0x7FFF) {
3776 g_lt (flags, val+1);
3779 Warning ("Condition is always true");
3780 AddCodeLine ("jsr return1");
3786 if (flags & CF_UNSIGNED) {
3787 /* Unsigned compare */
3788 if (val < 0xFFFFFFFF) {
3789 /* Use < instead of <= because the former gives
3790 * better code on the 6502 than the latter.
3792 g_lt (flags, val+1);
3795 Warning ("Condition is always true");
3796 AddCodeLine ("jsr return1");
3799 /* Signed compare */
3800 if ((long) val < 0x7FFFFFFF) {
3801 g_lt (flags, val+1);
3804 Warning ("Condition is always true");
3805 AddCodeLine ("jsr return1");
3814 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3815 * into the normal, non-optimized stuff. Note: The standard stuff will
3816 * always work with ints.
3818 flags &= ~CF_FORCECHAR;
3819 g_push (flags & ~CF_CONST, 0);
3822 /* Use long way over the stack */
3823 oper (flags, val, ops);
3828 void g_gt (unsigned flags, unsigned long val)
3829 /* Test for greater than */
3831 static const char* ops[12] = {
3832 "tosgtax", "tosugtax", "tosgteax", "tosugteax",
3836 /* If the right hand side is const, the lhs is not on stack but still
3837 * in the primary register.
3839 if (flags & CF_CONST) {
3841 /* Look at the type */
3842 switch (flags & CF_TYPE) {
3845 if (flags & CF_FORCECHAR) {
3846 if (flags & CF_UNSIGNED) {
3848 /* If we have a compare > 0, we will replace it by
3849 * != 0 here, since both are identical but the
3850 * latter is easier to optimize.
3853 } else if (val < 0xFF) {
3854 /* Use >= instead of > because the former gives
3855 * better code on the 6502 than the latter.
3857 g_ge (flags, val+1);
3860 Warning ("Condition is never true");
3861 AddCodeLine ("jsr return0");
3864 if ((long) val < 0x7F) {
3865 /* Use >= instead of > because the former gives
3866 * better code on the 6502 than the latter.
3868 g_ge (flags, val+1);
3871 Warning ("Condition is never true");
3872 AddCodeLine ("jsr return0");
3880 if (flags & CF_UNSIGNED) {
3881 /* Unsigned compare */
3883 /* If we have a compare > 0, we will replace it by
3884 * != 0 here, since both are identical but the latter
3885 * is easier to optimize.
3888 } else if (val < 0xFFFF) {
3889 /* Use >= instead of > because the former gives better
3890 * code on the 6502 than the latter.
3892 g_ge (flags, val+1);
3895 Warning ("Condition is never true");
3896 AddCodeLine ("jsr return0");
3899 /* Signed compare */
3900 if ((long) val < 0x7FFF) {
3901 g_ge (flags, val+1);
3904 Warning ("Condition is never true");
3905 AddCodeLine ("jsr return0");
3911 if (flags & CF_UNSIGNED) {
3912 /* Unsigned compare */
3914 /* If we have a compare > 0, we will replace it by
3915 * != 0 here, since both are identical but the latter
3916 * is easier to optimize.
3919 } else if (val < 0xFFFFFFFF) {
3920 /* Use >= instead of > because the former gives better
3921 * code on the 6502 than the latter.
3923 g_ge (flags, val+1);
3926 Warning ("Condition is never true");
3927 AddCodeLine ("jsr return0");
3930 /* Signed compare */
3931 if ((long) val < 0x7FFFFFFF) {
3932 g_ge (flags, val+1);
3935 Warning ("Condition is never true");
3936 AddCodeLine ("jsr return0");
3945 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3946 * into the normal, non-optimized stuff. Note: The standard stuff will
3947 * always work with ints.
3949 flags &= ~CF_FORCECHAR;
3950 g_push (flags & ~CF_CONST, 0);
3953 /* Use long way over the stack */
3954 oper (flags, val, ops);
3959 void g_ge (unsigned flags, unsigned long val)
3960 /* Test for greater than or equal to */
3962 static const char* ops[12] = {
3963 "tosgeax", "tosugeax", "tosgeeax", "tosugeeax",
3969 /* If the right hand side is const, the lhs is not on stack but still
3970 * in the primary register.
3972 if (flags & CF_CONST) {
3974 /* Because the handling of the overflow flag is too complex for
3975 * inlining, we can handle only unsigned compares, and signed
3976 * compares against zero here.
3978 if (flags & CF_UNSIGNED) {
3980 /* Give a warning in some special cases */
3982 Warning ("Condition is always true");
3983 AddCodeLine ("jsr return1");
3987 /* Look at the type */
3988 switch (flags & CF_TYPE) {
3991 if (flags & CF_FORCECHAR) {
3992 /* Do a subtraction. Condition is true if carry set */
3993 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3994 AddCodeLine ("lda #$00");
3995 AddCodeLine ("ldx #$00");
3996 AddCodeLine ("rol a");
4002 /* Do a subtraction. Condition is true if carry set */
4003 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4004 AddCodeLine ("txa");
4005 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4006 AddCodeLine ("lda #$00");
4007 AddCodeLine ("ldx #$00");
4008 AddCodeLine ("rol a");
4012 /* Do a subtraction. Condition is true if carry set */
4013 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4014 AddCodeLine ("txa");
4015 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4016 AddCodeLine ("lda sreg");
4017 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
4018 AddCodeLine ("lda sreg+1");
4019 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
4020 AddCodeLine ("lda #$00");
4021 AddCodeLine ("ldx #$00");
4022 AddCodeLine ("rol a");
4029 } else if (val == 0) {
4031 /* A signed compare against zero must only look at the sign bit */
4032 switch (flags & CF_TYPE) {
4035 if (flags & CF_FORCECHAR) {
4036 AddCodeLine ("tax");
4037 AddCodeLine ("jsr boolge");
4043 /* Just test the high byte */
4044 AddCodeLine ("txa");
4045 AddCodeLine ("jsr boolge");
4049 /* Just test the high byte */
4050 AddCodeLine ("lda sreg+1");
4051 AddCodeLine ("jsr boolge");
4060 /* Signed compare against a constant != zero */
4061 switch (flags & CF_TYPE) {
4064 if (flags & CF_FORCECHAR) {
4065 Label = GetLocalLabel ();
4066 AddCodeLine ("sec");
4067 AddCodeLine ("sbc #$%02X", (unsigned char)val);
4068 AddCodeLine ("bvs %s", LocalLabelName (Label));
4069 AddCodeLine ("eor #$80");
4070 g_defcodelabel (Label);
4071 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4072 AddCodeLine ("lda #$00");
4073 AddCodeLine ("ldx #$00");
4074 AddCodeLine ("rol a");
4080 /* Do a subtraction */
4081 Label = GetLocalLabel ();
4082 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4083 AddCodeLine ("txa");
4084 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4085 AddCodeLine ("bvs %s", LocalLabelName (Label));
4086 AddCodeLine ("eor #$80");
4087 g_defcodelabel (Label);
4088 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4089 AddCodeLine ("lda #$00");
4090 AddCodeLine ("ldx #$00");
4091 AddCodeLine ("rol a");
4095 /* This one is too costly */
4103 /* If we go here, we didn't emit code. Push the lhs on stack and fall
4104 * into the normal, non-optimized stuff. Note: The standard stuff will
4105 * always work with ints.
4107 flags &= ~CF_FORCECHAR;
4108 g_push (flags & ~CF_CONST, 0);
4111 /* Use long way over the stack */
4112 oper (flags, val, ops);
4117 /*****************************************************************************/
4118 /* Allocating static storage */
4119 /*****************************************************************************/
4123 void g_res (unsigned n)
4124 /* Reserve static storage, n bytes */
4126 AddDataLine ("\t.res\t%u,$00", n);
4131 void g_defdata (unsigned flags, unsigned long val, long offs)
4132 /* Define data with the size given in flags */
4134 if (flags & CF_CONST) {
4136 /* Numeric constant */
4137 switch (flags & CF_TYPE) {
4140 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
4144 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
4148 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
4159 /* Create the correct label name */
4160 const char* Label = GetLabelName (flags, val, offs);
4162 /* Labels are always 16 bit */
4163 AddDataLine ("\t.addr\t%s", Label);
4170 void g_defbytes (const void* Bytes, unsigned Count)
4171 /* Output a row of bytes as a constant */
4177 /* Cast the buffer pointer */
4178 const unsigned char* Data = (const unsigned char*) Bytes;
4180 /* Output the stuff */
4183 /* How many go into this line? */
4184 if ((Chunk = Count) > 16) {
4189 /* Output one line */
4190 strcpy (Buf, "\t.byte\t");
4193 B += sprintf (B, "$%02X", *Data++);
4199 /* Output the line */
4206 void g_zerobytes (unsigned Count)
4207 /* Output Count bytes of data initialized with zero */
4210 AddDataLine ("\t.res\t%u,$00", Count);
4216 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4217 /* Initialize a register variable from static initialization data */
4219 /* Register variables do always have less than 128 bytes */
4220 unsigned CodeLabel = GetLocalLabel ();
4222 g_defcodelabel (CodeLabel);
4223 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4224 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4225 AddCodeLine ("dex");
4226 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4231 void g_initauto (unsigned Label, unsigned Size)
4232 /* Initialize a local variable at stack offset zero from static data */
4234 unsigned CodeLabel = GetLocalLabel ();
4236 CheckLocalOffs (Size);
4239 g_defcodelabel (CodeLabel);
4240 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4241 AddCodeLine ("sta (sp),y");
4242 AddCodeLine ("dey");
4243 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4244 } else if (Size <= 256) {
4246 g_defcodelabel (CodeLabel);
4247 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4248 AddCodeLine ("sta (sp),y");
4249 AddCodeLine ("iny");
4250 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4251 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4257 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4258 /* Initialize a static local variable from static initialization data */
4261 unsigned CodeLabel = GetLocalLabel ();
4263 g_defcodelabel (CodeLabel);
4264 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4265 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4266 AddCodeLine ("dey");
4267 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4268 } else if (Size <= 256) {
4269 unsigned CodeLabel = GetLocalLabel ();
4271 g_defcodelabel (CodeLabel);
4272 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4273 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4274 AddCodeLine ("iny");
4275 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4276 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4278 /* Use the easy way here: memcpy */
4279 g_getimmed (CF_STATIC, VarLabel, 0);
4280 AddCodeLine ("jsr pushax");
4281 g_getimmed (CF_STATIC, InitLabel, 0);
4282 AddCodeLine ("jsr pushax");
4283 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4284 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4290 /*****************************************************************************/
4291 /* Switch statement */
4292 /*****************************************************************************/
4296 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4297 /* Generate code for a switch statement */
4299 unsigned NextLabel = 0;
4302 /* Setup registers and determine which compare insn to use */
4303 const char* Compare;
4306 Compare = "cmp #$%02X";
4309 Compare = "cpx #$%02X";
4312 AddCodeLine ("ldy sreg");
4313 Compare = "cpy #$%02X";
4316 AddCodeLine ("ldy sreg+1");
4317 Compare = "cpy #$%02X";
4320 Internal ("Invalid depth in g_switch: %u", Depth);
4323 /* Walk over all nodes */
4324 for (I = 0; I < CollCount (Nodes); ++I) {
4326 /* Get the next case node */
4327 CaseNode* N = CollAtUnchecked (Nodes, I);
4329 /* If we have a next label, define it */
4331 g_defcodelabel (NextLabel);
4335 /* Do the compare */
4336 AddCodeLine (Compare, CN_GetValue (N));
4338 /* If this is the last level, jump directly to the case code if found */
4341 /* Branch if equal */
4342 g_falsejump (0, CN_GetLabel (N));
4346 /* Determine the next label */
4347 if (I == CollCount (Nodes) - 1) {
4348 /* Last node means not found */
4349 g_truejump (0, DefaultLabel);
4351 /* Jump to the next check */
4352 NextLabel = GetLocalLabel ();
4353 g_truejump (0, NextLabel);
4356 /* Check the next level */
4357 g_switch (N->Nodes, DefaultLabel, Depth-1);
4362 /* If we go here, we haven't found the label */
4363 g_jump (DefaultLabel);
4368 /*****************************************************************************/
4369 /* User supplied assembler code */
4370 /*****************************************************************************/
4374 void g_asmcode (struct StrBuf* B)
4375 /* Output one line of assembler code. */
4377 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));