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 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
447 static void ldxconst (unsigned val)
448 /* Load x with a constant */
450 AddCodeLine ("\tldx\t#$%02X", val & 0xFF);
455 static void ldyconst (unsigned val)
456 /* Load y with a constant */
458 AddCodeLine ("\tldy\t#$%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 AddCodeLine ("\tjsr\tenter");
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 AddCodeLine ("\trts");
520 AddCodeHint ("y:-"); /* Y register no longer used */
521 AddCodeLine ("\tjmp\tincsp%d", k);
525 AddCodeLine ("\tjmp\taddysp");
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 AddCodeLine ("\tlda\tregbank%+d", RegOffs);
579 AddCodeLine ("\tjsr\tpusha");
581 } else if (Bytes == 2) {
583 AddCodeLine ("\tlda\tregbank%+d", RegOffs);
584 AddCodeLine ("\tldx\tregbank%+d", RegOffs+1);
585 AddCodeLine ("\tjsr\tpushax");
589 /* More than two bytes - loop */
590 unsigned Label = GetLocalLabel ();
592 ldyconst (Bytes - 1);
594 g_defloclabel (Label);
595 AddCodeLine ("\tlda\tregbank%+d,x", RegOffs-1);
596 AddCodeLine ("\tsta\t(sp),y");
597 AddCodeLine ("\tdey");
598 AddCodeLine ("\tdex");
599 AddCodeLine ("\tbne\tL%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 AddCodeLine ("\tlda\t(sp),y");
621 AddCodeLine ("\tsta\tregbank%+d", RegOffs);
623 } else if (Bytes == 2) {
625 ldyconst (StackOffs);
626 AddCodeLine ("\tlda\t(sp),y");
627 AddCodeLine ("\tsta\tregbank%+d", RegOffs);
628 AddCodeLine ("\tiny");
629 AddCodeLine ("\tlda\t(sp),y");
630 AddCodeLine ("\tsta\tregbank%+d", RegOffs+1);
634 /* More than two bytes - loop */
635 unsigned Label = GetLocalLabel ();
636 ldyconst (StackOffs+Bytes-1);
638 g_defloclabel (Label);
639 AddCodeLine ("\tlda\t(sp),y");
640 AddCodeLine ("\tsta\tregbank%+d,x", RegOffs-1);
641 AddCodeLine ("\tdey");
642 AddCodeLine ("\tdex");
643 AddCodeLine ("\tbne\tL%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 AddCodeLine ("\tldx\t#$00");
678 AddCodeLine ("\tstx\tsreg+1");
679 AddCodeLine ("\tstx\tsreg");
680 AddCodeLine ("\tlda\t#$%02X", (unsigned char) val);
681 } else if ((val & 0xFFFF00FF) == 0) {
682 AddCodeLine ("\tlda\t#$00");
683 AddCodeLine ("\tsta\tsreg+1");
684 AddCodeLine ("\tsta\tsreg");
685 AddCodeLine ("\tldx\t#$%02X", (unsigned char) (val >> 8));
686 } else if ((val & 0xFFFF0000) == 0 && CodeSizeFactor > 140) {
687 AddCodeLine ("\tlda\t#$00");
688 AddCodeLine ("\tsta\tsreg+1");
689 AddCodeLine ("\tsta\tsreg");
690 AddCodeLine ("\tlda\t#$%02X", (unsigned char) val);
691 AddCodeLine ("\tldx\t#$%02X", (unsigned char) (val >> 8));
692 } else if ((val & 0xFFFFFF00) == 0xFFFFFF00) {
693 AddCodeLine ("\tldx\t#$FF");
694 AddCodeLine ("\tstx\tsreg+1");
695 AddCodeLine ("\tstx\tsreg");
696 if ((val & 0xFF) == 0xFF) {
697 AddCodeLine ("\ttxa");
699 AddCodeLine ("\tlda\t#$%02X", (unsigned char) val);
701 } else if ((val & 0xFFFF00FF) == 0xFFFF00FF) {
702 AddCodeLine ("\tlda\t#$FF");
703 AddCodeLine ("\tsta\tsreg+1");
704 AddCodeLine ("\tsta\tsreg");
705 AddCodeLine ("\tldx\t#$%02X", (unsigned char) (val >> 8));
707 /* Call a subroutine that will load following value */
708 AddCodeLine ("\tjsr\tldeax");
709 AddCodeLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
721 /* Some sort of label */
722 const char* Label = GetLabelName (flags, val, offs);
724 /* Load the address into the primary */
725 AddCodeLine ("\tlda\t#<(%s)", Label);
726 AddCodeLine ("\tldx\t#>(%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 AddCodeLine ("\tlda\t%s", lbuf); /* load A from the label */
747 AddCodeLine ("\tlda\t%s", lbuf); /* load A from the label */
748 if (!(flags & CF_UNSIGNED)) {
749 /* Must sign extend */
750 AddCodeLine ("\tbpl\t*+3");
751 AddCodeLine ("\tdex");
752 AddCodeHint ("x:!"); /* X is invalid now */
758 AddCodeLine ("\tlda\t%s", lbuf);
759 if (flags & CF_TEST) {
760 AddCodeLine ("\tora\t%s+1", lbuf);
762 AddCodeLine ("\tldx\t%s+1", lbuf);
767 if (flags & CF_TEST) {
768 AddCodeLine ("\tlda\t%s+3", lbuf);
769 AddCodeLine ("\tora\t%s+2", lbuf);
770 AddCodeLine ("\tora\t%s+1", lbuf);
771 AddCodeLine ("\tora\t%s+0", lbuf);
773 AddCodeLine ("\tlda\t%s+3", lbuf);
774 AddCodeLine ("\tsta\tsreg+1");
775 AddCodeLine ("\tlda\t%s+2", lbuf);
776 AddCodeLine ("\tsta\tsreg");
777 AddCodeLine ("\tldx\t%s+1", lbuf);
778 AddCodeLine ("\tlda\t%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 AddCodeLine ("\tlda\t(sp)");
803 AddCodeLine ("\tlda\t(sp),y");
807 AddCodeLine ("\tldx\t#$00");
808 AddCodeLine ("\tlda\t(sp,x)");
811 AddCodeLine ("\tldx\t#$00");
812 AddCodeLine ("\tlda\t(sp),y");
814 if ((flags & CF_UNSIGNED) == 0) {
815 AddCodeLine ("\tbpl\t*+3");
816 AddCodeLine ("\tdex");
817 AddCodeHint ("x:!"); /* X is invalid now */
823 CheckLocalOffs (offs + 1);
824 if (flags & CF_TEST) {
826 AddCodeLine ("\tlda\t(sp),y");
827 AddCodeLine ("\tdey");
828 AddCodeLine ("\tora\t(sp),y");
830 if (CodeSizeFactor > 180) {
832 AddCodeLine ("\tlda\t(sp),y");
833 AddCodeLine ("\ttax");
834 AddCodeLine ("\tdey");
835 AddCodeLine ("\tlda\t(sp),y");
839 AddCodeLine ("\tjsr\tldaxysp");
841 AddCodeLine ("\tjsr\tldax0sp");
850 AddCodeLine ("\tjsr\tldeaxysp");
852 AddCodeLine ("\tjsr\tldeax0sp");
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 AddCodeLine ("\tjsr\tldauidx");
884 AddCodeLine ("\tjsr\tldaidx");
887 if (flags & CF_UNSIGNED) {
888 if (CodeSizeFactor > 250) {
889 AddCodeLine ("\tsta\tptr1");
890 AddCodeLine ("\tstx\tptr1+1");
891 AddCodeLine ("\tldx\t#$00");
892 AddCodeLine ("\tlda\t(ptr1,x)");
894 AddCodeLine ("\tjsr\tldaui");
897 AddCodeLine ("\tjsr\tldai");
903 if (flags & CF_TEST) {
905 AddCodeLine ("\tsta\tptr1");
906 AddCodeLine ("\tstx\tptr1+1");
907 AddCodeLine ("\tlda\t(ptr1),y");
908 AddCodeLine ("\tiny");
909 AddCodeLine ("\tora\t(ptr1),y");
912 AddCodeLine ("\tjsr\tldaxi");
915 AddCodeLine ("\tjsr\tldaxidx");
922 AddCodeLine ("\tjsr\tldeaxi");
925 AddCodeLine ("\tjsr\tldeaxidx");
927 if (flags & CF_TEST) {
928 AddCodeLine ("\tjsr\ttsteax");
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 AddCodeLine ("\tlda\tsp");
949 AddCodeLine ("\tldx\tsp+1");
951 if (CodeSizeFactor < 300) {
952 ldaconst (offs); /* Load A with offset value */
953 AddCodeLine ("\tjsr\tleaasp"); /* Load effective address */
955 if (CPU == CPU_65C02 && offs == 1) {
956 AddCodeLine ("\tlda\tsp");
957 AddCodeLine ("\tldx\tsp+1");
958 AddCodeLine ("\tina");
959 AddCodeLine ("\tbne\t*+3");
960 AddCodeLine ("\tinx");
961 AddCodeHint ("x:!"); /* Invalidate X */
964 AddCodeLine ("\tclc");
965 AddCodeLine ("\tldx\tsp+1");
966 AddCodeLine ("\tadc\tsp");
967 AddCodeLine ("\tbcc\t*+3");
968 AddCodeLine ("\tinx");
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 AddCodeLine ("\tlda\t(sp)");
998 ldyconst (ArgSizeOffs);
999 AddCodeLine ("\tlda\t(sp),y");
1002 /* Add the value of the stackpointer */
1003 if (CodeSizeFactor > 250) {
1004 AddCodeLine ("\tldx\tsp+1");
1005 AddCodeLine ("\tclc");
1006 AddCodeLine ("\tadc\tsp");
1007 AddCodeLine ("\tbcc\t*+3");
1008 AddCodeLine ("\tinx");
1009 AddCodeHint ("x:!"); /* Invalidate X */
1011 AddCodeLine ("\tjsr\tleaasp");
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 AddCodeLine ("\tsta\t%s", lbuf);
1044 AddCodeLine ("\tsta\t%s", lbuf);
1045 AddCodeLine ("\tstx\t%s+1", lbuf);
1049 AddCodeLine ("\tsta\t%s", lbuf);
1050 AddCodeLine ("\tstx\t%s+1", lbuf);
1051 AddCodeLine ("\tldy\tsreg");
1052 AddCodeLine ("\tsty\t%s+2", lbuf);
1053 AddCodeLine ("\tldy\tsreg+1");
1054 AddCodeLine ("\tsty\t%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 AddCodeLine ("\tlda\t#$%02X", (unsigned char) Val);
1076 if (CPU == CPU_65C02 && Offs == 0) {
1077 AddCodeLine ("\tsta\t(sp)");
1080 AddCodeLine ("\tsta\t(sp),y");
1085 if (Flags & CF_CONST) {
1087 AddCodeLine ("\tlda\t#$%02X", (unsigned char) (Val >> 8));
1088 AddCodeLine ("\tsta\t(sp),y");
1089 if ((Flags & CF_NOKEEP) == 0) {
1090 /* Place high byte into X */
1091 AddCodeLine ("\ttax");
1093 if (CPU == CPU_65C02 && Offs == 0) {
1094 AddCodeLine ("\tlda\t#$%02X", (unsigned char) Val);
1095 AddCodeLine ("\tsta\t(sp)");
1097 if ((Val & 0xFF) == Offs+1) {
1098 /* The value we need is already in Y */
1099 AddCodeLine ("\ttya");
1100 AddCodeLine ("\tdey");
1102 AddCodeLine ("\tdey");
1103 AddCodeLine ("\tlda\t#$%02X", (unsigned char) Val);
1105 AddCodeLine ("\tsta\t(sp),y");
1108 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
1111 AddCodeLine ("\tjsr\tstaxysp");
1113 AddCodeLine ("\tjsr\tstax0sp");
1116 if (CPU == CPU_65C02 && Offs == 0) {
1117 AddCodeLine ("\tsta\t(sp)");
1119 AddCodeLine ("\ttxa");
1120 AddCodeLine ("\tsta\t(sp),y");
1123 AddCodeLine ("\tsta\t(sp),y");
1124 AddCodeLine ("\tiny");
1125 AddCodeLine ("\ttxa");
1126 AddCodeLine ("\tsta\t(sp),y");
1133 if (Flags & CF_CONST) {
1134 g_getimmed (Flags, Val, 0);
1138 AddCodeLine ("\tjsr\tsteaxysp");
1140 AddCodeLine ("\tjsr\tsteax0sp");
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 AddCodeLine ("\tldy\t#$00");
1166 AddCodeLine ("\tclc");
1167 AddCodeLine ("\tpha");
1168 AddCodeLine ("\tlda\t#$%02X", Offs & 0xFF);
1169 AddCodeLine ("\tadc\t(sp),y");
1170 AddCodeLine ("\tsta\t(sp),y");
1171 AddCodeLine ("\tiny");
1172 AddCodeLine ("\tlda\t#$%02X", (Offs >> 8) & 0xFF);
1173 AddCodeLine ("\tadc\t(sp),y");
1174 AddCodeLine ("\tsta\t(sp),y");
1175 AddCodeLine ("\tpla");
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 AddCodeLine ("\tldy\t#$01");
1184 AddCodeLine ("\tclc");
1185 AddCodeLine ("\tpha");
1186 AddCodeLine ("\tlda\t#$%02X", (Offs >> 8) & 0xFF);
1187 AddCodeLine ("\tadc\t(sp),y");
1188 AddCodeLine ("\tsta\t(sp),y");
1189 AddCodeLine ("\tpla");
1191 /* Offset is now just the low byte */
1195 /* Check the size and determine operation */
1196 switch (Flags & CF_TYPE) {
1201 AddCodeLine ("\tjsr\tstaspidx");
1203 AddCodeLine ("\tjsr\tstaspp");
1210 AddCodeLine ("\tjsr\tstaxspidx");
1212 AddCodeLine ("\tjsr\tstaxspp");
1219 AddCodeLine ("\tjsr\tsteaxspidx");
1221 AddCodeLine ("\tjsr\tsteaxspp");
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 AddCodeLine ("\tjsr\ttosulong");
1252 AddCodeLine ("\tjsr\ttoslong");
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 AddCodeLine ("\tjsr\ttosint");
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 AddCodeLine ("\tsty\tsreg");
1299 AddCodeLine ("\tsty\tsreg+1");
1301 AddCodeLine ("\tjsr\taxulong");
1304 AddCodeLine ("\tjsr\taxlong");
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 AddCodeLine ("\tasl\ta");
1432 if (CodeSizeFactor >= (p2+1)*130U) {
1433 AddCodeLine ("\tstx\ttmp1");
1435 AddCodeLine ("\tasl\ta");
1436 AddCodeLine ("\trol\ttmp1");
1438 AddCodeLine ("\tldx\ttmp1");
1440 if (flags & CF_UNSIGNED) {
1441 AddCodeLine ("\tjsr\tshlax%d", p2);
1443 AddCodeLine ("\tjsr\taslax%d", p2);
1449 if (flags & CF_UNSIGNED) {
1450 AddCodeLine ("\tjsr\tshleax%d", p2);
1452 AddCodeLine ("\tjsr\tasleax%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 AddCodeLine ("\tlsr\ta");
1484 } else if (p2 <= 2) {
1485 AddCodeLine ("\tcmp\t#$80");
1486 AddCodeLine ("\tror\ta");
1493 if (flags & CF_UNSIGNED) {
1494 if (CodeSizeFactor >= (p2+1)*130U) {
1495 AddCodeLine ("\tstx\ttmp1");
1497 AddCodeLine ("\tlsr\ttmp1");
1498 AddCodeLine ("\tror\ta");
1500 AddCodeLine ("\tldx\ttmp1");
1502 AddCodeLine ("\tjsr\tlsrax%d", p2);
1505 if (CodeSizeFactor >= (p2+1)*150U) {
1506 AddCodeLine ("\tstx\ttmp1");
1508 AddCodeLine ("\tcpx\t#$80");
1509 AddCodeLine ("\tror\ttmp1");
1510 AddCodeLine ("\tror\ta");
1512 AddCodeLine ("\tldx\ttmp1");
1514 AddCodeLine ("\tjsr\tasrax%d", p2);
1520 if (flags & CF_UNSIGNED) {
1521 AddCodeLine ("\tjsr\tlsreax%d", p2);
1523 AddCodeLine ("\tjsr\tasreax%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 AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1560 AddCodeLine ("\tclc");
1561 AddCodeLine ("\tadc\t(sp),y");
1562 AddCodeLine ("\tbcc\t*+3");
1563 AddCodeLine ("\tinx");
1564 AddCodeHint ("x:!");
1568 AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1569 AddCodeLine ("\tclc");
1570 AddCodeLine ("\tadc\t(sp),y");
1571 AddCodeLine ("\tpha");
1572 AddCodeLine ("\ttxa");
1573 AddCodeLine ("\tiny");
1574 AddCodeLine ("\tadc\t(sp),y");
1575 AddCodeLine ("\ttax");
1576 AddCodeLine ("\tpla");
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 AddCodeLine ("\tclc");
1604 AddCodeLine ("\tadc\t%s", lbuf);
1605 AddCodeLine ("\tbcc\t*+3");
1606 AddCodeLine ("\tinx");
1607 AddCodeHint ("x:!");
1611 AddCodeLine ("\tclc");
1612 AddCodeLine ("\tadc\t%s", lbuf);
1613 AddCodeLine ("\ttay");
1614 AddCodeLine ("\ttxa");
1615 AddCodeLine ("\tadc\t%s+1", lbuf);
1616 AddCodeLine ("\ttax");
1617 AddCodeLine ("\ttya");
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 AddCodeLine ("\tldx\t#$00");
1676 if (flags & CF_CONST) {
1678 AddCodeLine ("\tinc\t%s", lbuf);
1679 AddCodeLine ("\tlda\t%s", lbuf);
1681 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1682 AddCodeLine ("\tclc");
1683 AddCodeLine ("\tadc\t%s", lbuf);
1684 AddCodeLine ("\tsta\t%s", lbuf);
1687 AddCodeLine ("\tclc");
1688 AddCodeLine ("\tadc\t%s", lbuf);
1689 AddCodeLine ("\tsta\t%s", lbuf);
1691 if ((flags & CF_UNSIGNED) == 0) {
1692 AddCodeLine ("\tbpl\t*+3");
1693 AddCodeLine ("\tdex");
1694 AddCodeHint ("x:!"); /* Invalidate X */
1701 if (flags & CF_CONST) {
1703 label = GetLocalLabel ();
1704 AddCodeLine ("\tinc\t%s", lbuf);
1705 AddCodeLine ("\tbne\tL%04X", (int)label);
1706 AddCodeLine ("\tinc\t%s+1", lbuf);
1707 g_defloclabel (label);
1708 AddCodeLine ("\tlda\t%s", lbuf); /* Hmmm... */
1709 AddCodeLine ("\tldx\t%s+1", lbuf);
1711 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1712 AddCodeLine ("\tclc");
1713 AddCodeLine ("\tadc\t%s", lbuf);
1714 AddCodeLine ("\tsta\t%s", lbuf);
1716 label = GetLocalLabel ();
1717 AddCodeLine ("\tbcc\tL%04X", (int)label);
1718 AddCodeLine ("\tinc\t%s+1", lbuf);
1719 g_defloclabel (label);
1720 AddCodeLine ("\tldx\t%s+1", lbuf);
1722 AddCodeLine ("\tlda\t#$%02X", (unsigned char)(val >> 8));
1723 AddCodeLine ("\tadc\t%s+1", lbuf);
1724 AddCodeLine ("\tsta\t%s+1", lbuf);
1725 AddCodeLine ("\ttax");
1726 AddCodeLine ("\tlda\t%s", lbuf);
1730 AddCodeLine ("\tclc");
1731 AddCodeLine ("\tadc\t%s", lbuf);
1732 AddCodeLine ("\tsta\t%s", lbuf);
1733 AddCodeLine ("\ttxa");
1734 AddCodeLine ("\tadc\t%s+1", lbuf);
1735 AddCodeLine ("\tsta\t%s+1", lbuf);
1736 AddCodeLine ("\ttax");
1737 AddCodeLine ("\tlda\t%s", lbuf);
1742 if (flags & CF_CONST) {
1744 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1745 AddCodeLine ("\tsty\tptr1");
1746 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1748 AddCodeLine ("\tjsr\tladdeq1");
1750 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1751 AddCodeLine ("\tjsr\tladdeqa");
1754 g_getstatic (flags, label, offs);
1756 g_putstatic (flags, label, offs);
1759 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1760 AddCodeLine ("\tsty\tptr1");
1761 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1762 AddCodeLine ("\tjsr\tladdeq");
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 AddCodeLine ("\tldx\t#$00");
1787 if (flags & CF_CONST) {
1788 AddCodeLine ("\tclc");
1789 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1790 AddCodeLine ("\tadc\t(sp,x)");
1791 AddCodeLine ("\tsta\t(sp,x)");
1793 AddCodeLine ("\tclc");
1794 AddCodeLine ("\tadc\t(sp,x)");
1795 AddCodeLine ("\tsta\t(sp,x)");
1799 AddCodeLine ("\tldx\t#$00");
1800 if (flags & CF_CONST) {
1801 AddCodeLine ("\tclc");
1802 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1803 AddCodeLine ("\tadc\t(sp),y");
1804 AddCodeLine ("\tsta\t(sp),y");
1806 AddCodeLine ("\tclc");
1807 AddCodeLine ("\tadc\t(sp),y");
1808 AddCodeLine ("\tsta\t(sp),y");
1811 if ((flags & CF_UNSIGNED) == 0) {
1812 AddCodeLine ("\tbpl\t*+3");
1813 AddCodeLine ("\tdex");
1814 AddCodeHint ("x:!"); /* Invalidate X */
1821 if (flags & CF_CONST) {
1822 g_getimmed (flags, val, 0);
1825 AddCodeLine ("\tjsr\taddeq0sp");
1828 AddCodeLine ("\tjsr\taddeqysp");
1833 if (flags & CF_CONST) {
1834 g_getimmed (flags, val, 0);
1837 AddCodeLine ("\tjsr\tladdeq0sp");
1840 AddCodeLine ("\tjsr\tladdeqysp");
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 AddCodeLine ("\tsta\tptr1");
1865 AddCodeLine ("\tstx\tptr1+1");
1867 AddCodeLine ("\tldx\t#$00");
1868 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1869 AddCodeLine ("\tclc");
1870 AddCodeLine ("\tadc\t(ptr1,x)");
1871 AddCodeLine ("\tsta\t(ptr1,x)");
1873 AddCodeLine ("\tldy\t#$%02X", offs);
1874 AddCodeLine ("\tldx\t#$00");
1875 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1876 AddCodeLine ("\tclc");
1877 AddCodeLine ("\tadc\t(ptr1),y");
1878 AddCodeLine ("\tsta\t(ptr1),y");
1883 if (CodeSizeFactor >= 200) {
1884 /* Lots of code, use only if size is not important */
1885 AddCodeLine ("\tsta\tptr1");
1886 AddCodeLine ("\tstx\tptr1+1");
1887 AddCodeLine ("\tldy\t#$%02X", offs);
1888 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1889 AddCodeLine ("\tclc");
1890 AddCodeLine ("\tadc\t(ptr1),y");
1891 AddCodeLine ("\tsta\t(ptr1),y");
1892 AddCodeLine ("\tpha");
1893 AddCodeLine ("\tiny");
1894 AddCodeLine ("\tlda\t#$%02X", (unsigned char)(val >> 8));
1895 AddCodeLine ("\tadc\t(ptr1),y");
1896 AddCodeLine ("\tsta\t(ptr1),y");
1897 AddCodeLine ("\ttax");
1898 AddCodeLine ("\tpla");
1904 AddCodeLine ("\tjsr\tpushax"); /* 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 AddCodeLine ("\tldx\t#$00");
1931 if (flags & CF_CONST) {
1933 AddCodeLine ("\tdec\t%s", lbuf);
1934 AddCodeLine ("\tlda\t%s", lbuf);
1936 AddCodeLine ("\tsec");
1937 AddCodeLine ("\tlda\t%s", lbuf);
1938 AddCodeLine ("\tsbc\t#$%02X", (int)(val & 0xFF));
1939 AddCodeLine ("\tsta\t%s", lbuf);
1942 AddCodeLine ("\tsec");
1943 AddCodeLine ("\tsta\ttmp1");
1944 AddCodeLine ("\tlda\t%s", lbuf);
1945 AddCodeLine ("\tsbc\ttmp1");
1946 AddCodeLine ("\tsta\t%s", lbuf);
1948 if ((flags & CF_UNSIGNED) == 0) {
1949 AddCodeLine ("\tbpl\t*+3");
1950 AddCodeLine ("\tdex");
1951 AddCodeHint ("x:!"); /* Invalidate X */
1958 AddCodeLine ("\tsec");
1959 if (flags & CF_CONST) {
1960 AddCodeLine ("\tlda\t%s", lbuf);
1961 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
1962 AddCodeLine ("\tsta\t%s", lbuf);
1964 label = GetLocalLabel ();
1965 AddCodeLine ("\tbcs\tL%04X", (unsigned)label);
1966 AddCodeLine ("\tdec\t%s+1", lbuf);
1967 g_defloclabel (label);
1968 AddCodeLine ("\tldx\t%s+1", lbuf);
1970 AddCodeLine ("\tlda\t%s+1", lbuf);
1971 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)(val >> 8));
1972 AddCodeLine ("\tsta\t%s+1", lbuf);
1973 AddCodeLine ("\ttax");
1974 AddCodeLine ("\tlda\t%s", lbuf);
1977 AddCodeLine ("\tsta\ttmp1");
1978 AddCodeLine ("\tlda\t%s", lbuf);
1979 AddCodeLine ("\tsbc\ttmp1");
1980 AddCodeLine ("\tsta\t%s", lbuf);
1981 AddCodeLine ("\tstx\ttmp1");
1982 AddCodeLine ("\tlda\t%s+1", lbuf);
1983 AddCodeLine ("\tsbc\ttmp1");
1984 AddCodeLine ("\tsta\t%s+1", lbuf);
1985 AddCodeLine ("\ttax");
1986 AddCodeLine ("\tlda\t%s", lbuf);
1991 if (flags & CF_CONST) {
1993 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1994 AddCodeLine ("\tsty\tptr1");
1995 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1997 AddCodeLine ("\tjsr\tlsubeq1");
1999 AddCodeLine ("\tlda\t#$%02X", (unsigned char)val);
2000 AddCodeLine ("\tjsr\tlsubeqa");
2003 g_getstatic (flags, label, offs);
2005 g_putstatic (flags, label, offs);
2008 AddCodeLine ("\tldy\t#<(%s)", lbuf);
2009 AddCodeLine ("\tsty\tptr1");
2010 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
2011 AddCodeLine ("\tjsr\tlsubeq");
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 AddCodeLine ("\tldx\t#$00");
2036 AddCodeLine ("\tsec");
2037 if (flags & CF_CONST) {
2038 AddCodeLine ("\tlda\t(sp),y");
2039 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2041 AddCodeLine ("\tsta\ttmp1");
2042 AddCodeLine ("\tlda\t(sp),y");
2043 AddCodeLine ("\tsbc\ttmp1");
2045 AddCodeLine ("\tsta\t(sp),y");
2046 if ((flags & CF_UNSIGNED) == 0) {
2047 AddCodeLine ("\tbpl\t*+3");
2048 AddCodeLine ("\tdex");
2049 AddCodeHint ("x:!"); /* Invalidate X */
2056 if (flags & CF_CONST) {
2057 g_getimmed (flags, val, 0);
2060 AddCodeLine ("\tjsr\tsubeq0sp");
2063 AddCodeLine ("\tjsr\tsubeqysp");
2068 if (flags & CF_CONST) {
2069 g_getimmed (flags, val, 0);
2072 AddCodeLine ("\tjsr\tlsubeq0sp");
2075 AddCodeLine ("\tjsr\tlsubeqysp");
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 AddCodeLine ("\tsta\tptr1");
2100 AddCodeLine ("\tstx\tptr1+1");
2102 AddCodeLine ("\tldx\t#$00");
2103 AddCodeLine ("\tlda\t(ptr1,x)");
2104 AddCodeLine ("\tsec");
2105 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2106 AddCodeLine ("\tsta\t(ptr1,x)");
2108 AddCodeLine ("\tldy\t#$%02X", offs);
2109 AddCodeLine ("\tldx\t#$00");
2110 AddCodeLine ("\tlda\t(ptr1),y");
2111 AddCodeLine ("\tsec");
2112 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2113 AddCodeLine ("\tsta\t(ptr1),y");
2118 if (CodeSizeFactor >= 200) {
2119 /* Lots of code, use only if size is not important */
2120 AddCodeLine ("\tsta\tptr1");
2121 AddCodeLine ("\tstx\tptr1+1");
2122 AddCodeLine ("\tldy\t#$%02X", offs);
2123 AddCodeLine ("\tlda\t(ptr1),y");
2124 AddCodeLine ("\tsec");
2125 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2126 AddCodeLine ("\tsta\t(ptr1),y");
2127 AddCodeLine ("\tpha");
2128 AddCodeLine ("\tiny");
2129 AddCodeLine ("\tlda\t(ptr1),y");
2130 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)(val >> 8));
2131 AddCodeLine ("\tsta\t(ptr1),y");
2132 AddCodeLine ("\ttax");
2133 AddCodeLine ("\tpla");
2139 AddCodeLine ("\tjsr\tpushax"); /* 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 AddCodeLine ("\tclc");
2168 AddCodeLine ("\tadc\t#$%02X", offs & 0xFF);
2169 AddCodeLine ("\tbcc\t*+4"); /* Do also skip the CLC insn below */
2170 AddCodeLine ("\tinx");
2171 AddCodeHint ("x:!"); /* Invalidate X */
2174 /* Add the current stackpointer value */
2175 AddCodeLine ("\tclc");
2176 AddCodeLine ("\tadc\tsp");
2177 AddCodeLine ("\ttay");
2178 AddCodeLine ("\ttxa");
2179 AddCodeLine ("\tadc\tsp+1");
2180 AddCodeLine ("\ttax");
2181 AddCodeLine ("\ttya");
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 AddCodeLine ("\tclc");
2194 AddCodeLine ("\tadc\t#<(%s)", lbuf);
2195 AddCodeLine ("\ttay");
2196 AddCodeLine ("\ttxa");
2197 AddCodeLine ("\tadc\t#>(%s)", lbuf);
2198 AddCodeLine ("\ttax");
2199 AddCodeLine ("\ttya");
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 AddCodeLine ("\tpha");
2224 AddCodeLine ("\tsta\tregsave");
2225 AddCodeLine ("\tstx\tregsave+1");
2229 AddCodeLine ("\tjsr\tsaveeax");
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 AddCodeLine ("\tpla");
2253 AddCodeLine ("\tlda\tregsave");
2254 AddCodeLine ("\tldx\tregsave+1");
2258 AddCodeLine ("\tjsr\tresteax");
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 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
2284 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
2285 AddCodeLine ("\tbne\t*+4");
2286 AddCodeLine ("\tcpx\t#$%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 AddCodeLine ("\tjsr\t%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 AddCodeLine ("\tjsr\t%s", subs [offs+1]);
2341 /* Others: arbitrary constant value */
2342 g_getimmed (flags, val, 0); /* Load value */
2343 AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2346 /* Value not constant (is already in (e)ax) */
2347 AddCodeLine ("\tjsr\t%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 AddCodeLine ("\ttax");
2369 AddCodeLine ("\tstx\ttmp1");
2370 AddCodeLine ("\tora\ttmp1");
2374 if (flags & CF_UNSIGNED) {
2375 AddCodeLine ("\tjsr\tutsteax");
2377 AddCodeLine ("\tjsr\ttsteax");
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 AddCodeLine ("\tjsr\tpusha");
2404 AddCodeLine ("\tjsr\tpushc%d", (int) val);
2409 /* Handle as 16 bit value */
2410 hi = (unsigned char) (val >> 8);
2412 AddCodeLine ("\tjsr\tpush%u", (unsigned) val);
2413 } else if (hi == 0 || hi == 0xFF) {
2414 /* Use special function */
2416 AddCodeLine ("\tjsr\t%s", (hi == 0)? "pusha0" : "pushaFF");
2419 g_getimmed (flags, val, 0);
2420 AddCodeLine ("\tjsr\tpushax");
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 AddCodeLine ("\tjsr\tpusha");
2443 AddCodeLine ("\tjsr\tpushax");
2447 AddCodeLine ("\tjsr\tpusheax");
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 AddCodeLine ("\tjsr\tswapstk");
2476 AddCodeLine ("\tjsr\tswapestk");
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 AddCodeLine ("\tjsr\t_%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 AddCodeLine ("\tjsr\tcallax"); /* do the call */
2508 oursp += ArgSize; /* callee pops args */
2513 void g_jump (unsigned Label)
2514 /* Jump to specified internal label number */
2516 AddCodeLine ("\tjmp\tL%04X", Label);
2521 void g_switch (unsigned Flags)
2522 /* Output switch statement preamble */
2524 switch (Flags & CF_TYPE) {
2528 AddCodeLine ("\tjsr\tswitch");
2532 AddCodeLine ("\tjsr\tlswitch");
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 AddCodeLine ("\t.word\t$%04X, L%04X",
2551 (unsigned)(val & 0xFFFF),
2552 (unsigned)(label & 0xFFFF));
2556 AddCodeLine ("\t.dword\t$%08lX", val);
2557 AddCodeLine ("\t.word\tL%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 AddCodeLine ("\tbne\tL%04X", label);
2574 AddCodeLine ("\tjne\tL%04X", label);
2580 void g_falsejump (unsigned flags, unsigned label)
2581 /* Jump to label if zero flag set */
2583 if (flags & CF_SHORT) {
2584 AddCodeLine ("\tbeq\tL%04X", label);
2586 AddCodeLine ("\tjeq\tL%04X", label);
2592 static void mod_internal (int k, char* verb1, char* verb2)
2595 AddCodeLine ("\tjsr\t%ssp%c", verb1, k + '0');
2599 AddCodeLine ("\tjsr\t%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 AddCodeLine ("\tjsr\tcstkchk");
2625 void g_stackcheck (void)
2626 /* Check for a stack overflow */
2628 AddCodeLine ("\tjsr\tstkchk");
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 AddCodeLine ("\tsta\ttmp1");
2718 AddCodeLine ("\tasl\ta");
2719 AddCodeLine ("\tclc");
2720 AddCodeLine ("\tadc\ttmp1");
2724 AddCodeLine ("\tsta\ttmp1");
2725 AddCodeLine ("\tasl\ta");
2726 AddCodeLine ("\tasl\ta");
2727 AddCodeLine ("\tclc");
2728 AddCodeLine ("\tadc\ttmp1");
2732 AddCodeLine ("\tsta\ttmp1");
2733 AddCodeLine ("\tasl\ta");
2734 AddCodeLine ("\tasl\ta");
2735 AddCodeLine ("\tclc");
2736 AddCodeLine ("\tadc\ttmp1");
2737 AddCodeLine ("\tasl\ta");
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 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2851 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2858 AddCodeLine ("\tora\t#$%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 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2910 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2913 } else if ((val & 0xFF) == 0) {
2914 AddCodeLine ("\tpha");
2915 AddCodeLine ("\ttxa");
2916 AddCodeLine ("\teor\t#$%02X", (unsigned char)(val >> 8));
2917 AddCodeLine ("\ttax");
2918 AddCodeLine ("\tpla");
2926 AddCodeLine ("\teor\t#$%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 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2973 if ((val & 0xFFFF) != 0xFFFF) {
2978 } else if (val != 0xFF) {
2979 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2981 } else if ((val & 0xFF00) == 0xFF00) {
2982 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2983 } else if ((val & 0x00FF) == 0x0000) {
2984 AddCodeLine ("\ttxa");
2985 AddCodeLine ("\tand\t#$%02X", (unsigned char)(val >> 8));
2986 AddCodeLine ("\ttax");
2989 AddCodeLine ("\ttay");
2990 AddCodeLine ("\ttxa");
2991 AddCodeLine ("\tand\t#$%02X", (unsigned char)(val >> 8));
2992 AddCodeLine ("\ttax");
2993 AddCodeLine ("\ttya");
2994 if ((val & 0x00FF) != 0x00FF) {
2995 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
3004 AddCodeLine ("\tstx\tsreg+1");
3005 AddCodeLine ("\tstx\tsreg");
3006 if ((val & 0xFF) != 0xFF) {
3007 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
3010 } else if (val == 0xFF00) {
3012 AddCodeLine ("\tsta\tsreg+1");
3013 AddCodeLine ("\tsta\tsreg");
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 AddCodeLine ("\tjsr\tshrax%ld", val);
3058 AddCodeLine ("\tjsr\tasrax%ld", val);
3061 } else if (val == 8 && (flags & CF_UNSIGNED)) {
3062 AddCodeLine ("\ttxa");
3069 if (val >= 1 && val <= 3) {
3070 if (flags & CF_UNSIGNED) {
3071 AddCodeLine ("\tjsr\tshreax%ld", val);
3073 AddCodeLine ("\tjsr\tasreax%ld", val);
3076 } else if (val == 8 && (flags & CF_UNSIGNED)) {
3077 AddCodeLine ("\ttxa");
3078 AddCodeLine ("\tldx\tsreg");
3079 AddCodeLine ("\tldy\tsreg+1");
3080 AddCodeLine ("\tsty\tsreg");
3081 AddCodeLine ("\tldy\t#$00");
3082 AddCodeLine ("\tsty\tsreg+1");
3084 } else if (val == 16) {
3085 AddCodeLine ("\tldy\t#$00");
3086 AddCodeLine ("\tldx\tsreg+1");
3087 if ((flags & CF_UNSIGNED) == 0) {
3088 AddCodeLine ("\tbpl\t*+3");
3089 AddCodeLine ("\tdey");
3090 AddCodeHint ("y:!");
3092 AddCodeLine ("\tlda\tsreg");
3093 AddCodeLine ("\tsty\tsreg+1");
3094 AddCodeLine ("\tsty\tsreg");
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 AddCodeLine ("\tjsr\tshlax%ld", val);
3140 AddCodeLine ("\tjsr\taslax%ld", val);
3143 } else if (val == 8) {
3144 AddCodeLine ("\ttax");
3145 AddCodeLine ("\tlda\t#$00");
3151 if (val >= 1 && val <= 3) {
3152 if (flags & CF_UNSIGNED) {
3153 AddCodeLine ("\tjsr\tshleax%ld", val);
3155 AddCodeLine ("\tjsr\tasleax%ld", val);
3158 } else if (val == 8) {
3159 AddCodeLine ("\tldy\tsreg");
3160 AddCodeLine ("\tsty\tsreg+1");
3161 AddCodeLine ("\tstx\tsreg");
3162 AddCodeLine ("\ttax");
3163 AddCodeLine ("\tlda\t#$00");
3165 } else if (val == 16) {
3166 AddCodeLine ("\tstx\tsreg+1");
3167 AddCodeLine ("\tsta\tsreg");
3168 AddCodeLine ("\tlda\t#$00");
3169 AddCodeLine ("\ttax");
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 AddCodeLine ("\tjsr\tnegax");
3202 AddCodeLine ("\tjsr\tnegeax");
3212 void g_bneg (unsigned flags)
3213 /* Primary = !Primary */
3215 switch (flags & CF_TYPE) {
3218 AddCodeLine ("\tjsr\tbnega");
3222 AddCodeLine ("\tjsr\tbnegax");
3226 AddCodeLine ("\tjsr\tbnegeax");
3236 void g_com (unsigned flags)
3237 /* Primary = ~Primary */
3239 switch (flags & CF_TYPE) {
3243 AddCodeLine ("\tjsr\tcomplax");
3247 AddCodeLine ("\tjsr\tcompleax");
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 AddCodeLine ("\tina");
3276 AddCodeLine ("\tclc");
3277 AddCodeLine ("\tadc\t#$%02X", (unsigned char)val);
3284 if (CPU == CPU_65C02 && val == 1) {
3285 AddCodeLine ("\tina");
3286 AddCodeLine ("\tbne\t*+3");
3287 AddCodeLine ("\tinx");
3288 /* Tell the optimizer that the X register may be invalid */
3289 AddCodeHint ("x:!");
3290 } else if (CodeSizeFactor < 200) {
3293 AddCodeLine ("\tjsr\tincax%lu", val);
3294 } else if (val <= 255) {
3296 AddCodeLine ("\tjsr\tincaxy");
3298 g_add (flags | CF_CONST, val);
3301 /* Inline the code */
3303 if ((val & 0xFF) != 0) {
3304 AddCodeLine ("\tclc");
3305 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3306 AddCodeLine ("\tbcc\t*+3");
3307 AddCodeLine ("\tinx");
3308 /* Tell the optimizer that the X register may be invalid */
3309 AddCodeHint ("x:!");
3312 AddCodeLine ("\tinx");
3315 AddCodeLine ("\tinx");
3318 AddCodeLine ("\tclc");
3319 if ((val & 0xFF) != 0) {
3320 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3321 /* Tell the optimizer that the X register may be invalid */
3322 AddCodeHint ("x:!");
3324 AddCodeLine ("\tpha");
3325 AddCodeLine ("\ttxa");
3326 AddCodeLine ("\tadc\t#$%02X", (unsigned char) (val >> 8));
3327 AddCodeLine ("\ttax");
3328 AddCodeLine ("\tpla");
3336 AddCodeLine ("\tjsr\tinceaxy");
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 AddCodeLine ("\tdea");
3369 AddCodeLine ("\tsec");
3370 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
3377 if (CodeSizeFactor < 200) {
3378 /* Use subroutines */
3380 AddCodeLine ("\tjsr\tdecax%d", (int) val);
3381 } else if (val <= 255) {
3383 AddCodeLine ("\tjsr\tdecaxy");
3385 g_sub (flags | CF_CONST, val);
3388 /* Inline the code */
3390 if ((val & 0xFF) != 0) {
3391 AddCodeLine ("\tsec");
3392 AddCodeLine ("\tsbc\t#$%02X", (unsigned char) val);
3393 AddCodeLine ("\tbcs\t*+3");
3394 AddCodeLine ("\tdex");
3395 /* Tell the optimizer that the X register may be invalid */
3396 AddCodeHint ("x:!");
3399 AddCodeLine ("\tdex");
3402 AddCodeLine ("\tdex");
3405 AddCodeLine ("\tsec");
3406 if ((val & 0xFF) != 0) {
3407 AddCodeLine ("\tsbc\t#$%02X", (unsigned char) val);
3408 /* Tell the optimizer that the X register may be invalid */
3409 AddCodeHint ("x:!");
3411 AddCodeLine ("\tpha");
3412 AddCodeLine ("\ttxa");
3413 AddCodeLine ("\tsbc\t#$%02X", (unsigned char) (val >> 8));
3414 AddCodeLine ("\ttax");
3415 AddCodeLine ("\tpla");
3423 AddCodeLine ("\tjsr\tdeceaxy");
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 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3465 AddCodeLine ("\tjsr\tbooleq");
3471 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3472 AddCodeLine ("\tbne\t*+4");
3473 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3474 AddCodeLine ("\tjsr\tbooleq");
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 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3518 AddCodeLine ("\tjsr\tboolne");
3524 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3525 AddCodeLine ("\tbne\t*+4");
3526 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3527 AddCodeLine ("\tjsr\tboolne");
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 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3576 if (flags & CF_UNSIGNED) {
3577 AddCodeLine ("\tjsr\tboolult");
3579 AddCodeLine ("\tjsr\tboollt");
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 AddCodeLine ("\ttxa");
3591 AddCodeLine ("\tjsr\tboollt");
3594 /* Direct code only for unsigned data types */
3595 if (flags & CF_UNSIGNED) {
3596 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3597 AddCodeLine ("\tbne\t*+4");
3598 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3599 AddCodeLine ("\tjsr\tboolult");
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 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3646 if (flags & CF_UNSIGNED) {
3647 AddCodeLine ("\tjsr\tboolule");
3649 AddCodeLine ("\tjsr\tboolle");
3656 if (flags & CF_UNSIGNED) {
3657 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3658 AddCodeLine ("\tbne\t*+4");
3659 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3660 AddCodeLine ("\tjsr\tboolule");
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 AddCodeLine ("\tcmp\t#$%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 AddCodeLine ("\tjsr\tboolugt");
3715 AddCodeLine ("\tjsr\tboolne");
3718 AddCodeLine ("\tjsr\tboolgt");
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 AddCodeLine ("\tstx\ttmp1");
3732 AddCodeLine ("\tora\ttmp1");
3733 AddCodeLine ("\tjsr\tboolne");
3735 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3736 AddCodeLine ("\tbne\t*+4");
3737 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3738 AddCodeLine ("\tjsr\tboolugt");
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 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3791 if (flags & CF_UNSIGNED) {
3792 AddCodeLine ("\tjsr\tbooluge");
3794 AddCodeLine ("\tjsr\tboolge");
3801 if (flags & CF_UNSIGNED) {
3802 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3803 AddCodeLine ("\tbne\t*+4");
3804 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3805 AddCodeLine ("\tjsr\tbooluge");
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 AddCodeLine ("\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 AddCodeLine ("\t.byte\t$%02lX", val & 0xFF);
3857 AddCodeLine ("\t.word\t$%04lX", val & 0xFFFF);
3861 AddCodeLine ("\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 AddCodeLine ("\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 AddCodeLine ("\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 AddCodeLine ("\tldy\t#$FF");
3967 g_defloclabel (label);
3968 AddCodeLine ("\tiny");
3969 AddCodeLine ("\tlda\t%s,y", lbuf);
3970 AddCodeLine ("\tbne\tL%04X", label);
3971 AddCodeLine ("\ttax");
3972 AddCodeLine ("\ttya");
3976 /* Address not constant but in primary */
3977 if (CodeSizeFactor < 400) {
3978 /* This is too much code, so call strlen instead of inlining */
3979 AddCodeLine ("\tjsr\t_strlen");
3981 /* Inline the function */
3982 AddCodeLine ("\tsta\tptr1");
3983 AddCodeLine ("\tstx\tptr1+1");
3984 AddCodeLine ("\tldy\t#$FF");
3985 g_defloclabel (label);
3986 AddCodeLine ("\tiny");
3987 AddCodeLine ("\tlda\t(ptr1),y");
3988 AddCodeLine ("\tbne\tL%04X", label);
3989 AddCodeLine ("\ttax");
3990 AddCodeLine ("\ttya");