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 DS_AddLine (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 */
802 if (flags & CF_UNSIGNED) {
804 AddCodeLine ("jsr ldauidx");
807 AddCodeLine ("jsr ldaidx");
812 if (flags & CF_TEST) {
814 AddCodeLine ("sta ptr1");
815 AddCodeLine ("stx ptr1+1");
816 AddCodeLine ("lda (ptr1),y");
818 AddCodeLine ("ora (ptr1),y");
821 AddCodeLine ("jsr ldaxi");
824 AddCodeLine ("jsr ldaxidx");
831 AddCodeLine ("jsr ldeaxi");
834 AddCodeLine ("jsr ldeaxidx");
836 if (flags & CF_TEST) {
837 AddCodeLine ("jsr tsteax");
849 void g_leasp (int offs)
850 /* Fetch the address of the specified symbol into the primary register */
852 /* Calculate the offset relative to sp */
855 /* For value 0 we do direct code */
857 AddCodeLine ("lda sp");
858 AddCodeLine ("ldx sp+1");
860 if (CodeSizeFactor < 300) {
861 ldaconst (offs); /* Load A with offset value */
862 AddCodeLine ("jsr leaasp"); /* Load effective address */
864 unsigned L = GetLocalLabel ();
865 if (CPU == CPU_65C02 && offs == 1) {
866 AddCodeLine ("lda sp");
867 AddCodeLine ("ldx sp+1");
869 AddCodeLine ("bne %s", LocalLabelName (L));
874 AddCodeLine ("ldx sp+1");
875 AddCodeLine ("adc sp");
876 AddCodeLine ("bcc %s", LocalLabelName (L));
886 void g_leavariadic (int Offs)
887 /* Fetch the address of a parameter in a variadic function into the primary
891 unsigned ArgSizeOffs;
893 /* Calculate the offset relative to sp */
896 /* Get the offset of the parameter which is stored at sp+0 on function
897 * entry and check if this offset is reachable with a byte offset.
900 ArgSizeOffs = -oursp;
901 CheckLocalOffs (ArgSizeOffs);
903 /* Get the size of all parameters. */
904 if (ArgSizeOffs == 0 && CPU == CPU_65C02) {
905 AddCodeLine ("lda (sp)");
907 ldyconst (ArgSizeOffs);
908 AddCodeLine ("lda (sp),y");
911 /* Add the value of the stackpointer */
912 if (CodeSizeFactor > 250) {
913 unsigned L = GetLocalLabel();
914 AddCodeLine ("ldx sp+1");
916 AddCodeLine ("adc sp");
917 AddCodeLine ("bcc %s", LocalLabelName (L));
921 AddCodeLine ("jsr leaasp");
924 /* Add the offset to the primary */
926 g_inc (CF_INT | CF_CONST, Offs);
927 } else if (Offs < 0) {
928 g_dec (CF_INT | CF_CONST, -Offs);
934 /*****************************************************************************/
935 /* Store into memory */
936 /*****************************************************************************/
940 void g_putstatic (unsigned flags, unsigned long label, unsigned offs)
941 /* Store the primary register into the specified static memory cell */
943 /* Create the correct label name */
944 char* lbuf = GetLabelName (flags, label, offs);
946 /* Check the size and generate the correct store operation */
947 switch (flags & CF_TYPE) {
950 AddCodeLine ("sta %s", lbuf);
954 AddCodeLine ("sta %s", lbuf);
955 AddCodeLine ("stx %s+1", lbuf);
959 AddCodeLine ("sta %s", lbuf);
960 AddCodeLine ("stx %s+1", lbuf);
961 AddCodeLine ("ldy sreg");
962 AddCodeLine ("sty %s+2", lbuf);
963 AddCodeLine ("ldy sreg+1");
964 AddCodeLine ("sty %s+3", lbuf);
975 void g_putlocal (unsigned Flags, int Offs, long Val)
976 /* Put data into local object. */
979 CheckLocalOffs (Offs);
980 switch (Flags & CF_TYPE) {
983 if (Flags & CF_CONST) {
984 AddCodeLine ("lda #$%02X", (unsigned char) Val);
986 if (CPU == CPU_65C02 && Offs == 0) {
987 AddCodeLine ("sta (sp)");
990 AddCodeLine ("sta (sp),y");
995 if (Flags & CF_CONST) {
997 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
998 AddCodeLine ("sta (sp),y");
999 if ((Flags & CF_NOKEEP) == 0) {
1000 /* Place high byte into X */
1001 AddCodeLine ("tax");
1003 if (CPU == CPU_65C02 && Offs == 0) {
1004 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1005 AddCodeLine ("sta (sp)");
1007 if ((Val & 0xFF) == Offs+1) {
1008 /* The value we need is already in Y */
1009 AddCodeLine ("tya");
1010 AddCodeLine ("dey");
1012 AddCodeLine ("dey");
1013 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1015 AddCodeLine ("sta (sp),y");
1018 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
1021 AddCodeLine ("jsr staxysp");
1023 AddCodeLine ("jsr stax0sp");
1026 if (CPU == CPU_65C02 && Offs == 0) {
1027 AddCodeLine ("sta (sp)");
1029 AddCodeLine ("txa");
1030 AddCodeLine ("sta (sp),y");
1033 AddCodeLine ("sta (sp),y");
1034 AddCodeLine ("iny");
1035 AddCodeLine ("txa");
1036 AddCodeLine ("sta (sp),y");
1043 if (Flags & CF_CONST) {
1044 g_getimmed (Flags, Val, 0);
1048 AddCodeLine ("jsr steaxysp");
1050 AddCodeLine ("jsr steax0sp");
1062 void g_putind (unsigned Flags, unsigned Offs)
1063 /* Store the specified object type in the primary register at the address
1064 * on the top of the stack
1067 /* We can handle offsets below $100 directly, larger offsets must be added
1068 * to the address. Since a/x is in use, best code is achieved by adding
1069 * just the high byte. Be sure to check if the low byte will overflow while
1072 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1074 /* Overflow - we need to add the low byte also */
1075 AddCodeLine ("ldy #$00");
1076 AddCodeLine ("clc");
1077 AddCodeLine ("pha");
1078 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1079 AddCodeLine ("adc (sp),y");
1080 AddCodeLine ("sta (sp),y");
1081 AddCodeLine ("iny");
1082 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1083 AddCodeLine ("adc (sp),y");
1084 AddCodeLine ("sta (sp),y");
1085 AddCodeLine ("pla");
1087 /* Complete address is on stack, new offset is zero */
1090 } else if ((Offs & 0xFF00) != 0) {
1092 /* We can just add the high byte */
1093 AddCodeLine ("ldy #$01");
1094 AddCodeLine ("clc");
1095 AddCodeLine ("pha");
1096 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1097 AddCodeLine ("adc (sp),y");
1098 AddCodeLine ("sta (sp),y");
1099 AddCodeLine ("pla");
1101 /* Offset is now just the low byte */
1105 /* Check the size and determine operation */
1106 switch (Flags & CF_TYPE) {
1110 AddCodeLine ("jsr staspidx");
1115 AddCodeLine ("jsr staxspidx");
1120 AddCodeLine ("jsr steaxspidx");
1128 /* Pop the argument which is always a pointer */
1134 /*****************************************************************************/
1135 /* type conversion and similiar stuff */
1136 /*****************************************************************************/
1140 void g_toslong (unsigned flags)
1141 /* Make sure, the value on TOS is a long. Convert if necessary */
1143 switch (flags & CF_TYPE) {
1147 if (flags & CF_UNSIGNED) {
1148 AddCodeLine ("jsr tosulong");
1150 AddCodeLine ("jsr toslong");
1165 void g_tosint (unsigned flags)
1166 /* Make sure, the value on TOS is an int. Convert if necessary */
1168 switch (flags & CF_TYPE) {
1175 AddCodeLine ("jsr tosint");
1186 void g_reglong (unsigned flags)
1187 /* Make sure, the value in the primary register a long. Convert if necessary */
1189 switch (flags & CF_TYPE) {
1193 if (flags & CF_UNSIGNED) {
1194 if (CodeSizeFactor >= 200) {
1196 AddCodeLine ("sty sreg");
1197 AddCodeLine ("sty sreg+1");
1199 AddCodeLine ("jsr axulong");
1202 AddCodeLine ("jsr axlong");
1216 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1217 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1218 * value, that corresponds to the value on TOS, rhs corresponds to the value
1219 * in (e)ax. The return value is the the flags value for the resulting type.
1222 unsigned ltype, rtype;
1225 /* Get the type spec from the flags */
1226 ltype = lhs & CF_TYPE;
1227 rtype = rhs & CF_TYPE;
1229 /* Check if a conversion is needed */
1230 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1231 /* We must promote the primary register to long */
1233 /* Get the new rhs type */
1234 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1236 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1237 /* We must promote the lhs to long */
1243 /* Get the new rhs type */
1244 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1248 /* Determine the result type for the operation:
1249 * - The result is const if both operands are const.
1250 * - The result is unsigned if one of the operands is unsigned.
1251 * - The result is long if one of the operands is long.
1252 * - Otherwise the result is int sized.
1254 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1255 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1256 if (rtype == CF_LONG || ltype == CF_LONG) {
1266 unsigned g_typecast (unsigned lhs, unsigned rhs)
1267 /* Cast the value in the primary register to the operand size that is flagged
1268 * by the lhs value. Return the result value.
1271 unsigned ltype, rtype;
1273 /* Get the type spec from the flags */
1274 ltype = lhs & CF_TYPE;
1275 rtype = rhs & CF_TYPE;
1277 /* Check if a conversion is needed */
1278 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1279 /* We must promote the primary register to long */
1283 /* Do not need any other action. If the left type is int, and the primary
1284 * register is long, it will be automagically truncated. If the right hand
1285 * side is const, it is not located in the primary register and handled by
1286 * the expression parser code.
1289 /* Result is const if the right hand side was const */
1290 lhs |= (rhs & CF_CONST);
1292 /* The resulting type is that of the left hand side (that's why you called
1300 void g_scale (unsigned flags, long val)
1301 /* Scale the value in the primary register by the given value. If val is positive,
1302 * scale up, is val is negative, scale down. This function is used to scale
1303 * the operands or results of pointer arithmetic by the size of the type, the
1304 * pointer points to.
1309 /* Value may not be zero */
1311 Internal ("Data type has no size");
1312 } else if (val > 0) {
1315 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1317 /* Factor is 2, 4, 8 and 16, use special function */
1318 switch (flags & CF_TYPE) {
1321 if (flags & CF_FORCECHAR) {
1323 AddCodeLine ("asl a");
1330 if (CodeSizeFactor >= (p2+1)*130U) {
1331 AddCodeLine ("stx tmp1");
1333 AddCodeLine ("asl a");
1334 AddCodeLine ("rol tmp1");
1336 AddCodeLine ("ldx tmp1");
1338 if (flags & CF_UNSIGNED) {
1339 AddCodeLine ("jsr shlax%d", p2);
1341 AddCodeLine ("jsr aslax%d", p2);
1347 if (flags & CF_UNSIGNED) {
1348 AddCodeLine ("jsr shleax%d", p2);
1350 AddCodeLine ("jsr asleax%d", p2);
1359 } else if (val != 1) {
1361 /* Use a multiplication instead */
1362 g_mul (flags | CF_CONST, val);
1370 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1372 /* Factor is 2, 4, 8 and 16 use special function */
1373 switch (flags & CF_TYPE) {
1376 if (flags & CF_FORCECHAR) {
1377 if (flags & CF_UNSIGNED) {
1379 AddCodeLine ("lsr a");
1382 } else if (p2 <= 2) {
1383 AddCodeLine ("cmp #$80");
1384 AddCodeLine ("ror a");
1391 if (flags & CF_UNSIGNED) {
1392 if (CodeSizeFactor >= (p2+1)*130U) {
1393 AddCodeLine ("stx tmp1");
1395 AddCodeLine ("lsr tmp1");
1396 AddCodeLine ("ror a");
1398 AddCodeLine ("ldx tmp1");
1400 AddCodeLine ("jsr lsrax%d", p2);
1403 if (CodeSizeFactor >= (p2+1)*150U) {
1404 AddCodeLine ("stx tmp1");
1406 AddCodeLine ("cpx #$80");
1407 AddCodeLine ("ror tmp1");
1408 AddCodeLine ("ror a");
1410 AddCodeLine ("ldx tmp1");
1412 AddCodeLine ("jsr asrax%d", p2);
1418 if (flags & CF_UNSIGNED) {
1419 AddCodeLine ("jsr lsreax%d", p2);
1421 AddCodeLine ("jsr asreax%d", p2);
1430 } else if (val != 1) {
1432 /* Use a division instead */
1433 g_div (flags | CF_CONST, val);
1441 /*****************************************************************************/
1442 /* Adds and subs of variables fix a fixed address */
1443 /*****************************************************************************/
1447 void g_addlocal (unsigned flags, int offs)
1448 /* Add a local variable to ax */
1452 /* Correct the offset and check it */
1454 CheckLocalOffs (offs);
1456 switch (flags & CF_TYPE) {
1459 L = GetLocalLabel();
1460 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1461 AddCodeLine ("clc");
1462 AddCodeLine ("adc (sp),y");
1463 AddCodeLine ("bcc %s", LocalLabelName (L));
1464 AddCodeLine ("inx");
1469 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1470 AddCodeLine ("clc");
1471 AddCodeLine ("adc (sp),y");
1472 AddCodeLine ("pha");
1473 AddCodeLine ("txa");
1474 AddCodeLine ("iny");
1475 AddCodeLine ("adc (sp),y");
1476 AddCodeLine ("tax");
1477 AddCodeLine ("pla");
1481 /* Do it the old way */
1483 g_getlocal (flags, offs);
1495 void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
1496 /* Add a static variable to ax */
1500 /* Create the correct label name */
1501 char* lbuf = GetLabelName (flags, label, offs);
1503 switch (flags & CF_TYPE) {
1506 L = GetLocalLabel();
1507 AddCodeLine ("clc");
1508 AddCodeLine ("adc %s", lbuf);
1509 AddCodeLine ("bcc %s", LocalLabelName (L));
1510 AddCodeLine ("inx");
1515 AddCodeLine ("clc");
1516 AddCodeLine ("adc %s", lbuf);
1517 AddCodeLine ("tay");
1518 AddCodeLine ("txa");
1519 AddCodeLine ("adc %s+1", lbuf);
1520 AddCodeLine ("tax");
1521 AddCodeLine ("tya");
1525 /* Do it the old way */
1527 g_getstatic (flags, label, offs);
1539 /*****************************************************************************/
1540 /* Compares of ax with a variable with fixed address */
1541 /*****************************************************************************/
1545 void g_cmplocal (unsigned flags, int offs)
1546 /* Compare a local variable to ax */
1548 Internal ("g_cmplocal not implemented");
1553 void g_cmpstatic (unsigned flags, unsigned label, unsigned offs)
1554 /* Compare a static variable to ax */
1556 Internal ("g_cmpstatic not implemented");
1561 /*****************************************************************************/
1562 /* Special op= functions */
1563 /*****************************************************************************/
1567 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
1569 /* Emit += for a static variable */
1571 /* Create the correct label name */
1572 char* lbuf = GetLabelName (flags, label, offs);
1574 /* Check the size and determine operation */
1575 switch (flags & CF_TYPE) {
1578 if (flags & CF_FORCECHAR) {
1579 AddCodeLine ("ldx #$00");
1580 if (flags & CF_CONST) {
1582 AddCodeLine ("inc %s", lbuf);
1583 AddCodeLine ("lda %s", lbuf);
1585 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1586 AddCodeLine ("clc");
1587 AddCodeLine ("adc %s", lbuf);
1588 AddCodeLine ("sta %s", lbuf);
1591 AddCodeLine ("clc");
1592 AddCodeLine ("adc %s", lbuf);
1593 AddCodeLine ("sta %s", lbuf);
1595 if ((flags & CF_UNSIGNED) == 0) {
1596 unsigned L = GetLocalLabel();
1597 AddCodeLine ("bpl %s", LocalLabelName (L));
1598 AddCodeLine ("dex");
1606 if (flags & CF_CONST) {
1608 unsigned L = GetLocalLabel ();
1609 AddCodeLine ("inc %s", lbuf);
1610 AddCodeLine ("bne %s", LocalLabelName (L));
1611 AddCodeLine ("inc %s+1", lbuf);
1613 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1614 AddCodeLine ("ldx %s+1", lbuf);
1616 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1617 AddCodeLine ("clc");
1618 AddCodeLine ("adc %s", lbuf);
1619 AddCodeLine ("sta %s", lbuf);
1621 unsigned L = GetLocalLabel ();
1622 AddCodeLine ("bcc %s", LocalLabelName (L));
1623 AddCodeLine ("inc %s+1", lbuf);
1625 AddCodeLine ("ldx %s+1", lbuf);
1627 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1628 AddCodeLine ("adc %s+1", lbuf);
1629 AddCodeLine ("sta %s+1", lbuf);
1630 AddCodeLine ("tax");
1631 AddCodeLine ("lda %s", lbuf);
1635 AddCodeLine ("clc");
1636 AddCodeLine ("adc %s", lbuf);
1637 AddCodeLine ("sta %s", lbuf);
1638 AddCodeLine ("txa");
1639 AddCodeLine ("adc %s+1", lbuf);
1640 AddCodeLine ("sta %s+1", lbuf);
1641 AddCodeLine ("tax");
1642 AddCodeLine ("lda %s", lbuf);
1647 if (flags & CF_CONST) {
1649 AddCodeLine ("ldy #<(%s)", lbuf);
1650 AddCodeLine ("sty ptr1");
1651 AddCodeLine ("ldy #>(%s+1)", lbuf);
1653 AddCodeLine ("jsr laddeq1");
1655 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1656 AddCodeLine ("jsr laddeqa");
1659 g_getstatic (flags, label, offs);
1661 g_putstatic (flags, label, offs);
1664 AddCodeLine ("ldy #<(%s)", lbuf);
1665 AddCodeLine ("sty ptr1");
1666 AddCodeLine ("ldy #>(%s+1)", lbuf);
1667 AddCodeLine ("jsr laddeq");
1678 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1679 /* Emit += for a local variable */
1681 /* Calculate the true offset, check it, load it into Y */
1683 CheckLocalOffs (offs);
1685 /* Check the size and determine operation */
1686 switch (flags & CF_TYPE) {
1689 if (flags & CF_FORCECHAR) {
1691 AddCodeLine ("ldx #$00");
1692 if (flags & CF_CONST) {
1693 AddCodeLine ("clc");
1694 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1695 AddCodeLine ("adc (sp),y");
1696 AddCodeLine ("sta (sp),y");
1698 AddCodeLine ("clc");
1699 AddCodeLine ("adc (sp),y");
1700 AddCodeLine ("sta (sp),y");
1702 if ((flags & CF_UNSIGNED) == 0) {
1703 unsigned L = GetLocalLabel();
1704 AddCodeLine ("bpl %s", LocalLabelName (L));
1705 AddCodeLine ("dex");
1713 if (flags & CF_CONST) {
1714 g_getimmed (flags, val, 0);
1717 AddCodeLine ("jsr addeq0sp");
1720 AddCodeLine ("jsr addeqysp");
1725 if (flags & CF_CONST) {
1726 g_getimmed (flags, val, 0);
1729 AddCodeLine ("jsr laddeq0sp");
1732 AddCodeLine ("jsr laddeqysp");
1743 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1744 /* Emit += for the location with address in ax */
1746 /* If the offset is too large for a byte register, add the high byte
1747 * of the offset to the primary. Beware: We need a special correction
1748 * if the offset in the low byte will overflow in the operation.
1750 offs = MakeByteOffs (flags, offs);
1752 /* Check the size and determine operation */
1753 switch (flags & CF_TYPE) {
1756 AddCodeLine ("sta ptr1");
1757 AddCodeLine ("stx ptr1+1");
1758 AddCodeLine ("ldy #$%02X", offs);
1759 AddCodeLine ("ldx #$00");
1760 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1761 AddCodeLine ("clc");
1762 AddCodeLine ("adc (ptr1),y");
1763 AddCodeLine ("sta (ptr1),y");
1767 if (CodeSizeFactor >= 200) {
1768 /* Lots of code, use only if size is not important */
1769 AddCodeLine ("sta ptr1");
1770 AddCodeLine ("stx ptr1+1");
1771 AddCodeLine ("ldy #$%02X", offs);
1772 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1773 AddCodeLine ("clc");
1774 AddCodeLine ("adc (ptr1),y");
1775 AddCodeLine ("sta (ptr1),y");
1776 AddCodeLine ("pha");
1777 AddCodeLine ("iny");
1778 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1779 AddCodeLine ("adc (ptr1),y");
1780 AddCodeLine ("sta (ptr1),y");
1781 AddCodeLine ("tax");
1782 AddCodeLine ("pla");
1788 AddCodeLine ("jsr pushax"); /* Push the address */
1789 push (flags); /* Correct the internal sp */
1790 g_getind (flags, offs); /* Fetch the value */
1791 g_inc (flags, val); /* Increment value in primary */
1792 g_putind (flags, offs); /* Store the value back */
1802 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1804 /* Emit -= for a static variable */
1806 /* Create the correct label name */
1807 char* lbuf = GetLabelName (flags, label, offs);
1809 /* Check the size and determine operation */
1810 switch (flags & CF_TYPE) {
1813 if (flags & CF_FORCECHAR) {
1814 AddCodeLine ("ldx #$00");
1815 if (flags & CF_CONST) {
1817 AddCodeLine ("dec %s", lbuf);
1818 AddCodeLine ("lda %s", lbuf);
1820 AddCodeLine ("sec");
1821 AddCodeLine ("lda %s", lbuf);
1822 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1823 AddCodeLine ("sta %s", lbuf);
1826 AddCodeLine ("sec");
1827 AddCodeLine ("sta tmp1");
1828 AddCodeLine ("lda %s", lbuf);
1829 AddCodeLine ("sbc tmp1");
1830 AddCodeLine ("sta %s", lbuf);
1832 if ((flags & CF_UNSIGNED) == 0) {
1833 unsigned L = GetLocalLabel();
1834 AddCodeLine ("bpl %s", LocalLabelName (L));
1835 AddCodeLine ("dex");
1843 AddCodeLine ("sec");
1844 if (flags & CF_CONST) {
1845 AddCodeLine ("lda %s", lbuf);
1846 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1847 AddCodeLine ("sta %s", lbuf);
1849 unsigned L = GetLocalLabel ();
1850 AddCodeLine ("bcs %s", LocalLabelName (L));
1851 AddCodeLine ("dec %s+1", lbuf);
1853 AddCodeLine ("ldx %s+1", lbuf);
1855 AddCodeLine ("lda %s+1", lbuf);
1856 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1857 AddCodeLine ("sta %s+1", lbuf);
1858 AddCodeLine ("tax");
1859 AddCodeLine ("lda %s", lbuf);
1862 AddCodeLine ("sta tmp1");
1863 AddCodeLine ("lda %s", lbuf);
1864 AddCodeLine ("sbc tmp1");
1865 AddCodeLine ("sta %s", lbuf);
1866 AddCodeLine ("stx tmp1");
1867 AddCodeLine ("lda %s+1", lbuf);
1868 AddCodeLine ("sbc tmp1");
1869 AddCodeLine ("sta %s+1", lbuf);
1870 AddCodeLine ("tax");
1871 AddCodeLine ("lda %s", lbuf);
1876 if (flags & CF_CONST) {
1878 AddCodeLine ("ldy #<(%s)", lbuf);
1879 AddCodeLine ("sty ptr1");
1880 AddCodeLine ("ldy #>(%s+1)", lbuf);
1882 AddCodeLine ("jsr lsubeq1");
1884 AddCodeLine ("lda #$%02X", (unsigned char)val);
1885 AddCodeLine ("jsr lsubeqa");
1888 g_getstatic (flags, label, offs);
1890 g_putstatic (flags, label, offs);
1893 AddCodeLine ("ldy #<(%s)", lbuf);
1894 AddCodeLine ("sty ptr1");
1895 AddCodeLine ("ldy #>(%s+1)", lbuf);
1896 AddCodeLine ("jsr lsubeq");
1907 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1908 /* Emit -= for a local variable */
1910 /* Calculate the true offset, check it, load it into Y */
1912 CheckLocalOffs (offs);
1914 /* Check the size and determine operation */
1915 switch (flags & CF_TYPE) {
1918 if (flags & CF_FORCECHAR) {
1920 AddCodeLine ("ldx #$00");
1921 AddCodeLine ("sec");
1922 if (flags & CF_CONST) {
1923 AddCodeLine ("lda (sp),y");
1924 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1926 AddCodeLine ("sta tmp1");
1927 AddCodeLine ("lda (sp),y");
1928 AddCodeLine ("sbc tmp1");
1930 AddCodeLine ("sta (sp),y");
1931 if ((flags & CF_UNSIGNED) == 0) {
1932 unsigned L = GetLocalLabel();
1933 AddCodeLine ("bpl %s", LocalLabelName (L));
1934 AddCodeLine ("dex");
1942 if (flags & CF_CONST) {
1943 g_getimmed (flags, val, 0);
1946 AddCodeLine ("jsr subeq0sp");
1949 AddCodeLine ("jsr subeqysp");
1954 if (flags & CF_CONST) {
1955 g_getimmed (flags, val, 0);
1958 AddCodeLine ("jsr lsubeq0sp");
1961 AddCodeLine ("jsr lsubeqysp");
1972 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
1973 /* Emit -= for the location with address in ax */
1975 /* If the offset is too large for a byte register, add the high byte
1976 * of the offset to the primary. Beware: We need a special correction
1977 * if the offset in the low byte will overflow in the operation.
1979 offs = MakeByteOffs (flags, offs);
1981 /* Check the size and determine operation */
1982 switch (flags & CF_TYPE) {
1985 AddCodeLine ("sta ptr1");
1986 AddCodeLine ("stx ptr1+1");
1987 AddCodeLine ("ldy #$%02X", offs);
1988 AddCodeLine ("ldx #$00");
1989 AddCodeLine ("lda (ptr1),y");
1990 AddCodeLine ("sec");
1991 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1992 AddCodeLine ("sta (ptr1),y");
1996 if (CodeSizeFactor >= 200) {
1997 /* Lots of code, use only if size is not important */
1998 AddCodeLine ("sta ptr1");
1999 AddCodeLine ("stx ptr1+1");
2000 AddCodeLine ("ldy #$%02X", offs);
2001 AddCodeLine ("lda (ptr1),y");
2002 AddCodeLine ("sec");
2003 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2004 AddCodeLine ("sta (ptr1),y");
2005 AddCodeLine ("pha");
2006 AddCodeLine ("iny");
2007 AddCodeLine ("lda (ptr1),y");
2008 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2009 AddCodeLine ("sta (ptr1),y");
2010 AddCodeLine ("tax");
2011 AddCodeLine ("pla");
2017 AddCodeLine ("jsr pushax"); /* Push the address */
2018 push (flags); /* Correct the internal sp */
2019 g_getind (flags, offs); /* Fetch the value */
2020 g_dec (flags, val); /* Increment value in primary */
2021 g_putind (flags, offs); /* Store the value back */
2031 /*****************************************************************************/
2032 /* Add a variable address to the value in ax */
2033 /*****************************************************************************/
2037 void g_addaddr_local (unsigned flags, int offs)
2038 /* Add the address of a local variable to ax */
2042 /* Add the offset */
2045 /* We cannot address more then 256 bytes of locals anyway */
2046 L = GetLocalLabel();
2047 CheckLocalOffs (offs);
2048 AddCodeLine ("clc");
2049 AddCodeLine ("adc #$%02X", offs & 0xFF);
2050 /* Do also skip the CLC insn below */
2051 AddCodeLine ("bcc %s", LocalLabelName (L));
2052 AddCodeLine ("inx");
2055 /* Add the current stackpointer value */
2056 AddCodeLine ("clc");
2058 /* Label was used above */
2061 AddCodeLine ("adc sp");
2062 AddCodeLine ("tay");
2063 AddCodeLine ("txa");
2064 AddCodeLine ("adc sp+1");
2065 AddCodeLine ("tax");
2066 AddCodeLine ("tya");
2071 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2072 /* Add the address of a static variable to ax */
2074 /* Create the correct label name */
2075 char* lbuf = GetLabelName (flags, label, offs);
2077 /* Add the address to the current ax value */
2078 AddCodeLine ("clc");
2079 AddCodeLine ("adc #<(%s)", lbuf);
2080 AddCodeLine ("tay");
2081 AddCodeLine ("txa");
2082 AddCodeLine ("adc #>(%s)", lbuf);
2083 AddCodeLine ("tax");
2084 AddCodeLine ("tya");
2089 /*****************************************************************************/
2091 /*****************************************************************************/
2095 void g_save (unsigned flags)
2096 /* Copy primary register to hold register. */
2098 /* Check the size and determine operation */
2099 switch (flags & CF_TYPE) {
2102 if (flags & CF_FORCECHAR) {
2103 AddCodeLine ("pha");
2109 AddCodeLine ("sta regsave");
2110 AddCodeLine ("stx regsave+1");
2114 AddCodeLine ("jsr saveeax");
2124 void g_restore (unsigned flags)
2125 /* Copy hold register to primary. */
2127 /* Check the size and determine operation */
2128 switch (flags & CF_TYPE) {
2131 if (flags & CF_FORCECHAR) {
2132 AddCodeLine ("pla");
2138 AddCodeLine ("lda regsave");
2139 AddCodeLine ("ldx regsave+1");
2143 AddCodeLine ("jsr resteax");
2153 void g_cmp (unsigned flags, unsigned long val)
2154 /* Immidiate compare. The primary register will not be changed, Z flag
2160 /* Check the size and determine operation */
2161 switch (flags & CF_TYPE) {
2164 if (flags & CF_FORCECHAR) {
2165 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2171 L = GetLocalLabel();
2172 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2173 AddCodeLine ("bne %s", LocalLabelName (L));
2174 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2179 Internal ("g_cmp: Long compares not implemented");
2189 static void oper (unsigned flags, unsigned long val, char** subs)
2190 /* Encode a binary operation. subs is a pointer to four groups of three
2192 * 0-2 --> Operate on ints
2193 * 3-5 --> Operate on unsigneds
2194 * 6-8 --> Operate on longs
2195 * 9-11 --> Operate on unsigned longs
2197 * The first subroutine names in each string group is used to encode an
2198 * operation with a zero constant, the second to encode an operation with
2199 * a 8 bit constant, and the third is used in all other cases.
2204 /* Determine the offset into the array */
2205 offs = (flags & CF_UNSIGNED)? 3 : 0;
2206 switch (flags & CF_TYPE) {
2219 /* Encode the operation */
2220 if (flags & CF_CONST) {
2221 /* Constant value given */
2222 if (val == 0 && subs [offs+0]) {
2223 /* Special case: constant with value zero */
2224 AddCodeLine ("jsr %s", subs [offs+0]);
2225 } else if (val < 0x100 && subs [offs+1]) {
2226 /* Special case: constant with high byte zero */
2227 ldaconst (val); /* Load low byte */
2228 AddCodeLine ("jsr %s", subs [offs+1]);
2230 /* Others: arbitrary constant value */
2231 g_getimmed (flags, val, 0); /* Load value */
2232 AddCodeLine ("jsr %s", subs [offs+2]);
2235 /* Value not constant (is already in (e)ax) */
2236 AddCodeLine ("jsr %s", subs [offs+2]);
2239 /* The operation will pop it's argument */
2245 void g_test (unsigned flags)
2246 /* Test the value in the primary and set the condition codes */
2248 switch (flags & CF_TYPE) {
2251 if (flags & CF_FORCECHAR) {
2252 AddCodeLine ("tax");
2258 AddCodeLine ("stx tmp1");
2259 AddCodeLine ("ora tmp1");
2263 if (flags & CF_UNSIGNED) {
2264 AddCodeLine ("jsr utsteax");
2266 AddCodeLine ("jsr tsteax");
2278 void g_push (unsigned flags, unsigned long val)
2279 /* Push the primary register or a constant value onto the stack */
2283 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2285 /* We have a constant 8 or 16 bit value */
2286 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2288 /* Handle as 8 bit value */
2289 if (CodeSizeFactor >= 165 || val > 2) {
2291 AddCodeLine ("jsr pusha");
2293 AddCodeLine ("jsr pushc%d", (int) val);
2298 /* Handle as 16 bit value */
2299 hi = (unsigned char) (val >> 8);
2301 AddCodeLine ("jsr push%u", (unsigned) val);
2302 } else if (hi == 0 || hi == 0xFF) {
2303 /* Use special function */
2305 AddCodeLine ("jsr %s", (hi == 0)? "pusha0" : "pushaFF");
2308 g_getimmed (flags, val, 0);
2309 AddCodeLine ("jsr pushax");
2315 /* Value is not 16 bit or not constant */
2316 if (flags & CF_CONST) {
2317 /* Constant 32 bit value, load into eax */
2318 g_getimmed (flags, val, 0);
2321 /* Push the primary register */
2322 switch (flags & CF_TYPE) {
2325 if (flags & CF_FORCECHAR) {
2326 /* Handle as char */
2327 AddCodeLine ("jsr pusha");
2332 AddCodeLine ("jsr pushax");
2336 AddCodeLine ("jsr pusheax");
2346 /* Adjust the stack offset */
2352 void g_swap (unsigned flags)
2353 /* Swap the primary register and the top of the stack. flags give the type
2354 * of *both* values (must have same size).
2357 switch (flags & CF_TYPE) {
2361 AddCodeLine ("jsr swapstk");
2365 AddCodeLine ("jsr swapestk");
2376 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2377 /* Call the specified subroutine name */
2379 if ((Flags & CF_FIXARGC) == 0) {
2380 /* Pass the argument count */
2383 AddCodeLine ("jsr _%s", Label);
2384 oursp += ArgSize; /* callee pops args */
2389 void g_callind (unsigned Flags, unsigned ArgSize)
2390 /* Call subroutine with address in AX */
2392 if ((Flags & CF_FIXARGC) == 0) {
2393 /* Pass arg count */
2396 AddCodeLine ("jsr callax"); /* do the call */
2397 oursp += ArgSize; /* callee pops args */
2402 void g_jump (unsigned Label)
2403 /* Jump to specified internal label number */
2405 AddCodeLine ("jmp %s", LocalLabelName (Label));
2410 void g_switch (unsigned Flags)
2411 /* Output switch statement preamble */
2413 switch (Flags & CF_TYPE) {
2417 AddCodeLine ("jsr switch");
2421 AddCodeLine ("jsr lswitch");
2432 void g_case (unsigned flags, unsigned label, unsigned long val)
2433 /* Create table code for one case selector */
2435 switch (flags & CF_TYPE) {
2439 AddCodeLine (".word $%04X, %s",
2440 (unsigned)(val & 0xFFFF),
2441 LocalLabelName (label));
2445 AddCodeLine (".dword $%08lX", val);
2446 AddCodeLine (".word %s", LocalLabelName (label));
2457 void g_truejump (unsigned flags, unsigned label)
2458 /* Jump to label if zero flag clear */
2460 AddCodeLine ("jne %s", LocalLabelName (label));
2465 void g_falsejump (unsigned flags, unsigned label)
2466 /* Jump to label if zero flag set */
2468 AddCodeLine ("jeq %s", LocalLabelName (label));
2473 static void mod_internal (int k, char* verb1, char* verb2)
2476 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2480 AddCodeLine ("jsr %ssp", verb2);
2486 void g_space (int space)
2487 /* Create or drop space on the stack */
2490 mod_internal (-space, "inc", "addy");
2491 } else if (space > 0) {
2492 mod_internal (space, "dec", "suby");
2498 void g_cstackcheck (void)
2499 /* Check for a C stack overflow */
2501 AddCodeLine ("jsr cstkchk");
2506 void g_stackcheck (void)
2507 /* Check for a stack overflow */
2509 AddCodeLine ("jsr stkchk");
2514 void g_add (unsigned flags, unsigned long val)
2515 /* Primary = TOS + Primary */
2517 static char* ops [12] = {
2518 0, "tosadda0", "tosaddax",
2519 0, "tosadda0", "tosaddax",
2524 if (flags & CF_CONST) {
2525 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2526 g_push (flags & ~CF_CONST, 0);
2528 oper (flags, val, ops);
2533 void g_sub (unsigned flags, unsigned long val)
2534 /* Primary = TOS - Primary */
2536 static char* ops [12] = {
2537 0, "tossuba0", "tossubax",
2538 0, "tossuba0", "tossubax",
2543 if (flags & CF_CONST) {
2544 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2545 g_push (flags & ~CF_CONST, 0);
2547 oper (flags, val, ops);
2552 void g_rsub (unsigned flags, unsigned long val)
2553 /* Primary = Primary - TOS */
2555 static char* ops [12] = {
2556 0, "tosrsuba0", "tosrsubax",
2557 0, "tosrsuba0", "tosrsubax",
2561 oper (flags, val, ops);
2566 void g_mul (unsigned flags, unsigned long val)
2567 /* Primary = TOS * Primary */
2569 static char* ops [12] = {
2570 0, "tosmula0", "tosmulax",
2571 0, "tosumula0", "tosumulax",
2578 /* Do strength reduction if the value is constant and a power of two */
2579 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2580 /* Generate a shift instead */
2585 /* If the right hand side is const, the lhs is not on stack but still
2586 * in the primary register.
2588 if (flags & CF_CONST) {
2590 switch (flags & CF_TYPE) {
2593 if (flags & CF_FORCECHAR) {
2594 /* Handle some special cases */
2598 AddCodeLine ("sta tmp1");
2599 AddCodeLine ("asl a");
2600 AddCodeLine ("clc");
2601 AddCodeLine ("adc tmp1");
2605 AddCodeLine ("sta tmp1");
2606 AddCodeLine ("asl a");
2607 AddCodeLine ("asl a");
2608 AddCodeLine ("clc");
2609 AddCodeLine ("adc tmp1");
2613 AddCodeLine ("sta tmp1");
2614 AddCodeLine ("asl a");
2615 AddCodeLine ("asl a");
2616 AddCodeLine ("clc");
2617 AddCodeLine ("adc tmp1");
2618 AddCodeLine ("asl a");
2634 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2635 * into the normal, non-optimized stuff.
2637 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2638 g_push (flags & ~CF_CONST, 0);
2642 /* Use long way over the stack */
2643 oper (flags, val, ops);
2648 void g_div (unsigned flags, unsigned long val)
2649 /* Primary = TOS / Primary */
2651 static char* ops [12] = {
2652 0, "tosdiva0", "tosdivax",
2653 0, "tosudiva0", "tosudivax",
2658 /* Do strength reduction if the value is constant and a power of two */
2660 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2661 /* Generate a shift instead */
2664 /* Generate a division */
2665 if (flags & CF_CONST) {
2666 /* lhs is not on stack */
2667 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2668 g_push (flags & ~CF_CONST, 0);
2670 oper (flags, val, ops);
2676 void g_mod (unsigned flags, unsigned long val)
2677 /* Primary = TOS % Primary */
2679 static char* ops [12] = {
2680 0, "tosmoda0", "tosmodax",
2681 0, "tosumoda0", "tosumodax",
2687 /* Check if we can do some cost reduction */
2688 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2689 /* We can do that with an AND operation */
2690 g_and (flags, val - 1);
2692 /* Do it the hard way... */
2693 if (flags & CF_CONST) {
2694 /* lhs is not on stack */
2695 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2696 g_push (flags & ~CF_CONST, 0);
2698 oper (flags, val, ops);
2704 void g_or (unsigned flags, unsigned long val)
2705 /* Primary = TOS | Primary */
2707 static char* ops [12] = {
2708 0, "tosora0", "tosorax",
2709 0, "tosora0", "tosorax",
2714 /* If the right hand side is const, the lhs is not on stack but still
2715 * in the primary register.
2717 if (flags & CF_CONST) {
2719 switch (flags & CF_TYPE) {
2722 if (flags & CF_FORCECHAR) {
2723 if ((val & 0xFF) != 0xFF) {
2724 AddCodeLine ("ora #$%02X", (unsigned char)val);
2732 AddCodeLine ("ora #$%02X", (unsigned char)val);
2739 AddCodeLine ("ora #$%02X", (unsigned char)val);
2748 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2749 * into the normal, non-optimized stuff.
2751 g_push (flags & ~CF_CONST, 0);
2755 /* Use long way over the stack */
2756 oper (flags, val, ops);
2761 void g_xor (unsigned flags, unsigned long val)
2762 /* Primary = TOS ^ Primary */
2764 static char* ops [12] = {
2765 0, "tosxora0", "tosxorax",
2766 0, "tosxora0", "tosxorax",
2772 /* If the right hand side is const, the lhs is not on stack but still
2773 * in the primary register.
2775 if (flags & CF_CONST) {
2777 switch (flags & CF_TYPE) {
2780 if (flags & CF_FORCECHAR) {
2781 if ((val & 0xFF) != 0) {
2782 AddCodeLine ("eor #$%02X", (unsigned char)val);
2791 AddCodeLine ("eor #$%02X", (unsigned char)val);
2794 } else if ((val & 0xFF) == 0) {
2795 AddCodeLine ("pha");
2796 AddCodeLine ("txa");
2797 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2798 AddCodeLine ("tax");
2799 AddCodeLine ("pla");
2807 AddCodeLine ("eor #$%02X", (unsigned char)val);
2817 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2818 * into the normal, non-optimized stuff.
2820 g_push (flags & ~CF_CONST, 0);
2824 /* Use long way over the stack */
2825 oper (flags, val, ops);
2830 void g_and (unsigned flags, unsigned long val)
2831 /* Primary = TOS & Primary */
2833 static char* ops [12] = {
2834 0, "tosanda0", "tosandax",
2835 0, "tosanda0", "tosandax",
2840 /* If the right hand side is const, the lhs is not on stack but still
2841 * in the primary register.
2843 if (flags & CF_CONST) {
2845 switch (flags & CF_TYPE) {
2848 if (flags & CF_FORCECHAR) {
2849 AddCodeLine ("and #$%02X", (unsigned char)val);
2854 if ((val & 0xFFFF) != 0xFFFF) {
2859 } else if (val != 0xFF) {
2860 AddCodeLine ("and #$%02X", (unsigned char)val);
2862 } else if ((val & 0xFF00) == 0xFF00) {
2863 AddCodeLine ("and #$%02X", (unsigned char)val);
2864 } else if ((val & 0x00FF) == 0x0000) {
2865 AddCodeLine ("txa");
2866 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2867 AddCodeLine ("tax");
2870 AddCodeLine ("tay");
2871 AddCodeLine ("txa");
2872 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2873 AddCodeLine ("tax");
2874 AddCodeLine ("tya");
2875 if ((val & 0x00FF) != 0x00FF) {
2876 AddCodeLine ("and #$%02X", (unsigned char)val);
2885 AddCodeLine ("stx sreg+1");
2886 AddCodeLine ("stx sreg");
2887 if ((val & 0xFF) != 0xFF) {
2888 AddCodeLine ("and #$%02X", (unsigned char)val);
2891 } else if (val == 0xFF00) {
2893 AddCodeLine ("sta sreg+1");
2894 AddCodeLine ("sta sreg");
2903 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2904 * into the normal, non-optimized stuff.
2906 g_push (flags & ~CF_CONST, 0);
2910 /* Use long way over the stack */
2911 oper (flags, val, ops);
2916 void g_asr (unsigned flags, unsigned long val)
2917 /* Primary = TOS >> Primary */
2919 static char* ops [12] = {
2920 0, "tosasra0", "tosasrax",
2921 0, "tosshra0", "tosshrax",
2926 /* If the right hand side is const, the lhs is not on stack but still
2927 * in the primary register.
2929 if (flags & CF_CONST) {
2931 switch (flags & CF_TYPE) {
2935 if (val >= 1 && val <= 4) {
2936 if (flags & CF_UNSIGNED) {
2937 AddCodeLine ("jsr shrax%ld", val);
2939 AddCodeLine ("jsr asrax%ld", val);
2942 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2943 AddCodeLine ("txa");
2950 if (val >= 1 && val <= 4) {
2951 if (flags & CF_UNSIGNED) {
2952 AddCodeLine ("jsr shreax%ld", val);
2954 AddCodeLine ("jsr asreax%ld", val);
2957 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2958 AddCodeLine ("txa");
2959 AddCodeLine ("ldx sreg");
2960 AddCodeLine ("ldy sreg+1");
2961 AddCodeLine ("sty sreg");
2962 AddCodeLine ("ldy #$00");
2963 AddCodeLine ("sty sreg+1");
2965 } else if (val == 16) {
2966 AddCodeLine ("ldy #$00");
2967 AddCodeLine ("ldx sreg+1");
2968 if ((flags & CF_UNSIGNED) == 0) {
2969 unsigned L = GetLocalLabel();
2970 AddCodeLine ("bpl %s", LocalLabelName (L));
2971 AddCodeLine ("dey");
2974 AddCodeLine ("lda sreg");
2975 AddCodeLine ("sty sreg+1");
2976 AddCodeLine ("sty sreg");
2985 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2986 * into the normal, non-optimized stuff.
2988 g_push (flags & ~CF_CONST, 0);
2992 /* Use long way over the stack */
2993 oper (flags, val, ops);
2998 void g_asl (unsigned flags, unsigned long val)
2999 /* Primary = TOS << Primary */
3001 static char* ops [12] = {
3002 0, "tosasla0", "tosaslax",
3003 0, "tosshla0", "tosshlax",
3009 /* If the right hand side is const, the lhs is not on stack but still
3010 * in the primary register.
3012 if (flags & CF_CONST) {
3014 switch (flags & CF_TYPE) {
3018 if (val >= 1 && val <= 4) {
3019 if (flags & CF_UNSIGNED) {
3020 AddCodeLine ("jsr shlax%ld", val);
3022 AddCodeLine ("jsr aslax%ld", val);
3025 } else if (val == 8) {
3026 AddCodeLine ("tax");
3027 AddCodeLine ("lda #$00");
3033 if (val >= 1 && val <= 4) {
3034 if (flags & CF_UNSIGNED) {
3035 AddCodeLine ("jsr shleax%ld", val);
3037 AddCodeLine ("jsr asleax%ld", val);
3040 } else if (val == 8) {
3041 AddCodeLine ("ldy sreg");
3042 AddCodeLine ("sty sreg+1");
3043 AddCodeLine ("stx sreg");
3044 AddCodeLine ("tax");
3045 AddCodeLine ("lda #$00");
3047 } else if (val == 16) {
3048 AddCodeLine ("stx sreg+1");
3049 AddCodeLine ("sta sreg");
3050 AddCodeLine ("lda #$00");
3051 AddCodeLine ("tax");
3060 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3061 * into the normal, non-optimized stuff.
3063 g_push (flags & ~CF_CONST, 0);
3067 /* Use long way over the stack */
3068 oper (flags, val, ops);
3073 void g_neg (unsigned flags)
3074 /* Primary = -Primary */
3076 switch (flags & CF_TYPE) {
3080 AddCodeLine ("jsr negax");
3084 AddCodeLine ("jsr negeax");
3094 void g_bneg (unsigned flags)
3095 /* Primary = !Primary */
3097 switch (flags & CF_TYPE) {
3100 AddCodeLine ("jsr bnega");
3104 AddCodeLine ("jsr bnegax");
3108 AddCodeLine ("jsr bnegeax");
3118 void g_com (unsigned flags)
3119 /* Primary = ~Primary */
3121 switch (flags & CF_TYPE) {
3125 AddCodeLine ("jsr complax");
3129 AddCodeLine ("jsr compleax");
3139 void g_inc (unsigned flags, unsigned long val)
3140 /* Increment the primary register by a given number */
3142 /* Don't inc by zero */
3147 /* Generate code for the supported types */
3149 switch (flags & CF_TYPE) {
3152 if (flags & CF_FORCECHAR) {
3153 if (CPU == CPU_65C02 && val <= 2) {
3155 AddCodeLine ("ina");
3158 AddCodeLine ("clc");
3159 AddCodeLine ("adc #$%02X", (unsigned char)val);
3166 if (CPU == CPU_65C02 && val == 1) {
3167 unsigned L = GetLocalLabel();
3168 AddCodeLine ("ina");
3169 AddCodeLine ("bne %s", LocalLabelName (L));
3170 AddCodeLine ("inx");
3172 } else if (CodeSizeFactor < 200) {
3175 AddCodeLine ("jsr incax%lu", val);
3176 } else if (val <= 255) {
3178 AddCodeLine ("jsr incaxy");
3180 g_add (flags | CF_CONST, val);
3183 /* Inline the code */
3185 if ((val & 0xFF) != 0) {
3186 unsigned L = GetLocalLabel();
3187 AddCodeLine ("clc");
3188 AddCodeLine ("adc #$%02X", (unsigned char) val);
3189 AddCodeLine ("bcc %s", LocalLabelName (L));
3190 AddCodeLine ("inx");
3194 AddCodeLine ("inx");
3197 AddCodeLine ("inx");
3200 AddCodeLine ("clc");
3201 if ((val & 0xFF) != 0) {
3202 AddCodeLine ("adc #$%02X", (unsigned char) val);
3204 AddCodeLine ("pha");
3205 AddCodeLine ("txa");
3206 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3207 AddCodeLine ("tax");
3208 AddCodeLine ("pla");
3216 AddCodeLine ("jsr inceaxy");
3218 g_add (flags | CF_CONST, val);
3230 void g_dec (unsigned flags, unsigned long val)
3231 /* Decrement the primary register by a given number */
3233 /* Don't dec by zero */
3238 /* Generate code for the supported types */
3240 switch (flags & CF_TYPE) {
3243 if (flags & CF_FORCECHAR) {
3244 if (CPU == CPU_65C02 && val <= 2) {
3246 AddCodeLine ("dea");
3249 AddCodeLine ("sec");
3250 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3257 if (CodeSizeFactor < 200) {
3258 /* Use subroutines */
3260 AddCodeLine ("jsr decax%d", (int) val);
3261 } else if (val <= 255) {
3263 AddCodeLine ("jsr decaxy");
3265 g_sub (flags | CF_CONST, val);
3268 /* Inline the code */
3270 if ((val & 0xFF) != 0) {
3271 unsigned L = GetLocalLabel();
3272 AddCodeLine ("sec");
3273 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3274 AddCodeLine ("bcs %s", LocalLabelName (L));
3275 AddCodeLine ("dex");
3279 AddCodeLine ("dex");
3282 AddCodeLine ("dex");
3285 AddCodeLine ("sec");
3286 if ((val & 0xFF) != 0) {
3287 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3289 AddCodeLine ("pha");
3290 AddCodeLine ("txa");
3291 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3292 AddCodeLine ("tax");
3293 AddCodeLine ("pla");
3301 AddCodeLine ("jsr deceaxy");
3303 g_sub (flags | CF_CONST, val);
3316 * Following are the conditional operators. They compare the TOS against
3317 * the primary and put a literal 1 in the primary if the condition is
3318 * true, otherwise they clear the primary register
3323 void g_eq (unsigned flags, unsigned long val)
3324 /* Test for equal */
3326 static char* ops [12] = {
3327 "toseq00", "toseqa0", "toseqax",
3328 "toseq00", "toseqa0", "toseqax",
3335 /* If the right hand side is const, the lhs is not on stack but still
3336 * in the primary register.
3338 if (flags & CF_CONST) {
3340 switch (flags & CF_TYPE) {
3343 if (flags & CF_FORCECHAR) {
3344 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3345 AddCodeLine ("jsr booleq");
3351 L = GetLocalLabel();
3352 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3353 AddCodeLine ("bne %s", LocalLabelName (L));
3354 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3356 AddCodeLine ("jsr booleq");
3366 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3367 * into the normal, non-optimized stuff.
3369 g_push (flags & ~CF_CONST, 0);
3373 /* Use long way over the stack */
3374 oper (flags, val, ops);
3379 void g_ne (unsigned flags, unsigned long val)
3380 /* Test for not equal */
3382 static char* ops [12] = {
3383 "tosne00", "tosnea0", "tosneax",
3384 "tosne00", "tosnea0", "tosneax",
3391 /* If the right hand side is const, the lhs is not on stack but still
3392 * in the primary register.
3394 if (flags & CF_CONST) {
3396 switch (flags & CF_TYPE) {
3399 if (flags & CF_FORCECHAR) {
3400 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3401 AddCodeLine ("jsr boolne");
3407 L = GetLocalLabel();
3408 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3409 AddCodeLine ("bne %s", LocalLabelName (L));
3410 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3412 AddCodeLine ("jsr boolne");
3422 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3423 * into the normal, non-optimized stuff.
3425 g_push (flags & ~CF_CONST, 0);
3429 /* Use long way over the stack */
3430 oper (flags, val, ops);
3435 void g_lt (unsigned flags, unsigned long val)
3436 /* Test for less than */
3438 static char* ops [12] = {
3439 "toslt00", "toslta0", "tosltax",
3440 "tosult00", "tosulta0", "tosultax",
3445 /* If the right hand side is const, the lhs is not on stack but still
3446 * in the primary register.
3448 if (flags & CF_CONST) {
3450 /* Give a warning in some special cases */
3451 if ((flags & CF_UNSIGNED) && val == 0) {
3452 Warning ("Condition is never true");
3455 /* Look at the type */
3456 switch (flags & CF_TYPE) {
3459 if (flags & CF_FORCECHAR) {
3460 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3461 if (flags & CF_UNSIGNED) {
3462 AddCodeLine ("jsr boolult");
3464 AddCodeLine ("jsr boollt");
3471 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3472 /* If we have a signed compare against zero, we only need to
3473 * test the high byte.
3475 AddCodeLine ("txa");
3476 AddCodeLine ("jsr boollt");
3479 /* Direct code only for unsigned data types */
3480 if (flags & CF_UNSIGNED) {
3481 unsigned L = GetLocalLabel();
3482 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3483 AddCodeLine ("bne %s", LocalLabelName (L));
3484 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3486 AddCodeLine ("jsr boolult");
3492 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3493 /* If we have a signed compare against zero, we only need to
3494 * test the high byte.
3496 AddCodeLine ("lda sreg+1");
3497 AddCodeLine ("jsr boollt");
3506 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3507 * into the normal, non-optimized stuff.
3509 g_push (flags & ~CF_CONST, 0);
3513 /* Use long way over the stack */
3514 oper (flags, val, ops);
3519 void g_le (unsigned flags, unsigned long val)
3520 /* Test for less than or equal to */
3522 static char* ops [12] = {
3523 "tosle00", "toslea0", "tosleax",
3524 "tosule00", "tosulea0", "tosuleax",
3530 /* If the right hand side is const, the lhs is not on stack but still
3531 * in the primary register.
3533 if (flags & CF_CONST) {
3535 /* <= is not very effective on the 6502, so try to convert
3536 * it into < if the value is in a valid range.
3539 /* Look at the type */
3540 switch (flags & CF_TYPE) {
3543 if (flags & CF_FORCECHAR) {
3544 if (flags & CF_UNSIGNED) {
3546 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3547 AddCodeLine ("jsr boolult");
3549 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3550 AddCodeLine ("jsr boolule");
3554 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3555 AddCodeLine ("jsr boollt");
3557 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3558 AddCodeLine ("jsr boolle");
3566 if (flags & CF_UNSIGNED) {
3567 unsigned L = GetLocalLabel();
3568 const char* Name = "boolule";
3573 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3574 AddCodeLine ("bne %s", LocalLabelName (L));
3575 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3577 AddCodeLine ("jsr %s", Name);
3589 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3590 * into the normal, non-optimized stuff.
3592 g_push (flags & ~CF_CONST, 0);
3596 /* Use long way over the stack */
3597 oper (flags, val, ops);
3602 void g_gt (unsigned flags, unsigned long val)
3603 /* Test for greater than */
3605 static char* ops [12] = {
3606 "tosgt00", "tosgta0", "tosgtax",
3607 "tosugt00", "tosugta0", "tosugtax",
3613 /* If the right hand side is const, the lhs is not on stack but still
3614 * in the primary register.
3616 if (flags & CF_CONST) {
3618 /* > is not very effective on the 6502, so try to convert
3619 * it into >= if the value is in a valid range.
3622 /* Look at the type */
3623 switch (flags & CF_TYPE) {
3626 if (flags & CF_FORCECHAR) {
3627 if (flags & CF_UNSIGNED) {
3628 /* If we have a compare > 0, we will replace it by
3629 * != 0 here, since both are identical but the latter
3630 * is easier to optimize.
3633 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3634 AddCodeLine ("jsr boolne");
3635 } else if (val < 255) {
3636 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3637 AddCodeLine ("jsr booluge");
3639 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3640 AddCodeLine ("jsr boolugt");
3644 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3645 AddCodeLine ("jsr boolge");
3647 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3648 AddCodeLine ("jsr boolgt");
3656 if (flags & CF_UNSIGNED) {
3657 /* If we have a compare > 0, we will replace it by
3658 * != 0 here, since both are identical but the latter
3659 * is easier to optimize.
3662 AddCodeLine ("stx tmp1");
3663 AddCodeLine ("ora tmp1");
3664 AddCodeLine ("jsr boolne");
3666 unsigned L = GetLocalLabel();
3667 const char* Name = "boolugt";
3672 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3673 AddCodeLine ("bne %s", LocalLabelName (L));
3674 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3676 AddCodeLine ("jsr %s", Name);
3689 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3690 * into the normal, non-optimized stuff.
3692 g_push (flags & ~CF_CONST, 0);
3696 /* Use long way over the stack */
3697 oper (flags, val, ops);
3702 void g_ge (unsigned flags, unsigned long val)
3703 /* Test for greater than or equal to */
3705 static char* ops [12] = {
3706 "tosge00", "tosgea0", "tosgeax",
3707 "tosuge00", "tosugea0", "tosugeax",
3713 /* If the right hand side is const, the lhs is not on stack but still
3714 * in the primary register.
3716 if (flags & CF_CONST) {
3718 /* Give a warning in some special cases */
3719 if ((flags & CF_UNSIGNED) && val == 0) {
3720 Warning ("Condition is always true");
3723 /* Look at the type */
3724 switch (flags & CF_TYPE) {
3727 if (flags & CF_FORCECHAR) {
3728 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3729 if (flags & CF_UNSIGNED) {
3730 AddCodeLine ("jsr booluge");
3732 AddCodeLine ("jsr boolge");
3739 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3740 /* If we have a signed compare against zero, we only need to
3741 * test the high byte.
3743 AddCodeLine ("txa");
3744 AddCodeLine ("jsr boolge");
3747 /* Direct code only for unsigned data types */
3748 if (flags & CF_UNSIGNED) {
3749 unsigned L = GetLocalLabel();
3750 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3751 AddCodeLine ("bne %s", LocalLabelName (L));
3752 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3754 AddCodeLine ("jsr booluge");
3760 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3761 /* If we have a signed compare against zero, we only need to
3762 * test the high byte.
3764 AddCodeLine ("lda sreg+1");
3765 AddCodeLine ("jsr boolge");
3774 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3775 * into the normal, non-optimized stuff.
3777 g_push (flags & ~CF_CONST, 0);
3781 /* Use long way over the stack */
3782 oper (flags, val, ops);
3787 /*****************************************************************************/
3788 /* Allocating static storage */
3789 /*****************************************************************************/
3793 void g_res (unsigned n)
3794 /* Reserve static storage, n bytes */
3796 AddDataLine ("\t.res\t%u,$00", n);
3801 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3802 /* Define data with the size given in flags */
3804 if (flags & CF_CONST) {
3806 /* Numeric constant */
3807 switch (flags & CF_TYPE) {
3810 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
3814 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
3818 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3829 /* Create the correct label name */
3830 const char* Label = GetLabelName (flags, val, offs);
3832 /* Labels are always 16 bit */
3833 AddDataLine ("\t.word\t%s", Label);
3840 void g_defbytes (const void* Bytes, unsigned Count)
3841 /* Output a row of bytes as a constant */
3847 /* Cast the buffer pointer */
3848 const unsigned char* Data = (const unsigned char*) Bytes;
3850 /* Output the stuff */
3853 /* How many go into this line? */
3854 if ((Chunk = Count) > 16) {
3859 /* Output one line */
3860 strcpy (Buf, "\t.byte\t");
3863 B += sprintf (B, "$%02X", *Data++);
3869 /* Output the line */
3876 void g_zerobytes (unsigned n)
3877 /* Output n bytes of data initialized with zero */
3879 AddDataLine ("\t.res\t%u,$00", n);
3884 /*****************************************************************************/
3885 /* User supplied assembler code */
3886 /*****************************************************************************/
3890 void g_asmcode (const char* Line, int Len)
3891 /* Output one line of assembler code. If Len is greater than zero, it is used
3892 * as the maximum number of characters to use from Line.
3896 AddCodeLine ("%.*s", Len, Line);
3898 AddCodeLine ("%s", Line);
3904 /*****************************************************************************/
3905 /* Inlined known functions */
3906 /*****************************************************************************/
3910 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3911 /* Inline the strlen() function */
3913 /* We need a label in both cases */
3914 unsigned label = GetLocalLabel ();
3916 /* Two different encodings */
3917 if (flags & CF_CONST) {
3919 /* The address of the string is constant. Create the correct label name */
3920 char* lbuf = GetLabelName (flags, val, offs);
3922 /* Generate the strlen code */
3923 AddCodeLine ("ldy #$FF");
3924 g_defcodelabel (label);
3925 AddCodeLine ("iny");
3926 AddCodeLine ("lda %s,y", lbuf);
3927 AddCodeLine ("bne %s", LocalLabelName (label));
3928 AddCodeLine ("tax");
3929 AddCodeLine ("tya");
3933 /* Address not constant but in primary */
3934 if (CodeSizeFactor < 400) {
3935 /* This is too much code, so call strlen instead of inlining */
3936 AddCodeLine ("jsr _strlen");
3938 /* Inline the function */
3939 AddCodeLine ("sta ptr1");
3940 AddCodeLine ("stx ptr1+1");
3941 AddCodeLine ("ldy #$FF");
3942 g_defcodelabel (label);
3943 AddCodeLine ("iny");
3944 AddCodeLine ("lda (ptr1),y");
3945 AddCodeLine ("bne %s", LocalLabelName (label));
3946 AddCodeLine ("tax");
3947 AddCodeLine ("tya");