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", (int)(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", (int)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", (int)(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", (int)label);
1586 AddCodeLine ("\tinc\t%s+1", lbuf);
1587 g_defloclabel (label);
1588 AddCodeLine ("\tldx\t%s+1", lbuf);
1590 AddCodeLine ("\tlda\t#$%02X", (unsigned char)(val >> 8));
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", (int)(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", (int)(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", (int)(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", (int)(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", (int)(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", (int)(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", (unsigned char)(val >> 8));
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", (int)(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", (unsigned char)val);
1830 AddCodeLine ("\tsta\t%s", lbuf);
1832 label = GetLabel ();
1833 AddCodeLine ("\tbcs\tL%04X", (unsigned)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", (unsigned char)(val >> 8));
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", (unsigned char)val);
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", (unsigned char)val);
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", (unsigned char)val);
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", (unsigned char)val);
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", (unsigned char)val);
1994 AddCodeLine ("\tsta\t(ptr1),y");
1995 AddCodeLine ("\tpha");
1996 AddCodeLine ("\tiny");
1997 AddCodeLine ("\tlda\t(ptr1),y");
1998 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)(val >> 8));
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", (unsigned char)val);
2152 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
2153 AddCodeLine ("\tbne\t*+4");
2154 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
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",
2419 (unsigned)(val & 0xFFFF),
2420 (unsigned)(label & 0xFFFF));
2424 AddCodeLine ("\t.dword\t$%08lX", val);
2425 AddCodeLine ("\t.word\tL%04X", label & 0xFFFF);
2436 void g_truejump (unsigned flags, unsigned label)
2437 /* Jump to label if zero flag clear */
2439 if (flags & CF_SHORT) {
2440 AddCodeLine ("\tbne\tL%04X", label);
2442 AddCodeLine ("\tjne\tL%04X", label);
2448 void g_falsejump (unsigned flags, unsigned label)
2449 /* Jump to label if zero flag set */
2451 if (flags & CF_SHORT) {
2452 AddCodeLine ("\tbeq\tL%04X", label);
2454 AddCodeLine ("\tjeq\tL%04X", label);
2460 static void mod_internal (int k, char* verb1, char* verb2)
2463 AddCodeLine ("\tjsr\t%ssp%c", verb1, k + '0');
2467 AddCodeLine ("\tjsr\t%ssp", verb2);
2473 void g_space (int space)
2474 /* Create or drop space on the stack */
2477 mod_internal (-space, "inc", "addy");
2478 } else if (space > 0) {
2479 mod_internal (space, "dec", "suby");
2485 void g_add (unsigned flags, unsigned long val)
2486 /* Primary = TOS + Primary */
2488 static char* ops [12] = {
2489 0, "tosadda0", "tosaddax",
2490 0, "tosadda0", "tosaddax",
2495 if (flags & CF_CONST) {
2496 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2497 g_push (flags & ~CF_CONST, 0);
2499 oper (flags, val, ops);
2504 void g_sub (unsigned flags, unsigned long val)
2505 /* Primary = TOS - Primary */
2507 static char* ops [12] = {
2508 0, "tossuba0", "tossubax",
2509 0, "tossuba0", "tossubax",
2514 if (flags & CF_CONST) {
2515 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2516 g_push (flags & ~CF_CONST, 0);
2518 oper (flags, val, ops);
2523 void g_rsub (unsigned flags, unsigned long val)
2524 /* Primary = Primary - TOS */
2526 static char* ops [12] = {
2527 0, "tosrsuba0", "tosrsubax",
2528 0, "tosrsuba0", "tosrsubax",
2532 oper (flags, val, ops);
2537 void g_mul (unsigned flags, unsigned long val)
2538 /* Primary = TOS * Primary */
2540 static char* ops [12] = {
2541 0, "tosmula0", "tosmulax",
2542 0, "tosumula0", "tosumulax",
2549 /* Do strength reduction if the value is constant and a power of two */
2550 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2551 /* Generate a shift instead */
2556 /* If the right hand side is const, the lhs is not on stack but still
2557 * in the primary register.
2559 if (flags & CF_CONST) {
2561 switch (flags & CF_TYPE) {
2564 if (flags & CF_FORCECHAR) {
2565 /* Handle some special cases */
2569 AddCodeLine ("\tsta\ttmp1");
2570 AddCodeLine ("\tasl\ta");
2571 AddCodeLine ("\tclc");
2572 AddCodeLine ("\tadc\ttmp1");
2576 AddCodeLine ("\tsta\ttmp1");
2577 AddCodeLine ("\tasl\ta");
2578 AddCodeLine ("\tasl\ta");
2579 AddCodeLine ("\tclc");
2580 AddCodeLine ("\tadc\ttmp1");
2584 AddCodeLine ("\tsta\ttmp1");
2585 AddCodeLine ("\tasl\ta");
2586 AddCodeLine ("\tasl\ta");
2587 AddCodeLine ("\tclc");
2588 AddCodeLine ("\tadc\ttmp1");
2589 AddCodeLine ("\tasl\ta");
2605 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2606 * into the normal, non-optimized stuff.
2608 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2609 g_push (flags & ~CF_CONST, 0);
2613 /* Use long way over the stack */
2614 oper (flags, val, ops);
2619 void g_div (unsigned flags, unsigned long val)
2620 /* Primary = TOS / Primary */
2622 static char* ops [12] = {
2623 0, "tosdiva0", "tosdivax",
2624 0, "tosudiva0", "tosudivax",
2629 /* Do strength reduction if the value is constant and a power of two */
2631 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2632 /* Generate a shift instead */
2635 /* Generate a division */
2636 if (flags & CF_CONST) {
2637 /* lhs is not on stack */
2638 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2639 g_push (flags & ~CF_CONST, 0);
2641 oper (flags, val, ops);
2647 void g_mod (unsigned flags, unsigned long val)
2648 /* Primary = TOS % Primary */
2650 static char* ops [12] = {
2651 0, "tosmoda0", "tosmodax",
2652 0, "tosumoda0", "tosumodax",
2658 /* Check if we can do some cost reduction */
2659 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2660 /* We can do that with an AND operation */
2661 g_and (flags, val - 1);
2663 /* Do it the hard way... */
2664 if (flags & CF_CONST) {
2665 /* lhs is not on stack */
2666 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2667 g_push (flags & ~CF_CONST, 0);
2669 oper (flags, val, ops);
2675 void g_or (unsigned flags, unsigned long val)
2676 /* Primary = TOS | Primary */
2678 static char* ops [12] = {
2679 0, "tosora0", "tosorax",
2680 0, "tosora0", "tosorax",
2685 /* If the right hand side is const, the lhs is not on stack but still
2686 * in the primary register.
2688 if (flags & CF_CONST) {
2690 switch (flags & CF_TYPE) {
2693 if (flags & CF_FORCECHAR) {
2694 if ((val & 0xFF) != 0xFF) {
2695 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2703 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2710 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2719 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2720 * into the normal, non-optimized stuff.
2722 g_push (flags & ~CF_CONST, 0);
2726 /* Use long way over the stack */
2727 oper (flags, val, ops);
2732 void g_xor (unsigned flags, unsigned long val)
2733 /* Primary = TOS ^ Primary */
2735 static char* ops [12] = {
2736 0, "tosxora0", "tosxorax",
2737 0, "tosxora0", "tosxorax",
2743 /* If the right hand side is const, the lhs is not on stack but still
2744 * in the primary register.
2746 if (flags & CF_CONST) {
2748 switch (flags & CF_TYPE) {
2751 if (flags & CF_FORCECHAR) {
2752 if ((val & 0xFF) != 0) {
2753 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2762 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2765 } else if ((val & 0xFF) == 0) {
2766 AddCodeLine ("\tpha");
2767 AddCodeLine ("\ttxa");
2768 AddCodeLine ("\teor\t#$%02X", (unsigned char)(val >> 8));
2769 AddCodeLine ("\ttax");
2770 AddCodeLine ("\tpla");
2778 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2788 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2789 * into the normal, non-optimized stuff.
2791 g_push (flags & ~CF_CONST, 0);
2795 /* Use long way over the stack */
2796 oper (flags, val, ops);
2801 void g_and (unsigned flags, unsigned long val)
2802 /* Primary = TOS & Primary */
2804 static char* ops [12] = {
2805 0, "tosanda0", "tosandax",
2806 0, "tosanda0", "tosandax",
2811 /* If the right hand side is const, the lhs is not on stack but still
2812 * in the primary register.
2814 if (flags & CF_CONST) {
2816 switch (flags & CF_TYPE) {
2819 if (flags & CF_FORCECHAR) {
2820 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2825 if ((val & 0xFFFF) != 0xFFFF) {
2830 } else if (val != 0xFF) {
2831 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2833 } else if ((val & 0xFF00) == 0xFF00) {
2834 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2835 } else if ((val & 0x00FF) == 0x0000) {
2836 AddCodeLine ("\ttxa");
2837 AddCodeLine ("\tand\t#$%02X", (unsigned char)(val >> 8));
2838 AddCodeLine ("\ttax");
2841 AddCodeLine ("\ttay");
2842 AddCodeLine ("\ttxa");
2843 AddCodeLine ("\tand\t#$%02X", (unsigned char)(val >> 8));
2844 AddCodeLine ("\ttax");
2845 AddCodeLine ("\ttya");
2846 if ((val & 0x00FF) != 0x00FF) {
2847 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2856 AddCodeLine ("\tstx\tsreg+1");
2857 AddCodeLine ("\tstx\tsreg");
2858 if ((val & 0xFF) != 0xFF) {
2859 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2862 } else if (val == 0xFF00) {
2864 AddCodeLine ("\tsta\tsreg+1");
2865 AddCodeLine ("\tsta\tsreg");
2874 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2875 * into the normal, non-optimized stuff.
2877 g_push (flags & ~CF_CONST, 0);
2881 /* Use long way over the stack */
2882 oper (flags, val, ops);
2887 void g_asr (unsigned flags, unsigned long val)
2888 /* Primary = TOS >> Primary */
2890 static char* ops [12] = {
2891 0, "tosasra0", "tosasrax",
2892 0, "tosshra0", "tosshrax",
2897 /* If the right hand side is const, the lhs is not on stack but still
2898 * in the primary register.
2900 if (flags & CF_CONST) {
2902 switch (flags & CF_TYPE) {
2906 if (val >= 1 && val <= 3) {
2907 if (flags & CF_UNSIGNED) {
2908 AddCodeLine ("\tjsr\tshrax%ld", val);
2910 AddCodeLine ("\tjsr\tasrax%ld", val);
2913 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2914 AddCodeLine ("\ttxa");
2921 if (val >= 1 && val <= 3) {
2922 if (flags & CF_UNSIGNED) {
2923 AddCodeLine ("\tjsr\tshreax%ld", val);
2925 AddCodeLine ("\tjsr\tasreax%ld", val);
2928 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2929 AddCodeLine ("\ttxa");
2930 AddCodeLine ("\tldx\tsreg");
2931 AddCodeLine ("\tldy\tsreg+1");
2932 AddCodeLine ("\tsty\tsreg");
2933 AddCodeLine ("\tldy\t#$00");
2934 AddCodeLine ("\tsty\tsreg+1");
2936 } else if (val == 16) {
2937 AddCodeLine ("\tldy\t#$00");
2938 AddCodeLine ("\tldx\tsreg+1");
2939 if ((flags & CF_UNSIGNED) == 0) {
2940 AddCodeLine ("\tbpl\t*+3");
2941 AddCodeLine ("\tdey");
2942 AddCodeHint ("y:!");
2944 AddCodeLine ("\tlda\tsreg");
2945 AddCodeLine ("\tsty\tsreg+1");
2946 AddCodeLine ("\tsty\tsreg");
2955 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2956 * into the normal, non-optimized stuff.
2958 g_push (flags & ~CF_CONST, 0);
2962 /* Use long way over the stack */
2963 oper (flags, val, ops);
2968 void g_asl (unsigned flags, unsigned long val)
2969 /* Primary = TOS << Primary */
2971 static char* ops [12] = {
2972 0, "tosasla0", "tosaslax",
2973 0, "tosshla0", "tosshlax",
2979 /* If the right hand side is const, the lhs is not on stack but still
2980 * in the primary register.
2982 if (flags & CF_CONST) {
2984 switch (flags & CF_TYPE) {
2988 if (val >= 1 && val <= 3) {
2989 if (flags & CF_UNSIGNED) {
2990 AddCodeLine ("\tjsr\tshlax%ld", val);
2992 AddCodeLine ("\tjsr\taslax%ld", val);
2995 } else if (val == 8) {
2996 AddCodeLine ("\ttax");
2997 AddCodeLine ("\tlda\t#$00");
3003 if (val >= 1 && val <= 3) {
3004 if (flags & CF_UNSIGNED) {
3005 AddCodeLine ("\tjsr\tshleax%ld", val);
3007 AddCodeLine ("\tjsr\tasleax%ld", val);
3010 } else if (val == 8) {
3011 AddCodeLine ("\tldy\tsreg");
3012 AddCodeLine ("\tsty\tsreg+1");
3013 AddCodeLine ("\tstx\tsreg");
3014 AddCodeLine ("\ttax");
3015 AddCodeLine ("\tlda\t#$00");
3017 } else if (val == 16) {
3018 AddCodeLine ("\tstx\tsreg+1");
3019 AddCodeLine ("\tsta\tsreg");
3020 AddCodeLine ("\tlda\t#$00");
3021 AddCodeLine ("\ttax");
3030 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3031 * into the normal, non-optimized stuff.
3033 g_push (flags & ~CF_CONST, 0);
3037 /* Use long way over the stack */
3038 oper (flags, val, ops);
3043 void g_neg (unsigned flags)
3044 /* Primary = -Primary */
3046 switch (flags & CF_TYPE) {
3050 AddCodeLine ("\tjsr\tnegax");
3054 AddCodeLine ("\tjsr\tnegeax");
3064 void g_bneg (unsigned flags)
3065 /* Primary = !Primary */
3067 switch (flags & CF_TYPE) {
3070 AddCodeLine ("\tjsr\tbnega");
3074 AddCodeLine ("\tjsr\tbnegax");
3078 AddCodeLine ("\tjsr\tbnegeax");
3088 void g_com (unsigned flags)
3089 /* Primary = ~Primary */
3091 switch (flags & CF_TYPE) {
3095 AddCodeLine ("\tjsr\tcomplax");
3099 AddCodeLine ("\tjsr\tcompleax");
3109 void g_inc (unsigned flags, unsigned long val)
3110 /* Increment the primary register by a given number */
3112 /* Don't inc by zero */
3117 /* Generate code for the supported types */
3119 switch (flags & CF_TYPE) {
3122 if (flags & CF_FORCECHAR) {
3123 if (CPU == CPU_65C02 && val <= 2) {
3125 AddCodeLine ("\tina");
3128 AddCodeLine ("\tclc");
3129 AddCodeLine ("\tadc\t#$%02X", (unsigned char)val);
3136 if (CPU == CPU_65C02 && val == 1) {
3137 AddCodeLine ("\tina");
3138 AddCodeLine ("\tbne\t*+3");
3139 AddCodeLine ("\tinx");
3140 /* Tell the optimizer that the X register may be invalid */
3141 AddCodeHint ("x:!");
3142 } else if (FavourSize) {
3145 AddCodeLine ("\tjsr\tincax%lu", val);
3146 } else if (val <= 255) {
3148 AddCodeLine ("\tjsr\tincaxy");
3150 g_add (flags | CF_CONST, val);
3153 /* Inline the code */
3155 if ((val & 0xFF) != 0) {
3156 AddCodeLine ("\tclc");
3157 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3158 AddCodeLine ("\tbcc\t*+3");
3159 AddCodeLine ("\tinx");
3160 /* Tell the optimizer that the X register may be invalid */
3161 AddCodeHint ("x:!");
3164 AddCodeLine ("\tinx");
3167 AddCodeLine ("\tinx");
3170 AddCodeLine ("\tclc");
3171 if ((val & 0xFF) != 0) {
3172 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3173 /* Tell the optimizer that the X register may be invalid */
3174 AddCodeHint ("x:!");
3176 AddCodeLine ("\tpha");
3177 AddCodeLine ("\ttxa");
3178 AddCodeLine ("\tadc\t#$%02X", (unsigned char) (val >> 8));
3179 AddCodeLine ("\ttax");
3180 AddCodeLine ("\tpla");
3188 AddCodeLine ("\tjsr\tinceaxy");
3190 g_add (flags | CF_CONST, val);
3202 void g_dec (unsigned flags, unsigned long val)
3203 /* Decrement the primary register by a given number */
3205 /* Generate code for the supported types */
3207 switch (flags & CF_TYPE) {
3210 if (flags & CF_FORCECHAR) {
3211 if (CPU == CPU_65C02 && val <= 2) {
3213 AddCodeLine ("\tdea");
3216 AddCodeLine ("\tsec");
3217 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
3225 AddCodeLine ("\tjsr\tdecax%d", (int) val);
3226 } else if (val <= 255) {
3228 AddCodeLine ("\tjsr\tdecaxy");
3230 g_sub (flags | CF_CONST, val);
3237 AddCodeLine ("\tjsr\tdeceaxy");
3239 g_sub (flags | CF_CONST, val);
3252 * Following are the conditional operators. They compare the TOS against
3253 * the primary and put a literal 1 in the primary if the condition is
3254 * true, otherwise they clear the primary register
3259 void g_eq (unsigned flags, unsigned long val)
3260 /* Test for equal */
3262 static char* ops [12] = {
3263 "toseq00", "toseqa0", "toseqax",
3264 "toseq00", "toseqa0", "toseqax",
3269 /* If the right hand side is const, the lhs is not on stack but still
3270 * in the primary register.
3272 if (flags & CF_CONST) {
3274 switch (flags & CF_TYPE) {
3277 if (flags & CF_FORCECHAR) {
3278 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3279 AddCodeLine ("\tjsr\tbooleq");
3285 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3286 AddCodeLine ("\tbne\t*+4");
3287 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3288 AddCodeLine ("\tjsr\tbooleq");
3298 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3299 * into the normal, non-optimized stuff.
3301 g_push (flags & ~CF_CONST, 0);
3305 /* Use long way over the stack */
3306 oper (flags, val, ops);
3311 void g_ne (unsigned flags, unsigned long val)
3312 /* Test for not equal */
3314 static char* ops [12] = {
3315 "tosne00", "tosnea0", "tosneax",
3316 "tosne00", "tosnea0", "tosneax",
3322 /* If the right hand side is const, the lhs is not on stack but still
3323 * in the primary register.
3325 if (flags & CF_CONST) {
3327 switch (flags & CF_TYPE) {
3330 if (flags & CF_FORCECHAR) {
3331 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3332 AddCodeLine ("\tjsr\tboolne");
3338 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3339 AddCodeLine ("\tbne\t*+4");
3340 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3341 AddCodeLine ("\tjsr\tboolne");
3351 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3352 * into the normal, non-optimized stuff.
3354 g_push (flags & ~CF_CONST, 0);
3358 /* Use long way over the stack */
3359 oper (flags, val, ops);
3364 void g_lt (unsigned flags, unsigned long val)
3365 /* Test for less than */
3367 static char* ops [12] = {
3368 "toslt00", "toslta0", "tosltax",
3369 "tosult00", "tosulta0", "tosultax",
3374 /* If the right hand side is const, the lhs is not on stack but still
3375 * in the primary register.
3377 if (flags & CF_CONST) {
3379 /* Give a warning in some special cases */
3380 if ((flags & CF_UNSIGNED) && val == 0) {
3381 Warning (WARN_COND_NEVER_TRUE);
3384 /* Look at the type */
3385 switch (flags & CF_TYPE) {
3388 if (flags & CF_FORCECHAR) {
3389 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3390 if (flags & CF_UNSIGNED) {
3391 AddCodeLine ("\tjsr\tboolult");
3393 AddCodeLine ("\tjsr\tboollt");
3400 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3401 /* If we have a signed compare against zero, we only need to
3402 * test the high byte.
3404 AddCodeLine ("\ttxa");
3405 AddCodeLine ("\tjsr\tboollt");
3408 /* Direct code only for unsigned data types */
3409 if (flags & CF_UNSIGNED) {
3410 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3411 AddCodeLine ("\tbne\t*+4");
3412 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3413 AddCodeLine ("\tjsr\tboolult");
3425 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3426 * into the normal, non-optimized stuff.
3428 g_push (flags & ~CF_CONST, 0);
3432 /* Use long way over the stack */
3433 oper (flags, val, ops);
3438 void g_le (unsigned flags, unsigned long val)
3439 /* Test for less than or equal to */
3441 static char* ops [12] = {
3442 "tosle00", "toslea0", "tosleax",
3443 "tosule00", "tosulea0", "tosuleax",
3449 /* If the right hand side is const, the lhs is not on stack but still
3450 * in the primary register.
3452 if (flags & CF_CONST) {
3454 /* Look at the type */
3455 switch (flags & CF_TYPE) {
3458 if (flags & CF_FORCECHAR) {
3459 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3460 if (flags & CF_UNSIGNED) {
3461 AddCodeLine ("\tjsr\tboolule");
3463 AddCodeLine ("\tjsr\tboolle");
3470 if (flags & CF_UNSIGNED) {
3471 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3472 AddCodeLine ("\tbne\t*+4");
3473 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3474 AddCodeLine ("\tjsr\tboolule");
3486 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3487 * into the normal, non-optimized stuff.
3489 g_push (flags & ~CF_CONST, 0);
3493 /* Use long way over the stack */
3494 oper (flags, val, ops);
3499 void g_gt (unsigned flags, unsigned long val)
3500 /* Test for greater than */
3502 static char* ops [12] = {
3503 "tosgt00", "tosgta0", "tosgtax",
3504 "tosugt00", "tosugta0", "tosugtax",
3510 /* If the right hand side is const, the lhs is not on stack but still
3511 * in the primary register.
3513 if (flags & CF_CONST) {
3515 /* Look at the type */
3516 switch (flags & CF_TYPE) {
3519 if (flags & CF_FORCECHAR) {
3520 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3521 if (flags & CF_UNSIGNED) {
3522 /* If we have a compare > 0, we will replace it by
3523 * != 0 here, since both are identical but the latter
3524 * is easier to optimize.
3527 AddCodeLine ("\tjsr\tboolugt");
3529 AddCodeLine ("\tjsr\tboolne");
3532 AddCodeLine ("\tjsr\tboolgt");
3539 if (flags & CF_UNSIGNED) {
3540 /* If we have a compare > 0, we will replace it by
3541 * != 0 here, since both are identical but the latter
3542 * is easier to optimize.
3544 if ((val & 0xFFFF) == 0) {
3545 AddCodeLine ("\tstx\ttmp1");
3546 AddCodeLine ("\tora\ttmp1");
3547 AddCodeLine ("\tjsr\tboolne");
3549 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3550 AddCodeLine ("\tbne\t*+4");
3551 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3552 AddCodeLine ("\tjsr\tboolugt");
3565 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3566 * into the normal, non-optimized stuff.
3568 g_push (flags & ~CF_CONST, 0);
3572 /* Use long way over the stack */
3573 oper (flags, val, ops);
3578 void g_ge (unsigned flags, unsigned long val)
3579 /* Test for greater than or equal to */
3581 static char* ops [12] = {
3582 "tosge00", "tosgea0", "tosgeax",
3583 "tosuge00", "tosugea0", "tosugeax",
3589 /* If the right hand side is const, the lhs is not on stack but still
3590 * in the primary register.
3592 if (flags & CF_CONST) {
3594 /* Give a warning in some special cases */
3595 if ((flags & CF_UNSIGNED) && val == 0) {
3596 Warning (WARN_COND_ALWAYS_TRUE);
3599 /* Look at the type */
3600 switch (flags & CF_TYPE) {
3603 if (flags & CF_FORCECHAR) {
3604 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3605 if (flags & CF_UNSIGNED) {
3606 AddCodeLine ("\tjsr\tbooluge");
3608 AddCodeLine ("\tjsr\tboolge");
3615 if (flags & CF_UNSIGNED) {
3616 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3617 AddCodeLine ("\tbne\t*+4");
3618 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3619 AddCodeLine ("\tjsr\tbooluge");
3631 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3632 * into the normal, non-optimized stuff.
3634 g_push (flags & ~CF_CONST, 0);
3638 /* Use long way over the stack */
3639 oper (flags, val, ops);
3644 /*****************************************************************************/
3645 /* Allocating static storage */
3646 /*****************************************************************************/
3650 void g_res (unsigned n)
3651 /* Reserve static storage, n bytes */
3653 AddCodeLine ("\t.res\t%u,$00", n);
3658 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3659 /* Define data with the size given in flags */
3661 if (flags & CF_CONST) {
3663 /* Numeric constant */
3664 switch (flags & CF_TYPE) {
3667 AddCodeLine ("\t.byte\t$%02lX", val & 0xFF);
3671 AddCodeLine ("\t.word\t$%04lX", val & 0xFFFF);
3675 AddCodeLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3686 /* Create the correct label name */
3687 const char* Label = GetLabelName (flags, val, offs);
3689 /* Labels are always 16 bit */
3690 AddCodeLine ("\t.word\t%s", Label);
3697 void g_defbytes (const unsigned char* Bytes, unsigned Count)
3698 /* Output a row of bytes as a constant */
3704 /* Output the stuff */
3707 /* How many go into this line? */
3708 if ((Chunk = Count) > 16) {
3713 /* Output one line */
3714 strcpy (Buf, "\t.byte\t");
3717 B += sprintf (B, "$%02X", *Bytes++ & 0xFF);
3723 /* Output the line */
3730 void g_zerobytes (unsigned n)
3731 /* Output n bytes of data initialized with zero */
3733 AddCodeLine ("\t.res\t%u,$00", n);
3738 /*****************************************************************************/
3739 /* Inlined known functions */
3740 /*****************************************************************************/
3744 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3745 /* Inline the strlen() function */
3747 /* We need a label in both cases */
3748 unsigned label = GetLabel ();
3750 /* Two different encodings */
3751 if (flags & CF_CONST) {
3753 /* The address of the string is constant. Create the correct label name */
3754 char* lbuf = GetLabelName (flags, val, offs);
3756 /* Generate the strlen code */
3757 AddCodeLine ("\tldy\t#$FF");
3758 g_defloclabel (label);
3759 AddCodeLine ("\tiny");
3760 AddCodeLine ("\tlda\t%s,y", lbuf);
3761 AddCodeLine ("\tbne\tL%04X", label);
3762 AddCodeLine ("\ttax");
3763 AddCodeLine ("\ttya");
3767 /* Address not constant but in primary */
3769 /* This is too much code, so call strlen instead of inlining */
3770 AddCodeLine ("\tjsr\t_strlen");
3772 /* Inline the function */
3773 AddCodeLine ("\tsta\tptr1");
3774 AddCodeLine ("\tstx\tptr1+1");
3775 AddCodeLine ("\tldy\t#$FF");
3776 g_defloclabel (label);
3777 AddCodeLine ("\tiny");
3778 AddCodeLine ("\tlda\t(ptr1),y");
3779 AddCodeLine ("\tbne\tL%04X", label);
3780 AddCodeLine ("\ttax");
3781 AddCodeLine ("\ttya");