1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2013, 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_TYPEMASK) == CF_FLOAT) {
75 Fatal ("Floating point type is currently unsupported");
77 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPEMASK);
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 /* Identify the compiler version */
150 AddTextLine ("; File generated by cc65 v %s", GetVersionAsString ());
153 /* Insert some object file options */
154 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %s\"",
155 GetVersionAsString ());
157 /* If we're producing code for some other CPU, switch the command set */
159 case CPU_6502: AddTextLine ("\t.setcpu\t\t\"6502\""); break;
160 case CPU_6502X: AddTextLine ("\t.setcpu\t\t\"6502X\""); break;
161 case CPU_65SC02: AddTextLine ("\t.setcpu\t\t\"65SC02\""); break;
162 case CPU_65C02: AddTextLine ("\t.setcpu\t\t\"65C02\""); break;
163 case CPU_65816: AddTextLine ("\t.setcpu\t\t\"65816\""); break;
164 case CPU_HUC6280: AddTextLine ("\t.setcpu\t\t\"HUC6280\""); break;
165 default: Internal ("Unknown CPU: %d", CPU);
169 AddTextLine ("\t.smart\t\ton");
171 /* Allow auto import for runtime library routines */
172 AddTextLine ("\t.autoimport\ton");
174 /* Switch the assembler into case sensitive mode */
175 AddTextLine ("\t.case\t\ton");
177 /* Tell the assembler if we want to generate debug info */
178 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
180 /* Import zero page variables */
181 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank");
182 AddTextLine ("\t.importzp\ttmp1, tmp2, tmp3, tmp4, ptr1, ptr2, ptr3, ptr4");
184 /* Define long branch macros */
185 AddTextLine ("\t.macpack\tlongbranch");
190 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
191 /* If debug info is enabled, place a file info into the source */
194 /* We have to place this into the global text segment, so it will
195 ** appear before all .dbg line statements.
197 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
203 /*****************************************************************************/
204 /* Segment support */
205 /*****************************************************************************/
209 void g_userodata (void)
210 /* Switch to the read only data segment */
212 UseDataSeg (SEG_RODATA);
217 void g_usedata (void)
218 /* Switch to the data segment */
220 UseDataSeg (SEG_DATA);
226 /* Switch to the bss segment */
228 UseDataSeg (SEG_BSS);
233 void g_segname (segment_t Seg)
234 /* Emit the name of a segment if necessary */
236 /* Emit a segment directive for the data style segments */
239 case SEG_RODATA: S = CS->ROData; break;
240 case SEG_DATA: S = CS->Data; break;
241 case SEG_BSS: S = CS->BSS; break;
242 default: S = 0; break;
245 DS_AddLine (S, ".segment\t\"%s\"", GetSegName (Seg));
251 /*****************************************************************************/
253 /*****************************************************************************/
257 unsigned sizeofarg (unsigned flags)
258 /* Return the size of a function argument type that is encoded in flags */
260 switch (flags & CF_TYPEMASK) {
263 return (flags & CF_FORCECHAR)? 1 : 2;
283 int pop (unsigned flags)
284 /* Pop an argument of the given size */
286 return StackPtr += sizeofarg (flags);
291 int push (unsigned flags)
292 /* Push an argument of the given size */
294 return StackPtr -= sizeofarg (flags);
299 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
300 /* The value in Offs is an offset to an address in a/x. Make sure, an object
301 ** of the type given in Flags can be loaded or stored into this address by
302 ** adding part of the offset to the address in ax, so that the remaining
303 ** offset fits into an index register. Return the remaining offset.
306 /* If the offset is too large for a byte register, add the high byte
307 ** of the offset to the primary. Beware: We need a special correction
308 ** if the offset in the low byte will overflow in the operation.
310 unsigned O = Offs & ~0xFFU;
311 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
312 /* We need to add the low byte also */
316 /* Do the correction if we need one */
318 g_inc (CF_INT | CF_CONST, O);
322 /* Return the new offset */
328 /*****************************************************************************/
329 /* Functions handling local labels */
330 /*****************************************************************************/
334 void g_defcodelabel (unsigned label)
335 /* Define a local code label */
337 CS_AddLabel (CS->Code, LocalLabelName (label));
342 void g_defdatalabel (unsigned label)
343 /* Define a local data label */
345 AddDataLine ("%s:", LocalLabelName (label));
350 void g_aliasdatalabel (unsigned label, unsigned baselabel, long offs)
351 /* Define label as a local alias for baselabel+offs */
353 /* We need an intermediate buffer here since LocalLabelName uses a
354 ** static buffer which changes with each call.
356 StrBuf L = AUTO_STRBUF_INITIALIZER;
357 SB_AppendStr (&L, LocalLabelName (label));
359 AddDataLine ("%s\t:=\t%s+%ld",
361 LocalLabelName (baselabel),
368 /*****************************************************************************/
369 /* Functions handling global labels */
370 /*****************************************************************************/
374 void g_defgloblabel (const char* Name)
375 /* Define a global label with the given name */
377 /* Global labels are always data labels */
378 AddDataLine ("_%s:", Name);
383 void g_defexport (const char* Name, int ZP)
384 /* Export the given label */
387 AddTextLine ("\t.exportzp\t_%s", Name);
389 AddTextLine ("\t.export\t\t_%s", Name);
395 void g_defimport (const char* Name, int ZP)
396 /* Import the given label */
399 AddTextLine ("\t.importzp\t_%s", Name);
401 AddTextLine ("\t.import\t\t_%s", Name);
407 void g_importstartup (void)
408 /* Forced import of the startup module */
410 AddTextLine ("\t.forceimport\t__STARTUP__");
415 void g_importmainargs (void)
416 /* Forced import of a special symbol that handles arguments to main */
418 AddTextLine ("\t.forceimport\tinitmainargs");
423 /*****************************************************************************/
424 /* Function entry and exit */
425 /*****************************************************************************/
429 /* Remember the argument size of a function. The variable is set by g_enter
430 ** and used by g_leave. If the function gets its argument size by the caller
431 ** (variable param list or function without prototype), g_enter will set the
437 void g_enter (unsigned flags, unsigned argsize)
438 /* Function prologue */
440 if ((flags & CF_FIXARGC) != 0) {
441 /* Just remember the argument size for the leave */
445 AddCodeLine ("jsr enter");
452 /* Function epilogue */
454 /* How many bytes of locals do we have to drop? */
455 unsigned ToDrop = (unsigned) -StackPtr;
457 /* If we didn't have a variable argument list, don't call leave */
460 /* Drop stackframe if needed */
461 g_drop (ToDrop + funcargs);
463 } else if (StackPtr != 0) {
465 /* We've a stack frame to drop */
467 g_drop (ToDrop); /* Inlines the code */
468 AddCodeLine ("jsr leave");
470 AddCodeLine ("ldy #$%02X", ToDrop);
471 AddCodeLine ("jsr leavey");
476 /* Nothing to drop */
477 AddCodeLine ("jsr leave");
481 /* Add the final rts */
487 /*****************************************************************************/
488 /* Register variables */
489 /*****************************************************************************/
493 void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
494 /* Swap a register variable with a location on the stack */
496 /* Calculate the actual stack offset and check it */
497 StackOffs -= StackPtr;
498 CheckLocalOffs (StackOffs);
501 AddCodeLine ("ldy #$%02X", StackOffs & 0xFF);
504 if (IS_Get (&CodeSizeFactor) < 165) {
505 AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
506 AddCodeLine ("jsr regswap1");
508 AddCodeLine ("lda (sp),y");
509 AddCodeLine ("ldx regbank%+d", RegOffs);
510 AddCodeLine ("sta regbank%+d", RegOffs);
512 AddCodeLine ("sta (sp),y");
515 } else if (Bytes == 2) {
517 AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
518 AddCodeLine ("jsr regswap2");
522 AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
523 AddCodeLine ("lda #$%02X", Bytes & 0xFF);
524 AddCodeLine ("jsr regswap");
530 void g_save_regvars (int RegOffs, unsigned Bytes)
531 /* Save register variables */
533 /* Don't loop for up to two bytes */
536 AddCodeLine ("lda regbank%+d", RegOffs);
537 AddCodeLine ("jsr pusha");
539 } else if (Bytes == 2) {
541 AddCodeLine ("lda regbank%+d", RegOffs);
542 AddCodeLine ("ldx regbank%+d", RegOffs+1);
543 AddCodeLine ("jsr pushax");
547 /* More than two bytes - loop */
548 unsigned Label = GetLocalLabel ();
550 AddCodeLine ("ldy #$%02X", (unsigned char) (Bytes - 1));
551 AddCodeLine ("ldx #$%02X", (unsigned char) Bytes);
552 g_defcodelabel (Label);
553 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
554 AddCodeLine ("sta (sp),y");
557 AddCodeLine ("bne %s", LocalLabelName (Label));
561 /* We pushed stuff, correct the stack pointer */
567 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
568 /* Restore register variables */
570 /* Calculate the actual stack offset and check it */
571 StackOffs -= StackPtr;
572 CheckLocalOffs (StackOffs);
574 /* Don't loop for up to two bytes */
577 AddCodeLine ("ldy #$%02X", StackOffs);
578 AddCodeLine ("lda (sp),y");
579 AddCodeLine ("sta regbank%+d", RegOffs);
581 } else if (Bytes == 2) {
583 AddCodeLine ("ldy #$%02X", StackOffs);
584 AddCodeLine ("lda (sp),y");
585 AddCodeLine ("sta regbank%+d", RegOffs);
587 AddCodeLine ("lda (sp),y");
588 AddCodeLine ("sta regbank%+d", RegOffs+1);
590 } else if (Bytes == 3 && IS_Get (&CodeSizeFactor) >= 133) {
592 AddCodeLine ("ldy #$%02X", StackOffs);
593 AddCodeLine ("lda (sp),y");
594 AddCodeLine ("sta regbank%+d", RegOffs);
596 AddCodeLine ("lda (sp),y");
597 AddCodeLine ("sta regbank%+d", RegOffs+1);
599 AddCodeLine ("lda (sp),y");
600 AddCodeLine ("sta regbank%+d", RegOffs+2);
602 } else if (StackOffs <= RegOffs) {
604 /* More bytes, but the relation between the register offset in the
605 ** register bank and the stack offset allows us to generate short
606 ** code that uses just one index register.
608 unsigned Label = GetLocalLabel ();
609 AddCodeLine ("ldy #$%02X", StackOffs);
610 g_defcodelabel (Label);
611 AddCodeLine ("lda (sp),y");
612 AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs);
614 AddCodeLine ("cpy #$%02X", StackOffs + Bytes);
615 AddCodeLine ("bne %s", LocalLabelName (Label));
619 /* OK, this is the generic code. We need to save X because the
620 ** caller will only save A.
622 unsigned Label = GetLocalLabel ();
623 AddCodeLine ("stx tmp1");
624 AddCodeLine ("ldy #$%02X", (unsigned char) (StackOffs + Bytes - 1));
625 AddCodeLine ("ldx #$%02X", (unsigned char) (Bytes - 1));
626 g_defcodelabel (Label);
627 AddCodeLine ("lda (sp),y");
628 AddCodeLine ("sta regbank%+d,x", RegOffs);
631 AddCodeLine ("bpl %s", LocalLabelName (Label));
632 AddCodeLine ("ldx tmp1");
639 /*****************************************************************************/
640 /* Fetching memory cells */
641 /*****************************************************************************/
645 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
646 /* Load a constant into the primary register */
648 unsigned char B1, B2, B3, B4;
652 if ((Flags & CF_CONST) != 0) {
654 /* Numeric constant */
655 switch (Flags & CF_TYPEMASK) {
658 if ((Flags & CF_FORCECHAR) != 0) {
659 AddCodeLine ("lda #$%02X", (unsigned char) Val);
664 AddCodeLine ("ldx #$%02X", (unsigned char) (Val >> 8));
665 AddCodeLine ("lda #$%02X", (unsigned char) Val);
669 /* Split the value into 4 bytes */
670 B1 = (unsigned char) (Val >> 0);
671 B2 = (unsigned char) (Val >> 8);
672 B3 = (unsigned char) (Val >> 16);
673 B4 = (unsigned char) (Val >> 24);
675 /* Remember which bytes are done */
679 AddCodeLine ("ldx #$%02X", B2);
682 AddCodeLine ("stx sreg");
686 AddCodeLine ("stx sreg+1");
689 if ((Done & 0x04) == 0 && B1 != B3) {
690 AddCodeLine ("lda #$%02X", B3);
691 AddCodeLine ("sta sreg");
694 if ((Done & 0x08) == 0 && B1 != B4) {
695 AddCodeLine ("lda #$%02X", B4);
696 AddCodeLine ("sta sreg+1");
699 AddCodeLine ("lda #$%02X", B1);
701 if ((Done & 0x04) == 0) {
703 AddCodeLine ("sta sreg");
705 if ((Done & 0x08) == 0) {
707 AddCodeLine ("sta sreg+1");
719 /* Some sort of label */
720 const char* Label = GetLabelName (Flags, Val, Offs);
722 /* Load the address into the primary */
723 AddCodeLine ("lda #<(%s)", Label);
724 AddCodeLine ("ldx #>(%s)", Label);
731 void g_getstatic (unsigned flags, unsigned long label, long offs)
732 /* Fetch an static memory cell into the primary register */
734 /* Create the correct label name */
735 const char* lbuf = GetLabelName (flags, label, offs);
737 /* Check the size and generate the correct load operation */
738 switch (flags & CF_TYPEMASK) {
741 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
742 AddCodeLine ("lda %s", lbuf); /* load A from the label */
744 AddCodeLine ("ldx #$00");
745 AddCodeLine ("lda %s", lbuf); /* load A from the label */
746 if (!(flags & CF_UNSIGNED)) {
747 /* Must sign extend */
748 unsigned L = GetLocalLabel ();
749 AddCodeLine ("bpl %s", LocalLabelName (L));
757 AddCodeLine ("lda %s", lbuf);
758 if (flags & CF_TEST) {
759 AddCodeLine ("ora %s+1", lbuf);
761 AddCodeLine ("ldx %s+1", lbuf);
766 if (flags & CF_TEST) {
767 AddCodeLine ("lda %s+3", lbuf);
768 AddCodeLine ("ora %s+2", lbuf);
769 AddCodeLine ("ora %s+1", lbuf);
770 AddCodeLine ("ora %s+0", lbuf);
772 AddCodeLine ("lda %s+3", lbuf);
773 AddCodeLine ("sta sreg+1");
774 AddCodeLine ("lda %s+2", lbuf);
775 AddCodeLine ("sta sreg");
776 AddCodeLine ("ldx %s+1", lbuf);
777 AddCodeLine ("lda %s", lbuf);
789 void g_getlocal (unsigned Flags, int Offs)
790 /* Fetch specified local object (local var). */
793 switch (Flags & CF_TYPEMASK) {
796 CheckLocalOffs (Offs);
797 if ((Flags & CF_FORCECHAR) || (Flags & CF_TEST)) {
798 AddCodeLine ("ldy #$%02X", Offs);
799 AddCodeLine ("lda (sp),y");
801 AddCodeLine ("ldy #$%02X", Offs);
802 AddCodeLine ("ldx #$00");
803 AddCodeLine ("lda (sp),y");
804 if ((Flags & CF_UNSIGNED) == 0) {
805 unsigned L = GetLocalLabel();
806 AddCodeLine ("bpl %s", LocalLabelName (L));
814 CheckLocalOffs (Offs + 1);
815 AddCodeLine ("ldy #$%02X", (unsigned char) (Offs+1));
816 if (Flags & CF_TEST) {
817 AddCodeLine ("lda (sp),y");
819 AddCodeLine ("ora (sp),y");
821 AddCodeLine ("jsr ldaxysp");
826 CheckLocalOffs (Offs + 3);
827 AddCodeLine ("ldy #$%02X", (unsigned char) (Offs+3));
828 AddCodeLine ("jsr ldeaxysp");
829 if (Flags & CF_TEST) {
841 void g_getind (unsigned Flags, unsigned Offs)
842 /* Fetch the specified object type indirect through the primary register
843 ** into the primary register
846 /* If the offset is greater than 255, add the part that is > 255 to
847 ** the primary. This way we get an easy addition and use the low byte
850 Offs = MakeByteOffs (Flags, Offs);
852 /* Handle the indirect fetch */
853 switch (Flags & CF_TYPEMASK) {
856 /* Character sized */
857 AddCodeLine ("ldy #$%02X", Offs);
858 if (Flags & CF_UNSIGNED) {
859 AddCodeLine ("jsr ldauidx");
861 AddCodeLine ("jsr ldaidx");
866 if (Flags & CF_TEST) {
867 AddCodeLine ("ldy #$%02X", Offs);
868 AddCodeLine ("sta ptr1");
869 AddCodeLine ("stx ptr1+1");
870 AddCodeLine ("lda (ptr1),y");
872 AddCodeLine ("ora (ptr1),y");
874 AddCodeLine ("ldy #$%02X", Offs+1);
875 AddCodeLine ("jsr ldaxidx");
880 AddCodeLine ("ldy #$%02X", Offs+3);
881 AddCodeLine ("jsr ldeaxidx");
882 if (Flags & CF_TEST) {
895 void g_leasp (int Offs)
896 /* Fetch the address of the specified symbol into the primary register */
898 unsigned char Lo, Hi;
900 /* Calculate the offset relative to sp */
903 /* Get low and high byte */
904 Lo = (unsigned char) Offs;
905 Hi = (unsigned char) (Offs >> 8);
910 AddCodeLine ("lda sp");
911 AddCodeLine ("ldx sp+1");
916 AddCodeLine ("lda sp+1");
918 AddCodeLine ("adc #$%02X", Hi);
920 AddCodeLine ("lda sp");
922 } else if (Hi == 0) {
924 if (IS_Get (&CodeSizeFactor) < 200) {
925 /* 8 bit offset with subroutine call */
926 AddCodeLine ("lda #$%02X", Lo);
927 AddCodeLine ("jsr leaa0sp");
929 /* 8 bit offset inlined */
930 unsigned L = GetLocalLabel ();
931 AddCodeLine ("lda sp");
932 AddCodeLine ("ldx sp+1");
934 AddCodeLine ("adc #$%02X", Lo);
935 AddCodeLine ("bcc %s", LocalLabelName (L));
939 } else if (IS_Get (&CodeSizeFactor) < 170) {
940 /* Full 16 bit offset with subroutine call */
941 AddCodeLine ("lda #$%02X", Lo);
942 AddCodeLine ("ldx #$%02X", Hi);
943 AddCodeLine ("jsr leaaxsp");
945 /* Full 16 bit offset inlined */
946 AddCodeLine ("lda sp");
948 AddCodeLine ("adc #$%02X", Lo);
950 AddCodeLine ("lda sp+1");
951 AddCodeLine ("adc #$%02X", Hi);
959 void g_leavariadic (int Offs)
960 /* Fetch the address of a parameter in a variadic function into the primary
964 unsigned ArgSizeOffs;
966 /* Calculate the offset relative to sp */
969 /* Get the offset of the parameter which is stored at sp+0 on function
970 ** entry and check if this offset is reachable with a byte offset.
972 CHECK (StackPtr <= 0);
973 ArgSizeOffs = -StackPtr;
974 CheckLocalOffs (ArgSizeOffs);
976 /* Get the size of all parameters. */
977 AddCodeLine ("ldy #$%02X", ArgSizeOffs);
978 AddCodeLine ("lda (sp),y");
980 /* Add the value of the stackpointer */
981 if (IS_Get (&CodeSizeFactor) > 250) {
982 unsigned L = GetLocalLabel();
983 AddCodeLine ("ldx sp+1");
985 AddCodeLine ("adc sp");
986 AddCodeLine ("bcc %s", LocalLabelName (L));
990 AddCodeLine ("ldx #$00");
991 AddCodeLine ("jsr leaaxsp");
994 /* Add the offset to the primary */
996 g_inc (CF_INT | CF_CONST, Offs);
997 } else if (Offs < 0) {
998 g_dec (CF_INT | CF_CONST, -Offs);
1004 /*****************************************************************************/
1005 /* Store into memory */
1006 /*****************************************************************************/
1010 void g_putstatic (unsigned flags, unsigned long label, long offs)
1011 /* Store the primary register into the specified static memory cell */
1013 /* Create the correct label name */
1014 const char* lbuf = GetLabelName (flags, label, offs);
1016 /* Check the size and generate the correct store operation */
1017 switch (flags & CF_TYPEMASK) {
1020 AddCodeLine ("sta %s", lbuf);
1024 AddCodeLine ("sta %s", lbuf);
1025 AddCodeLine ("stx %s+1", lbuf);
1029 AddCodeLine ("sta %s", lbuf);
1030 AddCodeLine ("stx %s+1", lbuf);
1031 AddCodeLine ("ldy sreg");
1032 AddCodeLine ("sty %s+2", lbuf);
1033 AddCodeLine ("ldy sreg+1");
1034 AddCodeLine ("sty %s+3", lbuf);
1045 void g_putlocal (unsigned Flags, int Offs, long Val)
1046 /* Put data into local object. */
1049 CheckLocalOffs (Offs);
1050 switch (Flags & CF_TYPEMASK) {
1053 if (Flags & CF_CONST) {
1054 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1056 AddCodeLine ("ldy #$%02X", Offs);
1057 AddCodeLine ("sta (sp),y");
1061 if (Flags & CF_CONST) {
1062 AddCodeLine ("ldy #$%02X", Offs+1);
1063 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1064 AddCodeLine ("sta (sp),y");
1065 if ((Flags & CF_NOKEEP) == 0) {
1066 /* Place high byte into X */
1067 AddCodeLine ("tax");
1069 if ((Val & 0xFF) == Offs+1) {
1070 /* The value we need is already in Y */
1071 AddCodeLine ("tya");
1072 AddCodeLine ("dey");
1074 AddCodeLine ("dey");
1075 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1077 AddCodeLine ("sta (sp),y");
1079 AddCodeLine ("ldy #$%02X", Offs);
1080 if ((Flags & CF_NOKEEP) == 0 || IS_Get (&CodeSizeFactor) < 160) {
1081 AddCodeLine ("jsr staxysp");
1083 AddCodeLine ("sta (sp),y");
1084 AddCodeLine ("iny");
1085 AddCodeLine ("txa");
1086 AddCodeLine ("sta (sp),y");
1092 if (Flags & CF_CONST) {
1093 g_getimmed (Flags, Val, 0);
1095 AddCodeLine ("ldy #$%02X", Offs);
1096 AddCodeLine ("jsr steaxysp");
1107 void g_putind (unsigned Flags, unsigned Offs)
1108 /* Store the specified object type in the primary register at the address
1109 ** on the top of the stack
1112 /* We can handle offsets below $100 directly, larger offsets must be added
1113 ** to the address. Since a/x is in use, best code is achieved by adding
1114 ** just the high byte. Be sure to check if the low byte will overflow while
1117 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1119 /* Overflow - we need to add the low byte also */
1120 AddCodeLine ("ldy #$00");
1121 AddCodeLine ("clc");
1122 AddCodeLine ("pha");
1123 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1124 AddCodeLine ("adc (sp),y");
1125 AddCodeLine ("sta (sp),y");
1126 AddCodeLine ("iny");
1127 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1128 AddCodeLine ("adc (sp),y");
1129 AddCodeLine ("sta (sp),y");
1130 AddCodeLine ("pla");
1132 /* Complete address is on stack, new offset is zero */
1135 } else if ((Offs & 0xFF00) != 0) {
1137 /* We can just add the high byte */
1138 AddCodeLine ("ldy #$01");
1139 AddCodeLine ("clc");
1140 AddCodeLine ("pha");
1141 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1142 AddCodeLine ("adc (sp),y");
1143 AddCodeLine ("sta (sp),y");
1144 AddCodeLine ("pla");
1146 /* Offset is now just the low byte */
1150 /* Check the size and determine operation */
1151 AddCodeLine ("ldy #$%02X", Offs);
1152 switch (Flags & CF_TYPEMASK) {
1155 AddCodeLine ("jsr staspidx");
1159 AddCodeLine ("jsr staxspidx");
1163 AddCodeLine ("jsr steaxspidx");
1171 /* Pop the argument which is always a pointer */
1177 /*****************************************************************************/
1178 /* type conversion and similiar stuff */
1179 /*****************************************************************************/
1183 void g_toslong (unsigned flags)
1184 /* Make sure, the value on TOS is a long. Convert if necessary */
1186 switch (flags & CF_TYPEMASK) {
1190 if (flags & CF_UNSIGNED) {
1191 AddCodeLine ("jsr tosulong");
1193 AddCodeLine ("jsr toslong");
1208 void g_tosint (unsigned flags)
1209 /* Make sure, the value on TOS is an int. Convert if necessary */
1211 switch (flags & CF_TYPEMASK) {
1218 AddCodeLine ("jsr tosint");
1229 static void g_regchar (unsigned Flags)
1230 /* Make sure, the value in the primary register is in the range of char. Truncate if necessary */
1234 AddCodeLine ("ldx #$00");
1236 if ((Flags & CF_UNSIGNED) == 0) {
1238 L = GetLocalLabel();
1239 AddCodeLine ("cmp #$80");
1240 AddCodeLine ("bcc %s", LocalLabelName (L));
1241 AddCodeLine ("dex");
1248 void g_regint (unsigned Flags)
1249 /* Make sure, the value in the primary register an int. Convert if necessary */
1251 switch (Flags & CF_TYPEMASK) {
1254 if (Flags & CF_FORCECHAR) {
1255 /* Conversion is from char */
1271 void g_reglong (unsigned Flags)
1272 /* Make sure, the value in the primary register a long. Convert if necessary */
1274 switch (Flags & CF_TYPEMASK) {
1277 if (Flags & CF_FORCECHAR) {
1278 /* Conversion is from char */
1279 if (Flags & CF_UNSIGNED) {
1280 if (IS_Get (&CodeSizeFactor) >= 200) {
1281 AddCodeLine ("ldx #$00");
1282 AddCodeLine ("stx sreg");
1283 AddCodeLine ("stx sreg+1");
1285 AddCodeLine ("jsr aulong");
1288 if (IS_Get (&CodeSizeFactor) >= 366) {
1290 AddCodeLine ("stx sreg");
1291 AddCodeLine ("stx sreg+1");
1293 AddCodeLine ("jsr along");
1300 if (Flags & CF_UNSIGNED) {
1301 if (IS_Get (&CodeSizeFactor) >= 200) {
1302 AddCodeLine ("ldy #$00");
1303 AddCodeLine ("sty sreg");
1304 AddCodeLine ("sty sreg+1");
1306 AddCodeLine ("jsr axulong");
1309 AddCodeLine ("jsr axlong");
1323 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1324 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1325 ** value, that corresponds to the value on TOS, rhs corresponds to the value
1326 ** in (e)ax. The return value is the the flags value for the resulting type.
1329 unsigned ltype, rtype;
1332 /* Get the type spec from the flags */
1333 ltype = lhs & CF_TYPEMASK;
1334 rtype = rhs & CF_TYPEMASK;
1336 /* Check if a conversion is needed */
1337 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1338 /* We must promote the primary register to long */
1340 /* Get the new rhs type */
1341 rhs = (rhs & ~CF_TYPEMASK) | CF_LONG;
1343 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1344 /* We must promote the lhs to long */
1350 /* Get the new rhs type */
1351 lhs = (lhs & ~CF_TYPEMASK) | CF_LONG;
1355 /* Determine the result type for the operation:
1356 ** - The result is const if both operands are const.
1357 ** - The result is unsigned if one of the operands is unsigned.
1358 ** - The result is long if one of the operands is long.
1359 ** - Otherwise the result is int sized.
1361 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1362 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1363 if (rtype == CF_LONG || ltype == CF_LONG) {
1373 unsigned g_typecast (unsigned lhs, unsigned rhs)
1374 /* Cast the value in the primary register to the operand size that is flagged
1375 ** by the lhs value. Return the result value.
1378 /* Check if a conversion is needed */
1379 if ((rhs & CF_CONST) == 0) {
1380 switch (lhs & CF_TYPEMASK) {
1383 /* We must promote the primary register to long */
1388 /* We must promote the primary register to int */
1393 /* We must truncate the primary register to char */
1402 /* Do not need any other action. If the left type is int, and the primary
1403 ** register is long, it will be automagically truncated. If the right hand
1404 ** side is const, it is not located in the primary register and handled by
1405 ** the expression parser code.
1408 /* Result is const if the right hand side was const */
1409 lhs |= (rhs & CF_CONST);
1411 /* The resulting type is that of the left hand side (that's why you called
1412 ** this function :-)
1419 void g_scale (unsigned flags, long val)
1420 /* Scale the value in the primary register by the given value. If val is positive,
1421 ** scale up, is val is negative, scale down. This function is used to scale
1422 ** the operands or results of pointer arithmetic by the size of the type, the
1423 ** pointer points to.
1428 /* Value may not be zero */
1430 Internal ("Data type has no size");
1431 } else if (val > 0) {
1434 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1436 /* Factor is 2, 4, 8 and 16, use special function */
1437 switch (flags & CF_TYPEMASK) {
1440 if (flags & CF_FORCECHAR) {
1442 AddCodeLine ("asl a");
1449 if (flags & CF_UNSIGNED) {
1450 AddCodeLine ("jsr shlax%d", p2);
1452 AddCodeLine ("jsr aslax%d", p2);
1457 if (flags & CF_UNSIGNED) {
1458 AddCodeLine ("jsr shleax%d", p2);
1460 AddCodeLine ("jsr asleax%d", p2);
1469 } else if (val != 1) {
1471 /* Use a multiplication instead */
1472 g_mul (flags | CF_CONST, val);
1480 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1482 /* Factor is 2, 4, 8 and 16 use special function */
1483 switch (flags & CF_TYPEMASK) {
1486 if (flags & CF_FORCECHAR) {
1487 if (flags & CF_UNSIGNED) {
1489 AddCodeLine ("lsr a");
1492 } else if (p2 <= 2) {
1493 AddCodeLine ("cmp #$80");
1494 AddCodeLine ("ror a");
1501 if (flags & CF_UNSIGNED) {
1502 AddCodeLine ("jsr lsrax%d", p2);
1504 AddCodeLine ("jsr asrax%d", p2);
1509 if (flags & CF_UNSIGNED) {
1510 AddCodeLine ("jsr lsreax%d", p2);
1512 AddCodeLine ("jsr asreax%d", p2);
1521 } else if (val != 1) {
1523 /* Use a division instead */
1524 g_div (flags | CF_CONST, val);
1532 /*****************************************************************************/
1533 /* Adds and subs of variables fix a fixed address */
1534 /*****************************************************************************/
1538 void g_addlocal (unsigned flags, int offs)
1539 /* Add a local variable to ax */
1543 /* Correct the offset and check it */
1545 CheckLocalOffs (offs);
1547 switch (flags & CF_TYPEMASK) {
1550 L = GetLocalLabel();
1551 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1552 AddCodeLine ("clc");
1553 AddCodeLine ("adc (sp),y");
1554 AddCodeLine ("bcc %s", LocalLabelName (L));
1555 AddCodeLine ("inx");
1560 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1561 AddCodeLine ("clc");
1562 AddCodeLine ("adc (sp),y");
1563 AddCodeLine ("pha");
1564 AddCodeLine ("txa");
1565 AddCodeLine ("iny");
1566 AddCodeLine ("adc (sp),y");
1567 AddCodeLine ("tax");
1568 AddCodeLine ("pla");
1572 /* Do it the old way */
1574 g_getlocal (flags, offs);
1586 void g_addstatic (unsigned flags, unsigned long label, long offs)
1587 /* Add a static variable to ax */
1591 /* Create the correct label name */
1592 const char* lbuf = GetLabelName (flags, label, offs);
1594 switch (flags & CF_TYPEMASK) {
1597 L = GetLocalLabel();
1598 AddCodeLine ("clc");
1599 AddCodeLine ("adc %s", lbuf);
1600 AddCodeLine ("bcc %s", LocalLabelName (L));
1601 AddCodeLine ("inx");
1606 AddCodeLine ("clc");
1607 AddCodeLine ("adc %s", lbuf);
1608 AddCodeLine ("tay");
1609 AddCodeLine ("txa");
1610 AddCodeLine ("adc %s+1", lbuf);
1611 AddCodeLine ("tax");
1612 AddCodeLine ("tya");
1616 /* Do it the old way */
1618 g_getstatic (flags, label, offs);
1630 /*****************************************************************************/
1631 /* Special op= functions */
1632 /*****************************************************************************/
1636 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1638 /* Emit += for a static variable */
1640 /* Create the correct label name */
1641 const char* lbuf = GetLabelName (flags, label, offs);
1643 /* Check the size and determine operation */
1644 switch (flags & CF_TYPEMASK) {
1647 if (flags & CF_FORCECHAR) {
1648 AddCodeLine ("ldx #$00");
1649 if (flags & CF_CONST) {
1651 AddCodeLine ("inc %s", lbuf);
1652 AddCodeLine ("lda %s", lbuf);
1654 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1655 AddCodeLine ("clc");
1656 AddCodeLine ("adc %s", lbuf);
1657 AddCodeLine ("sta %s", lbuf);
1660 AddCodeLine ("clc");
1661 AddCodeLine ("adc %s", lbuf);
1662 AddCodeLine ("sta %s", lbuf);
1664 if ((flags & CF_UNSIGNED) == 0) {
1665 unsigned L = GetLocalLabel();
1666 AddCodeLine ("bpl %s", LocalLabelName (L));
1667 AddCodeLine ("dex");
1675 if (flags & CF_CONST) {
1677 unsigned L = GetLocalLabel ();
1678 AddCodeLine ("inc %s", lbuf);
1679 AddCodeLine ("bne %s", LocalLabelName (L));
1680 AddCodeLine ("inc %s+1", lbuf);
1682 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1683 AddCodeLine ("ldx %s+1", lbuf);
1685 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1686 AddCodeLine ("clc");
1687 AddCodeLine ("adc %s", lbuf);
1688 AddCodeLine ("sta %s", lbuf);
1690 unsigned L = GetLocalLabel ();
1691 AddCodeLine ("bcc %s", LocalLabelName (L));
1692 AddCodeLine ("inc %s+1", lbuf);
1694 AddCodeLine ("ldx %s+1", lbuf);
1696 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1697 AddCodeLine ("adc %s+1", lbuf);
1698 AddCodeLine ("sta %s+1", lbuf);
1699 AddCodeLine ("tax");
1700 AddCodeLine ("lda %s", lbuf);
1704 AddCodeLine ("clc");
1705 AddCodeLine ("adc %s", lbuf);
1706 AddCodeLine ("sta %s", lbuf);
1707 AddCodeLine ("txa");
1708 AddCodeLine ("adc %s+1", lbuf);
1709 AddCodeLine ("sta %s+1", lbuf);
1710 AddCodeLine ("tax");
1711 AddCodeLine ("lda %s", lbuf);
1716 if (flags & CF_CONST) {
1718 AddCodeLine ("ldy #<(%s)", lbuf);
1719 AddCodeLine ("sty ptr1");
1720 AddCodeLine ("ldy #>(%s)", lbuf);
1722 AddCodeLine ("jsr laddeq1");
1724 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1725 AddCodeLine ("jsr laddeqa");
1728 g_getstatic (flags, label, offs);
1730 g_putstatic (flags, label, offs);
1733 AddCodeLine ("ldy #<(%s)", lbuf);
1734 AddCodeLine ("sty ptr1");
1735 AddCodeLine ("ldy #>(%s)", lbuf);
1736 AddCodeLine ("jsr laddeq");
1747 void g_addeqlocal (unsigned flags, int Offs, unsigned long val)
1748 /* Emit += for a local variable */
1750 /* Calculate the true offset, check it, load it into Y */
1752 CheckLocalOffs (Offs);
1754 /* Check the size and determine operation */
1755 switch (flags & CF_TYPEMASK) {
1758 if (flags & CF_FORCECHAR) {
1759 AddCodeLine ("ldy #$%02X", Offs);
1760 AddCodeLine ("ldx #$00");
1761 if (flags & CF_CONST) {
1762 AddCodeLine ("clc");
1763 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1764 AddCodeLine ("adc (sp),y");
1765 AddCodeLine ("sta (sp),y");
1767 AddCodeLine ("clc");
1768 AddCodeLine ("adc (sp),y");
1769 AddCodeLine ("sta (sp),y");
1771 if ((flags & CF_UNSIGNED) == 0) {
1772 unsigned L = GetLocalLabel();
1773 AddCodeLine ("bpl %s", LocalLabelName (L));
1774 AddCodeLine ("dex");
1782 AddCodeLine ("ldy #$%02X", Offs);
1783 if (flags & CF_CONST) {
1784 if (IS_Get (&CodeSizeFactor) >= 400) {
1785 AddCodeLine ("clc");
1786 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1787 AddCodeLine ("adc (sp),y");
1788 AddCodeLine ("sta (sp),y");
1789 AddCodeLine ("iny");
1790 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1791 AddCodeLine ("adc (sp),y");
1792 AddCodeLine ("sta (sp),y");
1793 AddCodeLine ("tax");
1794 AddCodeLine ("dey");
1795 AddCodeLine ("lda (sp),y");
1797 g_getimmed (flags, val, 0);
1798 AddCodeLine ("jsr addeqysp");
1801 AddCodeLine ("jsr addeqysp");
1806 if (flags & CF_CONST) {
1807 g_getimmed (flags, val, 0);
1809 AddCodeLine ("ldy #$%02X", Offs);
1810 AddCodeLine ("jsr laddeqysp");
1820 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1821 /* Emit += for the location with address in ax */
1823 /* If the offset is too large for a byte register, add the high byte
1824 ** of the offset to the primary. Beware: We need a special correction
1825 ** if the offset in the low byte will overflow in the operation.
1827 offs = MakeByteOffs (flags, offs);
1829 /* Check the size and determine operation */
1830 switch (flags & CF_TYPEMASK) {
1833 AddCodeLine ("sta ptr1");
1834 AddCodeLine ("stx ptr1+1");
1835 AddCodeLine ("ldy #$%02X", offs);
1836 AddCodeLine ("ldx #$00");
1837 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1838 AddCodeLine ("clc");
1839 AddCodeLine ("adc (ptr1),y");
1840 AddCodeLine ("sta (ptr1),y");
1845 AddCodeLine ("jsr pushax"); /* Push the address */
1846 push (CF_PTR); /* Correct the internal sp */
1847 g_getind (flags, offs); /* Fetch the value */
1848 g_inc (flags, val); /* Increment value in primary */
1849 g_putind (flags, offs); /* Store the value back */
1859 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1861 /* Emit -= for a static variable */
1863 /* Create the correct label name */
1864 const char* lbuf = GetLabelName (flags, label, offs);
1866 /* Check the size and determine operation */
1867 switch (flags & CF_TYPEMASK) {
1870 if (flags & CF_FORCECHAR) {
1871 AddCodeLine ("ldx #$00");
1872 if (flags & CF_CONST) {
1874 AddCodeLine ("dec %s", lbuf);
1875 AddCodeLine ("lda %s", lbuf);
1877 AddCodeLine ("lda %s", lbuf);
1878 AddCodeLine ("sec");
1879 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1880 AddCodeLine ("sta %s", lbuf);
1883 AddCodeLine ("eor #$FF");
1884 AddCodeLine ("sec");
1885 AddCodeLine ("adc %s", lbuf);
1886 AddCodeLine ("sta %s", lbuf);
1888 if ((flags & CF_UNSIGNED) == 0) {
1889 unsigned L = GetLocalLabel();
1890 AddCodeLine ("bpl %s", LocalLabelName (L));
1891 AddCodeLine ("dex");
1899 if (flags & CF_CONST) {
1900 AddCodeLine ("lda %s", lbuf);
1901 AddCodeLine ("sec");
1902 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1903 AddCodeLine ("sta %s", lbuf);
1905 unsigned L = GetLocalLabel ();
1906 AddCodeLine ("bcs %s", LocalLabelName (L));
1907 AddCodeLine ("dec %s+1", lbuf);
1909 AddCodeLine ("ldx %s+1", lbuf);
1911 AddCodeLine ("lda %s+1", lbuf);
1912 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1913 AddCodeLine ("sta %s+1", lbuf);
1914 AddCodeLine ("tax");
1915 AddCodeLine ("lda %s", lbuf);
1918 AddCodeLine ("eor #$FF");
1919 AddCodeLine ("sec");
1920 AddCodeLine ("adc %s", lbuf);
1921 AddCodeLine ("sta %s", lbuf);
1922 AddCodeLine ("txa");
1923 AddCodeLine ("eor #$FF");
1924 AddCodeLine ("adc %s+1", lbuf);
1925 AddCodeLine ("sta %s+1", lbuf);
1926 AddCodeLine ("tax");
1927 AddCodeLine ("lda %s", lbuf);
1932 if (flags & CF_CONST) {
1934 AddCodeLine ("ldy #<(%s)", lbuf);
1935 AddCodeLine ("sty ptr1");
1936 AddCodeLine ("ldy #>(%s)", lbuf);
1937 AddCodeLine ("lda #$%02X", (unsigned char)val);
1938 AddCodeLine ("jsr lsubeqa");
1940 g_getstatic (flags, label, offs);
1942 g_putstatic (flags, label, offs);
1945 AddCodeLine ("ldy #<(%s)", lbuf);
1946 AddCodeLine ("sty ptr1");
1947 AddCodeLine ("ldy #>(%s)", lbuf);
1948 AddCodeLine ("jsr lsubeq");
1959 void g_subeqlocal (unsigned flags, int Offs, unsigned long val)
1960 /* Emit -= for a local variable */
1962 /* Calculate the true offset, check it, load it into Y */
1964 CheckLocalOffs (Offs);
1966 /* Check the size and determine operation */
1967 switch (flags & CF_TYPEMASK) {
1970 if (flags & CF_FORCECHAR) {
1971 AddCodeLine ("ldy #$%02X", Offs);
1972 AddCodeLine ("ldx #$00");
1973 if (flags & CF_CONST) {
1974 AddCodeLine ("lda (sp),y");
1975 AddCodeLine ("sec");
1976 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1978 AddCodeLine ("eor #$FF");
1979 AddCodeLine ("sec");
1980 AddCodeLine ("adc (sp),y");
1982 AddCodeLine ("sta (sp),y");
1983 if ((flags & CF_UNSIGNED) == 0) {
1984 unsigned L = GetLocalLabel();
1985 AddCodeLine ("bpl %s", LocalLabelName (L));
1986 AddCodeLine ("dex");
1994 if (flags & CF_CONST) {
1995 g_getimmed (flags, val, 0);
1997 AddCodeLine ("ldy #$%02X", Offs);
1998 AddCodeLine ("jsr subeqysp");
2002 if (flags & CF_CONST) {
2003 g_getimmed (flags, val, 0);
2005 AddCodeLine ("ldy #$%02X", Offs);
2006 AddCodeLine ("jsr lsubeqysp");
2016 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2017 /* Emit -= for the location with address in ax */
2019 /* If the offset is too large for a byte register, add the high byte
2020 ** of the offset to the primary. Beware: We need a special correction
2021 ** if the offset in the low byte will overflow in the operation.
2023 offs = MakeByteOffs (flags, offs);
2025 /* Check the size and determine operation */
2026 switch (flags & CF_TYPEMASK) {
2029 AddCodeLine ("sta ptr1");
2030 AddCodeLine ("stx ptr1+1");
2031 AddCodeLine ("ldy #$%02X", offs);
2032 AddCodeLine ("ldx #$00");
2033 AddCodeLine ("lda (ptr1),y");
2034 AddCodeLine ("sec");
2035 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2036 AddCodeLine ("sta (ptr1),y");
2041 AddCodeLine ("jsr pushax"); /* Push the address */
2042 push (CF_PTR); /* Correct the internal sp */
2043 g_getind (flags, offs); /* Fetch the value */
2044 g_dec (flags, val); /* Increment value in primary */
2045 g_putind (flags, offs); /* Store the value back */
2055 /*****************************************************************************/
2056 /* Add a variable address to the value in ax */
2057 /*****************************************************************************/
2061 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2062 /* Add the address of a local variable to ax */
2066 /* Add the offset */
2069 /* We cannot address more then 256 bytes of locals anyway */
2070 L = GetLocalLabel();
2071 CheckLocalOffs (offs);
2072 AddCodeLine ("clc");
2073 AddCodeLine ("adc #$%02X", offs & 0xFF);
2074 /* Do also skip the CLC insn below */
2075 AddCodeLine ("bcc %s", LocalLabelName (L));
2076 AddCodeLine ("inx");
2079 /* Add the current stackpointer value */
2080 AddCodeLine ("clc");
2082 /* Label was used above */
2085 AddCodeLine ("adc sp");
2086 AddCodeLine ("tay");
2087 AddCodeLine ("txa");
2088 AddCodeLine ("adc sp+1");
2089 AddCodeLine ("tax");
2090 AddCodeLine ("tya");
2095 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2096 /* Add the address of a static variable to ax */
2098 /* Create the correct label name */
2099 const char* lbuf = GetLabelName (flags, label, offs);
2101 /* Add the address to the current ax value */
2102 AddCodeLine ("clc");
2103 AddCodeLine ("adc #<(%s)", lbuf);
2104 AddCodeLine ("tay");
2105 AddCodeLine ("txa");
2106 AddCodeLine ("adc #>(%s)", lbuf);
2107 AddCodeLine ("tax");
2108 AddCodeLine ("tya");
2113 /*****************************************************************************/
2115 /*****************************************************************************/
2119 void g_save (unsigned flags)
2120 /* Copy primary register to hold register. */
2122 /* Check the size and determine operation */
2123 switch (flags & CF_TYPEMASK) {
2126 if (flags & CF_FORCECHAR) {
2127 AddCodeLine ("pha");
2133 AddCodeLine ("sta regsave");
2134 AddCodeLine ("stx regsave+1");
2138 AddCodeLine ("jsr saveeax");
2148 void g_restore (unsigned flags)
2149 /* Copy hold register to primary. */
2151 /* Check the size and determine operation */
2152 switch (flags & CF_TYPEMASK) {
2155 if (flags & CF_FORCECHAR) {
2156 AddCodeLine ("pla");
2162 AddCodeLine ("lda regsave");
2163 AddCodeLine ("ldx regsave+1");
2167 AddCodeLine ("jsr resteax");
2177 void g_cmp (unsigned flags, unsigned long val)
2178 /* Immidiate compare. The primary register will not be changed, Z flag
2184 /* Check the size and determine operation */
2185 switch (flags & CF_TYPEMASK) {
2188 if (flags & CF_FORCECHAR) {
2189 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2195 L = GetLocalLabel();
2196 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2197 AddCodeLine ("bne %s", LocalLabelName (L));
2198 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2203 Internal ("g_cmp: Long compares not implemented");
2213 static void oper (unsigned Flags, unsigned long Val, const char** Subs)
2214 /* Encode a binary operation. subs is a pointer to four strings:
2215 ** 0 --> Operate on ints
2216 ** 1 --> Operate on unsigneds
2217 ** 2 --> Operate on longs
2218 ** 3 --> Operate on unsigned longs
2221 /* Determine the offset into the array */
2222 if (Flags & CF_UNSIGNED) {
2225 if ((Flags & CF_TYPEMASK) == CF_LONG) {
2229 /* Load the value if it is not already in the primary */
2230 if (Flags & CF_CONST) {
2232 g_getimmed (Flags, Val, 0);
2235 /* Output the operation */
2236 AddCodeLine ("jsr %s", *Subs);
2238 /* The operation will pop it's argument */
2244 void g_test (unsigned flags)
2245 /* Test the value in the primary and set the condition codes */
2247 switch (flags & CF_TYPEMASK) {
2250 if (flags & CF_FORCECHAR) {
2251 AddCodeLine ("tax");
2257 AddCodeLine ("stx tmp1");
2258 AddCodeLine ("ora tmp1");
2262 if (flags & CF_UNSIGNED) {
2263 AddCodeLine ("jsr utsteax");
2265 AddCodeLine ("jsr tsteax");
2277 void g_push (unsigned flags, unsigned long val)
2278 /* Push the primary register or a constant value onto the stack */
2280 if (flags & CF_CONST && (flags & CF_TYPEMASK) != CF_LONG) {
2282 /* We have a constant 8 or 16 bit value */
2283 if ((flags & CF_TYPEMASK) == CF_CHAR && (flags & CF_FORCECHAR)) {
2285 /* Handle as 8 bit value */
2286 AddCodeLine ("lda #$%02X", (unsigned char) val);
2287 AddCodeLine ("jsr pusha");
2291 /* Handle as 16 bit value */
2292 g_getimmed (flags, val, 0);
2293 AddCodeLine ("jsr pushax");
2298 /* Value is not 16 bit or not constant */
2299 if (flags & CF_CONST) {
2300 /* Constant 32 bit value, load into eax */
2301 g_getimmed (flags, val, 0);
2304 /* Push the primary register */
2305 switch (flags & CF_TYPEMASK) {
2308 if (flags & CF_FORCECHAR) {
2309 /* Handle as char */
2310 AddCodeLine ("jsr pusha");
2315 AddCodeLine ("jsr pushax");
2319 AddCodeLine ("jsr pusheax");
2329 /* Adjust the stack offset */
2335 void g_swap (unsigned flags)
2336 /* Swap the primary register and the top of the stack. flags give the type
2337 ** of *both* values (must have same size).
2340 switch (flags & CF_TYPEMASK) {
2344 AddCodeLine ("jsr swapstk");
2348 AddCodeLine ("jsr swapestk");
2359 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2360 /* Call the specified subroutine name */
2362 if ((Flags & CF_FIXARGC) == 0) {
2363 /* Pass the argument count */
2364 AddCodeLine ("ldy #$%02X", ArgSize);
2366 AddCodeLine ("jsr _%s", Label);
2367 StackPtr += ArgSize; /* callee pops args */
2372 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2373 /* Call subroutine indirect */
2375 if ((Flags & CF_LOCAL) == 0) {
2376 /* Address is in a/x */
2377 if ((Flags & CF_FIXARGC) == 0) {
2378 /* Pass arg count */
2379 AddCodeLine ("ldy #$%02X", ArgSize);
2381 AddCodeLine ("jsr callax");
2383 /* The address is on stack, offset is on Val */
2385 CheckLocalOffs (Offs);
2386 AddCodeLine ("pha");
2387 AddCodeLine ("ldy #$%02X", Offs);
2388 AddCodeLine ("lda (sp),y");
2389 AddCodeLine ("sta jmpvec+1");
2390 AddCodeLine ("iny");
2391 AddCodeLine ("lda (sp),y");
2392 AddCodeLine ("sta jmpvec+2");
2393 AddCodeLine ("pla");
2394 AddCodeLine ("jsr jmpvec");
2397 /* Callee pops args */
2398 StackPtr += ArgSize;
2403 void g_jump (unsigned Label)
2404 /* Jump to specified internal label number */
2406 AddCodeLine ("jmp %s", LocalLabelName (Label));
2411 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2412 /* Jump to label if zero flag clear */
2414 AddCodeLine ("jne %s", LocalLabelName (label));
2419 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2420 /* Jump to label if zero flag set */
2422 AddCodeLine ("jeq %s", LocalLabelName (label));
2427 void g_drop (unsigned Space)
2428 /* Drop space allocated on the stack */
2431 /* Inline the code since calling addysp repeatedly is quite some
2434 AddCodeLine ("pha");
2435 AddCodeLine ("lda #$%02X", (unsigned char) Space);
2436 AddCodeLine ("clc");
2437 AddCodeLine ("adc sp");
2438 AddCodeLine ("sta sp");
2439 AddCodeLine ("lda #$%02X", (unsigned char) (Space >> 8));
2440 AddCodeLine ("adc sp+1");
2441 AddCodeLine ("sta sp+1");
2442 AddCodeLine ("pla");
2443 } else if (Space > 8) {
2444 AddCodeLine ("ldy #$%02X", Space);
2445 AddCodeLine ("jsr addysp");
2446 } else if (Space != 0) {
2447 AddCodeLine ("jsr incsp%u", Space);
2453 void g_space (int Space)
2454 /* Create or drop space on the stack */
2457 /* This is actually a drop operation */
2459 } else if (Space > 255) {
2460 /* Inline the code since calling subysp repeatedly is quite some
2463 AddCodeLine ("pha");
2464 AddCodeLine ("lda sp");
2465 AddCodeLine ("sec");
2466 AddCodeLine ("sbc #$%02X", (unsigned char) Space);
2467 AddCodeLine ("sta sp");
2468 AddCodeLine ("lda sp+1");
2469 AddCodeLine ("sbc #$%02X", (unsigned char) (Space >> 8));
2470 AddCodeLine ("sta sp+1");
2471 AddCodeLine ("pla");
2472 } else if (Space > 8) {
2473 AddCodeLine ("ldy #$%02X", Space);
2474 AddCodeLine ("jsr subysp");
2475 } else if (Space != 0) {
2476 AddCodeLine ("jsr decsp%u", Space);
2482 void g_cstackcheck (void)
2483 /* Check for a C stack overflow */
2485 AddCodeLine ("jsr cstkchk");
2490 void g_stackcheck (void)
2491 /* Check for a stack overflow */
2493 AddCodeLine ("jsr stkchk");
2498 void g_add (unsigned flags, unsigned long val)
2499 /* Primary = TOS + Primary */
2501 static const char* ops[12] = {
2502 "tosaddax", "tosaddax", "tosaddeax", "tosaddeax"
2505 if (flags & CF_CONST) {
2506 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2507 g_push (flags & ~CF_CONST, 0);
2509 oper (flags, val, ops);
2514 void g_sub (unsigned flags, unsigned long val)
2515 /* Primary = TOS - Primary */
2517 static const char* ops[12] = {
2518 "tossubax", "tossubax", "tossubeax", "tossubeax",
2521 if (flags & CF_CONST) {
2522 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2523 g_push (flags & ~CF_CONST, 0);
2525 oper (flags, val, ops);
2530 void g_rsub (unsigned flags, unsigned long val)
2531 /* Primary = Primary - TOS */
2533 static const char* ops[12] = {
2534 "tosrsubax", "tosrsubax", "tosrsubeax", "tosrsubeax",
2536 oper (flags, val, ops);
2541 void g_mul (unsigned flags, unsigned long val)
2542 /* Primary = TOS * Primary */
2544 static const char* ops[12] = {
2545 "tosmulax", "tosumulax", "tosmuleax", "tosumuleax",
2550 /* Do strength reduction if the value is constant and a power of two */
2551 if (flags & CF_CONST && (p2 = PowerOf2 (val)) >= 0) {
2552 /* Generate a shift instead */
2557 /* If the right hand side is const, the lhs is not on stack but still
2558 ** in the primary register.
2560 if (flags & CF_CONST) {
2562 switch (flags & CF_TYPEMASK) {
2565 if (flags & CF_FORCECHAR) {
2566 /* Handle some special cases */
2570 AddCodeLine ("sta tmp1");
2571 AddCodeLine ("asl a");
2572 AddCodeLine ("clc");
2573 AddCodeLine ("adc tmp1");
2577 AddCodeLine ("sta tmp1");
2578 AddCodeLine ("asl a");
2579 AddCodeLine ("asl a");
2580 AddCodeLine ("clc");
2581 AddCodeLine ("adc tmp1");
2585 AddCodeLine ("sta tmp1");
2586 AddCodeLine ("asl a");
2587 AddCodeLine ("clc");
2588 AddCodeLine ("adc tmp1");
2589 AddCodeLine ("asl a");
2593 AddCodeLine ("sta tmp1");
2594 AddCodeLine ("asl a");
2595 AddCodeLine ("asl a");
2596 AddCodeLine ("clc");
2597 AddCodeLine ("adc tmp1");
2598 AddCodeLine ("asl a");
2607 AddCodeLine ("jsr mulax3");
2610 AddCodeLine ("jsr mulax5");
2613 AddCodeLine ("jsr mulax6");
2616 AddCodeLine ("jsr mulax7");
2619 AddCodeLine ("jsr mulax9");
2622 AddCodeLine ("jsr mulax10");
2634 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2635 ** into the normal, non-optimized stuff.
2637 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2638 g_push (flags & ~CF_CONST, 0);
2642 /* Use long way over the stack */
2643 oper (flags, val, ops);
2648 void g_div (unsigned flags, unsigned long val)
2649 /* Primary = TOS / Primary */
2651 static const char* ops[12] = {
2652 "tosdivax", "tosudivax", "tosdiveax", "tosudiveax",
2655 /* Do strength reduction if the value is constant and a power of two */
2657 if ((flags & CF_CONST) && (p2 = PowerOf2 (val)) >= 0) {
2658 /* Generate a shift instead */
2661 /* Generate a division */
2662 if (flags & CF_CONST) {
2663 /* lhs is not on stack */
2664 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2665 g_push (flags & ~CF_CONST, 0);
2667 oper (flags, val, ops);
2673 void g_mod (unsigned flags, unsigned long val)
2674 /* Primary = TOS % Primary */
2676 static const char* ops[12] = {
2677 "tosmodax", "tosumodax", "tosmodeax", "tosumodeax",
2681 /* Check if we can do some cost reduction */
2682 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = PowerOf2 (val)) >= 0) {
2683 /* We can do that with an AND operation */
2684 g_and (flags, val - 1);
2686 /* Do it the hard way... */
2687 if (flags & CF_CONST) {
2688 /* lhs is not on stack */
2689 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2690 g_push (flags & ~CF_CONST, 0);
2692 oper (flags, val, ops);
2698 void g_or (unsigned flags, unsigned long val)
2699 /* Primary = TOS | Primary */
2701 static const char* ops[12] = {
2702 "tosorax", "tosorax", "tosoreax", "tosoreax",
2705 /* If the right hand side is const, the lhs is not on stack but still
2706 ** in the primary register.
2708 if (flags & CF_CONST) {
2710 switch (flags & CF_TYPEMASK) {
2713 if (flags & CF_FORCECHAR) {
2714 if ((val & 0xFF) != 0) {
2715 AddCodeLine ("ora #$%02X", (unsigned char)val);
2723 if ((val & 0xFF) != 0) {
2724 AddCodeLine ("ora #$%02X", (unsigned char)val);
2726 } else if ((val & 0xFF00) == 0xFF00) {
2727 if ((val & 0xFF) != 0) {
2728 AddCodeLine ("ora #$%02X", (unsigned char)val);
2730 AddCodeLine ("ldx #$FF");
2731 } else if (val != 0) {
2732 AddCodeLine ("ora #$%02X", (unsigned char)val);
2733 AddCodeLine ("pha");
2734 AddCodeLine ("txa");
2735 AddCodeLine ("ora #$%02X", (unsigned char)(val >> 8));
2736 AddCodeLine ("tax");
2737 AddCodeLine ("pla");
2743 if ((val & 0xFF) != 0) {
2744 AddCodeLine ("ora #$%02X", (unsigned char)val);
2754 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2755 ** into the normal, non-optimized stuff. Note: The standard stuff will
2756 ** always work with ints.
2758 flags &= ~CF_FORCECHAR;
2759 g_push (flags & ~CF_CONST, 0);
2762 /* Use long way over the stack */
2763 oper (flags, val, ops);
2768 void g_xor (unsigned flags, unsigned long val)
2769 /* Primary = TOS ^ Primary */
2771 static const char* ops[12] = {
2772 "tosxorax", "tosxorax", "tosxoreax", "tosxoreax",
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_TYPEMASK) {
2784 if (flags & CF_FORCECHAR) {
2785 if ((val & 0xFF) != 0) {
2786 AddCodeLine ("eor #$%02X", (unsigned char)val);
2795 AddCodeLine ("eor #$%02X", (unsigned char)val);
2797 } else if (val != 0) {
2798 if ((val & 0xFF) != 0) {
2799 AddCodeLine ("eor #$%02X", (unsigned char)val);
2801 AddCodeLine ("pha");
2802 AddCodeLine ("txa");
2803 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2804 AddCodeLine ("tax");
2805 AddCodeLine ("pla");
2812 AddCodeLine ("eor #$%02X", (unsigned char)val);
2822 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2823 ** into the normal, non-optimized stuff. Note: The standard stuff will
2824 ** always work with ints.
2826 flags &= ~CF_FORCECHAR;
2827 g_push (flags & ~CF_CONST, 0);
2830 /* Use long way over the stack */
2831 oper (flags, val, ops);
2836 void g_and (unsigned Flags, unsigned long Val)
2837 /* Primary = TOS & Primary */
2839 static const char* ops[12] = {
2840 "tosandax", "tosandax", "tosandeax", "tosandeax",
2843 /* If the right hand side is const, the lhs is not on stack but still
2844 ** in the primary register.
2846 if (Flags & CF_CONST) {
2848 switch (Flags & CF_TYPEMASK) {
2851 if (Flags & CF_FORCECHAR) {
2852 if ((Val & 0xFF) == 0x00) {
2853 AddCodeLine ("lda #$00");
2854 } else if ((Val & 0xFF) != 0xFF) {
2855 AddCodeLine ("and #$%02X", (unsigned char)Val);
2861 if ((Val & 0xFFFF) != 0xFFFF) {
2863 AddCodeLine ("ldx #$00");
2865 AddCodeLine ("lda #$00");
2866 } else if (Val != 0xFF) {
2867 AddCodeLine ("and #$%02X", (unsigned char)Val);
2869 } else if ((Val & 0xFFFF) == 0xFF00) {
2870 AddCodeLine ("lda #$00");
2871 } else if ((Val & 0xFF00) == 0xFF00) {
2872 AddCodeLine ("and #$%02X", (unsigned char)Val);
2873 } else if ((Val & 0x00FF) == 0x0000) {
2874 AddCodeLine ("txa");
2875 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2876 AddCodeLine ("tax");
2877 AddCodeLine ("lda #$00");
2879 AddCodeLine ("tay");
2880 AddCodeLine ("txa");
2881 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2882 AddCodeLine ("tax");
2883 AddCodeLine ("tya");
2884 if ((Val & 0x00FF) == 0x0000) {
2885 AddCodeLine ("lda #$00");
2886 } else if ((Val & 0x00FF) != 0x00FF) {
2887 AddCodeLine ("and #$%02X", (unsigned char)Val);
2895 AddCodeLine ("ldx #$00");
2896 AddCodeLine ("stx sreg+1");
2897 AddCodeLine ("stx sreg");
2898 if ((Val & 0xFF) != 0xFF) {
2899 AddCodeLine ("and #$%02X", (unsigned char)Val);
2902 } else if (Val == 0xFF00) {
2903 AddCodeLine ("lda #$00");
2904 AddCodeLine ("sta sreg+1");
2905 AddCodeLine ("sta sreg");
2914 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2915 ** into the normal, non-optimized stuff. Note: The standard stuff will
2916 ** always work with ints.
2918 Flags &= ~CF_FORCECHAR;
2919 g_push (Flags & ~CF_CONST, 0);
2922 /* Use long way over the stack */
2923 oper (Flags, Val, ops);
2928 void g_asr (unsigned flags, unsigned long val)
2929 /* Primary = TOS >> Primary */
2931 static const char* ops[12] = {
2932 "tosasrax", "tosshrax", "tosasreax", "tosshreax",
2935 /* If the right hand side is const, the lhs is not on stack but still
2936 ** in the primary register.
2938 if (flags & CF_CONST) {
2940 switch (flags & CF_TYPEMASK) {
2946 if (flags & CF_UNSIGNED) {
2947 AddCodeLine ("txa");
2948 AddCodeLine ("ldx #$00");
2950 unsigned L = GetLocalLabel();
2951 AddCodeLine ("cpx #$80"); /* Sign bit into carry */
2952 AddCodeLine ("txa");
2953 AddCodeLine ("ldx #$00");
2954 AddCodeLine ("bcc %s", LocalLabelName (L));
2955 AddCodeLine ("dex"); /* Make $FF */
2961 if (flags & CF_UNSIGNED) {
2962 AddCodeLine ("jsr shrax4");
2964 AddCodeLine ("jsr asrax4");
2969 if (flags & CF_UNSIGNED) {
2970 AddCodeLine ("jsr shrax%ld", val);
2972 AddCodeLine ("jsr asrax%ld", val);
2980 AddCodeLine ("ldx #$00");
2981 AddCodeLine ("lda sreg+1");
2982 if ((flags & CF_UNSIGNED) == 0) {
2983 unsigned L = GetLocalLabel();
2984 AddCodeLine ("bpl %s", LocalLabelName (L));
2985 AddCodeLine ("dex");
2988 AddCodeLine ("stx sreg");
2989 AddCodeLine ("stx sreg+1");
2993 AddCodeLine ("ldy #$00");
2994 AddCodeLine ("ldx sreg+1");
2995 if ((flags & CF_UNSIGNED) == 0) {
2996 unsigned L = GetLocalLabel();
2997 AddCodeLine ("bpl %s", LocalLabelName (L));
2998 AddCodeLine ("dey");
3001 AddCodeLine ("lda sreg");
3002 AddCodeLine ("sty sreg+1");
3003 AddCodeLine ("sty sreg");
3007 AddCodeLine ("txa");
3008 AddCodeLine ("ldx sreg");
3009 AddCodeLine ("ldy sreg+1");
3010 AddCodeLine ("sty sreg");
3011 if ((flags & CF_UNSIGNED) == 0) {
3012 unsigned L = GetLocalLabel();
3013 AddCodeLine ("cpy #$80");
3014 AddCodeLine ("ldy #$00");
3015 AddCodeLine ("bcc %s", LocalLabelName (L));
3016 AddCodeLine ("dey");
3019 AddCodeLine ("ldy #$00");
3021 AddCodeLine ("sty sreg+1");
3025 if (flags & CF_UNSIGNED) {
3026 AddCodeLine ("jsr shreax4");
3028 AddCodeLine ("jsr asreax4");
3033 if (flags & CF_UNSIGNED) {
3034 AddCodeLine ("jsr shreax%ld", val);
3036 AddCodeLine ("jsr asreax%ld", val);
3045 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3046 ** into the normal, non-optimized stuff. Note: The standard stuff will
3047 ** always work with ints.
3049 flags &= ~CF_FORCECHAR;
3050 g_push (flags & ~CF_CONST, 0);
3053 /* Use long way over the stack */
3054 oper (flags, val, ops);
3059 void g_asl (unsigned flags, unsigned long val)
3060 /* Primary = TOS << Primary */
3062 static const char* ops[12] = {
3063 "tosaslax", "tosshlax", "tosasleax", "tosshleax",
3067 /* If the right hand side is const, the lhs is not on stack but still
3068 ** in the primary register.
3070 if (flags & CF_CONST) {
3072 switch (flags & CF_TYPEMASK) {
3078 AddCodeLine ("tax");
3079 AddCodeLine ("lda #$00");
3083 if (flags & CF_UNSIGNED) {
3084 AddCodeLine ("jsr shlax4");
3086 AddCodeLine ("jsr aslax4");
3091 if (flags & CF_UNSIGNED) {
3092 AddCodeLine ("jsr shlax%ld", val);
3094 AddCodeLine ("jsr aslax%ld", val);
3102 AddCodeLine ("sta sreg+1");
3103 AddCodeLine ("lda #$00");
3104 AddCodeLine ("tax");
3105 AddCodeLine ("sta sreg");
3109 AddCodeLine ("stx sreg+1");
3110 AddCodeLine ("sta sreg");
3111 AddCodeLine ("lda #$00");
3112 AddCodeLine ("tax");
3116 AddCodeLine ("ldy sreg");
3117 AddCodeLine ("sty sreg+1");
3118 AddCodeLine ("stx sreg");
3119 AddCodeLine ("tax");
3120 AddCodeLine ("lda #$00");
3124 if (flags & CF_UNSIGNED) {
3125 AddCodeLine ("jsr shleax4");
3127 AddCodeLine ("jsr asleax4");
3132 if (flags & CF_UNSIGNED) {
3133 AddCodeLine ("jsr shleax%ld", val);
3135 AddCodeLine ("jsr asleax%ld", val);
3144 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3145 ** into the normal, non-optimized stuff. Note: The standard stuff will
3146 ** always work with ints.
3148 flags &= ~CF_FORCECHAR;
3149 g_push (flags & ~CF_CONST, 0);
3152 /* Use long way over the stack */
3153 oper (flags, val, ops);
3158 void g_neg (unsigned Flags)
3159 /* Primary = -Primary */
3161 switch (Flags & CF_TYPEMASK) {
3164 if (Flags & CF_FORCECHAR) {
3165 AddCodeLine ("eor #$FF");
3166 AddCodeLine ("clc");
3167 AddCodeLine ("adc #$01");
3173 AddCodeLine ("jsr negax");
3177 AddCodeLine ("jsr negeax");
3187 void g_bneg (unsigned flags)
3188 /* Primary = !Primary */
3190 switch (flags & CF_TYPEMASK) {
3193 AddCodeLine ("jsr bnega");
3197 AddCodeLine ("jsr bnegax");
3201 AddCodeLine ("jsr bnegeax");
3211 void g_com (unsigned Flags)
3212 /* Primary = ~Primary */
3214 switch (Flags & CF_TYPEMASK) {
3217 if (Flags & CF_FORCECHAR) {
3218 AddCodeLine ("eor #$FF");
3224 AddCodeLine ("jsr complax");
3228 AddCodeLine ("jsr compleax");
3238 void g_inc (unsigned flags, unsigned long val)
3239 /* Increment the primary register by a given number */
3241 /* Don't inc by zero */
3246 /* Generate code for the supported types */
3248 switch (flags & CF_TYPEMASK) {
3251 if (flags & CF_FORCECHAR) {
3252 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3254 AddCodeLine ("ina");
3257 AddCodeLine ("clc");
3258 AddCodeLine ("adc #$%02X", (unsigned char)val);
3265 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) {
3266 unsigned L = GetLocalLabel();
3267 AddCodeLine ("ina");
3268 AddCodeLine ("bne %s", LocalLabelName (L));
3269 AddCodeLine ("inx");
3271 } else if (IS_Get (&CodeSizeFactor) < 200) {
3274 AddCodeLine ("jsr incax%lu", val);
3275 } else if (val <= 255) {
3276 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3277 AddCodeLine ("jsr incaxy");
3279 g_add (flags | CF_CONST, val);
3282 /* Inline the code */
3284 if ((val & 0xFF) != 0) {
3285 unsigned L = GetLocalLabel();
3286 AddCodeLine ("clc");
3287 AddCodeLine ("adc #$%02X", (unsigned char) val);
3288 AddCodeLine ("bcc %s", LocalLabelName (L));
3289 AddCodeLine ("inx");
3293 AddCodeLine ("inx");
3296 AddCodeLine ("inx");
3299 AddCodeLine ("inx");
3301 } else if ((val & 0xFF) != 0) {
3302 AddCodeLine ("clc");
3303 AddCodeLine ("adc #$%02X", (unsigned char) val);
3304 AddCodeLine ("pha");
3305 AddCodeLine ("txa");
3306 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3307 AddCodeLine ("tax");
3308 AddCodeLine ("pla");
3310 AddCodeLine ("pha");
3311 AddCodeLine ("txa");
3312 AddCodeLine ("clc");
3313 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3314 AddCodeLine ("tax");
3315 AddCodeLine ("pla");
3322 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3323 AddCodeLine ("jsr inceaxy");
3325 g_add (flags | CF_CONST, val);
3337 void g_dec (unsigned flags, unsigned long val)
3338 /* Decrement the primary register by a given number */
3340 /* Don't dec by zero */
3345 /* Generate code for the supported types */
3347 switch (flags & CF_TYPEMASK) {
3350 if (flags & CF_FORCECHAR) {
3351 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3353 AddCodeLine ("dea");
3356 AddCodeLine ("sec");
3357 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3364 if (IS_Get (&CodeSizeFactor) < 200) {
3365 /* Use subroutines */
3367 AddCodeLine ("jsr decax%d", (int) val);
3368 } else if (val <= 255) {
3369 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3370 AddCodeLine ("jsr decaxy");
3372 g_sub (flags | CF_CONST, val);
3375 /* Inline the code */
3377 if ((val & 0xFF) != 0) {
3378 unsigned L = GetLocalLabel();
3379 AddCodeLine ("sec");
3380 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3381 AddCodeLine ("bcs %s", LocalLabelName (L));
3382 AddCodeLine ("dex");
3386 AddCodeLine ("dex");
3389 AddCodeLine ("dex");
3392 if ((val & 0xFF) != 0) {
3393 AddCodeLine ("sec");
3394 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3395 AddCodeLine ("pha");
3396 AddCodeLine ("txa");
3397 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3398 AddCodeLine ("tax");
3399 AddCodeLine ("pla");
3401 AddCodeLine ("pha");
3402 AddCodeLine ("txa");
3403 AddCodeLine ("sec");
3404 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3405 AddCodeLine ("tax");
3406 AddCodeLine ("pla");
3414 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3415 AddCodeLine ("jsr deceaxy");
3417 g_sub (flags | CF_CONST, val);
3430 ** Following are the conditional operators. They compare the TOS against
3431 ** the primary and put a literal 1 in the primary if the condition is
3432 ** true, otherwise they clear the primary register
3437 void g_eq (unsigned flags, unsigned long val)
3438 /* Test for equal */
3440 static const char* ops[12] = {
3441 "toseqax", "toseqax", "toseqeax", "toseqeax",
3446 /* If the right hand side is const, the lhs is not on stack but still
3447 ** in the primary register.
3449 if (flags & CF_CONST) {
3451 switch (flags & CF_TYPEMASK) {
3454 if (flags & CF_FORCECHAR) {
3455 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3456 AddCodeLine ("jsr booleq");
3462 L = GetLocalLabel();
3463 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3464 AddCodeLine ("bne %s", LocalLabelName (L));
3465 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3467 AddCodeLine ("jsr booleq");
3477 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3478 ** into the normal, non-optimized stuff. Note: The standard stuff will
3479 ** always work with ints.
3481 flags &= ~CF_FORCECHAR;
3482 g_push (flags & ~CF_CONST, 0);
3485 /* Use long way over the stack */
3486 oper (flags, val, ops);
3491 void g_ne (unsigned flags, unsigned long val)
3492 /* Test for not equal */
3494 static const char* ops[12] = {
3495 "tosneax", "tosneax", "tosneeax", "tosneeax",
3500 /* If the right hand side is const, the lhs is not on stack but still
3501 ** in the primary register.
3503 if (flags & CF_CONST) {
3505 switch (flags & CF_TYPEMASK) {
3508 if (flags & CF_FORCECHAR) {
3509 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3510 AddCodeLine ("jsr boolne");
3516 L = GetLocalLabel();
3517 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3518 AddCodeLine ("bne %s", LocalLabelName (L));
3519 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3521 AddCodeLine ("jsr boolne");
3531 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3532 ** into the normal, non-optimized stuff. Note: The standard stuff will
3533 ** always work with ints.
3535 flags &= ~CF_FORCECHAR;
3536 g_push (flags & ~CF_CONST, 0);
3539 /* Use long way over the stack */
3540 oper (flags, val, ops);
3545 void g_lt (unsigned flags, unsigned long val)
3546 /* Test for less than */
3548 static const char* ops[12] = {
3549 "tosltax", "tosultax", "toslteax", "tosulteax",
3554 /* If the right hand side is const, the lhs is not on stack but still
3555 ** in the primary register.
3557 if (flags & CF_CONST) {
3559 /* Because the handling of the overflow flag is too complex for
3560 ** inlining, we can handle only unsigned compares, and signed
3561 ** compares against zero here.
3563 if (flags & CF_UNSIGNED) {
3565 /* Give a warning in some special cases */
3567 Warning ("Condition is never true");
3568 AddCodeLine ("jsr return0");
3572 /* Look at the type */
3573 switch (flags & CF_TYPEMASK) {
3576 if (flags & CF_FORCECHAR) {
3577 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3578 AddCodeLine ("jsr boolult");
3584 /* If the low byte is zero, we must only test the high byte */
3585 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3586 if ((val & 0xFF) != 0) {
3587 unsigned L = GetLocalLabel();
3588 AddCodeLine ("bne %s", LocalLabelName (L));
3589 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3592 AddCodeLine ("jsr boolult");
3596 /* Do a subtraction */
3597 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3598 AddCodeLine ("txa");
3599 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3600 AddCodeLine ("lda sreg");
3601 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3602 AddCodeLine ("lda sreg+1");
3603 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3604 AddCodeLine ("jsr boolult");
3611 } else if (val == 0) {
3613 /* A signed compare against zero must only look at the sign bit */
3614 switch (flags & CF_TYPEMASK) {
3617 if (flags & CF_FORCECHAR) {
3618 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3619 AddCodeLine ("lda #$00");
3620 AddCodeLine ("ldx #$00");
3621 AddCodeLine ("rol a");
3627 /* Just check the high byte */
3628 AddCodeLine ("cpx #$80"); /* Bit 7 -> carry */
3629 AddCodeLine ("lda #$00");
3630 AddCodeLine ("ldx #$00");
3631 AddCodeLine ("rol a");
3635 /* Just check the high byte */
3636 AddCodeLine ("lda sreg+1");
3637 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3638 AddCodeLine ("lda #$00");
3639 AddCodeLine ("ldx #$00");
3640 AddCodeLine ("rol a");
3649 /* Signed compare against a constant != zero */
3650 switch (flags & CF_TYPEMASK) {
3653 if (flags & CF_FORCECHAR) {
3654 Label = GetLocalLabel ();
3655 AddCodeLine ("sec");
3656 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3657 AddCodeLine ("bvc %s", LocalLabelName (Label));
3658 AddCodeLine ("eor #$80");
3659 g_defcodelabel (Label);
3660 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3661 AddCodeLine ("lda #$00");
3662 AddCodeLine ("ldx #$00");
3663 AddCodeLine ("rol a");
3669 /* Do a subtraction */
3670 Label = GetLocalLabel ();
3671 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3672 AddCodeLine ("txa");
3673 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3674 AddCodeLine ("bvc %s", LocalLabelName (Label));
3675 AddCodeLine ("eor #$80");
3676 g_defcodelabel (Label);
3677 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3678 AddCodeLine ("lda #$00");
3679 AddCodeLine ("ldx #$00");
3680 AddCodeLine ("rol a");
3684 /* This one is too costly */
3693 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3694 ** into the normal, non-optimized stuff. Note: The standard stuff will
3695 ** always work with ints.
3697 flags &= ~CF_FORCECHAR;
3698 g_push (flags & ~CF_CONST, 0);
3701 /* Use long way over the stack */
3702 oper (flags, val, ops);
3707 void g_le (unsigned flags, unsigned long val)
3708 /* Test for less than or equal to */
3710 static const char* ops[12] = {
3711 "tosleax", "tosuleax", "tosleeax", "tosuleeax",
3715 /* If the right hand side is const, the lhs is not on stack but still
3716 ** in the primary register.
3718 if (flags & CF_CONST) {
3720 /* Look at the type */
3721 switch (flags & CF_TYPEMASK) {
3724 if (flags & CF_FORCECHAR) {
3725 if (flags & CF_UNSIGNED) {
3726 /* Unsigned compare */
3728 /* Use < instead of <= because the former gives
3729 ** better code on the 6502 than the latter.
3731 g_lt (flags, val+1);
3734 Warning ("Condition is always true");
3735 AddCodeLine ("jsr return1");
3738 /* Signed compare */
3739 if ((long) val < 0x7F) {
3740 /* Use < instead of <= because the former gives
3741 ** better code on the 6502 than the latter.
3743 g_lt (flags, val+1);
3746 Warning ("Condition is always true");
3747 AddCodeLine ("jsr return1");
3755 if (flags & CF_UNSIGNED) {
3756 /* Unsigned compare */
3758 /* Use < instead of <= because the former gives
3759 ** better code on the 6502 than the latter.
3761 g_lt (flags, val+1);
3764 Warning ("Condition is always true");
3765 AddCodeLine ("jsr return1");
3768 /* Signed compare */
3769 if ((long) val < 0x7FFF) {
3770 g_lt (flags, val+1);
3773 Warning ("Condition is always true");
3774 AddCodeLine ("jsr return1");
3780 if (flags & CF_UNSIGNED) {
3781 /* Unsigned compare */
3782 if (val < 0xFFFFFFFF) {
3783 /* Use < instead of <= because the former gives
3784 ** better code on the 6502 than the latter.
3786 g_lt (flags, val+1);
3789 Warning ("Condition is always true");
3790 AddCodeLine ("jsr return1");
3793 /* Signed compare */
3794 if ((long) val < 0x7FFFFFFF) {
3795 g_lt (flags, val+1);
3798 Warning ("Condition is always true");
3799 AddCodeLine ("jsr return1");
3808 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3809 ** into the normal, non-optimized stuff. Note: The standard stuff will
3810 ** always work with ints.
3812 flags &= ~CF_FORCECHAR;
3813 g_push (flags & ~CF_CONST, 0);
3816 /* Use long way over the stack */
3817 oper (flags, val, ops);
3822 void g_gt (unsigned flags, unsigned long val)
3823 /* Test for greater than */
3825 static const char* ops[12] = {
3826 "tosgtax", "tosugtax", "tosgteax", "tosugteax",
3830 /* If the right hand side is const, the lhs is not on stack but still
3831 ** in the primary register.
3833 if (flags & CF_CONST) {
3835 /* Look at the type */
3836 switch (flags & CF_TYPEMASK) {
3839 if (flags & CF_FORCECHAR) {
3840 if (flags & CF_UNSIGNED) {
3842 /* If we have a compare > 0, we will replace it by
3843 ** != 0 here, since both are identical but the
3844 ** latter is easier to optimize.
3847 } else if (val < 0xFF) {
3848 /* Use >= instead of > because the former gives
3849 ** better code on the 6502 than the latter.
3851 g_ge (flags, val+1);
3854 Warning ("Condition is never true");
3855 AddCodeLine ("jsr return0");
3858 if ((long) val < 0x7F) {
3859 /* Use >= instead of > because the former gives
3860 ** better code on the 6502 than the latter.
3862 g_ge (flags, val+1);
3865 Warning ("Condition is never true");
3866 AddCodeLine ("jsr return0");
3874 if (flags & CF_UNSIGNED) {
3875 /* Unsigned compare */
3877 /* If we have a compare > 0, we will replace it by
3878 ** != 0 here, since both are identical but the latter
3879 ** is easier to optimize.
3882 } else if (val < 0xFFFF) {
3883 /* Use >= instead of > because the former gives better
3884 ** code on the 6502 than the latter.
3886 g_ge (flags, val+1);
3889 Warning ("Condition is never true");
3890 AddCodeLine ("jsr return0");
3893 /* Signed compare */
3894 if ((long) val < 0x7FFF) {
3895 g_ge (flags, val+1);
3898 Warning ("Condition is never true");
3899 AddCodeLine ("jsr return0");
3905 if (flags & CF_UNSIGNED) {
3906 /* Unsigned compare */
3908 /* If we have a compare > 0, we will replace it by
3909 ** != 0 here, since both are identical but the latter
3910 ** is easier to optimize.
3913 } else if (val < 0xFFFFFFFF) {
3914 /* Use >= instead of > because the former gives better
3915 ** code on the 6502 than the latter.
3917 g_ge (flags, val+1);
3920 Warning ("Condition is never true");
3921 AddCodeLine ("jsr return0");
3924 /* Signed compare */
3925 if ((long) val < 0x7FFFFFFF) {
3926 g_ge (flags, val+1);
3929 Warning ("Condition is never true");
3930 AddCodeLine ("jsr return0");
3939 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3940 ** into the normal, non-optimized stuff. Note: The standard stuff will
3941 ** always work with ints.
3943 flags &= ~CF_FORCECHAR;
3944 g_push (flags & ~CF_CONST, 0);
3947 /* Use long way over the stack */
3948 oper (flags, val, ops);
3953 void g_ge (unsigned flags, unsigned long val)
3954 /* Test for greater than or equal to */
3956 static const char* ops[12] = {
3957 "tosgeax", "tosugeax", "tosgeeax", "tosugeeax",
3963 /* If the right hand side is const, the lhs is not on stack but still
3964 ** in the primary register.
3966 if (flags & CF_CONST) {
3968 /* Because the handling of the overflow flag is too complex for
3969 ** inlining, we can handle only unsigned compares, and signed
3970 ** compares against zero here.
3972 if (flags & CF_UNSIGNED) {
3974 /* Give a warning in some special cases */
3976 Warning ("Condition is always true");
3977 AddCodeLine ("jsr return1");
3981 /* Look at the type */
3982 switch (flags & CF_TYPEMASK) {
3985 if (flags & CF_FORCECHAR) {
3986 /* Do a subtraction. Condition is true if carry set */
3987 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3988 AddCodeLine ("lda #$00");
3989 AddCodeLine ("ldx #$00");
3990 AddCodeLine ("rol a");
3996 /* Do a subtraction. Condition is true if carry set */
3997 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3998 AddCodeLine ("txa");
3999 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4000 AddCodeLine ("lda #$00");
4001 AddCodeLine ("ldx #$00");
4002 AddCodeLine ("rol a");
4006 /* Do a subtraction. Condition is true if carry set */
4007 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4008 AddCodeLine ("txa");
4009 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4010 AddCodeLine ("lda sreg");
4011 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
4012 AddCodeLine ("lda sreg+1");
4013 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
4014 AddCodeLine ("lda #$00");
4015 AddCodeLine ("ldx #$00");
4016 AddCodeLine ("rol a");
4023 } else if (val == 0) {
4025 /* A signed compare against zero must only look at the sign bit */
4026 switch (flags & CF_TYPEMASK) {
4029 if (flags & CF_FORCECHAR) {
4030 AddCodeLine ("tax");
4031 AddCodeLine ("jsr boolge");
4037 /* Just test the high byte */
4038 AddCodeLine ("txa");
4039 AddCodeLine ("jsr boolge");
4043 /* Just test the high byte */
4044 AddCodeLine ("lda sreg+1");
4045 AddCodeLine ("jsr boolge");
4054 /* Signed compare against a constant != zero */
4055 switch (flags & CF_TYPEMASK) {
4058 if (flags & CF_FORCECHAR) {
4059 Label = GetLocalLabel ();
4060 AddCodeLine ("sec");
4061 AddCodeLine ("sbc #$%02X", (unsigned char)val);
4062 AddCodeLine ("bvs %s", LocalLabelName (Label));
4063 AddCodeLine ("eor #$80");
4064 g_defcodelabel (Label);
4065 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4066 AddCodeLine ("lda #$00");
4067 AddCodeLine ("ldx #$00");
4068 AddCodeLine ("rol a");
4074 /* Do a subtraction */
4075 Label = GetLocalLabel ();
4076 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4077 AddCodeLine ("txa");
4078 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4079 AddCodeLine ("bvs %s", LocalLabelName (Label));
4080 AddCodeLine ("eor #$80");
4081 g_defcodelabel (Label);
4082 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4083 AddCodeLine ("lda #$00");
4084 AddCodeLine ("ldx #$00");
4085 AddCodeLine ("rol a");
4089 /* This one is too costly */
4097 /* If we go here, we didn't emit code. Push the lhs on stack and fall
4098 ** into the normal, non-optimized stuff. Note: The standard stuff will
4099 ** always work with ints.
4101 flags &= ~CF_FORCECHAR;
4102 g_push (flags & ~CF_CONST, 0);
4105 /* Use long way over the stack */
4106 oper (flags, val, ops);
4111 /*****************************************************************************/
4112 /* Allocating static storage */
4113 /*****************************************************************************/
4117 void g_res (unsigned n)
4118 /* Reserve static storage, n bytes */
4120 AddDataLine ("\t.res\t%u,$00", n);
4125 void g_defdata (unsigned flags, unsigned long val, long offs)
4126 /* Define data with the size given in flags */
4128 if (flags & CF_CONST) {
4130 /* Numeric constant */
4131 switch (flags & CF_TYPEMASK) {
4134 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
4138 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
4142 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
4153 /* Create the correct label name */
4154 const char* Label = GetLabelName (flags, val, offs);
4156 /* Labels are always 16 bit */
4157 AddDataLine ("\t.addr\t%s", Label);
4164 void g_defbytes (const void* Bytes, unsigned Count)
4165 /* Output a row of bytes as a constant */
4171 /* Cast the buffer pointer */
4172 const unsigned char* Data = (const unsigned char*) Bytes;
4174 /* Output the stuff */
4177 /* How many go into this line? */
4178 if ((Chunk = Count) > 16) {
4183 /* Output one line */
4184 strcpy (Buf, "\t.byte\t");
4187 B += sprintf (B, "$%02X", *Data++);
4193 /* Output the line */
4194 AddDataLine ("%s", Buf);
4200 void g_zerobytes (unsigned Count)
4201 /* Output Count bytes of data initialized with zero */
4204 AddDataLine ("\t.res\t%u,$00", Count);
4210 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4211 /* Initialize a register variable from static initialization data */
4213 /* Register variables do always have less than 128 bytes */
4214 unsigned CodeLabel = GetLocalLabel ();
4215 AddCodeLine ("ldx #$%02X", (unsigned char) (Size - 1));
4216 g_defcodelabel (CodeLabel);
4217 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4218 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4219 AddCodeLine ("dex");
4220 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4225 void g_initauto (unsigned Label, unsigned Size)
4226 /* Initialize a local variable at stack offset zero from static data */
4228 unsigned CodeLabel = GetLocalLabel ();
4230 CheckLocalOffs (Size);
4232 AddCodeLine ("ldy #$%02X", Size-1);
4233 g_defcodelabel (CodeLabel);
4234 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4235 AddCodeLine ("sta (sp),y");
4236 AddCodeLine ("dey");
4237 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4238 } else if (Size <= 256) {
4239 AddCodeLine ("ldy #$00");
4240 g_defcodelabel (CodeLabel);
4241 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4242 AddCodeLine ("sta (sp),y");
4243 AddCodeLine ("iny");
4244 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4245 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4251 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4252 /* Initialize a static local variable from static initialization data */
4255 unsigned CodeLabel = GetLocalLabel ();
4256 AddCodeLine ("ldy #$%02X", Size-1);
4257 g_defcodelabel (CodeLabel);
4258 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4259 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4260 AddCodeLine ("dey");
4261 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4262 } else if (Size <= 256) {
4263 unsigned CodeLabel = GetLocalLabel ();
4264 AddCodeLine ("ldy #$00");
4265 g_defcodelabel (CodeLabel);
4266 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4267 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4268 AddCodeLine ("iny");
4269 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4270 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4272 /* Use the easy way here: memcpy */
4273 g_getimmed (CF_STATIC, VarLabel, 0);
4274 AddCodeLine ("jsr pushax");
4275 g_getimmed (CF_STATIC, InitLabel, 0);
4276 AddCodeLine ("jsr pushax");
4277 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4278 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4284 /*****************************************************************************/
4285 /* Switch statement */
4286 /*****************************************************************************/
4290 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4291 /* Generate code for a switch statement */
4293 unsigned NextLabel = 0;
4296 /* Setup registers and determine which compare insn to use */
4297 const char* Compare;
4300 Compare = "cmp #$%02X";
4303 Compare = "cpx #$%02X";
4306 AddCodeLine ("ldy sreg");
4307 Compare = "cpy #$%02X";
4310 AddCodeLine ("ldy sreg+1");
4311 Compare = "cpy #$%02X";
4314 Internal ("Invalid depth in g_switch: %u", Depth);
4317 /* Walk over all nodes */
4318 for (I = 0; I < CollCount (Nodes); ++I) {
4320 /* Get the next case node */
4321 CaseNode* N = CollAtUnchecked (Nodes, I);
4323 /* If we have a next label, define it */
4325 g_defcodelabel (NextLabel);
4329 /* Do the compare */
4330 AddCodeLine (Compare, CN_GetValue (N));
4332 /* If this is the last level, jump directly to the case code if found */
4335 /* Branch if equal */
4336 g_falsejump (0, CN_GetLabel (N));
4340 /* Determine the next label */
4341 if (I == CollCount (Nodes) - 1) {
4342 /* Last node means not found */
4343 g_truejump (0, DefaultLabel);
4345 /* Jump to the next check */
4346 NextLabel = GetLocalLabel ();
4347 g_truejump (0, NextLabel);
4350 /* Check the next level */
4351 g_switch (N->Nodes, DefaultLabel, Depth-1);
4356 /* If we go here, we haven't found the label */
4357 g_jump (DefaultLabel);
4362 /*****************************************************************************/
4363 /* User supplied assembler code */
4364 /*****************************************************************************/
4368 void g_asmcode (struct StrBuf* B)
4369 /* Output one line of assembler code. */
4371 AddCodeLine ("%.*s", (int) SB_GetLen (B), SB_GetConstBuf (B));