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 /*****************************************************************************/
66 /*****************************************************************************/
68 /*****************************************************************************/
72 static void typeerror (unsigned type)
73 /* Print an error message about an invalid operand type */
75 /* Special handling for floats here: */
76 if ((type & CF_TYPEMASK) == CF_FLOAT) {
77 Fatal ("Floating point type is currently unsupported");
79 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPEMASK);
85 static void CheckLocalOffs (unsigned Offs)
86 /* Check the offset into the stack for 8bit range */
89 /* Too many local vars */
90 Error ("Too many local variables");
96 static const char* GetLabelName (unsigned Flags, uintptr_t Label, long Offs)
98 static char Buf [256]; /* Label name */
100 /* Create the correct label name */
101 switch (Flags & CF_ADDRMASK) {
104 /* Static memory cell */
106 xsprintf (Buf, sizeof (Buf), "%s%+ld", LocalLabelName (Label), Offs);
108 xsprintf (Buf, sizeof (Buf), "%s", LocalLabelName (Label));
115 xsprintf (Buf, sizeof (Buf), "_%s%+ld", (char*) Label, Offs);
117 xsprintf (Buf, sizeof (Buf), "_%s", (char*) Label);
122 /* Absolute address */
123 xsprintf (Buf, sizeof (Buf), "$%04X", (unsigned)((Label+Offs) & 0xFFFF));
127 /* Variable in register bank */
128 xsprintf (Buf, sizeof (Buf), "regbank+%u", (unsigned)((Label+Offs) & 0xFFFF));
132 Internal ("Invalid address flags: %04X", Flags);
135 /* Return a pointer to the static buffer */
141 /*****************************************************************************/
142 /* Pre- and postamble */
143 /*****************************************************************************/
147 void g_preamble (void)
148 /* Generate the assembler code preamble */
150 /* Identify the compiler version */
152 AddTextLine ("; File generated by cc65 v %s", GetVersionAsString ());
155 /* Insert some object file options */
156 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %s\"",
157 GetVersionAsString ());
159 /* If we're producing code for some other CPU, switch the command set */
161 case CPU_6502: AddTextLine ("\t.setcpu\t\t\"6502\""); break;
162 case CPU_6502X: AddTextLine ("\t.setcpu\t\t\"6502X\""); break;
163 case CPU_65SC02: AddTextLine ("\t.setcpu\t\t\"65SC02\""); break;
164 case CPU_65C02: AddTextLine ("\t.setcpu\t\t\"65C02\""); break;
165 case CPU_65816: AddTextLine ("\t.setcpu\t\t\"65816\""); break;
166 case CPU_HUC6280: AddTextLine ("\t.setcpu\t\t\"HUC6280\""); break;
167 default: Internal ("Unknown CPU: %d", CPU);
171 AddTextLine ("\t.smart\t\ton");
173 /* Allow auto import for runtime library routines */
174 AddTextLine ("\t.autoimport\ton");
176 /* Switch the assembler into case sensitive mode */
177 AddTextLine ("\t.case\t\ton");
179 /* Tell the assembler if we want to generate debug info */
180 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
182 /* Import zero page variables */
183 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank");
184 AddTextLine ("\t.importzp\ttmp1, tmp2, tmp3, tmp4, ptr1, ptr2, ptr3, ptr4");
186 /* Define long branch macros */
187 AddTextLine ("\t.macpack\tlongbranch");
192 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
193 /* If debug info is enabled, place a file info into the source */
196 /* We have to place this into the global text segment, so it will
197 ** appear before all .dbg line statements.
199 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
205 /*****************************************************************************/
206 /* Segment support */
207 /*****************************************************************************/
211 void g_userodata (void)
212 /* Switch to the read only data segment */
214 UseDataSeg (SEG_RODATA);
219 void g_usedata (void)
220 /* Switch to the data segment */
222 UseDataSeg (SEG_DATA);
228 /* Switch to the bss segment */
230 UseDataSeg (SEG_BSS);
235 void g_segname (segment_t Seg)
236 /* Emit the name of a segment if necessary */
238 /* Emit a segment directive for the data style segments */
241 case SEG_RODATA: S = CS->ROData; break;
242 case SEG_DATA: S = CS->Data; break;
243 case SEG_BSS: S = CS->BSS; break;
244 default: S = 0; break;
247 DS_AddLine (S, ".segment\t\"%s\"", GetSegName (Seg));
253 /*****************************************************************************/
255 /*****************************************************************************/
259 unsigned sizeofarg (unsigned flags)
260 /* Return the size of a function argument type that is encoded in flags */
262 switch (flags & CF_TYPEMASK) {
265 return (flags & CF_FORCECHAR)? 1 : 2;
285 int pop (unsigned flags)
286 /* Pop an argument of the given size */
288 return StackPtr += sizeofarg (flags);
293 int push (unsigned flags)
294 /* Push an argument of the given size */
296 return StackPtr -= sizeofarg (flags);
301 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
302 /* The value in Offs is an offset to an address in a/x. Make sure, an object
303 ** of the type given in Flags can be loaded or stored into this address by
304 ** adding part of the offset to the address in ax, so that the remaining
305 ** offset fits into an index register. Return the remaining offset.
308 /* If the offset is too large for a byte register, add the high byte
309 ** of the offset to the primary. Beware: We need a special correction
310 ** if the offset in the low byte will overflow in the operation.
312 unsigned O = Offs & ~0xFFU;
313 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
314 /* We need to add the low byte also */
318 /* Do the correction if we need one */
320 g_inc (CF_INT | CF_CONST, O);
324 /* Return the new offset */
330 /*****************************************************************************/
331 /* Functions handling local labels */
332 /*****************************************************************************/
336 void g_defcodelabel (unsigned label)
337 /* Define a local code label */
339 CS_AddLabel (CS->Code, LocalLabelName (label));
344 void g_defdatalabel (unsigned label)
345 /* Define a local data label */
347 AddDataLine ("%s:", LocalLabelName (label));
352 void g_aliasdatalabel (unsigned label, unsigned baselabel, long offs)
353 /* Define label as a local alias for baselabel+offs */
355 /* We need an intermediate buffer here since LocalLabelName uses a
356 ** static buffer which changes with each call.
358 StrBuf L = AUTO_STRBUF_INITIALIZER;
359 SB_AppendStr (&L, LocalLabelName (label));
361 AddDataLine ("%s\t:=\t%s+%ld",
363 LocalLabelName (baselabel),
370 /*****************************************************************************/
371 /* Functions handling global labels */
372 /*****************************************************************************/
376 void g_defgloblabel (const char* Name)
377 /* Define a global label with the given name */
379 /* Global labels are always data labels */
380 AddDataLine ("_%s:", Name);
385 void g_defexport (const char* Name, int ZP)
386 /* Export the given label */
389 AddTextLine ("\t.exportzp\t_%s", Name);
391 AddTextLine ("\t.export\t\t_%s", Name);
397 void g_defimport (const char* Name, int ZP)
398 /* Import the given label */
401 AddTextLine ("\t.importzp\t_%s", Name);
403 AddTextLine ("\t.import\t\t_%s", Name);
409 void g_importstartup (void)
410 /* Forced import of the startup module */
412 AddTextLine ("\t.forceimport\t__STARTUP__");
417 void g_importmainargs (void)
418 /* Forced import of a special symbol that handles arguments to main */
420 AddTextLine ("\t.forceimport\tinitmainargs");
425 /*****************************************************************************/
426 /* Function entry and exit */
427 /*****************************************************************************/
431 /* Remember the argument size of a function. The variable is set by g_enter
432 ** and used by g_leave. If the function gets its argument size by the caller
433 ** (variable param list or function without prototype), g_enter will set the
439 void g_enter (unsigned flags, unsigned argsize)
440 /* Function prologue */
442 if ((flags & CF_FIXARGC) != 0) {
443 /* Just remember the argument size for the leave */
447 AddCodeLine ("jsr enter");
454 /* Function epilogue */
456 /* How many bytes of locals do we have to drop? */
457 unsigned ToDrop = (unsigned) -StackPtr;
459 /* If we didn't have a variable argument list, don't call leave */
462 /* Drop stackframe if needed */
463 g_drop (ToDrop + funcargs);
465 } else if (StackPtr != 0) {
467 /* We've a stack frame to drop */
469 g_drop (ToDrop); /* Inlines the code */
470 AddCodeLine ("jsr leave");
472 AddCodeLine ("ldy #$%02X", ToDrop);
473 AddCodeLine ("jsr leavey");
478 /* Nothing to drop */
479 AddCodeLine ("jsr leave");
483 /* Add the final rts */
489 /*****************************************************************************/
490 /* Register variables */
491 /*****************************************************************************/
495 void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
496 /* Swap a register variable with a location on the stack */
498 /* Calculate the actual stack offset and check it */
499 StackOffs -= StackPtr;
500 CheckLocalOffs (StackOffs);
503 AddCodeLine ("ldy #$%02X", StackOffs & 0xFF);
506 if (IS_Get (&CodeSizeFactor) < 165) {
507 AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
508 AddCodeLine ("jsr regswap1");
510 AddCodeLine ("lda (sp),y");
511 AddCodeLine ("ldx regbank%+d", RegOffs);
512 AddCodeLine ("sta regbank%+d", RegOffs);
514 AddCodeLine ("sta (sp),y");
517 } else if (Bytes == 2) {
519 AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
520 AddCodeLine ("jsr regswap2");
524 AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
525 AddCodeLine ("lda #$%02X", Bytes & 0xFF);
526 AddCodeLine ("jsr regswap");
532 void g_save_regvars (int RegOffs, unsigned Bytes)
533 /* Save register variables */
535 /* Don't loop for up to two bytes */
538 AddCodeLine ("lda regbank%+d", RegOffs);
539 AddCodeLine ("jsr pusha");
541 } else if (Bytes == 2) {
543 AddCodeLine ("lda regbank%+d", RegOffs);
544 AddCodeLine ("ldx regbank%+d", RegOffs+1);
545 AddCodeLine ("jsr pushax");
549 /* More than two bytes - loop */
550 unsigned Label = GetLocalLabel ();
552 AddCodeLine ("ldy #$%02X", (unsigned char) (Bytes - 1));
553 AddCodeLine ("ldx #$%02X", (unsigned char) Bytes);
554 g_defcodelabel (Label);
555 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
556 AddCodeLine ("sta (sp),y");
559 AddCodeLine ("bne %s", LocalLabelName (Label));
563 /* We pushed stuff, correct the stack pointer */
569 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
570 /* Restore register variables */
572 /* Calculate the actual stack offset and check it */
573 StackOffs -= StackPtr;
574 CheckLocalOffs (StackOffs);
576 /* Don't loop for up to two bytes */
579 AddCodeLine ("ldy #$%02X", StackOffs);
580 AddCodeLine ("lda (sp),y");
581 AddCodeLine ("sta regbank%+d", RegOffs);
583 } else if (Bytes == 2) {
585 AddCodeLine ("ldy #$%02X", StackOffs);
586 AddCodeLine ("lda (sp),y");
587 AddCodeLine ("sta regbank%+d", RegOffs);
589 AddCodeLine ("lda (sp),y");
590 AddCodeLine ("sta regbank%+d", RegOffs+1);
592 } else if (Bytes == 3 && IS_Get (&CodeSizeFactor) >= 133) {
594 AddCodeLine ("ldy #$%02X", StackOffs);
595 AddCodeLine ("lda (sp),y");
596 AddCodeLine ("sta regbank%+d", RegOffs);
598 AddCodeLine ("lda (sp),y");
599 AddCodeLine ("sta regbank%+d", RegOffs+1);
601 AddCodeLine ("lda (sp),y");
602 AddCodeLine ("sta regbank%+d", RegOffs+2);
604 } else if (StackOffs <= RegOffs) {
606 /* More bytes, but the relation between the register offset in the
607 ** register bank and the stack offset allows us to generate short
608 ** code that uses just one index register.
610 unsigned Label = GetLocalLabel ();
611 AddCodeLine ("ldy #$%02X", StackOffs);
612 g_defcodelabel (Label);
613 AddCodeLine ("lda (sp),y");
614 AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs);
616 AddCodeLine ("cpy #$%02X", StackOffs + Bytes);
617 AddCodeLine ("bne %s", LocalLabelName (Label));
621 /* OK, this is the generic code. We need to save X because the
622 ** caller will only save A.
624 unsigned Label = GetLocalLabel ();
625 AddCodeLine ("stx tmp1");
626 AddCodeLine ("ldy #$%02X", (unsigned char) (StackOffs + Bytes - 1));
627 AddCodeLine ("ldx #$%02X", (unsigned char) (Bytes - 1));
628 g_defcodelabel (Label);
629 AddCodeLine ("lda (sp),y");
630 AddCodeLine ("sta regbank%+d,x", RegOffs);
633 AddCodeLine ("bpl %s", LocalLabelName (Label));
634 AddCodeLine ("ldx tmp1");
641 /*****************************************************************************/
642 /* Fetching memory cells */
643 /*****************************************************************************/
647 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
648 /* Load a constant into the primary register */
650 unsigned char B1, B2, B3, B4;
654 if ((Flags & CF_CONST) != 0) {
656 /* Numeric constant */
657 switch (Flags & CF_TYPEMASK) {
660 if ((Flags & CF_FORCECHAR) != 0) {
661 AddCodeLine ("lda #$%02X", (unsigned char) Val);
666 AddCodeLine ("ldx #$%02X", (unsigned char) (Val >> 8));
667 AddCodeLine ("lda #$%02X", (unsigned char) Val);
671 /* Split the value into 4 bytes */
672 B1 = (unsigned char) (Val >> 0);
673 B2 = (unsigned char) (Val >> 8);
674 B3 = (unsigned char) (Val >> 16);
675 B4 = (unsigned char) (Val >> 24);
677 /* Remember which bytes are done */
681 AddCodeLine ("ldx #$%02X", B2);
684 AddCodeLine ("stx sreg");
688 AddCodeLine ("stx sreg+1");
691 if ((Done & 0x04) == 0 && B1 != B3) {
692 AddCodeLine ("lda #$%02X", B3);
693 AddCodeLine ("sta sreg");
696 if ((Done & 0x08) == 0 && B1 != B4) {
697 AddCodeLine ("lda #$%02X", B4);
698 AddCodeLine ("sta sreg+1");
701 AddCodeLine ("lda #$%02X", B1);
703 if ((Done & 0x04) == 0) {
705 AddCodeLine ("sta sreg");
707 if ((Done & 0x08) == 0) {
709 AddCodeLine ("sta sreg+1");
721 /* Some sort of label */
722 const char* Label = GetLabelName (Flags, Val, Offs);
724 /* Load the address into the primary */
725 AddCodeLine ("lda #<(%s)", Label);
726 AddCodeLine ("ldx #>(%s)", Label);
733 void g_getstatic (unsigned flags, uintptr_t label, long offs)
734 /* Fetch an static memory cell into the primary register */
736 /* Create the correct label name */
737 const char* lbuf = GetLabelName (flags, label, offs);
739 /* Check the size and generate the correct load operation */
740 switch (flags & CF_TYPEMASK) {
743 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
744 AddCodeLine ("lda %s", lbuf); /* load A from the label */
746 AddCodeLine ("ldx #$00");
747 AddCodeLine ("lda %s", lbuf); /* load A from the label */
748 if (!(flags & CF_UNSIGNED)) {
749 /* Must sign extend */
750 unsigned L = GetLocalLabel ();
751 AddCodeLine ("bpl %s", LocalLabelName (L));
759 AddCodeLine ("lda %s", lbuf);
760 if (flags & CF_TEST) {
761 AddCodeLine ("ora %s+1", lbuf);
763 AddCodeLine ("ldx %s+1", lbuf);
768 if (flags & CF_TEST) {
769 AddCodeLine ("lda %s+3", lbuf);
770 AddCodeLine ("ora %s+2", lbuf);
771 AddCodeLine ("ora %s+1", lbuf);
772 AddCodeLine ("ora %s+0", lbuf);
774 AddCodeLine ("lda %s+3", lbuf);
775 AddCodeLine ("sta sreg+1");
776 AddCodeLine ("lda %s+2", lbuf);
777 AddCodeLine ("sta sreg");
778 AddCodeLine ("ldx %s+1", lbuf);
779 AddCodeLine ("lda %s", lbuf);
791 void g_getlocal (unsigned Flags, int Offs)
792 /* Fetch specified local object (local var). */
795 switch (Flags & CF_TYPEMASK) {
798 CheckLocalOffs (Offs);
799 if ((Flags & CF_FORCECHAR) || (Flags & CF_TEST)) {
800 AddCodeLine ("ldy #$%02X", Offs);
801 AddCodeLine ("lda (sp),y");
803 AddCodeLine ("ldy #$%02X", Offs);
804 AddCodeLine ("ldx #$00");
805 AddCodeLine ("lda (sp),y");
806 if ((Flags & CF_UNSIGNED) == 0) {
807 unsigned L = GetLocalLabel();
808 AddCodeLine ("bpl %s", LocalLabelName (L));
816 CheckLocalOffs (Offs + 1);
817 AddCodeLine ("ldy #$%02X", (unsigned char) (Offs+1));
818 if (Flags & CF_TEST) {
819 AddCodeLine ("lda (sp),y");
821 AddCodeLine ("ora (sp),y");
823 AddCodeLine ("jsr ldaxysp");
828 CheckLocalOffs (Offs + 3);
829 AddCodeLine ("ldy #$%02X", (unsigned char) (Offs+3));
830 AddCodeLine ("jsr ldeaxysp");
831 if (Flags & CF_TEST) {
843 void g_getind (unsigned Flags, unsigned Offs)
844 /* Fetch the specified object type indirect through the primary register
845 ** into the primary register
848 /* If the offset is greater than 255, add the part that is > 255 to
849 ** the primary. This way we get an easy addition and use the low byte
852 Offs = MakeByteOffs (Flags, Offs);
854 /* Handle the indirect fetch */
855 switch (Flags & CF_TYPEMASK) {
858 /* Character sized */
859 AddCodeLine ("ldy #$%02X", Offs);
860 if (Flags & CF_UNSIGNED) {
861 AddCodeLine ("jsr ldauidx");
863 AddCodeLine ("jsr ldaidx");
868 if (Flags & CF_TEST) {
869 AddCodeLine ("ldy #$%02X", Offs);
870 AddCodeLine ("sta ptr1");
871 AddCodeLine ("stx ptr1+1");
872 AddCodeLine ("lda (ptr1),y");
874 AddCodeLine ("ora (ptr1),y");
876 AddCodeLine ("ldy #$%02X", Offs+1);
877 AddCodeLine ("jsr ldaxidx");
882 AddCodeLine ("ldy #$%02X", Offs+3);
883 AddCodeLine ("jsr ldeaxidx");
884 if (Flags & CF_TEST) {
897 void g_leasp (int Offs)
898 /* Fetch the address of the specified symbol into the primary register */
900 unsigned char Lo, Hi;
902 /* Calculate the offset relative to sp */
905 /* Get low and high byte */
906 Lo = (unsigned char) Offs;
907 Hi = (unsigned char) (Offs >> 8);
912 AddCodeLine ("lda sp");
913 AddCodeLine ("ldx sp+1");
918 AddCodeLine ("lda sp+1");
920 AddCodeLine ("adc #$%02X", Hi);
922 AddCodeLine ("lda sp");
924 } else if (Hi == 0) {
926 if (IS_Get (&CodeSizeFactor) < 200) {
927 /* 8 bit offset with subroutine call */
928 AddCodeLine ("lda #$%02X", Lo);
929 AddCodeLine ("jsr leaa0sp");
931 /* 8 bit offset inlined */
932 unsigned L = GetLocalLabel ();
933 AddCodeLine ("lda sp");
934 AddCodeLine ("ldx sp+1");
936 AddCodeLine ("adc #$%02X", Lo);
937 AddCodeLine ("bcc %s", LocalLabelName (L));
941 } else if (IS_Get (&CodeSizeFactor) < 170) {
942 /* Full 16 bit offset with subroutine call */
943 AddCodeLine ("lda #$%02X", Lo);
944 AddCodeLine ("ldx #$%02X", Hi);
945 AddCodeLine ("jsr leaaxsp");
947 /* Full 16 bit offset inlined */
948 AddCodeLine ("lda sp");
950 AddCodeLine ("adc #$%02X", Lo);
952 AddCodeLine ("lda sp+1");
953 AddCodeLine ("adc #$%02X", Hi);
961 void g_leavariadic (int Offs)
962 /* Fetch the address of a parameter in a variadic function into the primary
966 unsigned ArgSizeOffs;
968 /* Calculate the offset relative to sp */
971 /* Get the offset of the parameter which is stored at sp+0 on function
972 ** entry and check if this offset is reachable with a byte offset.
974 CHECK (StackPtr <= 0);
975 ArgSizeOffs = -StackPtr;
976 CheckLocalOffs (ArgSizeOffs);
978 /* Get the size of all parameters. */
979 AddCodeLine ("ldy #$%02X", ArgSizeOffs);
980 AddCodeLine ("lda (sp),y");
982 /* Add the value of the stackpointer */
983 if (IS_Get (&CodeSizeFactor) > 250) {
984 unsigned L = GetLocalLabel();
985 AddCodeLine ("ldx sp+1");
987 AddCodeLine ("adc sp");
988 AddCodeLine ("bcc %s", LocalLabelName (L));
992 AddCodeLine ("ldx #$00");
993 AddCodeLine ("jsr leaaxsp");
996 /* Add the offset to the primary */
998 g_inc (CF_INT | CF_CONST, Offs);
999 } else if (Offs < 0) {
1000 g_dec (CF_INT | CF_CONST, -Offs);
1006 /*****************************************************************************/
1007 /* Store into memory */
1008 /*****************************************************************************/
1012 void g_putstatic (unsigned flags, uintptr_t label, long offs)
1013 /* Store the primary register into the specified static memory cell */
1015 /* Create the correct label name */
1016 const char* lbuf = GetLabelName (flags, label, offs);
1018 /* Check the size and generate the correct store operation */
1019 switch (flags & CF_TYPEMASK) {
1022 AddCodeLine ("sta %s", lbuf);
1026 AddCodeLine ("sta %s", lbuf);
1027 AddCodeLine ("stx %s+1", lbuf);
1031 AddCodeLine ("sta %s", lbuf);
1032 AddCodeLine ("stx %s+1", lbuf);
1033 AddCodeLine ("ldy sreg");
1034 AddCodeLine ("sty %s+2", lbuf);
1035 AddCodeLine ("ldy sreg+1");
1036 AddCodeLine ("sty %s+3", lbuf);
1047 void g_putlocal (unsigned Flags, int Offs, long Val)
1048 /* Put data into local object. */
1051 CheckLocalOffs (Offs);
1052 switch (Flags & CF_TYPEMASK) {
1055 if (Flags & CF_CONST) {
1056 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1058 AddCodeLine ("ldy #$%02X", Offs);
1059 AddCodeLine ("sta (sp),y");
1063 if (Flags & CF_CONST) {
1064 AddCodeLine ("ldy #$%02X", Offs+1);
1065 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1066 AddCodeLine ("sta (sp),y");
1067 if ((Flags & CF_NOKEEP) == 0) {
1068 /* Place high byte into X */
1069 AddCodeLine ("tax");
1071 if ((Val & 0xFF) == Offs+1) {
1072 /* The value we need is already in Y */
1073 AddCodeLine ("tya");
1074 AddCodeLine ("dey");
1076 AddCodeLine ("dey");
1077 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1079 AddCodeLine ("sta (sp),y");
1081 AddCodeLine ("ldy #$%02X", Offs);
1082 if ((Flags & CF_NOKEEP) == 0 || IS_Get (&CodeSizeFactor) < 160) {
1083 AddCodeLine ("jsr staxysp");
1085 AddCodeLine ("sta (sp),y");
1086 AddCodeLine ("iny");
1087 AddCodeLine ("txa");
1088 AddCodeLine ("sta (sp),y");
1094 if (Flags & CF_CONST) {
1095 g_getimmed (Flags, Val, 0);
1097 AddCodeLine ("ldy #$%02X", Offs);
1098 AddCodeLine ("jsr steaxysp");
1109 void g_putind (unsigned Flags, unsigned Offs)
1110 /* Store the specified object type in the primary register at the address
1111 ** on the top of the stack
1114 /* We can handle offsets below $100 directly, larger offsets must be added
1115 ** to the address. Since a/x is in use, best code is achieved by adding
1116 ** just the high byte. Be sure to check if the low byte will overflow while
1119 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1121 /* Overflow - we need to add the low byte also */
1122 AddCodeLine ("ldy #$00");
1123 AddCodeLine ("clc");
1124 AddCodeLine ("pha");
1125 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1126 AddCodeLine ("adc (sp),y");
1127 AddCodeLine ("sta (sp),y");
1128 AddCodeLine ("iny");
1129 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1130 AddCodeLine ("adc (sp),y");
1131 AddCodeLine ("sta (sp),y");
1132 AddCodeLine ("pla");
1134 /* Complete address is on stack, new offset is zero */
1137 } else if ((Offs & 0xFF00) != 0) {
1139 /* We can just add the high byte */
1140 AddCodeLine ("ldy #$01");
1141 AddCodeLine ("clc");
1142 AddCodeLine ("pha");
1143 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1144 AddCodeLine ("adc (sp),y");
1145 AddCodeLine ("sta (sp),y");
1146 AddCodeLine ("pla");
1148 /* Offset is now just the low byte */
1152 /* Check the size and determine operation */
1153 AddCodeLine ("ldy #$%02X", Offs);
1154 switch (Flags & CF_TYPEMASK) {
1157 AddCodeLine ("jsr staspidx");
1161 AddCodeLine ("jsr staxspidx");
1165 AddCodeLine ("jsr steaxspidx");
1173 /* Pop the argument which is always a pointer */
1179 /*****************************************************************************/
1180 /* type conversion and similiar stuff */
1181 /*****************************************************************************/
1185 void g_toslong (unsigned flags)
1186 /* Make sure, the value on TOS is a long. Convert if necessary */
1188 switch (flags & CF_TYPEMASK) {
1192 if (flags & CF_UNSIGNED) {
1193 AddCodeLine ("jsr tosulong");
1195 AddCodeLine ("jsr toslong");
1210 void g_tosint (unsigned flags)
1211 /* Make sure, the value on TOS is an int. Convert if necessary */
1213 switch (flags & CF_TYPEMASK) {
1220 AddCodeLine ("jsr tosint");
1231 static void g_regchar (unsigned Flags)
1232 /* Make sure, the value in the primary register is in the range of char. Truncate if necessary */
1236 AddCodeLine ("ldx #$00");
1238 if ((Flags & CF_UNSIGNED) == 0) {
1240 L = GetLocalLabel();
1241 AddCodeLine ("cmp #$80");
1242 AddCodeLine ("bcc %s", LocalLabelName (L));
1243 AddCodeLine ("dex");
1250 void g_regint (unsigned Flags)
1251 /* Make sure, the value in the primary register an int. Convert if necessary */
1253 switch (Flags & CF_TYPEMASK) {
1256 if (Flags & CF_FORCECHAR) {
1257 /* Conversion is from char */
1273 void g_reglong (unsigned Flags)
1274 /* Make sure, the value in the primary register a long. Convert if necessary */
1276 switch (Flags & CF_TYPEMASK) {
1279 if (Flags & CF_FORCECHAR) {
1280 /* Conversion is from char */
1281 if (Flags & CF_UNSIGNED) {
1282 if (IS_Get (&CodeSizeFactor) >= 200) {
1283 AddCodeLine ("ldx #$00");
1284 AddCodeLine ("stx sreg");
1285 AddCodeLine ("stx sreg+1");
1287 AddCodeLine ("jsr aulong");
1290 if (IS_Get (&CodeSizeFactor) >= 366) {
1292 AddCodeLine ("stx sreg");
1293 AddCodeLine ("stx sreg+1");
1295 AddCodeLine ("jsr along");
1302 if (Flags & CF_UNSIGNED) {
1303 if (IS_Get (&CodeSizeFactor) >= 200) {
1304 AddCodeLine ("ldy #$00");
1305 AddCodeLine ("sty sreg");
1306 AddCodeLine ("sty sreg+1");
1308 AddCodeLine ("jsr axulong");
1311 AddCodeLine ("jsr axlong");
1325 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1326 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1327 ** value, that corresponds to the value on TOS, rhs corresponds to the value
1328 ** in (e)ax. The return value is the the flags value for the resulting type.
1331 unsigned ltype, rtype;
1334 /* Get the type spec from the flags */
1335 ltype = lhs & CF_TYPEMASK;
1336 rtype = rhs & CF_TYPEMASK;
1338 /* Check if a conversion is needed */
1339 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1340 /* We must promote the primary register to long */
1342 /* Get the new rhs type */
1343 rhs = (rhs & ~CF_TYPEMASK) | CF_LONG;
1345 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1346 /* We must promote the lhs to long */
1352 /* Get the new rhs type */
1353 lhs = (lhs & ~CF_TYPEMASK) | CF_LONG;
1357 /* Determine the result type for the operation:
1358 ** - The result is const if both operands are const.
1359 ** - The result is unsigned if one of the operands is unsigned.
1360 ** - The result is long if one of the operands is long.
1361 ** - Otherwise the result is int sized.
1363 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1364 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1365 if (rtype == CF_LONG || ltype == CF_LONG) {
1375 unsigned g_typecast (unsigned lhs, unsigned rhs)
1376 /* Cast the value in the primary register to the operand size that is flagged
1377 ** by the lhs value. Return the result value.
1380 /* Check if a conversion is needed */
1381 if ((rhs & CF_CONST) == 0) {
1382 switch (lhs & CF_TYPEMASK) {
1385 /* We must promote the primary register to long */
1390 /* We must promote the primary register to int */
1395 /* We must truncate the primary register to char */
1404 /* Do not need any other action. If the left type is int, and the primary
1405 ** register is long, it will be automagically truncated. If the right hand
1406 ** side is const, it is not located in the primary register and handled by
1407 ** the expression parser code.
1410 /* Result is const if the right hand side was const */
1411 lhs |= (rhs & CF_CONST);
1413 /* The resulting type is that of the left hand side (that's why you called
1414 ** this function :-)
1421 void g_scale (unsigned flags, long val)
1422 /* Scale the value in the primary register by the given value. If val is positive,
1423 ** scale up, is val is negative, scale down. This function is used to scale
1424 ** the operands or results of pointer arithmetic by the size of the type, the
1425 ** pointer points to.
1430 /* Value may not be zero */
1432 Internal ("Data type has no size");
1433 } else if (val > 0) {
1436 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1438 /* Factor is 2, 4, 8 and 16, use special function */
1439 switch (flags & CF_TYPEMASK) {
1442 if (flags & CF_FORCECHAR) {
1444 AddCodeLine ("asl a");
1451 if (flags & CF_UNSIGNED) {
1452 AddCodeLine ("jsr shlax%d", p2);
1454 AddCodeLine ("jsr aslax%d", p2);
1459 if (flags & CF_UNSIGNED) {
1460 AddCodeLine ("jsr shleax%d", p2);
1462 AddCodeLine ("jsr asleax%d", p2);
1471 } else if (val != 1) {
1473 /* Use a multiplication instead */
1474 g_mul (flags | CF_CONST, val);
1482 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1484 /* Factor is 2, 4, 8 and 16 use special function */
1485 switch (flags & CF_TYPEMASK) {
1488 if (flags & CF_FORCECHAR) {
1489 if (flags & CF_UNSIGNED) {
1491 AddCodeLine ("lsr a");
1494 } else if (p2 <= 2) {
1495 AddCodeLine ("cmp #$80");
1496 AddCodeLine ("ror a");
1503 if (flags & CF_UNSIGNED) {
1504 AddCodeLine ("jsr lsrax%d", p2);
1506 AddCodeLine ("jsr asrax%d", p2);
1511 if (flags & CF_UNSIGNED) {
1512 AddCodeLine ("jsr lsreax%d", p2);
1514 AddCodeLine ("jsr asreax%d", p2);
1523 } else if (val != 1) {
1525 /* Use a division instead */
1526 g_div (flags | CF_CONST, val);
1534 /*****************************************************************************/
1535 /* Adds and subs of variables fix a fixed address */
1536 /*****************************************************************************/
1540 void g_addlocal (unsigned flags, int offs)
1541 /* Add a local variable to ax */
1545 /* Correct the offset and check it */
1547 CheckLocalOffs (offs);
1549 switch (flags & CF_TYPEMASK) {
1552 L = GetLocalLabel();
1553 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1554 AddCodeLine ("clc");
1555 AddCodeLine ("adc (sp),y");
1556 AddCodeLine ("bcc %s", LocalLabelName (L));
1557 AddCodeLine ("inx");
1562 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1563 AddCodeLine ("clc");
1564 AddCodeLine ("adc (sp),y");
1565 AddCodeLine ("pha");
1566 AddCodeLine ("txa");
1567 AddCodeLine ("iny");
1568 AddCodeLine ("adc (sp),y");
1569 AddCodeLine ("tax");
1570 AddCodeLine ("pla");
1574 /* Do it the old way */
1576 g_getlocal (flags, offs);
1588 void g_addstatic (unsigned flags, uintptr_t label, long offs)
1589 /* Add a static variable to ax */
1593 /* Create the correct label name */
1594 const char* lbuf = GetLabelName (flags, label, offs);
1596 switch (flags & CF_TYPEMASK) {
1599 L = GetLocalLabel();
1600 AddCodeLine ("clc");
1601 AddCodeLine ("adc %s", lbuf);
1602 AddCodeLine ("bcc %s", LocalLabelName (L));
1603 AddCodeLine ("inx");
1608 AddCodeLine ("clc");
1609 AddCodeLine ("adc %s", lbuf);
1610 AddCodeLine ("tay");
1611 AddCodeLine ("txa");
1612 AddCodeLine ("adc %s+1", lbuf);
1613 AddCodeLine ("tax");
1614 AddCodeLine ("tya");
1618 /* Do it the old way */
1620 g_getstatic (flags, label, offs);
1632 /*****************************************************************************/
1633 /* Special op= functions */
1634 /*****************************************************************************/
1638 void g_addeqstatic (unsigned flags, uintptr_t label, long offs,
1640 /* Emit += for a static variable */
1642 /* Create the correct label name */
1643 const char* lbuf = GetLabelName (flags, label, offs);
1645 /* Check the size and determine operation */
1646 switch (flags & CF_TYPEMASK) {
1649 if (flags & CF_FORCECHAR) {
1650 AddCodeLine ("ldx #$00");
1651 if (flags & CF_CONST) {
1653 AddCodeLine ("inc %s", lbuf);
1654 AddCodeLine ("lda %s", lbuf);
1656 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1657 AddCodeLine ("clc");
1658 AddCodeLine ("adc %s", lbuf);
1659 AddCodeLine ("sta %s", lbuf);
1662 AddCodeLine ("clc");
1663 AddCodeLine ("adc %s", lbuf);
1664 AddCodeLine ("sta %s", lbuf);
1666 if ((flags & CF_UNSIGNED) == 0) {
1667 unsigned L = GetLocalLabel();
1668 AddCodeLine ("bpl %s", LocalLabelName (L));
1669 AddCodeLine ("dex");
1677 if (flags & CF_CONST) {
1679 unsigned L = GetLocalLabel ();
1680 AddCodeLine ("inc %s", lbuf);
1681 AddCodeLine ("bne %s", LocalLabelName (L));
1682 AddCodeLine ("inc %s+1", lbuf);
1684 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1685 AddCodeLine ("ldx %s+1", lbuf);
1687 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1688 AddCodeLine ("clc");
1689 AddCodeLine ("adc %s", lbuf);
1690 AddCodeLine ("sta %s", lbuf);
1692 unsigned L = GetLocalLabel ();
1693 AddCodeLine ("bcc %s", LocalLabelName (L));
1694 AddCodeLine ("inc %s+1", lbuf);
1696 AddCodeLine ("ldx %s+1", lbuf);
1698 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1699 AddCodeLine ("adc %s+1", lbuf);
1700 AddCodeLine ("sta %s+1", lbuf);
1701 AddCodeLine ("tax");
1702 AddCodeLine ("lda %s", lbuf);
1706 AddCodeLine ("clc");
1707 AddCodeLine ("adc %s", lbuf);
1708 AddCodeLine ("sta %s", lbuf);
1709 AddCodeLine ("txa");
1710 AddCodeLine ("adc %s+1", lbuf);
1711 AddCodeLine ("sta %s+1", lbuf);
1712 AddCodeLine ("tax");
1713 AddCodeLine ("lda %s", lbuf);
1718 if (flags & CF_CONST) {
1720 AddCodeLine ("ldy #<(%s)", lbuf);
1721 AddCodeLine ("sty ptr1");
1722 AddCodeLine ("ldy #>(%s)", lbuf);
1724 AddCodeLine ("jsr laddeq1");
1726 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1727 AddCodeLine ("jsr laddeqa");
1730 g_getstatic (flags, label, offs);
1732 g_putstatic (flags, label, offs);
1735 AddCodeLine ("ldy #<(%s)", lbuf);
1736 AddCodeLine ("sty ptr1");
1737 AddCodeLine ("ldy #>(%s)", lbuf);
1738 AddCodeLine ("jsr laddeq");
1749 void g_addeqlocal (unsigned flags, int Offs, unsigned long val)
1750 /* Emit += for a local variable */
1752 /* Calculate the true offset, check it, load it into Y */
1754 CheckLocalOffs (Offs);
1756 /* Check the size and determine operation */
1757 switch (flags & CF_TYPEMASK) {
1760 if (flags & CF_FORCECHAR) {
1761 AddCodeLine ("ldy #$%02X", Offs);
1762 AddCodeLine ("ldx #$00");
1763 if (flags & CF_CONST) {
1764 AddCodeLine ("clc");
1765 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1766 AddCodeLine ("adc (sp),y");
1767 AddCodeLine ("sta (sp),y");
1769 AddCodeLine ("clc");
1770 AddCodeLine ("adc (sp),y");
1771 AddCodeLine ("sta (sp),y");
1773 if ((flags & CF_UNSIGNED) == 0) {
1774 unsigned L = GetLocalLabel();
1775 AddCodeLine ("bpl %s", LocalLabelName (L));
1776 AddCodeLine ("dex");
1784 AddCodeLine ("ldy #$%02X", Offs);
1785 if (flags & CF_CONST) {
1786 if (IS_Get (&CodeSizeFactor) >= 400) {
1787 AddCodeLine ("clc");
1788 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1789 AddCodeLine ("adc (sp),y");
1790 AddCodeLine ("sta (sp),y");
1791 AddCodeLine ("iny");
1792 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1793 AddCodeLine ("adc (sp),y");
1794 AddCodeLine ("sta (sp),y");
1795 AddCodeLine ("tax");
1796 AddCodeLine ("dey");
1797 AddCodeLine ("lda (sp),y");
1799 g_getimmed (flags, val, 0);
1800 AddCodeLine ("jsr addeqysp");
1803 AddCodeLine ("jsr addeqysp");
1808 if (flags & CF_CONST) {
1809 g_getimmed (flags, val, 0);
1811 AddCodeLine ("ldy #$%02X", Offs);
1812 AddCodeLine ("jsr laddeqysp");
1822 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1823 /* Emit += for the location with address in ax */
1825 /* If the offset is too large for a byte register, add the high byte
1826 ** of the offset to the primary. Beware: We need a special correction
1827 ** if the offset in the low byte will overflow in the operation.
1829 offs = MakeByteOffs (flags, offs);
1831 /* Check the size and determine operation */
1832 switch (flags & CF_TYPEMASK) {
1835 AddCodeLine ("sta ptr1");
1836 AddCodeLine ("stx ptr1+1");
1837 AddCodeLine ("ldy #$%02X", offs);
1838 AddCodeLine ("ldx #$00");
1839 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1840 AddCodeLine ("clc");
1841 AddCodeLine ("adc (ptr1),y");
1842 AddCodeLine ("sta (ptr1),y");
1847 AddCodeLine ("jsr pushax"); /* Push the address */
1848 push (CF_PTR); /* Correct the internal sp */
1849 g_getind (flags, offs); /* Fetch the value */
1850 g_inc (flags, val); /* Increment value in primary */
1851 g_putind (flags, offs); /* Store the value back */
1861 void g_subeqstatic (unsigned flags, uintptr_t label, long offs,
1863 /* Emit -= for a static variable */
1865 /* Create the correct label name */
1866 const char* lbuf = GetLabelName (flags, label, offs);
1868 /* Check the size and determine operation */
1869 switch (flags & CF_TYPEMASK) {
1872 if (flags & CF_FORCECHAR) {
1873 AddCodeLine ("ldx #$00");
1874 if (flags & CF_CONST) {
1876 AddCodeLine ("dec %s", lbuf);
1877 AddCodeLine ("lda %s", lbuf);
1879 AddCodeLine ("lda %s", lbuf);
1880 AddCodeLine ("sec");
1881 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1882 AddCodeLine ("sta %s", lbuf);
1885 AddCodeLine ("eor #$FF");
1886 AddCodeLine ("sec");
1887 AddCodeLine ("adc %s", lbuf);
1888 AddCodeLine ("sta %s", lbuf);
1890 if ((flags & CF_UNSIGNED) == 0) {
1891 unsigned L = GetLocalLabel();
1892 AddCodeLine ("bpl %s", LocalLabelName (L));
1893 AddCodeLine ("dex");
1901 if (flags & CF_CONST) {
1902 AddCodeLine ("lda %s", lbuf);
1903 AddCodeLine ("sec");
1904 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1905 AddCodeLine ("sta %s", lbuf);
1907 unsigned L = GetLocalLabel ();
1908 AddCodeLine ("bcs %s", LocalLabelName (L));
1909 AddCodeLine ("dec %s+1", lbuf);
1911 AddCodeLine ("ldx %s+1", lbuf);
1913 AddCodeLine ("lda %s+1", lbuf);
1914 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1915 AddCodeLine ("sta %s+1", lbuf);
1916 AddCodeLine ("tax");
1917 AddCodeLine ("lda %s", lbuf);
1920 AddCodeLine ("eor #$FF");
1921 AddCodeLine ("sec");
1922 AddCodeLine ("adc %s", lbuf);
1923 AddCodeLine ("sta %s", lbuf);
1924 AddCodeLine ("txa");
1925 AddCodeLine ("eor #$FF");
1926 AddCodeLine ("adc %s+1", lbuf);
1927 AddCodeLine ("sta %s+1", lbuf);
1928 AddCodeLine ("tax");
1929 AddCodeLine ("lda %s", lbuf);
1934 if (flags & CF_CONST) {
1936 AddCodeLine ("ldy #<(%s)", lbuf);
1937 AddCodeLine ("sty ptr1");
1938 AddCodeLine ("ldy #>(%s)", lbuf);
1939 AddCodeLine ("lda #$%02X", (unsigned char)val);
1940 AddCodeLine ("jsr lsubeqa");
1942 g_getstatic (flags, label, offs);
1944 g_putstatic (flags, label, offs);
1947 AddCodeLine ("ldy #<(%s)", lbuf);
1948 AddCodeLine ("sty ptr1");
1949 AddCodeLine ("ldy #>(%s)", lbuf);
1950 AddCodeLine ("jsr lsubeq");
1961 void g_subeqlocal (unsigned flags, int Offs, unsigned long val)
1962 /* Emit -= for a local variable */
1964 /* Calculate the true offset, check it, load it into Y */
1966 CheckLocalOffs (Offs);
1968 /* Check the size and determine operation */
1969 switch (flags & CF_TYPEMASK) {
1972 if (flags & CF_FORCECHAR) {
1973 AddCodeLine ("ldy #$%02X", Offs);
1974 AddCodeLine ("ldx #$00");
1975 if (flags & CF_CONST) {
1976 AddCodeLine ("lda (sp),y");
1977 AddCodeLine ("sec");
1978 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1980 AddCodeLine ("eor #$FF");
1981 AddCodeLine ("sec");
1982 AddCodeLine ("adc (sp),y");
1984 AddCodeLine ("sta (sp),y");
1985 if ((flags & CF_UNSIGNED) == 0) {
1986 unsigned L = GetLocalLabel();
1987 AddCodeLine ("bpl %s", LocalLabelName (L));
1988 AddCodeLine ("dex");
1996 if (flags & CF_CONST) {
1997 g_getimmed (flags, val, 0);
1999 AddCodeLine ("ldy #$%02X", Offs);
2000 AddCodeLine ("jsr subeqysp");
2004 if (flags & CF_CONST) {
2005 g_getimmed (flags, val, 0);
2007 AddCodeLine ("ldy #$%02X", Offs);
2008 AddCodeLine ("jsr lsubeqysp");
2018 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2019 /* Emit -= for the location with address in ax */
2021 /* If the offset is too large for a byte register, add the high byte
2022 ** of the offset to the primary. Beware: We need a special correction
2023 ** if the offset in the low byte will overflow in the operation.
2025 offs = MakeByteOffs (flags, offs);
2027 /* Check the size and determine operation */
2028 switch (flags & CF_TYPEMASK) {
2031 AddCodeLine ("sta ptr1");
2032 AddCodeLine ("stx ptr1+1");
2033 AddCodeLine ("ldy #$%02X", offs);
2034 AddCodeLine ("ldx #$00");
2035 AddCodeLine ("lda (ptr1),y");
2036 AddCodeLine ("sec");
2037 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2038 AddCodeLine ("sta (ptr1),y");
2043 AddCodeLine ("jsr pushax"); /* Push the address */
2044 push (CF_PTR); /* Correct the internal sp */
2045 g_getind (flags, offs); /* Fetch the value */
2046 g_dec (flags, val); /* Increment value in primary */
2047 g_putind (flags, offs); /* Store the value back */
2057 /*****************************************************************************/
2058 /* Add a variable address to the value in ax */
2059 /*****************************************************************************/
2063 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2064 /* Add the address of a local variable to ax */
2068 /* Add the offset */
2071 /* We cannot address more then 256 bytes of locals anyway */
2072 L = GetLocalLabel();
2073 CheckLocalOffs (offs);
2074 AddCodeLine ("clc");
2075 AddCodeLine ("adc #$%02X", offs & 0xFF);
2076 /* Do also skip the CLC insn below */
2077 AddCodeLine ("bcc %s", LocalLabelName (L));
2078 AddCodeLine ("inx");
2081 /* Add the current stackpointer value */
2082 AddCodeLine ("clc");
2084 /* Label was used above */
2087 AddCodeLine ("adc sp");
2088 AddCodeLine ("tay");
2089 AddCodeLine ("txa");
2090 AddCodeLine ("adc sp+1");
2091 AddCodeLine ("tax");
2092 AddCodeLine ("tya");
2097 void g_addaddr_static (unsigned flags, uintptr_t label, long offs)
2098 /* Add the address of a static variable to ax */
2100 /* Create the correct label name */
2101 const char* lbuf = GetLabelName (flags, label, offs);
2103 /* Add the address to the current ax value */
2104 AddCodeLine ("clc");
2105 AddCodeLine ("adc #<(%s)", lbuf);
2106 AddCodeLine ("tay");
2107 AddCodeLine ("txa");
2108 AddCodeLine ("adc #>(%s)", lbuf);
2109 AddCodeLine ("tax");
2110 AddCodeLine ("tya");
2115 /*****************************************************************************/
2117 /*****************************************************************************/
2121 void g_save (unsigned flags)
2122 /* Copy primary register to hold register. */
2124 /* Check the size and determine operation */
2125 switch (flags & CF_TYPEMASK) {
2128 if (flags & CF_FORCECHAR) {
2129 AddCodeLine ("pha");
2135 AddCodeLine ("sta regsave");
2136 AddCodeLine ("stx regsave+1");
2140 AddCodeLine ("jsr saveeax");
2150 void g_restore (unsigned flags)
2151 /* Copy hold register to primary. */
2153 /* Check the size and determine operation */
2154 switch (flags & CF_TYPEMASK) {
2157 if (flags & CF_FORCECHAR) {
2158 AddCodeLine ("pla");
2164 AddCodeLine ("lda regsave");
2165 AddCodeLine ("ldx regsave+1");
2169 AddCodeLine ("jsr resteax");
2179 void g_cmp (unsigned flags, unsigned long val)
2180 /* Immidiate compare. The primary register will not be changed, Z flag
2186 /* Check the size and determine operation */
2187 switch (flags & CF_TYPEMASK) {
2190 if (flags & CF_FORCECHAR) {
2191 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2197 L = GetLocalLabel();
2198 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2199 AddCodeLine ("bne %s", LocalLabelName (L));
2200 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2205 Internal ("g_cmp: Long compares not implemented");
2215 static void oper (unsigned Flags, unsigned long Val, const char* const* Subs)
2216 /* Encode a binary operation. subs is a pointer to four strings:
2217 ** 0 --> Operate on ints
2218 ** 1 --> Operate on unsigneds
2219 ** 2 --> Operate on longs
2220 ** 3 --> Operate on unsigned longs
2223 /* Determine the offset into the array */
2224 if (Flags & CF_UNSIGNED) {
2227 if ((Flags & CF_TYPEMASK) == CF_LONG) {
2231 /* Load the value if it is not already in the primary */
2232 if (Flags & CF_CONST) {
2234 g_getimmed (Flags, Val, 0);
2237 /* Output the operation */
2238 AddCodeLine ("jsr %s", *Subs);
2240 /* The operation will pop it's argument */
2246 void g_test (unsigned flags)
2247 /* Test the value in the primary and set the condition codes */
2249 switch (flags & CF_TYPEMASK) {
2252 if (flags & CF_FORCECHAR) {
2253 AddCodeLine ("tax");
2259 AddCodeLine ("stx tmp1");
2260 AddCodeLine ("ora tmp1");
2264 if (flags & CF_UNSIGNED) {
2265 AddCodeLine ("jsr utsteax");
2267 AddCodeLine ("jsr tsteax");
2279 void g_push (unsigned flags, unsigned long val)
2280 /* Push the primary register or a constant value onto the stack */
2282 if (flags & CF_CONST && (flags & CF_TYPEMASK) != CF_LONG) {
2284 /* We have a constant 8 or 16 bit value */
2285 if ((flags & CF_TYPEMASK) == CF_CHAR && (flags & CF_FORCECHAR)) {
2287 /* Handle as 8 bit value */
2288 AddCodeLine ("lda #$%02X", (unsigned char) val);
2289 AddCodeLine ("jsr pusha");
2293 /* Handle as 16 bit value */
2294 g_getimmed (flags, val, 0);
2295 AddCodeLine ("jsr pushax");
2300 /* Value is not 16 bit or not constant */
2301 if (flags & CF_CONST) {
2302 /* Constant 32 bit value, load into eax */
2303 g_getimmed (flags, val, 0);
2306 /* Push the primary register */
2307 switch (flags & CF_TYPEMASK) {
2310 if (flags & CF_FORCECHAR) {
2311 /* Handle as char */
2312 AddCodeLine ("jsr pusha");
2317 AddCodeLine ("jsr pushax");
2321 AddCodeLine ("jsr pusheax");
2331 /* Adjust the stack offset */
2337 void g_swap (unsigned flags)
2338 /* Swap the primary register and the top of the stack. flags give the type
2339 ** of *both* values (must have same size).
2342 switch (flags & CF_TYPEMASK) {
2346 AddCodeLine ("jsr swapstk");
2350 AddCodeLine ("jsr swapestk");
2361 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2362 /* Call the specified subroutine name */
2364 if ((Flags & CF_FIXARGC) == 0) {
2365 /* Pass the argument count */
2366 AddCodeLine ("ldy #$%02X", ArgSize);
2368 AddCodeLine ("jsr _%s", Label);
2369 StackPtr += ArgSize; /* callee pops args */
2374 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2375 /* Call subroutine indirect */
2377 if ((Flags & CF_LOCAL) == 0) {
2378 /* Address is in a/x */
2379 if ((Flags & CF_FIXARGC) == 0) {
2380 /* Pass arg count */
2381 AddCodeLine ("ldy #$%02X", ArgSize);
2383 AddCodeLine ("jsr callax");
2385 /* The address is on stack, offset is on Val */
2387 CheckLocalOffs (Offs);
2388 AddCodeLine ("pha");
2389 AddCodeLine ("ldy #$%02X", Offs);
2390 AddCodeLine ("lda (sp),y");
2391 AddCodeLine ("sta jmpvec+1");
2392 AddCodeLine ("iny");
2393 AddCodeLine ("lda (sp),y");
2394 AddCodeLine ("sta jmpvec+2");
2395 AddCodeLine ("pla");
2396 AddCodeLine ("jsr jmpvec");
2399 /* Callee pops args */
2400 StackPtr += ArgSize;
2405 void g_jump (unsigned Label)
2406 /* Jump to specified internal label number */
2408 AddCodeLine ("jmp %s", LocalLabelName (Label));
2413 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2414 /* Jump to label if zero flag clear */
2416 AddCodeLine ("jne %s", LocalLabelName (label));
2421 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2422 /* Jump to label if zero flag set */
2424 AddCodeLine ("jeq %s", LocalLabelName (label));
2428 void g_lateadjustSP (unsigned label)
2429 /* Adjust stack based on non-immediate data */
2431 AddCodeLine ("pha");
2432 AddCodeLine ("lda %s", LocalLabelName (label));
2433 AddCodeLine ("clc");
2434 AddCodeLine ("adc sp");
2435 AddCodeLine ("sta sp");
2436 AddCodeLine ("lda %s+1", LocalLabelName (label));
2437 AddCodeLine ("adc sp+1");
2438 AddCodeLine ("sta sp+1");
2439 AddCodeLine ("pla");
2442 void g_drop (unsigned Space)
2443 /* Drop space allocated on the stack */
2446 /* Inline the code since calling addysp repeatedly is quite some
2449 AddCodeLine ("pha");
2450 AddCodeLine ("lda #$%02X", (unsigned char) Space);
2451 AddCodeLine ("clc");
2452 AddCodeLine ("adc sp");
2453 AddCodeLine ("sta sp");
2454 AddCodeLine ("lda #$%02X", (unsigned char) (Space >> 8));
2455 AddCodeLine ("adc sp+1");
2456 AddCodeLine ("sta sp+1");
2457 AddCodeLine ("pla");
2458 } else if (Space > 8) {
2459 AddCodeLine ("ldy #$%02X", Space);
2460 AddCodeLine ("jsr addysp");
2461 } else if (Space != 0) {
2462 AddCodeLine ("jsr incsp%u", Space);
2468 void g_space (int Space)
2469 /* Create or drop space on the stack */
2472 /* This is actually a drop operation */
2474 } else if (Space > 255) {
2475 /* Inline the code since calling subysp repeatedly is quite some
2478 AddCodeLine ("pha");
2479 AddCodeLine ("lda sp");
2480 AddCodeLine ("sec");
2481 AddCodeLine ("sbc #$%02X", (unsigned char) Space);
2482 AddCodeLine ("sta sp");
2483 AddCodeLine ("lda sp+1");
2484 AddCodeLine ("sbc #$%02X", (unsigned char) (Space >> 8));
2485 AddCodeLine ("sta sp+1");
2486 AddCodeLine ("pla");
2487 } else if (Space > 8) {
2488 AddCodeLine ("ldy #$%02X", Space);
2489 AddCodeLine ("jsr subysp");
2490 } else if (Space != 0) {
2491 AddCodeLine ("jsr decsp%u", Space);
2497 void g_cstackcheck (void)
2498 /* Check for a C stack overflow */
2500 AddCodeLine ("jsr cstkchk");
2505 void g_stackcheck (void)
2506 /* Check for a stack overflow */
2508 AddCodeLine ("jsr stkchk");
2513 void g_add (unsigned flags, unsigned long val)
2514 /* Primary = TOS + Primary */
2516 static const char* const ops[4] = {
2517 "tosaddax", "tosaddax", "tosaddeax", "tosaddeax"
2520 if (flags & CF_CONST) {
2521 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2522 g_push (flags & ~CF_CONST, 0);
2524 oper (flags, val, ops);
2529 void g_sub (unsigned flags, unsigned long val)
2530 /* Primary = TOS - Primary */
2532 static const char* const ops[4] = {
2533 "tossubax", "tossubax", "tossubeax", "tossubeax"
2536 if (flags & CF_CONST) {
2537 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2538 g_push (flags & ~CF_CONST, 0);
2540 oper (flags, val, ops);
2545 void g_rsub (unsigned flags, unsigned long val)
2546 /* Primary = Primary - TOS */
2548 static const char* const ops[4] = {
2549 "tosrsubax", "tosrsubax", "tosrsubeax", "tosrsubeax"
2551 oper (flags, val, ops);
2556 void g_mul (unsigned flags, unsigned long val)
2557 /* Primary = TOS * Primary */
2559 static const char* const ops[4] = {
2560 "tosmulax", "tosumulax", "tosmuleax", "tosumuleax"
2565 /* Do strength reduction if the value is constant and a power of two */
2566 if (flags & CF_CONST && (p2 = PowerOf2 (val)) >= 0) {
2567 /* Generate a shift instead */
2572 /* If the right hand side is const, the lhs is not on stack but still
2573 ** in the primary register.
2575 if (flags & CF_CONST) {
2577 switch (flags & CF_TYPEMASK) {
2580 if (flags & CF_FORCECHAR) {
2581 /* Handle some special cases */
2585 AddCodeLine ("sta tmp1");
2586 AddCodeLine ("asl a");
2587 AddCodeLine ("clc");
2588 AddCodeLine ("adc tmp1");
2592 AddCodeLine ("sta tmp1");
2593 AddCodeLine ("asl a");
2594 AddCodeLine ("asl a");
2595 AddCodeLine ("clc");
2596 AddCodeLine ("adc tmp1");
2600 AddCodeLine ("sta tmp1");
2601 AddCodeLine ("asl a");
2602 AddCodeLine ("clc");
2603 AddCodeLine ("adc tmp1");
2604 AddCodeLine ("asl a");
2608 AddCodeLine ("sta tmp1");
2609 AddCodeLine ("asl a");
2610 AddCodeLine ("asl a");
2611 AddCodeLine ("clc");
2612 AddCodeLine ("adc tmp1");
2613 AddCodeLine ("asl a");
2622 AddCodeLine ("jsr mulax3");
2625 AddCodeLine ("jsr mulax5");
2628 AddCodeLine ("jsr mulax6");
2631 AddCodeLine ("jsr mulax7");
2634 AddCodeLine ("jsr mulax9");
2637 AddCodeLine ("jsr mulax10");
2649 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2650 ** into the normal, non-optimized stuff.
2652 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2653 g_push (flags & ~CF_CONST, 0);
2657 /* Use long way over the stack */
2658 oper (flags, val, ops);
2663 void g_div (unsigned flags, unsigned long val)
2664 /* Primary = TOS / Primary */
2666 static const char* const ops[4] = {
2667 "tosdivax", "tosudivax", "tosdiveax", "tosudiveax"
2670 /* Do strength reduction if the value is constant and a power of two */
2672 if ((flags & CF_CONST) && (p2 = PowerOf2 (val)) >= 0) {
2673 /* Generate a shift instead */
2676 /* Generate a division */
2677 if (flags & CF_CONST) {
2678 /* lhs is not on stack */
2679 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2680 g_push (flags & ~CF_CONST, 0);
2682 oper (flags, val, ops);
2688 void g_mod (unsigned flags, unsigned long val)
2689 /* Primary = TOS % Primary */
2691 static const char* const ops[4] = {
2692 "tosmodax", "tosumodax", "tosmodeax", "tosumodeax"
2696 /* Check if we can do some cost reduction */
2697 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = PowerOf2 (val)) >= 0) {
2698 /* We can do that with an AND operation */
2699 g_and (flags, val - 1);
2701 /* Do it the hard way... */
2702 if (flags & CF_CONST) {
2703 /* lhs is not on stack */
2704 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2705 g_push (flags & ~CF_CONST, 0);
2707 oper (flags, val, ops);
2713 void g_or (unsigned flags, unsigned long val)
2714 /* Primary = TOS | Primary */
2716 static const char* const ops[4] = {
2717 "tosorax", "tosorax", "tosoreax", "tosoreax"
2720 /* If the right hand side is const, the lhs is not on stack but still
2721 ** in the primary register.
2723 if (flags & CF_CONST) {
2725 switch (flags & CF_TYPEMASK) {
2728 if (flags & CF_FORCECHAR) {
2729 if ((val & 0xFF) != 0) {
2730 AddCodeLine ("ora #$%02X", (unsigned char)val);
2738 if ((val & 0xFF) != 0) {
2739 AddCodeLine ("ora #$%02X", (unsigned char)val);
2741 } else if ((val & 0xFF00) == 0xFF00) {
2742 if ((val & 0xFF) != 0) {
2743 AddCodeLine ("ora #$%02X", (unsigned char)val);
2745 AddCodeLine ("ldx #$FF");
2746 } else if (val != 0) {
2747 AddCodeLine ("ora #$%02X", (unsigned char)val);
2748 AddCodeLine ("pha");
2749 AddCodeLine ("txa");
2750 AddCodeLine ("ora #$%02X", (unsigned char)(val >> 8));
2751 AddCodeLine ("tax");
2752 AddCodeLine ("pla");
2758 if ((val & 0xFF) != 0) {
2759 AddCodeLine ("ora #$%02X", (unsigned char)val);
2769 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2770 ** into the normal, non-optimized stuff. Note: The standard stuff will
2771 ** always work with ints.
2773 flags &= ~CF_FORCECHAR;
2774 g_push (flags & ~CF_CONST, 0);
2777 /* Use long way over the stack */
2778 oper (flags, val, ops);
2783 void g_xor (unsigned flags, unsigned long val)
2784 /* Primary = TOS ^ Primary */
2786 static const char* const ops[4] = {
2787 "tosxorax", "tosxorax", "tosxoreax", "tosxoreax"
2791 /* If the right hand side is const, the lhs is not on stack but still
2792 ** in the primary register.
2794 if (flags & CF_CONST) {
2796 switch (flags & CF_TYPEMASK) {
2799 if (flags & CF_FORCECHAR) {
2800 if ((val & 0xFF) != 0) {
2801 AddCodeLine ("eor #$%02X", (unsigned char)val);
2810 AddCodeLine ("eor #$%02X", (unsigned char)val);
2812 } else if (val != 0) {
2813 if ((val & 0xFF) != 0) {
2814 AddCodeLine ("eor #$%02X", (unsigned char)val);
2816 AddCodeLine ("pha");
2817 AddCodeLine ("txa");
2818 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2819 AddCodeLine ("tax");
2820 AddCodeLine ("pla");
2827 AddCodeLine ("eor #$%02X", (unsigned char)val);
2837 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2838 ** into the normal, non-optimized stuff. Note: The standard stuff will
2839 ** always work with ints.
2841 flags &= ~CF_FORCECHAR;
2842 g_push (flags & ~CF_CONST, 0);
2845 /* Use long way over the stack */
2846 oper (flags, val, ops);
2851 void g_and (unsigned Flags, unsigned long Val)
2852 /* Primary = TOS & Primary */
2854 static const char* const ops[4] = {
2855 "tosandax", "tosandax", "tosandeax", "tosandeax"
2858 /* If the right hand side is const, the lhs is not on stack but still
2859 ** in the primary register.
2861 if (Flags & CF_CONST) {
2863 switch (Flags & CF_TYPEMASK) {
2866 if (Flags & CF_FORCECHAR) {
2867 if ((Val & 0xFF) == 0x00) {
2868 AddCodeLine ("lda #$00");
2869 } else if ((Val & 0xFF) != 0xFF) {
2870 AddCodeLine ("and #$%02X", (unsigned char)Val);
2876 if ((Val & 0xFFFF) != 0xFFFF) {
2878 AddCodeLine ("ldx #$00");
2880 AddCodeLine ("lda #$00");
2881 } else if (Val != 0xFF) {
2882 AddCodeLine ("and #$%02X", (unsigned char)Val);
2884 } else if ((Val & 0xFFFF) == 0xFF00) {
2885 AddCodeLine ("lda #$00");
2886 } else if ((Val & 0xFF00) == 0xFF00) {
2887 AddCodeLine ("and #$%02X", (unsigned char)Val);
2888 } else if ((Val & 0x00FF) == 0x0000) {
2889 AddCodeLine ("txa");
2890 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2891 AddCodeLine ("tax");
2892 AddCodeLine ("lda #$00");
2894 AddCodeLine ("tay");
2895 AddCodeLine ("txa");
2896 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2897 AddCodeLine ("tax");
2898 AddCodeLine ("tya");
2899 if ((Val & 0x00FF) == 0x0000) {
2900 AddCodeLine ("lda #$00");
2901 } else if ((Val & 0x00FF) != 0x00FF) {
2902 AddCodeLine ("and #$%02X", (unsigned char)Val);
2910 AddCodeLine ("ldx #$00");
2911 AddCodeLine ("stx sreg+1");
2912 AddCodeLine ("stx sreg");
2913 if ((Val & 0xFF) != 0xFF) {
2914 AddCodeLine ("and #$%02X", (unsigned char)Val);
2917 } else if (Val == 0xFF00) {
2918 AddCodeLine ("lda #$00");
2919 AddCodeLine ("sta sreg+1");
2920 AddCodeLine ("sta sreg");
2929 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2930 ** into the normal, non-optimized stuff. Note: The standard stuff will
2931 ** always work with ints.
2933 Flags &= ~CF_FORCECHAR;
2934 g_push (Flags & ~CF_CONST, 0);
2937 /* Use long way over the stack */
2938 oper (Flags, Val, ops);
2943 void g_asr (unsigned flags, unsigned long val)
2944 /* Primary = TOS >> Primary */
2946 static const char* const ops[4] = {
2947 "tosasrax", "tosshrax", "tosasreax", "tosshreax"
2950 /* If the right hand side is const, the lhs is not on stack but still
2951 ** in the primary register.
2953 if (flags & CF_CONST) {
2955 switch (flags & CF_TYPEMASK) {
2961 if (flags & CF_UNSIGNED) {
2962 AddCodeLine ("txa");
2963 AddCodeLine ("ldx #$00");
2965 unsigned L = GetLocalLabel();
2966 AddCodeLine ("cpx #$80"); /* Sign bit into carry */
2967 AddCodeLine ("txa");
2968 AddCodeLine ("ldx #$00");
2969 AddCodeLine ("bcc %s", LocalLabelName (L));
2970 AddCodeLine ("dex"); /* Make $FF */
2976 if (flags & CF_UNSIGNED) {
2977 AddCodeLine ("jsr shrax4");
2979 AddCodeLine ("jsr asrax4");
2984 if (flags & CF_UNSIGNED) {
2985 AddCodeLine ("jsr shrax%ld", val);
2987 AddCodeLine ("jsr asrax%ld", val);
2995 AddCodeLine ("ldx #$00");
2996 AddCodeLine ("lda sreg+1");
2997 if ((flags & CF_UNSIGNED) == 0) {
2998 unsigned L = GetLocalLabel();
2999 AddCodeLine ("bpl %s", LocalLabelName (L));
3000 AddCodeLine ("dex");
3003 AddCodeLine ("stx sreg");
3004 AddCodeLine ("stx sreg+1");
3008 AddCodeLine ("ldy #$00");
3009 AddCodeLine ("ldx sreg+1");
3010 if ((flags & CF_UNSIGNED) == 0) {
3011 unsigned L = GetLocalLabel();
3012 AddCodeLine ("bpl %s", LocalLabelName (L));
3013 AddCodeLine ("dey");
3016 AddCodeLine ("lda sreg");
3017 AddCodeLine ("sty sreg+1");
3018 AddCodeLine ("sty sreg");
3022 AddCodeLine ("txa");
3023 AddCodeLine ("ldx sreg");
3024 AddCodeLine ("ldy sreg+1");
3025 AddCodeLine ("sty sreg");
3026 if ((flags & CF_UNSIGNED) == 0) {
3027 unsigned L = GetLocalLabel();
3028 AddCodeLine ("cpy #$80");
3029 AddCodeLine ("ldy #$00");
3030 AddCodeLine ("bcc %s", LocalLabelName (L));
3031 AddCodeLine ("dey");
3034 AddCodeLine ("ldy #$00");
3036 AddCodeLine ("sty sreg+1");
3040 if (flags & CF_UNSIGNED) {
3041 AddCodeLine ("jsr shreax4");
3043 AddCodeLine ("jsr asreax4");
3048 if (flags & CF_UNSIGNED) {
3049 AddCodeLine ("jsr shreax%ld", val);
3051 AddCodeLine ("jsr asreax%ld", val);
3060 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3061 ** into the normal, non-optimized stuff. Note: The standard stuff will
3062 ** always work with ints.
3064 flags &= ~CF_FORCECHAR;
3065 g_push (flags & ~CF_CONST, 0);
3068 /* Use long way over the stack */
3069 oper (flags, val, ops);
3074 void g_asl (unsigned flags, unsigned long val)
3075 /* Primary = TOS << Primary */
3077 static const char* const ops[4] = {
3078 "tosaslax", "tosshlax", "tosasleax", "tosshleax"
3082 /* If the right hand side is const, the lhs is not on stack but still
3083 ** in the primary register.
3085 if (flags & CF_CONST) {
3087 switch (flags & CF_TYPEMASK) {
3093 AddCodeLine ("tax");
3094 AddCodeLine ("lda #$00");
3098 if (flags & CF_UNSIGNED) {
3099 AddCodeLine ("jsr shlax4");
3101 AddCodeLine ("jsr aslax4");
3106 if (flags & CF_UNSIGNED) {
3107 AddCodeLine ("jsr shlax%ld", val);
3109 AddCodeLine ("jsr aslax%ld", val);
3117 AddCodeLine ("sta sreg+1");
3118 AddCodeLine ("lda #$00");
3119 AddCodeLine ("tax");
3120 AddCodeLine ("sta sreg");
3124 AddCodeLine ("stx sreg+1");
3125 AddCodeLine ("sta sreg");
3126 AddCodeLine ("lda #$00");
3127 AddCodeLine ("tax");
3131 AddCodeLine ("ldy sreg");
3132 AddCodeLine ("sty sreg+1");
3133 AddCodeLine ("stx sreg");
3134 AddCodeLine ("tax");
3135 AddCodeLine ("lda #$00");
3139 if (flags & CF_UNSIGNED) {
3140 AddCodeLine ("jsr shleax4");
3142 AddCodeLine ("jsr asleax4");
3147 if (flags & CF_UNSIGNED) {
3148 AddCodeLine ("jsr shleax%ld", val);
3150 AddCodeLine ("jsr asleax%ld", val);
3159 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3160 ** into the normal, non-optimized stuff. Note: The standard stuff will
3161 ** always work with ints.
3163 flags &= ~CF_FORCECHAR;
3164 g_push (flags & ~CF_CONST, 0);
3167 /* Use long way over the stack */
3168 oper (flags, val, ops);
3173 void g_neg (unsigned Flags)
3174 /* Primary = -Primary */
3176 switch (Flags & CF_TYPEMASK) {
3179 if (Flags & CF_FORCECHAR) {
3180 AddCodeLine ("eor #$FF");
3181 AddCodeLine ("clc");
3182 AddCodeLine ("adc #$01");
3188 AddCodeLine ("jsr negax");
3192 AddCodeLine ("jsr negeax");
3202 void g_bneg (unsigned flags)
3203 /* Primary = !Primary */
3205 switch (flags & CF_TYPEMASK) {
3208 AddCodeLine ("jsr bnega");
3212 AddCodeLine ("jsr bnegax");
3216 AddCodeLine ("jsr bnegeax");
3226 void g_com (unsigned Flags)
3227 /* Primary = ~Primary */
3229 switch (Flags & CF_TYPEMASK) {
3232 if (Flags & CF_FORCECHAR) {
3233 AddCodeLine ("eor #$FF");
3239 AddCodeLine ("jsr complax");
3243 AddCodeLine ("jsr compleax");
3253 void g_inc (unsigned flags, unsigned long val)
3254 /* Increment the primary register by a given number */
3256 /* Don't inc by zero */
3261 /* Generate code for the supported types */
3263 switch (flags & CF_TYPEMASK) {
3266 if (flags & CF_FORCECHAR) {
3267 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3269 AddCodeLine ("ina");
3272 AddCodeLine ("clc");
3273 AddCodeLine ("adc #$%02X", (unsigned char)val);
3280 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) {
3281 unsigned L = GetLocalLabel();
3282 AddCodeLine ("ina");
3283 AddCodeLine ("bne %s", LocalLabelName (L));
3284 AddCodeLine ("inx");
3286 } else if (IS_Get (&CodeSizeFactor) < 200) {
3289 AddCodeLine ("jsr incax%lu", val);
3290 } else if (val <= 255) {
3291 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3292 AddCodeLine ("jsr incaxy");
3294 g_add (flags | CF_CONST, val);
3297 /* Inline the code */
3299 if ((val & 0xFF) != 0) {
3300 unsigned L = GetLocalLabel();
3301 AddCodeLine ("clc");
3302 AddCodeLine ("adc #$%02X", (unsigned char) val);
3303 AddCodeLine ("bcc %s", LocalLabelName (L));
3304 AddCodeLine ("inx");
3308 AddCodeLine ("inx");
3311 AddCodeLine ("inx");
3314 AddCodeLine ("inx");
3316 } else if ((val & 0xFF) != 0) {
3317 AddCodeLine ("clc");
3318 AddCodeLine ("adc #$%02X", (unsigned char) val);
3319 AddCodeLine ("pha");
3320 AddCodeLine ("txa");
3321 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3322 AddCodeLine ("tax");
3323 AddCodeLine ("pla");
3325 AddCodeLine ("pha");
3326 AddCodeLine ("txa");
3327 AddCodeLine ("clc");
3328 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3329 AddCodeLine ("tax");
3330 AddCodeLine ("pla");
3337 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3338 AddCodeLine ("jsr inceaxy");
3340 g_add (flags | CF_CONST, val);
3352 void g_dec (unsigned flags, unsigned long val)
3353 /* Decrement the primary register by a given number */
3355 /* Don't dec by zero */
3360 /* Generate code for the supported types */
3362 switch (flags & CF_TYPEMASK) {
3365 if (flags & CF_FORCECHAR) {
3366 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3368 AddCodeLine ("dea");
3371 AddCodeLine ("sec");
3372 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3379 if (IS_Get (&CodeSizeFactor) < 200) {
3380 /* Use subroutines */
3382 AddCodeLine ("jsr decax%d", (int) val);
3383 } else if (val <= 255) {
3384 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3385 AddCodeLine ("jsr decaxy");
3387 g_sub (flags | CF_CONST, val);
3390 /* Inline the code */
3392 if ((val & 0xFF) != 0) {
3393 unsigned L = GetLocalLabel();
3394 AddCodeLine ("sec");
3395 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3396 AddCodeLine ("bcs %s", LocalLabelName (L));
3397 AddCodeLine ("dex");
3401 AddCodeLine ("dex");
3404 AddCodeLine ("dex");
3407 if ((val & 0xFF) != 0) {
3408 AddCodeLine ("sec");
3409 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3410 AddCodeLine ("pha");
3411 AddCodeLine ("txa");
3412 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3413 AddCodeLine ("tax");
3414 AddCodeLine ("pla");
3416 AddCodeLine ("pha");
3417 AddCodeLine ("txa");
3418 AddCodeLine ("sec");
3419 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3420 AddCodeLine ("tax");
3421 AddCodeLine ("pla");
3429 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3430 AddCodeLine ("jsr deceaxy");
3432 g_sub (flags | CF_CONST, val);
3445 ** Following are the conditional operators. They compare the TOS against
3446 ** the primary and put a literal 1 in the primary if the condition is
3447 ** true, otherwise they clear the primary register
3452 void g_eq (unsigned flags, unsigned long val)
3453 /* Test for equal */
3455 static const char* const ops[4] = {
3456 "toseqax", "toseqax", "toseqeax", "toseqeax"
3461 /* If the right hand side is const, the lhs is not on stack but still
3462 ** in the primary register.
3464 if (flags & CF_CONST) {
3466 switch (flags & CF_TYPEMASK) {
3469 if (flags & CF_FORCECHAR) {
3470 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3471 AddCodeLine ("jsr booleq");
3477 L = GetLocalLabel();
3478 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3479 AddCodeLine ("bne %s", LocalLabelName (L));
3480 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3482 AddCodeLine ("jsr booleq");
3492 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3493 ** into the normal, non-optimized stuff. Note: The standard stuff will
3494 ** always work with ints.
3496 flags &= ~CF_FORCECHAR;
3497 g_push (flags & ~CF_CONST, 0);
3500 /* Use long way over the stack */
3501 oper (flags, val, ops);
3506 void g_ne (unsigned flags, unsigned long val)
3507 /* Test for not equal */
3509 static const char* const ops[4] = {
3510 "tosneax", "tosneax", "tosneeax", "tosneeax"
3515 /* If the right hand side is const, the lhs is not on stack but still
3516 ** in the primary register.
3518 if (flags & CF_CONST) {
3520 switch (flags & CF_TYPEMASK) {
3523 if (flags & CF_FORCECHAR) {
3524 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3525 AddCodeLine ("jsr boolne");
3531 L = GetLocalLabel();
3532 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3533 AddCodeLine ("bne %s", LocalLabelName (L));
3534 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3536 AddCodeLine ("jsr boolne");
3546 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3547 ** into the normal, non-optimized stuff. Note: The standard stuff will
3548 ** always work with ints.
3550 flags &= ~CF_FORCECHAR;
3551 g_push (flags & ~CF_CONST, 0);
3554 /* Use long way over the stack */
3555 oper (flags, val, ops);
3560 void g_lt (unsigned flags, unsigned long val)
3561 /* Test for less than */
3563 static const char* const ops[4] = {
3564 "tosltax", "tosultax", "toslteax", "tosulteax"
3569 /* If the right hand side is const, the lhs is not on stack but still
3570 ** in the primary register.
3572 if (flags & CF_CONST) {
3574 /* Because the handling of the overflow flag is too complex for
3575 ** inlining, we can handle only unsigned compares, and signed
3576 ** compares against zero here.
3578 if (flags & CF_UNSIGNED) {
3580 /* Give a warning in some special cases */
3582 Warning ("Condition is never true");
3583 AddCodeLine ("jsr return0");
3587 /* Look at the type */
3588 switch (flags & CF_TYPEMASK) {
3591 if (flags & CF_FORCECHAR) {
3592 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3593 AddCodeLine ("jsr boolult");
3599 /* If the low byte is zero, we must only test the high byte */
3600 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3601 if ((val & 0xFF) != 0) {
3602 unsigned L = GetLocalLabel();
3603 AddCodeLine ("bne %s", LocalLabelName (L));
3604 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3607 AddCodeLine ("jsr boolult");
3611 /* Do a subtraction */
3612 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3613 AddCodeLine ("txa");
3614 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3615 AddCodeLine ("lda sreg");
3616 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3617 AddCodeLine ("lda sreg+1");
3618 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3619 AddCodeLine ("jsr boolult");
3626 } else if (val == 0) {
3628 /* A signed compare against zero must only look at the sign bit */
3629 switch (flags & CF_TYPEMASK) {
3632 if (flags & CF_FORCECHAR) {
3633 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3634 AddCodeLine ("lda #$00");
3635 AddCodeLine ("ldx #$00");
3636 AddCodeLine ("rol a");
3642 /* Just check the high byte */
3643 AddCodeLine ("cpx #$80"); /* Bit 7 -> carry */
3644 AddCodeLine ("lda #$00");
3645 AddCodeLine ("ldx #$00");
3646 AddCodeLine ("rol a");
3650 /* Just check the high byte */
3651 AddCodeLine ("lda sreg+1");
3652 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3653 AddCodeLine ("lda #$00");
3654 AddCodeLine ("ldx #$00");
3655 AddCodeLine ("rol a");
3664 /* Signed compare against a constant != zero */
3665 switch (flags & CF_TYPEMASK) {
3668 if (flags & CF_FORCECHAR) {
3669 Label = GetLocalLabel ();
3670 AddCodeLine ("sec");
3671 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3672 AddCodeLine ("bvc %s", LocalLabelName (Label));
3673 AddCodeLine ("eor #$80");
3674 g_defcodelabel (Label);
3675 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3676 AddCodeLine ("lda #$00");
3677 AddCodeLine ("ldx #$00");
3678 AddCodeLine ("rol a");
3684 /* Do a subtraction */
3685 Label = GetLocalLabel ();
3686 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3687 AddCodeLine ("txa");
3688 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3689 AddCodeLine ("bvc %s", LocalLabelName (Label));
3690 AddCodeLine ("eor #$80");
3691 g_defcodelabel (Label);
3692 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3693 AddCodeLine ("lda #$00");
3694 AddCodeLine ("ldx #$00");
3695 AddCodeLine ("rol a");
3699 /* This one is too costly */
3708 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3709 ** into the normal, non-optimized stuff. Note: The standard stuff will
3710 ** always work with ints.
3712 flags &= ~CF_FORCECHAR;
3713 g_push (flags & ~CF_CONST, 0);
3716 /* Use long way over the stack */
3717 oper (flags, val, ops);
3722 void g_le (unsigned flags, unsigned long val)
3723 /* Test for less than or equal to */
3725 static const char* const ops[4] = {
3726 "tosleax", "tosuleax", "tosleeax", "tosuleeax"
3730 /* If the right hand side is const, the lhs is not on stack but still
3731 ** in the primary register.
3733 if (flags & CF_CONST) {
3735 /* Look at the type */
3736 switch (flags & CF_TYPEMASK) {
3739 if (flags & CF_FORCECHAR) {
3740 if (flags & CF_UNSIGNED) {
3741 /* Unsigned compare */
3743 /* Use < instead of <= because the former gives
3744 ** better code on the 6502 than the latter.
3746 g_lt (flags, val+1);
3749 Warning ("Condition is always true");
3750 AddCodeLine ("jsr return1");
3753 /* Signed compare */
3754 if ((long) val < 0x7F) {
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");
3770 if (flags & CF_UNSIGNED) {
3771 /* Unsigned compare */
3773 /* Use < instead of <= because the former gives
3774 ** better code on the 6502 than the latter.
3776 g_lt (flags, val+1);
3779 Warning ("Condition is always true");
3780 AddCodeLine ("jsr return1");
3783 /* Signed compare */
3784 if ((long) val < 0x7FFF) {
3785 g_lt (flags, val+1);
3788 Warning ("Condition is always true");
3789 AddCodeLine ("jsr return1");
3795 if (flags & CF_UNSIGNED) {
3796 /* Unsigned compare */
3797 if (val < 0xFFFFFFFF) {
3798 /* Use < instead of <= because the former gives
3799 ** better code on the 6502 than the latter.
3801 g_lt (flags, val+1);
3804 Warning ("Condition is always true");
3805 AddCodeLine ("jsr return1");
3808 /* Signed compare */
3809 if ((long) val < 0x7FFFFFFF) {
3810 g_lt (flags, val+1);
3813 Warning ("Condition is always true");
3814 AddCodeLine ("jsr return1");
3823 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3824 ** into the normal, non-optimized stuff. Note: The standard stuff will
3825 ** always work with ints.
3827 flags &= ~CF_FORCECHAR;
3828 g_push (flags & ~CF_CONST, 0);
3831 /* Use long way over the stack */
3832 oper (flags, val, ops);
3837 void g_gt (unsigned flags, unsigned long val)
3838 /* Test for greater than */
3840 static const char* const ops[4] = {
3841 "tosgtax", "tosugtax", "tosgteax", "tosugteax"
3845 /* If the right hand side is const, the lhs is not on stack but still
3846 ** in the primary register.
3848 if (flags & CF_CONST) {
3850 /* Look at the type */
3851 switch (flags & CF_TYPEMASK) {
3854 if (flags & CF_FORCECHAR) {
3855 if (flags & CF_UNSIGNED) {
3857 /* If we have a compare > 0, we will replace it by
3858 ** != 0 here, since both are identical but the
3859 ** latter is easier to optimize.
3862 } else if (val < 0xFF) {
3863 /* Use >= instead of > because the former gives
3864 ** better code on the 6502 than the latter.
3866 g_ge (flags, val+1);
3869 Warning ("Condition is never true");
3870 AddCodeLine ("jsr return0");
3873 if ((long) val < 0x7F) {
3874 /* Use >= instead of > because the former gives
3875 ** better code on the 6502 than the latter.
3877 g_ge (flags, val+1);
3880 Warning ("Condition is never true");
3881 AddCodeLine ("jsr return0");
3889 if (flags & CF_UNSIGNED) {
3890 /* Unsigned compare */
3892 /* If we have a compare > 0, we will replace it by
3893 ** != 0 here, since both are identical but the latter
3894 ** is easier to optimize.
3897 } else if (val < 0xFFFF) {
3898 /* Use >= instead of > because the former gives better
3899 ** code on the 6502 than the latter.
3901 g_ge (flags, val+1);
3904 Warning ("Condition is never true");
3905 AddCodeLine ("jsr return0");
3908 /* Signed compare */
3909 if ((long) val < 0x7FFF) {
3910 g_ge (flags, val+1);
3913 Warning ("Condition is never true");
3914 AddCodeLine ("jsr return0");
3920 if (flags & CF_UNSIGNED) {
3921 /* Unsigned compare */
3923 /* If we have a compare > 0, we will replace it by
3924 ** != 0 here, since both are identical but the latter
3925 ** is easier to optimize.
3928 } else if (val < 0xFFFFFFFF) {
3929 /* Use >= instead of > because the former gives better
3930 ** code on the 6502 than the latter.
3932 g_ge (flags, val+1);
3935 Warning ("Condition is never true");
3936 AddCodeLine ("jsr return0");
3939 /* Signed compare */
3940 if ((long) val < 0x7FFFFFFF) {
3941 g_ge (flags, val+1);
3944 Warning ("Condition is never true");
3945 AddCodeLine ("jsr return0");
3954 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3955 ** into the normal, non-optimized stuff. Note: The standard stuff will
3956 ** always work with ints.
3958 flags &= ~CF_FORCECHAR;
3959 g_push (flags & ~CF_CONST, 0);
3962 /* Use long way over the stack */
3963 oper (flags, val, ops);
3968 void g_ge (unsigned flags, unsigned long val)
3969 /* Test for greater than or equal to */
3971 static const char* const ops[4] = {
3972 "tosgeax", "tosugeax", "tosgeeax", "tosugeeax"
3978 /* If the right hand side is const, the lhs is not on stack but still
3979 ** in the primary register.
3981 if (flags & CF_CONST) {
3983 /* Because the handling of the overflow flag is too complex for
3984 ** inlining, we can handle only unsigned compares, and signed
3985 ** compares against zero here.
3987 if (flags & CF_UNSIGNED) {
3989 /* Give a warning in some special cases */
3991 Warning ("Condition is always true");
3992 AddCodeLine ("jsr return1");
3996 /* Look at the type */
3997 switch (flags & CF_TYPEMASK) {
4000 if (flags & CF_FORCECHAR) {
4001 /* Do a subtraction. Condition is true if carry set */
4002 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4003 AddCodeLine ("lda #$00");
4004 AddCodeLine ("ldx #$00");
4005 AddCodeLine ("rol a");
4011 /* Do a subtraction. Condition is true if carry set */
4012 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4013 AddCodeLine ("txa");
4014 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4015 AddCodeLine ("lda #$00");
4016 AddCodeLine ("ldx #$00");
4017 AddCodeLine ("rol a");
4021 /* Do a subtraction. Condition is true if carry set */
4022 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4023 AddCodeLine ("txa");
4024 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4025 AddCodeLine ("lda sreg");
4026 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
4027 AddCodeLine ("lda sreg+1");
4028 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
4029 AddCodeLine ("lda #$00");
4030 AddCodeLine ("ldx #$00");
4031 AddCodeLine ("rol a");
4038 } else if (val == 0) {
4040 /* A signed compare against zero must only look at the sign bit */
4041 switch (flags & CF_TYPEMASK) {
4044 if (flags & CF_FORCECHAR) {
4045 AddCodeLine ("tax");
4046 AddCodeLine ("jsr boolge");
4052 /* Just test the high byte */
4053 AddCodeLine ("txa");
4054 AddCodeLine ("jsr boolge");
4058 /* Just test the high byte */
4059 AddCodeLine ("lda sreg+1");
4060 AddCodeLine ("jsr boolge");
4069 /* Signed compare against a constant != zero */
4070 switch (flags & CF_TYPEMASK) {
4073 if (flags & CF_FORCECHAR) {
4074 Label = GetLocalLabel ();
4075 AddCodeLine ("sec");
4076 AddCodeLine ("sbc #$%02X", (unsigned char)val);
4077 AddCodeLine ("bvs %s", LocalLabelName (Label));
4078 AddCodeLine ("eor #$80");
4079 g_defcodelabel (Label);
4080 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4081 AddCodeLine ("lda #$00");
4082 AddCodeLine ("ldx #$00");
4083 AddCodeLine ("rol a");
4089 /* Do a subtraction */
4090 Label = GetLocalLabel ();
4091 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4092 AddCodeLine ("txa");
4093 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4094 AddCodeLine ("bvs %s", LocalLabelName (Label));
4095 AddCodeLine ("eor #$80");
4096 g_defcodelabel (Label);
4097 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4098 AddCodeLine ("lda #$00");
4099 AddCodeLine ("ldx #$00");
4100 AddCodeLine ("rol a");
4104 /* This one is too costly */
4112 /* If we go here, we didn't emit code. Push the lhs on stack and fall
4113 ** into the normal, non-optimized stuff. Note: The standard stuff will
4114 ** always work with ints.
4116 flags &= ~CF_FORCECHAR;
4117 g_push (flags & ~CF_CONST, 0);
4120 /* Use long way over the stack */
4121 oper (flags, val, ops);
4126 /*****************************************************************************/
4127 /* Allocating static storage */
4128 /*****************************************************************************/
4132 void g_res (unsigned n)
4133 /* Reserve static storage, n bytes */
4135 AddDataLine ("\t.res\t%u,$00", n);
4140 void g_defdata (unsigned flags, unsigned long val, long offs)
4141 /* Define data with the size given in flags */
4143 if (flags & CF_CONST) {
4145 /* Numeric constant */
4146 switch (flags & CF_TYPEMASK) {
4149 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
4153 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
4157 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
4168 /* Create the correct label name */
4169 const char* Label = GetLabelName (flags, val, offs);
4171 /* Labels are always 16 bit */
4172 AddDataLine ("\t.addr\t%s", Label);
4179 void g_defbytes (const void* Bytes, unsigned Count)
4180 /* Output a row of bytes as a constant */
4186 /* Cast the buffer pointer */
4187 const unsigned char* Data = (const unsigned char*) Bytes;
4189 /* Output the stuff */
4192 /* How many go into this line? */
4193 if ((Chunk = Count) > 16) {
4198 /* Output one line */
4199 strcpy (Buf, "\t.byte\t");
4202 B += sprintf (B, "$%02X", *Data++);
4208 /* Output the line */
4209 AddDataLine ("%s", Buf);
4215 void g_zerobytes (unsigned Count)
4216 /* Output Count bytes of data initialized with zero */
4219 AddDataLine ("\t.res\t%u,$00", Count);
4225 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4226 /* Initialize a register variable from static initialization data */
4228 /* Register variables do always have less than 128 bytes */
4229 unsigned CodeLabel = GetLocalLabel ();
4230 AddCodeLine ("ldx #$%02X", (unsigned char) (Size - 1));
4231 g_defcodelabel (CodeLabel);
4232 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4233 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4234 AddCodeLine ("dex");
4235 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4240 void g_initauto (unsigned Label, unsigned Size)
4241 /* Initialize a local variable at stack offset zero from static data */
4243 unsigned CodeLabel = GetLocalLabel ();
4245 CheckLocalOffs (Size);
4247 AddCodeLine ("ldy #$%02X", Size-1);
4248 g_defcodelabel (CodeLabel);
4249 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4250 AddCodeLine ("sta (sp),y");
4251 AddCodeLine ("dey");
4252 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4253 } else if (Size <= 256) {
4254 AddCodeLine ("ldy #$00");
4255 g_defcodelabel (CodeLabel);
4256 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4257 AddCodeLine ("sta (sp),y");
4258 AddCodeLine ("iny");
4259 AddCmpCodeIfSizeNot256 ("cpy #$%02X", Size);
4260 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4266 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4267 /* Initialize a static local variable from static initialization data */
4270 unsigned CodeLabel = GetLocalLabel ();
4271 AddCodeLine ("ldy #$%02X", Size-1);
4272 g_defcodelabel (CodeLabel);
4273 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4274 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4275 AddCodeLine ("dey");
4276 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4277 } else if (Size <= 256) {
4278 unsigned CodeLabel = GetLocalLabel ();
4279 AddCodeLine ("ldy #$00");
4280 g_defcodelabel (CodeLabel);
4281 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4282 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4283 AddCodeLine ("iny");
4284 AddCmpCodeIfSizeNot256 ("cpy #$%02X", Size);
4285 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4287 /* Use the easy way here: memcpy() */
4288 g_getimmed (CF_STATIC, VarLabel, 0);
4289 AddCodeLine ("jsr pushax");
4290 g_getimmed (CF_STATIC, InitLabel, 0);
4291 AddCodeLine ("jsr pushax");
4292 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4293 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (uintptr_t) "memcpy", 0));
4299 /*****************************************************************************/
4300 /* Switch statement */
4301 /*****************************************************************************/
4305 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4306 /* Generate code for a switch statement */
4308 unsigned NextLabel = 0;
4311 /* Setup registers and determine which compare insn to use */
4312 const char* Compare;
4315 Compare = "cmp #$%02X";
4318 Compare = "cpx #$%02X";
4321 AddCodeLine ("ldy sreg");
4322 Compare = "cpy #$%02X";
4325 AddCodeLine ("ldy sreg+1");
4326 Compare = "cpy #$%02X";
4329 Internal ("Invalid depth in g_switch: %u", Depth);
4332 /* Walk over all nodes */
4333 for (I = 0; I < CollCount (Nodes); ++I) {
4335 /* Get the next case node */
4336 CaseNode* N = CollAtUnchecked (Nodes, I);
4338 /* If we have a next label, define it */
4340 g_defcodelabel (NextLabel);
4344 /* Do the compare */
4345 AddCodeLine (Compare, CN_GetValue (N));
4347 /* If this is the last level, jump directly to the case code if found */
4350 /* Branch if equal */
4351 g_falsejump (0, CN_GetLabel (N));
4355 /* Determine the next label */
4356 if (I == CollCount (Nodes) - 1) {
4357 /* Last node means not found */
4358 g_truejump (0, DefaultLabel);
4360 /* Jump to the next check */
4361 NextLabel = GetLocalLabel ();
4362 g_truejump (0, NextLabel);
4365 /* Check the next level */
4366 g_switch (N->Nodes, DefaultLabel, Depth-1);
4371 /* If we go here, we haven't found the label */
4372 g_jump (DefaultLabel);
4377 /*****************************************************************************/
4378 /* User supplied assembler code */
4379 /*****************************************************************************/
4383 void g_asmcode (struct StrBuf* B)
4384 /* Output one line of assembler code. */
4386 AddCodeLine ("%.*s", (int) SB_GetLen (B), SB_GetConstBuf (B));