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 cannot currently handle more than byte sized offsets */
1048 if (offs > 256 - sizeofarg (flags)) {
1049 Internal ("g_putind: Large offsets not implemented");
1052 /* Check the size and determine operation */
1053 switch (flags & CF_TYPE) {
1058 AddCodeLine ("\tjsr\tstaspidx");
1060 AddCodeLine ("\tjsr\tstaspp");
1067 AddCodeLine ("\tjsr\tstaxspidx");
1069 AddCodeLine ("\tjsr\tstaxspp");
1076 AddCodeLine ("\tjsr\tsteaxspidx");
1078 AddCodeLine ("\tjsr\tsteaxspp");
1087 /* Pop the argument which is always a pointer */
1093 /*****************************************************************************/
1094 /* type conversion and similiar stuff */
1095 /*****************************************************************************/
1099 void g_toslong (unsigned flags)
1100 /* Make sure, the value on TOS is a long. Convert if necessary */
1102 switch (flags & CF_TYPE) {
1106 if (flags & CF_UNSIGNED) {
1107 AddCodeLine ("\tjsr\ttosulong");
1109 AddCodeLine ("\tjsr\ttoslong");
1124 void g_tosint (unsigned flags)
1125 /* Make sure, the value on TOS is an int. Convert if necessary */
1127 switch (flags & CF_TYPE) {
1134 AddCodeLine ("\tjsr\ttosint");
1145 void g_reglong (unsigned flags)
1146 /* Make sure, the value in the primary register a long. Convert if necessary */
1148 switch (flags & CF_TYPE) {
1152 if (flags & CF_UNSIGNED) {
1154 AddCodeLine ("\tjsr\taxulong");
1157 AddCodeLine ("\tsty\tsreg");
1158 AddCodeLine ("\tsty\tsreg+1");
1161 AddCodeLine ("\tjsr\taxlong");
1175 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1176 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1177 * value, that corresponds to the value on TOS, rhs corresponds to the value
1178 * in (e)ax. The return value is the the flags value for the resulting type.
1181 unsigned ltype, rtype;
1184 /* Get the type spec from the flags */
1185 ltype = lhs & CF_TYPE;
1186 rtype = rhs & CF_TYPE;
1188 /* Check if a conversion is needed */
1189 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1190 /* We must promote the primary register to long */
1192 /* Get the new rhs type */
1193 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1195 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1196 /* We must promote the lhs to long */
1202 /* Get the new rhs type */
1203 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1207 /* Determine the result type for the operation:
1208 * - The result is const if both operands are const.
1209 * - The result is unsigned if one of the operands is unsigned.
1210 * - The result is long if one of the operands is long.
1211 * - Otherwise the result is int sized.
1213 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1214 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1215 if (rtype == CF_LONG || ltype == CF_LONG) {
1225 unsigned g_typecast (unsigned lhs, unsigned rhs)
1226 /* Cast the value in the primary register to the operand size that is flagged
1227 * by the lhs value. Return the result value.
1230 unsigned ltype, rtype;
1232 /* Get the type spec from the flags */
1233 ltype = lhs & CF_TYPE;
1234 rtype = rhs & CF_TYPE;
1236 /* Check if a conversion is needed */
1237 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1238 /* We must promote the primary register to long */
1242 /* Do not need any other action. If the left type is int, and the primary
1243 * register is long, it will be automagically truncated. If the right hand
1244 * side is const, it is not located in the primary register and handled by
1245 * the expression parser code.
1248 /* Result is const if the right hand side was const */
1249 lhs |= (rhs & CF_CONST);
1251 /* The resulting type is that of the left hand side (that's why you called
1259 void g_scale (unsigned flags, long val)
1260 /* Scale the value in the primary register by the given value. If val is positive,
1261 * scale up, is val is negative, scale down. This function is used to scale
1262 * the operands or results of pointer arithmetic by the size of the type, the
1263 * pointer points to.
1268 /* Value may not be zero */
1270 Internal ("Data type has no size");
1271 } else if (val > 0) {
1274 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1276 /* Factor is 2, 4 or 8, use special function */
1277 switch (flags & CF_TYPE) {
1280 if (flags & CF_FORCECHAR) {
1282 AddCodeLine ("\tasl\ta");
1289 if (FavourSize || p2 >= 3) {
1290 if (flags & CF_UNSIGNED) {
1291 AddCodeLine ("\tjsr\tshlax%d", p2);
1293 AddCodeLine ("\tjsr\taslax%d", p2);
1296 AddCodeLine ("\tstx\ttmp1");
1298 AddCodeLine ("\tasl\ta");
1299 AddCodeLine ("\trol\ttmp1");
1301 AddCodeLine ("\tldx\ttmp1");
1306 if (flags & CF_UNSIGNED) {
1307 AddCodeLine ("\tjsr\tshleax%d", p2);
1309 AddCodeLine ("\tjsr\tasleax%d", p2);
1318 } else if (val != 1) {
1320 /* Use a multiplication instead */
1321 g_mul (flags | CF_CONST, val);
1329 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1331 /* Factor is 2, 4 or 8, use special function */
1332 switch (flags & CF_TYPE) {
1335 if (flags & CF_FORCECHAR) {
1336 if (flags & CF_UNSIGNED) {
1338 AddCodeLine ("\tlsr\ta");
1341 } else if (p2 <= 2) {
1342 AddCodeLine ("\tcmp\t#$80");
1343 AddCodeLine ("\tror\ta");
1350 if (flags & CF_UNSIGNED) {
1351 if (FavourSize || p2 >= 3) {
1352 AddCodeLine ("\tjsr\tlsrax%d", p2);
1354 AddCodeLine ("\tstx\ttmp1");
1356 AddCodeLine ("\tlsr\ttmp1");
1357 AddCodeLine ("\tror\ta");
1359 AddCodeLine ("\tldx\ttmp1");
1362 if (FavourSize || p2 >= 3) {
1363 AddCodeLine ("\tjsr\tasrax%d", p2);
1365 AddCodeLine ("\tstx\ttmp1");
1367 AddCodeLine ("\tcpx\t#$80");
1368 AddCodeLine ("\tror\ttmp1");
1369 AddCodeLine ("\tror\ta");
1371 AddCodeLine ("\tldx\ttmp1");
1377 if (flags & CF_UNSIGNED) {
1378 AddCodeLine ("\tjsr\tlsreax%d", p2);
1380 AddCodeLine ("\tjsr\tasreax%d", p2);
1389 } else if (val != 1) {
1391 /* Use a division instead */
1392 g_div (flags | CF_CONST, val);
1400 /*****************************************************************************/
1401 /* Adds and subs of variables fix a fixed address */
1402 /*****************************************************************************/
1406 void g_addlocal (unsigned flags, int offs)
1407 /* Add a local variable to ax */
1409 /* Correct the offset and check it */
1411 CheckLocalOffs (offs);
1413 switch (flags & CF_TYPE) {
1416 AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1417 AddCodeLine ("\tclc");
1418 AddCodeLine ("\tadc\t(sp),y");
1419 AddCodeLine ("\tbcc\t*+3");
1420 AddCodeLine ("\tinx");
1421 AddCodeHint ("x:!");
1425 AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1426 AddCodeLine ("\tclc");
1427 AddCodeLine ("\tadc\t(sp),y");
1428 AddCodeLine ("\tpha");
1429 AddCodeLine ("\ttxa");
1430 AddCodeLine ("\tiny");
1431 AddCodeLine ("\tadc\t(sp),y");
1432 AddCodeLine ("\ttax");
1433 AddCodeLine ("\tpla");
1437 /* Do it the old way */
1439 g_getlocal (flags, offs);
1451 void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
1452 /* Add a static variable to ax */
1454 /* Create the correct label name */
1455 char* lbuf = GetLabelName (flags, label, offs);
1457 switch (flags & CF_TYPE) {
1460 AddCodeLine ("\tclc");
1461 AddCodeLine ("\tadc\t%s", lbuf);
1462 AddCodeLine ("\tbcc\t*+3");
1463 AddCodeLine ("\tinx");
1464 AddCodeHint ("x:!");
1468 AddCodeLine ("\tclc");
1469 AddCodeLine ("\tadc\t%s", lbuf);
1470 AddCodeLine ("\ttay");
1471 AddCodeLine ("\ttxa");
1472 AddCodeLine ("\tadc\t%s+1", lbuf);
1473 AddCodeLine ("\ttax");
1474 AddCodeLine ("\ttya");
1478 /* Do it the old way */
1480 g_getstatic (flags, label, offs);
1492 /*****************************************************************************/
1493 /* Compares of ax with a variable with fixed address */
1494 /*****************************************************************************/
1498 void g_cmplocal (unsigned flags, int offs)
1499 /* Compare a local variable to ax */
1501 Internal ("g_cmplocal not implemented");
1506 void g_cmpstatic (unsigned flags, unsigned label, unsigned offs)
1507 /* Compare a static variable to ax */
1509 Internal ("g_cmpstatic not implemented");
1514 /*****************************************************************************/
1515 /* Special op= functions */
1516 /*****************************************************************************/
1520 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
1522 /* Emit += for a static variable */
1524 /* Create the correct label name */
1525 char* lbuf = GetLabelName (flags, label, offs);
1527 /* Check the size and determine operation */
1528 switch (flags & CF_TYPE) {
1531 if (flags & CF_FORCECHAR) {
1532 AddCodeLine ("\tldx\t#$00");
1533 if (flags & CF_CONST) {
1535 AddCodeLine ("\tinc\t%s", lbuf);
1536 AddCodeLine ("\tlda\t%s", lbuf);
1538 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1539 AddCodeLine ("\tclc");
1540 AddCodeLine ("\tadc\t%s", lbuf);
1541 AddCodeLine ("\tsta\t%s", lbuf);
1544 AddCodeLine ("\tclc");
1545 AddCodeLine ("\tadc\t%s", lbuf);
1546 AddCodeLine ("\tsta\t%s", lbuf);
1548 if ((flags & CF_UNSIGNED) == 0) {
1549 AddCodeLine ("\tbpl\t*+3");
1550 AddCodeLine ("\tdex");
1551 AddCodeHint ("x:!"); /* Invalidate X */
1558 if (flags & CF_CONST) {
1560 label = GetLabel ();
1561 AddCodeLine ("\tinc\t%s", lbuf);
1562 AddCodeLine ("\tbne\tL%04X", (int)label);
1563 AddCodeLine ("\tinc\t%s+1", lbuf);
1564 g_defloclabel (label);
1565 AddCodeLine ("\tlda\t%s", lbuf); /* Hmmm... */
1566 AddCodeLine ("\tldx\t%s+1", lbuf);
1568 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1569 AddCodeLine ("\tclc");
1570 AddCodeLine ("\tadc\t%s", lbuf);
1571 AddCodeLine ("\tsta\t%s", lbuf);
1573 label = GetLabel ();
1574 AddCodeLine ("\tbcc\tL%04X", (int)label);
1575 AddCodeLine ("\tinc\t%s+1", lbuf);
1576 g_defloclabel (label);
1577 AddCodeLine ("\tldx\t%s+1", lbuf);
1579 AddCodeLine ("\tlda\t#$%02X", (unsigned char)(val >> 8));
1580 AddCodeLine ("\tadc\t%s+1", lbuf);
1581 AddCodeLine ("\tsta\t%s+1", lbuf);
1582 AddCodeLine ("\ttax");
1583 AddCodeLine ("\tlda\t%s", lbuf);
1587 AddCodeLine ("\tclc");
1588 AddCodeLine ("\tadc\t%s", lbuf);
1589 AddCodeLine ("\tsta\t%s", lbuf);
1590 AddCodeLine ("\ttxa");
1591 AddCodeLine ("\tadc\t%s+1", lbuf);
1592 AddCodeLine ("\tsta\t%s+1", lbuf);
1593 AddCodeLine ("\ttax");
1594 AddCodeLine ("\tlda\t%s", lbuf);
1599 if (flags & CF_CONST) {
1601 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1602 AddCodeLine ("\tsty\tptr1");
1603 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1605 AddCodeLine ("\tjsr\tladdeq1");
1607 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1608 AddCodeLine ("\tjsr\tladdeqa");
1611 g_getstatic (flags, label, offs);
1613 g_putstatic (flags, label, offs);
1616 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1617 AddCodeLine ("\tsty\tptr1");
1618 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1619 AddCodeLine ("\tjsr\tladdeq");
1630 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1631 /* Emit += for a local variable */
1633 /* Calculate the true offset, check it, load it into Y */
1635 CheckLocalOffs (offs);
1637 /* Check the size and determine operation */
1638 switch (flags & CF_TYPE) {
1641 if (flags & CF_FORCECHAR) {
1643 AddCodeLine ("\tldx\t#$00");
1644 if (flags & CF_CONST) {
1645 AddCodeLine ("\tclc");
1646 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1647 AddCodeLine ("\tadc\t(sp,x)");
1648 AddCodeLine ("\tsta\t(sp,x)");
1650 AddCodeLine ("\tclc");
1651 AddCodeLine ("\tadc\t(sp,x)");
1652 AddCodeLine ("\tsta\t(sp,x)");
1656 AddCodeLine ("\tldx\t#$00");
1657 if (flags & CF_CONST) {
1658 AddCodeLine ("\tclc");
1659 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1660 AddCodeLine ("\tadc\t(sp),y");
1661 AddCodeLine ("\tsta\t(sp),y");
1663 AddCodeLine ("\tclc");
1664 AddCodeLine ("\tadc\t(sp),y");
1665 AddCodeLine ("\tsta\t(sp),y");
1668 if ((flags & CF_UNSIGNED) == 0) {
1669 AddCodeLine ("\tbpl\t*+3");
1670 AddCodeLine ("\tdex");
1671 AddCodeHint ("x:!"); /* Invalidate X */
1678 if (flags & CF_CONST) {
1679 g_getimmed (flags, val, 0);
1682 AddCodeLine ("\tjsr\taddeq0sp");
1685 AddCodeLine ("\tjsr\taddeqysp");
1690 if (flags & CF_CONST) {
1691 g_getimmed (flags, val, 0);
1694 AddCodeLine ("\tjsr\tladdeq0sp");
1697 AddCodeLine ("\tjsr\tladdeqysp");
1708 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1709 /* Emit += for the location with address in ax */
1711 /* If the offset is too large for a byte register, add the high byte
1712 * of the offset to the primary. Beware: We need a special correction
1713 * if the offset in the low byte will overflow in the operation.
1715 offs = MakeByteOffs (flags, offs);
1717 /* Check the size and determine operation */
1718 switch (flags & CF_TYPE) {
1721 AddCodeLine ("\tsta\tptr1");
1722 AddCodeLine ("\tstx\tptr1+1");
1724 AddCodeLine ("\tldx\t#$00");
1725 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1726 AddCodeLine ("\tclc");
1727 AddCodeLine ("\tadc\t(ptr1,x)");
1728 AddCodeLine ("\tsta\t(ptr1,x)");
1730 AddCodeLine ("\tldy\t#$%02X", offs);
1731 AddCodeLine ("\tldx\t#$00");
1732 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1733 AddCodeLine ("\tclc");
1734 AddCodeLine ("\tadc\t(ptr1),y");
1735 AddCodeLine ("\tsta\t(ptr1),y");
1741 /* Lots of code, use only if size is not important */
1742 AddCodeLine ("\tsta\tptr1");
1743 AddCodeLine ("\tstx\tptr1+1");
1744 AddCodeLine ("\tldy\t#$%02X", offs);
1745 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1746 AddCodeLine ("\tclc");
1747 AddCodeLine ("\tadc\t(ptr1),y");
1748 AddCodeLine ("\tsta\t(ptr1),y");
1749 AddCodeLine ("\tpha");
1750 AddCodeLine ("\tiny");
1751 AddCodeLine ("\tlda\t#$%02X", (unsigned char)(val >> 8));
1752 AddCodeLine ("\tadc\t(ptr1),y");
1753 AddCodeLine ("\tsta\t(ptr1),y");
1754 AddCodeLine ("\ttax");
1755 AddCodeLine ("\tpla");
1761 AddCodeLine ("\tjsr\tpushax"); /* Push the address */
1762 push (flags); /* Correct the internal sp */
1763 g_getind (flags, offs); /* Fetch the value */
1764 g_inc (flags, val); /* Increment value in primary */
1765 g_putind (flags, offs); /* Store the value back */
1775 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1777 /* Emit -= for a static variable */
1779 /* Create the correct label name */
1780 char* lbuf = GetLabelName (flags, label, offs);
1782 /* Check the size and determine operation */
1783 switch (flags & CF_TYPE) {
1786 if (flags & CF_FORCECHAR) {
1787 AddCodeLine ("\tldx\t#$00");
1788 if (flags & CF_CONST) {
1790 AddCodeLine ("\tdec\t%s", lbuf);
1791 AddCodeLine ("\tlda\t%s", lbuf);
1793 AddCodeLine ("\tsec");
1794 AddCodeLine ("\tlda\t%s", lbuf);
1795 AddCodeLine ("\tsbc\t#$%02X", (int)(val & 0xFF));
1796 AddCodeLine ("\tsta\t%s", lbuf);
1799 AddCodeLine ("\tsec");
1800 AddCodeLine ("\tsta\ttmp1");
1801 AddCodeLine ("\tlda\t%s", lbuf);
1802 AddCodeLine ("\tsbc\ttmp1");
1803 AddCodeLine ("\tsta\t%s", lbuf);
1805 if ((flags & CF_UNSIGNED) == 0) {
1806 AddCodeLine ("\tbpl\t*+3");
1807 AddCodeLine ("\tdex");
1808 AddCodeHint ("x:!"); /* Invalidate X */
1815 AddCodeLine ("\tsec");
1816 if (flags & CF_CONST) {
1817 AddCodeLine ("\tlda\t%s", lbuf);
1818 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
1819 AddCodeLine ("\tsta\t%s", lbuf);
1821 label = GetLabel ();
1822 AddCodeLine ("\tbcs\tL%04X", (unsigned)label);
1823 AddCodeLine ("\tdec\t%s+1", lbuf);
1824 g_defloclabel (label);
1825 AddCodeLine ("\tldx\t%s+1", lbuf);
1827 AddCodeLine ("\tlda\t%s+1", lbuf);
1828 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)(val >> 8));
1829 AddCodeLine ("\tsta\t%s+1", lbuf);
1830 AddCodeLine ("\ttax");
1831 AddCodeLine ("\tlda\t%s", lbuf);
1834 AddCodeLine ("\tsta\ttmp1");
1835 AddCodeLine ("\tlda\t%s", lbuf);
1836 AddCodeLine ("\tsbc\ttmp1");
1837 AddCodeLine ("\tsta\t%s", lbuf);
1838 AddCodeLine ("\tstx\ttmp1");
1839 AddCodeLine ("\tlda\t%s+1", lbuf);
1840 AddCodeLine ("\tsbc\ttmp1");
1841 AddCodeLine ("\tsta\t%s+1", lbuf);
1842 AddCodeLine ("\ttax");
1843 AddCodeLine ("\tlda\t%s", lbuf);
1848 if (flags & CF_CONST) {
1850 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1851 AddCodeLine ("\tsty\tptr1");
1852 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1854 AddCodeLine ("\tjsr\tlsubeq1");
1856 AddCodeLine ("\tlda\t#$%02X", (unsigned char)val);
1857 AddCodeLine ("\tjsr\tlsubeqa");
1860 g_getstatic (flags, label, offs);
1862 g_putstatic (flags, label, offs);
1865 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1866 AddCodeLine ("\tsty\tptr1");
1867 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1868 AddCodeLine ("\tjsr\tlsubeq");
1879 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1880 /* Emit -= for a local variable */
1882 /* Calculate the true offset, check it, load it into Y */
1884 CheckLocalOffs (offs);
1886 /* Check the size and determine operation */
1887 switch (flags & CF_TYPE) {
1890 if (flags & CF_FORCECHAR) {
1892 AddCodeLine ("\tldx\t#$00");
1893 AddCodeLine ("\tsec");
1894 if (flags & CF_CONST) {
1895 AddCodeLine ("\tlda\t(sp),y");
1896 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
1898 AddCodeLine ("\tsta\ttmp1");
1899 AddCodeLine ("\tlda\t(sp),y");
1900 AddCodeLine ("\tsbc\ttmp1");
1902 AddCodeLine ("\tsta\t(sp),y");
1903 if ((flags & CF_UNSIGNED) == 0) {
1904 AddCodeLine ("\tbpl\t*+3");
1905 AddCodeLine ("\tdex");
1906 AddCodeHint ("x:!"); /* Invalidate X */
1913 if (flags & CF_CONST) {
1914 g_getimmed (flags, val, 0);
1917 AddCodeLine ("\tjsr\tsubeq0sp");
1920 AddCodeLine ("\tjsr\tsubeqysp");
1925 if (flags & CF_CONST) {
1926 g_getimmed (flags, val, 0);
1929 AddCodeLine ("\tjsr\tlsubeq0sp");
1932 AddCodeLine ("\tjsr\tlsubeqysp");
1943 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
1944 /* Emit -= for the location with address in ax */
1946 /* If the offset is too large for a byte register, add the high byte
1947 * of the offset to the primary. Beware: We need a special correction
1948 * if the offset in the low byte will overflow in the operation.
1950 offs = MakeByteOffs (flags, offs);
1952 /* Check the size and determine operation */
1953 switch (flags & CF_TYPE) {
1956 AddCodeLine ("\tsta\tptr1");
1957 AddCodeLine ("\tstx\tptr1+1");
1959 AddCodeLine ("\tldx\t#$00");
1960 AddCodeLine ("\tlda\t(ptr1,x)");
1961 AddCodeLine ("\tsec");
1962 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
1963 AddCodeLine ("\tsta\t(ptr1,x)");
1965 AddCodeLine ("\tldy\t#$%02X", offs);
1966 AddCodeLine ("\tldx\t#$00");
1967 AddCodeLine ("\tlda\t(ptr1),y");
1968 AddCodeLine ("\tsec");
1969 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
1970 AddCodeLine ("\tsta\t(ptr1),y");
1976 /* Lots of code, use only if size is not important */
1977 AddCodeLine ("\tsta\tptr1");
1978 AddCodeLine ("\tstx\tptr1+1");
1979 AddCodeLine ("\tldy\t#$%02X", offs);
1980 AddCodeLine ("\tlda\t(ptr1),y");
1981 AddCodeLine ("\tsec");
1982 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
1983 AddCodeLine ("\tsta\t(ptr1),y");
1984 AddCodeLine ("\tpha");
1985 AddCodeLine ("\tiny");
1986 AddCodeLine ("\tlda\t(ptr1),y");
1987 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)(val >> 8));
1988 AddCodeLine ("\tsta\t(ptr1),y");
1989 AddCodeLine ("\ttax");
1990 AddCodeLine ("\tpla");
1996 AddCodeLine ("\tjsr\tpushax"); /* Push the address */
1997 push (flags); /* Correct the internal sp */
1998 g_getind (flags, offs); /* Fetch the value */
1999 g_dec (flags, val); /* Increment value in primary */
2000 g_putind (flags, offs); /* Store the value back */
2010 /*****************************************************************************/
2011 /* Add a variable address to the value in ax */
2012 /*****************************************************************************/
2016 void g_addaddr_local (unsigned flags, int offs)
2017 /* Add the address of a local variable to ax */
2019 /* Add the offset */
2022 /* We cannot address more then 256 bytes of locals anyway */
2023 CheckLocalOffs (offs);
2024 AddCodeLine ("\tclc");
2025 AddCodeLine ("\tadc\t#$%02X", offs & 0xFF);
2026 AddCodeLine ("\tbcc\t*+4"); /* Do also skip the CLC insn below */
2027 AddCodeLine ("\tinx");
2028 AddCodeHint ("x:!"); /* Invalidate X */
2031 /* Add the current stackpointer value */
2032 AddCodeLine ("\tclc");
2033 AddCodeLine ("\tadc\tsp");
2034 AddCodeLine ("\ttay");
2035 AddCodeLine ("\ttxa");
2036 AddCodeLine ("\tadc\tsp+1");
2037 AddCodeLine ("\ttax");
2038 AddCodeLine ("\ttya");
2043 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2044 /* Add the address of a static variable to ax */
2046 /* Create the correct label name */
2047 char* lbuf = GetLabelName (flags, label, offs);
2049 /* Add the address to the current ax value */
2050 AddCodeLine ("\tclc");
2051 AddCodeLine ("\tadc\t#<(%s)", lbuf);
2052 AddCodeLine ("\ttay");
2053 AddCodeLine ("\ttxa");
2054 AddCodeLine ("\tadc\t#>(%s)", lbuf);
2055 AddCodeLine ("\ttax");
2056 AddCodeLine ("\ttya");
2061 /*****************************************************************************/
2063 /*****************************************************************************/
2067 void g_save (unsigned flags)
2068 /* Copy primary register to hold register. */
2070 /* Check the size and determine operation */
2071 switch (flags & CF_TYPE) {
2074 if (flags & CF_FORCECHAR) {
2075 AddCodeLine ("\tpha");
2081 AddCodeLine ("\tsta\tregsave");
2082 AddCodeLine ("\tstx\tregsave+1");
2086 AddCodeLine ("\tjsr\tsaveeax");
2096 void g_restore (unsigned flags)
2097 /* Copy hold register to P. */
2099 /* Check the size and determine operation */
2100 switch (flags & CF_TYPE) {
2103 if (flags & CF_FORCECHAR) {
2104 AddCodeLine ("\tpla");
2110 AddCodeLine ("\tlda\tregsave");
2111 AddCodeLine ("\tldx\tregsave+1");
2115 AddCodeLine ("\tjsr\tresteax");
2125 void g_cmp (unsigned flags, unsigned long val)
2126 /* Immidiate compare. The primary register will not be changed, Z flag
2130 /* Check the size and determine operation */
2131 switch (flags & CF_TYPE) {
2134 if (flags & CF_FORCECHAR) {
2135 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
2141 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
2142 AddCodeLine ("\tbne\t*+4");
2143 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
2147 Internal ("g_cmp: Long compares not implemented");
2157 static void oper (unsigned flags, unsigned long val, char** subs)
2158 /* Encode a binary operation. subs is a pointer to four groups of three
2160 * 0-2 --> Operate on ints
2161 * 3-5 --> Operate on unsigneds
2162 * 6-8 --> Operate on longs
2163 * 9-11 --> Operate on unsigned longs
2165 * The first subroutine names in each string group is used to encode an
2166 * operation with a zero constant, the second to encode an operation with
2167 * a 8 bit constant, and the third is used in all other cases.
2172 /* Determine the offset into the array */
2173 offs = (flags & CF_UNSIGNED)? 3 : 0;
2174 switch (flags & CF_TYPE) {
2187 /* Encode the operation */
2188 if (flags & CF_CONST) {
2189 /* Constant value given */
2190 if (val == 0 && subs [offs+0]) {
2191 /* Special case: constant with value zero */
2192 AddCodeLine ("\tjsr\t%s", subs [offs+0]);
2193 } else if (val < 0x100 && subs [offs+1]) {
2194 /* Special case: constant with high byte zero */
2195 ldaconst (val); /* Load low byte */
2196 AddCodeLine ("\tjsr\t%s", subs [offs+1]);
2198 /* Others: arbitrary constant value */
2199 g_getimmed (flags, val, 0); /* Load value */
2200 AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2203 /* Value not constant (is already in (e)ax) */
2204 AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2207 /* The operation will pop it's argument */
2213 void g_test (unsigned flags)
2214 /* Force a test to set cond codes right */
2216 switch (flags & CF_TYPE) {
2219 if (flags & CF_FORCECHAR) {
2220 AddCodeLine ("\ttax");
2226 AddCodeLine ("\tstx\ttmp1");
2227 AddCodeLine ("\tora\ttmp1");
2231 if (flags & CF_UNSIGNED) {
2232 AddCodeLine ("\tjsr\tutsteax");
2234 AddCodeLine ("\tjsr\ttsteax");
2246 void g_push (unsigned flags, unsigned long val)
2247 /* Push the primary register or a constant value onto the stack */
2251 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2253 /* We have a constant 8 or 16 bit value */
2254 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2256 /* Handle as 8 bit value */
2257 if (FavourSize && val <= 2) {
2258 AddCodeLine ("\tjsr\tpushc%d", (int) val);
2261 AddCodeLine ("\tjsr\tpusha");
2266 /* Handle as 16 bit value */
2267 hi = (unsigned char) (val >> 8);
2269 AddCodeLine ("\tjsr\tpush%u", (unsigned) val);
2270 } else if (hi == 0 || hi == 0xFF) {
2271 /* Use special function */
2273 AddCodeLine ("\tjsr\t%s", (hi == 0)? "pusha0" : "pushaFF");
2276 g_getimmed (flags, val, 0);
2277 AddCodeLine ("\tjsr\tpushax");
2283 /* Value is not 16 bit or not constant */
2284 if (flags & CF_CONST) {
2285 /* Constant 32 bit value, load into eax */
2286 g_getimmed (flags, val, 0);
2289 /* Push the primary register */
2290 switch (flags & CF_TYPE) {
2293 if (flags & CF_FORCECHAR) {
2294 /* Handle as char */
2295 AddCodeLine ("\tjsr\tpusha");
2300 AddCodeLine ("\tjsr\tpushax");
2304 AddCodeLine ("\tjsr\tpusheax");
2314 /* Adjust the stack offset */
2320 void g_swap (unsigned flags)
2321 /* Swap the primary register and the top of the stack. flags give the type
2322 * of *both* values (must have same size).
2325 switch (flags & CF_TYPE) {
2329 AddCodeLine ("\tjsr\tswapstk");
2333 AddCodeLine ("\tjsr\tswapestk");
2344 void g_call (unsigned flags, char* lbl, unsigned argsize)
2345 /* Call the specified subroutine name */
2347 if ((flags & CF_FIXARGC) == 0) {
2348 /* Pass arg count */
2351 AddCodeLine ("\tjsr\t_%s", lbl);
2352 oursp += argsize; /* callee pops args */
2357 void g_callind (unsigned flags, unsigned argsize)
2358 /* Call subroutine with address in AX */
2360 if ((flags & CF_FIXARGC) == 0) {
2361 /* Pass arg count */
2364 AddCodeLine ("\tjsr\tcallax"); /* do the call */
2365 oursp += argsize; /* callee pops args */
2370 void g_jump (unsigned label)
2371 /* Jump to specified internal label number */
2373 AddCodeLine ("\tjmp\tL%04X", label);
2378 void g_switch (unsigned flags)
2379 /* Output switch statement preample */
2381 switch (flags & CF_TYPE) {
2385 AddCodeLine ("\tjsr\tswitch");
2389 AddCodeLine ("\tjsr\tlswitch");
2400 void g_case (unsigned flags, unsigned label, unsigned long val)
2401 /* Create table code for one case selector */
2403 switch (flags & CF_TYPE) {
2407 AddCodeLine ("\t.word\t$%04X, L%04X",
2408 (unsigned)(val & 0xFFFF),
2409 (unsigned)(label & 0xFFFF));
2413 AddCodeLine ("\t.dword\t$%08lX", val);
2414 AddCodeLine ("\t.word\tL%04X", label & 0xFFFF);
2425 void g_truejump (unsigned flags, unsigned label)
2426 /* Jump to label if zero flag clear */
2428 if (flags & CF_SHORT) {
2429 AddCodeLine ("\tbne\tL%04X", label);
2431 AddCodeLine ("\tjne\tL%04X", label);
2437 void g_falsejump (unsigned flags, unsigned label)
2438 /* Jump to label if zero flag set */
2440 if (flags & CF_SHORT) {
2441 AddCodeLine ("\tbeq\tL%04X", label);
2443 AddCodeLine ("\tjeq\tL%04X", label);
2449 static void mod_internal (int k, char* verb1, char* verb2)
2452 AddCodeLine ("\tjsr\t%ssp%c", verb1, k + '0');
2456 AddCodeLine ("\tjsr\t%ssp", verb2);
2462 void g_space (int space)
2463 /* Create or drop space on the stack */
2466 mod_internal (-space, "inc", "addy");
2467 } else if (space > 0) {
2468 mod_internal (space, "dec", "suby");
2474 void g_add (unsigned flags, unsigned long val)
2475 /* Primary = TOS + Primary */
2477 static char* ops [12] = {
2478 0, "tosadda0", "tosaddax",
2479 0, "tosadda0", "tosaddax",
2484 if (flags & CF_CONST) {
2485 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2486 g_push (flags & ~CF_CONST, 0);
2488 oper (flags, val, ops);
2493 void g_sub (unsigned flags, unsigned long val)
2494 /* Primary = TOS - Primary */
2496 static char* ops [12] = {
2497 0, "tossuba0", "tossubax",
2498 0, "tossuba0", "tossubax",
2503 if (flags & CF_CONST) {
2504 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2505 g_push (flags & ~CF_CONST, 0);
2507 oper (flags, val, ops);
2512 void g_rsub (unsigned flags, unsigned long val)
2513 /* Primary = Primary - TOS */
2515 static char* ops [12] = {
2516 0, "tosrsuba0", "tosrsubax",
2517 0, "tosrsuba0", "tosrsubax",
2521 oper (flags, val, ops);
2526 void g_mul (unsigned flags, unsigned long val)
2527 /* Primary = TOS * Primary */
2529 static char* ops [12] = {
2530 0, "tosmula0", "tosmulax",
2531 0, "tosumula0", "tosumulax",
2538 /* Do strength reduction if the value is constant and a power of two */
2539 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2540 /* Generate a shift instead */
2545 /* If the right hand side is const, the lhs is not on stack but still
2546 * in the primary register.
2548 if (flags & CF_CONST) {
2550 switch (flags & CF_TYPE) {
2553 if (flags & CF_FORCECHAR) {
2554 /* Handle some special cases */
2558 AddCodeLine ("\tsta\ttmp1");
2559 AddCodeLine ("\tasl\ta");
2560 AddCodeLine ("\tclc");
2561 AddCodeLine ("\tadc\ttmp1");
2565 AddCodeLine ("\tsta\ttmp1");
2566 AddCodeLine ("\tasl\ta");
2567 AddCodeLine ("\tasl\ta");
2568 AddCodeLine ("\tclc");
2569 AddCodeLine ("\tadc\ttmp1");
2573 AddCodeLine ("\tsta\ttmp1");
2574 AddCodeLine ("\tasl\ta");
2575 AddCodeLine ("\tasl\ta");
2576 AddCodeLine ("\tclc");
2577 AddCodeLine ("\tadc\ttmp1");
2578 AddCodeLine ("\tasl\ta");
2594 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2595 * into the normal, non-optimized stuff.
2597 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2598 g_push (flags & ~CF_CONST, 0);
2602 /* Use long way over the stack */
2603 oper (flags, val, ops);
2608 void g_div (unsigned flags, unsigned long val)
2609 /* Primary = TOS / Primary */
2611 static char* ops [12] = {
2612 0, "tosdiva0", "tosdivax",
2613 0, "tosudiva0", "tosudivax",
2618 /* Do strength reduction if the value is constant and a power of two */
2620 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2621 /* Generate a shift instead */
2624 /* Generate a division */
2625 if (flags & CF_CONST) {
2626 /* lhs is not on stack */
2627 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2628 g_push (flags & ~CF_CONST, 0);
2630 oper (flags, val, ops);
2636 void g_mod (unsigned flags, unsigned long val)
2637 /* Primary = TOS % Primary */
2639 static char* ops [12] = {
2640 0, "tosmoda0", "tosmodax",
2641 0, "tosumoda0", "tosumodax",
2647 /* Check if we can do some cost reduction */
2648 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2649 /* We can do that with an AND operation */
2650 g_and (flags, val - 1);
2652 /* Do it the hard way... */
2653 if (flags & CF_CONST) {
2654 /* lhs is not on stack */
2655 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2656 g_push (flags & ~CF_CONST, 0);
2658 oper (flags, val, ops);
2664 void g_or (unsigned flags, unsigned long val)
2665 /* Primary = TOS | Primary */
2667 static char* ops [12] = {
2668 0, "tosora0", "tosorax",
2669 0, "tosora0", "tosorax",
2674 /* If the right hand side is const, the lhs is not on stack but still
2675 * in the primary register.
2677 if (flags & CF_CONST) {
2679 switch (flags & CF_TYPE) {
2682 if (flags & CF_FORCECHAR) {
2683 if ((val & 0xFF) != 0xFF) {
2684 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2692 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2699 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2708 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2709 * into the normal, non-optimized stuff.
2711 g_push (flags & ~CF_CONST, 0);
2715 /* Use long way over the stack */
2716 oper (flags, val, ops);
2721 void g_xor (unsigned flags, unsigned long val)
2722 /* Primary = TOS ^ Primary */
2724 static char* ops [12] = {
2725 0, "tosxora0", "tosxorax",
2726 0, "tosxora0", "tosxorax",
2732 /* If the right hand side is const, the lhs is not on stack but still
2733 * in the primary register.
2735 if (flags & CF_CONST) {
2737 switch (flags & CF_TYPE) {
2740 if (flags & CF_FORCECHAR) {
2741 if ((val & 0xFF) != 0) {
2742 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2751 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2754 } else if ((val & 0xFF) == 0) {
2755 AddCodeLine ("\tpha");
2756 AddCodeLine ("\ttxa");
2757 AddCodeLine ("\teor\t#$%02X", (unsigned char)(val >> 8));
2758 AddCodeLine ("\ttax");
2759 AddCodeLine ("\tpla");
2767 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2777 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2778 * into the normal, non-optimized stuff.
2780 g_push (flags & ~CF_CONST, 0);
2784 /* Use long way over the stack */
2785 oper (flags, val, ops);
2790 void g_and (unsigned flags, unsigned long val)
2791 /* Primary = TOS & Primary */
2793 static char* ops [12] = {
2794 0, "tosanda0", "tosandax",
2795 0, "tosanda0", "tosandax",
2800 /* If the right hand side is const, the lhs is not on stack but still
2801 * in the primary register.
2803 if (flags & CF_CONST) {
2805 switch (flags & CF_TYPE) {
2808 if (flags & CF_FORCECHAR) {
2809 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2814 if ((val & 0xFFFF) != 0xFFFF) {
2819 } else if (val != 0xFF) {
2820 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2822 } else if ((val & 0xFF00) == 0xFF00) {
2823 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2824 } else if ((val & 0x00FF) == 0x0000) {
2825 AddCodeLine ("\ttxa");
2826 AddCodeLine ("\tand\t#$%02X", (unsigned char)(val >> 8));
2827 AddCodeLine ("\ttax");
2830 AddCodeLine ("\ttay");
2831 AddCodeLine ("\ttxa");
2832 AddCodeLine ("\tand\t#$%02X", (unsigned char)(val >> 8));
2833 AddCodeLine ("\ttax");
2834 AddCodeLine ("\ttya");
2835 if ((val & 0x00FF) != 0x00FF) {
2836 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2845 AddCodeLine ("\tstx\tsreg+1");
2846 AddCodeLine ("\tstx\tsreg");
2847 if ((val & 0xFF) != 0xFF) {
2848 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2851 } else if (val == 0xFF00) {
2853 AddCodeLine ("\tsta\tsreg+1");
2854 AddCodeLine ("\tsta\tsreg");
2863 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2864 * into the normal, non-optimized stuff.
2866 g_push (flags & ~CF_CONST, 0);
2870 /* Use long way over the stack */
2871 oper (flags, val, ops);
2876 void g_asr (unsigned flags, unsigned long val)
2877 /* Primary = TOS >> Primary */
2879 static char* ops [12] = {
2880 0, "tosasra0", "tosasrax",
2881 0, "tosshra0", "tosshrax",
2886 /* If the right hand side is const, the lhs is not on stack but still
2887 * in the primary register.
2889 if (flags & CF_CONST) {
2891 switch (flags & CF_TYPE) {
2895 if (val >= 1 && val <= 3) {
2896 if (flags & CF_UNSIGNED) {
2897 AddCodeLine ("\tjsr\tshrax%ld", val);
2899 AddCodeLine ("\tjsr\tasrax%ld", val);
2902 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2903 AddCodeLine ("\ttxa");
2910 if (val >= 1 && val <= 3) {
2911 if (flags & CF_UNSIGNED) {
2912 AddCodeLine ("\tjsr\tshreax%ld", val);
2914 AddCodeLine ("\tjsr\tasreax%ld", val);
2917 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2918 AddCodeLine ("\ttxa");
2919 AddCodeLine ("\tldx\tsreg");
2920 AddCodeLine ("\tldy\tsreg+1");
2921 AddCodeLine ("\tsty\tsreg");
2922 AddCodeLine ("\tldy\t#$00");
2923 AddCodeLine ("\tsty\tsreg+1");
2925 } else if (val == 16) {
2926 AddCodeLine ("\tldy\t#$00");
2927 AddCodeLine ("\tldx\tsreg+1");
2928 if ((flags & CF_UNSIGNED) == 0) {
2929 AddCodeLine ("\tbpl\t*+3");
2930 AddCodeLine ("\tdey");
2931 AddCodeHint ("y:!");
2933 AddCodeLine ("\tlda\tsreg");
2934 AddCodeLine ("\tsty\tsreg+1");
2935 AddCodeLine ("\tsty\tsreg");
2944 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2945 * into the normal, non-optimized stuff.
2947 g_push (flags & ~CF_CONST, 0);
2951 /* Use long way over the stack */
2952 oper (flags, val, ops);
2957 void g_asl (unsigned flags, unsigned long val)
2958 /* Primary = TOS << Primary */
2960 static char* ops [12] = {
2961 0, "tosasla0", "tosaslax",
2962 0, "tosshla0", "tosshlax",
2968 /* If the right hand side is const, the lhs is not on stack but still
2969 * in the primary register.
2971 if (flags & CF_CONST) {
2973 switch (flags & CF_TYPE) {
2977 if (val >= 1 && val <= 3) {
2978 if (flags & CF_UNSIGNED) {
2979 AddCodeLine ("\tjsr\tshlax%ld", val);
2981 AddCodeLine ("\tjsr\taslax%ld", val);
2984 } else if (val == 8) {
2985 AddCodeLine ("\ttax");
2986 AddCodeLine ("\tlda\t#$00");
2992 if (val >= 1 && val <= 3) {
2993 if (flags & CF_UNSIGNED) {
2994 AddCodeLine ("\tjsr\tshleax%ld", val);
2996 AddCodeLine ("\tjsr\tasleax%ld", val);
2999 } else if (val == 8) {
3000 AddCodeLine ("\tldy\tsreg");
3001 AddCodeLine ("\tsty\tsreg+1");
3002 AddCodeLine ("\tstx\tsreg");
3003 AddCodeLine ("\ttax");
3004 AddCodeLine ("\tlda\t#$00");
3006 } else if (val == 16) {
3007 AddCodeLine ("\tstx\tsreg+1");
3008 AddCodeLine ("\tsta\tsreg");
3009 AddCodeLine ("\tlda\t#$00");
3010 AddCodeLine ("\ttax");
3019 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3020 * into the normal, non-optimized stuff.
3022 g_push (flags & ~CF_CONST, 0);
3026 /* Use long way over the stack */
3027 oper (flags, val, ops);
3032 void g_neg (unsigned flags)
3033 /* Primary = -Primary */
3035 switch (flags & CF_TYPE) {
3039 AddCodeLine ("\tjsr\tnegax");
3043 AddCodeLine ("\tjsr\tnegeax");
3053 void g_bneg (unsigned flags)
3054 /* Primary = !Primary */
3056 switch (flags & CF_TYPE) {
3059 AddCodeLine ("\tjsr\tbnega");
3063 AddCodeLine ("\tjsr\tbnegax");
3067 AddCodeLine ("\tjsr\tbnegeax");
3077 void g_com (unsigned flags)
3078 /* Primary = ~Primary */
3080 switch (flags & CF_TYPE) {
3084 AddCodeLine ("\tjsr\tcomplax");
3088 AddCodeLine ("\tjsr\tcompleax");
3098 void g_inc (unsigned flags, unsigned long val)
3099 /* Increment the primary register by a given number */
3101 /* Don't inc by zero */
3106 /* Generate code for the supported types */
3108 switch (flags & CF_TYPE) {
3111 if (flags & CF_FORCECHAR) {
3112 if (CPU == CPU_65C02 && val <= 2) {
3114 AddCodeLine ("\tina");
3117 AddCodeLine ("\tclc");
3118 AddCodeLine ("\tadc\t#$%02X", (unsigned char)val);
3125 if (CPU == CPU_65C02 && val == 1) {
3126 AddCodeLine ("\tina");
3127 AddCodeLine ("\tbne\t*+3");
3128 AddCodeLine ("\tinx");
3129 /* Tell the optimizer that the X register may be invalid */
3130 AddCodeHint ("x:!");
3131 } else if (FavourSize) {
3134 AddCodeLine ("\tjsr\tincax%lu", val);
3135 } else if (val <= 255) {
3137 AddCodeLine ("\tjsr\tincaxy");
3139 g_add (flags | CF_CONST, val);
3142 /* Inline the code */
3144 if ((val & 0xFF) != 0) {
3145 AddCodeLine ("\tclc");
3146 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3147 AddCodeLine ("\tbcc\t*+3");
3148 AddCodeLine ("\tinx");
3149 /* Tell the optimizer that the X register may be invalid */
3150 AddCodeHint ("x:!");
3153 AddCodeLine ("\tinx");
3156 AddCodeLine ("\tinx");
3159 AddCodeLine ("\tclc");
3160 if ((val & 0xFF) != 0) {
3161 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3162 /* Tell the optimizer that the X register may be invalid */
3163 AddCodeHint ("x:!");
3165 AddCodeLine ("\tpha");
3166 AddCodeLine ("\ttxa");
3167 AddCodeLine ("\tadc\t#$%02X", (unsigned char) (val >> 8));
3168 AddCodeLine ("\ttax");
3169 AddCodeLine ("\tpla");
3177 AddCodeLine ("\tjsr\tinceaxy");
3179 g_add (flags | CF_CONST, val);
3191 void g_dec (unsigned flags, unsigned long val)
3192 /* Decrement the primary register by a given number */
3194 /* Don't dec by zero */
3199 /* Generate code for the supported types */
3201 switch (flags & CF_TYPE) {
3204 if (flags & CF_FORCECHAR) {
3205 if (CPU == CPU_65C02 && val <= 2) {
3207 AddCodeLine ("\tdea");
3210 AddCodeLine ("\tsec");
3211 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
3219 AddCodeLine ("\tjsr\tdecax%d", (int) val);
3220 } else if (val <= 255) {
3222 AddCodeLine ("\tjsr\tdecaxy");
3224 g_sub (flags | CF_CONST, val);
3231 AddCodeLine ("\tjsr\tdeceaxy");
3233 g_sub (flags | CF_CONST, val);
3246 * Following are the conditional operators. They compare the TOS against
3247 * the primary and put a literal 1 in the primary if the condition is
3248 * true, otherwise they clear the primary register
3253 void g_eq (unsigned flags, unsigned long val)
3254 /* Test for equal */
3256 static char* ops [12] = {
3257 "toseq00", "toseqa0", "toseqax",
3258 "toseq00", "toseqa0", "toseqax",
3263 /* If the right hand side is const, the lhs is not on stack but still
3264 * in the primary register.
3266 if (flags & CF_CONST) {
3268 switch (flags & CF_TYPE) {
3271 if (flags & CF_FORCECHAR) {
3272 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3273 AddCodeLine ("\tjsr\tbooleq");
3279 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3280 AddCodeLine ("\tbne\t*+4");
3281 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3282 AddCodeLine ("\tjsr\tbooleq");
3292 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3293 * into the normal, non-optimized stuff.
3295 g_push (flags & ~CF_CONST, 0);
3299 /* Use long way over the stack */
3300 oper (flags, val, ops);
3305 void g_ne (unsigned flags, unsigned long val)
3306 /* Test for not equal */
3308 static char* ops [12] = {
3309 "tosne00", "tosnea0", "tosneax",
3310 "tosne00", "tosnea0", "tosneax",
3316 /* If the right hand side is const, the lhs is not on stack but still
3317 * in the primary register.
3319 if (flags & CF_CONST) {
3321 switch (flags & CF_TYPE) {
3324 if (flags & CF_FORCECHAR) {
3325 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3326 AddCodeLine ("\tjsr\tboolne");
3332 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3333 AddCodeLine ("\tbne\t*+4");
3334 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3335 AddCodeLine ("\tjsr\tboolne");
3345 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3346 * into the normal, non-optimized stuff.
3348 g_push (flags & ~CF_CONST, 0);
3352 /* Use long way over the stack */
3353 oper (flags, val, ops);
3358 void g_lt (unsigned flags, unsigned long val)
3359 /* Test for less than */
3361 static char* ops [12] = {
3362 "toslt00", "toslta0", "tosltax",
3363 "tosult00", "tosulta0", "tosultax",
3368 /* If the right hand side is const, the lhs is not on stack but still
3369 * in the primary register.
3371 if (flags & CF_CONST) {
3373 /* Give a warning in some special cases */
3374 if ((flags & CF_UNSIGNED) && val == 0) {
3375 Warning ("Condition is never true");
3378 /* Look at the type */
3379 switch (flags & CF_TYPE) {
3382 if (flags & CF_FORCECHAR) {
3383 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3384 if (flags & CF_UNSIGNED) {
3385 AddCodeLine ("\tjsr\tboolult");
3387 AddCodeLine ("\tjsr\tboollt");
3394 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3395 /* If we have a signed compare against zero, we only need to
3396 * test the high byte.
3398 AddCodeLine ("\ttxa");
3399 AddCodeLine ("\tjsr\tboollt");
3402 /* Direct code only for unsigned data types */
3403 if (flags & CF_UNSIGNED) {
3404 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3405 AddCodeLine ("\tbne\t*+4");
3406 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3407 AddCodeLine ("\tjsr\tboolult");
3419 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3420 * into the normal, non-optimized stuff.
3422 g_push (flags & ~CF_CONST, 0);
3426 /* Use long way over the stack */
3427 oper (flags, val, ops);
3432 void g_le (unsigned flags, unsigned long val)
3433 /* Test for less than or equal to */
3435 static char* ops [12] = {
3436 "tosle00", "toslea0", "tosleax",
3437 "tosule00", "tosulea0", "tosuleax",
3443 /* If the right hand side is const, the lhs is not on stack but still
3444 * in the primary register.
3446 if (flags & CF_CONST) {
3448 /* Look at the type */
3449 switch (flags & CF_TYPE) {
3452 if (flags & CF_FORCECHAR) {
3453 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3454 if (flags & CF_UNSIGNED) {
3455 AddCodeLine ("\tjsr\tboolule");
3457 AddCodeLine ("\tjsr\tboolle");
3464 if (flags & CF_UNSIGNED) {
3465 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3466 AddCodeLine ("\tbne\t*+4");
3467 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3468 AddCodeLine ("\tjsr\tboolule");
3480 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3481 * into the normal, non-optimized stuff.
3483 g_push (flags & ~CF_CONST, 0);
3487 /* Use long way over the stack */
3488 oper (flags, val, ops);
3493 void g_gt (unsigned flags, unsigned long val)
3494 /* Test for greater than */
3496 static char* ops [12] = {
3497 "tosgt00", "tosgta0", "tosgtax",
3498 "tosugt00", "tosugta0", "tosugtax",
3504 /* If the right hand side is const, the lhs is not on stack but still
3505 * in the primary register.
3507 if (flags & CF_CONST) {
3509 /* Look at the type */
3510 switch (flags & CF_TYPE) {
3513 if (flags & CF_FORCECHAR) {
3514 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3515 if (flags & CF_UNSIGNED) {
3516 /* If we have a compare > 0, we will replace it by
3517 * != 0 here, since both are identical but the latter
3518 * is easier to optimize.
3521 AddCodeLine ("\tjsr\tboolugt");
3523 AddCodeLine ("\tjsr\tboolne");
3526 AddCodeLine ("\tjsr\tboolgt");
3533 if (flags & CF_UNSIGNED) {
3534 /* If we have a compare > 0, we will replace it by
3535 * != 0 here, since both are identical but the latter
3536 * is easier to optimize.
3538 if ((val & 0xFFFF) == 0) {
3539 AddCodeLine ("\tstx\ttmp1");
3540 AddCodeLine ("\tora\ttmp1");
3541 AddCodeLine ("\tjsr\tboolne");
3543 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3544 AddCodeLine ("\tbne\t*+4");
3545 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3546 AddCodeLine ("\tjsr\tboolugt");
3559 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3560 * into the normal, non-optimized stuff.
3562 g_push (flags & ~CF_CONST, 0);
3566 /* Use long way over the stack */
3567 oper (flags, val, ops);
3572 void g_ge (unsigned flags, unsigned long val)
3573 /* Test for greater than or equal to */
3575 static char* ops [12] = {
3576 "tosge00", "tosgea0", "tosgeax",
3577 "tosuge00", "tosugea0", "tosugeax",
3583 /* If the right hand side is const, the lhs is not on stack but still
3584 * in the primary register.
3586 if (flags & CF_CONST) {
3588 /* Give a warning in some special cases */
3589 if ((flags & CF_UNSIGNED) && val == 0) {
3590 Warning ("Condition is always true");
3593 /* Look at the type */
3594 switch (flags & CF_TYPE) {
3597 if (flags & CF_FORCECHAR) {
3598 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3599 if (flags & CF_UNSIGNED) {
3600 AddCodeLine ("\tjsr\tbooluge");
3602 AddCodeLine ("\tjsr\tboolge");
3609 if (flags & CF_UNSIGNED) {
3610 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3611 AddCodeLine ("\tbne\t*+4");
3612 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3613 AddCodeLine ("\tjsr\tbooluge");
3625 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3626 * into the normal, non-optimized stuff.
3628 g_push (flags & ~CF_CONST, 0);
3632 /* Use long way over the stack */
3633 oper (flags, val, ops);
3638 /*****************************************************************************/
3639 /* Allocating static storage */
3640 /*****************************************************************************/
3644 void g_res (unsigned n)
3645 /* Reserve static storage, n bytes */
3647 AddCodeLine ("\t.res\t%u,$00", n);
3652 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3653 /* Define data with the size given in flags */
3655 if (flags & CF_CONST) {
3657 /* Numeric constant */
3658 switch (flags & CF_TYPE) {
3661 AddCodeLine ("\t.byte\t$%02lX", val & 0xFF);
3665 AddCodeLine ("\t.word\t$%04lX", val & 0xFFFF);
3669 AddCodeLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3680 /* Create the correct label name */
3681 const char* Label = GetLabelName (flags, val, offs);
3683 /* Labels are always 16 bit */
3684 AddCodeLine ("\t.word\t%s", Label);
3691 void g_defbytes (const unsigned char* Bytes, unsigned Count)
3692 /* Output a row of bytes as a constant */
3698 /* Output the stuff */
3701 /* How many go into this line? */
3702 if ((Chunk = Count) > 16) {
3707 /* Output one line */
3708 strcpy (Buf, "\t.byte\t");
3711 B += sprintf (B, "$%02X", *Bytes++ & 0xFF);
3717 /* Output the line */
3724 void g_zerobytes (unsigned n)
3725 /* Output n bytes of data initialized with zero */
3727 AddCodeLine ("\t.res\t%u,$00", n);
3732 /*****************************************************************************/
3733 /* Inlined known functions */
3734 /*****************************************************************************/
3738 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3739 /* Inline the strlen() function */
3741 /* We need a label in both cases */
3742 unsigned label = GetLabel ();
3744 /* Two different encodings */
3745 if (flags & CF_CONST) {
3747 /* The address of the string is constant. Create the correct label name */
3748 char* lbuf = GetLabelName (flags, val, offs);
3750 /* Generate the strlen code */
3751 AddCodeLine ("\tldy\t#$FF");
3752 g_defloclabel (label);
3753 AddCodeLine ("\tiny");
3754 AddCodeLine ("\tlda\t%s,y", lbuf);
3755 AddCodeLine ("\tbne\tL%04X", label);
3756 AddCodeLine ("\ttax");
3757 AddCodeLine ("\ttya");
3761 /* Address not constant but in primary */
3763 /* This is too much code, so call strlen instead of inlining */
3764 AddCodeLine ("\tjsr\t_strlen");
3766 /* Inline the function */
3767 AddCodeLine ("\tsta\tptr1");
3768 AddCodeLine ("\tstx\tptr1+1");
3769 AddCodeLine ("\tldy\t#$FF");
3770 g_defloclabel (label);
3771 AddCodeLine ("\tiny");
3772 AddCodeLine ("\tlda\t(ptr1),y");
3773 AddCodeLine ("\tbne\tL%04X", label);
3774 AddCodeLine ("\ttax");
3775 AddCodeLine ("\ttya");