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 /* Get low and high byte */
928 unsigned char Lo = (unsigned char) Offs;
929 unsigned char Hi = (unsigned char) (Offs >> 8);
931 /* Calculate the offset relative to sp */
937 AddCodeLine ("lda sp");
938 AddCodeLine ("ldx sp+1");
943 AddCodeLine ("lda sp+1");
945 AddCodeLine ("adc #$%02X", Hi);
947 AddCodeLine ("lda sp");
949 } else if (Hi == 0) {
951 if (IS_Get (&CodeSizeFactor) < 200) {
952 /* 8 bit offset with subroutine call */
953 AddCodeLine ("lda #$%02X", Lo);
954 AddCodeLine ("jsr leaa0sp");
956 /* 8 bit offset inlined */
957 unsigned L = GetLocalLabel ();
958 AddCodeLine ("lda sp");
959 AddCodeLine ("ldx sp+1");
961 AddCodeLine ("adc #$%02X", Lo);
962 AddCodeLine ("bcc %s", LocalLabelName (L));
966 } else if (IS_Get (&CodeSizeFactor) < 170) {
967 /* Full 16 bit offset with subroutine call */
968 AddCodeLine ("lda #$%02X", Lo);
969 AddCodeLine ("ldx #$%02X", Hi);
970 AddCodeLine ("jsr leaaxsp");
972 /* Full 16 bit offset inlined */
973 AddCodeLine ("lda sp");
975 AddCodeLine ("adc #$%02X", (unsigned char) Offs);
977 AddCodeLine ("lda sp+1");
978 AddCodeLine ("adc #$%02X", (unsigned char) (Offs >> 8));
986 void g_leavariadic (int Offs)
987 /* Fetch the address of a parameter in a variadic function into the primary
991 unsigned ArgSizeOffs;
993 /* Calculate the offset relative to sp */
996 /* Get the offset of the parameter which is stored at sp+0 on function
997 * entry and check if this offset is reachable with a byte offset.
999 CHECK (StackPtr <= 0);
1000 ArgSizeOffs = -StackPtr;
1001 CheckLocalOffs (ArgSizeOffs);
1003 /* Get the size of all parameters. */
1004 ldyconst (ArgSizeOffs);
1005 AddCodeLine ("lda (sp),y");
1007 /* Add the value of the stackpointer */
1008 if (IS_Get (&CodeSizeFactor) > 250) {
1009 unsigned L = GetLocalLabel();
1010 AddCodeLine ("ldx sp+1");
1011 AddCodeLine ("clc");
1012 AddCodeLine ("adc sp");
1013 AddCodeLine ("bcc %s", LocalLabelName (L));
1014 AddCodeLine ("inx");
1017 AddCodeLine ("ldx #$00");
1018 AddCodeLine ("jsr leaaxsp");
1021 /* Add the offset to the primary */
1023 g_inc (CF_INT | CF_CONST, Offs);
1024 } else if (Offs < 0) {
1025 g_dec (CF_INT | CF_CONST, -Offs);
1031 /*****************************************************************************/
1032 /* Store into memory */
1033 /*****************************************************************************/
1037 void g_putstatic (unsigned flags, unsigned long label, long offs)
1038 /* Store the primary register into the specified static memory cell */
1040 /* Create the correct label name */
1041 const char* lbuf = GetLabelName (flags, label, offs);
1043 /* Check the size and generate the correct store operation */
1044 switch (flags & CF_TYPE) {
1047 AddCodeLine ("sta %s", lbuf);
1051 AddCodeLine ("sta %s", lbuf);
1052 AddCodeLine ("stx %s+1", lbuf);
1056 AddCodeLine ("sta %s", lbuf);
1057 AddCodeLine ("stx %s+1", lbuf);
1058 AddCodeLine ("ldy sreg");
1059 AddCodeLine ("sty %s+2", lbuf);
1060 AddCodeLine ("ldy sreg+1");
1061 AddCodeLine ("sty %s+3", lbuf);
1072 void g_putlocal (unsigned Flags, int Offs, long Val)
1073 /* Put data into local object. */
1076 CheckLocalOffs (Offs);
1077 switch (Flags & CF_TYPE) {
1080 if (Flags & CF_CONST) {
1081 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1084 AddCodeLine ("sta (sp),y");
1088 if (Flags & CF_CONST) {
1090 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1091 AddCodeLine ("sta (sp),y");
1092 if ((Flags & CF_NOKEEP) == 0) {
1093 /* Place high byte into X */
1094 AddCodeLine ("tax");
1096 if ((Val & 0xFF) == Offs+1) {
1097 /* The value we need is already in Y */
1098 AddCodeLine ("tya");
1099 AddCodeLine ("dey");
1101 AddCodeLine ("dey");
1102 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1104 AddCodeLine ("sta (sp),y");
1106 if ((Flags & CF_NOKEEP) == 0 || IS_Get (&CodeSizeFactor) < 160) {
1108 AddCodeLine ("jsr staxysp");
1111 AddCodeLine ("sta (sp),y");
1112 AddCodeLine ("iny");
1113 AddCodeLine ("txa");
1114 AddCodeLine ("sta (sp),y");
1120 if (Flags & CF_CONST) {
1121 g_getimmed (Flags, Val, 0);
1124 AddCodeLine ("jsr steaxysp");
1135 void g_putind (unsigned Flags, unsigned Offs)
1136 /* Store the specified object type in the primary register at the address
1137 * on the top of the stack
1140 /* We can handle offsets below $100 directly, larger offsets must be added
1141 * to the address. Since a/x is in use, best code is achieved by adding
1142 * just the high byte. Be sure to check if the low byte will overflow while
1145 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1147 /* Overflow - we need to add the low byte also */
1148 AddCodeLine ("ldy #$00");
1149 AddCodeLine ("clc");
1150 AddCodeLine ("pha");
1151 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1152 AddCodeLine ("adc (sp),y");
1153 AddCodeLine ("sta (sp),y");
1154 AddCodeLine ("iny");
1155 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1156 AddCodeLine ("adc (sp),y");
1157 AddCodeLine ("sta (sp),y");
1158 AddCodeLine ("pla");
1160 /* Complete address is on stack, new offset is zero */
1163 } else if ((Offs & 0xFF00) != 0) {
1165 /* We can just add the high byte */
1166 AddCodeLine ("ldy #$01");
1167 AddCodeLine ("clc");
1168 AddCodeLine ("pha");
1169 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1170 AddCodeLine ("adc (sp),y");
1171 AddCodeLine ("sta (sp),y");
1172 AddCodeLine ("pla");
1174 /* Offset is now just the low byte */
1178 /* Check the size and determine operation */
1179 switch (Flags & CF_TYPE) {
1183 AddCodeLine ("jsr staspidx");
1188 AddCodeLine ("jsr staxspidx");
1193 AddCodeLine ("jsr steaxspidx");
1201 /* Pop the argument which is always a pointer */
1207 /*****************************************************************************/
1208 /* type conversion and similiar stuff */
1209 /*****************************************************************************/
1213 void g_toslong (unsigned flags)
1214 /* Make sure, the value on TOS is a long. Convert if necessary */
1216 switch (flags & CF_TYPE) {
1220 if (flags & CF_UNSIGNED) {
1221 AddCodeLine ("jsr tosulong");
1223 AddCodeLine ("jsr toslong");
1238 void g_tosint (unsigned flags)
1239 /* Make sure, the value on TOS is an int. Convert if necessary */
1241 switch (flags & CF_TYPE) {
1248 AddCodeLine ("jsr tosint");
1259 void g_regint (unsigned Flags)
1260 /* Make sure, the value in the primary register an int. Convert if necessary */
1264 switch (Flags & CF_TYPE) {
1267 if (Flags & CF_FORCECHAR) {
1268 /* Conversion is from char */
1269 if (Flags & CF_UNSIGNED) {
1270 AddCodeLine ("ldx #$00");
1272 L = GetLocalLabel();
1273 AddCodeLine ("ldx #$00");
1274 AddCodeLine ("cmp #$80");
1275 AddCodeLine ("bcc %s", LocalLabelName (L));
1276 AddCodeLine ("dex");
1293 void g_reglong (unsigned Flags)
1294 /* Make sure, the value in the primary register a long. Convert if necessary */
1298 switch (Flags & CF_TYPE) {
1301 if (Flags & CF_FORCECHAR) {
1302 /* Conversion is from char */
1303 if (Flags & CF_UNSIGNED) {
1304 if (IS_Get (&CodeSizeFactor) >= 200) {
1305 AddCodeLine ("ldx #$00");
1306 AddCodeLine ("stx sreg");
1307 AddCodeLine ("stx sreg+1");
1309 AddCodeLine ("jsr aulong");
1312 if (IS_Get (&CodeSizeFactor) >= 366) {
1313 L = GetLocalLabel();
1314 AddCodeLine ("ldx #$00");
1315 AddCodeLine ("cmp #$80");
1316 AddCodeLine ("bcc %s", LocalLabelName (L));
1317 AddCodeLine ("dex");
1319 AddCodeLine ("stx sreg");
1320 AddCodeLine ("stx sreg+1");
1322 AddCodeLine ("jsr along");
1329 if (Flags & CF_UNSIGNED) {
1330 if (IS_Get (&CodeSizeFactor) >= 200) {
1332 AddCodeLine ("sty sreg");
1333 AddCodeLine ("sty sreg+1");
1335 AddCodeLine ("jsr axulong");
1338 AddCodeLine ("jsr axlong");
1352 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1353 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1354 * value, that corresponds to the value on TOS, rhs corresponds to the value
1355 * in (e)ax. The return value is the the flags value for the resulting type.
1358 unsigned ltype, rtype;
1361 /* Get the type spec from the flags */
1362 ltype = lhs & CF_TYPE;
1363 rtype = rhs & CF_TYPE;
1365 /* Check if a conversion is needed */
1366 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1367 /* We must promote the primary register to long */
1369 /* Get the new rhs type */
1370 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1372 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1373 /* We must promote the lhs to long */
1379 /* Get the new rhs type */
1380 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1384 /* Determine the result type for the operation:
1385 * - The result is const if both operands are const.
1386 * - The result is unsigned if one of the operands is unsigned.
1387 * - The result is long if one of the operands is long.
1388 * - Otherwise the result is int sized.
1390 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1391 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1392 if (rtype == CF_LONG || ltype == CF_LONG) {
1402 unsigned g_typecast (unsigned lhs, unsigned rhs)
1403 /* Cast the value in the primary register to the operand size that is flagged
1404 * by the lhs value. Return the result value.
1407 unsigned ltype, rtype;
1409 /* Get the type spec from the flags */
1410 ltype = lhs & CF_TYPE;
1411 rtype = rhs & CF_TYPE;
1413 /* Check if a conversion is needed */
1414 if ((rhs & CF_CONST) == 0) {
1415 if (ltype == CF_LONG && rtype != CF_LONG) {
1416 /* We must promote the primary register to long */
1418 } else if (ltype == CF_INT && rtype != CF_INT) {
1419 /* We must promote the primary register to int */
1424 /* Do not need any other action. If the left type is int, and the primary
1425 * register is long, it will be automagically truncated. If the right hand
1426 * side is const, it is not located in the primary register and handled by
1427 * the expression parser code.
1430 /* Result is const if the right hand side was const */
1431 lhs |= (rhs & CF_CONST);
1433 /* The resulting type is that of the left hand side (that's why you called
1441 void g_scale (unsigned flags, long val)
1442 /* Scale the value in the primary register by the given value. If val is positive,
1443 * scale up, is val is negative, scale down. This function is used to scale
1444 * the operands or results of pointer arithmetic by the size of the type, the
1445 * pointer points to.
1450 /* Value may not be zero */
1452 Internal ("Data type has no size");
1453 } else if (val > 0) {
1456 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1458 /* Factor is 2, 4, 8 and 16, use special function */
1459 switch (flags & CF_TYPE) {
1462 if (flags & CF_FORCECHAR) {
1464 AddCodeLine ("asl a");
1471 if (flags & CF_UNSIGNED) {
1472 AddCodeLine ("jsr shlax%d", p2);
1474 AddCodeLine ("jsr aslax%d", p2);
1479 if (flags & CF_UNSIGNED) {
1480 AddCodeLine ("jsr shleax%d", p2);
1482 AddCodeLine ("jsr asleax%d", p2);
1491 } else if (val != 1) {
1493 /* Use a multiplication instead */
1494 g_mul (flags | CF_CONST, val);
1502 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1504 /* Factor is 2, 4, 8 and 16 use special function */
1505 switch (flags & CF_TYPE) {
1508 if (flags & CF_FORCECHAR) {
1509 if (flags & CF_UNSIGNED) {
1511 AddCodeLine ("lsr a");
1514 } else if (p2 <= 2) {
1515 AddCodeLine ("cmp #$80");
1516 AddCodeLine ("ror a");
1523 if (flags & CF_UNSIGNED) {
1524 AddCodeLine ("jsr lsrax%d", p2);
1526 AddCodeLine ("jsr asrax%d", p2);
1531 if (flags & CF_UNSIGNED) {
1532 AddCodeLine ("jsr lsreax%d", p2);
1534 AddCodeLine ("jsr asreax%d", p2);
1543 } else if (val != 1) {
1545 /* Use a division instead */
1546 g_div (flags | CF_CONST, val);
1554 /*****************************************************************************/
1555 /* Adds and subs of variables fix a fixed address */
1556 /*****************************************************************************/
1560 void g_addlocal (unsigned flags, int offs)
1561 /* Add a local variable to ax */
1565 /* Correct the offset and check it */
1567 CheckLocalOffs (offs);
1569 switch (flags & CF_TYPE) {
1572 L = GetLocalLabel();
1573 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1574 AddCodeLine ("clc");
1575 AddCodeLine ("adc (sp),y");
1576 AddCodeLine ("bcc %s", LocalLabelName (L));
1577 AddCodeLine ("inx");
1582 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1583 AddCodeLine ("clc");
1584 AddCodeLine ("adc (sp),y");
1585 AddCodeLine ("pha");
1586 AddCodeLine ("txa");
1587 AddCodeLine ("iny");
1588 AddCodeLine ("adc (sp),y");
1589 AddCodeLine ("tax");
1590 AddCodeLine ("pla");
1594 /* Do it the old way */
1596 g_getlocal (flags, offs);
1608 void g_addstatic (unsigned flags, unsigned long label, long offs)
1609 /* Add a static variable to ax */
1613 /* Create the correct label name */
1614 const char* lbuf = GetLabelName (flags, label, offs);
1616 switch (flags & CF_TYPE) {
1619 L = GetLocalLabel();
1620 AddCodeLine ("clc");
1621 AddCodeLine ("adc %s", lbuf);
1622 AddCodeLine ("bcc %s", LocalLabelName (L));
1623 AddCodeLine ("inx");
1628 AddCodeLine ("clc");
1629 AddCodeLine ("adc %s", lbuf);
1630 AddCodeLine ("tay");
1631 AddCodeLine ("txa");
1632 AddCodeLine ("adc %s+1", lbuf);
1633 AddCodeLine ("tax");
1634 AddCodeLine ("tya");
1638 /* Do it the old way */
1640 g_getstatic (flags, label, offs);
1652 /*****************************************************************************/
1653 /* Special op= functions */
1654 /*****************************************************************************/
1658 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1660 /* Emit += for a static variable */
1662 /* Create the correct label name */
1663 const char* lbuf = GetLabelName (flags, label, offs);
1665 /* Check the size and determine operation */
1666 switch (flags & CF_TYPE) {
1669 if (flags & CF_FORCECHAR) {
1670 AddCodeLine ("ldx #$00");
1671 if (flags & CF_CONST) {
1673 AddCodeLine ("inc %s", lbuf);
1674 AddCodeLine ("lda %s", lbuf);
1676 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1677 AddCodeLine ("clc");
1678 AddCodeLine ("adc %s", lbuf);
1679 AddCodeLine ("sta %s", lbuf);
1682 AddCodeLine ("clc");
1683 AddCodeLine ("adc %s", lbuf);
1684 AddCodeLine ("sta %s", lbuf);
1686 if ((flags & CF_UNSIGNED) == 0) {
1687 unsigned L = GetLocalLabel();
1688 AddCodeLine ("bpl %s", LocalLabelName (L));
1689 AddCodeLine ("dex");
1697 if (flags & CF_CONST) {
1699 unsigned L = GetLocalLabel ();
1700 AddCodeLine ("inc %s", lbuf);
1701 AddCodeLine ("bne %s", LocalLabelName (L));
1702 AddCodeLine ("inc %s+1", lbuf);
1704 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1705 AddCodeLine ("ldx %s+1", lbuf);
1707 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1708 AddCodeLine ("clc");
1709 AddCodeLine ("adc %s", lbuf);
1710 AddCodeLine ("sta %s", lbuf);
1712 unsigned L = GetLocalLabel ();
1713 AddCodeLine ("bcc %s", LocalLabelName (L));
1714 AddCodeLine ("inc %s+1", lbuf);
1716 AddCodeLine ("ldx %s+1", lbuf);
1718 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1719 AddCodeLine ("adc %s+1", lbuf);
1720 AddCodeLine ("sta %s+1", lbuf);
1721 AddCodeLine ("tax");
1722 AddCodeLine ("lda %s", lbuf);
1726 AddCodeLine ("clc");
1727 AddCodeLine ("adc %s", lbuf);
1728 AddCodeLine ("sta %s", lbuf);
1729 AddCodeLine ("txa");
1730 AddCodeLine ("adc %s+1", lbuf);
1731 AddCodeLine ("sta %s+1", lbuf);
1732 AddCodeLine ("tax");
1733 AddCodeLine ("lda %s", lbuf);
1738 if (flags & CF_CONST) {
1740 AddCodeLine ("ldy #<(%s)", lbuf);
1741 AddCodeLine ("sty ptr1");
1742 AddCodeLine ("ldy #>(%s)", lbuf);
1744 AddCodeLine ("jsr laddeq1");
1746 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1747 AddCodeLine ("jsr laddeqa");
1750 g_getstatic (flags, label, offs);
1752 g_putstatic (flags, label, offs);
1755 AddCodeLine ("ldy #<(%s)", lbuf);
1756 AddCodeLine ("sty ptr1");
1757 AddCodeLine ("ldy #>(%s)", lbuf);
1758 AddCodeLine ("jsr laddeq");
1769 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1770 /* Emit += for a local variable */
1772 /* Calculate the true offset, check it, load it into Y */
1774 CheckLocalOffs (offs);
1776 /* Check the size and determine operation */
1777 switch (flags & CF_TYPE) {
1780 if (flags & CF_FORCECHAR) {
1782 AddCodeLine ("ldx #$00");
1783 if (flags & CF_CONST) {
1784 AddCodeLine ("clc");
1785 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1786 AddCodeLine ("adc (sp),y");
1787 AddCodeLine ("sta (sp),y");
1789 AddCodeLine ("clc");
1790 AddCodeLine ("adc (sp),y");
1791 AddCodeLine ("sta (sp),y");
1793 if ((flags & CF_UNSIGNED) == 0) {
1794 unsigned L = GetLocalLabel();
1795 AddCodeLine ("bpl %s", LocalLabelName (L));
1796 AddCodeLine ("dex");
1805 if (flags & CF_CONST) {
1806 if (IS_Get (&CodeSizeFactor) >= 400) {
1807 AddCodeLine ("clc");
1808 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1809 AddCodeLine ("adc (sp),y");
1810 AddCodeLine ("sta (sp),y");
1811 AddCodeLine ("iny");
1812 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1813 AddCodeLine ("adc (sp),y");
1814 AddCodeLine ("sta (sp),y");
1815 AddCodeLine ("tax");
1816 AddCodeLine ("dey");
1817 AddCodeLine ("lda (sp),y");
1819 g_getimmed (flags, val, 0);
1820 AddCodeLine ("jsr addeqysp");
1823 AddCodeLine ("jsr addeqysp");
1828 if (flags & CF_CONST) {
1829 g_getimmed (flags, val, 0);
1832 AddCodeLine ("jsr laddeqysp");
1842 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1843 /* Emit += for the location with address in ax */
1845 /* If the offset is too large for a byte register, add the high byte
1846 * of the offset to the primary. Beware: We need a special correction
1847 * if the offset in the low byte will overflow in the operation.
1849 offs = MakeByteOffs (flags, offs);
1851 /* Check the size and determine operation */
1852 switch (flags & CF_TYPE) {
1855 AddCodeLine ("sta ptr1");
1856 AddCodeLine ("stx ptr1+1");
1857 AddCodeLine ("ldy #$%02X", offs);
1858 AddCodeLine ("ldx #$00");
1859 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1860 AddCodeLine ("clc");
1861 AddCodeLine ("adc (ptr1),y");
1862 AddCodeLine ("sta (ptr1),y");
1866 if (IS_Get (&CodeSizeFactor) >= 200) {
1867 /* Lots of code, use only if size is not important */
1868 AddCodeLine ("sta ptr1");
1869 AddCodeLine ("stx ptr1+1");
1870 AddCodeLine ("ldy #$%02X", offs);
1871 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1872 AddCodeLine ("clc");
1873 AddCodeLine ("adc (ptr1),y");
1874 AddCodeLine ("sta (ptr1),y");
1875 AddCodeLine ("pha");
1876 AddCodeLine ("iny");
1877 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1878 AddCodeLine ("adc (ptr1),y");
1879 AddCodeLine ("sta (ptr1),y");
1880 AddCodeLine ("tax");
1881 AddCodeLine ("pla");
1887 AddCodeLine ("jsr pushax"); /* Push the address */
1888 push (CF_PTR); /* Correct the internal sp */
1889 g_getind (flags, offs); /* Fetch the value */
1890 g_inc (flags, val); /* Increment value in primary */
1891 g_putind (flags, offs); /* Store the value back */
1901 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1903 /* Emit -= for a static variable */
1905 /* Create the correct label name */
1906 const char* lbuf = GetLabelName (flags, label, offs);
1908 /* Check the size and determine operation */
1909 switch (flags & CF_TYPE) {
1912 if (flags & CF_FORCECHAR) {
1913 AddCodeLine ("ldx #$00");
1914 if (flags & CF_CONST) {
1916 AddCodeLine ("dec %s", lbuf);
1917 AddCodeLine ("lda %s", lbuf);
1919 AddCodeLine ("lda %s", lbuf);
1920 AddCodeLine ("sec");
1921 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1922 AddCodeLine ("sta %s", lbuf);
1925 AddCodeLine ("eor #$FF");
1926 AddCodeLine ("sec");
1927 AddCodeLine ("adc %s", lbuf);
1928 AddCodeLine ("sta %s", lbuf);
1930 if ((flags & CF_UNSIGNED) == 0) {
1931 unsigned L = GetLocalLabel();
1932 AddCodeLine ("bpl %s", LocalLabelName (L));
1933 AddCodeLine ("dex");
1941 if (flags & CF_CONST) {
1942 AddCodeLine ("lda %s", lbuf);
1943 AddCodeLine ("sec");
1944 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1945 AddCodeLine ("sta %s", lbuf);
1947 unsigned L = GetLocalLabel ();
1948 AddCodeLine ("bcs %s", LocalLabelName (L));
1949 AddCodeLine ("dec %s+1", lbuf);
1951 AddCodeLine ("ldx %s+1", lbuf);
1953 AddCodeLine ("lda %s+1", lbuf);
1954 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1955 AddCodeLine ("sta %s+1", lbuf);
1956 AddCodeLine ("tax");
1957 AddCodeLine ("lda %s", lbuf);
1960 AddCodeLine ("eor #$FF");
1961 AddCodeLine ("sec");
1962 AddCodeLine ("adc %s", lbuf);
1963 AddCodeLine ("sta %s", lbuf);
1964 AddCodeLine ("txa");
1965 AddCodeLine ("eor #$FF");
1966 AddCodeLine ("adc %s+1", lbuf);
1967 AddCodeLine ("sta %s+1", lbuf);
1968 AddCodeLine ("tax");
1969 AddCodeLine ("lda %s", lbuf);
1974 if (flags & CF_CONST) {
1976 AddCodeLine ("ldy #<(%s)", lbuf);
1977 AddCodeLine ("sty ptr1");
1978 AddCodeLine ("ldy #>(%s)", lbuf);
1979 AddCodeLine ("lda #$%02X", (unsigned char)val);
1980 AddCodeLine ("jsr lsubeqa");
1982 g_getstatic (flags, label, offs);
1984 g_putstatic (flags, label, offs);
1987 AddCodeLine ("ldy #<(%s)", lbuf);
1988 AddCodeLine ("sty ptr1");
1989 AddCodeLine ("ldy #>(%s)", lbuf);
1990 AddCodeLine ("jsr lsubeq");
2001 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
2002 /* Emit -= for a local variable */
2004 /* Calculate the true offset, check it, load it into Y */
2006 CheckLocalOffs (offs);
2008 /* Check the size and determine operation */
2009 switch (flags & CF_TYPE) {
2012 if (flags & CF_FORCECHAR) {
2014 AddCodeLine ("ldx #$00");
2015 if (flags & CF_CONST) {
2016 AddCodeLine ("lda (sp),y");
2017 AddCodeLine ("sec");
2018 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2020 AddCodeLine ("eor #$FF");
2021 AddCodeLine ("sec");
2022 AddCodeLine ("adc (sp),y");
2024 AddCodeLine ("sta (sp),y");
2025 if ((flags & CF_UNSIGNED) == 0) {
2026 unsigned L = GetLocalLabel();
2027 AddCodeLine ("bpl %s", LocalLabelName (L));
2028 AddCodeLine ("dex");
2036 if (flags & CF_CONST) {
2037 g_getimmed (flags, val, 0);
2040 AddCodeLine ("jsr subeqysp");
2044 if (flags & CF_CONST) {
2045 g_getimmed (flags, val, 0);
2048 AddCodeLine ("jsr lsubeqysp");
2058 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2059 /* Emit -= for the location with address in ax */
2061 /* If the offset is too large for a byte register, add the high byte
2062 * of the offset to the primary. Beware: We need a special correction
2063 * if the offset in the low byte will overflow in the operation.
2065 offs = MakeByteOffs (flags, offs);
2067 /* Check the size and determine operation */
2068 switch (flags & CF_TYPE) {
2071 AddCodeLine ("sta ptr1");
2072 AddCodeLine ("stx ptr1+1");
2073 AddCodeLine ("ldy #$%02X", offs);
2074 AddCodeLine ("ldx #$00");
2075 AddCodeLine ("lda (ptr1),y");
2076 AddCodeLine ("sec");
2077 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2078 AddCodeLine ("sta (ptr1),y");
2082 if (IS_Get (&CodeSizeFactor) >= 200) {
2083 /* Lots of code, use only if size is not important */
2084 AddCodeLine ("sta ptr1");
2085 AddCodeLine ("stx ptr1+1");
2086 AddCodeLine ("ldy #$%02X", offs);
2087 AddCodeLine ("lda (ptr1),y");
2088 AddCodeLine ("sec");
2089 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2090 AddCodeLine ("sta (ptr1),y");
2091 AddCodeLine ("pha");
2092 AddCodeLine ("iny");
2093 AddCodeLine ("lda (ptr1),y");
2094 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2095 AddCodeLine ("sta (ptr1),y");
2096 AddCodeLine ("tax");
2097 AddCodeLine ("pla");
2103 AddCodeLine ("jsr pushax"); /* Push the address */
2104 push (CF_PTR); /* Correct the internal sp */
2105 g_getind (flags, offs); /* Fetch the value */
2106 g_dec (flags, val); /* Increment value in primary */
2107 g_putind (flags, offs); /* Store the value back */
2117 /*****************************************************************************/
2118 /* Add a variable address to the value in ax */
2119 /*****************************************************************************/
2123 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2124 /* Add the address of a local variable to ax */
2128 /* Add the offset */
2131 /* We cannot address more then 256 bytes of locals anyway */
2132 L = GetLocalLabel();
2133 CheckLocalOffs (offs);
2134 AddCodeLine ("clc");
2135 AddCodeLine ("adc #$%02X", offs & 0xFF);
2136 /* Do also skip the CLC insn below */
2137 AddCodeLine ("bcc %s", LocalLabelName (L));
2138 AddCodeLine ("inx");
2141 /* Add the current stackpointer value */
2142 AddCodeLine ("clc");
2144 /* Label was used above */
2147 AddCodeLine ("adc sp");
2148 AddCodeLine ("tay");
2149 AddCodeLine ("txa");
2150 AddCodeLine ("adc sp+1");
2151 AddCodeLine ("tax");
2152 AddCodeLine ("tya");
2157 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2158 /* Add the address of a static variable to ax */
2160 /* Create the correct label name */
2161 const char* lbuf = GetLabelName (flags, label, offs);
2163 /* Add the address to the current ax value */
2164 AddCodeLine ("clc");
2165 AddCodeLine ("adc #<(%s)", lbuf);
2166 AddCodeLine ("tay");
2167 AddCodeLine ("txa");
2168 AddCodeLine ("adc #>(%s)", lbuf);
2169 AddCodeLine ("tax");
2170 AddCodeLine ("tya");
2175 /*****************************************************************************/
2177 /*****************************************************************************/
2181 void g_save (unsigned flags)
2182 /* Copy primary register to hold register. */
2184 /* Check the size and determine operation */
2185 switch (flags & CF_TYPE) {
2188 if (flags & CF_FORCECHAR) {
2189 AddCodeLine ("pha");
2195 AddCodeLine ("sta regsave");
2196 AddCodeLine ("stx regsave+1");
2200 AddCodeLine ("jsr saveeax");
2210 void g_restore (unsigned flags)
2211 /* Copy hold register to primary. */
2213 /* Check the size and determine operation */
2214 switch (flags & CF_TYPE) {
2217 if (flags & CF_FORCECHAR) {
2218 AddCodeLine ("pla");
2224 AddCodeLine ("lda regsave");
2225 AddCodeLine ("ldx regsave+1");
2229 AddCodeLine ("jsr resteax");
2239 void g_cmp (unsigned flags, unsigned long val)
2240 /* Immidiate compare. The primary register will not be changed, Z flag
2246 /* Check the size and determine operation */
2247 switch (flags & CF_TYPE) {
2250 if (flags & CF_FORCECHAR) {
2251 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2257 L = GetLocalLabel();
2258 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2259 AddCodeLine ("bne %s", LocalLabelName (L));
2260 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2265 Internal ("g_cmp: Long compares not implemented");
2275 static void oper (unsigned Flags, unsigned long Val, const char** Subs)
2276 /* Encode a binary operation. subs is a pointer to four strings:
2277 * 0 --> Operate on ints
2278 * 1 --> Operate on unsigneds
2279 * 2 --> Operate on longs
2280 * 3 --> Operate on unsigned longs
2283 /* Determine the offset into the array */
2284 if (Flags & CF_UNSIGNED) {
2287 if ((Flags & CF_TYPE) == CF_LONG) {
2291 /* Load the value if it is not already in the primary */
2292 if (Flags & CF_CONST) {
2294 g_getimmed (Flags, Val, 0);
2297 /* Output the operation */
2298 AddCodeLine ("jsr %s", *Subs);
2300 /* The operation will pop it's argument */
2306 void g_test (unsigned flags)
2307 /* Test the value in the primary and set the condition codes */
2309 switch (flags & CF_TYPE) {
2312 if (flags & CF_FORCECHAR) {
2313 AddCodeLine ("tax");
2319 AddCodeLine ("stx tmp1");
2320 AddCodeLine ("ora tmp1");
2324 if (flags & CF_UNSIGNED) {
2325 AddCodeLine ("jsr utsteax");
2327 AddCodeLine ("jsr tsteax");
2339 void g_push (unsigned flags, unsigned long val)
2340 /* Push the primary register or a constant value onto the stack */
2342 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2344 /* We have a constant 8 or 16 bit value */
2345 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2347 /* Handle as 8 bit value */
2349 AddCodeLine ("jsr pusha");
2353 /* Handle as 16 bit value */
2354 g_getimmed (flags, val, 0);
2355 AddCodeLine ("jsr pushax");
2360 /* Value is not 16 bit or not constant */
2361 if (flags & CF_CONST) {
2362 /* Constant 32 bit value, load into eax */
2363 g_getimmed (flags, val, 0);
2366 /* Push the primary register */
2367 switch (flags & CF_TYPE) {
2370 if (flags & CF_FORCECHAR) {
2371 /* Handle as char */
2372 AddCodeLine ("jsr pusha");
2377 AddCodeLine ("jsr pushax");
2381 AddCodeLine ("jsr pusheax");
2391 /* Adjust the stack offset */
2397 void g_swap (unsigned flags)
2398 /* Swap the primary register and the top of the stack. flags give the type
2399 * of *both* values (must have same size).
2402 switch (flags & CF_TYPE) {
2406 AddCodeLine ("jsr swapstk");
2410 AddCodeLine ("jsr swapestk");
2421 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2422 /* Call the specified subroutine name */
2424 if ((Flags & CF_FIXARGC) == 0) {
2425 /* Pass the argument count */
2428 AddCodeLine ("jsr _%s", Label);
2429 StackPtr += ArgSize; /* callee pops args */
2434 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2435 /* Call subroutine indirect */
2437 if ((Flags & CF_LOCAL) == 0) {
2438 /* Address is in a/x */
2439 if ((Flags & CF_FIXARGC) == 0) {
2440 /* Pass arg count */
2443 AddCodeLine ("jsr callax");
2445 /* The address is on stack, offset is on Val */
2447 CheckLocalOffs (Offs);
2448 AddCodeLine ("pha");
2449 AddCodeLine ("ldy #$%02X", Offs);
2450 AddCodeLine ("lda (sp),y");
2451 AddCodeLine ("sta jmpvec+1");
2452 AddCodeLine ("iny");
2453 AddCodeLine ("lda (sp),y");
2454 AddCodeLine ("sta jmpvec+2");
2455 AddCodeLine ("pla");
2456 AddCodeLine ("jsr jmpvec");
2459 /* Callee pops args */
2460 StackPtr += ArgSize;
2465 void g_jump (unsigned Label)
2466 /* Jump to specified internal label number */
2468 AddCodeLine ("jmp %s", LocalLabelName (Label));
2473 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2474 /* Jump to label if zero flag clear */
2476 AddCodeLine ("jne %s", LocalLabelName (label));
2481 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2482 /* Jump to label if zero flag set */
2484 AddCodeLine ("jeq %s", LocalLabelName (label));
2489 static void mod_internal (int k, char* verb1, char* verb2)
2492 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2496 AddCodeLine ("jsr %ssp", verb2);
2502 void g_space (int space)
2503 /* Create or drop space on the stack */
2506 mod_internal (-space, "inc", "addy");
2507 } else if (space > 0) {
2508 mod_internal (space, "dec", "suby");
2514 void g_cstackcheck (void)
2515 /* Check for a C stack overflow */
2517 AddCodeLine ("jsr cstkchk");
2522 void g_stackcheck (void)
2523 /* Check for a stack overflow */
2525 AddCodeLine ("jsr stkchk");
2530 void g_add (unsigned flags, unsigned long val)
2531 /* Primary = TOS + Primary */
2533 static const char* ops[12] = {
2534 "tosaddax", "tosaddax", "tosaddeax", "tosaddeax"
2537 if (flags & CF_CONST) {
2538 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2539 g_push (flags & ~CF_CONST, 0);
2541 oper (flags, val, ops);
2546 void g_sub (unsigned flags, unsigned long val)
2547 /* Primary = TOS - Primary */
2549 static const char* ops[12] = {
2550 "tossubax", "tossubax", "tossubeax", "tossubeax",
2553 if (flags & CF_CONST) {
2554 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2555 g_push (flags & ~CF_CONST, 0);
2557 oper (flags, val, ops);
2562 void g_rsub (unsigned flags, unsigned long val)
2563 /* Primary = Primary - TOS */
2565 static const char* ops[12] = {
2566 "tosrsubax", "tosrsubax", "tosrsubeax", "tosrsubeax",
2568 oper (flags, val, ops);
2573 void g_mul (unsigned flags, unsigned long val)
2574 /* Primary = TOS * Primary */
2576 static const char* ops[12] = {
2577 "tosmulax", "tosumulax", "tosmuleax", "tosumuleax",
2582 /* Do strength reduction if the value is constant and a power of two */
2583 if (flags & CF_CONST && (p2 = PowerOf2 (val)) >= 0) {
2584 /* Generate a shift instead */
2589 /* If the right hand side is const, the lhs is not on stack but still
2590 * in the primary register.
2592 if (flags & CF_CONST) {
2594 switch (flags & CF_TYPE) {
2597 if (flags & CF_FORCECHAR) {
2598 /* Handle some special cases */
2602 AddCodeLine ("sta tmp1");
2603 AddCodeLine ("asl a");
2604 AddCodeLine ("clc");
2605 AddCodeLine ("adc tmp1");
2609 AddCodeLine ("sta tmp1");
2610 AddCodeLine ("asl a");
2611 AddCodeLine ("asl a");
2612 AddCodeLine ("clc");
2613 AddCodeLine ("adc tmp1");
2617 AddCodeLine ("sta tmp1");
2618 AddCodeLine ("asl a");
2619 AddCodeLine ("clc");
2620 AddCodeLine ("adc tmp1");
2621 AddCodeLine ("asl a");
2625 AddCodeLine ("sta tmp1");
2626 AddCodeLine ("asl a");
2627 AddCodeLine ("asl a");
2628 AddCodeLine ("clc");
2629 AddCodeLine ("adc tmp1");
2630 AddCodeLine ("asl a");
2639 AddCodeLine ("jsr mulax3");
2642 AddCodeLine ("jsr mulax5");
2645 AddCodeLine ("jsr mulax6");
2648 AddCodeLine ("jsr mulax7");
2651 AddCodeLine ("jsr mulax9");
2654 AddCodeLine ("jsr mulax10");
2666 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2667 * into the normal, non-optimized stuff.
2669 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2670 g_push (flags & ~CF_CONST, 0);
2674 /* Use long way over the stack */
2675 oper (flags, val, ops);
2680 void g_div (unsigned flags, unsigned long val)
2681 /* Primary = TOS / Primary */
2683 static const char* ops[12] = {
2684 "tosdivax", "tosudivax", "tosdiveax", "tosudiveax",
2687 /* Do strength reduction if the value is constant and a power of two */
2689 if ((flags & CF_CONST) && (p2 = PowerOf2 (val)) >= 0) {
2690 /* Generate a shift instead */
2693 /* Generate a division */
2694 if (flags & CF_CONST) {
2695 /* lhs is not on stack */
2696 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2697 g_push (flags & ~CF_CONST, 0);
2699 oper (flags, val, ops);
2705 void g_mod (unsigned flags, unsigned long val)
2706 /* Primary = TOS % Primary */
2708 static const char* ops[12] = {
2709 "tosmodax", "tosumodax", "tosmodeax", "tosumodeax",
2713 /* Check if we can do some cost reduction */
2714 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = PowerOf2 (val)) >= 0) {
2715 /* We can do that with an AND operation */
2716 g_and (flags, val - 1);
2718 /* Do it the hard way... */
2719 if (flags & CF_CONST) {
2720 /* lhs is not on stack */
2721 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2722 g_push (flags & ~CF_CONST, 0);
2724 oper (flags, val, ops);
2730 void g_or (unsigned flags, unsigned long val)
2731 /* Primary = TOS | Primary */
2733 static const char* ops[12] = {
2734 "tosorax", "tosorax", "tosoreax", "tosoreax",
2737 /* If the right hand side is const, the lhs is not on stack but still
2738 * in the primary register.
2740 if (flags & CF_CONST) {
2742 switch (flags & CF_TYPE) {
2745 if (flags & CF_FORCECHAR) {
2746 if ((val & 0xFF) != 0) {
2747 AddCodeLine ("ora #$%02X", (unsigned char)val);
2755 if ((val & 0xFF) != 0) {
2756 AddCodeLine ("ora #$%02X", (unsigned char)val);
2758 } else if ((val & 0xFF00) == 0xFF00) {
2759 if ((val & 0xFF) != 0) {
2760 AddCodeLine ("ora #$%02X", (unsigned char)val);
2763 } else if (val != 0) {
2764 AddCodeLine ("ora #$%02X", (unsigned char)val);
2765 AddCodeLine ("pha");
2766 AddCodeLine ("txa");
2767 AddCodeLine ("ora #$%02X", (unsigned char)(val >> 8));
2768 AddCodeLine ("tax");
2769 AddCodeLine ("pla");
2775 if ((val & 0xFF) != 0) {
2776 AddCodeLine ("ora #$%02X", (unsigned char)val);
2786 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2787 * into the normal, non-optimized stuff. Note: The standard stuff will
2788 * always work with ints.
2790 flags &= ~CF_FORCECHAR;
2791 g_push (flags & ~CF_CONST, 0);
2794 /* Use long way over the stack */
2795 oper (flags, val, ops);
2800 void g_xor (unsigned flags, unsigned long val)
2801 /* Primary = TOS ^ Primary */
2803 static const char* ops[12] = {
2804 "tosxorax", "tosxorax", "tosxoreax", "tosxoreax",
2808 /* If the right hand side is const, the lhs is not on stack but still
2809 * in the primary register.
2811 if (flags & CF_CONST) {
2813 switch (flags & CF_TYPE) {
2816 if (flags & CF_FORCECHAR) {
2817 if ((val & 0xFF) != 0) {
2818 AddCodeLine ("eor #$%02X", (unsigned char)val);
2827 AddCodeLine ("eor #$%02X", (unsigned char)val);
2829 } else if (val != 0) {
2830 if ((val & 0xFF) != 0) {
2831 AddCodeLine ("eor #$%02X", (unsigned char)val);
2833 AddCodeLine ("pha");
2834 AddCodeLine ("txa");
2835 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2836 AddCodeLine ("tax");
2837 AddCodeLine ("pla");
2844 AddCodeLine ("eor #$%02X", (unsigned char)val);
2854 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2855 * into the normal, non-optimized stuff. Note: The standard stuff will
2856 * always work with ints.
2858 flags &= ~CF_FORCECHAR;
2859 g_push (flags & ~CF_CONST, 0);
2862 /* Use long way over the stack */
2863 oper (flags, val, ops);
2868 void g_and (unsigned Flags, unsigned long Val)
2869 /* Primary = TOS & Primary */
2871 static const char* ops[12] = {
2872 "tosandax", "tosandax", "tosandeax", "tosandeax",
2875 /* If the right hand side is const, the lhs is not on stack but still
2876 * in the primary register.
2878 if (Flags & CF_CONST) {
2880 switch (Flags & CF_TYPE) {
2883 if (Flags & CF_FORCECHAR) {
2884 if ((Val & 0xFF) == 0x00) {
2885 AddCodeLine ("lda #$00");
2886 } else if ((Val & 0xFF) != 0xFF) {
2887 AddCodeLine ("and #$%02X", (unsigned char)Val);
2893 if ((Val & 0xFFFF) != 0xFFFF) {
2897 AddCodeLine ("lda #$00");
2898 } else if (Val != 0xFF) {
2899 AddCodeLine ("and #$%02X", (unsigned char)Val);
2901 } else if ((Val & 0xFFFF) == 0xFF00) {
2902 AddCodeLine ("lda #$00");
2903 } else if ((Val & 0xFF00) == 0xFF00) {
2904 AddCodeLine ("and #$%02X", (unsigned char)Val);
2905 } else if ((Val & 0x00FF) == 0x0000) {
2906 AddCodeLine ("txa");
2907 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2908 AddCodeLine ("tax");
2909 AddCodeLine ("lda #$00");
2911 AddCodeLine ("tay");
2912 AddCodeLine ("txa");
2913 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2914 AddCodeLine ("tax");
2915 AddCodeLine ("tya");
2916 if ((Val & 0x00FF) == 0x0000) {
2917 AddCodeLine ("lda #$00");
2918 } else if ((Val & 0x00FF) != 0x00FF) {
2919 AddCodeLine ("and #$%02X", (unsigned char)Val);
2928 AddCodeLine ("stx sreg+1");
2929 AddCodeLine ("stx sreg");
2930 if ((Val & 0xFF) != 0xFF) {
2931 AddCodeLine ("and #$%02X", (unsigned char)Val);
2934 } else if (Val == 0xFF00) {
2936 AddCodeLine ("sta sreg+1");
2937 AddCodeLine ("sta sreg");
2946 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2947 * into the normal, non-optimized stuff. Note: The standard stuff will
2948 * always work with ints.
2950 Flags &= ~CF_FORCECHAR;
2951 g_push (Flags & ~CF_CONST, 0);
2954 /* Use long way over the stack */
2955 oper (Flags, Val, ops);
2960 void g_asr (unsigned flags, unsigned long val)
2961 /* Primary = TOS >> Primary */
2963 static const char* ops[12] = {
2964 "tosasrax", "tosshrax", "tosasreax", "tosshreax",
2967 /* If the right hand side is const, the lhs is not on stack but still
2968 * in the primary register.
2970 if (flags & CF_CONST) {
2972 switch (flags & CF_TYPE) {
2978 if (flags & CF_UNSIGNED) {
2979 AddCodeLine ("txa");
2982 unsigned L = GetLocalLabel();
2983 AddCodeLine ("cpx #$80"); /* Sign bit into carry */
2984 AddCodeLine ("txa");
2985 AddCodeLine ("ldx #$00");
2986 AddCodeLine ("bcc %s", LocalLabelName (L));
2987 AddCodeLine ("dex"); /* Make $FF */
2993 if (flags & CF_UNSIGNED) {
2994 AddCodeLine ("jsr shrax4");
2996 AddCodeLine ("jsr asrax4");
3001 if (flags & CF_UNSIGNED) {
3002 AddCodeLine ("jsr shrax%ld", val);
3004 AddCodeLine ("jsr asrax%ld", val);
3012 AddCodeLine ("ldx #$00");
3013 AddCodeLine ("lda sreg+1");
3014 if ((flags & CF_UNSIGNED) == 0) {
3015 unsigned L = GetLocalLabel();
3016 AddCodeLine ("bpl %s", LocalLabelName (L));
3017 AddCodeLine ("dex");
3020 AddCodeLine ("stx sreg");
3021 AddCodeLine ("stx sreg+1");
3025 AddCodeLine ("ldy #$00");
3026 AddCodeLine ("ldx sreg+1");
3027 if ((flags & CF_UNSIGNED) == 0) {
3028 unsigned L = GetLocalLabel();
3029 AddCodeLine ("bpl %s", LocalLabelName (L));
3030 AddCodeLine ("dey");
3033 AddCodeLine ("lda sreg");
3034 AddCodeLine ("sty sreg+1");
3035 AddCodeLine ("sty sreg");
3039 AddCodeLine ("txa");
3040 AddCodeLine ("ldx sreg");
3041 AddCodeLine ("ldy sreg+1");
3042 AddCodeLine ("sty sreg");
3043 if ((flags & CF_UNSIGNED) == 0) {
3044 unsigned L = GetLocalLabel();
3045 AddCodeLine ("cpy #$80");
3046 AddCodeLine ("ldy #$00");
3047 AddCodeLine ("bcc %s", LocalLabelName (L));
3048 AddCodeLine ("dey");
3051 AddCodeLine ("ldy #$00");
3053 AddCodeLine ("sty sreg+1");
3057 if (flags & CF_UNSIGNED) {
3058 AddCodeLine ("jsr shreax4");
3060 AddCodeLine ("jsr asreax4");
3065 if (flags & CF_UNSIGNED) {
3066 AddCodeLine ("jsr shreax%ld", val);
3068 AddCodeLine ("jsr asreax%ld", val);
3077 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3078 * into the normal, non-optimized stuff. Note: The standard stuff will
3079 * always work with ints.
3081 flags &= ~CF_FORCECHAR;
3082 g_push (flags & ~CF_CONST, 0);
3085 /* Use long way over the stack */
3086 oper (flags, val, ops);
3091 void g_asl (unsigned flags, unsigned long val)
3092 /* Primary = TOS << Primary */
3094 static const char* ops[12] = {
3095 "tosaslax", "tosshlax", "tosasleax", "tosshleax",
3099 /* If the right hand side is const, the lhs is not on stack but still
3100 * in the primary register.
3102 if (flags & CF_CONST) {
3104 switch (flags & CF_TYPE) {
3110 AddCodeLine ("tax");
3111 AddCodeLine ("lda #$00");
3115 if (flags & CF_UNSIGNED) {
3116 AddCodeLine ("jsr shlax4");
3118 AddCodeLine ("jsr aslax4");
3123 if (flags & CF_UNSIGNED) {
3124 AddCodeLine ("jsr shlax%ld", val);
3126 AddCodeLine ("jsr aslax%ld", val);
3134 AddCodeLine ("sta sreg+1");
3135 AddCodeLine ("lda #$00");
3136 AddCodeLine ("tax");
3137 AddCodeLine ("sta sreg");
3141 AddCodeLine ("stx sreg+1");
3142 AddCodeLine ("sta sreg");
3143 AddCodeLine ("lda #$00");
3144 AddCodeLine ("tax");
3148 AddCodeLine ("ldy sreg");
3149 AddCodeLine ("sty sreg+1");
3150 AddCodeLine ("stx sreg");
3151 AddCodeLine ("tax");
3152 AddCodeLine ("lda #$00");
3156 if (flags & CF_UNSIGNED) {
3157 AddCodeLine ("jsr shleax4");
3159 AddCodeLine ("jsr asleax4");
3164 if (flags & CF_UNSIGNED) {
3165 AddCodeLine ("jsr shleax%ld", val);
3167 AddCodeLine ("jsr asleax%ld", val);
3176 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3177 * into the normal, non-optimized stuff. Note: The standard stuff will
3178 * always work with ints.
3180 flags &= ~CF_FORCECHAR;
3181 g_push (flags & ~CF_CONST, 0);
3184 /* Use long way over the stack */
3185 oper (flags, val, ops);
3190 void g_neg (unsigned Flags)
3191 /* Primary = -Primary */
3193 switch (Flags & CF_TYPE) {
3196 if (Flags & CF_FORCECHAR) {
3197 AddCodeLine ("eor #$FF");
3198 AddCodeLine ("clc");
3199 AddCodeLine ("adc #$01");
3205 AddCodeLine ("jsr negax");
3209 AddCodeLine ("jsr negeax");
3219 void g_bneg (unsigned flags)
3220 /* Primary = !Primary */
3222 switch (flags & CF_TYPE) {
3225 AddCodeLine ("jsr bnega");
3229 AddCodeLine ("jsr bnegax");
3233 AddCodeLine ("jsr bnegeax");
3243 void g_com (unsigned Flags)
3244 /* Primary = ~Primary */
3246 switch (Flags & CF_TYPE) {
3249 if (Flags & CF_FORCECHAR) {
3250 AddCodeLine ("eor #$FF");
3256 AddCodeLine ("jsr complax");
3260 AddCodeLine ("jsr compleax");
3270 void g_inc (unsigned flags, unsigned long val)
3271 /* Increment the primary register by a given number */
3273 /* Don't inc by zero */
3278 /* Generate code for the supported types */
3280 switch (flags & CF_TYPE) {
3283 if (flags & CF_FORCECHAR) {
3284 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3286 AddCodeLine ("ina");
3289 AddCodeLine ("clc");
3290 AddCodeLine ("adc #$%02X", (unsigned char)val);
3297 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) {
3298 unsigned L = GetLocalLabel();
3299 AddCodeLine ("ina");
3300 AddCodeLine ("bne %s", LocalLabelName (L));
3301 AddCodeLine ("inx");
3303 } else if (IS_Get (&CodeSizeFactor) < 200) {
3306 AddCodeLine ("jsr incax%lu", val);
3307 } else if (val <= 255) {
3309 AddCodeLine ("jsr incaxy");
3311 g_add (flags | CF_CONST, val);
3314 /* Inline the code */
3316 if ((val & 0xFF) != 0) {
3317 unsigned L = GetLocalLabel();
3318 AddCodeLine ("clc");
3319 AddCodeLine ("adc #$%02X", (unsigned char) val);
3320 AddCodeLine ("bcc %s", LocalLabelName (L));
3321 AddCodeLine ("inx");
3325 AddCodeLine ("inx");
3328 AddCodeLine ("inx");
3331 AddCodeLine ("inx");
3333 } else if ((val & 0xFF) != 0) {
3334 AddCodeLine ("clc");
3335 AddCodeLine ("adc #$%02X", (unsigned char) val);
3336 AddCodeLine ("pha");
3337 AddCodeLine ("txa");
3338 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3339 AddCodeLine ("tax");
3340 AddCodeLine ("pla");
3342 AddCodeLine ("pha");
3343 AddCodeLine ("txa");
3344 AddCodeLine ("clc");
3345 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3346 AddCodeLine ("tax");
3347 AddCodeLine ("pla");
3355 AddCodeLine ("jsr inceaxy");
3357 g_add (flags | CF_CONST, val);
3369 void g_dec (unsigned flags, unsigned long val)
3370 /* Decrement the primary register by a given number */
3372 /* Don't dec by zero */
3377 /* Generate code for the supported types */
3379 switch (flags & CF_TYPE) {
3382 if (flags & CF_FORCECHAR) {
3383 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3385 AddCodeLine ("dea");
3388 AddCodeLine ("sec");
3389 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3396 if (IS_Get (&CodeSizeFactor) < 200) {
3397 /* Use subroutines */
3399 AddCodeLine ("jsr decax%d", (int) val);
3400 } else if (val <= 255) {
3402 AddCodeLine ("jsr decaxy");
3404 g_sub (flags | CF_CONST, val);
3407 /* Inline the code */
3409 if ((val & 0xFF) != 0) {
3410 unsigned L = GetLocalLabel();
3411 AddCodeLine ("sec");
3412 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3413 AddCodeLine ("bcs %s", LocalLabelName (L));
3414 AddCodeLine ("dex");
3418 AddCodeLine ("dex");
3421 AddCodeLine ("dex");
3424 if ((val & 0xFF) != 0) {
3425 AddCodeLine ("sec");
3426 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3427 AddCodeLine ("pha");
3428 AddCodeLine ("txa");
3429 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3430 AddCodeLine ("tax");
3431 AddCodeLine ("pla");
3433 AddCodeLine ("pha");
3434 AddCodeLine ("txa");
3435 AddCodeLine ("sec");
3436 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3437 AddCodeLine ("tax");
3438 AddCodeLine ("pla");
3447 AddCodeLine ("jsr deceaxy");
3449 g_sub (flags | CF_CONST, val);
3462 * Following are the conditional operators. They compare the TOS against
3463 * the primary and put a literal 1 in the primary if the condition is
3464 * true, otherwise they clear the primary register
3469 void g_eq (unsigned flags, unsigned long val)
3470 /* Test for equal */
3472 static const char* ops[12] = {
3473 "toseqax", "toseqax", "toseqeax", "toseqeax",
3478 /* If the right hand side is const, the lhs is not on stack but still
3479 * in the primary register.
3481 if (flags & CF_CONST) {
3483 switch (flags & CF_TYPE) {
3486 if (flags & CF_FORCECHAR) {
3487 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3488 AddCodeLine ("jsr booleq");
3494 L = GetLocalLabel();
3495 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3496 AddCodeLine ("bne %s", LocalLabelName (L));
3497 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3499 AddCodeLine ("jsr booleq");
3509 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3510 * into the normal, non-optimized stuff. Note: The standard stuff will
3511 * always work with ints.
3513 flags &= ~CF_FORCECHAR;
3514 g_push (flags & ~CF_CONST, 0);
3517 /* Use long way over the stack */
3518 oper (flags, val, ops);
3523 void g_ne (unsigned flags, unsigned long val)
3524 /* Test for not equal */
3526 static const char* ops[12] = {
3527 "tosneax", "tosneax", "tosneeax", "tosneeax",
3532 /* If the right hand side is const, the lhs is not on stack but still
3533 * in the primary register.
3535 if (flags & CF_CONST) {
3537 switch (flags & CF_TYPE) {
3540 if (flags & CF_FORCECHAR) {
3541 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3542 AddCodeLine ("jsr boolne");
3548 L = GetLocalLabel();
3549 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3550 AddCodeLine ("bne %s", LocalLabelName (L));
3551 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3553 AddCodeLine ("jsr boolne");
3563 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3564 * into the normal, non-optimized stuff. Note: The standard stuff will
3565 * always work with ints.
3567 flags &= ~CF_FORCECHAR;
3568 g_push (flags & ~CF_CONST, 0);
3571 /* Use long way over the stack */
3572 oper (flags, val, ops);
3577 void g_lt (unsigned flags, unsigned long val)
3578 /* Test for less than */
3580 static const char* ops[12] = {
3581 "tosltax", "tosultax", "toslteax", "tosulteax",
3586 /* If the right hand side is const, the lhs is not on stack but still
3587 * in the primary register.
3589 if (flags & CF_CONST) {
3591 /* Because the handling of the overflow flag is too complex for
3592 * inlining, we can handle only unsigned compares, and signed
3593 * compares against zero here.
3595 if (flags & CF_UNSIGNED) {
3597 /* Give a warning in some special cases */
3599 Warning ("Condition is never true");
3600 AddCodeLine ("jsr return0");
3604 /* Look at the type */
3605 switch (flags & CF_TYPE) {
3608 if (flags & CF_FORCECHAR) {
3609 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3610 AddCodeLine ("jsr boolult");
3616 /* If the low byte is zero, we must only test the high byte */
3617 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3618 if ((val & 0xFF) != 0) {
3619 unsigned L = GetLocalLabel();
3620 AddCodeLine ("bne %s", LocalLabelName (L));
3621 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3624 AddCodeLine ("jsr boolult");
3628 /* Do a subtraction */
3629 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3630 AddCodeLine ("txa");
3631 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3632 AddCodeLine ("lda sreg");
3633 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3634 AddCodeLine ("lda sreg+1");
3635 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3636 AddCodeLine ("jsr boolult");
3643 } else if (val == 0) {
3645 /* A signed compare against zero must only look at the sign bit */
3646 switch (flags & CF_TYPE) {
3649 if (flags & CF_FORCECHAR) {
3650 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3651 AddCodeLine ("lda #$00");
3652 AddCodeLine ("ldx #$00");
3653 AddCodeLine ("rol a");
3659 /* Just check the high byte */
3660 AddCodeLine ("cpx #$80"); /* Bit 7 -> carry */
3661 AddCodeLine ("lda #$00");
3662 AddCodeLine ("ldx #$00");
3663 AddCodeLine ("rol a");
3667 /* Just check the high byte */
3668 AddCodeLine ("lda sreg+1");
3669 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3670 AddCodeLine ("lda #$00");
3671 AddCodeLine ("ldx #$00");
3672 AddCodeLine ("rol a");
3681 /* Signed compare against a constant != zero */
3682 switch (flags & CF_TYPE) {
3685 if (flags & CF_FORCECHAR) {
3686 Label = GetLocalLabel ();
3687 AddCodeLine ("sec");
3688 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3689 AddCodeLine ("bvc %s", LocalLabelName (Label));
3690 AddCodeLine ("eor #$80");
3691 g_defcodelabel (Label);
3692 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3693 AddCodeLine ("lda #$00");
3694 AddCodeLine ("ldx #$00");
3695 AddCodeLine ("rol a");
3701 /* Do a subtraction */
3702 Label = GetLocalLabel ();
3703 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3704 AddCodeLine ("txa");
3705 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3706 AddCodeLine ("bvc %s", LocalLabelName (Label));
3707 AddCodeLine ("eor #$80");
3708 g_defcodelabel (Label);
3709 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3710 AddCodeLine ("lda #$00");
3711 AddCodeLine ("ldx #$00");
3712 AddCodeLine ("rol a");
3716 /* This one is too costly */
3725 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3726 * into the normal, non-optimized stuff. Note: The standard stuff will
3727 * always work with ints.
3729 flags &= ~CF_FORCECHAR;
3730 g_push (flags & ~CF_CONST, 0);
3733 /* Use long way over the stack */
3734 oper (flags, val, ops);
3739 void g_le (unsigned flags, unsigned long val)
3740 /* Test for less than or equal to */
3742 static const char* ops[12] = {
3743 "tosleax", "tosuleax", "tosleeax", "tosuleeax",
3747 /* If the right hand side is const, the lhs is not on stack but still
3748 * in the primary register.
3750 if (flags & CF_CONST) {
3752 /* Look at the type */
3753 switch (flags & CF_TYPE) {
3756 if (flags & CF_FORCECHAR) {
3757 if (flags & CF_UNSIGNED) {
3758 /* Unsigned compare */
3760 /* Use < instead of <= because the former gives
3761 * better code on the 6502 than the latter.
3763 g_lt (flags, val+1);
3766 Warning ("Condition is always true");
3767 AddCodeLine ("jsr return1");
3770 /* Signed compare */
3771 if ((long) val < 0x7F) {
3772 /* Use < instead of <= because the former gives
3773 * better code on the 6502 than the latter.
3775 g_lt (flags, val+1);
3778 Warning ("Condition is always true");
3779 AddCodeLine ("jsr return1");
3787 if (flags & CF_UNSIGNED) {
3788 /* Unsigned compare */
3790 /* Use < instead of <= because the former gives
3791 * better code on the 6502 than the latter.
3793 g_lt (flags, val+1);
3796 Warning ("Condition is always true");
3797 AddCodeLine ("jsr return1");
3800 /* Signed compare */
3801 if ((long) val < 0x7FFF) {
3802 g_lt (flags, val+1);
3805 Warning ("Condition is always true");
3806 AddCodeLine ("jsr return1");
3812 if (flags & CF_UNSIGNED) {
3813 /* Unsigned compare */
3814 if (val < 0xFFFFFFFF) {
3815 /* Use < instead of <= because the former gives
3816 * better code on the 6502 than the latter.
3818 g_lt (flags, val+1);
3821 Warning ("Condition is always true");
3822 AddCodeLine ("jsr return1");
3825 /* Signed compare */
3826 if ((long) val < 0x7FFFFFFF) {
3827 g_lt (flags, val+1);
3830 Warning ("Condition is always true");
3831 AddCodeLine ("jsr return1");
3840 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3841 * into the normal, non-optimized stuff. Note: The standard stuff will
3842 * always work with ints.
3844 flags &= ~CF_FORCECHAR;
3845 g_push (flags & ~CF_CONST, 0);
3848 /* Use long way over the stack */
3849 oper (flags, val, ops);
3854 void g_gt (unsigned flags, unsigned long val)
3855 /* Test for greater than */
3857 static const char* ops[12] = {
3858 "tosgtax", "tosugtax", "tosgteax", "tosugteax",
3862 /* If the right hand side is const, the lhs is not on stack but still
3863 * in the primary register.
3865 if (flags & CF_CONST) {
3867 /* Look at the type */
3868 switch (flags & CF_TYPE) {
3871 if (flags & CF_FORCECHAR) {
3872 if (flags & CF_UNSIGNED) {
3874 /* If we have a compare > 0, we will replace it by
3875 * != 0 here, since both are identical but the
3876 * latter is easier to optimize.
3879 } else if (val < 0xFF) {
3880 /* Use >= instead of > because the former gives
3881 * better code on the 6502 than the latter.
3883 g_ge (flags, val+1);
3886 Warning ("Condition is never true");
3887 AddCodeLine ("jsr return0");
3890 if ((long) val < 0x7F) {
3891 /* Use >= instead of > because the former gives
3892 * better code on the 6502 than the latter.
3894 g_ge (flags, val+1);
3897 Warning ("Condition is never true");
3898 AddCodeLine ("jsr return0");
3906 if (flags & CF_UNSIGNED) {
3907 /* Unsigned compare */
3909 /* If we have a compare > 0, we will replace it by
3910 * != 0 here, since both are identical but the latter
3911 * is easier to optimize.
3914 } else if (val < 0xFFFF) {
3915 /* Use >= instead of > because the former gives better
3916 * code on the 6502 than the latter.
3918 g_ge (flags, val+1);
3921 Warning ("Condition is never true");
3922 AddCodeLine ("jsr return0");
3925 /* Signed compare */
3926 if ((long) val < 0x7FFF) {
3927 g_ge (flags, val+1);
3930 Warning ("Condition is never true");
3931 AddCodeLine ("jsr return0");
3937 if (flags & CF_UNSIGNED) {
3938 /* Unsigned compare */
3940 /* If we have a compare > 0, we will replace it by
3941 * != 0 here, since both are identical but the latter
3942 * is easier to optimize.
3945 } else if (val < 0xFFFFFFFF) {
3946 /* Use >= instead of > because the former gives better
3947 * code on the 6502 than the latter.
3949 g_ge (flags, val+1);
3952 Warning ("Condition is never true");
3953 AddCodeLine ("jsr return0");
3956 /* Signed compare */
3957 if ((long) val < 0x7FFFFFFF) {
3958 g_ge (flags, val+1);
3961 Warning ("Condition is never true");
3962 AddCodeLine ("jsr return0");
3971 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3972 * into the normal, non-optimized stuff. Note: The standard stuff will
3973 * always work with ints.
3975 flags &= ~CF_FORCECHAR;
3976 g_push (flags & ~CF_CONST, 0);
3979 /* Use long way over the stack */
3980 oper (flags, val, ops);
3985 void g_ge (unsigned flags, unsigned long val)
3986 /* Test for greater than or equal to */
3988 static const char* ops[12] = {
3989 "tosgeax", "tosugeax", "tosgeeax", "tosugeeax",
3995 /* If the right hand side is const, the lhs is not on stack but still
3996 * in the primary register.
3998 if (flags & CF_CONST) {
4000 /* Because the handling of the overflow flag is too complex for
4001 * inlining, we can handle only unsigned compares, and signed
4002 * compares against zero here.
4004 if (flags & CF_UNSIGNED) {
4006 /* Give a warning in some special cases */
4008 Warning ("Condition is always true");
4009 AddCodeLine ("jsr return1");
4013 /* Look at the type */
4014 switch (flags & CF_TYPE) {
4017 if (flags & CF_FORCECHAR) {
4018 /* Do a subtraction. Condition is true if carry set */
4019 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4020 AddCodeLine ("lda #$00");
4021 AddCodeLine ("ldx #$00");
4022 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 #$00");
4033 AddCodeLine ("ldx #$00");
4034 AddCodeLine ("rol a");
4038 /* Do a subtraction. Condition is true if carry set */
4039 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4040 AddCodeLine ("txa");
4041 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4042 AddCodeLine ("lda sreg");
4043 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
4044 AddCodeLine ("lda sreg+1");
4045 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
4046 AddCodeLine ("lda #$00");
4047 AddCodeLine ("ldx #$00");
4048 AddCodeLine ("rol a");
4055 } else if (val == 0) {
4057 /* A signed compare against zero must only look at the sign bit */
4058 switch (flags & CF_TYPE) {
4061 if (flags & CF_FORCECHAR) {
4062 AddCodeLine ("tax");
4063 AddCodeLine ("jsr boolge");
4069 /* Just test the high byte */
4070 AddCodeLine ("txa");
4071 AddCodeLine ("jsr boolge");
4075 /* Just test the high byte */
4076 AddCodeLine ("lda sreg+1");
4077 AddCodeLine ("jsr boolge");
4086 /* Signed compare against a constant != zero */
4087 switch (flags & CF_TYPE) {
4090 if (flags & CF_FORCECHAR) {
4091 Label = GetLocalLabel ();
4092 AddCodeLine ("sec");
4093 AddCodeLine ("sbc #$%02X", (unsigned char)val);
4094 AddCodeLine ("bvs %s", LocalLabelName (Label));
4095 AddCodeLine ("eor #$80");
4096 g_defcodelabel (Label);
4097 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4098 AddCodeLine ("lda #$00");
4099 AddCodeLine ("ldx #$00");
4100 AddCodeLine ("rol a");
4106 /* Do a subtraction */
4107 Label = GetLocalLabel ();
4108 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4109 AddCodeLine ("txa");
4110 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4111 AddCodeLine ("bvs %s", LocalLabelName (Label));
4112 AddCodeLine ("eor #$80");
4113 g_defcodelabel (Label);
4114 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4115 AddCodeLine ("lda #$00");
4116 AddCodeLine ("ldx #$00");
4117 AddCodeLine ("rol a");
4121 /* This one is too costly */
4129 /* If we go here, we didn't emit code. Push the lhs on stack and fall
4130 * into the normal, non-optimized stuff. Note: The standard stuff will
4131 * always work with ints.
4133 flags &= ~CF_FORCECHAR;
4134 g_push (flags & ~CF_CONST, 0);
4137 /* Use long way over the stack */
4138 oper (flags, val, ops);
4143 /*****************************************************************************/
4144 /* Allocating static storage */
4145 /*****************************************************************************/
4149 void g_res (unsigned n)
4150 /* Reserve static storage, n bytes */
4152 AddDataLine ("\t.res\t%u,$00", n);
4157 void g_defdata (unsigned flags, unsigned long val, long offs)
4158 /* Define data with the size given in flags */
4160 if (flags & CF_CONST) {
4162 /* Numeric constant */
4163 switch (flags & CF_TYPE) {
4166 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
4170 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
4174 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
4185 /* Create the correct label name */
4186 const char* Label = GetLabelName (flags, val, offs);
4188 /* Labels are always 16 bit */
4189 AddDataLine ("\t.addr\t%s", Label);
4196 void g_defbytes (const void* Bytes, unsigned Count)
4197 /* Output a row of bytes as a constant */
4203 /* Cast the buffer pointer */
4204 const unsigned char* Data = (const unsigned char*) Bytes;
4206 /* Output the stuff */
4209 /* How many go into this line? */
4210 if ((Chunk = Count) > 16) {
4215 /* Output one line */
4216 strcpy (Buf, "\t.byte\t");
4219 B += sprintf (B, "$%02X", *Data++);
4225 /* Output the line */
4232 void g_zerobytes (unsigned Count)
4233 /* Output Count bytes of data initialized with zero */
4236 AddDataLine ("\t.res\t%u,$00", Count);
4242 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4243 /* Initialize a register variable from static initialization data */
4245 /* Register variables do always have less than 128 bytes */
4246 unsigned CodeLabel = GetLocalLabel ();
4248 g_defcodelabel (CodeLabel);
4249 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4250 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4251 AddCodeLine ("dex");
4252 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4257 void g_initauto (unsigned Label, unsigned Size)
4258 /* Initialize a local variable at stack offset zero from static data */
4260 unsigned CodeLabel = GetLocalLabel ();
4262 CheckLocalOffs (Size);
4265 g_defcodelabel (CodeLabel);
4266 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4267 AddCodeLine ("sta (sp),y");
4268 AddCodeLine ("dey");
4269 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4270 } else if (Size <= 256) {
4272 g_defcodelabel (CodeLabel);
4273 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4274 AddCodeLine ("sta (sp),y");
4275 AddCodeLine ("iny");
4276 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4277 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4283 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4284 /* Initialize a static local variable from static initialization data */
4287 unsigned CodeLabel = GetLocalLabel ();
4289 g_defcodelabel (CodeLabel);
4290 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4291 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4292 AddCodeLine ("dey");
4293 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4294 } else if (Size <= 256) {
4295 unsigned CodeLabel = GetLocalLabel ();
4297 g_defcodelabel (CodeLabel);
4298 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4299 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4300 AddCodeLine ("iny");
4301 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4302 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4304 /* Use the easy way here: memcpy */
4305 g_getimmed (CF_STATIC, VarLabel, 0);
4306 AddCodeLine ("jsr pushax");
4307 g_getimmed (CF_STATIC, InitLabel, 0);
4308 AddCodeLine ("jsr pushax");
4309 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4310 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4316 /*****************************************************************************/
4317 /* Switch statement */
4318 /*****************************************************************************/
4322 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4323 /* Generate code for a switch statement */
4325 unsigned NextLabel = 0;
4328 /* Setup registers and determine which compare insn to use */
4329 const char* Compare;
4332 Compare = "cmp #$%02X";
4335 Compare = "cpx #$%02X";
4338 AddCodeLine ("ldy sreg");
4339 Compare = "cpy #$%02X";
4342 AddCodeLine ("ldy sreg+1");
4343 Compare = "cpy #$%02X";
4346 Internal ("Invalid depth in g_switch: %u", Depth);
4349 /* Walk over all nodes */
4350 for (I = 0; I < CollCount (Nodes); ++I) {
4352 /* Get the next case node */
4353 CaseNode* N = CollAtUnchecked (Nodes, I);
4355 /* If we have a next label, define it */
4357 g_defcodelabel (NextLabel);
4361 /* Do the compare */
4362 AddCodeLine (Compare, CN_GetValue (N));
4364 /* If this is the last level, jump directly to the case code if found */
4367 /* Branch if equal */
4368 g_falsejump (0, CN_GetLabel (N));
4372 /* Determine the next label */
4373 if (I == CollCount (Nodes) - 1) {
4374 /* Last node means not found */
4375 g_truejump (0, DefaultLabel);
4377 /* Jump to the next check */
4378 NextLabel = GetLocalLabel ();
4379 g_truejump (0, NextLabel);
4382 /* Check the next level */
4383 g_switch (N->Nodes, DefaultLabel, Depth-1);
4388 /* If we go here, we haven't found the label */
4389 g_jump (DefaultLabel);
4394 /*****************************************************************************/
4395 /* User supplied assembler code */
4396 /*****************************************************************************/
4400 void g_asmcode (struct StrBuf* B)
4401 /* Output one line of assembler code. */
4403 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));