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 /*****************************************************************************/
63 /*****************************************************************************/
65 /*****************************************************************************/
69 /* Compiler relative stack pointer */
74 /*****************************************************************************/
76 /*****************************************************************************/
80 static void typeerror (unsigned type)
81 /* Print an error message about an invalid operand type */
83 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
88 static void CheckLocalOffs (unsigned Offs)
89 /* Check the offset into the stack for 8bit range */
92 /* Too many local vars */
93 Error ("Too many local variables");
99 static const char* GetLabelName (unsigned Flags, unsigned long Label, long Offs)
101 static char Buf [256]; /* Label name */
103 /* Create the correct label name */
104 switch (Flags & CF_ADDRMASK) {
107 /* Static memory cell */
109 xsprintf (Buf, sizeof (Buf), "%s%+ld", LocalLabelName (Label), Offs);
111 xsprintf (Buf, sizeof (Buf), "%s", LocalLabelName (Label));
118 xsprintf (Buf, sizeof (Buf), "_%s%+ld", (char*) Label, Offs);
120 xsprintf (Buf, sizeof (Buf), "_%s", (char*) Label);
125 /* Absolute address */
126 xsprintf (Buf, sizeof (Buf), "$%04X", (int)((Label+Offs) & 0xFFFF));
130 /* Variable in register bank */
131 xsprintf (Buf, sizeof (Buf), "regbank+%u", (unsigned)((Label+Offs) & 0xFFFF));
135 Internal ("Invalid address flags: %04X", Flags);
138 /* Return a pointer to the static buffer */
144 /*****************************************************************************/
145 /* Pre- and postamble */
146 /*****************************************************************************/
150 void g_preamble (void)
151 /* Generate the assembler code preamble */
153 /* Create a new (global) segment list and remember it */
157 /* Identify the compiler version */
159 AddTextLine ("; File generated by cc65 v %u.%u.%u",
160 VER_MAJOR, VER_MINOR, VER_PATCH);
163 /* Insert some object file options */
164 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
165 VER_MAJOR, VER_MINOR, VER_PATCH);
167 /* If we're producing code for some other CPU, switch the command set */
169 case CPU_6502: AddTextLine ("\t.setcpu\t\t\"6502\""); break;
170 case CPU_6502X: AddTextLine ("\t.setcpu\t\t\"6502X\""); break;
171 case CPU_65SC02: AddTextLine ("\t.setcpu\t\t\"65SC02\""); break;
172 case CPU_65C02: AddTextLine ("\t.setcpu\t\t\"65C02\""); break;
173 case CPU_65816: AddTextLine ("\t.setcpu\t\t\"65816\""); break;
174 default: Internal ("Unknown CPU: %d", CPU);
178 AddTextLine ("\t.smart\t\ton");
180 /* Allow auto import for runtime library routines */
181 AddTextLine ("\t.autoimport\ton");
183 /* Switch the assembler into case sensitive mode */
184 AddTextLine ("\t.case\t\ton");
186 /* Tell the assembler if we want to generate debug info */
187 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
189 /* Import the stack pointer for direct auto variable access */
190 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
192 /* Define long branch macros */
193 AddTextLine ("\t.macpack\tlongbranch");
198 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
199 /* If debug info is enabled, place a file info into the source */
202 /* We have to place this into the global text segment, so it will
203 * appear before all .dbg line statements.
205 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
211 /*****************************************************************************/
212 /* Segment support */
213 /*****************************************************************************/
217 void g_userodata (void)
218 /* Switch to the read only data segment */
220 UseDataSeg (SEG_RODATA);
225 void g_usedata (void)
226 /* Switch to the data segment */
228 UseDataSeg (SEG_DATA);
234 /* Switch to the bss segment */
236 UseDataSeg (SEG_BSS);
241 void g_segname (segment_t Seg)
242 /* Emit the name of a segment if necessary */
244 /* Emit a segment directive for the data style segments */
247 case SEG_RODATA: S = CS->ROData; break;
248 case SEG_DATA: S = CS->Data; break;
249 case SEG_BSS: S = CS->BSS; break;
250 default: S = 0; break;
253 DS_AddLine (S, ".segment\t\"%s\"", GetSegName (Seg));
259 /*****************************************************************************/
261 /*****************************************************************************/
265 unsigned sizeofarg (unsigned flags)
266 /* Return the size of a function argument type that is encoded in flags */
268 switch (flags & CF_TYPE) {
271 return (flags & CF_FORCECHAR)? 1 : 2;
291 int pop (unsigned flags)
292 /* Pop an argument of the given size */
294 return oursp += sizeofarg (flags);
299 int push (unsigned flags)
300 /* Push an argument of the given size */
302 return oursp -= sizeofarg (flags);
307 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
308 /* The value in Offs is an offset to an address in a/x. Make sure, an object
309 * of the type given in Flags can be loaded or stored into this address by
310 * adding part of the offset to the address in ax, so that the remaining
311 * offset fits into an index register. Return the remaining offset.
314 /* If the offset is too large for a byte register, add the high byte
315 * of the offset to the primary. Beware: We need a special correction
316 * if the offset in the low byte will overflow in the operation.
318 unsigned O = Offs & ~0xFFU;
319 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
320 /* We need to add the low byte also */
324 /* Do the correction if we need one */
326 g_inc (CF_INT | CF_CONST, O);
330 /* Return the new offset */
336 /*****************************************************************************/
337 /* Functions handling local labels */
338 /*****************************************************************************/
342 void g_defcodelabel (unsigned label)
343 /* Define a local code label */
345 CS_AddLabel (CS->Code, LocalLabelName (label));
350 void g_defdatalabel (unsigned label)
351 /* Define a local data label */
353 AddDataLine ("%s:", LocalLabelName (label));
358 /*****************************************************************************/
359 /* Functions handling global labels */
360 /*****************************************************************************/
364 void g_defgloblabel (const char* Name)
365 /* Define a global label with the given name */
367 /* Global labels are always data labels */
368 AddDataLine ("_%s:", Name);
373 void g_defexport (const char* Name, int ZP)
374 /* Export the given label */
377 AddTextLine ("\t.exportzp\t_%s", Name);
379 AddTextLine ("\t.export\t\t_%s", Name);
385 void g_defimport (const char* Name, int ZP)
386 /* Import the given label */
389 AddTextLine ("\t.importzp\t_%s", Name);
391 AddTextLine ("\t.import\t\t_%s", Name);
397 void g_importmainargs (void)
398 /* Forced import of a special symbol that handles arguments to main */
400 AddTextLine ("\t.forceimport\tinitmainargs");
405 /*****************************************************************************/
406 /* Load functions for various registers */
407 /*****************************************************************************/
411 static void ldaconst (unsigned val)
412 /* Load a with a constant */
414 AddCodeLine ("lda #$%02X", val & 0xFF);
419 static void ldxconst (unsigned val)
420 /* Load x with a constant */
422 AddCodeLine ("ldx #$%02X", val & 0xFF);
427 static void ldyconst (unsigned val)
428 /* Load y with a constant */
430 AddCodeLine ("ldy #$%02X", val & 0xFF);
435 /*****************************************************************************/
436 /* Function entry and exit */
437 /*****************************************************************************/
441 /* Remember the argument size of a function. The variable is set by g_enter
442 * and used by g_leave. If the functions gets its argument size by the caller
443 * (variable param list or function without prototype), g_enter will set the
449 void g_enter (unsigned flags, unsigned argsize)
450 /* Function prologue */
452 if ((flags & CF_FIXARGC) != 0) {
453 /* Just remember the argument size for the leave */
457 AddCodeLine ("jsr enter");
464 /* Function epilogue */
466 /* How many bytes of locals do we have to drop? */
469 /* If we didn't have a variable argument list, don't call leave */
472 /* Drop stackframe if needed */
476 AddCodeLine ("jsr incsp%d", k);
480 AddCodeLine ("jsr addysp");
487 /* Nothing to drop */
488 AddCodeLine ("jsr leave");
490 /* We've a stack frame to drop */
492 AddCodeLine ("jsr leavey");
496 /* Add the final rts */
502 /*****************************************************************************/
503 /* Register variables */
504 /*****************************************************************************/
508 void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
509 /* Swap a register variable with a location on the stack */
511 /* Calculate the actual stack offset and check it */
513 CheckLocalOffs (StackOffs);
518 if (CodeSizeFactor < 165) {
519 ldyconst (StackOffs);
521 AddCodeLine ("jsr regswap1");
523 ldyconst (StackOffs);
524 AddCodeLine ("lda (sp),y");
525 AddCodeLine ("ldx regbank%+d", RegOffs);
526 AddCodeLine ("sta regbank%+d", RegOffs);
528 AddCodeLine ("sta (sp),y");
531 } else if (Bytes == 2) {
533 ldyconst (StackOffs);
535 AddCodeLine ("jsr regswap2");
539 ldyconst (StackOffs);
542 AddCodeLine ("jsr regswap");
548 void g_save_regvars (int RegOffs, unsigned Bytes)
549 /* Save register variables */
551 /* Don't loop for up to two bytes */
554 AddCodeLine ("lda regbank%+d", RegOffs);
555 AddCodeLine ("jsr pusha");
557 } else if (Bytes == 2) {
559 AddCodeLine ("lda regbank%+d", RegOffs);
560 AddCodeLine ("ldx regbank%+d", RegOffs+1);
561 AddCodeLine ("jsr pushax");
565 /* More than two bytes - loop */
566 unsigned Label = GetLocalLabel ();
568 ldyconst (Bytes - 1);
570 g_defcodelabel (Label);
571 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
572 AddCodeLine ("sta (sp),y");
575 AddCodeLine ("bne %s", LocalLabelName (Label));
579 /* We pushed stuff, correct the stack pointer */
585 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
586 /* Restore register variables */
588 /* Calculate the actual stack offset and check it */
590 CheckLocalOffs (StackOffs);
592 /* Don't loop for up to two bytes */
595 ldyconst (StackOffs);
596 AddCodeLine ("lda (sp),y");
597 AddCodeLine ("sta regbank%+d", RegOffs);
599 } else if (Bytes == 2) {
601 ldyconst (StackOffs);
602 AddCodeLine ("lda (sp),y");
603 AddCodeLine ("sta regbank%+d", RegOffs);
605 AddCodeLine ("lda (sp),y");
606 AddCodeLine ("sta regbank%+d", RegOffs+1);
608 } else if (Bytes == 3 && CodeSizeFactor >= 133) {
610 ldyconst (StackOffs);
611 AddCodeLine ("lda (sp),y");
612 AddCodeLine ("sta regbank%+d", RegOffs);
614 AddCodeLine ("lda (sp),y");
615 AddCodeLine ("sta regbank%+d", RegOffs+1);
617 AddCodeLine ("lda (sp),y");
618 AddCodeLine ("sta regbank%+d", RegOffs+2);
620 } else if (StackOffs <= RegOffs) {
622 /* More bytes, but the relation between the register offset in the
623 * register bank and the stack offset allows us to generate short
624 * code that uses just one index register.
626 unsigned Label = GetLocalLabel ();
627 ldyconst (StackOffs);
628 g_defcodelabel (Label);
629 AddCodeLine ("lda (sp),y");
630 AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs);
632 AddCodeLine ("cpy #$%02X", StackOffs + Bytes);
633 AddCodeLine ("bne %s", LocalLabelName (Label));
637 /* Ok, this is the generic code. We need to save X because the
638 * caller will only save A.
640 unsigned Label = GetLocalLabel ();
641 AddCodeLine ("stx tmp1");
642 ldyconst (StackOffs + Bytes - 1);
643 ldxconst (Bytes - 1);
644 g_defcodelabel (Label);
645 AddCodeLine ("lda (sp),y");
646 AddCodeLine ("sta regbank%+d,x", RegOffs);
649 AddCodeLine ("bpl %s", LocalLabelName (Label));
650 AddCodeLine ("ldx tmp1");
657 /*****************************************************************************/
658 /* Fetching memory cells */
659 /*****************************************************************************/
663 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
664 /* Load a constant into the primary register */
666 unsigned char B1, B2, B3, B4;
670 if ((Flags & CF_CONST) != 0) {
672 /* Numeric constant */
673 switch (Flags & CF_TYPE) {
676 if ((Flags & CF_FORCECHAR) != 0) {
682 ldxconst ((Val >> 8) & 0xFF);
683 ldaconst (Val & 0xFF);
687 /* Split the value into 4 bytes */
688 B1 = (unsigned char) (Val >> 0);
689 B2 = (unsigned char) (Val >> 8);
690 B3 = (unsigned char) (Val >> 16);
691 B4 = (unsigned char) (Val >> 24);
693 /* Remember which bytes are done */
697 AddCodeLine ("ldx #$%02X", B2);
700 AddCodeLine ("stx sreg");
704 AddCodeLine ("stx sreg+1");
707 if ((Done & 0x04) == 0 && B1 != B3) {
708 AddCodeLine ("lda #$%02X", B3);
709 AddCodeLine ("sta sreg");
712 if ((Done & 0x08) == 0 && B1 != B4) {
713 AddCodeLine ("lda #$%02X", B4);
714 AddCodeLine ("sta sreg+1");
717 AddCodeLine ("lda #$%02X", B1);
719 if ((Done & 0x04) == 0) {
721 AddCodeLine ("sta sreg");
723 if ((Done & 0x08) == 0) {
725 AddCodeLine ("sta sreg+1");
737 /* Some sort of label */
738 const char* Label = GetLabelName (Flags, Val, Offs);
740 /* Load the address into the primary */
741 AddCodeLine ("lda #<(%s)", Label);
742 AddCodeLine ("ldx #>(%s)", Label);
749 void g_getstatic (unsigned flags, unsigned long label, long offs)
750 /* Fetch an static memory cell into the primary register */
752 /* Create the correct label name */
753 const char* lbuf = GetLabelName (flags, label, offs);
755 /* Check the size and generate the correct load operation */
756 switch (flags & CF_TYPE) {
759 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
760 AddCodeLine ("lda %s", lbuf); /* load A from the label */
763 AddCodeLine ("lda %s", lbuf); /* load A from the label */
764 if (!(flags & CF_UNSIGNED)) {
765 /* Must sign extend */
766 unsigned L = GetLocalLabel ();
767 AddCodeLine ("bpl %s", LocalLabelName (L));
775 AddCodeLine ("lda %s", lbuf);
776 if (flags & CF_TEST) {
777 AddCodeLine ("ora %s+1", lbuf);
779 AddCodeLine ("ldx %s+1", lbuf);
784 if (flags & CF_TEST) {
785 AddCodeLine ("lda %s+3", lbuf);
786 AddCodeLine ("ora %s+2", lbuf);
787 AddCodeLine ("ora %s+1", lbuf);
788 AddCodeLine ("ora %s+0", lbuf);
790 AddCodeLine ("lda %s+3", lbuf);
791 AddCodeLine ("sta sreg+1");
792 AddCodeLine ("lda %s+2", lbuf);
793 AddCodeLine ("sta sreg");
794 AddCodeLine ("ldx %s+1", lbuf);
795 AddCodeLine ("lda %s", lbuf);
807 void g_getlocal (unsigned flags, int offs)
808 /* Fetch specified local object (local var). */
811 CheckLocalOffs (offs);
812 switch (flags & CF_TYPE) {
815 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
817 AddCodeLine ("lda (sp),y");
820 AddCodeLine ("ldx #$00");
821 AddCodeLine ("lda (sp),y");
822 if ((flags & CF_UNSIGNED) == 0) {
823 unsigned L = GetLocalLabel();
824 AddCodeLine ("bpl %s", LocalLabelName (L));
832 CheckLocalOffs (offs + 1);
833 if (flags & CF_TEST) {
835 AddCodeLine ("lda (sp),y");
837 AddCodeLine ("ora (sp),y");
840 AddCodeLine ("jsr ldaxysp");
846 AddCodeLine ("jsr ldeaxysp");
856 void g_getind (unsigned flags, unsigned offs)
857 /* Fetch the specified object type indirect through the primary register
858 * into the primary register
861 /* If the offset is greater than 255, add the part that is > 255 to
862 * the primary. This way we get an easy addition and use the low byte
865 offs = MakeByteOffs (flags, offs);
867 /* Handle the indirect fetch */
868 switch (flags & CF_TYPE) {
871 /* Character sized */
872 if (flags & CF_UNSIGNED) {
874 AddCodeLine ("jsr ldauidx");
877 AddCodeLine ("jsr ldaidx");
882 if (flags & CF_TEST) {
884 AddCodeLine ("sta ptr1");
885 AddCodeLine ("stx ptr1+1");
886 AddCodeLine ("lda (ptr1),y");
888 AddCodeLine ("ora (ptr1),y");
891 AddCodeLine ("jsr ldaxidx");
897 AddCodeLine ("jsr ldeaxidx");
898 if (flags & CF_TEST) {
899 AddCodeLine ("jsr tsteax");
911 void g_leasp (int offs)
912 /* Fetch the address of the specified symbol into the primary register */
914 /* Calculate the offset relative to sp */
917 /* For value 0 we do direct code */
919 AddCodeLine ("lda sp");
920 AddCodeLine ("ldx sp+1");
922 if (CodeSizeFactor < 300) {
923 ldaconst (offs); /* Load A with offset value */
924 AddCodeLine ("jsr leaasp"); /* Load effective address */
926 unsigned L = GetLocalLabel ();
927 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && offs == 1) {
928 AddCodeLine ("lda sp");
929 AddCodeLine ("ldx sp+1");
931 AddCodeLine ("bne %s", LocalLabelName (L));
936 AddCodeLine ("ldx sp+1");
937 AddCodeLine ("adc sp");
938 AddCodeLine ("bcc %s", LocalLabelName (L));
948 void g_leavariadic (int Offs)
949 /* Fetch the address of a parameter in a variadic function into the primary
953 unsigned ArgSizeOffs;
955 /* Calculate the offset relative to sp */
958 /* Get the offset of the parameter which is stored at sp+0 on function
959 * entry and check if this offset is reachable with a byte offset.
962 ArgSizeOffs = -oursp;
963 CheckLocalOffs (ArgSizeOffs);
965 /* Get the size of all parameters. */
966 ldyconst (ArgSizeOffs);
967 AddCodeLine ("lda (sp),y");
969 /* Add the value of the stackpointer */
970 if (CodeSizeFactor > 250) {
971 unsigned L = GetLocalLabel();
972 AddCodeLine ("ldx sp+1");
974 AddCodeLine ("adc sp");
975 AddCodeLine ("bcc %s", LocalLabelName (L));
979 AddCodeLine ("jsr leaasp");
982 /* Add the offset to the primary */
984 g_inc (CF_INT | CF_CONST, Offs);
985 } else if (Offs < 0) {
986 g_dec (CF_INT | CF_CONST, -Offs);
992 /*****************************************************************************/
993 /* Store into memory */
994 /*****************************************************************************/
998 void g_putstatic (unsigned flags, unsigned long label, long offs)
999 /* Store the primary register into the specified static memory cell */
1001 /* Create the correct label name */
1002 const char* lbuf = GetLabelName (flags, label, offs);
1004 /* Check the size and generate the correct store operation */
1005 switch (flags & CF_TYPE) {
1008 AddCodeLine ("sta %s", lbuf);
1012 AddCodeLine ("sta %s", lbuf);
1013 AddCodeLine ("stx %s+1", lbuf);
1017 AddCodeLine ("sta %s", lbuf);
1018 AddCodeLine ("stx %s+1", lbuf);
1019 AddCodeLine ("ldy sreg");
1020 AddCodeLine ("sty %s+2", lbuf);
1021 AddCodeLine ("ldy sreg+1");
1022 AddCodeLine ("sty %s+3", lbuf);
1033 void g_putlocal (unsigned Flags, int Offs, long Val)
1034 /* Put data into local object. */
1037 CheckLocalOffs (Offs);
1038 switch (Flags & CF_TYPE) {
1041 if (Flags & CF_CONST) {
1042 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1045 AddCodeLine ("sta (sp),y");
1049 if (Flags & CF_CONST) {
1051 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1052 AddCodeLine ("sta (sp),y");
1053 if ((Flags & CF_NOKEEP) == 0) {
1054 /* Place high byte into X */
1055 AddCodeLine ("tax");
1057 if ((Val & 0xFF) == Offs+1) {
1058 /* The value we need is already in Y */
1059 AddCodeLine ("tya");
1060 AddCodeLine ("dey");
1062 AddCodeLine ("dey");
1063 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1065 AddCodeLine ("sta (sp),y");
1067 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
1069 AddCodeLine ("jsr staxysp");
1072 AddCodeLine ("sta (sp),y");
1073 AddCodeLine ("iny");
1074 AddCodeLine ("txa");
1075 AddCodeLine ("sta (sp),y");
1081 if (Flags & CF_CONST) {
1082 g_getimmed (Flags, Val, 0);
1085 AddCodeLine ("jsr steaxysp");
1096 void g_putind (unsigned Flags, unsigned Offs)
1097 /* Store the specified object type in the primary register at the address
1098 * on the top of the stack
1101 /* We can handle offsets below $100 directly, larger offsets must be added
1102 * to the address. Since a/x is in use, best code is achieved by adding
1103 * just the high byte. Be sure to check if the low byte will overflow while
1106 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1108 /* Overflow - we need to add the low byte also */
1109 AddCodeLine ("ldy #$00");
1110 AddCodeLine ("clc");
1111 AddCodeLine ("pha");
1112 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1113 AddCodeLine ("adc (sp),y");
1114 AddCodeLine ("sta (sp),y");
1115 AddCodeLine ("iny");
1116 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1117 AddCodeLine ("adc (sp),y");
1118 AddCodeLine ("sta (sp),y");
1119 AddCodeLine ("pla");
1121 /* Complete address is on stack, new offset is zero */
1124 } else if ((Offs & 0xFF00) != 0) {
1126 /* We can just add the high byte */
1127 AddCodeLine ("ldy #$01");
1128 AddCodeLine ("clc");
1129 AddCodeLine ("pha");
1130 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1131 AddCodeLine ("adc (sp),y");
1132 AddCodeLine ("sta (sp),y");
1133 AddCodeLine ("pla");
1135 /* Offset is now just the low byte */
1139 /* Check the size and determine operation */
1140 switch (Flags & CF_TYPE) {
1144 AddCodeLine ("jsr staspidx");
1149 AddCodeLine ("jsr staxspidx");
1154 AddCodeLine ("jsr steaxspidx");
1162 /* Pop the argument which is always a pointer */
1168 /*****************************************************************************/
1169 /* type conversion and similiar stuff */
1170 /*****************************************************************************/
1174 void g_toslong (unsigned flags)
1175 /* Make sure, the value on TOS is a long. Convert if necessary */
1177 switch (flags & CF_TYPE) {
1181 if (flags & CF_UNSIGNED) {
1182 AddCodeLine ("jsr tosulong");
1184 AddCodeLine ("jsr toslong");
1199 void g_tosint (unsigned flags)
1200 /* Make sure, the value on TOS is an int. Convert if necessary */
1202 switch (flags & CF_TYPE) {
1209 AddCodeLine ("jsr tosint");
1220 void g_regint (unsigned Flags)
1221 /* Make sure, the value in the primary register an int. Convert if necessary */
1225 switch (Flags & CF_TYPE) {
1228 if (Flags & CF_FORCECHAR) {
1229 /* Conversion is from char */
1230 if (Flags & CF_UNSIGNED) {
1231 AddCodeLine ("ldx #$00");
1233 L = GetLocalLabel();
1234 AddCodeLine ("ldx #$00");
1235 AddCodeLine ("cmp #$80");
1236 AddCodeLine ("bcc %s", LocalLabelName (L));
1237 AddCodeLine ("dex");
1254 void g_reglong (unsigned Flags)
1255 /* Make sure, the value in the primary register a long. Convert if necessary */
1259 switch (Flags & CF_TYPE) {
1262 if (Flags & CF_FORCECHAR) {
1263 /* Conversion is from char */
1264 if (Flags & CF_UNSIGNED) {
1265 if (CodeSizeFactor >= 200) {
1266 AddCodeLine ("ldx #$00");
1267 AddCodeLine ("stx sreg");
1268 AddCodeLine ("stx sreg+1");
1270 AddCodeLine ("jsr aulong");
1273 if (CodeSizeFactor >= 366) {
1274 L = GetLocalLabel();
1275 AddCodeLine ("ldx #$00");
1276 AddCodeLine ("cmp #$80");
1277 AddCodeLine ("bcc %s", LocalLabelName (L));
1278 AddCodeLine ("dex");
1280 AddCodeLine ("stx sreg");
1281 AddCodeLine ("stx sreg+1");
1283 AddCodeLine ("jsr along");
1290 if (Flags & CF_UNSIGNED) {
1291 if (CodeSizeFactor >= 200) {
1293 AddCodeLine ("sty sreg");
1294 AddCodeLine ("sty sreg+1");
1296 AddCodeLine ("jsr axulong");
1299 AddCodeLine ("jsr axlong");
1313 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1314 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1315 * value, that corresponds to the value on TOS, rhs corresponds to the value
1316 * in (e)ax. The return value is the the flags value for the resulting type.
1319 unsigned ltype, rtype;
1322 /* Get the type spec from the flags */
1323 ltype = lhs & CF_TYPE;
1324 rtype = rhs & CF_TYPE;
1326 /* Check if a conversion is needed */
1327 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1328 /* We must promote the primary register to long */
1330 /* Get the new rhs type */
1331 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1333 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1334 /* We must promote the lhs to long */
1340 /* Get the new rhs type */
1341 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1345 /* Determine the result type for the operation:
1346 * - The result is const if both operands are const.
1347 * - The result is unsigned if one of the operands is unsigned.
1348 * - The result is long if one of the operands is long.
1349 * - Otherwise the result is int sized.
1351 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1352 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1353 if (rtype == CF_LONG || ltype == CF_LONG) {
1363 unsigned g_typecast (unsigned lhs, unsigned rhs)
1364 /* Cast the value in the primary register to the operand size that is flagged
1365 * by the lhs value. Return the result value.
1368 unsigned ltype, rtype;
1370 /* Get the type spec from the flags */
1371 ltype = lhs & CF_TYPE;
1372 rtype = rhs & CF_TYPE;
1374 /* Check if a conversion is needed */
1375 if ((rhs & CF_CONST) == 0) {
1376 if (ltype == CF_LONG && rtype != CF_LONG) {
1377 /* We must promote the primary register to long */
1379 } else if (ltype == CF_INT && rtype != CF_INT) {
1380 /* We must promote the primary register to int */
1385 /* Do not need any other action. If the left type is int, and the primary
1386 * register is long, it will be automagically truncated. If the right hand
1387 * side is const, it is not located in the primary register and handled by
1388 * the expression parser code.
1391 /* Result is const if the right hand side was const */
1392 lhs |= (rhs & CF_CONST);
1394 /* The resulting type is that of the left hand side (that's why you called
1402 void g_scale (unsigned flags, long val)
1403 /* Scale the value in the primary register by the given value. If val is positive,
1404 * scale up, is val is negative, scale down. This function is used to scale
1405 * the operands or results of pointer arithmetic by the size of the type, the
1406 * pointer points to.
1411 /* Value may not be zero */
1413 Internal ("Data type has no size");
1414 } else if (val > 0) {
1417 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1419 /* Factor is 2, 4, 8 and 16, use special function */
1420 switch (flags & CF_TYPE) {
1423 if (flags & CF_FORCECHAR) {
1425 AddCodeLine ("asl a");
1432 if (CodeSizeFactor >= (p2+1)*130U) {
1433 AddCodeLine ("stx tmp1");
1435 AddCodeLine ("asl a");
1436 AddCodeLine ("rol tmp1");
1438 AddCodeLine ("ldx tmp1");
1440 if (flags & CF_UNSIGNED) {
1441 AddCodeLine ("jsr shlax%d", p2);
1443 AddCodeLine ("jsr aslax%d", p2);
1449 if (flags & CF_UNSIGNED) {
1450 AddCodeLine ("jsr shleax%d", p2);
1452 AddCodeLine ("jsr asleax%d", p2);
1461 } else if (val != 1) {
1463 /* Use a multiplication instead */
1464 g_mul (flags | CF_CONST, val);
1472 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1474 /* Factor is 2, 4, 8 and 16 use special function */
1475 switch (flags & CF_TYPE) {
1478 if (flags & CF_FORCECHAR) {
1479 if (flags & CF_UNSIGNED) {
1481 AddCodeLine ("lsr a");
1484 } else if (p2 <= 2) {
1485 AddCodeLine ("cmp #$80");
1486 AddCodeLine ("ror a");
1493 if (flags & CF_UNSIGNED) {
1494 if (CodeSizeFactor >= (p2+1)*130U) {
1495 AddCodeLine ("stx tmp1");
1497 AddCodeLine ("lsr tmp1");
1498 AddCodeLine ("ror a");
1500 AddCodeLine ("ldx tmp1");
1502 AddCodeLine ("jsr lsrax%d", p2);
1505 if (CodeSizeFactor >= (p2+1)*150U) {
1506 AddCodeLine ("stx tmp1");
1508 AddCodeLine ("cpx #$80");
1509 AddCodeLine ("ror tmp1");
1510 AddCodeLine ("ror a");
1512 AddCodeLine ("ldx tmp1");
1514 AddCodeLine ("jsr asrax%d", p2);
1520 if (flags & CF_UNSIGNED) {
1521 AddCodeLine ("jsr lsreax%d", p2);
1523 AddCodeLine ("jsr asreax%d", p2);
1532 } else if (val != 1) {
1534 /* Use a division instead */
1535 g_div (flags | CF_CONST, val);
1543 /*****************************************************************************/
1544 /* Adds and subs of variables fix a fixed address */
1545 /*****************************************************************************/
1549 void g_addlocal (unsigned flags, int offs)
1550 /* Add a local variable to ax */
1554 /* Correct the offset and check it */
1556 CheckLocalOffs (offs);
1558 switch (flags & CF_TYPE) {
1561 L = GetLocalLabel();
1562 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1563 AddCodeLine ("clc");
1564 AddCodeLine ("adc (sp),y");
1565 AddCodeLine ("bcc %s", LocalLabelName (L));
1566 AddCodeLine ("inx");
1571 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1572 AddCodeLine ("clc");
1573 AddCodeLine ("adc (sp),y");
1574 AddCodeLine ("pha");
1575 AddCodeLine ("txa");
1576 AddCodeLine ("iny");
1577 AddCodeLine ("adc (sp),y");
1578 AddCodeLine ("tax");
1579 AddCodeLine ("pla");
1583 /* Do it the old way */
1585 g_getlocal (flags, offs);
1597 void g_addstatic (unsigned flags, unsigned long label, long offs)
1598 /* Add a static variable to ax */
1602 /* Create the correct label name */
1603 const char* lbuf = GetLabelName (flags, label, offs);
1605 switch (flags & CF_TYPE) {
1608 L = GetLocalLabel();
1609 AddCodeLine ("clc");
1610 AddCodeLine ("adc %s", lbuf);
1611 AddCodeLine ("bcc %s", LocalLabelName (L));
1612 AddCodeLine ("inx");
1617 AddCodeLine ("clc");
1618 AddCodeLine ("adc %s", lbuf);
1619 AddCodeLine ("tay");
1620 AddCodeLine ("txa");
1621 AddCodeLine ("adc %s+1", lbuf);
1622 AddCodeLine ("tax");
1623 AddCodeLine ("tya");
1627 /* Do it the old way */
1629 g_getstatic (flags, label, offs);
1641 /*****************************************************************************/
1642 /* Special op= functions */
1643 /*****************************************************************************/
1647 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1649 /* Emit += for a static variable */
1651 /* Create the correct label name */
1652 const char* lbuf = GetLabelName (flags, label, offs);
1654 /* Check the size and determine operation */
1655 switch (flags & CF_TYPE) {
1658 if (flags & CF_FORCECHAR) {
1659 AddCodeLine ("ldx #$00");
1660 if (flags & CF_CONST) {
1662 AddCodeLine ("inc %s", lbuf);
1663 AddCodeLine ("lda %s", lbuf);
1665 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1666 AddCodeLine ("clc");
1667 AddCodeLine ("adc %s", lbuf);
1668 AddCodeLine ("sta %s", lbuf);
1671 AddCodeLine ("clc");
1672 AddCodeLine ("adc %s", lbuf);
1673 AddCodeLine ("sta %s", lbuf);
1675 if ((flags & CF_UNSIGNED) == 0) {
1676 unsigned L = GetLocalLabel();
1677 AddCodeLine ("bpl %s", LocalLabelName (L));
1678 AddCodeLine ("dex");
1686 if (flags & CF_CONST) {
1688 unsigned L = GetLocalLabel ();
1689 AddCodeLine ("inc %s", lbuf);
1690 AddCodeLine ("bne %s", LocalLabelName (L));
1691 AddCodeLine ("inc %s+1", lbuf);
1693 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1694 AddCodeLine ("ldx %s+1", lbuf);
1696 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1697 AddCodeLine ("clc");
1698 AddCodeLine ("adc %s", lbuf);
1699 AddCodeLine ("sta %s", lbuf);
1701 unsigned L = GetLocalLabel ();
1702 AddCodeLine ("bcc %s", LocalLabelName (L));
1703 AddCodeLine ("inc %s+1", lbuf);
1705 AddCodeLine ("ldx %s+1", lbuf);
1707 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1708 AddCodeLine ("adc %s+1", lbuf);
1709 AddCodeLine ("sta %s+1", lbuf);
1710 AddCodeLine ("tax");
1711 AddCodeLine ("lda %s", lbuf);
1715 AddCodeLine ("clc");
1716 AddCodeLine ("adc %s", lbuf);
1717 AddCodeLine ("sta %s", lbuf);
1718 AddCodeLine ("txa");
1719 AddCodeLine ("adc %s+1", lbuf);
1720 AddCodeLine ("sta %s+1", lbuf);
1721 AddCodeLine ("tax");
1722 AddCodeLine ("lda %s", lbuf);
1727 if (flags & CF_CONST) {
1729 AddCodeLine ("ldy #<(%s)", lbuf);
1730 AddCodeLine ("sty ptr1");
1731 AddCodeLine ("ldy #>(%s)", lbuf);
1733 AddCodeLine ("jsr laddeq1");
1735 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1736 AddCodeLine ("jsr laddeqa");
1739 g_getstatic (flags, label, offs);
1741 g_putstatic (flags, label, offs);
1744 AddCodeLine ("ldy #<(%s)", lbuf);
1745 AddCodeLine ("sty ptr1");
1746 AddCodeLine ("ldy #>(%s)", lbuf);
1747 AddCodeLine ("jsr laddeq");
1758 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1759 /* Emit += for a local variable */
1761 /* Calculate the true offset, check it, load it into Y */
1763 CheckLocalOffs (offs);
1765 /* Check the size and determine operation */
1766 switch (flags & CF_TYPE) {
1769 if (flags & CF_FORCECHAR) {
1771 AddCodeLine ("ldx #$00");
1772 if (flags & CF_CONST) {
1773 AddCodeLine ("clc");
1774 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1775 AddCodeLine ("adc (sp),y");
1776 AddCodeLine ("sta (sp),y");
1778 AddCodeLine ("clc");
1779 AddCodeLine ("adc (sp),y");
1780 AddCodeLine ("sta (sp),y");
1782 if ((flags & CF_UNSIGNED) == 0) {
1783 unsigned L = GetLocalLabel();
1784 AddCodeLine ("bpl %s", LocalLabelName (L));
1785 AddCodeLine ("dex");
1794 if (flags & CF_CONST) {
1795 if (CodeSizeFactor >= 400) {
1796 AddCodeLine ("clc");
1797 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1798 AddCodeLine ("adc (sp),y");
1799 AddCodeLine ("sta (sp),y");
1800 AddCodeLine ("iny");
1801 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1802 AddCodeLine ("adc (sp),y");
1803 AddCodeLine ("sta (sp),y");
1804 AddCodeLine ("tax");
1805 AddCodeLine ("dey");
1806 AddCodeLine ("lda (sp),y");
1808 g_getimmed (flags, val, 0);
1809 AddCodeLine ("jsr addeqysp");
1812 AddCodeLine ("jsr addeqysp");
1817 if (flags & CF_CONST) {
1818 g_getimmed (flags, val, 0);
1821 AddCodeLine ("jsr laddeqysp");
1831 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1832 /* Emit += for the location with address in ax */
1834 /* If the offset is too large for a byte register, add the high byte
1835 * of the offset to the primary. Beware: We need a special correction
1836 * if the offset in the low byte will overflow in the operation.
1838 offs = MakeByteOffs (flags, offs);
1840 /* Check the size and determine operation */
1841 switch (flags & CF_TYPE) {
1844 AddCodeLine ("sta ptr1");
1845 AddCodeLine ("stx ptr1+1");
1846 AddCodeLine ("ldy #$%02X", offs);
1847 AddCodeLine ("ldx #$00");
1848 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1849 AddCodeLine ("clc");
1850 AddCodeLine ("adc (ptr1),y");
1851 AddCodeLine ("sta (ptr1),y");
1855 if (CodeSizeFactor >= 200) {
1856 /* Lots of code, use only if size is not important */
1857 AddCodeLine ("sta ptr1");
1858 AddCodeLine ("stx ptr1+1");
1859 AddCodeLine ("ldy #$%02X", offs);
1860 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1861 AddCodeLine ("clc");
1862 AddCodeLine ("adc (ptr1),y");
1863 AddCodeLine ("sta (ptr1),y");
1864 AddCodeLine ("pha");
1865 AddCodeLine ("iny");
1866 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1867 AddCodeLine ("adc (ptr1),y");
1868 AddCodeLine ("sta (ptr1),y");
1869 AddCodeLine ("tax");
1870 AddCodeLine ("pla");
1876 AddCodeLine ("jsr pushax"); /* Push the address */
1877 push (CF_PTR); /* Correct the internal sp */
1878 g_getind (flags, offs); /* Fetch the value */
1879 g_inc (flags, val); /* Increment value in primary */
1880 g_putind (flags, offs); /* Store the value back */
1890 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1892 /* Emit -= for a static variable */
1894 /* Create the correct label name */
1895 const char* lbuf = GetLabelName (flags, label, offs);
1897 /* Check the size and determine operation */
1898 switch (flags & CF_TYPE) {
1901 if (flags & CF_FORCECHAR) {
1902 AddCodeLine ("ldx #$00");
1903 if (flags & CF_CONST) {
1905 AddCodeLine ("dec %s", lbuf);
1906 AddCodeLine ("lda %s", lbuf);
1908 AddCodeLine ("sec");
1909 AddCodeLine ("lda %s", lbuf);
1910 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1911 AddCodeLine ("sta %s", lbuf);
1914 AddCodeLine ("eor #$FF");
1915 AddCodeLine ("sec");
1916 AddCodeLine ("adc %s", lbuf);
1917 AddCodeLine ("sta %s", lbuf);
1919 if ((flags & CF_UNSIGNED) == 0) {
1920 unsigned L = GetLocalLabel();
1921 AddCodeLine ("bpl %s", LocalLabelName (L));
1922 AddCodeLine ("dex");
1930 AddCodeLine ("sec");
1931 if (flags & CF_CONST) {
1932 AddCodeLine ("lda %s", lbuf);
1933 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1934 AddCodeLine ("sta %s", lbuf);
1936 unsigned L = GetLocalLabel ();
1937 AddCodeLine ("bcs %s", LocalLabelName (L));
1938 AddCodeLine ("dec %s+1", lbuf);
1940 AddCodeLine ("ldx %s+1", lbuf);
1942 AddCodeLine ("lda %s+1", lbuf);
1943 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1944 AddCodeLine ("sta %s+1", lbuf);
1945 AddCodeLine ("tax");
1946 AddCodeLine ("lda %s", lbuf);
1949 AddCodeLine ("eor #$FF");
1950 AddCodeLine ("adc %s", lbuf);
1951 AddCodeLine ("sta %s", lbuf);
1952 AddCodeLine ("txa");
1953 AddCodeLine ("eor #$FF");
1954 AddCodeLine ("adc %s+1", lbuf);
1955 AddCodeLine ("sta %s+1", lbuf);
1956 AddCodeLine ("tax");
1957 AddCodeLine ("lda %s", lbuf);
1962 if (flags & CF_CONST) {
1964 AddCodeLine ("ldy #<(%s)", lbuf);
1965 AddCodeLine ("sty ptr1");
1966 AddCodeLine ("ldy #>(%s)", lbuf);
1967 AddCodeLine ("lda #$%02X", (unsigned char)val);
1968 AddCodeLine ("jsr lsubeqa");
1970 g_getstatic (flags, label, offs);
1972 g_putstatic (flags, label, offs);
1975 AddCodeLine ("ldy #<(%s)", lbuf);
1976 AddCodeLine ("sty ptr1");
1977 AddCodeLine ("ldy #>(%s)", lbuf);
1978 AddCodeLine ("jsr lsubeq");
1989 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1990 /* Emit -= for a local variable */
1992 /* Calculate the true offset, check it, load it into Y */
1994 CheckLocalOffs (offs);
1996 /* Check the size and determine operation */
1997 switch (flags & CF_TYPE) {
2000 if (flags & CF_FORCECHAR) {
2002 AddCodeLine ("ldx #$00");
2003 AddCodeLine ("sec");
2004 if (flags & CF_CONST) {
2005 AddCodeLine ("lda (sp),y");
2006 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2008 AddCodeLine ("eor #$FF");
2009 AddCodeLine ("adc (sp),y");
2011 AddCodeLine ("sta (sp),y");
2012 if ((flags & CF_UNSIGNED) == 0) {
2013 unsigned L = GetLocalLabel();
2014 AddCodeLine ("bpl %s", LocalLabelName (L));
2015 AddCodeLine ("dex");
2023 if (flags & CF_CONST) {
2024 g_getimmed (flags, val, 0);
2027 AddCodeLine ("jsr subeqysp");
2031 if (flags & CF_CONST) {
2032 g_getimmed (flags, val, 0);
2035 AddCodeLine ("jsr lsubeqysp");
2045 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2046 /* Emit -= for the location with address in ax */
2048 /* If the offset is too large for a byte register, add the high byte
2049 * of the offset to the primary. Beware: We need a special correction
2050 * if the offset in the low byte will overflow in the operation.
2052 offs = MakeByteOffs (flags, offs);
2054 /* Check the size and determine operation */
2055 switch (flags & CF_TYPE) {
2058 AddCodeLine ("sta ptr1");
2059 AddCodeLine ("stx ptr1+1");
2060 AddCodeLine ("ldy #$%02X", offs);
2061 AddCodeLine ("ldx #$00");
2062 AddCodeLine ("lda (ptr1),y");
2063 AddCodeLine ("sec");
2064 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2065 AddCodeLine ("sta (ptr1),y");
2069 if (CodeSizeFactor >= 200) {
2070 /* Lots of code, use only if size is not important */
2071 AddCodeLine ("sta ptr1");
2072 AddCodeLine ("stx ptr1+1");
2073 AddCodeLine ("ldy #$%02X", offs);
2074 AddCodeLine ("lda (ptr1),y");
2075 AddCodeLine ("sec");
2076 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2077 AddCodeLine ("sta (ptr1),y");
2078 AddCodeLine ("pha");
2079 AddCodeLine ("iny");
2080 AddCodeLine ("lda (ptr1),y");
2081 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2082 AddCodeLine ("sta (ptr1),y");
2083 AddCodeLine ("tax");
2084 AddCodeLine ("pla");
2090 AddCodeLine ("jsr pushax"); /* Push the address */
2091 push (CF_PTR); /* Correct the internal sp */
2092 g_getind (flags, offs); /* Fetch the value */
2093 g_dec (flags, val); /* Increment value in primary */
2094 g_putind (flags, offs); /* Store the value back */
2104 /*****************************************************************************/
2105 /* Add a variable address to the value in ax */
2106 /*****************************************************************************/
2110 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2111 /* Add the address of a local variable to ax */
2115 /* Add the offset */
2118 /* We cannot address more then 256 bytes of locals anyway */
2119 L = GetLocalLabel();
2120 CheckLocalOffs (offs);
2121 AddCodeLine ("clc");
2122 AddCodeLine ("adc #$%02X", offs & 0xFF);
2123 /* Do also skip the CLC insn below */
2124 AddCodeLine ("bcc %s", LocalLabelName (L));
2125 AddCodeLine ("inx");
2128 /* Add the current stackpointer value */
2129 AddCodeLine ("clc");
2131 /* Label was used above */
2134 AddCodeLine ("adc sp");
2135 AddCodeLine ("tay");
2136 AddCodeLine ("txa");
2137 AddCodeLine ("adc sp+1");
2138 AddCodeLine ("tax");
2139 AddCodeLine ("tya");
2144 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2145 /* Add the address of a static variable to ax */
2147 /* Create the correct label name */
2148 const char* lbuf = GetLabelName (flags, label, offs);
2150 /* Add the address to the current ax value */
2151 AddCodeLine ("clc");
2152 AddCodeLine ("adc #<(%s)", lbuf);
2153 AddCodeLine ("tay");
2154 AddCodeLine ("txa");
2155 AddCodeLine ("adc #>(%s)", lbuf);
2156 AddCodeLine ("tax");
2157 AddCodeLine ("tya");
2162 /*****************************************************************************/
2164 /*****************************************************************************/
2168 void g_save (unsigned flags)
2169 /* Copy primary register to hold register. */
2171 /* Check the size and determine operation */
2172 switch (flags & CF_TYPE) {
2175 if (flags & CF_FORCECHAR) {
2176 AddCodeLine ("pha");
2182 AddCodeLine ("sta regsave");
2183 AddCodeLine ("stx regsave+1");
2187 AddCodeLine ("jsr saveeax");
2197 void g_restore (unsigned flags)
2198 /* Copy hold register to primary. */
2200 /* Check the size and determine operation */
2201 switch (flags & CF_TYPE) {
2204 if (flags & CF_FORCECHAR) {
2205 AddCodeLine ("pla");
2211 AddCodeLine ("lda regsave");
2212 AddCodeLine ("ldx regsave+1");
2216 AddCodeLine ("jsr resteax");
2226 void g_cmp (unsigned flags, unsigned long val)
2227 /* Immidiate compare. The primary register will not be changed, Z flag
2233 /* Check the size and determine operation */
2234 switch (flags & CF_TYPE) {
2237 if (flags & CF_FORCECHAR) {
2238 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2244 L = GetLocalLabel();
2245 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2246 AddCodeLine ("bne %s", LocalLabelName (L));
2247 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2252 Internal ("g_cmp: Long compares not implemented");
2262 static void oper (unsigned flags, unsigned long val, char** subs)
2263 /* Encode a binary operation. subs is a pointer to four groups of three
2265 * 0-2 --> Operate on ints
2266 * 3-5 --> Operate on unsigneds
2267 * 6-8 --> Operate on longs
2268 * 9-11 --> Operate on unsigned longs
2270 * The first subroutine names in each string group is used to encode an
2271 * operation with a zero constant, the second to encode an operation with
2272 * a 8 bit constant, and the third is used in all other cases.
2277 /* Determine the offset into the array */
2278 offs = (flags & CF_UNSIGNED)? 3 : 0;
2279 switch (flags & CF_TYPE) {
2292 /* Encode the operation */
2293 if (flags & CF_CONST) {
2294 /* Constant value given */
2295 if (val == 0 && subs [offs+0]) {
2296 /* Special case: constant with value zero */
2297 AddCodeLine ("jsr %s", subs [offs+0]);
2298 } else if (val < 0x100 && subs [offs+1]) {
2299 /* Special case: constant with high byte zero */
2300 ldaconst (val); /* Load low byte */
2301 AddCodeLine ("jsr %s", subs [offs+1]);
2303 /* Others: arbitrary constant value */
2304 g_getimmed (flags, val, 0); /* Load value */
2305 AddCodeLine ("jsr %s", subs [offs+2]);
2308 /* Value not constant (is already in (e)ax) */
2309 AddCodeLine ("jsr %s", subs [offs+2]);
2312 /* The operation will pop it's argument */
2318 void g_test (unsigned flags)
2319 /* Test the value in the primary and set the condition codes */
2321 switch (flags & CF_TYPE) {
2324 if (flags & CF_FORCECHAR) {
2325 AddCodeLine ("tax");
2331 AddCodeLine ("stx tmp1");
2332 AddCodeLine ("ora tmp1");
2336 if (flags & CF_UNSIGNED) {
2337 AddCodeLine ("jsr utsteax");
2339 AddCodeLine ("jsr tsteax");
2351 void g_push (unsigned flags, unsigned long val)
2352 /* Push the primary register or a constant value onto the stack */
2354 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2356 /* We have a constant 8 or 16 bit value */
2357 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2359 /* Handle as 8 bit value */
2361 AddCodeLine ("jsr pusha");
2365 /* Handle as 16 bit value */
2366 g_getimmed (flags, val, 0);
2367 AddCodeLine ("jsr pushax");
2372 /* Value is not 16 bit or not constant */
2373 if (flags & CF_CONST) {
2374 /* Constant 32 bit value, load into eax */
2375 g_getimmed (flags, val, 0);
2378 /* Push the primary register */
2379 switch (flags & CF_TYPE) {
2382 if (flags & CF_FORCECHAR) {
2383 /* Handle as char */
2384 AddCodeLine ("jsr pusha");
2389 AddCodeLine ("jsr pushax");
2393 AddCodeLine ("jsr pusheax");
2403 /* Adjust the stack offset */
2409 void g_swap (unsigned flags)
2410 /* Swap the primary register and the top of the stack. flags give the type
2411 * of *both* values (must have same size).
2414 switch (flags & CF_TYPE) {
2418 AddCodeLine ("jsr swapstk");
2422 AddCodeLine ("jsr swapestk");
2433 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2434 /* Call the specified subroutine name */
2436 if ((Flags & CF_FIXARGC) == 0) {
2437 /* Pass the argument count */
2440 AddCodeLine ("jsr _%s", Label);
2441 oursp += ArgSize; /* callee pops args */
2446 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2447 /* Call subroutine indirect */
2449 if ((Flags & CF_LOCAL) == 0) {
2450 /* Address is in a/x */
2451 if ((Flags & CF_FIXARGC) == 0) {
2452 /* Pass arg count */
2455 AddCodeLine ("jsr callax");
2457 /* The address is on stack, offset is on Val */
2459 CheckLocalOffs (Offs);
2460 AddCodeLine ("pha");
2461 AddCodeLine ("ldy #$%02X", Offs);
2462 AddCodeLine ("lda (sp),y");
2463 AddCodeLine ("sta jmpvec+1");
2464 AddCodeLine ("iny");
2465 AddCodeLine ("lda (sp),y");
2466 AddCodeLine ("sta jmpvec+2");
2467 AddCodeLine ("pla");
2468 AddCodeLine ("jsr jmpvec");
2471 /* Callee pops args */
2477 void g_jump (unsigned Label)
2478 /* Jump to specified internal label number */
2480 AddCodeLine ("jmp %s", LocalLabelName (Label));
2485 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2486 /* Jump to label if zero flag clear */
2488 AddCodeLine ("jne %s", LocalLabelName (label));
2493 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2494 /* Jump to label if zero flag set */
2496 AddCodeLine ("jeq %s", LocalLabelName (label));
2501 static void mod_internal (int k, char* verb1, char* verb2)
2504 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2508 AddCodeLine ("jsr %ssp", verb2);
2514 void g_space (int space)
2515 /* Create or drop space on the stack */
2518 mod_internal (-space, "inc", "addy");
2519 } else if (space > 0) {
2520 mod_internal (space, "dec", "suby");
2526 void g_cstackcheck (void)
2527 /* Check for a C stack overflow */
2529 AddCodeLine ("jsr cstkchk");
2534 void g_stackcheck (void)
2535 /* Check for a stack overflow */
2537 AddCodeLine ("jsr stkchk");
2542 void g_add (unsigned flags, unsigned long val)
2543 /* Primary = TOS + Primary */
2545 static char* ops [12] = {
2546 0, "tosadda0", "tosaddax",
2547 0, "tosadda0", "tosaddax",
2552 if (flags & CF_CONST) {
2553 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2554 g_push (flags & ~CF_CONST, 0);
2556 oper (flags, val, ops);
2561 void g_sub (unsigned flags, unsigned long val)
2562 /* Primary = TOS - Primary */
2564 static char* ops [12] = {
2565 0, "tossuba0", "tossubax",
2566 0, "tossuba0", "tossubax",
2571 if (flags & CF_CONST) {
2572 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2573 g_push (flags & ~CF_CONST, 0);
2575 oper (flags, val, ops);
2580 void g_rsub (unsigned flags, unsigned long val)
2581 /* Primary = Primary - TOS */
2583 static char* ops [12] = {
2584 0, "tosrsuba0", "tosrsubax",
2585 0, "tosrsuba0", "tosrsubax",
2589 oper (flags, val, ops);
2594 void g_mul (unsigned flags, unsigned long val)
2595 /* Primary = TOS * Primary */
2597 static char* ops [12] = {
2598 0, "tosmula0", "tosmulax",
2599 0, "tosumula0", "tosumulax",
2606 /* Do strength reduction if the value is constant and a power of two */
2607 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2608 /* Generate a shift instead */
2613 /* If the right hand side is const, the lhs is not on stack but still
2614 * in the primary register.
2616 if (flags & CF_CONST) {
2618 switch (flags & CF_TYPE) {
2621 if (flags & CF_FORCECHAR) {
2622 /* Handle some special cases */
2626 AddCodeLine ("sta tmp1");
2627 AddCodeLine ("asl a");
2628 AddCodeLine ("clc");
2629 AddCodeLine ("adc tmp1");
2633 AddCodeLine ("sta tmp1");
2634 AddCodeLine ("asl a");
2635 AddCodeLine ("asl a");
2636 AddCodeLine ("clc");
2637 AddCodeLine ("adc tmp1");
2641 AddCodeLine ("sta tmp1");
2642 AddCodeLine ("asl a");
2643 AddCodeLine ("clc");
2644 AddCodeLine ("adc tmp1");
2645 AddCodeLine ("asl a");
2649 AddCodeLine ("sta tmp1");
2650 AddCodeLine ("asl a");
2651 AddCodeLine ("asl a");
2652 AddCodeLine ("clc");
2653 AddCodeLine ("adc tmp1");
2654 AddCodeLine ("asl a");
2663 AddCodeLine ("jsr mulax3");
2666 AddCodeLine ("jsr mulax5");
2669 AddCodeLine ("jsr mulax6");
2672 AddCodeLine ("jsr mulax7");
2675 AddCodeLine ("jsr mulax9");
2678 AddCodeLine ("jsr mulax10");
2690 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2691 * into the normal, non-optimized stuff.
2693 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2694 g_push (flags & ~CF_CONST, 0);
2698 /* Use long way over the stack */
2699 oper (flags, val, ops);
2704 void g_div (unsigned flags, unsigned long val)
2705 /* Primary = TOS / Primary */
2707 static char* ops [12] = {
2708 0, "tosdiva0", "tosdivax",
2709 0, "tosudiva0", "tosudivax",
2714 /* Do strength reduction if the value is constant and a power of two */
2716 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2717 /* Generate a shift instead */
2720 /* Generate a division */
2721 if (flags & CF_CONST) {
2722 /* lhs is not on stack */
2723 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2724 g_push (flags & ~CF_CONST, 0);
2726 oper (flags, val, ops);
2732 void g_mod (unsigned flags, unsigned long val)
2733 /* Primary = TOS % Primary */
2735 static char* ops [12] = {
2736 0, "tosmoda0", "tosmodax",
2737 0, "tosumoda0", "tosumodax",
2743 /* Check if we can do some cost reduction */
2744 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2745 /* We can do that with an AND operation */
2746 g_and (flags, val - 1);
2748 /* Do it the hard way... */
2749 if (flags & CF_CONST) {
2750 /* lhs is not on stack */
2751 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2752 g_push (flags & ~CF_CONST, 0);
2754 oper (flags, val, ops);
2760 void g_or (unsigned flags, unsigned long val)
2761 /* Primary = TOS | Primary */
2763 static char* ops [12] = {
2764 0, "tosora0", "tosorax",
2765 0, "tosora0", "tosorax",
2770 /* If the right hand side is const, the lhs is not on stack but still
2771 * in the primary register.
2773 if (flags & CF_CONST) {
2775 switch (flags & CF_TYPE) {
2778 if (flags & CF_FORCECHAR) {
2779 if ((val & 0xFF) != 0) {
2780 AddCodeLine ("ora #$%02X", (unsigned char)val);
2788 if ((val & 0xFF) != 0) {
2789 AddCodeLine ("ora #$%02X", (unsigned char)val);
2792 } else if ((val & 0xFF00) == 0xFF00) {
2793 if ((val & 0xFF) != 0) {
2794 AddCodeLine ("ora #$%02X", (unsigned char)val);
2803 if ((val & 0xFF) != 0) {
2804 AddCodeLine ("ora #$%02X", (unsigned char)val);
2814 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2815 * into the normal, non-optimized stuff. Note: The standard stuff will
2816 * always work with ints.
2818 flags &= ~CF_FORCECHAR;
2819 g_push (flags & ~CF_CONST, 0);
2822 /* Use long way over the stack */
2823 oper (flags, val, ops);
2828 void g_xor (unsigned flags, unsigned long val)
2829 /* Primary = TOS ^ Primary */
2831 static char* ops [12] = {
2832 0, "tosxora0", "tosxorax",
2833 0, "tosxora0", "tosxorax",
2839 /* If the right hand side is const, the lhs is not on stack but still
2840 * in the primary register.
2842 if (flags & CF_CONST) {
2844 switch (flags & CF_TYPE) {
2847 if (flags & CF_FORCECHAR) {
2848 if ((val & 0xFF) != 0) {
2849 AddCodeLine ("eor #$%02X", (unsigned char)val);
2858 AddCodeLine ("eor #$%02X", (unsigned char)val);
2861 } else if ((val & 0xFF) == 0) {
2862 AddCodeLine ("pha");
2863 AddCodeLine ("txa");
2864 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2865 AddCodeLine ("tax");
2866 AddCodeLine ("pla");
2874 AddCodeLine ("eor #$%02X", (unsigned char)val);
2884 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2885 * into the normal, non-optimized stuff. Note: The standard stuff will
2886 * always work with ints.
2888 flags &= ~CF_FORCECHAR;
2889 g_push (flags & ~CF_CONST, 0);
2892 /* Use long way over the stack */
2893 oper (flags, val, ops);
2898 void g_and (unsigned Flags, unsigned long Val)
2899 /* Primary = TOS & Primary */
2901 static char* ops [12] = {
2902 0, "tosanda0", "tosandax",
2903 0, "tosanda0", "tosandax",
2908 /* If the right hand side is const, the lhs is not on stack but still
2909 * in the primary register.
2911 if (Flags & CF_CONST) {
2913 switch (Flags & CF_TYPE) {
2916 if (Flags & CF_FORCECHAR) {
2917 if ((Val & 0xFF) != 0xFF) {
2918 AddCodeLine ("and #$%02X", (unsigned char)Val);
2924 if ((Val & 0xFFFF) != 0xFFFF) {
2929 } else if (Val != 0xFF) {
2930 AddCodeLine ("and #$%02X", (unsigned char)Val);
2932 } else if ((Val & 0xFF00) == 0xFF00) {
2933 AddCodeLine ("and #$%02X", (unsigned char)Val);
2934 } else if ((Val & 0x00FF) == 0x0000) {
2935 AddCodeLine ("txa");
2936 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2937 AddCodeLine ("tax");
2940 AddCodeLine ("tay");
2941 AddCodeLine ("txa");
2942 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2943 AddCodeLine ("tax");
2944 AddCodeLine ("tya");
2945 if ((Val & 0x00FF) != 0x00FF) {
2946 AddCodeLine ("and #$%02X", (unsigned char)Val);
2955 AddCodeLine ("stx sreg+1");
2956 AddCodeLine ("stx sreg");
2957 if ((Val & 0xFF) != 0xFF) {
2958 AddCodeLine ("and #$%02X", (unsigned char)Val);
2961 } else if (Val == 0xFF00) {
2963 AddCodeLine ("sta sreg+1");
2964 AddCodeLine ("sta sreg");
2973 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2974 * into the normal, non-optimized stuff. Note: The standard stuff will
2975 * always work with ints.
2977 Flags &= ~CF_FORCECHAR;
2978 g_push (Flags & ~CF_CONST, 0);
2981 /* Use long way over the stack */
2982 oper (Flags, Val, ops);
2987 void g_asr (unsigned flags, unsigned long val)
2988 /* Primary = TOS >> Primary */
2990 static char* ops [12] = {
2991 0, "tosasra0", "tosasrax",
2992 0, "tosshra0", "tosshrax",
2997 /* If the right hand side is const, the lhs is not on stack but still
2998 * in the primary register.
3000 if (flags & CF_CONST) {
3002 switch (flags & CF_TYPE) {
3006 if (val >= 8 && (flags & CF_UNSIGNED)) {
3007 AddCodeLine ("txa");
3014 } else if (val >= 1 && val <= 4) {
3015 if (flags & CF_UNSIGNED) {
3016 AddCodeLine ("jsr shrax%ld", val);
3018 AddCodeLine ("jsr asrax%ld", val);
3028 } else if (val >= 1 && val <= 4) {
3029 if (flags & CF_UNSIGNED) {
3030 AddCodeLine ("jsr shreax%ld", val);
3032 AddCodeLine ("jsr asreax%ld", val);
3035 } else if (val == 8 && (flags & CF_UNSIGNED)) {
3036 AddCodeLine ("txa");
3037 AddCodeLine ("ldx sreg");
3038 AddCodeLine ("ldy sreg+1");
3039 AddCodeLine ("sty sreg");
3040 AddCodeLine ("ldy #$00");
3041 AddCodeLine ("sty sreg+1");
3043 } else if (val == 16) {
3044 AddCodeLine ("ldy #$00");
3045 AddCodeLine ("ldx sreg+1");
3046 if ((flags & CF_UNSIGNED) == 0) {
3047 unsigned L = GetLocalLabel();
3048 AddCodeLine ("bpl %s", LocalLabelName (L));
3049 AddCodeLine ("dey");
3052 AddCodeLine ("lda sreg");
3053 AddCodeLine ("sty sreg+1");
3054 AddCodeLine ("sty sreg");
3063 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3064 * into the normal, non-optimized stuff. Note: The standard stuff will
3065 * always work with ints.
3067 flags &= ~CF_FORCECHAR;
3068 g_push (flags & ~CF_CONST, 0);
3071 /* Use long way over the stack */
3072 oper (flags, val, ops);
3077 void g_asl (unsigned flags, unsigned long val)
3078 /* Primary = TOS << Primary */
3080 static char* ops [12] = {
3081 0, "tosasla0", "tosaslax",
3082 0, "tosshla0", "tosshlax",
3088 /* If the right hand side is const, the lhs is not on stack but still
3089 * in the primary register.
3091 if (flags & CF_CONST) {
3093 switch (flags & CF_TYPE) {
3098 AddCodeLine ("tax");
3099 AddCodeLine ("lda #$00");
3105 } else if (val >= 1 && val <= 4) {
3106 if (flags & CF_UNSIGNED) {
3107 AddCodeLine ("jsr shlax%ld", val);
3109 AddCodeLine ("jsr aslax%ld", val);
3119 } else if (val >= 1 && val <= 4) {
3120 if (flags & CF_UNSIGNED) {
3121 AddCodeLine ("jsr shleax%ld", val);
3123 AddCodeLine ("jsr asleax%ld", val);
3126 } else if (val == 8) {
3127 AddCodeLine ("ldy sreg");
3128 AddCodeLine ("sty sreg+1");
3129 AddCodeLine ("stx sreg");
3130 AddCodeLine ("tax");
3131 AddCodeLine ("lda #$00");
3133 } else if (val == 16) {
3134 AddCodeLine ("stx sreg+1");
3135 AddCodeLine ("sta sreg");
3136 AddCodeLine ("lda #$00");
3137 AddCodeLine ("tax");
3146 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3147 * into the normal, non-optimized stuff. Note: The standard stuff will
3148 * always work with ints.
3150 flags &= ~CF_FORCECHAR;
3151 g_push (flags & ~CF_CONST, 0);
3154 /* Use long way over the stack */
3155 oper (flags, val, ops);
3160 void g_neg (unsigned Flags)
3161 /* Primary = -Primary */
3163 switch (Flags & CF_TYPE) {
3166 if (Flags & CF_FORCECHAR) {
3167 AddCodeLine ("eor #$FF");
3168 AddCodeLine ("clc");
3169 AddCodeLine ("adc #$01");
3175 AddCodeLine ("jsr negax");
3179 AddCodeLine ("jsr negeax");
3189 void g_bneg (unsigned flags)
3190 /* Primary = !Primary */
3192 switch (flags & CF_TYPE) {
3195 AddCodeLine ("jsr bnega");
3199 AddCodeLine ("jsr bnegax");
3203 AddCodeLine ("jsr bnegeax");
3213 void g_com (unsigned Flags)
3214 /* Primary = ~Primary */
3216 switch (Flags & CF_TYPE) {
3219 if (Flags & CF_FORCECHAR) {
3220 AddCodeLine ("eor #$FF");
3226 AddCodeLine ("jsr complax");
3230 AddCodeLine ("jsr compleax");
3240 void g_inc (unsigned flags, unsigned long val)
3241 /* Increment the primary register by a given number */
3243 /* Don't inc by zero */
3248 /* Generate code for the supported types */
3250 switch (flags & CF_TYPE) {
3253 if (flags & CF_FORCECHAR) {
3254 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3256 AddCodeLine ("ina");
3259 AddCodeLine ("clc");
3260 AddCodeLine ("adc #$%02X", (unsigned char)val);
3267 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) {
3268 unsigned L = GetLocalLabel();
3269 AddCodeLine ("ina");
3270 AddCodeLine ("bne %s", LocalLabelName (L));
3271 AddCodeLine ("inx");
3273 } else if (CodeSizeFactor < 200) {
3276 AddCodeLine ("jsr incax%lu", val);
3277 } else if (val <= 255) {
3279 AddCodeLine ("jsr incaxy");
3281 g_add (flags | CF_CONST, val);
3284 /* Inline the code */
3286 if ((val & 0xFF) != 0) {
3287 unsigned L = GetLocalLabel();
3288 AddCodeLine ("clc");
3289 AddCodeLine ("adc #$%02X", (unsigned char) val);
3290 AddCodeLine ("bcc %s", LocalLabelName (L));
3291 AddCodeLine ("inx");
3295 AddCodeLine ("inx");
3298 AddCodeLine ("inx");
3301 AddCodeLine ("inx");
3304 AddCodeLine ("clc");
3305 if ((val & 0xFF) != 0) {
3306 AddCodeLine ("adc #$%02X", (unsigned char) val);
3308 AddCodeLine ("pha");
3309 AddCodeLine ("txa");
3310 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3311 AddCodeLine ("tax");
3312 AddCodeLine ("pla");
3320 AddCodeLine ("jsr inceaxy");
3322 g_add (flags | CF_CONST, val);
3334 void g_dec (unsigned flags, unsigned long val)
3335 /* Decrement the primary register by a given number */
3337 /* Don't dec by zero */
3342 /* Generate code for the supported types */
3344 switch (flags & CF_TYPE) {
3347 if (flags & CF_FORCECHAR) {
3348 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3350 AddCodeLine ("dea");
3353 AddCodeLine ("sec");
3354 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3361 if (CodeSizeFactor < 200) {
3362 /* Use subroutines */
3364 AddCodeLine ("jsr decax%d", (int) val);
3365 } else if (val <= 255) {
3367 AddCodeLine ("jsr decaxy");
3369 g_sub (flags | CF_CONST, val);
3372 /* Inline the code */
3374 if ((val & 0xFF) != 0) {
3375 unsigned L = GetLocalLabel();
3376 AddCodeLine ("sec");
3377 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3378 AddCodeLine ("bcs %s", LocalLabelName (L));
3379 AddCodeLine ("dex");
3383 AddCodeLine ("dex");
3386 AddCodeLine ("dex");
3389 AddCodeLine ("sec");
3390 if ((val & 0xFF) != 0) {
3391 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3393 AddCodeLine ("pha");
3394 AddCodeLine ("txa");
3395 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3396 AddCodeLine ("tax");
3397 AddCodeLine ("pla");
3405 AddCodeLine ("jsr deceaxy");
3407 g_sub (flags | CF_CONST, val);
3420 * Following are the conditional operators. They compare the TOS against
3421 * the primary and put a literal 1 in the primary if the condition is
3422 * true, otherwise they clear the primary register
3427 void g_eq (unsigned flags, unsigned long val)
3428 /* Test for equal */
3430 static char* ops [12] = {
3431 "toseq00", "toseqa0", "toseqax",
3432 "toseq00", "toseqa0", "toseqax",
3439 /* If the right hand side is const, the lhs is not on stack but still
3440 * in the primary register.
3442 if (flags & CF_CONST) {
3444 switch (flags & CF_TYPE) {
3447 if (flags & CF_FORCECHAR) {
3448 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3449 AddCodeLine ("jsr booleq");
3455 L = GetLocalLabel();
3456 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3457 AddCodeLine ("bne %s", LocalLabelName (L));
3458 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3460 AddCodeLine ("jsr booleq");
3470 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3471 * into the normal, non-optimized stuff. Note: The standard stuff will
3472 * always work with ints.
3474 flags &= ~CF_FORCECHAR;
3475 g_push (flags & ~CF_CONST, 0);
3478 /* Use long way over the stack */
3479 oper (flags, val, ops);
3484 void g_ne (unsigned flags, unsigned long val)
3485 /* Test for not equal */
3487 static char* ops [12] = {
3488 "tosne00", "tosnea0", "tosneax",
3489 "tosne00", "tosnea0", "tosneax",
3496 /* If the right hand side is const, the lhs is not on stack but still
3497 * in the primary register.
3499 if (flags & CF_CONST) {
3501 switch (flags & CF_TYPE) {
3504 if (flags & CF_FORCECHAR) {
3505 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3506 AddCodeLine ("jsr boolne");
3512 L = GetLocalLabel();
3513 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3514 AddCodeLine ("bne %s", LocalLabelName (L));
3515 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3517 AddCodeLine ("jsr boolne");
3527 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3528 * into the normal, non-optimized stuff. Note: The standard stuff will
3529 * always work with ints.
3531 flags &= ~CF_FORCECHAR;
3532 g_push (flags & ~CF_CONST, 0);
3535 /* Use long way over the stack */
3536 oper (flags, val, ops);
3541 void g_lt (unsigned flags, unsigned long val)
3542 /* Test for less than */
3544 static char* ops [12] = {
3545 "toslt00", "toslta0", "tosltax",
3546 "tosult00", "tosulta0", "tosultax",
3551 /* If the right hand side is const, the lhs is not on stack but still
3552 * in the primary register.
3554 if (flags & CF_CONST) {
3556 /* Because the handling of the overflow flag is too complex for
3557 * inlining, we can handle only unsigned compares, and signed
3558 * compares against zero here.
3560 if (flags & CF_UNSIGNED) {
3562 /* Give a warning in some special cases */
3564 Warning ("Condition is never true");
3565 AddCodeLine ("jsr return0");
3569 /* Look at the type */
3570 switch (flags & CF_TYPE) {
3573 if (flags & CF_FORCECHAR) {
3574 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3575 AddCodeLine ("jsr boolult");
3581 /* If the low byte is zero, we must only test the high byte */
3582 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3583 if ((val & 0xFF) != 0) {
3584 unsigned L = GetLocalLabel();
3585 AddCodeLine ("bne %s", LocalLabelName (L));
3586 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3589 AddCodeLine ("jsr boolult");
3593 /* Do a subtraction */
3594 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3595 AddCodeLine ("txa");
3596 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3597 AddCodeLine ("lda sreg");
3598 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3599 AddCodeLine ("lda sreg+1");
3600 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3601 AddCodeLine ("jsr boolult");
3608 } else if (val == 0) {
3610 /* Look at the type */
3611 switch (flags & CF_TYPE) {
3614 if (flags & CF_FORCECHAR) {
3615 AddCodeLine ("tax");
3616 AddCodeLine ("jsr boollt");
3622 /* Just check the high byte */
3623 AddCodeLine ("txa");
3624 AddCodeLine ("jsr boollt");
3628 /* Just check the high byte */
3629 AddCodeLine ("lda sreg+1");
3630 AddCodeLine ("jsr boollt");
3638 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3639 * into the normal, non-optimized stuff. Note: The standard stuff will
3640 * always work with ints.
3642 flags &= ~CF_FORCECHAR;
3643 g_push (flags & ~CF_CONST, 0);
3646 /* Use long way over the stack */
3647 oper (flags, val, ops);
3652 void g_le (unsigned flags, unsigned long val)
3653 /* Test for less than or equal to */
3655 static char* ops [12] = {
3656 "tosle00", "toslea0", "tosleax",
3657 "tosule00", "tosulea0", "tosuleax",
3663 /* If the right hand side is const, the lhs is not on stack but still
3664 * in the primary register.
3666 if (flags & CF_CONST) {
3668 /* Look at the type */
3669 switch (flags & CF_TYPE) {
3672 if (flags & CF_FORCECHAR) {
3673 if (flags & CF_UNSIGNED) {
3674 /* Unsigned compare */
3676 /* Use < instead of <= because the former gives
3677 * better code on the 6502 than the latter.
3679 g_lt (flags, val+1);
3682 Warning ("Condition is always true");
3683 AddCodeLine ("jsr return1");
3686 /* Signed compare */
3687 if ((long) val < 0x7F) {
3688 /* Use < instead of <= because the former gives
3689 * better code on the 6502 than the latter.
3691 g_lt (flags, val+1);
3694 Warning ("Condition is always true");
3695 AddCodeLine ("jsr return1");
3703 if (flags & CF_UNSIGNED) {
3704 /* Unsigned compare */
3706 /* Use < instead of <= because the former gives
3707 * better code on the 6502 than the latter.
3709 g_lt (flags, val+1);
3712 Warning ("Condition is always true");
3713 AddCodeLine ("jsr return1");
3716 /* Signed compare */
3717 if ((long) val < 0x7FFF) {
3718 g_lt (flags, val+1);
3721 Warning ("Condition is always true");
3722 AddCodeLine ("jsr return1");
3728 if (flags & CF_UNSIGNED) {
3729 /* Unsigned compare */
3730 if (val < 0xFFFFFFFF) {
3731 /* Use < instead of <= because the former gives
3732 * better code on the 6502 than the latter.
3734 g_lt (flags, val+1);
3737 Warning ("Condition is always true");
3738 AddCodeLine ("jsr return1");
3741 /* Signed compare */
3742 if ((long) val < 0x7FFFFFFF) {
3743 g_lt (flags, val+1);
3746 Warning ("Condition is always true");
3747 AddCodeLine ("jsr return1");
3756 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3757 * into the normal, non-optimized stuff. Note: The standard stuff will
3758 * always work with ints.
3760 flags &= ~CF_FORCECHAR;
3761 g_push (flags & ~CF_CONST, 0);
3764 /* Use long way over the stack */
3765 oper (flags, val, ops);
3770 void g_gt (unsigned flags, unsigned long val)
3771 /* Test for greater than */
3773 static char* ops [12] = {
3774 "tosgt00", "tosgta0", "tosgtax",
3775 "tosugt00", "tosugta0", "tosugtax",
3781 /* If the right hand side is const, the lhs is not on stack but still
3782 * in the primary register.
3784 if (flags & CF_CONST) {
3786 /* Look at the type */
3787 switch (flags & CF_TYPE) {
3790 if (flags & CF_FORCECHAR) {
3791 if (flags & CF_UNSIGNED) {
3793 /* If we have a compare > 0, we will replace it by
3794 * != 0 here, since both are identical but the
3795 * latter is easier to optimize.
3798 } else if (val < 0xFF) {
3799 /* Use >= instead of > because the former gives
3800 * better code on the 6502 than the latter.
3802 g_ge (flags, val+1);
3805 Warning ("Condition is never true");
3806 AddCodeLine ("jsr return0");
3809 if ((long) val < 0x7F) {
3810 /* Use >= instead of > because the former gives
3811 * better code on the 6502 than the latter.
3813 g_ge (flags, val+1);
3816 Warning ("Condition is never true");
3817 AddCodeLine ("jsr return0");
3825 if (flags & CF_UNSIGNED) {
3826 /* Unsigned compare */
3828 /* If we have a compare > 0, we will replace it by
3829 * != 0 here, since both are identical but the latter
3830 * is easier to optimize.
3833 } else if (val < 0xFFFF) {
3834 /* Use >= instead of > because the former gives better
3835 * code on the 6502 than the latter.
3837 g_ge (flags, val+1);
3840 Warning ("Condition is never true");
3841 AddCodeLine ("jsr return0");
3844 /* Signed compare */
3845 if ((long) val < 0x7FFF) {
3846 g_ge (flags, val+1);
3849 Warning ("Condition is never true");
3850 AddCodeLine ("jsr return0");
3856 if (flags & CF_UNSIGNED) {
3857 /* Unsigned compare */
3859 /* If we have a compare > 0, we will replace it by
3860 * != 0 here, since both are identical but the latter
3861 * is easier to optimize.
3864 } else if (val < 0xFFFFFFFF) {
3865 /* Use >= instead of > because the former gives better
3866 * code on the 6502 than the latter.
3868 g_ge (flags, val+1);
3871 Warning ("Condition is never true");
3872 AddCodeLine ("jsr return0");
3875 /* Signed compare */
3876 if ((long) val < 0x7FFFFFFF) {
3877 g_ge (flags, val+1);
3880 Warning ("Condition is never true");
3881 AddCodeLine ("jsr return0");
3890 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3891 * into the normal, non-optimized stuff. Note: The standard stuff will
3892 * always work with ints.
3894 flags &= ~CF_FORCECHAR;
3895 g_push (flags & ~CF_CONST, 0);
3898 /* Use long way over the stack */
3899 oper (flags, val, ops);
3904 void g_ge (unsigned flags, unsigned long val)
3905 /* Test for greater than or equal to */
3907 static char* ops [12] = {
3908 "tosge00", "tosgea0", "tosgeax",
3909 "tosuge00", "tosugea0", "tosugeax",
3915 /* If the right hand side is const, the lhs is not on stack but still
3916 * in the primary register.
3918 if (flags & CF_CONST) {
3920 /* Because the handling of the overflow flag is too complex for
3921 * inlining, we can handle only unsigned compares, and signed
3922 * compares against zero here.
3924 if (flags & CF_UNSIGNED) {
3926 /* Give a warning in some special cases */
3928 Warning ("Condition is always true");
3929 AddCodeLine ("jsr return1");
3933 /* Look at the type */
3934 switch (flags & CF_TYPE) {
3937 if (flags & CF_FORCECHAR) {
3938 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3939 AddCodeLine ("jsr booluge");
3945 /* If the low byte is zero, we must only test the high byte */
3946 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3947 if ((val & 0xFF) != 0) {
3948 unsigned L = GetLocalLabel();
3949 AddCodeLine ("bne %s", LocalLabelName (L));
3950 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3953 AddCodeLine ("jsr booluge");
3957 /* Do a subtraction */
3958 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3959 AddCodeLine ("txa");
3960 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3961 AddCodeLine ("lda sreg");
3962 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3963 AddCodeLine ("lda sreg+1");
3964 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3965 AddCodeLine ("jsr booluge");
3972 } else if (val == 0) {
3974 /* Look at the type */
3975 switch (flags & CF_TYPE) {
3978 if (flags & CF_FORCECHAR) {
3979 AddCodeLine ("tax");
3980 AddCodeLine ("jsr boolge");
3986 /* Just test the high byte */
3987 AddCodeLine ("txa");
3988 AddCodeLine ("jsr boolge");
3992 /* Just test the high byte */
3993 AddCodeLine ("lda sreg+1");
3994 AddCodeLine ("jsr boolge");
4003 /* If we go here, we didn't emit code. Push the lhs on stack and fall
4004 * into the normal, non-optimized stuff. Note: The standard stuff will
4005 * always work with ints.
4007 flags &= ~CF_FORCECHAR;
4008 g_push (flags & ~CF_CONST, 0);
4011 /* Use long way over the stack */
4012 oper (flags, val, ops);
4017 /*****************************************************************************/
4018 /* Allocating static storage */
4019 /*****************************************************************************/
4023 void g_res (unsigned n)
4024 /* Reserve static storage, n bytes */
4026 AddDataLine ("\t.res\t%u,$00", n);
4031 void g_defdata (unsigned flags, unsigned long val, long offs)
4032 /* Define data with the size given in flags */
4034 if (flags & CF_CONST) {
4036 /* Numeric constant */
4037 switch (flags & CF_TYPE) {
4040 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
4044 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
4048 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
4059 /* Create the correct label name */
4060 const char* Label = GetLabelName (flags, val, offs);
4062 /* Labels are always 16 bit */
4063 AddDataLine ("\t.addr\t%s", Label);
4070 void g_defbytes (const void* Bytes, unsigned Count)
4071 /* Output a row of bytes as a constant */
4077 /* Cast the buffer pointer */
4078 const unsigned char* Data = (const unsigned char*) Bytes;
4080 /* Output the stuff */
4083 /* How many go into this line? */
4084 if ((Chunk = Count) > 16) {
4089 /* Output one line */
4090 strcpy (Buf, "\t.byte\t");
4093 B += sprintf (B, "$%02X", *Data++);
4099 /* Output the line */
4106 void g_zerobytes (unsigned Count)
4107 /* Output Count bytes of data initialized with zero */
4110 AddDataLine ("\t.res\t%u,$00", Count);
4116 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4117 /* Initialize a register variable from static initialization data */
4119 /* Register variables do always have less than 128 bytes */
4120 unsigned CodeLabel = GetLocalLabel ();
4122 g_defcodelabel (CodeLabel);
4123 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4124 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4125 AddCodeLine ("dex");
4126 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4131 void g_initauto (unsigned Label, unsigned Size)
4132 /* Initialize a local variable at stack offset zero from static data */
4134 unsigned CodeLabel = GetLocalLabel ();
4136 CheckLocalOffs (Size);
4139 g_defcodelabel (CodeLabel);
4140 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4141 AddCodeLine ("sta (sp),y");
4142 AddCodeLine ("dey");
4143 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4144 } else if (Size <= 256) {
4146 g_defcodelabel (CodeLabel);
4147 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4148 AddCodeLine ("sta (sp),y");
4149 AddCodeLine ("iny");
4150 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4151 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4157 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4158 /* Initialize a static local variable from static initialization data */
4161 unsigned CodeLabel = GetLocalLabel ();
4163 g_defcodelabel (CodeLabel);
4164 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4165 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4166 AddCodeLine ("dey");
4167 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4168 } else if (Size <= 256) {
4169 unsigned CodeLabel = GetLocalLabel ();
4171 g_defcodelabel (CodeLabel);
4172 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4173 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4174 AddCodeLine ("iny");
4175 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4176 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4178 /* Use the easy way here: memcpy */
4179 g_getimmed (CF_STATIC, VarLabel, 0);
4180 AddCodeLine ("jsr pushax");
4181 g_getimmed (CF_STATIC, InitLabel, 0);
4182 AddCodeLine ("jsr pushax");
4183 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4184 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4190 /*****************************************************************************/
4191 /* Switch statement */
4192 /*****************************************************************************/
4196 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4197 /* Generate code for a switch statement */
4199 unsigned NextLabel = 0;
4202 /* Setup registers and determine which compare insn to use */
4203 const char* Compare;
4206 Compare = "cmp #$%02X";
4209 Compare = "cpx #$%02X";
4212 AddCodeLine ("ldy sreg");
4213 Compare = "cpy #$%02X";
4216 AddCodeLine ("ldy sreg+1");
4217 Compare = "cpy #$%02X";
4220 Internal ("Invalid depth in g_switch: %u", Depth);
4223 /* Walk over all nodes */
4224 for (I = 0; I < CollCount (Nodes); ++I) {
4226 /* Get the next case node */
4227 CaseNode* N = CollAtUnchecked (Nodes, I);
4229 /* If we have a next label, define it */
4231 g_defcodelabel (NextLabel);
4235 /* Do the compare */
4236 AddCodeLine (Compare, CN_GetValue (N));
4238 /* If this is the last level, jump directly to the case code if found */
4241 /* Branch if equal */
4242 g_falsejump (0, CN_GetLabel (N));
4246 /* Determine the next label */
4247 if (I == CollCount (Nodes) - 1) {
4248 /* Last node means not found */
4249 g_truejump (0, DefaultLabel);
4251 /* Jump to the next check */
4252 NextLabel = GetLocalLabel ();
4253 g_truejump (0, NextLabel);
4256 /* Check the next level */
4257 g_switch (N->Nodes, DefaultLabel, Depth-1);
4262 /* If we go here, we haven't found the label */
4263 g_jump (DefaultLabel);
4268 /*****************************************************************************/
4269 /* User supplied assembler code */
4270 /*****************************************************************************/
4274 void g_asmcode (struct StrBuf* B)
4275 /* Output one line of assembler code. */
4277 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));
4282 /*****************************************************************************/
4283 /* Inlined known functions */
4284 /*****************************************************************************/
4288 void g_strlen (unsigned flags, unsigned long val, long offs)
4289 /* Inline the strlen() function */
4291 /* We need a label in both cases */
4292 unsigned label = GetLocalLabel ();
4294 /* Two different encodings */
4295 if (flags & CF_CONST) {
4297 /* The address of the string is constant. Create the correct label name */
4298 const char* lbuf = GetLabelName (flags, val, offs);
4300 /* Generate the strlen code */
4301 AddCodeLine ("ldy #$FF");
4302 g_defcodelabel (label);
4303 AddCodeLine ("iny");
4304 AddCodeLine ("lda %s,y", lbuf);
4305 AddCodeLine ("bne %s", LocalLabelName (label));
4306 AddCodeLine ("tax");
4307 AddCodeLine ("tya");
4311 /* Address not constant but in primary */
4312 if (CodeSizeFactor < 400) {
4313 /* This is too much code, so call strlen instead of inlining */
4314 AddCodeLine ("jsr _strlen");
4316 /* Inline the function */
4317 AddCodeLine ("sta ptr1");
4318 AddCodeLine ("stx ptr1+1");
4319 AddCodeLine ("ldy #$FF");
4320 g_defcodelabel (label);
4321 AddCodeLine ("iny");
4322 AddCodeLine ("lda (ptr1),y");
4323 AddCodeLine ("bne %s", LocalLabelName (label));
4324 AddCodeLine ("tax");
4325 AddCodeLine ("tya");