1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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 /*****************************************************************************/
63 /*****************************************************************************/
65 /*****************************************************************************/
69 /* Compiler relative stack pointer */
74 /*****************************************************************************/
76 /*****************************************************************************/
80 static void typeerror (unsigned type)
81 /* Print an error message about an invalid operand type */
83 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
88 static void CheckLocalOffs (unsigned Offs)
89 /* Check the offset into the stack for 8bit range */
92 /* Too many local vars */
93 Error ("Too many local variables");
99 static const char* GetLabelName (unsigned Flags, unsigned long Label, long Offs)
101 static char Buf [256]; /* Label name */
103 /* Create the correct label name */
104 switch (Flags & CF_ADDRMASK) {
107 /* Static memory cell */
109 xsprintf (Buf, sizeof (Buf), "%s%+ld", LocalLabelName (Label), Offs);
111 xsprintf (Buf, sizeof (Buf), "%s", LocalLabelName (Label));
118 xsprintf (Buf, sizeof (Buf), "_%s%+ld", (char*) Label, Offs);
120 xsprintf (Buf, sizeof (Buf), "_%s", (char*) Label);
125 /* Absolute address */
126 xsprintf (Buf, sizeof (Buf), "$%04X", (int)((Label+Offs) & 0xFFFF));
130 /* Variable in register bank */
131 xsprintf (Buf, sizeof (Buf), "regbank+%u", (unsigned)((Label+Offs) & 0xFFFF));
135 Internal ("Invalid address flags: %04X", Flags);
138 /* Return a pointer to the static buffer */
144 /*****************************************************************************/
145 /* Pre- and postamble */
146 /*****************************************************************************/
150 void g_preamble (void)
151 /* Generate the assembler code preamble */
153 /* Create a new (global) segment list and remember it */
157 /* Identify the compiler version */
159 AddTextLine ("; File generated by cc65 v %u.%u.%u",
160 VER_MAJOR, VER_MINOR, VER_PATCH);
163 /* Insert some object file options */
164 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
165 VER_MAJOR, VER_MINOR, VER_PATCH);
167 /* If we're producing code for some other CPU, switch the command set */
168 if (CPU == CPU_65C02) {
169 AddTextLine ("\t.setcpu\t\t\"65C02\"");
172 /* Allow auto import for runtime library routines */
173 AddTextLine ("\t.autoimport\ton");
175 /* Switch the assembler into case sensitive mode */
176 AddTextLine ("\t.case\t\ton");
178 /* Tell the assembler if we want to generate debug info */
179 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
181 /* Import the stack pointer for direct auto variable access */
182 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
184 /* Define long branch macros */
185 AddTextLine ("\t.macpack\tlongbranch");
190 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
191 /* If debug info is enabled, place a file info into the source */
194 /* We have to place this into the global text segment, so it will
195 * appear before all .dbg line statements.
197 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
203 /*****************************************************************************/
204 /* Segment support */
205 /*****************************************************************************/
209 void g_userodata (void)
210 /* Switch to the read only data segment */
212 UseDataSeg (SEG_RODATA);
217 void g_usedata (void)
218 /* Switch to the data segment */
220 UseDataSeg (SEG_DATA);
226 /* Switch to the bss segment */
228 UseDataSeg (SEG_BSS);
233 void g_segname (segment_t Seg, const char* Name)
234 /* Set the name of a segment */
238 /* Remember the new name */
239 NewSegName (Seg, Name);
241 /* Emit a segment directive for the data style segments */
243 case SEG_RODATA: S = CS->ROData; break;
244 case SEG_DATA: S = CS->Data; break;
245 case SEG_BSS: S = CS->BSS; break;
246 default: S = 0; break;
249 DS_AddLine (S, ".segment\t\"%s\"", Name);
255 /*****************************************************************************/
257 /*****************************************************************************/
261 unsigned sizeofarg (unsigned flags)
262 /* Return the size of a function argument type that is encoded in flags */
264 switch (flags & CF_TYPE) {
267 return (flags & CF_FORCECHAR)? 1 : 2;
287 int pop (unsigned flags)
288 /* Pop an argument of the given size */
290 return oursp += sizeofarg (flags);
295 int push (unsigned flags)
296 /* Push an argument of the given size */
298 return oursp -= sizeofarg (flags);
303 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
304 /* The value in Offs is an offset to an address in a/x. Make sure, an object
305 * of the type given in Flags can be loaded or stored into this address by
306 * adding part of the offset to the address in ax, so that the remaining
307 * offset fits into an index register. Return the remaining offset.
310 /* If the offset is too large for a byte register, add the high byte
311 * of the offset to the primary. Beware: We need a special correction
312 * if the offset in the low byte will overflow in the operation.
314 unsigned O = Offs & ~0xFFU;
315 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
316 /* We need to add the low byte also */
320 /* Do the correction if we need one */
322 g_inc (CF_INT | CF_CONST, O);
326 /* Return the new offset */
332 /*****************************************************************************/
333 /* Functions handling local labels */
334 /*****************************************************************************/
338 void g_defcodelabel (unsigned label)
339 /* Define a local code label */
341 CS_AddLabel (CS->Code, LocalLabelName (label));
346 void g_defdatalabel (unsigned label)
347 /* Define a local data label */
349 AddDataLine ("%s:", LocalLabelName (label));
354 /*****************************************************************************/
355 /* Functions handling global labels */
356 /*****************************************************************************/
360 void g_defgloblabel (const char* Name)
361 /* Define a global label with the given name */
363 /* Global labels are always data labels */
364 AddDataLine ("_%s:", Name);
369 void g_defexport (const char* Name, int ZP)
370 /* Export the given label */
373 AddTextLine ("\t.exportzp\t_%s", Name);
375 AddTextLine ("\t.export\t\t_%s", Name);
381 void g_defimport (const char* Name, int ZP)
382 /* Import the given label */
385 AddTextLine ("\t.importzp\t_%s", Name);
387 AddTextLine ("\t.import\t\t_%s", Name);
393 void g_importmainargs (void)
394 /* Forced import of a special symbol that handles arguments to main */
396 AddTextLine ("\t.forceimport\tinitmainargs");
401 /*****************************************************************************/
402 /* Load functions for various registers */
403 /*****************************************************************************/
407 static void ldaconst (unsigned val)
408 /* Load a with a constant */
410 AddCodeLine ("lda #$%02X", val & 0xFF);
415 static void ldxconst (unsigned val)
416 /* Load x with a constant */
418 AddCodeLine ("ldx #$%02X", val & 0xFF);
423 static void ldyconst (unsigned val)
424 /* Load y with a constant */
426 AddCodeLine ("ldy #$%02X", val & 0xFF);
431 /*****************************************************************************/
432 /* Function entry and exit */
433 /*****************************************************************************/
437 /* Remember the argument size of a function. The variable is set by g_enter
438 * and used by g_leave. If the functions gets its argument size by the caller
439 * (variable param list or function without prototype), g_enter will set the
445 void g_enter (unsigned flags, unsigned argsize)
446 /* Function prologue */
448 if ((flags & CF_FIXARGC) != 0) {
449 /* Just remember the argument size for the leave */
453 AddCodeLine ("jsr enter");
460 /* Function epilogue */
462 /* How many bytes of locals do we have to drop? */
465 /* If we didn't have a variable argument list, don't call leave */
468 /* Drop stackframe if needed */
472 AddCodeLine ("jsr incsp%d", k);
476 AddCodeLine ("jsr addysp");
483 /* Nothing to drop */
484 AddCodeLine ("jsr leave");
486 /* We've a stack frame to drop */
488 AddCodeLine ("jsr leavey");
492 /* Add the final rts */
498 /*****************************************************************************/
499 /* Register variables */
500 /*****************************************************************************/
504 void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
505 /* Swap a register variable with a location on the stack */
507 /* Calculate the actual stack offset and check it */
509 CheckLocalOffs (StackOffs);
514 if (CodeSizeFactor < 165) {
515 ldyconst (StackOffs);
517 AddCodeLine ("jsr regswap1");
519 ldyconst (StackOffs);
520 AddCodeLine ("lda (sp),y");
521 AddCodeLine ("ldx regbank%+d", RegOffs);
522 AddCodeLine ("sta regbank%+d", RegOffs);
524 AddCodeLine ("sta (sp),y");
527 } else if (Bytes == 2) {
529 ldyconst (StackOffs);
531 AddCodeLine ("jsr regswap2");
535 ldyconst (StackOffs);
538 AddCodeLine ("jsr regswap");
544 void g_save_regvars (int RegOffs, unsigned Bytes)
545 /* Save register variables */
547 /* Don't loop for up to two bytes */
550 AddCodeLine ("lda regbank%+d", RegOffs);
551 AddCodeLine ("jsr pusha");
553 } else if (Bytes == 2) {
555 AddCodeLine ("lda regbank%+d", RegOffs);
556 AddCodeLine ("ldx regbank%+d", RegOffs+1);
557 AddCodeLine ("jsr pushax");
561 /* More than two bytes - loop */
562 unsigned Label = GetLocalLabel ();
564 ldyconst (Bytes - 1);
566 g_defcodelabel (Label);
567 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
568 AddCodeLine ("sta (sp),y");
571 AddCodeLine ("bne %s", LocalLabelName (Label));
575 /* We pushed stuff, correct the stack pointer */
581 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
582 /* Restore register variables */
584 /* Calculate the actual stack offset and check it */
586 CheckLocalOffs (StackOffs);
588 /* Don't loop for up to two bytes */
591 ldyconst (StackOffs);
592 AddCodeLine ("lda (sp),y");
593 AddCodeLine ("sta regbank%+d", RegOffs);
595 } else if (Bytes == 2) {
597 ldyconst (StackOffs);
598 AddCodeLine ("lda (sp),y");
599 AddCodeLine ("sta regbank%+d", RegOffs);
601 AddCodeLine ("lda (sp),y");
602 AddCodeLine ("sta regbank%+d", RegOffs+1);
604 } else if (Bytes == 3 && CodeSizeFactor >= 133) {
606 ldyconst (StackOffs);
607 AddCodeLine ("lda (sp),y");
608 AddCodeLine ("sta regbank%+d", RegOffs);
610 AddCodeLine ("lda (sp),y");
611 AddCodeLine ("sta regbank%+d", RegOffs+1);
613 AddCodeLine ("lda (sp),y");
614 AddCodeLine ("sta regbank%+d", RegOffs+2);
618 /* More bytes - loop */
619 unsigned Label = GetLocalLabel ();
620 ldyconst (StackOffs);
621 g_defcodelabel (Label);
622 AddCodeLine ("lda (sp),y");
623 AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs);
625 AddCodeLine ("cpy #$%02X", StackOffs + Bytes);
626 AddCodeLine ("bne %s", LocalLabelName (Label));
632 /*****************************************************************************/
633 /* Fetching memory cells */
634 /*****************************************************************************/
638 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
639 /* Load a constant into the primary register */
641 unsigned char B1, B2, B3, B4;
645 if ((Flags & CF_CONST) != 0) {
647 /* Numeric constant */
648 switch (Flags & CF_TYPE) {
651 if ((Flags & CF_FORCECHAR) != 0) {
657 ldxconst ((Val >> 8) & 0xFF);
658 ldaconst (Val & 0xFF);
662 /* Split the value into 4 bytes */
663 B1 = (unsigned char) (Val >> 0);
664 B2 = (unsigned char) (Val >> 8);
665 B3 = (unsigned char) (Val >> 16);
666 B4 = (unsigned char) (Val >> 24);
668 /* Remember which bytes are done */
672 AddCodeLine ("ldx #$%02X", B2);
675 AddCodeLine ("stx sreg");
679 AddCodeLine ("stx sreg+1");
682 if ((Done & 0x04) == 0 && B1 != B3) {
683 AddCodeLine ("lda #$%02X", B3);
684 AddCodeLine ("sta sreg");
687 if ((Done & 0x08) == 0 && B1 != B4) {
688 AddCodeLine ("lda #$%02X", B4);
689 AddCodeLine ("sta sreg+1");
692 AddCodeLine ("lda #$%02X", B1);
694 if ((Done & 0x04) == 0) {
696 AddCodeLine ("sta sreg");
698 if ((Done & 0x08) == 0) {
700 AddCodeLine ("sta sreg+1");
712 /* Some sort of label */
713 const char* Label = GetLabelName (Flags, Val, Offs);
715 /* Load the address into the primary */
716 AddCodeLine ("lda #<(%s)", Label);
717 AddCodeLine ("ldx #>(%s)", Label);
724 void g_getstatic (unsigned flags, unsigned long label, long offs)
725 /* Fetch an static memory cell into the primary register */
727 /* Create the correct label name */
728 const char* lbuf = GetLabelName (flags, label, offs);
730 /* Check the size and generate the correct load operation */
731 switch (flags & CF_TYPE) {
734 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
735 AddCodeLine ("lda %s", lbuf); /* load A from the label */
738 AddCodeLine ("lda %s", lbuf); /* load A from the label */
739 if (!(flags & CF_UNSIGNED)) {
740 /* Must sign extend */
741 unsigned L = GetLocalLabel ();
742 AddCodeLine ("bpl %s", LocalLabelName (L));
750 AddCodeLine ("lda %s", lbuf);
751 if (flags & CF_TEST) {
752 AddCodeLine ("ora %s+1", lbuf);
754 AddCodeLine ("ldx %s+1", lbuf);
759 if (flags & CF_TEST) {
760 AddCodeLine ("lda %s+3", lbuf);
761 AddCodeLine ("ora %s+2", lbuf);
762 AddCodeLine ("ora %s+1", lbuf);
763 AddCodeLine ("ora %s+0", lbuf);
765 AddCodeLine ("lda %s+3", lbuf);
766 AddCodeLine ("sta sreg+1");
767 AddCodeLine ("lda %s+2", lbuf);
768 AddCodeLine ("sta sreg");
769 AddCodeLine ("ldx %s+1", lbuf);
770 AddCodeLine ("lda %s", lbuf);
782 void g_getlocal (unsigned flags, int offs)
783 /* Fetch specified local object (local var). */
786 CheckLocalOffs (offs);
787 switch (flags & CF_TYPE) {
790 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
792 AddCodeLine ("lda (sp),y");
795 AddCodeLine ("ldx #$00");
796 AddCodeLine ("lda (sp),y");
797 if ((flags & CF_UNSIGNED) == 0) {
798 unsigned L = GetLocalLabel();
799 AddCodeLine ("bpl %s", LocalLabelName (L));
807 CheckLocalOffs (offs + 1);
808 if (flags & CF_TEST) {
810 AddCodeLine ("lda (sp),y");
812 AddCodeLine ("ora (sp),y");
815 AddCodeLine ("jsr ldaxysp");
821 AddCodeLine ("jsr ldeaxysp");
831 void g_getind (unsigned flags, unsigned offs)
832 /* Fetch the specified object type indirect through the primary register
833 * into the primary register
836 /* If the offset is greater than 255, add the part that is > 255 to
837 * the primary. This way we get an easy addition and use the low byte
840 offs = MakeByteOffs (flags, offs);
842 /* Handle the indirect fetch */
843 switch (flags & CF_TYPE) {
846 /* Character sized */
847 if (flags & CF_UNSIGNED) {
849 AddCodeLine ("jsr ldauidx");
852 AddCodeLine ("jsr ldaidx");
857 if (flags & CF_TEST) {
859 AddCodeLine ("sta ptr1");
860 AddCodeLine ("stx ptr1+1");
861 AddCodeLine ("lda (ptr1),y");
863 AddCodeLine ("ora (ptr1),y");
866 AddCodeLine ("jsr ldaxidx");
872 AddCodeLine ("jsr ldeaxidx");
873 if (flags & CF_TEST) {
874 AddCodeLine ("jsr tsteax");
886 void g_leasp (int offs)
887 /* Fetch the address of the specified symbol into the primary register */
889 /* Calculate the offset relative to sp */
892 /* For value 0 we do direct code */
894 AddCodeLine ("lda sp");
895 AddCodeLine ("ldx sp+1");
897 if (CodeSizeFactor < 300) {
898 ldaconst (offs); /* Load A with offset value */
899 AddCodeLine ("jsr leaasp"); /* Load effective address */
901 unsigned L = GetLocalLabel ();
902 if (CPU == CPU_65C02 && offs == 1) {
903 AddCodeLine ("lda sp");
904 AddCodeLine ("ldx sp+1");
906 AddCodeLine ("bne %s", LocalLabelName (L));
911 AddCodeLine ("ldx sp+1");
912 AddCodeLine ("adc sp");
913 AddCodeLine ("bcc %s", LocalLabelName (L));
923 void g_leavariadic (int Offs)
924 /* Fetch the address of a parameter in a variadic function into the primary
928 unsigned ArgSizeOffs;
930 /* Calculate the offset relative to sp */
933 /* Get the offset of the parameter which is stored at sp+0 on function
934 * entry and check if this offset is reachable with a byte offset.
937 ArgSizeOffs = -oursp;
938 CheckLocalOffs (ArgSizeOffs);
940 /* Get the size of all parameters. */
941 ldyconst (ArgSizeOffs);
942 AddCodeLine ("lda (sp),y");
944 /* Add the value of the stackpointer */
945 if (CodeSizeFactor > 250) {
946 unsigned L = GetLocalLabel();
947 AddCodeLine ("ldx sp+1");
949 AddCodeLine ("adc sp");
950 AddCodeLine ("bcc %s", LocalLabelName (L));
954 AddCodeLine ("jsr leaasp");
957 /* Add the offset to the primary */
959 g_inc (CF_INT | CF_CONST, Offs);
960 } else if (Offs < 0) {
961 g_dec (CF_INT | CF_CONST, -Offs);
967 /*****************************************************************************/
968 /* Store into memory */
969 /*****************************************************************************/
973 void g_putstatic (unsigned flags, unsigned long label, long offs)
974 /* Store the primary register into the specified static memory cell */
976 /* Create the correct label name */
977 const char* lbuf = GetLabelName (flags, label, offs);
979 /* Check the size and generate the correct store operation */
980 switch (flags & CF_TYPE) {
983 AddCodeLine ("sta %s", lbuf);
987 AddCodeLine ("sta %s", lbuf);
988 AddCodeLine ("stx %s+1", lbuf);
992 AddCodeLine ("sta %s", lbuf);
993 AddCodeLine ("stx %s+1", lbuf);
994 AddCodeLine ("ldy sreg");
995 AddCodeLine ("sty %s+2", lbuf);
996 AddCodeLine ("ldy sreg+1");
997 AddCodeLine ("sty %s+3", lbuf);
1008 void g_putlocal (unsigned Flags, int Offs, long Val)
1009 /* Put data into local object. */
1012 CheckLocalOffs (Offs);
1013 switch (Flags & CF_TYPE) {
1016 if (Flags & CF_CONST) {
1017 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1020 AddCodeLine ("sta (sp),y");
1024 if (Flags & CF_CONST) {
1026 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1027 AddCodeLine ("sta (sp),y");
1028 if ((Flags & CF_NOKEEP) == 0) {
1029 /* Place high byte into X */
1030 AddCodeLine ("tax");
1032 if ((Val & 0xFF) == Offs+1) {
1033 /* The value we need is already in Y */
1034 AddCodeLine ("tya");
1035 AddCodeLine ("dey");
1037 AddCodeLine ("dey");
1038 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1040 AddCodeLine ("sta (sp),y");
1042 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
1044 AddCodeLine ("jsr staxysp");
1047 AddCodeLine ("sta (sp),y");
1048 AddCodeLine ("iny");
1049 AddCodeLine ("txa");
1050 AddCodeLine ("sta (sp),y");
1056 if (Flags & CF_CONST) {
1057 g_getimmed (Flags, Val, 0);
1060 AddCodeLine ("jsr steaxysp");
1071 void g_putind (unsigned Flags, unsigned Offs)
1072 /* Store the specified object type in the primary register at the address
1073 * on the top of the stack
1076 /* We can handle offsets below $100 directly, larger offsets must be added
1077 * to the address. Since a/x is in use, best code is achieved by adding
1078 * just the high byte. Be sure to check if the low byte will overflow while
1081 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1083 /* Overflow - we need to add the low byte also */
1084 AddCodeLine ("ldy #$00");
1085 AddCodeLine ("clc");
1086 AddCodeLine ("pha");
1087 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1088 AddCodeLine ("adc (sp),y");
1089 AddCodeLine ("sta (sp),y");
1090 AddCodeLine ("iny");
1091 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1092 AddCodeLine ("adc (sp),y");
1093 AddCodeLine ("sta (sp),y");
1094 AddCodeLine ("pla");
1096 /* Complete address is on stack, new offset is zero */
1099 } else if ((Offs & 0xFF00) != 0) {
1101 /* We can just add the high byte */
1102 AddCodeLine ("ldy #$01");
1103 AddCodeLine ("clc");
1104 AddCodeLine ("pha");
1105 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1106 AddCodeLine ("adc (sp),y");
1107 AddCodeLine ("sta (sp),y");
1108 AddCodeLine ("pla");
1110 /* Offset is now just the low byte */
1114 /* Check the size and determine operation */
1115 switch (Flags & CF_TYPE) {
1119 AddCodeLine ("jsr staspidx");
1124 AddCodeLine ("jsr staxspidx");
1129 AddCodeLine ("jsr steaxspidx");
1137 /* Pop the argument which is always a pointer */
1143 /*****************************************************************************/
1144 /* type conversion and similiar stuff */
1145 /*****************************************************************************/
1149 void g_toslong (unsigned flags)
1150 /* Make sure, the value on TOS is a long. Convert if necessary */
1152 switch (flags & CF_TYPE) {
1156 if (flags & CF_UNSIGNED) {
1157 AddCodeLine ("jsr tosulong");
1159 AddCodeLine ("jsr toslong");
1174 void g_tosint (unsigned flags)
1175 /* Make sure, the value on TOS is an int. Convert if necessary */
1177 switch (flags & CF_TYPE) {
1184 AddCodeLine ("jsr tosint");
1195 void g_regint (unsigned Flags)
1196 /* Make sure, the value in the primary register an int. Convert if necessary */
1200 switch (Flags & CF_TYPE) {
1203 if (Flags & CF_FORCECHAR) {
1204 /* Conversion is from char */
1205 if (Flags & CF_UNSIGNED) {
1206 AddCodeLine ("ldx #$00");
1208 L = GetLocalLabel();
1209 AddCodeLine ("ldx #$00");
1210 AddCodeLine ("cmp #$80");
1211 AddCodeLine ("bcc %s", LocalLabelName (L));
1212 AddCodeLine ("dex");
1229 void g_reglong (unsigned Flags)
1230 /* Make sure, the value in the primary register a long. Convert if necessary */
1234 switch (Flags & CF_TYPE) {
1237 if (Flags & CF_FORCECHAR) {
1238 /* Conversion is from char */
1239 if (Flags & CF_UNSIGNED) {
1240 if (CodeSizeFactor >= 200) {
1241 AddCodeLine ("ldx #$00");
1242 AddCodeLine ("stx sreg");
1243 AddCodeLine ("stx sreg+1");
1245 AddCodeLine ("jsr aulong");
1248 if (CodeSizeFactor >= 366) {
1249 L = GetLocalLabel();
1250 AddCodeLine ("ldx #$00");
1251 AddCodeLine ("cmp #$80");
1252 AddCodeLine ("bcc %s", LocalLabelName (L));
1253 AddCodeLine ("dex");
1255 AddCodeLine ("stx sreg");
1256 AddCodeLine ("stx sreg+1");
1258 AddCodeLine ("jsr along");
1265 if (Flags & CF_UNSIGNED) {
1266 if (CodeSizeFactor >= 200) {
1268 AddCodeLine ("sty sreg");
1269 AddCodeLine ("sty sreg+1");
1271 AddCodeLine ("jsr axulong");
1274 AddCodeLine ("jsr axlong");
1288 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1289 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1290 * value, that corresponds to the value on TOS, rhs corresponds to the value
1291 * in (e)ax. The return value is the the flags value for the resulting type.
1294 unsigned ltype, rtype;
1297 /* Get the type spec from the flags */
1298 ltype = lhs & CF_TYPE;
1299 rtype = rhs & CF_TYPE;
1301 /* Check if a conversion is needed */
1302 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1303 /* We must promote the primary register to long */
1305 /* Get the new rhs type */
1306 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1308 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1309 /* We must promote the lhs to long */
1315 /* Get the new rhs type */
1316 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1320 /* Determine the result type for the operation:
1321 * - The result is const if both operands are const.
1322 * - The result is unsigned if one of the operands is unsigned.
1323 * - The result is long if one of the operands is long.
1324 * - Otherwise the result is int sized.
1326 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1327 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1328 if (rtype == CF_LONG || ltype == CF_LONG) {
1338 unsigned g_typecast (unsigned lhs, unsigned rhs)
1339 /* Cast the value in the primary register to the operand size that is flagged
1340 * by the lhs value. Return the result value.
1343 unsigned ltype, rtype;
1345 /* Get the type spec from the flags */
1346 ltype = lhs & CF_TYPE;
1347 rtype = rhs & CF_TYPE;
1349 /* Check if a conversion is needed */
1350 if ((rhs & CF_CONST) == 0) {
1351 if (ltype == CF_LONG && rtype != CF_LONG) {
1352 /* We must promote the primary register to long */
1354 } else if (ltype == CF_INT && rtype != CF_INT) {
1355 /* We must promote the primary register to int */
1360 /* Do not need any other action. If the left type is int, and the primary
1361 * register is long, it will be automagically truncated. If the right hand
1362 * side is const, it is not located in the primary register and handled by
1363 * the expression parser code.
1366 /* Result is const if the right hand side was const */
1367 lhs |= (rhs & CF_CONST);
1369 /* The resulting type is that of the left hand side (that's why you called
1377 void g_scale (unsigned flags, long val)
1378 /* Scale the value in the primary register by the given value. If val is positive,
1379 * scale up, is val is negative, scale down. This function is used to scale
1380 * the operands or results of pointer arithmetic by the size of the type, the
1381 * pointer points to.
1386 /* Value may not be zero */
1388 Internal ("Data type has no size");
1389 } else if (val > 0) {
1392 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1394 /* Factor is 2, 4, 8 and 16, use special function */
1395 switch (flags & CF_TYPE) {
1398 if (flags & CF_FORCECHAR) {
1400 AddCodeLine ("asl a");
1407 if (CodeSizeFactor >= (p2+1)*130U) {
1408 AddCodeLine ("stx tmp1");
1410 AddCodeLine ("asl a");
1411 AddCodeLine ("rol tmp1");
1413 AddCodeLine ("ldx tmp1");
1415 if (flags & CF_UNSIGNED) {
1416 AddCodeLine ("jsr shlax%d", p2);
1418 AddCodeLine ("jsr aslax%d", p2);
1424 if (flags & CF_UNSIGNED) {
1425 AddCodeLine ("jsr shleax%d", p2);
1427 AddCodeLine ("jsr asleax%d", p2);
1436 } else if (val != 1) {
1438 /* Use a multiplication instead */
1439 g_mul (flags | CF_CONST, val);
1447 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1449 /* Factor is 2, 4, 8 and 16 use special function */
1450 switch (flags & CF_TYPE) {
1453 if (flags & CF_FORCECHAR) {
1454 if (flags & CF_UNSIGNED) {
1456 AddCodeLine ("lsr a");
1459 } else if (p2 <= 2) {
1460 AddCodeLine ("cmp #$80");
1461 AddCodeLine ("ror a");
1468 if (flags & CF_UNSIGNED) {
1469 if (CodeSizeFactor >= (p2+1)*130U) {
1470 AddCodeLine ("stx tmp1");
1472 AddCodeLine ("lsr tmp1");
1473 AddCodeLine ("ror a");
1475 AddCodeLine ("ldx tmp1");
1477 AddCodeLine ("jsr lsrax%d", p2);
1480 if (CodeSizeFactor >= (p2+1)*150U) {
1481 AddCodeLine ("stx tmp1");
1483 AddCodeLine ("cpx #$80");
1484 AddCodeLine ("ror tmp1");
1485 AddCodeLine ("ror a");
1487 AddCodeLine ("ldx tmp1");
1489 AddCodeLine ("jsr asrax%d", p2);
1495 if (flags & CF_UNSIGNED) {
1496 AddCodeLine ("jsr lsreax%d", p2);
1498 AddCodeLine ("jsr asreax%d", p2);
1507 } else if (val != 1) {
1509 /* Use a division instead */
1510 g_div (flags | CF_CONST, val);
1518 /*****************************************************************************/
1519 /* Adds and subs of variables fix a fixed address */
1520 /*****************************************************************************/
1524 void g_addlocal (unsigned flags, int offs)
1525 /* Add a local variable to ax */
1529 /* Correct the offset and check it */
1531 CheckLocalOffs (offs);
1533 switch (flags & CF_TYPE) {
1536 L = GetLocalLabel();
1537 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1538 AddCodeLine ("clc");
1539 AddCodeLine ("adc (sp),y");
1540 AddCodeLine ("bcc %s", LocalLabelName (L));
1541 AddCodeLine ("inx");
1546 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1547 AddCodeLine ("clc");
1548 AddCodeLine ("adc (sp),y");
1549 AddCodeLine ("pha");
1550 AddCodeLine ("txa");
1551 AddCodeLine ("iny");
1552 AddCodeLine ("adc (sp),y");
1553 AddCodeLine ("tax");
1554 AddCodeLine ("pla");
1558 /* Do it the old way */
1560 g_getlocal (flags, offs);
1572 void g_addstatic (unsigned flags, unsigned long label, long offs)
1573 /* Add a static variable to ax */
1577 /* Create the correct label name */
1578 const char* lbuf = GetLabelName (flags, label, offs);
1580 switch (flags & CF_TYPE) {
1583 L = GetLocalLabel();
1584 AddCodeLine ("clc");
1585 AddCodeLine ("adc %s", lbuf);
1586 AddCodeLine ("bcc %s", LocalLabelName (L));
1587 AddCodeLine ("inx");
1592 AddCodeLine ("clc");
1593 AddCodeLine ("adc %s", lbuf);
1594 AddCodeLine ("tay");
1595 AddCodeLine ("txa");
1596 AddCodeLine ("adc %s+1", lbuf);
1597 AddCodeLine ("tax");
1598 AddCodeLine ("tya");
1602 /* Do it the old way */
1604 g_getstatic (flags, label, offs);
1616 /*****************************************************************************/
1617 /* Special op= functions */
1618 /*****************************************************************************/
1622 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1624 /* Emit += for a static variable */
1626 /* Create the correct label name */
1627 const char* lbuf = GetLabelName (flags, label, offs);
1629 /* Check the size and determine operation */
1630 switch (flags & CF_TYPE) {
1633 if (flags & CF_FORCECHAR) {
1634 AddCodeLine ("ldx #$00");
1635 if (flags & CF_CONST) {
1637 AddCodeLine ("inc %s", lbuf);
1638 AddCodeLine ("lda %s", lbuf);
1640 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1641 AddCodeLine ("clc");
1642 AddCodeLine ("adc %s", lbuf);
1643 AddCodeLine ("sta %s", lbuf);
1646 AddCodeLine ("clc");
1647 AddCodeLine ("adc %s", lbuf);
1648 AddCodeLine ("sta %s", lbuf);
1650 if ((flags & CF_UNSIGNED) == 0) {
1651 unsigned L = GetLocalLabel();
1652 AddCodeLine ("bpl %s", LocalLabelName (L));
1653 AddCodeLine ("dex");
1661 if (flags & CF_CONST) {
1663 unsigned L = GetLocalLabel ();
1664 AddCodeLine ("inc %s", lbuf);
1665 AddCodeLine ("bne %s", LocalLabelName (L));
1666 AddCodeLine ("inc %s+1", lbuf);
1668 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1669 AddCodeLine ("ldx %s+1", lbuf);
1671 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1672 AddCodeLine ("clc");
1673 AddCodeLine ("adc %s", lbuf);
1674 AddCodeLine ("sta %s", lbuf);
1676 unsigned L = GetLocalLabel ();
1677 AddCodeLine ("bcc %s", LocalLabelName (L));
1678 AddCodeLine ("inc %s+1", lbuf);
1680 AddCodeLine ("ldx %s+1", lbuf);
1682 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1683 AddCodeLine ("adc %s+1", lbuf);
1684 AddCodeLine ("sta %s+1", lbuf);
1685 AddCodeLine ("tax");
1686 AddCodeLine ("lda %s", lbuf);
1690 AddCodeLine ("clc");
1691 AddCodeLine ("adc %s", lbuf);
1692 AddCodeLine ("sta %s", lbuf);
1693 AddCodeLine ("txa");
1694 AddCodeLine ("adc %s+1", lbuf);
1695 AddCodeLine ("sta %s+1", lbuf);
1696 AddCodeLine ("tax");
1697 AddCodeLine ("lda %s", lbuf);
1702 if (flags & CF_CONST) {
1704 AddCodeLine ("ldy #<(%s)", lbuf);
1705 AddCodeLine ("sty ptr1");
1706 AddCodeLine ("ldy #>(%s)", lbuf);
1708 AddCodeLine ("jsr laddeq1");
1710 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1711 AddCodeLine ("jsr laddeqa");
1714 g_getstatic (flags, label, offs);
1716 g_putstatic (flags, label, offs);
1719 AddCodeLine ("ldy #<(%s)", lbuf);
1720 AddCodeLine ("sty ptr1");
1721 AddCodeLine ("ldy #>(%s)", lbuf);
1722 AddCodeLine ("jsr laddeq");
1733 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1734 /* Emit += for a local variable */
1736 /* Calculate the true offset, check it, load it into Y */
1738 CheckLocalOffs (offs);
1740 /* Check the size and determine operation */
1741 switch (flags & CF_TYPE) {
1744 if (flags & CF_FORCECHAR) {
1746 AddCodeLine ("ldx #$00");
1747 if (flags & CF_CONST) {
1748 AddCodeLine ("clc");
1749 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1750 AddCodeLine ("adc (sp),y");
1751 AddCodeLine ("sta (sp),y");
1753 AddCodeLine ("clc");
1754 AddCodeLine ("adc (sp),y");
1755 AddCodeLine ("sta (sp),y");
1757 if ((flags & CF_UNSIGNED) == 0) {
1758 unsigned L = GetLocalLabel();
1759 AddCodeLine ("bpl %s", LocalLabelName (L));
1760 AddCodeLine ("dex");
1769 if (flags & CF_CONST) {
1770 if (CodeSizeFactor >= 400) {
1771 AddCodeLine ("clc");
1772 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1773 AddCodeLine ("adc (sp),y");
1774 AddCodeLine ("sta (sp),y");
1775 AddCodeLine ("iny");
1776 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1777 AddCodeLine ("adc (sp),y");
1778 AddCodeLine ("sta (sp),y");
1779 AddCodeLine ("tax");
1780 AddCodeLine ("dey");
1781 AddCodeLine ("lda (sp),y");
1783 g_getimmed (flags, val, 0);
1784 AddCodeLine ("jsr addeqysp");
1787 AddCodeLine ("jsr addeqysp");
1792 if (flags & CF_CONST) {
1793 g_getimmed (flags, val, 0);
1796 AddCodeLine ("jsr laddeqysp");
1806 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1807 /* Emit += for the location with address in ax */
1809 /* If the offset is too large for a byte register, add the high byte
1810 * of the offset to the primary. Beware: We need a special correction
1811 * if the offset in the low byte will overflow in the operation.
1813 offs = MakeByteOffs (flags, offs);
1815 /* Check the size and determine operation */
1816 switch (flags & CF_TYPE) {
1819 AddCodeLine ("sta ptr1");
1820 AddCodeLine ("stx ptr1+1");
1821 AddCodeLine ("ldy #$%02X", offs);
1822 AddCodeLine ("ldx #$00");
1823 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1824 AddCodeLine ("clc");
1825 AddCodeLine ("adc (ptr1),y");
1826 AddCodeLine ("sta (ptr1),y");
1830 if (CodeSizeFactor >= 200) {
1831 /* Lots of code, use only if size is not important */
1832 AddCodeLine ("sta ptr1");
1833 AddCodeLine ("stx ptr1+1");
1834 AddCodeLine ("ldy #$%02X", offs);
1835 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1836 AddCodeLine ("clc");
1837 AddCodeLine ("adc (ptr1),y");
1838 AddCodeLine ("sta (ptr1),y");
1839 AddCodeLine ("pha");
1840 AddCodeLine ("iny");
1841 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1842 AddCodeLine ("adc (ptr1),y");
1843 AddCodeLine ("sta (ptr1),y");
1844 AddCodeLine ("tax");
1845 AddCodeLine ("pla");
1851 AddCodeLine ("jsr pushax"); /* Push the address */
1852 push (CF_PTR); /* Correct the internal sp */
1853 g_getind (flags, offs); /* Fetch the value */
1854 g_inc (flags, val); /* Increment value in primary */
1855 g_putind (flags, offs); /* Store the value back */
1865 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1867 /* Emit -= for a static variable */
1869 /* Create the correct label name */
1870 const char* lbuf = GetLabelName (flags, label, offs);
1872 /* Check the size and determine operation */
1873 switch (flags & CF_TYPE) {
1876 if (flags & CF_FORCECHAR) {
1877 AddCodeLine ("ldx #$00");
1878 if (flags & CF_CONST) {
1880 AddCodeLine ("dec %s", lbuf);
1881 AddCodeLine ("lda %s", lbuf);
1883 AddCodeLine ("sec");
1884 AddCodeLine ("lda %s", lbuf);
1885 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1886 AddCodeLine ("sta %s", lbuf);
1889 AddCodeLine ("eor #$FF");
1890 AddCodeLine ("sec");
1891 AddCodeLine ("adc %s", lbuf);
1892 AddCodeLine ("sta %s", lbuf);
1894 if ((flags & CF_UNSIGNED) == 0) {
1895 unsigned L = GetLocalLabel();
1896 AddCodeLine ("bpl %s", LocalLabelName (L));
1897 AddCodeLine ("dex");
1905 AddCodeLine ("sec");
1906 if (flags & CF_CONST) {
1907 AddCodeLine ("lda %s", lbuf);
1908 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1909 AddCodeLine ("sta %s", lbuf);
1911 unsigned L = GetLocalLabel ();
1912 AddCodeLine ("bcs %s", LocalLabelName (L));
1913 AddCodeLine ("dec %s+1", lbuf);
1915 AddCodeLine ("ldx %s+1", lbuf);
1917 AddCodeLine ("lda %s+1", lbuf);
1918 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1919 AddCodeLine ("sta %s+1", lbuf);
1920 AddCodeLine ("tax");
1921 AddCodeLine ("lda %s", lbuf);
1924 AddCodeLine ("eor #$FF");
1925 AddCodeLine ("adc %s", lbuf);
1926 AddCodeLine ("sta %s", lbuf);
1927 AddCodeLine ("txa");
1928 AddCodeLine ("eor #$FF");
1929 AddCodeLine ("adc %s+1", lbuf);
1930 AddCodeLine ("sta %s+1", lbuf);
1931 AddCodeLine ("tax");
1932 AddCodeLine ("lda %s", lbuf);
1937 if (flags & CF_CONST) {
1939 AddCodeLine ("ldy #<(%s)", lbuf);
1940 AddCodeLine ("sty ptr1");
1941 AddCodeLine ("ldy #>(%s)", lbuf);
1942 AddCodeLine ("lda #$%02X", (unsigned char)val);
1943 AddCodeLine ("jsr lsubeqa");
1945 g_getstatic (flags, label, offs);
1947 g_putstatic (flags, label, offs);
1950 AddCodeLine ("ldy #<(%s)", lbuf);
1951 AddCodeLine ("sty ptr1");
1952 AddCodeLine ("ldy #>(%s)", lbuf);
1953 AddCodeLine ("jsr lsubeq");
1964 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1965 /* Emit -= for a local variable */
1967 /* Calculate the true offset, check it, load it into Y */
1969 CheckLocalOffs (offs);
1971 /* Check the size and determine operation */
1972 switch (flags & CF_TYPE) {
1975 if (flags & CF_FORCECHAR) {
1977 AddCodeLine ("ldx #$00");
1978 AddCodeLine ("sec");
1979 if (flags & CF_CONST) {
1980 AddCodeLine ("lda (sp),y");
1981 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1983 AddCodeLine ("eor #$FF");
1984 AddCodeLine ("adc (sp),y");
1986 AddCodeLine ("sta (sp),y");
1987 if ((flags & CF_UNSIGNED) == 0) {
1988 unsigned L = GetLocalLabel();
1989 AddCodeLine ("bpl %s", LocalLabelName (L));
1990 AddCodeLine ("dex");
1998 if (flags & CF_CONST) {
1999 g_getimmed (flags, val, 0);
2002 AddCodeLine ("jsr subeqysp");
2006 if (flags & CF_CONST) {
2007 g_getimmed (flags, val, 0);
2010 AddCodeLine ("jsr lsubeqysp");
2020 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2021 /* Emit -= for the location with address in ax */
2023 /* If the offset is too large for a byte register, add the high byte
2024 * of the offset to the primary. Beware: We need a special correction
2025 * if the offset in the low byte will overflow in the operation.
2027 offs = MakeByteOffs (flags, offs);
2029 /* Check the size and determine operation */
2030 switch (flags & CF_TYPE) {
2033 AddCodeLine ("sta ptr1");
2034 AddCodeLine ("stx ptr1+1");
2035 AddCodeLine ("ldy #$%02X", offs);
2036 AddCodeLine ("ldx #$00");
2037 AddCodeLine ("lda (ptr1),y");
2038 AddCodeLine ("sec");
2039 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2040 AddCodeLine ("sta (ptr1),y");
2044 if (CodeSizeFactor >= 200) {
2045 /* Lots of code, use only if size is not important */
2046 AddCodeLine ("sta ptr1");
2047 AddCodeLine ("stx ptr1+1");
2048 AddCodeLine ("ldy #$%02X", offs);
2049 AddCodeLine ("lda (ptr1),y");
2050 AddCodeLine ("sec");
2051 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2052 AddCodeLine ("sta (ptr1),y");
2053 AddCodeLine ("pha");
2054 AddCodeLine ("iny");
2055 AddCodeLine ("lda (ptr1),y");
2056 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2057 AddCodeLine ("sta (ptr1),y");
2058 AddCodeLine ("tax");
2059 AddCodeLine ("pla");
2065 AddCodeLine ("jsr pushax"); /* Push the address */
2066 push (CF_PTR); /* Correct the internal sp */
2067 g_getind (flags, offs); /* Fetch the value */
2068 g_dec (flags, val); /* Increment value in primary */
2069 g_putind (flags, offs); /* Store the value back */
2079 /*****************************************************************************/
2080 /* Add a variable address to the value in ax */
2081 /*****************************************************************************/
2085 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2086 /* Add the address of a local variable to ax */
2090 /* Add the offset */
2093 /* We cannot address more then 256 bytes of locals anyway */
2094 L = GetLocalLabel();
2095 CheckLocalOffs (offs);
2096 AddCodeLine ("clc");
2097 AddCodeLine ("adc #$%02X", offs & 0xFF);
2098 /* Do also skip the CLC insn below */
2099 AddCodeLine ("bcc %s", LocalLabelName (L));
2100 AddCodeLine ("inx");
2103 /* Add the current stackpointer value */
2104 AddCodeLine ("clc");
2106 /* Label was used above */
2109 AddCodeLine ("adc sp");
2110 AddCodeLine ("tay");
2111 AddCodeLine ("txa");
2112 AddCodeLine ("adc sp+1");
2113 AddCodeLine ("tax");
2114 AddCodeLine ("tya");
2119 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2120 /* Add the address of a static variable to ax */
2122 /* Create the correct label name */
2123 const char* lbuf = GetLabelName (flags, label, offs);
2125 /* Add the address to the current ax value */
2126 AddCodeLine ("clc");
2127 AddCodeLine ("adc #<(%s)", lbuf);
2128 AddCodeLine ("tay");
2129 AddCodeLine ("txa");
2130 AddCodeLine ("adc #>(%s)", lbuf);
2131 AddCodeLine ("tax");
2132 AddCodeLine ("tya");
2137 /*****************************************************************************/
2139 /*****************************************************************************/
2143 void g_save (unsigned flags)
2144 /* Copy primary register to hold register. */
2146 /* Check the size and determine operation */
2147 switch (flags & CF_TYPE) {
2150 if (flags & CF_FORCECHAR) {
2151 AddCodeLine ("pha");
2157 AddCodeLine ("sta regsave");
2158 AddCodeLine ("stx regsave+1");
2162 AddCodeLine ("jsr saveeax");
2172 void g_restore (unsigned flags)
2173 /* Copy hold register to primary. */
2175 /* Check the size and determine operation */
2176 switch (flags & CF_TYPE) {
2179 if (flags & CF_FORCECHAR) {
2180 AddCodeLine ("pla");
2186 AddCodeLine ("lda regsave");
2187 AddCodeLine ("ldx regsave+1");
2191 AddCodeLine ("jsr resteax");
2201 void g_cmp (unsigned flags, unsigned long val)
2202 /* Immidiate compare. The primary register will not be changed, Z flag
2208 /* Check the size and determine operation */
2209 switch (flags & CF_TYPE) {
2212 if (flags & CF_FORCECHAR) {
2213 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2219 L = GetLocalLabel();
2220 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2221 AddCodeLine ("bne %s", LocalLabelName (L));
2222 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2227 Internal ("g_cmp: Long compares not implemented");
2237 static void oper (unsigned flags, unsigned long val, char** subs)
2238 /* Encode a binary operation. subs is a pointer to four groups of three
2240 * 0-2 --> Operate on ints
2241 * 3-5 --> Operate on unsigneds
2242 * 6-8 --> Operate on longs
2243 * 9-11 --> Operate on unsigned longs
2245 * The first subroutine names in each string group is used to encode an
2246 * operation with a zero constant, the second to encode an operation with
2247 * a 8 bit constant, and the third is used in all other cases.
2252 /* Determine the offset into the array */
2253 offs = (flags & CF_UNSIGNED)? 3 : 0;
2254 switch (flags & CF_TYPE) {
2267 /* Encode the operation */
2268 if (flags & CF_CONST) {
2269 /* Constant value given */
2270 if (val == 0 && subs [offs+0]) {
2271 /* Special case: constant with value zero */
2272 AddCodeLine ("jsr %s", subs [offs+0]);
2273 } else if (val < 0x100 && subs [offs+1]) {
2274 /* Special case: constant with high byte zero */
2275 ldaconst (val); /* Load low byte */
2276 AddCodeLine ("jsr %s", subs [offs+1]);
2278 /* Others: arbitrary constant value */
2279 g_getimmed (flags, val, 0); /* Load value */
2280 AddCodeLine ("jsr %s", subs [offs+2]);
2283 /* Value not constant (is already in (e)ax) */
2284 AddCodeLine ("jsr %s", subs [offs+2]);
2287 /* The operation will pop it's argument */
2293 void g_test (unsigned flags)
2294 /* Test the value in the primary and set the condition codes */
2296 switch (flags & CF_TYPE) {
2299 if (flags & CF_FORCECHAR) {
2300 AddCodeLine ("tax");
2306 AddCodeLine ("stx tmp1");
2307 AddCodeLine ("ora tmp1");
2311 if (flags & CF_UNSIGNED) {
2312 AddCodeLine ("jsr utsteax");
2314 AddCodeLine ("jsr tsteax");
2326 void g_push (unsigned flags, unsigned long val)
2327 /* Push the primary register or a constant value onto the stack */
2329 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2331 /* We have a constant 8 or 16 bit value */
2332 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2334 /* Handle as 8 bit value */
2336 AddCodeLine ("jsr pusha");
2340 /* Handle as 16 bit value */
2341 g_getimmed (flags, val, 0);
2342 AddCodeLine ("jsr pushax");
2347 /* Value is not 16 bit or not constant */
2348 if (flags & CF_CONST) {
2349 /* Constant 32 bit value, load into eax */
2350 g_getimmed (flags, val, 0);
2353 /* Push the primary register */
2354 switch (flags & CF_TYPE) {
2357 if (flags & CF_FORCECHAR) {
2358 /* Handle as char */
2359 AddCodeLine ("jsr pusha");
2364 AddCodeLine ("jsr pushax");
2368 AddCodeLine ("jsr pusheax");
2378 /* Adjust the stack offset */
2384 void g_swap (unsigned flags)
2385 /* Swap the primary register and the top of the stack. flags give the type
2386 * of *both* values (must have same size).
2389 switch (flags & CF_TYPE) {
2393 AddCodeLine ("jsr swapstk");
2397 AddCodeLine ("jsr swapestk");
2408 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2409 /* Call the specified subroutine name */
2411 if ((Flags & CF_FIXARGC) == 0) {
2412 /* Pass the argument count */
2415 AddCodeLine ("jsr _%s", Label);
2416 oursp += ArgSize; /* callee pops args */
2421 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2422 /* Call subroutine indirect */
2424 if ((Flags & CF_LOCAL) == 0) {
2425 /* Address is in a/x */
2426 if ((Flags & CF_FIXARGC) == 0) {
2427 /* Pass arg count */
2430 AddCodeLine ("jsr callax");
2432 /* The address is on stack, offset is on Val */
2434 CheckLocalOffs (Offs);
2435 AddCodeLine ("pha");
2436 AddCodeLine ("ldy #$%02X", Offs);
2437 AddCodeLine ("lda (sp),y");
2438 AddCodeLine ("sta jmpvec+1");
2439 AddCodeLine ("iny");
2440 AddCodeLine ("lda (sp),y");
2441 AddCodeLine ("sta jmpvec+2");
2442 AddCodeLine ("pla");
2443 AddCodeLine ("jsr jmpvec");
2446 /* Callee pops args */
2452 void g_jump (unsigned Label)
2453 /* Jump to specified internal label number */
2455 AddCodeLine ("jmp %s", LocalLabelName (Label));
2460 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2461 /* Jump to label if zero flag clear */
2463 AddCodeLine ("jne %s", LocalLabelName (label));
2468 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2469 /* Jump to label if zero flag set */
2471 AddCodeLine ("jeq %s", LocalLabelName (label));
2476 static void mod_internal (int k, char* verb1, char* verb2)
2479 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2483 AddCodeLine ("jsr %ssp", verb2);
2489 void g_space (int space)
2490 /* Create or drop space on the stack */
2493 mod_internal (-space, "inc", "addy");
2494 } else if (space > 0) {
2495 mod_internal (space, "dec", "suby");
2501 void g_cstackcheck (void)
2502 /* Check for a C stack overflow */
2504 AddCodeLine ("jsr cstkchk");
2509 void g_stackcheck (void)
2510 /* Check for a stack overflow */
2512 AddCodeLine ("jsr stkchk");
2517 void g_add (unsigned flags, unsigned long val)
2518 /* Primary = TOS + Primary */
2520 static char* ops [12] = {
2521 0, "tosadda0", "tosaddax",
2522 0, "tosadda0", "tosaddax",
2527 if (flags & CF_CONST) {
2528 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2529 g_push (flags & ~CF_CONST, 0);
2531 oper (flags, val, ops);
2536 void g_sub (unsigned flags, unsigned long val)
2537 /* Primary = TOS - Primary */
2539 static char* ops [12] = {
2540 0, "tossuba0", "tossubax",
2541 0, "tossuba0", "tossubax",
2546 if (flags & CF_CONST) {
2547 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2548 g_push (flags & ~CF_CONST, 0);
2550 oper (flags, val, ops);
2555 void g_rsub (unsigned flags, unsigned long val)
2556 /* Primary = Primary - TOS */
2558 static char* ops [12] = {
2559 0, "tosrsuba0", "tosrsubax",
2560 0, "tosrsuba0", "tosrsubax",
2564 oper (flags, val, ops);
2569 void g_mul (unsigned flags, unsigned long val)
2570 /* Primary = TOS * Primary */
2572 static char* ops [12] = {
2573 0, "tosmula0", "tosmulax",
2574 0, "tosumula0", "tosumulax",
2581 /* Do strength reduction if the value is constant and a power of two */
2582 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2583 /* Generate a shift instead */
2588 /* If the right hand side is const, the lhs is not on stack but still
2589 * in the primary register.
2591 if (flags & CF_CONST) {
2593 switch (flags & CF_TYPE) {
2596 if (flags & CF_FORCECHAR) {
2597 /* Handle some special cases */
2601 AddCodeLine ("sta tmp1");
2602 AddCodeLine ("asl a");
2603 AddCodeLine ("clc");
2604 AddCodeLine ("adc tmp1");
2608 AddCodeLine ("sta tmp1");
2609 AddCodeLine ("asl a");
2610 AddCodeLine ("asl a");
2611 AddCodeLine ("clc");
2612 AddCodeLine ("adc tmp1");
2616 AddCodeLine ("sta tmp1");
2617 AddCodeLine ("asl a");
2618 AddCodeLine ("clc");
2619 AddCodeLine ("adc tmp1");
2620 AddCodeLine ("asl a");
2624 AddCodeLine ("sta tmp1");
2625 AddCodeLine ("asl a");
2626 AddCodeLine ("asl a");
2627 AddCodeLine ("clc");
2628 AddCodeLine ("adc tmp1");
2629 AddCodeLine ("asl a");
2638 AddCodeLine ("jsr mulax3");
2641 AddCodeLine ("jsr mulax5");
2644 AddCodeLine ("jsr mulax6");
2647 AddCodeLine ("jsr mulax7");
2650 AddCodeLine ("jsr mulax9");
2653 AddCodeLine ("jsr mulax10");
2665 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2666 * into the normal, non-optimized stuff.
2668 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2669 g_push (flags & ~CF_CONST, 0);
2673 /* Use long way over the stack */
2674 oper (flags, val, ops);
2679 void g_div (unsigned flags, unsigned long val)
2680 /* Primary = TOS / Primary */
2682 static char* ops [12] = {
2683 0, "tosdiva0", "tosdivax",
2684 0, "tosudiva0", "tosudivax",
2689 /* 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 */
2695 /* Generate a division */
2696 if (flags & CF_CONST) {
2697 /* lhs is not on stack */
2698 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2699 g_push (flags & ~CF_CONST, 0);
2701 oper (flags, val, ops);
2707 void g_mod (unsigned flags, unsigned long val)
2708 /* Primary = TOS % Primary */
2710 static char* ops [12] = {
2711 0, "tosmoda0", "tosmodax",
2712 0, "tosumoda0", "tosumodax",
2718 /* Check if we can do some cost reduction */
2719 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2720 /* We can do that with an AND operation */
2721 g_and (flags, val - 1);
2723 /* Do it the hard way... */
2724 if (flags & CF_CONST) {
2725 /* lhs is not on stack */
2726 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2727 g_push (flags & ~CF_CONST, 0);
2729 oper (flags, val, ops);
2735 void g_or (unsigned flags, unsigned long val)
2736 /* Primary = TOS | Primary */
2738 static char* ops [12] = {
2739 0, "tosora0", "tosorax",
2740 0, "tosora0", "tosorax",
2745 /* If the right hand side is const, the lhs is not on stack but still
2746 * in the primary register.
2748 if (flags & CF_CONST) {
2750 switch (flags & CF_TYPE) {
2753 if (flags & CF_FORCECHAR) {
2754 if ((val & 0xFF) != 0xFF) {
2755 AddCodeLine ("ora #$%02X", (unsigned char)val);
2763 AddCodeLine ("ora #$%02X", (unsigned char)val);
2770 AddCodeLine ("ora #$%02X", (unsigned char)val);
2779 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2780 * into the normal, non-optimized stuff.
2782 g_push (flags & ~CF_CONST, 0);
2786 /* Use long way over the stack */
2787 oper (flags, val, ops);
2792 void g_xor (unsigned flags, unsigned long val)
2793 /* Primary = TOS ^ Primary */
2795 static char* ops [12] = {
2796 0, "tosxora0", "tosxorax",
2797 0, "tosxora0", "tosxorax",
2803 /* If the right hand side is const, the lhs is not on stack but still
2804 * in the primary register.
2806 if (flags & CF_CONST) {
2808 switch (flags & CF_TYPE) {
2811 if (flags & CF_FORCECHAR) {
2812 if ((val & 0xFF) != 0) {
2813 AddCodeLine ("eor #$%02X", (unsigned char)val);
2822 AddCodeLine ("eor #$%02X", (unsigned char)val);
2825 } else if ((val & 0xFF) == 0) {
2826 AddCodeLine ("pha");
2827 AddCodeLine ("txa");
2828 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2829 AddCodeLine ("tax");
2830 AddCodeLine ("pla");
2838 AddCodeLine ("eor #$%02X", (unsigned char)val);
2848 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2849 * into the normal, non-optimized stuff.
2851 g_push (flags & ~CF_CONST, 0);
2855 /* Use long way over the stack */
2856 oper (flags, val, ops);
2861 void g_and (unsigned flags, unsigned long val)
2862 /* Primary = TOS & Primary */
2864 static char* ops [12] = {
2865 0, "tosanda0", "tosandax",
2866 0, "tosanda0", "tosandax",
2871 /* If the right hand side is const, the lhs is not on stack but still
2872 * in the primary register.
2874 if (flags & CF_CONST) {
2876 switch (flags & CF_TYPE) {
2879 if (flags & CF_FORCECHAR) {
2880 AddCodeLine ("and #$%02X", (unsigned char)val);
2885 if ((val & 0xFFFF) != 0xFFFF) {
2890 } else if (val != 0xFF) {
2891 AddCodeLine ("and #$%02X", (unsigned char)val);
2893 } else if ((val & 0xFF00) == 0xFF00) {
2894 AddCodeLine ("and #$%02X", (unsigned char)val);
2895 } else if ((val & 0x00FF) == 0x0000) {
2896 AddCodeLine ("txa");
2897 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2898 AddCodeLine ("tax");
2901 AddCodeLine ("tay");
2902 AddCodeLine ("txa");
2903 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2904 AddCodeLine ("tax");
2905 AddCodeLine ("tya");
2906 if ((val & 0x00FF) != 0x00FF) {
2907 AddCodeLine ("and #$%02X", (unsigned char)val);
2916 AddCodeLine ("stx sreg+1");
2917 AddCodeLine ("stx sreg");
2918 if ((val & 0xFF) != 0xFF) {
2919 AddCodeLine ("and #$%02X", (unsigned char)val);
2922 } else if (val == 0xFF00) {
2924 AddCodeLine ("sta sreg+1");
2925 AddCodeLine ("sta sreg");
2934 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2935 * into the normal, non-optimized stuff.
2937 g_push (flags & ~CF_CONST, 0);
2941 /* Use long way over the stack */
2942 oper (flags, val, ops);
2947 void g_asr (unsigned flags, unsigned long val)
2948 /* Primary = TOS >> Primary */
2950 static char* ops [12] = {
2951 0, "tosasra0", "tosasrax",
2952 0, "tosshra0", "tosshrax",
2957 /* If the right hand side is const, the lhs is not on stack but still
2958 * in the primary register.
2960 if (flags & CF_CONST) {
2962 switch (flags & CF_TYPE) {
2966 if (val >= 8 && (flags & CF_UNSIGNED)) {
2967 AddCodeLine ("txa");
2974 } else if (val >= 1 && val <= 4) {
2975 if (flags & CF_UNSIGNED) {
2976 AddCodeLine ("jsr shrax%ld", val);
2978 AddCodeLine ("jsr asrax%ld", val);
2988 } else if (val >= 1 && val <= 4) {
2989 if (flags & CF_UNSIGNED) {
2990 AddCodeLine ("jsr shreax%ld", val);
2992 AddCodeLine ("jsr asreax%ld", val);
2995 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2996 AddCodeLine ("txa");
2997 AddCodeLine ("ldx sreg");
2998 AddCodeLine ("ldy sreg+1");
2999 AddCodeLine ("sty sreg");
3000 AddCodeLine ("ldy #$00");
3001 AddCodeLine ("sty sreg+1");
3003 } else if (val == 16) {
3004 AddCodeLine ("ldy #$00");
3005 AddCodeLine ("ldx sreg+1");
3006 if ((flags & CF_UNSIGNED) == 0) {
3007 unsigned L = GetLocalLabel();
3008 AddCodeLine ("bpl %s", LocalLabelName (L));
3009 AddCodeLine ("dey");
3012 AddCodeLine ("lda sreg");
3013 AddCodeLine ("sty sreg+1");
3014 AddCodeLine ("sty sreg");
3023 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3024 * into the normal, non-optimized stuff.
3026 g_push (flags & ~CF_CONST, 0);
3030 /* Use long way over the stack */
3031 oper (flags, val, ops);
3036 void g_asl (unsigned flags, unsigned long val)
3037 /* Primary = TOS << Primary */
3039 static char* ops [12] = {
3040 0, "tosasla0", "tosaslax",
3041 0, "tosshla0", "tosshlax",
3047 /* If the right hand side is const, the lhs is not on stack but still
3048 * in the primary register.
3050 if (flags & CF_CONST) {
3052 switch (flags & CF_TYPE) {
3057 AddCodeLine ("tax");
3058 AddCodeLine ("lda #$00");
3064 } else if (val >= 1 && val <= 4) {
3065 if (flags & CF_UNSIGNED) {
3066 AddCodeLine ("jsr shlax%ld", val);
3068 AddCodeLine ("jsr aslax%ld", val);
3078 } else if (val >= 1 && val <= 4) {
3079 if (flags & CF_UNSIGNED) {
3080 AddCodeLine ("jsr shleax%ld", val);
3082 AddCodeLine ("jsr asleax%ld", val);
3085 } else if (val == 8) {
3086 AddCodeLine ("ldy sreg");
3087 AddCodeLine ("sty sreg+1");
3088 AddCodeLine ("stx sreg");
3089 AddCodeLine ("tax");
3090 AddCodeLine ("lda #$00");
3092 } else if (val == 16) {
3093 AddCodeLine ("stx sreg+1");
3094 AddCodeLine ("sta sreg");
3095 AddCodeLine ("lda #$00");
3096 AddCodeLine ("tax");
3105 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3106 * into the normal, non-optimized stuff.
3108 g_push (flags & ~CF_CONST, 0);
3112 /* Use long way over the stack */
3113 oper (flags, val, ops);
3118 void g_neg (unsigned flags)
3119 /* Primary = -Primary */
3121 switch (flags & CF_TYPE) {
3125 AddCodeLine ("jsr negax");
3129 AddCodeLine ("jsr negeax");
3139 void g_bneg (unsigned flags)
3140 /* Primary = !Primary */
3142 switch (flags & CF_TYPE) {
3145 AddCodeLine ("jsr bnega");
3149 AddCodeLine ("jsr bnegax");
3153 AddCodeLine ("jsr bnegeax");
3163 void g_com (unsigned flags)
3164 /* Primary = ~Primary */
3166 switch (flags & CF_TYPE) {
3170 AddCodeLine ("jsr complax");
3174 AddCodeLine ("jsr compleax");
3184 void g_inc (unsigned flags, unsigned long val)
3185 /* Increment the primary register by a given number */
3187 /* Don't inc by zero */
3192 /* Generate code for the supported types */
3194 switch (flags & CF_TYPE) {
3197 if (flags & CF_FORCECHAR) {
3198 if (CPU == CPU_65C02 && val <= 2) {
3200 AddCodeLine ("ina");
3203 AddCodeLine ("clc");
3204 AddCodeLine ("adc #$%02X", (unsigned char)val);
3211 if (CPU == CPU_65C02 && val == 1) {
3212 unsigned L = GetLocalLabel();
3213 AddCodeLine ("ina");
3214 AddCodeLine ("bne %s", LocalLabelName (L));
3215 AddCodeLine ("inx");
3217 } else if (CodeSizeFactor < 200) {
3220 AddCodeLine ("jsr incax%lu", val);
3221 } else if (val <= 255) {
3223 AddCodeLine ("jsr incaxy");
3225 g_add (flags | CF_CONST, val);
3228 /* Inline the code */
3230 if ((val & 0xFF) != 0) {
3231 unsigned L = GetLocalLabel();
3232 AddCodeLine ("clc");
3233 AddCodeLine ("adc #$%02X", (unsigned char) val);
3234 AddCodeLine ("bcc %s", LocalLabelName (L));
3235 AddCodeLine ("inx");
3239 AddCodeLine ("inx");
3242 AddCodeLine ("inx");
3245 AddCodeLine ("inx");
3248 AddCodeLine ("clc");
3249 if ((val & 0xFF) != 0) {
3250 AddCodeLine ("adc #$%02X", (unsigned char) val);
3252 AddCodeLine ("pha");
3253 AddCodeLine ("txa");
3254 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3255 AddCodeLine ("tax");
3256 AddCodeLine ("pla");
3264 AddCodeLine ("jsr inceaxy");
3266 g_add (flags | CF_CONST, val);
3278 void g_dec (unsigned flags, unsigned long val)
3279 /* Decrement the primary register by a given number */
3281 /* Don't dec by zero */
3286 /* Generate code for the supported types */
3288 switch (flags & CF_TYPE) {
3291 if (flags & CF_FORCECHAR) {
3292 if (CPU == CPU_65C02 && val <= 2) {
3294 AddCodeLine ("dea");
3297 AddCodeLine ("sec");
3298 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3305 if (CodeSizeFactor < 200) {
3306 /* Use subroutines */
3308 AddCodeLine ("jsr decax%d", (int) val);
3309 } else if (val <= 255) {
3311 AddCodeLine ("jsr decaxy");
3313 g_sub (flags | CF_CONST, val);
3316 /* Inline the code */
3318 if ((val & 0xFF) != 0) {
3319 unsigned L = GetLocalLabel();
3320 AddCodeLine ("sec");
3321 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3322 AddCodeLine ("bcs %s", LocalLabelName (L));
3323 AddCodeLine ("dex");
3327 AddCodeLine ("dex");
3330 AddCodeLine ("dex");
3333 AddCodeLine ("sec");
3334 if ((val & 0xFF) != 0) {
3335 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3337 AddCodeLine ("pha");
3338 AddCodeLine ("txa");
3339 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3340 AddCodeLine ("tax");
3341 AddCodeLine ("pla");
3349 AddCodeLine ("jsr deceaxy");
3351 g_sub (flags | CF_CONST, val);
3364 * Following are the conditional operators. They compare the TOS against
3365 * the primary and put a literal 1 in the primary if the condition is
3366 * true, otherwise they clear the primary register
3371 void g_eq (unsigned flags, unsigned long val)
3372 /* Test for equal */
3374 static char* ops [12] = {
3375 "toseq00", "toseqa0", "toseqax",
3376 "toseq00", "toseqa0", "toseqax",
3383 /* If the right hand side is const, the lhs is not on stack but still
3384 * in the primary register.
3386 if (flags & CF_CONST) {
3388 switch (flags & CF_TYPE) {
3391 if (flags & CF_FORCECHAR) {
3392 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3393 AddCodeLine ("jsr booleq");
3399 L = GetLocalLabel();
3400 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3401 AddCodeLine ("bne %s", LocalLabelName (L));
3402 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3404 AddCodeLine ("jsr booleq");
3414 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3415 * into the normal, non-optimized stuff. Note: The standard stuff will
3416 * always work with ints.
3418 flags &= ~CF_FORCECHAR;
3419 g_push (flags & ~CF_CONST, 0);
3422 /* Use long way over the stack */
3423 oper (flags, val, ops);
3428 void g_ne (unsigned flags, unsigned long val)
3429 /* Test for not equal */
3431 static char* ops [12] = {
3432 "tosne00", "tosnea0", "tosneax",
3433 "tosne00", "tosnea0", "tosneax",
3440 /* If the right hand side is const, the lhs is not on stack but still
3441 * in the primary register.
3443 if (flags & CF_CONST) {
3445 switch (flags & CF_TYPE) {
3448 if (flags & CF_FORCECHAR) {
3449 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3450 AddCodeLine ("jsr boolne");
3456 L = GetLocalLabel();
3457 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3458 AddCodeLine ("bne %s", LocalLabelName (L));
3459 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3461 AddCodeLine ("jsr boolne");
3471 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3472 * into the normal, non-optimized stuff. Note: The standard stuff will
3473 * always work with ints.
3475 flags &= ~CF_FORCECHAR;
3476 g_push (flags & ~CF_CONST, 0);
3479 /* Use long way over the stack */
3480 oper (flags, val, ops);
3485 void g_lt (unsigned flags, unsigned long val)
3486 /* Test for less than */
3488 static char* ops [12] = {
3489 "toslt00", "toslta0", "tosltax",
3490 "tosult00", "tosulta0", "tosultax",
3495 /* If the right hand side is const, the lhs is not on stack but still
3496 * in the primary register.
3498 if (flags & CF_CONST) {
3500 /* Because the handling of the overflow flag is too complex for
3501 * inlining, we can handle only unsigned compares here.
3503 if (flags & CF_UNSIGNED) {
3505 /* Give a warning in some special cases */
3507 Warning ("Condition is never true");
3508 AddCodeLine ("jsr return0");
3512 /* Look at the type */
3513 switch (flags & CF_TYPE) {
3516 if (flags & CF_FORCECHAR) {
3517 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3518 AddCodeLine ("jsr boolult");
3524 /* If the low byte is zero, we must only test the high byte */
3525 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3526 if ((val & 0xFF) != 0) {
3527 unsigned L = GetLocalLabel();
3528 AddCodeLine ("bne %s", LocalLabelName (L));
3529 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3532 AddCodeLine ("jsr boolult");
3536 /* Do a subtraction */
3537 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3538 AddCodeLine ("txa");
3539 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3540 AddCodeLine ("lda sreg");
3541 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3542 AddCodeLine ("lda sreg+1");
3543 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3544 AddCodeLine ("jsr boolult");
3553 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3554 * into the normal, non-optimized stuff. Note: The standard stuff will
3555 * always work with ints.
3557 flags &= ~CF_FORCECHAR;
3558 g_push (flags & ~CF_CONST, 0);
3561 /* Use long way over the stack */
3562 oper (flags, val, ops);
3567 void g_le (unsigned flags, unsigned long val)
3568 /* Test for less than or equal to */
3570 static char* ops [12] = {
3571 "tosle00", "toslea0", "tosleax",
3572 "tosule00", "tosulea0", "tosuleax",
3578 /* If the right hand side is const, the lhs is not on stack but still
3579 * in the primary register.
3581 if (flags & CF_CONST) {
3583 /* Look at the type */
3584 switch (flags & CF_TYPE) {
3587 if (flags & CF_FORCECHAR) {
3588 if (flags & CF_UNSIGNED) {
3589 /* Unsigned compare */
3591 /* Use < instead of <= because the former gives
3592 * better code on the 6502 than the latter.
3594 g_lt (flags, val+1);
3597 Warning ("Condition is always true");
3598 AddCodeLine ("jsr return1");
3601 /* Signed compare */
3602 if ((long) val < 0x7F) {
3603 /* Use < instead of <= because the former gives
3604 * better code on the 6502 than the latter.
3606 g_lt (flags, val+1);
3609 Warning ("Condition is always true");
3610 AddCodeLine ("jsr return1");
3618 if (flags & CF_UNSIGNED) {
3619 /* Unsigned compare */
3621 /* Use < instead of <= because the former gives
3622 * better code on the 6502 than the latter.
3624 g_lt (flags, val+1);
3627 Warning ("Condition is always true");
3628 AddCodeLine ("jsr return1");
3631 /* Signed compare */
3632 if ((long) val < 0x7FFF) {
3633 g_lt (flags, val+1);
3636 Warning ("Condition is always true");
3637 AddCodeLine ("jsr return1");
3643 if (flags & CF_UNSIGNED) {
3644 /* Unsigned compare */
3645 if (val < 0xFFFFFFFF) {
3646 /* Use < instead of <= because the former gives
3647 * better code on the 6502 than the latter.
3649 g_lt (flags, val+1);
3652 Warning ("Condition is always true");
3653 AddCodeLine ("jsr return1");
3656 /* Signed compare */
3657 if ((long) val < 0x7FFFFFFF) {
3658 g_lt (flags, val+1);
3661 Warning ("Condition is always true");
3662 AddCodeLine ("jsr return1");
3671 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3672 * into the normal, non-optimized stuff. Note: The standard stuff will
3673 * always work with ints.
3675 flags &= ~CF_FORCECHAR;
3676 g_push (flags & ~CF_CONST, 0);
3679 /* Use long way over the stack */
3680 oper (flags, val, ops);
3685 void g_gt (unsigned flags, unsigned long val)
3686 /* Test for greater than */
3688 static char* ops [12] = {
3689 "tosgt00", "tosgta0", "tosgtax",
3690 "tosugt00", "tosugta0", "tosugtax",
3696 /* If the right hand side is const, the lhs is not on stack but still
3697 * in the primary register.
3699 if (flags & CF_CONST) {
3701 /* Look at the type */
3702 switch (flags & CF_TYPE) {
3705 if (flags & CF_FORCECHAR) {
3706 if (flags & CF_UNSIGNED) {
3708 /* If we have a compare > 0, we will replace it by
3709 * != 0 here, since both are identical but the
3710 * latter is easier to optimize.
3713 } else if (val < 0xFF) {
3714 /* Use >= instead of > because the former gives
3715 * better code on the 6502 than the latter.
3717 g_ge (flags, val+1);
3720 Warning ("Condition is never true");
3721 AddCodeLine ("jsr return0");
3724 if ((long) val < 0x7F) {
3725 /* Use >= instead of > because the former gives
3726 * better code on the 6502 than the latter.
3728 g_ge (flags, val+1);
3731 Warning ("Condition is never true");
3732 AddCodeLine ("jsr return0");
3740 if (flags & CF_UNSIGNED) {
3741 /* Unsigned compare */
3743 /* If we have a compare > 0, we will replace it by
3744 * != 0 here, since both are identical but the latter
3745 * is easier to optimize.
3748 } else if (val < 0xFFFF) {
3749 /* Use >= instead of > because the former gives better
3750 * code on the 6502 than the latter.
3752 g_ge (flags, val+1);
3755 Warning ("Condition is never true");
3756 AddCodeLine ("jsr return0");
3759 /* Signed compare */
3760 if ((long) val < 0x7FFF) {
3761 g_ge (flags, val+1);
3764 Warning ("Condition is never true");
3765 AddCodeLine ("jsr return0");
3771 if (flags & CF_UNSIGNED) {
3772 /* Unsigned compare */
3774 /* If we have a compare > 0, we will replace it by
3775 * != 0 here, since both are identical but the latter
3776 * is easier to optimize.
3779 } else if (val < 0xFFFFFFFF) {
3780 /* Use >= instead of > because the former gives better
3781 * code on the 6502 than the latter.
3783 g_ge (flags, val+1);
3786 Warning ("Condition is never true");
3787 AddCodeLine ("jsr return0");
3790 /* Signed compare */
3791 if ((long) val < 0x7FFFFFFF) {
3792 g_ge (flags, val+1);
3795 Warning ("Condition is never true");
3796 AddCodeLine ("jsr return0");
3805 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3806 * into the normal, non-optimized stuff. Note: The standard stuff will
3807 * always work with ints.
3809 flags &= ~CF_FORCECHAR;
3810 g_push (flags & ~CF_CONST, 0);
3813 /* Use long way over the stack */
3814 oper (flags, val, ops);
3819 void g_ge (unsigned flags, unsigned long val)
3820 /* Test for greater than or equal to */
3822 static char* ops [12] = {
3823 "tosge00", "tosgea0", "tosgeax",
3824 "tosuge00", "tosugea0", "tosugeax",
3830 /* If the right hand side is const, the lhs is not on stack but still
3831 * in the primary register.
3833 if (flags & CF_CONST) {
3835 /* Because the handling of the overflow flag is too complex for
3836 * inlining, we can handle only unsigned compares here.
3838 if (flags & CF_UNSIGNED) {
3840 /* Give a warning in some special cases */
3842 Warning ("Condition is always true");
3843 AddCodeLine ("jsr return1");
3847 /* Look at the type */
3848 switch (flags & CF_TYPE) {
3851 if (flags & CF_FORCECHAR) {
3852 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3853 AddCodeLine ("jsr booluge");
3859 /* If the low byte is zero, we must only test the high byte */
3860 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3861 if ((val & 0xFF) != 0) {
3862 unsigned L = GetLocalLabel();
3863 AddCodeLine ("bne %s", LocalLabelName (L));
3864 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3867 AddCodeLine ("jsr booluge");
3871 /* Do a subtraction */
3872 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3873 AddCodeLine ("txa");
3874 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3875 AddCodeLine ("lda sreg");
3876 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3877 AddCodeLine ("lda sreg+1");
3878 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3879 AddCodeLine ("jsr booluge");
3888 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3889 * into the normal, non-optimized stuff. Note: The standard stuff will
3890 * always work with ints.
3892 flags &= ~CF_FORCECHAR;
3893 g_push (flags & ~CF_CONST, 0);
3896 /* Use long way over the stack */
3897 oper (flags, val, ops);
3902 /*****************************************************************************/
3903 /* Allocating static storage */
3904 /*****************************************************************************/
3908 void g_res (unsigned n)
3909 /* Reserve static storage, n bytes */
3911 AddDataLine ("\t.res\t%u,$00", n);
3916 void g_defdata (unsigned flags, unsigned long val, long offs)
3917 /* Define data with the size given in flags */
3919 if (flags & CF_CONST) {
3921 /* Numeric constant */
3922 switch (flags & CF_TYPE) {
3925 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
3929 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
3933 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3944 /* Create the correct label name */
3945 const char* Label = GetLabelName (flags, val, offs);
3947 /* Labels are always 16 bit */
3948 AddDataLine ("\t.addr\t%s", Label);
3955 void g_defbytes (const void* Bytes, unsigned Count)
3956 /* Output a row of bytes as a constant */
3962 /* Cast the buffer pointer */
3963 const unsigned char* Data = (const unsigned char*) Bytes;
3965 /* Output the stuff */
3968 /* How many go into this line? */
3969 if ((Chunk = Count) > 16) {
3974 /* Output one line */
3975 strcpy (Buf, "\t.byte\t");
3978 B += sprintf (B, "$%02X", *Data++);
3984 /* Output the line */
3991 void g_zerobytes (unsigned Count)
3992 /* Output Count bytes of data initialized with zero */
3995 AddDataLine ("\t.res\t%u,$00", Count);
4001 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4002 /* Initialize a register variable from static initialization data */
4004 /* Register variables do always have less than 128 bytes */
4005 unsigned CodeLabel = GetLocalLabel ();
4007 g_defcodelabel (CodeLabel);
4008 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4009 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4010 AddCodeLine ("dex");
4011 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4016 void g_initauto (unsigned Label, unsigned Size)
4017 /* Initialize a local variable at stack offset zero from static data */
4019 unsigned CodeLabel = GetLocalLabel ();
4021 CheckLocalOffs (Size);
4024 g_defcodelabel (CodeLabel);
4025 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4026 AddCodeLine ("sta (sp),y");
4027 AddCodeLine ("dey");
4028 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4029 } else if (Size <= 256) {
4031 g_defcodelabel (CodeLabel);
4032 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4033 AddCodeLine ("sta (sp),y");
4034 AddCodeLine ("iny");
4035 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4036 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4042 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4043 /* Initialize a static local variable from static initialization data */
4046 unsigned CodeLabel = GetLocalLabel ();
4048 g_defcodelabel (CodeLabel);
4049 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4050 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4051 AddCodeLine ("dey");
4052 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4053 } else if (Size <= 256) {
4054 unsigned CodeLabel = GetLocalLabel ();
4056 g_defcodelabel (CodeLabel);
4057 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4058 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4059 AddCodeLine ("iny");
4060 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4061 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4063 /* Use the easy way here: memcpy */
4064 g_getimmed (CF_STATIC, VarLabel, 0);
4065 AddCodeLine ("jsr pushax");
4066 g_getimmed (CF_STATIC, InitLabel, 0);
4067 AddCodeLine ("jsr pushax");
4068 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4069 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4075 /*****************************************************************************/
4076 /* Switch statement */
4077 /*****************************************************************************/
4081 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4082 /* Generate code for a switch statement */
4084 unsigned NextLabel = 0;
4087 /* Setup registers and determine which compare insn to use */
4088 const char* Compare;
4091 Compare = "cmp #$%02X";
4094 Compare = "cpx #$%02X";
4097 AddCodeLine ("ldy sreg");
4098 Compare = "cpy #$%02X";
4101 AddCodeLine ("ldy sreg+1");
4102 Compare = "cpy #$%02X";
4105 Internal ("Invalid depth in g_switch: %u", Depth);
4108 /* Walk over all nodes */
4109 for (I = 0; I < CollCount (Nodes); ++I) {
4111 /* Get the next case node */
4112 CaseNode* N = CollAtUnchecked (Nodes, I);
4114 /* If we have a next label, define it */
4116 g_defcodelabel (NextLabel);
4120 /* Do the compare */
4121 AddCodeLine (Compare, CN_GetValue (N));
4123 /* If this is the last level, jump directly to the case code if found */
4126 /* Branch if equal */
4127 g_falsejump (0, CN_GetLabel (N));
4131 /* Determine the next label */
4132 if (I == CollCount (Nodes) - 1) {
4133 /* Last node means not found */
4134 g_truejump (0, DefaultLabel);
4136 /* Jump to the next check */
4137 NextLabel = GetLocalLabel ();
4138 g_truejump (0, NextLabel);
4141 /* Check the next level */
4142 g_switch (N->Nodes, DefaultLabel, Depth-1);
4147 /* If we go here, we haven't found the label */
4148 g_jump (DefaultLabel);
4153 /*****************************************************************************/
4154 /* User supplied assembler code */
4155 /*****************************************************************************/
4159 void g_asmcode (struct StrBuf* B)
4160 /* Output one line of assembler code. */
4162 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));
4167 /*****************************************************************************/
4168 /* Inlined known functions */
4169 /*****************************************************************************/
4173 void g_strlen (unsigned flags, unsigned long val, long offs)
4174 /* Inline the strlen() function */
4176 /* We need a label in both cases */
4177 unsigned label = GetLocalLabel ();
4179 /* Two different encodings */
4180 if (flags & CF_CONST) {
4182 /* The address of the string is constant. Create the correct label name */
4183 const char* lbuf = GetLabelName (flags, val, offs);
4185 /* Generate the strlen code */
4186 AddCodeLine ("ldy #$FF");
4187 g_defcodelabel (label);
4188 AddCodeLine ("iny");
4189 AddCodeLine ("lda %s,y", lbuf);
4190 AddCodeLine ("bne %s", LocalLabelName (label));
4191 AddCodeLine ("tax");
4192 AddCodeLine ("tya");
4196 /* Address not constant but in primary */
4197 if (CodeSizeFactor < 400) {
4198 /* This is too much code, so call strlen instead of inlining */
4199 AddCodeLine ("jsr _strlen");
4201 /* Inline the function */
4202 AddCodeLine ("sta ptr1");
4203 AddCodeLine ("stx ptr1+1");
4204 AddCodeLine ("ldy #$FF");
4205 g_defcodelabel (label);
4206 AddCodeLine ("iny");
4207 AddCodeLine ("lda (ptr1),y");
4208 AddCodeLine ("bne %s", LocalLabelName (label));
4209 AddCodeLine ("tax");
4210 AddCodeLine ("tya");