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 /* Special handling for floats here: */
74 if ((type & CF_TYPE) == CF_FLOAT) {
75 Fatal ("Floating point type is currently unsupported");
77 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
83 static void CheckLocalOffs (unsigned Offs)
84 /* Check the offset into the stack for 8bit range */
87 /* Too many local vars */
88 Error ("Too many local variables");
94 static const char* GetLabelName (unsigned Flags, unsigned long Label, long Offs)
96 static char Buf [256]; /* Label name */
98 /* Create the correct label name */
99 switch (Flags & CF_ADDRMASK) {
102 /* Static memory cell */
104 xsprintf (Buf, sizeof (Buf), "%s%+ld", LocalLabelName (Label), Offs);
106 xsprintf (Buf, sizeof (Buf), "%s", LocalLabelName (Label));
113 xsprintf (Buf, sizeof (Buf), "_%s%+ld", (char*) Label, Offs);
115 xsprintf (Buf, sizeof (Buf), "_%s", (char*) Label);
120 /* Absolute address */
121 xsprintf (Buf, sizeof (Buf), "$%04X", (int)((Label+Offs) & 0xFFFF));
125 /* Variable in register bank */
126 xsprintf (Buf, sizeof (Buf), "regbank+%u", (unsigned)((Label+Offs) & 0xFFFF));
130 Internal ("Invalid address flags: %04X", Flags);
133 /* Return a pointer to the static buffer */
139 /*****************************************************************************/
140 /* Pre- and postamble */
141 /*****************************************************************************/
145 void g_preamble (void)
146 /* Generate the assembler code preamble */
148 /* Create a new (global) segment list and remember it */
152 /* Identify the compiler version */
154 AddTextLine ("; File generated by cc65 v %s", GetVersionAsString ());
157 /* Insert some object file options */
158 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %s\"",
159 GetVersionAsString ());
161 /* If we're producing code for some other CPU, switch the command set */
163 case CPU_6502: AddTextLine ("\t.setcpu\t\t\"6502\""); break;
164 case CPU_6502X: AddTextLine ("\t.setcpu\t\t\"6502X\""); break;
165 case CPU_65SC02: AddTextLine ("\t.setcpu\t\t\"65SC02\""); break;
166 case CPU_65C02: AddTextLine ("\t.setcpu\t\t\"65C02\""); break;
167 case CPU_65816: AddTextLine ("\t.setcpu\t\t\"65816\""); break;
168 case CPU_HUC6280: AddTextLine ("\t.setcpu\t\t\"HUC6280\""); break;
169 default: Internal ("Unknown CPU: %d", CPU);
173 AddTextLine ("\t.smart\t\ton");
175 /* Allow auto import for runtime library routines */
176 AddTextLine ("\t.autoimport\ton");
178 /* Switch the assembler into case sensitive mode */
179 AddTextLine ("\t.case\t\ton");
181 /* Tell the assembler if we want to generate debug info */
182 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
184 /* Import the stack pointer for direct auto variable access */
185 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
187 /* Define long branch macros */
188 AddTextLine ("\t.macpack\tlongbranch");
193 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
194 /* If debug info is enabled, place a file info into the source */
197 /* We have to place this into the global text segment, so it will
198 * appear before all .dbg line statements.
200 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
206 /*****************************************************************************/
207 /* Segment support */
208 /*****************************************************************************/
212 void g_userodata (void)
213 /* Switch to the read only data segment */
215 UseDataSeg (SEG_RODATA);
220 void g_usedata (void)
221 /* Switch to the data segment */
223 UseDataSeg (SEG_DATA);
229 /* Switch to the bss segment */
231 UseDataSeg (SEG_BSS);
236 void g_segname (segment_t Seg)
237 /* Emit the name of a segment if necessary */
239 /* Emit a segment directive for the data style segments */
242 case SEG_RODATA: S = CS->ROData; break;
243 case SEG_DATA: S = CS->Data; break;
244 case SEG_BSS: S = CS->BSS; break;
245 default: S = 0; break;
248 DS_AddLine (S, ".segment\t\"%s\"", GetSegName (Seg));
254 /*****************************************************************************/
256 /*****************************************************************************/
260 unsigned sizeofarg (unsigned flags)
261 /* Return the size of a function argument type that is encoded in flags */
263 switch (flags & CF_TYPE) {
266 return (flags & CF_FORCECHAR)? 1 : 2;
286 int pop (unsigned flags)
287 /* Pop an argument of the given size */
289 return StackPtr += sizeofarg (flags);
294 int push (unsigned flags)
295 /* Push an argument of the given size */
297 return StackPtr -= sizeofarg (flags);
302 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
303 /* The value in Offs is an offset to an address in a/x. Make sure, an object
304 * of the type given in Flags can be loaded or stored into this address by
305 * adding part of the offset to the address in ax, so that the remaining
306 * offset fits into an index register. Return the remaining offset.
309 /* If the offset is too large for a byte register, add the high byte
310 * of the offset to the primary. Beware: We need a special correction
311 * if the offset in the low byte will overflow in the operation.
313 unsigned O = Offs & ~0xFFU;
314 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
315 /* We need to add the low byte also */
319 /* Do the correction if we need one */
321 g_inc (CF_INT | CF_CONST, O);
325 /* Return the new offset */
331 /*****************************************************************************/
332 /* Functions handling local labels */
333 /*****************************************************************************/
337 void g_defcodelabel (unsigned label)
338 /* Define a local code label */
340 CS_AddLabel (CS->Code, LocalLabelName (label));
345 void g_defdatalabel (unsigned label)
346 /* Define a local data label */
348 AddDataLine ("%s:", LocalLabelName (label));
353 /*****************************************************************************/
354 /* Functions handling global labels */
355 /*****************************************************************************/
359 void g_defgloblabel (const char* Name)
360 /* Define a global label with the given name */
362 /* Global labels are always data labels */
363 AddDataLine ("_%s:", Name);
368 void g_defexport (const char* Name, int ZP)
369 /* Export the given label */
372 AddTextLine ("\t.exportzp\t_%s", Name);
374 AddTextLine ("\t.export\t\t_%s", Name);
380 void g_defimport (const char* Name, int ZP)
381 /* Import the given label */
384 AddTextLine ("\t.importzp\t_%s", Name);
386 AddTextLine ("\t.import\t\t_%s", Name);
392 void g_importstartup (void)
393 /* Forced import of the startup module */
395 AddTextLine ("\t.forceimport\t__STARTUP__");
400 void g_importmainargs (void)
401 /* Forced import of a special symbol that handles arguments to main */
403 AddTextLine ("\t.forceimport\tinitmainargs");
408 /*****************************************************************************/
409 /* Load functions for various registers */
410 /*****************************************************************************/
414 static void ldxconst (unsigned val)
415 /* Load x with a constant */
417 AddCodeLine ("ldx #$%02X", val & 0xFF);
422 static void ldyconst (unsigned val)
423 /* Load y with a constant */
425 AddCodeLine ("ldy #$%02X", val & 0xFF);
430 /*****************************************************************************/
431 /* Function entry and exit */
432 /*****************************************************************************/
436 /* Remember the argument size of a function. The variable is set by g_enter
437 * and used by g_leave. If the functions gets its argument size by the caller
438 * (variable param list or function without prototype), g_enter will set the
444 void g_enter (unsigned flags, unsigned argsize)
445 /* Function prologue */
447 if ((flags & CF_FIXARGC) != 0) {
448 /* Just remember the argument size for the leave */
452 AddCodeLine ("jsr enter");
459 /* Function epilogue */
461 /* How many bytes of locals do we have to drop? */
462 unsigned k = (unsigned) -StackPtr;
464 /* If we didn't have a variable argument list, don't call leave */
467 /* Drop stackframe if needed. We can only drop 255 bytes at a time. */
470 unsigned ToDrop = (k > 255)? 255 : k;
472 AddCodeLine ("jsr incsp%d", k);
475 AddCodeLine ("jsr addysp");
483 /* Nothing to drop */
484 AddCodeLine ("jsr leave");
486 /* We've a stack frame to drop */
488 AddCodeLine ("ldy #$FF");
489 AddCodeLine ("jsr addysp");
493 AddCodeLine ("jsr leavey");
497 /* Add the final rts */
503 /*****************************************************************************/
504 /* Register variables */
505 /*****************************************************************************/
509 void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
510 /* Swap a register variable with a location on the stack */
512 /* Calculate the actual stack offset and check it */
513 StackOffs -= StackPtr;
514 CheckLocalOffs (StackOffs);
517 AddCodeLine ("ldy #$%02X", StackOffs & 0xFF);
520 if (IS_Get (&CodeSizeFactor) < 165) {
521 AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
522 AddCodeLine ("jsr regswap1");
524 AddCodeLine ("lda (sp),y");
525 AddCodeLine ("ldx regbank%+d", RegOffs);
526 AddCodeLine ("sta regbank%+d", RegOffs);
528 AddCodeLine ("sta (sp),y");
531 } else if (Bytes == 2) {
533 AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
534 AddCodeLine ("jsr regswap2");
538 AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
539 AddCodeLine ("lda #$%02X", Bytes & 0xFF);
540 AddCodeLine ("jsr regswap");
546 void g_save_regvars (int RegOffs, unsigned Bytes)
547 /* Save register variables */
549 /* Don't loop for up to two bytes */
552 AddCodeLine ("lda regbank%+d", RegOffs);
553 AddCodeLine ("jsr pusha");
555 } else if (Bytes == 2) {
557 AddCodeLine ("lda regbank%+d", RegOffs);
558 AddCodeLine ("ldx regbank%+d", RegOffs+1);
559 AddCodeLine ("jsr pushax");
563 /* More than two bytes - loop */
564 unsigned Label = GetLocalLabel ();
566 ldyconst (Bytes - 1);
568 g_defcodelabel (Label);
569 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
570 AddCodeLine ("sta (sp),y");
573 AddCodeLine ("bne %s", LocalLabelName (Label));
577 /* We pushed stuff, correct the stack pointer */
583 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
584 /* Restore register variables */
586 /* Calculate the actual stack offset and check it */
587 StackOffs -= StackPtr;
588 CheckLocalOffs (StackOffs);
590 /* Don't loop for up to two bytes */
593 ldyconst (StackOffs);
594 AddCodeLine ("lda (sp),y");
595 AddCodeLine ("sta regbank%+d", RegOffs);
597 } else if (Bytes == 2) {
599 ldyconst (StackOffs);
600 AddCodeLine ("lda (sp),y");
601 AddCodeLine ("sta regbank%+d", RegOffs);
603 AddCodeLine ("lda (sp),y");
604 AddCodeLine ("sta regbank%+d", RegOffs+1);
606 } else if (Bytes == 3 && IS_Get (&CodeSizeFactor) >= 133) {
608 ldyconst (StackOffs);
609 AddCodeLine ("lda (sp),y");
610 AddCodeLine ("sta regbank%+d", RegOffs);
612 AddCodeLine ("lda (sp),y");
613 AddCodeLine ("sta regbank%+d", RegOffs+1);
615 AddCodeLine ("lda (sp),y");
616 AddCodeLine ("sta regbank%+d", RegOffs+2);
618 } else if (StackOffs <= RegOffs) {
620 /* More bytes, but the relation between the register offset in the
621 * register bank and the stack offset allows us to generate short
622 * code that uses just one index register.
624 unsigned Label = GetLocalLabel ();
625 ldyconst (StackOffs);
626 g_defcodelabel (Label);
627 AddCodeLine ("lda (sp),y");
628 AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs);
630 AddCodeLine ("cpy #$%02X", StackOffs + Bytes);
631 AddCodeLine ("bne %s", LocalLabelName (Label));
635 /* Ok, this is the generic code. We need to save X because the
636 * caller will only save A.
638 unsigned Label = GetLocalLabel ();
639 AddCodeLine ("stx tmp1");
640 ldyconst (StackOffs + Bytes - 1);
641 ldxconst (Bytes - 1);
642 g_defcodelabel (Label);
643 AddCodeLine ("lda (sp),y");
644 AddCodeLine ("sta regbank%+d,x", RegOffs);
647 AddCodeLine ("bpl %s", LocalLabelName (Label));
648 AddCodeLine ("ldx tmp1");
655 /*****************************************************************************/
656 /* Fetching memory cells */
657 /*****************************************************************************/
661 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
662 /* Load a constant into the primary register */
664 unsigned char B1, B2, B3, B4;
668 if ((Flags & CF_CONST) != 0) {
670 /* Numeric constant */
671 switch (Flags & CF_TYPE) {
674 if ((Flags & CF_FORCECHAR) != 0) {
675 AddCodeLine ("lda #$%02X", (unsigned char) Val);
680 AddCodeLine ("ldx #$%02X", (unsigned char) (Val >> 8));
681 AddCodeLine ("lda #$%02X", (unsigned char) Val);
685 /* Split the value into 4 bytes */
686 B1 = (unsigned char) (Val >> 0);
687 B2 = (unsigned char) (Val >> 8);
688 B3 = (unsigned char) (Val >> 16);
689 B4 = (unsigned char) (Val >> 24);
691 /* Remember which bytes are done */
695 AddCodeLine ("ldx #$%02X", B2);
698 AddCodeLine ("stx sreg");
702 AddCodeLine ("stx sreg+1");
705 if ((Done & 0x04) == 0 && B1 != B3) {
706 AddCodeLine ("lda #$%02X", B3);
707 AddCodeLine ("sta sreg");
710 if ((Done & 0x08) == 0 && B1 != B4) {
711 AddCodeLine ("lda #$%02X", B4);
712 AddCodeLine ("sta sreg+1");
715 AddCodeLine ("lda #$%02X", B1);
717 if ((Done & 0x04) == 0) {
719 AddCodeLine ("sta sreg");
721 if ((Done & 0x08) == 0) {
723 AddCodeLine ("sta sreg+1");
735 /* Some sort of label */
736 const char* Label = GetLabelName (Flags, Val, Offs);
738 /* Load the address into the primary */
739 AddCodeLine ("lda #<(%s)", Label);
740 AddCodeLine ("ldx #>(%s)", Label);
747 void g_getstatic (unsigned flags, unsigned long label, long offs)
748 /* Fetch an static memory cell into the primary register */
750 /* Create the correct label name */
751 const char* lbuf = GetLabelName (flags, label, offs);
753 /* Check the size and generate the correct load operation */
754 switch (flags & CF_TYPE) {
757 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
758 AddCodeLine ("lda %s", lbuf); /* load A from the label */
761 AddCodeLine ("lda %s", lbuf); /* load A from the label */
762 if (!(flags & CF_UNSIGNED)) {
763 /* Must sign extend */
764 unsigned L = GetLocalLabel ();
765 AddCodeLine ("bpl %s", LocalLabelName (L));
773 AddCodeLine ("lda %s", lbuf);
774 if (flags & CF_TEST) {
775 AddCodeLine ("ora %s+1", lbuf);
777 AddCodeLine ("ldx %s+1", lbuf);
782 if (flags & CF_TEST) {
783 AddCodeLine ("lda %s+3", lbuf);
784 AddCodeLine ("ora %s+2", lbuf);
785 AddCodeLine ("ora %s+1", lbuf);
786 AddCodeLine ("ora %s+0", lbuf);
788 AddCodeLine ("lda %s+3", lbuf);
789 AddCodeLine ("sta sreg+1");
790 AddCodeLine ("lda %s+2", lbuf);
791 AddCodeLine ("sta sreg");
792 AddCodeLine ("ldx %s+1", lbuf);
793 AddCodeLine ("lda %s", lbuf);
805 void g_getlocal (unsigned Flags, int Offs)
806 /* Fetch specified local object (local var). */
809 switch (Flags & CF_TYPE) {
812 CheckLocalOffs (Offs);
813 if ((Flags & CF_FORCECHAR) || (Flags & CF_TEST)) {
815 AddCodeLine ("lda (sp),y");
818 AddCodeLine ("ldx #$00");
819 AddCodeLine ("lda (sp),y");
820 if ((Flags & CF_UNSIGNED) == 0) {
821 unsigned L = GetLocalLabel();
822 AddCodeLine ("bpl %s", LocalLabelName (L));
830 CheckLocalOffs (Offs + 1);
831 AddCodeLine ("ldy #$%02X", (unsigned char) (Offs+1));
832 if (Flags & CF_TEST) {
833 AddCodeLine ("lda (sp),y");
835 AddCodeLine ("ora (sp),y");
836 } else if (IS_Get (&CodeSizeFactor) < 165) {
837 AddCodeLine ("jsr ldaxysp");
839 AddCodeLine ("lda (sp),y");
842 AddCodeLine ("lda (sp),y");
847 CheckLocalOffs (Offs + 3);
848 AddCodeLine ("ldy #$%02X", (unsigned char) (Offs+3));
849 AddCodeLine ("jsr ldeaxysp");
850 if (Flags & CF_TEST) {
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) {
917 void g_leasp (int Offs)
918 /* Fetch the address of the specified symbol into the primary register */
920 unsigned char Lo, Hi;
922 /* Calculate the offset relative to sp */
925 /* Get low and high byte */
926 Lo = (unsigned char) Offs;
927 Hi = (unsigned char) (Offs >> 8);
932 AddCodeLine ("lda sp");
933 AddCodeLine ("ldx sp+1");
938 AddCodeLine ("lda sp+1");
940 AddCodeLine ("adc #$%02X", Hi);
942 AddCodeLine ("lda sp");
944 } else if (Hi == 0) {
946 if (IS_Get (&CodeSizeFactor) < 200) {
947 /* 8 bit offset with subroutine call */
948 AddCodeLine ("lda #$%02X", Lo);
949 AddCodeLine ("jsr leaa0sp");
951 /* 8 bit offset inlined */
952 unsigned L = GetLocalLabel ();
953 AddCodeLine ("lda sp");
954 AddCodeLine ("ldx sp+1");
956 AddCodeLine ("adc #$%02X", Lo);
957 AddCodeLine ("bcc %s", LocalLabelName (L));
961 } else if (IS_Get (&CodeSizeFactor) < 170) {
962 /* Full 16 bit offset with subroutine call */
963 AddCodeLine ("lda #$%02X", Lo);
964 AddCodeLine ("ldx #$%02X", Hi);
965 AddCodeLine ("jsr leaaxsp");
967 /* Full 16 bit offset inlined */
968 AddCodeLine ("lda sp");
970 AddCodeLine ("adc #$%02X", Lo);
972 AddCodeLine ("lda sp+1");
973 AddCodeLine ("adc #$%02X", Hi);
981 void g_leavariadic (int Offs)
982 /* Fetch the address of a parameter in a variadic function into the primary
986 unsigned ArgSizeOffs;
988 /* Calculate the offset relative to sp */
991 /* Get the offset of the parameter which is stored at sp+0 on function
992 * entry and check if this offset is reachable with a byte offset.
994 CHECK (StackPtr <= 0);
995 ArgSizeOffs = -StackPtr;
996 CheckLocalOffs (ArgSizeOffs);
998 /* Get the size of all parameters. */
999 ldyconst (ArgSizeOffs);
1000 AddCodeLine ("lda (sp),y");
1002 /* Add the value of the stackpointer */
1003 if (IS_Get (&CodeSizeFactor) > 250) {
1004 unsigned L = GetLocalLabel();
1005 AddCodeLine ("ldx sp+1");
1006 AddCodeLine ("clc");
1007 AddCodeLine ("adc sp");
1008 AddCodeLine ("bcc %s", LocalLabelName (L));
1009 AddCodeLine ("inx");
1012 AddCodeLine ("ldx #$00");
1013 AddCodeLine ("jsr leaaxsp");
1016 /* Add the offset to the primary */
1018 g_inc (CF_INT | CF_CONST, Offs);
1019 } else if (Offs < 0) {
1020 g_dec (CF_INT | CF_CONST, -Offs);
1026 /*****************************************************************************/
1027 /* Store into memory */
1028 /*****************************************************************************/
1032 void g_putstatic (unsigned flags, unsigned long label, long offs)
1033 /* Store the primary register into the specified static memory cell */
1035 /* Create the correct label name */
1036 const char* lbuf = GetLabelName (flags, label, offs);
1038 /* Check the size and generate the correct store operation */
1039 switch (flags & CF_TYPE) {
1042 AddCodeLine ("sta %s", lbuf);
1046 AddCodeLine ("sta %s", lbuf);
1047 AddCodeLine ("stx %s+1", lbuf);
1051 AddCodeLine ("sta %s", lbuf);
1052 AddCodeLine ("stx %s+1", lbuf);
1053 AddCodeLine ("ldy sreg");
1054 AddCodeLine ("sty %s+2", lbuf);
1055 AddCodeLine ("ldy sreg+1");
1056 AddCodeLine ("sty %s+3", lbuf);
1067 void g_putlocal (unsigned Flags, int Offs, long Val)
1068 /* Put data into local object. */
1071 CheckLocalOffs (Offs);
1072 switch (Flags & CF_TYPE) {
1075 if (Flags & CF_CONST) {
1076 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1079 AddCodeLine ("sta (sp),y");
1083 if (Flags & CF_CONST) {
1085 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1086 AddCodeLine ("sta (sp),y");
1087 if ((Flags & CF_NOKEEP) == 0) {
1088 /* Place high byte into X */
1089 AddCodeLine ("tax");
1091 if ((Val & 0xFF) == Offs+1) {
1092 /* The value we need is already in Y */
1093 AddCodeLine ("tya");
1094 AddCodeLine ("dey");
1096 AddCodeLine ("dey");
1097 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1099 AddCodeLine ("sta (sp),y");
1101 if ((Flags & CF_NOKEEP) == 0 || IS_Get (&CodeSizeFactor) < 160) {
1103 AddCodeLine ("jsr staxysp");
1106 AddCodeLine ("sta (sp),y");
1107 AddCodeLine ("iny");
1108 AddCodeLine ("txa");
1109 AddCodeLine ("sta (sp),y");
1115 if (Flags & CF_CONST) {
1116 g_getimmed (Flags, Val, 0);
1119 AddCodeLine ("jsr steaxysp");
1130 void g_putind (unsigned Flags, unsigned Offs)
1131 /* Store the specified object type in the primary register at the address
1132 * on the top of the stack
1135 /* We can handle offsets below $100 directly, larger offsets must be added
1136 * to the address. Since a/x is in use, best code is achieved by adding
1137 * just the high byte. Be sure to check if the low byte will overflow while
1140 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1142 /* Overflow - we need to add the low byte also */
1143 AddCodeLine ("ldy #$00");
1144 AddCodeLine ("clc");
1145 AddCodeLine ("pha");
1146 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1147 AddCodeLine ("adc (sp),y");
1148 AddCodeLine ("sta (sp),y");
1149 AddCodeLine ("iny");
1150 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1151 AddCodeLine ("adc (sp),y");
1152 AddCodeLine ("sta (sp),y");
1153 AddCodeLine ("pla");
1155 /* Complete address is on stack, new offset is zero */
1158 } else if ((Offs & 0xFF00) != 0) {
1160 /* We can just add the high byte */
1161 AddCodeLine ("ldy #$01");
1162 AddCodeLine ("clc");
1163 AddCodeLine ("pha");
1164 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1165 AddCodeLine ("adc (sp),y");
1166 AddCodeLine ("sta (sp),y");
1167 AddCodeLine ("pla");
1169 /* Offset is now just the low byte */
1173 /* Check the size and determine operation */
1174 switch (Flags & CF_TYPE) {
1178 AddCodeLine ("jsr staspidx");
1183 AddCodeLine ("jsr staxspidx");
1188 AddCodeLine ("jsr steaxspidx");
1196 /* Pop the argument which is always a pointer */
1202 /*****************************************************************************/
1203 /* type conversion and similiar stuff */
1204 /*****************************************************************************/
1208 void g_toslong (unsigned flags)
1209 /* Make sure, the value on TOS is a long. Convert if necessary */
1211 switch (flags & CF_TYPE) {
1215 if (flags & CF_UNSIGNED) {
1216 AddCodeLine ("jsr tosulong");
1218 AddCodeLine ("jsr toslong");
1233 void g_tosint (unsigned flags)
1234 /* Make sure, the value on TOS is an int. Convert if necessary */
1236 switch (flags & CF_TYPE) {
1243 AddCodeLine ("jsr tosint");
1254 void g_regint (unsigned Flags)
1255 /* Make sure, the value in the primary register an int. Convert if necessary */
1259 switch (Flags & CF_TYPE) {
1262 if (Flags & CF_FORCECHAR) {
1263 /* Conversion is from char */
1264 if (Flags & CF_UNSIGNED) {
1265 AddCodeLine ("ldx #$00");
1267 L = GetLocalLabel();
1268 AddCodeLine ("ldx #$00");
1269 AddCodeLine ("cmp #$80");
1270 AddCodeLine ("bcc %s", LocalLabelName (L));
1271 AddCodeLine ("dex");
1288 void g_reglong (unsigned Flags)
1289 /* Make sure, the value in the primary register a long. Convert if necessary */
1293 switch (Flags & CF_TYPE) {
1296 if (Flags & CF_FORCECHAR) {
1297 /* Conversion is from char */
1298 if (Flags & CF_UNSIGNED) {
1299 if (IS_Get (&CodeSizeFactor) >= 200) {
1300 AddCodeLine ("ldx #$00");
1301 AddCodeLine ("stx sreg");
1302 AddCodeLine ("stx sreg+1");
1304 AddCodeLine ("jsr aulong");
1307 if (IS_Get (&CodeSizeFactor) >= 366) {
1308 L = GetLocalLabel();
1309 AddCodeLine ("ldx #$00");
1310 AddCodeLine ("cmp #$80");
1311 AddCodeLine ("bcc %s", LocalLabelName (L));
1312 AddCodeLine ("dex");
1314 AddCodeLine ("stx sreg");
1315 AddCodeLine ("stx sreg+1");
1317 AddCodeLine ("jsr along");
1324 if (Flags & CF_UNSIGNED) {
1325 if (IS_Get (&CodeSizeFactor) >= 200) {
1326 AddCodeLine ("ldy #$00");
1327 AddCodeLine ("sty sreg");
1328 AddCodeLine ("sty sreg+1");
1330 AddCodeLine ("jsr axulong");
1333 AddCodeLine ("jsr axlong");
1347 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1348 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1349 * value, that corresponds to the value on TOS, rhs corresponds to the value
1350 * in (e)ax. The return value is the the flags value for the resulting type.
1353 unsigned ltype, rtype;
1356 /* Get the type spec from the flags */
1357 ltype = lhs & CF_TYPE;
1358 rtype = rhs & CF_TYPE;
1360 /* Check if a conversion is needed */
1361 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1362 /* We must promote the primary register to long */
1364 /* Get the new rhs type */
1365 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1367 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1368 /* We must promote the lhs to long */
1374 /* Get the new rhs type */
1375 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1379 /* Determine the result type for the operation:
1380 * - The result is const if both operands are const.
1381 * - The result is unsigned if one of the operands is unsigned.
1382 * - The result is long if one of the operands is long.
1383 * - Otherwise the result is int sized.
1385 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1386 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1387 if (rtype == CF_LONG || ltype == CF_LONG) {
1397 unsigned g_typecast (unsigned lhs, unsigned rhs)
1398 /* Cast the value in the primary register to the operand size that is flagged
1399 * by the lhs value. Return the result value.
1402 unsigned ltype, rtype;
1404 /* Get the type spec from the flags */
1405 ltype = lhs & CF_TYPE;
1406 rtype = rhs & CF_TYPE;
1408 /* Check if a conversion is needed */
1409 if ((rhs & CF_CONST) == 0) {
1410 if (ltype == CF_LONG && rtype != CF_LONG) {
1411 /* We must promote the primary register to long */
1413 } else if (ltype == CF_INT && rtype != CF_INT) {
1414 /* We must promote the primary register to int */
1419 /* Do not need any other action. If the left type is int, and the primary
1420 * register is long, it will be automagically truncated. If the right hand
1421 * side is const, it is not located in the primary register and handled by
1422 * the expression parser code.
1425 /* Result is const if the right hand side was const */
1426 lhs |= (rhs & CF_CONST);
1428 /* The resulting type is that of the left hand side (that's why you called
1436 void g_scale (unsigned flags, long val)
1437 /* Scale the value in the primary register by the given value. If val is positive,
1438 * scale up, is val is negative, scale down. This function is used to scale
1439 * the operands or results of pointer arithmetic by the size of the type, the
1440 * pointer points to.
1445 /* Value may not be zero */
1447 Internal ("Data type has no size");
1448 } else if (val > 0) {
1451 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1453 /* Factor is 2, 4, 8 and 16, use special function */
1454 switch (flags & CF_TYPE) {
1457 if (flags & CF_FORCECHAR) {
1459 AddCodeLine ("asl a");
1466 if (flags & CF_UNSIGNED) {
1467 AddCodeLine ("jsr shlax%d", p2);
1469 AddCodeLine ("jsr aslax%d", p2);
1474 if (flags & CF_UNSIGNED) {
1475 AddCodeLine ("jsr shleax%d", p2);
1477 AddCodeLine ("jsr asleax%d", p2);
1486 } else if (val != 1) {
1488 /* Use a multiplication instead */
1489 g_mul (flags | CF_CONST, val);
1497 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1499 /* Factor is 2, 4, 8 and 16 use special function */
1500 switch (flags & CF_TYPE) {
1503 if (flags & CF_FORCECHAR) {
1504 if (flags & CF_UNSIGNED) {
1506 AddCodeLine ("lsr a");
1509 } else if (p2 <= 2) {
1510 AddCodeLine ("cmp #$80");
1511 AddCodeLine ("ror a");
1518 if (flags & CF_UNSIGNED) {
1519 AddCodeLine ("jsr lsrax%d", p2);
1521 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 (IS_Get (&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 (IS_Get (&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 ("lda %s", lbuf);
1915 AddCodeLine ("sec");
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 if (flags & CF_CONST) {
1937 AddCodeLine ("lda %s", lbuf);
1938 AddCodeLine ("sec");
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 ("sec");
1957 AddCodeLine ("adc %s", lbuf);
1958 AddCodeLine ("sta %s", lbuf);
1959 AddCodeLine ("txa");
1960 AddCodeLine ("eor #$FF");
1961 AddCodeLine ("adc %s+1", lbuf);
1962 AddCodeLine ("sta %s+1", lbuf);
1963 AddCodeLine ("tax");
1964 AddCodeLine ("lda %s", lbuf);
1969 if (flags & CF_CONST) {
1971 AddCodeLine ("ldy #<(%s)", lbuf);
1972 AddCodeLine ("sty ptr1");
1973 AddCodeLine ("ldy #>(%s)", lbuf);
1974 AddCodeLine ("lda #$%02X", (unsigned char)val);
1975 AddCodeLine ("jsr lsubeqa");
1977 g_getstatic (flags, label, offs);
1979 g_putstatic (flags, label, offs);
1982 AddCodeLine ("ldy #<(%s)", lbuf);
1983 AddCodeLine ("sty ptr1");
1984 AddCodeLine ("ldy #>(%s)", lbuf);
1985 AddCodeLine ("jsr lsubeq");
1996 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1997 /* Emit -= for a local variable */
1999 /* Calculate the true offset, check it, load it into Y */
2001 CheckLocalOffs (offs);
2003 /* Check the size and determine operation */
2004 switch (flags & CF_TYPE) {
2007 if (flags & CF_FORCECHAR) {
2009 AddCodeLine ("ldx #$00");
2010 if (flags & CF_CONST) {
2011 AddCodeLine ("lda (sp),y");
2012 AddCodeLine ("sec");
2013 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2015 AddCodeLine ("eor #$FF");
2016 AddCodeLine ("sec");
2017 AddCodeLine ("adc (sp),y");
2019 AddCodeLine ("sta (sp),y");
2020 if ((flags & CF_UNSIGNED) == 0) {
2021 unsigned L = GetLocalLabel();
2022 AddCodeLine ("bpl %s", LocalLabelName (L));
2023 AddCodeLine ("dex");
2031 if (flags & CF_CONST) {
2032 g_getimmed (flags, val, 0);
2035 AddCodeLine ("jsr subeqysp");
2039 if (flags & CF_CONST) {
2040 g_getimmed (flags, val, 0);
2043 AddCodeLine ("jsr lsubeqysp");
2053 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2054 /* Emit -= for the location with address in ax */
2056 /* If the offset is too large for a byte register, add the high byte
2057 * of the offset to the primary. Beware: We need a special correction
2058 * if the offset in the low byte will overflow in the operation.
2060 offs = MakeByteOffs (flags, offs);
2062 /* Check the size and determine operation */
2063 switch (flags & CF_TYPE) {
2066 AddCodeLine ("sta ptr1");
2067 AddCodeLine ("stx ptr1+1");
2068 AddCodeLine ("ldy #$%02X", offs);
2069 AddCodeLine ("ldx #$00");
2070 AddCodeLine ("lda (ptr1),y");
2071 AddCodeLine ("sec");
2072 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2073 AddCodeLine ("sta (ptr1),y");
2077 if (IS_Get (&CodeSizeFactor) >= 200) {
2078 /* Lots of code, use only if size is not important */
2079 AddCodeLine ("sta ptr1");
2080 AddCodeLine ("stx ptr1+1");
2081 AddCodeLine ("ldy #$%02X", offs);
2082 AddCodeLine ("lda (ptr1),y");
2083 AddCodeLine ("sec");
2084 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2085 AddCodeLine ("sta (ptr1),y");
2086 AddCodeLine ("pha");
2087 AddCodeLine ("iny");
2088 AddCodeLine ("lda (ptr1),y");
2089 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2090 AddCodeLine ("sta (ptr1),y");
2091 AddCodeLine ("tax");
2092 AddCodeLine ("pla");
2098 AddCodeLine ("jsr pushax"); /* Push the address */
2099 push (CF_PTR); /* Correct the internal sp */
2100 g_getind (flags, offs); /* Fetch the value */
2101 g_dec (flags, val); /* Increment value in primary */
2102 g_putind (flags, offs); /* Store the value back */
2112 /*****************************************************************************/
2113 /* Add a variable address to the value in ax */
2114 /*****************************************************************************/
2118 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2119 /* Add the address of a local variable to ax */
2123 /* Add the offset */
2126 /* We cannot address more then 256 bytes of locals anyway */
2127 L = GetLocalLabel();
2128 CheckLocalOffs (offs);
2129 AddCodeLine ("clc");
2130 AddCodeLine ("adc #$%02X", offs & 0xFF);
2131 /* Do also skip the CLC insn below */
2132 AddCodeLine ("bcc %s", LocalLabelName (L));
2133 AddCodeLine ("inx");
2136 /* Add the current stackpointer value */
2137 AddCodeLine ("clc");
2139 /* Label was used above */
2142 AddCodeLine ("adc sp");
2143 AddCodeLine ("tay");
2144 AddCodeLine ("txa");
2145 AddCodeLine ("adc sp+1");
2146 AddCodeLine ("tax");
2147 AddCodeLine ("tya");
2152 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2153 /* Add the address of a static variable to ax */
2155 /* Create the correct label name */
2156 const char* lbuf = GetLabelName (flags, label, offs);
2158 /* Add the address to the current ax value */
2159 AddCodeLine ("clc");
2160 AddCodeLine ("adc #<(%s)", lbuf);
2161 AddCodeLine ("tay");
2162 AddCodeLine ("txa");
2163 AddCodeLine ("adc #>(%s)", lbuf);
2164 AddCodeLine ("tax");
2165 AddCodeLine ("tya");
2170 /*****************************************************************************/
2172 /*****************************************************************************/
2176 void g_save (unsigned flags)
2177 /* Copy primary register to hold register. */
2179 /* Check the size and determine operation */
2180 switch (flags & CF_TYPE) {
2183 if (flags & CF_FORCECHAR) {
2184 AddCodeLine ("pha");
2190 AddCodeLine ("sta regsave");
2191 AddCodeLine ("stx regsave+1");
2195 AddCodeLine ("jsr saveeax");
2205 void g_restore (unsigned flags)
2206 /* Copy hold register to primary. */
2208 /* Check the size and determine operation */
2209 switch (flags & CF_TYPE) {
2212 if (flags & CF_FORCECHAR) {
2213 AddCodeLine ("pla");
2219 AddCodeLine ("lda regsave");
2220 AddCodeLine ("ldx regsave+1");
2224 AddCodeLine ("jsr resteax");
2234 void g_cmp (unsigned flags, unsigned long val)
2235 /* Immidiate compare. The primary register will not be changed, Z flag
2241 /* Check the size and determine operation */
2242 switch (flags & CF_TYPE) {
2245 if (flags & CF_FORCECHAR) {
2246 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2252 L = GetLocalLabel();
2253 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2254 AddCodeLine ("bne %s", LocalLabelName (L));
2255 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2260 Internal ("g_cmp: Long compares not implemented");
2270 static void oper (unsigned Flags, unsigned long Val, const char** Subs)
2271 /* Encode a binary operation. subs is a pointer to four strings:
2272 * 0 --> Operate on ints
2273 * 1 --> Operate on unsigneds
2274 * 2 --> Operate on longs
2275 * 3 --> Operate on unsigned longs
2278 /* Determine the offset into the array */
2279 if (Flags & CF_UNSIGNED) {
2282 if ((Flags & CF_TYPE) == CF_LONG) {
2286 /* Load the value if it is not already in the primary */
2287 if (Flags & CF_CONST) {
2289 g_getimmed (Flags, Val, 0);
2292 /* Output the operation */
2293 AddCodeLine ("jsr %s", *Subs);
2295 /* The operation will pop it's argument */
2301 void g_test (unsigned flags)
2302 /* Test the value in the primary and set the condition codes */
2304 switch (flags & CF_TYPE) {
2307 if (flags & CF_FORCECHAR) {
2308 AddCodeLine ("tax");
2314 AddCodeLine ("stx tmp1");
2315 AddCodeLine ("ora tmp1");
2319 if (flags & CF_UNSIGNED) {
2320 AddCodeLine ("jsr utsteax");
2322 AddCodeLine ("jsr tsteax");
2334 void g_push (unsigned flags, unsigned long val)
2335 /* Push the primary register or a constant value onto the stack */
2337 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2339 /* We have a constant 8 or 16 bit value */
2340 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2342 /* Handle as 8 bit value */
2343 AddCodeLine ("lda #$%02X", (unsigned char) val);
2344 AddCodeLine ("jsr pusha");
2348 /* Handle as 16 bit value */
2349 g_getimmed (flags, val, 0);
2350 AddCodeLine ("jsr pushax");
2355 /* Value is not 16 bit or not constant */
2356 if (flags & CF_CONST) {
2357 /* Constant 32 bit value, load into eax */
2358 g_getimmed (flags, val, 0);
2361 /* Push the primary register */
2362 switch (flags & CF_TYPE) {
2365 if (flags & CF_FORCECHAR) {
2366 /* Handle as char */
2367 AddCodeLine ("jsr pusha");
2372 AddCodeLine ("jsr pushax");
2376 AddCodeLine ("jsr pusheax");
2386 /* Adjust the stack offset */
2392 void g_swap (unsigned flags)
2393 /* Swap the primary register and the top of the stack. flags give the type
2394 * of *both* values (must have same size).
2397 switch (flags & CF_TYPE) {
2401 AddCodeLine ("jsr swapstk");
2405 AddCodeLine ("jsr swapestk");
2416 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2417 /* Call the specified subroutine name */
2419 if ((Flags & CF_FIXARGC) == 0) {
2420 /* Pass the argument count */
2423 AddCodeLine ("jsr _%s", Label);
2424 StackPtr += ArgSize; /* callee pops args */
2429 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2430 /* Call subroutine indirect */
2432 if ((Flags & CF_LOCAL) == 0) {
2433 /* Address is in a/x */
2434 if ((Flags & CF_FIXARGC) == 0) {
2435 /* Pass arg count */
2438 AddCodeLine ("jsr callax");
2440 /* The address is on stack, offset is on Val */
2442 CheckLocalOffs (Offs);
2443 AddCodeLine ("pha");
2444 AddCodeLine ("ldy #$%02X", Offs);
2445 AddCodeLine ("lda (sp),y");
2446 AddCodeLine ("sta jmpvec+1");
2447 AddCodeLine ("iny");
2448 AddCodeLine ("lda (sp),y");
2449 AddCodeLine ("sta jmpvec+2");
2450 AddCodeLine ("pla");
2451 AddCodeLine ("jsr jmpvec");
2454 /* Callee pops args */
2455 StackPtr += ArgSize;
2460 void g_jump (unsigned Label)
2461 /* Jump to specified internal label number */
2463 AddCodeLine ("jmp %s", LocalLabelName (Label));
2468 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2469 /* Jump to label if zero flag clear */
2471 AddCodeLine ("jne %s", LocalLabelName (label));
2476 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2477 /* Jump to label if zero flag set */
2479 AddCodeLine ("jeq %s", LocalLabelName (label));
2484 static void mod_internal (int k, char* verb1, char* verb2)
2487 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2491 AddCodeLine ("jsr %ssp", verb2);
2497 void g_space (int space)
2498 /* Create or drop space on the stack */
2501 mod_internal (-space, "inc", "addy");
2502 } else if (space > 0) {
2503 mod_internal (space, "dec", "suby");
2509 void g_cstackcheck (void)
2510 /* Check for a C stack overflow */
2512 AddCodeLine ("jsr cstkchk");
2517 void g_stackcheck (void)
2518 /* Check for a stack overflow */
2520 AddCodeLine ("jsr stkchk");
2525 void g_add (unsigned flags, unsigned long val)
2526 /* Primary = TOS + Primary */
2528 static const char* ops[12] = {
2529 "tosaddax", "tosaddax", "tosaddeax", "tosaddeax"
2532 if (flags & CF_CONST) {
2533 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2534 g_push (flags & ~CF_CONST, 0);
2536 oper (flags, val, ops);
2541 void g_sub (unsigned flags, unsigned long val)
2542 /* Primary = TOS - Primary */
2544 static const char* ops[12] = {
2545 "tossubax", "tossubax", "tossubeax", "tossubeax",
2548 if (flags & CF_CONST) {
2549 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2550 g_push (flags & ~CF_CONST, 0);
2552 oper (flags, val, ops);
2557 void g_rsub (unsigned flags, unsigned long val)
2558 /* Primary = Primary - TOS */
2560 static const char* ops[12] = {
2561 "tosrsubax", "tosrsubax", "tosrsubeax", "tosrsubeax",
2563 oper (flags, val, ops);
2568 void g_mul (unsigned flags, unsigned long val)
2569 /* Primary = TOS * Primary */
2571 static const char* ops[12] = {
2572 "tosmulax", "tosumulax", "tosmuleax", "tosumuleax",
2577 /* Do strength reduction if the value is constant and a power of two */
2578 if (flags & CF_CONST && (p2 = PowerOf2 (val)) >= 0) {
2579 /* Generate a shift instead */
2584 /* If the right hand side is const, the lhs is not on stack but still
2585 * in the primary register.
2587 if (flags & CF_CONST) {
2589 switch (flags & CF_TYPE) {
2592 if (flags & CF_FORCECHAR) {
2593 /* Handle some special cases */
2597 AddCodeLine ("sta tmp1");
2598 AddCodeLine ("asl a");
2599 AddCodeLine ("clc");
2600 AddCodeLine ("adc tmp1");
2604 AddCodeLine ("sta tmp1");
2605 AddCodeLine ("asl a");
2606 AddCodeLine ("asl a");
2607 AddCodeLine ("clc");
2608 AddCodeLine ("adc tmp1");
2612 AddCodeLine ("sta tmp1");
2613 AddCodeLine ("asl a");
2614 AddCodeLine ("clc");
2615 AddCodeLine ("adc tmp1");
2616 AddCodeLine ("asl a");
2620 AddCodeLine ("sta tmp1");
2621 AddCodeLine ("asl a");
2622 AddCodeLine ("asl a");
2623 AddCodeLine ("clc");
2624 AddCodeLine ("adc tmp1");
2625 AddCodeLine ("asl a");
2634 AddCodeLine ("jsr mulax3");
2637 AddCodeLine ("jsr mulax5");
2640 AddCodeLine ("jsr mulax6");
2643 AddCodeLine ("jsr mulax7");
2646 AddCodeLine ("jsr mulax9");
2649 AddCodeLine ("jsr mulax10");
2661 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2662 * into the normal, non-optimized stuff.
2664 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2665 g_push (flags & ~CF_CONST, 0);
2669 /* Use long way over the stack */
2670 oper (flags, val, ops);
2675 void g_div (unsigned flags, unsigned long val)
2676 /* Primary = TOS / Primary */
2678 static const char* ops[12] = {
2679 "tosdivax", "tosudivax", "tosdiveax", "tosudiveax",
2682 /* Do strength reduction if the value is constant and a power of two */
2684 if ((flags & CF_CONST) && (p2 = PowerOf2 (val)) >= 0) {
2685 /* Generate a shift instead */
2688 /* Generate a division */
2689 if (flags & CF_CONST) {
2690 /* lhs is not on stack */
2691 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2692 g_push (flags & ~CF_CONST, 0);
2694 oper (flags, val, ops);
2700 void g_mod (unsigned flags, unsigned long val)
2701 /* Primary = TOS % Primary */
2703 static const char* ops[12] = {
2704 "tosmodax", "tosumodax", "tosmodeax", "tosumodeax",
2708 /* Check if we can do some cost reduction */
2709 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = PowerOf2 (val)) >= 0) {
2710 /* We can do that with an AND operation */
2711 g_and (flags, val - 1);
2713 /* Do it the hard way... */
2714 if (flags & CF_CONST) {
2715 /* lhs is not on stack */
2716 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2717 g_push (flags & ~CF_CONST, 0);
2719 oper (flags, val, ops);
2725 void g_or (unsigned flags, unsigned long val)
2726 /* Primary = TOS | Primary */
2728 static const char* ops[12] = {
2729 "tosorax", "tosorax", "tosoreax", "tosoreax",
2732 /* If the right hand side is const, the lhs is not on stack but still
2733 * in the primary register.
2735 if (flags & CF_CONST) {
2737 switch (flags & CF_TYPE) {
2740 if (flags & CF_FORCECHAR) {
2741 if ((val & 0xFF) != 0) {
2742 AddCodeLine ("ora #$%02X", (unsigned char)val);
2750 if ((val & 0xFF) != 0) {
2751 AddCodeLine ("ora #$%02X", (unsigned char)val);
2753 } else if ((val & 0xFF00) == 0xFF00) {
2754 if ((val & 0xFF) != 0) {
2755 AddCodeLine ("ora #$%02X", (unsigned char)val);
2758 } else if (val != 0) {
2759 AddCodeLine ("ora #$%02X", (unsigned char)val);
2760 AddCodeLine ("pha");
2761 AddCodeLine ("txa");
2762 AddCodeLine ("ora #$%02X", (unsigned char)(val >> 8));
2763 AddCodeLine ("tax");
2764 AddCodeLine ("pla");
2770 if ((val & 0xFF) != 0) {
2771 AddCodeLine ("ora #$%02X", (unsigned char)val);
2781 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2782 * into the normal, non-optimized stuff. Note: The standard stuff will
2783 * always work with ints.
2785 flags &= ~CF_FORCECHAR;
2786 g_push (flags & ~CF_CONST, 0);
2789 /* Use long way over the stack */
2790 oper (flags, val, ops);
2795 void g_xor (unsigned flags, unsigned long val)
2796 /* Primary = TOS ^ Primary */
2798 static const char* ops[12] = {
2799 "tosxorax", "tosxorax", "tosxoreax", "tosxoreax",
2803 /* If the right hand side is const, the lhs is not on stack but still
2804 * in the primary register.
2806 if (flags & CF_CONST) {
2808 switch (flags & CF_TYPE) {
2811 if (flags & CF_FORCECHAR) {
2812 if ((val & 0xFF) != 0) {
2813 AddCodeLine ("eor #$%02X", (unsigned char)val);
2822 AddCodeLine ("eor #$%02X", (unsigned char)val);
2824 } else if (val != 0) {
2825 if ((val & 0xFF) != 0) {
2826 AddCodeLine ("eor #$%02X", (unsigned char)val);
2828 AddCodeLine ("pha");
2829 AddCodeLine ("txa");
2830 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2831 AddCodeLine ("tax");
2832 AddCodeLine ("pla");
2839 AddCodeLine ("eor #$%02X", (unsigned char)val);
2849 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2850 * into the normal, non-optimized stuff. Note: The standard stuff will
2851 * always work with ints.
2853 flags &= ~CF_FORCECHAR;
2854 g_push (flags & ~CF_CONST, 0);
2857 /* Use long way over the stack */
2858 oper (flags, val, ops);
2863 void g_and (unsigned Flags, unsigned long Val)
2864 /* Primary = TOS & Primary */
2866 static const char* ops[12] = {
2867 "tosandax", "tosandax", "tosandeax", "tosandeax",
2870 /* If the right hand side is const, the lhs is not on stack but still
2871 * in the primary register.
2873 if (Flags & CF_CONST) {
2875 switch (Flags & CF_TYPE) {
2878 if (Flags & CF_FORCECHAR) {
2879 if ((Val & 0xFF) == 0x00) {
2880 AddCodeLine ("lda #$00");
2881 } else if ((Val & 0xFF) != 0xFF) {
2882 AddCodeLine ("and #$%02X", (unsigned char)Val);
2888 if ((Val & 0xFFFF) != 0xFFFF) {
2892 AddCodeLine ("lda #$00");
2893 } else if (Val != 0xFF) {
2894 AddCodeLine ("and #$%02X", (unsigned char)Val);
2896 } else if ((Val & 0xFFFF) == 0xFF00) {
2897 AddCodeLine ("lda #$00");
2898 } else if ((Val & 0xFF00) == 0xFF00) {
2899 AddCodeLine ("and #$%02X", (unsigned char)Val);
2900 } else if ((Val & 0x00FF) == 0x0000) {
2901 AddCodeLine ("txa");
2902 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2903 AddCodeLine ("tax");
2904 AddCodeLine ("lda #$00");
2906 AddCodeLine ("tay");
2907 AddCodeLine ("txa");
2908 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2909 AddCodeLine ("tax");
2910 AddCodeLine ("tya");
2911 if ((Val & 0x00FF) == 0x0000) {
2912 AddCodeLine ("lda #$00");
2913 } else if ((Val & 0x00FF) != 0x00FF) {
2914 AddCodeLine ("and #$%02X", (unsigned char)Val);
2922 AddCodeLine ("ldx #$00");
2923 AddCodeLine ("stx sreg+1");
2924 AddCodeLine ("stx sreg");
2925 if ((Val & 0xFF) != 0xFF) {
2926 AddCodeLine ("and #$%02X", (unsigned char)Val);
2929 } else if (Val == 0xFF00) {
2930 AddCodeLine ("lda #$00");
2931 AddCodeLine ("sta sreg+1");
2932 AddCodeLine ("sta sreg");
2941 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2942 * into the normal, non-optimized stuff. Note: The standard stuff will
2943 * always work with ints.
2945 Flags &= ~CF_FORCECHAR;
2946 g_push (Flags & ~CF_CONST, 0);
2949 /* Use long way over the stack */
2950 oper (Flags, Val, ops);
2955 void g_asr (unsigned flags, unsigned long val)
2956 /* Primary = TOS >> Primary */
2958 static const char* ops[12] = {
2959 "tosasrax", "tosshrax", "tosasreax", "tosshreax",
2962 /* If the right hand side is const, the lhs is not on stack but still
2963 * in the primary register.
2965 if (flags & CF_CONST) {
2967 switch (flags & CF_TYPE) {
2973 if (flags & CF_UNSIGNED) {
2974 AddCodeLine ("txa");
2977 unsigned L = GetLocalLabel();
2978 AddCodeLine ("cpx #$80"); /* Sign bit into carry */
2979 AddCodeLine ("txa");
2980 AddCodeLine ("ldx #$00");
2981 AddCodeLine ("bcc %s", LocalLabelName (L));
2982 AddCodeLine ("dex"); /* Make $FF */
2988 if (flags & CF_UNSIGNED) {
2989 AddCodeLine ("jsr shrax4");
2991 AddCodeLine ("jsr asrax4");
2996 if (flags & CF_UNSIGNED) {
2997 AddCodeLine ("jsr shrax%ld", val);
2999 AddCodeLine ("jsr asrax%ld", val);
3007 AddCodeLine ("ldx #$00");
3008 AddCodeLine ("lda sreg+1");
3009 if ((flags & CF_UNSIGNED) == 0) {
3010 unsigned L = GetLocalLabel();
3011 AddCodeLine ("bpl %s", LocalLabelName (L));
3012 AddCodeLine ("dex");
3015 AddCodeLine ("stx sreg");
3016 AddCodeLine ("stx sreg+1");
3020 AddCodeLine ("ldy #$00");
3021 AddCodeLine ("ldx sreg+1");
3022 if ((flags & CF_UNSIGNED) == 0) {
3023 unsigned L = GetLocalLabel();
3024 AddCodeLine ("bpl %s", LocalLabelName (L));
3025 AddCodeLine ("dey");
3028 AddCodeLine ("lda sreg");
3029 AddCodeLine ("sty sreg+1");
3030 AddCodeLine ("sty sreg");
3034 AddCodeLine ("txa");
3035 AddCodeLine ("ldx sreg");
3036 AddCodeLine ("ldy sreg+1");
3037 AddCodeLine ("sty sreg");
3038 if ((flags & CF_UNSIGNED) == 0) {
3039 unsigned L = GetLocalLabel();
3040 AddCodeLine ("cpy #$80");
3041 AddCodeLine ("ldy #$00");
3042 AddCodeLine ("bcc %s", LocalLabelName (L));
3043 AddCodeLine ("dey");
3046 AddCodeLine ("ldy #$00");
3048 AddCodeLine ("sty sreg+1");
3052 if (flags & CF_UNSIGNED) {
3053 AddCodeLine ("jsr shreax4");
3055 AddCodeLine ("jsr asreax4");
3060 if (flags & CF_UNSIGNED) {
3061 AddCodeLine ("jsr shreax%ld", val);
3063 AddCodeLine ("jsr asreax%ld", val);
3072 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3073 * into the normal, non-optimized stuff. Note: The standard stuff will
3074 * always work with ints.
3076 flags &= ~CF_FORCECHAR;
3077 g_push (flags & ~CF_CONST, 0);
3080 /* Use long way over the stack */
3081 oper (flags, val, ops);
3086 void g_asl (unsigned flags, unsigned long val)
3087 /* Primary = TOS << Primary */
3089 static const char* ops[12] = {
3090 "tosaslax", "tosshlax", "tosasleax", "tosshleax",
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) {
3105 AddCodeLine ("tax");
3106 AddCodeLine ("lda #$00");
3110 if (flags & CF_UNSIGNED) {
3111 AddCodeLine ("jsr shlax4");
3113 AddCodeLine ("jsr aslax4");
3118 if (flags & CF_UNSIGNED) {
3119 AddCodeLine ("jsr shlax%ld", val);
3121 AddCodeLine ("jsr aslax%ld", val);
3129 AddCodeLine ("sta sreg+1");
3130 AddCodeLine ("lda #$00");
3131 AddCodeLine ("tax");
3132 AddCodeLine ("sta sreg");
3136 AddCodeLine ("stx sreg+1");
3137 AddCodeLine ("sta sreg");
3138 AddCodeLine ("lda #$00");
3139 AddCodeLine ("tax");
3143 AddCodeLine ("ldy sreg");
3144 AddCodeLine ("sty sreg+1");
3145 AddCodeLine ("stx sreg");
3146 AddCodeLine ("tax");
3147 AddCodeLine ("lda #$00");
3151 if (flags & CF_UNSIGNED) {
3152 AddCodeLine ("jsr shleax4");
3154 AddCodeLine ("jsr asleax4");
3159 if (flags & CF_UNSIGNED) {
3160 AddCodeLine ("jsr shleax%ld", val);
3162 AddCodeLine ("jsr asleax%ld", val);
3171 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3172 * into the normal, non-optimized stuff. Note: The standard stuff will
3173 * always work with ints.
3175 flags &= ~CF_FORCECHAR;
3176 g_push (flags & ~CF_CONST, 0);
3179 /* Use long way over the stack */
3180 oper (flags, val, ops);
3185 void g_neg (unsigned Flags)
3186 /* Primary = -Primary */
3188 switch (Flags & CF_TYPE) {
3191 if (Flags & CF_FORCECHAR) {
3192 AddCodeLine ("eor #$FF");
3193 AddCodeLine ("clc");
3194 AddCodeLine ("adc #$01");
3200 AddCodeLine ("jsr negax");
3204 AddCodeLine ("jsr negeax");
3214 void g_bneg (unsigned flags)
3215 /* Primary = !Primary */
3217 switch (flags & CF_TYPE) {
3220 AddCodeLine ("jsr bnega");
3224 AddCodeLine ("jsr bnegax");
3228 AddCodeLine ("jsr bnegeax");
3238 void g_com (unsigned Flags)
3239 /* Primary = ~Primary */
3241 switch (Flags & CF_TYPE) {
3244 if (Flags & CF_FORCECHAR) {
3245 AddCodeLine ("eor #$FF");
3251 AddCodeLine ("jsr complax");
3255 AddCodeLine ("jsr compleax");
3265 void g_inc (unsigned flags, unsigned long val)
3266 /* Increment the primary register by a given number */
3268 /* Don't inc by zero */
3273 /* Generate code for the supported types */
3275 switch (flags & CF_TYPE) {
3278 if (flags & CF_FORCECHAR) {
3279 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3281 AddCodeLine ("ina");
3284 AddCodeLine ("clc");
3285 AddCodeLine ("adc #$%02X", (unsigned char)val);
3292 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) {
3293 unsigned L = GetLocalLabel();
3294 AddCodeLine ("ina");
3295 AddCodeLine ("bne %s", LocalLabelName (L));
3296 AddCodeLine ("inx");
3298 } else if (IS_Get (&CodeSizeFactor) < 200) {
3301 AddCodeLine ("jsr incax%lu", val);
3302 } else if (val <= 255) {
3303 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3304 AddCodeLine ("jsr incaxy");
3306 g_add (flags | CF_CONST, val);
3309 /* Inline the code */
3311 if ((val & 0xFF) != 0) {
3312 unsigned L = GetLocalLabel();
3313 AddCodeLine ("clc");
3314 AddCodeLine ("adc #$%02X", (unsigned char) val);
3315 AddCodeLine ("bcc %s", LocalLabelName (L));
3316 AddCodeLine ("inx");
3320 AddCodeLine ("inx");
3323 AddCodeLine ("inx");
3326 AddCodeLine ("inx");
3328 } else if ((val & 0xFF) != 0) {
3329 AddCodeLine ("clc");
3330 AddCodeLine ("adc #$%02X", (unsigned char) val);
3331 AddCodeLine ("pha");
3332 AddCodeLine ("txa");
3333 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3334 AddCodeLine ("tax");
3335 AddCodeLine ("pla");
3337 AddCodeLine ("pha");
3338 AddCodeLine ("txa");
3339 AddCodeLine ("clc");
3340 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3341 AddCodeLine ("tax");
3342 AddCodeLine ("pla");
3349 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3350 AddCodeLine ("jsr inceaxy");
3352 g_add (flags | CF_CONST, val);
3364 void g_dec (unsigned flags, unsigned long val)
3365 /* Decrement the primary register by a given number */
3367 /* Don't dec by zero */
3372 /* Generate code for the supported types */
3374 switch (flags & CF_TYPE) {
3377 if (flags & CF_FORCECHAR) {
3378 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3380 AddCodeLine ("dea");
3383 AddCodeLine ("sec");
3384 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3391 if (IS_Get (&CodeSizeFactor) < 200) {
3392 /* Use subroutines */
3394 AddCodeLine ("jsr decax%d", (int) val);
3395 } else if (val <= 255) {
3396 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3397 AddCodeLine ("jsr decaxy");
3399 g_sub (flags | CF_CONST, val);
3402 /* Inline the code */
3404 if ((val & 0xFF) != 0) {
3405 unsigned L = GetLocalLabel();
3406 AddCodeLine ("sec");
3407 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3408 AddCodeLine ("bcs %s", LocalLabelName (L));
3409 AddCodeLine ("dex");
3413 AddCodeLine ("dex");
3416 AddCodeLine ("dex");
3419 if ((val & 0xFF) != 0) {
3420 AddCodeLine ("sec");
3421 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3422 AddCodeLine ("pha");
3423 AddCodeLine ("txa");
3424 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3425 AddCodeLine ("tax");
3426 AddCodeLine ("pla");
3428 AddCodeLine ("pha");
3429 AddCodeLine ("txa");
3430 AddCodeLine ("sec");
3431 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3432 AddCodeLine ("tax");
3433 AddCodeLine ("pla");
3441 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3442 AddCodeLine ("jsr deceaxy");
3444 g_sub (flags | CF_CONST, val);
3457 * Following are the conditional operators. They compare the TOS against
3458 * the primary and put a literal 1 in the primary if the condition is
3459 * true, otherwise they clear the primary register
3464 void g_eq (unsigned flags, unsigned long val)
3465 /* Test for equal */
3467 static const char* ops[12] = {
3468 "toseqax", "toseqax", "toseqeax", "toseqeax",
3473 /* If the right hand side is const, the lhs is not on stack but still
3474 * in the primary register.
3476 if (flags & CF_CONST) {
3478 switch (flags & CF_TYPE) {
3481 if (flags & CF_FORCECHAR) {
3482 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3483 AddCodeLine ("jsr booleq");
3489 L = GetLocalLabel();
3490 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3491 AddCodeLine ("bne %s", LocalLabelName (L));
3492 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3494 AddCodeLine ("jsr booleq");
3504 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3505 * into the normal, non-optimized stuff. Note: The standard stuff will
3506 * always work with ints.
3508 flags &= ~CF_FORCECHAR;
3509 g_push (flags & ~CF_CONST, 0);
3512 /* Use long way over the stack */
3513 oper (flags, val, ops);
3518 void g_ne (unsigned flags, unsigned long val)
3519 /* Test for not equal */
3521 static const char* ops[12] = {
3522 "tosneax", "tosneax", "tosneeax", "tosneeax",
3527 /* If the right hand side is const, the lhs is not on stack but still
3528 * in the primary register.
3530 if (flags & CF_CONST) {
3532 switch (flags & CF_TYPE) {
3535 if (flags & CF_FORCECHAR) {
3536 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3537 AddCodeLine ("jsr boolne");
3543 L = GetLocalLabel();
3544 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3545 AddCodeLine ("bne %s", LocalLabelName (L));
3546 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3548 AddCodeLine ("jsr boolne");
3558 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3559 * into the normal, non-optimized stuff. Note: The standard stuff will
3560 * always work with ints.
3562 flags &= ~CF_FORCECHAR;
3563 g_push (flags & ~CF_CONST, 0);
3566 /* Use long way over the stack */
3567 oper (flags, val, ops);
3572 void g_lt (unsigned flags, unsigned long val)
3573 /* Test for less than */
3575 static const char* ops[12] = {
3576 "tosltax", "tosultax", "toslteax", "tosulteax",
3581 /* If the right hand side is const, the lhs is not on stack but still
3582 * in the primary register.
3584 if (flags & CF_CONST) {
3586 /* Because the handling of the overflow flag is too complex for
3587 * inlining, we can handle only unsigned compares, and signed
3588 * compares against zero here.
3590 if (flags & CF_UNSIGNED) {
3592 /* Give a warning in some special cases */
3594 Warning ("Condition is never true");
3595 AddCodeLine ("jsr return0");
3599 /* Look at the type */
3600 switch (flags & CF_TYPE) {
3603 if (flags & CF_FORCECHAR) {
3604 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3605 AddCodeLine ("jsr boolult");
3611 /* If the low byte is zero, we must only test the high byte */
3612 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3613 if ((val & 0xFF) != 0) {
3614 unsigned L = GetLocalLabel();
3615 AddCodeLine ("bne %s", LocalLabelName (L));
3616 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3619 AddCodeLine ("jsr boolult");
3623 /* Do a subtraction */
3624 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3625 AddCodeLine ("txa");
3626 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3627 AddCodeLine ("lda sreg");
3628 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3629 AddCodeLine ("lda sreg+1");
3630 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3631 AddCodeLine ("jsr boolult");
3638 } else if (val == 0) {
3640 /* A signed compare against zero must only look at the sign bit */
3641 switch (flags & CF_TYPE) {
3644 if (flags & CF_FORCECHAR) {
3645 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3646 AddCodeLine ("lda #$00");
3647 AddCodeLine ("ldx #$00");
3648 AddCodeLine ("rol a");
3654 /* Just check the high byte */
3655 AddCodeLine ("cpx #$80"); /* Bit 7 -> carry */
3656 AddCodeLine ("lda #$00");
3657 AddCodeLine ("ldx #$00");
3658 AddCodeLine ("rol a");
3662 /* Just check the high byte */
3663 AddCodeLine ("lda sreg+1");
3664 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3665 AddCodeLine ("lda #$00");
3666 AddCodeLine ("ldx #$00");
3667 AddCodeLine ("rol a");
3676 /* Signed compare against a constant != zero */
3677 switch (flags & CF_TYPE) {
3680 if (flags & CF_FORCECHAR) {
3681 Label = GetLocalLabel ();
3682 AddCodeLine ("sec");
3683 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3684 AddCodeLine ("bvc %s", LocalLabelName (Label));
3685 AddCodeLine ("eor #$80");
3686 g_defcodelabel (Label);
3687 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3688 AddCodeLine ("lda #$00");
3689 AddCodeLine ("ldx #$00");
3690 AddCodeLine ("rol a");
3696 /* Do a subtraction */
3697 Label = GetLocalLabel ();
3698 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3699 AddCodeLine ("txa");
3700 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3701 AddCodeLine ("bvc %s", LocalLabelName (Label));
3702 AddCodeLine ("eor #$80");
3703 g_defcodelabel (Label);
3704 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3705 AddCodeLine ("lda #$00");
3706 AddCodeLine ("ldx #$00");
3707 AddCodeLine ("rol a");
3711 /* This one is too costly */
3720 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3721 * into the normal, non-optimized stuff. Note: The standard stuff will
3722 * always work with ints.
3724 flags &= ~CF_FORCECHAR;
3725 g_push (flags & ~CF_CONST, 0);
3728 /* Use long way over the stack */
3729 oper (flags, val, ops);
3734 void g_le (unsigned flags, unsigned long val)
3735 /* Test for less than or equal to */
3737 static const char* ops[12] = {
3738 "tosleax", "tosuleax", "tosleeax", "tosuleeax",
3742 /* If the right hand side is const, the lhs is not on stack but still
3743 * in the primary register.
3745 if (flags & CF_CONST) {
3747 /* Look at the type */
3748 switch (flags & CF_TYPE) {
3751 if (flags & CF_FORCECHAR) {
3752 if (flags & CF_UNSIGNED) {
3753 /* Unsigned compare */
3755 /* Use < instead of <= because the former gives
3756 * better code on the 6502 than the latter.
3758 g_lt (flags, val+1);
3761 Warning ("Condition is always true");
3762 AddCodeLine ("jsr return1");
3765 /* Signed compare */
3766 if ((long) val < 0x7F) {
3767 /* Use < instead of <= because the former gives
3768 * better code on the 6502 than the latter.
3770 g_lt (flags, val+1);
3773 Warning ("Condition is always true");
3774 AddCodeLine ("jsr return1");
3782 if (flags & CF_UNSIGNED) {
3783 /* Unsigned compare */
3785 /* Use < instead of <= because the former gives
3786 * better code on the 6502 than the latter.
3788 g_lt (flags, val+1);
3791 Warning ("Condition is always true");
3792 AddCodeLine ("jsr return1");
3795 /* Signed compare */
3796 if ((long) val < 0x7FFF) {
3797 g_lt (flags, val+1);
3800 Warning ("Condition is always true");
3801 AddCodeLine ("jsr return1");
3807 if (flags & CF_UNSIGNED) {
3808 /* Unsigned compare */
3809 if (val < 0xFFFFFFFF) {
3810 /* Use < instead of <= because the former gives
3811 * better code on the 6502 than the latter.
3813 g_lt (flags, val+1);
3816 Warning ("Condition is always true");
3817 AddCodeLine ("jsr return1");
3820 /* Signed compare */
3821 if ((long) val < 0x7FFFFFFF) {
3822 g_lt (flags, val+1);
3825 Warning ("Condition is always true");
3826 AddCodeLine ("jsr return1");
3835 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3836 * into the normal, non-optimized stuff. Note: The standard stuff will
3837 * always work with ints.
3839 flags &= ~CF_FORCECHAR;
3840 g_push (flags & ~CF_CONST, 0);
3843 /* Use long way over the stack */
3844 oper (flags, val, ops);
3849 void g_gt (unsigned flags, unsigned long val)
3850 /* Test for greater than */
3852 static const char* ops[12] = {
3853 "tosgtax", "tosugtax", "tosgteax", "tosugteax",
3857 /* If the right hand side is const, the lhs is not on stack but still
3858 * in the primary register.
3860 if (flags & CF_CONST) {
3862 /* Look at the type */
3863 switch (flags & CF_TYPE) {
3866 if (flags & CF_FORCECHAR) {
3867 if (flags & CF_UNSIGNED) {
3869 /* If we have a compare > 0, we will replace it by
3870 * != 0 here, since both are identical but the
3871 * latter is easier to optimize.
3874 } else if (val < 0xFF) {
3875 /* Use >= instead of > because the former gives
3876 * better code on the 6502 than the latter.
3878 g_ge (flags, val+1);
3881 Warning ("Condition is never true");
3882 AddCodeLine ("jsr return0");
3885 if ((long) val < 0x7F) {
3886 /* Use >= instead of > because the former gives
3887 * better code on the 6502 than the latter.
3889 g_ge (flags, val+1);
3892 Warning ("Condition is never true");
3893 AddCodeLine ("jsr return0");
3901 if (flags & CF_UNSIGNED) {
3902 /* Unsigned compare */
3904 /* If we have a compare > 0, we will replace it by
3905 * != 0 here, since both are identical but the latter
3906 * is easier to optimize.
3909 } else if (val < 0xFFFF) {
3910 /* Use >= instead of > because the former gives better
3911 * code on the 6502 than the latter.
3913 g_ge (flags, val+1);
3916 Warning ("Condition is never true");
3917 AddCodeLine ("jsr return0");
3920 /* Signed compare */
3921 if ((long) val < 0x7FFF) {
3922 g_ge (flags, val+1);
3925 Warning ("Condition is never true");
3926 AddCodeLine ("jsr return0");
3932 if (flags & CF_UNSIGNED) {
3933 /* Unsigned compare */
3935 /* If we have a compare > 0, we will replace it by
3936 * != 0 here, since both are identical but the latter
3937 * is easier to optimize.
3940 } else if (val < 0xFFFFFFFF) {
3941 /* Use >= instead of > because the former gives better
3942 * code on the 6502 than the latter.
3944 g_ge (flags, val+1);
3947 Warning ("Condition is never true");
3948 AddCodeLine ("jsr return0");
3951 /* Signed compare */
3952 if ((long) val < 0x7FFFFFFF) {
3953 g_ge (flags, val+1);
3956 Warning ("Condition is never true");
3957 AddCodeLine ("jsr return0");
3966 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3967 * into the normal, non-optimized stuff. Note: The standard stuff will
3968 * always work with ints.
3970 flags &= ~CF_FORCECHAR;
3971 g_push (flags & ~CF_CONST, 0);
3974 /* Use long way over the stack */
3975 oper (flags, val, ops);
3980 void g_ge (unsigned flags, unsigned long val)
3981 /* Test for greater than or equal to */
3983 static const char* ops[12] = {
3984 "tosgeax", "tosugeax", "tosgeeax", "tosugeeax",
3990 /* If the right hand side is const, the lhs is not on stack but still
3991 * in the primary register.
3993 if (flags & CF_CONST) {
3995 /* Because the handling of the overflow flag is too complex for
3996 * inlining, we can handle only unsigned compares, and signed
3997 * compares against zero here.
3999 if (flags & CF_UNSIGNED) {
4001 /* Give a warning in some special cases */
4003 Warning ("Condition is always true");
4004 AddCodeLine ("jsr return1");
4008 /* Look at the type */
4009 switch (flags & CF_TYPE) {
4012 if (flags & CF_FORCECHAR) {
4013 /* Do a subtraction. Condition is true if carry set */
4014 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4015 AddCodeLine ("lda #$00");
4016 AddCodeLine ("ldx #$00");
4017 AddCodeLine ("rol a");
4023 /* Do a subtraction. Condition is true if carry set */
4024 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4025 AddCodeLine ("txa");
4026 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4027 AddCodeLine ("lda #$00");
4028 AddCodeLine ("ldx #$00");
4029 AddCodeLine ("rol a");
4033 /* Do a subtraction. Condition is true if carry set */
4034 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4035 AddCodeLine ("txa");
4036 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4037 AddCodeLine ("lda sreg");
4038 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
4039 AddCodeLine ("lda sreg+1");
4040 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
4041 AddCodeLine ("lda #$00");
4042 AddCodeLine ("ldx #$00");
4043 AddCodeLine ("rol a");
4050 } else if (val == 0) {
4052 /* A signed compare against zero must only look at the sign bit */
4053 switch (flags & CF_TYPE) {
4056 if (flags & CF_FORCECHAR) {
4057 AddCodeLine ("tax");
4058 AddCodeLine ("jsr boolge");
4064 /* Just test the high byte */
4065 AddCodeLine ("txa");
4066 AddCodeLine ("jsr boolge");
4070 /* Just test the high byte */
4071 AddCodeLine ("lda sreg+1");
4072 AddCodeLine ("jsr boolge");
4081 /* Signed compare against a constant != zero */
4082 switch (flags & CF_TYPE) {
4085 if (flags & CF_FORCECHAR) {
4086 Label = GetLocalLabel ();
4087 AddCodeLine ("sec");
4088 AddCodeLine ("sbc #$%02X", (unsigned char)val);
4089 AddCodeLine ("bvs %s", LocalLabelName (Label));
4090 AddCodeLine ("eor #$80");
4091 g_defcodelabel (Label);
4092 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4093 AddCodeLine ("lda #$00");
4094 AddCodeLine ("ldx #$00");
4095 AddCodeLine ("rol a");
4101 /* Do a subtraction */
4102 Label = GetLocalLabel ();
4103 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4104 AddCodeLine ("txa");
4105 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4106 AddCodeLine ("bvs %s", LocalLabelName (Label));
4107 AddCodeLine ("eor #$80");
4108 g_defcodelabel (Label);
4109 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4110 AddCodeLine ("lda #$00");
4111 AddCodeLine ("ldx #$00");
4112 AddCodeLine ("rol a");
4116 /* This one is too costly */
4124 /* If we go here, we didn't emit code. Push the lhs on stack and fall
4125 * into the normal, non-optimized stuff. Note: The standard stuff will
4126 * always work with ints.
4128 flags &= ~CF_FORCECHAR;
4129 g_push (flags & ~CF_CONST, 0);
4132 /* Use long way over the stack */
4133 oper (flags, val, ops);
4138 /*****************************************************************************/
4139 /* Allocating static storage */
4140 /*****************************************************************************/
4144 void g_res (unsigned n)
4145 /* Reserve static storage, n bytes */
4147 AddDataLine ("\t.res\t%u,$00", n);
4152 void g_defdata (unsigned flags, unsigned long val, long offs)
4153 /* Define data with the size given in flags */
4155 if (flags & CF_CONST) {
4157 /* Numeric constant */
4158 switch (flags & CF_TYPE) {
4161 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
4165 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
4169 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
4180 /* Create the correct label name */
4181 const char* Label = GetLabelName (flags, val, offs);
4183 /* Labels are always 16 bit */
4184 AddDataLine ("\t.addr\t%s", Label);
4191 void g_defbytes (const void* Bytes, unsigned Count)
4192 /* Output a row of bytes as a constant */
4198 /* Cast the buffer pointer */
4199 const unsigned char* Data = (const unsigned char*) Bytes;
4201 /* Output the stuff */
4204 /* How many go into this line? */
4205 if ((Chunk = Count) > 16) {
4210 /* Output one line */
4211 strcpy (Buf, "\t.byte\t");
4214 B += sprintf (B, "$%02X", *Data++);
4220 /* Output the line */
4221 AddDataLine ("%s", Buf);
4227 void g_zerobytes (unsigned Count)
4228 /* Output Count bytes of data initialized with zero */
4231 AddDataLine ("\t.res\t%u,$00", Count);
4237 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4238 /* Initialize a register variable from static initialization data */
4240 /* Register variables do always have less than 128 bytes */
4241 unsigned CodeLabel = GetLocalLabel ();
4243 g_defcodelabel (CodeLabel);
4244 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4245 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4246 AddCodeLine ("dex");
4247 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4252 void g_initauto (unsigned Label, unsigned Size)
4253 /* Initialize a local variable at stack offset zero from static data */
4255 unsigned CodeLabel = GetLocalLabel ();
4257 CheckLocalOffs (Size);
4260 g_defcodelabel (CodeLabel);
4261 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4262 AddCodeLine ("sta (sp),y");
4263 AddCodeLine ("dey");
4264 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4265 } else if (Size <= 256) {
4266 AddCodeLine ("ldy #$00");
4267 g_defcodelabel (CodeLabel);
4268 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4269 AddCodeLine ("sta (sp),y");
4270 AddCodeLine ("iny");
4271 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4272 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4278 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4279 /* Initialize a static local variable from static initialization data */
4282 unsigned CodeLabel = GetLocalLabel ();
4284 g_defcodelabel (CodeLabel);
4285 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4286 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4287 AddCodeLine ("dey");
4288 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4289 } else if (Size <= 256) {
4290 unsigned CodeLabel = GetLocalLabel ();
4291 AddCodeLine ("ldy #$00");
4292 g_defcodelabel (CodeLabel);
4293 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4294 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4295 AddCodeLine ("iny");
4296 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4297 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4299 /* Use the easy way here: memcpy */
4300 g_getimmed (CF_STATIC, VarLabel, 0);
4301 AddCodeLine ("jsr pushax");
4302 g_getimmed (CF_STATIC, InitLabel, 0);
4303 AddCodeLine ("jsr pushax");
4304 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4305 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4311 /*****************************************************************************/
4312 /* Switch statement */
4313 /*****************************************************************************/
4317 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4318 /* Generate code for a switch statement */
4320 unsigned NextLabel = 0;
4323 /* Setup registers and determine which compare insn to use */
4324 const char* Compare;
4327 Compare = "cmp #$%02X";
4330 Compare = "cpx #$%02X";
4333 AddCodeLine ("ldy sreg");
4334 Compare = "cpy #$%02X";
4337 AddCodeLine ("ldy sreg+1");
4338 Compare = "cpy #$%02X";
4341 Internal ("Invalid depth in g_switch: %u", Depth);
4344 /* Walk over all nodes */
4345 for (I = 0; I < CollCount (Nodes); ++I) {
4347 /* Get the next case node */
4348 CaseNode* N = CollAtUnchecked (Nodes, I);
4350 /* If we have a next label, define it */
4352 g_defcodelabel (NextLabel);
4356 /* Do the compare */
4357 AddCodeLine (Compare, CN_GetValue (N));
4359 /* If this is the last level, jump directly to the case code if found */
4362 /* Branch if equal */
4363 g_falsejump (0, CN_GetLabel (N));
4367 /* Determine the next label */
4368 if (I == CollCount (Nodes) - 1) {
4369 /* Last node means not found */
4370 g_truejump (0, DefaultLabel);
4372 /* Jump to the next check */
4373 NextLabel = GetLocalLabel ();
4374 g_truejump (0, NextLabel);
4377 /* Check the next level */
4378 g_switch (N->Nodes, DefaultLabel, Depth-1);
4383 /* If we go here, we haven't found the label */
4384 g_jump (DefaultLabel);
4389 /*****************************************************************************/
4390 /* User supplied assembler code */
4391 /*****************************************************************************/
4395 void g_asmcode (struct StrBuf* B)
4396 /* Output one line of assembler code. */
4398 AddCodeLine ("%.*s", (int) SB_GetLen (B), SB_GetConstBuf (B));