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");
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");
1704 if (flags & CF_CONST) {
1705 g_getimmed (flags, val, 0);
1708 AddCodeLine ("jsr addeqysp");
1712 if (flags & CF_CONST) {
1713 g_getimmed (flags, val, 0);
1716 AddCodeLine ("jsr laddeq0sp");
1719 AddCodeLine ("jsr laddeqysp");
1730 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1731 /* Emit += for the location with address in ax */
1733 /* If the offset is too large for a byte register, add the high byte
1734 * of the offset to the primary. Beware: We need a special correction
1735 * if the offset in the low byte will overflow in the operation.
1737 offs = MakeByteOffs (flags, offs);
1739 /* Check the size and determine operation */
1740 switch (flags & CF_TYPE) {
1743 AddCodeLine ("sta ptr1");
1744 AddCodeLine ("stx ptr1+1");
1745 AddCodeLine ("ldy #$%02X", offs);
1746 AddCodeLine ("ldx #$00");
1747 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1748 AddCodeLine ("clc");
1749 AddCodeLine ("adc (ptr1),y");
1750 AddCodeLine ("sta (ptr1),y");
1754 if (CodeSizeFactor >= 200) {
1755 /* Lots of code, use only if size is not important */
1756 AddCodeLine ("sta ptr1");
1757 AddCodeLine ("stx ptr1+1");
1758 AddCodeLine ("ldy #$%02X", offs);
1759 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1760 AddCodeLine ("clc");
1761 AddCodeLine ("adc (ptr1),y");
1762 AddCodeLine ("sta (ptr1),y");
1763 AddCodeLine ("pha");
1764 AddCodeLine ("iny");
1765 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1766 AddCodeLine ("adc (ptr1),y");
1767 AddCodeLine ("sta (ptr1),y");
1768 AddCodeLine ("tax");
1769 AddCodeLine ("pla");
1775 AddCodeLine ("jsr pushax"); /* Push the address */
1776 push (CF_PTR); /* Correct the internal sp */
1777 g_getind (flags, offs); /* Fetch the value */
1778 g_inc (flags, val); /* Increment value in primary */
1779 g_putind (flags, offs); /* Store the value back */
1789 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1791 /* Emit -= for a static variable */
1793 /* Create the correct label name */
1794 char* lbuf = GetLabelName (flags, label, offs);
1796 /* Check the size and determine operation */
1797 switch (flags & CF_TYPE) {
1800 if (flags & CF_FORCECHAR) {
1801 AddCodeLine ("ldx #$00");
1802 if (flags & CF_CONST) {
1804 AddCodeLine ("dec %s", lbuf);
1805 AddCodeLine ("lda %s", lbuf);
1807 AddCodeLine ("sec");
1808 AddCodeLine ("lda %s", lbuf);
1809 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1810 AddCodeLine ("sta %s", lbuf);
1813 AddCodeLine ("sec");
1814 AddCodeLine ("sta tmp1");
1815 AddCodeLine ("lda %s", lbuf);
1816 AddCodeLine ("sbc tmp1");
1817 AddCodeLine ("sta %s", lbuf);
1819 if ((flags & CF_UNSIGNED) == 0) {
1820 unsigned L = GetLocalLabel();
1821 AddCodeLine ("bpl %s", LocalLabelName (L));
1822 AddCodeLine ("dex");
1830 AddCodeLine ("sec");
1831 if (flags & CF_CONST) {
1832 AddCodeLine ("lda %s", lbuf);
1833 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1834 AddCodeLine ("sta %s", lbuf);
1836 unsigned L = GetLocalLabel ();
1837 AddCodeLine ("bcs %s", LocalLabelName (L));
1838 AddCodeLine ("dec %s+1", lbuf);
1840 AddCodeLine ("ldx %s+1", lbuf);
1842 AddCodeLine ("lda %s+1", lbuf);
1843 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1844 AddCodeLine ("sta %s+1", lbuf);
1845 AddCodeLine ("tax");
1846 AddCodeLine ("lda %s", lbuf);
1849 AddCodeLine ("sta tmp1");
1850 AddCodeLine ("lda %s", lbuf);
1851 AddCodeLine ("sbc tmp1");
1852 AddCodeLine ("sta %s", lbuf);
1853 AddCodeLine ("stx tmp1");
1854 AddCodeLine ("lda %s+1", lbuf);
1855 AddCodeLine ("sbc tmp1");
1856 AddCodeLine ("sta %s+1", lbuf);
1857 AddCodeLine ("tax");
1858 AddCodeLine ("lda %s", lbuf);
1863 if (flags & CF_CONST) {
1865 AddCodeLine ("ldy #<(%s)", lbuf);
1866 AddCodeLine ("sty ptr1");
1867 AddCodeLine ("ldy #>(%s+1)", lbuf);
1869 AddCodeLine ("jsr lsubeq1");
1871 AddCodeLine ("lda #$%02X", (unsigned char)val);
1872 AddCodeLine ("jsr lsubeqa");
1875 g_getstatic (flags, label, offs);
1877 g_putstatic (flags, label, offs);
1880 AddCodeLine ("ldy #<(%s)", lbuf);
1881 AddCodeLine ("sty ptr1");
1882 AddCodeLine ("ldy #>(%s+1)", lbuf);
1883 AddCodeLine ("jsr lsubeq");
1894 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1895 /* Emit -= for a local variable */
1897 /* Calculate the true offset, check it, load it into Y */
1899 CheckLocalOffs (offs);
1901 /* Check the size and determine operation */
1902 switch (flags & CF_TYPE) {
1905 if (flags & CF_FORCECHAR) {
1907 AddCodeLine ("ldx #$00");
1908 AddCodeLine ("sec");
1909 if (flags & CF_CONST) {
1910 AddCodeLine ("lda (sp),y");
1911 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1913 AddCodeLine ("sta tmp1");
1914 AddCodeLine ("lda (sp),y");
1915 AddCodeLine ("sbc tmp1");
1917 AddCodeLine ("sta (sp),y");
1918 if ((flags & CF_UNSIGNED) == 0) {
1919 unsigned L = GetLocalLabel();
1920 AddCodeLine ("bpl %s", LocalLabelName (L));
1921 AddCodeLine ("dex");
1929 if (flags & CF_CONST) {
1930 g_getimmed (flags, val, 0);
1933 AddCodeLine ("jsr subeq0sp");
1936 AddCodeLine ("jsr subeqysp");
1941 if (flags & CF_CONST) {
1942 g_getimmed (flags, val, 0);
1945 AddCodeLine ("jsr lsubeq0sp");
1948 AddCodeLine ("jsr lsubeqysp");
1959 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
1960 /* Emit -= for the location with address in ax */
1962 /* If the offset is too large for a byte register, add the high byte
1963 * of the offset to the primary. Beware: We need a special correction
1964 * if the offset in the low byte will overflow in the operation.
1966 offs = MakeByteOffs (flags, offs);
1968 /* Check the size and determine operation */
1969 switch (flags & CF_TYPE) {
1972 AddCodeLine ("sta ptr1");
1973 AddCodeLine ("stx ptr1+1");
1974 AddCodeLine ("ldy #$%02X", offs);
1975 AddCodeLine ("ldx #$00");
1976 AddCodeLine ("lda (ptr1),y");
1977 AddCodeLine ("sec");
1978 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1979 AddCodeLine ("sta (ptr1),y");
1983 if (CodeSizeFactor >= 200) {
1984 /* Lots of code, use only if size is not important */
1985 AddCodeLine ("sta ptr1");
1986 AddCodeLine ("stx ptr1+1");
1987 AddCodeLine ("ldy #$%02X", offs);
1988 AddCodeLine ("lda (ptr1),y");
1989 AddCodeLine ("sec");
1990 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1991 AddCodeLine ("sta (ptr1),y");
1992 AddCodeLine ("pha");
1993 AddCodeLine ("iny");
1994 AddCodeLine ("lda (ptr1),y");
1995 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1996 AddCodeLine ("sta (ptr1),y");
1997 AddCodeLine ("tax");
1998 AddCodeLine ("pla");
2004 AddCodeLine ("jsr pushax"); /* Push the address */
2005 push (CF_PTR); /* Correct the internal sp */
2006 g_getind (flags, offs); /* Fetch the value */
2007 g_dec (flags, val); /* Increment value in primary */
2008 g_putind (flags, offs); /* Store the value back */
2018 /*****************************************************************************/
2019 /* Add a variable address to the value in ax */
2020 /*****************************************************************************/
2024 void g_addaddr_local (unsigned flags, int offs)
2025 /* Add the address of a local variable to ax */
2029 /* Add the offset */
2032 /* We cannot address more then 256 bytes of locals anyway */
2033 L = GetLocalLabel();
2034 CheckLocalOffs (offs);
2035 AddCodeLine ("clc");
2036 AddCodeLine ("adc #$%02X", offs & 0xFF);
2037 /* Do also skip the CLC insn below */
2038 AddCodeLine ("bcc %s", LocalLabelName (L));
2039 AddCodeLine ("inx");
2042 /* Add the current stackpointer value */
2043 AddCodeLine ("clc");
2045 /* Label was used above */
2048 AddCodeLine ("adc sp");
2049 AddCodeLine ("tay");
2050 AddCodeLine ("txa");
2051 AddCodeLine ("adc sp+1");
2052 AddCodeLine ("tax");
2053 AddCodeLine ("tya");
2058 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2059 /* Add the address of a static variable to ax */
2061 /* Create the correct label name */
2062 char* lbuf = GetLabelName (flags, label, offs);
2064 /* Add the address to the current ax value */
2065 AddCodeLine ("clc");
2066 AddCodeLine ("adc #<(%s)", lbuf);
2067 AddCodeLine ("tay");
2068 AddCodeLine ("txa");
2069 AddCodeLine ("adc #>(%s)", lbuf);
2070 AddCodeLine ("tax");
2071 AddCodeLine ("tya");
2076 /*****************************************************************************/
2078 /*****************************************************************************/
2082 void g_save (unsigned flags)
2083 /* Copy primary register to hold register. */
2085 /* Check the size and determine operation */
2086 switch (flags & CF_TYPE) {
2089 if (flags & CF_FORCECHAR) {
2090 AddCodeLine ("pha");
2096 AddCodeLine ("sta regsave");
2097 AddCodeLine ("stx regsave+1");
2101 AddCodeLine ("jsr saveeax");
2111 void g_restore (unsigned flags)
2112 /* Copy hold register to primary. */
2114 /* Check the size and determine operation */
2115 switch (flags & CF_TYPE) {
2118 if (flags & CF_FORCECHAR) {
2119 AddCodeLine ("pla");
2125 AddCodeLine ("lda regsave");
2126 AddCodeLine ("ldx regsave+1");
2130 AddCodeLine ("jsr resteax");
2140 void g_cmp (unsigned flags, unsigned long val)
2141 /* Immidiate compare. The primary register will not be changed, Z flag
2147 /* Check the size and determine operation */
2148 switch (flags & CF_TYPE) {
2151 if (flags & CF_FORCECHAR) {
2152 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2158 L = GetLocalLabel();
2159 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2160 AddCodeLine ("bne %s", LocalLabelName (L));
2161 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2166 Internal ("g_cmp: Long compares not implemented");
2176 static void oper (unsigned flags, unsigned long val, char** subs)
2177 /* Encode a binary operation. subs is a pointer to four groups of three
2179 * 0-2 --> Operate on ints
2180 * 3-5 --> Operate on unsigneds
2181 * 6-8 --> Operate on longs
2182 * 9-11 --> Operate on unsigned longs
2184 * The first subroutine names in each string group is used to encode an
2185 * operation with a zero constant, the second to encode an operation with
2186 * a 8 bit constant, and the third is used in all other cases.
2191 /* Determine the offset into the array */
2192 offs = (flags & CF_UNSIGNED)? 3 : 0;
2193 switch (flags & CF_TYPE) {
2206 /* Encode the operation */
2207 if (flags & CF_CONST) {
2208 /* Constant value given */
2209 if (val == 0 && subs [offs+0]) {
2210 /* Special case: constant with value zero */
2211 AddCodeLine ("jsr %s", subs [offs+0]);
2212 } else if (val < 0x100 && subs [offs+1]) {
2213 /* Special case: constant with high byte zero */
2214 ldaconst (val); /* Load low byte */
2215 AddCodeLine ("jsr %s", subs [offs+1]);
2217 /* Others: arbitrary constant value */
2218 g_getimmed (flags, val, 0); /* Load value */
2219 AddCodeLine ("jsr %s", subs [offs+2]);
2222 /* Value not constant (is already in (e)ax) */
2223 AddCodeLine ("jsr %s", subs [offs+2]);
2226 /* The operation will pop it's argument */
2232 void g_test (unsigned flags)
2233 /* Test the value in the primary and set the condition codes */
2235 switch (flags & CF_TYPE) {
2238 if (flags & CF_FORCECHAR) {
2239 AddCodeLine ("tax");
2245 AddCodeLine ("stx tmp1");
2246 AddCodeLine ("ora tmp1");
2250 if (flags & CF_UNSIGNED) {
2251 AddCodeLine ("jsr utsteax");
2253 AddCodeLine ("jsr tsteax");
2265 void g_push (unsigned flags, unsigned long val)
2266 /* Push the primary register or a constant value onto the stack */
2270 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2272 /* We have a constant 8 or 16 bit value */
2273 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2275 /* Handle as 8 bit value */
2276 if (CodeSizeFactor >= 165 || val > 2) {
2278 AddCodeLine ("jsr pusha");
2280 AddCodeLine ("jsr pushc%d", (int) val);
2285 /* Handle as 16 bit value */
2286 hi = (unsigned char) (val >> 8);
2288 AddCodeLine ("jsr push%u", (unsigned) val);
2289 } else if (hi == 0 || hi == 0xFF) {
2290 /* Use special function */
2292 AddCodeLine ("jsr %s", (hi == 0)? "pusha0" : "pushaFF");
2295 g_getimmed (flags, val, 0);
2296 AddCodeLine ("jsr pushax");
2302 /* Value is not 16 bit or not constant */
2303 if (flags & CF_CONST) {
2304 /* Constant 32 bit value, load into eax */
2305 g_getimmed (flags, val, 0);
2308 /* Push the primary register */
2309 switch (flags & CF_TYPE) {
2312 if (flags & CF_FORCECHAR) {
2313 /* Handle as char */
2314 AddCodeLine ("jsr pusha");
2319 AddCodeLine ("jsr pushax");
2323 AddCodeLine ("jsr pusheax");
2333 /* Adjust the stack offset */
2339 void g_swap (unsigned flags)
2340 /* Swap the primary register and the top of the stack. flags give the type
2341 * of *both* values (must have same size).
2344 switch (flags & CF_TYPE) {
2348 AddCodeLine ("jsr swapstk");
2352 AddCodeLine ("jsr swapestk");
2363 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2364 /* Call the specified subroutine name */
2366 if ((Flags & CF_FIXARGC) == 0) {
2367 /* Pass the argument count */
2370 AddCodeLine ("jsr _%s", Label);
2371 oursp += ArgSize; /* callee pops args */
2376 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2377 /* Call subroutine indirect */
2379 if ((Flags & CF_LOCAL) == 0) {
2380 /* Address is in a/x */
2381 if ((Flags & CF_FIXARGC) == 0) {
2382 /* Pass arg count */
2385 AddCodeLine ("jsr callax");
2387 /* The address is on stack, offset is on Val */
2389 CheckLocalOffs (Offs);
2390 AddCodeLine ("pha");
2391 AddCodeLine ("ldy #$%02X", Offs);
2392 AddCodeLine ("lda (sp),y");
2393 AddCodeLine ("sta jmpvec+1");
2394 AddCodeLine ("iny");
2395 AddCodeLine ("lda (sp),y");
2396 AddCodeLine ("sta jmpvec+2");
2397 AddCodeLine ("pla");
2398 AddCodeLine ("jsr jmpvec");
2401 /* Callee pops args */
2407 void g_jump (unsigned Label)
2408 /* Jump to specified internal label number */
2410 AddCodeLine ("jmp %s", LocalLabelName (Label));
2415 void g_switch (unsigned Flags)
2416 /* Output switch statement preamble */
2418 switch (Flags & CF_TYPE) {
2422 AddCodeLine ("jsr switch");
2426 AddCodeLine ("jsr lswitch");
2437 void g_case (unsigned flags, unsigned label, unsigned long val)
2438 /* Create table code for one case selector */
2440 switch (flags & CF_TYPE) {
2444 AddCodeLine (".word $%04X, %s",
2445 (unsigned)(val & 0xFFFF),
2446 LocalLabelName (label));
2450 AddCodeLine (".dword $%08lX", val);
2451 AddCodeLine (".word %s", LocalLabelName (label));
2462 void g_truejump (unsigned flags, unsigned label)
2463 /* Jump to label if zero flag clear */
2465 AddCodeLine ("jne %s", LocalLabelName (label));
2470 void g_falsejump (unsigned flags, unsigned label)
2471 /* Jump to label if zero flag set */
2473 AddCodeLine ("jeq %s", LocalLabelName (label));
2478 static void mod_internal (int k, char* verb1, char* verb2)
2481 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2485 AddCodeLine ("jsr %ssp", verb2);
2491 void g_space (int space)
2492 /* Create or drop space on the stack */
2495 mod_internal (-space, "inc", "addy");
2496 } else if (space > 0) {
2497 mod_internal (space, "dec", "suby");
2503 void g_cstackcheck (void)
2504 /* Check for a C stack overflow */
2506 AddCodeLine ("jsr cstkchk");
2511 void g_stackcheck (void)
2512 /* Check for a stack overflow */
2514 AddCodeLine ("jsr stkchk");
2519 void g_add (unsigned flags, unsigned long val)
2520 /* Primary = TOS + Primary */
2522 static char* ops [12] = {
2523 0, "tosadda0", "tosaddax",
2524 0, "tosadda0", "tosaddax",
2529 if (flags & CF_CONST) {
2530 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2531 g_push (flags & ~CF_CONST, 0);
2533 oper (flags, val, ops);
2538 void g_sub (unsigned flags, unsigned long val)
2539 /* Primary = TOS - Primary */
2541 static char* ops [12] = {
2542 0, "tossuba0", "tossubax",
2543 0, "tossuba0", "tossubax",
2548 if (flags & CF_CONST) {
2549 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2550 g_push (flags & ~CF_CONST, 0);
2552 oper (flags, val, ops);
2557 void g_rsub (unsigned flags, unsigned long val)
2558 /* Primary = Primary - TOS */
2560 static char* ops [12] = {
2561 0, "tosrsuba0", "tosrsubax",
2562 0, "tosrsuba0", "tosrsubax",
2566 oper (flags, val, ops);
2571 void g_mul (unsigned flags, unsigned long val)
2572 /* Primary = TOS * Primary */
2574 static char* ops [12] = {
2575 0, "tosmula0", "tosmulax",
2576 0, "tosumula0", "tosumulax",
2583 /* Do strength reduction if the value is constant and a power of two */
2584 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2585 /* Generate a shift instead */
2590 /* If the right hand side is const, the lhs is not on stack but still
2591 * in the primary register.
2593 if (flags & CF_CONST) {
2595 switch (flags & CF_TYPE) {
2598 if (flags & CF_FORCECHAR) {
2599 /* Handle some special cases */
2603 AddCodeLine ("sta tmp1");
2604 AddCodeLine ("asl a");
2605 AddCodeLine ("clc");
2606 AddCodeLine ("adc tmp1");
2610 AddCodeLine ("sta tmp1");
2611 AddCodeLine ("asl a");
2612 AddCodeLine ("asl a");
2613 AddCodeLine ("clc");
2614 AddCodeLine ("adc tmp1");
2618 AddCodeLine ("sta tmp1");
2619 AddCodeLine ("asl a");
2620 AddCodeLine ("asl a");
2621 AddCodeLine ("clc");
2622 AddCodeLine ("adc tmp1");
2623 AddCodeLine ("asl a");
2639 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2640 * into the normal, non-optimized stuff.
2642 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2643 g_push (flags & ~CF_CONST, 0);
2647 /* Use long way over the stack */
2648 oper (flags, val, ops);
2653 void g_div (unsigned flags, unsigned long val)
2654 /* Primary = TOS / Primary */
2656 static char* ops [12] = {
2657 0, "tosdiva0", "tosdivax",
2658 0, "tosudiva0", "tosudivax",
2663 /* Do strength reduction if the value is constant and a power of two */
2665 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2666 /* Generate a shift instead */
2669 /* Generate a division */
2670 if (flags & CF_CONST) {
2671 /* lhs is not on stack */
2672 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2673 g_push (flags & ~CF_CONST, 0);
2675 oper (flags, val, ops);
2681 void g_mod (unsigned flags, unsigned long val)
2682 /* Primary = TOS % Primary */
2684 static char* ops [12] = {
2685 0, "tosmoda0", "tosmodax",
2686 0, "tosumoda0", "tosumodax",
2692 /* Check if we can do some cost reduction */
2693 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2694 /* We can do that with an AND operation */
2695 g_and (flags, val - 1);
2697 /* Do it the hard way... */
2698 if (flags & CF_CONST) {
2699 /* lhs is not on stack */
2700 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2701 g_push (flags & ~CF_CONST, 0);
2703 oper (flags, val, ops);
2709 void g_or (unsigned flags, unsigned long val)
2710 /* Primary = TOS | Primary */
2712 static char* ops [12] = {
2713 0, "tosora0", "tosorax",
2714 0, "tosora0", "tosorax",
2719 /* If the right hand side is const, the lhs is not on stack but still
2720 * in the primary register.
2722 if (flags & CF_CONST) {
2724 switch (flags & CF_TYPE) {
2727 if (flags & CF_FORCECHAR) {
2728 if ((val & 0xFF) != 0xFF) {
2729 AddCodeLine ("ora #$%02X", (unsigned char)val);
2737 AddCodeLine ("ora #$%02X", (unsigned char)val);
2744 AddCodeLine ("ora #$%02X", (unsigned char)val);
2753 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2754 * into the normal, non-optimized stuff.
2756 g_push (flags & ~CF_CONST, 0);
2760 /* Use long way over the stack */
2761 oper (flags, val, ops);
2766 void g_xor (unsigned flags, unsigned long val)
2767 /* Primary = TOS ^ Primary */
2769 static char* ops [12] = {
2770 0, "tosxora0", "tosxorax",
2771 0, "tosxora0", "tosxorax",
2777 /* If the right hand side is const, the lhs is not on stack but still
2778 * in the primary register.
2780 if (flags & CF_CONST) {
2782 switch (flags & CF_TYPE) {
2785 if (flags & CF_FORCECHAR) {
2786 if ((val & 0xFF) != 0) {
2787 AddCodeLine ("eor #$%02X", (unsigned char)val);
2796 AddCodeLine ("eor #$%02X", (unsigned char)val);
2799 } else if ((val & 0xFF) == 0) {
2800 AddCodeLine ("pha");
2801 AddCodeLine ("txa");
2802 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2803 AddCodeLine ("tax");
2804 AddCodeLine ("pla");
2812 AddCodeLine ("eor #$%02X", (unsigned char)val);
2822 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2823 * into the normal, non-optimized stuff.
2825 g_push (flags & ~CF_CONST, 0);
2829 /* Use long way over the stack */
2830 oper (flags, val, ops);
2835 void g_and (unsigned flags, unsigned long val)
2836 /* Primary = TOS & Primary */
2838 static char* ops [12] = {
2839 0, "tosanda0", "tosandax",
2840 0, "tosanda0", "tosandax",
2845 /* If the right hand side is const, the lhs is not on stack but still
2846 * in the primary register.
2848 if (flags & CF_CONST) {
2850 switch (flags & CF_TYPE) {
2853 if (flags & CF_FORCECHAR) {
2854 AddCodeLine ("and #$%02X", (unsigned char)val);
2859 if ((val & 0xFFFF) != 0xFFFF) {
2864 } else if (val != 0xFF) {
2865 AddCodeLine ("and #$%02X", (unsigned char)val);
2867 } else if ((val & 0xFF00) == 0xFF00) {
2868 AddCodeLine ("and #$%02X", (unsigned char)val);
2869 } else if ((val & 0x00FF) == 0x0000) {
2870 AddCodeLine ("txa");
2871 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2872 AddCodeLine ("tax");
2875 AddCodeLine ("tay");
2876 AddCodeLine ("txa");
2877 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2878 AddCodeLine ("tax");
2879 AddCodeLine ("tya");
2880 if ((val & 0x00FF) != 0x00FF) {
2881 AddCodeLine ("and #$%02X", (unsigned char)val);
2890 AddCodeLine ("stx sreg+1");
2891 AddCodeLine ("stx sreg");
2892 if ((val & 0xFF) != 0xFF) {
2893 AddCodeLine ("and #$%02X", (unsigned char)val);
2896 } else if (val == 0xFF00) {
2898 AddCodeLine ("sta sreg+1");
2899 AddCodeLine ("sta sreg");
2908 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2909 * into the normal, non-optimized stuff.
2911 g_push (flags & ~CF_CONST, 0);
2915 /* Use long way over the stack */
2916 oper (flags, val, ops);
2921 void g_asr (unsigned flags, unsigned long val)
2922 /* Primary = TOS >> Primary */
2924 static char* ops [12] = {
2925 0, "tosasra0", "tosasrax",
2926 0, "tosshra0", "tosshrax",
2931 /* If the right hand side is const, the lhs is not on stack but still
2932 * in the primary register.
2934 if (flags & CF_CONST) {
2936 switch (flags & CF_TYPE) {
2940 if (val >= 1 && val <= 4) {
2941 if (flags & CF_UNSIGNED) {
2942 AddCodeLine ("jsr shrax%ld", val);
2944 AddCodeLine ("jsr asrax%ld", val);
2947 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2948 AddCodeLine ("txa");
2955 if (val >= 1 && val <= 4) {
2956 if (flags & CF_UNSIGNED) {
2957 AddCodeLine ("jsr shreax%ld", val);
2959 AddCodeLine ("jsr asreax%ld", val);
2962 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2963 AddCodeLine ("txa");
2964 AddCodeLine ("ldx sreg");
2965 AddCodeLine ("ldy sreg+1");
2966 AddCodeLine ("sty sreg");
2967 AddCodeLine ("ldy #$00");
2968 AddCodeLine ("sty sreg+1");
2970 } else if (val == 16) {
2971 AddCodeLine ("ldy #$00");
2972 AddCodeLine ("ldx sreg+1");
2973 if ((flags & CF_UNSIGNED) == 0) {
2974 unsigned L = GetLocalLabel();
2975 AddCodeLine ("bpl %s", LocalLabelName (L));
2976 AddCodeLine ("dey");
2979 AddCodeLine ("lda sreg");
2980 AddCodeLine ("sty sreg+1");
2981 AddCodeLine ("sty sreg");
2990 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2991 * into the normal, non-optimized stuff.
2993 g_push (flags & ~CF_CONST, 0);
2997 /* Use long way over the stack */
2998 oper (flags, val, ops);
3003 void g_asl (unsigned flags, unsigned long val)
3004 /* Primary = TOS << Primary */
3006 static char* ops [12] = {
3007 0, "tosasla0", "tosaslax",
3008 0, "tosshla0", "tosshlax",
3014 /* If the right hand side is const, the lhs is not on stack but still
3015 * in the primary register.
3017 if (flags & CF_CONST) {
3019 switch (flags & CF_TYPE) {
3023 if (val >= 1 && val <= 4) {
3024 if (flags & CF_UNSIGNED) {
3025 AddCodeLine ("jsr shlax%ld", val);
3027 AddCodeLine ("jsr aslax%ld", val);
3030 } else if (val == 8) {
3031 AddCodeLine ("tax");
3032 AddCodeLine ("lda #$00");
3038 if (val >= 1 && val <= 4) {
3039 if (flags & CF_UNSIGNED) {
3040 AddCodeLine ("jsr shleax%ld", val);
3042 AddCodeLine ("jsr asleax%ld", val);
3045 } else if (val == 8) {
3046 AddCodeLine ("ldy sreg");
3047 AddCodeLine ("sty sreg+1");
3048 AddCodeLine ("stx sreg");
3049 AddCodeLine ("tax");
3050 AddCodeLine ("lda #$00");
3052 } else if (val == 16) {
3053 AddCodeLine ("stx sreg+1");
3054 AddCodeLine ("sta sreg");
3055 AddCodeLine ("lda #$00");
3056 AddCodeLine ("tax");
3065 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3066 * into the normal, non-optimized stuff.
3068 g_push (flags & ~CF_CONST, 0);
3072 /* Use long way over the stack */
3073 oper (flags, val, ops);
3078 void g_neg (unsigned flags)
3079 /* Primary = -Primary */
3081 switch (flags & CF_TYPE) {
3085 AddCodeLine ("jsr negax");
3089 AddCodeLine ("jsr negeax");
3099 void g_bneg (unsigned flags)
3100 /* Primary = !Primary */
3102 switch (flags & CF_TYPE) {
3105 AddCodeLine ("jsr bnega");
3109 AddCodeLine ("jsr bnegax");
3113 AddCodeLine ("jsr bnegeax");
3123 void g_com (unsigned flags)
3124 /* Primary = ~Primary */
3126 switch (flags & CF_TYPE) {
3130 AddCodeLine ("jsr complax");
3134 AddCodeLine ("jsr compleax");
3144 void g_inc (unsigned flags, unsigned long val)
3145 /* Increment the primary register by a given number */
3147 /* Don't inc by zero */
3152 /* Generate code for the supported types */
3154 switch (flags & CF_TYPE) {
3157 if (flags & CF_FORCECHAR) {
3158 if (CPU == CPU_65C02 && val <= 2) {
3160 AddCodeLine ("ina");
3163 AddCodeLine ("clc");
3164 AddCodeLine ("adc #$%02X", (unsigned char)val);
3171 if (CPU == CPU_65C02 && val == 1) {
3172 unsigned L = GetLocalLabel();
3173 AddCodeLine ("ina");
3174 AddCodeLine ("bne %s", LocalLabelName (L));
3175 AddCodeLine ("inx");
3177 } else if (CodeSizeFactor < 200) {
3180 AddCodeLine ("jsr incax%lu", val);
3181 } else if (val <= 255) {
3183 AddCodeLine ("jsr incaxy");
3185 g_add (flags | CF_CONST, val);
3188 /* Inline the code */
3190 if ((val & 0xFF) != 0) {
3191 unsigned L = GetLocalLabel();
3192 AddCodeLine ("clc");
3193 AddCodeLine ("adc #$%02X", (unsigned char) val);
3194 AddCodeLine ("bcc %s", LocalLabelName (L));
3195 AddCodeLine ("inx");
3199 AddCodeLine ("inx");
3202 AddCodeLine ("inx");
3205 AddCodeLine ("clc");
3206 if ((val & 0xFF) != 0) {
3207 AddCodeLine ("adc #$%02X", (unsigned char) val);
3209 AddCodeLine ("pha");
3210 AddCodeLine ("txa");
3211 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3212 AddCodeLine ("tax");
3213 AddCodeLine ("pla");
3221 AddCodeLine ("jsr inceaxy");
3223 g_add (flags | CF_CONST, val);
3235 void g_dec (unsigned flags, unsigned long val)
3236 /* Decrement the primary register by a given number */
3238 /* Don't dec by zero */
3243 /* Generate code for the supported types */
3245 switch (flags & CF_TYPE) {
3248 if (flags & CF_FORCECHAR) {
3249 if (CPU == CPU_65C02 && val <= 2) {
3251 AddCodeLine ("dea");
3254 AddCodeLine ("sec");
3255 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3262 if (CodeSizeFactor < 200) {
3263 /* Use subroutines */
3265 AddCodeLine ("jsr decax%d", (int) val);
3266 } else if (val <= 255) {
3268 AddCodeLine ("jsr decaxy");
3270 g_sub (flags | CF_CONST, val);
3273 /* Inline the code */
3275 if ((val & 0xFF) != 0) {
3276 unsigned L = GetLocalLabel();
3277 AddCodeLine ("sec");
3278 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3279 AddCodeLine ("bcs %s", LocalLabelName (L));
3280 AddCodeLine ("dex");
3284 AddCodeLine ("dex");
3287 AddCodeLine ("dex");
3290 AddCodeLine ("sec");
3291 if ((val & 0xFF) != 0) {
3292 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3294 AddCodeLine ("pha");
3295 AddCodeLine ("txa");
3296 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3297 AddCodeLine ("tax");
3298 AddCodeLine ("pla");
3306 AddCodeLine ("jsr deceaxy");
3308 g_sub (flags | CF_CONST, val);
3321 * Following are the conditional operators. They compare the TOS against
3322 * the primary and put a literal 1 in the primary if the condition is
3323 * true, otherwise they clear the primary register
3328 void g_eq (unsigned flags, unsigned long val)
3329 /* Test for equal */
3331 static char* ops [12] = {
3332 "toseq00", "toseqa0", "toseqax",
3333 "toseq00", "toseqa0", "toseqax",
3340 /* If the right hand side is const, the lhs is not on stack but still
3341 * in the primary register.
3343 if (flags & CF_CONST) {
3345 switch (flags & CF_TYPE) {
3348 if (flags & CF_FORCECHAR) {
3349 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3350 AddCodeLine ("jsr booleq");
3356 L = GetLocalLabel();
3357 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3358 AddCodeLine ("bne %s", LocalLabelName (L));
3359 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3361 AddCodeLine ("jsr booleq");
3371 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3372 * into the normal, non-optimized stuff.
3374 g_push (flags & ~CF_CONST, 0);
3378 /* Use long way over the stack */
3379 oper (flags, val, ops);
3384 void g_ne (unsigned flags, unsigned long val)
3385 /* Test for not equal */
3387 static char* ops [12] = {
3388 "tosne00", "tosnea0", "tosneax",
3389 "tosne00", "tosnea0", "tosneax",
3396 /* If the right hand side is const, the lhs is not on stack but still
3397 * in the primary register.
3399 if (flags & CF_CONST) {
3401 switch (flags & CF_TYPE) {
3404 if (flags & CF_FORCECHAR) {
3405 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3406 AddCodeLine ("jsr boolne");
3412 L = GetLocalLabel();
3413 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3414 AddCodeLine ("bne %s", LocalLabelName (L));
3415 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3417 AddCodeLine ("jsr boolne");
3427 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3428 * into the normal, non-optimized stuff.
3430 g_push (flags & ~CF_CONST, 0);
3434 /* Use long way over the stack */
3435 oper (flags, val, ops);
3440 void g_lt (unsigned flags, unsigned long val)
3441 /* Test for less than */
3443 static char* ops [12] = {
3444 "toslt00", "toslta0", "tosltax",
3445 "tosult00", "tosulta0", "tosultax",
3450 /* If the right hand side is const, the lhs is not on stack but still
3451 * in the primary register.
3453 if (flags & CF_CONST) {
3455 /* Give a warning in some special cases */
3456 if ((flags & CF_UNSIGNED) && val == 0) {
3457 Warning ("Condition is never true");
3460 /* Look at the type */
3461 switch (flags & CF_TYPE) {
3464 if (flags & CF_FORCECHAR) {
3465 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3466 if (flags & CF_UNSIGNED) {
3467 AddCodeLine ("jsr boolult");
3469 AddCodeLine ("jsr boollt");
3476 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3477 /* If we have a signed compare against zero, we only need to
3478 * test the high byte.
3480 AddCodeLine ("txa");
3481 AddCodeLine ("jsr boollt");
3484 /* Direct code only for unsigned data types */
3485 if (flags & CF_UNSIGNED) {
3486 unsigned L = GetLocalLabel();
3487 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3488 AddCodeLine ("bne %s", LocalLabelName (L));
3489 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3491 AddCodeLine ("jsr boolult");
3497 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3498 /* If we have a signed compare against zero, we only need to
3499 * test the high byte.
3501 AddCodeLine ("lda sreg+1");
3502 AddCodeLine ("jsr boollt");
3511 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3512 * into the normal, non-optimized stuff.
3514 g_push (flags & ~CF_CONST, 0);
3518 /* Use long way over the stack */
3519 oper (flags, val, ops);
3524 void g_le (unsigned flags, unsigned long val)
3525 /* Test for less than or equal to */
3527 static char* ops [12] = {
3528 "tosle00", "toslea0", "tosleax",
3529 "tosule00", "tosulea0", "tosuleax",
3535 /* If the right hand side is const, the lhs is not on stack but still
3536 * in the primary register.
3538 if (flags & CF_CONST) {
3540 /* <= is not very effective on the 6502, so try to convert
3541 * it into < if the value is in a valid range.
3544 /* Look at the type */
3545 switch (flags & CF_TYPE) {
3548 if (flags & CF_FORCECHAR) {
3549 if (flags & CF_UNSIGNED) {
3551 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3552 AddCodeLine ("jsr boolult");
3554 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3555 AddCodeLine ("jsr boolule");
3559 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3560 AddCodeLine ("jsr boollt");
3562 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3563 AddCodeLine ("jsr boolle");
3571 if (flags & CF_UNSIGNED) {
3572 unsigned L = GetLocalLabel();
3573 const char* Name = "boolule";
3578 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3579 AddCodeLine ("bne %s", LocalLabelName (L));
3580 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3582 AddCodeLine ("jsr %s", Name);
3594 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3595 * into the normal, non-optimized stuff.
3597 g_push (flags & ~CF_CONST, 0);
3601 /* Use long way over the stack */
3602 oper (flags, val, ops);
3607 void g_gt (unsigned flags, unsigned long val)
3608 /* Test for greater than */
3610 static char* ops [12] = {
3611 "tosgt00", "tosgta0", "tosgtax",
3612 "tosugt00", "tosugta0", "tosugtax",
3618 /* If the right hand side is const, the lhs is not on stack but still
3619 * in the primary register.
3621 if (flags & CF_CONST) {
3623 /* > is not very effective on the 6502, so try to convert
3624 * it into >= if the value is in a valid range.
3627 /* Look at the type */
3628 switch (flags & CF_TYPE) {
3631 if (flags & CF_FORCECHAR) {
3632 if (flags & CF_UNSIGNED) {
3633 /* If we have a compare > 0, we will replace it by
3634 * != 0 here, since both are identical but the latter
3635 * is easier to optimize.
3638 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3639 AddCodeLine ("jsr boolne");
3640 } else if (val < 255) {
3641 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3642 AddCodeLine ("jsr booluge");
3644 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3645 AddCodeLine ("jsr boolugt");
3649 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3650 AddCodeLine ("jsr boolge");
3652 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3653 AddCodeLine ("jsr boolgt");
3661 if (flags & CF_UNSIGNED) {
3662 /* If we have a compare > 0, we will replace it by
3663 * != 0 here, since both are identical but the latter
3664 * is easier to optimize.
3667 AddCodeLine ("stx tmp1");
3668 AddCodeLine ("ora tmp1");
3669 AddCodeLine ("jsr boolne");
3671 unsigned L = GetLocalLabel();
3672 const char* Name = "boolugt";
3677 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3678 AddCodeLine ("bne %s", LocalLabelName (L));
3679 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3681 AddCodeLine ("jsr %s", Name);
3694 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3695 * into the normal, non-optimized stuff.
3697 g_push (flags & ~CF_CONST, 0);
3701 /* Use long way over the stack */
3702 oper (flags, val, ops);
3707 void g_ge (unsigned flags, unsigned long val)
3708 /* Test for greater than or equal to */
3710 static char* ops [12] = {
3711 "tosge00", "tosgea0", "tosgeax",
3712 "tosuge00", "tosugea0", "tosugeax",
3718 /* If the right hand side is const, the lhs is not on stack but still
3719 * in the primary register.
3721 if (flags & CF_CONST) {
3723 /* Give a warning in some special cases */
3724 if ((flags & CF_UNSIGNED) && val == 0) {
3725 Warning ("Condition is always true");
3728 /* Look at the type */
3729 switch (flags & CF_TYPE) {
3732 if (flags & CF_FORCECHAR) {
3733 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3734 if (flags & CF_UNSIGNED) {
3735 AddCodeLine ("jsr booluge");
3737 AddCodeLine ("jsr boolge");
3744 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3745 /* If we have a signed compare against zero, we only need to
3746 * test the high byte.
3748 AddCodeLine ("txa");
3749 AddCodeLine ("jsr boolge");
3752 /* Direct code only for unsigned data types */
3753 if (flags & CF_UNSIGNED) {
3754 unsigned L = GetLocalLabel();
3755 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3756 AddCodeLine ("bne %s", LocalLabelName (L));
3757 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3759 AddCodeLine ("jsr booluge");
3765 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3766 /* If we have a signed compare against zero, we only need to
3767 * test the high byte.
3769 AddCodeLine ("lda sreg+1");
3770 AddCodeLine ("jsr boolge");
3779 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3780 * into the normal, non-optimized stuff.
3782 g_push (flags & ~CF_CONST, 0);
3786 /* Use long way over the stack */
3787 oper (flags, val, ops);
3792 /*****************************************************************************/
3793 /* Allocating static storage */
3794 /*****************************************************************************/
3798 void g_res (unsigned n)
3799 /* Reserve static storage, n bytes */
3801 AddDataLine ("\t.res\t%u,$00", n);
3806 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3807 /* Define data with the size given in flags */
3809 if (flags & CF_CONST) {
3811 /* Numeric constant */
3812 switch (flags & CF_TYPE) {
3815 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
3819 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
3823 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3834 /* Create the correct label name */
3835 const char* Label = GetLabelName (flags, val, offs);
3837 /* Labels are always 16 bit */
3838 AddDataLine ("\t.word\t%s", Label);
3845 void g_defbytes (const void* Bytes, unsigned Count)
3846 /* Output a row of bytes as a constant */
3852 /* Cast the buffer pointer */
3853 const unsigned char* Data = (const unsigned char*) Bytes;
3855 /* Output the stuff */
3858 /* How many go into this line? */
3859 if ((Chunk = Count) > 16) {
3864 /* Output one line */
3865 strcpy (Buf, "\t.byte\t");
3868 B += sprintf (B, "$%02X", *Data++);
3874 /* Output the line */
3881 void g_zerobytes (unsigned n)
3882 /* Output n bytes of data initialized with zero */
3884 AddDataLine ("\t.res\t%u,$00", n);
3889 /*****************************************************************************/
3890 /* User supplied assembler code */
3891 /*****************************************************************************/
3895 void g_asmcode (struct StrBuf* B)
3896 /* Output one line of assembler code. */
3898 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));
3903 /*****************************************************************************/
3904 /* Inlined known functions */
3905 /*****************************************************************************/
3909 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3910 /* Inline the strlen() function */
3912 /* We need a label in both cases */
3913 unsigned label = GetLocalLabel ();
3915 /* Two different encodings */
3916 if (flags & CF_CONST) {
3918 /* The address of the string is constant. Create the correct label name */
3919 char* lbuf = GetLabelName (flags, val, offs);
3921 /* Generate the strlen code */
3922 AddCodeLine ("ldy #$FF");
3923 g_defcodelabel (label);
3924 AddCodeLine ("iny");
3925 AddCodeLine ("lda %s,y", lbuf);
3926 AddCodeLine ("bne %s", LocalLabelName (label));
3927 AddCodeLine ("tax");
3928 AddCodeLine ("tya");
3932 /* Address not constant but in primary */
3933 if (CodeSizeFactor < 400) {
3934 /* This is too much code, so call strlen instead of inlining */
3935 AddCodeLine ("jsr _strlen");
3937 /* Inline the function */
3938 AddCodeLine ("sta ptr1");
3939 AddCodeLine ("stx ptr1+1");
3940 AddCodeLine ("ldy #$FF");
3941 g_defcodelabel (label);
3942 AddCodeLine ("iny");
3943 AddCodeLine ("lda (ptr1),y");
3944 AddCodeLine ("bne %s", LocalLabelName (label));
3945 AddCodeLine ("tax");
3946 AddCodeLine ("tya");