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 /* Get low and high byte */
933 unsigned char Lo = (unsigned char) Offs;
934 unsigned char Hi = (unsigned char) (Offs >> 8);
936 /* Calculate the offset relative to sp */
942 AddCodeLine ("lda sp");
943 AddCodeLine ("ldx sp+1");
948 AddCodeLine ("lda sp+1");
950 AddCodeLine ("adc #$%02X", Hi);
952 AddCodeLine ("lda sp");
954 } else if (Hi == 0) {
956 if (IS_Get (&CodeSizeFactor) < 200) {
957 /* 8 bit offset with subroutine call */
958 AddCodeLine ("lda #$%02X", Lo);
959 AddCodeLine ("jsr leaa0sp");
961 /* 8 bit offset inlined */
962 unsigned L = GetLocalLabel ();
963 AddCodeLine ("lda sp");
964 AddCodeLine ("ldx sp+1");
966 AddCodeLine ("adc #$%02X", Lo);
967 AddCodeLine ("bcc %s", LocalLabelName (L));
971 } else if (IS_Get (&CodeSizeFactor) < 170) {
972 /* Full 16 bit offset with subroutine call */
973 AddCodeLine ("lda #$%02X", Lo);
974 AddCodeLine ("ldx #$%02X", Hi);
975 AddCodeLine ("jsr leaaxsp");
977 /* Full 16 bit offset inlined */
978 AddCodeLine ("lda sp");
980 AddCodeLine ("adc #$%02X", (unsigned char) Offs);
982 AddCodeLine ("lda sp+1");
983 AddCodeLine ("adc #$%02X", (unsigned char) (Offs >> 8));
991 void g_leavariadic (int Offs)
992 /* Fetch the address of a parameter in a variadic function into the primary
996 unsigned ArgSizeOffs;
998 /* Calculate the offset relative to sp */
1001 /* Get the offset of the parameter which is stored at sp+0 on function
1002 * entry and check if this offset is reachable with a byte offset.
1004 CHECK (StackPtr <= 0);
1005 ArgSizeOffs = -StackPtr;
1006 CheckLocalOffs (ArgSizeOffs);
1008 /* Get the size of all parameters. */
1009 ldyconst (ArgSizeOffs);
1010 AddCodeLine ("lda (sp),y");
1012 /* Add the value of the stackpointer */
1013 if (IS_Get (&CodeSizeFactor) > 250) {
1014 unsigned L = GetLocalLabel();
1015 AddCodeLine ("ldx sp+1");
1016 AddCodeLine ("clc");
1017 AddCodeLine ("adc sp");
1018 AddCodeLine ("bcc %s", LocalLabelName (L));
1019 AddCodeLine ("inx");
1022 AddCodeLine ("ldx #$00");
1023 AddCodeLine ("jsr leaaxsp");
1026 /* Add the offset to the primary */
1028 g_inc (CF_INT | CF_CONST, Offs);
1029 } else if (Offs < 0) {
1030 g_dec (CF_INT | CF_CONST, -Offs);
1036 /*****************************************************************************/
1037 /* Store into memory */
1038 /*****************************************************************************/
1042 void g_putstatic (unsigned flags, unsigned long label, long offs)
1043 /* Store the primary register into the specified static memory cell */
1045 /* Create the correct label name */
1046 const char* lbuf = GetLabelName (flags, label, offs);
1048 /* Check the size and generate the correct store operation */
1049 switch (flags & CF_TYPE) {
1052 AddCodeLine ("sta %s", lbuf);
1056 AddCodeLine ("sta %s", lbuf);
1057 AddCodeLine ("stx %s+1", lbuf);
1061 AddCodeLine ("sta %s", lbuf);
1062 AddCodeLine ("stx %s+1", lbuf);
1063 AddCodeLine ("ldy sreg");
1064 AddCodeLine ("sty %s+2", lbuf);
1065 AddCodeLine ("ldy sreg+1");
1066 AddCodeLine ("sty %s+3", lbuf);
1077 void g_putlocal (unsigned Flags, int Offs, long Val)
1078 /* Put data into local object. */
1081 CheckLocalOffs (Offs);
1082 switch (Flags & CF_TYPE) {
1085 if (Flags & CF_CONST) {
1086 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1089 AddCodeLine ("sta (sp),y");
1093 if (Flags & CF_CONST) {
1095 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1096 AddCodeLine ("sta (sp),y");
1097 if ((Flags & CF_NOKEEP) == 0) {
1098 /* Place high byte into X */
1099 AddCodeLine ("tax");
1101 if ((Val & 0xFF) == Offs+1) {
1102 /* The value we need is already in Y */
1103 AddCodeLine ("tya");
1104 AddCodeLine ("dey");
1106 AddCodeLine ("dey");
1107 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1109 AddCodeLine ("sta (sp),y");
1111 if ((Flags & CF_NOKEEP) == 0 || IS_Get (&CodeSizeFactor) < 160) {
1113 AddCodeLine ("jsr staxysp");
1116 AddCodeLine ("sta (sp),y");
1117 AddCodeLine ("iny");
1118 AddCodeLine ("txa");
1119 AddCodeLine ("sta (sp),y");
1125 if (Flags & CF_CONST) {
1126 g_getimmed (Flags, Val, 0);
1129 AddCodeLine ("jsr steaxysp");
1140 void g_putind (unsigned Flags, unsigned Offs)
1141 /* Store the specified object type in the primary register at the address
1142 * on the top of the stack
1145 /* We can handle offsets below $100 directly, larger offsets must be added
1146 * to the address. Since a/x is in use, best code is achieved by adding
1147 * just the high byte. Be sure to check if the low byte will overflow while
1150 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1152 /* Overflow - we need to add the low byte also */
1153 AddCodeLine ("ldy #$00");
1154 AddCodeLine ("clc");
1155 AddCodeLine ("pha");
1156 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1157 AddCodeLine ("adc (sp),y");
1158 AddCodeLine ("sta (sp),y");
1159 AddCodeLine ("iny");
1160 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1161 AddCodeLine ("adc (sp),y");
1162 AddCodeLine ("sta (sp),y");
1163 AddCodeLine ("pla");
1165 /* Complete address is on stack, new offset is zero */
1168 } else if ((Offs & 0xFF00) != 0) {
1170 /* We can just add the high byte */
1171 AddCodeLine ("ldy #$01");
1172 AddCodeLine ("clc");
1173 AddCodeLine ("pha");
1174 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1175 AddCodeLine ("adc (sp),y");
1176 AddCodeLine ("sta (sp),y");
1177 AddCodeLine ("pla");
1179 /* Offset is now just the low byte */
1183 /* Check the size and determine operation */
1184 switch (Flags & CF_TYPE) {
1188 AddCodeLine ("jsr staspidx");
1193 AddCodeLine ("jsr staxspidx");
1198 AddCodeLine ("jsr steaxspidx");
1206 /* Pop the argument which is always a pointer */
1212 /*****************************************************************************/
1213 /* type conversion and similiar stuff */
1214 /*****************************************************************************/
1218 void g_toslong (unsigned flags)
1219 /* Make sure, the value on TOS is a long. Convert if necessary */
1221 switch (flags & CF_TYPE) {
1225 if (flags & CF_UNSIGNED) {
1226 AddCodeLine ("jsr tosulong");
1228 AddCodeLine ("jsr toslong");
1243 void g_tosint (unsigned flags)
1244 /* Make sure, the value on TOS is an int. Convert if necessary */
1246 switch (flags & CF_TYPE) {
1253 AddCodeLine ("jsr tosint");
1264 void g_regint (unsigned Flags)
1265 /* Make sure, the value in the primary register an int. Convert if necessary */
1269 switch (Flags & CF_TYPE) {
1272 if (Flags & CF_FORCECHAR) {
1273 /* Conversion is from char */
1274 if (Flags & CF_UNSIGNED) {
1275 AddCodeLine ("ldx #$00");
1277 L = GetLocalLabel();
1278 AddCodeLine ("ldx #$00");
1279 AddCodeLine ("cmp #$80");
1280 AddCodeLine ("bcc %s", LocalLabelName (L));
1281 AddCodeLine ("dex");
1298 void g_reglong (unsigned Flags)
1299 /* Make sure, the value in the primary register a long. Convert if necessary */
1303 switch (Flags & CF_TYPE) {
1306 if (Flags & CF_FORCECHAR) {
1307 /* Conversion is from char */
1308 if (Flags & CF_UNSIGNED) {
1309 if (IS_Get (&CodeSizeFactor) >= 200) {
1310 AddCodeLine ("ldx #$00");
1311 AddCodeLine ("stx sreg");
1312 AddCodeLine ("stx sreg+1");
1314 AddCodeLine ("jsr aulong");
1317 if (IS_Get (&CodeSizeFactor) >= 366) {
1318 L = GetLocalLabel();
1319 AddCodeLine ("ldx #$00");
1320 AddCodeLine ("cmp #$80");
1321 AddCodeLine ("bcc %s", LocalLabelName (L));
1322 AddCodeLine ("dex");
1324 AddCodeLine ("stx sreg");
1325 AddCodeLine ("stx sreg+1");
1327 AddCodeLine ("jsr along");
1334 if (Flags & CF_UNSIGNED) {
1335 if (IS_Get (&CodeSizeFactor) >= 200) {
1337 AddCodeLine ("sty sreg");
1338 AddCodeLine ("sty sreg+1");
1340 AddCodeLine ("jsr axulong");
1343 AddCodeLine ("jsr axlong");
1357 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1358 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1359 * value, that corresponds to the value on TOS, rhs corresponds to the value
1360 * in (e)ax. The return value is the the flags value for the resulting type.
1363 unsigned ltype, rtype;
1366 /* Get the type spec from the flags */
1367 ltype = lhs & CF_TYPE;
1368 rtype = rhs & CF_TYPE;
1370 /* Check if a conversion is needed */
1371 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1372 /* We must promote the primary register to long */
1374 /* Get the new rhs type */
1375 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1377 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1378 /* We must promote the lhs to long */
1384 /* Get the new rhs type */
1385 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1389 /* Determine the result type for the operation:
1390 * - The result is const if both operands are const.
1391 * - The result is unsigned if one of the operands is unsigned.
1392 * - The result is long if one of the operands is long.
1393 * - Otherwise the result is int sized.
1395 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1396 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1397 if (rtype == CF_LONG || ltype == CF_LONG) {
1407 unsigned g_typecast (unsigned lhs, unsigned rhs)
1408 /* Cast the value in the primary register to the operand size that is flagged
1409 * by the lhs value. Return the result value.
1412 unsigned ltype, rtype;
1414 /* Get the type spec from the flags */
1415 ltype = lhs & CF_TYPE;
1416 rtype = rhs & CF_TYPE;
1418 /* Check if a conversion is needed */
1419 if ((rhs & CF_CONST) == 0) {
1420 if (ltype == CF_LONG && rtype != CF_LONG) {
1421 /* We must promote the primary register to long */
1423 } else if (ltype == CF_INT && rtype != CF_INT) {
1424 /* We must promote the primary register to int */
1429 /* Do not need any other action. If the left type is int, and the primary
1430 * register is long, it will be automagically truncated. If the right hand
1431 * side is const, it is not located in the primary register and handled by
1432 * the expression parser code.
1435 /* Result is const if the right hand side was const */
1436 lhs |= (rhs & CF_CONST);
1438 /* The resulting type is that of the left hand side (that's why you called
1446 void g_scale (unsigned flags, long val)
1447 /* Scale the value in the primary register by the given value. If val is positive,
1448 * scale up, is val is negative, scale down. This function is used to scale
1449 * the operands or results of pointer arithmetic by the size of the type, the
1450 * pointer points to.
1455 /* Value may not be zero */
1457 Internal ("Data type has no size");
1458 } else if (val > 0) {
1461 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1463 /* Factor is 2, 4, 8 and 16, use special function */
1464 switch (flags & CF_TYPE) {
1467 if (flags & CF_FORCECHAR) {
1469 AddCodeLine ("asl a");
1476 if (flags & CF_UNSIGNED) {
1477 AddCodeLine ("jsr shlax%d", p2);
1479 AddCodeLine ("jsr aslax%d", p2);
1484 if (flags & CF_UNSIGNED) {
1485 AddCodeLine ("jsr shleax%d", p2);
1487 AddCodeLine ("jsr asleax%d", p2);
1496 } else if (val != 1) {
1498 /* Use a multiplication instead */
1499 g_mul (flags | CF_CONST, val);
1507 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1509 /* Factor is 2, 4, 8 and 16 use special function */
1510 switch (flags & CF_TYPE) {
1513 if (flags & CF_FORCECHAR) {
1514 if (flags & CF_UNSIGNED) {
1516 AddCodeLine ("lsr a");
1519 } else if (p2 <= 2) {
1520 AddCodeLine ("cmp #$80");
1521 AddCodeLine ("ror a");
1528 if (flags & CF_UNSIGNED) {
1529 AddCodeLine ("jsr lsrax%d", p2);
1531 AddCodeLine ("jsr asrax%d", p2);
1536 if (flags & CF_UNSIGNED) {
1537 AddCodeLine ("jsr lsreax%d", p2);
1539 AddCodeLine ("jsr asreax%d", p2);
1548 } else if (val != 1) {
1550 /* Use a division instead */
1551 g_div (flags | CF_CONST, val);
1559 /*****************************************************************************/
1560 /* Adds and subs of variables fix a fixed address */
1561 /*****************************************************************************/
1565 void g_addlocal (unsigned flags, int offs)
1566 /* Add a local variable to ax */
1570 /* Correct the offset and check it */
1572 CheckLocalOffs (offs);
1574 switch (flags & CF_TYPE) {
1577 L = GetLocalLabel();
1578 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1579 AddCodeLine ("clc");
1580 AddCodeLine ("adc (sp),y");
1581 AddCodeLine ("bcc %s", LocalLabelName (L));
1582 AddCodeLine ("inx");
1587 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1588 AddCodeLine ("clc");
1589 AddCodeLine ("adc (sp),y");
1590 AddCodeLine ("pha");
1591 AddCodeLine ("txa");
1592 AddCodeLine ("iny");
1593 AddCodeLine ("adc (sp),y");
1594 AddCodeLine ("tax");
1595 AddCodeLine ("pla");
1599 /* Do it the old way */
1601 g_getlocal (flags, offs);
1613 void g_addstatic (unsigned flags, unsigned long label, long offs)
1614 /* Add a static variable to ax */
1618 /* Create the correct label name */
1619 const char* lbuf = GetLabelName (flags, label, offs);
1621 switch (flags & CF_TYPE) {
1624 L = GetLocalLabel();
1625 AddCodeLine ("clc");
1626 AddCodeLine ("adc %s", lbuf);
1627 AddCodeLine ("bcc %s", LocalLabelName (L));
1628 AddCodeLine ("inx");
1633 AddCodeLine ("clc");
1634 AddCodeLine ("adc %s", lbuf);
1635 AddCodeLine ("tay");
1636 AddCodeLine ("txa");
1637 AddCodeLine ("adc %s+1", lbuf);
1638 AddCodeLine ("tax");
1639 AddCodeLine ("tya");
1643 /* Do it the old way */
1645 g_getstatic (flags, label, offs);
1657 /*****************************************************************************/
1658 /* Special op= functions */
1659 /*****************************************************************************/
1663 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1665 /* Emit += for a static variable */
1667 /* Create the correct label name */
1668 const char* lbuf = GetLabelName (flags, label, offs);
1670 /* Check the size and determine operation */
1671 switch (flags & CF_TYPE) {
1674 if (flags & CF_FORCECHAR) {
1675 AddCodeLine ("ldx #$00");
1676 if (flags & CF_CONST) {
1678 AddCodeLine ("inc %s", lbuf);
1679 AddCodeLine ("lda %s", lbuf);
1681 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1682 AddCodeLine ("clc");
1683 AddCodeLine ("adc %s", lbuf);
1684 AddCodeLine ("sta %s", lbuf);
1687 AddCodeLine ("clc");
1688 AddCodeLine ("adc %s", lbuf);
1689 AddCodeLine ("sta %s", lbuf);
1691 if ((flags & CF_UNSIGNED) == 0) {
1692 unsigned L = GetLocalLabel();
1693 AddCodeLine ("bpl %s", LocalLabelName (L));
1694 AddCodeLine ("dex");
1702 if (flags & CF_CONST) {
1704 unsigned L = GetLocalLabel ();
1705 AddCodeLine ("inc %s", lbuf);
1706 AddCodeLine ("bne %s", LocalLabelName (L));
1707 AddCodeLine ("inc %s+1", lbuf);
1709 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1710 AddCodeLine ("ldx %s+1", lbuf);
1712 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1713 AddCodeLine ("clc");
1714 AddCodeLine ("adc %s", lbuf);
1715 AddCodeLine ("sta %s", lbuf);
1717 unsigned L = GetLocalLabel ();
1718 AddCodeLine ("bcc %s", LocalLabelName (L));
1719 AddCodeLine ("inc %s+1", lbuf);
1721 AddCodeLine ("ldx %s+1", lbuf);
1723 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1724 AddCodeLine ("adc %s+1", lbuf);
1725 AddCodeLine ("sta %s+1", lbuf);
1726 AddCodeLine ("tax");
1727 AddCodeLine ("lda %s", lbuf);
1731 AddCodeLine ("clc");
1732 AddCodeLine ("adc %s", lbuf);
1733 AddCodeLine ("sta %s", lbuf);
1734 AddCodeLine ("txa");
1735 AddCodeLine ("adc %s+1", lbuf);
1736 AddCodeLine ("sta %s+1", lbuf);
1737 AddCodeLine ("tax");
1738 AddCodeLine ("lda %s", lbuf);
1743 if (flags & CF_CONST) {
1745 AddCodeLine ("ldy #<(%s)", lbuf);
1746 AddCodeLine ("sty ptr1");
1747 AddCodeLine ("ldy #>(%s)", lbuf);
1749 AddCodeLine ("jsr laddeq1");
1751 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1752 AddCodeLine ("jsr laddeqa");
1755 g_getstatic (flags, label, offs);
1757 g_putstatic (flags, label, offs);
1760 AddCodeLine ("ldy #<(%s)", lbuf);
1761 AddCodeLine ("sty ptr1");
1762 AddCodeLine ("ldy #>(%s)", lbuf);
1763 AddCodeLine ("jsr laddeq");
1774 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1775 /* Emit += for a local variable */
1777 /* Calculate the true offset, check it, load it into Y */
1779 CheckLocalOffs (offs);
1781 /* Check the size and determine operation */
1782 switch (flags & CF_TYPE) {
1785 if (flags & CF_FORCECHAR) {
1787 AddCodeLine ("ldx #$00");
1788 if (flags & CF_CONST) {
1789 AddCodeLine ("clc");
1790 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1791 AddCodeLine ("adc (sp),y");
1792 AddCodeLine ("sta (sp),y");
1794 AddCodeLine ("clc");
1795 AddCodeLine ("adc (sp),y");
1796 AddCodeLine ("sta (sp),y");
1798 if ((flags & CF_UNSIGNED) == 0) {
1799 unsigned L = GetLocalLabel();
1800 AddCodeLine ("bpl %s", LocalLabelName (L));
1801 AddCodeLine ("dex");
1810 if (flags & CF_CONST) {
1811 if (IS_Get (&CodeSizeFactor) >= 400) {
1812 AddCodeLine ("clc");
1813 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1814 AddCodeLine ("adc (sp),y");
1815 AddCodeLine ("sta (sp),y");
1816 AddCodeLine ("iny");
1817 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1818 AddCodeLine ("adc (sp),y");
1819 AddCodeLine ("sta (sp),y");
1820 AddCodeLine ("tax");
1821 AddCodeLine ("dey");
1822 AddCodeLine ("lda (sp),y");
1824 g_getimmed (flags, val, 0);
1825 AddCodeLine ("jsr addeqysp");
1828 AddCodeLine ("jsr addeqysp");
1833 if (flags & CF_CONST) {
1834 g_getimmed (flags, val, 0);
1837 AddCodeLine ("jsr laddeqysp");
1847 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1848 /* Emit += for the location with address in ax */
1850 /* If the offset is too large for a byte register, add the high byte
1851 * of the offset to the primary. Beware: We need a special correction
1852 * if the offset in the low byte will overflow in the operation.
1854 offs = MakeByteOffs (flags, offs);
1856 /* Check the size and determine operation */
1857 switch (flags & CF_TYPE) {
1860 AddCodeLine ("sta ptr1");
1861 AddCodeLine ("stx ptr1+1");
1862 AddCodeLine ("ldy #$%02X", offs);
1863 AddCodeLine ("ldx #$00");
1864 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1865 AddCodeLine ("clc");
1866 AddCodeLine ("adc (ptr1),y");
1867 AddCodeLine ("sta (ptr1),y");
1871 if (IS_Get (&CodeSizeFactor) >= 200) {
1872 /* Lots of code, use only if size is not important */
1873 AddCodeLine ("sta ptr1");
1874 AddCodeLine ("stx ptr1+1");
1875 AddCodeLine ("ldy #$%02X", offs);
1876 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1877 AddCodeLine ("clc");
1878 AddCodeLine ("adc (ptr1),y");
1879 AddCodeLine ("sta (ptr1),y");
1880 AddCodeLine ("pha");
1881 AddCodeLine ("iny");
1882 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1883 AddCodeLine ("adc (ptr1),y");
1884 AddCodeLine ("sta (ptr1),y");
1885 AddCodeLine ("tax");
1886 AddCodeLine ("pla");
1892 AddCodeLine ("jsr pushax"); /* Push the address */
1893 push (CF_PTR); /* Correct the internal sp */
1894 g_getind (flags, offs); /* Fetch the value */
1895 g_inc (flags, val); /* Increment value in primary */
1896 g_putind (flags, offs); /* Store the value back */
1906 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1908 /* Emit -= for a static variable */
1910 /* Create the correct label name */
1911 const char* lbuf = GetLabelName (flags, label, offs);
1913 /* Check the size and determine operation */
1914 switch (flags & CF_TYPE) {
1917 if (flags & CF_FORCECHAR) {
1918 AddCodeLine ("ldx #$00");
1919 if (flags & CF_CONST) {
1921 AddCodeLine ("dec %s", lbuf);
1922 AddCodeLine ("lda %s", lbuf);
1924 AddCodeLine ("lda %s", lbuf);
1925 AddCodeLine ("sec");
1926 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1927 AddCodeLine ("sta %s", lbuf);
1930 AddCodeLine ("eor #$FF");
1931 AddCodeLine ("sec");
1932 AddCodeLine ("adc %s", lbuf);
1933 AddCodeLine ("sta %s", lbuf);
1935 if ((flags & CF_UNSIGNED) == 0) {
1936 unsigned L = GetLocalLabel();
1937 AddCodeLine ("bpl %s", LocalLabelName (L));
1938 AddCodeLine ("dex");
1946 if (flags & CF_CONST) {
1947 AddCodeLine ("lda %s", lbuf);
1948 AddCodeLine ("sec");
1949 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1950 AddCodeLine ("sta %s", lbuf);
1952 unsigned L = GetLocalLabel ();
1953 AddCodeLine ("bcs %s", LocalLabelName (L));
1954 AddCodeLine ("dec %s+1", lbuf);
1956 AddCodeLine ("ldx %s+1", lbuf);
1958 AddCodeLine ("lda %s+1", lbuf);
1959 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1960 AddCodeLine ("sta %s+1", lbuf);
1961 AddCodeLine ("tax");
1962 AddCodeLine ("lda %s", lbuf);
1965 AddCodeLine ("eor #$FF");
1966 AddCodeLine ("sec");
1967 AddCodeLine ("adc %s", lbuf);
1968 AddCodeLine ("sta %s", lbuf);
1969 AddCodeLine ("txa");
1970 AddCodeLine ("eor #$FF");
1971 AddCodeLine ("adc %s+1", lbuf);
1972 AddCodeLine ("sta %s+1", lbuf);
1973 AddCodeLine ("tax");
1974 AddCodeLine ("lda %s", lbuf);
1979 if (flags & CF_CONST) {
1981 AddCodeLine ("ldy #<(%s)", lbuf);
1982 AddCodeLine ("sty ptr1");
1983 AddCodeLine ("ldy #>(%s)", lbuf);
1984 AddCodeLine ("lda #$%02X", (unsigned char)val);
1985 AddCodeLine ("jsr lsubeqa");
1987 g_getstatic (flags, label, offs);
1989 g_putstatic (flags, label, offs);
1992 AddCodeLine ("ldy #<(%s)", lbuf);
1993 AddCodeLine ("sty ptr1");
1994 AddCodeLine ("ldy #>(%s)", lbuf);
1995 AddCodeLine ("jsr lsubeq");
2006 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
2007 /* Emit -= for a local variable */
2009 /* Calculate the true offset, check it, load it into Y */
2011 CheckLocalOffs (offs);
2013 /* Check the size and determine operation */
2014 switch (flags & CF_TYPE) {
2017 if (flags & CF_FORCECHAR) {
2019 AddCodeLine ("ldx #$00");
2020 if (flags & CF_CONST) {
2021 AddCodeLine ("lda (sp),y");
2022 AddCodeLine ("sec");
2023 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2025 AddCodeLine ("eor #$FF");
2026 AddCodeLine ("sec");
2027 AddCodeLine ("adc (sp),y");
2029 AddCodeLine ("sta (sp),y");
2030 if ((flags & CF_UNSIGNED) == 0) {
2031 unsigned L = GetLocalLabel();
2032 AddCodeLine ("bpl %s", LocalLabelName (L));
2033 AddCodeLine ("dex");
2041 if (flags & CF_CONST) {
2042 g_getimmed (flags, val, 0);
2045 AddCodeLine ("jsr subeqysp");
2049 if (flags & CF_CONST) {
2050 g_getimmed (flags, val, 0);
2053 AddCodeLine ("jsr lsubeqysp");
2063 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2064 /* Emit -= for the location with address in ax */
2066 /* If the offset is too large for a byte register, add the high byte
2067 * of the offset to the primary. Beware: We need a special correction
2068 * if the offset in the low byte will overflow in the operation.
2070 offs = MakeByteOffs (flags, offs);
2072 /* Check the size and determine operation */
2073 switch (flags & CF_TYPE) {
2076 AddCodeLine ("sta ptr1");
2077 AddCodeLine ("stx ptr1+1");
2078 AddCodeLine ("ldy #$%02X", offs);
2079 AddCodeLine ("ldx #$00");
2080 AddCodeLine ("lda (ptr1),y");
2081 AddCodeLine ("sec");
2082 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2083 AddCodeLine ("sta (ptr1),y");
2087 if (IS_Get (&CodeSizeFactor) >= 200) {
2088 /* Lots of code, use only if size is not important */
2089 AddCodeLine ("sta ptr1");
2090 AddCodeLine ("stx ptr1+1");
2091 AddCodeLine ("ldy #$%02X", offs);
2092 AddCodeLine ("lda (ptr1),y");
2093 AddCodeLine ("sec");
2094 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2095 AddCodeLine ("sta (ptr1),y");
2096 AddCodeLine ("pha");
2097 AddCodeLine ("iny");
2098 AddCodeLine ("lda (ptr1),y");
2099 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2100 AddCodeLine ("sta (ptr1),y");
2101 AddCodeLine ("tax");
2102 AddCodeLine ("pla");
2108 AddCodeLine ("jsr pushax"); /* Push the address */
2109 push (CF_PTR); /* Correct the internal sp */
2110 g_getind (flags, offs); /* Fetch the value */
2111 g_dec (flags, val); /* Increment value in primary */
2112 g_putind (flags, offs); /* Store the value back */
2122 /*****************************************************************************/
2123 /* Add a variable address to the value in ax */
2124 /*****************************************************************************/
2128 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2129 /* Add the address of a local variable to ax */
2133 /* Add the offset */
2136 /* We cannot address more then 256 bytes of locals anyway */
2137 L = GetLocalLabel();
2138 CheckLocalOffs (offs);
2139 AddCodeLine ("clc");
2140 AddCodeLine ("adc #$%02X", offs & 0xFF);
2141 /* Do also skip the CLC insn below */
2142 AddCodeLine ("bcc %s", LocalLabelName (L));
2143 AddCodeLine ("inx");
2146 /* Add the current stackpointer value */
2147 AddCodeLine ("clc");
2149 /* Label was used above */
2152 AddCodeLine ("adc sp");
2153 AddCodeLine ("tay");
2154 AddCodeLine ("txa");
2155 AddCodeLine ("adc sp+1");
2156 AddCodeLine ("tax");
2157 AddCodeLine ("tya");
2162 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2163 /* Add the address of a static variable to ax */
2165 /* Create the correct label name */
2166 const char* lbuf = GetLabelName (flags, label, offs);
2168 /* Add the address to the current ax value */
2169 AddCodeLine ("clc");
2170 AddCodeLine ("adc #<(%s)", lbuf);
2171 AddCodeLine ("tay");
2172 AddCodeLine ("txa");
2173 AddCodeLine ("adc #>(%s)", lbuf);
2174 AddCodeLine ("tax");
2175 AddCodeLine ("tya");
2180 /*****************************************************************************/
2182 /*****************************************************************************/
2186 void g_save (unsigned flags)
2187 /* Copy primary register to hold register. */
2189 /* Check the size and determine operation */
2190 switch (flags & CF_TYPE) {
2193 if (flags & CF_FORCECHAR) {
2194 AddCodeLine ("pha");
2200 AddCodeLine ("sta regsave");
2201 AddCodeLine ("stx regsave+1");
2205 AddCodeLine ("jsr saveeax");
2215 void g_restore (unsigned flags)
2216 /* Copy hold register to primary. */
2218 /* Check the size and determine operation */
2219 switch (flags & CF_TYPE) {
2222 if (flags & CF_FORCECHAR) {
2223 AddCodeLine ("pla");
2229 AddCodeLine ("lda regsave");
2230 AddCodeLine ("ldx regsave+1");
2234 AddCodeLine ("jsr resteax");
2244 void g_cmp (unsigned flags, unsigned long val)
2245 /* Immidiate compare. The primary register will not be changed, Z flag
2251 /* Check the size and determine operation */
2252 switch (flags & CF_TYPE) {
2255 if (flags & CF_FORCECHAR) {
2256 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2262 L = GetLocalLabel();
2263 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2264 AddCodeLine ("bne %s", LocalLabelName (L));
2265 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2270 Internal ("g_cmp: Long compares not implemented");
2280 static void oper (unsigned Flags, unsigned long Val, const char** Subs)
2281 /* Encode a binary operation. subs is a pointer to four strings:
2282 * 0 --> Operate on ints
2283 * 1 --> Operate on unsigneds
2284 * 2 --> Operate on longs
2285 * 3 --> Operate on unsigned longs
2288 /* Determine the offset into the array */
2289 if (Flags & CF_UNSIGNED) {
2292 if ((Flags & CF_TYPE) == CF_LONG) {
2296 /* Load the value if it is not already in the primary */
2297 if (Flags & CF_CONST) {
2299 g_getimmed (Flags, Val, 0);
2302 /* Output the operation */
2303 AddCodeLine ("jsr %s", *Subs);
2305 /* The operation will pop it's argument */
2311 void g_test (unsigned flags)
2312 /* Test the value in the primary and set the condition codes */
2314 switch (flags & CF_TYPE) {
2317 if (flags & CF_FORCECHAR) {
2318 AddCodeLine ("tax");
2324 AddCodeLine ("stx tmp1");
2325 AddCodeLine ("ora tmp1");
2329 if (flags & CF_UNSIGNED) {
2330 AddCodeLine ("jsr utsteax");
2332 AddCodeLine ("jsr tsteax");
2344 void g_push (unsigned flags, unsigned long val)
2345 /* Push the primary register or a constant value onto the stack */
2347 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2349 /* We have a constant 8 or 16 bit value */
2350 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2352 /* Handle as 8 bit value */
2354 AddCodeLine ("jsr pusha");
2358 /* Handle as 16 bit value */
2359 g_getimmed (flags, val, 0);
2360 AddCodeLine ("jsr pushax");
2365 /* Value is not 16 bit or not constant */
2366 if (flags & CF_CONST) {
2367 /* Constant 32 bit value, load into eax */
2368 g_getimmed (flags, val, 0);
2371 /* Push the primary register */
2372 switch (flags & CF_TYPE) {
2375 if (flags & CF_FORCECHAR) {
2376 /* Handle as char */
2377 AddCodeLine ("jsr pusha");
2382 AddCodeLine ("jsr pushax");
2386 AddCodeLine ("jsr pusheax");
2396 /* Adjust the stack offset */
2402 void g_swap (unsigned flags)
2403 /* Swap the primary register and the top of the stack. flags give the type
2404 * of *both* values (must have same size).
2407 switch (flags & CF_TYPE) {
2411 AddCodeLine ("jsr swapstk");
2415 AddCodeLine ("jsr swapestk");
2426 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2427 /* Call the specified subroutine name */
2429 if ((Flags & CF_FIXARGC) == 0) {
2430 /* Pass the argument count */
2433 AddCodeLine ("jsr _%s", Label);
2434 StackPtr += ArgSize; /* callee pops args */
2439 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2440 /* Call subroutine indirect */
2442 if ((Flags & CF_LOCAL) == 0) {
2443 /* Address is in a/x */
2444 if ((Flags & CF_FIXARGC) == 0) {
2445 /* Pass arg count */
2448 AddCodeLine ("jsr callax");
2450 /* The address is on stack, offset is on Val */
2452 CheckLocalOffs (Offs);
2453 AddCodeLine ("pha");
2454 AddCodeLine ("ldy #$%02X", Offs);
2455 AddCodeLine ("lda (sp),y");
2456 AddCodeLine ("sta jmpvec+1");
2457 AddCodeLine ("iny");
2458 AddCodeLine ("lda (sp),y");
2459 AddCodeLine ("sta jmpvec+2");
2460 AddCodeLine ("pla");
2461 AddCodeLine ("jsr jmpvec");
2464 /* Callee pops args */
2465 StackPtr += ArgSize;
2470 void g_jump (unsigned Label)
2471 /* Jump to specified internal label number */
2473 AddCodeLine ("jmp %s", LocalLabelName (Label));
2478 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2479 /* Jump to label if zero flag clear */
2481 AddCodeLine ("jne %s", LocalLabelName (label));
2486 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2487 /* Jump to label if zero flag set */
2489 AddCodeLine ("jeq %s", LocalLabelName (label));
2494 static void mod_internal (int k, char* verb1, char* verb2)
2497 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2501 AddCodeLine ("jsr %ssp", verb2);
2507 void g_space (int space)
2508 /* Create or drop space on the stack */
2511 mod_internal (-space, "inc", "addy");
2512 } else if (space > 0) {
2513 mod_internal (space, "dec", "suby");
2519 void g_cstackcheck (void)
2520 /* Check for a C stack overflow */
2522 AddCodeLine ("jsr cstkchk");
2527 void g_stackcheck (void)
2528 /* Check for a stack overflow */
2530 AddCodeLine ("jsr stkchk");
2535 void g_add (unsigned flags, unsigned long val)
2536 /* Primary = TOS + Primary */
2538 static const char* ops[12] = {
2539 "tosaddax", "tosaddax", "tosaddeax", "tosaddeax"
2542 if (flags & CF_CONST) {
2543 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2544 g_push (flags & ~CF_CONST, 0);
2546 oper (flags, val, ops);
2551 void g_sub (unsigned flags, unsigned long val)
2552 /* Primary = TOS - Primary */
2554 static const char* ops[12] = {
2555 "tossubax", "tossubax", "tossubeax", "tossubeax",
2558 if (flags & CF_CONST) {
2559 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2560 g_push (flags & ~CF_CONST, 0);
2562 oper (flags, val, ops);
2567 void g_rsub (unsigned flags, unsigned long val)
2568 /* Primary = Primary - TOS */
2570 static const char* ops[12] = {
2571 "tosrsubax", "tosrsubax", "tosrsubeax", "tosrsubeax",
2573 oper (flags, val, ops);
2578 void g_mul (unsigned flags, unsigned long val)
2579 /* Primary = TOS * Primary */
2581 static const char* ops[12] = {
2582 "tosmulax", "tosumulax", "tosmuleax", "tosumuleax",
2587 /* Do strength reduction if the value is constant and a power of two */
2588 if (flags & CF_CONST && (p2 = PowerOf2 (val)) >= 0) {
2589 /* Generate a shift instead */
2594 /* If the right hand side is const, the lhs is not on stack but still
2595 * in the primary register.
2597 if (flags & CF_CONST) {
2599 switch (flags & CF_TYPE) {
2602 if (flags & CF_FORCECHAR) {
2603 /* Handle some special cases */
2607 AddCodeLine ("sta tmp1");
2608 AddCodeLine ("asl a");
2609 AddCodeLine ("clc");
2610 AddCodeLine ("adc tmp1");
2614 AddCodeLine ("sta tmp1");
2615 AddCodeLine ("asl a");
2616 AddCodeLine ("asl a");
2617 AddCodeLine ("clc");
2618 AddCodeLine ("adc tmp1");
2622 AddCodeLine ("sta tmp1");
2623 AddCodeLine ("asl a");
2624 AddCodeLine ("clc");
2625 AddCodeLine ("adc tmp1");
2626 AddCodeLine ("asl a");
2630 AddCodeLine ("sta tmp1");
2631 AddCodeLine ("asl a");
2632 AddCodeLine ("asl a");
2633 AddCodeLine ("clc");
2634 AddCodeLine ("adc tmp1");
2635 AddCodeLine ("asl a");
2644 AddCodeLine ("jsr mulax3");
2647 AddCodeLine ("jsr mulax5");
2650 AddCodeLine ("jsr mulax6");
2653 AddCodeLine ("jsr mulax7");
2656 AddCodeLine ("jsr mulax9");
2659 AddCodeLine ("jsr mulax10");
2671 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2672 * into the normal, non-optimized stuff.
2674 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2675 g_push (flags & ~CF_CONST, 0);
2679 /* Use long way over the stack */
2680 oper (flags, val, ops);
2685 void g_div (unsigned flags, unsigned long val)
2686 /* Primary = TOS / Primary */
2688 static const char* ops[12] = {
2689 "tosdivax", "tosudivax", "tosdiveax", "tosudiveax",
2692 /* Do strength reduction if the value is constant and a power of two */
2694 if ((flags & CF_CONST) && (p2 = PowerOf2 (val)) >= 0) {
2695 /* Generate a shift instead */
2698 /* Generate a division */
2699 if (flags & CF_CONST) {
2700 /* lhs is not on stack */
2701 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2702 g_push (flags & ~CF_CONST, 0);
2704 oper (flags, val, ops);
2710 void g_mod (unsigned flags, unsigned long val)
2711 /* Primary = TOS % Primary */
2713 static const char* ops[12] = {
2714 "tosmodax", "tosumodax", "tosmodeax", "tosumodeax",
2718 /* Check if we can do some cost reduction */
2719 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = PowerOf2 (val)) >= 0) {
2720 /* We can do that with an AND operation */
2721 g_and (flags, val - 1);
2723 /* Do it the hard way... */
2724 if (flags & CF_CONST) {
2725 /* lhs is not on stack */
2726 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2727 g_push (flags & ~CF_CONST, 0);
2729 oper (flags, val, ops);
2735 void g_or (unsigned flags, unsigned long val)
2736 /* Primary = TOS | Primary */
2738 static const char* ops[12] = {
2739 "tosorax", "tosorax", "tosoreax", "tosoreax",
2742 /* If the right hand side is const, the lhs is not on stack but still
2743 * in the primary register.
2745 if (flags & CF_CONST) {
2747 switch (flags & CF_TYPE) {
2750 if (flags & CF_FORCECHAR) {
2751 if ((val & 0xFF) != 0) {
2752 AddCodeLine ("ora #$%02X", (unsigned char)val);
2760 if ((val & 0xFF) != 0) {
2761 AddCodeLine ("ora #$%02X", (unsigned char)val);
2763 } else if ((val & 0xFF00) == 0xFF00) {
2764 if ((val & 0xFF) != 0) {
2765 AddCodeLine ("ora #$%02X", (unsigned char)val);
2768 } else if (val != 0) {
2769 AddCodeLine ("ora #$%02X", (unsigned char)val);
2770 AddCodeLine ("pha");
2771 AddCodeLine ("txa");
2772 AddCodeLine ("ora #$%02X", (unsigned char)(val >> 8));
2773 AddCodeLine ("tax");
2774 AddCodeLine ("pla");
2780 if ((val & 0xFF) != 0) {
2781 AddCodeLine ("ora #$%02X", (unsigned char)val);
2791 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2792 * into the normal, non-optimized stuff. Note: The standard stuff will
2793 * always work with ints.
2795 flags &= ~CF_FORCECHAR;
2796 g_push (flags & ~CF_CONST, 0);
2799 /* Use long way over the stack */
2800 oper (flags, val, ops);
2805 void g_xor (unsigned flags, unsigned long val)
2806 /* Primary = TOS ^ Primary */
2808 static const char* ops[12] = {
2809 "tosxorax", "tosxorax", "tosxoreax", "tosxoreax",
2813 /* If the right hand side is const, the lhs is not on stack but still
2814 * in the primary register.
2816 if (flags & CF_CONST) {
2818 switch (flags & CF_TYPE) {
2821 if (flags & CF_FORCECHAR) {
2822 if ((val & 0xFF) != 0) {
2823 AddCodeLine ("eor #$%02X", (unsigned char)val);
2832 AddCodeLine ("eor #$%02X", (unsigned char)val);
2834 } else if (val != 0) {
2835 if ((val & 0xFF) != 0) {
2836 AddCodeLine ("eor #$%02X", (unsigned char)val);
2838 AddCodeLine ("pha");
2839 AddCodeLine ("txa");
2840 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2841 AddCodeLine ("tax");
2842 AddCodeLine ("pla");
2849 AddCodeLine ("eor #$%02X", (unsigned char)val);
2859 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2860 * into the normal, non-optimized stuff. Note: The standard stuff will
2861 * always work with ints.
2863 flags &= ~CF_FORCECHAR;
2864 g_push (flags & ~CF_CONST, 0);
2867 /* Use long way over the stack */
2868 oper (flags, val, ops);
2873 void g_and (unsigned Flags, unsigned long Val)
2874 /* Primary = TOS & Primary */
2876 static const char* ops[12] = {
2877 "tosandax", "tosandax", "tosandeax", "tosandeax",
2880 /* If the right hand side is const, the lhs is not on stack but still
2881 * in the primary register.
2883 if (Flags & CF_CONST) {
2885 switch (Flags & CF_TYPE) {
2888 if (Flags & CF_FORCECHAR) {
2889 if ((Val & 0xFF) == 0x00) {
2890 AddCodeLine ("lda #$00");
2891 } else if ((Val & 0xFF) != 0xFF) {
2892 AddCodeLine ("and #$%02X", (unsigned char)Val);
2898 if ((Val & 0xFFFF) != 0xFFFF) {
2902 AddCodeLine ("lda #$00");
2903 } else if (Val != 0xFF) {
2904 AddCodeLine ("and #$%02X", (unsigned char)Val);
2906 } else if ((Val & 0xFFFF) == 0xFF00) {
2907 AddCodeLine ("lda #$00");
2908 } else if ((Val & 0xFF00) == 0xFF00) {
2909 AddCodeLine ("and #$%02X", (unsigned char)Val);
2910 } else if ((Val & 0x00FF) == 0x0000) {
2911 AddCodeLine ("txa");
2912 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2913 AddCodeLine ("tax");
2914 AddCodeLine ("lda #$00");
2916 AddCodeLine ("tay");
2917 AddCodeLine ("txa");
2918 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2919 AddCodeLine ("tax");
2920 AddCodeLine ("tya");
2921 if ((Val & 0x00FF) == 0x0000) {
2922 AddCodeLine ("lda #$00");
2923 } else if ((Val & 0x00FF) != 0x00FF) {
2924 AddCodeLine ("and #$%02X", (unsigned char)Val);
2933 AddCodeLine ("stx sreg+1");
2934 AddCodeLine ("stx sreg");
2935 if ((Val & 0xFF) != 0xFF) {
2936 AddCodeLine ("and #$%02X", (unsigned char)Val);
2939 } else if (Val == 0xFF00) {
2941 AddCodeLine ("sta sreg+1");
2942 AddCodeLine ("sta sreg");
2951 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2952 * into the normal, non-optimized stuff. Note: The standard stuff will
2953 * always work with ints.
2955 Flags &= ~CF_FORCECHAR;
2956 g_push (Flags & ~CF_CONST, 0);
2959 /* Use long way over the stack */
2960 oper (Flags, Val, ops);
2965 void g_asr (unsigned flags, unsigned long val)
2966 /* Primary = TOS >> Primary */
2968 static const char* ops[12] = {
2969 "tosasrax", "tosshrax", "tosasreax", "tosshreax",
2972 /* If the right hand side is const, the lhs is not on stack but still
2973 * in the primary register.
2975 if (flags & CF_CONST) {
2977 switch (flags & CF_TYPE) {
2983 if (flags & CF_UNSIGNED) {
2984 AddCodeLine ("txa");
2987 unsigned L = GetLocalLabel();
2988 AddCodeLine ("cpx #$80"); /* Sign bit into carry */
2989 AddCodeLine ("txa");
2990 AddCodeLine ("ldx #$00");
2991 AddCodeLine ("bcc %s", LocalLabelName (L));
2992 AddCodeLine ("dex"); /* Make $FF */
2998 if (flags & CF_UNSIGNED) {
2999 AddCodeLine ("jsr shrax4");
3001 AddCodeLine ("jsr asrax4");
3006 if (flags & CF_UNSIGNED) {
3007 AddCodeLine ("jsr shrax%ld", val);
3009 AddCodeLine ("jsr asrax%ld", val);
3017 AddCodeLine ("ldx #$00");
3018 AddCodeLine ("lda sreg+1");
3019 if ((flags & CF_UNSIGNED) == 0) {
3020 unsigned L = GetLocalLabel();
3021 AddCodeLine ("bpl %s", LocalLabelName (L));
3022 AddCodeLine ("dex");
3025 AddCodeLine ("stx sreg");
3026 AddCodeLine ("stx sreg+1");
3030 AddCodeLine ("ldy #$00");
3031 AddCodeLine ("ldx sreg+1");
3032 if ((flags & CF_UNSIGNED) == 0) {
3033 unsigned L = GetLocalLabel();
3034 AddCodeLine ("bpl %s", LocalLabelName (L));
3035 AddCodeLine ("dey");
3038 AddCodeLine ("lda sreg");
3039 AddCodeLine ("sty sreg+1");
3040 AddCodeLine ("sty sreg");
3044 AddCodeLine ("txa");
3045 AddCodeLine ("ldx sreg");
3046 AddCodeLine ("ldy sreg+1");
3047 AddCodeLine ("sty sreg");
3048 if ((flags & CF_UNSIGNED) == 0) {
3049 unsigned L = GetLocalLabel();
3050 AddCodeLine ("cpy #$80");
3051 AddCodeLine ("ldy #$00");
3052 AddCodeLine ("bcc %s", LocalLabelName (L));
3053 AddCodeLine ("dey");
3056 AddCodeLine ("ldy #$00");
3058 AddCodeLine ("sty sreg+1");
3062 if (flags & CF_UNSIGNED) {
3063 AddCodeLine ("jsr shreax4");
3065 AddCodeLine ("jsr asreax4");
3070 if (flags & CF_UNSIGNED) {
3071 AddCodeLine ("jsr shreax%ld", val);
3073 AddCodeLine ("jsr asreax%ld", val);
3082 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3083 * into the normal, non-optimized stuff. Note: The standard stuff will
3084 * always work with ints.
3086 flags &= ~CF_FORCECHAR;
3087 g_push (flags & ~CF_CONST, 0);
3090 /* Use long way over the stack */
3091 oper (flags, val, ops);
3096 void g_asl (unsigned flags, unsigned long val)
3097 /* Primary = TOS << Primary */
3099 static const char* ops[12] = {
3100 "tosaslax", "tosshlax", "tosasleax", "tosshleax",
3104 /* If the right hand side is const, the lhs is not on stack but still
3105 * in the primary register.
3107 if (flags & CF_CONST) {
3109 switch (flags & CF_TYPE) {
3115 AddCodeLine ("tax");
3116 AddCodeLine ("lda #$00");
3120 if (flags & CF_UNSIGNED) {
3121 AddCodeLine ("jsr shlax4");
3123 AddCodeLine ("jsr aslax4");
3128 if (flags & CF_UNSIGNED) {
3129 AddCodeLine ("jsr shlax%ld", val);
3131 AddCodeLine ("jsr aslax%ld", val);
3139 AddCodeLine ("sta sreg+1");
3140 AddCodeLine ("lda #$00");
3141 AddCodeLine ("tax");
3142 AddCodeLine ("sta sreg");
3146 AddCodeLine ("stx sreg+1");
3147 AddCodeLine ("sta sreg");
3148 AddCodeLine ("lda #$00");
3149 AddCodeLine ("tax");
3153 AddCodeLine ("ldy sreg");
3154 AddCodeLine ("sty sreg+1");
3155 AddCodeLine ("stx sreg");
3156 AddCodeLine ("tax");
3157 AddCodeLine ("lda #$00");
3161 if (flags & CF_UNSIGNED) {
3162 AddCodeLine ("jsr shleax4");
3164 AddCodeLine ("jsr asleax4");
3169 if (flags & CF_UNSIGNED) {
3170 AddCodeLine ("jsr shleax%ld", val);
3172 AddCodeLine ("jsr asleax%ld", val);
3181 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3182 * into the normal, non-optimized stuff. Note: The standard stuff will
3183 * always work with ints.
3185 flags &= ~CF_FORCECHAR;
3186 g_push (flags & ~CF_CONST, 0);
3189 /* Use long way over the stack */
3190 oper (flags, val, ops);
3195 void g_neg (unsigned Flags)
3196 /* Primary = -Primary */
3198 switch (Flags & CF_TYPE) {
3201 if (Flags & CF_FORCECHAR) {
3202 AddCodeLine ("eor #$FF");
3203 AddCodeLine ("clc");
3204 AddCodeLine ("adc #$01");
3210 AddCodeLine ("jsr negax");
3214 AddCodeLine ("jsr negeax");
3224 void g_bneg (unsigned flags)
3225 /* Primary = !Primary */
3227 switch (flags & CF_TYPE) {
3230 AddCodeLine ("jsr bnega");
3234 AddCodeLine ("jsr bnegax");
3238 AddCodeLine ("jsr bnegeax");
3248 void g_com (unsigned Flags)
3249 /* Primary = ~Primary */
3251 switch (Flags & CF_TYPE) {
3254 if (Flags & CF_FORCECHAR) {
3255 AddCodeLine ("eor #$FF");
3261 AddCodeLine ("jsr complax");
3265 AddCodeLine ("jsr compleax");
3275 void g_inc (unsigned flags, unsigned long val)
3276 /* Increment the primary register by a given number */
3278 /* Don't inc by zero */
3283 /* Generate code for the supported types */
3285 switch (flags & CF_TYPE) {
3288 if (flags & CF_FORCECHAR) {
3289 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3291 AddCodeLine ("ina");
3294 AddCodeLine ("clc");
3295 AddCodeLine ("adc #$%02X", (unsigned char)val);
3302 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) {
3303 unsigned L = GetLocalLabel();
3304 AddCodeLine ("ina");
3305 AddCodeLine ("bne %s", LocalLabelName (L));
3306 AddCodeLine ("inx");
3308 } else if (IS_Get (&CodeSizeFactor) < 200) {
3311 AddCodeLine ("jsr incax%lu", val);
3312 } else if (val <= 255) {
3314 AddCodeLine ("jsr incaxy");
3316 g_add (flags | CF_CONST, val);
3319 /* Inline the code */
3321 if ((val & 0xFF) != 0) {
3322 unsigned L = GetLocalLabel();
3323 AddCodeLine ("clc");
3324 AddCodeLine ("adc #$%02X", (unsigned char) val);
3325 AddCodeLine ("bcc %s", LocalLabelName (L));
3326 AddCodeLine ("inx");
3330 AddCodeLine ("inx");
3333 AddCodeLine ("inx");
3336 AddCodeLine ("inx");
3338 } else if ((val & 0xFF) != 0) {
3339 AddCodeLine ("clc");
3340 AddCodeLine ("adc #$%02X", (unsigned char) val);
3341 AddCodeLine ("pha");
3342 AddCodeLine ("txa");
3343 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3344 AddCodeLine ("tax");
3345 AddCodeLine ("pla");
3347 AddCodeLine ("pha");
3348 AddCodeLine ("txa");
3349 AddCodeLine ("clc");
3350 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3351 AddCodeLine ("tax");
3352 AddCodeLine ("pla");
3360 AddCodeLine ("jsr inceaxy");
3362 g_add (flags | CF_CONST, val);
3374 void g_dec (unsigned flags, unsigned long val)
3375 /* Decrement the primary register by a given number */
3377 /* Don't dec by zero */
3382 /* Generate code for the supported types */
3384 switch (flags & CF_TYPE) {
3387 if (flags & CF_FORCECHAR) {
3388 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3390 AddCodeLine ("dea");
3393 AddCodeLine ("sec");
3394 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3401 if (IS_Get (&CodeSizeFactor) < 200) {
3402 /* Use subroutines */
3404 AddCodeLine ("jsr decax%d", (int) val);
3405 } else if (val <= 255) {
3407 AddCodeLine ("jsr decaxy");
3409 g_sub (flags | CF_CONST, val);
3412 /* Inline the code */
3414 if ((val & 0xFF) != 0) {
3415 unsigned L = GetLocalLabel();
3416 AddCodeLine ("sec");
3417 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3418 AddCodeLine ("bcs %s", LocalLabelName (L));
3419 AddCodeLine ("dex");
3423 AddCodeLine ("dex");
3426 AddCodeLine ("dex");
3429 if ((val & 0xFF) != 0) {
3430 AddCodeLine ("sec");
3431 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3432 AddCodeLine ("pha");
3433 AddCodeLine ("txa");
3434 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3435 AddCodeLine ("tax");
3436 AddCodeLine ("pla");
3438 AddCodeLine ("pha");
3439 AddCodeLine ("txa");
3440 AddCodeLine ("sec");
3441 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3442 AddCodeLine ("tax");
3443 AddCodeLine ("pla");
3452 AddCodeLine ("jsr deceaxy");
3454 g_sub (flags | CF_CONST, val);
3467 * Following are the conditional operators. They compare the TOS against
3468 * the primary and put a literal 1 in the primary if the condition is
3469 * true, otherwise they clear the primary register
3474 void g_eq (unsigned flags, unsigned long val)
3475 /* Test for equal */
3477 static const char* ops[12] = {
3478 "toseqax", "toseqax", "toseqeax", "toseqeax",
3483 /* If the right hand side is const, the lhs is not on stack but still
3484 * in the primary register.
3486 if (flags & CF_CONST) {
3488 switch (flags & CF_TYPE) {
3491 if (flags & CF_FORCECHAR) {
3492 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3493 AddCodeLine ("jsr booleq");
3499 L = GetLocalLabel();
3500 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3501 AddCodeLine ("bne %s", LocalLabelName (L));
3502 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3504 AddCodeLine ("jsr booleq");
3514 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3515 * into the normal, non-optimized stuff. Note: The standard stuff will
3516 * always work with ints.
3518 flags &= ~CF_FORCECHAR;
3519 g_push (flags & ~CF_CONST, 0);
3522 /* Use long way over the stack */
3523 oper (flags, val, ops);
3528 void g_ne (unsigned flags, unsigned long val)
3529 /* Test for not equal */
3531 static const char* ops[12] = {
3532 "tosneax", "tosneax", "tosneeax", "tosneeax",
3537 /* If the right hand side is const, the lhs is not on stack but still
3538 * in the primary register.
3540 if (flags & CF_CONST) {
3542 switch (flags & CF_TYPE) {
3545 if (flags & CF_FORCECHAR) {
3546 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3547 AddCodeLine ("jsr boolne");
3553 L = GetLocalLabel();
3554 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3555 AddCodeLine ("bne %s", LocalLabelName (L));
3556 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3558 AddCodeLine ("jsr boolne");
3568 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3569 * into the normal, non-optimized stuff. Note: The standard stuff will
3570 * always work with ints.
3572 flags &= ~CF_FORCECHAR;
3573 g_push (flags & ~CF_CONST, 0);
3576 /* Use long way over the stack */
3577 oper (flags, val, ops);
3582 void g_lt (unsigned flags, unsigned long val)
3583 /* Test for less than */
3585 static const char* ops[12] = {
3586 "tosltax", "tosultax", "toslteax", "tosulteax",
3591 /* If the right hand side is const, the lhs is not on stack but still
3592 * in the primary register.
3594 if (flags & CF_CONST) {
3596 /* Because the handling of the overflow flag is too complex for
3597 * inlining, we can handle only unsigned compares, and signed
3598 * compares against zero here.
3600 if (flags & CF_UNSIGNED) {
3602 /* Give a warning in some special cases */
3604 Warning ("Condition is never true");
3605 AddCodeLine ("jsr return0");
3609 /* Look at the type */
3610 switch (flags & CF_TYPE) {
3613 if (flags & CF_FORCECHAR) {
3614 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3615 AddCodeLine ("jsr boolult");
3621 /* If the low byte is zero, we must only test the high byte */
3622 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3623 if ((val & 0xFF) != 0) {
3624 unsigned L = GetLocalLabel();
3625 AddCodeLine ("bne %s", LocalLabelName (L));
3626 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3629 AddCodeLine ("jsr boolult");
3633 /* Do a subtraction */
3634 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3635 AddCodeLine ("txa");
3636 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3637 AddCodeLine ("lda sreg");
3638 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3639 AddCodeLine ("lda sreg+1");
3640 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3641 AddCodeLine ("jsr boolult");
3648 } else if (val == 0) {
3650 /* A signed compare against zero must only look at the sign bit */
3651 switch (flags & CF_TYPE) {
3654 if (flags & CF_FORCECHAR) {
3655 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3656 AddCodeLine ("lda #$00");
3657 AddCodeLine ("ldx #$00");
3658 AddCodeLine ("rol a");
3664 /* Just check the high byte */
3665 AddCodeLine ("cpx #$80"); /* Bit 7 -> carry */
3666 AddCodeLine ("lda #$00");
3667 AddCodeLine ("ldx #$00");
3668 AddCodeLine ("rol a");
3672 /* Just check the high byte */
3673 AddCodeLine ("lda sreg+1");
3674 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3675 AddCodeLine ("lda #$00");
3676 AddCodeLine ("ldx #$00");
3677 AddCodeLine ("rol a");
3686 /* Signed compare against a constant != zero */
3687 switch (flags & CF_TYPE) {
3690 if (flags & CF_FORCECHAR) {
3691 Label = GetLocalLabel ();
3692 AddCodeLine ("sec");
3693 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3694 AddCodeLine ("bvc %s", LocalLabelName (Label));
3695 AddCodeLine ("eor #$80");
3696 g_defcodelabel (Label);
3697 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3698 AddCodeLine ("lda #$00");
3699 AddCodeLine ("ldx #$00");
3700 AddCodeLine ("rol a");
3706 /* Do a subtraction */
3707 Label = GetLocalLabel ();
3708 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3709 AddCodeLine ("txa");
3710 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3711 AddCodeLine ("bvc %s", LocalLabelName (Label));
3712 AddCodeLine ("eor #$80");
3713 g_defcodelabel (Label);
3714 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3715 AddCodeLine ("lda #$00");
3716 AddCodeLine ("ldx #$00");
3717 AddCodeLine ("rol a");
3721 /* This one is too costly */
3730 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3731 * into the normal, non-optimized stuff. Note: The standard stuff will
3732 * always work with ints.
3734 flags &= ~CF_FORCECHAR;
3735 g_push (flags & ~CF_CONST, 0);
3738 /* Use long way over the stack */
3739 oper (flags, val, ops);
3744 void g_le (unsigned flags, unsigned long val)
3745 /* Test for less than or equal to */
3747 static const char* ops[12] = {
3748 "tosleax", "tosuleax", "tosleeax", "tosuleeax",
3752 /* If the right hand side is const, the lhs is not on stack but still
3753 * in the primary register.
3755 if (flags & CF_CONST) {
3757 /* Look at the type */
3758 switch (flags & CF_TYPE) {
3761 if (flags & CF_FORCECHAR) {
3762 if (flags & CF_UNSIGNED) {
3763 /* Unsigned compare */
3765 /* Use < instead of <= because the former gives
3766 * better code on the 6502 than the latter.
3768 g_lt (flags, val+1);
3771 Warning ("Condition is always true");
3772 AddCodeLine ("jsr return1");
3775 /* Signed compare */
3776 if ((long) val < 0x7F) {
3777 /* Use < instead of <= because the former gives
3778 * better code on the 6502 than the latter.
3780 g_lt (flags, val+1);
3783 Warning ("Condition is always true");
3784 AddCodeLine ("jsr return1");
3792 if (flags & CF_UNSIGNED) {
3793 /* Unsigned compare */
3795 /* Use < instead of <= because the former gives
3796 * better code on the 6502 than the latter.
3798 g_lt (flags, val+1);
3801 Warning ("Condition is always true");
3802 AddCodeLine ("jsr return1");
3805 /* Signed compare */
3806 if ((long) val < 0x7FFF) {
3807 g_lt (flags, val+1);
3810 Warning ("Condition is always true");
3811 AddCodeLine ("jsr return1");
3817 if (flags & CF_UNSIGNED) {
3818 /* Unsigned compare */
3819 if (val < 0xFFFFFFFF) {
3820 /* Use < instead of <= because the former gives
3821 * better code on the 6502 than the latter.
3823 g_lt (flags, val+1);
3826 Warning ("Condition is always true");
3827 AddCodeLine ("jsr return1");
3830 /* Signed compare */
3831 if ((long) val < 0x7FFFFFFF) {
3832 g_lt (flags, val+1);
3835 Warning ("Condition is always true");
3836 AddCodeLine ("jsr return1");
3845 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3846 * into the normal, non-optimized stuff. Note: The standard stuff will
3847 * always work with ints.
3849 flags &= ~CF_FORCECHAR;
3850 g_push (flags & ~CF_CONST, 0);
3853 /* Use long way over the stack */
3854 oper (flags, val, ops);
3859 void g_gt (unsigned flags, unsigned long val)
3860 /* Test for greater than */
3862 static const char* ops[12] = {
3863 "tosgtax", "tosugtax", "tosgteax", "tosugteax",
3867 /* If the right hand side is const, the lhs is not on stack but still
3868 * in the primary register.
3870 if (flags & CF_CONST) {
3872 /* Look at the type */
3873 switch (flags & CF_TYPE) {
3876 if (flags & CF_FORCECHAR) {
3877 if (flags & CF_UNSIGNED) {
3879 /* If we have a compare > 0, we will replace it by
3880 * != 0 here, since both are identical but the
3881 * latter is easier to optimize.
3884 } else if (val < 0xFF) {
3885 /* Use >= instead of > because the former gives
3886 * better code on the 6502 than the latter.
3888 g_ge (flags, val+1);
3891 Warning ("Condition is never true");
3892 AddCodeLine ("jsr return0");
3895 if ((long) val < 0x7F) {
3896 /* Use >= instead of > because the former gives
3897 * better code on the 6502 than the latter.
3899 g_ge (flags, val+1);
3902 Warning ("Condition is never true");
3903 AddCodeLine ("jsr return0");
3911 if (flags & CF_UNSIGNED) {
3912 /* Unsigned compare */
3914 /* If we have a compare > 0, we will replace it by
3915 * != 0 here, since both are identical but the latter
3916 * is easier to optimize.
3919 } else if (val < 0xFFFF) {
3920 /* Use >= instead of > because the former gives better
3921 * code on the 6502 than the latter.
3923 g_ge (flags, val+1);
3926 Warning ("Condition is never true");
3927 AddCodeLine ("jsr return0");
3930 /* Signed compare */
3931 if ((long) val < 0x7FFF) {
3932 g_ge (flags, val+1);
3935 Warning ("Condition is never true");
3936 AddCodeLine ("jsr return0");
3942 if (flags & CF_UNSIGNED) {
3943 /* Unsigned compare */
3945 /* If we have a compare > 0, we will replace it by
3946 * != 0 here, since both are identical but the latter
3947 * is easier to optimize.
3950 } else if (val < 0xFFFFFFFF) {
3951 /* Use >= instead of > because the former gives better
3952 * code on the 6502 than the latter.
3954 g_ge (flags, val+1);
3957 Warning ("Condition is never true");
3958 AddCodeLine ("jsr return0");
3961 /* Signed compare */
3962 if ((long) val < 0x7FFFFFFF) {
3963 g_ge (flags, val+1);
3966 Warning ("Condition is never true");
3967 AddCodeLine ("jsr return0");
3976 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3977 * into the normal, non-optimized stuff. Note: The standard stuff will
3978 * always work with ints.
3980 flags &= ~CF_FORCECHAR;
3981 g_push (flags & ~CF_CONST, 0);
3984 /* Use long way over the stack */
3985 oper (flags, val, ops);
3990 void g_ge (unsigned flags, unsigned long val)
3991 /* Test for greater than or equal to */
3993 static const char* ops[12] = {
3994 "tosgeax", "tosugeax", "tosgeeax", "tosugeeax",
4000 /* If the right hand side is const, the lhs is not on stack but still
4001 * in the primary register.
4003 if (flags & CF_CONST) {
4005 /* Because the handling of the overflow flag is too complex for
4006 * inlining, we can handle only unsigned compares, and signed
4007 * compares against zero here.
4009 if (flags & CF_UNSIGNED) {
4011 /* Give a warning in some special cases */
4013 Warning ("Condition is always true");
4014 AddCodeLine ("jsr return1");
4018 /* Look at the type */
4019 switch (flags & CF_TYPE) {
4022 if (flags & CF_FORCECHAR) {
4023 /* Do a subtraction. Condition is true if carry set */
4024 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4025 AddCodeLine ("lda #$00");
4026 AddCodeLine ("ldx #$00");
4027 AddCodeLine ("rol a");
4033 /* Do a subtraction. Condition is true if carry set */
4034 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4035 AddCodeLine ("txa");
4036 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4037 AddCodeLine ("lda #$00");
4038 AddCodeLine ("ldx #$00");
4039 AddCodeLine ("rol a");
4043 /* Do a subtraction. Condition is true if carry set */
4044 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4045 AddCodeLine ("txa");
4046 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4047 AddCodeLine ("lda sreg");
4048 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
4049 AddCodeLine ("lda sreg+1");
4050 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
4051 AddCodeLine ("lda #$00");
4052 AddCodeLine ("ldx #$00");
4053 AddCodeLine ("rol a");
4060 } else if (val == 0) {
4062 /* A signed compare against zero must only look at the sign bit */
4063 switch (flags & CF_TYPE) {
4066 if (flags & CF_FORCECHAR) {
4067 AddCodeLine ("tax");
4068 AddCodeLine ("jsr boolge");
4074 /* Just test the high byte */
4075 AddCodeLine ("txa");
4076 AddCodeLine ("jsr boolge");
4080 /* Just test the high byte */
4081 AddCodeLine ("lda sreg+1");
4082 AddCodeLine ("jsr boolge");
4091 /* Signed compare against a constant != zero */
4092 switch (flags & CF_TYPE) {
4095 if (flags & CF_FORCECHAR) {
4096 Label = GetLocalLabel ();
4097 AddCodeLine ("sec");
4098 AddCodeLine ("sbc #$%02X", (unsigned char)val);
4099 AddCodeLine ("bvs %s", LocalLabelName (Label));
4100 AddCodeLine ("eor #$80");
4101 g_defcodelabel (Label);
4102 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4103 AddCodeLine ("lda #$00");
4104 AddCodeLine ("ldx #$00");
4105 AddCodeLine ("rol a");
4111 /* Do a subtraction */
4112 Label = GetLocalLabel ();
4113 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4114 AddCodeLine ("txa");
4115 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4116 AddCodeLine ("bvs %s", LocalLabelName (Label));
4117 AddCodeLine ("eor #$80");
4118 g_defcodelabel (Label);
4119 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4120 AddCodeLine ("lda #$00");
4121 AddCodeLine ("ldx #$00");
4122 AddCodeLine ("rol a");
4126 /* This one is too costly */
4134 /* If we go here, we didn't emit code. Push the lhs on stack and fall
4135 * into the normal, non-optimized stuff. Note: The standard stuff will
4136 * always work with ints.
4138 flags &= ~CF_FORCECHAR;
4139 g_push (flags & ~CF_CONST, 0);
4142 /* Use long way over the stack */
4143 oper (flags, val, ops);
4148 /*****************************************************************************/
4149 /* Allocating static storage */
4150 /*****************************************************************************/
4154 void g_res (unsigned n)
4155 /* Reserve static storage, n bytes */
4157 AddDataLine ("\t.res\t%u,$00", n);
4162 void g_defdata (unsigned flags, unsigned long val, long offs)
4163 /* Define data with the size given in flags */
4165 if (flags & CF_CONST) {
4167 /* Numeric constant */
4168 switch (flags & CF_TYPE) {
4171 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
4175 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
4179 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
4190 /* Create the correct label name */
4191 const char* Label = GetLabelName (flags, val, offs);
4193 /* Labels are always 16 bit */
4194 AddDataLine ("\t.addr\t%s", Label);
4201 void g_defbytes (const void* Bytes, unsigned Count)
4202 /* Output a row of bytes as a constant */
4208 /* Cast the buffer pointer */
4209 const unsigned char* Data = (const unsigned char*) Bytes;
4211 /* Output the stuff */
4214 /* How many go into this line? */
4215 if ((Chunk = Count) > 16) {
4220 /* Output one line */
4221 strcpy (Buf, "\t.byte\t");
4224 B += sprintf (B, "$%02X", *Data++);
4230 /* Output the line */
4237 void g_zerobytes (unsigned Count)
4238 /* Output Count bytes of data initialized with zero */
4241 AddDataLine ("\t.res\t%u,$00", Count);
4247 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4248 /* Initialize a register variable from static initialization data */
4250 /* Register variables do always have less than 128 bytes */
4251 unsigned CodeLabel = GetLocalLabel ();
4253 g_defcodelabel (CodeLabel);
4254 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4255 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4256 AddCodeLine ("dex");
4257 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4262 void g_initauto (unsigned Label, unsigned Size)
4263 /* Initialize a local variable at stack offset zero from static data */
4265 unsigned CodeLabel = GetLocalLabel ();
4267 CheckLocalOffs (Size);
4270 g_defcodelabel (CodeLabel);
4271 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4272 AddCodeLine ("sta (sp),y");
4273 AddCodeLine ("dey");
4274 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4275 } else if (Size <= 256) {
4277 g_defcodelabel (CodeLabel);
4278 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4279 AddCodeLine ("sta (sp),y");
4280 AddCodeLine ("iny");
4281 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4282 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4288 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4289 /* Initialize a static local variable from static initialization data */
4292 unsigned CodeLabel = GetLocalLabel ();
4294 g_defcodelabel (CodeLabel);
4295 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4296 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4297 AddCodeLine ("dey");
4298 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4299 } else if (Size <= 256) {
4300 unsigned CodeLabel = GetLocalLabel ();
4302 g_defcodelabel (CodeLabel);
4303 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4304 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4305 AddCodeLine ("iny");
4306 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4307 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4309 /* Use the easy way here: memcpy */
4310 g_getimmed (CF_STATIC, VarLabel, 0);
4311 AddCodeLine ("jsr pushax");
4312 g_getimmed (CF_STATIC, InitLabel, 0);
4313 AddCodeLine ("jsr pushax");
4314 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4315 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4321 /*****************************************************************************/
4322 /* Switch statement */
4323 /*****************************************************************************/
4327 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4328 /* Generate code for a switch statement */
4330 unsigned NextLabel = 0;
4333 /* Setup registers and determine which compare insn to use */
4334 const char* Compare;
4337 Compare = "cmp #$%02X";
4340 Compare = "cpx #$%02X";
4343 AddCodeLine ("ldy sreg");
4344 Compare = "cpy #$%02X";
4347 AddCodeLine ("ldy sreg+1");
4348 Compare = "cpy #$%02X";
4351 Internal ("Invalid depth in g_switch: %u", Depth);
4354 /* Walk over all nodes */
4355 for (I = 0; I < CollCount (Nodes); ++I) {
4357 /* Get the next case node */
4358 CaseNode* N = CollAtUnchecked (Nodes, I);
4360 /* If we have a next label, define it */
4362 g_defcodelabel (NextLabel);
4366 /* Do the compare */
4367 AddCodeLine (Compare, CN_GetValue (N));
4369 /* If this is the last level, jump directly to the case code if found */
4372 /* Branch if equal */
4373 g_falsejump (0, CN_GetLabel (N));
4377 /* Determine the next label */
4378 if (I == CollCount (Nodes) - 1) {
4379 /* Last node means not found */
4380 g_truejump (0, DefaultLabel);
4382 /* Jump to the next check */
4383 NextLabel = GetLocalLabel ();
4384 g_truejump (0, NextLabel);
4387 /* Check the next level */
4388 g_switch (N->Nodes, DefaultLabel, Depth-1);
4393 /* If we go here, we haven't found the label */
4394 g_jump (DefaultLabel);
4399 /*****************************************************************************/
4400 /* User supplied assembler code */
4401 /*****************************************************************************/
4405 void g_asmcode (struct StrBuf* B)
4406 /* Output one line of assembler code. */
4408 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));