1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2004 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_6502X: AddTextLine ("\t.setcpu\t\t\"6502X\""); break;
171 case CPU_65SC02: AddTextLine ("\t.setcpu\t\t\"65SC02\""); break;
172 case CPU_65C02: AddTextLine ("\t.setcpu\t\t\"65C02\""); break;
173 case CPU_65816: AddTextLine ("\t.setcpu\t\t\"65816\""); break;
174 default: Internal ("Unknown CPU: %d", CPU);
178 AddTextLine ("\t.smart\t\ton");
180 /* Allow auto import for runtime library routines */
181 AddTextLine ("\t.autoimport\ton");
183 /* Switch the assembler into case sensitive mode */
184 AddTextLine ("\t.case\t\ton");
186 /* Tell the assembler if we want to generate debug info */
187 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
189 /* Import the stack pointer for direct auto variable access */
190 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
192 /* Define long branch macros */
193 AddTextLine ("\t.macpack\tlongbranch");
198 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
199 /* If debug info is enabled, place a file info into the source */
202 /* We have to place this into the global text segment, so it will
203 * appear before all .dbg line statements.
205 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
211 /*****************************************************************************/
212 /* Segment support */
213 /*****************************************************************************/
217 void g_userodata (void)
218 /* Switch to the read only data segment */
220 UseDataSeg (SEG_RODATA);
225 void g_usedata (void)
226 /* Switch to the data segment */
228 UseDataSeg (SEG_DATA);
234 /* Switch to the bss segment */
236 UseDataSeg (SEG_BSS);
241 void g_segname (segment_t Seg)
242 /* Emit the name of a segment if necessary */
244 /* Emit a segment directive for the data style segments */
247 case SEG_RODATA: S = CS->ROData; break;
248 case SEG_DATA: S = CS->Data; break;
249 case SEG_BSS: S = CS->BSS; break;
250 default: S = 0; break;
253 DS_AddLine (S, ".segment\t\"%s\"", GetSegName (Seg));
259 /*****************************************************************************/
261 /*****************************************************************************/
265 unsigned sizeofarg (unsigned flags)
266 /* Return the size of a function argument type that is encoded in flags */
268 switch (flags & CF_TYPE) {
271 return (flags & CF_FORCECHAR)? 1 : 2;
291 int pop (unsigned flags)
292 /* Pop an argument of the given size */
294 return StackPtr += sizeofarg (flags);
299 int push (unsigned flags)
300 /* Push an argument of the given size */
302 return StackPtr -= sizeofarg (flags);
307 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
308 /* The value in Offs is an offset to an address in a/x. Make sure, an object
309 * of the type given in Flags can be loaded or stored into this address by
310 * adding part of the offset to the address in ax, so that the remaining
311 * offset fits into an index register. Return the remaining offset.
314 /* If the offset is too large for a byte register, add the high byte
315 * of the offset to the primary. Beware: We need a special correction
316 * if the offset in the low byte will overflow in the operation.
318 unsigned O = Offs & ~0xFFU;
319 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
320 /* We need to add the low byte also */
324 /* Do the correction if we need one */
326 g_inc (CF_INT | CF_CONST, O);
330 /* Return the new offset */
336 /*****************************************************************************/
337 /* Functions handling local labels */
338 /*****************************************************************************/
342 void g_defcodelabel (unsigned label)
343 /* Define a local code label */
345 CS_AddLabel (CS->Code, LocalLabelName (label));
350 void g_defdatalabel (unsigned label)
351 /* Define a local data label */
353 AddDataLine ("%s:", LocalLabelName (label));
358 /*****************************************************************************/
359 /* Functions handling global labels */
360 /*****************************************************************************/
364 void g_defgloblabel (const char* Name)
365 /* Define a global label with the given name */
367 /* Global labels are always data labels */
368 AddDataLine ("_%s:", Name);
373 void g_defexport (const char* Name, int ZP)
374 /* Export the given label */
377 AddTextLine ("\t.exportzp\t_%s", Name);
379 AddTextLine ("\t.export\t\t_%s", Name);
385 void g_defimport (const char* Name, int ZP)
386 /* Import the given label */
389 AddTextLine ("\t.importzp\t_%s", Name);
391 AddTextLine ("\t.import\t\t_%s", Name);
397 void g_importmainargs (void)
398 /* Forced import of a special symbol that handles arguments to main */
400 AddTextLine ("\t.forceimport\tinitmainargs");
405 /*****************************************************************************/
406 /* Load functions for various registers */
407 /*****************************************************************************/
411 static void ldaconst (unsigned val)
412 /* Load a with a constant */
414 AddCodeLine ("lda #$%02X", val & 0xFF);
419 static void ldxconst (unsigned val)
420 /* Load x with a constant */
422 AddCodeLine ("ldx #$%02X", val & 0xFF);
427 static void ldyconst (unsigned val)
428 /* Load y with a constant */
430 AddCodeLine ("ldy #$%02X", val & 0xFF);
435 /*****************************************************************************/
436 /* Function entry and exit */
437 /*****************************************************************************/
441 /* Remember the argument size of a function. The variable is set by g_enter
442 * and used by g_leave. If the functions gets its argument size by the caller
443 * (variable param list or function without prototype), g_enter will set the
449 void g_enter (unsigned flags, unsigned argsize)
450 /* Function prologue */
452 if ((flags & CF_FIXARGC) != 0) {
453 /* Just remember the argument size for the leave */
457 AddCodeLine ("jsr enter");
464 /* Function epilogue */
466 /* How many bytes of locals do we have to drop? */
467 unsigned k = (unsigned) -StackPtr;
469 /* If we didn't have a variable argument list, don't call leave */
472 /* Drop stackframe if needed. We can only drop 255 bytes at a time. */
475 unsigned ToDrop = (k > 255)? 255 : k;
477 AddCodeLine ("jsr incsp%d", k);
480 AddCodeLine ("jsr addysp");
488 /* Nothing to drop */
489 AddCodeLine ("jsr leave");
491 /* We've a stack frame to drop */
494 AddCodeLine ("jsr addysp");
498 AddCodeLine ("jsr leavey");
502 /* Add the final rts */
508 /*****************************************************************************/
509 /* Register variables */
510 /*****************************************************************************/
514 void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
515 /* Swap a register variable with a location on the stack */
517 /* Calculate the actual stack offset and check it */
518 StackOffs -= StackPtr;
519 CheckLocalOffs (StackOffs);
524 if (CodeSizeFactor < 165) {
525 ldyconst (StackOffs);
527 AddCodeLine ("jsr regswap1");
529 ldyconst (StackOffs);
530 AddCodeLine ("lda (sp),y");
531 AddCodeLine ("ldx regbank%+d", RegOffs);
532 AddCodeLine ("sta regbank%+d", RegOffs);
534 AddCodeLine ("sta (sp),y");
537 } else if (Bytes == 2) {
539 ldyconst (StackOffs);
541 AddCodeLine ("jsr regswap2");
545 ldyconst (StackOffs);
548 AddCodeLine ("jsr regswap");
554 void g_save_regvars (int RegOffs, unsigned Bytes)
555 /* Save register variables */
557 /* Don't loop for up to two bytes */
560 AddCodeLine ("lda regbank%+d", RegOffs);
561 AddCodeLine ("jsr pusha");
563 } else if (Bytes == 2) {
565 AddCodeLine ("lda regbank%+d", RegOffs);
566 AddCodeLine ("ldx regbank%+d", RegOffs+1);
567 AddCodeLine ("jsr pushax");
571 /* More than two bytes - loop */
572 unsigned Label = GetLocalLabel ();
574 ldyconst (Bytes - 1);
576 g_defcodelabel (Label);
577 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
578 AddCodeLine ("sta (sp),y");
581 AddCodeLine ("bne %s", LocalLabelName (Label));
585 /* We pushed stuff, correct the stack pointer */
591 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
592 /* Restore register variables */
594 /* Calculate the actual stack offset and check it */
595 StackOffs -= StackPtr;
596 CheckLocalOffs (StackOffs);
598 /* Don't loop for up to two bytes */
601 ldyconst (StackOffs);
602 AddCodeLine ("lda (sp),y");
603 AddCodeLine ("sta regbank%+d", RegOffs);
605 } else if (Bytes == 2) {
607 ldyconst (StackOffs);
608 AddCodeLine ("lda (sp),y");
609 AddCodeLine ("sta regbank%+d", RegOffs);
611 AddCodeLine ("lda (sp),y");
612 AddCodeLine ("sta regbank%+d", RegOffs+1);
614 } else if (Bytes == 3 && CodeSizeFactor >= 133) {
616 ldyconst (StackOffs);
617 AddCodeLine ("lda (sp),y");
618 AddCodeLine ("sta regbank%+d", RegOffs);
620 AddCodeLine ("lda (sp),y");
621 AddCodeLine ("sta regbank%+d", RegOffs+1);
623 AddCodeLine ("lda (sp),y");
624 AddCodeLine ("sta regbank%+d", RegOffs+2);
626 } else if (StackOffs <= RegOffs) {
628 /* More bytes, but the relation between the register offset in the
629 * register bank and the stack offset allows us to generate short
630 * code that uses just one index register.
632 unsigned Label = GetLocalLabel ();
633 ldyconst (StackOffs);
634 g_defcodelabel (Label);
635 AddCodeLine ("lda (sp),y");
636 AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs);
638 AddCodeLine ("cpy #$%02X", StackOffs + Bytes);
639 AddCodeLine ("bne %s", LocalLabelName (Label));
643 /* Ok, this is the generic code. We need to save X because the
644 * caller will only save A.
646 unsigned Label = GetLocalLabel ();
647 AddCodeLine ("stx tmp1");
648 ldyconst (StackOffs + Bytes - 1);
649 ldxconst (Bytes - 1);
650 g_defcodelabel (Label);
651 AddCodeLine ("lda (sp),y");
652 AddCodeLine ("sta regbank%+d,x", RegOffs);
655 AddCodeLine ("bpl %s", LocalLabelName (Label));
656 AddCodeLine ("ldx tmp1");
663 /*****************************************************************************/
664 /* Fetching memory cells */
665 /*****************************************************************************/
669 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
670 /* Load a constant into the primary register */
672 unsigned char B1, B2, B3, B4;
676 if ((Flags & CF_CONST) != 0) {
678 /* Numeric constant */
679 switch (Flags & CF_TYPE) {
682 if ((Flags & CF_FORCECHAR) != 0) {
688 ldxconst ((Val >> 8) & 0xFF);
689 ldaconst (Val & 0xFF);
693 /* Split the value into 4 bytes */
694 B1 = (unsigned char) (Val >> 0);
695 B2 = (unsigned char) (Val >> 8);
696 B3 = (unsigned char) (Val >> 16);
697 B4 = (unsigned char) (Val >> 24);
699 /* Remember which bytes are done */
703 AddCodeLine ("ldx #$%02X", B2);
706 AddCodeLine ("stx sreg");
710 AddCodeLine ("stx sreg+1");
713 if ((Done & 0x04) == 0 && B1 != B3) {
714 AddCodeLine ("lda #$%02X", B3);
715 AddCodeLine ("sta sreg");
718 if ((Done & 0x08) == 0 && B1 != B4) {
719 AddCodeLine ("lda #$%02X", B4);
720 AddCodeLine ("sta sreg+1");
723 AddCodeLine ("lda #$%02X", B1);
725 if ((Done & 0x04) == 0) {
727 AddCodeLine ("sta sreg");
729 if ((Done & 0x08) == 0) {
731 AddCodeLine ("sta sreg+1");
743 /* Some sort of label */
744 const char* Label = GetLabelName (Flags, Val, Offs);
746 /* Load the address into the primary */
747 AddCodeLine ("lda #<(%s)", Label);
748 AddCodeLine ("ldx #>(%s)", Label);
755 void g_getstatic (unsigned flags, unsigned long label, long offs)
756 /* Fetch an static memory cell into the primary register */
758 /* Create the correct label name */
759 const char* lbuf = GetLabelName (flags, label, offs);
761 /* Check the size and generate the correct load operation */
762 switch (flags & CF_TYPE) {
765 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
766 AddCodeLine ("lda %s", lbuf); /* load A from the label */
769 AddCodeLine ("lda %s", lbuf); /* load A from the label */
770 if (!(flags & CF_UNSIGNED)) {
771 /* Must sign extend */
772 unsigned L = GetLocalLabel ();
773 AddCodeLine ("bpl %s", LocalLabelName (L));
781 AddCodeLine ("lda %s", lbuf);
782 if (flags & CF_TEST) {
783 AddCodeLine ("ora %s+1", lbuf);
785 AddCodeLine ("ldx %s+1", lbuf);
790 if (flags & CF_TEST) {
791 AddCodeLine ("lda %s+3", lbuf);
792 AddCodeLine ("ora %s+2", lbuf);
793 AddCodeLine ("ora %s+1", lbuf);
794 AddCodeLine ("ora %s+0", lbuf);
796 AddCodeLine ("lda %s+3", lbuf);
797 AddCodeLine ("sta sreg+1");
798 AddCodeLine ("lda %s+2", lbuf);
799 AddCodeLine ("sta sreg");
800 AddCodeLine ("ldx %s+1", lbuf);
801 AddCodeLine ("lda %s", lbuf);
813 void g_getlocal (unsigned flags, int offs)
814 /* Fetch specified local object (local var). */
817 CheckLocalOffs (offs);
818 switch (flags & CF_TYPE) {
821 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
823 AddCodeLine ("lda (sp),y");
826 AddCodeLine ("ldx #$00");
827 AddCodeLine ("lda (sp),y");
828 if ((flags & CF_UNSIGNED) == 0) {
829 unsigned L = GetLocalLabel();
830 AddCodeLine ("bpl %s", LocalLabelName (L));
838 CheckLocalOffs (offs + 1);
839 if (flags & CF_TEST) {
841 AddCodeLine ("lda (sp),y");
843 AddCodeLine ("ora (sp),y");
846 AddCodeLine ("jsr ldaxysp");
852 AddCodeLine ("jsr ldeaxysp");
862 void g_getind (unsigned flags, unsigned offs)
863 /* Fetch the specified object type indirect through the primary register
864 * into the primary register
867 /* If the offset is greater than 255, add the part that is > 255 to
868 * the primary. This way we get an easy addition and use the low byte
871 offs = MakeByteOffs (flags, offs);
873 /* Handle the indirect fetch */
874 switch (flags & CF_TYPE) {
877 /* Character sized */
878 if (flags & CF_UNSIGNED) {
880 AddCodeLine ("jsr ldauidx");
883 AddCodeLine ("jsr ldaidx");
888 if (flags & CF_TEST) {
890 AddCodeLine ("sta ptr1");
891 AddCodeLine ("stx ptr1+1");
892 AddCodeLine ("lda (ptr1),y");
894 AddCodeLine ("ora (ptr1),y");
897 AddCodeLine ("jsr ldaxidx");
903 AddCodeLine ("jsr ldeaxidx");
904 if (flags & CF_TEST) {
905 AddCodeLine ("jsr tsteax");
917 void g_leasp (int offs)
918 /* Fetch the address of the specified symbol into the primary register */
920 /* Calculate the offset relative to sp */
923 /* For value 0 we do direct code */
925 AddCodeLine ("lda sp");
926 AddCodeLine ("ldx sp+1");
928 if (CodeSizeFactor < 300) {
929 ldaconst (offs); /* Load A with offset value */
930 AddCodeLine ("jsr leaasp"); /* Load effective address */
932 unsigned L = GetLocalLabel ();
933 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && offs == 1) {
934 AddCodeLine ("lda sp");
935 AddCodeLine ("ldx sp+1");
937 AddCodeLine ("bne %s", LocalLabelName (L));
942 AddCodeLine ("ldx sp+1");
943 AddCodeLine ("adc sp");
944 AddCodeLine ("bcc %s", LocalLabelName (L));
954 void g_leavariadic (int Offs)
955 /* Fetch the address of a parameter in a variadic function into the primary
959 unsigned ArgSizeOffs;
961 /* Calculate the offset relative to sp */
964 /* Get the offset of the parameter which is stored at sp+0 on function
965 * entry and check if this offset is reachable with a byte offset.
967 CHECK (StackPtr <= 0);
968 ArgSizeOffs = -StackPtr;
969 CheckLocalOffs (ArgSizeOffs);
971 /* Get the size of all parameters. */
972 ldyconst (ArgSizeOffs);
973 AddCodeLine ("lda (sp),y");
975 /* Add the value of the stackpointer */
976 if (CodeSizeFactor > 250) {
977 unsigned L = GetLocalLabel();
978 AddCodeLine ("ldx sp+1");
980 AddCodeLine ("adc sp");
981 AddCodeLine ("bcc %s", LocalLabelName (L));
985 AddCodeLine ("jsr leaasp");
988 /* Add the offset to the primary */
990 g_inc (CF_INT | CF_CONST, Offs);
991 } else if (Offs < 0) {
992 g_dec (CF_INT | CF_CONST, -Offs);
998 /*****************************************************************************/
999 /* Store into memory */
1000 /*****************************************************************************/
1004 void g_putstatic (unsigned flags, unsigned long label, long offs)
1005 /* Store the primary register into the specified static memory cell */
1007 /* Create the correct label name */
1008 const char* lbuf = GetLabelName (flags, label, offs);
1010 /* Check the size and generate the correct store operation */
1011 switch (flags & CF_TYPE) {
1014 AddCodeLine ("sta %s", lbuf);
1018 AddCodeLine ("sta %s", lbuf);
1019 AddCodeLine ("stx %s+1", lbuf);
1023 AddCodeLine ("sta %s", lbuf);
1024 AddCodeLine ("stx %s+1", lbuf);
1025 AddCodeLine ("ldy sreg");
1026 AddCodeLine ("sty %s+2", lbuf);
1027 AddCodeLine ("ldy sreg+1");
1028 AddCodeLine ("sty %s+3", lbuf);
1039 void g_putlocal (unsigned Flags, int Offs, long Val)
1040 /* Put data into local object. */
1043 CheckLocalOffs (Offs);
1044 switch (Flags & CF_TYPE) {
1047 if (Flags & CF_CONST) {
1048 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1051 AddCodeLine ("sta (sp),y");
1055 if (Flags & CF_CONST) {
1057 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1058 AddCodeLine ("sta (sp),y");
1059 if ((Flags & CF_NOKEEP) == 0) {
1060 /* Place high byte into X */
1061 AddCodeLine ("tax");
1063 if ((Val & 0xFF) == Offs+1) {
1064 /* The value we need is already in Y */
1065 AddCodeLine ("tya");
1066 AddCodeLine ("dey");
1068 AddCodeLine ("dey");
1069 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1071 AddCodeLine ("sta (sp),y");
1073 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
1075 AddCodeLine ("jsr staxysp");
1078 AddCodeLine ("sta (sp),y");
1079 AddCodeLine ("iny");
1080 AddCodeLine ("txa");
1081 AddCodeLine ("sta (sp),y");
1087 if (Flags & CF_CONST) {
1088 g_getimmed (Flags, Val, 0);
1091 AddCodeLine ("jsr steaxysp");
1102 void g_putind (unsigned Flags, unsigned Offs)
1103 /* Store the specified object type in the primary register at the address
1104 * on the top of the stack
1107 /* We can handle offsets below $100 directly, larger offsets must be added
1108 * to the address. Since a/x is in use, best code is achieved by adding
1109 * just the high byte. Be sure to check if the low byte will overflow while
1112 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1114 /* Overflow - we need to add the low byte also */
1115 AddCodeLine ("ldy #$00");
1116 AddCodeLine ("clc");
1117 AddCodeLine ("pha");
1118 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1119 AddCodeLine ("adc (sp),y");
1120 AddCodeLine ("sta (sp),y");
1121 AddCodeLine ("iny");
1122 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1123 AddCodeLine ("adc (sp),y");
1124 AddCodeLine ("sta (sp),y");
1125 AddCodeLine ("pla");
1127 /* Complete address is on stack, new offset is zero */
1130 } else if ((Offs & 0xFF00) != 0) {
1132 /* We can just add the high byte */
1133 AddCodeLine ("ldy #$01");
1134 AddCodeLine ("clc");
1135 AddCodeLine ("pha");
1136 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1137 AddCodeLine ("adc (sp),y");
1138 AddCodeLine ("sta (sp),y");
1139 AddCodeLine ("pla");
1141 /* Offset is now just the low byte */
1145 /* Check the size and determine operation */
1146 switch (Flags & CF_TYPE) {
1150 AddCodeLine ("jsr staspidx");
1155 AddCodeLine ("jsr staxspidx");
1160 AddCodeLine ("jsr steaxspidx");
1168 /* Pop the argument which is always a pointer */
1174 /*****************************************************************************/
1175 /* type conversion and similiar stuff */
1176 /*****************************************************************************/
1180 void g_toslong (unsigned flags)
1181 /* Make sure, the value on TOS is a long. Convert if necessary */
1183 switch (flags & CF_TYPE) {
1187 if (flags & CF_UNSIGNED) {
1188 AddCodeLine ("jsr tosulong");
1190 AddCodeLine ("jsr toslong");
1205 void g_tosint (unsigned flags)
1206 /* Make sure, the value on TOS is an int. Convert if necessary */
1208 switch (flags & CF_TYPE) {
1215 AddCodeLine ("jsr tosint");
1226 void g_regint (unsigned Flags)
1227 /* Make sure, the value in the primary register an int. Convert if necessary */
1231 switch (Flags & CF_TYPE) {
1234 if (Flags & CF_FORCECHAR) {
1235 /* Conversion is from char */
1236 if (Flags & CF_UNSIGNED) {
1237 AddCodeLine ("ldx #$00");
1239 L = GetLocalLabel();
1240 AddCodeLine ("ldx #$00");
1241 AddCodeLine ("cmp #$80");
1242 AddCodeLine ("bcc %s", LocalLabelName (L));
1243 AddCodeLine ("dex");
1260 void g_reglong (unsigned Flags)
1261 /* Make sure, the value in the primary register a long. Convert if necessary */
1265 switch (Flags & CF_TYPE) {
1268 if (Flags & CF_FORCECHAR) {
1269 /* Conversion is from char */
1270 if (Flags & CF_UNSIGNED) {
1271 if (CodeSizeFactor >= 200) {
1272 AddCodeLine ("ldx #$00");
1273 AddCodeLine ("stx sreg");
1274 AddCodeLine ("stx sreg+1");
1276 AddCodeLine ("jsr aulong");
1279 if (CodeSizeFactor >= 366) {
1280 L = GetLocalLabel();
1281 AddCodeLine ("ldx #$00");
1282 AddCodeLine ("cmp #$80");
1283 AddCodeLine ("bcc %s", LocalLabelName (L));
1284 AddCodeLine ("dex");
1286 AddCodeLine ("stx sreg");
1287 AddCodeLine ("stx sreg+1");
1289 AddCodeLine ("jsr along");
1296 if (Flags & CF_UNSIGNED) {
1297 if (CodeSizeFactor >= 200) {
1299 AddCodeLine ("sty sreg");
1300 AddCodeLine ("sty sreg+1");
1302 AddCodeLine ("jsr axulong");
1305 AddCodeLine ("jsr axlong");
1319 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1320 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1321 * value, that corresponds to the value on TOS, rhs corresponds to the value
1322 * in (e)ax. The return value is the the flags value for the resulting type.
1325 unsigned ltype, rtype;
1328 /* Get the type spec from the flags */
1329 ltype = lhs & CF_TYPE;
1330 rtype = rhs & CF_TYPE;
1332 /* Check if a conversion is needed */
1333 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1334 /* We must promote the primary register to long */
1336 /* Get the new rhs type */
1337 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1339 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1340 /* We must promote the lhs to long */
1346 /* Get the new rhs type */
1347 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1351 /* Determine the result type for the operation:
1352 * - The result is const if both operands are const.
1353 * - The result is unsigned if one of the operands is unsigned.
1354 * - The result is long if one of the operands is long.
1355 * - Otherwise the result is int sized.
1357 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1358 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1359 if (rtype == CF_LONG || ltype == CF_LONG) {
1369 unsigned g_typecast (unsigned lhs, unsigned rhs)
1370 /* Cast the value in the primary register to the operand size that is flagged
1371 * by the lhs value. Return the result value.
1374 unsigned ltype, rtype;
1376 /* Get the type spec from the flags */
1377 ltype = lhs & CF_TYPE;
1378 rtype = rhs & CF_TYPE;
1380 /* Check if a conversion is needed */
1381 if ((rhs & CF_CONST) == 0) {
1382 if (ltype == CF_LONG && rtype != CF_LONG) {
1383 /* We must promote the primary register to long */
1385 } else if (ltype == CF_INT && rtype != CF_INT) {
1386 /* We must promote the primary register to int */
1391 /* Do not need any other action. If the left type is int, and the primary
1392 * register is long, it will be automagically truncated. If the right hand
1393 * side is const, it is not located in the primary register and handled by
1394 * the expression parser code.
1397 /* Result is const if the right hand side was const */
1398 lhs |= (rhs & CF_CONST);
1400 /* The resulting type is that of the left hand side (that's why you called
1408 void g_scale (unsigned flags, long val)
1409 /* Scale the value in the primary register by the given value. If val is positive,
1410 * scale up, is val is negative, scale down. This function is used to scale
1411 * the operands or results of pointer arithmetic by the size of the type, the
1412 * pointer points to.
1417 /* Value may not be zero */
1419 Internal ("Data type has no size");
1420 } else if (val > 0) {
1423 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1425 /* Factor is 2, 4, 8 and 16, use special function */
1426 switch (flags & CF_TYPE) {
1429 if (flags & CF_FORCECHAR) {
1431 AddCodeLine ("asl a");
1438 if (CodeSizeFactor >= (p2+1)*130U) {
1439 AddCodeLine ("stx tmp1");
1441 AddCodeLine ("asl a");
1442 AddCodeLine ("rol tmp1");
1444 AddCodeLine ("ldx tmp1");
1446 if (flags & CF_UNSIGNED) {
1447 AddCodeLine ("jsr shlax%d", p2);
1449 AddCodeLine ("jsr aslax%d", p2);
1455 if (flags & CF_UNSIGNED) {
1456 AddCodeLine ("jsr shleax%d", p2);
1458 AddCodeLine ("jsr asleax%d", p2);
1467 } else if (val != 1) {
1469 /* Use a multiplication instead */
1470 g_mul (flags | CF_CONST, val);
1478 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1480 /* Factor is 2, 4, 8 and 16 use special function */
1481 switch (flags & CF_TYPE) {
1484 if (flags & CF_FORCECHAR) {
1485 if (flags & CF_UNSIGNED) {
1487 AddCodeLine ("lsr a");
1490 } else if (p2 <= 2) {
1491 AddCodeLine ("cmp #$80");
1492 AddCodeLine ("ror a");
1499 if (flags & CF_UNSIGNED) {
1500 if (CodeSizeFactor >= (p2+1)*130U) {
1501 AddCodeLine ("stx tmp1");
1503 AddCodeLine ("lsr tmp1");
1504 AddCodeLine ("ror a");
1506 AddCodeLine ("ldx tmp1");
1508 AddCodeLine ("jsr lsrax%d", p2);
1511 if (CodeSizeFactor >= (p2+1)*150U) {
1512 AddCodeLine ("stx tmp1");
1514 AddCodeLine ("cpx #$80");
1515 AddCodeLine ("ror tmp1");
1516 AddCodeLine ("ror a");
1518 AddCodeLine ("ldx tmp1");
1520 AddCodeLine ("jsr asrax%d", p2);
1526 if (flags & CF_UNSIGNED) {
1527 AddCodeLine ("jsr lsreax%d", p2);
1529 AddCodeLine ("jsr asreax%d", p2);
1538 } else if (val != 1) {
1540 /* Use a division instead */
1541 g_div (flags | CF_CONST, val);
1549 /*****************************************************************************/
1550 /* Adds and subs of variables fix a fixed address */
1551 /*****************************************************************************/
1555 void g_addlocal (unsigned flags, int offs)
1556 /* Add a local variable to ax */
1560 /* Correct the offset and check it */
1562 CheckLocalOffs (offs);
1564 switch (flags & CF_TYPE) {
1567 L = GetLocalLabel();
1568 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1569 AddCodeLine ("clc");
1570 AddCodeLine ("adc (sp),y");
1571 AddCodeLine ("bcc %s", LocalLabelName (L));
1572 AddCodeLine ("inx");
1577 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1578 AddCodeLine ("clc");
1579 AddCodeLine ("adc (sp),y");
1580 AddCodeLine ("pha");
1581 AddCodeLine ("txa");
1582 AddCodeLine ("iny");
1583 AddCodeLine ("adc (sp),y");
1584 AddCodeLine ("tax");
1585 AddCodeLine ("pla");
1589 /* Do it the old way */
1591 g_getlocal (flags, offs);
1603 void g_addstatic (unsigned flags, unsigned long label, long offs)
1604 /* Add a static variable to ax */
1608 /* Create the correct label name */
1609 const char* lbuf = GetLabelName (flags, label, offs);
1611 switch (flags & CF_TYPE) {
1614 L = GetLocalLabel();
1615 AddCodeLine ("clc");
1616 AddCodeLine ("adc %s", lbuf);
1617 AddCodeLine ("bcc %s", LocalLabelName (L));
1618 AddCodeLine ("inx");
1623 AddCodeLine ("clc");
1624 AddCodeLine ("adc %s", lbuf);
1625 AddCodeLine ("tay");
1626 AddCodeLine ("txa");
1627 AddCodeLine ("adc %s+1", lbuf);
1628 AddCodeLine ("tax");
1629 AddCodeLine ("tya");
1633 /* Do it the old way */
1635 g_getstatic (flags, label, offs);
1647 /*****************************************************************************/
1648 /* Special op= functions */
1649 /*****************************************************************************/
1653 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1655 /* Emit += for a static variable */
1657 /* Create the correct label name */
1658 const char* lbuf = GetLabelName (flags, label, offs);
1660 /* Check the size and determine operation */
1661 switch (flags & CF_TYPE) {
1664 if (flags & CF_FORCECHAR) {
1665 AddCodeLine ("ldx #$00");
1666 if (flags & CF_CONST) {
1668 AddCodeLine ("inc %s", lbuf);
1669 AddCodeLine ("lda %s", lbuf);
1671 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1672 AddCodeLine ("clc");
1673 AddCodeLine ("adc %s", lbuf);
1674 AddCodeLine ("sta %s", lbuf);
1677 AddCodeLine ("clc");
1678 AddCodeLine ("adc %s", lbuf);
1679 AddCodeLine ("sta %s", lbuf);
1681 if ((flags & CF_UNSIGNED) == 0) {
1682 unsigned L = GetLocalLabel();
1683 AddCodeLine ("bpl %s", LocalLabelName (L));
1684 AddCodeLine ("dex");
1692 if (flags & CF_CONST) {
1694 unsigned L = GetLocalLabel ();
1695 AddCodeLine ("inc %s", lbuf);
1696 AddCodeLine ("bne %s", LocalLabelName (L));
1697 AddCodeLine ("inc %s+1", lbuf);
1699 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1700 AddCodeLine ("ldx %s+1", lbuf);
1702 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1703 AddCodeLine ("clc");
1704 AddCodeLine ("adc %s", lbuf);
1705 AddCodeLine ("sta %s", lbuf);
1707 unsigned L = GetLocalLabel ();
1708 AddCodeLine ("bcc %s", LocalLabelName (L));
1709 AddCodeLine ("inc %s+1", lbuf);
1711 AddCodeLine ("ldx %s+1", lbuf);
1713 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1714 AddCodeLine ("adc %s+1", lbuf);
1715 AddCodeLine ("sta %s+1", lbuf);
1716 AddCodeLine ("tax");
1717 AddCodeLine ("lda %s", lbuf);
1721 AddCodeLine ("clc");
1722 AddCodeLine ("adc %s", lbuf);
1723 AddCodeLine ("sta %s", lbuf);
1724 AddCodeLine ("txa");
1725 AddCodeLine ("adc %s+1", lbuf);
1726 AddCodeLine ("sta %s+1", lbuf);
1727 AddCodeLine ("tax");
1728 AddCodeLine ("lda %s", lbuf);
1733 if (flags & CF_CONST) {
1735 AddCodeLine ("ldy #<(%s)", lbuf);
1736 AddCodeLine ("sty ptr1");
1737 AddCodeLine ("ldy #>(%s)", lbuf);
1739 AddCodeLine ("jsr laddeq1");
1741 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1742 AddCodeLine ("jsr laddeqa");
1745 g_getstatic (flags, label, offs);
1747 g_putstatic (flags, label, offs);
1750 AddCodeLine ("ldy #<(%s)", lbuf);
1751 AddCodeLine ("sty ptr1");
1752 AddCodeLine ("ldy #>(%s)", lbuf);
1753 AddCodeLine ("jsr laddeq");
1764 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1765 /* Emit += for a local variable */
1767 /* Calculate the true offset, check it, load it into Y */
1769 CheckLocalOffs (offs);
1771 /* Check the size and determine operation */
1772 switch (flags & CF_TYPE) {
1775 if (flags & CF_FORCECHAR) {
1777 AddCodeLine ("ldx #$00");
1778 if (flags & CF_CONST) {
1779 AddCodeLine ("clc");
1780 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1781 AddCodeLine ("adc (sp),y");
1782 AddCodeLine ("sta (sp),y");
1784 AddCodeLine ("clc");
1785 AddCodeLine ("adc (sp),y");
1786 AddCodeLine ("sta (sp),y");
1788 if ((flags & CF_UNSIGNED) == 0) {
1789 unsigned L = GetLocalLabel();
1790 AddCodeLine ("bpl %s", LocalLabelName (L));
1791 AddCodeLine ("dex");
1800 if (flags & CF_CONST) {
1801 if (CodeSizeFactor >= 400) {
1802 AddCodeLine ("clc");
1803 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1804 AddCodeLine ("adc (sp),y");
1805 AddCodeLine ("sta (sp),y");
1806 AddCodeLine ("iny");
1807 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1808 AddCodeLine ("adc (sp),y");
1809 AddCodeLine ("sta (sp),y");
1810 AddCodeLine ("tax");
1811 AddCodeLine ("dey");
1812 AddCodeLine ("lda (sp),y");
1814 g_getimmed (flags, val, 0);
1815 AddCodeLine ("jsr addeqysp");
1818 AddCodeLine ("jsr addeqysp");
1823 if (flags & CF_CONST) {
1824 g_getimmed (flags, val, 0);
1827 AddCodeLine ("jsr laddeqysp");
1837 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1838 /* Emit += for the location with address in ax */
1840 /* If the offset is too large for a byte register, add the high byte
1841 * of the offset to the primary. Beware: We need a special correction
1842 * if the offset in the low byte will overflow in the operation.
1844 offs = MakeByteOffs (flags, offs);
1846 /* Check the size and determine operation */
1847 switch (flags & CF_TYPE) {
1850 AddCodeLine ("sta ptr1");
1851 AddCodeLine ("stx ptr1+1");
1852 AddCodeLine ("ldy #$%02X", offs);
1853 AddCodeLine ("ldx #$00");
1854 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1855 AddCodeLine ("clc");
1856 AddCodeLine ("adc (ptr1),y");
1857 AddCodeLine ("sta (ptr1),y");
1861 if (CodeSizeFactor >= 200) {
1862 /* Lots of code, use only if size is not important */
1863 AddCodeLine ("sta ptr1");
1864 AddCodeLine ("stx ptr1+1");
1865 AddCodeLine ("ldy #$%02X", offs);
1866 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1867 AddCodeLine ("clc");
1868 AddCodeLine ("adc (ptr1),y");
1869 AddCodeLine ("sta (ptr1),y");
1870 AddCodeLine ("pha");
1871 AddCodeLine ("iny");
1872 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1873 AddCodeLine ("adc (ptr1),y");
1874 AddCodeLine ("sta (ptr1),y");
1875 AddCodeLine ("tax");
1876 AddCodeLine ("pla");
1882 AddCodeLine ("jsr pushax"); /* Push the address */
1883 push (CF_PTR); /* Correct the internal sp */
1884 g_getind (flags, offs); /* Fetch the value */
1885 g_inc (flags, val); /* Increment value in primary */
1886 g_putind (flags, offs); /* Store the value back */
1896 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1898 /* Emit -= for a static variable */
1900 /* Create the correct label name */
1901 const char* lbuf = GetLabelName (flags, label, offs);
1903 /* Check the size and determine operation */
1904 switch (flags & CF_TYPE) {
1907 if (flags & CF_FORCECHAR) {
1908 AddCodeLine ("ldx #$00");
1909 if (flags & CF_CONST) {
1911 AddCodeLine ("dec %s", lbuf);
1912 AddCodeLine ("lda %s", lbuf);
1914 AddCodeLine ("sec");
1915 AddCodeLine ("lda %s", lbuf);
1916 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1917 AddCodeLine ("sta %s", lbuf);
1920 AddCodeLine ("eor #$FF");
1921 AddCodeLine ("sec");
1922 AddCodeLine ("adc %s", lbuf);
1923 AddCodeLine ("sta %s", lbuf);
1925 if ((flags & CF_UNSIGNED) == 0) {
1926 unsigned L = GetLocalLabel();
1927 AddCodeLine ("bpl %s", LocalLabelName (L));
1928 AddCodeLine ("dex");
1936 AddCodeLine ("sec");
1937 if (flags & CF_CONST) {
1938 AddCodeLine ("lda %s", lbuf);
1939 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1940 AddCodeLine ("sta %s", lbuf);
1942 unsigned L = GetLocalLabel ();
1943 AddCodeLine ("bcs %s", LocalLabelName (L));
1944 AddCodeLine ("dec %s+1", lbuf);
1946 AddCodeLine ("ldx %s+1", lbuf);
1948 AddCodeLine ("lda %s+1", lbuf);
1949 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1950 AddCodeLine ("sta %s+1", lbuf);
1951 AddCodeLine ("tax");
1952 AddCodeLine ("lda %s", lbuf);
1955 AddCodeLine ("eor #$FF");
1956 AddCodeLine ("adc %s", lbuf);
1957 AddCodeLine ("sta %s", lbuf);
1958 AddCodeLine ("txa");
1959 AddCodeLine ("eor #$FF");
1960 AddCodeLine ("adc %s+1", lbuf);
1961 AddCodeLine ("sta %s+1", lbuf);
1962 AddCodeLine ("tax");
1963 AddCodeLine ("lda %s", lbuf);
1968 if (flags & CF_CONST) {
1970 AddCodeLine ("ldy #<(%s)", lbuf);
1971 AddCodeLine ("sty ptr1");
1972 AddCodeLine ("ldy #>(%s)", lbuf);
1973 AddCodeLine ("lda #$%02X", (unsigned char)val);
1974 AddCodeLine ("jsr lsubeqa");
1976 g_getstatic (flags, label, offs);
1978 g_putstatic (flags, label, offs);
1981 AddCodeLine ("ldy #<(%s)", lbuf);
1982 AddCodeLine ("sty ptr1");
1983 AddCodeLine ("ldy #>(%s)", lbuf);
1984 AddCodeLine ("jsr lsubeq");
1995 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1996 /* Emit -= for a local variable */
1998 /* Calculate the true offset, check it, load it into Y */
2000 CheckLocalOffs (offs);
2002 /* Check the size and determine operation */
2003 switch (flags & CF_TYPE) {
2006 if (flags & CF_FORCECHAR) {
2008 AddCodeLine ("ldx #$00");
2009 AddCodeLine ("sec");
2010 if (flags & CF_CONST) {
2011 AddCodeLine ("lda (sp),y");
2012 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2014 AddCodeLine ("eor #$FF");
2015 AddCodeLine ("adc (sp),y");
2017 AddCodeLine ("sta (sp),y");
2018 if ((flags & CF_UNSIGNED) == 0) {
2019 unsigned L = GetLocalLabel();
2020 AddCodeLine ("bpl %s", LocalLabelName (L));
2021 AddCodeLine ("dex");
2029 if (flags & CF_CONST) {
2030 g_getimmed (flags, val, 0);
2033 AddCodeLine ("jsr subeqysp");
2037 if (flags & CF_CONST) {
2038 g_getimmed (flags, val, 0);
2041 AddCodeLine ("jsr lsubeqysp");
2051 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2052 /* Emit -= for the location with address in ax */
2054 /* If the offset is too large for a byte register, add the high byte
2055 * of the offset to the primary. Beware: We need a special correction
2056 * if the offset in the low byte will overflow in the operation.
2058 offs = MakeByteOffs (flags, offs);
2060 /* Check the size and determine operation */
2061 switch (flags & CF_TYPE) {
2064 AddCodeLine ("sta ptr1");
2065 AddCodeLine ("stx ptr1+1");
2066 AddCodeLine ("ldy #$%02X", offs);
2067 AddCodeLine ("ldx #$00");
2068 AddCodeLine ("lda (ptr1),y");
2069 AddCodeLine ("sec");
2070 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2071 AddCodeLine ("sta (ptr1),y");
2075 if (CodeSizeFactor >= 200) {
2076 /* Lots of code, use only if size is not important */
2077 AddCodeLine ("sta ptr1");
2078 AddCodeLine ("stx ptr1+1");
2079 AddCodeLine ("ldy #$%02X", offs);
2080 AddCodeLine ("lda (ptr1),y");
2081 AddCodeLine ("sec");
2082 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2083 AddCodeLine ("sta (ptr1),y");
2084 AddCodeLine ("pha");
2085 AddCodeLine ("iny");
2086 AddCodeLine ("lda (ptr1),y");
2087 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2088 AddCodeLine ("sta (ptr1),y");
2089 AddCodeLine ("tax");
2090 AddCodeLine ("pla");
2096 AddCodeLine ("jsr pushax"); /* Push the address */
2097 push (CF_PTR); /* Correct the internal sp */
2098 g_getind (flags, offs); /* Fetch the value */
2099 g_dec (flags, val); /* Increment value in primary */
2100 g_putind (flags, offs); /* Store the value back */
2110 /*****************************************************************************/
2111 /* Add a variable address to the value in ax */
2112 /*****************************************************************************/
2116 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2117 /* Add the address of a local variable to ax */
2121 /* Add the offset */
2124 /* We cannot address more then 256 bytes of locals anyway */
2125 L = GetLocalLabel();
2126 CheckLocalOffs (offs);
2127 AddCodeLine ("clc");
2128 AddCodeLine ("adc #$%02X", offs & 0xFF);
2129 /* Do also skip the CLC insn below */
2130 AddCodeLine ("bcc %s", LocalLabelName (L));
2131 AddCodeLine ("inx");
2134 /* Add the current stackpointer value */
2135 AddCodeLine ("clc");
2137 /* Label was used above */
2140 AddCodeLine ("adc sp");
2141 AddCodeLine ("tay");
2142 AddCodeLine ("txa");
2143 AddCodeLine ("adc sp+1");
2144 AddCodeLine ("tax");
2145 AddCodeLine ("tya");
2150 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2151 /* Add the address of a static variable to ax */
2153 /* Create the correct label name */
2154 const char* lbuf = GetLabelName (flags, label, offs);
2156 /* Add the address to the current ax value */
2157 AddCodeLine ("clc");
2158 AddCodeLine ("adc #<(%s)", lbuf);
2159 AddCodeLine ("tay");
2160 AddCodeLine ("txa");
2161 AddCodeLine ("adc #>(%s)", lbuf);
2162 AddCodeLine ("tax");
2163 AddCodeLine ("tya");
2168 /*****************************************************************************/
2170 /*****************************************************************************/
2174 void g_save (unsigned flags)
2175 /* Copy primary register to hold register. */
2177 /* Check the size and determine operation */
2178 switch (flags & CF_TYPE) {
2181 if (flags & CF_FORCECHAR) {
2182 AddCodeLine ("pha");
2188 AddCodeLine ("sta regsave");
2189 AddCodeLine ("stx regsave+1");
2193 AddCodeLine ("jsr saveeax");
2203 void g_restore (unsigned flags)
2204 /* Copy hold register to primary. */
2206 /* Check the size and determine operation */
2207 switch (flags & CF_TYPE) {
2210 if (flags & CF_FORCECHAR) {
2211 AddCodeLine ("pla");
2217 AddCodeLine ("lda regsave");
2218 AddCodeLine ("ldx regsave+1");
2222 AddCodeLine ("jsr resteax");
2232 void g_cmp (unsigned flags, unsigned long val)
2233 /* Immidiate compare. The primary register will not be changed, Z flag
2239 /* Check the size and determine operation */
2240 switch (flags & CF_TYPE) {
2243 if (flags & CF_FORCECHAR) {
2244 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2250 L = GetLocalLabel();
2251 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2252 AddCodeLine ("bne %s", LocalLabelName (L));
2253 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2258 Internal ("g_cmp: Long compares not implemented");
2268 static void oper (unsigned flags, unsigned long val, char** subs)
2269 /* Encode a binary operation. subs is a pointer to four groups of three
2271 * 0-2 --> Operate on ints
2272 * 3-5 --> Operate on unsigneds
2273 * 6-8 --> Operate on longs
2274 * 9-11 --> Operate on unsigned longs
2276 * The first subroutine names in each string group is used to encode an
2277 * operation with a zero constant, the second to encode an operation with
2278 * a 8 bit constant, and the third is used in all other cases.
2283 /* Determine the offset into the array */
2284 offs = (flags & CF_UNSIGNED)? 3 : 0;
2285 switch (flags & CF_TYPE) {
2298 /* Encode the operation */
2299 if (flags & CF_CONST) {
2300 /* Constant value given */
2301 if (val == 0 && subs [offs+0]) {
2302 /* Special case: constant with value zero */
2303 AddCodeLine ("jsr %s", subs [offs+0]);
2304 } else if (val < 0x100 && subs [offs+1]) {
2305 /* Special case: constant with high byte zero */
2306 ldaconst (val); /* Load low byte */
2307 AddCodeLine ("jsr %s", subs [offs+1]);
2309 /* Others: arbitrary constant value */
2310 g_getimmed (flags, val, 0); /* Load value */
2311 AddCodeLine ("jsr %s", subs [offs+2]);
2314 /* Value not constant (is already in (e)ax) */
2315 AddCodeLine ("jsr %s", subs [offs+2]);
2318 /* The operation will pop it's argument */
2324 void g_test (unsigned flags)
2325 /* Test the value in the primary and set the condition codes */
2327 switch (flags & CF_TYPE) {
2330 if (flags & CF_FORCECHAR) {
2331 AddCodeLine ("tax");
2337 AddCodeLine ("stx tmp1");
2338 AddCodeLine ("ora tmp1");
2342 if (flags & CF_UNSIGNED) {
2343 AddCodeLine ("jsr utsteax");
2345 AddCodeLine ("jsr tsteax");
2357 void g_push (unsigned flags, unsigned long val)
2358 /* Push the primary register or a constant value onto the stack */
2360 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2362 /* We have a constant 8 or 16 bit value */
2363 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2365 /* Handle as 8 bit value */
2367 AddCodeLine ("jsr pusha");
2371 /* Handle as 16 bit value */
2372 g_getimmed (flags, val, 0);
2373 AddCodeLine ("jsr pushax");
2378 /* Value is not 16 bit or not constant */
2379 if (flags & CF_CONST) {
2380 /* Constant 32 bit value, load into eax */
2381 g_getimmed (flags, val, 0);
2384 /* Push the primary register */
2385 switch (flags & CF_TYPE) {
2388 if (flags & CF_FORCECHAR) {
2389 /* Handle as char */
2390 AddCodeLine ("jsr pusha");
2395 AddCodeLine ("jsr pushax");
2399 AddCodeLine ("jsr pusheax");
2409 /* Adjust the stack offset */
2415 void g_swap (unsigned flags)
2416 /* Swap the primary register and the top of the stack. flags give the type
2417 * of *both* values (must have same size).
2420 switch (flags & CF_TYPE) {
2424 AddCodeLine ("jsr swapstk");
2428 AddCodeLine ("jsr swapestk");
2439 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2440 /* Call the specified subroutine name */
2442 if ((Flags & CF_FIXARGC) == 0) {
2443 /* Pass the argument count */
2446 AddCodeLine ("jsr _%s", Label);
2447 StackPtr += ArgSize; /* callee pops args */
2452 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2453 /* Call subroutine indirect */
2455 if ((Flags & CF_LOCAL) == 0) {
2456 /* Address is in a/x */
2457 if ((Flags & CF_FIXARGC) == 0) {
2458 /* Pass arg count */
2461 AddCodeLine ("jsr callax");
2463 /* The address is on stack, offset is on Val */
2465 CheckLocalOffs (Offs);
2466 AddCodeLine ("pha");
2467 AddCodeLine ("ldy #$%02X", Offs);
2468 AddCodeLine ("lda (sp),y");
2469 AddCodeLine ("sta jmpvec+1");
2470 AddCodeLine ("iny");
2471 AddCodeLine ("lda (sp),y");
2472 AddCodeLine ("sta jmpvec+2");
2473 AddCodeLine ("pla");
2474 AddCodeLine ("jsr jmpvec");
2477 /* Callee pops args */
2478 StackPtr += ArgSize;
2483 void g_jump (unsigned Label)
2484 /* Jump to specified internal label number */
2486 AddCodeLine ("jmp %s", LocalLabelName (Label));
2491 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2492 /* Jump to label if zero flag clear */
2494 AddCodeLine ("jne %s", LocalLabelName (label));
2499 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2500 /* Jump to label if zero flag set */
2502 AddCodeLine ("jeq %s", LocalLabelName (label));
2507 static void mod_internal (int k, char* verb1, char* verb2)
2510 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2514 AddCodeLine ("jsr %ssp", verb2);
2520 void g_space (int space)
2521 /* Create or drop space on the stack */
2524 mod_internal (-space, "inc", "addy");
2525 } else if (space > 0) {
2526 mod_internal (space, "dec", "suby");
2532 void g_cstackcheck (void)
2533 /* Check for a C stack overflow */
2535 AddCodeLine ("jsr cstkchk");
2540 void g_stackcheck (void)
2541 /* Check for a stack overflow */
2543 AddCodeLine ("jsr stkchk");
2548 void g_add (unsigned flags, unsigned long val)
2549 /* Primary = TOS + Primary */
2551 static char* ops [12] = {
2552 0, "tosadda0", "tosaddax",
2553 0, "tosadda0", "tosaddax",
2558 if (flags & CF_CONST) {
2559 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2560 g_push (flags & ~CF_CONST, 0);
2562 oper (flags, val, ops);
2567 void g_sub (unsigned flags, unsigned long val)
2568 /* Primary = TOS - Primary */
2570 static char* ops [12] = {
2571 0, "tossuba0", "tossubax",
2572 0, "tossuba0", "tossubax",
2577 if (flags & CF_CONST) {
2578 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2579 g_push (flags & ~CF_CONST, 0);
2581 oper (flags, val, ops);
2586 void g_rsub (unsigned flags, unsigned long val)
2587 /* Primary = Primary - TOS */
2589 static char* ops [12] = {
2590 0, "tosrsuba0", "tosrsubax",
2591 0, "tosrsuba0", "tosrsubax",
2595 oper (flags, val, ops);
2600 void g_mul (unsigned flags, unsigned long val)
2601 /* Primary = TOS * Primary */
2603 static char* ops [12] = {
2604 0, "tosmula0", "tosmulax",
2605 0, "tosumula0", "tosumulax",
2612 /* Do strength reduction if the value is constant and a power of two */
2613 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2614 /* Generate a shift instead */
2619 /* If the right hand side is const, the lhs is not on stack but still
2620 * in the primary register.
2622 if (flags & CF_CONST) {
2624 switch (flags & CF_TYPE) {
2627 if (flags & CF_FORCECHAR) {
2628 /* Handle some special cases */
2632 AddCodeLine ("sta tmp1");
2633 AddCodeLine ("asl a");
2634 AddCodeLine ("clc");
2635 AddCodeLine ("adc tmp1");
2639 AddCodeLine ("sta tmp1");
2640 AddCodeLine ("asl a");
2641 AddCodeLine ("asl a");
2642 AddCodeLine ("clc");
2643 AddCodeLine ("adc tmp1");
2647 AddCodeLine ("sta tmp1");
2648 AddCodeLine ("asl a");
2649 AddCodeLine ("clc");
2650 AddCodeLine ("adc tmp1");
2651 AddCodeLine ("asl a");
2655 AddCodeLine ("sta tmp1");
2656 AddCodeLine ("asl a");
2657 AddCodeLine ("asl a");
2658 AddCodeLine ("clc");
2659 AddCodeLine ("adc tmp1");
2660 AddCodeLine ("asl a");
2669 AddCodeLine ("jsr mulax3");
2672 AddCodeLine ("jsr mulax5");
2675 AddCodeLine ("jsr mulax6");
2678 AddCodeLine ("jsr mulax7");
2681 AddCodeLine ("jsr mulax9");
2684 AddCodeLine ("jsr mulax10");
2696 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2697 * into the normal, non-optimized stuff.
2699 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2700 g_push (flags & ~CF_CONST, 0);
2704 /* Use long way over the stack */
2705 oper (flags, val, ops);
2710 void g_div (unsigned flags, unsigned long val)
2711 /* Primary = TOS / Primary */
2713 static char* ops [12] = {
2714 0, "tosdiva0", "tosdivax",
2715 0, "tosudiva0", "tosudivax",
2720 /* Do strength reduction if the value is constant and a power of two */
2722 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2723 /* Generate a shift instead */
2726 /* Generate a division */
2727 if (flags & CF_CONST) {
2728 /* lhs is not on stack */
2729 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2730 g_push (flags & ~CF_CONST, 0);
2732 oper (flags, val, ops);
2738 void g_mod (unsigned flags, unsigned long val)
2739 /* Primary = TOS % Primary */
2741 static char* ops [12] = {
2742 0, "tosmoda0", "tosmodax",
2743 0, "tosumoda0", "tosumodax",
2749 /* Check if we can do some cost reduction */
2750 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2751 /* We can do that with an AND operation */
2752 g_and (flags, val - 1);
2754 /* Do it the hard way... */
2755 if (flags & CF_CONST) {
2756 /* lhs is not on stack */
2757 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2758 g_push (flags & ~CF_CONST, 0);
2760 oper (flags, val, ops);
2766 void g_or (unsigned flags, unsigned long val)
2767 /* Primary = TOS | Primary */
2769 static char* ops [12] = {
2770 0, "tosora0", "tosorax",
2771 0, "tosora0", "tosorax",
2776 /* If the right hand side is const, the lhs is not on stack but still
2777 * in the primary register.
2779 if (flags & CF_CONST) {
2781 switch (flags & CF_TYPE) {
2784 if (flags & CF_FORCECHAR) {
2785 if ((val & 0xFF) != 0) {
2786 AddCodeLine ("ora #$%02X", (unsigned char)val);
2794 if ((val & 0xFF) != 0) {
2795 AddCodeLine ("ora #$%02X", (unsigned char)val);
2798 } else if ((val & 0xFF00) == 0xFF00) {
2799 if ((val & 0xFF) != 0) {
2800 AddCodeLine ("ora #$%02X", (unsigned char)val);
2809 if ((val & 0xFF) != 0) {
2810 AddCodeLine ("ora #$%02X", (unsigned char)val);
2820 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2821 * into the normal, non-optimized stuff. Note: The standard stuff will
2822 * always work with ints.
2824 flags &= ~CF_FORCECHAR;
2825 g_push (flags & ~CF_CONST, 0);
2828 /* Use long way over the stack */
2829 oper (flags, val, ops);
2834 void g_xor (unsigned flags, unsigned long val)
2835 /* Primary = TOS ^ Primary */
2837 static char* ops [12] = {
2838 0, "tosxora0", "tosxorax",
2839 0, "tosxora0", "tosxorax",
2845 /* If the right hand side is const, the lhs is not on stack but still
2846 * in the primary register.
2848 if (flags & CF_CONST) {
2850 switch (flags & CF_TYPE) {
2853 if (flags & CF_FORCECHAR) {
2854 if ((val & 0xFF) != 0) {
2855 AddCodeLine ("eor #$%02X", (unsigned char)val);
2864 AddCodeLine ("eor #$%02X", (unsigned char)val);
2867 } else if ((val & 0xFF) == 0) {
2868 AddCodeLine ("pha");
2869 AddCodeLine ("txa");
2870 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2871 AddCodeLine ("tax");
2872 AddCodeLine ("pla");
2880 AddCodeLine ("eor #$%02X", (unsigned char)val);
2890 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2891 * into the normal, non-optimized stuff. Note: The standard stuff will
2892 * always work with ints.
2894 flags &= ~CF_FORCECHAR;
2895 g_push (flags & ~CF_CONST, 0);
2898 /* Use long way over the stack */
2899 oper (flags, val, ops);
2904 void g_and (unsigned Flags, unsigned long Val)
2905 /* Primary = TOS & Primary */
2907 static char* ops [12] = {
2908 0, "tosanda0", "tosandax",
2909 0, "tosanda0", "tosandax",
2914 /* If the right hand side is const, the lhs is not on stack but still
2915 * in the primary register.
2917 if (Flags & CF_CONST) {
2919 switch (Flags & CF_TYPE) {
2922 if (Flags & CF_FORCECHAR) {
2923 if ((Val & 0xFF) != 0xFF) {
2924 AddCodeLine ("and #$%02X", (unsigned char)Val);
2930 if ((Val & 0xFFFF) != 0xFFFF) {
2935 } else if (Val != 0xFF) {
2936 AddCodeLine ("and #$%02X", (unsigned char)Val);
2938 } else if ((Val & 0xFF00) == 0xFF00) {
2939 AddCodeLine ("and #$%02X", (unsigned char)Val);
2940 } else if ((Val & 0x00FF) == 0x0000) {
2941 AddCodeLine ("txa");
2942 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2943 AddCodeLine ("tax");
2946 AddCodeLine ("tay");
2947 AddCodeLine ("txa");
2948 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2949 AddCodeLine ("tax");
2950 AddCodeLine ("tya");
2951 if ((Val & 0x00FF) != 0x00FF) {
2952 AddCodeLine ("and #$%02X", (unsigned char)Val);
2961 AddCodeLine ("stx sreg+1");
2962 AddCodeLine ("stx sreg");
2963 if ((Val & 0xFF) != 0xFF) {
2964 AddCodeLine ("and #$%02X", (unsigned char)Val);
2967 } else if (Val == 0xFF00) {
2969 AddCodeLine ("sta sreg+1");
2970 AddCodeLine ("sta sreg");
2979 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2980 * into the normal, non-optimized stuff. Note: The standard stuff will
2981 * always work with ints.
2983 Flags &= ~CF_FORCECHAR;
2984 g_push (Flags & ~CF_CONST, 0);
2987 /* Use long way over the stack */
2988 oper (Flags, Val, ops);
2993 void g_asr (unsigned flags, unsigned long val)
2994 /* Primary = TOS >> Primary */
2996 static char* ops [12] = {
2997 0, "tosasra0", "tosasrax",
2998 0, "tosshra0", "tosshrax",
3003 /* If the right hand side is const, the lhs is not on stack but still
3004 * in the primary register.
3006 if (flags & CF_CONST) {
3008 switch (flags & CF_TYPE) {
3012 if (val >= 8 && (flags & CF_UNSIGNED)) {
3013 AddCodeLine ("txa");
3020 } else if (val >= 1 && val <= 4) {
3021 if (flags & CF_UNSIGNED) {
3022 AddCodeLine ("jsr shrax%ld", val);
3024 AddCodeLine ("jsr asrax%ld", val);
3034 } else if (val >= 1 && val <= 4) {
3035 if (flags & CF_UNSIGNED) {
3036 AddCodeLine ("jsr shreax%ld", val);
3038 AddCodeLine ("jsr asreax%ld", val);
3041 } else if (val == 8 && (flags & CF_UNSIGNED)) {
3042 AddCodeLine ("txa");
3043 AddCodeLine ("ldx sreg");
3044 AddCodeLine ("ldy sreg+1");
3045 AddCodeLine ("sty sreg");
3046 AddCodeLine ("ldy #$00");
3047 AddCodeLine ("sty sreg+1");
3049 } else if (val == 16) {
3050 AddCodeLine ("ldy #$00");
3051 AddCodeLine ("ldx sreg+1");
3052 if ((flags & CF_UNSIGNED) == 0) {
3053 unsigned L = GetLocalLabel();
3054 AddCodeLine ("bpl %s", LocalLabelName (L));
3055 AddCodeLine ("dey");
3058 AddCodeLine ("lda sreg");
3059 AddCodeLine ("sty sreg+1");
3060 AddCodeLine ("sty sreg");
3069 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3070 * into the normal, non-optimized stuff. Note: The standard stuff will
3071 * always work with ints.
3073 flags &= ~CF_FORCECHAR;
3074 g_push (flags & ~CF_CONST, 0);
3077 /* Use long way over the stack */
3078 oper (flags, val, ops);
3083 void g_asl (unsigned flags, unsigned long val)
3084 /* Primary = TOS << Primary */
3086 static char* ops [12] = {
3087 0, "tosasla0", "tosaslax",
3088 0, "tosshla0", "tosshlax",
3094 /* If the right hand side is const, the lhs is not on stack but still
3095 * in the primary register.
3097 if (flags & CF_CONST) {
3099 switch (flags & CF_TYPE) {
3104 AddCodeLine ("tax");
3105 AddCodeLine ("lda #$00");
3111 } else if (val >= 1 && val <= 4) {
3112 if (flags & CF_UNSIGNED) {
3113 AddCodeLine ("jsr shlax%ld", val);
3115 AddCodeLine ("jsr aslax%ld", val);
3125 } else if (val >= 1 && val <= 4) {
3126 if (flags & CF_UNSIGNED) {
3127 AddCodeLine ("jsr shleax%ld", val);
3129 AddCodeLine ("jsr asleax%ld", val);
3132 } else if (val == 8) {
3133 AddCodeLine ("ldy sreg");
3134 AddCodeLine ("sty sreg+1");
3135 AddCodeLine ("stx sreg");
3136 AddCodeLine ("tax");
3137 AddCodeLine ("lda #$00");
3139 } else if (val == 16) {
3140 AddCodeLine ("stx sreg+1");
3141 AddCodeLine ("sta sreg");
3142 AddCodeLine ("lda #$00");
3143 AddCodeLine ("tax");
3152 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3153 * into the normal, non-optimized stuff. Note: The standard stuff will
3154 * always work with ints.
3156 flags &= ~CF_FORCECHAR;
3157 g_push (flags & ~CF_CONST, 0);
3160 /* Use long way over the stack */
3161 oper (flags, val, ops);
3166 void g_neg (unsigned Flags)
3167 /* Primary = -Primary */
3169 switch (Flags & CF_TYPE) {
3172 if (Flags & CF_FORCECHAR) {
3173 AddCodeLine ("eor #$FF");
3174 AddCodeLine ("clc");
3175 AddCodeLine ("adc #$01");
3181 AddCodeLine ("jsr negax");
3185 AddCodeLine ("jsr negeax");
3195 void g_bneg (unsigned flags)
3196 /* Primary = !Primary */
3198 switch (flags & CF_TYPE) {
3201 AddCodeLine ("jsr bnega");
3205 AddCodeLine ("jsr bnegax");
3209 AddCodeLine ("jsr bnegeax");
3219 void g_com (unsigned Flags)
3220 /* Primary = ~Primary */
3222 switch (Flags & CF_TYPE) {
3225 if (Flags & CF_FORCECHAR) {
3226 AddCodeLine ("eor #$FF");
3232 AddCodeLine ("jsr complax");
3236 AddCodeLine ("jsr compleax");
3246 void g_inc (unsigned flags, unsigned long val)
3247 /* Increment the primary register by a given number */
3249 /* Don't inc by zero */
3254 /* Generate code for the supported types */
3256 switch (flags & CF_TYPE) {
3259 if (flags & CF_FORCECHAR) {
3260 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3262 AddCodeLine ("ina");
3265 AddCodeLine ("clc");
3266 AddCodeLine ("adc #$%02X", (unsigned char)val);
3273 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) {
3274 unsigned L = GetLocalLabel();
3275 AddCodeLine ("ina");
3276 AddCodeLine ("bne %s", LocalLabelName (L));
3277 AddCodeLine ("inx");
3279 } else if (CodeSizeFactor < 200) {
3282 AddCodeLine ("jsr incax%lu", val);
3283 } else if (val <= 255) {
3285 AddCodeLine ("jsr incaxy");
3287 g_add (flags | CF_CONST, val);
3290 /* Inline the code */
3292 if ((val & 0xFF) != 0) {
3293 unsigned L = GetLocalLabel();
3294 AddCodeLine ("clc");
3295 AddCodeLine ("adc #$%02X", (unsigned char) val);
3296 AddCodeLine ("bcc %s", LocalLabelName (L));
3297 AddCodeLine ("inx");
3301 AddCodeLine ("inx");
3304 AddCodeLine ("inx");
3307 AddCodeLine ("inx");
3310 AddCodeLine ("clc");
3311 if ((val & 0xFF) != 0) {
3312 AddCodeLine ("adc #$%02X", (unsigned char) val);
3314 AddCodeLine ("pha");
3315 AddCodeLine ("txa");
3316 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3317 AddCodeLine ("tax");
3318 AddCodeLine ("pla");
3326 AddCodeLine ("jsr inceaxy");
3328 g_add (flags | CF_CONST, val);
3340 void g_dec (unsigned flags, unsigned long val)
3341 /* Decrement the primary register by a given number */
3343 /* Don't dec by zero */
3348 /* Generate code for the supported types */
3350 switch (flags & CF_TYPE) {
3353 if (flags & CF_FORCECHAR) {
3354 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3356 AddCodeLine ("dea");
3359 AddCodeLine ("sec");
3360 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3367 if (CodeSizeFactor < 200) {
3368 /* Use subroutines */
3370 AddCodeLine ("jsr decax%d", (int) val);
3371 } else if (val <= 255) {
3373 AddCodeLine ("jsr decaxy");
3375 g_sub (flags | CF_CONST, val);
3378 /* Inline the code */
3380 if ((val & 0xFF) != 0) {
3381 unsigned L = GetLocalLabel();
3382 AddCodeLine ("sec");
3383 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3384 AddCodeLine ("bcs %s", LocalLabelName (L));
3385 AddCodeLine ("dex");
3389 AddCodeLine ("dex");
3392 AddCodeLine ("dex");
3395 AddCodeLine ("sec");
3396 if ((val & 0xFF) != 0) {
3397 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3399 AddCodeLine ("pha");
3400 AddCodeLine ("txa");
3401 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3402 AddCodeLine ("tax");
3403 AddCodeLine ("pla");
3411 AddCodeLine ("jsr deceaxy");
3413 g_sub (flags | CF_CONST, val);
3426 * Following are the conditional operators. They compare the TOS against
3427 * the primary and put a literal 1 in the primary if the condition is
3428 * true, otherwise they clear the primary register
3433 void g_eq (unsigned flags, unsigned long val)
3434 /* Test for equal */
3436 static char* ops [12] = {
3437 "toseq00", "toseqa0", "toseqax",
3438 "toseq00", "toseqa0", "toseqax",
3445 /* If the right hand side is const, the lhs is not on stack but still
3446 * in the primary register.
3448 if (flags & CF_CONST) {
3450 switch (flags & CF_TYPE) {
3453 if (flags & CF_FORCECHAR) {
3454 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3455 AddCodeLine ("jsr booleq");
3461 L = GetLocalLabel();
3462 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3463 AddCodeLine ("bne %s", LocalLabelName (L));
3464 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3466 AddCodeLine ("jsr booleq");
3476 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3477 * into the normal, non-optimized stuff. Note: The standard stuff will
3478 * always work with ints.
3480 flags &= ~CF_FORCECHAR;
3481 g_push (flags & ~CF_CONST, 0);
3484 /* Use long way over the stack */
3485 oper (flags, val, ops);
3490 void g_ne (unsigned flags, unsigned long val)
3491 /* Test for not equal */
3493 static char* ops [12] = {
3494 "tosne00", "tosnea0", "tosneax",
3495 "tosne00", "tosnea0", "tosneax",
3502 /* If the right hand side is const, the lhs is not on stack but still
3503 * in the primary register.
3505 if (flags & CF_CONST) {
3507 switch (flags & CF_TYPE) {
3510 if (flags & CF_FORCECHAR) {
3511 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3512 AddCodeLine ("jsr boolne");
3518 L = GetLocalLabel();
3519 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3520 AddCodeLine ("bne %s", LocalLabelName (L));
3521 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3523 AddCodeLine ("jsr boolne");
3533 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3534 * into the normal, non-optimized stuff. Note: The standard stuff will
3535 * always work with ints.
3537 flags &= ~CF_FORCECHAR;
3538 g_push (flags & ~CF_CONST, 0);
3541 /* Use long way over the stack */
3542 oper (flags, val, ops);
3547 void g_lt (unsigned flags, unsigned long val)
3548 /* Test for less than */
3550 static char* ops [12] = {
3551 "toslt00", "toslta0", "tosltax",
3552 "tosult00", "tosulta0", "tosultax",
3557 /* If the right hand side is const, the lhs is not on stack but still
3558 * in the primary register.
3560 if (flags & CF_CONST) {
3562 /* Because the handling of the overflow flag is too complex for
3563 * inlining, we can handle only unsigned compares, and signed
3564 * compares against zero here.
3566 if (flags & CF_UNSIGNED) {
3568 /* Give a warning in some special cases */
3570 Warning ("Condition is never true");
3571 AddCodeLine ("jsr return0");
3575 /* Look at the type */
3576 switch (flags & CF_TYPE) {
3579 if (flags & CF_FORCECHAR) {
3580 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3581 AddCodeLine ("jsr boolult");
3587 /* If the low byte is zero, we must only test the high byte */
3588 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3589 if ((val & 0xFF) != 0) {
3590 unsigned L = GetLocalLabel();
3591 AddCodeLine ("bne %s", LocalLabelName (L));
3592 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3595 AddCodeLine ("jsr boolult");
3599 /* Do a subtraction */
3600 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3601 AddCodeLine ("txa");
3602 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3603 AddCodeLine ("lda sreg");
3604 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3605 AddCodeLine ("lda sreg+1");
3606 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3607 AddCodeLine ("jsr boolult");
3614 } else if (val == 0) {
3616 /* Look at the type */
3617 switch (flags & CF_TYPE) {
3620 if (flags & CF_FORCECHAR) {
3621 AddCodeLine ("tax");
3622 AddCodeLine ("jsr boollt");
3628 /* Just check the high byte */
3629 AddCodeLine ("txa");
3630 AddCodeLine ("jsr boollt");
3634 /* Just check the high byte */
3635 AddCodeLine ("lda sreg+1");
3636 AddCodeLine ("jsr boollt");
3644 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3645 * into the normal, non-optimized stuff. Note: The standard stuff will
3646 * always work with ints.
3648 flags &= ~CF_FORCECHAR;
3649 g_push (flags & ~CF_CONST, 0);
3652 /* Use long way over the stack */
3653 oper (flags, val, ops);
3658 void g_le (unsigned flags, unsigned long val)
3659 /* Test for less than or equal to */
3661 static char* ops [12] = {
3662 "tosle00", "toslea0", "tosleax",
3663 "tosule00", "tosulea0", "tosuleax",
3669 /* If the right hand side is const, the lhs is not on stack but still
3670 * in the primary register.
3672 if (flags & CF_CONST) {
3674 /* Look at the type */
3675 switch (flags & CF_TYPE) {
3678 if (flags & CF_FORCECHAR) {
3679 if (flags & CF_UNSIGNED) {
3680 /* Unsigned compare */
3682 /* Use < instead of <= because the former gives
3683 * better code on the 6502 than the latter.
3685 g_lt (flags, val+1);
3688 Warning ("Condition is always true");
3689 AddCodeLine ("jsr return1");
3692 /* Signed compare */
3693 if ((long) val < 0x7F) {
3694 /* Use < instead of <= because the former gives
3695 * better code on the 6502 than the latter.
3697 g_lt (flags, val+1);
3700 Warning ("Condition is always true");
3701 AddCodeLine ("jsr return1");
3709 if (flags & CF_UNSIGNED) {
3710 /* Unsigned compare */
3712 /* Use < instead of <= because the former gives
3713 * better code on the 6502 than the latter.
3715 g_lt (flags, val+1);
3718 Warning ("Condition is always true");
3719 AddCodeLine ("jsr return1");
3722 /* Signed compare */
3723 if ((long) val < 0x7FFF) {
3724 g_lt (flags, val+1);
3727 Warning ("Condition is always true");
3728 AddCodeLine ("jsr return1");
3734 if (flags & CF_UNSIGNED) {
3735 /* Unsigned compare */
3736 if (val < 0xFFFFFFFF) {
3737 /* Use < instead of <= because the former gives
3738 * better code on the 6502 than the latter.
3740 g_lt (flags, val+1);
3743 Warning ("Condition is always true");
3744 AddCodeLine ("jsr return1");
3747 /* Signed compare */
3748 if ((long) val < 0x7FFFFFFF) {
3749 g_lt (flags, val+1);
3752 Warning ("Condition is always true");
3753 AddCodeLine ("jsr return1");
3762 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3763 * into the normal, non-optimized stuff. Note: The standard stuff will
3764 * always work with ints.
3766 flags &= ~CF_FORCECHAR;
3767 g_push (flags & ~CF_CONST, 0);
3770 /* Use long way over the stack */
3771 oper (flags, val, ops);
3776 void g_gt (unsigned flags, unsigned long val)
3777 /* Test for greater than */
3779 static char* ops [12] = {
3780 "tosgt00", "tosgta0", "tosgtax",
3781 "tosugt00", "tosugta0", "tosugtax",
3787 /* If the right hand side is const, the lhs is not on stack but still
3788 * in the primary register.
3790 if (flags & CF_CONST) {
3792 /* Look at the type */
3793 switch (flags & CF_TYPE) {
3796 if (flags & CF_FORCECHAR) {
3797 if (flags & CF_UNSIGNED) {
3799 /* If we have a compare > 0, we will replace it by
3800 * != 0 here, since both are identical but the
3801 * latter is easier to optimize.
3804 } else if (val < 0xFF) {
3805 /* Use >= instead of > because the former gives
3806 * better code on the 6502 than the latter.
3808 g_ge (flags, val+1);
3811 Warning ("Condition is never true");
3812 AddCodeLine ("jsr return0");
3815 if ((long) val < 0x7F) {
3816 /* Use >= instead of > because the former gives
3817 * better code on the 6502 than the latter.
3819 g_ge (flags, val+1);
3822 Warning ("Condition is never true");
3823 AddCodeLine ("jsr return0");
3831 if (flags & CF_UNSIGNED) {
3832 /* Unsigned compare */
3834 /* If we have a compare > 0, we will replace it by
3835 * != 0 here, since both are identical but the latter
3836 * is easier to optimize.
3839 } else if (val < 0xFFFF) {
3840 /* Use >= instead of > because the former gives better
3841 * code on the 6502 than the latter.
3843 g_ge (flags, val+1);
3846 Warning ("Condition is never true");
3847 AddCodeLine ("jsr return0");
3850 /* Signed compare */
3851 if ((long) val < 0x7FFF) {
3852 g_ge (flags, val+1);
3855 Warning ("Condition is never true");
3856 AddCodeLine ("jsr return0");
3862 if (flags & CF_UNSIGNED) {
3863 /* Unsigned compare */
3865 /* If we have a compare > 0, we will replace it by
3866 * != 0 here, since both are identical but the latter
3867 * is easier to optimize.
3870 } else if (val < 0xFFFFFFFF) {
3871 /* Use >= instead of > because the former gives better
3872 * code on the 6502 than the latter.
3874 g_ge (flags, val+1);
3877 Warning ("Condition is never true");
3878 AddCodeLine ("jsr return0");
3881 /* Signed compare */
3882 if ((long) val < 0x7FFFFFFF) {
3883 g_ge (flags, val+1);
3886 Warning ("Condition is never true");
3887 AddCodeLine ("jsr return0");
3896 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3897 * into the normal, non-optimized stuff. Note: The standard stuff will
3898 * always work with ints.
3900 flags &= ~CF_FORCECHAR;
3901 g_push (flags & ~CF_CONST, 0);
3904 /* Use long way over the stack */
3905 oper (flags, val, ops);
3910 void g_ge (unsigned flags, unsigned long val)
3911 /* Test for greater than or equal to */
3913 static char* ops [12] = {
3914 "tosge00", "tosgea0", "tosgeax",
3915 "tosuge00", "tosugea0", "tosugeax",
3921 /* If the right hand side is const, the lhs is not on stack but still
3922 * in the primary register.
3924 if (flags & CF_CONST) {
3926 /* Because the handling of the overflow flag is too complex for
3927 * inlining, we can handle only unsigned compares, and signed
3928 * compares against zero here.
3930 if (flags & CF_UNSIGNED) {
3932 /* Give a warning in some special cases */
3934 Warning ("Condition is always true");
3935 AddCodeLine ("jsr return1");
3939 /* Look at the type */
3940 switch (flags & CF_TYPE) {
3943 if (flags & CF_FORCECHAR) {
3944 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3945 AddCodeLine ("jsr booluge");
3951 /* If the low byte is zero, we must only test the high byte */
3952 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3953 if ((val & 0xFF) != 0) {
3954 unsigned L = GetLocalLabel();
3955 AddCodeLine ("bne %s", LocalLabelName (L));
3956 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3959 AddCodeLine ("jsr booluge");
3963 /* Do a subtraction */
3964 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3965 AddCodeLine ("txa");
3966 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3967 AddCodeLine ("lda sreg");
3968 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3969 AddCodeLine ("lda sreg+1");
3970 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3971 AddCodeLine ("jsr booluge");
3978 } else if (val == 0) {
3980 /* Look at the type */
3981 switch (flags & CF_TYPE) {
3984 if (flags & CF_FORCECHAR) {
3985 AddCodeLine ("tax");
3986 AddCodeLine ("jsr boolge");
3992 /* Just test the high byte */
3993 AddCodeLine ("txa");
3994 AddCodeLine ("jsr boolge");
3998 /* Just test the high byte */
3999 AddCodeLine ("lda sreg+1");
4000 AddCodeLine ("jsr boolge");
4009 /* If we go here, we didn't emit code. Push the lhs on stack and fall
4010 * into the normal, non-optimized stuff. Note: The standard stuff will
4011 * always work with ints.
4013 flags &= ~CF_FORCECHAR;
4014 g_push (flags & ~CF_CONST, 0);
4017 /* Use long way over the stack */
4018 oper (flags, val, ops);
4023 /*****************************************************************************/
4024 /* Allocating static storage */
4025 /*****************************************************************************/
4029 void g_res (unsigned n)
4030 /* Reserve static storage, n bytes */
4032 AddDataLine ("\t.res\t%u,$00", n);
4037 void g_defdata (unsigned flags, unsigned long val, long offs)
4038 /* Define data with the size given in flags */
4040 if (flags & CF_CONST) {
4042 /* Numeric constant */
4043 switch (flags & CF_TYPE) {
4046 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
4050 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
4054 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
4065 /* Create the correct label name */
4066 const char* Label = GetLabelName (flags, val, offs);
4068 /* Labels are always 16 bit */
4069 AddDataLine ("\t.addr\t%s", Label);
4076 void g_defbytes (const void* Bytes, unsigned Count)
4077 /* Output a row of bytes as a constant */
4083 /* Cast the buffer pointer */
4084 const unsigned char* Data = (const unsigned char*) Bytes;
4086 /* Output the stuff */
4089 /* How many go into this line? */
4090 if ((Chunk = Count) > 16) {
4095 /* Output one line */
4096 strcpy (Buf, "\t.byte\t");
4099 B += sprintf (B, "$%02X", *Data++);
4105 /* Output the line */
4112 void g_zerobytes (unsigned Count)
4113 /* Output Count bytes of data initialized with zero */
4116 AddDataLine ("\t.res\t%u,$00", Count);
4122 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4123 /* Initialize a register variable from static initialization data */
4125 /* Register variables do always have less than 128 bytes */
4126 unsigned CodeLabel = GetLocalLabel ();
4128 g_defcodelabel (CodeLabel);
4129 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4130 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4131 AddCodeLine ("dex");
4132 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4137 void g_initauto (unsigned Label, unsigned Size)
4138 /* Initialize a local variable at stack offset zero from static data */
4140 unsigned CodeLabel = GetLocalLabel ();
4142 CheckLocalOffs (Size);
4145 g_defcodelabel (CodeLabel);
4146 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4147 AddCodeLine ("sta (sp),y");
4148 AddCodeLine ("dey");
4149 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4150 } else if (Size <= 256) {
4152 g_defcodelabel (CodeLabel);
4153 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4154 AddCodeLine ("sta (sp),y");
4155 AddCodeLine ("iny");
4156 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4157 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4163 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4164 /* Initialize a static local variable from static initialization data */
4167 unsigned CodeLabel = GetLocalLabel ();
4169 g_defcodelabel (CodeLabel);
4170 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4171 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4172 AddCodeLine ("dey");
4173 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4174 } else if (Size <= 256) {
4175 unsigned CodeLabel = GetLocalLabel ();
4177 g_defcodelabel (CodeLabel);
4178 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4179 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4180 AddCodeLine ("iny");
4181 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4182 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4184 /* Use the easy way here: memcpy */
4185 g_getimmed (CF_STATIC, VarLabel, 0);
4186 AddCodeLine ("jsr pushax");
4187 g_getimmed (CF_STATIC, InitLabel, 0);
4188 AddCodeLine ("jsr pushax");
4189 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4190 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4196 /*****************************************************************************/
4197 /* Switch statement */
4198 /*****************************************************************************/
4202 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4203 /* Generate code for a switch statement */
4205 unsigned NextLabel = 0;
4208 /* Setup registers and determine which compare insn to use */
4209 const char* Compare;
4212 Compare = "cmp #$%02X";
4215 Compare = "cpx #$%02X";
4218 AddCodeLine ("ldy sreg");
4219 Compare = "cpy #$%02X";
4222 AddCodeLine ("ldy sreg+1");
4223 Compare = "cpy #$%02X";
4226 Internal ("Invalid depth in g_switch: %u", Depth);
4229 /* Walk over all nodes */
4230 for (I = 0; I < CollCount (Nodes); ++I) {
4232 /* Get the next case node */
4233 CaseNode* N = CollAtUnchecked (Nodes, I);
4235 /* If we have a next label, define it */
4237 g_defcodelabel (NextLabel);
4241 /* Do the compare */
4242 AddCodeLine (Compare, CN_GetValue (N));
4244 /* If this is the last level, jump directly to the case code if found */
4247 /* Branch if equal */
4248 g_falsejump (0, CN_GetLabel (N));
4252 /* Determine the next label */
4253 if (I == CollCount (Nodes) - 1) {
4254 /* Last node means not found */
4255 g_truejump (0, DefaultLabel);
4257 /* Jump to the next check */
4258 NextLabel = GetLocalLabel ();
4259 g_truejump (0, NextLabel);
4262 /* Check the next level */
4263 g_switch (N->Nodes, DefaultLabel, Depth-1);
4268 /* If we go here, we haven't found the label */
4269 g_jump (DefaultLabel);
4274 /*****************************************************************************/
4275 /* User supplied assembler code */
4276 /*****************************************************************************/
4280 void g_asmcode (struct StrBuf* B)
4281 /* Output one line of assembler code. */
4283 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));