1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
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 /*****************************************************************************/
60 /*****************************************************************************/
62 /*****************************************************************************/
66 /* Compiler relative stack pointer */
71 /*****************************************************************************/
73 /*****************************************************************************/
77 static void typeerror (unsigned type)
78 /* Print an error message about an invalid operand type */
80 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
85 static void CheckLocalOffs (unsigned Offs)
86 /* Check the offset into the stack for 8bit range */
89 /* Too many local vars */
90 Error ("Too many local variables");
96 static char* GetLabelName (unsigned flags, unsigned long label, unsigned offs)
98 static char lbuf [128]; /* Label name */
100 /* Create the correct label name */
101 switch (flags & CF_ADDRMASK) {
104 /* Static memory cell */
105 sprintf (lbuf, "%s+%u", LocalLabelName (label), offs);
110 sprintf (lbuf, "_%s+%u", (char*) label, offs);
114 /* Absolute address */
115 sprintf (lbuf, "$%04X", (unsigned)((label+offs) & 0xFFFF));
119 /* Variable in register bank */
120 sprintf (lbuf, "regbank+%u", (unsigned)((label+offs) & 0xFFFF));
124 Internal ("Invalid address flags");
127 /* Return a pointer to the static buffer */
133 /*****************************************************************************/
134 /* Pre- and postamble */
135 /*****************************************************************************/
139 void g_preamble (void)
140 /* Generate the assembler code preamble */
142 /* Create a new segment list */
145 /* Identify the compiler version */
147 AddTextLine ("; File generated by cc65 v %u.%u.%u",
148 VER_MAJOR, VER_MINOR, VER_PATCH);
151 /* Insert some object file options */
152 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
153 VER_MAJOR, VER_MINOR, VER_PATCH);
155 /* If we're producing code for some other CPU, switch the command set */
156 if (CPU == CPU_65C02) {
157 AddTextLine ("\t.pc02");
160 /* Allow auto import for runtime library routines */
161 AddTextLine ("\t.autoimport\ton");
163 /* Switch the assembler into case sensitive mode */
164 AddTextLine ("\t.case\t\ton");
166 /* Tell the assembler if we want to generate debug info */
167 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
169 /* Import the stack pointer for direct auto variable access */
170 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1");
172 /* Define long branch macros */
173 AddTextLine ("\t.macpack\tlongbranch");
178 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
179 /* If debug info is enabled, place a file info into the source */
182 AddTextLine ("\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
188 /*****************************************************************************/
189 /* Segment support */
190 /*****************************************************************************/
194 void g_userodata (void)
195 /* Switch to the read only data segment */
197 UseDataSeg (SEG_RODATA);
202 void g_usedata (void)
203 /* Switch to the data segment */
205 UseDataSeg (SEG_DATA);
211 /* Switch to the bss segment */
213 UseDataSeg (SEG_BSS);
218 static void OutputDataLine (DataSeg* S, const char* Format, ...)
219 /* Add a line to the current data segment */
222 va_start (ap, Format);
223 AddDataEntry (S, Format, ap);
229 void g_segname (segment_t Seg, const char* Name)
230 /* Set the name of a segment */
234 /* Remember the new name */
235 NewSegName (Seg, Name);
237 /* Emit a segment directive for the data style segments */
239 case SEG_RODATA: S = CS->ROData; break;
240 case SEG_DATA: S = CS->Data; break;
241 case SEG_BSS: S = CS->BSS; break;
242 default: S = 0; break;
245 OutputDataLine (S, ".segment\t\"%s\"", Name);
251 /*****************************************************************************/
253 /*****************************************************************************/
257 unsigned sizeofarg (unsigned flags)
258 /* Return the size of a function argument type that is encoded in flags */
260 switch (flags & CF_TYPE) {
263 return (flags & CF_FORCECHAR)? 1 : 2;
280 int pop (unsigned flags)
281 /* Pop an argument of the given size */
283 return oursp += sizeofarg (flags);
288 int push (unsigned flags)
289 /* Push an argument of the given size */
291 return oursp -= sizeofarg (flags);
296 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
297 /* The value in Offs is an offset to an address in a/x. Make sure, an object
298 * of the type given in Flags can be loaded or stored into this address by
299 * adding part of the offset to the address in ax, so that the remaining
300 * offset fits into an index register. Return the remaining offset.
303 /* If the offset is too large for a byte register, add the high byte
304 * of the offset to the primary. Beware: We need a special correction
305 * if the offset in the low byte will overflow in the operation.
307 unsigned O = Offs & ~0xFFU;
308 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
309 /* We need to add the low byte also */
313 /* Do the correction if we need one */
315 g_inc (CF_INT | CF_CONST, O);
319 /* Return the new offset */
325 /*****************************************************************************/
326 /* Functions handling local labels */
327 /*****************************************************************************/
331 void g_defcodelabel (unsigned label)
332 /* Define a local code label */
334 CS_AddLabel (CS->Code, LocalLabelName (label));
339 void g_defdatalabel (unsigned label)
340 /* Define a local data label */
342 AddDataLine ("%s:", LocalLabelName (label));
347 /*****************************************************************************/
348 /* Functions handling global labels */
349 /*****************************************************************************/
353 void g_defgloblabel (const char* Name)
354 /* Define a global label with the given name */
356 /* Global labels are always data labels */
357 AddDataLine ("_%s:", Name);
362 void g_defexport (const char* Name, int ZP)
363 /* Export the given label */
366 AddTextLine ("\t.exportzp\t_%s", Name);
368 AddTextLine ("\t.export\t\t_%s", Name);
374 void g_defimport (const char* Name, int ZP)
375 /* Import the given label */
378 AddTextLine ("\t.importzp\t_%s", Name);
380 AddTextLine ("\t.import\t\t_%s", Name);
386 /*****************************************************************************/
387 /* Load functions for various registers */
388 /*****************************************************************************/
392 static void ldaconst (unsigned val)
393 /* Load a with a constant */
395 AddCodeLine ("lda #$%02X", val & 0xFF);
400 static void ldxconst (unsigned val)
401 /* Load x with a constant */
403 AddCodeLine ("ldx #$%02X", val & 0xFF);
408 static void ldyconst (unsigned val)
409 /* Load y with a constant */
411 AddCodeLine ("ldy #$%02X", val & 0xFF);
416 /*****************************************************************************/
417 /* Function entry and exit */
418 /*****************************************************************************/
422 /* Remember the argument size of a function. The variable is set by g_enter
423 * and used by g_leave. If the functions gets its argument size by the caller
424 * (variable param list or function without prototype), g_enter will set the
430 void g_enter (unsigned flags, unsigned argsize)
431 /* Function prologue */
433 if ((flags & CF_FIXARGC) != 0) {
434 /* Just remember the argument size for the leave */
438 AddCodeLine ("jsr enter");
445 /* Function epilogue */
447 /* How many bytes of locals do we have to drop? */
450 /* If we didn't have a variable argument list, don't call leave */
453 /* Drop stackframe if needed */
457 AddCodeLine ("jsr incsp%d", k);
461 AddCodeLine ("jsr addysp");
468 /* Nothing to drop */
469 AddCodeLine ("jsr leave");
471 /* We've a stack frame to drop */
473 AddCodeLine ("jsr leavey");
477 /* Add the final rts */
483 /*****************************************************************************/
484 /* Register variables */
485 /*****************************************************************************/
489 void g_save_regvars (int RegOffs, unsigned Bytes)
490 /* Save register variables */
492 /* Don't loop for up to two bytes */
495 AddCodeLine ("lda regbank%+d", RegOffs);
496 AddCodeLine ("jsr pusha");
498 } else if (Bytes == 2) {
500 AddCodeLine ("lda regbank%+d", RegOffs);
501 AddCodeLine ("ldx regbank%+d", RegOffs+1);
502 AddCodeLine ("jsr pushax");
506 /* More than two bytes - loop */
507 unsigned Label = GetLocalLabel ();
509 ldyconst (Bytes - 1);
511 g_defcodelabel (Label);
512 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
513 AddCodeLine ("sta (sp),y");
516 AddCodeLine ("bne %s", LocalLabelName (Label));
520 /* We pushed stuff, correct the stack pointer */
526 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
527 /* Restore register variables */
529 /* Calculate the actual stack offset and check it */
531 CheckLocalOffs (StackOffs);
533 /* Don't loop for up to two bytes */
536 ldyconst (StackOffs);
537 AddCodeLine ("lda (sp),y");
538 AddCodeLine ("sta regbank%+d", RegOffs);
540 } else if (Bytes == 2) {
542 ldyconst (StackOffs);
543 AddCodeLine ("lda (sp),y");
544 AddCodeLine ("sta regbank%+d", RegOffs);
546 AddCodeLine ("lda (sp),y");
547 AddCodeLine ("sta regbank%+d", RegOffs+1);
551 /* More than two bytes - loop */
552 unsigned Label = GetLocalLabel ();
553 ldyconst (StackOffs+Bytes-1);
555 g_defcodelabel (Label);
556 AddCodeLine ("lda (sp),y");
557 AddCodeLine ("sta regbank%+d,x", RegOffs-1);
560 AddCodeLine ("bne %s", LocalLabelName (Label));
567 /*****************************************************************************/
568 /* Fetching memory cells */
569 /*****************************************************************************/
573 void g_getimmed (unsigned Flags, unsigned long Val, unsigned Offs)
574 /* Load a constant into the primary register */
576 unsigned char B1, B2, B3, B4;
580 if ((Flags & CF_CONST) != 0) {
582 /* Numeric constant */
583 switch (Flags & CF_TYPE) {
586 if ((Flags & CF_FORCECHAR) != 0) {
592 ldxconst ((Val >> 8) & 0xFF);
593 ldaconst (Val & 0xFF);
597 /* Split the value into 4 bytes */
598 B1 = (unsigned char) (Val >> 0);
599 B2 = (unsigned char) (Val >> 8);
600 B3 = (unsigned char) (Val >> 16);
601 B4 = (unsigned char) (Val >> 24);
603 /* Remember which bytes are done */
607 AddCodeLine ("ldx #$%02X", B2);
610 AddCodeLine ("stx sreg");
614 AddCodeLine ("stx sreg+1");
617 if ((Done & 0x04) == 0 && B1 != B3) {
618 AddCodeLine ("lda #$%02X", B3);
619 AddCodeLine ("sta sreg");
622 if ((Done & 0x08) == 0 && B1 != B4) {
623 AddCodeLine ("lda #$%02X", B4);
624 AddCodeLine ("sta sreg+1");
627 AddCodeLine ("lda #$%02X", B1);
629 if ((Done & 0x04) == 0) {
631 AddCodeLine ("sta sreg");
633 if ((Done & 0x08) == 0) {
635 AddCodeLine ("sta sreg+1");
647 /* Some sort of label */
648 const char* Label = GetLabelName (Flags, Val, Offs);
650 /* Load the address into the primary */
651 AddCodeLine ("lda #<(%s)", Label);
652 AddCodeLine ("ldx #>(%s)", Label);
659 void g_getstatic (unsigned flags, unsigned long label, unsigned offs)
660 /* Fetch an static memory cell into the primary register */
662 /* Create the correct label name */
663 char* lbuf = GetLabelName (flags, label, offs);
665 /* Check the size and generate the correct load operation */
666 switch (flags & CF_TYPE) {
669 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
670 AddCodeLine ("lda %s", lbuf); /* load A from the label */
673 AddCodeLine ("lda %s", lbuf); /* load A from the label */
674 if (!(flags & CF_UNSIGNED)) {
675 /* Must sign extend */
676 unsigned L = GetLocalLabel ();
677 AddCodeLine ("bpl %s", LocalLabelName (L));
685 AddCodeLine ("lda %s", lbuf);
686 if (flags & CF_TEST) {
687 AddCodeLine ("ora %s+1", lbuf);
689 AddCodeLine ("ldx %s+1", lbuf);
694 if (flags & CF_TEST) {
695 AddCodeLine ("lda %s+3", lbuf);
696 AddCodeLine ("ora %s+2", lbuf);
697 AddCodeLine ("ora %s+1", lbuf);
698 AddCodeLine ("ora %s+0", lbuf);
700 AddCodeLine ("lda %s+3", lbuf);
701 AddCodeLine ("sta sreg+1");
702 AddCodeLine ("lda %s+2", lbuf);
703 AddCodeLine ("sta sreg");
704 AddCodeLine ("ldx %s+1", lbuf);
705 AddCodeLine ("lda %s", lbuf);
717 void g_getlocal (unsigned flags, int offs)
718 /* Fetch specified local object (local var). */
721 CheckLocalOffs (offs);
722 switch (flags & CF_TYPE) {
725 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
726 if (CPU == CPU_65C02 && offs == 0) {
727 AddCodeLine ("lda (sp)");
730 AddCodeLine ("lda (sp),y");
734 AddCodeLine ("ldx #$00");
735 AddCodeLine ("lda (sp),y");
736 if ((flags & CF_UNSIGNED) == 0) {
737 unsigned L = GetLocalLabel();
738 AddCodeLine ("bpl %s", LocalLabelName (L));
746 CheckLocalOffs (offs + 1);
747 if (flags & CF_TEST) {
749 AddCodeLine ("lda (sp),y");
751 AddCodeLine ("ora (sp),y");
753 if (CodeSizeFactor > 180) {
755 AddCodeLine ("lda (sp),y");
758 AddCodeLine ("lda (sp),y");
762 AddCodeLine ("jsr ldaxysp");
764 AddCodeLine ("jsr ldax0sp");
773 AddCodeLine ("jsr ldeaxysp");
775 AddCodeLine ("jsr ldeax0sp");
786 void g_getind (unsigned flags, unsigned offs)
787 /* Fetch the specified object type indirect through the primary register
788 * into the primary register
791 /* If the offset is greater than 255, add the part that is > 255 to
792 * the primary. This way we get an easy addition and use the low byte
795 offs = MakeByteOffs (flags, offs);
797 /* Handle the indirect fetch */
798 switch (flags & CF_TYPE) {
801 /* Character sized */
804 if (flags & CF_UNSIGNED) {
805 AddCodeLine ("jsr ldauidx");
807 AddCodeLine ("jsr ldaidx");
810 if (flags & CF_UNSIGNED) {
811 if (CodeSizeFactor > 330) {
812 AddCodeLine ("sta ptr1");
813 AddCodeLine ("stx ptr1+1");
814 AddCodeLine ("ldy #$00");
815 AddCodeLine ("ldx #$00");
816 AddCodeLine ("lda (ptr1),y");
818 AddCodeLine ("jsr ldaui");
821 AddCodeLine ("jsr ldai");
827 if (flags & CF_TEST) {
829 AddCodeLine ("sta ptr1");
830 AddCodeLine ("stx ptr1+1");
831 AddCodeLine ("lda (ptr1),y");
833 AddCodeLine ("ora (ptr1),y");
836 AddCodeLine ("jsr ldaxi");
839 AddCodeLine ("jsr ldaxidx");
846 AddCodeLine ("jsr ldeaxi");
849 AddCodeLine ("jsr ldeaxidx");
851 if (flags & CF_TEST) {
852 AddCodeLine ("jsr tsteax");
864 void g_leasp (int offs)
865 /* Fetch the address of the specified symbol into the primary register */
867 /* Calculate the offset relative to sp */
870 /* For value 0 we do direct code */
872 AddCodeLine ("lda sp");
873 AddCodeLine ("ldx sp+1");
875 if (CodeSizeFactor < 300) {
876 ldaconst (offs); /* Load A with offset value */
877 AddCodeLine ("jsr leaasp"); /* Load effective address */
879 unsigned L = GetLocalLabel ();
880 if (CPU == CPU_65C02 && offs == 1) {
881 AddCodeLine ("lda sp");
882 AddCodeLine ("ldx sp+1");
884 AddCodeLine ("bne %s", LocalLabelName (L));
889 AddCodeLine ("ldx sp+1");
890 AddCodeLine ("adc sp");
891 AddCodeLine ("bcc %s", LocalLabelName (L));
901 void g_leavariadic (int Offs)
902 /* Fetch the address of a parameter in a variadic function into the primary
906 unsigned ArgSizeOffs;
908 /* Calculate the offset relative to sp */
911 /* Get the offset of the parameter which is stored at sp+0 on function
912 * entry and check if this offset is reachable with a byte offset.
915 ArgSizeOffs = -oursp;
916 CheckLocalOffs (ArgSizeOffs);
918 /* Get the size of all parameters. */
919 if (ArgSizeOffs == 0 && CPU == CPU_65C02) {
920 AddCodeLine ("lda (sp)");
922 ldyconst (ArgSizeOffs);
923 AddCodeLine ("lda (sp),y");
926 /* Add the value of the stackpointer */
927 if (CodeSizeFactor > 250) {
928 unsigned L = GetLocalLabel();
929 AddCodeLine ("ldx sp+1");
931 AddCodeLine ("adc sp");
932 AddCodeLine ("bcc %s", LocalLabelName (L));
936 AddCodeLine ("jsr leaasp");
939 /* Add the offset to the primary */
941 g_inc (CF_INT | CF_CONST, Offs);
942 } else if (Offs < 0) {
943 g_dec (CF_INT | CF_CONST, -Offs);
949 /*****************************************************************************/
950 /* Store into memory */
951 /*****************************************************************************/
955 void g_putstatic (unsigned flags, unsigned long label, unsigned offs)
956 /* Store the primary register into the specified static memory cell */
958 /* Create the correct label name */
959 char* lbuf = GetLabelName (flags, label, offs);
961 /* Check the size and generate the correct store operation */
962 switch (flags & CF_TYPE) {
965 AddCodeLine ("sta %s", lbuf);
969 AddCodeLine ("sta %s", lbuf);
970 AddCodeLine ("stx %s+1", lbuf);
974 AddCodeLine ("sta %s", lbuf);
975 AddCodeLine ("stx %s+1", lbuf);
976 AddCodeLine ("ldy sreg");
977 AddCodeLine ("sty %s+2", lbuf);
978 AddCodeLine ("ldy sreg+1");
979 AddCodeLine ("sty %s+3", lbuf);
990 void g_putlocal (unsigned Flags, int Offs, long Val)
991 /* Put data into local object. */
994 CheckLocalOffs (Offs);
995 switch (Flags & CF_TYPE) {
998 if (Flags & CF_CONST) {
999 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1001 if (CPU == CPU_65C02 && Offs == 0) {
1002 AddCodeLine ("sta (sp)");
1005 AddCodeLine ("sta (sp),y");
1010 if (Flags & CF_CONST) {
1012 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1013 AddCodeLine ("sta (sp),y");
1014 if ((Flags & CF_NOKEEP) == 0) {
1015 /* Place high byte into X */
1016 AddCodeLine ("tax");
1018 if (CPU == CPU_65C02 && Offs == 0) {
1019 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1020 AddCodeLine ("sta (sp)");
1022 if ((Val & 0xFF) == Offs+1) {
1023 /* The value we need is already in Y */
1024 AddCodeLine ("tya");
1025 AddCodeLine ("dey");
1027 AddCodeLine ("dey");
1028 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1030 AddCodeLine ("sta (sp),y");
1033 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
1036 AddCodeLine ("jsr staxysp");
1038 AddCodeLine ("jsr stax0sp");
1041 if (CPU == CPU_65C02 && Offs == 0) {
1042 AddCodeLine ("sta (sp)");
1044 AddCodeLine ("txa");
1045 AddCodeLine ("sta (sp),y");
1048 AddCodeLine ("sta (sp),y");
1049 AddCodeLine ("iny");
1050 AddCodeLine ("txa");
1051 AddCodeLine ("sta (sp),y");
1058 if (Flags & CF_CONST) {
1059 g_getimmed (Flags, Val, 0);
1063 AddCodeLine ("jsr steaxysp");
1065 AddCodeLine ("jsr steax0sp");
1077 void g_putind (unsigned Flags, unsigned Offs)
1078 /* Store the specified object type in the primary register at the address
1079 * on the top of the stack
1082 /* We can handle offsets below $100 directly, larger offsets must be added
1083 * to the address. Since a/x is in use, best code is achieved by adding
1084 * just the high byte. Be sure to check if the low byte will overflow while
1087 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1089 /* Overflow - we need to add the low byte also */
1090 AddCodeLine ("ldy #$00");
1091 AddCodeLine ("clc");
1092 AddCodeLine ("pha");
1093 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1094 AddCodeLine ("adc (sp),y");
1095 AddCodeLine ("sta (sp),y");
1096 AddCodeLine ("iny");
1097 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1098 AddCodeLine ("adc (sp),y");
1099 AddCodeLine ("sta (sp),y");
1100 AddCodeLine ("pla");
1102 /* Complete address is on stack, new offset is zero */
1105 } else if ((Offs & 0xFF00) != 0) {
1107 /* We can just add the high byte */
1108 AddCodeLine ("ldy #$01");
1109 AddCodeLine ("clc");
1110 AddCodeLine ("pha");
1111 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1112 AddCodeLine ("adc (sp),y");
1113 AddCodeLine ("sta (sp),y");
1114 AddCodeLine ("pla");
1116 /* Offset is now just the low byte */
1120 /* Check the size and determine operation */
1121 switch (Flags & CF_TYPE) {
1126 AddCodeLine ("jsr staspidx");
1128 AddCodeLine ("jsr staspp");
1135 AddCodeLine ("jsr staxspidx");
1137 AddCodeLine ("jsr staxspp");
1144 AddCodeLine ("jsr steaxspidx");
1146 AddCodeLine ("jsr steaxspp");
1155 /* Pop the argument which is always a pointer */
1161 /*****************************************************************************/
1162 /* type conversion and similiar stuff */
1163 /*****************************************************************************/
1167 void g_toslong (unsigned flags)
1168 /* Make sure, the value on TOS is a long. Convert if necessary */
1170 switch (flags & CF_TYPE) {
1174 if (flags & CF_UNSIGNED) {
1175 AddCodeLine ("jsr tosulong");
1177 AddCodeLine ("jsr toslong");
1192 void g_tosint (unsigned flags)
1193 /* Make sure, the value on TOS is an int. Convert if necessary */
1195 switch (flags & CF_TYPE) {
1202 AddCodeLine ("jsr tosint");
1213 void g_reglong (unsigned flags)
1214 /* Make sure, the value in the primary register a long. Convert if necessary */
1216 switch (flags & CF_TYPE) {
1220 if (flags & CF_UNSIGNED) {
1221 if (CodeSizeFactor >= 200) {
1223 AddCodeLine ("sty sreg");
1224 AddCodeLine ("sty sreg+1");
1226 AddCodeLine ("jsr axulong");
1229 AddCodeLine ("jsr axlong");
1243 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1244 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1245 * value, that corresponds to the value on TOS, rhs corresponds to the value
1246 * in (e)ax. The return value is the the flags value for the resulting type.
1249 unsigned ltype, rtype;
1252 /* Get the type spec from the flags */
1253 ltype = lhs & CF_TYPE;
1254 rtype = rhs & CF_TYPE;
1256 /* Check if a conversion is needed */
1257 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1258 /* We must promote the primary register to long */
1260 /* Get the new rhs type */
1261 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1263 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1264 /* We must promote the lhs to long */
1270 /* Get the new rhs type */
1271 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1275 /* Determine the result type for the operation:
1276 * - The result is const if both operands are const.
1277 * - The result is unsigned if one of the operands is unsigned.
1278 * - The result is long if one of the operands is long.
1279 * - Otherwise the result is int sized.
1281 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1282 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1283 if (rtype == CF_LONG || ltype == CF_LONG) {
1293 unsigned g_typecast (unsigned lhs, unsigned rhs)
1294 /* Cast the value in the primary register to the operand size that is flagged
1295 * by the lhs value. Return the result value.
1298 unsigned ltype, rtype;
1300 /* Get the type spec from the flags */
1301 ltype = lhs & CF_TYPE;
1302 rtype = rhs & CF_TYPE;
1304 /* Check if a conversion is needed */
1305 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1306 /* We must promote the primary register to long */
1310 /* Do not need any other action. If the left type is int, and the primary
1311 * register is long, it will be automagically truncated. If the right hand
1312 * side is const, it is not located in the primary register and handled by
1313 * the expression parser code.
1316 /* Result is const if the right hand side was const */
1317 lhs |= (rhs & CF_CONST);
1319 /* The resulting type is that of the left hand side (that's why you called
1327 void g_scale (unsigned flags, long val)
1328 /* Scale the value in the primary register by the given value. If val is positive,
1329 * scale up, is val is negative, scale down. This function is used to scale
1330 * the operands or results of pointer arithmetic by the size of the type, the
1331 * pointer points to.
1336 /* Value may not be zero */
1338 Internal ("Data type has no size");
1339 } else if (val > 0) {
1342 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1344 /* Factor is 2, 4 or 8, use special function */
1345 switch (flags & CF_TYPE) {
1348 if (flags & CF_FORCECHAR) {
1350 AddCodeLine ("asl a");
1357 if (CodeSizeFactor >= (p2+1)*130U) {
1358 AddCodeLine ("stx tmp1");
1360 AddCodeLine ("asl a");
1361 AddCodeLine ("rol tmp1");
1363 AddCodeLine ("ldx tmp1");
1365 if (flags & CF_UNSIGNED) {
1366 AddCodeLine ("jsr shlax%d", p2);
1368 AddCodeLine ("jsr aslax%d", p2);
1374 if (flags & CF_UNSIGNED) {
1375 AddCodeLine ("jsr shleax%d", p2);
1377 AddCodeLine ("jsr asleax%d", p2);
1386 } else if (val != 1) {
1388 /* Use a multiplication instead */
1389 g_mul (flags | CF_CONST, val);
1397 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1399 /* Factor is 2, 4 or 8, use special function */
1400 switch (flags & CF_TYPE) {
1403 if (flags & CF_FORCECHAR) {
1404 if (flags & CF_UNSIGNED) {
1406 AddCodeLine ("lsr a");
1409 } else if (p2 <= 2) {
1410 AddCodeLine ("cmp #$80");
1411 AddCodeLine ("ror a");
1418 if (flags & CF_UNSIGNED) {
1419 if (CodeSizeFactor >= (p2+1)*130U) {
1420 AddCodeLine ("stx tmp1");
1422 AddCodeLine ("lsr tmp1");
1423 AddCodeLine ("ror a");
1425 AddCodeLine ("ldx tmp1");
1427 AddCodeLine ("jsr lsrax%d", p2);
1430 if (CodeSizeFactor >= (p2+1)*150U) {
1431 AddCodeLine ("stx tmp1");
1433 AddCodeLine ("cpx #$80");
1434 AddCodeLine ("ror tmp1");
1435 AddCodeLine ("ror a");
1437 AddCodeLine ("ldx tmp1");
1439 AddCodeLine ("jsr asrax%d", p2);
1445 if (flags & CF_UNSIGNED) {
1446 AddCodeLine ("jsr lsreax%d", p2);
1448 AddCodeLine ("jsr asreax%d", p2);
1457 } else if (val != 1) {
1459 /* Use a division instead */
1460 g_div (flags | CF_CONST, val);
1468 /*****************************************************************************/
1469 /* Adds and subs of variables fix a fixed address */
1470 /*****************************************************************************/
1474 void g_addlocal (unsigned flags, int offs)
1475 /* Add a local variable to ax */
1479 /* Correct the offset and check it */
1481 CheckLocalOffs (offs);
1483 switch (flags & CF_TYPE) {
1486 L = GetLocalLabel();
1487 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1488 AddCodeLine ("clc");
1489 AddCodeLine ("adc (sp),y");
1490 AddCodeLine ("bcc %s", LocalLabelName (L));
1491 AddCodeLine ("inx");
1496 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1497 AddCodeLine ("clc");
1498 AddCodeLine ("adc (sp),y");
1499 AddCodeLine ("pha");
1500 AddCodeLine ("txa");
1501 AddCodeLine ("iny");
1502 AddCodeLine ("adc (sp),y");
1503 AddCodeLine ("tax");
1504 AddCodeLine ("pla");
1508 /* Do it the old way */
1510 g_getlocal (flags, offs);
1522 void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
1523 /* Add a static variable to ax */
1527 /* Create the correct label name */
1528 char* lbuf = GetLabelName (flags, label, offs);
1530 switch (flags & CF_TYPE) {
1533 L = GetLocalLabel();
1534 AddCodeLine ("clc");
1535 AddCodeLine ("adc %s", lbuf);
1536 AddCodeLine ("bcc %s", LocalLabelName (L));
1537 AddCodeLine ("inx");
1542 AddCodeLine ("clc");
1543 AddCodeLine ("adc %s", lbuf);
1544 AddCodeLine ("tay");
1545 AddCodeLine ("txa");
1546 AddCodeLine ("adc %s+1", lbuf);
1547 AddCodeLine ("tax");
1548 AddCodeLine ("tya");
1552 /* Do it the old way */
1554 g_getstatic (flags, label, offs);
1566 /*****************************************************************************/
1567 /* Compares of ax with a variable with fixed address */
1568 /*****************************************************************************/
1572 void g_cmplocal (unsigned flags, int offs)
1573 /* Compare a local variable to ax */
1575 Internal ("g_cmplocal not implemented");
1580 void g_cmpstatic (unsigned flags, unsigned label, unsigned offs)
1581 /* Compare a static variable to ax */
1583 Internal ("g_cmpstatic not implemented");
1588 /*****************************************************************************/
1589 /* Special op= functions */
1590 /*****************************************************************************/
1594 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
1596 /* Emit += for a static variable */
1598 /* Create the correct label name */
1599 char* lbuf = GetLabelName (flags, label, offs);
1601 /* Check the size and determine operation */
1602 switch (flags & CF_TYPE) {
1605 if (flags & CF_FORCECHAR) {
1606 AddCodeLine ("ldx #$00");
1607 if (flags & CF_CONST) {
1609 AddCodeLine ("inc %s", lbuf);
1610 AddCodeLine ("lda %s", lbuf);
1612 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1613 AddCodeLine ("clc");
1614 AddCodeLine ("adc %s", lbuf);
1615 AddCodeLine ("sta %s", lbuf);
1618 AddCodeLine ("clc");
1619 AddCodeLine ("adc %s", lbuf);
1620 AddCodeLine ("sta %s", lbuf);
1622 if ((flags & CF_UNSIGNED) == 0) {
1623 unsigned L = GetLocalLabel();
1624 AddCodeLine ("bpl %s", LocalLabelName (L));
1625 AddCodeLine ("dex");
1633 if (flags & CF_CONST) {
1635 unsigned L = GetLocalLabel ();
1636 AddCodeLine ("inc %s", lbuf);
1637 AddCodeLine ("bne %s", LocalLabelName (L));
1638 AddCodeLine ("inc %s+1", lbuf);
1640 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1641 AddCodeLine ("ldx %s+1", lbuf);
1643 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1644 AddCodeLine ("clc");
1645 AddCodeLine ("adc %s", lbuf);
1646 AddCodeLine ("sta %s", lbuf);
1648 unsigned L = GetLocalLabel ();
1649 AddCodeLine ("bcc %s", LocalLabelName (L));
1650 AddCodeLine ("inc %s+1", lbuf);
1652 AddCodeLine ("ldx %s+1", lbuf);
1654 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1655 AddCodeLine ("adc %s+1", lbuf);
1656 AddCodeLine ("sta %s+1", lbuf);
1657 AddCodeLine ("tax");
1658 AddCodeLine ("lda %s", lbuf);
1662 AddCodeLine ("clc");
1663 AddCodeLine ("adc %s", lbuf);
1664 AddCodeLine ("sta %s", lbuf);
1665 AddCodeLine ("txa");
1666 AddCodeLine ("adc %s+1", lbuf);
1667 AddCodeLine ("sta %s+1", lbuf);
1668 AddCodeLine ("tax");
1669 AddCodeLine ("lda %s", lbuf);
1674 if (flags & CF_CONST) {
1676 AddCodeLine ("ldy #<(%s)", lbuf);
1677 AddCodeLine ("sty ptr1");
1678 AddCodeLine ("ldy #>(%s+1)", lbuf);
1680 AddCodeLine ("jsr laddeq1");
1682 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1683 AddCodeLine ("jsr laddeqa");
1686 g_getstatic (flags, label, offs);
1688 g_putstatic (flags, label, offs);
1691 AddCodeLine ("ldy #<(%s)", lbuf);
1692 AddCodeLine ("sty ptr1");
1693 AddCodeLine ("ldy #>(%s+1)", lbuf);
1694 AddCodeLine ("jsr laddeq");
1705 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1706 /* Emit += for a local variable */
1708 /* Calculate the true offset, check it, load it into Y */
1710 CheckLocalOffs (offs);
1712 /* Check the size and determine operation */
1713 switch (flags & CF_TYPE) {
1716 if (flags & CF_FORCECHAR) {
1718 AddCodeLine ("ldx #$00");
1719 if (flags & CF_CONST) {
1720 AddCodeLine ("clc");
1721 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1722 AddCodeLine ("adc (sp),y");
1723 AddCodeLine ("sta (sp),y");
1725 AddCodeLine ("clc");
1726 AddCodeLine ("adc (sp),y");
1727 AddCodeLine ("sta (sp),y");
1729 if ((flags & CF_UNSIGNED) == 0) {
1730 unsigned L = GetLocalLabel();
1731 AddCodeLine ("bpl %s", LocalLabelName (L));
1732 AddCodeLine ("dex");
1740 if (flags & CF_CONST) {
1741 g_getimmed (flags, val, 0);
1744 AddCodeLine ("jsr addeq0sp");
1747 AddCodeLine ("jsr addeqysp");
1752 if (flags & CF_CONST) {
1753 g_getimmed (flags, val, 0);
1756 AddCodeLine ("jsr laddeq0sp");
1759 AddCodeLine ("jsr laddeqysp");
1770 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1771 /* Emit += for the location with address in ax */
1773 /* If the offset is too large for a byte register, add the high byte
1774 * of the offset to the primary. Beware: We need a special correction
1775 * if the offset in the low byte will overflow in the operation.
1777 offs = MakeByteOffs (flags, offs);
1779 /* Check the size and determine operation */
1780 switch (flags & CF_TYPE) {
1783 AddCodeLine ("sta ptr1");
1784 AddCodeLine ("stx ptr1+1");
1785 AddCodeLine ("ldy #$%02X", offs);
1786 AddCodeLine ("ldx #$00");
1787 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1788 AddCodeLine ("clc");
1789 AddCodeLine ("adc (ptr1),y");
1790 AddCodeLine ("sta (ptr1),y");
1794 if (CodeSizeFactor >= 200) {
1795 /* Lots of code, use only if size is not important */
1796 AddCodeLine ("sta ptr1");
1797 AddCodeLine ("stx ptr1+1");
1798 AddCodeLine ("ldy #$%02X", offs);
1799 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1800 AddCodeLine ("clc");
1801 AddCodeLine ("adc (ptr1),y");
1802 AddCodeLine ("sta (ptr1),y");
1803 AddCodeLine ("pha");
1804 AddCodeLine ("iny");
1805 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1806 AddCodeLine ("adc (ptr1),y");
1807 AddCodeLine ("sta (ptr1),y");
1808 AddCodeLine ("tax");
1809 AddCodeLine ("pla");
1815 AddCodeLine ("jsr pushax"); /* Push the address */
1816 push (flags); /* Correct the internal sp */
1817 g_getind (flags, offs); /* Fetch the value */
1818 g_inc (flags, val); /* Increment value in primary */
1819 g_putind (flags, offs); /* Store the value back */
1829 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1831 /* Emit -= for a static variable */
1833 /* Create the correct label name */
1834 char* lbuf = GetLabelName (flags, label, offs);
1836 /* Check the size and determine operation */
1837 switch (flags & CF_TYPE) {
1840 if (flags & CF_FORCECHAR) {
1841 AddCodeLine ("ldx #$00");
1842 if (flags & CF_CONST) {
1844 AddCodeLine ("dec %s", lbuf);
1845 AddCodeLine ("lda %s", lbuf);
1847 AddCodeLine ("sec");
1848 AddCodeLine ("lda %s", lbuf);
1849 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1850 AddCodeLine ("sta %s", lbuf);
1853 AddCodeLine ("sec");
1854 AddCodeLine ("sta tmp1");
1855 AddCodeLine ("lda %s", lbuf);
1856 AddCodeLine ("sbc tmp1");
1857 AddCodeLine ("sta %s", lbuf);
1859 if ((flags & CF_UNSIGNED) == 0) {
1860 unsigned L = GetLocalLabel();
1861 AddCodeLine ("bpl %s", LocalLabelName (L));
1862 AddCodeLine ("dex");
1870 AddCodeLine ("sec");
1871 if (flags & CF_CONST) {
1872 AddCodeLine ("lda %s", lbuf);
1873 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1874 AddCodeLine ("sta %s", lbuf);
1876 unsigned L = GetLocalLabel ();
1877 AddCodeLine ("bcs %s", LocalLabelName (L));
1878 AddCodeLine ("dec %s+1", lbuf);
1880 AddCodeLine ("ldx %s+1", lbuf);
1882 AddCodeLine ("lda %s+1", lbuf);
1883 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1884 AddCodeLine ("sta %s+1", lbuf);
1885 AddCodeLine ("tax");
1886 AddCodeLine ("lda %s", lbuf);
1889 AddCodeLine ("sta tmp1");
1890 AddCodeLine ("lda %s", lbuf);
1891 AddCodeLine ("sbc tmp1");
1892 AddCodeLine ("sta %s", lbuf);
1893 AddCodeLine ("stx tmp1");
1894 AddCodeLine ("lda %s+1", lbuf);
1895 AddCodeLine ("sbc tmp1");
1896 AddCodeLine ("sta %s+1", lbuf);
1897 AddCodeLine ("tax");
1898 AddCodeLine ("lda %s", lbuf);
1903 if (flags & CF_CONST) {
1905 AddCodeLine ("ldy #<(%s)", lbuf);
1906 AddCodeLine ("sty ptr1");
1907 AddCodeLine ("ldy #>(%s+1)", lbuf);
1909 AddCodeLine ("jsr lsubeq1");
1911 AddCodeLine ("lda #$%02X", (unsigned char)val);
1912 AddCodeLine ("jsr lsubeqa");
1915 g_getstatic (flags, label, offs);
1917 g_putstatic (flags, label, offs);
1920 AddCodeLine ("ldy #<(%s)", lbuf);
1921 AddCodeLine ("sty ptr1");
1922 AddCodeLine ("ldy #>(%s+1)", lbuf);
1923 AddCodeLine ("jsr lsubeq");
1934 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1935 /* Emit -= for a local variable */
1937 /* Calculate the true offset, check it, load it into Y */
1939 CheckLocalOffs (offs);
1941 /* Check the size and determine operation */
1942 switch (flags & CF_TYPE) {
1945 if (flags & CF_FORCECHAR) {
1947 AddCodeLine ("ldx #$00");
1948 AddCodeLine ("sec");
1949 if (flags & CF_CONST) {
1950 AddCodeLine ("lda (sp),y");
1951 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1953 AddCodeLine ("sta tmp1");
1954 AddCodeLine ("lda (sp),y");
1955 AddCodeLine ("sbc tmp1");
1957 AddCodeLine ("sta (sp),y");
1958 if ((flags & CF_UNSIGNED) == 0) {
1959 unsigned L = GetLocalLabel();
1960 AddCodeLine ("bpl %s", LocalLabelName (L));
1961 AddCodeLine ("dex");
1969 if (flags & CF_CONST) {
1970 g_getimmed (flags, val, 0);
1973 AddCodeLine ("jsr subeq0sp");
1976 AddCodeLine ("jsr subeqysp");
1981 if (flags & CF_CONST) {
1982 g_getimmed (flags, val, 0);
1985 AddCodeLine ("jsr lsubeq0sp");
1988 AddCodeLine ("jsr lsubeqysp");
1999 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2000 /* Emit -= for the location with address in ax */
2002 /* If the offset is too large for a byte register, add the high byte
2003 * of the offset to the primary. Beware: We need a special correction
2004 * if the offset in the low byte will overflow in the operation.
2006 offs = MakeByteOffs (flags, offs);
2008 /* Check the size and determine operation */
2009 switch (flags & CF_TYPE) {
2012 AddCodeLine ("sta ptr1");
2013 AddCodeLine ("stx ptr1+1");
2014 AddCodeLine ("ldy #$%02X", offs);
2015 AddCodeLine ("ldx #$00");
2016 AddCodeLine ("lda (ptr1),y");
2017 AddCodeLine ("sec");
2018 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2019 AddCodeLine ("sta (ptr1),y");
2023 if (CodeSizeFactor >= 200) {
2024 /* Lots of code, use only if size is not important */
2025 AddCodeLine ("sta ptr1");
2026 AddCodeLine ("stx ptr1+1");
2027 AddCodeLine ("ldy #$%02X", offs);
2028 AddCodeLine ("lda (ptr1),y");
2029 AddCodeLine ("sec");
2030 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2031 AddCodeLine ("sta (ptr1),y");
2032 AddCodeLine ("pha");
2033 AddCodeLine ("iny");
2034 AddCodeLine ("lda (ptr1),y");
2035 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2036 AddCodeLine ("sta (ptr1),y");
2037 AddCodeLine ("tax");
2038 AddCodeLine ("pla");
2044 AddCodeLine ("jsr pushax"); /* Push the address */
2045 push (flags); /* Correct the internal sp */
2046 g_getind (flags, offs); /* Fetch the value */
2047 g_dec (flags, val); /* Increment value in primary */
2048 g_putind (flags, offs); /* Store the value back */
2058 /*****************************************************************************/
2059 /* Add a variable address to the value in ax */
2060 /*****************************************************************************/
2064 void g_addaddr_local (unsigned flags, int offs)
2065 /* Add the address of a local variable to ax */
2069 /* Add the offset */
2072 /* We cannot address more then 256 bytes of locals anyway */
2073 L = GetLocalLabel();
2074 CheckLocalOffs (offs);
2075 AddCodeLine ("clc");
2076 AddCodeLine ("adc #$%02X", offs & 0xFF);
2077 /* Do also skip the CLC insn below */
2078 AddCodeLine ("bcc %s", LocalLabelName (L));
2079 AddCodeLine ("inx");
2082 /* Add the current stackpointer value */
2083 AddCodeLine ("clc");
2085 /* Label was used above */
2088 AddCodeLine ("adc sp");
2089 AddCodeLine ("tay");
2090 AddCodeLine ("txa");
2091 AddCodeLine ("adc sp+1");
2092 AddCodeLine ("tax");
2093 AddCodeLine ("tya");
2098 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2099 /* Add the address of a static variable to ax */
2101 /* Create the correct label name */
2102 char* lbuf = GetLabelName (flags, label, offs);
2104 /* Add the address to the current ax value */
2105 AddCodeLine ("clc");
2106 AddCodeLine ("adc #<(%s)", lbuf);
2107 AddCodeLine ("tay");
2108 AddCodeLine ("txa");
2109 AddCodeLine ("adc #>(%s)", lbuf);
2110 AddCodeLine ("tax");
2111 AddCodeLine ("tya");
2116 /*****************************************************************************/
2118 /*****************************************************************************/
2122 void g_save (unsigned flags)
2123 /* Copy primary register to hold register. */
2125 /* Check the size and determine operation */
2126 switch (flags & CF_TYPE) {
2129 if (flags & CF_FORCECHAR) {
2130 AddCodeLine ("pha");
2136 AddCodeLine ("sta regsave");
2137 AddCodeLine ("stx regsave+1");
2141 AddCodeLine ("jsr saveeax");
2151 void g_restore (unsigned flags)
2152 /* Copy hold register to primary. */
2154 /* Check the size and determine operation */
2155 switch (flags & CF_TYPE) {
2158 if (flags & CF_FORCECHAR) {
2159 AddCodeLine ("pla");
2165 AddCodeLine ("lda regsave");
2166 AddCodeLine ("ldx regsave+1");
2170 AddCodeLine ("jsr resteax");
2180 void g_cmp (unsigned flags, unsigned long val)
2181 /* Immidiate compare. The primary register will not be changed, Z flag
2187 /* Check the size and determine operation */
2188 switch (flags & CF_TYPE) {
2191 if (flags & CF_FORCECHAR) {
2192 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2198 L = GetLocalLabel();
2199 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2200 AddCodeLine ("bne %s", LocalLabelName (L));
2201 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2206 Internal ("g_cmp: Long compares not implemented");
2216 static void oper (unsigned flags, unsigned long val, char** subs)
2217 /* Encode a binary operation. subs is a pointer to four groups of three
2219 * 0-2 --> Operate on ints
2220 * 3-5 --> Operate on unsigneds
2221 * 6-8 --> Operate on longs
2222 * 9-11 --> Operate on unsigned longs
2224 * The first subroutine names in each string group is used to encode an
2225 * operation with a zero constant, the second to encode an operation with
2226 * a 8 bit constant, and the third is used in all other cases.
2231 /* Determine the offset into the array */
2232 offs = (flags & CF_UNSIGNED)? 3 : 0;
2233 switch (flags & CF_TYPE) {
2246 /* Encode the operation */
2247 if (flags & CF_CONST) {
2248 /* Constant value given */
2249 if (val == 0 && subs [offs+0]) {
2250 /* Special case: constant with value zero */
2251 AddCodeLine ("jsr %s", subs [offs+0]);
2252 } else if (val < 0x100 && subs [offs+1]) {
2253 /* Special case: constant with high byte zero */
2254 ldaconst (val); /* Load low byte */
2255 AddCodeLine ("jsr %s", subs [offs+1]);
2257 /* Others: arbitrary constant value */
2258 g_getimmed (flags, val, 0); /* Load value */
2259 AddCodeLine ("jsr %s", subs [offs+2]);
2262 /* Value not constant (is already in (e)ax) */
2263 AddCodeLine ("jsr %s", subs [offs+2]);
2266 /* The operation will pop it's argument */
2272 void g_test (unsigned flags)
2273 /* Test the value in the primary and set the condition codes */
2275 switch (flags & CF_TYPE) {
2278 if (flags & CF_FORCECHAR) {
2279 AddCodeLine ("tax");
2285 AddCodeLine ("stx tmp1");
2286 AddCodeLine ("ora tmp1");
2290 if (flags & CF_UNSIGNED) {
2291 AddCodeLine ("jsr utsteax");
2293 AddCodeLine ("jsr tsteax");
2305 void g_push (unsigned flags, unsigned long val)
2306 /* Push the primary register or a constant value onto the stack */
2310 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2312 /* We have a constant 8 or 16 bit value */
2313 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2315 /* Handle as 8 bit value */
2316 if (CodeSizeFactor >= 165 || val > 2) {
2318 AddCodeLine ("jsr pusha");
2320 AddCodeLine ("jsr pushc%d", (int) val);
2325 /* Handle as 16 bit value */
2326 hi = (unsigned char) (val >> 8);
2328 AddCodeLine ("jsr push%u", (unsigned) val);
2329 } else if (hi == 0 || hi == 0xFF) {
2330 /* Use special function */
2332 AddCodeLine ("jsr %s", (hi == 0)? "pusha0" : "pushaFF");
2335 g_getimmed (flags, val, 0);
2336 AddCodeLine ("jsr pushax");
2342 /* Value is not 16 bit or not constant */
2343 if (flags & CF_CONST) {
2344 /* Constant 32 bit value, load into eax */
2345 g_getimmed (flags, val, 0);
2348 /* Push the primary register */
2349 switch (flags & CF_TYPE) {
2352 if (flags & CF_FORCECHAR) {
2353 /* Handle as char */
2354 AddCodeLine ("jsr pusha");
2359 AddCodeLine ("jsr pushax");
2363 AddCodeLine ("jsr pusheax");
2373 /* Adjust the stack offset */
2379 void g_swap (unsigned flags)
2380 /* Swap the primary register and the top of the stack. flags give the type
2381 * of *both* values (must have same size).
2384 switch (flags & CF_TYPE) {
2388 AddCodeLine ("jsr swapstk");
2392 AddCodeLine ("jsr swapestk");
2403 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2404 /* Call the specified subroutine name */
2406 if ((Flags & CF_FIXARGC) == 0) {
2407 /* Pass the argument count */
2410 AddCodeLine ("jsr _%s", Label);
2411 oursp += ArgSize; /* callee pops args */
2416 void g_callind (unsigned Flags, unsigned ArgSize)
2417 /* Call subroutine with address in AX */
2419 if ((Flags & CF_FIXARGC) == 0) {
2420 /* Pass arg count */
2423 AddCodeLine ("jsr callax"); /* do the call */
2424 oursp += ArgSize; /* callee pops args */
2429 void g_jump (unsigned Label)
2430 /* Jump to specified internal label number */
2432 AddCodeLine ("jmp %s", LocalLabelName (Label));
2437 void g_switch (unsigned Flags)
2438 /* Output switch statement preamble */
2440 switch (Flags & CF_TYPE) {
2444 AddCodeLine ("jsr switch");
2448 AddCodeLine ("jsr lswitch");
2459 void g_case (unsigned flags, unsigned label, unsigned long val)
2460 /* Create table code for one case selector */
2462 switch (flags & CF_TYPE) {
2466 AddCodeLine (".word $%04X, %s",
2467 (unsigned)(val & 0xFFFF),
2468 LocalLabelName (label));
2472 AddCodeLine (".dword $%08lX", val);
2473 AddCodeLine (".word %s", LocalLabelName (label));
2484 void g_truejump (unsigned flags, unsigned label)
2485 /* Jump to label if zero flag clear */
2487 AddCodeLine ("jne %s", LocalLabelName (label));
2492 void g_falsejump (unsigned flags, unsigned label)
2493 /* Jump to label if zero flag set */
2495 AddCodeLine ("jeq %s", LocalLabelName (label));
2500 static void mod_internal (int k, char* verb1, char* verb2)
2503 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2507 AddCodeLine ("jsr %ssp", verb2);
2513 void g_space (int space)
2514 /* Create or drop space on the stack */
2517 mod_internal (-space, "inc", "addy");
2518 } else if (space > 0) {
2519 mod_internal (space, "dec", "suby");
2525 void g_cstackcheck (void)
2526 /* Check for a C stack overflow */
2528 AddCodeLine ("jsr cstkchk");
2533 void g_stackcheck (void)
2534 /* Check for a stack overflow */
2536 AddCodeLine ("jsr stkchk");
2541 void g_add (unsigned flags, unsigned long val)
2542 /* Primary = TOS + Primary */
2544 static char* ops [12] = {
2545 0, "tosadda0", "tosaddax",
2546 0, "tosadda0", "tosaddax",
2551 if (flags & CF_CONST) {
2552 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2553 g_push (flags & ~CF_CONST, 0);
2555 oper (flags, val, ops);
2560 void g_sub (unsigned flags, unsigned long val)
2561 /* Primary = TOS - Primary */
2563 static char* ops [12] = {
2564 0, "tossuba0", "tossubax",
2565 0, "tossuba0", "tossubax",
2570 if (flags & CF_CONST) {
2571 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2572 g_push (flags & ~CF_CONST, 0);
2574 oper (flags, val, ops);
2579 void g_rsub (unsigned flags, unsigned long val)
2580 /* Primary = Primary - TOS */
2582 static char* ops [12] = {
2583 0, "tosrsuba0", "tosrsubax",
2584 0, "tosrsuba0", "tosrsubax",
2588 oper (flags, val, ops);
2593 void g_mul (unsigned flags, unsigned long val)
2594 /* Primary = TOS * Primary */
2596 static char* ops [12] = {
2597 0, "tosmula0", "tosmulax",
2598 0, "tosumula0", "tosumulax",
2605 /* Do strength reduction if the value is constant and a power of two */
2606 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2607 /* Generate a shift instead */
2612 /* If the right hand side is const, the lhs is not on stack but still
2613 * in the primary register.
2615 if (flags & CF_CONST) {
2617 switch (flags & CF_TYPE) {
2620 if (flags & CF_FORCECHAR) {
2621 /* Handle some special cases */
2625 AddCodeLine ("sta tmp1");
2626 AddCodeLine ("asl a");
2627 AddCodeLine ("clc");
2628 AddCodeLine ("adc tmp1");
2632 AddCodeLine ("sta tmp1");
2633 AddCodeLine ("asl a");
2634 AddCodeLine ("asl a");
2635 AddCodeLine ("clc");
2636 AddCodeLine ("adc tmp1");
2640 AddCodeLine ("sta tmp1");
2641 AddCodeLine ("asl a");
2642 AddCodeLine ("asl a");
2643 AddCodeLine ("clc");
2644 AddCodeLine ("adc tmp1");
2645 AddCodeLine ("asl a");
2661 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2662 * into the normal, non-optimized stuff.
2664 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2665 g_push (flags & ~CF_CONST, 0);
2669 /* Use long way over the stack */
2670 oper (flags, val, ops);
2675 void g_div (unsigned flags, unsigned long val)
2676 /* Primary = TOS / Primary */
2678 static char* ops [12] = {
2679 0, "tosdiva0", "tosdivax",
2680 0, "tosudiva0", "tosudivax",
2685 /* Do strength reduction if the value is constant and a power of two */
2687 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2688 /* Generate a shift instead */
2691 /* Generate a division */
2692 if (flags & CF_CONST) {
2693 /* lhs is not on stack */
2694 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2695 g_push (flags & ~CF_CONST, 0);
2697 oper (flags, val, ops);
2703 void g_mod (unsigned flags, unsigned long val)
2704 /* Primary = TOS % Primary */
2706 static char* ops [12] = {
2707 0, "tosmoda0", "tosmodax",
2708 0, "tosumoda0", "tosumodax",
2714 /* Check if we can do some cost reduction */
2715 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2716 /* We can do that with an AND operation */
2717 g_and (flags, val - 1);
2719 /* Do it the hard way... */
2720 if (flags & CF_CONST) {
2721 /* lhs is not on stack */
2722 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2723 g_push (flags & ~CF_CONST, 0);
2725 oper (flags, val, ops);
2731 void g_or (unsigned flags, unsigned long val)
2732 /* Primary = TOS | Primary */
2734 static char* ops [12] = {
2735 0, "tosora0", "tosorax",
2736 0, "tosora0", "tosorax",
2741 /* If the right hand side is const, the lhs is not on stack but still
2742 * in the primary register.
2744 if (flags & CF_CONST) {
2746 switch (flags & CF_TYPE) {
2749 if (flags & CF_FORCECHAR) {
2750 if ((val & 0xFF) != 0xFF) {
2751 AddCodeLine ("ora #$%02X", (unsigned char)val);
2759 AddCodeLine ("ora #$%02X", (unsigned char)val);
2766 AddCodeLine ("ora #$%02X", (unsigned char)val);
2775 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2776 * into the normal, non-optimized stuff.
2778 g_push (flags & ~CF_CONST, 0);
2782 /* Use long way over the stack */
2783 oper (flags, val, ops);
2788 void g_xor (unsigned flags, unsigned long val)
2789 /* Primary = TOS ^ Primary */
2791 static char* ops [12] = {
2792 0, "tosxora0", "tosxorax",
2793 0, "tosxora0", "tosxorax",
2799 /* If the right hand side is const, the lhs is not on stack but still
2800 * in the primary register.
2802 if (flags & CF_CONST) {
2804 switch (flags & CF_TYPE) {
2807 if (flags & CF_FORCECHAR) {
2808 if ((val & 0xFF) != 0) {
2809 AddCodeLine ("eor #$%02X", (unsigned char)val);
2818 AddCodeLine ("eor #$%02X", (unsigned char)val);
2821 } else if ((val & 0xFF) == 0) {
2822 AddCodeLine ("pha");
2823 AddCodeLine ("txa");
2824 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2825 AddCodeLine ("tax");
2826 AddCodeLine ("pla");
2834 AddCodeLine ("eor #$%02X", (unsigned char)val);
2844 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2845 * into the normal, non-optimized stuff.
2847 g_push (flags & ~CF_CONST, 0);
2851 /* Use long way over the stack */
2852 oper (flags, val, ops);
2857 void g_and (unsigned flags, unsigned long val)
2858 /* Primary = TOS & Primary */
2860 static char* ops [12] = {
2861 0, "tosanda0", "tosandax",
2862 0, "tosanda0", "tosandax",
2867 /* If the right hand side is const, the lhs is not on stack but still
2868 * in the primary register.
2870 if (flags & CF_CONST) {
2872 switch (flags & CF_TYPE) {
2875 if (flags & CF_FORCECHAR) {
2876 AddCodeLine ("and #$%02X", (unsigned char)val);
2881 if ((val & 0xFFFF) != 0xFFFF) {
2886 } else if (val != 0xFF) {
2887 AddCodeLine ("and #$%02X", (unsigned char)val);
2889 } else if ((val & 0xFF00) == 0xFF00) {
2890 AddCodeLine ("and #$%02X", (unsigned char)val);
2891 } else if ((val & 0x00FF) == 0x0000) {
2892 AddCodeLine ("txa");
2893 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2894 AddCodeLine ("tax");
2897 AddCodeLine ("tay");
2898 AddCodeLine ("txa");
2899 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2900 AddCodeLine ("tax");
2901 AddCodeLine ("tya");
2902 if ((val & 0x00FF) != 0x00FF) {
2903 AddCodeLine ("and #$%02X", (unsigned char)val);
2912 AddCodeLine ("stx sreg+1");
2913 AddCodeLine ("stx sreg");
2914 if ((val & 0xFF) != 0xFF) {
2915 AddCodeLine ("and #$%02X", (unsigned char)val);
2918 } else if (val == 0xFF00) {
2920 AddCodeLine ("sta sreg+1");
2921 AddCodeLine ("sta sreg");
2930 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2931 * into the normal, non-optimized stuff.
2933 g_push (flags & ~CF_CONST, 0);
2937 /* Use long way over the stack */
2938 oper (flags, val, ops);
2943 void g_asr (unsigned flags, unsigned long val)
2944 /* Primary = TOS >> Primary */
2946 static char* ops [12] = {
2947 0, "tosasra0", "tosasrax",
2948 0, "tosshra0", "tosshrax",
2953 /* If the right hand side is const, the lhs is not on stack but still
2954 * in the primary register.
2956 if (flags & CF_CONST) {
2958 switch (flags & CF_TYPE) {
2962 if (val >= 1 && val <= 3) {
2963 if (flags & CF_UNSIGNED) {
2964 AddCodeLine ("jsr shrax%ld", val);
2966 AddCodeLine ("jsr asrax%ld", val);
2969 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2970 AddCodeLine ("txa");
2977 if (val >= 1 && val <= 3) {
2978 if (flags & CF_UNSIGNED) {
2979 AddCodeLine ("jsr shreax%ld", val);
2981 AddCodeLine ("jsr asreax%ld", val);
2984 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2985 AddCodeLine ("txa");
2986 AddCodeLine ("ldx sreg");
2987 AddCodeLine ("ldy sreg+1");
2988 AddCodeLine ("sty sreg");
2989 AddCodeLine ("ldy #$00");
2990 AddCodeLine ("sty sreg+1");
2992 } else if (val == 16) {
2993 AddCodeLine ("ldy #$00");
2994 AddCodeLine ("ldx sreg+1");
2995 if ((flags & CF_UNSIGNED) == 0) {
2996 unsigned L = GetLocalLabel();
2997 AddCodeLine ("bpl %s", LocalLabelName (L));
2998 AddCodeLine ("dey");
3001 AddCodeLine ("lda sreg");
3002 AddCodeLine ("sty sreg+1");
3003 AddCodeLine ("sty sreg");
3012 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3013 * into the normal, non-optimized stuff.
3015 g_push (flags & ~CF_CONST, 0);
3019 /* Use long way over the stack */
3020 oper (flags, val, ops);
3025 void g_asl (unsigned flags, unsigned long val)
3026 /* Primary = TOS << Primary */
3028 static char* ops [12] = {
3029 0, "tosasla0", "tosaslax",
3030 0, "tosshla0", "tosshlax",
3036 /* If the right hand side is const, the lhs is not on stack but still
3037 * in the primary register.
3039 if (flags & CF_CONST) {
3041 switch (flags & CF_TYPE) {
3045 if (val >= 1 && val <= 3) {
3046 if (flags & CF_UNSIGNED) {
3047 AddCodeLine ("jsr shlax%ld", val);
3049 AddCodeLine ("jsr aslax%ld", val);
3052 } else if (val == 8) {
3053 AddCodeLine ("tax");
3054 AddCodeLine ("lda #$00");
3060 if (val >= 1 && val <= 3) {
3061 if (flags & CF_UNSIGNED) {
3062 AddCodeLine ("jsr shleax%ld", val);
3064 AddCodeLine ("jsr asleax%ld", val);
3067 } else if (val == 8) {
3068 AddCodeLine ("ldy sreg");
3069 AddCodeLine ("sty sreg+1");
3070 AddCodeLine ("stx sreg");
3071 AddCodeLine ("tax");
3072 AddCodeLine ("lda #$00");
3074 } else if (val == 16) {
3075 AddCodeLine ("stx sreg+1");
3076 AddCodeLine ("sta sreg");
3077 AddCodeLine ("lda #$00");
3078 AddCodeLine ("tax");
3087 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3088 * into the normal, non-optimized stuff.
3090 g_push (flags & ~CF_CONST, 0);
3094 /* Use long way over the stack */
3095 oper (flags, val, ops);
3100 void g_neg (unsigned flags)
3101 /* Primary = -Primary */
3103 switch (flags & CF_TYPE) {
3107 AddCodeLine ("jsr negax");
3111 AddCodeLine ("jsr negeax");
3121 void g_bneg (unsigned flags)
3122 /* Primary = !Primary */
3124 switch (flags & CF_TYPE) {
3127 AddCodeLine ("jsr bnega");
3131 AddCodeLine ("jsr bnegax");
3135 AddCodeLine ("jsr bnegeax");
3145 void g_com (unsigned flags)
3146 /* Primary = ~Primary */
3148 switch (flags & CF_TYPE) {
3152 AddCodeLine ("jsr complax");
3156 AddCodeLine ("jsr compleax");
3166 void g_inc (unsigned flags, unsigned long val)
3167 /* Increment the primary register by a given number */
3169 /* Don't inc by zero */
3174 /* Generate code for the supported types */
3176 switch (flags & CF_TYPE) {
3179 if (flags & CF_FORCECHAR) {
3180 if (CPU == CPU_65C02 && val <= 2) {
3182 AddCodeLine ("ina");
3185 AddCodeLine ("clc");
3186 AddCodeLine ("adc #$%02X", (unsigned char)val);
3193 if (CPU == CPU_65C02 && val == 1) {
3194 unsigned L = GetLocalLabel();
3195 AddCodeLine ("ina");
3196 AddCodeLine ("bne %s", LocalLabelName (L));
3197 AddCodeLine ("inx");
3199 } else if (CodeSizeFactor < 200) {
3202 AddCodeLine ("jsr incax%lu", val);
3203 } else if (val <= 255) {
3205 AddCodeLine ("jsr incaxy");
3207 g_add (flags | CF_CONST, val);
3210 /* Inline the code */
3212 if ((val & 0xFF) != 0) {
3213 unsigned L = GetLocalLabel();
3214 AddCodeLine ("clc");
3215 AddCodeLine ("adc #$%02X", (unsigned char) val);
3216 AddCodeLine ("bcc %s", LocalLabelName (L));
3217 AddCodeLine ("inx");
3221 AddCodeLine ("inx");
3224 AddCodeLine ("inx");
3227 AddCodeLine ("clc");
3228 if ((val & 0xFF) != 0) {
3229 AddCodeLine ("adc #$%02X", (unsigned char) val);
3231 AddCodeLine ("pha");
3232 AddCodeLine ("txa");
3233 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3234 AddCodeLine ("tax");
3235 AddCodeLine ("pla");
3243 AddCodeLine ("jsr inceaxy");
3245 g_add (flags | CF_CONST, val);
3257 void g_dec (unsigned flags, unsigned long val)
3258 /* Decrement the primary register by a given number */
3260 /* Don't dec by zero */
3265 /* Generate code for the supported types */
3267 switch (flags & CF_TYPE) {
3270 if (flags & CF_FORCECHAR) {
3271 if (CPU == CPU_65C02 && val <= 2) {
3273 AddCodeLine ("dea");
3276 AddCodeLine ("sec");
3277 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3284 if (CodeSizeFactor < 200) {
3285 /* Use subroutines */
3287 AddCodeLine ("jsr decax%d", (int) val);
3288 } else if (val <= 255) {
3290 AddCodeLine ("jsr decaxy");
3292 g_sub (flags | CF_CONST, val);
3295 /* Inline the code */
3297 if ((val & 0xFF) != 0) {
3298 unsigned L = GetLocalLabel();
3299 AddCodeLine ("sec");
3300 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3301 AddCodeLine ("bcs %s", LocalLabelName (L));
3302 AddCodeLine ("dex");
3306 AddCodeLine ("dex");
3309 AddCodeLine ("dex");
3312 AddCodeLine ("sec");
3313 if ((val & 0xFF) != 0) {
3314 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3316 AddCodeLine ("pha");
3317 AddCodeLine ("txa");
3318 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3319 AddCodeLine ("tax");
3320 AddCodeLine ("pla");
3328 AddCodeLine ("jsr deceaxy");
3330 g_sub (flags | CF_CONST, val);
3343 * Following are the conditional operators. They compare the TOS against
3344 * the primary and put a literal 1 in the primary if the condition is
3345 * true, otherwise they clear the primary register
3350 void g_eq (unsigned flags, unsigned long val)
3351 /* Test for equal */
3353 static char* ops [12] = {
3354 "toseq00", "toseqa0", "toseqax",
3355 "toseq00", "toseqa0", "toseqax",
3362 /* If the right hand side is const, the lhs is not on stack but still
3363 * in the primary register.
3365 if (flags & CF_CONST) {
3367 switch (flags & CF_TYPE) {
3370 if (flags & CF_FORCECHAR) {
3371 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3372 AddCodeLine ("jsr booleq");
3378 L = GetLocalLabel();
3379 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3380 AddCodeLine ("bne %s", LocalLabelName (L));
3381 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3383 AddCodeLine ("jsr booleq");
3393 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3394 * into the normal, non-optimized stuff.
3396 g_push (flags & ~CF_CONST, 0);
3400 /* Use long way over the stack */
3401 oper (flags, val, ops);
3406 void g_ne (unsigned flags, unsigned long val)
3407 /* Test for not equal */
3409 static char* ops [12] = {
3410 "tosne00", "tosnea0", "tosneax",
3411 "tosne00", "tosnea0", "tosneax",
3418 /* If the right hand side is const, the lhs is not on stack but still
3419 * in the primary register.
3421 if (flags & CF_CONST) {
3423 switch (flags & CF_TYPE) {
3426 if (flags & CF_FORCECHAR) {
3427 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3428 AddCodeLine ("jsr boolne");
3434 L = GetLocalLabel();
3435 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3436 AddCodeLine ("bne %s", LocalLabelName (L));
3437 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3439 AddCodeLine ("jsr boolne");
3449 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3450 * into the normal, non-optimized stuff.
3452 g_push (flags & ~CF_CONST, 0);
3456 /* Use long way over the stack */
3457 oper (flags, val, ops);
3462 void g_lt (unsigned flags, unsigned long val)
3463 /* Test for less than */
3465 static char* ops [12] = {
3466 "toslt00", "toslta0", "tosltax",
3467 "tosult00", "tosulta0", "tosultax",
3472 /* If the right hand side is const, the lhs is not on stack but still
3473 * in the primary register.
3475 if (flags & CF_CONST) {
3477 /* Give a warning in some special cases */
3478 if ((flags & CF_UNSIGNED) && val == 0) {
3479 Warning ("Condition is never true");
3482 /* Look at the type */
3483 switch (flags & CF_TYPE) {
3486 if (flags & CF_FORCECHAR) {
3487 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3488 if (flags & CF_UNSIGNED) {
3489 AddCodeLine ("jsr boolult");
3491 AddCodeLine ("jsr boollt");
3498 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3499 /* If we have a signed compare against zero, we only need to
3500 * test the high byte.
3502 AddCodeLine ("txa");
3503 AddCodeLine ("jsr boollt");
3506 /* Direct code only for unsigned data types */
3507 if (flags & CF_UNSIGNED) {
3508 unsigned L = GetLocalLabel();
3509 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3510 AddCodeLine ("bne %s", LocalLabelName (L));
3511 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3513 AddCodeLine ("jsr boolult");
3519 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3520 /* If we have a signed compare against zero, we only need to
3521 * test the high byte.
3523 AddCodeLine ("lda sreg+1");
3524 AddCodeLine ("jsr boollt");
3533 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3534 * into the normal, non-optimized stuff.
3536 g_push (flags & ~CF_CONST, 0);
3540 /* Use long way over the stack */
3541 oper (flags, val, ops);
3546 void g_le (unsigned flags, unsigned long val)
3547 /* Test for less than or equal to */
3549 static char* ops [12] = {
3550 "tosle00", "toslea0", "tosleax",
3551 "tosule00", "tosulea0", "tosuleax",
3557 /* If the right hand side is const, the lhs is not on stack but still
3558 * in the primary register.
3560 if (flags & CF_CONST) {
3562 /* Look at the type */
3563 switch (flags & CF_TYPE) {
3566 if (flags & CF_FORCECHAR) {
3567 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3568 if (flags & CF_UNSIGNED) {
3569 AddCodeLine ("jsr boolule");
3571 AddCodeLine ("jsr boolle");
3578 if (flags & CF_UNSIGNED) {
3579 unsigned L = GetLocalLabel();
3580 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3581 AddCodeLine ("bne %s", LocalLabelName (L));
3582 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3584 AddCodeLine ("jsr boolule");
3596 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3597 * into the normal, non-optimized stuff.
3599 g_push (flags & ~CF_CONST, 0);
3603 /* Use long way over the stack */
3604 oper (flags, val, ops);
3609 void g_gt (unsigned flags, unsigned long val)
3610 /* Test for greater than */
3612 static char* ops [12] = {
3613 "tosgt00", "tosgta0", "tosgtax",
3614 "tosugt00", "tosugta0", "tosugtax",
3620 /* If the right hand side is const, the lhs is not on stack but still
3621 * in the primary register.
3623 if (flags & CF_CONST) {
3625 /* Look at the type */
3626 switch (flags & CF_TYPE) {
3629 if (flags & CF_FORCECHAR) {
3630 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3631 if (flags & CF_UNSIGNED) {
3632 /* If we have a compare > 0, we will replace it by
3633 * != 0 here, since both are identical but the latter
3634 * is easier to optimize.
3637 AddCodeLine ("jsr boolugt");
3639 AddCodeLine ("jsr boolne");
3642 AddCodeLine ("jsr boolgt");
3649 if (flags & CF_UNSIGNED) {
3650 /* If we have a compare > 0, we will replace it by
3651 * != 0 here, since both are identical but the latter
3652 * is easier to optimize.
3654 if ((val & 0xFFFF) == 0) {
3655 AddCodeLine ("stx tmp1");
3656 AddCodeLine ("ora tmp1");
3657 AddCodeLine ("jsr boolne");
3659 unsigned L = GetLocalLabel();
3660 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3661 AddCodeLine ("bne %s", LocalLabelName (L));
3662 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3664 AddCodeLine ("jsr boolugt");
3677 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3678 * into the normal, non-optimized stuff.
3680 g_push (flags & ~CF_CONST, 0);
3684 /* Use long way over the stack */
3685 oper (flags, val, ops);
3690 void g_ge (unsigned flags, unsigned long val)
3691 /* Test for greater than or equal to */
3693 static char* ops [12] = {
3694 "tosge00", "tosgea0", "tosgeax",
3695 "tosuge00", "tosugea0", "tosugeax",
3701 /* If the right hand side is const, the lhs is not on stack but still
3702 * in the primary register.
3704 if (flags & CF_CONST) {
3706 /* Give a warning in some special cases */
3707 if ((flags & CF_UNSIGNED) && val == 0) {
3708 Warning ("Condition is always true");
3711 /* Look at the type */
3712 switch (flags & CF_TYPE) {
3715 if (flags & CF_FORCECHAR) {
3716 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3717 if (flags & CF_UNSIGNED) {
3718 AddCodeLine ("jsr booluge");
3720 AddCodeLine ("jsr boolge");
3727 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3728 /* If we have a signed compare against zero, we only need to
3729 * test the high byte.
3731 AddCodeLine ("txa");
3732 AddCodeLine ("jsr boolge");
3735 /* Direct code only for unsigned data types */
3736 if (flags & CF_UNSIGNED) {
3737 unsigned L = GetLocalLabel();
3738 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3739 AddCodeLine ("bne %s", LocalLabelName (L));
3740 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3742 AddCodeLine ("jsr booluge");
3748 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3749 /* If we have a signed compare against zero, we only need to
3750 * test the high byte.
3752 AddCodeLine ("lda sreg+1");
3753 AddCodeLine ("jsr boolge");
3762 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3763 * into the normal, non-optimized stuff.
3765 g_push (flags & ~CF_CONST, 0);
3769 /* Use long way over the stack */
3770 oper (flags, val, ops);
3775 /*****************************************************************************/
3776 /* Allocating static storage */
3777 /*****************************************************************************/
3781 void g_res (unsigned n)
3782 /* Reserve static storage, n bytes */
3784 AddDataLine ("\t.res\t%u,$00", n);
3789 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3790 /* Define data with the size given in flags */
3792 if (flags & CF_CONST) {
3794 /* Numeric constant */
3795 switch (flags & CF_TYPE) {
3798 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
3802 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
3806 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3817 /* Create the correct label name */
3818 const char* Label = GetLabelName (flags, val, offs);
3820 /* Labels are always 16 bit */
3821 AddDataLine ("\t.word\t%s", Label);
3828 void g_defbytes (const void* Bytes, unsigned Count)
3829 /* Output a row of bytes as a constant */
3835 /* Cast the buffer pointer */
3836 const unsigned char* Data = (const unsigned char*) Bytes;
3838 /* Output the stuff */
3841 /* How many go into this line? */
3842 if ((Chunk = Count) > 16) {
3847 /* Output one line */
3848 strcpy (Buf, "\t.byte\t");
3851 B += sprintf (B, "$%02X", *Data++);
3857 /* Output the line */
3864 void g_zerobytes (unsigned n)
3865 /* Output n bytes of data initialized with zero */
3867 AddDataLine ("\t.res\t%u,$00", n);
3872 /*****************************************************************************/
3873 /* User supplied assembler code */
3874 /*****************************************************************************/
3878 void g_asmcode (const char* Line, int Len)
3879 /* Output one line of assembler code. If Len is greater than zero, it is used
3880 * as the maximum number of characters to use from Line.
3884 AddCodeLine ("%.*s", Len, Line);
3886 AddCodeLine ("%s", Line);
3892 /*****************************************************************************/
3893 /* Inlined known functions */
3894 /*****************************************************************************/
3898 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3899 /* Inline the strlen() function */
3901 /* We need a label in both cases */
3902 unsigned label = GetLocalLabel ();
3904 /* Two different encodings */
3905 if (flags & CF_CONST) {
3907 /* The address of the string is constant. Create the correct label name */
3908 char* lbuf = GetLabelName (flags, val, offs);
3910 /* Generate the strlen code */
3911 AddCodeLine ("ldy #$FF");
3912 g_defcodelabel (label);
3913 AddCodeLine ("iny");
3914 AddCodeLine ("lda %s,y", lbuf);
3915 AddCodeLine ("bne %s", LocalLabelName (label));
3916 AddCodeLine ("tax");
3917 AddCodeLine ("tya");
3921 /* Address not constant but in primary */
3922 if (CodeSizeFactor < 400) {
3923 /* This is too much code, so call strlen instead of inlining */
3924 AddCodeLine ("jsr _strlen");
3926 /* Inline the function */
3927 AddCodeLine ("sta ptr1");
3928 AddCodeLine ("stx ptr1+1");
3929 AddCodeLine ("ldy #$FF");
3930 g_defcodelabel (label);
3931 AddCodeLine ("iny");
3932 AddCodeLine ("lda (ptr1),y");
3933 AddCodeLine ("bne %s", LocalLabelName (label));
3934 AddCodeLine ("tax");
3935 AddCodeLine ("tya");