1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
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 /*****************************************************************************/
63 /*****************************************************************************/
65 /*****************************************************************************/
69 /* Compiler relative stack pointer */
74 /*****************************************************************************/
76 /*****************************************************************************/
80 static void typeerror (unsigned type)
81 /* Print an error message about an invalid operand type */
83 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
88 static void CheckLocalOffs (unsigned Offs)
89 /* Check the offset into the stack for 8bit range */
92 /* Too many local vars */
93 Error ("Too many local variables");
99 static const char* GetLabelName (unsigned Flags, unsigned long Label, long Offs)
101 static char Buf [256]; /* Label name */
103 /* Create the correct label name */
104 switch (Flags & CF_ADDRMASK) {
107 /* Static memory cell */
109 xsprintf (Buf, sizeof (Buf), "%s%+ld", LocalLabelName (Label), Offs);
111 xsprintf (Buf, sizeof (Buf), "%s", LocalLabelName (Label));
118 xsprintf (Buf, sizeof (Buf), "_%s%+ld", (char*) Label, Offs);
120 xsprintf (Buf, sizeof (Buf), "_%s", (char*) Label);
125 /* Absolute address */
126 xsprintf (Buf, sizeof (Buf), "$%04X", (int)((Label+Offs) & 0xFFFF));
130 /* Variable in register bank */
131 xsprintf (Buf, sizeof (Buf), "regbank+%u", (unsigned)((Label+Offs) & 0xFFFF));
135 Internal ("Invalid address flags: %04X", Flags);
138 /* Return a pointer to the static buffer */
144 /*****************************************************************************/
145 /* Pre- and postamble */
146 /*****************************************************************************/
150 void g_preamble (void)
151 /* Generate the assembler code preamble */
153 /* Create a new (global) segment list and remember it */
157 /* Identify the compiler version */
159 AddTextLine ("; File generated by cc65 v %u.%u.%u",
160 VER_MAJOR, VER_MINOR, VER_PATCH);
163 /* Insert some object file options */
164 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
165 VER_MAJOR, VER_MINOR, VER_PATCH);
167 /* If we're producing code for some other CPU, switch the command set */
168 if (CPU == CPU_65C02) {
169 AddTextLine ("\t.setcpu\t\t\"65C02\"");
172 /* Allow auto import for runtime library routines */
173 AddTextLine ("\t.autoimport\ton");
175 /* Switch the assembler into case sensitive mode */
176 AddTextLine ("\t.case\t\ton");
178 /* Tell the assembler if we want to generate debug info */
179 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
181 /* Import the stack pointer for direct auto variable access */
182 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
184 /* Define long branch macros */
185 AddTextLine ("\t.macpack\tlongbranch");
190 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
191 /* If debug info is enabled, place a file info into the source */
194 /* We have to place this into the global text segment, so it will
195 * appear before all .dbg line statements.
197 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
203 /*****************************************************************************/
204 /* Segment support */
205 /*****************************************************************************/
209 void g_userodata (void)
210 /* Switch to the read only data segment */
212 UseDataSeg (SEG_RODATA);
217 void g_usedata (void)
218 /* Switch to the data segment */
220 UseDataSeg (SEG_DATA);
226 /* Switch to the bss segment */
228 UseDataSeg (SEG_BSS);
233 void g_segname (segment_t Seg, const char* Name)
234 /* Set the name of a segment */
238 /* Remember the new name */
239 NewSegName (Seg, Name);
241 /* Emit a segment directive for the data style segments */
243 case SEG_RODATA: S = CS->ROData; break;
244 case SEG_DATA: S = CS->Data; break;
245 case SEG_BSS: S = CS->BSS; break;
246 default: S = 0; break;
249 DS_AddLine (S, ".segment\t\"%s\"", Name);
255 /*****************************************************************************/
257 /*****************************************************************************/
261 unsigned sizeofarg (unsigned flags)
262 /* Return the size of a function argument type that is encoded in flags */
264 switch (flags & CF_TYPE) {
267 return (flags & CF_FORCECHAR)? 1 : 2;
284 int pop (unsigned flags)
285 /* Pop an argument of the given size */
287 return oursp += sizeofarg (flags);
292 int push (unsigned flags)
293 /* Push an argument of the given size */
295 return oursp -= sizeofarg (flags);
300 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
301 /* The value in Offs is an offset to an address in a/x. Make sure, an object
302 * of the type given in Flags can be loaded or stored into this address by
303 * adding part of the offset to the address in ax, so that the remaining
304 * offset fits into an index register. Return the remaining offset.
307 /* If the offset is too large for a byte register, add the high byte
308 * of the offset to the primary. Beware: We need a special correction
309 * if the offset in the low byte will overflow in the operation.
311 unsigned O = Offs & ~0xFFU;
312 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
313 /* We need to add the low byte also */
317 /* Do the correction if we need one */
319 g_inc (CF_INT | CF_CONST, O);
323 /* Return the new offset */
329 /*****************************************************************************/
330 /* Functions handling local labels */
331 /*****************************************************************************/
335 void g_defcodelabel (unsigned label)
336 /* Define a local code label */
338 CS_AddLabel (CS->Code, LocalLabelName (label));
343 void g_defdatalabel (unsigned label)
344 /* Define a local data label */
346 AddDataLine ("%s:", LocalLabelName (label));
351 /*****************************************************************************/
352 /* Functions handling global labels */
353 /*****************************************************************************/
357 void g_defgloblabel (const char* Name)
358 /* Define a global label with the given name */
360 /* Global labels are always data labels */
361 AddDataLine ("_%s:", Name);
366 void g_defexport (const char* Name, int ZP)
367 /* Export the given label */
370 AddTextLine ("\t.exportzp\t_%s", Name);
372 AddTextLine ("\t.export\t\t_%s", Name);
378 void g_defimport (const char* Name, int ZP)
379 /* Import the given label */
382 AddTextLine ("\t.importzp\t_%s", Name);
384 AddTextLine ("\t.import\t\t_%s", Name);
390 void g_importmainargs (void)
391 /* Forced import of a special symbol that handles arguments to main */
393 AddTextLine ("\t.forceimport\tinitmainargs");
398 /*****************************************************************************/
399 /* Load functions for various registers */
400 /*****************************************************************************/
404 static void ldaconst (unsigned val)
405 /* Load a with a constant */
407 AddCodeLine ("lda #$%02X", val & 0xFF);
412 static void ldxconst (unsigned val)
413 /* Load x with a constant */
415 AddCodeLine ("ldx #$%02X", val & 0xFF);
420 static void ldyconst (unsigned val)
421 /* Load y with a constant */
423 AddCodeLine ("ldy #$%02X", val & 0xFF);
428 /*****************************************************************************/
429 /* Function entry and exit */
430 /*****************************************************************************/
434 /* Remember the argument size of a function. The variable is set by g_enter
435 * and used by g_leave. If the functions gets its argument size by the caller
436 * (variable param list or function without prototype), g_enter will set the
442 void g_enter (unsigned flags, unsigned argsize)
443 /* Function prologue */
445 if ((flags & CF_FIXARGC) != 0) {
446 /* Just remember the argument size for the leave */
450 AddCodeLine ("jsr enter");
457 /* Function epilogue */
459 /* How many bytes of locals do we have to drop? */
462 /* If we didn't have a variable argument list, don't call leave */
465 /* Drop stackframe if needed */
469 AddCodeLine ("jsr incsp%d", k);
473 AddCodeLine ("jsr addysp");
480 /* Nothing to drop */
481 AddCodeLine ("jsr leave");
483 /* We've a stack frame to drop */
485 AddCodeLine ("jsr leavey");
489 /* Add the final rts */
495 /*****************************************************************************/
496 /* Register variables */
497 /*****************************************************************************/
501 void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
502 /* Swap a register variable with a location on the stack */
504 /* Calculate the actual stack offset and check it */
506 CheckLocalOffs (StackOffs);
511 if (CodeSizeFactor < 165) {
512 ldyconst (StackOffs);
514 AddCodeLine ("jsr regswap1");
516 ldyconst (StackOffs);
517 AddCodeLine ("lda (sp),y");
518 AddCodeLine ("ldx regbank%+d", RegOffs);
519 AddCodeLine ("sta regbank%+d", RegOffs);
521 AddCodeLine ("sta (sp),y");
524 } else if (Bytes == 2) {
526 ldyconst (StackOffs);
528 AddCodeLine ("jsr regswap2");
532 ldyconst (StackOffs);
535 AddCodeLine ("jsr regswap");
541 void g_save_regvars (int RegOffs, unsigned Bytes)
542 /* Save register variables */
544 /* Don't loop for up to two bytes */
547 AddCodeLine ("lda regbank%+d", RegOffs);
548 AddCodeLine ("jsr pusha");
550 } else if (Bytes == 2) {
552 AddCodeLine ("lda regbank%+d", RegOffs);
553 AddCodeLine ("ldx regbank%+d", RegOffs+1);
554 AddCodeLine ("jsr pushax");
558 /* More than two bytes - loop */
559 unsigned Label = GetLocalLabel ();
561 ldyconst (Bytes - 1);
563 g_defcodelabel (Label);
564 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
565 AddCodeLine ("sta (sp),y");
568 AddCodeLine ("bne %s", LocalLabelName (Label));
572 /* We pushed stuff, correct the stack pointer */
578 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
579 /* Restore register variables */
581 /* Calculate the actual stack offset and check it */
583 CheckLocalOffs (StackOffs);
585 /* Don't loop for up to two bytes */
588 ldyconst (StackOffs);
589 AddCodeLine ("lda (sp),y");
590 AddCodeLine ("sta regbank%+d", RegOffs);
592 } else if (Bytes == 2) {
594 ldyconst (StackOffs);
595 AddCodeLine ("lda (sp),y");
596 AddCodeLine ("sta regbank%+d", RegOffs);
598 AddCodeLine ("lda (sp),y");
599 AddCodeLine ("sta regbank%+d", RegOffs+1);
601 } else if (Bytes == 3 && CodeSizeFactor >= 133) {
603 ldyconst (StackOffs);
604 AddCodeLine ("lda (sp),y");
605 AddCodeLine ("sta regbank%+d", RegOffs);
607 AddCodeLine ("lda (sp),y");
608 AddCodeLine ("sta regbank%+d", RegOffs+1);
610 AddCodeLine ("lda (sp),y");
611 AddCodeLine ("sta regbank%+d", RegOffs+2);
615 /* More bytes - loop */
616 unsigned Label = GetLocalLabel ();
617 ldyconst (StackOffs);
618 g_defcodelabel (Label);
619 AddCodeLine ("lda (sp),y");
620 AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs);
622 AddCodeLine ("cpy #$%02X", StackOffs + Bytes);
623 AddCodeLine ("bne %s", LocalLabelName (Label));
629 /*****************************************************************************/
630 /* Fetching memory cells */
631 /*****************************************************************************/
635 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
636 /* Load a constant into the primary register */
638 unsigned char B1, B2, B3, B4;
642 if ((Flags & CF_CONST) != 0) {
644 /* Numeric constant */
645 switch (Flags & CF_TYPE) {
648 if ((Flags & CF_FORCECHAR) != 0) {
654 ldxconst ((Val >> 8) & 0xFF);
655 ldaconst (Val & 0xFF);
659 /* Split the value into 4 bytes */
660 B1 = (unsigned char) (Val >> 0);
661 B2 = (unsigned char) (Val >> 8);
662 B3 = (unsigned char) (Val >> 16);
663 B4 = (unsigned char) (Val >> 24);
665 /* Remember which bytes are done */
669 AddCodeLine ("ldx #$%02X", B2);
672 AddCodeLine ("stx sreg");
676 AddCodeLine ("stx sreg+1");
679 if ((Done & 0x04) == 0 && B1 != B3) {
680 AddCodeLine ("lda #$%02X", B3);
681 AddCodeLine ("sta sreg");
684 if ((Done & 0x08) == 0 && B1 != B4) {
685 AddCodeLine ("lda #$%02X", B4);
686 AddCodeLine ("sta sreg+1");
689 AddCodeLine ("lda #$%02X", B1);
691 if ((Done & 0x04) == 0) {
693 AddCodeLine ("sta sreg");
695 if ((Done & 0x08) == 0) {
697 AddCodeLine ("sta sreg+1");
709 /* Some sort of label */
710 const char* Label = GetLabelName (Flags, Val, Offs);
712 /* Load the address into the primary */
713 AddCodeLine ("lda #<(%s)", Label);
714 AddCodeLine ("ldx #>(%s)", Label);
721 void g_getstatic (unsigned flags, unsigned long label, long offs)
722 /* Fetch an static memory cell into the primary register */
724 /* Create the correct label name */
725 const char* lbuf = GetLabelName (flags, label, offs);
727 /* Check the size and generate the correct load operation */
728 switch (flags & CF_TYPE) {
731 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
732 AddCodeLine ("lda %s", lbuf); /* load A from the label */
735 AddCodeLine ("lda %s", lbuf); /* load A from the label */
736 if (!(flags & CF_UNSIGNED)) {
737 /* Must sign extend */
738 unsigned L = GetLocalLabel ();
739 AddCodeLine ("bpl %s", LocalLabelName (L));
747 AddCodeLine ("lda %s", lbuf);
748 if (flags & CF_TEST) {
749 AddCodeLine ("ora %s+1", lbuf);
751 AddCodeLine ("ldx %s+1", lbuf);
756 if (flags & CF_TEST) {
757 AddCodeLine ("lda %s+3", lbuf);
758 AddCodeLine ("ora %s+2", lbuf);
759 AddCodeLine ("ora %s+1", lbuf);
760 AddCodeLine ("ora %s+0", lbuf);
762 AddCodeLine ("lda %s+3", lbuf);
763 AddCodeLine ("sta sreg+1");
764 AddCodeLine ("lda %s+2", lbuf);
765 AddCodeLine ("sta sreg");
766 AddCodeLine ("ldx %s+1", lbuf);
767 AddCodeLine ("lda %s", lbuf);
779 void g_getlocal (unsigned flags, int offs)
780 /* Fetch specified local object (local var). */
783 CheckLocalOffs (offs);
784 switch (flags & CF_TYPE) {
787 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
789 AddCodeLine ("lda (sp),y");
792 AddCodeLine ("ldx #$00");
793 AddCodeLine ("lda (sp),y");
794 if ((flags & CF_UNSIGNED) == 0) {
795 unsigned L = GetLocalLabel();
796 AddCodeLine ("bpl %s", LocalLabelName (L));
804 CheckLocalOffs (offs + 1);
805 if (flags & CF_TEST) {
807 AddCodeLine ("lda (sp),y");
809 AddCodeLine ("ora (sp),y");
812 AddCodeLine ("jsr ldaxysp");
818 AddCodeLine ("jsr ldeaxysp");
828 void g_getind (unsigned flags, unsigned offs)
829 /* Fetch the specified object type indirect through the primary register
830 * into the primary register
833 /* If the offset is greater than 255, add the part that is > 255 to
834 * the primary. This way we get an easy addition and use the low byte
837 offs = MakeByteOffs (flags, offs);
839 /* Handle the indirect fetch */
840 switch (flags & CF_TYPE) {
843 /* Character sized */
844 if (flags & CF_UNSIGNED) {
846 AddCodeLine ("jsr ldauidx");
849 AddCodeLine ("jsr ldaidx");
854 if (flags & CF_TEST) {
856 AddCodeLine ("sta ptr1");
857 AddCodeLine ("stx ptr1+1");
858 AddCodeLine ("lda (ptr1),y");
860 AddCodeLine ("ora (ptr1),y");
863 AddCodeLine ("jsr ldaxidx");
869 AddCodeLine ("jsr ldeaxidx");
870 if (flags & CF_TEST) {
871 AddCodeLine ("jsr tsteax");
883 void g_leasp (int offs)
884 /* Fetch the address of the specified symbol into the primary register */
886 /* Calculate the offset relative to sp */
889 /* For value 0 we do direct code */
891 AddCodeLine ("lda sp");
892 AddCodeLine ("ldx sp+1");
894 if (CodeSizeFactor < 300) {
895 ldaconst (offs); /* Load A with offset value */
896 AddCodeLine ("jsr leaasp"); /* Load effective address */
898 unsigned L = GetLocalLabel ();
899 if (CPU == CPU_65C02 && offs == 1) {
900 AddCodeLine ("lda sp");
901 AddCodeLine ("ldx sp+1");
903 AddCodeLine ("bne %s", LocalLabelName (L));
908 AddCodeLine ("ldx sp+1");
909 AddCodeLine ("adc sp");
910 AddCodeLine ("bcc %s", LocalLabelName (L));
920 void g_leavariadic (int Offs)
921 /* Fetch the address of a parameter in a variadic function into the primary
925 unsigned ArgSizeOffs;
927 /* Calculate the offset relative to sp */
930 /* Get the offset of the parameter which is stored at sp+0 on function
931 * entry and check if this offset is reachable with a byte offset.
934 ArgSizeOffs = -oursp;
935 CheckLocalOffs (ArgSizeOffs);
937 /* Get the size of all parameters. */
938 ldyconst (ArgSizeOffs);
939 AddCodeLine ("lda (sp),y");
941 /* Add the value of the stackpointer */
942 if (CodeSizeFactor > 250) {
943 unsigned L = GetLocalLabel();
944 AddCodeLine ("ldx sp+1");
946 AddCodeLine ("adc sp");
947 AddCodeLine ("bcc %s", LocalLabelName (L));
951 AddCodeLine ("jsr leaasp");
954 /* Add the offset to the primary */
956 g_inc (CF_INT | CF_CONST, Offs);
957 } else if (Offs < 0) {
958 g_dec (CF_INT | CF_CONST, -Offs);
964 /*****************************************************************************/
965 /* Store into memory */
966 /*****************************************************************************/
970 void g_putstatic (unsigned flags, unsigned long label, long offs)
971 /* Store the primary register into the specified static memory cell */
973 /* Create the correct label name */
974 const char* lbuf = GetLabelName (flags, label, offs);
976 /* Check the size and generate the correct store operation */
977 switch (flags & CF_TYPE) {
980 AddCodeLine ("sta %s", lbuf);
984 AddCodeLine ("sta %s", lbuf);
985 AddCodeLine ("stx %s+1", lbuf);
989 AddCodeLine ("sta %s", lbuf);
990 AddCodeLine ("stx %s+1", lbuf);
991 AddCodeLine ("ldy sreg");
992 AddCodeLine ("sty %s+2", lbuf);
993 AddCodeLine ("ldy sreg+1");
994 AddCodeLine ("sty %s+3", lbuf);
1005 void g_putlocal (unsigned Flags, int Offs, long Val)
1006 /* Put data into local object. */
1009 CheckLocalOffs (Offs);
1010 switch (Flags & CF_TYPE) {
1013 if (Flags & CF_CONST) {
1014 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1017 AddCodeLine ("sta (sp),y");
1021 if (Flags & CF_CONST) {
1023 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1024 AddCodeLine ("sta (sp),y");
1025 if ((Flags & CF_NOKEEP) == 0) {
1026 /* Place high byte into X */
1027 AddCodeLine ("tax");
1029 if ((Val & 0xFF) == Offs+1) {
1030 /* The value we need is already in Y */
1031 AddCodeLine ("tya");
1032 AddCodeLine ("dey");
1034 AddCodeLine ("dey");
1035 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1037 AddCodeLine ("sta (sp),y");
1039 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
1041 AddCodeLine ("jsr staxysp");
1044 AddCodeLine ("sta (sp),y");
1045 AddCodeLine ("iny");
1046 AddCodeLine ("txa");
1047 AddCodeLine ("sta (sp),y");
1053 if (Flags & CF_CONST) {
1054 g_getimmed (Flags, Val, 0);
1057 AddCodeLine ("jsr steaxysp");
1068 void g_putind (unsigned Flags, unsigned Offs)
1069 /* Store the specified object type in the primary register at the address
1070 * on the top of the stack
1073 /* We can handle offsets below $100 directly, larger offsets must be added
1074 * to the address. Since a/x is in use, best code is achieved by adding
1075 * just the high byte. Be sure to check if the low byte will overflow while
1078 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1080 /* Overflow - we need to add the low byte also */
1081 AddCodeLine ("ldy #$00");
1082 AddCodeLine ("clc");
1083 AddCodeLine ("pha");
1084 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1085 AddCodeLine ("adc (sp),y");
1086 AddCodeLine ("sta (sp),y");
1087 AddCodeLine ("iny");
1088 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1089 AddCodeLine ("adc (sp),y");
1090 AddCodeLine ("sta (sp),y");
1091 AddCodeLine ("pla");
1093 /* Complete address is on stack, new offset is zero */
1096 } else if ((Offs & 0xFF00) != 0) {
1098 /* We can just add the high byte */
1099 AddCodeLine ("ldy #$01");
1100 AddCodeLine ("clc");
1101 AddCodeLine ("pha");
1102 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1103 AddCodeLine ("adc (sp),y");
1104 AddCodeLine ("sta (sp),y");
1105 AddCodeLine ("pla");
1107 /* Offset is now just the low byte */
1111 /* Check the size and determine operation */
1112 switch (Flags & CF_TYPE) {
1116 AddCodeLine ("jsr staspidx");
1121 AddCodeLine ("jsr staxspidx");
1126 AddCodeLine ("jsr steaxspidx");
1134 /* Pop the argument which is always a pointer */
1140 /*****************************************************************************/
1141 /* type conversion and similiar stuff */
1142 /*****************************************************************************/
1146 void g_toslong (unsigned flags)
1147 /* Make sure, the value on TOS is a long. Convert if necessary */
1149 switch (flags & CF_TYPE) {
1153 if (flags & CF_UNSIGNED) {
1154 AddCodeLine ("jsr tosulong");
1156 AddCodeLine ("jsr toslong");
1171 void g_tosint (unsigned flags)
1172 /* Make sure, the value on TOS is an int. Convert if necessary */
1174 switch (flags & CF_TYPE) {
1181 AddCodeLine ("jsr tosint");
1192 void g_regint (unsigned Flags)
1193 /* Make sure, the value in the primary register an int. Convert if necessary */
1197 switch (Flags & CF_TYPE) {
1200 if (Flags & CF_FORCECHAR) {
1201 /* Conversion is from char */
1202 if (Flags & CF_UNSIGNED) {
1203 AddCodeLine ("ldx #$00");
1205 L = GetLocalLabel();
1206 AddCodeLine ("ldx #$00");
1207 AddCodeLine ("cmp #$80");
1208 AddCodeLine ("bcc %s", LocalLabelName (L));
1209 AddCodeLine ("dex");
1226 void g_reglong (unsigned Flags)
1227 /* Make sure, the value in the primary register a long. Convert if necessary */
1231 switch (Flags & CF_TYPE) {
1234 if (Flags & CF_FORCECHAR) {
1235 /* Conversion is from char */
1236 if (Flags & CF_UNSIGNED) {
1237 if (CodeSizeFactor >= 200) {
1238 AddCodeLine ("ldx #$00");
1239 AddCodeLine ("stx sreg");
1240 AddCodeLine ("stx sreg+1");
1242 AddCodeLine ("jsr aulong");
1245 if (CodeSizeFactor >= 366) {
1246 L = GetLocalLabel();
1247 AddCodeLine ("ldx #$00");
1248 AddCodeLine ("cmp #$80");
1249 AddCodeLine ("bcc %s", LocalLabelName (L));
1250 AddCodeLine ("dex");
1252 AddCodeLine ("stx sreg");
1253 AddCodeLine ("stx sreg+1");
1255 AddCodeLine ("jsr along");
1262 if (Flags & CF_UNSIGNED) {
1263 if (CodeSizeFactor >= 200) {
1265 AddCodeLine ("sty sreg");
1266 AddCodeLine ("sty sreg+1");
1268 AddCodeLine ("jsr axulong");
1271 AddCodeLine ("jsr axlong");
1285 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1286 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1287 * value, that corresponds to the value on TOS, rhs corresponds to the value
1288 * in (e)ax. The return value is the the flags value for the resulting type.
1291 unsigned ltype, rtype;
1294 /* Get the type spec from the flags */
1295 ltype = lhs & CF_TYPE;
1296 rtype = rhs & CF_TYPE;
1298 /* Check if a conversion is needed */
1299 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1300 /* We must promote the primary register to long */
1302 /* Get the new rhs type */
1303 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1305 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1306 /* We must promote the lhs to long */
1312 /* Get the new rhs type */
1313 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1317 /* Determine the result type for the operation:
1318 * - The result is const if both operands are const.
1319 * - The result is unsigned if one of the operands is unsigned.
1320 * - The result is long if one of the operands is long.
1321 * - Otherwise the result is int sized.
1323 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1324 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1325 if (rtype == CF_LONG || ltype == CF_LONG) {
1335 unsigned g_typecast (unsigned lhs, unsigned rhs)
1336 /* Cast the value in the primary register to the operand size that is flagged
1337 * by the lhs value. Return the result value.
1340 unsigned ltype, rtype;
1342 /* Get the type spec from the flags */
1343 ltype = lhs & CF_TYPE;
1344 rtype = rhs & CF_TYPE;
1346 /* Check if a conversion is needed */
1347 if ((rhs & CF_CONST) == 0) {
1348 if (ltype == CF_LONG && rtype != CF_LONG) {
1349 /* We must promote the primary register to long */
1351 } else if (ltype == CF_INT && rtype != CF_INT) {
1352 /* We must promote the primary register to int */
1357 /* Do not need any other action. If the left type is int, and the primary
1358 * register is long, it will be automagically truncated. If the right hand
1359 * side is const, it is not located in the primary register and handled by
1360 * the expression parser code.
1363 /* Result is const if the right hand side was const */
1364 lhs |= (rhs & CF_CONST);
1366 /* The resulting type is that of the left hand side (that's why you called
1374 void g_scale (unsigned flags, long val)
1375 /* Scale the value in the primary register by the given value. If val is positive,
1376 * scale up, is val is negative, scale down. This function is used to scale
1377 * the operands or results of pointer arithmetic by the size of the type, the
1378 * pointer points to.
1383 /* Value may not be zero */
1385 Internal ("Data type has no size");
1386 } else if (val > 0) {
1389 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1391 /* Factor is 2, 4, 8 and 16, use special function */
1392 switch (flags & CF_TYPE) {
1395 if (flags & CF_FORCECHAR) {
1397 AddCodeLine ("asl a");
1404 if (CodeSizeFactor >= (p2+1)*130U) {
1405 AddCodeLine ("stx tmp1");
1407 AddCodeLine ("asl a");
1408 AddCodeLine ("rol tmp1");
1410 AddCodeLine ("ldx tmp1");
1412 if (flags & CF_UNSIGNED) {
1413 AddCodeLine ("jsr shlax%d", p2);
1415 AddCodeLine ("jsr aslax%d", p2);
1421 if (flags & CF_UNSIGNED) {
1422 AddCodeLine ("jsr shleax%d", p2);
1424 AddCodeLine ("jsr asleax%d", p2);
1433 } else if (val != 1) {
1435 /* Use a multiplication instead */
1436 g_mul (flags | CF_CONST, val);
1444 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1446 /* Factor is 2, 4, 8 and 16 use special function */
1447 switch (flags & CF_TYPE) {
1450 if (flags & CF_FORCECHAR) {
1451 if (flags & CF_UNSIGNED) {
1453 AddCodeLine ("lsr a");
1456 } else if (p2 <= 2) {
1457 AddCodeLine ("cmp #$80");
1458 AddCodeLine ("ror a");
1465 if (flags & CF_UNSIGNED) {
1466 if (CodeSizeFactor >= (p2+1)*130U) {
1467 AddCodeLine ("stx tmp1");
1469 AddCodeLine ("lsr tmp1");
1470 AddCodeLine ("ror a");
1472 AddCodeLine ("ldx tmp1");
1474 AddCodeLine ("jsr lsrax%d", p2);
1477 if (CodeSizeFactor >= (p2+1)*150U) {
1478 AddCodeLine ("stx tmp1");
1480 AddCodeLine ("cpx #$80");
1481 AddCodeLine ("ror tmp1");
1482 AddCodeLine ("ror a");
1484 AddCodeLine ("ldx tmp1");
1486 AddCodeLine ("jsr asrax%d", p2);
1492 if (flags & CF_UNSIGNED) {
1493 AddCodeLine ("jsr lsreax%d", p2);
1495 AddCodeLine ("jsr asreax%d", p2);
1504 } else if (val != 1) {
1506 /* Use a division instead */
1507 g_div (flags | CF_CONST, val);
1515 /*****************************************************************************/
1516 /* Adds and subs of variables fix a fixed address */
1517 /*****************************************************************************/
1521 void g_addlocal (unsigned flags, int offs)
1522 /* Add a local variable to ax */
1526 /* Correct the offset and check it */
1528 CheckLocalOffs (offs);
1530 switch (flags & CF_TYPE) {
1533 L = GetLocalLabel();
1534 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1535 AddCodeLine ("clc");
1536 AddCodeLine ("adc (sp),y");
1537 AddCodeLine ("bcc %s", LocalLabelName (L));
1538 AddCodeLine ("inx");
1543 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1544 AddCodeLine ("clc");
1545 AddCodeLine ("adc (sp),y");
1546 AddCodeLine ("pha");
1547 AddCodeLine ("txa");
1548 AddCodeLine ("iny");
1549 AddCodeLine ("adc (sp),y");
1550 AddCodeLine ("tax");
1551 AddCodeLine ("pla");
1555 /* Do it the old way */
1557 g_getlocal (flags, offs);
1569 void g_addstatic (unsigned flags, unsigned long label, long offs)
1570 /* Add a static variable to ax */
1574 /* Create the correct label name */
1575 const char* lbuf = GetLabelName (flags, label, offs);
1577 switch (flags & CF_TYPE) {
1580 L = GetLocalLabel();
1581 AddCodeLine ("clc");
1582 AddCodeLine ("adc %s", lbuf);
1583 AddCodeLine ("bcc %s", LocalLabelName (L));
1584 AddCodeLine ("inx");
1589 AddCodeLine ("clc");
1590 AddCodeLine ("adc %s", lbuf);
1591 AddCodeLine ("tay");
1592 AddCodeLine ("txa");
1593 AddCodeLine ("adc %s+1", lbuf);
1594 AddCodeLine ("tax");
1595 AddCodeLine ("tya");
1599 /* Do it the old way */
1601 g_getstatic (flags, label, offs);
1613 /*****************************************************************************/
1614 /* Special op= functions */
1615 /*****************************************************************************/
1619 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1621 /* Emit += for a static variable */
1623 /* Create the correct label name */
1624 const char* lbuf = GetLabelName (flags, label, offs);
1626 /* Check the size and determine operation */
1627 switch (flags & CF_TYPE) {
1630 if (flags & CF_FORCECHAR) {
1631 AddCodeLine ("ldx #$00");
1632 if (flags & CF_CONST) {
1634 AddCodeLine ("inc %s", lbuf);
1635 AddCodeLine ("lda %s", lbuf);
1637 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1638 AddCodeLine ("clc");
1639 AddCodeLine ("adc %s", lbuf);
1640 AddCodeLine ("sta %s", lbuf);
1643 AddCodeLine ("clc");
1644 AddCodeLine ("adc %s", lbuf);
1645 AddCodeLine ("sta %s", lbuf);
1647 if ((flags & CF_UNSIGNED) == 0) {
1648 unsigned L = GetLocalLabel();
1649 AddCodeLine ("bpl %s", LocalLabelName (L));
1650 AddCodeLine ("dex");
1658 if (flags & CF_CONST) {
1660 unsigned L = GetLocalLabel ();
1661 AddCodeLine ("inc %s", lbuf);
1662 AddCodeLine ("bne %s", LocalLabelName (L));
1663 AddCodeLine ("inc %s+1", lbuf);
1665 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1666 AddCodeLine ("ldx %s+1", lbuf);
1668 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1669 AddCodeLine ("clc");
1670 AddCodeLine ("adc %s", lbuf);
1671 AddCodeLine ("sta %s", lbuf);
1673 unsigned L = GetLocalLabel ();
1674 AddCodeLine ("bcc %s", LocalLabelName (L));
1675 AddCodeLine ("inc %s+1", lbuf);
1677 AddCodeLine ("ldx %s+1", lbuf);
1679 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1680 AddCodeLine ("adc %s+1", lbuf);
1681 AddCodeLine ("sta %s+1", lbuf);
1682 AddCodeLine ("tax");
1683 AddCodeLine ("lda %s", lbuf);
1687 AddCodeLine ("clc");
1688 AddCodeLine ("adc %s", lbuf);
1689 AddCodeLine ("sta %s", lbuf);
1690 AddCodeLine ("txa");
1691 AddCodeLine ("adc %s+1", lbuf);
1692 AddCodeLine ("sta %s+1", lbuf);
1693 AddCodeLine ("tax");
1694 AddCodeLine ("lda %s", lbuf);
1699 if (flags & CF_CONST) {
1701 AddCodeLine ("ldy #<(%s)", lbuf);
1702 AddCodeLine ("sty ptr1");
1703 AddCodeLine ("ldy #>(%s)", lbuf);
1705 AddCodeLine ("jsr laddeq1");
1707 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1708 AddCodeLine ("jsr laddeqa");
1711 g_getstatic (flags, label, offs);
1713 g_putstatic (flags, label, offs);
1716 AddCodeLine ("ldy #<(%s)", lbuf);
1717 AddCodeLine ("sty ptr1");
1718 AddCodeLine ("ldy #>(%s)", lbuf);
1719 AddCodeLine ("jsr laddeq");
1730 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1731 /* Emit += for a local variable */
1733 /* Calculate the true offset, check it, load it into Y */
1735 CheckLocalOffs (offs);
1737 /* Check the size and determine operation */
1738 switch (flags & CF_TYPE) {
1741 if (flags & CF_FORCECHAR) {
1743 AddCodeLine ("ldx #$00");
1744 if (flags & CF_CONST) {
1745 AddCodeLine ("clc");
1746 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1747 AddCodeLine ("adc (sp),y");
1748 AddCodeLine ("sta (sp),y");
1750 AddCodeLine ("clc");
1751 AddCodeLine ("adc (sp),y");
1752 AddCodeLine ("sta (sp),y");
1754 if ((flags & CF_UNSIGNED) == 0) {
1755 unsigned L = GetLocalLabel();
1756 AddCodeLine ("bpl %s", LocalLabelName (L));
1757 AddCodeLine ("dex");
1766 if (flags & CF_CONST) {
1767 if (CodeSizeFactor >= 400) {
1768 AddCodeLine ("clc");
1769 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1770 AddCodeLine ("adc (sp),y");
1771 AddCodeLine ("sta (sp),y");
1772 AddCodeLine ("iny");
1773 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1774 AddCodeLine ("adc (sp),y");
1775 AddCodeLine ("sta (sp),y");
1776 AddCodeLine ("tax");
1777 AddCodeLine ("dey");
1778 AddCodeLine ("lda (sp),y");
1780 g_getimmed (flags, val, 0);
1781 AddCodeLine ("jsr addeqysp");
1784 AddCodeLine ("jsr addeqysp");
1789 if (flags & CF_CONST) {
1790 g_getimmed (flags, val, 0);
1793 AddCodeLine ("jsr laddeqysp");
1803 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1804 /* Emit += for the location with address in ax */
1806 /* If the offset is too large for a byte register, add the high byte
1807 * of the offset to the primary. Beware: We need a special correction
1808 * if the offset in the low byte will overflow in the operation.
1810 offs = MakeByteOffs (flags, offs);
1812 /* Check the size and determine operation */
1813 switch (flags & CF_TYPE) {
1816 AddCodeLine ("sta ptr1");
1817 AddCodeLine ("stx ptr1+1");
1818 AddCodeLine ("ldy #$%02X", offs);
1819 AddCodeLine ("ldx #$00");
1820 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1821 AddCodeLine ("clc");
1822 AddCodeLine ("adc (ptr1),y");
1823 AddCodeLine ("sta (ptr1),y");
1827 if (CodeSizeFactor >= 200) {
1828 /* Lots of code, use only if size is not important */
1829 AddCodeLine ("sta ptr1");
1830 AddCodeLine ("stx ptr1+1");
1831 AddCodeLine ("ldy #$%02X", offs);
1832 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1833 AddCodeLine ("clc");
1834 AddCodeLine ("adc (ptr1),y");
1835 AddCodeLine ("sta (ptr1),y");
1836 AddCodeLine ("pha");
1837 AddCodeLine ("iny");
1838 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1839 AddCodeLine ("adc (ptr1),y");
1840 AddCodeLine ("sta (ptr1),y");
1841 AddCodeLine ("tax");
1842 AddCodeLine ("pla");
1848 AddCodeLine ("jsr pushax"); /* Push the address */
1849 push (CF_PTR); /* Correct the internal sp */
1850 g_getind (flags, offs); /* Fetch the value */
1851 g_inc (flags, val); /* Increment value in primary */
1852 g_putind (flags, offs); /* Store the value back */
1862 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1864 /* Emit -= for a static variable */
1866 /* Create the correct label name */
1867 const char* lbuf = GetLabelName (flags, label, offs);
1869 /* Check the size and determine operation */
1870 switch (flags & CF_TYPE) {
1873 if (flags & CF_FORCECHAR) {
1874 AddCodeLine ("ldx #$00");
1875 if (flags & CF_CONST) {
1877 AddCodeLine ("dec %s", lbuf);
1878 AddCodeLine ("lda %s", lbuf);
1880 AddCodeLine ("sec");
1881 AddCodeLine ("lda %s", lbuf);
1882 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1883 AddCodeLine ("sta %s", lbuf);
1886 AddCodeLine ("eor #$FF");
1887 AddCodeLine ("sec");
1888 AddCodeLine ("adc %s", lbuf);
1889 AddCodeLine ("sta %s", lbuf);
1891 if ((flags & CF_UNSIGNED) == 0) {
1892 unsigned L = GetLocalLabel();
1893 AddCodeLine ("bpl %s", LocalLabelName (L));
1894 AddCodeLine ("dex");
1902 AddCodeLine ("sec");
1903 if (flags & CF_CONST) {
1904 AddCodeLine ("lda %s", lbuf);
1905 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1906 AddCodeLine ("sta %s", lbuf);
1908 unsigned L = GetLocalLabel ();
1909 AddCodeLine ("bcs %s", LocalLabelName (L));
1910 AddCodeLine ("dec %s+1", lbuf);
1912 AddCodeLine ("ldx %s+1", lbuf);
1914 AddCodeLine ("lda %s+1", lbuf);
1915 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1916 AddCodeLine ("sta %s+1", lbuf);
1917 AddCodeLine ("tax");
1918 AddCodeLine ("lda %s", lbuf);
1921 AddCodeLine ("eor #$FF");
1922 AddCodeLine ("adc %s", lbuf);
1923 AddCodeLine ("sta %s", lbuf);
1924 AddCodeLine ("txa");
1925 AddCodeLine ("eor #$FF");
1926 AddCodeLine ("adc %s+1", lbuf);
1927 AddCodeLine ("sta %s+1", lbuf);
1928 AddCodeLine ("tax");
1929 AddCodeLine ("lda %s", lbuf);
1934 if (flags & CF_CONST) {
1936 AddCodeLine ("ldy #<(%s)", lbuf);
1937 AddCodeLine ("sty ptr1");
1938 AddCodeLine ("ldy #>(%s)", lbuf);
1939 AddCodeLine ("lda #$%02X", (unsigned char)val);
1940 AddCodeLine ("jsr lsubeqa");
1942 g_getstatic (flags, label, offs);
1944 g_putstatic (flags, label, offs);
1947 AddCodeLine ("ldy #<(%s)", lbuf);
1948 AddCodeLine ("sty ptr1");
1949 AddCodeLine ("ldy #>(%s)", lbuf);
1950 AddCodeLine ("jsr lsubeq");
1961 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1962 /* Emit -= for a local variable */
1964 /* Calculate the true offset, check it, load it into Y */
1966 CheckLocalOffs (offs);
1968 /* Check the size and determine operation */
1969 switch (flags & CF_TYPE) {
1972 if (flags & CF_FORCECHAR) {
1974 AddCodeLine ("ldx #$00");
1975 AddCodeLine ("sec");
1976 if (flags & CF_CONST) {
1977 AddCodeLine ("lda (sp),y");
1978 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1980 AddCodeLine ("eor #$FF");
1981 AddCodeLine ("adc (sp),y");
1983 AddCodeLine ("sta (sp),y");
1984 if ((flags & CF_UNSIGNED) == 0) {
1985 unsigned L = GetLocalLabel();
1986 AddCodeLine ("bpl %s", LocalLabelName (L));
1987 AddCodeLine ("dex");
1995 if (flags & CF_CONST) {
1996 g_getimmed (flags, val, 0);
1999 AddCodeLine ("jsr subeqysp");
2003 if (flags & CF_CONST) {
2004 g_getimmed (flags, val, 0);
2007 AddCodeLine ("jsr lsubeqysp");
2017 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2018 /* Emit -= for the location with address in ax */
2020 /* If the offset is too large for a byte register, add the high byte
2021 * of the offset to the primary. Beware: We need a special correction
2022 * if the offset in the low byte will overflow in the operation.
2024 offs = MakeByteOffs (flags, offs);
2026 /* Check the size and determine operation */
2027 switch (flags & CF_TYPE) {
2030 AddCodeLine ("sta ptr1");
2031 AddCodeLine ("stx ptr1+1");
2032 AddCodeLine ("ldy #$%02X", offs);
2033 AddCodeLine ("ldx #$00");
2034 AddCodeLine ("lda (ptr1),y");
2035 AddCodeLine ("sec");
2036 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2037 AddCodeLine ("sta (ptr1),y");
2041 if (CodeSizeFactor >= 200) {
2042 /* Lots of code, use only if size is not important */
2043 AddCodeLine ("sta ptr1");
2044 AddCodeLine ("stx ptr1+1");
2045 AddCodeLine ("ldy #$%02X", offs);
2046 AddCodeLine ("lda (ptr1),y");
2047 AddCodeLine ("sec");
2048 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2049 AddCodeLine ("sta (ptr1),y");
2050 AddCodeLine ("pha");
2051 AddCodeLine ("iny");
2052 AddCodeLine ("lda (ptr1),y");
2053 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2054 AddCodeLine ("sta (ptr1),y");
2055 AddCodeLine ("tax");
2056 AddCodeLine ("pla");
2062 AddCodeLine ("jsr pushax"); /* Push the address */
2063 push (CF_PTR); /* Correct the internal sp */
2064 g_getind (flags, offs); /* Fetch the value */
2065 g_dec (flags, val); /* Increment value in primary */
2066 g_putind (flags, offs); /* Store the value back */
2076 /*****************************************************************************/
2077 /* Add a variable address to the value in ax */
2078 /*****************************************************************************/
2082 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2083 /* Add the address of a local variable to ax */
2087 /* Add the offset */
2090 /* We cannot address more then 256 bytes of locals anyway */
2091 L = GetLocalLabel();
2092 CheckLocalOffs (offs);
2093 AddCodeLine ("clc");
2094 AddCodeLine ("adc #$%02X", offs & 0xFF);
2095 /* Do also skip the CLC insn below */
2096 AddCodeLine ("bcc %s", LocalLabelName (L));
2097 AddCodeLine ("inx");
2100 /* Add the current stackpointer value */
2101 AddCodeLine ("clc");
2103 /* Label was used above */
2106 AddCodeLine ("adc sp");
2107 AddCodeLine ("tay");
2108 AddCodeLine ("txa");
2109 AddCodeLine ("adc sp+1");
2110 AddCodeLine ("tax");
2111 AddCodeLine ("tya");
2116 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2117 /* Add the address of a static variable to ax */
2119 /* Create the correct label name */
2120 const char* lbuf = GetLabelName (flags, label, offs);
2122 /* Add the address to the current ax value */
2123 AddCodeLine ("clc");
2124 AddCodeLine ("adc #<(%s)", lbuf);
2125 AddCodeLine ("tay");
2126 AddCodeLine ("txa");
2127 AddCodeLine ("adc #>(%s)", lbuf);
2128 AddCodeLine ("tax");
2129 AddCodeLine ("tya");
2134 /*****************************************************************************/
2136 /*****************************************************************************/
2140 void g_save (unsigned flags)
2141 /* Copy primary register to hold register. */
2143 /* Check the size and determine operation */
2144 switch (flags & CF_TYPE) {
2147 if (flags & CF_FORCECHAR) {
2148 AddCodeLine ("pha");
2154 AddCodeLine ("sta regsave");
2155 AddCodeLine ("stx regsave+1");
2159 AddCodeLine ("jsr saveeax");
2169 void g_restore (unsigned flags)
2170 /* Copy hold register to primary. */
2172 /* Check the size and determine operation */
2173 switch (flags & CF_TYPE) {
2176 if (flags & CF_FORCECHAR) {
2177 AddCodeLine ("pla");
2183 AddCodeLine ("lda regsave");
2184 AddCodeLine ("ldx regsave+1");
2188 AddCodeLine ("jsr resteax");
2198 void g_cmp (unsigned flags, unsigned long val)
2199 /* Immidiate compare. The primary register will not be changed, Z flag
2205 /* Check the size and determine operation */
2206 switch (flags & CF_TYPE) {
2209 if (flags & CF_FORCECHAR) {
2210 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2216 L = GetLocalLabel();
2217 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2218 AddCodeLine ("bne %s", LocalLabelName (L));
2219 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2224 Internal ("g_cmp: Long compares not implemented");
2234 static void oper (unsigned flags, unsigned long val, char** subs)
2235 /* Encode a binary operation. subs is a pointer to four groups of three
2237 * 0-2 --> Operate on ints
2238 * 3-5 --> Operate on unsigneds
2239 * 6-8 --> Operate on longs
2240 * 9-11 --> Operate on unsigned longs
2242 * The first subroutine names in each string group is used to encode an
2243 * operation with a zero constant, the second to encode an operation with
2244 * a 8 bit constant, and the third is used in all other cases.
2249 /* Determine the offset into the array */
2250 offs = (flags & CF_UNSIGNED)? 3 : 0;
2251 switch (flags & CF_TYPE) {
2264 /* Encode the operation */
2265 if (flags & CF_CONST) {
2266 /* Constant value given */
2267 if (val == 0 && subs [offs+0]) {
2268 /* Special case: constant with value zero */
2269 AddCodeLine ("jsr %s", subs [offs+0]);
2270 } else if (val < 0x100 && subs [offs+1]) {
2271 /* Special case: constant with high byte zero */
2272 ldaconst (val); /* Load low byte */
2273 AddCodeLine ("jsr %s", subs [offs+1]);
2275 /* Others: arbitrary constant value */
2276 g_getimmed (flags, val, 0); /* Load value */
2277 AddCodeLine ("jsr %s", subs [offs+2]);
2280 /* Value not constant (is already in (e)ax) */
2281 AddCodeLine ("jsr %s", subs [offs+2]);
2284 /* The operation will pop it's argument */
2290 void g_test (unsigned flags)
2291 /* Test the value in the primary and set the condition codes */
2293 switch (flags & CF_TYPE) {
2296 if (flags & CF_FORCECHAR) {
2297 AddCodeLine ("tax");
2303 AddCodeLine ("stx tmp1");
2304 AddCodeLine ("ora tmp1");
2308 if (flags & CF_UNSIGNED) {
2309 AddCodeLine ("jsr utsteax");
2311 AddCodeLine ("jsr tsteax");
2323 void g_push (unsigned flags, unsigned long val)
2324 /* Push the primary register or a constant value onto the stack */
2326 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2328 /* We have a constant 8 or 16 bit value */
2329 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2331 /* Handle as 8 bit value */
2333 AddCodeLine ("jsr pusha");
2337 /* Handle as 16 bit value */
2338 g_getimmed (flags, val, 0);
2339 AddCodeLine ("jsr pushax");
2344 /* Value is not 16 bit or not constant */
2345 if (flags & CF_CONST) {
2346 /* Constant 32 bit value, load into eax */
2347 g_getimmed (flags, val, 0);
2350 /* Push the primary register */
2351 switch (flags & CF_TYPE) {
2354 if (flags & CF_FORCECHAR) {
2355 /* Handle as char */
2356 AddCodeLine ("jsr pusha");
2361 AddCodeLine ("jsr pushax");
2365 AddCodeLine ("jsr pusheax");
2375 /* Adjust the stack offset */
2381 void g_swap (unsigned flags)
2382 /* Swap the primary register and the top of the stack. flags give the type
2383 * of *both* values (must have same size).
2386 switch (flags & CF_TYPE) {
2390 AddCodeLine ("jsr swapstk");
2394 AddCodeLine ("jsr swapestk");
2405 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2406 /* Call the specified subroutine name */
2408 if ((Flags & CF_FIXARGC) == 0) {
2409 /* Pass the argument count */
2412 AddCodeLine ("jsr _%s", Label);
2413 oursp += ArgSize; /* callee pops args */
2418 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2419 /* Call subroutine indirect */
2421 if ((Flags & CF_LOCAL) == 0) {
2422 /* Address is in a/x */
2423 if ((Flags & CF_FIXARGC) == 0) {
2424 /* Pass arg count */
2427 AddCodeLine ("jsr callax");
2429 /* The address is on stack, offset is on Val */
2431 CheckLocalOffs (Offs);
2432 AddCodeLine ("pha");
2433 AddCodeLine ("ldy #$%02X", Offs);
2434 AddCodeLine ("lda (sp),y");
2435 AddCodeLine ("sta jmpvec+1");
2436 AddCodeLine ("iny");
2437 AddCodeLine ("lda (sp),y");
2438 AddCodeLine ("sta jmpvec+2");
2439 AddCodeLine ("pla");
2440 AddCodeLine ("jsr jmpvec");
2443 /* Callee pops args */
2449 void g_jump (unsigned Label)
2450 /* Jump to specified internal label number */
2452 AddCodeLine ("jmp %s", LocalLabelName (Label));
2457 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2458 /* Jump to label if zero flag clear */
2460 AddCodeLine ("jne %s", LocalLabelName (label));
2465 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2466 /* Jump to label if zero flag set */
2468 AddCodeLine ("jeq %s", LocalLabelName (label));
2473 static void mod_internal (int k, char* verb1, char* verb2)
2476 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2480 AddCodeLine ("jsr %ssp", verb2);
2486 void g_space (int space)
2487 /* Create or drop space on the stack */
2490 mod_internal (-space, "inc", "addy");
2491 } else if (space > 0) {
2492 mod_internal (space, "dec", "suby");
2498 void g_cstackcheck (void)
2499 /* Check for a C stack overflow */
2501 AddCodeLine ("jsr cstkchk");
2506 void g_stackcheck (void)
2507 /* Check for a stack overflow */
2509 AddCodeLine ("jsr stkchk");
2514 void g_add (unsigned flags, unsigned long val)
2515 /* Primary = TOS + Primary */
2517 static char* ops [12] = {
2518 0, "tosadda0", "tosaddax",
2519 0, "tosadda0", "tosaddax",
2524 if (flags & CF_CONST) {
2525 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2526 g_push (flags & ~CF_CONST, 0);
2528 oper (flags, val, ops);
2533 void g_sub (unsigned flags, unsigned long val)
2534 /* Primary = TOS - Primary */
2536 static char* ops [12] = {
2537 0, "tossuba0", "tossubax",
2538 0, "tossuba0", "tossubax",
2543 if (flags & CF_CONST) {
2544 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2545 g_push (flags & ~CF_CONST, 0);
2547 oper (flags, val, ops);
2552 void g_rsub (unsigned flags, unsigned long val)
2553 /* Primary = Primary - TOS */
2555 static char* ops [12] = {
2556 0, "tosrsuba0", "tosrsubax",
2557 0, "tosrsuba0", "tosrsubax",
2561 oper (flags, val, ops);
2566 void g_mul (unsigned flags, unsigned long val)
2567 /* Primary = TOS * Primary */
2569 static char* ops [12] = {
2570 0, "tosmula0", "tosmulax",
2571 0, "tosumula0", "tosumulax",
2578 /* Do strength reduction if the value is constant and a power of two */
2579 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2580 /* Generate a shift instead */
2585 /* If the right hand side is const, the lhs is not on stack but still
2586 * in the primary register.
2588 if (flags & CF_CONST) {
2590 switch (flags & CF_TYPE) {
2593 if (flags & CF_FORCECHAR) {
2594 /* Handle some special cases */
2598 AddCodeLine ("sta tmp1");
2599 AddCodeLine ("asl a");
2600 AddCodeLine ("clc");
2601 AddCodeLine ("adc tmp1");
2605 AddCodeLine ("sta tmp1");
2606 AddCodeLine ("asl a");
2607 AddCodeLine ("asl a");
2608 AddCodeLine ("clc");
2609 AddCodeLine ("adc tmp1");
2613 AddCodeLine ("sta tmp1");
2614 AddCodeLine ("asl a");
2615 AddCodeLine ("clc");
2616 AddCodeLine ("adc tmp1");
2617 AddCodeLine ("asl a");
2621 AddCodeLine ("sta tmp1");
2622 AddCodeLine ("asl a");
2623 AddCodeLine ("asl a");
2624 AddCodeLine ("clc");
2625 AddCodeLine ("adc tmp1");
2626 AddCodeLine ("asl a");
2635 AddCodeLine ("jsr mulax3");
2638 AddCodeLine ("jsr mulax5");
2641 AddCodeLine ("jsr mulax6");
2644 AddCodeLine ("jsr mulax7");
2647 AddCodeLine ("jsr mulax9");
2650 AddCodeLine ("jsr mulax10");
2662 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2663 * into the normal, non-optimized stuff.
2665 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2666 g_push (flags & ~CF_CONST, 0);
2670 /* Use long way over the stack */
2671 oper (flags, val, ops);
2676 void g_div (unsigned flags, unsigned long val)
2677 /* Primary = TOS / Primary */
2679 static char* ops [12] = {
2680 0, "tosdiva0", "tosdivax",
2681 0, "tosudiva0", "tosudivax",
2686 /* Do strength reduction if the value is constant and a power of two */
2688 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2689 /* Generate a shift instead */
2692 /* Generate a division */
2693 if (flags & CF_CONST) {
2694 /* lhs is not on stack */
2695 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2696 g_push (flags & ~CF_CONST, 0);
2698 oper (flags, val, ops);
2704 void g_mod (unsigned flags, unsigned long val)
2705 /* Primary = TOS % Primary */
2707 static char* ops [12] = {
2708 0, "tosmoda0", "tosmodax",
2709 0, "tosumoda0", "tosumodax",
2715 /* Check if we can do some cost reduction */
2716 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2717 /* We can do that with an AND operation */
2718 g_and (flags, val - 1);
2720 /* Do it the hard way... */
2721 if (flags & CF_CONST) {
2722 /* lhs is not on stack */
2723 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2724 g_push (flags & ~CF_CONST, 0);
2726 oper (flags, val, ops);
2732 void g_or (unsigned flags, unsigned long val)
2733 /* Primary = TOS | Primary */
2735 static char* ops [12] = {
2736 0, "tosora0", "tosorax",
2737 0, "tosora0", "tosorax",
2742 /* If the right hand side is const, the lhs is not on stack but still
2743 * in the primary register.
2745 if (flags & CF_CONST) {
2747 switch (flags & CF_TYPE) {
2750 if (flags & CF_FORCECHAR) {
2751 if ((val & 0xFF) != 0xFF) {
2752 AddCodeLine ("ora #$%02X", (unsigned char)val);
2760 AddCodeLine ("ora #$%02X", (unsigned char)val);
2767 AddCodeLine ("ora #$%02X", (unsigned char)val);
2776 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2777 * into the normal, non-optimized stuff.
2779 g_push (flags & ~CF_CONST, 0);
2783 /* Use long way over the stack */
2784 oper (flags, val, ops);
2789 void g_xor (unsigned flags, unsigned long val)
2790 /* Primary = TOS ^ Primary */
2792 static char* ops [12] = {
2793 0, "tosxora0", "tosxorax",
2794 0, "tosxora0", "tosxorax",
2800 /* If the right hand side is const, the lhs is not on stack but still
2801 * in the primary register.
2803 if (flags & CF_CONST) {
2805 switch (flags & CF_TYPE) {
2808 if (flags & CF_FORCECHAR) {
2809 if ((val & 0xFF) != 0) {
2810 AddCodeLine ("eor #$%02X", (unsigned char)val);
2819 AddCodeLine ("eor #$%02X", (unsigned char)val);
2822 } else if ((val & 0xFF) == 0) {
2823 AddCodeLine ("pha");
2824 AddCodeLine ("txa");
2825 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2826 AddCodeLine ("tax");
2827 AddCodeLine ("pla");
2835 AddCodeLine ("eor #$%02X", (unsigned char)val);
2845 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2846 * into the normal, non-optimized stuff.
2848 g_push (flags & ~CF_CONST, 0);
2852 /* Use long way over the stack */
2853 oper (flags, val, ops);
2858 void g_and (unsigned flags, unsigned long val)
2859 /* Primary = TOS & Primary */
2861 static char* ops [12] = {
2862 0, "tosanda0", "tosandax",
2863 0, "tosanda0", "tosandax",
2868 /* If the right hand side is const, the lhs is not on stack but still
2869 * in the primary register.
2871 if (flags & CF_CONST) {
2873 switch (flags & CF_TYPE) {
2876 if (flags & CF_FORCECHAR) {
2877 AddCodeLine ("and #$%02X", (unsigned char)val);
2882 if ((val & 0xFFFF) != 0xFFFF) {
2887 } else if (val != 0xFF) {
2888 AddCodeLine ("and #$%02X", (unsigned char)val);
2890 } else if ((val & 0xFF00) == 0xFF00) {
2891 AddCodeLine ("and #$%02X", (unsigned char)val);
2892 } else if ((val & 0x00FF) == 0x0000) {
2893 AddCodeLine ("txa");
2894 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2895 AddCodeLine ("tax");
2898 AddCodeLine ("tay");
2899 AddCodeLine ("txa");
2900 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2901 AddCodeLine ("tax");
2902 AddCodeLine ("tya");
2903 if ((val & 0x00FF) != 0x00FF) {
2904 AddCodeLine ("and #$%02X", (unsigned char)val);
2913 AddCodeLine ("stx sreg+1");
2914 AddCodeLine ("stx sreg");
2915 if ((val & 0xFF) != 0xFF) {
2916 AddCodeLine ("and #$%02X", (unsigned char)val);
2919 } else if (val == 0xFF00) {
2921 AddCodeLine ("sta sreg+1");
2922 AddCodeLine ("sta sreg");
2931 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2932 * into the normal, non-optimized stuff.
2934 g_push (flags & ~CF_CONST, 0);
2938 /* Use long way over the stack */
2939 oper (flags, val, ops);
2944 void g_asr (unsigned flags, unsigned long val)
2945 /* Primary = TOS >> Primary */
2947 static char* ops [12] = {
2948 0, "tosasra0", "tosasrax",
2949 0, "tosshra0", "tosshrax",
2954 /* If the right hand side is const, the lhs is not on stack but still
2955 * in the primary register.
2957 if (flags & CF_CONST) {
2959 switch (flags & CF_TYPE) {
2963 if (val >= 8 && (flags & CF_UNSIGNED)) {
2964 AddCodeLine ("txa");
2971 } else if (val >= 1 && val <= 4) {
2972 if (flags & CF_UNSIGNED) {
2973 AddCodeLine ("jsr shrax%ld", val);
2975 AddCodeLine ("jsr asrax%ld", val);
2985 } else if (val >= 1 && val <= 4) {
2986 if (flags & CF_UNSIGNED) {
2987 AddCodeLine ("jsr shreax%ld", val);
2989 AddCodeLine ("jsr asreax%ld", val);
2992 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2993 AddCodeLine ("txa");
2994 AddCodeLine ("ldx sreg");
2995 AddCodeLine ("ldy sreg+1");
2996 AddCodeLine ("sty sreg");
2997 AddCodeLine ("ldy #$00");
2998 AddCodeLine ("sty sreg+1");
3000 } else if (val == 16) {
3001 AddCodeLine ("ldy #$00");
3002 AddCodeLine ("ldx sreg+1");
3003 if ((flags & CF_UNSIGNED) == 0) {
3004 unsigned L = GetLocalLabel();
3005 AddCodeLine ("bpl %s", LocalLabelName (L));
3006 AddCodeLine ("dey");
3009 AddCodeLine ("lda sreg");
3010 AddCodeLine ("sty sreg+1");
3011 AddCodeLine ("sty sreg");
3020 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3021 * into the normal, non-optimized stuff.
3023 g_push (flags & ~CF_CONST, 0);
3027 /* Use long way over the stack */
3028 oper (flags, val, ops);
3033 void g_asl (unsigned flags, unsigned long val)
3034 /* Primary = TOS << Primary */
3036 static char* ops [12] = {
3037 0, "tosasla0", "tosaslax",
3038 0, "tosshla0", "tosshlax",
3044 /* If the right hand side is const, the lhs is not on stack but still
3045 * in the primary register.
3047 if (flags & CF_CONST) {
3049 switch (flags & CF_TYPE) {
3054 AddCodeLine ("tax");
3055 AddCodeLine ("lda #$00");
3061 } else if (val >= 1 && val <= 4) {
3062 if (flags & CF_UNSIGNED) {
3063 AddCodeLine ("jsr shlax%ld", val);
3065 AddCodeLine ("jsr aslax%ld", val);
3075 } else if (val >= 1 && val <= 4) {
3076 if (flags & CF_UNSIGNED) {
3077 AddCodeLine ("jsr shleax%ld", val);
3079 AddCodeLine ("jsr asleax%ld", val);
3082 } else if (val == 8) {
3083 AddCodeLine ("ldy sreg");
3084 AddCodeLine ("sty sreg+1");
3085 AddCodeLine ("stx sreg");
3086 AddCodeLine ("tax");
3087 AddCodeLine ("lda #$00");
3089 } else if (val == 16) {
3090 AddCodeLine ("stx sreg+1");
3091 AddCodeLine ("sta sreg");
3092 AddCodeLine ("lda #$00");
3093 AddCodeLine ("tax");
3102 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3103 * into the normal, non-optimized stuff.
3105 g_push (flags & ~CF_CONST, 0);
3109 /* Use long way over the stack */
3110 oper (flags, val, ops);
3115 void g_neg (unsigned flags)
3116 /* Primary = -Primary */
3118 switch (flags & CF_TYPE) {
3122 AddCodeLine ("jsr negax");
3126 AddCodeLine ("jsr negeax");
3136 void g_bneg (unsigned flags)
3137 /* Primary = !Primary */
3139 switch (flags & CF_TYPE) {
3142 AddCodeLine ("jsr bnega");
3146 AddCodeLine ("jsr bnegax");
3150 AddCodeLine ("jsr bnegeax");
3160 void g_com (unsigned flags)
3161 /* Primary = ~Primary */
3163 switch (flags & CF_TYPE) {
3167 AddCodeLine ("jsr complax");
3171 AddCodeLine ("jsr compleax");
3181 void g_inc (unsigned flags, unsigned long val)
3182 /* Increment the primary register by a given number */
3184 /* Don't inc by zero */
3189 /* Generate code for the supported types */
3191 switch (flags & CF_TYPE) {
3194 if (flags & CF_FORCECHAR) {
3195 if (CPU == CPU_65C02 && val <= 2) {
3197 AddCodeLine ("ina");
3200 AddCodeLine ("clc");
3201 AddCodeLine ("adc #$%02X", (unsigned char)val);
3208 if (CPU == CPU_65C02 && val == 1) {
3209 unsigned L = GetLocalLabel();
3210 AddCodeLine ("ina");
3211 AddCodeLine ("bne %s", LocalLabelName (L));
3212 AddCodeLine ("inx");
3214 } else if (CodeSizeFactor < 200) {
3217 AddCodeLine ("jsr incax%lu", val);
3218 } else if (val <= 255) {
3220 AddCodeLine ("jsr incaxy");
3222 g_add (flags | CF_CONST, val);
3225 /* Inline the code */
3227 if ((val & 0xFF) != 0) {
3228 unsigned L = GetLocalLabel();
3229 AddCodeLine ("clc");
3230 AddCodeLine ("adc #$%02X", (unsigned char) val);
3231 AddCodeLine ("bcc %s", LocalLabelName (L));
3232 AddCodeLine ("inx");
3236 AddCodeLine ("inx");
3239 AddCodeLine ("inx");
3242 AddCodeLine ("inx");
3245 AddCodeLine ("clc");
3246 if ((val & 0xFF) != 0) {
3247 AddCodeLine ("adc #$%02X", (unsigned char) val);
3249 AddCodeLine ("pha");
3250 AddCodeLine ("txa");
3251 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3252 AddCodeLine ("tax");
3253 AddCodeLine ("pla");
3261 AddCodeLine ("jsr inceaxy");
3263 g_add (flags | CF_CONST, val);
3275 void g_dec (unsigned flags, unsigned long val)
3276 /* Decrement the primary register by a given number */
3278 /* Don't dec by zero */
3283 /* Generate code for the supported types */
3285 switch (flags & CF_TYPE) {
3288 if (flags & CF_FORCECHAR) {
3289 if (CPU == CPU_65C02 && val <= 2) {
3291 AddCodeLine ("dea");
3294 AddCodeLine ("sec");
3295 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3302 if (CodeSizeFactor < 200) {
3303 /* Use subroutines */
3305 AddCodeLine ("jsr decax%d", (int) val);
3306 } else if (val <= 255) {
3308 AddCodeLine ("jsr decaxy");
3310 g_sub (flags | CF_CONST, val);
3313 /* Inline the code */
3315 if ((val & 0xFF) != 0) {
3316 unsigned L = GetLocalLabel();
3317 AddCodeLine ("sec");
3318 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3319 AddCodeLine ("bcs %s", LocalLabelName (L));
3320 AddCodeLine ("dex");
3324 AddCodeLine ("dex");
3327 AddCodeLine ("dex");
3330 AddCodeLine ("sec");
3331 if ((val & 0xFF) != 0) {
3332 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3334 AddCodeLine ("pha");
3335 AddCodeLine ("txa");
3336 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3337 AddCodeLine ("tax");
3338 AddCodeLine ("pla");
3346 AddCodeLine ("jsr deceaxy");
3348 g_sub (flags | CF_CONST, val);
3361 * Following are the conditional operators. They compare the TOS against
3362 * the primary and put a literal 1 in the primary if the condition is
3363 * true, otherwise they clear the primary register
3368 void g_eq (unsigned flags, unsigned long val)
3369 /* Test for equal */
3371 static char* ops [12] = {
3372 "toseq00", "toseqa0", "toseqax",
3373 "toseq00", "toseqa0", "toseqax",
3380 /* If the right hand side is const, the lhs is not on stack but still
3381 * in the primary register.
3383 if (flags & CF_CONST) {
3385 switch (flags & CF_TYPE) {
3388 if (flags & CF_FORCECHAR) {
3389 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3390 AddCodeLine ("jsr booleq");
3396 L = GetLocalLabel();
3397 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3398 AddCodeLine ("bne %s", LocalLabelName (L));
3399 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3401 AddCodeLine ("jsr booleq");
3411 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3412 * into the normal, non-optimized stuff.
3414 g_push (flags & ~CF_CONST, 0);
3418 /* Use long way over the stack */
3419 oper (flags, val, ops);
3424 void g_ne (unsigned flags, unsigned long val)
3425 /* Test for not equal */
3427 static char* ops [12] = {
3428 "tosne00", "tosnea0", "tosneax",
3429 "tosne00", "tosnea0", "tosneax",
3436 /* If the right hand side is const, the lhs is not on stack but still
3437 * in the primary register.
3439 if (flags & CF_CONST) {
3441 switch (flags & CF_TYPE) {
3444 if (flags & CF_FORCECHAR) {
3445 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3446 AddCodeLine ("jsr boolne");
3452 L = GetLocalLabel();
3453 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3454 AddCodeLine ("bne %s", LocalLabelName (L));
3455 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3457 AddCodeLine ("jsr boolne");
3467 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3468 * into the normal, non-optimized stuff.
3470 g_push (flags & ~CF_CONST, 0);
3474 /* Use long way over the stack */
3475 oper (flags, val, ops);
3480 void g_lt (unsigned flags, unsigned long val)
3481 /* Test for less than */
3483 static char* ops [12] = {
3484 "toslt00", "toslta0", "tosltax",
3485 "tosult00", "tosulta0", "tosultax",
3490 /* If the right hand side is const, the lhs is not on stack but still
3491 * in the primary register.
3493 if (flags & CF_CONST) {
3495 /* Because the handling of the overflow flag is too complex for
3496 * inlining, we can handle only unsigned compares here.
3498 if (flags & CF_UNSIGNED) {
3500 /* Give a warning in some special cases */
3502 Warning ("Condition is never true");
3503 AddCodeLine ("jsr return0");
3507 /* Look at the type */
3508 switch (flags & CF_TYPE) {
3511 if (flags & CF_FORCECHAR) {
3512 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3513 AddCodeLine ("jsr boolult");
3519 /* If the low byte is zero, we must only test the high byte */
3520 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3521 if ((val & 0xFF) != 0) {
3522 unsigned L = GetLocalLabel();
3523 AddCodeLine ("bne %s", LocalLabelName (L));
3524 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3527 AddCodeLine ("jsr boolult");
3531 /* Do a subtraction */
3532 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3533 AddCodeLine ("txa");
3534 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3535 AddCodeLine ("lda sreg");
3536 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3537 AddCodeLine ("lda sreg+1");
3538 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3539 AddCodeLine ("jsr boolult");
3548 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3549 * into the normal, non-optimized stuff.
3551 g_push (flags & ~CF_CONST, 0);
3555 /* Use long way over the stack */
3556 oper (flags, val, ops);
3561 void g_le (unsigned flags, unsigned long val)
3562 /* Test for less than or equal to */
3564 static char* ops [12] = {
3565 "tosle00", "toslea0", "tosleax",
3566 "tosule00", "tosulea0", "tosuleax",
3572 /* If the right hand side is const, the lhs is not on stack but still
3573 * in the primary register.
3575 if (flags & CF_CONST) {
3577 /* Look at the type */
3578 switch (flags & CF_TYPE) {
3581 if (flags & CF_FORCECHAR) {
3582 if (flags & CF_UNSIGNED) {
3583 /* Unsigned compare */
3585 /* Use < instead of <= because the former gives
3586 * better code on the 6502 than the latter.
3588 g_lt (flags, val+1);
3591 Warning ("Condition is always true");
3592 AddCodeLine ("jsr return1");
3595 /* Signed compare */
3596 if ((long) val < 0x7F) {
3597 /* Use < instead of <= because the former gives
3598 * better code on the 6502 than the latter.
3600 g_lt (flags, val+1);
3603 Warning ("Condition is always true");
3604 AddCodeLine ("jsr return1");
3612 if (flags & CF_UNSIGNED) {
3613 /* Unsigned compare */
3615 /* Use < instead of <= because the former gives
3616 * better code on the 6502 than the latter.
3618 g_lt (flags, val+1);
3621 Warning ("Condition is always true");
3622 AddCodeLine ("jsr return1");
3625 /* Signed compare */
3626 if ((long) val < 0x7FFF) {
3627 g_lt (flags, val+1);
3630 Warning ("Condition is always true");
3631 AddCodeLine ("jsr return1");
3637 if (flags & CF_UNSIGNED) {
3638 /* Unsigned compare */
3639 if (val < 0xFFFFFFFF) {
3640 /* Use < instead of <= because the former gives
3641 * better code on the 6502 than the latter.
3643 g_lt (flags, val+1);
3646 Warning ("Condition is always true");
3647 AddCodeLine ("jsr return1");
3650 /* Signed compare */
3651 if ((long) val < 0x7FFFFFFF) {
3652 g_lt (flags, val+1);
3655 Warning ("Condition is always true");
3656 AddCodeLine ("jsr return1");
3665 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3666 * into the normal, non-optimized stuff.
3668 g_push (flags & ~CF_CONST, 0);
3672 /* Use long way over the stack */
3673 oper (flags, val, ops);
3678 void g_gt (unsigned flags, unsigned long val)
3679 /* Test for greater than */
3681 static char* ops [12] = {
3682 "tosgt00", "tosgta0", "tosgtax",
3683 "tosugt00", "tosugta0", "tosugtax",
3689 /* If the right hand side is const, the lhs is not on stack but still
3690 * in the primary register.
3692 if (flags & CF_CONST) {
3694 /* Look at the type */
3695 switch (flags & CF_TYPE) {
3698 if (flags & CF_FORCECHAR) {
3699 if (flags & CF_UNSIGNED) {
3701 /* If we have a compare > 0, we will replace it by
3702 * != 0 here, since both are identical but the
3703 * latter is easier to optimize.
3706 } else if (val < 0xFF) {
3707 /* Use >= instead of > because the former gives
3708 * better code on the 6502 than the latter.
3710 g_ge (flags, val+1);
3713 Warning ("Condition is never true");
3714 AddCodeLine ("jsr return0");
3717 if ((long) val < 0x7F) {
3718 /* Use >= instead of > because the former gives
3719 * better code on the 6502 than the latter.
3721 g_ge (flags, val+1);
3724 Warning ("Condition is never true");
3725 AddCodeLine ("jsr return0");
3733 if (flags & CF_UNSIGNED) {
3734 /* Unsigned compare */
3736 /* If we have a compare > 0, we will replace it by
3737 * != 0 here, since both are identical but the latter
3738 * is easier to optimize.
3741 } else if (val < 0xFFFF) {
3742 /* Use >= instead of > because the former gives better
3743 * code on the 6502 than the latter.
3745 g_ge (flags, val+1);
3748 Warning ("Condition is never true");
3749 AddCodeLine ("jsr return0");
3752 /* Signed compare */
3753 if ((long) val < 0x7FFF) {
3754 g_ge (flags, val+1);
3757 Warning ("Condition is never true");
3758 AddCodeLine ("jsr return0");
3764 if (flags & CF_UNSIGNED) {
3765 /* Unsigned compare */
3767 /* If we have a compare > 0, we will replace it by
3768 * != 0 here, since both are identical but the latter
3769 * is easier to optimize.
3772 } else if (val < 0xFFFFFFFF) {
3773 /* Use >= instead of > because the former gives better
3774 * code on the 6502 than the latter.
3776 g_ge (flags, val+1);
3779 Warning ("Condition is never true");
3780 AddCodeLine ("jsr return0");
3783 /* Signed compare */
3784 if ((long) val < 0x7FFFFFFF) {
3785 g_ge (flags, val+1);
3788 Warning ("Condition is never true");
3789 AddCodeLine ("jsr return0");
3798 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3799 * into the normal, non-optimized stuff.
3801 g_push (flags & ~CF_CONST, 0);
3805 /* Use long way over the stack */
3806 oper (flags, val, ops);
3811 void g_ge (unsigned flags, unsigned long val)
3812 /* Test for greater than or equal to */
3814 static char* ops [12] = {
3815 "tosge00", "tosgea0", "tosgeax",
3816 "tosuge00", "tosugea0", "tosugeax",
3822 /* If the right hand side is const, the lhs is not on stack but still
3823 * in the primary register.
3825 if (flags & CF_CONST) {
3827 /* Because the handling of the overflow flag is too complex for
3828 * inlining, we can handle only unsigned compares here.
3830 if (flags & CF_UNSIGNED) {
3832 /* Give a warning in some special cases */
3834 Warning ("Condition is always true");
3835 AddCodeLine ("jsr return1");
3839 /* Look at the type */
3840 switch (flags & CF_TYPE) {
3843 if (flags & CF_FORCECHAR) {
3844 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3845 AddCodeLine ("jsr booluge");
3851 /* If the low byte is zero, we must only test the high byte */
3852 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3853 if ((val & 0xFF) != 0) {
3854 unsigned L = GetLocalLabel();
3855 AddCodeLine ("bne %s", LocalLabelName (L));
3856 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3859 AddCodeLine ("jsr booluge");
3863 /* Do a subtraction */
3864 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3865 AddCodeLine ("txa");
3866 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3867 AddCodeLine ("lda sreg");
3868 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3869 AddCodeLine ("lda sreg+1");
3870 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3871 AddCodeLine ("jsr booluge");
3880 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3881 * into the normal, non-optimized stuff.
3883 g_push (flags & ~CF_CONST, 0);
3887 /* Use long way over the stack */
3888 oper (flags, val, ops);
3893 /*****************************************************************************/
3894 /* Allocating static storage */
3895 /*****************************************************************************/
3899 void g_res (unsigned n)
3900 /* Reserve static storage, n bytes */
3902 AddDataLine ("\t.res\t%u,$00", n);
3907 void g_defdata (unsigned flags, unsigned long val, long offs)
3908 /* Define data with the size given in flags */
3910 if (flags & CF_CONST) {
3912 /* Numeric constant */
3913 switch (flags & CF_TYPE) {
3916 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
3920 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
3924 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3935 /* Create the correct label name */
3936 const char* Label = GetLabelName (flags, val, offs);
3938 /* Labels are always 16 bit */
3939 AddDataLine ("\t.addr\t%s", Label);
3946 void g_defbytes (const void* Bytes, unsigned Count)
3947 /* Output a row of bytes as a constant */
3953 /* Cast the buffer pointer */
3954 const unsigned char* Data = (const unsigned char*) Bytes;
3956 /* Output the stuff */
3959 /* How many go into this line? */
3960 if ((Chunk = Count) > 16) {
3965 /* Output one line */
3966 strcpy (Buf, "\t.byte\t");
3969 B += sprintf (B, "$%02X", *Data++);
3975 /* Output the line */
3982 void g_zerobytes (unsigned Count)
3983 /* Output Count bytes of data initialized with zero */
3986 AddDataLine ("\t.res\t%u,$00", Count);
3992 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
3993 /* Initialize a register variable from static initialization data */
3995 /* Register variables do always have less than 128 bytes */
3996 unsigned CodeLabel = GetLocalLabel ();
3998 g_defcodelabel (CodeLabel);
3999 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4000 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4001 AddCodeLine ("dex");
4002 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4007 void g_initauto (unsigned Label, unsigned Size)
4008 /* Initialize a local variable at stack offset zero from static data */
4010 unsigned CodeLabel = GetLocalLabel ();
4012 CheckLocalOffs (Size);
4015 g_defcodelabel (CodeLabel);
4016 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4017 AddCodeLine ("sta (sp),y");
4018 AddCodeLine ("dey");
4019 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4020 } else if (Size <= 256) {
4022 g_defcodelabel (CodeLabel);
4023 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4024 AddCodeLine ("sta (sp),y");
4025 AddCodeLine ("iny");
4026 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4027 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4033 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4034 /* Initialize a static local variable from static initialization data */
4037 unsigned CodeLabel = GetLocalLabel ();
4039 g_defcodelabel (CodeLabel);
4040 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4041 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4042 AddCodeLine ("dey");
4043 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4044 } else if (Size <= 256) {
4045 unsigned CodeLabel = GetLocalLabel ();
4047 g_defcodelabel (CodeLabel);
4048 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4049 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4050 AddCodeLine ("iny");
4051 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4052 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4054 /* Use the easy way here: memcpy */
4055 g_getimmed (CF_STATIC, VarLabel, 0);
4056 AddCodeLine ("jsr pushax");
4057 g_getimmed (CF_STATIC, InitLabel, 0);
4058 AddCodeLine ("jsr pushax");
4059 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4060 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4066 /*****************************************************************************/
4067 /* Switch statement */
4068 /*****************************************************************************/
4072 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4073 /* Generate code for a switch statement */
4075 unsigned NextLabel = 0;
4078 /* Setup registers and determine which compare insn to use */
4079 const char* Compare;
4082 Compare = "cmp #$%02X";
4085 Compare = "cpx #$%02X";
4088 AddCodeLine ("ldy sreg");
4089 Compare = "cpy #$%02X";
4092 AddCodeLine ("ldy sreg+1");
4093 Compare = "cpy #$%02X";
4096 Internal ("Invalid depth in g_switch: %u", Depth);
4099 /* Walk over all nodes */
4100 for (I = 0; I < CollCount (Nodes); ++I) {
4102 /* Get the next case node */
4103 CaseNode* N = CollAtUnchecked (Nodes, I);
4105 /* If we have a next label, define it */
4107 g_defcodelabel (NextLabel);
4111 /* Do the compare */
4112 AddCodeLine (Compare, CN_GetValue (N));
4114 /* If this is the last level, jump directly to the case code if found */
4117 /* Branch if equal */
4118 g_falsejump (0, CN_GetLabel (N));
4122 /* Determine the next label */
4123 if (I == CollCount (Nodes) - 1) {
4124 /* Last node means not found */
4125 g_truejump (0, DefaultLabel);
4127 /* Jump to the next check */
4128 NextLabel = GetLocalLabel ();
4129 g_truejump (0, NextLabel);
4132 /* Check the next level */
4133 g_switch (N->Nodes, DefaultLabel, Depth-1);
4138 /* If we go here, we haven't found the label */
4139 g_jump (DefaultLabel);
4144 /*****************************************************************************/
4145 /* User supplied assembler code */
4146 /*****************************************************************************/
4150 void g_asmcode (struct StrBuf* B)
4151 /* Output one line of assembler code. */
4153 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));
4158 /*****************************************************************************/
4159 /* Inlined known functions */
4160 /*****************************************************************************/
4164 void g_strlen (unsigned flags, unsigned long val, long offs)
4165 /* Inline the strlen() function */
4167 /* We need a label in both cases */
4168 unsigned label = GetLocalLabel ();
4170 /* Two different encodings */
4171 if (flags & CF_CONST) {
4173 /* The address of the string is constant. Create the correct label name */
4174 const char* lbuf = GetLabelName (flags, val, offs);
4176 /* Generate the strlen code */
4177 AddCodeLine ("ldy #$FF");
4178 g_defcodelabel (label);
4179 AddCodeLine ("iny");
4180 AddCodeLine ("lda %s,y", lbuf);
4181 AddCodeLine ("bne %s", LocalLabelName (label));
4182 AddCodeLine ("tax");
4183 AddCodeLine ("tya");
4187 /* Address not constant but in primary */
4188 if (CodeSizeFactor < 400) {
4189 /* This is too much code, so call strlen instead of inlining */
4190 AddCodeLine ("jsr _strlen");
4192 /* Inline the function */
4193 AddCodeLine ("sta ptr1");
4194 AddCodeLine ("stx ptr1+1");
4195 AddCodeLine ("ldy #$FF");
4196 g_defcodelabel (label);
4197 AddCodeLine ("iny");
4198 AddCodeLine ("lda (ptr1),y");
4199 AddCodeLine ("bne %s", LocalLabelName (label));
4200 AddCodeLine ("tax");
4201 AddCodeLine ("tya");