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 (IS_Get (&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 && IS_Get (&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");
843 if (flags & CF_TEST) {
855 void g_getind (unsigned flags, unsigned offs)
856 /* Fetch the specified object type indirect through the primary register
857 * into the primary register
860 /* If the offset is greater than 255, add the part that is > 255 to
861 * the primary. This way we get an easy addition and use the low byte
864 offs = MakeByteOffs (flags, offs);
866 /* Handle the indirect fetch */
867 switch (flags & CF_TYPE) {
870 /* Character sized */
871 if (flags & CF_UNSIGNED) {
873 AddCodeLine ("jsr ldauidx");
876 AddCodeLine ("jsr ldaidx");
881 if (flags & CF_TEST) {
883 AddCodeLine ("sta ptr1");
884 AddCodeLine ("stx ptr1+1");
885 AddCodeLine ("lda (ptr1),y");
887 AddCodeLine ("ora (ptr1),y");
890 AddCodeLine ("jsr ldaxidx");
896 AddCodeLine ("jsr ldeaxidx");
897 if (flags & CF_TEST) {
910 void g_leasp (int offs)
911 /* Fetch the address of the specified symbol into the primary register */
913 /* Calculate the offset relative to sp */
916 /* For value 0 we do direct code */
918 AddCodeLine ("lda sp");
919 AddCodeLine ("ldx sp+1");
921 if (IS_Get (&CodeSizeFactor) < 300) {
922 ldaconst (offs); /* Load A with offset value */
923 AddCodeLine ("jsr leaasp"); /* Load effective address */
925 unsigned L = GetLocalLabel ();
926 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && offs == 1) {
927 AddCodeLine ("lda sp");
928 AddCodeLine ("ldx sp+1");
930 AddCodeLine ("bne %s", LocalLabelName (L));
935 AddCodeLine ("ldx sp+1");
936 AddCodeLine ("adc sp");
937 AddCodeLine ("bcc %s", LocalLabelName (L));
947 void g_leavariadic (int Offs)
948 /* Fetch the address of a parameter in a variadic function into the primary
952 unsigned ArgSizeOffs;
954 /* Calculate the offset relative to sp */
957 /* Get the offset of the parameter which is stored at sp+0 on function
958 * entry and check if this offset is reachable with a byte offset.
960 CHECK (StackPtr <= 0);
961 ArgSizeOffs = -StackPtr;
962 CheckLocalOffs (ArgSizeOffs);
964 /* Get the size of all parameters. */
965 ldyconst (ArgSizeOffs);
966 AddCodeLine ("lda (sp),y");
968 /* Add the value of the stackpointer */
969 if (IS_Get (&CodeSizeFactor) > 250) {
970 unsigned L = GetLocalLabel();
971 AddCodeLine ("ldx sp+1");
973 AddCodeLine ("adc sp");
974 AddCodeLine ("bcc %s", LocalLabelName (L));
978 AddCodeLine ("jsr leaasp");
981 /* Add the offset to the primary */
983 g_inc (CF_INT | CF_CONST, Offs);
984 } else if (Offs < 0) {
985 g_dec (CF_INT | CF_CONST, -Offs);
991 /*****************************************************************************/
992 /* Store into memory */
993 /*****************************************************************************/
997 void g_putstatic (unsigned flags, unsigned long label, long offs)
998 /* Store the primary register into the specified static memory cell */
1000 /* Create the correct label name */
1001 const char* lbuf = GetLabelName (flags, label, offs);
1003 /* Check the size and generate the correct store operation */
1004 switch (flags & CF_TYPE) {
1007 AddCodeLine ("sta %s", lbuf);
1011 AddCodeLine ("sta %s", lbuf);
1012 AddCodeLine ("stx %s+1", lbuf);
1016 AddCodeLine ("sta %s", lbuf);
1017 AddCodeLine ("stx %s+1", lbuf);
1018 AddCodeLine ("ldy sreg");
1019 AddCodeLine ("sty %s+2", lbuf);
1020 AddCodeLine ("ldy sreg+1");
1021 AddCodeLine ("sty %s+3", lbuf);
1032 void g_putlocal (unsigned Flags, int Offs, long Val)
1033 /* Put data into local object. */
1036 CheckLocalOffs (Offs);
1037 switch (Flags & CF_TYPE) {
1040 if (Flags & CF_CONST) {
1041 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1044 AddCodeLine ("sta (sp),y");
1048 if (Flags & CF_CONST) {
1050 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1051 AddCodeLine ("sta (sp),y");
1052 if ((Flags & CF_NOKEEP) == 0) {
1053 /* Place high byte into X */
1054 AddCodeLine ("tax");
1056 if ((Val & 0xFF) == Offs+1) {
1057 /* The value we need is already in Y */
1058 AddCodeLine ("tya");
1059 AddCodeLine ("dey");
1061 AddCodeLine ("dey");
1062 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1064 AddCodeLine ("sta (sp),y");
1066 if ((Flags & CF_NOKEEP) == 0 || IS_Get (&CodeSizeFactor) < 160) {
1068 AddCodeLine ("jsr staxysp");
1071 AddCodeLine ("sta (sp),y");
1072 AddCodeLine ("iny");
1073 AddCodeLine ("txa");
1074 AddCodeLine ("sta (sp),y");
1080 if (Flags & CF_CONST) {
1081 g_getimmed (Flags, Val, 0);
1084 AddCodeLine ("jsr steaxysp");
1095 void g_putind (unsigned Flags, unsigned Offs)
1096 /* Store the specified object type in the primary register at the address
1097 * on the top of the stack
1100 /* We can handle offsets below $100 directly, larger offsets must be added
1101 * to the address. Since a/x is in use, best code is achieved by adding
1102 * just the high byte. Be sure to check if the low byte will overflow while
1105 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1107 /* Overflow - we need to add the low byte also */
1108 AddCodeLine ("ldy #$00");
1109 AddCodeLine ("clc");
1110 AddCodeLine ("pha");
1111 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1112 AddCodeLine ("adc (sp),y");
1113 AddCodeLine ("sta (sp),y");
1114 AddCodeLine ("iny");
1115 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1116 AddCodeLine ("adc (sp),y");
1117 AddCodeLine ("sta (sp),y");
1118 AddCodeLine ("pla");
1120 /* Complete address is on stack, new offset is zero */
1123 } else if ((Offs & 0xFF00) != 0) {
1125 /* We can just add the high byte */
1126 AddCodeLine ("ldy #$01");
1127 AddCodeLine ("clc");
1128 AddCodeLine ("pha");
1129 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1130 AddCodeLine ("adc (sp),y");
1131 AddCodeLine ("sta (sp),y");
1132 AddCodeLine ("pla");
1134 /* Offset is now just the low byte */
1138 /* Check the size and determine operation */
1139 switch (Flags & CF_TYPE) {
1143 AddCodeLine ("jsr staspidx");
1148 AddCodeLine ("jsr staxspidx");
1153 AddCodeLine ("jsr steaxspidx");
1161 /* Pop the argument which is always a pointer */
1167 /*****************************************************************************/
1168 /* type conversion and similiar stuff */
1169 /*****************************************************************************/
1173 void g_toslong (unsigned flags)
1174 /* Make sure, the value on TOS is a long. Convert if necessary */
1176 switch (flags & CF_TYPE) {
1180 if (flags & CF_UNSIGNED) {
1181 AddCodeLine ("jsr tosulong");
1183 AddCodeLine ("jsr toslong");
1198 void g_tosint (unsigned flags)
1199 /* Make sure, the value on TOS is an int. Convert if necessary */
1201 switch (flags & CF_TYPE) {
1208 AddCodeLine ("jsr tosint");
1219 void g_regint (unsigned Flags)
1220 /* Make sure, the value in the primary register an int. Convert if necessary */
1224 switch (Flags & CF_TYPE) {
1227 if (Flags & CF_FORCECHAR) {
1228 /* Conversion is from char */
1229 if (Flags & CF_UNSIGNED) {
1230 AddCodeLine ("ldx #$00");
1232 L = GetLocalLabel();
1233 AddCodeLine ("ldx #$00");
1234 AddCodeLine ("cmp #$80");
1235 AddCodeLine ("bcc %s", LocalLabelName (L));
1236 AddCodeLine ("dex");
1253 void g_reglong (unsigned Flags)
1254 /* Make sure, the value in the primary register a long. Convert if necessary */
1258 switch (Flags & CF_TYPE) {
1261 if (Flags & CF_FORCECHAR) {
1262 /* Conversion is from char */
1263 if (Flags & CF_UNSIGNED) {
1264 if (IS_Get (&CodeSizeFactor) >= 200) {
1265 AddCodeLine ("ldx #$00");
1266 AddCodeLine ("stx sreg");
1267 AddCodeLine ("stx sreg+1");
1269 AddCodeLine ("jsr aulong");
1272 if (IS_Get (&CodeSizeFactor) >= 366) {
1273 L = GetLocalLabel();
1274 AddCodeLine ("ldx #$00");
1275 AddCodeLine ("cmp #$80");
1276 AddCodeLine ("bcc %s", LocalLabelName (L));
1277 AddCodeLine ("dex");
1279 AddCodeLine ("stx sreg");
1280 AddCodeLine ("stx sreg+1");
1282 AddCodeLine ("jsr along");
1289 if (Flags & CF_UNSIGNED) {
1290 if (IS_Get (&CodeSizeFactor) >= 200) {
1292 AddCodeLine ("sty sreg");
1293 AddCodeLine ("sty sreg+1");
1295 AddCodeLine ("jsr axulong");
1298 AddCodeLine ("jsr axlong");
1312 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1313 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1314 * value, that corresponds to the value on TOS, rhs corresponds to the value
1315 * in (e)ax. The return value is the the flags value for the resulting type.
1318 unsigned ltype, rtype;
1321 /* Get the type spec from the flags */
1322 ltype = lhs & CF_TYPE;
1323 rtype = rhs & CF_TYPE;
1325 /* Check if a conversion is needed */
1326 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1327 /* We must promote the primary register to long */
1329 /* Get the new rhs type */
1330 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1332 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1333 /* We must promote the lhs to long */
1339 /* Get the new rhs type */
1340 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1344 /* Determine the result type for the operation:
1345 * - The result is const if both operands are const.
1346 * - The result is unsigned if one of the operands is unsigned.
1347 * - The result is long if one of the operands is long.
1348 * - Otherwise the result is int sized.
1350 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1351 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1352 if (rtype == CF_LONG || ltype == CF_LONG) {
1362 unsigned g_typecast (unsigned lhs, unsigned rhs)
1363 /* Cast the value in the primary register to the operand size that is flagged
1364 * by the lhs value. Return the result value.
1367 unsigned ltype, rtype;
1369 /* Get the type spec from the flags */
1370 ltype = lhs & CF_TYPE;
1371 rtype = rhs & CF_TYPE;
1373 /* Check if a conversion is needed */
1374 if ((rhs & CF_CONST) == 0) {
1375 if (ltype == CF_LONG && rtype != CF_LONG) {
1376 /* We must promote the primary register to long */
1378 } else if (ltype == CF_INT && rtype != CF_INT) {
1379 /* We must promote the primary register to int */
1384 /* Do not need any other action. If the left type is int, and the primary
1385 * register is long, it will be automagically truncated. If the right hand
1386 * side is const, it is not located in the primary register and handled by
1387 * the expression parser code.
1390 /* Result is const if the right hand side was const */
1391 lhs |= (rhs & CF_CONST);
1393 /* The resulting type is that of the left hand side (that's why you called
1401 void g_scale (unsigned flags, long val)
1402 /* Scale the value in the primary register by the given value. If val is positive,
1403 * scale up, is val is negative, scale down. This function is used to scale
1404 * the operands or results of pointer arithmetic by the size of the type, the
1405 * pointer points to.
1410 /* Value may not be zero */
1412 Internal ("Data type has no size");
1413 } else if (val > 0) {
1416 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1418 /* Factor is 2, 4, 8 and 16, use special function */
1419 switch (flags & CF_TYPE) {
1422 if (flags & CF_FORCECHAR) {
1424 AddCodeLine ("asl a");
1431 if (IS_Get (&CodeSizeFactor) >= (p2+1)*130) {
1432 AddCodeLine ("stx tmp1");
1434 AddCodeLine ("asl a");
1435 AddCodeLine ("rol tmp1");
1437 AddCodeLine ("ldx tmp1");
1439 if (flags & CF_UNSIGNED) {
1440 AddCodeLine ("jsr shlax%d", p2);
1442 AddCodeLine ("jsr aslax%d", p2);
1448 if (flags & CF_UNSIGNED) {
1449 AddCodeLine ("jsr shleax%d", p2);
1451 AddCodeLine ("jsr asleax%d", p2);
1460 } else if (val != 1) {
1462 /* Use a multiplication instead */
1463 g_mul (flags | CF_CONST, val);
1471 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1473 /* Factor is 2, 4, 8 and 16 use special function */
1474 switch (flags & CF_TYPE) {
1477 if (flags & CF_FORCECHAR) {
1478 if (flags & CF_UNSIGNED) {
1480 AddCodeLine ("lsr a");
1483 } else if (p2 <= 2) {
1484 AddCodeLine ("cmp #$80");
1485 AddCodeLine ("ror a");
1492 if (flags & CF_UNSIGNED) {
1493 if (IS_Get (&CodeSizeFactor) >= (p2+1)*130) {
1494 AddCodeLine ("stx tmp1");
1496 AddCodeLine ("lsr tmp1");
1497 AddCodeLine ("ror a");
1499 AddCodeLine ("ldx tmp1");
1501 AddCodeLine ("jsr lsrax%d", p2);
1504 if (IS_Get (&CodeSizeFactor) >= (p2+1)*150) {
1505 AddCodeLine ("stx tmp1");
1507 AddCodeLine ("cpx #$80");
1508 AddCodeLine ("ror tmp1");
1509 AddCodeLine ("ror a");
1511 AddCodeLine ("ldx tmp1");
1513 AddCodeLine ("jsr asrax%d", p2);
1519 if (flags & CF_UNSIGNED) {
1520 AddCodeLine ("jsr lsreax%d", p2);
1522 AddCodeLine ("jsr asreax%d", p2);
1531 } else if (val != 1) {
1533 /* Use a division instead */
1534 g_div (flags | CF_CONST, val);
1542 /*****************************************************************************/
1543 /* Adds and subs of variables fix a fixed address */
1544 /*****************************************************************************/
1548 void g_addlocal (unsigned flags, int offs)
1549 /* Add a local variable to ax */
1553 /* Correct the offset and check it */
1555 CheckLocalOffs (offs);
1557 switch (flags & CF_TYPE) {
1560 L = GetLocalLabel();
1561 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1562 AddCodeLine ("clc");
1563 AddCodeLine ("adc (sp),y");
1564 AddCodeLine ("bcc %s", LocalLabelName (L));
1565 AddCodeLine ("inx");
1570 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1571 AddCodeLine ("clc");
1572 AddCodeLine ("adc (sp),y");
1573 AddCodeLine ("pha");
1574 AddCodeLine ("txa");
1575 AddCodeLine ("iny");
1576 AddCodeLine ("adc (sp),y");
1577 AddCodeLine ("tax");
1578 AddCodeLine ("pla");
1582 /* Do it the old way */
1584 g_getlocal (flags, offs);
1596 void g_addstatic (unsigned flags, unsigned long label, long offs)
1597 /* Add a static variable to ax */
1601 /* Create the correct label name */
1602 const char* lbuf = GetLabelName (flags, label, offs);
1604 switch (flags & CF_TYPE) {
1607 L = GetLocalLabel();
1608 AddCodeLine ("clc");
1609 AddCodeLine ("adc %s", lbuf);
1610 AddCodeLine ("bcc %s", LocalLabelName (L));
1611 AddCodeLine ("inx");
1616 AddCodeLine ("clc");
1617 AddCodeLine ("adc %s", lbuf);
1618 AddCodeLine ("tay");
1619 AddCodeLine ("txa");
1620 AddCodeLine ("adc %s+1", lbuf);
1621 AddCodeLine ("tax");
1622 AddCodeLine ("tya");
1626 /* Do it the old way */
1628 g_getstatic (flags, label, offs);
1640 /*****************************************************************************/
1641 /* Special op= functions */
1642 /*****************************************************************************/
1646 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1648 /* Emit += for a static variable */
1650 /* Create the correct label name */
1651 const char* lbuf = GetLabelName (flags, label, offs);
1653 /* Check the size and determine operation */
1654 switch (flags & CF_TYPE) {
1657 if (flags & CF_FORCECHAR) {
1658 AddCodeLine ("ldx #$00");
1659 if (flags & CF_CONST) {
1661 AddCodeLine ("inc %s", lbuf);
1662 AddCodeLine ("lda %s", lbuf);
1664 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1665 AddCodeLine ("clc");
1666 AddCodeLine ("adc %s", lbuf);
1667 AddCodeLine ("sta %s", lbuf);
1670 AddCodeLine ("clc");
1671 AddCodeLine ("adc %s", lbuf);
1672 AddCodeLine ("sta %s", lbuf);
1674 if ((flags & CF_UNSIGNED) == 0) {
1675 unsigned L = GetLocalLabel();
1676 AddCodeLine ("bpl %s", LocalLabelName (L));
1677 AddCodeLine ("dex");
1685 if (flags & CF_CONST) {
1687 unsigned L = GetLocalLabel ();
1688 AddCodeLine ("inc %s", lbuf);
1689 AddCodeLine ("bne %s", LocalLabelName (L));
1690 AddCodeLine ("inc %s+1", lbuf);
1692 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1693 AddCodeLine ("ldx %s+1", lbuf);
1695 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1696 AddCodeLine ("clc");
1697 AddCodeLine ("adc %s", lbuf);
1698 AddCodeLine ("sta %s", lbuf);
1700 unsigned L = GetLocalLabel ();
1701 AddCodeLine ("bcc %s", LocalLabelName (L));
1702 AddCodeLine ("inc %s+1", lbuf);
1704 AddCodeLine ("ldx %s+1", lbuf);
1706 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1707 AddCodeLine ("adc %s+1", lbuf);
1708 AddCodeLine ("sta %s+1", lbuf);
1709 AddCodeLine ("tax");
1710 AddCodeLine ("lda %s", lbuf);
1714 AddCodeLine ("clc");
1715 AddCodeLine ("adc %s", lbuf);
1716 AddCodeLine ("sta %s", lbuf);
1717 AddCodeLine ("txa");
1718 AddCodeLine ("adc %s+1", lbuf);
1719 AddCodeLine ("sta %s+1", lbuf);
1720 AddCodeLine ("tax");
1721 AddCodeLine ("lda %s", lbuf);
1726 if (flags & CF_CONST) {
1728 AddCodeLine ("ldy #<(%s)", lbuf);
1729 AddCodeLine ("sty ptr1");
1730 AddCodeLine ("ldy #>(%s)", lbuf);
1732 AddCodeLine ("jsr laddeq1");
1734 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1735 AddCodeLine ("jsr laddeqa");
1738 g_getstatic (flags, label, offs);
1740 g_putstatic (flags, label, offs);
1743 AddCodeLine ("ldy #<(%s)", lbuf);
1744 AddCodeLine ("sty ptr1");
1745 AddCodeLine ("ldy #>(%s)", lbuf);
1746 AddCodeLine ("jsr laddeq");
1757 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1758 /* Emit += for a local variable */
1760 /* Calculate the true offset, check it, load it into Y */
1762 CheckLocalOffs (offs);
1764 /* Check the size and determine operation */
1765 switch (flags & CF_TYPE) {
1768 if (flags & CF_FORCECHAR) {
1770 AddCodeLine ("ldx #$00");
1771 if (flags & CF_CONST) {
1772 AddCodeLine ("clc");
1773 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1774 AddCodeLine ("adc (sp),y");
1775 AddCodeLine ("sta (sp),y");
1777 AddCodeLine ("clc");
1778 AddCodeLine ("adc (sp),y");
1779 AddCodeLine ("sta (sp),y");
1781 if ((flags & CF_UNSIGNED) == 0) {
1782 unsigned L = GetLocalLabel();
1783 AddCodeLine ("bpl %s", LocalLabelName (L));
1784 AddCodeLine ("dex");
1793 if (flags & CF_CONST) {
1794 if (IS_Get (&CodeSizeFactor) >= 400) {
1795 AddCodeLine ("clc");
1796 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1797 AddCodeLine ("adc (sp),y");
1798 AddCodeLine ("sta (sp),y");
1799 AddCodeLine ("iny");
1800 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1801 AddCodeLine ("adc (sp),y");
1802 AddCodeLine ("sta (sp),y");
1803 AddCodeLine ("tax");
1804 AddCodeLine ("dey");
1805 AddCodeLine ("lda (sp),y");
1807 g_getimmed (flags, val, 0);
1808 AddCodeLine ("jsr addeqysp");
1811 AddCodeLine ("jsr addeqysp");
1816 if (flags & CF_CONST) {
1817 g_getimmed (flags, val, 0);
1820 AddCodeLine ("jsr laddeqysp");
1830 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1831 /* Emit += for the location with address in ax */
1833 /* If the offset is too large for a byte register, add the high byte
1834 * of the offset to the primary. Beware: We need a special correction
1835 * if the offset in the low byte will overflow in the operation.
1837 offs = MakeByteOffs (flags, offs);
1839 /* Check the size and determine operation */
1840 switch (flags & CF_TYPE) {
1843 AddCodeLine ("sta ptr1");
1844 AddCodeLine ("stx ptr1+1");
1845 AddCodeLine ("ldy #$%02X", offs);
1846 AddCodeLine ("ldx #$00");
1847 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1848 AddCodeLine ("clc");
1849 AddCodeLine ("adc (ptr1),y");
1850 AddCodeLine ("sta (ptr1),y");
1854 if (IS_Get (&CodeSizeFactor) >= 200) {
1855 /* Lots of code, use only if size is not important */
1856 AddCodeLine ("sta ptr1");
1857 AddCodeLine ("stx ptr1+1");
1858 AddCodeLine ("ldy #$%02X", offs);
1859 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1860 AddCodeLine ("clc");
1861 AddCodeLine ("adc (ptr1),y");
1862 AddCodeLine ("sta (ptr1),y");
1863 AddCodeLine ("pha");
1864 AddCodeLine ("iny");
1865 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1866 AddCodeLine ("adc (ptr1),y");
1867 AddCodeLine ("sta (ptr1),y");
1868 AddCodeLine ("tax");
1869 AddCodeLine ("pla");
1875 AddCodeLine ("jsr pushax"); /* Push the address */
1876 push (CF_PTR); /* Correct the internal sp */
1877 g_getind (flags, offs); /* Fetch the value */
1878 g_inc (flags, val); /* Increment value in primary */
1879 g_putind (flags, offs); /* Store the value back */
1889 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1891 /* Emit -= for a static variable */
1893 /* Create the correct label name */
1894 const char* lbuf = GetLabelName (flags, label, offs);
1896 /* Check the size and determine operation */
1897 switch (flags & CF_TYPE) {
1900 if (flags & CF_FORCECHAR) {
1901 AddCodeLine ("ldx #$00");
1902 if (flags & CF_CONST) {
1904 AddCodeLine ("dec %s", lbuf);
1905 AddCodeLine ("lda %s", lbuf);
1907 AddCodeLine ("sec");
1908 AddCodeLine ("lda %s", lbuf);
1909 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1910 AddCodeLine ("sta %s", lbuf);
1913 AddCodeLine ("eor #$FF");
1914 AddCodeLine ("sec");
1915 AddCodeLine ("adc %s", lbuf);
1916 AddCodeLine ("sta %s", lbuf);
1918 if ((flags & CF_UNSIGNED) == 0) {
1919 unsigned L = GetLocalLabel();
1920 AddCodeLine ("bpl %s", LocalLabelName (L));
1921 AddCodeLine ("dex");
1929 AddCodeLine ("sec");
1930 if (flags & CF_CONST) {
1931 AddCodeLine ("lda %s", lbuf);
1932 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1933 AddCodeLine ("sta %s", lbuf);
1935 unsigned L = GetLocalLabel ();
1936 AddCodeLine ("bcs %s", LocalLabelName (L));
1937 AddCodeLine ("dec %s+1", lbuf);
1939 AddCodeLine ("ldx %s+1", lbuf);
1941 AddCodeLine ("lda %s+1", lbuf);
1942 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1943 AddCodeLine ("sta %s+1", lbuf);
1944 AddCodeLine ("tax");
1945 AddCodeLine ("lda %s", lbuf);
1948 AddCodeLine ("eor #$FF");
1949 AddCodeLine ("adc %s", lbuf);
1950 AddCodeLine ("sta %s", lbuf);
1951 AddCodeLine ("txa");
1952 AddCodeLine ("eor #$FF");
1953 AddCodeLine ("adc %s+1", lbuf);
1954 AddCodeLine ("sta %s+1", lbuf);
1955 AddCodeLine ("tax");
1956 AddCodeLine ("lda %s", lbuf);
1961 if (flags & CF_CONST) {
1963 AddCodeLine ("ldy #<(%s)", lbuf);
1964 AddCodeLine ("sty ptr1");
1965 AddCodeLine ("ldy #>(%s)", lbuf);
1966 AddCodeLine ("lda #$%02X", (unsigned char)val);
1967 AddCodeLine ("jsr lsubeqa");
1969 g_getstatic (flags, label, offs);
1971 g_putstatic (flags, label, offs);
1974 AddCodeLine ("ldy #<(%s)", lbuf);
1975 AddCodeLine ("sty ptr1");
1976 AddCodeLine ("ldy #>(%s)", lbuf);
1977 AddCodeLine ("jsr lsubeq");
1988 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1989 /* Emit -= for a local variable */
1991 /* Calculate the true offset, check it, load it into Y */
1993 CheckLocalOffs (offs);
1995 /* Check the size and determine operation */
1996 switch (flags & CF_TYPE) {
1999 if (flags & CF_FORCECHAR) {
2001 AddCodeLine ("ldx #$00");
2002 AddCodeLine ("sec");
2003 if (flags & CF_CONST) {
2004 AddCodeLine ("lda (sp),y");
2005 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2007 AddCodeLine ("eor #$FF");
2008 AddCodeLine ("adc (sp),y");
2010 AddCodeLine ("sta (sp),y");
2011 if ((flags & CF_UNSIGNED) == 0) {
2012 unsigned L = GetLocalLabel();
2013 AddCodeLine ("bpl %s", LocalLabelName (L));
2014 AddCodeLine ("dex");
2022 if (flags & CF_CONST) {
2023 g_getimmed (flags, val, 0);
2026 AddCodeLine ("jsr subeqysp");
2030 if (flags & CF_CONST) {
2031 g_getimmed (flags, val, 0);
2034 AddCodeLine ("jsr lsubeqysp");
2044 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2045 /* Emit -= for the location with address in ax */
2047 /* If the offset is too large for a byte register, add the high byte
2048 * of the offset to the primary. Beware: We need a special correction
2049 * if the offset in the low byte will overflow in the operation.
2051 offs = MakeByteOffs (flags, offs);
2053 /* Check the size and determine operation */
2054 switch (flags & CF_TYPE) {
2057 AddCodeLine ("sta ptr1");
2058 AddCodeLine ("stx ptr1+1");
2059 AddCodeLine ("ldy #$%02X", offs);
2060 AddCodeLine ("ldx #$00");
2061 AddCodeLine ("lda (ptr1),y");
2062 AddCodeLine ("sec");
2063 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2064 AddCodeLine ("sta (ptr1),y");
2068 if (IS_Get (&CodeSizeFactor) >= 200) {
2069 /* Lots of code, use only if size is not important */
2070 AddCodeLine ("sta ptr1");
2071 AddCodeLine ("stx ptr1+1");
2072 AddCodeLine ("ldy #$%02X", offs);
2073 AddCodeLine ("lda (ptr1),y");
2074 AddCodeLine ("sec");
2075 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2076 AddCodeLine ("sta (ptr1),y");
2077 AddCodeLine ("pha");
2078 AddCodeLine ("iny");
2079 AddCodeLine ("lda (ptr1),y");
2080 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2081 AddCodeLine ("sta (ptr1),y");
2082 AddCodeLine ("tax");
2083 AddCodeLine ("pla");
2089 AddCodeLine ("jsr pushax"); /* Push the address */
2090 push (CF_PTR); /* Correct the internal sp */
2091 g_getind (flags, offs); /* Fetch the value */
2092 g_dec (flags, val); /* Increment value in primary */
2093 g_putind (flags, offs); /* Store the value back */
2103 /*****************************************************************************/
2104 /* Add a variable address to the value in ax */
2105 /*****************************************************************************/
2109 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2110 /* Add the address of a local variable to ax */
2114 /* Add the offset */
2117 /* We cannot address more then 256 bytes of locals anyway */
2118 L = GetLocalLabel();
2119 CheckLocalOffs (offs);
2120 AddCodeLine ("clc");
2121 AddCodeLine ("adc #$%02X", offs & 0xFF);
2122 /* Do also skip the CLC insn below */
2123 AddCodeLine ("bcc %s", LocalLabelName (L));
2124 AddCodeLine ("inx");
2127 /* Add the current stackpointer value */
2128 AddCodeLine ("clc");
2130 /* Label was used above */
2133 AddCodeLine ("adc sp");
2134 AddCodeLine ("tay");
2135 AddCodeLine ("txa");
2136 AddCodeLine ("adc sp+1");
2137 AddCodeLine ("tax");
2138 AddCodeLine ("tya");
2143 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2144 /* Add the address of a static variable to ax */
2146 /* Create the correct label name */
2147 const char* lbuf = GetLabelName (flags, label, offs);
2149 /* Add the address to the current ax value */
2150 AddCodeLine ("clc");
2151 AddCodeLine ("adc #<(%s)", lbuf);
2152 AddCodeLine ("tay");
2153 AddCodeLine ("txa");
2154 AddCodeLine ("adc #>(%s)", lbuf);
2155 AddCodeLine ("tax");
2156 AddCodeLine ("tya");
2161 /*****************************************************************************/
2163 /*****************************************************************************/
2167 void g_save (unsigned flags)
2168 /* Copy primary register to hold register. */
2170 /* Check the size and determine operation */
2171 switch (flags & CF_TYPE) {
2174 if (flags & CF_FORCECHAR) {
2175 AddCodeLine ("pha");
2181 AddCodeLine ("sta regsave");
2182 AddCodeLine ("stx regsave+1");
2186 AddCodeLine ("jsr saveeax");
2196 void g_restore (unsigned flags)
2197 /* Copy hold register to primary. */
2199 /* Check the size and determine operation */
2200 switch (flags & CF_TYPE) {
2203 if (flags & CF_FORCECHAR) {
2204 AddCodeLine ("pla");
2210 AddCodeLine ("lda regsave");
2211 AddCodeLine ("ldx regsave+1");
2215 AddCodeLine ("jsr resteax");
2225 void g_cmp (unsigned flags, unsigned long val)
2226 /* Immidiate compare. The primary register will not be changed, Z flag
2232 /* Check the size and determine operation */
2233 switch (flags & CF_TYPE) {
2236 if (flags & CF_FORCECHAR) {
2237 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2243 L = GetLocalLabel();
2244 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2245 AddCodeLine ("bne %s", LocalLabelName (L));
2246 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2251 Internal ("g_cmp: Long compares not implemented");
2261 static void oper (unsigned flags, unsigned long val, char** subs)
2262 /* Encode a binary operation. subs is a pointer to four groups of three
2264 * 0-2 --> Operate on ints
2265 * 3-5 --> Operate on unsigneds
2266 * 6-8 --> Operate on longs
2267 * 9-11 --> Operate on unsigned longs
2269 * The first subroutine names in each string group is used to encode an
2270 * operation with a zero constant, the second to encode an operation with
2271 * a 8 bit constant, and the third is used in all other cases.
2276 /* Determine the offset into the array */
2277 offs = (flags & CF_UNSIGNED)? 3 : 0;
2278 switch (flags & CF_TYPE) {
2291 /* Encode the operation */
2292 if (flags & CF_CONST) {
2293 /* Constant value given */
2294 if (val == 0 && subs [offs+0]) {
2295 /* Special case: constant with value zero */
2296 AddCodeLine ("jsr %s", subs [offs+0]);
2297 } else if (val < 0x100 && subs [offs+1]) {
2298 /* Special case: constant with high byte zero */
2299 ldaconst (val); /* Load low byte */
2300 AddCodeLine ("jsr %s", subs [offs+1]);
2302 /* Others: arbitrary constant value */
2303 g_getimmed (flags, val, 0); /* Load value */
2304 AddCodeLine ("jsr %s", subs [offs+2]);
2307 /* Value not constant (is already in (e)ax) */
2308 AddCodeLine ("jsr %s", subs [offs+2]);
2311 /* The operation will pop it's argument */
2317 void g_test (unsigned flags)
2318 /* Test the value in the primary and set the condition codes */
2320 switch (flags & CF_TYPE) {
2323 if (flags & CF_FORCECHAR) {
2324 AddCodeLine ("tax");
2330 AddCodeLine ("stx tmp1");
2331 AddCodeLine ("ora tmp1");
2335 if (flags & CF_UNSIGNED) {
2336 AddCodeLine ("jsr utsteax");
2338 AddCodeLine ("jsr tsteax");
2350 void g_push (unsigned flags, unsigned long val)
2351 /* Push the primary register or a constant value onto the stack */
2353 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2355 /* We have a constant 8 or 16 bit value */
2356 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2358 /* Handle as 8 bit value */
2360 AddCodeLine ("jsr pusha");
2364 /* Handle as 16 bit value */
2365 g_getimmed (flags, val, 0);
2366 AddCodeLine ("jsr pushax");
2371 /* Value is not 16 bit or not constant */
2372 if (flags & CF_CONST) {
2373 /* Constant 32 bit value, load into eax */
2374 g_getimmed (flags, val, 0);
2377 /* Push the primary register */
2378 switch (flags & CF_TYPE) {
2381 if (flags & CF_FORCECHAR) {
2382 /* Handle as char */
2383 AddCodeLine ("jsr pusha");
2388 AddCodeLine ("jsr pushax");
2392 AddCodeLine ("jsr pusheax");
2402 /* Adjust the stack offset */
2408 void g_swap (unsigned flags)
2409 /* Swap the primary register and the top of the stack. flags give the type
2410 * of *both* values (must have same size).
2413 switch (flags & CF_TYPE) {
2417 AddCodeLine ("jsr swapstk");
2421 AddCodeLine ("jsr swapestk");
2432 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2433 /* Call the specified subroutine name */
2435 if ((Flags & CF_FIXARGC) == 0) {
2436 /* Pass the argument count */
2439 AddCodeLine ("jsr _%s", Label);
2440 StackPtr += ArgSize; /* callee pops args */
2445 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2446 /* Call subroutine indirect */
2448 if ((Flags & CF_LOCAL) == 0) {
2449 /* Address is in a/x */
2450 if ((Flags & CF_FIXARGC) == 0) {
2451 /* Pass arg count */
2454 AddCodeLine ("jsr callax");
2456 /* The address is on stack, offset is on Val */
2458 CheckLocalOffs (Offs);
2459 AddCodeLine ("pha");
2460 AddCodeLine ("ldy #$%02X", Offs);
2461 AddCodeLine ("lda (sp),y");
2462 AddCodeLine ("sta jmpvec+1");
2463 AddCodeLine ("iny");
2464 AddCodeLine ("lda (sp),y");
2465 AddCodeLine ("sta jmpvec+2");
2466 AddCodeLine ("pla");
2467 AddCodeLine ("jsr jmpvec");
2470 /* Callee pops args */
2471 StackPtr += ArgSize;
2476 void g_jump (unsigned Label)
2477 /* Jump to specified internal label number */
2479 AddCodeLine ("jmp %s", LocalLabelName (Label));
2484 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2485 /* Jump to label if zero flag clear */
2487 AddCodeLine ("jne %s", LocalLabelName (label));
2492 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2493 /* Jump to label if zero flag set */
2495 AddCodeLine ("jeq %s", LocalLabelName (label));
2500 static void mod_internal (int k, char* verb1, char* verb2)
2503 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2507 AddCodeLine ("jsr %ssp", verb2);
2513 void g_space (int space)
2514 /* Create or drop space on the stack */
2517 mod_internal (-space, "inc", "addy");
2518 } else if (space > 0) {
2519 mod_internal (space, "dec", "suby");
2525 void g_cstackcheck (void)
2526 /* Check for a C stack overflow */
2528 AddCodeLine ("jsr cstkchk");
2533 void g_stackcheck (void)
2534 /* Check for a stack overflow */
2536 AddCodeLine ("jsr stkchk");
2541 void g_add (unsigned flags, unsigned long val)
2542 /* Primary = TOS + Primary */
2544 static char* ops [12] = {
2545 0, "tosadda0", "tosaddax",
2546 0, "tosadda0", "tosaddax",
2551 if (flags & CF_CONST) {
2552 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2553 g_push (flags & ~CF_CONST, 0);
2555 oper (flags, val, ops);
2560 void g_sub (unsigned flags, unsigned long val)
2561 /* Primary = TOS - Primary */
2563 static char* ops [12] = {
2564 0, "tossuba0", "tossubax",
2565 0, "tossuba0", "tossubax",
2570 if (flags & CF_CONST) {
2571 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2572 g_push (flags & ~CF_CONST, 0);
2574 oper (flags, val, ops);
2579 void g_rsub (unsigned flags, unsigned long val)
2580 /* Primary = Primary - TOS */
2582 static char* ops [12] = {
2583 0, "tosrsuba0", "tosrsubax",
2584 0, "tosrsuba0", "tosrsubax",
2588 oper (flags, val, ops);
2593 void g_mul (unsigned flags, unsigned long val)
2594 /* Primary = TOS * Primary */
2596 static char* ops [12] = {
2597 0, "tosmula0", "tosmulax",
2598 0, "tosumula0", "tosumulax",
2605 /* Do strength reduction if the value is constant and a power of two */
2606 if (flags & CF_CONST && (p2 = PowerOf2 (val)) >= 0) {
2607 /* Generate a shift instead */
2612 /* If the right hand side is const, the lhs is not on stack but still
2613 * in the primary register.
2615 if (flags & CF_CONST) {
2617 switch (flags & CF_TYPE) {
2620 if (flags & CF_FORCECHAR) {
2621 /* Handle some special cases */
2625 AddCodeLine ("sta tmp1");
2626 AddCodeLine ("asl a");
2627 AddCodeLine ("clc");
2628 AddCodeLine ("adc tmp1");
2632 AddCodeLine ("sta tmp1");
2633 AddCodeLine ("asl a");
2634 AddCodeLine ("asl a");
2635 AddCodeLine ("clc");
2636 AddCodeLine ("adc tmp1");
2640 AddCodeLine ("sta tmp1");
2641 AddCodeLine ("asl a");
2642 AddCodeLine ("clc");
2643 AddCodeLine ("adc tmp1");
2644 AddCodeLine ("asl a");
2648 AddCodeLine ("sta tmp1");
2649 AddCodeLine ("asl a");
2650 AddCodeLine ("asl a");
2651 AddCodeLine ("clc");
2652 AddCodeLine ("adc tmp1");
2653 AddCodeLine ("asl a");
2662 AddCodeLine ("jsr mulax3");
2665 AddCodeLine ("jsr mulax5");
2668 AddCodeLine ("jsr mulax6");
2671 AddCodeLine ("jsr mulax7");
2674 AddCodeLine ("jsr mulax9");
2677 AddCodeLine ("jsr mulax10");
2689 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2690 * into the normal, non-optimized stuff.
2692 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2693 g_push (flags & ~CF_CONST, 0);
2697 /* Use long way over the stack */
2698 oper (flags, val, ops);
2703 void g_div (unsigned flags, unsigned long val)
2704 /* Primary = TOS / Primary */
2706 static char* ops [12] = {
2707 0, "tosdiva0", "tosdivax",
2708 0, "tosudiva0", "tosudivax",
2713 /* Do strength reduction if the value is constant and a power of two */
2715 if ((flags & CF_CONST) && (p2 = PowerOf2 (val)) >= 0) {
2716 /* Generate a shift instead */
2719 /* Generate a division */
2720 if (flags & CF_CONST) {
2721 /* lhs is not on stack */
2722 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2723 g_push (flags & ~CF_CONST, 0);
2725 oper (flags, val, ops);
2731 void g_mod (unsigned flags, unsigned long val)
2732 /* Primary = TOS % Primary */
2734 static char* ops [12] = {
2735 0, "tosmoda0", "tosmodax",
2736 0, "tosumoda0", "tosumodax",
2742 /* Check if we can do some cost reduction */
2743 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = PowerOf2 (val)) >= 0) {
2744 /* We can do that with an AND operation */
2745 g_and (flags, val - 1);
2747 /* Do it the hard way... */
2748 if (flags & CF_CONST) {
2749 /* lhs is not on stack */
2750 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2751 g_push (flags & ~CF_CONST, 0);
2753 oper (flags, val, ops);
2759 void g_or (unsigned flags, unsigned long val)
2760 /* Primary = TOS | Primary */
2762 static char* ops [12] = {
2763 0, "tosora0", "tosorax",
2764 0, "tosora0", "tosorax",
2769 /* If the right hand side is const, the lhs is not on stack but still
2770 * in the primary register.
2772 if (flags & CF_CONST) {
2774 switch (flags & CF_TYPE) {
2777 if (flags & CF_FORCECHAR) {
2778 if ((val & 0xFF) != 0) {
2779 AddCodeLine ("ora #$%02X", (unsigned char)val);
2787 if ((val & 0xFF) != 0) {
2788 AddCodeLine ("ora #$%02X", (unsigned char)val);
2790 } else if ((val & 0xFF00) == 0xFF00) {
2791 if ((val & 0xFF) != 0) {
2792 AddCodeLine ("ora #$%02X", (unsigned char)val);
2795 } else if (val != 0) {
2796 AddCodeLine ("ora #$%02X", (unsigned char)val);
2797 AddCodeLine ("pha");
2798 AddCodeLine ("txa");
2799 AddCodeLine ("ora #$%02X", (unsigned char)(val >> 8));
2800 AddCodeLine ("tax");
2801 AddCodeLine ("pla");
2807 if ((val & 0xFF) != 0) {
2808 AddCodeLine ("ora #$%02X", (unsigned char)val);
2818 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2819 * into the normal, non-optimized stuff. Note: The standard stuff will
2820 * always work with ints.
2822 flags &= ~CF_FORCECHAR;
2823 g_push (flags & ~CF_CONST, 0);
2826 /* Use long way over the stack */
2827 oper (flags, val, ops);
2832 void g_xor (unsigned flags, unsigned long val)
2833 /* Primary = TOS ^ Primary */
2835 static char* ops [12] = {
2836 0, "tosxora0", "tosxorax",
2837 0, "tosxora0", "tosxorax",
2843 /* If the right hand side is const, the lhs is not on stack but still
2844 * in the primary register.
2846 if (flags & CF_CONST) {
2848 switch (flags & CF_TYPE) {
2851 if (flags & CF_FORCECHAR) {
2852 if ((val & 0xFF) != 0) {
2853 AddCodeLine ("eor #$%02X", (unsigned char)val);
2862 AddCodeLine ("eor #$%02X", (unsigned char)val);
2864 } else if (val != 0) {
2865 if ((val & 0xFF) != 0) {
2866 AddCodeLine ("eor #$%02X", (unsigned char)val);
2868 AddCodeLine ("pha");
2869 AddCodeLine ("txa");
2870 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2871 AddCodeLine ("tax");
2872 AddCodeLine ("pla");
2879 AddCodeLine ("eor #$%02X", (unsigned char)val);
2889 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2890 * into the normal, non-optimized stuff. Note: The standard stuff will
2891 * always work with ints.
2893 flags &= ~CF_FORCECHAR;
2894 g_push (flags & ~CF_CONST, 0);
2897 /* Use long way over the stack */
2898 oper (flags, val, ops);
2903 void g_and (unsigned Flags, unsigned long Val)
2904 /* Primary = TOS & Primary */
2906 static char* ops [12] = {
2907 0, "tosanda0", "tosandax",
2908 0, "tosanda0", "tosandax",
2913 /* If the right hand side is const, the lhs is not on stack but still
2914 * in the primary register.
2916 if (Flags & CF_CONST) {
2918 switch (Flags & CF_TYPE) {
2921 if (Flags & CF_FORCECHAR) {
2922 if ((Val & 0xFF) != 0xFF) {
2923 AddCodeLine ("and #$%02X", (unsigned char)Val);
2929 if ((Val & 0xFFFF) != 0xFFFF) {
2934 } else if (Val != 0xFF) {
2935 AddCodeLine ("and #$%02X", (unsigned char)Val);
2937 } else if ((Val & 0xFF00) == 0xFF00) {
2938 AddCodeLine ("and #$%02X", (unsigned char)Val);
2939 } else if ((Val & 0x00FF) == 0x0000) {
2940 AddCodeLine ("txa");
2941 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2942 AddCodeLine ("tax");
2945 AddCodeLine ("tay");
2946 AddCodeLine ("txa");
2947 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2948 AddCodeLine ("tax");
2949 AddCodeLine ("tya");
2950 if ((Val & 0x00FF) != 0x00FF) {
2951 AddCodeLine ("and #$%02X", (unsigned char)Val);
2960 AddCodeLine ("stx sreg+1");
2961 AddCodeLine ("stx sreg");
2962 if ((Val & 0xFF) != 0xFF) {
2963 AddCodeLine ("and #$%02X", (unsigned char)Val);
2966 } else if (Val == 0xFF00) {
2968 AddCodeLine ("sta sreg+1");
2969 AddCodeLine ("sta sreg");
2978 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2979 * into the normal, non-optimized stuff. Note: The standard stuff will
2980 * always work with ints.
2982 Flags &= ~CF_FORCECHAR;
2983 g_push (Flags & ~CF_CONST, 0);
2986 /* Use long way over the stack */
2987 oper (Flags, Val, ops);
2992 void g_asr (unsigned flags, unsigned long val)
2993 /* Primary = TOS >> Primary */
2995 static char* ops [12] = {
3002 /* If the right hand side is const, the lhs is not on stack but still
3003 * in the primary register.
3005 if (flags & CF_CONST) {
3007 switch (flags & CF_TYPE) {
3011 if (val >= 8 && (flags & CF_UNSIGNED)) {
3012 AddCodeLine ("txa");
3019 } else if (val >= 1 && val <= 4) {
3020 if (flags & CF_UNSIGNED) {
3021 AddCodeLine ("jsr shrax%ld", val);
3023 AddCodeLine ("jsr asrax%ld", val);
3033 } else if (val >= 1 && val <= 4) {
3034 if (flags & CF_UNSIGNED) {
3035 AddCodeLine ("jsr shreax%ld", val);
3037 AddCodeLine ("jsr asreax%ld", val);
3040 } else if (val == 8 && (flags & CF_UNSIGNED)) {
3041 AddCodeLine ("txa");
3042 AddCodeLine ("ldx sreg");
3043 AddCodeLine ("ldy sreg+1");
3044 AddCodeLine ("sty sreg");
3045 AddCodeLine ("ldy #$00");
3046 AddCodeLine ("sty sreg+1");
3048 } else if (val == 16) {
3049 AddCodeLine ("ldy #$00");
3050 AddCodeLine ("ldx sreg+1");
3051 if ((flags & CF_UNSIGNED) == 0) {
3052 unsigned L = GetLocalLabel();
3053 AddCodeLine ("bpl %s", LocalLabelName (L));
3054 AddCodeLine ("dey");
3057 AddCodeLine ("lda sreg");
3058 AddCodeLine ("sty sreg+1");
3059 AddCodeLine ("sty sreg");
3068 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3069 * into the normal, non-optimized stuff. Note: The standard stuff will
3070 * always work with ints.
3072 flags &= ~CF_FORCECHAR;
3073 g_push (flags & ~CF_CONST, 0);
3076 /* Use long way over the stack */
3077 oper (flags, val, ops);
3082 void g_asl (unsigned flags, unsigned long val)
3083 /* Primary = TOS << Primary */
3085 static char* ops [12] = {
3093 /* If the right hand side is const, the lhs is not on stack but still
3094 * in the primary register.
3096 if (flags & CF_CONST) {
3098 switch (flags & CF_TYPE) {
3103 AddCodeLine ("tax");
3104 AddCodeLine ("lda #$00");
3110 } else if (val >= 1 && val <= 4) {
3111 if (flags & CF_UNSIGNED) {
3112 AddCodeLine ("jsr shlax%ld", val);
3114 AddCodeLine ("jsr aslax%ld", val);
3124 } else if (val >= 1 && val <= 4) {
3125 if (flags & CF_UNSIGNED) {
3126 AddCodeLine ("jsr shleax%ld", val);
3128 AddCodeLine ("jsr asleax%ld", val);
3131 } else if (val == 8) {
3132 AddCodeLine ("ldy sreg");
3133 AddCodeLine ("sty sreg+1");
3134 AddCodeLine ("stx sreg");
3135 AddCodeLine ("tax");
3136 AddCodeLine ("lda #$00");
3138 } else if (val == 16) {
3139 AddCodeLine ("stx sreg+1");
3140 AddCodeLine ("sta sreg");
3141 AddCodeLine ("lda #$00");
3142 AddCodeLine ("tax");
3151 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3152 * into the normal, non-optimized stuff. Note: The standard stuff will
3153 * always work with ints.
3155 flags &= ~CF_FORCECHAR;
3156 g_push (flags & ~CF_CONST, 0);
3159 /* Use long way over the stack */
3160 oper (flags, val, ops);
3165 void g_neg (unsigned Flags)
3166 /* Primary = -Primary */
3168 switch (Flags & CF_TYPE) {
3171 if (Flags & CF_FORCECHAR) {
3172 AddCodeLine ("eor #$FF");
3173 AddCodeLine ("clc");
3174 AddCodeLine ("adc #$01");
3180 AddCodeLine ("jsr negax");
3184 AddCodeLine ("jsr negeax");
3194 void g_bneg (unsigned flags)
3195 /* Primary = !Primary */
3197 switch (flags & CF_TYPE) {
3200 AddCodeLine ("jsr bnega");
3204 AddCodeLine ("jsr bnegax");
3208 AddCodeLine ("jsr bnegeax");
3218 void g_com (unsigned Flags)
3219 /* Primary = ~Primary */
3221 switch (Flags & CF_TYPE) {
3224 if (Flags & CF_FORCECHAR) {
3225 AddCodeLine ("eor #$FF");
3231 AddCodeLine ("jsr complax");
3235 AddCodeLine ("jsr compleax");
3245 void g_inc (unsigned flags, unsigned long val)
3246 /* Increment the primary register by a given number */
3248 /* Don't inc by zero */
3253 /* Generate code for the supported types */
3255 switch (flags & CF_TYPE) {
3258 if (flags & CF_FORCECHAR) {
3259 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3261 AddCodeLine ("ina");
3264 AddCodeLine ("clc");
3265 AddCodeLine ("adc #$%02X", (unsigned char)val);
3272 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) {
3273 unsigned L = GetLocalLabel();
3274 AddCodeLine ("ina");
3275 AddCodeLine ("bne %s", LocalLabelName (L));
3276 AddCodeLine ("inx");
3278 } else if (IS_Get (&CodeSizeFactor) < 200) {
3281 AddCodeLine ("jsr incax%lu", val);
3282 } else if (val <= 255) {
3284 AddCodeLine ("jsr incaxy");
3286 g_add (flags | CF_CONST, val);
3289 /* Inline the code */
3291 if ((val & 0xFF) != 0) {
3292 unsigned L = GetLocalLabel();
3293 AddCodeLine ("clc");
3294 AddCodeLine ("adc #$%02X", (unsigned char) val);
3295 AddCodeLine ("bcc %s", LocalLabelName (L));
3296 AddCodeLine ("inx");
3300 AddCodeLine ("inx");
3303 AddCodeLine ("inx");
3306 AddCodeLine ("inx");
3309 AddCodeLine ("clc");
3310 if ((val & 0xFF) != 0) {
3311 AddCodeLine ("adc #$%02X", (unsigned char) val);
3313 AddCodeLine ("pha");
3314 AddCodeLine ("txa");
3315 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3316 AddCodeLine ("tax");
3317 AddCodeLine ("pla");
3325 AddCodeLine ("jsr inceaxy");
3327 g_add (flags | CF_CONST, val);
3339 void g_dec (unsigned flags, unsigned long val)
3340 /* Decrement the primary register by a given number */
3342 /* Don't dec by zero */
3347 /* Generate code for the supported types */
3349 switch (flags & CF_TYPE) {
3352 if (flags & CF_FORCECHAR) {
3353 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3355 AddCodeLine ("dea");
3358 AddCodeLine ("sec");
3359 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3366 if (IS_Get (&CodeSizeFactor) < 200) {
3367 /* Use subroutines */
3369 AddCodeLine ("jsr decax%d", (int) val);
3370 } else if (val <= 255) {
3372 AddCodeLine ("jsr decaxy");
3374 g_sub (flags | CF_CONST, val);
3377 /* Inline the code */
3379 if ((val & 0xFF) != 0) {
3380 unsigned L = GetLocalLabel();
3381 AddCodeLine ("sec");
3382 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3383 AddCodeLine ("bcs %s", LocalLabelName (L));
3384 AddCodeLine ("dex");
3388 AddCodeLine ("dex");
3391 AddCodeLine ("dex");
3394 AddCodeLine ("sec");
3395 if ((val & 0xFF) != 0) {
3396 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3398 AddCodeLine ("pha");
3399 AddCodeLine ("txa");
3400 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3401 AddCodeLine ("tax");
3402 AddCodeLine ("pla");
3410 AddCodeLine ("jsr deceaxy");
3412 g_sub (flags | CF_CONST, val);
3425 * Following are the conditional operators. They compare the TOS against
3426 * the primary and put a literal 1 in the primary if the condition is
3427 * true, otherwise they clear the primary register
3432 void g_eq (unsigned flags, unsigned long val)
3433 /* Test for equal */
3435 static char* ops [12] = {
3436 "toseq00", "toseqa0", "toseqax",
3437 "toseq00", "toseqa0", "toseqax",
3444 /* If the right hand side is const, the lhs is not on stack but still
3445 * in the primary register.
3447 if (flags & CF_CONST) {
3449 switch (flags & CF_TYPE) {
3452 if (flags & CF_FORCECHAR) {
3453 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3454 AddCodeLine ("jsr booleq");
3460 L = GetLocalLabel();
3461 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3462 AddCodeLine ("bne %s", LocalLabelName (L));
3463 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3465 AddCodeLine ("jsr booleq");
3475 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3476 * into the normal, non-optimized stuff. Note: The standard stuff will
3477 * always work with ints.
3479 flags &= ~CF_FORCECHAR;
3480 g_push (flags & ~CF_CONST, 0);
3483 /* Use long way over the stack */
3484 oper (flags, val, ops);
3489 void g_ne (unsigned flags, unsigned long val)
3490 /* Test for not equal */
3492 static char* ops [12] = {
3493 "tosne00", "tosnea0", "tosneax",
3494 "tosne00", "tosnea0", "tosneax",
3501 /* If the right hand side is const, the lhs is not on stack but still
3502 * in the primary register.
3504 if (flags & CF_CONST) {
3506 switch (flags & CF_TYPE) {
3509 if (flags & CF_FORCECHAR) {
3510 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3511 AddCodeLine ("jsr boolne");
3517 L = GetLocalLabel();
3518 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3519 AddCodeLine ("bne %s", LocalLabelName (L));
3520 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3522 AddCodeLine ("jsr boolne");
3532 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3533 * into the normal, non-optimized stuff. Note: The standard stuff will
3534 * always work with ints.
3536 flags &= ~CF_FORCECHAR;
3537 g_push (flags & ~CF_CONST, 0);
3540 /* Use long way over the stack */
3541 oper (flags, val, ops);
3546 void g_lt (unsigned flags, unsigned long val)
3547 /* Test for less than */
3549 static char* ops [12] = {
3550 "toslt00", "toslta0", "tosltax",
3551 "tosult00", "tosulta0", "tosultax",
3556 /* If the right hand side is const, the lhs is not on stack but still
3557 * in the primary register.
3559 if (flags & CF_CONST) {
3561 /* Because the handling of the overflow flag is too complex for
3562 * inlining, we can handle only unsigned compares, and signed
3563 * compares against zero here.
3565 if (flags & CF_UNSIGNED) {
3567 /* Give a warning in some special cases */
3569 Warning ("Condition is never true");
3570 AddCodeLine ("jsr return0");
3574 /* Look at the type */
3575 switch (flags & CF_TYPE) {
3578 if (flags & CF_FORCECHAR) {
3579 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3580 AddCodeLine ("jsr boolult");
3586 /* If the low byte is zero, we must only test the high byte */
3587 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3588 if ((val & 0xFF) != 0) {
3589 unsigned L = GetLocalLabel();
3590 AddCodeLine ("bne %s", LocalLabelName (L));
3591 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3594 AddCodeLine ("jsr boolult");
3598 /* Do a subtraction */
3599 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3600 AddCodeLine ("txa");
3601 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3602 AddCodeLine ("lda sreg");
3603 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3604 AddCodeLine ("lda sreg+1");
3605 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3606 AddCodeLine ("jsr boolult");
3613 } else if (val == 0) {
3615 /* Look at the type */
3616 switch (flags & CF_TYPE) {
3619 if (flags & CF_FORCECHAR) {
3620 AddCodeLine ("tax");
3621 AddCodeLine ("jsr boollt");
3627 /* Just check the high byte */
3628 AddCodeLine ("txa");
3629 AddCodeLine ("jsr boollt");
3633 /* Just check the high byte */
3634 AddCodeLine ("lda sreg+1");
3635 AddCodeLine ("jsr boollt");
3643 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3644 * into the normal, non-optimized stuff. Note: The standard stuff will
3645 * always work with ints.
3647 flags &= ~CF_FORCECHAR;
3648 g_push (flags & ~CF_CONST, 0);
3651 /* Use long way over the stack */
3652 oper (flags, val, ops);
3657 void g_le (unsigned flags, unsigned long val)
3658 /* Test for less than or equal to */
3660 static char* ops [12] = {
3661 "tosle00", "toslea0", "tosleax",
3662 "tosule00", "tosulea0", "tosuleax",
3668 /* If the right hand side is const, the lhs is not on stack but still
3669 * in the primary register.
3671 if (flags & CF_CONST) {
3673 /* Look at the type */
3674 switch (flags & CF_TYPE) {
3677 if (flags & CF_FORCECHAR) {
3678 if (flags & CF_UNSIGNED) {
3679 /* Unsigned compare */
3681 /* Use < instead of <= because the former gives
3682 * better code on the 6502 than the latter.
3684 g_lt (flags, val+1);
3687 Warning ("Condition is always true");
3688 AddCodeLine ("jsr return1");
3691 /* Signed compare */
3692 if ((long) val < 0x7F) {
3693 /* Use < instead of <= because the former gives
3694 * better code on the 6502 than the latter.
3696 g_lt (flags, val+1);
3699 Warning ("Condition is always true");
3700 AddCodeLine ("jsr return1");
3708 if (flags & CF_UNSIGNED) {
3709 /* Unsigned compare */
3711 /* Use < instead of <= because the former gives
3712 * better code on the 6502 than the latter.
3714 g_lt (flags, val+1);
3717 Warning ("Condition is always true");
3718 AddCodeLine ("jsr return1");
3721 /* Signed compare */
3722 if ((long) val < 0x7FFF) {
3723 g_lt (flags, val+1);
3726 Warning ("Condition is always true");
3727 AddCodeLine ("jsr return1");
3733 if (flags & CF_UNSIGNED) {
3734 /* Unsigned compare */
3735 if (val < 0xFFFFFFFF) {
3736 /* Use < instead of <= because the former gives
3737 * better code on the 6502 than the latter.
3739 g_lt (flags, val+1);
3742 Warning ("Condition is always true");
3743 AddCodeLine ("jsr return1");
3746 /* Signed compare */
3747 if ((long) val < 0x7FFFFFFF) {
3748 g_lt (flags, val+1);
3751 Warning ("Condition is always true");
3752 AddCodeLine ("jsr return1");
3761 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3762 * into the normal, non-optimized stuff. Note: The standard stuff will
3763 * always work with ints.
3765 flags &= ~CF_FORCECHAR;
3766 g_push (flags & ~CF_CONST, 0);
3769 /* Use long way over the stack */
3770 oper (flags, val, ops);
3775 void g_gt (unsigned flags, unsigned long val)
3776 /* Test for greater than */
3778 static char* ops [12] = {
3779 "tosgt00", "tosgta0", "tosgtax",
3780 "tosugt00", "tosugta0", "tosugtax",
3786 /* If the right hand side is const, the lhs is not on stack but still
3787 * in the primary register.
3789 if (flags & CF_CONST) {
3791 /* Look at the type */
3792 switch (flags & CF_TYPE) {
3795 if (flags & CF_FORCECHAR) {
3796 if (flags & CF_UNSIGNED) {
3798 /* If we have a compare > 0, we will replace it by
3799 * != 0 here, since both are identical but the
3800 * latter is easier to optimize.
3803 } else if (val < 0xFF) {
3804 /* Use >= instead of > because the former gives
3805 * better code on the 6502 than the latter.
3807 g_ge (flags, val+1);
3810 Warning ("Condition is never true");
3811 AddCodeLine ("jsr return0");
3814 if ((long) val < 0x7F) {
3815 /* Use >= instead of > because the former gives
3816 * better code on the 6502 than the latter.
3818 g_ge (flags, val+1);
3821 Warning ("Condition is never true");
3822 AddCodeLine ("jsr return0");
3830 if (flags & CF_UNSIGNED) {
3831 /* Unsigned compare */
3833 /* If we have a compare > 0, we will replace it by
3834 * != 0 here, since both are identical but the latter
3835 * is easier to optimize.
3838 } else if (val < 0xFFFF) {
3839 /* Use >= instead of > because the former gives better
3840 * code on the 6502 than the latter.
3842 g_ge (flags, val+1);
3845 Warning ("Condition is never true");
3846 AddCodeLine ("jsr return0");
3849 /* Signed compare */
3850 if ((long) val < 0x7FFF) {
3851 g_ge (flags, val+1);
3854 Warning ("Condition is never true");
3855 AddCodeLine ("jsr return0");
3861 if (flags & CF_UNSIGNED) {
3862 /* Unsigned compare */
3864 /* If we have a compare > 0, we will replace it by
3865 * != 0 here, since both are identical but the latter
3866 * is easier to optimize.
3869 } else if (val < 0xFFFFFFFF) {
3870 /* Use >= instead of > because the former gives better
3871 * code on the 6502 than the latter.
3873 g_ge (flags, val+1);
3876 Warning ("Condition is never true");
3877 AddCodeLine ("jsr return0");
3880 /* Signed compare */
3881 if ((long) val < 0x7FFFFFFF) {
3882 g_ge (flags, val+1);
3885 Warning ("Condition is never true");
3886 AddCodeLine ("jsr return0");
3895 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3896 * into the normal, non-optimized stuff. Note: The standard stuff will
3897 * always work with ints.
3899 flags &= ~CF_FORCECHAR;
3900 g_push (flags & ~CF_CONST, 0);
3903 /* Use long way over the stack */
3904 oper (flags, val, ops);
3909 void g_ge (unsigned flags, unsigned long val)
3910 /* Test for greater than or equal to */
3912 static char* ops [12] = {
3913 "tosge00", "tosgea0", "tosgeax",
3914 "tosuge00", "tosugea0", "tosugeax",
3920 /* If the right hand side is const, the lhs is not on stack but still
3921 * in the primary register.
3923 if (flags & CF_CONST) {
3925 /* Because the handling of the overflow flag is too complex for
3926 * inlining, we can handle only unsigned compares, and signed
3927 * compares against zero here.
3929 if (flags & CF_UNSIGNED) {
3931 /* Give a warning in some special cases */
3933 Warning ("Condition is always true");
3934 AddCodeLine ("jsr return1");
3938 /* Look at the type */
3939 switch (flags & CF_TYPE) {
3942 if (flags & CF_FORCECHAR) {
3943 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3944 AddCodeLine ("jsr booluge");
3950 /* If the low byte is zero, we must only test the high byte */
3951 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3952 if ((val & 0xFF) != 0) {
3953 unsigned L = GetLocalLabel();
3954 AddCodeLine ("bne %s", LocalLabelName (L));
3955 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3958 AddCodeLine ("jsr booluge");
3962 /* Do a subtraction */
3963 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3964 AddCodeLine ("txa");
3965 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3966 AddCodeLine ("lda sreg");
3967 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3968 AddCodeLine ("lda sreg+1");
3969 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3970 AddCodeLine ("jsr booluge");
3977 } else if (val == 0) {
3979 /* Look at the type */
3980 switch (flags & CF_TYPE) {
3983 if (flags & CF_FORCECHAR) {
3984 AddCodeLine ("tax");
3985 AddCodeLine ("jsr boolge");
3991 /* Just test the high byte */
3992 AddCodeLine ("txa");
3993 AddCodeLine ("jsr boolge");
3997 /* Just test the high byte */
3998 AddCodeLine ("lda sreg+1");
3999 AddCodeLine ("jsr boolge");
4008 /* If we go here, we didn't emit code. Push the lhs on stack and fall
4009 * into the normal, non-optimized stuff. Note: The standard stuff will
4010 * always work with ints.
4012 flags &= ~CF_FORCECHAR;
4013 g_push (flags & ~CF_CONST, 0);
4016 /* Use long way over the stack */
4017 oper (flags, val, ops);
4022 /*****************************************************************************/
4023 /* Allocating static storage */
4024 /*****************************************************************************/
4028 void g_res (unsigned n)
4029 /* Reserve static storage, n bytes */
4031 AddDataLine ("\t.res\t%u,$00", n);
4036 void g_defdata (unsigned flags, unsigned long val, long offs)
4037 /* Define data with the size given in flags */
4039 if (flags & CF_CONST) {
4041 /* Numeric constant */
4042 switch (flags & CF_TYPE) {
4045 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
4049 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
4053 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
4064 /* Create the correct label name */
4065 const char* Label = GetLabelName (flags, val, offs);
4067 /* Labels are always 16 bit */
4068 AddDataLine ("\t.addr\t%s", Label);
4075 void g_defbytes (const void* Bytes, unsigned Count)
4076 /* Output a row of bytes as a constant */
4082 /* Cast the buffer pointer */
4083 const unsigned char* Data = (const unsigned char*) Bytes;
4085 /* Output the stuff */
4088 /* How many go into this line? */
4089 if ((Chunk = Count) > 16) {
4094 /* Output one line */
4095 strcpy (Buf, "\t.byte\t");
4098 B += sprintf (B, "$%02X", *Data++);
4104 /* Output the line */
4111 void g_zerobytes (unsigned Count)
4112 /* Output Count bytes of data initialized with zero */
4115 AddDataLine ("\t.res\t%u,$00", Count);
4121 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4122 /* Initialize a register variable from static initialization data */
4124 /* Register variables do always have less than 128 bytes */
4125 unsigned CodeLabel = GetLocalLabel ();
4127 g_defcodelabel (CodeLabel);
4128 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4129 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4130 AddCodeLine ("dex");
4131 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4136 void g_initauto (unsigned Label, unsigned Size)
4137 /* Initialize a local variable at stack offset zero from static data */
4139 unsigned CodeLabel = GetLocalLabel ();
4141 CheckLocalOffs (Size);
4144 g_defcodelabel (CodeLabel);
4145 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4146 AddCodeLine ("sta (sp),y");
4147 AddCodeLine ("dey");
4148 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4149 } else if (Size <= 256) {
4151 g_defcodelabel (CodeLabel);
4152 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4153 AddCodeLine ("sta (sp),y");
4154 AddCodeLine ("iny");
4155 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4156 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4162 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4163 /* Initialize a static local variable from static initialization data */
4166 unsigned CodeLabel = GetLocalLabel ();
4168 g_defcodelabel (CodeLabel);
4169 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4170 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4171 AddCodeLine ("dey");
4172 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4173 } else if (Size <= 256) {
4174 unsigned CodeLabel = GetLocalLabel ();
4176 g_defcodelabel (CodeLabel);
4177 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4178 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4179 AddCodeLine ("iny");
4180 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4181 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4183 /* Use the easy way here: memcpy */
4184 g_getimmed (CF_STATIC, VarLabel, 0);
4185 AddCodeLine ("jsr pushax");
4186 g_getimmed (CF_STATIC, InitLabel, 0);
4187 AddCodeLine ("jsr pushax");
4188 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4189 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4195 /*****************************************************************************/
4196 /* Switch statement */
4197 /*****************************************************************************/
4201 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4202 /* Generate code for a switch statement */
4204 unsigned NextLabel = 0;
4207 /* Setup registers and determine which compare insn to use */
4208 const char* Compare;
4211 Compare = "cmp #$%02X";
4214 Compare = "cpx #$%02X";
4217 AddCodeLine ("ldy sreg");
4218 Compare = "cpy #$%02X";
4221 AddCodeLine ("ldy sreg+1");
4222 Compare = "cpy #$%02X";
4225 Internal ("Invalid depth in g_switch: %u", Depth);
4228 /* Walk over all nodes */
4229 for (I = 0; I < CollCount (Nodes); ++I) {
4231 /* Get the next case node */
4232 CaseNode* N = CollAtUnchecked (Nodes, I);
4234 /* If we have a next label, define it */
4236 g_defcodelabel (NextLabel);
4240 /* Do the compare */
4241 AddCodeLine (Compare, CN_GetValue (N));
4243 /* If this is the last level, jump directly to the case code if found */
4246 /* Branch if equal */
4247 g_falsejump (0, CN_GetLabel (N));
4251 /* Determine the next label */
4252 if (I == CollCount (Nodes) - 1) {
4253 /* Last node means not found */
4254 g_truejump (0, DefaultLabel);
4256 /* Jump to the next check */
4257 NextLabel = GetLocalLabel ();
4258 g_truejump (0, NextLabel);
4261 /* Check the next level */
4262 g_switch (N->Nodes, DefaultLabel, Depth-1);
4267 /* If we go here, we haven't found the label */
4268 g_jump (DefaultLabel);
4273 /*****************************************************************************/
4274 /* User supplied assembler code */
4275 /*****************************************************************************/
4279 void g_asmcode (struct StrBuf* B)
4280 /* Output one line of assembler code. */
4282 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));