1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
58 /*****************************************************************************/
60 /*****************************************************************************/
64 /* Compiler relative stk ptr */
68 segment_t CurSeg = SEG_INV;
71 static char* SegmentHints [4] = {
72 "seg:code", "seg:rodata", "seg:data", "seg:bss"
77 /*****************************************************************************/
79 /*****************************************************************************/
83 static void typeerror (unsigned type)
84 /* Print an error message about an invalid operand type */
86 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
91 static void CheckLocalOffs (unsigned Offs)
92 /* Check the offset into the stack for 8bit range */
95 /* Too many local vars */
96 AddCodeLine (";*** Too many locals");
97 Error ("Too many local variables");
103 static char* GetLabelName (unsigned flags, unsigned long label, unsigned offs)
105 static char lbuf [128]; /* Label name */
107 /* Create the correct label name */
108 switch (flags & CF_ADDRMASK) {
111 /* Static memory cell */
112 sprintf (lbuf, "L%04X+%u", (unsigned)(label & 0xFFFF), offs);
117 sprintf (lbuf, "_%s+%u", (char*) label, offs);
121 /* Absolute address */
122 sprintf (lbuf, "$%04X", (unsigned)((label+offs) & 0xFFFF));
126 /* Variable in register bank */
127 sprintf (lbuf, "regbank+%u", (unsigned)((label+offs) & 0xFFFF));
131 Internal ("Invalid address flags");
134 /* Return a pointer to the static buffer */
140 /*****************************************************************************/
141 /* Pre- and postamble */
142 /*****************************************************************************/
146 void g_preamble (void)
147 /* Generate the assembler code preamble */
149 AddCodeLine ("; File generated by cc65 v %u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
152 /* Insert some object file options */
153 AddCodeLine (".fopt\t\tcompiler,\"cc65 v %u.%u.%u\"", VER_MAJOR, VER_MINOR, VER_PATCH);
156 /* If we're producing code for some other CPU, switch the command set */
157 if (CPU == CPU_65C02) {
158 AddCodeLine (".pc02");
161 /* Allow auto import for runtime library routines */
162 AddCodeLine (".autoimport\ton");
164 /* Switch the assembler into case sensitive mode */
165 AddCodeLine (".case\t\ton");
167 /* Tell the assembler if we want to generate debug info */
168 AddCodeLine (".debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
170 /* Import the stack pointer for direct auto variable access */
171 AddCodeLine (".importzp\tsp, sreg, regsave, regbank, tmp1, ptr1");
173 /* Define long branch macros */
174 AddCodeLine (".macpack\tlongbranch");
177 /* Define the ldax macro */
178 AddCodeLine (".macro ldax Value");
179 AddCodeLine (" lda #<(Value)");
180 AddCodeLine (" ldx #>(Value)");
181 AddCodeLine (".endmacro");
184 /* Tell the optimizer that this is the end of the preamble */
185 AddCodeHint ("end_of_preamble");
190 void g_postamble (void)
191 /* Generate assembler code postamble */
193 /* Tell the optimizer that this is the start of the postamble */
194 AddCodeHint ("start_of_postamble");
199 /*****************************************************************************/
200 /* Segment support */
201 /*****************************************************************************/
205 static void UseSeg (int NewSeg)
206 /* Switch to a specific segment */
208 if (CurSeg != NewSeg) {
210 AddCodeLine (".segment\t\"%s\"", SegmentNames [CurSeg]);
211 AddCodeHint (SegmentHints [CurSeg]);
217 void g_usecode (void)
218 /* Switch to the code segment */
225 void g_userodata (void)
226 /* Switch to the read only data segment */
233 void g_usedata (void)
234 /* Switch to the data segment */
242 /* Switch to the bss segment */
249 static void SegName (segment_t Seg, const char* Name)
250 /* Set the name of a segment */
252 /* Free the old name and set a new one */
253 NewSegName (Seg, Name);
255 /* If the new segment is the current segment, emit a segment directive
259 CurSeg = SEG_INV; /* Invalidate */
266 void g_codename (const char* Name)
267 /* Set the name of the CODE segment */
269 SegName (SEG_CODE, Name);
274 void g_rodataname (const char* Name)
275 /* Set the name of the RODATA segment */
277 SegName (SEG_RODATA, Name);
282 void g_dataname (const char* Name)
283 /* Set the name of the DATA segment */
285 SegName (SEG_DATA, Name);
290 void g_bssname (const char* Name)
291 /* Set the name of the BSS segment */
293 SegName (SEG_BSS, Name);
298 /*****************************************************************************/
300 /*****************************************************************************/
304 unsigned sizeofarg (unsigned flags)
305 /* Return the size of a function argument type that is encoded in flags */
307 switch (flags & CF_TYPE) {
310 return (flags & CF_FORCECHAR)? 1 : 2;
327 int pop (unsigned flags)
328 /* Pop an argument of the given size */
330 return oursp += sizeofarg (flags);
335 int push (unsigned flags)
336 /* Push an argument of the given size */
338 return oursp -= sizeofarg (flags);
343 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
344 /* The value in Offs is an offset to an address in a/x. Make sure, an object
345 * of the type given in Flags can be loaded or stored into this address by
346 * adding part of the offset to the address in ax, so that the remaining
347 * offset fits into an index register. Return the remaining offset.
350 /* If the offset is too large for a byte register, add the high byte
351 * of the offset to the primary. Beware: We need a special correction
352 * if the offset in the low byte will overflow in the operation.
354 unsigned O = Offs & ~0xFFU;
355 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
356 /* We need to add the low byte also */
360 /* Do the correction if we need one */
362 g_inc (CF_INT | CF_CONST, O);
366 /* Return the new offset */
372 /*****************************************************************************/
373 /* Functions handling local labels */
374 /*****************************************************************************/
378 void g_defloclabel (unsigned label)
379 /* Define a local label */
381 AddCodeLine ("L%04X:", label & 0xFFFF);
386 /*****************************************************************************/
387 /* Functions handling global labels */
388 /*****************************************************************************/
392 void g_defgloblabel (const char* Name)
393 /* Define a global label with the given name */
395 AddCodeLine ("_%s:", Name);
400 void g_defexport (const char* Name, int ZP)
401 /* Export the given label */
404 AddCodeLine ("\t.exportzp\t_%s", Name);
406 AddCodeLine ("\t.export\t\t_%s", Name);
412 void g_defimport (const char* Name, int ZP)
413 /* Import the given label */
416 AddCodeLine ("\t.importzp\t_%s", Name);
418 AddCodeLine ("\t.import\t\t_%s", Name);
424 /*****************************************************************************/
425 /* Load functions for various registers */
426 /*****************************************************************************/
430 static void ldaconst (unsigned val)
431 /* Load a with a constant */
433 AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
438 static void ldxconst (unsigned val)
439 /* Load x with a constant */
441 AddCodeLine ("\tldx\t#$%02X", val & 0xFF);
446 static void ldyconst (unsigned val)
447 /* Load y with a constant */
449 AddCodeLine ("\tldy\t#$%02X", val & 0xFF);
454 /*****************************************************************************/
455 /* Function entry and exit */
456 /*****************************************************************************/
460 /* Remember the argument size of a function. The variable is set by g_enter
461 * and used by g_leave. If the functions gets its argument size by the caller
462 * (variable param list or function without prototype), g_enter will set the
468 void g_enter (unsigned flags, unsigned argsize)
469 /* Function prologue */
471 if ((flags & CF_FIXARGC) != 0) {
472 /* Just remember the argument size for the leave */
476 AddCodeLine ("\tjsr\tenter");
482 void g_leave (int flags, int val)
483 /* Function epilogue */
488 /* CF_REG is set if we're returning a value from the function */
489 if ((flags & CF_REG) == 0) {
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 AddCodeHint ("y:-"); /* Y register no longer used */
509 AddCodeLine ("\trts");
511 AddCodeHint ("y:-"); /* Y register no longer used */
512 AddCodeLine ("\tjmp\tincsp%d", k);
516 AddCodeLine ("\tjmp\taddysp");
521 strcpy (buf, "\tjmp\tleave");
523 /* We've a stack frame to drop */
527 /* Y register no longer used */
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 can handle offsets below $100 directly, larger offsets must be added
1059 * to the address. Since a/x is in use, best code is achieved by adding
1060 * just the high byte. Be sure to check if the low byte will overflow while
1063 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1065 /* Overflow - we need to add the low byte also */
1066 AddCodeLine ("\tldy\t#$00");
1067 AddCodeLine ("\tclc");
1068 AddCodeLine ("\tpha");
1069 AddCodeLine ("\tlda\t#$%02X", Offs & 0xFF);
1070 AddCodeLine ("\tadc\t(sp),y");
1071 AddCodeLine ("\tsta\t(sp),y");
1072 AddCodeLine ("\tiny");
1073 AddCodeLine ("\tlda\t#$%02X", (Offs >> 8) & 0xFF);
1074 AddCodeLine ("\tadc\t(sp),y");
1075 AddCodeLine ("\tsta\t(sp),y");
1076 AddCodeLine ("\tpla");
1078 /* Complete address is on stack, new offset is zero */
1081 } else if ((Offs & 0xFF00) != 0) {
1083 /* We can just add the high byte */
1084 AddCodeLine ("\tldy\t#$01");
1085 AddCodeLine ("\tclc");
1086 AddCodeLine ("\tpha");
1087 AddCodeLine ("\tlda\t#$%02X", (Offs >> 8) & 0xFF);
1088 AddCodeLine ("\tadc\t(sp),y");
1089 AddCodeLine ("\tsta\t(sp),y");
1090 AddCodeLine ("\tpla");
1092 /* Offset is now just the low byte */
1096 /* Check the size and determine operation */
1097 switch (Flags & CF_TYPE) {
1102 AddCodeLine ("\tjsr\tstaspidx");
1104 AddCodeLine ("\tjsr\tstaspp");
1111 AddCodeLine ("\tjsr\tstaxspidx");
1113 AddCodeLine ("\tjsr\tstaxspp");
1120 AddCodeLine ("\tjsr\tsteaxspidx");
1122 AddCodeLine ("\tjsr\tsteaxspp");
1131 /* Pop the argument which is always a pointer */
1137 /*****************************************************************************/
1138 /* type conversion and similiar stuff */
1139 /*****************************************************************************/
1143 void g_toslong (unsigned flags)
1144 /* Make sure, the value on TOS is a long. Convert if necessary */
1146 switch (flags & CF_TYPE) {
1150 if (flags & CF_UNSIGNED) {
1151 AddCodeLine ("\tjsr\ttosulong");
1153 AddCodeLine ("\tjsr\ttoslong");
1168 void g_tosint (unsigned flags)
1169 /* Make sure, the value on TOS is an int. Convert if necessary */
1171 switch (flags & CF_TYPE) {
1178 AddCodeLine ("\tjsr\ttosint");
1189 void g_reglong (unsigned flags)
1190 /* Make sure, the value in the primary register a long. Convert if necessary */
1192 switch (flags & CF_TYPE) {
1196 if (flags & CF_UNSIGNED) {
1198 AddCodeLine ("\tjsr\taxulong");
1201 AddCodeLine ("\tsty\tsreg");
1202 AddCodeLine ("\tsty\tsreg+1");
1205 AddCodeLine ("\tjsr\taxlong");
1219 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1220 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1221 * value, that corresponds to the value on TOS, rhs corresponds to the value
1222 * in (e)ax. The return value is the the flags value for the resulting type.
1225 unsigned ltype, rtype;
1228 /* Get the type spec from the flags */
1229 ltype = lhs & CF_TYPE;
1230 rtype = rhs & CF_TYPE;
1232 /* Check if a conversion is needed */
1233 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1234 /* We must promote the primary register to long */
1236 /* Get the new rhs type */
1237 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1239 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1240 /* We must promote the lhs to long */
1246 /* Get the new rhs type */
1247 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1251 /* Determine the result type for the operation:
1252 * - The result is const if both operands are const.
1253 * - The result is unsigned if one of the operands is unsigned.
1254 * - The result is long if one of the operands is long.
1255 * - Otherwise the result is int sized.
1257 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1258 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1259 if (rtype == CF_LONG || ltype == CF_LONG) {
1269 unsigned g_typecast (unsigned lhs, unsigned rhs)
1270 /* Cast the value in the primary register to the operand size that is flagged
1271 * by the lhs value. Return the result value.
1274 unsigned ltype, rtype;
1276 /* Get the type spec from the flags */
1277 ltype = lhs & CF_TYPE;
1278 rtype = rhs & CF_TYPE;
1280 /* Check if a conversion is needed */
1281 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1282 /* We must promote the primary register to long */
1286 /* Do not need any other action. If the left type is int, and the primary
1287 * register is long, it will be automagically truncated. If the right hand
1288 * side is const, it is not located in the primary register and handled by
1289 * the expression parser code.
1292 /* Result is const if the right hand side was const */
1293 lhs |= (rhs & CF_CONST);
1295 /* The resulting type is that of the left hand side (that's why you called
1303 void g_scale (unsigned flags, long val)
1304 /* Scale the value in the primary register by the given value. If val is positive,
1305 * scale up, is val is negative, scale down. This function is used to scale
1306 * the operands or results of pointer arithmetic by the size of the type, the
1307 * pointer points to.
1312 /* Value may not be zero */
1314 Internal ("Data type has no size");
1315 } else if (val > 0) {
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) {
1326 AddCodeLine ("\tasl\ta");
1333 if (FavourSize || p2 >= 3) {
1334 if (flags & CF_UNSIGNED) {
1335 AddCodeLine ("\tjsr\tshlax%d", p2);
1337 AddCodeLine ("\tjsr\taslax%d", p2);
1340 AddCodeLine ("\tstx\ttmp1");
1342 AddCodeLine ("\tasl\ta");
1343 AddCodeLine ("\trol\ttmp1");
1345 AddCodeLine ("\tldx\ttmp1");
1350 if (flags & CF_UNSIGNED) {
1351 AddCodeLine ("\tjsr\tshleax%d", p2);
1353 AddCodeLine ("\tjsr\tasleax%d", p2);
1362 } else if (val != 1) {
1364 /* Use a multiplication instead */
1365 g_mul (flags | CF_CONST, val);
1373 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1375 /* Factor is 2, 4 or 8, use special function */
1376 switch (flags & CF_TYPE) {
1379 if (flags & CF_FORCECHAR) {
1380 if (flags & CF_UNSIGNED) {
1382 AddCodeLine ("\tlsr\ta");
1385 } else if (p2 <= 2) {
1386 AddCodeLine ("\tcmp\t#$80");
1387 AddCodeLine ("\tror\ta");
1394 if (flags & CF_UNSIGNED) {
1395 if (FavourSize || p2 >= 3) {
1396 AddCodeLine ("\tjsr\tlsrax%d", p2);
1398 AddCodeLine ("\tstx\ttmp1");
1400 AddCodeLine ("\tlsr\ttmp1");
1401 AddCodeLine ("\tror\ta");
1403 AddCodeLine ("\tldx\ttmp1");
1406 if (FavourSize || p2 >= 3) {
1407 AddCodeLine ("\tjsr\tasrax%d", p2);
1409 AddCodeLine ("\tstx\ttmp1");
1411 AddCodeLine ("\tcpx\t#$80");
1412 AddCodeLine ("\tror\ttmp1");
1413 AddCodeLine ("\tror\ta");
1415 AddCodeLine ("\tldx\ttmp1");
1421 if (flags & CF_UNSIGNED) {
1422 AddCodeLine ("\tjsr\tlsreax%d", p2);
1424 AddCodeLine ("\tjsr\tasreax%d", p2);
1433 } else if (val != 1) {
1435 /* Use a division instead */
1436 g_div (flags | CF_CONST, val);
1444 /*****************************************************************************/
1445 /* Adds and subs of variables fix a fixed address */
1446 /*****************************************************************************/
1450 void g_addlocal (unsigned flags, int offs)
1451 /* Add a local variable to ax */
1453 /* Correct the offset and check it */
1455 CheckLocalOffs (offs);
1457 switch (flags & CF_TYPE) {
1460 AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1461 AddCodeLine ("\tclc");
1462 AddCodeLine ("\tadc\t(sp),y");
1463 AddCodeLine ("\tbcc\t*+3");
1464 AddCodeLine ("\tinx");
1465 AddCodeHint ("x:!");
1469 AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1470 AddCodeLine ("\tclc");
1471 AddCodeLine ("\tadc\t(sp),y");
1472 AddCodeLine ("\tpha");
1473 AddCodeLine ("\ttxa");
1474 AddCodeLine ("\tiny");
1475 AddCodeLine ("\tadc\t(sp),y");
1476 AddCodeLine ("\ttax");
1477 AddCodeLine ("\tpla");
1481 /* Do it the old way */
1483 g_getlocal (flags, offs);
1495 void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
1496 /* Add a static variable to ax */
1498 /* Create the correct label name */
1499 char* lbuf = GetLabelName (flags, label, offs);
1501 switch (flags & CF_TYPE) {
1504 AddCodeLine ("\tclc");
1505 AddCodeLine ("\tadc\t%s", lbuf);
1506 AddCodeLine ("\tbcc\t*+3");
1507 AddCodeLine ("\tinx");
1508 AddCodeHint ("x:!");
1512 AddCodeLine ("\tclc");
1513 AddCodeLine ("\tadc\t%s", lbuf);
1514 AddCodeLine ("\ttay");
1515 AddCodeLine ("\ttxa");
1516 AddCodeLine ("\tadc\t%s+1", lbuf);
1517 AddCodeLine ("\ttax");
1518 AddCodeLine ("\ttya");
1522 /* Do it the old way */
1524 g_getstatic (flags, label, offs);
1536 /*****************************************************************************/
1537 /* Compares of ax with a variable with fixed address */
1538 /*****************************************************************************/
1542 void g_cmplocal (unsigned flags, int offs)
1543 /* Compare a local variable to ax */
1545 Internal ("g_cmplocal not implemented");
1550 void g_cmpstatic (unsigned flags, unsigned label, unsigned offs)
1551 /* Compare a static variable to ax */
1553 Internal ("g_cmpstatic not implemented");
1558 /*****************************************************************************/
1559 /* Special op= functions */
1560 /*****************************************************************************/
1564 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
1566 /* Emit += for a static variable */
1568 /* Create the correct label name */
1569 char* lbuf = GetLabelName (flags, label, offs);
1571 /* Check the size and determine operation */
1572 switch (flags & CF_TYPE) {
1575 if (flags & CF_FORCECHAR) {
1576 AddCodeLine ("\tldx\t#$00");
1577 if (flags & CF_CONST) {
1579 AddCodeLine ("\tinc\t%s", lbuf);
1580 AddCodeLine ("\tlda\t%s", lbuf);
1582 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1583 AddCodeLine ("\tclc");
1584 AddCodeLine ("\tadc\t%s", lbuf);
1585 AddCodeLine ("\tsta\t%s", lbuf);
1588 AddCodeLine ("\tclc");
1589 AddCodeLine ("\tadc\t%s", lbuf);
1590 AddCodeLine ("\tsta\t%s", lbuf);
1592 if ((flags & CF_UNSIGNED) == 0) {
1593 AddCodeLine ("\tbpl\t*+3");
1594 AddCodeLine ("\tdex");
1595 AddCodeHint ("x:!"); /* Invalidate X */
1602 if (flags & CF_CONST) {
1604 label = GetLabel ();
1605 AddCodeLine ("\tinc\t%s", lbuf);
1606 AddCodeLine ("\tbne\tL%04X", (int)label);
1607 AddCodeLine ("\tinc\t%s+1", lbuf);
1608 g_defloclabel (label);
1609 AddCodeLine ("\tlda\t%s", lbuf); /* Hmmm... */
1610 AddCodeLine ("\tldx\t%s+1", lbuf);
1612 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1613 AddCodeLine ("\tclc");
1614 AddCodeLine ("\tadc\t%s", lbuf);
1615 AddCodeLine ("\tsta\t%s", lbuf);
1617 label = GetLabel ();
1618 AddCodeLine ("\tbcc\tL%04X", (int)label);
1619 AddCodeLine ("\tinc\t%s+1", lbuf);
1620 g_defloclabel (label);
1621 AddCodeLine ("\tldx\t%s+1", lbuf);
1623 AddCodeLine ("\tlda\t#$%02X", (unsigned char)(val >> 8));
1624 AddCodeLine ("\tadc\t%s+1", lbuf);
1625 AddCodeLine ("\tsta\t%s+1", lbuf);
1626 AddCodeLine ("\ttax");
1627 AddCodeLine ("\tlda\t%s", lbuf);
1631 AddCodeLine ("\tclc");
1632 AddCodeLine ("\tadc\t%s", lbuf);
1633 AddCodeLine ("\tsta\t%s", lbuf);
1634 AddCodeLine ("\ttxa");
1635 AddCodeLine ("\tadc\t%s+1", lbuf);
1636 AddCodeLine ("\tsta\t%s+1", lbuf);
1637 AddCodeLine ("\ttax");
1638 AddCodeLine ("\tlda\t%s", lbuf);
1643 if (flags & CF_CONST) {
1645 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1646 AddCodeLine ("\tsty\tptr1");
1647 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1649 AddCodeLine ("\tjsr\tladdeq1");
1651 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1652 AddCodeLine ("\tjsr\tladdeqa");
1655 g_getstatic (flags, label, offs);
1657 g_putstatic (flags, label, offs);
1660 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1661 AddCodeLine ("\tsty\tptr1");
1662 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1663 AddCodeLine ("\tjsr\tladdeq");
1674 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1675 /* Emit += for a local variable */
1677 /* Calculate the true offset, check it, load it into Y */
1679 CheckLocalOffs (offs);
1681 /* Check the size and determine operation */
1682 switch (flags & CF_TYPE) {
1685 if (flags & CF_FORCECHAR) {
1687 AddCodeLine ("\tldx\t#$00");
1688 if (flags & CF_CONST) {
1689 AddCodeLine ("\tclc");
1690 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1691 AddCodeLine ("\tadc\t(sp,x)");
1692 AddCodeLine ("\tsta\t(sp,x)");
1694 AddCodeLine ("\tclc");
1695 AddCodeLine ("\tadc\t(sp,x)");
1696 AddCodeLine ("\tsta\t(sp,x)");
1700 AddCodeLine ("\tldx\t#$00");
1701 if (flags & CF_CONST) {
1702 AddCodeLine ("\tclc");
1703 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1704 AddCodeLine ("\tadc\t(sp),y");
1705 AddCodeLine ("\tsta\t(sp),y");
1707 AddCodeLine ("\tclc");
1708 AddCodeLine ("\tadc\t(sp),y");
1709 AddCodeLine ("\tsta\t(sp),y");
1712 if ((flags & CF_UNSIGNED) == 0) {
1713 AddCodeLine ("\tbpl\t*+3");
1714 AddCodeLine ("\tdex");
1715 AddCodeHint ("x:!"); /* Invalidate X */
1722 if (flags & CF_CONST) {
1723 g_getimmed (flags, val, 0);
1726 AddCodeLine ("\tjsr\taddeq0sp");
1729 AddCodeLine ("\tjsr\taddeqysp");
1734 if (flags & CF_CONST) {
1735 g_getimmed (flags, val, 0);
1738 AddCodeLine ("\tjsr\tladdeq0sp");
1741 AddCodeLine ("\tjsr\tladdeqysp");
1752 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1753 /* Emit += for the location with address in ax */
1755 /* If the offset is too large for a byte register, add the high byte
1756 * of the offset to the primary. Beware: We need a special correction
1757 * if the offset in the low byte will overflow in the operation.
1759 offs = MakeByteOffs (flags, offs);
1761 /* Check the size and determine operation */
1762 switch (flags & CF_TYPE) {
1765 AddCodeLine ("\tsta\tptr1");
1766 AddCodeLine ("\tstx\tptr1+1");
1768 AddCodeLine ("\tldx\t#$00");
1769 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1770 AddCodeLine ("\tclc");
1771 AddCodeLine ("\tadc\t(ptr1,x)");
1772 AddCodeLine ("\tsta\t(ptr1,x)");
1774 AddCodeLine ("\tldy\t#$%02X", offs);
1775 AddCodeLine ("\tldx\t#$00");
1776 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1777 AddCodeLine ("\tclc");
1778 AddCodeLine ("\tadc\t(ptr1),y");
1779 AddCodeLine ("\tsta\t(ptr1),y");
1785 /* Lots of code, use only if size is not important */
1786 AddCodeLine ("\tsta\tptr1");
1787 AddCodeLine ("\tstx\tptr1+1");
1788 AddCodeLine ("\tldy\t#$%02X", offs);
1789 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1790 AddCodeLine ("\tclc");
1791 AddCodeLine ("\tadc\t(ptr1),y");
1792 AddCodeLine ("\tsta\t(ptr1),y");
1793 AddCodeLine ("\tpha");
1794 AddCodeLine ("\tiny");
1795 AddCodeLine ("\tlda\t#$%02X", (unsigned char)(val >> 8));
1796 AddCodeLine ("\tadc\t(ptr1),y");
1797 AddCodeLine ("\tsta\t(ptr1),y");
1798 AddCodeLine ("\ttax");
1799 AddCodeLine ("\tpla");
1805 AddCodeLine ("\tjsr\tpushax"); /* Push the address */
1806 push (flags); /* Correct the internal sp */
1807 g_getind (flags, offs); /* Fetch the value */
1808 g_inc (flags, val); /* Increment value in primary */
1809 g_putind (flags, offs); /* Store the value back */
1819 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1821 /* Emit -= for a static variable */
1823 /* Create the correct label name */
1824 char* lbuf = GetLabelName (flags, label, offs);
1826 /* Check the size and determine operation */
1827 switch (flags & CF_TYPE) {
1830 if (flags & CF_FORCECHAR) {
1831 AddCodeLine ("\tldx\t#$00");
1832 if (flags & CF_CONST) {
1834 AddCodeLine ("\tdec\t%s", lbuf);
1835 AddCodeLine ("\tlda\t%s", lbuf);
1837 AddCodeLine ("\tsec");
1838 AddCodeLine ("\tlda\t%s", lbuf);
1839 AddCodeLine ("\tsbc\t#$%02X", (int)(val & 0xFF));
1840 AddCodeLine ("\tsta\t%s", lbuf);
1843 AddCodeLine ("\tsec");
1844 AddCodeLine ("\tsta\ttmp1");
1845 AddCodeLine ("\tlda\t%s", lbuf);
1846 AddCodeLine ("\tsbc\ttmp1");
1847 AddCodeLine ("\tsta\t%s", lbuf);
1849 if ((flags & CF_UNSIGNED) == 0) {
1850 AddCodeLine ("\tbpl\t*+3");
1851 AddCodeLine ("\tdex");
1852 AddCodeHint ("x:!"); /* Invalidate X */
1859 AddCodeLine ("\tsec");
1860 if (flags & CF_CONST) {
1861 AddCodeLine ("\tlda\t%s", lbuf);
1862 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
1863 AddCodeLine ("\tsta\t%s", lbuf);
1865 label = GetLabel ();
1866 AddCodeLine ("\tbcs\tL%04X", (unsigned)label);
1867 AddCodeLine ("\tdec\t%s+1", lbuf);
1868 g_defloclabel (label);
1869 AddCodeLine ("\tldx\t%s+1", lbuf);
1871 AddCodeLine ("\tlda\t%s+1", lbuf);
1872 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)(val >> 8));
1873 AddCodeLine ("\tsta\t%s+1", lbuf);
1874 AddCodeLine ("\ttax");
1875 AddCodeLine ("\tlda\t%s", lbuf);
1878 AddCodeLine ("\tsta\ttmp1");
1879 AddCodeLine ("\tlda\t%s", lbuf);
1880 AddCodeLine ("\tsbc\ttmp1");
1881 AddCodeLine ("\tsta\t%s", lbuf);
1882 AddCodeLine ("\tstx\ttmp1");
1883 AddCodeLine ("\tlda\t%s+1", lbuf);
1884 AddCodeLine ("\tsbc\ttmp1");
1885 AddCodeLine ("\tsta\t%s+1", lbuf);
1886 AddCodeLine ("\ttax");
1887 AddCodeLine ("\tlda\t%s", lbuf);
1892 if (flags & CF_CONST) {
1894 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1895 AddCodeLine ("\tsty\tptr1");
1896 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1898 AddCodeLine ("\tjsr\tlsubeq1");
1900 AddCodeLine ("\tlda\t#$%02X", (unsigned char)val);
1901 AddCodeLine ("\tjsr\tlsubeqa");
1904 g_getstatic (flags, label, offs);
1906 g_putstatic (flags, label, offs);
1909 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1910 AddCodeLine ("\tsty\tptr1");
1911 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1912 AddCodeLine ("\tjsr\tlsubeq");
1923 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1924 /* Emit -= for a local variable */
1926 /* Calculate the true offset, check it, load it into Y */
1928 CheckLocalOffs (offs);
1930 /* Check the size and determine operation */
1931 switch (flags & CF_TYPE) {
1934 if (flags & CF_FORCECHAR) {
1936 AddCodeLine ("\tldx\t#$00");
1937 AddCodeLine ("\tsec");
1938 if (flags & CF_CONST) {
1939 AddCodeLine ("\tlda\t(sp),y");
1940 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
1942 AddCodeLine ("\tsta\ttmp1");
1943 AddCodeLine ("\tlda\t(sp),y");
1944 AddCodeLine ("\tsbc\ttmp1");
1946 AddCodeLine ("\tsta\t(sp),y");
1947 if ((flags & CF_UNSIGNED) == 0) {
1948 AddCodeLine ("\tbpl\t*+3");
1949 AddCodeLine ("\tdex");
1950 AddCodeHint ("x:!"); /* Invalidate X */
1957 if (flags & CF_CONST) {
1958 g_getimmed (flags, val, 0);
1961 AddCodeLine ("\tjsr\tsubeq0sp");
1964 AddCodeLine ("\tjsr\tsubeqysp");
1969 if (flags & CF_CONST) {
1970 g_getimmed (flags, val, 0);
1973 AddCodeLine ("\tjsr\tlsubeq0sp");
1976 AddCodeLine ("\tjsr\tlsubeqysp");
1987 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
1988 /* Emit -= for the location with address in ax */
1990 /* If the offset is too large for a byte register, add the high byte
1991 * of the offset to the primary. Beware: We need a special correction
1992 * if the offset in the low byte will overflow in the operation.
1994 offs = MakeByteOffs (flags, offs);
1996 /* Check the size and determine operation */
1997 switch (flags & CF_TYPE) {
2000 AddCodeLine ("\tsta\tptr1");
2001 AddCodeLine ("\tstx\tptr1+1");
2003 AddCodeLine ("\tldx\t#$00");
2004 AddCodeLine ("\tlda\t(ptr1,x)");
2005 AddCodeLine ("\tsec");
2006 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2007 AddCodeLine ("\tsta\t(ptr1,x)");
2009 AddCodeLine ("\tldy\t#$%02X", offs);
2010 AddCodeLine ("\tldx\t#$00");
2011 AddCodeLine ("\tlda\t(ptr1),y");
2012 AddCodeLine ("\tsec");
2013 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2014 AddCodeLine ("\tsta\t(ptr1),y");
2020 /* Lots of code, use only if size is not important */
2021 AddCodeLine ("\tsta\tptr1");
2022 AddCodeLine ("\tstx\tptr1+1");
2023 AddCodeLine ("\tldy\t#$%02X", offs);
2024 AddCodeLine ("\tlda\t(ptr1),y");
2025 AddCodeLine ("\tsec");
2026 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2027 AddCodeLine ("\tsta\t(ptr1),y");
2028 AddCodeLine ("\tpha");
2029 AddCodeLine ("\tiny");
2030 AddCodeLine ("\tlda\t(ptr1),y");
2031 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)(val >> 8));
2032 AddCodeLine ("\tsta\t(ptr1),y");
2033 AddCodeLine ("\ttax");
2034 AddCodeLine ("\tpla");
2040 AddCodeLine ("\tjsr\tpushax"); /* Push the address */
2041 push (flags); /* Correct the internal sp */
2042 g_getind (flags, offs); /* Fetch the value */
2043 g_dec (flags, val); /* Increment value in primary */
2044 g_putind (flags, offs); /* Store the value back */
2054 /*****************************************************************************/
2055 /* Add a variable address to the value in ax */
2056 /*****************************************************************************/
2060 void g_addaddr_local (unsigned flags, int offs)
2061 /* Add the address of a local variable to ax */
2063 /* Add the offset */
2066 /* We cannot address more then 256 bytes of locals anyway */
2067 CheckLocalOffs (offs);
2068 AddCodeLine ("\tclc");
2069 AddCodeLine ("\tadc\t#$%02X", offs & 0xFF);
2070 AddCodeLine ("\tbcc\t*+4"); /* Do also skip the CLC insn below */
2071 AddCodeLine ("\tinx");
2072 AddCodeHint ("x:!"); /* Invalidate X */
2075 /* Add the current stackpointer value */
2076 AddCodeLine ("\tclc");
2077 AddCodeLine ("\tadc\tsp");
2078 AddCodeLine ("\ttay");
2079 AddCodeLine ("\ttxa");
2080 AddCodeLine ("\tadc\tsp+1");
2081 AddCodeLine ("\ttax");
2082 AddCodeLine ("\ttya");
2087 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2088 /* Add the address of a static variable to ax */
2090 /* Create the correct label name */
2091 char* lbuf = GetLabelName (flags, label, offs);
2093 /* Add the address to the current ax value */
2094 AddCodeLine ("\tclc");
2095 AddCodeLine ("\tadc\t#<(%s)", lbuf);
2096 AddCodeLine ("\ttay");
2097 AddCodeLine ("\ttxa");
2098 AddCodeLine ("\tadc\t#>(%s)", lbuf);
2099 AddCodeLine ("\ttax");
2100 AddCodeLine ("\ttya");
2105 /*****************************************************************************/
2107 /*****************************************************************************/
2111 void g_save (unsigned flags)
2112 /* Copy primary register to hold register. */
2114 /* Check the size and determine operation */
2115 switch (flags & CF_TYPE) {
2118 if (flags & CF_FORCECHAR) {
2119 AddCodeLine ("\tpha");
2125 AddCodeLine ("\tsta\tregsave");
2126 AddCodeLine ("\tstx\tregsave+1");
2130 AddCodeLine ("\tjsr\tsaveeax");
2140 void g_restore (unsigned flags)
2141 /* Copy hold register to P. */
2143 /* Check the size and determine operation */
2144 switch (flags & CF_TYPE) {
2147 if (flags & CF_FORCECHAR) {
2148 AddCodeLine ("\tpla");
2154 AddCodeLine ("\tlda\tregsave");
2155 AddCodeLine ("\tldx\tregsave+1");
2159 AddCodeLine ("\tjsr\tresteax");
2169 void g_cmp (unsigned flags, unsigned long val)
2170 /* Immidiate compare. The primary register will not be changed, Z flag
2174 /* Check the size and determine operation */
2175 switch (flags & CF_TYPE) {
2178 if (flags & CF_FORCECHAR) {
2179 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
2185 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
2186 AddCodeLine ("\tbne\t*+4");
2187 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
2191 Internal ("g_cmp: Long compares not implemented");
2201 static void oper (unsigned flags, unsigned long val, char** subs)
2202 /* Encode a binary operation. subs is a pointer to four groups of three
2204 * 0-2 --> Operate on ints
2205 * 3-5 --> Operate on unsigneds
2206 * 6-8 --> Operate on longs
2207 * 9-11 --> Operate on unsigned longs
2209 * The first subroutine names in each string group is used to encode an
2210 * operation with a zero constant, the second to encode an operation with
2211 * a 8 bit constant, and the third is used in all other cases.
2216 /* Determine the offset into the array */
2217 offs = (flags & CF_UNSIGNED)? 3 : 0;
2218 switch (flags & CF_TYPE) {
2231 /* Encode the operation */
2232 if (flags & CF_CONST) {
2233 /* Constant value given */
2234 if (val == 0 && subs [offs+0]) {
2235 /* Special case: constant with value zero */
2236 AddCodeLine ("\tjsr\t%s", subs [offs+0]);
2237 } else if (val < 0x100 && subs [offs+1]) {
2238 /* Special case: constant with high byte zero */
2239 ldaconst (val); /* Load low byte */
2240 AddCodeLine ("\tjsr\t%s", subs [offs+1]);
2242 /* Others: arbitrary constant value */
2243 g_getimmed (flags, val, 0); /* Load value */
2244 AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2247 /* Value not constant (is already in (e)ax) */
2248 AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2251 /* The operation will pop it's argument */
2257 void g_test (unsigned flags)
2258 /* Force a test to set cond codes right */
2260 switch (flags & CF_TYPE) {
2263 if (flags & CF_FORCECHAR) {
2264 AddCodeLine ("\ttax");
2270 AddCodeLine ("\tstx\ttmp1");
2271 AddCodeLine ("\tora\ttmp1");
2275 if (flags & CF_UNSIGNED) {
2276 AddCodeLine ("\tjsr\tutsteax");
2278 AddCodeLine ("\tjsr\ttsteax");
2290 void g_push (unsigned flags, unsigned long val)
2291 /* Push the primary register or a constant value onto the stack */
2295 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2297 /* We have a constant 8 or 16 bit value */
2298 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2300 /* Handle as 8 bit value */
2301 if (FavourSize && val <= 2) {
2302 AddCodeLine ("\tjsr\tpushc%d", (int) val);
2305 AddCodeLine ("\tjsr\tpusha");
2310 /* Handle as 16 bit value */
2311 hi = (unsigned char) (val >> 8);
2313 AddCodeLine ("\tjsr\tpush%u", (unsigned) val);
2314 } else if (hi == 0 || hi == 0xFF) {
2315 /* Use special function */
2317 AddCodeLine ("\tjsr\t%s", (hi == 0)? "pusha0" : "pushaFF");
2320 g_getimmed (flags, val, 0);
2321 AddCodeLine ("\tjsr\tpushax");
2327 /* Value is not 16 bit or not constant */
2328 if (flags & CF_CONST) {
2329 /* Constant 32 bit value, load into eax */
2330 g_getimmed (flags, val, 0);
2333 /* Push the primary register */
2334 switch (flags & CF_TYPE) {
2337 if (flags & CF_FORCECHAR) {
2338 /* Handle as char */
2339 AddCodeLine ("\tjsr\tpusha");
2344 AddCodeLine ("\tjsr\tpushax");
2348 AddCodeLine ("\tjsr\tpusheax");
2358 /* Adjust the stack offset */
2364 void g_swap (unsigned flags)
2365 /* Swap the primary register and the top of the stack. flags give the type
2366 * of *both* values (must have same size).
2369 switch (flags & CF_TYPE) {
2373 AddCodeLine ("\tjsr\tswapstk");
2377 AddCodeLine ("\tjsr\tswapestk");
2388 void g_call (unsigned flags, char* lbl, unsigned argsize)
2389 /* Call the specified subroutine name */
2391 if ((flags & CF_FIXARGC) == 0) {
2392 /* Pass arg count */
2395 AddCodeLine ("\tjsr\t_%s", lbl);
2396 oursp += argsize; /* callee pops args */
2401 void g_callind (unsigned flags, unsigned argsize)
2402 /* Call subroutine with address in AX */
2404 if ((flags & CF_FIXARGC) == 0) {
2405 /* Pass arg count */
2408 AddCodeLine ("\tjsr\tcallax"); /* do the call */
2409 oursp += argsize; /* callee pops args */
2414 void g_jump (unsigned label)
2415 /* Jump to specified internal label number */
2417 AddCodeLine ("\tjmp\tL%04X", label);
2422 void g_switch (unsigned flags)
2423 /* Output switch statement preample */
2425 switch (flags & CF_TYPE) {
2429 AddCodeLine ("\tjsr\tswitch");
2433 AddCodeLine ("\tjsr\tlswitch");
2444 void g_case (unsigned flags, unsigned label, unsigned long val)
2445 /* Create table code for one case selector */
2447 switch (flags & CF_TYPE) {
2451 AddCodeLine ("\t.word\t$%04X, L%04X",
2452 (unsigned)(val & 0xFFFF),
2453 (unsigned)(label & 0xFFFF));
2457 AddCodeLine ("\t.dword\t$%08lX", val);
2458 AddCodeLine ("\t.word\tL%04X", label & 0xFFFF);
2469 void g_truejump (unsigned flags, unsigned label)
2470 /* Jump to label if zero flag clear */
2472 if (flags & CF_SHORT) {
2473 AddCodeLine ("\tbne\tL%04X", label);
2475 AddCodeLine ("\tjne\tL%04X", label);
2481 void g_falsejump (unsigned flags, unsigned label)
2482 /* Jump to label if zero flag set */
2484 if (flags & CF_SHORT) {
2485 AddCodeLine ("\tbeq\tL%04X", label);
2487 AddCodeLine ("\tjeq\tL%04X", label);
2493 static void mod_internal (int k, char* verb1, char* verb2)
2496 AddCodeLine ("\tjsr\t%ssp%c", verb1, k + '0');
2500 AddCodeLine ("\tjsr\t%ssp", verb2);
2506 void g_space (int space)
2507 /* Create or drop space on the stack */
2510 mod_internal (-space, "inc", "addy");
2511 } else if (space > 0) {
2512 mod_internal (space, "dec", "suby");
2518 void g_cstackcheck (void)
2519 /* Check for a C stack overflow */
2521 AddCodeLine ("\tjsr\tcstkchk");
2526 void g_stackcheck (void)
2527 /* Check for a stack overflow */
2529 AddCodeLine ("\tjsr\tstkchk");
2534 void g_add (unsigned flags, unsigned long val)
2535 /* Primary = TOS + Primary */
2537 static char* ops [12] = {
2538 0, "tosadda0", "tosaddax",
2539 0, "tosadda0", "tosaddax",
2544 if (flags & CF_CONST) {
2545 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2546 g_push (flags & ~CF_CONST, 0);
2548 oper (flags, val, ops);
2553 void g_sub (unsigned flags, unsigned long val)
2554 /* Primary = TOS - Primary */
2556 static char* ops [12] = {
2557 0, "tossuba0", "tossubax",
2558 0, "tossuba0", "tossubax",
2563 if (flags & CF_CONST) {
2564 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2565 g_push (flags & ~CF_CONST, 0);
2567 oper (flags, val, ops);
2572 void g_rsub (unsigned flags, unsigned long val)
2573 /* Primary = Primary - TOS */
2575 static char* ops [12] = {
2576 0, "tosrsuba0", "tosrsubax",
2577 0, "tosrsuba0", "tosrsubax",
2581 oper (flags, val, ops);
2586 void g_mul (unsigned flags, unsigned long val)
2587 /* Primary = TOS * Primary */
2589 static char* ops [12] = {
2590 0, "tosmula0", "tosmulax",
2591 0, "tosumula0", "tosumulax",
2598 /* Do strength reduction if the value is constant and a power of two */
2599 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2600 /* Generate a shift instead */
2605 /* If the right hand side is const, the lhs is not on stack but still
2606 * in the primary register.
2608 if (flags & CF_CONST) {
2610 switch (flags & CF_TYPE) {
2613 if (flags & CF_FORCECHAR) {
2614 /* Handle some special cases */
2618 AddCodeLine ("\tsta\ttmp1");
2619 AddCodeLine ("\tasl\ta");
2620 AddCodeLine ("\tclc");
2621 AddCodeLine ("\tadc\ttmp1");
2625 AddCodeLine ("\tsta\ttmp1");
2626 AddCodeLine ("\tasl\ta");
2627 AddCodeLine ("\tasl\ta");
2628 AddCodeLine ("\tclc");
2629 AddCodeLine ("\tadc\ttmp1");
2633 AddCodeLine ("\tsta\ttmp1");
2634 AddCodeLine ("\tasl\ta");
2635 AddCodeLine ("\tasl\ta");
2636 AddCodeLine ("\tclc");
2637 AddCodeLine ("\tadc\ttmp1");
2638 AddCodeLine ("\tasl\ta");
2654 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2655 * into the normal, non-optimized stuff.
2657 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2658 g_push (flags & ~CF_CONST, 0);
2662 /* Use long way over the stack */
2663 oper (flags, val, ops);
2668 void g_div (unsigned flags, unsigned long val)
2669 /* Primary = TOS / Primary */
2671 static char* ops [12] = {
2672 0, "tosdiva0", "tosdivax",
2673 0, "tosudiva0", "tosudivax",
2678 /* Do strength reduction if the value is constant and a power of two */
2680 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2681 /* Generate a shift instead */
2684 /* Generate a division */
2685 if (flags & CF_CONST) {
2686 /* lhs is not on stack */
2687 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2688 g_push (flags & ~CF_CONST, 0);
2690 oper (flags, val, ops);
2696 void g_mod (unsigned flags, unsigned long val)
2697 /* Primary = TOS % Primary */
2699 static char* ops [12] = {
2700 0, "tosmoda0", "tosmodax",
2701 0, "tosumoda0", "tosumodax",
2707 /* Check if we can do some cost reduction */
2708 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2709 /* We can do that with an AND operation */
2710 g_and (flags, val - 1);
2712 /* Do it the hard way... */
2713 if (flags & CF_CONST) {
2714 /* lhs is not on stack */
2715 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2716 g_push (flags & ~CF_CONST, 0);
2718 oper (flags, val, ops);
2724 void g_or (unsigned flags, unsigned long val)
2725 /* Primary = TOS | Primary */
2727 static char* ops [12] = {
2728 0, "tosora0", "tosorax",
2729 0, "tosora0", "tosorax",
2734 /* If the right hand side is const, the lhs is not on stack but still
2735 * in the primary register.
2737 if (flags & CF_CONST) {
2739 switch (flags & CF_TYPE) {
2742 if (flags & CF_FORCECHAR) {
2743 if ((val & 0xFF) != 0xFF) {
2744 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2752 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2759 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2768 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2769 * into the normal, non-optimized stuff.
2771 g_push (flags & ~CF_CONST, 0);
2775 /* Use long way over the stack */
2776 oper (flags, val, ops);
2781 void g_xor (unsigned flags, unsigned long val)
2782 /* Primary = TOS ^ Primary */
2784 static char* ops [12] = {
2785 0, "tosxora0", "tosxorax",
2786 0, "tosxora0", "tosxorax",
2792 /* If the right hand side is const, the lhs is not on stack but still
2793 * in the primary register.
2795 if (flags & CF_CONST) {
2797 switch (flags & CF_TYPE) {
2800 if (flags & CF_FORCECHAR) {
2801 if ((val & 0xFF) != 0) {
2802 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2811 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2814 } else if ((val & 0xFF) == 0) {
2815 AddCodeLine ("\tpha");
2816 AddCodeLine ("\ttxa");
2817 AddCodeLine ("\teor\t#$%02X", (unsigned char)(val >> 8));
2818 AddCodeLine ("\ttax");
2819 AddCodeLine ("\tpla");
2827 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2837 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2838 * into the normal, non-optimized stuff.
2840 g_push (flags & ~CF_CONST, 0);
2844 /* Use long way over the stack */
2845 oper (flags, val, ops);
2850 void g_and (unsigned flags, unsigned long val)
2851 /* Primary = TOS & Primary */
2853 static char* ops [12] = {
2854 0, "tosanda0", "tosandax",
2855 0, "tosanda0", "tosandax",
2860 /* If the right hand side is const, the lhs is not on stack but still
2861 * in the primary register.
2863 if (flags & CF_CONST) {
2865 switch (flags & CF_TYPE) {
2868 if (flags & CF_FORCECHAR) {
2869 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2874 if ((val & 0xFFFF) != 0xFFFF) {
2879 } else if (val != 0xFF) {
2880 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2882 } else if ((val & 0xFF00) == 0xFF00) {
2883 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2884 } else if ((val & 0x00FF) == 0x0000) {
2885 AddCodeLine ("\ttxa");
2886 AddCodeLine ("\tand\t#$%02X", (unsigned char)(val >> 8));
2887 AddCodeLine ("\ttax");
2890 AddCodeLine ("\ttay");
2891 AddCodeLine ("\ttxa");
2892 AddCodeLine ("\tand\t#$%02X", (unsigned char)(val >> 8));
2893 AddCodeLine ("\ttax");
2894 AddCodeLine ("\ttya");
2895 if ((val & 0x00FF) != 0x00FF) {
2896 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2905 AddCodeLine ("\tstx\tsreg+1");
2906 AddCodeLine ("\tstx\tsreg");
2907 if ((val & 0xFF) != 0xFF) {
2908 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2911 } else if (val == 0xFF00) {
2913 AddCodeLine ("\tsta\tsreg+1");
2914 AddCodeLine ("\tsta\tsreg");
2923 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2924 * into the normal, non-optimized stuff.
2926 g_push (flags & ~CF_CONST, 0);
2930 /* Use long way over the stack */
2931 oper (flags, val, ops);
2936 void g_asr (unsigned flags, unsigned long val)
2937 /* Primary = TOS >> Primary */
2939 static char* ops [12] = {
2940 0, "tosasra0", "tosasrax",
2941 0, "tosshra0", "tosshrax",
2946 /* If the right hand side is const, the lhs is not on stack but still
2947 * in the primary register.
2949 if (flags & CF_CONST) {
2951 switch (flags & CF_TYPE) {
2955 if (val >= 1 && val <= 3) {
2956 if (flags & CF_UNSIGNED) {
2957 AddCodeLine ("\tjsr\tshrax%ld", val);
2959 AddCodeLine ("\tjsr\tasrax%ld", val);
2962 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2963 AddCodeLine ("\ttxa");
2970 if (val >= 1 && val <= 3) {
2971 if (flags & CF_UNSIGNED) {
2972 AddCodeLine ("\tjsr\tshreax%ld", val);
2974 AddCodeLine ("\tjsr\tasreax%ld", val);
2977 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2978 AddCodeLine ("\ttxa");
2979 AddCodeLine ("\tldx\tsreg");
2980 AddCodeLine ("\tldy\tsreg+1");
2981 AddCodeLine ("\tsty\tsreg");
2982 AddCodeLine ("\tldy\t#$00");
2983 AddCodeLine ("\tsty\tsreg+1");
2985 } else if (val == 16) {
2986 AddCodeLine ("\tldy\t#$00");
2987 AddCodeLine ("\tldx\tsreg+1");
2988 if ((flags & CF_UNSIGNED) == 0) {
2989 AddCodeLine ("\tbpl\t*+3");
2990 AddCodeLine ("\tdey");
2991 AddCodeHint ("y:!");
2993 AddCodeLine ("\tlda\tsreg");
2994 AddCodeLine ("\tsty\tsreg+1");
2995 AddCodeLine ("\tsty\tsreg");
3004 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3005 * into the normal, non-optimized stuff.
3007 g_push (flags & ~CF_CONST, 0);
3011 /* Use long way over the stack */
3012 oper (flags, val, ops);
3017 void g_asl (unsigned flags, unsigned long val)
3018 /* Primary = TOS << Primary */
3020 static char* ops [12] = {
3021 0, "tosasla0", "tosaslax",
3022 0, "tosshla0", "tosshlax",
3028 /* If the right hand side is const, the lhs is not on stack but still
3029 * in the primary register.
3031 if (flags & CF_CONST) {
3033 switch (flags & CF_TYPE) {
3037 if (val >= 1 && val <= 3) {
3038 if (flags & CF_UNSIGNED) {
3039 AddCodeLine ("\tjsr\tshlax%ld", val);
3041 AddCodeLine ("\tjsr\taslax%ld", val);
3044 } else if (val == 8) {
3045 AddCodeLine ("\ttax");
3046 AddCodeLine ("\tlda\t#$00");
3052 if (val >= 1 && val <= 3) {
3053 if (flags & CF_UNSIGNED) {
3054 AddCodeLine ("\tjsr\tshleax%ld", val);
3056 AddCodeLine ("\tjsr\tasleax%ld", val);
3059 } else if (val == 8) {
3060 AddCodeLine ("\tldy\tsreg");
3061 AddCodeLine ("\tsty\tsreg+1");
3062 AddCodeLine ("\tstx\tsreg");
3063 AddCodeLine ("\ttax");
3064 AddCodeLine ("\tlda\t#$00");
3066 } else if (val == 16) {
3067 AddCodeLine ("\tstx\tsreg+1");
3068 AddCodeLine ("\tsta\tsreg");
3069 AddCodeLine ("\tlda\t#$00");
3070 AddCodeLine ("\ttax");
3079 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3080 * into the normal, non-optimized stuff.
3082 g_push (flags & ~CF_CONST, 0);
3086 /* Use long way over the stack */
3087 oper (flags, val, ops);
3092 void g_neg (unsigned flags)
3093 /* Primary = -Primary */
3095 switch (flags & CF_TYPE) {
3099 AddCodeLine ("\tjsr\tnegax");
3103 AddCodeLine ("\tjsr\tnegeax");
3113 void g_bneg (unsigned flags)
3114 /* Primary = !Primary */
3116 switch (flags & CF_TYPE) {
3119 AddCodeLine ("\tjsr\tbnega");
3123 AddCodeLine ("\tjsr\tbnegax");
3127 AddCodeLine ("\tjsr\tbnegeax");
3137 void g_com (unsigned flags)
3138 /* Primary = ~Primary */
3140 switch (flags & CF_TYPE) {
3144 AddCodeLine ("\tjsr\tcomplax");
3148 AddCodeLine ("\tjsr\tcompleax");
3158 void g_inc (unsigned flags, unsigned long val)
3159 /* Increment the primary register by a given number */
3161 /* Don't inc by zero */
3166 /* Generate code for the supported types */
3168 switch (flags & CF_TYPE) {
3171 if (flags & CF_FORCECHAR) {
3172 if (CPU == CPU_65C02 && val <= 2) {
3174 AddCodeLine ("\tina");
3177 AddCodeLine ("\tclc");
3178 AddCodeLine ("\tadc\t#$%02X", (unsigned char)val);
3185 if (CPU == CPU_65C02 && val == 1) {
3186 AddCodeLine ("\tina");
3187 AddCodeLine ("\tbne\t*+3");
3188 AddCodeLine ("\tinx");
3189 /* Tell the optimizer that the X register may be invalid */
3190 AddCodeHint ("x:!");
3191 } else if (FavourSize) {
3194 AddCodeLine ("\tjsr\tincax%lu", val);
3195 } else if (val <= 255) {
3197 AddCodeLine ("\tjsr\tincaxy");
3199 g_add (flags | CF_CONST, val);
3202 /* Inline the code */
3204 if ((val & 0xFF) != 0) {
3205 AddCodeLine ("\tclc");
3206 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3207 AddCodeLine ("\tbcc\t*+3");
3208 AddCodeLine ("\tinx");
3209 /* Tell the optimizer that the X register may be invalid */
3210 AddCodeHint ("x:!");
3213 AddCodeLine ("\tinx");
3216 AddCodeLine ("\tinx");
3219 AddCodeLine ("\tclc");
3220 if ((val & 0xFF) != 0) {
3221 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3222 /* Tell the optimizer that the X register may be invalid */
3223 AddCodeHint ("x:!");
3225 AddCodeLine ("\tpha");
3226 AddCodeLine ("\ttxa");
3227 AddCodeLine ("\tadc\t#$%02X", (unsigned char) (val >> 8));
3228 AddCodeLine ("\ttax");
3229 AddCodeLine ("\tpla");
3237 AddCodeLine ("\tjsr\tinceaxy");
3239 g_add (flags | CF_CONST, val);
3251 void g_dec (unsigned flags, unsigned long val)
3252 /* Decrement the primary register by a given number */
3254 /* Don't dec by zero */
3259 /* Generate code for the supported types */
3261 switch (flags & CF_TYPE) {
3264 if (flags & CF_FORCECHAR) {
3265 if (CPU == CPU_65C02 && val <= 2) {
3267 AddCodeLine ("\tdea");
3270 AddCodeLine ("\tsec");
3271 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
3279 AddCodeLine ("\tjsr\tdecax%d", (int) val);
3280 } else if (val <= 255) {
3282 AddCodeLine ("\tjsr\tdecaxy");
3284 g_sub (flags | CF_CONST, val);
3291 AddCodeLine ("\tjsr\tdeceaxy");
3293 g_sub (flags | CF_CONST, val);
3306 * Following are the conditional operators. They compare the TOS against
3307 * the primary and put a literal 1 in the primary if the condition is
3308 * true, otherwise they clear the primary register
3313 void g_eq (unsigned flags, unsigned long val)
3314 /* Test for equal */
3316 static char* ops [12] = {
3317 "toseq00", "toseqa0", "toseqax",
3318 "toseq00", "toseqa0", "toseqax",
3323 /* If the right hand side is const, the lhs is not on stack but still
3324 * in the primary register.
3326 if (flags & CF_CONST) {
3328 switch (flags & CF_TYPE) {
3331 if (flags & CF_FORCECHAR) {
3332 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3333 AddCodeLine ("\tjsr\tbooleq");
3339 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3340 AddCodeLine ("\tbne\t*+4");
3341 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3342 AddCodeLine ("\tjsr\tbooleq");
3352 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3353 * into the normal, non-optimized stuff.
3355 g_push (flags & ~CF_CONST, 0);
3359 /* Use long way over the stack */
3360 oper (flags, val, ops);
3365 void g_ne (unsigned flags, unsigned long val)
3366 /* Test for not equal */
3368 static char* ops [12] = {
3369 "tosne00", "tosnea0", "tosneax",
3370 "tosne00", "tosnea0", "tosneax",
3376 /* If the right hand side is const, the lhs is not on stack but still
3377 * in the primary register.
3379 if (flags & CF_CONST) {
3381 switch (flags & CF_TYPE) {
3384 if (flags & CF_FORCECHAR) {
3385 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3386 AddCodeLine ("\tjsr\tboolne");
3392 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3393 AddCodeLine ("\tbne\t*+4");
3394 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3395 AddCodeLine ("\tjsr\tboolne");
3405 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3406 * into the normal, non-optimized stuff.
3408 g_push (flags & ~CF_CONST, 0);
3412 /* Use long way over the stack */
3413 oper (flags, val, ops);
3418 void g_lt (unsigned flags, unsigned long val)
3419 /* Test for less than */
3421 static char* ops [12] = {
3422 "toslt00", "toslta0", "tosltax",
3423 "tosult00", "tosulta0", "tosultax",
3428 /* If the right hand side is const, the lhs is not on stack but still
3429 * in the primary register.
3431 if (flags & CF_CONST) {
3433 /* Give a warning in some special cases */
3434 if ((flags & CF_UNSIGNED) && val == 0) {
3435 Warning ("Condition is never true");
3438 /* Look at the type */
3439 switch (flags & CF_TYPE) {
3442 if (flags & CF_FORCECHAR) {
3443 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3444 if (flags & CF_UNSIGNED) {
3445 AddCodeLine ("\tjsr\tboolult");
3447 AddCodeLine ("\tjsr\tboollt");
3454 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3455 /* If we have a signed compare against zero, we only need to
3456 * test the high byte.
3458 AddCodeLine ("\ttxa");
3459 AddCodeLine ("\tjsr\tboollt");
3462 /* Direct code only for unsigned data types */
3463 if (flags & CF_UNSIGNED) {
3464 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3465 AddCodeLine ("\tbne\t*+4");
3466 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3467 AddCodeLine ("\tjsr\tboolult");
3479 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3480 * into the normal, non-optimized stuff.
3482 g_push (flags & ~CF_CONST, 0);
3486 /* Use long way over the stack */
3487 oper (flags, val, ops);
3492 void g_le (unsigned flags, unsigned long val)
3493 /* Test for less than or equal to */
3495 static char* ops [12] = {
3496 "tosle00", "toslea0", "tosleax",
3497 "tosule00", "tosulea0", "tosuleax",
3503 /* If the right hand side is const, the lhs is not on stack but still
3504 * in the primary register.
3506 if (flags & CF_CONST) {
3508 /* Look at the type */
3509 switch (flags & CF_TYPE) {
3512 if (flags & CF_FORCECHAR) {
3513 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3514 if (flags & CF_UNSIGNED) {
3515 AddCodeLine ("\tjsr\tboolule");
3517 AddCodeLine ("\tjsr\tboolle");
3524 if (flags & CF_UNSIGNED) {
3525 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3526 AddCodeLine ("\tbne\t*+4");
3527 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3528 AddCodeLine ("\tjsr\tboolule");
3540 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3541 * into the normal, non-optimized stuff.
3543 g_push (flags & ~CF_CONST, 0);
3547 /* Use long way over the stack */
3548 oper (flags, val, ops);
3553 void g_gt (unsigned flags, unsigned long val)
3554 /* Test for greater than */
3556 static char* ops [12] = {
3557 "tosgt00", "tosgta0", "tosgtax",
3558 "tosugt00", "tosugta0", "tosugtax",
3564 /* If the right hand side is const, the lhs is not on stack but still
3565 * in the primary register.
3567 if (flags & CF_CONST) {
3569 /* Look at the type */
3570 switch (flags & CF_TYPE) {
3573 if (flags & CF_FORCECHAR) {
3574 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3575 if (flags & CF_UNSIGNED) {
3576 /* If we have a compare > 0, we will replace it by
3577 * != 0 here, since both are identical but the latter
3578 * is easier to optimize.
3581 AddCodeLine ("\tjsr\tboolugt");
3583 AddCodeLine ("\tjsr\tboolne");
3586 AddCodeLine ("\tjsr\tboolgt");
3593 if (flags & CF_UNSIGNED) {
3594 /* If we have a compare > 0, we will replace it by
3595 * != 0 here, since both are identical but the latter
3596 * is easier to optimize.
3598 if ((val & 0xFFFF) == 0) {
3599 AddCodeLine ("\tstx\ttmp1");
3600 AddCodeLine ("\tora\ttmp1");
3601 AddCodeLine ("\tjsr\tboolne");
3603 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3604 AddCodeLine ("\tbne\t*+4");
3605 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3606 AddCodeLine ("\tjsr\tboolugt");
3619 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3620 * into the normal, non-optimized stuff.
3622 g_push (flags & ~CF_CONST, 0);
3626 /* Use long way over the stack */
3627 oper (flags, val, ops);
3632 void g_ge (unsigned flags, unsigned long val)
3633 /* Test for greater than or equal to */
3635 static char* ops [12] = {
3636 "tosge00", "tosgea0", "tosgeax",
3637 "tosuge00", "tosugea0", "tosugeax",
3643 /* If the right hand side is const, the lhs is not on stack but still
3644 * in the primary register.
3646 if (flags & CF_CONST) {
3648 /* Give a warning in some special cases */
3649 if ((flags & CF_UNSIGNED) && val == 0) {
3650 Warning ("Condition is always true");
3653 /* Look at the type */
3654 switch (flags & CF_TYPE) {
3657 if (flags & CF_FORCECHAR) {
3658 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3659 if (flags & CF_UNSIGNED) {
3660 AddCodeLine ("\tjsr\tbooluge");
3662 AddCodeLine ("\tjsr\tboolge");
3669 if (flags & CF_UNSIGNED) {
3670 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3671 AddCodeLine ("\tbne\t*+4");
3672 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3673 AddCodeLine ("\tjsr\tbooluge");
3685 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3686 * into the normal, non-optimized stuff.
3688 g_push (flags & ~CF_CONST, 0);
3692 /* Use long way over the stack */
3693 oper (flags, val, ops);
3698 /*****************************************************************************/
3699 /* Allocating static storage */
3700 /*****************************************************************************/
3704 void g_res (unsigned n)
3705 /* Reserve static storage, n bytes */
3707 AddCodeLine ("\t.res\t%u,$00", n);
3712 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3713 /* Define data with the size given in flags */
3715 if (flags & CF_CONST) {
3717 /* Numeric constant */
3718 switch (flags & CF_TYPE) {
3721 AddCodeLine ("\t.byte\t$%02lX", val & 0xFF);
3725 AddCodeLine ("\t.word\t$%04lX", val & 0xFFFF);
3729 AddCodeLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3740 /* Create the correct label name */
3741 const char* Label = GetLabelName (flags, val, offs);
3743 /* Labels are always 16 bit */
3744 AddCodeLine ("\t.word\t%s", Label);
3751 void g_defbytes (const unsigned char* Bytes, unsigned Count)
3752 /* Output a row of bytes as a constant */
3758 /* Output the stuff */
3761 /* How many go into this line? */
3762 if ((Chunk = Count) > 16) {
3767 /* Output one line */
3768 strcpy (Buf, "\t.byte\t");
3771 B += sprintf (B, "$%02X", *Bytes++ & 0xFF);
3777 /* Output the line */
3784 void g_zerobytes (unsigned n)
3785 /* Output n bytes of data initialized with zero */
3787 AddCodeLine ("\t.res\t%u,$00", n);
3792 /*****************************************************************************/
3793 /* Inlined known functions */
3794 /*****************************************************************************/
3798 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3799 /* Inline the strlen() function */
3801 /* We need a label in both cases */
3802 unsigned label = GetLabel ();
3804 /* Two different encodings */
3805 if (flags & CF_CONST) {
3807 /* The address of the string is constant. Create the correct label name */
3808 char* lbuf = GetLabelName (flags, val, offs);
3810 /* Generate the strlen code */
3811 AddCodeLine ("\tldy\t#$FF");
3812 g_defloclabel (label);
3813 AddCodeLine ("\tiny");
3814 AddCodeLine ("\tlda\t%s,y", lbuf);
3815 AddCodeLine ("\tbne\tL%04X", label);
3816 AddCodeLine ("\ttax");
3817 AddCodeLine ("\ttya");
3821 /* Address not constant but in primary */
3823 /* This is too much code, so call strlen instead of inlining */
3824 AddCodeLine ("\tjsr\t_strlen");
3826 /* Inline the function */
3827 AddCodeLine ("\tsta\tptr1");
3828 AddCodeLine ("\tstx\tptr1+1");
3829 AddCodeLine ("\tldy\t#$FF");
3830 g_defloclabel (label);
3831 AddCodeLine ("\tiny");
3832 AddCodeLine ("\tlda\t(ptr1),y");
3833 AddCodeLine ("\tbne\tL%04X", label);
3834 AddCodeLine ("\ttax");
3835 AddCodeLine ("\ttya");