1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2009, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
64 /*****************************************************************************/
66 /*****************************************************************************/
70 static void typeerror (unsigned type)
71 /* Print an error message about an invalid operand type */
73 /* Special handling for floats here: */
74 if ((type & CF_TYPE) == CF_FLOAT) {
75 Fatal ("Floating point type is currently unsupported");
77 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
83 static void CheckLocalOffs (unsigned Offs)
84 /* Check the offset into the stack for 8bit range */
87 /* Too many local vars */
88 Error ("Too many local variables");
94 static const char* GetLabelName (unsigned Flags, unsigned long Label, long Offs)
96 static char Buf [256]; /* Label name */
98 /* Create the correct label name */
99 switch (Flags & CF_ADDRMASK) {
102 /* Static memory cell */
104 xsprintf (Buf, sizeof (Buf), "%s%+ld", LocalLabelName (Label), Offs);
106 xsprintf (Buf, sizeof (Buf), "%s", LocalLabelName (Label));
113 xsprintf (Buf, sizeof (Buf), "_%s%+ld", (char*) Label, Offs);
115 xsprintf (Buf, sizeof (Buf), "_%s", (char*) Label);
120 /* Absolute address */
121 xsprintf (Buf, sizeof (Buf), "$%04X", (int)((Label+Offs) & 0xFFFF));
125 /* Variable in register bank */
126 xsprintf (Buf, sizeof (Buf), "regbank+%u", (unsigned)((Label+Offs) & 0xFFFF));
130 Internal ("Invalid address flags: %04X", Flags);
133 /* Return a pointer to the static buffer */
139 /*****************************************************************************/
140 /* Pre- and postamble */
141 /*****************************************************************************/
145 void g_preamble (void)
146 /* Generate the assembler code preamble */
148 /* Create a new (global) segment list and remember it */
152 /* Identify the compiler version */
154 AddTextLine ("; File generated by cc65 v %s", GetVersionAsString ());
157 /* Insert some object file options */
158 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %s\"",
159 GetVersionAsString ());
161 /* If we're producing code for some other CPU, switch the command set */
163 case CPU_6502: AddTextLine ("\t.setcpu\t\t\"6502\""); break;
164 case CPU_6502X: AddTextLine ("\t.setcpu\t\t\"6502X\""); break;
165 case CPU_65SC02: AddTextLine ("\t.setcpu\t\t\"65SC02\""); break;
166 case CPU_65C02: AddTextLine ("\t.setcpu\t\t\"65C02\""); break;
167 case CPU_65816: AddTextLine ("\t.setcpu\t\t\"65816\""); break;
168 case CPU_HUC6280: AddTextLine ("\t.setcpu\t\t\"HUC6280\""); break;
169 default: Internal ("Unknown CPU: %d", CPU);
173 AddTextLine ("\t.smart\t\ton");
175 /* Allow auto import for runtime library routines */
176 AddTextLine ("\t.autoimport\ton");
178 /* Switch the assembler into case sensitive mode */
179 AddTextLine ("\t.case\t\ton");
181 /* Tell the assembler if we want to generate debug info */
182 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
184 /* Import the stack pointer for direct auto variable access */
185 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
187 /* Define long branch macros */
188 AddTextLine ("\t.macpack\tlongbranch");
193 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
194 /* If debug info is enabled, place a file info into the source */
197 /* We have to place this into the global text segment, so it will
198 * appear before all .dbg line statements.
200 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
206 /*****************************************************************************/
207 /* Segment support */
208 /*****************************************************************************/
212 void g_userodata (void)
213 /* Switch to the read only data segment */
215 UseDataSeg (SEG_RODATA);
220 void g_usedata (void)
221 /* Switch to the data segment */
223 UseDataSeg (SEG_DATA);
229 /* Switch to the bss segment */
231 UseDataSeg (SEG_BSS);
236 void g_segname (segment_t Seg)
237 /* Emit the name of a segment if necessary */
239 /* Emit a segment directive for the data style segments */
242 case SEG_RODATA: S = CS->ROData; break;
243 case SEG_DATA: S = CS->Data; break;
244 case SEG_BSS: S = CS->BSS; break;
245 default: S = 0; break;
248 DS_AddLine (S, ".segment\t\"%s\"", GetSegName (Seg));
254 /*****************************************************************************/
256 /*****************************************************************************/
260 unsigned sizeofarg (unsigned flags)
261 /* Return the size of a function argument type that is encoded in flags */
263 switch (flags & CF_TYPE) {
266 return (flags & CF_FORCECHAR)? 1 : 2;
286 int pop (unsigned flags)
287 /* Pop an argument of the given size */
289 return StackPtr += sizeofarg (flags);
294 int push (unsigned flags)
295 /* Push an argument of the given size */
297 return StackPtr -= sizeofarg (flags);
302 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
303 /* The value in Offs is an offset to an address in a/x. Make sure, an object
304 * of the type given in Flags can be loaded or stored into this address by
305 * adding part of the offset to the address in ax, so that the remaining
306 * offset fits into an index register. Return the remaining offset.
309 /* If the offset is too large for a byte register, add the high byte
310 * of the offset to the primary. Beware: We need a special correction
311 * if the offset in the low byte will overflow in the operation.
313 unsigned O = Offs & ~0xFFU;
314 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
315 /* We need to add the low byte also */
319 /* Do the correction if we need one */
321 g_inc (CF_INT | CF_CONST, O);
325 /* Return the new offset */
331 /*****************************************************************************/
332 /* Functions handling local labels */
333 /*****************************************************************************/
337 void g_defcodelabel (unsigned label)
338 /* Define a local code label */
340 CS_AddLabel (CS->Code, LocalLabelName (label));
345 void g_defdatalabel (unsigned label)
346 /* Define a local data label */
348 AddDataLine ("%s:", LocalLabelName (label));
353 /*****************************************************************************/
354 /* Functions handling global labels */
355 /*****************************************************************************/
359 void g_defgloblabel (const char* Name)
360 /* Define a global label with the given name */
362 /* Global labels are always data labels */
363 AddDataLine ("_%s:", Name);
368 void g_defexport (const char* Name, int ZP)
369 /* Export the given label */
372 AddTextLine ("\t.exportzp\t_%s", Name);
374 AddTextLine ("\t.export\t\t_%s", Name);
380 void g_defimport (const char* Name, int ZP)
381 /* Import the given label */
384 AddTextLine ("\t.importzp\t_%s", Name);
386 AddTextLine ("\t.import\t\t_%s", Name);
392 void g_importstartup (void)
393 /* Forced import of the startup module */
395 AddTextLine ("\t.forceimport\t__STARTUP__");
400 void g_importmainargs (void)
401 /* Forced import of a special symbol that handles arguments to main */
403 AddTextLine ("\t.forceimport\tinitmainargs");
408 /*****************************************************************************/
409 /* Load functions for various registers */
410 /*****************************************************************************/
414 static void ldaconst (unsigned val)
415 /* Load a with a constant */
417 AddCodeLine ("lda #$%02X", val & 0xFF);
422 static void ldxconst (unsigned val)
423 /* Load x with a constant */
425 AddCodeLine ("ldx #$%02X", val & 0xFF);
430 static void ldyconst (unsigned val)
431 /* Load y with a constant */
433 AddCodeLine ("ldy #$%02X", val & 0xFF);
438 /*****************************************************************************/
439 /* Function entry and exit */
440 /*****************************************************************************/
444 /* Remember the argument size of a function. The variable is set by g_enter
445 * and used by g_leave. If the functions gets its argument size by the caller
446 * (variable param list or function without prototype), g_enter will set the
452 void g_enter (unsigned flags, unsigned argsize)
453 /* Function prologue */
455 if ((flags & CF_FIXARGC) != 0) {
456 /* Just remember the argument size for the leave */
460 AddCodeLine ("jsr enter");
467 /* Function epilogue */
469 /* How many bytes of locals do we have to drop? */
470 unsigned k = (unsigned) -StackPtr;
472 /* If we didn't have a variable argument list, don't call leave */
475 /* Drop stackframe if needed. We can only drop 255 bytes at a time. */
478 unsigned ToDrop = (k > 255)? 255 : k;
480 AddCodeLine ("jsr incsp%d", k);
483 AddCodeLine ("jsr addysp");
491 /* Nothing to drop */
492 AddCodeLine ("jsr leave");
494 /* We've a stack frame to drop */
497 AddCodeLine ("jsr addysp");
501 AddCodeLine ("jsr leavey");
505 /* Add the final rts */
511 /*****************************************************************************/
512 /* Register variables */
513 /*****************************************************************************/
517 void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
518 /* Swap a register variable with a location on the stack */
520 /* Calculate the actual stack offset and check it */
521 StackOffs -= StackPtr;
522 CheckLocalOffs (StackOffs);
527 if (IS_Get (&CodeSizeFactor) < 165) {
528 ldyconst (StackOffs);
530 AddCodeLine ("jsr regswap1");
532 ldyconst (StackOffs);
533 AddCodeLine ("lda (sp),y");
534 AddCodeLine ("ldx regbank%+d", RegOffs);
535 AddCodeLine ("sta regbank%+d", RegOffs);
537 AddCodeLine ("sta (sp),y");
540 } else if (Bytes == 2) {
542 ldyconst (StackOffs);
544 AddCodeLine ("jsr regswap2");
548 ldyconst (StackOffs);
551 AddCodeLine ("jsr regswap");
557 void g_save_regvars (int RegOffs, unsigned Bytes)
558 /* Save register variables */
560 /* Don't loop for up to two bytes */
563 AddCodeLine ("lda regbank%+d", RegOffs);
564 AddCodeLine ("jsr pusha");
566 } else if (Bytes == 2) {
568 AddCodeLine ("lda regbank%+d", RegOffs);
569 AddCodeLine ("ldx regbank%+d", RegOffs+1);
570 AddCodeLine ("jsr pushax");
574 /* More than two bytes - loop */
575 unsigned Label = GetLocalLabel ();
577 ldyconst (Bytes - 1);
579 g_defcodelabel (Label);
580 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
581 AddCodeLine ("sta (sp),y");
584 AddCodeLine ("bne %s", LocalLabelName (Label));
588 /* We pushed stuff, correct the stack pointer */
594 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
595 /* Restore register variables */
597 /* Calculate the actual stack offset and check it */
598 StackOffs -= StackPtr;
599 CheckLocalOffs (StackOffs);
601 /* Don't loop for up to two bytes */
604 ldyconst (StackOffs);
605 AddCodeLine ("lda (sp),y");
606 AddCodeLine ("sta regbank%+d", RegOffs);
608 } else if (Bytes == 2) {
610 ldyconst (StackOffs);
611 AddCodeLine ("lda (sp),y");
612 AddCodeLine ("sta regbank%+d", RegOffs);
614 AddCodeLine ("lda (sp),y");
615 AddCodeLine ("sta regbank%+d", RegOffs+1);
617 } else if (Bytes == 3 && IS_Get (&CodeSizeFactor) >= 133) {
619 ldyconst (StackOffs);
620 AddCodeLine ("lda (sp),y");
621 AddCodeLine ("sta regbank%+d", RegOffs);
623 AddCodeLine ("lda (sp),y");
624 AddCodeLine ("sta regbank%+d", RegOffs+1);
626 AddCodeLine ("lda (sp),y");
627 AddCodeLine ("sta regbank%+d", RegOffs+2);
629 } else if (StackOffs <= RegOffs) {
631 /* More bytes, but the relation between the register offset in the
632 * register bank and the stack offset allows us to generate short
633 * code that uses just one index register.
635 unsigned Label = GetLocalLabel ();
636 ldyconst (StackOffs);
637 g_defcodelabel (Label);
638 AddCodeLine ("lda (sp),y");
639 AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs);
641 AddCodeLine ("cpy #$%02X", StackOffs + Bytes);
642 AddCodeLine ("bne %s", LocalLabelName (Label));
646 /* Ok, this is the generic code. We need to save X because the
647 * caller will only save A.
649 unsigned Label = GetLocalLabel ();
650 AddCodeLine ("stx tmp1");
651 ldyconst (StackOffs + Bytes - 1);
652 ldxconst (Bytes - 1);
653 g_defcodelabel (Label);
654 AddCodeLine ("lda (sp),y");
655 AddCodeLine ("sta regbank%+d,x", RegOffs);
658 AddCodeLine ("bpl %s", LocalLabelName (Label));
659 AddCodeLine ("ldx tmp1");
666 /*****************************************************************************/
667 /* Fetching memory cells */
668 /*****************************************************************************/
672 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
673 /* Load a constant into the primary register */
675 unsigned char B1, B2, B3, B4;
679 if ((Flags & CF_CONST) != 0) {
681 /* Numeric constant */
682 switch (Flags & CF_TYPE) {
685 if ((Flags & CF_FORCECHAR) != 0) {
691 ldxconst ((Val >> 8) & 0xFF);
692 ldaconst (Val & 0xFF);
696 /* Split the value into 4 bytes */
697 B1 = (unsigned char) (Val >> 0);
698 B2 = (unsigned char) (Val >> 8);
699 B3 = (unsigned char) (Val >> 16);
700 B4 = (unsigned char) (Val >> 24);
702 /* Remember which bytes are done */
706 AddCodeLine ("ldx #$%02X", B2);
709 AddCodeLine ("stx sreg");
713 AddCodeLine ("stx sreg+1");
716 if ((Done & 0x04) == 0 && B1 != B3) {
717 AddCodeLine ("lda #$%02X", B3);
718 AddCodeLine ("sta sreg");
721 if ((Done & 0x08) == 0 && B1 != B4) {
722 AddCodeLine ("lda #$%02X", B4);
723 AddCodeLine ("sta sreg+1");
726 AddCodeLine ("lda #$%02X", B1);
728 if ((Done & 0x04) == 0) {
730 AddCodeLine ("sta sreg");
732 if ((Done & 0x08) == 0) {
734 AddCodeLine ("sta sreg+1");
746 /* Some sort of label */
747 const char* Label = GetLabelName (Flags, Val, Offs);
749 /* Load the address into the primary */
750 AddCodeLine ("lda #<(%s)", Label);
751 AddCodeLine ("ldx #>(%s)", Label);
758 void g_getstatic (unsigned flags, unsigned long label, long offs)
759 /* Fetch an static memory cell into the primary register */
761 /* Create the correct label name */
762 const char* lbuf = GetLabelName (flags, label, offs);
764 /* Check the size and generate the correct load operation */
765 switch (flags & CF_TYPE) {
768 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
769 AddCodeLine ("lda %s", lbuf); /* load A from the label */
772 AddCodeLine ("lda %s", lbuf); /* load A from the label */
773 if (!(flags & CF_UNSIGNED)) {
774 /* Must sign extend */
775 unsigned L = GetLocalLabel ();
776 AddCodeLine ("bpl %s", LocalLabelName (L));
784 AddCodeLine ("lda %s", lbuf);
785 if (flags & CF_TEST) {
786 AddCodeLine ("ora %s+1", lbuf);
788 AddCodeLine ("ldx %s+1", lbuf);
793 if (flags & CF_TEST) {
794 AddCodeLine ("lda %s+3", lbuf);
795 AddCodeLine ("ora %s+2", lbuf);
796 AddCodeLine ("ora %s+1", lbuf);
797 AddCodeLine ("ora %s+0", lbuf);
799 AddCodeLine ("lda %s+3", lbuf);
800 AddCodeLine ("sta sreg+1");
801 AddCodeLine ("lda %s+2", lbuf);
802 AddCodeLine ("sta sreg");
803 AddCodeLine ("ldx %s+1", lbuf);
804 AddCodeLine ("lda %s", lbuf);
816 void g_getlocal (unsigned Flags, int Offs)
817 /* Fetch specified local object (local var). */
820 switch (Flags & CF_TYPE) {
823 CheckLocalOffs (Offs);
824 if ((Flags & CF_FORCECHAR) || (Flags & CF_TEST)) {
826 AddCodeLine ("lda (sp),y");
829 AddCodeLine ("ldx #$00");
830 AddCodeLine ("lda (sp),y");
831 if ((Flags & CF_UNSIGNED) == 0) {
832 unsigned L = GetLocalLabel();
833 AddCodeLine ("bpl %s", LocalLabelName (L));
841 CheckLocalOffs (Offs + 1);
842 AddCodeLine ("ldy #$%02X", (unsigned char) (Offs+1));
843 if (Flags & CF_TEST) {
844 AddCodeLine ("lda (sp),y");
846 AddCodeLine ("ora (sp),y");
847 } else if (IS_Get (&CodeSizeFactor) < 165) {
848 AddCodeLine ("jsr ldaxysp");
850 AddCodeLine ("lda (sp),y");
853 AddCodeLine ("lda (sp),y");
858 CheckLocalOffs (Offs + 3);
859 AddCodeLine ("ldy #$%02X", (unsigned char) (Offs+3));
860 AddCodeLine ("jsr ldeaxysp");
861 if (Flags & CF_TEST) {
873 void g_getind (unsigned Flags, unsigned Offs)
874 /* Fetch the specified object type indirect through the primary register
875 * into the primary register
878 /* If the offset is greater than 255, add the part that is > 255 to
879 * the primary. This way we get an easy addition and use the low byte
882 Offs = MakeByteOffs (Flags, Offs);
884 /* Handle the indirect fetch */
885 switch (Flags & CF_TYPE) {
888 /* Character sized */
889 if (Flags & CF_UNSIGNED) {
891 AddCodeLine ("jsr ldauidx");
894 AddCodeLine ("jsr ldaidx");
899 if (Flags & CF_TEST) {
901 AddCodeLine ("sta ptr1");
902 AddCodeLine ("stx ptr1+1");
903 AddCodeLine ("lda (ptr1),y");
905 AddCodeLine ("ora (ptr1),y");
908 AddCodeLine ("jsr ldaxidx");
914 AddCodeLine ("jsr ldeaxidx");
915 if (Flags & CF_TEST) {
928 void g_leasp (int Offs)
929 /* Fetch the address of the specified symbol into the primary register */
931 unsigned char Lo, Hi;
933 /* Calculate the offset relative to sp */
936 /* Get low and high byte */
937 Lo = (unsigned char) Offs;
938 Hi = (unsigned char) (Offs >> 8);
943 AddCodeLine ("lda sp");
944 AddCodeLine ("ldx sp+1");
949 AddCodeLine ("lda sp+1");
951 AddCodeLine ("adc #$%02X", Hi);
953 AddCodeLine ("lda sp");
955 } else if (Hi == 0) {
957 if (IS_Get (&CodeSizeFactor) < 200) {
958 /* 8 bit offset with subroutine call */
959 AddCodeLine ("lda #$%02X", Lo);
960 AddCodeLine ("jsr leaa0sp");
962 /* 8 bit offset inlined */
963 unsigned L = GetLocalLabel ();
964 AddCodeLine ("lda sp");
965 AddCodeLine ("ldx sp+1");
967 AddCodeLine ("adc #$%02X", Lo);
968 AddCodeLine ("bcc %s", LocalLabelName (L));
972 } else if (IS_Get (&CodeSizeFactor) < 170) {
973 /* Full 16 bit offset with subroutine call */
974 AddCodeLine ("lda #$%02X", Lo);
975 AddCodeLine ("ldx #$%02X", Hi);
976 AddCodeLine ("jsr leaaxsp");
978 /* Full 16 bit offset inlined */
979 AddCodeLine ("lda sp");
981 AddCodeLine ("adc #$%02X", Lo);
983 AddCodeLine ("lda sp+1");
984 AddCodeLine ("adc #$%02X", Hi);
992 void g_leavariadic (int Offs)
993 /* Fetch the address of a parameter in a variadic function into the primary
997 unsigned ArgSizeOffs;
999 /* Calculate the offset relative to sp */
1002 /* Get the offset of the parameter which is stored at sp+0 on function
1003 * entry and check if this offset is reachable with a byte offset.
1005 CHECK (StackPtr <= 0);
1006 ArgSizeOffs = -StackPtr;
1007 CheckLocalOffs (ArgSizeOffs);
1009 /* Get the size of all parameters. */
1010 ldyconst (ArgSizeOffs);
1011 AddCodeLine ("lda (sp),y");
1013 /* Add the value of the stackpointer */
1014 if (IS_Get (&CodeSizeFactor) > 250) {
1015 unsigned L = GetLocalLabel();
1016 AddCodeLine ("ldx sp+1");
1017 AddCodeLine ("clc");
1018 AddCodeLine ("adc sp");
1019 AddCodeLine ("bcc %s", LocalLabelName (L));
1020 AddCodeLine ("inx");
1023 AddCodeLine ("ldx #$00");
1024 AddCodeLine ("jsr leaaxsp");
1027 /* Add the offset to the primary */
1029 g_inc (CF_INT | CF_CONST, Offs);
1030 } else if (Offs < 0) {
1031 g_dec (CF_INT | CF_CONST, -Offs);
1037 /*****************************************************************************/
1038 /* Store into memory */
1039 /*****************************************************************************/
1043 void g_putstatic (unsigned flags, unsigned long label, long offs)
1044 /* Store the primary register into the specified static memory cell */
1046 /* Create the correct label name */
1047 const char* lbuf = GetLabelName (flags, label, offs);
1049 /* Check the size and generate the correct store operation */
1050 switch (flags & CF_TYPE) {
1053 AddCodeLine ("sta %s", lbuf);
1057 AddCodeLine ("sta %s", lbuf);
1058 AddCodeLine ("stx %s+1", lbuf);
1062 AddCodeLine ("sta %s", lbuf);
1063 AddCodeLine ("stx %s+1", lbuf);
1064 AddCodeLine ("ldy sreg");
1065 AddCodeLine ("sty %s+2", lbuf);
1066 AddCodeLine ("ldy sreg+1");
1067 AddCodeLine ("sty %s+3", lbuf);
1078 void g_putlocal (unsigned Flags, int Offs, long Val)
1079 /* Put data into local object. */
1082 CheckLocalOffs (Offs);
1083 switch (Flags & CF_TYPE) {
1086 if (Flags & CF_CONST) {
1087 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1090 AddCodeLine ("sta (sp),y");
1094 if (Flags & CF_CONST) {
1096 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1097 AddCodeLine ("sta (sp),y");
1098 if ((Flags & CF_NOKEEP) == 0) {
1099 /* Place high byte into X */
1100 AddCodeLine ("tax");
1102 if ((Val & 0xFF) == Offs+1) {
1103 /* The value we need is already in Y */
1104 AddCodeLine ("tya");
1105 AddCodeLine ("dey");
1107 AddCodeLine ("dey");
1108 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1110 AddCodeLine ("sta (sp),y");
1112 if ((Flags & CF_NOKEEP) == 0 || IS_Get (&CodeSizeFactor) < 160) {
1114 AddCodeLine ("jsr staxysp");
1117 AddCodeLine ("sta (sp),y");
1118 AddCodeLine ("iny");
1119 AddCodeLine ("txa");
1120 AddCodeLine ("sta (sp),y");
1126 if (Flags & CF_CONST) {
1127 g_getimmed (Flags, Val, 0);
1130 AddCodeLine ("jsr steaxysp");
1141 void g_putind (unsigned Flags, unsigned Offs)
1142 /* Store the specified object type in the primary register at the address
1143 * on the top of the stack
1146 /* We can handle offsets below $100 directly, larger offsets must be added
1147 * to the address. Since a/x is in use, best code is achieved by adding
1148 * just the high byte. Be sure to check if the low byte will overflow while
1151 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1153 /* Overflow - we need to add the low byte also */
1154 AddCodeLine ("ldy #$00");
1155 AddCodeLine ("clc");
1156 AddCodeLine ("pha");
1157 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1158 AddCodeLine ("adc (sp),y");
1159 AddCodeLine ("sta (sp),y");
1160 AddCodeLine ("iny");
1161 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1162 AddCodeLine ("adc (sp),y");
1163 AddCodeLine ("sta (sp),y");
1164 AddCodeLine ("pla");
1166 /* Complete address is on stack, new offset is zero */
1169 } else if ((Offs & 0xFF00) != 0) {
1171 /* We can just add the high byte */
1172 AddCodeLine ("ldy #$01");
1173 AddCodeLine ("clc");
1174 AddCodeLine ("pha");
1175 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1176 AddCodeLine ("adc (sp),y");
1177 AddCodeLine ("sta (sp),y");
1178 AddCodeLine ("pla");
1180 /* Offset is now just the low byte */
1184 /* Check the size and determine operation */
1185 switch (Flags & CF_TYPE) {
1189 AddCodeLine ("jsr staspidx");
1194 AddCodeLine ("jsr staxspidx");
1199 AddCodeLine ("jsr steaxspidx");
1207 /* Pop the argument which is always a pointer */
1213 /*****************************************************************************/
1214 /* type conversion and similiar stuff */
1215 /*****************************************************************************/
1219 void g_toslong (unsigned flags)
1220 /* Make sure, the value on TOS is a long. Convert if necessary */
1222 switch (flags & CF_TYPE) {
1226 if (flags & CF_UNSIGNED) {
1227 AddCodeLine ("jsr tosulong");
1229 AddCodeLine ("jsr toslong");
1244 void g_tosint (unsigned flags)
1245 /* Make sure, the value on TOS is an int. Convert if necessary */
1247 switch (flags & CF_TYPE) {
1254 AddCodeLine ("jsr tosint");
1265 void g_regint (unsigned Flags)
1266 /* Make sure, the value in the primary register an int. Convert if necessary */
1270 switch (Flags & CF_TYPE) {
1273 if (Flags & CF_FORCECHAR) {
1274 /* Conversion is from char */
1275 if (Flags & CF_UNSIGNED) {
1276 AddCodeLine ("ldx #$00");
1278 L = GetLocalLabel();
1279 AddCodeLine ("ldx #$00");
1280 AddCodeLine ("cmp #$80");
1281 AddCodeLine ("bcc %s", LocalLabelName (L));
1282 AddCodeLine ("dex");
1299 void g_reglong (unsigned Flags)
1300 /* Make sure, the value in the primary register a long. Convert if necessary */
1304 switch (Flags & CF_TYPE) {
1307 if (Flags & CF_FORCECHAR) {
1308 /* Conversion is from char */
1309 if (Flags & CF_UNSIGNED) {
1310 if (IS_Get (&CodeSizeFactor) >= 200) {
1311 AddCodeLine ("ldx #$00");
1312 AddCodeLine ("stx sreg");
1313 AddCodeLine ("stx sreg+1");
1315 AddCodeLine ("jsr aulong");
1318 if (IS_Get (&CodeSizeFactor) >= 366) {
1319 L = GetLocalLabel();
1320 AddCodeLine ("ldx #$00");
1321 AddCodeLine ("cmp #$80");
1322 AddCodeLine ("bcc %s", LocalLabelName (L));
1323 AddCodeLine ("dex");
1325 AddCodeLine ("stx sreg");
1326 AddCodeLine ("stx sreg+1");
1328 AddCodeLine ("jsr along");
1335 if (Flags & CF_UNSIGNED) {
1336 if (IS_Get (&CodeSizeFactor) >= 200) {
1338 AddCodeLine ("sty sreg");
1339 AddCodeLine ("sty sreg+1");
1341 AddCodeLine ("jsr axulong");
1344 AddCodeLine ("jsr axlong");
1358 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1359 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1360 * value, that corresponds to the value on TOS, rhs corresponds to the value
1361 * in (e)ax. The return value is the the flags value for the resulting type.
1364 unsigned ltype, rtype;
1367 /* Get the type spec from the flags */
1368 ltype = lhs & CF_TYPE;
1369 rtype = rhs & CF_TYPE;
1371 /* Check if a conversion is needed */
1372 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1373 /* We must promote the primary register to long */
1375 /* Get the new rhs type */
1376 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1378 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1379 /* We must promote the lhs to long */
1385 /* Get the new rhs type */
1386 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1390 /* Determine the result type for the operation:
1391 * - The result is const if both operands are const.
1392 * - The result is unsigned if one of the operands is unsigned.
1393 * - The result is long if one of the operands is long.
1394 * - Otherwise the result is int sized.
1396 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1397 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1398 if (rtype == CF_LONG || ltype == CF_LONG) {
1408 unsigned g_typecast (unsigned lhs, unsigned rhs)
1409 /* Cast the value in the primary register to the operand size that is flagged
1410 * by the lhs value. Return the result value.
1413 unsigned ltype, rtype;
1415 /* Get the type spec from the flags */
1416 ltype = lhs & CF_TYPE;
1417 rtype = rhs & CF_TYPE;
1419 /* Check if a conversion is needed */
1420 if ((rhs & CF_CONST) == 0) {
1421 if (ltype == CF_LONG && rtype != CF_LONG) {
1422 /* We must promote the primary register to long */
1424 } else if (ltype == CF_INT && rtype != CF_INT) {
1425 /* We must promote the primary register to int */
1430 /* Do not need any other action. If the left type is int, and the primary
1431 * register is long, it will be automagically truncated. If the right hand
1432 * side is const, it is not located in the primary register and handled by
1433 * the expression parser code.
1436 /* Result is const if the right hand side was const */
1437 lhs |= (rhs & CF_CONST);
1439 /* The resulting type is that of the left hand side (that's why you called
1447 void g_scale (unsigned flags, long val)
1448 /* Scale the value in the primary register by the given value. If val is positive,
1449 * scale up, is val is negative, scale down. This function is used to scale
1450 * the operands or results of pointer arithmetic by the size of the type, the
1451 * pointer points to.
1456 /* Value may not be zero */
1458 Internal ("Data type has no size");
1459 } else if (val > 0) {
1462 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1464 /* Factor is 2, 4, 8 and 16, use special function */
1465 switch (flags & CF_TYPE) {
1468 if (flags & CF_FORCECHAR) {
1470 AddCodeLine ("asl a");
1477 if (flags & CF_UNSIGNED) {
1478 AddCodeLine ("jsr shlax%d", p2);
1480 AddCodeLine ("jsr aslax%d", p2);
1485 if (flags & CF_UNSIGNED) {
1486 AddCodeLine ("jsr shleax%d", p2);
1488 AddCodeLine ("jsr asleax%d", p2);
1497 } else if (val != 1) {
1499 /* Use a multiplication instead */
1500 g_mul (flags | CF_CONST, val);
1508 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1510 /* Factor is 2, 4, 8 and 16 use special function */
1511 switch (flags & CF_TYPE) {
1514 if (flags & CF_FORCECHAR) {
1515 if (flags & CF_UNSIGNED) {
1517 AddCodeLine ("lsr a");
1520 } else if (p2 <= 2) {
1521 AddCodeLine ("cmp #$80");
1522 AddCodeLine ("ror a");
1529 if (flags & CF_UNSIGNED) {
1530 AddCodeLine ("jsr lsrax%d", p2);
1532 AddCodeLine ("jsr asrax%d", p2);
1537 if (flags & CF_UNSIGNED) {
1538 AddCodeLine ("jsr lsreax%d", p2);
1540 AddCodeLine ("jsr asreax%d", p2);
1549 } else if (val != 1) {
1551 /* Use a division instead */
1552 g_div (flags | CF_CONST, val);
1560 /*****************************************************************************/
1561 /* Adds and subs of variables fix a fixed address */
1562 /*****************************************************************************/
1566 void g_addlocal (unsigned flags, int offs)
1567 /* Add a local variable to ax */
1571 /* Correct the offset and check it */
1573 CheckLocalOffs (offs);
1575 switch (flags & CF_TYPE) {
1578 L = GetLocalLabel();
1579 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1580 AddCodeLine ("clc");
1581 AddCodeLine ("adc (sp),y");
1582 AddCodeLine ("bcc %s", LocalLabelName (L));
1583 AddCodeLine ("inx");
1588 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1589 AddCodeLine ("clc");
1590 AddCodeLine ("adc (sp),y");
1591 AddCodeLine ("pha");
1592 AddCodeLine ("txa");
1593 AddCodeLine ("iny");
1594 AddCodeLine ("adc (sp),y");
1595 AddCodeLine ("tax");
1596 AddCodeLine ("pla");
1600 /* Do it the old way */
1602 g_getlocal (flags, offs);
1614 void g_addstatic (unsigned flags, unsigned long label, long offs)
1615 /* Add a static variable to ax */
1619 /* Create the correct label name */
1620 const char* lbuf = GetLabelName (flags, label, offs);
1622 switch (flags & CF_TYPE) {
1625 L = GetLocalLabel();
1626 AddCodeLine ("clc");
1627 AddCodeLine ("adc %s", lbuf);
1628 AddCodeLine ("bcc %s", LocalLabelName (L));
1629 AddCodeLine ("inx");
1634 AddCodeLine ("clc");
1635 AddCodeLine ("adc %s", lbuf);
1636 AddCodeLine ("tay");
1637 AddCodeLine ("txa");
1638 AddCodeLine ("adc %s+1", lbuf);
1639 AddCodeLine ("tax");
1640 AddCodeLine ("tya");
1644 /* Do it the old way */
1646 g_getstatic (flags, label, offs);
1658 /*****************************************************************************/
1659 /* Special op= functions */
1660 /*****************************************************************************/
1664 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1666 /* Emit += for a static variable */
1668 /* Create the correct label name */
1669 const char* lbuf = GetLabelName (flags, label, offs);
1671 /* Check the size and determine operation */
1672 switch (flags & CF_TYPE) {
1675 if (flags & CF_FORCECHAR) {
1676 AddCodeLine ("ldx #$00");
1677 if (flags & CF_CONST) {
1679 AddCodeLine ("inc %s", lbuf);
1680 AddCodeLine ("lda %s", lbuf);
1682 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1683 AddCodeLine ("clc");
1684 AddCodeLine ("adc %s", lbuf);
1685 AddCodeLine ("sta %s", lbuf);
1688 AddCodeLine ("clc");
1689 AddCodeLine ("adc %s", lbuf);
1690 AddCodeLine ("sta %s", lbuf);
1692 if ((flags & CF_UNSIGNED) == 0) {
1693 unsigned L = GetLocalLabel();
1694 AddCodeLine ("bpl %s", LocalLabelName (L));
1695 AddCodeLine ("dex");
1703 if (flags & CF_CONST) {
1705 unsigned L = GetLocalLabel ();
1706 AddCodeLine ("inc %s", lbuf);
1707 AddCodeLine ("bne %s", LocalLabelName (L));
1708 AddCodeLine ("inc %s+1", lbuf);
1710 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1711 AddCodeLine ("ldx %s+1", lbuf);
1713 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1714 AddCodeLine ("clc");
1715 AddCodeLine ("adc %s", lbuf);
1716 AddCodeLine ("sta %s", lbuf);
1718 unsigned L = GetLocalLabel ();
1719 AddCodeLine ("bcc %s", LocalLabelName (L));
1720 AddCodeLine ("inc %s+1", lbuf);
1722 AddCodeLine ("ldx %s+1", lbuf);
1724 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1725 AddCodeLine ("adc %s+1", lbuf);
1726 AddCodeLine ("sta %s+1", lbuf);
1727 AddCodeLine ("tax");
1728 AddCodeLine ("lda %s", lbuf);
1732 AddCodeLine ("clc");
1733 AddCodeLine ("adc %s", lbuf);
1734 AddCodeLine ("sta %s", lbuf);
1735 AddCodeLine ("txa");
1736 AddCodeLine ("adc %s+1", lbuf);
1737 AddCodeLine ("sta %s+1", lbuf);
1738 AddCodeLine ("tax");
1739 AddCodeLine ("lda %s", lbuf);
1744 if (flags & CF_CONST) {
1746 AddCodeLine ("ldy #<(%s)", lbuf);
1747 AddCodeLine ("sty ptr1");
1748 AddCodeLine ("ldy #>(%s)", lbuf);
1750 AddCodeLine ("jsr laddeq1");
1752 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1753 AddCodeLine ("jsr laddeqa");
1756 g_getstatic (flags, label, offs);
1758 g_putstatic (flags, label, offs);
1761 AddCodeLine ("ldy #<(%s)", lbuf);
1762 AddCodeLine ("sty ptr1");
1763 AddCodeLine ("ldy #>(%s)", lbuf);
1764 AddCodeLine ("jsr laddeq");
1775 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1776 /* Emit += for a local variable */
1778 /* Calculate the true offset, check it, load it into Y */
1780 CheckLocalOffs (offs);
1782 /* Check the size and determine operation */
1783 switch (flags & CF_TYPE) {
1786 if (flags & CF_FORCECHAR) {
1788 AddCodeLine ("ldx #$00");
1789 if (flags & CF_CONST) {
1790 AddCodeLine ("clc");
1791 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1792 AddCodeLine ("adc (sp),y");
1793 AddCodeLine ("sta (sp),y");
1795 AddCodeLine ("clc");
1796 AddCodeLine ("adc (sp),y");
1797 AddCodeLine ("sta (sp),y");
1799 if ((flags & CF_UNSIGNED) == 0) {
1800 unsigned L = GetLocalLabel();
1801 AddCodeLine ("bpl %s", LocalLabelName (L));
1802 AddCodeLine ("dex");
1811 if (flags & CF_CONST) {
1812 if (IS_Get (&CodeSizeFactor) >= 400) {
1813 AddCodeLine ("clc");
1814 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1815 AddCodeLine ("adc (sp),y");
1816 AddCodeLine ("sta (sp),y");
1817 AddCodeLine ("iny");
1818 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1819 AddCodeLine ("adc (sp),y");
1820 AddCodeLine ("sta (sp),y");
1821 AddCodeLine ("tax");
1822 AddCodeLine ("dey");
1823 AddCodeLine ("lda (sp),y");
1825 g_getimmed (flags, val, 0);
1826 AddCodeLine ("jsr addeqysp");
1829 AddCodeLine ("jsr addeqysp");
1834 if (flags & CF_CONST) {
1835 g_getimmed (flags, val, 0);
1838 AddCodeLine ("jsr laddeqysp");
1848 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1849 /* Emit += for the location with address in ax */
1851 /* If the offset is too large for a byte register, add the high byte
1852 * of the offset to the primary. Beware: We need a special correction
1853 * if the offset in the low byte will overflow in the operation.
1855 offs = MakeByteOffs (flags, offs);
1857 /* Check the size and determine operation */
1858 switch (flags & CF_TYPE) {
1861 AddCodeLine ("sta ptr1");
1862 AddCodeLine ("stx ptr1+1");
1863 AddCodeLine ("ldy #$%02X", offs);
1864 AddCodeLine ("ldx #$00");
1865 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1866 AddCodeLine ("clc");
1867 AddCodeLine ("adc (ptr1),y");
1868 AddCodeLine ("sta (ptr1),y");
1872 if (IS_Get (&CodeSizeFactor) >= 200) {
1873 /* Lots of code, use only if size is not important */
1874 AddCodeLine ("sta ptr1");
1875 AddCodeLine ("stx ptr1+1");
1876 AddCodeLine ("ldy #$%02X", offs);
1877 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1878 AddCodeLine ("clc");
1879 AddCodeLine ("adc (ptr1),y");
1880 AddCodeLine ("sta (ptr1),y");
1881 AddCodeLine ("pha");
1882 AddCodeLine ("iny");
1883 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1884 AddCodeLine ("adc (ptr1),y");
1885 AddCodeLine ("sta (ptr1),y");
1886 AddCodeLine ("tax");
1887 AddCodeLine ("pla");
1893 AddCodeLine ("jsr pushax"); /* Push the address */
1894 push (CF_PTR); /* Correct the internal sp */
1895 g_getind (flags, offs); /* Fetch the value */
1896 g_inc (flags, val); /* Increment value in primary */
1897 g_putind (flags, offs); /* Store the value back */
1907 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1909 /* Emit -= for a static variable */
1911 /* Create the correct label name */
1912 const char* lbuf = GetLabelName (flags, label, offs);
1914 /* Check the size and determine operation */
1915 switch (flags & CF_TYPE) {
1918 if (flags & CF_FORCECHAR) {
1919 AddCodeLine ("ldx #$00");
1920 if (flags & CF_CONST) {
1922 AddCodeLine ("dec %s", lbuf);
1923 AddCodeLine ("lda %s", lbuf);
1925 AddCodeLine ("lda %s", lbuf);
1926 AddCodeLine ("sec");
1927 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1928 AddCodeLine ("sta %s", lbuf);
1931 AddCodeLine ("eor #$FF");
1932 AddCodeLine ("sec");
1933 AddCodeLine ("adc %s", lbuf);
1934 AddCodeLine ("sta %s", lbuf);
1936 if ((flags & CF_UNSIGNED) == 0) {
1937 unsigned L = GetLocalLabel();
1938 AddCodeLine ("bpl %s", LocalLabelName (L));
1939 AddCodeLine ("dex");
1947 if (flags & CF_CONST) {
1948 AddCodeLine ("lda %s", lbuf);
1949 AddCodeLine ("sec");
1950 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1951 AddCodeLine ("sta %s", lbuf);
1953 unsigned L = GetLocalLabel ();
1954 AddCodeLine ("bcs %s", LocalLabelName (L));
1955 AddCodeLine ("dec %s+1", lbuf);
1957 AddCodeLine ("ldx %s+1", lbuf);
1959 AddCodeLine ("lda %s+1", lbuf);
1960 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1961 AddCodeLine ("sta %s+1", lbuf);
1962 AddCodeLine ("tax");
1963 AddCodeLine ("lda %s", lbuf);
1966 AddCodeLine ("eor #$FF");
1967 AddCodeLine ("sec");
1968 AddCodeLine ("adc %s", lbuf);
1969 AddCodeLine ("sta %s", lbuf);
1970 AddCodeLine ("txa");
1971 AddCodeLine ("eor #$FF");
1972 AddCodeLine ("adc %s+1", lbuf);
1973 AddCodeLine ("sta %s+1", lbuf);
1974 AddCodeLine ("tax");
1975 AddCodeLine ("lda %s", lbuf);
1980 if (flags & CF_CONST) {
1982 AddCodeLine ("ldy #<(%s)", lbuf);
1983 AddCodeLine ("sty ptr1");
1984 AddCodeLine ("ldy #>(%s)", lbuf);
1985 AddCodeLine ("lda #$%02X", (unsigned char)val);
1986 AddCodeLine ("jsr lsubeqa");
1988 g_getstatic (flags, label, offs);
1990 g_putstatic (flags, label, offs);
1993 AddCodeLine ("ldy #<(%s)", lbuf);
1994 AddCodeLine ("sty ptr1");
1995 AddCodeLine ("ldy #>(%s)", lbuf);
1996 AddCodeLine ("jsr lsubeq");
2007 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
2008 /* Emit -= for a local variable */
2010 /* Calculate the true offset, check it, load it into Y */
2012 CheckLocalOffs (offs);
2014 /* Check the size and determine operation */
2015 switch (flags & CF_TYPE) {
2018 if (flags & CF_FORCECHAR) {
2020 AddCodeLine ("ldx #$00");
2021 if (flags & CF_CONST) {
2022 AddCodeLine ("lda (sp),y");
2023 AddCodeLine ("sec");
2024 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2026 AddCodeLine ("eor #$FF");
2027 AddCodeLine ("sec");
2028 AddCodeLine ("adc (sp),y");
2030 AddCodeLine ("sta (sp),y");
2031 if ((flags & CF_UNSIGNED) == 0) {
2032 unsigned L = GetLocalLabel();
2033 AddCodeLine ("bpl %s", LocalLabelName (L));
2034 AddCodeLine ("dex");
2042 if (flags & CF_CONST) {
2043 g_getimmed (flags, val, 0);
2046 AddCodeLine ("jsr subeqysp");
2050 if (flags & CF_CONST) {
2051 g_getimmed (flags, val, 0);
2054 AddCodeLine ("jsr lsubeqysp");
2064 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2065 /* Emit -= for the location with address in ax */
2067 /* If the offset is too large for a byte register, add the high byte
2068 * of the offset to the primary. Beware: We need a special correction
2069 * if the offset in the low byte will overflow in the operation.
2071 offs = MakeByteOffs (flags, offs);
2073 /* Check the size and determine operation */
2074 switch (flags & CF_TYPE) {
2077 AddCodeLine ("sta ptr1");
2078 AddCodeLine ("stx ptr1+1");
2079 AddCodeLine ("ldy #$%02X", offs);
2080 AddCodeLine ("ldx #$00");
2081 AddCodeLine ("lda (ptr1),y");
2082 AddCodeLine ("sec");
2083 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2084 AddCodeLine ("sta (ptr1),y");
2088 if (IS_Get (&CodeSizeFactor) >= 200) {
2089 /* Lots of code, use only if size is not important */
2090 AddCodeLine ("sta ptr1");
2091 AddCodeLine ("stx ptr1+1");
2092 AddCodeLine ("ldy #$%02X", offs);
2093 AddCodeLine ("lda (ptr1),y");
2094 AddCodeLine ("sec");
2095 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2096 AddCodeLine ("sta (ptr1),y");
2097 AddCodeLine ("pha");
2098 AddCodeLine ("iny");
2099 AddCodeLine ("lda (ptr1),y");
2100 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2101 AddCodeLine ("sta (ptr1),y");
2102 AddCodeLine ("tax");
2103 AddCodeLine ("pla");
2109 AddCodeLine ("jsr pushax"); /* Push the address */
2110 push (CF_PTR); /* Correct the internal sp */
2111 g_getind (flags, offs); /* Fetch the value */
2112 g_dec (flags, val); /* Increment value in primary */
2113 g_putind (flags, offs); /* Store the value back */
2123 /*****************************************************************************/
2124 /* Add a variable address to the value in ax */
2125 /*****************************************************************************/
2129 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2130 /* Add the address of a local variable to ax */
2134 /* Add the offset */
2137 /* We cannot address more then 256 bytes of locals anyway */
2138 L = GetLocalLabel();
2139 CheckLocalOffs (offs);
2140 AddCodeLine ("clc");
2141 AddCodeLine ("adc #$%02X", offs & 0xFF);
2142 /* Do also skip the CLC insn below */
2143 AddCodeLine ("bcc %s", LocalLabelName (L));
2144 AddCodeLine ("inx");
2147 /* Add the current stackpointer value */
2148 AddCodeLine ("clc");
2150 /* Label was used above */
2153 AddCodeLine ("adc sp");
2154 AddCodeLine ("tay");
2155 AddCodeLine ("txa");
2156 AddCodeLine ("adc sp+1");
2157 AddCodeLine ("tax");
2158 AddCodeLine ("tya");
2163 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2164 /* Add the address of a static variable to ax */
2166 /* Create the correct label name */
2167 const char* lbuf = GetLabelName (flags, label, offs);
2169 /* Add the address to the current ax value */
2170 AddCodeLine ("clc");
2171 AddCodeLine ("adc #<(%s)", lbuf);
2172 AddCodeLine ("tay");
2173 AddCodeLine ("txa");
2174 AddCodeLine ("adc #>(%s)", lbuf);
2175 AddCodeLine ("tax");
2176 AddCodeLine ("tya");
2181 /*****************************************************************************/
2183 /*****************************************************************************/
2187 void g_save (unsigned flags)
2188 /* Copy primary register to hold register. */
2190 /* Check the size and determine operation */
2191 switch (flags & CF_TYPE) {
2194 if (flags & CF_FORCECHAR) {
2195 AddCodeLine ("pha");
2201 AddCodeLine ("sta regsave");
2202 AddCodeLine ("stx regsave+1");
2206 AddCodeLine ("jsr saveeax");
2216 void g_restore (unsigned flags)
2217 /* Copy hold register to primary. */
2219 /* Check the size and determine operation */
2220 switch (flags & CF_TYPE) {
2223 if (flags & CF_FORCECHAR) {
2224 AddCodeLine ("pla");
2230 AddCodeLine ("lda regsave");
2231 AddCodeLine ("ldx regsave+1");
2235 AddCodeLine ("jsr resteax");
2245 void g_cmp (unsigned flags, unsigned long val)
2246 /* Immidiate compare. The primary register will not be changed, Z flag
2252 /* Check the size and determine operation */
2253 switch (flags & CF_TYPE) {
2256 if (flags & CF_FORCECHAR) {
2257 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2263 L = GetLocalLabel();
2264 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2265 AddCodeLine ("bne %s", LocalLabelName (L));
2266 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2271 Internal ("g_cmp: Long compares not implemented");
2281 static void oper (unsigned Flags, unsigned long Val, const char** Subs)
2282 /* Encode a binary operation. subs is a pointer to four strings:
2283 * 0 --> Operate on ints
2284 * 1 --> Operate on unsigneds
2285 * 2 --> Operate on longs
2286 * 3 --> Operate on unsigned longs
2289 /* Determine the offset into the array */
2290 if (Flags & CF_UNSIGNED) {
2293 if ((Flags & CF_TYPE) == CF_LONG) {
2297 /* Load the value if it is not already in the primary */
2298 if (Flags & CF_CONST) {
2300 g_getimmed (Flags, Val, 0);
2303 /* Output the operation */
2304 AddCodeLine ("jsr %s", *Subs);
2306 /* The operation will pop it's argument */
2312 void g_test (unsigned flags)
2313 /* Test the value in the primary and set the condition codes */
2315 switch (flags & CF_TYPE) {
2318 if (flags & CF_FORCECHAR) {
2319 AddCodeLine ("tax");
2325 AddCodeLine ("stx tmp1");
2326 AddCodeLine ("ora tmp1");
2330 if (flags & CF_UNSIGNED) {
2331 AddCodeLine ("jsr utsteax");
2333 AddCodeLine ("jsr tsteax");
2345 void g_push (unsigned flags, unsigned long val)
2346 /* Push the primary register or a constant value onto the stack */
2348 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2350 /* We have a constant 8 or 16 bit value */
2351 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2353 /* Handle as 8 bit value */
2355 AddCodeLine ("jsr pusha");
2359 /* Handle as 16 bit value */
2360 g_getimmed (flags, val, 0);
2361 AddCodeLine ("jsr pushax");
2366 /* Value is not 16 bit or not constant */
2367 if (flags & CF_CONST) {
2368 /* Constant 32 bit value, load into eax */
2369 g_getimmed (flags, val, 0);
2372 /* Push the primary register */
2373 switch (flags & CF_TYPE) {
2376 if (flags & CF_FORCECHAR) {
2377 /* Handle as char */
2378 AddCodeLine ("jsr pusha");
2383 AddCodeLine ("jsr pushax");
2387 AddCodeLine ("jsr pusheax");
2397 /* Adjust the stack offset */
2403 void g_swap (unsigned flags)
2404 /* Swap the primary register and the top of the stack. flags give the type
2405 * of *both* values (must have same size).
2408 switch (flags & CF_TYPE) {
2412 AddCodeLine ("jsr swapstk");
2416 AddCodeLine ("jsr swapestk");
2427 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2428 /* Call the specified subroutine name */
2430 if ((Flags & CF_FIXARGC) == 0) {
2431 /* Pass the argument count */
2434 AddCodeLine ("jsr _%s", Label);
2435 StackPtr += ArgSize; /* callee pops args */
2440 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2441 /* Call subroutine indirect */
2443 if ((Flags & CF_LOCAL) == 0) {
2444 /* Address is in a/x */
2445 if ((Flags & CF_FIXARGC) == 0) {
2446 /* Pass arg count */
2449 AddCodeLine ("jsr callax");
2451 /* The address is on stack, offset is on Val */
2453 CheckLocalOffs (Offs);
2454 AddCodeLine ("pha");
2455 AddCodeLine ("ldy #$%02X", Offs);
2456 AddCodeLine ("lda (sp),y");
2457 AddCodeLine ("sta jmpvec+1");
2458 AddCodeLine ("iny");
2459 AddCodeLine ("lda (sp),y");
2460 AddCodeLine ("sta jmpvec+2");
2461 AddCodeLine ("pla");
2462 AddCodeLine ("jsr jmpvec");
2465 /* Callee pops args */
2466 StackPtr += ArgSize;
2471 void g_jump (unsigned Label)
2472 /* Jump to specified internal label number */
2474 AddCodeLine ("jmp %s", LocalLabelName (Label));
2479 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2480 /* Jump to label if zero flag clear */
2482 AddCodeLine ("jne %s", LocalLabelName (label));
2487 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2488 /* Jump to label if zero flag set */
2490 AddCodeLine ("jeq %s", LocalLabelName (label));
2495 static void mod_internal (int k, char* verb1, char* verb2)
2498 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2502 AddCodeLine ("jsr %ssp", verb2);
2508 void g_space (int space)
2509 /* Create or drop space on the stack */
2512 mod_internal (-space, "inc", "addy");
2513 } else if (space > 0) {
2514 mod_internal (space, "dec", "suby");
2520 void g_cstackcheck (void)
2521 /* Check for a C stack overflow */
2523 AddCodeLine ("jsr cstkchk");
2528 void g_stackcheck (void)
2529 /* Check for a stack overflow */
2531 AddCodeLine ("jsr stkchk");
2536 void g_add (unsigned flags, unsigned long val)
2537 /* Primary = TOS + Primary */
2539 static const char* ops[12] = {
2540 "tosaddax", "tosaddax", "tosaddeax", "tosaddeax"
2543 if (flags & CF_CONST) {
2544 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2545 g_push (flags & ~CF_CONST, 0);
2547 oper (flags, val, ops);
2552 void g_sub (unsigned flags, unsigned long val)
2553 /* Primary = TOS - Primary */
2555 static const char* ops[12] = {
2556 "tossubax", "tossubax", "tossubeax", "tossubeax",
2559 if (flags & CF_CONST) {
2560 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2561 g_push (flags & ~CF_CONST, 0);
2563 oper (flags, val, ops);
2568 void g_rsub (unsigned flags, unsigned long val)
2569 /* Primary = Primary - TOS */
2571 static const char* ops[12] = {
2572 "tosrsubax", "tosrsubax", "tosrsubeax", "tosrsubeax",
2574 oper (flags, val, ops);
2579 void g_mul (unsigned flags, unsigned long val)
2580 /* Primary = TOS * Primary */
2582 static const char* ops[12] = {
2583 "tosmulax", "tosumulax", "tosmuleax", "tosumuleax",
2588 /* Do strength reduction if the value is constant and a power of two */
2589 if (flags & CF_CONST && (p2 = PowerOf2 (val)) >= 0) {
2590 /* Generate a shift instead */
2595 /* If the right hand side is const, the lhs is not on stack but still
2596 * in the primary register.
2598 if (flags & CF_CONST) {
2600 switch (flags & CF_TYPE) {
2603 if (flags & CF_FORCECHAR) {
2604 /* Handle some special cases */
2608 AddCodeLine ("sta tmp1");
2609 AddCodeLine ("asl a");
2610 AddCodeLine ("clc");
2611 AddCodeLine ("adc tmp1");
2615 AddCodeLine ("sta tmp1");
2616 AddCodeLine ("asl a");
2617 AddCodeLine ("asl a");
2618 AddCodeLine ("clc");
2619 AddCodeLine ("adc tmp1");
2623 AddCodeLine ("sta tmp1");
2624 AddCodeLine ("asl a");
2625 AddCodeLine ("clc");
2626 AddCodeLine ("adc tmp1");
2627 AddCodeLine ("asl a");
2631 AddCodeLine ("sta tmp1");
2632 AddCodeLine ("asl a");
2633 AddCodeLine ("asl a");
2634 AddCodeLine ("clc");
2635 AddCodeLine ("adc tmp1");
2636 AddCodeLine ("asl a");
2645 AddCodeLine ("jsr mulax3");
2648 AddCodeLine ("jsr mulax5");
2651 AddCodeLine ("jsr mulax6");
2654 AddCodeLine ("jsr mulax7");
2657 AddCodeLine ("jsr mulax9");
2660 AddCodeLine ("jsr mulax10");
2672 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2673 * into the normal, non-optimized stuff.
2675 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2676 g_push (flags & ~CF_CONST, 0);
2680 /* Use long way over the stack */
2681 oper (flags, val, ops);
2686 void g_div (unsigned flags, unsigned long val)
2687 /* Primary = TOS / Primary */
2689 static const char* ops[12] = {
2690 "tosdivax", "tosudivax", "tosdiveax", "tosudiveax",
2693 /* Do strength reduction if the value is constant and a power of two */
2695 if ((flags & CF_CONST) && (p2 = PowerOf2 (val)) >= 0) {
2696 /* Generate a shift instead */
2699 /* Generate a division */
2700 if (flags & CF_CONST) {
2701 /* lhs is not on stack */
2702 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2703 g_push (flags & ~CF_CONST, 0);
2705 oper (flags, val, ops);
2711 void g_mod (unsigned flags, unsigned long val)
2712 /* Primary = TOS % Primary */
2714 static const char* ops[12] = {
2715 "tosmodax", "tosumodax", "tosmodeax", "tosumodeax",
2719 /* Check if we can do some cost reduction */
2720 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = PowerOf2 (val)) >= 0) {
2721 /* We can do that with an AND operation */
2722 g_and (flags, val - 1);
2724 /* Do it the hard way... */
2725 if (flags & CF_CONST) {
2726 /* lhs is not on stack */
2727 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2728 g_push (flags & ~CF_CONST, 0);
2730 oper (flags, val, ops);
2736 void g_or (unsigned flags, unsigned long val)
2737 /* Primary = TOS | Primary */
2739 static const char* ops[12] = {
2740 "tosorax", "tosorax", "tosoreax", "tosoreax",
2743 /* If the right hand side is const, the lhs is not on stack but still
2744 * in the primary register.
2746 if (flags & CF_CONST) {
2748 switch (flags & CF_TYPE) {
2751 if (flags & CF_FORCECHAR) {
2752 if ((val & 0xFF) != 0) {
2753 AddCodeLine ("ora #$%02X", (unsigned char)val);
2761 if ((val & 0xFF) != 0) {
2762 AddCodeLine ("ora #$%02X", (unsigned char)val);
2764 } else if ((val & 0xFF00) == 0xFF00) {
2765 if ((val & 0xFF) != 0) {
2766 AddCodeLine ("ora #$%02X", (unsigned char)val);
2769 } else if (val != 0) {
2770 AddCodeLine ("ora #$%02X", (unsigned char)val);
2771 AddCodeLine ("pha");
2772 AddCodeLine ("txa");
2773 AddCodeLine ("ora #$%02X", (unsigned char)(val >> 8));
2774 AddCodeLine ("tax");
2775 AddCodeLine ("pla");
2781 if ((val & 0xFF) != 0) {
2782 AddCodeLine ("ora #$%02X", (unsigned char)val);
2792 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2793 * into the normal, non-optimized stuff. Note: The standard stuff will
2794 * always work with ints.
2796 flags &= ~CF_FORCECHAR;
2797 g_push (flags & ~CF_CONST, 0);
2800 /* Use long way over the stack */
2801 oper (flags, val, ops);
2806 void g_xor (unsigned flags, unsigned long val)
2807 /* Primary = TOS ^ Primary */
2809 static const char* ops[12] = {
2810 "tosxorax", "tosxorax", "tosxoreax", "tosxoreax",
2814 /* If the right hand side is const, the lhs is not on stack but still
2815 * in the primary register.
2817 if (flags & CF_CONST) {
2819 switch (flags & CF_TYPE) {
2822 if (flags & CF_FORCECHAR) {
2823 if ((val & 0xFF) != 0) {
2824 AddCodeLine ("eor #$%02X", (unsigned char)val);
2833 AddCodeLine ("eor #$%02X", (unsigned char)val);
2835 } else if (val != 0) {
2836 if ((val & 0xFF) != 0) {
2837 AddCodeLine ("eor #$%02X", (unsigned char)val);
2839 AddCodeLine ("pha");
2840 AddCodeLine ("txa");
2841 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2842 AddCodeLine ("tax");
2843 AddCodeLine ("pla");
2850 AddCodeLine ("eor #$%02X", (unsigned char)val);
2860 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2861 * into the normal, non-optimized stuff. Note: The standard stuff will
2862 * always work with ints.
2864 flags &= ~CF_FORCECHAR;
2865 g_push (flags & ~CF_CONST, 0);
2868 /* Use long way over the stack */
2869 oper (flags, val, ops);
2874 void g_and (unsigned Flags, unsigned long Val)
2875 /* Primary = TOS & Primary */
2877 static const char* ops[12] = {
2878 "tosandax", "tosandax", "tosandeax", "tosandeax",
2881 /* If the right hand side is const, the lhs is not on stack but still
2882 * in the primary register.
2884 if (Flags & CF_CONST) {
2886 switch (Flags & CF_TYPE) {
2889 if (Flags & CF_FORCECHAR) {
2890 if ((Val & 0xFF) == 0x00) {
2891 AddCodeLine ("lda #$00");
2892 } else if ((Val & 0xFF) != 0xFF) {
2893 AddCodeLine ("and #$%02X", (unsigned char)Val);
2899 if ((Val & 0xFFFF) != 0xFFFF) {
2903 AddCodeLine ("lda #$00");
2904 } else if (Val != 0xFF) {
2905 AddCodeLine ("and #$%02X", (unsigned char)Val);
2907 } else if ((Val & 0xFFFF) == 0xFF00) {
2908 AddCodeLine ("lda #$00");
2909 } else if ((Val & 0xFF00) == 0xFF00) {
2910 AddCodeLine ("and #$%02X", (unsigned char)Val);
2911 } else if ((Val & 0x00FF) == 0x0000) {
2912 AddCodeLine ("txa");
2913 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2914 AddCodeLine ("tax");
2915 AddCodeLine ("lda #$00");
2917 AddCodeLine ("tay");
2918 AddCodeLine ("txa");
2919 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2920 AddCodeLine ("tax");
2921 AddCodeLine ("tya");
2922 if ((Val & 0x00FF) == 0x0000) {
2923 AddCodeLine ("lda #$00");
2924 } else if ((Val & 0x00FF) != 0x00FF) {
2925 AddCodeLine ("and #$%02X", (unsigned char)Val);
2934 AddCodeLine ("stx sreg+1");
2935 AddCodeLine ("stx sreg");
2936 if ((Val & 0xFF) != 0xFF) {
2937 AddCodeLine ("and #$%02X", (unsigned char)Val);
2940 } else if (Val == 0xFF00) {
2942 AddCodeLine ("sta sreg+1");
2943 AddCodeLine ("sta sreg");
2952 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2953 * into the normal, non-optimized stuff. Note: The standard stuff will
2954 * always work with ints.
2956 Flags &= ~CF_FORCECHAR;
2957 g_push (Flags & ~CF_CONST, 0);
2960 /* Use long way over the stack */
2961 oper (Flags, Val, ops);
2966 void g_asr (unsigned flags, unsigned long val)
2967 /* Primary = TOS >> Primary */
2969 static const char* ops[12] = {
2970 "tosasrax", "tosshrax", "tosasreax", "tosshreax",
2973 /* If the right hand side is const, the lhs is not on stack but still
2974 * in the primary register.
2976 if (flags & CF_CONST) {
2978 switch (flags & CF_TYPE) {
2984 if (flags & CF_UNSIGNED) {
2985 AddCodeLine ("txa");
2988 unsigned L = GetLocalLabel();
2989 AddCodeLine ("cpx #$80"); /* Sign bit into carry */
2990 AddCodeLine ("txa");
2991 AddCodeLine ("ldx #$00");
2992 AddCodeLine ("bcc %s", LocalLabelName (L));
2993 AddCodeLine ("dex"); /* Make $FF */
2999 if (flags & CF_UNSIGNED) {
3000 AddCodeLine ("jsr shrax4");
3002 AddCodeLine ("jsr asrax4");
3007 if (flags & CF_UNSIGNED) {
3008 AddCodeLine ("jsr shrax%ld", val);
3010 AddCodeLine ("jsr asrax%ld", val);
3018 AddCodeLine ("ldx #$00");
3019 AddCodeLine ("lda sreg+1");
3020 if ((flags & CF_UNSIGNED) == 0) {
3021 unsigned L = GetLocalLabel();
3022 AddCodeLine ("bpl %s", LocalLabelName (L));
3023 AddCodeLine ("dex");
3026 AddCodeLine ("stx sreg");
3027 AddCodeLine ("stx sreg+1");
3031 AddCodeLine ("ldy #$00");
3032 AddCodeLine ("ldx sreg+1");
3033 if ((flags & CF_UNSIGNED) == 0) {
3034 unsigned L = GetLocalLabel();
3035 AddCodeLine ("bpl %s", LocalLabelName (L));
3036 AddCodeLine ("dey");
3039 AddCodeLine ("lda sreg");
3040 AddCodeLine ("sty sreg+1");
3041 AddCodeLine ("sty sreg");
3045 AddCodeLine ("txa");
3046 AddCodeLine ("ldx sreg");
3047 AddCodeLine ("ldy sreg+1");
3048 AddCodeLine ("sty sreg");
3049 if ((flags & CF_UNSIGNED) == 0) {
3050 unsigned L = GetLocalLabel();
3051 AddCodeLine ("cpy #$80");
3052 AddCodeLine ("ldy #$00");
3053 AddCodeLine ("bcc %s", LocalLabelName (L));
3054 AddCodeLine ("dey");
3057 AddCodeLine ("ldy #$00");
3059 AddCodeLine ("sty sreg+1");
3063 if (flags & CF_UNSIGNED) {
3064 AddCodeLine ("jsr shreax4");
3066 AddCodeLine ("jsr asreax4");
3071 if (flags & CF_UNSIGNED) {
3072 AddCodeLine ("jsr shreax%ld", val);
3074 AddCodeLine ("jsr asreax%ld", val);
3083 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3084 * into the normal, non-optimized stuff. Note: The standard stuff will
3085 * always work with ints.
3087 flags &= ~CF_FORCECHAR;
3088 g_push (flags & ~CF_CONST, 0);
3091 /* Use long way over the stack */
3092 oper (flags, val, ops);
3097 void g_asl (unsigned flags, unsigned long val)
3098 /* Primary = TOS << Primary */
3100 static const char* ops[12] = {
3101 "tosaslax", "tosshlax", "tosasleax", "tosshleax",
3105 /* If the right hand side is const, the lhs is not on stack but still
3106 * in the primary register.
3108 if (flags & CF_CONST) {
3110 switch (flags & CF_TYPE) {
3116 AddCodeLine ("tax");
3117 AddCodeLine ("lda #$00");
3121 if (flags & CF_UNSIGNED) {
3122 AddCodeLine ("jsr shlax4");
3124 AddCodeLine ("jsr aslax4");
3129 if (flags & CF_UNSIGNED) {
3130 AddCodeLine ("jsr shlax%ld", val);
3132 AddCodeLine ("jsr aslax%ld", val);
3140 AddCodeLine ("sta sreg+1");
3141 AddCodeLine ("lda #$00");
3142 AddCodeLine ("tax");
3143 AddCodeLine ("sta sreg");
3147 AddCodeLine ("stx sreg+1");
3148 AddCodeLine ("sta sreg");
3149 AddCodeLine ("lda #$00");
3150 AddCodeLine ("tax");
3154 AddCodeLine ("ldy sreg");
3155 AddCodeLine ("sty sreg+1");
3156 AddCodeLine ("stx sreg");
3157 AddCodeLine ("tax");
3158 AddCodeLine ("lda #$00");
3162 if (flags & CF_UNSIGNED) {
3163 AddCodeLine ("jsr shleax4");
3165 AddCodeLine ("jsr asleax4");
3170 if (flags & CF_UNSIGNED) {
3171 AddCodeLine ("jsr shleax%ld", val);
3173 AddCodeLine ("jsr asleax%ld", val);
3182 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3183 * into the normal, non-optimized stuff. Note: The standard stuff will
3184 * always work with ints.
3186 flags &= ~CF_FORCECHAR;
3187 g_push (flags & ~CF_CONST, 0);
3190 /* Use long way over the stack */
3191 oper (flags, val, ops);
3196 void g_neg (unsigned Flags)
3197 /* Primary = -Primary */
3199 switch (Flags & CF_TYPE) {
3202 if (Flags & CF_FORCECHAR) {
3203 AddCodeLine ("eor #$FF");
3204 AddCodeLine ("clc");
3205 AddCodeLine ("adc #$01");
3211 AddCodeLine ("jsr negax");
3215 AddCodeLine ("jsr negeax");
3225 void g_bneg (unsigned flags)
3226 /* Primary = !Primary */
3228 switch (flags & CF_TYPE) {
3231 AddCodeLine ("jsr bnega");
3235 AddCodeLine ("jsr bnegax");
3239 AddCodeLine ("jsr bnegeax");
3249 void g_com (unsigned Flags)
3250 /* Primary = ~Primary */
3252 switch (Flags & CF_TYPE) {
3255 if (Flags & CF_FORCECHAR) {
3256 AddCodeLine ("eor #$FF");
3262 AddCodeLine ("jsr complax");
3266 AddCodeLine ("jsr compleax");
3276 void g_inc (unsigned flags, unsigned long val)
3277 /* Increment the primary register by a given number */
3279 /* Don't inc by zero */
3284 /* Generate code for the supported types */
3286 switch (flags & CF_TYPE) {
3289 if (flags & CF_FORCECHAR) {
3290 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3292 AddCodeLine ("ina");
3295 AddCodeLine ("clc");
3296 AddCodeLine ("adc #$%02X", (unsigned char)val);
3303 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) {
3304 unsigned L = GetLocalLabel();
3305 AddCodeLine ("ina");
3306 AddCodeLine ("bne %s", LocalLabelName (L));
3307 AddCodeLine ("inx");
3309 } else if (IS_Get (&CodeSizeFactor) < 200) {
3312 AddCodeLine ("jsr incax%lu", val);
3313 } else if (val <= 255) {
3315 AddCodeLine ("jsr incaxy");
3317 g_add (flags | CF_CONST, val);
3320 /* Inline the code */
3322 if ((val & 0xFF) != 0) {
3323 unsigned L = GetLocalLabel();
3324 AddCodeLine ("clc");
3325 AddCodeLine ("adc #$%02X", (unsigned char) val);
3326 AddCodeLine ("bcc %s", LocalLabelName (L));
3327 AddCodeLine ("inx");
3331 AddCodeLine ("inx");
3334 AddCodeLine ("inx");
3337 AddCodeLine ("inx");
3339 } else if ((val & 0xFF) != 0) {
3340 AddCodeLine ("clc");
3341 AddCodeLine ("adc #$%02X", (unsigned char) val);
3342 AddCodeLine ("pha");
3343 AddCodeLine ("txa");
3344 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3345 AddCodeLine ("tax");
3346 AddCodeLine ("pla");
3348 AddCodeLine ("pha");
3349 AddCodeLine ("txa");
3350 AddCodeLine ("clc");
3351 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3352 AddCodeLine ("tax");
3353 AddCodeLine ("pla");
3361 AddCodeLine ("jsr inceaxy");
3363 g_add (flags | CF_CONST, val);
3375 void g_dec (unsigned flags, unsigned long val)
3376 /* Decrement the primary register by a given number */
3378 /* Don't dec by zero */
3383 /* Generate code for the supported types */
3385 switch (flags & CF_TYPE) {
3388 if (flags & CF_FORCECHAR) {
3389 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3391 AddCodeLine ("dea");
3394 AddCodeLine ("sec");
3395 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3402 if (IS_Get (&CodeSizeFactor) < 200) {
3403 /* Use subroutines */
3405 AddCodeLine ("jsr decax%d", (int) val);
3406 } else if (val <= 255) {
3408 AddCodeLine ("jsr decaxy");
3410 g_sub (flags | CF_CONST, val);
3413 /* Inline the code */
3415 if ((val & 0xFF) != 0) {
3416 unsigned L = GetLocalLabel();
3417 AddCodeLine ("sec");
3418 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3419 AddCodeLine ("bcs %s", LocalLabelName (L));
3420 AddCodeLine ("dex");
3424 AddCodeLine ("dex");
3427 AddCodeLine ("dex");
3430 if ((val & 0xFF) != 0) {
3431 AddCodeLine ("sec");
3432 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3433 AddCodeLine ("pha");
3434 AddCodeLine ("txa");
3435 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3436 AddCodeLine ("tax");
3437 AddCodeLine ("pla");
3439 AddCodeLine ("pha");
3440 AddCodeLine ("txa");
3441 AddCodeLine ("sec");
3442 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3443 AddCodeLine ("tax");
3444 AddCodeLine ("pla");
3453 AddCodeLine ("jsr deceaxy");
3455 g_sub (flags | CF_CONST, val);
3468 * Following are the conditional operators. They compare the TOS against
3469 * the primary and put a literal 1 in the primary if the condition is
3470 * true, otherwise they clear the primary register
3475 void g_eq (unsigned flags, unsigned long val)
3476 /* Test for equal */
3478 static const char* ops[12] = {
3479 "toseqax", "toseqax", "toseqeax", "toseqeax",
3484 /* If the right hand side is const, the lhs is not on stack but still
3485 * in the primary register.
3487 if (flags & CF_CONST) {
3489 switch (flags & CF_TYPE) {
3492 if (flags & CF_FORCECHAR) {
3493 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3494 AddCodeLine ("jsr booleq");
3500 L = GetLocalLabel();
3501 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3502 AddCodeLine ("bne %s", LocalLabelName (L));
3503 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3505 AddCodeLine ("jsr booleq");
3515 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3516 * into the normal, non-optimized stuff. Note: The standard stuff will
3517 * always work with ints.
3519 flags &= ~CF_FORCECHAR;
3520 g_push (flags & ~CF_CONST, 0);
3523 /* Use long way over the stack */
3524 oper (flags, val, ops);
3529 void g_ne (unsigned flags, unsigned long val)
3530 /* Test for not equal */
3532 static const char* ops[12] = {
3533 "tosneax", "tosneax", "tosneeax", "tosneeax",
3538 /* If the right hand side is const, the lhs is not on stack but still
3539 * in the primary register.
3541 if (flags & CF_CONST) {
3543 switch (flags & CF_TYPE) {
3546 if (flags & CF_FORCECHAR) {
3547 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3548 AddCodeLine ("jsr boolne");
3554 L = GetLocalLabel();
3555 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3556 AddCodeLine ("bne %s", LocalLabelName (L));
3557 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3559 AddCodeLine ("jsr boolne");
3569 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3570 * into the normal, non-optimized stuff. Note: The standard stuff will
3571 * always work with ints.
3573 flags &= ~CF_FORCECHAR;
3574 g_push (flags & ~CF_CONST, 0);
3577 /* Use long way over the stack */
3578 oper (flags, val, ops);
3583 void g_lt (unsigned flags, unsigned long val)
3584 /* Test for less than */
3586 static const char* ops[12] = {
3587 "tosltax", "tosultax", "toslteax", "tosulteax",
3592 /* If the right hand side is const, the lhs is not on stack but still
3593 * in the primary register.
3595 if (flags & CF_CONST) {
3597 /* Because the handling of the overflow flag is too complex for
3598 * inlining, we can handle only unsigned compares, and signed
3599 * compares against zero here.
3601 if (flags & CF_UNSIGNED) {
3603 /* Give a warning in some special cases */
3605 Warning ("Condition is never true");
3606 AddCodeLine ("jsr return0");
3610 /* Look at the type */
3611 switch (flags & CF_TYPE) {
3614 if (flags & CF_FORCECHAR) {
3615 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3616 AddCodeLine ("jsr boolult");
3622 /* If the low byte is zero, we must only test the high byte */
3623 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3624 if ((val & 0xFF) != 0) {
3625 unsigned L = GetLocalLabel();
3626 AddCodeLine ("bne %s", LocalLabelName (L));
3627 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3630 AddCodeLine ("jsr boolult");
3634 /* Do a subtraction */
3635 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3636 AddCodeLine ("txa");
3637 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3638 AddCodeLine ("lda sreg");
3639 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3640 AddCodeLine ("lda sreg+1");
3641 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3642 AddCodeLine ("jsr boolult");
3649 } else if (val == 0) {
3651 /* A signed compare against zero must only look at the sign bit */
3652 switch (flags & CF_TYPE) {
3655 if (flags & CF_FORCECHAR) {
3656 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3657 AddCodeLine ("lda #$00");
3658 AddCodeLine ("ldx #$00");
3659 AddCodeLine ("rol a");
3665 /* Just check the high byte */
3666 AddCodeLine ("cpx #$80"); /* Bit 7 -> carry */
3667 AddCodeLine ("lda #$00");
3668 AddCodeLine ("ldx #$00");
3669 AddCodeLine ("rol a");
3673 /* Just check the high byte */
3674 AddCodeLine ("lda sreg+1");
3675 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3676 AddCodeLine ("lda #$00");
3677 AddCodeLine ("ldx #$00");
3678 AddCodeLine ("rol a");
3687 /* Signed compare against a constant != zero */
3688 switch (flags & CF_TYPE) {
3691 if (flags & CF_FORCECHAR) {
3692 Label = GetLocalLabel ();
3693 AddCodeLine ("sec");
3694 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3695 AddCodeLine ("bvc %s", LocalLabelName (Label));
3696 AddCodeLine ("eor #$80");
3697 g_defcodelabel (Label);
3698 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3699 AddCodeLine ("lda #$00");
3700 AddCodeLine ("ldx #$00");
3701 AddCodeLine ("rol a");
3707 /* Do a subtraction */
3708 Label = GetLocalLabel ();
3709 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3710 AddCodeLine ("txa");
3711 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3712 AddCodeLine ("bvc %s", LocalLabelName (Label));
3713 AddCodeLine ("eor #$80");
3714 g_defcodelabel (Label);
3715 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3716 AddCodeLine ("lda #$00");
3717 AddCodeLine ("ldx #$00");
3718 AddCodeLine ("rol a");
3722 /* This one is too costly */
3731 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3732 * into the normal, non-optimized stuff. Note: The standard stuff will
3733 * always work with ints.
3735 flags &= ~CF_FORCECHAR;
3736 g_push (flags & ~CF_CONST, 0);
3739 /* Use long way over the stack */
3740 oper (flags, val, ops);
3745 void g_le (unsigned flags, unsigned long val)
3746 /* Test for less than or equal to */
3748 static const char* ops[12] = {
3749 "tosleax", "tosuleax", "tosleeax", "tosuleeax",
3753 /* If the right hand side is const, the lhs is not on stack but still
3754 * in the primary register.
3756 if (flags & CF_CONST) {
3758 /* Look at the type */
3759 switch (flags & CF_TYPE) {
3762 if (flags & CF_FORCECHAR) {
3763 if (flags & CF_UNSIGNED) {
3764 /* Unsigned compare */
3766 /* Use < instead of <= because the former gives
3767 * better code on the 6502 than the latter.
3769 g_lt (flags, val+1);
3772 Warning ("Condition is always true");
3773 AddCodeLine ("jsr return1");
3776 /* Signed compare */
3777 if ((long) val < 0x7F) {
3778 /* Use < instead of <= because the former gives
3779 * better code on the 6502 than the latter.
3781 g_lt (flags, val+1);
3784 Warning ("Condition is always true");
3785 AddCodeLine ("jsr return1");
3793 if (flags & CF_UNSIGNED) {
3794 /* Unsigned compare */
3796 /* Use < instead of <= because the former gives
3797 * better code on the 6502 than the latter.
3799 g_lt (flags, val+1);
3802 Warning ("Condition is always true");
3803 AddCodeLine ("jsr return1");
3806 /* Signed compare */
3807 if ((long) val < 0x7FFF) {
3808 g_lt (flags, val+1);
3811 Warning ("Condition is always true");
3812 AddCodeLine ("jsr return1");
3818 if (flags & CF_UNSIGNED) {
3819 /* Unsigned compare */
3820 if (val < 0xFFFFFFFF) {
3821 /* Use < instead of <= because the former gives
3822 * better code on the 6502 than the latter.
3824 g_lt (flags, val+1);
3827 Warning ("Condition is always true");
3828 AddCodeLine ("jsr return1");
3831 /* Signed compare */
3832 if ((long) val < 0x7FFFFFFF) {
3833 g_lt (flags, val+1);
3836 Warning ("Condition is always true");
3837 AddCodeLine ("jsr return1");
3846 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3847 * into the normal, non-optimized stuff. Note: The standard stuff will
3848 * always work with ints.
3850 flags &= ~CF_FORCECHAR;
3851 g_push (flags & ~CF_CONST, 0);
3854 /* Use long way over the stack */
3855 oper (flags, val, ops);
3860 void g_gt (unsigned flags, unsigned long val)
3861 /* Test for greater than */
3863 static const char* ops[12] = {
3864 "tosgtax", "tosugtax", "tosgteax", "tosugteax",
3868 /* If the right hand side is const, the lhs is not on stack but still
3869 * in the primary register.
3871 if (flags & CF_CONST) {
3873 /* Look at the type */
3874 switch (flags & CF_TYPE) {
3877 if (flags & CF_FORCECHAR) {
3878 if (flags & CF_UNSIGNED) {
3880 /* If we have a compare > 0, we will replace it by
3881 * != 0 here, since both are identical but the
3882 * latter is easier to optimize.
3885 } else if (val < 0xFF) {
3886 /* Use >= instead of > because the former gives
3887 * better code on the 6502 than the latter.
3889 g_ge (flags, val+1);
3892 Warning ("Condition is never true");
3893 AddCodeLine ("jsr return0");
3896 if ((long) val < 0x7F) {
3897 /* Use >= instead of > because the former gives
3898 * better code on the 6502 than the latter.
3900 g_ge (flags, val+1);
3903 Warning ("Condition is never true");
3904 AddCodeLine ("jsr return0");
3912 if (flags & CF_UNSIGNED) {
3913 /* Unsigned compare */
3915 /* If we have a compare > 0, we will replace it by
3916 * != 0 here, since both are identical but the latter
3917 * is easier to optimize.
3920 } else if (val < 0xFFFF) {
3921 /* Use >= instead of > because the former gives better
3922 * code on the 6502 than the latter.
3924 g_ge (flags, val+1);
3927 Warning ("Condition is never true");
3928 AddCodeLine ("jsr return0");
3931 /* Signed compare */
3932 if ((long) val < 0x7FFF) {
3933 g_ge (flags, val+1);
3936 Warning ("Condition is never true");
3937 AddCodeLine ("jsr return0");
3943 if (flags & CF_UNSIGNED) {
3944 /* Unsigned compare */
3946 /* If we have a compare > 0, we will replace it by
3947 * != 0 here, since both are identical but the latter
3948 * is easier to optimize.
3951 } else if (val < 0xFFFFFFFF) {
3952 /* Use >= instead of > because the former gives better
3953 * code on the 6502 than the latter.
3955 g_ge (flags, val+1);
3958 Warning ("Condition is never true");
3959 AddCodeLine ("jsr return0");
3962 /* Signed compare */
3963 if ((long) val < 0x7FFFFFFF) {
3964 g_ge (flags, val+1);
3967 Warning ("Condition is never true");
3968 AddCodeLine ("jsr return0");
3977 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3978 * into the normal, non-optimized stuff. Note: The standard stuff will
3979 * always work with ints.
3981 flags &= ~CF_FORCECHAR;
3982 g_push (flags & ~CF_CONST, 0);
3985 /* Use long way over the stack */
3986 oper (flags, val, ops);
3991 void g_ge (unsigned flags, unsigned long val)
3992 /* Test for greater than or equal to */
3994 static const char* ops[12] = {
3995 "tosgeax", "tosugeax", "tosgeeax", "tosugeeax",
4001 /* If the right hand side is const, the lhs is not on stack but still
4002 * in the primary register.
4004 if (flags & CF_CONST) {
4006 /* Because the handling of the overflow flag is too complex for
4007 * inlining, we can handle only unsigned compares, and signed
4008 * compares against zero here.
4010 if (flags & CF_UNSIGNED) {
4012 /* Give a warning in some special cases */
4014 Warning ("Condition is always true");
4015 AddCodeLine ("jsr return1");
4019 /* Look at the type */
4020 switch (flags & CF_TYPE) {
4023 if (flags & CF_FORCECHAR) {
4024 /* Do a subtraction. Condition is true if carry set */
4025 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4026 AddCodeLine ("lda #$00");
4027 AddCodeLine ("ldx #$00");
4028 AddCodeLine ("rol a");
4034 /* Do a subtraction. Condition is true if carry set */
4035 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4036 AddCodeLine ("txa");
4037 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4038 AddCodeLine ("lda #$00");
4039 AddCodeLine ("ldx #$00");
4040 AddCodeLine ("rol a");
4044 /* Do a subtraction. Condition is true if carry set */
4045 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4046 AddCodeLine ("txa");
4047 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4048 AddCodeLine ("lda sreg");
4049 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
4050 AddCodeLine ("lda sreg+1");
4051 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
4052 AddCodeLine ("lda #$00");
4053 AddCodeLine ("ldx #$00");
4054 AddCodeLine ("rol a");
4061 } else if (val == 0) {
4063 /* A signed compare against zero must only look at the sign bit */
4064 switch (flags & CF_TYPE) {
4067 if (flags & CF_FORCECHAR) {
4068 AddCodeLine ("tax");
4069 AddCodeLine ("jsr boolge");
4075 /* Just test the high byte */
4076 AddCodeLine ("txa");
4077 AddCodeLine ("jsr boolge");
4081 /* Just test the high byte */
4082 AddCodeLine ("lda sreg+1");
4083 AddCodeLine ("jsr boolge");
4092 /* Signed compare against a constant != zero */
4093 switch (flags & CF_TYPE) {
4096 if (flags & CF_FORCECHAR) {
4097 Label = GetLocalLabel ();
4098 AddCodeLine ("sec");
4099 AddCodeLine ("sbc #$%02X", (unsigned char)val);
4100 AddCodeLine ("bvs %s", LocalLabelName (Label));
4101 AddCodeLine ("eor #$80");
4102 g_defcodelabel (Label);
4103 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4104 AddCodeLine ("lda #$00");
4105 AddCodeLine ("ldx #$00");
4106 AddCodeLine ("rol a");
4112 /* Do a subtraction */
4113 Label = GetLocalLabel ();
4114 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4115 AddCodeLine ("txa");
4116 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4117 AddCodeLine ("bvs %s", LocalLabelName (Label));
4118 AddCodeLine ("eor #$80");
4119 g_defcodelabel (Label);
4120 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4121 AddCodeLine ("lda #$00");
4122 AddCodeLine ("ldx #$00");
4123 AddCodeLine ("rol a");
4127 /* This one is too costly */
4135 /* If we go here, we didn't emit code. Push the lhs on stack and fall
4136 * into the normal, non-optimized stuff. Note: The standard stuff will
4137 * always work with ints.
4139 flags &= ~CF_FORCECHAR;
4140 g_push (flags & ~CF_CONST, 0);
4143 /* Use long way over the stack */
4144 oper (flags, val, ops);
4149 /*****************************************************************************/
4150 /* Allocating static storage */
4151 /*****************************************************************************/
4155 void g_res (unsigned n)
4156 /* Reserve static storage, n bytes */
4158 AddDataLine ("\t.res\t%u,$00", n);
4163 void g_defdata (unsigned flags, unsigned long val, long offs)
4164 /* Define data with the size given in flags */
4166 if (flags & CF_CONST) {
4168 /* Numeric constant */
4169 switch (flags & CF_TYPE) {
4172 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
4176 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
4180 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
4191 /* Create the correct label name */
4192 const char* Label = GetLabelName (flags, val, offs);
4194 /* Labels are always 16 bit */
4195 AddDataLine ("\t.addr\t%s", Label);
4202 void g_defbytes (const void* Bytes, unsigned Count)
4203 /* Output a row of bytes as a constant */
4209 /* Cast the buffer pointer */
4210 const unsigned char* Data = (const unsigned char*) Bytes;
4212 /* Output the stuff */
4215 /* How many go into this line? */
4216 if ((Chunk = Count) > 16) {
4221 /* Output one line */
4222 strcpy (Buf, "\t.byte\t");
4225 B += sprintf (B, "$%02X", *Data++);
4231 /* Output the line */
4232 AddDataLine ("%s", Buf);
4238 void g_zerobytes (unsigned Count)
4239 /* Output Count bytes of data initialized with zero */
4242 AddDataLine ("\t.res\t%u,$00", Count);
4248 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4249 /* Initialize a register variable from static initialization data */
4251 /* Register variables do always have less than 128 bytes */
4252 unsigned CodeLabel = GetLocalLabel ();
4254 g_defcodelabel (CodeLabel);
4255 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4256 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4257 AddCodeLine ("dex");
4258 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4263 void g_initauto (unsigned Label, unsigned Size)
4264 /* Initialize a local variable at stack offset zero from static data */
4266 unsigned CodeLabel = GetLocalLabel ();
4268 CheckLocalOffs (Size);
4271 g_defcodelabel (CodeLabel);
4272 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4273 AddCodeLine ("sta (sp),y");
4274 AddCodeLine ("dey");
4275 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4276 } else if (Size <= 256) {
4278 g_defcodelabel (CodeLabel);
4279 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4280 AddCodeLine ("sta (sp),y");
4281 AddCodeLine ("iny");
4282 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4283 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4289 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4290 /* Initialize a static local variable from static initialization data */
4293 unsigned CodeLabel = GetLocalLabel ();
4295 g_defcodelabel (CodeLabel);
4296 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4297 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4298 AddCodeLine ("dey");
4299 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4300 } else if (Size <= 256) {
4301 unsigned CodeLabel = GetLocalLabel ();
4303 g_defcodelabel (CodeLabel);
4304 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4305 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4306 AddCodeLine ("iny");
4307 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4308 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4310 /* Use the easy way here: memcpy */
4311 g_getimmed (CF_STATIC, VarLabel, 0);
4312 AddCodeLine ("jsr pushax");
4313 g_getimmed (CF_STATIC, InitLabel, 0);
4314 AddCodeLine ("jsr pushax");
4315 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4316 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4322 /*****************************************************************************/
4323 /* Switch statement */
4324 /*****************************************************************************/
4328 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4329 /* Generate code for a switch statement */
4331 unsigned NextLabel = 0;
4334 /* Setup registers and determine which compare insn to use */
4335 const char* Compare;
4338 Compare = "cmp #$%02X";
4341 Compare = "cpx #$%02X";
4344 AddCodeLine ("ldy sreg");
4345 Compare = "cpy #$%02X";
4348 AddCodeLine ("ldy sreg+1");
4349 Compare = "cpy #$%02X";
4352 Internal ("Invalid depth in g_switch: %u", Depth);
4355 /* Walk over all nodes */
4356 for (I = 0; I < CollCount (Nodes); ++I) {
4358 /* Get the next case node */
4359 CaseNode* N = CollAtUnchecked (Nodes, I);
4361 /* If we have a next label, define it */
4363 g_defcodelabel (NextLabel);
4367 /* Do the compare */
4368 AddCodeLine (Compare, CN_GetValue (N));
4370 /* If this is the last level, jump directly to the case code if found */
4373 /* Branch if equal */
4374 g_falsejump (0, CN_GetLabel (N));
4378 /* Determine the next label */
4379 if (I == CollCount (Nodes) - 1) {
4380 /* Last node means not found */
4381 g_truejump (0, DefaultLabel);
4383 /* Jump to the next check */
4384 NextLabel = GetLocalLabel ();
4385 g_truejump (0, NextLabel);
4388 /* Check the next level */
4389 g_switch (N->Nodes, DefaultLabel, Depth-1);
4394 /* If we go here, we haven't found the label */
4395 g_jump (DefaultLabel);
4400 /*****************************************************************************/
4401 /* User supplied assembler code */
4402 /*****************************************************************************/
4406 void g_asmcode (struct StrBuf* B)
4407 /* Output one line of assembler code. */
4409 AddCodeLine ("%.*s", (int) SB_GetLen (B), SB_GetConstBuf (B));