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));
2429 void g_drop (unsigned Space)
2430 /* Drop space allocated on the stack */
2433 /* Inline the code since calling addysp repeatedly is quite some
2436 AddCodeLine ("pha");
2437 AddCodeLine ("lda #$%02X", (unsigned char) Space);
2438 AddCodeLine ("clc");
2439 AddCodeLine ("adc sp");
2440 AddCodeLine ("sta sp");
2441 AddCodeLine ("lda #$%02X", (unsigned char) (Space >> 8));
2442 AddCodeLine ("adc sp+1");
2443 AddCodeLine ("sta sp+1");
2444 AddCodeLine ("pla");
2445 } else if (Space > 8) {
2446 AddCodeLine ("ldy #$%02X", Space);
2447 AddCodeLine ("jsr addysp");
2448 } else if (Space != 0) {
2449 AddCodeLine ("jsr incsp%u", Space);
2455 void g_space (int Space)
2456 /* Create or drop space on the stack */
2459 /* This is actually a drop operation */
2461 } else if (Space > 255) {
2462 /* Inline the code since calling subysp repeatedly is quite some
2465 AddCodeLine ("pha");
2466 AddCodeLine ("lda sp");
2467 AddCodeLine ("sec");
2468 AddCodeLine ("sbc #$%02X", (unsigned char) Space);
2469 AddCodeLine ("sta sp");
2470 AddCodeLine ("lda sp+1");
2471 AddCodeLine ("sbc #$%02X", (unsigned char) (Space >> 8));
2472 AddCodeLine ("sta sp+1");
2473 AddCodeLine ("pla");
2474 } else if (Space > 8) {
2475 AddCodeLine ("ldy #$%02X", Space);
2476 AddCodeLine ("jsr subysp");
2477 } else if (Space != 0) {
2478 AddCodeLine ("jsr decsp%u", Space);
2484 void g_cstackcheck (void)
2485 /* Check for a C stack overflow */
2487 AddCodeLine ("jsr cstkchk");
2492 void g_stackcheck (void)
2493 /* Check for a stack overflow */
2495 AddCodeLine ("jsr stkchk");
2500 void g_add (unsigned flags, unsigned long val)
2501 /* Primary = TOS + Primary */
2503 static const char* const ops[4] = {
2504 "tosaddax", "tosaddax", "tosaddeax", "tosaddeax"
2507 if (flags & CF_CONST) {
2508 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2509 g_push (flags & ~CF_CONST, 0);
2511 oper (flags, val, ops);
2516 void g_sub (unsigned flags, unsigned long val)
2517 /* Primary = TOS - Primary */
2519 static const char* const ops[4] = {
2520 "tossubax", "tossubax", "tossubeax", "tossubeax"
2523 if (flags & CF_CONST) {
2524 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2525 g_push (flags & ~CF_CONST, 0);
2527 oper (flags, val, ops);
2532 void g_rsub (unsigned flags, unsigned long val)
2533 /* Primary = Primary - TOS */
2535 static const char* const ops[4] = {
2536 "tosrsubax", "tosrsubax", "tosrsubeax", "tosrsubeax"
2538 oper (flags, val, ops);
2543 void g_mul (unsigned flags, unsigned long val)
2544 /* Primary = TOS * Primary */
2546 static const char* const ops[4] = {
2547 "tosmulax", "tosumulax", "tosmuleax", "tosumuleax"
2552 /* Do strength reduction if the value is constant and a power of two */
2553 if (flags & CF_CONST && (p2 = PowerOf2 (val)) >= 0) {
2554 /* Generate a shift instead */
2559 /* If the right hand side is const, the lhs is not on stack but still
2560 ** in the primary register.
2562 if (flags & CF_CONST) {
2564 switch (flags & CF_TYPEMASK) {
2567 if (flags & CF_FORCECHAR) {
2568 /* Handle some special cases */
2572 AddCodeLine ("sta tmp1");
2573 AddCodeLine ("asl a");
2574 AddCodeLine ("clc");
2575 AddCodeLine ("adc tmp1");
2579 AddCodeLine ("sta tmp1");
2580 AddCodeLine ("asl a");
2581 AddCodeLine ("asl a");
2582 AddCodeLine ("clc");
2583 AddCodeLine ("adc tmp1");
2587 AddCodeLine ("sta tmp1");
2588 AddCodeLine ("asl a");
2589 AddCodeLine ("clc");
2590 AddCodeLine ("adc tmp1");
2591 AddCodeLine ("asl a");
2595 AddCodeLine ("sta tmp1");
2596 AddCodeLine ("asl a");
2597 AddCodeLine ("asl a");
2598 AddCodeLine ("clc");
2599 AddCodeLine ("adc tmp1");
2600 AddCodeLine ("asl a");
2609 AddCodeLine ("jsr mulax3");
2612 AddCodeLine ("jsr mulax5");
2615 AddCodeLine ("jsr mulax6");
2618 AddCodeLine ("jsr mulax7");
2621 AddCodeLine ("jsr mulax9");
2624 AddCodeLine ("jsr mulax10");
2636 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2637 ** into the normal, non-optimized stuff.
2639 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2640 g_push (flags & ~CF_CONST, 0);
2644 /* Use long way over the stack */
2645 oper (flags, val, ops);
2650 void g_div (unsigned flags, unsigned long val)
2651 /* Primary = TOS / Primary */
2653 static const char* const ops[4] = {
2654 "tosdivax", "tosudivax", "tosdiveax", "tosudiveax"
2657 /* Do strength reduction if the value is constant and a power of two */
2659 if ((flags & CF_CONST) && (p2 = PowerOf2 (val)) >= 0) {
2660 /* Generate a shift instead */
2663 /* Generate a division */
2664 if (flags & CF_CONST) {
2665 /* lhs is not on stack */
2666 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2667 g_push (flags & ~CF_CONST, 0);
2669 oper (flags, val, ops);
2675 void g_mod (unsigned flags, unsigned long val)
2676 /* Primary = TOS % Primary */
2678 static const char* const ops[4] = {
2679 "tosmodax", "tosumodax", "tosmodeax", "tosumodeax"
2683 /* Check if we can do some cost reduction */
2684 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = PowerOf2 (val)) >= 0) {
2685 /* We can do that with an AND operation */
2686 g_and (flags, val - 1);
2688 /* Do it the hard way... */
2689 if (flags & CF_CONST) {
2690 /* lhs is not on stack */
2691 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2692 g_push (flags & ~CF_CONST, 0);
2694 oper (flags, val, ops);
2700 void g_or (unsigned flags, unsigned long val)
2701 /* Primary = TOS | Primary */
2703 static const char* const ops[4] = {
2704 "tosorax", "tosorax", "tosoreax", "tosoreax"
2707 /* If the right hand side is const, the lhs is not on stack but still
2708 ** in the primary register.
2710 if (flags & CF_CONST) {
2712 switch (flags & CF_TYPEMASK) {
2715 if (flags & CF_FORCECHAR) {
2716 if ((val & 0xFF) != 0) {
2717 AddCodeLine ("ora #$%02X", (unsigned char)val);
2725 if ((val & 0xFF) != 0) {
2726 AddCodeLine ("ora #$%02X", (unsigned char)val);
2728 } else if ((val & 0xFF00) == 0xFF00) {
2729 if ((val & 0xFF) != 0) {
2730 AddCodeLine ("ora #$%02X", (unsigned char)val);
2732 AddCodeLine ("ldx #$FF");
2733 } else if (val != 0) {
2734 AddCodeLine ("ora #$%02X", (unsigned char)val);
2735 AddCodeLine ("pha");
2736 AddCodeLine ("txa");
2737 AddCodeLine ("ora #$%02X", (unsigned char)(val >> 8));
2738 AddCodeLine ("tax");
2739 AddCodeLine ("pla");
2745 if ((val & 0xFF) != 0) {
2746 AddCodeLine ("ora #$%02X", (unsigned char)val);
2756 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2757 ** into the normal, non-optimized stuff. Note: The standard stuff will
2758 ** always work with ints.
2760 flags &= ~CF_FORCECHAR;
2761 g_push (flags & ~CF_CONST, 0);
2764 /* Use long way over the stack */
2765 oper (flags, val, ops);
2770 void g_xor (unsigned flags, unsigned long val)
2771 /* Primary = TOS ^ Primary */
2773 static const char* const ops[4] = {
2774 "tosxorax", "tosxorax", "tosxoreax", "tosxoreax"
2778 /* If the right hand side is const, the lhs is not on stack but still
2779 ** in the primary register.
2781 if (flags & CF_CONST) {
2783 switch (flags & CF_TYPEMASK) {
2786 if (flags & CF_FORCECHAR) {
2787 if ((val & 0xFF) != 0) {
2788 AddCodeLine ("eor #$%02X", (unsigned char)val);
2797 AddCodeLine ("eor #$%02X", (unsigned char)val);
2799 } else if (val != 0) {
2800 if ((val & 0xFF) != 0) {
2801 AddCodeLine ("eor #$%02X", (unsigned char)val);
2803 AddCodeLine ("pha");
2804 AddCodeLine ("txa");
2805 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2806 AddCodeLine ("tax");
2807 AddCodeLine ("pla");
2814 AddCodeLine ("eor #$%02X", (unsigned char)val);
2824 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2825 ** into the normal, non-optimized stuff. Note: The standard stuff will
2826 ** always work with ints.
2828 flags &= ~CF_FORCECHAR;
2829 g_push (flags & ~CF_CONST, 0);
2832 /* Use long way over the stack */
2833 oper (flags, val, ops);
2838 void g_and (unsigned Flags, unsigned long Val)
2839 /* Primary = TOS & Primary */
2841 static const char* const ops[4] = {
2842 "tosandax", "tosandax", "tosandeax", "tosandeax"
2845 /* If the right hand side is const, the lhs is not on stack but still
2846 ** in the primary register.
2848 if (Flags & CF_CONST) {
2850 switch (Flags & CF_TYPEMASK) {
2853 if (Flags & CF_FORCECHAR) {
2854 if ((Val & 0xFF) == 0x00) {
2855 AddCodeLine ("lda #$00");
2856 } else if ((Val & 0xFF) != 0xFF) {
2857 AddCodeLine ("and #$%02X", (unsigned char)Val);
2863 if ((Val & 0xFFFF) != 0xFFFF) {
2865 AddCodeLine ("ldx #$00");
2867 AddCodeLine ("lda #$00");
2868 } else if (Val != 0xFF) {
2869 AddCodeLine ("and #$%02X", (unsigned char)Val);
2871 } else if ((Val & 0xFFFF) == 0xFF00) {
2872 AddCodeLine ("lda #$00");
2873 } else if ((Val & 0xFF00) == 0xFF00) {
2874 AddCodeLine ("and #$%02X", (unsigned char)Val);
2875 } else if ((Val & 0x00FF) == 0x0000) {
2876 AddCodeLine ("txa");
2877 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2878 AddCodeLine ("tax");
2879 AddCodeLine ("lda #$00");
2881 AddCodeLine ("tay");
2882 AddCodeLine ("txa");
2883 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2884 AddCodeLine ("tax");
2885 AddCodeLine ("tya");
2886 if ((Val & 0x00FF) == 0x0000) {
2887 AddCodeLine ("lda #$00");
2888 } else if ((Val & 0x00FF) != 0x00FF) {
2889 AddCodeLine ("and #$%02X", (unsigned char)Val);
2897 AddCodeLine ("ldx #$00");
2898 AddCodeLine ("stx sreg+1");
2899 AddCodeLine ("stx sreg");
2900 if ((Val & 0xFF) != 0xFF) {
2901 AddCodeLine ("and #$%02X", (unsigned char)Val);
2904 } else if (Val == 0xFF00) {
2905 AddCodeLine ("lda #$00");
2906 AddCodeLine ("sta sreg+1");
2907 AddCodeLine ("sta sreg");
2916 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2917 ** into the normal, non-optimized stuff. Note: The standard stuff will
2918 ** always work with ints.
2920 Flags &= ~CF_FORCECHAR;
2921 g_push (Flags & ~CF_CONST, 0);
2924 /* Use long way over the stack */
2925 oper (Flags, Val, ops);
2930 void g_asr (unsigned flags, unsigned long val)
2931 /* Primary = TOS >> Primary */
2933 static const char* const ops[4] = {
2934 "tosasrax", "tosshrax", "tosasreax", "tosshreax"
2937 /* If the right hand side is const, the lhs is not on stack but still
2938 ** in the primary register.
2940 if (flags & CF_CONST) {
2942 switch (flags & CF_TYPEMASK) {
2948 if (flags & CF_UNSIGNED) {
2949 AddCodeLine ("txa");
2950 AddCodeLine ("ldx #$00");
2952 unsigned L = GetLocalLabel();
2953 AddCodeLine ("cpx #$80"); /* Sign bit into carry */
2954 AddCodeLine ("txa");
2955 AddCodeLine ("ldx #$00");
2956 AddCodeLine ("bcc %s", LocalLabelName (L));
2957 AddCodeLine ("dex"); /* Make $FF */
2963 if (flags & CF_UNSIGNED) {
2964 AddCodeLine ("jsr shrax4");
2966 AddCodeLine ("jsr asrax4");
2971 if (flags & CF_UNSIGNED) {
2972 AddCodeLine ("jsr shrax%ld", val);
2974 AddCodeLine ("jsr asrax%ld", val);
2982 AddCodeLine ("ldx #$00");
2983 AddCodeLine ("lda sreg+1");
2984 if ((flags & CF_UNSIGNED) == 0) {
2985 unsigned L = GetLocalLabel();
2986 AddCodeLine ("bpl %s", LocalLabelName (L));
2987 AddCodeLine ("dex");
2990 AddCodeLine ("stx sreg");
2991 AddCodeLine ("stx sreg+1");
2995 AddCodeLine ("ldy #$00");
2996 AddCodeLine ("ldx sreg+1");
2997 if ((flags & CF_UNSIGNED) == 0) {
2998 unsigned L = GetLocalLabel();
2999 AddCodeLine ("bpl %s", LocalLabelName (L));
3000 AddCodeLine ("dey");
3003 AddCodeLine ("lda sreg");
3004 AddCodeLine ("sty sreg+1");
3005 AddCodeLine ("sty sreg");
3009 AddCodeLine ("txa");
3010 AddCodeLine ("ldx sreg");
3011 AddCodeLine ("ldy sreg+1");
3012 AddCodeLine ("sty sreg");
3013 if ((flags & CF_UNSIGNED) == 0) {
3014 unsigned L = GetLocalLabel();
3015 AddCodeLine ("cpy #$80");
3016 AddCodeLine ("ldy #$00");
3017 AddCodeLine ("bcc %s", LocalLabelName (L));
3018 AddCodeLine ("dey");
3021 AddCodeLine ("ldy #$00");
3023 AddCodeLine ("sty sreg+1");
3027 if (flags & CF_UNSIGNED) {
3028 AddCodeLine ("jsr shreax4");
3030 AddCodeLine ("jsr asreax4");
3035 if (flags & CF_UNSIGNED) {
3036 AddCodeLine ("jsr shreax%ld", val);
3038 AddCodeLine ("jsr asreax%ld", val);
3047 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3048 ** into the normal, non-optimized stuff. Note: The standard stuff will
3049 ** always work with ints.
3051 flags &= ~CF_FORCECHAR;
3052 g_push (flags & ~CF_CONST, 0);
3055 /* Use long way over the stack */
3056 oper (flags, val, ops);
3061 void g_asl (unsigned flags, unsigned long val)
3062 /* Primary = TOS << Primary */
3064 static const char* const ops[4] = {
3065 "tosaslax", "tosshlax", "tosasleax", "tosshleax"
3069 /* If the right hand side is const, the lhs is not on stack but still
3070 ** in the primary register.
3072 if (flags & CF_CONST) {
3074 switch (flags & CF_TYPEMASK) {
3080 AddCodeLine ("tax");
3081 AddCodeLine ("lda #$00");
3085 if (flags & CF_UNSIGNED) {
3086 AddCodeLine ("jsr shlax4");
3088 AddCodeLine ("jsr aslax4");
3093 if (flags & CF_UNSIGNED) {
3094 AddCodeLine ("jsr shlax%ld", val);
3096 AddCodeLine ("jsr aslax%ld", val);
3104 AddCodeLine ("sta sreg+1");
3105 AddCodeLine ("lda #$00");
3106 AddCodeLine ("tax");
3107 AddCodeLine ("sta sreg");
3111 AddCodeLine ("stx sreg+1");
3112 AddCodeLine ("sta sreg");
3113 AddCodeLine ("lda #$00");
3114 AddCodeLine ("tax");
3118 AddCodeLine ("ldy sreg");
3119 AddCodeLine ("sty sreg+1");
3120 AddCodeLine ("stx sreg");
3121 AddCodeLine ("tax");
3122 AddCodeLine ("lda #$00");
3126 if (flags & CF_UNSIGNED) {
3127 AddCodeLine ("jsr shleax4");
3129 AddCodeLine ("jsr asleax4");
3134 if (flags & CF_UNSIGNED) {
3135 AddCodeLine ("jsr shleax%ld", val);
3137 AddCodeLine ("jsr asleax%ld", val);
3146 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3147 ** into the normal, non-optimized stuff. Note: The standard stuff will
3148 ** always work with ints.
3150 flags &= ~CF_FORCECHAR;
3151 g_push (flags & ~CF_CONST, 0);
3154 /* Use long way over the stack */
3155 oper (flags, val, ops);
3160 void g_neg (unsigned Flags)
3161 /* Primary = -Primary */
3163 switch (Flags & CF_TYPEMASK) {
3166 if (Flags & CF_FORCECHAR) {
3167 AddCodeLine ("eor #$FF");
3168 AddCodeLine ("clc");
3169 AddCodeLine ("adc #$01");
3175 AddCodeLine ("jsr negax");
3179 AddCodeLine ("jsr negeax");
3189 void g_bneg (unsigned flags)
3190 /* Primary = !Primary */
3192 switch (flags & CF_TYPEMASK) {
3195 AddCodeLine ("jsr bnega");
3199 AddCodeLine ("jsr bnegax");
3203 AddCodeLine ("jsr bnegeax");
3213 void g_com (unsigned Flags)
3214 /* Primary = ~Primary */
3216 switch (Flags & CF_TYPEMASK) {
3219 if (Flags & CF_FORCECHAR) {
3220 AddCodeLine ("eor #$FF");
3226 AddCodeLine ("jsr complax");
3230 AddCodeLine ("jsr compleax");
3240 void g_inc (unsigned flags, unsigned long val)
3241 /* Increment the primary register by a given number */
3243 /* Don't inc by zero */
3248 /* Generate code for the supported types */
3250 switch (flags & CF_TYPEMASK) {
3253 if (flags & CF_FORCECHAR) {
3254 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3256 AddCodeLine ("ina");
3259 AddCodeLine ("clc");
3260 AddCodeLine ("adc #$%02X", (unsigned char)val);
3267 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) {
3268 unsigned L = GetLocalLabel();
3269 AddCodeLine ("ina");
3270 AddCodeLine ("bne %s", LocalLabelName (L));
3271 AddCodeLine ("inx");
3273 } else if (IS_Get (&CodeSizeFactor) < 200) {
3276 AddCodeLine ("jsr incax%lu", val);
3277 } else if (val <= 255) {
3278 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3279 AddCodeLine ("jsr incaxy");
3281 g_add (flags | CF_CONST, val);
3284 /* Inline the code */
3286 if ((val & 0xFF) != 0) {
3287 unsigned L = GetLocalLabel();
3288 AddCodeLine ("clc");
3289 AddCodeLine ("adc #$%02X", (unsigned char) val);
3290 AddCodeLine ("bcc %s", LocalLabelName (L));
3291 AddCodeLine ("inx");
3295 AddCodeLine ("inx");
3298 AddCodeLine ("inx");
3301 AddCodeLine ("inx");
3303 } else if ((val & 0xFF) != 0) {
3304 AddCodeLine ("clc");
3305 AddCodeLine ("adc #$%02X", (unsigned char) val);
3306 AddCodeLine ("pha");
3307 AddCodeLine ("txa");
3308 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3309 AddCodeLine ("tax");
3310 AddCodeLine ("pla");
3312 AddCodeLine ("pha");
3313 AddCodeLine ("txa");
3314 AddCodeLine ("clc");
3315 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3316 AddCodeLine ("tax");
3317 AddCodeLine ("pla");
3324 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3325 AddCodeLine ("jsr inceaxy");
3327 g_add (flags | CF_CONST, val);
3339 void g_dec (unsigned flags, unsigned long val)
3340 /* Decrement the primary register by a given number */
3342 /* Don't dec by zero */
3347 /* Generate code for the supported types */
3349 switch (flags & CF_TYPEMASK) {
3352 if (flags & CF_FORCECHAR) {
3353 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3355 AddCodeLine ("dea");
3358 AddCodeLine ("sec");
3359 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3366 if (IS_Get (&CodeSizeFactor) < 200) {
3367 /* Use subroutines */
3369 AddCodeLine ("jsr decax%d", (int) val);
3370 } else if (val <= 255) {
3371 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3372 AddCodeLine ("jsr decaxy");
3374 g_sub (flags | CF_CONST, val);
3377 /* Inline the code */
3379 if ((val & 0xFF) != 0) {
3380 unsigned L = GetLocalLabel();
3381 AddCodeLine ("sec");
3382 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3383 AddCodeLine ("bcs %s", LocalLabelName (L));
3384 AddCodeLine ("dex");
3388 AddCodeLine ("dex");
3391 AddCodeLine ("dex");
3394 if ((val & 0xFF) != 0) {
3395 AddCodeLine ("sec");
3396 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3397 AddCodeLine ("pha");
3398 AddCodeLine ("txa");
3399 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3400 AddCodeLine ("tax");
3401 AddCodeLine ("pla");
3403 AddCodeLine ("pha");
3404 AddCodeLine ("txa");
3405 AddCodeLine ("sec");
3406 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3407 AddCodeLine ("tax");
3408 AddCodeLine ("pla");
3416 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3417 AddCodeLine ("jsr deceaxy");
3419 g_sub (flags | CF_CONST, val);
3432 ** Following are the conditional operators. They compare the TOS against
3433 ** the primary and put a literal 1 in the primary if the condition is
3434 ** true, otherwise they clear the primary register
3439 void g_eq (unsigned flags, unsigned long val)
3440 /* Test for equal */
3442 static const char* const ops[4] = {
3443 "toseqax", "toseqax", "toseqeax", "toseqeax"
3448 /* If the right hand side is const, the lhs is not on stack but still
3449 ** in the primary register.
3451 if (flags & CF_CONST) {
3453 switch (flags & CF_TYPEMASK) {
3456 if (flags & CF_FORCECHAR) {
3457 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3458 AddCodeLine ("jsr booleq");
3464 L = GetLocalLabel();
3465 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3466 AddCodeLine ("bne %s", LocalLabelName (L));
3467 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3469 AddCodeLine ("jsr booleq");
3479 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3480 ** into the normal, non-optimized stuff. Note: The standard stuff will
3481 ** always work with ints.
3483 flags &= ~CF_FORCECHAR;
3484 g_push (flags & ~CF_CONST, 0);
3487 /* Use long way over the stack */
3488 oper (flags, val, ops);
3493 void g_ne (unsigned flags, unsigned long val)
3494 /* Test for not equal */
3496 static const char* const ops[4] = {
3497 "tosneax", "tosneax", "tosneeax", "tosneeax"
3502 /* If the right hand side is const, the lhs is not on stack but still
3503 ** in the primary register.
3505 if (flags & CF_CONST) {
3507 switch (flags & CF_TYPEMASK) {
3510 if (flags & CF_FORCECHAR) {
3511 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3512 AddCodeLine ("jsr boolne");
3518 L = GetLocalLabel();
3519 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3520 AddCodeLine ("bne %s", LocalLabelName (L));
3521 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3523 AddCodeLine ("jsr boolne");
3533 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3534 ** into the normal, non-optimized stuff. Note: The standard stuff will
3535 ** always work with ints.
3537 flags &= ~CF_FORCECHAR;
3538 g_push (flags & ~CF_CONST, 0);
3541 /* Use long way over the stack */
3542 oper (flags, val, ops);
3547 void g_lt (unsigned flags, unsigned long val)
3548 /* Test for less than */
3550 static const char* const ops[4] = {
3551 "tosltax", "tosultax", "toslteax", "tosulteax"
3556 /* If the right hand side is const, the lhs is not on stack but still
3557 ** in the primary register.
3559 if (flags & CF_CONST) {
3561 /* Because the handling of the overflow flag is too complex for
3562 ** inlining, we can handle only unsigned compares, and signed
3563 ** compares against zero here.
3565 if (flags & CF_UNSIGNED) {
3567 /* Give a warning in some special cases */
3569 Warning ("Condition is never true");
3570 AddCodeLine ("jsr return0");
3574 /* Look at the type */
3575 switch (flags & CF_TYPEMASK) {
3578 if (flags & CF_FORCECHAR) {
3579 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3580 AddCodeLine ("jsr boolult");
3586 /* If the low byte is zero, we must only test the high byte */
3587 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3588 if ((val & 0xFF) != 0) {
3589 unsigned L = GetLocalLabel();
3590 AddCodeLine ("bne %s", LocalLabelName (L));
3591 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3594 AddCodeLine ("jsr boolult");
3598 /* Do a subtraction */
3599 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3600 AddCodeLine ("txa");
3601 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3602 AddCodeLine ("lda sreg");
3603 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3604 AddCodeLine ("lda sreg+1");
3605 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3606 AddCodeLine ("jsr boolult");
3613 } else if (val == 0) {
3615 /* A signed compare against zero must only look at the sign bit */
3616 switch (flags & CF_TYPEMASK) {
3619 if (flags & CF_FORCECHAR) {
3620 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3621 AddCodeLine ("lda #$00");
3622 AddCodeLine ("ldx #$00");
3623 AddCodeLine ("rol a");
3629 /* Just check the high byte */
3630 AddCodeLine ("cpx #$80"); /* Bit 7 -> carry */
3631 AddCodeLine ("lda #$00");
3632 AddCodeLine ("ldx #$00");
3633 AddCodeLine ("rol a");
3637 /* Just check the high byte */
3638 AddCodeLine ("lda sreg+1");
3639 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3640 AddCodeLine ("lda #$00");
3641 AddCodeLine ("ldx #$00");
3642 AddCodeLine ("rol a");
3651 /* Signed compare against a constant != zero */
3652 switch (flags & CF_TYPEMASK) {
3655 if (flags & CF_FORCECHAR) {
3656 Label = GetLocalLabel ();
3657 AddCodeLine ("sec");
3658 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3659 AddCodeLine ("bvc %s", LocalLabelName (Label));
3660 AddCodeLine ("eor #$80");
3661 g_defcodelabel (Label);
3662 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3663 AddCodeLine ("lda #$00");
3664 AddCodeLine ("ldx #$00");
3665 AddCodeLine ("rol a");
3671 /* Do a subtraction */
3672 Label = GetLocalLabel ();
3673 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3674 AddCodeLine ("txa");
3675 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3676 AddCodeLine ("bvc %s", LocalLabelName (Label));
3677 AddCodeLine ("eor #$80");
3678 g_defcodelabel (Label);
3679 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3680 AddCodeLine ("lda #$00");
3681 AddCodeLine ("ldx #$00");
3682 AddCodeLine ("rol a");
3686 /* This one is too costly */
3695 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3696 ** into the normal, non-optimized stuff. Note: The standard stuff will
3697 ** always work with ints.
3699 flags &= ~CF_FORCECHAR;
3700 g_push (flags & ~CF_CONST, 0);
3703 /* Use long way over the stack */
3704 oper (flags, val, ops);
3709 void g_le (unsigned flags, unsigned long val)
3710 /* Test for less than or equal to */
3712 static const char* const ops[4] = {
3713 "tosleax", "tosuleax", "tosleeax", "tosuleeax"
3717 /* If the right hand side is const, the lhs is not on stack but still
3718 ** in the primary register.
3720 if (flags & CF_CONST) {
3722 /* Look at the type */
3723 switch (flags & CF_TYPEMASK) {
3726 if (flags & CF_FORCECHAR) {
3727 if (flags & CF_UNSIGNED) {
3728 /* Unsigned compare */
3730 /* Use < instead of <= because the former gives
3731 ** better code on the 6502 than the latter.
3733 g_lt (flags, val+1);
3736 Warning ("Condition is always true");
3737 AddCodeLine ("jsr return1");
3740 /* Signed compare */
3741 if ((long) val < 0x7F) {
3742 /* Use < instead of <= because the former gives
3743 ** better code on the 6502 than the latter.
3745 g_lt (flags, val+1);
3748 Warning ("Condition is always true");
3749 AddCodeLine ("jsr return1");
3757 if (flags & CF_UNSIGNED) {
3758 /* Unsigned compare */
3760 /* Use < instead of <= because the former gives
3761 ** better code on the 6502 than the latter.
3763 g_lt (flags, val+1);
3766 Warning ("Condition is always true");
3767 AddCodeLine ("jsr return1");
3770 /* Signed compare */
3771 if ((long) val < 0x7FFF) {
3772 g_lt (flags, val+1);
3775 Warning ("Condition is always true");
3776 AddCodeLine ("jsr return1");
3782 if (flags & CF_UNSIGNED) {
3783 /* Unsigned compare */
3784 if (val < 0xFFFFFFFF) {
3785 /* Use < instead of <= because the former gives
3786 ** better code on the 6502 than the latter.
3788 g_lt (flags, val+1);
3791 Warning ("Condition is always true");
3792 AddCodeLine ("jsr return1");
3795 /* Signed compare */
3796 if ((long) val < 0x7FFFFFFF) {
3797 g_lt (flags, val+1);
3800 Warning ("Condition is always true");
3801 AddCodeLine ("jsr return1");
3810 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3811 ** into the normal, non-optimized stuff. Note: The standard stuff will
3812 ** always work with ints.
3814 flags &= ~CF_FORCECHAR;
3815 g_push (flags & ~CF_CONST, 0);
3818 /* Use long way over the stack */
3819 oper (flags, val, ops);
3824 void g_gt (unsigned flags, unsigned long val)
3825 /* Test for greater than */
3827 static const char* const ops[4] = {
3828 "tosgtax", "tosugtax", "tosgteax", "tosugteax"
3832 /* If the right hand side is const, the lhs is not on stack but still
3833 ** in the primary register.
3835 if (flags & CF_CONST) {
3837 /* Look at the type */
3838 switch (flags & CF_TYPEMASK) {
3841 if (flags & CF_FORCECHAR) {
3842 if (flags & CF_UNSIGNED) {
3844 /* If we have a compare > 0, we will replace it by
3845 ** != 0 here, since both are identical but the
3846 ** latter is easier to optimize.
3849 } else if (val < 0xFF) {
3850 /* Use >= instead of > because the former gives
3851 ** better code on the 6502 than the latter.
3853 g_ge (flags, val+1);
3856 Warning ("Condition is never true");
3857 AddCodeLine ("jsr return0");
3860 if ((long) val < 0x7F) {
3861 /* Use >= instead of > because the former gives
3862 ** better code on the 6502 than the latter.
3864 g_ge (flags, val+1);
3867 Warning ("Condition is never true");
3868 AddCodeLine ("jsr return0");
3876 if (flags & CF_UNSIGNED) {
3877 /* Unsigned compare */
3879 /* If we have a compare > 0, we will replace it by
3880 ** != 0 here, since both are identical but the latter
3881 ** is easier to optimize.
3884 } else if (val < 0xFFFF) {
3885 /* Use >= instead of > because the former gives better
3886 ** code on the 6502 than the latter.
3888 g_ge (flags, val+1);
3891 Warning ("Condition is never true");
3892 AddCodeLine ("jsr return0");
3895 /* Signed compare */
3896 if ((long) val < 0x7FFF) {
3897 g_ge (flags, val+1);
3900 Warning ("Condition is never true");
3901 AddCodeLine ("jsr return0");
3907 if (flags & CF_UNSIGNED) {
3908 /* Unsigned compare */
3910 /* If we have a compare > 0, we will replace it by
3911 ** != 0 here, since both are identical but the latter
3912 ** is easier to optimize.
3915 } else if (val < 0xFFFFFFFF) {
3916 /* Use >= instead of > because the former gives better
3917 ** code on the 6502 than the latter.
3919 g_ge (flags, val+1);
3922 Warning ("Condition is never true");
3923 AddCodeLine ("jsr return0");
3926 /* Signed compare */
3927 if ((long) val < 0x7FFFFFFF) {
3928 g_ge (flags, val+1);
3931 Warning ("Condition is never true");
3932 AddCodeLine ("jsr return0");
3941 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3942 ** into the normal, non-optimized stuff. Note: The standard stuff will
3943 ** always work with ints.
3945 flags &= ~CF_FORCECHAR;
3946 g_push (flags & ~CF_CONST, 0);
3949 /* Use long way over the stack */
3950 oper (flags, val, ops);
3955 void g_ge (unsigned flags, unsigned long val)
3956 /* Test for greater than or equal to */
3958 static const char* const ops[4] = {
3959 "tosgeax", "tosugeax", "tosgeeax", "tosugeeax"
3965 /* If the right hand side is const, the lhs is not on stack but still
3966 ** in the primary register.
3968 if (flags & CF_CONST) {
3970 /* Because the handling of the overflow flag is too complex for
3971 ** inlining, we can handle only unsigned compares, and signed
3972 ** compares against zero here.
3974 if (flags & CF_UNSIGNED) {
3976 /* Give a warning in some special cases */
3978 Warning ("Condition is always true");
3979 AddCodeLine ("jsr return1");
3983 /* Look at the type */
3984 switch (flags & CF_TYPEMASK) {
3987 if (flags & CF_FORCECHAR) {
3988 /* Do a subtraction. Condition is true if carry set */
3989 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3990 AddCodeLine ("lda #$00");
3991 AddCodeLine ("ldx #$00");
3992 AddCodeLine ("rol a");
3998 /* Do a subtraction. Condition is true if carry set */
3999 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4000 AddCodeLine ("txa");
4001 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4002 AddCodeLine ("lda #$00");
4003 AddCodeLine ("ldx #$00");
4004 AddCodeLine ("rol a");
4008 /* Do a subtraction. Condition is true if carry set */
4009 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4010 AddCodeLine ("txa");
4011 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4012 AddCodeLine ("lda sreg");
4013 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
4014 AddCodeLine ("lda sreg+1");
4015 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
4016 AddCodeLine ("lda #$00");
4017 AddCodeLine ("ldx #$00");
4018 AddCodeLine ("rol a");
4025 } else if (val == 0) {
4027 /* A signed compare against zero must only look at the sign bit */
4028 switch (flags & CF_TYPEMASK) {
4031 if (flags & CF_FORCECHAR) {
4032 AddCodeLine ("tax");
4033 AddCodeLine ("jsr boolge");
4039 /* Just test the high byte */
4040 AddCodeLine ("txa");
4041 AddCodeLine ("jsr boolge");
4045 /* Just test the high byte */
4046 AddCodeLine ("lda sreg+1");
4047 AddCodeLine ("jsr boolge");
4056 /* Signed compare against a constant != zero */
4057 switch (flags & CF_TYPEMASK) {
4060 if (flags & CF_FORCECHAR) {
4061 Label = GetLocalLabel ();
4062 AddCodeLine ("sec");
4063 AddCodeLine ("sbc #$%02X", (unsigned char)val);
4064 AddCodeLine ("bvs %s", LocalLabelName (Label));
4065 AddCodeLine ("eor #$80");
4066 g_defcodelabel (Label);
4067 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4068 AddCodeLine ("lda #$00");
4069 AddCodeLine ("ldx #$00");
4070 AddCodeLine ("rol a");
4076 /* Do a subtraction */
4077 Label = GetLocalLabel ();
4078 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4079 AddCodeLine ("txa");
4080 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4081 AddCodeLine ("bvs %s", LocalLabelName (Label));
4082 AddCodeLine ("eor #$80");
4083 g_defcodelabel (Label);
4084 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4085 AddCodeLine ("lda #$00");
4086 AddCodeLine ("ldx #$00");
4087 AddCodeLine ("rol a");
4091 /* This one is too costly */
4099 /* If we go here, we didn't emit code. Push the lhs on stack and fall
4100 ** into the normal, non-optimized stuff. Note: The standard stuff will
4101 ** always work with ints.
4103 flags &= ~CF_FORCECHAR;
4104 g_push (flags & ~CF_CONST, 0);
4107 /* Use long way over the stack */
4108 oper (flags, val, ops);
4113 /*****************************************************************************/
4114 /* Allocating static storage */
4115 /*****************************************************************************/
4119 void g_res (unsigned n)
4120 /* Reserve static storage, n bytes */
4122 AddDataLine ("\t.res\t%u,$00", n);
4127 void g_defdata (unsigned flags, unsigned long val, long offs)
4128 /* Define data with the size given in flags */
4130 if (flags & CF_CONST) {
4132 /* Numeric constant */
4133 switch (flags & CF_TYPEMASK) {
4136 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
4140 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
4144 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
4155 /* Create the correct label name */
4156 const char* Label = GetLabelName (flags, val, offs);
4158 /* Labels are always 16 bit */
4159 AddDataLine ("\t.addr\t%s", Label);
4166 void g_defbytes (const void* Bytes, unsigned Count)
4167 /* Output a row of bytes as a constant */
4173 /* Cast the buffer pointer */
4174 const unsigned char* Data = (const unsigned char*) Bytes;
4176 /* Output the stuff */
4179 /* How many go into this line? */
4180 if ((Chunk = Count) > 16) {
4185 /* Output one line */
4186 strcpy (Buf, "\t.byte\t");
4189 B += sprintf (B, "$%02X", *Data++);
4195 /* Output the line */
4196 AddDataLine ("%s", Buf);
4202 void g_zerobytes (unsigned Count)
4203 /* Output Count bytes of data initialized with zero */
4206 AddDataLine ("\t.res\t%u,$00", Count);
4212 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4213 /* Initialize a register variable from static initialization data */
4215 /* Register variables do always have less than 128 bytes */
4216 unsigned CodeLabel = GetLocalLabel ();
4217 AddCodeLine ("ldx #$%02X", (unsigned char) (Size - 1));
4218 g_defcodelabel (CodeLabel);
4219 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4220 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4221 AddCodeLine ("dex");
4222 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4227 void g_initauto (unsigned Label, unsigned Size)
4228 /* Initialize a local variable at stack offset zero from static data */
4230 unsigned CodeLabel = GetLocalLabel ();
4232 CheckLocalOffs (Size);
4234 AddCodeLine ("ldy #$%02X", Size-1);
4235 g_defcodelabel (CodeLabel);
4236 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4237 AddCodeLine ("sta (sp),y");
4238 AddCodeLine ("dey");
4239 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4240 } else if (Size <= 256) {
4241 AddCodeLine ("ldy #$00");
4242 g_defcodelabel (CodeLabel);
4243 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4244 AddCodeLine ("sta (sp),y");
4245 AddCodeLine ("iny");
4246 AddCmpCodeIfSizeNot256 ("cpy #$%02X", Size);
4247 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4253 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4254 /* Initialize a static local variable from static initialization data */
4257 unsigned CodeLabel = GetLocalLabel ();
4258 AddCodeLine ("ldy #$%02X", Size-1);
4259 g_defcodelabel (CodeLabel);
4260 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4261 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4262 AddCodeLine ("dey");
4263 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4264 } else if (Size <= 256) {
4265 unsigned CodeLabel = GetLocalLabel ();
4266 AddCodeLine ("ldy #$00");
4267 g_defcodelabel (CodeLabel);
4268 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4269 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4270 AddCodeLine ("iny");
4271 AddCmpCodeIfSizeNot256 ("cpy #$%02X", Size);
4272 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4274 /* Use the easy way here: memcpy() */
4275 g_getimmed (CF_STATIC, VarLabel, 0);
4276 AddCodeLine ("jsr pushax");
4277 g_getimmed (CF_STATIC, InitLabel, 0);
4278 AddCodeLine ("jsr pushax");
4279 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4280 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (uintptr_t) "memcpy", 0));
4286 /*****************************************************************************/
4287 /* Switch statement */
4288 /*****************************************************************************/
4292 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4293 /* Generate code for a switch statement */
4295 unsigned NextLabel = 0;
4298 /* Setup registers and determine which compare insn to use */
4299 const char* Compare;
4302 Compare = "cmp #$%02X";
4305 Compare = "cpx #$%02X";
4308 AddCodeLine ("ldy sreg");
4309 Compare = "cpy #$%02X";
4312 AddCodeLine ("ldy sreg+1");
4313 Compare = "cpy #$%02X";
4316 Internal ("Invalid depth in g_switch: %u", Depth);
4319 /* Walk over all nodes */
4320 for (I = 0; I < CollCount (Nodes); ++I) {
4322 /* Get the next case node */
4323 CaseNode* N = CollAtUnchecked (Nodes, I);
4325 /* If we have a next label, define it */
4327 g_defcodelabel (NextLabel);
4331 /* Do the compare */
4332 AddCodeLine (Compare, CN_GetValue (N));
4334 /* If this is the last level, jump directly to the case code if found */
4337 /* Branch if equal */
4338 g_falsejump (0, CN_GetLabel (N));
4342 /* Determine the next label */
4343 if (I == CollCount (Nodes) - 1) {
4344 /* Last node means not found */
4345 g_truejump (0, DefaultLabel);
4347 /* Jump to the next check */
4348 NextLabel = GetLocalLabel ();
4349 g_truejump (0, NextLabel);
4352 /* Check the next level */
4353 g_switch (N->Nodes, DefaultLabel, Depth-1);
4358 /* If we go here, we haven't found the label */
4359 g_jump (DefaultLabel);
4364 /*****************************************************************************/
4365 /* User supplied assembler code */
4366 /*****************************************************************************/
4370 void g_asmcode (struct StrBuf* B)
4371 /* Output one line of assembler code. */
4373 AddCodeLine ("%.*s", (int) SB_GetLen (B), SB_GetConstBuf (B));