1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998 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 /*****************************************************************************/
39 #include "../common/version.h"
56 /*****************************************************************************/
58 /*****************************************************************************/
62 /* Compiler relative stk ptr */
67 SEG_INV = -1, /* Invalid segment */
75 static char* SegmentNames [4];
76 static char* SegmentHints [4] = {
77 "seg:code", "seg:rodata", "seg:data", "seg:bss"
82 /*****************************************************************************/
84 /*****************************************************************************/
88 static void typeerror (unsigned type)
89 /* Print an error message about an invalid operand type */
91 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
96 static void CheckLocalOffs (unsigned Offs)
97 /* Check the offset into the stack for 8bit range */
100 /* Too many local vars */
101 AddCodeLine (";*** Too many locals");
102 Error (ERR_TOO_MANY_LOCALS);
108 static char* GetLabelName (unsigned flags, unsigned long label, unsigned offs)
110 static char lbuf [128]; /* Label name */
112 /* Create the correct label name */
113 switch (flags & CF_ADDRMASK) {
116 /* Static memory cell */
117 sprintf (lbuf, "L%04X+%u", (unsigned)(label & 0xFFFF), offs);
122 sprintf (lbuf, "_%s+%u", (char*) label, offs);
126 /* Absolute address */
127 sprintf (lbuf, "$%04X", (unsigned)((label+offs) & 0xFFFF));
131 /* Variable in register bank */
132 sprintf (lbuf, "regbank+%u", (unsigned)((label+offs) & 0xFFFF));
136 Internal ("Invalid address flags");
139 /* Return a pointer to the static buffer */
145 /*****************************************************************************/
146 /* Pre- and postamble */
147 /*****************************************************************************/
151 void g_preamble (void)
152 /* Generate the assembler code preamble */
154 AddCodeLine ("; File generated by cc65 v %u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
157 /* Insert some object file options */
158 AddCodeLine (".fopt\t\tcompiler,\"cc65 v %u.%u.%u\"", VER_MAJOR, VER_MINOR, VER_PATCH);
161 /* If we're producing code for some other CPU, switch the command set */
162 if (CPU == CPU_65C02) {
163 AddCodeLine (".pc02");
166 /* Allow auto import for runtime library routines */
167 AddCodeLine (".autoimport\ton");
169 /* Switch the assembler into case sensible mode */
170 AddCodeLine (".case\t\ton");
172 /* Tell the assembler if we want to generate debug info */
173 AddCodeLine (".debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
175 /* Import the stack pointer for direct auto variable access */
176 AddCodeLine (".importzp\tsp, sreg, regsave, regbank, tmp1, ptr1");
178 /* Define long branch macros */
179 AddCodeLine (".macpack\tlongbranch");
182 /* Define the ldax macro */
183 AddCodeLine (".macro ldax Value");
184 AddCodeLine (" lda #<(Value)");
185 AddCodeLine (" ldx #>(Value)");
186 AddCodeLine (".endmacro");
189 /* Define the default names for the segments */
190 SegmentNames [SEG_CODE] = xstrdup ("CODE");
191 SegmentNames [SEG_RODATA] = xstrdup ("RODATA");
192 SegmentNames [SEG_DATA] = xstrdup ("DATA");
193 SegmentNames [SEG_BSS] = xstrdup ("BSS");
195 /* Tell the optimizer that this is the end of the preamble */
196 AddCodeHint ("end_of_preamble");
201 void g_postamble (void)
202 /* Generate assembler code postamble */
204 /* Tell the optimizer that this is the start of the postamble */
205 AddCodeHint ("start_of_postamble");
210 /*****************************************************************************/
211 /* Segment support */
212 /*****************************************************************************/
216 static void UseSeg (int NewSeg)
217 /* Switch to a specific segment */
219 if (CurSeg != NewSeg) {
221 AddCodeLine (".segment\t\"%s\"", SegmentNames [CurSeg]);
222 AddCodeHint (SegmentHints [CurSeg]);
228 void g_usecode (void)
229 /* Switch to the code segment */
236 void g_userodata (void)
237 /* Switch to the read only data segment */
244 void g_usedata (void)
245 /* Switch to the data segment */
253 /* Switch to the bss segment */
260 static void SegName (int Seg, const char* Name)
261 /* Set the name of a segment */
263 /* Free the old name and set a new one */
264 xfree (SegmentNames [Seg]);
265 SegmentNames [Seg] = xstrdup (Name);
267 /* If the new segment is the current segment, emit a segment directive
271 CurSeg = SEG_INV; /* Invalidate */
278 void g_codename (const char* Name)
279 /* Set the name of the CODE segment */
281 SegName (SEG_CODE, Name);
286 void g_rodataname (const char* Name)
287 /* Set the name of the RODATA segment */
289 SegName (SEG_RODATA, Name);
294 void g_dataname (const char* Name)
295 /* Set the name of the DATA segment */
297 SegName (SEG_DATA, Name);
302 void g_bssname (const char* Name)
303 /* Set the name of the BSS segment */
305 SegName (SEG_BSS, Name);
310 /*****************************************************************************/
312 /*****************************************************************************/
316 unsigned sizeofarg (unsigned flags)
317 /* Return the size of a function argument type that is encoded in flags */
319 switch (flags & CF_TYPE) {
322 return (flags & CF_FORCECHAR)? 1 : 2;
339 int pop (unsigned flags)
340 /* Pop an argument of the given size */
342 return oursp += sizeofarg (flags);
347 int push (unsigned flags)
348 /* Push an argument of the given size */
350 return oursp -= sizeofarg (flags);
355 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
356 /* The value in Offs is an offset to an address in a/x. Make sure, an object
357 * of the type given in Flags can be loaded or stored into this address by
358 * adding part of the offset to the address in ax, so that the remaining
359 * offset fits into an index register. Return the remaining offset.
362 /* If the offset is too large for a byte register, add the high byte
363 * of the offset to the primary. Beware: We need a special correction
364 * if the offset in the low byte will overflow in the operation.
366 unsigned O = Offs & ~0xFFU;
367 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
368 /* We need to add the low byte also */
372 /* Do the correction if we need one */
374 g_inc (CF_INT | CF_CONST, O);
378 /* Return the new offset */
384 /*****************************************************************************/
385 /* Functions handling local labels */
386 /*****************************************************************************/
390 void g_defloclabel (unsigned label)
391 /* Define a local label */
393 AddCodeLine ("L%04X:", label & 0xFFFF);
398 /*****************************************************************************/
399 /* Functions handling global labels */
400 /*****************************************************************************/
404 void g_defgloblabel (const char* Name)
405 /* Define a global label with the given name */
407 AddCodeLine ("_%s:", Name);
412 void g_defexport (const char* Name, int ZP)
413 /* Export the given label */
416 AddCodeLine ("\t.exportzp\t_%s", Name);
418 AddCodeLine ("\t.export\t\t_%s", Name);
424 void g_defimport (const char* Name, int ZP)
425 /* Import the given label */
428 AddCodeLine ("\t.importzp\t_%s", Name);
430 AddCodeLine ("\t.import\t\t_%s", Name);
436 /*****************************************************************************/
437 /* Load functions for various registers */
438 /*****************************************************************************/
442 static void ldaconst (unsigned val)
443 /* Load a with a constant */
445 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
450 static void ldxconst (unsigned val)
451 /* Load x with a constant */
453 AddCodeLine ("\tldx\t#$%02X", val & 0xFF);
458 static void ldyconst (unsigned val)
459 /* Load y with a constant */
461 AddCodeLine ("\tldy\t#$%02X", val & 0xFF);
466 /*****************************************************************************/
467 /* Function entry and exit */
468 /*****************************************************************************/
472 /* Remember the argument size of a function. The variable is set by g_enter
473 * and used by g_leave. If the functions gets its argument size by the caller
474 * (variable param list or function without prototype), g_enter will set the
480 void g_enter (unsigned flags, unsigned argsize)
481 /* Function prologue */
483 if ((flags & CF_FIXARGC) != 0) {
484 /* Just remember the argument size for the leave */
488 AddCodeLine ("\tjsr\tenter");
494 void g_leave (int flags, int val)
495 /* Function epilogue */
500 /* How many bytes of locals do we have to drop? */
503 /* If we didn't have a variable argument list, don't call leave */
506 /* Load a function return code if needed */
507 if ((flags & CF_CONST) != 0) {
508 g_getimmed (flags, val, 0);
511 /* Drop stackframe or leave with rts */
514 AddCodeLine ("\trts");
516 AddCodeLine ("\tjmp\tincsp%d", k);
520 AddCodeLine ("\tjmp\taddysp");
525 strcpy (buf, "\tjmp\tleave");
527 /* We've a stack frame to drop */
531 if (flags & CF_CONST) {
532 if ((flags & CF_TYPE) != CF_LONG) {
533 /* Constant int sized value given for return code */
535 /* Special case: return 0 */
537 } else if (((val >> 8) & 0xFF) == 0) {
538 /* Special case: constant with high byte zero */
539 ldaconst (val); /* Load low byte */
542 /* Others: arbitrary constant value */
543 g_getimmed (flags, val, 0); /* Load value */
546 /* Constant long value: No shortcut possible */
547 g_getimmed (flags, val, 0);
551 /* Output the jump */
555 /* Add an empty line after a function to make the code more readable */
561 /*****************************************************************************/
562 /* Register variables */
563 /*****************************************************************************/
567 void g_save_regvars (int RegOffs, unsigned Bytes)
568 /* Save register variables */
570 /* Don't loop for up to two bytes */
573 AddCodeLine ("\tlda\tregbank%+d", RegOffs);
574 AddCodeLine ("\tjsr\tpusha");
576 } else if (Bytes == 2) {
578 AddCodeLine ("\tlda\tregbank%+d", RegOffs);
579 AddCodeLine ("\tldx\tregbank%+d", RegOffs+1);
580 AddCodeLine ("\tjsr\tpushax");
584 /* More than two bytes - loop */
585 unsigned Label = GetLabel ();
587 ldyconst (Bytes - 1);
589 g_defloclabel (Label);
590 AddCodeLine ("\tlda\tregbank%+d,x", RegOffs-1);
591 AddCodeLine ("\tsta\t(sp),y");
592 AddCodeLine ("\tdey");
593 AddCodeLine ("\tdex");
594 AddCodeLine ("\tbne\tL%04X", Label);
598 /* We pushed stuff, correct the stack pointer */
604 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
605 /* Restore register variables */
607 /* Calculate the actual stack offset and check it */
609 CheckLocalOffs (StackOffs);
611 /* Don't loop for up to two bytes */
614 ldyconst (StackOffs);
615 AddCodeLine ("\tlda\t(sp),y");
616 AddCodeLine ("\tsta\tregbank%+d", RegOffs);
618 } else if (Bytes == 2) {
620 ldyconst (StackOffs);
621 AddCodeLine ("\tlda\t(sp),y");
622 AddCodeLine ("\tsta\tregbank%+d", RegOffs);
623 AddCodeLine ("\tiny");
624 AddCodeLine ("\tlda\t(sp),y");
625 AddCodeLine ("\tsta\tregbank%+d", RegOffs+1);
629 /* More than two bytes - loop */
630 unsigned Label = GetLabel ();
631 ldyconst (StackOffs+Bytes-1);
633 g_defloclabel (Label);
634 AddCodeLine ("\tlda\t(sp),y");
635 AddCodeLine ("\tsta\tregbank%+d,x", RegOffs-1);
636 AddCodeLine ("\tdey");
637 AddCodeLine ("\tdex");
638 AddCodeLine ("\tbne\tL%04X", Label);
645 /*****************************************************************************/
646 /* Fetching memory cells */
647 /*****************************************************************************/
651 void g_getimmed (unsigned flags, unsigned long val, unsigned offs)
652 /* Load a constant into the primary register */
654 if ((flags & CF_CONST) != 0) {
656 /* Numeric constant */
657 switch (flags & CF_TYPE) {
660 if ((flags & CF_FORCECHAR) != 0) {
666 ldxconst ((val >> 8) & 0xFF);
667 ldaconst (val & 0xFF);
672 AddCodeLine ("\tldx\t#$00");
673 AddCodeLine ("\tstx\tsreg+1");
674 AddCodeLine ("\tstx\tsreg");
675 AddCodeLine ("\tlda\t#$%02X", (unsigned char) val);
676 } else if ((val & 0xFFFF00FF) == 0) {
677 AddCodeLine ("\tlda\t#$00");
678 AddCodeLine ("\tsta\tsreg+1");
679 AddCodeLine ("\tsta\tsreg");
680 AddCodeLine ("\tldx\t#$%02X", (unsigned char) (val >> 8));
681 } else if ((val & 0xFFFF0000) == 0 && FavourSize == 0) {
682 AddCodeLine ("\tlda\t#$00");
683 AddCodeLine ("\tsta\tsreg+1");
684 AddCodeLine ("\tsta\tsreg");
685 AddCodeLine ("\tlda\t#$%02X", (unsigned char) val);
686 AddCodeLine ("\tldx\t#$%02X", (unsigned char) (val >> 8));
687 } else if ((val & 0xFFFFFF00) == 0xFFFFFF00) {
688 AddCodeLine ("\tldx\t#$FF");
689 AddCodeLine ("\tstx\tsreg+1");
690 AddCodeLine ("\tstx\tsreg");
691 if ((val & 0xFF) == 0xFF) {
692 AddCodeLine ("\ttxa");
694 AddCodeLine ("\tlda\t#$%02X", (unsigned char) val);
696 } else if ((val & 0xFFFF00FF) == 0xFFFF00FF) {
697 AddCodeLine ("\tlda\t#$FF");
698 AddCodeLine ("\tsta\tsreg+1");
699 AddCodeLine ("\tsta\tsreg");
700 AddCodeLine ("\tldx\t#$%02X", (unsigned char) (val >> 8));
702 /* Call a subroutine that will load following value */
703 AddCodeLine ("\tjsr\tldeax");
704 AddCodeLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
716 /* Some sort of label */
717 const char* Label = GetLabelName (flags, val, offs);
719 /* Load the address into the primary */
720 AddCodeLine ("\tldax\t%s", Label);
727 void g_getstatic (unsigned flags, unsigned long label, unsigned offs)
728 /* Fetch an static memory cell into the primary register */
730 /* Create the correct label name */
731 char* lbuf = GetLabelName (flags, label, offs);
733 /* Check the size and generate the correct load operation */
734 switch (flags & CF_TYPE) {
737 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
738 AddCodeLine ("\tlda\t%s", lbuf); /* load A from the label */
741 AddCodeLine ("\tlda\t%s", lbuf); /* load A from the label */
742 if (!(flags & CF_UNSIGNED)) {
743 /* Must sign extend */
744 AddCodeLine ("\tbpl\t*+3");
745 AddCodeLine ("\tdex");
746 AddCodeHint ("x:!"); /* X is invalid now */
752 AddCodeLine ("\tlda\t%s", lbuf);
753 if (flags & CF_TEST) {
754 AddCodeLine ("\tora\t%s+1", lbuf);
756 AddCodeLine ("\tldx\t%s+1", lbuf);
761 if (flags & CF_TEST) {
762 AddCodeLine ("\tlda\t%s+3", lbuf);
763 AddCodeLine ("\tora\t%s+2", lbuf);
764 AddCodeLine ("\tora\t%s+1", lbuf);
765 AddCodeLine ("\tora\t%s+0", lbuf);
767 AddCodeLine ("\tlda\t%s+3", lbuf);
768 AddCodeLine ("\tsta\tsreg+1");
769 AddCodeLine ("\tlda\t%s+2", lbuf);
770 AddCodeLine ("\tsta\tsreg");
771 AddCodeLine ("\tldx\t%s+1", lbuf);
772 AddCodeLine ("\tlda\t%s", lbuf);
784 void g_getlocal (unsigned flags, int offs)
785 /* Fetch specified local object (local var). */
788 CheckLocalOffs (offs);
789 switch (flags & CF_TYPE) {
792 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
793 if (CPU == CPU_65C02 && offs == 0) {
794 AddCodeLine ("\tlda\t(sp)");
797 AddCodeLine ("\tlda\t(sp),y");
801 AddCodeLine ("\tldx\t#$00");
802 AddCodeLine ("\tlda\t(sp,x)");
805 AddCodeLine ("\tldx\t#$00");
806 AddCodeLine ("\tlda\t(sp),y");
808 if ((flags & CF_UNSIGNED) == 0) {
809 AddCodeLine ("\tbpl\t*+3");
810 AddCodeLine ("\tdex");
811 AddCodeHint ("x:!"); /* X is invalid now */
817 CheckLocalOffs (offs + 1);
818 if (flags & CF_TEST) {
820 AddCodeLine ("\tlda\t(sp),y");
821 AddCodeLine ("\tdey");
822 AddCodeLine ("\tora\t(sp),y");
827 AddCodeLine ("\tjsr\tldaxysp");
829 AddCodeLine ("\tjsr\tldax0sp");
833 AddCodeLine ("\tlda\t(sp),y");
834 AddCodeLine ("\ttax");
835 AddCodeLine ("\tdey");
836 AddCodeLine ("\tlda\t(sp),y");
844 AddCodeLine ("\tjsr\tldeaxysp");
846 AddCodeLine ("\tjsr\tldeax0sp");
857 void g_getind (unsigned flags, unsigned offs)
858 /* Fetch the specified object type indirect through the primary register
859 * into the primary register
862 /* If the offset is greater than 255, add the part that is > 255 to
863 * the primary. This way we get an easy addition and use the low byte
866 offs = MakeByteOffs (flags, offs);
868 /* Handle the indirect fetch */
869 switch (flags & CF_TYPE) {
872 /* Character sized */
875 if (flags & CF_UNSIGNED) {
876 AddCodeLine ("\tjsr\tldauidx");
878 AddCodeLine ("\tjsr\tldaidx");
881 if (flags & CF_UNSIGNED) {
883 AddCodeLine ("\tjsr\tldaui");
885 AddCodeLine ("\tsta\tptr1");
886 AddCodeLine ("\tstx\tptr1+1");
887 AddCodeLine ("\tldx\t#$00");
888 AddCodeLine ("\tlda\t(ptr1,x)");
891 AddCodeLine ("\tjsr\tldai");
897 if (flags & CF_TEST) {
899 AddCodeLine ("\tsta\tptr1");
900 AddCodeLine ("\tstx\tptr1+1");
901 AddCodeLine ("\tlda\t(ptr1),y");
902 AddCodeLine ("\tiny");
903 AddCodeLine ("\tora\t(ptr1),y");
906 AddCodeLine ("\tjsr\tldaxi");
909 AddCodeLine ("\tjsr\tldaxidx");
916 AddCodeLine ("\tjsr\tldeaxi");
919 AddCodeLine ("\tjsr\tldeaxidx");
921 if (flags & CF_TEST) {
922 AddCodeLine ("\tjsr\ttsteax");
934 void g_leasp (int offs)
935 /* Fetch the address of the specified symbol into the primary register */
937 /* Calculate the offset relative to sp */
940 /* For value 0 we do direct code */
942 AddCodeLine ("\tlda\tsp");
943 AddCodeLine ("\tldx\tsp+1");
946 ldaconst (offs); /* Load A with offset value */
947 AddCodeLine ("\tjsr\tleaasp"); /* Load effective address */
950 AddCodeLine ("\tclc");
951 AddCodeLine ("\tldx\tsp+1");
952 AddCodeLine ("\tadc\tsp");
953 AddCodeLine ("\tbcc\t*+3");
954 AddCodeLine ("\tinx");
955 AddCodeHint ("x:!"); /* Invalidate X */
962 /*****************************************************************************/
963 /* Store into memory */
964 /*****************************************************************************/
968 void g_putstatic (unsigned flags, unsigned long label, unsigned offs)
969 /* Store the primary register into the specified static memory cell */
971 /* Create the correct label name */
972 char* lbuf = GetLabelName (flags, label, offs);
974 /* Check the size and generate the correct store operation */
975 switch (flags & CF_TYPE) {
978 AddCodeLine ("\tsta\t%s", lbuf);
982 AddCodeLine ("\tsta\t%s", lbuf);
983 AddCodeLine ("\tstx\t%s+1", lbuf);
987 AddCodeLine ("\tsta\t%s", lbuf);
988 AddCodeLine ("\tstx\t%s+1", lbuf);
989 AddCodeLine ("\tldy\tsreg");
990 AddCodeLine ("\tsty\t%s+2", lbuf);
991 AddCodeLine ("\tldy\tsreg+1");
992 AddCodeLine ("\tsty\t%s+3", lbuf);
1003 void g_putlocal (unsigned flags, int offs)
1004 /* Put data into local object. */
1007 CheckLocalOffs (offs);
1008 switch (flags & CF_TYPE) {
1011 if (CPU == CPU_65C02 && offs == 0) {
1012 AddCodeLine ("\tsta\t(sp)");
1015 AddCodeLine ("\tsta\t(sp),y");
1022 AddCodeLine ("\tjsr\tstaxysp");
1024 AddCodeLine ("\tjsr\tstax0sp");
1031 AddCodeLine ("\tjsr\tsteaxysp");
1033 AddCodeLine ("\tjsr\tsteax0sp");
1045 void g_putind (unsigned flags, unsigned offs)
1046 /* Store the specified object type in the primary register at the address
1047 * on the top of the stack
1050 /* We cannot currently handle more than byte sized offsets */
1051 if (offs > 256 - sizeofarg (flags)) {
1052 Internal ("g_putind: Large offsets not implemented");
1055 /* Check the size and determine operation */
1056 switch (flags & CF_TYPE) {
1061 AddCodeLine ("\tjsr\tstaspidx");
1063 AddCodeLine ("\tjsr\tstaspp");
1070 AddCodeLine ("\tjsr\tstaxspidx");
1072 AddCodeLine ("\tjsr\tstaxspp");
1079 AddCodeLine ("\tjsr\tsteaxspidx");
1081 AddCodeLine ("\tjsr\tsteaxspp");
1090 /* Pop the argument which is always a pointer */
1096 /*****************************************************************************/
1097 /* type conversion and similiar stuff */
1098 /*****************************************************************************/
1102 void g_toslong (unsigned flags)
1103 /* Make sure, the value on TOS is a long. Convert if necessary */
1105 switch (flags & CF_TYPE) {
1109 if (flags & CF_UNSIGNED) {
1110 AddCodeLine ("\tjsr\ttosulong");
1112 AddCodeLine ("\tjsr\ttoslong");
1127 void g_tosint (unsigned flags)
1128 /* Make sure, the value on TOS is an int. Convert if necessary */
1130 switch (flags & CF_TYPE) {
1137 AddCodeLine ("\tjsr\ttosint");
1148 void g_reglong (unsigned flags)
1149 /* Make sure, the value in the primary register a long. Convert if necessary */
1151 switch (flags & CF_TYPE) {
1155 if (flags & CF_UNSIGNED) {
1157 AddCodeLine ("\tjsr\taxulong");
1160 AddCodeLine ("\tsty\tsreg");
1161 AddCodeLine ("\tsty\tsreg+1");
1164 AddCodeLine ("\tjsr\taxlong");
1178 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1179 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1180 * value, that corresponds to the value on TOS, rhs corresponds to the value
1181 * in (e)ax. The return value is the the flags value for the resulting type.
1184 unsigned ltype, rtype;
1187 /* Get the type spec from the flags */
1188 ltype = lhs & CF_TYPE;
1189 rtype = rhs & CF_TYPE;
1191 /* Check if a conversion is needed */
1192 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1193 /* We must promote the primary register to long */
1195 /* Get the new rhs type */
1196 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1198 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1199 /* We must promote the lhs to long */
1205 /* Get the new rhs type */
1206 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1210 /* Determine the result type for the operation:
1211 * - The result is const if both operands are const.
1212 * - The result is unsigned if one of the operands is unsigned.
1213 * - The result is long if one of the operands is long.
1214 * - Otherwise the result is int sized.
1216 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1217 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1218 if (rtype == CF_LONG || ltype == CF_LONG) {
1228 unsigned g_typecast (unsigned lhs, unsigned rhs)
1229 /* Cast the value in the primary register to the operand size that is flagged
1230 * by the lhs value. Return the result value.
1233 unsigned ltype, rtype;
1235 /* Get the type spec from the flags */
1236 ltype = lhs & CF_TYPE;
1237 rtype = rhs & CF_TYPE;
1239 /* Check if a conversion is needed */
1240 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1241 /* We must promote the primary register to long */
1245 /* Do not need any other action. If the left type is int, and the primary
1246 * register is long, it will be automagically truncated. If the right hand
1247 * side is const, it is not located in the primary register and handled by
1248 * the expression parser code.
1251 /* Result is const if the right hand side was const */
1252 lhs |= (rhs & CF_CONST);
1254 /* The resulting type is that of the left hand side (that's why you called
1262 void g_scale (unsigned flags, long val)
1263 /* Scale the value in the primary register by the given value. If val is positive,
1264 * scale up, is val is negative, scale down. This function is used to scale
1265 * the operands or results of pointer arithmetic by the size of the type, the
1266 * pointer points to.
1271 /* Value may not be zero */
1273 Internal ("Data type has no size");
1274 } else if (val > 0) {
1277 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1279 /* Factor is 2, 4 or 8, use special function */
1280 switch (flags & CF_TYPE) {
1283 if (flags & CF_FORCECHAR) {
1285 AddCodeLine ("\tasl\ta");
1292 if (FavourSize || p2 >= 3) {
1293 if (flags & CF_UNSIGNED) {
1294 AddCodeLine ("\tjsr\tshlax%d", p2);
1296 AddCodeLine ("\tjsr\taslax%d", p2);
1299 AddCodeLine ("\tstx\ttmp1");
1301 AddCodeLine ("\tasl\ta");
1302 AddCodeLine ("\trol\ttmp1");
1304 AddCodeLine ("\tldx\ttmp1");
1309 if (flags & CF_UNSIGNED) {
1310 AddCodeLine ("\tjsr\tshleax%d", p2);
1312 AddCodeLine ("\tjsr\tasleax%d", p2);
1321 } else if (val != 1) {
1323 /* Use a multiplication instead */
1324 g_mul (flags | CF_CONST, val);
1332 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1334 /* Factor is 2, 4 or 8, use special function */
1335 switch (flags & CF_TYPE) {
1338 if (flags & CF_FORCECHAR) {
1339 if (flags & CF_UNSIGNED) {
1341 AddCodeLine ("\tlsr\ta");
1344 } else if (p2 <= 2) {
1345 AddCodeLine ("\tcmp\t#$80");
1346 AddCodeLine ("\tror\ta");
1353 if (flags & CF_UNSIGNED) {
1354 if (FavourSize || p2 >= 3) {
1355 AddCodeLine ("\tjsr\tlsrax%d", p2);
1357 AddCodeLine ("\tstx\ttmp1");
1359 AddCodeLine ("\tlsr\ttmp1");
1360 AddCodeLine ("\tror\ta");
1362 AddCodeLine ("\tldx\ttmp1");
1365 if (FavourSize || p2 >= 3) {
1366 AddCodeLine ("\tjsr\tasrax%d", p2);
1368 AddCodeLine ("\tstx\ttmp1");
1370 AddCodeLine ("\tcpx\t#$80");
1371 AddCodeLine ("\tror\ttmp1");
1372 AddCodeLine ("\tror\ta");
1374 AddCodeLine ("\tldx\ttmp1");
1380 if (flags & CF_UNSIGNED) {
1381 AddCodeLine ("\tjsr\tlsreax%d", p2);
1383 AddCodeLine ("\tjsr\tasreax%d", p2);
1392 } else if (val != 1) {
1394 /* Use a division instead */
1395 g_div (flags | CF_CONST, val);
1403 /*****************************************************************************/
1404 /* Adds and subs of variables fix a fixed address */
1405 /*****************************************************************************/
1409 void g_addlocal (unsigned flags, int offs)
1410 /* Add a local variable to ax */
1412 /* Correct the offset and check it */
1414 CheckLocalOffs (offs);
1416 switch (flags & CF_TYPE) {
1419 AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1420 AddCodeLine ("\tclc");
1421 AddCodeLine ("\tadc\t(sp),y");
1422 AddCodeLine ("\tbcc\t*+3");
1423 AddCodeLine ("\tinx");
1424 AddCodeHint ("x:!");
1428 AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1429 AddCodeLine ("\tclc");
1430 AddCodeLine ("\tadc\t(sp),y");
1431 AddCodeLine ("\tpha");
1432 AddCodeLine ("\ttxa");
1433 AddCodeLine ("\tiny");
1434 AddCodeLine ("\tadc\t(sp),y");
1435 AddCodeLine ("\ttax");
1436 AddCodeLine ("\tpla");
1440 /* Do it the old way */
1442 g_getlocal (flags, offs);
1454 void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
1455 /* Add a static variable to ax */
1457 /* Create the correct label name */
1458 char* lbuf = GetLabelName (flags, label, offs);
1460 switch (flags & CF_TYPE) {
1463 AddCodeLine ("\tclc");
1464 AddCodeLine ("\tadc\t%s", lbuf);
1465 AddCodeLine ("\tbcc\t*+3");
1466 AddCodeLine ("\tinx");
1467 AddCodeHint ("x:!");
1471 AddCodeLine ("\tclc");
1472 AddCodeLine ("\tadc\t%s", lbuf);
1473 AddCodeLine ("\ttay");
1474 AddCodeLine ("\ttxa");
1475 AddCodeLine ("\tadc\t%s+1", lbuf);
1476 AddCodeLine ("\ttax");
1477 AddCodeLine ("\ttya");
1481 /* Do it the old way */
1483 g_getstatic (flags, label, offs);
1495 /*****************************************************************************/
1496 /* Compares of ax with a variable with fixed address */
1497 /*****************************************************************************/
1501 void g_cmplocal (unsigned flags, int offs)
1502 /* Compare a local variable to ax */
1504 Internal ("g_cmplocal not implemented");
1509 void g_cmpstatic (unsigned flags, unsigned label, unsigned offs)
1510 /* Compare a static variable to ax */
1512 Internal ("g_cmpstatic not implemented");
1517 /*****************************************************************************/
1518 /* Special op= functions */
1519 /*****************************************************************************/
1523 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
1525 /* Emit += for a static variable */
1527 /* Create the correct label name */
1528 char* lbuf = GetLabelName (flags, label, offs);
1530 /* Check the size and determine operation */
1531 switch (flags & CF_TYPE) {
1534 if (flags & CF_FORCECHAR) {
1535 AddCodeLine ("\tldx\t#$00");
1536 if (flags & CF_CONST) {
1538 AddCodeLine ("\tinc\t%s", lbuf);
1539 AddCodeLine ("\tlda\t%s", lbuf);
1541 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1542 AddCodeLine ("\tclc");
1543 AddCodeLine ("\tadc\t%s", lbuf);
1544 AddCodeLine ("\tsta\t%s", lbuf);
1547 AddCodeLine ("\tclc");
1548 AddCodeLine ("\tadc\t%s", lbuf);
1549 AddCodeLine ("\tsta\t%s", lbuf);
1551 if ((flags & CF_UNSIGNED) == 0) {
1552 AddCodeLine ("\tbpl\t*+3");
1553 AddCodeLine ("\tdex");
1554 AddCodeHint ("x:!"); /* Invalidate X */
1561 if (flags & CF_CONST) {
1563 label = GetLabel ();
1564 AddCodeLine ("\tinc\t%s", lbuf);
1565 AddCodeLine ("\tbne\tL%04X", label);
1566 AddCodeLine ("\tinc\t%s+1", lbuf);
1567 g_defloclabel (label);
1568 AddCodeLine ("\tlda\t%s", lbuf); /* Hmmm... */
1569 AddCodeLine ("\tldx\t%s+1", lbuf);
1571 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1572 AddCodeLine ("\tclc");
1573 AddCodeLine ("\tadc\t%s", lbuf);
1574 AddCodeLine ("\tsta\t%s", lbuf);
1576 label = GetLabel ();
1577 AddCodeLine ("\tbcc\tL%04X", label);
1578 AddCodeLine ("\tinc\t%s+1", lbuf);
1579 g_defloclabel (label);
1580 AddCodeLine ("\tldx\t%s+1", lbuf);
1582 AddCodeLine ("\tlda\t#$%02X", (val >> 8) & 0xFF);
1583 AddCodeLine ("\tadc\t%s+1", lbuf);
1584 AddCodeLine ("\tsta\t%s+1", lbuf);
1585 AddCodeLine ("\ttax");
1586 AddCodeLine ("\tlda\t%s", lbuf);
1590 AddCodeLine ("\tclc");
1591 AddCodeLine ("\tadc\t%s", lbuf);
1592 AddCodeLine ("\tsta\t%s", lbuf);
1593 AddCodeLine ("\ttxa");
1594 AddCodeLine ("\tadc\t%s+1", lbuf);
1595 AddCodeLine ("\tsta\t%s+1", lbuf);
1596 AddCodeLine ("\ttax");
1597 AddCodeLine ("\tlda\t%s", lbuf);
1602 if (flags & CF_CONST) {
1604 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1605 AddCodeLine ("\tsty\tptr1");
1606 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1608 AddCodeLine ("\tjsr\tladdeq1");
1610 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1611 AddCodeLine ("\tjsr\tladdeqa");
1614 g_getstatic (flags, label, offs);
1616 g_putstatic (flags, label, offs);
1619 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1620 AddCodeLine ("\tsty\tptr1");
1621 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1622 AddCodeLine ("\tjsr\tladdeq");
1633 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1634 /* Emit += for a local variable */
1636 /* Calculate the true offset, check it, load it into Y */
1638 CheckLocalOffs (offs);
1640 /* Check the size and determine operation */
1641 switch (flags & CF_TYPE) {
1644 if (flags & CF_FORCECHAR) {
1646 AddCodeLine ("\tldx\t#$00");
1647 if (flags & CF_CONST) {
1648 AddCodeLine ("\tclc");
1649 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1650 AddCodeLine ("\tadc\t(sp,x)");
1651 AddCodeLine ("\tsta\t(sp,x)");
1653 AddCodeLine ("\tclc");
1654 AddCodeLine ("\tadc\t(sp,x)");
1655 AddCodeLine ("\tsta\t(sp,x)");
1659 AddCodeLine ("\tldx\t#$00");
1660 if (flags & CF_CONST) {
1661 AddCodeLine ("\tclc");
1662 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1663 AddCodeLine ("\tadc\t(sp),y");
1664 AddCodeLine ("\tsta\t(sp),y");
1666 AddCodeLine ("\tclc");
1667 AddCodeLine ("\tadc\t(sp),y");
1668 AddCodeLine ("\tsta\t(sp),y");
1671 if ((flags & CF_UNSIGNED) == 0) {
1672 AddCodeLine ("\tbpl\t*+3");
1673 AddCodeLine ("\tdex");
1674 AddCodeHint ("x:!"); /* Invalidate X */
1681 if (flags & CF_CONST) {
1682 g_getimmed (flags, val, 0);
1685 AddCodeLine ("\tjsr\taddeq0sp");
1688 AddCodeLine ("\tjsr\taddeqysp");
1693 if (flags & CF_CONST) {
1694 g_getimmed (flags, val, 0);
1697 AddCodeLine ("\tjsr\tladdeq0sp");
1700 AddCodeLine ("\tjsr\tladdeqysp");
1711 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1712 /* Emit += for the location with address in ax */
1714 /* If the offset is too large for a byte register, add the high byte
1715 * of the offset to the primary. Beware: We need a special correction
1716 * if the offset in the low byte will overflow in the operation.
1718 offs = MakeByteOffs (flags, offs);
1720 /* Check the size and determine operation */
1721 switch (flags & CF_TYPE) {
1724 AddCodeLine ("\tsta\tptr1");
1725 AddCodeLine ("\tstx\tptr1+1");
1727 AddCodeLine ("\tldx\t#$00");
1728 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1729 AddCodeLine ("\tclc");
1730 AddCodeLine ("\tadc\t(ptr1,x)");
1731 AddCodeLine ("\tsta\t(ptr1,x)");
1733 AddCodeLine ("\tldy\t#$%02X", offs);
1734 AddCodeLine ("\tldx\t#$00");
1735 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1736 AddCodeLine ("\tclc");
1737 AddCodeLine ("\tadc\t(ptr1),y");
1738 AddCodeLine ("\tsta\t(ptr1),y");
1744 /* Lots of code, use only if size is not important */
1745 AddCodeLine ("\tsta\tptr1");
1746 AddCodeLine ("\tstx\tptr1+1");
1747 AddCodeLine ("\tldy\t#$%02X", offs);
1748 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1749 AddCodeLine ("\tclc");
1750 AddCodeLine ("\tadc\t(ptr1),y");
1751 AddCodeLine ("\tsta\t(ptr1),y");
1752 AddCodeLine ("\tpha");
1753 AddCodeLine ("\tiny");
1754 AddCodeLine ("\tlda\t#$%02X", (val >> 8) & 0xFF);
1755 AddCodeLine ("\tadc\t(ptr1),y");
1756 AddCodeLine ("\tsta\t(ptr1),y");
1757 AddCodeLine ("\ttax");
1758 AddCodeLine ("\tpla");
1764 AddCodeLine ("\tjsr\tpushax"); /* Push the address */
1765 push (flags); /* Correct the internal sp */
1766 g_getind (flags, offs); /* Fetch the value */
1767 g_inc (flags, val); /* Increment value in primary */
1768 g_putind (flags, offs); /* Store the value back */
1778 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1780 /* Emit -= for a static variable */
1782 /* Create the correct label name */
1783 char* lbuf = GetLabelName (flags, label, offs);
1785 /* Check the size and determine operation */
1786 switch (flags & CF_TYPE) {
1789 if (flags & CF_FORCECHAR) {
1790 AddCodeLine ("\tldx\t#$00");
1791 if (flags & CF_CONST) {
1793 AddCodeLine ("\tdec\t%s", lbuf);
1794 AddCodeLine ("\tlda\t%s", lbuf);
1796 AddCodeLine ("\tsec");
1797 AddCodeLine ("\tlda\t%s", lbuf);
1798 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
1799 AddCodeLine ("\tsta\t%s", lbuf);
1802 AddCodeLine ("\tsec");
1803 AddCodeLine ("\tsta\ttmp1");
1804 AddCodeLine ("\tlda\t%s", lbuf);
1805 AddCodeLine ("\tsbc\ttmp1");
1806 AddCodeLine ("\tsta\t%s", lbuf);
1808 if ((flags & CF_UNSIGNED) == 0) {
1809 AddCodeLine ("\tbpl\t*+3");
1810 AddCodeLine ("\tdex");
1811 AddCodeHint ("x:!"); /* Invalidate X */
1818 AddCodeLine ("\tsec");
1819 if (flags & CF_CONST) {
1820 AddCodeLine ("\tlda\t%s", lbuf);
1821 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
1822 AddCodeLine ("\tsta\t%s", lbuf);
1824 label = GetLabel ();
1825 AddCodeLine ("\tbcs\tL%04X", label);
1826 AddCodeLine ("\tdec\t%s+1", lbuf);
1827 g_defloclabel (label);
1828 AddCodeLine ("\tldx\t%s+1", lbuf);
1830 AddCodeLine ("\tlda\t%s+1", lbuf);
1831 AddCodeLine ("\tsbc\t#$%02X", (val >> 8) & 0xFF);
1832 AddCodeLine ("\tsta\t%s+1", lbuf);
1833 AddCodeLine ("\ttax");
1834 AddCodeLine ("\tlda\t%s", lbuf);
1837 AddCodeLine ("\tsta\ttmp1");
1838 AddCodeLine ("\tlda\t%s", lbuf);
1839 AddCodeLine ("\tsbc\ttmp1");
1840 AddCodeLine ("\tsta\t%s", lbuf);
1841 AddCodeLine ("\tstx\ttmp1");
1842 AddCodeLine ("\tlda\t%s+1", lbuf);
1843 AddCodeLine ("\tsbc\ttmp1");
1844 AddCodeLine ("\tsta\t%s+1", lbuf);
1845 AddCodeLine ("\ttax");
1846 AddCodeLine ("\tlda\t%s", lbuf);
1851 if (flags & CF_CONST) {
1853 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1854 AddCodeLine ("\tsty\tptr1");
1855 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1857 AddCodeLine ("\tjsr\tlsubeq1");
1859 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1860 AddCodeLine ("\tjsr\tlsubeqa");
1863 g_getstatic (flags, label, offs);
1865 g_putstatic (flags, label, offs);
1868 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1869 AddCodeLine ("\tsty\tptr1");
1870 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1871 AddCodeLine ("\tjsr\tlsubeq");
1882 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1883 /* Emit -= for a local variable */
1885 /* Calculate the true offset, check it, load it into Y */
1887 CheckLocalOffs (offs);
1889 /* Check the size and determine operation */
1890 switch (flags & CF_TYPE) {
1893 if (flags & CF_FORCECHAR) {
1895 AddCodeLine ("\tldx\t#$00");
1896 AddCodeLine ("\tsec");
1897 if (flags & CF_CONST) {
1898 AddCodeLine ("\tlda\t(sp),y");
1899 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
1901 AddCodeLine ("\tsta\ttmp1");
1902 AddCodeLine ("\tlda\t(sp),y");
1903 AddCodeLine ("\tsbc\ttmp1");
1905 AddCodeLine ("\tsta\t(sp),y");
1906 if ((flags & CF_UNSIGNED) == 0) {
1907 AddCodeLine ("\tbpl\t*+3");
1908 AddCodeLine ("\tdex");
1909 AddCodeHint ("x:!"); /* Invalidate X */
1916 if (flags & CF_CONST) {
1917 g_getimmed (flags, val, 0);
1920 AddCodeLine ("\tjsr\tsubeq0sp");
1923 AddCodeLine ("\tjsr\tsubeqysp");
1928 if (flags & CF_CONST) {
1929 g_getimmed (flags, val, 0);
1932 AddCodeLine ("\tjsr\tlsubeq0sp");
1935 AddCodeLine ("\tjsr\tlsubeqysp");
1946 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
1947 /* Emit -= for the location with address in ax */
1949 /* If the offset is too large for a byte register, add the high byte
1950 * of the offset to the primary. Beware: We need a special correction
1951 * if the offset in the low byte will overflow in the operation.
1953 offs = MakeByteOffs (flags, offs);
1955 /* Check the size and determine operation */
1956 switch (flags & CF_TYPE) {
1959 AddCodeLine ("\tsta\tptr1");
1960 AddCodeLine ("\tstx\tptr1+1");
1962 AddCodeLine ("\tldx\t#$00");
1963 AddCodeLine ("\tlda\t(ptr1,x)");
1964 AddCodeLine ("\tsec");
1965 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
1966 AddCodeLine ("\tsta\t(ptr1,x)");
1968 AddCodeLine ("\tldy\t#$%02X", offs);
1969 AddCodeLine ("\tldx\t#$00");
1970 AddCodeLine ("\tlda\t(ptr1),y");
1971 AddCodeLine ("\tsec");
1972 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
1973 AddCodeLine ("\tsta\t(ptr1),y");
1979 /* Lots of code, use only if size is not important */
1980 AddCodeLine ("\tsta\tptr1");
1981 AddCodeLine ("\tstx\tptr1+1");
1982 AddCodeLine ("\tldy\t#$%02X", offs);
1983 AddCodeLine ("\tlda\t(ptr1),y");
1984 AddCodeLine ("\tsec");
1985 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
1986 AddCodeLine ("\tsta\t(ptr1),y");
1987 AddCodeLine ("\tpha");
1988 AddCodeLine ("\tiny");
1989 AddCodeLine ("\tlda\t(ptr1),y");
1990 AddCodeLine ("\tsbc\t#$%02X", (val >> 8) & 0xFF);
1991 AddCodeLine ("\tsta\t(ptr1),y");
1992 AddCodeLine ("\ttax");
1993 AddCodeLine ("\tpla");
1999 AddCodeLine ("\tjsr\tpushax"); /* Push the address */
2000 push (flags); /* Correct the internal sp */
2001 g_getind (flags, offs); /* Fetch the value */
2002 g_dec (flags, val); /* Increment value in primary */
2003 g_putind (flags, offs); /* Store the value back */
2013 /*****************************************************************************/
2014 /* Add a variable address to the value in ax */
2015 /*****************************************************************************/
2019 void g_addaddr_local (unsigned flags, int offs)
2020 /* Add the address of a local variable to ax */
2022 /* Add the offset */
2025 /* We cannot address more then 256 bytes of locals anyway */
2026 CheckLocalOffs (offs);
2027 AddCodeLine ("\tclc");
2028 AddCodeLine ("\tadc\t#$%02X", offs & 0xFF);
2029 AddCodeLine ("\tbcc\t*+4"); /* Do also skip the CLC insn below */
2030 AddCodeLine ("\tinx");
2031 AddCodeHint ("x:!"); /* Invalidate X */
2034 /* Add the current stackpointer value */
2035 AddCodeLine ("\tclc");
2036 AddCodeLine ("\tadc\tsp");
2037 AddCodeLine ("\ttay");
2038 AddCodeLine ("\ttxa");
2039 AddCodeLine ("\tadc\tsp+1");
2040 AddCodeLine ("\ttax");
2041 AddCodeLine ("\ttya");
2046 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2047 /* Add the address of a static variable to ax */
2049 /* Create the correct label name */
2050 char* lbuf = GetLabelName (flags, label, offs);
2052 /* Add the address to the current ax value */
2053 AddCodeLine ("\tclc");
2054 AddCodeLine ("\tadc\t#<(%s)", lbuf);
2055 AddCodeLine ("\ttay");
2056 AddCodeLine ("\ttxa");
2057 AddCodeLine ("\tadc\t#>(%s)", lbuf);
2058 AddCodeLine ("\ttax");
2059 AddCodeLine ("\ttya");
2064 /*****************************************************************************/
2066 /*****************************************************************************/
2070 void g_save (unsigned flags)
2071 /* Copy primary register to hold register. */
2073 /* Check the size and determine operation */
2074 switch (flags & CF_TYPE) {
2077 if (flags & CF_FORCECHAR) {
2078 AddCodeLine ("\tpha");
2084 AddCodeLine ("\tsta\tregsave");
2085 AddCodeLine ("\tstx\tregsave+1");
2089 AddCodeLine ("\tjsr\tsaveeax");
2099 void g_restore (unsigned flags)
2100 /* Copy hold register to P. */
2102 /* Check the size and determine operation */
2103 switch (flags & CF_TYPE) {
2106 if (flags & CF_FORCECHAR) {
2107 AddCodeLine ("\tpla");
2113 AddCodeLine ("\tlda\tregsave");
2114 AddCodeLine ("\tldx\tregsave+1");
2118 AddCodeLine ("\tjsr\tresteax");
2128 void g_cmp (unsigned flags, unsigned long val)
2129 /* Immidiate compare. The primary register will not be changed, Z flag
2133 /* Check the size and determine operation */
2134 switch (flags & CF_TYPE) {
2137 if (flags & CF_FORCECHAR) {
2138 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
2144 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
2145 AddCodeLine ("\tbne\t*+4");
2146 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
2150 Internal ("g_cmp: Long compares not implemented");
2160 static void oper (unsigned flags, unsigned long val, char** subs)
2161 /* Encode a binary operation. subs is a pointer to four groups of three
2163 * 0-2 --> Operate on ints
2164 * 3-5 --> Operate on unsigneds
2165 * 6-8 --> Operate on longs
2166 * 9-11 --> Operate on unsigned longs
2168 * The first subroutine names in each string group is used to encode an
2169 * operation with a zero constant, the second to encode an operation with
2170 * a 8 bit constant, and the third is used in all other cases.
2175 /* Determine the offset into the array */
2176 offs = (flags & CF_UNSIGNED)? 3 : 0;
2177 switch (flags & CF_TYPE) {
2190 /* Encode the operation */
2191 if (flags & CF_CONST) {
2192 /* Constant value given */
2193 if (val == 0 && subs [offs+0]) {
2194 /* Special case: constant with value zero */
2195 AddCodeLine ("\tjsr\t%s", subs [offs+0]);
2196 } else if (val < 0x100 && subs [offs+1]) {
2197 /* Special case: constant with high byte zero */
2198 ldaconst (val); /* Load low byte */
2199 AddCodeLine ("\tjsr\t%s", subs [offs+1]);
2201 /* Others: arbitrary constant value */
2202 g_getimmed (flags, val, 0); /* Load value */
2203 AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2206 /* Value not constant (is already in (e)ax) */
2207 AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2210 /* The operation will pop it's argument */
2216 void g_test (unsigned flags)
2217 /* Force a test to set cond codes right */
2219 switch (flags & CF_TYPE) {
2222 if (flags & CF_FORCECHAR) {
2223 AddCodeLine ("\ttax");
2229 AddCodeLine ("\tstx\ttmp1");
2230 AddCodeLine ("\tora\ttmp1");
2234 if (flags & CF_UNSIGNED) {
2235 AddCodeLine ("\tjsr\tutsteax");
2237 AddCodeLine ("\tjsr\ttsteax");
2249 void g_push (unsigned flags, unsigned long val)
2250 /* Push the primary register or a constant value onto the stack */
2254 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2256 /* We have a constant 8 or 16 bit value */
2257 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2259 /* Handle as 8 bit value */
2260 if (FavourSize && val <= 2) {
2261 AddCodeLine ("\tjsr\tpushc%d", (int) val);
2264 AddCodeLine ("\tjsr\tpusha");
2269 /* Handle as 16 bit value */
2270 hi = (unsigned char) (val >> 8);
2272 AddCodeLine ("\tjsr\tpush%u", (unsigned) val);
2273 } else if (hi == 0 || hi == 0xFF) {
2274 /* Use special function */
2276 AddCodeLine ("\tjsr\t%s", (hi == 0)? "pusha0" : "pushaFF");
2279 g_getimmed (flags, val, 0);
2280 AddCodeLine ("\tjsr\tpushax");
2286 /* Value is not 16 bit or not constant */
2287 if (flags & CF_CONST) {
2288 /* Constant 32 bit value, load into eax */
2289 g_getimmed (flags, val, 0);
2292 /* Push the primary register */
2293 switch (flags & CF_TYPE) {
2296 if (flags & CF_FORCECHAR) {
2297 /* Handle as char */
2298 AddCodeLine ("\tjsr\tpusha");
2303 AddCodeLine ("\tjsr\tpushax");
2307 AddCodeLine ("\tjsr\tpusheax");
2317 /* Adjust the stack offset */
2323 void g_swap (unsigned flags)
2324 /* Swap the primary register and the top of the stack. flags give the type
2325 * of *both* values (must have same size).
2328 switch (flags & CF_TYPE) {
2332 AddCodeLine ("\tjsr\tswapstk");
2336 AddCodeLine ("\tjsr\tswapestk");
2347 void g_call (unsigned flags, char* lbl, unsigned argsize)
2348 /* Call the specified subroutine name */
2350 if ((flags & CF_FIXARGC) == 0) {
2351 /* Pass arg count */
2354 AddCodeLine ("\tjsr\t_%s", lbl);
2355 oursp += argsize; /* callee pops args */
2360 void g_callind (unsigned flags, unsigned argsize)
2361 /* Call subroutine with address in AX */
2363 if ((flags & CF_FIXARGC) == 0) {
2364 /* Pass arg count */
2367 AddCodeLine ("\tjsr\tcallax"); /* do the call */
2368 oursp += argsize; /* callee pops args */
2373 void g_jump (unsigned label)
2374 /* Jump to specified internal label number */
2376 AddCodeLine ("\tjmp\tL%04X", label);
2381 void g_switch (unsigned flags)
2382 /* Output switch statement preample */
2384 switch (flags & CF_TYPE) {
2388 AddCodeLine ("\tjsr\tswitch");
2392 AddCodeLine ("\tjsr\tlswitch");
2403 void g_case (unsigned flags, unsigned label, unsigned long val)
2404 /* Create table code for one case selector */
2406 switch (flags & CF_TYPE) {
2410 AddCodeLine ("\t.word\t$%04X, L%04X", val & 0xFFFF, label & 0xFFFF);
2414 AddCodeLine ("\t.dword\t$%08X", val);
2415 AddCodeLine ("\t.word\tL%04X", label & 0xFFFF);
2426 void g_truejump (unsigned flags, unsigned label)
2427 /* Jump to label if zero flag clear */
2429 if (flags & CF_SHORT) {
2430 AddCodeLine ("\tbne\tL%04X", label);
2432 AddCodeLine ("\tjne\tL%04X", label);
2438 void g_falsejump (unsigned flags, unsigned label)
2439 /* Jump to label if zero flag set */
2441 if (flags & CF_SHORT) {
2442 AddCodeLine ("\tbeq\tL%04X", label);
2444 AddCodeLine ("\tjeq\tL%04X", label);
2450 static void mod_internal (int k, char* verb1, char* verb2)
2453 AddCodeLine ("\tjsr\t%ssp%c", verb1, k + '0');
2457 AddCodeLine ("\tjsr\t%ssp", verb2);
2463 void g_space (int space)
2464 /* Create or drop space on the stack */
2467 mod_internal (-space, "inc", "addy");
2468 } else if (space > 0) {
2469 mod_internal (space, "dec", "suby");
2475 void g_add (unsigned flags, unsigned long val)
2476 /* Primary = TOS + Primary */
2478 static char* ops [12] = {
2479 0, "tosadda0", "tosaddax",
2480 0, "tosadda0", "tosaddax",
2485 if (flags & CF_CONST) {
2486 flags &= ~CF_FORCECHAR; // Handle chars as ints
2487 g_push (flags & ~CF_CONST, 0);
2489 oper (flags, val, ops);
2494 void g_sub (unsigned flags, unsigned long val)
2495 /* Primary = TOS - Primary */
2497 static char* ops [12] = {
2498 0, "tossuba0", "tossubax",
2499 0, "tossuba0", "tossubax",
2504 if (flags & CF_CONST) {
2505 flags &= ~CF_FORCECHAR; // Handle chars as ints
2506 g_push (flags & ~CF_CONST, 0);
2508 oper (flags, val, ops);
2513 void g_rsub (unsigned flags, unsigned long val)
2514 /* Primary = Primary - TOS */
2516 static char* ops [12] = {
2517 0, "tosrsuba0", "tosrsubax",
2518 0, "tosrsuba0", "tosrsubax",
2522 oper (flags, val, ops);
2527 void g_mul (unsigned flags, unsigned long val)
2528 /* Primary = TOS * Primary */
2530 static char* ops [12] = {
2531 0, "tosmula0", "tosmulax",
2532 0, "tosumula0", "tosumulax",
2539 /* Do strength reduction if the value is constant and a power of two */
2540 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2541 /* Generate a shift instead */
2546 /* If the right hand side is const, the lhs is not on stack but still
2547 * in the primary register.
2549 if (flags & CF_CONST) {
2551 switch (flags & CF_TYPE) {
2554 if (flags & CF_FORCECHAR) {
2555 /* Handle some special cases */
2559 AddCodeLine ("\tsta\ttmp1");
2560 AddCodeLine ("\tasl\ta");
2561 AddCodeLine ("\tclc");
2562 AddCodeLine ("\tadc\ttmp1");
2566 AddCodeLine ("\tsta\ttmp1");
2567 AddCodeLine ("\tasl\ta");
2568 AddCodeLine ("\tasl\ta");
2569 AddCodeLine ("\tclc");
2570 AddCodeLine ("\tadc\ttmp1");
2574 AddCodeLine ("\tsta\ttmp1");
2575 AddCodeLine ("\tasl\ta");
2576 AddCodeLine ("\tasl\ta");
2577 AddCodeLine ("\tclc");
2578 AddCodeLine ("\tadc\ttmp1");
2579 AddCodeLine ("\tasl\ta");
2595 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2596 * into the normal, non-optimized stuff.
2598 flags &= ~CF_FORCECHAR; // Handle chars as ints
2599 g_push (flags & ~CF_CONST, 0);
2603 /* Use long way over the stack */
2604 oper (flags, val, ops);
2609 void g_div (unsigned flags, unsigned long val)
2610 /* Primary = TOS / Primary */
2612 static char* ops [12] = {
2613 0, "tosdiva0", "tosdivax",
2614 0, "tosudiva0", "tosudivax",
2619 /* Do strength reduction if the value is constant and a power of two */
2621 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2622 /* Generate a shift instead */
2625 /* Generate a division */
2626 if (flags & CF_CONST) {
2627 /* lhs is not on stack */
2628 flags &= ~CF_FORCECHAR; // Handle chars as ints
2629 g_push (flags & ~CF_CONST, 0);
2631 oper (flags, val, ops);
2637 void g_mod (unsigned flags, unsigned long val)
2638 /* Primary = TOS % Primary */
2640 static char* ops [12] = {
2641 0, "tosmoda0", "tosmodax",
2642 0, "tosumoda0", "tosumodax",
2648 /* Check if we can do some cost reduction */
2649 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2650 /* We can do that with an AND operation */
2651 g_and (flags, val - 1);
2653 /* Do it the hard way... */
2654 if (flags & CF_CONST) {
2655 /* lhs is not on stack */
2656 flags &= ~CF_FORCECHAR; // Handle chars as ints
2657 g_push (flags & ~CF_CONST, 0);
2659 oper (flags, val, ops);
2665 void g_or (unsigned flags, unsigned long val)
2666 /* Primary = TOS | Primary */
2668 static char* ops [12] = {
2669 0, "tosora0", "tosorax",
2670 0, "tosora0", "tosorax",
2675 /* If the right hand side is const, the lhs is not on stack but still
2676 * in the primary register.
2678 if (flags & CF_CONST) {
2680 switch (flags & CF_TYPE) {
2683 if (flags & CF_FORCECHAR) {
2684 if ((val & 0xFF) != 0xFF) {
2685 AddCodeLine ("\tora\t#$%02X", val & 0xFF);
2693 AddCodeLine ("\tora\t#$%02X", val & 0xFF);
2700 AddCodeLine ("\tora\t#$%02X", val & 0xFF);
2709 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2710 * into the normal, non-optimized stuff.
2712 g_push (flags & ~CF_CONST, 0);
2716 /* Use long way over the stack */
2717 oper (flags, val, ops);
2722 void g_xor (unsigned flags, unsigned long val)
2723 /* Primary = TOS ^ Primary */
2725 static char* ops [12] = {
2726 0, "tosxora0", "tosxorax",
2727 0, "tosxora0", "tosxorax",
2733 /* If the right hand side is const, the lhs is not on stack but still
2734 * in the primary register.
2736 if (flags & CF_CONST) {
2738 switch (flags & CF_TYPE) {
2741 if (flags & CF_FORCECHAR) {
2742 if ((val & 0xFF) != 0) {
2743 AddCodeLine ("\teor\t#$%02X", val & 0xFF);
2752 AddCodeLine ("\teor\t#$%02X", val);
2755 } else if ((val & 0xFF) == 0) {
2756 AddCodeLine ("\tpha");
2757 AddCodeLine ("\ttxa");
2758 AddCodeLine ("\teor\t#$%02X", (val >> 8) & 0xFF);
2759 AddCodeLine ("\ttax");
2760 AddCodeLine ("\tpla");
2768 AddCodeLine ("\teor\t#$%02X", val & 0xFF);
2778 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2779 * into the normal, non-optimized stuff.
2781 g_push (flags & ~CF_CONST, 0);
2785 /* Use long way over the stack */
2786 oper (flags, val, ops);
2791 void g_and (unsigned flags, unsigned long val)
2792 /* Primary = TOS & Primary */
2794 static char* ops [12] = {
2795 0, "tosanda0", "tosandax",
2796 0, "tosanda0", "tosandax",
2801 /* If the right hand side is const, the lhs is not on stack but still
2802 * in the primary register.
2804 if (flags & CF_CONST) {
2806 switch (flags & CF_TYPE) {
2809 if (flags & CF_FORCECHAR) {
2810 AddCodeLine ("\tand\t#$%02X", val & 0xFF);
2815 if ((val & 0xFFFF) != 0xFFFF) {
2820 } else if (val != 0xFF) {
2821 AddCodeLine ("\tand\t#$%02X", val & 0xFF);
2823 } else if ((val & 0xFF00) == 0xFF00) {
2824 AddCodeLine ("\tand\t#$%02X", val & 0xFF);
2825 } else if ((val & 0x00FF) == 0x0000) {
2826 AddCodeLine ("\ttxa");
2827 AddCodeLine ("\tand\t#$%02X", (val >> 8) & 0xFF);
2828 AddCodeLine ("\ttax");
2831 AddCodeLine ("\ttay");
2832 AddCodeLine ("\ttxa");
2833 AddCodeLine ("\tand\t#$%02X", (val >> 8) & 0xFF);
2834 AddCodeLine ("\ttax");
2835 AddCodeLine ("\ttya");
2836 if ((val & 0x00FF) != 0x00FF) {
2837 AddCodeLine ("\tand\t#$%02X", val & 0xFF);
2846 AddCodeLine ("\tstx\tsreg+1");
2847 AddCodeLine ("\tstx\tsreg");
2848 if ((val & 0xFF) != 0xFF) {
2849 AddCodeLine ("\tand\t#$%02X", val & 0xFF);
2852 } else if (val == 0xFF00) {
2854 AddCodeLine ("\tsta\tsreg+1");
2855 AddCodeLine ("\tsta\tsreg");
2864 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2865 * into the normal, non-optimized stuff.
2867 g_push (flags & ~CF_CONST, 0);
2871 /* Use long way over the stack */
2872 oper (flags, val, ops);
2877 void g_asr (unsigned flags, unsigned long val)
2878 /* Primary = TOS >> Primary */
2880 static char* ops [12] = {
2881 0, "tosasra0", "tosasrax",
2882 0, "tosshra0", "tosshrax",
2887 /* If the right hand side is const, the lhs is not on stack but still
2888 * in the primary register.
2890 if (flags & CF_CONST) {
2892 switch (flags & CF_TYPE) {
2896 if (val >= 1 && val <= 3) {
2897 if (flags & CF_UNSIGNED) {
2898 AddCodeLine ("\tjsr\tshrax%ld", val);
2900 AddCodeLine ("\tjsr\tasrax%ld", val);
2903 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2904 AddCodeLine ("\ttxa");
2911 if (val >= 1 && val <= 3) {
2912 if (flags & CF_UNSIGNED) {
2913 AddCodeLine ("\tjsr\tshreax%ld", val);
2915 AddCodeLine ("\tjsr\tasreax%ld", val);
2918 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2919 AddCodeLine ("\ttxa");
2920 AddCodeLine ("\tldx\tsreg");
2921 AddCodeLine ("\tldy\tsreg+1");
2922 AddCodeLine ("\tsty\tsreg");
2923 AddCodeLine ("\tldy\t#$00");
2924 AddCodeLine ("\tsty\tsreg+1");
2926 } else if (val == 16) {
2927 AddCodeLine ("\tldy\t#$00");
2928 AddCodeLine ("\tldx\tsreg+1");
2929 if ((flags & CF_UNSIGNED) == 0) {
2930 AddCodeLine ("\tbpl\t*+3");
2931 AddCodeLine ("\tdey");
2932 AddCodeHint ("y:!");
2934 AddCodeLine ("\tlda\tsreg");
2935 AddCodeLine ("\tsty\tsreg+1");
2936 AddCodeLine ("\tsty\tsreg");
2945 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2946 * into the normal, non-optimized stuff.
2948 g_push (flags & ~CF_CONST, 0);
2952 /* Use long way over the stack */
2953 oper (flags, val, ops);
2958 void g_asl (unsigned flags, unsigned long val)
2959 /* Primary = TOS << Primary */
2961 static char* ops [12] = {
2962 0, "tosasla0", "tosaslax",
2963 0, "tosshla0", "tosshlax",
2969 /* If the right hand side is const, the lhs is not on stack but still
2970 * in the primary register.
2972 if (flags & CF_CONST) {
2974 switch (flags & CF_TYPE) {
2978 if (val >= 1 && val <= 3) {
2979 if (flags & CF_UNSIGNED) {
2980 AddCodeLine ("\tjsr\tshlax%ld", val);
2982 AddCodeLine ("\tjsr\taslax%ld", val);
2985 } else if (val == 8) {
2986 AddCodeLine ("\ttax");
2987 AddCodeLine ("\tlda\t#$00");
2993 if (val >= 1 && val <= 3) {
2994 if (flags & CF_UNSIGNED) {
2995 AddCodeLine ("\tjsr\tshleax%ld", val);
2997 AddCodeLine ("\tjsr\tasleax%ld", val);
3000 } else if (val == 8) {
3001 AddCodeLine ("\tldy\tsreg");
3002 AddCodeLine ("\tsty\tsreg+1");
3003 AddCodeLine ("\tstx\tsreg");
3004 AddCodeLine ("\ttax");
3005 AddCodeLine ("\tlda\t#$00");
3007 } else if (val == 16) {
3008 AddCodeLine ("\tstx\tsreg+1");
3009 AddCodeLine ("\tsta\tsreg");
3010 AddCodeLine ("\tlda\t#$00");
3011 AddCodeLine ("\ttax");
3020 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3021 * into the normal, non-optimized stuff.
3023 g_push (flags & ~CF_CONST, 0);
3027 /* Use long way over the stack */
3028 oper (flags, val, ops);
3033 void g_neg (unsigned flags)
3034 /* Primary = -Primary */
3036 switch (flags & CF_TYPE) {
3040 AddCodeLine ("\tjsr\tnegax");
3044 AddCodeLine ("\tjsr\tnegeax");
3054 void g_bneg (unsigned flags)
3055 /* Primary = !Primary */
3057 switch (flags & CF_TYPE) {
3060 AddCodeLine ("\tjsr\tbnega");
3064 AddCodeLine ("\tjsr\tbnegax");
3068 AddCodeLine ("\tjsr\tbnegeax");
3078 void g_com (unsigned flags)
3079 /* Primary = ~Primary */
3081 switch (flags & CF_TYPE) {
3085 AddCodeLine ("\tjsr\tcomplax");
3089 AddCodeLine ("\tjsr\tcompleax");
3099 void g_inc (unsigned flags, unsigned long val)
3100 /* Increment the primary register by a given number */
3102 /* Don't inc by zero */
3107 /* Generate code for the supported types */
3109 switch (flags & CF_TYPE) {
3112 if (flags & CF_FORCECHAR) {
3113 if (CPU == CPU_65C02 && val <= 2) {
3115 AddCodeLine ("\tina");
3118 AddCodeLine ("\tclc");
3119 AddCodeLine ("\tadc\t#$%02X", val & 0xFF);
3126 if (CPU == CPU_65C02 && val == 1) {
3127 AddCodeLine ("\tina");
3128 AddCodeLine ("\tbne\t*+3");
3129 AddCodeLine ("\tinx");
3130 /* Tell the optimizer that the X register may be invalid */
3131 AddCodeHint ("x:!");
3132 } else if (FavourSize) {
3135 AddCodeLine ("\tjsr\tincax%u", val);
3136 } else if (val <= 255) {
3138 AddCodeLine ("\tjsr\tincaxy");
3140 g_add (flags | CF_CONST, val);
3143 /* Inline the code */
3145 if ((val & 0xFF) != 0) {
3146 AddCodeLine ("\tclc");
3147 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3148 AddCodeLine ("\tbcc\t*+3");
3149 AddCodeLine ("\tinx");
3150 /* Tell the optimizer that the X register may be invalid */
3151 AddCodeHint ("x:!");
3154 AddCodeLine ("\tinx");
3157 AddCodeLine ("\tinx");
3160 AddCodeLine ("\tclc");
3161 if ((val & 0xFF) != 0) {
3162 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3163 /* Tell the optimizer that the X register may be invalid */
3164 AddCodeHint ("x:!");
3166 AddCodeLine ("\tpha");
3167 AddCodeLine ("\ttxa");
3168 AddCodeLine ("\tadc\t#$%02X", (unsigned char) (val >> 8));
3169 AddCodeLine ("\ttax");
3170 AddCodeLine ("\tpla");
3178 AddCodeLine ("\tjsr\tinceaxy");
3180 g_add (flags | CF_CONST, val);
3192 void g_dec (unsigned flags, unsigned long val)
3193 /* Decrement the primary register by a given number */
3195 /* Generate code for the supported types */
3197 switch (flags & CF_TYPE) {
3200 if (flags & CF_FORCECHAR) {
3201 if (CPU == CPU_65C02 && val <= 2) {
3203 AddCodeLine ("\tdea");
3206 AddCodeLine ("\tsec");
3207 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
3215 AddCodeLine ("\tjsr\tdecax%d", (int) val);
3216 } else if (val <= 255) {
3218 AddCodeLine ("\tjsr\tdecaxy");
3220 g_sub (flags | CF_CONST, val);
3227 AddCodeLine ("\tjsr\tdeceaxy");
3229 g_sub (flags | CF_CONST, val);
3242 * Following are the conditional operators. They compare the TOS against
3243 * the primary and put a literal 1 in the primary if the condition is
3244 * true, otherwise they clear the primary register
3249 void g_eq (unsigned flags, unsigned long val)
3250 /* Test for equal */
3252 static char* ops [12] = {
3253 "toseq00", "toseqa0", "toseqax",
3254 "toseq00", "toseqa0", "toseqax",
3259 /* If the right hand side is const, the lhs is not on stack but still
3260 * in the primary register.
3262 if (flags & CF_CONST) {
3264 switch (flags & CF_TYPE) {
3267 if (flags & CF_FORCECHAR) {
3268 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3269 AddCodeLine ("\tjsr\tbooleq");
3275 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
3276 AddCodeLine ("\tbne\t*+4");
3277 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3278 AddCodeLine ("\tjsr\tbooleq");
3288 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3289 * into the normal, non-optimized stuff.
3291 g_push (flags & ~CF_CONST, 0);
3295 /* Use long way over the stack */
3296 oper (flags, val, ops);
3301 void g_ne (unsigned flags, unsigned long val)
3302 /* Test for not equal */
3304 static char* ops [12] = {
3305 "tosne00", "tosnea0", "tosneax",
3306 "tosne00", "tosnea0", "tosneax",
3312 /* If the right hand side is const, the lhs is not on stack but still
3313 * in the primary register.
3315 if (flags & CF_CONST) {
3317 switch (flags & CF_TYPE) {
3320 if (flags & CF_FORCECHAR) {
3321 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3322 AddCodeLine ("\tjsr\tboolne");
3328 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
3329 AddCodeLine ("\tbne\t*+4");
3330 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3331 AddCodeLine ("\tjsr\tboolne");
3341 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3342 * into the normal, non-optimized stuff.
3344 g_push (flags & ~CF_CONST, 0);
3348 /* Use long way over the stack */
3349 oper (flags, val, ops);
3354 void g_lt (unsigned flags, unsigned long val)
3355 /* Test for less than */
3357 static char* ops [12] = {
3358 "toslt00", "toslta0", "tosltax",
3359 "tosult00", "tosulta0", "tosultax",
3364 /* If the right hand side is const, the lhs is not on stack but still
3365 * in the primary register.
3367 if (flags & CF_CONST) {
3369 /* Give a warning in some special cases */
3370 if ((flags & CF_UNSIGNED) && val == 0) {
3371 Warning (WARN_COND_NEVER_TRUE);
3374 /* Look at the type */
3375 switch (flags & CF_TYPE) {
3378 if (flags & CF_FORCECHAR) {
3379 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3380 if (flags & CF_UNSIGNED) {
3381 AddCodeLine ("\tjsr\tboolult");
3383 AddCodeLine ("\tjsr\tboollt");
3390 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3391 /* If we have a signed compare against zero, we only need to
3392 * test the high byte.
3394 AddCodeLine ("\ttxa");
3395 AddCodeLine ("\tjsr\tboollt");
3398 /* Direct code only for unsigned data types */
3399 if (flags & CF_UNSIGNED) {
3400 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
3401 AddCodeLine ("\tbne\t*+4");
3402 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3403 AddCodeLine ("\tjsr\tboolult");
3415 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3416 * into the normal, non-optimized stuff.
3418 g_push (flags & ~CF_CONST, 0);
3422 /* Use long way over the stack */
3423 oper (flags, val, ops);
3428 void g_le (unsigned flags, unsigned long val)
3429 /* Test for less than or equal to */
3431 static char* ops [12] = {
3432 "tosle00", "toslea0", "tosleax",
3433 "tosule00", "tosulea0", "tosuleax",
3439 /* If the right hand side is const, the lhs is not on stack but still
3440 * in the primary register.
3442 if (flags & CF_CONST) {
3444 /* Look at the type */
3445 switch (flags & CF_TYPE) {
3448 if (flags & CF_FORCECHAR) {
3449 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3450 if (flags & CF_UNSIGNED) {
3451 AddCodeLine ("\tjsr\tboolule");
3453 AddCodeLine ("\tjsr\tboolle");
3460 if (flags & CF_UNSIGNED) {
3461 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
3462 AddCodeLine ("\tbne\t*+4");
3463 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3464 AddCodeLine ("\tjsr\tboolule");
3476 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3477 * into the normal, non-optimized stuff.
3479 g_push (flags & ~CF_CONST, 0);
3483 /* Use long way over the stack */
3484 oper (flags, val, ops);
3489 void g_gt (unsigned flags, unsigned long val)
3490 /* Test for greater than */
3492 static char* ops [12] = {
3493 "tosgt00", "tosgta0", "tosgtax",
3494 "tosugt00", "tosugta0", "tosugtax",
3500 /* If the right hand side is const, the lhs is not on stack but still
3501 * in the primary register.
3503 if (flags & CF_CONST) {
3505 /* Look at the type */
3506 switch (flags & CF_TYPE) {
3509 if (flags & CF_FORCECHAR) {
3510 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3511 if (flags & CF_UNSIGNED) {
3512 /* If we have a compare > 0, we will replace it by
3513 * != 0 here, since both are identical but the latter
3514 * is easier to optimize.
3517 AddCodeLine ("\tjsr\tboolugt");
3519 AddCodeLine ("\tjsr\tboolne");
3522 AddCodeLine ("\tjsr\tboolgt");
3529 if (flags & CF_UNSIGNED) {
3530 /* If we have a compare > 0, we will replace it by
3531 * != 0 here, since both are identical but the latter
3532 * is easier to optimize.
3534 if ((val & 0xFFFF) == 0) {
3535 AddCodeLine ("\tstx\ttmp1");
3536 AddCodeLine ("\tora\ttmp1");
3537 AddCodeLine ("\tjsr\tboolne");
3539 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
3540 AddCodeLine ("\tbne\t*+4");
3541 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3542 AddCodeLine ("\tjsr\tboolugt");
3555 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3556 * into the normal, non-optimized stuff.
3558 g_push (flags & ~CF_CONST, 0);
3562 /* Use long way over the stack */
3563 oper (flags, val, ops);
3568 void g_ge (unsigned flags, unsigned long val)
3569 /* Test for greater than or equal to */
3571 static char* ops [12] = {
3572 "tosge00", "tosgea0", "tosgeax",
3573 "tosuge00", "tosugea0", "tosugeax",
3579 /* If the right hand side is const, the lhs is not on stack but still
3580 * in the primary register.
3582 if (flags & CF_CONST) {
3584 /* Give a warning in some special cases */
3585 if ((flags & CF_UNSIGNED) && val == 0) {
3586 Warning (WARN_COND_ALWAYS_TRUE);
3589 /* Look at the type */
3590 switch (flags & CF_TYPE) {
3593 if (flags & CF_FORCECHAR) {
3594 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3595 if (flags & CF_UNSIGNED) {
3596 AddCodeLine ("\tjsr\tbooluge");
3598 AddCodeLine ("\tjsr\tboolge");
3605 if (flags & CF_UNSIGNED) {
3606 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
3607 AddCodeLine ("\tbne\t*+4");
3608 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3609 AddCodeLine ("\tjsr\tbooluge");
3621 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3622 * into the normal, non-optimized stuff.
3624 g_push (flags & ~CF_CONST, 0);
3628 /* Use long way over the stack */
3629 oper (flags, val, ops);
3634 /*****************************************************************************/
3635 /* Allocating static storage */
3636 /*****************************************************************************/
3640 void g_res (unsigned n)
3641 /* Reserve static storage, n bytes */
3643 AddCodeLine ("\t.res\t%u,$00", n);
3648 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3649 /* Define data with the size given in flags */
3651 if (flags & CF_CONST) {
3653 /* Numeric constant */
3654 switch (flags & CF_TYPE) {
3657 AddCodeLine ("\t.byte\t$%02lX", val & 0xFF);
3661 AddCodeLine ("\t.word\t$%04lX", val & 0xFFFF);
3665 AddCodeLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3676 /* Create the correct label name */
3677 const char* Label = GetLabelName (flags, val, offs);
3679 /* Labels are always 16 bit */
3680 AddCodeLine ("\t.word\t%s", Label);
3687 void g_defbytes (const unsigned char* Bytes, unsigned Count)
3688 /* Output a row of bytes as a constant */
3694 /* Output the stuff */
3697 /* How many go into this line? */
3698 if ((Chunk = Count) > 16) {
3703 /* Output one line */
3704 strcpy (Buf, "\t.byte\t");
3707 B += sprintf (B, "$%02X", *Bytes++ & 0xFF);
3713 /* Output the line */
3720 void g_zerobytes (unsigned n)
3721 /* Output n bytes of data initialized with zero */
3723 AddCodeLine ("\t.res\t%u,$00", n);
3728 /*****************************************************************************/
3729 /* Inlined known functions */
3730 /*****************************************************************************/
3734 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3735 /* Inline the strlen() function */
3737 /* We need a label in both cases */
3738 unsigned label = GetLabel ();
3740 /* Two different encodings */
3741 if (flags & CF_CONST) {
3743 /* The address of the string is constant. Create the correct label name */
3744 char* lbuf = GetLabelName (flags, val, offs);
3746 /* Generate the strlen code */
3747 AddCodeLine ("\tldy\t#$FF");
3748 g_defloclabel (label);
3749 AddCodeLine ("\tiny");
3750 AddCodeLine ("\tlda\t%s,y", lbuf);
3751 AddCodeLine ("\tbne\tL%04X", label);
3752 AddCodeLine ("\ttax");
3753 AddCodeLine ("\ttya");
3757 /* Address not constant but in primary */
3759 /* This is too much code, so call strlen instead of inlining */
3760 AddCodeLine ("\tjsr\t_strlen");
3762 /* Inline the function */
3763 AddCodeLine ("\tsta\tptr1");
3764 AddCodeLine ("\tstx\tptr1+1");
3765 AddCodeLine ("\tldy\t#$FF");
3766 g_defloclabel (label);
3767 AddCodeLine ("\tiny");
3768 AddCodeLine ("\tlda\t(ptr1),y");
3769 AddCodeLine ("\tbne\tL%04X", label);
3770 AddCodeLine ("\ttax");
3771 AddCodeLine ("\ttya");