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"
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 /* Allow auto import for runtime library routines */
161 AddCodeLine (".autoimport\ton");
163 /* Switch the assembler into case sensible mode */
164 AddCodeLine (".case\t\ton");
166 /* Tell the assembler if we want to generate debug info */
167 AddCodeLine (".debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
169 /* Import the stack pointer for direct auto variable access */
170 AddCodeLine (".importzp\tsp, sreg, regsave, regbank, tmp1, ptr1");
172 /* Define long branch macros */
173 AddCodeLine (".macpack\tlongbranch");
176 /* Define the ldax macro */
177 AddCodeLine (".macro ldax Value");
178 AddCodeLine (" lda #<(Value)");
179 AddCodeLine (" ldx #>(Value)");
180 AddCodeLine (".endmacro");
183 /* Define the default names for the segments */
184 SegmentNames [SEG_CODE] = xstrdup ("CODE");
185 SegmentNames [SEG_RODATA] = xstrdup ("RODATA");
186 SegmentNames [SEG_DATA] = xstrdup ("DATA");
187 SegmentNames [SEG_BSS] = xstrdup ("BSS");
189 /* Tell the optimizer that this is the end of the preamble */
190 AddCodeHint ("end_of_preamble");
195 void g_postamble (void)
196 /* Generate assembler code postamble */
198 /* Tell the optimizer that this is the start of the postamble */
199 AddCodeHint ("start_of_postamble");
204 /*****************************************************************************/
205 /* Segment support */
206 /*****************************************************************************/
210 static void UseSeg (int NewSeg)
211 /* Switch to a specific segment */
213 if (CurSeg != NewSeg) {
215 AddCodeLine (".segment\t\"%s\"", SegmentNames [CurSeg]);
216 AddCodeHint (SegmentHints [CurSeg]);
222 void g_usecode (void)
223 /* Switch to the code segment */
230 void g_userodata (void)
231 /* Switch to the read only data segment */
238 void g_usedata (void)
239 /* Switch to the data segment */
247 /* Switch to the bss segment */
254 static void SegName (int Seg, const char* Name)
255 /* Set the name of a segment */
257 /* Free the old name and set a new one */
258 xfree (SegmentNames [Seg]);
259 SegmentNames [Seg] = xstrdup (Name);
261 /* If the new segment is the current segment, emit a segment directive
265 CurSeg = SEG_INV; /* Invalidate */
272 void g_codename (const char* Name)
273 /* Set the name of the CODE segment */
275 SegName (SEG_CODE, Name);
280 void g_rodataname (const char* Name)
281 /* Set the name of the RODATA segment */
283 SegName (SEG_RODATA, Name);
288 void g_dataname (const char* Name)
289 /* Set the name of the DATA segment */
291 SegName (SEG_DATA, Name);
296 void g_bssname (const char* Name)
297 /* Set the name of the BSS segment */
299 SegName (SEG_BSS, Name);
304 /*****************************************************************************/
306 /*****************************************************************************/
310 unsigned sizeofarg (unsigned flags)
311 /* Return the size of a function argument type that is encoded in flags */
313 switch (flags & CF_TYPE) {
316 return (flags & CF_FORCECHAR)? 1 : 2;
333 int pop (unsigned flags)
334 /* Pop an argument of the given size */
336 return oursp += sizeofarg (flags);
341 int push (unsigned flags)
342 /* Push an argument of the given size */
344 return oursp -= sizeofarg (flags);
349 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
350 /* The value in Offs is an offset to an address in a/x. Make sure, an object
351 * of the type given in Flags can be loaded or stored into this address by
352 * adding part of the offset to the address in ax, so that the remaining
353 * offset fits into an index register. Return the remaining offset.
356 /* If the offset is too large for a byte register, add the high byte
357 * of the offset to the primary. Beware: We need a special correction
358 * if the offset in the low byte will overflow in the operation.
360 unsigned O = Offs & ~0xFFU;
361 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
362 /* We need to add the low byte also */
366 /* Do the correction if we need one */
368 g_inc (CF_INT | CF_CONST, O);
372 /* Return the new offset */
378 /*****************************************************************************/
379 /* Functions handling local labels */
380 /*****************************************************************************/
384 void g_defloclabel (unsigned label)
385 /* Define a local label */
387 AddCodeLine ("L%04X:", label & 0xFFFF);
392 /*****************************************************************************/
393 /* Functions handling global labels */
394 /*****************************************************************************/
398 void g_defgloblabel (const char* Name)
399 /* Define a global label with the given name */
401 AddCodeLine ("_%s:", Name);
406 void g_defexport (const char* Name, int ZP)
407 /* Export the given label */
410 AddCodeLine ("\t.exportzp\t_%s", Name);
412 AddCodeLine ("\t.export\t\t_%s", Name);
418 void g_defimport (const char* Name, int ZP)
419 /* Import the given label */
422 AddCodeLine ("\t.importzp\t_%s", Name);
424 AddCodeLine ("\t.import\t\t_%s", Name);
430 /*****************************************************************************/
431 /* Load functions for various registers */
432 /*****************************************************************************/
436 static void ldaconst (unsigned val)
437 /* Load a with a constant */
439 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
444 static void ldxconst (unsigned val)
445 /* Load x with a constant */
447 AddCodeLine ("\tldx\t#$%02X", val & 0xFF);
452 static void ldyconst (unsigned val)
453 /* Load y with a constant */
455 AddCodeLine ("\tldy\t#$%02X", val & 0xFF);
460 /*****************************************************************************/
461 /* Function entry and exit */
462 /*****************************************************************************/
466 /* Remember the argument size of a function. The variable is set by g_enter
467 * and used by g_leave. If the functions gets its argument size by the caller
468 * (variable param list or function without prototype), g_enter will set the
474 void g_enter (unsigned flags, unsigned argsize)
475 /* Function prologue */
477 if ((flags & CF_FIXARGC) != 0) {
478 /* Just remember the argument size for the leave */
482 AddCodeLine ("\tjsr\tenter");
488 void g_leave (int flags, int val)
489 /* Function epilogue */
494 /* How many bytes of locals do we have to drop? */
497 /* If we didn't have a variable argument list, don't call leave */
500 /* Load a function return code if needed */
501 if ((flags & CF_CONST) != 0) {
502 g_getimmed (flags, val, 0);
505 /* Drop stackframe or leave with rts */
508 AddCodeLine ("\trts");
510 AddCodeLine ("\tjmp\tincsp%d", k);
514 AddCodeLine ("\tjmp\taddysp");
519 strcpy (buf, "\tjmp\tleave");
521 /* We've a stack frame to drop */
525 if (flags & CF_CONST) {
526 if ((flags & CF_TYPE) != CF_LONG) {
527 /* Constant int sized value given for return code */
529 /* Special case: return 0 */
531 } else if (((val >> 8) & 0xFF) == 0) {
532 /* Special case: constant with high byte zero */
533 ldaconst (val); /* Load low byte */
536 /* Others: arbitrary constant value */
537 g_getimmed (flags, val, 0); /* Load value */
540 /* Constant long value: No shortcut possible */
541 g_getimmed (flags, val, 0);
545 /* Output the jump */
549 /* Add an empty line after a function to make the code more readable */
555 /*****************************************************************************/
556 /* Register variables */
557 /*****************************************************************************/
561 void g_save_regvars (int RegOffs, unsigned Bytes)
562 /* Save register variables */
564 /* Don't loop for up to two bytes */
567 AddCodeLine ("\tlda\tregbank%+d", RegOffs);
568 AddCodeLine ("\tjsr\tpusha");
570 } else if (Bytes == 2) {
572 AddCodeLine ("\tlda\tregbank%+d", RegOffs);
573 AddCodeLine ("\tldx\tregbank%+d", RegOffs+1);
574 AddCodeLine ("\tjsr\tpushax");
578 /* More than two bytes - loop */
579 unsigned Label = GetLabel ();
581 ldyconst (Bytes - 1);
583 g_defloclabel (Label);
584 AddCodeLine ("\tlda\tregbank%+d,x", RegOffs-1);
585 AddCodeLine ("\tsta\t(sp),y");
586 AddCodeLine ("\tdey");
587 AddCodeLine ("\tdex");
588 AddCodeLine ("\tbne\tL%04X", Label);
592 /* We pushed stuff, correct the stack pointer */
598 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
599 /* Restore register variables */
601 /* Calculate the actual stack offset and check it */
603 CheckLocalOffs (StackOffs);
605 /* Don't loop for up to two bytes */
608 ldyconst (StackOffs);
609 AddCodeLine ("\tlda\t(sp),y");
610 AddCodeLine ("\tsta\tregbank%+d", RegOffs);
612 } else if (Bytes == 2) {
614 ldyconst (StackOffs);
615 AddCodeLine ("\tlda\t(sp),y");
616 AddCodeLine ("\tsta\tregbank%+d", RegOffs);
617 AddCodeLine ("\tiny");
618 AddCodeLine ("\tlda\t(sp),y");
619 AddCodeLine ("\tsta\tregbank%+d", RegOffs+1);
623 /* More than two bytes - loop */
624 unsigned Label = GetLabel ();
625 ldyconst (StackOffs+Bytes-1);
627 g_defloclabel (Label);
628 AddCodeLine ("\tlda\t(sp),y");
629 AddCodeLine ("\tsta\tregbank%+d,x", RegOffs-1);
630 AddCodeLine ("\tdey");
631 AddCodeLine ("\tdex");
632 AddCodeLine ("\tbne\tL%04X", Label);
639 /*****************************************************************************/
640 /* Fetching memory cells */
641 /*****************************************************************************/
645 void g_getimmed (unsigned flags, unsigned long val, unsigned offs)
646 /* Load a constant into the primary register */
648 if ((flags & CF_CONST) != 0) {
650 /* Numeric constant */
651 switch (flags & CF_TYPE) {
654 if ((flags & CF_FORCECHAR) != 0) {
660 ldxconst ((val >> 8) & 0xFF);
661 ldaconst (val & 0xFF);
666 AddCodeLine ("\tldx\t#$00");
667 AddCodeLine ("\tstx\tsreg+1");
668 AddCodeLine ("\tstx\tsreg");
669 AddCodeLine ("\tlda\t#$%02X", (unsigned char) val);
670 } else if ((val & 0xFFFF00FF) == 0) {
671 AddCodeLine ("\tlda\t#$00");
672 AddCodeLine ("\tsta\tsreg+1");
673 AddCodeLine ("\tsta\tsreg");
674 AddCodeLine ("\tldx\t#$%02X", (unsigned char) (val >> 8));
675 } else if ((val & 0xFFFF0000) == 0 && FavourSize == 0) {
676 AddCodeLine ("\tlda\t#$00");
677 AddCodeLine ("\tsta\tsreg+1");
678 AddCodeLine ("\tsta\tsreg");
679 AddCodeLine ("\tlda\t#$%02X", (unsigned char) val);
680 AddCodeLine ("\tldx\t#$%02X", (unsigned char) (val >> 8));
681 } else if ((val & 0xFFFFFF00) == 0xFFFFFF00) {
682 AddCodeLine ("\tldx\t#$FF");
683 AddCodeLine ("\tstx\tsreg+1");
684 AddCodeLine ("\tstx\tsreg");
685 if ((val & 0xFF) == 0xFF) {
686 AddCodeLine ("\ttxa");
688 AddCodeLine ("\tlda\t#$%02X", (unsigned char) val);
690 } else if ((val & 0xFFFF00FF) == 0xFFFF00FF) {
691 AddCodeLine ("\tlda\t#$FF");
692 AddCodeLine ("\tsta\tsreg+1");
693 AddCodeLine ("\tsta\tsreg");
694 AddCodeLine ("\tldx\t#$%02X", (unsigned char) (val >> 8));
696 /* Call a subroutine that will load following value */
697 AddCodeLine ("\tjsr\tldeax");
698 AddCodeLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
710 /* Some sort of label */
711 const char* Label = GetLabelName (flags, val, offs);
713 /* Load the address into the primary */
714 AddCodeLine ("\tldax\t%s", Label);
721 void g_getstatic (unsigned flags, unsigned long label, unsigned offs)
722 /* Fetch an static memory cell into the primary register */
724 /* Create the correct label name */
725 char* lbuf = GetLabelName (flags, label, offs);
727 /* Check the size and generate the correct load operation */
728 switch (flags & CF_TYPE) {
731 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
732 AddCodeLine ("\tlda\t%s", lbuf); /* load A from the label */
735 AddCodeLine ("\tlda\t%s", lbuf); /* load A from the label */
736 if (!(flags & CF_UNSIGNED)) {
737 /* Must sign extend */
738 AddCodeLine ("\tbpl\t*+3");
739 AddCodeLine ("\tdex");
740 AddCodeHint ("x:!"); /* X is invalid now */
746 AddCodeLine ("\tlda\t%s", lbuf);
747 if (flags & CF_TEST) {
748 AddCodeLine ("\tora\t%s+1", lbuf);
750 AddCodeLine ("\tldx\t%s+1", lbuf);
755 if (flags & CF_TEST) {
756 AddCodeLine ("\tlda\t%s+3", lbuf);
757 AddCodeLine ("\tora\t%s+2", lbuf);
758 AddCodeLine ("\tora\t%s+1", lbuf);
759 AddCodeLine ("\tora\t%s+0", lbuf);
761 AddCodeLine ("\tlda\t%s+3", lbuf);
762 AddCodeLine ("\tsta\tsreg+1");
763 AddCodeLine ("\tlda\t%s+2", lbuf);
764 AddCodeLine ("\tsta\tsreg");
765 AddCodeLine ("\tldx\t%s+1", lbuf);
766 AddCodeLine ("\tlda\t%s", lbuf);
778 void g_getlocal (unsigned flags, int offs)
779 /* Fetch specified local object (local var). */
782 CheckLocalOffs (offs);
783 switch (flags & CF_TYPE) {
786 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
788 AddCodeLine ("\tlda\t(sp),y");
791 AddCodeLine ("\tldx\t#$00");
792 AddCodeLine ("\tlda\t(sp,x)");
795 AddCodeLine ("\tldx\t#$00");
796 AddCodeLine ("\tlda\t(sp),y");
798 if ((flags & CF_UNSIGNED) == 0) {
799 AddCodeLine ("\tbpl\t*+3");
800 AddCodeLine ("\tdex");
801 AddCodeHint ("x:!"); /* X is invalid now */
807 CheckLocalOffs (offs + 1);
808 if (flags & CF_TEST) {
810 AddCodeLine ("\tlda\t(sp),y");
811 AddCodeLine ("\tdey");
812 AddCodeLine ("\tora\t(sp),y");
817 AddCodeLine ("\tjsr\tldaxysp");
819 AddCodeLine ("\tjsr\tldax0sp");
823 AddCodeLine ("\tlda\t(sp),y");
824 AddCodeLine ("\ttax");
825 AddCodeLine ("\tdey");
826 AddCodeLine ("\tlda\t(sp),y");
834 AddCodeLine ("\tjsr\tldeaxysp");
836 AddCodeLine ("\tjsr\tldeax0sp");
847 void g_getind (unsigned flags, unsigned offs)
848 /* Fetch the specified object type indirect through the primary register
849 * into the primary register
852 /* If the offset is greater than 255, add the part that is > 255 to
853 * the primary. This way we get an easy addition and use the low byte
856 offs = MakeByteOffs (flags, offs);
858 /* Handle the indirect fetch */
859 switch (flags & CF_TYPE) {
862 /* Character sized */
865 if (flags & CF_UNSIGNED) {
866 AddCodeLine ("\tjsr\tldauidx");
868 AddCodeLine ("\tjsr\tldaidx");
871 if (flags & CF_UNSIGNED) {
873 AddCodeLine ("\tjsr\tldaui");
875 AddCodeLine ("\tsta\tptr1");
876 AddCodeLine ("\tstx\tptr1+1");
877 AddCodeLine ("\tldx\t#$00");
878 AddCodeLine ("\tlda\t(ptr1,x)");
881 AddCodeLine ("\tjsr\tldai");
887 if (flags & CF_TEST) {
889 AddCodeLine ("\tsta\tptr1");
890 AddCodeLine ("\tstx\tptr1+1");
891 AddCodeLine ("\tlda\t(ptr1),y");
892 AddCodeLine ("\tiny");
893 AddCodeLine ("\tora\t(ptr1),y");
896 AddCodeLine ("\tjsr\tldaxi");
899 AddCodeLine ("\tjsr\tldaxidx");
906 AddCodeLine ("\tjsr\tldeaxi");
909 AddCodeLine ("\tjsr\tldeaxidx");
911 if (flags & CF_TEST) {
912 AddCodeLine ("\tjsr\ttsteax");
924 void g_leasp (int offs)
925 /* Fetch the address of the specified symbol into the primary register */
927 /* Calculate the offset relative to sp */
930 /* For value 0 we do direct code */
932 AddCodeLine ("\tlda\tsp");
933 AddCodeLine ("\tldx\tsp+1");
936 ldaconst (offs); /* Load A with offset value */
937 AddCodeLine ("\tjsr\tleaasp"); /* Load effective address */
940 AddCodeLine ("\tclc");
941 AddCodeLine ("\tldx\tsp+1");
942 AddCodeLine ("\tadc\tsp");
943 AddCodeLine ("\tbcc\t*+3");
944 AddCodeLine ("\tinx");
945 AddCodeHint ("x:!"); /* Invalidate X */
952 /*****************************************************************************/
953 /* Store into memory */
954 /*****************************************************************************/
958 void g_putstatic (unsigned flags, unsigned long label, unsigned offs)
959 /* Store the primary register into the specified static memory cell */
961 /* Create the correct label name */
962 char* lbuf = GetLabelName (flags, label, offs);
964 /* Check the size and generate the correct store operation */
965 switch (flags & CF_TYPE) {
968 AddCodeLine ("\tsta\t%s", lbuf);
972 AddCodeLine ("\tsta\t%s", lbuf);
973 AddCodeLine ("\tstx\t%s+1", lbuf);
977 AddCodeLine ("\tsta\t%s", lbuf);
978 AddCodeLine ("\tstx\t%s+1", lbuf);
979 AddCodeLine ("\tldy\tsreg");
980 AddCodeLine ("\tsty\t%s+2", lbuf);
981 AddCodeLine ("\tldy\tsreg+1");
982 AddCodeLine ("\tsty\t%s+3", lbuf);
993 void g_putlocal (unsigned flags, int offs)
994 /* Put data into local object. */
997 CheckLocalOffs (offs);
998 switch (flags & CF_TYPE) {
1002 AddCodeLine ("\tsta\t(sp),y");
1008 AddCodeLine ("\tjsr\tstaxysp");
1010 AddCodeLine ("\tjsr\tstax0sp");
1017 AddCodeLine ("\tjsr\tsteaxysp");
1019 AddCodeLine ("\tjsr\tsteax0sp");
1031 void g_putind (unsigned flags, unsigned offs)
1032 /* Store the specified object type in the primary register at the address
1033 * on the top of the stack
1036 /* We cannot currently handle more than byte sized offsets */
1037 if (offs > 256 - sizeofarg (flags)) {
1038 Internal ("g_putind: Large offsets not implemented");
1041 /* Check the size and determine operation */
1042 switch (flags & CF_TYPE) {
1047 AddCodeLine ("\tjsr\tstaspidx");
1049 AddCodeLine ("\tjsr\tstaspp");
1056 AddCodeLine ("\tjsr\tstaxspidx");
1058 AddCodeLine ("\tjsr\tstaxspp");
1065 AddCodeLine ("\tjsr\tsteaxspidx");
1067 AddCodeLine ("\tjsr\tsteaxspp");
1076 /* Pop the argument which is always a pointer */
1082 /*****************************************************************************/
1083 /* type conversion and similiar stuff */
1084 /*****************************************************************************/
1088 void g_toslong (unsigned flags)
1089 /* Make sure, the value on TOS is a long. Convert if necessary */
1091 switch (flags & CF_TYPE) {
1095 if (flags & CF_UNSIGNED) {
1096 AddCodeLine ("\tjsr\ttosulong");
1098 AddCodeLine ("\tjsr\ttoslong");
1113 void g_tosint (unsigned flags)
1114 /* Make sure, the value on TOS is an int. Convert if necessary */
1116 switch (flags & CF_TYPE) {
1123 AddCodeLine ("\tjsr\ttosint");
1134 void g_reglong (unsigned flags)
1135 /* Make sure, the value in the primary register a long. Convert if necessary */
1137 switch (flags & CF_TYPE) {
1141 if (flags & CF_UNSIGNED) {
1143 AddCodeLine ("\tjsr\taxulong");
1146 AddCodeLine ("\tsty\tsreg");
1147 AddCodeLine ("\tsty\tsreg+1");
1150 AddCodeLine ("\tjsr\taxlong");
1164 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1165 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1166 * value, that corresponds to the value on TOS, rhs corresponds to the value
1167 * in (e)ax. The return value is the the flags value for the resulting type.
1170 unsigned ltype, rtype;
1173 /* Get the type spec from the flags */
1174 ltype = lhs & CF_TYPE;
1175 rtype = rhs & CF_TYPE;
1177 /* Check if a conversion is needed */
1178 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1179 /* We must promote the primary register to long */
1181 /* Get the new rhs type */
1182 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1184 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1185 /* We must promote the lhs to long */
1191 /* Get the new rhs type */
1192 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1196 /* Determine the result type for the operation:
1197 * - The result is const if both operands are const.
1198 * - The result is unsigned if one of the operands is unsigned.
1199 * - The result is long if one of the operands is long.
1200 * - Otherwise the result is int sized.
1202 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1203 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1204 if (rtype == CF_LONG || ltype == CF_LONG) {
1214 unsigned g_typecast (unsigned lhs, unsigned rhs)
1215 /* Cast the value in the primary register to the operand size that is flagged
1216 * by the lhs value. Return the result value.
1219 unsigned ltype, rtype;
1221 /* Get the type spec from the flags */
1222 ltype = lhs & CF_TYPE;
1223 rtype = rhs & CF_TYPE;
1225 /* Check if a conversion is needed */
1226 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1227 /* We must promote the primary register to long */
1231 /* Do not need any other action. If the left type is int, and the primary
1232 * register is long, it will be automagically truncated. If the right hand
1233 * side is const, it is not located in the primary register and handled by
1234 * the expression parser code.
1237 /* Result is const if the right hand side was const */
1238 lhs |= (rhs & CF_CONST);
1240 /* The resulting type is that of the left hand side (that's why you called
1248 void g_scale (unsigned flags, long val)
1249 /* Scale the value in the primary register by the given value. If val is positive,
1250 * scale up, is val is negative, scale down. This function is used to scale
1251 * the operands or results of pointer arithmetic by the size of the type, the
1252 * pointer points to.
1257 /* Value may not be zero */
1259 Internal ("Data type has no size");
1260 } else if (val > 0) {
1263 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1265 /* Factor is 2, 4 or 8, use special function */
1266 switch (flags & CF_TYPE) {
1269 if (flags & CF_FORCECHAR) {
1271 AddCodeLine ("\tasl\ta");
1278 if (FavourSize || p2 >= 3) {
1279 if (flags & CF_UNSIGNED) {
1280 AddCodeLine ("\tjsr\tshlax%d", p2);
1282 AddCodeLine ("\tjsr\taslax%d", p2);
1285 AddCodeLine ("\tstx\ttmp1");
1287 AddCodeLine ("\tasl\ta");
1288 AddCodeLine ("\trol\ttmp1");
1290 AddCodeLine ("\tldx\ttmp1");
1295 if (flags & CF_UNSIGNED) {
1296 AddCodeLine ("\tjsr\tshleax%d", p2);
1298 AddCodeLine ("\tjsr\tasleax%d", p2);
1307 } else if (val != 1) {
1309 /* Use a multiplication instead */
1310 g_mul (flags | CF_CONST, val);
1318 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1320 /* Factor is 2, 4 or 8, use special function */
1321 switch (flags & CF_TYPE) {
1324 if (flags & CF_FORCECHAR) {
1325 if (flags & CF_UNSIGNED) {
1327 AddCodeLine ("\tlsr\ta");
1330 } else if (p2 <= 2) {
1331 AddCodeLine ("\tcmp\t#$80");
1332 AddCodeLine ("\tror\ta");
1339 if (flags & CF_UNSIGNED) {
1340 if (FavourSize || p2 >= 3) {
1341 AddCodeLine ("\tjsr\tlsrax%d", p2);
1343 AddCodeLine ("\tstx\ttmp1");
1345 AddCodeLine ("\tlsr\ttmp1");
1346 AddCodeLine ("\tror\ta");
1348 AddCodeLine ("\tldx\ttmp1");
1351 if (FavourSize || p2 >= 3) {
1352 AddCodeLine ("\tjsr\tasrax%d", p2);
1354 AddCodeLine ("\tstx\ttmp1");
1356 AddCodeLine ("\tcpx\t#$80");
1357 AddCodeLine ("\tror\ttmp1");
1358 AddCodeLine ("\tror\ta");
1360 AddCodeLine ("\tldx\ttmp1");
1366 if (flags & CF_UNSIGNED) {
1367 AddCodeLine ("\tjsr\tlsreax%d", p2);
1369 AddCodeLine ("\tjsr\tasreax%d", p2);
1378 } else if (val != 1) {
1380 /* Use a division instead */
1381 g_div (flags | CF_CONST, val);
1389 /*****************************************************************************/
1390 /* Adds and subs of variables fix a fixed address */
1391 /*****************************************************************************/
1395 void g_addlocal (unsigned flags, int offs)
1396 /* Add a local variable to ax */
1398 /* Correct the offset and check it */
1400 CheckLocalOffs (offs);
1402 switch (flags & CF_TYPE) {
1405 AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1406 AddCodeLine ("\tclc");
1407 AddCodeLine ("\tadc\t(sp),y");
1408 AddCodeLine ("\tbcc\t*+3");
1409 AddCodeLine ("\tinx");
1410 AddCodeHint ("x:!");
1414 AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1415 AddCodeLine ("\tclc");
1416 AddCodeLine ("\tadc\t(sp),y");
1417 AddCodeLine ("\tpha");
1418 AddCodeLine ("\ttxa");
1419 AddCodeLine ("\tiny");
1420 AddCodeLine ("\tadc\t(sp),y");
1421 AddCodeLine ("\ttax");
1422 AddCodeLine ("\tpla");
1426 /* Do it the old way */
1428 g_getlocal (flags, offs);
1440 void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
1441 /* Add a static variable to ax */
1443 /* Create the correct label name */
1444 char* lbuf = GetLabelName (flags, label, offs);
1446 switch (flags & CF_TYPE) {
1449 AddCodeLine ("\tclc");
1450 AddCodeLine ("\tadc\t%s", lbuf);
1451 AddCodeLine ("\tbcc\t*+3");
1452 AddCodeLine ("\tinx");
1453 AddCodeHint ("x:!");
1457 AddCodeLine ("\tclc");
1458 AddCodeLine ("\tadc\t%s", lbuf);
1459 AddCodeLine ("\ttay");
1460 AddCodeLine ("\ttxa");
1461 AddCodeLine ("\tadc\t%s+1", lbuf);
1462 AddCodeLine ("\ttax");
1463 AddCodeLine ("\ttya");
1467 /* Do it the old way */
1469 g_getstatic (flags, label, offs);
1481 /*****************************************************************************/
1482 /* Compares of ax with a variable with fixed address */
1483 /*****************************************************************************/
1487 void g_cmplocal (unsigned flags, int offs)
1488 /* Compare a local variable to ax */
1490 Internal ("g_cmplocal not implemented");
1495 void g_cmpstatic (unsigned flags, unsigned label, unsigned offs)
1496 /* Compare a static variable to ax */
1498 Internal ("g_cmpstatic not implemented");
1503 /*****************************************************************************/
1504 /* Special op= functions */
1505 /*****************************************************************************/
1509 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
1511 /* Emit += for a static variable */
1513 /* Create the correct label name */
1514 char* lbuf = GetLabelName (flags, label, offs);
1516 /* Check the size and determine operation */
1517 switch (flags & CF_TYPE) {
1520 if (flags & CF_FORCECHAR) {
1521 AddCodeLine ("\tldx\t#$00");
1522 if (flags & CF_CONST) {
1524 AddCodeLine ("\tinc\t%s", lbuf);
1525 AddCodeLine ("\tlda\t%s", lbuf);
1527 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1528 AddCodeLine ("\tclc");
1529 AddCodeLine ("\tadc\t%s", lbuf);
1530 AddCodeLine ("\tsta\t%s", lbuf);
1533 AddCodeLine ("\tclc");
1534 AddCodeLine ("\tadc\t%s", lbuf);
1535 AddCodeLine ("\tsta\t%s", lbuf);
1537 if ((flags & CF_UNSIGNED) == 0) {
1538 AddCodeLine ("\tbpl\t*+3");
1539 AddCodeLine ("\tdex");
1540 AddCodeHint ("x:!"); /* Invalidate X */
1547 if (flags & CF_CONST) {
1549 label = GetLabel ();
1550 AddCodeLine ("\tinc\t%s", lbuf);
1551 AddCodeLine ("\tbne\tL%04X", label);
1552 AddCodeLine ("\tinc\t%s+1", lbuf);
1553 g_defloclabel (label);
1554 AddCodeLine ("\tlda\t%s", lbuf); /* Hmmm... */
1555 AddCodeLine ("\tldx\t%s+1", lbuf);
1557 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1558 AddCodeLine ("\tclc");
1559 AddCodeLine ("\tadc\t%s", lbuf);
1560 AddCodeLine ("\tsta\t%s", lbuf);
1562 label = GetLabel ();
1563 AddCodeLine ("\tbcc\tL%04X", label);
1564 AddCodeLine ("\tinc\t%s+1", lbuf);
1565 g_defloclabel (label);
1566 AddCodeLine ("\tldx\t%s+1", lbuf);
1568 AddCodeLine ("\tlda\t#$%02X", (val >> 8) & 0xFF);
1569 AddCodeLine ("\tadc\t%s+1", lbuf);
1570 AddCodeLine ("\tsta\t%s+1", lbuf);
1571 AddCodeLine ("\ttax");
1572 AddCodeLine ("\tlda\t%s", lbuf);
1576 AddCodeLine ("\tclc");
1577 AddCodeLine ("\tadc\t%s", lbuf);
1578 AddCodeLine ("\tsta\t%s", lbuf);
1579 AddCodeLine ("\ttxa");
1580 AddCodeLine ("\tadc\t%s+1", lbuf);
1581 AddCodeLine ("\tsta\t%s+1", lbuf);
1582 AddCodeLine ("\ttax");
1583 AddCodeLine ("\tlda\t%s", lbuf);
1588 if (flags & CF_CONST) {
1590 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1591 AddCodeLine ("\tsty\tptr1");
1592 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1594 AddCodeLine ("\tjsr\tladdeq1");
1596 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1597 AddCodeLine ("\tjsr\tladdeqa");
1600 g_getstatic (flags, label, offs);
1602 g_putstatic (flags, label, offs);
1605 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1606 AddCodeLine ("\tsty\tptr1");
1607 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1608 AddCodeLine ("\tjsr\tladdeq");
1619 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1620 /* Emit += for a local variable */
1622 /* Calculate the true offset, check it, load it into Y */
1624 CheckLocalOffs (offs);
1626 /* Check the size and determine operation */
1627 switch (flags & CF_TYPE) {
1630 if (flags & CF_FORCECHAR) {
1632 AddCodeLine ("\tldx\t#$00");
1633 if (flags & CF_CONST) {
1634 AddCodeLine ("\tclc");
1635 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1636 AddCodeLine ("\tadc\t(sp,x)");
1637 AddCodeLine ("\tsta\t(sp,x)");
1639 AddCodeLine ("\tclc");
1640 AddCodeLine ("\tadc\t(sp,x)");
1641 AddCodeLine ("\tsta\t(sp,x)");
1645 AddCodeLine ("\tldx\t#$00");
1646 if (flags & CF_CONST) {
1647 AddCodeLine ("\tclc");
1648 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1649 AddCodeLine ("\tadc\t(sp),y");
1650 AddCodeLine ("\tsta\t(sp),y");
1652 AddCodeLine ("\tclc");
1653 AddCodeLine ("\tadc\t(sp),y");
1654 AddCodeLine ("\tsta\t(sp),y");
1657 if ((flags & CF_UNSIGNED) == 0) {
1658 AddCodeLine ("\tbpl\t*+3");
1659 AddCodeLine ("\tdex");
1660 AddCodeHint ("x:!"); /* Invalidate X */
1667 if (flags & CF_CONST) {
1668 g_getimmed (flags, val, 0);
1671 AddCodeLine ("\tjsr\taddeq0sp");
1674 AddCodeLine ("\tjsr\taddeqysp");
1679 if (flags & CF_CONST) {
1680 g_getimmed (flags, val, 0);
1683 AddCodeLine ("\tjsr\tladdeq0sp");
1686 AddCodeLine ("\tjsr\tladdeqysp");
1697 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1698 /* Emit += for the location with address in ax */
1700 /* If the offset is too large for a byte register, add the high byte
1701 * of the offset to the primary. Beware: We need a special correction
1702 * if the offset in the low byte will overflow in the operation.
1704 offs = MakeByteOffs (flags, offs);
1706 /* Check the size and determine operation */
1707 switch (flags & CF_TYPE) {
1710 AddCodeLine ("\tsta\tptr1");
1711 AddCodeLine ("\tstx\tptr1+1");
1713 AddCodeLine ("\tldx\t#$00");
1714 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1715 AddCodeLine ("\tclc");
1716 AddCodeLine ("\tadc\t(ptr1,x)");
1717 AddCodeLine ("\tsta\t(ptr1,x)");
1719 AddCodeLine ("\tldy\t#$%02X", offs);
1720 AddCodeLine ("\tldx\t#$00");
1721 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1722 AddCodeLine ("\tclc");
1723 AddCodeLine ("\tadc\t(ptr1),y");
1724 AddCodeLine ("\tsta\t(ptr1),y");
1730 /* Lots of code, use only if size is not important */
1731 AddCodeLine ("\tsta\tptr1");
1732 AddCodeLine ("\tstx\tptr1+1");
1733 AddCodeLine ("\tldy\t#$%02X", offs);
1734 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1735 AddCodeLine ("\tclc");
1736 AddCodeLine ("\tadc\t(ptr1),y");
1737 AddCodeLine ("\tsta\t(ptr1),y");
1738 AddCodeLine ("\tpha");
1739 AddCodeLine ("\tiny");
1740 AddCodeLine ("\tlda\t#$%02X", (val >> 8) & 0xFF);
1741 AddCodeLine ("\tadc\t(ptr1),y");
1742 AddCodeLine ("\tsta\t(ptr1),y");
1743 AddCodeLine ("\ttax");
1744 AddCodeLine ("\tpla");
1750 AddCodeLine ("\tjsr\tpushax"); /* Push the address */
1751 push (flags); /* Correct the internal sp */
1752 g_getind (flags, offs); /* Fetch the value */
1753 g_inc (flags, val); /* Increment value in primary */
1754 g_putind (flags, offs); /* Store the value back */
1764 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1766 /* Emit -= for a static variable */
1768 /* Create the correct label name */
1769 char* lbuf = GetLabelName (flags, label, offs);
1771 /* Check the size and determine operation */
1772 switch (flags & CF_TYPE) {
1775 if (flags & CF_FORCECHAR) {
1776 AddCodeLine ("\tldx\t#$00");
1777 if (flags & CF_CONST) {
1779 AddCodeLine ("\tdec\t%s", lbuf);
1780 AddCodeLine ("\tlda\t%s", lbuf);
1782 AddCodeLine ("\tsec");
1783 AddCodeLine ("\tlda\t%s", lbuf);
1784 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
1785 AddCodeLine ("\tsta\t%s", lbuf);
1788 AddCodeLine ("\tsec");
1789 AddCodeLine ("\tsta\ttmp1");
1790 AddCodeLine ("\tlda\t%s", lbuf);
1791 AddCodeLine ("\tsbc\ttmp1");
1792 AddCodeLine ("\tsta\t%s", lbuf);
1794 if ((flags & CF_UNSIGNED) == 0) {
1795 AddCodeLine ("\tbpl\t*+3");
1796 AddCodeLine ("\tdex");
1797 AddCodeHint ("x:!"); /* Invalidate X */
1804 AddCodeLine ("\tsec");
1805 if (flags & CF_CONST) {
1806 AddCodeLine ("\tlda\t%s", lbuf);
1807 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
1808 AddCodeLine ("\tsta\t%s", lbuf);
1810 label = GetLabel ();
1811 AddCodeLine ("\tbcs\tL%04X", label);
1812 AddCodeLine ("\tdec\t%s+1", lbuf);
1813 g_defloclabel (label);
1814 AddCodeLine ("\tldx\t%s+1", lbuf);
1816 AddCodeLine ("\tlda\t%s+1", lbuf);
1817 AddCodeLine ("\tsbc\t#$%02X", (val >> 8) & 0xFF);
1818 AddCodeLine ("\tsta\t%s+1", lbuf);
1819 AddCodeLine ("\ttax");
1820 AddCodeLine ("\tlda\t%s", lbuf);
1823 AddCodeLine ("\tsta\ttmp1");
1824 AddCodeLine ("\tlda\t%s", lbuf);
1825 AddCodeLine ("\tsbc\ttmp1");
1826 AddCodeLine ("\tsta\t%s", lbuf);
1827 AddCodeLine ("\tstx\ttmp1");
1828 AddCodeLine ("\tlda\t%s+1", lbuf);
1829 AddCodeLine ("\tsbc\ttmp1");
1830 AddCodeLine ("\tsta\t%s+1", lbuf);
1831 AddCodeLine ("\ttax");
1832 AddCodeLine ("\tlda\t%s", lbuf);
1837 if (flags & CF_CONST) {
1839 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1840 AddCodeLine ("\tsty\tptr1");
1841 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1843 AddCodeLine ("\tjsr\tlsubeq1");
1845 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
1846 AddCodeLine ("\tjsr\tlsubeqa");
1849 g_getstatic (flags, label, offs);
1851 g_putstatic (flags, label, offs);
1854 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1855 AddCodeLine ("\tsty\tptr1");
1856 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1857 AddCodeLine ("\tjsr\tlsubeq");
1868 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1869 /* Emit -= for a local variable */
1871 /* Calculate the true offset, check it, load it into Y */
1873 CheckLocalOffs (offs);
1875 /* Check the size and determine operation */
1876 switch (flags & CF_TYPE) {
1879 if (flags & CF_FORCECHAR) {
1881 AddCodeLine ("\tldx\t#$00");
1882 AddCodeLine ("\tsec");
1883 if (flags & CF_CONST) {
1884 AddCodeLine ("\tlda\t(sp),y");
1885 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
1887 AddCodeLine ("\tsta\ttmp1");
1888 AddCodeLine ("\tlda\t(sp),y");
1889 AddCodeLine ("\tsbc\ttmp1");
1891 AddCodeLine ("\tsta\t(sp),y");
1892 if ((flags & CF_UNSIGNED) == 0) {
1893 AddCodeLine ("\tbpl\t*+3");
1894 AddCodeLine ("\tdex");
1895 AddCodeHint ("x:!"); /* Invalidate X */
1902 if (flags & CF_CONST) {
1903 g_getimmed (flags, val, 0);
1906 AddCodeLine ("\tjsr\tsubeq0sp");
1909 AddCodeLine ("\tjsr\tsubeqysp");
1914 if (flags & CF_CONST) {
1915 g_getimmed (flags, val, 0);
1918 AddCodeLine ("\tjsr\tlsubeq0sp");
1921 AddCodeLine ("\tjsr\tlsubeqysp");
1932 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
1933 /* Emit -= for the location with address in ax */
1935 /* If the offset is too large for a byte register, add the high byte
1936 * of the offset to the primary. Beware: We need a special correction
1937 * if the offset in the low byte will overflow in the operation.
1939 offs = MakeByteOffs (flags, offs);
1941 /* Check the size and determine operation */
1942 switch (flags & CF_TYPE) {
1945 AddCodeLine ("\tsta\tptr1");
1946 AddCodeLine ("\tstx\tptr1+1");
1948 AddCodeLine ("\tldx\t#$00");
1949 AddCodeLine ("\tlda\t(ptr1,x)");
1950 AddCodeLine ("\tsec");
1951 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
1952 AddCodeLine ("\tsta\t(ptr1,x)");
1954 AddCodeLine ("\tldy\t#$%02X", offs);
1955 AddCodeLine ("\tldx\t#$00");
1956 AddCodeLine ("\tlda\t(ptr1),y");
1957 AddCodeLine ("\tsec");
1958 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
1959 AddCodeLine ("\tsta\t(ptr1),y");
1965 /* Lots of code, use only if size is not important */
1966 AddCodeLine ("\tsta\tptr1");
1967 AddCodeLine ("\tstx\tptr1+1");
1968 AddCodeLine ("\tldy\t#$%02X", offs);
1969 AddCodeLine ("\tlda\t(ptr1),y");
1970 AddCodeLine ("\tsec");
1971 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
1972 AddCodeLine ("\tsta\t(ptr1),y");
1973 AddCodeLine ("\tpha");
1974 AddCodeLine ("\tiny");
1975 AddCodeLine ("\tlda\t(ptr1),y");
1976 AddCodeLine ("\tsbc\t#$%02X", (val >> 8) & 0xFF);
1977 AddCodeLine ("\tsta\t(ptr1),y");
1978 AddCodeLine ("\ttax");
1979 AddCodeLine ("\tpla");
1985 AddCodeLine ("\tjsr\tpushax"); /* Push the address */
1986 push (flags); /* Correct the internal sp */
1987 g_getind (flags, offs); /* Fetch the value */
1988 g_dec (flags, val); /* Increment value in primary */
1989 g_putind (flags, offs); /* Store the value back */
1999 /*****************************************************************************/
2000 /* Add a variable address to the value in ax */
2001 /*****************************************************************************/
2005 void g_addaddr_local (unsigned flags, int offs)
2006 /* Add the address of a local variable to ax */
2008 /* Add the offset */
2011 /* We cannot address more then 256 bytes of locals anyway */
2012 CheckLocalOffs (offs);
2013 AddCodeLine ("\tclc");
2014 AddCodeLine ("\tadc\t#$%02X", offs & 0xFF);
2015 AddCodeLine ("\tbcc\t*+4"); /* Do also skip the CLC insn below */
2016 AddCodeLine ("\tinx");
2017 AddCodeHint ("x:!"); /* Invalidate X */
2020 /* Add the current stackpointer value */
2021 AddCodeLine ("\tclc");
2022 AddCodeLine ("\tadc\tsp");
2023 AddCodeLine ("\ttay");
2024 AddCodeLine ("\ttxa");
2025 AddCodeLine ("\tadc\tsp+1");
2026 AddCodeLine ("\ttax");
2027 AddCodeLine ("\ttya");
2032 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2033 /* Add the address of a static variable to ax */
2035 /* Create the correct label name */
2036 char* lbuf = GetLabelName (flags, label, offs);
2038 /* Add the address to the current ax value */
2039 AddCodeLine ("\tclc");
2040 AddCodeLine ("\tadc\t#<(%s)", lbuf);
2041 AddCodeLine ("\ttay");
2042 AddCodeLine ("\ttxa");
2043 AddCodeLine ("\tadc\t#>(%s)", lbuf);
2044 AddCodeLine ("\ttax");
2045 AddCodeLine ("\ttya");
2050 /*****************************************************************************/
2052 /*****************************************************************************/
2056 void g_save (unsigned flags)
2057 /* Copy primary register to hold register. */
2059 /* Check the size and determine operation */
2060 switch (flags & CF_TYPE) {
2063 if (flags & CF_FORCECHAR) {
2064 AddCodeLine ("\tpha");
2070 AddCodeLine ("\tsta\tregsave");
2071 AddCodeLine ("\tstx\tregsave+1");
2075 AddCodeLine ("\tjsr\tsaveeax");
2085 void g_restore (unsigned flags)
2086 /* Copy hold register to P. */
2088 /* Check the size and determine operation */
2089 switch (flags & CF_TYPE) {
2092 if (flags & CF_FORCECHAR) {
2093 AddCodeLine ("\tpla");
2099 AddCodeLine ("\tlda\tregsave");
2100 AddCodeLine ("\tldx\tregsave+1");
2104 AddCodeLine ("\tjsr\tresteax");
2114 void g_cmp (unsigned flags, unsigned long val)
2115 /* Immidiate compare. The primary register will not be changed, Z flag
2119 /* Check the size and determine operation */
2120 switch (flags & CF_TYPE) {
2123 if (flags & CF_FORCECHAR) {
2124 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
2130 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
2131 AddCodeLine ("\tbne\t*+4");
2132 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
2136 Internal ("g_cmp: Long compares not implemented");
2146 static void oper (unsigned flags, unsigned long val, char** subs)
2147 /* Encode a binary operation. subs is a pointer to four groups of three
2149 * 0-2 --> Operate on ints
2150 * 3-5 --> Operate on unsigneds
2151 * 6-8 --> Operate on longs
2152 * 9-11 --> Operate on unsigned longs
2154 * The first subroutine names in each string group is used to encode an
2155 * operation with a zero constant, the second to encode an operation with
2156 * a 8 bit constant, and the third is used in all other cases.
2161 /* Determine the offset into the array */
2162 offs = (flags & CF_UNSIGNED)? 3 : 0;
2163 switch (flags & CF_TYPE) {
2176 /* Encode the operation */
2177 if (flags & CF_CONST) {
2178 /* Constant value given */
2179 if (val == 0 && subs [offs+0]) {
2180 /* Special case: constant with value zero */
2181 AddCodeLine ("\tjsr\t%s", subs [offs+0]);
2182 } else if (val < 0x100 && subs [offs+1]) {
2183 /* Special case: constant with high byte zero */
2184 ldaconst (val); /* Load low byte */
2185 AddCodeLine ("\tjsr\t%s", subs [offs+1]);
2187 /* Others: arbitrary constant value */
2188 g_getimmed (flags, val, 0); /* Load value */
2189 AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2192 /* Value not constant (is already in (e)ax) */
2193 AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2196 /* The operation will pop it's argument */
2202 void g_test (unsigned flags)
2203 /* Force a test to set cond codes right */
2205 switch (flags & CF_TYPE) {
2208 if (flags & CF_FORCECHAR) {
2209 AddCodeLine ("\ttax");
2215 AddCodeLine ("\tstx\ttmp1");
2216 AddCodeLine ("\tora\ttmp1");
2220 if (flags & CF_UNSIGNED) {
2221 AddCodeLine ("\tjsr\tutsteax");
2223 AddCodeLine ("\tjsr\ttsteax");
2235 void g_push (unsigned flags, unsigned long val)
2236 /* Push the primary register or a constant value onto the stack */
2240 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2242 /* We have a constant 8 or 16 bit value */
2243 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2245 /* Handle as 8 bit value */
2246 if (FavourSize && val <= 2) {
2247 AddCodeLine ("\tjsr\tpushc%d", (int) val);
2250 AddCodeLine ("\tjsr\tpusha");
2255 /* Handle as 16 bit value */
2256 hi = (unsigned char) (val >> 8);
2258 AddCodeLine ("\tjsr\tpush%u", (unsigned) val);
2259 } else if (hi == 0 || hi == 0xFF) {
2260 /* Use special function */
2262 AddCodeLine ("\tjsr\t%s", (hi == 0)? "pusha0" : "pushaFF");
2265 g_getimmed (flags, val, 0);
2266 AddCodeLine ("\tjsr\tpushax");
2272 /* Value is not 16 bit or not constant */
2273 if (flags & CF_CONST) {
2274 /* Constant 32 bit value, load into eax */
2275 g_getimmed (flags, val, 0);
2278 /* Push the primary register */
2279 switch (flags & CF_TYPE) {
2282 if (flags & CF_FORCECHAR) {
2283 /* Handle as char */
2284 AddCodeLine ("\tjsr\tpusha");
2289 AddCodeLine ("\tjsr\tpushax");
2293 AddCodeLine ("\tjsr\tpusheax");
2303 /* Adjust the stack offset */
2309 void g_swap (unsigned flags)
2310 /* Swap the primary register and the top of the stack. flags give the type
2311 * of *both* values (must have same size).
2314 switch (flags & CF_TYPE) {
2318 AddCodeLine ("\tjsr\tswapstk");
2322 AddCodeLine ("\tjsr\tswapestk");
2333 void g_call (unsigned flags, char* lbl, unsigned argsize)
2334 /* Call the specified subroutine name */
2336 if ((flags & CF_FIXARGC) == 0) {
2337 /* Pass arg count */
2340 AddCodeLine ("\tjsr\t_%s", lbl);
2341 oursp += argsize; /* callee pops args */
2346 void g_callind (unsigned flags, unsigned argsize)
2347 /* Call subroutine with address in AX */
2349 if ((flags & CF_FIXARGC) == 0) {
2350 /* Pass arg count */
2353 AddCodeLine ("\tjsr\tcallax"); /* do the call */
2354 oursp += argsize; /* callee pops args */
2359 void g_jump (unsigned label)
2360 /* Jump to specified internal label number */
2362 AddCodeLine ("\tjmp\tL%04X", label);
2367 void g_switch (unsigned flags)
2368 /* Output switch statement preample */
2370 switch (flags & CF_TYPE) {
2374 AddCodeLine ("\tjsr\tswitch");
2378 AddCodeLine ("\tjsr\tlswitch");
2389 void g_case (unsigned flags, unsigned label, unsigned long val)
2390 /* Create table code for one case selector */
2392 switch (flags & CF_TYPE) {
2396 AddCodeLine ("\t.word\t$%04X, L%04X", val & 0xFFFF, label & 0xFFFF);
2400 AddCodeLine ("\t.dword\t$%08X", val);
2401 AddCodeLine ("\t.word\tL%04X", label & 0xFFFF);
2412 void g_truejump (unsigned flags, unsigned label)
2413 /* Jump to label if zero flag clear */
2415 if (flags & CF_SHORT) {
2416 AddCodeLine ("\tbne\tL%04X", label);
2418 AddCodeLine ("\tjne\tL%04X", label);
2424 void g_falsejump (unsigned flags, unsigned label)
2425 /* Jump to label if zero flag set */
2427 if (flags & CF_SHORT) {
2428 AddCodeLine ("\tbeq\tL%04X", label);
2430 AddCodeLine ("\tjeq\tL%04X", label);
2436 static void mod_internal (int k, char* verb1, char* verb2)
2439 AddCodeLine ("\tjsr\t%ssp%c", verb1, k + '0');
2443 AddCodeLine ("\tjsr\t%ssp", verb2);
2449 void g_space (int space)
2450 /* Create or drop space on the stack */
2453 mod_internal (-space, "inc", "addy");
2454 } else if (space > 0) {
2455 mod_internal (space, "dec", "suby");
2461 void g_add (unsigned flags, unsigned long val)
2462 /* Primary = TOS + Primary */
2464 static char* ops [12] = {
2465 0, "tosadda0", "tosaddax",
2466 0, "tosadda0", "tosaddax",
2471 if (flags & CF_CONST) {
2472 flags &= ~CF_FORCECHAR; // Handle chars as ints
2473 g_push (flags & ~CF_CONST, 0);
2475 oper (flags, val, ops);
2480 void g_sub (unsigned flags, unsigned long val)
2481 /* Primary = TOS - Primary */
2483 static char* ops [12] = {
2484 0, "tossuba0", "tossubax",
2485 0, "tossuba0", "tossubax",
2490 if (flags & CF_CONST) {
2491 flags &= ~CF_FORCECHAR; // Handle chars as ints
2492 g_push (flags & ~CF_CONST, 0);
2494 oper (flags, val, ops);
2499 void g_rsub (unsigned flags, unsigned long val)
2500 /* Primary = Primary - TOS */
2502 static char* ops [12] = {
2503 0, "tosrsuba0", "tosrsubax",
2504 0, "tosrsuba0", "tosrsubax",
2508 oper (flags, val, ops);
2513 void g_mul (unsigned flags, unsigned long val)
2514 /* Primary = TOS * Primary */
2516 static char* ops [12] = {
2517 0, "tosmula0", "tosmulax",
2518 0, "tosumula0", "tosumulax",
2525 /* Do strength reduction if the value is constant and a power of two */
2526 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2527 /* Generate a shift instead */
2532 /* If the right hand side is const, the lhs is not on stack but still
2533 * in the primary register.
2535 if (flags & CF_CONST) {
2537 switch (flags & CF_TYPE) {
2540 if (flags & CF_FORCECHAR) {
2541 /* Handle some special cases */
2545 AddCodeLine ("\tsta\ttmp1");
2546 AddCodeLine ("\tasl\ta");
2547 AddCodeLine ("\tclc");
2548 AddCodeLine ("\tadc\ttmp1");
2552 AddCodeLine ("\tsta\ttmp1");
2553 AddCodeLine ("\tasl\ta");
2554 AddCodeLine ("\tasl\ta");
2555 AddCodeLine ("\tclc");
2556 AddCodeLine ("\tadc\ttmp1");
2560 AddCodeLine ("\tsta\ttmp1");
2561 AddCodeLine ("\tasl\ta");
2562 AddCodeLine ("\tasl\ta");
2563 AddCodeLine ("\tclc");
2564 AddCodeLine ("\tadc\ttmp1");
2565 AddCodeLine ("\tasl\ta");
2581 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2582 * into the normal, non-optimized stuff.
2584 flags &= ~CF_FORCECHAR; // Handle chars as ints
2585 g_push (flags & ~CF_CONST, 0);
2589 /* Use long way over the stack */
2590 oper (flags, val, ops);
2595 void g_div (unsigned flags, unsigned long val)
2596 /* Primary = TOS / Primary */
2598 static char* ops [12] = {
2599 0, "tosdiva0", "tosdivax",
2600 0, "tosudiva0", "tosudivax",
2605 /* Do strength reduction if the value is constant and a power of two */
2607 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2608 /* Generate a shift instead */
2611 /* Generate a division */
2612 if (flags & CF_CONST) {
2613 /* lhs is not on stack */
2614 flags &= ~CF_FORCECHAR; // Handle chars as ints
2615 g_push (flags & ~CF_CONST, 0);
2617 oper (flags, val, ops);
2623 void g_mod (unsigned flags, unsigned long val)
2624 /* Primary = TOS % Primary */
2626 static char* ops [12] = {
2627 0, "tosmoda0", "tosmodax",
2628 0, "tosumoda0", "tosumodax",
2634 /* Check if we can do some cost reduction */
2635 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2636 /* We can do that with an AND operation */
2637 g_and (flags, val - 1);
2639 /* Do it the hard way... */
2640 if (flags & CF_CONST) {
2641 /* lhs is not on stack */
2642 flags &= ~CF_FORCECHAR; // Handle chars as ints
2643 g_push (flags & ~CF_CONST, 0);
2645 oper (flags, val, ops);
2651 void g_or (unsigned flags, unsigned long val)
2652 /* Primary = TOS | Primary */
2654 static char* ops [12] = {
2655 0, "tosora0", "tosorax",
2656 0, "tosora0", "tosorax",
2661 /* If the right hand side is const, the lhs is not on stack but still
2662 * in the primary register.
2664 if (flags & CF_CONST) {
2666 switch (flags & CF_TYPE) {
2669 if (flags & CF_FORCECHAR) {
2670 if ((val & 0xFF) != 0xFF) {
2671 AddCodeLine ("\tora\t#$%02X", val & 0xFF);
2679 AddCodeLine ("\tora\t#$%02X", val & 0xFF);
2686 AddCodeLine ("\tora\t#$%02X", val & 0xFF);
2695 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2696 * into the normal, non-optimized stuff.
2698 g_push (flags & ~CF_CONST, 0);
2702 /* Use long way over the stack */
2703 oper (flags, val, ops);
2708 void g_xor (unsigned flags, unsigned long val)
2709 /* Primary = TOS ^ Primary */
2711 static char* ops [12] = {
2712 0, "tosxora0", "tosxorax",
2713 0, "tosxora0", "tosxorax",
2719 /* If the right hand side is const, the lhs is not on stack but still
2720 * in the primary register.
2722 if (flags & CF_CONST) {
2724 switch (flags & CF_TYPE) {
2727 if (flags & CF_FORCECHAR) {
2728 if ((val & 0xFF) != 0) {
2729 AddCodeLine ("\teor\t#$%02X", val & 0xFF);
2738 AddCodeLine ("\teor\t#$%02X", val);
2741 } else if ((val & 0xFF) == 0) {
2742 AddCodeLine ("\tpha");
2743 AddCodeLine ("\ttxa");
2744 AddCodeLine ("\teor\t#$%02X", (val >> 8) & 0xFF);
2745 AddCodeLine ("\ttax");
2746 AddCodeLine ("\tpla");
2754 AddCodeLine ("\teor\t#$%02X", val & 0xFF);
2764 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2765 * into the normal, non-optimized stuff.
2767 g_push (flags & ~CF_CONST, 0);
2771 /* Use long way over the stack */
2772 oper (flags, val, ops);
2777 void g_and (unsigned flags, unsigned long val)
2778 /* Primary = TOS & Primary */
2780 static char* ops [12] = {
2781 0, "tosanda0", "tosandax",
2782 0, "tosanda0", "tosandax",
2787 /* If the right hand side is const, the lhs is not on stack but still
2788 * in the primary register.
2790 if (flags & CF_CONST) {
2792 switch (flags & CF_TYPE) {
2795 if (flags & CF_FORCECHAR) {
2796 AddCodeLine ("\tand\t#$%02X", val & 0xFF);
2801 if ((val & 0xFFFF) != 0xFFFF) {
2806 } else if (val != 0xFF) {
2807 AddCodeLine ("\tand\t#$%02X", val & 0xFF);
2809 } else if ((val & 0xFF00) == 0xFF00) {
2810 AddCodeLine ("\tand\t#$%02X", val & 0xFF);
2811 } else if ((val & 0x00FF) == 0x0000) {
2812 AddCodeLine ("\ttxa");
2813 AddCodeLine ("\tand\t#$%02X", (val >> 8) & 0xFF);
2814 AddCodeLine ("\ttax");
2817 AddCodeLine ("\ttay");
2818 AddCodeLine ("\ttxa");
2819 AddCodeLine ("\tand\t#$%02X", (val >> 8) & 0xFF);
2820 AddCodeLine ("\ttax");
2821 AddCodeLine ("\ttya");
2822 if ((val & 0x00FF) != 0x00FF) {
2823 AddCodeLine ("\tand\t#$%02X", val & 0xFF);
2832 AddCodeLine ("\tstx\tsreg+1");
2833 AddCodeLine ("\tstx\tsreg");
2834 if ((val & 0xFF) != 0xFF) {
2835 AddCodeLine ("\tand\t#$%02X", val & 0xFF);
2838 } else if (val == 0xFF00) {
2840 AddCodeLine ("\tsta\tsreg+1");
2841 AddCodeLine ("\tsta\tsreg");
2850 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2851 * into the normal, non-optimized stuff.
2853 g_push (flags & ~CF_CONST, 0);
2857 /* Use long way over the stack */
2858 oper (flags, val, ops);
2863 void g_asr (unsigned flags, unsigned long val)
2864 /* Primary = TOS >> Primary */
2866 static char* ops [12] = {
2867 0, "tosasra0", "tosasrax",
2868 0, "tosshra0", "tosshrax",
2873 /* If the right hand side is const, the lhs is not on stack but still
2874 * in the primary register.
2876 if (flags & CF_CONST) {
2878 switch (flags & CF_TYPE) {
2882 if (val >= 1 && val <= 3) {
2883 if (flags & CF_UNSIGNED) {
2884 AddCodeLine ("\tjsr\tshrax%ld", val);
2886 AddCodeLine ("\tjsr\tasrax%ld", val);
2889 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2890 AddCodeLine ("\ttxa");
2897 if (val >= 1 && val <= 3) {
2898 if (flags & CF_UNSIGNED) {
2899 AddCodeLine ("\tjsr\tshreax%ld", val);
2901 AddCodeLine ("\tjsr\tasreax%ld", val);
2904 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2905 AddCodeLine ("\ttxa");
2906 AddCodeLine ("\tldx\tsreg");
2907 AddCodeLine ("\tldy\tsreg+1");
2908 AddCodeLine ("\tsty\tsreg");
2909 AddCodeLine ("\tldy\t#$00");
2910 AddCodeLine ("\tsty\tsreg+1");
2912 } else if (val == 16) {
2913 AddCodeLine ("\tldy\t#$00");
2914 AddCodeLine ("\tldx\tsreg+1");
2915 if ((flags & CF_UNSIGNED) == 0) {
2916 AddCodeLine ("\tbpl\t*+3");
2917 AddCodeLine ("\tdey");
2918 AddCodeHint ("y:!");
2920 AddCodeLine ("\tlda\tsreg");
2921 AddCodeLine ("\tsty\tsreg+1");
2922 AddCodeLine ("\tsty\tsreg");
2931 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2932 * into the normal, non-optimized stuff.
2934 g_push (flags & ~CF_CONST, 0);
2938 /* Use long way over the stack */
2939 oper (flags, val, ops);
2944 void g_asl (unsigned flags, unsigned long val)
2945 /* Primary = TOS << Primary */
2947 static char* ops [12] = {
2948 0, "tosasla0", "tosaslax",
2949 0, "tosshla0", "tosshlax",
2955 /* If the right hand side is const, the lhs is not on stack but still
2956 * in the primary register.
2958 if (flags & CF_CONST) {
2960 switch (flags & CF_TYPE) {
2964 if (val >= 1 && val <= 3) {
2965 if (flags & CF_UNSIGNED) {
2966 AddCodeLine ("\tjsr\tshlax%ld", val);
2968 AddCodeLine ("\tjsr\taslax%ld", val);
2971 } else if (val == 8) {
2972 AddCodeLine ("\ttax");
2973 AddCodeLine ("\tlda\t#$00");
2979 if (val >= 1 && val <= 3) {
2980 if (flags & CF_UNSIGNED) {
2981 AddCodeLine ("\tjsr\tshleax%ld", val);
2983 AddCodeLine ("\tjsr\tasleax%ld", val);
2986 } else if (val == 8) {
2987 AddCodeLine ("\tldy\tsreg");
2988 AddCodeLine ("\tsty\tsreg+1");
2989 AddCodeLine ("\tstx\tsreg");
2990 AddCodeLine ("\ttax");
2991 AddCodeLine ("\tlda\t#$00");
2993 } else if (val == 16) {
2994 AddCodeLine ("\tstx\tsreg+1");
2995 AddCodeLine ("\tsta\tsreg");
2996 AddCodeLine ("\tlda\t#$00");
2997 AddCodeLine ("\ttax");
3006 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3007 * into the normal, non-optimized stuff.
3009 g_push (flags & ~CF_CONST, 0);
3013 /* Use long way over the stack */
3014 oper (flags, val, ops);
3019 void g_neg (unsigned flags)
3020 /* Primary = -Primary */
3022 switch (flags & CF_TYPE) {
3026 AddCodeLine ("\tjsr\tnegax");
3030 AddCodeLine ("\tjsr\tnegeax");
3040 void g_bneg (unsigned flags)
3041 /* Primary = !Primary */
3043 switch (flags & CF_TYPE) {
3046 AddCodeLine ("\tjsr\tbnega");
3050 AddCodeLine ("\tjsr\tbnegax");
3054 AddCodeLine ("\tjsr\tbnegeax");
3064 void g_com (unsigned flags)
3065 /* Primary = ~Primary */
3067 switch (flags & CF_TYPE) {
3071 AddCodeLine ("\tjsr\tcomplax");
3075 AddCodeLine ("\tjsr\tcompleax");
3085 void g_inc (unsigned flags, unsigned long val)
3086 /* Increment the primary register by a given number */
3088 /* Don't inc by zero */
3093 /* Generate code for the supported types */
3095 switch (flags & CF_TYPE) {
3098 if (flags & CF_FORCECHAR) {
3099 AddCodeLine ("\tclc");
3100 AddCodeLine ("\tadc\t#$%02X", val & 0xFF);
3109 AddCodeLine ("\tjsr\tincax%u", val);
3110 } else if (val <= 255) {
3112 AddCodeLine ("\tjsr\tincaxy");
3114 g_add (flags | CF_CONST, val);
3117 /* Inline the code */
3119 if ((val & 0xFF) != 0) {
3120 AddCodeLine ("\tclc");
3121 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3122 AddCodeLine ("\tbcc\t*+3");
3123 AddCodeLine ("\tinx");
3124 /* Tell the optimizer that the X register may be invalid */
3125 AddCodeHint ("x:!");
3128 AddCodeLine ("\tinx");
3131 AddCodeLine ("\tinx");
3134 AddCodeLine ("\tclc");
3135 if ((val & 0xFF) != 0) {
3136 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3137 /* Tell the optimizer that the X register may be invalid */
3138 AddCodeHint ("x:!");
3140 AddCodeLine ("\tpha");
3141 AddCodeLine ("\ttxa");
3142 AddCodeLine ("\tadc\t#$%02X", (unsigned char) (val >> 8));
3143 AddCodeLine ("\ttax");
3144 AddCodeLine ("\tpla");
3152 AddCodeLine ("\tjsr\tinceaxy");
3154 g_add (flags | CF_CONST, val);
3166 void g_dec (unsigned flags, unsigned long val)
3167 /* Decrement the primary register by a given number */
3169 /* Generate code for the supported types */
3171 switch (flags & CF_TYPE) {
3174 if (flags & CF_FORCECHAR) {
3175 AddCodeLine ("\tsec");
3176 AddCodeLine ("\tsbc\t#$%02X", val & 0xFF);
3183 AddCodeLine ("\tjsr\tdecax%d", (int) val);
3184 } else if (val <= 255) {
3186 AddCodeLine ("\tjsr\tdecaxy");
3188 g_sub (flags | CF_CONST, val);
3195 AddCodeLine ("\tjsr\tdeceaxy");
3197 g_sub (flags | CF_CONST, val);
3210 * Following are the conditional operators. They compare the TOS against
3211 * the primary and put a literal 1 in the primary if the condition is
3212 * true, otherwise they clear the primary register
3217 void g_eq (unsigned flags, unsigned long val)
3218 /* Test for equal */
3220 static char* ops [12] = {
3221 "toseq00", "toseqa0", "toseqax",
3222 "toseq00", "toseqa0", "toseqax",
3227 /* If the right hand side is const, the lhs is not on stack but still
3228 * in the primary register.
3230 if (flags & CF_CONST) {
3232 switch (flags & CF_TYPE) {
3235 if (flags & CF_FORCECHAR) {
3236 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3237 AddCodeLine ("\tjsr\tbooleq");
3243 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
3244 AddCodeLine ("\tbne\t*+4");
3245 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3246 AddCodeLine ("\tjsr\tbooleq");
3256 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3257 * into the normal, non-optimized stuff.
3259 g_push (flags & ~CF_CONST, 0);
3263 /* Use long way over the stack */
3264 oper (flags, val, ops);
3269 void g_ne (unsigned flags, unsigned long val)
3270 /* Test for not equal */
3272 static char* ops [12] = {
3273 "tosne00", "tosnea0", "tosneax",
3274 "tosne00", "tosnea0", "tosneax",
3280 /* If the right hand side is const, the lhs is not on stack but still
3281 * in the primary register.
3283 if (flags & CF_CONST) {
3285 switch (flags & CF_TYPE) {
3288 if (flags & CF_FORCECHAR) {
3289 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3290 AddCodeLine ("\tjsr\tboolne");
3296 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
3297 AddCodeLine ("\tbne\t*+4");
3298 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3299 AddCodeLine ("\tjsr\tboolne");
3309 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3310 * into the normal, non-optimized stuff.
3312 g_push (flags & ~CF_CONST, 0);
3316 /* Use long way over the stack */
3317 oper (flags, val, ops);
3322 void g_lt (unsigned flags, unsigned long val)
3323 /* Test for less than */
3325 static char* ops [12] = {
3326 "toslt00", "toslta0", "tosltax",
3327 "tosult00", "tosulta0", "tosultax",
3332 /* If the right hand side is const, the lhs is not on stack but still
3333 * in the primary register.
3335 if (flags & CF_CONST) {
3337 /* Give a warning in some special cases */
3338 if ((flags & CF_UNSIGNED) && val == 0) {
3339 Warning (WARN_COND_NEVER_TRUE);
3342 /* Look at the type */
3343 switch (flags & CF_TYPE) {
3346 if (flags & CF_FORCECHAR) {
3347 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3348 if (flags & CF_UNSIGNED) {
3349 AddCodeLine ("\tjsr\tboolult");
3351 AddCodeLine ("\tjsr\tboollt");
3358 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3359 /* If we have a signed compare against zero, we only need to
3360 * test the high byte.
3362 AddCodeLine ("\ttxa");
3363 AddCodeLine ("\tjsr\tboollt");
3366 /* Direct code only for unsigned data types */
3367 if (flags & CF_UNSIGNED) {
3368 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
3369 AddCodeLine ("\tbne\t*+4");
3370 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3371 AddCodeLine ("\tjsr\tboolult");
3383 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3384 * into the normal, non-optimized stuff.
3386 g_push (flags & ~CF_CONST, 0);
3390 /* Use long way over the stack */
3391 oper (flags, val, ops);
3396 void g_le (unsigned flags, unsigned long val)
3397 /* Test for less than or equal to */
3399 static char* ops [12] = {
3400 "tosle00", "toslea0", "tosleax",
3401 "tosule00", "tosulea0", "tosuleax",
3407 /* If the right hand side is const, the lhs is not on stack but still
3408 * in the primary register.
3410 if (flags & CF_CONST) {
3412 /* Look at the type */
3413 switch (flags & CF_TYPE) {
3416 if (flags & CF_FORCECHAR) {
3417 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3418 if (flags & CF_UNSIGNED) {
3419 AddCodeLine ("\tjsr\tboolule");
3421 AddCodeLine ("\tjsr\tboolle");
3428 if (flags & CF_UNSIGNED) {
3429 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
3430 AddCodeLine ("\tbne\t*+4");
3431 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3432 AddCodeLine ("\tjsr\tboolule");
3444 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3445 * into the normal, non-optimized stuff.
3447 g_push (flags & ~CF_CONST, 0);
3451 /* Use long way over the stack */
3452 oper (flags, val, ops);
3457 void g_gt (unsigned flags, unsigned long val)
3458 /* Test for greater than */
3460 static char* ops [12] = {
3461 "tosgt00", "tosgta0", "tosgtax",
3462 "tosugt00", "tosugta0", "tosugtax",
3468 /* If the right hand side is const, the lhs is not on stack but still
3469 * in the primary register.
3471 if (flags & CF_CONST) {
3473 /* Look at the type */
3474 switch (flags & CF_TYPE) {
3477 if (flags & CF_FORCECHAR) {
3478 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3479 if (flags & CF_UNSIGNED) {
3480 /* If we have a compare > 0, we will replace it by
3481 * != 0 here, since both are identical but the latter
3482 * is easier to optimize.
3485 AddCodeLine ("\tjsr\tboolugt");
3487 AddCodeLine ("\tjsr\tboolne");
3490 AddCodeLine ("\tjsr\tboolgt");
3497 if (flags & CF_UNSIGNED) {
3498 /* If we have a compare > 0, we will replace it by
3499 * != 0 here, since both are identical but the latter
3500 * is easier to optimize.
3502 if ((val & 0xFFFF) == 0) {
3503 AddCodeLine ("\tstx\ttmp1");
3504 AddCodeLine ("\tora\ttmp1");
3505 AddCodeLine ("\tjsr\tboolne");
3507 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
3508 AddCodeLine ("\tbne\t*+4");
3509 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3510 AddCodeLine ("\tjsr\tboolugt");
3523 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3524 * into the normal, non-optimized stuff.
3526 g_push (flags & ~CF_CONST, 0);
3530 /* Use long way over the stack */
3531 oper (flags, val, ops);
3536 void g_ge (unsigned flags, unsigned long val)
3537 /* Test for greater than or equal to */
3539 static char* ops [12] = {
3540 "tosge00", "tosgea0", "tosgeax",
3541 "tosuge00", "tosugea0", "tosugeax",
3547 /* If the right hand side is const, the lhs is not on stack but still
3548 * in the primary register.
3550 if (flags & CF_CONST) {
3552 /* Give a warning in some special cases */
3553 if ((flags & CF_UNSIGNED) && val == 0) {
3554 Warning (WARN_COND_ALWAYS_TRUE);
3557 /* Look at the type */
3558 switch (flags & CF_TYPE) {
3561 if (flags & CF_FORCECHAR) {
3562 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3563 if (flags & CF_UNSIGNED) {
3564 AddCodeLine ("\tjsr\tbooluge");
3566 AddCodeLine ("\tjsr\tboolge");
3573 if (flags & CF_UNSIGNED) {
3574 AddCodeLine ("\tcpx\t#$%02X", (val >> 8) & 0xFF);
3575 AddCodeLine ("\tbne\t*+4");
3576 AddCodeLine ("\tcmp\t#$%02X", val & 0xFF);
3577 AddCodeLine ("\tjsr\tbooluge");
3589 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3590 * into the normal, non-optimized stuff.
3592 g_push (flags & ~CF_CONST, 0);
3596 /* Use long way over the stack */
3597 oper (flags, val, ops);
3602 /*****************************************************************************/
3603 /* Allocating static storage */
3604 /*****************************************************************************/
3608 void g_res (unsigned n)
3609 /* Reserve static storage, n bytes */
3611 AddCodeLine ("\t.res\t%u,$00", n);
3616 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3617 /* Define data with the size given in flags */
3619 if (flags & CF_CONST) {
3621 /* Numeric constant */
3622 switch (flags & CF_TYPE) {
3625 AddCodeLine ("\t.byte\t$%02lX", val & 0xFF);
3629 AddCodeLine ("\t.word\t$%04lX", val & 0xFFFF);
3633 AddCodeLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3644 /* Create the correct label name */
3645 const char* Label = GetLabelName (flags, val, offs);
3647 /* Labels are always 16 bit */
3648 AddCodeLine ("\t.word\t%s", Label);
3655 void g_defbytes (const unsigned char* Bytes, unsigned Count)
3656 /* Output a row of bytes as a constant */
3662 /* Output the stuff */
3665 /* How many go into this line? */
3666 if ((Chunk = Count) > 16) {
3671 /* Output one line */
3672 strcpy (Buf, "\t.byte\t");
3675 B += sprintf (B, "$%02X", *Bytes++ & 0xFF);
3681 /* Output the line */
3688 void g_zerobytes (unsigned n)
3689 /* Output n bytes of data initialized with zero */
3691 AddCodeLine ("\t.res\t%u,$00", n);
3696 /*****************************************************************************/
3697 /* Inlined known functions */
3698 /*****************************************************************************/
3702 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3703 /* Inline the strlen() function */
3705 /* We need a label in both cases */
3706 unsigned label = GetLabel ();
3708 /* Two different encodings */
3709 if (flags & CF_CONST) {
3711 /* The address of the string is constant. Create the correct label name */
3712 char* lbuf = GetLabelName (flags, val, offs);
3714 /* Generate the strlen code */
3715 AddCodeLine ("\tldy\t#$FF");
3716 g_defloclabel (label);
3717 AddCodeLine ("\tiny");
3718 AddCodeLine ("\tlda\t%s,y", lbuf);
3719 AddCodeLine ("\tbne\tL%04X", label);
3720 AddCodeLine ("\ttya");
3721 AddCodeLine ("\tldx\t#$00");
3725 /* Address not constant but in primary */
3727 /* This is too much code, so call strlen instead of inlining */
3728 AddCodeLine ("\tjsr\t_strlen");
3730 /* Inline the function */
3731 AddCodeLine ("\tsta\tptr1");
3732 AddCodeLine ("\tstx\tptr1+1");
3733 AddCodeLine ("\tldy\t#$FF");
3734 g_defloclabel (label);
3735 AddCodeLine ("\tiny");
3736 AddCodeLine ("\tlda\t(ptr1),y");
3737 AddCodeLine ("\tbne\tL%04X", label);
3738 AddCodeLine ("\ttya");
3739 AddCodeLine ("\tldx\t#$00");