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"
40 #include "../common/xmalloc.h"
55 /*****************************************************************************/
57 /*****************************************************************************/
61 /* Compiler relative stk ptr */
66 SEG_INV = -1, /* Invalid segment */
74 static char* SegmentNames [4];
75 static char* SegmentHints [4] = {
76 "seg:code", "seg:rodata", "seg:data", "seg:bss"
81 /*****************************************************************************/
83 /*****************************************************************************/
87 static void typeerror (unsigned type)
88 /* Print an error message about an invalid operand type */
90 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
95 static void CheckLocalOffs (unsigned Offs)
96 /* Check the offset into the stack for 8bit range */
99 /* Too many local vars */
100 AddCodeLine (";*** Too many locals");
101 Error (ERR_TOO_MANY_LOCALS);
107 static char* GetLabelName (unsigned flags, unsigned long label, unsigned offs)
109 static char lbuf [128]; /* Label name */
111 /* Create the correct label name */
112 switch (flags & CF_ADDRMASK) {
115 /* Static memory cell */
116 sprintf (lbuf, "L%04X+%u", (unsigned)(label & 0xFFFF), offs);
121 sprintf (lbuf, "_%s+%u", (char*) label, offs);
125 /* Absolute address */
126 sprintf (lbuf, "$%04X", (unsigned)((label+offs) & 0xFFFF));
130 /* Variable in register bank */
131 sprintf (lbuf, "regbank+%u", (unsigned)((label+offs) & 0xFFFF));
135 Internal ("Invalid address flags");
138 /* Return a pointer to the static buffer */
144 /*****************************************************************************/
145 /* Pre- and postamble */
146 /*****************************************************************************/
150 void g_preamble (void)
151 /* Generate the assembler code preamble */
153 AddCodeLine ("; File generated by cc65 v %u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
156 /* Insert some object file options */
157 AddCodeLine (".fopt\t\tcompiler,\"cc65 v %u.%u.%u\"", VER_MAJOR, VER_MINOR, VER_PATCH);
160 /* If we're producing code for some other CPU, switch the command set */
161 if (CPU == CPU_65C02) {
162 AddCodeLine (".pc02");
165 /* Allow auto import for runtime library routines */
166 AddCodeLine (".autoimport\ton");
168 /* Switch the assembler into case sensible mode */
169 AddCodeLine (".case\t\ton");
171 /* Tell the assembler if we want to generate debug info */
172 AddCodeLine (".debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
174 /* Import the stack pointer for direct auto variable access */
175 AddCodeLine (".importzp\tsp, sreg, regsave, regbank, tmp1, ptr1");
177 /* Define long branch macros */
178 AddCodeLine (".macpack\tlongbranch");
181 /* Define the ldax macro */
182 AddCodeLine (".macro ldax Value");
183 AddCodeLine (" lda #<(Value)");
184 AddCodeLine (" ldx #>(Value)");
185 AddCodeLine (".endmacro");
188 /* Define the default names for the segments */
189 SegmentNames [SEG_CODE] = xstrdup ("CODE");
190 SegmentNames [SEG_RODATA] = xstrdup ("RODATA");
191 SegmentNames [SEG_DATA] = xstrdup ("DATA");
192 SegmentNames [SEG_BSS] = xstrdup ("BSS");
194 /* Tell the optimizer that this is the end of the preamble */
195 AddCodeHint ("end_of_preamble");
200 void g_postamble (void)
201 /* Generate assembler code postamble */
203 /* Tell the optimizer that this is the start of the postamble */
204 AddCodeHint ("start_of_postamble");
209 /*****************************************************************************/
210 /* Segment support */
211 /*****************************************************************************/
215 static void UseSeg (int NewSeg)
216 /* Switch to a specific segment */
218 if (CurSeg != NewSeg) {
220 AddCodeLine (".segment\t\"%s\"", SegmentNames [CurSeg]);
221 AddCodeHint (SegmentHints [CurSeg]);
227 void g_usecode (void)
228 /* Switch to the code segment */
235 void g_userodata (void)
236 /* Switch to the read only data segment */
243 void g_usedata (void)
244 /* Switch to the data segment */
252 /* Switch to the bss segment */
259 static void SegName (int Seg, const char* Name)
260 /* Set the name of a segment */
262 /* Free the old name and set a new one */
263 xfree (SegmentNames [Seg]);
264 SegmentNames [Seg] = xstrdup (Name);
266 /* If the new segment is the current segment, emit a segment directive
270 CurSeg = SEG_INV; /* Invalidate */
277 void g_codename (const char* Name)
278 /* Set the name of the CODE segment */
280 SegName (SEG_CODE, Name);
285 void g_rodataname (const char* Name)
286 /* Set the name of the RODATA segment */
288 SegName (SEG_RODATA, Name);
293 void g_dataname (const char* Name)
294 /* Set the name of the DATA segment */
296 SegName (SEG_DATA, Name);
301 void g_bssname (const char* Name)
302 /* Set the name of the BSS segment */
304 SegName (SEG_BSS, Name);
309 /*****************************************************************************/
311 /*****************************************************************************/
315 unsigned sizeofarg (unsigned flags)
316 /* Return the size of a function argument type that is encoded in flags */
318 switch (flags & CF_TYPE) {
321 return (flags & CF_FORCECHAR)? 1 : 2;
338 int pop (unsigned flags)
339 /* Pop an argument of the given size */
341 return oursp += sizeofarg (flags);
346 int push (unsigned flags)
347 /* Push an argument of the given size */
349 return oursp -= sizeofarg (flags);
354 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
355 /* The value in Offs is an offset to an address in a/x. Make sure, an object
356 * of the type given in Flags can be loaded or stored into this address by
357 * adding part of the offset to the address in ax, so that the remaining
358 * offset fits into an index register. Return the remaining offset.
361 /* If the offset is too large for a byte register, add the high byte
362 * of the offset to the primary. Beware: We need a special correction
363 * if the offset in the low byte will overflow in the operation.
365 unsigned O = Offs & ~0xFFU;
366 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
367 /* We need to add the low byte also */
371 /* Do the correction if we need one */
373 g_inc (CF_INT | CF_CONST, O);
377 /* Return the new offset */
383 /*****************************************************************************/
384 /* Functions handling local labels */
385 /*****************************************************************************/
389 void g_defloclabel (unsigned label)
390 /* Define a local label */
392 AddCodeLine ("L%04X:", label & 0xFFFF);
397 /*****************************************************************************/
398 /* Functions handling global labels */
399 /*****************************************************************************/
403 void g_defgloblabel (const char* Name)
404 /* Define a global label with the given name */
406 AddCodeLine ("_%s:", Name);
411 void g_defexport (const char* Name, int ZP)
412 /* Export the given label */
415 AddCodeLine ("\t.exportzp\t_%s", Name);
417 AddCodeLine ("\t.export\t\t_%s", Name);
423 void g_defimport (const char* Name, int ZP)
424 /* Import the given label */
427 AddCodeLine ("\t.importzp\t_%s", Name);
429 AddCodeLine ("\t.import\t\t_%s", Name);
435 /*****************************************************************************/
436 /* Load functions for various registers */
437 /*****************************************************************************/
441 static void ldaconst (unsigned val)
442 /* Load a with a constant */
444 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
449 static void ldxconst (unsigned val)
450 /* Load x with a constant */
452 AddCodeLine ("\tldx\t#$%02X", val & 0xFF);
457 static void ldyconst (unsigned val)
458 /* Load y with a constant */
460 AddCodeLine ("\tldy\t#$%02X", val & 0xFF);
465 /*****************************************************************************/
466 /* Function entry and exit */
467 /*****************************************************************************/
471 /* Remember the argument size of a function. The variable is set by g_enter
472 * and used by g_leave. If the functions gets its argument size by the caller
473 * (variable param list or function without prototype), g_enter will set the
479 void g_enter (unsigned flags, unsigned argsize)
480 /* Function prologue */
482 if ((flags & CF_FIXARGC) != 0) {
483 /* Just remember the argument size for the leave */
487 AddCodeLine ("\tjsr\tenter");
493 void g_leave (int flags, int val)
494 /* Function epilogue */
499 /* How many bytes of locals do we have to drop? */
502 /* If we didn't have a variable argument list, don't call leave */
505 /* Load a function return code if needed */
506 if ((flags & CF_CONST) != 0) {
507 g_getimmed (flags, val, 0);
510 /* Drop stackframe or leave with rts */
513 AddCodeLine ("\trts");
515 AddCodeLine ("\tjmp\tincsp%d", k);
519 AddCodeLine ("\tjmp\taddysp");
524 strcpy (buf, "\tjmp\tleave");
526 /* We've a stack frame to drop */
530 if (flags & CF_CONST) {
531 if ((flags & CF_TYPE) != CF_LONG) {
532 /* Constant int sized value given for return code */
534 /* Special case: return 0 */
536 } else if (((val >> 8) & 0xFF) == 0) {
537 /* Special case: constant with high byte zero */
538 ldaconst (val); /* Load low byte */
541 /* Others: arbitrary constant value */
542 g_getimmed (flags, val, 0); /* Load value */
545 /* Constant long value: No shortcut possible */
546 g_getimmed (flags, val, 0);
550 /* Output the jump */
554 /* Add an empty line after a function to make the code more readable */
560 /*****************************************************************************/
561 /* Register variables */
562 /*****************************************************************************/
566 void g_save_regvars (int RegOffs, unsigned Bytes)
567 /* Save register variables */
569 /* Don't loop for up to two bytes */
572 AddCodeLine ("\tlda\tregbank%+d", RegOffs);
573 AddCodeLine ("\tjsr\tpusha");
575 } else if (Bytes == 2) {
577 AddCodeLine ("\tlda\tregbank%+d", RegOffs);
578 AddCodeLine ("\tldx\tregbank%+d", RegOffs+1);
579 AddCodeLine ("\tjsr\tpushax");
583 /* More than two bytes - loop */
584 unsigned Label = GetLabel ();
586 ldyconst (Bytes - 1);
588 g_defloclabel (Label);
589 AddCodeLine ("\tlda\tregbank%+d,x", RegOffs-1);
590 AddCodeLine ("\tsta\t(sp),y");
591 AddCodeLine ("\tdey");
592 AddCodeLine ("\tdex");
593 AddCodeLine ("\tbne\tL%04X", Label);
597 /* We pushed stuff, correct the stack pointer */
603 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
604 /* Restore register variables */
606 /* Calculate the actual stack offset and check it */
608 CheckLocalOffs (StackOffs);
610 /* Don't loop for up to two bytes */
613 ldyconst (StackOffs);
614 AddCodeLine ("\tlda\t(sp),y");
615 AddCodeLine ("\tsta\tregbank%+d", RegOffs);
617 } else if (Bytes == 2) {
619 ldyconst (StackOffs);
620 AddCodeLine ("\tlda\t(sp),y");
621 AddCodeLine ("\tsta\tregbank%+d", RegOffs);
622 AddCodeLine ("\tiny");
623 AddCodeLine ("\tlda\t(sp),y");
624 AddCodeLine ("\tsta\tregbank%+d", RegOffs+1);
628 /* More than two bytes - loop */
629 unsigned Label = GetLabel ();
630 ldyconst (StackOffs+Bytes-1);
632 g_defloclabel (Label);
633 AddCodeLine ("\tlda\t(sp),y");
634 AddCodeLine ("\tsta\tregbank%+d,x", RegOffs-1);
635 AddCodeLine ("\tdey");
636 AddCodeLine ("\tdex");
637 AddCodeLine ("\tbne\tL%04X", Label);
644 /*****************************************************************************/
645 /* Fetching memory cells */
646 /*****************************************************************************/
650 void g_getimmed (unsigned flags, unsigned long val, unsigned offs)
651 /* Load a constant into the primary register */
653 if ((flags & CF_CONST) != 0) {
655 /* Numeric constant */
656 switch (flags & CF_TYPE) {
659 if ((flags & CF_FORCECHAR) != 0) {
665 ldxconst ((val >> 8) & 0xFF);
666 ldaconst (val & 0xFF);
671 AddCodeLine ("\tldx\t#$00");
672 AddCodeLine ("\tstx\tsreg+1");
673 AddCodeLine ("\tstx\tsreg");
674 AddCodeLine ("\tlda\t#$%02X", (unsigned char) val);
675 } else if ((val & 0xFFFF00FF) == 0) {
676 AddCodeLine ("\tlda\t#$00");
677 AddCodeLine ("\tsta\tsreg+1");
678 AddCodeLine ("\tsta\tsreg");
679 AddCodeLine ("\tldx\t#$%02X", (unsigned char) (val >> 8));
680 } else if ((val & 0xFFFF0000) == 0 && FavourSize == 0) {
681 AddCodeLine ("\tlda\t#$00");
682 AddCodeLine ("\tsta\tsreg+1");
683 AddCodeLine ("\tsta\tsreg");
684 AddCodeLine ("\tlda\t#$%02X", (unsigned char) val);
685 AddCodeLine ("\tldx\t#$%02X", (unsigned char) (val >> 8));
686 } else if ((val & 0xFFFFFF00) == 0xFFFFFF00) {
687 AddCodeLine ("\tldx\t#$FF");
688 AddCodeLine ("\tstx\tsreg+1");
689 AddCodeLine ("\tstx\tsreg");
690 if ((val & 0xFF) == 0xFF) {
691 AddCodeLine ("\ttxa");
693 AddCodeLine ("\tlda\t#$%02X", (unsigned char) val);
695 } else if ((val & 0xFFFF00FF) == 0xFFFF00FF) {
696 AddCodeLine ("\tlda\t#$FF");
697 AddCodeLine ("\tsta\tsreg+1");
698 AddCodeLine ("\tsta\tsreg");
699 AddCodeLine ("\tldx\t#$%02X", (unsigned char) (val >> 8));
701 /* Call a subroutine that will load following value */
702 AddCodeLine ("\tjsr\tldeax");
703 AddCodeLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
715 /* Some sort of label */
716 const char* Label = GetLabelName (flags, val, offs);
718 /* Load the address into the primary */
719 AddCodeLine ("\tldax\t%s", Label);
726 void g_getstatic (unsigned flags, unsigned long label, unsigned offs)
727 /* Fetch an static memory cell into the primary register */
729 /* Create the correct label name */
730 char* lbuf = GetLabelName (flags, label, offs);
732 /* Check the size and generate the correct load operation */
733 switch (flags & CF_TYPE) {
736 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
737 AddCodeLine ("\tlda\t%s", lbuf); /* load A from the label */
740 AddCodeLine ("\tlda\t%s", lbuf); /* load A from the label */
741 if (!(flags & CF_UNSIGNED)) {
742 /* Must sign extend */
743 AddCodeLine ("\tbpl\t*+3");
744 AddCodeLine ("\tdex");
745 AddCodeHint ("x:!"); /* X is invalid now */
751 AddCodeLine ("\tlda\t%s", lbuf);
752 if (flags & CF_TEST) {
753 AddCodeLine ("\tora\t%s+1", lbuf);
755 AddCodeLine ("\tldx\t%s+1", lbuf);
760 if (flags & CF_TEST) {
761 AddCodeLine ("\tlda\t%s+3", lbuf);
762 AddCodeLine ("\tora\t%s+2", lbuf);
763 AddCodeLine ("\tora\t%s+1", lbuf);
764 AddCodeLine ("\tora\t%s+0", lbuf);
766 AddCodeLine ("\tlda\t%s+3", lbuf);
767 AddCodeLine ("\tsta\tsreg+1");
768 AddCodeLine ("\tlda\t%s+2", lbuf);
769 AddCodeLine ("\tsta\tsreg");
770 AddCodeLine ("\tldx\t%s+1", lbuf);
771 AddCodeLine ("\tlda\t%s", lbuf);
783 void g_getlocal (unsigned flags, int offs)
784 /* Fetch specified local object (local var). */
787 CheckLocalOffs (offs);
788 switch (flags & CF_TYPE) {
791 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
792 if (CPU == CPU_65C02 && offs == 0) {
793 AddCodeLine ("\tlda\t(sp)");
796 AddCodeLine ("\tlda\t(sp),y");
800 AddCodeLine ("\tldx\t#$00");
801 AddCodeLine ("\tlda\t(sp,x)");
804 AddCodeLine ("\tldx\t#$00");
805 AddCodeLine ("\tlda\t(sp),y");
807 if ((flags & CF_UNSIGNED) == 0) {
808 AddCodeLine ("\tbpl\t*+3");
809 AddCodeLine ("\tdex");
810 AddCodeHint ("x:!"); /* X is invalid now */
816 CheckLocalOffs (offs + 1);
817 if (flags & CF_TEST) {
819 AddCodeLine ("\tlda\t(sp),y");
820 AddCodeLine ("\tdey");
821 AddCodeLine ("\tora\t(sp),y");
826 AddCodeLine ("\tjsr\tldaxysp");
828 AddCodeLine ("\tjsr\tldax0sp");
832 AddCodeLine ("\tlda\t(sp),y");
833 AddCodeLine ("\ttax");
834 AddCodeLine ("\tdey");
835 AddCodeLine ("\tlda\t(sp),y");
843 AddCodeLine ("\tjsr\tldeaxysp");
845 AddCodeLine ("\tjsr\tldeax0sp");
856 void g_getind (unsigned flags, unsigned offs)
857 /* Fetch the specified object type indirect through the primary register
858 * into the primary register
861 /* If the offset is greater than 255, add the part that is > 255 to
862 * the primary. This way we get an easy addition and use the low byte
865 offs = MakeByteOffs (flags, offs);
867 /* Handle the indirect fetch */
868 switch (flags & CF_TYPE) {
871 /* Character sized */
874 if (flags & CF_UNSIGNED) {
875 AddCodeLine ("\tjsr\tldauidx");
877 AddCodeLine ("\tjsr\tldaidx");
880 if (flags & CF_UNSIGNED) {
882 AddCodeLine ("\tjsr\tldaui");
884 AddCodeLine ("\tsta\tptr1");
885 AddCodeLine ("\tstx\tptr1+1");
886 AddCodeLine ("\tldx\t#$00");
887 AddCodeLine ("\tlda\t(ptr1,x)");
890 AddCodeLine ("\tjsr\tldai");
896 if (flags & CF_TEST) {
898 AddCodeLine ("\tsta\tptr1");
899 AddCodeLine ("\tstx\tptr1+1");
900 AddCodeLine ("\tlda\t(ptr1),y");
901 AddCodeLine ("\tiny");
902 AddCodeLine ("\tora\t(ptr1),y");
905 AddCodeLine ("\tjsr\tldaxi");
908 AddCodeLine ("\tjsr\tldaxidx");
915 AddCodeLine ("\tjsr\tldeaxi");
918 AddCodeLine ("\tjsr\tldeaxidx");
920 if (flags & CF_TEST) {
921 AddCodeLine ("\tjsr\ttsteax");
933 void g_leasp (int offs)
934 /* Fetch the address of the specified symbol into the primary register */
936 /* Calculate the offset relative to sp */
939 /* For value 0 we do direct code */
941 AddCodeLine ("\tlda\tsp");
942 AddCodeLine ("\tldx\tsp+1");
945 ldaconst (offs); /* Load A with offset value */
946 AddCodeLine ("\tjsr\tleaasp"); /* Load effective address */
948 if (CPU == CPU_65C02 && offs == 1) {
949 AddCodeLine ("\tlda\tsp");
950 AddCodeLine ("\tldx\tsp+1");
951 AddCodeLine ("\tina");
952 AddCodeLine ("\tbne\t*+3");
953 AddCodeLine ("\tinx");
954 AddCodeHint ("x:!"); /* Invalidate X */
957 AddCodeLine ("\tclc");
958 AddCodeLine ("\tldx\tsp+1");
959 AddCodeLine ("\tadc\tsp");
960 AddCodeLine ("\tbcc\t*+3");
961 AddCodeLine ("\tinx");
962 AddCodeHint ("x:!"); /* Invalidate X */
970 /*****************************************************************************/
971 /* Store into memory */
972 /*****************************************************************************/
976 void g_putstatic (unsigned flags, unsigned long label, unsigned offs)
977 /* Store the primary register into the specified static memory cell */
979 /* Create the correct label name */
980 char* lbuf = GetLabelName (flags, label, offs);
982 /* Check the size and generate the correct store operation */
983 switch (flags & CF_TYPE) {
986 AddCodeLine ("\tsta\t%s", lbuf);
990 AddCodeLine ("\tsta\t%s", lbuf);
991 AddCodeLine ("\tstx\t%s+1", lbuf);
995 AddCodeLine ("\tsta\t%s", lbuf);
996 AddCodeLine ("\tstx\t%s+1", lbuf);
997 AddCodeLine ("\tldy\tsreg");
998 AddCodeLine ("\tsty\t%s+2", lbuf);
999 AddCodeLine ("\tldy\tsreg+1");
1000 AddCodeLine ("\tsty\t%s+3", lbuf);
1011 void g_putlocal (unsigned flags, int offs)
1012 /* Put data into local object. */
1015 CheckLocalOffs (offs);
1016 switch (flags & CF_TYPE) {
1019 if (CPU == CPU_65C02 && offs == 0) {
1020 AddCodeLine ("\tsta\t(sp)");
1023 AddCodeLine ("\tsta\t(sp),y");
1030 AddCodeLine ("\tjsr\tstaxysp");
1032 AddCodeLine ("\tjsr\tstax0sp");
1039 AddCodeLine ("\tjsr\tsteaxysp");
1041 AddCodeLine ("\tjsr\tsteax0sp");
1053 void g_putind (unsigned flags, unsigned offs)
1054 /* Store the specified object type in the primary register at the address
1055 * on the top of the stack
1058 /* We cannot currently handle more than byte sized offsets */
1059 if (offs > 256 - sizeofarg (flags)) {
1060 Internal ("g_putind: Large offsets not implemented");
1063 /* Check the size and determine operation */
1064 switch (flags & CF_TYPE) {
1069 AddCodeLine ("\tjsr\tstaspidx");
1071 AddCodeLine ("\tjsr\tstaspp");
1078 AddCodeLine ("\tjsr\tstaxspidx");
1080 AddCodeLine ("\tjsr\tstaxspp");
1087 AddCodeLine ("\tjsr\tsteaxspidx");
1089 AddCodeLine ("\tjsr\tsteaxspp");
1098 /* Pop the argument which is always a pointer */
1104 /*****************************************************************************/
1105 /* type conversion and similiar stuff */
1106 /*****************************************************************************/
1110 void g_toslong (unsigned flags)
1111 /* Make sure, the value on TOS is a long. Convert if necessary */
1113 switch (flags & CF_TYPE) {
1117 if (flags & CF_UNSIGNED) {
1118 AddCodeLine ("\tjsr\ttosulong");
1120 AddCodeLine ("\tjsr\ttoslong");
1135 void g_tosint (unsigned flags)
1136 /* Make sure, the value on TOS is an int. Convert if necessary */
1138 switch (flags & CF_TYPE) {
1145 AddCodeLine ("\tjsr\ttosint");
1156 void g_reglong (unsigned flags)
1157 /* Make sure, the value in the primary register a long. Convert if necessary */
1159 switch (flags & CF_TYPE) {
1163 if (flags & CF_UNSIGNED) {
1165 AddCodeLine ("\tjsr\taxulong");
1168 AddCodeLine ("\tsty\tsreg");
1169 AddCodeLine ("\tsty\tsreg+1");
1172 AddCodeLine ("\tjsr\taxlong");
1186 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1187 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1188 * value, that corresponds to the value on TOS, rhs corresponds to the value
1189 * in (e)ax. The return value is the the flags value for the resulting type.
1192 unsigned ltype, rtype;
1195 /* Get the type spec from the flags */
1196 ltype = lhs & CF_TYPE;
1197 rtype = rhs & CF_TYPE;
1199 /* Check if a conversion is needed */
1200 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1201 /* We must promote the primary register to long */
1203 /* Get the new rhs type */
1204 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1206 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1207 /* We must promote the lhs to long */
1213 /* Get the new rhs type */
1214 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1218 /* Determine the result type for the operation:
1219 * - The result is const if both operands are const.
1220 * - The result is unsigned if one of the operands is unsigned.
1221 * - The result is long if one of the operands is long.
1222 * - Otherwise the result is int sized.
1224 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1225 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1226 if (rtype == CF_LONG || ltype == CF_LONG) {
1236 unsigned g_typecast (unsigned lhs, unsigned rhs)
1237 /* Cast the value in the primary register to the operand size that is flagged
1238 * by the lhs value. Return the result value.
1241 unsigned ltype, rtype;
1243 /* Get the type spec from the flags */
1244 ltype = lhs & CF_TYPE;
1245 rtype = rhs & CF_TYPE;
1247 /* Check if a conversion is needed */
1248 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1249 /* We must promote the primary register to long */
1253 /* Do not need any other action. If the left type is int, and the primary
1254 * register is long, it will be automagically truncated. If the right hand
1255 * side is const, it is not located in the primary register and handled by
1256 * the expression parser code.
1259 /* Result is const if the right hand side was const */
1260 lhs |= (rhs & CF_CONST);
1262 /* The resulting type is that of the left hand side (that's why you called
1270 void g_scale (unsigned flags, long val)
1271 /* Scale the value in the primary register by the given value. If val is positive,
1272 * scale up, is val is negative, scale down. This function is used to scale
1273 * the operands or results of pointer arithmetic by the size of the type, the
1274 * pointer points to.
1279 /* Value may not be zero */
1281 Internal ("Data type has no size");
1282 } else if (val > 0) {
1285 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1287 /* Factor is 2, 4 or 8, use special function */
1288 switch (flags & CF_TYPE) {
1291 if (flags & CF_FORCECHAR) {
1293 AddCodeLine ("\tasl\ta");
1300 if (FavourSize || p2 >= 3) {
1301 if (flags & CF_UNSIGNED) {
1302 AddCodeLine ("\tjsr\tshlax%d", p2);
1304 AddCodeLine ("\tjsr\taslax%d", p2);
1307 AddCodeLine ("\tstx\ttmp1");
1309 AddCodeLine ("\tasl\ta");
1310 AddCodeLine ("\trol\ttmp1");
1312 AddCodeLine ("\tldx\ttmp1");
1317 if (flags & CF_UNSIGNED) {
1318 AddCodeLine ("\tjsr\tshleax%d", p2);
1320 AddCodeLine ("\tjsr\tasleax%d", p2);
1329 } else if (val != 1) {
1331 /* Use a multiplication instead */
1332 g_mul (flags | CF_CONST, val);
1340 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1342 /* Factor is 2, 4 or 8, use special function */
1343 switch (flags & CF_TYPE) {
1346 if (flags & CF_FORCECHAR) {
1347 if (flags & CF_UNSIGNED) {
1349 AddCodeLine ("\tlsr\ta");
1352 } else if (p2 <= 2) {
1353 AddCodeLine ("\tcmp\t#$80");
1354 AddCodeLine ("\tror\ta");
1361 if (flags & CF_UNSIGNED) {
1362 if (FavourSize || p2 >= 3) {
1363 AddCodeLine ("\tjsr\tlsrax%d", p2);
1365 AddCodeLine ("\tstx\ttmp1");
1367 AddCodeLine ("\tlsr\ttmp1");
1368 AddCodeLine ("\tror\ta");
1370 AddCodeLine ("\tldx\ttmp1");
1373 if (FavourSize || p2 >= 3) {
1374 AddCodeLine ("\tjsr\tasrax%d", p2);
1376 AddCodeLine ("\tstx\ttmp1");
1378 AddCodeLine ("\tcpx\t#$80");
1379 AddCodeLine ("\tror\ttmp1");
1380 AddCodeLine ("\tror\ta");
1382 AddCodeLine ("\tldx\ttmp1");
1388 if (flags & CF_UNSIGNED) {
1389 AddCodeLine ("\tjsr\tlsreax%d", p2);
1391 AddCodeLine ("\tjsr\tasreax%d", p2);
1400 } else if (val != 1) {
1402 /* Use a division instead */
1403 g_div (flags | CF_CONST, val);
1411 /*****************************************************************************/
1412 /* Adds and subs of variables fix a fixed address */
1413 /*****************************************************************************/
1417 void g_addlocal (unsigned flags, int offs)
1418 /* Add a local variable to ax */
1420 /* Correct the offset and check it */
1422 CheckLocalOffs (offs);
1424 switch (flags & CF_TYPE) {
1427 AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1428 AddCodeLine ("\tclc");
1429 AddCodeLine ("\tadc\t(sp),y");
1430 AddCodeLine ("\tbcc\t*+3");
1431 AddCodeLine ("\tinx");
1432 AddCodeHint ("x:!");
1436 AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1437 AddCodeLine ("\tclc");
1438 AddCodeLine ("\tadc\t(sp),y");
1439 AddCodeLine ("\tpha");
1440 AddCodeLine ("\ttxa");
1441 AddCodeLine ("\tiny");
1442 AddCodeLine ("\tadc\t(sp),y");
1443 AddCodeLine ("\ttax");
1444 AddCodeLine ("\tpla");
1448 /* Do it the old way */
1450 g_getlocal (flags, offs);
1462 void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
1463 /* Add a static variable to ax */
1465 /* Create the correct label name */
1466 char* lbuf = GetLabelName (flags, label, offs);
1468 switch (flags & CF_TYPE) {
1471 AddCodeLine ("\tclc");
1472 AddCodeLine ("\tadc\t%s", lbuf);
1473 AddCodeLine ("\tbcc\t*+3");
1474 AddCodeLine ("\tinx");
1475 AddCodeHint ("x:!");
1479 AddCodeLine ("\tclc");
1480 AddCodeLine ("\tadc\t%s", lbuf);
1481 AddCodeLine ("\ttay");
1482 AddCodeLine ("\ttxa");
1483 AddCodeLine ("\tadc\t%s+1", lbuf);
1484 AddCodeLine ("\ttax");
1485 AddCodeLine ("\ttya");
1489 /* Do it the old way */
1491 g_getstatic (flags, label, offs);
1503 /*****************************************************************************/
1504 /* Compares of ax with a variable with fixed address */
1505 /*****************************************************************************/
1509 void g_cmplocal (unsigned flags, int offs)
1510 /* Compare a local variable to ax */
1512 Internal ("g_cmplocal not implemented");
1517 void g_cmpstatic (unsigned flags, unsigned label, unsigned offs)
1518 /* Compare a static variable to ax */
1520 Internal ("g_cmpstatic not implemented");
1525 /*****************************************************************************/
1526 /* Special op= functions */
1527 /*****************************************************************************/
1531 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
1533 /* Emit += for a static variable */
1535 /* Create the correct label name */
1536 char* lbuf = GetLabelName (flags, label, offs);
1538 /* Check the size and determine operation */
1539 switch (flags & CF_TYPE) {
1542 if (flags & CF_FORCECHAR) {
1543 AddCodeLine ("\tldx\t#$00");
1544 if (flags & CF_CONST) {
1546 AddCodeLine ("\tinc\t%s", lbuf);
1547 AddCodeLine ("\tlda\t%s", lbuf);
1549 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1550 AddCodeLine ("\tclc");
1551 AddCodeLine ("\tadc\t%s", lbuf);
1552 AddCodeLine ("\tsta\t%s", lbuf);
1555 AddCodeLine ("\tclc");
1556 AddCodeLine ("\tadc\t%s", lbuf);
1557 AddCodeLine ("\tsta\t%s", lbuf);
1559 if ((flags & CF_UNSIGNED) == 0) {
1560 AddCodeLine ("\tbpl\t*+3");
1561 AddCodeLine ("\tdex");
1562 AddCodeHint ("x:!"); /* Invalidate X */
1569 if (flags & CF_CONST) {
1571 label = GetLabel ();
1572 AddCodeLine ("\tinc\t%s", lbuf);
1573 AddCodeLine ("\tbne\tL%04X", label);
1574 AddCodeLine ("\tinc\t%s+1", lbuf);
1575 g_defloclabel (label);
1576 AddCodeLine ("\tlda\t%s", lbuf); /* Hmmm... */
1577 AddCodeLine ("\tldx\t%s+1", lbuf);
1579 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1580 AddCodeLine ("\tclc");
1581 AddCodeLine ("\tadc\t%s", lbuf);
1582 AddCodeLine ("\tsta\t%s", lbuf);
1584 label = GetLabel ();
1585 AddCodeLine ("\tbcc\tL%04X", label);
1586 AddCodeLine ("\tinc\t%s+1", lbuf);
1587 g_defloclabel (label);
1588 AddCodeLine ("\tldx\t%s+1", lbuf);
1590 AddCodeLine ("\tlda\t#$%02X", (val >> 8) & 0xFF);
1591 AddCodeLine ("\tadc\t%s+1", lbuf);
1592 AddCodeLine ("\tsta\t%s+1", lbuf);
1593 AddCodeLine ("\ttax");
1594 AddCodeLine ("\tlda\t%s", lbuf);
1598 AddCodeLine ("\tclc");
1599 AddCodeLine ("\tadc\t%s", lbuf);
1600 AddCodeLine ("\tsta\t%s", lbuf);
1601 AddCodeLine ("\ttxa");
1602 AddCodeLine ("\tadc\t%s+1", lbuf);
1603 AddCodeLine ("\tsta\t%s+1", lbuf);
1604 AddCodeLine ("\ttax");
1605 AddCodeLine ("\tlda\t%s", lbuf);
1610 if (flags & CF_CONST) {
1612 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1613 AddCodeLine ("\tsty\tptr1");
1614 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1616 AddCodeLine ("\tjsr\tladdeq1");
1618 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1619 AddCodeLine ("\tjsr\tladdeqa");
1622 g_getstatic (flags, label, offs);
1624 g_putstatic (flags, label, offs);
1627 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1628 AddCodeLine ("\tsty\tptr1");
1629 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1630 AddCodeLine ("\tjsr\tladdeq");
1641 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1642 /* Emit += for a local variable */
1644 /* Calculate the true offset, check it, load it into Y */
1646 CheckLocalOffs (offs);
1648 /* Check the size and determine operation */
1649 switch (flags & CF_TYPE) {
1652 if (flags & CF_FORCECHAR) {
1654 AddCodeLine ("\tldx\t#$00");
1655 if (flags & CF_CONST) {
1656 AddCodeLine ("\tclc");
1657 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1658 AddCodeLine ("\tadc\t(sp,x)");
1659 AddCodeLine ("\tsta\t(sp,x)");
1661 AddCodeLine ("\tclc");
1662 AddCodeLine ("\tadc\t(sp,x)");
1663 AddCodeLine ("\tsta\t(sp,x)");
1667 AddCodeLine ("\tldx\t#$00");
1668 if (flags & CF_CONST) {
1669 AddCodeLine ("\tclc");
1670 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1671 AddCodeLine ("\tadc\t(sp),y");
1672 AddCodeLine ("\tsta\t(sp),y");
1674 AddCodeLine ("\tclc");
1675 AddCodeLine ("\tadc\t(sp),y");
1676 AddCodeLine ("\tsta\t(sp),y");
1679 if ((flags & CF_UNSIGNED) == 0) {
1680 AddCodeLine ("\tbpl\t*+3");
1681 AddCodeLine ("\tdex");
1682 AddCodeHint ("x:!"); /* Invalidate X */
1689 if (flags & CF_CONST) {
1690 g_getimmed (flags, val, 0);
1693 AddCodeLine ("\tjsr\taddeq0sp");
1696 AddCodeLine ("\tjsr\taddeqysp");
1701 if (flags & CF_CONST) {
1702 g_getimmed (flags, val, 0);
1705 AddCodeLine ("\tjsr\tladdeq0sp");
1708 AddCodeLine ("\tjsr\tladdeqysp");
1719 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1720 /* Emit += for the location with address in ax */
1722 /* If the offset is too large for a byte register, add the high byte
1723 * of the offset to the primary. Beware: We need a special correction
1724 * if the offset in the low byte will overflow in the operation.
1726 offs = MakeByteOffs (flags, offs);
1728 /* Check the size and determine operation */
1729 switch (flags & CF_TYPE) {
1732 AddCodeLine ("\tsta\tptr1");
1733 AddCodeLine ("\tstx\tptr1+1");
1735 AddCodeLine ("\tldx\t#$00");
1736 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1737 AddCodeLine ("\tclc");
1738 AddCodeLine ("\tadc\t(ptr1,x)");
1739 AddCodeLine ("\tsta\t(ptr1,x)");
1741 AddCodeLine ("\tldy\t#$%02X", offs);
1742 AddCodeLine ("\tldx\t#$00");
1743 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1744 AddCodeLine ("\tclc");
1745 AddCodeLine ("\tadc\t(ptr1),y");
1746 AddCodeLine ("\tsta\t(ptr1),y");
1752 /* Lots of code, use only if size is not important */
1753 AddCodeLine ("\tsta\tptr1");
1754 AddCodeLine ("\tstx\tptr1+1");
1755 AddCodeLine ("\tldy\t#$%02X", offs);
1756 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1757 AddCodeLine ("\tclc");
1758 AddCodeLine ("\tadc\t(ptr1),y");
1759 AddCodeLine ("\tsta\t(ptr1),y");
1760 AddCodeLine ("\tpha");
1761 AddCodeLine ("\tiny");
1762 AddCodeLine ("\tlda\t#$%02X", (val >> 8) & 0xFF);
1763 AddCodeLine ("\tadc\t(ptr1),y");
1764 AddCodeLine ("\tsta\t(ptr1),y");
1765 AddCodeLine ("\ttax");
1766 AddCodeLine ("\tpla");
1772 AddCodeLine ("\tjsr\tpushax"); /* Push the address */
1773 push (flags); /* Correct the internal sp */
1774 g_getind (flags, offs); /* Fetch the value */
1775 g_inc (flags, val); /* Increment value in primary */
1776 g_putind (flags, offs); /* Store the value back */
1786 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1788 /* Emit -= for a static variable */
1790 /* Create the correct label name */
1791 char* lbuf = GetLabelName (flags, label, offs);
1793 /* Check the size and determine operation */
1794 switch (flags & CF_TYPE) {
1797 if (flags & CF_FORCECHAR) {
1798 AddCodeLine ("\tldx\t#$00");
1799 if (flags & CF_CONST) {
1801 AddCodeLine ("\tdec\t%s", lbuf);
1802 AddCodeLine ("\tlda\t%s", lbuf);
1804 AddCodeLine ("\tsec");
1805 AddCodeLine ("\tlda\t%s", lbuf);
1806 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
1807 AddCodeLine ("\tsta\t%s", lbuf);
1810 AddCodeLine ("\tsec");
1811 AddCodeLine ("\tsta\ttmp1");
1812 AddCodeLine ("\tlda\t%s", lbuf);
1813 AddCodeLine ("\tsbc\ttmp1");
1814 AddCodeLine ("\tsta\t%s", lbuf);
1816 if ((flags & CF_UNSIGNED) == 0) {
1817 AddCodeLine ("\tbpl\t*+3");
1818 AddCodeLine ("\tdex");
1819 AddCodeHint ("x:!"); /* Invalidate X */
1826 AddCodeLine ("\tsec");
1827 if (flags & CF_CONST) {
1828 AddCodeLine ("\tlda\t%s", lbuf);
1829 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
1830 AddCodeLine ("\tsta\t%s", lbuf);
1832 label = GetLabel ();
1833 AddCodeLine ("\tbcs\tL%04X", label);
1834 AddCodeLine ("\tdec\t%s+1", lbuf);
1835 g_defloclabel (label);
1836 AddCodeLine ("\tldx\t%s+1", lbuf);
1838 AddCodeLine ("\tlda\t%s+1", lbuf);
1839 AddCodeLine ("\tsbc\t#$%02X", (val >> 8) & 0xFF);
1840 AddCodeLine ("\tsta\t%s+1", lbuf);
1841 AddCodeLine ("\ttax");
1842 AddCodeLine ("\tlda\t%s", lbuf);
1845 AddCodeLine ("\tsta\ttmp1");
1846 AddCodeLine ("\tlda\t%s", lbuf);
1847 AddCodeLine ("\tsbc\ttmp1");
1848 AddCodeLine ("\tsta\t%s", lbuf);
1849 AddCodeLine ("\tstx\ttmp1");
1850 AddCodeLine ("\tlda\t%s+1", lbuf);
1851 AddCodeLine ("\tsbc\ttmp1");
1852 AddCodeLine ("\tsta\t%s+1", lbuf);
1853 AddCodeLine ("\ttax");
1854 AddCodeLine ("\tlda\t%s", lbuf);
1859 if (flags & CF_CONST) {
1861 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1862 AddCodeLine ("\tsty\tptr1");
1863 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1865 AddCodeLine ("\tjsr\tlsubeq1");
1867 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1868 AddCodeLine ("\tjsr\tlsubeqa");
1871 g_getstatic (flags, label, offs);
1873 g_putstatic (flags, label, offs);
1876 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1877 AddCodeLine ("\tsty\tptr1");
1878 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1879 AddCodeLine ("\tjsr\tlsubeq");
1890 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1891 /* Emit -= for a local variable */
1893 /* Calculate the true offset, check it, load it into Y */
1895 CheckLocalOffs (offs);
1897 /* Check the size and determine operation */
1898 switch (flags & CF_TYPE) {
1901 if (flags & CF_FORCECHAR) {
1903 AddCodeLine ("\tldx\t#$00");
1904 AddCodeLine ("\tsec");
1905 if (flags & CF_CONST) {
1906 AddCodeLine ("\tlda\t(sp),y");
1907 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
1909 AddCodeLine ("\tsta\ttmp1");
1910 AddCodeLine ("\tlda\t(sp),y");
1911 AddCodeLine ("\tsbc\ttmp1");
1913 AddCodeLine ("\tsta\t(sp),y");
1914 if ((flags & CF_UNSIGNED) == 0) {
1915 AddCodeLine ("\tbpl\t*+3");
1916 AddCodeLine ("\tdex");
1917 AddCodeHint ("x:!"); /* Invalidate X */
1924 if (flags & CF_CONST) {
1925 g_getimmed (flags, val, 0);
1928 AddCodeLine ("\tjsr\tsubeq0sp");
1931 AddCodeLine ("\tjsr\tsubeqysp");
1936 if (flags & CF_CONST) {
1937 g_getimmed (flags, val, 0);
1940 AddCodeLine ("\tjsr\tlsubeq0sp");
1943 AddCodeLine ("\tjsr\tlsubeqysp");
1954 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
1955 /* Emit -= for the location with address in ax */
1957 /* If the offset is too large for a byte register, add the high byte
1958 * of the offset to the primary. Beware: We need a special correction
1959 * if the offset in the low byte will overflow in the operation.
1961 offs = MakeByteOffs (flags, offs);
1963 /* Check the size and determine operation */
1964 switch (flags & CF_TYPE) {
1967 AddCodeLine ("\tsta\tptr1");
1968 AddCodeLine ("\tstx\tptr1+1");
1970 AddCodeLine ("\tldx\t#$00");
1971 AddCodeLine ("\tlda\t(ptr1,x)");
1972 AddCodeLine ("\tsec");
1973 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
1974 AddCodeLine ("\tsta\t(ptr1,x)");
1976 AddCodeLine ("\tldy\t#$%02X", offs);
1977 AddCodeLine ("\tldx\t#$00");
1978 AddCodeLine ("\tlda\t(ptr1),y");
1979 AddCodeLine ("\tsec");
1980 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
1981 AddCodeLine ("\tsta\t(ptr1),y");
1987 /* Lots of code, use only if size is not important */
1988 AddCodeLine ("\tsta\tptr1");
1989 AddCodeLine ("\tstx\tptr1+1");
1990 AddCodeLine ("\tldy\t#$%02X", offs);
1991 AddCodeLine ("\tlda\t(ptr1),y");
1992 AddCodeLine ("\tsec");
1993 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
1994 AddCodeLine ("\tsta\t(ptr1),y");
1995 AddCodeLine ("\tpha");
1996 AddCodeLine ("\tiny");
1997 AddCodeLine ("\tlda\t(ptr1),y");
1998 AddCodeLine ("\tsbc\t#$%02X", (val >> 8) & 0xFF);
1999 AddCodeLine ("\tsta\t(ptr1),y");
2000 AddCodeLine ("\ttax");
2001 AddCodeLine ("\tpla");
2007 AddCodeLine ("\tjsr\tpushax"); /* Push the address */
2008 push (flags); /* Correct the internal sp */
2009 g_getind (flags, offs); /* Fetch the value */
2010 g_dec (flags, val); /* Increment value in primary */
2011 g_putind (flags, offs); /* Store the value back */
2021 /*****************************************************************************/
2022 /* Add a variable address to the value in ax */
2023 /*****************************************************************************/
2027 void g_addaddr_local (unsigned flags, int offs)
2028 /* Add the address of a local variable to ax */
2030 /* Add the offset */
2033 /* We cannot address more then 256 bytes of locals anyway */
2034 CheckLocalOffs (offs);
2035 AddCodeLine ("\tclc");
2036 AddCodeLine ("\tadc\t#$%02X", offs & 0xFF);
2037 AddCodeLine ("\tbcc\t*+4"); /* Do also skip the CLC insn below */
2038 AddCodeLine ("\tinx");
2039 AddCodeHint ("x:!"); /* Invalidate X */
2042 /* Add the current stackpointer value */
2043 AddCodeLine ("\tclc");
2044 AddCodeLine ("\tadc\tsp");
2045 AddCodeLine ("\ttay");
2046 AddCodeLine ("\ttxa");
2047 AddCodeLine ("\tadc\tsp+1");
2048 AddCodeLine ("\ttax");
2049 AddCodeLine ("\ttya");
2054 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2055 /* Add the address of a static variable to ax */
2057 /* Create the correct label name */
2058 char* lbuf = GetLabelName (flags, label, offs);
2060 /* Add the address to the current ax value */
2061 AddCodeLine ("\tclc");
2062 AddCodeLine ("\tadc\t#<(%s)", lbuf);
2063 AddCodeLine ("\ttay");
2064 AddCodeLine ("\ttxa");
2065 AddCodeLine ("\tadc\t#>(%s)", lbuf);
2066 AddCodeLine ("\ttax");
2067 AddCodeLine ("\ttya");
2072 /*****************************************************************************/
2074 /*****************************************************************************/
2078 void g_save (unsigned flags)
2079 /* Copy primary register to hold register. */
2081 /* Check the size and determine operation */
2082 switch (flags & CF_TYPE) {
2085 if (flags & CF_FORCECHAR) {
2086 AddCodeLine ("\tpha");
2092 AddCodeLine ("\tsta\tregsave");
2093 AddCodeLine ("\tstx\tregsave+1");
2097 AddCodeLine ("\tjsr\tsaveeax");
2107 void g_restore (unsigned flags)
2108 /* Copy hold register to P. */
2110 /* Check the size and determine operation */
2111 switch (flags & CF_TYPE) {
2114 if (flags & CF_FORCECHAR) {
2115 AddCodeLine ("\tpla");
2121 AddCodeLine ("\tlda\tregsave");
2122 AddCodeLine ("\tldx\tregsave+1");
2126 AddCodeLine ("\tjsr\tresteax");
2136 void g_cmp (unsigned flags, unsigned long val)
2137 /* Immidiate compare. The primary register will not be changed, Z flag
2141 /* Check the size and determine operation */
2142 switch (flags & CF_TYPE) {
2145 if (flags & CF_FORCECHAR) {
2146 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
2152 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
2153 AddCodeLine ("\tbne\t*+4");
2154 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
2158 Internal ("g_cmp: Long compares not implemented");
2168 static void oper (unsigned flags, unsigned long val, char** subs)
2169 /* Encode a binary operation. subs is a pointer to four groups of three
2171 * 0-2 --> Operate on ints
2172 * 3-5 --> Operate on unsigneds
2173 * 6-8 --> Operate on longs
2174 * 9-11 --> Operate on unsigned longs
2176 * The first subroutine names in each string group is used to encode an
2177 * operation with a zero constant, the second to encode an operation with
2178 * a 8 bit constant, and the third is used in all other cases.
2183 /* Determine the offset into the array */
2184 offs = (flags & CF_UNSIGNED)? 3 : 0;
2185 switch (flags & CF_TYPE) {
2198 /* Encode the operation */
2199 if (flags & CF_CONST) {
2200 /* Constant value given */
2201 if (val == 0 && subs [offs+0]) {
2202 /* Special case: constant with value zero */
2203 AddCodeLine ("\tjsr\t%s", subs [offs+0]);
2204 } else if (val < 0x100 && subs [offs+1]) {
2205 /* Special case: constant with high byte zero */
2206 ldaconst (val); /* Load low byte */
2207 AddCodeLine ("\tjsr\t%s", subs [offs+1]);
2209 /* Others: arbitrary constant value */
2210 g_getimmed (flags, val, 0); /* Load value */
2211 AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2214 /* Value not constant (is already in (e)ax) */
2215 AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2218 /* The operation will pop it's argument */
2224 void g_test (unsigned flags)
2225 /* Force a test to set cond codes right */
2227 switch (flags & CF_TYPE) {
2230 if (flags & CF_FORCECHAR) {
2231 AddCodeLine ("\ttax");
2237 AddCodeLine ("\tstx\ttmp1");
2238 AddCodeLine ("\tora\ttmp1");
2242 if (flags & CF_UNSIGNED) {
2243 AddCodeLine ("\tjsr\tutsteax");
2245 AddCodeLine ("\tjsr\ttsteax");
2257 void g_push (unsigned flags, unsigned long val)
2258 /* Push the primary register or a constant value onto the stack */
2262 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2264 /* We have a constant 8 or 16 bit value */
2265 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2267 /* Handle as 8 bit value */
2268 if (FavourSize && val <= 2) {
2269 AddCodeLine ("\tjsr\tpushc%d", (int) val);
2272 AddCodeLine ("\tjsr\tpusha");
2277 /* Handle as 16 bit value */
2278 hi = (unsigned char) (val >> 8);
2280 AddCodeLine ("\tjsr\tpush%u", (unsigned) val);
2281 } else if (hi == 0 || hi == 0xFF) {
2282 /* Use special function */
2284 AddCodeLine ("\tjsr\t%s", (hi == 0)? "pusha0" : "pushaFF");
2287 g_getimmed (flags, val, 0);
2288 AddCodeLine ("\tjsr\tpushax");
2294 /* Value is not 16 bit or not constant */
2295 if (flags & CF_CONST) {
2296 /* Constant 32 bit value, load into eax */
2297 g_getimmed (flags, val, 0);
2300 /* Push the primary register */
2301 switch (flags & CF_TYPE) {
2304 if (flags & CF_FORCECHAR) {
2305 /* Handle as char */
2306 AddCodeLine ("\tjsr\tpusha");
2311 AddCodeLine ("\tjsr\tpushax");
2315 AddCodeLine ("\tjsr\tpusheax");
2325 /* Adjust the stack offset */
2331 void g_swap (unsigned flags)
2332 /* Swap the primary register and the top of the stack. flags give the type
2333 * of *both* values (must have same size).
2336 switch (flags & CF_TYPE) {
2340 AddCodeLine ("\tjsr\tswapstk");
2344 AddCodeLine ("\tjsr\tswapestk");
2355 void g_call (unsigned flags, char* lbl, unsigned argsize)
2356 /* Call the specified subroutine name */
2358 if ((flags & CF_FIXARGC) == 0) {
2359 /* Pass arg count */
2362 AddCodeLine ("\tjsr\t_%s", lbl);
2363 oursp += argsize; /* callee pops args */
2368 void g_callind (unsigned flags, unsigned argsize)
2369 /* Call subroutine with address in AX */
2371 if ((flags & CF_FIXARGC) == 0) {
2372 /* Pass arg count */
2375 AddCodeLine ("\tjsr\tcallax"); /* do the call */
2376 oursp += argsize; /* callee pops args */
2381 void g_jump (unsigned label)
2382 /* Jump to specified internal label number */
2384 AddCodeLine ("\tjmp\tL%04X", label);
2389 void g_switch (unsigned flags)
2390 /* Output switch statement preample */
2392 switch (flags & CF_TYPE) {
2396 AddCodeLine ("\tjsr\tswitch");
2400 AddCodeLine ("\tjsr\tlswitch");
2411 void g_case (unsigned flags, unsigned label, unsigned long val)
2412 /* Create table code for one case selector */
2414 switch (flags & CF_TYPE) {
2418 AddCodeLine ("\t.word\t$%04X, L%04X", val & 0xFFFF, label & 0xFFFF);
2422 AddCodeLine ("\t.dword\t$%08X", val);
2423 AddCodeLine ("\t.word\tL%04X", label & 0xFFFF);
2434 void g_truejump (unsigned flags, unsigned label)
2435 /* Jump to label if zero flag clear */
2437 if (flags & CF_SHORT) {
2438 AddCodeLine ("\tbne\tL%04X", label);
2440 AddCodeLine ("\tjne\tL%04X", label);
2446 void g_falsejump (unsigned flags, unsigned label)
2447 /* Jump to label if zero flag set */
2449 if (flags & CF_SHORT) {
2450 AddCodeLine ("\tbeq\tL%04X", label);
2452 AddCodeLine ("\tjeq\tL%04X", label);
2458 static void mod_internal (int k, char* verb1, char* verb2)
2461 AddCodeLine ("\tjsr\t%ssp%c", verb1, k + '0');
2465 AddCodeLine ("\tjsr\t%ssp", verb2);
2471 void g_space (int space)
2472 /* Create or drop space on the stack */
2475 mod_internal (-space, "inc", "addy");
2476 } else if (space > 0) {
2477 mod_internal (space, "dec", "suby");
2483 void g_add (unsigned flags, unsigned long val)
2484 /* Primary = TOS + Primary */
2486 static char* ops [12] = {
2487 0, "tosadda0", "tosaddax",
2488 0, "tosadda0", "tosaddax",
2493 if (flags & CF_CONST) {
2494 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2495 g_push (flags & ~CF_CONST, 0);
2497 oper (flags, val, ops);
2502 void g_sub (unsigned flags, unsigned long val)
2503 /* Primary = TOS - Primary */
2505 static char* ops [12] = {
2506 0, "tossuba0", "tossubax",
2507 0, "tossuba0", "tossubax",
2512 if (flags & CF_CONST) {
2513 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2514 g_push (flags & ~CF_CONST, 0);
2516 oper (flags, val, ops);
2521 void g_rsub (unsigned flags, unsigned long val)
2522 /* Primary = Primary - TOS */
2524 static char* ops [12] = {
2525 0, "tosrsuba0", "tosrsubax",
2526 0, "tosrsuba0", "tosrsubax",
2530 oper (flags, val, ops);
2535 void g_mul (unsigned flags, unsigned long val)
2536 /* Primary = TOS * Primary */
2538 static char* ops [12] = {
2539 0, "tosmula0", "tosmulax",
2540 0, "tosumula0", "tosumulax",
2547 /* Do strength reduction if the value is constant and a power of two */
2548 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2549 /* Generate a shift instead */
2554 /* If the right hand side is const, the lhs is not on stack but still
2555 * in the primary register.
2557 if (flags & CF_CONST) {
2559 switch (flags & CF_TYPE) {
2562 if (flags & CF_FORCECHAR) {
2563 /* Handle some special cases */
2567 AddCodeLine ("\tsta\ttmp1");
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");
2582 AddCodeLine ("\tsta\ttmp1");
2583 AddCodeLine ("\tasl\ta");
2584 AddCodeLine ("\tasl\ta");
2585 AddCodeLine ("\tclc");
2586 AddCodeLine ("\tadc\ttmp1");
2587 AddCodeLine ("\tasl\ta");
2603 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2604 * into the normal, non-optimized stuff.
2606 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2607 g_push (flags & ~CF_CONST, 0);
2611 /* Use long way over the stack */
2612 oper (flags, val, ops);
2617 void g_div (unsigned flags, unsigned long val)
2618 /* Primary = TOS / Primary */
2620 static char* ops [12] = {
2621 0, "tosdiva0", "tosdivax",
2622 0, "tosudiva0", "tosudivax",
2627 /* Do strength reduction if the value is constant and a power of two */
2629 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2630 /* Generate a shift instead */
2633 /* Generate a division */
2634 if (flags & CF_CONST) {
2635 /* lhs is not on stack */
2636 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2637 g_push (flags & ~CF_CONST, 0);
2639 oper (flags, val, ops);
2645 void g_mod (unsigned flags, unsigned long val)
2646 /* Primary = TOS % Primary */
2648 static char* ops [12] = {
2649 0, "tosmoda0", "tosmodax",
2650 0, "tosumoda0", "tosumodax",
2656 /* Check if we can do some cost reduction */
2657 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2658 /* We can do that with an AND operation */
2659 g_and (flags, val - 1);
2661 /* Do it the hard way... */
2662 if (flags & CF_CONST) {
2663 /* lhs is not on stack */
2664 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2665 g_push (flags & ~CF_CONST, 0);
2667 oper (flags, val, ops);
2673 void g_or (unsigned flags, unsigned long val)
2674 /* Primary = TOS | Primary */
2676 static char* ops [12] = {
2677 0, "tosora0", "tosorax",
2678 0, "tosora0", "tosorax",
2683 /* If the right hand side is const, the lhs is not on stack but still
2684 * in the primary register.
2686 if (flags & CF_CONST) {
2688 switch (flags & CF_TYPE) {
2691 if (flags & CF_FORCECHAR) {
2692 if ((val & 0xFF) != 0xFF) {
2693 AddCodeLine ("\tora\t#$%02X", val & 0xFF);
2701 AddCodeLine ("\tora\t#$%02X", val & 0xFF);
2708 AddCodeLine ("\tora\t#$%02X", val & 0xFF);
2717 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2718 * into the normal, non-optimized stuff.
2720 g_push (flags & ~CF_CONST, 0);
2724 /* Use long way over the stack */
2725 oper (flags, val, ops);
2730 void g_xor (unsigned flags, unsigned long val)
2731 /* Primary = TOS ^ Primary */
2733 static char* ops [12] = {
2734 0, "tosxora0", "tosxorax",
2735 0, "tosxora0", "tosxorax",
2741 /* If the right hand side is const, the lhs is not on stack but still
2742 * in the primary register.
2744 if (flags & CF_CONST) {
2746 switch (flags & CF_TYPE) {
2749 if (flags & CF_FORCECHAR) {
2750 if ((val & 0xFF) != 0) {
2751 AddCodeLine ("\teor\t#$%02X", val & 0xFF);
2760 AddCodeLine ("\teor\t#$%02X", val);
2763 } else if ((val & 0xFF) == 0) {
2764 AddCodeLine ("\tpha");
2765 AddCodeLine ("\ttxa");
2766 AddCodeLine ("\teor\t#$%02X", (val >> 8) & 0xFF);
2767 AddCodeLine ("\ttax");
2768 AddCodeLine ("\tpla");
2776 AddCodeLine ("\teor\t#$%02X", val & 0xFF);
2786 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2787 * into the normal, non-optimized stuff.
2789 g_push (flags & ~CF_CONST, 0);
2793 /* Use long way over the stack */
2794 oper (flags, val, ops);
2799 void g_and (unsigned flags, unsigned long val)
2800 /* Primary = TOS & Primary */
2802 static char* ops [12] = {
2803 0, "tosanda0", "tosandax",
2804 0, "tosanda0", "tosandax",
2809 /* If the right hand side is const, the lhs is not on stack but still
2810 * in the primary register.
2812 if (flags & CF_CONST) {
2814 switch (flags & CF_TYPE) {
2817 if (flags & CF_FORCECHAR) {
2818 AddCodeLine ("\tand\t#$%02X", val & 0xFF);
2823 if ((val & 0xFFFF) != 0xFFFF) {
2828 } else if (val != 0xFF) {
2829 AddCodeLine ("\tand\t#$%02X", val & 0xFF);
2831 } else if ((val & 0xFF00) == 0xFF00) {
2832 AddCodeLine ("\tand\t#$%02X", val & 0xFF);
2833 } else if ((val & 0x00FF) == 0x0000) {
2834 AddCodeLine ("\ttxa");
2835 AddCodeLine ("\tand\t#$%02X", (val >> 8) & 0xFF);
2836 AddCodeLine ("\ttax");
2839 AddCodeLine ("\ttay");
2840 AddCodeLine ("\ttxa");
2841 AddCodeLine ("\tand\t#$%02X", (val >> 8) & 0xFF);
2842 AddCodeLine ("\ttax");
2843 AddCodeLine ("\ttya");
2844 if ((val & 0x00FF) != 0x00FF) {
2845 AddCodeLine ("\tand\t#$%02X", val & 0xFF);
2854 AddCodeLine ("\tstx\tsreg+1");
2855 AddCodeLine ("\tstx\tsreg");
2856 if ((val & 0xFF) != 0xFF) {
2857 AddCodeLine ("\tand\t#$%02X", val & 0xFF);
2860 } else if (val == 0xFF00) {
2862 AddCodeLine ("\tsta\tsreg+1");
2863 AddCodeLine ("\tsta\tsreg");
2872 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2873 * into the normal, non-optimized stuff.
2875 g_push (flags & ~CF_CONST, 0);
2879 /* Use long way over the stack */
2880 oper (flags, val, ops);
2885 void g_asr (unsigned flags, unsigned long val)
2886 /* Primary = TOS >> Primary */
2888 static char* ops [12] = {
2889 0, "tosasra0", "tosasrax",
2890 0, "tosshra0", "tosshrax",
2895 /* If the right hand side is const, the lhs is not on stack but still
2896 * in the primary register.
2898 if (flags & CF_CONST) {
2900 switch (flags & CF_TYPE) {
2904 if (val >= 1 && val <= 3) {
2905 if (flags & CF_UNSIGNED) {
2906 AddCodeLine ("\tjsr\tshrax%ld", val);
2908 AddCodeLine ("\tjsr\tasrax%ld", val);
2911 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2912 AddCodeLine ("\ttxa");
2919 if (val >= 1 && val <= 3) {
2920 if (flags & CF_UNSIGNED) {
2921 AddCodeLine ("\tjsr\tshreax%ld", val);
2923 AddCodeLine ("\tjsr\tasreax%ld", val);
2926 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2927 AddCodeLine ("\ttxa");
2928 AddCodeLine ("\tldx\tsreg");
2929 AddCodeLine ("\tldy\tsreg+1");
2930 AddCodeLine ("\tsty\tsreg");
2931 AddCodeLine ("\tldy\t#$00");
2932 AddCodeLine ("\tsty\tsreg+1");
2934 } else if (val == 16) {
2935 AddCodeLine ("\tldy\t#$00");
2936 AddCodeLine ("\tldx\tsreg+1");
2937 if ((flags & CF_UNSIGNED) == 0) {
2938 AddCodeLine ("\tbpl\t*+3");
2939 AddCodeLine ("\tdey");
2940 AddCodeHint ("y:!");
2942 AddCodeLine ("\tlda\tsreg");
2943 AddCodeLine ("\tsty\tsreg+1");
2944 AddCodeLine ("\tsty\tsreg");
2953 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2954 * into the normal, non-optimized stuff.
2956 g_push (flags & ~CF_CONST, 0);
2960 /* Use long way over the stack */
2961 oper (flags, val, ops);
2966 void g_asl (unsigned flags, unsigned long val)
2967 /* Primary = TOS << Primary */
2969 static char* ops [12] = {
2970 0, "tosasla0", "tosaslax",
2971 0, "tosshla0", "tosshlax",
2977 /* If the right hand side is const, the lhs is not on stack but still
2978 * in the primary register.
2980 if (flags & CF_CONST) {
2982 switch (flags & CF_TYPE) {
2986 if (val >= 1 && val <= 3) {
2987 if (flags & CF_UNSIGNED) {
2988 AddCodeLine ("\tjsr\tshlax%ld", val);
2990 AddCodeLine ("\tjsr\taslax%ld", val);
2993 } else if (val == 8) {
2994 AddCodeLine ("\ttax");
2995 AddCodeLine ("\tlda\t#$00");
3001 if (val >= 1 && val <= 3) {
3002 if (flags & CF_UNSIGNED) {
3003 AddCodeLine ("\tjsr\tshleax%ld", val);
3005 AddCodeLine ("\tjsr\tasleax%ld", val);
3008 } else if (val == 8) {
3009 AddCodeLine ("\tldy\tsreg");
3010 AddCodeLine ("\tsty\tsreg+1");
3011 AddCodeLine ("\tstx\tsreg");
3012 AddCodeLine ("\ttax");
3013 AddCodeLine ("\tlda\t#$00");
3015 } else if (val == 16) {
3016 AddCodeLine ("\tstx\tsreg+1");
3017 AddCodeLine ("\tsta\tsreg");
3018 AddCodeLine ("\tlda\t#$00");
3019 AddCodeLine ("\ttax");
3028 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3029 * into the normal, non-optimized stuff.
3031 g_push (flags & ~CF_CONST, 0);
3035 /* Use long way over the stack */
3036 oper (flags, val, ops);
3041 void g_neg (unsigned flags)
3042 /* Primary = -Primary */
3044 switch (flags & CF_TYPE) {
3048 AddCodeLine ("\tjsr\tnegax");
3052 AddCodeLine ("\tjsr\tnegeax");
3062 void g_bneg (unsigned flags)
3063 /* Primary = !Primary */
3065 switch (flags & CF_TYPE) {
3068 AddCodeLine ("\tjsr\tbnega");
3072 AddCodeLine ("\tjsr\tbnegax");
3076 AddCodeLine ("\tjsr\tbnegeax");
3086 void g_com (unsigned flags)
3087 /* Primary = ~Primary */
3089 switch (flags & CF_TYPE) {
3093 AddCodeLine ("\tjsr\tcomplax");
3097 AddCodeLine ("\tjsr\tcompleax");
3107 void g_inc (unsigned flags, unsigned long val)
3108 /* Increment the primary register by a given number */
3110 /* Don't inc by zero */
3115 /* Generate code for the supported types */
3117 switch (flags & CF_TYPE) {
3120 if (flags & CF_FORCECHAR) {
3121 if (CPU == CPU_65C02 && val <= 2) {
3123 AddCodeLine ("\tina");
3126 AddCodeLine ("\tclc");
3127 AddCodeLine ("\tadc\t#$%02X", val & 0xFF);
3134 if (CPU == CPU_65C02 && val == 1) {
3135 AddCodeLine ("\tina");
3136 AddCodeLine ("\tbne\t*+3");
3137 AddCodeLine ("\tinx");
3138 /* Tell the optimizer that the X register may be invalid */
3139 AddCodeHint ("x:!");
3140 } else if (FavourSize) {
3143 AddCodeLine ("\tjsr\tincax%u", val);
3144 } else if (val <= 255) {
3146 AddCodeLine ("\tjsr\tincaxy");
3148 g_add (flags | CF_CONST, val);
3151 /* Inline the code */
3153 if ((val & 0xFF) != 0) {
3154 AddCodeLine ("\tclc");
3155 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3156 AddCodeLine ("\tbcc\t*+3");
3157 AddCodeLine ("\tinx");
3158 /* Tell the optimizer that the X register may be invalid */
3159 AddCodeHint ("x:!");
3162 AddCodeLine ("\tinx");
3165 AddCodeLine ("\tinx");
3168 AddCodeLine ("\tclc");
3169 if ((val & 0xFF) != 0) {
3170 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3171 /* Tell the optimizer that the X register may be invalid */
3172 AddCodeHint ("x:!");
3174 AddCodeLine ("\tpha");
3175 AddCodeLine ("\ttxa");
3176 AddCodeLine ("\tadc\t#$%02X", (unsigned char) (val >> 8));
3177 AddCodeLine ("\ttax");
3178 AddCodeLine ("\tpla");
3186 AddCodeLine ("\tjsr\tinceaxy");
3188 g_add (flags | CF_CONST, val);
3200 void g_dec (unsigned flags, unsigned long val)
3201 /* Decrement the primary register by a given number */
3203 /* Generate code for the supported types */
3205 switch (flags & CF_TYPE) {
3208 if (flags & CF_FORCECHAR) {
3209 if (CPU == CPU_65C02 && val <= 2) {
3211 AddCodeLine ("\tdea");
3214 AddCodeLine ("\tsec");
3215 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
3223 AddCodeLine ("\tjsr\tdecax%d", (int) val);
3224 } else if (val <= 255) {
3226 AddCodeLine ("\tjsr\tdecaxy");
3228 g_sub (flags | CF_CONST, val);
3235 AddCodeLine ("\tjsr\tdeceaxy");
3237 g_sub (flags | CF_CONST, val);
3250 * Following are the conditional operators. They compare the TOS against
3251 * the primary and put a literal 1 in the primary if the condition is
3252 * true, otherwise they clear the primary register
3257 void g_eq (unsigned flags, unsigned long val)
3258 /* Test for equal */
3260 static char* ops [12] = {
3261 "toseq00", "toseqa0", "toseqax",
3262 "toseq00", "toseqa0", "toseqax",
3267 /* If the right hand side is const, the lhs is not on stack but still
3268 * in the primary register.
3270 if (flags & CF_CONST) {
3272 switch (flags & CF_TYPE) {
3275 if (flags & CF_FORCECHAR) {
3276 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3277 AddCodeLine ("\tjsr\tbooleq");
3283 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
3284 AddCodeLine ("\tbne\t*+4");
3285 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3286 AddCodeLine ("\tjsr\tbooleq");
3296 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3297 * into the normal, non-optimized stuff.
3299 g_push (flags & ~CF_CONST, 0);
3303 /* Use long way over the stack */
3304 oper (flags, val, ops);
3309 void g_ne (unsigned flags, unsigned long val)
3310 /* Test for not equal */
3312 static char* ops [12] = {
3313 "tosne00", "tosnea0", "tosneax",
3314 "tosne00", "tosnea0", "tosneax",
3320 /* If the right hand side is const, the lhs is not on stack but still
3321 * in the primary register.
3323 if (flags & CF_CONST) {
3325 switch (flags & CF_TYPE) {
3328 if (flags & CF_FORCECHAR) {
3329 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3330 AddCodeLine ("\tjsr\tboolne");
3336 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
3337 AddCodeLine ("\tbne\t*+4");
3338 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3339 AddCodeLine ("\tjsr\tboolne");
3349 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3350 * into the normal, non-optimized stuff.
3352 g_push (flags & ~CF_CONST, 0);
3356 /* Use long way over the stack */
3357 oper (flags, val, ops);
3362 void g_lt (unsigned flags, unsigned long val)
3363 /* Test for less than */
3365 static char* ops [12] = {
3366 "toslt00", "toslta0", "tosltax",
3367 "tosult00", "tosulta0", "tosultax",
3372 /* If the right hand side is const, the lhs is not on stack but still
3373 * in the primary register.
3375 if (flags & CF_CONST) {
3377 /* Give a warning in some special cases */
3378 if ((flags & CF_UNSIGNED) && val == 0) {
3379 Warning (WARN_COND_NEVER_TRUE);
3382 /* Look at the type */
3383 switch (flags & CF_TYPE) {
3386 if (flags & CF_FORCECHAR) {
3387 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3388 if (flags & CF_UNSIGNED) {
3389 AddCodeLine ("\tjsr\tboolult");
3391 AddCodeLine ("\tjsr\tboollt");
3398 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3399 /* If we have a signed compare against zero, we only need to
3400 * test the high byte.
3402 AddCodeLine ("\ttxa");
3403 AddCodeLine ("\tjsr\tboollt");
3406 /* Direct code only for unsigned data types */
3407 if (flags & CF_UNSIGNED) {
3408 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
3409 AddCodeLine ("\tbne\t*+4");
3410 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3411 AddCodeLine ("\tjsr\tboolult");
3423 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3424 * into the normal, non-optimized stuff.
3426 g_push (flags & ~CF_CONST, 0);
3430 /* Use long way over the stack */
3431 oper (flags, val, ops);
3436 void g_le (unsigned flags, unsigned long val)
3437 /* Test for less than or equal to */
3439 static char* ops [12] = {
3440 "tosle00", "toslea0", "tosleax",
3441 "tosule00", "tosulea0", "tosuleax",
3447 /* If the right hand side is const, the lhs is not on stack but still
3448 * in the primary register.
3450 if (flags & CF_CONST) {
3452 /* Look at the type */
3453 switch (flags & CF_TYPE) {
3456 if (flags & CF_FORCECHAR) {
3457 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3458 if (flags & CF_UNSIGNED) {
3459 AddCodeLine ("\tjsr\tboolule");
3461 AddCodeLine ("\tjsr\tboolle");
3468 if (flags & CF_UNSIGNED) {
3469 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
3470 AddCodeLine ("\tbne\t*+4");
3471 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3472 AddCodeLine ("\tjsr\tboolule");
3484 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3485 * into the normal, non-optimized stuff.
3487 g_push (flags & ~CF_CONST, 0);
3491 /* Use long way over the stack */
3492 oper (flags, val, ops);
3497 void g_gt (unsigned flags, unsigned long val)
3498 /* Test for greater than */
3500 static char* ops [12] = {
3501 "tosgt00", "tosgta0", "tosgtax",
3502 "tosugt00", "tosugta0", "tosugtax",
3508 /* If the right hand side is const, the lhs is not on stack but still
3509 * in the primary register.
3511 if (flags & CF_CONST) {
3513 /* Look at the type */
3514 switch (flags & CF_TYPE) {
3517 if (flags & CF_FORCECHAR) {
3518 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3519 if (flags & CF_UNSIGNED) {
3520 /* If we have a compare > 0, we will replace it by
3521 * != 0 here, since both are identical but the latter
3522 * is easier to optimize.
3525 AddCodeLine ("\tjsr\tboolugt");
3527 AddCodeLine ("\tjsr\tboolne");
3530 AddCodeLine ("\tjsr\tboolgt");
3537 if (flags & CF_UNSIGNED) {
3538 /* If we have a compare > 0, we will replace it by
3539 * != 0 here, since both are identical but the latter
3540 * is easier to optimize.
3542 if ((val & 0xFFFF) == 0) {
3543 AddCodeLine ("\tstx\ttmp1");
3544 AddCodeLine ("\tora\ttmp1");
3545 AddCodeLine ("\tjsr\tboolne");
3547 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
3548 AddCodeLine ("\tbne\t*+4");
3549 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3550 AddCodeLine ("\tjsr\tboolugt");
3563 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3564 * into the normal, non-optimized stuff.
3566 g_push (flags & ~CF_CONST, 0);
3570 /* Use long way over the stack */
3571 oper (flags, val, ops);
3576 void g_ge (unsigned flags, unsigned long val)
3577 /* Test for greater than or equal to */
3579 static char* ops [12] = {
3580 "tosge00", "tosgea0", "tosgeax",
3581 "tosuge00", "tosugea0", "tosugeax",
3587 /* If the right hand side is const, the lhs is not on stack but still
3588 * in the primary register.
3590 if (flags & CF_CONST) {
3592 /* Give a warning in some special cases */
3593 if ((flags & CF_UNSIGNED) && val == 0) {
3594 Warning (WARN_COND_ALWAYS_TRUE);
3597 /* Look at the type */
3598 switch (flags & CF_TYPE) {
3601 if (flags & CF_FORCECHAR) {
3602 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3603 if (flags & CF_UNSIGNED) {
3604 AddCodeLine ("\tjsr\tbooluge");
3606 AddCodeLine ("\tjsr\tboolge");
3613 if (flags & CF_UNSIGNED) {
3614 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
3615 AddCodeLine ("\tbne\t*+4");
3616 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3617 AddCodeLine ("\tjsr\tbooluge");
3629 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3630 * into the normal, non-optimized stuff.
3632 g_push (flags & ~CF_CONST, 0);
3636 /* Use long way over the stack */
3637 oper (flags, val, ops);
3642 /*****************************************************************************/
3643 /* Allocating static storage */
3644 /*****************************************************************************/
3648 void g_res (unsigned n)
3649 /* Reserve static storage, n bytes */
3651 AddCodeLine ("\t.res\t%u,$00", n);
3656 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3657 /* Define data with the size given in flags */
3659 if (flags & CF_CONST) {
3661 /* Numeric constant */
3662 switch (flags & CF_TYPE) {
3665 AddCodeLine ("\t.byte\t$%02lX", val & 0xFF);
3669 AddCodeLine ("\t.word\t$%04lX", val & 0xFFFF);
3673 AddCodeLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3684 /* Create the correct label name */
3685 const char* Label = GetLabelName (flags, val, offs);
3687 /* Labels are always 16 bit */
3688 AddCodeLine ("\t.word\t%s", Label);
3695 void g_defbytes (const unsigned char* Bytes, unsigned Count)
3696 /* Output a row of bytes as a constant */
3702 /* Output the stuff */
3705 /* How many go into this line? */
3706 if ((Chunk = Count) > 16) {
3711 /* Output one line */
3712 strcpy (Buf, "\t.byte\t");
3715 B += sprintf (B, "$%02X", *Bytes++ & 0xFF);
3721 /* Output the line */
3728 void g_zerobytes (unsigned n)
3729 /* Output n bytes of data initialized with zero */
3731 AddCodeLine ("\t.res\t%u,$00", n);
3736 /*****************************************************************************/
3737 /* Inlined known functions */
3738 /*****************************************************************************/
3742 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3743 /* Inline the strlen() function */
3745 /* We need a label in both cases */
3746 unsigned label = GetLabel ();
3748 /* Two different encodings */
3749 if (flags & CF_CONST) {
3751 /* The address of the string is constant. Create the correct label name */
3752 char* lbuf = GetLabelName (flags, val, offs);
3754 /* Generate the strlen code */
3755 AddCodeLine ("\tldy\t#$FF");
3756 g_defloclabel (label);
3757 AddCodeLine ("\tiny");
3758 AddCodeLine ("\tlda\t%s,y", lbuf);
3759 AddCodeLine ("\tbne\tL%04X", label);
3760 AddCodeLine ("\ttax");
3761 AddCodeLine ("\ttya");
3765 /* Address not constant but in primary */
3767 /* This is too much code, so call strlen instead of inlining */
3768 AddCodeLine ("\tjsr\t_strlen");
3770 /* Inline the function */
3771 AddCodeLine ("\tsta\tptr1");
3772 AddCodeLine ("\tstx\tptr1+1");
3773 AddCodeLine ("\tldy\t#$FF");
3774 g_defloclabel (label);
3775 AddCodeLine ("\tiny");
3776 AddCodeLine ("\tlda\t(ptr1),y");
3777 AddCodeLine ("\tbne\tL%04X", label);
3778 AddCodeLine ("\ttax");
3779 AddCodeLine ("\ttya");