1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2004 Ullrich von Bassewitz */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
64 /*****************************************************************************/
66 /*****************************************************************************/
70 static void typeerror (unsigned type)
71 /* Print an error message about an invalid operand type */
73 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
78 static void CheckLocalOffs (unsigned Offs)
79 /* Check the offset into the stack for 8bit range */
82 /* Too many local vars */
83 Error ("Too many local variables");
89 static const char* GetLabelName (unsigned Flags, unsigned long Label, long Offs)
91 static char Buf [256]; /* Label name */
93 /* Create the correct label name */
94 switch (Flags & CF_ADDRMASK) {
97 /* Static memory cell */
99 xsprintf (Buf, sizeof (Buf), "%s%+ld", LocalLabelName (Label), Offs);
101 xsprintf (Buf, sizeof (Buf), "%s", LocalLabelName (Label));
108 xsprintf (Buf, sizeof (Buf), "_%s%+ld", (char*) Label, Offs);
110 xsprintf (Buf, sizeof (Buf), "_%s", (char*) Label);
115 /* Absolute address */
116 xsprintf (Buf, sizeof (Buf), "$%04X", (int)((Label+Offs) & 0xFFFF));
120 /* Variable in register bank */
121 xsprintf (Buf, sizeof (Buf), "regbank+%u", (unsigned)((Label+Offs) & 0xFFFF));
125 Internal ("Invalid address flags: %04X", Flags);
128 /* Return a pointer to the static buffer */
134 /*****************************************************************************/
135 /* Pre- and postamble */
136 /*****************************************************************************/
140 void g_preamble (void)
141 /* Generate the assembler code preamble */
143 /* Create a new (global) segment list and remember it */
147 /* Identify the compiler version */
149 AddTextLine ("; File generated by cc65 v %u.%u.%u",
150 VER_MAJOR, VER_MINOR, VER_PATCH);
153 /* Insert some object file options */
154 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
155 VER_MAJOR, VER_MINOR, VER_PATCH);
157 /* If we're producing code for some other CPU, switch the command set */
159 case CPU_6502: AddTextLine ("\t.setcpu\t\t\"6502\""); break;
160 case CPU_6502X: AddTextLine ("\t.setcpu\t\t\"6502X\""); break;
161 case CPU_65SC02: AddTextLine ("\t.setcpu\t\t\"65SC02\""); break;
162 case CPU_65C02: AddTextLine ("\t.setcpu\t\t\"65C02\""); break;
163 case CPU_65816: AddTextLine ("\t.setcpu\t\t\"65816\""); break;
164 default: Internal ("Unknown CPU: %d", CPU);
168 AddTextLine ("\t.smart\t\ton");
170 /* Allow auto import for runtime library routines */
171 AddTextLine ("\t.autoimport\ton");
173 /* Switch the assembler into case sensitive mode */
174 AddTextLine ("\t.case\t\ton");
176 /* Tell the assembler if we want to generate debug info */
177 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
179 /* Import the stack pointer for direct auto variable access */
180 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
182 /* Define long branch macros */
183 AddTextLine ("\t.macpack\tlongbranch");
188 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
189 /* If debug info is enabled, place a file info into the source */
192 /* We have to place this into the global text segment, so it will
193 * appear before all .dbg line statements.
195 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
201 /*****************************************************************************/
202 /* Segment support */
203 /*****************************************************************************/
207 void g_userodata (void)
208 /* Switch to the read only data segment */
210 UseDataSeg (SEG_RODATA);
215 void g_usedata (void)
216 /* Switch to the data segment */
218 UseDataSeg (SEG_DATA);
224 /* Switch to the bss segment */
226 UseDataSeg (SEG_BSS);
231 void g_segname (segment_t Seg)
232 /* Emit the name of a segment if necessary */
234 /* Emit a segment directive for the data style segments */
237 case SEG_RODATA: S = CS->ROData; break;
238 case SEG_DATA: S = CS->Data; break;
239 case SEG_BSS: S = CS->BSS; break;
240 default: S = 0; break;
243 DS_AddLine (S, ".segment\t\"%s\"", GetSegName (Seg));
249 /*****************************************************************************/
251 /*****************************************************************************/
255 unsigned sizeofarg (unsigned flags)
256 /* Return the size of a function argument type that is encoded in flags */
258 switch (flags & CF_TYPE) {
261 return (flags & CF_FORCECHAR)? 1 : 2;
281 int pop (unsigned flags)
282 /* Pop an argument of the given size */
284 return StackPtr += sizeofarg (flags);
289 int push (unsigned flags)
290 /* Push an argument of the given size */
292 return StackPtr -= sizeofarg (flags);
297 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
298 /* The value in Offs is an offset to an address in a/x. Make sure, an object
299 * of the type given in Flags can be loaded or stored into this address by
300 * adding part of the offset to the address in ax, so that the remaining
301 * offset fits into an index register. Return the remaining offset.
304 /* If the offset is too large for a byte register, add the high byte
305 * of the offset to the primary. Beware: We need a special correction
306 * if the offset in the low byte will overflow in the operation.
308 unsigned O = Offs & ~0xFFU;
309 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
310 /* We need to add the low byte also */
314 /* Do the correction if we need one */
316 g_inc (CF_INT | CF_CONST, O);
320 /* Return the new offset */
326 /*****************************************************************************/
327 /* Functions handling local labels */
328 /*****************************************************************************/
332 void g_defcodelabel (unsigned label)
333 /* Define a local code label */
335 CS_AddLabel (CS->Code, LocalLabelName (label));
340 void g_defdatalabel (unsigned label)
341 /* Define a local data label */
343 AddDataLine ("%s:", LocalLabelName (label));
348 /*****************************************************************************/
349 /* Functions handling global labels */
350 /*****************************************************************************/
354 void g_defgloblabel (const char* Name)
355 /* Define a global label with the given name */
357 /* Global labels are always data labels */
358 AddDataLine ("_%s:", Name);
363 void g_defexport (const char* Name, int ZP)
364 /* Export the given label */
367 AddTextLine ("\t.exportzp\t_%s", Name);
369 AddTextLine ("\t.export\t\t_%s", Name);
375 void g_defimport (const char* Name, int ZP)
376 /* Import the given label */
379 AddTextLine ("\t.importzp\t_%s", Name);
381 AddTextLine ("\t.import\t\t_%s", Name);
387 void g_importmainargs (void)
388 /* Forced import of a special symbol that handles arguments to main */
390 AddTextLine ("\t.forceimport\tinitmainargs");
395 /*****************************************************************************/
396 /* Load functions for various registers */
397 /*****************************************************************************/
401 static void ldaconst (unsigned val)
402 /* Load a with a constant */
404 AddCodeLine ("lda #$%02X", val & 0xFF);
409 static void ldxconst (unsigned val)
410 /* Load x with a constant */
412 AddCodeLine ("ldx #$%02X", val & 0xFF);
417 static void ldyconst (unsigned val)
418 /* Load y with a constant */
420 AddCodeLine ("ldy #$%02X", val & 0xFF);
425 /*****************************************************************************/
426 /* Function entry and exit */
427 /*****************************************************************************/
431 /* Remember the argument size of a function. The variable is set by g_enter
432 * and used by g_leave. If the functions gets its argument size by the caller
433 * (variable param list or function without prototype), g_enter will set the
439 void g_enter (unsigned flags, unsigned argsize)
440 /* Function prologue */
442 if ((flags & CF_FIXARGC) != 0) {
443 /* Just remember the argument size for the leave */
447 AddCodeLine ("jsr enter");
454 /* Function epilogue */
456 /* How many bytes of locals do we have to drop? */
457 unsigned k = (unsigned) -StackPtr;
459 /* If we didn't have a variable argument list, don't call leave */
462 /* Drop stackframe if needed. We can only drop 255 bytes at a time. */
465 unsigned ToDrop = (k > 255)? 255 : k;
467 AddCodeLine ("jsr incsp%d", k);
470 AddCodeLine ("jsr addysp");
478 /* Nothing to drop */
479 AddCodeLine ("jsr leave");
481 /* We've a stack frame to drop */
484 AddCodeLine ("jsr addysp");
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 */
508 StackOffs -= StackPtr;
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 */
585 StackOffs -= StackPtr;
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 ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && 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.
957 CHECK (StackPtr <= 0);
958 ArgSizeOffs = -StackPtr;
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 StackPtr += 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 */
2468 StackPtr += ArgSize;
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) != 0) {
2776 AddCodeLine ("ora #$%02X", (unsigned char)val);
2784 if ((val & 0xFF) != 0) {
2785 AddCodeLine ("ora #$%02X", (unsigned char)val);
2787 } else if ((val & 0xFF00) == 0xFF00) {
2788 if ((val & 0xFF) != 0) {
2789 AddCodeLine ("ora #$%02X", (unsigned char)val);
2792 } else if (val != 0) {
2793 AddCodeLine ("ora #$%02X", (unsigned char)val);
2794 AddCodeLine ("pha");
2795 AddCodeLine ("txa");
2796 AddCodeLine ("ora #$%02X", (unsigned char)(val >> 8));
2797 AddCodeLine ("tax");
2798 AddCodeLine ("pla");
2804 if ((val & 0xFF) != 0) {
2805 AddCodeLine ("ora #$%02X", (unsigned char)val);
2815 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2816 * into the normal, non-optimized stuff. Note: The standard stuff will
2817 * always work with ints.
2819 flags &= ~CF_FORCECHAR;
2820 g_push (flags & ~CF_CONST, 0);
2823 /* Use long way over the stack */
2824 oper (flags, val, ops);
2829 void g_xor (unsigned flags, unsigned long val)
2830 /* Primary = TOS ^ Primary */
2832 static char* ops [12] = {
2833 0, "tosxora0", "tosxorax",
2834 0, "tosxora0", "tosxorax",
2840 /* If the right hand side is const, the lhs is not on stack but still
2841 * in the primary register.
2843 if (flags & CF_CONST) {
2845 switch (flags & CF_TYPE) {
2848 if (flags & CF_FORCECHAR) {
2849 if ((val & 0xFF) != 0) {
2850 AddCodeLine ("eor #$%02X", (unsigned char)val);
2859 AddCodeLine ("eor #$%02X", (unsigned char)val);
2861 } else if (val != 0) {
2862 if ((val & 0xFF) != 0) {
2863 AddCodeLine ("eor #$%02X", (unsigned char)val);
2865 AddCodeLine ("pha");
2866 AddCodeLine ("txa");
2867 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2868 AddCodeLine ("tax");
2869 AddCodeLine ("pla");
2876 AddCodeLine ("eor #$%02X", (unsigned char)val);
2886 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2887 * into the normal, non-optimized stuff. Note: The standard stuff will
2888 * always work with ints.
2890 flags &= ~CF_FORCECHAR;
2891 g_push (flags & ~CF_CONST, 0);
2894 /* Use long way over the stack */
2895 oper (flags, val, ops);
2900 void g_and (unsigned Flags, unsigned long Val)
2901 /* Primary = TOS & Primary */
2903 static char* ops [12] = {
2904 0, "tosanda0", "tosandax",
2905 0, "tosanda0", "tosandax",
2910 /* If the right hand side is const, the lhs is not on stack but still
2911 * in the primary register.
2913 if (Flags & CF_CONST) {
2915 switch (Flags & CF_TYPE) {
2918 if (Flags & CF_FORCECHAR) {
2919 if ((Val & 0xFF) != 0xFF) {
2920 AddCodeLine ("and #$%02X", (unsigned char)Val);
2926 if ((Val & 0xFFFF) != 0xFFFF) {
2931 } else if (Val != 0xFF) {
2932 AddCodeLine ("and #$%02X", (unsigned char)Val);
2934 } else if ((Val & 0xFF00) == 0xFF00) {
2935 AddCodeLine ("and #$%02X", (unsigned char)Val);
2936 } else if ((Val & 0x00FF) == 0x0000) {
2937 AddCodeLine ("txa");
2938 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2939 AddCodeLine ("tax");
2942 AddCodeLine ("tay");
2943 AddCodeLine ("txa");
2944 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2945 AddCodeLine ("tax");
2946 AddCodeLine ("tya");
2947 if ((Val & 0x00FF) != 0x00FF) {
2948 AddCodeLine ("and #$%02X", (unsigned char)Val);
2957 AddCodeLine ("stx sreg+1");
2958 AddCodeLine ("stx sreg");
2959 if ((Val & 0xFF) != 0xFF) {
2960 AddCodeLine ("and #$%02X", (unsigned char)Val);
2963 } else if (Val == 0xFF00) {
2965 AddCodeLine ("sta sreg+1");
2966 AddCodeLine ("sta sreg");
2975 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2976 * into the normal, non-optimized stuff. Note: The standard stuff will
2977 * always work with ints.
2979 Flags &= ~CF_FORCECHAR;
2980 g_push (Flags & ~CF_CONST, 0);
2983 /* Use long way over the stack */
2984 oper (Flags, Val, ops);
2989 void g_asr (unsigned flags, unsigned long val)
2990 /* Primary = TOS >> Primary */
2992 static char* ops [12] = {
2993 0, "tosasra0", "tosasrax",
2994 0, "tosshra0", "tosshrax",
2999 /* If the right hand side is const, the lhs is not on stack but still
3000 * in the primary register.
3002 if (flags & CF_CONST) {
3004 switch (flags & CF_TYPE) {
3008 if (val >= 8 && (flags & CF_UNSIGNED)) {
3009 AddCodeLine ("txa");
3016 } else if (val >= 1 && val <= 4) {
3017 if (flags & CF_UNSIGNED) {
3018 AddCodeLine ("jsr shrax%ld", val);
3020 AddCodeLine ("jsr asrax%ld", val);
3030 } else if (val >= 1 && val <= 4) {
3031 if (flags & CF_UNSIGNED) {
3032 AddCodeLine ("jsr shreax%ld", val);
3034 AddCodeLine ("jsr asreax%ld", val);
3037 } else if (val == 8 && (flags & CF_UNSIGNED)) {
3038 AddCodeLine ("txa");
3039 AddCodeLine ("ldx sreg");
3040 AddCodeLine ("ldy sreg+1");
3041 AddCodeLine ("sty sreg");
3042 AddCodeLine ("ldy #$00");
3043 AddCodeLine ("sty sreg+1");
3045 } else if (val == 16) {
3046 AddCodeLine ("ldy #$00");
3047 AddCodeLine ("ldx sreg+1");
3048 if ((flags & CF_UNSIGNED) == 0) {
3049 unsigned L = GetLocalLabel();
3050 AddCodeLine ("bpl %s", LocalLabelName (L));
3051 AddCodeLine ("dey");
3054 AddCodeLine ("lda sreg");
3055 AddCodeLine ("sty sreg+1");
3056 AddCodeLine ("sty sreg");
3065 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3066 * into the normal, non-optimized stuff. Note: The standard stuff will
3067 * always work with ints.
3069 flags &= ~CF_FORCECHAR;
3070 g_push (flags & ~CF_CONST, 0);
3073 /* Use long way over the stack */
3074 oper (flags, val, ops);
3079 void g_asl (unsigned flags, unsigned long val)
3080 /* Primary = TOS << Primary */
3082 static char* ops [12] = {
3083 0, "tosasla0", "tosaslax",
3084 0, "tosshla0", "tosshlax",
3090 /* If the right hand side is const, the lhs is not on stack but still
3091 * in the primary register.
3093 if (flags & CF_CONST) {
3095 switch (flags & CF_TYPE) {
3100 AddCodeLine ("tax");
3101 AddCodeLine ("lda #$00");
3107 } else if (val >= 1 && val <= 4) {
3108 if (flags & CF_UNSIGNED) {
3109 AddCodeLine ("jsr shlax%ld", val);
3111 AddCodeLine ("jsr aslax%ld", val);
3121 } else if (val >= 1 && val <= 4) {
3122 if (flags & CF_UNSIGNED) {
3123 AddCodeLine ("jsr shleax%ld", val);
3125 AddCodeLine ("jsr asleax%ld", val);
3128 } else if (val == 8) {
3129 AddCodeLine ("ldy sreg");
3130 AddCodeLine ("sty sreg+1");
3131 AddCodeLine ("stx sreg");
3132 AddCodeLine ("tax");
3133 AddCodeLine ("lda #$00");
3135 } else if (val == 16) {
3136 AddCodeLine ("stx sreg+1");
3137 AddCodeLine ("sta sreg");
3138 AddCodeLine ("lda #$00");
3139 AddCodeLine ("tax");
3148 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3149 * into the normal, non-optimized stuff. Note: The standard stuff will
3150 * always work with ints.
3152 flags &= ~CF_FORCECHAR;
3153 g_push (flags & ~CF_CONST, 0);
3156 /* Use long way over the stack */
3157 oper (flags, val, ops);
3162 void g_neg (unsigned Flags)
3163 /* Primary = -Primary */
3165 switch (Flags & CF_TYPE) {
3168 if (Flags & CF_FORCECHAR) {
3169 AddCodeLine ("eor #$FF");
3170 AddCodeLine ("clc");
3171 AddCodeLine ("adc #$01");
3177 AddCodeLine ("jsr negax");
3181 AddCodeLine ("jsr negeax");
3191 void g_bneg (unsigned flags)
3192 /* Primary = !Primary */
3194 switch (flags & CF_TYPE) {
3197 AddCodeLine ("jsr bnega");
3201 AddCodeLine ("jsr bnegax");
3205 AddCodeLine ("jsr bnegeax");
3215 void g_com (unsigned Flags)
3216 /* Primary = ~Primary */
3218 switch (Flags & CF_TYPE) {
3221 if (Flags & CF_FORCECHAR) {
3222 AddCodeLine ("eor #$FF");
3228 AddCodeLine ("jsr complax");
3232 AddCodeLine ("jsr compleax");
3242 void g_inc (unsigned flags, unsigned long val)
3243 /* Increment the primary register by a given number */
3245 /* Don't inc by zero */
3250 /* Generate code for the supported types */
3252 switch (flags & CF_TYPE) {
3255 if (flags & CF_FORCECHAR) {
3256 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3258 AddCodeLine ("ina");
3261 AddCodeLine ("clc");
3262 AddCodeLine ("adc #$%02X", (unsigned char)val);
3269 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) {
3270 unsigned L = GetLocalLabel();
3271 AddCodeLine ("ina");
3272 AddCodeLine ("bne %s", LocalLabelName (L));
3273 AddCodeLine ("inx");
3275 } else if (CodeSizeFactor < 200) {
3278 AddCodeLine ("jsr incax%lu", val);
3279 } else if (val <= 255) {
3281 AddCodeLine ("jsr incaxy");
3283 g_add (flags | CF_CONST, val);
3286 /* Inline the code */
3288 if ((val & 0xFF) != 0) {
3289 unsigned L = GetLocalLabel();
3290 AddCodeLine ("clc");
3291 AddCodeLine ("adc #$%02X", (unsigned char) val);
3292 AddCodeLine ("bcc %s", LocalLabelName (L));
3293 AddCodeLine ("inx");
3297 AddCodeLine ("inx");
3300 AddCodeLine ("inx");
3303 AddCodeLine ("inx");
3306 AddCodeLine ("clc");
3307 if ((val & 0xFF) != 0) {
3308 AddCodeLine ("adc #$%02X", (unsigned char) val);
3310 AddCodeLine ("pha");
3311 AddCodeLine ("txa");
3312 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3313 AddCodeLine ("tax");
3314 AddCodeLine ("pla");
3322 AddCodeLine ("jsr inceaxy");
3324 g_add (flags | CF_CONST, val);
3336 void g_dec (unsigned flags, unsigned long val)
3337 /* Decrement the primary register by a given number */
3339 /* Don't dec by zero */
3344 /* Generate code for the supported types */
3346 switch (flags & CF_TYPE) {
3349 if (flags & CF_FORCECHAR) {
3350 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3352 AddCodeLine ("dea");
3355 AddCodeLine ("sec");
3356 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3363 if (CodeSizeFactor < 200) {
3364 /* Use subroutines */
3366 AddCodeLine ("jsr decax%d", (int) val);
3367 } else if (val <= 255) {
3369 AddCodeLine ("jsr decaxy");
3371 g_sub (flags | CF_CONST, val);
3374 /* Inline the code */
3376 if ((val & 0xFF) != 0) {
3377 unsigned L = GetLocalLabel();
3378 AddCodeLine ("sec");
3379 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3380 AddCodeLine ("bcs %s", LocalLabelName (L));
3381 AddCodeLine ("dex");
3385 AddCodeLine ("dex");
3388 AddCodeLine ("dex");
3391 AddCodeLine ("sec");
3392 if ((val & 0xFF) != 0) {
3393 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3395 AddCodeLine ("pha");
3396 AddCodeLine ("txa");
3397 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3398 AddCodeLine ("tax");
3399 AddCodeLine ("pla");
3407 AddCodeLine ("jsr deceaxy");
3409 g_sub (flags | CF_CONST, val);
3422 * Following are the conditional operators. They compare the TOS against
3423 * the primary and put a literal 1 in the primary if the condition is
3424 * true, otherwise they clear the primary register
3429 void g_eq (unsigned flags, unsigned long val)
3430 /* Test for equal */
3432 static char* ops [12] = {
3433 "toseq00", "toseqa0", "toseqax",
3434 "toseq00", "toseqa0", "toseqax",
3441 /* If the right hand side is const, the lhs is not on stack but still
3442 * in the primary register.
3444 if (flags & CF_CONST) {
3446 switch (flags & CF_TYPE) {
3449 if (flags & CF_FORCECHAR) {
3450 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3451 AddCodeLine ("jsr booleq");
3457 L = GetLocalLabel();
3458 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3459 AddCodeLine ("bne %s", LocalLabelName (L));
3460 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3462 AddCodeLine ("jsr booleq");
3472 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3473 * into the normal, non-optimized stuff. Note: The standard stuff will
3474 * always work with ints.
3476 flags &= ~CF_FORCECHAR;
3477 g_push (flags & ~CF_CONST, 0);
3480 /* Use long way over the stack */
3481 oper (flags, val, ops);
3486 void g_ne (unsigned flags, unsigned long val)
3487 /* Test for not equal */
3489 static char* ops [12] = {
3490 "tosne00", "tosnea0", "tosneax",
3491 "tosne00", "tosnea0", "tosneax",
3498 /* If the right hand side is const, the lhs is not on stack but still
3499 * in the primary register.
3501 if (flags & CF_CONST) {
3503 switch (flags & CF_TYPE) {
3506 if (flags & CF_FORCECHAR) {
3507 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3508 AddCodeLine ("jsr boolne");
3514 L = GetLocalLabel();
3515 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3516 AddCodeLine ("bne %s", LocalLabelName (L));
3517 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3519 AddCodeLine ("jsr boolne");
3529 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3530 * into the normal, non-optimized stuff. Note: The standard stuff will
3531 * always work with ints.
3533 flags &= ~CF_FORCECHAR;
3534 g_push (flags & ~CF_CONST, 0);
3537 /* Use long way over the stack */
3538 oper (flags, val, ops);
3543 void g_lt (unsigned flags, unsigned long val)
3544 /* Test for less than */
3546 static char* ops [12] = {
3547 "toslt00", "toslta0", "tosltax",
3548 "tosult00", "tosulta0", "tosultax",
3553 /* If the right hand side is const, the lhs is not on stack but still
3554 * in the primary register.
3556 if (flags & CF_CONST) {
3558 /* Because the handling of the overflow flag is too complex for
3559 * inlining, we can handle only unsigned compares, and signed
3560 * compares against zero here.
3562 if (flags & CF_UNSIGNED) {
3564 /* Give a warning in some special cases */
3566 Warning ("Condition is never true");
3567 AddCodeLine ("jsr return0");
3571 /* Look at the type */
3572 switch (flags & CF_TYPE) {
3575 if (flags & CF_FORCECHAR) {
3576 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3577 AddCodeLine ("jsr boolult");
3583 /* If the low byte is zero, we must only test the high byte */
3584 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3585 if ((val & 0xFF) != 0) {
3586 unsigned L = GetLocalLabel();
3587 AddCodeLine ("bne %s", LocalLabelName (L));
3588 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3591 AddCodeLine ("jsr boolult");
3595 /* Do a subtraction */
3596 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3597 AddCodeLine ("txa");
3598 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3599 AddCodeLine ("lda sreg");
3600 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3601 AddCodeLine ("lda sreg+1");
3602 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3603 AddCodeLine ("jsr boolult");
3610 } else if (val == 0) {
3612 /* Look at the type */
3613 switch (flags & CF_TYPE) {
3616 if (flags & CF_FORCECHAR) {
3617 AddCodeLine ("tax");
3618 AddCodeLine ("jsr boollt");
3624 /* Just check the high byte */
3625 AddCodeLine ("txa");
3626 AddCodeLine ("jsr boollt");
3630 /* Just check the high byte */
3631 AddCodeLine ("lda sreg+1");
3632 AddCodeLine ("jsr boollt");
3640 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3641 * into the normal, non-optimized stuff. Note: The standard stuff will
3642 * always work with ints.
3644 flags &= ~CF_FORCECHAR;
3645 g_push (flags & ~CF_CONST, 0);
3648 /* Use long way over the stack */
3649 oper (flags, val, ops);
3654 void g_le (unsigned flags, unsigned long val)
3655 /* Test for less than or equal to */
3657 static char* ops [12] = {
3658 "tosle00", "toslea0", "tosleax",
3659 "tosule00", "tosulea0", "tosuleax",
3665 /* If the right hand side is const, the lhs is not on stack but still
3666 * in the primary register.
3668 if (flags & CF_CONST) {
3670 /* Look at the type */
3671 switch (flags & CF_TYPE) {
3674 if (flags & CF_FORCECHAR) {
3675 if (flags & CF_UNSIGNED) {
3676 /* Unsigned compare */
3678 /* Use < instead of <= because the former gives
3679 * better code on the 6502 than the latter.
3681 g_lt (flags, val+1);
3684 Warning ("Condition is always true");
3685 AddCodeLine ("jsr return1");
3688 /* Signed compare */
3689 if ((long) val < 0x7F) {
3690 /* Use < instead of <= because the former gives
3691 * better code on the 6502 than the latter.
3693 g_lt (flags, val+1);
3696 Warning ("Condition is always true");
3697 AddCodeLine ("jsr return1");
3705 if (flags & CF_UNSIGNED) {
3706 /* Unsigned compare */
3708 /* Use < instead of <= because the former gives
3709 * better code on the 6502 than the latter.
3711 g_lt (flags, val+1);
3714 Warning ("Condition is always true");
3715 AddCodeLine ("jsr return1");
3718 /* Signed compare */
3719 if ((long) val < 0x7FFF) {
3720 g_lt (flags, val+1);
3723 Warning ("Condition is always true");
3724 AddCodeLine ("jsr return1");
3730 if (flags & CF_UNSIGNED) {
3731 /* Unsigned compare */
3732 if (val < 0xFFFFFFFF) {
3733 /* Use < instead of <= because the former gives
3734 * better code on the 6502 than the latter.
3736 g_lt (flags, val+1);
3739 Warning ("Condition is always true");
3740 AddCodeLine ("jsr return1");
3743 /* Signed compare */
3744 if ((long) val < 0x7FFFFFFF) {
3745 g_lt (flags, val+1);
3748 Warning ("Condition is always true");
3749 AddCodeLine ("jsr return1");
3758 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3759 * into the normal, non-optimized stuff. Note: The standard stuff will
3760 * always work with ints.
3762 flags &= ~CF_FORCECHAR;
3763 g_push (flags & ~CF_CONST, 0);
3766 /* Use long way over the stack */
3767 oper (flags, val, ops);
3772 void g_gt (unsigned flags, unsigned long val)
3773 /* Test for greater than */
3775 static char* ops [12] = {
3776 "tosgt00", "tosgta0", "tosgtax",
3777 "tosugt00", "tosugta0", "tosugtax",
3783 /* If the right hand side is const, the lhs is not on stack but still
3784 * in the primary register.
3786 if (flags & CF_CONST) {
3788 /* Look at the type */
3789 switch (flags & CF_TYPE) {
3792 if (flags & CF_FORCECHAR) {
3793 if (flags & CF_UNSIGNED) {
3795 /* If we have a compare > 0, we will replace it by
3796 * != 0 here, since both are identical but the
3797 * latter is easier to optimize.
3800 } else if (val < 0xFF) {
3801 /* Use >= instead of > because the former gives
3802 * better code on the 6502 than the latter.
3804 g_ge (flags, val+1);
3807 Warning ("Condition is never true");
3808 AddCodeLine ("jsr return0");
3811 if ((long) val < 0x7F) {
3812 /* Use >= instead of > because the former gives
3813 * better code on the 6502 than the latter.
3815 g_ge (flags, val+1);
3818 Warning ("Condition is never true");
3819 AddCodeLine ("jsr return0");
3827 if (flags & CF_UNSIGNED) {
3828 /* Unsigned compare */
3830 /* If we have a compare > 0, we will replace it by
3831 * != 0 here, since both are identical but the latter
3832 * is easier to optimize.
3835 } else if (val < 0xFFFF) {
3836 /* Use >= instead of > because the former gives better
3837 * code on the 6502 than the latter.
3839 g_ge (flags, val+1);
3842 Warning ("Condition is never true");
3843 AddCodeLine ("jsr return0");
3846 /* Signed compare */
3847 if ((long) val < 0x7FFF) {
3848 g_ge (flags, val+1);
3851 Warning ("Condition is never true");
3852 AddCodeLine ("jsr return0");
3858 if (flags & CF_UNSIGNED) {
3859 /* Unsigned compare */
3861 /* If we have a compare > 0, we will replace it by
3862 * != 0 here, since both are identical but the latter
3863 * is easier to optimize.
3866 } else if (val < 0xFFFFFFFF) {
3867 /* Use >= instead of > because the former gives better
3868 * code on the 6502 than the latter.
3870 g_ge (flags, val+1);
3873 Warning ("Condition is never true");
3874 AddCodeLine ("jsr return0");
3877 /* Signed compare */
3878 if ((long) val < 0x7FFFFFFF) {
3879 g_ge (flags, val+1);
3882 Warning ("Condition is never true");
3883 AddCodeLine ("jsr return0");
3892 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3893 * into the normal, non-optimized stuff. Note: The standard stuff will
3894 * always work with ints.
3896 flags &= ~CF_FORCECHAR;
3897 g_push (flags & ~CF_CONST, 0);
3900 /* Use long way over the stack */
3901 oper (flags, val, ops);
3906 void g_ge (unsigned flags, unsigned long val)
3907 /* Test for greater than or equal to */
3909 static char* ops [12] = {
3910 "tosge00", "tosgea0", "tosgeax",
3911 "tosuge00", "tosugea0", "tosugeax",
3917 /* If the right hand side is const, the lhs is not on stack but still
3918 * in the primary register.
3920 if (flags & CF_CONST) {
3922 /* Because the handling of the overflow flag is too complex for
3923 * inlining, we can handle only unsigned compares, and signed
3924 * compares against zero here.
3926 if (flags & CF_UNSIGNED) {
3928 /* Give a warning in some special cases */
3930 Warning ("Condition is always true");
3931 AddCodeLine ("jsr return1");
3935 /* Look at the type */
3936 switch (flags & CF_TYPE) {
3939 if (flags & CF_FORCECHAR) {
3940 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3941 AddCodeLine ("jsr booluge");
3947 /* If the low byte is zero, we must only test the high byte */
3948 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3949 if ((val & 0xFF) != 0) {
3950 unsigned L = GetLocalLabel();
3951 AddCodeLine ("bne %s", LocalLabelName (L));
3952 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3955 AddCodeLine ("jsr booluge");
3959 /* Do a subtraction */
3960 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3961 AddCodeLine ("txa");
3962 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3963 AddCodeLine ("lda sreg");
3964 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3965 AddCodeLine ("lda sreg+1");
3966 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3967 AddCodeLine ("jsr booluge");
3974 } else if (val == 0) {
3976 /* Look at the type */
3977 switch (flags & CF_TYPE) {
3980 if (flags & CF_FORCECHAR) {
3981 AddCodeLine ("tax");
3982 AddCodeLine ("jsr boolge");
3988 /* Just test the high byte */
3989 AddCodeLine ("txa");
3990 AddCodeLine ("jsr boolge");
3994 /* Just test the high byte */
3995 AddCodeLine ("lda sreg+1");
3996 AddCodeLine ("jsr boolge");
4005 /* If we go here, we didn't emit code. Push the lhs on stack and fall
4006 * into the normal, non-optimized stuff. Note: The standard stuff will
4007 * always work with ints.
4009 flags &= ~CF_FORCECHAR;
4010 g_push (flags & ~CF_CONST, 0);
4013 /* Use long way over the stack */
4014 oper (flags, val, ops);
4019 /*****************************************************************************/
4020 /* Allocating static storage */
4021 /*****************************************************************************/
4025 void g_res (unsigned n)
4026 /* Reserve static storage, n bytes */
4028 AddDataLine ("\t.res\t%u,$00", n);
4033 void g_defdata (unsigned flags, unsigned long val, long offs)
4034 /* Define data with the size given in flags */
4036 if (flags & CF_CONST) {
4038 /* Numeric constant */
4039 switch (flags & CF_TYPE) {
4042 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
4046 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
4050 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
4061 /* Create the correct label name */
4062 const char* Label = GetLabelName (flags, val, offs);
4064 /* Labels are always 16 bit */
4065 AddDataLine ("\t.addr\t%s", Label);
4072 void g_defbytes (const void* Bytes, unsigned Count)
4073 /* Output a row of bytes as a constant */
4079 /* Cast the buffer pointer */
4080 const unsigned char* Data = (const unsigned char*) Bytes;
4082 /* Output the stuff */
4085 /* How many go into this line? */
4086 if ((Chunk = Count) > 16) {
4091 /* Output one line */
4092 strcpy (Buf, "\t.byte\t");
4095 B += sprintf (B, "$%02X", *Data++);
4101 /* Output the line */
4108 void g_zerobytes (unsigned Count)
4109 /* Output Count bytes of data initialized with zero */
4112 AddDataLine ("\t.res\t%u,$00", Count);
4118 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4119 /* Initialize a register variable from static initialization data */
4121 /* Register variables do always have less than 128 bytes */
4122 unsigned CodeLabel = GetLocalLabel ();
4124 g_defcodelabel (CodeLabel);
4125 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4126 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4127 AddCodeLine ("dex");
4128 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4133 void g_initauto (unsigned Label, unsigned Size)
4134 /* Initialize a local variable at stack offset zero from static data */
4136 unsigned CodeLabel = GetLocalLabel ();
4138 CheckLocalOffs (Size);
4141 g_defcodelabel (CodeLabel);
4142 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4143 AddCodeLine ("sta (sp),y");
4144 AddCodeLine ("dey");
4145 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4146 } else if (Size <= 256) {
4148 g_defcodelabel (CodeLabel);
4149 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4150 AddCodeLine ("sta (sp),y");
4151 AddCodeLine ("iny");
4152 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4153 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4159 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4160 /* Initialize a static local variable from static initialization data */
4163 unsigned CodeLabel = GetLocalLabel ();
4165 g_defcodelabel (CodeLabel);
4166 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4167 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4168 AddCodeLine ("dey");
4169 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4170 } else if (Size <= 256) {
4171 unsigned CodeLabel = GetLocalLabel ();
4173 g_defcodelabel (CodeLabel);
4174 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4175 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4176 AddCodeLine ("iny");
4177 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4178 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4180 /* Use the easy way here: memcpy */
4181 g_getimmed (CF_STATIC, VarLabel, 0);
4182 AddCodeLine ("jsr pushax");
4183 g_getimmed (CF_STATIC, InitLabel, 0);
4184 AddCodeLine ("jsr pushax");
4185 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4186 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4192 /*****************************************************************************/
4193 /* Switch statement */
4194 /*****************************************************************************/
4198 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4199 /* Generate code for a switch statement */
4201 unsigned NextLabel = 0;
4204 /* Setup registers and determine which compare insn to use */
4205 const char* Compare;
4208 Compare = "cmp #$%02X";
4211 Compare = "cpx #$%02X";
4214 AddCodeLine ("ldy sreg");
4215 Compare = "cpy #$%02X";
4218 AddCodeLine ("ldy sreg+1");
4219 Compare = "cpy #$%02X";
4222 Internal ("Invalid depth in g_switch: %u", Depth);
4225 /* Walk over all nodes */
4226 for (I = 0; I < CollCount (Nodes); ++I) {
4228 /* Get the next case node */
4229 CaseNode* N = CollAtUnchecked (Nodes, I);
4231 /* If we have a next label, define it */
4233 g_defcodelabel (NextLabel);
4237 /* Do the compare */
4238 AddCodeLine (Compare, CN_GetValue (N));
4240 /* If this is the last level, jump directly to the case code if found */
4243 /* Branch if equal */
4244 g_falsejump (0, CN_GetLabel (N));
4248 /* Determine the next label */
4249 if (I == CollCount (Nodes) - 1) {
4250 /* Last node means not found */
4251 g_truejump (0, DefaultLabel);
4253 /* Jump to the next check */
4254 NextLabel = GetLocalLabel ();
4255 g_truejump (0, NextLabel);
4258 /* Check the next level */
4259 g_switch (N->Nodes, DefaultLabel, Depth-1);
4264 /* If we go here, we haven't found the label */
4265 g_jump (DefaultLabel);
4270 /*****************************************************************************/
4271 /* User supplied assembler code */
4272 /*****************************************************************************/
4276 void g_asmcode (struct StrBuf* B)
4277 /* Output one line of assembler code. */
4279 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));