1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
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 */
169 case CPU_6502: AddTextLine ("\t.setcpu\t\t\"6502\""); break;
170 case CPU_65SC02: AddTextLine ("\t.setcpu\t\t\"65SC02\""); break;
171 case CPU_65C02: AddTextLine ("\t.setcpu\t\t\"65C02\""); break;
172 case CPU_65816: AddTextLine ("\t.setcpu\t\t\"65816\""); break;
173 default: Internal ("Unknown CPU: %d", CPU);
177 AddTextLine ("\t.smart\t\ton");
179 /* Allow auto import for runtime library routines */
180 AddTextLine ("\t.autoimport\ton");
182 /* Switch the assembler into case sensitive mode */
183 AddTextLine ("\t.case\t\ton");
185 /* Tell the assembler if we want to generate debug info */
186 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
188 /* Import the stack pointer for direct auto variable access */
189 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
191 /* Define long branch macros */
192 AddTextLine ("\t.macpack\tlongbranch");
197 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
198 /* If debug info is enabled, place a file info into the source */
201 /* We have to place this into the global text segment, so it will
202 * appear before all .dbg line statements.
204 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
210 /*****************************************************************************/
211 /* Segment support */
212 /*****************************************************************************/
216 void g_userodata (void)
217 /* Switch to the read only data segment */
219 UseDataSeg (SEG_RODATA);
224 void g_usedata (void)
225 /* Switch to the data segment */
227 UseDataSeg (SEG_DATA);
233 /* Switch to the bss segment */
235 UseDataSeg (SEG_BSS);
240 void g_segname (segment_t Seg)
241 /* Emit the name of a segment if necessary */
243 /* Emit a segment directive for the data style segments */
246 case SEG_RODATA: S = CS->ROData; break;
247 case SEG_DATA: S = CS->Data; break;
248 case SEG_BSS: S = CS->BSS; break;
249 default: S = 0; break;
252 DS_AddLine (S, ".segment\t\"%s\"", GetSegName (Seg));
258 /*****************************************************************************/
260 /*****************************************************************************/
264 unsigned sizeofarg (unsigned flags)
265 /* Return the size of a function argument type that is encoded in flags */
267 switch (flags & CF_TYPE) {
270 return (flags & CF_FORCECHAR)? 1 : 2;
290 int pop (unsigned flags)
291 /* Pop an argument of the given size */
293 return oursp += sizeofarg (flags);
298 int push (unsigned flags)
299 /* Push an argument of the given size */
301 return oursp -= sizeofarg (flags);
306 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
307 /* The value in Offs is an offset to an address in a/x. Make sure, an object
308 * of the type given in Flags can be loaded or stored into this address by
309 * adding part of the offset to the address in ax, so that the remaining
310 * offset fits into an index register. Return the remaining offset.
313 /* If the offset is too large for a byte register, add the high byte
314 * of the offset to the primary. Beware: We need a special correction
315 * if the offset in the low byte will overflow in the operation.
317 unsigned O = Offs & ~0xFFU;
318 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
319 /* We need to add the low byte also */
323 /* Do the correction if we need one */
325 g_inc (CF_INT | CF_CONST, O);
329 /* Return the new offset */
335 /*****************************************************************************/
336 /* Functions handling local labels */
337 /*****************************************************************************/
341 void g_defcodelabel (unsigned label)
342 /* Define a local code label */
344 CS_AddLabel (CS->Code, LocalLabelName (label));
349 void g_defdatalabel (unsigned label)
350 /* Define a local data label */
352 AddDataLine ("%s:", LocalLabelName (label));
357 /*****************************************************************************/
358 /* Functions handling global labels */
359 /*****************************************************************************/
363 void g_defgloblabel (const char* Name)
364 /* Define a global label with the given name */
366 /* Global labels are always data labels */
367 AddDataLine ("_%s:", Name);
372 void g_defexport (const char* Name, int ZP)
373 /* Export the given label */
376 AddTextLine ("\t.exportzp\t_%s", Name);
378 AddTextLine ("\t.export\t\t_%s", Name);
384 void g_defimport (const char* Name, int ZP)
385 /* Import the given label */
388 AddTextLine ("\t.importzp\t_%s", Name);
390 AddTextLine ("\t.import\t\t_%s", Name);
396 void g_importmainargs (void)
397 /* Forced import of a special symbol that handles arguments to main */
399 AddTextLine ("\t.forceimport\tinitmainargs");
404 /*****************************************************************************/
405 /* Load functions for various registers */
406 /*****************************************************************************/
410 static void ldaconst (unsigned val)
411 /* Load a with a constant */
413 AddCodeLine ("lda #$%02X", val & 0xFF);
418 static void ldxconst (unsigned val)
419 /* Load x with a constant */
421 AddCodeLine ("ldx #$%02X", val & 0xFF);
426 static void ldyconst (unsigned val)
427 /* Load y with a constant */
429 AddCodeLine ("ldy #$%02X", val & 0xFF);
434 /*****************************************************************************/
435 /* Function entry and exit */
436 /*****************************************************************************/
440 /* Remember the argument size of a function. The variable is set by g_enter
441 * and used by g_leave. If the functions gets its argument size by the caller
442 * (variable param list or function without prototype), g_enter will set the
448 void g_enter (unsigned flags, unsigned argsize)
449 /* Function prologue */
451 if ((flags & CF_FIXARGC) != 0) {
452 /* Just remember the argument size for the leave */
456 AddCodeLine ("jsr enter");
463 /* Function epilogue */
465 /* How many bytes of locals do we have to drop? */
468 /* If we didn't have a variable argument list, don't call leave */
471 /* Drop stackframe if needed */
475 AddCodeLine ("jsr incsp%d", k);
479 AddCodeLine ("jsr addysp");
486 /* Nothing to drop */
487 AddCodeLine ("jsr leave");
489 /* We've a stack frame to drop */
491 AddCodeLine ("jsr leavey");
495 /* Add the final rts */
501 /*****************************************************************************/
502 /* Register variables */
503 /*****************************************************************************/
507 void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
508 /* Swap a register variable with a location on the stack */
510 /* Calculate the actual stack offset and check it */
512 CheckLocalOffs (StackOffs);
517 if (CodeSizeFactor < 165) {
518 ldyconst (StackOffs);
520 AddCodeLine ("jsr regswap1");
522 ldyconst (StackOffs);
523 AddCodeLine ("lda (sp),y");
524 AddCodeLine ("ldx regbank%+d", RegOffs);
525 AddCodeLine ("sta regbank%+d", RegOffs);
527 AddCodeLine ("sta (sp),y");
530 } else if (Bytes == 2) {
532 ldyconst (StackOffs);
534 AddCodeLine ("jsr regswap2");
538 ldyconst (StackOffs);
541 AddCodeLine ("jsr regswap");
547 void g_save_regvars (int RegOffs, unsigned Bytes)
548 /* Save register variables */
550 /* Don't loop for up to two bytes */
553 AddCodeLine ("lda regbank%+d", RegOffs);
554 AddCodeLine ("jsr pusha");
556 } else if (Bytes == 2) {
558 AddCodeLine ("lda regbank%+d", RegOffs);
559 AddCodeLine ("ldx regbank%+d", RegOffs+1);
560 AddCodeLine ("jsr pushax");
564 /* More than two bytes - loop */
565 unsigned Label = GetLocalLabel ();
567 ldyconst (Bytes - 1);
569 g_defcodelabel (Label);
570 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
571 AddCodeLine ("sta (sp),y");
574 AddCodeLine ("bne %s", LocalLabelName (Label));
578 /* We pushed stuff, correct the stack pointer */
584 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
585 /* Restore register variables */
587 /* Calculate the actual stack offset and check it */
589 CheckLocalOffs (StackOffs);
591 /* Don't loop for up to two bytes */
594 ldyconst (StackOffs);
595 AddCodeLine ("lda (sp),y");
596 AddCodeLine ("sta regbank%+d", RegOffs);
598 } else if (Bytes == 2) {
600 ldyconst (StackOffs);
601 AddCodeLine ("lda (sp),y");
602 AddCodeLine ("sta regbank%+d", RegOffs);
604 AddCodeLine ("lda (sp),y");
605 AddCodeLine ("sta regbank%+d", RegOffs+1);
607 } else if (Bytes == 3 && CodeSizeFactor >= 133) {
609 ldyconst (StackOffs);
610 AddCodeLine ("lda (sp),y");
611 AddCodeLine ("sta regbank%+d", RegOffs);
613 AddCodeLine ("lda (sp),y");
614 AddCodeLine ("sta regbank%+d", RegOffs+1);
616 AddCodeLine ("lda (sp),y");
617 AddCodeLine ("sta regbank%+d", RegOffs+2);
619 } else if (StackOffs <= RegOffs) {
621 /* More bytes, but the relation between the register offset in the
622 * register bank and the stack offset allows us to generate short
623 * code that uses just one index register.
625 unsigned Label = GetLocalLabel ();
626 ldyconst (StackOffs);
627 g_defcodelabel (Label);
628 AddCodeLine ("lda (sp),y");
629 AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs);
631 AddCodeLine ("cpy #$%02X", StackOffs + Bytes);
632 AddCodeLine ("bne %s", LocalLabelName (Label));
636 /* Ok, this is the generic code. We need to save X because the
637 * caller will only save A.
639 unsigned Label = GetLocalLabel ();
640 AddCodeLine ("stx tmp1");
641 ldyconst (StackOffs + Bytes - 1);
642 ldxconst (Bytes - 1);
643 g_defcodelabel (Label);
644 AddCodeLine ("lda (sp),y");
645 AddCodeLine ("sta regbank%+d,x", RegOffs);
648 AddCodeLine ("bpl %s", LocalLabelName (Label));
649 AddCodeLine ("ldx tmp1");
656 /*****************************************************************************/
657 /* Fetching memory cells */
658 /*****************************************************************************/
662 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
663 /* Load a constant into the primary register */
665 unsigned char B1, B2, B3, B4;
669 if ((Flags & CF_CONST) != 0) {
671 /* Numeric constant */
672 switch (Flags & CF_TYPE) {
675 if ((Flags & CF_FORCECHAR) != 0) {
681 ldxconst ((Val >> 8) & 0xFF);
682 ldaconst (Val & 0xFF);
686 /* Split the value into 4 bytes */
687 B1 = (unsigned char) (Val >> 0);
688 B2 = (unsigned char) (Val >> 8);
689 B3 = (unsigned char) (Val >> 16);
690 B4 = (unsigned char) (Val >> 24);
692 /* Remember which bytes are done */
696 AddCodeLine ("ldx #$%02X", B2);
699 AddCodeLine ("stx sreg");
703 AddCodeLine ("stx sreg+1");
706 if ((Done & 0x04) == 0 && B1 != B3) {
707 AddCodeLine ("lda #$%02X", B3);
708 AddCodeLine ("sta sreg");
711 if ((Done & 0x08) == 0 && B1 != B4) {
712 AddCodeLine ("lda #$%02X", B4);
713 AddCodeLine ("sta sreg+1");
716 AddCodeLine ("lda #$%02X", B1);
718 if ((Done & 0x04) == 0) {
720 AddCodeLine ("sta sreg");
722 if ((Done & 0x08) == 0) {
724 AddCodeLine ("sta sreg+1");
736 /* Some sort of label */
737 const char* Label = GetLabelName (Flags, Val, Offs);
739 /* Load the address into the primary */
740 AddCodeLine ("lda #<(%s)", Label);
741 AddCodeLine ("ldx #>(%s)", Label);
748 void g_getstatic (unsigned flags, unsigned long label, long offs)
749 /* Fetch an static memory cell into the primary register */
751 /* Create the correct label name */
752 const char* lbuf = GetLabelName (flags, label, offs);
754 /* Check the size and generate the correct load operation */
755 switch (flags & CF_TYPE) {
758 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
759 AddCodeLine ("lda %s", lbuf); /* load A from the label */
762 AddCodeLine ("lda %s", lbuf); /* load A from the label */
763 if (!(flags & CF_UNSIGNED)) {
764 /* Must sign extend */
765 unsigned L = GetLocalLabel ();
766 AddCodeLine ("bpl %s", LocalLabelName (L));
774 AddCodeLine ("lda %s", lbuf);
775 if (flags & CF_TEST) {
776 AddCodeLine ("ora %s+1", lbuf);
778 AddCodeLine ("ldx %s+1", lbuf);
783 if (flags & CF_TEST) {
784 AddCodeLine ("lda %s+3", lbuf);
785 AddCodeLine ("ora %s+2", lbuf);
786 AddCodeLine ("ora %s+1", lbuf);
787 AddCodeLine ("ora %s+0", lbuf);
789 AddCodeLine ("lda %s+3", lbuf);
790 AddCodeLine ("sta sreg+1");
791 AddCodeLine ("lda %s+2", lbuf);
792 AddCodeLine ("sta sreg");
793 AddCodeLine ("ldx %s+1", lbuf);
794 AddCodeLine ("lda %s", lbuf);
806 void g_getlocal (unsigned flags, int offs)
807 /* Fetch specified local object (local var). */
810 CheckLocalOffs (offs);
811 switch (flags & CF_TYPE) {
814 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
816 AddCodeLine ("lda (sp),y");
819 AddCodeLine ("ldx #$00");
820 AddCodeLine ("lda (sp),y");
821 if ((flags & CF_UNSIGNED) == 0) {
822 unsigned L = GetLocalLabel();
823 AddCodeLine ("bpl %s", LocalLabelName (L));
831 CheckLocalOffs (offs + 1);
832 if (flags & CF_TEST) {
834 AddCodeLine ("lda (sp),y");
836 AddCodeLine ("ora (sp),y");
839 AddCodeLine ("jsr ldaxysp");
845 AddCodeLine ("jsr ldeaxysp");
855 void g_getind (unsigned flags, unsigned offs)
856 /* Fetch the specified object type indirect through the primary register
857 * into the primary register
860 /* If the offset is greater than 255, add the part that is > 255 to
861 * the primary. This way we get an easy addition and use the low byte
864 offs = MakeByteOffs (flags, offs);
866 /* Handle the indirect fetch */
867 switch (flags & CF_TYPE) {
870 /* Character sized */
871 if (flags & CF_UNSIGNED) {
873 AddCodeLine ("jsr ldauidx");
876 AddCodeLine ("jsr ldaidx");
881 if (flags & CF_TEST) {
883 AddCodeLine ("sta ptr1");
884 AddCodeLine ("stx ptr1+1");
885 AddCodeLine ("lda (ptr1),y");
887 AddCodeLine ("ora (ptr1),y");
890 AddCodeLine ("jsr ldaxidx");
896 AddCodeLine ("jsr ldeaxidx");
897 if (flags & CF_TEST) {
898 AddCodeLine ("jsr tsteax");
910 void g_leasp (int offs)
911 /* Fetch the address of the specified symbol into the primary register */
913 /* Calculate the offset relative to sp */
916 /* For value 0 we do direct code */
918 AddCodeLine ("lda sp");
919 AddCodeLine ("ldx sp+1");
921 if (CodeSizeFactor < 300) {
922 ldaconst (offs); /* Load A with offset value */
923 AddCodeLine ("jsr leaasp"); /* Load effective address */
925 unsigned L = GetLocalLabel ();
926 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && offs == 1) {
927 AddCodeLine ("lda sp");
928 AddCodeLine ("ldx sp+1");
930 AddCodeLine ("bne %s", LocalLabelName (L));
935 AddCodeLine ("ldx sp+1");
936 AddCodeLine ("adc sp");
937 AddCodeLine ("bcc %s", LocalLabelName (L));
947 void g_leavariadic (int Offs)
948 /* Fetch the address of a parameter in a variadic function into the primary
952 unsigned ArgSizeOffs;
954 /* Calculate the offset relative to sp */
957 /* Get the offset of the parameter which is stored at sp+0 on function
958 * entry and check if this offset is reachable with a byte offset.
961 ArgSizeOffs = -oursp;
962 CheckLocalOffs (ArgSizeOffs);
964 /* Get the size of all parameters. */
965 ldyconst (ArgSizeOffs);
966 AddCodeLine ("lda (sp),y");
968 /* Add the value of the stackpointer */
969 if (CodeSizeFactor > 250) {
970 unsigned L = GetLocalLabel();
971 AddCodeLine ("ldx sp+1");
973 AddCodeLine ("adc sp");
974 AddCodeLine ("bcc %s", LocalLabelName (L));
978 AddCodeLine ("jsr leaasp");
981 /* Add the offset to the primary */
983 g_inc (CF_INT | CF_CONST, Offs);
984 } else if (Offs < 0) {
985 g_dec (CF_INT | CF_CONST, -Offs);
991 /*****************************************************************************/
992 /* Store into memory */
993 /*****************************************************************************/
997 void g_putstatic (unsigned flags, unsigned long label, long offs)
998 /* Store the primary register into the specified static memory cell */
1000 /* Create the correct label name */
1001 const char* lbuf = GetLabelName (flags, label, offs);
1003 /* Check the size and generate the correct store operation */
1004 switch (flags & CF_TYPE) {
1007 AddCodeLine ("sta %s", lbuf);
1011 AddCodeLine ("sta %s", lbuf);
1012 AddCodeLine ("stx %s+1", lbuf);
1016 AddCodeLine ("sta %s", lbuf);
1017 AddCodeLine ("stx %s+1", lbuf);
1018 AddCodeLine ("ldy sreg");
1019 AddCodeLine ("sty %s+2", lbuf);
1020 AddCodeLine ("ldy sreg+1");
1021 AddCodeLine ("sty %s+3", lbuf);
1032 void g_putlocal (unsigned Flags, int Offs, long Val)
1033 /* Put data into local object. */
1036 CheckLocalOffs (Offs);
1037 switch (Flags & CF_TYPE) {
1040 if (Flags & CF_CONST) {
1041 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1044 AddCodeLine ("sta (sp),y");
1048 if (Flags & CF_CONST) {
1050 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1051 AddCodeLine ("sta (sp),y");
1052 if ((Flags & CF_NOKEEP) == 0) {
1053 /* Place high byte into X */
1054 AddCodeLine ("tax");
1056 if ((Val & 0xFF) == Offs+1) {
1057 /* The value we need is already in Y */
1058 AddCodeLine ("tya");
1059 AddCodeLine ("dey");
1061 AddCodeLine ("dey");
1062 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1064 AddCodeLine ("sta (sp),y");
1066 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
1068 AddCodeLine ("jsr staxysp");
1071 AddCodeLine ("sta (sp),y");
1072 AddCodeLine ("iny");
1073 AddCodeLine ("txa");
1074 AddCodeLine ("sta (sp),y");
1080 if (Flags & CF_CONST) {
1081 g_getimmed (Flags, Val, 0);
1084 AddCodeLine ("jsr steaxysp");
1095 void g_putind (unsigned Flags, unsigned Offs)
1096 /* Store the specified object type in the primary register at the address
1097 * on the top of the stack
1100 /* We can handle offsets below $100 directly, larger offsets must be added
1101 * to the address. Since a/x is in use, best code is achieved by adding
1102 * just the high byte. Be sure to check if the low byte will overflow while
1105 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1107 /* Overflow - we need to add the low byte also */
1108 AddCodeLine ("ldy #$00");
1109 AddCodeLine ("clc");
1110 AddCodeLine ("pha");
1111 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1112 AddCodeLine ("adc (sp),y");
1113 AddCodeLine ("sta (sp),y");
1114 AddCodeLine ("iny");
1115 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1116 AddCodeLine ("adc (sp),y");
1117 AddCodeLine ("sta (sp),y");
1118 AddCodeLine ("pla");
1120 /* Complete address is on stack, new offset is zero */
1123 } else if ((Offs & 0xFF00) != 0) {
1125 /* We can just add the high byte */
1126 AddCodeLine ("ldy #$01");
1127 AddCodeLine ("clc");
1128 AddCodeLine ("pha");
1129 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1130 AddCodeLine ("adc (sp),y");
1131 AddCodeLine ("sta (sp),y");
1132 AddCodeLine ("pla");
1134 /* Offset is now just the low byte */
1138 /* Check the size and determine operation */
1139 switch (Flags & CF_TYPE) {
1143 AddCodeLine ("jsr staspidx");
1148 AddCodeLine ("jsr staxspidx");
1153 AddCodeLine ("jsr steaxspidx");
1161 /* Pop the argument which is always a pointer */
1167 /*****************************************************************************/
1168 /* type conversion and similiar stuff */
1169 /*****************************************************************************/
1173 void g_toslong (unsigned flags)
1174 /* Make sure, the value on TOS is a long. Convert if necessary */
1176 switch (flags & CF_TYPE) {
1180 if (flags & CF_UNSIGNED) {
1181 AddCodeLine ("jsr tosulong");
1183 AddCodeLine ("jsr toslong");
1198 void g_tosint (unsigned flags)
1199 /* Make sure, the value on TOS is an int. Convert if necessary */
1201 switch (flags & CF_TYPE) {
1208 AddCodeLine ("jsr tosint");
1219 void g_regint (unsigned Flags)
1220 /* Make sure, the value in the primary register an int. Convert if necessary */
1224 switch (Flags & CF_TYPE) {
1227 if (Flags & CF_FORCECHAR) {
1228 /* Conversion is from char */
1229 if (Flags & CF_UNSIGNED) {
1230 AddCodeLine ("ldx #$00");
1232 L = GetLocalLabel();
1233 AddCodeLine ("ldx #$00");
1234 AddCodeLine ("cmp #$80");
1235 AddCodeLine ("bcc %s", LocalLabelName (L));
1236 AddCodeLine ("dex");
1253 void g_reglong (unsigned Flags)
1254 /* Make sure, the value in the primary register a long. Convert if necessary */
1258 switch (Flags & CF_TYPE) {
1261 if (Flags & CF_FORCECHAR) {
1262 /* Conversion is from char */
1263 if (Flags & CF_UNSIGNED) {
1264 if (CodeSizeFactor >= 200) {
1265 AddCodeLine ("ldx #$00");
1266 AddCodeLine ("stx sreg");
1267 AddCodeLine ("stx sreg+1");
1269 AddCodeLine ("jsr aulong");
1272 if (CodeSizeFactor >= 366) {
1273 L = GetLocalLabel();
1274 AddCodeLine ("ldx #$00");
1275 AddCodeLine ("cmp #$80");
1276 AddCodeLine ("bcc %s", LocalLabelName (L));
1277 AddCodeLine ("dex");
1279 AddCodeLine ("stx sreg");
1280 AddCodeLine ("stx sreg+1");
1282 AddCodeLine ("jsr along");
1289 if (Flags & CF_UNSIGNED) {
1290 if (CodeSizeFactor >= 200) {
1292 AddCodeLine ("sty sreg");
1293 AddCodeLine ("sty sreg+1");
1295 AddCodeLine ("jsr axulong");
1298 AddCodeLine ("jsr axlong");
1312 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1313 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1314 * value, that corresponds to the value on TOS, rhs corresponds to the value
1315 * in (e)ax. The return value is the the flags value for the resulting type.
1318 unsigned ltype, rtype;
1321 /* Get the type spec from the flags */
1322 ltype = lhs & CF_TYPE;
1323 rtype = rhs & CF_TYPE;
1325 /* Check if a conversion is needed */
1326 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1327 /* We must promote the primary register to long */
1329 /* Get the new rhs type */
1330 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1332 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1333 /* We must promote the lhs to long */
1339 /* Get the new rhs type */
1340 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1344 /* Determine the result type for the operation:
1345 * - The result is const if both operands are const.
1346 * - The result is unsigned if one of the operands is unsigned.
1347 * - The result is long if one of the operands is long.
1348 * - Otherwise the result is int sized.
1350 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1351 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1352 if (rtype == CF_LONG || ltype == CF_LONG) {
1362 unsigned g_typecast (unsigned lhs, unsigned rhs)
1363 /* Cast the value in the primary register to the operand size that is flagged
1364 * by the lhs value. Return the result value.
1367 unsigned ltype, rtype;
1369 /* Get the type spec from the flags */
1370 ltype = lhs & CF_TYPE;
1371 rtype = rhs & CF_TYPE;
1373 /* Check if a conversion is needed */
1374 if ((rhs & CF_CONST) == 0) {
1375 if (ltype == CF_LONG && rtype != CF_LONG) {
1376 /* We must promote the primary register to long */
1378 } else if (ltype == CF_INT && rtype != CF_INT) {
1379 /* We must promote the primary register to int */
1384 /* Do not need any other action. If the left type is int, and the primary
1385 * register is long, it will be automagically truncated. If the right hand
1386 * side is const, it is not located in the primary register and handled by
1387 * the expression parser code.
1390 /* Result is const if the right hand side was const */
1391 lhs |= (rhs & CF_CONST);
1393 /* The resulting type is that of the left hand side (that's why you called
1401 void g_scale (unsigned flags, long val)
1402 /* Scale the value in the primary register by the given value. If val is positive,
1403 * scale up, is val is negative, scale down. This function is used to scale
1404 * the operands or results of pointer arithmetic by the size of the type, the
1405 * pointer points to.
1410 /* Value may not be zero */
1412 Internal ("Data type has no size");
1413 } else if (val > 0) {
1416 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1418 /* Factor is 2, 4, 8 and 16, use special function */
1419 switch (flags & CF_TYPE) {
1422 if (flags & CF_FORCECHAR) {
1424 AddCodeLine ("asl a");
1431 if (CodeSizeFactor >= (p2+1)*130U) {
1432 AddCodeLine ("stx tmp1");
1434 AddCodeLine ("asl a");
1435 AddCodeLine ("rol tmp1");
1437 AddCodeLine ("ldx tmp1");
1439 if (flags & CF_UNSIGNED) {
1440 AddCodeLine ("jsr shlax%d", p2);
1442 AddCodeLine ("jsr aslax%d", p2);
1448 if (flags & CF_UNSIGNED) {
1449 AddCodeLine ("jsr shleax%d", p2);
1451 AddCodeLine ("jsr asleax%d", p2);
1460 } else if (val != 1) {
1462 /* Use a multiplication instead */
1463 g_mul (flags | CF_CONST, val);
1471 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1473 /* Factor is 2, 4, 8 and 16 use special function */
1474 switch (flags & CF_TYPE) {
1477 if (flags & CF_FORCECHAR) {
1478 if (flags & CF_UNSIGNED) {
1480 AddCodeLine ("lsr a");
1483 } else if (p2 <= 2) {
1484 AddCodeLine ("cmp #$80");
1485 AddCodeLine ("ror a");
1492 if (flags & CF_UNSIGNED) {
1493 if (CodeSizeFactor >= (p2+1)*130U) {
1494 AddCodeLine ("stx tmp1");
1496 AddCodeLine ("lsr tmp1");
1497 AddCodeLine ("ror a");
1499 AddCodeLine ("ldx tmp1");
1501 AddCodeLine ("jsr lsrax%d", p2);
1504 if (CodeSizeFactor >= (p2+1)*150U) {
1505 AddCodeLine ("stx tmp1");
1507 AddCodeLine ("cpx #$80");
1508 AddCodeLine ("ror tmp1");
1509 AddCodeLine ("ror a");
1511 AddCodeLine ("ldx tmp1");
1513 AddCodeLine ("jsr asrax%d", p2);
1519 if (flags & CF_UNSIGNED) {
1520 AddCodeLine ("jsr lsreax%d", p2);
1522 AddCodeLine ("jsr asreax%d", p2);
1531 } else if (val != 1) {
1533 /* Use a division instead */
1534 g_div (flags | CF_CONST, val);
1542 /*****************************************************************************/
1543 /* Adds and subs of variables fix a fixed address */
1544 /*****************************************************************************/
1548 void g_addlocal (unsigned flags, int offs)
1549 /* Add a local variable to ax */
1553 /* Correct the offset and check it */
1555 CheckLocalOffs (offs);
1557 switch (flags & CF_TYPE) {
1560 L = GetLocalLabel();
1561 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1562 AddCodeLine ("clc");
1563 AddCodeLine ("adc (sp),y");
1564 AddCodeLine ("bcc %s", LocalLabelName (L));
1565 AddCodeLine ("inx");
1570 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1571 AddCodeLine ("clc");
1572 AddCodeLine ("adc (sp),y");
1573 AddCodeLine ("pha");
1574 AddCodeLine ("txa");
1575 AddCodeLine ("iny");
1576 AddCodeLine ("adc (sp),y");
1577 AddCodeLine ("tax");
1578 AddCodeLine ("pla");
1582 /* Do it the old way */
1584 g_getlocal (flags, offs);
1596 void g_addstatic (unsigned flags, unsigned long label, long offs)
1597 /* Add a static variable to ax */
1601 /* Create the correct label name */
1602 const char* lbuf = GetLabelName (flags, label, offs);
1604 switch (flags & CF_TYPE) {
1607 L = GetLocalLabel();
1608 AddCodeLine ("clc");
1609 AddCodeLine ("adc %s", lbuf);
1610 AddCodeLine ("bcc %s", LocalLabelName (L));
1611 AddCodeLine ("inx");
1616 AddCodeLine ("clc");
1617 AddCodeLine ("adc %s", lbuf);
1618 AddCodeLine ("tay");
1619 AddCodeLine ("txa");
1620 AddCodeLine ("adc %s+1", lbuf);
1621 AddCodeLine ("tax");
1622 AddCodeLine ("tya");
1626 /* Do it the old way */
1628 g_getstatic (flags, label, offs);
1640 /*****************************************************************************/
1641 /* Special op= functions */
1642 /*****************************************************************************/
1646 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1648 /* Emit += for a static variable */
1650 /* Create the correct label name */
1651 const char* lbuf = GetLabelName (flags, label, offs);
1653 /* Check the size and determine operation */
1654 switch (flags & CF_TYPE) {
1657 if (flags & CF_FORCECHAR) {
1658 AddCodeLine ("ldx #$00");
1659 if (flags & CF_CONST) {
1661 AddCodeLine ("inc %s", lbuf);
1662 AddCodeLine ("lda %s", lbuf);
1664 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1665 AddCodeLine ("clc");
1666 AddCodeLine ("adc %s", lbuf);
1667 AddCodeLine ("sta %s", lbuf);
1670 AddCodeLine ("clc");
1671 AddCodeLine ("adc %s", lbuf);
1672 AddCodeLine ("sta %s", lbuf);
1674 if ((flags & CF_UNSIGNED) == 0) {
1675 unsigned L = GetLocalLabel();
1676 AddCodeLine ("bpl %s", LocalLabelName (L));
1677 AddCodeLine ("dex");
1685 if (flags & CF_CONST) {
1687 unsigned L = GetLocalLabel ();
1688 AddCodeLine ("inc %s", lbuf);
1689 AddCodeLine ("bne %s", LocalLabelName (L));
1690 AddCodeLine ("inc %s+1", lbuf);
1692 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1693 AddCodeLine ("ldx %s+1", lbuf);
1695 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1696 AddCodeLine ("clc");
1697 AddCodeLine ("adc %s", lbuf);
1698 AddCodeLine ("sta %s", lbuf);
1700 unsigned L = GetLocalLabel ();
1701 AddCodeLine ("bcc %s", LocalLabelName (L));
1702 AddCodeLine ("inc %s+1", lbuf);
1704 AddCodeLine ("ldx %s+1", lbuf);
1706 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1707 AddCodeLine ("adc %s+1", lbuf);
1708 AddCodeLine ("sta %s+1", lbuf);
1709 AddCodeLine ("tax");
1710 AddCodeLine ("lda %s", lbuf);
1714 AddCodeLine ("clc");
1715 AddCodeLine ("adc %s", lbuf);
1716 AddCodeLine ("sta %s", lbuf);
1717 AddCodeLine ("txa");
1718 AddCodeLine ("adc %s+1", lbuf);
1719 AddCodeLine ("sta %s+1", lbuf);
1720 AddCodeLine ("tax");
1721 AddCodeLine ("lda %s", lbuf);
1726 if (flags & CF_CONST) {
1728 AddCodeLine ("ldy #<(%s)", lbuf);
1729 AddCodeLine ("sty ptr1");
1730 AddCodeLine ("ldy #>(%s)", lbuf);
1732 AddCodeLine ("jsr laddeq1");
1734 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1735 AddCodeLine ("jsr laddeqa");
1738 g_getstatic (flags, label, offs);
1740 g_putstatic (flags, label, offs);
1743 AddCodeLine ("ldy #<(%s)", lbuf);
1744 AddCodeLine ("sty ptr1");
1745 AddCodeLine ("ldy #>(%s)", lbuf);
1746 AddCodeLine ("jsr laddeq");
1757 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1758 /* Emit += for a local variable */
1760 /* Calculate the true offset, check it, load it into Y */
1762 CheckLocalOffs (offs);
1764 /* Check the size and determine operation */
1765 switch (flags & CF_TYPE) {
1768 if (flags & CF_FORCECHAR) {
1770 AddCodeLine ("ldx #$00");
1771 if (flags & CF_CONST) {
1772 AddCodeLine ("clc");
1773 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1774 AddCodeLine ("adc (sp),y");
1775 AddCodeLine ("sta (sp),y");
1777 AddCodeLine ("clc");
1778 AddCodeLine ("adc (sp),y");
1779 AddCodeLine ("sta (sp),y");
1781 if ((flags & CF_UNSIGNED) == 0) {
1782 unsigned L = GetLocalLabel();
1783 AddCodeLine ("bpl %s", LocalLabelName (L));
1784 AddCodeLine ("dex");
1793 if (flags & CF_CONST) {
1794 if (CodeSizeFactor >= 400) {
1795 AddCodeLine ("clc");
1796 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1797 AddCodeLine ("adc (sp),y");
1798 AddCodeLine ("sta (sp),y");
1799 AddCodeLine ("iny");
1800 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1801 AddCodeLine ("adc (sp),y");
1802 AddCodeLine ("sta (sp),y");
1803 AddCodeLine ("tax");
1804 AddCodeLine ("dey");
1805 AddCodeLine ("lda (sp),y");
1807 g_getimmed (flags, val, 0);
1808 AddCodeLine ("jsr addeqysp");
1811 AddCodeLine ("jsr addeqysp");
1816 if (flags & CF_CONST) {
1817 g_getimmed (flags, val, 0);
1820 AddCodeLine ("jsr laddeqysp");
1830 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1831 /* Emit += for the location with address in ax */
1833 /* If the offset is too large for a byte register, add the high byte
1834 * of the offset to the primary. Beware: We need a special correction
1835 * if the offset in the low byte will overflow in the operation.
1837 offs = MakeByteOffs (flags, offs);
1839 /* Check the size and determine operation */
1840 switch (flags & CF_TYPE) {
1843 AddCodeLine ("sta ptr1");
1844 AddCodeLine ("stx ptr1+1");
1845 AddCodeLine ("ldy #$%02X", offs);
1846 AddCodeLine ("ldx #$00");
1847 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1848 AddCodeLine ("clc");
1849 AddCodeLine ("adc (ptr1),y");
1850 AddCodeLine ("sta (ptr1),y");
1854 if (CodeSizeFactor >= 200) {
1855 /* Lots of code, use only if size is not important */
1856 AddCodeLine ("sta ptr1");
1857 AddCodeLine ("stx ptr1+1");
1858 AddCodeLine ("ldy #$%02X", offs);
1859 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1860 AddCodeLine ("clc");
1861 AddCodeLine ("adc (ptr1),y");
1862 AddCodeLine ("sta (ptr1),y");
1863 AddCodeLine ("pha");
1864 AddCodeLine ("iny");
1865 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1866 AddCodeLine ("adc (ptr1),y");
1867 AddCodeLine ("sta (ptr1),y");
1868 AddCodeLine ("tax");
1869 AddCodeLine ("pla");
1875 AddCodeLine ("jsr pushax"); /* Push the address */
1876 push (CF_PTR); /* Correct the internal sp */
1877 g_getind (flags, offs); /* Fetch the value */
1878 g_inc (flags, val); /* Increment value in primary */
1879 g_putind (flags, offs); /* Store the value back */
1889 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1891 /* Emit -= for a static variable */
1893 /* Create the correct label name */
1894 const char* lbuf = GetLabelName (flags, label, offs);
1896 /* Check the size and determine operation */
1897 switch (flags & CF_TYPE) {
1900 if (flags & CF_FORCECHAR) {
1901 AddCodeLine ("ldx #$00");
1902 if (flags & CF_CONST) {
1904 AddCodeLine ("dec %s", lbuf);
1905 AddCodeLine ("lda %s", lbuf);
1907 AddCodeLine ("sec");
1908 AddCodeLine ("lda %s", lbuf);
1909 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1910 AddCodeLine ("sta %s", lbuf);
1913 AddCodeLine ("eor #$FF");
1914 AddCodeLine ("sec");
1915 AddCodeLine ("adc %s", lbuf);
1916 AddCodeLine ("sta %s", lbuf);
1918 if ((flags & CF_UNSIGNED) == 0) {
1919 unsigned L = GetLocalLabel();
1920 AddCodeLine ("bpl %s", LocalLabelName (L));
1921 AddCodeLine ("dex");
1929 AddCodeLine ("sec");
1930 if (flags & CF_CONST) {
1931 AddCodeLine ("lda %s", lbuf);
1932 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1933 AddCodeLine ("sta %s", lbuf);
1935 unsigned L = GetLocalLabel ();
1936 AddCodeLine ("bcs %s", LocalLabelName (L));
1937 AddCodeLine ("dec %s+1", lbuf);
1939 AddCodeLine ("ldx %s+1", lbuf);
1941 AddCodeLine ("lda %s+1", lbuf);
1942 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1943 AddCodeLine ("sta %s+1", lbuf);
1944 AddCodeLine ("tax");
1945 AddCodeLine ("lda %s", lbuf);
1948 AddCodeLine ("eor #$FF");
1949 AddCodeLine ("adc %s", lbuf);
1950 AddCodeLine ("sta %s", lbuf);
1951 AddCodeLine ("txa");
1952 AddCodeLine ("eor #$FF");
1953 AddCodeLine ("adc %s+1", lbuf);
1954 AddCodeLine ("sta %s+1", lbuf);
1955 AddCodeLine ("tax");
1956 AddCodeLine ("lda %s", lbuf);
1961 if (flags & CF_CONST) {
1963 AddCodeLine ("ldy #<(%s)", lbuf);
1964 AddCodeLine ("sty ptr1");
1965 AddCodeLine ("ldy #>(%s)", lbuf);
1966 AddCodeLine ("lda #$%02X", (unsigned char)val);
1967 AddCodeLine ("jsr lsubeqa");
1969 g_getstatic (flags, label, offs);
1971 g_putstatic (flags, label, offs);
1974 AddCodeLine ("ldy #<(%s)", lbuf);
1975 AddCodeLine ("sty ptr1");
1976 AddCodeLine ("ldy #>(%s)", lbuf);
1977 AddCodeLine ("jsr lsubeq");
1988 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1989 /* Emit -= for a local variable */
1991 /* Calculate the true offset, check it, load it into Y */
1993 CheckLocalOffs (offs);
1995 /* Check the size and determine operation */
1996 switch (flags & CF_TYPE) {
1999 if (flags & CF_FORCECHAR) {
2001 AddCodeLine ("ldx #$00");
2002 AddCodeLine ("sec");
2003 if (flags & CF_CONST) {
2004 AddCodeLine ("lda (sp),y");
2005 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2007 AddCodeLine ("eor #$FF");
2008 AddCodeLine ("adc (sp),y");
2010 AddCodeLine ("sta (sp),y");
2011 if ((flags & CF_UNSIGNED) == 0) {
2012 unsigned L = GetLocalLabel();
2013 AddCodeLine ("bpl %s", LocalLabelName (L));
2014 AddCodeLine ("dex");
2022 if (flags & CF_CONST) {
2023 g_getimmed (flags, val, 0);
2026 AddCodeLine ("jsr subeqysp");
2030 if (flags & CF_CONST) {
2031 g_getimmed (flags, val, 0);
2034 AddCodeLine ("jsr lsubeqysp");
2044 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2045 /* Emit -= for the location with address in ax */
2047 /* If the offset is too large for a byte register, add the high byte
2048 * of the offset to the primary. Beware: We need a special correction
2049 * if the offset in the low byte will overflow in the operation.
2051 offs = MakeByteOffs (flags, offs);
2053 /* Check the size and determine operation */
2054 switch (flags & CF_TYPE) {
2057 AddCodeLine ("sta ptr1");
2058 AddCodeLine ("stx ptr1+1");
2059 AddCodeLine ("ldy #$%02X", offs);
2060 AddCodeLine ("ldx #$00");
2061 AddCodeLine ("lda (ptr1),y");
2062 AddCodeLine ("sec");
2063 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2064 AddCodeLine ("sta (ptr1),y");
2068 if (CodeSizeFactor >= 200) {
2069 /* Lots of code, use only if size is not important */
2070 AddCodeLine ("sta ptr1");
2071 AddCodeLine ("stx ptr1+1");
2072 AddCodeLine ("ldy #$%02X", offs);
2073 AddCodeLine ("lda (ptr1),y");
2074 AddCodeLine ("sec");
2075 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2076 AddCodeLine ("sta (ptr1),y");
2077 AddCodeLine ("pha");
2078 AddCodeLine ("iny");
2079 AddCodeLine ("lda (ptr1),y");
2080 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2081 AddCodeLine ("sta (ptr1),y");
2082 AddCodeLine ("tax");
2083 AddCodeLine ("pla");
2089 AddCodeLine ("jsr pushax"); /* Push the address */
2090 push (CF_PTR); /* Correct the internal sp */
2091 g_getind (flags, offs); /* Fetch the value */
2092 g_dec (flags, val); /* Increment value in primary */
2093 g_putind (flags, offs); /* Store the value back */
2103 /*****************************************************************************/
2104 /* Add a variable address to the value in ax */
2105 /*****************************************************************************/
2109 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2110 /* Add the address of a local variable to ax */
2114 /* Add the offset */
2117 /* We cannot address more then 256 bytes of locals anyway */
2118 L = GetLocalLabel();
2119 CheckLocalOffs (offs);
2120 AddCodeLine ("clc");
2121 AddCodeLine ("adc #$%02X", offs & 0xFF);
2122 /* Do also skip the CLC insn below */
2123 AddCodeLine ("bcc %s", LocalLabelName (L));
2124 AddCodeLine ("inx");
2127 /* Add the current stackpointer value */
2128 AddCodeLine ("clc");
2130 /* Label was used above */
2133 AddCodeLine ("adc sp");
2134 AddCodeLine ("tay");
2135 AddCodeLine ("txa");
2136 AddCodeLine ("adc sp+1");
2137 AddCodeLine ("tax");
2138 AddCodeLine ("tya");
2143 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2144 /* Add the address of a static variable to ax */
2146 /* Create the correct label name */
2147 const char* lbuf = GetLabelName (flags, label, offs);
2149 /* Add the address to the current ax value */
2150 AddCodeLine ("clc");
2151 AddCodeLine ("adc #<(%s)", lbuf);
2152 AddCodeLine ("tay");
2153 AddCodeLine ("txa");
2154 AddCodeLine ("adc #>(%s)", lbuf);
2155 AddCodeLine ("tax");
2156 AddCodeLine ("tya");
2161 /*****************************************************************************/
2163 /*****************************************************************************/
2167 void g_save (unsigned flags)
2168 /* Copy primary register to hold register. */
2170 /* Check the size and determine operation */
2171 switch (flags & CF_TYPE) {
2174 if (flags & CF_FORCECHAR) {
2175 AddCodeLine ("pha");
2181 AddCodeLine ("sta regsave");
2182 AddCodeLine ("stx regsave+1");
2186 AddCodeLine ("jsr saveeax");
2196 void g_restore (unsigned flags)
2197 /* Copy hold register to primary. */
2199 /* Check the size and determine operation */
2200 switch (flags & CF_TYPE) {
2203 if (flags & CF_FORCECHAR) {
2204 AddCodeLine ("pla");
2210 AddCodeLine ("lda regsave");
2211 AddCodeLine ("ldx regsave+1");
2215 AddCodeLine ("jsr resteax");
2225 void g_cmp (unsigned flags, unsigned long val)
2226 /* Immidiate compare. The primary register will not be changed, Z flag
2232 /* Check the size and determine operation */
2233 switch (flags & CF_TYPE) {
2236 if (flags & CF_FORCECHAR) {
2237 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2243 L = GetLocalLabel();
2244 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2245 AddCodeLine ("bne %s", LocalLabelName (L));
2246 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2251 Internal ("g_cmp: Long compares not implemented");
2261 static void oper (unsigned flags, unsigned long val, char** subs)
2262 /* Encode a binary operation. subs is a pointer to four groups of three
2264 * 0-2 --> Operate on ints
2265 * 3-5 --> Operate on unsigneds
2266 * 6-8 --> Operate on longs
2267 * 9-11 --> Operate on unsigned longs
2269 * The first subroutine names in each string group is used to encode an
2270 * operation with a zero constant, the second to encode an operation with
2271 * a 8 bit constant, and the third is used in all other cases.
2276 /* Determine the offset into the array */
2277 offs = (flags & CF_UNSIGNED)? 3 : 0;
2278 switch (flags & CF_TYPE) {
2291 /* Encode the operation */
2292 if (flags & CF_CONST) {
2293 /* Constant value given */
2294 if (val == 0 && subs [offs+0]) {
2295 /* Special case: constant with value zero */
2296 AddCodeLine ("jsr %s", subs [offs+0]);
2297 } else if (val < 0x100 && subs [offs+1]) {
2298 /* Special case: constant with high byte zero */
2299 ldaconst (val); /* Load low byte */
2300 AddCodeLine ("jsr %s", subs [offs+1]);
2302 /* Others: arbitrary constant value */
2303 g_getimmed (flags, val, 0); /* Load value */
2304 AddCodeLine ("jsr %s", subs [offs+2]);
2307 /* Value not constant (is already in (e)ax) */
2308 AddCodeLine ("jsr %s", subs [offs+2]);
2311 /* The operation will pop it's argument */
2317 void g_test (unsigned flags)
2318 /* Test the value in the primary and set the condition codes */
2320 switch (flags & CF_TYPE) {
2323 if (flags & CF_FORCECHAR) {
2324 AddCodeLine ("tax");
2330 AddCodeLine ("stx tmp1");
2331 AddCodeLine ("ora tmp1");
2335 if (flags & CF_UNSIGNED) {
2336 AddCodeLine ("jsr utsteax");
2338 AddCodeLine ("jsr tsteax");
2350 void g_push (unsigned flags, unsigned long val)
2351 /* Push the primary register or a constant value onto the stack */
2353 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2355 /* We have a constant 8 or 16 bit value */
2356 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2358 /* Handle as 8 bit value */
2360 AddCodeLine ("jsr pusha");
2364 /* Handle as 16 bit value */
2365 g_getimmed (flags, val, 0);
2366 AddCodeLine ("jsr pushax");
2371 /* Value is not 16 bit or not constant */
2372 if (flags & CF_CONST) {
2373 /* Constant 32 bit value, load into eax */
2374 g_getimmed (flags, val, 0);
2377 /* Push the primary register */
2378 switch (flags & CF_TYPE) {
2381 if (flags & CF_FORCECHAR) {
2382 /* Handle as char */
2383 AddCodeLine ("jsr pusha");
2388 AddCodeLine ("jsr pushax");
2392 AddCodeLine ("jsr pusheax");
2402 /* Adjust the stack offset */
2408 void g_swap (unsigned flags)
2409 /* Swap the primary register and the top of the stack. flags give the type
2410 * of *both* values (must have same size).
2413 switch (flags & CF_TYPE) {
2417 AddCodeLine ("jsr swapstk");
2421 AddCodeLine ("jsr swapestk");
2432 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2433 /* Call the specified subroutine name */
2435 if ((Flags & CF_FIXARGC) == 0) {
2436 /* Pass the argument count */
2439 AddCodeLine ("jsr _%s", Label);
2440 oursp += ArgSize; /* callee pops args */
2445 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2446 /* Call subroutine indirect */
2448 if ((Flags & CF_LOCAL) == 0) {
2449 /* Address is in a/x */
2450 if ((Flags & CF_FIXARGC) == 0) {
2451 /* Pass arg count */
2454 AddCodeLine ("jsr callax");
2456 /* The address is on stack, offset is on Val */
2458 CheckLocalOffs (Offs);
2459 AddCodeLine ("pha");
2460 AddCodeLine ("ldy #$%02X", Offs);
2461 AddCodeLine ("lda (sp),y");
2462 AddCodeLine ("sta jmpvec+1");
2463 AddCodeLine ("iny");
2464 AddCodeLine ("lda (sp),y");
2465 AddCodeLine ("sta jmpvec+2");
2466 AddCodeLine ("pla");
2467 AddCodeLine ("jsr jmpvec");
2470 /* Callee pops args */
2476 void g_jump (unsigned Label)
2477 /* Jump to specified internal label number */
2479 AddCodeLine ("jmp %s", LocalLabelName (Label));
2484 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2485 /* Jump to label if zero flag clear */
2487 AddCodeLine ("jne %s", LocalLabelName (label));
2492 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2493 /* Jump to label if zero flag set */
2495 AddCodeLine ("jeq %s", LocalLabelName (label));
2500 static void mod_internal (int k, char* verb1, char* verb2)
2503 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2507 AddCodeLine ("jsr %ssp", verb2);
2513 void g_space (int space)
2514 /* Create or drop space on the stack */
2517 mod_internal (-space, "inc", "addy");
2518 } else if (space > 0) {
2519 mod_internal (space, "dec", "suby");
2525 void g_cstackcheck (void)
2526 /* Check for a C stack overflow */
2528 AddCodeLine ("jsr cstkchk");
2533 void g_stackcheck (void)
2534 /* Check for a stack overflow */
2536 AddCodeLine ("jsr stkchk");
2541 void g_add (unsigned flags, unsigned long val)
2542 /* Primary = TOS + Primary */
2544 static char* ops [12] = {
2545 0, "tosadda0", "tosaddax",
2546 0, "tosadda0", "tosaddax",
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_sub (unsigned flags, unsigned long val)
2561 /* Primary = TOS - Primary */
2563 static char* ops [12] = {
2564 0, "tossuba0", "tossubax",
2565 0, "tossuba0", "tossubax",
2570 if (flags & CF_CONST) {
2571 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2572 g_push (flags & ~CF_CONST, 0);
2574 oper (flags, val, ops);
2579 void g_rsub (unsigned flags, unsigned long val)
2580 /* Primary = Primary - TOS */
2582 static char* ops [12] = {
2583 0, "tosrsuba0", "tosrsubax",
2584 0, "tosrsuba0", "tosrsubax",
2588 oper (flags, val, ops);
2593 void g_mul (unsigned flags, unsigned long val)
2594 /* Primary = TOS * Primary */
2596 static char* ops [12] = {
2597 0, "tosmula0", "tosmulax",
2598 0, "tosumula0", "tosumulax",
2605 /* Do strength reduction if the value is constant and a power of two */
2606 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2607 /* Generate a shift instead */
2612 /* If the right hand side is const, the lhs is not on stack but still
2613 * in the primary register.
2615 if (flags & CF_CONST) {
2617 switch (flags & CF_TYPE) {
2620 if (flags & CF_FORCECHAR) {
2621 /* Handle some special cases */
2625 AddCodeLine ("sta tmp1");
2626 AddCodeLine ("asl a");
2627 AddCodeLine ("clc");
2628 AddCodeLine ("adc tmp1");
2632 AddCodeLine ("sta tmp1");
2633 AddCodeLine ("asl a");
2634 AddCodeLine ("asl a");
2635 AddCodeLine ("clc");
2636 AddCodeLine ("adc tmp1");
2640 AddCodeLine ("sta tmp1");
2641 AddCodeLine ("asl a");
2642 AddCodeLine ("clc");
2643 AddCodeLine ("adc tmp1");
2644 AddCodeLine ("asl a");
2648 AddCodeLine ("sta tmp1");
2649 AddCodeLine ("asl a");
2650 AddCodeLine ("asl a");
2651 AddCodeLine ("clc");
2652 AddCodeLine ("adc tmp1");
2653 AddCodeLine ("asl a");
2662 AddCodeLine ("jsr mulax3");
2665 AddCodeLine ("jsr mulax5");
2668 AddCodeLine ("jsr mulax6");
2671 AddCodeLine ("jsr mulax7");
2674 AddCodeLine ("jsr mulax9");
2677 AddCodeLine ("jsr mulax10");
2689 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2690 * into the normal, non-optimized stuff.
2692 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2693 g_push (flags & ~CF_CONST, 0);
2697 /* Use long way over the stack */
2698 oper (flags, val, ops);
2703 void g_div (unsigned flags, unsigned long val)
2704 /* Primary = TOS / Primary */
2706 static char* ops [12] = {
2707 0, "tosdiva0", "tosdivax",
2708 0, "tosudiva0", "tosudivax",
2713 /* Do strength reduction if the value is constant and a power of two */
2715 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2716 /* Generate a shift instead */
2719 /* Generate a division */
2720 if (flags & CF_CONST) {
2721 /* lhs is not on stack */
2722 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2723 g_push (flags & ~CF_CONST, 0);
2725 oper (flags, val, ops);
2731 void g_mod (unsigned flags, unsigned long val)
2732 /* Primary = TOS % Primary */
2734 static char* ops [12] = {
2735 0, "tosmoda0", "tosmodax",
2736 0, "tosumoda0", "tosumodax",
2742 /* Check if we can do some cost reduction */
2743 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2744 /* We can do that with an AND operation */
2745 g_and (flags, val - 1);
2747 /* Do it the hard way... */
2748 if (flags & CF_CONST) {
2749 /* lhs is not on stack */
2750 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2751 g_push (flags & ~CF_CONST, 0);
2753 oper (flags, val, ops);
2759 void g_or (unsigned flags, unsigned long val)
2760 /* Primary = TOS | Primary */
2762 static char* ops [12] = {
2763 0, "tosora0", "tosorax",
2764 0, "tosora0", "tosorax",
2769 /* If the right hand side is const, the lhs is not on stack but still
2770 * in the primary register.
2772 if (flags & CF_CONST) {
2774 switch (flags & CF_TYPE) {
2777 if (flags & CF_FORCECHAR) {
2778 if ((val & 0xFF) != 0) {
2779 AddCodeLine ("ora #$%02X", (unsigned char)val);
2787 if ((val & 0xFF) != 0) {
2788 AddCodeLine ("ora #$%02X", (unsigned char)val);
2791 } else if ((val & 0xFF00) == 0xFF00) {
2792 if ((val & 0xFF) != 0) {
2793 AddCodeLine ("ora #$%02X", (unsigned char)val);
2802 if ((val & 0xFF) != 0) {
2803 AddCodeLine ("ora #$%02X", (unsigned char)val);
2813 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2814 * into the normal, non-optimized stuff. Note: The standard stuff will
2815 * always work with ints.
2817 flags &= ~CF_FORCECHAR;
2818 g_push (flags & ~CF_CONST, 0);
2821 /* Use long way over the stack */
2822 oper (flags, val, ops);
2827 void g_xor (unsigned flags, unsigned long val)
2828 /* Primary = TOS ^ Primary */
2830 static char* ops [12] = {
2831 0, "tosxora0", "tosxorax",
2832 0, "tosxora0", "tosxorax",
2838 /* If the right hand side is const, the lhs is not on stack but still
2839 * in the primary register.
2841 if (flags & CF_CONST) {
2843 switch (flags & CF_TYPE) {
2846 if (flags & CF_FORCECHAR) {
2847 if ((val & 0xFF) != 0) {
2848 AddCodeLine ("eor #$%02X", (unsigned char)val);
2857 AddCodeLine ("eor #$%02X", (unsigned char)val);
2860 } else if ((val & 0xFF) == 0) {
2861 AddCodeLine ("pha");
2862 AddCodeLine ("txa");
2863 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2864 AddCodeLine ("tax");
2865 AddCodeLine ("pla");
2873 AddCodeLine ("eor #$%02X", (unsigned char)val);
2883 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2884 * into the normal, non-optimized stuff. Note: The standard stuff will
2885 * always work with ints.
2887 flags &= ~CF_FORCECHAR;
2888 g_push (flags & ~CF_CONST, 0);
2891 /* Use long way over the stack */
2892 oper (flags, val, ops);
2897 void g_and (unsigned Flags, unsigned long Val)
2898 /* Primary = TOS & Primary */
2900 static char* ops [12] = {
2901 0, "tosanda0", "tosandax",
2902 0, "tosanda0", "tosandax",
2907 /* If the right hand side is const, the lhs is not on stack but still
2908 * in the primary register.
2910 if (Flags & CF_CONST) {
2912 switch (Flags & CF_TYPE) {
2915 if (Flags & CF_FORCECHAR) {
2916 if ((Val & 0xFF) != 0xFF) {
2917 AddCodeLine ("and #$%02X", (unsigned char)Val);
2923 if ((Val & 0xFFFF) != 0xFFFF) {
2928 } else if (Val != 0xFF) {
2929 AddCodeLine ("and #$%02X", (unsigned char)Val);
2931 } else if ((Val & 0xFF00) == 0xFF00) {
2932 AddCodeLine ("and #$%02X", (unsigned char)Val);
2933 } else if ((Val & 0x00FF) == 0x0000) {
2934 AddCodeLine ("txa");
2935 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2936 AddCodeLine ("tax");
2939 AddCodeLine ("tay");
2940 AddCodeLine ("txa");
2941 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2942 AddCodeLine ("tax");
2943 AddCodeLine ("tya");
2944 if ((Val & 0x00FF) != 0x00FF) {
2945 AddCodeLine ("and #$%02X", (unsigned char)Val);
2954 AddCodeLine ("stx sreg+1");
2955 AddCodeLine ("stx sreg");
2956 if ((Val & 0xFF) != 0xFF) {
2957 AddCodeLine ("and #$%02X", (unsigned char)Val);
2960 } else if (Val == 0xFF00) {
2962 AddCodeLine ("sta sreg+1");
2963 AddCodeLine ("sta sreg");
2972 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2973 * into the normal, non-optimized stuff. Note: The standard stuff will
2974 * always work with ints.
2976 Flags &= ~CF_FORCECHAR;
2977 g_push (Flags & ~CF_CONST, 0);
2980 /* Use long way over the stack */
2981 oper (Flags, Val, ops);
2986 void g_asr (unsigned flags, unsigned long val)
2987 /* Primary = TOS >> Primary */
2989 static char* ops [12] = {
2990 0, "tosasra0", "tosasrax",
2991 0, "tosshra0", "tosshrax",
2996 /* If the right hand side is const, the lhs is not on stack but still
2997 * in the primary register.
2999 if (flags & CF_CONST) {
3001 switch (flags & CF_TYPE) {
3005 if (val >= 8 && (flags & CF_UNSIGNED)) {
3006 AddCodeLine ("txa");
3013 } else if (val >= 1 && val <= 4) {
3014 if (flags & CF_UNSIGNED) {
3015 AddCodeLine ("jsr shrax%ld", val);
3017 AddCodeLine ("jsr asrax%ld", val);
3027 } else if (val >= 1 && val <= 4) {
3028 if (flags & CF_UNSIGNED) {
3029 AddCodeLine ("jsr shreax%ld", val);
3031 AddCodeLine ("jsr asreax%ld", val);
3034 } else if (val == 8 && (flags & CF_UNSIGNED)) {
3035 AddCodeLine ("txa");
3036 AddCodeLine ("ldx sreg");
3037 AddCodeLine ("ldy sreg+1");
3038 AddCodeLine ("sty sreg");
3039 AddCodeLine ("ldy #$00");
3040 AddCodeLine ("sty sreg+1");
3042 } else if (val == 16) {
3043 AddCodeLine ("ldy #$00");
3044 AddCodeLine ("ldx sreg+1");
3045 if ((flags & CF_UNSIGNED) == 0) {
3046 unsigned L = GetLocalLabel();
3047 AddCodeLine ("bpl %s", LocalLabelName (L));
3048 AddCodeLine ("dey");
3051 AddCodeLine ("lda sreg");
3052 AddCodeLine ("sty sreg+1");
3053 AddCodeLine ("sty sreg");
3062 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3063 * into the normal, non-optimized stuff. Note: The standard stuff will
3064 * always work with ints.
3066 flags &= ~CF_FORCECHAR;
3067 g_push (flags & ~CF_CONST, 0);
3070 /* Use long way over the stack */
3071 oper (flags, val, ops);
3076 void g_asl (unsigned flags, unsigned long val)
3077 /* Primary = TOS << Primary */
3079 static char* ops [12] = {
3080 0, "tosasla0", "tosaslax",
3081 0, "tosshla0", "tosshlax",
3087 /* If the right hand side is const, the lhs is not on stack but still
3088 * in the primary register.
3090 if (flags & CF_CONST) {
3092 switch (flags & CF_TYPE) {
3097 AddCodeLine ("tax");
3098 AddCodeLine ("lda #$00");
3104 } else if (val >= 1 && val <= 4) {
3105 if (flags & CF_UNSIGNED) {
3106 AddCodeLine ("jsr shlax%ld", val);
3108 AddCodeLine ("jsr aslax%ld", val);
3118 } else if (val >= 1 && val <= 4) {
3119 if (flags & CF_UNSIGNED) {
3120 AddCodeLine ("jsr shleax%ld", val);
3122 AddCodeLine ("jsr asleax%ld", val);
3125 } else if (val == 8) {
3126 AddCodeLine ("ldy sreg");
3127 AddCodeLine ("sty sreg+1");
3128 AddCodeLine ("stx sreg");
3129 AddCodeLine ("tax");
3130 AddCodeLine ("lda #$00");
3132 } else if (val == 16) {
3133 AddCodeLine ("stx sreg+1");
3134 AddCodeLine ("sta sreg");
3135 AddCodeLine ("lda #$00");
3136 AddCodeLine ("tax");
3145 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3146 * into the normal, non-optimized stuff. Note: The standard stuff will
3147 * always work with ints.
3149 flags &= ~CF_FORCECHAR;
3150 g_push (flags & ~CF_CONST, 0);
3153 /* Use long way over the stack */
3154 oper (flags, val, ops);
3159 void g_neg (unsigned Flags)
3160 /* Primary = -Primary */
3162 switch (Flags & CF_TYPE) {
3165 if (Flags & CF_FORCECHAR) {
3166 AddCodeLine ("eor #$FF");
3167 AddCodeLine ("clc");
3168 AddCodeLine ("adc #$01");
3174 AddCodeLine ("jsr negax");
3178 AddCodeLine ("jsr negeax");
3188 void g_bneg (unsigned flags)
3189 /* Primary = !Primary */
3191 switch (flags & CF_TYPE) {
3194 AddCodeLine ("jsr bnega");
3198 AddCodeLine ("jsr bnegax");
3202 AddCodeLine ("jsr bnegeax");
3212 void g_com (unsigned Flags)
3213 /* Primary = ~Primary */
3215 switch (Flags & CF_TYPE) {
3218 if (Flags & CF_FORCECHAR) {
3219 AddCodeLine ("eor #$FF");
3225 AddCodeLine ("jsr complax");
3229 AddCodeLine ("jsr compleax");
3239 void g_inc (unsigned flags, unsigned long val)
3240 /* Increment the primary register by a given number */
3242 /* Don't inc by zero */
3247 /* Generate code for the supported types */
3249 switch (flags & CF_TYPE) {
3252 if (flags & CF_FORCECHAR) {
3253 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3255 AddCodeLine ("ina");
3258 AddCodeLine ("clc");
3259 AddCodeLine ("adc #$%02X", (unsigned char)val);
3266 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) {
3267 unsigned L = GetLocalLabel();
3268 AddCodeLine ("ina");
3269 AddCodeLine ("bne %s", LocalLabelName (L));
3270 AddCodeLine ("inx");
3272 } else if (CodeSizeFactor < 200) {
3275 AddCodeLine ("jsr incax%lu", val);
3276 } else if (val <= 255) {
3278 AddCodeLine ("jsr incaxy");
3280 g_add (flags | CF_CONST, val);
3283 /* Inline the code */
3285 if ((val & 0xFF) != 0) {
3286 unsigned L = GetLocalLabel();
3287 AddCodeLine ("clc");
3288 AddCodeLine ("adc #$%02X", (unsigned char) val);
3289 AddCodeLine ("bcc %s", LocalLabelName (L));
3290 AddCodeLine ("inx");
3294 AddCodeLine ("inx");
3297 AddCodeLine ("inx");
3300 AddCodeLine ("inx");
3303 AddCodeLine ("clc");
3304 if ((val & 0xFF) != 0) {
3305 AddCodeLine ("adc #$%02X", (unsigned char) val);
3307 AddCodeLine ("pha");
3308 AddCodeLine ("txa");
3309 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3310 AddCodeLine ("tax");
3311 AddCodeLine ("pla");
3319 AddCodeLine ("jsr inceaxy");
3321 g_add (flags | CF_CONST, val);
3333 void g_dec (unsigned flags, unsigned long val)
3334 /* Decrement the primary register by a given number */
3336 /* Don't dec by zero */
3341 /* Generate code for the supported types */
3343 switch (flags & CF_TYPE) {
3346 if (flags & CF_FORCECHAR) {
3347 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3349 AddCodeLine ("dea");
3352 AddCodeLine ("sec");
3353 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3360 if (CodeSizeFactor < 200) {
3361 /* Use subroutines */
3363 AddCodeLine ("jsr decax%d", (int) val);
3364 } else if (val <= 255) {
3366 AddCodeLine ("jsr decaxy");
3368 g_sub (flags | CF_CONST, val);
3371 /* Inline the code */
3373 if ((val & 0xFF) != 0) {
3374 unsigned L = GetLocalLabel();
3375 AddCodeLine ("sec");
3376 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3377 AddCodeLine ("bcs %s", LocalLabelName (L));
3378 AddCodeLine ("dex");
3382 AddCodeLine ("dex");
3385 AddCodeLine ("dex");
3388 AddCodeLine ("sec");
3389 if ((val & 0xFF) != 0) {
3390 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3392 AddCodeLine ("pha");
3393 AddCodeLine ("txa");
3394 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3395 AddCodeLine ("tax");
3396 AddCodeLine ("pla");
3404 AddCodeLine ("jsr deceaxy");
3406 g_sub (flags | CF_CONST, val);
3419 * Following are the conditional operators. They compare the TOS against
3420 * the primary and put a literal 1 in the primary if the condition is
3421 * true, otherwise they clear the primary register
3426 void g_eq (unsigned flags, unsigned long val)
3427 /* Test for equal */
3429 static char* ops [12] = {
3430 "toseq00", "toseqa0", "toseqax",
3431 "toseq00", "toseqa0", "toseqax",
3438 /* If the right hand side is const, the lhs is not on stack but still
3439 * in the primary register.
3441 if (flags & CF_CONST) {
3443 switch (flags & CF_TYPE) {
3446 if (flags & CF_FORCECHAR) {
3447 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3448 AddCodeLine ("jsr booleq");
3454 L = GetLocalLabel();
3455 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3456 AddCodeLine ("bne %s", LocalLabelName (L));
3457 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3459 AddCodeLine ("jsr booleq");
3469 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3470 * into the normal, non-optimized stuff. Note: The standard stuff will
3471 * always work with ints.
3473 flags &= ~CF_FORCECHAR;
3474 g_push (flags & ~CF_CONST, 0);
3477 /* Use long way over the stack */
3478 oper (flags, val, ops);
3483 void g_ne (unsigned flags, unsigned long val)
3484 /* Test for not equal */
3486 static char* ops [12] = {
3487 "tosne00", "tosnea0", "tosneax",
3488 "tosne00", "tosnea0", "tosneax",
3495 /* If the right hand side is const, the lhs is not on stack but still
3496 * in the primary register.
3498 if (flags & CF_CONST) {
3500 switch (flags & CF_TYPE) {
3503 if (flags & CF_FORCECHAR) {
3504 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3505 AddCodeLine ("jsr boolne");
3511 L = GetLocalLabel();
3512 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3513 AddCodeLine ("bne %s", LocalLabelName (L));
3514 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3516 AddCodeLine ("jsr boolne");
3526 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3527 * into the normal, non-optimized stuff. Note: The standard stuff will
3528 * always work with ints.
3530 flags &= ~CF_FORCECHAR;
3531 g_push (flags & ~CF_CONST, 0);
3534 /* Use long way over the stack */
3535 oper (flags, val, ops);
3540 void g_lt (unsigned flags, unsigned long val)
3541 /* Test for less than */
3543 static char* ops [12] = {
3544 "toslt00", "toslta0", "tosltax",
3545 "tosult00", "tosulta0", "tosultax",
3550 /* If the right hand side is const, the lhs is not on stack but still
3551 * in the primary register.
3553 if (flags & CF_CONST) {
3555 /* Because the handling of the overflow flag is too complex for
3556 * inlining, we can handle only unsigned compares, and signed
3557 * compares against zero here.
3559 if (flags & CF_UNSIGNED) {
3561 /* Give a warning in some special cases */
3563 Warning ("Condition is never true");
3564 AddCodeLine ("jsr return0");
3568 /* Look at the type */
3569 switch (flags & CF_TYPE) {
3572 if (flags & CF_FORCECHAR) {
3573 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3574 AddCodeLine ("jsr boolult");
3580 /* If the low byte is zero, we must only test the high byte */
3581 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3582 if ((val & 0xFF) != 0) {
3583 unsigned L = GetLocalLabel();
3584 AddCodeLine ("bne %s", LocalLabelName (L));
3585 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3588 AddCodeLine ("jsr boolult");
3592 /* Do a subtraction */
3593 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3594 AddCodeLine ("txa");
3595 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3596 AddCodeLine ("lda sreg");
3597 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3598 AddCodeLine ("lda sreg+1");
3599 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3600 AddCodeLine ("jsr boolult");
3607 } else if (val == 0) {
3609 /* Look at the type */
3610 switch (flags & CF_TYPE) {
3613 if (flags & CF_FORCECHAR) {
3614 AddCodeLine ("tax");
3615 AddCodeLine ("jsr boollt");
3621 /* Just check the high byte */
3622 AddCodeLine ("txa");
3623 AddCodeLine ("jsr boollt");
3627 /* Just check the high byte */
3628 AddCodeLine ("lda sreg+1");
3629 AddCodeLine ("jsr boollt");
3637 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3638 * into the normal, non-optimized stuff. Note: The standard stuff will
3639 * always work with ints.
3641 flags &= ~CF_FORCECHAR;
3642 g_push (flags & ~CF_CONST, 0);
3645 /* Use long way over the stack */
3646 oper (flags, val, ops);
3651 void g_le (unsigned flags, unsigned long val)
3652 /* Test for less than or equal to */
3654 static char* ops [12] = {
3655 "tosle00", "toslea0", "tosleax",
3656 "tosule00", "tosulea0", "tosuleax",
3662 /* If the right hand side is const, the lhs is not on stack but still
3663 * in the primary register.
3665 if (flags & CF_CONST) {
3667 /* Look at the type */
3668 switch (flags & CF_TYPE) {
3671 if (flags & CF_FORCECHAR) {
3672 if (flags & CF_UNSIGNED) {
3673 /* Unsigned compare */
3675 /* Use < instead of <= because the former gives
3676 * better code on the 6502 than the latter.
3678 g_lt (flags, val+1);
3681 Warning ("Condition is always true");
3682 AddCodeLine ("jsr return1");
3685 /* Signed compare */
3686 if ((long) val < 0x7F) {
3687 /* Use < instead of <= because the former gives
3688 * better code on the 6502 than the latter.
3690 g_lt (flags, val+1);
3693 Warning ("Condition is always true");
3694 AddCodeLine ("jsr return1");
3702 if (flags & CF_UNSIGNED) {
3703 /* Unsigned compare */
3705 /* Use < instead of <= because the former gives
3706 * better code on the 6502 than the latter.
3708 g_lt (flags, val+1);
3711 Warning ("Condition is always true");
3712 AddCodeLine ("jsr return1");
3715 /* Signed compare */
3716 if ((long) val < 0x7FFF) {
3717 g_lt (flags, val+1);
3720 Warning ("Condition is always true");
3721 AddCodeLine ("jsr return1");
3727 if (flags & CF_UNSIGNED) {
3728 /* Unsigned compare */
3729 if (val < 0xFFFFFFFF) {
3730 /* Use < instead of <= because the former gives
3731 * better code on the 6502 than the latter.
3733 g_lt (flags, val+1);
3736 Warning ("Condition is always true");
3737 AddCodeLine ("jsr return1");
3740 /* Signed compare */
3741 if ((long) val < 0x7FFFFFFF) {
3742 g_lt (flags, val+1);
3745 Warning ("Condition is always true");
3746 AddCodeLine ("jsr return1");
3755 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3756 * into the normal, non-optimized stuff. Note: The standard stuff will
3757 * always work with ints.
3759 flags &= ~CF_FORCECHAR;
3760 g_push (flags & ~CF_CONST, 0);
3763 /* Use long way over the stack */
3764 oper (flags, val, ops);
3769 void g_gt (unsigned flags, unsigned long val)
3770 /* Test for greater than */
3772 static char* ops [12] = {
3773 "tosgt00", "tosgta0", "tosgtax",
3774 "tosugt00", "tosugta0", "tosugtax",
3780 /* If the right hand side is const, the lhs is not on stack but still
3781 * in the primary register.
3783 if (flags & CF_CONST) {
3785 /* Look at the type */
3786 switch (flags & CF_TYPE) {
3789 if (flags & CF_FORCECHAR) {
3790 if (flags & CF_UNSIGNED) {
3792 /* If we have a compare > 0, we will replace it by
3793 * != 0 here, since both are identical but the
3794 * latter is easier to optimize.
3797 } else if (val < 0xFF) {
3798 /* Use >= instead of > because the former gives
3799 * better code on the 6502 than the latter.
3801 g_ge (flags, val+1);
3804 Warning ("Condition is never true");
3805 AddCodeLine ("jsr return0");
3808 if ((long) val < 0x7F) {
3809 /* Use >= instead of > because the former gives
3810 * better code on the 6502 than the latter.
3812 g_ge (flags, val+1);
3815 Warning ("Condition is never true");
3816 AddCodeLine ("jsr return0");
3824 if (flags & CF_UNSIGNED) {
3825 /* Unsigned compare */
3827 /* If we have a compare > 0, we will replace it by
3828 * != 0 here, since both are identical but the latter
3829 * is easier to optimize.
3832 } else if (val < 0xFFFF) {
3833 /* Use >= instead of > because the former gives better
3834 * code on the 6502 than the latter.
3836 g_ge (flags, val+1);
3839 Warning ("Condition is never true");
3840 AddCodeLine ("jsr return0");
3843 /* Signed compare */
3844 if ((long) val < 0x7FFF) {
3845 g_ge (flags, val+1);
3848 Warning ("Condition is never true");
3849 AddCodeLine ("jsr return0");
3855 if (flags & CF_UNSIGNED) {
3856 /* Unsigned compare */
3858 /* If we have a compare > 0, we will replace it by
3859 * != 0 here, since both are identical but the latter
3860 * is easier to optimize.
3863 } else if (val < 0xFFFFFFFF) {
3864 /* Use >= instead of > because the former gives better
3865 * code on the 6502 than the latter.
3867 g_ge (flags, val+1);
3870 Warning ("Condition is never true");
3871 AddCodeLine ("jsr return0");
3874 /* Signed compare */
3875 if ((long) val < 0x7FFFFFFF) {
3876 g_ge (flags, val+1);
3879 Warning ("Condition is never true");
3880 AddCodeLine ("jsr return0");
3889 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3890 * into the normal, non-optimized stuff. Note: The standard stuff will
3891 * always work with ints.
3893 flags &= ~CF_FORCECHAR;
3894 g_push (flags & ~CF_CONST, 0);
3897 /* Use long way over the stack */
3898 oper (flags, val, ops);
3903 void g_ge (unsigned flags, unsigned long val)
3904 /* Test for greater than or equal to */
3906 static char* ops [12] = {
3907 "tosge00", "tosgea0", "tosgeax",
3908 "tosuge00", "tosugea0", "tosugeax",
3914 /* If the right hand side is const, the lhs is not on stack but still
3915 * in the primary register.
3917 if (flags & CF_CONST) {
3919 /* Because the handling of the overflow flag is too complex for
3920 * inlining, we can handle only unsigned compares, and signed
3921 * compares against zero here.
3923 if (flags & CF_UNSIGNED) {
3925 /* Give a warning in some special cases */
3927 Warning ("Condition is always true");
3928 AddCodeLine ("jsr return1");
3932 /* Look at the type */
3933 switch (flags & CF_TYPE) {
3936 if (flags & CF_FORCECHAR) {
3937 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3938 AddCodeLine ("jsr booluge");
3944 /* If the low byte is zero, we must only test the high byte */
3945 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3946 if ((val & 0xFF) != 0) {
3947 unsigned L = GetLocalLabel();
3948 AddCodeLine ("bne %s", LocalLabelName (L));
3949 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3952 AddCodeLine ("jsr booluge");
3956 /* Do a subtraction */
3957 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3958 AddCodeLine ("txa");
3959 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3960 AddCodeLine ("lda sreg");
3961 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3962 AddCodeLine ("lda sreg+1");
3963 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3964 AddCodeLine ("jsr booluge");
3971 } else if (val == 0) {
3973 /* Look at the type */
3974 switch (flags & CF_TYPE) {
3977 if (flags & CF_FORCECHAR) {
3978 AddCodeLine ("tax");
3979 AddCodeLine ("jsr boolge");
3985 /* Just test the high byte */
3986 AddCodeLine ("txa");
3987 AddCodeLine ("jsr boolge");
3991 /* Just test the high byte */
3992 AddCodeLine ("lda sreg+1");
3993 AddCodeLine ("jsr boolge");
4002 /* If we go here, we didn't emit code. Push the lhs on stack and fall
4003 * into the normal, non-optimized stuff. Note: The standard stuff will
4004 * always work with ints.
4006 flags &= ~CF_FORCECHAR;
4007 g_push (flags & ~CF_CONST, 0);
4010 /* Use long way over the stack */
4011 oper (flags, val, ops);
4016 /*****************************************************************************/
4017 /* Allocating static storage */
4018 /*****************************************************************************/
4022 void g_res (unsigned n)
4023 /* Reserve static storage, n bytes */
4025 AddDataLine ("\t.res\t%u,$00", n);
4030 void g_defdata (unsigned flags, unsigned long val, long offs)
4031 /* Define data with the size given in flags */
4033 if (flags & CF_CONST) {
4035 /* Numeric constant */
4036 switch (flags & CF_TYPE) {
4039 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
4043 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
4047 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
4058 /* Create the correct label name */
4059 const char* Label = GetLabelName (flags, val, offs);
4061 /* Labels are always 16 bit */
4062 AddDataLine ("\t.addr\t%s", Label);
4069 void g_defbytes (const void* Bytes, unsigned Count)
4070 /* Output a row of bytes as a constant */
4076 /* Cast the buffer pointer */
4077 const unsigned char* Data = (const unsigned char*) Bytes;
4079 /* Output the stuff */
4082 /* How many go into this line? */
4083 if ((Chunk = Count) > 16) {
4088 /* Output one line */
4089 strcpy (Buf, "\t.byte\t");
4092 B += sprintf (B, "$%02X", *Data++);
4098 /* Output the line */
4105 void g_zerobytes (unsigned Count)
4106 /* Output Count bytes of data initialized with zero */
4109 AddDataLine ("\t.res\t%u,$00", Count);
4115 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4116 /* Initialize a register variable from static initialization data */
4118 /* Register variables do always have less than 128 bytes */
4119 unsigned CodeLabel = GetLocalLabel ();
4121 g_defcodelabel (CodeLabel);
4122 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4123 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4124 AddCodeLine ("dex");
4125 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4130 void g_initauto (unsigned Label, unsigned Size)
4131 /* Initialize a local variable at stack offset zero from static data */
4133 unsigned CodeLabel = GetLocalLabel ();
4135 CheckLocalOffs (Size);
4138 g_defcodelabel (CodeLabel);
4139 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4140 AddCodeLine ("sta (sp),y");
4141 AddCodeLine ("dey");
4142 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4143 } else if (Size <= 256) {
4145 g_defcodelabel (CodeLabel);
4146 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4147 AddCodeLine ("sta (sp),y");
4148 AddCodeLine ("iny");
4149 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4150 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4156 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4157 /* Initialize a static local variable from static initialization data */
4160 unsigned CodeLabel = GetLocalLabel ();
4162 g_defcodelabel (CodeLabel);
4163 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4164 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4165 AddCodeLine ("dey");
4166 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4167 } else if (Size <= 256) {
4168 unsigned CodeLabel = GetLocalLabel ();
4170 g_defcodelabel (CodeLabel);
4171 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4172 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4173 AddCodeLine ("iny");
4174 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4175 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4177 /* Use the easy way here: memcpy */
4178 g_getimmed (CF_STATIC, VarLabel, 0);
4179 AddCodeLine ("jsr pushax");
4180 g_getimmed (CF_STATIC, InitLabel, 0);
4181 AddCodeLine ("jsr pushax");
4182 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4183 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4189 /*****************************************************************************/
4190 /* Switch statement */
4191 /*****************************************************************************/
4195 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4196 /* Generate code for a switch statement */
4198 unsigned NextLabel = 0;
4201 /* Setup registers and determine which compare insn to use */
4202 const char* Compare;
4205 Compare = "cmp #$%02X";
4208 Compare = "cpx #$%02X";
4211 AddCodeLine ("ldy sreg");
4212 Compare = "cpy #$%02X";
4215 AddCodeLine ("ldy sreg+1");
4216 Compare = "cpy #$%02X";
4219 Internal ("Invalid depth in g_switch: %u", Depth);
4222 /* Walk over all nodes */
4223 for (I = 0; I < CollCount (Nodes); ++I) {
4225 /* Get the next case node */
4226 CaseNode* N = CollAtUnchecked (Nodes, I);
4228 /* If we have a next label, define it */
4230 g_defcodelabel (NextLabel);
4234 /* Do the compare */
4235 AddCodeLine (Compare, CN_GetValue (N));
4237 /* If this is the last level, jump directly to the case code if found */
4240 /* Branch if equal */
4241 g_falsejump (0, CN_GetLabel (N));
4245 /* Determine the next label */
4246 if (I == CollCount (Nodes) - 1) {
4247 /* Last node means not found */
4248 g_truejump (0, DefaultLabel);
4250 /* Jump to the next check */
4251 NextLabel = GetLocalLabel ();
4252 g_truejump (0, NextLabel);
4255 /* Check the next level */
4256 g_switch (N->Nodes, DefaultLabel, Depth-1);
4261 /* If we go here, we haven't found the label */
4262 g_jump (DefaultLabel);
4267 /*****************************************************************************/
4268 /* User supplied assembler code */
4269 /*****************************************************************************/
4273 void g_asmcode (struct StrBuf* B)
4274 /* Output one line of assembler code. */
4276 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));
4281 /*****************************************************************************/
4282 /* Inlined known functions */
4283 /*****************************************************************************/
4287 void g_strlen (unsigned flags, unsigned long val, long offs)
4288 /* Inline the strlen() function */
4290 /* We need a label in both cases */
4291 unsigned label = GetLocalLabel ();
4293 /* Two different encodings */
4294 if (flags & CF_CONST) {
4296 /* The address of the string is constant. Create the correct label name */
4297 const char* lbuf = GetLabelName (flags, val, offs);
4299 /* Generate the strlen code */
4300 AddCodeLine ("ldy #$FF");
4301 g_defcodelabel (label);
4302 AddCodeLine ("iny");
4303 AddCodeLine ("lda %s,y", lbuf);
4304 AddCodeLine ("bne %s", LocalLabelName (label));
4305 AddCodeLine ("tax");
4306 AddCodeLine ("tya");
4310 /* Address not constant but in primary */
4311 if (CodeSizeFactor < 400) {
4312 /* This is too much code, so call strlen instead of inlining */
4313 AddCodeLine ("jsr _strlen");
4315 /* Inline the function */
4316 AddCodeLine ("sta ptr1");
4317 AddCodeLine ("stx ptr1+1");
4318 AddCodeLine ("ldy #$FF");
4319 g_defcodelabel (label);
4320 AddCodeLine ("iny");
4321 AddCodeLine ("lda (ptr1),y");
4322 AddCodeLine ("bne %s", LocalLabelName (label));
4323 AddCodeLine ("tax");
4324 AddCodeLine ("tya");