1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 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 /*****************************************************************************/
63 /*****************************************************************************/
65 /*****************************************************************************/
69 /* Compiler relative stack pointer */
74 /*****************************************************************************/
76 /*****************************************************************************/
80 static void typeerror (unsigned type)
81 /* Print an error message about an invalid operand type */
83 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
88 static void CheckLocalOffs (unsigned Offs)
89 /* Check the offset into the stack for 8bit range */
92 /* Too many local vars */
93 Error ("Too many local variables");
99 static const char* GetLabelName (unsigned Flags, unsigned long Label, long Offs)
101 static char Buf [256]; /* Label name */
103 /* Create the correct label name */
104 switch (Flags & CF_ADDRMASK) {
107 /* Static memory cell */
109 xsprintf (Buf, sizeof (Buf), "%s%+ld", LocalLabelName (Label), Offs);
111 xsprintf (Buf, sizeof (Buf), "%s", LocalLabelName (Label));
118 xsprintf (Buf, sizeof (Buf), "_%s%+ld", (char*) Label, Offs);
120 xsprintf (Buf, sizeof (Buf), "_%s", (char*) Label);
125 /* Absolute address */
126 xsprintf (Buf, sizeof (Buf), "$%04X", (int)((Label+Offs) & 0xFFFF));
130 /* Variable in register bank */
131 xsprintf (Buf, sizeof (Buf), "regbank+%u", (unsigned)((Label+Offs) & 0xFFFF));
135 Internal ("Invalid address flags: %04X", Flags);
138 /* Return a pointer to the static buffer */
144 /*****************************************************************************/
145 /* Pre- and postamble */
146 /*****************************************************************************/
150 void g_preamble (void)
151 /* Generate the assembler code preamble */
153 /* Create a new (global) segment list and remember it */
157 /* Identify the compiler version */
159 AddTextLine ("; File generated by cc65 v %u.%u.%u",
160 VER_MAJOR, VER_MINOR, VER_PATCH);
163 /* Insert some object file options */
164 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
165 VER_MAJOR, VER_MINOR, VER_PATCH);
167 /* If we're producing code for some other CPU, switch the command set */
168 if (CPU == CPU_65C02) {
169 AddTextLine ("\t.pc02");
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 the stack pointer for direct auto variable access */
182 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
184 /* Define long branch macros */
185 AddTextLine ("\t.macpack\tlongbranch");
190 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
191 /* If debug info is enabled, place a file info into the source */
194 /* We have to place this into the global text segment, so it will
195 * appear before all .dbg line statements.
197 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
203 /*****************************************************************************/
204 /* Segment support */
205 /*****************************************************************************/
209 void g_userodata (void)
210 /* Switch to the read only data segment */
212 UseDataSeg (SEG_RODATA);
217 void g_usedata (void)
218 /* Switch to the data segment */
220 UseDataSeg (SEG_DATA);
226 /* Switch to the bss segment */
228 UseDataSeg (SEG_BSS);
233 void g_segname (segment_t Seg, const char* Name)
234 /* Set the name of a segment */
238 /* Remember the new name */
239 NewSegName (Seg, Name);
241 /* Emit a segment directive for the data style segments */
243 case SEG_RODATA: S = CS->ROData; break;
244 case SEG_DATA: S = CS->Data; break;
245 case SEG_BSS: S = CS->BSS; break;
246 default: S = 0; break;
249 DS_AddLine (S, ".segment\t\"%s\"", Name);
255 /*****************************************************************************/
257 /*****************************************************************************/
261 unsigned sizeofarg (unsigned flags)
262 /* Return the size of a function argument type that is encoded in flags */
264 switch (flags & CF_TYPE) {
267 return (flags & CF_FORCECHAR)? 1 : 2;
284 int pop (unsigned flags)
285 /* Pop an argument of the given size */
287 return oursp += sizeofarg (flags);
292 int push (unsigned flags)
293 /* Push an argument of the given size */
295 return oursp -= 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 /*****************************************************************************/
352 /* Functions handling global labels */
353 /*****************************************************************************/
357 void g_defgloblabel (const char* Name)
358 /* Define a global label with the given name */
360 /* Global labels are always data labels */
361 AddDataLine ("_%s:", Name);
366 void g_defexport (const char* Name, int ZP)
367 /* Export the given label */
370 AddTextLine ("\t.exportzp\t_%s", Name);
372 AddTextLine ("\t.export\t\t_%s", Name);
378 void g_defimport (const char* Name, int ZP)
379 /* Import the given label */
382 AddTextLine ("\t.importzp\t_%s", Name);
384 AddTextLine ("\t.import\t\t_%s", Name);
390 /*****************************************************************************/
391 /* Load functions for various registers */
392 /*****************************************************************************/
396 static void ldaconst (unsigned val)
397 /* Load a with a constant */
399 AddCodeLine ("lda #$%02X", val & 0xFF);
404 static void ldxconst (unsigned val)
405 /* Load x with a constant */
407 AddCodeLine ("ldx #$%02X", val & 0xFF);
412 static void ldyconst (unsigned val)
413 /* Load y with a constant */
415 AddCodeLine ("ldy #$%02X", val & 0xFF);
420 /*****************************************************************************/
421 /* Function entry and exit */
422 /*****************************************************************************/
426 /* Remember the argument size of a function. The variable is set by g_enter
427 * and used by g_leave. If the functions gets its argument size by the caller
428 * (variable param list or function without prototype), g_enter will set the
434 void g_enter (unsigned flags, unsigned argsize)
435 /* Function prologue */
437 if ((flags & CF_FIXARGC) != 0) {
438 /* Just remember the argument size for the leave */
442 AddCodeLine ("jsr enter");
449 /* Function epilogue */
451 /* How many bytes of locals do we have to drop? */
454 /* If we didn't have a variable argument list, don't call leave */
457 /* Drop stackframe if needed */
461 AddCodeLine ("jsr incsp%d", k);
465 AddCodeLine ("jsr addysp");
472 /* Nothing to drop */
473 AddCodeLine ("jsr leave");
475 /* We've a stack frame to drop */
477 AddCodeLine ("jsr leavey");
481 /* Add the final rts */
487 /*****************************************************************************/
488 /* Register variables */
489 /*****************************************************************************/
493 void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes)
494 /* Swap a register variable with a location on the stack */
496 /* Calculate the actual stack offset and check it */
498 CheckLocalOffs (StackOffs);
503 if (CodeSizeFactor < 165) {
504 ldyconst (StackOffs);
506 AddCodeLine ("jsr regswap1");
508 ldyconst (StackOffs);
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 ldyconst (StackOffs);
520 AddCodeLine ("jsr regswap2");
524 ldyconst (StackOffs);
527 AddCodeLine ("jsr regswap");
533 void g_save_regvars (int RegOffs, unsigned Bytes)
534 /* Save register variables */
536 /* Don't loop for up to two bytes */
539 AddCodeLine ("lda regbank%+d", RegOffs);
540 AddCodeLine ("jsr pusha");
542 } else if (Bytes == 2) {
544 AddCodeLine ("lda regbank%+d", RegOffs);
545 AddCodeLine ("ldx regbank%+d", RegOffs+1);
546 AddCodeLine ("jsr pushax");
550 /* More than two bytes - loop */
551 unsigned Label = GetLocalLabel ();
553 ldyconst (Bytes - 1);
555 g_defcodelabel (Label);
556 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
557 AddCodeLine ("sta (sp),y");
560 AddCodeLine ("bne %s", LocalLabelName (Label));
564 /* We pushed stuff, correct the stack pointer */
570 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
571 /* Restore register variables */
573 /* Calculate the actual stack offset and check it */
575 CheckLocalOffs (StackOffs);
577 /* Don't loop for up to two bytes */
580 ldyconst (StackOffs);
581 AddCodeLine ("lda (sp),y");
582 AddCodeLine ("sta regbank%+d", RegOffs);
584 } else if (Bytes == 2) {
586 ldyconst (StackOffs);
587 AddCodeLine ("lda (sp),y");
588 AddCodeLine ("sta regbank%+d", RegOffs);
590 AddCodeLine ("lda (sp),y");
591 AddCodeLine ("sta regbank%+d", RegOffs+1);
595 /* More than two bytes - loop */
596 unsigned Label = GetLocalLabel ();
597 ldyconst (StackOffs+Bytes-1);
599 g_defcodelabel (Label);
600 AddCodeLine ("lda (sp),y");
601 AddCodeLine ("sta regbank%+d,x", RegOffs-1);
604 AddCodeLine ("bne %s", LocalLabelName (Label));
611 /*****************************************************************************/
612 /* Fetching memory cells */
613 /*****************************************************************************/
617 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
618 /* Load a constant into the primary register */
620 unsigned char B1, B2, B3, B4;
624 if ((Flags & CF_CONST) != 0) {
626 /* Numeric constant */
627 switch (Flags & CF_TYPE) {
630 if ((Flags & CF_FORCECHAR) != 0) {
636 ldxconst ((Val >> 8) & 0xFF);
637 ldaconst (Val & 0xFF);
641 /* Split the value into 4 bytes */
642 B1 = (unsigned char) (Val >> 0);
643 B2 = (unsigned char) (Val >> 8);
644 B3 = (unsigned char) (Val >> 16);
645 B4 = (unsigned char) (Val >> 24);
647 /* Remember which bytes are done */
651 AddCodeLine ("ldx #$%02X", B2);
654 AddCodeLine ("stx sreg");
658 AddCodeLine ("stx sreg+1");
661 if ((Done & 0x04) == 0 && B1 != B3) {
662 AddCodeLine ("lda #$%02X", B3);
663 AddCodeLine ("sta sreg");
666 if ((Done & 0x08) == 0 && B1 != B4) {
667 AddCodeLine ("lda #$%02X", B4);
668 AddCodeLine ("sta sreg+1");
671 AddCodeLine ("lda #$%02X", B1);
673 if ((Done & 0x04) == 0) {
675 AddCodeLine ("sta sreg");
677 if ((Done & 0x08) == 0) {
679 AddCodeLine ("sta sreg+1");
691 /* Some sort of label */
692 const char* Label = GetLabelName (Flags, Val, Offs);
694 /* Load the address into the primary */
695 AddCodeLine ("lda #<(%s)", Label);
696 AddCodeLine ("ldx #>(%s)", Label);
703 void g_getstatic (unsigned flags, unsigned long label, long offs)
704 /* Fetch an static memory cell into the primary register */
706 /* Create the correct label name */
707 const char* lbuf = GetLabelName (flags, label, offs);
709 /* Check the size and generate the correct load operation */
710 switch (flags & CF_TYPE) {
713 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
714 AddCodeLine ("lda %s", lbuf); /* load A from the label */
717 AddCodeLine ("lda %s", lbuf); /* load A from the label */
718 if (!(flags & CF_UNSIGNED)) {
719 /* Must sign extend */
720 unsigned L = GetLocalLabel ();
721 AddCodeLine ("bpl %s", LocalLabelName (L));
729 AddCodeLine ("lda %s", lbuf);
730 if (flags & CF_TEST) {
731 AddCodeLine ("ora %s+1", lbuf);
733 AddCodeLine ("ldx %s+1", lbuf);
738 if (flags & CF_TEST) {
739 AddCodeLine ("lda %s+3", lbuf);
740 AddCodeLine ("ora %s+2", lbuf);
741 AddCodeLine ("ora %s+1", lbuf);
742 AddCodeLine ("ora %s+0", lbuf);
744 AddCodeLine ("lda %s+3", lbuf);
745 AddCodeLine ("sta sreg+1");
746 AddCodeLine ("lda %s+2", lbuf);
747 AddCodeLine ("sta sreg");
748 AddCodeLine ("ldx %s+1", lbuf);
749 AddCodeLine ("lda %s", lbuf);
761 void g_getlocal (unsigned flags, int offs)
762 /* Fetch specified local object (local var). */
765 CheckLocalOffs (offs);
766 switch (flags & CF_TYPE) {
769 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
771 AddCodeLine ("lda (sp),y");
774 AddCodeLine ("ldx #$00");
775 AddCodeLine ("lda (sp),y");
776 if ((flags & CF_UNSIGNED) == 0) {
777 unsigned L = GetLocalLabel();
778 AddCodeLine ("bpl %s", LocalLabelName (L));
786 CheckLocalOffs (offs + 1);
787 if (flags & CF_TEST) {
789 AddCodeLine ("lda (sp),y");
791 AddCodeLine ("ora (sp),y");
794 AddCodeLine ("jsr ldaxysp");
800 AddCodeLine ("jsr ldeaxysp");
810 void g_getind (unsigned flags, unsigned offs)
811 /* Fetch the specified object type indirect through the primary register
812 * into the primary register
815 /* If the offset is greater than 255, add the part that is > 255 to
816 * the primary. This way we get an easy addition and use the low byte
819 offs = MakeByteOffs (flags, offs);
821 /* Handle the indirect fetch */
822 switch (flags & CF_TYPE) {
825 /* Character sized */
826 if (flags & CF_UNSIGNED) {
828 AddCodeLine ("jsr ldauidx");
831 AddCodeLine ("jsr ldaidx");
836 if (flags & CF_TEST) {
838 AddCodeLine ("sta ptr1");
839 AddCodeLine ("stx ptr1+1");
840 AddCodeLine ("lda (ptr1),y");
842 AddCodeLine ("ora (ptr1),y");
845 AddCodeLine ("jsr ldaxidx");
851 AddCodeLine ("jsr ldeaxidx");
852 if (flags & CF_TEST) {
853 AddCodeLine ("jsr tsteax");
865 void g_leasp (int offs)
866 /* Fetch the address of the specified symbol into the primary register */
868 /* Calculate the offset relative to sp */
871 /* For value 0 we do direct code */
873 AddCodeLine ("lda sp");
874 AddCodeLine ("ldx sp+1");
876 if (CodeSizeFactor < 300) {
877 ldaconst (offs); /* Load A with offset value */
878 AddCodeLine ("jsr leaasp"); /* Load effective address */
880 unsigned L = GetLocalLabel ();
881 if (CPU == CPU_65C02 && offs == 1) {
882 AddCodeLine ("lda sp");
883 AddCodeLine ("ldx sp+1");
885 AddCodeLine ("bne %s", LocalLabelName (L));
890 AddCodeLine ("ldx sp+1");
891 AddCodeLine ("adc sp");
892 AddCodeLine ("bcc %s", LocalLabelName (L));
902 void g_leavariadic (int Offs)
903 /* Fetch the address of a parameter in a variadic function into the primary
907 unsigned ArgSizeOffs;
909 /* Calculate the offset relative to sp */
912 /* Get the offset of the parameter which is stored at sp+0 on function
913 * entry and check if this offset is reachable with a byte offset.
916 ArgSizeOffs = -oursp;
917 CheckLocalOffs (ArgSizeOffs);
919 /* Get the size of all parameters. */
920 ldyconst (ArgSizeOffs);
921 AddCodeLine ("lda (sp),y");
923 /* Add the value of the stackpointer */
924 if (CodeSizeFactor > 250) {
925 unsigned L = GetLocalLabel();
926 AddCodeLine ("ldx sp+1");
928 AddCodeLine ("adc sp");
929 AddCodeLine ("bcc %s", LocalLabelName (L));
933 AddCodeLine ("jsr leaasp");
936 /* Add the offset to the primary */
938 g_inc (CF_INT | CF_CONST, Offs);
939 } else if (Offs < 0) {
940 g_dec (CF_INT | CF_CONST, -Offs);
946 /*****************************************************************************/
947 /* Store into memory */
948 /*****************************************************************************/
952 void g_putstatic (unsigned flags, unsigned long label, long offs)
953 /* Store the primary register into the specified static memory cell */
955 /* Create the correct label name */
956 const char* lbuf = GetLabelName (flags, label, offs);
958 /* Check the size and generate the correct store operation */
959 switch (flags & CF_TYPE) {
962 AddCodeLine ("sta %s", lbuf);
966 AddCodeLine ("sta %s", lbuf);
967 AddCodeLine ("stx %s+1", lbuf);
971 AddCodeLine ("sta %s", lbuf);
972 AddCodeLine ("stx %s+1", lbuf);
973 AddCodeLine ("ldy sreg");
974 AddCodeLine ("sty %s+2", lbuf);
975 AddCodeLine ("ldy sreg+1");
976 AddCodeLine ("sty %s+3", lbuf);
987 void g_putlocal (unsigned Flags, int Offs, long Val)
988 /* Put data into local object. */
991 CheckLocalOffs (Offs);
992 switch (Flags & CF_TYPE) {
995 if (Flags & CF_CONST) {
996 AddCodeLine ("lda #$%02X", (unsigned char) Val);
999 AddCodeLine ("sta (sp),y");
1003 if (Flags & CF_CONST) {
1005 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
1006 AddCodeLine ("sta (sp),y");
1007 if ((Flags & CF_NOKEEP) == 0) {
1008 /* Place high byte into X */
1009 AddCodeLine ("tax");
1011 if ((Val & 0xFF) == Offs+1) {
1012 /* The value we need is already in Y */
1013 AddCodeLine ("tya");
1014 AddCodeLine ("dey");
1016 AddCodeLine ("dey");
1017 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1019 AddCodeLine ("sta (sp),y");
1021 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
1023 AddCodeLine ("jsr staxysp");
1026 AddCodeLine ("sta (sp),y");
1027 AddCodeLine ("iny");
1028 AddCodeLine ("txa");
1029 AddCodeLine ("sta (sp),y");
1035 if (Flags & CF_CONST) {
1036 g_getimmed (Flags, Val, 0);
1039 AddCodeLine ("jsr steaxysp");
1050 void g_putind (unsigned Flags, unsigned Offs)
1051 /* Store the specified object type in the primary register at the address
1052 * on the top of the stack
1055 /* We can handle offsets below $100 directly, larger offsets must be added
1056 * to the address. Since a/x is in use, best code is achieved by adding
1057 * just the high byte. Be sure to check if the low byte will overflow while
1060 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1062 /* Overflow - we need to add the low byte also */
1063 AddCodeLine ("ldy #$00");
1064 AddCodeLine ("clc");
1065 AddCodeLine ("pha");
1066 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1067 AddCodeLine ("adc (sp),y");
1068 AddCodeLine ("sta (sp),y");
1069 AddCodeLine ("iny");
1070 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1071 AddCodeLine ("adc (sp),y");
1072 AddCodeLine ("sta (sp),y");
1073 AddCodeLine ("pla");
1075 /* Complete address is on stack, new offset is zero */
1078 } else if ((Offs & 0xFF00) != 0) {
1080 /* We can just add the high byte */
1081 AddCodeLine ("ldy #$01");
1082 AddCodeLine ("clc");
1083 AddCodeLine ("pha");
1084 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1085 AddCodeLine ("adc (sp),y");
1086 AddCodeLine ("sta (sp),y");
1087 AddCodeLine ("pla");
1089 /* Offset is now just the low byte */
1093 /* Check the size and determine operation */
1094 switch (Flags & CF_TYPE) {
1098 AddCodeLine ("jsr staspidx");
1103 AddCodeLine ("jsr staxspidx");
1108 AddCodeLine ("jsr steaxspidx");
1116 /* Pop the argument which is always a pointer */
1122 /*****************************************************************************/
1123 /* type conversion and similiar stuff */
1124 /*****************************************************************************/
1128 void g_toslong (unsigned flags)
1129 /* Make sure, the value on TOS is a long. Convert if necessary */
1131 switch (flags & CF_TYPE) {
1135 if (flags & CF_UNSIGNED) {
1136 AddCodeLine ("jsr tosulong");
1138 AddCodeLine ("jsr toslong");
1153 void g_tosint (unsigned flags)
1154 /* Make sure, the value on TOS is an int. Convert if necessary */
1156 switch (flags & CF_TYPE) {
1163 AddCodeLine ("jsr tosint");
1174 void g_regint (unsigned Flags)
1175 /* Make sure, the value in the primary register an int. Convert if necessary */
1179 switch (Flags & CF_TYPE) {
1182 if (Flags & CF_FORCECHAR) {
1183 /* Conversion is from char */
1184 if (Flags & CF_UNSIGNED) {
1185 AddCodeLine ("ldx #$00");
1187 L = GetLocalLabel();
1188 AddCodeLine ("ldx #$00");
1189 AddCodeLine ("cmp #$80");
1190 AddCodeLine ("bcc %s", LocalLabelName (L));
1191 AddCodeLine ("dex");
1208 void g_reglong (unsigned Flags)
1209 /* Make sure, the value in the primary register a long. Convert if necessary */
1213 switch (Flags & CF_TYPE) {
1216 if (Flags & CF_FORCECHAR) {
1217 /* Conversion is from char */
1218 if (Flags & CF_UNSIGNED) {
1219 if (CodeSizeFactor >= 200) {
1220 AddCodeLine ("ldx #$00");
1221 AddCodeLine ("stx sreg");
1222 AddCodeLine ("stx sreg+1");
1224 AddCodeLine ("jsr aulong");
1227 if (CodeSizeFactor >= 366) {
1228 L = GetLocalLabel();
1229 AddCodeLine ("ldx #$00");
1230 AddCodeLine ("cmp #$80");
1231 AddCodeLine ("bcc %s", LocalLabelName (L));
1232 AddCodeLine ("dex");
1234 AddCodeLine ("stx sreg");
1235 AddCodeLine ("stx sreg+1");
1237 AddCodeLine ("jsr along");
1244 if (Flags & CF_UNSIGNED) {
1245 if (CodeSizeFactor >= 200) {
1247 AddCodeLine ("sty sreg");
1248 AddCodeLine ("sty sreg+1");
1250 AddCodeLine ("jsr axulong");
1253 AddCodeLine ("jsr axlong");
1267 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1268 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1269 * value, that corresponds to the value on TOS, rhs corresponds to the value
1270 * in (e)ax. The return value is the the flags value for the resulting type.
1273 unsigned ltype, rtype;
1276 /* Get the type spec from the flags */
1277 ltype = lhs & CF_TYPE;
1278 rtype = rhs & CF_TYPE;
1280 /* Check if a conversion is needed */
1281 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1282 /* We must promote the primary register to long */
1284 /* Get the new rhs type */
1285 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1287 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1288 /* We must promote the lhs to long */
1294 /* Get the new rhs type */
1295 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1299 /* Determine the result type for the operation:
1300 * - The result is const if both operands are const.
1301 * - The result is unsigned if one of the operands is unsigned.
1302 * - The result is long if one of the operands is long.
1303 * - Otherwise the result is int sized.
1305 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1306 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1307 if (rtype == CF_LONG || ltype == CF_LONG) {
1317 unsigned g_typecast (unsigned lhs, unsigned rhs)
1318 /* Cast the value in the primary register to the operand size that is flagged
1319 * by the lhs value. Return the result value.
1322 unsigned ltype, rtype;
1324 /* Get the type spec from the flags */
1325 ltype = lhs & CF_TYPE;
1326 rtype = rhs & CF_TYPE;
1328 /* Check if a conversion is needed */
1329 if ((rhs & CF_CONST) == 0) {
1330 if (ltype == CF_LONG && rtype != CF_LONG) {
1331 /* We must promote the primary register to long */
1333 } else if (ltype == CF_INT && rtype != CF_INT) {
1334 /* We must promote the primary register to int */
1339 /* Do not need any other action. If the left type is int, and the primary
1340 * register is long, it will be automagically truncated. If the right hand
1341 * side is const, it is not located in the primary register and handled by
1342 * the expression parser code.
1345 /* Result is const if the right hand side was const */
1346 lhs |= (rhs & CF_CONST);
1348 /* The resulting type is that of the left hand side (that's why you called
1356 void g_scale (unsigned flags, long val)
1357 /* Scale the value in the primary register by the given value. If val is positive,
1358 * scale up, is val is negative, scale down. This function is used to scale
1359 * the operands or results of pointer arithmetic by the size of the type, the
1360 * pointer points to.
1365 /* Value may not be zero */
1367 Internal ("Data type has no size");
1368 } else if (val > 0) {
1371 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1373 /* Factor is 2, 4, 8 and 16, use special function */
1374 switch (flags & CF_TYPE) {
1377 if (flags & CF_FORCECHAR) {
1379 AddCodeLine ("asl a");
1386 if (CodeSizeFactor >= (p2+1)*130U) {
1387 AddCodeLine ("stx tmp1");
1389 AddCodeLine ("asl a");
1390 AddCodeLine ("rol tmp1");
1392 AddCodeLine ("ldx tmp1");
1394 if (flags & CF_UNSIGNED) {
1395 AddCodeLine ("jsr shlax%d", p2);
1397 AddCodeLine ("jsr aslax%d", p2);
1403 if (flags & CF_UNSIGNED) {
1404 AddCodeLine ("jsr shleax%d", p2);
1406 AddCodeLine ("jsr asleax%d", p2);
1415 } else if (val != 1) {
1417 /* Use a multiplication instead */
1418 g_mul (flags | CF_CONST, val);
1426 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1428 /* Factor is 2, 4, 8 and 16 use special function */
1429 switch (flags & CF_TYPE) {
1432 if (flags & CF_FORCECHAR) {
1433 if (flags & CF_UNSIGNED) {
1435 AddCodeLine ("lsr a");
1438 } else if (p2 <= 2) {
1439 AddCodeLine ("cmp #$80");
1440 AddCodeLine ("ror a");
1447 if (flags & CF_UNSIGNED) {
1448 if (CodeSizeFactor >= (p2+1)*130U) {
1449 AddCodeLine ("stx tmp1");
1451 AddCodeLine ("lsr tmp1");
1452 AddCodeLine ("ror a");
1454 AddCodeLine ("ldx tmp1");
1456 AddCodeLine ("jsr lsrax%d", p2);
1459 if (CodeSizeFactor >= (p2+1)*150U) {
1460 AddCodeLine ("stx tmp1");
1462 AddCodeLine ("cpx #$80");
1463 AddCodeLine ("ror tmp1");
1464 AddCodeLine ("ror a");
1466 AddCodeLine ("ldx tmp1");
1468 AddCodeLine ("jsr asrax%d", p2);
1474 if (flags & CF_UNSIGNED) {
1475 AddCodeLine ("jsr lsreax%d", p2);
1477 AddCodeLine ("jsr asreax%d", p2);
1486 } else if (val != 1) {
1488 /* Use a division instead */
1489 g_div (flags | CF_CONST, val);
1497 /*****************************************************************************/
1498 /* Adds and subs of variables fix a fixed address */
1499 /*****************************************************************************/
1503 void g_addlocal (unsigned flags, int offs)
1504 /* Add a local variable to ax */
1508 /* Correct the offset and check it */
1510 CheckLocalOffs (offs);
1512 switch (flags & CF_TYPE) {
1515 L = GetLocalLabel();
1516 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1517 AddCodeLine ("clc");
1518 AddCodeLine ("adc (sp),y");
1519 AddCodeLine ("bcc %s", LocalLabelName (L));
1520 AddCodeLine ("inx");
1525 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1526 AddCodeLine ("clc");
1527 AddCodeLine ("adc (sp),y");
1528 AddCodeLine ("pha");
1529 AddCodeLine ("txa");
1530 AddCodeLine ("iny");
1531 AddCodeLine ("adc (sp),y");
1532 AddCodeLine ("tax");
1533 AddCodeLine ("pla");
1537 /* Do it the old way */
1539 g_getlocal (flags, offs);
1551 void g_addstatic (unsigned flags, unsigned long label, long offs)
1552 /* Add a static variable to ax */
1556 /* Create the correct label name */
1557 const char* lbuf = GetLabelName (flags, label, offs);
1559 switch (flags & CF_TYPE) {
1562 L = GetLocalLabel();
1563 AddCodeLine ("clc");
1564 AddCodeLine ("adc %s", lbuf);
1565 AddCodeLine ("bcc %s", LocalLabelName (L));
1566 AddCodeLine ("inx");
1571 AddCodeLine ("clc");
1572 AddCodeLine ("adc %s", lbuf);
1573 AddCodeLine ("tay");
1574 AddCodeLine ("txa");
1575 AddCodeLine ("adc %s+1", lbuf);
1576 AddCodeLine ("tax");
1577 AddCodeLine ("tya");
1581 /* Do it the old way */
1583 g_getstatic (flags, label, offs);
1595 /*****************************************************************************/
1596 /* Special op= functions */
1597 /*****************************************************************************/
1601 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1603 /* Emit += for a static variable */
1605 /* Create the correct label name */
1606 const char* lbuf = GetLabelName (flags, label, offs);
1608 /* Check the size and determine operation */
1609 switch (flags & CF_TYPE) {
1612 if (flags & CF_FORCECHAR) {
1613 AddCodeLine ("ldx #$00");
1614 if (flags & CF_CONST) {
1616 AddCodeLine ("inc %s", lbuf);
1617 AddCodeLine ("lda %s", lbuf);
1619 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1620 AddCodeLine ("clc");
1621 AddCodeLine ("adc %s", lbuf);
1622 AddCodeLine ("sta %s", lbuf);
1625 AddCodeLine ("clc");
1626 AddCodeLine ("adc %s", lbuf);
1627 AddCodeLine ("sta %s", lbuf);
1629 if ((flags & CF_UNSIGNED) == 0) {
1630 unsigned L = GetLocalLabel();
1631 AddCodeLine ("bpl %s", LocalLabelName (L));
1632 AddCodeLine ("dex");
1640 if (flags & CF_CONST) {
1642 unsigned L = GetLocalLabel ();
1643 AddCodeLine ("inc %s", lbuf);
1644 AddCodeLine ("bne %s", LocalLabelName (L));
1645 AddCodeLine ("inc %s+1", lbuf);
1647 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1648 AddCodeLine ("ldx %s+1", lbuf);
1650 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1651 AddCodeLine ("clc");
1652 AddCodeLine ("adc %s", lbuf);
1653 AddCodeLine ("sta %s", lbuf);
1655 unsigned L = GetLocalLabel ();
1656 AddCodeLine ("bcc %s", LocalLabelName (L));
1657 AddCodeLine ("inc %s+1", lbuf);
1659 AddCodeLine ("ldx %s+1", lbuf);
1661 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1662 AddCodeLine ("adc %s+1", lbuf);
1663 AddCodeLine ("sta %s+1", lbuf);
1664 AddCodeLine ("tax");
1665 AddCodeLine ("lda %s", lbuf);
1669 AddCodeLine ("clc");
1670 AddCodeLine ("adc %s", lbuf);
1671 AddCodeLine ("sta %s", lbuf);
1672 AddCodeLine ("txa");
1673 AddCodeLine ("adc %s+1", lbuf);
1674 AddCodeLine ("sta %s+1", lbuf);
1675 AddCodeLine ("tax");
1676 AddCodeLine ("lda %s", lbuf);
1681 if (flags & CF_CONST) {
1683 AddCodeLine ("ldy #<(%s)", lbuf);
1684 AddCodeLine ("sty ptr1");
1685 AddCodeLine ("ldy #>(%s)", lbuf);
1687 AddCodeLine ("jsr laddeq1");
1689 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1690 AddCodeLine ("jsr laddeqa");
1693 g_getstatic (flags, label, offs);
1695 g_putstatic (flags, label, offs);
1698 AddCodeLine ("ldy #<(%s)", lbuf);
1699 AddCodeLine ("sty ptr1");
1700 AddCodeLine ("ldy #>(%s)", lbuf);
1701 AddCodeLine ("jsr laddeq");
1712 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1713 /* Emit += for a local variable */
1715 /* Calculate the true offset, check it, load it into Y */
1717 CheckLocalOffs (offs);
1719 /* Check the size and determine operation */
1720 switch (flags & CF_TYPE) {
1723 if (flags & CF_FORCECHAR) {
1725 AddCodeLine ("ldx #$00");
1726 if (flags & CF_CONST) {
1727 AddCodeLine ("clc");
1728 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1729 AddCodeLine ("adc (sp),y");
1730 AddCodeLine ("sta (sp),y");
1732 AddCodeLine ("clc");
1733 AddCodeLine ("adc (sp),y");
1734 AddCodeLine ("sta (sp),y");
1736 if ((flags & CF_UNSIGNED) == 0) {
1737 unsigned L = GetLocalLabel();
1738 AddCodeLine ("bpl %s", LocalLabelName (L));
1739 AddCodeLine ("dex");
1748 if (flags & CF_CONST) {
1749 if (CodeSizeFactor >= 400) {
1750 AddCodeLine ("clc");
1751 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1752 AddCodeLine ("adc (sp),y");
1753 AddCodeLine ("sta (sp),y");
1754 AddCodeLine ("iny");
1755 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1756 AddCodeLine ("adc (sp),y");
1757 AddCodeLine ("sta (sp),y");
1758 AddCodeLine ("tax");
1759 AddCodeLine ("dey");
1760 AddCodeLine ("lda (sp),y");
1762 g_getimmed (flags, val, 0);
1763 AddCodeLine ("jsr addeqysp");
1766 AddCodeLine ("jsr addeqysp");
1771 if (flags & CF_CONST) {
1772 g_getimmed (flags, val, 0);
1775 AddCodeLine ("jsr laddeqysp");
1785 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1786 /* Emit += for the location with address in ax */
1788 /* If the offset is too large for a byte register, add the high byte
1789 * of the offset to the primary. Beware: We need a special correction
1790 * if the offset in the low byte will overflow in the operation.
1792 offs = MakeByteOffs (flags, offs);
1794 /* Check the size and determine operation */
1795 switch (flags & CF_TYPE) {
1798 AddCodeLine ("sta ptr1");
1799 AddCodeLine ("stx ptr1+1");
1800 AddCodeLine ("ldy #$%02X", offs);
1801 AddCodeLine ("ldx #$00");
1802 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1803 AddCodeLine ("clc");
1804 AddCodeLine ("adc (ptr1),y");
1805 AddCodeLine ("sta (ptr1),y");
1809 if (CodeSizeFactor >= 200) {
1810 /* Lots of code, use only if size is not important */
1811 AddCodeLine ("sta ptr1");
1812 AddCodeLine ("stx ptr1+1");
1813 AddCodeLine ("ldy #$%02X", offs);
1814 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1815 AddCodeLine ("clc");
1816 AddCodeLine ("adc (ptr1),y");
1817 AddCodeLine ("sta (ptr1),y");
1818 AddCodeLine ("pha");
1819 AddCodeLine ("iny");
1820 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1821 AddCodeLine ("adc (ptr1),y");
1822 AddCodeLine ("sta (ptr1),y");
1823 AddCodeLine ("tax");
1824 AddCodeLine ("pla");
1830 AddCodeLine ("jsr pushax"); /* Push the address */
1831 push (CF_PTR); /* Correct the internal sp */
1832 g_getind (flags, offs); /* Fetch the value */
1833 g_inc (flags, val); /* Increment value in primary */
1834 g_putind (flags, offs); /* Store the value back */
1844 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1846 /* Emit -= for a static variable */
1848 /* Create the correct label name */
1849 const char* lbuf = GetLabelName (flags, label, offs);
1851 /* Check the size and determine operation */
1852 switch (flags & CF_TYPE) {
1855 if (flags & CF_FORCECHAR) {
1856 AddCodeLine ("ldx #$00");
1857 if (flags & CF_CONST) {
1859 AddCodeLine ("dec %s", lbuf);
1860 AddCodeLine ("lda %s", lbuf);
1862 AddCodeLine ("sec");
1863 AddCodeLine ("lda %s", lbuf);
1864 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1865 AddCodeLine ("sta %s", lbuf);
1868 AddCodeLine ("eor #$FF");
1869 AddCodeLine ("sec");
1870 AddCodeLine ("adc %s", lbuf);
1871 AddCodeLine ("sta %s", lbuf);
1873 if ((flags & CF_UNSIGNED) == 0) {
1874 unsigned L = GetLocalLabel();
1875 AddCodeLine ("bpl %s", LocalLabelName (L));
1876 AddCodeLine ("dex");
1884 AddCodeLine ("sec");
1885 if (flags & CF_CONST) {
1886 AddCodeLine ("lda %s", lbuf);
1887 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1888 AddCodeLine ("sta %s", lbuf);
1890 unsigned L = GetLocalLabel ();
1891 AddCodeLine ("bcs %s", LocalLabelName (L));
1892 AddCodeLine ("dec %s+1", lbuf);
1894 AddCodeLine ("ldx %s+1", lbuf);
1896 AddCodeLine ("lda %s+1", lbuf);
1897 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1898 AddCodeLine ("sta %s+1", lbuf);
1899 AddCodeLine ("tax");
1900 AddCodeLine ("lda %s", lbuf);
1903 AddCodeLine ("eor #$FF");
1904 AddCodeLine ("adc %s", lbuf);
1905 AddCodeLine ("sta %s", lbuf);
1906 AddCodeLine ("txa");
1907 AddCodeLine ("eor #$FF");
1908 AddCodeLine ("adc %s+1", lbuf);
1909 AddCodeLine ("sta %s+1", lbuf);
1910 AddCodeLine ("tax");
1911 AddCodeLine ("lda %s", lbuf);
1916 if (flags & CF_CONST) {
1918 AddCodeLine ("ldy #<(%s)", lbuf);
1919 AddCodeLine ("sty ptr1");
1920 AddCodeLine ("ldy #>(%s)", lbuf);
1921 AddCodeLine ("lda #$%02X", (unsigned char)val);
1922 AddCodeLine ("jsr lsubeqa");
1924 g_getstatic (flags, label, offs);
1926 g_putstatic (flags, label, offs);
1929 AddCodeLine ("ldy #<(%s)", lbuf);
1930 AddCodeLine ("sty ptr1");
1931 AddCodeLine ("ldy #>(%s)", lbuf);
1932 AddCodeLine ("jsr lsubeq");
1943 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1944 /* Emit -= for a local variable */
1946 /* Calculate the true offset, check it, load it into Y */
1948 CheckLocalOffs (offs);
1950 /* Check the size and determine operation */
1951 switch (flags & CF_TYPE) {
1954 if (flags & CF_FORCECHAR) {
1956 AddCodeLine ("ldx #$00");
1957 AddCodeLine ("sec");
1958 if (flags & CF_CONST) {
1959 AddCodeLine ("lda (sp),y");
1960 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1962 AddCodeLine ("eor #$FF");
1963 AddCodeLine ("adc (sp),y");
1965 AddCodeLine ("sta (sp),y");
1966 if ((flags & CF_UNSIGNED) == 0) {
1967 unsigned L = GetLocalLabel();
1968 AddCodeLine ("bpl %s", LocalLabelName (L));
1969 AddCodeLine ("dex");
1977 if (flags & CF_CONST) {
1978 g_getimmed (flags, val, 0);
1981 AddCodeLine ("jsr subeqysp");
1985 if (flags & CF_CONST) {
1986 g_getimmed (flags, val, 0);
1989 AddCodeLine ("jsr lsubeqysp");
1999 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
2000 /* Emit -= for the location with address in ax */
2002 /* If the offset is too large for a byte register, add the high byte
2003 * of the offset to the primary. Beware: We need a special correction
2004 * if the offset in the low byte will overflow in the operation.
2006 offs = MakeByteOffs (flags, offs);
2008 /* Check the size and determine operation */
2009 switch (flags & CF_TYPE) {
2012 AddCodeLine ("sta ptr1");
2013 AddCodeLine ("stx ptr1+1");
2014 AddCodeLine ("ldy #$%02X", offs);
2015 AddCodeLine ("ldx #$00");
2016 AddCodeLine ("lda (ptr1),y");
2017 AddCodeLine ("sec");
2018 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2019 AddCodeLine ("sta (ptr1),y");
2023 if (CodeSizeFactor >= 200) {
2024 /* Lots of code, use only if size is not important */
2025 AddCodeLine ("sta ptr1");
2026 AddCodeLine ("stx ptr1+1");
2027 AddCodeLine ("ldy #$%02X", offs);
2028 AddCodeLine ("lda (ptr1),y");
2029 AddCodeLine ("sec");
2030 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2031 AddCodeLine ("sta (ptr1),y");
2032 AddCodeLine ("pha");
2033 AddCodeLine ("iny");
2034 AddCodeLine ("lda (ptr1),y");
2035 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2036 AddCodeLine ("sta (ptr1),y");
2037 AddCodeLine ("tax");
2038 AddCodeLine ("pla");
2044 AddCodeLine ("jsr pushax"); /* Push the address */
2045 push (CF_PTR); /* Correct the internal sp */
2046 g_getind (flags, offs); /* Fetch the value */
2047 g_dec (flags, val); /* Increment value in primary */
2048 g_putind (flags, offs); /* Store the value back */
2058 /*****************************************************************************/
2059 /* Add a variable address to the value in ax */
2060 /*****************************************************************************/
2064 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2065 /* Add the address of a local variable to ax */
2069 /* Add the offset */
2072 /* We cannot address more then 256 bytes of locals anyway */
2073 L = GetLocalLabel();
2074 CheckLocalOffs (offs);
2075 AddCodeLine ("clc");
2076 AddCodeLine ("adc #$%02X", offs & 0xFF);
2077 /* Do also skip the CLC insn below */
2078 AddCodeLine ("bcc %s", LocalLabelName (L));
2079 AddCodeLine ("inx");
2082 /* Add the current stackpointer value */
2083 AddCodeLine ("clc");
2085 /* Label was used above */
2088 AddCodeLine ("adc sp");
2089 AddCodeLine ("tay");
2090 AddCodeLine ("txa");
2091 AddCodeLine ("adc sp+1");
2092 AddCodeLine ("tax");
2093 AddCodeLine ("tya");
2098 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2099 /* Add the address of a static variable to ax */
2101 /* Create the correct label name */
2102 const char* lbuf = GetLabelName (flags, label, offs);
2104 /* Add the address to the current ax value */
2105 AddCodeLine ("clc");
2106 AddCodeLine ("adc #<(%s)", lbuf);
2107 AddCodeLine ("tay");
2108 AddCodeLine ("txa");
2109 AddCodeLine ("adc #>(%s)", lbuf);
2110 AddCodeLine ("tax");
2111 AddCodeLine ("tya");
2116 /*****************************************************************************/
2118 /*****************************************************************************/
2122 void g_save (unsigned flags)
2123 /* Copy primary register to hold register. */
2125 /* Check the size and determine operation */
2126 switch (flags & CF_TYPE) {
2129 if (flags & CF_FORCECHAR) {
2130 AddCodeLine ("pha");
2136 AddCodeLine ("sta regsave");
2137 AddCodeLine ("stx regsave+1");
2141 AddCodeLine ("jsr saveeax");
2151 void g_restore (unsigned flags)
2152 /* Copy hold register to primary. */
2154 /* Check the size and determine operation */
2155 switch (flags & CF_TYPE) {
2158 if (flags & CF_FORCECHAR) {
2159 AddCodeLine ("pla");
2165 AddCodeLine ("lda regsave");
2166 AddCodeLine ("ldx regsave+1");
2170 AddCodeLine ("jsr resteax");
2180 void g_cmp (unsigned flags, unsigned long val)
2181 /* Immidiate compare. The primary register will not be changed, Z flag
2187 /* Check the size and determine operation */
2188 switch (flags & CF_TYPE) {
2191 if (flags & CF_FORCECHAR) {
2192 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2198 L = GetLocalLabel();
2199 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2200 AddCodeLine ("bne %s", LocalLabelName (L));
2201 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2206 Internal ("g_cmp: Long compares not implemented");
2216 static void oper (unsigned flags, unsigned long val, char** subs)
2217 /* Encode a binary operation. subs is a pointer to four groups of three
2219 * 0-2 --> Operate on ints
2220 * 3-5 --> Operate on unsigneds
2221 * 6-8 --> Operate on longs
2222 * 9-11 --> Operate on unsigned longs
2224 * The first subroutine names in each string group is used to encode an
2225 * operation with a zero constant, the second to encode an operation with
2226 * a 8 bit constant, and the third is used in all other cases.
2231 /* Determine the offset into the array */
2232 offs = (flags & CF_UNSIGNED)? 3 : 0;
2233 switch (flags & CF_TYPE) {
2246 /* Encode the operation */
2247 if (flags & CF_CONST) {
2248 /* Constant value given */
2249 if (val == 0 && subs [offs+0]) {
2250 /* Special case: constant with value zero */
2251 AddCodeLine ("jsr %s", subs [offs+0]);
2252 } else if (val < 0x100 && subs [offs+1]) {
2253 /* Special case: constant with high byte zero */
2254 ldaconst (val); /* Load low byte */
2255 AddCodeLine ("jsr %s", subs [offs+1]);
2257 /* Others: arbitrary constant value */
2258 g_getimmed (flags, val, 0); /* Load value */
2259 AddCodeLine ("jsr %s", subs [offs+2]);
2262 /* Value not constant (is already in (e)ax) */
2263 AddCodeLine ("jsr %s", subs [offs+2]);
2266 /* The operation will pop it's argument */
2272 void g_test (unsigned flags)
2273 /* Test the value in the primary and set the condition codes */
2275 switch (flags & CF_TYPE) {
2278 if (flags & CF_FORCECHAR) {
2279 AddCodeLine ("tax");
2285 AddCodeLine ("stx tmp1");
2286 AddCodeLine ("ora tmp1");
2290 if (flags & CF_UNSIGNED) {
2291 AddCodeLine ("jsr utsteax");
2293 AddCodeLine ("jsr tsteax");
2305 void g_push (unsigned flags, unsigned long val)
2306 /* Push the primary register or a constant value onto the stack */
2308 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2310 /* We have a constant 8 or 16 bit value */
2311 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2313 /* Handle as 8 bit value */
2315 AddCodeLine ("jsr pusha");
2319 /* Handle as 16 bit value */
2320 g_getimmed (flags, val, 0);
2321 AddCodeLine ("jsr pushax");
2326 /* Value is not 16 bit or not constant */
2327 if (flags & CF_CONST) {
2328 /* Constant 32 bit value, load into eax */
2329 g_getimmed (flags, val, 0);
2332 /* Push the primary register */
2333 switch (flags & CF_TYPE) {
2336 if (flags & CF_FORCECHAR) {
2337 /* Handle as char */
2338 AddCodeLine ("jsr pusha");
2343 AddCodeLine ("jsr pushax");
2347 AddCodeLine ("jsr pusheax");
2357 /* Adjust the stack offset */
2363 void g_swap (unsigned flags)
2364 /* Swap the primary register and the top of the stack. flags give the type
2365 * of *both* values (must have same size).
2368 switch (flags & CF_TYPE) {
2372 AddCodeLine ("jsr swapstk");
2376 AddCodeLine ("jsr swapestk");
2387 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2388 /* Call the specified subroutine name */
2390 if ((Flags & CF_FIXARGC) == 0) {
2391 /* Pass the argument count */
2394 AddCodeLine ("jsr _%s", Label);
2395 oursp += ArgSize; /* callee pops args */
2400 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2401 /* Call subroutine indirect */
2403 if ((Flags & CF_LOCAL) == 0) {
2404 /* Address is in a/x */
2405 if ((Flags & CF_FIXARGC) == 0) {
2406 /* Pass arg count */
2409 AddCodeLine ("jsr callax");
2411 /* The address is on stack, offset is on Val */
2413 CheckLocalOffs (Offs);
2414 AddCodeLine ("pha");
2415 AddCodeLine ("ldy #$%02X", Offs);
2416 AddCodeLine ("lda (sp),y");
2417 AddCodeLine ("sta jmpvec+1");
2418 AddCodeLine ("iny");
2419 AddCodeLine ("lda (sp),y");
2420 AddCodeLine ("sta jmpvec+2");
2421 AddCodeLine ("pla");
2422 AddCodeLine ("jsr jmpvec");
2425 /* Callee pops args */
2431 void g_jump (unsigned Label)
2432 /* Jump to specified internal label number */
2434 AddCodeLine ("jmp %s", LocalLabelName (Label));
2439 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2440 /* Jump to label if zero flag clear */
2442 AddCodeLine ("jne %s", LocalLabelName (label));
2447 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2448 /* Jump to label if zero flag set */
2450 AddCodeLine ("jeq %s", LocalLabelName (label));
2455 static void mod_internal (int k, char* verb1, char* verb2)
2458 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2462 AddCodeLine ("jsr %ssp", verb2);
2468 void g_space (int space)
2469 /* Create or drop space on the stack */
2472 mod_internal (-space, "inc", "addy");
2473 } else if (space > 0) {
2474 mod_internal (space, "dec", "suby");
2480 void g_cstackcheck (void)
2481 /* Check for a C stack overflow */
2483 AddCodeLine ("jsr cstkchk");
2488 void g_stackcheck (void)
2489 /* Check for a stack overflow */
2491 AddCodeLine ("jsr stkchk");
2496 void g_add (unsigned flags, unsigned long val)
2497 /* Primary = TOS + Primary */
2499 static char* ops [12] = {
2500 0, "tosadda0", "tosaddax",
2501 0, "tosadda0", "tosaddax",
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 char* ops [12] = {
2519 0, "tossuba0", "tossubax",
2520 0, "tossuba0", "tossubax",
2525 if (flags & CF_CONST) {
2526 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2527 g_push (flags & ~CF_CONST, 0);
2529 oper (flags, val, ops);
2534 void g_rsub (unsigned flags, unsigned long val)
2535 /* Primary = Primary - TOS */
2537 static char* ops [12] = {
2538 0, "tosrsuba0", "tosrsubax",
2539 0, "tosrsuba0", "tosrsubax",
2543 oper (flags, val, ops);
2548 void g_mul (unsigned flags, unsigned long val)
2549 /* Primary = TOS * Primary */
2551 static char* ops [12] = {
2552 0, "tosmula0", "tosmulax",
2553 0, "tosumula0", "tosumulax",
2560 /* Do strength reduction if the value is constant and a power of two */
2561 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2562 /* Generate a shift instead */
2567 /* If the right hand side is const, the lhs is not on stack but still
2568 * in the primary register.
2570 if (flags & CF_CONST) {
2572 switch (flags & CF_TYPE) {
2575 if (flags & CF_FORCECHAR) {
2576 /* Handle some special cases */
2580 AddCodeLine ("sta tmp1");
2581 AddCodeLine ("asl a");
2582 AddCodeLine ("clc");
2583 AddCodeLine ("adc tmp1");
2587 AddCodeLine ("sta tmp1");
2588 AddCodeLine ("asl a");
2589 AddCodeLine ("asl a");
2590 AddCodeLine ("clc");
2591 AddCodeLine ("adc tmp1");
2595 AddCodeLine ("sta tmp1");
2596 AddCodeLine ("asl a");
2597 AddCodeLine ("clc");
2598 AddCodeLine ("adc tmp1");
2599 AddCodeLine ("asl a");
2603 AddCodeLine ("sta tmp1");
2604 AddCodeLine ("asl a");
2605 AddCodeLine ("asl a");
2606 AddCodeLine ("clc");
2607 AddCodeLine ("adc tmp1");
2608 AddCodeLine ("asl a");
2617 AddCodeLine ("jsr mulax3");
2620 AddCodeLine ("jsr mulax5");
2623 AddCodeLine ("jsr mulax6");
2626 AddCodeLine ("jsr mulax7");
2629 AddCodeLine ("jsr mulax9");
2632 AddCodeLine ("jsr mulax10");
2644 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2645 * into the normal, non-optimized stuff.
2647 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2648 g_push (flags & ~CF_CONST, 0);
2652 /* Use long way over the stack */
2653 oper (flags, val, ops);
2658 void g_div (unsigned flags, unsigned long val)
2659 /* Primary = TOS / Primary */
2661 static char* ops [12] = {
2662 0, "tosdiva0", "tosdivax",
2663 0, "tosudiva0", "tosudivax",
2668 /* Do strength reduction if the value is constant and a power of two */
2670 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2671 /* Generate a shift instead */
2674 /* Generate a division */
2675 if (flags & CF_CONST) {
2676 /* lhs is not on stack */
2677 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2678 g_push (flags & ~CF_CONST, 0);
2680 oper (flags, val, ops);
2686 void g_mod (unsigned flags, unsigned long val)
2687 /* Primary = TOS % Primary */
2689 static char* ops [12] = {
2690 0, "tosmoda0", "tosmodax",
2691 0, "tosumoda0", "tosumodax",
2697 /* Check if we can do some cost reduction */
2698 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2699 /* We can do that with an AND operation */
2700 g_and (flags, val - 1);
2702 /* Do it the hard way... */
2703 if (flags & CF_CONST) {
2704 /* lhs is not on stack */
2705 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2706 g_push (flags & ~CF_CONST, 0);
2708 oper (flags, val, ops);
2714 void g_or (unsigned flags, unsigned long val)
2715 /* Primary = TOS | Primary */
2717 static char* ops [12] = {
2718 0, "tosora0", "tosorax",
2719 0, "tosora0", "tosorax",
2724 /* If the right hand side is const, the lhs is not on stack but still
2725 * in the primary register.
2727 if (flags & CF_CONST) {
2729 switch (flags & CF_TYPE) {
2732 if (flags & CF_FORCECHAR) {
2733 if ((val & 0xFF) != 0xFF) {
2734 AddCodeLine ("ora #$%02X", (unsigned char)val);
2742 AddCodeLine ("ora #$%02X", (unsigned char)val);
2749 AddCodeLine ("ora #$%02X", (unsigned char)val);
2758 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2759 * into the normal, non-optimized stuff.
2761 g_push (flags & ~CF_CONST, 0);
2765 /* Use long way over the stack */
2766 oper (flags, val, ops);
2771 void g_xor (unsigned flags, unsigned long val)
2772 /* Primary = TOS ^ Primary */
2774 static char* ops [12] = {
2775 0, "tosxora0", "tosxorax",
2776 0, "tosxora0", "tosxorax",
2782 /* If the right hand side is const, the lhs is not on stack but still
2783 * in the primary register.
2785 if (flags & CF_CONST) {
2787 switch (flags & CF_TYPE) {
2790 if (flags & CF_FORCECHAR) {
2791 if ((val & 0xFF) != 0) {
2792 AddCodeLine ("eor #$%02X", (unsigned char)val);
2801 AddCodeLine ("eor #$%02X", (unsigned char)val);
2804 } else if ((val & 0xFF) == 0) {
2805 AddCodeLine ("pha");
2806 AddCodeLine ("txa");
2807 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2808 AddCodeLine ("tax");
2809 AddCodeLine ("pla");
2817 AddCodeLine ("eor #$%02X", (unsigned char)val);
2827 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2828 * into the normal, non-optimized stuff.
2830 g_push (flags & ~CF_CONST, 0);
2834 /* Use long way over the stack */
2835 oper (flags, val, ops);
2840 void g_and (unsigned flags, unsigned long val)
2841 /* Primary = TOS & Primary */
2843 static char* ops [12] = {
2844 0, "tosanda0", "tosandax",
2845 0, "tosanda0", "tosandax",
2850 /* If the right hand side is const, the lhs is not on stack but still
2851 * in the primary register.
2853 if (flags & CF_CONST) {
2855 switch (flags & CF_TYPE) {
2858 if (flags & CF_FORCECHAR) {
2859 AddCodeLine ("and #$%02X", (unsigned char)val);
2864 if ((val & 0xFFFF) != 0xFFFF) {
2869 } else if (val != 0xFF) {
2870 AddCodeLine ("and #$%02X", (unsigned char)val);
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");
2880 AddCodeLine ("tay");
2881 AddCodeLine ("txa");
2882 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2883 AddCodeLine ("tax");
2884 AddCodeLine ("tya");
2885 if ((val & 0x00FF) != 0x00FF) {
2886 AddCodeLine ("and #$%02X", (unsigned char)val);
2895 AddCodeLine ("stx sreg+1");
2896 AddCodeLine ("stx sreg");
2897 if ((val & 0xFF) != 0xFF) {
2898 AddCodeLine ("and #$%02X", (unsigned char)val);
2901 } else if (val == 0xFF00) {
2903 AddCodeLine ("sta sreg+1");
2904 AddCodeLine ("sta sreg");
2913 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2914 * into the normal, non-optimized stuff.
2916 g_push (flags & ~CF_CONST, 0);
2920 /* Use long way over the stack */
2921 oper (flags, val, ops);
2926 void g_asr (unsigned flags, unsigned long val)
2927 /* Primary = TOS >> Primary */
2929 static char* ops [12] = {
2930 0, "tosasra0", "tosasrax",
2931 0, "tosshra0", "tosshrax",
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_TYPE) {
2945 if (val >= 8 && (flags & CF_UNSIGNED)) {
2946 AddCodeLine ("txa");
2953 } else if (val >= 1 && val <= 4) {
2954 if (flags & CF_UNSIGNED) {
2955 AddCodeLine ("jsr shrax%ld", val);
2957 AddCodeLine ("jsr asrax%ld", val);
2967 } else if (val >= 1 && val <= 4) {
2968 if (flags & CF_UNSIGNED) {
2969 AddCodeLine ("jsr shreax%ld", val);
2971 AddCodeLine ("jsr asreax%ld", val);
2974 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2975 AddCodeLine ("txa");
2976 AddCodeLine ("ldx sreg");
2977 AddCodeLine ("ldy sreg+1");
2978 AddCodeLine ("sty sreg");
2979 AddCodeLine ("ldy #$00");
2980 AddCodeLine ("sty sreg+1");
2982 } else if (val == 16) {
2983 AddCodeLine ("ldy #$00");
2984 AddCodeLine ("ldx sreg+1");
2985 if ((flags & CF_UNSIGNED) == 0) {
2986 unsigned L = GetLocalLabel();
2987 AddCodeLine ("bpl %s", LocalLabelName (L));
2988 AddCodeLine ("dey");
2991 AddCodeLine ("lda sreg");
2992 AddCodeLine ("sty sreg+1");
2993 AddCodeLine ("sty sreg");
3002 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3003 * into the normal, non-optimized stuff.
3005 g_push (flags & ~CF_CONST, 0);
3009 /* Use long way over the stack */
3010 oper (flags, val, ops);
3015 void g_asl (unsigned flags, unsigned long val)
3016 /* Primary = TOS << Primary */
3018 static char* ops [12] = {
3019 0, "tosasla0", "tosaslax",
3020 0, "tosshla0", "tosshlax",
3026 /* If the right hand side is const, the lhs is not on stack but still
3027 * in the primary register.
3029 if (flags & CF_CONST) {
3031 switch (flags & CF_TYPE) {
3036 AddCodeLine ("tax");
3037 AddCodeLine ("lda #$00");
3043 } else if (val >= 1 && val <= 4) {
3044 if (flags & CF_UNSIGNED) {
3045 AddCodeLine ("jsr shlax%ld", val);
3047 AddCodeLine ("jsr aslax%ld", val);
3057 } else if (val >= 1 && val <= 4) {
3058 if (flags & CF_UNSIGNED) {
3059 AddCodeLine ("jsr shleax%ld", val);
3061 AddCodeLine ("jsr asleax%ld", val);
3064 } else if (val == 8) {
3065 AddCodeLine ("ldy sreg");
3066 AddCodeLine ("sty sreg+1");
3067 AddCodeLine ("stx sreg");
3068 AddCodeLine ("tax");
3069 AddCodeLine ("lda #$00");
3071 } else if (val == 16) {
3072 AddCodeLine ("stx sreg+1");
3073 AddCodeLine ("sta sreg");
3074 AddCodeLine ("lda #$00");
3075 AddCodeLine ("tax");
3084 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3085 * into the normal, non-optimized stuff.
3087 g_push (flags & ~CF_CONST, 0);
3091 /* Use long way over the stack */
3092 oper (flags, val, ops);
3097 void g_neg (unsigned flags)
3098 /* Primary = -Primary */
3100 switch (flags & CF_TYPE) {
3104 AddCodeLine ("jsr negax");
3108 AddCodeLine ("jsr negeax");
3118 void g_bneg (unsigned flags)
3119 /* Primary = !Primary */
3121 switch (flags & CF_TYPE) {
3124 AddCodeLine ("jsr bnega");
3128 AddCodeLine ("jsr bnegax");
3132 AddCodeLine ("jsr bnegeax");
3142 void g_com (unsigned flags)
3143 /* Primary = ~Primary */
3145 switch (flags & CF_TYPE) {
3149 AddCodeLine ("jsr complax");
3153 AddCodeLine ("jsr compleax");
3163 void g_inc (unsigned flags, unsigned long val)
3164 /* Increment the primary register by a given number */
3166 /* Don't inc by zero */
3171 /* Generate code for the supported types */
3173 switch (flags & CF_TYPE) {
3176 if (flags & CF_FORCECHAR) {
3177 if (CPU == CPU_65C02 && val <= 2) {
3179 AddCodeLine ("ina");
3182 AddCodeLine ("clc");
3183 AddCodeLine ("adc #$%02X", (unsigned char)val);
3190 if (CPU == CPU_65C02 && val == 1) {
3191 unsigned L = GetLocalLabel();
3192 AddCodeLine ("ina");
3193 AddCodeLine ("bne %s", LocalLabelName (L));
3194 AddCodeLine ("inx");
3196 } else if (CodeSizeFactor < 200) {
3199 AddCodeLine ("jsr incax%lu", val);
3200 } else if (val <= 255) {
3202 AddCodeLine ("jsr incaxy");
3204 g_add (flags | CF_CONST, val);
3207 /* Inline the code */
3209 if ((val & 0xFF) != 0) {
3210 unsigned L = GetLocalLabel();
3211 AddCodeLine ("clc");
3212 AddCodeLine ("adc #$%02X", (unsigned char) val);
3213 AddCodeLine ("bcc %s", LocalLabelName (L));
3214 AddCodeLine ("inx");
3218 AddCodeLine ("inx");
3221 AddCodeLine ("inx");
3224 AddCodeLine ("inx");
3227 AddCodeLine ("clc");
3228 if ((val & 0xFF) != 0) {
3229 AddCodeLine ("adc #$%02X", (unsigned char) val);
3231 AddCodeLine ("pha");
3232 AddCodeLine ("txa");
3233 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3234 AddCodeLine ("tax");
3235 AddCodeLine ("pla");
3243 AddCodeLine ("jsr inceaxy");
3245 g_add (flags | CF_CONST, val);
3257 void g_dec (unsigned flags, unsigned long val)
3258 /* Decrement the primary register by a given number */
3260 /* Don't dec by zero */
3265 /* Generate code for the supported types */
3267 switch (flags & CF_TYPE) {
3270 if (flags & CF_FORCECHAR) {
3271 if (CPU == CPU_65C02 && val <= 2) {
3273 AddCodeLine ("dea");
3276 AddCodeLine ("sec");
3277 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3284 if (CodeSizeFactor < 200) {
3285 /* Use subroutines */
3287 AddCodeLine ("jsr decax%d", (int) val);
3288 } else if (val <= 255) {
3290 AddCodeLine ("jsr decaxy");
3292 g_sub (flags | CF_CONST, val);
3295 /* Inline the code */
3297 if ((val & 0xFF) != 0) {
3298 unsigned L = GetLocalLabel();
3299 AddCodeLine ("sec");
3300 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3301 AddCodeLine ("bcs %s", LocalLabelName (L));
3302 AddCodeLine ("dex");
3306 AddCodeLine ("dex");
3309 AddCodeLine ("dex");
3312 AddCodeLine ("sec");
3313 if ((val & 0xFF) != 0) {
3314 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3316 AddCodeLine ("pha");
3317 AddCodeLine ("txa");
3318 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3319 AddCodeLine ("tax");
3320 AddCodeLine ("pla");
3328 AddCodeLine ("jsr deceaxy");
3330 g_sub (flags | CF_CONST, val);
3343 * Following are the conditional operators. They compare the TOS against
3344 * the primary and put a literal 1 in the primary if the condition is
3345 * true, otherwise they clear the primary register
3350 void g_eq (unsigned flags, unsigned long val)
3351 /* Test for equal */
3353 static char* ops [12] = {
3354 "toseq00", "toseqa0", "toseqax",
3355 "toseq00", "toseqa0", "toseqax",
3362 /* If the right hand side is const, the lhs is not on stack but still
3363 * in the primary register.
3365 if (flags & CF_CONST) {
3367 switch (flags & CF_TYPE) {
3370 if (flags & CF_FORCECHAR) {
3371 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3372 AddCodeLine ("jsr booleq");
3378 L = GetLocalLabel();
3379 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3380 AddCodeLine ("bne %s", LocalLabelName (L));
3381 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3383 AddCodeLine ("jsr booleq");
3393 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3394 * into the normal, non-optimized stuff.
3396 g_push (flags & ~CF_CONST, 0);
3400 /* Use long way over the stack */
3401 oper (flags, val, ops);
3406 void g_ne (unsigned flags, unsigned long val)
3407 /* Test for not equal */
3409 static char* ops [12] = {
3410 "tosne00", "tosnea0", "tosneax",
3411 "tosne00", "tosnea0", "tosneax",
3418 /* If the right hand side is const, the lhs is not on stack but still
3419 * in the primary register.
3421 if (flags & CF_CONST) {
3423 switch (flags & CF_TYPE) {
3426 if (flags & CF_FORCECHAR) {
3427 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3428 AddCodeLine ("jsr boolne");
3434 L = GetLocalLabel();
3435 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3436 AddCodeLine ("bne %s", LocalLabelName (L));
3437 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3439 AddCodeLine ("jsr boolne");
3449 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3450 * into the normal, non-optimized stuff.
3452 g_push (flags & ~CF_CONST, 0);
3456 /* Use long way over the stack */
3457 oper (flags, val, ops);
3462 void g_lt (unsigned flags, unsigned long val)
3463 /* Test for less than */
3465 static char* ops [12] = {
3466 "toslt00", "toslta0", "tosltax",
3467 "tosult00", "tosulta0", "tosultax",
3472 /* If the right hand side is const, the lhs is not on stack but still
3473 * in the primary register.
3475 if (flags & CF_CONST) {
3477 /* Give a warning in some special cases */
3478 if ((flags & CF_UNSIGNED) && val == 0) {
3479 Warning ("Condition is never true");
3480 AddCodeLine ("jsr return0");
3484 /* Look at the type */
3485 switch (flags & CF_TYPE) {
3488 if (flags & CF_FORCECHAR) {
3489 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3490 if (flags & CF_UNSIGNED) {
3491 AddCodeLine ("jsr boolult");
3493 AddCodeLine ("jsr boollt");
3500 if (flags & CF_UNSIGNED) {
3501 /* Unsigned compare */
3502 /* If the low byte is zero, we must only test the high byte */
3503 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3504 if ((val & 0xFF) != 0) {
3505 unsigned L = GetLocalLabel();
3506 AddCodeLine ("bne %s", LocalLabelName (L));
3507 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3510 AddCodeLine ("jsr boolult");
3512 /* Signed compare */
3513 if ((val & 0xFF) == 0) {
3514 /* Low byte is zero, just look at the high byte */
3515 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3517 /* Subtract the two values */
3518 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3519 AddCodeLine ("txa");
3520 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3522 AddCodeLine ("jsr boollt");
3527 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3528 /* If we have a signed compare against zero, we only need to
3529 * test the high byte.
3531 AddCodeLine ("lda sreg+1");
3533 /* Do a subtraction */
3534 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3535 AddCodeLine ("txa");
3536 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3537 AddCodeLine ("lda sreg");
3538 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3539 AddCodeLine ("lda sreg+1");
3540 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3542 /* Emit the proper makebool routine */
3543 if (flags & CF_UNSIGNED) {
3544 /* Unsigned compare */
3545 AddCodeLine ("jsr boolult");
3547 /* Signed compare */
3548 AddCodeLine ("jsr boollt");
3556 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3557 * into the normal, non-optimized stuff.
3559 g_push (flags & ~CF_CONST, 0);
3563 /* Use long way over the stack */
3564 oper (flags, val, ops);
3569 void g_le (unsigned flags, unsigned long val)
3570 /* Test for less than or equal to */
3572 static char* ops [12] = {
3573 "tosle00", "toslea0", "tosleax",
3574 "tosule00", "tosulea0", "tosuleax",
3580 /* If the right hand side is const, the lhs is not on stack but still
3581 * in the primary register.
3583 if (flags & CF_CONST) {
3585 /* Look at the type */
3586 switch (flags & CF_TYPE) {
3589 if (flags & CF_FORCECHAR) {
3590 if (flags & CF_UNSIGNED) {
3591 /* Unsigned compare */
3593 /* Use < instead of <= because the former gives
3594 * better code on the 6502 than the latter.
3596 g_lt (flags, val+1);
3599 Warning ("Condition is always true");
3600 AddCodeLine ("jsr return1");
3603 /* Signed compare */
3604 if ((long) val < 0x7F) {
3605 /* Use < instead of <= because the former gives
3606 * better code on the 6502 than the latter.
3608 g_lt (flags, val+1);
3611 Warning ("Condition is always true");
3612 AddCodeLine ("jsr return1");
3620 if (flags & CF_UNSIGNED) {
3621 /* Unsigned compare */
3623 /* Use < instead of <= because the former gives
3624 * better code on the 6502 than the latter.
3626 g_lt (flags, val+1);
3629 Warning ("Condition is always true");
3630 AddCodeLine ("jsr return1");
3633 /* Signed compare */
3634 if ((long) val < 0x7FFF) {
3635 g_lt (flags, val+1);
3638 Warning ("Condition is always true");
3639 AddCodeLine ("jsr return1");
3645 if (flags & CF_UNSIGNED) {
3646 /* Unsigned compare */
3647 if (val < 0xFFFFFFFF) {
3648 /* Use < instead of <= because the former gives
3649 * better code on the 6502 than the latter.
3651 g_lt (flags, val+1);
3654 Warning ("Condition is always true");
3655 AddCodeLine ("jsr return1");
3658 /* Signed compare */
3659 if ((long) val < 0x7FFFFFFF) {
3660 g_lt (flags, val+1);
3663 Warning ("Condition is always true");
3664 AddCodeLine ("jsr return1");
3673 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3674 * into the normal, non-optimized stuff.
3676 g_push (flags & ~CF_CONST, 0);
3680 /* Use long way over the stack */
3681 oper (flags, val, ops);
3686 void g_gt (unsigned flags, unsigned long val)
3687 /* Test for greater than */
3689 static char* ops [12] = {
3690 "tosgt00", "tosgta0", "tosgtax",
3691 "tosugt00", "tosugta0", "tosugtax",
3697 /* If the right hand side is const, the lhs is not on stack but still
3698 * in the primary register.
3700 if (flags & CF_CONST) {
3702 /* Look at the type */
3703 switch (flags & CF_TYPE) {
3706 if (flags & CF_FORCECHAR) {
3707 if (flags & CF_UNSIGNED) {
3709 /* If we have a compare > 0, we will replace it by
3710 * != 0 here, since both are identical but the
3711 * latter is easier to optimize.
3714 } else if (val < 0xFF) {
3715 /* Use >= instead of > because the former gives
3716 * better code on the 6502 than the latter.
3718 g_ge (flags, val+1);
3721 Warning ("Condition is never true");
3722 AddCodeLine ("jsr return0");
3725 if ((long) val < 0x7F) {
3726 /* Use >= instead of > because the former gives
3727 * better code on the 6502 than the latter.
3729 g_ge (flags, val+1);
3732 Warning ("Condition is never true");
3733 AddCodeLine ("jsr return0");
3741 if (flags & CF_UNSIGNED) {
3742 /* Unsigned compare */
3744 /* If we have a compare > 0, we will replace it by
3745 * != 0 here, since both are identical but the latter
3746 * is easier to optimize.
3749 } else if (val < 0xFFFF) {
3750 /* Use >= instead of > because the former gives better
3751 * code on the 6502 than the latter.
3753 g_ge (flags, val+1);
3756 Warning ("Condition is never true");
3757 AddCodeLine ("jsr return0");
3760 /* Signed compare */
3761 if ((long) val < 0x7FFF) {
3762 g_ge (flags, val+1);
3765 Warning ("Condition is never true");
3766 AddCodeLine ("jsr return0");
3772 if (flags & CF_UNSIGNED) {
3773 /* Unsigned compare */
3775 /* If we have a compare > 0, we will replace it by
3776 * != 0 here, since both are identical but the latter
3777 * is easier to optimize.
3780 } else if (val < 0xFFFFFFFF) {
3781 /* Use >= instead of > because the former gives better
3782 * code on the 6502 than the latter.
3784 g_ge (flags, val+1);
3787 Warning ("Condition is never true");
3788 AddCodeLine ("jsr return0");
3791 /* Signed compare */
3792 if ((long) val < 0x7FFFFFFF) {
3793 g_ge (flags, val+1);
3796 Warning ("Condition is never true");
3797 AddCodeLine ("jsr return0");
3806 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3807 * into the normal, non-optimized stuff.
3809 g_push (flags & ~CF_CONST, 0);
3813 /* Use long way over the stack */
3814 oper (flags, val, ops);
3819 void g_ge (unsigned flags, unsigned long val)
3820 /* Test for greater than or equal to */
3822 static char* ops [12] = {
3823 "tosge00", "tosgea0", "tosgeax",
3824 "tosuge00", "tosugea0", "tosugeax",
3830 /* If the right hand side is const, the lhs is not on stack but still
3831 * in the primary register.
3833 if (flags & CF_CONST) {
3835 /* Give a warning in some special cases */
3836 if ((flags & CF_UNSIGNED) && val == 0) {
3837 Warning ("Condition is always true");
3838 AddCodeLine ("jsr return1");
3842 /* Look at the type */
3843 switch (flags & CF_TYPE) {
3846 if (flags & CF_FORCECHAR) {
3847 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3848 if (flags & CF_UNSIGNED) {
3849 AddCodeLine ("jsr booluge");
3851 AddCodeLine ("jsr boolge");
3858 if (flags & CF_UNSIGNED) {
3859 /* Unsigned compare */
3860 /* If the low byte is zero, we must only test the high byte */
3861 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3862 if ((val & 0xFF) != 0) {
3863 unsigned L = GetLocalLabel();
3864 AddCodeLine ("bne %s", LocalLabelName (L));
3865 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3868 AddCodeLine ("jsr booluge");
3870 /* Signed compare */
3871 if ((val & 0xFF) == 0) {
3872 /* Low byte is zero, just look at the high byte */
3873 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3875 /* Subtract the two values */
3876 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3877 AddCodeLine ("txa");
3878 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3880 AddCodeLine ("jsr boolge");
3885 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3886 /* If we have a signed compare against zero, we only need to
3887 * test the high byte.
3889 AddCodeLine ("lda sreg+1");
3891 /* Do a subtraction */
3892 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3893 AddCodeLine ("txa");
3894 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3895 AddCodeLine ("lda sreg");
3896 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3897 AddCodeLine ("lda sreg+1");
3898 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3900 /* Emit the proper makebool routine */
3901 if (flags & CF_UNSIGNED) {
3902 /* Unsigned compare */
3903 AddCodeLine ("jsr booluge");
3905 /* Signed compare */
3906 AddCodeLine ("jsr boolge");
3914 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3915 * into the normal, non-optimized stuff.
3917 g_push (flags & ~CF_CONST, 0);
3921 /* Use long way over the stack */
3922 oper (flags, val, ops);
3927 /*****************************************************************************/
3928 /* Allocating static storage */
3929 /*****************************************************************************/
3933 void g_res (unsigned n)
3934 /* Reserve static storage, n bytes */
3936 AddDataLine ("\t.res\t%u,$00", n);
3941 void g_defdata (unsigned flags, unsigned long val, long offs)
3942 /* Define data with the size given in flags */
3944 if (flags & CF_CONST) {
3946 /* Numeric constant */
3947 switch (flags & CF_TYPE) {
3950 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
3954 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
3958 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3969 /* Create the correct label name */
3970 const char* Label = GetLabelName (flags, val, offs);
3972 /* Labels are always 16 bit */
3973 AddDataLine ("\t.addr\t%s", Label);
3980 void g_defbytes (const void* Bytes, unsigned Count)
3981 /* Output a row of bytes as a constant */
3987 /* Cast the buffer pointer */
3988 const unsigned char* Data = (const unsigned char*) Bytes;
3990 /* Output the stuff */
3993 /* How many go into this line? */
3994 if ((Chunk = Count) > 16) {
3999 /* Output one line */
4000 strcpy (Buf, "\t.byte\t");
4003 B += sprintf (B, "$%02X", *Data++);
4009 /* Output the line */
4016 void g_zerobytes (unsigned Count)
4017 /* Output Count bytes of data initialized with zero */
4020 AddDataLine ("\t.res\t%u,$00", Count);
4026 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4027 /* Initialize a register variable from static initialization data */
4029 /* Register variables do always have less than 128 bytes */
4030 unsigned CodeLabel = GetLocalLabel ();
4032 g_defcodelabel (CodeLabel);
4033 AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4034 AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4035 AddCodeLine ("dex");
4036 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4041 void g_initauto (unsigned Label, unsigned Size)
4042 /* Initialize a local variable at stack offset zero from static data */
4044 unsigned CodeLabel = GetLocalLabel ();
4046 CheckLocalOffs (Size);
4049 g_defcodelabel (CodeLabel);
4050 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4051 AddCodeLine ("sta (sp),y");
4052 AddCodeLine ("dey");
4053 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4054 } else if (Size <= 256) {
4056 g_defcodelabel (CodeLabel);
4057 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4058 AddCodeLine ("sta (sp),y");
4059 AddCodeLine ("iny");
4060 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4061 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4067 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4068 /* Initialize a static local variable from static initialization data */
4071 unsigned CodeLabel = GetLocalLabel ();
4073 g_defcodelabel (CodeLabel);
4074 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4075 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4076 AddCodeLine ("dey");
4077 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4078 } else if (Size <= 256) {
4079 unsigned CodeLabel = GetLocalLabel ();
4081 g_defcodelabel (CodeLabel);
4082 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4083 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4084 AddCodeLine ("iny");
4085 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4086 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4088 /* Use the easy way here: memcpy */
4089 g_getimmed (CF_STATIC, VarLabel, 0);
4090 AddCodeLine ("jsr pushax");
4091 g_getimmed (CF_STATIC, InitLabel, 0);
4092 AddCodeLine ("jsr pushax");
4093 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4094 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4100 /*****************************************************************************/
4101 /* Switch statement */
4102 /*****************************************************************************/
4106 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4107 /* Generate code for a switch statement */
4109 unsigned NextLabel = 0;
4112 /* Setup registers and determine which compare insn to use */
4113 const char* Compare;
4116 Compare = "cmp #$%02X";
4119 Compare = "cpx #$%02X";
4122 AddCodeLine ("ldy sreg");
4123 Compare = "cpy #$%02X";
4126 AddCodeLine ("ldy sreg+1");
4127 Compare = "cpy #$%02X";
4130 Internal ("Invalid depth in g_switch: %u", Depth);
4133 /* Walk over all nodes */
4134 for (I = 0; I < CollCount (Nodes); ++I) {
4136 /* Get the next case node */
4137 CaseNode* N = CollAtUnchecked (Nodes, I);
4139 /* If we have a next label, define it */
4141 g_defcodelabel (NextLabel);
4145 /* Do the compare */
4146 AddCodeLine (Compare, CN_GetValue (N));
4148 /* If this is the last level, jump directly to the case code if found */
4151 /* Branch if equal */
4152 g_falsejump (0, CN_GetLabel (N));
4156 /* Determine the next label */
4157 if (I == CollCount (Nodes) - 1) {
4158 /* Last node means not found */
4159 g_truejump (0, DefaultLabel);
4161 /* Jump to the next check */
4162 NextLabel = GetLocalLabel ();
4163 g_truejump (0, NextLabel);
4166 /* Check the next level */
4167 g_switch (N->Nodes, DefaultLabel, Depth-1);
4172 /* If we go here, we haven't found the label */
4173 g_jump (DefaultLabel);
4178 /*****************************************************************************/
4179 /* User supplied assembler code */
4180 /*****************************************************************************/
4184 void g_asmcode (struct StrBuf* B)
4185 /* Output one line of assembler code. */
4187 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));
4192 /*****************************************************************************/
4193 /* Inlined known functions */
4194 /*****************************************************************************/
4198 void g_strlen (unsigned flags, unsigned long val, long offs)
4199 /* Inline the strlen() function */
4201 /* We need a label in both cases */
4202 unsigned label = GetLocalLabel ();
4204 /* Two different encodings */
4205 if (flags & CF_CONST) {
4207 /* The address of the string is constant. Create the correct label name */
4208 const char* lbuf = GetLabelName (flags, val, offs);
4210 /* Generate the strlen code */
4211 AddCodeLine ("ldy #$FF");
4212 g_defcodelabel (label);
4213 AddCodeLine ("iny");
4214 AddCodeLine ("lda %s,y", lbuf);
4215 AddCodeLine ("bne %s", LocalLabelName (label));
4216 AddCodeLine ("tax");
4217 AddCodeLine ("tya");
4221 /* Address not constant but in primary */
4222 if (CodeSizeFactor < 400) {
4223 /* This is too much code, so call strlen instead of inlining */
4224 AddCodeLine ("jsr _strlen");
4226 /* Inline the function */
4227 AddCodeLine ("sta ptr1");
4228 AddCodeLine ("stx ptr1+1");
4229 AddCodeLine ("ldy #$FF");
4230 g_defcodelabel (label);
4231 AddCodeLine ("iny");
4232 AddCodeLine ("lda (ptr1),y");
4233 AddCodeLine ("bne %s", LocalLabelName (label));
4234 AddCodeLine ("tax");
4235 AddCodeLine ("tya");