1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 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 /*****************************************************************************/
63 /*****************************************************************************/
65 /*****************************************************************************/
69 /* Compiler relative stack pointer */
74 /*****************************************************************************/
76 /*****************************************************************************/
80 static void typeerror (unsigned type)
81 /* Print an error message about an invalid operand type */
83 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
88 static void CheckLocalOffs (unsigned Offs)
89 /* Check the offset into the stack for 8bit range */
92 /* Too many local vars */
93 Error ("Too many local variables");
99 static const char* GetLabelName (unsigned Flags, unsigned long Label, long Offs)
101 static char Buf [256]; /* Label name */
103 /* Create the correct label name */
104 switch (Flags & CF_ADDRMASK) {
107 /* Static memory cell */
109 xsprintf (Buf, sizeof (Buf), "%s%+ld", LocalLabelName (Label), Offs);
111 xsprintf (Buf, sizeof (Buf), "%s", LocalLabelName (Label));
118 xsprintf (Buf, sizeof (Buf), "_%s%+ld", (char*) Label, Offs);
120 xsprintf (Buf, sizeof (Buf), "_%s", (char*) Label);
125 /* Absolute address */
126 xsprintf (Buf, sizeof (Buf), "$%04X", (int)((Label+Offs) & 0xFFFF));
130 /* Variable in register bank */
131 xsprintf (Buf, sizeof (Buf), "regbank+%u", (unsigned)((Label+Offs) & 0xFFFF));
135 Internal ("Invalid address flags: %04X", Flags);
138 /* Return a pointer to the static buffer */
144 /*****************************************************************************/
145 /* Pre- and postamble */
146 /*****************************************************************************/
150 void g_preamble (void)
151 /* Generate the assembler code preamble */
153 /* Create a new (global) segment list and remember it */
157 /* Identify the compiler version */
159 AddTextLine ("; File generated by cc65 v %u.%u.%u",
160 VER_MAJOR, VER_MINOR, VER_PATCH);
163 /* Insert some object file options */
164 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
165 VER_MAJOR, VER_MINOR, VER_PATCH);
167 /* If we're producing code for some other CPU, switch the command set */
168 if (CPU == CPU_65C02) {
169 AddTextLine ("\t.setcpu\t\t\"65C02\"");
172 /* Allow auto import for runtime library routines */
173 AddTextLine ("\t.autoimport\ton");
175 /* Switch the assembler into case sensitive mode */
176 AddTextLine ("\t.case\t\ton");
178 /* Tell the assembler if we want to generate debug info */
179 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
181 /* Import the stack pointer for direct auto variable access */
182 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
184 /* Define long branch macros */
185 AddTextLine ("\t.macpack\tlongbranch");
190 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
191 /* If debug info is enabled, place a file info into the source */
194 /* We have to place this into the global text segment, so it will
195 * appear before all .dbg line statements.
197 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
203 /*****************************************************************************/
204 /* Segment support */
205 /*****************************************************************************/
209 void g_userodata (void)
210 /* Switch to the read only data segment */
212 UseDataSeg (SEG_RODATA);
217 void g_usedata (void)
218 /* Switch to the data segment */
220 UseDataSeg (SEG_DATA);
226 /* Switch to the bss segment */
228 UseDataSeg (SEG_BSS);
233 void g_segname (segment_t Seg, const char* Name)
234 /* Set the name of a segment */
238 /* Remember the new name */
239 NewSegName (Seg, Name);
241 /* 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\"", Name);
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 oursp += sizeofarg (flags);
295 int push (unsigned flags)
296 /* Push an argument of the given size */
298 return oursp -= 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_importmainargs (void)
394 /* Forced import of a special symbol that handles arguments to main */
396 AddTextLine ("\t.forceimport\tinitmainargs");
401 /*****************************************************************************/
402 /* Load functions for various registers */
403 /*****************************************************************************/
407 static void ldaconst (unsigned val)
408 /* Load a with a constant */
410 AddCodeLine ("lda #$%02X", val & 0xFF);
415 static void ldxconst (unsigned val)
416 /* Load x with a constant */
418 AddCodeLine ("ldx #$%02X", val & 0xFF);
423 static void ldyconst (unsigned val)
424 /* Load y with a constant */
426 AddCodeLine ("ldy #$%02X", val & 0xFF);
431 /*****************************************************************************/
432 /* Function entry and exit */
433 /*****************************************************************************/
437 /* Remember the argument size of a function. The variable is set by g_enter
438 * and used by g_leave. If the functions gets its argument size by the caller
439 * (variable param list or function without prototype), g_enter will set the
445 void g_enter (unsigned flags, unsigned argsize)
446 /* Function prologue */
448 if ((flags & CF_FIXARGC) != 0) {
449 /* Just remember the argument size for the leave */
453 AddCodeLine ("jsr enter");
460 /* Function epilogue */
462 /* How many bytes of locals do we have to drop? */
465 /* If we didn't have a variable argument list, don't call leave */
468 /* Drop stackframe if needed */
472 AddCodeLine ("jsr incsp%d", k);
476 AddCodeLine ("jsr addysp");
483 /* Nothing to drop */
484 AddCodeLine ("jsr leave");
486 /* We've a stack frame to drop */
488 AddCodeLine ("jsr leavey");
492 /* Add the final rts */
498 /*****************************************************************************/
499 /* Register variables */
500 /*****************************************************************************/
504 void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
505 /* Swap a register variable with a location on the stack */
507 /* Calculate the actual stack offset and check it */
509 CheckLocalOffs (StackOffs);
514 if (CodeSizeFactor < 165) {
515 ldyconst (StackOffs);
517 AddCodeLine ("jsr regswap1");
519 ldyconst (StackOffs);
520 AddCodeLine ("lda (sp),y");
521 AddCodeLine ("ldx regbank%+d", RegOffs);
522 AddCodeLine ("sta regbank%+d", RegOffs);
524 AddCodeLine ("sta (sp),y");
527 } else if (Bytes == 2) {
529 ldyconst (StackOffs);
531 AddCodeLine ("jsr regswap2");
535 ldyconst (StackOffs);
538 AddCodeLine ("jsr regswap");
544 void g_save_regvars (int RegOffs, unsigned Bytes)
545 /* Save register variables */
547 /* Don't loop for up to two bytes */
550 AddCodeLine ("lda regbank%+d", RegOffs);
551 AddCodeLine ("jsr pusha");
553 } else if (Bytes == 2) {
555 AddCodeLine ("lda regbank%+d", RegOffs);
556 AddCodeLine ("ldx regbank%+d", RegOffs+1);
557 AddCodeLine ("jsr pushax");
561 /* More than two bytes - loop */
562 unsigned Label = GetLocalLabel ();
564 ldyconst (Bytes - 1);
566 g_defcodelabel (Label);
567 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
568 AddCodeLine ("sta (sp),y");
571 AddCodeLine ("bne %s", LocalLabelName (Label));
575 /* We pushed stuff, correct the stack pointer */
581 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
582 /* Restore register variables */
584 /* Calculate the actual stack offset and check it */
586 CheckLocalOffs (StackOffs);
588 /* Don't loop for up to two bytes */
591 ldyconst (StackOffs);
592 AddCodeLine ("lda (sp),y");
593 AddCodeLine ("sta regbank%+d", RegOffs);
595 } else if (Bytes == 2) {
597 ldyconst (StackOffs);
598 AddCodeLine ("lda (sp),y");
599 AddCodeLine ("sta regbank%+d", RegOffs);
601 AddCodeLine ("lda (sp),y");
602 AddCodeLine ("sta regbank%+d", RegOffs+1);
604 } else if (Bytes == 3 && CodeSizeFactor >= 133) {
606 ldyconst (StackOffs);
607 AddCodeLine ("lda (sp),y");
608 AddCodeLine ("sta regbank%+d", RegOffs);
610 AddCodeLine ("lda (sp),y");
611 AddCodeLine ("sta regbank%+d", RegOffs+1);
613 AddCodeLine ("lda (sp),y");
614 AddCodeLine ("sta regbank%+d", RegOffs+2);
616 } else if (StackOffs <= RegOffs) {
618 /* More bytes, but the relation between the register offset in the
619 * register bank and the stack offset allows us to generate short
620 * code that uses just one index register.
622 unsigned Label = GetLocalLabel ();
623 ldyconst (StackOffs);
624 g_defcodelabel (Label);
625 AddCodeLine ("lda (sp),y");
626 AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs);
628 AddCodeLine ("cpy #$%02X", StackOffs + Bytes);
629 AddCodeLine ("bne %s", LocalLabelName (Label));
633 /* Ok, this is the generic code. We need to save X because the
634 * caller will only save A.
636 unsigned Label = GetLocalLabel ();
637 AddCodeLine ("stx tmp1");
638 ldyconst (StackOffs + Bytes - 1);
639 ldxconst (Bytes - 1);
640 g_defcodelabel (Label);
641 AddCodeLine ("lda (sp),y");
642 AddCodeLine ("sta regbank%+d,x", RegOffs);
645 AddCodeLine ("bpl %s", LocalLabelName (Label));
646 AddCodeLine ("ldx tmp1");
653 /*****************************************************************************/
654 /* Fetching memory cells */
655 /*****************************************************************************/
659 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
660 /* Load a constant into the primary register */
662 unsigned char B1, B2, B3, B4;
666 if ((Flags & CF_CONST) != 0) {
668 /* Numeric constant */
669 switch (Flags & CF_TYPE) {
672 if ((Flags & CF_FORCECHAR) != 0) {
678 ldxconst ((Val >> 8) & 0xFF);
679 ldaconst (Val & 0xFF);
683 /* Split the value into 4 bytes */
684 B1 = (unsigned char) (Val >> 0);
685 B2 = (unsigned char) (Val >> 8);
686 B3 = (unsigned char) (Val >> 16);
687 B4 = (unsigned char) (Val >> 24);
689 /* Remember which bytes are done */
693 AddCodeLine ("ldx #$%02X", B2);
696 AddCodeLine ("stx sreg");
700 AddCodeLine ("stx sreg+1");
703 if ((Done & 0x04) == 0 && B1 != B3) {
704 AddCodeLine ("lda #$%02X", B3);
705 AddCodeLine ("sta sreg");
708 if ((Done & 0x08) == 0 && B1 != B4) {
709 AddCodeLine ("lda #$%02X", B4);
710 AddCodeLine ("sta sreg+1");
713 AddCodeLine ("lda #$%02X", B1);
715 if ((Done & 0x04) == 0) {
717 AddCodeLine ("sta sreg");
719 if ((Done & 0x08) == 0) {
721 AddCodeLine ("sta sreg+1");
733 /* Some sort of label */
734 const char* Label = GetLabelName (Flags, Val, Offs);
736 /* Load the address into the primary */
737 AddCodeLine ("lda #<(%s)", Label);
738 AddCodeLine ("ldx #>(%s)", Label);
745 void g_getstatic (unsigned flags, unsigned long label, long offs)
746 /* Fetch an static memory cell into the primary register */
748 /* Create the correct label name */
749 const char* lbuf = GetLabelName (flags, label, offs);
751 /* Check the size and generate the correct load operation */
752 switch (flags & CF_TYPE) {
755 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
756 AddCodeLine ("lda %s", lbuf); /* load A from the label */
759 AddCodeLine ("lda %s", lbuf); /* load A from the label */
760 if (!(flags & CF_UNSIGNED)) {
761 /* Must sign extend */
762 unsigned L = GetLocalLabel ();
763 AddCodeLine ("bpl %s", LocalLabelName (L));
771 AddCodeLine ("lda %s", lbuf);
772 if (flags & CF_TEST) {
773 AddCodeLine ("ora %s+1", lbuf);
775 AddCodeLine ("ldx %s+1", lbuf);
780 if (flags & CF_TEST) {
781 AddCodeLine ("lda %s+3", lbuf);
782 AddCodeLine ("ora %s+2", lbuf);
783 AddCodeLine ("ora %s+1", lbuf);
784 AddCodeLine ("ora %s+0", lbuf);
786 AddCodeLine ("lda %s+3", lbuf);
787 AddCodeLine ("sta sreg+1");
788 AddCodeLine ("lda %s+2", lbuf);
789 AddCodeLine ("sta sreg");
790 AddCodeLine ("ldx %s+1", lbuf);
791 AddCodeLine ("lda %s", lbuf);
803 void g_getlocal (unsigned flags, int offs)
804 /* Fetch specified local object (local var). */
807 CheckLocalOffs (offs);
808 switch (flags & CF_TYPE) {
811 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
813 AddCodeLine ("lda (sp),y");
816 AddCodeLine ("ldx #$00");
817 AddCodeLine ("lda (sp),y");
818 if ((flags & CF_UNSIGNED) == 0) {
819 unsigned L = GetLocalLabel();
820 AddCodeLine ("bpl %s", LocalLabelName (L));
828 CheckLocalOffs (offs + 1);
829 if (flags & CF_TEST) {
831 AddCodeLine ("lda (sp),y");
833 AddCodeLine ("ora (sp),y");
836 AddCodeLine ("jsr ldaxysp");
842 AddCodeLine ("jsr ldeaxysp");
852 void g_getind (unsigned flags, unsigned offs)
853 /* Fetch the specified object type indirect through the primary register
854 * into the primary register
857 /* If the offset is greater than 255, add the part that is > 255 to
858 * the primary. This way we get an easy addition and use the low byte
861 offs = MakeByteOffs (flags, offs);
863 /* Handle the indirect fetch */
864 switch (flags & CF_TYPE) {
867 /* Character sized */
868 if (flags & CF_UNSIGNED) {
870 AddCodeLine ("jsr ldauidx");
873 AddCodeLine ("jsr ldaidx");
878 if (flags & CF_TEST) {
880 AddCodeLine ("sta ptr1");
881 AddCodeLine ("stx ptr1+1");
882 AddCodeLine ("lda (ptr1),y");
884 AddCodeLine ("ora (ptr1),y");
887 AddCodeLine ("jsr ldaxidx");
893 AddCodeLine ("jsr ldeaxidx");
894 if (flags & CF_TEST) {
895 AddCodeLine ("jsr tsteax");
907 void g_leasp (int offs)
908 /* Fetch the address of the specified symbol into the primary register */
910 /* Calculate the offset relative to sp */
913 /* For value 0 we do direct code */
915 AddCodeLine ("lda sp");
916 AddCodeLine ("ldx sp+1");
918 if (CodeSizeFactor < 300) {
919 ldaconst (offs); /* Load A with offset value */
920 AddCodeLine ("jsr leaasp"); /* Load effective address */
922 unsigned L = GetLocalLabel ();
923 if (CPU == CPU_65C02 && offs == 1) {
924 AddCodeLine ("lda sp");
925 AddCodeLine ("ldx sp+1");
927 AddCodeLine ("bne %s", LocalLabelName (L));
932 AddCodeLine ("ldx sp+1");
933 AddCodeLine ("adc sp");
934 AddCodeLine ("bcc %s", LocalLabelName (L));
944 void g_leavariadic (int Offs)
945 /* Fetch the address of a parameter in a variadic function into the primary
949 unsigned ArgSizeOffs;
951 /* Calculate the offset relative to sp */
954 /* Get the offset of the parameter which is stored at sp+0 on function
955 * entry and check if this offset is reachable with a byte offset.
958 ArgSizeOffs = -oursp;
959 CheckLocalOffs (ArgSizeOffs);
961 /* Get the size of all parameters. */
962 ldyconst (ArgSizeOffs);
963 AddCodeLine ("lda (sp),y");
965 /* Add the value of the stackpointer */
966 if (CodeSizeFactor > 250) {
967 unsigned L = GetLocalLabel();
968 AddCodeLine ("ldx sp+1");
970 AddCodeLine ("adc sp");
971 AddCodeLine ("bcc %s", LocalLabelName (L));
975 AddCodeLine ("jsr leaasp");
978 /* Add the offset to the primary */
980 g_inc (CF_INT | CF_CONST, Offs);
981 } else if (Offs < 0) {
982 g_dec (CF_INT | CF_CONST, -Offs);
988 /*****************************************************************************/
989 /* Store into memory */
990 /*****************************************************************************/
994 void g_putstatic (unsigned flags, unsigned long label, long offs)
995 /* Store the primary register into the specified static memory cell */
997 /* Create the correct label name */
998 const char* lbuf = GetLabelName (flags, label, offs);
1000 /* Check the size and generate the correct store operation */
1001 switch (flags & CF_TYPE) {
1004 AddCodeLine ("sta %s", lbuf);
1008 AddCodeLine ("sta %s", lbuf);
1009 AddCodeLine ("stx %s+1", lbuf);
1013 AddCodeLine ("sta %s", lbuf);
1014 AddCodeLine ("stx %s+1", lbuf);
1015 AddCodeLine ("ldy sreg");
1016 AddCodeLine ("sty %s+2", lbuf);
1017 AddCodeLine ("ldy sreg+1");
1018 AddCodeLine ("sty %s+3", lbuf);
1029 void g_putlocal (unsigned Flags, int Offs, long Val)
1030 /* Put data into local object. */
1033 CheckLocalOffs (Offs);
1034 switch (Flags & CF_TYPE) {
1037 if (Flags & CF_CONST) {
1038 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1041 AddCodeLine ("sta (sp),y");
1045 if (Flags & CF_CONST) {
1047 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1048 AddCodeLine ("sta (sp),y");
1049 if ((Flags & CF_NOKEEP) == 0) {
1050 /* Place high byte into X */
1051 AddCodeLine ("tax");
1053 if ((Val & 0xFF) == Offs+1) {
1054 /* The value we need is already in Y */
1055 AddCodeLine ("tya");
1056 AddCodeLine ("dey");
1058 AddCodeLine ("dey");
1059 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1061 AddCodeLine ("sta (sp),y");
1063 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
1065 AddCodeLine ("jsr staxysp");
1068 AddCodeLine ("sta (sp),y");
1069 AddCodeLine ("iny");
1070 AddCodeLine ("txa");
1071 AddCodeLine ("sta (sp),y");
1077 if (Flags & CF_CONST) {
1078 g_getimmed (Flags, Val, 0);
1081 AddCodeLine ("jsr steaxysp");
1092 void g_putind (unsigned Flags, unsigned Offs)
1093 /* Store the specified object type in the primary register at the address
1094 * on the top of the stack
1097 /* We can handle offsets below $100 directly, larger offsets must be added
1098 * to the address. Since a/x is in use, best code is achieved by adding
1099 * just the high byte. Be sure to check if the low byte will overflow while
1102 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1104 /* Overflow - we need to add the low byte also */
1105 AddCodeLine ("ldy #$00");
1106 AddCodeLine ("clc");
1107 AddCodeLine ("pha");
1108 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1109 AddCodeLine ("adc (sp),y");
1110 AddCodeLine ("sta (sp),y");
1111 AddCodeLine ("iny");
1112 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1113 AddCodeLine ("adc (sp),y");
1114 AddCodeLine ("sta (sp),y");
1115 AddCodeLine ("pla");
1117 /* Complete address is on stack, new offset is zero */
1120 } else if ((Offs & 0xFF00) != 0) {
1122 /* We can just add the high byte */
1123 AddCodeLine ("ldy #$01");
1124 AddCodeLine ("clc");
1125 AddCodeLine ("pha");
1126 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1127 AddCodeLine ("adc (sp),y");
1128 AddCodeLine ("sta (sp),y");
1129 AddCodeLine ("pla");
1131 /* Offset is now just the low byte */
1135 /* Check the size and determine operation */
1136 switch (Flags & CF_TYPE) {
1140 AddCodeLine ("jsr staspidx");
1145 AddCodeLine ("jsr staxspidx");
1150 AddCodeLine ("jsr steaxspidx");
1158 /* Pop the argument which is always a pointer */
1164 /*****************************************************************************/
1165 /* type conversion and similiar stuff */
1166 /*****************************************************************************/
1170 void g_toslong (unsigned flags)
1171 /* Make sure, the value on TOS is a long. Convert if necessary */
1173 switch (flags & CF_TYPE) {
1177 if (flags & CF_UNSIGNED) {
1178 AddCodeLine ("jsr tosulong");
1180 AddCodeLine ("jsr toslong");
1195 void g_tosint (unsigned flags)
1196 /* Make sure, the value on TOS is an int. Convert if necessary */
1198 switch (flags & CF_TYPE) {
1205 AddCodeLine ("jsr tosint");
1216 void g_regint (unsigned Flags)
1217 /* Make sure, the value in the primary register an int. Convert if necessary */
1221 switch (Flags & CF_TYPE) {
1224 if (Flags & CF_FORCECHAR) {
1225 /* Conversion is from char */
1226 if (Flags & CF_UNSIGNED) {
1227 AddCodeLine ("ldx #$00");
1229 L = GetLocalLabel();
1230 AddCodeLine ("ldx #$00");
1231 AddCodeLine ("cmp #$80");
1232 AddCodeLine ("bcc %s", LocalLabelName (L));
1233 AddCodeLine ("dex");
1250 void g_reglong (unsigned Flags)
1251 /* Make sure, the value in the primary register a long. Convert if necessary */
1255 switch (Flags & CF_TYPE) {
1258 if (Flags & CF_FORCECHAR) {
1259 /* Conversion is from char */
1260 if (Flags & CF_UNSIGNED) {
1261 if (CodeSizeFactor >= 200) {
1262 AddCodeLine ("ldx #$00");
1263 AddCodeLine ("stx sreg");
1264 AddCodeLine ("stx sreg+1");
1266 AddCodeLine ("jsr aulong");
1269 if (CodeSizeFactor >= 366) {
1270 L = GetLocalLabel();
1271 AddCodeLine ("ldx #$00");
1272 AddCodeLine ("cmp #$80");
1273 AddCodeLine ("bcc %s", LocalLabelName (L));
1274 AddCodeLine ("dex");
1276 AddCodeLine ("stx sreg");
1277 AddCodeLine ("stx sreg+1");
1279 AddCodeLine ("jsr along");
1286 if (Flags & CF_UNSIGNED) {
1287 if (CodeSizeFactor >= 200) {
1289 AddCodeLine ("sty sreg");
1290 AddCodeLine ("sty sreg+1");
1292 AddCodeLine ("jsr axulong");
1295 AddCodeLine ("jsr axlong");
1309 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1310 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1311 * value, that corresponds to the value on TOS, rhs corresponds to the value
1312 * in (e)ax. The return value is the the flags value for the resulting type.
1315 unsigned ltype, rtype;
1318 /* Get the type spec from the flags */
1319 ltype = lhs & CF_TYPE;
1320 rtype = rhs & CF_TYPE;
1322 /* Check if a conversion is needed */
1323 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1324 /* We must promote the primary register to long */
1326 /* Get the new rhs type */
1327 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1329 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1330 /* We must promote the lhs to long */
1336 /* Get the new rhs type */
1337 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1341 /* Determine the result type for the operation:
1342 * - The result is const if both operands are const.
1343 * - The result is unsigned if one of the operands is unsigned.
1344 * - The result is long if one of the operands is long.
1345 * - Otherwise the result is int sized.
1347 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1348 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1349 if (rtype == CF_LONG || ltype == CF_LONG) {
1359 unsigned g_typecast (unsigned lhs, unsigned rhs)
1360 /* Cast the value in the primary register to the operand size that is flagged
1361 * by the lhs value. Return the result value.
1364 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 ((rhs & CF_CONST) == 0) {
1372 if (ltype == CF_LONG && rtype != CF_LONG) {
1373 /* We must promote the primary register to long */
1375 } else if (ltype == CF_INT && rtype != CF_INT) {
1376 /* We must promote the primary register to int */
1381 /* Do not need any other action. If the left type is int, and the primary
1382 * register is long, it will be automagically truncated. If the right hand
1383 * side is const, it is not located in the primary register and handled by
1384 * the expression parser code.
1387 /* Result is const if the right hand side was const */
1388 lhs |= (rhs & CF_CONST);
1390 /* The resulting type is that of the left hand side (that's why you called
1398 void g_scale (unsigned flags, long val)
1399 /* Scale the value in the primary register by the given value. If val is positive,
1400 * scale up, is val is negative, scale down. This function is used to scale
1401 * the operands or results of pointer arithmetic by the size of the type, the
1402 * pointer points to.
1407 /* Value may not be zero */
1409 Internal ("Data type has no size");
1410 } else if (val > 0) {
1413 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1415 /* Factor is 2, 4, 8 and 16, use special function */
1416 switch (flags & CF_TYPE) {
1419 if (flags & CF_FORCECHAR) {
1421 AddCodeLine ("asl a");
1428 if (CodeSizeFactor >= (p2+1)*130U) {
1429 AddCodeLine ("stx tmp1");
1431 AddCodeLine ("asl a");
1432 AddCodeLine ("rol tmp1");
1434 AddCodeLine ("ldx tmp1");
1436 if (flags & CF_UNSIGNED) {
1437 AddCodeLine ("jsr shlax%d", p2);
1439 AddCodeLine ("jsr aslax%d", p2);
1445 if (flags & CF_UNSIGNED) {
1446 AddCodeLine ("jsr shleax%d", p2);
1448 AddCodeLine ("jsr asleax%d", p2);
1457 } else if (val != 1) {
1459 /* Use a multiplication instead */
1460 g_mul (flags | CF_CONST, val);
1468 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1470 /* Factor is 2, 4, 8 and 16 use special function */
1471 switch (flags & CF_TYPE) {
1474 if (flags & CF_FORCECHAR) {
1475 if (flags & CF_UNSIGNED) {
1477 AddCodeLine ("lsr a");
1480 } else if (p2 <= 2) {
1481 AddCodeLine ("cmp #$80");
1482 AddCodeLine ("ror a");
1489 if (flags & CF_UNSIGNED) {
1490 if (CodeSizeFactor >= (p2+1)*130U) {
1491 AddCodeLine ("stx tmp1");
1493 AddCodeLine ("lsr tmp1");
1494 AddCodeLine ("ror a");
1496 AddCodeLine ("ldx tmp1");
1498 AddCodeLine ("jsr lsrax%d", p2);
1501 if (CodeSizeFactor >= (p2+1)*150U) {
1502 AddCodeLine ("stx tmp1");
1504 AddCodeLine ("cpx #$80");
1505 AddCodeLine ("ror tmp1");
1506 AddCodeLine ("ror a");
1508 AddCodeLine ("ldx tmp1");
1510 AddCodeLine ("jsr asrax%d", p2);
1516 if (flags & CF_UNSIGNED) {
1517 AddCodeLine ("jsr lsreax%d", p2);
1519 AddCodeLine ("jsr asreax%d", p2);
1528 } else if (val != 1) {
1530 /* Use a division instead */
1531 g_div (flags | CF_CONST, val);
1539 /*****************************************************************************/
1540 /* Adds and subs of variables fix a fixed address */
1541 /*****************************************************************************/
1545 void g_addlocal (unsigned flags, int offs)
1546 /* Add a local variable to ax */
1550 /* Correct the offset and check it */
1552 CheckLocalOffs (offs);
1554 switch (flags & CF_TYPE) {
1557 L = GetLocalLabel();
1558 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1559 AddCodeLine ("clc");
1560 AddCodeLine ("adc (sp),y");
1561 AddCodeLine ("bcc %s", LocalLabelName (L));
1562 AddCodeLine ("inx");
1567 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1568 AddCodeLine ("clc");
1569 AddCodeLine ("adc (sp),y");
1570 AddCodeLine ("pha");
1571 AddCodeLine ("txa");
1572 AddCodeLine ("iny");
1573 AddCodeLine ("adc (sp),y");
1574 AddCodeLine ("tax");
1575 AddCodeLine ("pla");
1579 /* Do it the old way */
1581 g_getlocal (flags, offs);
1593 void g_addstatic (unsigned flags, unsigned long label, long offs)
1594 /* Add a static variable to ax */
1598 /* Create the correct label name */
1599 const char* lbuf = GetLabelName (flags, label, offs);
1601 switch (flags & CF_TYPE) {
1604 L = GetLocalLabel();
1605 AddCodeLine ("clc");
1606 AddCodeLine ("adc %s", lbuf);
1607 AddCodeLine ("bcc %s", LocalLabelName (L));
1608 AddCodeLine ("inx");
1613 AddCodeLine ("clc");
1614 AddCodeLine ("adc %s", lbuf);
1615 AddCodeLine ("tay");
1616 AddCodeLine ("txa");
1617 AddCodeLine ("adc %s+1", lbuf);
1618 AddCodeLine ("tax");
1619 AddCodeLine ("tya");
1623 /* Do it the old way */
1625 g_getstatic (flags, label, offs);
1637 /*****************************************************************************/
1638 /* Special op= functions */
1639 /*****************************************************************************/
1643 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1645 /* Emit += for a static variable */
1647 /* Create the correct label name */
1648 const char* lbuf = GetLabelName (flags, label, offs);
1650 /* Check the size and determine operation */
1651 switch (flags & CF_TYPE) {
1654 if (flags & CF_FORCECHAR) {
1655 AddCodeLine ("ldx #$00");
1656 if (flags & CF_CONST) {
1658 AddCodeLine ("inc %s", lbuf);
1659 AddCodeLine ("lda %s", lbuf);
1661 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1662 AddCodeLine ("clc");
1663 AddCodeLine ("adc %s", lbuf);
1664 AddCodeLine ("sta %s", lbuf);
1667 AddCodeLine ("clc");
1668 AddCodeLine ("adc %s", lbuf);
1669 AddCodeLine ("sta %s", lbuf);
1671 if ((flags & CF_UNSIGNED) == 0) {
1672 unsigned L = GetLocalLabel();
1673 AddCodeLine ("bpl %s", LocalLabelName (L));
1674 AddCodeLine ("dex");
1682 if (flags & CF_CONST) {
1684 unsigned L = GetLocalLabel ();
1685 AddCodeLine ("inc %s", lbuf);
1686 AddCodeLine ("bne %s", LocalLabelName (L));
1687 AddCodeLine ("inc %s+1", lbuf);
1689 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1690 AddCodeLine ("ldx %s+1", lbuf);
1692 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1693 AddCodeLine ("clc");
1694 AddCodeLine ("adc %s", lbuf);
1695 AddCodeLine ("sta %s", lbuf);
1697 unsigned L = GetLocalLabel ();
1698 AddCodeLine ("bcc %s", LocalLabelName (L));
1699 AddCodeLine ("inc %s+1", lbuf);
1701 AddCodeLine ("ldx %s+1", lbuf);
1703 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1704 AddCodeLine ("adc %s+1", lbuf);
1705 AddCodeLine ("sta %s+1", lbuf);
1706 AddCodeLine ("tax");
1707 AddCodeLine ("lda %s", lbuf);
1711 AddCodeLine ("clc");
1712 AddCodeLine ("adc %s", lbuf);
1713 AddCodeLine ("sta %s", lbuf);
1714 AddCodeLine ("txa");
1715 AddCodeLine ("adc %s+1", lbuf);
1716 AddCodeLine ("sta %s+1", lbuf);
1717 AddCodeLine ("tax");
1718 AddCodeLine ("lda %s", lbuf);
1723 if (flags & CF_CONST) {
1725 AddCodeLine ("ldy #<(%s)", lbuf);
1726 AddCodeLine ("sty ptr1");
1727 AddCodeLine ("ldy #>(%s)", lbuf);
1729 AddCodeLine ("jsr laddeq1");
1731 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1732 AddCodeLine ("jsr laddeqa");
1735 g_getstatic (flags, label, offs);
1737 g_putstatic (flags, label, offs);
1740 AddCodeLine ("ldy #<(%s)", lbuf);
1741 AddCodeLine ("sty ptr1");
1742 AddCodeLine ("ldy #>(%s)", lbuf);
1743 AddCodeLine ("jsr laddeq");
1754 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1755 /* Emit += for a local variable */
1757 /* Calculate the true offset, check it, load it into Y */
1759 CheckLocalOffs (offs);
1761 /* Check the size and determine operation */
1762 switch (flags & CF_TYPE) {
1765 if (flags & CF_FORCECHAR) {
1767 AddCodeLine ("ldx #$00");
1768 if (flags & CF_CONST) {
1769 AddCodeLine ("clc");
1770 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1771 AddCodeLine ("adc (sp),y");
1772 AddCodeLine ("sta (sp),y");
1774 AddCodeLine ("clc");
1775 AddCodeLine ("adc (sp),y");
1776 AddCodeLine ("sta (sp),y");
1778 if ((flags & CF_UNSIGNED) == 0) {
1779 unsigned L = GetLocalLabel();
1780 AddCodeLine ("bpl %s", LocalLabelName (L));
1781 AddCodeLine ("dex");
1790 if (flags & CF_CONST) {
1791 if (CodeSizeFactor >= 400) {
1792 AddCodeLine ("clc");
1793 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1794 AddCodeLine ("adc (sp),y");
1795 AddCodeLine ("sta (sp),y");
1796 AddCodeLine ("iny");
1797 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1798 AddCodeLine ("adc (sp),y");
1799 AddCodeLine ("sta (sp),y");
1800 AddCodeLine ("tax");
1801 AddCodeLine ("dey");
1802 AddCodeLine ("lda (sp),y");
1804 g_getimmed (flags, val, 0);
1805 AddCodeLine ("jsr addeqysp");
1808 AddCodeLine ("jsr addeqysp");
1813 if (flags & CF_CONST) {
1814 g_getimmed (flags, val, 0);
1817 AddCodeLine ("jsr laddeqysp");
1827 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1828 /* Emit += for the location with address in ax */
1830 /* If the offset is too large for a byte register, add the high byte
1831 * of the offset to the primary. Beware: We need a special correction
1832 * if the offset in the low byte will overflow in the operation.
1834 offs = MakeByteOffs (flags, offs);
1836 /* Check the size and determine operation */
1837 switch (flags & CF_TYPE) {
1840 AddCodeLine ("sta ptr1");
1841 AddCodeLine ("stx ptr1+1");
1842 AddCodeLine ("ldy #$%02X", offs);
1843 AddCodeLine ("ldx #$00");
1844 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1845 AddCodeLine ("clc");
1846 AddCodeLine ("adc (ptr1),y");
1847 AddCodeLine ("sta (ptr1),y");
1851 if (CodeSizeFactor >= 200) {
1852 /* Lots of code, use only if size is not important */
1853 AddCodeLine ("sta ptr1");
1854 AddCodeLine ("stx ptr1+1");
1855 AddCodeLine ("ldy #$%02X", offs);
1856 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1857 AddCodeLine ("clc");
1858 AddCodeLine ("adc (ptr1),y");
1859 AddCodeLine ("sta (ptr1),y");
1860 AddCodeLine ("pha");
1861 AddCodeLine ("iny");
1862 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1863 AddCodeLine ("adc (ptr1),y");
1864 AddCodeLine ("sta (ptr1),y");
1865 AddCodeLine ("tax");
1866 AddCodeLine ("pla");
1872 AddCodeLine ("jsr pushax"); /* Push the address */
1873 push (CF_PTR); /* Correct the internal sp */
1874 g_getind (flags, offs); /* Fetch the value */
1875 g_inc (flags, val); /* Increment value in primary */
1876 g_putind (flags, offs); /* Store the value back */
1886 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1888 /* Emit -= for a static variable */
1890 /* Create the correct label name */
1891 const char* lbuf = GetLabelName (flags, label, offs);
1893 /* Check the size and determine operation */
1894 switch (flags & CF_TYPE) {
1897 if (flags & CF_FORCECHAR) {
1898 AddCodeLine ("ldx #$00");
1899 if (flags & CF_CONST) {
1901 AddCodeLine ("dec %s", lbuf);
1902 AddCodeLine ("lda %s", lbuf);
1904 AddCodeLine ("sec");
1905 AddCodeLine ("lda %s", lbuf);
1906 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1907 AddCodeLine ("sta %s", lbuf);
1910 AddCodeLine ("eor #$FF");
1911 AddCodeLine ("sec");
1912 AddCodeLine ("adc %s", lbuf);
1913 AddCodeLine ("sta %s", lbuf);
1915 if ((flags & CF_UNSIGNED) == 0) {
1916 unsigned L = GetLocalLabel();
1917 AddCodeLine ("bpl %s", LocalLabelName (L));
1918 AddCodeLine ("dex");
1926 AddCodeLine ("sec");
1927 if (flags & CF_CONST) {
1928 AddCodeLine ("lda %s", lbuf);
1929 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1930 AddCodeLine ("sta %s", lbuf);
1932 unsigned L = GetLocalLabel ();
1933 AddCodeLine ("bcs %s", LocalLabelName (L));
1934 AddCodeLine ("dec %s+1", lbuf);
1936 AddCodeLine ("ldx %s+1", lbuf);
1938 AddCodeLine ("lda %s+1", lbuf);
1939 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1940 AddCodeLine ("sta %s+1", lbuf);
1941 AddCodeLine ("tax");
1942 AddCodeLine ("lda %s", lbuf);
1945 AddCodeLine ("eor #$FF");
1946 AddCodeLine ("adc %s", lbuf);
1947 AddCodeLine ("sta %s", lbuf);
1948 AddCodeLine ("txa");
1949 AddCodeLine ("eor #$FF");
1950 AddCodeLine ("adc %s+1", lbuf);
1951 AddCodeLine ("sta %s+1", lbuf);
1952 AddCodeLine ("tax");
1953 AddCodeLine ("lda %s", lbuf);
1958 if (flags & CF_CONST) {
1960 AddCodeLine ("ldy #<(%s)", lbuf);
1961 AddCodeLine ("sty ptr1");
1962 AddCodeLine ("ldy #>(%s)", lbuf);
1963 AddCodeLine ("lda #$%02X", (unsigned char)val);
1964 AddCodeLine ("jsr lsubeqa");
1966 g_getstatic (flags, label, offs);
1968 g_putstatic (flags, label, offs);
1971 AddCodeLine ("ldy #<(%s)", lbuf);
1972 AddCodeLine ("sty ptr1");
1973 AddCodeLine ("ldy #>(%s)", lbuf);
1974 AddCodeLine ("jsr lsubeq");
1985 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1986 /* Emit -= for a local variable */
1988 /* Calculate the true offset, check it, load it into Y */
1990 CheckLocalOffs (offs);
1992 /* Check the size and determine operation */
1993 switch (flags & CF_TYPE) {
1996 if (flags & CF_FORCECHAR) {
1998 AddCodeLine ("ldx #$00");
1999 AddCodeLine ("sec");
2000 if (flags & CF_CONST) {
2001 AddCodeLine ("lda (sp),y");
2002 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2004 AddCodeLine ("eor #$FF");
2005 AddCodeLine ("adc (sp),y");
2007 AddCodeLine ("sta (sp),y");
2008 if ((flags & CF_UNSIGNED) == 0) {
2009 unsigned L = GetLocalLabel();
2010 AddCodeLine ("bpl %s", LocalLabelName (L));
2011 AddCodeLine ("dex");
2019 if (flags & CF_CONST) {
2020 g_getimmed (flags, val, 0);
2023 AddCodeLine ("jsr subeqysp");
2027 if (flags & CF_CONST) {
2028 g_getimmed (flags, val, 0);
2031 AddCodeLine ("jsr lsubeqysp");
2041 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2042 /* Emit -= for the location with address in ax */
2044 /* If the offset is too large for a byte register, add the high byte
2045 * of the offset to the primary. Beware: We need a special correction
2046 * if the offset in the low byte will overflow in the operation.
2048 offs = MakeByteOffs (flags, offs);
2050 /* Check the size and determine operation */
2051 switch (flags & CF_TYPE) {
2054 AddCodeLine ("sta ptr1");
2055 AddCodeLine ("stx ptr1+1");
2056 AddCodeLine ("ldy #$%02X", offs);
2057 AddCodeLine ("ldx #$00");
2058 AddCodeLine ("lda (ptr1),y");
2059 AddCodeLine ("sec");
2060 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2061 AddCodeLine ("sta (ptr1),y");
2065 if (CodeSizeFactor >= 200) {
2066 /* Lots of code, use only if size is not important */
2067 AddCodeLine ("sta ptr1");
2068 AddCodeLine ("stx ptr1+1");
2069 AddCodeLine ("ldy #$%02X", offs);
2070 AddCodeLine ("lda (ptr1),y");
2071 AddCodeLine ("sec");
2072 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2073 AddCodeLine ("sta (ptr1),y");
2074 AddCodeLine ("pha");
2075 AddCodeLine ("iny");
2076 AddCodeLine ("lda (ptr1),y");
2077 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2078 AddCodeLine ("sta (ptr1),y");
2079 AddCodeLine ("tax");
2080 AddCodeLine ("pla");
2086 AddCodeLine ("jsr pushax"); /* Push the address */
2087 push (CF_PTR); /* Correct the internal sp */
2088 g_getind (flags, offs); /* Fetch the value */
2089 g_dec (flags, val); /* Increment value in primary */
2090 g_putind (flags, offs); /* Store the value back */
2100 /*****************************************************************************/
2101 /* Add a variable address to the value in ax */
2102 /*****************************************************************************/
2106 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2107 /* Add the address of a local variable to ax */
2111 /* Add the offset */
2114 /* We cannot address more then 256 bytes of locals anyway */
2115 L = GetLocalLabel();
2116 CheckLocalOffs (offs);
2117 AddCodeLine ("clc");
2118 AddCodeLine ("adc #$%02X", offs & 0xFF);
2119 /* Do also skip the CLC insn below */
2120 AddCodeLine ("bcc %s", LocalLabelName (L));
2121 AddCodeLine ("inx");
2124 /* Add the current stackpointer value */
2125 AddCodeLine ("clc");
2127 /* Label was used above */
2130 AddCodeLine ("adc sp");
2131 AddCodeLine ("tay");
2132 AddCodeLine ("txa");
2133 AddCodeLine ("adc sp+1");
2134 AddCodeLine ("tax");
2135 AddCodeLine ("tya");
2140 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2141 /* Add the address of a static variable to ax */
2143 /* Create the correct label name */
2144 const char* lbuf = GetLabelName (flags, label, offs);
2146 /* Add the address to the current ax value */
2147 AddCodeLine ("clc");
2148 AddCodeLine ("adc #<(%s)", lbuf);
2149 AddCodeLine ("tay");
2150 AddCodeLine ("txa");
2151 AddCodeLine ("adc #>(%s)", lbuf);
2152 AddCodeLine ("tax");
2153 AddCodeLine ("tya");
2158 /*****************************************************************************/
2160 /*****************************************************************************/
2164 void g_save (unsigned flags)
2165 /* Copy primary register to hold register. */
2167 /* Check the size and determine operation */
2168 switch (flags & CF_TYPE) {
2171 if (flags & CF_FORCECHAR) {
2172 AddCodeLine ("pha");
2178 AddCodeLine ("sta regsave");
2179 AddCodeLine ("stx regsave+1");
2183 AddCodeLine ("jsr saveeax");
2193 void g_restore (unsigned flags)
2194 /* Copy hold register to primary. */
2196 /* Check the size and determine operation */
2197 switch (flags & CF_TYPE) {
2200 if (flags & CF_FORCECHAR) {
2201 AddCodeLine ("pla");
2207 AddCodeLine ("lda regsave");
2208 AddCodeLine ("ldx regsave+1");
2212 AddCodeLine ("jsr resteax");
2222 void g_cmp (unsigned flags, unsigned long val)
2223 /* Immidiate compare. The primary register will not be changed, Z flag
2229 /* Check the size and determine operation */
2230 switch (flags & CF_TYPE) {
2233 if (flags & CF_FORCECHAR) {
2234 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2240 L = GetLocalLabel();
2241 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2242 AddCodeLine ("bne %s", LocalLabelName (L));
2243 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2248 Internal ("g_cmp: Long compares not implemented");
2258 static void oper (unsigned flags, unsigned long val, char** subs)
2259 /* Encode a binary operation. subs is a pointer to four groups of three
2261 * 0-2 --> Operate on ints
2262 * 3-5 --> Operate on unsigneds
2263 * 6-8 --> Operate on longs
2264 * 9-11 --> Operate on unsigned longs
2266 * The first subroutine names in each string group is used to encode an
2267 * operation with a zero constant, the second to encode an operation with
2268 * a 8 bit constant, and the third is used in all other cases.
2273 /* Determine the offset into the array */
2274 offs = (flags & CF_UNSIGNED)? 3 : 0;
2275 switch (flags & CF_TYPE) {
2288 /* Encode the operation */
2289 if (flags & CF_CONST) {
2290 /* Constant value given */
2291 if (val == 0 && subs [offs+0]) {
2292 /* Special case: constant with value zero */
2293 AddCodeLine ("jsr %s", subs [offs+0]);
2294 } else if (val < 0x100 && subs [offs+1]) {
2295 /* Special case: constant with high byte zero */
2296 ldaconst (val); /* Load low byte */
2297 AddCodeLine ("jsr %s", subs [offs+1]);
2299 /* Others: arbitrary constant value */
2300 g_getimmed (flags, val, 0); /* Load value */
2301 AddCodeLine ("jsr %s", subs [offs+2]);
2304 /* Value not constant (is already in (e)ax) */
2305 AddCodeLine ("jsr %s", subs [offs+2]);
2308 /* The operation will pop it's argument */
2314 void g_test (unsigned flags)
2315 /* Test the value in the primary and set the condition codes */
2317 switch (flags & CF_TYPE) {
2320 if (flags & CF_FORCECHAR) {
2321 AddCodeLine ("tax");
2327 AddCodeLine ("stx tmp1");
2328 AddCodeLine ("ora tmp1");
2332 if (flags & CF_UNSIGNED) {
2333 AddCodeLine ("jsr utsteax");
2335 AddCodeLine ("jsr tsteax");
2347 void g_push (unsigned flags, unsigned long val)
2348 /* Push the primary register or a constant value onto the stack */
2350 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2352 /* We have a constant 8 or 16 bit value */
2353 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2355 /* Handle as 8 bit value */
2357 AddCodeLine ("jsr pusha");
2361 /* Handle as 16 bit value */
2362 g_getimmed (flags, val, 0);
2363 AddCodeLine ("jsr pushax");
2368 /* Value is not 16 bit or not constant */
2369 if (flags & CF_CONST) {
2370 /* Constant 32 bit value, load into eax */
2371 g_getimmed (flags, val, 0);
2374 /* Push the primary register */
2375 switch (flags & CF_TYPE) {
2378 if (flags & CF_FORCECHAR) {
2379 /* Handle as char */
2380 AddCodeLine ("jsr pusha");
2385 AddCodeLine ("jsr pushax");
2389 AddCodeLine ("jsr pusheax");
2399 /* Adjust the stack offset */
2405 void g_swap (unsigned flags)
2406 /* Swap the primary register and the top of the stack. flags give the type
2407 * of *both* values (must have same size).
2410 switch (flags & CF_TYPE) {
2414 AddCodeLine ("jsr swapstk");
2418 AddCodeLine ("jsr swapestk");
2429 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2430 /* Call the specified subroutine name */
2432 if ((Flags & CF_FIXARGC) == 0) {
2433 /* Pass the argument count */
2436 AddCodeLine ("jsr _%s", Label);
2437 oursp += ArgSize; /* callee pops args */
2442 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2443 /* Call subroutine indirect */
2445 if ((Flags & CF_LOCAL) == 0) {
2446 /* Address is in a/x */
2447 if ((Flags & CF_FIXARGC) == 0) {
2448 /* Pass arg count */
2451 AddCodeLine ("jsr callax");
2453 /* The address is on stack, offset is on Val */
2455 CheckLocalOffs (Offs);
2456 AddCodeLine ("pha");
2457 AddCodeLine ("ldy #$%02X", Offs);
2458 AddCodeLine ("lda (sp),y");
2459 AddCodeLine ("sta jmpvec+1");
2460 AddCodeLine ("iny");
2461 AddCodeLine ("lda (sp),y");
2462 AddCodeLine ("sta jmpvec+2");
2463 AddCodeLine ("pla");
2464 AddCodeLine ("jsr jmpvec");
2467 /* Callee pops args */
2473 void g_jump (unsigned Label)
2474 /* Jump to specified internal label number */
2476 AddCodeLine ("jmp %s", LocalLabelName (Label));
2481 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2482 /* Jump to label if zero flag clear */
2484 AddCodeLine ("jne %s", LocalLabelName (label));
2489 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2490 /* Jump to label if zero flag set */
2492 AddCodeLine ("jeq %s", LocalLabelName (label));
2497 static void mod_internal (int k, char* verb1, char* verb2)
2500 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2504 AddCodeLine ("jsr %ssp", verb2);
2510 void g_space (int space)
2511 /* Create or drop space on the stack */
2514 mod_internal (-space, "inc", "addy");
2515 } else if (space > 0) {
2516 mod_internal (space, "dec", "suby");
2522 void g_cstackcheck (void)
2523 /* Check for a C stack overflow */
2525 AddCodeLine ("jsr cstkchk");
2530 void g_stackcheck (void)
2531 /* Check for a stack overflow */
2533 AddCodeLine ("jsr stkchk");
2538 void g_add (unsigned flags, unsigned long val)
2539 /* Primary = TOS + Primary */
2541 static char* ops [12] = {
2542 0, "tosadda0", "tosaddax",
2543 0, "tosadda0", "tosaddax",
2548 if (flags & CF_CONST) {
2549 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2550 g_push (flags & ~CF_CONST, 0);
2552 oper (flags, val, ops);
2557 void g_sub (unsigned flags, unsigned long val)
2558 /* Primary = TOS - Primary */
2560 static char* ops [12] = {
2561 0, "tossuba0", "tossubax",
2562 0, "tossuba0", "tossubax",
2567 if (flags & CF_CONST) {
2568 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2569 g_push (flags & ~CF_CONST, 0);
2571 oper (flags, val, ops);
2576 void g_rsub (unsigned flags, unsigned long val)
2577 /* Primary = Primary - TOS */
2579 static char* ops [12] = {
2580 0, "tosrsuba0", "tosrsubax",
2581 0, "tosrsuba0", "tosrsubax",
2585 oper (flags, val, ops);
2590 void g_mul (unsigned flags, unsigned long val)
2591 /* Primary = TOS * Primary */
2593 static char* ops [12] = {
2594 0, "tosmula0", "tosmulax",
2595 0, "tosumula0", "tosumulax",
2602 /* Do strength reduction if the value is constant and a power of two */
2603 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2604 /* Generate a shift instead */
2609 /* If the right hand side is const, the lhs is not on stack but still
2610 * in the primary register.
2612 if (flags & CF_CONST) {
2614 switch (flags & CF_TYPE) {
2617 if (flags & CF_FORCECHAR) {
2618 /* Handle some special cases */
2622 AddCodeLine ("sta tmp1");
2623 AddCodeLine ("asl a");
2624 AddCodeLine ("clc");
2625 AddCodeLine ("adc tmp1");
2629 AddCodeLine ("sta tmp1");
2630 AddCodeLine ("asl a");
2631 AddCodeLine ("asl a");
2632 AddCodeLine ("clc");
2633 AddCodeLine ("adc tmp1");
2637 AddCodeLine ("sta tmp1");
2638 AddCodeLine ("asl a");
2639 AddCodeLine ("clc");
2640 AddCodeLine ("adc tmp1");
2641 AddCodeLine ("asl a");
2645 AddCodeLine ("sta tmp1");
2646 AddCodeLine ("asl a");
2647 AddCodeLine ("asl a");
2648 AddCodeLine ("clc");
2649 AddCodeLine ("adc tmp1");
2650 AddCodeLine ("asl a");
2659 AddCodeLine ("jsr mulax3");
2662 AddCodeLine ("jsr mulax5");
2665 AddCodeLine ("jsr mulax6");
2668 AddCodeLine ("jsr mulax7");
2671 AddCodeLine ("jsr mulax9");
2674 AddCodeLine ("jsr mulax10");
2686 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2687 * into the normal, non-optimized stuff.
2689 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2690 g_push (flags & ~CF_CONST, 0);
2694 /* Use long way over the stack */
2695 oper (flags, val, ops);
2700 void g_div (unsigned flags, unsigned long val)
2701 /* Primary = TOS / Primary */
2703 static char* ops [12] = {
2704 0, "tosdiva0", "tosdivax",
2705 0, "tosudiva0", "tosudivax",
2710 /* Do strength reduction if the value is constant and a power of two */
2712 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2713 /* Generate a shift instead */
2716 /* Generate a division */
2717 if (flags & CF_CONST) {
2718 /* lhs is not on stack */
2719 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2720 g_push (flags & ~CF_CONST, 0);
2722 oper (flags, val, ops);
2728 void g_mod (unsigned flags, unsigned long val)
2729 /* Primary = TOS % Primary */
2731 static char* ops [12] = {
2732 0, "tosmoda0", "tosmodax",
2733 0, "tosumoda0", "tosumodax",
2739 /* Check if we can do some cost reduction */
2740 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2741 /* We can do that with an AND operation */
2742 g_and (flags, val - 1);
2744 /* Do it the hard way... */
2745 if (flags & CF_CONST) {
2746 /* lhs is not on stack */
2747 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2748 g_push (flags & ~CF_CONST, 0);
2750 oper (flags, val, ops);
2756 void g_or (unsigned flags, unsigned long val)
2757 /* Primary = TOS | Primary */
2759 static char* ops [12] = {
2760 0, "tosora0", "tosorax",
2761 0, "tosora0", "tosorax",
2766 /* If the right hand side is const, the lhs is not on stack but still
2767 * in the primary register.
2769 if (flags & CF_CONST) {
2771 switch (flags & CF_TYPE) {
2774 if (flags & CF_FORCECHAR) {
2775 if ((val & 0xFF) != 0xFF) {
2776 AddCodeLine ("ora #$%02X", (unsigned char)val);
2784 AddCodeLine ("ora #$%02X", (unsigned char)val);
2791 AddCodeLine ("ora #$%02X", (unsigned char)val);
2800 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2801 * into the normal, non-optimized stuff. Note: The standard stuff will
2802 * always work with ints.
2804 flags &= ~CF_FORCECHAR;
2805 g_push (flags & ~CF_CONST, 0);
2808 /* Use long way over the stack */
2809 oper (flags, val, ops);
2814 void g_xor (unsigned flags, unsigned long val)
2815 /* Primary = TOS ^ Primary */
2817 static char* ops [12] = {
2818 0, "tosxora0", "tosxorax",
2819 0, "tosxora0", "tosxorax",
2825 /* If the right hand side is const, the lhs is not on stack but still
2826 * in the primary register.
2828 if (flags & CF_CONST) {
2830 switch (flags & CF_TYPE) {
2833 if (flags & CF_FORCECHAR) {
2834 if ((val & 0xFF) != 0) {
2835 AddCodeLine ("eor #$%02X", (unsigned char)val);
2844 AddCodeLine ("eor #$%02X", (unsigned char)val);
2847 } else if ((val & 0xFF) == 0) {
2848 AddCodeLine ("pha");
2849 AddCodeLine ("txa");
2850 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2851 AddCodeLine ("tax");
2852 AddCodeLine ("pla");
2860 AddCodeLine ("eor #$%02X", (unsigned char)val);
2870 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2871 * into the normal, non-optimized stuff. Note: The standard stuff will
2872 * always work with ints.
2874 flags &= ~CF_FORCECHAR;
2875 g_push (flags & ~CF_CONST, 0);
2878 /* Use long way over the stack */
2879 oper (flags, val, ops);
2884 void g_and (unsigned flags, unsigned long val)
2885 /* Primary = TOS & Primary */
2887 static char* ops [12] = {
2888 0, "tosanda0", "tosandax",
2889 0, "tosanda0", "tosandax",
2894 /* If the right hand side is const, the lhs is not on stack but still
2895 * in the primary register.
2897 if (flags & CF_CONST) {
2899 switch (flags & CF_TYPE) {
2902 if (flags & CF_FORCECHAR) {
2903 AddCodeLine ("and #$%02X", (unsigned char)val);
2908 if ((val & 0xFFFF) != 0xFFFF) {
2913 } else if (val != 0xFF) {
2914 AddCodeLine ("and #$%02X", (unsigned char)val);
2916 } else if ((val & 0xFF00) == 0xFF00) {
2917 AddCodeLine ("and #$%02X", (unsigned char)val);
2918 } else if ((val & 0x00FF) == 0x0000) {
2919 AddCodeLine ("txa");
2920 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2921 AddCodeLine ("tax");
2924 AddCodeLine ("tay");
2925 AddCodeLine ("txa");
2926 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2927 AddCodeLine ("tax");
2928 AddCodeLine ("tya");
2929 if ((val & 0x00FF) != 0x00FF) {
2930 AddCodeLine ("and #$%02X", (unsigned char)val);
2939 AddCodeLine ("stx sreg+1");
2940 AddCodeLine ("stx sreg");
2941 if ((val & 0xFF) != 0xFF) {
2942 AddCodeLine ("and #$%02X", (unsigned char)val);
2945 } else if (val == 0xFF00) {
2947 AddCodeLine ("sta sreg+1");
2948 AddCodeLine ("sta sreg");
2957 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2958 * into the normal, non-optimized stuff. Note: The standard stuff will
2959 * always work with ints.
2961 flags &= ~CF_FORCECHAR;
2962 g_push (flags & ~CF_CONST, 0);
2965 /* Use long way over the stack */
2966 oper (flags, val, ops);
2971 void g_asr (unsigned flags, unsigned long val)
2972 /* Primary = TOS >> Primary */
2974 static char* ops [12] = {
2975 0, "tosasra0", "tosasrax",
2976 0, "tosshra0", "tosshrax",
2981 /* If the right hand side is const, the lhs is not on stack but still
2982 * in the primary register.
2984 if (flags & CF_CONST) {
2986 switch (flags & CF_TYPE) {
2990 if (val >= 8 && (flags & CF_UNSIGNED)) {
2991 AddCodeLine ("txa");
2998 } else if (val >= 1 && val <= 4) {
2999 if (flags & CF_UNSIGNED) {
3000 AddCodeLine ("jsr shrax%ld", val);
3002 AddCodeLine ("jsr asrax%ld", val);
3012 } else if (val >= 1 && val <= 4) {
3013 if (flags & CF_UNSIGNED) {
3014 AddCodeLine ("jsr shreax%ld", val);
3016 AddCodeLine ("jsr asreax%ld", val);
3019 } else if (val == 8 && (flags & CF_UNSIGNED)) {
3020 AddCodeLine ("txa");
3021 AddCodeLine ("ldx sreg");
3022 AddCodeLine ("ldy sreg+1");
3023 AddCodeLine ("sty sreg");
3024 AddCodeLine ("ldy #$00");
3025 AddCodeLine ("sty sreg+1");
3027 } else if (val == 16) {
3028 AddCodeLine ("ldy #$00");
3029 AddCodeLine ("ldx sreg+1");
3030 if ((flags & CF_UNSIGNED) == 0) {
3031 unsigned L = GetLocalLabel();
3032 AddCodeLine ("bpl %s", LocalLabelName (L));
3033 AddCodeLine ("dey");
3036 AddCodeLine ("lda sreg");
3037 AddCodeLine ("sty sreg+1");
3038 AddCodeLine ("sty sreg");
3047 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3048 * into the normal, non-optimized stuff. Note: The standard stuff will
3049 * always work with ints.
3051 flags &= ~CF_FORCECHAR;
3052 g_push (flags & ~CF_CONST, 0);
3055 /* Use long way over the stack */
3056 oper (flags, val, ops);
3061 void g_asl (unsigned flags, unsigned long val)
3062 /* Primary = TOS << Primary */
3064 static char* ops [12] = {
3065 0, "tosasla0", "tosaslax",
3066 0, "tosshla0", "tosshlax",
3072 /* If the right hand side is const, the lhs is not on stack but still
3073 * in the primary register.
3075 if (flags & CF_CONST) {
3077 switch (flags & CF_TYPE) {
3082 AddCodeLine ("tax");
3083 AddCodeLine ("lda #$00");
3089 } else if (val >= 1 && val <= 4) {
3090 if (flags & CF_UNSIGNED) {
3091 AddCodeLine ("jsr shlax%ld", val);
3093 AddCodeLine ("jsr aslax%ld", val);
3103 } else if (val >= 1 && val <= 4) {
3104 if (flags & CF_UNSIGNED) {
3105 AddCodeLine ("jsr shleax%ld", val);
3107 AddCodeLine ("jsr asleax%ld", val);
3110 } else if (val == 8) {
3111 AddCodeLine ("ldy sreg");
3112 AddCodeLine ("sty sreg+1");
3113 AddCodeLine ("stx sreg");
3114 AddCodeLine ("tax");
3115 AddCodeLine ("lda #$00");
3117 } else if (val == 16) {
3118 AddCodeLine ("stx sreg+1");
3119 AddCodeLine ("sta sreg");
3120 AddCodeLine ("lda #$00");
3121 AddCodeLine ("tax");
3130 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3131 * into the normal, non-optimized stuff. Note: The standard stuff will
3132 * always work with ints.
3134 flags &= ~CF_FORCECHAR;
3135 g_push (flags & ~CF_CONST, 0);
3138 /* Use long way over the stack */
3139 oper (flags, val, ops);
3144 void g_neg (unsigned Flags)
3145 /* Primary = -Primary */
3147 switch (Flags & CF_TYPE) {
3150 if (Flags & CF_FORCECHAR) {
3151 AddCodeLine ("eor #$FF");
3152 AddCodeLine ("clc");
3153 AddCodeLine ("adc #$01");
3159 AddCodeLine ("jsr negax");
3163 AddCodeLine ("jsr negeax");
3173 void g_bneg (unsigned flags)
3174 /* Primary = !Primary */
3176 switch (flags & CF_TYPE) {
3179 AddCodeLine ("jsr bnega");
3183 AddCodeLine ("jsr bnegax");
3187 AddCodeLine ("jsr bnegeax");
3197 void g_com (unsigned Flags)
3198 /* Primary = ~Primary */
3200 switch (Flags & CF_TYPE) {
3203 if (Flags & CF_FORCECHAR) {
3204 AddCodeLine ("eor #$FF");
3210 AddCodeLine ("jsr complax");
3214 AddCodeLine ("jsr compleax");
3224 void g_inc (unsigned flags, unsigned long val)
3225 /* Increment the primary register by a given number */
3227 /* Don't inc by zero */
3232 /* Generate code for the supported types */
3234 switch (flags & CF_TYPE) {
3237 if (flags & CF_FORCECHAR) {
3238 if (CPU == CPU_65C02 && val <= 2) {
3240 AddCodeLine ("ina");
3243 AddCodeLine ("clc");
3244 AddCodeLine ("adc #$%02X", (unsigned char)val);
3251 if (CPU == CPU_65C02 && val == 1) {
3252 unsigned L = GetLocalLabel();
3253 AddCodeLine ("ina");
3254 AddCodeLine ("bne %s", LocalLabelName (L));
3255 AddCodeLine ("inx");
3257 } else if (CodeSizeFactor < 200) {
3260 AddCodeLine ("jsr incax%lu", val);
3261 } else if (val <= 255) {
3263 AddCodeLine ("jsr incaxy");
3265 g_add (flags | CF_CONST, val);
3268 /* Inline the code */
3270 if ((val & 0xFF) != 0) {
3271 unsigned L = GetLocalLabel();
3272 AddCodeLine ("clc");
3273 AddCodeLine ("adc #$%02X", (unsigned char) val);
3274 AddCodeLine ("bcc %s", LocalLabelName (L));
3275 AddCodeLine ("inx");
3279 AddCodeLine ("inx");
3282 AddCodeLine ("inx");
3285 AddCodeLine ("inx");
3288 AddCodeLine ("clc");
3289 if ((val & 0xFF) != 0) {
3290 AddCodeLine ("adc #$%02X", (unsigned char) val);
3292 AddCodeLine ("pha");
3293 AddCodeLine ("txa");
3294 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3295 AddCodeLine ("tax");
3296 AddCodeLine ("pla");
3304 AddCodeLine ("jsr inceaxy");
3306 g_add (flags | CF_CONST, val);
3318 void g_dec (unsigned flags, unsigned long val)
3319 /* Decrement the primary register by a given number */
3321 /* Don't dec by zero */
3326 /* Generate code for the supported types */
3328 switch (flags & CF_TYPE) {
3331 if (flags & CF_FORCECHAR) {
3332 if (CPU == CPU_65C02 && val <= 2) {
3334 AddCodeLine ("dea");
3337 AddCodeLine ("sec");
3338 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3345 if (CodeSizeFactor < 200) {
3346 /* Use subroutines */
3348 AddCodeLine ("jsr decax%d", (int) val);
3349 } else if (val <= 255) {
3351 AddCodeLine ("jsr decaxy");
3353 g_sub (flags | CF_CONST, val);
3356 /* Inline the code */
3358 if ((val & 0xFF) != 0) {
3359 unsigned L = GetLocalLabel();
3360 AddCodeLine ("sec");
3361 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3362 AddCodeLine ("bcs %s", LocalLabelName (L));
3363 AddCodeLine ("dex");
3367 AddCodeLine ("dex");
3370 AddCodeLine ("dex");
3373 AddCodeLine ("sec");
3374 if ((val & 0xFF) != 0) {
3375 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3377 AddCodeLine ("pha");
3378 AddCodeLine ("txa");
3379 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3380 AddCodeLine ("tax");
3381 AddCodeLine ("pla");
3389 AddCodeLine ("jsr deceaxy");
3391 g_sub (flags | CF_CONST, val);
3404 * Following are the conditional operators. They compare the TOS against
3405 * the primary and put a literal 1 in the primary if the condition is
3406 * true, otherwise they clear the primary register
3411 void g_eq (unsigned flags, unsigned long val)
3412 /* Test for equal */
3414 static char* ops [12] = {
3415 "toseq00", "toseqa0", "toseqax",
3416 "toseq00", "toseqa0", "toseqax",
3423 /* If the right hand side is const, the lhs is not on stack but still
3424 * in the primary register.
3426 if (flags & CF_CONST) {
3428 switch (flags & CF_TYPE) {
3431 if (flags & CF_FORCECHAR) {
3432 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3433 AddCodeLine ("jsr booleq");
3439 L = GetLocalLabel();
3440 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3441 AddCodeLine ("bne %s", LocalLabelName (L));
3442 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3444 AddCodeLine ("jsr booleq");
3454 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3455 * into the normal, non-optimized stuff. Note: The standard stuff will
3456 * always work with ints.
3458 flags &= ~CF_FORCECHAR;
3459 g_push (flags & ~CF_CONST, 0);
3462 /* Use long way over the stack */
3463 oper (flags, val, ops);
3468 void g_ne (unsigned flags, unsigned long val)
3469 /* Test for not equal */
3471 static char* ops [12] = {
3472 "tosne00", "tosnea0", "tosneax",
3473 "tosne00", "tosnea0", "tosneax",
3480 /* If the right hand side is const, the lhs is not on stack but still
3481 * in the primary register.
3483 if (flags & CF_CONST) {
3485 switch (flags & CF_TYPE) {
3488 if (flags & CF_FORCECHAR) {
3489 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3490 AddCodeLine ("jsr boolne");
3496 L = GetLocalLabel();
3497 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3498 AddCodeLine ("bne %s", LocalLabelName (L));
3499 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3501 AddCodeLine ("jsr boolne");
3511 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3512 * into the normal, non-optimized stuff. Note: The standard stuff will
3513 * always work with ints.
3515 flags &= ~CF_FORCECHAR;
3516 g_push (flags & ~CF_CONST, 0);
3519 /* Use long way over the stack */
3520 oper (flags, val, ops);
3525 void g_lt (unsigned flags, unsigned long val)
3526 /* Test for less than */
3528 static char* ops [12] = {
3529 "toslt00", "toslta0", "tosltax",
3530 "tosult00", "tosulta0", "tosultax",
3535 /* If the right hand side is const, the lhs is not on stack but still
3536 * in the primary register.
3538 if (flags & CF_CONST) {
3540 /* Because the handling of the overflow flag is too complex for
3541 * inlining, we can handle only unsigned compares here.
3543 if (flags & CF_UNSIGNED) {
3545 /* Give a warning in some special cases */
3547 Warning ("Condition is never true");
3548 AddCodeLine ("jsr return0");
3552 /* Look at the type */
3553 switch (flags & CF_TYPE) {
3556 if (flags & CF_FORCECHAR) {
3557 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3558 AddCodeLine ("jsr boolult");
3564 /* If the low byte is zero, we must only test the high byte */
3565 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3566 if ((val & 0xFF) != 0) {
3567 unsigned L = GetLocalLabel();
3568 AddCodeLine ("bne %s", LocalLabelName (L));
3569 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3572 AddCodeLine ("jsr boolult");
3576 /* Do a subtraction */
3577 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3578 AddCodeLine ("txa");
3579 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3580 AddCodeLine ("lda sreg");
3581 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3582 AddCodeLine ("lda sreg+1");
3583 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3584 AddCodeLine ("jsr boolult");
3593 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3594 * into the normal, non-optimized stuff. Note: The standard stuff will
3595 * always work with ints.
3597 flags &= ~CF_FORCECHAR;
3598 g_push (flags & ~CF_CONST, 0);
3601 /* Use long way over the stack */
3602 oper (flags, val, ops);
3607 void g_le (unsigned flags, unsigned long val)
3608 /* Test for less than or equal to */
3610 static char* ops [12] = {
3611 "tosle00", "toslea0", "tosleax",
3612 "tosule00", "tosulea0", "tosuleax",
3618 /* If the right hand side is const, the lhs is not on stack but still
3619 * in the primary register.
3621 if (flags & CF_CONST) {
3623 /* Look at the type */
3624 switch (flags & CF_TYPE) {
3627 if (flags & CF_FORCECHAR) {
3628 if (flags & CF_UNSIGNED) {
3629 /* Unsigned compare */
3631 /* Use < instead of <= because the former gives
3632 * better code on the 6502 than the latter.
3634 g_lt (flags, val+1);
3637 Warning ("Condition is always true");
3638 AddCodeLine ("jsr return1");
3641 /* Signed compare */
3642 if ((long) val < 0x7F) {
3643 /* Use < instead of <= because the former gives
3644 * better code on the 6502 than the latter.
3646 g_lt (flags, val+1);
3649 Warning ("Condition is always true");
3650 AddCodeLine ("jsr return1");
3658 if (flags & CF_UNSIGNED) {
3659 /* Unsigned compare */
3661 /* Use < instead of <= because the former gives
3662 * better code on the 6502 than the latter.
3664 g_lt (flags, val+1);
3667 Warning ("Condition is always true");
3668 AddCodeLine ("jsr return1");
3671 /* Signed compare */
3672 if ((long) val < 0x7FFF) {
3673 g_lt (flags, val+1);
3676 Warning ("Condition is always true");
3677 AddCodeLine ("jsr return1");
3683 if (flags & CF_UNSIGNED) {
3684 /* Unsigned compare */
3685 if (val < 0xFFFFFFFF) {
3686 /* Use < instead of <= because the former gives
3687 * better code on the 6502 than the latter.
3689 g_lt (flags, val+1);
3692 Warning ("Condition is always true");
3693 AddCodeLine ("jsr return1");
3696 /* Signed compare */
3697 if ((long) val < 0x7FFFFFFF) {
3698 g_lt (flags, val+1);
3701 Warning ("Condition is always true");
3702 AddCodeLine ("jsr return1");
3711 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3712 * into the normal, non-optimized stuff. Note: The standard stuff will
3713 * always work with ints.
3715 flags &= ~CF_FORCECHAR;
3716 g_push (flags & ~CF_CONST, 0);
3719 /* Use long way over the stack */
3720 oper (flags, val, ops);
3725 void g_gt (unsigned flags, unsigned long val)
3726 /* Test for greater than */
3728 static char* ops [12] = {
3729 "tosgt00", "tosgta0", "tosgtax",
3730 "tosugt00", "tosugta0", "tosugtax",
3736 /* If the right hand side is const, the lhs is not on stack but still
3737 * in the primary register.
3739 if (flags & CF_CONST) {
3741 /* Look at the type */
3742 switch (flags & CF_TYPE) {
3745 if (flags & CF_FORCECHAR) {
3746 if (flags & CF_UNSIGNED) {
3748 /* If we have a compare > 0, we will replace it by
3749 * != 0 here, since both are identical but the
3750 * latter is easier to optimize.
3753 } else if (val < 0xFF) {
3754 /* Use >= instead of > because the former gives
3755 * better code on the 6502 than the latter.
3757 g_ge (flags, val+1);
3760 Warning ("Condition is never true");
3761 AddCodeLine ("jsr return0");
3764 if ((long) val < 0x7F) {
3765 /* Use >= instead of > because the former gives
3766 * better code on the 6502 than the latter.
3768 g_ge (flags, val+1);
3771 Warning ("Condition is never true");
3772 AddCodeLine ("jsr return0");
3780 if (flags & CF_UNSIGNED) {
3781 /* Unsigned compare */
3783 /* If we have a compare > 0, we will replace it by
3784 * != 0 here, since both are identical but the latter
3785 * is easier to optimize.
3788 } else if (val < 0xFFFF) {
3789 /* Use >= instead of > because the former gives better
3790 * code on the 6502 than the latter.
3792 g_ge (flags, val+1);
3795 Warning ("Condition is never true");
3796 AddCodeLine ("jsr return0");
3799 /* Signed compare */
3800 if ((long) val < 0x7FFF) {
3801 g_ge (flags, val+1);
3804 Warning ("Condition is never true");
3805 AddCodeLine ("jsr return0");
3811 if (flags & CF_UNSIGNED) {
3812 /* Unsigned compare */
3814 /* If we have a compare > 0, we will replace it by
3815 * != 0 here, since both are identical but the latter
3816 * is easier to optimize.
3819 } else if (val < 0xFFFFFFFF) {
3820 /* Use >= instead of > because the former gives better
3821 * code on the 6502 than the latter.
3823 g_ge (flags, val+1);
3826 Warning ("Condition is never true");
3827 AddCodeLine ("jsr return0");
3830 /* Signed compare */
3831 if ((long) val < 0x7FFFFFFF) {
3832 g_ge (flags, val+1);
3835 Warning ("Condition is never true");
3836 AddCodeLine ("jsr return0");
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_ge (unsigned flags, unsigned long val)
3860 /* Test for greater than or equal to */
3862 static char* ops [12] = {
3863 "tosge00", "tosgea0", "tosgeax",
3864 "tosuge00", "tosugea0", "tosugeax",
3870 /* If the right hand side is const, the lhs is not on stack but still
3871 * in the primary register.
3873 if (flags & CF_CONST) {
3875 /* Because the handling of the overflow flag is too complex for
3876 * inlining, we can handle only unsigned compares here.
3878 if (flags & CF_UNSIGNED) {
3880 /* Give a warning in some special cases */
3882 Warning ("Condition is always true");
3883 AddCodeLine ("jsr return1");
3887 /* Look at the type */
3888 switch (flags & CF_TYPE) {
3891 if (flags & CF_FORCECHAR) {
3892 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3893 AddCodeLine ("jsr booluge");
3899 /* If the low byte is zero, we must only test the high byte */
3900 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3901 if ((val & 0xFF) != 0) {
3902 unsigned L = GetLocalLabel();
3903 AddCodeLine ("bne %s", LocalLabelName (L));
3904 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3907 AddCodeLine ("jsr booluge");
3911 /* Do a subtraction */
3912 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3913 AddCodeLine ("txa");
3914 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3915 AddCodeLine ("lda sreg");
3916 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3917 AddCodeLine ("lda sreg+1");
3918 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3919 AddCodeLine ("jsr booluge");
3928 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3929 * into the normal, non-optimized stuff. Note: The standard stuff will
3930 * always work with ints.
3932 flags &= ~CF_FORCECHAR;
3933 g_push (flags & ~CF_CONST, 0);
3936 /* Use long way over the stack */
3937 oper (flags, val, ops);
3942 /*****************************************************************************/
3943 /* Allocating static storage */
3944 /*****************************************************************************/
3948 void g_res (unsigned n)
3949 /* Reserve static storage, n bytes */
3951 AddDataLine ("\t.res\t%u,$00", n);
3956 void g_defdata (unsigned flags, unsigned long val, long offs)
3957 /* Define data with the size given in flags */
3959 if (flags & CF_CONST) {
3961 /* Numeric constant */
3962 switch (flags & CF_TYPE) {
3965 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
3969 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
3973 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3984 /* Create the correct label name */
3985 const char* Label = GetLabelName (flags, val, offs);
3987 /* Labels are always 16 bit */
3988 AddDataLine ("\t.addr\t%s", Label);
3995 void g_defbytes (const void* Bytes, unsigned Count)
3996 /* Output a row of bytes as a constant */
4002 /* Cast the buffer pointer */
4003 const unsigned char* Data = (const unsigned char*) Bytes;
4005 /* Output the stuff */
4008 /* How many go into this line? */
4009 if ((Chunk = Count) > 16) {
4014 /* Output one line */
4015 strcpy (Buf, "\t.byte\t");
4018 B += sprintf (B, "$%02X", *Data++);
4024 /* Output the line */
4031 void g_zerobytes (unsigned Count)
4032 /* Output Count bytes of data initialized with zero */
4035 AddDataLine ("\t.res\t%u,$00", Count);
4041 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4042 /* Initialize a register variable from static initialization data */
4044 /* Register variables do always have less than 128 bytes */
4045 unsigned CodeLabel = GetLocalLabel ();
4047 g_defcodelabel (CodeLabel);
4048 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4049 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4050 AddCodeLine ("dex");
4051 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4056 void g_initauto (unsigned Label, unsigned Size)
4057 /* Initialize a local variable at stack offset zero from static data */
4059 unsigned CodeLabel = GetLocalLabel ();
4061 CheckLocalOffs (Size);
4064 g_defcodelabel (CodeLabel);
4065 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4066 AddCodeLine ("sta (sp),y");
4067 AddCodeLine ("dey");
4068 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4069 } else if (Size <= 256) {
4071 g_defcodelabel (CodeLabel);
4072 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4073 AddCodeLine ("sta (sp),y");
4074 AddCodeLine ("iny");
4075 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4076 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4082 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4083 /* Initialize a static local variable from static initialization data */
4086 unsigned CodeLabel = GetLocalLabel ();
4088 g_defcodelabel (CodeLabel);
4089 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4090 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4091 AddCodeLine ("dey");
4092 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4093 } else if (Size <= 256) {
4094 unsigned CodeLabel = GetLocalLabel ();
4096 g_defcodelabel (CodeLabel);
4097 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4098 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4099 AddCodeLine ("iny");
4100 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4101 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4103 /* Use the easy way here: memcpy */
4104 g_getimmed (CF_STATIC, VarLabel, 0);
4105 AddCodeLine ("jsr pushax");
4106 g_getimmed (CF_STATIC, InitLabel, 0);
4107 AddCodeLine ("jsr pushax");
4108 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4109 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4115 /*****************************************************************************/
4116 /* Switch statement */
4117 /*****************************************************************************/
4121 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4122 /* Generate code for a switch statement */
4124 unsigned NextLabel = 0;
4127 /* Setup registers and determine which compare insn to use */
4128 const char* Compare;
4131 Compare = "cmp #$%02X";
4134 Compare = "cpx #$%02X";
4137 AddCodeLine ("ldy sreg");
4138 Compare = "cpy #$%02X";
4141 AddCodeLine ("ldy sreg+1");
4142 Compare = "cpy #$%02X";
4145 Internal ("Invalid depth in g_switch: %u", Depth);
4148 /* Walk over all nodes */
4149 for (I = 0; I < CollCount (Nodes); ++I) {
4151 /* Get the next case node */
4152 CaseNode* N = CollAtUnchecked (Nodes, I);
4154 /* If we have a next label, define it */
4156 g_defcodelabel (NextLabel);
4160 /* Do the compare */
4161 AddCodeLine (Compare, CN_GetValue (N));
4163 /* If this is the last level, jump directly to the case code if found */
4166 /* Branch if equal */
4167 g_falsejump (0, CN_GetLabel (N));
4171 /* Determine the next label */
4172 if (I == CollCount (Nodes) - 1) {
4173 /* Last node means not found */
4174 g_truejump (0, DefaultLabel);
4176 /* Jump to the next check */
4177 NextLabel = GetLocalLabel ();
4178 g_truejump (0, NextLabel);
4181 /* Check the next level */
4182 g_switch (N->Nodes, DefaultLabel, Depth-1);
4187 /* If we go here, we haven't found the label */
4188 g_jump (DefaultLabel);
4193 /*****************************************************************************/
4194 /* User supplied assembler code */
4195 /*****************************************************************************/
4199 void g_asmcode (struct StrBuf* B)
4200 /* Output one line of assembler code. */
4202 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));
4207 /*****************************************************************************/
4208 /* Inlined known functions */
4209 /*****************************************************************************/
4213 void g_strlen (unsigned flags, unsigned long val, long offs)
4214 /* Inline the strlen() function */
4216 /* We need a label in both cases */
4217 unsigned label = GetLocalLabel ();
4219 /* Two different encodings */
4220 if (flags & CF_CONST) {
4222 /* The address of the string is constant. Create the correct label name */
4223 const char* lbuf = GetLabelName (flags, val, offs);
4225 /* Generate the strlen code */
4226 AddCodeLine ("ldy #$FF");
4227 g_defcodelabel (label);
4228 AddCodeLine ("iny");
4229 AddCodeLine ("lda %s,y", lbuf);
4230 AddCodeLine ("bne %s", LocalLabelName (label));
4231 AddCodeLine ("tax");
4232 AddCodeLine ("tya");
4236 /* Address not constant but in primary */
4237 if (CodeSizeFactor < 400) {
4238 /* This is too much code, so call strlen instead of inlining */
4239 AddCodeLine ("jsr _strlen");
4241 /* Inline the function */
4242 AddCodeLine ("sta ptr1");
4243 AddCodeLine ("stx ptr1+1");
4244 AddCodeLine ("ldy #$FF");
4245 g_defcodelabel (label);
4246 AddCodeLine ("iny");
4247 AddCodeLine ("lda (ptr1),y");
4248 AddCodeLine ("bne %s", LocalLabelName (label));
4249 AddCodeLine ("tax");
4250 AddCodeLine ("tya");