1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2000 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 stk ptr */
68 segment_t CurSeg = SEG_INV;
71 static char* SegmentHints [4] = {
72 "seg:code", "seg:rodata", "seg:data", "seg:bss"
77 /*****************************************************************************/
79 /*****************************************************************************/
83 static void typeerror (unsigned type)
84 /* Print an error message about an invalid operand type */
86 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
91 static void CheckLocalOffs (unsigned Offs)
92 /* Check the offset into the stack for 8bit range */
95 /* Too many local vars */
96 AddCodeLine (";*** Too many locals");
97 Error ("Too many local variables");
103 static char* GetLabelName (unsigned flags, unsigned long label, unsigned offs)
105 static char lbuf [128]; /* Label name */
107 /* Create the correct label name */
108 switch (flags & CF_ADDRMASK) {
111 /* Static memory cell */
112 sprintf (lbuf, "L%04X+%u", (unsigned)(label & 0xFFFF), offs);
117 sprintf (lbuf, "_%s+%u", (char*) label, offs);
121 /* Absolute address */
122 sprintf (lbuf, "$%04X", (unsigned)((label+offs) & 0xFFFF));
126 /* Variable in register bank */
127 sprintf (lbuf, "regbank+%u", (unsigned)((label+offs) & 0xFFFF));
131 Internal ("Invalid address flags");
134 /* Return a pointer to the static buffer */
140 /*****************************************************************************/
141 /* Pre- and postamble */
142 /*****************************************************************************/
146 void g_preamble (void)
147 /* Generate the assembler code preamble */
149 AddCodeLine ("; File generated by cc65 v %u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
152 /* Insert some object file options */
153 AddCodeLine (".fopt\t\tcompiler,\"cc65 v %u.%u.%u\"", VER_MAJOR, VER_MINOR, VER_PATCH);
156 /* If we're producing code for some other CPU, switch the command set */
157 if (CPU == CPU_65C02) {
158 AddCodeLine (".pc02");
161 /* Allow auto import for runtime library routines */
162 AddCodeLine (".autoimport\ton");
164 /* Switch the assembler into case sensitive mode */
165 AddCodeLine (".case\t\ton");
167 /* Tell the assembler if we want to generate debug info */
168 AddCodeLine (".debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
170 /* Import the stack pointer for direct auto variable access */
171 AddCodeLine (".importzp\tsp, sreg, regsave, regbank, tmp1, ptr1");
173 /* Define long branch macros */
174 AddCodeLine (".macpack\tlongbranch");
177 /* Define the ldax macro */
178 AddCodeLine (".macro ldax Value");
179 AddCodeLine (" lda #<(Value)");
180 AddCodeLine (" ldx #>(Value)");
181 AddCodeLine (".endmacro");
184 /* Tell the optimizer that this is the end of the preamble */
185 AddCodeHint ("end_of_preamble");
190 void g_postamble (void)
191 /* Generate assembler code postamble */
193 /* Tell the optimizer that this is the start of the postamble */
194 AddCodeHint ("start_of_postamble");
199 /*****************************************************************************/
200 /* Segment support */
201 /*****************************************************************************/
205 static void UseSeg (int NewSeg)
206 /* Switch to a specific segment */
208 if (CurSeg != NewSeg) {
210 AddCodeLine (".segment\t\"%s\"", SegmentNames [CurSeg]);
211 AddCodeHint (SegmentHints [CurSeg]);
217 void g_usecode (void)
218 /* Switch to the code segment */
225 void g_userodata (void)
226 /* Switch to the read only data segment */
233 void g_usedata (void)
234 /* Switch to the data segment */
242 /* Switch to the bss segment */
249 static void SegName (segment_t Seg, const char* Name)
250 /* Set the name of a segment */
252 /* Free the old name and set a new one */
253 NewSegName (Seg, Name);
255 /* If the new segment is the current segment, emit a segment directive
259 CurSeg = SEG_INV; /* Invalidate */
266 void g_codename (const char* Name)
267 /* Set the name of the CODE segment */
269 SegName (SEG_CODE, Name);
274 void g_rodataname (const char* Name)
275 /* Set the name of the RODATA segment */
277 SegName (SEG_RODATA, Name);
282 void g_dataname (const char* Name)
283 /* Set the name of the DATA segment */
285 SegName (SEG_DATA, Name);
290 void g_bssname (const char* Name)
291 /* Set the name of the BSS segment */
293 SegName (SEG_BSS, Name);
298 /*****************************************************************************/
300 /*****************************************************************************/
304 unsigned sizeofarg (unsigned flags)
305 /* Return the size of a function argument type that is encoded in flags */
307 switch (flags & CF_TYPE) {
310 return (flags & CF_FORCECHAR)? 1 : 2;
327 int pop (unsigned flags)
328 /* Pop an argument of the given size */
330 return oursp += sizeofarg (flags);
335 int push (unsigned flags)
336 /* Push an argument of the given size */
338 return oursp -= sizeofarg (flags);
343 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
344 /* The value in Offs is an offset to an address in a/x. Make sure, an object
345 * of the type given in Flags can be loaded or stored into this address by
346 * adding part of the offset to the address in ax, so that the remaining
347 * offset fits into an index register. Return the remaining offset.
350 /* If the offset is too large for a byte register, add the high byte
351 * of the offset to the primary. Beware: We need a special correction
352 * if the offset in the low byte will overflow in the operation.
354 unsigned O = Offs & ~0xFFU;
355 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
356 /* We need to add the low byte also */
360 /* Do the correction if we need one */
362 g_inc (CF_INT | CF_CONST, O);
366 /* Return the new offset */
372 /*****************************************************************************/
373 /* Functions handling local labels */
374 /*****************************************************************************/
378 void g_defloclabel (unsigned label)
379 /* Define a local label */
381 AddCodeLine ("L%04X:", label & 0xFFFF);
386 /*****************************************************************************/
387 /* Functions handling global labels */
388 /*****************************************************************************/
392 void g_defgloblabel (const char* Name)
393 /* Define a global label with the given name */
395 AddCodeLine ("_%s:", Name);
400 void g_defexport (const char* Name, int ZP)
401 /* Export the given label */
404 AddCodeLine ("\t.exportzp\t_%s", Name);
406 AddCodeLine ("\t.export\t\t_%s", Name);
412 void g_defimport (const char* Name, int ZP)
413 /* Import the given label */
416 AddCodeLine ("\t.importzp\t_%s", Name);
418 AddCodeLine ("\t.import\t\t_%s", Name);
424 /*****************************************************************************/
425 /* Load functions for various registers */
426 /*****************************************************************************/
430 static void ldaconst (unsigned val)
431 /* Load a with a constant */
433 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
438 static void ldxconst (unsigned val)
439 /* Load x with a constant */
441 AddCodeLine ("\tldx\t#$%02X", val & 0xFF);
446 static void ldyconst (unsigned val)
447 /* Load y with a constant */
449 AddCodeLine ("\tldy\t#$%02X", val & 0xFF);
454 /*****************************************************************************/
455 /* Function entry and exit */
456 /*****************************************************************************/
460 /* Remember the argument size of a function. The variable is set by g_enter
461 * and used by g_leave. If the functions gets its argument size by the caller
462 * (variable param list or function without prototype), g_enter will set the
468 void g_enter (unsigned flags, unsigned argsize)
469 /* Function prologue */
471 if ((flags & CF_FIXARGC) != 0) {
472 /* Just remember the argument size for the leave */
476 AddCodeLine ("\tjsr\tenter");
482 void g_leave (int flags, int val)
483 /* Function epilogue */
488 /* How many bytes of locals do we have to drop? */
491 /* If we didn't have a variable argument list, don't call leave */
494 /* Load a function return code if needed */
495 if ((flags & CF_CONST) != 0) {
496 g_getimmed (flags, val, 0);
499 /* Drop stackframe or leave with rts */
502 AddCodeLine ("\trts");
504 AddCodeLine ("\tjmp\tincsp%d", k);
508 AddCodeLine ("\tjmp\taddysp");
513 strcpy (buf, "\tjmp\tleave");
515 /* We've a stack frame to drop */
519 if (flags & CF_CONST) {
520 if ((flags & CF_TYPE) != CF_LONG) {
521 /* Constant int sized value given for return code */
523 /* Special case: return 0 */
525 } else if (((val >> 8) & 0xFF) == 0) {
526 /* Special case: constant with high byte zero */
527 ldaconst (val); /* Load low byte */
530 /* Others: arbitrary constant value */
531 g_getimmed (flags, val, 0); /* Load value */
534 /* Constant long value: No shortcut possible */
535 g_getimmed (flags, val, 0);
539 /* Output the jump */
543 /* Add an empty line after a function to make the code more readable */
549 /*****************************************************************************/
550 /* Register variables */
551 /*****************************************************************************/
555 void g_save_regvars (int RegOffs, unsigned Bytes)
556 /* Save register variables */
558 /* Don't loop for up to two bytes */
561 AddCodeLine ("\tlda\tregbank%+d", RegOffs);
562 AddCodeLine ("\tjsr\tpusha");
564 } else if (Bytes == 2) {
566 AddCodeLine ("\tlda\tregbank%+d", RegOffs);
567 AddCodeLine ("\tldx\tregbank%+d", RegOffs+1);
568 AddCodeLine ("\tjsr\tpushax");
572 /* More than two bytes - loop */
573 unsigned Label = GetLabel ();
575 ldyconst (Bytes - 1);
577 g_defloclabel (Label);
578 AddCodeLine ("\tlda\tregbank%+d,x", RegOffs-1);
579 AddCodeLine ("\tsta\t(sp),y");
580 AddCodeLine ("\tdey");
581 AddCodeLine ("\tdex");
582 AddCodeLine ("\tbne\tL%04X", Label);
586 /* We pushed stuff, correct the stack pointer */
592 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
593 /* Restore register variables */
595 /* Calculate the actual stack offset and check it */
597 CheckLocalOffs (StackOffs);
599 /* Don't loop for up to two bytes */
602 ldyconst (StackOffs);
603 AddCodeLine ("\tlda\t(sp),y");
604 AddCodeLine ("\tsta\tregbank%+d", RegOffs);
606 } else if (Bytes == 2) {
608 ldyconst (StackOffs);
609 AddCodeLine ("\tlda\t(sp),y");
610 AddCodeLine ("\tsta\tregbank%+d", RegOffs);
611 AddCodeLine ("\tiny");
612 AddCodeLine ("\tlda\t(sp),y");
613 AddCodeLine ("\tsta\tregbank%+d", RegOffs+1);
617 /* More than two bytes - loop */
618 unsigned Label = GetLabel ();
619 ldyconst (StackOffs+Bytes-1);
621 g_defloclabel (Label);
622 AddCodeLine ("\tlda\t(sp),y");
623 AddCodeLine ("\tsta\tregbank%+d,x", RegOffs-1);
624 AddCodeLine ("\tdey");
625 AddCodeLine ("\tdex");
626 AddCodeLine ("\tbne\tL%04X", Label);
633 /*****************************************************************************/
634 /* Fetching memory cells */
635 /*****************************************************************************/
639 void g_getimmed (unsigned flags, unsigned long val, unsigned offs)
640 /* Load a constant into the primary register */
642 if ((flags & CF_CONST) != 0) {
644 /* Numeric constant */
645 switch (flags & CF_TYPE) {
648 if ((flags & CF_FORCECHAR) != 0) {
654 ldxconst ((val >> 8) & 0xFF);
655 ldaconst (val & 0xFF);
660 AddCodeLine ("\tldx\t#$00");
661 AddCodeLine ("\tstx\tsreg+1");
662 AddCodeLine ("\tstx\tsreg");
663 AddCodeLine ("\tlda\t#$%02X", (unsigned char) val);
664 } else if ((val & 0xFFFF00FF) == 0) {
665 AddCodeLine ("\tlda\t#$00");
666 AddCodeLine ("\tsta\tsreg+1");
667 AddCodeLine ("\tsta\tsreg");
668 AddCodeLine ("\tldx\t#$%02X", (unsigned char) (val >> 8));
669 } else if ((val & 0xFFFF0000) == 0 && FavourSize == 0) {
670 AddCodeLine ("\tlda\t#$00");
671 AddCodeLine ("\tsta\tsreg+1");
672 AddCodeLine ("\tsta\tsreg");
673 AddCodeLine ("\tlda\t#$%02X", (unsigned char) val);
674 AddCodeLine ("\tldx\t#$%02X", (unsigned char) (val >> 8));
675 } else if ((val & 0xFFFFFF00) == 0xFFFFFF00) {
676 AddCodeLine ("\tldx\t#$FF");
677 AddCodeLine ("\tstx\tsreg+1");
678 AddCodeLine ("\tstx\tsreg");
679 if ((val & 0xFF) == 0xFF) {
680 AddCodeLine ("\ttxa");
682 AddCodeLine ("\tlda\t#$%02X", (unsigned char) val);
684 } else if ((val & 0xFFFF00FF) == 0xFFFF00FF) {
685 AddCodeLine ("\tlda\t#$FF");
686 AddCodeLine ("\tsta\tsreg+1");
687 AddCodeLine ("\tsta\tsreg");
688 AddCodeLine ("\tldx\t#$%02X", (unsigned char) (val >> 8));
690 /* Call a subroutine that will load following value */
691 AddCodeLine ("\tjsr\tldeax");
692 AddCodeLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
704 /* Some sort of label */
705 const char* Label = GetLabelName (flags, val, offs);
707 /* Load the address into the primary */
708 AddCodeLine ("\tldax\t%s", Label);
715 void g_getstatic (unsigned flags, unsigned long label, unsigned offs)
716 /* Fetch an static memory cell into the primary register */
718 /* Create the correct label name */
719 char* lbuf = GetLabelName (flags, label, offs);
721 /* Check the size and generate the correct load operation */
722 switch (flags & CF_TYPE) {
725 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
726 AddCodeLine ("\tlda\t%s", lbuf); /* load A from the label */
729 AddCodeLine ("\tlda\t%s", lbuf); /* load A from the label */
730 if (!(flags & CF_UNSIGNED)) {
731 /* Must sign extend */
732 AddCodeLine ("\tbpl\t*+3");
733 AddCodeLine ("\tdex");
734 AddCodeHint ("x:!"); /* X is invalid now */
740 AddCodeLine ("\tlda\t%s", lbuf);
741 if (flags & CF_TEST) {
742 AddCodeLine ("\tora\t%s+1", lbuf);
744 AddCodeLine ("\tldx\t%s+1", lbuf);
749 if (flags & CF_TEST) {
750 AddCodeLine ("\tlda\t%s+3", lbuf);
751 AddCodeLine ("\tora\t%s+2", lbuf);
752 AddCodeLine ("\tora\t%s+1", lbuf);
753 AddCodeLine ("\tora\t%s+0", lbuf);
755 AddCodeLine ("\tlda\t%s+3", lbuf);
756 AddCodeLine ("\tsta\tsreg+1");
757 AddCodeLine ("\tlda\t%s+2", lbuf);
758 AddCodeLine ("\tsta\tsreg");
759 AddCodeLine ("\tldx\t%s+1", lbuf);
760 AddCodeLine ("\tlda\t%s", lbuf);
772 void g_getlocal (unsigned flags, int offs)
773 /* Fetch specified local object (local var). */
776 CheckLocalOffs (offs);
777 switch (flags & CF_TYPE) {
780 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
781 if (CPU == CPU_65C02 && offs == 0) {
782 AddCodeLine ("\tlda\t(sp)");
785 AddCodeLine ("\tlda\t(sp),y");
789 AddCodeLine ("\tldx\t#$00");
790 AddCodeLine ("\tlda\t(sp,x)");
793 AddCodeLine ("\tldx\t#$00");
794 AddCodeLine ("\tlda\t(sp),y");
796 if ((flags & CF_UNSIGNED) == 0) {
797 AddCodeLine ("\tbpl\t*+3");
798 AddCodeLine ("\tdex");
799 AddCodeHint ("x:!"); /* X is invalid now */
805 CheckLocalOffs (offs + 1);
806 if (flags & CF_TEST) {
808 AddCodeLine ("\tlda\t(sp),y");
809 AddCodeLine ("\tdey");
810 AddCodeLine ("\tora\t(sp),y");
815 AddCodeLine ("\tjsr\tldaxysp");
817 AddCodeLine ("\tjsr\tldax0sp");
821 AddCodeLine ("\tlda\t(sp),y");
822 AddCodeLine ("\ttax");
823 AddCodeLine ("\tdey");
824 AddCodeLine ("\tlda\t(sp),y");
832 AddCodeLine ("\tjsr\tldeaxysp");
834 AddCodeLine ("\tjsr\tldeax0sp");
845 void g_getind (unsigned flags, unsigned offs)
846 /* Fetch the specified object type indirect through the primary register
847 * into the primary register
850 /* If the offset is greater than 255, add the part that is > 255 to
851 * the primary. This way we get an easy addition and use the low byte
854 offs = MakeByteOffs (flags, offs);
856 /* Handle the indirect fetch */
857 switch (flags & CF_TYPE) {
860 /* Character sized */
863 if (flags & CF_UNSIGNED) {
864 AddCodeLine ("\tjsr\tldauidx");
866 AddCodeLine ("\tjsr\tldaidx");
869 if (flags & CF_UNSIGNED) {
871 AddCodeLine ("\tjsr\tldaui");
873 AddCodeLine ("\tsta\tptr1");
874 AddCodeLine ("\tstx\tptr1+1");
875 AddCodeLine ("\tldx\t#$00");
876 AddCodeLine ("\tlda\t(ptr1,x)");
879 AddCodeLine ("\tjsr\tldai");
885 if (flags & CF_TEST) {
887 AddCodeLine ("\tsta\tptr1");
888 AddCodeLine ("\tstx\tptr1+1");
889 AddCodeLine ("\tlda\t(ptr1),y");
890 AddCodeLine ("\tiny");
891 AddCodeLine ("\tora\t(ptr1),y");
894 AddCodeLine ("\tjsr\tldaxi");
897 AddCodeLine ("\tjsr\tldaxidx");
904 AddCodeLine ("\tjsr\tldeaxi");
907 AddCodeLine ("\tjsr\tldeaxidx");
909 if (flags & CF_TEST) {
910 AddCodeLine ("\tjsr\ttsteax");
922 void g_leasp (int offs)
923 /* Fetch the address of the specified symbol into the primary register */
925 /* Calculate the offset relative to sp */
928 /* For value 0 we do direct code */
930 AddCodeLine ("\tlda\tsp");
931 AddCodeLine ("\tldx\tsp+1");
934 ldaconst (offs); /* Load A with offset value */
935 AddCodeLine ("\tjsr\tleaasp"); /* Load effective address */
937 if (CPU == CPU_65C02 && offs == 1) {
938 AddCodeLine ("\tlda\tsp");
939 AddCodeLine ("\tldx\tsp+1");
940 AddCodeLine ("\tina");
941 AddCodeLine ("\tbne\t*+3");
942 AddCodeLine ("\tinx");
943 AddCodeHint ("x:!"); /* Invalidate X */
946 AddCodeLine ("\tclc");
947 AddCodeLine ("\tldx\tsp+1");
948 AddCodeLine ("\tadc\tsp");
949 AddCodeLine ("\tbcc\t*+3");
950 AddCodeLine ("\tinx");
951 AddCodeHint ("x:!"); /* Invalidate X */
959 /*****************************************************************************/
960 /* Store into memory */
961 /*****************************************************************************/
965 void g_putstatic (unsigned flags, unsigned long label, unsigned offs)
966 /* Store the primary register into the specified static memory cell */
968 /* Create the correct label name */
969 char* lbuf = GetLabelName (flags, label, offs);
971 /* Check the size and generate the correct store operation */
972 switch (flags & CF_TYPE) {
975 AddCodeLine ("\tsta\t%s", lbuf);
979 AddCodeLine ("\tsta\t%s", lbuf);
980 AddCodeLine ("\tstx\t%s+1", lbuf);
984 AddCodeLine ("\tsta\t%s", lbuf);
985 AddCodeLine ("\tstx\t%s+1", lbuf);
986 AddCodeLine ("\tldy\tsreg");
987 AddCodeLine ("\tsty\t%s+2", lbuf);
988 AddCodeLine ("\tldy\tsreg+1");
989 AddCodeLine ("\tsty\t%s+3", lbuf);
1000 void g_putlocal (unsigned flags, int offs)
1001 /* Put data into local object. */
1004 CheckLocalOffs (offs);
1005 switch (flags & CF_TYPE) {
1008 if (CPU == CPU_65C02 && offs == 0) {
1009 AddCodeLine ("\tsta\t(sp)");
1012 AddCodeLine ("\tsta\t(sp),y");
1019 AddCodeLine ("\tjsr\tstaxysp");
1021 AddCodeLine ("\tjsr\tstax0sp");
1028 AddCodeLine ("\tjsr\tsteaxysp");
1030 AddCodeLine ("\tjsr\tsteax0sp");
1042 void g_putind (unsigned Flags, unsigned Offs)
1043 /* Store the specified object type in the primary register at the address
1044 * on the top of the stack
1047 /* We can handle offsets below $100 directly, larger offsets must be added
1048 * to the address. Since a/x is in use, best code is achieved by adding
1049 * just the high byte. Be sure to check if the low byte will overflow while
1052 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1054 /* Overflow - we need to add the low byte also */
1055 AddCodeLine ("\tldy\t#$00");
1056 AddCodeLine ("\tclc");
1057 AddCodeLine ("\tpha");
1058 AddCodeLine ("\tlda\t#$%02X", Offs & 0xFF);
1059 AddCodeLine ("\tadc\t(sp),y");
1060 AddCodeLine ("\tsta\t(sp),y");
1061 AddCodeLine ("\tiny");
1062 AddCodeLine ("\tlda\t#$%02X", (Offs >> 8) & 0xFF);
1063 AddCodeLine ("\tadc\t(sp),y");
1064 AddCodeLine ("\tsta\t(sp),y");
1065 AddCodeLine ("\tpla");
1067 /* Complete address is on stack, new offset is zero */
1070 } else if ((Offs & 0xFF00) != 0) {
1072 /* We can just add the high byte */
1073 AddCodeLine ("\tldy\t#$01");
1074 AddCodeLine ("\tclc");
1075 AddCodeLine ("\tpha");
1076 AddCodeLine ("\tlda\t#$%02X", (Offs >> 8) & 0xFF);
1077 AddCodeLine ("\tadc\t(sp),y");
1078 AddCodeLine ("\tsta\t(sp),y");
1079 AddCodeLine ("\tpla");
1081 /* Offset is now just the low byte */
1085 /* Check the size and determine operation */
1086 switch (Flags & CF_TYPE) {
1091 AddCodeLine ("\tjsr\tstaspidx");
1093 AddCodeLine ("\tjsr\tstaspp");
1100 AddCodeLine ("\tjsr\tstaxspidx");
1102 AddCodeLine ("\tjsr\tstaxspp");
1109 AddCodeLine ("\tjsr\tsteaxspidx");
1111 AddCodeLine ("\tjsr\tsteaxspp");
1120 /* Pop the argument which is always a pointer */
1126 /*****************************************************************************/
1127 /* type conversion and similiar stuff */
1128 /*****************************************************************************/
1132 void g_toslong (unsigned flags)
1133 /* Make sure, the value on TOS is a long. Convert if necessary */
1135 switch (flags & CF_TYPE) {
1139 if (flags & CF_UNSIGNED) {
1140 AddCodeLine ("\tjsr\ttosulong");
1142 AddCodeLine ("\tjsr\ttoslong");
1157 void g_tosint (unsigned flags)
1158 /* Make sure, the value on TOS is an int. Convert if necessary */
1160 switch (flags & CF_TYPE) {
1167 AddCodeLine ("\tjsr\ttosint");
1178 void g_reglong (unsigned flags)
1179 /* Make sure, the value in the primary register a long. Convert if necessary */
1181 switch (flags & CF_TYPE) {
1185 if (flags & CF_UNSIGNED) {
1187 AddCodeLine ("\tjsr\taxulong");
1190 AddCodeLine ("\tsty\tsreg");
1191 AddCodeLine ("\tsty\tsreg+1");
1194 AddCodeLine ("\tjsr\taxlong");
1208 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1209 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1210 * value, that corresponds to the value on TOS, rhs corresponds to the value
1211 * in (e)ax. The return value is the the flags value for the resulting type.
1214 unsigned ltype, rtype;
1217 /* Get the type spec from the flags */
1218 ltype = lhs & CF_TYPE;
1219 rtype = rhs & CF_TYPE;
1221 /* Check if a conversion is needed */
1222 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1223 /* We must promote the primary register to long */
1225 /* Get the new rhs type */
1226 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1228 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1229 /* We must promote the lhs to long */
1235 /* Get the new rhs type */
1236 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1240 /* Determine the result type for the operation:
1241 * - The result is const if both operands are const.
1242 * - The result is unsigned if one of the operands is unsigned.
1243 * - The result is long if one of the operands is long.
1244 * - Otherwise the result is int sized.
1246 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1247 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1248 if (rtype == CF_LONG || ltype == CF_LONG) {
1258 unsigned g_typecast (unsigned lhs, unsigned rhs)
1259 /* Cast the value in the primary register to the operand size that is flagged
1260 * by the lhs value. Return the result value.
1263 unsigned ltype, rtype;
1265 /* Get the type spec from the flags */
1266 ltype = lhs & CF_TYPE;
1267 rtype = rhs & CF_TYPE;
1269 /* Check if a conversion is needed */
1270 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1271 /* We must promote the primary register to long */
1275 /* Do not need any other action. If the left type is int, and the primary
1276 * register is long, it will be automagically truncated. If the right hand
1277 * side is const, it is not located in the primary register and handled by
1278 * the expression parser code.
1281 /* Result is const if the right hand side was const */
1282 lhs |= (rhs & CF_CONST);
1284 /* The resulting type is that of the left hand side (that's why you called
1292 void g_scale (unsigned flags, long val)
1293 /* Scale the value in the primary register by the given value. If val is positive,
1294 * scale up, is val is negative, scale down. This function is used to scale
1295 * the operands or results of pointer arithmetic by the size of the type, the
1296 * pointer points to.
1301 /* Value may not be zero */
1303 Internal ("Data type has no size");
1304 } else if (val > 0) {
1307 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1309 /* Factor is 2, 4 or 8, use special function */
1310 switch (flags & CF_TYPE) {
1313 if (flags & CF_FORCECHAR) {
1315 AddCodeLine ("\tasl\ta");
1322 if (FavourSize || p2 >= 3) {
1323 if (flags & CF_UNSIGNED) {
1324 AddCodeLine ("\tjsr\tshlax%d", p2);
1326 AddCodeLine ("\tjsr\taslax%d", p2);
1329 AddCodeLine ("\tstx\ttmp1");
1331 AddCodeLine ("\tasl\ta");
1332 AddCodeLine ("\trol\ttmp1");
1334 AddCodeLine ("\tldx\ttmp1");
1339 if (flags & CF_UNSIGNED) {
1340 AddCodeLine ("\tjsr\tshleax%d", p2);
1342 AddCodeLine ("\tjsr\tasleax%d", p2);
1351 } else if (val != 1) {
1353 /* Use a multiplication instead */
1354 g_mul (flags | CF_CONST, val);
1362 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1364 /* Factor is 2, 4 or 8, use special function */
1365 switch (flags & CF_TYPE) {
1368 if (flags & CF_FORCECHAR) {
1369 if (flags & CF_UNSIGNED) {
1371 AddCodeLine ("\tlsr\ta");
1374 } else if (p2 <= 2) {
1375 AddCodeLine ("\tcmp\t#$80");
1376 AddCodeLine ("\tror\ta");
1383 if (flags & CF_UNSIGNED) {
1384 if (FavourSize || p2 >= 3) {
1385 AddCodeLine ("\tjsr\tlsrax%d", p2);
1387 AddCodeLine ("\tstx\ttmp1");
1389 AddCodeLine ("\tlsr\ttmp1");
1390 AddCodeLine ("\tror\ta");
1392 AddCodeLine ("\tldx\ttmp1");
1395 if (FavourSize || p2 >= 3) {
1396 AddCodeLine ("\tjsr\tasrax%d", p2);
1398 AddCodeLine ("\tstx\ttmp1");
1400 AddCodeLine ("\tcpx\t#$80");
1401 AddCodeLine ("\tror\ttmp1");
1402 AddCodeLine ("\tror\ta");
1404 AddCodeLine ("\tldx\ttmp1");
1410 if (flags & CF_UNSIGNED) {
1411 AddCodeLine ("\tjsr\tlsreax%d", p2);
1413 AddCodeLine ("\tjsr\tasreax%d", p2);
1422 } else if (val != 1) {
1424 /* Use a division instead */
1425 g_div (flags | CF_CONST, val);
1433 /*****************************************************************************/
1434 /* Adds and subs of variables fix a fixed address */
1435 /*****************************************************************************/
1439 void g_addlocal (unsigned flags, int offs)
1440 /* Add a local variable to ax */
1442 /* Correct the offset and check it */
1444 CheckLocalOffs (offs);
1446 switch (flags & CF_TYPE) {
1449 AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1450 AddCodeLine ("\tclc");
1451 AddCodeLine ("\tadc\t(sp),y");
1452 AddCodeLine ("\tbcc\t*+3");
1453 AddCodeLine ("\tinx");
1454 AddCodeHint ("x:!");
1458 AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1459 AddCodeLine ("\tclc");
1460 AddCodeLine ("\tadc\t(sp),y");
1461 AddCodeLine ("\tpha");
1462 AddCodeLine ("\ttxa");
1463 AddCodeLine ("\tiny");
1464 AddCodeLine ("\tadc\t(sp),y");
1465 AddCodeLine ("\ttax");
1466 AddCodeLine ("\tpla");
1470 /* Do it the old way */
1472 g_getlocal (flags, offs);
1484 void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
1485 /* Add a static variable to ax */
1487 /* Create the correct label name */
1488 char* lbuf = GetLabelName (flags, label, offs);
1490 switch (flags & CF_TYPE) {
1493 AddCodeLine ("\tclc");
1494 AddCodeLine ("\tadc\t%s", lbuf);
1495 AddCodeLine ("\tbcc\t*+3");
1496 AddCodeLine ("\tinx");
1497 AddCodeHint ("x:!");
1501 AddCodeLine ("\tclc");
1502 AddCodeLine ("\tadc\t%s", lbuf);
1503 AddCodeLine ("\ttay");
1504 AddCodeLine ("\ttxa");
1505 AddCodeLine ("\tadc\t%s+1", lbuf);
1506 AddCodeLine ("\ttax");
1507 AddCodeLine ("\ttya");
1511 /* Do it the old way */
1513 g_getstatic (flags, label, offs);
1525 /*****************************************************************************/
1526 /* Compares of ax with a variable with fixed address */
1527 /*****************************************************************************/
1531 void g_cmplocal (unsigned flags, int offs)
1532 /* Compare a local variable to ax */
1534 Internal ("g_cmplocal not implemented");
1539 void g_cmpstatic (unsigned flags, unsigned label, unsigned offs)
1540 /* Compare a static variable to ax */
1542 Internal ("g_cmpstatic not implemented");
1547 /*****************************************************************************/
1548 /* Special op= functions */
1549 /*****************************************************************************/
1553 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
1555 /* Emit += for a static variable */
1557 /* Create the correct label name */
1558 char* lbuf = GetLabelName (flags, label, offs);
1560 /* Check the size and determine operation */
1561 switch (flags & CF_TYPE) {
1564 if (flags & CF_FORCECHAR) {
1565 AddCodeLine ("\tldx\t#$00");
1566 if (flags & CF_CONST) {
1568 AddCodeLine ("\tinc\t%s", lbuf);
1569 AddCodeLine ("\tlda\t%s", lbuf);
1571 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1572 AddCodeLine ("\tclc");
1573 AddCodeLine ("\tadc\t%s", lbuf);
1574 AddCodeLine ("\tsta\t%s", lbuf);
1577 AddCodeLine ("\tclc");
1578 AddCodeLine ("\tadc\t%s", lbuf);
1579 AddCodeLine ("\tsta\t%s", lbuf);
1581 if ((flags & CF_UNSIGNED) == 0) {
1582 AddCodeLine ("\tbpl\t*+3");
1583 AddCodeLine ("\tdex");
1584 AddCodeHint ("x:!"); /* Invalidate X */
1591 if (flags & CF_CONST) {
1593 label = GetLabel ();
1594 AddCodeLine ("\tinc\t%s", lbuf);
1595 AddCodeLine ("\tbne\tL%04X", (int)label);
1596 AddCodeLine ("\tinc\t%s+1", lbuf);
1597 g_defloclabel (label);
1598 AddCodeLine ("\tlda\t%s", lbuf); /* Hmmm... */
1599 AddCodeLine ("\tldx\t%s+1", lbuf);
1601 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1602 AddCodeLine ("\tclc");
1603 AddCodeLine ("\tadc\t%s", lbuf);
1604 AddCodeLine ("\tsta\t%s", lbuf);
1606 label = GetLabel ();
1607 AddCodeLine ("\tbcc\tL%04X", (int)label);
1608 AddCodeLine ("\tinc\t%s+1", lbuf);
1609 g_defloclabel (label);
1610 AddCodeLine ("\tldx\t%s+1", lbuf);
1612 AddCodeLine ("\tlda\t#$%02X", (unsigned char)(val >> 8));
1613 AddCodeLine ("\tadc\t%s+1", lbuf);
1614 AddCodeLine ("\tsta\t%s+1", lbuf);
1615 AddCodeLine ("\ttax");
1616 AddCodeLine ("\tlda\t%s", lbuf);
1620 AddCodeLine ("\tclc");
1621 AddCodeLine ("\tadc\t%s", lbuf);
1622 AddCodeLine ("\tsta\t%s", lbuf);
1623 AddCodeLine ("\ttxa");
1624 AddCodeLine ("\tadc\t%s+1", lbuf);
1625 AddCodeLine ("\tsta\t%s+1", lbuf);
1626 AddCodeLine ("\ttax");
1627 AddCodeLine ("\tlda\t%s", lbuf);
1632 if (flags & CF_CONST) {
1634 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1635 AddCodeLine ("\tsty\tptr1");
1636 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1638 AddCodeLine ("\tjsr\tladdeq1");
1640 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1641 AddCodeLine ("\tjsr\tladdeqa");
1644 g_getstatic (flags, label, offs);
1646 g_putstatic (flags, label, offs);
1649 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1650 AddCodeLine ("\tsty\tptr1");
1651 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1652 AddCodeLine ("\tjsr\tladdeq");
1663 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1664 /* Emit += for a local variable */
1666 /* Calculate the true offset, check it, load it into Y */
1668 CheckLocalOffs (offs);
1670 /* Check the size and determine operation */
1671 switch (flags & CF_TYPE) {
1674 if (flags & CF_FORCECHAR) {
1676 AddCodeLine ("\tldx\t#$00");
1677 if (flags & CF_CONST) {
1678 AddCodeLine ("\tclc");
1679 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1680 AddCodeLine ("\tadc\t(sp,x)");
1681 AddCodeLine ("\tsta\t(sp,x)");
1683 AddCodeLine ("\tclc");
1684 AddCodeLine ("\tadc\t(sp,x)");
1685 AddCodeLine ("\tsta\t(sp,x)");
1689 AddCodeLine ("\tldx\t#$00");
1690 if (flags & CF_CONST) {
1691 AddCodeLine ("\tclc");
1692 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1693 AddCodeLine ("\tadc\t(sp),y");
1694 AddCodeLine ("\tsta\t(sp),y");
1696 AddCodeLine ("\tclc");
1697 AddCodeLine ("\tadc\t(sp),y");
1698 AddCodeLine ("\tsta\t(sp),y");
1701 if ((flags & CF_UNSIGNED) == 0) {
1702 AddCodeLine ("\tbpl\t*+3");
1703 AddCodeLine ("\tdex");
1704 AddCodeHint ("x:!"); /* Invalidate X */
1711 if (flags & CF_CONST) {
1712 g_getimmed (flags, val, 0);
1715 AddCodeLine ("\tjsr\taddeq0sp");
1718 AddCodeLine ("\tjsr\taddeqysp");
1723 if (flags & CF_CONST) {
1724 g_getimmed (flags, val, 0);
1727 AddCodeLine ("\tjsr\tladdeq0sp");
1730 AddCodeLine ("\tjsr\tladdeqysp");
1741 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1742 /* Emit += for the location with address in ax */
1744 /* If the offset is too large for a byte register, add the high byte
1745 * of the offset to the primary. Beware: We need a special correction
1746 * if the offset in the low byte will overflow in the operation.
1748 offs = MakeByteOffs (flags, offs);
1750 /* Check the size and determine operation */
1751 switch (flags & CF_TYPE) {
1754 AddCodeLine ("\tsta\tptr1");
1755 AddCodeLine ("\tstx\tptr1+1");
1757 AddCodeLine ("\tldx\t#$00");
1758 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1759 AddCodeLine ("\tclc");
1760 AddCodeLine ("\tadc\t(ptr1,x)");
1761 AddCodeLine ("\tsta\t(ptr1,x)");
1763 AddCodeLine ("\tldy\t#$%02X", offs);
1764 AddCodeLine ("\tldx\t#$00");
1765 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1766 AddCodeLine ("\tclc");
1767 AddCodeLine ("\tadc\t(ptr1),y");
1768 AddCodeLine ("\tsta\t(ptr1),y");
1774 /* Lots of code, use only if size is not important */
1775 AddCodeLine ("\tsta\tptr1");
1776 AddCodeLine ("\tstx\tptr1+1");
1777 AddCodeLine ("\tldy\t#$%02X", offs);
1778 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1779 AddCodeLine ("\tclc");
1780 AddCodeLine ("\tadc\t(ptr1),y");
1781 AddCodeLine ("\tsta\t(ptr1),y");
1782 AddCodeLine ("\tpha");
1783 AddCodeLine ("\tiny");
1784 AddCodeLine ("\tlda\t#$%02X", (unsigned char)(val >> 8));
1785 AddCodeLine ("\tadc\t(ptr1),y");
1786 AddCodeLine ("\tsta\t(ptr1),y");
1787 AddCodeLine ("\ttax");
1788 AddCodeLine ("\tpla");
1794 AddCodeLine ("\tjsr\tpushax"); /* Push the address */
1795 push (flags); /* Correct the internal sp */
1796 g_getind (flags, offs); /* Fetch the value */
1797 g_inc (flags, val); /* Increment value in primary */
1798 g_putind (flags, offs); /* Store the value back */
1808 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1810 /* Emit -= for a static variable */
1812 /* Create the correct label name */
1813 char* lbuf = GetLabelName (flags, label, offs);
1815 /* Check the size and determine operation */
1816 switch (flags & CF_TYPE) {
1819 if (flags & CF_FORCECHAR) {
1820 AddCodeLine ("\tldx\t#$00");
1821 if (flags & CF_CONST) {
1823 AddCodeLine ("\tdec\t%s", lbuf);
1824 AddCodeLine ("\tlda\t%s", lbuf);
1826 AddCodeLine ("\tsec");
1827 AddCodeLine ("\tlda\t%s", lbuf);
1828 AddCodeLine ("\tsbc\t#$%02X", (int)(val & 0xFF));
1829 AddCodeLine ("\tsta\t%s", lbuf);
1832 AddCodeLine ("\tsec");
1833 AddCodeLine ("\tsta\ttmp1");
1834 AddCodeLine ("\tlda\t%s", lbuf);
1835 AddCodeLine ("\tsbc\ttmp1");
1836 AddCodeLine ("\tsta\t%s", lbuf);
1838 if ((flags & CF_UNSIGNED) == 0) {
1839 AddCodeLine ("\tbpl\t*+3");
1840 AddCodeLine ("\tdex");
1841 AddCodeHint ("x:!"); /* Invalidate X */
1848 AddCodeLine ("\tsec");
1849 if (flags & CF_CONST) {
1850 AddCodeLine ("\tlda\t%s", lbuf);
1851 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
1852 AddCodeLine ("\tsta\t%s", lbuf);
1854 label = GetLabel ();
1855 AddCodeLine ("\tbcs\tL%04X", (unsigned)label);
1856 AddCodeLine ("\tdec\t%s+1", lbuf);
1857 g_defloclabel (label);
1858 AddCodeLine ("\tldx\t%s+1", lbuf);
1860 AddCodeLine ("\tlda\t%s+1", lbuf);
1861 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)(val >> 8));
1862 AddCodeLine ("\tsta\t%s+1", lbuf);
1863 AddCodeLine ("\ttax");
1864 AddCodeLine ("\tlda\t%s", lbuf);
1867 AddCodeLine ("\tsta\ttmp1");
1868 AddCodeLine ("\tlda\t%s", lbuf);
1869 AddCodeLine ("\tsbc\ttmp1");
1870 AddCodeLine ("\tsta\t%s", lbuf);
1871 AddCodeLine ("\tstx\ttmp1");
1872 AddCodeLine ("\tlda\t%s+1", lbuf);
1873 AddCodeLine ("\tsbc\ttmp1");
1874 AddCodeLine ("\tsta\t%s+1", lbuf);
1875 AddCodeLine ("\ttax");
1876 AddCodeLine ("\tlda\t%s", lbuf);
1881 if (flags & CF_CONST) {
1883 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1884 AddCodeLine ("\tsty\tptr1");
1885 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1887 AddCodeLine ("\tjsr\tlsubeq1");
1889 AddCodeLine ("\tlda\t#$%02X", (unsigned char)val);
1890 AddCodeLine ("\tjsr\tlsubeqa");
1893 g_getstatic (flags, label, offs);
1895 g_putstatic (flags, label, offs);
1898 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1899 AddCodeLine ("\tsty\tptr1");
1900 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1901 AddCodeLine ("\tjsr\tlsubeq");
1912 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1913 /* Emit -= for a local variable */
1915 /* Calculate the true offset, check it, load it into Y */
1917 CheckLocalOffs (offs);
1919 /* Check the size and determine operation */
1920 switch (flags & CF_TYPE) {
1923 if (flags & CF_FORCECHAR) {
1925 AddCodeLine ("\tldx\t#$00");
1926 AddCodeLine ("\tsec");
1927 if (flags & CF_CONST) {
1928 AddCodeLine ("\tlda\t(sp),y");
1929 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
1931 AddCodeLine ("\tsta\ttmp1");
1932 AddCodeLine ("\tlda\t(sp),y");
1933 AddCodeLine ("\tsbc\ttmp1");
1935 AddCodeLine ("\tsta\t(sp),y");
1936 if ((flags & CF_UNSIGNED) == 0) {
1937 AddCodeLine ("\tbpl\t*+3");
1938 AddCodeLine ("\tdex");
1939 AddCodeHint ("x:!"); /* Invalidate X */
1946 if (flags & CF_CONST) {
1947 g_getimmed (flags, val, 0);
1950 AddCodeLine ("\tjsr\tsubeq0sp");
1953 AddCodeLine ("\tjsr\tsubeqysp");
1958 if (flags & CF_CONST) {
1959 g_getimmed (flags, val, 0);
1962 AddCodeLine ("\tjsr\tlsubeq0sp");
1965 AddCodeLine ("\tjsr\tlsubeqysp");
1976 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
1977 /* Emit -= for the location with address in ax */
1979 /* If the offset is too large for a byte register, add the high byte
1980 * of the offset to the primary. Beware: We need a special correction
1981 * if the offset in the low byte will overflow in the operation.
1983 offs = MakeByteOffs (flags, offs);
1985 /* Check the size and determine operation */
1986 switch (flags & CF_TYPE) {
1989 AddCodeLine ("\tsta\tptr1");
1990 AddCodeLine ("\tstx\tptr1+1");
1992 AddCodeLine ("\tldx\t#$00");
1993 AddCodeLine ("\tlda\t(ptr1,x)");
1994 AddCodeLine ("\tsec");
1995 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
1996 AddCodeLine ("\tsta\t(ptr1,x)");
1998 AddCodeLine ("\tldy\t#$%02X", offs);
1999 AddCodeLine ("\tldx\t#$00");
2000 AddCodeLine ("\tlda\t(ptr1),y");
2001 AddCodeLine ("\tsec");
2002 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2003 AddCodeLine ("\tsta\t(ptr1),y");
2009 /* Lots of code, use only if size is not important */
2010 AddCodeLine ("\tsta\tptr1");
2011 AddCodeLine ("\tstx\tptr1+1");
2012 AddCodeLine ("\tldy\t#$%02X", offs);
2013 AddCodeLine ("\tlda\t(ptr1),y");
2014 AddCodeLine ("\tsec");
2015 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2016 AddCodeLine ("\tsta\t(ptr1),y");
2017 AddCodeLine ("\tpha");
2018 AddCodeLine ("\tiny");
2019 AddCodeLine ("\tlda\t(ptr1),y");
2020 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)(val >> 8));
2021 AddCodeLine ("\tsta\t(ptr1),y");
2022 AddCodeLine ("\ttax");
2023 AddCodeLine ("\tpla");
2029 AddCodeLine ("\tjsr\tpushax"); /* Push the address */
2030 push (flags); /* Correct the internal sp */
2031 g_getind (flags, offs); /* Fetch the value */
2032 g_dec (flags, val); /* Increment value in primary */
2033 g_putind (flags, offs); /* Store the value back */
2043 /*****************************************************************************/
2044 /* Add a variable address to the value in ax */
2045 /*****************************************************************************/
2049 void g_addaddr_local (unsigned flags, int offs)
2050 /* Add the address of a local variable to ax */
2052 /* Add the offset */
2055 /* We cannot address more then 256 bytes of locals anyway */
2056 CheckLocalOffs (offs);
2057 AddCodeLine ("\tclc");
2058 AddCodeLine ("\tadc\t#$%02X", offs & 0xFF);
2059 AddCodeLine ("\tbcc\t*+4"); /* Do also skip the CLC insn below */
2060 AddCodeLine ("\tinx");
2061 AddCodeHint ("x:!"); /* Invalidate X */
2064 /* Add the current stackpointer value */
2065 AddCodeLine ("\tclc");
2066 AddCodeLine ("\tadc\tsp");
2067 AddCodeLine ("\ttay");
2068 AddCodeLine ("\ttxa");
2069 AddCodeLine ("\tadc\tsp+1");
2070 AddCodeLine ("\ttax");
2071 AddCodeLine ("\ttya");
2076 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2077 /* Add the address of a static variable to ax */
2079 /* Create the correct label name */
2080 char* lbuf = GetLabelName (flags, label, offs);
2082 /* Add the address to the current ax value */
2083 AddCodeLine ("\tclc");
2084 AddCodeLine ("\tadc\t#<(%s)", lbuf);
2085 AddCodeLine ("\ttay");
2086 AddCodeLine ("\ttxa");
2087 AddCodeLine ("\tadc\t#>(%s)", lbuf);
2088 AddCodeLine ("\ttax");
2089 AddCodeLine ("\ttya");
2094 /*****************************************************************************/
2096 /*****************************************************************************/
2100 void g_save (unsigned flags)
2101 /* Copy primary register to hold register. */
2103 /* Check the size and determine operation */
2104 switch (flags & CF_TYPE) {
2107 if (flags & CF_FORCECHAR) {
2108 AddCodeLine ("\tpha");
2114 AddCodeLine ("\tsta\tregsave");
2115 AddCodeLine ("\tstx\tregsave+1");
2119 AddCodeLine ("\tjsr\tsaveeax");
2129 void g_restore (unsigned flags)
2130 /* Copy hold register to P. */
2132 /* Check the size and determine operation */
2133 switch (flags & CF_TYPE) {
2136 if (flags & CF_FORCECHAR) {
2137 AddCodeLine ("\tpla");
2143 AddCodeLine ("\tlda\tregsave");
2144 AddCodeLine ("\tldx\tregsave+1");
2148 AddCodeLine ("\tjsr\tresteax");
2158 void g_cmp (unsigned flags, unsigned long val)
2159 /* Immidiate compare. The primary register will not be changed, Z flag
2163 /* Check the size and determine operation */
2164 switch (flags & CF_TYPE) {
2167 if (flags & CF_FORCECHAR) {
2168 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
2174 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
2175 AddCodeLine ("\tbne\t*+4");
2176 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
2180 Internal ("g_cmp: Long compares not implemented");
2190 static void oper (unsigned flags, unsigned long val, char** subs)
2191 /* Encode a binary operation. subs is a pointer to four groups of three
2193 * 0-2 --> Operate on ints
2194 * 3-5 --> Operate on unsigneds
2195 * 6-8 --> Operate on longs
2196 * 9-11 --> Operate on unsigned longs
2198 * The first subroutine names in each string group is used to encode an
2199 * operation with a zero constant, the second to encode an operation with
2200 * a 8 bit constant, and the third is used in all other cases.
2205 /* Determine the offset into the array */
2206 offs = (flags & CF_UNSIGNED)? 3 : 0;
2207 switch (flags & CF_TYPE) {
2220 /* Encode the operation */
2221 if (flags & CF_CONST) {
2222 /* Constant value given */
2223 if (val == 0 && subs [offs+0]) {
2224 /* Special case: constant with value zero */
2225 AddCodeLine ("\tjsr\t%s", subs [offs+0]);
2226 } else if (val < 0x100 && subs [offs+1]) {
2227 /* Special case: constant with high byte zero */
2228 ldaconst (val); /* Load low byte */
2229 AddCodeLine ("\tjsr\t%s", subs [offs+1]);
2231 /* Others: arbitrary constant value */
2232 g_getimmed (flags, val, 0); /* Load value */
2233 AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2236 /* Value not constant (is already in (e)ax) */
2237 AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2240 /* The operation will pop it's argument */
2246 void g_test (unsigned flags)
2247 /* Force a test to set cond codes right */
2249 switch (flags & CF_TYPE) {
2252 if (flags & CF_FORCECHAR) {
2253 AddCodeLine ("\ttax");
2259 AddCodeLine ("\tstx\ttmp1");
2260 AddCodeLine ("\tora\ttmp1");
2264 if (flags & CF_UNSIGNED) {
2265 AddCodeLine ("\tjsr\tutsteax");
2267 AddCodeLine ("\tjsr\ttsteax");
2279 void g_push (unsigned flags, unsigned long val)
2280 /* Push the primary register or a constant value onto the stack */
2284 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2286 /* We have a constant 8 or 16 bit value */
2287 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2289 /* Handle as 8 bit value */
2290 if (FavourSize && val <= 2) {
2291 AddCodeLine ("\tjsr\tpushc%d", (int) val);
2294 AddCodeLine ("\tjsr\tpusha");
2299 /* Handle as 16 bit value */
2300 hi = (unsigned char) (val >> 8);
2302 AddCodeLine ("\tjsr\tpush%u", (unsigned) val);
2303 } else if (hi == 0 || hi == 0xFF) {
2304 /* Use special function */
2306 AddCodeLine ("\tjsr\t%s", (hi == 0)? "pusha0" : "pushaFF");
2309 g_getimmed (flags, val, 0);
2310 AddCodeLine ("\tjsr\tpushax");
2316 /* Value is not 16 bit or not constant */
2317 if (flags & CF_CONST) {
2318 /* Constant 32 bit value, load into eax */
2319 g_getimmed (flags, val, 0);
2322 /* Push the primary register */
2323 switch (flags & CF_TYPE) {
2326 if (flags & CF_FORCECHAR) {
2327 /* Handle as char */
2328 AddCodeLine ("\tjsr\tpusha");
2333 AddCodeLine ("\tjsr\tpushax");
2337 AddCodeLine ("\tjsr\tpusheax");
2347 /* Adjust the stack offset */
2353 void g_swap (unsigned flags)
2354 /* Swap the primary register and the top of the stack. flags give the type
2355 * of *both* values (must have same size).
2358 switch (flags & CF_TYPE) {
2362 AddCodeLine ("\tjsr\tswapstk");
2366 AddCodeLine ("\tjsr\tswapestk");
2377 void g_call (unsigned flags, char* lbl, unsigned argsize)
2378 /* Call the specified subroutine name */
2380 if ((flags & CF_FIXARGC) == 0) {
2381 /* Pass arg count */
2384 AddCodeLine ("\tjsr\t_%s", lbl);
2385 oursp += argsize; /* callee pops args */
2390 void g_callind (unsigned flags, unsigned argsize)
2391 /* Call subroutine with address in AX */
2393 if ((flags & CF_FIXARGC) == 0) {
2394 /* Pass arg count */
2397 AddCodeLine ("\tjsr\tcallax"); /* do the call */
2398 oursp += argsize; /* callee pops args */
2403 void g_jump (unsigned label)
2404 /* Jump to specified internal label number */
2406 AddCodeLine ("\tjmp\tL%04X", label);
2411 void g_switch (unsigned flags)
2412 /* Output switch statement preample */
2414 switch (flags & CF_TYPE) {
2418 AddCodeLine ("\tjsr\tswitch");
2422 AddCodeLine ("\tjsr\tlswitch");
2433 void g_case (unsigned flags, unsigned label, unsigned long val)
2434 /* Create table code for one case selector */
2436 switch (flags & CF_TYPE) {
2440 AddCodeLine ("\t.word\t$%04X, L%04X",
2441 (unsigned)(val & 0xFFFF),
2442 (unsigned)(label & 0xFFFF));
2446 AddCodeLine ("\t.dword\t$%08lX", val);
2447 AddCodeLine ("\t.word\tL%04X", label & 0xFFFF);
2458 void g_truejump (unsigned flags, unsigned label)
2459 /* Jump to label if zero flag clear */
2461 if (flags & CF_SHORT) {
2462 AddCodeLine ("\tbne\tL%04X", label);
2464 AddCodeLine ("\tjne\tL%04X", label);
2470 void g_falsejump (unsigned flags, unsigned label)
2471 /* Jump to label if zero flag set */
2473 if (flags & CF_SHORT) {
2474 AddCodeLine ("\tbeq\tL%04X", label);
2476 AddCodeLine ("\tjeq\tL%04X", label);
2482 static void mod_internal (int k, char* verb1, char* verb2)
2485 AddCodeLine ("\tjsr\t%ssp%c", verb1, k + '0');
2489 AddCodeLine ("\tjsr\t%ssp", verb2);
2495 void g_space (int space)
2496 /* Create or drop space on the stack */
2499 mod_internal (-space, "inc", "addy");
2500 } else if (space > 0) {
2501 mod_internal (space, "dec", "suby");
2507 void g_add (unsigned flags, unsigned long val)
2508 /* Primary = TOS + Primary */
2510 static char* ops [12] = {
2511 0, "tosadda0", "tosaddax",
2512 0, "tosadda0", "tosaddax",
2517 if (flags & CF_CONST) {
2518 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2519 g_push (flags & ~CF_CONST, 0);
2521 oper (flags, val, ops);
2526 void g_sub (unsigned flags, unsigned long val)
2527 /* Primary = TOS - Primary */
2529 static char* ops [12] = {
2530 0, "tossuba0", "tossubax",
2531 0, "tossuba0", "tossubax",
2536 if (flags & CF_CONST) {
2537 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2538 g_push (flags & ~CF_CONST, 0);
2540 oper (flags, val, ops);
2545 void g_rsub (unsigned flags, unsigned long val)
2546 /* Primary = Primary - TOS */
2548 static char* ops [12] = {
2549 0, "tosrsuba0", "tosrsubax",
2550 0, "tosrsuba0", "tosrsubax",
2554 oper (flags, val, ops);
2559 void g_mul (unsigned flags, unsigned long val)
2560 /* Primary = TOS * Primary */
2562 static char* ops [12] = {
2563 0, "tosmula0", "tosmulax",
2564 0, "tosumula0", "tosumulax",
2571 /* Do strength reduction if the value is constant and a power of two */
2572 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2573 /* Generate a shift instead */
2578 /* If the right hand side is const, the lhs is not on stack but still
2579 * in the primary register.
2581 if (flags & CF_CONST) {
2583 switch (flags & CF_TYPE) {
2586 if (flags & CF_FORCECHAR) {
2587 /* Handle some special cases */
2591 AddCodeLine ("\tsta\ttmp1");
2592 AddCodeLine ("\tasl\ta");
2593 AddCodeLine ("\tclc");
2594 AddCodeLine ("\tadc\ttmp1");
2598 AddCodeLine ("\tsta\ttmp1");
2599 AddCodeLine ("\tasl\ta");
2600 AddCodeLine ("\tasl\ta");
2601 AddCodeLine ("\tclc");
2602 AddCodeLine ("\tadc\ttmp1");
2606 AddCodeLine ("\tsta\ttmp1");
2607 AddCodeLine ("\tasl\ta");
2608 AddCodeLine ("\tasl\ta");
2609 AddCodeLine ("\tclc");
2610 AddCodeLine ("\tadc\ttmp1");
2611 AddCodeLine ("\tasl\ta");
2627 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2628 * into the normal, non-optimized stuff.
2630 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2631 g_push (flags & ~CF_CONST, 0);
2635 /* Use long way over the stack */
2636 oper (flags, val, ops);
2641 void g_div (unsigned flags, unsigned long val)
2642 /* Primary = TOS / Primary */
2644 static char* ops [12] = {
2645 0, "tosdiva0", "tosdivax",
2646 0, "tosudiva0", "tosudivax",
2651 /* Do strength reduction if the value is constant and a power of two */
2653 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2654 /* Generate a shift instead */
2657 /* Generate a division */
2658 if (flags & CF_CONST) {
2659 /* lhs is not on stack */
2660 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2661 g_push (flags & ~CF_CONST, 0);
2663 oper (flags, val, ops);
2669 void g_mod (unsigned flags, unsigned long val)
2670 /* Primary = TOS % Primary */
2672 static char* ops [12] = {
2673 0, "tosmoda0", "tosmodax",
2674 0, "tosumoda0", "tosumodax",
2680 /* Check if we can do some cost reduction */
2681 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2682 /* We can do that with an AND operation */
2683 g_and (flags, val - 1);
2685 /* Do it the hard way... */
2686 if (flags & CF_CONST) {
2687 /* lhs is not on stack */
2688 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2689 g_push (flags & ~CF_CONST, 0);
2691 oper (flags, val, ops);
2697 void g_or (unsigned flags, unsigned long val)
2698 /* Primary = TOS | Primary */
2700 static char* ops [12] = {
2701 0, "tosora0", "tosorax",
2702 0, "tosora0", "tosorax",
2707 /* If the right hand side is const, the lhs is not on stack but still
2708 * in the primary register.
2710 if (flags & CF_CONST) {
2712 switch (flags & CF_TYPE) {
2715 if (flags & CF_FORCECHAR) {
2716 if ((val & 0xFF) != 0xFF) {
2717 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2725 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2732 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2741 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2742 * into the normal, non-optimized stuff.
2744 g_push (flags & ~CF_CONST, 0);
2748 /* Use long way over the stack */
2749 oper (flags, val, ops);
2754 void g_xor (unsigned flags, unsigned long val)
2755 /* Primary = TOS ^ Primary */
2757 static char* ops [12] = {
2758 0, "tosxora0", "tosxorax",
2759 0, "tosxora0", "tosxorax",
2765 /* If the right hand side is const, the lhs is not on stack but still
2766 * in the primary register.
2768 if (flags & CF_CONST) {
2770 switch (flags & CF_TYPE) {
2773 if (flags & CF_FORCECHAR) {
2774 if ((val & 0xFF) != 0) {
2775 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2784 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2787 } else if ((val & 0xFF) == 0) {
2788 AddCodeLine ("\tpha");
2789 AddCodeLine ("\ttxa");
2790 AddCodeLine ("\teor\t#$%02X", (unsigned char)(val >> 8));
2791 AddCodeLine ("\ttax");
2792 AddCodeLine ("\tpla");
2800 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2810 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2811 * into the normal, non-optimized stuff.
2813 g_push (flags & ~CF_CONST, 0);
2817 /* Use long way over the stack */
2818 oper (flags, val, ops);
2823 void g_and (unsigned flags, unsigned long val)
2824 /* Primary = TOS & Primary */
2826 static char* ops [12] = {
2827 0, "tosanda0", "tosandax",
2828 0, "tosanda0", "tosandax",
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 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2847 if ((val & 0xFFFF) != 0xFFFF) {
2852 } else if (val != 0xFF) {
2853 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2855 } else if ((val & 0xFF00) == 0xFF00) {
2856 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2857 } else if ((val & 0x00FF) == 0x0000) {
2858 AddCodeLine ("\ttxa");
2859 AddCodeLine ("\tand\t#$%02X", (unsigned char)(val >> 8));
2860 AddCodeLine ("\ttax");
2863 AddCodeLine ("\ttay");
2864 AddCodeLine ("\ttxa");
2865 AddCodeLine ("\tand\t#$%02X", (unsigned char)(val >> 8));
2866 AddCodeLine ("\ttax");
2867 AddCodeLine ("\ttya");
2868 if ((val & 0x00FF) != 0x00FF) {
2869 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2878 AddCodeLine ("\tstx\tsreg+1");
2879 AddCodeLine ("\tstx\tsreg");
2880 if ((val & 0xFF) != 0xFF) {
2881 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2884 } else if (val == 0xFF00) {
2886 AddCodeLine ("\tsta\tsreg+1");
2887 AddCodeLine ("\tsta\tsreg");
2896 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2897 * into the normal, non-optimized stuff.
2899 g_push (flags & ~CF_CONST, 0);
2903 /* Use long way over the stack */
2904 oper (flags, val, ops);
2909 void g_asr (unsigned flags, unsigned long val)
2910 /* Primary = TOS >> Primary */
2912 static char* ops [12] = {
2913 0, "tosasra0", "tosasrax",
2914 0, "tosshra0", "tosshrax",
2919 /* If the right hand side is const, the lhs is not on stack but still
2920 * in the primary register.
2922 if (flags & CF_CONST) {
2924 switch (flags & CF_TYPE) {
2928 if (val >= 1 && val <= 3) {
2929 if (flags & CF_UNSIGNED) {
2930 AddCodeLine ("\tjsr\tshrax%ld", val);
2932 AddCodeLine ("\tjsr\tasrax%ld", val);
2935 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2936 AddCodeLine ("\ttxa");
2943 if (val >= 1 && val <= 3) {
2944 if (flags & CF_UNSIGNED) {
2945 AddCodeLine ("\tjsr\tshreax%ld", val);
2947 AddCodeLine ("\tjsr\tasreax%ld", val);
2950 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2951 AddCodeLine ("\ttxa");
2952 AddCodeLine ("\tldx\tsreg");
2953 AddCodeLine ("\tldy\tsreg+1");
2954 AddCodeLine ("\tsty\tsreg");
2955 AddCodeLine ("\tldy\t#$00");
2956 AddCodeLine ("\tsty\tsreg+1");
2958 } else if (val == 16) {
2959 AddCodeLine ("\tldy\t#$00");
2960 AddCodeLine ("\tldx\tsreg+1");
2961 if ((flags & CF_UNSIGNED) == 0) {
2962 AddCodeLine ("\tbpl\t*+3");
2963 AddCodeLine ("\tdey");
2964 AddCodeHint ("y:!");
2966 AddCodeLine ("\tlda\tsreg");
2967 AddCodeLine ("\tsty\tsreg+1");
2968 AddCodeLine ("\tsty\tsreg");
2977 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2978 * into the normal, non-optimized stuff.
2980 g_push (flags & ~CF_CONST, 0);
2984 /* Use long way over the stack */
2985 oper (flags, val, ops);
2990 void g_asl (unsigned flags, unsigned long val)
2991 /* Primary = TOS << Primary */
2993 static char* ops [12] = {
2994 0, "tosasla0", "tosaslax",
2995 0, "tosshla0", "tosshlax",
3001 /* If the right hand side is const, the lhs is not on stack but still
3002 * in the primary register.
3004 if (flags & CF_CONST) {
3006 switch (flags & CF_TYPE) {
3010 if (val >= 1 && val <= 3) {
3011 if (flags & CF_UNSIGNED) {
3012 AddCodeLine ("\tjsr\tshlax%ld", val);
3014 AddCodeLine ("\tjsr\taslax%ld", val);
3017 } else if (val == 8) {
3018 AddCodeLine ("\ttax");
3019 AddCodeLine ("\tlda\t#$00");
3025 if (val >= 1 && val <= 3) {
3026 if (flags & CF_UNSIGNED) {
3027 AddCodeLine ("\tjsr\tshleax%ld", val);
3029 AddCodeLine ("\tjsr\tasleax%ld", val);
3032 } else if (val == 8) {
3033 AddCodeLine ("\tldy\tsreg");
3034 AddCodeLine ("\tsty\tsreg+1");
3035 AddCodeLine ("\tstx\tsreg");
3036 AddCodeLine ("\ttax");
3037 AddCodeLine ("\tlda\t#$00");
3039 } else if (val == 16) {
3040 AddCodeLine ("\tstx\tsreg+1");
3041 AddCodeLine ("\tsta\tsreg");
3042 AddCodeLine ("\tlda\t#$00");
3043 AddCodeLine ("\ttax");
3052 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3053 * into the normal, non-optimized stuff.
3055 g_push (flags & ~CF_CONST, 0);
3059 /* Use long way over the stack */
3060 oper (flags, val, ops);
3065 void g_neg (unsigned flags)
3066 /* Primary = -Primary */
3068 switch (flags & CF_TYPE) {
3072 AddCodeLine ("\tjsr\tnegax");
3076 AddCodeLine ("\tjsr\tnegeax");
3086 void g_bneg (unsigned flags)
3087 /* Primary = !Primary */
3089 switch (flags & CF_TYPE) {
3092 AddCodeLine ("\tjsr\tbnega");
3096 AddCodeLine ("\tjsr\tbnegax");
3100 AddCodeLine ("\tjsr\tbnegeax");
3110 void g_com (unsigned flags)
3111 /* Primary = ~Primary */
3113 switch (flags & CF_TYPE) {
3117 AddCodeLine ("\tjsr\tcomplax");
3121 AddCodeLine ("\tjsr\tcompleax");
3131 void g_inc (unsigned flags, unsigned long val)
3132 /* Increment the primary register by a given number */
3134 /* Don't inc by zero */
3139 /* Generate code for the supported types */
3141 switch (flags & CF_TYPE) {
3144 if (flags & CF_FORCECHAR) {
3145 if (CPU == CPU_65C02 && val <= 2) {
3147 AddCodeLine ("\tina");
3150 AddCodeLine ("\tclc");
3151 AddCodeLine ("\tadc\t#$%02X", (unsigned char)val);
3158 if (CPU == CPU_65C02 && val == 1) {
3159 AddCodeLine ("\tina");
3160 AddCodeLine ("\tbne\t*+3");
3161 AddCodeLine ("\tinx");
3162 /* Tell the optimizer that the X register may be invalid */
3163 AddCodeHint ("x:!");
3164 } else if (FavourSize) {
3167 AddCodeLine ("\tjsr\tincax%lu", val);
3168 } else if (val <= 255) {
3170 AddCodeLine ("\tjsr\tincaxy");
3172 g_add (flags | CF_CONST, val);
3175 /* Inline the code */
3177 if ((val & 0xFF) != 0) {
3178 AddCodeLine ("\tclc");
3179 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3180 AddCodeLine ("\tbcc\t*+3");
3181 AddCodeLine ("\tinx");
3182 /* Tell the optimizer that the X register may be invalid */
3183 AddCodeHint ("x:!");
3186 AddCodeLine ("\tinx");
3189 AddCodeLine ("\tinx");
3192 AddCodeLine ("\tclc");
3193 if ((val & 0xFF) != 0) {
3194 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3195 /* Tell the optimizer that the X register may be invalid */
3196 AddCodeHint ("x:!");
3198 AddCodeLine ("\tpha");
3199 AddCodeLine ("\ttxa");
3200 AddCodeLine ("\tadc\t#$%02X", (unsigned char) (val >> 8));
3201 AddCodeLine ("\ttax");
3202 AddCodeLine ("\tpla");
3210 AddCodeLine ("\tjsr\tinceaxy");
3212 g_add (flags | CF_CONST, val);
3224 void g_dec (unsigned flags, unsigned long val)
3225 /* Decrement the primary register by a given number */
3227 /* Don't dec by zero */
3232 /* Generate code for the supported types */
3234 switch (flags & CF_TYPE) {
3237 if (flags & CF_FORCECHAR) {
3238 if (CPU == CPU_65C02 && val <= 2) {
3240 AddCodeLine ("\tdea");
3243 AddCodeLine ("\tsec");
3244 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
3252 AddCodeLine ("\tjsr\tdecax%d", (int) val);
3253 } else if (val <= 255) {
3255 AddCodeLine ("\tjsr\tdecaxy");
3257 g_sub (flags | CF_CONST, val);
3264 AddCodeLine ("\tjsr\tdeceaxy");
3266 g_sub (flags | CF_CONST, val);
3279 * Following are the conditional operators. They compare the TOS against
3280 * the primary and put a literal 1 in the primary if the condition is
3281 * true, otherwise they clear the primary register
3286 void g_eq (unsigned flags, unsigned long val)
3287 /* Test for equal */
3289 static char* ops [12] = {
3290 "toseq00", "toseqa0", "toseqax",
3291 "toseq00", "toseqa0", "toseqax",
3296 /* If the right hand side is const, the lhs is not on stack but still
3297 * in the primary register.
3299 if (flags & CF_CONST) {
3301 switch (flags & CF_TYPE) {
3304 if (flags & CF_FORCECHAR) {
3305 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3306 AddCodeLine ("\tjsr\tbooleq");
3312 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3313 AddCodeLine ("\tbne\t*+4");
3314 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3315 AddCodeLine ("\tjsr\tbooleq");
3325 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3326 * into the normal, non-optimized stuff.
3328 g_push (flags & ~CF_CONST, 0);
3332 /* Use long way over the stack */
3333 oper (flags, val, ops);
3338 void g_ne (unsigned flags, unsigned long val)
3339 /* Test for not equal */
3341 static char* ops [12] = {
3342 "tosne00", "tosnea0", "tosneax",
3343 "tosne00", "tosnea0", "tosneax",
3349 /* If the right hand side is const, the lhs is not on stack but still
3350 * in the primary register.
3352 if (flags & CF_CONST) {
3354 switch (flags & CF_TYPE) {
3357 if (flags & CF_FORCECHAR) {
3358 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3359 AddCodeLine ("\tjsr\tboolne");
3365 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3366 AddCodeLine ("\tbne\t*+4");
3367 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3368 AddCodeLine ("\tjsr\tboolne");
3378 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3379 * into the normal, non-optimized stuff.
3381 g_push (flags & ~CF_CONST, 0);
3385 /* Use long way over the stack */
3386 oper (flags, val, ops);
3391 void g_lt (unsigned flags, unsigned long val)
3392 /* Test for less than */
3394 static char* ops [12] = {
3395 "toslt00", "toslta0", "tosltax",
3396 "tosult00", "tosulta0", "tosultax",
3401 /* If the right hand side is const, the lhs is not on stack but still
3402 * in the primary register.
3404 if (flags & CF_CONST) {
3406 /* Give a warning in some special cases */
3407 if ((flags & CF_UNSIGNED) && val == 0) {
3408 Warning ("Condition is never true");
3411 /* Look at the type */
3412 switch (flags & CF_TYPE) {
3415 if (flags & CF_FORCECHAR) {
3416 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3417 if (flags & CF_UNSIGNED) {
3418 AddCodeLine ("\tjsr\tboolult");
3420 AddCodeLine ("\tjsr\tboollt");
3427 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3428 /* If we have a signed compare against zero, we only need to
3429 * test the high byte.
3431 AddCodeLine ("\ttxa");
3432 AddCodeLine ("\tjsr\tboollt");
3435 /* Direct code only for unsigned data types */
3436 if (flags & CF_UNSIGNED) {
3437 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3438 AddCodeLine ("\tbne\t*+4");
3439 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3440 AddCodeLine ("\tjsr\tboolult");
3452 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3453 * into the normal, non-optimized stuff.
3455 g_push (flags & ~CF_CONST, 0);
3459 /* Use long way over the stack */
3460 oper (flags, val, ops);
3465 void g_le (unsigned flags, unsigned long val)
3466 /* Test for less than or equal to */
3468 static char* ops [12] = {
3469 "tosle00", "toslea0", "tosleax",
3470 "tosule00", "tosulea0", "tosuleax",
3476 /* If the right hand side is const, the lhs is not on stack but still
3477 * in the primary register.
3479 if (flags & CF_CONST) {
3481 /* Look at the type */
3482 switch (flags & CF_TYPE) {
3485 if (flags & CF_FORCECHAR) {
3486 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3487 if (flags & CF_UNSIGNED) {
3488 AddCodeLine ("\tjsr\tboolule");
3490 AddCodeLine ("\tjsr\tboolle");
3497 if (flags & CF_UNSIGNED) {
3498 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3499 AddCodeLine ("\tbne\t*+4");
3500 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3501 AddCodeLine ("\tjsr\tboolule");
3513 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3514 * into the normal, non-optimized stuff.
3516 g_push (flags & ~CF_CONST, 0);
3520 /* Use long way over the stack */
3521 oper (flags, val, ops);
3526 void g_gt (unsigned flags, unsigned long val)
3527 /* Test for greater than */
3529 static char* ops [12] = {
3530 "tosgt00", "tosgta0", "tosgtax",
3531 "tosugt00", "tosugta0", "tosugtax",
3537 /* If the right hand side is const, the lhs is not on stack but still
3538 * in the primary register.
3540 if (flags & CF_CONST) {
3542 /* Look at the type */
3543 switch (flags & CF_TYPE) {
3546 if (flags & CF_FORCECHAR) {
3547 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3548 if (flags & CF_UNSIGNED) {
3549 /* If we have a compare > 0, we will replace it by
3550 * != 0 here, since both are identical but the latter
3551 * is easier to optimize.
3554 AddCodeLine ("\tjsr\tboolugt");
3556 AddCodeLine ("\tjsr\tboolne");
3559 AddCodeLine ("\tjsr\tboolgt");
3566 if (flags & CF_UNSIGNED) {
3567 /* If we have a compare > 0, we will replace it by
3568 * != 0 here, since both are identical but the latter
3569 * is easier to optimize.
3571 if ((val & 0xFFFF) == 0) {
3572 AddCodeLine ("\tstx\ttmp1");
3573 AddCodeLine ("\tora\ttmp1");
3574 AddCodeLine ("\tjsr\tboolne");
3576 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3577 AddCodeLine ("\tbne\t*+4");
3578 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3579 AddCodeLine ("\tjsr\tboolugt");
3592 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3593 * into the normal, non-optimized stuff.
3595 g_push (flags & ~CF_CONST, 0);
3599 /* Use long way over the stack */
3600 oper (flags, val, ops);
3605 void g_ge (unsigned flags, unsigned long val)
3606 /* Test for greater than or equal to */
3608 static char* ops [12] = {
3609 "tosge00", "tosgea0", "tosgeax",
3610 "tosuge00", "tosugea0", "tosugeax",
3616 /* If the right hand side is const, the lhs is not on stack but still
3617 * in the primary register.
3619 if (flags & CF_CONST) {
3621 /* Give a warning in some special cases */
3622 if ((flags & CF_UNSIGNED) && val == 0) {
3623 Warning ("Condition is always true");
3626 /* Look at the type */
3627 switch (flags & CF_TYPE) {
3630 if (flags & CF_FORCECHAR) {
3631 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3632 if (flags & CF_UNSIGNED) {
3633 AddCodeLine ("\tjsr\tbooluge");
3635 AddCodeLine ("\tjsr\tboolge");
3642 if (flags & CF_UNSIGNED) {
3643 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3644 AddCodeLine ("\tbne\t*+4");
3645 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3646 AddCodeLine ("\tjsr\tbooluge");
3658 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3659 * into the normal, non-optimized stuff.
3661 g_push (flags & ~CF_CONST, 0);
3665 /* Use long way over the stack */
3666 oper (flags, val, ops);
3671 /*****************************************************************************/
3672 /* Allocating static storage */
3673 /*****************************************************************************/
3677 void g_res (unsigned n)
3678 /* Reserve static storage, n bytes */
3680 AddCodeLine ("\t.res\t%u,$00", n);
3685 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3686 /* Define data with the size given in flags */
3688 if (flags & CF_CONST) {
3690 /* Numeric constant */
3691 switch (flags & CF_TYPE) {
3694 AddCodeLine ("\t.byte\t$%02lX", val & 0xFF);
3698 AddCodeLine ("\t.word\t$%04lX", val & 0xFFFF);
3702 AddCodeLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3713 /* Create the correct label name */
3714 const char* Label = GetLabelName (flags, val, offs);
3716 /* Labels are always 16 bit */
3717 AddCodeLine ("\t.word\t%s", Label);
3724 void g_defbytes (const unsigned char* Bytes, unsigned Count)
3725 /* Output a row of bytes as a constant */
3731 /* Output the stuff */
3734 /* How many go into this line? */
3735 if ((Chunk = Count) > 16) {
3740 /* Output one line */
3741 strcpy (Buf, "\t.byte\t");
3744 B += sprintf (B, "$%02X", *Bytes++ & 0xFF);
3750 /* Output the line */
3757 void g_zerobytes (unsigned n)
3758 /* Output n bytes of data initialized with zero */
3760 AddCodeLine ("\t.res\t%u,$00", n);
3765 /*****************************************************************************/
3766 /* Inlined known functions */
3767 /*****************************************************************************/
3771 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3772 /* Inline the strlen() function */
3774 /* We need a label in both cases */
3775 unsigned label = GetLabel ();
3777 /* Two different encodings */
3778 if (flags & CF_CONST) {
3780 /* The address of the string is constant. Create the correct label name */
3781 char* lbuf = GetLabelName (flags, val, offs);
3783 /* Generate the strlen code */
3784 AddCodeLine ("\tldy\t#$FF");
3785 g_defloclabel (label);
3786 AddCodeLine ("\tiny");
3787 AddCodeLine ("\tlda\t%s,y", lbuf);
3788 AddCodeLine ("\tbne\tL%04X", label);
3789 AddCodeLine ("\ttax");
3790 AddCodeLine ("\ttya");
3794 /* Address not constant but in primary */
3796 /* This is too much code, so call strlen instead of inlining */
3797 AddCodeLine ("\tjsr\t_strlen");
3799 /* Inline the function */
3800 AddCodeLine ("\tsta\tptr1");
3801 AddCodeLine ("\tstx\tptr1+1");
3802 AddCodeLine ("\tldy\t#$FF");
3803 g_defloclabel (label);
3804 AddCodeLine ("\tiny");
3805 AddCodeLine ("\tlda\t(ptr1),y");
3806 AddCodeLine ("\tbne\tL%04X", label);
3807 AddCodeLine ("\ttax");
3808 AddCodeLine ("\ttya");