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 && FavourSize == 0) {
681 AddCodeLine ("\tlda\t#$00");
682 AddCodeLine ("\tsta\tsreg+1");
683 AddCodeLine ("\tsta\tsreg");
684 AddCodeLine ("\tlda\t#$%02X", (unsigned char) val);
685 AddCodeLine ("\tldx\t#$%02X", (unsigned char) (val >> 8));
686 } else if ((val & 0xFFFFFF00) == 0xFFFFFF00) {
687 AddCodeLine ("\tldx\t#$FF");
688 AddCodeLine ("\tstx\tsreg+1");
689 AddCodeLine ("\tstx\tsreg");
690 if ((val & 0xFF) == 0xFF) {
691 AddCodeLine ("\ttxa");
693 AddCodeLine ("\tlda\t#$%02X", (unsigned char) val);
695 } else if ((val & 0xFFFF00FF) == 0xFFFF00FF) {
696 AddCodeLine ("\tlda\t#$FF");
697 AddCodeLine ("\tsta\tsreg+1");
698 AddCodeLine ("\tsta\tsreg");
699 AddCodeLine ("\tldx\t#$%02X", (unsigned char) (val >> 8));
701 /* Call a subroutine that will load following value */
702 AddCodeLine ("\tjsr\tldeax");
703 AddCodeLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
715 /* Some sort of label */
716 const char* Label = GetLabelName (flags, val, offs);
718 /* Load the address into the primary */
719 AddCodeLine ("\tldax\t%s", Label);
726 void g_getstatic (unsigned flags, unsigned long label, unsigned offs)
727 /* Fetch an static memory cell into the primary register */
729 /* Create the correct label name */
730 char* lbuf = GetLabelName (flags, label, offs);
732 /* Check the size and generate the correct load operation */
733 switch (flags & CF_TYPE) {
736 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
737 AddCodeLine ("\tlda\t%s", lbuf); /* load A from the label */
740 AddCodeLine ("\tlda\t%s", lbuf); /* load A from the label */
741 if (!(flags & CF_UNSIGNED)) {
742 /* Must sign extend */
743 AddCodeLine ("\tbpl\t*+3");
744 AddCodeLine ("\tdex");
745 AddCodeHint ("x:!"); /* X is invalid now */
751 AddCodeLine ("\tlda\t%s", lbuf);
752 if (flags & CF_TEST) {
753 AddCodeLine ("\tora\t%s+1", lbuf);
755 AddCodeLine ("\tldx\t%s+1", lbuf);
760 if (flags & CF_TEST) {
761 AddCodeLine ("\tlda\t%s+3", lbuf);
762 AddCodeLine ("\tora\t%s+2", lbuf);
763 AddCodeLine ("\tora\t%s+1", lbuf);
764 AddCodeLine ("\tora\t%s+0", lbuf);
766 AddCodeLine ("\tlda\t%s+3", lbuf);
767 AddCodeLine ("\tsta\tsreg+1");
768 AddCodeLine ("\tlda\t%s+2", lbuf);
769 AddCodeLine ("\tsta\tsreg");
770 AddCodeLine ("\tldx\t%s+1", lbuf);
771 AddCodeLine ("\tlda\t%s", lbuf);
783 void g_getlocal (unsigned flags, int offs)
784 /* Fetch specified local object (local var). */
787 CheckLocalOffs (offs);
788 switch (flags & CF_TYPE) {
791 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
792 if (CPU == CPU_65C02 && offs == 0) {
793 AddCodeLine ("\tlda\t(sp)");
796 AddCodeLine ("\tlda\t(sp),y");
800 AddCodeLine ("\tldx\t#$00");
801 AddCodeLine ("\tlda\t(sp,x)");
804 AddCodeLine ("\tldx\t#$00");
805 AddCodeLine ("\tlda\t(sp),y");
807 if ((flags & CF_UNSIGNED) == 0) {
808 AddCodeLine ("\tbpl\t*+3");
809 AddCodeLine ("\tdex");
810 AddCodeHint ("x:!"); /* X is invalid now */
816 CheckLocalOffs (offs + 1);
817 if (flags & CF_TEST) {
819 AddCodeLine ("\tlda\t(sp),y");
820 AddCodeLine ("\tdey");
821 AddCodeLine ("\tora\t(sp),y");
826 AddCodeLine ("\tjsr\tldaxysp");
828 AddCodeLine ("\tjsr\tldax0sp");
832 AddCodeLine ("\tlda\t(sp),y");
833 AddCodeLine ("\ttax");
834 AddCodeLine ("\tdey");
835 AddCodeLine ("\tlda\t(sp),y");
843 AddCodeLine ("\tjsr\tldeaxysp");
845 AddCodeLine ("\tjsr\tldeax0sp");
856 void g_getind (unsigned flags, unsigned offs)
857 /* Fetch the specified object type indirect through the primary register
858 * into the primary register
861 /* If the offset is greater than 255, add the part that is > 255 to
862 * the primary. This way we get an easy addition and use the low byte
865 offs = MakeByteOffs (flags, offs);
867 /* Handle the indirect fetch */
868 switch (flags & CF_TYPE) {
871 /* Character sized */
874 if (flags & CF_UNSIGNED) {
875 AddCodeLine ("\tjsr\tldauidx");
877 AddCodeLine ("\tjsr\tldaidx");
880 if (flags & CF_UNSIGNED) {
882 AddCodeLine ("\tjsr\tldaui");
884 AddCodeLine ("\tsta\tptr1");
885 AddCodeLine ("\tstx\tptr1+1");
886 AddCodeLine ("\tldx\t#$00");
887 AddCodeLine ("\tlda\t(ptr1,x)");
890 AddCodeLine ("\tjsr\tldai");
896 if (flags & CF_TEST) {
898 AddCodeLine ("\tsta\tptr1");
899 AddCodeLine ("\tstx\tptr1+1");
900 AddCodeLine ("\tlda\t(ptr1),y");
901 AddCodeLine ("\tiny");
902 AddCodeLine ("\tora\t(ptr1),y");
905 AddCodeLine ("\tjsr\tldaxi");
908 AddCodeLine ("\tjsr\tldaxidx");
915 AddCodeLine ("\tjsr\tldeaxi");
918 AddCodeLine ("\tjsr\tldeaxidx");
920 if (flags & CF_TEST) {
921 AddCodeLine ("\tjsr\ttsteax");
933 void g_leasp (int offs)
934 /* Fetch the address of the specified symbol into the primary register */
936 /* Calculate the offset relative to sp */
939 /* For value 0 we do direct code */
941 AddCodeLine ("\tlda\tsp");
942 AddCodeLine ("\tldx\tsp+1");
945 ldaconst (offs); /* Load A with offset value */
946 AddCodeLine ("\tjsr\tleaasp"); /* Load effective address */
948 if (CPU == CPU_65C02 && offs == 1) {
949 AddCodeLine ("\tlda\tsp");
950 AddCodeLine ("\tldx\tsp+1");
951 AddCodeLine ("\tina");
952 AddCodeLine ("\tbne\t*+3");
953 AddCodeLine ("\tinx");
954 AddCodeHint ("x:!"); /* Invalidate X */
957 AddCodeLine ("\tclc");
958 AddCodeLine ("\tldx\tsp+1");
959 AddCodeLine ("\tadc\tsp");
960 AddCodeLine ("\tbcc\t*+3");
961 AddCodeLine ("\tinx");
962 AddCodeHint ("x:!"); /* Invalidate X */
970 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)
1088 /* Put data into local object. */
1091 CheckLocalOffs (offs);
1092 switch (flags & CF_TYPE) {
1095 if (CPU == CPU_65C02 && offs == 0) {
1096 AddCodeLine ("\tsta\t(sp)");
1099 AddCodeLine ("\tsta\t(sp),y");
1106 AddCodeLine ("\tjsr\tstaxysp");
1108 AddCodeLine ("\tjsr\tstax0sp");
1115 AddCodeLine ("\tjsr\tsteaxysp");
1117 AddCodeLine ("\tjsr\tsteax0sp");
1129 void g_putind (unsigned Flags, unsigned Offs)
1130 /* Store the specified object type in the primary register at the address
1131 * on the top of the stack
1134 /* We can handle offsets below $100 directly, larger offsets must be added
1135 * to the address. Since a/x is in use, best code is achieved by adding
1136 * just the high byte. Be sure to check if the low byte will overflow while
1139 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1141 /* Overflow - we need to add the low byte also */
1142 AddCodeLine ("\tldy\t#$00");
1143 AddCodeLine ("\tclc");
1144 AddCodeLine ("\tpha");
1145 AddCodeLine ("\tlda\t#$%02X", Offs & 0xFF);
1146 AddCodeLine ("\tadc\t(sp),y");
1147 AddCodeLine ("\tsta\t(sp),y");
1148 AddCodeLine ("\tiny");
1149 AddCodeLine ("\tlda\t#$%02X", (Offs >> 8) & 0xFF);
1150 AddCodeLine ("\tadc\t(sp),y");
1151 AddCodeLine ("\tsta\t(sp),y");
1152 AddCodeLine ("\tpla");
1154 /* Complete address is on stack, new offset is zero */
1157 } else if ((Offs & 0xFF00) != 0) {
1159 /* We can just add the high byte */
1160 AddCodeLine ("\tldy\t#$01");
1161 AddCodeLine ("\tclc");
1162 AddCodeLine ("\tpha");
1163 AddCodeLine ("\tlda\t#$%02X", (Offs >> 8) & 0xFF);
1164 AddCodeLine ("\tadc\t(sp),y");
1165 AddCodeLine ("\tsta\t(sp),y");
1166 AddCodeLine ("\tpla");
1168 /* Offset is now just the low byte */
1172 /* Check the size and determine operation */
1173 switch (Flags & CF_TYPE) {
1178 AddCodeLine ("\tjsr\tstaspidx");
1180 AddCodeLine ("\tjsr\tstaspp");
1187 AddCodeLine ("\tjsr\tstaxspidx");
1189 AddCodeLine ("\tjsr\tstaxspp");
1196 AddCodeLine ("\tjsr\tsteaxspidx");
1198 AddCodeLine ("\tjsr\tsteaxspp");
1207 /* Pop the argument which is always a pointer */
1213 /*****************************************************************************/
1214 /* type conversion and similiar stuff */
1215 /*****************************************************************************/
1219 void g_toslong (unsigned flags)
1220 /* Make sure, the value on TOS is a long. Convert if necessary */
1222 switch (flags & CF_TYPE) {
1226 if (flags & CF_UNSIGNED) {
1227 AddCodeLine ("\tjsr\ttosulong");
1229 AddCodeLine ("\tjsr\ttoslong");
1244 void g_tosint (unsigned flags)
1245 /* Make sure, the value on TOS is an int. Convert if necessary */
1247 switch (flags & CF_TYPE) {
1254 AddCodeLine ("\tjsr\ttosint");
1265 void g_reglong (unsigned flags)
1266 /* Make sure, the value in the primary register a long. Convert if necessary */
1268 switch (flags & CF_TYPE) {
1272 if (flags & CF_UNSIGNED) {
1274 AddCodeLine ("\tjsr\taxulong");
1277 AddCodeLine ("\tsty\tsreg");
1278 AddCodeLine ("\tsty\tsreg+1");
1281 AddCodeLine ("\tjsr\taxlong");
1295 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1296 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1297 * value, that corresponds to the value on TOS, rhs corresponds to the value
1298 * in (e)ax. The return value is the the flags value for the resulting type.
1301 unsigned ltype, rtype;
1304 /* Get the type spec from the flags */
1305 ltype = lhs & CF_TYPE;
1306 rtype = rhs & CF_TYPE;
1308 /* Check if a conversion is needed */
1309 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1310 /* We must promote the primary register to long */
1312 /* Get the new rhs type */
1313 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1315 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1316 /* We must promote the lhs to long */
1322 /* Get the new rhs type */
1323 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1327 /* Determine the result type for the operation:
1328 * - The result is const if both operands are const.
1329 * - The result is unsigned if one of the operands is unsigned.
1330 * - The result is long if one of the operands is long.
1331 * - Otherwise the result is int sized.
1333 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1334 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1335 if (rtype == CF_LONG || ltype == CF_LONG) {
1345 unsigned g_typecast (unsigned lhs, unsigned rhs)
1346 /* Cast the value in the primary register to the operand size that is flagged
1347 * by the lhs value. Return the result value.
1350 unsigned ltype, rtype;
1352 /* Get the type spec from the flags */
1353 ltype = lhs & CF_TYPE;
1354 rtype = rhs & CF_TYPE;
1356 /* Check if a conversion is needed */
1357 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1358 /* We must promote the primary register to long */
1362 /* Do not need any other action. If the left type is int, and the primary
1363 * register is long, it will be automagically truncated. If the right hand
1364 * side is const, it is not located in the primary register and handled by
1365 * the expression parser code.
1368 /* Result is const if the right hand side was const */
1369 lhs |= (rhs & CF_CONST);
1371 /* The resulting type is that of the left hand side (that's why you called
1379 void g_scale (unsigned flags, long val)
1380 /* Scale the value in the primary register by the given value. If val is positive,
1381 * scale up, is val is negative, scale down. This function is used to scale
1382 * the operands or results of pointer arithmetic by the size of the type, the
1383 * pointer points to.
1388 /* Value may not be zero */
1390 Internal ("Data type has no size");
1391 } else if (val > 0) {
1394 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1396 /* Factor is 2, 4 or 8, use special function */
1397 switch (flags & CF_TYPE) {
1400 if (flags & CF_FORCECHAR) {
1402 AddCodeLine ("\tasl\ta");
1409 if (FavourSize || p2 >= 3) {
1410 if (flags & CF_UNSIGNED) {
1411 AddCodeLine ("\tjsr\tshlax%d", p2);
1413 AddCodeLine ("\tjsr\taslax%d", p2);
1416 AddCodeLine ("\tstx\ttmp1");
1418 AddCodeLine ("\tasl\ta");
1419 AddCodeLine ("\trol\ttmp1");
1421 AddCodeLine ("\tldx\ttmp1");
1426 if (flags & CF_UNSIGNED) {
1427 AddCodeLine ("\tjsr\tshleax%d", p2);
1429 AddCodeLine ("\tjsr\tasleax%d", p2);
1438 } else if (val != 1) {
1440 /* Use a multiplication instead */
1441 g_mul (flags | CF_CONST, val);
1449 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1451 /* Factor is 2, 4 or 8, use special function */
1452 switch (flags & CF_TYPE) {
1455 if (flags & CF_FORCECHAR) {
1456 if (flags & CF_UNSIGNED) {
1458 AddCodeLine ("\tlsr\ta");
1461 } else if (p2 <= 2) {
1462 AddCodeLine ("\tcmp\t#$80");
1463 AddCodeLine ("\tror\ta");
1470 if (flags & CF_UNSIGNED) {
1471 if (FavourSize || p2 >= 3) {
1472 AddCodeLine ("\tjsr\tlsrax%d", p2);
1474 AddCodeLine ("\tstx\ttmp1");
1476 AddCodeLine ("\tlsr\ttmp1");
1477 AddCodeLine ("\tror\ta");
1479 AddCodeLine ("\tldx\ttmp1");
1482 if (FavourSize || p2 >= 3) {
1483 AddCodeLine ("\tjsr\tasrax%d", p2);
1485 AddCodeLine ("\tstx\ttmp1");
1487 AddCodeLine ("\tcpx\t#$80");
1488 AddCodeLine ("\tror\ttmp1");
1489 AddCodeLine ("\tror\ta");
1491 AddCodeLine ("\tldx\ttmp1");
1497 if (flags & CF_UNSIGNED) {
1498 AddCodeLine ("\tjsr\tlsreax%d", p2);
1500 AddCodeLine ("\tjsr\tasreax%d", p2);
1509 } else if (val != 1) {
1511 /* Use a division instead */
1512 g_div (flags | CF_CONST, val);
1520 /*****************************************************************************/
1521 /* Adds and subs of variables fix a fixed address */
1522 /*****************************************************************************/
1526 void g_addlocal (unsigned flags, int offs)
1527 /* Add a local variable to ax */
1529 /* Correct the offset and check it */
1531 CheckLocalOffs (offs);
1533 switch (flags & CF_TYPE) {
1536 AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1537 AddCodeLine ("\tclc");
1538 AddCodeLine ("\tadc\t(sp),y");
1539 AddCodeLine ("\tbcc\t*+3");
1540 AddCodeLine ("\tinx");
1541 AddCodeHint ("x:!");
1545 AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1546 AddCodeLine ("\tclc");
1547 AddCodeLine ("\tadc\t(sp),y");
1548 AddCodeLine ("\tpha");
1549 AddCodeLine ("\ttxa");
1550 AddCodeLine ("\tiny");
1551 AddCodeLine ("\tadc\t(sp),y");
1552 AddCodeLine ("\ttax");
1553 AddCodeLine ("\tpla");
1557 /* Do it the old way */
1559 g_getlocal (flags, offs);
1571 void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
1572 /* Add a static variable to ax */
1574 /* Create the correct label name */
1575 char* lbuf = GetLabelName (flags, label, offs);
1577 switch (flags & CF_TYPE) {
1580 AddCodeLine ("\tclc");
1581 AddCodeLine ("\tadc\t%s", lbuf);
1582 AddCodeLine ("\tbcc\t*+3");
1583 AddCodeLine ("\tinx");
1584 AddCodeHint ("x:!");
1588 AddCodeLine ("\tclc");
1589 AddCodeLine ("\tadc\t%s", lbuf);
1590 AddCodeLine ("\ttay");
1591 AddCodeLine ("\ttxa");
1592 AddCodeLine ("\tadc\t%s+1", lbuf);
1593 AddCodeLine ("\ttax");
1594 AddCodeLine ("\ttya");
1598 /* Do it the old way */
1600 g_getstatic (flags, label, offs);
1612 /*****************************************************************************/
1613 /* Compares of ax with a variable with fixed address */
1614 /*****************************************************************************/
1618 void g_cmplocal (unsigned flags, int offs)
1619 /* Compare a local variable to ax */
1621 Internal ("g_cmplocal not implemented");
1626 void g_cmpstatic (unsigned flags, unsigned label, unsigned offs)
1627 /* Compare a static variable to ax */
1629 Internal ("g_cmpstatic not implemented");
1634 /*****************************************************************************/
1635 /* Special op= functions */
1636 /*****************************************************************************/
1640 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
1642 /* Emit += for a static variable */
1644 /* Create the correct label name */
1645 char* lbuf = GetLabelName (flags, label, offs);
1647 /* Check the size and determine operation */
1648 switch (flags & CF_TYPE) {
1651 if (flags & CF_FORCECHAR) {
1652 AddCodeLine ("\tldx\t#$00");
1653 if (flags & CF_CONST) {
1655 AddCodeLine ("\tinc\t%s", lbuf);
1656 AddCodeLine ("\tlda\t%s", lbuf);
1658 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1659 AddCodeLine ("\tclc");
1660 AddCodeLine ("\tadc\t%s", lbuf);
1661 AddCodeLine ("\tsta\t%s", lbuf);
1664 AddCodeLine ("\tclc");
1665 AddCodeLine ("\tadc\t%s", lbuf);
1666 AddCodeLine ("\tsta\t%s", lbuf);
1668 if ((flags & CF_UNSIGNED) == 0) {
1669 AddCodeLine ("\tbpl\t*+3");
1670 AddCodeLine ("\tdex");
1671 AddCodeHint ("x:!"); /* Invalidate X */
1678 if (flags & CF_CONST) {
1680 label = GetLabel ();
1681 AddCodeLine ("\tinc\t%s", lbuf);
1682 AddCodeLine ("\tbne\tL%04X", (int)label);
1683 AddCodeLine ("\tinc\t%s+1", lbuf);
1684 g_defloclabel (label);
1685 AddCodeLine ("\tlda\t%s", lbuf); /* Hmmm... */
1686 AddCodeLine ("\tldx\t%s+1", lbuf);
1688 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1689 AddCodeLine ("\tclc");
1690 AddCodeLine ("\tadc\t%s", lbuf);
1691 AddCodeLine ("\tsta\t%s", lbuf);
1693 label = GetLabel ();
1694 AddCodeLine ("\tbcc\tL%04X", (int)label);
1695 AddCodeLine ("\tinc\t%s+1", lbuf);
1696 g_defloclabel (label);
1697 AddCodeLine ("\tldx\t%s+1", lbuf);
1699 AddCodeLine ("\tlda\t#$%02X", (unsigned char)(val >> 8));
1700 AddCodeLine ("\tadc\t%s+1", lbuf);
1701 AddCodeLine ("\tsta\t%s+1", lbuf);
1702 AddCodeLine ("\ttax");
1703 AddCodeLine ("\tlda\t%s", lbuf);
1707 AddCodeLine ("\tclc");
1708 AddCodeLine ("\tadc\t%s", lbuf);
1709 AddCodeLine ("\tsta\t%s", lbuf);
1710 AddCodeLine ("\ttxa");
1711 AddCodeLine ("\tadc\t%s+1", lbuf);
1712 AddCodeLine ("\tsta\t%s+1", lbuf);
1713 AddCodeLine ("\ttax");
1714 AddCodeLine ("\tlda\t%s", lbuf);
1719 if (flags & CF_CONST) {
1721 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1722 AddCodeLine ("\tsty\tptr1");
1723 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1725 AddCodeLine ("\tjsr\tladdeq1");
1727 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1728 AddCodeLine ("\tjsr\tladdeqa");
1731 g_getstatic (flags, label, offs);
1733 g_putstatic (flags, label, offs);
1736 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1737 AddCodeLine ("\tsty\tptr1");
1738 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1739 AddCodeLine ("\tjsr\tladdeq");
1750 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1751 /* Emit += for a local variable */
1753 /* Calculate the true offset, check it, load it into Y */
1755 CheckLocalOffs (offs);
1757 /* Check the size and determine operation */
1758 switch (flags & CF_TYPE) {
1761 if (flags & CF_FORCECHAR) {
1763 AddCodeLine ("\tldx\t#$00");
1764 if (flags & CF_CONST) {
1765 AddCodeLine ("\tclc");
1766 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1767 AddCodeLine ("\tadc\t(sp,x)");
1768 AddCodeLine ("\tsta\t(sp,x)");
1770 AddCodeLine ("\tclc");
1771 AddCodeLine ("\tadc\t(sp,x)");
1772 AddCodeLine ("\tsta\t(sp,x)");
1776 AddCodeLine ("\tldx\t#$00");
1777 if (flags & CF_CONST) {
1778 AddCodeLine ("\tclc");
1779 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1780 AddCodeLine ("\tadc\t(sp),y");
1781 AddCodeLine ("\tsta\t(sp),y");
1783 AddCodeLine ("\tclc");
1784 AddCodeLine ("\tadc\t(sp),y");
1785 AddCodeLine ("\tsta\t(sp),y");
1788 if ((flags & CF_UNSIGNED) == 0) {
1789 AddCodeLine ("\tbpl\t*+3");
1790 AddCodeLine ("\tdex");
1791 AddCodeHint ("x:!"); /* Invalidate X */
1798 if (flags & CF_CONST) {
1799 g_getimmed (flags, val, 0);
1802 AddCodeLine ("\tjsr\taddeq0sp");
1805 AddCodeLine ("\tjsr\taddeqysp");
1810 if (flags & CF_CONST) {
1811 g_getimmed (flags, val, 0);
1814 AddCodeLine ("\tjsr\tladdeq0sp");
1817 AddCodeLine ("\tjsr\tladdeqysp");
1828 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1829 /* Emit += for the location with address in ax */
1831 /* If the offset is too large for a byte register, add the high byte
1832 * of the offset to the primary. Beware: We need a special correction
1833 * if the offset in the low byte will overflow in the operation.
1835 offs = MakeByteOffs (flags, offs);
1837 /* Check the size and determine operation */
1838 switch (flags & CF_TYPE) {
1841 AddCodeLine ("\tsta\tptr1");
1842 AddCodeLine ("\tstx\tptr1+1");
1844 AddCodeLine ("\tldx\t#$00");
1845 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1846 AddCodeLine ("\tclc");
1847 AddCodeLine ("\tadc\t(ptr1,x)");
1848 AddCodeLine ("\tsta\t(ptr1,x)");
1850 AddCodeLine ("\tldy\t#$%02X", offs);
1851 AddCodeLine ("\tldx\t#$00");
1852 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1853 AddCodeLine ("\tclc");
1854 AddCodeLine ("\tadc\t(ptr1),y");
1855 AddCodeLine ("\tsta\t(ptr1),y");
1861 /* Lots of code, use only if size is not important */
1862 AddCodeLine ("\tsta\tptr1");
1863 AddCodeLine ("\tstx\tptr1+1");
1864 AddCodeLine ("\tldy\t#$%02X", offs);
1865 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1866 AddCodeLine ("\tclc");
1867 AddCodeLine ("\tadc\t(ptr1),y");
1868 AddCodeLine ("\tsta\t(ptr1),y");
1869 AddCodeLine ("\tpha");
1870 AddCodeLine ("\tiny");
1871 AddCodeLine ("\tlda\t#$%02X", (unsigned char)(val >> 8));
1872 AddCodeLine ("\tadc\t(ptr1),y");
1873 AddCodeLine ("\tsta\t(ptr1),y");
1874 AddCodeLine ("\ttax");
1875 AddCodeLine ("\tpla");
1881 AddCodeLine ("\tjsr\tpushax"); /* Push the address */
1882 push (flags); /* Correct the internal sp */
1883 g_getind (flags, offs); /* Fetch the value */
1884 g_inc (flags, val); /* Increment value in primary */
1885 g_putind (flags, offs); /* Store the value back */
1895 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1897 /* Emit -= for a static variable */
1899 /* Create the correct label name */
1900 char* lbuf = GetLabelName (flags, label, offs);
1902 /* Check the size and determine operation */
1903 switch (flags & CF_TYPE) {
1906 if (flags & CF_FORCECHAR) {
1907 AddCodeLine ("\tldx\t#$00");
1908 if (flags & CF_CONST) {
1910 AddCodeLine ("\tdec\t%s", lbuf);
1911 AddCodeLine ("\tlda\t%s", lbuf);
1913 AddCodeLine ("\tsec");
1914 AddCodeLine ("\tlda\t%s", lbuf);
1915 AddCodeLine ("\tsbc\t#$%02X", (int)(val & 0xFF));
1916 AddCodeLine ("\tsta\t%s", lbuf);
1919 AddCodeLine ("\tsec");
1920 AddCodeLine ("\tsta\ttmp1");
1921 AddCodeLine ("\tlda\t%s", lbuf);
1922 AddCodeLine ("\tsbc\ttmp1");
1923 AddCodeLine ("\tsta\t%s", lbuf);
1925 if ((flags & CF_UNSIGNED) == 0) {
1926 AddCodeLine ("\tbpl\t*+3");
1927 AddCodeLine ("\tdex");
1928 AddCodeHint ("x:!"); /* Invalidate X */
1935 AddCodeLine ("\tsec");
1936 if (flags & CF_CONST) {
1937 AddCodeLine ("\tlda\t%s", lbuf);
1938 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
1939 AddCodeLine ("\tsta\t%s", lbuf);
1941 label = GetLabel ();
1942 AddCodeLine ("\tbcs\tL%04X", (unsigned)label);
1943 AddCodeLine ("\tdec\t%s+1", lbuf);
1944 g_defloclabel (label);
1945 AddCodeLine ("\tldx\t%s+1", lbuf);
1947 AddCodeLine ("\tlda\t%s+1", lbuf);
1948 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)(val >> 8));
1949 AddCodeLine ("\tsta\t%s+1", lbuf);
1950 AddCodeLine ("\ttax");
1951 AddCodeLine ("\tlda\t%s", lbuf);
1954 AddCodeLine ("\tsta\ttmp1");
1955 AddCodeLine ("\tlda\t%s", lbuf);
1956 AddCodeLine ("\tsbc\ttmp1");
1957 AddCodeLine ("\tsta\t%s", lbuf);
1958 AddCodeLine ("\tstx\ttmp1");
1959 AddCodeLine ("\tlda\t%s+1", lbuf);
1960 AddCodeLine ("\tsbc\ttmp1");
1961 AddCodeLine ("\tsta\t%s+1", lbuf);
1962 AddCodeLine ("\ttax");
1963 AddCodeLine ("\tlda\t%s", lbuf);
1968 if (flags & CF_CONST) {
1970 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1971 AddCodeLine ("\tsty\tptr1");
1972 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1974 AddCodeLine ("\tjsr\tlsubeq1");
1976 AddCodeLine ("\tlda\t#$%02X", (unsigned char)val);
1977 AddCodeLine ("\tjsr\tlsubeqa");
1980 g_getstatic (flags, label, offs);
1982 g_putstatic (flags, label, offs);
1985 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1986 AddCodeLine ("\tsty\tptr1");
1987 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1988 AddCodeLine ("\tjsr\tlsubeq");
1999 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
2000 /* Emit -= for a local variable */
2002 /* Calculate the true offset, check it, load it into Y */
2004 CheckLocalOffs (offs);
2006 /* Check the size and determine operation */
2007 switch (flags & CF_TYPE) {
2010 if (flags & CF_FORCECHAR) {
2012 AddCodeLine ("\tldx\t#$00");
2013 AddCodeLine ("\tsec");
2014 if (flags & CF_CONST) {
2015 AddCodeLine ("\tlda\t(sp),y");
2016 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2018 AddCodeLine ("\tsta\ttmp1");
2019 AddCodeLine ("\tlda\t(sp),y");
2020 AddCodeLine ("\tsbc\ttmp1");
2022 AddCodeLine ("\tsta\t(sp),y");
2023 if ((flags & CF_UNSIGNED) == 0) {
2024 AddCodeLine ("\tbpl\t*+3");
2025 AddCodeLine ("\tdex");
2026 AddCodeHint ("x:!"); /* Invalidate X */
2033 if (flags & CF_CONST) {
2034 g_getimmed (flags, val, 0);
2037 AddCodeLine ("\tjsr\tsubeq0sp");
2040 AddCodeLine ("\tjsr\tsubeqysp");
2045 if (flags & CF_CONST) {
2046 g_getimmed (flags, val, 0);
2049 AddCodeLine ("\tjsr\tlsubeq0sp");
2052 AddCodeLine ("\tjsr\tlsubeqysp");
2063 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2064 /* Emit -= for the location with address in ax */
2066 /* If the offset is too large for a byte register, add the high byte
2067 * of the offset to the primary. Beware: We need a special correction
2068 * if the offset in the low byte will overflow in the operation.
2070 offs = MakeByteOffs (flags, offs);
2072 /* Check the size and determine operation */
2073 switch (flags & CF_TYPE) {
2076 AddCodeLine ("\tsta\tptr1");
2077 AddCodeLine ("\tstx\tptr1+1");
2079 AddCodeLine ("\tldx\t#$00");
2080 AddCodeLine ("\tlda\t(ptr1,x)");
2081 AddCodeLine ("\tsec");
2082 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2083 AddCodeLine ("\tsta\t(ptr1,x)");
2085 AddCodeLine ("\tldy\t#$%02X", offs);
2086 AddCodeLine ("\tldx\t#$00");
2087 AddCodeLine ("\tlda\t(ptr1),y");
2088 AddCodeLine ("\tsec");
2089 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2090 AddCodeLine ("\tsta\t(ptr1),y");
2096 /* Lots of code, use only if size is not important */
2097 AddCodeLine ("\tsta\tptr1");
2098 AddCodeLine ("\tstx\tptr1+1");
2099 AddCodeLine ("\tldy\t#$%02X", offs);
2100 AddCodeLine ("\tlda\t(ptr1),y");
2101 AddCodeLine ("\tsec");
2102 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2103 AddCodeLine ("\tsta\t(ptr1),y");
2104 AddCodeLine ("\tpha");
2105 AddCodeLine ("\tiny");
2106 AddCodeLine ("\tlda\t(ptr1),y");
2107 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)(val >> 8));
2108 AddCodeLine ("\tsta\t(ptr1),y");
2109 AddCodeLine ("\ttax");
2110 AddCodeLine ("\tpla");
2116 AddCodeLine ("\tjsr\tpushax"); /* Push the address */
2117 push (flags); /* Correct the internal sp */
2118 g_getind (flags, offs); /* Fetch the value */
2119 g_dec (flags, val); /* Increment value in primary */
2120 g_putind (flags, offs); /* Store the value back */
2130 /*****************************************************************************/
2131 /* Add a variable address to the value in ax */
2132 /*****************************************************************************/
2136 void g_addaddr_local (unsigned flags, int offs)
2137 /* Add the address of a local variable to ax */
2139 /* Add the offset */
2142 /* We cannot address more then 256 bytes of locals anyway */
2143 CheckLocalOffs (offs);
2144 AddCodeLine ("\tclc");
2145 AddCodeLine ("\tadc\t#$%02X", offs & 0xFF);
2146 AddCodeLine ("\tbcc\t*+4"); /* Do also skip the CLC insn below */
2147 AddCodeLine ("\tinx");
2148 AddCodeHint ("x:!"); /* Invalidate X */
2151 /* Add the current stackpointer value */
2152 AddCodeLine ("\tclc");
2153 AddCodeLine ("\tadc\tsp");
2154 AddCodeLine ("\ttay");
2155 AddCodeLine ("\ttxa");
2156 AddCodeLine ("\tadc\tsp+1");
2157 AddCodeLine ("\ttax");
2158 AddCodeLine ("\ttya");
2163 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2164 /* Add the address of a static variable to ax */
2166 /* Create the correct label name */
2167 char* lbuf = GetLabelName (flags, label, offs);
2169 /* Add the address to the current ax value */
2170 AddCodeLine ("\tclc");
2171 AddCodeLine ("\tadc\t#<(%s)", lbuf);
2172 AddCodeLine ("\ttay");
2173 AddCodeLine ("\ttxa");
2174 AddCodeLine ("\tadc\t#>(%s)", lbuf);
2175 AddCodeLine ("\ttax");
2176 AddCodeLine ("\ttya");
2181 /*****************************************************************************/
2183 /*****************************************************************************/
2187 void g_save (unsigned flags)
2188 /* Copy primary register to hold register. */
2190 /* Check the size and determine operation */
2191 switch (flags & CF_TYPE) {
2194 if (flags & CF_FORCECHAR) {
2195 AddCodeLine ("\tpha");
2201 AddCodeLine ("\tsta\tregsave");
2202 AddCodeLine ("\tstx\tregsave+1");
2206 AddCodeLine ("\tjsr\tsaveeax");
2216 void g_restore (unsigned flags)
2217 /* Copy hold register to P. */
2219 /* Check the size and determine operation */
2220 switch (flags & CF_TYPE) {
2223 if (flags & CF_FORCECHAR) {
2224 AddCodeLine ("\tpla");
2230 AddCodeLine ("\tlda\tregsave");
2231 AddCodeLine ("\tldx\tregsave+1");
2235 AddCodeLine ("\tjsr\tresteax");
2245 void g_cmp (unsigned flags, unsigned long val)
2246 /* Immidiate compare. The primary register will not be changed, Z flag
2250 /* Check the size and determine operation */
2251 switch (flags & CF_TYPE) {
2254 if (flags & CF_FORCECHAR) {
2255 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
2261 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
2262 AddCodeLine ("\tbne\t*+4");
2263 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
2267 Internal ("g_cmp: Long compares not implemented");
2277 static void oper (unsigned flags, unsigned long val, char** subs)
2278 /* Encode a binary operation. subs is a pointer to four groups of three
2280 * 0-2 --> Operate on ints
2281 * 3-5 --> Operate on unsigneds
2282 * 6-8 --> Operate on longs
2283 * 9-11 --> Operate on unsigned longs
2285 * The first subroutine names in each string group is used to encode an
2286 * operation with a zero constant, the second to encode an operation with
2287 * a 8 bit constant, and the third is used in all other cases.
2292 /* Determine the offset into the array */
2293 offs = (flags & CF_UNSIGNED)? 3 : 0;
2294 switch (flags & CF_TYPE) {
2307 /* Encode the operation */
2308 if (flags & CF_CONST) {
2309 /* Constant value given */
2310 if (val == 0 && subs [offs+0]) {
2311 /* Special case: constant with value zero */
2312 AddCodeLine ("\tjsr\t%s", subs [offs+0]);
2313 } else if (val < 0x100 && subs [offs+1]) {
2314 /* Special case: constant with high byte zero */
2315 ldaconst (val); /* Load low byte */
2316 AddCodeLine ("\tjsr\t%s", subs [offs+1]);
2318 /* Others: arbitrary constant value */
2319 g_getimmed (flags, val, 0); /* Load value */
2320 AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2323 /* Value not constant (is already in (e)ax) */
2324 AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2327 /* The operation will pop it's argument */
2333 void g_test (unsigned flags)
2334 /* Force a test to set cond codes right */
2336 switch (flags & CF_TYPE) {
2339 if (flags & CF_FORCECHAR) {
2340 AddCodeLine ("\ttax");
2346 AddCodeLine ("\tstx\ttmp1");
2347 AddCodeLine ("\tora\ttmp1");
2351 if (flags & CF_UNSIGNED) {
2352 AddCodeLine ("\tjsr\tutsteax");
2354 AddCodeLine ("\tjsr\ttsteax");
2366 void g_push (unsigned flags, unsigned long val)
2367 /* Push the primary register or a constant value onto the stack */
2371 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2373 /* We have a constant 8 or 16 bit value */
2374 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2376 /* Handle as 8 bit value */
2377 if (FavourSize && val <= 2) {
2378 AddCodeLine ("\tjsr\tpushc%d", (int) val);
2381 AddCodeLine ("\tjsr\tpusha");
2386 /* Handle as 16 bit value */
2387 hi = (unsigned char) (val >> 8);
2389 AddCodeLine ("\tjsr\tpush%u", (unsigned) val);
2390 } else if (hi == 0 || hi == 0xFF) {
2391 /* Use special function */
2393 AddCodeLine ("\tjsr\t%s", (hi == 0)? "pusha0" : "pushaFF");
2396 g_getimmed (flags, val, 0);
2397 AddCodeLine ("\tjsr\tpushax");
2403 /* Value is not 16 bit or not constant */
2404 if (flags & CF_CONST) {
2405 /* Constant 32 bit value, load into eax */
2406 g_getimmed (flags, val, 0);
2409 /* Push the primary register */
2410 switch (flags & CF_TYPE) {
2413 if (flags & CF_FORCECHAR) {
2414 /* Handle as char */
2415 AddCodeLine ("\tjsr\tpusha");
2420 AddCodeLine ("\tjsr\tpushax");
2424 AddCodeLine ("\tjsr\tpusheax");
2434 /* Adjust the stack offset */
2440 void g_swap (unsigned flags)
2441 /* Swap the primary register and the top of the stack. flags give the type
2442 * of *both* values (must have same size).
2445 switch (flags & CF_TYPE) {
2449 AddCodeLine ("\tjsr\tswapstk");
2453 AddCodeLine ("\tjsr\tswapestk");
2464 void g_call (unsigned flags, char* lbl, unsigned argsize)
2465 /* Call the specified subroutine name */
2467 if ((flags & CF_FIXARGC) == 0) {
2468 /* Pass arg count */
2471 AddCodeLine ("\tjsr\t_%s", lbl);
2472 oursp += argsize; /* callee pops args */
2477 void g_callind (unsigned flags, unsigned argsize)
2478 /* Call subroutine with address in AX */
2480 if ((flags & CF_FIXARGC) == 0) {
2481 /* Pass arg count */
2484 AddCodeLine ("\tjsr\tcallax"); /* do the call */
2485 oursp += argsize; /* callee pops args */
2490 void g_jump (unsigned label)
2491 /* Jump to specified internal label number */
2493 AddCodeLine ("\tjmp\tL%04X", label);
2498 void g_switch (unsigned flags)
2499 /* Output switch statement preample */
2501 switch (flags & CF_TYPE) {
2505 AddCodeLine ("\tjsr\tswitch");
2509 AddCodeLine ("\tjsr\tlswitch");
2520 void g_case (unsigned flags, unsigned label, unsigned long val)
2521 /* Create table code for one case selector */
2523 switch (flags & CF_TYPE) {
2527 AddCodeLine ("\t.word\t$%04X, L%04X",
2528 (unsigned)(val & 0xFFFF),
2529 (unsigned)(label & 0xFFFF));
2533 AddCodeLine ("\t.dword\t$%08lX", val);
2534 AddCodeLine ("\t.word\tL%04X", label & 0xFFFF);
2545 void g_truejump (unsigned flags, unsigned label)
2546 /* Jump to label if zero flag clear */
2548 if (flags & CF_SHORT) {
2549 AddCodeLine ("\tbne\tL%04X", label);
2551 AddCodeLine ("\tjne\tL%04X", label);
2557 void g_falsejump (unsigned flags, unsigned label)
2558 /* Jump to label if zero flag set */
2560 if (flags & CF_SHORT) {
2561 AddCodeLine ("\tbeq\tL%04X", label);
2563 AddCodeLine ("\tjeq\tL%04X", label);
2569 static void mod_internal (int k, char* verb1, char* verb2)
2572 AddCodeLine ("\tjsr\t%ssp%c", verb1, k + '0');
2576 AddCodeLine ("\tjsr\t%ssp", verb2);
2582 void g_space (int space)
2583 /* Create or drop space on the stack */
2586 mod_internal (-space, "inc", "addy");
2587 } else if (space > 0) {
2588 mod_internal (space, "dec", "suby");
2594 void g_cstackcheck (void)
2595 /* Check for a C stack overflow */
2597 AddCodeLine ("\tjsr\tcstkchk");
2602 void g_stackcheck (void)
2603 /* Check for a stack overflow */
2605 AddCodeLine ("\tjsr\tstkchk");
2610 void g_add (unsigned flags, unsigned long val)
2611 /* Primary = TOS + Primary */
2613 static char* ops [12] = {
2614 0, "tosadda0", "tosaddax",
2615 0, "tosadda0", "tosaddax",
2620 if (flags & CF_CONST) {
2621 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2622 g_push (flags & ~CF_CONST, 0);
2624 oper (flags, val, ops);
2629 void g_sub (unsigned flags, unsigned long val)
2630 /* Primary = TOS - Primary */
2632 static char* ops [12] = {
2633 0, "tossuba0", "tossubax",
2634 0, "tossuba0", "tossubax",
2639 if (flags & CF_CONST) {
2640 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2641 g_push (flags & ~CF_CONST, 0);
2643 oper (flags, val, ops);
2648 void g_rsub (unsigned flags, unsigned long val)
2649 /* Primary = Primary - TOS */
2651 static char* ops [12] = {
2652 0, "tosrsuba0", "tosrsubax",
2653 0, "tosrsuba0", "tosrsubax",
2657 oper (flags, val, ops);
2662 void g_mul (unsigned flags, unsigned long val)
2663 /* Primary = TOS * Primary */
2665 static char* ops [12] = {
2666 0, "tosmula0", "tosmulax",
2667 0, "tosumula0", "tosumulax",
2674 /* Do strength reduction if the value is constant and a power of two */
2675 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2676 /* Generate a shift instead */
2681 /* If the right hand side is const, the lhs is not on stack but still
2682 * in the primary register.
2684 if (flags & CF_CONST) {
2686 switch (flags & CF_TYPE) {
2689 if (flags & CF_FORCECHAR) {
2690 /* Handle some special cases */
2694 AddCodeLine ("\tsta\ttmp1");
2695 AddCodeLine ("\tasl\ta");
2696 AddCodeLine ("\tclc");
2697 AddCodeLine ("\tadc\ttmp1");
2701 AddCodeLine ("\tsta\ttmp1");
2702 AddCodeLine ("\tasl\ta");
2703 AddCodeLine ("\tasl\ta");
2704 AddCodeLine ("\tclc");
2705 AddCodeLine ("\tadc\ttmp1");
2709 AddCodeLine ("\tsta\ttmp1");
2710 AddCodeLine ("\tasl\ta");
2711 AddCodeLine ("\tasl\ta");
2712 AddCodeLine ("\tclc");
2713 AddCodeLine ("\tadc\ttmp1");
2714 AddCodeLine ("\tasl\ta");
2730 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2731 * into the normal, non-optimized stuff.
2733 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2734 g_push (flags & ~CF_CONST, 0);
2738 /* Use long way over the stack */
2739 oper (flags, val, ops);
2744 void g_div (unsigned flags, unsigned long val)
2745 /* Primary = TOS / Primary */
2747 static char* ops [12] = {
2748 0, "tosdiva0", "tosdivax",
2749 0, "tosudiva0", "tosudivax",
2754 /* Do strength reduction if the value is constant and a power of two */
2756 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2757 /* Generate a shift instead */
2760 /* Generate a division */
2761 if (flags & CF_CONST) {
2762 /* lhs is not on stack */
2763 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2764 g_push (flags & ~CF_CONST, 0);
2766 oper (flags, val, ops);
2772 void g_mod (unsigned flags, unsigned long val)
2773 /* Primary = TOS % Primary */
2775 static char* ops [12] = {
2776 0, "tosmoda0", "tosmodax",
2777 0, "tosumoda0", "tosumodax",
2783 /* Check if we can do some cost reduction */
2784 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2785 /* We can do that with an AND operation */
2786 g_and (flags, val - 1);
2788 /* Do it the hard way... */
2789 if (flags & CF_CONST) {
2790 /* lhs is not on stack */
2791 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2792 g_push (flags & ~CF_CONST, 0);
2794 oper (flags, val, ops);
2800 void g_or (unsigned flags, unsigned long val)
2801 /* Primary = TOS | Primary */
2803 static char* ops [12] = {
2804 0, "tosora0", "tosorax",
2805 0, "tosora0", "tosorax",
2810 /* If the right hand side is const, the lhs is not on stack but still
2811 * in the primary register.
2813 if (flags & CF_CONST) {
2815 switch (flags & CF_TYPE) {
2818 if (flags & CF_FORCECHAR) {
2819 if ((val & 0xFF) != 0xFF) {
2820 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2828 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2835 AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2844 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2845 * into the normal, non-optimized stuff.
2847 g_push (flags & ~CF_CONST, 0);
2851 /* Use long way over the stack */
2852 oper (flags, val, ops);
2857 void g_xor (unsigned flags, unsigned long val)
2858 /* Primary = TOS ^ Primary */
2860 static char* ops [12] = {
2861 0, "tosxora0", "tosxorax",
2862 0, "tosxora0", "tosxorax",
2868 /* If the right hand side is const, the lhs is not on stack but still
2869 * in the primary register.
2871 if (flags & CF_CONST) {
2873 switch (flags & CF_TYPE) {
2876 if (flags & CF_FORCECHAR) {
2877 if ((val & 0xFF) != 0) {
2878 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2887 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2890 } else if ((val & 0xFF) == 0) {
2891 AddCodeLine ("\tpha");
2892 AddCodeLine ("\ttxa");
2893 AddCodeLine ("\teor\t#$%02X", (unsigned char)(val >> 8));
2894 AddCodeLine ("\ttax");
2895 AddCodeLine ("\tpla");
2903 AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2913 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2914 * into the normal, non-optimized stuff.
2916 g_push (flags & ~CF_CONST, 0);
2920 /* Use long way over the stack */
2921 oper (flags, val, ops);
2926 void g_and (unsigned flags, unsigned long val)
2927 /* Primary = TOS & Primary */
2929 static char* ops [12] = {
2930 0, "tosanda0", "tosandax",
2931 0, "tosanda0", "tosandax",
2936 /* If the right hand side is const, the lhs is not on stack but still
2937 * in the primary register.
2939 if (flags & CF_CONST) {
2941 switch (flags & CF_TYPE) {
2944 if (flags & CF_FORCECHAR) {
2945 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2950 if ((val & 0xFFFF) != 0xFFFF) {
2955 } else if (val != 0xFF) {
2956 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2958 } else if ((val & 0xFF00) == 0xFF00) {
2959 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2960 } else if ((val & 0x00FF) == 0x0000) {
2961 AddCodeLine ("\ttxa");
2962 AddCodeLine ("\tand\t#$%02X", (unsigned char)(val >> 8));
2963 AddCodeLine ("\ttax");
2966 AddCodeLine ("\ttay");
2967 AddCodeLine ("\ttxa");
2968 AddCodeLine ("\tand\t#$%02X", (unsigned char)(val >> 8));
2969 AddCodeLine ("\ttax");
2970 AddCodeLine ("\ttya");
2971 if ((val & 0x00FF) != 0x00FF) {
2972 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2981 AddCodeLine ("\tstx\tsreg+1");
2982 AddCodeLine ("\tstx\tsreg");
2983 if ((val & 0xFF) != 0xFF) {
2984 AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2987 } else if (val == 0xFF00) {
2989 AddCodeLine ("\tsta\tsreg+1");
2990 AddCodeLine ("\tsta\tsreg");
2999 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3000 * into the normal, non-optimized stuff.
3002 g_push (flags & ~CF_CONST, 0);
3006 /* Use long way over the stack */
3007 oper (flags, val, ops);
3012 void g_asr (unsigned flags, unsigned long val)
3013 /* Primary = TOS >> Primary */
3015 static char* ops [12] = {
3016 0, "tosasra0", "tosasrax",
3017 0, "tosshra0", "tosshrax",
3022 /* If the right hand side is const, the lhs is not on stack but still
3023 * in the primary register.
3025 if (flags & CF_CONST) {
3027 switch (flags & CF_TYPE) {
3031 if (val >= 1 && val <= 3) {
3032 if (flags & CF_UNSIGNED) {
3033 AddCodeLine ("\tjsr\tshrax%ld", val);
3035 AddCodeLine ("\tjsr\tasrax%ld", val);
3038 } else if (val == 8 && (flags & CF_UNSIGNED)) {
3039 AddCodeLine ("\ttxa");
3046 if (val >= 1 && val <= 3) {
3047 if (flags & CF_UNSIGNED) {
3048 AddCodeLine ("\tjsr\tshreax%ld", val);
3050 AddCodeLine ("\tjsr\tasreax%ld", val);
3053 } else if (val == 8 && (flags & CF_UNSIGNED)) {
3054 AddCodeLine ("\ttxa");
3055 AddCodeLine ("\tldx\tsreg");
3056 AddCodeLine ("\tldy\tsreg+1");
3057 AddCodeLine ("\tsty\tsreg");
3058 AddCodeLine ("\tldy\t#$00");
3059 AddCodeLine ("\tsty\tsreg+1");
3061 } else if (val == 16) {
3062 AddCodeLine ("\tldy\t#$00");
3063 AddCodeLine ("\tldx\tsreg+1");
3064 if ((flags & CF_UNSIGNED) == 0) {
3065 AddCodeLine ("\tbpl\t*+3");
3066 AddCodeLine ("\tdey");
3067 AddCodeHint ("y:!");
3069 AddCodeLine ("\tlda\tsreg");
3070 AddCodeLine ("\tsty\tsreg+1");
3071 AddCodeLine ("\tsty\tsreg");
3080 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3081 * into the normal, non-optimized stuff.
3083 g_push (flags & ~CF_CONST, 0);
3087 /* Use long way over the stack */
3088 oper (flags, val, ops);
3093 void g_asl (unsigned flags, unsigned long val)
3094 /* Primary = TOS << Primary */
3096 static char* ops [12] = {
3097 0, "tosasla0", "tosaslax",
3098 0, "tosshla0", "tosshlax",
3104 /* If the right hand side is const, the lhs is not on stack but still
3105 * in the primary register.
3107 if (flags & CF_CONST) {
3109 switch (flags & CF_TYPE) {
3113 if (val >= 1 && val <= 3) {
3114 if (flags & CF_UNSIGNED) {
3115 AddCodeLine ("\tjsr\tshlax%ld", val);
3117 AddCodeLine ("\tjsr\taslax%ld", val);
3120 } else if (val == 8) {
3121 AddCodeLine ("\ttax");
3122 AddCodeLine ("\tlda\t#$00");
3128 if (val >= 1 && val <= 3) {
3129 if (flags & CF_UNSIGNED) {
3130 AddCodeLine ("\tjsr\tshleax%ld", val);
3132 AddCodeLine ("\tjsr\tasleax%ld", val);
3135 } else if (val == 8) {
3136 AddCodeLine ("\tldy\tsreg");
3137 AddCodeLine ("\tsty\tsreg+1");
3138 AddCodeLine ("\tstx\tsreg");
3139 AddCodeLine ("\ttax");
3140 AddCodeLine ("\tlda\t#$00");
3142 } else if (val == 16) {
3143 AddCodeLine ("\tstx\tsreg+1");
3144 AddCodeLine ("\tsta\tsreg");
3145 AddCodeLine ("\tlda\t#$00");
3146 AddCodeLine ("\ttax");
3155 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3156 * into the normal, non-optimized stuff.
3158 g_push (flags & ~CF_CONST, 0);
3162 /* Use long way over the stack */
3163 oper (flags, val, ops);
3168 void g_neg (unsigned flags)
3169 /* Primary = -Primary */
3171 switch (flags & CF_TYPE) {
3175 AddCodeLine ("\tjsr\tnegax");
3179 AddCodeLine ("\tjsr\tnegeax");
3189 void g_bneg (unsigned flags)
3190 /* Primary = !Primary */
3192 switch (flags & CF_TYPE) {
3195 AddCodeLine ("\tjsr\tbnega");
3199 AddCodeLine ("\tjsr\tbnegax");
3203 AddCodeLine ("\tjsr\tbnegeax");
3213 void g_com (unsigned flags)
3214 /* Primary = ~Primary */
3216 switch (flags & CF_TYPE) {
3220 AddCodeLine ("\tjsr\tcomplax");
3224 AddCodeLine ("\tjsr\tcompleax");
3234 void g_inc (unsigned flags, unsigned long val)
3235 /* Increment the primary register by a given number */
3237 /* Don't inc by zero */
3242 /* Generate code for the supported types */
3244 switch (flags & CF_TYPE) {
3247 if (flags & CF_FORCECHAR) {
3248 if (CPU == CPU_65C02 && val <= 2) {
3250 AddCodeLine ("\tina");
3253 AddCodeLine ("\tclc");
3254 AddCodeLine ("\tadc\t#$%02X", (unsigned char)val);
3261 if (CPU == CPU_65C02 && val == 1) {
3262 AddCodeLine ("\tina");
3263 AddCodeLine ("\tbne\t*+3");
3264 AddCodeLine ("\tinx");
3265 /* Tell the optimizer that the X register may be invalid */
3266 AddCodeHint ("x:!");
3267 } else if (FavourSize) {
3270 AddCodeLine ("\tjsr\tincax%lu", val);
3271 } else if (val <= 255) {
3273 AddCodeLine ("\tjsr\tincaxy");
3275 g_add (flags | CF_CONST, val);
3278 /* Inline the code */
3280 if ((val & 0xFF) != 0) {
3281 AddCodeLine ("\tclc");
3282 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3283 AddCodeLine ("\tbcc\t*+3");
3284 AddCodeLine ("\tinx");
3285 /* Tell the optimizer that the X register may be invalid */
3286 AddCodeHint ("x:!");
3289 AddCodeLine ("\tinx");
3292 AddCodeLine ("\tinx");
3295 AddCodeLine ("\tclc");
3296 if ((val & 0xFF) != 0) {
3297 AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3298 /* Tell the optimizer that the X register may be invalid */
3299 AddCodeHint ("x:!");
3301 AddCodeLine ("\tpha");
3302 AddCodeLine ("\ttxa");
3303 AddCodeLine ("\tadc\t#$%02X", (unsigned char) (val >> 8));
3304 AddCodeLine ("\ttax");
3305 AddCodeLine ("\tpla");
3313 AddCodeLine ("\tjsr\tinceaxy");
3315 g_add (flags | CF_CONST, val);
3327 void g_dec (unsigned flags, unsigned long val)
3328 /* Decrement the primary register by a given number */
3330 /* Don't dec by zero */
3335 /* Generate code for the supported types */
3337 switch (flags & CF_TYPE) {
3340 if (flags & CF_FORCECHAR) {
3341 if (CPU == CPU_65C02 && val <= 2) {
3343 AddCodeLine ("\tdea");
3346 AddCodeLine ("\tsec");
3347 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
3355 AddCodeLine ("\tjsr\tdecax%d", (int) val);
3356 } else if (val <= 255) {
3358 AddCodeLine ("\tjsr\tdecaxy");
3360 g_sub (flags | CF_CONST, val);
3367 AddCodeLine ("\tjsr\tdeceaxy");
3369 g_sub (flags | CF_CONST, val);
3382 * Following are the conditional operators. They compare the TOS against
3383 * the primary and put a literal 1 in the primary if the condition is
3384 * true, otherwise they clear the primary register
3389 void g_eq (unsigned flags, unsigned long val)
3390 /* Test for equal */
3392 static char* ops [12] = {
3393 "toseq00", "toseqa0", "toseqax",
3394 "toseq00", "toseqa0", "toseqax",
3399 /* If the right hand side is const, the lhs is not on stack but still
3400 * in the primary register.
3402 if (flags & CF_CONST) {
3404 switch (flags & CF_TYPE) {
3407 if (flags & CF_FORCECHAR) {
3408 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3409 AddCodeLine ("\tjsr\tbooleq");
3415 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3416 AddCodeLine ("\tbne\t*+4");
3417 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3418 AddCodeLine ("\tjsr\tbooleq");
3428 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3429 * into the normal, non-optimized stuff.
3431 g_push (flags & ~CF_CONST, 0);
3435 /* Use long way over the stack */
3436 oper (flags, val, ops);
3441 void g_ne (unsigned flags, unsigned long val)
3442 /* Test for not equal */
3444 static char* ops [12] = {
3445 "tosne00", "tosnea0", "tosneax",
3446 "tosne00", "tosnea0", "tosneax",
3452 /* If the right hand side is const, the lhs is not on stack but still
3453 * in the primary register.
3455 if (flags & CF_CONST) {
3457 switch (flags & CF_TYPE) {
3460 if (flags & CF_FORCECHAR) {
3461 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3462 AddCodeLine ("\tjsr\tboolne");
3468 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3469 AddCodeLine ("\tbne\t*+4");
3470 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3471 AddCodeLine ("\tjsr\tboolne");
3481 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3482 * into the normal, non-optimized stuff.
3484 g_push (flags & ~CF_CONST, 0);
3488 /* Use long way over the stack */
3489 oper (flags, val, ops);
3494 void g_lt (unsigned flags, unsigned long val)
3495 /* Test for less than */
3497 static char* ops [12] = {
3498 "toslt00", "toslta0", "tosltax",
3499 "tosult00", "tosulta0", "tosultax",
3504 /* If the right hand side is const, the lhs is not on stack but still
3505 * in the primary register.
3507 if (flags & CF_CONST) {
3509 /* Give a warning in some special cases */
3510 if ((flags & CF_UNSIGNED) && val == 0) {
3511 Warning ("Condition is never true");
3514 /* Look at the type */
3515 switch (flags & CF_TYPE) {
3518 if (flags & CF_FORCECHAR) {
3519 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3520 if (flags & CF_UNSIGNED) {
3521 AddCodeLine ("\tjsr\tboolult");
3523 AddCodeLine ("\tjsr\tboollt");
3530 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3531 /* If we have a signed compare against zero, we only need to
3532 * test the high byte.
3534 AddCodeLine ("\ttxa");
3535 AddCodeLine ("\tjsr\tboollt");
3538 /* Direct code only for unsigned data types */
3539 if (flags & CF_UNSIGNED) {
3540 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3541 AddCodeLine ("\tbne\t*+4");
3542 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3543 AddCodeLine ("\tjsr\tboolult");
3555 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3556 * into the normal, non-optimized stuff.
3558 g_push (flags & ~CF_CONST, 0);
3562 /* Use long way over the stack */
3563 oper (flags, val, ops);
3568 void g_le (unsigned flags, unsigned long val)
3569 /* Test for less than or equal to */
3571 static char* ops [12] = {
3572 "tosle00", "toslea0", "tosleax",
3573 "tosule00", "tosulea0", "tosuleax",
3579 /* If the right hand side is const, the lhs is not on stack but still
3580 * in the primary register.
3582 if (flags & CF_CONST) {
3584 /* Look at the type */
3585 switch (flags & CF_TYPE) {
3588 if (flags & CF_FORCECHAR) {
3589 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3590 if (flags & CF_UNSIGNED) {
3591 AddCodeLine ("\tjsr\tboolule");
3593 AddCodeLine ("\tjsr\tboolle");
3600 if (flags & CF_UNSIGNED) {
3601 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3602 AddCodeLine ("\tbne\t*+4");
3603 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3604 AddCodeLine ("\tjsr\tboolule");
3616 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3617 * into the normal, non-optimized stuff.
3619 g_push (flags & ~CF_CONST, 0);
3623 /* Use long way over the stack */
3624 oper (flags, val, ops);
3629 void g_gt (unsigned flags, unsigned long val)
3630 /* Test for greater than */
3632 static char* ops [12] = {
3633 "tosgt00", "tosgta0", "tosgtax",
3634 "tosugt00", "tosugta0", "tosugtax",
3640 /* If the right hand side is const, the lhs is not on stack but still
3641 * in the primary register.
3643 if (flags & CF_CONST) {
3645 /* Look at the type */
3646 switch (flags & CF_TYPE) {
3649 if (flags & CF_FORCECHAR) {
3650 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3651 if (flags & CF_UNSIGNED) {
3652 /* If we have a compare > 0, we will replace it by
3653 * != 0 here, since both are identical but the latter
3654 * is easier to optimize.
3657 AddCodeLine ("\tjsr\tboolugt");
3659 AddCodeLine ("\tjsr\tboolne");
3662 AddCodeLine ("\tjsr\tboolgt");
3669 if (flags & CF_UNSIGNED) {
3670 /* If we have a compare > 0, we will replace it by
3671 * != 0 here, since both are identical but the latter
3672 * is easier to optimize.
3674 if ((val & 0xFFFF) == 0) {
3675 AddCodeLine ("\tstx\ttmp1");
3676 AddCodeLine ("\tora\ttmp1");
3677 AddCodeLine ("\tjsr\tboolne");
3679 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3680 AddCodeLine ("\tbne\t*+4");
3681 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3682 AddCodeLine ("\tjsr\tboolugt");
3695 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3696 * into the normal, non-optimized stuff.
3698 g_push (flags & ~CF_CONST, 0);
3702 /* Use long way over the stack */
3703 oper (flags, val, ops);
3708 void g_ge (unsigned flags, unsigned long val)
3709 /* Test for greater than or equal to */
3711 static char* ops [12] = {
3712 "tosge00", "tosgea0", "tosgeax",
3713 "tosuge00", "tosugea0", "tosugeax",
3719 /* If the right hand side is const, the lhs is not on stack but still
3720 * in the primary register.
3722 if (flags & CF_CONST) {
3724 /* Give a warning in some special cases */
3725 if ((flags & CF_UNSIGNED) && val == 0) {
3726 Warning ("Condition is always true");
3729 /* Look at the type */
3730 switch (flags & CF_TYPE) {
3733 if (flags & CF_FORCECHAR) {
3734 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3735 if (flags & CF_UNSIGNED) {
3736 AddCodeLine ("\tjsr\tbooluge");
3738 AddCodeLine ("\tjsr\tboolge");
3745 if (flags & CF_UNSIGNED) {
3746 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3747 AddCodeLine ("\tbne\t*+4");
3748 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3749 AddCodeLine ("\tjsr\tbooluge");
3761 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3762 * into the normal, non-optimized stuff.
3764 g_push (flags & ~CF_CONST, 0);
3768 /* Use long way over the stack */
3769 oper (flags, val, ops);
3774 /*****************************************************************************/
3775 /* Allocating static storage */
3776 /*****************************************************************************/
3780 void g_res (unsigned n)
3781 /* Reserve static storage, n bytes */
3783 AddCodeLine ("\t.res\t%u,$00", n);
3788 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3789 /* Define data with the size given in flags */
3791 if (flags & CF_CONST) {
3793 /* Numeric constant */
3794 switch (flags & CF_TYPE) {
3797 AddCodeLine ("\t.byte\t$%02lX", val & 0xFF);
3801 AddCodeLine ("\t.word\t$%04lX", val & 0xFFFF);
3805 AddCodeLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3816 /* Create the correct label name */
3817 const char* Label = GetLabelName (flags, val, offs);
3819 /* Labels are always 16 bit */
3820 AddCodeLine ("\t.word\t%s", Label);
3827 void g_defbytes (const void* Bytes, unsigned Count)
3828 /* Output a row of bytes as a constant */
3834 /* Cast the buffer pointer */
3835 const unsigned char* Data = (const unsigned char*) Bytes;
3837 /* Output the stuff */
3840 /* How many go into this line? */
3841 if ((Chunk = Count) > 16) {
3846 /* Output one line */
3847 strcpy (Buf, "\t.byte\t");
3850 B += sprintf (B, "$%02X", *Data++);
3856 /* Output the line */
3863 void g_zerobytes (unsigned n)
3864 /* Output n bytes of data initialized with zero */
3866 AddCodeLine ("\t.res\t%u,$00", n);
3871 /*****************************************************************************/
3872 /* Inlined known functions */
3873 /*****************************************************************************/
3877 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3878 /* Inline the strlen() function */
3880 /* We need a label in both cases */
3881 unsigned label = GetLabel ();
3883 /* Two different encodings */
3884 if (flags & CF_CONST) {
3886 /* The address of the string is constant. Create the correct label name */
3887 char* lbuf = GetLabelName (flags, val, offs);
3889 /* Generate the strlen code */
3890 AddCodeLine ("\tldy\t#$FF");
3891 g_defloclabel (label);
3892 AddCodeLine ("\tiny");
3893 AddCodeLine ("\tlda\t%s,y", lbuf);
3894 AddCodeLine ("\tbne\tL%04X", label);
3895 AddCodeLine ("\ttax");
3896 AddCodeLine ("\ttya");
3900 /* Address not constant but in primary */
3902 /* This is too much code, so call strlen instead of inlining */
3903 AddCodeLine ("\tjsr\t_strlen");
3905 /* Inline the function */
3906 AddCodeLine ("\tsta\tptr1");
3907 AddCodeLine ("\tstx\tptr1+1");
3908 AddCodeLine ("\tldy\t#$FF");
3909 g_defloclabel (label);
3910 AddCodeLine ("\tiny");
3911 AddCodeLine ("\tlda\t(ptr1),y");
3912 AddCodeLine ("\tbne\tL%04X", label);
3913 AddCodeLine ("\ttax");
3914 AddCodeLine ("\ttya");