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.pc02");
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;
284 int pop (unsigned flags)
285 /* Pop an argument of the given size */
287 return oursp += sizeofarg (flags);
292 int push (unsigned flags)
293 /* Push an argument of the given size */
295 return oursp -= sizeofarg (flags);
300 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
301 /* The value in Offs is an offset to an address in a/x. Make sure, an object
302 * of the type given in Flags can be loaded or stored into this address by
303 * adding part of the offset to the address in ax, so that the remaining
304 * offset fits into an index register. Return the remaining offset.
307 /* If the offset is too large for a byte register, add the high byte
308 * of the offset to the primary. Beware: We need a special correction
309 * if the offset in the low byte will overflow in the operation.
311 unsigned O = Offs & ~0xFFU;
312 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
313 /* We need to add the low byte also */
317 /* Do the correction if we need one */
319 g_inc (CF_INT | CF_CONST, O);
323 /* Return the new offset */
329 /*****************************************************************************/
330 /* Functions handling local labels */
331 /*****************************************************************************/
335 void g_defcodelabel (unsigned label)
336 /* Define a local code label */
338 CS_AddLabel (CS->Code, LocalLabelName (label));
343 void g_defdatalabel (unsigned label)
344 /* Define a local data label */
346 AddDataLine ("%s:", LocalLabelName (label));
351 /*****************************************************************************/
352 /* Functions handling global labels */
353 /*****************************************************************************/
357 void g_defgloblabel (const char* Name)
358 /* Define a global label with the given name */
360 /* Global labels are always data labels */
361 AddDataLine ("_%s:", Name);
366 void g_defexport (const char* Name, int ZP)
367 /* Export the given label */
370 AddTextLine ("\t.exportzp\t_%s", Name);
372 AddTextLine ("\t.export\t\t_%s", Name);
378 void g_defimport (const char* Name, int ZP)
379 /* Import the given label */
382 AddTextLine ("\t.importzp\t_%s", Name);
384 AddTextLine ("\t.import\t\t_%s", Name);
390 void g_importmainargs (void)
391 /* Forced import of a special symbol that handles arguments to main */
393 AddTextLine ("\t.forceimport\tinitmainargs");
398 /*****************************************************************************/
399 /* Load functions for various registers */
400 /*****************************************************************************/
404 static void ldaconst (unsigned val)
405 /* Load a with a constant */
407 AddCodeLine ("lda #$%02X", val & 0xFF);
412 static void ldxconst (unsigned val)
413 /* Load x with a constant */
415 AddCodeLine ("ldx #$%02X", val & 0xFF);
420 static void ldyconst (unsigned val)
421 /* Load y with a constant */
423 AddCodeLine ("ldy #$%02X", val & 0xFF);
428 /*****************************************************************************/
429 /* Function entry and exit */
430 /*****************************************************************************/
434 /* Remember the argument size of a function. The variable is set by g_enter
435 * and used by g_leave. If the functions gets its argument size by the caller
436 * (variable param list or function without prototype), g_enter will set the
442 void g_enter (unsigned flags, unsigned argsize)
443 /* Function prologue */
445 if ((flags & CF_FIXARGC) != 0) {
446 /* Just remember the argument size for the leave */
450 AddCodeLine ("jsr enter");
457 /* Function epilogue */
459 /* How many bytes of locals do we have to drop? */
462 /* If we didn't have a variable argument list, don't call leave */
465 /* Drop stackframe if needed */
469 AddCodeLine ("jsr incsp%d", k);
473 AddCodeLine ("jsr addysp");
480 /* Nothing to drop */
481 AddCodeLine ("jsr leave");
483 /* We've a stack frame to drop */
485 AddCodeLine ("jsr leavey");
489 /* Add the final rts */
495 /*****************************************************************************/
496 /* Register variables */
497 /*****************************************************************************/
501 void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
502 /* Swap a register variable with a location on the stack */
504 /* Calculate the actual stack offset and check it */
506 CheckLocalOffs (StackOffs);
511 if (CodeSizeFactor < 165) {
512 ldyconst (StackOffs);
514 AddCodeLine ("jsr regswap1");
516 ldyconst (StackOffs);
517 AddCodeLine ("lda (sp),y");
518 AddCodeLine ("ldx regbank%+d", RegOffs);
519 AddCodeLine ("sta regbank%+d", RegOffs);
521 AddCodeLine ("sta (sp),y");
524 } else if (Bytes == 2) {
526 ldyconst (StackOffs);
528 AddCodeLine ("jsr regswap2");
532 ldyconst (StackOffs);
535 AddCodeLine ("jsr regswap");
541 void g_save_regvars (int RegOffs, unsigned Bytes)
542 /* Save register variables */
544 /* Don't loop for up to two bytes */
547 AddCodeLine ("lda regbank%+d", RegOffs);
548 AddCodeLine ("jsr pusha");
550 } else if (Bytes == 2) {
552 AddCodeLine ("lda regbank%+d", RegOffs);
553 AddCodeLine ("ldx regbank%+d", RegOffs+1);
554 AddCodeLine ("jsr pushax");
558 /* More than two bytes - loop */
559 unsigned Label = GetLocalLabel ();
561 ldyconst (Bytes - 1);
563 g_defcodelabel (Label);
564 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
565 AddCodeLine ("sta (sp),y");
568 AddCodeLine ("bne %s", LocalLabelName (Label));
572 /* We pushed stuff, correct the stack pointer */
578 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
579 /* Restore register variables */
581 /* Calculate the actual stack offset and check it */
583 CheckLocalOffs (StackOffs);
585 /* Don't loop for up to two bytes */
588 ldyconst (StackOffs);
589 AddCodeLine ("lda (sp),y");
590 AddCodeLine ("sta regbank%+d", RegOffs);
592 } else if (Bytes == 2) {
594 ldyconst (StackOffs);
595 AddCodeLine ("lda (sp),y");
596 AddCodeLine ("sta regbank%+d", RegOffs);
598 AddCodeLine ("lda (sp),y");
599 AddCodeLine ("sta regbank%+d", RegOffs+1);
603 /* More than two bytes - loop */
604 unsigned Label = GetLocalLabel ();
605 ldyconst (StackOffs+Bytes-1);
607 g_defcodelabel (Label);
608 AddCodeLine ("lda (sp),y");
609 AddCodeLine ("sta regbank%+d,x", RegOffs-1);
612 AddCodeLine ("bne %s", LocalLabelName (Label));
619 /*****************************************************************************/
620 /* Fetching memory cells */
621 /*****************************************************************************/
625 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
626 /* Load a constant into the primary register */
628 unsigned char B1, B2, B3, B4;
632 if ((Flags & CF_CONST) != 0) {
634 /* Numeric constant */
635 switch (Flags & CF_TYPE) {
638 if ((Flags & CF_FORCECHAR) != 0) {
644 ldxconst ((Val >> 8) & 0xFF);
645 ldaconst (Val & 0xFF);
649 /* Split the value into 4 bytes */
650 B1 = (unsigned char) (Val >> 0);
651 B2 = (unsigned char) (Val >> 8);
652 B3 = (unsigned char) (Val >> 16);
653 B4 = (unsigned char) (Val >> 24);
655 /* Remember which bytes are done */
659 AddCodeLine ("ldx #$%02X", B2);
662 AddCodeLine ("stx sreg");
666 AddCodeLine ("stx sreg+1");
669 if ((Done & 0x04) == 0 && B1 != B3) {
670 AddCodeLine ("lda #$%02X", B3);
671 AddCodeLine ("sta sreg");
674 if ((Done & 0x08) == 0 && B1 != B4) {
675 AddCodeLine ("lda #$%02X", B4);
676 AddCodeLine ("sta sreg+1");
679 AddCodeLine ("lda #$%02X", B1);
681 if ((Done & 0x04) == 0) {
683 AddCodeLine ("sta sreg");
685 if ((Done & 0x08) == 0) {
687 AddCodeLine ("sta sreg+1");
699 /* Some sort of label */
700 const char* Label = GetLabelName (Flags, Val, Offs);
702 /* Load the address into the primary */
703 AddCodeLine ("lda #<(%s)", Label);
704 AddCodeLine ("ldx #>(%s)", Label);
711 void g_getstatic (unsigned flags, unsigned long label, long offs)
712 /* Fetch an static memory cell into the primary register */
714 /* Create the correct label name */
715 const char* lbuf = GetLabelName (flags, label, offs);
717 /* Check the size and generate the correct load operation */
718 switch (flags & CF_TYPE) {
721 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
722 AddCodeLine ("lda %s", lbuf); /* load A from the label */
725 AddCodeLine ("lda %s", lbuf); /* load A from the label */
726 if (!(flags & CF_UNSIGNED)) {
727 /* Must sign extend */
728 unsigned L = GetLocalLabel ();
729 AddCodeLine ("bpl %s", LocalLabelName (L));
737 AddCodeLine ("lda %s", lbuf);
738 if (flags & CF_TEST) {
739 AddCodeLine ("ora %s+1", lbuf);
741 AddCodeLine ("ldx %s+1", lbuf);
746 if (flags & CF_TEST) {
747 AddCodeLine ("lda %s+3", lbuf);
748 AddCodeLine ("ora %s+2", lbuf);
749 AddCodeLine ("ora %s+1", lbuf);
750 AddCodeLine ("ora %s+0", lbuf);
752 AddCodeLine ("lda %s+3", lbuf);
753 AddCodeLine ("sta sreg+1");
754 AddCodeLine ("lda %s+2", lbuf);
755 AddCodeLine ("sta sreg");
756 AddCodeLine ("ldx %s+1", lbuf);
757 AddCodeLine ("lda %s", lbuf);
769 void g_getlocal (unsigned flags, int offs)
770 /* Fetch specified local object (local var). */
773 CheckLocalOffs (offs);
774 switch (flags & CF_TYPE) {
777 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
779 AddCodeLine ("lda (sp),y");
782 AddCodeLine ("ldx #$00");
783 AddCodeLine ("lda (sp),y");
784 if ((flags & CF_UNSIGNED) == 0) {
785 unsigned L = GetLocalLabel();
786 AddCodeLine ("bpl %s", LocalLabelName (L));
794 CheckLocalOffs (offs + 1);
795 if (flags & CF_TEST) {
797 AddCodeLine ("lda (sp),y");
799 AddCodeLine ("ora (sp),y");
802 AddCodeLine ("jsr ldaxysp");
808 AddCodeLine ("jsr ldeaxysp");
818 void g_getind (unsigned flags, unsigned offs)
819 /* Fetch the specified object type indirect through the primary register
820 * into the primary register
823 /* If the offset is greater than 255, add the part that is > 255 to
824 * the primary. This way we get an easy addition and use the low byte
827 offs = MakeByteOffs (flags, offs);
829 /* Handle the indirect fetch */
830 switch (flags & CF_TYPE) {
833 /* Character sized */
834 if (flags & CF_UNSIGNED) {
836 AddCodeLine ("jsr ldauidx");
839 AddCodeLine ("jsr ldaidx");
844 if (flags & CF_TEST) {
846 AddCodeLine ("sta ptr1");
847 AddCodeLine ("stx ptr1+1");
848 AddCodeLine ("lda (ptr1),y");
850 AddCodeLine ("ora (ptr1),y");
853 AddCodeLine ("jsr ldaxidx");
859 AddCodeLine ("jsr ldeaxidx");
860 if (flags & CF_TEST) {
861 AddCodeLine ("jsr tsteax");
873 void g_leasp (int offs)
874 /* Fetch the address of the specified symbol into the primary register */
876 /* Calculate the offset relative to sp */
879 /* For value 0 we do direct code */
881 AddCodeLine ("lda sp");
882 AddCodeLine ("ldx sp+1");
884 if (CodeSizeFactor < 300) {
885 ldaconst (offs); /* Load A with offset value */
886 AddCodeLine ("jsr leaasp"); /* Load effective address */
888 unsigned L = GetLocalLabel ();
889 if (CPU == CPU_65C02 && offs == 1) {
890 AddCodeLine ("lda sp");
891 AddCodeLine ("ldx sp+1");
893 AddCodeLine ("bne %s", LocalLabelName (L));
898 AddCodeLine ("ldx sp+1");
899 AddCodeLine ("adc sp");
900 AddCodeLine ("bcc %s", LocalLabelName (L));
910 void g_leavariadic (int Offs)
911 /* Fetch the address of a parameter in a variadic function into the primary
915 unsigned ArgSizeOffs;
917 /* Calculate the offset relative to sp */
920 /* Get the offset of the parameter which is stored at sp+0 on function
921 * entry and check if this offset is reachable with a byte offset.
924 ArgSizeOffs = -oursp;
925 CheckLocalOffs (ArgSizeOffs);
927 /* Get the size of all parameters. */
928 ldyconst (ArgSizeOffs);
929 AddCodeLine ("lda (sp),y");
931 /* Add the value of the stackpointer */
932 if (CodeSizeFactor > 250) {
933 unsigned L = GetLocalLabel();
934 AddCodeLine ("ldx sp+1");
936 AddCodeLine ("adc sp");
937 AddCodeLine ("bcc %s", LocalLabelName (L));
941 AddCodeLine ("jsr leaasp");
944 /* Add the offset to the primary */
946 g_inc (CF_INT | CF_CONST, Offs);
947 } else if (Offs < 0) {
948 g_dec (CF_INT | CF_CONST, -Offs);
954 /*****************************************************************************/
955 /* Store into memory */
956 /*****************************************************************************/
960 void g_putstatic (unsigned flags, unsigned long label, long offs)
961 /* Store the primary register into the specified static memory cell */
963 /* Create the correct label name */
964 const char* lbuf = GetLabelName (flags, label, offs);
966 /* Check the size and generate the correct store operation */
967 switch (flags & CF_TYPE) {
970 AddCodeLine ("sta %s", lbuf);
974 AddCodeLine ("sta %s", lbuf);
975 AddCodeLine ("stx %s+1", lbuf);
979 AddCodeLine ("sta %s", lbuf);
980 AddCodeLine ("stx %s+1", lbuf);
981 AddCodeLine ("ldy sreg");
982 AddCodeLine ("sty %s+2", lbuf);
983 AddCodeLine ("ldy sreg+1");
984 AddCodeLine ("sty %s+3", lbuf);
995 void g_putlocal (unsigned Flags, int Offs, long Val)
996 /* Put data into local object. */
999 CheckLocalOffs (Offs);
1000 switch (Flags & CF_TYPE) {
1003 if (Flags & CF_CONST) {
1004 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1007 AddCodeLine ("sta (sp),y");
1011 if (Flags & CF_CONST) {
1013 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1014 AddCodeLine ("sta (sp),y");
1015 if ((Flags & CF_NOKEEP) == 0) {
1016 /* Place high byte into X */
1017 AddCodeLine ("tax");
1019 if ((Val & 0xFF) == Offs+1) {
1020 /* The value we need is already in Y */
1021 AddCodeLine ("tya");
1022 AddCodeLine ("dey");
1024 AddCodeLine ("dey");
1025 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1027 AddCodeLine ("sta (sp),y");
1029 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
1031 AddCodeLine ("jsr staxysp");
1034 AddCodeLine ("sta (sp),y");
1035 AddCodeLine ("iny");
1036 AddCodeLine ("txa");
1037 AddCodeLine ("sta (sp),y");
1043 if (Flags & CF_CONST) {
1044 g_getimmed (Flags, Val, 0);
1047 AddCodeLine ("jsr steaxysp");
1058 void g_putind (unsigned Flags, unsigned Offs)
1059 /* Store the specified object type in the primary register at the address
1060 * on the top of the stack
1063 /* We can handle offsets below $100 directly, larger offsets must be added
1064 * to the address. Since a/x is in use, best code is achieved by adding
1065 * just the high byte. Be sure to check if the low byte will overflow while
1068 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1070 /* Overflow - we need to add the low byte also */
1071 AddCodeLine ("ldy #$00");
1072 AddCodeLine ("clc");
1073 AddCodeLine ("pha");
1074 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1075 AddCodeLine ("adc (sp),y");
1076 AddCodeLine ("sta (sp),y");
1077 AddCodeLine ("iny");
1078 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1079 AddCodeLine ("adc (sp),y");
1080 AddCodeLine ("sta (sp),y");
1081 AddCodeLine ("pla");
1083 /* Complete address is on stack, new offset is zero */
1086 } else if ((Offs & 0xFF00) != 0) {
1088 /* We can just add the high byte */
1089 AddCodeLine ("ldy #$01");
1090 AddCodeLine ("clc");
1091 AddCodeLine ("pha");
1092 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1093 AddCodeLine ("adc (sp),y");
1094 AddCodeLine ("sta (sp),y");
1095 AddCodeLine ("pla");
1097 /* Offset is now just the low byte */
1101 /* Check the size and determine operation */
1102 switch (Flags & CF_TYPE) {
1106 AddCodeLine ("jsr staspidx");
1111 AddCodeLine ("jsr staxspidx");
1116 AddCodeLine ("jsr steaxspidx");
1124 /* Pop the argument which is always a pointer */
1130 /*****************************************************************************/
1131 /* type conversion and similiar stuff */
1132 /*****************************************************************************/
1136 void g_toslong (unsigned flags)
1137 /* Make sure, the value on TOS is a long. Convert if necessary */
1139 switch (flags & CF_TYPE) {
1143 if (flags & CF_UNSIGNED) {
1144 AddCodeLine ("jsr tosulong");
1146 AddCodeLine ("jsr toslong");
1161 void g_tosint (unsigned flags)
1162 /* Make sure, the value on TOS is an int. Convert if necessary */
1164 switch (flags & CF_TYPE) {
1171 AddCodeLine ("jsr tosint");
1182 void g_regint (unsigned Flags)
1183 /* Make sure, the value in the primary register an int. Convert if necessary */
1187 switch (Flags & CF_TYPE) {
1190 if (Flags & CF_FORCECHAR) {
1191 /* Conversion is from char */
1192 if (Flags & CF_UNSIGNED) {
1193 AddCodeLine ("ldx #$00");
1195 L = GetLocalLabel();
1196 AddCodeLine ("ldx #$00");
1197 AddCodeLine ("cmp #$80");
1198 AddCodeLine ("bcc %s", LocalLabelName (L));
1199 AddCodeLine ("dex");
1216 void g_reglong (unsigned Flags)
1217 /* Make sure, the value in the primary register a long. Convert if necessary */
1221 switch (Flags & CF_TYPE) {
1224 if (Flags & CF_FORCECHAR) {
1225 /* Conversion is from char */
1226 if (Flags & CF_UNSIGNED) {
1227 if (CodeSizeFactor >= 200) {
1228 AddCodeLine ("ldx #$00");
1229 AddCodeLine ("stx sreg");
1230 AddCodeLine ("stx sreg+1");
1232 AddCodeLine ("jsr aulong");
1235 if (CodeSizeFactor >= 366) {
1236 L = GetLocalLabel();
1237 AddCodeLine ("ldx #$00");
1238 AddCodeLine ("cmp #$80");
1239 AddCodeLine ("bcc %s", LocalLabelName (L));
1240 AddCodeLine ("dex");
1242 AddCodeLine ("stx sreg");
1243 AddCodeLine ("stx sreg+1");
1245 AddCodeLine ("jsr along");
1252 if (Flags & CF_UNSIGNED) {
1253 if (CodeSizeFactor >= 200) {
1255 AddCodeLine ("sty sreg");
1256 AddCodeLine ("sty sreg+1");
1258 AddCodeLine ("jsr axulong");
1261 AddCodeLine ("jsr axlong");
1275 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1276 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1277 * value, that corresponds to the value on TOS, rhs corresponds to the value
1278 * in (e)ax. The return value is the the flags value for the resulting type.
1281 unsigned ltype, rtype;
1284 /* Get the type spec from the flags */
1285 ltype = lhs & CF_TYPE;
1286 rtype = rhs & CF_TYPE;
1288 /* Check if a conversion is needed */
1289 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1290 /* We must promote the primary register to long */
1292 /* Get the new rhs type */
1293 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1295 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1296 /* We must promote the lhs to long */
1302 /* Get the new rhs type */
1303 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1307 /* Determine the result type for the operation:
1308 * - The result is const if both operands are const.
1309 * - The result is unsigned if one of the operands is unsigned.
1310 * - The result is long if one of the operands is long.
1311 * - Otherwise the result is int sized.
1313 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1314 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1315 if (rtype == CF_LONG || ltype == CF_LONG) {
1325 unsigned g_typecast (unsigned lhs, unsigned rhs)
1326 /* Cast the value in the primary register to the operand size that is flagged
1327 * by the lhs value. Return the result value.
1330 unsigned ltype, rtype;
1332 /* Get the type spec from the flags */
1333 ltype = lhs & CF_TYPE;
1334 rtype = rhs & CF_TYPE;
1336 /* Check if a conversion is needed */
1337 if ((rhs & CF_CONST) == 0) {
1338 if (ltype == CF_LONG && rtype != CF_LONG) {
1339 /* We must promote the primary register to long */
1341 } else if (ltype == CF_INT && rtype != CF_INT) {
1342 /* We must promote the primary register to int */
1347 /* Do not need any other action. If the left type is int, and the primary
1348 * register is long, it will be automagically truncated. If the right hand
1349 * side is const, it is not located in the primary register and handled by
1350 * the expression parser code.
1353 /* Result is const if the right hand side was const */
1354 lhs |= (rhs & CF_CONST);
1356 /* The resulting type is that of the left hand side (that's why you called
1364 void g_scale (unsigned flags, long val)
1365 /* Scale the value in the primary register by the given value. If val is positive,
1366 * scale up, is val is negative, scale down. This function is used to scale
1367 * the operands or results of pointer arithmetic by the size of the type, the
1368 * pointer points to.
1373 /* Value may not be zero */
1375 Internal ("Data type has no size");
1376 } else if (val > 0) {
1379 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1381 /* Factor is 2, 4, 8 and 16, use special function */
1382 switch (flags & CF_TYPE) {
1385 if (flags & CF_FORCECHAR) {
1387 AddCodeLine ("asl a");
1394 if (CodeSizeFactor >= (p2+1)*130U) {
1395 AddCodeLine ("stx tmp1");
1397 AddCodeLine ("asl a");
1398 AddCodeLine ("rol tmp1");
1400 AddCodeLine ("ldx tmp1");
1402 if (flags & CF_UNSIGNED) {
1403 AddCodeLine ("jsr shlax%d", p2);
1405 AddCodeLine ("jsr aslax%d", p2);
1411 if (flags & CF_UNSIGNED) {
1412 AddCodeLine ("jsr shleax%d", p2);
1414 AddCodeLine ("jsr asleax%d", p2);
1423 } else if (val != 1) {
1425 /* Use a multiplication instead */
1426 g_mul (flags | CF_CONST, val);
1434 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1436 /* Factor is 2, 4, 8 and 16 use special function */
1437 switch (flags & CF_TYPE) {
1440 if (flags & CF_FORCECHAR) {
1441 if (flags & CF_UNSIGNED) {
1443 AddCodeLine ("lsr a");
1446 } else if (p2 <= 2) {
1447 AddCodeLine ("cmp #$80");
1448 AddCodeLine ("ror a");
1455 if (flags & CF_UNSIGNED) {
1456 if (CodeSizeFactor >= (p2+1)*130U) {
1457 AddCodeLine ("stx tmp1");
1459 AddCodeLine ("lsr tmp1");
1460 AddCodeLine ("ror a");
1462 AddCodeLine ("ldx tmp1");
1464 AddCodeLine ("jsr lsrax%d", p2);
1467 if (CodeSizeFactor >= (p2+1)*150U) {
1468 AddCodeLine ("stx tmp1");
1470 AddCodeLine ("cpx #$80");
1471 AddCodeLine ("ror tmp1");
1472 AddCodeLine ("ror a");
1474 AddCodeLine ("ldx tmp1");
1476 AddCodeLine ("jsr asrax%d", p2);
1482 if (flags & CF_UNSIGNED) {
1483 AddCodeLine ("jsr lsreax%d", p2);
1485 AddCodeLine ("jsr asreax%d", p2);
1494 } else if (val != 1) {
1496 /* Use a division instead */
1497 g_div (flags | CF_CONST, val);
1505 /*****************************************************************************/
1506 /* Adds and subs of variables fix a fixed address */
1507 /*****************************************************************************/
1511 void g_addlocal (unsigned flags, int offs)
1512 /* Add a local variable to ax */
1516 /* Correct the offset and check it */
1518 CheckLocalOffs (offs);
1520 switch (flags & CF_TYPE) {
1523 L = GetLocalLabel();
1524 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1525 AddCodeLine ("clc");
1526 AddCodeLine ("adc (sp),y");
1527 AddCodeLine ("bcc %s", LocalLabelName (L));
1528 AddCodeLine ("inx");
1533 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1534 AddCodeLine ("clc");
1535 AddCodeLine ("adc (sp),y");
1536 AddCodeLine ("pha");
1537 AddCodeLine ("txa");
1538 AddCodeLine ("iny");
1539 AddCodeLine ("adc (sp),y");
1540 AddCodeLine ("tax");
1541 AddCodeLine ("pla");
1545 /* Do it the old way */
1547 g_getlocal (flags, offs);
1559 void g_addstatic (unsigned flags, unsigned long label, long offs)
1560 /* Add a static variable to ax */
1564 /* Create the correct label name */
1565 const char* lbuf = GetLabelName (flags, label, offs);
1567 switch (flags & CF_TYPE) {
1570 L = GetLocalLabel();
1571 AddCodeLine ("clc");
1572 AddCodeLine ("adc %s", lbuf);
1573 AddCodeLine ("bcc %s", LocalLabelName (L));
1574 AddCodeLine ("inx");
1579 AddCodeLine ("clc");
1580 AddCodeLine ("adc %s", lbuf);
1581 AddCodeLine ("tay");
1582 AddCodeLine ("txa");
1583 AddCodeLine ("adc %s+1", lbuf);
1584 AddCodeLine ("tax");
1585 AddCodeLine ("tya");
1589 /* Do it the old way */
1591 g_getstatic (flags, label, offs);
1603 /*****************************************************************************/
1604 /* Special op= functions */
1605 /*****************************************************************************/
1609 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1611 /* Emit += for a static variable */
1613 /* Create the correct label name */
1614 const char* lbuf = GetLabelName (flags, label, offs);
1616 /* Check the size and determine operation */
1617 switch (flags & CF_TYPE) {
1620 if (flags & CF_FORCECHAR) {
1621 AddCodeLine ("ldx #$00");
1622 if (flags & CF_CONST) {
1624 AddCodeLine ("inc %s", lbuf);
1625 AddCodeLine ("lda %s", lbuf);
1627 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1628 AddCodeLine ("clc");
1629 AddCodeLine ("adc %s", lbuf);
1630 AddCodeLine ("sta %s", lbuf);
1633 AddCodeLine ("clc");
1634 AddCodeLine ("adc %s", lbuf);
1635 AddCodeLine ("sta %s", lbuf);
1637 if ((flags & CF_UNSIGNED) == 0) {
1638 unsigned L = GetLocalLabel();
1639 AddCodeLine ("bpl %s", LocalLabelName (L));
1640 AddCodeLine ("dex");
1648 if (flags & CF_CONST) {
1650 unsigned L = GetLocalLabel ();
1651 AddCodeLine ("inc %s", lbuf);
1652 AddCodeLine ("bne %s", LocalLabelName (L));
1653 AddCodeLine ("inc %s+1", lbuf);
1655 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1656 AddCodeLine ("ldx %s+1", lbuf);
1658 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1659 AddCodeLine ("clc");
1660 AddCodeLine ("adc %s", lbuf);
1661 AddCodeLine ("sta %s", lbuf);
1663 unsigned L = GetLocalLabel ();
1664 AddCodeLine ("bcc %s", LocalLabelName (L));
1665 AddCodeLine ("inc %s+1", lbuf);
1667 AddCodeLine ("ldx %s+1", lbuf);
1669 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1670 AddCodeLine ("adc %s+1", lbuf);
1671 AddCodeLine ("sta %s+1", lbuf);
1672 AddCodeLine ("tax");
1673 AddCodeLine ("lda %s", lbuf);
1677 AddCodeLine ("clc");
1678 AddCodeLine ("adc %s", lbuf);
1679 AddCodeLine ("sta %s", lbuf);
1680 AddCodeLine ("txa");
1681 AddCodeLine ("adc %s+1", lbuf);
1682 AddCodeLine ("sta %s+1", lbuf);
1683 AddCodeLine ("tax");
1684 AddCodeLine ("lda %s", lbuf);
1689 if (flags & CF_CONST) {
1691 AddCodeLine ("ldy #<(%s)", lbuf);
1692 AddCodeLine ("sty ptr1");
1693 AddCodeLine ("ldy #>(%s)", lbuf);
1695 AddCodeLine ("jsr laddeq1");
1697 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1698 AddCodeLine ("jsr laddeqa");
1701 g_getstatic (flags, label, offs);
1703 g_putstatic (flags, label, offs);
1706 AddCodeLine ("ldy #<(%s)", lbuf);
1707 AddCodeLine ("sty ptr1");
1708 AddCodeLine ("ldy #>(%s)", lbuf);
1709 AddCodeLine ("jsr laddeq");
1720 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1721 /* Emit += for a local variable */
1723 /* Calculate the true offset, check it, load it into Y */
1725 CheckLocalOffs (offs);
1727 /* Check the size and determine operation */
1728 switch (flags & CF_TYPE) {
1731 if (flags & CF_FORCECHAR) {
1733 AddCodeLine ("ldx #$00");
1734 if (flags & CF_CONST) {
1735 AddCodeLine ("clc");
1736 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1737 AddCodeLine ("adc (sp),y");
1738 AddCodeLine ("sta (sp),y");
1740 AddCodeLine ("clc");
1741 AddCodeLine ("adc (sp),y");
1742 AddCodeLine ("sta (sp),y");
1744 if ((flags & CF_UNSIGNED) == 0) {
1745 unsigned L = GetLocalLabel();
1746 AddCodeLine ("bpl %s", LocalLabelName (L));
1747 AddCodeLine ("dex");
1756 if (flags & CF_CONST) {
1757 if (CodeSizeFactor >= 400) {
1758 AddCodeLine ("clc");
1759 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1760 AddCodeLine ("adc (sp),y");
1761 AddCodeLine ("sta (sp),y");
1762 AddCodeLine ("iny");
1763 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1764 AddCodeLine ("adc (sp),y");
1765 AddCodeLine ("sta (sp),y");
1766 AddCodeLine ("tax");
1767 AddCodeLine ("dey");
1768 AddCodeLine ("lda (sp),y");
1770 g_getimmed (flags, val, 0);
1771 AddCodeLine ("jsr addeqysp");
1774 AddCodeLine ("jsr addeqysp");
1779 if (flags & CF_CONST) {
1780 g_getimmed (flags, val, 0);
1783 AddCodeLine ("jsr laddeqysp");
1793 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1794 /* Emit += for the location with address in ax */
1796 /* If the offset is too large for a byte register, add the high byte
1797 * of the offset to the primary. Beware: We need a special correction
1798 * if the offset in the low byte will overflow in the operation.
1800 offs = MakeByteOffs (flags, offs);
1802 /* Check the size and determine operation */
1803 switch (flags & CF_TYPE) {
1806 AddCodeLine ("sta ptr1");
1807 AddCodeLine ("stx ptr1+1");
1808 AddCodeLine ("ldy #$%02X", offs);
1809 AddCodeLine ("ldx #$00");
1810 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1811 AddCodeLine ("clc");
1812 AddCodeLine ("adc (ptr1),y");
1813 AddCodeLine ("sta (ptr1),y");
1817 if (CodeSizeFactor >= 200) {
1818 /* Lots of code, use only if size is not important */
1819 AddCodeLine ("sta ptr1");
1820 AddCodeLine ("stx ptr1+1");
1821 AddCodeLine ("ldy #$%02X", offs);
1822 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1823 AddCodeLine ("clc");
1824 AddCodeLine ("adc (ptr1),y");
1825 AddCodeLine ("sta (ptr1),y");
1826 AddCodeLine ("pha");
1827 AddCodeLine ("iny");
1828 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1829 AddCodeLine ("adc (ptr1),y");
1830 AddCodeLine ("sta (ptr1),y");
1831 AddCodeLine ("tax");
1832 AddCodeLine ("pla");
1838 AddCodeLine ("jsr pushax"); /* Push the address */
1839 push (CF_PTR); /* Correct the internal sp */
1840 g_getind (flags, offs); /* Fetch the value */
1841 g_inc (flags, val); /* Increment value in primary */
1842 g_putind (flags, offs); /* Store the value back */
1852 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1854 /* Emit -= for a static variable */
1856 /* Create the correct label name */
1857 const char* lbuf = GetLabelName (flags, label, offs);
1859 /* Check the size and determine operation */
1860 switch (flags & CF_TYPE) {
1863 if (flags & CF_FORCECHAR) {
1864 AddCodeLine ("ldx #$00");
1865 if (flags & CF_CONST) {
1867 AddCodeLine ("dec %s", lbuf);
1868 AddCodeLine ("lda %s", lbuf);
1870 AddCodeLine ("sec");
1871 AddCodeLine ("lda %s", lbuf);
1872 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1873 AddCodeLine ("sta %s", lbuf);
1876 AddCodeLine ("eor #$FF");
1877 AddCodeLine ("sec");
1878 AddCodeLine ("adc %s", lbuf);
1879 AddCodeLine ("sta %s", lbuf);
1881 if ((flags & CF_UNSIGNED) == 0) {
1882 unsigned L = GetLocalLabel();
1883 AddCodeLine ("bpl %s", LocalLabelName (L));
1884 AddCodeLine ("dex");
1892 AddCodeLine ("sec");
1893 if (flags & CF_CONST) {
1894 AddCodeLine ("lda %s", lbuf);
1895 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1896 AddCodeLine ("sta %s", lbuf);
1898 unsigned L = GetLocalLabel ();
1899 AddCodeLine ("bcs %s", LocalLabelName (L));
1900 AddCodeLine ("dec %s+1", lbuf);
1902 AddCodeLine ("ldx %s+1", lbuf);
1904 AddCodeLine ("lda %s+1", lbuf);
1905 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1906 AddCodeLine ("sta %s+1", lbuf);
1907 AddCodeLine ("tax");
1908 AddCodeLine ("lda %s", lbuf);
1911 AddCodeLine ("eor #$FF");
1912 AddCodeLine ("adc %s", lbuf);
1913 AddCodeLine ("sta %s", lbuf);
1914 AddCodeLine ("txa");
1915 AddCodeLine ("eor #$FF");
1916 AddCodeLine ("adc %s+1", lbuf);
1917 AddCodeLine ("sta %s+1", lbuf);
1918 AddCodeLine ("tax");
1919 AddCodeLine ("lda %s", lbuf);
1924 if (flags & CF_CONST) {
1926 AddCodeLine ("ldy #<(%s)", lbuf);
1927 AddCodeLine ("sty ptr1");
1928 AddCodeLine ("ldy #>(%s)", lbuf);
1929 AddCodeLine ("lda #$%02X", (unsigned char)val);
1930 AddCodeLine ("jsr lsubeqa");
1932 g_getstatic (flags, label, offs);
1934 g_putstatic (flags, label, offs);
1937 AddCodeLine ("ldy #<(%s)", lbuf);
1938 AddCodeLine ("sty ptr1");
1939 AddCodeLine ("ldy #>(%s)", lbuf);
1940 AddCodeLine ("jsr lsubeq");
1951 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1952 /* Emit -= for a local variable */
1954 /* Calculate the true offset, check it, load it into Y */
1956 CheckLocalOffs (offs);
1958 /* Check the size and determine operation */
1959 switch (flags & CF_TYPE) {
1962 if (flags & CF_FORCECHAR) {
1964 AddCodeLine ("ldx #$00");
1965 AddCodeLine ("sec");
1966 if (flags & CF_CONST) {
1967 AddCodeLine ("lda (sp),y");
1968 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1970 AddCodeLine ("eor #$FF");
1971 AddCodeLine ("adc (sp),y");
1973 AddCodeLine ("sta (sp),y");
1974 if ((flags & CF_UNSIGNED) == 0) {
1975 unsigned L = GetLocalLabel();
1976 AddCodeLine ("bpl %s", LocalLabelName (L));
1977 AddCodeLine ("dex");
1985 if (flags & CF_CONST) {
1986 g_getimmed (flags, val, 0);
1989 AddCodeLine ("jsr subeqysp");
1993 if (flags & CF_CONST) {
1994 g_getimmed (flags, val, 0);
1997 AddCodeLine ("jsr lsubeqysp");
2007 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2008 /* Emit -= for the location with address in ax */
2010 /* If the offset is too large for a byte register, add the high byte
2011 * of the offset to the primary. Beware: We need a special correction
2012 * if the offset in the low byte will overflow in the operation.
2014 offs = MakeByteOffs (flags, offs);
2016 /* Check the size and determine operation */
2017 switch (flags & CF_TYPE) {
2020 AddCodeLine ("sta ptr1");
2021 AddCodeLine ("stx ptr1+1");
2022 AddCodeLine ("ldy #$%02X", offs);
2023 AddCodeLine ("ldx #$00");
2024 AddCodeLine ("lda (ptr1),y");
2025 AddCodeLine ("sec");
2026 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2027 AddCodeLine ("sta (ptr1),y");
2031 if (CodeSizeFactor >= 200) {
2032 /* Lots of code, use only if size is not important */
2033 AddCodeLine ("sta ptr1");
2034 AddCodeLine ("stx ptr1+1");
2035 AddCodeLine ("ldy #$%02X", offs);
2036 AddCodeLine ("lda (ptr1),y");
2037 AddCodeLine ("sec");
2038 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2039 AddCodeLine ("sta (ptr1),y");
2040 AddCodeLine ("pha");
2041 AddCodeLine ("iny");
2042 AddCodeLine ("lda (ptr1),y");
2043 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2044 AddCodeLine ("sta (ptr1),y");
2045 AddCodeLine ("tax");
2046 AddCodeLine ("pla");
2052 AddCodeLine ("jsr pushax"); /* Push the address */
2053 push (CF_PTR); /* Correct the internal sp */
2054 g_getind (flags, offs); /* Fetch the value */
2055 g_dec (flags, val); /* Increment value in primary */
2056 g_putind (flags, offs); /* Store the value back */
2066 /*****************************************************************************/
2067 /* Add a variable address to the value in ax */
2068 /*****************************************************************************/
2072 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2073 /* Add the address of a local variable to ax */
2077 /* Add the offset */
2080 /* We cannot address more then 256 bytes of locals anyway */
2081 L = GetLocalLabel();
2082 CheckLocalOffs (offs);
2083 AddCodeLine ("clc");
2084 AddCodeLine ("adc #$%02X", offs & 0xFF);
2085 /* Do also skip the CLC insn below */
2086 AddCodeLine ("bcc %s", LocalLabelName (L));
2087 AddCodeLine ("inx");
2090 /* Add the current stackpointer value */
2091 AddCodeLine ("clc");
2093 /* Label was used above */
2096 AddCodeLine ("adc sp");
2097 AddCodeLine ("tay");
2098 AddCodeLine ("txa");
2099 AddCodeLine ("adc sp+1");
2100 AddCodeLine ("tax");
2101 AddCodeLine ("tya");
2106 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2107 /* Add the address of a static variable to ax */
2109 /* Create the correct label name */
2110 const char* lbuf = GetLabelName (flags, label, offs);
2112 /* Add the address to the current ax value */
2113 AddCodeLine ("clc");
2114 AddCodeLine ("adc #<(%s)", lbuf);
2115 AddCodeLine ("tay");
2116 AddCodeLine ("txa");
2117 AddCodeLine ("adc #>(%s)", lbuf);
2118 AddCodeLine ("tax");
2119 AddCodeLine ("tya");
2124 /*****************************************************************************/
2126 /*****************************************************************************/
2130 void g_save (unsigned flags)
2131 /* Copy primary register to hold register. */
2133 /* Check the size and determine operation */
2134 switch (flags & CF_TYPE) {
2137 if (flags & CF_FORCECHAR) {
2138 AddCodeLine ("pha");
2144 AddCodeLine ("sta regsave");
2145 AddCodeLine ("stx regsave+1");
2149 AddCodeLine ("jsr saveeax");
2159 void g_restore (unsigned flags)
2160 /* Copy hold register to primary. */
2162 /* Check the size and determine operation */
2163 switch (flags & CF_TYPE) {
2166 if (flags & CF_FORCECHAR) {
2167 AddCodeLine ("pla");
2173 AddCodeLine ("lda regsave");
2174 AddCodeLine ("ldx regsave+1");
2178 AddCodeLine ("jsr resteax");
2188 void g_cmp (unsigned flags, unsigned long val)
2189 /* Immidiate compare. The primary register will not be changed, Z flag
2195 /* Check the size and determine operation */
2196 switch (flags & CF_TYPE) {
2199 if (flags & CF_FORCECHAR) {
2200 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2206 L = GetLocalLabel();
2207 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2208 AddCodeLine ("bne %s", LocalLabelName (L));
2209 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2214 Internal ("g_cmp: Long compares not implemented");
2224 static void oper (unsigned flags, unsigned long val, char** subs)
2225 /* Encode a binary operation. subs is a pointer to four groups of three
2227 * 0-2 --> Operate on ints
2228 * 3-5 --> Operate on unsigneds
2229 * 6-8 --> Operate on longs
2230 * 9-11 --> Operate on unsigned longs
2232 * The first subroutine names in each string group is used to encode an
2233 * operation with a zero constant, the second to encode an operation with
2234 * a 8 bit constant, and the third is used in all other cases.
2239 /* Determine the offset into the array */
2240 offs = (flags & CF_UNSIGNED)? 3 : 0;
2241 switch (flags & CF_TYPE) {
2254 /* Encode the operation */
2255 if (flags & CF_CONST) {
2256 /* Constant value given */
2257 if (val == 0 && subs [offs+0]) {
2258 /* Special case: constant with value zero */
2259 AddCodeLine ("jsr %s", subs [offs+0]);
2260 } else if (val < 0x100 && subs [offs+1]) {
2261 /* Special case: constant with high byte zero */
2262 ldaconst (val); /* Load low byte */
2263 AddCodeLine ("jsr %s", subs [offs+1]);
2265 /* Others: arbitrary constant value */
2266 g_getimmed (flags, val, 0); /* Load value */
2267 AddCodeLine ("jsr %s", subs [offs+2]);
2270 /* Value not constant (is already in (e)ax) */
2271 AddCodeLine ("jsr %s", subs [offs+2]);
2274 /* The operation will pop it's argument */
2280 void g_test (unsigned flags)
2281 /* Test the value in the primary and set the condition codes */
2283 switch (flags & CF_TYPE) {
2286 if (flags & CF_FORCECHAR) {
2287 AddCodeLine ("tax");
2293 AddCodeLine ("stx tmp1");
2294 AddCodeLine ("ora tmp1");
2298 if (flags & CF_UNSIGNED) {
2299 AddCodeLine ("jsr utsteax");
2301 AddCodeLine ("jsr tsteax");
2313 void g_push (unsigned flags, unsigned long val)
2314 /* Push the primary register or a constant value onto the stack */
2316 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2318 /* We have a constant 8 or 16 bit value */
2319 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2321 /* Handle as 8 bit value */
2323 AddCodeLine ("jsr pusha");
2327 /* Handle as 16 bit value */
2328 g_getimmed (flags, val, 0);
2329 AddCodeLine ("jsr pushax");
2334 /* Value is not 16 bit or not constant */
2335 if (flags & CF_CONST) {
2336 /* Constant 32 bit value, load into eax */
2337 g_getimmed (flags, val, 0);
2340 /* Push the primary register */
2341 switch (flags & CF_TYPE) {
2344 if (flags & CF_FORCECHAR) {
2345 /* Handle as char */
2346 AddCodeLine ("jsr pusha");
2351 AddCodeLine ("jsr pushax");
2355 AddCodeLine ("jsr pusheax");
2365 /* Adjust the stack offset */
2371 void g_swap (unsigned flags)
2372 /* Swap the primary register and the top of the stack. flags give the type
2373 * of *both* values (must have same size).
2376 switch (flags & CF_TYPE) {
2380 AddCodeLine ("jsr swapstk");
2384 AddCodeLine ("jsr swapestk");
2395 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2396 /* Call the specified subroutine name */
2398 if ((Flags & CF_FIXARGC) == 0) {
2399 /* Pass the argument count */
2402 AddCodeLine ("jsr _%s", Label);
2403 oursp += ArgSize; /* callee pops args */
2408 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2409 /* Call subroutine indirect */
2411 if ((Flags & CF_LOCAL) == 0) {
2412 /* Address is in a/x */
2413 if ((Flags & CF_FIXARGC) == 0) {
2414 /* Pass arg count */
2417 AddCodeLine ("jsr callax");
2419 /* The address is on stack, offset is on Val */
2421 CheckLocalOffs (Offs);
2422 AddCodeLine ("pha");
2423 AddCodeLine ("ldy #$%02X", Offs);
2424 AddCodeLine ("lda (sp),y");
2425 AddCodeLine ("sta jmpvec+1");
2426 AddCodeLine ("iny");
2427 AddCodeLine ("lda (sp),y");
2428 AddCodeLine ("sta jmpvec+2");
2429 AddCodeLine ("pla");
2430 AddCodeLine ("jsr jmpvec");
2433 /* Callee pops args */
2439 void g_jump (unsigned Label)
2440 /* Jump to specified internal label number */
2442 AddCodeLine ("jmp %s", LocalLabelName (Label));
2447 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2448 /* Jump to label if zero flag clear */
2450 AddCodeLine ("jne %s", LocalLabelName (label));
2455 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2456 /* Jump to label if zero flag set */
2458 AddCodeLine ("jeq %s", LocalLabelName (label));
2463 static void mod_internal (int k, char* verb1, char* verb2)
2466 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2470 AddCodeLine ("jsr %ssp", verb2);
2476 void g_space (int space)
2477 /* Create or drop space on the stack */
2480 mod_internal (-space, "inc", "addy");
2481 } else if (space > 0) {
2482 mod_internal (space, "dec", "suby");
2488 void g_cstackcheck (void)
2489 /* Check for a C stack overflow */
2491 AddCodeLine ("jsr cstkchk");
2496 void g_stackcheck (void)
2497 /* Check for a stack overflow */
2499 AddCodeLine ("jsr stkchk");
2504 void g_add (unsigned flags, unsigned long val)
2505 /* Primary = TOS + Primary */
2507 static char* ops [12] = {
2508 0, "tosadda0", "tosaddax",
2509 0, "tosadda0", "tosaddax",
2514 if (flags & CF_CONST) {
2515 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2516 g_push (flags & ~CF_CONST, 0);
2518 oper (flags, val, ops);
2523 void g_sub (unsigned flags, unsigned long val)
2524 /* Primary = TOS - Primary */
2526 static char* ops [12] = {
2527 0, "tossuba0", "tossubax",
2528 0, "tossuba0", "tossubax",
2533 if (flags & CF_CONST) {
2534 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2535 g_push (flags & ~CF_CONST, 0);
2537 oper (flags, val, ops);
2542 void g_rsub (unsigned flags, unsigned long val)
2543 /* Primary = Primary - TOS */
2545 static char* ops [12] = {
2546 0, "tosrsuba0", "tosrsubax",
2547 0, "tosrsuba0", "tosrsubax",
2551 oper (flags, val, ops);
2556 void g_mul (unsigned flags, unsigned long val)
2557 /* Primary = TOS * Primary */
2559 static char* ops [12] = {
2560 0, "tosmula0", "tosmulax",
2561 0, "tosumula0", "tosumulax",
2568 /* Do strength reduction if the value is constant and a power of two */
2569 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2570 /* Generate a shift instead */
2575 /* If the right hand side is const, the lhs is not on stack but still
2576 * in the primary register.
2578 if (flags & CF_CONST) {
2580 switch (flags & CF_TYPE) {
2583 if (flags & CF_FORCECHAR) {
2584 /* Handle some special cases */
2588 AddCodeLine ("sta tmp1");
2589 AddCodeLine ("asl a");
2590 AddCodeLine ("clc");
2591 AddCodeLine ("adc tmp1");
2595 AddCodeLine ("sta tmp1");
2596 AddCodeLine ("asl a");
2597 AddCodeLine ("asl a");
2598 AddCodeLine ("clc");
2599 AddCodeLine ("adc tmp1");
2603 AddCodeLine ("sta tmp1");
2604 AddCodeLine ("asl a");
2605 AddCodeLine ("clc");
2606 AddCodeLine ("adc tmp1");
2607 AddCodeLine ("asl a");
2611 AddCodeLine ("sta tmp1");
2612 AddCodeLine ("asl a");
2613 AddCodeLine ("asl a");
2614 AddCodeLine ("clc");
2615 AddCodeLine ("adc tmp1");
2616 AddCodeLine ("asl a");
2625 AddCodeLine ("jsr mulax3");
2628 AddCodeLine ("jsr mulax5");
2631 AddCodeLine ("jsr mulax6");
2634 AddCodeLine ("jsr mulax7");
2637 AddCodeLine ("jsr mulax9");
2640 AddCodeLine ("jsr mulax10");
2652 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2653 * into the normal, non-optimized stuff.
2655 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2656 g_push (flags & ~CF_CONST, 0);
2660 /* Use long way over the stack */
2661 oper (flags, val, ops);
2666 void g_div (unsigned flags, unsigned long val)
2667 /* Primary = TOS / Primary */
2669 static char* ops [12] = {
2670 0, "tosdiva0", "tosdivax",
2671 0, "tosudiva0", "tosudivax",
2676 /* Do strength reduction if the value is constant and a power of two */
2678 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2679 /* Generate a shift instead */
2682 /* Generate a division */
2683 if (flags & CF_CONST) {
2684 /* lhs is not on stack */
2685 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2686 g_push (flags & ~CF_CONST, 0);
2688 oper (flags, val, ops);
2694 void g_mod (unsigned flags, unsigned long val)
2695 /* Primary = TOS % Primary */
2697 static char* ops [12] = {
2698 0, "tosmoda0", "tosmodax",
2699 0, "tosumoda0", "tosumodax",
2705 /* Check if we can do some cost reduction */
2706 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2707 /* We can do that with an AND operation */
2708 g_and (flags, val - 1);
2710 /* Do it the hard way... */
2711 if (flags & CF_CONST) {
2712 /* lhs is not on stack */
2713 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2714 g_push (flags & ~CF_CONST, 0);
2716 oper (flags, val, ops);
2722 void g_or (unsigned flags, unsigned long val)
2723 /* Primary = TOS | Primary */
2725 static char* ops [12] = {
2726 0, "tosora0", "tosorax",
2727 0, "tosora0", "tosorax",
2732 /* If the right hand side is const, the lhs is not on stack but still
2733 * in the primary register.
2735 if (flags & CF_CONST) {
2737 switch (flags & CF_TYPE) {
2740 if (flags & CF_FORCECHAR) {
2741 if ((val & 0xFF) != 0xFF) {
2742 AddCodeLine ("ora #$%02X", (unsigned char)val);
2750 AddCodeLine ("ora #$%02X", (unsigned char)val);
2757 AddCodeLine ("ora #$%02X", (unsigned char)val);
2766 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2767 * into the normal, non-optimized stuff.
2769 g_push (flags & ~CF_CONST, 0);
2773 /* Use long way over the stack */
2774 oper (flags, val, ops);
2779 void g_xor (unsigned flags, unsigned long val)
2780 /* Primary = TOS ^ Primary */
2782 static char* ops [12] = {
2783 0, "tosxora0", "tosxorax",
2784 0, "tosxora0", "tosxorax",
2790 /* If the right hand side is const, the lhs is not on stack but still
2791 * in the primary register.
2793 if (flags & CF_CONST) {
2795 switch (flags & CF_TYPE) {
2798 if (flags & CF_FORCECHAR) {
2799 if ((val & 0xFF) != 0) {
2800 AddCodeLine ("eor #$%02X", (unsigned char)val);
2809 AddCodeLine ("eor #$%02X", (unsigned char)val);
2812 } else if ((val & 0xFF) == 0) {
2813 AddCodeLine ("pha");
2814 AddCodeLine ("txa");
2815 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2816 AddCodeLine ("tax");
2817 AddCodeLine ("pla");
2825 AddCodeLine ("eor #$%02X", (unsigned char)val);
2835 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2836 * into the normal, non-optimized stuff.
2838 g_push (flags & ~CF_CONST, 0);
2842 /* Use long way over the stack */
2843 oper (flags, val, ops);
2848 void g_and (unsigned flags, unsigned long val)
2849 /* Primary = TOS & Primary */
2851 static char* ops [12] = {
2852 0, "tosanda0", "tosandax",
2853 0, "tosanda0", "tosandax",
2858 /* If the right hand side is const, the lhs is not on stack but still
2859 * in the primary register.
2861 if (flags & CF_CONST) {
2863 switch (flags & CF_TYPE) {
2866 if (flags & CF_FORCECHAR) {
2867 AddCodeLine ("and #$%02X", (unsigned char)val);
2872 if ((val & 0xFFFF) != 0xFFFF) {
2877 } else if (val != 0xFF) {
2878 AddCodeLine ("and #$%02X", (unsigned char)val);
2880 } else if ((val & 0xFF00) == 0xFF00) {
2881 AddCodeLine ("and #$%02X", (unsigned char)val);
2882 } else if ((val & 0x00FF) == 0x0000) {
2883 AddCodeLine ("txa");
2884 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2885 AddCodeLine ("tax");
2888 AddCodeLine ("tay");
2889 AddCodeLine ("txa");
2890 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2891 AddCodeLine ("tax");
2892 AddCodeLine ("tya");
2893 if ((val & 0x00FF) != 0x00FF) {
2894 AddCodeLine ("and #$%02X", (unsigned char)val);
2903 AddCodeLine ("stx sreg+1");
2904 AddCodeLine ("stx sreg");
2905 if ((val & 0xFF) != 0xFF) {
2906 AddCodeLine ("and #$%02X", (unsigned char)val);
2909 } else if (val == 0xFF00) {
2911 AddCodeLine ("sta sreg+1");
2912 AddCodeLine ("sta sreg");
2921 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2922 * into the normal, non-optimized stuff.
2924 g_push (flags & ~CF_CONST, 0);
2928 /* Use long way over the stack */
2929 oper (flags, val, ops);
2934 void g_asr (unsigned flags, unsigned long val)
2935 /* Primary = TOS >> Primary */
2937 static char* ops [12] = {
2938 0, "tosasra0", "tosasrax",
2939 0, "tosshra0", "tosshrax",
2944 /* If the right hand side is const, the lhs is not on stack but still
2945 * in the primary register.
2947 if (flags & CF_CONST) {
2949 switch (flags & CF_TYPE) {
2953 if (val >= 8 && (flags & CF_UNSIGNED)) {
2954 AddCodeLine ("txa");
2961 } else if (val >= 1 && val <= 4) {
2962 if (flags & CF_UNSIGNED) {
2963 AddCodeLine ("jsr shrax%ld", val);
2965 AddCodeLine ("jsr asrax%ld", val);
2975 } else if (val >= 1 && val <= 4) {
2976 if (flags & CF_UNSIGNED) {
2977 AddCodeLine ("jsr shreax%ld", val);
2979 AddCodeLine ("jsr asreax%ld", val);
2982 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2983 AddCodeLine ("txa");
2984 AddCodeLine ("ldx sreg");
2985 AddCodeLine ("ldy sreg+1");
2986 AddCodeLine ("sty sreg");
2987 AddCodeLine ("ldy #$00");
2988 AddCodeLine ("sty sreg+1");
2990 } else if (val == 16) {
2991 AddCodeLine ("ldy #$00");
2992 AddCodeLine ("ldx sreg+1");
2993 if ((flags & CF_UNSIGNED) == 0) {
2994 unsigned L = GetLocalLabel();
2995 AddCodeLine ("bpl %s", LocalLabelName (L));
2996 AddCodeLine ("dey");
2999 AddCodeLine ("lda sreg");
3000 AddCodeLine ("sty sreg+1");
3001 AddCodeLine ("sty sreg");
3010 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3011 * into the normal, non-optimized stuff.
3013 g_push (flags & ~CF_CONST, 0);
3017 /* Use long way over the stack */
3018 oper (flags, val, ops);
3023 void g_asl (unsigned flags, unsigned long val)
3024 /* Primary = TOS << Primary */
3026 static char* ops [12] = {
3027 0, "tosasla0", "tosaslax",
3028 0, "tosshla0", "tosshlax",
3034 /* If the right hand side is const, the lhs is not on stack but still
3035 * in the primary register.
3037 if (flags & CF_CONST) {
3039 switch (flags & CF_TYPE) {
3044 AddCodeLine ("tax");
3045 AddCodeLine ("lda #$00");
3051 } else if (val >= 1 && val <= 4) {
3052 if (flags & CF_UNSIGNED) {
3053 AddCodeLine ("jsr shlax%ld", val);
3055 AddCodeLine ("jsr aslax%ld", val);
3065 } else if (val >= 1 && val <= 4) {
3066 if (flags & CF_UNSIGNED) {
3067 AddCodeLine ("jsr shleax%ld", val);
3069 AddCodeLine ("jsr asleax%ld", val);
3072 } else if (val == 8) {
3073 AddCodeLine ("ldy sreg");
3074 AddCodeLine ("sty sreg+1");
3075 AddCodeLine ("stx sreg");
3076 AddCodeLine ("tax");
3077 AddCodeLine ("lda #$00");
3079 } else if (val == 16) {
3080 AddCodeLine ("stx sreg+1");
3081 AddCodeLine ("sta sreg");
3082 AddCodeLine ("lda #$00");
3083 AddCodeLine ("tax");
3092 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3093 * into the normal, non-optimized stuff.
3095 g_push (flags & ~CF_CONST, 0);
3099 /* Use long way over the stack */
3100 oper (flags, val, ops);
3105 void g_neg (unsigned flags)
3106 /* Primary = -Primary */
3108 switch (flags & CF_TYPE) {
3112 AddCodeLine ("jsr negax");
3116 AddCodeLine ("jsr negeax");
3126 void g_bneg (unsigned flags)
3127 /* Primary = !Primary */
3129 switch (flags & CF_TYPE) {
3132 AddCodeLine ("jsr bnega");
3136 AddCodeLine ("jsr bnegax");
3140 AddCodeLine ("jsr bnegeax");
3150 void g_com (unsigned flags)
3151 /* Primary = ~Primary */
3153 switch (flags & CF_TYPE) {
3157 AddCodeLine ("jsr complax");
3161 AddCodeLine ("jsr compleax");
3171 void g_inc (unsigned flags, unsigned long val)
3172 /* Increment the primary register by a given number */
3174 /* Don't inc by zero */
3179 /* Generate code for the supported types */
3181 switch (flags & CF_TYPE) {
3184 if (flags & CF_FORCECHAR) {
3185 if (CPU == CPU_65C02 && val <= 2) {
3187 AddCodeLine ("ina");
3190 AddCodeLine ("clc");
3191 AddCodeLine ("adc #$%02X", (unsigned char)val);
3198 if (CPU == CPU_65C02 && val == 1) {
3199 unsigned L = GetLocalLabel();
3200 AddCodeLine ("ina");
3201 AddCodeLine ("bne %s", LocalLabelName (L));
3202 AddCodeLine ("inx");
3204 } else if (CodeSizeFactor < 200) {
3207 AddCodeLine ("jsr incax%lu", val);
3208 } else if (val <= 255) {
3210 AddCodeLine ("jsr incaxy");
3212 g_add (flags | CF_CONST, val);
3215 /* Inline the code */
3217 if ((val & 0xFF) != 0) {
3218 unsigned L = GetLocalLabel();
3219 AddCodeLine ("clc");
3220 AddCodeLine ("adc #$%02X", (unsigned char) val);
3221 AddCodeLine ("bcc %s", LocalLabelName (L));
3222 AddCodeLine ("inx");
3226 AddCodeLine ("inx");
3229 AddCodeLine ("inx");
3232 AddCodeLine ("inx");
3235 AddCodeLine ("clc");
3236 if ((val & 0xFF) != 0) {
3237 AddCodeLine ("adc #$%02X", (unsigned char) val);
3239 AddCodeLine ("pha");
3240 AddCodeLine ("txa");
3241 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3242 AddCodeLine ("tax");
3243 AddCodeLine ("pla");
3251 AddCodeLine ("jsr inceaxy");
3253 g_add (flags | CF_CONST, val);
3265 void g_dec (unsigned flags, unsigned long val)
3266 /* Decrement the primary register by a given number */
3268 /* Don't dec by zero */
3273 /* Generate code for the supported types */
3275 switch (flags & CF_TYPE) {
3278 if (flags & CF_FORCECHAR) {
3279 if (CPU == CPU_65C02 && val <= 2) {
3281 AddCodeLine ("dea");
3284 AddCodeLine ("sec");
3285 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3292 if (CodeSizeFactor < 200) {
3293 /* Use subroutines */
3295 AddCodeLine ("jsr decax%d", (int) val);
3296 } else if (val <= 255) {
3298 AddCodeLine ("jsr decaxy");
3300 g_sub (flags | CF_CONST, val);
3303 /* Inline the code */
3305 if ((val & 0xFF) != 0) {
3306 unsigned L = GetLocalLabel();
3307 AddCodeLine ("sec");
3308 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3309 AddCodeLine ("bcs %s", LocalLabelName (L));
3310 AddCodeLine ("dex");
3314 AddCodeLine ("dex");
3317 AddCodeLine ("dex");
3320 AddCodeLine ("sec");
3321 if ((val & 0xFF) != 0) {
3322 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3324 AddCodeLine ("pha");
3325 AddCodeLine ("txa");
3326 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3327 AddCodeLine ("tax");
3328 AddCodeLine ("pla");
3336 AddCodeLine ("jsr deceaxy");
3338 g_sub (flags | CF_CONST, val);
3351 * Following are the conditional operators. They compare the TOS against
3352 * the primary and put a literal 1 in the primary if the condition is
3353 * true, otherwise they clear the primary register
3358 void g_eq (unsigned flags, unsigned long val)
3359 /* Test for equal */
3361 static char* ops [12] = {
3362 "toseq00", "toseqa0", "toseqax",
3363 "toseq00", "toseqa0", "toseqax",
3370 /* If the right hand side is const, the lhs is not on stack but still
3371 * in the primary register.
3373 if (flags & CF_CONST) {
3375 switch (flags & CF_TYPE) {
3378 if (flags & CF_FORCECHAR) {
3379 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3380 AddCodeLine ("jsr booleq");
3386 L = GetLocalLabel();
3387 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3388 AddCodeLine ("bne %s", LocalLabelName (L));
3389 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3391 AddCodeLine ("jsr booleq");
3401 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3402 * into the normal, non-optimized stuff.
3404 g_push (flags & ~CF_CONST, 0);
3408 /* Use long way over the stack */
3409 oper (flags, val, ops);
3414 void g_ne (unsigned flags, unsigned long val)
3415 /* Test for not equal */
3417 static char* ops [12] = {
3418 "tosne00", "tosnea0", "tosneax",
3419 "tosne00", "tosnea0", "tosneax",
3426 /* If the right hand side is const, the lhs is not on stack but still
3427 * in the primary register.
3429 if (flags & CF_CONST) {
3431 switch (flags & CF_TYPE) {
3434 if (flags & CF_FORCECHAR) {
3435 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3436 AddCodeLine ("jsr boolne");
3442 L = GetLocalLabel();
3443 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3444 AddCodeLine ("bne %s", LocalLabelName (L));
3445 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3447 AddCodeLine ("jsr boolne");
3457 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3458 * into the normal, non-optimized stuff.
3460 g_push (flags & ~CF_CONST, 0);
3464 /* Use long way over the stack */
3465 oper (flags, val, ops);
3470 void g_lt (unsigned flags, unsigned long val)
3471 /* Test for less than */
3473 static char* ops [12] = {
3474 "toslt00", "toslta0", "tosltax",
3475 "tosult00", "tosulta0", "tosultax",
3480 /* If the right hand side is const, the lhs is not on stack but still
3481 * in the primary register.
3483 if (flags & CF_CONST) {
3485 /* Give a warning in some special cases */
3486 if ((flags & CF_UNSIGNED) && val == 0) {
3487 Warning ("Condition is never true");
3488 AddCodeLine ("jsr return0");
3492 /* Look at the type */
3493 switch (flags & CF_TYPE) {
3496 if (flags & CF_FORCECHAR) {
3497 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3498 if (flags & CF_UNSIGNED) {
3499 AddCodeLine ("jsr boolult");
3501 AddCodeLine ("jsr boollt");
3508 if (flags & CF_UNSIGNED) {
3509 /* Unsigned compare */
3510 /* If the low byte is zero, we must only test the high byte */
3511 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3512 if ((val & 0xFF) != 0) {
3513 unsigned L = GetLocalLabel();
3514 AddCodeLine ("bne %s", LocalLabelName (L));
3515 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3518 AddCodeLine ("jsr boolult");
3520 /* Signed compare */
3521 if ((val & 0xFF) == 0) {
3522 /* Low byte is zero, just look at the high byte */
3523 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3525 /* Subtract the two values */
3526 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3527 AddCodeLine ("txa");
3528 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3530 AddCodeLine ("jsr boollt");
3535 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3536 /* If we have a signed compare against zero, we only need to
3537 * test the high byte.
3539 AddCodeLine ("lda sreg+1");
3541 /* Do a subtraction */
3542 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3543 AddCodeLine ("txa");
3544 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3545 AddCodeLine ("lda sreg");
3546 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3547 AddCodeLine ("lda sreg+1");
3548 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3550 /* Emit the proper makebool routine */
3551 if (flags & CF_UNSIGNED) {
3552 /* Unsigned compare */
3553 AddCodeLine ("jsr boolult");
3555 /* Signed compare */
3556 AddCodeLine ("jsr boollt");
3564 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3565 * into the normal, non-optimized stuff.
3567 g_push (flags & ~CF_CONST, 0);
3571 /* Use long way over the stack */
3572 oper (flags, val, ops);
3577 void g_le (unsigned flags, unsigned long val)
3578 /* Test for less than or equal to */
3580 static char* ops [12] = {
3581 "tosle00", "toslea0", "tosleax",
3582 "tosule00", "tosulea0", "tosuleax",
3588 /* If the right hand side is const, the lhs is not on stack but still
3589 * in the primary register.
3591 if (flags & CF_CONST) {
3593 /* Look at the type */
3594 switch (flags & CF_TYPE) {
3597 if (flags & CF_FORCECHAR) {
3598 if (flags & CF_UNSIGNED) {
3599 /* Unsigned compare */
3601 /* Use < instead of <= because the former gives
3602 * better code on the 6502 than the latter.
3604 g_lt (flags, val+1);
3607 Warning ("Condition is always true");
3608 AddCodeLine ("jsr return1");
3611 /* Signed compare */
3612 if ((long) val < 0x7F) {
3613 /* Use < instead of <= because the former gives
3614 * better code on the 6502 than the latter.
3616 g_lt (flags, val+1);
3619 Warning ("Condition is always true");
3620 AddCodeLine ("jsr return1");
3628 if (flags & CF_UNSIGNED) {
3629 /* Unsigned compare */
3631 /* Use < instead of <= because the former gives
3632 * better code on the 6502 than the latter.
3634 g_lt (flags, val+1);
3637 Warning ("Condition is always true");
3638 AddCodeLine ("jsr return1");
3641 /* Signed compare */
3642 if ((long) val < 0x7FFF) {
3643 g_lt (flags, val+1);
3646 Warning ("Condition is always true");
3647 AddCodeLine ("jsr return1");
3653 if (flags & CF_UNSIGNED) {
3654 /* Unsigned compare */
3655 if (val < 0xFFFFFFFF) {
3656 /* Use < instead of <= because the former gives
3657 * better code on the 6502 than the latter.
3659 g_lt (flags, val+1);
3662 Warning ("Condition is always true");
3663 AddCodeLine ("jsr return1");
3666 /* Signed compare */
3667 if ((long) val < 0x7FFFFFFF) {
3668 g_lt (flags, val+1);
3671 Warning ("Condition is always true");
3672 AddCodeLine ("jsr return1");
3681 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3682 * into the normal, non-optimized stuff.
3684 g_push (flags & ~CF_CONST, 0);
3688 /* Use long way over the stack */
3689 oper (flags, val, ops);
3694 void g_gt (unsigned flags, unsigned long val)
3695 /* Test for greater than */
3697 static char* ops [12] = {
3698 "tosgt00", "tosgta0", "tosgtax",
3699 "tosugt00", "tosugta0", "tosugtax",
3705 /* If the right hand side is const, the lhs is not on stack but still
3706 * in the primary register.
3708 if (flags & CF_CONST) {
3710 /* Look at the type */
3711 switch (flags & CF_TYPE) {
3714 if (flags & CF_FORCECHAR) {
3715 if (flags & CF_UNSIGNED) {
3717 /* If we have a compare > 0, we will replace it by
3718 * != 0 here, since both are identical but the
3719 * latter is easier to optimize.
3722 } else if (val < 0xFF) {
3723 /* Use >= instead of > because the former gives
3724 * better code on the 6502 than the latter.
3726 g_ge (flags, val+1);
3729 Warning ("Condition is never true");
3730 AddCodeLine ("jsr return0");
3733 if ((long) val < 0x7F) {
3734 /* Use >= instead of > because the former gives
3735 * better code on the 6502 than the latter.
3737 g_ge (flags, val+1);
3740 Warning ("Condition is never true");
3741 AddCodeLine ("jsr return0");
3749 if (flags & CF_UNSIGNED) {
3750 /* Unsigned compare */
3752 /* If we have a compare > 0, we will replace it by
3753 * != 0 here, since both are identical but the latter
3754 * is easier to optimize.
3757 } else if (val < 0xFFFF) {
3758 /* Use >= instead of > because the former gives better
3759 * code on the 6502 than the latter.
3761 g_ge (flags, val+1);
3764 Warning ("Condition is never true");
3765 AddCodeLine ("jsr return0");
3768 /* Signed compare */
3769 if ((long) val < 0x7FFF) {
3770 g_ge (flags, val+1);
3773 Warning ("Condition is never true");
3774 AddCodeLine ("jsr return0");
3780 if (flags & CF_UNSIGNED) {
3781 /* Unsigned compare */
3783 /* If we have a compare > 0, we will replace it by
3784 * != 0 here, since both are identical but the latter
3785 * is easier to optimize.
3788 } else if (val < 0xFFFFFFFF) {
3789 /* Use >= instead of > because the former gives better
3790 * code on the 6502 than the latter.
3792 g_ge (flags, val+1);
3795 Warning ("Condition is never true");
3796 AddCodeLine ("jsr return0");
3799 /* Signed compare */
3800 if ((long) val < 0x7FFFFFFF) {
3801 g_ge (flags, val+1);
3804 Warning ("Condition is never true");
3805 AddCodeLine ("jsr return0");
3814 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3815 * into the normal, non-optimized stuff.
3817 g_push (flags & ~CF_CONST, 0);
3821 /* Use long way over the stack */
3822 oper (flags, val, ops);
3827 void g_ge (unsigned flags, unsigned long val)
3828 /* Test for greater than or equal to */
3830 static char* ops [12] = {
3831 "tosge00", "tosgea0", "tosgeax",
3832 "tosuge00", "tosugea0", "tosugeax",
3838 /* If the right hand side is const, the lhs is not on stack but still
3839 * in the primary register.
3841 if (flags & CF_CONST) {
3843 /* Give a warning in some special cases */
3844 if ((flags & CF_UNSIGNED) && val == 0) {
3845 Warning ("Condition is always true");
3846 AddCodeLine ("jsr return1");
3850 /* Look at the type */
3851 switch (flags & CF_TYPE) {
3854 if (flags & CF_FORCECHAR) {
3855 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3856 if (flags & CF_UNSIGNED) {
3857 AddCodeLine ("jsr booluge");
3859 AddCodeLine ("jsr boolge");
3866 if (flags & CF_UNSIGNED) {
3867 /* Unsigned compare */
3868 /* If the low byte is zero, we must only test the high byte */
3869 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3870 if ((val & 0xFF) != 0) {
3871 unsigned L = GetLocalLabel();
3872 AddCodeLine ("bne %s", LocalLabelName (L));
3873 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3876 AddCodeLine ("jsr booluge");
3878 /* Signed compare */
3879 if ((val & 0xFF) == 0) {
3880 /* Low byte is zero, just look at the high byte */
3881 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3883 /* Subtract the two values */
3884 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3885 AddCodeLine ("txa");
3886 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3888 AddCodeLine ("jsr boolge");
3893 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3894 /* If we have a signed compare against zero, we only need to
3895 * test the high byte.
3897 AddCodeLine ("lda sreg+1");
3899 /* Do a subtraction */
3900 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3901 AddCodeLine ("txa");
3902 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3903 AddCodeLine ("lda sreg");
3904 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3905 AddCodeLine ("lda sreg+1");
3906 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3908 /* Emit the proper makebool routine */
3909 if (flags & CF_UNSIGNED) {
3910 /* Unsigned compare */
3911 AddCodeLine ("jsr booluge");
3913 /* Signed compare */
3914 AddCodeLine ("jsr boolge");
3922 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3923 * into the normal, non-optimized stuff.
3925 g_push (flags & ~CF_CONST, 0);
3929 /* Use long way over the stack */
3930 oper (flags, val, ops);
3935 /*****************************************************************************/
3936 /* Allocating static storage */
3937 /*****************************************************************************/
3941 void g_res (unsigned n)
3942 /* Reserve static storage, n bytes */
3944 AddDataLine ("\t.res\t%u,$00", n);
3949 void g_defdata (unsigned flags, unsigned long val, long offs)
3950 /* Define data with the size given in flags */
3952 if (flags & CF_CONST) {
3954 /* Numeric constant */
3955 switch (flags & CF_TYPE) {
3958 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
3962 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
3966 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3977 /* Create the correct label name */
3978 const char* Label = GetLabelName (flags, val, offs);
3980 /* Labels are always 16 bit */
3981 AddDataLine ("\t.addr\t%s", Label);
3988 void g_defbytes (const void* Bytes, unsigned Count)
3989 /* Output a row of bytes as a constant */
3995 /* Cast the buffer pointer */
3996 const unsigned char* Data = (const unsigned char*) Bytes;
3998 /* Output the stuff */
4001 /* How many go into this line? */
4002 if ((Chunk = Count) > 16) {
4007 /* Output one line */
4008 strcpy (Buf, "\t.byte\t");
4011 B += sprintf (B, "$%02X", *Data++);
4017 /* Output the line */
4024 void g_zerobytes (unsigned Count)
4025 /* Output Count bytes of data initialized with zero */
4028 AddDataLine ("\t.res\t%u,$00", Count);
4034 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4035 /* Initialize a register variable from static initialization data */
4037 /* Register variables do always have less than 128 bytes */
4038 unsigned CodeLabel = GetLocalLabel ();
4040 g_defcodelabel (CodeLabel);
4041 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4042 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4043 AddCodeLine ("dex");
4044 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4049 void g_initauto (unsigned Label, unsigned Size)
4050 /* Initialize a local variable at stack offset zero from static data */
4052 unsigned CodeLabel = GetLocalLabel ();
4054 CheckLocalOffs (Size);
4057 g_defcodelabel (CodeLabel);
4058 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4059 AddCodeLine ("sta (sp),y");
4060 AddCodeLine ("dey");
4061 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4062 } else if (Size <= 256) {
4064 g_defcodelabel (CodeLabel);
4065 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4066 AddCodeLine ("sta (sp),y");
4067 AddCodeLine ("iny");
4068 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4069 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4075 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4076 /* Initialize a static local variable from static initialization data */
4079 unsigned CodeLabel = GetLocalLabel ();
4081 g_defcodelabel (CodeLabel);
4082 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4083 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4084 AddCodeLine ("dey");
4085 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4086 } else if (Size <= 256) {
4087 unsigned CodeLabel = GetLocalLabel ();
4089 g_defcodelabel (CodeLabel);
4090 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4091 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4092 AddCodeLine ("iny");
4093 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4094 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4096 /* Use the easy way here: memcpy */
4097 g_getimmed (CF_STATIC, VarLabel, 0);
4098 AddCodeLine ("jsr pushax");
4099 g_getimmed (CF_STATIC, InitLabel, 0);
4100 AddCodeLine ("jsr pushax");
4101 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4102 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4108 /*****************************************************************************/
4109 /* Switch statement */
4110 /*****************************************************************************/
4114 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4115 /* Generate code for a switch statement */
4117 unsigned NextLabel = 0;
4120 /* Setup registers and determine which compare insn to use */
4121 const char* Compare;
4124 Compare = "cmp #$%02X";
4127 Compare = "cpx #$%02X";
4130 AddCodeLine ("ldy sreg");
4131 Compare = "cpy #$%02X";
4134 AddCodeLine ("ldy sreg+1");
4135 Compare = "cpy #$%02X";
4138 Internal ("Invalid depth in g_switch: %u", Depth);
4141 /* Walk over all nodes */
4142 for (I = 0; I < CollCount (Nodes); ++I) {
4144 /* Get the next case node */
4145 CaseNode* N = CollAtUnchecked (Nodes, I);
4147 /* If we have a next label, define it */
4149 g_defcodelabel (NextLabel);
4153 /* Do the compare */
4154 AddCodeLine (Compare, CN_GetValue (N));
4156 /* If this is the last level, jump directly to the case code if found */
4159 /* Branch if equal */
4160 g_falsejump (0, CN_GetLabel (N));
4164 /* Determine the next label */
4165 if (I == CollCount (Nodes) - 1) {
4166 /* Last node means not found */
4167 g_truejump (0, DefaultLabel);
4169 /* Jump to the next check */
4170 NextLabel = GetLocalLabel ();
4171 g_truejump (0, NextLabel);
4174 /* Check the next level */
4175 g_switch (N->Nodes, DefaultLabel, Depth-1);
4180 /* If we go here, we haven't found the label */
4181 g_jump (DefaultLabel);
4186 /*****************************************************************************/
4187 /* User supplied assembler code */
4188 /*****************************************************************************/
4192 void g_asmcode (struct StrBuf* B)
4193 /* Output one line of assembler code. */
4195 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));
4200 /*****************************************************************************/
4201 /* Inlined known functions */
4202 /*****************************************************************************/
4206 void g_strlen (unsigned flags, unsigned long val, long offs)
4207 /* Inline the strlen() function */
4209 /* We need a label in both cases */
4210 unsigned label = GetLocalLabel ();
4212 /* Two different encodings */
4213 if (flags & CF_CONST) {
4215 /* The address of the string is constant. Create the correct label name */
4216 const char* lbuf = GetLabelName (flags, val, offs);
4218 /* Generate the strlen code */
4219 AddCodeLine ("ldy #$FF");
4220 g_defcodelabel (label);
4221 AddCodeLine ("iny");
4222 AddCodeLine ("lda %s,y", lbuf);
4223 AddCodeLine ("bne %s", LocalLabelName (label));
4224 AddCodeLine ("tax");
4225 AddCodeLine ("tya");
4229 /* Address not constant but in primary */
4230 if (CodeSizeFactor < 400) {
4231 /* This is too much code, so call strlen instead of inlining */
4232 AddCodeLine ("jsr _strlen");
4234 /* Inline the function */
4235 AddCodeLine ("sta ptr1");
4236 AddCodeLine ("stx ptr1+1");
4237 AddCodeLine ("ldy #$FF");
4238 g_defcodelabel (label);
4239 AddCodeLine ("iny");
4240 AddCodeLine ("lda (ptr1),y");
4241 AddCodeLine ("bne %s", LocalLabelName (label));
4242 AddCodeLine ("tax");
4243 AddCodeLine ("tya");