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 /*****************************************************************************/
65 /*****************************************************************************/
67 /*****************************************************************************/
71 static void typeerror (unsigned type)
72 /* Print an error message about an invalid operand type */
74 /* Special handling for floats here: */
75 if ((type & CF_TYPEMASK) == CF_FLOAT) {
76 Fatal ("Floating point type is currently unsupported");
78 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPEMASK);
84 static void CheckLocalOffs (unsigned Offs)
85 /* Check the offset into the stack for 8bit range */
88 /* Too many local vars */
89 Error ("Too many local variables");
95 static const char* GetLabelName (unsigned Flags, unsigned long Label, long Offs)
97 static char Buf [256]; /* Label name */
99 /* Create the correct label name */
100 switch (Flags & CF_ADDRMASK) {
103 /* Static memory cell */
105 xsprintf (Buf, sizeof (Buf), "%s%+ld", LocalLabelName (Label), Offs);
107 xsprintf (Buf, sizeof (Buf), "%s", LocalLabelName (Label));
114 xsprintf (Buf, sizeof (Buf), "_%s%+ld", (char*) Label, Offs);
116 xsprintf (Buf, sizeof (Buf), "_%s", (char*) Label);
121 /* Absolute address */
122 xsprintf (Buf, sizeof (Buf), "$%04X", (int)((Label+Offs) & 0xFFFF));
126 /* Variable in register bank */
127 xsprintf (Buf, sizeof (Buf), "regbank+%u", (unsigned)((Label+Offs) & 0xFFFF));
131 Internal ("Invalid address flags: %04X", Flags);
134 /* Return a pointer to the static buffer */
140 /*****************************************************************************/
141 /* Pre- and postamble */
142 /*****************************************************************************/
146 void g_preamble (void)
147 /* Generate the assembler code preamble */
149 /* Identify the compiler version */
151 AddTextLine ("; File generated by cc65 v %s", GetVersionAsString ());
154 /* Insert some object file options */
155 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %s\"",
156 GetVersionAsString ());
158 /* If we're producing code for some other CPU, switch the command set */
160 case CPU_6502: AddTextLine ("\t.setcpu\t\t\"6502\""); break;
161 case CPU_6502X: AddTextLine ("\t.setcpu\t\t\"6502X\""); break;
162 case CPU_65SC02: AddTextLine ("\t.setcpu\t\t\"65SC02\""); break;
163 case CPU_65C02: AddTextLine ("\t.setcpu\t\t\"65C02\""); break;
164 case CPU_65816: AddTextLine ("\t.setcpu\t\t\"65816\""); break;
165 case CPU_HUC6280: AddTextLine ("\t.setcpu\t\t\"HUC6280\""); break;
166 default: Internal ("Unknown CPU: %d", CPU);
170 AddTextLine ("\t.smart\t\ton");
172 /* Allow auto import for runtime library routines */
173 AddTextLine ("\t.autoimport\ton");
175 /* Switch the assembler into case sensitive mode */
176 AddTextLine ("\t.case\t\ton");
178 /* Tell the assembler if we want to generate debug info */
179 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
181 /* Import zero page variables */
182 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank");
183 AddTextLine ("\t.importzp\ttmp1, tmp2, tmp3, tmp4, ptr1, ptr2, ptr3, ptr4");
185 /* Define long branch macros */
186 AddTextLine ("\t.macpack\tlongbranch");
191 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
192 /* If debug info is enabled, place a file info into the source */
195 /* We have to place this into the global text segment, so it will
196 ** appear before all .dbg line statements.
198 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
204 /*****************************************************************************/
205 /* Segment support */
206 /*****************************************************************************/
210 void g_userodata (void)
211 /* Switch to the read only data segment */
213 UseDataSeg (SEG_RODATA);
218 void g_usedata (void)
219 /* Switch to the data segment */
221 UseDataSeg (SEG_DATA);
227 /* Switch to the bss segment */
229 UseDataSeg (SEG_BSS);
234 void g_segname (segment_t Seg)
235 /* Emit the name of a segment if necessary */
237 /* Emit a segment directive for the data style segments */
240 case SEG_RODATA: S = CS->ROData; break;
241 case SEG_DATA: S = CS->Data; break;
242 case SEG_BSS: S = CS->BSS; break;
243 default: S = 0; break;
246 DS_AddLine (S, ".segment\t\"%s\"", GetSegName (Seg));
252 /*****************************************************************************/
254 /*****************************************************************************/
258 unsigned sizeofarg (unsigned flags)
259 /* Return the size of a function argument type that is encoded in flags */
261 switch (flags & CF_TYPEMASK) {
264 return (flags & CF_FORCECHAR)? 1 : 2;
284 int pop (unsigned flags)
285 /* Pop an argument of the given size */
287 return StackPtr += sizeofarg (flags);
292 int push (unsigned flags)
293 /* Push an argument of the given size */
295 return StackPtr -= sizeofarg (flags);
300 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
301 /* The value in Offs is an offset to an address in a/x. Make sure, an object
302 ** of the type given in Flags can be loaded or stored into this address by
303 ** adding part of the offset to the address in ax, so that the remaining
304 ** offset fits into an index register. Return the remaining offset.
307 /* If the offset is too large for a byte register, add the high byte
308 ** of the offset to the primary. Beware: We need a special correction
309 ** if the offset in the low byte will overflow in the operation.
311 unsigned O = Offs & ~0xFFU;
312 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
313 /* We need to add the low byte also */
317 /* Do the correction if we need one */
319 g_inc (CF_INT | CF_CONST, O);
323 /* Return the new offset */
329 /*****************************************************************************/
330 /* Functions handling local labels */
331 /*****************************************************************************/
335 void g_defcodelabel (unsigned label)
336 /* Define a local code label */
338 CS_AddLabel (CS->Code, LocalLabelName (label));
343 void g_defdatalabel (unsigned label)
344 /* Define a local data label */
346 AddDataLine ("%s:", LocalLabelName (label));
351 void g_aliasdatalabel (unsigned label, unsigned baselabel, long offs)
352 /* Define label as a local alias for baselabel+offs */
354 /* We need an intermediate buffer here since LocalLabelName uses a
355 ** static buffer which changes with each call.
357 StrBuf L = AUTO_STRBUF_INITIALIZER;
358 SB_AppendStr (&L, LocalLabelName (label));
360 AddDataLine ("%s\t:=\t%s+%ld",
362 LocalLabelName (baselabel),
369 /*****************************************************************************/
370 /* Functions handling global labels */
371 /*****************************************************************************/
375 void g_defgloblabel (const char* Name)
376 /* Define a global label with the given name */
378 /* Global labels are always data labels */
379 AddDataLine ("_%s:", Name);
384 void g_defexport (const char* Name, int ZP)
385 /* Export the given label */
388 AddTextLine ("\t.exportzp\t_%s", Name);
390 AddTextLine ("\t.export\t\t_%s", Name);
396 void g_defimport (const char* Name, int ZP)
397 /* Import the given label */
400 AddTextLine ("\t.importzp\t_%s", Name);
402 AddTextLine ("\t.import\t\t_%s", Name);
408 void g_importstartup (void)
409 /* Forced import of the startup module */
411 AddTextLine ("\t.forceimport\t__STARTUP__");
416 void g_importmainargs (void)
417 /* Forced import of a special symbol that handles arguments to main */
419 AddTextLine ("\t.forceimport\tinitmainargs");
424 /*****************************************************************************/
425 /* Function entry and exit */
426 /*****************************************************************************/
430 /* Remember the argument size of a function. The variable is set by g_enter
431 ** and used by g_leave. If the function gets its argument size by the caller
432 ** (variable param list or function without prototype), g_enter will set the
438 void g_enter (unsigned flags, unsigned argsize)
439 /* Function prologue */
441 if ((flags & CF_FIXARGC) != 0) {
442 /* Just remember the argument size for the leave */
446 AddCodeLine ("jsr enter");
453 /* Function epilogue */
455 /* How many bytes of locals do we have to drop? */
456 unsigned ToDrop = (unsigned) -StackPtr;
458 /* If we didn't have a variable argument list, don't call leave */
461 /* Drop stackframe if needed */
462 g_drop (ToDrop + funcargs);
464 } else if (StackPtr != 0) {
466 /* We've a stack frame to drop */
468 g_drop (ToDrop); /* Inlines the code */
469 AddCodeLine ("jsr leave");
471 AddCodeLine ("ldy #$%02X", ToDrop);
472 AddCodeLine ("jsr leavey");
477 /* Nothing to drop */
478 AddCodeLine ("jsr leave");
482 /* Add the final rts */
488 /*****************************************************************************/
489 /* Register variables */
490 /*****************************************************************************/
494 void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
495 /* Swap a register variable with a location on the stack */
497 /* Calculate the actual stack offset and check it */
498 StackOffs -= StackPtr;
499 CheckLocalOffs (StackOffs);
502 AddCodeLine ("ldy #$%02X", StackOffs & 0xFF);
505 if (IS_Get (&CodeSizeFactor) < 165) {
506 AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
507 AddCodeLine ("jsr regswap1");
509 AddCodeLine ("lda (sp),y");
510 AddCodeLine ("ldx regbank%+d", RegOffs);
511 AddCodeLine ("sta regbank%+d", RegOffs);
513 AddCodeLine ("sta (sp),y");
516 } else if (Bytes == 2) {
518 AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
519 AddCodeLine ("jsr regswap2");
523 AddCodeLine ("ldx #$%02X", RegOffs & 0xFF);
524 AddCodeLine ("lda #$%02X", Bytes & 0xFF);
525 AddCodeLine ("jsr regswap");
531 void g_save_regvars (int RegOffs, unsigned Bytes)
532 /* Save register variables */
534 /* Don't loop for up to two bytes */
537 AddCodeLine ("lda regbank%+d", RegOffs);
538 AddCodeLine ("jsr pusha");
540 } else if (Bytes == 2) {
542 AddCodeLine ("lda regbank%+d", RegOffs);
543 AddCodeLine ("ldx regbank%+d", RegOffs+1);
544 AddCodeLine ("jsr pushax");
548 /* More than two bytes - loop */
549 unsigned Label = GetLocalLabel ();
551 AddCodeLine ("ldy #$%02X", (unsigned char) (Bytes - 1));
552 AddCodeLine ("ldx #$%02X", (unsigned char) Bytes);
553 g_defcodelabel (Label);
554 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
555 AddCodeLine ("sta (sp),y");
558 AddCodeLine ("bne %s", LocalLabelName (Label));
562 /* We pushed stuff, correct the stack pointer */
568 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
569 /* Restore register variables */
571 /* Calculate the actual stack offset and check it */
572 StackOffs -= StackPtr;
573 CheckLocalOffs (StackOffs);
575 /* Don't loop for up to two bytes */
578 AddCodeLine ("ldy #$%02X", StackOffs);
579 AddCodeLine ("lda (sp),y");
580 AddCodeLine ("sta regbank%+d", RegOffs);
582 } else if (Bytes == 2) {
584 AddCodeLine ("ldy #$%02X", StackOffs);
585 AddCodeLine ("lda (sp),y");
586 AddCodeLine ("sta regbank%+d", RegOffs);
588 AddCodeLine ("lda (sp),y");
589 AddCodeLine ("sta regbank%+d", RegOffs+1);
591 } else if (Bytes == 3 && IS_Get (&CodeSizeFactor) >= 133) {
593 AddCodeLine ("ldy #$%02X", StackOffs);
594 AddCodeLine ("lda (sp),y");
595 AddCodeLine ("sta regbank%+d", RegOffs);
597 AddCodeLine ("lda (sp),y");
598 AddCodeLine ("sta regbank%+d", RegOffs+1);
600 AddCodeLine ("lda (sp),y");
601 AddCodeLine ("sta regbank%+d", RegOffs+2);
603 } else if (StackOffs <= RegOffs) {
605 /* More bytes, but the relation between the register offset in the
606 ** register bank and the stack offset allows us to generate short
607 ** code that uses just one index register.
609 unsigned Label = GetLocalLabel ();
610 AddCodeLine ("ldy #$%02X", StackOffs);
611 g_defcodelabel (Label);
612 AddCodeLine ("lda (sp),y");
613 AddCodeLine ("sta regbank%+d,y", RegOffs - StackOffs);
615 AddCodeLine ("cpy #$%02X", StackOffs + Bytes);
616 AddCodeLine ("bne %s", LocalLabelName (Label));
620 /* OK, this is the generic code. We need to save X because the
621 ** caller will only save A.
623 unsigned Label = GetLocalLabel ();
624 AddCodeLine ("stx tmp1");
625 AddCodeLine ("ldy #$%02X", (unsigned char) (StackOffs + Bytes - 1));
626 AddCodeLine ("ldx #$%02X", (unsigned char) (Bytes - 1));
627 g_defcodelabel (Label);
628 AddCodeLine ("lda (sp),y");
629 AddCodeLine ("sta regbank%+d,x", RegOffs);
632 AddCodeLine ("bpl %s", LocalLabelName (Label));
633 AddCodeLine ("ldx tmp1");
640 /*****************************************************************************/
641 /* Fetching memory cells */
642 /*****************************************************************************/
646 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
647 /* Load a constant into the primary register */
649 unsigned char B1, B2, B3, B4;
653 if ((Flags & CF_CONST) != 0) {
655 /* Numeric constant */
656 switch (Flags & CF_TYPEMASK) {
659 if ((Flags & CF_FORCECHAR) != 0) {
660 AddCodeLine ("lda #$%02X", (unsigned char) Val);
665 AddCodeLine ("ldx #$%02X", (unsigned char) (Val >> 8));
666 AddCodeLine ("lda #$%02X", (unsigned char) Val);
670 /* Split the value into 4 bytes */
671 B1 = (unsigned char) (Val >> 0);
672 B2 = (unsigned char) (Val >> 8);
673 B3 = (unsigned char) (Val >> 16);
674 B4 = (unsigned char) (Val >> 24);
676 /* Remember which bytes are done */
680 AddCodeLine ("ldx #$%02X", B2);
683 AddCodeLine ("stx sreg");
687 AddCodeLine ("stx sreg+1");
690 if ((Done & 0x04) == 0 && B1 != B3) {
691 AddCodeLine ("lda #$%02X", B3);
692 AddCodeLine ("sta sreg");
695 if ((Done & 0x08) == 0 && B1 != B4) {
696 AddCodeLine ("lda #$%02X", B4);
697 AddCodeLine ("sta sreg+1");
700 AddCodeLine ("lda #$%02X", B1);
702 if ((Done & 0x04) == 0) {
704 AddCodeLine ("sta sreg");
706 if ((Done & 0x08) == 0) {
708 AddCodeLine ("sta sreg+1");
720 /* Some sort of label */
721 const char* Label = GetLabelName (Flags, Val, Offs);
723 /* Load the address into the primary */
724 AddCodeLine ("lda #<(%s)", Label);
725 AddCodeLine ("ldx #>(%s)", Label);
732 void g_getstatic (unsigned flags, unsigned long label, long offs)
733 /* Fetch an static memory cell into the primary register */
735 /* Create the correct label name */
736 const char* lbuf = GetLabelName (flags, label, offs);
738 /* Check the size and generate the correct load operation */
739 switch (flags & CF_TYPEMASK) {
742 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
743 AddCodeLine ("lda %s", lbuf); /* load A from the label */
745 AddCodeLine ("ldx #$00");
746 AddCodeLine ("lda %s", lbuf); /* load A from the label */
747 if (!(flags & CF_UNSIGNED)) {
748 /* Must sign extend */
749 unsigned L = GetLocalLabel ();
750 AddCodeLine ("bpl %s", LocalLabelName (L));
758 AddCodeLine ("lda %s", lbuf);
759 if (flags & CF_TEST) {
760 AddCodeLine ("ora %s+1", lbuf);
762 AddCodeLine ("ldx %s+1", lbuf);
767 if (flags & CF_TEST) {
768 AddCodeLine ("lda %s+3", lbuf);
769 AddCodeLine ("ora %s+2", lbuf);
770 AddCodeLine ("ora %s+1", lbuf);
771 AddCodeLine ("ora %s+0", lbuf);
773 AddCodeLine ("lda %s+3", lbuf);
774 AddCodeLine ("sta sreg+1");
775 AddCodeLine ("lda %s+2", lbuf);
776 AddCodeLine ("sta sreg");
777 AddCodeLine ("ldx %s+1", lbuf);
778 AddCodeLine ("lda %s", lbuf);
790 void g_getlocal (unsigned Flags, int Offs)
791 /* Fetch specified local object (local var). */
794 switch (Flags & CF_TYPEMASK) {
797 CheckLocalOffs (Offs);
798 if ((Flags & CF_FORCECHAR) || (Flags & CF_TEST)) {
799 AddCodeLine ("ldy #$%02X", Offs);
800 AddCodeLine ("lda (sp),y");
802 AddCodeLine ("ldy #$%02X", Offs);
803 AddCodeLine ("ldx #$00");
804 AddCodeLine ("lda (sp),y");
805 if ((Flags & CF_UNSIGNED) == 0) {
806 unsigned L = GetLocalLabel();
807 AddCodeLine ("bpl %s", LocalLabelName (L));
815 CheckLocalOffs (Offs + 1);
816 AddCodeLine ("ldy #$%02X", (unsigned char) (Offs+1));
817 if (Flags & CF_TEST) {
818 AddCodeLine ("lda (sp),y");
820 AddCodeLine ("ora (sp),y");
822 AddCodeLine ("jsr ldaxysp");
827 CheckLocalOffs (Offs + 3);
828 AddCodeLine ("ldy #$%02X", (unsigned char) (Offs+3));
829 AddCodeLine ("jsr ldeaxysp");
830 if (Flags & CF_TEST) {
842 void g_getind (unsigned Flags, unsigned Offs)
843 /* Fetch the specified object type indirect through the primary register
844 ** into the primary register
847 /* If the offset is greater than 255, add the part that is > 255 to
848 ** the primary. This way we get an easy addition and use the low byte
851 Offs = MakeByteOffs (Flags, Offs);
853 /* Handle the indirect fetch */
854 switch (Flags & CF_TYPEMASK) {
857 /* Character sized */
858 AddCodeLine ("ldy #$%02X", Offs);
859 if (Flags & CF_UNSIGNED) {
860 AddCodeLine ("jsr ldauidx");
862 AddCodeLine ("jsr ldaidx");
867 if (Flags & CF_TEST) {
868 AddCodeLine ("ldy #$%02X", Offs);
869 AddCodeLine ("sta ptr1");
870 AddCodeLine ("stx ptr1+1");
871 AddCodeLine ("lda (ptr1),y");
873 AddCodeLine ("ora (ptr1),y");
875 AddCodeLine ("ldy #$%02X", Offs+1);
876 AddCodeLine ("jsr ldaxidx");
881 AddCodeLine ("ldy #$%02X", Offs+3);
882 AddCodeLine ("jsr ldeaxidx");
883 if (Flags & CF_TEST) {
896 void g_leasp (int Offs)
897 /* Fetch the address of the specified symbol into the primary register */
899 unsigned char Lo, Hi;
901 /* Calculate the offset relative to sp */
904 /* Get low and high byte */
905 Lo = (unsigned char) Offs;
906 Hi = (unsigned char) (Offs >> 8);
911 AddCodeLine ("lda sp");
912 AddCodeLine ("ldx sp+1");
917 AddCodeLine ("lda sp+1");
919 AddCodeLine ("adc #$%02X", Hi);
921 AddCodeLine ("lda sp");
923 } else if (Hi == 0) {
925 if (IS_Get (&CodeSizeFactor) < 200) {
926 /* 8 bit offset with subroutine call */
927 AddCodeLine ("lda #$%02X", Lo);
928 AddCodeLine ("jsr leaa0sp");
930 /* 8 bit offset inlined */
931 unsigned L = GetLocalLabel ();
932 AddCodeLine ("lda sp");
933 AddCodeLine ("ldx sp+1");
935 AddCodeLine ("adc #$%02X", Lo);
936 AddCodeLine ("bcc %s", LocalLabelName (L));
940 } else if (IS_Get (&CodeSizeFactor) < 170) {
941 /* Full 16 bit offset with subroutine call */
942 AddCodeLine ("lda #$%02X", Lo);
943 AddCodeLine ("ldx #$%02X", Hi);
944 AddCodeLine ("jsr leaaxsp");
946 /* Full 16 bit offset inlined */
947 AddCodeLine ("lda sp");
949 AddCodeLine ("adc #$%02X", Lo);
951 AddCodeLine ("lda sp+1");
952 AddCodeLine ("adc #$%02X", Hi);
960 void g_leavariadic (int Offs)
961 /* Fetch the address of a parameter in a variadic function into the primary
965 unsigned ArgSizeOffs;
967 /* Calculate the offset relative to sp */
970 /* Get the offset of the parameter which is stored at sp+0 on function
971 ** entry and check if this offset is reachable with a byte offset.
973 CHECK (StackPtr <= 0);
974 ArgSizeOffs = -StackPtr;
975 CheckLocalOffs (ArgSizeOffs);
977 /* Get the size of all parameters. */
978 AddCodeLine ("ldy #$%02X", ArgSizeOffs);
979 AddCodeLine ("lda (sp),y");
981 /* Add the value of the stackpointer */
982 if (IS_Get (&CodeSizeFactor) > 250) {
983 unsigned L = GetLocalLabel();
984 AddCodeLine ("ldx sp+1");
986 AddCodeLine ("adc sp");
987 AddCodeLine ("bcc %s", LocalLabelName (L));
991 AddCodeLine ("ldx #$00");
992 AddCodeLine ("jsr leaaxsp");
995 /* Add the offset to the primary */
997 g_inc (CF_INT | CF_CONST, Offs);
998 } else if (Offs < 0) {
999 g_dec (CF_INT | CF_CONST, -Offs);
1005 /*****************************************************************************/
1006 /* Store into memory */
1007 /*****************************************************************************/
1011 void g_putstatic (unsigned flags, unsigned long label, long offs)
1012 /* Store the primary register into the specified static memory cell */
1014 /* Create the correct label name */
1015 const char* lbuf = GetLabelName (flags, label, offs);
1017 /* Check the size and generate the correct store operation */
1018 switch (flags & CF_TYPEMASK) {
1021 AddCodeLine ("sta %s", lbuf);
1025 AddCodeLine ("sta %s", lbuf);
1026 AddCodeLine ("stx %s+1", lbuf);
1030 AddCodeLine ("sta %s", lbuf);
1031 AddCodeLine ("stx %s+1", lbuf);
1032 AddCodeLine ("ldy sreg");
1033 AddCodeLine ("sty %s+2", lbuf);
1034 AddCodeLine ("ldy sreg+1");
1035 AddCodeLine ("sty %s+3", lbuf);
1046 void g_putlocal (unsigned Flags, int Offs, long Val)
1047 /* Put data into local object. */
1050 CheckLocalOffs (Offs);
1051 switch (Flags & CF_TYPEMASK) {
1054 if (Flags & CF_CONST) {
1055 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1057 AddCodeLine ("ldy #$%02X", Offs);
1058 AddCodeLine ("sta (sp),y");
1062 if (Flags & CF_CONST) {
1063 AddCodeLine ("ldy #$%02X", Offs+1);
1064 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1065 AddCodeLine ("sta (sp),y");
1066 if ((Flags & CF_NOKEEP) == 0) {
1067 /* Place high byte into X */
1068 AddCodeLine ("tax");
1070 if ((Val & 0xFF) == Offs+1) {
1071 /* The value we need is already in Y */
1072 AddCodeLine ("tya");
1073 AddCodeLine ("dey");
1075 AddCodeLine ("dey");
1076 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1078 AddCodeLine ("sta (sp),y");
1080 AddCodeLine ("ldy #$%02X", Offs);
1081 if ((Flags & CF_NOKEEP) == 0 || IS_Get (&CodeSizeFactor) < 160) {
1082 AddCodeLine ("jsr staxysp");
1084 AddCodeLine ("sta (sp),y");
1085 AddCodeLine ("iny");
1086 AddCodeLine ("txa");
1087 AddCodeLine ("sta (sp),y");
1093 if (Flags & CF_CONST) {
1094 g_getimmed (Flags, Val, 0);
1096 AddCodeLine ("ldy #$%02X", Offs);
1097 AddCodeLine ("jsr steaxysp");
1108 void g_putind (unsigned Flags, unsigned Offs)
1109 /* Store the specified object type in the primary register at the address
1110 ** on the top of the stack
1113 /* We can handle offsets below $100 directly, larger offsets must be added
1114 ** to the address. Since a/x is in use, best code is achieved by adding
1115 ** just the high byte. Be sure to check if the low byte will overflow while
1118 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1120 /* Overflow - we need to add the low byte also */
1121 AddCodeLine ("ldy #$00");
1122 AddCodeLine ("clc");
1123 AddCodeLine ("pha");
1124 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1125 AddCodeLine ("adc (sp),y");
1126 AddCodeLine ("sta (sp),y");
1127 AddCodeLine ("iny");
1128 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1129 AddCodeLine ("adc (sp),y");
1130 AddCodeLine ("sta (sp),y");
1131 AddCodeLine ("pla");
1133 /* Complete address is on stack, new offset is zero */
1136 } else if ((Offs & 0xFF00) != 0) {
1138 /* We can just add the high byte */
1139 AddCodeLine ("ldy #$01");
1140 AddCodeLine ("clc");
1141 AddCodeLine ("pha");
1142 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1143 AddCodeLine ("adc (sp),y");
1144 AddCodeLine ("sta (sp),y");
1145 AddCodeLine ("pla");
1147 /* Offset is now just the low byte */
1151 /* Check the size and determine operation */
1152 AddCodeLine ("ldy #$%02X", Offs);
1153 switch (Flags & CF_TYPEMASK) {
1156 AddCodeLine ("jsr staspidx");
1160 AddCodeLine ("jsr staxspidx");
1164 AddCodeLine ("jsr steaxspidx");
1172 /* Pop the argument which is always a pointer */
1178 /*****************************************************************************/
1179 /* type conversion and similiar stuff */
1180 /*****************************************************************************/
1184 void g_toslong (unsigned flags)
1185 /* Make sure, the value on TOS is a long. Convert if necessary */
1187 switch (flags & CF_TYPEMASK) {
1191 if (flags & CF_UNSIGNED) {
1192 AddCodeLine ("jsr tosulong");
1194 AddCodeLine ("jsr toslong");
1209 void g_tosint (unsigned flags)
1210 /* Make sure, the value on TOS is an int. Convert if necessary */
1212 switch (flags & CF_TYPEMASK) {
1219 AddCodeLine ("jsr tosint");
1230 static void g_regchar (unsigned Flags)
1231 /* Make sure, the value in the primary register is in the range of char. Truncate if necessary */
1235 AddCodeLine ("ldx #$00");
1237 if ((Flags & CF_UNSIGNED) == 0) {
1239 L = GetLocalLabel();
1240 AddCodeLine ("cmp #$80");
1241 AddCodeLine ("bcc %s", LocalLabelName (L));
1242 AddCodeLine ("dex");
1249 void g_regint (unsigned Flags)
1250 /* Make sure, the value in the primary register an int. Convert if necessary */
1252 switch (Flags & CF_TYPEMASK) {
1255 if (Flags & CF_FORCECHAR) {
1256 /* Conversion is from char */
1272 void g_reglong (unsigned Flags)
1273 /* Make sure, the value in the primary register a long. Convert if necessary */
1275 switch (Flags & CF_TYPEMASK) {
1278 if (Flags & CF_FORCECHAR) {
1279 /* Conversion is from char */
1280 if (Flags & CF_UNSIGNED) {
1281 if (IS_Get (&CodeSizeFactor) >= 200) {
1282 AddCodeLine ("ldx #$00");
1283 AddCodeLine ("stx sreg");
1284 AddCodeLine ("stx sreg+1");
1286 AddCodeLine ("jsr aulong");
1289 if (IS_Get (&CodeSizeFactor) >= 366) {
1291 AddCodeLine ("stx sreg");
1292 AddCodeLine ("stx sreg+1");
1294 AddCodeLine ("jsr along");
1301 if (Flags & CF_UNSIGNED) {
1302 if (IS_Get (&CodeSizeFactor) >= 200) {
1303 AddCodeLine ("ldy #$00");
1304 AddCodeLine ("sty sreg");
1305 AddCodeLine ("sty sreg+1");
1307 AddCodeLine ("jsr axulong");
1310 AddCodeLine ("jsr axlong");
1324 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1325 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1326 ** value, that corresponds to the value on TOS, rhs corresponds to the value
1327 ** in (e)ax. The return value is the the flags value for the resulting type.
1330 unsigned ltype, rtype;
1333 /* Get the type spec from the flags */
1334 ltype = lhs & CF_TYPEMASK;
1335 rtype = rhs & CF_TYPEMASK;
1337 /* Check if a conversion is needed */
1338 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1339 /* We must promote the primary register to long */
1341 /* Get the new rhs type */
1342 rhs = (rhs & ~CF_TYPEMASK) | CF_LONG;
1344 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1345 /* We must promote the lhs to long */
1351 /* Get the new rhs type */
1352 lhs = (lhs & ~CF_TYPEMASK) | CF_LONG;
1356 /* Determine the result type for the operation:
1357 ** - The result is const if both operands are const.
1358 ** - The result is unsigned if one of the operands is unsigned.
1359 ** - The result is long if one of the operands is long.
1360 ** - Otherwise the result is int sized.
1362 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1363 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1364 if (rtype == CF_LONG || ltype == CF_LONG) {
1374 unsigned g_typecast (unsigned lhs, unsigned rhs)
1375 /* Cast the value in the primary register to the operand size that is flagged
1376 ** by the lhs value. Return the result value.
1379 /* Check if a conversion is needed */
1380 if ((rhs & CF_CONST) == 0) {
1381 switch (lhs & CF_TYPEMASK) {
1384 /* We must promote the primary register to long */
1389 /* We must promote the primary register to int */
1394 /* We must truncate the primary register to char */
1403 /* Do not need any other action. If the left type is int, and the primary
1404 ** register is long, it will be automagically truncated. If the right hand
1405 ** side is const, it is not located in the primary register and handled by
1406 ** the expression parser code.
1409 /* Result is const if the right hand side was const */
1410 lhs |= (rhs & CF_CONST);
1412 /* The resulting type is that of the left hand side (that's why you called
1413 ** this function :-)
1420 void g_scale (unsigned flags, long val)
1421 /* Scale the value in the primary register by the given value. If val is positive,
1422 ** scale up, is val is negative, scale down. This function is used to scale
1423 ** the operands or results of pointer arithmetic by the size of the type, the
1424 ** pointer points to.
1429 /* Value may not be zero */
1431 Internal ("Data type has no size");
1432 } else if (val > 0) {
1435 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1437 /* Factor is 2, 4, 8 and 16, use special function */
1438 switch (flags & CF_TYPEMASK) {
1441 if (flags & CF_FORCECHAR) {
1443 AddCodeLine ("asl a");
1450 if (flags & CF_UNSIGNED) {
1451 AddCodeLine ("jsr shlax%d", p2);
1453 AddCodeLine ("jsr aslax%d", p2);
1458 if (flags & CF_UNSIGNED) {
1459 AddCodeLine ("jsr shleax%d", p2);
1461 AddCodeLine ("jsr asleax%d", p2);
1470 } else if (val != 1) {
1472 /* Use a multiplication instead */
1473 g_mul (flags | CF_CONST, val);
1481 if ((p2 = PowerOf2 (val)) > 0 && p2 <= 4) {
1483 /* Factor is 2, 4, 8 and 16 use special function */
1484 switch (flags & CF_TYPEMASK) {
1487 if (flags & CF_FORCECHAR) {
1488 if (flags & CF_UNSIGNED) {
1490 AddCodeLine ("lsr a");
1493 } else if (p2 <= 2) {
1494 AddCodeLine ("cmp #$80");
1495 AddCodeLine ("ror a");
1502 if (flags & CF_UNSIGNED) {
1503 AddCodeLine ("jsr lsrax%d", p2);
1505 AddCodeLine ("jsr asrax%d", p2);
1510 if (flags & CF_UNSIGNED) {
1511 AddCodeLine ("jsr lsreax%d", p2);
1513 AddCodeLine ("jsr asreax%d", p2);
1522 } else if (val != 1) {
1524 /* Use a division instead */
1525 g_div (flags | CF_CONST, val);
1533 /*****************************************************************************/
1534 /* Adds and subs of variables fix a fixed address */
1535 /*****************************************************************************/
1539 void g_addlocal (unsigned flags, int offs)
1540 /* Add a local variable to ax */
1544 /* Correct the offset and check it */
1546 CheckLocalOffs (offs);
1548 switch (flags & CF_TYPEMASK) {
1551 L = GetLocalLabel();
1552 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1553 AddCodeLine ("clc");
1554 AddCodeLine ("adc (sp),y");
1555 AddCodeLine ("bcc %s", LocalLabelName (L));
1556 AddCodeLine ("inx");
1561 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1562 AddCodeLine ("clc");
1563 AddCodeLine ("adc (sp),y");
1564 AddCodeLine ("pha");
1565 AddCodeLine ("txa");
1566 AddCodeLine ("iny");
1567 AddCodeLine ("adc (sp),y");
1568 AddCodeLine ("tax");
1569 AddCodeLine ("pla");
1573 /* Do it the old way */
1575 g_getlocal (flags, offs);
1587 void g_addstatic (unsigned flags, unsigned long label, long offs)
1588 /* Add a static variable to ax */
1592 /* Create the correct label name */
1593 const char* lbuf = GetLabelName (flags, label, offs);
1595 switch (flags & CF_TYPEMASK) {
1598 L = GetLocalLabel();
1599 AddCodeLine ("clc");
1600 AddCodeLine ("adc %s", lbuf);
1601 AddCodeLine ("bcc %s", LocalLabelName (L));
1602 AddCodeLine ("inx");
1607 AddCodeLine ("clc");
1608 AddCodeLine ("adc %s", lbuf);
1609 AddCodeLine ("tay");
1610 AddCodeLine ("txa");
1611 AddCodeLine ("adc %s+1", lbuf);
1612 AddCodeLine ("tax");
1613 AddCodeLine ("tya");
1617 /* Do it the old way */
1619 g_getstatic (flags, label, offs);
1631 /*****************************************************************************/
1632 /* Special op= functions */
1633 /*****************************************************************************/
1637 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1639 /* Emit += for a static variable */
1641 /* Create the correct label name */
1642 const char* lbuf = GetLabelName (flags, label, offs);
1644 /* Check the size and determine operation */
1645 switch (flags & CF_TYPEMASK) {
1648 if (flags & CF_FORCECHAR) {
1649 AddCodeLine ("ldx #$00");
1650 if (flags & CF_CONST) {
1652 AddCodeLine ("inc %s", lbuf);
1653 AddCodeLine ("lda %s", lbuf);
1655 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1656 AddCodeLine ("clc");
1657 AddCodeLine ("adc %s", lbuf);
1658 AddCodeLine ("sta %s", lbuf);
1661 AddCodeLine ("clc");
1662 AddCodeLine ("adc %s", lbuf);
1663 AddCodeLine ("sta %s", lbuf);
1665 if ((flags & CF_UNSIGNED) == 0) {
1666 unsigned L = GetLocalLabel();
1667 AddCodeLine ("bpl %s", LocalLabelName (L));
1668 AddCodeLine ("dex");
1676 if (flags & CF_CONST) {
1678 unsigned L = GetLocalLabel ();
1679 AddCodeLine ("inc %s", lbuf);
1680 AddCodeLine ("bne %s", LocalLabelName (L));
1681 AddCodeLine ("inc %s+1", lbuf);
1683 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1684 AddCodeLine ("ldx %s+1", lbuf);
1686 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1687 AddCodeLine ("clc");
1688 AddCodeLine ("adc %s", lbuf);
1689 AddCodeLine ("sta %s", lbuf);
1691 unsigned L = GetLocalLabel ();
1692 AddCodeLine ("bcc %s", LocalLabelName (L));
1693 AddCodeLine ("inc %s+1", lbuf);
1695 AddCodeLine ("ldx %s+1", lbuf);
1697 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1698 AddCodeLine ("adc %s+1", lbuf);
1699 AddCodeLine ("sta %s+1", lbuf);
1700 AddCodeLine ("tax");
1701 AddCodeLine ("lda %s", lbuf);
1705 AddCodeLine ("clc");
1706 AddCodeLine ("adc %s", lbuf);
1707 AddCodeLine ("sta %s", lbuf);
1708 AddCodeLine ("txa");
1709 AddCodeLine ("adc %s+1", lbuf);
1710 AddCodeLine ("sta %s+1", lbuf);
1711 AddCodeLine ("tax");
1712 AddCodeLine ("lda %s", lbuf);
1717 if (flags & CF_CONST) {
1719 AddCodeLine ("ldy #<(%s)", lbuf);
1720 AddCodeLine ("sty ptr1");
1721 AddCodeLine ("ldy #>(%s)", lbuf);
1723 AddCodeLine ("jsr laddeq1");
1725 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1726 AddCodeLine ("jsr laddeqa");
1729 g_getstatic (flags, label, offs);
1731 g_putstatic (flags, label, offs);
1734 AddCodeLine ("ldy #<(%s)", lbuf);
1735 AddCodeLine ("sty ptr1");
1736 AddCodeLine ("ldy #>(%s)", lbuf);
1737 AddCodeLine ("jsr laddeq");
1748 void g_addeqlocal (unsigned flags, int Offs, unsigned long val)
1749 /* Emit += for a local variable */
1751 /* Calculate the true offset, check it, load it into Y */
1753 CheckLocalOffs (Offs);
1755 /* Check the size and determine operation */
1756 switch (flags & CF_TYPEMASK) {
1759 if (flags & CF_FORCECHAR) {
1760 AddCodeLine ("ldy #$%02X", Offs);
1761 AddCodeLine ("ldx #$00");
1762 if (flags & CF_CONST) {
1763 AddCodeLine ("clc");
1764 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1765 AddCodeLine ("adc (sp),y");
1766 AddCodeLine ("sta (sp),y");
1768 AddCodeLine ("clc");
1769 AddCodeLine ("adc (sp),y");
1770 AddCodeLine ("sta (sp),y");
1772 if ((flags & CF_UNSIGNED) == 0) {
1773 unsigned L = GetLocalLabel();
1774 AddCodeLine ("bpl %s", LocalLabelName (L));
1775 AddCodeLine ("dex");
1783 AddCodeLine ("ldy #$%02X", Offs);
1784 if (flags & CF_CONST) {
1785 if (IS_Get (&CodeSizeFactor) >= 400) {
1786 AddCodeLine ("clc");
1787 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1788 AddCodeLine ("adc (sp),y");
1789 AddCodeLine ("sta (sp),y");
1790 AddCodeLine ("iny");
1791 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1792 AddCodeLine ("adc (sp),y");
1793 AddCodeLine ("sta (sp),y");
1794 AddCodeLine ("tax");
1795 AddCodeLine ("dey");
1796 AddCodeLine ("lda (sp),y");
1798 g_getimmed (flags, val, 0);
1799 AddCodeLine ("jsr addeqysp");
1802 AddCodeLine ("jsr addeqysp");
1807 if (flags & CF_CONST) {
1808 g_getimmed (flags, val, 0);
1810 AddCodeLine ("ldy #$%02X", Offs);
1811 AddCodeLine ("jsr laddeqysp");
1821 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1822 /* Emit += for the location with address in ax */
1824 /* If the offset is too large for a byte register, add the high byte
1825 ** of the offset to the primary. Beware: We need a special correction
1826 ** if the offset in the low byte will overflow in the operation.
1828 offs = MakeByteOffs (flags, offs);
1830 /* Check the size and determine operation */
1831 switch (flags & CF_TYPEMASK) {
1834 AddCodeLine ("sta ptr1");
1835 AddCodeLine ("stx ptr1+1");
1836 AddCodeLine ("ldy #$%02X", offs);
1837 AddCodeLine ("ldx #$00");
1838 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1839 AddCodeLine ("clc");
1840 AddCodeLine ("adc (ptr1),y");
1841 AddCodeLine ("sta (ptr1),y");
1846 AddCodeLine ("jsr pushax"); /* Push the address */
1847 push (CF_PTR); /* Correct the internal sp */
1848 g_getind (flags, offs); /* Fetch the value */
1849 g_inc (flags, val); /* Increment value in primary */
1850 g_putind (flags, offs); /* Store the value back */
1860 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1862 /* Emit -= for a static variable */
1864 /* Create the correct label name */
1865 const char* lbuf = GetLabelName (flags, label, offs);
1867 /* Check the size and determine operation */
1868 switch (flags & CF_TYPEMASK) {
1871 if (flags & CF_FORCECHAR) {
1872 AddCodeLine ("ldx #$00");
1873 if (flags & CF_CONST) {
1875 AddCodeLine ("dec %s", lbuf);
1876 AddCodeLine ("lda %s", lbuf);
1878 AddCodeLine ("lda %s", lbuf);
1879 AddCodeLine ("sec");
1880 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1881 AddCodeLine ("sta %s", lbuf);
1884 AddCodeLine ("eor #$FF");
1885 AddCodeLine ("sec");
1886 AddCodeLine ("adc %s", lbuf);
1887 AddCodeLine ("sta %s", lbuf);
1889 if ((flags & CF_UNSIGNED) == 0) {
1890 unsigned L = GetLocalLabel();
1891 AddCodeLine ("bpl %s", LocalLabelName (L));
1892 AddCodeLine ("dex");
1900 if (flags & CF_CONST) {
1901 AddCodeLine ("lda %s", lbuf);
1902 AddCodeLine ("sec");
1903 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1904 AddCodeLine ("sta %s", lbuf);
1906 unsigned L = GetLocalLabel ();
1907 AddCodeLine ("bcs %s", LocalLabelName (L));
1908 AddCodeLine ("dec %s+1", lbuf);
1910 AddCodeLine ("ldx %s+1", lbuf);
1912 AddCodeLine ("lda %s+1", lbuf);
1913 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1914 AddCodeLine ("sta %s+1", lbuf);
1915 AddCodeLine ("tax");
1916 AddCodeLine ("lda %s", lbuf);
1919 AddCodeLine ("eor #$FF");
1920 AddCodeLine ("sec");
1921 AddCodeLine ("adc %s", lbuf);
1922 AddCodeLine ("sta %s", lbuf);
1923 AddCodeLine ("txa");
1924 AddCodeLine ("eor #$FF");
1925 AddCodeLine ("adc %s+1", lbuf);
1926 AddCodeLine ("sta %s+1", lbuf);
1927 AddCodeLine ("tax");
1928 AddCodeLine ("lda %s", lbuf);
1933 if (flags & CF_CONST) {
1935 AddCodeLine ("ldy #<(%s)", lbuf);
1936 AddCodeLine ("sty ptr1");
1937 AddCodeLine ("ldy #>(%s)", lbuf);
1938 AddCodeLine ("lda #$%02X", (unsigned char)val);
1939 AddCodeLine ("jsr lsubeqa");
1941 g_getstatic (flags, label, offs);
1943 g_putstatic (flags, label, offs);
1946 AddCodeLine ("ldy #<(%s)", lbuf);
1947 AddCodeLine ("sty ptr1");
1948 AddCodeLine ("ldy #>(%s)", lbuf);
1949 AddCodeLine ("jsr lsubeq");
1960 void g_subeqlocal (unsigned flags, int Offs, unsigned long val)
1961 /* Emit -= for a local variable */
1963 /* Calculate the true offset, check it, load it into Y */
1965 CheckLocalOffs (Offs);
1967 /* Check the size and determine operation */
1968 switch (flags & CF_TYPEMASK) {
1971 if (flags & CF_FORCECHAR) {
1972 AddCodeLine ("ldy #$%02X", Offs);
1973 AddCodeLine ("ldx #$00");
1974 if (flags & CF_CONST) {
1975 AddCodeLine ("lda (sp),y");
1976 AddCodeLine ("sec");
1977 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1979 AddCodeLine ("eor #$FF");
1980 AddCodeLine ("sec");
1981 AddCodeLine ("adc (sp),y");
1983 AddCodeLine ("sta (sp),y");
1984 if ((flags & CF_UNSIGNED) == 0) {
1985 unsigned L = GetLocalLabel();
1986 AddCodeLine ("bpl %s", LocalLabelName (L));
1987 AddCodeLine ("dex");
1995 if (flags & CF_CONST) {
1996 g_getimmed (flags, val, 0);
1998 AddCodeLine ("ldy #$%02X", Offs);
1999 AddCodeLine ("jsr subeqysp");
2003 if (flags & CF_CONST) {
2004 g_getimmed (flags, val, 0);
2006 AddCodeLine ("ldy #$%02X", Offs);
2007 AddCodeLine ("jsr lsubeqysp");
2017 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2018 /* Emit -= for the location with address in ax */
2020 /* If the offset is too large for a byte register, add the high byte
2021 ** of the offset to the primary. Beware: We need a special correction
2022 ** if the offset in the low byte will overflow in the operation.
2024 offs = MakeByteOffs (flags, offs);
2026 /* Check the size and determine operation */
2027 switch (flags & CF_TYPEMASK) {
2030 AddCodeLine ("sta ptr1");
2031 AddCodeLine ("stx ptr1+1");
2032 AddCodeLine ("ldy #$%02X", offs);
2033 AddCodeLine ("ldx #$00");
2034 AddCodeLine ("lda (ptr1),y");
2035 AddCodeLine ("sec");
2036 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2037 AddCodeLine ("sta (ptr1),y");
2042 AddCodeLine ("jsr pushax"); /* Push the address */
2043 push (CF_PTR); /* Correct the internal sp */
2044 g_getind (flags, offs); /* Fetch the value */
2045 g_dec (flags, val); /* Increment value in primary */
2046 g_putind (flags, offs); /* Store the value back */
2056 /*****************************************************************************/
2057 /* Add a variable address to the value in ax */
2058 /*****************************************************************************/
2062 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2063 /* Add the address of a local variable to ax */
2067 /* Add the offset */
2070 /* We cannot address more then 256 bytes of locals anyway */
2071 L = GetLocalLabel();
2072 CheckLocalOffs (offs);
2073 AddCodeLine ("clc");
2074 AddCodeLine ("adc #$%02X", offs & 0xFF);
2075 /* Do also skip the CLC insn below */
2076 AddCodeLine ("bcc %s", LocalLabelName (L));
2077 AddCodeLine ("inx");
2080 /* Add the current stackpointer value */
2081 AddCodeLine ("clc");
2083 /* Label was used above */
2086 AddCodeLine ("adc sp");
2087 AddCodeLine ("tay");
2088 AddCodeLine ("txa");
2089 AddCodeLine ("adc sp+1");
2090 AddCodeLine ("tax");
2091 AddCodeLine ("tya");
2096 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2097 /* Add the address of a static variable to ax */
2099 /* Create the correct label name */
2100 const char* lbuf = GetLabelName (flags, label, offs);
2102 /* Add the address to the current ax value */
2103 AddCodeLine ("clc");
2104 AddCodeLine ("adc #<(%s)", lbuf);
2105 AddCodeLine ("tay");
2106 AddCodeLine ("txa");
2107 AddCodeLine ("adc #>(%s)", lbuf);
2108 AddCodeLine ("tax");
2109 AddCodeLine ("tya");
2114 /*****************************************************************************/
2116 /*****************************************************************************/
2120 void g_save (unsigned flags)
2121 /* Copy primary register to hold register. */
2123 /* Check the size and determine operation */
2124 switch (flags & CF_TYPEMASK) {
2127 if (flags & CF_FORCECHAR) {
2128 AddCodeLine ("pha");
2134 AddCodeLine ("sta regsave");
2135 AddCodeLine ("stx regsave+1");
2139 AddCodeLine ("jsr saveeax");
2149 void g_restore (unsigned flags)
2150 /* Copy hold register to primary. */
2152 /* Check the size and determine operation */
2153 switch (flags & CF_TYPEMASK) {
2156 if (flags & CF_FORCECHAR) {
2157 AddCodeLine ("pla");
2163 AddCodeLine ("lda regsave");
2164 AddCodeLine ("ldx regsave+1");
2168 AddCodeLine ("jsr resteax");
2178 void g_cmp (unsigned flags, unsigned long val)
2179 /* Immidiate compare. The primary register will not be changed, Z flag
2185 /* Check the size and determine operation */
2186 switch (flags & CF_TYPEMASK) {
2189 if (flags & CF_FORCECHAR) {
2190 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2196 L = GetLocalLabel();
2197 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2198 AddCodeLine ("bne %s", LocalLabelName (L));
2199 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2204 Internal ("g_cmp: Long compares not implemented");
2214 static void oper (unsigned Flags, unsigned long Val, const char* const* Subs)
2215 /* Encode a binary operation. subs is a pointer to four strings:
2216 ** 0 --> Operate on ints
2217 ** 1 --> Operate on unsigneds
2218 ** 2 --> Operate on longs
2219 ** 3 --> Operate on unsigned longs
2222 /* Determine the offset into the array */
2223 if (Flags & CF_UNSIGNED) {
2226 if ((Flags & CF_TYPEMASK) == CF_LONG) {
2230 /* Load the value if it is not already in the primary */
2231 if (Flags & CF_CONST) {
2233 g_getimmed (Flags, Val, 0);
2236 /* Output the operation */
2237 AddCodeLine ("jsr %s", *Subs);
2239 /* The operation will pop it's argument */
2245 void g_test (unsigned flags)
2246 /* Test the value in the primary and set the condition codes */
2248 switch (flags & CF_TYPEMASK) {
2251 if (flags & CF_FORCECHAR) {
2252 AddCodeLine ("tax");
2258 AddCodeLine ("stx tmp1");
2259 AddCodeLine ("ora tmp1");
2263 if (flags & CF_UNSIGNED) {
2264 AddCodeLine ("jsr utsteax");
2266 AddCodeLine ("jsr tsteax");
2278 void g_push (unsigned flags, unsigned long val)
2279 /* Push the primary register or a constant value onto the stack */
2281 if (flags & CF_CONST && (flags & CF_TYPEMASK) != CF_LONG) {
2283 /* We have a constant 8 or 16 bit value */
2284 if ((flags & CF_TYPEMASK) == CF_CHAR && (flags & CF_FORCECHAR)) {
2286 /* Handle as 8 bit value */
2287 AddCodeLine ("lda #$%02X", (unsigned char) val);
2288 AddCodeLine ("jsr pusha");
2292 /* Handle as 16 bit value */
2293 g_getimmed (flags, val, 0);
2294 AddCodeLine ("jsr pushax");
2299 /* Value is not 16 bit or not constant */
2300 if (flags & CF_CONST) {
2301 /* Constant 32 bit value, load into eax */
2302 g_getimmed (flags, val, 0);
2305 /* Push the primary register */
2306 switch (flags & CF_TYPEMASK) {
2309 if (flags & CF_FORCECHAR) {
2310 /* Handle as char */
2311 AddCodeLine ("jsr pusha");
2316 AddCodeLine ("jsr pushax");
2320 AddCodeLine ("jsr pusheax");
2330 /* Adjust the stack offset */
2336 void g_swap (unsigned flags)
2337 /* Swap the primary register and the top of the stack. flags give the type
2338 ** of *both* values (must have same size).
2341 switch (flags & CF_TYPEMASK) {
2345 AddCodeLine ("jsr swapstk");
2349 AddCodeLine ("jsr swapestk");
2360 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2361 /* Call the specified subroutine name */
2363 if ((Flags & CF_FIXARGC) == 0) {
2364 /* Pass the argument count */
2365 AddCodeLine ("ldy #$%02X", ArgSize);
2367 AddCodeLine ("jsr _%s", Label);
2368 StackPtr += ArgSize; /* callee pops args */
2373 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2374 /* Call subroutine indirect */
2376 if ((Flags & CF_LOCAL) == 0) {
2377 /* Address is in a/x */
2378 if ((Flags & CF_FIXARGC) == 0) {
2379 /* Pass arg count */
2380 AddCodeLine ("ldy #$%02X", ArgSize);
2382 AddCodeLine ("jsr callax");
2384 /* The address is on stack, offset is on Val */
2386 CheckLocalOffs (Offs);
2387 AddCodeLine ("pha");
2388 AddCodeLine ("ldy #$%02X", Offs);
2389 AddCodeLine ("lda (sp),y");
2390 AddCodeLine ("sta jmpvec+1");
2391 AddCodeLine ("iny");
2392 AddCodeLine ("lda (sp),y");
2393 AddCodeLine ("sta jmpvec+2");
2394 AddCodeLine ("pla");
2395 AddCodeLine ("jsr jmpvec");
2398 /* Callee pops args */
2399 StackPtr += ArgSize;
2404 void g_jump (unsigned Label)
2405 /* Jump to specified internal label number */
2407 AddCodeLine ("jmp %s", LocalLabelName (Label));
2412 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2413 /* Jump to label if zero flag clear */
2415 AddCodeLine ("jne %s", LocalLabelName (label));
2420 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2421 /* Jump to label if zero flag set */
2423 AddCodeLine ("jeq %s", LocalLabelName (label));
2428 void g_drop (unsigned Space)
2429 /* Drop space allocated on the stack */
2432 /* Inline the code since calling addysp repeatedly is quite some
2435 AddCodeLine ("pha");
2436 AddCodeLine ("lda #$%02X", (unsigned char) Space);
2437 AddCodeLine ("clc");
2438 AddCodeLine ("adc sp");
2439 AddCodeLine ("sta sp");
2440 AddCodeLine ("lda #$%02X", (unsigned char) (Space >> 8));
2441 AddCodeLine ("adc sp+1");
2442 AddCodeLine ("sta sp+1");
2443 AddCodeLine ("pla");
2444 } else if (Space > 8) {
2445 AddCodeLine ("ldy #$%02X", Space);
2446 AddCodeLine ("jsr addysp");
2447 } else if (Space != 0) {
2448 AddCodeLine ("jsr incsp%u", Space);
2454 void g_space (int Space)
2455 /* Create or drop space on the stack */
2458 /* This is actually a drop operation */
2460 } else if (Space > 255) {
2461 /* Inline the code since calling subysp repeatedly is quite some
2464 AddCodeLine ("pha");
2465 AddCodeLine ("lda sp");
2466 AddCodeLine ("sec");
2467 AddCodeLine ("sbc #$%02X", (unsigned char) Space);
2468 AddCodeLine ("sta sp");
2469 AddCodeLine ("lda sp+1");
2470 AddCodeLine ("sbc #$%02X", (unsigned char) (Space >> 8));
2471 AddCodeLine ("sta sp+1");
2472 AddCodeLine ("pla");
2473 } else if (Space > 8) {
2474 AddCodeLine ("ldy #$%02X", Space);
2475 AddCodeLine ("jsr subysp");
2476 } else if (Space != 0) {
2477 AddCodeLine ("jsr decsp%u", Space);
2483 void g_cstackcheck (void)
2484 /* Check for a C stack overflow */
2486 AddCodeLine ("jsr cstkchk");
2491 void g_stackcheck (void)
2492 /* Check for a stack overflow */
2494 AddCodeLine ("jsr stkchk");
2499 void g_add (unsigned flags, unsigned long val)
2500 /* Primary = TOS + Primary */
2502 static const char* const ops[4] = {
2503 "tosaddax", "tosaddax", "tosaddeax", "tosaddeax"
2506 if (flags & CF_CONST) {
2507 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2508 g_push (flags & ~CF_CONST, 0);
2510 oper (flags, val, ops);
2515 void g_sub (unsigned flags, unsigned long val)
2516 /* Primary = TOS - Primary */
2518 static const char* const ops[4] = {
2519 "tossubax", "tossubax", "tossubeax", "tossubeax"
2522 if (flags & CF_CONST) {
2523 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2524 g_push (flags & ~CF_CONST, 0);
2526 oper (flags, val, ops);
2531 void g_rsub (unsigned flags, unsigned long val)
2532 /* Primary = Primary - TOS */
2534 static const char* const ops[4] = {
2535 "tosrsubax", "tosrsubax", "tosrsubeax", "tosrsubeax"
2537 oper (flags, val, ops);
2542 void g_mul (unsigned flags, unsigned long val)
2543 /* Primary = TOS * Primary */
2545 static const char* const ops[4] = {
2546 "tosmulax", "tosumulax", "tosmuleax", "tosumuleax"
2551 /* Do strength reduction if the value is constant and a power of two */
2552 if (flags & CF_CONST && (p2 = PowerOf2 (val)) >= 0) {
2553 /* Generate a shift instead */
2558 /* If the right hand side is const, the lhs is not on stack but still
2559 ** in the primary register.
2561 if (flags & CF_CONST) {
2563 switch (flags & CF_TYPEMASK) {
2566 if (flags & CF_FORCECHAR) {
2567 /* Handle some special cases */
2571 AddCodeLine ("sta tmp1");
2572 AddCodeLine ("asl a");
2573 AddCodeLine ("clc");
2574 AddCodeLine ("adc tmp1");
2578 AddCodeLine ("sta tmp1");
2579 AddCodeLine ("asl a");
2580 AddCodeLine ("asl a");
2581 AddCodeLine ("clc");
2582 AddCodeLine ("adc tmp1");
2586 AddCodeLine ("sta tmp1");
2587 AddCodeLine ("asl a");
2588 AddCodeLine ("clc");
2589 AddCodeLine ("adc tmp1");
2590 AddCodeLine ("asl a");
2594 AddCodeLine ("sta tmp1");
2595 AddCodeLine ("asl a");
2596 AddCodeLine ("asl a");
2597 AddCodeLine ("clc");
2598 AddCodeLine ("adc tmp1");
2599 AddCodeLine ("asl a");
2608 AddCodeLine ("jsr mulax3");
2611 AddCodeLine ("jsr mulax5");
2614 AddCodeLine ("jsr mulax6");
2617 AddCodeLine ("jsr mulax7");
2620 AddCodeLine ("jsr mulax9");
2623 AddCodeLine ("jsr mulax10");
2635 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2636 ** into the normal, non-optimized stuff.
2638 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2639 g_push (flags & ~CF_CONST, 0);
2643 /* Use long way over the stack */
2644 oper (flags, val, ops);
2649 void g_div (unsigned flags, unsigned long val)
2650 /* Primary = TOS / Primary */
2652 static const char* const ops[4] = {
2653 "tosdivax", "tosudivax", "tosdiveax", "tosudiveax"
2656 /* Do strength reduction if the value is constant and a power of two */
2658 if ((flags & CF_CONST) && (p2 = PowerOf2 (val)) >= 0) {
2659 /* Generate a shift instead */
2662 /* Generate a division */
2663 if (flags & CF_CONST) {
2664 /* lhs is not on stack */
2665 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2666 g_push (flags & ~CF_CONST, 0);
2668 oper (flags, val, ops);
2674 void g_mod (unsigned flags, unsigned long val)
2675 /* Primary = TOS % Primary */
2677 static const char* const ops[4] = {
2678 "tosmodax", "tosumodax", "tosmodeax", "tosumodeax"
2682 /* Check if we can do some cost reduction */
2683 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = PowerOf2 (val)) >= 0) {
2684 /* We can do that with an AND operation */
2685 g_and (flags, val - 1);
2687 /* Do it the hard way... */
2688 if (flags & CF_CONST) {
2689 /* lhs is not on stack */
2690 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2691 g_push (flags & ~CF_CONST, 0);
2693 oper (flags, val, ops);
2699 void g_or (unsigned flags, unsigned long val)
2700 /* Primary = TOS | Primary */
2702 static const char* const ops[4] = {
2703 "tosorax", "tosorax", "tosoreax", "tosoreax"
2706 /* If the right hand side is const, the lhs is not on stack but still
2707 ** in the primary register.
2709 if (flags & CF_CONST) {
2711 switch (flags & CF_TYPEMASK) {
2714 if (flags & CF_FORCECHAR) {
2715 if ((val & 0xFF) != 0) {
2716 AddCodeLine ("ora #$%02X", (unsigned char)val);
2724 if ((val & 0xFF) != 0) {
2725 AddCodeLine ("ora #$%02X", (unsigned char)val);
2727 } else if ((val & 0xFF00) == 0xFF00) {
2728 if ((val & 0xFF) != 0) {
2729 AddCodeLine ("ora #$%02X", (unsigned char)val);
2731 AddCodeLine ("ldx #$FF");
2732 } else if (val != 0) {
2733 AddCodeLine ("ora #$%02X", (unsigned char)val);
2734 AddCodeLine ("pha");
2735 AddCodeLine ("txa");
2736 AddCodeLine ("ora #$%02X", (unsigned char)(val >> 8));
2737 AddCodeLine ("tax");
2738 AddCodeLine ("pla");
2744 if ((val & 0xFF) != 0) {
2745 AddCodeLine ("ora #$%02X", (unsigned char)val);
2755 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2756 ** into the normal, non-optimized stuff. Note: The standard stuff will
2757 ** always work with ints.
2759 flags &= ~CF_FORCECHAR;
2760 g_push (flags & ~CF_CONST, 0);
2763 /* Use long way over the stack */
2764 oper (flags, val, ops);
2769 void g_xor (unsigned flags, unsigned long val)
2770 /* Primary = TOS ^ Primary */
2772 static const char* const ops[4] = {
2773 "tosxorax", "tosxorax", "tosxoreax", "tosxoreax"
2777 /* If the right hand side is const, the lhs is not on stack but still
2778 ** in the primary register.
2780 if (flags & CF_CONST) {
2782 switch (flags & CF_TYPEMASK) {
2785 if (flags & CF_FORCECHAR) {
2786 if ((val & 0xFF) != 0) {
2787 AddCodeLine ("eor #$%02X", (unsigned char)val);
2796 AddCodeLine ("eor #$%02X", (unsigned char)val);
2798 } else if (val != 0) {
2799 if ((val & 0xFF) != 0) {
2800 AddCodeLine ("eor #$%02X", (unsigned char)val);
2802 AddCodeLine ("pha");
2803 AddCodeLine ("txa");
2804 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2805 AddCodeLine ("tax");
2806 AddCodeLine ("pla");
2813 AddCodeLine ("eor #$%02X", (unsigned char)val);
2823 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2824 ** into the normal, non-optimized stuff. Note: The standard stuff will
2825 ** always work with ints.
2827 flags &= ~CF_FORCECHAR;
2828 g_push (flags & ~CF_CONST, 0);
2831 /* Use long way over the stack */
2832 oper (flags, val, ops);
2837 void g_and (unsigned Flags, unsigned long Val)
2838 /* Primary = TOS & Primary */
2840 static const char* const ops[4] = {
2841 "tosandax", "tosandax", "tosandeax", "tosandeax"
2844 /* If the right hand side is const, the lhs is not on stack but still
2845 ** in the primary register.
2847 if (Flags & CF_CONST) {
2849 switch (Flags & CF_TYPEMASK) {
2852 if (Flags & CF_FORCECHAR) {
2853 if ((Val & 0xFF) == 0x00) {
2854 AddCodeLine ("lda #$00");
2855 } else if ((Val & 0xFF) != 0xFF) {
2856 AddCodeLine ("and #$%02X", (unsigned char)Val);
2862 if ((Val & 0xFFFF) != 0xFFFF) {
2864 AddCodeLine ("ldx #$00");
2866 AddCodeLine ("lda #$00");
2867 } else if (Val != 0xFF) {
2868 AddCodeLine ("and #$%02X", (unsigned char)Val);
2870 } else if ((Val & 0xFFFF) == 0xFF00) {
2871 AddCodeLine ("lda #$00");
2872 } else if ((Val & 0xFF00) == 0xFF00) {
2873 AddCodeLine ("and #$%02X", (unsigned char)Val);
2874 } else if ((Val & 0x00FF) == 0x0000) {
2875 AddCodeLine ("txa");
2876 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2877 AddCodeLine ("tax");
2878 AddCodeLine ("lda #$00");
2880 AddCodeLine ("tay");
2881 AddCodeLine ("txa");
2882 AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2883 AddCodeLine ("tax");
2884 AddCodeLine ("tya");
2885 if ((Val & 0x00FF) == 0x0000) {
2886 AddCodeLine ("lda #$00");
2887 } else if ((Val & 0x00FF) != 0x00FF) {
2888 AddCodeLine ("and #$%02X", (unsigned char)Val);
2896 AddCodeLine ("ldx #$00");
2897 AddCodeLine ("stx sreg+1");
2898 AddCodeLine ("stx sreg");
2899 if ((Val & 0xFF) != 0xFF) {
2900 AddCodeLine ("and #$%02X", (unsigned char)Val);
2903 } else if (Val == 0xFF00) {
2904 AddCodeLine ("lda #$00");
2905 AddCodeLine ("sta sreg+1");
2906 AddCodeLine ("sta sreg");
2915 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2916 ** into the normal, non-optimized stuff. Note: The standard stuff will
2917 ** always work with ints.
2919 Flags &= ~CF_FORCECHAR;
2920 g_push (Flags & ~CF_CONST, 0);
2923 /* Use long way over the stack */
2924 oper (Flags, Val, ops);
2929 void g_asr (unsigned flags, unsigned long val)
2930 /* Primary = TOS >> Primary */
2932 static const char* const ops[4] = {
2933 "tosasrax", "tosshrax", "tosasreax", "tosshreax"
2936 /* If the right hand side is const, the lhs is not on stack but still
2937 ** in the primary register.
2939 if (flags & CF_CONST) {
2941 switch (flags & CF_TYPEMASK) {
2947 if (flags & CF_UNSIGNED) {
2948 AddCodeLine ("txa");
2949 AddCodeLine ("ldx #$00");
2951 unsigned L = GetLocalLabel();
2952 AddCodeLine ("cpx #$80"); /* Sign bit into carry */
2953 AddCodeLine ("txa");
2954 AddCodeLine ("ldx #$00");
2955 AddCodeLine ("bcc %s", LocalLabelName (L));
2956 AddCodeLine ("dex"); /* Make $FF */
2962 if (flags & CF_UNSIGNED) {
2963 AddCodeLine ("jsr shrax4");
2965 AddCodeLine ("jsr asrax4");
2970 if (flags & CF_UNSIGNED) {
2971 AddCodeLine ("jsr shrax%ld", val);
2973 AddCodeLine ("jsr asrax%ld", val);
2981 AddCodeLine ("ldx #$00");
2982 AddCodeLine ("lda sreg+1");
2983 if ((flags & CF_UNSIGNED) == 0) {
2984 unsigned L = GetLocalLabel();
2985 AddCodeLine ("bpl %s", LocalLabelName (L));
2986 AddCodeLine ("dex");
2989 AddCodeLine ("stx sreg");
2990 AddCodeLine ("stx sreg+1");
2994 AddCodeLine ("ldy #$00");
2995 AddCodeLine ("ldx sreg+1");
2996 if ((flags & CF_UNSIGNED) == 0) {
2997 unsigned L = GetLocalLabel();
2998 AddCodeLine ("bpl %s", LocalLabelName (L));
2999 AddCodeLine ("dey");
3002 AddCodeLine ("lda sreg");
3003 AddCodeLine ("sty sreg+1");
3004 AddCodeLine ("sty sreg");
3008 AddCodeLine ("txa");
3009 AddCodeLine ("ldx sreg");
3010 AddCodeLine ("ldy sreg+1");
3011 AddCodeLine ("sty sreg");
3012 if ((flags & CF_UNSIGNED) == 0) {
3013 unsigned L = GetLocalLabel();
3014 AddCodeLine ("cpy #$80");
3015 AddCodeLine ("ldy #$00");
3016 AddCodeLine ("bcc %s", LocalLabelName (L));
3017 AddCodeLine ("dey");
3020 AddCodeLine ("ldy #$00");
3022 AddCodeLine ("sty sreg+1");
3026 if (flags & CF_UNSIGNED) {
3027 AddCodeLine ("jsr shreax4");
3029 AddCodeLine ("jsr asreax4");
3034 if (flags & CF_UNSIGNED) {
3035 AddCodeLine ("jsr shreax%ld", val);
3037 AddCodeLine ("jsr asreax%ld", val);
3046 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3047 ** into the normal, non-optimized stuff. Note: The standard stuff will
3048 ** always work with ints.
3050 flags &= ~CF_FORCECHAR;
3051 g_push (flags & ~CF_CONST, 0);
3054 /* Use long way over the stack */
3055 oper (flags, val, ops);
3060 void g_asl (unsigned flags, unsigned long val)
3061 /* Primary = TOS << Primary */
3063 static const char* const ops[4] = {
3064 "tosaslax", "tosshlax", "tosasleax", "tosshleax"
3068 /* If the right hand side is const, the lhs is not on stack but still
3069 ** in the primary register.
3071 if (flags & CF_CONST) {
3073 switch (flags & CF_TYPEMASK) {
3079 AddCodeLine ("tax");
3080 AddCodeLine ("lda #$00");
3084 if (flags & CF_UNSIGNED) {
3085 AddCodeLine ("jsr shlax4");
3087 AddCodeLine ("jsr aslax4");
3092 if (flags & CF_UNSIGNED) {
3093 AddCodeLine ("jsr shlax%ld", val);
3095 AddCodeLine ("jsr aslax%ld", val);
3103 AddCodeLine ("sta sreg+1");
3104 AddCodeLine ("lda #$00");
3105 AddCodeLine ("tax");
3106 AddCodeLine ("sta sreg");
3110 AddCodeLine ("stx sreg+1");
3111 AddCodeLine ("sta sreg");
3112 AddCodeLine ("lda #$00");
3113 AddCodeLine ("tax");
3117 AddCodeLine ("ldy sreg");
3118 AddCodeLine ("sty sreg+1");
3119 AddCodeLine ("stx sreg");
3120 AddCodeLine ("tax");
3121 AddCodeLine ("lda #$00");
3125 if (flags & CF_UNSIGNED) {
3126 AddCodeLine ("jsr shleax4");
3128 AddCodeLine ("jsr asleax4");
3133 if (flags & CF_UNSIGNED) {
3134 AddCodeLine ("jsr shleax%ld", val);
3136 AddCodeLine ("jsr asleax%ld", val);
3145 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3146 ** into the normal, non-optimized stuff. Note: The standard stuff will
3147 ** always work with ints.
3149 flags &= ~CF_FORCECHAR;
3150 g_push (flags & ~CF_CONST, 0);
3153 /* Use long way over the stack */
3154 oper (flags, val, ops);
3159 void g_neg (unsigned Flags)
3160 /* Primary = -Primary */
3162 switch (Flags & CF_TYPEMASK) {
3165 if (Flags & CF_FORCECHAR) {
3166 AddCodeLine ("eor #$FF");
3167 AddCodeLine ("clc");
3168 AddCodeLine ("adc #$01");
3174 AddCodeLine ("jsr negax");
3178 AddCodeLine ("jsr negeax");
3188 void g_bneg (unsigned flags)
3189 /* Primary = !Primary */
3191 switch (flags & CF_TYPEMASK) {
3194 AddCodeLine ("jsr bnega");
3198 AddCodeLine ("jsr bnegax");
3202 AddCodeLine ("jsr bnegeax");
3212 void g_com (unsigned Flags)
3213 /* Primary = ~Primary */
3215 switch (Flags & CF_TYPEMASK) {
3218 if (Flags & CF_FORCECHAR) {
3219 AddCodeLine ("eor #$FF");
3225 AddCodeLine ("jsr complax");
3229 AddCodeLine ("jsr compleax");
3239 void g_inc (unsigned flags, unsigned long val)
3240 /* Increment the primary register by a given number */
3242 /* Don't inc by zero */
3247 /* Generate code for the supported types */
3249 switch (flags & CF_TYPEMASK) {
3252 if (flags & CF_FORCECHAR) {
3253 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3255 AddCodeLine ("ina");
3258 AddCodeLine ("clc");
3259 AddCodeLine ("adc #$%02X", (unsigned char)val);
3266 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) {
3267 unsigned L = GetLocalLabel();
3268 AddCodeLine ("ina");
3269 AddCodeLine ("bne %s", LocalLabelName (L));
3270 AddCodeLine ("inx");
3272 } else if (IS_Get (&CodeSizeFactor) < 200) {
3275 AddCodeLine ("jsr incax%lu", val);
3276 } else if (val <= 255) {
3277 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3278 AddCodeLine ("jsr incaxy");
3280 g_add (flags | CF_CONST, val);
3283 /* Inline the code */
3285 if ((val & 0xFF) != 0) {
3286 unsigned L = GetLocalLabel();
3287 AddCodeLine ("clc");
3288 AddCodeLine ("adc #$%02X", (unsigned char) val);
3289 AddCodeLine ("bcc %s", LocalLabelName (L));
3290 AddCodeLine ("inx");
3294 AddCodeLine ("inx");
3297 AddCodeLine ("inx");
3300 AddCodeLine ("inx");
3302 } else if ((val & 0xFF) != 0) {
3303 AddCodeLine ("clc");
3304 AddCodeLine ("adc #$%02X", (unsigned char) val);
3305 AddCodeLine ("pha");
3306 AddCodeLine ("txa");
3307 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3308 AddCodeLine ("tax");
3309 AddCodeLine ("pla");
3311 AddCodeLine ("pha");
3312 AddCodeLine ("txa");
3313 AddCodeLine ("clc");
3314 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3315 AddCodeLine ("tax");
3316 AddCodeLine ("pla");
3323 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3324 AddCodeLine ("jsr inceaxy");
3326 g_add (flags | CF_CONST, val);
3338 void g_dec (unsigned flags, unsigned long val)
3339 /* Decrement the primary register by a given number */
3341 /* Don't dec by zero */
3346 /* Generate code for the supported types */
3348 switch (flags & CF_TYPEMASK) {
3351 if (flags & CF_FORCECHAR) {
3352 if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val <= 2) {
3354 AddCodeLine ("dea");
3357 AddCodeLine ("sec");
3358 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3365 if (IS_Get (&CodeSizeFactor) < 200) {
3366 /* Use subroutines */
3368 AddCodeLine ("jsr decax%d", (int) val);
3369 } else if (val <= 255) {
3370 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3371 AddCodeLine ("jsr decaxy");
3373 g_sub (flags | CF_CONST, val);
3376 /* Inline the code */
3378 if ((val & 0xFF) != 0) {
3379 unsigned L = GetLocalLabel();
3380 AddCodeLine ("sec");
3381 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3382 AddCodeLine ("bcs %s", LocalLabelName (L));
3383 AddCodeLine ("dex");
3387 AddCodeLine ("dex");
3390 AddCodeLine ("dex");
3393 if ((val & 0xFF) != 0) {
3394 AddCodeLine ("sec");
3395 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3396 AddCodeLine ("pha");
3397 AddCodeLine ("txa");
3398 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3399 AddCodeLine ("tax");
3400 AddCodeLine ("pla");
3402 AddCodeLine ("pha");
3403 AddCodeLine ("txa");
3404 AddCodeLine ("sec");
3405 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3406 AddCodeLine ("tax");
3407 AddCodeLine ("pla");
3415 AddCodeLine ("ldy #$%02X", (unsigned char) val);
3416 AddCodeLine ("jsr deceaxy");
3418 g_sub (flags | CF_CONST, val);
3431 ** Following are the conditional operators. They compare the TOS against
3432 ** the primary and put a literal 1 in the primary if the condition is
3433 ** true, otherwise they clear the primary register
3438 void g_eq (unsigned flags, unsigned long val)
3439 /* Test for equal */
3441 static const char* const ops[4] = {
3442 "toseqax", "toseqax", "toseqeax", "toseqeax"
3447 /* If the right hand side is const, the lhs is not on stack but still
3448 ** in the primary register.
3450 if (flags & CF_CONST) {
3452 switch (flags & CF_TYPEMASK) {
3455 if (flags & CF_FORCECHAR) {
3456 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3457 AddCodeLine ("jsr booleq");
3463 L = GetLocalLabel();
3464 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3465 AddCodeLine ("bne %s", LocalLabelName (L));
3466 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3468 AddCodeLine ("jsr booleq");
3478 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3479 ** into the normal, non-optimized stuff. Note: The standard stuff will
3480 ** always work with ints.
3482 flags &= ~CF_FORCECHAR;
3483 g_push (flags & ~CF_CONST, 0);
3486 /* Use long way over the stack */
3487 oper (flags, val, ops);
3492 void g_ne (unsigned flags, unsigned long val)
3493 /* Test for not equal */
3495 static const char* const ops[4] = {
3496 "tosneax", "tosneax", "tosneeax", "tosneeax"
3501 /* If the right hand side is const, the lhs is not on stack but still
3502 ** in the primary register.
3504 if (flags & CF_CONST) {
3506 switch (flags & CF_TYPEMASK) {
3509 if (flags & CF_FORCECHAR) {
3510 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3511 AddCodeLine ("jsr boolne");
3517 L = GetLocalLabel();
3518 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3519 AddCodeLine ("bne %s", LocalLabelName (L));
3520 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3522 AddCodeLine ("jsr boolne");
3532 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3533 ** into the normal, non-optimized stuff. Note: The standard stuff will
3534 ** always work with ints.
3536 flags &= ~CF_FORCECHAR;
3537 g_push (flags & ~CF_CONST, 0);
3540 /* Use long way over the stack */
3541 oper (flags, val, ops);
3546 void g_lt (unsigned flags, unsigned long val)
3547 /* Test for less than */
3549 static const char* const ops[4] = {
3550 "tosltax", "tosultax", "toslteax", "tosulteax"
3555 /* If the right hand side is const, the lhs is not on stack but still
3556 ** in the primary register.
3558 if (flags & CF_CONST) {
3560 /* Because the handling of the overflow flag is too complex for
3561 ** inlining, we can handle only unsigned compares, and signed
3562 ** compares against zero here.
3564 if (flags & CF_UNSIGNED) {
3566 /* Give a warning in some special cases */
3568 Warning ("Condition is never true");
3569 AddCodeLine ("jsr return0");
3573 /* Look at the type */
3574 switch (flags & CF_TYPEMASK) {
3577 if (flags & CF_FORCECHAR) {
3578 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3579 AddCodeLine ("jsr boolult");
3585 /* If the low byte is zero, we must only test the high byte */
3586 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3587 if ((val & 0xFF) != 0) {
3588 unsigned L = GetLocalLabel();
3589 AddCodeLine ("bne %s", LocalLabelName (L));
3590 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3593 AddCodeLine ("jsr boolult");
3597 /* Do a subtraction */
3598 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3599 AddCodeLine ("txa");
3600 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3601 AddCodeLine ("lda sreg");
3602 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3603 AddCodeLine ("lda sreg+1");
3604 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3605 AddCodeLine ("jsr boolult");
3612 } else if (val == 0) {
3614 /* A signed compare against zero must only look at the sign bit */
3615 switch (flags & CF_TYPEMASK) {
3618 if (flags & CF_FORCECHAR) {
3619 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3620 AddCodeLine ("lda #$00");
3621 AddCodeLine ("ldx #$00");
3622 AddCodeLine ("rol a");
3628 /* Just check the high byte */
3629 AddCodeLine ("cpx #$80"); /* Bit 7 -> carry */
3630 AddCodeLine ("lda #$00");
3631 AddCodeLine ("ldx #$00");
3632 AddCodeLine ("rol a");
3636 /* Just check the high byte */
3637 AddCodeLine ("lda sreg+1");
3638 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3639 AddCodeLine ("lda #$00");
3640 AddCodeLine ("ldx #$00");
3641 AddCodeLine ("rol a");
3650 /* Signed compare against a constant != zero */
3651 switch (flags & CF_TYPEMASK) {
3654 if (flags & CF_FORCECHAR) {
3655 Label = GetLocalLabel ();
3656 AddCodeLine ("sec");
3657 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3658 AddCodeLine ("bvc %s", LocalLabelName (Label));
3659 AddCodeLine ("eor #$80");
3660 g_defcodelabel (Label);
3661 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3662 AddCodeLine ("lda #$00");
3663 AddCodeLine ("ldx #$00");
3664 AddCodeLine ("rol a");
3670 /* Do a subtraction */
3671 Label = GetLocalLabel ();
3672 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3673 AddCodeLine ("txa");
3674 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3675 AddCodeLine ("bvc %s", LocalLabelName (Label));
3676 AddCodeLine ("eor #$80");
3677 g_defcodelabel (Label);
3678 AddCodeLine ("asl a"); /* Bit 7 -> carry */
3679 AddCodeLine ("lda #$00");
3680 AddCodeLine ("ldx #$00");
3681 AddCodeLine ("rol a");
3685 /* This one is too costly */
3694 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3695 ** into the normal, non-optimized stuff. Note: The standard stuff will
3696 ** always work with ints.
3698 flags &= ~CF_FORCECHAR;
3699 g_push (flags & ~CF_CONST, 0);
3702 /* Use long way over the stack */
3703 oper (flags, val, ops);
3708 void g_le (unsigned flags, unsigned long val)
3709 /* Test for less than or equal to */
3711 static const char* const ops[4] = {
3712 "tosleax", "tosuleax", "tosleeax", "tosuleeax"
3716 /* If the right hand side is const, the lhs is not on stack but still
3717 ** in the primary register.
3719 if (flags & CF_CONST) {
3721 /* Look at the type */
3722 switch (flags & CF_TYPEMASK) {
3725 if (flags & CF_FORCECHAR) {
3726 if (flags & CF_UNSIGNED) {
3727 /* Unsigned compare */
3729 /* Use < instead of <= because the former gives
3730 ** better code on the 6502 than the latter.
3732 g_lt (flags, val+1);
3735 Warning ("Condition is always true");
3736 AddCodeLine ("jsr return1");
3739 /* Signed compare */
3740 if ((long) val < 0x7F) {
3741 /* Use < instead of <= because the former gives
3742 ** better code on the 6502 than the latter.
3744 g_lt (flags, val+1);
3747 Warning ("Condition is always true");
3748 AddCodeLine ("jsr return1");
3756 if (flags & CF_UNSIGNED) {
3757 /* Unsigned compare */
3759 /* Use < instead of <= because the former gives
3760 ** better code on the 6502 than the latter.
3762 g_lt (flags, val+1);
3765 Warning ("Condition is always true");
3766 AddCodeLine ("jsr return1");
3769 /* Signed compare */
3770 if ((long) val < 0x7FFF) {
3771 g_lt (flags, val+1);
3774 Warning ("Condition is always true");
3775 AddCodeLine ("jsr return1");
3781 if (flags & CF_UNSIGNED) {
3782 /* Unsigned compare */
3783 if (val < 0xFFFFFFFF) {
3784 /* Use < instead of <= because the former gives
3785 ** better code on the 6502 than the latter.
3787 g_lt (flags, val+1);
3790 Warning ("Condition is always true");
3791 AddCodeLine ("jsr return1");
3794 /* Signed compare */
3795 if ((long) val < 0x7FFFFFFF) {
3796 g_lt (flags, val+1);
3799 Warning ("Condition is always true");
3800 AddCodeLine ("jsr return1");
3809 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3810 ** into the normal, non-optimized stuff. Note: The standard stuff will
3811 ** always work with ints.
3813 flags &= ~CF_FORCECHAR;
3814 g_push (flags & ~CF_CONST, 0);
3817 /* Use long way over the stack */
3818 oper (flags, val, ops);
3823 void g_gt (unsigned flags, unsigned long val)
3824 /* Test for greater than */
3826 static const char* const ops[4] = {
3827 "tosgtax", "tosugtax", "tosgteax", "tosugteax"
3831 /* If the right hand side is const, the lhs is not on stack but still
3832 ** in the primary register.
3834 if (flags & CF_CONST) {
3836 /* Look at the type */
3837 switch (flags & CF_TYPEMASK) {
3840 if (flags & CF_FORCECHAR) {
3841 if (flags & CF_UNSIGNED) {
3843 /* If we have a compare > 0, we will replace it by
3844 ** != 0 here, since both are identical but the
3845 ** latter is easier to optimize.
3848 } else if (val < 0xFF) {
3849 /* Use >= instead of > because the former gives
3850 ** better code on the 6502 than the latter.
3852 g_ge (flags, val+1);
3855 Warning ("Condition is never true");
3856 AddCodeLine ("jsr return0");
3859 if ((long) val < 0x7F) {
3860 /* Use >= instead of > because the former gives
3861 ** better code on the 6502 than the latter.
3863 g_ge (flags, val+1);
3866 Warning ("Condition is never true");
3867 AddCodeLine ("jsr return0");
3875 if (flags & CF_UNSIGNED) {
3876 /* Unsigned compare */
3878 /* If we have a compare > 0, we will replace it by
3879 ** != 0 here, since both are identical but the latter
3880 ** is easier to optimize.
3883 } else if (val < 0xFFFF) {
3884 /* Use >= instead of > because the former gives better
3885 ** code on the 6502 than the latter.
3887 g_ge (flags, val+1);
3890 Warning ("Condition is never true");
3891 AddCodeLine ("jsr return0");
3894 /* Signed compare */
3895 if ((long) val < 0x7FFF) {
3896 g_ge (flags, val+1);
3899 Warning ("Condition is never true");
3900 AddCodeLine ("jsr return0");
3906 if (flags & CF_UNSIGNED) {
3907 /* Unsigned compare */
3909 /* If we have a compare > 0, we will replace it by
3910 ** != 0 here, since both are identical but the latter
3911 ** is easier to optimize.
3914 } else if (val < 0xFFFFFFFF) {
3915 /* Use >= instead of > because the former gives better
3916 ** code on the 6502 than the latter.
3918 g_ge (flags, val+1);
3921 Warning ("Condition is never true");
3922 AddCodeLine ("jsr return0");
3925 /* Signed compare */
3926 if ((long) val < 0x7FFFFFFF) {
3927 g_ge (flags, val+1);
3930 Warning ("Condition is never true");
3931 AddCodeLine ("jsr return0");
3940 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3941 ** into the normal, non-optimized stuff. Note: The standard stuff will
3942 ** always work with ints.
3944 flags &= ~CF_FORCECHAR;
3945 g_push (flags & ~CF_CONST, 0);
3948 /* Use long way over the stack */
3949 oper (flags, val, ops);
3954 void g_ge (unsigned flags, unsigned long val)
3955 /* Test for greater than or equal to */
3957 static const char* const ops[4] = {
3958 "tosgeax", "tosugeax", "tosgeeax", "tosugeeax"
3964 /* If the right hand side is const, the lhs is not on stack but still
3965 ** in the primary register.
3967 if (flags & CF_CONST) {
3969 /* Because the handling of the overflow flag is too complex for
3970 ** inlining, we can handle only unsigned compares, and signed
3971 ** compares against zero here.
3973 if (flags & CF_UNSIGNED) {
3975 /* Give a warning in some special cases */
3977 Warning ("Condition is always true");
3978 AddCodeLine ("jsr return1");
3982 /* Look at the type */
3983 switch (flags & CF_TYPEMASK) {
3986 if (flags & CF_FORCECHAR) {
3987 /* Do a subtraction. Condition is true if carry set */
3988 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3989 AddCodeLine ("lda #$00");
3990 AddCodeLine ("ldx #$00");
3991 AddCodeLine ("rol a");
3997 /* Do a subtraction. Condition is true if carry set */
3998 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3999 AddCodeLine ("txa");
4000 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4001 AddCodeLine ("lda #$00");
4002 AddCodeLine ("ldx #$00");
4003 AddCodeLine ("rol a");
4007 /* Do a subtraction. Condition is true if carry set */
4008 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4009 AddCodeLine ("txa");
4010 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4011 AddCodeLine ("lda sreg");
4012 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
4013 AddCodeLine ("lda sreg+1");
4014 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
4015 AddCodeLine ("lda #$00");
4016 AddCodeLine ("ldx #$00");
4017 AddCodeLine ("rol a");
4024 } else if (val == 0) {
4026 /* A signed compare against zero must only look at the sign bit */
4027 switch (flags & CF_TYPEMASK) {
4030 if (flags & CF_FORCECHAR) {
4031 AddCodeLine ("tax");
4032 AddCodeLine ("jsr boolge");
4038 /* Just test the high byte */
4039 AddCodeLine ("txa");
4040 AddCodeLine ("jsr boolge");
4044 /* Just test the high byte */
4045 AddCodeLine ("lda sreg+1");
4046 AddCodeLine ("jsr boolge");
4055 /* Signed compare against a constant != zero */
4056 switch (flags & CF_TYPEMASK) {
4059 if (flags & CF_FORCECHAR) {
4060 Label = GetLocalLabel ();
4061 AddCodeLine ("sec");
4062 AddCodeLine ("sbc #$%02X", (unsigned char)val);
4063 AddCodeLine ("bvs %s", LocalLabelName (Label));
4064 AddCodeLine ("eor #$80");
4065 g_defcodelabel (Label);
4066 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4067 AddCodeLine ("lda #$00");
4068 AddCodeLine ("ldx #$00");
4069 AddCodeLine ("rol a");
4075 /* Do a subtraction */
4076 Label = GetLocalLabel ();
4077 AddCodeLine ("cmp #$%02X", (unsigned char)val);
4078 AddCodeLine ("txa");
4079 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
4080 AddCodeLine ("bvs %s", LocalLabelName (Label));
4081 AddCodeLine ("eor #$80");
4082 g_defcodelabel (Label);
4083 AddCodeLine ("asl a"); /* Bit 7 -> carry */
4084 AddCodeLine ("lda #$00");
4085 AddCodeLine ("ldx #$00");
4086 AddCodeLine ("rol a");
4090 /* This one is too costly */
4098 /* If we go here, we didn't emit code. Push the lhs on stack and fall
4099 ** into the normal, non-optimized stuff. Note: The standard stuff will
4100 ** always work with ints.
4102 flags &= ~CF_FORCECHAR;
4103 g_push (flags & ~CF_CONST, 0);
4106 /* Use long way over the stack */
4107 oper (flags, val, ops);
4112 /*****************************************************************************/
4113 /* Allocating static storage */
4114 /*****************************************************************************/
4118 void g_res (unsigned n)
4119 /* Reserve static storage, n bytes */
4121 AddDataLine ("\t.res\t%u,$00", n);
4126 void g_defdata (unsigned flags, unsigned long val, long offs)
4127 /* Define data with the size given in flags */
4129 if (flags & CF_CONST) {
4131 /* Numeric constant */
4132 switch (flags & CF_TYPEMASK) {
4135 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
4139 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
4143 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
4154 /* Create the correct label name */
4155 const char* Label = GetLabelName (flags, val, offs);
4157 /* Labels are always 16 bit */
4158 AddDataLine ("\t.addr\t%s", Label);
4165 void g_defbytes (const void* Bytes, unsigned Count)
4166 /* Output a row of bytes as a constant */
4172 /* Cast the buffer pointer */
4173 const unsigned char* Data = (const unsigned char*) Bytes;
4175 /* Output the stuff */
4178 /* How many go into this line? */
4179 if ((Chunk = Count) > 16) {
4184 /* Output one line */
4185 strcpy (Buf, "\t.byte\t");
4188 B += sprintf (B, "$%02X", *Data++);
4194 /* Output the line */
4195 AddDataLine ("%s", Buf);
4201 void g_zerobytes (unsigned Count)
4202 /* Output Count bytes of data initialized with zero */
4205 AddDataLine ("\t.res\t%u,$00", Count);
4211 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4212 /* Initialize a register variable from static initialization data */
4214 /* Register variables do always have less than 128 bytes */
4215 unsigned CodeLabel = GetLocalLabel ();
4216 AddCodeLine ("ldx #$%02X", (unsigned char) (Size - 1));
4217 g_defcodelabel (CodeLabel);
4218 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4219 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4220 AddCodeLine ("dex");
4221 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4226 void g_initauto (unsigned Label, unsigned Size)
4227 /* Initialize a local variable at stack offset zero from static data */
4229 unsigned CodeLabel = GetLocalLabel ();
4231 CheckLocalOffs (Size);
4233 AddCodeLine ("ldy #$%02X", Size-1);
4234 g_defcodelabel (CodeLabel);
4235 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4236 AddCodeLine ("sta (sp),y");
4237 AddCodeLine ("dey");
4238 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4239 } else if (Size <= 256) {
4240 AddCodeLine ("ldy #$00");
4241 g_defcodelabel (CodeLabel);
4242 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4243 AddCodeLine ("sta (sp),y");
4244 AddCodeLine ("iny");
4245 AddCmpCodeIfSizeNot256 ("cpy #$%02X", Size);
4246 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4252 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4253 /* Initialize a static local variable from static initialization data */
4256 unsigned CodeLabel = GetLocalLabel ();
4257 AddCodeLine ("ldy #$%02X", Size-1);
4258 g_defcodelabel (CodeLabel);
4259 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4260 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4261 AddCodeLine ("dey");
4262 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4263 } else if (Size <= 256) {
4264 unsigned CodeLabel = GetLocalLabel ();
4265 AddCodeLine ("ldy #$00");
4266 g_defcodelabel (CodeLabel);
4267 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4268 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4269 AddCodeLine ("iny");
4270 AddCmpCodeIfSizeNot256 ("cpy #$%02X", Size);
4271 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4273 /* Use the easy way here: memcpy() */
4274 g_getimmed (CF_STATIC, VarLabel, 0);
4275 AddCodeLine ("jsr pushax");
4276 g_getimmed (CF_STATIC, InitLabel, 0);
4277 AddCodeLine ("jsr pushax");
4278 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4279 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4285 /*****************************************************************************/
4286 /* Switch statement */
4287 /*****************************************************************************/
4291 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4292 /* Generate code for a switch statement */
4294 unsigned NextLabel = 0;
4297 /* Setup registers and determine which compare insn to use */
4298 const char* Compare;
4301 Compare = "cmp #$%02X";
4304 Compare = "cpx #$%02X";
4307 AddCodeLine ("ldy sreg");
4308 Compare = "cpy #$%02X";
4311 AddCodeLine ("ldy sreg+1");
4312 Compare = "cpy #$%02X";
4315 Internal ("Invalid depth in g_switch: %u", Depth);
4318 /* Walk over all nodes */
4319 for (I = 0; I < CollCount (Nodes); ++I) {
4321 /* Get the next case node */
4322 CaseNode* N = CollAtUnchecked (Nodes, I);
4324 /* If we have a next label, define it */
4326 g_defcodelabel (NextLabel);
4330 /* Do the compare */
4331 AddCodeLine (Compare, CN_GetValue (N));
4333 /* If this is the last level, jump directly to the case code if found */
4336 /* Branch if equal */
4337 g_falsejump (0, CN_GetLabel (N));
4341 /* Determine the next label */
4342 if (I == CollCount (Nodes) - 1) {
4343 /* Last node means not found */
4344 g_truejump (0, DefaultLabel);
4346 /* Jump to the next check */
4347 NextLabel = GetLocalLabel ();
4348 g_truejump (0, NextLabel);
4351 /* Check the next level */
4352 g_switch (N->Nodes, DefaultLabel, Depth-1);
4357 /* If we go here, we haven't found the label */
4358 g_jump (DefaultLabel);
4363 /*****************************************************************************/
4364 /* User supplied assembler code */
4365 /*****************************************************************************/
4369 void g_asmcode (struct StrBuf* B)
4370 /* Output one line of assembler code. */
4372 AddCodeLine ("%.*s", (int) SB_GetLen (B), SB_GetConstBuf (B));