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 /*****************************************************************************/
62 /*****************************************************************************/
64 /*****************************************************************************/
68 /* Compiler relative stack pointer */
73 /*****************************************************************************/
75 /*****************************************************************************/
79 static void typeerror (unsigned type)
80 /* Print an error message about an invalid operand type */
82 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
87 static void CheckLocalOffs (unsigned Offs)
88 /* Check the offset into the stack for 8bit range */
91 /* Too many local vars */
92 Error ("Too many local variables");
98 static char* GetLabelName (unsigned flags, unsigned long label, unsigned offs)
100 static char lbuf [128]; /* Label name */
102 /* Create the correct label name */
103 switch (flags & CF_ADDRMASK) {
106 /* Static memory cell */
107 sprintf (lbuf, "%s+%u", LocalLabelName (label), offs);
112 sprintf (lbuf, "_%s+%u", (char*) label, offs);
116 /* Absolute address */
117 sprintf (lbuf, "$%04X", (unsigned)((label+offs) & 0xFFFF));
121 /* Variable in register bank */
122 sprintf (lbuf, "regbank+%u", (unsigned)((label+offs) & 0xFFFF));
126 Internal ("Invalid address flags");
129 /* Return a pointer to the static buffer */
135 /*****************************************************************************/
136 /* Pre- and postamble */
137 /*****************************************************************************/
141 void g_preamble (void)
142 /* Generate the assembler code preamble */
144 /* Create a new (global) segment list and remember it */
148 /* Identify the compiler version */
150 AddTextLine ("; File generated by cc65 v %u.%u.%u",
151 VER_MAJOR, VER_MINOR, VER_PATCH);
154 /* Insert some object file options */
155 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
156 VER_MAJOR, VER_MINOR, VER_PATCH);
158 /* If we're producing code for some other CPU, switch the command set */
159 if (CPU == CPU_65C02) {
160 AddTextLine ("\t.pc02");
163 /* Allow auto import for runtime library routines */
164 AddTextLine ("\t.autoimport\ton");
166 /* Switch the assembler into case sensitive mode */
167 AddTextLine ("\t.case\t\ton");
169 /* Tell the assembler if we want to generate debug info */
170 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
172 /* Import the stack pointer for direct auto variable access */
173 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
175 /* Define long branch macros */
176 AddTextLine ("\t.macpack\tlongbranch");
181 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
182 /* If debug info is enabled, place a file info into the source */
185 /* We have to place this into the global text segment, so it will
186 * appear before all .dbg line statements.
188 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
194 /*****************************************************************************/
195 /* Segment support */
196 /*****************************************************************************/
200 void g_userodata (void)
201 /* Switch to the read only data segment */
203 UseDataSeg (SEG_RODATA);
208 void g_usedata (void)
209 /* Switch to the data segment */
211 UseDataSeg (SEG_DATA);
217 /* Switch to the bss segment */
219 UseDataSeg (SEG_BSS);
224 void g_segname (segment_t Seg, const char* Name)
225 /* Set the name of a segment */
229 /* Remember the new name */
230 NewSegName (Seg, Name);
232 /* Emit a segment directive for the data style segments */
234 case SEG_RODATA: S = CS->ROData; break;
235 case SEG_DATA: S = CS->Data; break;
236 case SEG_BSS: S = CS->BSS; break;
237 default: S = 0; break;
240 DS_AddLine (S, ".segment\t\"%s\"", Name);
246 /*****************************************************************************/
248 /*****************************************************************************/
252 unsigned sizeofarg (unsigned flags)
253 /* Return the size of a function argument type that is encoded in flags */
255 switch (flags & CF_TYPE) {
258 return (flags & CF_FORCECHAR)? 1 : 2;
275 int pop (unsigned flags)
276 /* Pop an argument of the given size */
278 return oursp += sizeofarg (flags);
283 int push (unsigned flags)
284 /* Push an argument of the given size */
286 return oursp -= sizeofarg (flags);
291 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
292 /* The value in Offs is an offset to an address in a/x. Make sure, an object
293 * of the type given in Flags can be loaded or stored into this address by
294 * adding part of the offset to the address in ax, so that the remaining
295 * offset fits into an index register. Return the remaining offset.
298 /* If the offset is too large for a byte register, add the high byte
299 * of the offset to the primary. Beware: We need a special correction
300 * if the offset in the low byte will overflow in the operation.
302 unsigned O = Offs & ~0xFFU;
303 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
304 /* We need to add the low byte also */
308 /* Do the correction if we need one */
310 g_inc (CF_INT | CF_CONST, O);
314 /* Return the new offset */
320 /*****************************************************************************/
321 /* Functions handling local labels */
322 /*****************************************************************************/
326 void g_defcodelabel (unsigned label)
327 /* Define a local code label */
329 CS_AddLabel (CS->Code, LocalLabelName (label));
334 void g_defdatalabel (unsigned label)
335 /* Define a local data label */
337 AddDataLine ("%s:", LocalLabelName (label));
342 /*****************************************************************************/
343 /* Functions handling global labels */
344 /*****************************************************************************/
348 void g_defgloblabel (const char* Name)
349 /* Define a global label with the given name */
351 /* Global labels are always data labels */
352 AddDataLine ("_%s:", Name);
357 void g_defexport (const char* Name, int ZP)
358 /* Export the given label */
361 AddTextLine ("\t.exportzp\t_%s", Name);
363 AddTextLine ("\t.export\t\t_%s", Name);
369 void g_defimport (const char* Name, int ZP)
370 /* Import the given label */
373 AddTextLine ("\t.importzp\t_%s", Name);
375 AddTextLine ("\t.import\t\t_%s", Name);
381 /*****************************************************************************/
382 /* Load functions for various registers */
383 /*****************************************************************************/
387 static void ldaconst (unsigned val)
388 /* Load a with a constant */
390 AddCodeLine ("lda #$%02X", val & 0xFF);
395 static void ldxconst (unsigned val)
396 /* Load x with a constant */
398 AddCodeLine ("ldx #$%02X", val & 0xFF);
403 static void ldyconst (unsigned val)
404 /* Load y with a constant */
406 AddCodeLine ("ldy #$%02X", val & 0xFF);
411 /*****************************************************************************/
412 /* Function entry and exit */
413 /*****************************************************************************/
417 /* Remember the argument size of a function. The variable is set by g_enter
418 * and used by g_leave. If the functions gets its argument size by the caller
419 * (variable param list or function without prototype), g_enter will set the
425 void g_enter (unsigned flags, unsigned argsize)
426 /* Function prologue */
428 if ((flags & CF_FIXARGC) != 0) {
429 /* Just remember the argument size for the leave */
433 AddCodeLine ("jsr enter");
440 /* Function epilogue */
442 /* How many bytes of locals do we have to drop? */
445 /* If we didn't have a variable argument list, don't call leave */
448 /* Drop stackframe if needed */
452 AddCodeLine ("jsr incsp%d", k);
456 AddCodeLine ("jsr addysp");
463 /* Nothing to drop */
464 AddCodeLine ("jsr leave");
466 /* We've a stack frame to drop */
468 AddCodeLine ("jsr leavey");
472 /* Add the final rts */
478 /*****************************************************************************/
479 /* Register variables */
480 /*****************************************************************************/
484 void g_save_regvars (int RegOffs, unsigned Bytes)
485 /* Save register variables */
487 /* Don't loop for up to two bytes */
490 AddCodeLine ("lda regbank%+d", RegOffs);
491 AddCodeLine ("jsr pusha");
493 } else if (Bytes == 2) {
495 AddCodeLine ("lda regbank%+d", RegOffs);
496 AddCodeLine ("ldx regbank%+d", RegOffs+1);
497 AddCodeLine ("jsr pushax");
501 /* More than two bytes - loop */
502 unsigned Label = GetLocalLabel ();
504 ldyconst (Bytes - 1);
506 g_defcodelabel (Label);
507 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
508 AddCodeLine ("sta (sp),y");
511 AddCodeLine ("bne %s", LocalLabelName (Label));
515 /* We pushed stuff, correct the stack pointer */
521 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
522 /* Restore register variables */
524 /* Calculate the actual stack offset and check it */
526 CheckLocalOffs (StackOffs);
528 /* Don't loop for up to two bytes */
531 ldyconst (StackOffs);
532 AddCodeLine ("lda (sp),y");
533 AddCodeLine ("sta regbank%+d", RegOffs);
535 } else if (Bytes == 2) {
537 ldyconst (StackOffs);
538 AddCodeLine ("lda (sp),y");
539 AddCodeLine ("sta regbank%+d", RegOffs);
541 AddCodeLine ("lda (sp),y");
542 AddCodeLine ("sta regbank%+d", RegOffs+1);
546 /* More than two bytes - loop */
547 unsigned Label = GetLocalLabel ();
548 ldyconst (StackOffs+Bytes-1);
550 g_defcodelabel (Label);
551 AddCodeLine ("lda (sp),y");
552 AddCodeLine ("sta regbank%+d,x", RegOffs-1);
555 AddCodeLine ("bne %s", LocalLabelName (Label));
562 /*****************************************************************************/
563 /* Fetching memory cells */
564 /*****************************************************************************/
568 void g_getimmed (unsigned Flags, unsigned long Val, unsigned Offs)
569 /* Load a constant into the primary register */
571 unsigned char B1, B2, B3, B4;
575 if ((Flags & CF_CONST) != 0) {
577 /* Numeric constant */
578 switch (Flags & CF_TYPE) {
581 if ((Flags & CF_FORCECHAR) != 0) {
587 ldxconst ((Val >> 8) & 0xFF);
588 ldaconst (Val & 0xFF);
592 /* Split the value into 4 bytes */
593 B1 = (unsigned char) (Val >> 0);
594 B2 = (unsigned char) (Val >> 8);
595 B3 = (unsigned char) (Val >> 16);
596 B4 = (unsigned char) (Val >> 24);
598 /* Remember which bytes are done */
602 AddCodeLine ("ldx #$%02X", B2);
605 AddCodeLine ("stx sreg");
609 AddCodeLine ("stx sreg+1");
612 if ((Done & 0x04) == 0 && B1 != B3) {
613 AddCodeLine ("lda #$%02X", B3);
614 AddCodeLine ("sta sreg");
617 if ((Done & 0x08) == 0 && B1 != B4) {
618 AddCodeLine ("lda #$%02X", B4);
619 AddCodeLine ("sta sreg+1");
622 AddCodeLine ("lda #$%02X", B1);
624 if ((Done & 0x04) == 0) {
626 AddCodeLine ("sta sreg");
628 if ((Done & 0x08) == 0) {
630 AddCodeLine ("sta sreg+1");
642 /* Some sort of label */
643 const char* Label = GetLabelName (Flags, Val, Offs);
645 /* Load the address into the primary */
646 AddCodeLine ("lda #<(%s)", Label);
647 AddCodeLine ("ldx #>(%s)", Label);
654 void g_getstatic (unsigned flags, unsigned long label, unsigned offs)
655 /* Fetch an static memory cell into the primary register */
657 /* Create the correct label name */
658 char* lbuf = GetLabelName (flags, label, offs);
660 /* Check the size and generate the correct load operation */
661 switch (flags & CF_TYPE) {
664 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
665 AddCodeLine ("lda %s", lbuf); /* load A from the label */
668 AddCodeLine ("lda %s", lbuf); /* load A from the label */
669 if (!(flags & CF_UNSIGNED)) {
670 /* Must sign extend */
671 unsigned L = GetLocalLabel ();
672 AddCodeLine ("bpl %s", LocalLabelName (L));
680 AddCodeLine ("lda %s", lbuf);
681 if (flags & CF_TEST) {
682 AddCodeLine ("ora %s+1", lbuf);
684 AddCodeLine ("ldx %s+1", lbuf);
689 if (flags & CF_TEST) {
690 AddCodeLine ("lda %s+3", lbuf);
691 AddCodeLine ("ora %s+2", lbuf);
692 AddCodeLine ("ora %s+1", lbuf);
693 AddCodeLine ("ora %s+0", lbuf);
695 AddCodeLine ("lda %s+3", lbuf);
696 AddCodeLine ("sta sreg+1");
697 AddCodeLine ("lda %s+2", lbuf);
698 AddCodeLine ("sta sreg");
699 AddCodeLine ("ldx %s+1", lbuf);
700 AddCodeLine ("lda %s", lbuf);
712 void g_getlocal (unsigned flags, int offs)
713 /* Fetch specified local object (local var). */
716 CheckLocalOffs (offs);
717 switch (flags & CF_TYPE) {
720 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
721 if (CPU == CPU_65C02 && offs == 0) {
722 AddCodeLine ("lda (sp)");
725 AddCodeLine ("lda (sp),y");
729 AddCodeLine ("ldx #$00");
730 AddCodeLine ("lda (sp),y");
731 if ((flags & CF_UNSIGNED) == 0) {
732 unsigned L = GetLocalLabel();
733 AddCodeLine ("bpl %s", LocalLabelName (L));
741 CheckLocalOffs (offs + 1);
742 if (flags & CF_TEST) {
744 AddCodeLine ("lda (sp),y");
746 AddCodeLine ("ora (sp),y");
748 if (CodeSizeFactor > 180) {
750 AddCodeLine ("lda (sp),y");
753 AddCodeLine ("lda (sp),y");
757 AddCodeLine ("jsr ldaxysp");
759 AddCodeLine ("jsr ldax0sp");
768 AddCodeLine ("jsr ldeaxysp");
770 AddCodeLine ("jsr ldeax0sp");
781 void g_getind (unsigned flags, unsigned offs)
782 /* Fetch the specified object type indirect through the primary register
783 * into the primary register
786 /* If the offset is greater than 255, add the part that is > 255 to
787 * the primary. This way we get an easy addition and use the low byte
790 offs = MakeByteOffs (flags, offs);
792 /* Handle the indirect fetch */
793 switch (flags & CF_TYPE) {
796 /* Character sized */
797 if (flags & CF_UNSIGNED) {
799 AddCodeLine ("jsr ldauidx");
802 AddCodeLine ("jsr ldaidx");
807 if (flags & CF_TEST) {
809 AddCodeLine ("sta ptr1");
810 AddCodeLine ("stx ptr1+1");
811 AddCodeLine ("lda (ptr1),y");
813 AddCodeLine ("ora (ptr1),y");
816 AddCodeLine ("jsr ldaxi");
819 AddCodeLine ("jsr ldaxidx");
826 AddCodeLine ("jsr ldeaxi");
829 AddCodeLine ("jsr ldeaxidx");
831 if (flags & CF_TEST) {
832 AddCodeLine ("jsr tsteax");
844 void g_leasp (int offs)
845 /* Fetch the address of the specified symbol into the primary register */
847 /* Calculate the offset relative to sp */
850 /* For value 0 we do direct code */
852 AddCodeLine ("lda sp");
853 AddCodeLine ("ldx sp+1");
855 if (CodeSizeFactor < 300) {
856 ldaconst (offs); /* Load A with offset value */
857 AddCodeLine ("jsr leaasp"); /* Load effective address */
859 unsigned L = GetLocalLabel ();
860 if (CPU == CPU_65C02 && offs == 1) {
861 AddCodeLine ("lda sp");
862 AddCodeLine ("ldx sp+1");
864 AddCodeLine ("bne %s", LocalLabelName (L));
869 AddCodeLine ("ldx sp+1");
870 AddCodeLine ("adc sp");
871 AddCodeLine ("bcc %s", LocalLabelName (L));
881 void g_leavariadic (int Offs)
882 /* Fetch the address of a parameter in a variadic function into the primary
886 unsigned ArgSizeOffs;
888 /* Calculate the offset relative to sp */
891 /* Get the offset of the parameter which is stored at sp+0 on function
892 * entry and check if this offset is reachable with a byte offset.
895 ArgSizeOffs = -oursp;
896 CheckLocalOffs (ArgSizeOffs);
898 /* Get the size of all parameters. */
899 if (ArgSizeOffs == 0 && CPU == CPU_65C02) {
900 AddCodeLine ("lda (sp)");
902 ldyconst (ArgSizeOffs);
903 AddCodeLine ("lda (sp),y");
906 /* Add the value of the stackpointer */
907 if (CodeSizeFactor > 250) {
908 unsigned L = GetLocalLabel();
909 AddCodeLine ("ldx sp+1");
911 AddCodeLine ("adc sp");
912 AddCodeLine ("bcc %s", LocalLabelName (L));
916 AddCodeLine ("jsr leaasp");
919 /* Add the offset to the primary */
921 g_inc (CF_INT | CF_CONST, Offs);
922 } else if (Offs < 0) {
923 g_dec (CF_INT | CF_CONST, -Offs);
929 /*****************************************************************************/
930 /* Store into memory */
931 /*****************************************************************************/
935 void g_putstatic (unsigned flags, unsigned long label, unsigned offs)
936 /* Store the primary register into the specified static memory cell */
938 /* Create the correct label name */
939 char* lbuf = GetLabelName (flags, label, offs);
941 /* Check the size and generate the correct store operation */
942 switch (flags & CF_TYPE) {
945 AddCodeLine ("sta %s", lbuf);
949 AddCodeLine ("sta %s", lbuf);
950 AddCodeLine ("stx %s+1", lbuf);
954 AddCodeLine ("sta %s", lbuf);
955 AddCodeLine ("stx %s+1", lbuf);
956 AddCodeLine ("ldy sreg");
957 AddCodeLine ("sty %s+2", lbuf);
958 AddCodeLine ("ldy sreg+1");
959 AddCodeLine ("sty %s+3", lbuf);
970 void g_putlocal (unsigned Flags, int Offs, long Val)
971 /* Put data into local object. */
974 CheckLocalOffs (Offs);
975 switch (Flags & CF_TYPE) {
978 if (Flags & CF_CONST) {
979 AddCodeLine ("lda #$%02X", (unsigned char) Val);
981 if (CPU == CPU_65C02 && Offs == 0) {
982 AddCodeLine ("sta (sp)");
985 AddCodeLine ("sta (sp),y");
990 if (Flags & CF_CONST) {
992 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
993 AddCodeLine ("sta (sp),y");
994 if ((Flags & CF_NOKEEP) == 0) {
995 /* Place high byte into X */
998 if (CPU == CPU_65C02 && Offs == 0) {
999 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1000 AddCodeLine ("sta (sp)");
1002 if ((Val & 0xFF) == Offs+1) {
1003 /* The value we need is already in Y */
1004 AddCodeLine ("tya");
1005 AddCodeLine ("dey");
1007 AddCodeLine ("dey");
1008 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1010 AddCodeLine ("sta (sp),y");
1013 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
1015 AddCodeLine ("jsr staxysp");
1017 if (CPU == CPU_65C02 && Offs == 0) {
1018 AddCodeLine ("sta (sp)");
1020 AddCodeLine ("txa");
1021 AddCodeLine ("sta (sp),y");
1024 AddCodeLine ("sta (sp),y");
1025 AddCodeLine ("iny");
1026 AddCodeLine ("txa");
1027 AddCodeLine ("sta (sp),y");
1034 if (Flags & CF_CONST) {
1035 g_getimmed (Flags, Val, 0);
1039 AddCodeLine ("jsr steaxysp");
1041 AddCodeLine ("jsr steax0sp");
1053 void g_putind (unsigned Flags, unsigned Offs)
1054 /* Store the specified object type in the primary register at the address
1055 * on the top of the stack
1058 /* We can handle offsets below $100 directly, larger offsets must be added
1059 * to the address. Since a/x is in use, best code is achieved by adding
1060 * just the high byte. Be sure to check if the low byte will overflow while
1063 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1065 /* Overflow - we need to add the low byte also */
1066 AddCodeLine ("ldy #$00");
1067 AddCodeLine ("clc");
1068 AddCodeLine ("pha");
1069 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1070 AddCodeLine ("adc (sp),y");
1071 AddCodeLine ("sta (sp),y");
1072 AddCodeLine ("iny");
1073 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1074 AddCodeLine ("adc (sp),y");
1075 AddCodeLine ("sta (sp),y");
1076 AddCodeLine ("pla");
1078 /* Complete address is on stack, new offset is zero */
1081 } else if ((Offs & 0xFF00) != 0) {
1083 /* We can just add the high byte */
1084 AddCodeLine ("ldy #$01");
1085 AddCodeLine ("clc");
1086 AddCodeLine ("pha");
1087 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1088 AddCodeLine ("adc (sp),y");
1089 AddCodeLine ("sta (sp),y");
1090 AddCodeLine ("pla");
1092 /* Offset is now just the low byte */
1096 /* Check the size and determine operation */
1097 switch (Flags & CF_TYPE) {
1101 AddCodeLine ("jsr staspidx");
1106 AddCodeLine ("jsr staxspidx");
1111 AddCodeLine ("jsr steaxspidx");
1119 /* Pop the argument which is always a pointer */
1125 /*****************************************************************************/
1126 /* type conversion and similiar stuff */
1127 /*****************************************************************************/
1131 void g_toslong (unsigned flags)
1132 /* Make sure, the value on TOS is a long. Convert if necessary */
1134 switch (flags & CF_TYPE) {
1138 if (flags & CF_UNSIGNED) {
1139 AddCodeLine ("jsr tosulong");
1141 AddCodeLine ("jsr toslong");
1156 void g_tosint (unsigned flags)
1157 /* Make sure, the value on TOS is an int. Convert if necessary */
1159 switch (flags & CF_TYPE) {
1166 AddCodeLine ("jsr tosint");
1177 void g_reglong (unsigned flags)
1178 /* Make sure, the value in the primary register a long. Convert if necessary */
1180 switch (flags & CF_TYPE) {
1184 if (flags & CF_UNSIGNED) {
1185 if (CodeSizeFactor >= 200) {
1187 AddCodeLine ("sty sreg");
1188 AddCodeLine ("sty sreg+1");
1190 AddCodeLine ("jsr axulong");
1193 AddCodeLine ("jsr axlong");
1207 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1208 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1209 * value, that corresponds to the value on TOS, rhs corresponds to the value
1210 * in (e)ax. The return value is the the flags value for the resulting type.
1213 unsigned ltype, rtype;
1216 /* Get the type spec from the flags */
1217 ltype = lhs & CF_TYPE;
1218 rtype = rhs & CF_TYPE;
1220 /* Check if a conversion is needed */
1221 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1222 /* We must promote the primary register to long */
1224 /* Get the new rhs type */
1225 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1227 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1228 /* We must promote the lhs to long */
1234 /* Get the new rhs type */
1235 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1239 /* Determine the result type for the operation:
1240 * - The result is const if both operands are const.
1241 * - The result is unsigned if one of the operands is unsigned.
1242 * - The result is long if one of the operands is long.
1243 * - Otherwise the result is int sized.
1245 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1246 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1247 if (rtype == CF_LONG || ltype == CF_LONG) {
1257 unsigned g_typecast (unsigned lhs, unsigned rhs)
1258 /* Cast the value in the primary register to the operand size that is flagged
1259 * by the lhs value. Return the result value.
1262 unsigned ltype, rtype;
1264 /* Get the type spec from the flags */
1265 ltype = lhs & CF_TYPE;
1266 rtype = rhs & CF_TYPE;
1268 /* Check if a conversion is needed */
1269 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1270 /* We must promote the primary register to long */
1274 /* Do not need any other action. If the left type is int, and the primary
1275 * register is long, it will be automagically truncated. If the right hand
1276 * side is const, it is not located in the primary register and handled by
1277 * the expression parser code.
1280 /* Result is const if the right hand side was const */
1281 lhs |= (rhs & CF_CONST);
1283 /* The resulting type is that of the left hand side (that's why you called
1291 void g_scale (unsigned flags, long val)
1292 /* Scale the value in the primary register by the given value. If val is positive,
1293 * scale up, is val is negative, scale down. This function is used to scale
1294 * the operands or results of pointer arithmetic by the size of the type, the
1295 * pointer points to.
1300 /* Value may not be zero */
1302 Internal ("Data type has no size");
1303 } else if (val > 0) {
1306 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1308 /* Factor is 2, 4, 8 and 16, use special function */
1309 switch (flags & CF_TYPE) {
1312 if (flags & CF_FORCECHAR) {
1314 AddCodeLine ("asl a");
1321 if (CodeSizeFactor >= (p2+1)*130U) {
1322 AddCodeLine ("stx tmp1");
1324 AddCodeLine ("asl a");
1325 AddCodeLine ("rol tmp1");
1327 AddCodeLine ("ldx tmp1");
1329 if (flags & CF_UNSIGNED) {
1330 AddCodeLine ("jsr shlax%d", p2);
1332 AddCodeLine ("jsr aslax%d", p2);
1338 if (flags & CF_UNSIGNED) {
1339 AddCodeLine ("jsr shleax%d", p2);
1341 AddCodeLine ("jsr asleax%d", p2);
1350 } else if (val != 1) {
1352 /* Use a multiplication instead */
1353 g_mul (flags | CF_CONST, val);
1361 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1363 /* Factor is 2, 4, 8 and 16 use special function */
1364 switch (flags & CF_TYPE) {
1367 if (flags & CF_FORCECHAR) {
1368 if (flags & CF_UNSIGNED) {
1370 AddCodeLine ("lsr a");
1373 } else if (p2 <= 2) {
1374 AddCodeLine ("cmp #$80");
1375 AddCodeLine ("ror a");
1382 if (flags & CF_UNSIGNED) {
1383 if (CodeSizeFactor >= (p2+1)*130U) {
1384 AddCodeLine ("stx tmp1");
1386 AddCodeLine ("lsr tmp1");
1387 AddCodeLine ("ror a");
1389 AddCodeLine ("ldx tmp1");
1391 AddCodeLine ("jsr lsrax%d", p2);
1394 if (CodeSizeFactor >= (p2+1)*150U) {
1395 AddCodeLine ("stx tmp1");
1397 AddCodeLine ("cpx #$80");
1398 AddCodeLine ("ror tmp1");
1399 AddCodeLine ("ror a");
1401 AddCodeLine ("ldx tmp1");
1403 AddCodeLine ("jsr asrax%d", p2);
1409 if (flags & CF_UNSIGNED) {
1410 AddCodeLine ("jsr lsreax%d", p2);
1412 AddCodeLine ("jsr asreax%d", p2);
1421 } else if (val != 1) {
1423 /* Use a division instead */
1424 g_div (flags | CF_CONST, val);
1432 /*****************************************************************************/
1433 /* Adds and subs of variables fix a fixed address */
1434 /*****************************************************************************/
1438 void g_addlocal (unsigned flags, int offs)
1439 /* Add a local variable to ax */
1443 /* Correct the offset and check it */
1445 CheckLocalOffs (offs);
1447 switch (flags & CF_TYPE) {
1450 L = GetLocalLabel();
1451 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1452 AddCodeLine ("clc");
1453 AddCodeLine ("adc (sp),y");
1454 AddCodeLine ("bcc %s", LocalLabelName (L));
1455 AddCodeLine ("inx");
1460 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1461 AddCodeLine ("clc");
1462 AddCodeLine ("adc (sp),y");
1463 AddCodeLine ("pha");
1464 AddCodeLine ("txa");
1465 AddCodeLine ("iny");
1466 AddCodeLine ("adc (sp),y");
1467 AddCodeLine ("tax");
1468 AddCodeLine ("pla");
1472 /* Do it the old way */
1474 g_getlocal (flags, offs);
1486 void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
1487 /* Add a static variable to ax */
1491 /* Create the correct label name */
1492 char* lbuf = GetLabelName (flags, label, offs);
1494 switch (flags & CF_TYPE) {
1497 L = GetLocalLabel();
1498 AddCodeLine ("clc");
1499 AddCodeLine ("adc %s", lbuf);
1500 AddCodeLine ("bcc %s", LocalLabelName (L));
1501 AddCodeLine ("inx");
1506 AddCodeLine ("clc");
1507 AddCodeLine ("adc %s", lbuf);
1508 AddCodeLine ("tay");
1509 AddCodeLine ("txa");
1510 AddCodeLine ("adc %s+1", lbuf);
1511 AddCodeLine ("tax");
1512 AddCodeLine ("tya");
1516 /* Do it the old way */
1518 g_getstatic (flags, label, offs);
1530 /*****************************************************************************/
1531 /* Compares of ax with a variable with fixed address */
1532 /*****************************************************************************/
1536 void g_cmplocal (unsigned flags, int offs)
1537 /* Compare a local variable to ax */
1539 Internal ("g_cmplocal not implemented");
1544 void g_cmpstatic (unsigned flags, unsigned label, unsigned offs)
1545 /* Compare a static variable to ax */
1547 Internal ("g_cmpstatic not implemented");
1552 /*****************************************************************************/
1553 /* Special op= functions */
1554 /*****************************************************************************/
1558 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
1560 /* Emit += for a static variable */
1562 /* Create the correct label name */
1563 char* lbuf = GetLabelName (flags, label, offs);
1565 /* Check the size and determine operation */
1566 switch (flags & CF_TYPE) {
1569 if (flags & CF_FORCECHAR) {
1570 AddCodeLine ("ldx #$00");
1571 if (flags & CF_CONST) {
1573 AddCodeLine ("inc %s", lbuf);
1574 AddCodeLine ("lda %s", lbuf);
1576 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1577 AddCodeLine ("clc");
1578 AddCodeLine ("adc %s", lbuf);
1579 AddCodeLine ("sta %s", lbuf);
1582 AddCodeLine ("clc");
1583 AddCodeLine ("adc %s", lbuf);
1584 AddCodeLine ("sta %s", lbuf);
1586 if ((flags & CF_UNSIGNED) == 0) {
1587 unsigned L = GetLocalLabel();
1588 AddCodeLine ("bpl %s", LocalLabelName (L));
1589 AddCodeLine ("dex");
1597 if (flags & CF_CONST) {
1599 unsigned L = GetLocalLabel ();
1600 AddCodeLine ("inc %s", lbuf);
1601 AddCodeLine ("bne %s", LocalLabelName (L));
1602 AddCodeLine ("inc %s+1", lbuf);
1604 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1605 AddCodeLine ("ldx %s+1", lbuf);
1607 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1608 AddCodeLine ("clc");
1609 AddCodeLine ("adc %s", lbuf);
1610 AddCodeLine ("sta %s", lbuf);
1612 unsigned L = GetLocalLabel ();
1613 AddCodeLine ("bcc %s", LocalLabelName (L));
1614 AddCodeLine ("inc %s+1", lbuf);
1616 AddCodeLine ("ldx %s+1", lbuf);
1618 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1619 AddCodeLine ("adc %s+1", lbuf);
1620 AddCodeLine ("sta %s+1", lbuf);
1621 AddCodeLine ("tax");
1622 AddCodeLine ("lda %s", lbuf);
1626 AddCodeLine ("clc");
1627 AddCodeLine ("adc %s", lbuf);
1628 AddCodeLine ("sta %s", lbuf);
1629 AddCodeLine ("txa");
1630 AddCodeLine ("adc %s+1", lbuf);
1631 AddCodeLine ("sta %s+1", lbuf);
1632 AddCodeLine ("tax");
1633 AddCodeLine ("lda %s", lbuf);
1638 if (flags & CF_CONST) {
1640 AddCodeLine ("ldy #<(%s)", lbuf);
1641 AddCodeLine ("sty ptr1");
1642 AddCodeLine ("ldy #>(%s+1)", lbuf);
1644 AddCodeLine ("jsr laddeq1");
1646 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1647 AddCodeLine ("jsr laddeqa");
1650 g_getstatic (flags, label, offs);
1652 g_putstatic (flags, label, offs);
1655 AddCodeLine ("ldy #<(%s)", lbuf);
1656 AddCodeLine ("sty ptr1");
1657 AddCodeLine ("ldy #>(%s+1)", lbuf);
1658 AddCodeLine ("jsr laddeq");
1669 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1670 /* Emit += for a local variable */
1672 /* Calculate the true offset, check it, load it into Y */
1674 CheckLocalOffs (offs);
1676 /* Check the size and determine operation */
1677 switch (flags & CF_TYPE) {
1680 if (flags & CF_FORCECHAR) {
1682 AddCodeLine ("ldx #$00");
1683 if (flags & CF_CONST) {
1684 AddCodeLine ("clc");
1685 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1686 AddCodeLine ("adc (sp),y");
1687 AddCodeLine ("sta (sp),y");
1689 AddCodeLine ("clc");
1690 AddCodeLine ("adc (sp),y");
1691 AddCodeLine ("sta (sp),y");
1693 if ((flags & CF_UNSIGNED) == 0) {
1694 unsigned L = GetLocalLabel();
1695 AddCodeLine ("bpl %s", LocalLabelName (L));
1696 AddCodeLine ("dex");
1705 if (flags & CF_CONST) {
1706 if (CodeSizeFactor >= 400) {
1707 AddCodeLine ("clc");
1708 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1709 AddCodeLine ("adc (sp),y");
1710 AddCodeLine ("sta (sp),y");
1711 AddCodeLine ("iny");
1712 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1713 AddCodeLine ("adc (sp),y");
1714 AddCodeLine ("sta (sp),y");
1715 AddCodeLine ("tax");
1716 AddCodeLine ("dey");
1717 AddCodeLine ("lda (sp),y");
1719 g_getimmed (flags, val, 0);
1720 AddCodeLine ("jsr addeqysp");
1723 AddCodeLine ("jsr addeqysp");
1728 if (flags & CF_CONST) {
1729 g_getimmed (flags, val, 0);
1732 AddCodeLine ("jsr laddeq0sp");
1735 AddCodeLine ("jsr laddeqysp");
1746 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1747 /* Emit += for the location with address in ax */
1749 /* If the offset is too large for a byte register, add the high byte
1750 * of the offset to the primary. Beware: We need a special correction
1751 * if the offset in the low byte will overflow in the operation.
1753 offs = MakeByteOffs (flags, offs);
1755 /* Check the size and determine operation */
1756 switch (flags & CF_TYPE) {
1759 AddCodeLine ("sta ptr1");
1760 AddCodeLine ("stx ptr1+1");
1761 AddCodeLine ("ldy #$%02X", offs);
1762 AddCodeLine ("ldx #$00");
1763 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1764 AddCodeLine ("clc");
1765 AddCodeLine ("adc (ptr1),y");
1766 AddCodeLine ("sta (ptr1),y");
1770 if (CodeSizeFactor >= 200) {
1771 /* Lots of code, use only if size is not important */
1772 AddCodeLine ("sta ptr1");
1773 AddCodeLine ("stx ptr1+1");
1774 AddCodeLine ("ldy #$%02X", offs);
1775 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1776 AddCodeLine ("clc");
1777 AddCodeLine ("adc (ptr1),y");
1778 AddCodeLine ("sta (ptr1),y");
1779 AddCodeLine ("pha");
1780 AddCodeLine ("iny");
1781 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1782 AddCodeLine ("adc (ptr1),y");
1783 AddCodeLine ("sta (ptr1),y");
1784 AddCodeLine ("tax");
1785 AddCodeLine ("pla");
1791 AddCodeLine ("jsr pushax"); /* Push the address */
1792 push (CF_PTR); /* Correct the internal sp */
1793 g_getind (flags, offs); /* Fetch the value */
1794 g_inc (flags, val); /* Increment value in primary */
1795 g_putind (flags, offs); /* Store the value back */
1805 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1807 /* Emit -= for a static variable */
1809 /* Create the correct label name */
1810 char* lbuf = GetLabelName (flags, label, offs);
1812 /* Check the size and determine operation */
1813 switch (flags & CF_TYPE) {
1816 if (flags & CF_FORCECHAR) {
1817 AddCodeLine ("ldx #$00");
1818 if (flags & CF_CONST) {
1820 AddCodeLine ("dec %s", lbuf);
1821 AddCodeLine ("lda %s", lbuf);
1823 AddCodeLine ("sec");
1824 AddCodeLine ("lda %s", lbuf);
1825 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1826 AddCodeLine ("sta %s", lbuf);
1829 AddCodeLine ("sec");
1830 AddCodeLine ("sta tmp1");
1831 AddCodeLine ("lda %s", lbuf);
1832 AddCodeLine ("sbc tmp1");
1833 AddCodeLine ("sta %s", lbuf);
1835 if ((flags & CF_UNSIGNED) == 0) {
1836 unsigned L = GetLocalLabel();
1837 AddCodeLine ("bpl %s", LocalLabelName (L));
1838 AddCodeLine ("dex");
1846 AddCodeLine ("sec");
1847 if (flags & CF_CONST) {
1848 AddCodeLine ("lda %s", lbuf);
1849 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1850 AddCodeLine ("sta %s", lbuf);
1852 unsigned L = GetLocalLabel ();
1853 AddCodeLine ("bcs %s", LocalLabelName (L));
1854 AddCodeLine ("dec %s+1", lbuf);
1856 AddCodeLine ("ldx %s+1", lbuf);
1858 AddCodeLine ("lda %s+1", lbuf);
1859 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1860 AddCodeLine ("sta %s+1", lbuf);
1861 AddCodeLine ("tax");
1862 AddCodeLine ("lda %s", lbuf);
1865 AddCodeLine ("sta tmp1");
1866 AddCodeLine ("lda %s", lbuf);
1867 AddCodeLine ("sbc tmp1");
1868 AddCodeLine ("sta %s", lbuf);
1869 AddCodeLine ("stx tmp1");
1870 AddCodeLine ("lda %s+1", lbuf);
1871 AddCodeLine ("sbc tmp1");
1872 AddCodeLine ("sta %s+1", lbuf);
1873 AddCodeLine ("tax");
1874 AddCodeLine ("lda %s", lbuf);
1879 if (flags & CF_CONST) {
1881 AddCodeLine ("ldy #<(%s)", lbuf);
1882 AddCodeLine ("sty ptr1");
1883 AddCodeLine ("ldy #>(%s+1)", lbuf);
1885 AddCodeLine ("jsr lsubeq1");
1887 AddCodeLine ("lda #$%02X", (unsigned char)val);
1888 AddCodeLine ("jsr lsubeqa");
1891 g_getstatic (flags, label, offs);
1893 g_putstatic (flags, label, offs);
1896 AddCodeLine ("ldy #<(%s)", lbuf);
1897 AddCodeLine ("sty ptr1");
1898 AddCodeLine ("ldy #>(%s+1)", lbuf);
1899 AddCodeLine ("jsr lsubeq");
1910 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1911 /* Emit -= for a local variable */
1913 /* Calculate the true offset, check it, load it into Y */
1915 CheckLocalOffs (offs);
1917 /* Check the size and determine operation */
1918 switch (flags & CF_TYPE) {
1921 if (flags & CF_FORCECHAR) {
1923 AddCodeLine ("ldx #$00");
1924 AddCodeLine ("sec");
1925 if (flags & CF_CONST) {
1926 AddCodeLine ("lda (sp),y");
1927 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1929 AddCodeLine ("sta tmp1");
1930 AddCodeLine ("lda (sp),y");
1931 AddCodeLine ("sbc tmp1");
1933 AddCodeLine ("sta (sp),y");
1934 if ((flags & CF_UNSIGNED) == 0) {
1935 unsigned L = GetLocalLabel();
1936 AddCodeLine ("bpl %s", LocalLabelName (L));
1937 AddCodeLine ("dex");
1945 if (flags & CF_CONST) {
1946 g_getimmed (flags, val, 0);
1949 AddCodeLine ("jsr subeq0sp");
1952 AddCodeLine ("jsr subeqysp");
1957 if (flags & CF_CONST) {
1958 g_getimmed (flags, val, 0);
1961 AddCodeLine ("jsr lsubeq0sp");
1964 AddCodeLine ("jsr lsubeqysp");
1975 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
1976 /* Emit -= for the location with address in ax */
1978 /* If the offset is too large for a byte register, add the high byte
1979 * of the offset to the primary. Beware: We need a special correction
1980 * if the offset in the low byte will overflow in the operation.
1982 offs = MakeByteOffs (flags, offs);
1984 /* Check the size and determine operation */
1985 switch (flags & CF_TYPE) {
1988 AddCodeLine ("sta ptr1");
1989 AddCodeLine ("stx ptr1+1");
1990 AddCodeLine ("ldy #$%02X", offs);
1991 AddCodeLine ("ldx #$00");
1992 AddCodeLine ("lda (ptr1),y");
1993 AddCodeLine ("sec");
1994 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1995 AddCodeLine ("sta (ptr1),y");
1999 if (CodeSizeFactor >= 200) {
2000 /* Lots of code, use only if size is not important */
2001 AddCodeLine ("sta ptr1");
2002 AddCodeLine ("stx ptr1+1");
2003 AddCodeLine ("ldy #$%02X", offs);
2004 AddCodeLine ("lda (ptr1),y");
2005 AddCodeLine ("sec");
2006 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2007 AddCodeLine ("sta (ptr1),y");
2008 AddCodeLine ("pha");
2009 AddCodeLine ("iny");
2010 AddCodeLine ("lda (ptr1),y");
2011 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2012 AddCodeLine ("sta (ptr1),y");
2013 AddCodeLine ("tax");
2014 AddCodeLine ("pla");
2020 AddCodeLine ("jsr pushax"); /* Push the address */
2021 push (CF_PTR); /* Correct the internal sp */
2022 g_getind (flags, offs); /* Fetch the value */
2023 g_dec (flags, val); /* Increment value in primary */
2024 g_putind (flags, offs); /* Store the value back */
2034 /*****************************************************************************/
2035 /* Add a variable address to the value in ax */
2036 /*****************************************************************************/
2040 void g_addaddr_local (unsigned flags, int offs)
2041 /* Add the address of a local variable to ax */
2045 /* Add the offset */
2048 /* We cannot address more then 256 bytes of locals anyway */
2049 L = GetLocalLabel();
2050 CheckLocalOffs (offs);
2051 AddCodeLine ("clc");
2052 AddCodeLine ("adc #$%02X", offs & 0xFF);
2053 /* Do also skip the CLC insn below */
2054 AddCodeLine ("bcc %s", LocalLabelName (L));
2055 AddCodeLine ("inx");
2058 /* Add the current stackpointer value */
2059 AddCodeLine ("clc");
2061 /* Label was used above */
2064 AddCodeLine ("adc sp");
2065 AddCodeLine ("tay");
2066 AddCodeLine ("txa");
2067 AddCodeLine ("adc sp+1");
2068 AddCodeLine ("tax");
2069 AddCodeLine ("tya");
2074 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2075 /* Add the address of a static variable to ax */
2077 /* Create the correct label name */
2078 char* lbuf = GetLabelName (flags, label, offs);
2080 /* Add the address to the current ax value */
2081 AddCodeLine ("clc");
2082 AddCodeLine ("adc #<(%s)", lbuf);
2083 AddCodeLine ("tay");
2084 AddCodeLine ("txa");
2085 AddCodeLine ("adc #>(%s)", lbuf);
2086 AddCodeLine ("tax");
2087 AddCodeLine ("tya");
2092 /*****************************************************************************/
2094 /*****************************************************************************/
2098 void g_save (unsigned flags)
2099 /* Copy primary register to hold register. */
2101 /* Check the size and determine operation */
2102 switch (flags & CF_TYPE) {
2105 if (flags & CF_FORCECHAR) {
2106 AddCodeLine ("pha");
2112 AddCodeLine ("sta regsave");
2113 AddCodeLine ("stx regsave+1");
2117 AddCodeLine ("jsr saveeax");
2127 void g_restore (unsigned flags)
2128 /* Copy hold register to primary. */
2130 /* Check the size and determine operation */
2131 switch (flags & CF_TYPE) {
2134 if (flags & CF_FORCECHAR) {
2135 AddCodeLine ("pla");
2141 AddCodeLine ("lda regsave");
2142 AddCodeLine ("ldx regsave+1");
2146 AddCodeLine ("jsr resteax");
2156 void g_cmp (unsigned flags, unsigned long val)
2157 /* Immidiate compare. The primary register will not be changed, Z flag
2163 /* Check the size and determine operation */
2164 switch (flags & CF_TYPE) {
2167 if (flags & CF_FORCECHAR) {
2168 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2174 L = GetLocalLabel();
2175 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2176 AddCodeLine ("bne %s", LocalLabelName (L));
2177 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2182 Internal ("g_cmp: Long compares not implemented");
2192 static void oper (unsigned flags, unsigned long val, char** subs)
2193 /* Encode a binary operation. subs is a pointer to four groups of three
2195 * 0-2 --> Operate on ints
2196 * 3-5 --> Operate on unsigneds
2197 * 6-8 --> Operate on longs
2198 * 9-11 --> Operate on unsigned longs
2200 * The first subroutine names in each string group is used to encode an
2201 * operation with a zero constant, the second to encode an operation with
2202 * a 8 bit constant, and the third is used in all other cases.
2207 /* Determine the offset into the array */
2208 offs = (flags & CF_UNSIGNED)? 3 : 0;
2209 switch (flags & CF_TYPE) {
2222 /* Encode the operation */
2223 if (flags & CF_CONST) {
2224 /* Constant value given */
2225 if (val == 0 && subs [offs+0]) {
2226 /* Special case: constant with value zero */
2227 AddCodeLine ("jsr %s", subs [offs+0]);
2228 } else if (val < 0x100 && subs [offs+1]) {
2229 /* Special case: constant with high byte zero */
2230 ldaconst (val); /* Load low byte */
2231 AddCodeLine ("jsr %s", subs [offs+1]);
2233 /* Others: arbitrary constant value */
2234 g_getimmed (flags, val, 0); /* Load value */
2235 AddCodeLine ("jsr %s", subs [offs+2]);
2238 /* Value not constant (is already in (e)ax) */
2239 AddCodeLine ("jsr %s", subs [offs+2]);
2242 /* The operation will pop it's argument */
2248 void g_test (unsigned flags)
2249 /* Test the value in the primary and set the condition codes */
2251 switch (flags & CF_TYPE) {
2254 if (flags & CF_FORCECHAR) {
2255 AddCodeLine ("tax");
2261 AddCodeLine ("stx tmp1");
2262 AddCodeLine ("ora tmp1");
2266 if (flags & CF_UNSIGNED) {
2267 AddCodeLine ("jsr utsteax");
2269 AddCodeLine ("jsr tsteax");
2281 void g_push (unsigned flags, unsigned long val)
2282 /* Push the primary register or a constant value onto the stack */
2286 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2288 /* We have a constant 8 or 16 bit value */
2289 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2291 /* Handle as 8 bit value */
2292 if (CodeSizeFactor >= 165 || val > 2) {
2294 AddCodeLine ("jsr pusha");
2296 AddCodeLine ("jsr pushc%d", (int) val);
2301 /* Handle as 16 bit value */
2302 hi = (unsigned char) (val >> 8);
2304 AddCodeLine ("jsr push%u", (unsigned) val);
2305 } else if (hi == 0 || hi == 0xFF) {
2306 /* Use special function */
2308 AddCodeLine ("jsr %s", (hi == 0)? "pusha0" : "pushaFF");
2311 g_getimmed (flags, val, 0);
2312 AddCodeLine ("jsr pushax");
2318 /* Value is not 16 bit or not constant */
2319 if (flags & CF_CONST) {
2320 /* Constant 32 bit value, load into eax */
2321 g_getimmed (flags, val, 0);
2324 /* Push the primary register */
2325 switch (flags & CF_TYPE) {
2328 if (flags & CF_FORCECHAR) {
2329 /* Handle as char */
2330 AddCodeLine ("jsr pusha");
2335 AddCodeLine ("jsr pushax");
2339 AddCodeLine ("jsr pusheax");
2349 /* Adjust the stack offset */
2355 void g_swap (unsigned flags)
2356 /* Swap the primary register and the top of the stack. flags give the type
2357 * of *both* values (must have same size).
2360 switch (flags & CF_TYPE) {
2364 AddCodeLine ("jsr swapstk");
2368 AddCodeLine ("jsr swapestk");
2379 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2380 /* Call the specified subroutine name */
2382 if ((Flags & CF_FIXARGC) == 0) {
2383 /* Pass the argument count */
2386 AddCodeLine ("jsr _%s", Label);
2387 oursp += ArgSize; /* callee pops args */
2392 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2393 /* Call subroutine indirect */
2395 if ((Flags & CF_LOCAL) == 0) {
2396 /* Address is in a/x */
2397 if ((Flags & CF_FIXARGC) == 0) {
2398 /* Pass arg count */
2401 AddCodeLine ("jsr callax");
2403 /* The address is on stack, offset is on Val */
2405 CheckLocalOffs (Offs);
2406 AddCodeLine ("pha");
2407 AddCodeLine ("ldy #$%02X", Offs);
2408 AddCodeLine ("lda (sp),y");
2409 AddCodeLine ("sta jmpvec+1");
2410 AddCodeLine ("iny");
2411 AddCodeLine ("lda (sp),y");
2412 AddCodeLine ("sta jmpvec+2");
2413 AddCodeLine ("pla");
2414 AddCodeLine ("jsr jmpvec");
2417 /* Callee pops args */
2423 void g_jump (unsigned Label)
2424 /* Jump to specified internal label number */
2426 AddCodeLine ("jmp %s", LocalLabelName (Label));
2431 void g_switch (unsigned Flags)
2432 /* Output switch statement preamble */
2434 switch (Flags & CF_TYPE) {
2438 AddCodeLine ("jsr switch");
2442 AddCodeLine ("jsr lswitch");
2453 void g_case (unsigned flags, unsigned label, unsigned long val)
2454 /* Create table code for one case selector */
2456 switch (flags & CF_TYPE) {
2460 AddCodeLine (".word $%04X, %s",
2461 (unsigned)(val & 0xFFFF),
2462 LocalLabelName (label));
2466 AddCodeLine (".dword $%08lX", val);
2467 AddCodeLine (".word %s", LocalLabelName (label));
2478 void g_truejump (unsigned flags, unsigned label)
2479 /* Jump to label if zero flag clear */
2481 AddCodeLine ("jne %s", LocalLabelName (label));
2486 void g_falsejump (unsigned flags, unsigned label)
2487 /* Jump to label if zero flag set */
2489 AddCodeLine ("jeq %s", LocalLabelName (label));
2494 static void mod_internal (int k, char* verb1, char* verb2)
2497 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2501 AddCodeLine ("jsr %ssp", verb2);
2507 void g_space (int space)
2508 /* Create or drop space on the stack */
2511 mod_internal (-space, "inc", "addy");
2512 } else if (space > 0) {
2513 mod_internal (space, "dec", "suby");
2519 void g_cstackcheck (void)
2520 /* Check for a C stack overflow */
2522 AddCodeLine ("jsr cstkchk");
2527 void g_stackcheck (void)
2528 /* Check for a stack overflow */
2530 AddCodeLine ("jsr stkchk");
2535 void g_add (unsigned flags, unsigned long val)
2536 /* Primary = TOS + Primary */
2538 static char* ops [12] = {
2539 0, "tosadda0", "tosaddax",
2540 0, "tosadda0", "tosaddax",
2545 if (flags & CF_CONST) {
2546 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2547 g_push (flags & ~CF_CONST, 0);
2549 oper (flags, val, ops);
2554 void g_sub (unsigned flags, unsigned long val)
2555 /* Primary = TOS - Primary */
2557 static char* ops [12] = {
2558 0, "tossuba0", "tossubax",
2559 0, "tossuba0", "tossubax",
2564 if (flags & CF_CONST) {
2565 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2566 g_push (flags & ~CF_CONST, 0);
2568 oper (flags, val, ops);
2573 void g_rsub (unsigned flags, unsigned long val)
2574 /* Primary = Primary - TOS */
2576 static char* ops [12] = {
2577 0, "tosrsuba0", "tosrsubax",
2578 0, "tosrsuba0", "tosrsubax",
2582 oper (flags, val, ops);
2587 void g_mul (unsigned flags, unsigned long val)
2588 /* Primary = TOS * Primary */
2590 static char* ops [12] = {
2591 0, "tosmula0", "tosmulax",
2592 0, "tosumula0", "tosumulax",
2599 /* Do strength reduction if the value is constant and a power of two */
2600 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2601 /* Generate a shift instead */
2606 /* If the right hand side is const, the lhs is not on stack but still
2607 * in the primary register.
2609 if (flags & CF_CONST) {
2611 switch (flags & CF_TYPE) {
2614 if (flags & CF_FORCECHAR) {
2615 /* Handle some special cases */
2619 AddCodeLine ("sta tmp1");
2620 AddCodeLine ("asl a");
2621 AddCodeLine ("clc");
2622 AddCodeLine ("adc tmp1");
2626 AddCodeLine ("sta tmp1");
2627 AddCodeLine ("asl a");
2628 AddCodeLine ("asl a");
2629 AddCodeLine ("clc");
2630 AddCodeLine ("adc tmp1");
2634 AddCodeLine ("sta tmp1");
2635 AddCodeLine ("asl a");
2636 AddCodeLine ("asl a");
2637 AddCodeLine ("clc");
2638 AddCodeLine ("adc tmp1");
2639 AddCodeLine ("asl a");
2655 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2656 * into the normal, non-optimized stuff.
2658 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2659 g_push (flags & ~CF_CONST, 0);
2663 /* Use long way over the stack */
2664 oper (flags, val, ops);
2669 void g_div (unsigned flags, unsigned long val)
2670 /* Primary = TOS / Primary */
2672 static char* ops [12] = {
2673 0, "tosdiva0", "tosdivax",
2674 0, "tosudiva0", "tosudivax",
2679 /* Do strength reduction if the value is constant and a power of two */
2681 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2682 /* Generate a shift instead */
2685 /* Generate a division */
2686 if (flags & CF_CONST) {
2687 /* lhs is not on stack */
2688 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2689 g_push (flags & ~CF_CONST, 0);
2691 oper (flags, val, ops);
2697 void g_mod (unsigned flags, unsigned long val)
2698 /* Primary = TOS % Primary */
2700 static char* ops [12] = {
2701 0, "tosmoda0", "tosmodax",
2702 0, "tosumoda0", "tosumodax",
2708 /* Check if we can do some cost reduction */
2709 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2710 /* We can do that with an AND operation */
2711 g_and (flags, val - 1);
2713 /* Do it the hard way... */
2714 if (flags & CF_CONST) {
2715 /* lhs is not on stack */
2716 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2717 g_push (flags & ~CF_CONST, 0);
2719 oper (flags, val, ops);
2725 void g_or (unsigned flags, unsigned long val)
2726 /* Primary = TOS | Primary */
2728 static char* ops [12] = {
2729 0, "tosora0", "tosorax",
2730 0, "tosora0", "tosorax",
2735 /* If the right hand side is const, the lhs is not on stack but still
2736 * in the primary register.
2738 if (flags & CF_CONST) {
2740 switch (flags & CF_TYPE) {
2743 if (flags & CF_FORCECHAR) {
2744 if ((val & 0xFF) != 0xFF) {
2745 AddCodeLine ("ora #$%02X", (unsigned char)val);
2753 AddCodeLine ("ora #$%02X", (unsigned char)val);
2760 AddCodeLine ("ora #$%02X", (unsigned char)val);
2769 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2770 * into the normal, non-optimized stuff.
2772 g_push (flags & ~CF_CONST, 0);
2776 /* Use long way over the stack */
2777 oper (flags, val, ops);
2782 void g_xor (unsigned flags, unsigned long val)
2783 /* Primary = TOS ^ Primary */
2785 static char* ops [12] = {
2786 0, "tosxora0", "tosxorax",
2787 0, "tosxora0", "tosxorax",
2793 /* If the right hand side is const, the lhs is not on stack but still
2794 * in the primary register.
2796 if (flags & CF_CONST) {
2798 switch (flags & CF_TYPE) {
2801 if (flags & CF_FORCECHAR) {
2802 if ((val & 0xFF) != 0) {
2803 AddCodeLine ("eor #$%02X", (unsigned char)val);
2812 AddCodeLine ("eor #$%02X", (unsigned char)val);
2815 } else if ((val & 0xFF) == 0) {
2816 AddCodeLine ("pha");
2817 AddCodeLine ("txa");
2818 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2819 AddCodeLine ("tax");
2820 AddCodeLine ("pla");
2828 AddCodeLine ("eor #$%02X", (unsigned char)val);
2838 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2839 * into the normal, non-optimized stuff.
2841 g_push (flags & ~CF_CONST, 0);
2845 /* Use long way over the stack */
2846 oper (flags, val, ops);
2851 void g_and (unsigned flags, unsigned long val)
2852 /* Primary = TOS & Primary */
2854 static char* ops [12] = {
2855 0, "tosanda0", "tosandax",
2856 0, "tosanda0", "tosandax",
2861 /* If the right hand side is const, the lhs is not on stack but still
2862 * in the primary register.
2864 if (flags & CF_CONST) {
2866 switch (flags & CF_TYPE) {
2869 if (flags & CF_FORCECHAR) {
2870 AddCodeLine ("and #$%02X", (unsigned char)val);
2875 if ((val & 0xFFFF) != 0xFFFF) {
2880 } else if (val != 0xFF) {
2881 AddCodeLine ("and #$%02X", (unsigned char)val);
2883 } else if ((val & 0xFF00) == 0xFF00) {
2884 AddCodeLine ("and #$%02X", (unsigned char)val);
2885 } else if ((val & 0x00FF) == 0x0000) {
2886 AddCodeLine ("txa");
2887 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2888 AddCodeLine ("tax");
2891 AddCodeLine ("tay");
2892 AddCodeLine ("txa");
2893 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2894 AddCodeLine ("tax");
2895 AddCodeLine ("tya");
2896 if ((val & 0x00FF) != 0x00FF) {
2897 AddCodeLine ("and #$%02X", (unsigned char)val);
2906 AddCodeLine ("stx sreg+1");
2907 AddCodeLine ("stx sreg");
2908 if ((val & 0xFF) != 0xFF) {
2909 AddCodeLine ("and #$%02X", (unsigned char)val);
2912 } else if (val == 0xFF00) {
2914 AddCodeLine ("sta sreg+1");
2915 AddCodeLine ("sta sreg");
2924 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2925 * into the normal, non-optimized stuff.
2927 g_push (flags & ~CF_CONST, 0);
2931 /* Use long way over the stack */
2932 oper (flags, val, ops);
2937 void g_asr (unsigned flags, unsigned long val)
2938 /* Primary = TOS >> Primary */
2940 static char* ops [12] = {
2941 0, "tosasra0", "tosasrax",
2942 0, "tosshra0", "tosshrax",
2947 /* If the right hand side is const, the lhs is not on stack but still
2948 * in the primary register.
2950 if (flags & CF_CONST) {
2952 switch (flags & CF_TYPE) {
2956 if (val >= 1 && val <= 4) {
2957 if (flags & CF_UNSIGNED) {
2958 AddCodeLine ("jsr shrax%ld", val);
2960 AddCodeLine ("jsr asrax%ld", val);
2963 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2964 AddCodeLine ("txa");
2971 if (val >= 1 && val <= 4) {
2972 if (flags & CF_UNSIGNED) {
2973 AddCodeLine ("jsr shreax%ld", val);
2975 AddCodeLine ("jsr asreax%ld", val);
2978 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2979 AddCodeLine ("txa");
2980 AddCodeLine ("ldx sreg");
2981 AddCodeLine ("ldy sreg+1");
2982 AddCodeLine ("sty sreg");
2983 AddCodeLine ("ldy #$00");
2984 AddCodeLine ("sty sreg+1");
2986 } else if (val == 16) {
2987 AddCodeLine ("ldy #$00");
2988 AddCodeLine ("ldx sreg+1");
2989 if ((flags & CF_UNSIGNED) == 0) {
2990 unsigned L = GetLocalLabel();
2991 AddCodeLine ("bpl %s", LocalLabelName (L));
2992 AddCodeLine ("dey");
2995 AddCodeLine ("lda sreg");
2996 AddCodeLine ("sty sreg+1");
2997 AddCodeLine ("sty sreg");
3006 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3007 * into the normal, non-optimized stuff.
3009 g_push (flags & ~CF_CONST, 0);
3013 /* Use long way over the stack */
3014 oper (flags, val, ops);
3019 void g_asl (unsigned flags, unsigned long val)
3020 /* Primary = TOS << Primary */
3022 static char* ops [12] = {
3023 0, "tosasla0", "tosaslax",
3024 0, "tosshla0", "tosshlax",
3030 /* If the right hand side is const, the lhs is not on stack but still
3031 * in the primary register.
3033 if (flags & CF_CONST) {
3035 switch (flags & CF_TYPE) {
3039 if (val >= 1 && val <= 4) {
3040 if (flags & CF_UNSIGNED) {
3041 AddCodeLine ("jsr shlax%ld", val);
3043 AddCodeLine ("jsr aslax%ld", val);
3046 } else if (val == 8) {
3047 AddCodeLine ("tax");
3048 AddCodeLine ("lda #$00");
3054 if (val >= 1 && val <= 4) {
3055 if (flags & CF_UNSIGNED) {
3056 AddCodeLine ("jsr shleax%ld", val);
3058 AddCodeLine ("jsr asleax%ld", val);
3061 } else if (val == 8) {
3062 AddCodeLine ("ldy sreg");
3063 AddCodeLine ("sty sreg+1");
3064 AddCodeLine ("stx sreg");
3065 AddCodeLine ("tax");
3066 AddCodeLine ("lda #$00");
3068 } else if (val == 16) {
3069 AddCodeLine ("stx sreg+1");
3070 AddCodeLine ("sta sreg");
3071 AddCodeLine ("lda #$00");
3072 AddCodeLine ("tax");
3081 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3082 * into the normal, non-optimized stuff.
3084 g_push (flags & ~CF_CONST, 0);
3088 /* Use long way over the stack */
3089 oper (flags, val, ops);
3094 void g_neg (unsigned flags)
3095 /* Primary = -Primary */
3097 switch (flags & CF_TYPE) {
3101 AddCodeLine ("jsr negax");
3105 AddCodeLine ("jsr negeax");
3115 void g_bneg (unsigned flags)
3116 /* Primary = !Primary */
3118 switch (flags & CF_TYPE) {
3121 AddCodeLine ("jsr bnega");
3125 AddCodeLine ("jsr bnegax");
3129 AddCodeLine ("jsr bnegeax");
3139 void g_com (unsigned flags)
3140 /* Primary = ~Primary */
3142 switch (flags & CF_TYPE) {
3146 AddCodeLine ("jsr complax");
3150 AddCodeLine ("jsr compleax");
3160 void g_inc (unsigned flags, unsigned long val)
3161 /* Increment the primary register by a given number */
3163 /* Don't inc by zero */
3168 /* Generate code for the supported types */
3170 switch (flags & CF_TYPE) {
3173 if (flags & CF_FORCECHAR) {
3174 if (CPU == CPU_65C02 && val <= 2) {
3176 AddCodeLine ("ina");
3179 AddCodeLine ("clc");
3180 AddCodeLine ("adc #$%02X", (unsigned char)val);
3187 if (CPU == CPU_65C02 && val == 1) {
3188 unsigned L = GetLocalLabel();
3189 AddCodeLine ("ina");
3190 AddCodeLine ("bne %s", LocalLabelName (L));
3191 AddCodeLine ("inx");
3193 } else if (CodeSizeFactor < 200) {
3196 AddCodeLine ("jsr incax%lu", val);
3197 } else if (val <= 255) {
3199 AddCodeLine ("jsr incaxy");
3201 g_add (flags | CF_CONST, val);
3204 /* Inline the code */
3206 if ((val & 0xFF) != 0) {
3207 unsigned L = GetLocalLabel();
3208 AddCodeLine ("clc");
3209 AddCodeLine ("adc #$%02X", (unsigned char) val);
3210 AddCodeLine ("bcc %s", LocalLabelName (L));
3211 AddCodeLine ("inx");
3215 AddCodeLine ("inx");
3218 AddCodeLine ("inx");
3221 AddCodeLine ("clc");
3222 if ((val & 0xFF) != 0) {
3223 AddCodeLine ("adc #$%02X", (unsigned char) val);
3225 AddCodeLine ("pha");
3226 AddCodeLine ("txa");
3227 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3228 AddCodeLine ("tax");
3229 AddCodeLine ("pla");
3237 AddCodeLine ("jsr inceaxy");
3239 g_add (flags | CF_CONST, val);
3251 void g_dec (unsigned flags, unsigned long val)
3252 /* Decrement the primary register by a given number */
3254 /* Don't dec by zero */
3259 /* Generate code for the supported types */
3261 switch (flags & CF_TYPE) {
3264 if (flags & CF_FORCECHAR) {
3265 if (CPU == CPU_65C02 && val <= 2) {
3267 AddCodeLine ("dea");
3270 AddCodeLine ("sec");
3271 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3278 if (CodeSizeFactor < 200) {
3279 /* Use subroutines */
3281 AddCodeLine ("jsr decax%d", (int) val);
3282 } else if (val <= 255) {
3284 AddCodeLine ("jsr decaxy");
3286 g_sub (flags | CF_CONST, val);
3289 /* Inline the code */
3291 if ((val & 0xFF) != 0) {
3292 unsigned L = GetLocalLabel();
3293 AddCodeLine ("sec");
3294 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3295 AddCodeLine ("bcs %s", LocalLabelName (L));
3296 AddCodeLine ("dex");
3300 AddCodeLine ("dex");
3303 AddCodeLine ("dex");
3306 AddCodeLine ("sec");
3307 if ((val & 0xFF) != 0) {
3308 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3310 AddCodeLine ("pha");
3311 AddCodeLine ("txa");
3312 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3313 AddCodeLine ("tax");
3314 AddCodeLine ("pla");
3322 AddCodeLine ("jsr deceaxy");
3324 g_sub (flags | CF_CONST, val);
3337 * Following are the conditional operators. They compare the TOS against
3338 * the primary and put a literal 1 in the primary if the condition is
3339 * true, otherwise they clear the primary register
3344 void g_eq (unsigned flags, unsigned long val)
3345 /* Test for equal */
3347 static char* ops [12] = {
3348 "toseq00", "toseqa0", "toseqax",
3349 "toseq00", "toseqa0", "toseqax",
3356 /* If the right hand side is const, the lhs is not on stack but still
3357 * in the primary register.
3359 if (flags & CF_CONST) {
3361 switch (flags & CF_TYPE) {
3364 if (flags & CF_FORCECHAR) {
3365 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3366 AddCodeLine ("jsr booleq");
3372 L = GetLocalLabel();
3373 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3374 AddCodeLine ("bne %s", LocalLabelName (L));
3375 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3377 AddCodeLine ("jsr booleq");
3387 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3388 * into the normal, non-optimized stuff.
3390 g_push (flags & ~CF_CONST, 0);
3394 /* Use long way over the stack */
3395 oper (flags, val, ops);
3400 void g_ne (unsigned flags, unsigned long val)
3401 /* Test for not equal */
3403 static char* ops [12] = {
3404 "tosne00", "tosnea0", "tosneax",
3405 "tosne00", "tosnea0", "tosneax",
3412 /* If the right hand side is const, the lhs is not on stack but still
3413 * in the primary register.
3415 if (flags & CF_CONST) {
3417 switch (flags & CF_TYPE) {
3420 if (flags & CF_FORCECHAR) {
3421 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3422 AddCodeLine ("jsr boolne");
3428 L = GetLocalLabel();
3429 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3430 AddCodeLine ("bne %s", LocalLabelName (L));
3431 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3433 AddCodeLine ("jsr boolne");
3443 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3444 * into the normal, non-optimized stuff.
3446 g_push (flags & ~CF_CONST, 0);
3450 /* Use long way over the stack */
3451 oper (flags, val, ops);
3456 void g_lt (unsigned flags, unsigned long val)
3457 /* Test for less than */
3459 static char* ops [12] = {
3460 "toslt00", "toslta0", "tosltax",
3461 "tosult00", "tosulta0", "tosultax",
3466 /* If the right hand side is const, the lhs is not on stack but still
3467 * in the primary register.
3469 if (flags & CF_CONST) {
3471 /* Give a warning in some special cases */
3472 if ((flags & CF_UNSIGNED) && val == 0) {
3473 Warning ("Condition is never true");
3476 /* Look at the type */
3477 switch (flags & CF_TYPE) {
3480 if (flags & CF_FORCECHAR) {
3481 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3482 if (flags & CF_UNSIGNED) {
3483 AddCodeLine ("jsr boolult");
3485 AddCodeLine ("jsr boollt");
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 ("txa");
3497 AddCodeLine ("jsr boollt");
3500 /* Direct code only for unsigned data types */
3501 if (flags & CF_UNSIGNED) {
3502 unsigned L = GetLocalLabel();
3503 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3504 AddCodeLine ("bne %s", LocalLabelName (L));
3505 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3507 AddCodeLine ("jsr boolult");
3513 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3514 /* If we have a signed compare against zero, we only need to
3515 * test the high byte.
3517 AddCodeLine ("lda sreg+1");
3518 AddCodeLine ("jsr boollt");
3527 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3528 * into the normal, non-optimized stuff.
3530 g_push (flags & ~CF_CONST, 0);
3534 /* Use long way over the stack */
3535 oper (flags, val, ops);
3540 void g_le (unsigned flags, unsigned long val)
3541 /* Test for less than or equal to */
3543 static char* ops [12] = {
3544 "tosle00", "toslea0", "tosleax",
3545 "tosule00", "tosulea0", "tosuleax",
3551 /* If the right hand side is const, the lhs is not on stack but still
3552 * in the primary register.
3554 if (flags & CF_CONST) {
3556 /* <= is not very effective on the 6502, so try to convert
3557 * it into < if the value is in a valid range.
3560 /* Look at the type */
3561 switch (flags & CF_TYPE) {
3564 if (flags & CF_FORCECHAR) {
3565 if (flags & CF_UNSIGNED) {
3567 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3568 AddCodeLine ("jsr boolult");
3570 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3571 AddCodeLine ("jsr boolule");
3575 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3576 AddCodeLine ("jsr boollt");
3578 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3579 AddCodeLine ("jsr boolle");
3587 if (flags & CF_UNSIGNED) {
3588 unsigned L = GetLocalLabel();
3589 const char* Name = "boolule";
3594 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3595 AddCodeLine ("bne %s", LocalLabelName (L));
3596 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3598 AddCodeLine ("jsr %s", Name);
3610 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3611 * into the normal, non-optimized stuff.
3613 g_push (flags & ~CF_CONST, 0);
3617 /* Use long way over the stack */
3618 oper (flags, val, ops);
3623 void g_gt (unsigned flags, unsigned long val)
3624 /* Test for greater than */
3626 static char* ops [12] = {
3627 "tosgt00", "tosgta0", "tosgtax",
3628 "tosugt00", "tosugta0", "tosugtax",
3634 /* If the right hand side is const, the lhs is not on stack but still
3635 * in the primary register.
3637 if (flags & CF_CONST) {
3639 /* > is not very effective on the 6502, so try to convert
3640 * it into >= if the value is in a valid range.
3643 /* Look at the type */
3644 switch (flags & CF_TYPE) {
3647 if (flags & CF_FORCECHAR) {
3648 if (flags & CF_UNSIGNED) {
3649 /* If we have a compare > 0, we will replace it by
3650 * != 0 here, since both are identical but the latter
3651 * is easier to optimize.
3654 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3655 AddCodeLine ("jsr boolne");
3656 } else if (val < 255) {
3657 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3658 AddCodeLine ("jsr booluge");
3660 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3661 AddCodeLine ("jsr boolugt");
3665 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3666 AddCodeLine ("jsr boolge");
3668 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3669 AddCodeLine ("jsr boolgt");
3677 if (flags & CF_UNSIGNED) {
3678 /* If we have a compare > 0, we will replace it by
3679 * != 0 here, since both are identical but the latter
3680 * is easier to optimize.
3683 AddCodeLine ("stx tmp1");
3684 AddCodeLine ("ora tmp1");
3685 AddCodeLine ("jsr boolne");
3687 unsigned L = GetLocalLabel();
3688 const char* Name = "boolugt";
3693 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3694 AddCodeLine ("bne %s", LocalLabelName (L));
3695 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3697 AddCodeLine ("jsr %s", Name);
3710 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3711 * into the normal, non-optimized stuff.
3713 g_push (flags & ~CF_CONST, 0);
3717 /* Use long way over the stack */
3718 oper (flags, val, ops);
3723 void g_ge (unsigned flags, unsigned long val)
3724 /* Test for greater than or equal to */
3726 static char* ops [12] = {
3727 "tosge00", "tosgea0", "tosgeax",
3728 "tosuge00", "tosugea0", "tosugeax",
3734 /* If the right hand side is const, the lhs is not on stack but still
3735 * in the primary register.
3737 if (flags & CF_CONST) {
3739 /* Give a warning in some special cases */
3740 if ((flags & CF_UNSIGNED) && val == 0) {
3741 Warning ("Condition is always true");
3744 /* Look at the type */
3745 switch (flags & CF_TYPE) {
3748 if (flags & CF_FORCECHAR) {
3749 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3750 if (flags & CF_UNSIGNED) {
3751 AddCodeLine ("jsr booluge");
3753 AddCodeLine ("jsr boolge");
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 ("txa");
3765 AddCodeLine ("jsr boolge");
3768 /* Direct code only for unsigned data types */
3769 if (flags & CF_UNSIGNED) {
3770 unsigned L = GetLocalLabel();
3771 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3772 AddCodeLine ("bne %s", LocalLabelName (L));
3773 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3775 AddCodeLine ("jsr booluge");
3781 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3782 /* If we have a signed compare against zero, we only need to
3783 * test the high byte.
3785 AddCodeLine ("lda sreg+1");
3786 AddCodeLine ("jsr boolge");
3795 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3796 * into the normal, non-optimized stuff.
3798 g_push (flags & ~CF_CONST, 0);
3802 /* Use long way over the stack */
3803 oper (flags, val, ops);
3808 /*****************************************************************************/
3809 /* Allocating static storage */
3810 /*****************************************************************************/
3814 void g_res (unsigned n)
3815 /* Reserve static storage, n bytes */
3817 AddDataLine ("\t.res\t%u,$00", n);
3822 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3823 /* Define data with the size given in flags */
3825 if (flags & CF_CONST) {
3827 /* Numeric constant */
3828 switch (flags & CF_TYPE) {
3831 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
3835 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
3839 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3850 /* Create the correct label name */
3851 const char* Label = GetLabelName (flags, val, offs);
3853 /* Labels are always 16 bit */
3854 AddDataLine ("\t.word\t%s", Label);
3861 void g_defbytes (const void* Bytes, unsigned Count)
3862 /* Output a row of bytes as a constant */
3868 /* Cast the buffer pointer */
3869 const unsigned char* Data = (const unsigned char*) Bytes;
3871 /* Output the stuff */
3874 /* How many go into this line? */
3875 if ((Chunk = Count) > 16) {
3880 /* Output one line */
3881 strcpy (Buf, "\t.byte\t");
3884 B += sprintf (B, "$%02X", *Data++);
3890 /* Output the line */
3897 void g_zerobytes (unsigned n)
3898 /* Output n bytes of data initialized with zero */
3900 AddDataLine ("\t.res\t%u,$00", n);
3905 /*****************************************************************************/
3906 /* User supplied assembler code */
3907 /*****************************************************************************/
3911 void g_asmcode (struct StrBuf* B)
3912 /* Output one line of assembler code. */
3914 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));
3919 /*****************************************************************************/
3920 /* Inlined known functions */
3921 /*****************************************************************************/
3925 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3926 /* Inline the strlen() function */
3928 /* We need a label in both cases */
3929 unsigned label = GetLocalLabel ();
3931 /* Two different encodings */
3932 if (flags & CF_CONST) {
3934 /* The address of the string is constant. Create the correct label name */
3935 char* lbuf = GetLabelName (flags, val, offs);
3937 /* Generate the strlen code */
3938 AddCodeLine ("ldy #$FF");
3939 g_defcodelabel (label);
3940 AddCodeLine ("iny");
3941 AddCodeLine ("lda %s,y", lbuf);
3942 AddCodeLine ("bne %s", LocalLabelName (label));
3943 AddCodeLine ("tax");
3944 AddCodeLine ("tya");
3948 /* Address not constant but in primary */
3949 if (CodeSizeFactor < 400) {
3950 /* This is too much code, so call strlen instead of inlining */
3951 AddCodeLine ("jsr _strlen");
3953 /* Inline the function */
3954 AddCodeLine ("sta ptr1");
3955 AddCodeLine ("stx ptr1+1");
3956 AddCodeLine ("ldy #$FF");
3957 g_defcodelabel (label);
3958 AddCodeLine ("iny");
3959 AddCodeLine ("lda (ptr1),y");
3960 AddCodeLine ("bne %s", LocalLabelName (label));
3961 AddCodeLine ("tax");
3962 AddCodeLine ("tya");