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) {
209 CurSeg = (segment_t) 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 && CodeSizeFactor > 140) {
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");
823 if (CodeSizeFactor > 180) {
825 AddCodeLine ("\tlda\t(sp),y");
826 AddCodeLine ("\ttax");
827 AddCodeLine ("\tdey");
828 AddCodeLine ("\tlda\t(sp),y");
832 AddCodeLine ("\tjsr\tldaxysp");
834 AddCodeLine ("\tjsr\tldax0sp");
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) {
881 if (CodeSizeFactor > 250) {
882 AddCodeLine ("\tsta\tptr1");
883 AddCodeLine ("\tstx\tptr1+1");
884 AddCodeLine ("\tldx\t#$00");
885 AddCodeLine ("\tlda\t(ptr1,x)");
887 AddCodeLine ("\tjsr\tldaui");
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");
944 if (CodeSizeFactor < 300) {
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 void g_leavariadic (int Offs)
971 /* Fetch the address of a parameter in a variadic function into the primary
975 unsigned ArgSizeOffs;
977 /* Calculate the offset relative to sp */
980 /* Get the offset of the parameter which is stored at sp+0 on function
981 * entry and check if this offset is reachable with a byte offset.
984 ArgSizeOffs = -oursp;
985 CheckLocalOffs (ArgSizeOffs);
987 /* Get the stack pointer plus offset. Clear the carry as the result of
991 AddCodeLine ("\tclc");
992 AddCodeLine ("\tlda\tsp");
993 AddCodeLine ("\tadc\t#$%02X", Offs & 0xFF);
995 AddCodeLine ("\tpha");
996 AddCodeLine ("\tlda\tsp+1");
997 AddCodeLine ("\tadc\t#$%02X", (Offs >> 8) & 0xFF);
998 AddCodeLine ("\ttax");
999 AddCodeLine ("\tpla");
1000 AddCodeLine ("\tclc");
1002 AddCodeLine ("\tldx\tsp+1");
1003 AddCodeLine ("\tbcc\t*+4"); /* Jump over the clc */
1004 AddCodeLine ("\tinx");
1005 AddCodeHint ("x:!"); /* Invalidate X */
1006 AddCodeLine ("\tclc");
1008 } else if (Offs < 0) {
1010 AddCodeLine ("\tsec");
1011 AddCodeLine ("\tlda\tsp");
1012 AddCodeLine ("\tsbc\t#$%02X", Offs & 0xFF);
1014 AddCodeLine ("\tpha");
1015 AddCodeLine ("\tlda\tsp+1");
1016 AddCodeLine ("\tsbc\t#$%02X", (Offs >> 8) & 0xFF);
1017 AddCodeLine ("\ttax");
1018 AddCodeLine ("\tpla");
1020 AddCodeLine ("\tldx\tsp+1");
1021 AddCodeLine ("\tbcs\t*+3");
1022 AddCodeLine ("\tdex");
1023 AddCodeHint ("x:!"); /* Invalidate X */
1025 AddCodeLine ("\tclc");
1027 AddCodeLine ("\tlda\tsp");
1028 AddCodeLine ("\tldx\tsp+1");
1029 AddCodeLine ("\tclc");
1032 /* Add the size of all parameters. Carry is clear on entry. */
1033 if (ArgSizeOffs == 0 && CPU == CPU_65C02) {
1034 AddCodeLine ("\tadc\t(sp)");
1036 ldyconst (ArgSizeOffs);
1037 AddCodeLine ("\tadc\t(sp),y");
1039 AddCodeLine ("\tbcc\t*+3");
1040 AddCodeLine ("\tinx");
1041 AddCodeHint ("x:!"); /* Invalidate X */
1046 /*****************************************************************************/
1047 /* Store into memory */
1048 /*****************************************************************************/
1052 void g_putstatic (unsigned flags, unsigned long label, unsigned offs)
1053 /* Store the primary register into the specified static memory cell */
1055 /* Create the correct label name */
1056 char* lbuf = GetLabelName (flags, label, offs);
1058 /* Check the size and generate the correct store operation */
1059 switch (flags & CF_TYPE) {
1062 AddCodeLine ("\tsta\t%s", lbuf);
1066 AddCodeLine ("\tsta\t%s", lbuf);
1067 AddCodeLine ("\tstx\t%s+1", lbuf);
1071 AddCodeLine ("\tsta\t%s", lbuf);
1072 AddCodeLine ("\tstx\t%s+1", lbuf);
1073 AddCodeLine ("\tldy\tsreg");
1074 AddCodeLine ("\tsty\t%s+2", lbuf);
1075 AddCodeLine ("\tldy\tsreg+1");
1076 AddCodeLine ("\tsty\t%s+3", lbuf);
1087 void g_putlocal (unsigned Flags, int Offs, long Val)
1088 /* Put data into local object. */
1091 CheckLocalOffs (Offs);
1092 switch (Flags & CF_TYPE) {
1095 if (Flags & CF_CONST) {
1096 AddCodeLine ("\tlda\t#$%02X", (unsigned char) Val);
1098 if (CPU == CPU_65C02 && Offs == 0) {
1099 AddCodeLine ("\tsta\t(sp)");
1102 AddCodeLine ("\tsta\t(sp),y");
1107 if (Flags & CF_CONST) {
1109 AddCodeLine ("\tlda\t#$%02X", (unsigned char) (Val >> 8));
1110 AddCodeLine ("\tsta\t(sp),y");
1111 if ((Flags & CF_NOKEEP) == 0) {
1112 /* Place high byte into X */
1113 AddCodeLine ("\ttax");
1115 if (CPU == CPU_65C02 && Offs == 0) {
1116 AddCodeLine ("\tlda\t#$%02X", (unsigned char) Val);
1117 AddCodeLine ("\tsta\t(sp)");
1119 if ((Val & 0xFF) == Offs+1) {
1120 /* The value we need is already in Y */
1121 AddCodeLine ("\ttya");
1122 AddCodeLine ("\tdey");
1124 AddCodeLine ("\tdey");
1125 AddCodeLine ("\tlda\t#$%02X", (unsigned char) Val);
1127 AddCodeLine ("\tsta\t(sp),y");
1130 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
1133 AddCodeLine ("\tjsr\tstaxysp");
1135 AddCodeLine ("\tjsr\tstax0sp");
1138 if (CPU == CPU_65C02 && Offs == 0) {
1139 AddCodeLine ("\tsta\t(sp)");
1141 AddCodeLine ("\ttxa");
1142 AddCodeLine ("\tsta\t(sp),y");
1145 AddCodeLine ("\tsta\t(sp),y");
1146 AddCodeLine ("\tiny");
1147 AddCodeLine ("\ttxa");
1148 AddCodeLine ("\tsta\t(sp),y");
1155 if (Flags & CF_CONST) {
1156 g_getimmed (Flags, Val, 0);
1160 AddCodeLine ("\tjsr\tsteaxysp");
1162 AddCodeLine ("\tjsr\tsteax0sp");
1174 void g_putind (unsigned Flags, unsigned Offs)
1175 /* Store the specified object type in the primary register at the address
1176 * on the top of the stack
1179 /* We can handle offsets below $100 directly, larger offsets must be added
1180 * to the address. Since a/x is in use, best code is achieved by adding
1181 * just the high byte. Be sure to check if the low byte will overflow while
1184 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1186 /* Overflow - we need to add the low byte also */
1187 AddCodeLine ("\tldy\t#$00");
1188 AddCodeLine ("\tclc");
1189 AddCodeLine ("\tpha");
1190 AddCodeLine ("\tlda\t#$%02X", Offs & 0xFF);
1191 AddCodeLine ("\tadc\t(sp),y");
1192 AddCodeLine ("\tsta\t(sp),y");
1193 AddCodeLine ("\tiny");
1194 AddCodeLine ("\tlda\t#$%02X", (Offs >> 8) & 0xFF);
1195 AddCodeLine ("\tadc\t(sp),y");
1196 AddCodeLine ("\tsta\t(sp),y");
1197 AddCodeLine ("\tpla");
1199 /* Complete address is on stack, new offset is zero */
1202 } else if ((Offs & 0xFF00) != 0) {
1204 /* We can just add the high byte */
1205 AddCodeLine ("\tldy\t#$01");
1206 AddCodeLine ("\tclc");
1207 AddCodeLine ("\tpha");
1208 AddCodeLine ("\tlda\t#$%02X", (Offs >> 8) & 0xFF);
1209 AddCodeLine ("\tadc\t(sp),y");
1210 AddCodeLine ("\tsta\t(sp),y");
1211 AddCodeLine ("\tpla");
1213 /* Offset is now just the low byte */
1217 /* Check the size and determine operation */
1218 switch (Flags & CF_TYPE) {
1223 AddCodeLine ("\tjsr\tstaspidx");
1225 AddCodeLine ("\tjsr\tstaspp");
1232 AddCodeLine ("\tjsr\tstaxspidx");
1234 AddCodeLine ("\tjsr\tstaxspp");
1241 AddCodeLine ("\tjsr\tsteaxspidx");
1243 AddCodeLine ("\tjsr\tsteaxspp");
1252 /* Pop the argument which is always a pointer */
1258 /*****************************************************************************/
1259 /* type conversion and similiar stuff */
1260 /*****************************************************************************/
1264 void g_toslong (unsigned flags)
1265 /* Make sure, the value on TOS is a long. Convert if necessary */
1267 switch (flags & CF_TYPE) {
1271 if (flags & CF_UNSIGNED) {
1272 AddCodeLine ("\tjsr\ttosulong");
1274 AddCodeLine ("\tjsr\ttoslong");
1289 void g_tosint (unsigned flags)
1290 /* Make sure, the value on TOS is an int. Convert if necessary */
1292 switch (flags & CF_TYPE) {
1299 AddCodeLine ("\tjsr\ttosint");
1310 void g_reglong (unsigned flags)
1311 /* Make sure, the value in the primary register a long. Convert if necessary */
1313 switch (flags & CF_TYPE) {
1317 if (flags & CF_UNSIGNED) {
1318 if (CodeSizeFactor >= 200) {
1320 AddCodeLine ("\tsty\tsreg");
1321 AddCodeLine ("\tsty\tsreg+1");
1323 AddCodeLine ("\tjsr\taxulong");
1326 AddCodeLine ("\tjsr\taxlong");
1340 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1341 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1342 * value, that corresponds to the value on TOS, rhs corresponds to the value
1343 * in (e)ax. The return value is the the flags value for the resulting type.
1346 unsigned ltype, rtype;
1349 /* Get the type spec from the flags */
1350 ltype = lhs & CF_TYPE;
1351 rtype = rhs & CF_TYPE;
1353 /* Check if a conversion is needed */
1354 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1355 /* We must promote the primary register to long */
1357 /* Get the new rhs type */
1358 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1360 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1361 /* We must promote the lhs to long */
1367 /* Get the new rhs type */
1368 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1372 /* Determine the result type for the operation:
1373 * - The result is const if both operands are const.
1374 * - The result is unsigned if one of the operands is unsigned.
1375 * - The result is long if one of the operands is long.
1376 * - Otherwise the result is int sized.
1378 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1379 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1380 if (rtype == CF_LONG || ltype == CF_LONG) {
1390 unsigned g_typecast (unsigned lhs, unsigned rhs)
1391 /* Cast the value in the primary register to the operand size that is flagged
1392 * by the lhs value. Return the result value.
1395 unsigned ltype, rtype;
1397 /* Get the type spec from the flags */
1398 ltype = lhs & CF_TYPE;
1399 rtype = rhs & CF_TYPE;
1401 /* Check if a conversion is needed */
1402 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1403 /* We must promote the primary register to long */
1407 /* Do not need any other action. If the left type is int, and the primary
1408 * register is long, it will be automagically truncated. If the right hand
1409 * side is const, it is not located in the primary register and handled by
1410 * the expression parser code.
1413 /* Result is const if the right hand side was const */
1414 lhs |= (rhs & CF_CONST);
1416 /* The resulting type is that of the left hand side (that's why you called
1424 void g_scale (unsigned flags, long val)
1425 /* Scale the value in the primary register by the given value. If val is positive,
1426 * scale up, is val is negative, scale down. This function is used to scale
1427 * the operands or results of pointer arithmetic by the size of the type, the
1428 * pointer points to.
1433 /* Value may not be zero */
1435 Internal ("Data type has no size");
1436 } else if (val > 0) {
1439 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1441 /* Factor is 2, 4 or 8, use special function */
1442 switch (flags & CF_TYPE) {
1445 if (flags & CF_FORCECHAR) {
1447 AddCodeLine ("\tasl\ta");
1454 if (CodeSizeFactor >= (p2+1)*130U) {
1455 AddCodeLine ("\tstx\ttmp1");
1457 AddCodeLine ("\tasl\ta");
1458 AddCodeLine ("\trol\ttmp1");
1460 AddCodeLine ("\tldx\ttmp1");
1462 if (flags & CF_UNSIGNED) {
1463 AddCodeLine ("\tjsr\tshlax%d", p2);
1465 AddCodeLine ("\tjsr\taslax%d", p2);
1471 if (flags & CF_UNSIGNED) {
1472 AddCodeLine ("\tjsr\tshleax%d", p2);
1474 AddCodeLine ("\tjsr\tasleax%d", p2);
1483 } else if (val != 1) {
1485 /* Use a multiplication instead */
1486 g_mul (flags | CF_CONST, val);
1494 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1496 /* Factor is 2, 4 or 8, use special function */
1497 switch (flags & CF_TYPE) {
1500 if (flags & CF_FORCECHAR) {
1501 if (flags & CF_UNSIGNED) {
1503 AddCodeLine ("\tlsr\ta");
1506 } else if (p2 <= 2) {
1507 AddCodeLine ("\tcmp\t#$80");
1508 AddCodeLine ("\tror\ta");
1515 if (flags & CF_UNSIGNED) {
1516 if (CodeSizeFactor >= (p2+1)*130U) {
1517 AddCodeLine ("\tstx\ttmp1");
1519 AddCodeLine ("\tlsr\ttmp1");
1520 AddCodeLine ("\tror\ta");
1522 AddCodeLine ("\tldx\ttmp1");
1524 AddCodeLine ("\tjsr\tlsrax%d", p2);
1527 if (CodeSizeFactor >= (p2+1)*150U) {
1528 AddCodeLine ("\tstx\ttmp1");
1530 AddCodeLine ("\tcpx\t#$80");
1531 AddCodeLine ("\tror\ttmp1");
1532 AddCodeLine ("\tror\ta");
1534 AddCodeLine ("\tldx\ttmp1");
1536 AddCodeLine ("\tjsr\tasrax%d", p2);
1542 if (flags & CF_UNSIGNED) {
1543 AddCodeLine ("\tjsr\tlsreax%d", p2);
1545 AddCodeLine ("\tjsr\tasreax%d", p2);
1554 } else if (val != 1) {
1556 /* Use a division instead */
1557 g_div (flags | CF_CONST, val);
1565 /*****************************************************************************/
1566 /* Adds and subs of variables fix a fixed address */
1567 /*****************************************************************************/
1571 void g_addlocal (unsigned flags, int offs)
1572 /* Add a local variable to ax */
1574 /* Correct the offset and check it */
1576 CheckLocalOffs (offs);
1578 switch (flags & CF_TYPE) {
1581 AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1582 AddCodeLine ("\tclc");
1583 AddCodeLine ("\tadc\t(sp),y");
1584 AddCodeLine ("\tbcc\t*+3");
1585 AddCodeLine ("\tinx");
1586 AddCodeHint ("x:!");
1590 AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1591 AddCodeLine ("\tclc");
1592 AddCodeLine ("\tadc\t(sp),y");
1593 AddCodeLine ("\tpha");
1594 AddCodeLine ("\ttxa");
1595 AddCodeLine ("\tiny");
1596 AddCodeLine ("\tadc\t(sp),y");
1597 AddCodeLine ("\ttax");
1598 AddCodeLine ("\tpla");
1602 /* Do it the old way */
1604 g_getlocal (flags, offs);
1616 void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
1617 /* Add a static variable to ax */
1619 /* Create the correct label name */
1620 char* lbuf = GetLabelName (flags, label, offs);
1622 switch (flags & CF_TYPE) {
1625 AddCodeLine ("\tclc");
1626 AddCodeLine ("\tadc\t%s", lbuf);
1627 AddCodeLine ("\tbcc\t*+3");
1628 AddCodeLine ("\tinx");
1629 AddCodeHint ("x:!");
1633 AddCodeLine ("\tclc");
1634 AddCodeLine ("\tadc\t%s", lbuf);
1635 AddCodeLine ("\ttay");
1636 AddCodeLine ("\ttxa");
1637 AddCodeLine ("\tadc\t%s+1", lbuf);
1638 AddCodeLine ("\ttax");
1639 AddCodeLine ("\ttya");
1643 /* Do it the old way */
1645 g_getstatic (flags, label, offs);
1657 /*****************************************************************************/
1658 /* Compares of ax with a variable with fixed address */
1659 /*****************************************************************************/
1663 void g_cmplocal (unsigned flags, int offs)
1664 /* Compare a local variable to ax */
1666 Internal ("g_cmplocal not implemented");
1671 void g_cmpstatic (unsigned flags, unsigned label, unsigned offs)
1672 /* Compare a static variable to ax */
1674 Internal ("g_cmpstatic not implemented");
1679 /*****************************************************************************/
1680 /* Special op= functions */
1681 /*****************************************************************************/
1685 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
1687 /* Emit += for a static variable */
1689 /* Create the correct label name */
1690 char* lbuf = GetLabelName (flags, label, offs);
1692 /* Check the size and determine operation */
1693 switch (flags & CF_TYPE) {
1696 if (flags & CF_FORCECHAR) {
1697 AddCodeLine ("\tldx\t#$00");
1698 if (flags & CF_CONST) {
1700 AddCodeLine ("\tinc\t%s", lbuf);
1701 AddCodeLine ("\tlda\t%s", lbuf);
1703 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1704 AddCodeLine ("\tclc");
1705 AddCodeLine ("\tadc\t%s", lbuf);
1706 AddCodeLine ("\tsta\t%s", lbuf);
1709 AddCodeLine ("\tclc");
1710 AddCodeLine ("\tadc\t%s", lbuf);
1711 AddCodeLine ("\tsta\t%s", lbuf);
1713 if ((flags & CF_UNSIGNED) == 0) {
1714 AddCodeLine ("\tbpl\t*+3");
1715 AddCodeLine ("\tdex");
1716 AddCodeHint ("x:!"); /* Invalidate X */
1723 if (flags & CF_CONST) {
1725 label = GetLabel ();
1726 AddCodeLine ("\tinc\t%s", lbuf);
1727 AddCodeLine ("\tbne\tL%04X", (int)label);
1728 AddCodeLine ("\tinc\t%s+1", lbuf);
1729 g_defloclabel (label);
1730 AddCodeLine ("\tlda\t%s", lbuf); /* Hmmm... */
1731 AddCodeLine ("\tldx\t%s+1", lbuf);
1733 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1734 AddCodeLine ("\tclc");
1735 AddCodeLine ("\tadc\t%s", lbuf);
1736 AddCodeLine ("\tsta\t%s", lbuf);
1738 label = GetLabel ();
1739 AddCodeLine ("\tbcc\tL%04X", (int)label);
1740 AddCodeLine ("\tinc\t%s+1", lbuf);
1741 g_defloclabel (label);
1742 AddCodeLine ("\tldx\t%s+1", lbuf);
1744 AddCodeLine ("\tlda\t#$%02X", (unsigned char)(val >> 8));
1745 AddCodeLine ("\tadc\t%s+1", lbuf);
1746 AddCodeLine ("\tsta\t%s+1", lbuf);
1747 AddCodeLine ("\ttax");
1748 AddCodeLine ("\tlda\t%s", lbuf);
1752 AddCodeLine ("\tclc");
1753 AddCodeLine ("\tadc\t%s", lbuf);
1754 AddCodeLine ("\tsta\t%s", lbuf);
1755 AddCodeLine ("\ttxa");
1756 AddCodeLine ("\tadc\t%s+1", lbuf);
1757 AddCodeLine ("\tsta\t%s+1", lbuf);
1758 AddCodeLine ("\ttax");
1759 AddCodeLine ("\tlda\t%s", lbuf);
1764 if (flags & CF_CONST) {
1766 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1767 AddCodeLine ("\tsty\tptr1");
1768 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1770 AddCodeLine ("\tjsr\tladdeq1");
1772 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1773 AddCodeLine ("\tjsr\tladdeqa");
1776 g_getstatic (flags, label, offs);
1778 g_putstatic (flags, label, offs);
1781 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1782 AddCodeLine ("\tsty\tptr1");
1783 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1784 AddCodeLine ("\tjsr\tladdeq");
1795 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1796 /* Emit += for a local variable */
1798 /* Calculate the true offset, check it, load it into Y */
1800 CheckLocalOffs (offs);
1802 /* Check the size and determine operation */
1803 switch (flags & CF_TYPE) {
1806 if (flags & CF_FORCECHAR) {
1808 AddCodeLine ("\tldx\t#$00");
1809 if (flags & CF_CONST) {
1810 AddCodeLine ("\tclc");
1811 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1812 AddCodeLine ("\tadc\t(sp,x)");
1813 AddCodeLine ("\tsta\t(sp,x)");
1815 AddCodeLine ("\tclc");
1816 AddCodeLine ("\tadc\t(sp,x)");
1817 AddCodeLine ("\tsta\t(sp,x)");
1821 AddCodeLine ("\tldx\t#$00");
1822 if (flags & CF_CONST) {
1823 AddCodeLine ("\tclc");
1824 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1825 AddCodeLine ("\tadc\t(sp),y");
1826 AddCodeLine ("\tsta\t(sp),y");
1828 AddCodeLine ("\tclc");
1829 AddCodeLine ("\tadc\t(sp),y");
1830 AddCodeLine ("\tsta\t(sp),y");
1833 if ((flags & CF_UNSIGNED) == 0) {
1834 AddCodeLine ("\tbpl\t*+3");
1835 AddCodeLine ("\tdex");
1836 AddCodeHint ("x:!"); /* Invalidate X */
1843 if (flags & CF_CONST) {
1844 g_getimmed (flags, val, 0);
1847 AddCodeLine ("\tjsr\taddeq0sp");
1850 AddCodeLine ("\tjsr\taddeqysp");
1855 if (flags & CF_CONST) {
1856 g_getimmed (flags, val, 0);
1859 AddCodeLine ("\tjsr\tladdeq0sp");
1862 AddCodeLine ("\tjsr\tladdeqysp");
1873 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1874 /* Emit += for the location with address in ax */
1876 /* If the offset is too large for a byte register, add the high byte
1877 * of the offset to the primary. Beware: We need a special correction
1878 * if the offset in the low byte will overflow in the operation.
1880 offs = MakeByteOffs (flags, offs);
1882 /* Check the size and determine operation */
1883 switch (flags & CF_TYPE) {
1886 AddCodeLine ("\tsta\tptr1");
1887 AddCodeLine ("\tstx\tptr1+1");
1889 AddCodeLine ("\tldx\t#$00");
1890 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1891 AddCodeLine ("\tclc");
1892 AddCodeLine ("\tadc\t(ptr1,x)");
1893 AddCodeLine ("\tsta\t(ptr1,x)");
1895 AddCodeLine ("\tldy\t#$%02X", offs);
1896 AddCodeLine ("\tldx\t#$00");
1897 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1898 AddCodeLine ("\tclc");
1899 AddCodeLine ("\tadc\t(ptr1),y");
1900 AddCodeLine ("\tsta\t(ptr1),y");
1905 if (CodeSizeFactor >= 200) {
1906 /* Lots of code, use only if size is not important */
1907 AddCodeLine ("\tsta\tptr1");
1908 AddCodeLine ("\tstx\tptr1+1");
1909 AddCodeLine ("\tldy\t#$%02X", offs);
1910 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1911 AddCodeLine ("\tclc");
1912 AddCodeLine ("\tadc\t(ptr1),y");
1913 AddCodeLine ("\tsta\t(ptr1),y");
1914 AddCodeLine ("\tpha");
1915 AddCodeLine ("\tiny");
1916 AddCodeLine ("\tlda\t#$%02X", (unsigned char)(val >> 8));
1917 AddCodeLine ("\tadc\t(ptr1),y");
1918 AddCodeLine ("\tsta\t(ptr1),y");
1919 AddCodeLine ("\ttax");
1920 AddCodeLine ("\tpla");
1926 AddCodeLine ("\tjsr\tpushax"); /* Push the address */
1927 push (flags); /* Correct the internal sp */
1928 g_getind (flags, offs); /* Fetch the value */
1929 g_inc (flags, val); /* Increment value in primary */
1930 g_putind (flags, offs); /* Store the value back */
1940 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1942 /* Emit -= for a static variable */
1944 /* Create the correct label name */
1945 char* lbuf = GetLabelName (flags, label, offs);
1947 /* Check the size and determine operation */
1948 switch (flags & CF_TYPE) {
1951 if (flags & CF_FORCECHAR) {
1952 AddCodeLine ("\tldx\t#$00");
1953 if (flags & CF_CONST) {
1955 AddCodeLine ("\tdec\t%s", lbuf);
1956 AddCodeLine ("\tlda\t%s", lbuf);
1958 AddCodeLine ("\tsec");
1959 AddCodeLine ("\tlda\t%s", lbuf);
1960 AddCodeLine ("\tsbc\t#$%02X", (int)(val & 0xFF));
1961 AddCodeLine ("\tsta\t%s", lbuf);
1964 AddCodeLine ("\tsec");
1965 AddCodeLine ("\tsta\ttmp1");
1966 AddCodeLine ("\tlda\t%s", lbuf);
1967 AddCodeLine ("\tsbc\ttmp1");
1968 AddCodeLine ("\tsta\t%s", lbuf);
1970 if ((flags & CF_UNSIGNED) == 0) {
1971 AddCodeLine ("\tbpl\t*+3");
1972 AddCodeLine ("\tdex");
1973 AddCodeHint ("x:!"); /* Invalidate X */
1980 AddCodeLine ("\tsec");
1981 if (flags & CF_CONST) {
1982 AddCodeLine ("\tlda\t%s", lbuf);
1983 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
1984 AddCodeLine ("\tsta\t%s", lbuf);
1986 label = GetLabel ();
1987 AddCodeLine ("\tbcs\tL%04X", (unsigned)label);
1988 AddCodeLine ("\tdec\t%s+1", lbuf);
1989 g_defloclabel (label);
1990 AddCodeLine ("\tldx\t%s+1", lbuf);
1992 AddCodeLine ("\tlda\t%s+1", lbuf);
1993 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)(val >> 8));
1994 AddCodeLine ("\tsta\t%s+1", lbuf);
1995 AddCodeLine ("\ttax");
1996 AddCodeLine ("\tlda\t%s", lbuf);
1999 AddCodeLine ("\tsta\ttmp1");
2000 AddCodeLine ("\tlda\t%s", lbuf);
2001 AddCodeLine ("\tsbc\ttmp1");
2002 AddCodeLine ("\tsta\t%s", lbuf);
2003 AddCodeLine ("\tstx\ttmp1");
2004 AddCodeLine ("\tlda\t%s+1", lbuf);
2005 AddCodeLine ("\tsbc\ttmp1");
2006 AddCodeLine ("\tsta\t%s+1", lbuf);
2007 AddCodeLine ("\ttax");
2008 AddCodeLine ("\tlda\t%s", lbuf);
2013 if (flags & CF_CONST) {
2015 AddCodeLine ("\tldy\t#<(%s)", lbuf);
2016 AddCodeLine ("\tsty\tptr1");
2017 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
2019 AddCodeLine ("\tjsr\tlsubeq1");
2021 AddCodeLine ("\tlda\t#$%02X", (unsigned char)val);
2022 AddCodeLine ("\tjsr\tlsubeqa");
2025 g_getstatic (flags, label, offs);
2027 g_putstatic (flags, label, offs);
2030 AddCodeLine ("\tldy\t#<(%s)", lbuf);
2031 AddCodeLine ("\tsty\tptr1");
2032 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
2033 AddCodeLine ("\tjsr\tlsubeq");
2044 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
2045 /* Emit -= for a local variable */
2047 /* Calculate the true offset, check it, load it into Y */
2049 CheckLocalOffs (offs);
2051 /* Check the size and determine operation */
2052 switch (flags & CF_TYPE) {
2055 if (flags & CF_FORCECHAR) {
2057 AddCodeLine ("\tldx\t#$00");
2058 AddCodeLine ("\tsec");
2059 if (flags & CF_CONST) {
2060 AddCodeLine ("\tlda\t(sp),y");
2061 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2063 AddCodeLine ("\tsta\ttmp1");
2064 AddCodeLine ("\tlda\t(sp),y");
2065 AddCodeLine ("\tsbc\ttmp1");
2067 AddCodeLine ("\tsta\t(sp),y");
2068 if ((flags & CF_UNSIGNED) == 0) {
2069 AddCodeLine ("\tbpl\t*+3");
2070 AddCodeLine ("\tdex");
2071 AddCodeHint ("x:!"); /* Invalidate X */
2078 if (flags & CF_CONST) {
2079 g_getimmed (flags, val, 0);
2082 AddCodeLine ("\tjsr\tsubeq0sp");
2085 AddCodeLine ("\tjsr\tsubeqysp");
2090 if (flags & CF_CONST) {
2091 g_getimmed (flags, val, 0);
2094 AddCodeLine ("\tjsr\tlsubeq0sp");
2097 AddCodeLine ("\tjsr\tlsubeqysp");
2108 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2109 /* Emit -= for the location with address in ax */
2111 /* If the offset is too large for a byte register, add the high byte
2112 * of the offset to the primary. Beware: We need a special correction
2113 * if the offset in the low byte will overflow in the operation.
2115 offs = MakeByteOffs (flags, offs);
2117 /* Check the size and determine operation */
2118 switch (flags & CF_TYPE) {
2121 AddCodeLine ("\tsta\tptr1");
2122 AddCodeLine ("\tstx\tptr1+1");
2124 AddCodeLine ("\tldx\t#$00");
2125 AddCodeLine ("\tlda\t(ptr1,x)");
2126 AddCodeLine ("\tsec");
2127 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2128 AddCodeLine ("\tsta\t(ptr1,x)");
2130 AddCodeLine ("\tldy\t#$%02X", offs);
2131 AddCodeLine ("\tldx\t#$00");
2132 AddCodeLine ("\tlda\t(ptr1),y");
2133 AddCodeLine ("\tsec");
2134 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2135 AddCodeLine ("\tsta\t(ptr1),y");
2140 if (CodeSizeFactor >= 200) {
2141 /* Lots of code, use only if size is not important */
2142 AddCodeLine ("\tsta\tptr1");
2143 AddCodeLine ("\tstx\tptr1+1");
2144 AddCodeLine ("\tldy\t#$%02X", offs);
2145 AddCodeLine ("\tlda\t(ptr1),y");
2146 AddCodeLine ("\tsec");
2147 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2148 AddCodeLine ("\tsta\t(ptr1),y");
2149 AddCodeLine ("\tpha");
2150 AddCodeLine ("\tiny");
2151 AddCodeLine ("\tlda\t(ptr1),y");
2152 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)(val >> 8));
2153 AddCodeLine ("\tsta\t(ptr1),y");
2154 AddCodeLine ("\ttax");
2155 AddCodeLine ("\tpla");
2161 AddCodeLine ("\tjsr\tpushax"); /* Push the address */
2162 push (flags); /* Correct the internal sp */
2163 g_getind (flags, offs); /* Fetch the value */
2164 g_dec (flags, val); /* Increment value in primary */
2165 g_putind (flags, offs); /* Store the value back */
2175 /*****************************************************************************/
2176 /* Add a variable address to the value in ax */
2177 /*****************************************************************************/
2181 void g_addaddr_local (unsigned flags, int offs)
2182 /* Add the address of a local variable to ax */
2184 /* Add the offset */
2187 /* We cannot address more then 256 bytes of locals anyway */
2188 CheckLocalOffs (offs);
2189 AddCodeLine ("\tclc");
2190 AddCodeLine ("\tadc\t#$%02X", offs & 0xFF);
2191 AddCodeLine ("\tbcc\t*+4"); /* Do also skip the CLC insn below */
2192 AddCodeLine ("\tinx");
2193 AddCodeHint ("x:!"); /* Invalidate X */
2196 /* Add the current stackpointer value */
2197 AddCodeLine ("\tclc");
2198 AddCodeLine ("\tadc\tsp");
2199 AddCodeLine ("\ttay");
2200 AddCodeLine ("\ttxa");
2201 AddCodeLine ("\tadc\tsp+1");
2202 AddCodeLine ("\ttax");
2203 AddCodeLine ("\ttya");
2208 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2209 /* Add the address of a static variable to ax */
2211 /* Create the correct label name */
2212 char* lbuf = GetLabelName (flags, label, offs);
2214 /* Add the address to the current ax value */
2215 AddCodeLine ("\tclc");
2216 AddCodeLine ("\tadc\t#<(%s)", lbuf);
2217 AddCodeLine ("\ttay");
2218 AddCodeLine ("\ttxa");
2219 AddCodeLine ("\tadc\t#>(%s)", lbuf);
2220 AddCodeLine ("\ttax");
2221 AddCodeLine ("\ttya");
2226 /*****************************************************************************/
2228 /*****************************************************************************/
2232 void g_save (unsigned flags)
2233 /* Copy primary register to hold register. */
2235 /* Check the size and determine operation */
2236 switch (flags & CF_TYPE) {
2239 if (flags & CF_FORCECHAR) {
2240 AddCodeLine ("\tpha");
2246 AddCodeLine ("\tsta\tregsave");
2247 AddCodeLine ("\tstx\tregsave+1");
2251 AddCodeLine ("\tjsr\tsaveeax");
2261 void g_restore (unsigned flags)
2262 /* Copy hold register to P. */
2264 /* Check the size and determine operation */
2265 switch (flags & CF_TYPE) {
2268 if (flags & CF_FORCECHAR) {
2269 AddCodeLine ("\tpla");
2275 AddCodeLine ("\tlda\tregsave");
2276 AddCodeLine ("\tldx\tregsave+1");
2280 AddCodeLine ("\tjsr\tresteax");
2290 void g_cmp (unsigned flags, unsigned long val)
2291 /* Immidiate compare. The primary register will not be changed, Z flag
2295 /* Check the size and determine operation */
2296 switch (flags & CF_TYPE) {
2299 if (flags & CF_FORCECHAR) {
2300 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
2306 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
2307 AddCodeLine ("\tbne\t*+4");
2308 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
2312 Internal ("g_cmp: Long compares not implemented");
2322 static void oper (unsigned flags, unsigned long val, char** subs)
2323 /* Encode a binary operation. subs is a pointer to four groups of three
2325 * 0-2 --> Operate on ints
2326 * 3-5 --> Operate on unsigneds
2327 * 6-8 --> Operate on longs
2328 * 9-11 --> Operate on unsigned longs
2330 * The first subroutine names in each string group is used to encode an
2331 * operation with a zero constant, the second to encode an operation with
2332 * a 8 bit constant, and the third is used in all other cases.
2337 /* Determine the offset into the array */
2338 offs = (flags & CF_UNSIGNED)? 3 : 0;
2339 switch (flags & CF_TYPE) {
2352 /* Encode the operation */
2353 if (flags & CF_CONST) {
2354 /* Constant value given */
2355 if (val == 0 && subs [offs+0]) {
2356 /* Special case: constant with value zero */
2357 AddCodeLine ("\tjsr\t%s", subs [offs+0]);
2358 } else if (val < 0x100 && subs [offs+1]) {
2359 /* Special case: constant with high byte zero */
2360 ldaconst (val); /* Load low byte */
2361 AddCodeLine ("\tjsr\t%s", subs [offs+1]);
2363 /* Others: arbitrary constant value */
2364 g_getimmed (flags, val, 0); /* Load value */
2365 AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2368 /* Value not constant (is already in (e)ax) */
2369 AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2372 /* The operation will pop it's argument */
2378 void g_test (unsigned flags)
2379 /* Force a test to set cond codes right */
2381 switch (flags & CF_TYPE) {
2384 if (flags & CF_FORCECHAR) {
2385 AddCodeLine ("\ttax");
2391 AddCodeLine ("\tstx\ttmp1");
2392 AddCodeLine ("\tora\ttmp1");
2396 if (flags & CF_UNSIGNED) {
2397 AddCodeLine ("\tjsr\tutsteax");
2399 AddCodeLine ("\tjsr\ttsteax");
2411 void g_push (unsigned flags, unsigned long val)
2412 /* Push the primary register or a constant value onto the stack */
2416 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2418 /* We have a constant 8 or 16 bit value */
2419 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2421 /* Handle as 8 bit value */
2422 if (CodeSizeFactor >= 165 || val > 2) {
2424 AddCodeLine ("\tjsr\tpusha");
2426 AddCodeLine ("\tjsr\tpushc%d", (int) val);
2431 /* Handle as 16 bit value */
2432 hi = (unsigned char) (val >> 8);
2434 AddCodeLine ("\tjsr\tpush%u", (unsigned) val);
2435 } else if (hi == 0 || hi == 0xFF) {
2436 /* Use special function */
2438 AddCodeLine ("\tjsr\t%s", (hi == 0)? "pusha0" : "pushaFF");
2441 g_getimmed (flags, val, 0);
2442 AddCodeLine ("\tjsr\tpushax");
2448 /* Value is not 16 bit or not constant */
2449 if (flags & CF_CONST) {
2450 /* Constant 32 bit value, load into eax */
2451 g_getimmed (flags, val, 0);
2454 /* Push the primary register */
2455 switch (flags & CF_TYPE) {
2458 if (flags & CF_FORCECHAR) {
2459 /* Handle as char */
2460 AddCodeLine ("\tjsr\tpusha");
2465 AddCodeLine ("\tjsr\tpushax");
2469 AddCodeLine ("\tjsr\tpusheax");
2479 /* Adjust the stack offset */
2485 void g_swap (unsigned flags)
2486 /* Swap the primary register and the top of the stack. flags give the type
2487 * of *both* values (must have same size).
2490 switch (flags & CF_TYPE) {
2494 AddCodeLine ("\tjsr\tswapstk");
2498 AddCodeLine ("\tjsr\tswapestk");
2509 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2510 /* Call the specified subroutine name */
2512 if ((Flags & CF_FIXARGC) == 0) {
2513 /* Pass the argument count */
2516 AddCodeLine ("\tjsr\t_%s", Label);
2517 oursp += ArgSize; /* callee pops args */
2522 void g_callind (unsigned Flags, unsigned ArgSize)
2523 /* Call subroutine with address in AX */
2525 if ((Flags & CF_FIXARGC) == 0) {
2526 /* Pass arg count */
2529 AddCodeLine ("\tjsr\tcallax"); /* do the call */
2530 oursp += ArgSize; /* callee pops args */
2535 void g_jump (unsigned Label)
2536 /* Jump to specified internal label number */
2538 AddCodeLine ("\tjmp\tL%04X", Label);
2543 void g_switch (unsigned Flags)
2544 /* Output switch statement preamble */
2546 switch (Flags & CF_TYPE) {
2550 AddCodeLine ("\tjsr\tswitch");
2554 AddCodeLine ("\tjsr\tlswitch");
2565 void g_case (unsigned flags, unsigned label, unsigned long val)
2566 /* Create table code for one case selector */
2568 switch (flags & CF_TYPE) {
2572 AddCodeLine ("\t.word\t$%04X, L%04X",
2573 (unsigned)(val & 0xFFFF),
2574 (unsigned)(label & 0xFFFF));
2578 AddCodeLine ("\t.dword\t$%08lX", val);
2579 AddCodeLine ("\t.word\tL%04X", label & 0xFFFF);
2590 void g_truejump (unsigned flags, unsigned label)
2591 /* Jump to label if zero flag clear */
2593 if (flags & CF_SHORT) {
2594 AddCodeLine ("\tbne\tL%04X", label);
2596 AddCodeLine ("\tjne\tL%04X", label);
2602 void g_falsejump (unsigned flags, unsigned label)
2603 /* Jump to label if zero flag set */
2605 if (flags & CF_SHORT) {
2606 AddCodeLine ("\tbeq\tL%04X", label);
2608 AddCodeLine ("\tjeq\tL%04X", label);
2614 static void mod_internal (int k, char* verb1, char* verb2)
2617 AddCodeLine ("\tjsr\t%ssp%c", verb1, k + '0');
2621 AddCodeLine ("\tjsr\t%ssp", verb2);
2627 void g_space (int space)
2628 /* Create or drop space on the stack */
2631 mod_internal (-space, "inc", "addy");
2632 } else if (space > 0) {
2633 mod_internal (space, "dec", "suby");
2639 void g_cstackcheck (void)
2640 /* Check for a C stack overflow */
2642 AddCodeLine ("\tjsr\tcstkchk");
2647 void g_stackcheck (void)
2648 /* Check for a stack overflow */
2650 AddCodeLine ("\tjsr\tstkchk");
2655 void g_add (unsigned flags, unsigned long val)
2656 /* Primary = TOS + Primary */
2658 static char* ops [12] = {
2659 0, "tosadda0", "tosaddax",
2660 0, "tosadda0", "tosaddax",
2665 if (flags & CF_CONST) {
2666 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2667 g_push (flags & ~CF_CONST, 0);
2669 oper (flags, val, ops);
2674 void g_sub (unsigned flags, unsigned long val)
2675 /* Primary = TOS - Primary */
2677 static char* ops [12] = {
2678 0, "tossuba0", "tossubax",
2679 0, "tossuba0", "tossubax",
2684 if (flags & CF_CONST) {
2685 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2686 g_push (flags & ~CF_CONST, 0);
2688 oper (flags, val, ops);
2693 void g_rsub (unsigned flags, unsigned long val)
2694 /* Primary = Primary - TOS */
2696 static char* ops [12] = {
2697 0, "tosrsuba0", "tosrsubax",
2698 0, "tosrsuba0", "tosrsubax",
2702 oper (flags, val, ops);
2707 void g_mul (unsigned flags, unsigned long val)
2708 /* Primary = TOS * Primary */
2710 static char* ops [12] = {
2711 0, "tosmula0", "tosmulax",
2712 0, "tosumula0", "tosumulax",
2719 /* Do strength reduction if the value is constant and a power of two */
2720 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2721 /* Generate a shift instead */
2726 /* If the right hand side is const, the lhs is not on stack but still
2727 * in the primary register.
2729 if (flags & CF_CONST) {
2731 switch (flags & CF_TYPE) {
2734 if (flags & CF_FORCECHAR) {
2735 /* Handle some special cases */
2739 AddCodeLine ("\tsta\ttmp1");
2740 AddCodeLine ("\tasl\ta");
2741 AddCodeLine ("\tclc");
2742 AddCodeLine ("\tadc\ttmp1");
2746 AddCodeLine ("\tsta\ttmp1");
2747 AddCodeLine ("\tasl\ta");
2748 AddCodeLine ("\tasl\ta");
2749 AddCodeLine ("\tclc");
2750 AddCodeLine ("\tadc\ttmp1");
2754 AddCodeLine ("\tsta\ttmp1");
2755 AddCodeLine ("\tasl\ta");
2756 AddCodeLine ("\tasl\ta");
2757 AddCodeLine ("\tclc");
2758 AddCodeLine ("\tadc\ttmp1");
2759 AddCodeLine ("\tasl\ta");
2775 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2776 * into the normal, non-optimized stuff.
2778 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2779 g_push (flags & ~CF_CONST, 0);
2783 /* Use long way over the stack */
2784 oper (flags, val, ops);
2789 void g_div (unsigned flags, unsigned long val)
2790 /* Primary = TOS / Primary */
2792 static char* ops [12] = {
2793 0, "tosdiva0", "tosdivax",
2794 0, "tosudiva0", "tosudivax",
2799 /* Do strength reduction if the value is constant and a power of two */
2801 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2802 /* Generate a shift instead */
2805 /* Generate a division */
2806 if (flags & CF_CONST) {
2807 /* lhs is not on stack */
2808 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2809 g_push (flags & ~CF_CONST, 0);
2811 oper (flags, val, ops);
2817 void g_mod (unsigned flags, unsigned long val)
2818 /* Primary = TOS % Primary */
2820 static char* ops [12] = {
2821 0, "tosmoda0", "tosmodax",
2822 0, "tosumoda0", "tosumodax",
2828 /* Check if we can do some cost reduction */
2829 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2830 /* We can do that with an AND operation */
2831 g_and (flags, val - 1);
2833 /* Do it the hard way... */
2834 if (flags & CF_CONST) {
2835 /* lhs is not on stack */
2836 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2837 g_push (flags & ~CF_CONST, 0);
2839 oper (flags, val, ops);
2845 void g_or (unsigned flags, unsigned long val)
2846 /* Primary = TOS | Primary */
2848 static char* ops [12] = {
2849 0, "tosora0", "tosorax",
2850 0, "tosora0", "tosorax",
2855 /* If the right hand side is const, the lhs is not on stack but still
2856 * in the primary register.
2858 if (flags & CF_CONST) {
2860 switch (flags & CF_TYPE) {
2863 if (flags & CF_FORCECHAR) {
2864 if ((val & 0xFF) != 0xFF) {
2865 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2873 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2880 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2889 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2890 * into the normal, non-optimized stuff.
2892 g_push (flags & ~CF_CONST, 0);
2896 /* Use long way over the stack */
2897 oper (flags, val, ops);
2902 void g_xor (unsigned flags, unsigned long val)
2903 /* Primary = TOS ^ Primary */
2905 static char* ops [12] = {
2906 0, "tosxora0", "tosxorax",
2907 0, "tosxora0", "tosxorax",
2913 /* If the right hand side is const, the lhs is not on stack but still
2914 * in the primary register.
2916 if (flags & CF_CONST) {
2918 switch (flags & CF_TYPE) {
2921 if (flags & CF_FORCECHAR) {
2922 if ((val & 0xFF) != 0) {
2923 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2932 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2935 } else if ((val & 0xFF) == 0) {
2936 AddCodeLine ("\tpha");
2937 AddCodeLine ("\ttxa");
2938 AddCodeLine ("\teor\t#$%02X", (unsigned char)(val >> 8));
2939 AddCodeLine ("\ttax");
2940 AddCodeLine ("\tpla");
2948 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2958 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2959 * into the normal, non-optimized stuff.
2961 g_push (flags & ~CF_CONST, 0);
2965 /* Use long way over the stack */
2966 oper (flags, val, ops);
2971 void g_and (unsigned flags, unsigned long val)
2972 /* Primary = TOS & Primary */
2974 static char* ops [12] = {
2975 0, "tosanda0", "tosandax",
2976 0, "tosanda0", "tosandax",
2981 /* If the right hand side is const, the lhs is not on stack but still
2982 * in the primary register.
2984 if (flags & CF_CONST) {
2986 switch (flags & CF_TYPE) {
2989 if (flags & CF_FORCECHAR) {
2990 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2995 if ((val & 0xFFFF) != 0xFFFF) {
3000 } else if (val != 0xFF) {
3001 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
3003 } else if ((val & 0xFF00) == 0xFF00) {
3004 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
3005 } else if ((val & 0x00FF) == 0x0000) {
3006 AddCodeLine ("\ttxa");
3007 AddCodeLine ("\tand\t#$%02X", (unsigned char)(val >> 8));
3008 AddCodeLine ("\ttax");
3011 AddCodeLine ("\ttay");
3012 AddCodeLine ("\ttxa");
3013 AddCodeLine ("\tand\t#$%02X", (unsigned char)(val >> 8));
3014 AddCodeLine ("\ttax");
3015 AddCodeLine ("\ttya");
3016 if ((val & 0x00FF) != 0x00FF) {
3017 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
3026 AddCodeLine ("\tstx\tsreg+1");
3027 AddCodeLine ("\tstx\tsreg");
3028 if ((val & 0xFF) != 0xFF) {
3029 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
3032 } else if (val == 0xFF00) {
3034 AddCodeLine ("\tsta\tsreg+1");
3035 AddCodeLine ("\tsta\tsreg");
3044 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3045 * into the normal, non-optimized stuff.
3047 g_push (flags & ~CF_CONST, 0);
3051 /* Use long way over the stack */
3052 oper (flags, val, ops);
3057 void g_asr (unsigned flags, unsigned long val)
3058 /* Primary = TOS >> Primary */
3060 static char* ops [12] = {
3061 0, "tosasra0", "tosasrax",
3062 0, "tosshra0", "tosshrax",
3067 /* If the right hand side is const, the lhs is not on stack but still
3068 * in the primary register.
3070 if (flags & CF_CONST) {
3072 switch (flags & CF_TYPE) {
3076 if (val >= 1 && val <= 3) {
3077 if (flags & CF_UNSIGNED) {
3078 AddCodeLine ("\tjsr\tshrax%ld", val);
3080 AddCodeLine ("\tjsr\tasrax%ld", val);
3083 } else if (val == 8 && (flags & CF_UNSIGNED)) {
3084 AddCodeLine ("\ttxa");
3091 if (val >= 1 && val <= 3) {
3092 if (flags & CF_UNSIGNED) {
3093 AddCodeLine ("\tjsr\tshreax%ld", val);
3095 AddCodeLine ("\tjsr\tasreax%ld", val);
3098 } else if (val == 8 && (flags & CF_UNSIGNED)) {
3099 AddCodeLine ("\ttxa");
3100 AddCodeLine ("\tldx\tsreg");
3101 AddCodeLine ("\tldy\tsreg+1");
3102 AddCodeLine ("\tsty\tsreg");
3103 AddCodeLine ("\tldy\t#$00");
3104 AddCodeLine ("\tsty\tsreg+1");
3106 } else if (val == 16) {
3107 AddCodeLine ("\tldy\t#$00");
3108 AddCodeLine ("\tldx\tsreg+1");
3109 if ((flags & CF_UNSIGNED) == 0) {
3110 AddCodeLine ("\tbpl\t*+3");
3111 AddCodeLine ("\tdey");
3112 AddCodeHint ("y:!");
3114 AddCodeLine ("\tlda\tsreg");
3115 AddCodeLine ("\tsty\tsreg+1");
3116 AddCodeLine ("\tsty\tsreg");
3125 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3126 * into the normal, non-optimized stuff.
3128 g_push (flags & ~CF_CONST, 0);
3132 /* Use long way over the stack */
3133 oper (flags, val, ops);
3138 void g_asl (unsigned flags, unsigned long val)
3139 /* Primary = TOS << Primary */
3141 static char* ops [12] = {
3142 0, "tosasla0", "tosaslax",
3143 0, "tosshla0", "tosshlax",
3149 /* If the right hand side is const, the lhs is not on stack but still
3150 * in the primary register.
3152 if (flags & CF_CONST) {
3154 switch (flags & CF_TYPE) {
3158 if (val >= 1 && val <= 3) {
3159 if (flags & CF_UNSIGNED) {
3160 AddCodeLine ("\tjsr\tshlax%ld", val);
3162 AddCodeLine ("\tjsr\taslax%ld", val);
3165 } else if (val == 8) {
3166 AddCodeLine ("\ttax");
3167 AddCodeLine ("\tlda\t#$00");
3173 if (val >= 1 && val <= 3) {
3174 if (flags & CF_UNSIGNED) {
3175 AddCodeLine ("\tjsr\tshleax%ld", val);
3177 AddCodeLine ("\tjsr\tasleax%ld", val);
3180 } else if (val == 8) {
3181 AddCodeLine ("\tldy\tsreg");
3182 AddCodeLine ("\tsty\tsreg+1");
3183 AddCodeLine ("\tstx\tsreg");
3184 AddCodeLine ("\ttax");
3185 AddCodeLine ("\tlda\t#$00");
3187 } else if (val == 16) {
3188 AddCodeLine ("\tstx\tsreg+1");
3189 AddCodeLine ("\tsta\tsreg");
3190 AddCodeLine ("\tlda\t#$00");
3191 AddCodeLine ("\ttax");
3200 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3201 * into the normal, non-optimized stuff.
3203 g_push (flags & ~CF_CONST, 0);
3207 /* Use long way over the stack */
3208 oper (flags, val, ops);
3213 void g_neg (unsigned flags)
3214 /* Primary = -Primary */
3216 switch (flags & CF_TYPE) {
3220 AddCodeLine ("\tjsr\tnegax");
3224 AddCodeLine ("\tjsr\tnegeax");
3234 void g_bneg (unsigned flags)
3235 /* Primary = !Primary */
3237 switch (flags & CF_TYPE) {
3240 AddCodeLine ("\tjsr\tbnega");
3244 AddCodeLine ("\tjsr\tbnegax");
3248 AddCodeLine ("\tjsr\tbnegeax");
3258 void g_com (unsigned flags)
3259 /* Primary = ~Primary */
3261 switch (flags & CF_TYPE) {
3265 AddCodeLine ("\tjsr\tcomplax");
3269 AddCodeLine ("\tjsr\tcompleax");
3279 void g_inc (unsigned flags, unsigned long val)
3280 /* Increment the primary register by a given number */
3282 /* Don't inc by zero */
3287 /* Generate code for the supported types */
3289 switch (flags & CF_TYPE) {
3292 if (flags & CF_FORCECHAR) {
3293 if (CPU == CPU_65C02 && val <= 2) {
3295 AddCodeLine ("\tina");
3298 AddCodeLine ("\tclc");
3299 AddCodeLine ("\tadc\t#$%02X", (unsigned char)val);
3306 if (CPU == CPU_65C02 && val == 1) {
3307 AddCodeLine ("\tina");
3308 AddCodeLine ("\tbne\t*+3");
3309 AddCodeLine ("\tinx");
3310 /* Tell the optimizer that the X register may be invalid */
3311 AddCodeHint ("x:!");
3312 } else if (CodeSizeFactor < 200) {
3315 AddCodeLine ("\tjsr\tincax%lu", val);
3316 } else if (val <= 255) {
3318 AddCodeLine ("\tjsr\tincaxy");
3320 g_add (flags | CF_CONST, val);
3323 /* Inline the code */
3325 if ((val & 0xFF) != 0) {
3326 AddCodeLine ("\tclc");
3327 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3328 AddCodeLine ("\tbcc\t*+3");
3329 AddCodeLine ("\tinx");
3330 /* Tell the optimizer that the X register may be invalid */
3331 AddCodeHint ("x:!");
3334 AddCodeLine ("\tinx");
3337 AddCodeLine ("\tinx");
3340 AddCodeLine ("\tclc");
3341 if ((val & 0xFF) != 0) {
3342 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3343 /* Tell the optimizer that the X register may be invalid */
3344 AddCodeHint ("x:!");
3346 AddCodeLine ("\tpha");
3347 AddCodeLine ("\ttxa");
3348 AddCodeLine ("\tadc\t#$%02X", (unsigned char) (val >> 8));
3349 AddCodeLine ("\ttax");
3350 AddCodeLine ("\tpla");
3358 AddCodeLine ("\tjsr\tinceaxy");
3360 g_add (flags | CF_CONST, val);
3372 void g_dec (unsigned flags, unsigned long val)
3373 /* Decrement the primary register by a given number */
3375 /* Don't dec by zero */
3380 /* Generate code for the supported types */
3382 switch (flags & CF_TYPE) {
3385 if (flags & CF_FORCECHAR) {
3386 if (CPU == CPU_65C02 && val <= 2) {
3388 AddCodeLine ("\tdea");
3391 AddCodeLine ("\tsec");
3392 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
3400 AddCodeLine ("\tjsr\tdecax%d", (int) val);
3401 } else if (val <= 255) {
3403 AddCodeLine ("\tjsr\tdecaxy");
3405 g_sub (flags | CF_CONST, val);
3412 AddCodeLine ("\tjsr\tdeceaxy");
3414 g_sub (flags | CF_CONST, val);
3427 * Following are the conditional operators. They compare the TOS against
3428 * the primary and put a literal 1 in the primary if the condition is
3429 * true, otherwise they clear the primary register
3434 void g_eq (unsigned flags, unsigned long val)
3435 /* Test for equal */
3437 static char* ops [12] = {
3438 "toseq00", "toseqa0", "toseqax",
3439 "toseq00", "toseqa0", "toseqax",
3444 /* If the right hand side is const, the lhs is not on stack but still
3445 * in the primary register.
3447 if (flags & CF_CONST) {
3449 switch (flags & CF_TYPE) {
3452 if (flags & CF_FORCECHAR) {
3453 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3454 AddCodeLine ("\tjsr\tbooleq");
3460 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3461 AddCodeLine ("\tbne\t*+4");
3462 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3463 AddCodeLine ("\tjsr\tbooleq");
3473 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3474 * into the normal, non-optimized stuff.
3476 g_push (flags & ~CF_CONST, 0);
3480 /* Use long way over the stack */
3481 oper (flags, val, ops);
3486 void g_ne (unsigned flags, unsigned long val)
3487 /* Test for not equal */
3489 static char* ops [12] = {
3490 "tosne00", "tosnea0", "tosneax",
3491 "tosne00", "tosnea0", "tosneax",
3497 /* If the right hand side is const, the lhs is not on stack but still
3498 * in the primary register.
3500 if (flags & CF_CONST) {
3502 switch (flags & CF_TYPE) {
3505 if (flags & CF_FORCECHAR) {
3506 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3507 AddCodeLine ("\tjsr\tboolne");
3513 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3514 AddCodeLine ("\tbne\t*+4");
3515 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3516 AddCodeLine ("\tjsr\tboolne");
3526 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3527 * into the normal, non-optimized stuff.
3529 g_push (flags & ~CF_CONST, 0);
3533 /* Use long way over the stack */
3534 oper (flags, val, ops);
3539 void g_lt (unsigned flags, unsigned long val)
3540 /* Test for less than */
3542 static char* ops [12] = {
3543 "toslt00", "toslta0", "tosltax",
3544 "tosult00", "tosulta0", "tosultax",
3549 /* If the right hand side is const, the lhs is not on stack but still
3550 * in the primary register.
3552 if (flags & CF_CONST) {
3554 /* Give a warning in some special cases */
3555 if ((flags & CF_UNSIGNED) && val == 0) {
3556 Warning ("Condition is never true");
3559 /* Look at the type */
3560 switch (flags & CF_TYPE) {
3563 if (flags & CF_FORCECHAR) {
3564 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3565 if (flags & CF_UNSIGNED) {
3566 AddCodeLine ("\tjsr\tboolult");
3568 AddCodeLine ("\tjsr\tboollt");
3575 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3576 /* If we have a signed compare against zero, we only need to
3577 * test the high byte.
3579 AddCodeLine ("\ttxa");
3580 AddCodeLine ("\tjsr\tboollt");
3583 /* Direct code only for unsigned data types */
3584 if (flags & CF_UNSIGNED) {
3585 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3586 AddCodeLine ("\tbne\t*+4");
3587 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3588 AddCodeLine ("\tjsr\tboolult");
3600 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3601 * into the normal, non-optimized stuff.
3603 g_push (flags & ~CF_CONST, 0);
3607 /* Use long way over the stack */
3608 oper (flags, val, ops);
3613 void g_le (unsigned flags, unsigned long val)
3614 /* Test for less than or equal to */
3616 static char* ops [12] = {
3617 "tosle00", "toslea0", "tosleax",
3618 "tosule00", "tosulea0", "tosuleax",
3624 /* If the right hand side is const, the lhs is not on stack but still
3625 * in the primary register.
3627 if (flags & CF_CONST) {
3629 /* Look at the type */
3630 switch (flags & CF_TYPE) {
3633 if (flags & CF_FORCECHAR) {
3634 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3635 if (flags & CF_UNSIGNED) {
3636 AddCodeLine ("\tjsr\tboolule");
3638 AddCodeLine ("\tjsr\tboolle");
3645 if (flags & CF_UNSIGNED) {
3646 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3647 AddCodeLine ("\tbne\t*+4");
3648 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3649 AddCodeLine ("\tjsr\tboolule");
3661 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3662 * into the normal, non-optimized stuff.
3664 g_push (flags & ~CF_CONST, 0);
3668 /* Use long way over the stack */
3669 oper (flags, val, ops);
3674 void g_gt (unsigned flags, unsigned long val)
3675 /* Test for greater than */
3677 static char* ops [12] = {
3678 "tosgt00", "tosgta0", "tosgtax",
3679 "tosugt00", "tosugta0", "tosugtax",
3685 /* If the right hand side is const, the lhs is not on stack but still
3686 * in the primary register.
3688 if (flags & CF_CONST) {
3690 /* Look at the type */
3691 switch (flags & CF_TYPE) {
3694 if (flags & CF_FORCECHAR) {
3695 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3696 if (flags & CF_UNSIGNED) {
3697 /* If we have a compare > 0, we will replace it by
3698 * != 0 here, since both are identical but the latter
3699 * is easier to optimize.
3702 AddCodeLine ("\tjsr\tboolugt");
3704 AddCodeLine ("\tjsr\tboolne");
3707 AddCodeLine ("\tjsr\tboolgt");
3714 if (flags & CF_UNSIGNED) {
3715 /* If we have a compare > 0, we will replace it by
3716 * != 0 here, since both are identical but the latter
3717 * is easier to optimize.
3719 if ((val & 0xFFFF) == 0) {
3720 AddCodeLine ("\tstx\ttmp1");
3721 AddCodeLine ("\tora\ttmp1");
3722 AddCodeLine ("\tjsr\tboolne");
3724 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3725 AddCodeLine ("\tbne\t*+4");
3726 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3727 AddCodeLine ("\tjsr\tboolugt");
3740 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3741 * into the normal, non-optimized stuff.
3743 g_push (flags & ~CF_CONST, 0);
3747 /* Use long way over the stack */
3748 oper (flags, val, ops);
3753 void g_ge (unsigned flags, unsigned long val)
3754 /* Test for greater than or equal to */
3756 static char* ops [12] = {
3757 "tosge00", "tosgea0", "tosgeax",
3758 "tosuge00", "tosugea0", "tosugeax",
3764 /* If the right hand side is const, the lhs is not on stack but still
3765 * in the primary register.
3767 if (flags & CF_CONST) {
3769 /* Give a warning in some special cases */
3770 if ((flags & CF_UNSIGNED) && val == 0) {
3771 Warning ("Condition is always true");
3774 /* Look at the type */
3775 switch (flags & CF_TYPE) {
3778 if (flags & CF_FORCECHAR) {
3779 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3780 if (flags & CF_UNSIGNED) {
3781 AddCodeLine ("\tjsr\tbooluge");
3783 AddCodeLine ("\tjsr\tboolge");
3790 if (flags & CF_UNSIGNED) {
3791 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3792 AddCodeLine ("\tbne\t*+4");
3793 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3794 AddCodeLine ("\tjsr\tbooluge");
3806 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3807 * into the normal, non-optimized stuff.
3809 g_push (flags & ~CF_CONST, 0);
3813 /* Use long way over the stack */
3814 oper (flags, val, ops);
3819 /*****************************************************************************/
3820 /* Allocating static storage */
3821 /*****************************************************************************/
3825 void g_res (unsigned n)
3826 /* Reserve static storage, n bytes */
3828 AddCodeLine ("\t.res\t%u,$00", n);
3833 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3834 /* Define data with the size given in flags */
3836 if (flags & CF_CONST) {
3838 /* Numeric constant */
3839 switch (flags & CF_TYPE) {
3842 AddCodeLine ("\t.byte\t$%02lX", val & 0xFF);
3846 AddCodeLine ("\t.word\t$%04lX", val & 0xFFFF);
3850 AddCodeLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3861 /* Create the correct label name */
3862 const char* Label = GetLabelName (flags, val, offs);
3864 /* Labels are always 16 bit */
3865 AddCodeLine ("\t.word\t%s", Label);
3872 void g_defbytes (const void* Bytes, unsigned Count)
3873 /* Output a row of bytes as a constant */
3879 /* Cast the buffer pointer */
3880 const unsigned char* Data = (const unsigned char*) Bytes;
3882 /* Output the stuff */
3885 /* How many go into this line? */
3886 if ((Chunk = Count) > 16) {
3891 /* Output one line */
3892 strcpy (Buf, "\t.byte\t");
3895 B += sprintf (B, "$%02X", *Data++);
3901 /* Output the line */
3908 void g_zerobytes (unsigned n)
3909 /* Output n bytes of data initialized with zero */
3911 AddCodeLine ("\t.res\t%u,$00", n);
3916 /*****************************************************************************/
3917 /* Inlined known functions */
3918 /*****************************************************************************/
3922 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3923 /* Inline the strlen() function */
3925 /* We need a label in both cases */
3926 unsigned label = GetLabel ();
3928 /* Two different encodings */
3929 if (flags & CF_CONST) {
3931 /* The address of the string is constant. Create the correct label name */
3932 char* lbuf = GetLabelName (flags, val, offs);
3934 /* Generate the strlen code */
3935 AddCodeLine ("\tldy\t#$FF");
3936 g_defloclabel (label);
3937 AddCodeLine ("\tiny");
3938 AddCodeLine ("\tlda\t%s,y", lbuf);
3939 AddCodeLine ("\tbne\tL%04X", label);
3940 AddCodeLine ("\ttax");
3941 AddCodeLine ("\ttya");
3945 /* Address not constant but in primary */
3946 if (CodeSizeFactor < 400) {
3947 /* This is too much code, so call strlen instead of inlining */
3948 AddCodeLine ("\tjsr\t_strlen");
3950 /* Inline the function */
3951 AddCodeLine ("\tsta\tptr1");
3952 AddCodeLine ("\tstx\tptr1+1");
3953 AddCodeLine ("\tldy\t#$FF");
3954 g_defloclabel (label);
3955 AddCodeLine ("\tiny");
3956 AddCodeLine ("\tlda\t(ptr1),y");
3957 AddCodeLine ("\tbne\tL%04X", label);
3958 AddCodeLine ("\ttax");
3959 AddCodeLine ("\ttya");