1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
58 /*****************************************************************************/
60 /*****************************************************************************/
64 /* Compiler relative stack pointer */
72 segment_t CurSeg = SEG_INV;
76 /*****************************************************************************/
78 /*****************************************************************************/
82 static void AddCodeLine (const char* Format, ...)
86 va_start (ap, Format);
87 xvsprintf (Buf, sizeof (Buf), Format, ap);
89 if (CurSeg == SEG_CODE) {
90 AddCodeSegLine (CS, "%s", Buf);
92 AddDataSegLine (DS, "%s", Buf);
98 static void typeerror (unsigned type)
99 /* Print an error message about an invalid operand type */
101 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
106 static void CheckLocalOffs (unsigned Offs)
107 /* Check the offset into the stack for 8bit range */
110 /* Too many local vars */
111 AddCodeLine (";*** Too many locals");
112 Error ("Too many local variables");
118 static char* GetLabelName (unsigned flags, unsigned long label, unsigned offs)
120 static char lbuf [128]; /* Label name */
122 /* Create the correct label name */
123 switch (flags & CF_ADDRMASK) {
126 /* Static memory cell */
127 sprintf (lbuf, "L%04X+%u", (unsigned)(label & 0xFFFF), offs);
132 sprintf (lbuf, "_%s+%u", (char*) label, offs);
136 /* Absolute address */
137 sprintf (lbuf, "$%04X", (unsigned)((label+offs) & 0xFFFF));
141 /* Variable in register bank */
142 sprintf (lbuf, "regbank+%u", (unsigned)((label+offs) & 0xFFFF));
146 Internal ("Invalid address flags");
149 /* Return a pointer to the static buffer */
155 /*****************************************************************************/
156 /* Pre- and postamble */
157 /*****************************************************************************/
161 void g_preamble (void)
162 /* Generate the assembler code preamble */
165 CS = NewCodeSeg ("CODE");
168 AddCodeLine ("; File generated by cc65 v %u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
170 /* Insert some object file options */
171 AddCodeLine (".fopt\t\tcompiler,\"cc65 v %u.%u.%u\"", VER_MAJOR, VER_MINOR, VER_PATCH);
173 /* If we're producing code for some other CPU, switch the command set */
174 if (CPU == CPU_65C02) {
175 AddCodeLine (".pc02");
178 /* Allow auto import for runtime library routines */
179 AddCodeLine (".autoimport\ton");
181 /* Switch the assembler into case sensitive mode */
182 AddCodeLine (".case\t\ton");
184 /* Tell the assembler if we want to generate debug info */
185 AddCodeLine (".debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
187 /* Import the stack pointer for direct auto variable access */
188 AddCodeLine (".importzp\tsp, sreg, regsave, regbank, tmp1, ptr1");
190 /* Define long branch macros */
191 AddCodeLine (".macpack\tlongbranch");
196 /*****************************************************************************/
197 /* Segment support */
198 /*****************************************************************************/
202 static void UseSeg (int NewSeg)
203 /* Switch to a specific segment */
205 if (CurSeg != NewSeg) {
206 CurSeg = (segment_t) NewSeg;
207 if (CurSeg != SEG_CODE) {
208 AddCodeLine (".segment\t\"%s\"", SegmentNames [CurSeg]);
215 void g_usecode (void)
216 /* Switch to the code segment */
223 void g_userodata (void)
224 /* Switch to the read only data segment */
231 void g_usedata (void)
232 /* Switch to the data segment */
240 /* Switch to the bss segment */
247 static void SegName (segment_t Seg, const char* Name)
248 /* Set the name of a segment */
250 /* Free the old name and set a new one */
251 NewSegName (Seg, Name);
253 /* If the new segment is the current segment, emit a segment directive
257 CurSeg = SEG_INV; /* Invalidate */
264 void g_codename (const char* Name)
265 /* Set the name of the CODE segment */
267 SegName (SEG_CODE, Name);
272 void g_rodataname (const char* Name)
273 /* Set the name of the RODATA segment */
275 SegName (SEG_RODATA, Name);
280 void g_dataname (const char* Name)
281 /* Set the name of the DATA segment */
283 SegName (SEG_DATA, Name);
288 void g_bssname (const char* Name)
289 /* Set the name of the BSS segment */
291 SegName (SEG_BSS, Name);
296 /*****************************************************************************/
298 /*****************************************************************************/
302 unsigned sizeofarg (unsigned flags)
303 /* Return the size of a function argument type that is encoded in flags */
305 switch (flags & CF_TYPE) {
308 return (flags & CF_FORCECHAR)? 1 : 2;
325 int pop (unsigned flags)
326 /* Pop an argument of the given size */
328 return oursp += sizeofarg (flags);
333 int push (unsigned flags)
334 /* Push an argument of the given size */
336 return oursp -= sizeofarg (flags);
341 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
342 /* The value in Offs is an offset to an address in a/x. Make sure, an object
343 * of the type given in Flags can be loaded or stored into this address by
344 * adding part of the offset to the address in ax, so that the remaining
345 * offset fits into an index register. Return the remaining offset.
348 /* If the offset is too large for a byte register, add the high byte
349 * of the offset to the primary. Beware: We need a special correction
350 * if the offset in the low byte will overflow in the operation.
352 unsigned O = Offs & ~0xFFU;
353 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
354 /* We need to add the low byte also */
358 /* Do the correction if we need one */
360 g_inc (CF_INT | CF_CONST, O);
364 /* Return the new offset */
370 /*****************************************************************************/
371 /* Functions handling local labels */
372 /*****************************************************************************/
376 void g_defloclabel (unsigned label)
377 /* Define a local label */
379 if (CurSeg == SEG_CODE) {
380 AddLocCodeLabel (CS, LocalLabelName (label));
382 AddDataSegLine (DS, "%s:", LocalLabelName (label));
388 /*****************************************************************************/
389 /* Functions handling global labels */
390 /*****************************************************************************/
394 void g_defgloblabel (const char* Name)
395 /* Define a global label with the given name */
397 if (CurSeg == SEG_CODE) {
400 xsprintf (Buf, sizeof (Buf), "_%s", Name);
401 AddExtCodeLabel (CS, Buf);
403 AddDataSegLine (DS, "_%s:", Name);
409 void g_defexport (const char* Name, int ZP)
410 /* Export the given label */
413 AddDataSegLine (DS, "\t.exportzp\t_%s", Name);
415 AddDataSegLine (DS, "\t.export\t\t_%s", Name);
421 void g_defimport (const char* Name, int ZP)
422 /* Import the given label */
425 AddDataSegLine (DS, "\t.importzp\t_%s", Name);
427 AddDataSegLine (DS, "\t.import\t\t_%s", Name);
433 /*****************************************************************************/
434 /* Load functions for various registers */
435 /*****************************************************************************/
439 static void ldaconst (unsigned val)
440 /* Load a with a constant */
442 AddCodeSegLine (CS, "lda #$%02X", val & 0xFF);
447 static void ldxconst (unsigned val)
448 /* Load x with a constant */
450 AddCodeSegLine (CS, "ldx #$%02X", val & 0xFF);
455 static void ldyconst (unsigned val)
456 /* Load y with a constant */
458 AddCodeSegLine (CS, "ldy #$%02X", val & 0xFF);
463 /*****************************************************************************/
464 /* Function entry and exit */
465 /*****************************************************************************/
469 /* Remember the argument size of a function. The variable is set by g_enter
470 * and used by g_leave. If the functions gets its argument size by the caller
471 * (variable param list or function without prototype), g_enter will set the
477 void g_enter (unsigned flags, unsigned argsize)
478 /* Function prologue */
480 if ((flags & CF_FIXARGC) != 0) {
481 /* Just remember the argument size for the leave */
485 AddCodeSegLine (CS, "jsr enter");
491 void g_leave (int flags, int val)
492 /* Function epilogue */
497 /* CF_REG is set if we're returning a value from the function */
498 if ((flags & CF_REG) == 0) {
503 /* How many bytes of locals do we have to drop? */
506 /* If we didn't have a variable argument list, don't call leave */
509 /* Load a function return code if needed */
510 if ((flags & CF_CONST) != 0) {
511 g_getimmed (flags, val, 0);
514 /* Drop stackframe or leave with rts */
517 AddCodeHint ("y:-"); /* Y register no longer used */
518 AddCodeSegLine (CS, "rts");
520 AddCodeHint ("y:-"); /* Y register no longer used */
521 AddCodeSegLine (CS, "jmp incsp%d", k);
525 AddCodeSegLine (CS, "jmp addysp");
530 strcpy (buf, "\tjmp\tleave");
532 /* We've a stack frame to drop */
536 /* Y register no longer used */
539 if (flags & CF_CONST) {
540 if ((flags & CF_TYPE) != CF_LONG) {
541 /* Constant int sized value given for return code */
543 /* Special case: return 0 */
545 } else if (((val >> 8) & 0xFF) == 0) {
546 /* Special case: constant with high byte zero */
547 ldaconst (val); /* Load low byte */
550 /* Others: arbitrary constant value */
551 g_getimmed (flags, val, 0); /* Load value */
554 /* Constant long value: No shortcut possible */
555 g_getimmed (flags, val, 0);
559 /* Output the jump */
566 /*****************************************************************************/
567 /* Register variables */
568 /*****************************************************************************/
572 void g_save_regvars (int RegOffs, unsigned Bytes)
573 /* Save register variables */
575 /* Don't loop for up to two bytes */
578 AddCodeSegLine (CS, "lda regbank%+d", RegOffs);
579 AddCodeSegLine (CS, "jsr pusha");
581 } else if (Bytes == 2) {
583 AddCodeSegLine (CS, "lda regbank%+d", RegOffs);
584 AddCodeSegLine (CS, "ldx regbank%+d", RegOffs+1);
585 AddCodeSegLine (CS, "jsr pushax");
589 /* More than two bytes - loop */
590 unsigned Label = GetLocalLabel ();
592 ldyconst (Bytes - 1);
594 g_defloclabel (Label);
595 AddCodeSegLine (CS, "lda regbank%+d,x", RegOffs-1);
596 AddCodeSegLine (CS, "sta (sp),y");
597 AddCodeSegLine (CS, "dey");
598 AddCodeSegLine (CS, "dex");
599 AddCodeSegLine (CS, "bne L%04X", Label);
603 /* We pushed stuff, correct the stack pointer */
609 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
610 /* Restore register variables */
612 /* Calculate the actual stack offset and check it */
614 CheckLocalOffs (StackOffs);
616 /* Don't loop for up to two bytes */
619 ldyconst (StackOffs);
620 AddCodeSegLine (CS, "lda (sp),y");
621 AddCodeSegLine (CS, "sta regbank%+d", RegOffs);
623 } else if (Bytes == 2) {
625 ldyconst (StackOffs);
626 AddCodeSegLine (CS, "lda (sp),y");
627 AddCodeSegLine (CS, "sta regbank%+d", RegOffs);
628 AddCodeSegLine (CS, "iny");
629 AddCodeSegLine (CS, "lda (sp),y");
630 AddCodeSegLine (CS, "sta regbank%+d", RegOffs+1);
634 /* More than two bytes - loop */
635 unsigned Label = GetLocalLabel ();
636 ldyconst (StackOffs+Bytes-1);
638 g_defloclabel (Label);
639 AddCodeSegLine (CS, "lda (sp),y");
640 AddCodeSegLine (CS, "sta regbank%+d,x", RegOffs-1);
641 AddCodeSegLine (CS, "dey");
642 AddCodeSegLine (CS, "dex");
643 AddCodeSegLine (CS, "bne L%04X", Label);
650 /*****************************************************************************/
651 /* Fetching memory cells */
652 /*****************************************************************************/
656 void g_getimmed (unsigned flags, unsigned long val, unsigned offs)
657 /* Load a constant into the primary register */
659 if ((flags & CF_CONST) != 0) {
661 /* Numeric constant */
662 switch (flags & CF_TYPE) {
665 if ((flags & CF_FORCECHAR) != 0) {
671 ldxconst ((val >> 8) & 0xFF);
672 ldaconst (val & 0xFF);
677 AddCodeSegLine (CS, "ldx #$00");
678 AddCodeSegLine (CS, "stx sreg+1");
679 AddCodeSegLine (CS, "stx sreg");
680 AddCodeSegLine (CS, "lda #$%02X", (unsigned char) val);
681 } else if ((val & 0xFFFF00FF) == 0) {
682 AddCodeSegLine (CS, "lda #$00");
683 AddCodeSegLine (CS, "sta sreg+1");
684 AddCodeSegLine (CS, "sta sreg");
685 AddCodeSegLine (CS, "ldx #$%02X", (unsigned char) (val >> 8));
686 } else if ((val & 0xFFFF0000) == 0 && CodeSizeFactor > 140) {
687 AddCodeSegLine (CS, "lda #$00");
688 AddCodeSegLine (CS, "sta sreg+1");
689 AddCodeSegLine (CS, "sta sreg");
690 AddCodeSegLine (CS, "lda #$%02X", (unsigned char) val);
691 AddCodeSegLine (CS, "ldx #$%02X", (unsigned char) (val >> 8));
692 } else if ((val & 0xFFFFFF00) == 0xFFFFFF00) {
693 AddCodeSegLine (CS, "ldx #$FF");
694 AddCodeSegLine (CS, "stx sreg+1");
695 AddCodeSegLine (CS, "stx sreg");
696 if ((val & 0xFF) == 0xFF) {
697 AddCodeSegLine (CS, "txa");
699 AddCodeSegLine (CS, "lda #$%02X", (unsigned char) val);
701 } else if ((val & 0xFFFF00FF) == 0xFFFF00FF) {
702 AddCodeSegLine (CS, "lda #$FF");
703 AddCodeSegLine (CS, "sta sreg+1");
704 AddCodeSegLine (CS, "sta sreg");
705 AddCodeSegLine (CS, "ldx #$%02X", (unsigned char) (val >> 8));
707 /* Call a subroutine that will load following value */
708 AddCodeSegLine (CS, "jsr ldeax");
709 AddCodeSegLine (CS, ".dword $%08lX", val & 0xFFFFFFFF);
721 /* Some sort of label */
722 const char* Label = GetLabelName (flags, val, offs);
724 /* Load the address into the primary */
725 AddCodeSegLine (CS, "lda #<(%s)", Label);
726 AddCodeSegLine (CS, "ldx #>(%s)", Label);
733 void g_getstatic (unsigned flags, unsigned long label, unsigned offs)
734 /* Fetch an static memory cell into the primary register */
736 /* Create the correct label name */
737 char* lbuf = GetLabelName (flags, label, offs);
739 /* Check the size and generate the correct load operation */
740 switch (flags & CF_TYPE) {
743 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
744 AddCodeSegLine (CS, "lda %s", lbuf); /* load A from the label */
747 AddCodeSegLine (CS, "lda %s", lbuf); /* load A from the label */
748 if (!(flags & CF_UNSIGNED)) {
749 /* Must sign extend */
750 AddCodeSegLine (CS, "bpl *+3");
751 AddCodeSegLine (CS, "dex");
752 AddCodeHint ("x:!"); /* X is invalid now */
758 AddCodeSegLine (CS, "lda %s", lbuf);
759 if (flags & CF_TEST) {
760 AddCodeSegLine (CS, "ora %s+1", lbuf);
762 AddCodeSegLine (CS, "ldx %s+1", lbuf);
767 if (flags & CF_TEST) {
768 AddCodeSegLine (CS, "lda %s+3", lbuf);
769 AddCodeSegLine (CS, "ora %s+2", lbuf);
770 AddCodeSegLine (CS, "ora %s+1", lbuf);
771 AddCodeSegLine (CS, "ora %s+0", lbuf);
773 AddCodeSegLine (CS, "lda %s+3", lbuf);
774 AddCodeSegLine (CS, "sta sreg+1");
775 AddCodeSegLine (CS, "lda %s+2", lbuf);
776 AddCodeSegLine (CS, "sta sreg");
777 AddCodeSegLine (CS, "ldx %s+1", lbuf);
778 AddCodeSegLine (CS, "lda %s", lbuf);
790 void g_getlocal (unsigned flags, int offs)
791 /* Fetch specified local object (local var). */
794 CheckLocalOffs (offs);
795 switch (flags & CF_TYPE) {
798 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
799 if (CPU == CPU_65C02 && offs == 0) {
800 AddCodeSegLine (CS, "lda (sp)");
803 AddCodeSegLine (CS, "lda (sp),y");
807 AddCodeSegLine (CS, "ldx #$00");
808 AddCodeSegLine (CS, "lda (sp,x)");
811 AddCodeSegLine (CS, "ldx #$00");
812 AddCodeSegLine (CS, "lda (sp),y");
814 if ((flags & CF_UNSIGNED) == 0) {
815 AddCodeSegLine (CS, "bpl *+3");
816 AddCodeSegLine (CS, "dex");
817 AddCodeHint ("x:!"); /* X is invalid now */
823 CheckLocalOffs (offs + 1);
824 if (flags & CF_TEST) {
826 AddCodeSegLine (CS, "lda (sp),y");
827 AddCodeSegLine (CS, "dey");
828 AddCodeSegLine (CS, "ora (sp),y");
830 if (CodeSizeFactor > 180) {
832 AddCodeSegLine (CS, "lda (sp),y");
833 AddCodeSegLine (CS, "tax");
834 AddCodeSegLine (CS, "dey");
835 AddCodeSegLine (CS, "lda (sp),y");
839 AddCodeSegLine (CS, "jsr ldaxysp");
841 AddCodeSegLine (CS, "jsr ldax0sp");
850 AddCodeSegLine (CS, "jsr ldeaxysp");
852 AddCodeSegLine (CS, "jsr ldeax0sp");
863 void g_getind (unsigned flags, unsigned offs)
864 /* Fetch the specified object type indirect through the primary register
865 * into the primary register
868 /* If the offset is greater than 255, add the part that is > 255 to
869 * the primary. This way we get an easy addition and use the low byte
872 offs = MakeByteOffs (flags, offs);
874 /* Handle the indirect fetch */
875 switch (flags & CF_TYPE) {
878 /* Character sized */
881 if (flags & CF_UNSIGNED) {
882 AddCodeSegLine (CS, "jsr ldauidx");
884 AddCodeSegLine (CS, "jsr ldaidx");
887 if (flags & CF_UNSIGNED) {
888 if (CodeSizeFactor > 250) {
889 AddCodeSegLine (CS, "sta ptr1");
890 AddCodeSegLine (CS, "stx ptr1+1");
891 AddCodeSegLine (CS, "ldx #$00");
892 AddCodeSegLine (CS, "lda (ptr1,x)");
894 AddCodeSegLine (CS, "jsr ldaui");
897 AddCodeSegLine (CS, "jsr ldai");
903 if (flags & CF_TEST) {
905 AddCodeSegLine (CS, "sta ptr1");
906 AddCodeSegLine (CS, "stx ptr1+1");
907 AddCodeSegLine (CS, "lda (ptr1),y");
908 AddCodeSegLine (CS, "iny");
909 AddCodeSegLine (CS, "ora (ptr1),y");
912 AddCodeSegLine (CS, "jsr ldaxi");
915 AddCodeSegLine (CS, "jsr ldaxidx");
922 AddCodeSegLine (CS, "jsr ldeaxi");
925 AddCodeSegLine (CS, "jsr ldeaxidx");
927 if (flags & CF_TEST) {
928 AddCodeSegLine (CS, "jsr tsteax");
940 void g_leasp (int offs)
941 /* Fetch the address of the specified symbol into the primary register */
943 /* Calculate the offset relative to sp */
946 /* For value 0 we do direct code */
948 AddCodeSegLine (CS, "lda sp");
949 AddCodeSegLine (CS, "ldx sp+1");
951 if (CodeSizeFactor < 300) {
952 ldaconst (offs); /* Load A with offset value */
953 AddCodeSegLine (CS, "jsr leaasp"); /* Load effective address */
955 if (CPU == CPU_65C02 && offs == 1) {
956 AddCodeSegLine (CS, "lda sp");
957 AddCodeSegLine (CS, "ldx sp+1");
958 AddCodeSegLine (CS, "ina");
959 AddCodeSegLine (CS, "bne *+3");
960 AddCodeSegLine (CS, "inx");
961 AddCodeHint ("x:!"); /* Invalidate X */
964 AddCodeSegLine (CS, "clc");
965 AddCodeSegLine (CS, "ldx sp+1");
966 AddCodeSegLine (CS, "adc sp");
967 AddCodeSegLine (CS, "bcc *+3");
968 AddCodeSegLine (CS, "inx");
969 AddCodeHint ("x:!"); /* Invalidate X */
977 void g_leavariadic (int Offs)
978 /* Fetch the address of a parameter in a variadic function into the primary
982 unsigned ArgSizeOffs;
984 /* Calculate the offset relative to sp */
987 /* Get the offset of the parameter which is stored at sp+0 on function
988 * entry and check if this offset is reachable with a byte offset.
991 ArgSizeOffs = -oursp;
992 CheckLocalOffs (ArgSizeOffs);
994 /* Get the size of all parameters. */
995 if (ArgSizeOffs == 0 && CPU == CPU_65C02) {
996 AddCodeSegLine (CS, "lda (sp)");
998 ldyconst (ArgSizeOffs);
999 AddCodeSegLine (CS, "lda (sp),y");
1002 /* Add the value of the stackpointer */
1003 if (CodeSizeFactor > 250) {
1004 AddCodeSegLine (CS, "ldx sp+1");
1005 AddCodeSegLine (CS, "clc");
1006 AddCodeSegLine (CS, "adc sp");
1007 AddCodeSegLine (CS, "bcc *+3");
1008 AddCodeSegLine (CS, "inx");
1009 AddCodeHint ("x:!"); /* Invalidate X */
1011 AddCodeSegLine (CS, "jsr leaasp");
1014 /* Add the offset to the primary */
1016 g_inc (CF_INT | CF_CONST, Offs);
1017 } else if (Offs < 0) {
1018 g_dec (CF_INT | CF_CONST, -Offs);
1024 /*****************************************************************************/
1025 /* Store into memory */
1026 /*****************************************************************************/
1030 void g_putstatic (unsigned flags, unsigned long label, unsigned offs)
1031 /* Store the primary register into the specified static memory cell */
1033 /* Create the correct label name */
1034 char* lbuf = GetLabelName (flags, label, offs);
1036 /* Check the size and generate the correct store operation */
1037 switch (flags & CF_TYPE) {
1040 AddCodeSegLine (CS, "sta %s", lbuf);
1044 AddCodeSegLine (CS, "sta %s", lbuf);
1045 AddCodeSegLine (CS, "stx %s+1", lbuf);
1049 AddCodeSegLine (CS, "sta %s", lbuf);
1050 AddCodeSegLine (CS, "stx %s+1", lbuf);
1051 AddCodeSegLine (CS, "ldy sreg");
1052 AddCodeSegLine (CS, "sty %s+2", lbuf);
1053 AddCodeSegLine (CS, "ldy sreg+1");
1054 AddCodeSegLine (CS, "sty %s+3", lbuf);
1065 void g_putlocal (unsigned Flags, int Offs, long Val)
1066 /* Put data into local object. */
1069 CheckLocalOffs (Offs);
1070 switch (Flags & CF_TYPE) {
1073 if (Flags & CF_CONST) {
1074 AddCodeSegLine (CS, "lda #$%02X", (unsigned char) Val);
1076 if (CPU == CPU_65C02 && Offs == 0) {
1077 AddCodeSegLine (CS, "sta (sp)");
1080 AddCodeSegLine (CS, "sta (sp),y");
1085 if (Flags & CF_CONST) {
1087 AddCodeSegLine (CS, "lda #$%02X", (unsigned char) (Val >> 8));
1088 AddCodeSegLine (CS, "sta (sp),y");
1089 if ((Flags & CF_NOKEEP) == 0) {
1090 /* Place high byte into X */
1091 AddCodeSegLine (CS, "tax");
1093 if (CPU == CPU_65C02 && Offs == 0) {
1094 AddCodeSegLine (CS, "lda #$%02X", (unsigned char) Val);
1095 AddCodeSegLine (CS, "sta (sp)");
1097 if ((Val & 0xFF) == Offs+1) {
1098 /* The value we need is already in Y */
1099 AddCodeSegLine (CS, "tya");
1100 AddCodeSegLine (CS, "dey");
1102 AddCodeSegLine (CS, "dey");
1103 AddCodeSegLine (CS, "lda #$%02X", (unsigned char) Val);
1105 AddCodeSegLine (CS, "sta (sp),y");
1108 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
1111 AddCodeSegLine (CS, "jsr staxysp");
1113 AddCodeSegLine (CS, "jsr stax0sp");
1116 if (CPU == CPU_65C02 && Offs == 0) {
1117 AddCodeSegLine (CS, "sta (sp)");
1119 AddCodeSegLine (CS, "txa");
1120 AddCodeSegLine (CS, "sta (sp),y");
1123 AddCodeSegLine (CS, "sta (sp),y");
1124 AddCodeSegLine (CS, "iny");
1125 AddCodeSegLine (CS, "txa");
1126 AddCodeSegLine (CS, "sta (sp),y");
1133 if (Flags & CF_CONST) {
1134 g_getimmed (Flags, Val, 0);
1138 AddCodeSegLine (CS, "jsr steaxysp");
1140 AddCodeSegLine (CS, "jsr steax0sp");
1152 void g_putind (unsigned Flags, unsigned Offs)
1153 /* Store the specified object type in the primary register at the address
1154 * on the top of the stack
1157 /* We can handle offsets below $100 directly, larger offsets must be added
1158 * to the address. Since a/x is in use, best code is achieved by adding
1159 * just the high byte. Be sure to check if the low byte will overflow while
1162 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1164 /* Overflow - we need to add the low byte also */
1165 AddCodeSegLine (CS, "ldy #$00");
1166 AddCodeSegLine (CS, "clc");
1167 AddCodeSegLine (CS, "pha");
1168 AddCodeSegLine (CS, "lda #$%02X", Offs & 0xFF);
1169 AddCodeSegLine (CS, "adc (sp),y");
1170 AddCodeSegLine (CS, "sta (sp),y");
1171 AddCodeSegLine (CS, "iny");
1172 AddCodeSegLine (CS, "lda #$%02X", (Offs >> 8) & 0xFF);
1173 AddCodeSegLine (CS, "adc (sp),y");
1174 AddCodeSegLine (CS, "sta (sp),y");
1175 AddCodeSegLine (CS, "pla");
1177 /* Complete address is on stack, new offset is zero */
1180 } else if ((Offs & 0xFF00) != 0) {
1182 /* We can just add the high byte */
1183 AddCodeSegLine (CS, "ldy #$01");
1184 AddCodeSegLine (CS, "clc");
1185 AddCodeSegLine (CS, "pha");
1186 AddCodeSegLine (CS, "lda #$%02X", (Offs >> 8) & 0xFF);
1187 AddCodeSegLine (CS, "adc (sp),y");
1188 AddCodeSegLine (CS, "sta (sp),y");
1189 AddCodeSegLine (CS, "pla");
1191 /* Offset is now just the low byte */
1195 /* Check the size and determine operation */
1196 switch (Flags & CF_TYPE) {
1201 AddCodeSegLine (CS, "jsr staspidx");
1203 AddCodeSegLine (CS, "jsr staspp");
1210 AddCodeSegLine (CS, "jsr staxspidx");
1212 AddCodeSegLine (CS, "jsr staxspp");
1219 AddCodeSegLine (CS, "jsr steaxspidx");
1221 AddCodeSegLine (CS, "jsr steaxspp");
1230 /* Pop the argument which is always a pointer */
1236 /*****************************************************************************/
1237 /* type conversion and similiar stuff */
1238 /*****************************************************************************/
1242 void g_toslong (unsigned flags)
1243 /* Make sure, the value on TOS is a long. Convert if necessary */
1245 switch (flags & CF_TYPE) {
1249 if (flags & CF_UNSIGNED) {
1250 AddCodeSegLine (CS, "jsr tosulong");
1252 AddCodeSegLine (CS, "jsr toslong");
1267 void g_tosint (unsigned flags)
1268 /* Make sure, the value on TOS is an int. Convert if necessary */
1270 switch (flags & CF_TYPE) {
1277 AddCodeSegLine (CS, "jsr tosint");
1288 void g_reglong (unsigned flags)
1289 /* Make sure, the value in the primary register a long. Convert if necessary */
1291 switch (flags & CF_TYPE) {
1295 if (flags & CF_UNSIGNED) {
1296 if (CodeSizeFactor >= 200) {
1298 AddCodeSegLine (CS, "sty sreg");
1299 AddCodeSegLine (CS, "sty sreg+1");
1301 AddCodeSegLine (CS, "jsr axulong");
1304 AddCodeSegLine (CS, "jsr axlong");
1318 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1319 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1320 * value, that corresponds to the value on TOS, rhs corresponds to the value
1321 * in (e)ax. The return value is the the flags value for the resulting type.
1324 unsigned ltype, rtype;
1327 /* Get the type spec from the flags */
1328 ltype = lhs & CF_TYPE;
1329 rtype = rhs & CF_TYPE;
1331 /* Check if a conversion is needed */
1332 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1333 /* We must promote the primary register to long */
1335 /* Get the new rhs type */
1336 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1338 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1339 /* We must promote the lhs to long */
1345 /* Get the new rhs type */
1346 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1350 /* Determine the result type for the operation:
1351 * - The result is const if both operands are const.
1352 * - The result is unsigned if one of the operands is unsigned.
1353 * - The result is long if one of the operands is long.
1354 * - Otherwise the result is int sized.
1356 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1357 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1358 if (rtype == CF_LONG || ltype == CF_LONG) {
1368 unsigned g_typecast (unsigned lhs, unsigned rhs)
1369 /* Cast the value in the primary register to the operand size that is flagged
1370 * by the lhs value. Return the result value.
1373 unsigned ltype, rtype;
1375 /* Get the type spec from the flags */
1376 ltype = lhs & CF_TYPE;
1377 rtype = rhs & CF_TYPE;
1379 /* Check if a conversion is needed */
1380 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1381 /* We must promote the primary register to long */
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 <= 3) {
1419 /* Factor is 2, 4 or 8, use special function */
1420 switch (flags & CF_TYPE) {
1423 if (flags & CF_FORCECHAR) {
1425 AddCodeSegLine (CS, "asl a");
1432 if (CodeSizeFactor >= (p2+1)*130U) {
1433 AddCodeSegLine (CS, "stx tmp1");
1435 AddCodeSegLine (CS, "asl a");
1436 AddCodeSegLine (CS, "rol tmp1");
1438 AddCodeSegLine (CS, "ldx tmp1");
1440 if (flags & CF_UNSIGNED) {
1441 AddCodeSegLine (CS, "jsr shlax%d", p2);
1443 AddCodeSegLine (CS, "jsr aslax%d", p2);
1449 if (flags & CF_UNSIGNED) {
1450 AddCodeSegLine (CS, "jsr shleax%d", p2);
1452 AddCodeSegLine (CS, "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 <= 3) {
1474 /* Factor is 2, 4 or 8, use special function */
1475 switch (flags & CF_TYPE) {
1478 if (flags & CF_FORCECHAR) {
1479 if (flags & CF_UNSIGNED) {
1481 AddCodeSegLine (CS, "lsr a");
1484 } else if (p2 <= 2) {
1485 AddCodeSegLine (CS, "cmp #$80");
1486 AddCodeSegLine (CS, "ror a");
1493 if (flags & CF_UNSIGNED) {
1494 if (CodeSizeFactor >= (p2+1)*130U) {
1495 AddCodeSegLine (CS, "stx tmp1");
1497 AddCodeSegLine (CS, "lsr tmp1");
1498 AddCodeSegLine (CS, "ror a");
1500 AddCodeSegLine (CS, "ldx tmp1");
1502 AddCodeSegLine (CS, "jsr lsrax%d", p2);
1505 if (CodeSizeFactor >= (p2+1)*150U) {
1506 AddCodeSegLine (CS, "stx tmp1");
1508 AddCodeSegLine (CS, "cpx #$80");
1509 AddCodeSegLine (CS, "ror tmp1");
1510 AddCodeSegLine (CS, "ror a");
1512 AddCodeSegLine (CS, "ldx tmp1");
1514 AddCodeSegLine (CS, "jsr asrax%d", p2);
1520 if (flags & CF_UNSIGNED) {
1521 AddCodeSegLine (CS, "jsr lsreax%d", p2);
1523 AddCodeSegLine (CS, "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 */
1552 /* Correct the offset and check it */
1554 CheckLocalOffs (offs);
1556 switch (flags & CF_TYPE) {
1559 AddCodeSegLine (CS, "ldy #$%02X", offs & 0xFF);
1560 AddCodeSegLine (CS, "clc");
1561 AddCodeSegLine (CS, "adc (sp),y");
1562 AddCodeSegLine (CS, "bcc *+3");
1563 AddCodeSegLine (CS, "inx");
1564 AddCodeHint ("x:!");
1568 AddCodeSegLine (CS, "ldy #$%02X", offs & 0xFF);
1569 AddCodeSegLine (CS, "clc");
1570 AddCodeSegLine (CS, "adc (sp),y");
1571 AddCodeSegLine (CS, "pha");
1572 AddCodeSegLine (CS, "txa");
1573 AddCodeSegLine (CS, "iny");
1574 AddCodeSegLine (CS, "adc (sp),y");
1575 AddCodeSegLine (CS, "tax");
1576 AddCodeSegLine (CS, "pla");
1580 /* Do it the old way */
1582 g_getlocal (flags, offs);
1594 void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
1595 /* Add a static variable to ax */
1597 /* Create the correct label name */
1598 char* lbuf = GetLabelName (flags, label, offs);
1600 switch (flags & CF_TYPE) {
1603 AddCodeSegLine (CS, "clc");
1604 AddCodeSegLine (CS, "adc %s", lbuf);
1605 AddCodeSegLine (CS, "bcc *+3");
1606 AddCodeSegLine (CS, "inx");
1607 AddCodeHint ("x:!");
1611 AddCodeSegLine (CS, "clc");
1612 AddCodeSegLine (CS, "adc %s", lbuf);
1613 AddCodeSegLine (CS, "tay");
1614 AddCodeSegLine (CS, "txa");
1615 AddCodeSegLine (CS, "adc %s+1", lbuf);
1616 AddCodeSegLine (CS, "tax");
1617 AddCodeSegLine (CS, "tya");
1621 /* Do it the old way */
1623 g_getstatic (flags, label, offs);
1635 /*****************************************************************************/
1636 /* Compares of ax with a variable with fixed address */
1637 /*****************************************************************************/
1641 void g_cmplocal (unsigned flags, int offs)
1642 /* Compare a local variable to ax */
1644 Internal ("g_cmplocal not implemented");
1649 void g_cmpstatic (unsigned flags, unsigned label, unsigned offs)
1650 /* Compare a static variable to ax */
1652 Internal ("g_cmpstatic not implemented");
1657 /*****************************************************************************/
1658 /* Special op= functions */
1659 /*****************************************************************************/
1663 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
1665 /* Emit += for a static variable */
1667 /* Create the correct label name */
1668 char* lbuf = GetLabelName (flags, label, offs);
1670 /* Check the size and determine operation */
1671 switch (flags & CF_TYPE) {
1674 if (flags & CF_FORCECHAR) {
1675 AddCodeSegLine (CS, "ldx #$00");
1676 if (flags & CF_CONST) {
1678 AddCodeSegLine (CS, "inc %s", lbuf);
1679 AddCodeSegLine (CS, "lda %s", lbuf);
1681 AddCodeSegLine (CS, "lda #$%02X", (int)(val & 0xFF));
1682 AddCodeSegLine (CS, "clc");
1683 AddCodeSegLine (CS, "adc %s", lbuf);
1684 AddCodeSegLine (CS, "sta %s", lbuf);
1687 AddCodeSegLine (CS, "clc");
1688 AddCodeSegLine (CS, "adc %s", lbuf);
1689 AddCodeSegLine (CS, "sta %s", lbuf);
1691 if ((flags & CF_UNSIGNED) == 0) {
1692 AddCodeSegLine (CS, "bpl *+3");
1693 AddCodeSegLine (CS, "dex");
1694 AddCodeHint ("x:!"); /* Invalidate X */
1701 if (flags & CF_CONST) {
1703 label = GetLocalLabel ();
1704 AddCodeSegLine (CS, "inc %s", lbuf);
1705 AddCodeSegLine (CS, "bne L%04X", (int)label);
1706 AddCodeSegLine (CS, "inc %s+1", lbuf);
1707 g_defloclabel (label);
1708 AddCodeSegLine (CS, "lda %s", lbuf); /* Hmmm... */
1709 AddCodeSegLine (CS, "ldx %s+1", lbuf);
1711 AddCodeSegLine (CS, "lda #$%02X", (int)(val & 0xFF));
1712 AddCodeSegLine (CS, "clc");
1713 AddCodeSegLine (CS, "adc %s", lbuf);
1714 AddCodeSegLine (CS, "sta %s", lbuf);
1716 label = GetLocalLabel ();
1717 AddCodeSegLine (CS, "bcc L%04X", (int)label);
1718 AddCodeSegLine (CS, "inc %s+1", lbuf);
1719 g_defloclabel (label);
1720 AddCodeSegLine (CS, "ldx %s+1", lbuf);
1722 AddCodeSegLine (CS, "lda #$%02X", (unsigned char)(val >> 8));
1723 AddCodeSegLine (CS, "adc %s+1", lbuf);
1724 AddCodeSegLine (CS, "sta %s+1", lbuf);
1725 AddCodeSegLine (CS, "tax");
1726 AddCodeSegLine (CS, "lda %s", lbuf);
1730 AddCodeSegLine (CS, "clc");
1731 AddCodeSegLine (CS, "adc %s", lbuf);
1732 AddCodeSegLine (CS, "sta %s", lbuf);
1733 AddCodeSegLine (CS, "txa");
1734 AddCodeSegLine (CS, "adc %s+1", lbuf);
1735 AddCodeSegLine (CS, "sta %s+1", lbuf);
1736 AddCodeSegLine (CS, "tax");
1737 AddCodeSegLine (CS, "lda %s", lbuf);
1742 if (flags & CF_CONST) {
1744 AddCodeSegLine (CS, "ldy #<(%s)", lbuf);
1745 AddCodeSegLine (CS, "sty ptr1");
1746 AddCodeSegLine (CS, "ldy #>(%s+1)", lbuf);
1748 AddCodeSegLine (CS, "jsr laddeq1");
1750 AddCodeSegLine (CS, "lda #$%02X", (int)(val & 0xFF));
1751 AddCodeSegLine (CS, "jsr laddeqa");
1754 g_getstatic (flags, label, offs);
1756 g_putstatic (flags, label, offs);
1759 AddCodeSegLine (CS, "ldy #<(%s)", lbuf);
1760 AddCodeSegLine (CS, "sty ptr1");
1761 AddCodeSegLine (CS, "ldy #>(%s+1)", lbuf);
1762 AddCodeSegLine (CS, "jsr laddeq");
1773 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1774 /* Emit += for a local variable */
1776 /* Calculate the true offset, check it, load it into Y */
1778 CheckLocalOffs (offs);
1780 /* Check the size and determine operation */
1781 switch (flags & CF_TYPE) {
1784 if (flags & CF_FORCECHAR) {
1786 AddCodeSegLine (CS, "ldx #$00");
1787 if (flags & CF_CONST) {
1788 AddCodeSegLine (CS, "clc");
1789 AddCodeSegLine (CS, "lda #$%02X", (int)(val & 0xFF));
1790 AddCodeSegLine (CS, "adc (sp,x)");
1791 AddCodeSegLine (CS, "sta (sp,x)");
1793 AddCodeSegLine (CS, "clc");
1794 AddCodeSegLine (CS, "adc (sp,x)");
1795 AddCodeSegLine (CS, "sta (sp,x)");
1799 AddCodeSegLine (CS, "ldx #$00");
1800 if (flags & CF_CONST) {
1801 AddCodeSegLine (CS, "clc");
1802 AddCodeSegLine (CS, "lda #$%02X", (int)(val & 0xFF));
1803 AddCodeSegLine (CS, "adc (sp),y");
1804 AddCodeSegLine (CS, "sta (sp),y");
1806 AddCodeSegLine (CS, "clc");
1807 AddCodeSegLine (CS, "adc (sp),y");
1808 AddCodeSegLine (CS, "sta (sp),y");
1811 if ((flags & CF_UNSIGNED) == 0) {
1812 AddCodeSegLine (CS, "bpl *+3");
1813 AddCodeSegLine (CS, "dex");
1814 AddCodeHint ("x:!"); /* Invalidate X */
1821 if (flags & CF_CONST) {
1822 g_getimmed (flags, val, 0);
1825 AddCodeSegLine (CS, "jsr addeq0sp");
1828 AddCodeSegLine (CS, "jsr addeqysp");
1833 if (flags & CF_CONST) {
1834 g_getimmed (flags, val, 0);
1837 AddCodeSegLine (CS, "jsr laddeq0sp");
1840 AddCodeSegLine (CS, "jsr laddeqysp");
1851 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1852 /* Emit += for the location with address in ax */
1854 /* If the offset is too large for a byte register, add the high byte
1855 * of the offset to the primary. Beware: We need a special correction
1856 * if the offset in the low byte will overflow in the operation.
1858 offs = MakeByteOffs (flags, offs);
1860 /* Check the size and determine operation */
1861 switch (flags & CF_TYPE) {
1864 AddCodeSegLine (CS, "sta ptr1");
1865 AddCodeSegLine (CS, "stx ptr1+1");
1867 AddCodeSegLine (CS, "ldx #$00");
1868 AddCodeSegLine (CS, "lda #$%02X", (int)(val & 0xFF));
1869 AddCodeSegLine (CS, "clc");
1870 AddCodeSegLine (CS, "adc (ptr1,x)");
1871 AddCodeSegLine (CS, "sta (ptr1,x)");
1873 AddCodeSegLine (CS, "ldy #$%02X", offs);
1874 AddCodeSegLine (CS, "ldx #$00");
1875 AddCodeSegLine (CS, "lda #$%02X", (int)(val & 0xFF));
1876 AddCodeSegLine (CS, "clc");
1877 AddCodeSegLine (CS, "adc (ptr1),y");
1878 AddCodeSegLine (CS, "sta (ptr1),y");
1883 if (CodeSizeFactor >= 200) {
1884 /* Lots of code, use only if size is not important */
1885 AddCodeSegLine (CS, "sta ptr1");
1886 AddCodeSegLine (CS, "stx ptr1+1");
1887 AddCodeSegLine (CS, "ldy #$%02X", offs);
1888 AddCodeSegLine (CS, "lda #$%02X", (int)(val & 0xFF));
1889 AddCodeSegLine (CS, "clc");
1890 AddCodeSegLine (CS, "adc (ptr1),y");
1891 AddCodeSegLine (CS, "sta (ptr1),y");
1892 AddCodeSegLine (CS, "pha");
1893 AddCodeSegLine (CS, "iny");
1894 AddCodeSegLine (CS, "lda #$%02X", (unsigned char)(val >> 8));
1895 AddCodeSegLine (CS, "adc (ptr1),y");
1896 AddCodeSegLine (CS, "sta (ptr1),y");
1897 AddCodeSegLine (CS, "tax");
1898 AddCodeSegLine (CS, "pla");
1904 AddCodeSegLine (CS, "jsr pushax"); /* Push the address */
1905 push (flags); /* Correct the internal sp */
1906 g_getind (flags, offs); /* Fetch the value */
1907 g_inc (flags, val); /* Increment value in primary */
1908 g_putind (flags, offs); /* Store the value back */
1918 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1920 /* Emit -= for a static variable */
1922 /* Create the correct label name */
1923 char* lbuf = GetLabelName (flags, label, offs);
1925 /* Check the size and determine operation */
1926 switch (flags & CF_TYPE) {
1929 if (flags & CF_FORCECHAR) {
1930 AddCodeSegLine (CS, "ldx #$00");
1931 if (flags & CF_CONST) {
1933 AddCodeSegLine (CS, "dec %s", lbuf);
1934 AddCodeSegLine (CS, "lda %s", lbuf);
1936 AddCodeSegLine (CS, "sec");
1937 AddCodeSegLine (CS, "lda %s", lbuf);
1938 AddCodeSegLine (CS, "sbc #$%02X", (int)(val & 0xFF));
1939 AddCodeSegLine (CS, "sta %s", lbuf);
1942 AddCodeSegLine (CS, "sec");
1943 AddCodeSegLine (CS, "sta tmp1");
1944 AddCodeSegLine (CS, "lda %s", lbuf);
1945 AddCodeSegLine (CS, "sbc tmp1");
1946 AddCodeSegLine (CS, "sta %s", lbuf);
1948 if ((flags & CF_UNSIGNED) == 0) {
1949 AddCodeSegLine (CS, "bpl *+3");
1950 AddCodeSegLine (CS, "dex");
1951 AddCodeHint ("x:!"); /* Invalidate X */
1958 AddCodeSegLine (CS, "sec");
1959 if (flags & CF_CONST) {
1960 AddCodeSegLine (CS, "lda %s", lbuf);
1961 AddCodeSegLine (CS, "sbc #$%02X", (unsigned char)val);
1962 AddCodeSegLine (CS, "sta %s", lbuf);
1964 label = GetLocalLabel ();
1965 AddCodeSegLine (CS, "bcs L%04X", (unsigned)label);
1966 AddCodeSegLine (CS, "dec %s+1", lbuf);
1967 g_defloclabel (label);
1968 AddCodeSegLine (CS, "ldx %s+1", lbuf);
1970 AddCodeSegLine (CS, "lda %s+1", lbuf);
1971 AddCodeSegLine (CS, "sbc #$%02X", (unsigned char)(val >> 8));
1972 AddCodeSegLine (CS, "sta %s+1", lbuf);
1973 AddCodeSegLine (CS, "tax");
1974 AddCodeSegLine (CS, "lda %s", lbuf);
1977 AddCodeSegLine (CS, "sta tmp1");
1978 AddCodeSegLine (CS, "lda %s", lbuf);
1979 AddCodeSegLine (CS, "sbc tmp1");
1980 AddCodeSegLine (CS, "sta %s", lbuf);
1981 AddCodeSegLine (CS, "stx tmp1");
1982 AddCodeSegLine (CS, "lda %s+1", lbuf);
1983 AddCodeSegLine (CS, "sbc tmp1");
1984 AddCodeSegLine (CS, "sta %s+1", lbuf);
1985 AddCodeSegLine (CS, "tax");
1986 AddCodeSegLine (CS, "lda %s", lbuf);
1991 if (flags & CF_CONST) {
1993 AddCodeSegLine (CS, "ldy #<(%s)", lbuf);
1994 AddCodeSegLine (CS, "sty ptr1");
1995 AddCodeSegLine (CS, "ldy #>(%s+1)", lbuf);
1997 AddCodeSegLine (CS, "jsr lsubeq1");
1999 AddCodeSegLine (CS, "lda #$%02X", (unsigned char)val);
2000 AddCodeSegLine (CS, "jsr lsubeqa");
2003 g_getstatic (flags, label, offs);
2005 g_putstatic (flags, label, offs);
2008 AddCodeSegLine (CS, "ldy #<(%s)", lbuf);
2009 AddCodeSegLine (CS, "sty ptr1");
2010 AddCodeSegLine (CS, "ldy #>(%s+1)", lbuf);
2011 AddCodeSegLine (CS, "jsr lsubeq");
2022 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
2023 /* Emit -= for a local variable */
2025 /* Calculate the true offset, check it, load it into Y */
2027 CheckLocalOffs (offs);
2029 /* Check the size and determine operation */
2030 switch (flags & CF_TYPE) {
2033 if (flags & CF_FORCECHAR) {
2035 AddCodeSegLine (CS, "ldx #$00");
2036 AddCodeSegLine (CS, "sec");
2037 if (flags & CF_CONST) {
2038 AddCodeSegLine (CS, "lda (sp),y");
2039 AddCodeSegLine (CS, "sbc #$%02X", (unsigned char)val);
2041 AddCodeSegLine (CS, "sta tmp1");
2042 AddCodeSegLine (CS, "lda (sp),y");
2043 AddCodeSegLine (CS, "sbc tmp1");
2045 AddCodeSegLine (CS, "sta (sp),y");
2046 if ((flags & CF_UNSIGNED) == 0) {
2047 AddCodeSegLine (CS, "bpl *+3");
2048 AddCodeSegLine (CS, "dex");
2049 AddCodeHint ("x:!"); /* Invalidate X */
2056 if (flags & CF_CONST) {
2057 g_getimmed (flags, val, 0);
2060 AddCodeSegLine (CS, "jsr subeq0sp");
2063 AddCodeSegLine (CS, "jsr subeqysp");
2068 if (flags & CF_CONST) {
2069 g_getimmed (flags, val, 0);
2072 AddCodeSegLine (CS, "jsr lsubeq0sp");
2075 AddCodeSegLine (CS, "jsr lsubeqysp");
2086 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2087 /* Emit -= for the location with address in ax */
2089 /* If the offset is too large for a byte register, add the high byte
2090 * of the offset to the primary. Beware: We need a special correction
2091 * if the offset in the low byte will overflow in the operation.
2093 offs = MakeByteOffs (flags, offs);
2095 /* Check the size and determine operation */
2096 switch (flags & CF_TYPE) {
2099 AddCodeSegLine (CS, "sta ptr1");
2100 AddCodeSegLine (CS, "stx ptr1+1");
2102 AddCodeSegLine (CS, "ldx #$00");
2103 AddCodeSegLine (CS, "lda (ptr1,x)");
2104 AddCodeSegLine (CS, "sec");
2105 AddCodeSegLine (CS, "sbc #$%02X", (unsigned char)val);
2106 AddCodeSegLine (CS, "sta (ptr1,x)");
2108 AddCodeSegLine (CS, "ldy #$%02X", offs);
2109 AddCodeSegLine (CS, "ldx #$00");
2110 AddCodeSegLine (CS, "lda (ptr1),y");
2111 AddCodeSegLine (CS, "sec");
2112 AddCodeSegLine (CS, "sbc #$%02X", (unsigned char)val);
2113 AddCodeSegLine (CS, "sta (ptr1),y");
2118 if (CodeSizeFactor >= 200) {
2119 /* Lots of code, use only if size is not important */
2120 AddCodeSegLine (CS, "sta ptr1");
2121 AddCodeSegLine (CS, "stx ptr1+1");
2122 AddCodeSegLine (CS, "ldy #$%02X", offs);
2123 AddCodeSegLine (CS, "lda (ptr1),y");
2124 AddCodeSegLine (CS, "sec");
2125 AddCodeSegLine (CS, "sbc #$%02X", (unsigned char)val);
2126 AddCodeSegLine (CS, "sta (ptr1),y");
2127 AddCodeSegLine (CS, "pha");
2128 AddCodeSegLine (CS, "iny");
2129 AddCodeSegLine (CS, "lda (ptr1),y");
2130 AddCodeSegLine (CS, "sbc #$%02X", (unsigned char)(val >> 8));
2131 AddCodeSegLine (CS, "sta (ptr1),y");
2132 AddCodeSegLine (CS, "tax");
2133 AddCodeSegLine (CS, "pla");
2139 AddCodeSegLine (CS, "jsr pushax"); /* Push the address */
2140 push (flags); /* Correct the internal sp */
2141 g_getind (flags, offs); /* Fetch the value */
2142 g_dec (flags, val); /* Increment value in primary */
2143 g_putind (flags, offs); /* Store the value back */
2153 /*****************************************************************************/
2154 /* Add a variable address to the value in ax */
2155 /*****************************************************************************/
2159 void g_addaddr_local (unsigned flags, int offs)
2160 /* Add the address of a local variable to ax */
2162 /* Add the offset */
2165 /* We cannot address more then 256 bytes of locals anyway */
2166 CheckLocalOffs (offs);
2167 AddCodeSegLine (CS, "clc");
2168 AddCodeSegLine (CS, "adc #$%02X", offs & 0xFF);
2169 AddCodeSegLine (CS, "bcc *+4"); /* Do also skip the CLC insn below */
2170 AddCodeSegLine (CS, "inx");
2171 AddCodeHint ("x:!"); /* Invalidate X */
2174 /* Add the current stackpointer value */
2175 AddCodeSegLine (CS, "clc");
2176 AddCodeSegLine (CS, "adc sp");
2177 AddCodeSegLine (CS, "tay");
2178 AddCodeSegLine (CS, "txa");
2179 AddCodeSegLine (CS, "adc sp+1");
2180 AddCodeSegLine (CS, "tax");
2181 AddCodeSegLine (CS, "tya");
2186 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2187 /* Add the address of a static variable to ax */
2189 /* Create the correct label name */
2190 char* lbuf = GetLabelName (flags, label, offs);
2192 /* Add the address to the current ax value */
2193 AddCodeSegLine (CS, "clc");
2194 AddCodeSegLine (CS, "adc #<(%s)", lbuf);
2195 AddCodeSegLine (CS, "tay");
2196 AddCodeSegLine (CS, "txa");
2197 AddCodeSegLine (CS, "adc #>(%s)", lbuf);
2198 AddCodeSegLine (CS, "tax");
2199 AddCodeSegLine (CS, "tya");
2204 /*****************************************************************************/
2206 /*****************************************************************************/
2210 void g_save (unsigned flags)
2211 /* Copy primary register to hold register. */
2213 /* Check the size and determine operation */
2214 switch (flags & CF_TYPE) {
2217 if (flags & CF_FORCECHAR) {
2218 AddCodeSegLine (CS, "pha");
2224 AddCodeSegLine (CS, "sta regsave");
2225 AddCodeSegLine (CS, "stx regsave+1");
2229 AddCodeSegLine (CS, "jsr saveeax");
2239 void g_restore (unsigned flags)
2240 /* Copy hold register to P. */
2242 /* Check the size and determine operation */
2243 switch (flags & CF_TYPE) {
2246 if (flags & CF_FORCECHAR) {
2247 AddCodeSegLine (CS, "pla");
2253 AddCodeSegLine (CS, "lda regsave");
2254 AddCodeSegLine (CS, "ldx regsave+1");
2258 AddCodeSegLine (CS, "jsr resteax");
2268 void g_cmp (unsigned flags, unsigned long val)
2269 /* Immidiate compare. The primary register will not be changed, Z flag
2273 /* Check the size and determine operation */
2274 switch (flags & CF_TYPE) {
2277 if (flags & CF_FORCECHAR) {
2278 AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
2284 AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
2285 AddCodeSegLine (CS, "bne *+4");
2286 AddCodeSegLine (CS, "cpx #$%02X", (unsigned char)(val >> 8));
2290 Internal ("g_cmp: Long compares not implemented");
2300 static void oper (unsigned flags, unsigned long val, char** subs)
2301 /* Encode a binary operation. subs is a pointer to four groups of three
2303 * 0-2 --> Operate on ints
2304 * 3-5 --> Operate on unsigneds
2305 * 6-8 --> Operate on longs
2306 * 9-11 --> Operate on unsigned longs
2308 * The first subroutine names in each string group is used to encode an
2309 * operation with a zero constant, the second to encode an operation with
2310 * a 8 bit constant, and the third is used in all other cases.
2315 /* Determine the offset into the array */
2316 offs = (flags & CF_UNSIGNED)? 3 : 0;
2317 switch (flags & CF_TYPE) {
2330 /* Encode the operation */
2331 if (flags & CF_CONST) {
2332 /* Constant value given */
2333 if (val == 0 && subs [offs+0]) {
2334 /* Special case: constant with value zero */
2335 AddCodeSegLine (CS, "jsr %s", subs [offs+0]);
2336 } else if (val < 0x100 && subs [offs+1]) {
2337 /* Special case: constant with high byte zero */
2338 ldaconst (val); /* Load low byte */
2339 AddCodeSegLine (CS, "jsr %s", subs [offs+1]);
2341 /* Others: arbitrary constant value */
2342 g_getimmed (flags, val, 0); /* Load value */
2343 AddCodeSegLine (CS, "jsr %s", subs [offs+2]);
2346 /* Value not constant (is already in (e)ax) */
2347 AddCodeSegLine (CS, "jsr %s", subs [offs+2]);
2350 /* The operation will pop it's argument */
2356 void g_test (unsigned flags)
2357 /* Force a test to set cond codes right */
2359 switch (flags & CF_TYPE) {
2362 if (flags & CF_FORCECHAR) {
2363 AddCodeSegLine (CS, "tax");
2369 AddCodeSegLine (CS, "stx tmp1");
2370 AddCodeSegLine (CS, "ora tmp1");
2374 if (flags & CF_UNSIGNED) {
2375 AddCodeSegLine (CS, "jsr utsteax");
2377 AddCodeSegLine (CS, "jsr tsteax");
2389 void g_push (unsigned flags, unsigned long val)
2390 /* Push the primary register or a constant value onto the stack */
2394 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2396 /* We have a constant 8 or 16 bit value */
2397 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2399 /* Handle as 8 bit value */
2400 if (CodeSizeFactor >= 165 || val > 2) {
2402 AddCodeSegLine (CS, "jsr pusha");
2404 AddCodeSegLine (CS, "jsr pushc%d", (int) val);
2409 /* Handle as 16 bit value */
2410 hi = (unsigned char) (val >> 8);
2412 AddCodeSegLine (CS, "jsr push%u", (unsigned) val);
2413 } else if (hi == 0 || hi == 0xFF) {
2414 /* Use special function */
2416 AddCodeSegLine (CS, "jsr %s", (hi == 0)? "pusha0" : "pushaFF");
2419 g_getimmed (flags, val, 0);
2420 AddCodeSegLine (CS, "jsr pushax");
2426 /* Value is not 16 bit or not constant */
2427 if (flags & CF_CONST) {
2428 /* Constant 32 bit value, load into eax */
2429 g_getimmed (flags, val, 0);
2432 /* Push the primary register */
2433 switch (flags & CF_TYPE) {
2436 if (flags & CF_FORCECHAR) {
2437 /* Handle as char */
2438 AddCodeSegLine (CS, "jsr pusha");
2443 AddCodeSegLine (CS, "jsr pushax");
2447 AddCodeSegLine (CS, "jsr pusheax");
2457 /* Adjust the stack offset */
2463 void g_swap (unsigned flags)
2464 /* Swap the primary register and the top of the stack. flags give the type
2465 * of *both* values (must have same size).
2468 switch (flags & CF_TYPE) {
2472 AddCodeSegLine (CS, "jsr swapstk");
2476 AddCodeSegLine (CS, "jsr swapestk");
2487 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2488 /* Call the specified subroutine name */
2490 if ((Flags & CF_FIXARGC) == 0) {
2491 /* Pass the argument count */
2494 AddCodeSegLine (CS, "jsr _%s", Label);
2495 oursp += ArgSize; /* callee pops args */
2500 void g_callind (unsigned Flags, unsigned ArgSize)
2501 /* Call subroutine with address in AX */
2503 if ((Flags & CF_FIXARGC) == 0) {
2504 /* Pass arg count */
2507 AddCodeSegLine (CS, "jsr callax"); /* do the call */
2508 oursp += ArgSize; /* callee pops args */
2513 void g_jump (unsigned Label)
2514 /* Jump to specified internal label number */
2516 AddCodeSegLine (CS, "jmp L%04X", Label);
2521 void g_switch (unsigned Flags)
2522 /* Output switch statement preamble */
2524 switch (Flags & CF_TYPE) {
2528 AddCodeSegLine (CS, "jsr switch");
2532 AddCodeSegLine (CS, "jsr lswitch");
2543 void g_case (unsigned flags, unsigned label, unsigned long val)
2544 /* Create table code for one case selector */
2546 switch (flags & CF_TYPE) {
2550 AddCodeSegLine (CS, ".word $%04X, L%04X",
2551 (unsigned)(val & 0xFFFF),
2552 (unsigned)(label & 0xFFFF));
2556 AddCodeSegLine (CS, ".dword $%08lX", val);
2557 AddCodeSegLine (CS, ".word L%04X", label & 0xFFFF);
2568 void g_truejump (unsigned flags, unsigned label)
2569 /* Jump to label if zero flag clear */
2571 if (flags & CF_SHORT) {
2572 AddCodeSegLine (CS, "bne L%04X", label);
2574 AddCodeSegLine (CS, "jne L%04X", label);
2580 void g_falsejump (unsigned flags, unsigned label)
2581 /* Jump to label if zero flag set */
2583 if (flags & CF_SHORT) {
2584 AddCodeSegLine (CS, "beq L%04X", label);
2586 AddCodeSegLine (CS, "jeq L%04X", label);
2592 static void mod_internal (int k, char* verb1, char* verb2)
2595 AddCodeSegLine (CS, "jsr %ssp%c", verb1, k + '0');
2599 AddCodeSegLine (CS, "jsr %ssp", verb2);
2605 void g_space (int space)
2606 /* Create or drop space on the stack */
2609 mod_internal (-space, "inc", "addy");
2610 } else if (space > 0) {
2611 mod_internal (space, "dec", "suby");
2617 void g_cstackcheck (void)
2618 /* Check for a C stack overflow */
2620 AddCodeSegLine (CS, "jsr cstkchk");
2625 void g_stackcheck (void)
2626 /* Check for a stack overflow */
2628 AddCodeSegLine (CS, "jsr stkchk");
2633 void g_add (unsigned flags, unsigned long val)
2634 /* Primary = TOS + Primary */
2636 static char* ops [12] = {
2637 0, "tosadda0", "tosaddax",
2638 0, "tosadda0", "tosaddax",
2643 if (flags & CF_CONST) {
2644 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2645 g_push (flags & ~CF_CONST, 0);
2647 oper (flags, val, ops);
2652 void g_sub (unsigned flags, unsigned long val)
2653 /* Primary = TOS - Primary */
2655 static char* ops [12] = {
2656 0, "tossuba0", "tossubax",
2657 0, "tossuba0", "tossubax",
2662 if (flags & CF_CONST) {
2663 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2664 g_push (flags & ~CF_CONST, 0);
2666 oper (flags, val, ops);
2671 void g_rsub (unsigned flags, unsigned long val)
2672 /* Primary = Primary - TOS */
2674 static char* ops [12] = {
2675 0, "tosrsuba0", "tosrsubax",
2676 0, "tosrsuba0", "tosrsubax",
2680 oper (flags, val, ops);
2685 void g_mul (unsigned flags, unsigned long val)
2686 /* Primary = TOS * Primary */
2688 static char* ops [12] = {
2689 0, "tosmula0", "tosmulax",
2690 0, "tosumula0", "tosumulax",
2697 /* Do strength reduction if the value is constant and a power of two */
2698 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2699 /* Generate a shift instead */
2704 /* If the right hand side is const, the lhs is not on stack but still
2705 * in the primary register.
2707 if (flags & CF_CONST) {
2709 switch (flags & CF_TYPE) {
2712 if (flags & CF_FORCECHAR) {
2713 /* Handle some special cases */
2717 AddCodeSegLine (CS, "sta tmp1");
2718 AddCodeSegLine (CS, "asl a");
2719 AddCodeSegLine (CS, "clc");
2720 AddCodeSegLine (CS, "adc tmp1");
2724 AddCodeSegLine (CS, "sta tmp1");
2725 AddCodeSegLine (CS, "asl a");
2726 AddCodeSegLine (CS, "asl a");
2727 AddCodeSegLine (CS, "clc");
2728 AddCodeSegLine (CS, "adc tmp1");
2732 AddCodeSegLine (CS, "sta tmp1");
2733 AddCodeSegLine (CS, "asl a");
2734 AddCodeSegLine (CS, "asl a");
2735 AddCodeSegLine (CS, "clc");
2736 AddCodeSegLine (CS, "adc tmp1");
2737 AddCodeSegLine (CS, "asl a");
2753 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2754 * into the normal, non-optimized stuff.
2756 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2757 g_push (flags & ~CF_CONST, 0);
2761 /* Use long way over the stack */
2762 oper (flags, val, ops);
2767 void g_div (unsigned flags, unsigned long val)
2768 /* Primary = TOS / Primary */
2770 static char* ops [12] = {
2771 0, "tosdiva0", "tosdivax",
2772 0, "tosudiva0", "tosudivax",
2777 /* Do strength reduction if the value is constant and a power of two */
2779 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2780 /* Generate a shift instead */
2783 /* Generate a division */
2784 if (flags & CF_CONST) {
2785 /* lhs is not on stack */
2786 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2787 g_push (flags & ~CF_CONST, 0);
2789 oper (flags, val, ops);
2795 void g_mod (unsigned flags, unsigned long val)
2796 /* Primary = TOS % Primary */
2798 static char* ops [12] = {
2799 0, "tosmoda0", "tosmodax",
2800 0, "tosumoda0", "tosumodax",
2806 /* Check if we can do some cost reduction */
2807 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2808 /* We can do that with an AND operation */
2809 g_and (flags, val - 1);
2811 /* Do it the hard way... */
2812 if (flags & CF_CONST) {
2813 /* lhs is not on stack */
2814 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2815 g_push (flags & ~CF_CONST, 0);
2817 oper (flags, val, ops);
2823 void g_or (unsigned flags, unsigned long val)
2824 /* Primary = TOS | Primary */
2826 static char* ops [12] = {
2827 0, "tosora0", "tosorax",
2828 0, "tosora0", "tosorax",
2833 /* If the right hand side is const, the lhs is not on stack but still
2834 * in the primary register.
2836 if (flags & CF_CONST) {
2838 switch (flags & CF_TYPE) {
2841 if (flags & CF_FORCECHAR) {
2842 if ((val & 0xFF) != 0xFF) {
2843 AddCodeSegLine (CS, "ora #$%02X", (unsigned char)val);
2851 AddCodeSegLine (CS, "ora #$%02X", (unsigned char)val);
2858 AddCodeSegLine (CS, "ora #$%02X", (unsigned char)val);
2867 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2868 * into the normal, non-optimized stuff.
2870 g_push (flags & ~CF_CONST, 0);
2874 /* Use long way over the stack */
2875 oper (flags, val, ops);
2880 void g_xor (unsigned flags, unsigned long val)
2881 /* Primary = TOS ^ Primary */
2883 static char* ops [12] = {
2884 0, "tosxora0", "tosxorax",
2885 0, "tosxora0", "tosxorax",
2891 /* If the right hand side is const, the lhs is not on stack but still
2892 * in the primary register.
2894 if (flags & CF_CONST) {
2896 switch (flags & CF_TYPE) {
2899 if (flags & CF_FORCECHAR) {
2900 if ((val & 0xFF) != 0) {
2901 AddCodeSegLine (CS, "eor #$%02X", (unsigned char)val);
2910 AddCodeSegLine (CS, "eor #$%02X", (unsigned char)val);
2913 } else if ((val & 0xFF) == 0) {
2914 AddCodeSegLine (CS, "pha");
2915 AddCodeSegLine (CS, "txa");
2916 AddCodeSegLine (CS, "eor #$%02X", (unsigned char)(val >> 8));
2917 AddCodeSegLine (CS, "tax");
2918 AddCodeSegLine (CS, "pla");
2926 AddCodeSegLine (CS, "eor #$%02X", (unsigned char)val);
2936 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2937 * into the normal, non-optimized stuff.
2939 g_push (flags & ~CF_CONST, 0);
2943 /* Use long way over the stack */
2944 oper (flags, val, ops);
2949 void g_and (unsigned flags, unsigned long val)
2950 /* Primary = TOS & Primary */
2952 static char* ops [12] = {
2953 0, "tosanda0", "tosandax",
2954 0, "tosanda0", "tosandax",
2959 /* If the right hand side is const, the lhs is not on stack but still
2960 * in the primary register.
2962 if (flags & CF_CONST) {
2964 switch (flags & CF_TYPE) {
2967 if (flags & CF_FORCECHAR) {
2968 AddCodeSegLine (CS, "and #$%02X", (unsigned char)val);
2973 if ((val & 0xFFFF) != 0xFFFF) {
2978 } else if (val != 0xFF) {
2979 AddCodeSegLine (CS, "and #$%02X", (unsigned char)val);
2981 } else if ((val & 0xFF00) == 0xFF00) {
2982 AddCodeSegLine (CS, "and #$%02X", (unsigned char)val);
2983 } else if ((val & 0x00FF) == 0x0000) {
2984 AddCodeSegLine (CS, "txa");
2985 AddCodeSegLine (CS, "and #$%02X", (unsigned char)(val >> 8));
2986 AddCodeSegLine (CS, "tax");
2989 AddCodeSegLine (CS, "tay");
2990 AddCodeSegLine (CS, "txa");
2991 AddCodeSegLine (CS, "and #$%02X", (unsigned char)(val >> 8));
2992 AddCodeSegLine (CS, "tax");
2993 AddCodeSegLine (CS, "tya");
2994 if ((val & 0x00FF) != 0x00FF) {
2995 AddCodeSegLine (CS, "and #$%02X", (unsigned char)val);
3004 AddCodeSegLine (CS, "stx sreg+1");
3005 AddCodeSegLine (CS, "stx sreg");
3006 if ((val & 0xFF) != 0xFF) {
3007 AddCodeSegLine (CS, "and #$%02X", (unsigned char)val);
3010 } else if (val == 0xFF00) {
3012 AddCodeSegLine (CS, "sta sreg+1");
3013 AddCodeSegLine (CS, "sta sreg");
3022 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3023 * into the normal, non-optimized stuff.
3025 g_push (flags & ~CF_CONST, 0);
3029 /* Use long way over the stack */
3030 oper (flags, val, ops);
3035 void g_asr (unsigned flags, unsigned long val)
3036 /* Primary = TOS >> Primary */
3038 static char* ops [12] = {
3039 0, "tosasra0", "tosasrax",
3040 0, "tosshra0", "tosshrax",
3045 /* If the right hand side is const, the lhs is not on stack but still
3046 * in the primary register.
3048 if (flags & CF_CONST) {
3050 switch (flags & CF_TYPE) {
3054 if (val >= 1 && val <= 3) {
3055 if (flags & CF_UNSIGNED) {
3056 AddCodeSegLine (CS, "jsr shrax%ld", val);
3058 AddCodeSegLine (CS, "jsr asrax%ld", val);
3061 } else if (val == 8 && (flags & CF_UNSIGNED)) {
3062 AddCodeSegLine (CS, "txa");
3069 if (val >= 1 && val <= 3) {
3070 if (flags & CF_UNSIGNED) {
3071 AddCodeSegLine (CS, "jsr shreax%ld", val);
3073 AddCodeSegLine (CS, "jsr asreax%ld", val);
3076 } else if (val == 8 && (flags & CF_UNSIGNED)) {
3077 AddCodeSegLine (CS, "txa");
3078 AddCodeSegLine (CS, "ldx sreg");
3079 AddCodeSegLine (CS, "ldy sreg+1");
3080 AddCodeSegLine (CS, "sty sreg");
3081 AddCodeSegLine (CS, "ldy #$00");
3082 AddCodeSegLine (CS, "sty sreg+1");
3084 } else if (val == 16) {
3085 AddCodeSegLine (CS, "ldy #$00");
3086 AddCodeSegLine (CS, "ldx sreg+1");
3087 if ((flags & CF_UNSIGNED) == 0) {
3088 AddCodeSegLine (CS, "bpl *+3");
3089 AddCodeSegLine (CS, "dey");
3090 AddCodeHint ("y:!");
3092 AddCodeSegLine (CS, "lda sreg");
3093 AddCodeSegLine (CS, "sty sreg+1");
3094 AddCodeSegLine (CS, "sty sreg");
3103 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3104 * into the normal, non-optimized stuff.
3106 g_push (flags & ~CF_CONST, 0);
3110 /* Use long way over the stack */
3111 oper (flags, val, ops);
3116 void g_asl (unsigned flags, unsigned long val)
3117 /* Primary = TOS << Primary */
3119 static char* ops [12] = {
3120 0, "tosasla0", "tosaslax",
3121 0, "tosshla0", "tosshlax",
3127 /* If the right hand side is const, the lhs is not on stack but still
3128 * in the primary register.
3130 if (flags & CF_CONST) {
3132 switch (flags & CF_TYPE) {
3136 if (val >= 1 && val <= 3) {
3137 if (flags & CF_UNSIGNED) {
3138 AddCodeSegLine (CS, "jsr shlax%ld", val);
3140 AddCodeSegLine (CS, "jsr aslax%ld", val);
3143 } else if (val == 8) {
3144 AddCodeSegLine (CS, "tax");
3145 AddCodeSegLine (CS, "lda #$00");
3151 if (val >= 1 && val <= 3) {
3152 if (flags & CF_UNSIGNED) {
3153 AddCodeSegLine (CS, "jsr shleax%ld", val);
3155 AddCodeSegLine (CS, "jsr asleax%ld", val);
3158 } else if (val == 8) {
3159 AddCodeSegLine (CS, "ldy sreg");
3160 AddCodeSegLine (CS, "sty sreg+1");
3161 AddCodeSegLine (CS, "stx sreg");
3162 AddCodeSegLine (CS, "tax");
3163 AddCodeSegLine (CS, "lda #$00");
3165 } else if (val == 16) {
3166 AddCodeSegLine (CS, "stx sreg+1");
3167 AddCodeSegLine (CS, "sta sreg");
3168 AddCodeSegLine (CS, "lda #$00");
3169 AddCodeSegLine (CS, "tax");
3178 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3179 * into the normal, non-optimized stuff.
3181 g_push (flags & ~CF_CONST, 0);
3185 /* Use long way over the stack */
3186 oper (flags, val, ops);
3191 void g_neg (unsigned flags)
3192 /* Primary = -Primary */
3194 switch (flags & CF_TYPE) {
3198 AddCodeSegLine (CS, "jsr negax");
3202 AddCodeSegLine (CS, "jsr negeax");
3212 void g_bneg (unsigned flags)
3213 /* Primary = !Primary */
3215 switch (flags & CF_TYPE) {
3218 AddCodeSegLine (CS, "jsr bnega");
3222 AddCodeSegLine (CS, "jsr bnegax");
3226 AddCodeSegLine (CS, "jsr bnegeax");
3236 void g_com (unsigned flags)
3237 /* Primary = ~Primary */
3239 switch (flags & CF_TYPE) {
3243 AddCodeSegLine (CS, "jsr complax");
3247 AddCodeSegLine (CS, "jsr compleax");
3257 void g_inc (unsigned flags, unsigned long val)
3258 /* Increment the primary register by a given number */
3260 /* Don't inc by zero */
3265 /* Generate code for the supported types */
3267 switch (flags & CF_TYPE) {
3270 if (flags & CF_FORCECHAR) {
3271 if (CPU == CPU_65C02 && val <= 2) {
3273 AddCodeSegLine (CS, "ina");
3276 AddCodeSegLine (CS, "clc");
3277 AddCodeSegLine (CS, "adc #$%02X", (unsigned char)val);
3284 if (CPU == CPU_65C02 && val == 1) {
3285 AddCodeSegLine (CS, "ina");
3286 AddCodeSegLine (CS, "bne *+3");
3287 AddCodeSegLine (CS, "inx");
3288 /* Tell the optimizer that the X register may be invalid */
3289 AddCodeHint ("x:!");
3290 } else if (CodeSizeFactor < 200) {
3293 AddCodeSegLine (CS, "jsr incax%lu", val);
3294 } else if (val <= 255) {
3296 AddCodeSegLine (CS, "jsr incaxy");
3298 g_add (flags | CF_CONST, val);
3301 /* Inline the code */
3303 if ((val & 0xFF) != 0) {
3304 AddCodeSegLine (CS, "clc");
3305 AddCodeSegLine (CS, "adc #$%02X", (unsigned char) val);
3306 AddCodeSegLine (CS, "bcc *+3");
3307 AddCodeSegLine (CS, "inx");
3308 /* Tell the optimizer that the X register may be invalid */
3309 AddCodeHint ("x:!");
3312 AddCodeSegLine (CS, "inx");
3315 AddCodeSegLine (CS, "inx");
3318 AddCodeSegLine (CS, "clc");
3319 if ((val & 0xFF) != 0) {
3320 AddCodeSegLine (CS, "adc #$%02X", (unsigned char) val);
3321 /* Tell the optimizer that the X register may be invalid */
3322 AddCodeHint ("x:!");
3324 AddCodeSegLine (CS, "pha");
3325 AddCodeSegLine (CS, "txa");
3326 AddCodeSegLine (CS, "adc #$%02X", (unsigned char) (val >> 8));
3327 AddCodeSegLine (CS, "tax");
3328 AddCodeSegLine (CS, "pla");
3336 AddCodeSegLine (CS, "jsr inceaxy");
3338 g_add (flags | CF_CONST, val);
3350 void g_dec (unsigned flags, unsigned long val)
3351 /* Decrement the primary register by a given number */
3353 /* Don't dec by zero */
3358 /* Generate code for the supported types */
3360 switch (flags & CF_TYPE) {
3363 if (flags & CF_FORCECHAR) {
3364 if (CPU == CPU_65C02 && val <= 2) {
3366 AddCodeSegLine (CS, "dea");
3369 AddCodeSegLine (CS, "sec");
3370 AddCodeSegLine (CS, "sbc #$%02X", (unsigned char)val);
3377 if (CodeSizeFactor < 200) {
3378 /* Use subroutines */
3380 AddCodeSegLine (CS, "jsr decax%d", (int) val);
3381 } else if (val <= 255) {
3383 AddCodeSegLine (CS, "jsr decaxy");
3385 g_sub (flags | CF_CONST, val);
3388 /* Inline the code */
3390 if ((val & 0xFF) != 0) {
3391 AddCodeSegLine (CS, "sec");
3392 AddCodeSegLine (CS, "sbc #$%02X", (unsigned char) val);
3393 AddCodeSegLine (CS, "bcs *+3");
3394 AddCodeSegLine (CS, "dex");
3395 /* Tell the optimizer that the X register may be invalid */
3396 AddCodeHint ("x:!");
3399 AddCodeSegLine (CS, "dex");
3402 AddCodeSegLine (CS, "dex");
3405 AddCodeSegLine (CS, "sec");
3406 if ((val & 0xFF) != 0) {
3407 AddCodeSegLine (CS, "sbc #$%02X", (unsigned char) val);
3408 /* Tell the optimizer that the X register may be invalid */
3409 AddCodeHint ("x:!");
3411 AddCodeSegLine (CS, "pha");
3412 AddCodeSegLine (CS, "txa");
3413 AddCodeSegLine (CS, "sbc #$%02X", (unsigned char) (val >> 8));
3414 AddCodeSegLine (CS, "tax");
3415 AddCodeSegLine (CS, "pla");
3423 AddCodeSegLine (CS, "jsr deceaxy");
3425 g_sub (flags | CF_CONST, val);
3438 * Following are the conditional operators. They compare the TOS against
3439 * the primary and put a literal 1 in the primary if the condition is
3440 * true, otherwise they clear the primary register
3445 void g_eq (unsigned flags, unsigned long val)
3446 /* Test for equal */
3448 static char* ops [12] = {
3449 "toseq00", "toseqa0", "toseqax",
3450 "toseq00", "toseqa0", "toseqax",
3455 /* If the right hand side is const, the lhs is not on stack but still
3456 * in the primary register.
3458 if (flags & CF_CONST) {
3460 switch (flags & CF_TYPE) {
3463 if (flags & CF_FORCECHAR) {
3464 AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
3465 AddCodeSegLine (CS, "jsr booleq");
3471 AddCodeSegLine (CS, "cpx #$%02X", (unsigned char)(val >> 8));
3472 AddCodeSegLine (CS, "bne *+4");
3473 AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
3474 AddCodeSegLine (CS, "jsr booleq");
3484 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3485 * into the normal, non-optimized stuff.
3487 g_push (flags & ~CF_CONST, 0);
3491 /* Use long way over the stack */
3492 oper (flags, val, ops);
3497 void g_ne (unsigned flags, unsigned long val)
3498 /* Test for not equal */
3500 static char* ops [12] = {
3501 "tosne00", "tosnea0", "tosneax",
3502 "tosne00", "tosnea0", "tosneax",
3508 /* If the right hand side is const, the lhs is not on stack but still
3509 * in the primary register.
3511 if (flags & CF_CONST) {
3513 switch (flags & CF_TYPE) {
3516 if (flags & CF_FORCECHAR) {
3517 AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
3518 AddCodeSegLine (CS, "jsr boolne");
3524 AddCodeSegLine (CS, "cpx #$%02X", (unsigned char)(val >> 8));
3525 AddCodeSegLine (CS, "bne *+4");
3526 AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
3527 AddCodeSegLine (CS, "jsr boolne");
3537 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3538 * into the normal, non-optimized stuff.
3540 g_push (flags & ~CF_CONST, 0);
3544 /* Use long way over the stack */
3545 oper (flags, val, ops);
3550 void g_lt (unsigned flags, unsigned long val)
3551 /* Test for less than */
3553 static char* ops [12] = {
3554 "toslt00", "toslta0", "tosltax",
3555 "tosult00", "tosulta0", "tosultax",
3560 /* If the right hand side is const, the lhs is not on stack but still
3561 * in the primary register.
3563 if (flags & CF_CONST) {
3565 /* Give a warning in some special cases */
3566 if ((flags & CF_UNSIGNED) && val == 0) {
3567 Warning ("Condition is never true");
3570 /* Look at the type */
3571 switch (flags & CF_TYPE) {
3574 if (flags & CF_FORCECHAR) {
3575 AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
3576 if (flags & CF_UNSIGNED) {
3577 AddCodeSegLine (CS, "jsr boolult");
3579 AddCodeSegLine (CS, "jsr boollt");
3586 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3587 /* If we have a signed compare against zero, we only need to
3588 * test the high byte.
3590 AddCodeSegLine (CS, "txa");
3591 AddCodeSegLine (CS, "jsr boollt");
3594 /* Direct code only for unsigned data types */
3595 if (flags & CF_UNSIGNED) {
3596 AddCodeSegLine (CS, "cpx #$%02X", (unsigned char)(val >> 8));
3597 AddCodeSegLine (CS, "bne *+4");
3598 AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
3599 AddCodeSegLine (CS, "jsr boolult");
3611 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3612 * into the normal, non-optimized stuff.
3614 g_push (flags & ~CF_CONST, 0);
3618 /* Use long way over the stack */
3619 oper (flags, val, ops);
3624 void g_le (unsigned flags, unsigned long val)
3625 /* Test for less than or equal to */
3627 static char* ops [12] = {
3628 "tosle00", "toslea0", "tosleax",
3629 "tosule00", "tosulea0", "tosuleax",
3635 /* If the right hand side is const, the lhs is not on stack but still
3636 * in the primary register.
3638 if (flags & CF_CONST) {
3640 /* Look at the type */
3641 switch (flags & CF_TYPE) {
3644 if (flags & CF_FORCECHAR) {
3645 AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
3646 if (flags & CF_UNSIGNED) {
3647 AddCodeSegLine (CS, "jsr boolule");
3649 AddCodeSegLine (CS, "jsr boolle");
3656 if (flags & CF_UNSIGNED) {
3657 AddCodeSegLine (CS, "cpx #$%02X", (unsigned char)(val >> 8));
3658 AddCodeSegLine (CS, "bne *+4");
3659 AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
3660 AddCodeSegLine (CS, "jsr boolule");
3672 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3673 * into the normal, non-optimized stuff.
3675 g_push (flags & ~CF_CONST, 0);
3679 /* Use long way over the stack */
3680 oper (flags, val, ops);
3685 void g_gt (unsigned flags, unsigned long val)
3686 /* Test for greater than */
3688 static char* ops [12] = {
3689 "tosgt00", "tosgta0", "tosgtax",
3690 "tosugt00", "tosugta0", "tosugtax",
3696 /* If the right hand side is const, the lhs is not on stack but still
3697 * in the primary register.
3699 if (flags & CF_CONST) {
3701 /* Look at the type */
3702 switch (flags & CF_TYPE) {
3705 if (flags & CF_FORCECHAR) {
3706 AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
3707 if (flags & CF_UNSIGNED) {
3708 /* If we have a compare > 0, we will replace it by
3709 * != 0 here, since both are identical but the latter
3710 * is easier to optimize.
3713 AddCodeSegLine (CS, "jsr boolugt");
3715 AddCodeSegLine (CS, "jsr boolne");
3718 AddCodeSegLine (CS, "jsr boolgt");
3725 if (flags & CF_UNSIGNED) {
3726 /* If we have a compare > 0, we will replace it by
3727 * != 0 here, since both are identical but the latter
3728 * is easier to optimize.
3730 if ((val & 0xFFFF) == 0) {
3731 AddCodeSegLine (CS, "stx tmp1");
3732 AddCodeSegLine (CS, "ora tmp1");
3733 AddCodeSegLine (CS, "jsr boolne");
3735 AddCodeSegLine (CS, "cpx #$%02X", (unsigned char)(val >> 8));
3736 AddCodeSegLine (CS, "bne *+4");
3737 AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
3738 AddCodeSegLine (CS, "jsr boolugt");
3751 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3752 * into the normal, non-optimized stuff.
3754 g_push (flags & ~CF_CONST, 0);
3758 /* Use long way over the stack */
3759 oper (flags, val, ops);
3764 void g_ge (unsigned flags, unsigned long val)
3765 /* Test for greater than or equal to */
3767 static char* ops [12] = {
3768 "tosge00", "tosgea0", "tosgeax",
3769 "tosuge00", "tosugea0", "tosugeax",
3775 /* If the right hand side is const, the lhs is not on stack but still
3776 * in the primary register.
3778 if (flags & CF_CONST) {
3780 /* Give a warning in some special cases */
3781 if ((flags & CF_UNSIGNED) && val == 0) {
3782 Warning ("Condition is always true");
3785 /* Look at the type */
3786 switch (flags & CF_TYPE) {
3789 if (flags & CF_FORCECHAR) {
3790 AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
3791 if (flags & CF_UNSIGNED) {
3792 AddCodeSegLine (CS, "jsr booluge");
3794 AddCodeSegLine (CS, "jsr boolge");
3801 if (flags & CF_UNSIGNED) {
3802 AddCodeSegLine (CS, "cpx #$%02X", (unsigned char)(val >> 8));
3803 AddCodeSegLine (CS, "bne *+4");
3804 AddCodeSegLine (CS, "cmp #$%02X", (unsigned char)val);
3805 AddCodeSegLine (CS, "jsr booluge");
3817 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3818 * into the normal, non-optimized stuff.
3820 g_push (flags & ~CF_CONST, 0);
3824 /* Use long way over the stack */
3825 oper (flags, val, ops);
3830 /*****************************************************************************/
3831 /* Allocating static storage */
3832 /*****************************************************************************/
3836 void g_res (unsigned n)
3837 /* Reserve static storage, n bytes */
3839 AddDataSegLine (DS, "\t.res\t%u,$00", n);
3844 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3845 /* Define data with the size given in flags */
3847 if (flags & CF_CONST) {
3849 /* Numeric constant */
3850 switch (flags & CF_TYPE) {
3853 AddDataSegLine (DS, "\t.byte\t$%02lX", val & 0xFF);
3857 AddDataSegLine (DS, "\t.word\t$%04lX", val & 0xFFFF);
3861 AddDataSegLine (DS, "\t.dword\t$%08lX", val & 0xFFFFFFFF);
3872 /* Create the correct label name */
3873 const char* Label = GetLabelName (flags, val, offs);
3875 /* Labels are always 16 bit */
3876 AddDataSegLine (DS, "\t.word\t%s", Label);
3883 void g_defbytes (const void* Bytes, unsigned Count)
3884 /* Output a row of bytes as a constant */
3890 /* Cast the buffer pointer */
3891 const unsigned char* Data = (const unsigned char*) Bytes;
3893 /* Output the stuff */
3896 /* How many go into this line? */
3897 if ((Chunk = Count) > 16) {
3902 /* Output one line */
3903 strcpy (Buf, "\t.byte\t");
3906 B += sprintf (B, "$%02X", *Data++);
3912 /* Output the line */
3919 void g_zerobytes (unsigned n)
3920 /* Output n bytes of data initialized with zero */
3922 AddDataSegLine (DS, "\t.res\t%u,$00", n);
3927 /*****************************************************************************/
3928 /* User supplied assembler code */
3929 /*****************************************************************************/
3933 void g_asmcode (const char* Line, int Len)
3934 /* Output one line of assembler code. If Len is greater than zero, it is used
3935 * as the maximum number of characters to use from Line.
3939 AddCodeSegLine (CS, "%.*s", Len, Line);
3941 AddCodeSegLine (CS, "%s", Line);
3947 /*****************************************************************************/
3948 /* Inlined known functions */
3949 /*****************************************************************************/
3953 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3954 /* Inline the strlen() function */
3956 /* We need a label in both cases */
3957 unsigned label = GetLocalLabel ();
3959 /* Two different encodings */
3960 if (flags & CF_CONST) {
3962 /* The address of the string is constant. Create the correct label name */
3963 char* lbuf = GetLabelName (flags, val, offs);
3965 /* Generate the strlen code */
3966 AddCodeSegLine (CS, "ldy #$FF");
3967 g_defloclabel (label);
3968 AddCodeSegLine (CS, "iny");
3969 AddCodeSegLine (CS, "lda %s,y", lbuf);
3970 AddCodeSegLine (CS, "bne L%04X", label);
3971 AddCodeSegLine (CS, "tax");
3972 AddCodeSegLine (CS, "tya");
3976 /* Address not constant but in primary */
3977 if (CodeSizeFactor < 400) {
3978 /* This is too much code, so call strlen instead of inlining */
3979 AddCodeSegLine (CS, "jsr _strlen");
3981 /* Inline the function */
3982 AddCodeSegLine (CS, "sta ptr1");
3983 AddCodeSegLine (CS, "stx ptr1+1");
3984 AddCodeSegLine (CS, "ldy #$FF");
3985 g_defloclabel (label);
3986 AddCodeSegLine (CS, "iny");
3987 AddCodeSegLine (CS, "lda (ptr1),y");
3988 AddCodeSegLine (CS, "bne L%04X", label);
3989 AddCodeSegLine (CS, "tax");
3990 AddCodeSegLine (CS, "tya");