1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2004 Ullrich von Bassewitz */
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 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
78 static void CheckLocalOffs (unsigned Offs)
79 /* Check the offset into the stack for 8bit range */
82 /* Too many local vars */
83 Error ("Too many local variables");
89 static const char* GetLabelName (unsigned Flags, unsigned long Label, long Offs)
91 static char Buf [256]; /* Label name */
93 /* Create the correct label name */
94 switch (Flags & CF_ADDRMASK) {
97 /* Static memory cell */
99 xsprintf (Buf, sizeof (Buf), "%s%+ld", LocalLabelName (Label), Offs);
101 xsprintf (Buf, sizeof (Buf), "%s", LocalLabelName (Label));
108 xsprintf (Buf, sizeof (Buf), "_%s%+ld", (char*) Label, Offs);
110 xsprintf (Buf, sizeof (Buf), "_%s", (char*) Label);
115 /* Absolute address */
116 xsprintf (Buf, sizeof (Buf), "$%04X", (int)((Label+Offs) & 0xFFFF));
120 /* Variable in register bank */
121 xsprintf (Buf, sizeof (Buf), "regbank+%u", (unsigned)((Label+Offs) & 0xFFFF));
125 Internal ("Invalid address flags: %04X", Flags);
128 /* Return a pointer to the static buffer */
134 /*****************************************************************************/
135 /* Pre- and postamble */
136 /*****************************************************************************/
140 void g_preamble (void)
141 /* Generate the assembler code preamble */
143 /* Create a new (global) segment list and remember it */
147 /* Identify the compiler version */
149 AddTextLine ("; File generated by cc65 v %u.%u.%u",
150 VER_MAJOR, VER_MINOR, VER_PATCH);
153 /* Insert some object file options */
154 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
155 VER_MAJOR, VER_MINOR, VER_PATCH);
157 /* If we're producing code for some other CPU, switch the command set */
159 case CPU_6502: AddTextLine ("\t.setcpu\t\t\"6502\""); break;
160 case CPU_6502X: AddTextLine ("\t.setcpu\t\t\"6502X\""); break;
161 case CPU_65SC02: AddTextLine ("\t.setcpu\t\t\"65SC02\""); break;
162 case CPU_65C02: AddTextLine ("\t.setcpu\t\t\"65C02\""); break;
163 case CPU_65816: AddTextLine ("\t.setcpu\t\t\"65816\""); break;
164 case CPU_HUC6280: AddTextLine ("\t.setcpu\t\t\"HUC6280\""); break;
165 default: Internal ("Unknown CPU: %d", CPU);
169 AddTextLine ("\t.smart\t\ton");
171 /* Allow auto import for runtime library routines */
172 AddTextLine ("\t.autoimport\ton");
174 /* Switch the assembler into case sensitive mode */
175 AddTextLine ("\t.case\t\ton");
177 /* Tell the assembler if we want to generate debug info */
178 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
180 /* Import the stack pointer for direct auto variable access */
181 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
183 /* Define long branch macros */
184 AddTextLine ("\t.macpack\tlongbranch");
189 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
190 /* If debug info is enabled, place a file info into the source */
193 /* We have to place this into the global text segment, so it will
194 * appear before all .dbg line statements.
196 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
202 /*****************************************************************************/
203 /* Segment support */
204 /*****************************************************************************/
208 void g_userodata (void)
209 /* Switch to the read only data segment */
211 UseDataSeg (SEG_RODATA);
216 void g_usedata (void)
217 /* Switch to the data segment */
219 UseDataSeg (SEG_DATA);
225 /* Switch to the bss segment */
227 UseDataSeg (SEG_BSS);
232 void g_segname (segment_t Seg)
233 /* Emit the name of a segment if necessary */
235 /* Emit a segment directive for the data style segments */
238 case SEG_RODATA: S = CS->ROData; break;
239 case SEG_DATA: S = CS->Data; break;
240 case SEG_BSS: S = CS->BSS; break;
241 default: S = 0; break;
244 DS_AddLine (S, ".segment\t\"%s\"", GetSegName (Seg));
250 /*****************************************************************************/
252 /*****************************************************************************/
256 unsigned sizeofarg (unsigned flags)
257 /* Return the size of a function argument type that is encoded in flags */
259 switch (flags & CF_TYPE) {
262 return (flags & CF_FORCECHAR)? 1 : 2;
282 int pop (unsigned flags)
283 /* Pop an argument of the given size */
285 return StackPtr += sizeofarg (flags);
290 int push (unsigned flags)
291 /* Push an argument of the given size */
293 return StackPtr -= sizeofarg (flags);
298 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
299 /* The value in Offs is an offset to an address in a/x. Make sure, an object
300 * of the type given in Flags can be loaded or stored into this address by
301 * adding part of the offset to the address in ax, so that the remaining
302 * offset fits into an index register. Return the remaining offset.
305 /* If the offset is too large for a byte register, add the high byte
306 * of the offset to the primary. Beware: We need a special correction
307 * if the offset in the low byte will overflow in the operation.
309 unsigned O = Offs & ~0xFFU;
310 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
311 /* We need to add the low byte also */
315 /* Do the correction if we need one */
317 g_inc (CF_INT | CF_CONST, O);
321 /* Return the new offset */
327 /*****************************************************************************/
328 /* Functions handling local labels */
329 /*****************************************************************************/
333 void g_defcodelabel (unsigned label)
334 /* Define a local code label */
336 CS_AddLabel (CS->Code, LocalLabelName (label));
341 void g_defdatalabel (unsigned label)
342 /* Define a local data label */
344 AddDataLine ("%s:", LocalLabelName (label));
349 /*****************************************************************************/
350 /* Functions handling global labels */
351 /*****************************************************************************/
355 void g_defgloblabel (const char* Name)
356 /* Define a global label with the given name */
358 /* Global labels are always data labels */
359 AddDataLine ("_%s:", Name);
364 void g_defexport (const char* Name, int ZP)
365 /* Export the given label */
368 AddTextLine ("\t.exportzp\t_%s", Name);
370 AddTextLine ("\t.export\t\t_%s", Name);
376 void g_defimport (const char* Name, int ZP)
377 /* Import the given label */
380 AddTextLine ("\t.importzp\t_%s", Name);
382 AddTextLine ("\t.import\t\t_%s", Name);
388 void g_importstartup (void)
389 /* Forced import of the startup segment */
392 AddTextLine ("\t.forceimport\t__STARTUP_RUN__");
398 void g_importmainargs (void)
399 /* Forced import of a special symbol that handles arguments to main */
401 AddTextLine ("\t.forceimport\tinitmainargs");
406 /*****************************************************************************/
407 /* Load functions for various registers */
408 /*****************************************************************************/
412 static void ldaconst (unsigned val)
413 /* Load a with a constant */
415 AddCodeLine ("lda #$%02X", val & 0xFF);
420 static void ldxconst (unsigned val)
421 /* Load x with a constant */
423 AddCodeLine ("ldx #$%02X", val & 0xFF);
428 static void ldyconst (unsigned val)
429 /* Load y with a constant */
431 AddCodeLine ("ldy #$%02X", val & 0xFF);
436 /*****************************************************************************/
437 /* Function entry and exit */
438 /*****************************************************************************/
442 /* Remember the argument size of a function. The variable is set by g_enter
443 * and used by g_leave. If the functions gets its argument size by the caller
444 * (variable param list or function without prototype), g_enter will set the
450 void g_enter (unsigned flags, unsigned argsize)
451 /* Function prologue */
453 if ((flags & CF_FIXARGC) != 0) {
454 /* Just remember the argument size for the leave */
458 AddCodeLine ("jsr enter");
465 /* Function epilogue */
467 /* How many bytes of locals do we have to drop? */
468 unsigned k = (unsigned) -StackPtr;
470 /* If we didn't have a variable argument list, don't call leave */
473 /* Drop stackframe if needed. We can only drop 255 bytes at a time. */
476 unsigned ToDrop = (k > 255)? 255 : k;
478 AddCodeLine ("jsr incsp%d", k);
481 AddCodeLine ("jsr addysp");
489 /* Nothing to drop */
490 AddCodeLine ("jsr leave");
492 /* We've a stack frame to drop */
495 AddCodeLine ("jsr addysp");
499 AddCodeLine ("jsr leavey");
503 /* Add the final rts */
509 /*****************************************************************************/
510 /* Register variables */
511 /*****************************************************************************/
515 void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
516 /* Swap a register variable with a location on the stack */
518 /* Calculate the actual stack offset and check it */
519 StackOffs -= StackPtr;
520 CheckLocalOffs (StackOffs);
525 if (IS_Get (&CodeSizeFactor) < 165) {
526 ldyconst (StackOffs);
528 AddCodeLine ("jsr regswap1");
530 ldyconst (StackOffs);
531 AddCodeLine ("lda (sp),y");
532 AddCodeLine ("ldx regbank%+d", RegOffs);
533 AddCodeLine ("sta regbank%+d", RegOffs);
535 AddCodeLine ("sta (sp),y");
538 } else if (Bytes == 2) {
540 ldyconst (StackOffs);
542 AddCodeLine ("jsr regswap2");
546 ldyconst (StackOffs);
549 AddCodeLine ("jsr regswap");
555 void g_save_regvars (int RegOffs, unsigned Bytes)
556 /* Save register variables */
558 /* Don't loop for up to two bytes */
561 AddCodeLine ("lda regbank%+d", RegOffs);
562 AddCodeLine ("jsr pusha");
564 } else if (Bytes == 2) {
566 AddCodeLine ("lda regbank%+d", RegOffs);
567 AddCodeLine ("ldx regbank%+d", RegOffs+1);
568 AddCodeLine ("jsr pushax");
572 /* More than two bytes - loop */
573 unsigned Label = GetLocalLabel ();
575 ldyconst (Bytes - 1);
577 g_defcodelabel (Label);
578 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
579 AddCodeLine ("sta (sp),y");
582 AddCodeLine ("bne %s", LocalLabelName (Label));
586 /* We pushed stuff, correct the stack pointer */
592 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
593 /* Restore register variables */
595 /* Calculate the actual stack offset and check it */
596 StackOffs -= StackPtr;
597 CheckLocalOffs (StackOffs);
599 /* Don't loop for up to two bytes */
602 ldyconst (StackOffs);
603 AddCodeLine ("lda (sp),y");
604 AddCodeLine ("sta regbank%+d", RegOffs);
606 } else if (Bytes == 2) {
608 ldyconst (StackOffs);
609 AddCodeLine ("lda (sp),y");
610 AddCodeLine ("sta regbank%+d", RegOffs);
612 AddCodeLine ("lda (sp),y");
613 AddCodeLine ("sta regbank%+d", RegOffs+1);
615 } else if (Bytes == 3 && IS_Get (&CodeSizeFactor) >= 133) {
617 ldyconst (StackOffs);
618 AddCodeLine ("lda (sp),y");
619 AddCodeLine ("sta regbank%+d", RegOffs);
621 AddCodeLine ("lda (sp),y");
622 AddCodeLine ("sta regbank%+d", RegOffs+1);
624 AddCodeLine ("lda (sp),y");
625 AddCodeLine ("sta regbank%+d", RegOffs+2);
627 } else if (StackOffs <= RegOffs) {
629 /* More bytes, but the relation between the register offset in the
630 * register bank and the stack offset allows us to generate short
631 * code that uses just one index register.
633 unsigned Label = GetLocalLabel ();
634 ldyconst (StackOffs);
635 g_defcodelabel (Label);
636 AddCodeLine ("lda (sp),y");
637 AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs);
639 AddCodeLine ("cpy #$%02X", StackOffs + Bytes);
640 AddCodeLine ("bne %s", LocalLabelName (Label));
644 /* Ok, this is the generic code. We need to save X because the
645 * caller will only save A.
647 unsigned Label = GetLocalLabel ();
648 AddCodeLine ("stx tmp1");
649 ldyconst (StackOffs + Bytes - 1);
650 ldxconst (Bytes - 1);
651 g_defcodelabel (Label);
652 AddCodeLine ("lda (sp),y");
653 AddCodeLine ("sta regbank%+d,x", RegOffs);
656 AddCodeLine ("bpl %s", LocalLabelName (Label));
657 AddCodeLine ("ldx tmp1");
664 /*****************************************************************************/
665 /* Fetching memory cells */
666 /*****************************************************************************/
670 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
671 /* Load a constant into the primary register */
673 unsigned char B1, B2, B3, B4;
677 if ((Flags & CF_CONST) != 0) {
679 /* Numeric constant */
680 switch (Flags & CF_TYPE) {
683 if ((Flags & CF_FORCECHAR) != 0) {
689 ldxconst ((Val >> 8) & 0xFF);
690 ldaconst (Val & 0xFF);
694 /* Split the value into 4 bytes */
695 B1 = (unsigned char) (Val >> 0);
696 B2 = (unsigned char) (Val >> 8);
697 B3 = (unsigned char) (Val >> 16);
698 B4 = (unsigned char) (Val >> 24);
700 /* Remember which bytes are done */
704 AddCodeLine ("ldx #$%02X", B2);
707 AddCodeLine ("stx sreg");
711 AddCodeLine ("stx sreg+1");
714 if ((Done & 0x04) == 0 && B1 != B3) {
715 AddCodeLine ("lda #$%02X", B3);
716 AddCodeLine ("sta sreg");
719 if ((Done & 0x08) == 0 && B1 != B4) {
720 AddCodeLine ("lda #$%02X", B4);
721 AddCodeLine ("sta sreg+1");
724 AddCodeLine ("lda #$%02X", B1);
726 if ((Done & 0x04) == 0) {
728 AddCodeLine ("sta sreg");
730 if ((Done & 0x08) == 0) {
732 AddCodeLine ("sta sreg+1");
744 /* Some sort of label */
745 const char* Label = GetLabelName (Flags, Val, Offs);
747 /* Load the address into the primary */
748 AddCodeLine ("lda #<(%s)", Label);
749 AddCodeLine ("ldx #>(%s)", Label);
756 void g_getstatic (unsigned flags, unsigned long label, long offs)
757 /* Fetch an static memory cell into the primary register */
759 /* Create the correct label name */
760 const char* lbuf = GetLabelName (flags, label, offs);
762 /* Check the size and generate the correct load operation */
763 switch (flags & CF_TYPE) {
766 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
767 AddCodeLine ("lda %s", lbuf); /* load A from the label */
770 AddCodeLine ("lda %s", lbuf); /* load A from the label */
771 if (!(flags & CF_UNSIGNED)) {
772 /* Must sign extend */
773 unsigned L = GetLocalLabel ();
774 AddCodeLine ("bpl %s", LocalLabelName (L));
782 AddCodeLine ("lda %s", lbuf);
783 if (flags & CF_TEST) {
784 AddCodeLine ("ora %s+1", lbuf);
786 AddCodeLine ("ldx %s+1", lbuf);
791 if (flags & CF_TEST) {
792 AddCodeLine ("lda %s+3", lbuf);
793 AddCodeLine ("ora %s+2", lbuf);
794 AddCodeLine ("ora %s+1", lbuf);
795 AddCodeLine ("ora %s+0", lbuf);
797 AddCodeLine ("lda %s+3", lbuf);
798 AddCodeLine ("sta sreg+1");
799 AddCodeLine ("lda %s+2", lbuf);
800 AddCodeLine ("sta sreg");
801 AddCodeLine ("ldx %s+1", lbuf);
802 AddCodeLine ("lda %s", lbuf);
814 void g_getlocal (unsigned flags, int offs)
815 /* Fetch specified local object (local var). */
818 CheckLocalOffs (offs);
819 switch (flags & CF_TYPE) {
822 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
824 AddCodeLine ("lda (sp),y");
827 AddCodeLine ("ldx #$00");
828 AddCodeLine ("lda (sp),y");
829 if ((flags & CF_UNSIGNED) == 0) {
830 unsigned L = GetLocalLabel();
831 AddCodeLine ("bpl %s", LocalLabelName (L));
839 CheckLocalOffs (offs + 1);
840 if (flags & CF_TEST) {
842 AddCodeLine ("lda (sp),y");
844 AddCodeLine ("ora (sp),y");
847 AddCodeLine ("jsr ldaxysp");
853 AddCodeLine ("jsr ldeaxysp");
854 if (flags & CF_TEST) {
866 void g_getind (unsigned flags, unsigned offs)
867 /* Fetch the specified object type indirect through the primary register
868 * into the primary register
871 /* If the offset is greater than 255, add the part that is > 255 to
872 * the primary. This way we get an easy addition and use the low byte
875 offs = MakeByteOffs (flags, offs);
877 /* Handle the indirect fetch */
878 switch (flags & CF_TYPE) {
881 /* Character sized */
882 if (flags & CF_UNSIGNED) {
884 AddCodeLine ("jsr ldauidx");
887 AddCodeLine ("jsr ldaidx");
892 if (flags & CF_TEST) {
894 AddCodeLine ("sta ptr1");
895 AddCodeLine ("stx ptr1+1");
896 AddCodeLine ("lda (ptr1),y");
898 AddCodeLine ("ora (ptr1),y");
901 AddCodeLine ("jsr ldaxidx");
907 AddCodeLine ("jsr ldeaxidx");
908 if (flags & CF_TEST) {
921 void g_leasp (int offs)
922 /* Fetch the address of the specified symbol into the primary register */
924 /* Calculate the offset relative to sp */
927 /* For value 0 we do direct code */
929 AddCodeLine ("lda sp");
930 AddCodeLine ("ldx sp+1");
932 if (IS_Get (&CodeSizeFactor) < 300) {
933 ldaconst (offs); /* Load A with offset value */
934 AddCodeLine ("jsr leaasp"); /* Load effective address */
936 unsigned L = GetLocalLabel ();
937 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && offs == 1) {
938 AddCodeLine ("lda sp");
939 AddCodeLine ("ldx sp+1");
941 AddCodeLine ("bne %s", LocalLabelName (L));
946 AddCodeLine ("ldx sp+1");
947 AddCodeLine ("adc sp");
948 AddCodeLine ("bcc %s", LocalLabelName (L));
958 void g_leavariadic (int Offs)
959 /* Fetch the address of a parameter in a variadic function into the primary
963 unsigned ArgSizeOffs;
965 /* Calculate the offset relative to sp */
968 /* Get the offset of the parameter which is stored at sp+0 on function
969 * entry and check if this offset is reachable with a byte offset.
971 CHECK (StackPtr <= 0);
972 ArgSizeOffs = -StackPtr;
973 CheckLocalOffs (ArgSizeOffs);
975 /* Get the size of all parameters. */
976 ldyconst (ArgSizeOffs);
977 AddCodeLine ("lda (sp),y");
979 /* Add the value of the stackpointer */
980 if (IS_Get (&CodeSizeFactor) > 250) {
981 unsigned L = GetLocalLabel();
982 AddCodeLine ("ldx sp+1");
984 AddCodeLine ("adc sp");
985 AddCodeLine ("bcc %s", LocalLabelName (L));
989 AddCodeLine ("jsr leaasp");
992 /* Add the offset to the primary */
994 g_inc (CF_INT | CF_CONST, Offs);
995 } else if (Offs < 0) {
996 g_dec (CF_INT | CF_CONST, -Offs);
1002 /*****************************************************************************/
1003 /* Store into memory */
1004 /*****************************************************************************/
1008 void g_putstatic (unsigned flags, unsigned long label, long offs)
1009 /* Store the primary register into the specified static memory cell */
1011 /* Create the correct label name */
1012 const char* lbuf = GetLabelName (flags, label, offs);
1014 /* Check the size and generate the correct store operation */
1015 switch (flags & CF_TYPE) {
1018 AddCodeLine ("sta %s", lbuf);
1022 AddCodeLine ("sta %s", lbuf);
1023 AddCodeLine ("stx %s+1", lbuf);
1027 AddCodeLine ("sta %s", lbuf);
1028 AddCodeLine ("stx %s+1", lbuf);
1029 AddCodeLine ("ldy sreg");
1030 AddCodeLine ("sty %s+2", lbuf);
1031 AddCodeLine ("ldy sreg+1");
1032 AddCodeLine ("sty %s+3", lbuf);
1043 void g_putlocal (unsigned Flags, int Offs, long Val)
1044 /* Put data into local object. */
1047 CheckLocalOffs (Offs);
1048 switch (Flags & CF_TYPE) {
1051 if (Flags & CF_CONST) {
1052 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1055 AddCodeLine ("sta (sp),y");
1059 if (Flags & CF_CONST) {
1061 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1062 AddCodeLine ("sta (sp),y");
1063 if ((Flags & CF_NOKEEP) == 0) {
1064 /* Place high byte into X */
1065 AddCodeLine ("tax");
1067 if ((Val & 0xFF) == Offs+1) {
1068 /* The value we need is already in Y */
1069 AddCodeLine ("tya");
1070 AddCodeLine ("dey");
1072 AddCodeLine ("dey");
1073 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1075 AddCodeLine ("sta (sp),y");
1077 if ((Flags & CF_NOKEEP) == 0 || IS_Get (&CodeSizeFactor) < 160) {
1079 AddCodeLine ("jsr staxysp");
1082 AddCodeLine ("sta (sp),y");
1083 AddCodeLine ("iny");
1084 AddCodeLine ("txa");
1085 AddCodeLine ("sta (sp),y");
1091 if (Flags & CF_CONST) {
1092 g_getimmed (Flags, Val, 0);
1095 AddCodeLine ("jsr steaxysp");
1106 void g_putind (unsigned Flags, unsigned Offs)
1107 /* Store the specified object type in the primary register at the address
1108 * on the top of the stack
1111 /* We can handle offsets below $100 directly, larger offsets must be added
1112 * to the address. Since a/x is in use, best code is achieved by adding
1113 * just the high byte. Be sure to check if the low byte will overflow while
1116 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1118 /* Overflow - we need to add the low byte also */
1119 AddCodeLine ("ldy #$00");
1120 AddCodeLine ("clc");
1121 AddCodeLine ("pha");
1122 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1123 AddCodeLine ("adc (sp),y");
1124 AddCodeLine ("sta (sp),y");
1125 AddCodeLine ("iny");
1126 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1127 AddCodeLine ("adc (sp),y");
1128 AddCodeLine ("sta (sp),y");
1129 AddCodeLine ("pla");
1131 /* Complete address is on stack, new offset is zero */
1134 } else if ((Offs & 0xFF00) != 0) {
1136 /* We can just add the high byte */
1137 AddCodeLine ("ldy #$01");
1138 AddCodeLine ("clc");
1139 AddCodeLine ("pha");
1140 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1141 AddCodeLine ("adc (sp),y");
1142 AddCodeLine ("sta (sp),y");
1143 AddCodeLine ("pla");
1145 /* Offset is now just the low byte */
1149 /* Check the size and determine operation */
1150 switch (Flags & CF_TYPE) {
1154 AddCodeLine ("jsr staspidx");
1159 AddCodeLine ("jsr staxspidx");
1164 AddCodeLine ("jsr steaxspidx");
1172 /* Pop the argument which is always a pointer */
1178 /*****************************************************************************/
1179 /* type conversion and similiar stuff */
1180 /*****************************************************************************/
1184 void g_toslong (unsigned flags)
1185 /* Make sure, the value on TOS is a long. Convert if necessary */
1187 switch (flags & CF_TYPE) {
1191 if (flags & CF_UNSIGNED) {
1192 AddCodeLine ("jsr tosulong");
1194 AddCodeLine ("jsr toslong");
1209 void g_tosint (unsigned flags)
1210 /* Make sure, the value on TOS is an int. Convert if necessary */
1212 switch (flags & CF_TYPE) {
1219 AddCodeLine ("jsr tosint");
1230 void g_regint (unsigned Flags)
1231 /* Make sure, the value in the primary register an int. Convert if necessary */
1235 switch (Flags & CF_TYPE) {
1238 if (Flags & CF_FORCECHAR) {
1239 /* Conversion is from char */
1240 if (Flags & CF_UNSIGNED) {
1241 AddCodeLine ("ldx #$00");
1243 L = GetLocalLabel();
1244 AddCodeLine ("ldx #$00");
1245 AddCodeLine ("cmp #$80");
1246 AddCodeLine ("bcc %s", LocalLabelName (L));
1247 AddCodeLine ("dex");
1264 void g_reglong (unsigned Flags)
1265 /* Make sure, the value in the primary register a long. Convert if necessary */
1269 switch (Flags & CF_TYPE) {
1272 if (Flags & CF_FORCECHAR) {
1273 /* Conversion is from char */
1274 if (Flags & CF_UNSIGNED) {
1275 if (IS_Get (&CodeSizeFactor) >= 200) {
1276 AddCodeLine ("ldx #$00");
1277 AddCodeLine ("stx sreg");
1278 AddCodeLine ("stx sreg+1");
1280 AddCodeLine ("jsr aulong");
1283 if (IS_Get (&CodeSizeFactor) >= 366) {
1284 L = GetLocalLabel();
1285 AddCodeLine ("ldx #$00");
1286 AddCodeLine ("cmp #$80");
1287 AddCodeLine ("bcc %s", LocalLabelName (L));
1288 AddCodeLine ("dex");
1290 AddCodeLine ("stx sreg");
1291 AddCodeLine ("stx sreg+1");
1293 AddCodeLine ("jsr along");
1300 if (Flags & CF_UNSIGNED) {
1301 if (IS_Get (&CodeSizeFactor) >= 200) {
1303 AddCodeLine ("sty sreg");
1304 AddCodeLine ("sty sreg+1");
1306 AddCodeLine ("jsr axulong");
1309 AddCodeLine ("jsr axlong");
1323 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1324 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1325 * value, that corresponds to the value on TOS, rhs corresponds to the value
1326 * in (e)ax. The return value is the the flags value for the resulting type.
1329 unsigned ltype, rtype;
1332 /* Get the type spec from the flags */
1333 ltype = lhs & CF_TYPE;
1334 rtype = rhs & CF_TYPE;
1336 /* Check if a conversion is needed */
1337 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1338 /* We must promote the primary register to long */
1340 /* Get the new rhs type */
1341 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1343 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1344 /* We must promote the lhs to long */
1350 /* Get the new rhs type */
1351 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1355 /* Determine the result type for the operation:
1356 * - The result is const if both operands are const.
1357 * - The result is unsigned if one of the operands is unsigned.
1358 * - The result is long if one of the operands is long.
1359 * - Otherwise the result is int sized.
1361 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1362 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1363 if (rtype == CF_LONG || ltype == CF_LONG) {
1373 unsigned g_typecast (unsigned lhs, unsigned rhs)
1374 /* Cast the value in the primary register to the operand size that is flagged
1375 * by the lhs value. Return the result value.
1378 unsigned ltype, rtype;
1380 /* Get the type spec from the flags */
1381 ltype = lhs & CF_TYPE;
1382 rtype = rhs & CF_TYPE;
1384 /* Check if a conversion is needed */
1385 if ((rhs & CF_CONST) == 0) {
1386 if (ltype == CF_LONG && rtype != CF_LONG) {
1387 /* We must promote the primary register to long */
1389 } else if (ltype == CF_INT && rtype != CF_INT) {
1390 /* We must promote the primary register to int */
1395 /* Do not need any other action. If the left type is int, and the primary
1396 * register is long, it will be automagically truncated. If the right hand
1397 * side is const, it is not located in the primary register and handled by
1398 * the expression parser code.
1401 /* Result is const if the right hand side was const */
1402 lhs |= (rhs & CF_CONST);
1404 /* The resulting type is that of the left hand side (that's why you called
1412 void g_scale (unsigned flags, long val)
1413 /* Scale the value in the primary register by the given value. If val is positive,
1414 * scale up, is val is negative, scale down. This function is used to scale
1415 * the operands or results of pointer arithmetic by the size of the type, the
1416 * pointer points to.
1421 /* Value may not be zero */
1423 Internal ("Data type has no size");
1424 } else if (val > 0) {
1427 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1429 /* Factor is 2, 4, 8 and 16, use special function */
1430 switch (flags & CF_TYPE) {
1433 if (flags & CF_FORCECHAR) {
1435 AddCodeLine ("asl a");
1442 if (IS_Get (&CodeSizeFactor) >= (p2+1)*130) {
1443 AddCodeLine ("stx tmp1");
1445 AddCodeLine ("asl a");
1446 AddCodeLine ("rol tmp1");
1448 AddCodeLine ("ldx tmp1");
1450 if (flags & CF_UNSIGNED) {
1451 AddCodeLine ("jsr shlax%d", p2);
1453 AddCodeLine ("jsr aslax%d", p2);
1459 if (flags & CF_UNSIGNED) {
1460 AddCodeLine ("jsr shleax%d", p2);
1462 AddCodeLine ("jsr asleax%d", p2);
1471 } else if (val != 1) {
1473 /* Use a multiplication instead */
1474 g_mul (flags | CF_CONST, val);
1482 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1484 /* Factor is 2, 4, 8 and 16 use special function */
1485 switch (flags & CF_TYPE) {
1488 if (flags & CF_FORCECHAR) {
1489 if (flags & CF_UNSIGNED) {
1491 AddCodeLine ("lsr a");
1494 } else if (p2 <= 2) {
1495 AddCodeLine ("cmp #$80");
1496 AddCodeLine ("ror a");
1503 if (flags & CF_UNSIGNED) {
1504 if (IS_Get (&CodeSizeFactor) >= (p2+1)*130) {
1505 AddCodeLine ("stx tmp1");
1507 AddCodeLine ("lsr tmp1");
1508 AddCodeLine ("ror a");
1510 AddCodeLine ("ldx tmp1");
1512 AddCodeLine ("jsr lsrax%d", p2);
1515 if (IS_Get (&CodeSizeFactor) >= (p2+1)*150) {
1516 AddCodeLine ("stx tmp1");
1518 AddCodeLine ("cpx #$80");
1519 AddCodeLine ("ror tmp1");
1520 AddCodeLine ("ror a");
1522 AddCodeLine ("ldx tmp1");
1524 AddCodeLine ("jsr asrax%d", p2);
1530 if (flags & CF_UNSIGNED) {
1531 AddCodeLine ("jsr lsreax%d", p2);
1533 AddCodeLine ("jsr asreax%d", p2);
1542 } else if (val != 1) {
1544 /* Use a division instead */
1545 g_div (flags | CF_CONST, val);
1553 /*****************************************************************************/
1554 /* Adds and subs of variables fix a fixed address */
1555 /*****************************************************************************/
1559 void g_addlocal (unsigned flags, int offs)
1560 /* Add a local variable to ax */
1564 /* Correct the offset and check it */
1566 CheckLocalOffs (offs);
1568 switch (flags & CF_TYPE) {
1571 L = GetLocalLabel();
1572 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1573 AddCodeLine ("clc");
1574 AddCodeLine ("adc (sp),y");
1575 AddCodeLine ("bcc %s", LocalLabelName (L));
1576 AddCodeLine ("inx");
1581 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1582 AddCodeLine ("clc");
1583 AddCodeLine ("adc (sp),y");
1584 AddCodeLine ("pha");
1585 AddCodeLine ("txa");
1586 AddCodeLine ("iny");
1587 AddCodeLine ("adc (sp),y");
1588 AddCodeLine ("tax");
1589 AddCodeLine ("pla");
1593 /* Do it the old way */
1595 g_getlocal (flags, offs);
1607 void g_addstatic (unsigned flags, unsigned long label, long offs)
1608 /* Add a static variable to ax */
1612 /* Create the correct label name */
1613 const char* lbuf = GetLabelName (flags, label, offs);
1615 switch (flags & CF_TYPE) {
1618 L = GetLocalLabel();
1619 AddCodeLine ("clc");
1620 AddCodeLine ("adc %s", lbuf);
1621 AddCodeLine ("bcc %s", LocalLabelName (L));
1622 AddCodeLine ("inx");
1627 AddCodeLine ("clc");
1628 AddCodeLine ("adc %s", lbuf);
1629 AddCodeLine ("tay");
1630 AddCodeLine ("txa");
1631 AddCodeLine ("adc %s+1", lbuf);
1632 AddCodeLine ("tax");
1633 AddCodeLine ("tya");
1637 /* Do it the old way */
1639 g_getstatic (flags, label, offs);
1651 /*****************************************************************************/
1652 /* Special op= functions */
1653 /*****************************************************************************/
1657 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1659 /* Emit += for a static variable */
1661 /* Create the correct label name */
1662 const char* lbuf = GetLabelName (flags, label, offs);
1664 /* Check the size and determine operation */
1665 switch (flags & CF_TYPE) {
1668 if (flags & CF_FORCECHAR) {
1669 AddCodeLine ("ldx #$00");
1670 if (flags & CF_CONST) {
1672 AddCodeLine ("inc %s", lbuf);
1673 AddCodeLine ("lda %s", lbuf);
1675 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1676 AddCodeLine ("clc");
1677 AddCodeLine ("adc %s", lbuf);
1678 AddCodeLine ("sta %s", lbuf);
1681 AddCodeLine ("clc");
1682 AddCodeLine ("adc %s", lbuf);
1683 AddCodeLine ("sta %s", lbuf);
1685 if ((flags & CF_UNSIGNED) == 0) {
1686 unsigned L = GetLocalLabel();
1687 AddCodeLine ("bpl %s", LocalLabelName (L));
1688 AddCodeLine ("dex");
1696 if (flags & CF_CONST) {
1698 unsigned L = GetLocalLabel ();
1699 AddCodeLine ("inc %s", lbuf);
1700 AddCodeLine ("bne %s", LocalLabelName (L));
1701 AddCodeLine ("inc %s+1", lbuf);
1703 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1704 AddCodeLine ("ldx %s+1", lbuf);
1706 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1707 AddCodeLine ("clc");
1708 AddCodeLine ("adc %s", lbuf);
1709 AddCodeLine ("sta %s", lbuf);
1711 unsigned L = GetLocalLabel ();
1712 AddCodeLine ("bcc %s", LocalLabelName (L));
1713 AddCodeLine ("inc %s+1", lbuf);
1715 AddCodeLine ("ldx %s+1", lbuf);
1717 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1718 AddCodeLine ("adc %s+1", lbuf);
1719 AddCodeLine ("sta %s+1", lbuf);
1720 AddCodeLine ("tax");
1721 AddCodeLine ("lda %s", lbuf);
1725 AddCodeLine ("clc");
1726 AddCodeLine ("adc %s", lbuf);
1727 AddCodeLine ("sta %s", lbuf);
1728 AddCodeLine ("txa");
1729 AddCodeLine ("adc %s+1", lbuf);
1730 AddCodeLine ("sta %s+1", lbuf);
1731 AddCodeLine ("tax");
1732 AddCodeLine ("lda %s", lbuf);
1737 if (flags & CF_CONST) {
1739 AddCodeLine ("ldy #<(%s)", lbuf);
1740 AddCodeLine ("sty ptr1");
1741 AddCodeLine ("ldy #>(%s)", lbuf);
1743 AddCodeLine ("jsr laddeq1");
1745 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1746 AddCodeLine ("jsr laddeqa");
1749 g_getstatic (flags, label, offs);
1751 g_putstatic (flags, label, offs);
1754 AddCodeLine ("ldy #<(%s)", lbuf);
1755 AddCodeLine ("sty ptr1");
1756 AddCodeLine ("ldy #>(%s)", lbuf);
1757 AddCodeLine ("jsr laddeq");
1768 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1769 /* Emit += for a local variable */
1771 /* Calculate the true offset, check it, load it into Y */
1773 CheckLocalOffs (offs);
1775 /* Check the size and determine operation */
1776 switch (flags & CF_TYPE) {
1779 if (flags & CF_FORCECHAR) {
1781 AddCodeLine ("ldx #$00");
1782 if (flags & CF_CONST) {
1783 AddCodeLine ("clc");
1784 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1785 AddCodeLine ("adc (sp),y");
1786 AddCodeLine ("sta (sp),y");
1788 AddCodeLine ("clc");
1789 AddCodeLine ("adc (sp),y");
1790 AddCodeLine ("sta (sp),y");
1792 if ((flags & CF_UNSIGNED) == 0) {
1793 unsigned L = GetLocalLabel();
1794 AddCodeLine ("bpl %s", LocalLabelName (L));
1795 AddCodeLine ("dex");
1804 if (flags & CF_CONST) {
1805 if (IS_Get (&CodeSizeFactor) >= 400) {
1806 AddCodeLine ("clc");
1807 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1808 AddCodeLine ("adc (sp),y");
1809 AddCodeLine ("sta (sp),y");
1810 AddCodeLine ("iny");
1811 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1812 AddCodeLine ("adc (sp),y");
1813 AddCodeLine ("sta (sp),y");
1814 AddCodeLine ("tax");
1815 AddCodeLine ("dey");
1816 AddCodeLine ("lda (sp),y");
1818 g_getimmed (flags, val, 0);
1819 AddCodeLine ("jsr addeqysp");
1822 AddCodeLine ("jsr addeqysp");
1827 if (flags & CF_CONST) {
1828 g_getimmed (flags, val, 0);
1831 AddCodeLine ("jsr laddeqysp");
1841 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1842 /* Emit += for the location with address in ax */
1844 /* If the offset is too large for a byte register, add the high byte
1845 * of the offset to the primary. Beware: We need a special correction
1846 * if the offset in the low byte will overflow in the operation.
1848 offs = MakeByteOffs (flags, offs);
1850 /* Check the size and determine operation */
1851 switch (flags & CF_TYPE) {
1854 AddCodeLine ("sta ptr1");
1855 AddCodeLine ("stx ptr1+1");
1856 AddCodeLine ("ldy #$%02X", offs);
1857 AddCodeLine ("ldx #$00");
1858 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1859 AddCodeLine ("clc");
1860 AddCodeLine ("adc (ptr1),y");
1861 AddCodeLine ("sta (ptr1),y");
1865 if (IS_Get (&CodeSizeFactor) >= 200) {
1866 /* Lots of code, use only if size is not important */
1867 AddCodeLine ("sta ptr1");
1868 AddCodeLine ("stx ptr1+1");
1869 AddCodeLine ("ldy #$%02X", offs);
1870 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1871 AddCodeLine ("clc");
1872 AddCodeLine ("adc (ptr1),y");
1873 AddCodeLine ("sta (ptr1),y");
1874 AddCodeLine ("pha");
1875 AddCodeLine ("iny");
1876 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1877 AddCodeLine ("adc (ptr1),y");
1878 AddCodeLine ("sta (ptr1),y");
1879 AddCodeLine ("tax");
1880 AddCodeLine ("pla");
1886 AddCodeLine ("jsr pushax"); /* Push the address */
1887 push (CF_PTR); /* Correct the internal sp */
1888 g_getind (flags, offs); /* Fetch the value */
1889 g_inc (flags, val); /* Increment value in primary */
1890 g_putind (flags, offs); /* Store the value back */
1900 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1902 /* Emit -= for a static variable */
1904 /* Create the correct label name */
1905 const char* lbuf = GetLabelName (flags, label, offs);
1907 /* Check the size and determine operation */
1908 switch (flags & CF_TYPE) {
1911 if (flags & CF_FORCECHAR) {
1912 AddCodeLine ("ldx #$00");
1913 if (flags & CF_CONST) {
1915 AddCodeLine ("dec %s", lbuf);
1916 AddCodeLine ("lda %s", lbuf);
1918 AddCodeLine ("sec");
1919 AddCodeLine ("lda %s", lbuf);
1920 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1921 AddCodeLine ("sta %s", lbuf);
1924 AddCodeLine ("eor #$FF");
1925 AddCodeLine ("sec");
1926 AddCodeLine ("adc %s", lbuf);
1927 AddCodeLine ("sta %s", lbuf);
1929 if ((flags & CF_UNSIGNED) == 0) {
1930 unsigned L = GetLocalLabel();
1931 AddCodeLine ("bpl %s", LocalLabelName (L));
1932 AddCodeLine ("dex");
1940 AddCodeLine ("sec");
1941 if (flags & CF_CONST) {
1942 AddCodeLine ("lda %s", lbuf);
1943 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1944 AddCodeLine ("sta %s", lbuf);
1946 unsigned L = GetLocalLabel ();
1947 AddCodeLine ("bcs %s", LocalLabelName (L));
1948 AddCodeLine ("dec %s+1", lbuf);
1950 AddCodeLine ("ldx %s+1", lbuf);
1952 AddCodeLine ("lda %s+1", lbuf);
1953 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1954 AddCodeLine ("sta %s+1", lbuf);
1955 AddCodeLine ("tax");
1956 AddCodeLine ("lda %s", lbuf);
1959 AddCodeLine ("eor #$FF");
1960 AddCodeLine ("adc %s", lbuf);
1961 AddCodeLine ("sta %s", lbuf);
1962 AddCodeLine ("txa");
1963 AddCodeLine ("eor #$FF");
1964 AddCodeLine ("adc %s+1", lbuf);
1965 AddCodeLine ("sta %s+1", lbuf);
1966 AddCodeLine ("tax");
1967 AddCodeLine ("lda %s", lbuf);
1972 if (flags & CF_CONST) {
1974 AddCodeLine ("ldy #<(%s)", lbuf);
1975 AddCodeLine ("sty ptr1");
1976 AddCodeLine ("ldy #>(%s)", lbuf);
1977 AddCodeLine ("lda #$%02X", (unsigned char)val);
1978 AddCodeLine ("jsr lsubeqa");
1980 g_getstatic (flags, label, offs);
1982 g_putstatic (flags, label, offs);
1985 AddCodeLine ("ldy #<(%s)", lbuf);
1986 AddCodeLine ("sty ptr1");
1987 AddCodeLine ("ldy #>(%s)", lbuf);
1988 AddCodeLine ("jsr lsubeq");
1999 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
2000 /* Emit -= for a local variable */
2002 /* Calculate the true offset, check it, load it into Y */
2004 CheckLocalOffs (offs);
2006 /* Check the size and determine operation */
2007 switch (flags & CF_TYPE) {
2010 if (flags & CF_FORCECHAR) {
2012 AddCodeLine ("ldx #$00");
2013 AddCodeLine ("sec");
2014 if (flags & CF_CONST) {
2015 AddCodeLine ("lda (sp),y");
2016 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2018 AddCodeLine ("eor #$FF");
2019 AddCodeLine ("adc (sp),y");
2021 AddCodeLine ("sta (sp),y");
2022 if ((flags & CF_UNSIGNED) == 0) {
2023 unsigned L = GetLocalLabel();
2024 AddCodeLine ("bpl %s", LocalLabelName (L));
2025 AddCodeLine ("dex");
2033 if (flags & CF_CONST) {
2034 g_getimmed (flags, val, 0);
2037 AddCodeLine ("jsr subeqysp");
2041 if (flags & CF_CONST) {
2042 g_getimmed (flags, val, 0);
2045 AddCodeLine ("jsr lsubeqysp");
2055 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2056 /* Emit -= for the location with address in ax */
2058 /* If the offset is too large for a byte register, add the high byte
2059 * of the offset to the primary. Beware: We need a special correction
2060 * if the offset in the low byte will overflow in the operation.
2062 offs = MakeByteOffs (flags, offs);
2064 /* Check the size and determine operation */
2065 switch (flags & CF_TYPE) {
2068 AddCodeLine ("sta ptr1");
2069 AddCodeLine ("stx ptr1+1");
2070 AddCodeLine ("ldy #$%02X", offs);
2071 AddCodeLine ("ldx #$00");
2072 AddCodeLine ("lda (ptr1),y");
2073 AddCodeLine ("sec");
2074 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2075 AddCodeLine ("sta (ptr1),y");
2079 if (IS_Get (&CodeSizeFactor) >= 200) {
2080 /* Lots of code, use only if size is not important */
2081 AddCodeLine ("sta ptr1");
2082 AddCodeLine ("stx ptr1+1");
2083 AddCodeLine ("ldy #$%02X", offs);
2084 AddCodeLine ("lda (ptr1),y");
2085 AddCodeLine ("sec");
2086 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2087 AddCodeLine ("sta (ptr1),y");
2088 AddCodeLine ("pha");
2089 AddCodeLine ("iny");
2090 AddCodeLine ("lda (ptr1),y");
2091 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2092 AddCodeLine ("sta (ptr1),y");
2093 AddCodeLine ("tax");
2094 AddCodeLine ("pla");
2100 AddCodeLine ("jsr pushax"); /* Push the address */
2101 push (CF_PTR); /* Correct the internal sp */
2102 g_getind (flags, offs); /* Fetch the value */
2103 g_dec (flags, val); /* Increment value in primary */
2104 g_putind (flags, offs); /* Store the value back */
2114 /*****************************************************************************/
2115 /* Add a variable address to the value in ax */
2116 /*****************************************************************************/
2120 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2121 /* Add the address of a local variable to ax */
2125 /* Add the offset */
2128 /* We cannot address more then 256 bytes of locals anyway */
2129 L = GetLocalLabel();
2130 CheckLocalOffs (offs);
2131 AddCodeLine ("clc");
2132 AddCodeLine ("adc #$%02X", offs & 0xFF);
2133 /* Do also skip the CLC insn below */
2134 AddCodeLine ("bcc %s", LocalLabelName (L));
2135 AddCodeLine ("inx");
2138 /* Add the current stackpointer value */
2139 AddCodeLine ("clc");
2141 /* Label was used above */
2144 AddCodeLine ("adc sp");
2145 AddCodeLine ("tay");
2146 AddCodeLine ("txa");
2147 AddCodeLine ("adc sp+1");
2148 AddCodeLine ("tax");
2149 AddCodeLine ("tya");
2154 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2155 /* Add the address of a static variable to ax */
2157 /* Create the correct label name */
2158 const char* lbuf = GetLabelName (flags, label, offs);
2160 /* Add the address to the current ax value */
2161 AddCodeLine ("clc");
2162 AddCodeLine ("adc #<(%s)", lbuf);
2163 AddCodeLine ("tay");
2164 AddCodeLine ("txa");
2165 AddCodeLine ("adc #>(%s)", lbuf);
2166 AddCodeLine ("tax");
2167 AddCodeLine ("tya");
2172 /*****************************************************************************/
2174 /*****************************************************************************/
2178 void g_save (unsigned flags)
2179 /* Copy primary register to hold register. */
2181 /* Check the size and determine operation */
2182 switch (flags & CF_TYPE) {
2185 if (flags & CF_FORCECHAR) {
2186 AddCodeLine ("pha");
2192 AddCodeLine ("sta regsave");
2193 AddCodeLine ("stx regsave+1");
2197 AddCodeLine ("jsr saveeax");
2207 void g_restore (unsigned flags)
2208 /* Copy hold register to primary. */
2210 /* Check the size and determine operation */
2211 switch (flags & CF_TYPE) {
2214 if (flags & CF_FORCECHAR) {
2215 AddCodeLine ("pla");
2221 AddCodeLine ("lda regsave");
2222 AddCodeLine ("ldx regsave+1");
2226 AddCodeLine ("jsr resteax");
2236 void g_cmp (unsigned flags, unsigned long val)
2237 /* Immidiate compare. The primary register will not be changed, Z flag
2243 /* Check the size and determine operation */
2244 switch (flags & CF_TYPE) {
2247 if (flags & CF_FORCECHAR) {
2248 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2254 L = GetLocalLabel();
2255 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2256 AddCodeLine ("bne %s", LocalLabelName (L));
2257 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2262 Internal ("g_cmp: Long compares not implemented");
2272 static void oper (unsigned flags, unsigned long val, char** subs)
2273 /* Encode a binary operation. subs is a pointer to four groups of three
2275 * 0-2 --> Operate on ints
2276 * 3-5 --> Operate on unsigneds
2277 * 6-8 --> Operate on longs
2278 * 9-11 --> Operate on unsigned longs
2280 * The first subroutine names in each string group is used to encode an
2281 * operation with a zero constant, the second to encode an operation with
2282 * a 8 bit constant, and the third is used in all other cases.
2287 /* Determine the offset into the array */
2288 offs = (flags & CF_UNSIGNED)? 3 : 0;
2289 switch (flags & CF_TYPE) {
2302 /* Encode the operation */
2303 if (flags & CF_CONST) {
2304 /* Constant value given */
2305 if (val == 0 && subs [offs+0]) {
2306 /* Special case: constant with value zero */
2307 AddCodeLine ("jsr %s", subs [offs+0]);
2308 } else if (val < 0x100 && subs [offs+1]) {
2309 /* Special case: constant with high byte zero */
2310 ldaconst (val); /* Load low byte */
2311 AddCodeLine ("jsr %s", subs [offs+1]);
2313 /* Others: arbitrary constant value */
2314 g_getimmed (flags, val, 0); /* Load value */
2315 AddCodeLine ("jsr %s", subs [offs+2]);
2318 /* Value not constant (is already in (e)ax) */
2319 AddCodeLine ("jsr %s", subs [offs+2]);
2322 /* The operation will pop it's argument */
2328 void g_test (unsigned flags)
2329 /* Test the value in the primary and set the condition codes */
2331 switch (flags & CF_TYPE) {
2334 if (flags & CF_FORCECHAR) {
2335 AddCodeLine ("tax");
2341 AddCodeLine ("stx tmp1");
2342 AddCodeLine ("ora tmp1");
2346 if (flags & CF_UNSIGNED) {
2347 AddCodeLine ("jsr utsteax");
2349 AddCodeLine ("jsr tsteax");
2361 void g_push (unsigned flags, unsigned long val)
2362 /* Push the primary register or a constant value onto the stack */
2364 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2366 /* We have a constant 8 or 16 bit value */
2367 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2369 /* Handle as 8 bit value */
2371 AddCodeLine ("jsr pusha");
2375 /* Handle as 16 bit value */
2376 g_getimmed (flags, val, 0);
2377 AddCodeLine ("jsr pushax");
2382 /* Value is not 16 bit or not constant */
2383 if (flags & CF_CONST) {
2384 /* Constant 32 bit value, load into eax */
2385 g_getimmed (flags, val, 0);
2388 /* Push the primary register */
2389 switch (flags & CF_TYPE) {
2392 if (flags & CF_FORCECHAR) {
2393 /* Handle as char */
2394 AddCodeLine ("jsr pusha");
2399 AddCodeLine ("jsr pushax");
2403 AddCodeLine ("jsr pusheax");
2413 /* Adjust the stack offset */
2419 void g_swap (unsigned flags)
2420 /* Swap the primary register and the top of the stack. flags give the type
2421 * of *both* values (must have same size).
2424 switch (flags & CF_TYPE) {
2428 AddCodeLine ("jsr swapstk");
2432 AddCodeLine ("jsr swapestk");
2443 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2444 /* Call the specified subroutine name */
2446 if ((Flags & CF_FIXARGC) == 0) {
2447 /* Pass the argument count */
2450 AddCodeLine ("jsr _%s", Label);
2451 StackPtr += ArgSize; /* callee pops args */
2456 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2457 /* Call subroutine indirect */
2459 if ((Flags & CF_LOCAL) == 0) {
2460 /* Address is in a/x */
2461 if ((Flags & CF_FIXARGC) == 0) {
2462 /* Pass arg count */
2465 AddCodeLine ("jsr callax");
2467 /* The address is on stack, offset is on Val */
2469 CheckLocalOffs (Offs);
2470 AddCodeLine ("pha");
2471 AddCodeLine ("ldy #$%02X", Offs);
2472 AddCodeLine ("lda (sp),y");
2473 AddCodeLine ("sta jmpvec+1");
2474 AddCodeLine ("iny");
2475 AddCodeLine ("lda (sp),y");
2476 AddCodeLine ("sta jmpvec+2");
2477 AddCodeLine ("pla");
2478 AddCodeLine ("jsr jmpvec");
2481 /* Callee pops args */
2482 StackPtr += ArgSize;
2487 void g_jump (unsigned Label)
2488 /* Jump to specified internal label number */
2490 AddCodeLine ("jmp %s", LocalLabelName (Label));
2495 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2496 /* Jump to label if zero flag clear */
2498 AddCodeLine ("jne %s", LocalLabelName (label));
2503 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2504 /* Jump to label if zero flag set */
2506 AddCodeLine ("jeq %s", LocalLabelName (label));
2511 static void mod_internal (int k, char* verb1, char* verb2)
2514 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2518 AddCodeLine ("jsr %ssp", verb2);
2524 void g_space (int space)
2525 /* Create or drop space on the stack */
2528 mod_internal (-space, "inc", "addy");
2529 } else if (space > 0) {
2530 mod_internal (space, "dec", "suby");
2536 void g_cstackcheck (void)
2537 /* Check for a C stack overflow */
2539 AddCodeLine ("jsr cstkchk");
2544 void g_stackcheck (void)
2545 /* Check for a stack overflow */
2547 AddCodeLine ("jsr stkchk");
2552 void g_add (unsigned flags, unsigned long val)
2553 /* Primary = TOS + Primary */
2555 static char* ops [12] = {
2556 0, "tosadda0", "tosaddax",
2557 0, "tosadda0", "tosaddax",
2562 if (flags & CF_CONST) {
2563 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2564 g_push (flags & ~CF_CONST, 0);
2566 oper (flags, val, ops);
2571 void g_sub (unsigned flags, unsigned long val)
2572 /* Primary = TOS - Primary */
2574 static char* ops [12] = {
2575 0, "tossuba0", "tossubax",
2576 0, "tossuba0", "tossubax",
2581 if (flags & CF_CONST) {
2582 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2583 g_push (flags & ~CF_CONST, 0);
2585 oper (flags, val, ops);
2590 void g_rsub (unsigned flags, unsigned long val)
2591 /* Primary = Primary - TOS */
2593 static char* ops [12] = {
2594 0, "tosrsuba0", "tosrsubax",
2595 0, "tosrsuba0", "tosrsubax",
2599 oper (flags, val, ops);
2604 void g_mul (unsigned flags, unsigned long val)
2605 /* Primary = TOS * Primary */
2607 static char* ops [12] = {
2608 0, "tosmula0", "tosmulax",
2609 0, "tosumula0", "tosumulax",
2616 /* Do strength reduction if the value is constant and a power of two */
2617 if (flags & CF_CONST && (p2 = PowerOf2 (val)) >= 0) {
2618 /* Generate a shift instead */
2623 /* If the right hand side is const, the lhs is not on stack but still
2624 * in the primary register.
2626 if (flags & CF_CONST) {
2628 switch (flags & CF_TYPE) {
2631 if (flags & CF_FORCECHAR) {
2632 /* Handle some special cases */
2636 AddCodeLine ("sta tmp1");
2637 AddCodeLine ("asl a");
2638 AddCodeLine ("clc");
2639 AddCodeLine ("adc tmp1");
2643 AddCodeLine ("sta tmp1");
2644 AddCodeLine ("asl a");
2645 AddCodeLine ("asl a");
2646 AddCodeLine ("clc");
2647 AddCodeLine ("adc tmp1");
2651 AddCodeLine ("sta tmp1");
2652 AddCodeLine ("asl a");
2653 AddCodeLine ("clc");
2654 AddCodeLine ("adc tmp1");
2655 AddCodeLine ("asl a");
2659 AddCodeLine ("sta tmp1");
2660 AddCodeLine ("asl a");
2661 AddCodeLine ("asl a");
2662 AddCodeLine ("clc");
2663 AddCodeLine ("adc tmp1");
2664 AddCodeLine ("asl a");
2673 AddCodeLine ("jsr mulax3");
2676 AddCodeLine ("jsr mulax5");
2679 AddCodeLine ("jsr mulax6");
2682 AddCodeLine ("jsr mulax7");
2685 AddCodeLine ("jsr mulax9");
2688 AddCodeLine ("jsr mulax10");
2700 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2701 * into the normal, non-optimized stuff.
2703 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2704 g_push (flags & ~CF_CONST, 0);
2708 /* Use long way over the stack */
2709 oper (flags, val, ops);
2714 void g_div (unsigned flags, unsigned long val)
2715 /* Primary = TOS / Primary */
2717 static char* ops [12] = {
2718 0, "tosdiva0", "tosdivax",
2719 0, "tosudiva0", "tosudivax",
2724 /* Do strength reduction if the value is constant and a power of two */
2726 if ((flags & CF_CONST) && (p2 = PowerOf2 (val)) >= 0) {
2727 /* Generate a shift instead */
2730 /* Generate a division */
2731 if (flags & CF_CONST) {
2732 /* lhs is not on stack */
2733 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2734 g_push (flags & ~CF_CONST, 0);
2736 oper (flags, val, ops);
2742 void g_mod (unsigned flags, unsigned long val)
2743 /* Primary = TOS % Primary */
2745 static char* ops [12] = {
2746 0, "tosmoda0", "tosmodax",
2747 0, "tosumoda0", "tosumodax",
2753 /* Check if we can do some cost reduction */
2754 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = PowerOf2 (val)) >= 0) {
2755 /* We can do that with an AND operation */
2756 g_and (flags, val - 1);
2758 /* Do it the hard way... */
2759 if (flags & CF_CONST) {
2760 /* lhs is not on stack */
2761 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2762 g_push (flags & ~CF_CONST, 0);
2764 oper (flags, val, ops);
2770 void g_or (unsigned flags, unsigned long val)
2771 /* Primary = TOS | Primary */
2773 static char* ops [12] = {
2774 0, "tosora0", "tosorax",
2775 0, "tosora0", "tosorax",
2780 /* If the right hand side is const, the lhs is not on stack but still
2781 * in the primary register.
2783 if (flags & CF_CONST) {
2785 switch (flags & CF_TYPE) {
2788 if (flags & CF_FORCECHAR) {
2789 if ((val & 0xFF) != 0) {
2790 AddCodeLine ("ora #$%02X", (unsigned char)val);
2798 if ((val & 0xFF) != 0) {
2799 AddCodeLine ("ora #$%02X", (unsigned char)val);
2801 } else if ((val & 0xFF00) == 0xFF00) {
2802 if ((val & 0xFF) != 0) {
2803 AddCodeLine ("ora #$%02X", (unsigned char)val);
2806 } else if (val != 0) {
2807 AddCodeLine ("ora #$%02X", (unsigned char)val);
2808 AddCodeLine ("pha");
2809 AddCodeLine ("txa");
2810 AddCodeLine ("ora #$%02X", (unsigned char)(val >> 8));
2811 AddCodeLine ("tax");
2812 AddCodeLine ("pla");
2818 if ((val & 0xFF) != 0) {
2819 AddCodeLine ("ora #$%02X", (unsigned char)val);
2829 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2830 * into the normal, non-optimized stuff. Note: The standard stuff will
2831 * always work with ints.
2833 flags &= ~CF_FORCECHAR;
2834 g_push (flags & ~CF_CONST, 0);
2837 /* Use long way over the stack */
2838 oper (flags, val, ops);
2843 void g_xor (unsigned flags, unsigned long val)
2844 /* Primary = TOS ^ Primary */
2846 static char* ops [12] = {
2847 0, "tosxora0", "tosxorax",
2848 0, "tosxora0", "tosxorax",
2854 /* If the right hand side is const, the lhs is not on stack but still
2855 * in the primary register.
2857 if (flags & CF_CONST) {
2859 switch (flags & CF_TYPE) {
2862 if (flags & CF_FORCECHAR) {
2863 if ((val & 0xFF) != 0) {
2864 AddCodeLine ("eor #$%02X", (unsigned char)val);
2873 AddCodeLine ("eor #$%02X", (unsigned char)val);
2875 } else if (val != 0) {
2876 if ((val & 0xFF) != 0) {
2877 AddCodeLine ("eor #$%02X", (unsigned char)val);
2879 AddCodeLine ("pha");
2880 AddCodeLine ("txa");
2881 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2882 AddCodeLine ("tax");
2883 AddCodeLine ("pla");
2890 AddCodeLine ("eor #$%02X", (unsigned char)val);
2900 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2901 * into the normal, non-optimized stuff. Note: The standard stuff will
2902 * always work with ints.
2904 flags &= ~CF_FORCECHAR;
2905 g_push (flags & ~CF_CONST, 0);
2908 /* Use long way over the stack */
2909 oper (flags, val, ops);
2914 void g_and (unsigned Flags, unsigned long Val)
2915 /* Primary = TOS & Primary */
2917 static char* ops [12] = {
2918 0, "tosanda0", "tosandax",
2919 0, "tosanda0", "tosandax",
2924 /* If the right hand side is const, the lhs is not on stack but still
2925 * in the primary register.
2927 if (Flags & CF_CONST) {
2929 switch (Flags & CF_TYPE) {
2932 if (Flags & CF_FORCECHAR) {
2933 if ((Val & 0xFF) != 0xFF) {
2934 AddCodeLine ("and #$%02X", (unsigned char)Val);
2940 if ((Val & 0xFFFF) != 0xFFFF) {
2945 } else if (Val != 0xFF) {
2946 AddCodeLine ("and #$%02X", (unsigned char)Val);
2948 } else if ((Val & 0xFF00) == 0xFF00) {
2949 AddCodeLine ("and #$%02X", (unsigned char)Val);
2950 } else if ((Val & 0x00FF) == 0x0000) {
2951 AddCodeLine ("txa");
2952 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2953 AddCodeLine ("tax");
2956 AddCodeLine ("tay");
2957 AddCodeLine ("txa");
2958 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2959 AddCodeLine ("tax");
2960 AddCodeLine ("tya");
2961 if ((Val & 0x00FF) != 0x00FF) {
2962 AddCodeLine ("and #$%02X", (unsigned char)Val);
2971 AddCodeLine ("stx sreg+1");
2972 AddCodeLine ("stx sreg");
2973 if ((Val & 0xFF) != 0xFF) {
2974 AddCodeLine ("and #$%02X", (unsigned char)Val);
2977 } else if (Val == 0xFF00) {
2979 AddCodeLine ("sta sreg+1");
2980 AddCodeLine ("sta sreg");
2989 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2990 * into the normal, non-optimized stuff. Note: The standard stuff will
2991 * always work with ints.
2993 Flags &= ~CF_FORCECHAR;
2994 g_push (Flags & ~CF_CONST, 0);
2997 /* Use long way over the stack */
2998 oper (Flags, Val, ops);
3003 void g_asr (unsigned flags, unsigned long val)
3004 /* Primary = TOS >> Primary */
3006 static char* ops [12] = {
3013 /* If the right hand side is const, the lhs is not on stack but still
3014 * in the primary register.
3016 if (flags & CF_CONST) {
3018 switch (flags & CF_TYPE) {
3022 if (val >= 8 && (flags & CF_UNSIGNED)) {
3023 AddCodeLine ("txa");
3030 } else if (val >= 1 && val <= 4) {
3031 if (flags & CF_UNSIGNED) {
3032 AddCodeLine ("jsr shrax%ld", val);
3034 AddCodeLine ("jsr asrax%ld", val);
3044 } else if (val >= 1 && val <= 4) {
3045 if (flags & CF_UNSIGNED) {
3046 AddCodeLine ("jsr shreax%ld", val);
3048 AddCodeLine ("jsr asreax%ld", val);
3051 } else if (val == 8 && (flags & CF_UNSIGNED)) {
3052 AddCodeLine ("txa");
3053 AddCodeLine ("ldx sreg");
3054 AddCodeLine ("ldy sreg+1");
3055 AddCodeLine ("sty sreg");
3056 AddCodeLine ("ldy #$00");
3057 AddCodeLine ("sty sreg+1");
3059 } else if (val == 16) {
3060 AddCodeLine ("ldy #$00");
3061 AddCodeLine ("ldx sreg+1");
3062 if ((flags & CF_UNSIGNED) == 0) {
3063 unsigned L = GetLocalLabel();
3064 AddCodeLine ("bpl %s", LocalLabelName (L));
3065 AddCodeLine ("dey");
3068 AddCodeLine ("lda sreg");
3069 AddCodeLine ("sty sreg+1");
3070 AddCodeLine ("sty sreg");
3079 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3080 * into the normal, non-optimized stuff. Note: The standard stuff will
3081 * always work with ints.
3083 flags &= ~CF_FORCECHAR;
3084 g_push (flags & ~CF_CONST, 0);
3087 /* Use long way over the stack */
3088 oper (flags, val, ops);
3093 void g_asl (unsigned flags, unsigned long val)
3094 /* Primary = TOS << Primary */
3096 static char* ops [12] = {
3104 /* If the right hand side is const, the lhs is not on stack but still
3105 * in the primary register.
3107 if (flags & CF_CONST) {
3109 switch (flags & CF_TYPE) {
3114 AddCodeLine ("tax");
3115 AddCodeLine ("lda #$00");
3121 } else if (val >= 1 && val <= 4) {
3122 if (flags & CF_UNSIGNED) {
3123 AddCodeLine ("jsr shlax%ld", val);
3125 AddCodeLine ("jsr aslax%ld", val);
3135 } else if (val >= 1 && val <= 4) {
3136 if (flags & CF_UNSIGNED) {
3137 AddCodeLine ("jsr shleax%ld", val);
3139 AddCodeLine ("jsr asleax%ld", val);
3142 } else if (val == 8) {
3143 AddCodeLine ("ldy sreg");
3144 AddCodeLine ("sty sreg+1");
3145 AddCodeLine ("stx sreg");
3146 AddCodeLine ("tax");
3147 AddCodeLine ("lda #$00");
3149 } else if (val == 16) {
3150 AddCodeLine ("stx sreg+1");
3151 AddCodeLine ("sta sreg");
3152 AddCodeLine ("lda #$00");
3153 AddCodeLine ("tax");
3162 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3163 * into the normal, non-optimized stuff. Note: The standard stuff will
3164 * always work with ints.
3166 flags &= ~CF_FORCECHAR;
3167 g_push (flags & ~CF_CONST, 0);
3170 /* Use long way over the stack */
3171 oper (flags, val, ops);
3176 void g_neg (unsigned Flags)
3177 /* Primary = -Primary */
3179 switch (Flags & CF_TYPE) {
3182 if (Flags & CF_FORCECHAR) {
3183 AddCodeLine ("eor #$FF");
3184 AddCodeLine ("clc");
3185 AddCodeLine ("adc #$01");
3191 AddCodeLine ("jsr negax");
3195 AddCodeLine ("jsr negeax");
3205 void g_bneg (unsigned flags)
3206 /* Primary = !Primary */
3208 switch (flags & CF_TYPE) {
3211 AddCodeLine ("jsr bnega");
3215 AddCodeLine ("jsr bnegax");
3219 AddCodeLine ("jsr bnegeax");
3229 void g_com (unsigned Flags)
3230 /* Primary = ~Primary */
3232 switch (Flags & CF_TYPE) {
3235 if (Flags & CF_FORCECHAR) {
3236 AddCodeLine ("eor #$FF");
3242 AddCodeLine ("jsr complax");
3246 AddCodeLine ("jsr compleax");
3256 void g_inc (unsigned flags, unsigned long val)
3257 /* Increment the primary register by a given number */
3259 /* Don't inc by zero */
3264 /* Generate code for the supported types */
3266 switch (flags & CF_TYPE) {
3269 if (flags & CF_FORCECHAR) {
3270 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3272 AddCodeLine ("ina");
3275 AddCodeLine ("clc");
3276 AddCodeLine ("adc #$%02X", (unsigned char)val);
3283 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) {
3284 unsigned L = GetLocalLabel();
3285 AddCodeLine ("ina");
3286 AddCodeLine ("bne %s", LocalLabelName (L));
3287 AddCodeLine ("inx");
3289 } else if (IS_Get (&CodeSizeFactor) < 200) {
3292 AddCodeLine ("jsr incax%lu", val);
3293 } else if (val <= 255) {
3295 AddCodeLine ("jsr incaxy");
3297 g_add (flags | CF_CONST, val);
3300 /* Inline the code */
3302 if ((val & 0xFF) != 0) {
3303 unsigned L = GetLocalLabel();
3304 AddCodeLine ("clc");
3305 AddCodeLine ("adc #$%02X", (unsigned char) val);
3306 AddCodeLine ("bcc %s", LocalLabelName (L));
3307 AddCodeLine ("inx");
3311 AddCodeLine ("inx");
3314 AddCodeLine ("inx");
3317 AddCodeLine ("inx");
3320 AddCodeLine ("clc");
3321 if ((val & 0xFF) != 0) {
3322 AddCodeLine ("adc #$%02X", (unsigned char) val);
3324 AddCodeLine ("pha");
3325 AddCodeLine ("txa");
3326 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3327 AddCodeLine ("tax");
3328 AddCodeLine ("pla");
3336 AddCodeLine ("jsr inceaxy");
3338 g_add (flags | CF_CONST, val);
3350 void g_dec (unsigned flags, unsigned long val)
3351 /* Decrement the primary register by a given number */
3353 /* Don't dec by zero */
3358 /* Generate code for the supported types */
3360 switch (flags & CF_TYPE) {
3363 if (flags & CF_FORCECHAR) {
3364 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3366 AddCodeLine ("dea");
3369 AddCodeLine ("sec");
3370 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3377 if (IS_Get (&CodeSizeFactor) < 200) {
3378 /* Use subroutines */
3380 AddCodeLine ("jsr decax%d", (int) val);
3381 } else if (val <= 255) {
3383 AddCodeLine ("jsr decaxy");
3385 g_sub (flags | CF_CONST, val);
3388 /* Inline the code */
3390 if ((val & 0xFF) != 0) {
3391 unsigned L = GetLocalLabel();
3392 AddCodeLine ("sec");
3393 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3394 AddCodeLine ("bcs %s", LocalLabelName (L));
3395 AddCodeLine ("dex");
3399 AddCodeLine ("dex");
3402 AddCodeLine ("dex");
3405 AddCodeLine ("sec");
3406 if ((val & 0xFF) != 0) {
3407 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3409 AddCodeLine ("pha");
3410 AddCodeLine ("txa");
3411 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3412 AddCodeLine ("tax");
3413 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 char* ops [12] = {
3447 "toseq00", "toseqa0", "toseqax",
3448 "toseq00", "toseqa0", "toseqax",
3455 /* If the right hand side is const, the lhs is not on stack but still
3456 * in the primary register.
3458 if (flags & CF_CONST) {
3460 switch (flags & CF_TYPE) {
3463 if (flags & CF_FORCECHAR) {
3464 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3465 AddCodeLine ("jsr booleq");
3471 L = GetLocalLabel();
3472 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3473 AddCodeLine ("bne %s", LocalLabelName (L));
3474 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3476 AddCodeLine ("jsr booleq");
3486 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3487 * into the normal, non-optimized stuff. Note: The standard stuff will
3488 * always work with ints.
3490 flags &= ~CF_FORCECHAR;
3491 g_push (flags & ~CF_CONST, 0);
3494 /* Use long way over the stack */
3495 oper (flags, val, ops);
3500 void g_ne (unsigned flags, unsigned long val)
3501 /* Test for not equal */
3503 static char* ops [12] = {
3504 "tosne00", "tosnea0", "tosneax",
3505 "tosne00", "tosnea0", "tosneax",
3512 /* If the right hand side is const, the lhs is not on stack but still
3513 * in the primary register.
3515 if (flags & CF_CONST) {
3517 switch (flags & CF_TYPE) {
3520 if (flags & CF_FORCECHAR) {
3521 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3522 AddCodeLine ("jsr boolne");
3528 L = GetLocalLabel();
3529 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3530 AddCodeLine ("bne %s", LocalLabelName (L));
3531 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3533 AddCodeLine ("jsr boolne");
3543 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3544 * into the normal, non-optimized stuff. Note: The standard stuff will
3545 * always work with ints.
3547 flags &= ~CF_FORCECHAR;
3548 g_push (flags & ~CF_CONST, 0);
3551 /* Use long way over the stack */
3552 oper (flags, val, ops);
3557 void g_lt (unsigned flags, unsigned long val)
3558 /* Test for less than */
3560 static char* ops [12] = {
3561 "toslt00", "toslta0", "tosltax",
3562 "tosult00", "tosulta0", "tosultax",
3567 /* If the right hand side is const, the lhs is not on stack but still
3568 * in the primary register.
3570 if (flags & CF_CONST) {
3572 /* Because the handling of the overflow flag is too complex for
3573 * inlining, we can handle only unsigned compares, and signed
3574 * compares against zero here.
3576 if (flags & CF_UNSIGNED) {
3578 /* Give a warning in some special cases */
3580 Warning ("Condition is never true");
3581 AddCodeLine ("jsr return0");
3585 /* Look at the type */
3586 switch (flags & CF_TYPE) {
3589 if (flags & CF_FORCECHAR) {
3590 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3591 AddCodeLine ("jsr boolult");
3597 /* If the low byte is zero, we must only test the high byte */
3598 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3599 if ((val & 0xFF) != 0) {
3600 unsigned L = GetLocalLabel();
3601 AddCodeLine ("bne %s", LocalLabelName (L));
3602 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3605 AddCodeLine ("jsr boolult");
3609 /* Do a subtraction */
3610 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3611 AddCodeLine ("txa");
3612 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3613 AddCodeLine ("lda sreg");
3614 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3615 AddCodeLine ("lda sreg+1");
3616 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3617 AddCodeLine ("jsr boolult");
3624 } else if (val == 0) {
3626 /* Look at the type */
3627 switch (flags & CF_TYPE) {
3630 if (flags & CF_FORCECHAR) {
3631 AddCodeLine ("tax");
3632 AddCodeLine ("jsr boollt");
3638 /* Just check the high byte */
3639 AddCodeLine ("txa");
3640 AddCodeLine ("jsr boollt");
3644 /* Just check the high byte */
3645 AddCodeLine ("lda sreg+1");
3646 AddCodeLine ("jsr boollt");
3654 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3655 * into the normal, non-optimized stuff. Note: The standard stuff will
3656 * always work with ints.
3658 flags &= ~CF_FORCECHAR;
3659 g_push (flags & ~CF_CONST, 0);
3662 /* Use long way over the stack */
3663 oper (flags, val, ops);
3668 void g_le (unsigned flags, unsigned long val)
3669 /* Test for less than or equal to */
3671 static char* ops [12] = {
3672 "tosle00", "toslea0", "tosleax",
3673 "tosule00", "tosulea0", "tosuleax",
3679 /* If the right hand side is const, the lhs is not on stack but still
3680 * in the primary register.
3682 if (flags & CF_CONST) {
3684 /* Look at the type */
3685 switch (flags & CF_TYPE) {
3688 if (flags & CF_FORCECHAR) {
3689 if (flags & CF_UNSIGNED) {
3690 /* Unsigned compare */
3692 /* Use < instead of <= because the former gives
3693 * better code on the 6502 than the latter.
3695 g_lt (flags, val+1);
3698 Warning ("Condition is always true");
3699 AddCodeLine ("jsr return1");
3702 /* Signed compare */
3703 if ((long) val < 0x7F) {
3704 /* Use < instead of <= because the former gives
3705 * better code on the 6502 than the latter.
3707 g_lt (flags, val+1);
3710 Warning ("Condition is always true");
3711 AddCodeLine ("jsr return1");
3719 if (flags & CF_UNSIGNED) {
3720 /* Unsigned compare */
3722 /* Use < instead of <= because the former gives
3723 * better code on the 6502 than the latter.
3725 g_lt (flags, val+1);
3728 Warning ("Condition is always true");
3729 AddCodeLine ("jsr return1");
3732 /* Signed compare */
3733 if ((long) val < 0x7FFF) {
3734 g_lt (flags, val+1);
3737 Warning ("Condition is always true");
3738 AddCodeLine ("jsr return1");
3744 if (flags & CF_UNSIGNED) {
3745 /* Unsigned compare */
3746 if (val < 0xFFFFFFFF) {
3747 /* Use < instead of <= because the former gives
3748 * better code on the 6502 than the latter.
3750 g_lt (flags, val+1);
3753 Warning ("Condition is always true");
3754 AddCodeLine ("jsr return1");
3757 /* Signed compare */
3758 if ((long) val < 0x7FFFFFFF) {
3759 g_lt (flags, val+1);
3762 Warning ("Condition is always true");
3763 AddCodeLine ("jsr return1");
3772 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3773 * into the normal, non-optimized stuff. Note: The standard stuff will
3774 * always work with ints.
3776 flags &= ~CF_FORCECHAR;
3777 g_push (flags & ~CF_CONST, 0);
3780 /* Use long way over the stack */
3781 oper (flags, val, ops);
3786 void g_gt (unsigned flags, unsigned long val)
3787 /* Test for greater than */
3789 static char* ops [12] = {
3790 "tosgt00", "tosgta0", "tosgtax",
3791 "tosugt00", "tosugta0", "tosugtax",
3797 /* If the right hand side is const, the lhs is not on stack but still
3798 * in the primary register.
3800 if (flags & CF_CONST) {
3802 /* Look at the type */
3803 switch (flags & CF_TYPE) {
3806 if (flags & CF_FORCECHAR) {
3807 if (flags & CF_UNSIGNED) {
3809 /* If we have a compare > 0, we will replace it by
3810 * != 0 here, since both are identical but the
3811 * latter is easier to optimize.
3814 } else if (val < 0xFF) {
3815 /* Use >= instead of > because the former gives
3816 * better code on the 6502 than the latter.
3818 g_ge (flags, val+1);
3821 Warning ("Condition is never true");
3822 AddCodeLine ("jsr return0");
3825 if ((long) val < 0x7F) {
3826 /* Use >= instead of > because the former gives
3827 * better code on the 6502 than the latter.
3829 g_ge (flags, val+1);
3832 Warning ("Condition is never true");
3833 AddCodeLine ("jsr return0");
3841 if (flags & CF_UNSIGNED) {
3842 /* Unsigned compare */
3844 /* If we have a compare > 0, we will replace it by
3845 * != 0 here, since both are identical but the latter
3846 * is easier to optimize.
3849 } else if (val < 0xFFFF) {
3850 /* Use >= instead of > because the former gives better
3851 * code on the 6502 than the latter.
3853 g_ge (flags, val+1);
3856 Warning ("Condition is never true");
3857 AddCodeLine ("jsr return0");
3860 /* Signed compare */
3861 if ((long) val < 0x7FFF) {
3862 g_ge (flags, val+1);
3865 Warning ("Condition is never true");
3866 AddCodeLine ("jsr return0");
3872 if (flags & CF_UNSIGNED) {
3873 /* Unsigned compare */
3875 /* If we have a compare > 0, we will replace it by
3876 * != 0 here, since both are identical but the latter
3877 * is easier to optimize.
3880 } else if (val < 0xFFFFFFFF) {
3881 /* Use >= instead of > because the former gives better
3882 * code on the 6502 than the latter.
3884 g_ge (flags, val+1);
3887 Warning ("Condition is never true");
3888 AddCodeLine ("jsr return0");
3891 /* Signed compare */
3892 if ((long) val < 0x7FFFFFFF) {
3893 g_ge (flags, val+1);
3896 Warning ("Condition is never true");
3897 AddCodeLine ("jsr return0");
3906 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3907 * into the normal, non-optimized stuff. Note: The standard stuff will
3908 * always work with ints.
3910 flags &= ~CF_FORCECHAR;
3911 g_push (flags & ~CF_CONST, 0);
3914 /* Use long way over the stack */
3915 oper (flags, val, ops);
3920 void g_ge (unsigned flags, unsigned long val)
3921 /* Test for greater than or equal to */
3923 static char* ops [12] = {
3924 "tosge00", "tosgea0", "tosgeax",
3925 "tosuge00", "tosugea0", "tosugeax",
3931 /* If the right hand side is const, the lhs is not on stack but still
3932 * in the primary register.
3934 if (flags & CF_CONST) {
3936 /* Because the handling of the overflow flag is too complex for
3937 * inlining, we can handle only unsigned compares, and signed
3938 * compares against zero here.
3940 if (flags & CF_UNSIGNED) {
3942 /* Give a warning in some special cases */
3944 Warning ("Condition is always true");
3945 AddCodeLine ("jsr return1");
3949 /* Look at the type */
3950 switch (flags & CF_TYPE) {
3953 if (flags & CF_FORCECHAR) {
3954 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3955 AddCodeLine ("jsr booluge");
3961 /* If the low byte is zero, we must only test the high byte */
3962 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3963 if ((val & 0xFF) != 0) {
3964 unsigned L = GetLocalLabel();
3965 AddCodeLine ("bne %s", LocalLabelName (L));
3966 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3969 AddCodeLine ("jsr booluge");
3973 /* Do a subtraction */
3974 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3975 AddCodeLine ("txa");
3976 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3977 AddCodeLine ("lda sreg");
3978 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3979 AddCodeLine ("lda sreg+1");
3980 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3981 AddCodeLine ("jsr booluge");
3988 } else if (val == 0) {
3990 /* Look at the type */
3991 switch (flags & CF_TYPE) {
3994 if (flags & CF_FORCECHAR) {
3995 AddCodeLine ("tax");
3996 AddCodeLine ("jsr boolge");
4002 /* Just test the high byte */
4003 AddCodeLine ("txa");
4004 AddCodeLine ("jsr boolge");
4008 /* Just test the high byte */
4009 AddCodeLine ("lda sreg+1");
4010 AddCodeLine ("jsr boolge");
4019 /* If we go here, we didn't emit code. Push the lhs on stack and fall
4020 * into the normal, non-optimized stuff. Note: The standard stuff will
4021 * always work with ints.
4023 flags &= ~CF_FORCECHAR;
4024 g_push (flags & ~CF_CONST, 0);
4027 /* Use long way over the stack */
4028 oper (flags, val, ops);
4033 /*****************************************************************************/
4034 /* Allocating static storage */
4035 /*****************************************************************************/
4039 void g_res (unsigned n)
4040 /* Reserve static storage, n bytes */
4042 AddDataLine ("\t.res\t%u,$00", n);
4047 void g_defdata (unsigned flags, unsigned long val, long offs)
4048 /* Define data with the size given in flags */
4050 if (flags & CF_CONST) {
4052 /* Numeric constant */
4053 switch (flags & CF_TYPE) {
4056 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
4060 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
4064 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
4075 /* Create the correct label name */
4076 const char* Label = GetLabelName (flags, val, offs);
4078 /* Labels are always 16 bit */
4079 AddDataLine ("\t.addr\t%s", Label);
4086 void g_defbytes (const void* Bytes, unsigned Count)
4087 /* Output a row of bytes as a constant */
4093 /* Cast the buffer pointer */
4094 const unsigned char* Data = (const unsigned char*) Bytes;
4096 /* Output the stuff */
4099 /* How many go into this line? */
4100 if ((Chunk = Count) > 16) {
4105 /* Output one line */
4106 strcpy (Buf, "\t.byte\t");
4109 B += sprintf (B, "$%02X", *Data++);
4115 /* Output the line */
4122 void g_zerobytes (unsigned Count)
4123 /* Output Count bytes of data initialized with zero */
4126 AddDataLine ("\t.res\t%u,$00", Count);
4132 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4133 /* Initialize a register variable from static initialization data */
4135 /* Register variables do always have less than 128 bytes */
4136 unsigned CodeLabel = GetLocalLabel ();
4138 g_defcodelabel (CodeLabel);
4139 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4140 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4141 AddCodeLine ("dex");
4142 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4147 void g_initauto (unsigned Label, unsigned Size)
4148 /* Initialize a local variable at stack offset zero from static data */
4150 unsigned CodeLabel = GetLocalLabel ();
4152 CheckLocalOffs (Size);
4155 g_defcodelabel (CodeLabel);
4156 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4157 AddCodeLine ("sta (sp),y");
4158 AddCodeLine ("dey");
4159 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4160 } else if (Size <= 256) {
4162 g_defcodelabel (CodeLabel);
4163 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4164 AddCodeLine ("sta (sp),y");
4165 AddCodeLine ("iny");
4166 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4167 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4173 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4174 /* Initialize a static local variable from static initialization data */
4177 unsigned CodeLabel = GetLocalLabel ();
4179 g_defcodelabel (CodeLabel);
4180 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4181 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4182 AddCodeLine ("dey");
4183 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4184 } else if (Size <= 256) {
4185 unsigned CodeLabel = GetLocalLabel ();
4187 g_defcodelabel (CodeLabel);
4188 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4189 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4190 AddCodeLine ("iny");
4191 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4192 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4194 /* Use the easy way here: memcpy */
4195 g_getimmed (CF_STATIC, VarLabel, 0);
4196 AddCodeLine ("jsr pushax");
4197 g_getimmed (CF_STATIC, InitLabel, 0);
4198 AddCodeLine ("jsr pushax");
4199 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4200 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4206 /*****************************************************************************/
4207 /* Switch statement */
4208 /*****************************************************************************/
4212 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4213 /* Generate code for a switch statement */
4215 unsigned NextLabel = 0;
4218 /* Setup registers and determine which compare insn to use */
4219 const char* Compare;
4222 Compare = "cmp #$%02X";
4225 Compare = "cpx #$%02X";
4228 AddCodeLine ("ldy sreg");
4229 Compare = "cpy #$%02X";
4232 AddCodeLine ("ldy sreg+1");
4233 Compare = "cpy #$%02X";
4236 Internal ("Invalid depth in g_switch: %u", Depth);
4239 /* Walk over all nodes */
4240 for (I = 0; I < CollCount (Nodes); ++I) {
4242 /* Get the next case node */
4243 CaseNode* N = CollAtUnchecked (Nodes, I);
4245 /* If we have a next label, define it */
4247 g_defcodelabel (NextLabel);
4251 /* Do the compare */
4252 AddCodeLine (Compare, CN_GetValue (N));
4254 /* If this is the last level, jump directly to the case code if found */
4257 /* Branch if equal */
4258 g_falsejump (0, CN_GetLabel (N));
4262 /* Determine the next label */
4263 if (I == CollCount (Nodes) - 1) {
4264 /* Last node means not found */
4265 g_truejump (0, DefaultLabel);
4267 /* Jump to the next check */
4268 NextLabel = GetLocalLabel ();
4269 g_truejump (0, NextLabel);
4272 /* Check the next level */
4273 g_switch (N->Nodes, DefaultLabel, Depth-1);
4278 /* If we go here, we haven't found the label */
4279 g_jump (DefaultLabel);
4284 /*****************************************************************************/
4285 /* User supplied assembler code */
4286 /*****************************************************************************/
4290 void g_asmcode (struct StrBuf* B)
4291 /* Output one line of assembler code. */
4293 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));