1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2001 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 size of all parameters. */
988 if (ArgSizeOffs == 0 && CPU == CPU_65C02) {
989 AddCodeLine ("\tlda\t(sp)");
991 ldyconst (ArgSizeOffs);
992 AddCodeLine ("\tlda\t(sp),y");
995 /* Add the value of the stackpointer */
996 if (CodeSizeFactor > 250) {
997 AddCodeLine ("\tldx\tsp+1");
998 AddCodeLine ("\tclc");
999 AddCodeLine ("\tadc\tsp");
1000 AddCodeLine ("\tbcc\t*+3");
1001 AddCodeLine ("\tinx");
1002 AddCodeHint ("x:!"); /* Invalidate X */
1004 AddCodeLine ("\tjsr\tleaasp");
1007 /* Add the offset to the primary */
1009 g_inc (CF_INT | CF_CONST, Offs);
1010 } else if (Offs < 0) {
1011 g_dec (CF_INT | CF_CONST, -Offs);
1017 /*****************************************************************************/
1018 /* Store into memory */
1019 /*****************************************************************************/
1023 void g_putstatic (unsigned flags, unsigned long label, unsigned offs)
1024 /* Store the primary register into the specified static memory cell */
1026 /* Create the correct label name */
1027 char* lbuf = GetLabelName (flags, label, offs);
1029 /* Check the size and generate the correct store operation */
1030 switch (flags & CF_TYPE) {
1033 AddCodeLine ("\tsta\t%s", lbuf);
1037 AddCodeLine ("\tsta\t%s", lbuf);
1038 AddCodeLine ("\tstx\t%s+1", lbuf);
1042 AddCodeLine ("\tsta\t%s", lbuf);
1043 AddCodeLine ("\tstx\t%s+1", lbuf);
1044 AddCodeLine ("\tldy\tsreg");
1045 AddCodeLine ("\tsty\t%s+2", lbuf);
1046 AddCodeLine ("\tldy\tsreg+1");
1047 AddCodeLine ("\tsty\t%s+3", lbuf);
1058 void g_putlocal (unsigned Flags, int Offs, long Val)
1059 /* Put data into local object. */
1062 CheckLocalOffs (Offs);
1063 switch (Flags & CF_TYPE) {
1066 if (Flags & CF_CONST) {
1067 AddCodeLine ("\tlda\t#$%02X", (unsigned char) Val);
1069 if (CPU == CPU_65C02 && Offs == 0) {
1070 AddCodeLine ("\tsta\t(sp)");
1073 AddCodeLine ("\tsta\t(sp),y");
1078 if (Flags & CF_CONST) {
1080 AddCodeLine ("\tlda\t#$%02X", (unsigned char) (Val >> 8));
1081 AddCodeLine ("\tsta\t(sp),y");
1082 if ((Flags & CF_NOKEEP) == 0) {
1083 /* Place high byte into X */
1084 AddCodeLine ("\ttax");
1086 if (CPU == CPU_65C02 && Offs == 0) {
1087 AddCodeLine ("\tlda\t#$%02X", (unsigned char) Val);
1088 AddCodeLine ("\tsta\t(sp)");
1090 if ((Val & 0xFF) == Offs+1) {
1091 /* The value we need is already in Y */
1092 AddCodeLine ("\ttya");
1093 AddCodeLine ("\tdey");
1095 AddCodeLine ("\tdey");
1096 AddCodeLine ("\tlda\t#$%02X", (unsigned char) Val);
1098 AddCodeLine ("\tsta\t(sp),y");
1101 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
1104 AddCodeLine ("\tjsr\tstaxysp");
1106 AddCodeLine ("\tjsr\tstax0sp");
1109 if (CPU == CPU_65C02 && Offs == 0) {
1110 AddCodeLine ("\tsta\t(sp)");
1112 AddCodeLine ("\ttxa");
1113 AddCodeLine ("\tsta\t(sp),y");
1116 AddCodeLine ("\tsta\t(sp),y");
1117 AddCodeLine ("\tiny");
1118 AddCodeLine ("\ttxa");
1119 AddCodeLine ("\tsta\t(sp),y");
1126 if (Flags & CF_CONST) {
1127 g_getimmed (Flags, Val, 0);
1131 AddCodeLine ("\tjsr\tsteaxysp");
1133 AddCodeLine ("\tjsr\tsteax0sp");
1145 void g_putind (unsigned Flags, unsigned Offs)
1146 /* Store the specified object type in the primary register at the address
1147 * on the top of the stack
1150 /* We can handle offsets below $100 directly, larger offsets must be added
1151 * to the address. Since a/x is in use, best code is achieved by adding
1152 * just the high byte. Be sure to check if the low byte will overflow while
1155 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1157 /* Overflow - we need to add the low byte also */
1158 AddCodeLine ("\tldy\t#$00");
1159 AddCodeLine ("\tclc");
1160 AddCodeLine ("\tpha");
1161 AddCodeLine ("\tlda\t#$%02X", Offs & 0xFF);
1162 AddCodeLine ("\tadc\t(sp),y");
1163 AddCodeLine ("\tsta\t(sp),y");
1164 AddCodeLine ("\tiny");
1165 AddCodeLine ("\tlda\t#$%02X", (Offs >> 8) & 0xFF);
1166 AddCodeLine ("\tadc\t(sp),y");
1167 AddCodeLine ("\tsta\t(sp),y");
1168 AddCodeLine ("\tpla");
1170 /* Complete address is on stack, new offset is zero */
1173 } else if ((Offs & 0xFF00) != 0) {
1175 /* We can just add the high byte */
1176 AddCodeLine ("\tldy\t#$01");
1177 AddCodeLine ("\tclc");
1178 AddCodeLine ("\tpha");
1179 AddCodeLine ("\tlda\t#$%02X", (Offs >> 8) & 0xFF);
1180 AddCodeLine ("\tadc\t(sp),y");
1181 AddCodeLine ("\tsta\t(sp),y");
1182 AddCodeLine ("\tpla");
1184 /* Offset is now just the low byte */
1188 /* Check the size and determine operation */
1189 switch (Flags & CF_TYPE) {
1194 AddCodeLine ("\tjsr\tstaspidx");
1196 AddCodeLine ("\tjsr\tstaspp");
1203 AddCodeLine ("\tjsr\tstaxspidx");
1205 AddCodeLine ("\tjsr\tstaxspp");
1212 AddCodeLine ("\tjsr\tsteaxspidx");
1214 AddCodeLine ("\tjsr\tsteaxspp");
1223 /* Pop the argument which is always a pointer */
1229 /*****************************************************************************/
1230 /* type conversion and similiar stuff */
1231 /*****************************************************************************/
1235 void g_toslong (unsigned flags)
1236 /* Make sure, the value on TOS is a long. Convert if necessary */
1238 switch (flags & CF_TYPE) {
1242 if (flags & CF_UNSIGNED) {
1243 AddCodeLine ("\tjsr\ttosulong");
1245 AddCodeLine ("\tjsr\ttoslong");
1260 void g_tosint (unsigned flags)
1261 /* Make sure, the value on TOS is an int. Convert if necessary */
1263 switch (flags & CF_TYPE) {
1270 AddCodeLine ("\tjsr\ttosint");
1281 void g_reglong (unsigned flags)
1282 /* Make sure, the value in the primary register a long. Convert if necessary */
1284 switch (flags & CF_TYPE) {
1288 if (flags & CF_UNSIGNED) {
1289 if (CodeSizeFactor >= 200) {
1291 AddCodeLine ("\tsty\tsreg");
1292 AddCodeLine ("\tsty\tsreg+1");
1294 AddCodeLine ("\tjsr\taxulong");
1297 AddCodeLine ("\tjsr\taxlong");
1311 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1312 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1313 * value, that corresponds to the value on TOS, rhs corresponds to the value
1314 * in (e)ax. The return value is the the flags value for the resulting type.
1317 unsigned ltype, rtype;
1320 /* Get the type spec from the flags */
1321 ltype = lhs & CF_TYPE;
1322 rtype = rhs & CF_TYPE;
1324 /* Check if a conversion is needed */
1325 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1326 /* We must promote the primary register to long */
1328 /* Get the new rhs type */
1329 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1331 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1332 /* We must promote the lhs to long */
1338 /* Get the new rhs type */
1339 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1343 /* Determine the result type for the operation:
1344 * - The result is const if both operands are const.
1345 * - The result is unsigned if one of the operands is unsigned.
1346 * - The result is long if one of the operands is long.
1347 * - Otherwise the result is int sized.
1349 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1350 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1351 if (rtype == CF_LONG || ltype == CF_LONG) {
1361 unsigned g_typecast (unsigned lhs, unsigned rhs)
1362 /* Cast the value in the primary register to the operand size that is flagged
1363 * by the lhs value. Return the result value.
1366 unsigned ltype, rtype;
1368 /* Get the type spec from the flags */
1369 ltype = lhs & CF_TYPE;
1370 rtype = rhs & CF_TYPE;
1372 /* Check if a conversion is needed */
1373 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1374 /* We must promote the primary register to long */
1378 /* Do not need any other action. If the left type is int, and the primary
1379 * register is long, it will be automagically truncated. If the right hand
1380 * side is const, it is not located in the primary register and handled by
1381 * the expression parser code.
1384 /* Result is const if the right hand side was const */
1385 lhs |= (rhs & CF_CONST);
1387 /* The resulting type is that of the left hand side (that's why you called
1395 void g_scale (unsigned flags, long val)
1396 /* Scale the value in the primary register by the given value. If val is positive,
1397 * scale up, is val is negative, scale down. This function is used to scale
1398 * the operands or results of pointer arithmetic by the size of the type, the
1399 * pointer points to.
1404 /* Value may not be zero */
1406 Internal ("Data type has no size");
1407 } else if (val > 0) {
1410 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1412 /* Factor is 2, 4 or 8, use special function */
1413 switch (flags & CF_TYPE) {
1416 if (flags & CF_FORCECHAR) {
1418 AddCodeLine ("\tasl\ta");
1425 if (CodeSizeFactor >= (p2+1)*130U) {
1426 AddCodeLine ("\tstx\ttmp1");
1428 AddCodeLine ("\tasl\ta");
1429 AddCodeLine ("\trol\ttmp1");
1431 AddCodeLine ("\tldx\ttmp1");
1433 if (flags & CF_UNSIGNED) {
1434 AddCodeLine ("\tjsr\tshlax%d", p2);
1436 AddCodeLine ("\tjsr\taslax%d", p2);
1442 if (flags & CF_UNSIGNED) {
1443 AddCodeLine ("\tjsr\tshleax%d", p2);
1445 AddCodeLine ("\tjsr\tasleax%d", p2);
1454 } else if (val != 1) {
1456 /* Use a multiplication instead */
1457 g_mul (flags | CF_CONST, val);
1465 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1467 /* Factor is 2, 4 or 8, use special function */
1468 switch (flags & CF_TYPE) {
1471 if (flags & CF_FORCECHAR) {
1472 if (flags & CF_UNSIGNED) {
1474 AddCodeLine ("\tlsr\ta");
1477 } else if (p2 <= 2) {
1478 AddCodeLine ("\tcmp\t#$80");
1479 AddCodeLine ("\tror\ta");
1486 if (flags & CF_UNSIGNED) {
1487 if (CodeSizeFactor >= (p2+1)*130U) {
1488 AddCodeLine ("\tstx\ttmp1");
1490 AddCodeLine ("\tlsr\ttmp1");
1491 AddCodeLine ("\tror\ta");
1493 AddCodeLine ("\tldx\ttmp1");
1495 AddCodeLine ("\tjsr\tlsrax%d", p2);
1498 if (CodeSizeFactor >= (p2+1)*150U) {
1499 AddCodeLine ("\tstx\ttmp1");
1501 AddCodeLine ("\tcpx\t#$80");
1502 AddCodeLine ("\tror\ttmp1");
1503 AddCodeLine ("\tror\ta");
1505 AddCodeLine ("\tldx\ttmp1");
1507 AddCodeLine ("\tjsr\tasrax%d", p2);
1513 if (flags & CF_UNSIGNED) {
1514 AddCodeLine ("\tjsr\tlsreax%d", p2);
1516 AddCodeLine ("\tjsr\tasreax%d", p2);
1525 } else if (val != 1) {
1527 /* Use a division instead */
1528 g_div (flags | CF_CONST, val);
1536 /*****************************************************************************/
1537 /* Adds and subs of variables fix a fixed address */
1538 /*****************************************************************************/
1542 void g_addlocal (unsigned flags, int offs)
1543 /* Add a local variable to ax */
1545 /* Correct the offset and check it */
1547 CheckLocalOffs (offs);
1549 switch (flags & CF_TYPE) {
1552 AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1553 AddCodeLine ("\tclc");
1554 AddCodeLine ("\tadc\t(sp),y");
1555 AddCodeLine ("\tbcc\t*+3");
1556 AddCodeLine ("\tinx");
1557 AddCodeHint ("x:!");
1561 AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1562 AddCodeLine ("\tclc");
1563 AddCodeLine ("\tadc\t(sp),y");
1564 AddCodeLine ("\tpha");
1565 AddCodeLine ("\ttxa");
1566 AddCodeLine ("\tiny");
1567 AddCodeLine ("\tadc\t(sp),y");
1568 AddCodeLine ("\ttax");
1569 AddCodeLine ("\tpla");
1573 /* Do it the old way */
1575 g_getlocal (flags, offs);
1587 void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
1588 /* Add a static variable to ax */
1590 /* Create the correct label name */
1591 char* lbuf = GetLabelName (flags, label, offs);
1593 switch (flags & CF_TYPE) {
1596 AddCodeLine ("\tclc");
1597 AddCodeLine ("\tadc\t%s", lbuf);
1598 AddCodeLine ("\tbcc\t*+3");
1599 AddCodeLine ("\tinx");
1600 AddCodeHint ("x:!");
1604 AddCodeLine ("\tclc");
1605 AddCodeLine ("\tadc\t%s", lbuf);
1606 AddCodeLine ("\ttay");
1607 AddCodeLine ("\ttxa");
1608 AddCodeLine ("\tadc\t%s+1", lbuf);
1609 AddCodeLine ("\ttax");
1610 AddCodeLine ("\ttya");
1614 /* Do it the old way */
1616 g_getstatic (flags, label, offs);
1628 /*****************************************************************************/
1629 /* Compares of ax with a variable with fixed address */
1630 /*****************************************************************************/
1634 void g_cmplocal (unsigned flags, int offs)
1635 /* Compare a local variable to ax */
1637 Internal ("g_cmplocal not implemented");
1642 void g_cmpstatic (unsigned flags, unsigned label, unsigned offs)
1643 /* Compare a static variable to ax */
1645 Internal ("g_cmpstatic not implemented");
1650 /*****************************************************************************/
1651 /* Special op= functions */
1652 /*****************************************************************************/
1656 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
1658 /* Emit += for a static variable */
1660 /* Create the correct label name */
1661 char* lbuf = GetLabelName (flags, label, offs);
1663 /* Check the size and determine operation */
1664 switch (flags & CF_TYPE) {
1667 if (flags & CF_FORCECHAR) {
1668 AddCodeLine ("\tldx\t#$00");
1669 if (flags & CF_CONST) {
1671 AddCodeLine ("\tinc\t%s", lbuf);
1672 AddCodeLine ("\tlda\t%s", lbuf);
1674 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1675 AddCodeLine ("\tclc");
1676 AddCodeLine ("\tadc\t%s", lbuf);
1677 AddCodeLine ("\tsta\t%s", lbuf);
1680 AddCodeLine ("\tclc");
1681 AddCodeLine ("\tadc\t%s", lbuf);
1682 AddCodeLine ("\tsta\t%s", lbuf);
1684 if ((flags & CF_UNSIGNED) == 0) {
1685 AddCodeLine ("\tbpl\t*+3");
1686 AddCodeLine ("\tdex");
1687 AddCodeHint ("x:!"); /* Invalidate X */
1694 if (flags & CF_CONST) {
1696 label = GetLabel ();
1697 AddCodeLine ("\tinc\t%s", lbuf);
1698 AddCodeLine ("\tbne\tL%04X", (int)label);
1699 AddCodeLine ("\tinc\t%s+1", lbuf);
1700 g_defloclabel (label);
1701 AddCodeLine ("\tlda\t%s", lbuf); /* Hmmm... */
1702 AddCodeLine ("\tldx\t%s+1", lbuf);
1704 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1705 AddCodeLine ("\tclc");
1706 AddCodeLine ("\tadc\t%s", lbuf);
1707 AddCodeLine ("\tsta\t%s", lbuf);
1709 label = GetLabel ();
1710 AddCodeLine ("\tbcc\tL%04X", (int)label);
1711 AddCodeLine ("\tinc\t%s+1", lbuf);
1712 g_defloclabel (label);
1713 AddCodeLine ("\tldx\t%s+1", lbuf);
1715 AddCodeLine ("\tlda\t#$%02X", (unsigned char)(val >> 8));
1716 AddCodeLine ("\tadc\t%s+1", lbuf);
1717 AddCodeLine ("\tsta\t%s+1", lbuf);
1718 AddCodeLine ("\ttax");
1719 AddCodeLine ("\tlda\t%s", lbuf);
1723 AddCodeLine ("\tclc");
1724 AddCodeLine ("\tadc\t%s", lbuf);
1725 AddCodeLine ("\tsta\t%s", lbuf);
1726 AddCodeLine ("\ttxa");
1727 AddCodeLine ("\tadc\t%s+1", lbuf);
1728 AddCodeLine ("\tsta\t%s+1", lbuf);
1729 AddCodeLine ("\ttax");
1730 AddCodeLine ("\tlda\t%s", lbuf);
1735 if (flags & CF_CONST) {
1737 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1738 AddCodeLine ("\tsty\tptr1");
1739 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1741 AddCodeLine ("\tjsr\tladdeq1");
1743 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1744 AddCodeLine ("\tjsr\tladdeqa");
1747 g_getstatic (flags, label, offs);
1749 g_putstatic (flags, label, offs);
1752 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1753 AddCodeLine ("\tsty\tptr1");
1754 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1755 AddCodeLine ("\tjsr\tladdeq");
1766 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1767 /* Emit += for a local variable */
1769 /* Calculate the true offset, check it, load it into Y */
1771 CheckLocalOffs (offs);
1773 /* Check the size and determine operation */
1774 switch (flags & CF_TYPE) {
1777 if (flags & CF_FORCECHAR) {
1779 AddCodeLine ("\tldx\t#$00");
1780 if (flags & CF_CONST) {
1781 AddCodeLine ("\tclc");
1782 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1783 AddCodeLine ("\tadc\t(sp,x)");
1784 AddCodeLine ("\tsta\t(sp,x)");
1786 AddCodeLine ("\tclc");
1787 AddCodeLine ("\tadc\t(sp,x)");
1788 AddCodeLine ("\tsta\t(sp,x)");
1792 AddCodeLine ("\tldx\t#$00");
1793 if (flags & CF_CONST) {
1794 AddCodeLine ("\tclc");
1795 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1796 AddCodeLine ("\tadc\t(sp),y");
1797 AddCodeLine ("\tsta\t(sp),y");
1799 AddCodeLine ("\tclc");
1800 AddCodeLine ("\tadc\t(sp),y");
1801 AddCodeLine ("\tsta\t(sp),y");
1804 if ((flags & CF_UNSIGNED) == 0) {
1805 AddCodeLine ("\tbpl\t*+3");
1806 AddCodeLine ("\tdex");
1807 AddCodeHint ("x:!"); /* Invalidate X */
1814 if (flags & CF_CONST) {
1815 g_getimmed (flags, val, 0);
1818 AddCodeLine ("\tjsr\taddeq0sp");
1821 AddCodeLine ("\tjsr\taddeqysp");
1826 if (flags & CF_CONST) {
1827 g_getimmed (flags, val, 0);
1830 AddCodeLine ("\tjsr\tladdeq0sp");
1833 AddCodeLine ("\tjsr\tladdeqysp");
1844 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1845 /* Emit += for the location with address in ax */
1847 /* If the offset is too large for a byte register, add the high byte
1848 * of the offset to the primary. Beware: We need a special correction
1849 * if the offset in the low byte will overflow in the operation.
1851 offs = MakeByteOffs (flags, offs);
1853 /* Check the size and determine operation */
1854 switch (flags & CF_TYPE) {
1857 AddCodeLine ("\tsta\tptr1");
1858 AddCodeLine ("\tstx\tptr1+1");
1860 AddCodeLine ("\tldx\t#$00");
1861 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1862 AddCodeLine ("\tclc");
1863 AddCodeLine ("\tadc\t(ptr1,x)");
1864 AddCodeLine ("\tsta\t(ptr1,x)");
1866 AddCodeLine ("\tldy\t#$%02X", offs);
1867 AddCodeLine ("\tldx\t#$00");
1868 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1869 AddCodeLine ("\tclc");
1870 AddCodeLine ("\tadc\t(ptr1),y");
1871 AddCodeLine ("\tsta\t(ptr1),y");
1876 if (CodeSizeFactor >= 200) {
1877 /* Lots of code, use only if size is not important */
1878 AddCodeLine ("\tsta\tptr1");
1879 AddCodeLine ("\tstx\tptr1+1");
1880 AddCodeLine ("\tldy\t#$%02X", offs);
1881 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1882 AddCodeLine ("\tclc");
1883 AddCodeLine ("\tadc\t(ptr1),y");
1884 AddCodeLine ("\tsta\t(ptr1),y");
1885 AddCodeLine ("\tpha");
1886 AddCodeLine ("\tiny");
1887 AddCodeLine ("\tlda\t#$%02X", (unsigned char)(val >> 8));
1888 AddCodeLine ("\tadc\t(ptr1),y");
1889 AddCodeLine ("\tsta\t(ptr1),y");
1890 AddCodeLine ("\ttax");
1891 AddCodeLine ("\tpla");
1897 AddCodeLine ("\tjsr\tpushax"); /* Push the address */
1898 push (flags); /* Correct the internal sp */
1899 g_getind (flags, offs); /* Fetch the value */
1900 g_inc (flags, val); /* Increment value in primary */
1901 g_putind (flags, offs); /* Store the value back */
1911 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1913 /* Emit -= for a static variable */
1915 /* Create the correct label name */
1916 char* lbuf = GetLabelName (flags, label, offs);
1918 /* Check the size and determine operation */
1919 switch (flags & CF_TYPE) {
1922 if (flags & CF_FORCECHAR) {
1923 AddCodeLine ("\tldx\t#$00");
1924 if (flags & CF_CONST) {
1926 AddCodeLine ("\tdec\t%s", lbuf);
1927 AddCodeLine ("\tlda\t%s", lbuf);
1929 AddCodeLine ("\tsec");
1930 AddCodeLine ("\tlda\t%s", lbuf);
1931 AddCodeLine ("\tsbc\t#$%02X", (int)(val & 0xFF));
1932 AddCodeLine ("\tsta\t%s", lbuf);
1935 AddCodeLine ("\tsec");
1936 AddCodeLine ("\tsta\ttmp1");
1937 AddCodeLine ("\tlda\t%s", lbuf);
1938 AddCodeLine ("\tsbc\ttmp1");
1939 AddCodeLine ("\tsta\t%s", lbuf);
1941 if ((flags & CF_UNSIGNED) == 0) {
1942 AddCodeLine ("\tbpl\t*+3");
1943 AddCodeLine ("\tdex");
1944 AddCodeHint ("x:!"); /* Invalidate X */
1951 AddCodeLine ("\tsec");
1952 if (flags & CF_CONST) {
1953 AddCodeLine ("\tlda\t%s", lbuf);
1954 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
1955 AddCodeLine ("\tsta\t%s", lbuf);
1957 label = GetLabel ();
1958 AddCodeLine ("\tbcs\tL%04X", (unsigned)label);
1959 AddCodeLine ("\tdec\t%s+1", lbuf);
1960 g_defloclabel (label);
1961 AddCodeLine ("\tldx\t%s+1", lbuf);
1963 AddCodeLine ("\tlda\t%s+1", lbuf);
1964 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)(val >> 8));
1965 AddCodeLine ("\tsta\t%s+1", lbuf);
1966 AddCodeLine ("\ttax");
1967 AddCodeLine ("\tlda\t%s", lbuf);
1970 AddCodeLine ("\tsta\ttmp1");
1971 AddCodeLine ("\tlda\t%s", lbuf);
1972 AddCodeLine ("\tsbc\ttmp1");
1973 AddCodeLine ("\tsta\t%s", lbuf);
1974 AddCodeLine ("\tstx\ttmp1");
1975 AddCodeLine ("\tlda\t%s+1", lbuf);
1976 AddCodeLine ("\tsbc\ttmp1");
1977 AddCodeLine ("\tsta\t%s+1", lbuf);
1978 AddCodeLine ("\ttax");
1979 AddCodeLine ("\tlda\t%s", lbuf);
1984 if (flags & CF_CONST) {
1986 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1987 AddCodeLine ("\tsty\tptr1");
1988 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1990 AddCodeLine ("\tjsr\tlsubeq1");
1992 AddCodeLine ("\tlda\t#$%02X", (unsigned char)val);
1993 AddCodeLine ("\tjsr\tlsubeqa");
1996 g_getstatic (flags, label, offs);
1998 g_putstatic (flags, label, offs);
2001 AddCodeLine ("\tldy\t#<(%s)", lbuf);
2002 AddCodeLine ("\tsty\tptr1");
2003 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
2004 AddCodeLine ("\tjsr\tlsubeq");
2015 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
2016 /* Emit -= for a local variable */
2018 /* Calculate the true offset, check it, load it into Y */
2020 CheckLocalOffs (offs);
2022 /* Check the size and determine operation */
2023 switch (flags & CF_TYPE) {
2026 if (flags & CF_FORCECHAR) {
2028 AddCodeLine ("\tldx\t#$00");
2029 AddCodeLine ("\tsec");
2030 if (flags & CF_CONST) {
2031 AddCodeLine ("\tlda\t(sp),y");
2032 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2034 AddCodeLine ("\tsta\ttmp1");
2035 AddCodeLine ("\tlda\t(sp),y");
2036 AddCodeLine ("\tsbc\ttmp1");
2038 AddCodeLine ("\tsta\t(sp),y");
2039 if ((flags & CF_UNSIGNED) == 0) {
2040 AddCodeLine ("\tbpl\t*+3");
2041 AddCodeLine ("\tdex");
2042 AddCodeHint ("x:!"); /* Invalidate X */
2049 if (flags & CF_CONST) {
2050 g_getimmed (flags, val, 0);
2053 AddCodeLine ("\tjsr\tsubeq0sp");
2056 AddCodeLine ("\tjsr\tsubeqysp");
2061 if (flags & CF_CONST) {
2062 g_getimmed (flags, val, 0);
2065 AddCodeLine ("\tjsr\tlsubeq0sp");
2068 AddCodeLine ("\tjsr\tlsubeqysp");
2079 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2080 /* Emit -= for the location with address in ax */
2082 /* If the offset is too large for a byte register, add the high byte
2083 * of the offset to the primary. Beware: We need a special correction
2084 * if the offset in the low byte will overflow in the operation.
2086 offs = MakeByteOffs (flags, offs);
2088 /* Check the size and determine operation */
2089 switch (flags & CF_TYPE) {
2092 AddCodeLine ("\tsta\tptr1");
2093 AddCodeLine ("\tstx\tptr1+1");
2095 AddCodeLine ("\tldx\t#$00");
2096 AddCodeLine ("\tlda\t(ptr1,x)");
2097 AddCodeLine ("\tsec");
2098 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2099 AddCodeLine ("\tsta\t(ptr1,x)");
2101 AddCodeLine ("\tldy\t#$%02X", offs);
2102 AddCodeLine ("\tldx\t#$00");
2103 AddCodeLine ("\tlda\t(ptr1),y");
2104 AddCodeLine ("\tsec");
2105 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2106 AddCodeLine ("\tsta\t(ptr1),y");
2111 if (CodeSizeFactor >= 200) {
2112 /* Lots of code, use only if size is not important */
2113 AddCodeLine ("\tsta\tptr1");
2114 AddCodeLine ("\tstx\tptr1+1");
2115 AddCodeLine ("\tldy\t#$%02X", offs);
2116 AddCodeLine ("\tlda\t(ptr1),y");
2117 AddCodeLine ("\tsec");
2118 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2119 AddCodeLine ("\tsta\t(ptr1),y");
2120 AddCodeLine ("\tpha");
2121 AddCodeLine ("\tiny");
2122 AddCodeLine ("\tlda\t(ptr1),y");
2123 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)(val >> 8));
2124 AddCodeLine ("\tsta\t(ptr1),y");
2125 AddCodeLine ("\ttax");
2126 AddCodeLine ("\tpla");
2132 AddCodeLine ("\tjsr\tpushax"); /* Push the address */
2133 push (flags); /* Correct the internal sp */
2134 g_getind (flags, offs); /* Fetch the value */
2135 g_dec (flags, val); /* Increment value in primary */
2136 g_putind (flags, offs); /* Store the value back */
2146 /*****************************************************************************/
2147 /* Add a variable address to the value in ax */
2148 /*****************************************************************************/
2152 void g_addaddr_local (unsigned flags, int offs)
2153 /* Add the address of a local variable to ax */
2155 /* Add the offset */
2158 /* We cannot address more then 256 bytes of locals anyway */
2159 CheckLocalOffs (offs);
2160 AddCodeLine ("\tclc");
2161 AddCodeLine ("\tadc\t#$%02X", offs & 0xFF);
2162 AddCodeLine ("\tbcc\t*+4"); /* Do also skip the CLC insn below */
2163 AddCodeLine ("\tinx");
2164 AddCodeHint ("x:!"); /* Invalidate X */
2167 /* Add the current stackpointer value */
2168 AddCodeLine ("\tclc");
2169 AddCodeLine ("\tadc\tsp");
2170 AddCodeLine ("\ttay");
2171 AddCodeLine ("\ttxa");
2172 AddCodeLine ("\tadc\tsp+1");
2173 AddCodeLine ("\ttax");
2174 AddCodeLine ("\ttya");
2179 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2180 /* Add the address of a static variable to ax */
2182 /* Create the correct label name */
2183 char* lbuf = GetLabelName (flags, label, offs);
2185 /* Add the address to the current ax value */
2186 AddCodeLine ("\tclc");
2187 AddCodeLine ("\tadc\t#<(%s)", lbuf);
2188 AddCodeLine ("\ttay");
2189 AddCodeLine ("\ttxa");
2190 AddCodeLine ("\tadc\t#>(%s)", lbuf);
2191 AddCodeLine ("\ttax");
2192 AddCodeLine ("\ttya");
2197 /*****************************************************************************/
2199 /*****************************************************************************/
2203 void g_save (unsigned flags)
2204 /* Copy primary register to hold register. */
2206 /* Check the size and determine operation */
2207 switch (flags & CF_TYPE) {
2210 if (flags & CF_FORCECHAR) {
2211 AddCodeLine ("\tpha");
2217 AddCodeLine ("\tsta\tregsave");
2218 AddCodeLine ("\tstx\tregsave+1");
2222 AddCodeLine ("\tjsr\tsaveeax");
2232 void g_restore (unsigned flags)
2233 /* Copy hold register to P. */
2235 /* Check the size and determine operation */
2236 switch (flags & CF_TYPE) {
2239 if (flags & CF_FORCECHAR) {
2240 AddCodeLine ("\tpla");
2246 AddCodeLine ("\tlda\tregsave");
2247 AddCodeLine ("\tldx\tregsave+1");
2251 AddCodeLine ("\tjsr\tresteax");
2261 void g_cmp (unsigned flags, unsigned long val)
2262 /* Immidiate compare. The primary register will not be changed, Z flag
2266 /* Check the size and determine operation */
2267 switch (flags & CF_TYPE) {
2270 if (flags & CF_FORCECHAR) {
2271 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
2277 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
2278 AddCodeLine ("\tbne\t*+4");
2279 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
2283 Internal ("g_cmp: Long compares not implemented");
2293 static void oper (unsigned flags, unsigned long val, char** subs)
2294 /* Encode a binary operation. subs is a pointer to four groups of three
2296 * 0-2 --> Operate on ints
2297 * 3-5 --> Operate on unsigneds
2298 * 6-8 --> Operate on longs
2299 * 9-11 --> Operate on unsigned longs
2301 * The first subroutine names in each string group is used to encode an
2302 * operation with a zero constant, the second to encode an operation with
2303 * a 8 bit constant, and the third is used in all other cases.
2308 /* Determine the offset into the array */
2309 offs = (flags & CF_UNSIGNED)? 3 : 0;
2310 switch (flags & CF_TYPE) {
2323 /* Encode the operation */
2324 if (flags & CF_CONST) {
2325 /* Constant value given */
2326 if (val == 0 && subs [offs+0]) {
2327 /* Special case: constant with value zero */
2328 AddCodeLine ("\tjsr\t%s", subs [offs+0]);
2329 } else if (val < 0x100 && subs [offs+1]) {
2330 /* Special case: constant with high byte zero */
2331 ldaconst (val); /* Load low byte */
2332 AddCodeLine ("\tjsr\t%s", subs [offs+1]);
2334 /* Others: arbitrary constant value */
2335 g_getimmed (flags, val, 0); /* Load value */
2336 AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2339 /* Value not constant (is already in (e)ax) */
2340 AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2343 /* The operation will pop it's argument */
2349 void g_test (unsigned flags)
2350 /* Force a test to set cond codes right */
2352 switch (flags & CF_TYPE) {
2355 if (flags & CF_FORCECHAR) {
2356 AddCodeLine ("\ttax");
2362 AddCodeLine ("\tstx\ttmp1");
2363 AddCodeLine ("\tora\ttmp1");
2367 if (flags & CF_UNSIGNED) {
2368 AddCodeLine ("\tjsr\tutsteax");
2370 AddCodeLine ("\tjsr\ttsteax");
2382 void g_push (unsigned flags, unsigned long val)
2383 /* Push the primary register or a constant value onto the stack */
2387 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2389 /* We have a constant 8 or 16 bit value */
2390 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2392 /* Handle as 8 bit value */
2393 if (CodeSizeFactor >= 165 || val > 2) {
2395 AddCodeLine ("\tjsr\tpusha");
2397 AddCodeLine ("\tjsr\tpushc%d", (int) val);
2402 /* Handle as 16 bit value */
2403 hi = (unsigned char) (val >> 8);
2405 AddCodeLine ("\tjsr\tpush%u", (unsigned) val);
2406 } else if (hi == 0 || hi == 0xFF) {
2407 /* Use special function */
2409 AddCodeLine ("\tjsr\t%s", (hi == 0)? "pusha0" : "pushaFF");
2412 g_getimmed (flags, val, 0);
2413 AddCodeLine ("\tjsr\tpushax");
2419 /* Value is not 16 bit or not constant */
2420 if (flags & CF_CONST) {
2421 /* Constant 32 bit value, load into eax */
2422 g_getimmed (flags, val, 0);
2425 /* Push the primary register */
2426 switch (flags & CF_TYPE) {
2429 if (flags & CF_FORCECHAR) {
2430 /* Handle as char */
2431 AddCodeLine ("\tjsr\tpusha");
2436 AddCodeLine ("\tjsr\tpushax");
2440 AddCodeLine ("\tjsr\tpusheax");
2450 /* Adjust the stack offset */
2456 void g_swap (unsigned flags)
2457 /* Swap the primary register and the top of the stack. flags give the type
2458 * of *both* values (must have same size).
2461 switch (flags & CF_TYPE) {
2465 AddCodeLine ("\tjsr\tswapstk");
2469 AddCodeLine ("\tjsr\tswapestk");
2480 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2481 /* Call the specified subroutine name */
2483 if ((Flags & CF_FIXARGC) == 0) {
2484 /* Pass the argument count */
2487 AddCodeLine ("\tjsr\t_%s", Label);
2488 oursp += ArgSize; /* callee pops args */
2493 void g_callind (unsigned Flags, unsigned ArgSize)
2494 /* Call subroutine with address in AX */
2496 if ((Flags & CF_FIXARGC) == 0) {
2497 /* Pass arg count */
2500 AddCodeLine ("\tjsr\tcallax"); /* do the call */
2501 oursp += ArgSize; /* callee pops args */
2506 void g_jump (unsigned Label)
2507 /* Jump to specified internal label number */
2509 AddCodeLine ("\tjmp\tL%04X", Label);
2514 void g_switch (unsigned Flags)
2515 /* Output switch statement preamble */
2517 switch (Flags & CF_TYPE) {
2521 AddCodeLine ("\tjsr\tswitch");
2525 AddCodeLine ("\tjsr\tlswitch");
2536 void g_case (unsigned flags, unsigned label, unsigned long val)
2537 /* Create table code for one case selector */
2539 switch (flags & CF_TYPE) {
2543 AddCodeLine ("\t.word\t$%04X, L%04X",
2544 (unsigned)(val & 0xFFFF),
2545 (unsigned)(label & 0xFFFF));
2549 AddCodeLine ("\t.dword\t$%08lX", val);
2550 AddCodeLine ("\t.word\tL%04X", label & 0xFFFF);
2561 void g_truejump (unsigned flags, unsigned label)
2562 /* Jump to label if zero flag clear */
2564 if (flags & CF_SHORT) {
2565 AddCodeLine ("\tbne\tL%04X", label);
2567 AddCodeLine ("\tjne\tL%04X", label);
2573 void g_falsejump (unsigned flags, unsigned label)
2574 /* Jump to label if zero flag set */
2576 if (flags & CF_SHORT) {
2577 AddCodeLine ("\tbeq\tL%04X", label);
2579 AddCodeLine ("\tjeq\tL%04X", label);
2585 static void mod_internal (int k, char* verb1, char* verb2)
2588 AddCodeLine ("\tjsr\t%ssp%c", verb1, k + '0');
2592 AddCodeLine ("\tjsr\t%ssp", verb2);
2598 void g_space (int space)
2599 /* Create or drop space on the stack */
2602 mod_internal (-space, "inc", "addy");
2603 } else if (space > 0) {
2604 mod_internal (space, "dec", "suby");
2610 void g_cstackcheck (void)
2611 /* Check for a C stack overflow */
2613 AddCodeLine ("\tjsr\tcstkchk");
2618 void g_stackcheck (void)
2619 /* Check for a stack overflow */
2621 AddCodeLine ("\tjsr\tstkchk");
2626 void g_add (unsigned flags, unsigned long val)
2627 /* Primary = TOS + Primary */
2629 static char* ops [12] = {
2630 0, "tosadda0", "tosaddax",
2631 0, "tosadda0", "tosaddax",
2636 if (flags & CF_CONST) {
2637 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2638 g_push (flags & ~CF_CONST, 0);
2640 oper (flags, val, ops);
2645 void g_sub (unsigned flags, unsigned long val)
2646 /* Primary = TOS - Primary */
2648 static char* ops [12] = {
2649 0, "tossuba0", "tossubax",
2650 0, "tossuba0", "tossubax",
2655 if (flags & CF_CONST) {
2656 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2657 g_push (flags & ~CF_CONST, 0);
2659 oper (flags, val, ops);
2664 void g_rsub (unsigned flags, unsigned long val)
2665 /* Primary = Primary - TOS */
2667 static char* ops [12] = {
2668 0, "tosrsuba0", "tosrsubax",
2669 0, "tosrsuba0", "tosrsubax",
2673 oper (flags, val, ops);
2678 void g_mul (unsigned flags, unsigned long val)
2679 /* Primary = TOS * Primary */
2681 static char* ops [12] = {
2682 0, "tosmula0", "tosmulax",
2683 0, "tosumula0", "tosumulax",
2690 /* Do strength reduction if the value is constant and a power of two */
2691 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2692 /* Generate a shift instead */
2697 /* If the right hand side is const, the lhs is not on stack but still
2698 * in the primary register.
2700 if (flags & CF_CONST) {
2702 switch (flags & CF_TYPE) {
2705 if (flags & CF_FORCECHAR) {
2706 /* Handle some special cases */
2710 AddCodeLine ("\tsta\ttmp1");
2711 AddCodeLine ("\tasl\ta");
2712 AddCodeLine ("\tclc");
2713 AddCodeLine ("\tadc\ttmp1");
2717 AddCodeLine ("\tsta\ttmp1");
2718 AddCodeLine ("\tasl\ta");
2719 AddCodeLine ("\tasl\ta");
2720 AddCodeLine ("\tclc");
2721 AddCodeLine ("\tadc\ttmp1");
2725 AddCodeLine ("\tsta\ttmp1");
2726 AddCodeLine ("\tasl\ta");
2727 AddCodeLine ("\tasl\ta");
2728 AddCodeLine ("\tclc");
2729 AddCodeLine ("\tadc\ttmp1");
2730 AddCodeLine ("\tasl\ta");
2746 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2747 * into the normal, non-optimized stuff.
2749 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2750 g_push (flags & ~CF_CONST, 0);
2754 /* Use long way over the stack */
2755 oper (flags, val, ops);
2760 void g_div (unsigned flags, unsigned long val)
2761 /* Primary = TOS / Primary */
2763 static char* ops [12] = {
2764 0, "tosdiva0", "tosdivax",
2765 0, "tosudiva0", "tosudivax",
2770 /* Do strength reduction if the value is constant and a power of two */
2772 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2773 /* Generate a shift instead */
2776 /* Generate a division */
2777 if (flags & CF_CONST) {
2778 /* lhs is not on stack */
2779 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2780 g_push (flags & ~CF_CONST, 0);
2782 oper (flags, val, ops);
2788 void g_mod (unsigned flags, unsigned long val)
2789 /* Primary = TOS % Primary */
2791 static char* ops [12] = {
2792 0, "tosmoda0", "tosmodax",
2793 0, "tosumoda0", "tosumodax",
2799 /* Check if we can do some cost reduction */
2800 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2801 /* We can do that with an AND operation */
2802 g_and (flags, val - 1);
2804 /* Do it the hard way... */
2805 if (flags & CF_CONST) {
2806 /* lhs is not on stack */
2807 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2808 g_push (flags & ~CF_CONST, 0);
2810 oper (flags, val, ops);
2816 void g_or (unsigned flags, unsigned long val)
2817 /* Primary = TOS | Primary */
2819 static char* ops [12] = {
2820 0, "tosora0", "tosorax",
2821 0, "tosora0", "tosorax",
2826 /* If the right hand side is const, the lhs is not on stack but still
2827 * in the primary register.
2829 if (flags & CF_CONST) {
2831 switch (flags & CF_TYPE) {
2834 if (flags & CF_FORCECHAR) {
2835 if ((val & 0xFF) != 0xFF) {
2836 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2844 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2851 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2860 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2861 * into the normal, non-optimized stuff.
2863 g_push (flags & ~CF_CONST, 0);
2867 /* Use long way over the stack */
2868 oper (flags, val, ops);
2873 void g_xor (unsigned flags, unsigned long val)
2874 /* Primary = TOS ^ Primary */
2876 static char* ops [12] = {
2877 0, "tosxora0", "tosxorax",
2878 0, "tosxora0", "tosxorax",
2884 /* If the right hand side is const, the lhs is not on stack but still
2885 * in the primary register.
2887 if (flags & CF_CONST) {
2889 switch (flags & CF_TYPE) {
2892 if (flags & CF_FORCECHAR) {
2893 if ((val & 0xFF) != 0) {
2894 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2903 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2906 } else if ((val & 0xFF) == 0) {
2907 AddCodeLine ("\tpha");
2908 AddCodeLine ("\ttxa");
2909 AddCodeLine ("\teor\t#$%02X", (unsigned char)(val >> 8));
2910 AddCodeLine ("\ttax");
2911 AddCodeLine ("\tpla");
2919 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2929 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2930 * into the normal, non-optimized stuff.
2932 g_push (flags & ~CF_CONST, 0);
2936 /* Use long way over the stack */
2937 oper (flags, val, ops);
2942 void g_and (unsigned flags, unsigned long val)
2943 /* Primary = TOS & Primary */
2945 static char* ops [12] = {
2946 0, "tosanda0", "tosandax",
2947 0, "tosanda0", "tosandax",
2952 /* If the right hand side is const, the lhs is not on stack but still
2953 * in the primary register.
2955 if (flags & CF_CONST) {
2957 switch (flags & CF_TYPE) {
2960 if (flags & CF_FORCECHAR) {
2961 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2966 if ((val & 0xFFFF) != 0xFFFF) {
2971 } else if (val != 0xFF) {
2972 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2974 } else if ((val & 0xFF00) == 0xFF00) {
2975 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2976 } else if ((val & 0x00FF) == 0x0000) {
2977 AddCodeLine ("\ttxa");
2978 AddCodeLine ("\tand\t#$%02X", (unsigned char)(val >> 8));
2979 AddCodeLine ("\ttax");
2982 AddCodeLine ("\ttay");
2983 AddCodeLine ("\ttxa");
2984 AddCodeLine ("\tand\t#$%02X", (unsigned char)(val >> 8));
2985 AddCodeLine ("\ttax");
2986 AddCodeLine ("\ttya");
2987 if ((val & 0x00FF) != 0x00FF) {
2988 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2997 AddCodeLine ("\tstx\tsreg+1");
2998 AddCodeLine ("\tstx\tsreg");
2999 if ((val & 0xFF) != 0xFF) {
3000 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
3003 } else if (val == 0xFF00) {
3005 AddCodeLine ("\tsta\tsreg+1");
3006 AddCodeLine ("\tsta\tsreg");
3015 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3016 * into the normal, non-optimized stuff.
3018 g_push (flags & ~CF_CONST, 0);
3022 /* Use long way over the stack */
3023 oper (flags, val, ops);
3028 void g_asr (unsigned flags, unsigned long val)
3029 /* Primary = TOS >> Primary */
3031 static char* ops [12] = {
3032 0, "tosasra0", "tosasrax",
3033 0, "tosshra0", "tosshrax",
3038 /* If the right hand side is const, the lhs is not on stack but still
3039 * in the primary register.
3041 if (flags & CF_CONST) {
3043 switch (flags & CF_TYPE) {
3047 if (val >= 1 && val <= 3) {
3048 if (flags & CF_UNSIGNED) {
3049 AddCodeLine ("\tjsr\tshrax%ld", val);
3051 AddCodeLine ("\tjsr\tasrax%ld", val);
3054 } else if (val == 8 && (flags & CF_UNSIGNED)) {
3055 AddCodeLine ("\ttxa");
3062 if (val >= 1 && val <= 3) {
3063 if (flags & CF_UNSIGNED) {
3064 AddCodeLine ("\tjsr\tshreax%ld", val);
3066 AddCodeLine ("\tjsr\tasreax%ld", val);
3069 } else if (val == 8 && (flags & CF_UNSIGNED)) {
3070 AddCodeLine ("\ttxa");
3071 AddCodeLine ("\tldx\tsreg");
3072 AddCodeLine ("\tldy\tsreg+1");
3073 AddCodeLine ("\tsty\tsreg");
3074 AddCodeLine ("\tldy\t#$00");
3075 AddCodeLine ("\tsty\tsreg+1");
3077 } else if (val == 16) {
3078 AddCodeLine ("\tldy\t#$00");
3079 AddCodeLine ("\tldx\tsreg+1");
3080 if ((flags & CF_UNSIGNED) == 0) {
3081 AddCodeLine ("\tbpl\t*+3");
3082 AddCodeLine ("\tdey");
3083 AddCodeHint ("y:!");
3085 AddCodeLine ("\tlda\tsreg");
3086 AddCodeLine ("\tsty\tsreg+1");
3087 AddCodeLine ("\tsty\tsreg");
3096 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3097 * into the normal, non-optimized stuff.
3099 g_push (flags & ~CF_CONST, 0);
3103 /* Use long way over the stack */
3104 oper (flags, val, ops);
3109 void g_asl (unsigned flags, unsigned long val)
3110 /* Primary = TOS << Primary */
3112 static char* ops [12] = {
3113 0, "tosasla0", "tosaslax",
3114 0, "tosshla0", "tosshlax",
3120 /* If the right hand side is const, the lhs is not on stack but still
3121 * in the primary register.
3123 if (flags & CF_CONST) {
3125 switch (flags & CF_TYPE) {
3129 if (val >= 1 && val <= 3) {
3130 if (flags & CF_UNSIGNED) {
3131 AddCodeLine ("\tjsr\tshlax%ld", val);
3133 AddCodeLine ("\tjsr\taslax%ld", val);
3136 } else if (val == 8) {
3137 AddCodeLine ("\ttax");
3138 AddCodeLine ("\tlda\t#$00");
3144 if (val >= 1 && val <= 3) {
3145 if (flags & CF_UNSIGNED) {
3146 AddCodeLine ("\tjsr\tshleax%ld", val);
3148 AddCodeLine ("\tjsr\tasleax%ld", val);
3151 } else if (val == 8) {
3152 AddCodeLine ("\tldy\tsreg");
3153 AddCodeLine ("\tsty\tsreg+1");
3154 AddCodeLine ("\tstx\tsreg");
3155 AddCodeLine ("\ttax");
3156 AddCodeLine ("\tlda\t#$00");
3158 } else if (val == 16) {
3159 AddCodeLine ("\tstx\tsreg+1");
3160 AddCodeLine ("\tsta\tsreg");
3161 AddCodeLine ("\tlda\t#$00");
3162 AddCodeLine ("\ttax");
3171 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3172 * into the normal, non-optimized stuff.
3174 g_push (flags & ~CF_CONST, 0);
3178 /* Use long way over the stack */
3179 oper (flags, val, ops);
3184 void g_neg (unsigned flags)
3185 /* Primary = -Primary */
3187 switch (flags & CF_TYPE) {
3191 AddCodeLine ("\tjsr\tnegax");
3195 AddCodeLine ("\tjsr\tnegeax");
3205 void g_bneg (unsigned flags)
3206 /* Primary = !Primary */
3208 switch (flags & CF_TYPE) {
3211 AddCodeLine ("\tjsr\tbnega");
3215 AddCodeLine ("\tjsr\tbnegax");
3219 AddCodeLine ("\tjsr\tbnegeax");
3229 void g_com (unsigned flags)
3230 /* Primary = ~Primary */
3232 switch (flags & CF_TYPE) {
3236 AddCodeLine ("\tjsr\tcomplax");
3240 AddCodeLine ("\tjsr\tcompleax");
3250 void g_inc (unsigned flags, unsigned long val)
3251 /* Increment the primary register by a given number */
3253 /* Don't inc by zero */
3258 /* Generate code for the supported types */
3260 switch (flags & CF_TYPE) {
3263 if (flags & CF_FORCECHAR) {
3264 if (CPU == CPU_65C02 && val <= 2) {
3266 AddCodeLine ("\tina");
3269 AddCodeLine ("\tclc");
3270 AddCodeLine ("\tadc\t#$%02X", (unsigned char)val);
3277 if (CPU == CPU_65C02 && val == 1) {
3278 AddCodeLine ("\tina");
3279 AddCodeLine ("\tbne\t*+3");
3280 AddCodeLine ("\tinx");
3281 /* Tell the optimizer that the X register may be invalid */
3282 AddCodeHint ("x:!");
3283 } else if (CodeSizeFactor < 200) {
3286 AddCodeLine ("\tjsr\tincax%lu", val);
3287 } else if (val <= 255) {
3289 AddCodeLine ("\tjsr\tincaxy");
3291 g_add (flags | CF_CONST, val);
3294 /* Inline the code */
3296 if ((val & 0xFF) != 0) {
3297 AddCodeLine ("\tclc");
3298 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3299 AddCodeLine ("\tbcc\t*+3");
3300 AddCodeLine ("\tinx");
3301 /* Tell the optimizer that the X register may be invalid */
3302 AddCodeHint ("x:!");
3305 AddCodeLine ("\tinx");
3308 AddCodeLine ("\tinx");
3311 AddCodeLine ("\tclc");
3312 if ((val & 0xFF) != 0) {
3313 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3314 /* Tell the optimizer that the X register may be invalid */
3315 AddCodeHint ("x:!");
3317 AddCodeLine ("\tpha");
3318 AddCodeLine ("\ttxa");
3319 AddCodeLine ("\tadc\t#$%02X", (unsigned char) (val >> 8));
3320 AddCodeLine ("\ttax");
3321 AddCodeLine ("\tpla");
3329 AddCodeLine ("\tjsr\tinceaxy");
3331 g_add (flags | CF_CONST, val);
3343 void g_dec (unsigned flags, unsigned long val)
3344 /* Decrement the primary register by a given number */
3346 /* Don't dec by zero */
3351 /* Generate code for the supported types */
3353 switch (flags & CF_TYPE) {
3356 if (flags & CF_FORCECHAR) {
3357 if (CPU == CPU_65C02 && val <= 2) {
3359 AddCodeLine ("\tdea");
3362 AddCodeLine ("\tsec");
3363 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
3370 if (CodeSizeFactor < 200) {
3371 /* Use subroutines */
3373 AddCodeLine ("\tjsr\tdecax%d", (int) val);
3374 } else if (val <= 255) {
3376 AddCodeLine ("\tjsr\tdecaxy");
3378 g_sub (flags | CF_CONST, val);
3381 /* Inline the code */
3383 if ((val & 0xFF) != 0) {
3384 AddCodeLine ("\tsec");
3385 AddCodeLine ("\tsbc\t#$%02X", (unsigned char) val);
3386 AddCodeLine ("\tbcs\t*+3");
3387 AddCodeLine ("\tdex");
3388 /* Tell the optimizer that the X register may be invalid */
3389 AddCodeHint ("x:!");
3392 AddCodeLine ("\tdex");
3395 AddCodeLine ("\tdex");
3398 AddCodeLine ("\tsec");
3399 if ((val & 0xFF) != 0) {
3400 AddCodeLine ("\tsbc\t#$%02X", (unsigned char) val);
3401 /* Tell the optimizer that the X register may be invalid */
3402 AddCodeHint ("x:!");
3404 AddCodeLine ("\tpha");
3405 AddCodeLine ("\ttxa");
3406 AddCodeLine ("\tsbc\t#$%02X", (unsigned char) (val >> 8));
3407 AddCodeLine ("\ttax");
3408 AddCodeLine ("\tpla");
3416 AddCodeLine ("\tjsr\tdeceaxy");
3418 g_sub (flags | CF_CONST, val);
3431 * Following are the conditional operators. They compare the TOS against
3432 * the primary and put a literal 1 in the primary if the condition is
3433 * true, otherwise they clear the primary register
3438 void g_eq (unsigned flags, unsigned long val)
3439 /* Test for equal */
3441 static char* ops [12] = {
3442 "toseq00", "toseqa0", "toseqax",
3443 "toseq00", "toseqa0", "toseqax",
3448 /* If the right hand side is const, the lhs is not on stack but still
3449 * in the primary register.
3451 if (flags & CF_CONST) {
3453 switch (flags & CF_TYPE) {
3456 if (flags & CF_FORCECHAR) {
3457 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3458 AddCodeLine ("\tjsr\tbooleq");
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\tbooleq");
3477 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3478 * into the normal, non-optimized stuff.
3480 g_push (flags & ~CF_CONST, 0);
3484 /* Use long way over the stack */
3485 oper (flags, val, ops);
3490 void g_ne (unsigned flags, unsigned long val)
3491 /* Test for not equal */
3493 static char* ops [12] = {
3494 "tosne00", "tosnea0", "tosneax",
3495 "tosne00", "tosnea0", "tosneax",
3501 /* If the right hand side is const, the lhs is not on stack but still
3502 * in the primary register.
3504 if (flags & CF_CONST) {
3506 switch (flags & CF_TYPE) {
3509 if (flags & CF_FORCECHAR) {
3510 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3511 AddCodeLine ("\tjsr\tboolne");
3517 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3518 AddCodeLine ("\tbne\t*+4");
3519 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3520 AddCodeLine ("\tjsr\tboolne");
3530 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3531 * into the normal, non-optimized stuff.
3533 g_push (flags & ~CF_CONST, 0);
3537 /* Use long way over the stack */
3538 oper (flags, val, ops);
3543 void g_lt (unsigned flags, unsigned long val)
3544 /* Test for less than */
3546 static char* ops [12] = {
3547 "toslt00", "toslta0", "tosltax",
3548 "tosult00", "tosulta0", "tosultax",
3553 /* If the right hand side is const, the lhs is not on stack but still
3554 * in the primary register.
3556 if (flags & CF_CONST) {
3558 /* Give a warning in some special cases */
3559 if ((flags & CF_UNSIGNED) && val == 0) {
3560 Warning ("Condition is never true");
3563 /* Look at the type */
3564 switch (flags & CF_TYPE) {
3567 if (flags & CF_FORCECHAR) {
3568 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3569 if (flags & CF_UNSIGNED) {
3570 AddCodeLine ("\tjsr\tboolult");
3572 AddCodeLine ("\tjsr\tboollt");
3579 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3580 /* If we have a signed compare against zero, we only need to
3581 * test the high byte.
3583 AddCodeLine ("\ttxa");
3584 AddCodeLine ("\tjsr\tboollt");
3587 /* Direct code only for unsigned data types */
3588 if (flags & CF_UNSIGNED) {
3589 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3590 AddCodeLine ("\tbne\t*+4");
3591 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3592 AddCodeLine ("\tjsr\tboolult");
3604 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3605 * into the normal, non-optimized stuff.
3607 g_push (flags & ~CF_CONST, 0);
3611 /* Use long way over the stack */
3612 oper (flags, val, ops);
3617 void g_le (unsigned flags, unsigned long val)
3618 /* Test for less than or equal to */
3620 static char* ops [12] = {
3621 "tosle00", "toslea0", "tosleax",
3622 "tosule00", "tosulea0", "tosuleax",
3628 /* If the right hand side is const, the lhs is not on stack but still
3629 * in the primary register.
3631 if (flags & CF_CONST) {
3633 /* Look at the type */
3634 switch (flags & CF_TYPE) {
3637 if (flags & CF_FORCECHAR) {
3638 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3639 if (flags & CF_UNSIGNED) {
3640 AddCodeLine ("\tjsr\tboolule");
3642 AddCodeLine ("\tjsr\tboolle");
3649 if (flags & CF_UNSIGNED) {
3650 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3651 AddCodeLine ("\tbne\t*+4");
3652 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3653 AddCodeLine ("\tjsr\tboolule");
3665 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3666 * into the normal, non-optimized stuff.
3668 g_push (flags & ~CF_CONST, 0);
3672 /* Use long way over the stack */
3673 oper (flags, val, ops);
3678 void g_gt (unsigned flags, unsigned long val)
3679 /* Test for greater than */
3681 static char* ops [12] = {
3682 "tosgt00", "tosgta0", "tosgtax",
3683 "tosugt00", "tosugta0", "tosugtax",
3689 /* If the right hand side is const, the lhs is not on stack but still
3690 * in the primary register.
3692 if (flags & CF_CONST) {
3694 /* Look at the type */
3695 switch (flags & CF_TYPE) {
3698 if (flags & CF_FORCECHAR) {
3699 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3700 if (flags & CF_UNSIGNED) {
3701 /* If we have a compare > 0, we will replace it by
3702 * != 0 here, since both are identical but the latter
3703 * is easier to optimize.
3706 AddCodeLine ("\tjsr\tboolugt");
3708 AddCodeLine ("\tjsr\tboolne");
3711 AddCodeLine ("\tjsr\tboolgt");
3718 if (flags & CF_UNSIGNED) {
3719 /* If we have a compare > 0, we will replace it by
3720 * != 0 here, since both are identical but the latter
3721 * is easier to optimize.
3723 if ((val & 0xFFFF) == 0) {
3724 AddCodeLine ("\tstx\ttmp1");
3725 AddCodeLine ("\tora\ttmp1");
3726 AddCodeLine ("\tjsr\tboolne");
3728 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3729 AddCodeLine ("\tbne\t*+4");
3730 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3731 AddCodeLine ("\tjsr\tboolugt");
3744 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3745 * into the normal, non-optimized stuff.
3747 g_push (flags & ~CF_CONST, 0);
3751 /* Use long way over the stack */
3752 oper (flags, val, ops);
3757 void g_ge (unsigned flags, unsigned long val)
3758 /* Test for greater than or equal to */
3760 static char* ops [12] = {
3761 "tosge00", "tosgea0", "tosgeax",
3762 "tosuge00", "tosugea0", "tosugeax",
3768 /* If the right hand side is const, the lhs is not on stack but still
3769 * in the primary register.
3771 if (flags & CF_CONST) {
3773 /* Give a warning in some special cases */
3774 if ((flags & CF_UNSIGNED) && val == 0) {
3775 Warning ("Condition is always true");
3778 /* Look at the type */
3779 switch (flags & CF_TYPE) {
3782 if (flags & CF_FORCECHAR) {
3783 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3784 if (flags & CF_UNSIGNED) {
3785 AddCodeLine ("\tjsr\tbooluge");
3787 AddCodeLine ("\tjsr\tboolge");
3794 if (flags & CF_UNSIGNED) {
3795 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3796 AddCodeLine ("\tbne\t*+4");
3797 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3798 AddCodeLine ("\tjsr\tbooluge");
3810 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3811 * into the normal, non-optimized stuff.
3813 g_push (flags & ~CF_CONST, 0);
3817 /* Use long way over the stack */
3818 oper (flags, val, ops);
3823 /*****************************************************************************/
3824 /* Allocating static storage */
3825 /*****************************************************************************/
3829 void g_res (unsigned n)
3830 /* Reserve static storage, n bytes */
3832 AddCodeLine ("\t.res\t%u,$00", n);
3837 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3838 /* Define data with the size given in flags */
3840 if (flags & CF_CONST) {
3842 /* Numeric constant */
3843 switch (flags & CF_TYPE) {
3846 AddCodeLine ("\t.byte\t$%02lX", val & 0xFF);
3850 AddCodeLine ("\t.word\t$%04lX", val & 0xFFFF);
3854 AddCodeLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3865 /* Create the correct label name */
3866 const char* Label = GetLabelName (flags, val, offs);
3868 /* Labels are always 16 bit */
3869 AddCodeLine ("\t.word\t%s", Label);
3876 void g_defbytes (const void* Bytes, unsigned Count)
3877 /* Output a row of bytes as a constant */
3883 /* Cast the buffer pointer */
3884 const unsigned char* Data = (const unsigned char*) Bytes;
3886 /* Output the stuff */
3889 /* How many go into this line? */
3890 if ((Chunk = Count) > 16) {
3895 /* Output one line */
3896 strcpy (Buf, "\t.byte\t");
3899 B += sprintf (B, "$%02X", *Data++);
3905 /* Output the line */
3912 void g_zerobytes (unsigned n)
3913 /* Output n bytes of data initialized with zero */
3915 AddCodeLine ("\t.res\t%u,$00", n);
3920 /*****************************************************************************/
3921 /* Inlined known functions */
3922 /*****************************************************************************/
3926 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3927 /* Inline the strlen() function */
3929 /* We need a label in both cases */
3930 unsigned label = GetLabel ();
3932 /* Two different encodings */
3933 if (flags & CF_CONST) {
3935 /* The address of the string is constant. Create the correct label name */
3936 char* lbuf = GetLabelName (flags, val, offs);
3938 /* Generate the strlen code */
3939 AddCodeLine ("\tldy\t#$FF");
3940 g_defloclabel (label);
3941 AddCodeLine ("\tiny");
3942 AddCodeLine ("\tlda\t%s,y", lbuf);
3943 AddCodeLine ("\tbne\tL%04X", label);
3944 AddCodeLine ("\ttax");
3945 AddCodeLine ("\ttya");
3949 /* Address not constant but in primary */
3950 if (CodeSizeFactor < 400) {
3951 /* This is too much code, so call strlen instead of inlining */
3952 AddCodeLine ("\tjsr\t_strlen");
3954 /* Inline the function */
3955 AddCodeLine ("\tsta\tptr1");
3956 AddCodeLine ("\tstx\tptr1+1");
3957 AddCodeLine ("\tldy\t#$FF");
3958 g_defloclabel (label);
3959 AddCodeLine ("\tiny");
3960 AddCodeLine ("\tlda\t(ptr1),y");
3961 AddCodeLine ("\tbne\tL%04X", label);
3962 AddCodeLine ("\ttax");
3963 AddCodeLine ("\ttya");