1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2009 Ullrich von Bassewitz */
10 /* Roemerstrasse 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 /*****************************************************************************/
64 /*****************************************************************************/
66 /*****************************************************************************/
70 static void typeerror (unsigned type)
71 /* Print an error message about an invalid operand type */
73 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
78 static void CheckLocalOffs (unsigned Offs)
79 /* Check the offset into the stack for 8bit range */
82 /* Too many local vars */
83 Error ("Too many local variables");
89 static const char* GetLabelName (unsigned Flags, unsigned long Label, long Offs)
91 static char Buf [256]; /* Label name */
93 /* Create the correct label name */
94 switch (Flags & CF_ADDRMASK) {
97 /* Static memory cell */
99 xsprintf (Buf, sizeof (Buf), "%s%+ld", LocalLabelName (Label), Offs);
101 xsprintf (Buf, sizeof (Buf), "%s", LocalLabelName (Label));
108 xsprintf (Buf, sizeof (Buf), "_%s%+ld", (char*) Label, Offs);
110 xsprintf (Buf, sizeof (Buf), "_%s", (char*) Label);
115 /* Absolute address */
116 xsprintf (Buf, sizeof (Buf), "$%04X", (int)((Label+Offs) & 0xFFFF));
120 /* Variable in register bank */
121 xsprintf (Buf, sizeof (Buf), "regbank+%u", (unsigned)((Label+Offs) & 0xFFFF));
125 Internal ("Invalid address flags: %04X", Flags);
128 /* Return a pointer to the static buffer */
134 /*****************************************************************************/
135 /* Pre- and postamble */
136 /*****************************************************************************/
140 void g_preamble (void)
141 /* Generate the assembler code preamble */
143 /* Create a new (global) segment list and remember it */
147 /* Identify the compiler version */
149 AddTextLine ("; File generated by cc65 v %u.%u.%u",
150 VER_MAJOR, VER_MINOR, VER_PATCH);
153 /* Insert some object file options */
154 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
155 VER_MAJOR, VER_MINOR, VER_PATCH);
157 /* If we're producing code for some other CPU, switch the command set */
159 case CPU_6502: AddTextLine ("\t.setcpu\t\t\"6502\""); break;
160 case CPU_6502X: AddTextLine ("\t.setcpu\t\t\"6502X\""); break;
161 case CPU_65SC02: AddTextLine ("\t.setcpu\t\t\"65SC02\""); break;
162 case CPU_65C02: AddTextLine ("\t.setcpu\t\t\"65C02\""); break;
163 case CPU_65816: AddTextLine ("\t.setcpu\t\t\"65816\""); break;
164 case CPU_HUC6280: AddTextLine ("\t.setcpu\t\t\"HUC6280\""); break;
165 default: Internal ("Unknown CPU: %d", CPU);
169 AddTextLine ("\t.smart\t\ton");
171 /* Allow auto import for runtime library routines */
172 AddTextLine ("\t.autoimport\ton");
174 /* Switch the assembler into case sensitive mode */
175 AddTextLine ("\t.case\t\ton");
177 /* Tell the assembler if we want to generate debug info */
178 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
180 /* Import the stack pointer for direct auto variable access */
181 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
183 /* Define long branch macros */
184 AddTextLine ("\t.macpack\tlongbranch");
189 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
190 /* If debug info is enabled, place a file info into the source */
193 /* We have to place this into the global text segment, so it will
194 * appear before all .dbg line statements.
196 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
202 /*****************************************************************************/
203 /* Segment support */
204 /*****************************************************************************/
208 void g_userodata (void)
209 /* Switch to the read only data segment */
211 UseDataSeg (SEG_RODATA);
216 void g_usedata (void)
217 /* Switch to the data segment */
219 UseDataSeg (SEG_DATA);
225 /* Switch to the bss segment */
227 UseDataSeg (SEG_BSS);
232 void g_segname (segment_t Seg)
233 /* Emit the name of a segment if necessary */
235 /* Emit a segment directive for the data style segments */
238 case SEG_RODATA: S = CS->ROData; break;
239 case SEG_DATA: S = CS->Data; break;
240 case SEG_BSS: S = CS->BSS; break;
241 default: S = 0; break;
244 DS_AddLine (S, ".segment\t\"%s\"", GetSegName (Seg));
250 /*****************************************************************************/
252 /*****************************************************************************/
256 unsigned sizeofarg (unsigned flags)
257 /* Return the size of a function argument type that is encoded in flags */
259 switch (flags & CF_TYPE) {
262 return (flags & CF_FORCECHAR)? 1 : 2;
282 int pop (unsigned flags)
283 /* Pop an argument of the given size */
285 return StackPtr += sizeofarg (flags);
290 int push (unsigned flags)
291 /* Push an argument of the given size */
293 return StackPtr -= sizeofarg (flags);
298 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
299 /* The value in Offs is an offset to an address in a/x. Make sure, an object
300 * of the type given in Flags can be loaded or stored into this address by
301 * adding part of the offset to the address in ax, so that the remaining
302 * offset fits into an index register. Return the remaining offset.
305 /* If the offset is too large for a byte register, add the high byte
306 * of the offset to the primary. Beware: We need a special correction
307 * if the offset in the low byte will overflow in the operation.
309 unsigned O = Offs & ~0xFFU;
310 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
311 /* We need to add the low byte also */
315 /* Do the correction if we need one */
317 g_inc (CF_INT | CF_CONST, O);
321 /* Return the new offset */
327 /*****************************************************************************/
328 /* Functions handling local labels */
329 /*****************************************************************************/
333 void g_defcodelabel (unsigned label)
334 /* Define a local code label */
336 CS_AddLabel (CS->Code, LocalLabelName (label));
341 void g_defdatalabel (unsigned label)
342 /* Define a local data label */
344 AddDataLine ("%s:", LocalLabelName (label));
349 /*****************************************************************************/
350 /* Functions handling global labels */
351 /*****************************************************************************/
355 void g_defgloblabel (const char* Name)
356 /* Define a global label with the given name */
358 /* Global labels are always data labels */
359 AddDataLine ("_%s:", Name);
364 void g_defexport (const char* Name, int ZP)
365 /* Export the given label */
368 AddTextLine ("\t.exportzp\t_%s", Name);
370 AddTextLine ("\t.export\t\t_%s", Name);
376 void g_defimport (const char* Name, int ZP)
377 /* Import the given label */
380 AddTextLine ("\t.importzp\t_%s", Name);
382 AddTextLine ("\t.import\t\t_%s", Name);
388 void g_importstartup (void)
389 /* Forced import of the startup module */
391 AddTextLine ("\t.forceimport\t__STARTUP__");
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? */
466 unsigned k = (unsigned) -StackPtr;
468 /* If we didn't have a variable argument list, don't call leave */
471 /* Drop stackframe if needed. We can only drop 255 bytes at a time. */
474 unsigned ToDrop = (k > 255)? 255 : k;
476 AddCodeLine ("jsr incsp%d", k);
479 AddCodeLine ("jsr addysp");
487 /* Nothing to drop */
488 AddCodeLine ("jsr leave");
490 /* We've a stack frame to drop */
493 AddCodeLine ("jsr addysp");
497 AddCodeLine ("jsr leavey");
501 /* Add the final rts */
507 /*****************************************************************************/
508 /* Register variables */
509 /*****************************************************************************/
513 void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
514 /* Swap a register variable with a location on the stack */
516 /* Calculate the actual stack offset and check it */
517 StackOffs -= StackPtr;
518 CheckLocalOffs (StackOffs);
523 if (IS_Get (&CodeSizeFactor) < 165) {
524 ldyconst (StackOffs);
526 AddCodeLine ("jsr regswap1");
528 ldyconst (StackOffs);
529 AddCodeLine ("lda (sp),y");
530 AddCodeLine ("ldx regbank%+d", RegOffs);
531 AddCodeLine ("sta regbank%+d", RegOffs);
533 AddCodeLine ("sta (sp),y");
536 } else if (Bytes == 2) {
538 ldyconst (StackOffs);
540 AddCodeLine ("jsr regswap2");
544 ldyconst (StackOffs);
547 AddCodeLine ("jsr regswap");
553 void g_save_regvars (int RegOffs, unsigned Bytes)
554 /* Save register variables */
556 /* Don't loop for up to two bytes */
559 AddCodeLine ("lda regbank%+d", RegOffs);
560 AddCodeLine ("jsr pusha");
562 } else if (Bytes == 2) {
564 AddCodeLine ("lda regbank%+d", RegOffs);
565 AddCodeLine ("ldx regbank%+d", RegOffs+1);
566 AddCodeLine ("jsr pushax");
570 /* More than two bytes - loop */
571 unsigned Label = GetLocalLabel ();
573 ldyconst (Bytes - 1);
575 g_defcodelabel (Label);
576 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
577 AddCodeLine ("sta (sp),y");
580 AddCodeLine ("bne %s", LocalLabelName (Label));
584 /* We pushed stuff, correct the stack pointer */
590 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
591 /* Restore register variables */
593 /* Calculate the actual stack offset and check it */
594 StackOffs -= StackPtr;
595 CheckLocalOffs (StackOffs);
597 /* Don't loop for up to two bytes */
600 ldyconst (StackOffs);
601 AddCodeLine ("lda (sp),y");
602 AddCodeLine ("sta regbank%+d", RegOffs);
604 } else if (Bytes == 2) {
606 ldyconst (StackOffs);
607 AddCodeLine ("lda (sp),y");
608 AddCodeLine ("sta regbank%+d", RegOffs);
610 AddCodeLine ("lda (sp),y");
611 AddCodeLine ("sta regbank%+d", RegOffs+1);
613 } else if (Bytes == 3 && IS_Get (&CodeSizeFactor) >= 133) {
615 ldyconst (StackOffs);
616 AddCodeLine ("lda (sp),y");
617 AddCodeLine ("sta regbank%+d", RegOffs);
619 AddCodeLine ("lda (sp),y");
620 AddCodeLine ("sta regbank%+d", RegOffs+1);
622 AddCodeLine ("lda (sp),y");
623 AddCodeLine ("sta regbank%+d", RegOffs+2);
625 } else if (StackOffs <= RegOffs) {
627 /* More bytes, but the relation between the register offset in the
628 * register bank and the stack offset allows us to generate short
629 * code that uses just one index register.
631 unsigned Label = GetLocalLabel ();
632 ldyconst (StackOffs);
633 g_defcodelabel (Label);
634 AddCodeLine ("lda (sp),y");
635 AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs);
637 AddCodeLine ("cpy #$%02X", StackOffs + Bytes);
638 AddCodeLine ("bne %s", LocalLabelName (Label));
642 /* Ok, this is the generic code. We need to save X because the
643 * caller will only save A.
645 unsigned Label = GetLocalLabel ();
646 AddCodeLine ("stx tmp1");
647 ldyconst (StackOffs + Bytes - 1);
648 ldxconst (Bytes - 1);
649 g_defcodelabel (Label);
650 AddCodeLine ("lda (sp),y");
651 AddCodeLine ("sta regbank%+d,x", RegOffs);
654 AddCodeLine ("bpl %s", LocalLabelName (Label));
655 AddCodeLine ("ldx tmp1");
662 /*****************************************************************************/
663 /* Fetching memory cells */
664 /*****************************************************************************/
668 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
669 /* Load a constant into the primary register */
671 unsigned char B1, B2, B3, B4;
675 if ((Flags & CF_CONST) != 0) {
677 /* Numeric constant */
678 switch (Flags & CF_TYPE) {
681 if ((Flags & CF_FORCECHAR) != 0) {
687 ldxconst ((Val >> 8) & 0xFF);
688 ldaconst (Val & 0xFF);
692 /* Split the value into 4 bytes */
693 B1 = (unsigned char) (Val >> 0);
694 B2 = (unsigned char) (Val >> 8);
695 B3 = (unsigned char) (Val >> 16);
696 B4 = (unsigned char) (Val >> 24);
698 /* Remember which bytes are done */
702 AddCodeLine ("ldx #$%02X", B2);
705 AddCodeLine ("stx sreg");
709 AddCodeLine ("stx sreg+1");
712 if ((Done & 0x04) == 0 && B1 != B3) {
713 AddCodeLine ("lda #$%02X", B3);
714 AddCodeLine ("sta sreg");
717 if ((Done & 0x08) == 0 && B1 != B4) {
718 AddCodeLine ("lda #$%02X", B4);
719 AddCodeLine ("sta sreg+1");
722 AddCodeLine ("lda #$%02X", B1);
724 if ((Done & 0x04) == 0) {
726 AddCodeLine ("sta sreg");
728 if ((Done & 0x08) == 0) {
730 AddCodeLine ("sta sreg+1");
742 /* Some sort of label */
743 const char* Label = GetLabelName (Flags, Val, Offs);
745 /* Load the address into the primary */
746 AddCodeLine ("lda #<(%s)", Label);
747 AddCodeLine ("ldx #>(%s)", Label);
754 void g_getstatic (unsigned flags, unsigned long label, long offs)
755 /* Fetch an static memory cell into the primary register */
757 /* Create the correct label name */
758 const char* lbuf = GetLabelName (flags, label, offs);
760 /* Check the size and generate the correct load operation */
761 switch (flags & CF_TYPE) {
764 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
765 AddCodeLine ("lda %s", lbuf); /* load A from the label */
768 AddCodeLine ("lda %s", lbuf); /* load A from the label */
769 if (!(flags & CF_UNSIGNED)) {
770 /* Must sign extend */
771 unsigned L = GetLocalLabel ();
772 AddCodeLine ("bpl %s", LocalLabelName (L));
780 AddCodeLine ("lda %s", lbuf);
781 if (flags & CF_TEST) {
782 AddCodeLine ("ora %s+1", lbuf);
784 AddCodeLine ("ldx %s+1", lbuf);
789 if (flags & CF_TEST) {
790 AddCodeLine ("lda %s+3", lbuf);
791 AddCodeLine ("ora %s+2", lbuf);
792 AddCodeLine ("ora %s+1", lbuf);
793 AddCodeLine ("ora %s+0", lbuf);
795 AddCodeLine ("lda %s+3", lbuf);
796 AddCodeLine ("sta sreg+1");
797 AddCodeLine ("lda %s+2", lbuf);
798 AddCodeLine ("sta sreg");
799 AddCodeLine ("ldx %s+1", lbuf);
800 AddCodeLine ("lda %s", lbuf);
812 void g_getlocal (unsigned flags, int offs)
813 /* Fetch specified local object (local var). */
816 CheckLocalOffs (offs);
817 switch (flags & CF_TYPE) {
820 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
822 AddCodeLine ("lda (sp),y");
825 AddCodeLine ("ldx #$00");
826 AddCodeLine ("lda (sp),y");
827 if ((flags & CF_UNSIGNED) == 0) {
828 unsigned L = GetLocalLabel();
829 AddCodeLine ("bpl %s", LocalLabelName (L));
837 CheckLocalOffs (offs + 1);
838 if (flags & CF_TEST) {
840 AddCodeLine ("lda (sp),y");
842 AddCodeLine ("ora (sp),y");
845 AddCodeLine ("jsr ldaxysp");
851 AddCodeLine ("jsr ldeaxysp");
852 if (flags & CF_TEST) {
864 void g_getind (unsigned flags, unsigned offs)
865 /* Fetch the specified object type indirect through the primary register
866 * into the primary register
869 /* If the offset is greater than 255, add the part that is > 255 to
870 * the primary. This way we get an easy addition and use the low byte
873 offs = MakeByteOffs (flags, offs);
875 /* Handle the indirect fetch */
876 switch (flags & CF_TYPE) {
879 /* Character sized */
880 if (flags & CF_UNSIGNED) {
882 AddCodeLine ("jsr ldauidx");
885 AddCodeLine ("jsr ldaidx");
890 if (flags & CF_TEST) {
892 AddCodeLine ("sta ptr1");
893 AddCodeLine ("stx ptr1+1");
894 AddCodeLine ("lda (ptr1),y");
896 AddCodeLine ("ora (ptr1),y");
899 AddCodeLine ("jsr ldaxidx");
905 AddCodeLine ("jsr ldeaxidx");
906 if (flags & CF_TEST) {
919 void g_leasp (int offs)
920 /* Fetch the address of the specified symbol into the primary register */
922 /* Calculate the offset relative to sp */
925 /* For value 0 we do direct code */
927 AddCodeLine ("lda sp");
928 AddCodeLine ("ldx sp+1");
930 if (IS_Get (&CodeSizeFactor) < 300) {
931 ldaconst (offs); /* Load A with offset value */
932 AddCodeLine ("jsr leaasp"); /* Load effective address */
934 unsigned L = GetLocalLabel ();
935 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && offs == 1) {
936 AddCodeLine ("lda sp");
937 AddCodeLine ("ldx sp+1");
939 AddCodeLine ("bne %s", LocalLabelName (L));
944 AddCodeLine ("ldx sp+1");
945 AddCodeLine ("adc sp");
946 AddCodeLine ("bcc %s", LocalLabelName (L));
956 void g_leavariadic (int Offs)
957 /* Fetch the address of a parameter in a variadic function into the primary
961 unsigned ArgSizeOffs;
963 /* Calculate the offset relative to sp */
966 /* Get the offset of the parameter which is stored at sp+0 on function
967 * entry and check if this offset is reachable with a byte offset.
969 CHECK (StackPtr <= 0);
970 ArgSizeOffs = -StackPtr;
971 CheckLocalOffs (ArgSizeOffs);
973 /* Get the size of all parameters. */
974 ldyconst (ArgSizeOffs);
975 AddCodeLine ("lda (sp),y");
977 /* Add the value of the stackpointer */
978 if (IS_Get (&CodeSizeFactor) > 250) {
979 unsigned L = GetLocalLabel();
980 AddCodeLine ("ldx sp+1");
982 AddCodeLine ("adc sp");
983 AddCodeLine ("bcc %s", LocalLabelName (L));
987 AddCodeLine ("jsr leaasp");
990 /* Add the offset to the primary */
992 g_inc (CF_INT | CF_CONST, Offs);
993 } else if (Offs < 0) {
994 g_dec (CF_INT | CF_CONST, -Offs);
1000 /*****************************************************************************/
1001 /* Store into memory */
1002 /*****************************************************************************/
1006 void g_putstatic (unsigned flags, unsigned long label, long offs)
1007 /* Store the primary register into the specified static memory cell */
1009 /* Create the correct label name */
1010 const char* lbuf = GetLabelName (flags, label, offs);
1012 /* Check the size and generate the correct store operation */
1013 switch (flags & CF_TYPE) {
1016 AddCodeLine ("sta %s", lbuf);
1020 AddCodeLine ("sta %s", lbuf);
1021 AddCodeLine ("stx %s+1", lbuf);
1025 AddCodeLine ("sta %s", lbuf);
1026 AddCodeLine ("stx %s+1", lbuf);
1027 AddCodeLine ("ldy sreg");
1028 AddCodeLine ("sty %s+2", lbuf);
1029 AddCodeLine ("ldy sreg+1");
1030 AddCodeLine ("sty %s+3", lbuf);
1041 void g_putlocal (unsigned Flags, int Offs, long Val)
1042 /* Put data into local object. */
1045 CheckLocalOffs (Offs);
1046 switch (Flags & CF_TYPE) {
1049 if (Flags & CF_CONST) {
1050 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1053 AddCodeLine ("sta (sp),y");
1057 if (Flags & CF_CONST) {
1059 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1060 AddCodeLine ("sta (sp),y");
1061 if ((Flags & CF_NOKEEP) == 0) {
1062 /* Place high byte into X */
1063 AddCodeLine ("tax");
1065 if ((Val & 0xFF) == Offs+1) {
1066 /* The value we need is already in Y */
1067 AddCodeLine ("tya");
1068 AddCodeLine ("dey");
1070 AddCodeLine ("dey");
1071 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1073 AddCodeLine ("sta (sp),y");
1075 if ((Flags & CF_NOKEEP) == 0 || IS_Get (&CodeSizeFactor) < 160) {
1077 AddCodeLine ("jsr staxysp");
1080 AddCodeLine ("sta (sp),y");
1081 AddCodeLine ("iny");
1082 AddCodeLine ("txa");
1083 AddCodeLine ("sta (sp),y");
1089 if (Flags & CF_CONST) {
1090 g_getimmed (Flags, Val, 0);
1093 AddCodeLine ("jsr steaxysp");
1104 void g_putind (unsigned Flags, unsigned Offs)
1105 /* Store the specified object type in the primary register at the address
1106 * on the top of the stack
1109 /* We can handle offsets below $100 directly, larger offsets must be added
1110 * to the address. Since a/x is in use, best code is achieved by adding
1111 * just the high byte. Be sure to check if the low byte will overflow while
1114 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1116 /* Overflow - we need to add the low byte also */
1117 AddCodeLine ("ldy #$00");
1118 AddCodeLine ("clc");
1119 AddCodeLine ("pha");
1120 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1121 AddCodeLine ("adc (sp),y");
1122 AddCodeLine ("sta (sp),y");
1123 AddCodeLine ("iny");
1124 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1125 AddCodeLine ("adc (sp),y");
1126 AddCodeLine ("sta (sp),y");
1127 AddCodeLine ("pla");
1129 /* Complete address is on stack, new offset is zero */
1132 } else if ((Offs & 0xFF00) != 0) {
1134 /* We can just add the high byte */
1135 AddCodeLine ("ldy #$01");
1136 AddCodeLine ("clc");
1137 AddCodeLine ("pha");
1138 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1139 AddCodeLine ("adc (sp),y");
1140 AddCodeLine ("sta (sp),y");
1141 AddCodeLine ("pla");
1143 /* Offset is now just the low byte */
1147 /* Check the size and determine operation */
1148 switch (Flags & CF_TYPE) {
1152 AddCodeLine ("jsr staspidx");
1157 AddCodeLine ("jsr staxspidx");
1162 AddCodeLine ("jsr steaxspidx");
1170 /* Pop the argument which is always a pointer */
1176 /*****************************************************************************/
1177 /* type conversion and similiar stuff */
1178 /*****************************************************************************/
1182 void g_toslong (unsigned flags)
1183 /* Make sure, the value on TOS is a long. Convert if necessary */
1185 switch (flags & CF_TYPE) {
1189 if (flags & CF_UNSIGNED) {
1190 AddCodeLine ("jsr tosulong");
1192 AddCodeLine ("jsr toslong");
1207 void g_tosint (unsigned flags)
1208 /* Make sure, the value on TOS is an int. Convert if necessary */
1210 switch (flags & CF_TYPE) {
1217 AddCodeLine ("jsr tosint");
1228 void g_regint (unsigned Flags)
1229 /* Make sure, the value in the primary register an int. Convert if necessary */
1233 switch (Flags & CF_TYPE) {
1236 if (Flags & CF_FORCECHAR) {
1237 /* Conversion is from char */
1238 if (Flags & CF_UNSIGNED) {
1239 AddCodeLine ("ldx #$00");
1241 L = GetLocalLabel();
1242 AddCodeLine ("ldx #$00");
1243 AddCodeLine ("cmp #$80");
1244 AddCodeLine ("bcc %s", LocalLabelName (L));
1245 AddCodeLine ("dex");
1262 void g_reglong (unsigned Flags)
1263 /* Make sure, the value in the primary register a long. Convert if necessary */
1267 switch (Flags & CF_TYPE) {
1270 if (Flags & CF_FORCECHAR) {
1271 /* Conversion is from char */
1272 if (Flags & CF_UNSIGNED) {
1273 if (IS_Get (&CodeSizeFactor) >= 200) {
1274 AddCodeLine ("ldx #$00");
1275 AddCodeLine ("stx sreg");
1276 AddCodeLine ("stx sreg+1");
1278 AddCodeLine ("jsr aulong");
1281 if (IS_Get (&CodeSizeFactor) >= 366) {
1282 L = GetLocalLabel();
1283 AddCodeLine ("ldx #$00");
1284 AddCodeLine ("cmp #$80");
1285 AddCodeLine ("bcc %s", LocalLabelName (L));
1286 AddCodeLine ("dex");
1288 AddCodeLine ("stx sreg");
1289 AddCodeLine ("stx sreg+1");
1291 AddCodeLine ("jsr along");
1298 if (Flags & CF_UNSIGNED) {
1299 if (IS_Get (&CodeSizeFactor) >= 200) {
1301 AddCodeLine ("sty sreg");
1302 AddCodeLine ("sty sreg+1");
1304 AddCodeLine ("jsr axulong");
1307 AddCodeLine ("jsr axlong");
1321 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1322 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1323 * value, that corresponds to the value on TOS, rhs corresponds to the value
1324 * in (e)ax. The return value is the the flags value for the resulting type.
1327 unsigned ltype, rtype;
1330 /* Get the type spec from the flags */
1331 ltype = lhs & CF_TYPE;
1332 rtype = rhs & CF_TYPE;
1334 /* Check if a conversion is needed */
1335 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1336 /* We must promote the primary register to long */
1338 /* Get the new rhs type */
1339 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1341 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1342 /* We must promote the lhs to long */
1348 /* Get the new rhs type */
1349 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1353 /* Determine the result type for the operation:
1354 * - The result is const if both operands are const.
1355 * - The result is unsigned if one of the operands is unsigned.
1356 * - The result is long if one of the operands is long.
1357 * - Otherwise the result is int sized.
1359 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1360 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1361 if (rtype == CF_LONG || ltype == CF_LONG) {
1371 unsigned g_typecast (unsigned lhs, unsigned rhs)
1372 /* Cast the value in the primary register to the operand size that is flagged
1373 * by the lhs value. Return the result value.
1376 unsigned ltype, rtype;
1378 /* Get the type spec from the flags */
1379 ltype = lhs & CF_TYPE;
1380 rtype = rhs & CF_TYPE;
1382 /* Check if a conversion is needed */
1383 if ((rhs & CF_CONST) == 0) {
1384 if (ltype == CF_LONG && rtype != CF_LONG) {
1385 /* We must promote the primary register to long */
1387 } else if (ltype == CF_INT && rtype != CF_INT) {
1388 /* We must promote the primary register to int */
1393 /* Do not need any other action. If the left type is int, and the primary
1394 * register is long, it will be automagically truncated. If the right hand
1395 * side is const, it is not located in the primary register and handled by
1396 * the expression parser code.
1399 /* Result is const if the right hand side was const */
1400 lhs |= (rhs & CF_CONST);
1402 /* The resulting type is that of the left hand side (that's why you called
1410 void g_scale (unsigned flags, long val)
1411 /* Scale the value in the primary register by the given value. If val is positive,
1412 * scale up, is val is negative, scale down. This function is used to scale
1413 * the operands or results of pointer arithmetic by the size of the type, the
1414 * pointer points to.
1419 /* Value may not be zero */
1421 Internal ("Data type has no size");
1422 } else if (val > 0) {
1425 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1427 /* Factor is 2, 4, 8 and 16, use special function */
1428 switch (flags & CF_TYPE) {
1431 if (flags & CF_FORCECHAR) {
1433 AddCodeLine ("asl a");
1440 if (flags & CF_UNSIGNED) {
1441 AddCodeLine ("jsr shlax%d", p2);
1443 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 AddCodeLine ("jsr lsrax%d", p2);
1495 AddCodeLine ("jsr asrax%d", p2);
1500 if (flags & CF_UNSIGNED) {
1501 AddCodeLine ("jsr lsreax%d", p2);
1503 AddCodeLine ("jsr asreax%d", p2);
1512 } else if (val != 1) {
1514 /* Use a division instead */
1515 g_div (flags | CF_CONST, val);
1523 /*****************************************************************************/
1524 /* Adds and subs of variables fix a fixed address */
1525 /*****************************************************************************/
1529 void g_addlocal (unsigned flags, int offs)
1530 /* Add a local variable to ax */
1534 /* Correct the offset and check it */
1536 CheckLocalOffs (offs);
1538 switch (flags & CF_TYPE) {
1541 L = GetLocalLabel();
1542 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1543 AddCodeLine ("clc");
1544 AddCodeLine ("adc (sp),y");
1545 AddCodeLine ("bcc %s", LocalLabelName (L));
1546 AddCodeLine ("inx");
1551 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1552 AddCodeLine ("clc");
1553 AddCodeLine ("adc (sp),y");
1554 AddCodeLine ("pha");
1555 AddCodeLine ("txa");
1556 AddCodeLine ("iny");
1557 AddCodeLine ("adc (sp),y");
1558 AddCodeLine ("tax");
1559 AddCodeLine ("pla");
1563 /* Do it the old way */
1565 g_getlocal (flags, offs);
1577 void g_addstatic (unsigned flags, unsigned long label, long offs)
1578 /* Add a static variable to ax */
1582 /* Create the correct label name */
1583 const char* lbuf = GetLabelName (flags, label, offs);
1585 switch (flags & CF_TYPE) {
1588 L = GetLocalLabel();
1589 AddCodeLine ("clc");
1590 AddCodeLine ("adc %s", lbuf);
1591 AddCodeLine ("bcc %s", LocalLabelName (L));
1592 AddCodeLine ("inx");
1597 AddCodeLine ("clc");
1598 AddCodeLine ("adc %s", lbuf);
1599 AddCodeLine ("tay");
1600 AddCodeLine ("txa");
1601 AddCodeLine ("adc %s+1", lbuf);
1602 AddCodeLine ("tax");
1603 AddCodeLine ("tya");
1607 /* Do it the old way */
1609 g_getstatic (flags, label, offs);
1621 /*****************************************************************************/
1622 /* Special op= functions */
1623 /*****************************************************************************/
1627 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1629 /* Emit += for a static variable */
1631 /* Create the correct label name */
1632 const char* lbuf = GetLabelName (flags, label, offs);
1634 /* Check the size and determine operation */
1635 switch (flags & CF_TYPE) {
1638 if (flags & CF_FORCECHAR) {
1639 AddCodeLine ("ldx #$00");
1640 if (flags & CF_CONST) {
1642 AddCodeLine ("inc %s", lbuf);
1643 AddCodeLine ("lda %s", lbuf);
1645 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1646 AddCodeLine ("clc");
1647 AddCodeLine ("adc %s", lbuf);
1648 AddCodeLine ("sta %s", lbuf);
1651 AddCodeLine ("clc");
1652 AddCodeLine ("adc %s", lbuf);
1653 AddCodeLine ("sta %s", lbuf);
1655 if ((flags & CF_UNSIGNED) == 0) {
1656 unsigned L = GetLocalLabel();
1657 AddCodeLine ("bpl %s", LocalLabelName (L));
1658 AddCodeLine ("dex");
1666 if (flags & CF_CONST) {
1668 unsigned L = GetLocalLabel ();
1669 AddCodeLine ("inc %s", lbuf);
1670 AddCodeLine ("bne %s", LocalLabelName (L));
1671 AddCodeLine ("inc %s+1", lbuf);
1673 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1674 AddCodeLine ("ldx %s+1", lbuf);
1676 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1677 AddCodeLine ("clc");
1678 AddCodeLine ("adc %s", lbuf);
1679 AddCodeLine ("sta %s", lbuf);
1681 unsigned L = GetLocalLabel ();
1682 AddCodeLine ("bcc %s", LocalLabelName (L));
1683 AddCodeLine ("inc %s+1", lbuf);
1685 AddCodeLine ("ldx %s+1", lbuf);
1687 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1688 AddCodeLine ("adc %s+1", lbuf);
1689 AddCodeLine ("sta %s+1", lbuf);
1690 AddCodeLine ("tax");
1691 AddCodeLine ("lda %s", lbuf);
1695 AddCodeLine ("clc");
1696 AddCodeLine ("adc %s", lbuf);
1697 AddCodeLine ("sta %s", lbuf);
1698 AddCodeLine ("txa");
1699 AddCodeLine ("adc %s+1", lbuf);
1700 AddCodeLine ("sta %s+1", lbuf);
1701 AddCodeLine ("tax");
1702 AddCodeLine ("lda %s", lbuf);
1707 if (flags & CF_CONST) {
1709 AddCodeLine ("ldy #<(%s)", lbuf);
1710 AddCodeLine ("sty ptr1");
1711 AddCodeLine ("ldy #>(%s)", lbuf);
1713 AddCodeLine ("jsr laddeq1");
1715 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1716 AddCodeLine ("jsr laddeqa");
1719 g_getstatic (flags, label, offs);
1721 g_putstatic (flags, label, offs);
1724 AddCodeLine ("ldy #<(%s)", lbuf);
1725 AddCodeLine ("sty ptr1");
1726 AddCodeLine ("ldy #>(%s)", lbuf);
1727 AddCodeLine ("jsr laddeq");
1738 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1739 /* Emit += for a local variable */
1741 /* Calculate the true offset, check it, load it into Y */
1743 CheckLocalOffs (offs);
1745 /* Check the size and determine operation */
1746 switch (flags & CF_TYPE) {
1749 if (flags & CF_FORCECHAR) {
1751 AddCodeLine ("ldx #$00");
1752 if (flags & CF_CONST) {
1753 AddCodeLine ("clc");
1754 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1755 AddCodeLine ("adc (sp),y");
1756 AddCodeLine ("sta (sp),y");
1758 AddCodeLine ("clc");
1759 AddCodeLine ("adc (sp),y");
1760 AddCodeLine ("sta (sp),y");
1762 if ((flags & CF_UNSIGNED) == 0) {
1763 unsigned L = GetLocalLabel();
1764 AddCodeLine ("bpl %s", LocalLabelName (L));
1765 AddCodeLine ("dex");
1774 if (flags & CF_CONST) {
1775 if (IS_Get (&CodeSizeFactor) >= 400) {
1776 AddCodeLine ("clc");
1777 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1778 AddCodeLine ("adc (sp),y");
1779 AddCodeLine ("sta (sp),y");
1780 AddCodeLine ("iny");
1781 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1782 AddCodeLine ("adc (sp),y");
1783 AddCodeLine ("sta (sp),y");
1784 AddCodeLine ("tax");
1785 AddCodeLine ("dey");
1786 AddCodeLine ("lda (sp),y");
1788 g_getimmed (flags, val, 0);
1789 AddCodeLine ("jsr addeqysp");
1792 AddCodeLine ("jsr addeqysp");
1797 if (flags & CF_CONST) {
1798 g_getimmed (flags, val, 0);
1801 AddCodeLine ("jsr laddeqysp");
1811 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1812 /* Emit += for the location with address in ax */
1814 /* If the offset is too large for a byte register, add the high byte
1815 * of the offset to the primary. Beware: We need a special correction
1816 * if the offset in the low byte will overflow in the operation.
1818 offs = MakeByteOffs (flags, offs);
1820 /* Check the size and determine operation */
1821 switch (flags & CF_TYPE) {
1824 AddCodeLine ("sta ptr1");
1825 AddCodeLine ("stx ptr1+1");
1826 AddCodeLine ("ldy #$%02X", offs);
1827 AddCodeLine ("ldx #$00");
1828 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1829 AddCodeLine ("clc");
1830 AddCodeLine ("adc (ptr1),y");
1831 AddCodeLine ("sta (ptr1),y");
1835 if (IS_Get (&CodeSizeFactor) >= 200) {
1836 /* Lots of code, use only if size is not important */
1837 AddCodeLine ("sta ptr1");
1838 AddCodeLine ("stx ptr1+1");
1839 AddCodeLine ("ldy #$%02X", offs);
1840 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1841 AddCodeLine ("clc");
1842 AddCodeLine ("adc (ptr1),y");
1843 AddCodeLine ("sta (ptr1),y");
1844 AddCodeLine ("pha");
1845 AddCodeLine ("iny");
1846 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1847 AddCodeLine ("adc (ptr1),y");
1848 AddCodeLine ("sta (ptr1),y");
1849 AddCodeLine ("tax");
1850 AddCodeLine ("pla");
1856 AddCodeLine ("jsr pushax"); /* Push the address */
1857 push (CF_PTR); /* Correct the internal sp */
1858 g_getind (flags, offs); /* Fetch the value */
1859 g_inc (flags, val); /* Increment value in primary */
1860 g_putind (flags, offs); /* Store the value back */
1870 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1872 /* Emit -= for a static variable */
1874 /* Create the correct label name */
1875 const char* lbuf = GetLabelName (flags, label, offs);
1877 /* Check the size and determine operation */
1878 switch (flags & CF_TYPE) {
1881 if (flags & CF_FORCECHAR) {
1882 AddCodeLine ("ldx #$00");
1883 if (flags & CF_CONST) {
1885 AddCodeLine ("dec %s", lbuf);
1886 AddCodeLine ("lda %s", lbuf);
1888 AddCodeLine ("lda %s", lbuf);
1889 AddCodeLine ("sec");
1890 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1891 AddCodeLine ("sta %s", lbuf);
1894 AddCodeLine ("eor #$FF");
1895 AddCodeLine ("sec");
1896 AddCodeLine ("adc %s", lbuf);
1897 AddCodeLine ("sta %s", lbuf);
1899 if ((flags & CF_UNSIGNED) == 0) {
1900 unsigned L = GetLocalLabel();
1901 AddCodeLine ("bpl %s", LocalLabelName (L));
1902 AddCodeLine ("dex");
1910 if (flags & CF_CONST) {
1911 AddCodeLine ("lda %s", lbuf);
1912 AddCodeLine ("sec");
1913 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1914 AddCodeLine ("sta %s", lbuf);
1916 unsigned L = GetLocalLabel ();
1917 AddCodeLine ("bcs %s", LocalLabelName (L));
1918 AddCodeLine ("dec %s+1", lbuf);
1920 AddCodeLine ("ldx %s+1", lbuf);
1922 AddCodeLine ("lda %s+1", lbuf);
1923 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1924 AddCodeLine ("sta %s+1", lbuf);
1925 AddCodeLine ("tax");
1926 AddCodeLine ("lda %s", lbuf);
1929 AddCodeLine ("eor #$FF");
1930 AddCodeLine ("sec");
1931 AddCodeLine ("adc %s", lbuf);
1932 AddCodeLine ("sta %s", lbuf);
1933 AddCodeLine ("txa");
1934 AddCodeLine ("eor #$FF");
1935 AddCodeLine ("adc %s+1", lbuf);
1936 AddCodeLine ("sta %s+1", lbuf);
1937 AddCodeLine ("tax");
1938 AddCodeLine ("lda %s", lbuf);
1943 if (flags & CF_CONST) {
1945 AddCodeLine ("ldy #<(%s)", lbuf);
1946 AddCodeLine ("sty ptr1");
1947 AddCodeLine ("ldy #>(%s)", lbuf);
1948 AddCodeLine ("lda #$%02X", (unsigned char)val);
1949 AddCodeLine ("jsr lsubeqa");
1951 g_getstatic (flags, label, offs);
1953 g_putstatic (flags, label, offs);
1956 AddCodeLine ("ldy #<(%s)", lbuf);
1957 AddCodeLine ("sty ptr1");
1958 AddCodeLine ("ldy #>(%s)", lbuf);
1959 AddCodeLine ("jsr lsubeq");
1970 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1971 /* Emit -= for a local variable */
1973 /* Calculate the true offset, check it, load it into Y */
1975 CheckLocalOffs (offs);
1977 /* Check the size and determine operation */
1978 switch (flags & CF_TYPE) {
1981 if (flags & CF_FORCECHAR) {
1983 AddCodeLine ("ldx #$00");
1984 if (flags & CF_CONST) {
1985 AddCodeLine ("lda (sp),y");
1986 AddCodeLine ("sec");
1987 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1989 AddCodeLine ("eor #$FF");
1990 AddCodeLine ("sec");
1991 AddCodeLine ("adc (sp),y");
1993 AddCodeLine ("sta (sp),y");
1994 if ((flags & CF_UNSIGNED) == 0) {
1995 unsigned L = GetLocalLabel();
1996 AddCodeLine ("bpl %s", LocalLabelName (L));
1997 AddCodeLine ("dex");
2005 if (flags & CF_CONST) {
2006 g_getimmed (flags, val, 0);
2009 AddCodeLine ("jsr subeqysp");
2013 if (flags & CF_CONST) {
2014 g_getimmed (flags, val, 0);
2017 AddCodeLine ("jsr lsubeqysp");
2027 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2028 /* Emit -= for the location with address in ax */
2030 /* If the offset is too large for a byte register, add the high byte
2031 * of the offset to the primary. Beware: We need a special correction
2032 * if the offset in the low byte will overflow in the operation.
2034 offs = MakeByteOffs (flags, offs);
2036 /* Check the size and determine operation */
2037 switch (flags & CF_TYPE) {
2040 AddCodeLine ("sta ptr1");
2041 AddCodeLine ("stx ptr1+1");
2042 AddCodeLine ("ldy #$%02X", offs);
2043 AddCodeLine ("ldx #$00");
2044 AddCodeLine ("lda (ptr1),y");
2045 AddCodeLine ("sec");
2046 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2047 AddCodeLine ("sta (ptr1),y");
2051 if (IS_Get (&CodeSizeFactor) >= 200) {
2052 /* Lots of code, use only if size is not important */
2053 AddCodeLine ("sta ptr1");
2054 AddCodeLine ("stx ptr1+1");
2055 AddCodeLine ("ldy #$%02X", offs);
2056 AddCodeLine ("lda (ptr1),y");
2057 AddCodeLine ("sec");
2058 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2059 AddCodeLine ("sta (ptr1),y");
2060 AddCodeLine ("pha");
2061 AddCodeLine ("iny");
2062 AddCodeLine ("lda (ptr1),y");
2063 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2064 AddCodeLine ("sta (ptr1),y");
2065 AddCodeLine ("tax");
2066 AddCodeLine ("pla");
2072 AddCodeLine ("jsr pushax"); /* Push the address */
2073 push (CF_PTR); /* Correct the internal sp */
2074 g_getind (flags, offs); /* Fetch the value */
2075 g_dec (flags, val); /* Increment value in primary */
2076 g_putind (flags, offs); /* Store the value back */
2086 /*****************************************************************************/
2087 /* Add a variable address to the value in ax */
2088 /*****************************************************************************/
2092 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2093 /* Add the address of a local variable to ax */
2097 /* Add the offset */
2100 /* We cannot address more then 256 bytes of locals anyway */
2101 L = GetLocalLabel();
2102 CheckLocalOffs (offs);
2103 AddCodeLine ("clc");
2104 AddCodeLine ("adc #$%02X", offs & 0xFF);
2105 /* Do also skip the CLC insn below */
2106 AddCodeLine ("bcc %s", LocalLabelName (L));
2107 AddCodeLine ("inx");
2110 /* Add the current stackpointer value */
2111 AddCodeLine ("clc");
2113 /* Label was used above */
2116 AddCodeLine ("adc sp");
2117 AddCodeLine ("tay");
2118 AddCodeLine ("txa");
2119 AddCodeLine ("adc sp+1");
2120 AddCodeLine ("tax");
2121 AddCodeLine ("tya");
2126 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2127 /* Add the address of a static variable to ax */
2129 /* Create the correct label name */
2130 const char* lbuf = GetLabelName (flags, label, offs);
2132 /* Add the address to the current ax value */
2133 AddCodeLine ("clc");
2134 AddCodeLine ("adc #<(%s)", lbuf);
2135 AddCodeLine ("tay");
2136 AddCodeLine ("txa");
2137 AddCodeLine ("adc #>(%s)", lbuf);
2138 AddCodeLine ("tax");
2139 AddCodeLine ("tya");
2144 /*****************************************************************************/
2146 /*****************************************************************************/
2150 void g_save (unsigned flags)
2151 /* Copy primary register to hold register. */
2153 /* Check the size and determine operation */
2154 switch (flags & CF_TYPE) {
2157 if (flags & CF_FORCECHAR) {
2158 AddCodeLine ("pha");
2164 AddCodeLine ("sta regsave");
2165 AddCodeLine ("stx regsave+1");
2169 AddCodeLine ("jsr saveeax");
2179 void g_restore (unsigned flags)
2180 /* Copy hold register to primary. */
2182 /* Check the size and determine operation */
2183 switch (flags & CF_TYPE) {
2186 if (flags & CF_FORCECHAR) {
2187 AddCodeLine ("pla");
2193 AddCodeLine ("lda regsave");
2194 AddCodeLine ("ldx regsave+1");
2198 AddCodeLine ("jsr resteax");
2208 void g_cmp (unsigned flags, unsigned long val)
2209 /* Immidiate compare. The primary register will not be changed, Z flag
2215 /* Check the size and determine operation */
2216 switch (flags & CF_TYPE) {
2219 if (flags & CF_FORCECHAR) {
2220 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2226 L = GetLocalLabel();
2227 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2228 AddCodeLine ("bne %s", LocalLabelName (L));
2229 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2234 Internal ("g_cmp: Long compares not implemented");
2244 static void oper (unsigned Flags, unsigned long Val, const char** Subs)
2245 /* Encode a binary operation. subs is a pointer to four strings:
2246 * 0 --> Operate on ints
2247 * 1 --> Operate on unsigneds
2248 * 2 --> Operate on longs
2249 * 3 --> Operate on unsigned longs
2252 /* Determine the offset into the array */
2253 if (Flags & CF_UNSIGNED) {
2256 if ((Flags & CF_TYPE) == CF_LONG) {
2260 /* Load the value if it is not already in the primary */
2261 if (Flags & CF_CONST) {
2263 g_getimmed (Flags, Val, 0);
2266 /* Output the operation */
2267 AddCodeLine ("jsr %s", *Subs);
2269 /* The operation will pop it's argument */
2275 void g_test (unsigned flags)
2276 /* Test the value in the primary and set the condition codes */
2278 switch (flags & CF_TYPE) {
2281 if (flags & CF_FORCECHAR) {
2282 AddCodeLine ("tax");
2288 AddCodeLine ("stx tmp1");
2289 AddCodeLine ("ora tmp1");
2293 if (flags & CF_UNSIGNED) {
2294 AddCodeLine ("jsr utsteax");
2296 AddCodeLine ("jsr tsteax");
2308 void g_push (unsigned flags, unsigned long val)
2309 /* Push the primary register or a constant value onto the stack */
2311 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2313 /* We have a constant 8 or 16 bit value */
2314 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2316 /* Handle as 8 bit value */
2318 AddCodeLine ("jsr pusha");
2322 /* Handle as 16 bit value */
2323 g_getimmed (flags, val, 0);
2324 AddCodeLine ("jsr pushax");
2329 /* Value is not 16 bit or not constant */
2330 if (flags & CF_CONST) {
2331 /* Constant 32 bit value, load into eax */
2332 g_getimmed (flags, val, 0);
2335 /* Push the primary register */
2336 switch (flags & CF_TYPE) {
2339 if (flags & CF_FORCECHAR) {
2340 /* Handle as char */
2341 AddCodeLine ("jsr pusha");
2346 AddCodeLine ("jsr pushax");
2350 AddCodeLine ("jsr pusheax");
2360 /* Adjust the stack offset */
2366 void g_swap (unsigned flags)
2367 /* Swap the primary register and the top of the stack. flags give the type
2368 * of *both* values (must have same size).
2371 switch (flags & CF_TYPE) {
2375 AddCodeLine ("jsr swapstk");
2379 AddCodeLine ("jsr swapestk");
2390 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2391 /* Call the specified subroutine name */
2393 if ((Flags & CF_FIXARGC) == 0) {
2394 /* Pass the argument count */
2397 AddCodeLine ("jsr _%s", Label);
2398 StackPtr += ArgSize; /* callee pops args */
2403 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2404 /* Call subroutine indirect */
2406 if ((Flags & CF_LOCAL) == 0) {
2407 /* Address is in a/x */
2408 if ((Flags & CF_FIXARGC) == 0) {
2409 /* Pass arg count */
2412 AddCodeLine ("jsr callax");
2414 /* The address is on stack, offset is on Val */
2416 CheckLocalOffs (Offs);
2417 AddCodeLine ("pha");
2418 AddCodeLine ("ldy #$%02X", Offs);
2419 AddCodeLine ("lda (sp),y");
2420 AddCodeLine ("sta jmpvec+1");
2421 AddCodeLine ("iny");
2422 AddCodeLine ("lda (sp),y");
2423 AddCodeLine ("sta jmpvec+2");
2424 AddCodeLine ("pla");
2425 AddCodeLine ("jsr jmpvec");
2428 /* Callee pops args */
2429 StackPtr += ArgSize;
2434 void g_jump (unsigned Label)
2435 /* Jump to specified internal label number */
2437 AddCodeLine ("jmp %s", LocalLabelName (Label));
2442 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2443 /* Jump to label if zero flag clear */
2445 AddCodeLine ("jne %s", LocalLabelName (label));
2450 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2451 /* Jump to label if zero flag set */
2453 AddCodeLine ("jeq %s", LocalLabelName (label));
2458 static void mod_internal (int k, char* verb1, char* verb2)
2461 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2465 AddCodeLine ("jsr %ssp", verb2);
2471 void g_space (int space)
2472 /* Create or drop space on the stack */
2475 mod_internal (-space, "inc", "addy");
2476 } else if (space > 0) {
2477 mod_internal (space, "dec", "suby");
2483 void g_cstackcheck (void)
2484 /* Check for a C stack overflow */
2486 AddCodeLine ("jsr cstkchk");
2491 void g_stackcheck (void)
2492 /* Check for a stack overflow */
2494 AddCodeLine ("jsr stkchk");
2499 void g_add (unsigned flags, unsigned long val)
2500 /* Primary = TOS + Primary */
2502 static const char* ops[12] = {
2503 "tosaddax", "tosaddax", "tosaddeax", "tosaddeax"
2506 if (flags & CF_CONST) {
2507 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2508 g_push (flags & ~CF_CONST, 0);
2510 oper (flags, val, ops);
2515 void g_sub (unsigned flags, unsigned long val)
2516 /* Primary = TOS - Primary */
2518 static const char* ops[12] = {
2519 "tossubax", "tossubax", "tossubeax", "tossubeax",
2522 if (flags & CF_CONST) {
2523 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2524 g_push (flags & ~CF_CONST, 0);
2526 oper (flags, val, ops);
2531 void g_rsub (unsigned flags, unsigned long val)
2532 /* Primary = Primary - TOS */
2534 static const char* ops[12] = {
2535 "tosrsubax", "tosrsubax", "tosrsubeax", "tosrsubeax",
2537 oper (flags, val, ops);
2542 void g_mul (unsigned flags, unsigned long val)
2543 /* Primary = TOS * Primary */
2545 static const char* ops[12] = {
2546 "tosmulax", "tosumulax", "tosmuleax", "tosumuleax",
2551 /* Do strength reduction if the value is constant and a power of two */
2552 if (flags & CF_CONST && (p2 = PowerOf2 (val)) >= 0) {
2553 /* Generate a shift instead */
2558 /* If the right hand side is const, the lhs is not on stack but still
2559 * in the primary register.
2561 if (flags & CF_CONST) {
2563 switch (flags & CF_TYPE) {
2566 if (flags & CF_FORCECHAR) {
2567 /* Handle some special cases */
2571 AddCodeLine ("sta tmp1");
2572 AddCodeLine ("asl a");
2573 AddCodeLine ("clc");
2574 AddCodeLine ("adc tmp1");
2578 AddCodeLine ("sta tmp1");
2579 AddCodeLine ("asl a");
2580 AddCodeLine ("asl a");
2581 AddCodeLine ("clc");
2582 AddCodeLine ("adc tmp1");
2586 AddCodeLine ("sta tmp1");
2587 AddCodeLine ("asl a");
2588 AddCodeLine ("clc");
2589 AddCodeLine ("adc tmp1");
2590 AddCodeLine ("asl a");
2594 AddCodeLine ("sta tmp1");
2595 AddCodeLine ("asl a");
2596 AddCodeLine ("asl a");
2597 AddCodeLine ("clc");
2598 AddCodeLine ("adc tmp1");
2599 AddCodeLine ("asl a");
2608 AddCodeLine ("jsr mulax3");
2611 AddCodeLine ("jsr mulax5");
2614 AddCodeLine ("jsr mulax6");
2617 AddCodeLine ("jsr mulax7");
2620 AddCodeLine ("jsr mulax9");
2623 AddCodeLine ("jsr mulax10");
2635 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2636 * into the normal, non-optimized stuff.
2638 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2639 g_push (flags & ~CF_CONST, 0);
2643 /* Use long way over the stack */
2644 oper (flags, val, ops);
2649 void g_div (unsigned flags, unsigned long val)
2650 /* Primary = TOS / Primary */
2652 static const char* ops[12] = {
2653 "tosdivax", "tosudivax", "tosdiveax", "tosudiveax",
2656 /* Do strength reduction if the value is constant and a power of two */
2658 if ((flags & CF_CONST) && (p2 = PowerOf2 (val)) >= 0) {
2659 /* Generate a shift instead */
2662 /* Generate a division */
2663 if (flags & CF_CONST) {
2664 /* lhs is not on stack */
2665 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2666 g_push (flags & ~CF_CONST, 0);
2668 oper (flags, val, ops);
2674 void g_mod (unsigned flags, unsigned long val)
2675 /* Primary = TOS % Primary */
2677 static const char* ops[12] = {
2678 "tosmodax", "tosumodax", "tosmodeax", "tosumodeax",
2682 /* Check if we can do some cost reduction */
2683 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = PowerOf2 (val)) >= 0) {
2684 /* We can do that with an AND operation */
2685 g_and (flags, val - 1);
2687 /* Do it the hard way... */
2688 if (flags & CF_CONST) {
2689 /* lhs is not on stack */
2690 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2691 g_push (flags & ~CF_CONST, 0);
2693 oper (flags, val, ops);
2699 void g_or (unsigned flags, unsigned long val)
2700 /* Primary = TOS | Primary */
2702 static const char* ops[12] = {
2703 "tosorax", "tosorax", "tosoreax", "tosoreax",
2706 /* If the right hand side is const, the lhs is not on stack but still
2707 * in the primary register.
2709 if (flags & CF_CONST) {
2711 switch (flags & CF_TYPE) {
2714 if (flags & CF_FORCECHAR) {
2715 if ((val & 0xFF) != 0) {
2716 AddCodeLine ("ora #$%02X", (unsigned char)val);
2724 if ((val & 0xFF) != 0) {
2725 AddCodeLine ("ora #$%02X", (unsigned char)val);
2727 } else if ((val & 0xFF00) == 0xFF00) {
2728 if ((val & 0xFF) != 0) {
2729 AddCodeLine ("ora #$%02X", (unsigned char)val);
2732 } else if (val != 0) {
2733 AddCodeLine ("ora #$%02X", (unsigned char)val);
2734 AddCodeLine ("pha");
2735 AddCodeLine ("txa");
2736 AddCodeLine ("ora #$%02X", (unsigned char)(val >> 8));
2737 AddCodeLine ("tax");
2738 AddCodeLine ("pla");
2744 if ((val & 0xFF) != 0) {
2745 AddCodeLine ("ora #$%02X", (unsigned char)val);
2755 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2756 * into the normal, non-optimized stuff. Note: The standard stuff will
2757 * always work with ints.
2759 flags &= ~CF_FORCECHAR;
2760 g_push (flags & ~CF_CONST, 0);
2763 /* Use long way over the stack */
2764 oper (flags, val, ops);
2769 void g_xor (unsigned flags, unsigned long val)
2770 /* Primary = TOS ^ Primary */
2772 static const char* ops[12] = {
2773 "tosxorax", "tosxorax", "tosxoreax", "tosxoreax",
2777 /* If the right hand side is const, the lhs is not on stack but still
2778 * in the primary register.
2780 if (flags & CF_CONST) {
2782 switch (flags & CF_TYPE) {
2785 if (flags & CF_FORCECHAR) {
2786 if ((val & 0xFF) != 0) {
2787 AddCodeLine ("eor #$%02X", (unsigned char)val);
2796 AddCodeLine ("eor #$%02X", (unsigned char)val);
2798 } else if (val != 0) {
2799 if ((val & 0xFF) != 0) {
2800 AddCodeLine ("eor #$%02X", (unsigned char)val);
2802 AddCodeLine ("pha");
2803 AddCodeLine ("txa");
2804 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2805 AddCodeLine ("tax");
2806 AddCodeLine ("pla");
2813 AddCodeLine ("eor #$%02X", (unsigned char)val);
2823 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2824 * into the normal, non-optimized stuff. Note: The standard stuff will
2825 * always work with ints.
2827 flags &= ~CF_FORCECHAR;
2828 g_push (flags & ~CF_CONST, 0);
2831 /* Use long way over the stack */
2832 oper (flags, val, ops);
2837 void g_and (unsigned Flags, unsigned long Val)
2838 /* Primary = TOS & Primary */
2840 static const char* ops[12] = {
2841 "tosandax", "tosandax", "tosandeax", "tosandeax",
2844 /* If the right hand side is const, the lhs is not on stack but still
2845 * in the primary register.
2847 if (Flags & CF_CONST) {
2849 switch (Flags & CF_TYPE) {
2852 if (Flags & CF_FORCECHAR) {
2853 if ((Val & 0xFF) != 0xFF) {
2854 AddCodeLine ("and #$%02X", (unsigned char)Val);
2860 if ((Val & 0xFFFF) != 0xFFFF) {
2865 } else if (Val != 0xFF) {
2866 AddCodeLine ("and #$%02X", (unsigned char)Val);
2868 } else if ((Val & 0xFF00) == 0xFF00) {
2869 AddCodeLine ("and #$%02X", (unsigned char)Val);
2870 } else if ((Val & 0x00FF) == 0x0000) {
2871 AddCodeLine ("txa");
2872 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2873 AddCodeLine ("tax");
2876 AddCodeLine ("tay");
2877 AddCodeLine ("txa");
2878 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2879 AddCodeLine ("tax");
2880 AddCodeLine ("tya");
2881 if ((Val & 0x00FF) != 0x00FF) {
2882 AddCodeLine ("and #$%02X", (unsigned char)Val);
2891 AddCodeLine ("stx sreg+1");
2892 AddCodeLine ("stx sreg");
2893 if ((Val & 0xFF) != 0xFF) {
2894 AddCodeLine ("and #$%02X", (unsigned char)Val);
2897 } else if (Val == 0xFF00) {
2899 AddCodeLine ("sta sreg+1");
2900 AddCodeLine ("sta sreg");
2909 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2910 * into the normal, non-optimized stuff. Note: The standard stuff will
2911 * always work with ints.
2913 Flags &= ~CF_FORCECHAR;
2914 g_push (Flags & ~CF_CONST, 0);
2917 /* Use long way over the stack */
2918 oper (Flags, Val, ops);
2923 void g_asr (unsigned flags, unsigned long val)
2924 /* Primary = TOS >> Primary */
2926 static const char* ops[12] = {
2927 "tosasrax", "tosshrax", "tosasreax", "tosshreax",
2930 /* If the right hand side is const, the lhs is not on stack but still
2931 * in the primary register.
2933 if (flags & CF_CONST) {
2935 switch (flags & CF_TYPE) {
2939 if (val >= 8 && (flags & CF_UNSIGNED)) {
2940 AddCodeLine ("txa");
2947 } else if (val >= 1 && val <= 4) {
2948 if (flags & CF_UNSIGNED) {
2949 AddCodeLine ("jsr shrax%ld", val);
2951 AddCodeLine ("jsr asrax%ld", val);
2961 } else if (val >= 1 && val <= 4) {
2962 if (flags & CF_UNSIGNED) {
2963 AddCodeLine ("jsr shreax%ld", val);
2965 AddCodeLine ("jsr asreax%ld", val);
2968 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2969 AddCodeLine ("txa");
2970 AddCodeLine ("ldx sreg");
2971 AddCodeLine ("ldy sreg+1");
2972 AddCodeLine ("sty sreg");
2973 AddCodeLine ("ldy #$00");
2974 AddCodeLine ("sty sreg+1");
2976 } else if (val == 16) {
2977 AddCodeLine ("ldy #$00");
2978 AddCodeLine ("ldx sreg+1");
2979 if ((flags & CF_UNSIGNED) == 0) {
2980 unsigned L = GetLocalLabel();
2981 AddCodeLine ("bpl %s", LocalLabelName (L));
2982 AddCodeLine ("dey");
2985 AddCodeLine ("lda sreg");
2986 AddCodeLine ("sty sreg+1");
2987 AddCodeLine ("sty sreg");
2996 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2997 * into the normal, non-optimized stuff. Note: The standard stuff will
2998 * always work with ints.
3000 flags &= ~CF_FORCECHAR;
3001 g_push (flags & ~CF_CONST, 0);
3004 /* Use long way over the stack */
3005 oper (flags, val, ops);
3010 void g_asl (unsigned flags, unsigned long val)
3011 /* Primary = TOS << Primary */
3013 static const char* ops[12] = {
3014 "tosaslax", "tosshlax", "tosasleax", "tosshleax",
3018 /* If the right hand side is const, the lhs is not on stack but still
3019 * in the primary register.
3021 if (flags & CF_CONST) {
3023 switch (flags & CF_TYPE) {
3028 AddCodeLine ("tax");
3029 AddCodeLine ("lda #$00");
3035 } else if (val >= 1 && val <= 4) {
3036 if (flags & CF_UNSIGNED) {
3037 AddCodeLine ("jsr shlax%ld", val);
3039 AddCodeLine ("jsr aslax%ld", val);
3049 } else if (val >= 1 && val <= 4) {
3050 if (flags & CF_UNSIGNED) {
3051 AddCodeLine ("jsr shleax%ld", val);
3053 AddCodeLine ("jsr asleax%ld", val);
3056 } else if (val == 8) {
3057 AddCodeLine ("ldy sreg");
3058 AddCodeLine ("sty sreg+1");
3059 AddCodeLine ("stx sreg");
3060 AddCodeLine ("tax");
3061 AddCodeLine ("lda #$00");
3063 } else if (val == 16) {
3064 AddCodeLine ("stx sreg+1");
3065 AddCodeLine ("sta sreg");
3066 AddCodeLine ("lda #$00");
3067 AddCodeLine ("tax");
3076 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3077 * into the normal, non-optimized stuff. Note: The standard stuff will
3078 * always work with ints.
3080 flags &= ~CF_FORCECHAR;
3081 g_push (flags & ~CF_CONST, 0);
3084 /* Use long way over the stack */
3085 oper (flags, val, ops);
3090 void g_neg (unsigned Flags)
3091 /* Primary = -Primary */
3093 switch (Flags & CF_TYPE) {
3096 if (Flags & CF_FORCECHAR) {
3097 AddCodeLine ("eor #$FF");
3098 AddCodeLine ("clc");
3099 AddCodeLine ("adc #$01");
3105 AddCodeLine ("jsr negax");
3109 AddCodeLine ("jsr negeax");
3119 void g_bneg (unsigned flags)
3120 /* Primary = !Primary */
3122 switch (flags & CF_TYPE) {
3125 AddCodeLine ("jsr bnega");
3129 AddCodeLine ("jsr bnegax");
3133 AddCodeLine ("jsr bnegeax");
3143 void g_com (unsigned Flags)
3144 /* Primary = ~Primary */
3146 switch (Flags & CF_TYPE) {
3149 if (Flags & CF_FORCECHAR) {
3150 AddCodeLine ("eor #$FF");
3156 AddCodeLine ("jsr complax");
3160 AddCodeLine ("jsr compleax");
3170 void g_inc (unsigned flags, unsigned long val)
3171 /* Increment the primary register by a given number */
3173 /* Don't inc by zero */
3178 /* Generate code for the supported types */
3180 switch (flags & CF_TYPE) {
3183 if (flags & CF_FORCECHAR) {
3184 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3186 AddCodeLine ("ina");
3189 AddCodeLine ("clc");
3190 AddCodeLine ("adc #$%02X", (unsigned char)val);
3197 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) {
3198 unsigned L = GetLocalLabel();
3199 AddCodeLine ("ina");
3200 AddCodeLine ("bne %s", LocalLabelName (L));
3201 AddCodeLine ("inx");
3203 } else if (IS_Get (&CodeSizeFactor) < 200) {
3206 AddCodeLine ("jsr incax%lu", val);
3207 } else if (val <= 255) {
3209 AddCodeLine ("jsr incaxy");
3211 g_add (flags | CF_CONST, val);
3214 /* Inline the code */
3216 if ((val & 0xFF) != 0) {
3217 unsigned L = GetLocalLabel();
3218 AddCodeLine ("clc");
3219 AddCodeLine ("adc #$%02X", (unsigned char) val);
3220 AddCodeLine ("bcc %s", LocalLabelName (L));
3221 AddCodeLine ("inx");
3225 AddCodeLine ("inx");
3228 AddCodeLine ("inx");
3231 AddCodeLine ("inx");
3234 AddCodeLine ("clc");
3235 if ((val & 0xFF) != 0) {
3236 AddCodeLine ("adc #$%02X", (unsigned char) val);
3238 AddCodeLine ("pha");
3239 AddCodeLine ("txa");
3240 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3241 AddCodeLine ("tax");
3242 AddCodeLine ("pla");
3250 AddCodeLine ("jsr inceaxy");
3252 g_add (flags | CF_CONST, val);
3264 void g_dec (unsigned flags, unsigned long val)
3265 /* Decrement the primary register by a given number */
3267 /* Don't dec by zero */
3272 /* Generate code for the supported types */
3274 switch (flags & CF_TYPE) {
3277 if (flags & CF_FORCECHAR) {
3278 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3280 AddCodeLine ("dea");
3283 AddCodeLine ("sec");
3284 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3291 if (IS_Get (&CodeSizeFactor) < 200) {
3292 /* Use subroutines */
3294 AddCodeLine ("jsr decax%d", (int) val);
3295 } else if (val <= 255) {
3297 AddCodeLine ("jsr decaxy");
3299 g_sub (flags | CF_CONST, val);
3302 /* Inline the code */
3304 if ((val & 0xFF) != 0) {
3305 unsigned L = GetLocalLabel();
3306 AddCodeLine ("sec");
3307 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3308 AddCodeLine ("bcs %s", LocalLabelName (L));
3309 AddCodeLine ("dex");
3313 AddCodeLine ("dex");
3316 AddCodeLine ("dex");
3319 if ((val & 0xFF) != 0) {
3320 AddCodeLine ("sec");
3321 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3322 AddCodeLine ("pha");
3323 AddCodeLine ("txa");
3324 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3325 AddCodeLine ("tax");
3326 AddCodeLine ("pla");
3328 AddCodeLine ("pha");
3329 AddCodeLine ("txa");
3330 AddCodeLine ("sec");
3331 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3332 AddCodeLine ("tax");
3333 AddCodeLine ("pla");
3342 AddCodeLine ("jsr deceaxy");
3344 g_sub (flags | CF_CONST, val);
3357 * Following are the conditional operators. They compare the TOS against
3358 * the primary and put a literal 1 in the primary if the condition is
3359 * true, otherwise they clear the primary register
3364 void g_eq (unsigned flags, unsigned long val)
3365 /* Test for equal */
3367 static const char* ops[12] = {
3368 "toseqax", "toseqax", "toseqeax", "toseqeax",
3373 /* If the right hand side is const, the lhs is not on stack but still
3374 * in the primary register.
3376 if (flags & CF_CONST) {
3378 switch (flags & CF_TYPE) {
3381 if (flags & CF_FORCECHAR) {
3382 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3383 AddCodeLine ("jsr booleq");
3389 L = GetLocalLabel();
3390 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3391 AddCodeLine ("bne %s", LocalLabelName (L));
3392 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3394 AddCodeLine ("jsr booleq");
3404 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3405 * into the normal, non-optimized stuff. Note: The standard stuff will
3406 * always work with ints.
3408 flags &= ~CF_FORCECHAR;
3409 g_push (flags & ~CF_CONST, 0);
3412 /* Use long way over the stack */
3413 oper (flags, val, ops);
3418 void g_ne (unsigned flags, unsigned long val)
3419 /* Test for not equal */
3421 static const char* ops[12] = {
3422 "tosneax", "tosneax", "tosneeax", "tosneeax",
3427 /* If the right hand side is const, the lhs is not on stack but still
3428 * in the primary register.
3430 if (flags & CF_CONST) {
3432 switch (flags & CF_TYPE) {
3435 if (flags & CF_FORCECHAR) {
3436 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3437 AddCodeLine ("jsr boolne");
3443 L = GetLocalLabel();
3444 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3445 AddCodeLine ("bne %s", LocalLabelName (L));
3446 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3448 AddCodeLine ("jsr boolne");
3458 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3459 * into the normal, non-optimized stuff. Note: The standard stuff will
3460 * always work with ints.
3462 flags &= ~CF_FORCECHAR;
3463 g_push (flags & ~CF_CONST, 0);
3466 /* Use long way over the stack */
3467 oper (flags, val, ops);
3472 void g_lt (unsigned flags, unsigned long val)
3473 /* Test for less than */
3475 static const char* ops[12] = {
3476 "tosltax", "tosultax", "toslteax", "tosulteax",
3479 /* If the right hand side is const, the lhs is not on stack but still
3480 * in the primary register.
3482 if (flags & CF_CONST) {
3484 /* Because the handling of the overflow flag is too complex for
3485 * inlining, we can handle only unsigned compares, and signed
3486 * compares against zero here.
3488 if (flags & CF_UNSIGNED) {
3490 /* Give a warning in some special cases */
3492 Warning ("Condition is never true");
3493 AddCodeLine ("jsr return0");
3497 /* Look at the type */
3498 switch (flags & CF_TYPE) {
3501 if (flags & CF_FORCECHAR) {
3502 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3503 AddCodeLine ("jsr boolult");
3509 /* If the low byte is zero, we must only test the high byte */
3510 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3511 if ((val & 0xFF) != 0) {
3512 unsigned L = GetLocalLabel();
3513 AddCodeLine ("bne %s", LocalLabelName (L));
3514 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3517 AddCodeLine ("jsr boolult");
3521 /* Do a subtraction */
3522 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3523 AddCodeLine ("txa");
3524 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3525 AddCodeLine ("lda sreg");
3526 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3527 AddCodeLine ("lda sreg+1");
3528 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3529 AddCodeLine ("jsr boolult");
3536 } else if (val == 0) {
3538 /* Look at the type */
3539 switch (flags & CF_TYPE) {
3542 if (flags & CF_FORCECHAR) {
3543 AddCodeLine ("tax");
3544 AddCodeLine ("jsr boollt");
3550 /* Just check the high byte */
3551 AddCodeLine ("txa");
3552 AddCodeLine ("jsr boollt");
3556 /* Just check the high byte */
3557 AddCodeLine ("lda sreg+1");
3558 AddCodeLine ("jsr boollt");
3566 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3567 * into the normal, non-optimized stuff. Note: The standard stuff will
3568 * always work with ints.
3570 flags &= ~CF_FORCECHAR;
3571 g_push (flags & ~CF_CONST, 0);
3574 /* Use long way over the stack */
3575 oper (flags, val, ops);
3580 void g_le (unsigned flags, unsigned long val)
3581 /* Test for less than or equal to */
3583 static const char* ops[12] = {
3584 "tosleax", "tosuleax", "tosleeax", "tosuleeax",
3588 /* If the right hand side is const, the lhs is not on stack but still
3589 * in the primary register.
3591 if (flags & CF_CONST) {
3593 /* Look at the type */
3594 switch (flags & CF_TYPE) {
3597 if (flags & CF_FORCECHAR) {
3598 if (flags & CF_UNSIGNED) {
3599 /* Unsigned compare */
3601 /* Use < instead of <= because the former gives
3602 * better code on the 6502 than the latter.
3604 g_lt (flags, val+1);
3607 Warning ("Condition is always true");
3608 AddCodeLine ("jsr return1");
3611 /* Signed compare */
3612 if ((long) val < 0x7F) {
3613 /* Use < instead of <= because the former gives
3614 * better code on the 6502 than the latter.
3616 g_lt (flags, val+1);
3619 Warning ("Condition is always true");
3620 AddCodeLine ("jsr return1");
3628 if (flags & CF_UNSIGNED) {
3629 /* Unsigned compare */
3631 /* Use < instead of <= because the former gives
3632 * better code on the 6502 than the latter.
3634 g_lt (flags, val+1);
3637 Warning ("Condition is always true");
3638 AddCodeLine ("jsr return1");
3641 /* Signed compare */
3642 if ((long) val < 0x7FFF) {
3643 g_lt (flags, val+1);
3646 Warning ("Condition is always true");
3647 AddCodeLine ("jsr return1");
3653 if (flags & CF_UNSIGNED) {
3654 /* Unsigned compare */
3655 if (val < 0xFFFFFFFF) {
3656 /* Use < instead of <= because the former gives
3657 * better code on the 6502 than the latter.
3659 g_lt (flags, val+1);
3662 Warning ("Condition is always true");
3663 AddCodeLine ("jsr return1");
3666 /* Signed compare */
3667 if ((long) val < 0x7FFFFFFF) {
3668 g_lt (flags, val+1);
3671 Warning ("Condition is always true");
3672 AddCodeLine ("jsr return1");
3681 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3682 * into the normal, non-optimized stuff. Note: The standard stuff will
3683 * always work with ints.
3685 flags &= ~CF_FORCECHAR;
3686 g_push (flags & ~CF_CONST, 0);
3689 /* Use long way over the stack */
3690 oper (flags, val, ops);
3695 void g_gt (unsigned flags, unsigned long val)
3696 /* Test for greater than */
3698 static const char* ops[12] = {
3699 "tosgtax", "tosugtax", "tosgteax", "tosugteax",
3703 /* If the right hand side is const, the lhs is not on stack but still
3704 * in the primary register.
3706 if (flags & CF_CONST) {
3708 /* Look at the type */
3709 switch (flags & CF_TYPE) {
3712 if (flags & CF_FORCECHAR) {
3713 if (flags & CF_UNSIGNED) {
3715 /* If we have a compare > 0, we will replace it by
3716 * != 0 here, since both are identical but the
3717 * latter is easier to optimize.
3720 } else if (val < 0xFF) {
3721 /* Use >= instead of > because the former gives
3722 * better code on the 6502 than the latter.
3724 g_ge (flags, val+1);
3727 Warning ("Condition is never true");
3728 AddCodeLine ("jsr return0");
3731 if ((long) val < 0x7F) {
3732 /* Use >= instead of > because the former gives
3733 * better code on the 6502 than the latter.
3735 g_ge (flags, val+1);
3738 Warning ("Condition is never true");
3739 AddCodeLine ("jsr return0");
3747 if (flags & CF_UNSIGNED) {
3748 /* Unsigned compare */
3750 /* If we have a compare > 0, we will replace it by
3751 * != 0 here, since both are identical but the latter
3752 * is easier to optimize.
3755 } else if (val < 0xFFFF) {
3756 /* Use >= instead of > because the former gives better
3757 * code on the 6502 than the latter.
3759 g_ge (flags, val+1);
3762 Warning ("Condition is never true");
3763 AddCodeLine ("jsr return0");
3766 /* Signed compare */
3767 if ((long) val < 0x7FFF) {
3768 g_ge (flags, val+1);
3771 Warning ("Condition is never true");
3772 AddCodeLine ("jsr return0");
3778 if (flags & CF_UNSIGNED) {
3779 /* Unsigned compare */
3781 /* If we have a compare > 0, we will replace it by
3782 * != 0 here, since both are identical but the latter
3783 * is easier to optimize.
3786 } else if (val < 0xFFFFFFFF) {
3787 /* Use >= instead of > because the former gives better
3788 * code on the 6502 than the latter.
3790 g_ge (flags, val+1);
3793 Warning ("Condition is never true");
3794 AddCodeLine ("jsr return0");
3797 /* Signed compare */
3798 if ((long) val < 0x7FFFFFFF) {
3799 g_ge (flags, val+1);
3802 Warning ("Condition is never true");
3803 AddCodeLine ("jsr return0");
3812 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3813 * into the normal, non-optimized stuff. Note: The standard stuff will
3814 * always work with ints.
3816 flags &= ~CF_FORCECHAR;
3817 g_push (flags & ~CF_CONST, 0);
3820 /* Use long way over the stack */
3821 oper (flags, val, ops);
3826 void g_ge (unsigned flags, unsigned long val)
3827 /* Test for greater than or equal to */
3829 static const char* ops[12] = {
3830 "tosgeax", "tosugeax", "tosgeeax", "tosugeeax",
3834 /* If the right hand side is const, the lhs is not on stack but still
3835 * in the primary register.
3837 if (flags & CF_CONST) {
3839 /* Because the handling of the overflow flag is too complex for
3840 * inlining, we can handle only unsigned compares, and signed
3841 * compares against zero here.
3843 if (flags & CF_UNSIGNED) {
3845 /* Give a warning in some special cases */
3847 Warning ("Condition is always true");
3848 AddCodeLine ("jsr return1");
3852 /* Look at the type */
3853 switch (flags & CF_TYPE) {
3856 if (flags & CF_FORCECHAR) {
3857 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3858 AddCodeLine ("jsr booluge");
3864 /* If the low byte is zero, we must only test the high byte */
3865 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3866 if ((val & 0xFF) != 0) {
3867 unsigned L = GetLocalLabel();
3868 AddCodeLine ("bne %s", LocalLabelName (L));
3869 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3872 AddCodeLine ("jsr booluge");
3876 /* Do a subtraction */
3877 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3878 AddCodeLine ("txa");
3879 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3880 AddCodeLine ("lda sreg");
3881 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3882 AddCodeLine ("lda sreg+1");
3883 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3884 AddCodeLine ("jsr booluge");
3891 } else if (val == 0) {
3893 /* Look at the type */
3894 switch (flags & CF_TYPE) {
3897 if (flags & CF_FORCECHAR) {
3898 AddCodeLine ("tax");
3899 AddCodeLine ("jsr boolge");
3905 /* Just test the high byte */
3906 AddCodeLine ("txa");
3907 AddCodeLine ("jsr boolge");
3911 /* Just test the high byte */
3912 AddCodeLine ("lda sreg+1");
3913 AddCodeLine ("jsr boolge");
3922 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3923 * into the normal, non-optimized stuff. Note: The standard stuff will
3924 * always work with ints.
3926 flags &= ~CF_FORCECHAR;
3927 g_push (flags & ~CF_CONST, 0);
3930 /* Use long way over the stack */
3931 oper (flags, val, ops);
3936 /*****************************************************************************/
3937 /* Allocating static storage */
3938 /*****************************************************************************/
3942 void g_res (unsigned n)
3943 /* Reserve static storage, n bytes */
3945 AddDataLine ("\t.res\t%u,$00", n);
3950 void g_defdata (unsigned flags, unsigned long val, long offs)
3951 /* Define data with the size given in flags */
3953 if (flags & CF_CONST) {
3955 /* Numeric constant */
3956 switch (flags & CF_TYPE) {
3959 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
3963 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
3967 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3978 /* Create the correct label name */
3979 const char* Label = GetLabelName (flags, val, offs);
3981 /* Labels are always 16 bit */
3982 AddDataLine ("\t.addr\t%s", Label);
3989 void g_defbytes (const void* Bytes, unsigned Count)
3990 /* Output a row of bytes as a constant */
3996 /* Cast the buffer pointer */
3997 const unsigned char* Data = (const unsigned char*) Bytes;
3999 /* Output the stuff */
4002 /* How many go into this line? */
4003 if ((Chunk = Count) > 16) {
4008 /* Output one line */
4009 strcpy (Buf, "\t.byte\t");
4012 B += sprintf (B, "$%02X", *Data++);
4018 /* Output the line */
4025 void g_zerobytes (unsigned Count)
4026 /* Output Count bytes of data initialized with zero */
4029 AddDataLine ("\t.res\t%u,$00", Count);
4035 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4036 /* Initialize a register variable from static initialization data */
4038 /* Register variables do always have less than 128 bytes */
4039 unsigned CodeLabel = GetLocalLabel ();
4041 g_defcodelabel (CodeLabel);
4042 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4043 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4044 AddCodeLine ("dex");
4045 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4050 void g_initauto (unsigned Label, unsigned Size)
4051 /* Initialize a local variable at stack offset zero from static data */
4053 unsigned CodeLabel = GetLocalLabel ();
4055 CheckLocalOffs (Size);
4058 g_defcodelabel (CodeLabel);
4059 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4060 AddCodeLine ("sta (sp),y");
4061 AddCodeLine ("dey");
4062 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4063 } else if (Size <= 256) {
4065 g_defcodelabel (CodeLabel);
4066 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4067 AddCodeLine ("sta (sp),y");
4068 AddCodeLine ("iny");
4069 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4070 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4076 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4077 /* Initialize a static local variable from static initialization data */
4080 unsigned CodeLabel = GetLocalLabel ();
4082 g_defcodelabel (CodeLabel);
4083 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4084 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4085 AddCodeLine ("dey");
4086 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4087 } else if (Size <= 256) {
4088 unsigned CodeLabel = GetLocalLabel ();
4090 g_defcodelabel (CodeLabel);
4091 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4092 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4093 AddCodeLine ("iny");
4094 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4095 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4097 /* Use the easy way here: memcpy */
4098 g_getimmed (CF_STATIC, VarLabel, 0);
4099 AddCodeLine ("jsr pushax");
4100 g_getimmed (CF_STATIC, InitLabel, 0);
4101 AddCodeLine ("jsr pushax");
4102 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4103 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4109 /*****************************************************************************/
4110 /* Switch statement */
4111 /*****************************************************************************/
4115 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4116 /* Generate code for a switch statement */
4118 unsigned NextLabel = 0;
4121 /* Setup registers and determine which compare insn to use */
4122 const char* Compare;
4125 Compare = "cmp #$%02X";
4128 Compare = "cpx #$%02X";
4131 AddCodeLine ("ldy sreg");
4132 Compare = "cpy #$%02X";
4135 AddCodeLine ("ldy sreg+1");
4136 Compare = "cpy #$%02X";
4139 Internal ("Invalid depth in g_switch: %u", Depth);
4142 /* Walk over all nodes */
4143 for (I = 0; I < CollCount (Nodes); ++I) {
4145 /* Get the next case node */
4146 CaseNode* N = CollAtUnchecked (Nodes, I);
4148 /* If we have a next label, define it */
4150 g_defcodelabel (NextLabel);
4154 /* Do the compare */
4155 AddCodeLine (Compare, CN_GetValue (N));
4157 /* If this is the last level, jump directly to the case code if found */
4160 /* Branch if equal */
4161 g_falsejump (0, CN_GetLabel (N));
4165 /* Determine the next label */
4166 if (I == CollCount (Nodes) - 1) {
4167 /* Last node means not found */
4168 g_truejump (0, DefaultLabel);
4170 /* Jump to the next check */
4171 NextLabel = GetLocalLabel ();
4172 g_truejump (0, NextLabel);
4175 /* Check the next level */
4176 g_switch (N->Nodes, DefaultLabel, Depth-1);
4181 /* If we go here, we haven't found the label */
4182 g_jump (DefaultLabel);
4187 /*****************************************************************************/
4188 /* User supplied assembler code */
4189 /*****************************************************************************/
4193 void g_asmcode (struct StrBuf* B)
4194 /* Output one line of assembler code. */
4196 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));