1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2009 Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
64 /*****************************************************************************/
66 /*****************************************************************************/
70 static void typeerror (unsigned type)
71 /* Print an error message about an invalid operand type */
73 /* Special handling for floats here: */
74 if ((type & CF_TYPE) == CF_FLOAT) {
75 Fatal ("Floating point type is currently unsupported");
77 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
83 static void CheckLocalOffs (unsigned Offs)
84 /* Check the offset into the stack for 8bit range */
87 /* Too many local vars */
88 Error ("Too many local variables");
94 static const char* GetLabelName (unsigned Flags, unsigned long Label, long Offs)
96 static char Buf [256]; /* Label name */
98 /* Create the correct label name */
99 switch (Flags & CF_ADDRMASK) {
102 /* Static memory cell */
104 xsprintf (Buf, sizeof (Buf), "%s%+ld", LocalLabelName (Label), Offs);
106 xsprintf (Buf, sizeof (Buf), "%s", LocalLabelName (Label));
113 xsprintf (Buf, sizeof (Buf), "_%s%+ld", (char*) Label, Offs);
115 xsprintf (Buf, sizeof (Buf), "_%s", (char*) Label);
120 /* Absolute address */
121 xsprintf (Buf, sizeof (Buf), "$%04X", (int)((Label+Offs) & 0xFFFF));
125 /* Variable in register bank */
126 xsprintf (Buf, sizeof (Buf), "regbank+%u", (unsigned)((Label+Offs) & 0xFFFF));
130 Internal ("Invalid address flags: %04X", Flags);
133 /* Return a pointer to the static buffer */
139 /*****************************************************************************/
140 /* Pre- and postamble */
141 /*****************************************************************************/
145 void g_preamble (void)
146 /* Generate the assembler code preamble */
148 /* Create a new (global) segment list and remember it */
152 /* Identify the compiler version */
154 AddTextLine ("; File generated by cc65 v %u.%u.%u",
155 VER_MAJOR, VER_MINOR, VER_PATCH);
158 /* Insert some object file options */
159 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
160 VER_MAJOR, VER_MINOR, VER_PATCH);
162 /* If we're producing code for some other CPU, switch the command set */
164 case CPU_6502: AddTextLine ("\t.setcpu\t\t\"6502\""); break;
165 case CPU_6502X: AddTextLine ("\t.setcpu\t\t\"6502X\""); break;
166 case CPU_65SC02: AddTextLine ("\t.setcpu\t\t\"65SC02\""); break;
167 case CPU_65C02: AddTextLine ("\t.setcpu\t\t\"65C02\""); break;
168 case CPU_65816: AddTextLine ("\t.setcpu\t\t\"65816\""); break;
169 case CPU_HUC6280: AddTextLine ("\t.setcpu\t\t\"HUC6280\""); break;
170 default: Internal ("Unknown CPU: %d", CPU);
174 AddTextLine ("\t.smart\t\ton");
176 /* Allow auto import for runtime library routines */
177 AddTextLine ("\t.autoimport\ton");
179 /* Switch the assembler into case sensitive mode */
180 AddTextLine ("\t.case\t\ton");
182 /* Tell the assembler if we want to generate debug info */
183 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
185 /* Import the stack pointer for direct auto variable access */
186 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
188 /* Define long branch macros */
189 AddTextLine ("\t.macpack\tlongbranch");
194 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
195 /* If debug info is enabled, place a file info into the source */
198 /* We have to place this into the global text segment, so it will
199 * appear before all .dbg line statements.
201 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
207 /*****************************************************************************/
208 /* Segment support */
209 /*****************************************************************************/
213 void g_userodata (void)
214 /* Switch to the read only data segment */
216 UseDataSeg (SEG_RODATA);
221 void g_usedata (void)
222 /* Switch to the data segment */
224 UseDataSeg (SEG_DATA);
230 /* Switch to the bss segment */
232 UseDataSeg (SEG_BSS);
237 void g_segname (segment_t Seg)
238 /* Emit the name of a segment if necessary */
240 /* Emit a segment directive for the data style segments */
243 case SEG_RODATA: S = CS->ROData; break;
244 case SEG_DATA: S = CS->Data; break;
245 case SEG_BSS: S = CS->BSS; break;
246 default: S = 0; break;
249 DS_AddLine (S, ".segment\t\"%s\"", GetSegName (Seg));
255 /*****************************************************************************/
257 /*****************************************************************************/
261 unsigned sizeofarg (unsigned flags)
262 /* Return the size of a function argument type that is encoded in flags */
264 switch (flags & CF_TYPE) {
267 return (flags & CF_FORCECHAR)? 1 : 2;
287 int pop (unsigned flags)
288 /* Pop an argument of the given size */
290 return StackPtr += sizeofarg (flags);
295 int push (unsigned flags)
296 /* Push an argument of the given size */
298 return StackPtr -= sizeofarg (flags);
303 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
304 /* The value in Offs is an offset to an address in a/x. Make sure, an object
305 * of the type given in Flags can be loaded or stored into this address by
306 * adding part of the offset to the address in ax, so that the remaining
307 * offset fits into an index register. Return the remaining offset.
310 /* If the offset is too large for a byte register, add the high byte
311 * of the offset to the primary. Beware: We need a special correction
312 * if the offset in the low byte will overflow in the operation.
314 unsigned O = Offs & ~0xFFU;
315 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
316 /* We need to add the low byte also */
320 /* Do the correction if we need one */
322 g_inc (CF_INT | CF_CONST, O);
326 /* Return the new offset */
332 /*****************************************************************************/
333 /* Functions handling local labels */
334 /*****************************************************************************/
338 void g_defcodelabel (unsigned label)
339 /* Define a local code label */
341 CS_AddLabel (CS->Code, LocalLabelName (label));
346 void g_defdatalabel (unsigned label)
347 /* Define a local data label */
349 AddDataLine ("%s:", LocalLabelName (label));
354 /*****************************************************************************/
355 /* Functions handling global labels */
356 /*****************************************************************************/
360 void g_defgloblabel (const char* Name)
361 /* Define a global label with the given name */
363 /* Global labels are always data labels */
364 AddDataLine ("_%s:", Name);
369 void g_defexport (const char* Name, int ZP)
370 /* Export the given label */
373 AddTextLine ("\t.exportzp\t_%s", Name);
375 AddTextLine ("\t.export\t\t_%s", Name);
381 void g_defimport (const char* Name, int ZP)
382 /* Import the given label */
385 AddTextLine ("\t.importzp\t_%s", Name);
387 AddTextLine ("\t.import\t\t_%s", Name);
393 void g_importstartup (void)
394 /* Forced import of the startup module */
396 AddTextLine ("\t.forceimport\t__STARTUP__");
401 void g_importmainargs (void)
402 /* Forced import of a special symbol that handles arguments to main */
404 AddTextLine ("\t.forceimport\tinitmainargs");
409 /*****************************************************************************/
410 /* Load functions for various registers */
411 /*****************************************************************************/
415 static void ldaconst (unsigned val)
416 /* Load a with a constant */
418 AddCodeLine ("lda #$%02X", val & 0xFF);
423 static void ldxconst (unsigned val)
424 /* Load x with a constant */
426 AddCodeLine ("ldx #$%02X", val & 0xFF);
431 static void ldyconst (unsigned val)
432 /* Load y with a constant */
434 AddCodeLine ("ldy #$%02X", val & 0xFF);
439 /*****************************************************************************/
440 /* Function entry and exit */
441 /*****************************************************************************/
445 /* Remember the argument size of a function. The variable is set by g_enter
446 * and used by g_leave. If the functions gets its argument size by the caller
447 * (variable param list or function without prototype), g_enter will set the
453 void g_enter (unsigned flags, unsigned argsize)
454 /* Function prologue */
456 if ((flags & CF_FIXARGC) != 0) {
457 /* Just remember the argument size for the leave */
461 AddCodeLine ("jsr enter");
468 /* Function epilogue */
470 /* How many bytes of locals do we have to drop? */
471 unsigned k = (unsigned) -StackPtr;
473 /* If we didn't have a variable argument list, don't call leave */
476 /* Drop stackframe if needed. We can only drop 255 bytes at a time. */
479 unsigned ToDrop = (k > 255)? 255 : k;
481 AddCodeLine ("jsr incsp%d", k);
484 AddCodeLine ("jsr addysp");
492 /* Nothing to drop */
493 AddCodeLine ("jsr leave");
495 /* We've a stack frame to drop */
498 AddCodeLine ("jsr addysp");
502 AddCodeLine ("jsr leavey");
506 /* Add the final rts */
512 /*****************************************************************************/
513 /* Register variables */
514 /*****************************************************************************/
518 void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
519 /* Swap a register variable with a location on the stack */
521 /* Calculate the actual stack offset and check it */
522 StackOffs -= StackPtr;
523 CheckLocalOffs (StackOffs);
528 if (IS_Get (&CodeSizeFactor) < 165) {
529 ldyconst (StackOffs);
531 AddCodeLine ("jsr regswap1");
533 ldyconst (StackOffs);
534 AddCodeLine ("lda (sp),y");
535 AddCodeLine ("ldx regbank%+d", RegOffs);
536 AddCodeLine ("sta regbank%+d", RegOffs);
538 AddCodeLine ("sta (sp),y");
541 } else if (Bytes == 2) {
543 ldyconst (StackOffs);
545 AddCodeLine ("jsr regswap2");
549 ldyconst (StackOffs);
552 AddCodeLine ("jsr regswap");
558 void g_save_regvars (int RegOffs, unsigned Bytes)
559 /* Save register variables */
561 /* Don't loop for up to two bytes */
564 AddCodeLine ("lda regbank%+d", RegOffs);
565 AddCodeLine ("jsr pusha");
567 } else if (Bytes == 2) {
569 AddCodeLine ("lda regbank%+d", RegOffs);
570 AddCodeLine ("ldx regbank%+d", RegOffs+1);
571 AddCodeLine ("jsr pushax");
575 /* More than two bytes - loop */
576 unsigned Label = GetLocalLabel ();
578 ldyconst (Bytes - 1);
580 g_defcodelabel (Label);
581 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
582 AddCodeLine ("sta (sp),y");
585 AddCodeLine ("bne %s", LocalLabelName (Label));
589 /* We pushed stuff, correct the stack pointer */
595 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
596 /* Restore register variables */
598 /* Calculate the actual stack offset and check it */
599 StackOffs -= StackPtr;
600 CheckLocalOffs (StackOffs);
602 /* Don't loop for up to two bytes */
605 ldyconst (StackOffs);
606 AddCodeLine ("lda (sp),y");
607 AddCodeLine ("sta regbank%+d", RegOffs);
609 } else if (Bytes == 2) {
611 ldyconst (StackOffs);
612 AddCodeLine ("lda (sp),y");
613 AddCodeLine ("sta regbank%+d", RegOffs);
615 AddCodeLine ("lda (sp),y");
616 AddCodeLine ("sta regbank%+d", RegOffs+1);
618 } else if (Bytes == 3 && IS_Get (&CodeSizeFactor) >= 133) {
620 ldyconst (StackOffs);
621 AddCodeLine ("lda (sp),y");
622 AddCodeLine ("sta regbank%+d", RegOffs);
624 AddCodeLine ("lda (sp),y");
625 AddCodeLine ("sta regbank%+d", RegOffs+1);
627 AddCodeLine ("lda (sp),y");
628 AddCodeLine ("sta regbank%+d", RegOffs+2);
630 } else if (StackOffs <= RegOffs) {
632 /* More bytes, but the relation between the register offset in the
633 * register bank and the stack offset allows us to generate short
634 * code that uses just one index register.
636 unsigned Label = GetLocalLabel ();
637 ldyconst (StackOffs);
638 g_defcodelabel (Label);
639 AddCodeLine ("lda (sp),y");
640 AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs);
642 AddCodeLine ("cpy #$%02X", StackOffs + Bytes);
643 AddCodeLine ("bne %s", LocalLabelName (Label));
647 /* Ok, this is the generic code. We need to save X because the
648 * caller will only save A.
650 unsigned Label = GetLocalLabel ();
651 AddCodeLine ("stx tmp1");
652 ldyconst (StackOffs + Bytes - 1);
653 ldxconst (Bytes - 1);
654 g_defcodelabel (Label);
655 AddCodeLine ("lda (sp),y");
656 AddCodeLine ("sta regbank%+d,x", RegOffs);
659 AddCodeLine ("bpl %s", LocalLabelName (Label));
660 AddCodeLine ("ldx tmp1");
667 /*****************************************************************************/
668 /* Fetching memory cells */
669 /*****************************************************************************/
673 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
674 /* Load a constant into the primary register */
676 unsigned char B1, B2, B3, B4;
680 if ((Flags & CF_CONST) != 0) {
682 /* Numeric constant */
683 switch (Flags & CF_TYPE) {
686 if ((Flags & CF_FORCECHAR) != 0) {
692 ldxconst ((Val >> 8) & 0xFF);
693 ldaconst (Val & 0xFF);
697 /* Split the value into 4 bytes */
698 B1 = (unsigned char) (Val >> 0);
699 B2 = (unsigned char) (Val >> 8);
700 B3 = (unsigned char) (Val >> 16);
701 B4 = (unsigned char) (Val >> 24);
703 /* Remember which bytes are done */
707 AddCodeLine ("ldx #$%02X", B2);
710 AddCodeLine ("stx sreg");
714 AddCodeLine ("stx sreg+1");
717 if ((Done & 0x04) == 0 && B1 != B3) {
718 AddCodeLine ("lda #$%02X", B3);
719 AddCodeLine ("sta sreg");
722 if ((Done & 0x08) == 0 && B1 != B4) {
723 AddCodeLine ("lda #$%02X", B4);
724 AddCodeLine ("sta sreg+1");
727 AddCodeLine ("lda #$%02X", B1);
729 if ((Done & 0x04) == 0) {
731 AddCodeLine ("sta sreg");
733 if ((Done & 0x08) == 0) {
735 AddCodeLine ("sta sreg+1");
747 /* Some sort of label */
748 const char* Label = GetLabelName (Flags, Val, Offs);
750 /* Load the address into the primary */
751 AddCodeLine ("lda #<(%s)", Label);
752 AddCodeLine ("ldx #>(%s)", Label);
759 void g_getstatic (unsigned flags, unsigned long label, long offs)
760 /* Fetch an static memory cell into the primary register */
762 /* Create the correct label name */
763 const char* lbuf = GetLabelName (flags, label, offs);
765 /* Check the size and generate the correct load operation */
766 switch (flags & CF_TYPE) {
769 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
770 AddCodeLine ("lda %s", lbuf); /* load A from the label */
773 AddCodeLine ("lda %s", lbuf); /* load A from the label */
774 if (!(flags & CF_UNSIGNED)) {
775 /* Must sign extend */
776 unsigned L = GetLocalLabel ();
777 AddCodeLine ("bpl %s", LocalLabelName (L));
785 AddCodeLine ("lda %s", lbuf);
786 if (flags & CF_TEST) {
787 AddCodeLine ("ora %s+1", lbuf);
789 AddCodeLine ("ldx %s+1", lbuf);
794 if (flags & CF_TEST) {
795 AddCodeLine ("lda %s+3", lbuf);
796 AddCodeLine ("ora %s+2", lbuf);
797 AddCodeLine ("ora %s+1", lbuf);
798 AddCodeLine ("ora %s+0", lbuf);
800 AddCodeLine ("lda %s+3", lbuf);
801 AddCodeLine ("sta sreg+1");
802 AddCodeLine ("lda %s+2", lbuf);
803 AddCodeLine ("sta sreg");
804 AddCodeLine ("ldx %s+1", lbuf);
805 AddCodeLine ("lda %s", lbuf);
817 void g_getlocal (unsigned Flags, int Offs)
818 /* Fetch specified local object (local var). */
821 switch (Flags & CF_TYPE) {
824 CheckLocalOffs (Offs);
825 if ((Flags & CF_FORCECHAR) || (Flags & CF_TEST)) {
827 AddCodeLine ("lda (sp),y");
830 AddCodeLine ("ldx #$00");
831 AddCodeLine ("lda (sp),y");
832 if ((Flags & CF_UNSIGNED) == 0) {
833 unsigned L = GetLocalLabel();
834 AddCodeLine ("bpl %s", LocalLabelName (L));
842 CheckLocalOffs (Offs + 1);
843 AddCodeLine ("ldy #$%02X", (unsigned char) (Offs+1));
844 if (Flags & CF_TEST) {
845 AddCodeLine ("lda (sp),y");
847 AddCodeLine ("ora (sp),y");
848 } else if (IS_Get (&CodeSizeFactor) < 165) {
849 AddCodeLine ("jsr ldaxysp");
851 AddCodeLine ("lda (sp),y");
854 AddCodeLine ("lda (sp),y");
859 CheckLocalOffs (Offs + 3);
860 AddCodeLine ("ldy #$%02X", (unsigned char) (Offs+3));
861 AddCodeLine ("jsr ldeaxysp");
862 if (Flags & CF_TEST) {
874 void g_getind (unsigned Flags, unsigned Offs)
875 /* Fetch the specified object type indirect through the primary register
876 * into the primary register
879 /* If the offset is greater than 255, add the part that is > 255 to
880 * the primary. This way we get an easy addition and use the low byte
883 Offs = MakeByteOffs (Flags, Offs);
885 /* Handle the indirect fetch */
886 switch (Flags & CF_TYPE) {
889 /* Character sized */
890 if (Flags & CF_UNSIGNED) {
892 AddCodeLine ("jsr ldauidx");
895 AddCodeLine ("jsr ldaidx");
900 if (Flags & CF_TEST) {
902 AddCodeLine ("sta ptr1");
903 AddCodeLine ("stx ptr1+1");
904 AddCodeLine ("lda (ptr1),y");
906 AddCodeLine ("ora (ptr1),y");
909 AddCodeLine ("jsr ldaxidx");
915 AddCodeLine ("jsr ldeaxidx");
916 if (Flags & CF_TEST) {
929 void g_leasp (int Offs)
930 /* Fetch the address of the specified symbol into the primary register */
932 unsigned char Lo, Hi;
934 /* Calculate the offset relative to sp */
937 /* Get low and high byte */
938 Lo = (unsigned char) Offs;
939 Hi = (unsigned char) (Offs >> 8);
944 AddCodeLine ("lda sp");
945 AddCodeLine ("ldx sp+1");
950 AddCodeLine ("lda sp+1");
952 AddCodeLine ("adc #$%02X", Hi);
954 AddCodeLine ("lda sp");
956 } else if (Hi == 0) {
958 if (IS_Get (&CodeSizeFactor) < 200) {
959 /* 8 bit offset with subroutine call */
960 AddCodeLine ("lda #$%02X", Lo);
961 AddCodeLine ("jsr leaa0sp");
963 /* 8 bit offset inlined */
964 unsigned L = GetLocalLabel ();
965 AddCodeLine ("lda sp");
966 AddCodeLine ("ldx sp+1");
968 AddCodeLine ("adc #$%02X", Lo);
969 AddCodeLine ("bcc %s", LocalLabelName (L));
973 } else if (IS_Get (&CodeSizeFactor) < 170) {
974 /* Full 16 bit offset with subroutine call */
975 AddCodeLine ("lda #$%02X", Lo);
976 AddCodeLine ("ldx #$%02X", Hi);
977 AddCodeLine ("jsr leaaxsp");
979 /* Full 16 bit offset inlined */
980 AddCodeLine ("lda sp");
982 AddCodeLine ("adc #$%02X", Lo);
984 AddCodeLine ("lda sp+1");
985 AddCodeLine ("adc #$%02X", Hi);
993 void g_leavariadic (int Offs)
994 /* Fetch the address of a parameter in a variadic function into the primary
998 unsigned ArgSizeOffs;
1000 /* Calculate the offset relative to sp */
1003 /* Get the offset of the parameter which is stored at sp+0 on function
1004 * entry and check if this offset is reachable with a byte offset.
1006 CHECK (StackPtr <= 0);
1007 ArgSizeOffs = -StackPtr;
1008 CheckLocalOffs (ArgSizeOffs);
1010 /* Get the size of all parameters. */
1011 ldyconst (ArgSizeOffs);
1012 AddCodeLine ("lda (sp),y");
1014 /* Add the value of the stackpointer */
1015 if (IS_Get (&CodeSizeFactor) > 250) {
1016 unsigned L = GetLocalLabel();
1017 AddCodeLine ("ldx sp+1");
1018 AddCodeLine ("clc");
1019 AddCodeLine ("adc sp");
1020 AddCodeLine ("bcc %s", LocalLabelName (L));
1021 AddCodeLine ("inx");
1024 AddCodeLine ("ldx #$00");
1025 AddCodeLine ("jsr leaaxsp");
1028 /* Add the offset to the primary */
1030 g_inc (CF_INT | CF_CONST, Offs);
1031 } else if (Offs < 0) {
1032 g_dec (CF_INT | CF_CONST, -Offs);
1038 /*****************************************************************************/
1039 /* Store into memory */
1040 /*****************************************************************************/
1044 void g_putstatic (unsigned flags, unsigned long label, long offs)
1045 /* Store the primary register into the specified static memory cell */
1047 /* Create the correct label name */
1048 const char* lbuf = GetLabelName (flags, label, offs);
1050 /* Check the size and generate the correct store operation */
1051 switch (flags & CF_TYPE) {
1054 AddCodeLine ("sta %s", lbuf);
1058 AddCodeLine ("sta %s", lbuf);
1059 AddCodeLine ("stx %s+1", lbuf);
1063 AddCodeLine ("sta %s", lbuf);
1064 AddCodeLine ("stx %s+1", lbuf);
1065 AddCodeLine ("ldy sreg");
1066 AddCodeLine ("sty %s+2", lbuf);
1067 AddCodeLine ("ldy sreg+1");
1068 AddCodeLine ("sty %s+3", lbuf);
1079 void g_putlocal (unsigned Flags, int Offs, long Val)
1080 /* Put data into local object. */
1083 CheckLocalOffs (Offs);
1084 switch (Flags & CF_TYPE) {
1087 if (Flags & CF_CONST) {
1088 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1091 AddCodeLine ("sta (sp),y");
1095 if (Flags & CF_CONST) {
1097 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1098 AddCodeLine ("sta (sp),y");
1099 if ((Flags & CF_NOKEEP) == 0) {
1100 /* Place high byte into X */
1101 AddCodeLine ("tax");
1103 if ((Val & 0xFF) == Offs+1) {
1104 /* The value we need is already in Y */
1105 AddCodeLine ("tya");
1106 AddCodeLine ("dey");
1108 AddCodeLine ("dey");
1109 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1111 AddCodeLine ("sta (sp),y");
1113 if ((Flags & CF_NOKEEP) == 0 || IS_Get (&CodeSizeFactor) < 160) {
1115 AddCodeLine ("jsr staxysp");
1118 AddCodeLine ("sta (sp),y");
1119 AddCodeLine ("iny");
1120 AddCodeLine ("txa");
1121 AddCodeLine ("sta (sp),y");
1127 if (Flags & CF_CONST) {
1128 g_getimmed (Flags, Val, 0);
1131 AddCodeLine ("jsr steaxysp");
1142 void g_putind (unsigned Flags, unsigned Offs)
1143 /* Store the specified object type in the primary register at the address
1144 * on the top of the stack
1147 /* We can handle offsets below $100 directly, larger offsets must be added
1148 * to the address. Since a/x is in use, best code is achieved by adding
1149 * just the high byte. Be sure to check if the low byte will overflow while
1152 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1154 /* Overflow - we need to add the low byte also */
1155 AddCodeLine ("ldy #$00");
1156 AddCodeLine ("clc");
1157 AddCodeLine ("pha");
1158 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1159 AddCodeLine ("adc (sp),y");
1160 AddCodeLine ("sta (sp),y");
1161 AddCodeLine ("iny");
1162 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1163 AddCodeLine ("adc (sp),y");
1164 AddCodeLine ("sta (sp),y");
1165 AddCodeLine ("pla");
1167 /* Complete address is on stack, new offset is zero */
1170 } else if ((Offs & 0xFF00) != 0) {
1172 /* We can just add the high byte */
1173 AddCodeLine ("ldy #$01");
1174 AddCodeLine ("clc");
1175 AddCodeLine ("pha");
1176 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1177 AddCodeLine ("adc (sp),y");
1178 AddCodeLine ("sta (sp),y");
1179 AddCodeLine ("pla");
1181 /* Offset is now just the low byte */
1185 /* Check the size and determine operation */
1186 switch (Flags & CF_TYPE) {
1190 AddCodeLine ("jsr staspidx");
1195 AddCodeLine ("jsr staxspidx");
1200 AddCodeLine ("jsr steaxspidx");
1208 /* Pop the argument which is always a pointer */
1214 /*****************************************************************************/
1215 /* type conversion and similiar stuff */
1216 /*****************************************************************************/
1220 void g_toslong (unsigned flags)
1221 /* Make sure, the value on TOS is a long. Convert if necessary */
1223 switch (flags & CF_TYPE) {
1227 if (flags & CF_UNSIGNED) {
1228 AddCodeLine ("jsr tosulong");
1230 AddCodeLine ("jsr toslong");
1245 void g_tosint (unsigned flags)
1246 /* Make sure, the value on TOS is an int. Convert if necessary */
1248 switch (flags & CF_TYPE) {
1255 AddCodeLine ("jsr tosint");
1266 void g_regint (unsigned Flags)
1267 /* Make sure, the value in the primary register an int. Convert if necessary */
1271 switch (Flags & CF_TYPE) {
1274 if (Flags & CF_FORCECHAR) {
1275 /* Conversion is from char */
1276 if (Flags & CF_UNSIGNED) {
1277 AddCodeLine ("ldx #$00");
1279 L = GetLocalLabel();
1280 AddCodeLine ("ldx #$00");
1281 AddCodeLine ("cmp #$80");
1282 AddCodeLine ("bcc %s", LocalLabelName (L));
1283 AddCodeLine ("dex");
1300 void g_reglong (unsigned Flags)
1301 /* Make sure, the value in the primary register a long. Convert if necessary */
1305 switch (Flags & CF_TYPE) {
1308 if (Flags & CF_FORCECHAR) {
1309 /* Conversion is from char */
1310 if (Flags & CF_UNSIGNED) {
1311 if (IS_Get (&CodeSizeFactor) >= 200) {
1312 AddCodeLine ("ldx #$00");
1313 AddCodeLine ("stx sreg");
1314 AddCodeLine ("stx sreg+1");
1316 AddCodeLine ("jsr aulong");
1319 if (IS_Get (&CodeSizeFactor) >= 366) {
1320 L = GetLocalLabel();
1321 AddCodeLine ("ldx #$00");
1322 AddCodeLine ("cmp #$80");
1323 AddCodeLine ("bcc %s", LocalLabelName (L));
1324 AddCodeLine ("dex");
1326 AddCodeLine ("stx sreg");
1327 AddCodeLine ("stx sreg+1");
1329 AddCodeLine ("jsr along");
1336 if (Flags & CF_UNSIGNED) {
1337 if (IS_Get (&CodeSizeFactor) >= 200) {
1339 AddCodeLine ("sty sreg");
1340 AddCodeLine ("sty sreg+1");
1342 AddCodeLine ("jsr axulong");
1345 AddCodeLine ("jsr axlong");
1359 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1360 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1361 * value, that corresponds to the value on TOS, rhs corresponds to the value
1362 * in (e)ax. The return value is the the flags value for the resulting type.
1365 unsigned ltype, rtype;
1368 /* Get the type spec from the flags */
1369 ltype = lhs & CF_TYPE;
1370 rtype = rhs & CF_TYPE;
1372 /* Check if a conversion is needed */
1373 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1374 /* We must promote the primary register to long */
1376 /* Get the new rhs type */
1377 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1379 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1380 /* We must promote the lhs to long */
1386 /* Get the new rhs type */
1387 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1391 /* Determine the result type for the operation:
1392 * - The result is const if both operands are const.
1393 * - The result is unsigned if one of the operands is unsigned.
1394 * - The result is long if one of the operands is long.
1395 * - Otherwise the result is int sized.
1397 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1398 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1399 if (rtype == CF_LONG || ltype == CF_LONG) {
1409 unsigned g_typecast (unsigned lhs, unsigned rhs)
1410 /* Cast the value in the primary register to the operand size that is flagged
1411 * by the lhs value. Return the result value.
1414 unsigned ltype, rtype;
1416 /* Get the type spec from the flags */
1417 ltype = lhs & CF_TYPE;
1418 rtype = rhs & CF_TYPE;
1420 /* Check if a conversion is needed */
1421 if ((rhs & CF_CONST) == 0) {
1422 if (ltype == CF_LONG && rtype != CF_LONG) {
1423 /* We must promote the primary register to long */
1425 } else if (ltype == CF_INT && rtype != CF_INT) {
1426 /* We must promote the primary register to int */
1431 /* Do not need any other action. If the left type is int, and the primary
1432 * register is long, it will be automagically truncated. If the right hand
1433 * side is const, it is not located in the primary register and handled by
1434 * the expression parser code.
1437 /* Result is const if the right hand side was const */
1438 lhs |= (rhs & CF_CONST);
1440 /* The resulting type is that of the left hand side (that's why you called
1448 void g_scale (unsigned flags, long val)
1449 /* Scale the value in the primary register by the given value. If val is positive,
1450 * scale up, is val is negative, scale down. This function is used to scale
1451 * the operands or results of pointer arithmetic by the size of the type, the
1452 * pointer points to.
1457 /* Value may not be zero */
1459 Internal ("Data type has no size");
1460 } else if (val > 0) {
1463 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1465 /* Factor is 2, 4, 8 and 16, use special function */
1466 switch (flags & CF_TYPE) {
1469 if (flags & CF_FORCECHAR) {
1471 AddCodeLine ("asl a");
1478 if (flags & CF_UNSIGNED) {
1479 AddCodeLine ("jsr shlax%d", p2);
1481 AddCodeLine ("jsr aslax%d", p2);
1486 if (flags & CF_UNSIGNED) {
1487 AddCodeLine ("jsr shleax%d", p2);
1489 AddCodeLine ("jsr asleax%d", p2);
1498 } else if (val != 1) {
1500 /* Use a multiplication instead */
1501 g_mul (flags | CF_CONST, val);
1509 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1511 /* Factor is 2, 4, 8 and 16 use special function */
1512 switch (flags & CF_TYPE) {
1515 if (flags & CF_FORCECHAR) {
1516 if (flags & CF_UNSIGNED) {
1518 AddCodeLine ("lsr a");
1521 } else if (p2 <= 2) {
1522 AddCodeLine ("cmp #$80");
1523 AddCodeLine ("ror a");
1530 if (flags & CF_UNSIGNED) {
1531 AddCodeLine ("jsr lsrax%d", p2);
1533 AddCodeLine ("jsr asrax%d", p2);
1538 if (flags & CF_UNSIGNED) {
1539 AddCodeLine ("jsr lsreax%d", p2);
1541 AddCodeLine ("jsr asreax%d", p2);
1550 } else if (val != 1) {
1552 /* Use a division instead */
1553 g_div (flags | CF_CONST, val);
1561 /*****************************************************************************/
1562 /* Adds and subs of variables fix a fixed address */
1563 /*****************************************************************************/
1567 void g_addlocal (unsigned flags, int offs)
1568 /* Add a local variable to ax */
1572 /* Correct the offset and check it */
1574 CheckLocalOffs (offs);
1576 switch (flags & CF_TYPE) {
1579 L = GetLocalLabel();
1580 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1581 AddCodeLine ("clc");
1582 AddCodeLine ("adc (sp),y");
1583 AddCodeLine ("bcc %s", LocalLabelName (L));
1584 AddCodeLine ("inx");
1589 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1590 AddCodeLine ("clc");
1591 AddCodeLine ("adc (sp),y");
1592 AddCodeLine ("pha");
1593 AddCodeLine ("txa");
1594 AddCodeLine ("iny");
1595 AddCodeLine ("adc (sp),y");
1596 AddCodeLine ("tax");
1597 AddCodeLine ("pla");
1601 /* Do it the old way */
1603 g_getlocal (flags, offs);
1615 void g_addstatic (unsigned flags, unsigned long label, long offs)
1616 /* Add a static variable to ax */
1620 /* Create the correct label name */
1621 const char* lbuf = GetLabelName (flags, label, offs);
1623 switch (flags & CF_TYPE) {
1626 L = GetLocalLabel();
1627 AddCodeLine ("clc");
1628 AddCodeLine ("adc %s", lbuf);
1629 AddCodeLine ("bcc %s", LocalLabelName (L));
1630 AddCodeLine ("inx");
1635 AddCodeLine ("clc");
1636 AddCodeLine ("adc %s", lbuf);
1637 AddCodeLine ("tay");
1638 AddCodeLine ("txa");
1639 AddCodeLine ("adc %s+1", lbuf);
1640 AddCodeLine ("tax");
1641 AddCodeLine ("tya");
1645 /* Do it the old way */
1647 g_getstatic (flags, label, offs);
1659 /*****************************************************************************/
1660 /* Special op= functions */
1661 /*****************************************************************************/
1665 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1667 /* Emit += for a static variable */
1669 /* Create the correct label name */
1670 const char* lbuf = GetLabelName (flags, label, offs);
1672 /* Check the size and determine operation */
1673 switch (flags & CF_TYPE) {
1676 if (flags & CF_FORCECHAR) {
1677 AddCodeLine ("ldx #$00");
1678 if (flags & CF_CONST) {
1680 AddCodeLine ("inc %s", lbuf);
1681 AddCodeLine ("lda %s", lbuf);
1683 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1684 AddCodeLine ("clc");
1685 AddCodeLine ("adc %s", lbuf);
1686 AddCodeLine ("sta %s", lbuf);
1689 AddCodeLine ("clc");
1690 AddCodeLine ("adc %s", lbuf);
1691 AddCodeLine ("sta %s", lbuf);
1693 if ((flags & CF_UNSIGNED) == 0) {
1694 unsigned L = GetLocalLabel();
1695 AddCodeLine ("bpl %s", LocalLabelName (L));
1696 AddCodeLine ("dex");
1704 if (flags & CF_CONST) {
1706 unsigned L = GetLocalLabel ();
1707 AddCodeLine ("inc %s", lbuf);
1708 AddCodeLine ("bne %s", LocalLabelName (L));
1709 AddCodeLine ("inc %s+1", lbuf);
1711 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1712 AddCodeLine ("ldx %s+1", lbuf);
1714 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1715 AddCodeLine ("clc");
1716 AddCodeLine ("adc %s", lbuf);
1717 AddCodeLine ("sta %s", lbuf);
1719 unsigned L = GetLocalLabel ();
1720 AddCodeLine ("bcc %s", LocalLabelName (L));
1721 AddCodeLine ("inc %s+1", lbuf);
1723 AddCodeLine ("ldx %s+1", lbuf);
1725 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1726 AddCodeLine ("adc %s+1", lbuf);
1727 AddCodeLine ("sta %s+1", lbuf);
1728 AddCodeLine ("tax");
1729 AddCodeLine ("lda %s", lbuf);
1733 AddCodeLine ("clc");
1734 AddCodeLine ("adc %s", lbuf);
1735 AddCodeLine ("sta %s", lbuf);
1736 AddCodeLine ("txa");
1737 AddCodeLine ("adc %s+1", lbuf);
1738 AddCodeLine ("sta %s+1", lbuf);
1739 AddCodeLine ("tax");
1740 AddCodeLine ("lda %s", lbuf);
1745 if (flags & CF_CONST) {
1747 AddCodeLine ("ldy #<(%s)", lbuf);
1748 AddCodeLine ("sty ptr1");
1749 AddCodeLine ("ldy #>(%s)", lbuf);
1751 AddCodeLine ("jsr laddeq1");
1753 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1754 AddCodeLine ("jsr laddeqa");
1757 g_getstatic (flags, label, offs);
1759 g_putstatic (flags, label, offs);
1762 AddCodeLine ("ldy #<(%s)", lbuf);
1763 AddCodeLine ("sty ptr1");
1764 AddCodeLine ("ldy #>(%s)", lbuf);
1765 AddCodeLine ("jsr laddeq");
1776 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1777 /* Emit += for a local variable */
1779 /* Calculate the true offset, check it, load it into Y */
1781 CheckLocalOffs (offs);
1783 /* Check the size and determine operation */
1784 switch (flags & CF_TYPE) {
1787 if (flags & CF_FORCECHAR) {
1789 AddCodeLine ("ldx #$00");
1790 if (flags & CF_CONST) {
1791 AddCodeLine ("clc");
1792 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1793 AddCodeLine ("adc (sp),y");
1794 AddCodeLine ("sta (sp),y");
1796 AddCodeLine ("clc");
1797 AddCodeLine ("adc (sp),y");
1798 AddCodeLine ("sta (sp),y");
1800 if ((flags & CF_UNSIGNED) == 0) {
1801 unsigned L = GetLocalLabel();
1802 AddCodeLine ("bpl %s", LocalLabelName (L));
1803 AddCodeLine ("dex");
1812 if (flags & CF_CONST) {
1813 if (IS_Get (&CodeSizeFactor) >= 400) {
1814 AddCodeLine ("clc");
1815 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1816 AddCodeLine ("adc (sp),y");
1817 AddCodeLine ("sta (sp),y");
1818 AddCodeLine ("iny");
1819 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1820 AddCodeLine ("adc (sp),y");
1821 AddCodeLine ("sta (sp),y");
1822 AddCodeLine ("tax");
1823 AddCodeLine ("dey");
1824 AddCodeLine ("lda (sp),y");
1826 g_getimmed (flags, val, 0);
1827 AddCodeLine ("jsr addeqysp");
1830 AddCodeLine ("jsr addeqysp");
1835 if (flags & CF_CONST) {
1836 g_getimmed (flags, val, 0);
1839 AddCodeLine ("jsr laddeqysp");
1849 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1850 /* Emit += for the location with address in ax */
1852 /* If the offset is too large for a byte register, add the high byte
1853 * of the offset to the primary. Beware: We need a special correction
1854 * if the offset in the low byte will overflow in the operation.
1856 offs = MakeByteOffs (flags, offs);
1858 /* Check the size and determine operation */
1859 switch (flags & CF_TYPE) {
1862 AddCodeLine ("sta ptr1");
1863 AddCodeLine ("stx ptr1+1");
1864 AddCodeLine ("ldy #$%02X", offs);
1865 AddCodeLine ("ldx #$00");
1866 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1867 AddCodeLine ("clc");
1868 AddCodeLine ("adc (ptr1),y");
1869 AddCodeLine ("sta (ptr1),y");
1873 if (IS_Get (&CodeSizeFactor) >= 200) {
1874 /* Lots of code, use only if size is not important */
1875 AddCodeLine ("sta ptr1");
1876 AddCodeLine ("stx ptr1+1");
1877 AddCodeLine ("ldy #$%02X", offs);
1878 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1879 AddCodeLine ("clc");
1880 AddCodeLine ("adc (ptr1),y");
1881 AddCodeLine ("sta (ptr1),y");
1882 AddCodeLine ("pha");
1883 AddCodeLine ("iny");
1884 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1885 AddCodeLine ("adc (ptr1),y");
1886 AddCodeLine ("sta (ptr1),y");
1887 AddCodeLine ("tax");
1888 AddCodeLine ("pla");
1894 AddCodeLine ("jsr pushax"); /* Push the address */
1895 push (CF_PTR); /* Correct the internal sp */
1896 g_getind (flags, offs); /* Fetch the value */
1897 g_inc (flags, val); /* Increment value in primary */
1898 g_putind (flags, offs); /* Store the value back */
1908 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1910 /* Emit -= for a static variable */
1912 /* Create the correct label name */
1913 const char* lbuf = GetLabelName (flags, label, offs);
1915 /* Check the size and determine operation */
1916 switch (flags & CF_TYPE) {
1919 if (flags & CF_FORCECHAR) {
1920 AddCodeLine ("ldx #$00");
1921 if (flags & CF_CONST) {
1923 AddCodeLine ("dec %s", lbuf);
1924 AddCodeLine ("lda %s", lbuf);
1926 AddCodeLine ("lda %s", lbuf);
1927 AddCodeLine ("sec");
1928 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1929 AddCodeLine ("sta %s", lbuf);
1932 AddCodeLine ("eor #$FF");
1933 AddCodeLine ("sec");
1934 AddCodeLine ("adc %s", lbuf);
1935 AddCodeLine ("sta %s", lbuf);
1937 if ((flags & CF_UNSIGNED) == 0) {
1938 unsigned L = GetLocalLabel();
1939 AddCodeLine ("bpl %s", LocalLabelName (L));
1940 AddCodeLine ("dex");
1948 if (flags & CF_CONST) {
1949 AddCodeLine ("lda %s", lbuf);
1950 AddCodeLine ("sec");
1951 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1952 AddCodeLine ("sta %s", lbuf);
1954 unsigned L = GetLocalLabel ();
1955 AddCodeLine ("bcs %s", LocalLabelName (L));
1956 AddCodeLine ("dec %s+1", lbuf);
1958 AddCodeLine ("ldx %s+1", lbuf);
1960 AddCodeLine ("lda %s+1", lbuf);
1961 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1962 AddCodeLine ("sta %s+1", lbuf);
1963 AddCodeLine ("tax");
1964 AddCodeLine ("lda %s", lbuf);
1967 AddCodeLine ("eor #$FF");
1968 AddCodeLine ("sec");
1969 AddCodeLine ("adc %s", lbuf);
1970 AddCodeLine ("sta %s", lbuf);
1971 AddCodeLine ("txa");
1972 AddCodeLine ("eor #$FF");
1973 AddCodeLine ("adc %s+1", lbuf);
1974 AddCodeLine ("sta %s+1", lbuf);
1975 AddCodeLine ("tax");
1976 AddCodeLine ("lda %s", lbuf);
1981 if (flags & CF_CONST) {
1983 AddCodeLine ("ldy #<(%s)", lbuf);
1984 AddCodeLine ("sty ptr1");
1985 AddCodeLine ("ldy #>(%s)", lbuf);
1986 AddCodeLine ("lda #$%02X", (unsigned char)val);
1987 AddCodeLine ("jsr lsubeqa");
1989 g_getstatic (flags, label, offs);
1991 g_putstatic (flags, label, offs);
1994 AddCodeLine ("ldy #<(%s)", lbuf);
1995 AddCodeLine ("sty ptr1");
1996 AddCodeLine ("ldy #>(%s)", lbuf);
1997 AddCodeLine ("jsr lsubeq");
2008 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
2009 /* Emit -= for a local variable */
2011 /* Calculate the true offset, check it, load it into Y */
2013 CheckLocalOffs (offs);
2015 /* Check the size and determine operation */
2016 switch (flags & CF_TYPE) {
2019 if (flags & CF_FORCECHAR) {
2021 AddCodeLine ("ldx #$00");
2022 if (flags & CF_CONST) {
2023 AddCodeLine ("lda (sp),y");
2024 AddCodeLine ("sec");
2025 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2027 AddCodeLine ("eor #$FF");
2028 AddCodeLine ("sec");
2029 AddCodeLine ("adc (sp),y");
2031 AddCodeLine ("sta (sp),y");
2032 if ((flags & CF_UNSIGNED) == 0) {
2033 unsigned L = GetLocalLabel();
2034 AddCodeLine ("bpl %s", LocalLabelName (L));
2035 AddCodeLine ("dex");
2043 if (flags & CF_CONST) {
2044 g_getimmed (flags, val, 0);
2047 AddCodeLine ("jsr subeqysp");
2051 if (flags & CF_CONST) {
2052 g_getimmed (flags, val, 0);
2055 AddCodeLine ("jsr lsubeqysp");
2065 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2066 /* Emit -= for the location with address in ax */
2068 /* If the offset is too large for a byte register, add the high byte
2069 * of the offset to the primary. Beware: We need a special correction
2070 * if the offset in the low byte will overflow in the operation.
2072 offs = MakeByteOffs (flags, offs);
2074 /* Check the size and determine operation */
2075 switch (flags & CF_TYPE) {
2078 AddCodeLine ("sta ptr1");
2079 AddCodeLine ("stx ptr1+1");
2080 AddCodeLine ("ldy #$%02X", offs);
2081 AddCodeLine ("ldx #$00");
2082 AddCodeLine ("lda (ptr1),y");
2083 AddCodeLine ("sec");
2084 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2085 AddCodeLine ("sta (ptr1),y");
2089 if (IS_Get (&CodeSizeFactor) >= 200) {
2090 /* Lots of code, use only if size is not important */
2091 AddCodeLine ("sta ptr1");
2092 AddCodeLine ("stx ptr1+1");
2093 AddCodeLine ("ldy #$%02X", offs);
2094 AddCodeLine ("lda (ptr1),y");
2095 AddCodeLine ("sec");
2096 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2097 AddCodeLine ("sta (ptr1),y");
2098 AddCodeLine ("pha");
2099 AddCodeLine ("iny");
2100 AddCodeLine ("lda (ptr1),y");
2101 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2102 AddCodeLine ("sta (ptr1),y");
2103 AddCodeLine ("tax");
2104 AddCodeLine ("pla");
2110 AddCodeLine ("jsr pushax"); /* Push the address */
2111 push (CF_PTR); /* Correct the internal sp */
2112 g_getind (flags, offs); /* Fetch the value */
2113 g_dec (flags, val); /* Increment value in primary */
2114 g_putind (flags, offs); /* Store the value back */
2124 /*****************************************************************************/
2125 /* Add a variable address to the value in ax */
2126 /*****************************************************************************/
2130 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2131 /* Add the address of a local variable to ax */
2135 /* Add the offset */
2138 /* We cannot address more then 256 bytes of locals anyway */
2139 L = GetLocalLabel();
2140 CheckLocalOffs (offs);
2141 AddCodeLine ("clc");
2142 AddCodeLine ("adc #$%02X", offs & 0xFF);
2143 /* Do also skip the CLC insn below */
2144 AddCodeLine ("bcc %s", LocalLabelName (L));
2145 AddCodeLine ("inx");
2148 /* Add the current stackpointer value */
2149 AddCodeLine ("clc");
2151 /* Label was used above */
2154 AddCodeLine ("adc sp");
2155 AddCodeLine ("tay");
2156 AddCodeLine ("txa");
2157 AddCodeLine ("adc sp+1");
2158 AddCodeLine ("tax");
2159 AddCodeLine ("tya");
2164 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2165 /* Add the address of a static variable to ax */
2167 /* Create the correct label name */
2168 const char* lbuf = GetLabelName (flags, label, offs);
2170 /* Add the address to the current ax value */
2171 AddCodeLine ("clc");
2172 AddCodeLine ("adc #<(%s)", lbuf);
2173 AddCodeLine ("tay");
2174 AddCodeLine ("txa");
2175 AddCodeLine ("adc #>(%s)", lbuf);
2176 AddCodeLine ("tax");
2177 AddCodeLine ("tya");
2182 /*****************************************************************************/
2184 /*****************************************************************************/
2188 void g_save (unsigned flags)
2189 /* Copy primary register to hold register. */
2191 /* Check the size and determine operation */
2192 switch (flags & CF_TYPE) {
2195 if (flags & CF_FORCECHAR) {
2196 AddCodeLine ("pha");
2202 AddCodeLine ("sta regsave");
2203 AddCodeLine ("stx regsave+1");
2207 AddCodeLine ("jsr saveeax");
2217 void g_restore (unsigned flags)
2218 /* Copy hold register to primary. */
2220 /* Check the size and determine operation */
2221 switch (flags & CF_TYPE) {
2224 if (flags & CF_FORCECHAR) {
2225 AddCodeLine ("pla");
2231 AddCodeLine ("lda regsave");
2232 AddCodeLine ("ldx regsave+1");
2236 AddCodeLine ("jsr resteax");
2246 void g_cmp (unsigned flags, unsigned long val)
2247 /* Immidiate compare. The primary register will not be changed, Z flag
2253 /* Check the size and determine operation */
2254 switch (flags & CF_TYPE) {
2257 if (flags & CF_FORCECHAR) {
2258 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2264 L = GetLocalLabel();
2265 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2266 AddCodeLine ("bne %s", LocalLabelName (L));
2267 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2272 Internal ("g_cmp: Long compares not implemented");
2282 static void oper (unsigned Flags, unsigned long Val, const char** Subs)
2283 /* Encode a binary operation. subs is a pointer to four strings:
2284 * 0 --> Operate on ints
2285 * 1 --> Operate on unsigneds
2286 * 2 --> Operate on longs
2287 * 3 --> Operate on unsigned longs
2290 /* Determine the offset into the array */
2291 if (Flags & CF_UNSIGNED) {
2294 if ((Flags & CF_TYPE) == CF_LONG) {
2298 /* Load the value if it is not already in the primary */
2299 if (Flags & CF_CONST) {
2301 g_getimmed (Flags, Val, 0);
2304 /* Output the operation */
2305 AddCodeLine ("jsr %s", *Subs);
2307 /* The operation will pop it's argument */
2313 void g_test (unsigned flags)
2314 /* Test the value in the primary and set the condition codes */
2316 switch (flags & CF_TYPE) {
2319 if (flags & CF_FORCECHAR) {
2320 AddCodeLine ("tax");
2326 AddCodeLine ("stx tmp1");
2327 AddCodeLine ("ora tmp1");
2331 if (flags & CF_UNSIGNED) {
2332 AddCodeLine ("jsr utsteax");
2334 AddCodeLine ("jsr tsteax");
2346 void g_push (unsigned flags, unsigned long val)
2347 /* Push the primary register or a constant value onto the stack */
2349 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2351 /* We have a constant 8 or 16 bit value */
2352 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2354 /* Handle as 8 bit value */
2356 AddCodeLine ("jsr pusha");
2360 /* Handle as 16 bit value */
2361 g_getimmed (flags, val, 0);
2362 AddCodeLine ("jsr pushax");
2367 /* Value is not 16 bit or not constant */
2368 if (flags & CF_CONST) {
2369 /* Constant 32 bit value, load into eax */
2370 g_getimmed (flags, val, 0);
2373 /* Push the primary register */
2374 switch (flags & CF_TYPE) {
2377 if (flags & CF_FORCECHAR) {
2378 /* Handle as char */
2379 AddCodeLine ("jsr pusha");
2384 AddCodeLine ("jsr pushax");
2388 AddCodeLine ("jsr pusheax");
2398 /* Adjust the stack offset */
2404 void g_swap (unsigned flags)
2405 /* Swap the primary register and the top of the stack. flags give the type
2406 * of *both* values (must have same size).
2409 switch (flags & CF_TYPE) {
2413 AddCodeLine ("jsr swapstk");
2417 AddCodeLine ("jsr swapestk");
2428 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2429 /* Call the specified subroutine name */
2431 if ((Flags & CF_FIXARGC) == 0) {
2432 /* Pass the argument count */
2435 AddCodeLine ("jsr _%s", Label);
2436 StackPtr += ArgSize; /* callee pops args */
2441 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2442 /* Call subroutine indirect */
2444 if ((Flags & CF_LOCAL) == 0) {
2445 /* Address is in a/x */
2446 if ((Flags & CF_FIXARGC) == 0) {
2447 /* Pass arg count */
2450 AddCodeLine ("jsr callax");
2452 /* The address is on stack, offset is on Val */
2454 CheckLocalOffs (Offs);
2455 AddCodeLine ("pha");
2456 AddCodeLine ("ldy #$%02X", Offs);
2457 AddCodeLine ("lda (sp),y");
2458 AddCodeLine ("sta jmpvec+1");
2459 AddCodeLine ("iny");
2460 AddCodeLine ("lda (sp),y");
2461 AddCodeLine ("sta jmpvec+2");
2462 AddCodeLine ("pla");
2463 AddCodeLine ("jsr jmpvec");
2466 /* Callee pops args */
2467 StackPtr += ArgSize;
2472 void g_jump (unsigned Label)
2473 /* Jump to specified internal label number */
2475 AddCodeLine ("jmp %s", LocalLabelName (Label));
2480 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2481 /* Jump to label if zero flag clear */
2483 AddCodeLine ("jne %s", LocalLabelName (label));
2488 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2489 /* Jump to label if zero flag set */
2491 AddCodeLine ("jeq %s", LocalLabelName (label));
2496 static void mod_internal (int k, char* verb1, char* verb2)
2499 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2503 AddCodeLine ("jsr %ssp", verb2);
2509 void g_space (int space)
2510 /* Create or drop space on the stack */
2513 mod_internal (-space, "inc", "addy");
2514 } else if (space > 0) {
2515 mod_internal (space, "dec", "suby");
2521 void g_cstackcheck (void)
2522 /* Check for a C stack overflow */
2524 AddCodeLine ("jsr cstkchk");
2529 void g_stackcheck (void)
2530 /* Check for a stack overflow */
2532 AddCodeLine ("jsr stkchk");
2537 void g_add (unsigned flags, unsigned long val)
2538 /* Primary = TOS + Primary */
2540 static const char* ops[12] = {
2541 "tosaddax", "tosaddax", "tosaddeax", "tosaddeax"
2544 if (flags & CF_CONST) {
2545 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2546 g_push (flags & ~CF_CONST, 0);
2548 oper (flags, val, ops);
2553 void g_sub (unsigned flags, unsigned long val)
2554 /* Primary = TOS - Primary */
2556 static const char* ops[12] = {
2557 "tossubax", "tossubax", "tossubeax", "tossubeax",
2560 if (flags & CF_CONST) {
2561 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2562 g_push (flags & ~CF_CONST, 0);
2564 oper (flags, val, ops);
2569 void g_rsub (unsigned flags, unsigned long val)
2570 /* Primary = Primary - TOS */
2572 static const char* ops[12] = {
2573 "tosrsubax", "tosrsubax", "tosrsubeax", "tosrsubeax",
2575 oper (flags, val, ops);
2580 void g_mul (unsigned flags, unsigned long val)
2581 /* Primary = TOS * Primary */
2583 static const char* ops[12] = {
2584 "tosmulax", "tosumulax", "tosmuleax", "tosumuleax",
2589 /* Do strength reduction if the value is constant and a power of two */
2590 if (flags & CF_CONST && (p2 = PowerOf2 (val)) >= 0) {
2591 /* Generate a shift instead */
2596 /* If the right hand side is const, the lhs is not on stack but still
2597 * in the primary register.
2599 if (flags & CF_CONST) {
2601 switch (flags & CF_TYPE) {
2604 if (flags & CF_FORCECHAR) {
2605 /* Handle some special cases */
2609 AddCodeLine ("sta tmp1");
2610 AddCodeLine ("asl a");
2611 AddCodeLine ("clc");
2612 AddCodeLine ("adc tmp1");
2616 AddCodeLine ("sta tmp1");
2617 AddCodeLine ("asl a");
2618 AddCodeLine ("asl a");
2619 AddCodeLine ("clc");
2620 AddCodeLine ("adc tmp1");
2624 AddCodeLine ("sta tmp1");
2625 AddCodeLine ("asl a");
2626 AddCodeLine ("clc");
2627 AddCodeLine ("adc tmp1");
2628 AddCodeLine ("asl a");
2632 AddCodeLine ("sta tmp1");
2633 AddCodeLine ("asl a");
2634 AddCodeLine ("asl a");
2635 AddCodeLine ("clc");
2636 AddCodeLine ("adc tmp1");
2637 AddCodeLine ("asl a");
2646 AddCodeLine ("jsr mulax3");
2649 AddCodeLine ("jsr mulax5");
2652 AddCodeLine ("jsr mulax6");
2655 AddCodeLine ("jsr mulax7");
2658 AddCodeLine ("jsr mulax9");
2661 AddCodeLine ("jsr mulax10");
2673 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2674 * into the normal, non-optimized stuff.
2676 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2677 g_push (flags & ~CF_CONST, 0);
2681 /* Use long way over the stack */
2682 oper (flags, val, ops);
2687 void g_div (unsigned flags, unsigned long val)
2688 /* Primary = TOS / Primary */
2690 static const char* ops[12] = {
2691 "tosdivax", "tosudivax", "tosdiveax", "tosudiveax",
2694 /* Do strength reduction if the value is constant and a power of two */
2696 if ((flags & CF_CONST) && (p2 = PowerOf2 (val)) >= 0) {
2697 /* Generate a shift instead */
2700 /* Generate a division */
2701 if (flags & CF_CONST) {
2702 /* lhs is not on stack */
2703 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2704 g_push (flags & ~CF_CONST, 0);
2706 oper (flags, val, ops);
2712 void g_mod (unsigned flags, unsigned long val)
2713 /* Primary = TOS % Primary */
2715 static const char* ops[12] = {
2716 "tosmodax", "tosumodax", "tosmodeax", "tosumodeax",
2720 /* Check if we can do some cost reduction */
2721 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = PowerOf2 (val)) >= 0) {
2722 /* We can do that with an AND operation */
2723 g_and (flags, val - 1);
2725 /* Do it the hard way... */
2726 if (flags & CF_CONST) {
2727 /* lhs is not on stack */
2728 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2729 g_push (flags & ~CF_CONST, 0);
2731 oper (flags, val, ops);
2737 void g_or (unsigned flags, unsigned long val)
2738 /* Primary = TOS | Primary */
2740 static const char* ops[12] = {
2741 "tosorax", "tosorax", "tosoreax", "tosoreax",
2744 /* If the right hand side is const, the lhs is not on stack but still
2745 * in the primary register.
2747 if (flags & CF_CONST) {
2749 switch (flags & CF_TYPE) {
2752 if (flags & CF_FORCECHAR) {
2753 if ((val & 0xFF) != 0) {
2754 AddCodeLine ("ora #$%02X", (unsigned char)val);
2762 if ((val & 0xFF) != 0) {
2763 AddCodeLine ("ora #$%02X", (unsigned char)val);
2765 } else if ((val & 0xFF00) == 0xFF00) {
2766 if ((val & 0xFF) != 0) {
2767 AddCodeLine ("ora #$%02X", (unsigned char)val);
2770 } else if (val != 0) {
2771 AddCodeLine ("ora #$%02X", (unsigned char)val);
2772 AddCodeLine ("pha");
2773 AddCodeLine ("txa");
2774 AddCodeLine ("ora #$%02X", (unsigned char)(val >> 8));
2775 AddCodeLine ("tax");
2776 AddCodeLine ("pla");
2782 if ((val & 0xFF) != 0) {
2783 AddCodeLine ("ora #$%02X", (unsigned char)val);
2793 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2794 * into the normal, non-optimized stuff. Note: The standard stuff will
2795 * always work with ints.
2797 flags &= ~CF_FORCECHAR;
2798 g_push (flags & ~CF_CONST, 0);
2801 /* Use long way over the stack */
2802 oper (flags, val, ops);
2807 void g_xor (unsigned flags, unsigned long val)
2808 /* Primary = TOS ^ Primary */
2810 static const char* ops[12] = {
2811 "tosxorax", "tosxorax", "tosxoreax", "tosxoreax",
2815 /* If the right hand side is const, the lhs is not on stack but still
2816 * in the primary register.
2818 if (flags & CF_CONST) {
2820 switch (flags & CF_TYPE) {
2823 if (flags & CF_FORCECHAR) {
2824 if ((val & 0xFF) != 0) {
2825 AddCodeLine ("eor #$%02X", (unsigned char)val);
2834 AddCodeLine ("eor #$%02X", (unsigned char)val);
2836 } else if (val != 0) {
2837 if ((val & 0xFF) != 0) {
2838 AddCodeLine ("eor #$%02X", (unsigned char)val);
2840 AddCodeLine ("pha");
2841 AddCodeLine ("txa");
2842 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2843 AddCodeLine ("tax");
2844 AddCodeLine ("pla");
2851 AddCodeLine ("eor #$%02X", (unsigned char)val);
2861 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2862 * into the normal, non-optimized stuff. Note: The standard stuff will
2863 * always work with ints.
2865 flags &= ~CF_FORCECHAR;
2866 g_push (flags & ~CF_CONST, 0);
2869 /* Use long way over the stack */
2870 oper (flags, val, ops);
2875 void g_and (unsigned Flags, unsigned long Val)
2876 /* Primary = TOS & Primary */
2878 static const char* ops[12] = {
2879 "tosandax", "tosandax", "tosandeax", "tosandeax",
2882 /* If the right hand side is const, the lhs is not on stack but still
2883 * in the primary register.
2885 if (Flags & CF_CONST) {
2887 switch (Flags & CF_TYPE) {
2890 if (Flags & CF_FORCECHAR) {
2891 if ((Val & 0xFF) == 0x00) {
2892 AddCodeLine ("lda #$00");
2893 } else if ((Val & 0xFF) != 0xFF) {
2894 AddCodeLine ("and #$%02X", (unsigned char)Val);
2900 if ((Val & 0xFFFF) != 0xFFFF) {
2904 AddCodeLine ("lda #$00");
2905 } else if (Val != 0xFF) {
2906 AddCodeLine ("and #$%02X", (unsigned char)Val);
2908 } else if ((Val & 0xFFFF) == 0xFF00) {
2909 AddCodeLine ("lda #$00");
2910 } else if ((Val & 0xFF00) == 0xFF00) {
2911 AddCodeLine ("and #$%02X", (unsigned char)Val);
2912 } else if ((Val & 0x00FF) == 0x0000) {
2913 AddCodeLine ("txa");
2914 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2915 AddCodeLine ("tax");
2916 AddCodeLine ("lda #$00");
2918 AddCodeLine ("tay");
2919 AddCodeLine ("txa");
2920 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2921 AddCodeLine ("tax");
2922 AddCodeLine ("tya");
2923 if ((Val & 0x00FF) == 0x0000) {
2924 AddCodeLine ("lda #$00");
2925 } else if ((Val & 0x00FF) != 0x00FF) {
2926 AddCodeLine ("and #$%02X", (unsigned char)Val);
2935 AddCodeLine ("stx sreg+1");
2936 AddCodeLine ("stx sreg");
2937 if ((Val & 0xFF) != 0xFF) {
2938 AddCodeLine ("and #$%02X", (unsigned char)Val);
2941 } else if (Val == 0xFF00) {
2943 AddCodeLine ("sta sreg+1");
2944 AddCodeLine ("sta sreg");
2953 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2954 * into the normal, non-optimized stuff. Note: The standard stuff will
2955 * always work with ints.
2957 Flags &= ~CF_FORCECHAR;
2958 g_push (Flags & ~CF_CONST, 0);
2961 /* Use long way over the stack */
2962 oper (Flags, Val, ops);
2967 void g_asr (unsigned flags, unsigned long val)
2968 /* Primary = TOS >> Primary */
2970 static const char* ops[12] = {
2971 "tosasrax", "tosshrax", "tosasreax", "tosshreax",
2974 /* If the right hand side is const, the lhs is not on stack but still
2975 * in the primary register.
2977 if (flags & CF_CONST) {
2979 switch (flags & CF_TYPE) {
2985 if (flags & CF_UNSIGNED) {
2986 AddCodeLine ("txa");
2989 unsigned L = GetLocalLabel();
2990 AddCodeLine ("cpx #$80"); /* Sign bit into carry */
2991 AddCodeLine ("txa");
2992 AddCodeLine ("ldx #$00");
2993 AddCodeLine ("bcc %s", LocalLabelName (L));
2994 AddCodeLine ("dex"); /* Make $FF */
3000 if (flags & CF_UNSIGNED) {
3001 AddCodeLine ("jsr shrax4");
3003 AddCodeLine ("jsr asrax4");
3008 if (flags & CF_UNSIGNED) {
3009 AddCodeLine ("jsr shrax%ld", val);
3011 AddCodeLine ("jsr asrax%ld", val);
3019 AddCodeLine ("ldx #$00");
3020 AddCodeLine ("lda sreg+1");
3021 if ((flags & CF_UNSIGNED) == 0) {
3022 unsigned L = GetLocalLabel();
3023 AddCodeLine ("bpl %s", LocalLabelName (L));
3024 AddCodeLine ("dex");
3027 AddCodeLine ("stx sreg");
3028 AddCodeLine ("stx sreg+1");
3032 AddCodeLine ("ldy #$00");
3033 AddCodeLine ("ldx sreg+1");
3034 if ((flags & CF_UNSIGNED) == 0) {
3035 unsigned L = GetLocalLabel();
3036 AddCodeLine ("bpl %s", LocalLabelName (L));
3037 AddCodeLine ("dey");
3040 AddCodeLine ("lda sreg");
3041 AddCodeLine ("sty sreg+1");
3042 AddCodeLine ("sty sreg");
3046 AddCodeLine ("txa");
3047 AddCodeLine ("ldx sreg");
3048 AddCodeLine ("ldy sreg+1");
3049 AddCodeLine ("sty sreg");
3050 if ((flags & CF_UNSIGNED) == 0) {
3051 unsigned L = GetLocalLabel();
3052 AddCodeLine ("cpy #$80");
3053 AddCodeLine ("ldy #$00");
3054 AddCodeLine ("bcc %s", LocalLabelName (L));
3055 AddCodeLine ("dey");
3058 AddCodeLine ("ldy #$00");
3060 AddCodeLine ("sty sreg+1");
3064 if (flags & CF_UNSIGNED) {
3065 AddCodeLine ("jsr shreax4");
3067 AddCodeLine ("jsr asreax4");
3072 if (flags & CF_UNSIGNED) {
3073 AddCodeLine ("jsr shreax%ld", val);
3075 AddCodeLine ("jsr asreax%ld", val);
3084 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3085 * into the normal, non-optimized stuff. Note: The standard stuff will
3086 * always work with ints.
3088 flags &= ~CF_FORCECHAR;
3089 g_push (flags & ~CF_CONST, 0);
3092 /* Use long way over the stack */
3093 oper (flags, val, ops);
3098 void g_asl (unsigned flags, unsigned long val)
3099 /* Primary = TOS << Primary */
3101 static const char* ops[12] = {
3102 "tosaslax", "tosshlax", "tosasleax", "tosshleax",
3106 /* If the right hand side is const, the lhs is not on stack but still
3107 * in the primary register.
3109 if (flags & CF_CONST) {
3111 switch (flags & CF_TYPE) {
3117 AddCodeLine ("tax");
3118 AddCodeLine ("lda #$00");
3122 if (flags & CF_UNSIGNED) {
3123 AddCodeLine ("jsr shlax4");
3125 AddCodeLine ("jsr aslax4");
3130 if (flags & CF_UNSIGNED) {
3131 AddCodeLine ("jsr shlax%ld", val);
3133 AddCodeLine ("jsr aslax%ld", val);
3141 AddCodeLine ("sta sreg+1");
3142 AddCodeLine ("lda #$00");
3143 AddCodeLine ("tax");
3144 AddCodeLine ("sta sreg");
3148 AddCodeLine ("stx sreg+1");
3149 AddCodeLine ("sta sreg");
3150 AddCodeLine ("lda #$00");
3151 AddCodeLine ("tax");
3155 AddCodeLine ("ldy sreg");
3156 AddCodeLine ("sty sreg+1");
3157 AddCodeLine ("stx sreg");
3158 AddCodeLine ("tax");
3159 AddCodeLine ("lda #$00");
3163 if (flags & CF_UNSIGNED) {
3164 AddCodeLine ("jsr shleax4");
3166 AddCodeLine ("jsr asleax4");
3171 if (flags & CF_UNSIGNED) {
3172 AddCodeLine ("jsr shleax%ld", val);
3174 AddCodeLine ("jsr asleax%ld", val);
3183 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3184 * into the normal, non-optimized stuff. Note: The standard stuff will
3185 * always work with ints.
3187 flags &= ~CF_FORCECHAR;
3188 g_push (flags & ~CF_CONST, 0);
3191 /* Use long way over the stack */
3192 oper (flags, val, ops);
3197 void g_neg (unsigned Flags)
3198 /* Primary = -Primary */
3200 switch (Flags & CF_TYPE) {
3203 if (Flags & CF_FORCECHAR) {
3204 AddCodeLine ("eor #$FF");
3205 AddCodeLine ("clc");
3206 AddCodeLine ("adc #$01");
3212 AddCodeLine ("jsr negax");
3216 AddCodeLine ("jsr negeax");
3226 void g_bneg (unsigned flags)
3227 /* Primary = !Primary */
3229 switch (flags & CF_TYPE) {
3232 AddCodeLine ("jsr bnega");
3236 AddCodeLine ("jsr bnegax");
3240 AddCodeLine ("jsr bnegeax");
3250 void g_com (unsigned Flags)
3251 /* Primary = ~Primary */
3253 switch (Flags & CF_TYPE) {
3256 if (Flags & CF_FORCECHAR) {
3257 AddCodeLine ("eor #$FF");
3263 AddCodeLine ("jsr complax");
3267 AddCodeLine ("jsr compleax");
3277 void g_inc (unsigned flags, unsigned long val)
3278 /* Increment the primary register by a given number */
3280 /* Don't inc by zero */
3285 /* Generate code for the supported types */
3287 switch (flags & CF_TYPE) {
3290 if (flags & CF_FORCECHAR) {
3291 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3293 AddCodeLine ("ina");
3296 AddCodeLine ("clc");
3297 AddCodeLine ("adc #$%02X", (unsigned char)val);
3304 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) {
3305 unsigned L = GetLocalLabel();
3306 AddCodeLine ("ina");
3307 AddCodeLine ("bne %s", LocalLabelName (L));
3308 AddCodeLine ("inx");
3310 } else if (IS_Get (&CodeSizeFactor) < 200) {
3313 AddCodeLine ("jsr incax%lu", val);
3314 } else if (val <= 255) {
3316 AddCodeLine ("jsr incaxy");
3318 g_add (flags | CF_CONST, val);
3321 /* Inline the code */
3323 if ((val & 0xFF) != 0) {
3324 unsigned L = GetLocalLabel();
3325 AddCodeLine ("clc");
3326 AddCodeLine ("adc #$%02X", (unsigned char) val);
3327 AddCodeLine ("bcc %s", LocalLabelName (L));
3328 AddCodeLine ("inx");
3332 AddCodeLine ("inx");
3335 AddCodeLine ("inx");
3338 AddCodeLine ("inx");
3340 } else if ((val & 0xFF) != 0) {
3341 AddCodeLine ("clc");
3342 AddCodeLine ("adc #$%02X", (unsigned char) val);
3343 AddCodeLine ("pha");
3344 AddCodeLine ("txa");
3345 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3346 AddCodeLine ("tax");
3347 AddCodeLine ("pla");
3349 AddCodeLine ("pha");
3350 AddCodeLine ("txa");
3351 AddCodeLine ("clc");
3352 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3353 AddCodeLine ("tax");
3354 AddCodeLine ("pla");
3362 AddCodeLine ("jsr inceaxy");
3364 g_add (flags | CF_CONST, val);
3376 void g_dec (unsigned flags, unsigned long val)
3377 /* Decrement the primary register by a given number */
3379 /* Don't dec by zero */
3384 /* Generate code for the supported types */
3386 switch (flags & CF_TYPE) {
3389 if (flags & CF_FORCECHAR) {
3390 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3392 AddCodeLine ("dea");
3395 AddCodeLine ("sec");
3396 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3403 if (IS_Get (&CodeSizeFactor) < 200) {
3404 /* Use subroutines */
3406 AddCodeLine ("jsr decax%d", (int) val);
3407 } else if (val <= 255) {
3409 AddCodeLine ("jsr decaxy");
3411 g_sub (flags | CF_CONST, val);
3414 /* Inline the code */
3416 if ((val & 0xFF) != 0) {
3417 unsigned L = GetLocalLabel();
3418 AddCodeLine ("sec");
3419 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3420 AddCodeLine ("bcs %s", LocalLabelName (L));
3421 AddCodeLine ("dex");
3425 AddCodeLine ("dex");
3428 AddCodeLine ("dex");
3431 if ((val & 0xFF) != 0) {
3432 AddCodeLine ("sec");
3433 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3434 AddCodeLine ("pha");
3435 AddCodeLine ("txa");
3436 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3437 AddCodeLine ("tax");
3438 AddCodeLine ("pla");
3440 AddCodeLine ("pha");
3441 AddCodeLine ("txa");
3442 AddCodeLine ("sec");
3443 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3444 AddCodeLine ("tax");
3445 AddCodeLine ("pla");
3454 AddCodeLine ("jsr deceaxy");
3456 g_sub (flags | CF_CONST, val);
3469 * Following are the conditional operators. They compare the TOS against
3470 * the primary and put a literal 1 in the primary if the condition is
3471 * true, otherwise they clear the primary register
3476 void g_eq (unsigned flags, unsigned long val)
3477 /* Test for equal */
3479 static const char* ops[12] = {
3480 "toseqax", "toseqax", "toseqeax", "toseqeax",
3485 /* If the right hand side is const, the lhs is not on stack but still
3486 * in the primary register.
3488 if (flags & CF_CONST) {
3490 switch (flags & CF_TYPE) {
3493 if (flags & CF_FORCECHAR) {
3494 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3495 AddCodeLine ("jsr booleq");
3501 L = GetLocalLabel();
3502 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3503 AddCodeLine ("bne %s", LocalLabelName (L));
3504 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3506 AddCodeLine ("jsr booleq");
3516 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3517 * into the normal, non-optimized stuff. Note: The standard stuff will
3518 * always work with ints.
3520 flags &= ~CF_FORCECHAR;
3521 g_push (flags & ~CF_CONST, 0);
3524 /* Use long way over the stack */
3525 oper (flags, val, ops);
3530 void g_ne (unsigned flags, unsigned long val)
3531 /* Test for not equal */
3533 static const char* ops[12] = {
3534 "tosneax", "tosneax", "tosneeax", "tosneeax",
3539 /* If the right hand side is const, the lhs is not on stack but still
3540 * in the primary register.
3542 if (flags & CF_CONST) {
3544 switch (flags & CF_TYPE) {
3547 if (flags & CF_FORCECHAR) {
3548 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3549 AddCodeLine ("jsr boolne");
3555 L = GetLocalLabel();
3556 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3557 AddCodeLine ("bne %s", LocalLabelName (L));
3558 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3560 AddCodeLine ("jsr boolne");
3570 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3571 * into the normal, non-optimized stuff. Note: The standard stuff will
3572 * always work with ints.
3574 flags &= ~CF_FORCECHAR;
3575 g_push (flags & ~CF_CONST, 0);
3578 /* Use long way over the stack */
3579 oper (flags, val, ops);
3584 void g_lt (unsigned flags, unsigned long val)
3585 /* Test for less than */
3587 static const char* ops[12] = {
3588 "tosltax", "tosultax", "toslteax", "tosulteax",
3593 /* If the right hand side is const, the lhs is not on stack but still
3594 * in the primary register.
3596 if (flags & CF_CONST) {
3598 /* Because the handling of the overflow flag is too complex for
3599 * inlining, we can handle only unsigned compares, and signed
3600 * compares against zero here.
3602 if (flags & CF_UNSIGNED) {
3604 /* Give a warning in some special cases */
3606 Warning ("Condition is never true");
3607 AddCodeLine ("jsr return0");
3611 /* Look at the type */
3612 switch (flags & CF_TYPE) {
3615 if (flags & CF_FORCECHAR) {
3616 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3617 AddCodeLine ("jsr boolult");
3623 /* If the low byte is zero, we must only test the high byte */
3624 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3625 if ((val & 0xFF) != 0) {
3626 unsigned L = GetLocalLabel();
3627 AddCodeLine ("bne %s", LocalLabelName (L));
3628 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3631 AddCodeLine ("jsr boolult");
3635 /* Do a subtraction */
3636 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3637 AddCodeLine ("txa");
3638 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3639 AddCodeLine ("lda sreg");
3640 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3641 AddCodeLine ("lda sreg+1");
3642 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3643 AddCodeLine ("jsr boolult");
3650 } else if (val == 0) {
3652 /* A signed compare against zero must only look at the sign bit */
3653 switch (flags & CF_TYPE) {
3656 if (flags & CF_FORCECHAR) {
3657 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3658 AddCodeLine ("lda #$00");
3659 AddCodeLine ("ldx #$00");
3660 AddCodeLine ("rol a");
3666 /* Just check the high byte */
3667 AddCodeLine ("cpx #$80"); /* Bit 7 -> carry */
3668 AddCodeLine ("lda #$00");
3669 AddCodeLine ("ldx #$00");
3670 AddCodeLine ("rol a");
3674 /* Just check the high byte */
3675 AddCodeLine ("lda sreg+1");
3676 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3677 AddCodeLine ("lda #$00");
3678 AddCodeLine ("ldx #$00");
3679 AddCodeLine ("rol a");
3688 /* Signed compare against a constant != zero */
3689 switch (flags & CF_TYPE) {
3692 if (flags & CF_FORCECHAR) {
3693 Label = GetLocalLabel ();
3694 AddCodeLine ("sec");
3695 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3696 AddCodeLine ("bvc %s", LocalLabelName (Label));
3697 AddCodeLine ("eor #$80");
3698 g_defcodelabel (Label);
3699 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3700 AddCodeLine ("lda #$00");
3701 AddCodeLine ("ldx #$00");
3702 AddCodeLine ("rol a");
3708 /* Do a subtraction */
3709 Label = GetLocalLabel ();
3710 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3711 AddCodeLine ("txa");
3712 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3713 AddCodeLine ("bvc %s", LocalLabelName (Label));
3714 AddCodeLine ("eor #$80");
3715 g_defcodelabel (Label);
3716 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3717 AddCodeLine ("lda #$00");
3718 AddCodeLine ("ldx #$00");
3719 AddCodeLine ("rol a");
3723 /* This one is too costly */
3732 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3733 * into the normal, non-optimized stuff. Note: The standard stuff will
3734 * always work with ints.
3736 flags &= ~CF_FORCECHAR;
3737 g_push (flags & ~CF_CONST, 0);
3740 /* Use long way over the stack */
3741 oper (flags, val, ops);
3746 void g_le (unsigned flags, unsigned long val)
3747 /* Test for less than or equal to */
3749 static const char* ops[12] = {
3750 "tosleax", "tosuleax", "tosleeax", "tosuleeax",
3754 /* If the right hand side is const, the lhs is not on stack but still
3755 * in the primary register.
3757 if (flags & CF_CONST) {
3759 /* Look at the type */
3760 switch (flags & CF_TYPE) {
3763 if (flags & CF_FORCECHAR) {
3764 if (flags & CF_UNSIGNED) {
3765 /* Unsigned compare */
3767 /* Use < instead of <= because the former gives
3768 * better code on the 6502 than the latter.
3770 g_lt (flags, val+1);
3773 Warning ("Condition is always true");
3774 AddCodeLine ("jsr return1");
3777 /* Signed compare */
3778 if ((long) val < 0x7F) {
3779 /* Use < instead of <= because the former gives
3780 * better code on the 6502 than the latter.
3782 g_lt (flags, val+1);
3785 Warning ("Condition is always true");
3786 AddCodeLine ("jsr return1");
3794 if (flags & CF_UNSIGNED) {
3795 /* Unsigned compare */
3797 /* Use < instead of <= because the former gives
3798 * better code on the 6502 than the latter.
3800 g_lt (flags, val+1);
3803 Warning ("Condition is always true");
3804 AddCodeLine ("jsr return1");
3807 /* Signed compare */
3808 if ((long) val < 0x7FFF) {
3809 g_lt (flags, val+1);
3812 Warning ("Condition is always true");
3813 AddCodeLine ("jsr return1");
3819 if (flags & CF_UNSIGNED) {
3820 /* Unsigned compare */
3821 if (val < 0xFFFFFFFF) {
3822 /* Use < instead of <= because the former gives
3823 * better code on the 6502 than the latter.
3825 g_lt (flags, val+1);
3828 Warning ("Condition is always true");
3829 AddCodeLine ("jsr return1");
3832 /* Signed compare */
3833 if ((long) val < 0x7FFFFFFF) {
3834 g_lt (flags, val+1);
3837 Warning ("Condition is always true");
3838 AddCodeLine ("jsr return1");
3847 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3848 * into the normal, non-optimized stuff. Note: The standard stuff will
3849 * always work with ints.
3851 flags &= ~CF_FORCECHAR;
3852 g_push (flags & ~CF_CONST, 0);
3855 /* Use long way over the stack */
3856 oper (flags, val, ops);
3861 void g_gt (unsigned flags, unsigned long val)
3862 /* Test for greater than */
3864 static const char* ops[12] = {
3865 "tosgtax", "tosugtax", "tosgteax", "tosugteax",
3869 /* If the right hand side is const, the lhs is not on stack but still
3870 * in the primary register.
3872 if (flags & CF_CONST) {
3874 /* Look at the type */
3875 switch (flags & CF_TYPE) {
3878 if (flags & CF_FORCECHAR) {
3879 if (flags & CF_UNSIGNED) {
3881 /* If we have a compare > 0, we will replace it by
3882 * != 0 here, since both are identical but the
3883 * latter is easier to optimize.
3886 } else if (val < 0xFF) {
3887 /* Use >= instead of > because the former gives
3888 * better code on the 6502 than the latter.
3890 g_ge (flags, val+1);
3893 Warning ("Condition is never true");
3894 AddCodeLine ("jsr return0");
3897 if ((long) val < 0x7F) {
3898 /* Use >= instead of > because the former gives
3899 * better code on the 6502 than the latter.
3901 g_ge (flags, val+1);
3904 Warning ("Condition is never true");
3905 AddCodeLine ("jsr return0");
3913 if (flags & CF_UNSIGNED) {
3914 /* Unsigned compare */
3916 /* If we have a compare > 0, we will replace it by
3917 * != 0 here, since both are identical but the latter
3918 * is easier to optimize.
3921 } else if (val < 0xFFFF) {
3922 /* Use >= instead of > because the former gives better
3923 * code on the 6502 than the latter.
3925 g_ge (flags, val+1);
3928 Warning ("Condition is never true");
3929 AddCodeLine ("jsr return0");
3932 /* Signed compare */
3933 if ((long) val < 0x7FFF) {
3934 g_ge (flags, val+1);
3937 Warning ("Condition is never true");
3938 AddCodeLine ("jsr return0");
3944 if (flags & CF_UNSIGNED) {
3945 /* Unsigned compare */
3947 /* If we have a compare > 0, we will replace it by
3948 * != 0 here, since both are identical but the latter
3949 * is easier to optimize.
3952 } else if (val < 0xFFFFFFFF) {
3953 /* Use >= instead of > because the former gives better
3954 * code on the 6502 than the latter.
3956 g_ge (flags, val+1);
3959 Warning ("Condition is never true");
3960 AddCodeLine ("jsr return0");
3963 /* Signed compare */
3964 if ((long) val < 0x7FFFFFFF) {
3965 g_ge (flags, val+1);
3968 Warning ("Condition is never true");
3969 AddCodeLine ("jsr return0");
3978 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3979 * into the normal, non-optimized stuff. Note: The standard stuff will
3980 * always work with ints.
3982 flags &= ~CF_FORCECHAR;
3983 g_push (flags & ~CF_CONST, 0);
3986 /* Use long way over the stack */
3987 oper (flags, val, ops);
3992 void g_ge (unsigned flags, unsigned long val)
3993 /* Test for greater than or equal to */
3995 static const char* ops[12] = {
3996 "tosgeax", "tosugeax", "tosgeeax", "tosugeeax",
4002 /* If the right hand side is const, the lhs is not on stack but still
4003 * in the primary register.
4005 if (flags & CF_CONST) {
4007 /* Because the handling of the overflow flag is too complex for
4008 * inlining, we can handle only unsigned compares, and signed
4009 * compares against zero here.
4011 if (flags & CF_UNSIGNED) {
4013 /* Give a warning in some special cases */
4015 Warning ("Condition is always true");
4016 AddCodeLine ("jsr return1");
4020 /* Look at the type */
4021 switch (flags & CF_TYPE) {
4024 if (flags & CF_FORCECHAR) {
4025 /* Do a subtraction. Condition is true if carry set */
4026 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4027 AddCodeLine ("lda #$00");
4028 AddCodeLine ("ldx #$00");
4029 AddCodeLine ("rol a");
4035 /* Do a subtraction. Condition is true if carry set */
4036 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4037 AddCodeLine ("txa");
4038 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4039 AddCodeLine ("lda #$00");
4040 AddCodeLine ("ldx #$00");
4041 AddCodeLine ("rol a");
4045 /* Do a subtraction. Condition is true if carry set */
4046 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4047 AddCodeLine ("txa");
4048 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4049 AddCodeLine ("lda sreg");
4050 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
4051 AddCodeLine ("lda sreg+1");
4052 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
4053 AddCodeLine ("lda #$00");
4054 AddCodeLine ("ldx #$00");
4055 AddCodeLine ("rol a");
4062 } else if (val == 0) {
4064 /* A signed compare against zero must only look at the sign bit */
4065 switch (flags & CF_TYPE) {
4068 if (flags & CF_FORCECHAR) {
4069 AddCodeLine ("tax");
4070 AddCodeLine ("jsr boolge");
4076 /* Just test the high byte */
4077 AddCodeLine ("txa");
4078 AddCodeLine ("jsr boolge");
4082 /* Just test the high byte */
4083 AddCodeLine ("lda sreg+1");
4084 AddCodeLine ("jsr boolge");
4093 /* Signed compare against a constant != zero */
4094 switch (flags & CF_TYPE) {
4097 if (flags & CF_FORCECHAR) {
4098 Label = GetLocalLabel ();
4099 AddCodeLine ("sec");
4100 AddCodeLine ("sbc #$%02X", (unsigned char)val);
4101 AddCodeLine ("bvs %s", LocalLabelName (Label));
4102 AddCodeLine ("eor #$80");
4103 g_defcodelabel (Label);
4104 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4105 AddCodeLine ("lda #$00");
4106 AddCodeLine ("ldx #$00");
4107 AddCodeLine ("rol a");
4113 /* Do a subtraction */
4114 Label = GetLocalLabel ();
4115 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4116 AddCodeLine ("txa");
4117 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4118 AddCodeLine ("bvs %s", LocalLabelName (Label));
4119 AddCodeLine ("eor #$80");
4120 g_defcodelabel (Label);
4121 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4122 AddCodeLine ("lda #$00");
4123 AddCodeLine ("ldx #$00");
4124 AddCodeLine ("rol a");
4128 /* This one is too costly */
4136 /* If we go here, we didn't emit code. Push the lhs on stack and fall
4137 * into the normal, non-optimized stuff. Note: The standard stuff will
4138 * always work with ints.
4140 flags &= ~CF_FORCECHAR;
4141 g_push (flags & ~CF_CONST, 0);
4144 /* Use long way over the stack */
4145 oper (flags, val, ops);
4150 /*****************************************************************************/
4151 /* Allocating static storage */
4152 /*****************************************************************************/
4156 void g_res (unsigned n)
4157 /* Reserve static storage, n bytes */
4159 AddDataLine ("\t.res\t%u,$00", n);
4164 void g_defdata (unsigned flags, unsigned long val, long offs)
4165 /* Define data with the size given in flags */
4167 if (flags & CF_CONST) {
4169 /* Numeric constant */
4170 switch (flags & CF_TYPE) {
4173 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
4177 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
4181 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
4192 /* Create the correct label name */
4193 const char* Label = GetLabelName (flags, val, offs);
4195 /* Labels are always 16 bit */
4196 AddDataLine ("\t.addr\t%s", Label);
4203 void g_defbytes (const void* Bytes, unsigned Count)
4204 /* Output a row of bytes as a constant */
4210 /* Cast the buffer pointer */
4211 const unsigned char* Data = (const unsigned char*) Bytes;
4213 /* Output the stuff */
4216 /* How many go into this line? */
4217 if ((Chunk = Count) > 16) {
4222 /* Output one line */
4223 strcpy (Buf, "\t.byte\t");
4226 B += sprintf (B, "$%02X", *Data++);
4232 /* Output the line */
4233 AddDataLine ("%s", Buf);
4239 void g_zerobytes (unsigned Count)
4240 /* Output Count bytes of data initialized with zero */
4243 AddDataLine ("\t.res\t%u,$00", Count);
4249 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4250 /* Initialize a register variable from static initialization data */
4252 /* Register variables do always have less than 128 bytes */
4253 unsigned CodeLabel = GetLocalLabel ();
4255 g_defcodelabel (CodeLabel);
4256 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4257 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4258 AddCodeLine ("dex");
4259 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4264 void g_initauto (unsigned Label, unsigned Size)
4265 /* Initialize a local variable at stack offset zero from static data */
4267 unsigned CodeLabel = GetLocalLabel ();
4269 CheckLocalOffs (Size);
4272 g_defcodelabel (CodeLabel);
4273 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4274 AddCodeLine ("sta (sp),y");
4275 AddCodeLine ("dey");
4276 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4277 } else if (Size <= 256) {
4279 g_defcodelabel (CodeLabel);
4280 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4281 AddCodeLine ("sta (sp),y");
4282 AddCodeLine ("iny");
4283 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4284 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4290 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4291 /* Initialize a static local variable from static initialization data */
4294 unsigned CodeLabel = GetLocalLabel ();
4296 g_defcodelabel (CodeLabel);
4297 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4298 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4299 AddCodeLine ("dey");
4300 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4301 } else if (Size <= 256) {
4302 unsigned CodeLabel = GetLocalLabel ();
4304 g_defcodelabel (CodeLabel);
4305 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4306 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4307 AddCodeLine ("iny");
4308 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4309 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4311 /* Use the easy way here: memcpy */
4312 g_getimmed (CF_STATIC, VarLabel, 0);
4313 AddCodeLine ("jsr pushax");
4314 g_getimmed (CF_STATIC, InitLabel, 0);
4315 AddCodeLine ("jsr pushax");
4316 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4317 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4323 /*****************************************************************************/
4324 /* Switch statement */
4325 /*****************************************************************************/
4329 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4330 /* Generate code for a switch statement */
4332 unsigned NextLabel = 0;
4335 /* Setup registers and determine which compare insn to use */
4336 const char* Compare;
4339 Compare = "cmp #$%02X";
4342 Compare = "cpx #$%02X";
4345 AddCodeLine ("ldy sreg");
4346 Compare = "cpy #$%02X";
4349 AddCodeLine ("ldy sreg+1");
4350 Compare = "cpy #$%02X";
4353 Internal ("Invalid depth in g_switch: %u", Depth);
4356 /* Walk over all nodes */
4357 for (I = 0; I < CollCount (Nodes); ++I) {
4359 /* Get the next case node */
4360 CaseNode* N = CollAtUnchecked (Nodes, I);
4362 /* If we have a next label, define it */
4364 g_defcodelabel (NextLabel);
4368 /* Do the compare */
4369 AddCodeLine (Compare, CN_GetValue (N));
4371 /* If this is the last level, jump directly to the case code if found */
4374 /* Branch if equal */
4375 g_falsejump (0, CN_GetLabel (N));
4379 /* Determine the next label */
4380 if (I == CollCount (Nodes) - 1) {
4381 /* Last node means not found */
4382 g_truejump (0, DefaultLabel);
4384 /* Jump to the next check */
4385 NextLabel = GetLocalLabel ();
4386 g_truejump (0, NextLabel);
4389 /* Check the next level */
4390 g_switch (N->Nodes, DefaultLabel, Depth-1);
4395 /* If we go here, we haven't found the label */
4396 g_jump (DefaultLabel);
4401 /*****************************************************************************/
4402 /* User supplied assembler code */
4403 /*****************************************************************************/
4407 void g_asmcode (struct StrBuf* B)
4408 /* Output one line of assembler code. */
4410 AddCodeLine ("%.*s", (int) SB_GetLen (B), SB_GetConstBuf (B));