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 addeqysp");
1721 if (flags & CF_CONST) {
1722 g_getimmed (flags, val, 0);
1725 AddCodeLine ("jsr laddeq0sp");
1728 AddCodeLine ("jsr laddeqysp");
1739 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1740 /* Emit += for the location with address in ax */
1742 /* If the offset is too large for a byte register, add the high byte
1743 * of the offset to the primary. Beware: We need a special correction
1744 * if the offset in the low byte will overflow in the operation.
1746 offs = MakeByteOffs (flags, offs);
1748 /* Check the size and determine operation */
1749 switch (flags & CF_TYPE) {
1752 AddCodeLine ("sta ptr1");
1753 AddCodeLine ("stx ptr1+1");
1754 AddCodeLine ("ldy #$%02X", offs);
1755 AddCodeLine ("ldx #$00");
1756 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1757 AddCodeLine ("clc");
1758 AddCodeLine ("adc (ptr1),y");
1759 AddCodeLine ("sta (ptr1),y");
1763 if (CodeSizeFactor >= 200) {
1764 /* Lots of code, use only if size is not important */
1765 AddCodeLine ("sta ptr1");
1766 AddCodeLine ("stx ptr1+1");
1767 AddCodeLine ("ldy #$%02X", offs);
1768 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1769 AddCodeLine ("clc");
1770 AddCodeLine ("adc (ptr1),y");
1771 AddCodeLine ("sta (ptr1),y");
1772 AddCodeLine ("pha");
1773 AddCodeLine ("iny");
1774 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1775 AddCodeLine ("adc (ptr1),y");
1776 AddCodeLine ("sta (ptr1),y");
1777 AddCodeLine ("tax");
1778 AddCodeLine ("pla");
1784 AddCodeLine ("jsr pushax"); /* Push the address */
1785 push (CF_PTR); /* Correct the internal sp */
1786 g_getind (flags, offs); /* Fetch the value */
1787 g_inc (flags, val); /* Increment value in primary */
1788 g_putind (flags, offs); /* Store the value back */
1798 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1800 /* Emit -= for a static variable */
1802 /* Create the correct label name */
1803 char* lbuf = GetLabelName (flags, label, offs);
1805 /* Check the size and determine operation */
1806 switch (flags & CF_TYPE) {
1809 if (flags & CF_FORCECHAR) {
1810 AddCodeLine ("ldx #$00");
1811 if (flags & CF_CONST) {
1813 AddCodeLine ("dec %s", lbuf);
1814 AddCodeLine ("lda %s", lbuf);
1816 AddCodeLine ("sec");
1817 AddCodeLine ("lda %s", lbuf);
1818 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1819 AddCodeLine ("sta %s", lbuf);
1822 AddCodeLine ("sec");
1823 AddCodeLine ("sta tmp1");
1824 AddCodeLine ("lda %s", lbuf);
1825 AddCodeLine ("sbc tmp1");
1826 AddCodeLine ("sta %s", lbuf);
1828 if ((flags & CF_UNSIGNED) == 0) {
1829 unsigned L = GetLocalLabel();
1830 AddCodeLine ("bpl %s", LocalLabelName (L));
1831 AddCodeLine ("dex");
1839 AddCodeLine ("sec");
1840 if (flags & CF_CONST) {
1841 AddCodeLine ("lda %s", lbuf);
1842 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1843 AddCodeLine ("sta %s", lbuf);
1845 unsigned L = GetLocalLabel ();
1846 AddCodeLine ("bcs %s", LocalLabelName (L));
1847 AddCodeLine ("dec %s+1", lbuf);
1849 AddCodeLine ("ldx %s+1", lbuf);
1851 AddCodeLine ("lda %s+1", lbuf);
1852 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1853 AddCodeLine ("sta %s+1", lbuf);
1854 AddCodeLine ("tax");
1855 AddCodeLine ("lda %s", lbuf);
1858 AddCodeLine ("sta tmp1");
1859 AddCodeLine ("lda %s", lbuf);
1860 AddCodeLine ("sbc tmp1");
1861 AddCodeLine ("sta %s", lbuf);
1862 AddCodeLine ("stx tmp1");
1863 AddCodeLine ("lda %s+1", lbuf);
1864 AddCodeLine ("sbc tmp1");
1865 AddCodeLine ("sta %s+1", lbuf);
1866 AddCodeLine ("tax");
1867 AddCodeLine ("lda %s", lbuf);
1872 if (flags & CF_CONST) {
1874 AddCodeLine ("ldy #<(%s)", lbuf);
1875 AddCodeLine ("sty ptr1");
1876 AddCodeLine ("ldy #>(%s+1)", lbuf);
1878 AddCodeLine ("jsr lsubeq1");
1880 AddCodeLine ("lda #$%02X", (unsigned char)val);
1881 AddCodeLine ("jsr lsubeqa");
1884 g_getstatic (flags, label, offs);
1886 g_putstatic (flags, label, offs);
1889 AddCodeLine ("ldy #<(%s)", lbuf);
1890 AddCodeLine ("sty ptr1");
1891 AddCodeLine ("ldy #>(%s+1)", lbuf);
1892 AddCodeLine ("jsr lsubeq");
1903 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1904 /* Emit -= for a local variable */
1906 /* Calculate the true offset, check it, load it into Y */
1908 CheckLocalOffs (offs);
1910 /* Check the size and determine operation */
1911 switch (flags & CF_TYPE) {
1914 if (flags & CF_FORCECHAR) {
1916 AddCodeLine ("ldx #$00");
1917 AddCodeLine ("sec");
1918 if (flags & CF_CONST) {
1919 AddCodeLine ("lda (sp),y");
1920 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1922 AddCodeLine ("sta tmp1");
1923 AddCodeLine ("lda (sp),y");
1924 AddCodeLine ("sbc tmp1");
1926 AddCodeLine ("sta (sp),y");
1927 if ((flags & CF_UNSIGNED) == 0) {
1928 unsigned L = GetLocalLabel();
1929 AddCodeLine ("bpl %s", LocalLabelName (L));
1930 AddCodeLine ("dex");
1938 if (flags & CF_CONST) {
1939 g_getimmed (flags, val, 0);
1942 AddCodeLine ("jsr subeq0sp");
1945 AddCodeLine ("jsr subeqysp");
1950 if (flags & CF_CONST) {
1951 g_getimmed (flags, val, 0);
1954 AddCodeLine ("jsr lsubeq0sp");
1957 AddCodeLine ("jsr lsubeqysp");
1968 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
1969 /* Emit -= for the location with address in ax */
1971 /* If the offset is too large for a byte register, add the high byte
1972 * of the offset to the primary. Beware: We need a special correction
1973 * if the offset in the low byte will overflow in the operation.
1975 offs = MakeByteOffs (flags, offs);
1977 /* Check the size and determine operation */
1978 switch (flags & CF_TYPE) {
1981 AddCodeLine ("sta ptr1");
1982 AddCodeLine ("stx ptr1+1");
1983 AddCodeLine ("ldy #$%02X", offs);
1984 AddCodeLine ("ldx #$00");
1985 AddCodeLine ("lda (ptr1),y");
1986 AddCodeLine ("sec");
1987 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1988 AddCodeLine ("sta (ptr1),y");
1992 if (CodeSizeFactor >= 200) {
1993 /* Lots of code, use only if size is not important */
1994 AddCodeLine ("sta ptr1");
1995 AddCodeLine ("stx ptr1+1");
1996 AddCodeLine ("ldy #$%02X", offs);
1997 AddCodeLine ("lda (ptr1),y");
1998 AddCodeLine ("sec");
1999 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2000 AddCodeLine ("sta (ptr1),y");
2001 AddCodeLine ("pha");
2002 AddCodeLine ("iny");
2003 AddCodeLine ("lda (ptr1),y");
2004 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2005 AddCodeLine ("sta (ptr1),y");
2006 AddCodeLine ("tax");
2007 AddCodeLine ("pla");
2013 AddCodeLine ("jsr pushax"); /* Push the address */
2014 push (CF_PTR); /* Correct the internal sp */
2015 g_getind (flags, offs); /* Fetch the value */
2016 g_dec (flags, val); /* Increment value in primary */
2017 g_putind (flags, offs); /* Store the value back */
2027 /*****************************************************************************/
2028 /* Add a variable address to the value in ax */
2029 /*****************************************************************************/
2033 void g_addaddr_local (unsigned flags, int offs)
2034 /* Add the address of a local variable to ax */
2038 /* Add the offset */
2041 /* We cannot address more then 256 bytes of locals anyway */
2042 L = GetLocalLabel();
2043 CheckLocalOffs (offs);
2044 AddCodeLine ("clc");
2045 AddCodeLine ("adc #$%02X", offs & 0xFF);
2046 /* Do also skip the CLC insn below */
2047 AddCodeLine ("bcc %s", LocalLabelName (L));
2048 AddCodeLine ("inx");
2051 /* Add the current stackpointer value */
2052 AddCodeLine ("clc");
2054 /* Label was used above */
2057 AddCodeLine ("adc sp");
2058 AddCodeLine ("tay");
2059 AddCodeLine ("txa");
2060 AddCodeLine ("adc sp+1");
2061 AddCodeLine ("tax");
2062 AddCodeLine ("tya");
2067 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2068 /* Add the address of a static variable to ax */
2070 /* Create the correct label name */
2071 char* lbuf = GetLabelName (flags, label, offs);
2073 /* Add the address to the current ax value */
2074 AddCodeLine ("clc");
2075 AddCodeLine ("adc #<(%s)", lbuf);
2076 AddCodeLine ("tay");
2077 AddCodeLine ("txa");
2078 AddCodeLine ("adc #>(%s)", lbuf);
2079 AddCodeLine ("tax");
2080 AddCodeLine ("tya");
2085 /*****************************************************************************/
2087 /*****************************************************************************/
2091 void g_save (unsigned flags)
2092 /* Copy primary register to hold register. */
2094 /* Check the size and determine operation */
2095 switch (flags & CF_TYPE) {
2098 if (flags & CF_FORCECHAR) {
2099 AddCodeLine ("pha");
2105 AddCodeLine ("sta regsave");
2106 AddCodeLine ("stx regsave+1");
2110 AddCodeLine ("jsr saveeax");
2120 void g_restore (unsigned flags)
2121 /* Copy hold register to primary. */
2123 /* Check the size and determine operation */
2124 switch (flags & CF_TYPE) {
2127 if (flags & CF_FORCECHAR) {
2128 AddCodeLine ("pla");
2134 AddCodeLine ("lda regsave");
2135 AddCodeLine ("ldx regsave+1");
2139 AddCodeLine ("jsr resteax");
2149 void g_cmp (unsigned flags, unsigned long val)
2150 /* Immidiate compare. The primary register will not be changed, Z flag
2156 /* Check the size and determine operation */
2157 switch (flags & CF_TYPE) {
2160 if (flags & CF_FORCECHAR) {
2161 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2167 L = GetLocalLabel();
2168 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2169 AddCodeLine ("bne %s", LocalLabelName (L));
2170 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2175 Internal ("g_cmp: Long compares not implemented");
2185 static void oper (unsigned flags, unsigned long val, char** subs)
2186 /* Encode a binary operation. subs is a pointer to four groups of three
2188 * 0-2 --> Operate on ints
2189 * 3-5 --> Operate on unsigneds
2190 * 6-8 --> Operate on longs
2191 * 9-11 --> Operate on unsigned longs
2193 * The first subroutine names in each string group is used to encode an
2194 * operation with a zero constant, the second to encode an operation with
2195 * a 8 bit constant, and the third is used in all other cases.
2200 /* Determine the offset into the array */
2201 offs = (flags & CF_UNSIGNED)? 3 : 0;
2202 switch (flags & CF_TYPE) {
2215 /* Encode the operation */
2216 if (flags & CF_CONST) {
2217 /* Constant value given */
2218 if (val == 0 && subs [offs+0]) {
2219 /* Special case: constant with value zero */
2220 AddCodeLine ("jsr %s", subs [offs+0]);
2221 } else if (val < 0x100 && subs [offs+1]) {
2222 /* Special case: constant with high byte zero */
2223 ldaconst (val); /* Load low byte */
2224 AddCodeLine ("jsr %s", subs [offs+1]);
2226 /* Others: arbitrary constant value */
2227 g_getimmed (flags, val, 0); /* Load value */
2228 AddCodeLine ("jsr %s", subs [offs+2]);
2231 /* Value not constant (is already in (e)ax) */
2232 AddCodeLine ("jsr %s", subs [offs+2]);
2235 /* The operation will pop it's argument */
2241 void g_test (unsigned flags)
2242 /* Test the value in the primary and set the condition codes */
2244 switch (flags & CF_TYPE) {
2247 if (flags & CF_FORCECHAR) {
2248 AddCodeLine ("tax");
2254 AddCodeLine ("stx tmp1");
2255 AddCodeLine ("ora tmp1");
2259 if (flags & CF_UNSIGNED) {
2260 AddCodeLine ("jsr utsteax");
2262 AddCodeLine ("jsr tsteax");
2274 void g_push (unsigned flags, unsigned long val)
2275 /* Push the primary register or a constant value onto the stack */
2279 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2281 /* We have a constant 8 or 16 bit value */
2282 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2284 /* Handle as 8 bit value */
2285 if (CodeSizeFactor >= 165 || val > 2) {
2287 AddCodeLine ("jsr pusha");
2289 AddCodeLine ("jsr pushc%d", (int) val);
2294 /* Handle as 16 bit value */
2295 hi = (unsigned char) (val >> 8);
2297 AddCodeLine ("jsr push%u", (unsigned) val);
2298 } else if (hi == 0 || hi == 0xFF) {
2299 /* Use special function */
2301 AddCodeLine ("jsr %s", (hi == 0)? "pusha0" : "pushaFF");
2304 g_getimmed (flags, val, 0);
2305 AddCodeLine ("jsr pushax");
2311 /* Value is not 16 bit or not constant */
2312 if (flags & CF_CONST) {
2313 /* Constant 32 bit value, load into eax */
2314 g_getimmed (flags, val, 0);
2317 /* Push the primary register */
2318 switch (flags & CF_TYPE) {
2321 if (flags & CF_FORCECHAR) {
2322 /* Handle as char */
2323 AddCodeLine ("jsr pusha");
2328 AddCodeLine ("jsr pushax");
2332 AddCodeLine ("jsr pusheax");
2342 /* Adjust the stack offset */
2348 void g_swap (unsigned flags)
2349 /* Swap the primary register and the top of the stack. flags give the type
2350 * of *both* values (must have same size).
2353 switch (flags & CF_TYPE) {
2357 AddCodeLine ("jsr swapstk");
2361 AddCodeLine ("jsr swapestk");
2372 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2373 /* Call the specified subroutine name */
2375 if ((Flags & CF_FIXARGC) == 0) {
2376 /* Pass the argument count */
2379 AddCodeLine ("jsr _%s", Label);
2380 oursp += ArgSize; /* callee pops args */
2385 void g_callind (unsigned Flags, unsigned ArgSize)
2386 /* Call subroutine with address in AX */
2388 if ((Flags & CF_FIXARGC) == 0) {
2389 /* Pass arg count */
2392 AddCodeLine ("jsr callax"); /* do the call */
2393 oursp += ArgSize; /* callee pops args */
2398 void g_jump (unsigned Label)
2399 /* Jump to specified internal label number */
2401 AddCodeLine ("jmp %s", LocalLabelName (Label));
2406 void g_switch (unsigned Flags)
2407 /* Output switch statement preamble */
2409 switch (Flags & CF_TYPE) {
2413 AddCodeLine ("jsr switch");
2417 AddCodeLine ("jsr lswitch");
2428 void g_case (unsigned flags, unsigned label, unsigned long val)
2429 /* Create table code for one case selector */
2431 switch (flags & CF_TYPE) {
2435 AddCodeLine (".word $%04X, %s",
2436 (unsigned)(val & 0xFFFF),
2437 LocalLabelName (label));
2441 AddCodeLine (".dword $%08lX", val);
2442 AddCodeLine (".word %s", LocalLabelName (label));
2453 void g_truejump (unsigned flags, unsigned label)
2454 /* Jump to label if zero flag clear */
2456 AddCodeLine ("jne %s", LocalLabelName (label));
2461 void g_falsejump (unsigned flags, unsigned label)
2462 /* Jump to label if zero flag set */
2464 AddCodeLine ("jeq %s", LocalLabelName (label));
2469 static void mod_internal (int k, char* verb1, char* verb2)
2472 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2476 AddCodeLine ("jsr %ssp", verb2);
2482 void g_space (int space)
2483 /* Create or drop space on the stack */
2486 mod_internal (-space, "inc", "addy");
2487 } else if (space > 0) {
2488 mod_internal (space, "dec", "suby");
2494 void g_cstackcheck (void)
2495 /* Check for a C stack overflow */
2497 AddCodeLine ("jsr cstkchk");
2502 void g_stackcheck (void)
2503 /* Check for a stack overflow */
2505 AddCodeLine ("jsr stkchk");
2510 void g_add (unsigned flags, unsigned long val)
2511 /* Primary = TOS + Primary */
2513 static char* ops [12] = {
2514 0, "tosadda0", "tosaddax",
2515 0, "tosadda0", "tosaddax",
2520 if (flags & CF_CONST) {
2521 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2522 g_push (flags & ~CF_CONST, 0);
2524 oper (flags, val, ops);
2529 void g_sub (unsigned flags, unsigned long val)
2530 /* Primary = TOS - Primary */
2532 static char* ops [12] = {
2533 0, "tossuba0", "tossubax",
2534 0, "tossuba0", "tossubax",
2539 if (flags & CF_CONST) {
2540 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2541 g_push (flags & ~CF_CONST, 0);
2543 oper (flags, val, ops);
2548 void g_rsub (unsigned flags, unsigned long val)
2549 /* Primary = Primary - TOS */
2551 static char* ops [12] = {
2552 0, "tosrsuba0", "tosrsubax",
2553 0, "tosrsuba0", "tosrsubax",
2557 oper (flags, val, ops);
2562 void g_mul (unsigned flags, unsigned long val)
2563 /* Primary = TOS * Primary */
2565 static char* ops [12] = {
2566 0, "tosmula0", "tosmulax",
2567 0, "tosumula0", "tosumulax",
2574 /* Do strength reduction if the value is constant and a power of two */
2575 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2576 /* Generate a shift instead */
2581 /* If the right hand side is const, the lhs is not on stack but still
2582 * in the primary register.
2584 if (flags & CF_CONST) {
2586 switch (flags & CF_TYPE) {
2589 if (flags & CF_FORCECHAR) {
2590 /* Handle some special cases */
2594 AddCodeLine ("sta tmp1");
2595 AddCodeLine ("asl a");
2596 AddCodeLine ("clc");
2597 AddCodeLine ("adc tmp1");
2601 AddCodeLine ("sta tmp1");
2602 AddCodeLine ("asl a");
2603 AddCodeLine ("asl a");
2604 AddCodeLine ("clc");
2605 AddCodeLine ("adc tmp1");
2609 AddCodeLine ("sta tmp1");
2610 AddCodeLine ("asl a");
2611 AddCodeLine ("asl a");
2612 AddCodeLine ("clc");
2613 AddCodeLine ("adc tmp1");
2614 AddCodeLine ("asl a");
2630 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2631 * into the normal, non-optimized stuff.
2633 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2634 g_push (flags & ~CF_CONST, 0);
2638 /* Use long way over the stack */
2639 oper (flags, val, ops);
2644 void g_div (unsigned flags, unsigned long val)
2645 /* Primary = TOS / Primary */
2647 static char* ops [12] = {
2648 0, "tosdiva0", "tosdivax",
2649 0, "tosudiva0", "tosudivax",
2654 /* Do strength reduction if the value is constant and a power of two */
2656 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2657 /* Generate a shift instead */
2660 /* Generate a division */
2661 if (flags & CF_CONST) {
2662 /* lhs is not on stack */
2663 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2664 g_push (flags & ~CF_CONST, 0);
2666 oper (flags, val, ops);
2672 void g_mod (unsigned flags, unsigned long val)
2673 /* Primary = TOS % Primary */
2675 static char* ops [12] = {
2676 0, "tosmoda0", "tosmodax",
2677 0, "tosumoda0", "tosumodax",
2683 /* Check if we can do some cost reduction */
2684 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2685 /* We can do that with an AND operation */
2686 g_and (flags, val - 1);
2688 /* Do it the hard way... */
2689 if (flags & CF_CONST) {
2690 /* lhs is not on stack */
2691 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2692 g_push (flags & ~CF_CONST, 0);
2694 oper (flags, val, ops);
2700 void g_or (unsigned flags, unsigned long val)
2701 /* Primary = TOS | Primary */
2703 static char* ops [12] = {
2704 0, "tosora0", "tosorax",
2705 0, "tosora0", "tosorax",
2710 /* If the right hand side is const, the lhs is not on stack but still
2711 * in the primary register.
2713 if (flags & CF_CONST) {
2715 switch (flags & CF_TYPE) {
2718 if (flags & CF_FORCECHAR) {
2719 if ((val & 0xFF) != 0xFF) {
2720 AddCodeLine ("ora #$%02X", (unsigned char)val);
2728 AddCodeLine ("ora #$%02X", (unsigned char)val);
2735 AddCodeLine ("ora #$%02X", (unsigned char)val);
2744 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2745 * into the normal, non-optimized stuff.
2747 g_push (flags & ~CF_CONST, 0);
2751 /* Use long way over the stack */
2752 oper (flags, val, ops);
2757 void g_xor (unsigned flags, unsigned long val)
2758 /* Primary = TOS ^ Primary */
2760 static char* ops [12] = {
2761 0, "tosxora0", "tosxorax",
2762 0, "tosxora0", "tosxorax",
2768 /* If the right hand side is const, the lhs is not on stack but still
2769 * in the primary register.
2771 if (flags & CF_CONST) {
2773 switch (flags & CF_TYPE) {
2776 if (flags & CF_FORCECHAR) {
2777 if ((val & 0xFF) != 0) {
2778 AddCodeLine ("eor #$%02X", (unsigned char)val);
2787 AddCodeLine ("eor #$%02X", (unsigned char)val);
2790 } else if ((val & 0xFF) == 0) {
2791 AddCodeLine ("pha");
2792 AddCodeLine ("txa");
2793 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2794 AddCodeLine ("tax");
2795 AddCodeLine ("pla");
2803 AddCodeLine ("eor #$%02X", (unsigned char)val);
2813 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2814 * into the normal, non-optimized stuff.
2816 g_push (flags & ~CF_CONST, 0);
2820 /* Use long way over the stack */
2821 oper (flags, val, ops);
2826 void g_and (unsigned flags, unsigned long val)
2827 /* Primary = TOS & Primary */
2829 static char* ops [12] = {
2830 0, "tosanda0", "tosandax",
2831 0, "tosanda0", "tosandax",
2836 /* If the right hand side is const, the lhs is not on stack but still
2837 * in the primary register.
2839 if (flags & CF_CONST) {
2841 switch (flags & CF_TYPE) {
2844 if (flags & CF_FORCECHAR) {
2845 AddCodeLine ("and #$%02X", (unsigned char)val);
2850 if ((val & 0xFFFF) != 0xFFFF) {
2855 } else if (val != 0xFF) {
2856 AddCodeLine ("and #$%02X", (unsigned char)val);
2858 } else if ((val & 0xFF00) == 0xFF00) {
2859 AddCodeLine ("and #$%02X", (unsigned char)val);
2860 } else if ((val & 0x00FF) == 0x0000) {
2861 AddCodeLine ("txa");
2862 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2863 AddCodeLine ("tax");
2866 AddCodeLine ("tay");
2867 AddCodeLine ("txa");
2868 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2869 AddCodeLine ("tax");
2870 AddCodeLine ("tya");
2871 if ((val & 0x00FF) != 0x00FF) {
2872 AddCodeLine ("and #$%02X", (unsigned char)val);
2881 AddCodeLine ("stx sreg+1");
2882 AddCodeLine ("stx sreg");
2883 if ((val & 0xFF) != 0xFF) {
2884 AddCodeLine ("and #$%02X", (unsigned char)val);
2887 } else if (val == 0xFF00) {
2889 AddCodeLine ("sta sreg+1");
2890 AddCodeLine ("sta sreg");
2899 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2900 * into the normal, non-optimized stuff.
2902 g_push (flags & ~CF_CONST, 0);
2906 /* Use long way over the stack */
2907 oper (flags, val, ops);
2912 void g_asr (unsigned flags, unsigned long val)
2913 /* Primary = TOS >> Primary */
2915 static char* ops [12] = {
2916 0, "tosasra0", "tosasrax",
2917 0, "tosshra0", "tosshrax",
2922 /* If the right hand side is const, the lhs is not on stack but still
2923 * in the primary register.
2925 if (flags & CF_CONST) {
2927 switch (flags & CF_TYPE) {
2931 if (val >= 1 && val <= 4) {
2932 if (flags & CF_UNSIGNED) {
2933 AddCodeLine ("jsr shrax%ld", val);
2935 AddCodeLine ("jsr asrax%ld", val);
2938 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2939 AddCodeLine ("txa");
2946 if (val >= 1 && val <= 4) {
2947 if (flags & CF_UNSIGNED) {
2948 AddCodeLine ("jsr shreax%ld", val);
2950 AddCodeLine ("jsr asreax%ld", val);
2953 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2954 AddCodeLine ("txa");
2955 AddCodeLine ("ldx sreg");
2956 AddCodeLine ("ldy sreg+1");
2957 AddCodeLine ("sty sreg");
2958 AddCodeLine ("ldy #$00");
2959 AddCodeLine ("sty sreg+1");
2961 } else if (val == 16) {
2962 AddCodeLine ("ldy #$00");
2963 AddCodeLine ("ldx sreg+1");
2964 if ((flags & CF_UNSIGNED) == 0) {
2965 unsigned L = GetLocalLabel();
2966 AddCodeLine ("bpl %s", LocalLabelName (L));
2967 AddCodeLine ("dey");
2970 AddCodeLine ("lda sreg");
2971 AddCodeLine ("sty sreg+1");
2972 AddCodeLine ("sty sreg");
2981 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2982 * into the normal, non-optimized stuff.
2984 g_push (flags & ~CF_CONST, 0);
2988 /* Use long way over the stack */
2989 oper (flags, val, ops);
2994 void g_asl (unsigned flags, unsigned long val)
2995 /* Primary = TOS << Primary */
2997 static char* ops [12] = {
2998 0, "tosasla0", "tosaslax",
2999 0, "tosshla0", "tosshlax",
3005 /* If the right hand side is const, the lhs is not on stack but still
3006 * in the primary register.
3008 if (flags & CF_CONST) {
3010 switch (flags & CF_TYPE) {
3014 if (val >= 1 && val <= 4) {
3015 if (flags & CF_UNSIGNED) {
3016 AddCodeLine ("jsr shlax%ld", val);
3018 AddCodeLine ("jsr aslax%ld", val);
3021 } else if (val == 8) {
3022 AddCodeLine ("tax");
3023 AddCodeLine ("lda #$00");
3029 if (val >= 1 && val <= 4) {
3030 if (flags & CF_UNSIGNED) {
3031 AddCodeLine ("jsr shleax%ld", val);
3033 AddCodeLine ("jsr asleax%ld", val);
3036 } else if (val == 8) {
3037 AddCodeLine ("ldy sreg");
3038 AddCodeLine ("sty sreg+1");
3039 AddCodeLine ("stx sreg");
3040 AddCodeLine ("tax");
3041 AddCodeLine ("lda #$00");
3043 } else if (val == 16) {
3044 AddCodeLine ("stx sreg+1");
3045 AddCodeLine ("sta sreg");
3046 AddCodeLine ("lda #$00");
3047 AddCodeLine ("tax");
3056 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3057 * into the normal, non-optimized stuff.
3059 g_push (flags & ~CF_CONST, 0);
3063 /* Use long way over the stack */
3064 oper (flags, val, ops);
3069 void g_neg (unsigned flags)
3070 /* Primary = -Primary */
3072 switch (flags & CF_TYPE) {
3076 AddCodeLine ("jsr negax");
3080 AddCodeLine ("jsr negeax");
3090 void g_bneg (unsigned flags)
3091 /* Primary = !Primary */
3093 switch (flags & CF_TYPE) {
3096 AddCodeLine ("jsr bnega");
3100 AddCodeLine ("jsr bnegax");
3104 AddCodeLine ("jsr bnegeax");
3114 void g_com (unsigned flags)
3115 /* Primary = ~Primary */
3117 switch (flags & CF_TYPE) {
3121 AddCodeLine ("jsr complax");
3125 AddCodeLine ("jsr compleax");
3135 void g_inc (unsigned flags, unsigned long val)
3136 /* Increment the primary register by a given number */
3138 /* Don't inc by zero */
3143 /* Generate code for the supported types */
3145 switch (flags & CF_TYPE) {
3148 if (flags & CF_FORCECHAR) {
3149 if (CPU == CPU_65C02 && val <= 2) {
3151 AddCodeLine ("ina");
3154 AddCodeLine ("clc");
3155 AddCodeLine ("adc #$%02X", (unsigned char)val);
3162 if (CPU == CPU_65C02 && val == 1) {
3163 unsigned L = GetLocalLabel();
3164 AddCodeLine ("ina");
3165 AddCodeLine ("bne %s", LocalLabelName (L));
3166 AddCodeLine ("inx");
3168 } else if (CodeSizeFactor < 200) {
3171 AddCodeLine ("jsr incax%lu", val);
3172 } else if (val <= 255) {
3174 AddCodeLine ("jsr incaxy");
3176 g_add (flags | CF_CONST, val);
3179 /* Inline the code */
3181 if ((val & 0xFF) != 0) {
3182 unsigned L = GetLocalLabel();
3183 AddCodeLine ("clc");
3184 AddCodeLine ("adc #$%02X", (unsigned char) val);
3185 AddCodeLine ("bcc %s", LocalLabelName (L));
3186 AddCodeLine ("inx");
3190 AddCodeLine ("inx");
3193 AddCodeLine ("inx");
3196 AddCodeLine ("clc");
3197 if ((val & 0xFF) != 0) {
3198 AddCodeLine ("adc #$%02X", (unsigned char) val);
3200 AddCodeLine ("pha");
3201 AddCodeLine ("txa");
3202 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3203 AddCodeLine ("tax");
3204 AddCodeLine ("pla");
3212 AddCodeLine ("jsr inceaxy");
3214 g_add (flags | CF_CONST, val);
3226 void g_dec (unsigned flags, unsigned long val)
3227 /* Decrement the primary register by a given number */
3229 /* Don't dec by zero */
3234 /* Generate code for the supported types */
3236 switch (flags & CF_TYPE) {
3239 if (flags & CF_FORCECHAR) {
3240 if (CPU == CPU_65C02 && val <= 2) {
3242 AddCodeLine ("dea");
3245 AddCodeLine ("sec");
3246 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3253 if (CodeSizeFactor < 200) {
3254 /* Use subroutines */
3256 AddCodeLine ("jsr decax%d", (int) val);
3257 } else if (val <= 255) {
3259 AddCodeLine ("jsr decaxy");
3261 g_sub (flags | CF_CONST, val);
3264 /* Inline the code */
3266 if ((val & 0xFF) != 0) {
3267 unsigned L = GetLocalLabel();
3268 AddCodeLine ("sec");
3269 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3270 AddCodeLine ("bcs %s", LocalLabelName (L));
3271 AddCodeLine ("dex");
3275 AddCodeLine ("dex");
3278 AddCodeLine ("dex");
3281 AddCodeLine ("sec");
3282 if ((val & 0xFF) != 0) {
3283 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3285 AddCodeLine ("pha");
3286 AddCodeLine ("txa");
3287 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3288 AddCodeLine ("tax");
3289 AddCodeLine ("pla");
3297 AddCodeLine ("jsr deceaxy");
3299 g_sub (flags | CF_CONST, val);
3312 * Following are the conditional operators. They compare the TOS against
3313 * the primary and put a literal 1 in the primary if the condition is
3314 * true, otherwise they clear the primary register
3319 void g_eq (unsigned flags, unsigned long val)
3320 /* Test for equal */
3322 static char* ops [12] = {
3323 "toseq00", "toseqa0", "toseqax",
3324 "toseq00", "toseqa0", "toseqax",
3331 /* If the right hand side is const, the lhs is not on stack but still
3332 * in the primary register.
3334 if (flags & CF_CONST) {
3336 switch (flags & CF_TYPE) {
3339 if (flags & CF_FORCECHAR) {
3340 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3341 AddCodeLine ("jsr booleq");
3347 L = GetLocalLabel();
3348 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3349 AddCodeLine ("bne %s", LocalLabelName (L));
3350 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3352 AddCodeLine ("jsr booleq");
3362 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3363 * into the normal, non-optimized stuff.
3365 g_push (flags & ~CF_CONST, 0);
3369 /* Use long way over the stack */
3370 oper (flags, val, ops);
3375 void g_ne (unsigned flags, unsigned long val)
3376 /* Test for not equal */
3378 static char* ops [12] = {
3379 "tosne00", "tosnea0", "tosneax",
3380 "tosne00", "tosnea0", "tosneax",
3387 /* If the right hand side is const, the lhs is not on stack but still
3388 * in the primary register.
3390 if (flags & CF_CONST) {
3392 switch (flags & CF_TYPE) {
3395 if (flags & CF_FORCECHAR) {
3396 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3397 AddCodeLine ("jsr boolne");
3403 L = GetLocalLabel();
3404 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3405 AddCodeLine ("bne %s", LocalLabelName (L));
3406 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3408 AddCodeLine ("jsr boolne");
3418 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3419 * into the normal, non-optimized stuff.
3421 g_push (flags & ~CF_CONST, 0);
3425 /* Use long way over the stack */
3426 oper (flags, val, ops);
3431 void g_lt (unsigned flags, unsigned long val)
3432 /* Test for less than */
3434 static char* ops [12] = {
3435 "toslt00", "toslta0", "tosltax",
3436 "tosult00", "tosulta0", "tosultax",
3441 /* If the right hand side is const, the lhs is not on stack but still
3442 * in the primary register.
3444 if (flags & CF_CONST) {
3446 /* Give a warning in some special cases */
3447 if ((flags & CF_UNSIGNED) && val == 0) {
3448 Warning ("Condition is never true");
3451 /* Look at the type */
3452 switch (flags & CF_TYPE) {
3455 if (flags & CF_FORCECHAR) {
3456 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3457 if (flags & CF_UNSIGNED) {
3458 AddCodeLine ("jsr boolult");
3460 AddCodeLine ("jsr boollt");
3467 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3468 /* If we have a signed compare against zero, we only need to
3469 * test the high byte.
3471 AddCodeLine ("txa");
3472 AddCodeLine ("jsr boollt");
3475 /* Direct code only for unsigned data types */
3476 if (flags & CF_UNSIGNED) {
3477 unsigned L = GetLocalLabel();
3478 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3479 AddCodeLine ("bne %s", LocalLabelName (L));
3480 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3482 AddCodeLine ("jsr boolult");
3488 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3489 /* If we have a signed compare against zero, we only need to
3490 * test the high byte.
3492 AddCodeLine ("lda sreg+1");
3493 AddCodeLine ("jsr boollt");
3502 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3503 * into the normal, non-optimized stuff.
3505 g_push (flags & ~CF_CONST, 0);
3509 /* Use long way over the stack */
3510 oper (flags, val, ops);
3515 void g_le (unsigned flags, unsigned long val)
3516 /* Test for less than or equal to */
3518 static char* ops [12] = {
3519 "tosle00", "toslea0", "tosleax",
3520 "tosule00", "tosulea0", "tosuleax",
3526 /* If the right hand side is const, the lhs is not on stack but still
3527 * in the primary register.
3529 if (flags & CF_CONST) {
3531 /* <= is not very effective on the 6502, so try to convert
3532 * it into < if the value is in a valid range.
3535 /* Look at the type */
3536 switch (flags & CF_TYPE) {
3539 if (flags & CF_FORCECHAR) {
3540 if (flags & CF_UNSIGNED) {
3542 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3543 AddCodeLine ("jsr boolult");
3545 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3546 AddCodeLine ("jsr boolule");
3550 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3551 AddCodeLine ("jsr boollt");
3553 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3554 AddCodeLine ("jsr boolle");
3562 if (flags & CF_UNSIGNED) {
3563 unsigned L = GetLocalLabel();
3564 const char* Name = "boolule";
3569 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3570 AddCodeLine ("bne %s", LocalLabelName (L));
3571 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3573 AddCodeLine ("jsr %s", Name);
3585 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3586 * into the normal, non-optimized stuff.
3588 g_push (flags & ~CF_CONST, 0);
3592 /* Use long way over the stack */
3593 oper (flags, val, ops);
3598 void g_gt (unsigned flags, unsigned long val)
3599 /* Test for greater than */
3601 static char* ops [12] = {
3602 "tosgt00", "tosgta0", "tosgtax",
3603 "tosugt00", "tosugta0", "tosugtax",
3609 /* If the right hand side is const, the lhs is not on stack but still
3610 * in the primary register.
3612 if (flags & CF_CONST) {
3614 /* > is not very effective on the 6502, so try to convert
3615 * it into >= if the value is in a valid range.
3618 /* Look at the type */
3619 switch (flags & CF_TYPE) {
3622 if (flags & CF_FORCECHAR) {
3623 if (flags & CF_UNSIGNED) {
3624 /* If we have a compare > 0, we will replace it by
3625 * != 0 here, since both are identical but the latter
3626 * is easier to optimize.
3629 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3630 AddCodeLine ("jsr boolne");
3631 } else if (val < 255) {
3632 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3633 AddCodeLine ("jsr booluge");
3635 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3636 AddCodeLine ("jsr boolugt");
3640 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3641 AddCodeLine ("jsr boolge");
3643 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3644 AddCodeLine ("jsr boolgt");
3652 if (flags & CF_UNSIGNED) {
3653 /* If we have a compare > 0, we will replace it by
3654 * != 0 here, since both are identical but the latter
3655 * is easier to optimize.
3658 AddCodeLine ("stx tmp1");
3659 AddCodeLine ("ora tmp1");
3660 AddCodeLine ("jsr boolne");
3662 unsigned L = GetLocalLabel();
3663 const char* Name = "boolugt";
3668 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3669 AddCodeLine ("bne %s", LocalLabelName (L));
3670 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3672 AddCodeLine ("jsr %s", Name);
3685 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3686 * into the normal, non-optimized stuff.
3688 g_push (flags & ~CF_CONST, 0);
3692 /* Use long way over the stack */
3693 oper (flags, val, ops);
3698 void g_ge (unsigned flags, unsigned long val)
3699 /* Test for greater than or equal to */
3701 static char* ops [12] = {
3702 "tosge00", "tosgea0", "tosgeax",
3703 "tosuge00", "tosugea0", "tosugeax",
3709 /* If the right hand side is const, the lhs is not on stack but still
3710 * in the primary register.
3712 if (flags & CF_CONST) {
3714 /* Give a warning in some special cases */
3715 if ((flags & CF_UNSIGNED) && val == 0) {
3716 Warning ("Condition is always true");
3719 /* Look at the type */
3720 switch (flags & CF_TYPE) {
3723 if (flags & CF_FORCECHAR) {
3724 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3725 if (flags & CF_UNSIGNED) {
3726 AddCodeLine ("jsr booluge");
3728 AddCodeLine ("jsr boolge");
3735 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3736 /* If we have a signed compare against zero, we only need to
3737 * test the high byte.
3739 AddCodeLine ("txa");
3740 AddCodeLine ("jsr boolge");
3743 /* Direct code only for unsigned data types */
3744 if (flags & CF_UNSIGNED) {
3745 unsigned L = GetLocalLabel();
3746 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3747 AddCodeLine ("bne %s", LocalLabelName (L));
3748 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3750 AddCodeLine ("jsr booluge");
3756 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3757 /* If we have a signed compare against zero, we only need to
3758 * test the high byte.
3760 AddCodeLine ("lda sreg+1");
3761 AddCodeLine ("jsr boolge");
3770 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3771 * into the normal, non-optimized stuff.
3773 g_push (flags & ~CF_CONST, 0);
3777 /* Use long way over the stack */
3778 oper (flags, val, ops);
3783 /*****************************************************************************/
3784 /* Allocating static storage */
3785 /*****************************************************************************/
3789 void g_res (unsigned n)
3790 /* Reserve static storage, n bytes */
3792 AddDataLine ("\t.res\t%u,$00", n);
3797 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3798 /* Define data with the size given in flags */
3800 if (flags & CF_CONST) {
3802 /* Numeric constant */
3803 switch (flags & CF_TYPE) {
3806 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
3810 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
3814 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3825 /* Create the correct label name */
3826 const char* Label = GetLabelName (flags, val, offs);
3828 /* Labels are always 16 bit */
3829 AddDataLine ("\t.word\t%s", Label);
3836 void g_defbytes (const void* Bytes, unsigned Count)
3837 /* Output a row of bytes as a constant */
3843 /* Cast the buffer pointer */
3844 const unsigned char* Data = (const unsigned char*) Bytes;
3846 /* Output the stuff */
3849 /* How many go into this line? */
3850 if ((Chunk = Count) > 16) {
3855 /* Output one line */
3856 strcpy (Buf, "\t.byte\t");
3859 B += sprintf (B, "$%02X", *Data++);
3865 /* Output the line */
3872 void g_zerobytes (unsigned n)
3873 /* Output n bytes of data initialized with zero */
3875 AddDataLine ("\t.res\t%u,$00", n);
3880 /*****************************************************************************/
3881 /* User supplied assembler code */
3882 /*****************************************************************************/
3886 void g_asmcode (const char* Line, int Len)
3887 /* Output one line of assembler code. If Len is greater than zero, it is used
3888 * as the maximum number of characters to use from Line.
3892 AddCodeLine ("%.*s", Len, Line);
3894 AddCodeLine ("%s", Line);
3900 /*****************************************************************************/
3901 /* Inlined known functions */
3902 /*****************************************************************************/
3906 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3907 /* Inline the strlen() function */
3909 /* We need a label in both cases */
3910 unsigned label = GetLocalLabel ();
3912 /* Two different encodings */
3913 if (flags & CF_CONST) {
3915 /* The address of the string is constant. Create the correct label name */
3916 char* lbuf = GetLabelName (flags, val, offs);
3918 /* Generate the strlen code */
3919 AddCodeLine ("ldy #$FF");
3920 g_defcodelabel (label);
3921 AddCodeLine ("iny");
3922 AddCodeLine ("lda %s,y", lbuf);
3923 AddCodeLine ("bne %s", LocalLabelName (label));
3924 AddCodeLine ("tax");
3925 AddCodeLine ("tya");
3929 /* Address not constant but in primary */
3930 if (CodeSizeFactor < 400) {
3931 /* This is too much code, so call strlen instead of inlining */
3932 AddCodeLine ("jsr _strlen");
3934 /* Inline the function */
3935 AddCodeLine ("sta ptr1");
3936 AddCodeLine ("stx ptr1+1");
3937 AddCodeLine ("ldy #$FF");
3938 g_defcodelabel (label);
3939 AddCodeLine ("iny");
3940 AddCodeLine ("lda (ptr1),y");
3941 AddCodeLine ("bne %s", LocalLabelName (label));
3942 AddCodeLine ("tax");
3943 AddCodeLine ("tya");