1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
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 /*****************************************************************************/
60 /*****************************************************************************/
62 /*****************************************************************************/
66 /* Compiler relative stack pointer */
71 /*****************************************************************************/
73 /*****************************************************************************/
77 static void typeerror (unsigned type)
78 /* Print an error message about an invalid operand type */
80 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
85 static void CheckLocalOffs (unsigned Offs)
86 /* Check the offset into the stack for 8bit range */
89 /* Too many local vars */
90 Error ("Too many local variables");
96 static char* GetLabelName (unsigned flags, unsigned long label, unsigned offs)
98 static char lbuf [128]; /* Label name */
100 /* Create the correct label name */
101 switch (flags & CF_ADDRMASK) {
104 /* Static memory cell */
105 sprintf (lbuf, "%s+%u", LocalLabelName (label), offs);
110 sprintf (lbuf, "_%s+%u", (char*) label, offs);
114 /* Absolute address */
115 sprintf (lbuf, "$%04X", (unsigned)((label+offs) & 0xFFFF));
119 /* Variable in register bank */
120 sprintf (lbuf, "regbank+%u", (unsigned)((label+offs) & 0xFFFF));
124 Internal ("Invalid address flags");
127 /* Return a pointer to the static buffer */
133 /*****************************************************************************/
134 /* Pre- and postamble */
135 /*****************************************************************************/
139 void g_preamble (void)
140 /* Generate the assembler code preamble */
142 /* Create a new segment list */
145 /* Identify the compiler version */
146 AddDataLine ("; File generated by cc65 v %u.%u.%u",
147 VER_MAJOR, VER_MINOR, VER_PATCH);
149 /* Insert some object file options */
150 AddDataLine (".fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
151 VER_MAJOR, VER_MINOR, VER_PATCH);
153 /* If we're producing code for some other CPU, switch the command set */
154 if (CPU == CPU_65C02) {
155 AddDataLine (".pc02");
158 /* Allow auto import for runtime library routines */
159 AddDataLine (".autoimport\ton");
161 /* Switch the assembler into case sensitive mode */
162 AddDataLine (".case\t\ton");
164 /* Tell the assembler if we want to generate debug info */
165 AddDataLine (".debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
167 /* Import the stack pointer for direct auto variable access */
168 AddDataLine (".importzp\tsp, sreg, regsave, regbank, tmp1, ptr1");
170 /* Define long branch macros */
171 AddDataLine (".macpack\tlongbranch");
176 /*****************************************************************************/
177 /* Segment support */
178 /*****************************************************************************/
182 void g_userodata (void)
183 /* Switch to the read only data segment */
185 UseDataSeg (SEG_RODATA);
190 void g_usedata (void)
191 /* Switch to the data segment */
193 UseDataSeg (SEG_DATA);
199 /* Switch to the bss segment */
201 UseDataSeg (SEG_BSS);
206 /*****************************************************************************/
208 /*****************************************************************************/
212 unsigned sizeofarg (unsigned flags)
213 /* Return the size of a function argument type that is encoded in flags */
215 switch (flags & CF_TYPE) {
218 return (flags & CF_FORCECHAR)? 1 : 2;
235 int pop (unsigned flags)
236 /* Pop an argument of the given size */
238 return oursp += sizeofarg (flags);
243 int push (unsigned flags)
244 /* Push an argument of the given size */
246 return oursp -= sizeofarg (flags);
251 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
252 /* The value in Offs is an offset to an address in a/x. Make sure, an object
253 * of the type given in Flags can be loaded or stored into this address by
254 * adding part of the offset to the address in ax, so that the remaining
255 * offset fits into an index register. Return the remaining offset.
258 /* If the offset is too large for a byte register, add the high byte
259 * of the offset to the primary. Beware: We need a special correction
260 * if the offset in the low byte will overflow in the operation.
262 unsigned O = Offs & ~0xFFU;
263 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
264 /* We need to add the low byte also */
268 /* Do the correction if we need one */
270 g_inc (CF_INT | CF_CONST, O);
274 /* Return the new offset */
280 /*****************************************************************************/
281 /* Functions handling local labels */
282 /*****************************************************************************/
286 void g_defcodelabel (unsigned label)
287 /* Define a local code label */
289 AddCodeLabel (CS->Code, LocalLabelName (label));
294 void g_defdatalabel (unsigned label)
295 /* Define a local data label */
297 AddDataLine ("%s:", LocalLabelName (label));
302 /*****************************************************************************/
303 /* Functions handling global labels */
304 /*****************************************************************************/
308 void g_defgloblabel (const char* Name)
309 /* Define a global label with the given name */
311 /* Global labels are always data labels */
312 AddDataLine ("_%s:", Name);
317 void g_defexport (const char* Name, int ZP)
318 /* Export the given label */
321 AddDataLine ("\t.exportzp\t_%s", Name);
323 AddDataLine ("\t.export\t\t_%s", Name);
329 void g_defimport (const char* Name, int ZP)
330 /* Import the given label */
333 AddDataLine ("\t.importzp\t_%s", Name);
335 AddDataLine ("\t.import\t\t_%s", Name);
341 /*****************************************************************************/
342 /* Load functions for various registers */
343 /*****************************************************************************/
347 static void ldaconst (unsigned val)
348 /* Load a with a constant */
350 AddCodeLine ("lda #$%02X", val & 0xFF);
355 static void ldxconst (unsigned val)
356 /* Load x with a constant */
358 AddCodeLine ("ldx #$%02X", val & 0xFF);
363 static void ldyconst (unsigned val)
364 /* Load y with a constant */
366 AddCodeLine ("ldy #$%02X", val & 0xFF);
371 /*****************************************************************************/
372 /* Function entry and exit */
373 /*****************************************************************************/
377 /* Remember the argument size of a function. The variable is set by g_enter
378 * and used by g_leave. If the functions gets its argument size by the caller
379 * (variable param list or function without prototype), g_enter will set the
385 void g_enter (unsigned flags, unsigned argsize)
386 /* Function prologue */
388 if ((flags & CF_FIXARGC) != 0) {
389 /* Just remember the argument size for the leave */
393 AddCodeLine ("jsr enter");
399 void g_leave (int flags, int val)
400 /* Function epilogue */
405 /* CF_REG is set if we're returning a value from the function */
406 if ((flags & CF_REG) == 0) {
411 /* How many bytes of locals do we have to drop? */
414 /* If we didn't have a variable argument list, don't call leave */
417 /* Load a function return code if needed */
418 if ((flags & CF_CONST) != 0) {
419 g_getimmed (flags, val, 0);
422 /* Drop stackframe or leave with rts */
425 AddCodeHint ("y:-"); /* Y register no longer used */
428 AddCodeHint ("y:-"); /* Y register no longer used */
429 AddCodeLine ("jmp incsp%d", k);
433 AddCodeLine ("jmp addysp");
438 strcpy (buf, "\tjmp\tleave");
440 /* We've a stack frame to drop */
444 /* Y register no longer used */
447 if (flags & CF_CONST) {
448 if ((flags & CF_TYPE) != CF_LONG) {
449 /* Constant int sized value given for return code */
451 /* Special case: return 0 */
453 } else if (((val >> 8) & 0xFF) == 0) {
454 /* Special case: constant with high byte zero */
455 ldaconst (val); /* Load low byte */
458 /* Others: arbitrary constant value */
459 g_getimmed (flags, val, 0); /* Load value */
462 /* Constant long value: No shortcut possible */
463 g_getimmed (flags, val, 0);
467 /* Output the jump */
474 /*****************************************************************************/
475 /* Register variables */
476 /*****************************************************************************/
480 void g_save_regvars (int RegOffs, unsigned Bytes)
481 /* Save register variables */
483 /* Don't loop for up to two bytes */
486 AddCodeLine ("lda regbank%+d", RegOffs);
487 AddCodeLine ("jsr pusha");
489 } else if (Bytes == 2) {
491 AddCodeLine ("lda regbank%+d", RegOffs);
492 AddCodeLine ("ldx regbank%+d", RegOffs+1);
493 AddCodeLine ("jsr pushax");
497 /* More than two bytes - loop */
498 unsigned Label = GetLocalLabel ();
500 ldyconst (Bytes - 1);
502 g_defcodelabel (Label);
503 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
504 AddCodeLine ("sta (sp),y");
507 AddCodeLine ("bne %s", LocalLabelName (Label));
511 /* We pushed stuff, correct the stack pointer */
517 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
518 /* Restore register variables */
520 /* Calculate the actual stack offset and check it */
522 CheckLocalOffs (StackOffs);
524 /* Don't loop for up to two bytes */
527 ldyconst (StackOffs);
528 AddCodeLine ("lda (sp),y");
529 AddCodeLine ("sta regbank%+d", RegOffs);
531 } else if (Bytes == 2) {
533 ldyconst (StackOffs);
534 AddCodeLine ("lda (sp),y");
535 AddCodeLine ("sta regbank%+d", RegOffs);
537 AddCodeLine ("lda (sp),y");
538 AddCodeLine ("sta regbank%+d", RegOffs+1);
542 /* More than two bytes - loop */
543 unsigned Label = GetLocalLabel ();
544 ldyconst (StackOffs+Bytes-1);
546 g_defcodelabel (Label);
547 AddCodeLine ("lda (sp),y");
548 AddCodeLine ("sta regbank%+d,x", RegOffs-1);
551 AddCodeLine ("bne %s", LocalLabelName (Label));
558 /*****************************************************************************/
559 /* Fetching memory cells */
560 /*****************************************************************************/
564 void g_getimmed (unsigned flags, unsigned long val, unsigned offs)
565 /* Load a constant into the primary register */
567 if ((flags & CF_CONST) != 0) {
569 /* Numeric constant */
570 switch (flags & CF_TYPE) {
573 if ((flags & CF_FORCECHAR) != 0) {
579 ldxconst ((val >> 8) & 0xFF);
580 ldaconst (val & 0xFF);
585 AddCodeLine ("ldx #$00");
586 AddCodeLine ("stx sreg+1");
587 AddCodeLine ("stx sreg");
588 AddCodeLine ("lda #$%02X", (unsigned char) val);
589 } else if ((val & 0xFFFF00FF) == 0) {
590 AddCodeLine ("lda #$00");
591 AddCodeLine ("sta sreg+1");
592 AddCodeLine ("sta sreg");
593 AddCodeLine ("ldx #$%02X", (unsigned char) (val >> 8));
594 } else if ((val & 0xFFFF0000) == 0 && CodeSizeFactor > 140) {
595 AddCodeLine ("lda #$00");
596 AddCodeLine ("sta sreg+1");
597 AddCodeLine ("sta sreg");
598 AddCodeLine ("lda #$%02X", (unsigned char) val);
599 AddCodeLine ("ldx #$%02X", (unsigned char) (val >> 8));
600 } else if ((val & 0xFFFFFF00) == 0xFFFFFF00) {
601 AddCodeLine ("ldx #$FF");
602 AddCodeLine ("stx sreg+1");
603 AddCodeLine ("stx sreg");
604 if ((val & 0xFF) == 0xFF) {
607 AddCodeLine ("lda #$%02X", (unsigned char) val);
609 } else if ((val & 0xFFFF00FF) == 0xFFFF00FF) {
610 AddCodeLine ("lda #$FF");
611 AddCodeLine ("sta sreg+1");
612 AddCodeLine ("sta sreg");
613 AddCodeLine ("ldx #$%02X", (unsigned char) (val >> 8));
615 /* Call a subroutine that will load following value */
616 AddCodeLine ("jsr ldeax");
617 AddCodeLine (".dword $%08lX", val & 0xFFFFFFFF);
629 /* Some sort of label */
630 const char* Label = GetLabelName (flags, val, offs);
632 /* Load the address into the primary */
633 AddCodeLine ("lda #<(%s)", Label);
634 AddCodeLine ("ldx #>(%s)", Label);
641 void g_getstatic (unsigned flags, unsigned long label, unsigned offs)
642 /* Fetch an static memory cell into the primary register */
644 /* Create the correct label name */
645 char* lbuf = GetLabelName (flags, label, offs);
647 /* Check the size and generate the correct load operation */
648 switch (flags & CF_TYPE) {
651 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
652 AddCodeLine ("lda %s", lbuf); /* load A from the label */
655 AddCodeLine ("lda %s", lbuf); /* load A from the label */
656 if (!(flags & CF_UNSIGNED)) {
657 /* Must sign extend */
658 AddCodeLine ("bpl *+3");
660 AddCodeHint ("x:!"); /* X is invalid now */
666 AddCodeLine ("lda %s", lbuf);
667 if (flags & CF_TEST) {
668 AddCodeLine ("ora %s+1", lbuf);
670 AddCodeLine ("ldx %s+1", lbuf);
675 if (flags & CF_TEST) {
676 AddCodeLine ("lda %s+3", lbuf);
677 AddCodeLine ("ora %s+2", lbuf);
678 AddCodeLine ("ora %s+1", lbuf);
679 AddCodeLine ("ora %s+0", lbuf);
681 AddCodeLine ("lda %s+3", lbuf);
682 AddCodeLine ("sta sreg+1");
683 AddCodeLine ("lda %s+2", lbuf);
684 AddCodeLine ("sta sreg");
685 AddCodeLine ("ldx %s+1", lbuf);
686 AddCodeLine ("lda %s", lbuf);
698 void g_getlocal (unsigned flags, int offs)
699 /* Fetch specified local object (local var). */
702 CheckLocalOffs (offs);
703 switch (flags & CF_TYPE) {
706 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
707 if (CPU == CPU_65C02 && offs == 0) {
708 AddCodeLine ("lda (sp)");
711 AddCodeLine ("lda (sp),y");
715 AddCodeLine ("ldx #$00");
716 AddCodeLine ("lda (sp,x)");
719 AddCodeLine ("ldx #$00");
720 AddCodeLine ("lda (sp),y");
722 if ((flags & CF_UNSIGNED) == 0) {
723 AddCodeLine ("bpl *+3");
725 AddCodeHint ("x:!"); /* X is invalid now */
731 CheckLocalOffs (offs + 1);
732 if (flags & CF_TEST) {
734 AddCodeLine ("lda (sp),y");
736 AddCodeLine ("ora (sp),y");
738 if (CodeSizeFactor > 180) {
740 AddCodeLine ("lda (sp),y");
743 AddCodeLine ("lda (sp),y");
747 AddCodeLine ("jsr ldaxysp");
749 AddCodeLine ("jsr ldax0sp");
758 AddCodeLine ("jsr ldeaxysp");
760 AddCodeLine ("jsr ldeax0sp");
771 void g_getind (unsigned flags, unsigned offs)
772 /* Fetch the specified object type indirect through the primary register
773 * into the primary register
776 /* If the offset is greater than 255, add the part that is > 255 to
777 * the primary. This way we get an easy addition and use the low byte
780 offs = MakeByteOffs (flags, offs);
782 /* Handle the indirect fetch */
783 switch (flags & CF_TYPE) {
786 /* Character sized */
789 if (flags & CF_UNSIGNED) {
790 AddCodeLine ("jsr ldauidx");
792 AddCodeLine ("jsr ldaidx");
795 if (flags & CF_UNSIGNED) {
796 if (CodeSizeFactor > 250) {
797 AddCodeLine ("sta ptr1");
798 AddCodeLine ("stx ptr1+1");
799 AddCodeLine ("ldx #$00");
800 AddCodeLine ("lda (ptr1,x)");
802 AddCodeLine ("jsr ldaui");
805 AddCodeLine ("jsr ldai");
811 if (flags & CF_TEST) {
813 AddCodeLine ("sta ptr1");
814 AddCodeLine ("stx ptr1+1");
815 AddCodeLine ("lda (ptr1),y");
817 AddCodeLine ("ora (ptr1),y");
820 AddCodeLine ("jsr ldaxi");
823 AddCodeLine ("jsr ldaxidx");
830 AddCodeLine ("jsr ldeaxi");
833 AddCodeLine ("jsr ldeaxidx");
835 if (flags & CF_TEST) {
836 AddCodeLine ("jsr tsteax");
848 void g_leasp (int offs)
849 /* Fetch the address of the specified symbol into the primary register */
851 /* Calculate the offset relative to sp */
854 /* For value 0 we do direct code */
856 AddCodeLine ("lda sp");
857 AddCodeLine ("ldx sp+1");
859 if (CodeSizeFactor < 300) {
860 ldaconst (offs); /* Load A with offset value */
861 AddCodeLine ("jsr leaasp"); /* Load effective address */
863 if (CPU == CPU_65C02 && offs == 1) {
864 AddCodeLine ("lda sp");
865 AddCodeLine ("ldx sp+1");
867 AddCodeLine ("bne *+3");
869 AddCodeHint ("x:!"); /* Invalidate X */
873 AddCodeLine ("ldx sp+1");
874 AddCodeLine ("adc sp");
875 AddCodeLine ("bcc *+3");
877 AddCodeHint ("x:!"); /* Invalidate X */
885 void g_leavariadic (int Offs)
886 /* Fetch the address of a parameter in a variadic function into the primary
890 unsigned ArgSizeOffs;
892 /* Calculate the offset relative to sp */
895 /* Get the offset of the parameter which is stored at sp+0 on function
896 * entry and check if this offset is reachable with a byte offset.
899 ArgSizeOffs = -oursp;
900 CheckLocalOffs (ArgSizeOffs);
902 /* Get the size of all parameters. */
903 if (ArgSizeOffs == 0 && CPU == CPU_65C02) {
904 AddCodeLine ("lda (sp)");
906 ldyconst (ArgSizeOffs);
907 AddCodeLine ("lda (sp),y");
910 /* Add the value of the stackpointer */
911 if (CodeSizeFactor > 250) {
912 AddCodeLine ("ldx sp+1");
914 AddCodeLine ("adc sp");
915 AddCodeLine ("bcc *+3");
917 AddCodeHint ("x:!"); /* Invalidate X */
919 AddCodeLine ("jsr leaasp");
922 /* Add the offset to the primary */
924 g_inc (CF_INT | CF_CONST, Offs);
925 } else if (Offs < 0) {
926 g_dec (CF_INT | CF_CONST, -Offs);
932 /*****************************************************************************/
933 /* Store into memory */
934 /*****************************************************************************/
938 void g_putstatic (unsigned flags, unsigned long label, unsigned offs)
939 /* Store the primary register into the specified static memory cell */
941 /* Create the correct label name */
942 char* lbuf = GetLabelName (flags, label, offs);
944 /* Check the size and generate the correct store operation */
945 switch (flags & CF_TYPE) {
948 AddCodeLine ("sta %s", lbuf);
952 AddCodeLine ("sta %s", lbuf);
953 AddCodeLine ("stx %s+1", lbuf);
957 AddCodeLine ("sta %s", lbuf);
958 AddCodeLine ("stx %s+1", lbuf);
959 AddCodeLine ("ldy sreg");
960 AddCodeLine ("sty %s+2", lbuf);
961 AddCodeLine ("ldy sreg+1");
962 AddCodeLine ("sty %s+3", lbuf);
973 void g_putlocal (unsigned Flags, int Offs, long Val)
974 /* Put data into local object. */
977 CheckLocalOffs (Offs);
978 switch (Flags & CF_TYPE) {
981 if (Flags & CF_CONST) {
982 AddCodeLine ("lda #$%02X", (unsigned char) Val);
984 if (CPU == CPU_65C02 && Offs == 0) {
985 AddCodeLine ("sta (sp)");
988 AddCodeLine ("sta (sp),y");
993 if (Flags & CF_CONST) {
995 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
996 AddCodeLine ("sta (sp),y");
997 if ((Flags & CF_NOKEEP) == 0) {
998 /* Place high byte into X */
1001 if (CPU == CPU_65C02 && Offs == 0) {
1002 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1003 AddCodeLine ("sta (sp)");
1005 if ((Val & 0xFF) == Offs+1) {
1006 /* The value we need is already in Y */
1007 AddCodeLine ("tya");
1008 AddCodeLine ("dey");
1010 AddCodeLine ("dey");
1011 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1013 AddCodeLine ("sta (sp),y");
1016 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
1019 AddCodeLine ("jsr staxysp");
1021 AddCodeLine ("jsr stax0sp");
1024 if (CPU == CPU_65C02 && Offs == 0) {
1025 AddCodeLine ("sta (sp)");
1027 AddCodeLine ("txa");
1028 AddCodeLine ("sta (sp),y");
1031 AddCodeLine ("sta (sp),y");
1032 AddCodeLine ("iny");
1033 AddCodeLine ("txa");
1034 AddCodeLine ("sta (sp),y");
1041 if (Flags & CF_CONST) {
1042 g_getimmed (Flags, Val, 0);
1046 AddCodeLine ("jsr steaxysp");
1048 AddCodeLine ("jsr steax0sp");
1060 void g_putind (unsigned Flags, unsigned Offs)
1061 /* Store the specified object type in the primary register at the address
1062 * on the top of the stack
1065 /* We can handle offsets below $100 directly, larger offsets must be added
1066 * to the address. Since a/x is in use, best code is achieved by adding
1067 * just the high byte. Be sure to check if the low byte will overflow while
1070 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1072 /* Overflow - we need to add the low byte also */
1073 AddCodeLine ("ldy #$00");
1074 AddCodeLine ("clc");
1075 AddCodeLine ("pha");
1076 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1077 AddCodeLine ("adc (sp),y");
1078 AddCodeLine ("sta (sp),y");
1079 AddCodeLine ("iny");
1080 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1081 AddCodeLine ("adc (sp),y");
1082 AddCodeLine ("sta (sp),y");
1083 AddCodeLine ("pla");
1085 /* Complete address is on stack, new offset is zero */
1088 } else if ((Offs & 0xFF00) != 0) {
1090 /* We can just add the high byte */
1091 AddCodeLine ("ldy #$01");
1092 AddCodeLine ("clc");
1093 AddCodeLine ("pha");
1094 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1095 AddCodeLine ("adc (sp),y");
1096 AddCodeLine ("sta (sp),y");
1097 AddCodeLine ("pla");
1099 /* Offset is now just the low byte */
1103 /* Check the size and determine operation */
1104 switch (Flags & CF_TYPE) {
1109 AddCodeLine ("jsr staspidx");
1111 AddCodeLine ("jsr staspp");
1118 AddCodeLine ("jsr staxspidx");
1120 AddCodeLine ("jsr staxspp");
1127 AddCodeLine ("jsr steaxspidx");
1129 AddCodeLine ("jsr steaxspp");
1138 /* Pop the argument which is always a pointer */
1144 /*****************************************************************************/
1145 /* type conversion and similiar stuff */
1146 /*****************************************************************************/
1150 void g_toslong (unsigned flags)
1151 /* Make sure, the value on TOS is a long. Convert if necessary */
1153 switch (flags & CF_TYPE) {
1157 if (flags & CF_UNSIGNED) {
1158 AddCodeLine ("jsr tosulong");
1160 AddCodeLine ("jsr toslong");
1175 void g_tosint (unsigned flags)
1176 /* Make sure, the value on TOS is an int. Convert if necessary */
1178 switch (flags & CF_TYPE) {
1185 AddCodeLine ("jsr tosint");
1196 void g_reglong (unsigned flags)
1197 /* Make sure, the value in the primary register a long. Convert if necessary */
1199 switch (flags & CF_TYPE) {
1203 if (flags & CF_UNSIGNED) {
1204 if (CodeSizeFactor >= 200) {
1206 AddCodeLine ("sty sreg");
1207 AddCodeLine ("sty sreg+1");
1209 AddCodeLine ("jsr axulong");
1212 AddCodeLine ("jsr axlong");
1226 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1227 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1228 * value, that corresponds to the value on TOS, rhs corresponds to the value
1229 * in (e)ax. The return value is the the flags value for the resulting type.
1232 unsigned ltype, rtype;
1235 /* Get the type spec from the flags */
1236 ltype = lhs & CF_TYPE;
1237 rtype = rhs & CF_TYPE;
1239 /* Check if a conversion is needed */
1240 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1241 /* We must promote the primary register to long */
1243 /* Get the new rhs type */
1244 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1246 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1247 /* We must promote the lhs to long */
1253 /* Get the new rhs type */
1254 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1258 /* Determine the result type for the operation:
1259 * - The result is const if both operands are const.
1260 * - The result is unsigned if one of the operands is unsigned.
1261 * - The result is long if one of the operands is long.
1262 * - Otherwise the result is int sized.
1264 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1265 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1266 if (rtype == CF_LONG || ltype == CF_LONG) {
1276 unsigned g_typecast (unsigned lhs, unsigned rhs)
1277 /* Cast the value in the primary register to the operand size that is flagged
1278 * by the lhs value. Return the result value.
1281 unsigned ltype, rtype;
1283 /* Get the type spec from the flags */
1284 ltype = lhs & CF_TYPE;
1285 rtype = rhs & CF_TYPE;
1287 /* Check if a conversion is needed */
1288 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1289 /* We must promote the primary register to long */
1293 /* Do not need any other action. If the left type is int, and the primary
1294 * register is long, it will be automagically truncated. If the right hand
1295 * side is const, it is not located in the primary register and handled by
1296 * the expression parser code.
1299 /* Result is const if the right hand side was const */
1300 lhs |= (rhs & CF_CONST);
1302 /* The resulting type is that of the left hand side (that's why you called
1310 void g_scale (unsigned flags, long val)
1311 /* Scale the value in the primary register by the given value. If val is positive,
1312 * scale up, is val is negative, scale down. This function is used to scale
1313 * the operands or results of pointer arithmetic by the size of the type, the
1314 * pointer points to.
1319 /* Value may not be zero */
1321 Internal ("Data type has no size");
1322 } else if (val > 0) {
1325 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1327 /* Factor is 2, 4 or 8, use special function */
1328 switch (flags & CF_TYPE) {
1331 if (flags & CF_FORCECHAR) {
1333 AddCodeLine ("asl a");
1340 if (CodeSizeFactor >= (p2+1)*130U) {
1341 AddCodeLine ("stx tmp1");
1343 AddCodeLine ("asl a");
1344 AddCodeLine ("rol tmp1");
1346 AddCodeLine ("ldx tmp1");
1348 if (flags & CF_UNSIGNED) {
1349 AddCodeLine ("jsr shlax%d", p2);
1351 AddCodeLine ("jsr aslax%d", p2);
1357 if (flags & CF_UNSIGNED) {
1358 AddCodeLine ("jsr shleax%d", p2);
1360 AddCodeLine ("jsr asleax%d", p2);
1369 } else if (val != 1) {
1371 /* Use a multiplication instead */
1372 g_mul (flags | CF_CONST, val);
1380 if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1382 /* Factor is 2, 4 or 8, use special function */
1383 switch (flags & CF_TYPE) {
1386 if (flags & CF_FORCECHAR) {
1387 if (flags & CF_UNSIGNED) {
1389 AddCodeLine ("lsr a");
1392 } else if (p2 <= 2) {
1393 AddCodeLine ("cmp #$80");
1394 AddCodeLine ("ror a");
1401 if (flags & CF_UNSIGNED) {
1402 if (CodeSizeFactor >= (p2+1)*130U) {
1403 AddCodeLine ("stx tmp1");
1405 AddCodeLine ("lsr tmp1");
1406 AddCodeLine ("ror a");
1408 AddCodeLine ("ldx tmp1");
1410 AddCodeLine ("jsr lsrax%d", p2);
1413 if (CodeSizeFactor >= (p2+1)*150U) {
1414 AddCodeLine ("stx tmp1");
1416 AddCodeLine ("cpx #$80");
1417 AddCodeLine ("ror tmp1");
1418 AddCodeLine ("ror a");
1420 AddCodeLine ("ldx tmp1");
1422 AddCodeLine ("jsr asrax%d", p2);
1428 if (flags & CF_UNSIGNED) {
1429 AddCodeLine ("jsr lsreax%d", p2);
1431 AddCodeLine ("jsr asreax%d", p2);
1440 } else if (val != 1) {
1442 /* Use a division instead */
1443 g_div (flags | CF_CONST, val);
1451 /*****************************************************************************/
1452 /* Adds and subs of variables fix a fixed address */
1453 /*****************************************************************************/
1457 void g_addlocal (unsigned flags, int offs)
1458 /* Add a local variable to ax */
1460 /* Correct the offset and check it */
1462 CheckLocalOffs (offs);
1464 switch (flags & CF_TYPE) {
1467 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1468 AddCodeLine ("clc");
1469 AddCodeLine ("adc (sp),y");
1470 AddCodeLine ("bcc *+3");
1471 AddCodeLine ("inx");
1472 AddCodeHint ("x:!");
1476 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1477 AddCodeLine ("clc");
1478 AddCodeLine ("adc (sp),y");
1479 AddCodeLine ("pha");
1480 AddCodeLine ("txa");
1481 AddCodeLine ("iny");
1482 AddCodeLine ("adc (sp),y");
1483 AddCodeLine ("tax");
1484 AddCodeLine ("pla");
1488 /* Do it the old way */
1490 g_getlocal (flags, offs);
1502 void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
1503 /* Add a static variable to ax */
1505 /* Create the correct label name */
1506 char* lbuf = GetLabelName (flags, label, offs);
1508 switch (flags & CF_TYPE) {
1511 AddCodeLine ("clc");
1512 AddCodeLine ("adc %s", lbuf);
1513 AddCodeLine ("bcc *+3");
1514 AddCodeLine ("inx");
1515 AddCodeHint ("x:!");
1519 AddCodeLine ("clc");
1520 AddCodeLine ("adc %s", lbuf);
1521 AddCodeLine ("tay");
1522 AddCodeLine ("txa");
1523 AddCodeLine ("adc %s+1", lbuf);
1524 AddCodeLine ("tax");
1525 AddCodeLine ("tya");
1529 /* Do it the old way */
1531 g_getstatic (flags, label, offs);
1543 /*****************************************************************************/
1544 /* Compares of ax with a variable with fixed address */
1545 /*****************************************************************************/
1549 void g_cmplocal (unsigned flags, int offs)
1550 /* Compare a local variable to ax */
1552 Internal ("g_cmplocal not implemented");
1557 void g_cmpstatic (unsigned flags, unsigned label, unsigned offs)
1558 /* Compare a static variable to ax */
1560 Internal ("g_cmpstatic not implemented");
1565 /*****************************************************************************/
1566 /* Special op= functions */
1567 /*****************************************************************************/
1571 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
1573 /* Emit += for a static variable */
1575 /* Create the correct label name */
1576 char* lbuf = GetLabelName (flags, label, offs);
1578 /* Check the size and determine operation */
1579 switch (flags & CF_TYPE) {
1582 if (flags & CF_FORCECHAR) {
1583 AddCodeLine ("ldx #$00");
1584 if (flags & CF_CONST) {
1586 AddCodeLine ("inc %s", lbuf);
1587 AddCodeLine ("lda %s", lbuf);
1589 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1590 AddCodeLine ("clc");
1591 AddCodeLine ("adc %s", lbuf);
1592 AddCodeLine ("sta %s", lbuf);
1595 AddCodeLine ("clc");
1596 AddCodeLine ("adc %s", lbuf);
1597 AddCodeLine ("sta %s", lbuf);
1599 if ((flags & CF_UNSIGNED) == 0) {
1600 AddCodeLine ("bpl *+3");
1601 AddCodeLine ("dex");
1602 AddCodeHint ("x:!"); /* Invalidate X */
1609 if (flags & CF_CONST) {
1611 unsigned L = GetLocalLabel ();
1612 AddCodeLine ("inc %s", lbuf);
1613 AddCodeLine ("bne %s", LocalLabelName (L));
1614 AddCodeLine ("inc %s+1", lbuf);
1616 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1617 AddCodeLine ("ldx %s+1", lbuf);
1619 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1620 AddCodeLine ("clc");
1621 AddCodeLine ("adc %s", lbuf);
1622 AddCodeLine ("sta %s", lbuf);
1624 unsigned L = GetLocalLabel ();
1625 AddCodeLine ("bcc %s", LocalLabelName (L));
1626 AddCodeLine ("inc %s+1", lbuf);
1628 AddCodeLine ("ldx %s+1", lbuf);
1630 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1631 AddCodeLine ("adc %s+1", lbuf);
1632 AddCodeLine ("sta %s+1", lbuf);
1633 AddCodeLine ("tax");
1634 AddCodeLine ("lda %s", lbuf);
1638 AddCodeLine ("clc");
1639 AddCodeLine ("adc %s", lbuf);
1640 AddCodeLine ("sta %s", lbuf);
1641 AddCodeLine ("txa");
1642 AddCodeLine ("adc %s+1", lbuf);
1643 AddCodeLine ("sta %s+1", lbuf);
1644 AddCodeLine ("tax");
1645 AddCodeLine ("lda %s", lbuf);
1650 if (flags & CF_CONST) {
1652 AddCodeLine ("ldy #<(%s)", lbuf);
1653 AddCodeLine ("sty ptr1");
1654 AddCodeLine ("ldy #>(%s+1)", lbuf);
1656 AddCodeLine ("jsr laddeq1");
1658 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1659 AddCodeLine ("jsr laddeqa");
1662 g_getstatic (flags, label, offs);
1664 g_putstatic (flags, label, offs);
1667 AddCodeLine ("ldy #<(%s)", lbuf);
1668 AddCodeLine ("sty ptr1");
1669 AddCodeLine ("ldy #>(%s+1)", lbuf);
1670 AddCodeLine ("jsr laddeq");
1681 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1682 /* Emit += for a local variable */
1684 /* Calculate the true offset, check it, load it into Y */
1686 CheckLocalOffs (offs);
1688 /* Check the size and determine operation */
1689 switch (flags & CF_TYPE) {
1692 if (flags & CF_FORCECHAR) {
1694 AddCodeLine ("ldx #$00");
1695 if (flags & CF_CONST) {
1696 AddCodeLine ("clc");
1697 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1698 AddCodeLine ("adc (sp,x)");
1699 AddCodeLine ("sta (sp,x)");
1701 AddCodeLine ("clc");
1702 AddCodeLine ("adc (sp,x)");
1703 AddCodeLine ("sta (sp,x)");
1707 AddCodeLine ("ldx #$00");
1708 if (flags & CF_CONST) {
1709 AddCodeLine ("clc");
1710 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1711 AddCodeLine ("adc (sp),y");
1712 AddCodeLine ("sta (sp),y");
1714 AddCodeLine ("clc");
1715 AddCodeLine ("adc (sp),y");
1716 AddCodeLine ("sta (sp),y");
1719 if ((flags & CF_UNSIGNED) == 0) {
1720 AddCodeLine ("bpl *+3");
1721 AddCodeLine ("dex");
1722 AddCodeHint ("x:!"); /* Invalidate X */
1729 if (flags & CF_CONST) {
1730 g_getimmed (flags, val, 0);
1733 AddCodeLine ("jsr addeq0sp");
1736 AddCodeLine ("jsr addeqysp");
1741 if (flags & CF_CONST) {
1742 g_getimmed (flags, val, 0);
1745 AddCodeLine ("jsr laddeq0sp");
1748 AddCodeLine ("jsr laddeqysp");
1759 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1760 /* Emit += for the location with address in ax */
1762 /* If the offset is too large for a byte register, add the high byte
1763 * of the offset to the primary. Beware: We need a special correction
1764 * if the offset in the low byte will overflow in the operation.
1766 offs = MakeByteOffs (flags, offs);
1768 /* Check the size and determine operation */
1769 switch (flags & CF_TYPE) {
1772 AddCodeLine ("sta ptr1");
1773 AddCodeLine ("stx ptr1+1");
1775 AddCodeLine ("ldx #$00");
1776 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1777 AddCodeLine ("clc");
1778 AddCodeLine ("adc (ptr1,x)");
1779 AddCodeLine ("sta (ptr1,x)");
1781 AddCodeLine ("ldy #$%02X", offs);
1782 AddCodeLine ("ldx #$00");
1783 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1784 AddCodeLine ("clc");
1785 AddCodeLine ("adc (ptr1),y");
1786 AddCodeLine ("sta (ptr1),y");
1791 if (CodeSizeFactor >= 200) {
1792 /* Lots of code, use only if size is not important */
1793 AddCodeLine ("sta ptr1");
1794 AddCodeLine ("stx ptr1+1");
1795 AddCodeLine ("ldy #$%02X", offs);
1796 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1797 AddCodeLine ("clc");
1798 AddCodeLine ("adc (ptr1),y");
1799 AddCodeLine ("sta (ptr1),y");
1800 AddCodeLine ("pha");
1801 AddCodeLine ("iny");
1802 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1803 AddCodeLine ("adc (ptr1),y");
1804 AddCodeLine ("sta (ptr1),y");
1805 AddCodeLine ("tax");
1806 AddCodeLine ("pla");
1812 AddCodeLine ("jsr pushax"); /* Push the address */
1813 push (flags); /* Correct the internal sp */
1814 g_getind (flags, offs); /* Fetch the value */
1815 g_inc (flags, val); /* Increment value in primary */
1816 g_putind (flags, offs); /* Store the value back */
1826 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1828 /* Emit -= for a static variable */
1830 /* Create the correct label name */
1831 char* lbuf = GetLabelName (flags, label, offs);
1833 /* Check the size and determine operation */
1834 switch (flags & CF_TYPE) {
1837 if (flags & CF_FORCECHAR) {
1838 AddCodeLine ("ldx #$00");
1839 if (flags & CF_CONST) {
1841 AddCodeLine ("dec %s", lbuf);
1842 AddCodeLine ("lda %s", lbuf);
1844 AddCodeLine ("sec");
1845 AddCodeLine ("lda %s", lbuf);
1846 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1847 AddCodeLine ("sta %s", lbuf);
1850 AddCodeLine ("sec");
1851 AddCodeLine ("sta tmp1");
1852 AddCodeLine ("lda %s", lbuf);
1853 AddCodeLine ("sbc tmp1");
1854 AddCodeLine ("sta %s", lbuf);
1856 if ((flags & CF_UNSIGNED) == 0) {
1857 AddCodeLine ("bpl *+3");
1858 AddCodeLine ("dex");
1859 AddCodeHint ("x:!"); /* Invalidate X */
1866 AddCodeLine ("sec");
1867 if (flags & CF_CONST) {
1868 AddCodeLine ("lda %s", lbuf);
1869 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1870 AddCodeLine ("sta %s", lbuf);
1872 unsigned L = GetLocalLabel ();
1873 AddCodeLine ("bcs %s", LocalLabelName (L));
1874 AddCodeLine ("dec %s+1", lbuf);
1876 AddCodeLine ("ldx %s+1", lbuf);
1878 AddCodeLine ("lda %s+1", lbuf);
1879 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1880 AddCodeLine ("sta %s+1", lbuf);
1881 AddCodeLine ("tax");
1882 AddCodeLine ("lda %s", lbuf);
1885 AddCodeLine ("sta tmp1");
1886 AddCodeLine ("lda %s", lbuf);
1887 AddCodeLine ("sbc tmp1");
1888 AddCodeLine ("sta %s", lbuf);
1889 AddCodeLine ("stx tmp1");
1890 AddCodeLine ("lda %s+1", lbuf);
1891 AddCodeLine ("sbc tmp1");
1892 AddCodeLine ("sta %s+1", lbuf);
1893 AddCodeLine ("tax");
1894 AddCodeLine ("lda %s", lbuf);
1899 if (flags & CF_CONST) {
1901 AddCodeLine ("ldy #<(%s)", lbuf);
1902 AddCodeLine ("sty ptr1");
1903 AddCodeLine ("ldy #>(%s+1)", lbuf);
1905 AddCodeLine ("jsr lsubeq1");
1907 AddCodeLine ("lda #$%02X", (unsigned char)val);
1908 AddCodeLine ("jsr lsubeqa");
1911 g_getstatic (flags, label, offs);
1913 g_putstatic (flags, label, offs);
1916 AddCodeLine ("ldy #<(%s)", lbuf);
1917 AddCodeLine ("sty ptr1");
1918 AddCodeLine ("ldy #>(%s+1)", lbuf);
1919 AddCodeLine ("jsr lsubeq");
1930 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1931 /* Emit -= for a local variable */
1933 /* Calculate the true offset, check it, load it into Y */
1935 CheckLocalOffs (offs);
1937 /* Check the size and determine operation */
1938 switch (flags & CF_TYPE) {
1941 if (flags & CF_FORCECHAR) {
1943 AddCodeLine ("ldx #$00");
1944 AddCodeLine ("sec");
1945 if (flags & CF_CONST) {
1946 AddCodeLine ("lda (sp),y");
1947 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1949 AddCodeLine ("sta tmp1");
1950 AddCodeLine ("lda (sp),y");
1951 AddCodeLine ("sbc tmp1");
1953 AddCodeLine ("sta (sp),y");
1954 if ((flags & CF_UNSIGNED) == 0) {
1955 AddCodeLine ("bpl *+3");
1956 AddCodeLine ("dex");
1957 AddCodeHint ("x:!"); /* Invalidate X */
1964 if (flags & CF_CONST) {
1965 g_getimmed (flags, val, 0);
1968 AddCodeLine ("jsr subeq0sp");
1971 AddCodeLine ("jsr subeqysp");
1976 if (flags & CF_CONST) {
1977 g_getimmed (flags, val, 0);
1980 AddCodeLine ("jsr lsubeq0sp");
1983 AddCodeLine ("jsr lsubeqysp");
1994 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
1995 /* Emit -= for the location with address in ax */
1997 /* If the offset is too large for a byte register, add the high byte
1998 * of the offset to the primary. Beware: We need a special correction
1999 * if the offset in the low byte will overflow in the operation.
2001 offs = MakeByteOffs (flags, offs);
2003 /* Check the size and determine operation */
2004 switch (flags & CF_TYPE) {
2007 AddCodeLine ("sta ptr1");
2008 AddCodeLine ("stx ptr1+1");
2010 AddCodeLine ("ldx #$00");
2011 AddCodeLine ("lda (ptr1,x)");
2012 AddCodeLine ("sec");
2013 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2014 AddCodeLine ("sta (ptr1,x)");
2016 AddCodeLine ("ldy #$%02X", offs);
2017 AddCodeLine ("ldx #$00");
2018 AddCodeLine ("lda (ptr1),y");
2019 AddCodeLine ("sec");
2020 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2021 AddCodeLine ("sta (ptr1),y");
2026 if (CodeSizeFactor >= 200) {
2027 /* Lots of code, use only if size is not important */
2028 AddCodeLine ("sta ptr1");
2029 AddCodeLine ("stx ptr1+1");
2030 AddCodeLine ("ldy #$%02X", offs);
2031 AddCodeLine ("lda (ptr1),y");
2032 AddCodeLine ("sec");
2033 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2034 AddCodeLine ("sta (ptr1),y");
2035 AddCodeLine ("pha");
2036 AddCodeLine ("iny");
2037 AddCodeLine ("lda (ptr1),y");
2038 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2039 AddCodeLine ("sta (ptr1),y");
2040 AddCodeLine ("tax");
2041 AddCodeLine ("pla");
2047 AddCodeLine ("jsr pushax"); /* Push the address */
2048 push (flags); /* Correct the internal sp */
2049 g_getind (flags, offs); /* Fetch the value */
2050 g_dec (flags, val); /* Increment value in primary */
2051 g_putind (flags, offs); /* Store the value back */
2061 /*****************************************************************************/
2062 /* Add a variable address to the value in ax */
2063 /*****************************************************************************/
2067 void g_addaddr_local (unsigned flags, int offs)
2068 /* Add the address of a local variable to ax */
2070 /* Add the offset */
2073 /* We cannot address more then 256 bytes of locals anyway */
2074 CheckLocalOffs (offs);
2075 AddCodeLine ("clc");
2076 AddCodeLine ("adc #$%02X", offs & 0xFF);
2077 AddCodeLine ("bcc *+4"); /* Do also skip the CLC insn below */
2078 AddCodeLine ("inx");
2079 AddCodeHint ("x:!"); /* Invalidate X */
2082 /* Add the current stackpointer value */
2083 AddCodeLine ("clc");
2084 AddCodeLine ("adc sp");
2085 AddCodeLine ("tay");
2086 AddCodeLine ("txa");
2087 AddCodeLine ("adc sp+1");
2088 AddCodeLine ("tax");
2089 AddCodeLine ("tya");
2094 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2095 /* Add the address of a static variable to ax */
2097 /* Create the correct label name */
2098 char* lbuf = GetLabelName (flags, label, offs);
2100 /* Add the address to the current ax value */
2101 AddCodeLine ("clc");
2102 AddCodeLine ("adc #<(%s)", lbuf);
2103 AddCodeLine ("tay");
2104 AddCodeLine ("txa");
2105 AddCodeLine ("adc #>(%s)", lbuf);
2106 AddCodeLine ("tax");
2107 AddCodeLine ("tya");
2112 /*****************************************************************************/
2114 /*****************************************************************************/
2118 void g_save (unsigned flags)
2119 /* Copy primary register to hold register. */
2121 /* Check the size and determine operation */
2122 switch (flags & CF_TYPE) {
2125 if (flags & CF_FORCECHAR) {
2126 AddCodeLine ("pha");
2132 AddCodeLine ("sta regsave");
2133 AddCodeLine ("stx regsave+1");
2137 AddCodeLine ("jsr saveeax");
2147 void g_restore (unsigned flags)
2148 /* Copy hold register to primary. */
2150 /* Check the size and determine operation */
2151 switch (flags & CF_TYPE) {
2154 if (flags & CF_FORCECHAR) {
2155 AddCodeLine ("pla");
2161 AddCodeLine ("lda regsave");
2162 AddCodeLine ("ldx regsave+1");
2166 AddCodeLine ("jsr resteax");
2176 void g_cmp (unsigned flags, unsigned long val)
2177 /* Immidiate compare. The primary register will not be changed, Z flag
2181 /* Check the size and determine operation */
2182 switch (flags & CF_TYPE) {
2185 if (flags & CF_FORCECHAR) {
2186 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2192 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2193 AddCodeLine ("bne *+4");
2194 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2198 Internal ("g_cmp: Long compares not implemented");
2208 static void oper (unsigned flags, unsigned long val, char** subs)
2209 /* Encode a binary operation. subs is a pointer to four groups of three
2211 * 0-2 --> Operate on ints
2212 * 3-5 --> Operate on unsigneds
2213 * 6-8 --> Operate on longs
2214 * 9-11 --> Operate on unsigned longs
2216 * The first subroutine names in each string group is used to encode an
2217 * operation with a zero constant, the second to encode an operation with
2218 * a 8 bit constant, and the third is used in all other cases.
2223 /* Determine the offset into the array */
2224 offs = (flags & CF_UNSIGNED)? 3 : 0;
2225 switch (flags & CF_TYPE) {
2238 /* Encode the operation */
2239 if (flags & CF_CONST) {
2240 /* Constant value given */
2241 if (val == 0 && subs [offs+0]) {
2242 /* Special case: constant with value zero */
2243 AddCodeLine ("jsr %s", subs [offs+0]);
2244 } else if (val < 0x100 && subs [offs+1]) {
2245 /* Special case: constant with high byte zero */
2246 ldaconst (val); /* Load low byte */
2247 AddCodeLine ("jsr %s", subs [offs+1]);
2249 /* Others: arbitrary constant value */
2250 g_getimmed (flags, val, 0); /* Load value */
2251 AddCodeLine ("jsr %s", subs [offs+2]);
2254 /* Value not constant (is already in (e)ax) */
2255 AddCodeLine ("jsr %s", subs [offs+2]);
2258 /* The operation will pop it's argument */
2264 void g_test (unsigned flags)
2265 /* Test the value in the primary and set the condition codes */
2267 switch (flags & CF_TYPE) {
2270 if (flags & CF_FORCECHAR) {
2271 AddCodeLine ("tax");
2277 AddCodeLine ("stx tmp1");
2278 AddCodeLine ("ora tmp1");
2282 if (flags & CF_UNSIGNED) {
2283 AddCodeLine ("jsr utsteax");
2285 AddCodeLine ("jsr tsteax");
2297 void g_push (unsigned flags, unsigned long val)
2298 /* Push the primary register or a constant value onto the stack */
2302 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2304 /* We have a constant 8 or 16 bit value */
2305 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2307 /* Handle as 8 bit value */
2308 if (CodeSizeFactor >= 165 || val > 2) {
2310 AddCodeLine ("jsr pusha");
2312 AddCodeLine ("jsr pushc%d", (int) val);
2317 /* Handle as 16 bit value */
2318 hi = (unsigned char) (val >> 8);
2320 AddCodeLine ("jsr push%u", (unsigned) val);
2321 } else if (hi == 0 || hi == 0xFF) {
2322 /* Use special function */
2324 AddCodeLine ("jsr %s", (hi == 0)? "pusha0" : "pushaFF");
2327 g_getimmed (flags, val, 0);
2328 AddCodeLine ("jsr pushax");
2334 /* Value is not 16 bit or not constant */
2335 if (flags & CF_CONST) {
2336 /* Constant 32 bit value, load into eax */
2337 g_getimmed (flags, val, 0);
2340 /* Push the primary register */
2341 switch (flags & CF_TYPE) {
2344 if (flags & CF_FORCECHAR) {
2345 /* Handle as char */
2346 AddCodeLine ("jsr pusha");
2351 AddCodeLine ("jsr pushax");
2355 AddCodeLine ("jsr pusheax");
2365 /* Adjust the stack offset */
2371 void g_swap (unsigned flags)
2372 /* Swap the primary register and the top of the stack. flags give the type
2373 * of *both* values (must have same size).
2376 switch (flags & CF_TYPE) {
2380 AddCodeLine ("jsr swapstk");
2384 AddCodeLine ("jsr swapestk");
2395 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2396 /* Call the specified subroutine name */
2398 if ((Flags & CF_FIXARGC) == 0) {
2399 /* Pass the argument count */
2402 AddCodeLine ("jsr _%s", Label);
2403 oursp += ArgSize; /* callee pops args */
2408 void g_callind (unsigned Flags, unsigned ArgSize)
2409 /* Call subroutine with address in AX */
2411 if ((Flags & CF_FIXARGC) == 0) {
2412 /* Pass arg count */
2415 AddCodeLine ("jsr callax"); /* do the call */
2416 oursp += ArgSize; /* callee pops args */
2421 void g_jump (unsigned Label)
2422 /* Jump to specified internal label number */
2424 AddCodeLine ("jmp %s", LocalLabelName (Label));
2429 void g_switch (unsigned Flags)
2430 /* Output switch statement preamble */
2432 switch (Flags & CF_TYPE) {
2436 AddCodeLine ("jsr switch");
2440 AddCodeLine ("jsr lswitch");
2451 void g_case (unsigned flags, unsigned label, unsigned long val)
2452 /* Create table code for one case selector */
2454 switch (flags & CF_TYPE) {
2458 AddCodeLine (".word $%04X, %s",
2459 (unsigned)(val & 0xFFFF),
2460 LocalLabelName (label));
2464 AddCodeLine (".dword $%08lX", val);
2465 AddCodeLine (".word %s", LocalLabelName (label));
2476 void g_truejump (unsigned flags, unsigned label)
2477 /* Jump to label if zero flag clear */
2479 AddCodeLine ("jne %s", LocalLabelName (label));
2484 void g_falsejump (unsigned flags, unsigned label)
2485 /* Jump to label if zero flag set */
2487 AddCodeLine ("jeq %s", LocalLabelName (label));
2492 static void mod_internal (int k, char* verb1, char* verb2)
2495 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2499 AddCodeLine ("jsr %ssp", verb2);
2505 void g_space (int space)
2506 /* Create or drop space on the stack */
2509 mod_internal (-space, "inc", "addy");
2510 } else if (space > 0) {
2511 mod_internal (space, "dec", "suby");
2517 void g_cstackcheck (void)
2518 /* Check for a C stack overflow */
2520 AddCodeLine ("jsr cstkchk");
2525 void g_stackcheck (void)
2526 /* Check for a stack overflow */
2528 AddCodeLine ("jsr stkchk");
2533 void g_add (unsigned flags, unsigned long val)
2534 /* Primary = TOS + Primary */
2536 static char* ops [12] = {
2537 0, "tosadda0", "tosaddax",
2538 0, "tosadda0", "tosaddax",
2543 if (flags & CF_CONST) {
2544 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2545 g_push (flags & ~CF_CONST, 0);
2547 oper (flags, val, ops);
2552 void g_sub (unsigned flags, unsigned long val)
2553 /* Primary = TOS - Primary */
2555 static char* ops [12] = {
2556 0, "tossuba0", "tossubax",
2557 0, "tossuba0", "tossubax",
2562 if (flags & CF_CONST) {
2563 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2564 g_push (flags & ~CF_CONST, 0);
2566 oper (flags, val, ops);
2571 void g_rsub (unsigned flags, unsigned long val)
2572 /* Primary = Primary - TOS */
2574 static char* ops [12] = {
2575 0, "tosrsuba0", "tosrsubax",
2576 0, "tosrsuba0", "tosrsubax",
2580 oper (flags, val, ops);
2585 void g_mul (unsigned flags, unsigned long val)
2586 /* Primary = TOS * Primary */
2588 static char* ops [12] = {
2589 0, "tosmula0", "tosmulax",
2590 0, "tosumula0", "tosumulax",
2597 /* Do strength reduction if the value is constant and a power of two */
2598 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2599 /* Generate a shift instead */
2604 /* If the right hand side is const, the lhs is not on stack but still
2605 * in the primary register.
2607 if (flags & CF_CONST) {
2609 switch (flags & CF_TYPE) {
2612 if (flags & CF_FORCECHAR) {
2613 /* Handle some special cases */
2617 AddCodeLine ("sta tmp1");
2618 AddCodeLine ("asl a");
2619 AddCodeLine ("clc");
2620 AddCodeLine ("adc tmp1");
2624 AddCodeLine ("sta tmp1");
2625 AddCodeLine ("asl a");
2626 AddCodeLine ("asl a");
2627 AddCodeLine ("clc");
2628 AddCodeLine ("adc tmp1");
2632 AddCodeLine ("sta tmp1");
2633 AddCodeLine ("asl a");
2634 AddCodeLine ("asl a");
2635 AddCodeLine ("clc");
2636 AddCodeLine ("adc tmp1");
2637 AddCodeLine ("asl a");
2653 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2654 * into the normal, non-optimized stuff.
2656 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2657 g_push (flags & ~CF_CONST, 0);
2661 /* Use long way over the stack */
2662 oper (flags, val, ops);
2667 void g_div (unsigned flags, unsigned long val)
2668 /* Primary = TOS / Primary */
2670 static char* ops [12] = {
2671 0, "tosdiva0", "tosdivax",
2672 0, "tosudiva0", "tosudivax",
2677 /* Do strength reduction if the value is constant and a power of two */
2679 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2680 /* Generate a shift instead */
2683 /* Generate a division */
2684 if (flags & CF_CONST) {
2685 /* lhs is not on stack */
2686 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2687 g_push (flags & ~CF_CONST, 0);
2689 oper (flags, val, ops);
2695 void g_mod (unsigned flags, unsigned long val)
2696 /* Primary = TOS % Primary */
2698 static char* ops [12] = {
2699 0, "tosmoda0", "tosmodax",
2700 0, "tosumoda0", "tosumodax",
2706 /* Check if we can do some cost reduction */
2707 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2708 /* We can do that with an AND operation */
2709 g_and (flags, val - 1);
2711 /* Do it the hard way... */
2712 if (flags & CF_CONST) {
2713 /* lhs is not on stack */
2714 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2715 g_push (flags & ~CF_CONST, 0);
2717 oper (flags, val, ops);
2723 void g_or (unsigned flags, unsigned long val)
2724 /* Primary = TOS | Primary */
2726 static char* ops [12] = {
2727 0, "tosora0", "tosorax",
2728 0, "tosora0", "tosorax",
2733 /* If the right hand side is const, the lhs is not on stack but still
2734 * in the primary register.
2736 if (flags & CF_CONST) {
2738 switch (flags & CF_TYPE) {
2741 if (flags & CF_FORCECHAR) {
2742 if ((val & 0xFF) != 0xFF) {
2743 AddCodeLine ("ora #$%02X", (unsigned char)val);
2751 AddCodeLine ("ora #$%02X", (unsigned char)val);
2758 AddCodeLine ("ora #$%02X", (unsigned char)val);
2767 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2768 * into the normal, non-optimized stuff.
2770 g_push (flags & ~CF_CONST, 0);
2774 /* Use long way over the stack */
2775 oper (flags, val, ops);
2780 void g_xor (unsigned flags, unsigned long val)
2781 /* Primary = TOS ^ Primary */
2783 static char* ops [12] = {
2784 0, "tosxora0", "tosxorax",
2785 0, "tosxora0", "tosxorax",
2791 /* If the right hand side is const, the lhs is not on stack but still
2792 * in the primary register.
2794 if (flags & CF_CONST) {
2796 switch (flags & CF_TYPE) {
2799 if (flags & CF_FORCECHAR) {
2800 if ((val & 0xFF) != 0) {
2801 AddCodeLine ("eor #$%02X", (unsigned char)val);
2810 AddCodeLine ("eor #$%02X", (unsigned char)val);
2813 } else if ((val & 0xFF) == 0) {
2814 AddCodeLine ("pha");
2815 AddCodeLine ("txa");
2816 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2817 AddCodeLine ("tax");
2818 AddCodeLine ("pla");
2826 AddCodeLine ("eor #$%02X", (unsigned char)val);
2836 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2837 * into the normal, non-optimized stuff.
2839 g_push (flags & ~CF_CONST, 0);
2843 /* Use long way over the stack */
2844 oper (flags, val, ops);
2849 void g_and (unsigned flags, unsigned long val)
2850 /* Primary = TOS & Primary */
2852 static char* ops [12] = {
2853 0, "tosanda0", "tosandax",
2854 0, "tosanda0", "tosandax",
2859 /* If the right hand side is const, the lhs is not on stack but still
2860 * in the primary register.
2862 if (flags & CF_CONST) {
2864 switch (flags & CF_TYPE) {
2867 if (flags & CF_FORCECHAR) {
2868 AddCodeLine ("and #$%02X", (unsigned char)val);
2873 if ((val & 0xFFFF) != 0xFFFF) {
2878 } else if (val != 0xFF) {
2879 AddCodeLine ("and #$%02X", (unsigned char)val);
2881 } else if ((val & 0xFF00) == 0xFF00) {
2882 AddCodeLine ("and #$%02X", (unsigned char)val);
2883 } else if ((val & 0x00FF) == 0x0000) {
2884 AddCodeLine ("txa");
2885 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2886 AddCodeLine ("tax");
2889 AddCodeLine ("tay");
2890 AddCodeLine ("txa");
2891 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2892 AddCodeLine ("tax");
2893 AddCodeLine ("tya");
2894 if ((val & 0x00FF) != 0x00FF) {
2895 AddCodeLine ("and #$%02X", (unsigned char)val);
2904 AddCodeLine ("stx sreg+1");
2905 AddCodeLine ("stx sreg");
2906 if ((val & 0xFF) != 0xFF) {
2907 AddCodeLine ("and #$%02X", (unsigned char)val);
2910 } else if (val == 0xFF00) {
2912 AddCodeLine ("sta sreg+1");
2913 AddCodeLine ("sta sreg");
2922 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2923 * into the normal, non-optimized stuff.
2925 g_push (flags & ~CF_CONST, 0);
2929 /* Use long way over the stack */
2930 oper (flags, val, ops);
2935 void g_asr (unsigned flags, unsigned long val)
2936 /* Primary = TOS >> Primary */
2938 static char* ops [12] = {
2939 0, "tosasra0", "tosasrax",
2940 0, "tosshra0", "tosshrax",
2945 /* If the right hand side is const, the lhs is not on stack but still
2946 * in the primary register.
2948 if (flags & CF_CONST) {
2950 switch (flags & CF_TYPE) {
2954 if (val >= 1 && val <= 3) {
2955 if (flags & CF_UNSIGNED) {
2956 AddCodeLine ("jsr shrax%ld", val);
2958 AddCodeLine ("jsr asrax%ld", val);
2961 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2962 AddCodeLine ("txa");
2969 if (val >= 1 && val <= 3) {
2970 if (flags & CF_UNSIGNED) {
2971 AddCodeLine ("jsr shreax%ld", val);
2973 AddCodeLine ("jsr asreax%ld", val);
2976 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2977 AddCodeLine ("txa");
2978 AddCodeLine ("ldx sreg");
2979 AddCodeLine ("ldy sreg+1");
2980 AddCodeLine ("sty sreg");
2981 AddCodeLine ("ldy #$00");
2982 AddCodeLine ("sty sreg+1");
2984 } else if (val == 16) {
2985 AddCodeLine ("ldy #$00");
2986 AddCodeLine ("ldx sreg+1");
2987 if ((flags & CF_UNSIGNED) == 0) {
2988 AddCodeLine ("bpl *+3");
2989 AddCodeLine ("dey");
2990 AddCodeHint ("y:!");
2992 AddCodeLine ("lda sreg");
2993 AddCodeLine ("sty sreg+1");
2994 AddCodeLine ("sty sreg");
3003 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3004 * into the normal, non-optimized stuff.
3006 g_push (flags & ~CF_CONST, 0);
3010 /* Use long way over the stack */
3011 oper (flags, val, ops);
3016 void g_asl (unsigned flags, unsigned long val)
3017 /* Primary = TOS << Primary */
3019 static char* ops [12] = {
3020 0, "tosasla0", "tosaslax",
3021 0, "tosshla0", "tosshlax",
3027 /* If the right hand side is const, the lhs is not on stack but still
3028 * in the primary register.
3030 if (flags & CF_CONST) {
3032 switch (flags & CF_TYPE) {
3036 if (val >= 1 && val <= 3) {
3037 if (flags & CF_UNSIGNED) {
3038 AddCodeLine ("jsr shlax%ld", val);
3040 AddCodeLine ("jsr aslax%ld", val);
3043 } else if (val == 8) {
3044 AddCodeLine ("tax");
3045 AddCodeLine ("lda #$00");
3051 if (val >= 1 && val <= 3) {
3052 if (flags & CF_UNSIGNED) {
3053 AddCodeLine ("jsr shleax%ld", val);
3055 AddCodeLine ("jsr asleax%ld", val);
3058 } else if (val == 8) {
3059 AddCodeLine ("ldy sreg");
3060 AddCodeLine ("sty sreg+1");
3061 AddCodeLine ("stx sreg");
3062 AddCodeLine ("tax");
3063 AddCodeLine ("lda #$00");
3065 } else if (val == 16) {
3066 AddCodeLine ("stx sreg+1");
3067 AddCodeLine ("sta sreg");
3068 AddCodeLine ("lda #$00");
3069 AddCodeLine ("tax");
3078 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3079 * into the normal, non-optimized stuff.
3081 g_push (flags & ~CF_CONST, 0);
3085 /* Use long way over the stack */
3086 oper (flags, val, ops);
3091 void g_neg (unsigned flags)
3092 /* Primary = -Primary */
3094 switch (flags & CF_TYPE) {
3098 AddCodeLine ("jsr negax");
3102 AddCodeLine ("jsr negeax");
3112 void g_bneg (unsigned flags)
3113 /* Primary = !Primary */
3115 switch (flags & CF_TYPE) {
3118 AddCodeLine ("jsr bnega");
3122 AddCodeLine ("jsr bnegax");
3126 AddCodeLine ("jsr bnegeax");
3136 void g_com (unsigned flags)
3137 /* Primary = ~Primary */
3139 switch (flags & CF_TYPE) {
3143 AddCodeLine ("jsr complax");
3147 AddCodeLine ("jsr compleax");
3157 void g_inc (unsigned flags, unsigned long val)
3158 /* Increment the primary register by a given number */
3160 /* Don't inc by zero */
3165 /* Generate code for the supported types */
3167 switch (flags & CF_TYPE) {
3170 if (flags & CF_FORCECHAR) {
3171 if (CPU == CPU_65C02 && val <= 2) {
3173 AddCodeLine ("ina");
3176 AddCodeLine ("clc");
3177 AddCodeLine ("adc #$%02X", (unsigned char)val);
3184 if (CPU == CPU_65C02 && val == 1) {
3185 AddCodeLine ("ina");
3186 AddCodeLine ("bne *+3");
3187 AddCodeLine ("inx");
3188 /* Tell the optimizer that the X register may be invalid */
3189 AddCodeHint ("x:!");
3190 } else if (CodeSizeFactor < 200) {
3193 AddCodeLine ("jsr incax%lu", val);
3194 } else if (val <= 255) {
3196 AddCodeLine ("jsr incaxy");
3198 g_add (flags | CF_CONST, val);
3201 /* Inline the code */
3203 if ((val & 0xFF) != 0) {
3204 AddCodeLine ("clc");
3205 AddCodeLine ("adc #$%02X", (unsigned char) val);
3206 AddCodeLine ("bcc *+3");
3207 AddCodeLine ("inx");
3208 /* Tell the optimizer that the X register may be invalid */
3209 AddCodeHint ("x:!");
3212 AddCodeLine ("inx");
3215 AddCodeLine ("inx");
3218 AddCodeLine ("clc");
3219 if ((val & 0xFF) != 0) {
3220 AddCodeLine ("adc #$%02X", (unsigned char) val);
3221 /* Tell the optimizer that the X register may be invalid */
3222 AddCodeHint ("x:!");
3224 AddCodeLine ("pha");
3225 AddCodeLine ("txa");
3226 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3227 AddCodeLine ("tax");
3228 AddCodeLine ("pla");
3236 AddCodeLine ("jsr inceaxy");
3238 g_add (flags | CF_CONST, val);
3250 void g_dec (unsigned flags, unsigned long val)
3251 /* Decrement the primary register by a given number */
3253 /* Don't dec by zero */
3258 /* Generate code for the supported types */
3260 switch (flags & CF_TYPE) {
3263 if (flags & CF_FORCECHAR) {
3264 if (CPU == CPU_65C02 && val <= 2) {
3266 AddCodeLine ("dea");
3269 AddCodeLine ("sec");
3270 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3277 if (CodeSizeFactor < 200) {
3278 /* Use subroutines */
3280 AddCodeLine ("jsr decax%d", (int) val);
3281 } else if (val <= 255) {
3283 AddCodeLine ("jsr decaxy");
3285 g_sub (flags | CF_CONST, val);
3288 /* Inline the code */
3290 if ((val & 0xFF) != 0) {
3291 AddCodeLine ("sec");
3292 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3293 AddCodeLine ("bcs *+3");
3294 AddCodeLine ("dex");
3295 /* Tell the optimizer that the X register may be invalid */
3296 AddCodeHint ("x:!");
3299 AddCodeLine ("dex");
3302 AddCodeLine ("dex");
3305 AddCodeLine ("sec");
3306 if ((val & 0xFF) != 0) {
3307 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3308 /* Tell the optimizer that the X register may be invalid */
3309 AddCodeHint ("x:!");
3311 AddCodeLine ("pha");
3312 AddCodeLine ("txa");
3313 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3314 AddCodeLine ("tax");
3315 AddCodeLine ("pla");
3323 AddCodeLine ("jsr deceaxy");
3325 g_sub (flags | CF_CONST, val);
3338 * Following are the conditional operators. They compare the TOS against
3339 * the primary and put a literal 1 in the primary if the condition is
3340 * true, otherwise they clear the primary register
3345 void g_eq (unsigned flags, unsigned long val)
3346 /* Test for equal */
3348 static char* ops [12] = {
3349 "toseq00", "toseqa0", "toseqax",
3350 "toseq00", "toseqa0", "toseqax",
3355 /* If the right hand side is const, the lhs is not on stack but still
3356 * in the primary register.
3358 if (flags & CF_CONST) {
3360 switch (flags & CF_TYPE) {
3363 if (flags & CF_FORCECHAR) {
3364 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3365 AddCodeLine ("jsr booleq");
3371 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3372 AddCodeLine ("bne *+4");
3373 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3374 AddCodeLine ("jsr booleq");
3384 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3385 * into the normal, non-optimized stuff.
3387 g_push (flags & ~CF_CONST, 0);
3391 /* Use long way over the stack */
3392 oper (flags, val, ops);
3397 void g_ne (unsigned flags, unsigned long val)
3398 /* Test for not equal */
3400 static char* ops [12] = {
3401 "tosne00", "tosnea0", "tosneax",
3402 "tosne00", "tosnea0", "tosneax",
3408 /* If the right hand side is const, the lhs is not on stack but still
3409 * in the primary register.
3411 if (flags & CF_CONST) {
3413 switch (flags & CF_TYPE) {
3416 if (flags & CF_FORCECHAR) {
3417 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3418 AddCodeLine ("jsr boolne");
3424 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3425 AddCodeLine ("bne *+4");
3426 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3427 AddCodeLine ("jsr boolne");
3437 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3438 * into the normal, non-optimized stuff.
3440 g_push (flags & ~CF_CONST, 0);
3444 /* Use long way over the stack */
3445 oper (flags, val, ops);
3450 void g_lt (unsigned flags, unsigned long val)
3451 /* Test for less than */
3453 static char* ops [12] = {
3454 "toslt00", "toslta0", "tosltax",
3455 "tosult00", "tosulta0", "tosultax",
3460 /* If the right hand side is const, the lhs is not on stack but still
3461 * in the primary register.
3463 if (flags & CF_CONST) {
3465 /* Give a warning in some special cases */
3466 if ((flags & CF_UNSIGNED) && val == 0) {
3467 Warning ("Condition is never true");
3470 /* Look at the type */
3471 switch (flags & CF_TYPE) {
3474 if (flags & CF_FORCECHAR) {
3475 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3476 if (flags & CF_UNSIGNED) {
3477 AddCodeLine ("jsr boolult");
3479 AddCodeLine ("jsr boollt");
3486 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3487 /* If we have a signed compare against zero, we only need to
3488 * test the high byte.
3490 AddCodeLine ("txa");
3491 AddCodeLine ("jsr boollt");
3494 /* Direct code only for unsigned data types */
3495 if (flags & CF_UNSIGNED) {
3496 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3497 AddCodeLine ("bne *+4");
3498 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3499 AddCodeLine ("jsr boolult");
3511 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3512 * into the normal, non-optimized stuff.
3514 g_push (flags & ~CF_CONST, 0);
3518 /* Use long way over the stack */
3519 oper (flags, val, ops);
3524 void g_le (unsigned flags, unsigned long val)
3525 /* Test for less than or equal to */
3527 static char* ops [12] = {
3528 "tosle00", "toslea0", "tosleax",
3529 "tosule00", "tosulea0", "tosuleax",
3535 /* If the right hand side is const, the lhs is not on stack but still
3536 * in the primary register.
3538 if (flags & CF_CONST) {
3540 /* Look at the type */
3541 switch (flags & CF_TYPE) {
3544 if (flags & CF_FORCECHAR) {
3545 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3546 if (flags & CF_UNSIGNED) {
3547 AddCodeLine ("jsr boolule");
3549 AddCodeLine ("jsr boolle");
3556 if (flags & CF_UNSIGNED) {
3557 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3558 AddCodeLine ("bne *+4");
3559 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3560 AddCodeLine ("jsr boolule");
3572 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3573 * into the normal, non-optimized stuff.
3575 g_push (flags & ~CF_CONST, 0);
3579 /* Use long way over the stack */
3580 oper (flags, val, ops);
3585 void g_gt (unsigned flags, unsigned long val)
3586 /* Test for greater than */
3588 static char* ops [12] = {
3589 "tosgt00", "tosgta0", "tosgtax",
3590 "tosugt00", "tosugta0", "tosugtax",
3596 /* If the right hand side is const, the lhs is not on stack but still
3597 * in the primary register.
3599 if (flags & CF_CONST) {
3601 /* Look at the type */
3602 switch (flags & CF_TYPE) {
3605 if (flags & CF_FORCECHAR) {
3606 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3607 if (flags & CF_UNSIGNED) {
3608 /* If we have a compare > 0, we will replace it by
3609 * != 0 here, since both are identical but the latter
3610 * is easier to optimize.
3613 AddCodeLine ("jsr boolugt");
3615 AddCodeLine ("jsr boolne");
3618 AddCodeLine ("jsr boolgt");
3625 if (flags & CF_UNSIGNED) {
3626 /* If we have a compare > 0, we will replace it by
3627 * != 0 here, since both are identical but the latter
3628 * is easier to optimize.
3630 if ((val & 0xFFFF) == 0) {
3631 AddCodeLine ("stx tmp1");
3632 AddCodeLine ("ora tmp1");
3633 AddCodeLine ("jsr boolne");
3635 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3636 AddCodeLine ("bne *+4");
3637 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3638 AddCodeLine ("jsr boolugt");
3651 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3652 * into the normal, non-optimized stuff.
3654 g_push (flags & ~CF_CONST, 0);
3658 /* Use long way over the stack */
3659 oper (flags, val, ops);
3664 void g_ge (unsigned flags, unsigned long val)
3665 /* Test for greater than or equal to */
3667 static char* ops [12] = {
3668 "tosge00", "tosgea0", "tosgeax",
3669 "tosuge00", "tosugea0", "tosugeax",
3675 /* If the right hand side is const, the lhs is not on stack but still
3676 * in the primary register.
3678 if (flags & CF_CONST) {
3680 /* Give a warning in some special cases */
3681 if ((flags & CF_UNSIGNED) && val == 0) {
3682 Warning ("Condition is always true");
3685 /* Look at the type */
3686 switch (flags & CF_TYPE) {
3689 if (flags & CF_FORCECHAR) {
3690 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3691 if (flags & CF_UNSIGNED) {
3692 AddCodeLine ("jsr booluge");
3694 AddCodeLine ("jsr boolge");
3701 if (flags & CF_UNSIGNED) {
3702 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3703 AddCodeLine ("bne *+4");
3704 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3705 AddCodeLine ("jsr booluge");
3717 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3718 * into the normal, non-optimized stuff.
3720 g_push (flags & ~CF_CONST, 0);
3724 /* Use long way over the stack */
3725 oper (flags, val, ops);
3730 /*****************************************************************************/
3731 /* Allocating static storage */
3732 /*****************************************************************************/
3736 void g_res (unsigned n)
3737 /* Reserve static storage, n bytes */
3739 AddDataLine ("\t.res\t%u,$00", n);
3744 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3745 /* Define data with the size given in flags */
3747 if (flags & CF_CONST) {
3749 /* Numeric constant */
3750 switch (flags & CF_TYPE) {
3753 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
3757 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
3761 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3772 /* Create the correct label name */
3773 const char* Label = GetLabelName (flags, val, offs);
3775 /* Labels are always 16 bit */
3776 AddDataLine ("\t.word\t%s", Label);
3783 void g_defbytes (const void* Bytes, unsigned Count)
3784 /* Output a row of bytes as a constant */
3790 /* Cast the buffer pointer */
3791 const unsigned char* Data = (const unsigned char*) Bytes;
3793 /* Output the stuff */
3796 /* How many go into this line? */
3797 if ((Chunk = Count) > 16) {
3802 /* Output one line */
3803 strcpy (Buf, "\t.byte\t");
3806 B += sprintf (B, "$%02X", *Data++);
3812 /* Output the line */
3819 void g_zerobytes (unsigned n)
3820 /* Output n bytes of data initialized with zero */
3822 AddDataLine ("\t.res\t%u,$00", n);
3827 /*****************************************************************************/
3828 /* User supplied assembler code */
3829 /*****************************************************************************/
3833 void g_asmcode (const char* Line, int Len)
3834 /* Output one line of assembler code. If Len is greater than zero, it is used
3835 * as the maximum number of characters to use from Line.
3839 AddCodeLine ("%.*s", Len, Line);
3841 AddCodeLine ("%s", Line);
3847 /*****************************************************************************/
3848 /* Inlined known functions */
3849 /*****************************************************************************/
3853 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3854 /* Inline the strlen() function */
3856 /* We need a label in both cases */
3857 unsigned label = GetLocalLabel ();
3859 /* Two different encodings */
3860 if (flags & CF_CONST) {
3862 /* The address of the string is constant. Create the correct label name */
3863 char* lbuf = GetLabelName (flags, val, offs);
3865 /* Generate the strlen code */
3866 AddCodeLine ("ldy #$FF");
3867 g_defcodelabel (label);
3868 AddCodeLine ("iny");
3869 AddCodeLine ("lda %s,y", lbuf);
3870 AddCodeLine ("bne %s", LocalLabelName (label));
3871 AddCodeLine ("tax");
3872 AddCodeLine ("tya");
3876 /* Address not constant but in primary */
3877 if (CodeSizeFactor < 400) {
3878 /* This is too much code, so call strlen instead of inlining */
3879 AddCodeLine ("jsr _strlen");
3881 /* Inline the function */
3882 AddCodeLine ("sta ptr1");
3883 AddCodeLine ("stx ptr1+1");
3884 AddCodeLine ("ldy #$FF");
3885 g_defcodelabel (label);
3886 AddCodeLine ("iny");
3887 AddCodeLine ("lda (ptr1),y");
3888 AddCodeLine ("bne %s", LocalLabelName (label));
3889 AddCodeLine ("tax");
3890 AddCodeLine ("tya");