1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
60 /*****************************************************************************/
62 /*****************************************************************************/
66 /* Compiler relative stack pointer */
71 /*****************************************************************************/
73 /*****************************************************************************/
77 static void typeerror (unsigned type)
78 /* Print an error message about an invalid operand type */
80 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
85 static void CheckLocalOffs (unsigned Offs)
86 /* Check the offset into the stack for 8bit range */
89 /* Too many local vars */
90 Error ("Too many local variables");
96 static char* GetLabelName (unsigned flags, unsigned long label, unsigned offs)
98 static char lbuf [128]; /* Label name */
100 /* Create the correct label name */
101 switch (flags & CF_ADDRMASK) {
104 /* Static memory cell */
105 sprintf (lbuf, "%s+%u", LocalLabelName (label), offs);
110 sprintf (lbuf, "_%s+%u", (char*) label, offs);
114 /* Absolute address */
115 sprintf (lbuf, "$%04X", (unsigned)((label+offs) & 0xFFFF));
119 /* Variable in register bank */
120 sprintf (lbuf, "regbank+%u", (unsigned)((label+offs) & 0xFFFF));
124 Internal ("Invalid address flags");
127 /* Return a pointer to the static buffer */
133 /*****************************************************************************/
134 /* Pre- and postamble */
135 /*****************************************************************************/
139 void g_preamble (void)
140 /* Generate the assembler code preamble */
142 /* Create a new segment list */
145 /* Identify the compiler version */
147 AddTextLine ("; File generated by cc65 v %u.%u.%u",
148 VER_MAJOR, VER_MINOR, VER_PATCH);
151 /* Insert some object file options */
152 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
153 VER_MAJOR, VER_MINOR, VER_PATCH);
155 /* If we're producing code for some other CPU, switch the command set */
156 if (CPU == CPU_65C02) {
157 AddTextLine ("\t.pc02");
160 /* Allow auto import for runtime library routines */
161 AddTextLine ("\t.autoimport\ton");
163 /* Switch the assembler into case sensitive mode */
164 AddTextLine ("\t.case\t\ton");
166 /* Tell the assembler if we want to generate debug info */
167 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
169 /* Import the stack pointer for direct auto variable access */
170 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1");
172 /* Define long branch macros */
173 AddTextLine ("\t.macpack\tlongbranch");
178 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
179 /* If debug info is enabled, place a file info into the source */
182 AddTextLine ("\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
188 /*****************************************************************************/
189 /* Segment support */
190 /*****************************************************************************/
194 void g_userodata (void)
195 /* Switch to the read only data segment */
197 UseDataSeg (SEG_RODATA);
202 void g_usedata (void)
203 /* Switch to the data segment */
205 UseDataSeg (SEG_DATA);
211 /* Switch to the bss segment */
213 UseDataSeg (SEG_BSS);
218 static void OutputDataLine (DataSeg* S, const char* Format, ...)
219 /* Add a line to the current data segment */
222 va_start (ap, Format);
223 DS_AddLine (S, Format, ap);
229 void g_segname (segment_t Seg, const char* Name)
230 /* Set the name of a segment */
234 /* Remember the new name */
235 NewSegName (Seg, Name);
237 /* Emit a segment directive for the data style segments */
239 case SEG_RODATA: S = CS->ROData; break;
240 case SEG_DATA: S = CS->Data; break;
241 case SEG_BSS: S = CS->BSS; break;
242 default: S = 0; break;
245 OutputDataLine (S, ".segment\t\"%s\"", Name);
251 /*****************************************************************************/
253 /*****************************************************************************/
257 unsigned sizeofarg (unsigned flags)
258 /* Return the size of a function argument type that is encoded in flags */
260 switch (flags & CF_TYPE) {
263 return (flags & CF_FORCECHAR)? 1 : 2;
280 int pop (unsigned flags)
281 /* Pop an argument of the given size */
283 return oursp += sizeofarg (flags);
288 int push (unsigned flags)
289 /* Push an argument of the given size */
291 return oursp -= sizeofarg (flags);
296 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
297 /* The value in Offs is an offset to an address in a/x. Make sure, an object
298 * of the type given in Flags can be loaded or stored into this address by
299 * adding part of the offset to the address in ax, so that the remaining
300 * offset fits into an index register. Return the remaining offset.
303 /* If the offset is too large for a byte register, add the high byte
304 * of the offset to the primary. Beware: We need a special correction
305 * if the offset in the low byte will overflow in the operation.
307 unsigned O = Offs & ~0xFFU;
308 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
309 /* We need to add the low byte also */
313 /* Do the correction if we need one */
315 g_inc (CF_INT | CF_CONST, O);
319 /* Return the new offset */
325 /*****************************************************************************/
326 /* Functions handling local labels */
327 /*****************************************************************************/
331 void g_defcodelabel (unsigned label)
332 /* Define a local code label */
334 CS_AddLabel (CS->Code, LocalLabelName (label));
339 void g_defdatalabel (unsigned label)
340 /* Define a local data label */
342 AddDataLine ("%s:", LocalLabelName (label));
347 /*****************************************************************************/
348 /* Functions handling global labels */
349 /*****************************************************************************/
353 void g_defgloblabel (const char* Name)
354 /* Define a global label with the given name */
356 /* Global labels are always data labels */
357 AddDataLine ("_%s:", Name);
362 void g_defexport (const char* Name, int ZP)
363 /* Export the given label */
366 AddTextLine ("\t.exportzp\t_%s", Name);
368 AddTextLine ("\t.export\t\t_%s", Name);
374 void g_defimport (const char* Name, int ZP)
375 /* Import the given label */
378 AddTextLine ("\t.importzp\t_%s", Name);
380 AddTextLine ("\t.import\t\t_%s", Name);
386 /*****************************************************************************/
387 /* Load functions for various registers */
388 /*****************************************************************************/
392 static void ldaconst (unsigned val)
393 /* Load a with a constant */
395 AddCodeLine ("lda #$%02X", val & 0xFF);
400 static void ldxconst (unsigned val)
401 /* Load x with a constant */
403 AddCodeLine ("ldx #$%02X", val & 0xFF);
408 static void ldyconst (unsigned val)
409 /* Load y with a constant */
411 AddCodeLine ("ldy #$%02X", val & 0xFF);
416 /*****************************************************************************/
417 /* Function entry and exit */
418 /*****************************************************************************/
422 /* Remember the argument size of a function. The variable is set by g_enter
423 * and used by g_leave. If the functions gets its argument size by the caller
424 * (variable param list or function without prototype), g_enter will set the
430 void g_enter (unsigned flags, unsigned argsize)
431 /* Function prologue */
433 if ((flags & CF_FIXARGC) != 0) {
434 /* Just remember the argument size for the leave */
438 AddCodeLine ("jsr enter");
445 /* Function epilogue */
447 /* How many bytes of locals do we have to drop? */
450 /* If we didn't have a variable argument list, don't call leave */
453 /* Drop stackframe if needed */
457 AddCodeLine ("jsr incsp%d", k);
461 AddCodeLine ("jsr addysp");
468 /* Nothing to drop */
469 AddCodeLine ("jsr leave");
471 /* We've a stack frame to drop */
473 AddCodeLine ("jsr leavey");
477 /* Add the final rts */
483 /*****************************************************************************/
484 /* Register variables */
485 /*****************************************************************************/
489 void g_save_regvars (int RegOffs, unsigned Bytes)
490 /* Save register variables */
492 /* Don't loop for up to two bytes */
495 AddCodeLine ("lda regbank%+d", RegOffs);
496 AddCodeLine ("jsr pusha");
498 } else if (Bytes == 2) {
500 AddCodeLine ("lda regbank%+d", RegOffs);
501 AddCodeLine ("ldx regbank%+d", RegOffs+1);
502 AddCodeLine ("jsr pushax");
506 /* More than two bytes - loop */
507 unsigned Label = GetLocalLabel ();
509 ldyconst (Bytes - 1);
511 g_defcodelabel (Label);
512 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
513 AddCodeLine ("sta (sp),y");
516 AddCodeLine ("bne %s", LocalLabelName (Label));
520 /* We pushed stuff, correct the stack pointer */
526 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
527 /* Restore register variables */
529 /* Calculate the actual stack offset and check it */
531 CheckLocalOffs (StackOffs);
533 /* Don't loop for up to two bytes */
536 ldyconst (StackOffs);
537 AddCodeLine ("lda (sp),y");
538 AddCodeLine ("sta regbank%+d", RegOffs);
540 } else if (Bytes == 2) {
542 ldyconst (StackOffs);
543 AddCodeLine ("lda (sp),y");
544 AddCodeLine ("sta regbank%+d", RegOffs);
546 AddCodeLine ("lda (sp),y");
547 AddCodeLine ("sta regbank%+d", RegOffs+1);
551 /* More than two bytes - loop */
552 unsigned Label = GetLocalLabel ();
553 ldyconst (StackOffs+Bytes-1);
555 g_defcodelabel (Label);
556 AddCodeLine ("lda (sp),y");
557 AddCodeLine ("sta regbank%+d,x", RegOffs-1);
560 AddCodeLine ("bne %s", LocalLabelName (Label));
567 /*****************************************************************************/
568 /* Fetching memory cells */
569 /*****************************************************************************/
573 void g_getimmed (unsigned Flags, unsigned long Val, unsigned Offs)
574 /* Load a constant into the primary register */
576 unsigned char B1, B2, B3, B4;
580 if ((Flags & CF_CONST) != 0) {
582 /* Numeric constant */
583 switch (Flags & CF_TYPE) {
586 if ((Flags & CF_FORCECHAR) != 0) {
592 ldxconst ((Val >> 8) & 0xFF);
593 ldaconst (Val & 0xFF);
597 /* Split the value into 4 bytes */
598 B1 = (unsigned char) (Val >> 0);
599 B2 = (unsigned char) (Val >> 8);
600 B3 = (unsigned char) (Val >> 16);
601 B4 = (unsigned char) (Val >> 24);
603 /* Remember which bytes are done */
607 AddCodeLine ("ldx #$%02X", B2);
610 AddCodeLine ("stx sreg");
614 AddCodeLine ("stx sreg+1");
617 if ((Done & 0x04) == 0 && B1 != B3) {
618 AddCodeLine ("lda #$%02X", B3);
619 AddCodeLine ("sta sreg");
622 if ((Done & 0x08) == 0 && B1 != B4) {
623 AddCodeLine ("lda #$%02X", B4);
624 AddCodeLine ("sta sreg+1");
627 AddCodeLine ("lda #$%02X", B1);
629 if ((Done & 0x04) == 0) {
631 AddCodeLine ("sta sreg");
633 if ((Done & 0x08) == 0) {
635 AddCodeLine ("sta sreg+1");
647 /* Some sort of label */
648 const char* Label = GetLabelName (Flags, Val, Offs);
650 /* Load the address into the primary */
651 AddCodeLine ("lda #<(%s)", Label);
652 AddCodeLine ("ldx #>(%s)", Label);
659 void g_getstatic (unsigned flags, unsigned long label, unsigned offs)
660 /* Fetch an static memory cell into the primary register */
662 /* Create the correct label name */
663 char* lbuf = GetLabelName (flags, label, offs);
665 /* Check the size and generate the correct load operation */
666 switch (flags & CF_TYPE) {
669 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
670 AddCodeLine ("lda %s", lbuf); /* load A from the label */
673 AddCodeLine ("lda %s", lbuf); /* load A from the label */
674 if (!(flags & CF_UNSIGNED)) {
675 /* Must sign extend */
676 unsigned L = GetLocalLabel ();
677 AddCodeLine ("bpl %s", LocalLabelName (L));
685 AddCodeLine ("lda %s", lbuf);
686 if (flags & CF_TEST) {
687 AddCodeLine ("ora %s+1", lbuf);
689 AddCodeLine ("ldx %s+1", lbuf);
694 if (flags & CF_TEST) {
695 AddCodeLine ("lda %s+3", lbuf);
696 AddCodeLine ("ora %s+2", lbuf);
697 AddCodeLine ("ora %s+1", lbuf);
698 AddCodeLine ("ora %s+0", lbuf);
700 AddCodeLine ("lda %s+3", lbuf);
701 AddCodeLine ("sta sreg+1");
702 AddCodeLine ("lda %s+2", lbuf);
703 AddCodeLine ("sta sreg");
704 AddCodeLine ("ldx %s+1", lbuf);
705 AddCodeLine ("lda %s", lbuf);
717 void g_getlocal (unsigned flags, int offs)
718 /* Fetch specified local object (local var). */
721 CheckLocalOffs (offs);
722 switch (flags & CF_TYPE) {
725 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
726 if (CPU == CPU_65C02 && offs == 0) {
727 AddCodeLine ("lda (sp)");
730 AddCodeLine ("lda (sp),y");
734 AddCodeLine ("ldx #$00");
735 AddCodeLine ("lda (sp),y");
736 if ((flags & CF_UNSIGNED) == 0) {
737 unsigned L = GetLocalLabel();
738 AddCodeLine ("bpl %s", LocalLabelName (L));
746 CheckLocalOffs (offs + 1);
747 if (flags & CF_TEST) {
749 AddCodeLine ("lda (sp),y");
751 AddCodeLine ("ora (sp),y");
753 if (CodeSizeFactor > 180) {
755 AddCodeLine ("lda (sp),y");
758 AddCodeLine ("lda (sp),y");
762 AddCodeLine ("jsr ldaxysp");
764 AddCodeLine ("jsr ldax0sp");
773 AddCodeLine ("jsr ldeaxysp");
775 AddCodeLine ("jsr ldeax0sp");
786 void g_getind (unsigned flags, unsigned offs)
787 /* Fetch the specified object type indirect through the primary register
788 * into the primary register
791 /* If the offset is greater than 255, add the part that is > 255 to
792 * the primary. This way we get an easy addition and use the low byte
795 offs = MakeByteOffs (flags, offs);
797 /* Handle the indirect fetch */
798 switch (flags & CF_TYPE) {
801 /* Character sized */
802 if (flags & CF_UNSIGNED) {
804 AddCodeLine ("jsr ldauidx");
807 AddCodeLine ("jsr ldaidx");
812 if (flags & CF_TEST) {
814 AddCodeLine ("sta ptr1");
815 AddCodeLine ("stx ptr1+1");
816 AddCodeLine ("lda (ptr1),y");
818 AddCodeLine ("ora (ptr1),y");
821 AddCodeLine ("jsr ldaxi");
824 AddCodeLine ("jsr ldaxidx");
831 AddCodeLine ("jsr ldeaxi");
834 AddCodeLine ("jsr ldeaxidx");
836 if (flags & CF_TEST) {
837 AddCodeLine ("jsr tsteax");
849 void g_leasp (int offs)
850 /* Fetch the address of the specified symbol into the primary register */
852 /* Calculate the offset relative to sp */
855 /* For value 0 we do direct code */
857 AddCodeLine ("lda sp");
858 AddCodeLine ("ldx sp+1");
860 if (CodeSizeFactor < 300) {
861 ldaconst (offs); /* Load A with offset value */
862 AddCodeLine ("jsr leaasp"); /* Load effective address */
864 unsigned L = GetLocalLabel ();
865 if (CPU == CPU_65C02 && offs == 1) {
866 AddCodeLine ("lda sp");
867 AddCodeLine ("ldx sp+1");
869 AddCodeLine ("bne %s", LocalLabelName (L));
874 AddCodeLine ("ldx sp+1");
875 AddCodeLine ("adc sp");
876 AddCodeLine ("bcc %s", LocalLabelName (L));
886 void g_leavariadic (int Offs)
887 /* Fetch the address of a parameter in a variadic function into the primary
891 unsigned ArgSizeOffs;
893 /* Calculate the offset relative to sp */
896 /* Get the offset of the parameter which is stored at sp+0 on function
897 * entry and check if this offset is reachable with a byte offset.
900 ArgSizeOffs = -oursp;
901 CheckLocalOffs (ArgSizeOffs);
903 /* Get the size of all parameters. */
904 if (ArgSizeOffs == 0 && CPU == CPU_65C02) {
905 AddCodeLine ("lda (sp)");
907 ldyconst (ArgSizeOffs);
908 AddCodeLine ("lda (sp),y");
911 /* Add the value of the stackpointer */
912 if (CodeSizeFactor > 250) {
913 unsigned L = GetLocalLabel();
914 AddCodeLine ("ldx sp+1");
916 AddCodeLine ("adc sp");
917 AddCodeLine ("bcc %s", LocalLabelName (L));
921 AddCodeLine ("jsr leaasp");
924 /* Add the offset to the primary */
926 g_inc (CF_INT | CF_CONST, Offs);
927 } else if (Offs < 0) {
928 g_dec (CF_INT | CF_CONST, -Offs);
934 /*****************************************************************************/
935 /* Store into memory */
936 /*****************************************************************************/
940 void g_putstatic (unsigned flags, unsigned long label, unsigned offs)
941 /* Store the primary register into the specified static memory cell */
943 /* Create the correct label name */
944 char* lbuf = GetLabelName (flags, label, offs);
946 /* Check the size and generate the correct store operation */
947 switch (flags & CF_TYPE) {
950 AddCodeLine ("sta %s", lbuf);
954 AddCodeLine ("sta %s", lbuf);
955 AddCodeLine ("stx %s+1", lbuf);
959 AddCodeLine ("sta %s", lbuf);
960 AddCodeLine ("stx %s+1", lbuf);
961 AddCodeLine ("ldy sreg");
962 AddCodeLine ("sty %s+2", lbuf);
963 AddCodeLine ("ldy sreg+1");
964 AddCodeLine ("sty %s+3", lbuf);
975 void g_putlocal (unsigned Flags, int Offs, long Val)
976 /* Put data into local object. */
979 CheckLocalOffs (Offs);
980 switch (Flags & CF_TYPE) {
983 if (Flags & CF_CONST) {
984 AddCodeLine ("lda #$%02X", (unsigned char) Val);
986 if (CPU == CPU_65C02 && Offs == 0) {
987 AddCodeLine ("sta (sp)");
990 AddCodeLine ("sta (sp),y");
995 if (Flags & CF_CONST) {
997 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
998 AddCodeLine ("sta (sp),y");
999 if ((Flags & CF_NOKEEP) == 0) {
1000 /* Place high byte into X */
1001 AddCodeLine ("tax");
1003 if (CPU == CPU_65C02 && Offs == 0) {
1004 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1005 AddCodeLine ("sta (sp)");
1007 if ((Val & 0xFF) == Offs+1) {
1008 /* The value we need is already in Y */
1009 AddCodeLine ("tya");
1010 AddCodeLine ("dey");
1012 AddCodeLine ("dey");
1013 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1015 AddCodeLine ("sta (sp),y");
1018 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
1021 AddCodeLine ("jsr staxysp");
1023 AddCodeLine ("jsr stax0sp");
1026 if (CPU == CPU_65C02 && Offs == 0) {
1027 AddCodeLine ("sta (sp)");
1029 AddCodeLine ("txa");
1030 AddCodeLine ("sta (sp),y");
1033 AddCodeLine ("sta (sp),y");
1034 AddCodeLine ("iny");
1035 AddCodeLine ("txa");
1036 AddCodeLine ("sta (sp),y");
1043 if (Flags & CF_CONST) {
1044 g_getimmed (Flags, Val, 0);
1048 AddCodeLine ("jsr steaxysp");
1050 AddCodeLine ("jsr steax0sp");
1062 void g_putind (unsigned Flags, unsigned Offs)
1063 /* Store the specified object type in the primary register at the address
1064 * on the top of the stack
1067 /* We can handle offsets below $100 directly, larger offsets must be added
1068 * to the address. Since a/x is in use, best code is achieved by adding
1069 * just the high byte. Be sure to check if the low byte will overflow while
1072 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1074 /* Overflow - we need to add the low byte also */
1075 AddCodeLine ("ldy #$00");
1076 AddCodeLine ("clc");
1077 AddCodeLine ("pha");
1078 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1079 AddCodeLine ("adc (sp),y");
1080 AddCodeLine ("sta (sp),y");
1081 AddCodeLine ("iny");
1082 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1083 AddCodeLine ("adc (sp),y");
1084 AddCodeLine ("sta (sp),y");
1085 AddCodeLine ("pla");
1087 /* Complete address is on stack, new offset is zero */
1090 } else if ((Offs & 0xFF00) != 0) {
1092 /* We can just add the high byte */
1093 AddCodeLine ("ldy #$01");
1094 AddCodeLine ("clc");
1095 AddCodeLine ("pha");
1096 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1097 AddCodeLine ("adc (sp),y");
1098 AddCodeLine ("sta (sp),y");
1099 AddCodeLine ("pla");
1101 /* Offset is now just the low byte */
1105 /* Check the size and determine operation */
1106 switch (Flags & CF_TYPE) {
1110 AddCodeLine ("jsr staspidx");
1116 AddCodeLine ("jsr staxspidx");
1118 AddCodeLine ("jsr staxspp");
1125 AddCodeLine ("jsr steaxspidx");
1127 AddCodeLine ("jsr steaxspp");
1136 /* Pop the argument which is always a pointer */
1142 /*****************************************************************************/
1143 /* type conversion and similiar stuff */
1144 /*****************************************************************************/
1148 void g_toslong (unsigned flags)
1149 /* Make sure, the value on TOS is a long. Convert if necessary */
1151 switch (flags & CF_TYPE) {
1155 if (flags & CF_UNSIGNED) {
1156 AddCodeLine ("jsr tosulong");
1158 AddCodeLine ("jsr toslong");
1173 void g_tosint (unsigned flags)
1174 /* Make sure, the value on TOS is an int. Convert if necessary */
1176 switch (flags & CF_TYPE) {
1183 AddCodeLine ("jsr tosint");
1194 void g_reglong (unsigned flags)
1195 /* Make sure, the value in the primary register a long. Convert if necessary */
1197 switch (flags & CF_TYPE) {
1201 if (flags & CF_UNSIGNED) {
1202 if (CodeSizeFactor >= 200) {
1204 AddCodeLine ("sty sreg");
1205 AddCodeLine ("sty sreg+1");
1207 AddCodeLine ("jsr axulong");
1210 AddCodeLine ("jsr axlong");
1224 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1225 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1226 * value, that corresponds to the value on TOS, rhs corresponds to the value
1227 * in (e)ax. The return value is the the flags value for the resulting type.
1230 unsigned ltype, rtype;
1233 /* Get the type spec from the flags */
1234 ltype = lhs & CF_TYPE;
1235 rtype = rhs & CF_TYPE;
1237 /* Check if a conversion is needed */
1238 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1239 /* We must promote the primary register to long */
1241 /* Get the new rhs type */
1242 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1244 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1245 /* We must promote the lhs to long */
1251 /* Get the new rhs type */
1252 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1256 /* Determine the result type for the operation:
1257 * - The result is const if both operands are const.
1258 * - The result is unsigned if one of the operands is unsigned.
1259 * - The result is long if one of the operands is long.
1260 * - Otherwise the result is int sized.
1262 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1263 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1264 if (rtype == CF_LONG || ltype == CF_LONG) {
1274 unsigned g_typecast (unsigned lhs, unsigned rhs)
1275 /* Cast the value in the primary register to the operand size that is flagged
1276 * by the lhs value. Return the result value.
1279 unsigned ltype, rtype;
1281 /* Get the type spec from the flags */
1282 ltype = lhs & CF_TYPE;
1283 rtype = rhs & CF_TYPE;
1285 /* Check if a conversion is needed */
1286 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1287 /* We must promote the primary register to long */
1291 /* Do not need any other action. If the left type is int, and the primary
1292 * register is long, it will be automagically truncated. If the right hand
1293 * side is const, it is not located in the primary register and handled by
1294 * the expression parser code.
1297 /* Result is const if the right hand side was const */
1298 lhs |= (rhs & CF_CONST);
1300 /* The resulting type is that of the left hand side (that's why you called
1308 void g_scale (unsigned flags, long val)
1309 /* Scale the value in the primary register by the given value. If val is positive,
1310 * scale up, is val is negative, scale down. This function is used to scale
1311 * the operands or results of pointer arithmetic by the size of the type, the
1312 * pointer points to.
1317 /* Value may not be zero */
1319 Internal ("Data type has no size");
1320 } else if (val > 0) {
1323 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1325 /* Factor is 2, 4, 8 and 16, use special function */
1326 switch (flags & CF_TYPE) {
1329 if (flags & CF_FORCECHAR) {
1331 AddCodeLine ("asl a");
1338 if (CodeSizeFactor >= (p2+1)*130U) {
1339 AddCodeLine ("stx tmp1");
1341 AddCodeLine ("asl a");
1342 AddCodeLine ("rol tmp1");
1344 AddCodeLine ("ldx tmp1");
1346 if (flags & CF_UNSIGNED) {
1347 AddCodeLine ("jsr shlax%d", p2);
1349 AddCodeLine ("jsr aslax%d", p2);
1355 if (flags & CF_UNSIGNED) {
1356 AddCodeLine ("jsr shleax%d", p2);
1358 AddCodeLine ("jsr asleax%d", p2);
1367 } else if (val != 1) {
1369 /* Use a multiplication instead */
1370 g_mul (flags | CF_CONST, val);
1378 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1380 /* Factor is 2, 4, 8 and 16 use special function */
1381 switch (flags & CF_TYPE) {
1384 if (flags & CF_FORCECHAR) {
1385 if (flags & CF_UNSIGNED) {
1387 AddCodeLine ("lsr a");
1390 } else if (p2 <= 2) {
1391 AddCodeLine ("cmp #$80");
1392 AddCodeLine ("ror a");
1399 if (flags & CF_UNSIGNED) {
1400 if (CodeSizeFactor >= (p2+1)*130U) {
1401 AddCodeLine ("stx tmp1");
1403 AddCodeLine ("lsr tmp1");
1404 AddCodeLine ("ror a");
1406 AddCodeLine ("ldx tmp1");
1408 AddCodeLine ("jsr lsrax%d", p2);
1411 if (CodeSizeFactor >= (p2+1)*150U) {
1412 AddCodeLine ("stx tmp1");
1414 AddCodeLine ("cpx #$80");
1415 AddCodeLine ("ror tmp1");
1416 AddCodeLine ("ror a");
1418 AddCodeLine ("ldx tmp1");
1420 AddCodeLine ("jsr asrax%d", p2);
1426 if (flags & CF_UNSIGNED) {
1427 AddCodeLine ("jsr lsreax%d", p2);
1429 AddCodeLine ("jsr asreax%d", p2);
1438 } else if (val != 1) {
1440 /* Use a division instead */
1441 g_div (flags | CF_CONST, val);
1449 /*****************************************************************************/
1450 /* Adds and subs of variables fix a fixed address */
1451 /*****************************************************************************/
1455 void g_addlocal (unsigned flags, int offs)
1456 /* Add a local variable to ax */
1460 /* Correct the offset and check it */
1462 CheckLocalOffs (offs);
1464 switch (flags & CF_TYPE) {
1467 L = GetLocalLabel();
1468 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1469 AddCodeLine ("clc");
1470 AddCodeLine ("adc (sp),y");
1471 AddCodeLine ("bcc %s", LocalLabelName (L));
1472 AddCodeLine ("inx");
1477 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1478 AddCodeLine ("clc");
1479 AddCodeLine ("adc (sp),y");
1480 AddCodeLine ("pha");
1481 AddCodeLine ("txa");
1482 AddCodeLine ("iny");
1483 AddCodeLine ("adc (sp),y");
1484 AddCodeLine ("tax");
1485 AddCodeLine ("pla");
1489 /* Do it the old way */
1491 g_getlocal (flags, offs);
1503 void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
1504 /* Add a static variable to ax */
1508 /* Create the correct label name */
1509 char* lbuf = GetLabelName (flags, label, offs);
1511 switch (flags & CF_TYPE) {
1514 L = GetLocalLabel();
1515 AddCodeLine ("clc");
1516 AddCodeLine ("adc %s", lbuf);
1517 AddCodeLine ("bcc %s", LocalLabelName (L));
1518 AddCodeLine ("inx");
1523 AddCodeLine ("clc");
1524 AddCodeLine ("adc %s", lbuf);
1525 AddCodeLine ("tay");
1526 AddCodeLine ("txa");
1527 AddCodeLine ("adc %s+1", lbuf);
1528 AddCodeLine ("tax");
1529 AddCodeLine ("tya");
1533 /* Do it the old way */
1535 g_getstatic (flags, label, offs);
1547 /*****************************************************************************/
1548 /* Compares of ax with a variable with fixed address */
1549 /*****************************************************************************/
1553 void g_cmplocal (unsigned flags, int offs)
1554 /* Compare a local variable to ax */
1556 Internal ("g_cmplocal not implemented");
1561 void g_cmpstatic (unsigned flags, unsigned label, unsigned offs)
1562 /* Compare a static variable to ax */
1564 Internal ("g_cmpstatic not implemented");
1569 /*****************************************************************************/
1570 /* Special op= functions */
1571 /*****************************************************************************/
1575 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
1577 /* Emit += for a static variable */
1579 /* Create the correct label name */
1580 char* lbuf = GetLabelName (flags, label, offs);
1582 /* Check the size and determine operation */
1583 switch (flags & CF_TYPE) {
1586 if (flags & CF_FORCECHAR) {
1587 AddCodeLine ("ldx #$00");
1588 if (flags & CF_CONST) {
1590 AddCodeLine ("inc %s", lbuf);
1591 AddCodeLine ("lda %s", lbuf);
1593 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1594 AddCodeLine ("clc");
1595 AddCodeLine ("adc %s", lbuf);
1596 AddCodeLine ("sta %s", lbuf);
1599 AddCodeLine ("clc");
1600 AddCodeLine ("adc %s", lbuf);
1601 AddCodeLine ("sta %s", lbuf);
1603 if ((flags & CF_UNSIGNED) == 0) {
1604 unsigned L = GetLocalLabel();
1605 AddCodeLine ("bpl %s", LocalLabelName (L));
1606 AddCodeLine ("dex");
1614 if (flags & CF_CONST) {
1616 unsigned L = GetLocalLabel ();
1617 AddCodeLine ("inc %s", lbuf);
1618 AddCodeLine ("bne %s", LocalLabelName (L));
1619 AddCodeLine ("inc %s+1", lbuf);
1621 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1622 AddCodeLine ("ldx %s+1", lbuf);
1624 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1625 AddCodeLine ("clc");
1626 AddCodeLine ("adc %s", lbuf);
1627 AddCodeLine ("sta %s", lbuf);
1629 unsigned L = GetLocalLabel ();
1630 AddCodeLine ("bcc %s", LocalLabelName (L));
1631 AddCodeLine ("inc %s+1", lbuf);
1633 AddCodeLine ("ldx %s+1", lbuf);
1635 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1636 AddCodeLine ("adc %s+1", lbuf);
1637 AddCodeLine ("sta %s+1", lbuf);
1638 AddCodeLine ("tax");
1639 AddCodeLine ("lda %s", lbuf);
1643 AddCodeLine ("clc");
1644 AddCodeLine ("adc %s", lbuf);
1645 AddCodeLine ("sta %s", lbuf);
1646 AddCodeLine ("txa");
1647 AddCodeLine ("adc %s+1", lbuf);
1648 AddCodeLine ("sta %s+1", lbuf);
1649 AddCodeLine ("tax");
1650 AddCodeLine ("lda %s", lbuf);
1655 if (flags & CF_CONST) {
1657 AddCodeLine ("ldy #<(%s)", lbuf);
1658 AddCodeLine ("sty ptr1");
1659 AddCodeLine ("ldy #>(%s+1)", lbuf);
1661 AddCodeLine ("jsr laddeq1");
1663 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1664 AddCodeLine ("jsr laddeqa");
1667 g_getstatic (flags, label, offs);
1669 g_putstatic (flags, label, offs);
1672 AddCodeLine ("ldy #<(%s)", lbuf);
1673 AddCodeLine ("sty ptr1");
1674 AddCodeLine ("ldy #>(%s+1)", lbuf);
1675 AddCodeLine ("jsr laddeq");
1686 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1687 /* Emit += for a local variable */
1689 /* Calculate the true offset, check it, load it into Y */
1691 CheckLocalOffs (offs);
1693 /* Check the size and determine operation */
1694 switch (flags & CF_TYPE) {
1697 if (flags & CF_FORCECHAR) {
1699 AddCodeLine ("ldx #$00");
1700 if (flags & CF_CONST) {
1701 AddCodeLine ("clc");
1702 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1703 AddCodeLine ("adc (sp),y");
1704 AddCodeLine ("sta (sp),y");
1706 AddCodeLine ("clc");
1707 AddCodeLine ("adc (sp),y");
1708 AddCodeLine ("sta (sp),y");
1710 if ((flags & CF_UNSIGNED) == 0) {
1711 unsigned L = GetLocalLabel();
1712 AddCodeLine ("bpl %s", LocalLabelName (L));
1713 AddCodeLine ("dex");
1721 if (flags & CF_CONST) {
1722 g_getimmed (flags, val, 0);
1725 AddCodeLine ("jsr addeq0sp");
1728 AddCodeLine ("jsr addeqysp");
1733 if (flags & CF_CONST) {
1734 g_getimmed (flags, val, 0);
1737 AddCodeLine ("jsr laddeq0sp");
1740 AddCodeLine ("jsr laddeqysp");
1751 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1752 /* Emit += for the location with address in ax */
1754 /* If the offset is too large for a byte register, add the high byte
1755 * of the offset to the primary. Beware: We need a special correction
1756 * if the offset in the low byte will overflow in the operation.
1758 offs = MakeByteOffs (flags, offs);
1760 /* Check the size and determine operation */
1761 switch (flags & CF_TYPE) {
1764 AddCodeLine ("sta ptr1");
1765 AddCodeLine ("stx ptr1+1");
1766 AddCodeLine ("ldy #$%02X", offs);
1767 AddCodeLine ("ldx #$00");
1768 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1769 AddCodeLine ("clc");
1770 AddCodeLine ("adc (ptr1),y");
1771 AddCodeLine ("sta (ptr1),y");
1775 if (CodeSizeFactor >= 200) {
1776 /* Lots of code, use only if size is not important */
1777 AddCodeLine ("sta ptr1");
1778 AddCodeLine ("stx ptr1+1");
1779 AddCodeLine ("ldy #$%02X", offs);
1780 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1781 AddCodeLine ("clc");
1782 AddCodeLine ("adc (ptr1),y");
1783 AddCodeLine ("sta (ptr1),y");
1784 AddCodeLine ("pha");
1785 AddCodeLine ("iny");
1786 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1787 AddCodeLine ("adc (ptr1),y");
1788 AddCodeLine ("sta (ptr1),y");
1789 AddCodeLine ("tax");
1790 AddCodeLine ("pla");
1796 AddCodeLine ("jsr pushax"); /* Push the address */
1797 push (flags); /* Correct the internal sp */
1798 g_getind (flags, offs); /* Fetch the value */
1799 g_inc (flags, val); /* Increment value in primary */
1800 g_putind (flags, offs); /* Store the value back */
1810 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1812 /* Emit -= for a static variable */
1814 /* Create the correct label name */
1815 char* lbuf = GetLabelName (flags, label, offs);
1817 /* Check the size and determine operation */
1818 switch (flags & CF_TYPE) {
1821 if (flags & CF_FORCECHAR) {
1822 AddCodeLine ("ldx #$00");
1823 if (flags & CF_CONST) {
1825 AddCodeLine ("dec %s", lbuf);
1826 AddCodeLine ("lda %s", lbuf);
1828 AddCodeLine ("sec");
1829 AddCodeLine ("lda %s", lbuf);
1830 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1831 AddCodeLine ("sta %s", lbuf);
1834 AddCodeLine ("sec");
1835 AddCodeLine ("sta tmp1");
1836 AddCodeLine ("lda %s", lbuf);
1837 AddCodeLine ("sbc tmp1");
1838 AddCodeLine ("sta %s", lbuf);
1840 if ((flags & CF_UNSIGNED) == 0) {
1841 unsigned L = GetLocalLabel();
1842 AddCodeLine ("bpl %s", LocalLabelName (L));
1843 AddCodeLine ("dex");
1851 AddCodeLine ("sec");
1852 if (flags & CF_CONST) {
1853 AddCodeLine ("lda %s", lbuf);
1854 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1855 AddCodeLine ("sta %s", lbuf);
1857 unsigned L = GetLocalLabel ();
1858 AddCodeLine ("bcs %s", LocalLabelName (L));
1859 AddCodeLine ("dec %s+1", lbuf);
1861 AddCodeLine ("ldx %s+1", lbuf);
1863 AddCodeLine ("lda %s+1", lbuf);
1864 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1865 AddCodeLine ("sta %s+1", lbuf);
1866 AddCodeLine ("tax");
1867 AddCodeLine ("lda %s", lbuf);
1870 AddCodeLine ("sta tmp1");
1871 AddCodeLine ("lda %s", lbuf);
1872 AddCodeLine ("sbc tmp1");
1873 AddCodeLine ("sta %s", lbuf);
1874 AddCodeLine ("stx tmp1");
1875 AddCodeLine ("lda %s+1", lbuf);
1876 AddCodeLine ("sbc tmp1");
1877 AddCodeLine ("sta %s+1", lbuf);
1878 AddCodeLine ("tax");
1879 AddCodeLine ("lda %s", lbuf);
1884 if (flags & CF_CONST) {
1886 AddCodeLine ("ldy #<(%s)", lbuf);
1887 AddCodeLine ("sty ptr1");
1888 AddCodeLine ("ldy #>(%s+1)", lbuf);
1890 AddCodeLine ("jsr lsubeq1");
1892 AddCodeLine ("lda #$%02X", (unsigned char)val);
1893 AddCodeLine ("jsr lsubeqa");
1896 g_getstatic (flags, label, offs);
1898 g_putstatic (flags, label, offs);
1901 AddCodeLine ("ldy #<(%s)", lbuf);
1902 AddCodeLine ("sty ptr1");
1903 AddCodeLine ("ldy #>(%s+1)", lbuf);
1904 AddCodeLine ("jsr lsubeq");
1915 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1916 /* Emit -= for a local variable */
1918 /* Calculate the true offset, check it, load it into Y */
1920 CheckLocalOffs (offs);
1922 /* Check the size and determine operation */
1923 switch (flags & CF_TYPE) {
1926 if (flags & CF_FORCECHAR) {
1928 AddCodeLine ("ldx #$00");
1929 AddCodeLine ("sec");
1930 if (flags & CF_CONST) {
1931 AddCodeLine ("lda (sp),y");
1932 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1934 AddCodeLine ("sta tmp1");
1935 AddCodeLine ("lda (sp),y");
1936 AddCodeLine ("sbc tmp1");
1938 AddCodeLine ("sta (sp),y");
1939 if ((flags & CF_UNSIGNED) == 0) {
1940 unsigned L = GetLocalLabel();
1941 AddCodeLine ("bpl %s", LocalLabelName (L));
1942 AddCodeLine ("dex");
1950 if (flags & CF_CONST) {
1951 g_getimmed (flags, val, 0);
1954 AddCodeLine ("jsr subeq0sp");
1957 AddCodeLine ("jsr subeqysp");
1962 if (flags & CF_CONST) {
1963 g_getimmed (flags, val, 0);
1966 AddCodeLine ("jsr lsubeq0sp");
1969 AddCodeLine ("jsr lsubeqysp");
1980 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
1981 /* Emit -= for the location with address in ax */
1983 /* If the offset is too large for a byte register, add the high byte
1984 * of the offset to the primary. Beware: We need a special correction
1985 * if the offset in the low byte will overflow in the operation.
1987 offs = MakeByteOffs (flags, offs);
1989 /* Check the size and determine operation */
1990 switch (flags & CF_TYPE) {
1993 AddCodeLine ("sta ptr1");
1994 AddCodeLine ("stx ptr1+1");
1995 AddCodeLine ("ldy #$%02X", offs);
1996 AddCodeLine ("ldx #$00");
1997 AddCodeLine ("lda (ptr1),y");
1998 AddCodeLine ("sec");
1999 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2000 AddCodeLine ("sta (ptr1),y");
2004 if (CodeSizeFactor >= 200) {
2005 /* Lots of code, use only if size is not important */
2006 AddCodeLine ("sta ptr1");
2007 AddCodeLine ("stx ptr1+1");
2008 AddCodeLine ("ldy #$%02X", offs);
2009 AddCodeLine ("lda (ptr1),y");
2010 AddCodeLine ("sec");
2011 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2012 AddCodeLine ("sta (ptr1),y");
2013 AddCodeLine ("pha");
2014 AddCodeLine ("iny");
2015 AddCodeLine ("lda (ptr1),y");
2016 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2017 AddCodeLine ("sta (ptr1),y");
2018 AddCodeLine ("tax");
2019 AddCodeLine ("pla");
2025 AddCodeLine ("jsr pushax"); /* Push the address */
2026 push (flags); /* Correct the internal sp */
2027 g_getind (flags, offs); /* Fetch the value */
2028 g_dec (flags, val); /* Increment value in primary */
2029 g_putind (flags, offs); /* Store the value back */
2039 /*****************************************************************************/
2040 /* Add a variable address to the value in ax */
2041 /*****************************************************************************/
2045 void g_addaddr_local (unsigned flags, int offs)
2046 /* Add the address of a local variable to ax */
2050 /* Add the offset */
2053 /* We cannot address more then 256 bytes of locals anyway */
2054 L = GetLocalLabel();
2055 CheckLocalOffs (offs);
2056 AddCodeLine ("clc");
2057 AddCodeLine ("adc #$%02X", offs & 0xFF);
2058 /* Do also skip the CLC insn below */
2059 AddCodeLine ("bcc %s", LocalLabelName (L));
2060 AddCodeLine ("inx");
2063 /* Add the current stackpointer value */
2064 AddCodeLine ("clc");
2066 /* Label was used above */
2069 AddCodeLine ("adc sp");
2070 AddCodeLine ("tay");
2071 AddCodeLine ("txa");
2072 AddCodeLine ("adc sp+1");
2073 AddCodeLine ("tax");
2074 AddCodeLine ("tya");
2079 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2080 /* Add the address of a static variable to ax */
2082 /* Create the correct label name */
2083 char* lbuf = GetLabelName (flags, label, offs);
2085 /* Add the address to the current ax value */
2086 AddCodeLine ("clc");
2087 AddCodeLine ("adc #<(%s)", lbuf);
2088 AddCodeLine ("tay");
2089 AddCodeLine ("txa");
2090 AddCodeLine ("adc #>(%s)", lbuf);
2091 AddCodeLine ("tax");
2092 AddCodeLine ("tya");
2097 /*****************************************************************************/
2099 /*****************************************************************************/
2103 void g_save (unsigned flags)
2104 /* Copy primary register to hold register. */
2106 /* Check the size and determine operation */
2107 switch (flags & CF_TYPE) {
2110 if (flags & CF_FORCECHAR) {
2111 AddCodeLine ("pha");
2117 AddCodeLine ("sta regsave");
2118 AddCodeLine ("stx regsave+1");
2122 AddCodeLine ("jsr saveeax");
2132 void g_restore (unsigned flags)
2133 /* Copy hold register to primary. */
2135 /* Check the size and determine operation */
2136 switch (flags & CF_TYPE) {
2139 if (flags & CF_FORCECHAR) {
2140 AddCodeLine ("pla");
2146 AddCodeLine ("lda regsave");
2147 AddCodeLine ("ldx regsave+1");
2151 AddCodeLine ("jsr resteax");
2161 void g_cmp (unsigned flags, unsigned long val)
2162 /* Immidiate compare. The primary register will not be changed, Z flag
2168 /* Check the size and determine operation */
2169 switch (flags & CF_TYPE) {
2172 if (flags & CF_FORCECHAR) {
2173 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2179 L = GetLocalLabel();
2180 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2181 AddCodeLine ("bne %s", LocalLabelName (L));
2182 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2187 Internal ("g_cmp: Long compares not implemented");
2197 static void oper (unsigned flags, unsigned long val, char** subs)
2198 /* Encode a binary operation. subs is a pointer to four groups of three
2200 * 0-2 --> Operate on ints
2201 * 3-5 --> Operate on unsigneds
2202 * 6-8 --> Operate on longs
2203 * 9-11 --> Operate on unsigned longs
2205 * The first subroutine names in each string group is used to encode an
2206 * operation with a zero constant, the second to encode an operation with
2207 * a 8 bit constant, and the third is used in all other cases.
2212 /* Determine the offset into the array */
2213 offs = (flags & CF_UNSIGNED)? 3 : 0;
2214 switch (flags & CF_TYPE) {
2227 /* Encode the operation */
2228 if (flags & CF_CONST) {
2229 /* Constant value given */
2230 if (val == 0 && subs [offs+0]) {
2231 /* Special case: constant with value zero */
2232 AddCodeLine ("jsr %s", subs [offs+0]);
2233 } else if (val < 0x100 && subs [offs+1]) {
2234 /* Special case: constant with high byte zero */
2235 ldaconst (val); /* Load low byte */
2236 AddCodeLine ("jsr %s", subs [offs+1]);
2238 /* Others: arbitrary constant value */
2239 g_getimmed (flags, val, 0); /* Load value */
2240 AddCodeLine ("jsr %s", subs [offs+2]);
2243 /* Value not constant (is already in (e)ax) */
2244 AddCodeLine ("jsr %s", subs [offs+2]);
2247 /* The operation will pop it's argument */
2253 void g_test (unsigned flags)
2254 /* Test the value in the primary and set the condition codes */
2256 switch (flags & CF_TYPE) {
2259 if (flags & CF_FORCECHAR) {
2260 AddCodeLine ("tax");
2266 AddCodeLine ("stx tmp1");
2267 AddCodeLine ("ora tmp1");
2271 if (flags & CF_UNSIGNED) {
2272 AddCodeLine ("jsr utsteax");
2274 AddCodeLine ("jsr tsteax");
2286 void g_push (unsigned flags, unsigned long val)
2287 /* Push the primary register or a constant value onto the stack */
2291 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2293 /* We have a constant 8 or 16 bit value */
2294 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2296 /* Handle as 8 bit value */
2297 if (CodeSizeFactor >= 165 || val > 2) {
2299 AddCodeLine ("jsr pusha");
2301 AddCodeLine ("jsr pushc%d", (int) val);
2306 /* Handle as 16 bit value */
2307 hi = (unsigned char) (val >> 8);
2309 AddCodeLine ("jsr push%u", (unsigned) val);
2310 } else if (hi == 0 || hi == 0xFF) {
2311 /* Use special function */
2313 AddCodeLine ("jsr %s", (hi == 0)? "pusha0" : "pushaFF");
2316 g_getimmed (flags, val, 0);
2317 AddCodeLine ("jsr pushax");
2323 /* Value is not 16 bit or not constant */
2324 if (flags & CF_CONST) {
2325 /* Constant 32 bit value, load into eax */
2326 g_getimmed (flags, val, 0);
2329 /* Push the primary register */
2330 switch (flags & CF_TYPE) {
2333 if (flags & CF_FORCECHAR) {
2334 /* Handle as char */
2335 AddCodeLine ("jsr pusha");
2340 AddCodeLine ("jsr pushax");
2344 AddCodeLine ("jsr pusheax");
2354 /* Adjust the stack offset */
2360 void g_swap (unsigned flags)
2361 /* Swap the primary register and the top of the stack. flags give the type
2362 * of *both* values (must have same size).
2365 switch (flags & CF_TYPE) {
2369 AddCodeLine ("jsr swapstk");
2373 AddCodeLine ("jsr swapestk");
2384 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2385 /* Call the specified subroutine name */
2387 if ((Flags & CF_FIXARGC) == 0) {
2388 /* Pass the argument count */
2391 AddCodeLine ("jsr _%s", Label);
2392 oursp += ArgSize; /* callee pops args */
2397 void g_callind (unsigned Flags, unsigned ArgSize)
2398 /* Call subroutine with address in AX */
2400 if ((Flags & CF_FIXARGC) == 0) {
2401 /* Pass arg count */
2404 AddCodeLine ("jsr callax"); /* do the call */
2405 oursp += ArgSize; /* callee pops args */
2410 void g_jump (unsigned Label)
2411 /* Jump to specified internal label number */
2413 AddCodeLine ("jmp %s", LocalLabelName (Label));
2418 void g_switch (unsigned Flags)
2419 /* Output switch statement preamble */
2421 switch (Flags & CF_TYPE) {
2425 AddCodeLine ("jsr switch");
2429 AddCodeLine ("jsr lswitch");
2440 void g_case (unsigned flags, unsigned label, unsigned long val)
2441 /* Create table code for one case selector */
2443 switch (flags & CF_TYPE) {
2447 AddCodeLine (".word $%04X, %s",
2448 (unsigned)(val & 0xFFFF),
2449 LocalLabelName (label));
2453 AddCodeLine (".dword $%08lX", val);
2454 AddCodeLine (".word %s", LocalLabelName (label));
2465 void g_truejump (unsigned flags, unsigned label)
2466 /* Jump to label if zero flag clear */
2468 AddCodeLine ("jne %s", LocalLabelName (label));
2473 void g_falsejump (unsigned flags, unsigned label)
2474 /* Jump to label if zero flag set */
2476 AddCodeLine ("jeq %s", LocalLabelName (label));
2481 static void mod_internal (int k, char* verb1, char* verb2)
2484 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2488 AddCodeLine ("jsr %ssp", verb2);
2494 void g_space (int space)
2495 /* Create or drop space on the stack */
2498 mod_internal (-space, "inc", "addy");
2499 } else if (space > 0) {
2500 mod_internal (space, "dec", "suby");
2506 void g_cstackcheck (void)
2507 /* Check for a C stack overflow */
2509 AddCodeLine ("jsr cstkchk");
2514 void g_stackcheck (void)
2515 /* Check for a stack overflow */
2517 AddCodeLine ("jsr stkchk");
2522 void g_add (unsigned flags, unsigned long val)
2523 /* Primary = TOS + Primary */
2525 static char* ops [12] = {
2526 0, "tosadda0", "tosaddax",
2527 0, "tosadda0", "tosaddax",
2532 if (flags & CF_CONST) {
2533 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2534 g_push (flags & ~CF_CONST, 0);
2536 oper (flags, val, ops);
2541 void g_sub (unsigned flags, unsigned long val)
2542 /* Primary = TOS - Primary */
2544 static char* ops [12] = {
2545 0, "tossuba0", "tossubax",
2546 0, "tossuba0", "tossubax",
2551 if (flags & CF_CONST) {
2552 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2553 g_push (flags & ~CF_CONST, 0);
2555 oper (flags, val, ops);
2560 void g_rsub (unsigned flags, unsigned long val)
2561 /* Primary = Primary - TOS */
2563 static char* ops [12] = {
2564 0, "tosrsuba0", "tosrsubax",
2565 0, "tosrsuba0", "tosrsubax",
2569 oper (flags, val, ops);
2574 void g_mul (unsigned flags, unsigned long val)
2575 /* Primary = TOS * Primary */
2577 static char* ops [12] = {
2578 0, "tosmula0", "tosmulax",
2579 0, "tosumula0", "tosumulax",
2586 /* Do strength reduction if the value is constant and a power of two */
2587 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2588 /* Generate a shift instead */
2593 /* If the right hand side is const, the lhs is not on stack but still
2594 * in the primary register.
2596 if (flags & CF_CONST) {
2598 switch (flags & CF_TYPE) {
2601 if (flags & CF_FORCECHAR) {
2602 /* Handle some special cases */
2606 AddCodeLine ("sta tmp1");
2607 AddCodeLine ("asl a");
2608 AddCodeLine ("clc");
2609 AddCodeLine ("adc tmp1");
2613 AddCodeLine ("sta tmp1");
2614 AddCodeLine ("asl a");
2615 AddCodeLine ("asl a");
2616 AddCodeLine ("clc");
2617 AddCodeLine ("adc tmp1");
2621 AddCodeLine ("sta tmp1");
2622 AddCodeLine ("asl a");
2623 AddCodeLine ("asl a");
2624 AddCodeLine ("clc");
2625 AddCodeLine ("adc tmp1");
2626 AddCodeLine ("asl a");
2642 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2643 * into the normal, non-optimized stuff.
2645 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2646 g_push (flags & ~CF_CONST, 0);
2650 /* Use long way over the stack */
2651 oper (flags, val, ops);
2656 void g_div (unsigned flags, unsigned long val)
2657 /* Primary = TOS / Primary */
2659 static char* ops [12] = {
2660 0, "tosdiva0", "tosdivax",
2661 0, "tosudiva0", "tosudivax",
2666 /* Do strength reduction if the value is constant and a power of two */
2668 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2669 /* Generate a shift instead */
2672 /* Generate a division */
2673 if (flags & CF_CONST) {
2674 /* lhs is not on stack */
2675 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2676 g_push (flags & ~CF_CONST, 0);
2678 oper (flags, val, ops);
2684 void g_mod (unsigned flags, unsigned long val)
2685 /* Primary = TOS % Primary */
2687 static char* ops [12] = {
2688 0, "tosmoda0", "tosmodax",
2689 0, "tosumoda0", "tosumodax",
2695 /* Check if we can do some cost reduction */
2696 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2697 /* We can do that with an AND operation */
2698 g_and (flags, val - 1);
2700 /* Do it the hard way... */
2701 if (flags & CF_CONST) {
2702 /* lhs is not on stack */
2703 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2704 g_push (flags & ~CF_CONST, 0);
2706 oper (flags, val, ops);
2712 void g_or (unsigned flags, unsigned long val)
2713 /* Primary = TOS | Primary */
2715 static char* ops [12] = {
2716 0, "tosora0", "tosorax",
2717 0, "tosora0", "tosorax",
2722 /* If the right hand side is const, the lhs is not on stack but still
2723 * in the primary register.
2725 if (flags & CF_CONST) {
2727 switch (flags & CF_TYPE) {
2730 if (flags & CF_FORCECHAR) {
2731 if ((val & 0xFF) != 0xFF) {
2732 AddCodeLine ("ora #$%02X", (unsigned char)val);
2740 AddCodeLine ("ora #$%02X", (unsigned char)val);
2747 AddCodeLine ("ora #$%02X", (unsigned char)val);
2756 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2757 * into the normal, non-optimized stuff.
2759 g_push (flags & ~CF_CONST, 0);
2763 /* Use long way over the stack */
2764 oper (flags, val, ops);
2769 void g_xor (unsigned flags, unsigned long val)
2770 /* Primary = TOS ^ Primary */
2772 static char* ops [12] = {
2773 0, "tosxora0", "tosxorax",
2774 0, "tosxora0", "tosxorax",
2780 /* If the right hand side is const, the lhs is not on stack but still
2781 * in the primary register.
2783 if (flags & CF_CONST) {
2785 switch (flags & CF_TYPE) {
2788 if (flags & CF_FORCECHAR) {
2789 if ((val & 0xFF) != 0) {
2790 AddCodeLine ("eor #$%02X", (unsigned char)val);
2799 AddCodeLine ("eor #$%02X", (unsigned char)val);
2802 } else if ((val & 0xFF) == 0) {
2803 AddCodeLine ("pha");
2804 AddCodeLine ("txa");
2805 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2806 AddCodeLine ("tax");
2807 AddCodeLine ("pla");
2815 AddCodeLine ("eor #$%02X", (unsigned char)val);
2825 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2826 * into the normal, non-optimized stuff.
2828 g_push (flags & ~CF_CONST, 0);
2832 /* Use long way over the stack */
2833 oper (flags, val, ops);
2838 void g_and (unsigned flags, unsigned long val)
2839 /* Primary = TOS & Primary */
2841 static char* ops [12] = {
2842 0, "tosanda0", "tosandax",
2843 0, "tosanda0", "tosandax",
2848 /* If the right hand side is const, the lhs is not on stack but still
2849 * in the primary register.
2851 if (flags & CF_CONST) {
2853 switch (flags & CF_TYPE) {
2856 if (flags & CF_FORCECHAR) {
2857 AddCodeLine ("and #$%02X", (unsigned char)val);
2862 if ((val & 0xFFFF) != 0xFFFF) {
2867 } else if (val != 0xFF) {
2868 AddCodeLine ("and #$%02X", (unsigned char)val);
2870 } else if ((val & 0xFF00) == 0xFF00) {
2871 AddCodeLine ("and #$%02X", (unsigned char)val);
2872 } else if ((val & 0x00FF) == 0x0000) {
2873 AddCodeLine ("txa");
2874 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2875 AddCodeLine ("tax");
2878 AddCodeLine ("tay");
2879 AddCodeLine ("txa");
2880 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2881 AddCodeLine ("tax");
2882 AddCodeLine ("tya");
2883 if ((val & 0x00FF) != 0x00FF) {
2884 AddCodeLine ("and #$%02X", (unsigned char)val);
2893 AddCodeLine ("stx sreg+1");
2894 AddCodeLine ("stx sreg");
2895 if ((val & 0xFF) != 0xFF) {
2896 AddCodeLine ("and #$%02X", (unsigned char)val);
2899 } else if (val == 0xFF00) {
2901 AddCodeLine ("sta sreg+1");
2902 AddCodeLine ("sta sreg");
2911 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2912 * into the normal, non-optimized stuff.
2914 g_push (flags & ~CF_CONST, 0);
2918 /* Use long way over the stack */
2919 oper (flags, val, ops);
2924 void g_asr (unsigned flags, unsigned long val)
2925 /* Primary = TOS >> Primary */
2927 static char* ops [12] = {
2928 0, "tosasra0", "tosasrax",
2929 0, "tosshra0", "tosshrax",
2934 /* If the right hand side is const, the lhs is not on stack but still
2935 * in the primary register.
2937 if (flags & CF_CONST) {
2939 switch (flags & CF_TYPE) {
2943 if (val >= 1 && val <= 4) {
2944 if (flags & CF_UNSIGNED) {
2945 AddCodeLine ("jsr shrax%ld", val);
2947 AddCodeLine ("jsr asrax%ld", val);
2950 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2951 AddCodeLine ("txa");
2958 if (val >= 1 && val <= 4) {
2959 if (flags & CF_UNSIGNED) {
2960 AddCodeLine ("jsr shreax%ld", val);
2962 AddCodeLine ("jsr asreax%ld", val);
2965 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2966 AddCodeLine ("txa");
2967 AddCodeLine ("ldx sreg");
2968 AddCodeLine ("ldy sreg+1");
2969 AddCodeLine ("sty sreg");
2970 AddCodeLine ("ldy #$00");
2971 AddCodeLine ("sty sreg+1");
2973 } else if (val == 16) {
2974 AddCodeLine ("ldy #$00");
2975 AddCodeLine ("ldx sreg+1");
2976 if ((flags & CF_UNSIGNED) == 0) {
2977 unsigned L = GetLocalLabel();
2978 AddCodeLine ("bpl %s", LocalLabelName (L));
2979 AddCodeLine ("dey");
2982 AddCodeLine ("lda sreg");
2983 AddCodeLine ("sty sreg+1");
2984 AddCodeLine ("sty sreg");
2993 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2994 * into the normal, non-optimized stuff.
2996 g_push (flags & ~CF_CONST, 0);
3000 /* Use long way over the stack */
3001 oper (flags, val, ops);
3006 void g_asl (unsigned flags, unsigned long val)
3007 /* Primary = TOS << Primary */
3009 static char* ops [12] = {
3010 0, "tosasla0", "tosaslax",
3011 0, "tosshla0", "tosshlax",
3017 /* If the right hand side is const, the lhs is not on stack but still
3018 * in the primary register.
3020 if (flags & CF_CONST) {
3022 switch (flags & CF_TYPE) {
3026 if (val >= 1 && val <= 4) {
3027 if (flags & CF_UNSIGNED) {
3028 AddCodeLine ("jsr shlax%ld", val);
3030 AddCodeLine ("jsr aslax%ld", val);
3033 } else if (val == 8) {
3034 AddCodeLine ("tax");
3035 AddCodeLine ("lda #$00");
3041 if (val >= 1 && val <= 4) {
3042 if (flags & CF_UNSIGNED) {
3043 AddCodeLine ("jsr shleax%ld", val);
3045 AddCodeLine ("jsr asleax%ld", val);
3048 } else if (val == 8) {
3049 AddCodeLine ("ldy sreg");
3050 AddCodeLine ("sty sreg+1");
3051 AddCodeLine ("stx sreg");
3052 AddCodeLine ("tax");
3053 AddCodeLine ("lda #$00");
3055 } else if (val == 16) {
3056 AddCodeLine ("stx sreg+1");
3057 AddCodeLine ("sta sreg");
3058 AddCodeLine ("lda #$00");
3059 AddCodeLine ("tax");
3068 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3069 * into the normal, non-optimized stuff.
3071 g_push (flags & ~CF_CONST, 0);
3075 /* Use long way over the stack */
3076 oper (flags, val, ops);
3081 void g_neg (unsigned flags)
3082 /* Primary = -Primary */
3084 switch (flags & CF_TYPE) {
3088 AddCodeLine ("jsr negax");
3092 AddCodeLine ("jsr negeax");
3102 void g_bneg (unsigned flags)
3103 /* Primary = !Primary */
3105 switch (flags & CF_TYPE) {
3108 AddCodeLine ("jsr bnega");
3112 AddCodeLine ("jsr bnegax");
3116 AddCodeLine ("jsr bnegeax");
3126 void g_com (unsigned flags)
3127 /* Primary = ~Primary */
3129 switch (flags & CF_TYPE) {
3133 AddCodeLine ("jsr complax");
3137 AddCodeLine ("jsr compleax");
3147 void g_inc (unsigned flags, unsigned long val)
3148 /* Increment the primary register by a given number */
3150 /* Don't inc by zero */
3155 /* Generate code for the supported types */
3157 switch (flags & CF_TYPE) {
3160 if (flags & CF_FORCECHAR) {
3161 if (CPU == CPU_65C02 && val <= 2) {
3163 AddCodeLine ("ina");
3166 AddCodeLine ("clc");
3167 AddCodeLine ("adc #$%02X", (unsigned char)val);
3174 if (CPU == CPU_65C02 && val == 1) {
3175 unsigned L = GetLocalLabel();
3176 AddCodeLine ("ina");
3177 AddCodeLine ("bne %s", LocalLabelName (L));
3178 AddCodeLine ("inx");
3180 } else if (CodeSizeFactor < 200) {
3183 AddCodeLine ("jsr incax%lu", val);
3184 } else if (val <= 255) {
3186 AddCodeLine ("jsr incaxy");
3188 g_add (flags | CF_CONST, val);
3191 /* Inline the code */
3193 if ((val & 0xFF) != 0) {
3194 unsigned L = GetLocalLabel();
3195 AddCodeLine ("clc");
3196 AddCodeLine ("adc #$%02X", (unsigned char) val);
3197 AddCodeLine ("bcc %s", LocalLabelName (L));
3198 AddCodeLine ("inx");
3202 AddCodeLine ("inx");
3205 AddCodeLine ("inx");
3208 AddCodeLine ("clc");
3209 if ((val & 0xFF) != 0) {
3210 AddCodeLine ("adc #$%02X", (unsigned char) val);
3212 AddCodeLine ("pha");
3213 AddCodeLine ("txa");
3214 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3215 AddCodeLine ("tax");
3216 AddCodeLine ("pla");
3224 AddCodeLine ("jsr inceaxy");
3226 g_add (flags | CF_CONST, val);
3238 void g_dec (unsigned flags, unsigned long val)
3239 /* Decrement the primary register by a given number */
3241 /* Don't dec by zero */
3246 /* Generate code for the supported types */
3248 switch (flags & CF_TYPE) {
3251 if (flags & CF_FORCECHAR) {
3252 if (CPU == CPU_65C02 && val <= 2) {
3254 AddCodeLine ("dea");
3257 AddCodeLine ("sec");
3258 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3265 if (CodeSizeFactor < 200) {
3266 /* Use subroutines */
3268 AddCodeLine ("jsr decax%d", (int) val);
3269 } else if (val <= 255) {
3271 AddCodeLine ("jsr decaxy");
3273 g_sub (flags | CF_CONST, val);
3276 /* Inline the code */
3278 if ((val & 0xFF) != 0) {
3279 unsigned L = GetLocalLabel();
3280 AddCodeLine ("sec");
3281 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3282 AddCodeLine ("bcs %s", LocalLabelName (L));
3283 AddCodeLine ("dex");
3287 AddCodeLine ("dex");
3290 AddCodeLine ("dex");
3293 AddCodeLine ("sec");
3294 if ((val & 0xFF) != 0) {
3295 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3297 AddCodeLine ("pha");
3298 AddCodeLine ("txa");
3299 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3300 AddCodeLine ("tax");
3301 AddCodeLine ("pla");
3309 AddCodeLine ("jsr deceaxy");
3311 g_sub (flags | CF_CONST, val);
3324 * Following are the conditional operators. They compare the TOS against
3325 * the primary and put a literal 1 in the primary if the condition is
3326 * true, otherwise they clear the primary register
3331 void g_eq (unsigned flags, unsigned long val)
3332 /* Test for equal */
3334 static char* ops [12] = {
3335 "toseq00", "toseqa0", "toseqax",
3336 "toseq00", "toseqa0", "toseqax",
3343 /* If the right hand side is const, the lhs is not on stack but still
3344 * in the primary register.
3346 if (flags & CF_CONST) {
3348 switch (flags & CF_TYPE) {
3351 if (flags & CF_FORCECHAR) {
3352 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3353 AddCodeLine ("jsr booleq");
3359 L = GetLocalLabel();
3360 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3361 AddCodeLine ("bne %s", LocalLabelName (L));
3362 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3364 AddCodeLine ("jsr booleq");
3374 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3375 * into the normal, non-optimized stuff.
3377 g_push (flags & ~CF_CONST, 0);
3381 /* Use long way over the stack */
3382 oper (flags, val, ops);
3387 void g_ne (unsigned flags, unsigned long val)
3388 /* Test for not equal */
3390 static char* ops [12] = {
3391 "tosne00", "tosnea0", "tosneax",
3392 "tosne00", "tosnea0", "tosneax",
3399 /* If the right hand side is const, the lhs is not on stack but still
3400 * in the primary register.
3402 if (flags & CF_CONST) {
3404 switch (flags & CF_TYPE) {
3407 if (flags & CF_FORCECHAR) {
3408 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3409 AddCodeLine ("jsr boolne");
3415 L = GetLocalLabel();
3416 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3417 AddCodeLine ("bne %s", LocalLabelName (L));
3418 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3420 AddCodeLine ("jsr boolne");
3430 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3431 * into the normal, non-optimized stuff.
3433 g_push (flags & ~CF_CONST, 0);
3437 /* Use long way over the stack */
3438 oper (flags, val, ops);
3443 void g_lt (unsigned flags, unsigned long val)
3444 /* Test for less than */
3446 static char* ops [12] = {
3447 "toslt00", "toslta0", "tosltax",
3448 "tosult00", "tosulta0", "tosultax",
3453 /* If the right hand side is const, the lhs is not on stack but still
3454 * in the primary register.
3456 if (flags & CF_CONST) {
3458 /* Give a warning in some special cases */
3459 if ((flags & CF_UNSIGNED) && val == 0) {
3460 Warning ("Condition is never true");
3463 /* Look at the type */
3464 switch (flags & CF_TYPE) {
3467 if (flags & CF_FORCECHAR) {
3468 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3469 if (flags & CF_UNSIGNED) {
3470 AddCodeLine ("jsr boolult");
3472 AddCodeLine ("jsr boollt");
3479 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3480 /* If we have a signed compare against zero, we only need to
3481 * test the high byte.
3483 AddCodeLine ("txa");
3484 AddCodeLine ("jsr boollt");
3487 /* Direct code only for unsigned data types */
3488 if (flags & CF_UNSIGNED) {
3489 unsigned L = GetLocalLabel();
3490 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3491 AddCodeLine ("bne %s", LocalLabelName (L));
3492 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3494 AddCodeLine ("jsr boolult");
3500 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3501 /* If we have a signed compare against zero, we only need to
3502 * test the high byte.
3504 AddCodeLine ("lda sreg+1");
3505 AddCodeLine ("jsr boollt");
3514 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3515 * into the normal, non-optimized stuff.
3517 g_push (flags & ~CF_CONST, 0);
3521 /* Use long way over the stack */
3522 oper (flags, val, ops);
3527 void g_le (unsigned flags, unsigned long val)
3528 /* Test for less than or equal to */
3530 static char* ops [12] = {
3531 "tosle00", "toslea0", "tosleax",
3532 "tosule00", "tosulea0", "tosuleax",
3538 /* If the right hand side is const, the lhs is not on stack but still
3539 * in the primary register.
3541 if (flags & CF_CONST) {
3543 /* <= is not very effective on the 6502, so try to convert
3544 * it into < if the value is in a valid range.
3547 /* Look at the type */
3548 switch (flags & CF_TYPE) {
3551 if (flags & CF_FORCECHAR) {
3552 if (flags & CF_UNSIGNED) {
3554 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3555 AddCodeLine ("jsr boolult");
3557 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3558 AddCodeLine ("jsr boolule");
3562 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3563 AddCodeLine ("jsr boollt");
3565 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3566 AddCodeLine ("jsr boolle");
3574 if (flags & CF_UNSIGNED) {
3575 unsigned L = GetLocalLabel();
3576 const char* Name = "boolule";
3581 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3582 AddCodeLine ("bne %s", LocalLabelName (L));
3583 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3585 AddCodeLine ("jsr %s", Name);
3597 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3598 * into the normal, non-optimized stuff.
3600 g_push (flags & ~CF_CONST, 0);
3604 /* Use long way over the stack */
3605 oper (flags, val, ops);
3610 void g_gt (unsigned flags, unsigned long val)
3611 /* Test for greater than */
3613 static char* ops [12] = {
3614 "tosgt00", "tosgta0", "tosgtax",
3615 "tosugt00", "tosugta0", "tosugtax",
3621 /* If the right hand side is const, the lhs is not on stack but still
3622 * in the primary register.
3624 if (flags & CF_CONST) {
3626 /* > is not very effective on the 6502, so try to convert
3627 * it into >= if the value is in a valid range.
3630 /* Look at the type */
3631 switch (flags & CF_TYPE) {
3634 if (flags & CF_FORCECHAR) {
3635 if (flags & CF_UNSIGNED) {
3636 /* If we have a compare > 0, we will replace it by
3637 * != 0 here, since both are identical but the latter
3638 * is easier to optimize.
3641 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3642 AddCodeLine ("jsr boolne");
3643 } else if (val < 255) {
3644 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3645 AddCodeLine ("jsr booluge");
3647 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3648 AddCodeLine ("jsr boolugt");
3652 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3653 AddCodeLine ("jsr boolge");
3655 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3656 AddCodeLine ("jsr boolgt");
3664 if (flags & CF_UNSIGNED) {
3665 /* If we have a compare > 0, we will replace it by
3666 * != 0 here, since both are identical but the latter
3667 * is easier to optimize.
3670 AddCodeLine ("stx tmp1");
3671 AddCodeLine ("ora tmp1");
3672 AddCodeLine ("jsr boolne");
3674 unsigned L = GetLocalLabel();
3675 const char* Name = "boolugt";
3680 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3681 AddCodeLine ("bne %s", LocalLabelName (L));
3682 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3684 AddCodeLine ("jsr %s", Name);
3697 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3698 * into the normal, non-optimized stuff.
3700 g_push (flags & ~CF_CONST, 0);
3704 /* Use long way over the stack */
3705 oper (flags, val, ops);
3710 void g_ge (unsigned flags, unsigned long val)
3711 /* Test for greater than or equal to */
3713 static char* ops [12] = {
3714 "tosge00", "tosgea0", "tosgeax",
3715 "tosuge00", "tosugea0", "tosugeax",
3721 /* If the right hand side is const, the lhs is not on stack but still
3722 * in the primary register.
3724 if (flags & CF_CONST) {
3726 /* Give a warning in some special cases */
3727 if ((flags & CF_UNSIGNED) && val == 0) {
3728 Warning ("Condition is always true");
3731 /* Look at the type */
3732 switch (flags & CF_TYPE) {
3735 if (flags & CF_FORCECHAR) {
3736 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3737 if (flags & CF_UNSIGNED) {
3738 AddCodeLine ("jsr booluge");
3740 AddCodeLine ("jsr boolge");
3747 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3748 /* If we have a signed compare against zero, we only need to
3749 * test the high byte.
3751 AddCodeLine ("txa");
3752 AddCodeLine ("jsr boolge");
3755 /* Direct code only for unsigned data types */
3756 if (flags & CF_UNSIGNED) {
3757 unsigned L = GetLocalLabel();
3758 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3759 AddCodeLine ("bne %s", LocalLabelName (L));
3760 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3762 AddCodeLine ("jsr booluge");
3768 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3769 /* If we have a signed compare against zero, we only need to
3770 * test the high byte.
3772 AddCodeLine ("lda sreg+1");
3773 AddCodeLine ("jsr boolge");
3782 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3783 * into the normal, non-optimized stuff.
3785 g_push (flags & ~CF_CONST, 0);
3789 /* Use long way over the stack */
3790 oper (flags, val, ops);
3795 /*****************************************************************************/
3796 /* Allocating static storage */
3797 /*****************************************************************************/
3801 void g_res (unsigned n)
3802 /* Reserve static storage, n bytes */
3804 AddDataLine ("\t.res\t%u,$00", n);
3809 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3810 /* Define data with the size given in flags */
3812 if (flags & CF_CONST) {
3814 /* Numeric constant */
3815 switch (flags & CF_TYPE) {
3818 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
3822 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
3826 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3837 /* Create the correct label name */
3838 const char* Label = GetLabelName (flags, val, offs);
3840 /* Labels are always 16 bit */
3841 AddDataLine ("\t.word\t%s", Label);
3848 void g_defbytes (const void* Bytes, unsigned Count)
3849 /* Output a row of bytes as a constant */
3855 /* Cast the buffer pointer */
3856 const unsigned char* Data = (const unsigned char*) Bytes;
3858 /* Output the stuff */
3861 /* How many go into this line? */
3862 if ((Chunk = Count) > 16) {
3867 /* Output one line */
3868 strcpy (Buf, "\t.byte\t");
3871 B += sprintf (B, "$%02X", *Data++);
3877 /* Output the line */
3884 void g_zerobytes (unsigned n)
3885 /* Output n bytes of data initialized with zero */
3887 AddDataLine ("\t.res\t%u,$00", n);
3892 /*****************************************************************************/
3893 /* User supplied assembler code */
3894 /*****************************************************************************/
3898 void g_asmcode (const char* Line, int Len)
3899 /* Output one line of assembler code. If Len is greater than zero, it is used
3900 * as the maximum number of characters to use from Line.
3904 AddCodeLine ("%.*s", Len, Line);
3906 AddCodeLine ("%s", Line);
3912 /*****************************************************************************/
3913 /* Inlined known functions */
3914 /*****************************************************************************/
3918 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3919 /* Inline the strlen() function */
3921 /* We need a label in both cases */
3922 unsigned label = GetLocalLabel ();
3924 /* Two different encodings */
3925 if (flags & CF_CONST) {
3927 /* The address of the string is constant. Create the correct label name */
3928 char* lbuf = GetLabelName (flags, val, offs);
3930 /* Generate the strlen code */
3931 AddCodeLine ("ldy #$FF");
3932 g_defcodelabel (label);
3933 AddCodeLine ("iny");
3934 AddCodeLine ("lda %s,y", lbuf);
3935 AddCodeLine ("bne %s", LocalLabelName (label));
3936 AddCodeLine ("tax");
3937 AddCodeLine ("tya");
3941 /* Address not constant but in primary */
3942 if (CodeSizeFactor < 400) {
3943 /* This is too much code, so call strlen instead of inlining */
3944 AddCodeLine ("jsr _strlen");
3946 /* Inline the function */
3947 AddCodeLine ("sta ptr1");
3948 AddCodeLine ("stx ptr1+1");
3949 AddCodeLine ("ldy #$FF");
3950 g_defcodelabel (label);
3951 AddCodeLine ("iny");
3952 AddCodeLine ("lda (ptr1),y");
3953 AddCodeLine ("bne %s", LocalLabelName (label));
3954 AddCodeLine ("tax");
3955 AddCodeLine ("tya");