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 case CPU_HUC6280: AddTextLine ("\t.setcpu\t\t\"HUC6280\""); break;
165 default: Internal ("Unknown CPU: %d", CPU);
169 AddTextLine ("\t.smart\t\ton");
171 /* Allow auto import for runtime library routines */
172 AddTextLine ("\t.autoimport\ton");
174 /* Switch the assembler into case sensitive mode */
175 AddTextLine ("\t.case\t\ton");
177 /* Tell the assembler if we want to generate debug info */
178 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
180 /* Import the stack pointer for direct auto variable access */
181 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
183 /* Define long branch macros */
184 AddTextLine ("\t.macpack\tlongbranch");
189 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
190 /* If debug info is enabled, place a file info into the source */
193 /* We have to place this into the global text segment, so it will
194 * appear before all .dbg line statements.
196 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
202 /*****************************************************************************/
203 /* Segment support */
204 /*****************************************************************************/
208 void g_userodata (void)
209 /* Switch to the read only data segment */
211 UseDataSeg (SEG_RODATA);
216 void g_usedata (void)
217 /* Switch to the data segment */
219 UseDataSeg (SEG_DATA);
225 /* Switch to the bss segment */
227 UseDataSeg (SEG_BSS);
232 void g_segname (segment_t Seg)
233 /* Emit the name of a segment if necessary */
235 /* Emit a segment directive for the data style segments */
238 case SEG_RODATA: S = CS->ROData; break;
239 case SEG_DATA: S = CS->Data; break;
240 case SEG_BSS: S = CS->BSS; break;
241 default: S = 0; break;
244 DS_AddLine (S, ".segment\t\"%s\"", GetSegName (Seg));
250 /*****************************************************************************/
252 /*****************************************************************************/
256 unsigned sizeofarg (unsigned flags)
257 /* Return the size of a function argument type that is encoded in flags */
259 switch (flags & CF_TYPE) {
262 return (flags & CF_FORCECHAR)? 1 : 2;
282 int pop (unsigned flags)
283 /* Pop an argument of the given size */
285 return StackPtr += sizeofarg (flags);
290 int push (unsigned flags)
291 /* Push an argument of the given size */
293 return StackPtr -= sizeofarg (flags);
298 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
299 /* The value in Offs is an offset to an address in a/x. Make sure, an object
300 * of the type given in Flags can be loaded or stored into this address by
301 * adding part of the offset to the address in ax, so that the remaining
302 * offset fits into an index register. Return the remaining offset.
305 /* If the offset is too large for a byte register, add the high byte
306 * of the offset to the primary. Beware: We need a special correction
307 * if the offset in the low byte will overflow in the operation.
309 unsigned O = Offs & ~0xFFU;
310 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
311 /* We need to add the low byte also */
315 /* Do the correction if we need one */
317 g_inc (CF_INT | CF_CONST, O);
321 /* Return the new offset */
327 /*****************************************************************************/
328 /* Functions handling local labels */
329 /*****************************************************************************/
333 void g_defcodelabel (unsigned label)
334 /* Define a local code label */
336 CS_AddLabel (CS->Code, LocalLabelName (label));
341 void g_defdatalabel (unsigned label)
342 /* Define a local data label */
344 AddDataLine ("%s:", LocalLabelName (label));
349 /*****************************************************************************/
350 /* Functions handling global labels */
351 /*****************************************************************************/
355 void g_defgloblabel (const char* Name)
356 /* Define a global label with the given name */
358 /* Global labels are always data labels */
359 AddDataLine ("_%s:", Name);
364 void g_defexport (const char* Name, int ZP)
365 /* Export the given label */
368 AddTextLine ("\t.exportzp\t_%s", Name);
370 AddTextLine ("\t.export\t\t_%s", Name);
376 void g_defimport (const char* Name, int ZP)
377 /* Import the given label */
380 AddTextLine ("\t.importzp\t_%s", Name);
382 AddTextLine ("\t.import\t\t_%s", Name);
388 void g_importmainargs (void)
389 /* Forced import of a special symbol that handles arguments to main */
391 AddTextLine ("\t.forceimport\tinitmainargs");
396 /*****************************************************************************/
397 /* Load functions for various registers */
398 /*****************************************************************************/
402 static void ldaconst (unsigned val)
403 /* Load a with a constant */
405 AddCodeLine ("lda #$%02X", val & 0xFF);
410 static void ldxconst (unsigned val)
411 /* Load x with a constant */
413 AddCodeLine ("ldx #$%02X", val & 0xFF);
418 static void ldyconst (unsigned val)
419 /* Load y with a constant */
421 AddCodeLine ("ldy #$%02X", val & 0xFF);
426 /*****************************************************************************/
427 /* Function entry and exit */
428 /*****************************************************************************/
432 /* Remember the argument size of a function. The variable is set by g_enter
433 * and used by g_leave. If the functions gets its argument size by the caller
434 * (variable param list or function without prototype), g_enter will set the
440 void g_enter (unsigned flags, unsigned argsize)
441 /* Function prologue */
443 if ((flags & CF_FIXARGC) != 0) {
444 /* Just remember the argument size for the leave */
448 AddCodeLine ("jsr enter");
455 /* Function epilogue */
457 /* How many bytes of locals do we have to drop? */
458 unsigned k = (unsigned) -StackPtr;
460 /* If we didn't have a variable argument list, don't call leave */
463 /* Drop stackframe if needed. We can only drop 255 bytes at a time. */
466 unsigned ToDrop = (k > 255)? 255 : k;
468 AddCodeLine ("jsr incsp%d", k);
471 AddCodeLine ("jsr addysp");
479 /* Nothing to drop */
480 AddCodeLine ("jsr leave");
482 /* We've a stack frame to drop */
485 AddCodeLine ("jsr addysp");
489 AddCodeLine ("jsr leavey");
493 /* Add the final rts */
499 /*****************************************************************************/
500 /* Register variables */
501 /*****************************************************************************/
505 void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
506 /* Swap a register variable with a location on the stack */
508 /* Calculate the actual stack offset and check it */
509 StackOffs -= StackPtr;
510 CheckLocalOffs (StackOffs);
515 if (IS_Get (&CodeSizeFactor) < 165) {
516 ldyconst (StackOffs);
518 AddCodeLine ("jsr regswap1");
520 ldyconst (StackOffs);
521 AddCodeLine ("lda (sp),y");
522 AddCodeLine ("ldx regbank%+d", RegOffs);
523 AddCodeLine ("sta regbank%+d", RegOffs);
525 AddCodeLine ("sta (sp),y");
528 } else if (Bytes == 2) {
530 ldyconst (StackOffs);
532 AddCodeLine ("jsr regswap2");
536 ldyconst (StackOffs);
539 AddCodeLine ("jsr regswap");
545 void g_save_regvars (int RegOffs, unsigned Bytes)
546 /* Save register variables */
548 /* Don't loop for up to two bytes */
551 AddCodeLine ("lda regbank%+d", RegOffs);
552 AddCodeLine ("jsr pusha");
554 } else if (Bytes == 2) {
556 AddCodeLine ("lda regbank%+d", RegOffs);
557 AddCodeLine ("ldx regbank%+d", RegOffs+1);
558 AddCodeLine ("jsr pushax");
562 /* More than two bytes - loop */
563 unsigned Label = GetLocalLabel ();
565 ldyconst (Bytes - 1);
567 g_defcodelabel (Label);
568 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
569 AddCodeLine ("sta (sp),y");
572 AddCodeLine ("bne %s", LocalLabelName (Label));
576 /* We pushed stuff, correct the stack pointer */
582 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
583 /* Restore register variables */
585 /* Calculate the actual stack offset and check it */
586 StackOffs -= StackPtr;
587 CheckLocalOffs (StackOffs);
589 /* Don't loop for up to two bytes */
592 ldyconst (StackOffs);
593 AddCodeLine ("lda (sp),y");
594 AddCodeLine ("sta regbank%+d", RegOffs);
596 } else if (Bytes == 2) {
598 ldyconst (StackOffs);
599 AddCodeLine ("lda (sp),y");
600 AddCodeLine ("sta regbank%+d", RegOffs);
602 AddCodeLine ("lda (sp),y");
603 AddCodeLine ("sta regbank%+d", RegOffs+1);
605 } else if (Bytes == 3 && IS_Get (&CodeSizeFactor) >= 133) {
607 ldyconst (StackOffs);
608 AddCodeLine ("lda (sp),y");
609 AddCodeLine ("sta regbank%+d", RegOffs);
611 AddCodeLine ("lda (sp),y");
612 AddCodeLine ("sta regbank%+d", RegOffs+1);
614 AddCodeLine ("lda (sp),y");
615 AddCodeLine ("sta regbank%+d", RegOffs+2);
617 } else if (StackOffs <= RegOffs) {
619 /* More bytes, but the relation between the register offset in the
620 * register bank and the stack offset allows us to generate short
621 * code that uses just one index register.
623 unsigned Label = GetLocalLabel ();
624 ldyconst (StackOffs);
625 g_defcodelabel (Label);
626 AddCodeLine ("lda (sp),y");
627 AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs);
629 AddCodeLine ("cpy #$%02X", StackOffs + Bytes);
630 AddCodeLine ("bne %s", LocalLabelName (Label));
634 /* Ok, this is the generic code. We need to save X because the
635 * caller will only save A.
637 unsigned Label = GetLocalLabel ();
638 AddCodeLine ("stx tmp1");
639 ldyconst (StackOffs + Bytes - 1);
640 ldxconst (Bytes - 1);
641 g_defcodelabel (Label);
642 AddCodeLine ("lda (sp),y");
643 AddCodeLine ("sta regbank%+d,x", RegOffs);
646 AddCodeLine ("bpl %s", LocalLabelName (Label));
647 AddCodeLine ("ldx tmp1");
654 /*****************************************************************************/
655 /* Fetching memory cells */
656 /*****************************************************************************/
660 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
661 /* Load a constant into the primary register */
663 unsigned char B1, B2, B3, B4;
667 if ((Flags & CF_CONST) != 0) {
669 /* Numeric constant */
670 switch (Flags & CF_TYPE) {
673 if ((Flags & CF_FORCECHAR) != 0) {
679 ldxconst ((Val >> 8) & 0xFF);
680 ldaconst (Val & 0xFF);
684 /* Split the value into 4 bytes */
685 B1 = (unsigned char) (Val >> 0);
686 B2 = (unsigned char) (Val >> 8);
687 B3 = (unsigned char) (Val >> 16);
688 B4 = (unsigned char) (Val >> 24);
690 /* Remember which bytes are done */
694 AddCodeLine ("ldx #$%02X", B2);
697 AddCodeLine ("stx sreg");
701 AddCodeLine ("stx sreg+1");
704 if ((Done & 0x04) == 0 && B1 != B3) {
705 AddCodeLine ("lda #$%02X", B3);
706 AddCodeLine ("sta sreg");
709 if ((Done & 0x08) == 0 && B1 != B4) {
710 AddCodeLine ("lda #$%02X", B4);
711 AddCodeLine ("sta sreg+1");
714 AddCodeLine ("lda #$%02X", B1);
716 if ((Done & 0x04) == 0) {
718 AddCodeLine ("sta sreg");
720 if ((Done & 0x08) == 0) {
722 AddCodeLine ("sta sreg+1");
734 /* Some sort of label */
735 const char* Label = GetLabelName (Flags, Val, Offs);
737 /* Load the address into the primary */
738 AddCodeLine ("lda #<(%s)", Label);
739 AddCodeLine ("ldx #>(%s)", Label);
746 void g_getstatic (unsigned flags, unsigned long label, long offs)
747 /* Fetch an static memory cell into the primary register */
749 /* Create the correct label name */
750 const char* lbuf = GetLabelName (flags, label, offs);
752 /* Check the size and generate the correct load operation */
753 switch (flags & CF_TYPE) {
756 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
757 AddCodeLine ("lda %s", lbuf); /* load A from the label */
760 AddCodeLine ("lda %s", lbuf); /* load A from the label */
761 if (!(flags & CF_UNSIGNED)) {
762 /* Must sign extend */
763 unsigned L = GetLocalLabel ();
764 AddCodeLine ("bpl %s", LocalLabelName (L));
772 AddCodeLine ("lda %s", lbuf);
773 if (flags & CF_TEST) {
774 AddCodeLine ("ora %s+1", lbuf);
776 AddCodeLine ("ldx %s+1", lbuf);
781 if (flags & CF_TEST) {
782 AddCodeLine ("lda %s+3", lbuf);
783 AddCodeLine ("ora %s+2", lbuf);
784 AddCodeLine ("ora %s+1", lbuf);
785 AddCodeLine ("ora %s+0", lbuf);
787 AddCodeLine ("lda %s+3", lbuf);
788 AddCodeLine ("sta sreg+1");
789 AddCodeLine ("lda %s+2", lbuf);
790 AddCodeLine ("sta sreg");
791 AddCodeLine ("ldx %s+1", lbuf);
792 AddCodeLine ("lda %s", lbuf);
804 void g_getlocal (unsigned flags, int offs)
805 /* Fetch specified local object (local var). */
808 CheckLocalOffs (offs);
809 switch (flags & CF_TYPE) {
812 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
814 AddCodeLine ("lda (sp),y");
817 AddCodeLine ("ldx #$00");
818 AddCodeLine ("lda (sp),y");
819 if ((flags & CF_UNSIGNED) == 0) {
820 unsigned L = GetLocalLabel();
821 AddCodeLine ("bpl %s", LocalLabelName (L));
829 CheckLocalOffs (offs + 1);
830 if (flags & CF_TEST) {
832 AddCodeLine ("lda (sp),y");
834 AddCodeLine ("ora (sp),y");
837 AddCodeLine ("jsr ldaxysp");
843 AddCodeLine ("jsr ldeaxysp");
844 if (flags & CF_TEST) {
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) {
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 (IS_Get (&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.
961 CHECK (StackPtr <= 0);
962 ArgSizeOffs = -StackPtr;
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 (IS_Get (&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 || IS_Get (&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 (IS_Get (&CodeSizeFactor) >= 200) {
1266 AddCodeLine ("ldx #$00");
1267 AddCodeLine ("stx sreg");
1268 AddCodeLine ("stx sreg+1");
1270 AddCodeLine ("jsr aulong");
1273 if (IS_Get (&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 (IS_Get (&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 (IS_Get (&CodeSizeFactor) >= (p2+1)*130) {
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 (IS_Get (&CodeSizeFactor) >= (p2+1)*130) {
1495 AddCodeLine ("stx tmp1");
1497 AddCodeLine ("lsr tmp1");
1498 AddCodeLine ("ror a");
1500 AddCodeLine ("ldx tmp1");
1502 AddCodeLine ("jsr lsrax%d", p2);
1505 if (IS_Get (&CodeSizeFactor) >= (p2+1)*150) {
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 (IS_Get (&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 (IS_Get (&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 (IS_Get (&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 StackPtr += 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 */
2472 StackPtr += ArgSize;
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);
2791 } else if ((val & 0xFF00) == 0xFF00) {
2792 if ((val & 0xFF) != 0) {
2793 AddCodeLine ("ora #$%02X", (unsigned char)val);
2796 } else if (val != 0) {
2797 AddCodeLine ("ora #$%02X", (unsigned char)val);
2798 AddCodeLine ("pha");
2799 AddCodeLine ("txa");
2800 AddCodeLine ("ora #$%02X", (unsigned char)(val >> 8));
2801 AddCodeLine ("tax");
2802 AddCodeLine ("pla");
2808 if ((val & 0xFF) != 0) {
2809 AddCodeLine ("ora #$%02X", (unsigned char)val);
2819 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2820 * into the normal, non-optimized stuff. Note: The standard stuff will
2821 * always work with ints.
2823 flags &= ~CF_FORCECHAR;
2824 g_push (flags & ~CF_CONST, 0);
2827 /* Use long way over the stack */
2828 oper (flags, val, ops);
2833 void g_xor (unsigned flags, unsigned long val)
2834 /* Primary = TOS ^ Primary */
2836 static char* ops [12] = {
2837 0, "tosxora0", "tosxorax",
2838 0, "tosxora0", "tosxorax",
2844 /* If the right hand side is const, the lhs is not on stack but still
2845 * in the primary register.
2847 if (flags & CF_CONST) {
2849 switch (flags & CF_TYPE) {
2852 if (flags & CF_FORCECHAR) {
2853 if ((val & 0xFF) != 0) {
2854 AddCodeLine ("eor #$%02X", (unsigned char)val);
2863 AddCodeLine ("eor #$%02X", (unsigned char)val);
2865 } else if (val != 0) {
2866 if ((val & 0xFF) != 0) {
2867 AddCodeLine ("eor #$%02X", (unsigned char)val);
2869 AddCodeLine ("pha");
2870 AddCodeLine ("txa");
2871 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2872 AddCodeLine ("tax");
2873 AddCodeLine ("pla");
2880 AddCodeLine ("eor #$%02X", (unsigned char)val);
2890 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2891 * into the normal, non-optimized stuff. Note: The standard stuff will
2892 * always work with ints.
2894 flags &= ~CF_FORCECHAR;
2895 g_push (flags & ~CF_CONST, 0);
2898 /* Use long way over the stack */
2899 oper (flags, val, ops);
2904 void g_and (unsigned Flags, unsigned long Val)
2905 /* Primary = TOS & Primary */
2907 static char* ops [12] = {
2908 0, "tosanda0", "tosandax",
2909 0, "tosanda0", "tosandax",
2914 /* If the right hand side is const, the lhs is not on stack but still
2915 * in the primary register.
2917 if (Flags & CF_CONST) {
2919 switch (Flags & CF_TYPE) {
2922 if (Flags & CF_FORCECHAR) {
2923 if ((Val & 0xFF) != 0xFF) {
2924 AddCodeLine ("and #$%02X", (unsigned char)Val);
2930 if ((Val & 0xFFFF) != 0xFFFF) {
2935 } else if (Val != 0xFF) {
2936 AddCodeLine ("and #$%02X", (unsigned char)Val);
2938 } else if ((Val & 0xFF00) == 0xFF00) {
2939 AddCodeLine ("and #$%02X", (unsigned char)Val);
2940 } else if ((Val & 0x00FF) == 0x0000) {
2941 AddCodeLine ("txa");
2942 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2943 AddCodeLine ("tax");
2946 AddCodeLine ("tay");
2947 AddCodeLine ("txa");
2948 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2949 AddCodeLine ("tax");
2950 AddCodeLine ("tya");
2951 if ((Val & 0x00FF) != 0x00FF) {
2952 AddCodeLine ("and #$%02X", (unsigned char)Val);
2961 AddCodeLine ("stx sreg+1");
2962 AddCodeLine ("stx sreg");
2963 if ((Val & 0xFF) != 0xFF) {
2964 AddCodeLine ("and #$%02X", (unsigned char)Val);
2967 } else if (Val == 0xFF00) {
2969 AddCodeLine ("sta sreg+1");
2970 AddCodeLine ("sta sreg");
2979 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2980 * into the normal, non-optimized stuff. Note: The standard stuff will
2981 * always work with ints.
2983 Flags &= ~CF_FORCECHAR;
2984 g_push (Flags & ~CF_CONST, 0);
2987 /* Use long way over the stack */
2988 oper (Flags, Val, ops);
2993 void g_asr (unsigned flags, unsigned long val)
2994 /* Primary = TOS >> Primary */
2996 static char* ops [12] = {
3003 /* If the right hand side is const, the lhs is not on stack but still
3004 * in the primary register.
3006 if (flags & CF_CONST) {
3008 switch (flags & CF_TYPE) {
3012 if (val >= 8 && (flags & CF_UNSIGNED)) {
3013 AddCodeLine ("txa");
3020 } else if (val >= 1 && val <= 4) {
3021 if (flags & CF_UNSIGNED) {
3022 AddCodeLine ("jsr shrax%ld", val);
3024 AddCodeLine ("jsr asrax%ld", val);
3034 } else if (val >= 1 && val <= 4) {
3035 if (flags & CF_UNSIGNED) {
3036 AddCodeLine ("jsr shreax%ld", val);
3038 AddCodeLine ("jsr asreax%ld", val);
3041 } else if (val == 8 && (flags & CF_UNSIGNED)) {
3042 AddCodeLine ("txa");
3043 AddCodeLine ("ldx sreg");
3044 AddCodeLine ("ldy sreg+1");
3045 AddCodeLine ("sty sreg");
3046 AddCodeLine ("ldy #$00");
3047 AddCodeLine ("sty sreg+1");
3049 } else if (val == 16) {
3050 AddCodeLine ("ldy #$00");
3051 AddCodeLine ("ldx sreg+1");
3052 if ((flags & CF_UNSIGNED) == 0) {
3053 unsigned L = GetLocalLabel();
3054 AddCodeLine ("bpl %s", LocalLabelName (L));
3055 AddCodeLine ("dey");
3058 AddCodeLine ("lda sreg");
3059 AddCodeLine ("sty sreg+1");
3060 AddCodeLine ("sty sreg");
3069 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3070 * into the normal, non-optimized stuff. Note: The standard stuff will
3071 * always work with ints.
3073 flags &= ~CF_FORCECHAR;
3074 g_push (flags & ~CF_CONST, 0);
3077 /* Use long way over the stack */
3078 oper (flags, val, ops);
3083 void g_asl (unsigned flags, unsigned long val)
3084 /* Primary = TOS << Primary */
3086 static char* ops [12] = {
3094 /* If the right hand side is const, the lhs is not on stack but still
3095 * in the primary register.
3097 if (flags & CF_CONST) {
3099 switch (flags & CF_TYPE) {
3104 AddCodeLine ("tax");
3105 AddCodeLine ("lda #$00");
3111 } else if (val >= 1 && val <= 4) {
3112 if (flags & CF_UNSIGNED) {
3113 AddCodeLine ("jsr shlax%ld", val);
3115 AddCodeLine ("jsr aslax%ld", val);
3125 } else if (val >= 1 && val <= 4) {
3126 if (flags & CF_UNSIGNED) {
3127 AddCodeLine ("jsr shleax%ld", val);
3129 AddCodeLine ("jsr asleax%ld", val);
3132 } else if (val == 8) {
3133 AddCodeLine ("ldy sreg");
3134 AddCodeLine ("sty sreg+1");
3135 AddCodeLine ("stx sreg");
3136 AddCodeLine ("tax");
3137 AddCodeLine ("lda #$00");
3139 } else if (val == 16) {
3140 AddCodeLine ("stx sreg+1");
3141 AddCodeLine ("sta sreg");
3142 AddCodeLine ("lda #$00");
3143 AddCodeLine ("tax");
3152 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3153 * into the normal, non-optimized stuff. Note: The standard stuff will
3154 * always work with ints.
3156 flags &= ~CF_FORCECHAR;
3157 g_push (flags & ~CF_CONST, 0);
3160 /* Use long way over the stack */
3161 oper (flags, val, ops);
3166 void g_neg (unsigned Flags)
3167 /* Primary = -Primary */
3169 switch (Flags & CF_TYPE) {
3172 if (Flags & CF_FORCECHAR) {
3173 AddCodeLine ("eor #$FF");
3174 AddCodeLine ("clc");
3175 AddCodeLine ("adc #$01");
3181 AddCodeLine ("jsr negax");
3185 AddCodeLine ("jsr negeax");
3195 void g_bneg (unsigned flags)
3196 /* Primary = !Primary */
3198 switch (flags & CF_TYPE) {
3201 AddCodeLine ("jsr bnega");
3205 AddCodeLine ("jsr bnegax");
3209 AddCodeLine ("jsr bnegeax");
3219 void g_com (unsigned Flags)
3220 /* Primary = ~Primary */
3222 switch (Flags & CF_TYPE) {
3225 if (Flags & CF_FORCECHAR) {
3226 AddCodeLine ("eor #$FF");
3232 AddCodeLine ("jsr complax");
3236 AddCodeLine ("jsr compleax");
3246 void g_inc (unsigned flags, unsigned long val)
3247 /* Increment the primary register by a given number */
3249 /* Don't inc by zero */
3254 /* Generate code for the supported types */
3256 switch (flags & CF_TYPE) {
3259 if (flags & CF_FORCECHAR) {
3260 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3262 AddCodeLine ("ina");
3265 AddCodeLine ("clc");
3266 AddCodeLine ("adc #$%02X", (unsigned char)val);
3273 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) {
3274 unsigned L = GetLocalLabel();
3275 AddCodeLine ("ina");
3276 AddCodeLine ("bne %s", LocalLabelName (L));
3277 AddCodeLine ("inx");
3279 } else if (IS_Get (&CodeSizeFactor) < 200) {
3282 AddCodeLine ("jsr incax%lu", val);
3283 } else if (val <= 255) {
3285 AddCodeLine ("jsr incaxy");
3287 g_add (flags | CF_CONST, val);
3290 /* Inline the code */
3292 if ((val & 0xFF) != 0) {
3293 unsigned L = GetLocalLabel();
3294 AddCodeLine ("clc");
3295 AddCodeLine ("adc #$%02X", (unsigned char) val);
3296 AddCodeLine ("bcc %s", LocalLabelName (L));
3297 AddCodeLine ("inx");
3301 AddCodeLine ("inx");
3304 AddCodeLine ("inx");
3307 AddCodeLine ("inx");
3310 AddCodeLine ("clc");
3311 if ((val & 0xFF) != 0) {
3312 AddCodeLine ("adc #$%02X", (unsigned char) val);
3314 AddCodeLine ("pha");
3315 AddCodeLine ("txa");
3316 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3317 AddCodeLine ("tax");
3318 AddCodeLine ("pla");
3326 AddCodeLine ("jsr inceaxy");
3328 g_add (flags | CF_CONST, val);
3340 void g_dec (unsigned flags, unsigned long val)
3341 /* Decrement the primary register by a given number */
3343 /* Don't dec by zero */
3348 /* Generate code for the supported types */
3350 switch (flags & CF_TYPE) {
3353 if (flags & CF_FORCECHAR) {
3354 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3356 AddCodeLine ("dea");
3359 AddCodeLine ("sec");
3360 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3367 if (IS_Get (&CodeSizeFactor) < 200) {
3368 /* Use subroutines */
3370 AddCodeLine ("jsr decax%d", (int) val);
3371 } else if (val <= 255) {
3373 AddCodeLine ("jsr decaxy");
3375 g_sub (flags | CF_CONST, val);
3378 /* Inline the code */
3380 if ((val & 0xFF) != 0) {
3381 unsigned L = GetLocalLabel();
3382 AddCodeLine ("sec");
3383 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3384 AddCodeLine ("bcs %s", LocalLabelName (L));
3385 AddCodeLine ("dex");
3389 AddCodeLine ("dex");
3392 AddCodeLine ("dex");
3395 AddCodeLine ("sec");
3396 if ((val & 0xFF) != 0) {
3397 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3399 AddCodeLine ("pha");
3400 AddCodeLine ("txa");
3401 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3402 AddCodeLine ("tax");
3403 AddCodeLine ("pla");
3411 AddCodeLine ("jsr deceaxy");
3413 g_sub (flags | CF_CONST, val);
3426 * Following are the conditional operators. They compare the TOS against
3427 * the primary and put a literal 1 in the primary if the condition is
3428 * true, otherwise they clear the primary register
3433 void g_eq (unsigned flags, unsigned long val)
3434 /* Test for equal */
3436 static char* ops [12] = {
3437 "toseq00", "toseqa0", "toseqax",
3438 "toseq00", "toseqa0", "toseqax",
3445 /* If the right hand side is const, the lhs is not on stack but still
3446 * in the primary register.
3448 if (flags & CF_CONST) {
3450 switch (flags & CF_TYPE) {
3453 if (flags & CF_FORCECHAR) {
3454 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3455 AddCodeLine ("jsr booleq");
3461 L = GetLocalLabel();
3462 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3463 AddCodeLine ("bne %s", LocalLabelName (L));
3464 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3466 AddCodeLine ("jsr booleq");
3476 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3477 * into the normal, non-optimized stuff. Note: The standard stuff will
3478 * always work with ints.
3480 flags &= ~CF_FORCECHAR;
3481 g_push (flags & ~CF_CONST, 0);
3484 /* Use long way over the stack */
3485 oper (flags, val, ops);
3490 void g_ne (unsigned flags, unsigned long val)
3491 /* Test for not equal */
3493 static char* ops [12] = {
3494 "tosne00", "tosnea0", "tosneax",
3495 "tosne00", "tosnea0", "tosneax",
3502 /* If the right hand side is const, the lhs is not on stack but still
3503 * in the primary register.
3505 if (flags & CF_CONST) {
3507 switch (flags & CF_TYPE) {
3510 if (flags & CF_FORCECHAR) {
3511 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3512 AddCodeLine ("jsr boolne");
3518 L = GetLocalLabel();
3519 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3520 AddCodeLine ("bne %s", LocalLabelName (L));
3521 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3523 AddCodeLine ("jsr boolne");
3533 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3534 * into the normal, non-optimized stuff. Note: The standard stuff will
3535 * always work with ints.
3537 flags &= ~CF_FORCECHAR;
3538 g_push (flags & ~CF_CONST, 0);
3541 /* Use long way over the stack */
3542 oper (flags, val, ops);
3547 void g_lt (unsigned flags, unsigned long val)
3548 /* Test for less than */
3550 static char* ops [12] = {
3551 "toslt00", "toslta0", "tosltax",
3552 "tosult00", "tosulta0", "tosultax",
3557 /* If the right hand side is const, the lhs is not on stack but still
3558 * in the primary register.
3560 if (flags & CF_CONST) {
3562 /* Because the handling of the overflow flag is too complex for
3563 * inlining, we can handle only unsigned compares, and signed
3564 * compares against zero here.
3566 if (flags & CF_UNSIGNED) {
3568 /* Give a warning in some special cases */
3570 Warning ("Condition is never true");
3571 AddCodeLine ("jsr return0");
3575 /* Look at the type */
3576 switch (flags & CF_TYPE) {
3579 if (flags & CF_FORCECHAR) {
3580 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3581 AddCodeLine ("jsr boolult");
3587 /* If the low byte is zero, we must only test the high byte */
3588 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3589 if ((val & 0xFF) != 0) {
3590 unsigned L = GetLocalLabel();
3591 AddCodeLine ("bne %s", LocalLabelName (L));
3592 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3595 AddCodeLine ("jsr boolult");
3599 /* Do a subtraction */
3600 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3601 AddCodeLine ("txa");
3602 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3603 AddCodeLine ("lda sreg");
3604 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3605 AddCodeLine ("lda sreg+1");
3606 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3607 AddCodeLine ("jsr boolult");
3614 } else if (val == 0) {
3616 /* Look at the type */
3617 switch (flags & CF_TYPE) {
3620 if (flags & CF_FORCECHAR) {
3621 AddCodeLine ("tax");
3622 AddCodeLine ("jsr boollt");
3628 /* Just check the high byte */
3629 AddCodeLine ("txa");
3630 AddCodeLine ("jsr boollt");
3634 /* Just check the high byte */
3635 AddCodeLine ("lda sreg+1");
3636 AddCodeLine ("jsr boollt");
3644 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3645 * into the normal, non-optimized stuff. Note: The standard stuff will
3646 * always work with ints.
3648 flags &= ~CF_FORCECHAR;
3649 g_push (flags & ~CF_CONST, 0);
3652 /* Use long way over the stack */
3653 oper (flags, val, ops);
3658 void g_le (unsigned flags, unsigned long val)
3659 /* Test for less than or equal to */
3661 static char* ops [12] = {
3662 "tosle00", "toslea0", "tosleax",
3663 "tosule00", "tosulea0", "tosuleax",
3669 /* If the right hand side is const, the lhs is not on stack but still
3670 * in the primary register.
3672 if (flags & CF_CONST) {
3674 /* Look at the type */
3675 switch (flags & CF_TYPE) {
3678 if (flags & CF_FORCECHAR) {
3679 if (flags & CF_UNSIGNED) {
3680 /* Unsigned compare */
3682 /* Use < instead of <= because the former gives
3683 * better code on the 6502 than the latter.
3685 g_lt (flags, val+1);
3688 Warning ("Condition is always true");
3689 AddCodeLine ("jsr return1");
3692 /* Signed compare */
3693 if ((long) val < 0x7F) {
3694 /* Use < instead of <= because the former gives
3695 * better code on the 6502 than the latter.
3697 g_lt (flags, val+1);
3700 Warning ("Condition is always true");
3701 AddCodeLine ("jsr return1");
3709 if (flags & CF_UNSIGNED) {
3710 /* Unsigned compare */
3712 /* Use < instead of <= because the former gives
3713 * better code on the 6502 than the latter.
3715 g_lt (flags, val+1);
3718 Warning ("Condition is always true");
3719 AddCodeLine ("jsr return1");
3722 /* Signed compare */
3723 if ((long) val < 0x7FFF) {
3724 g_lt (flags, val+1);
3727 Warning ("Condition is always true");
3728 AddCodeLine ("jsr return1");
3734 if (flags & CF_UNSIGNED) {
3735 /* Unsigned compare */
3736 if (val < 0xFFFFFFFF) {
3737 /* Use < instead of <= because the former gives
3738 * better code on the 6502 than the latter.
3740 g_lt (flags, val+1);
3743 Warning ("Condition is always true");
3744 AddCodeLine ("jsr return1");
3747 /* Signed compare */
3748 if ((long) val < 0x7FFFFFFF) {
3749 g_lt (flags, val+1);
3752 Warning ("Condition is always true");
3753 AddCodeLine ("jsr return1");
3762 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3763 * into the normal, non-optimized stuff. Note: The standard stuff will
3764 * always work with ints.
3766 flags &= ~CF_FORCECHAR;
3767 g_push (flags & ~CF_CONST, 0);
3770 /* Use long way over the stack */
3771 oper (flags, val, ops);
3776 void g_gt (unsigned flags, unsigned long val)
3777 /* Test for greater than */
3779 static char* ops [12] = {
3780 "tosgt00", "tosgta0", "tosgtax",
3781 "tosugt00", "tosugta0", "tosugtax",
3787 /* If the right hand side is const, the lhs is not on stack but still
3788 * in the primary register.
3790 if (flags & CF_CONST) {
3792 /* Look at the type */
3793 switch (flags & CF_TYPE) {
3796 if (flags & CF_FORCECHAR) {
3797 if (flags & CF_UNSIGNED) {
3799 /* If we have a compare > 0, we will replace it by
3800 * != 0 here, since both are identical but the
3801 * latter is easier to optimize.
3804 } else if (val < 0xFF) {
3805 /* Use >= instead of > because the former gives
3806 * better code on the 6502 than the latter.
3808 g_ge (flags, val+1);
3811 Warning ("Condition is never true");
3812 AddCodeLine ("jsr return0");
3815 if ((long) val < 0x7F) {
3816 /* Use >= instead of > because the former gives
3817 * better code on the 6502 than the latter.
3819 g_ge (flags, val+1);
3822 Warning ("Condition is never true");
3823 AddCodeLine ("jsr return0");
3831 if (flags & CF_UNSIGNED) {
3832 /* Unsigned compare */
3834 /* If we have a compare > 0, we will replace it by
3835 * != 0 here, since both are identical but the latter
3836 * is easier to optimize.
3839 } else if (val < 0xFFFF) {
3840 /* Use >= instead of > because the former gives better
3841 * code on the 6502 than the latter.
3843 g_ge (flags, val+1);
3846 Warning ("Condition is never true");
3847 AddCodeLine ("jsr return0");
3850 /* Signed compare */
3851 if ((long) val < 0x7FFF) {
3852 g_ge (flags, val+1);
3855 Warning ("Condition is never true");
3856 AddCodeLine ("jsr return0");
3862 if (flags & CF_UNSIGNED) {
3863 /* Unsigned compare */
3865 /* If we have a compare > 0, we will replace it by
3866 * != 0 here, since both are identical but the latter
3867 * is easier to optimize.
3870 } else if (val < 0xFFFFFFFF) {
3871 /* Use >= instead of > because the former gives better
3872 * code on the 6502 than the latter.
3874 g_ge (flags, val+1);
3877 Warning ("Condition is never true");
3878 AddCodeLine ("jsr return0");
3881 /* Signed compare */
3882 if ((long) val < 0x7FFFFFFF) {
3883 g_ge (flags, val+1);
3886 Warning ("Condition is never true");
3887 AddCodeLine ("jsr return0");
3896 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3897 * into the normal, non-optimized stuff. Note: The standard stuff will
3898 * always work with ints.
3900 flags &= ~CF_FORCECHAR;
3901 g_push (flags & ~CF_CONST, 0);
3904 /* Use long way over the stack */
3905 oper (flags, val, ops);
3910 void g_ge (unsigned flags, unsigned long val)
3911 /* Test for greater than or equal to */
3913 static char* ops [12] = {
3914 "tosge00", "tosgea0", "tosgeax",
3915 "tosuge00", "tosugea0", "tosugeax",
3921 /* If the right hand side is const, the lhs is not on stack but still
3922 * in the primary register.
3924 if (flags & CF_CONST) {
3926 /* Because the handling of the overflow flag is too complex for
3927 * inlining, we can handle only unsigned compares, and signed
3928 * compares against zero here.
3930 if (flags & CF_UNSIGNED) {
3932 /* Give a warning in some special cases */
3934 Warning ("Condition is always true");
3935 AddCodeLine ("jsr return1");
3939 /* Look at the type */
3940 switch (flags & CF_TYPE) {
3943 if (flags & CF_FORCECHAR) {
3944 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3945 AddCodeLine ("jsr booluge");
3951 /* If the low byte is zero, we must only test the high byte */
3952 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3953 if ((val & 0xFF) != 0) {
3954 unsigned L = GetLocalLabel();
3955 AddCodeLine ("bne %s", LocalLabelName (L));
3956 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3959 AddCodeLine ("jsr booluge");
3963 /* Do a subtraction */
3964 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3965 AddCodeLine ("txa");
3966 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3967 AddCodeLine ("lda sreg");
3968 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3969 AddCodeLine ("lda sreg+1");
3970 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3971 AddCodeLine ("jsr booluge");
3978 } else if (val == 0) {
3980 /* Look at the type */
3981 switch (flags & CF_TYPE) {
3984 if (flags & CF_FORCECHAR) {
3985 AddCodeLine ("tax");
3986 AddCodeLine ("jsr boolge");
3992 /* Just test the high byte */
3993 AddCodeLine ("txa");
3994 AddCodeLine ("jsr boolge");
3998 /* Just test the high byte */
3999 AddCodeLine ("lda sreg+1");
4000 AddCodeLine ("jsr boolge");
4009 /* If we go here, we didn't emit code. Push the lhs on stack and fall
4010 * into the normal, non-optimized stuff. Note: The standard stuff will
4011 * always work with ints.
4013 flags &= ~CF_FORCECHAR;
4014 g_push (flags & ~CF_CONST, 0);
4017 /* Use long way over the stack */
4018 oper (flags, val, ops);
4023 /*****************************************************************************/
4024 /* Allocating static storage */
4025 /*****************************************************************************/
4029 void g_res (unsigned n)
4030 /* Reserve static storage, n bytes */
4032 AddDataLine ("\t.res\t%u,$00", n);
4037 void g_defdata (unsigned flags, unsigned long val, long offs)
4038 /* Define data with the size given in flags */
4040 if (flags & CF_CONST) {
4042 /* Numeric constant */
4043 switch (flags & CF_TYPE) {
4046 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
4050 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
4054 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
4065 /* Create the correct label name */
4066 const char* Label = GetLabelName (flags, val, offs);
4068 /* Labels are always 16 bit */
4069 AddDataLine ("\t.addr\t%s", Label);
4076 void g_defbytes (const void* Bytes, unsigned Count)
4077 /* Output a row of bytes as a constant */
4083 /* Cast the buffer pointer */
4084 const unsigned char* Data = (const unsigned char*) Bytes;
4086 /* Output the stuff */
4089 /* How many go into this line? */
4090 if ((Chunk = Count) > 16) {
4095 /* Output one line */
4096 strcpy (Buf, "\t.byte\t");
4099 B += sprintf (B, "$%02X", *Data++);
4105 /* Output the line */
4112 void g_zerobytes (unsigned Count)
4113 /* Output Count bytes of data initialized with zero */
4116 AddDataLine ("\t.res\t%u,$00", Count);
4122 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4123 /* Initialize a register variable from static initialization data */
4125 /* Register variables do always have less than 128 bytes */
4126 unsigned CodeLabel = GetLocalLabel ();
4128 g_defcodelabel (CodeLabel);
4129 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4130 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4131 AddCodeLine ("dex");
4132 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4137 void g_initauto (unsigned Label, unsigned Size)
4138 /* Initialize a local variable at stack offset zero from static data */
4140 unsigned CodeLabel = GetLocalLabel ();
4142 CheckLocalOffs (Size);
4145 g_defcodelabel (CodeLabel);
4146 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4147 AddCodeLine ("sta (sp),y");
4148 AddCodeLine ("dey");
4149 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4150 } else if (Size <= 256) {
4152 g_defcodelabel (CodeLabel);
4153 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4154 AddCodeLine ("sta (sp),y");
4155 AddCodeLine ("iny");
4156 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4157 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4163 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4164 /* Initialize a static local variable from static initialization data */
4167 unsigned CodeLabel = GetLocalLabel ();
4169 g_defcodelabel (CodeLabel);
4170 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4171 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4172 AddCodeLine ("dey");
4173 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4174 } else if (Size <= 256) {
4175 unsigned CodeLabel = GetLocalLabel ();
4177 g_defcodelabel (CodeLabel);
4178 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4179 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4180 AddCodeLine ("iny");
4181 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4182 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4184 /* Use the easy way here: memcpy */
4185 g_getimmed (CF_STATIC, VarLabel, 0);
4186 AddCodeLine ("jsr pushax");
4187 g_getimmed (CF_STATIC, InitLabel, 0);
4188 AddCodeLine ("jsr pushax");
4189 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4190 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4196 /*****************************************************************************/
4197 /* Switch statement */
4198 /*****************************************************************************/
4202 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4203 /* Generate code for a switch statement */
4205 unsigned NextLabel = 0;
4208 /* Setup registers and determine which compare insn to use */
4209 const char* Compare;
4212 Compare = "cmp #$%02X";
4215 Compare = "cpx #$%02X";
4218 AddCodeLine ("ldy sreg");
4219 Compare = "cpy #$%02X";
4222 AddCodeLine ("ldy sreg+1");
4223 Compare = "cpy #$%02X";
4226 Internal ("Invalid depth in g_switch: %u", Depth);
4229 /* Walk over all nodes */
4230 for (I = 0; I < CollCount (Nodes); ++I) {
4232 /* Get the next case node */
4233 CaseNode* N = CollAtUnchecked (Nodes, I);
4235 /* If we have a next label, define it */
4237 g_defcodelabel (NextLabel);
4241 /* Do the compare */
4242 AddCodeLine (Compare, CN_GetValue (N));
4244 /* If this is the last level, jump directly to the case code if found */
4247 /* Branch if equal */
4248 g_falsejump (0, CN_GetLabel (N));
4252 /* Determine the next label */
4253 if (I == CollCount (Nodes) - 1) {
4254 /* Last node means not found */
4255 g_truejump (0, DefaultLabel);
4257 /* Jump to the next check */
4258 NextLabel = GetLocalLabel ();
4259 g_truejump (0, NextLabel);
4262 /* Check the next level */
4263 g_switch (N->Nodes, DefaultLabel, Depth-1);
4268 /* If we go here, we haven't found the label */
4269 g_jump (DefaultLabel);
4274 /*****************************************************************************/
4275 /* User supplied assembler code */
4276 /*****************************************************************************/
4280 void g_asmcode (struct StrBuf* B)
4281 /* Output one line of assembler code. */
4283 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));