1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2002 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 /*****************************************************************************/
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");
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_save_regvars (int RegOffs, unsigned Bytes)
494 /* Save register variables */
496 /* Don't loop for up to two bytes */
499 AddCodeLine ("lda regbank%+d", RegOffs);
500 AddCodeLine ("jsr pusha");
502 } else if (Bytes == 2) {
504 AddCodeLine ("lda regbank%+d", RegOffs);
505 AddCodeLine ("ldx regbank%+d", RegOffs+1);
506 AddCodeLine ("jsr pushax");
510 /* More than two bytes - loop */
511 unsigned Label = GetLocalLabel ();
513 ldyconst (Bytes - 1);
515 g_defcodelabel (Label);
516 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
517 AddCodeLine ("sta (sp),y");
520 AddCodeLine ("bne %s", LocalLabelName (Label));
524 /* We pushed stuff, correct the stack pointer */
530 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
531 /* Restore register variables */
533 /* Calculate the actual stack offset and check it */
535 CheckLocalOffs (StackOffs);
537 /* Don't loop for up to two bytes */
540 ldyconst (StackOffs);
541 AddCodeLine ("lda (sp),y");
542 AddCodeLine ("sta regbank%+d", RegOffs);
544 } else if (Bytes == 2) {
546 ldyconst (StackOffs);
547 AddCodeLine ("lda (sp),y");
548 AddCodeLine ("sta regbank%+d", RegOffs);
550 AddCodeLine ("lda (sp),y");
551 AddCodeLine ("sta regbank%+d", RegOffs+1);
555 /* More than two bytes - loop */
556 unsigned Label = GetLocalLabel ();
557 ldyconst (StackOffs+Bytes-1);
559 g_defcodelabel (Label);
560 AddCodeLine ("lda (sp),y");
561 AddCodeLine ("sta regbank%+d,x", RegOffs-1);
564 AddCodeLine ("bne %s", LocalLabelName (Label));
571 /*****************************************************************************/
572 /* Fetching memory cells */
573 /*****************************************************************************/
577 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
578 /* Load a constant into the primary register */
580 unsigned char B1, B2, B3, B4;
584 if ((Flags & CF_CONST) != 0) {
586 /* Numeric constant */
587 switch (Flags & CF_TYPE) {
590 if ((Flags & CF_FORCECHAR) != 0) {
596 ldxconst ((Val >> 8) & 0xFF);
597 ldaconst (Val & 0xFF);
601 /* Split the value into 4 bytes */
602 B1 = (unsigned char) (Val >> 0);
603 B2 = (unsigned char) (Val >> 8);
604 B3 = (unsigned char) (Val >> 16);
605 B4 = (unsigned char) (Val >> 24);
607 /* Remember which bytes are done */
611 AddCodeLine ("ldx #$%02X", B2);
614 AddCodeLine ("stx sreg");
618 AddCodeLine ("stx sreg+1");
621 if ((Done & 0x04) == 0 && B1 != B3) {
622 AddCodeLine ("lda #$%02X", B3);
623 AddCodeLine ("sta sreg");
626 if ((Done & 0x08) == 0 && B1 != B4) {
627 AddCodeLine ("lda #$%02X", B4);
628 AddCodeLine ("sta sreg+1");
631 AddCodeLine ("lda #$%02X", B1);
633 if ((Done & 0x04) == 0) {
635 AddCodeLine ("sta sreg");
637 if ((Done & 0x08) == 0) {
639 AddCodeLine ("sta sreg+1");
651 /* Some sort of label */
652 const char* Label = GetLabelName (Flags, Val, Offs);
654 /* Load the address into the primary */
655 AddCodeLine ("lda #<(%s)", Label);
656 AddCodeLine ("ldx #>(%s)", Label);
663 void g_getstatic (unsigned flags, unsigned long label, long offs)
664 /* Fetch an static memory cell into the primary register */
666 /* Create the correct label name */
667 const char* lbuf = GetLabelName (flags, label, offs);
669 /* Check the size and generate the correct load operation */
670 switch (flags & CF_TYPE) {
673 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
674 AddCodeLine ("lda %s", lbuf); /* load A from the label */
677 AddCodeLine ("lda %s", lbuf); /* load A from the label */
678 if (!(flags & CF_UNSIGNED)) {
679 /* Must sign extend */
680 unsigned L = GetLocalLabel ();
681 AddCodeLine ("bpl %s", LocalLabelName (L));
689 AddCodeLine ("lda %s", lbuf);
690 if (flags & CF_TEST) {
691 AddCodeLine ("ora %s+1", lbuf);
693 AddCodeLine ("ldx %s+1", lbuf);
698 if (flags & CF_TEST) {
699 AddCodeLine ("lda %s+3", lbuf);
700 AddCodeLine ("ora %s+2", lbuf);
701 AddCodeLine ("ora %s+1", lbuf);
702 AddCodeLine ("ora %s+0", lbuf);
704 AddCodeLine ("lda %s+3", lbuf);
705 AddCodeLine ("sta sreg+1");
706 AddCodeLine ("lda %s+2", lbuf);
707 AddCodeLine ("sta sreg");
708 AddCodeLine ("ldx %s+1", lbuf);
709 AddCodeLine ("lda %s", lbuf);
721 void g_getlocal (unsigned flags, int offs)
722 /* Fetch specified local object (local var). */
725 CheckLocalOffs (offs);
726 switch (flags & CF_TYPE) {
729 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
731 AddCodeLine ("lda (sp),y");
734 AddCodeLine ("ldx #$00");
735 AddCodeLine ("lda (sp),y");
736 if ((flags & CF_UNSIGNED) == 0) {
737 unsigned L = GetLocalLabel();
738 AddCodeLine ("bpl %s", LocalLabelName (L));
746 CheckLocalOffs (offs + 1);
747 if (flags & CF_TEST) {
749 AddCodeLine ("lda (sp),y");
751 AddCodeLine ("ora (sp),y");
754 AddCodeLine ("jsr ldaxysp");
760 AddCodeLine ("jsr ldeaxysp");
770 void g_getind (unsigned flags, unsigned offs)
771 /* Fetch the specified object type indirect through the primary register
772 * into the primary register
775 /* If the offset is greater than 255, add the part that is > 255 to
776 * the primary. This way we get an easy addition and use the low byte
779 offs = MakeByteOffs (flags, offs);
781 /* Handle the indirect fetch */
782 switch (flags & CF_TYPE) {
785 /* Character sized */
786 if (flags & CF_UNSIGNED) {
788 AddCodeLine ("jsr ldauidx");
791 AddCodeLine ("jsr ldaidx");
796 if (flags & CF_TEST) {
798 AddCodeLine ("sta ptr1");
799 AddCodeLine ("stx ptr1+1");
800 AddCodeLine ("lda (ptr1),y");
802 AddCodeLine ("ora (ptr1),y");
805 AddCodeLine ("jsr ldaxidx");
811 AddCodeLine ("jsr ldeaxidx");
812 if (flags & CF_TEST) {
813 AddCodeLine ("jsr tsteax");
825 void g_leasp (int offs)
826 /* Fetch the address of the specified symbol into the primary register */
828 /* Calculate the offset relative to sp */
831 /* For value 0 we do direct code */
833 AddCodeLine ("lda sp");
834 AddCodeLine ("ldx sp+1");
836 if (CodeSizeFactor < 300) {
837 ldaconst (offs); /* Load A with offset value */
838 AddCodeLine ("jsr leaasp"); /* Load effective address */
840 unsigned L = GetLocalLabel ();
841 if (CPU == CPU_65C02 && offs == 1) {
842 AddCodeLine ("lda sp");
843 AddCodeLine ("ldx sp+1");
845 AddCodeLine ("bne %s", LocalLabelName (L));
850 AddCodeLine ("ldx sp+1");
851 AddCodeLine ("adc sp");
852 AddCodeLine ("bcc %s", LocalLabelName (L));
862 void g_leavariadic (int Offs)
863 /* Fetch the address of a parameter in a variadic function into the primary
867 unsigned ArgSizeOffs;
869 /* Calculate the offset relative to sp */
872 /* Get the offset of the parameter which is stored at sp+0 on function
873 * entry and check if this offset is reachable with a byte offset.
876 ArgSizeOffs = -oursp;
877 CheckLocalOffs (ArgSizeOffs);
879 /* Get the size of all parameters. */
880 ldyconst (ArgSizeOffs);
881 AddCodeLine ("lda (sp),y");
883 /* Add the value of the stackpointer */
884 if (CodeSizeFactor > 250) {
885 unsigned L = GetLocalLabel();
886 AddCodeLine ("ldx sp+1");
888 AddCodeLine ("adc sp");
889 AddCodeLine ("bcc %s", LocalLabelName (L));
893 AddCodeLine ("jsr leaasp");
896 /* Add the offset to the primary */
898 g_inc (CF_INT | CF_CONST, Offs);
899 } else if (Offs < 0) {
900 g_dec (CF_INT | CF_CONST, -Offs);
906 /*****************************************************************************/
907 /* Store into memory */
908 /*****************************************************************************/
912 void g_putstatic (unsigned flags, unsigned long label, long offs)
913 /* Store the primary register into the specified static memory cell */
915 /* Create the correct label name */
916 const char* lbuf = GetLabelName (flags, label, offs);
918 /* Check the size and generate the correct store operation */
919 switch (flags & CF_TYPE) {
922 AddCodeLine ("sta %s", lbuf);
926 AddCodeLine ("sta %s", lbuf);
927 AddCodeLine ("stx %s+1", lbuf);
931 AddCodeLine ("sta %s", lbuf);
932 AddCodeLine ("stx %s+1", lbuf);
933 AddCodeLine ("ldy sreg");
934 AddCodeLine ("sty %s+2", lbuf);
935 AddCodeLine ("ldy sreg+1");
936 AddCodeLine ("sty %s+3", lbuf);
947 void g_putlocal (unsigned Flags, int Offs, long Val)
948 /* Put data into local object. */
951 CheckLocalOffs (Offs);
952 switch (Flags & CF_TYPE) {
955 if (Flags & CF_CONST) {
956 AddCodeLine ("lda #$%02X", (unsigned char) Val);
959 AddCodeLine ("sta (sp),y");
963 if (Flags & CF_CONST) {
965 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
966 AddCodeLine ("sta (sp),y");
967 if ((Flags & CF_NOKEEP) == 0) {
968 /* Place high byte into X */
971 if ((Val & 0xFF) == Offs+1) {
972 /* The value we need is already in Y */
977 AddCodeLine ("lda #$%02X", (unsigned char) Val);
979 AddCodeLine ("sta (sp),y");
981 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
983 AddCodeLine ("jsr staxysp");
986 AddCodeLine ("sta (sp),y");
989 AddCodeLine ("sta (sp),y");
995 if (Flags & CF_CONST) {
996 g_getimmed (Flags, Val, 0);
999 AddCodeLine ("jsr steaxysp");
1010 void g_putind (unsigned Flags, unsigned Offs)
1011 /* Store the specified object type in the primary register at the address
1012 * on the top of the stack
1015 /* We can handle offsets below $100 directly, larger offsets must be added
1016 * to the address. Since a/x is in use, best code is achieved by adding
1017 * just the high byte. Be sure to check if the low byte will overflow while
1020 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1022 /* Overflow - we need to add the low byte also */
1023 AddCodeLine ("ldy #$00");
1024 AddCodeLine ("clc");
1025 AddCodeLine ("pha");
1026 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1027 AddCodeLine ("adc (sp),y");
1028 AddCodeLine ("sta (sp),y");
1029 AddCodeLine ("iny");
1030 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1031 AddCodeLine ("adc (sp),y");
1032 AddCodeLine ("sta (sp),y");
1033 AddCodeLine ("pla");
1035 /* Complete address is on stack, new offset is zero */
1038 } else if ((Offs & 0xFF00) != 0) {
1040 /* We can just add the high byte */
1041 AddCodeLine ("ldy #$01");
1042 AddCodeLine ("clc");
1043 AddCodeLine ("pha");
1044 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1045 AddCodeLine ("adc (sp),y");
1046 AddCodeLine ("sta (sp),y");
1047 AddCodeLine ("pla");
1049 /* Offset is now just the low byte */
1053 /* Check the size and determine operation */
1054 switch (Flags & CF_TYPE) {
1058 AddCodeLine ("jsr staspidx");
1063 AddCodeLine ("jsr staxspidx");
1068 AddCodeLine ("jsr steaxspidx");
1076 /* Pop the argument which is always a pointer */
1082 /*****************************************************************************/
1083 /* type conversion and similiar stuff */
1084 /*****************************************************************************/
1088 void g_toslong (unsigned flags)
1089 /* Make sure, the value on TOS is a long. Convert if necessary */
1091 switch (flags & CF_TYPE) {
1095 if (flags & CF_UNSIGNED) {
1096 AddCodeLine ("jsr tosulong");
1098 AddCodeLine ("jsr toslong");
1113 void g_tosint (unsigned flags)
1114 /* Make sure, the value on TOS is an int. Convert if necessary */
1116 switch (flags & CF_TYPE) {
1123 AddCodeLine ("jsr tosint");
1134 void g_reglong (unsigned flags)
1135 /* Make sure, the value in the primary register a long. Convert if necessary */
1137 switch (flags & CF_TYPE) {
1141 if (flags & CF_UNSIGNED) {
1142 if (CodeSizeFactor >= 200) {
1144 AddCodeLine ("sty sreg");
1145 AddCodeLine ("sty sreg+1");
1147 AddCodeLine ("jsr axulong");
1150 AddCodeLine ("jsr axlong");
1164 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1165 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1166 * value, that corresponds to the value on TOS, rhs corresponds to the value
1167 * in (e)ax. The return value is the the flags value for the resulting type.
1170 unsigned ltype, rtype;
1173 /* Get the type spec from the flags */
1174 ltype = lhs & CF_TYPE;
1175 rtype = rhs & CF_TYPE;
1177 /* Check if a conversion is needed */
1178 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1179 /* We must promote the primary register to long */
1181 /* Get the new rhs type */
1182 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1184 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1185 /* We must promote the lhs to long */
1191 /* Get the new rhs type */
1192 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1196 /* Determine the result type for the operation:
1197 * - The result is const if both operands are const.
1198 * - The result is unsigned if one of the operands is unsigned.
1199 * - The result is long if one of the operands is long.
1200 * - Otherwise the result is int sized.
1202 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1203 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1204 if (rtype == CF_LONG || ltype == CF_LONG) {
1214 unsigned g_typecast (unsigned lhs, unsigned rhs)
1215 /* Cast the value in the primary register to the operand size that is flagged
1216 * by the lhs value. Return the result value.
1219 unsigned ltype, rtype;
1221 /* Get the type spec from the flags */
1222 ltype = lhs & CF_TYPE;
1223 rtype = rhs & CF_TYPE;
1225 /* Check if a conversion is needed */
1226 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1227 /* We must promote the primary register to long */
1231 /* Do not need any other action. If the left type is int, and the primary
1232 * register is long, it will be automagically truncated. If the right hand
1233 * side is const, it is not located in the primary register and handled by
1234 * the expression parser code.
1237 /* Result is const if the right hand side was const */
1238 lhs |= (rhs & CF_CONST);
1240 /* The resulting type is that of the left hand side (that's why you called
1248 void g_scale (unsigned flags, long val)
1249 /* Scale the value in the primary register by the given value. If val is positive,
1250 * scale up, is val is negative, scale down. This function is used to scale
1251 * the operands or results of pointer arithmetic by the size of the type, the
1252 * pointer points to.
1257 /* Value may not be zero */
1259 Internal ("Data type has no size");
1260 } else if (val > 0) {
1263 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1265 /* Factor is 2, 4, 8 and 16, use special function */
1266 switch (flags & CF_TYPE) {
1269 if (flags & CF_FORCECHAR) {
1271 AddCodeLine ("asl a");
1278 if (CodeSizeFactor >= (p2+1)*130U) {
1279 AddCodeLine ("stx tmp1");
1281 AddCodeLine ("asl a");
1282 AddCodeLine ("rol tmp1");
1284 AddCodeLine ("ldx tmp1");
1286 if (flags & CF_UNSIGNED) {
1287 AddCodeLine ("jsr shlax%d", p2);
1289 AddCodeLine ("jsr aslax%d", p2);
1295 if (flags & CF_UNSIGNED) {
1296 AddCodeLine ("jsr shleax%d", p2);
1298 AddCodeLine ("jsr asleax%d", p2);
1307 } else if (val != 1) {
1309 /* Use a multiplication instead */
1310 g_mul (flags | CF_CONST, val);
1318 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1320 /* Factor is 2, 4, 8 and 16 use special function */
1321 switch (flags & CF_TYPE) {
1324 if (flags & CF_FORCECHAR) {
1325 if (flags & CF_UNSIGNED) {
1327 AddCodeLine ("lsr a");
1330 } else if (p2 <= 2) {
1331 AddCodeLine ("cmp #$80");
1332 AddCodeLine ("ror a");
1339 if (flags & CF_UNSIGNED) {
1340 if (CodeSizeFactor >= (p2+1)*130U) {
1341 AddCodeLine ("stx tmp1");
1343 AddCodeLine ("lsr tmp1");
1344 AddCodeLine ("ror a");
1346 AddCodeLine ("ldx tmp1");
1348 AddCodeLine ("jsr lsrax%d", p2);
1351 if (CodeSizeFactor >= (p2+1)*150U) {
1352 AddCodeLine ("stx tmp1");
1354 AddCodeLine ("cpx #$80");
1355 AddCodeLine ("ror tmp1");
1356 AddCodeLine ("ror a");
1358 AddCodeLine ("ldx tmp1");
1360 AddCodeLine ("jsr asrax%d", p2);
1366 if (flags & CF_UNSIGNED) {
1367 AddCodeLine ("jsr lsreax%d", p2);
1369 AddCodeLine ("jsr asreax%d", p2);
1378 } else if (val != 1) {
1380 /* Use a division instead */
1381 g_div (flags | CF_CONST, val);
1389 /*****************************************************************************/
1390 /* Adds and subs of variables fix a fixed address */
1391 /*****************************************************************************/
1395 void g_addlocal (unsigned flags, int offs)
1396 /* Add a local variable to ax */
1400 /* Correct the offset and check it */
1402 CheckLocalOffs (offs);
1404 switch (flags & CF_TYPE) {
1407 L = GetLocalLabel();
1408 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1409 AddCodeLine ("clc");
1410 AddCodeLine ("adc (sp),y");
1411 AddCodeLine ("bcc %s", LocalLabelName (L));
1412 AddCodeLine ("inx");
1417 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1418 AddCodeLine ("clc");
1419 AddCodeLine ("adc (sp),y");
1420 AddCodeLine ("pha");
1421 AddCodeLine ("txa");
1422 AddCodeLine ("iny");
1423 AddCodeLine ("adc (sp),y");
1424 AddCodeLine ("tax");
1425 AddCodeLine ("pla");
1429 /* Do it the old way */
1431 g_getlocal (flags, offs);
1443 void g_addstatic (unsigned flags, unsigned long label, long offs)
1444 /* Add a static variable to ax */
1448 /* Create the correct label name */
1449 const char* lbuf = GetLabelName (flags, label, offs);
1451 switch (flags & CF_TYPE) {
1454 L = GetLocalLabel();
1455 AddCodeLine ("clc");
1456 AddCodeLine ("adc %s", lbuf);
1457 AddCodeLine ("bcc %s", LocalLabelName (L));
1458 AddCodeLine ("inx");
1463 AddCodeLine ("clc");
1464 AddCodeLine ("adc %s", lbuf);
1465 AddCodeLine ("tay");
1466 AddCodeLine ("txa");
1467 AddCodeLine ("adc %s+1", lbuf);
1468 AddCodeLine ("tax");
1469 AddCodeLine ("tya");
1473 /* Do it the old way */
1475 g_getstatic (flags, label, offs);
1487 /*****************************************************************************/
1488 /* Special op= functions */
1489 /*****************************************************************************/
1493 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1495 /* Emit += for a static variable */
1497 /* Create the correct label name */
1498 const char* lbuf = GetLabelName (flags, label, offs);
1500 /* Check the size and determine operation */
1501 switch (flags & CF_TYPE) {
1504 if (flags & CF_FORCECHAR) {
1505 AddCodeLine ("ldx #$00");
1506 if (flags & CF_CONST) {
1508 AddCodeLine ("inc %s", lbuf);
1509 AddCodeLine ("lda %s", lbuf);
1511 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1512 AddCodeLine ("clc");
1513 AddCodeLine ("adc %s", lbuf);
1514 AddCodeLine ("sta %s", lbuf);
1517 AddCodeLine ("clc");
1518 AddCodeLine ("adc %s", lbuf);
1519 AddCodeLine ("sta %s", lbuf);
1521 if ((flags & CF_UNSIGNED) == 0) {
1522 unsigned L = GetLocalLabel();
1523 AddCodeLine ("bpl %s", LocalLabelName (L));
1524 AddCodeLine ("dex");
1532 if (flags & CF_CONST) {
1534 unsigned L = GetLocalLabel ();
1535 AddCodeLine ("inc %s", lbuf);
1536 AddCodeLine ("bne %s", LocalLabelName (L));
1537 AddCodeLine ("inc %s+1", lbuf);
1539 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1540 AddCodeLine ("ldx %s+1", lbuf);
1542 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1543 AddCodeLine ("clc");
1544 AddCodeLine ("adc %s", lbuf);
1545 AddCodeLine ("sta %s", lbuf);
1547 unsigned L = GetLocalLabel ();
1548 AddCodeLine ("bcc %s", LocalLabelName (L));
1549 AddCodeLine ("inc %s+1", lbuf);
1551 AddCodeLine ("ldx %s+1", lbuf);
1553 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1554 AddCodeLine ("adc %s+1", lbuf);
1555 AddCodeLine ("sta %s+1", lbuf);
1556 AddCodeLine ("tax");
1557 AddCodeLine ("lda %s", lbuf);
1561 AddCodeLine ("clc");
1562 AddCodeLine ("adc %s", lbuf);
1563 AddCodeLine ("sta %s", lbuf);
1564 AddCodeLine ("txa");
1565 AddCodeLine ("adc %s+1", lbuf);
1566 AddCodeLine ("sta %s+1", lbuf);
1567 AddCodeLine ("tax");
1568 AddCodeLine ("lda %s", lbuf);
1573 if (flags & CF_CONST) {
1575 AddCodeLine ("ldy #<(%s)", lbuf);
1576 AddCodeLine ("sty ptr1");
1577 AddCodeLine ("ldy #>(%s+1)", lbuf);
1579 AddCodeLine ("jsr laddeq1");
1581 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1582 AddCodeLine ("jsr laddeqa");
1585 g_getstatic (flags, label, offs);
1587 g_putstatic (flags, label, offs);
1590 AddCodeLine ("ldy #<(%s)", lbuf);
1591 AddCodeLine ("sty ptr1");
1592 AddCodeLine ("ldy #>(%s+1)", lbuf);
1593 AddCodeLine ("jsr laddeq");
1604 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1605 /* Emit += for a local variable */
1607 /* Calculate the true offset, check it, load it into Y */
1609 CheckLocalOffs (offs);
1611 /* Check the size and determine operation */
1612 switch (flags & CF_TYPE) {
1615 if (flags & CF_FORCECHAR) {
1617 AddCodeLine ("ldx #$00");
1618 if (flags & CF_CONST) {
1619 AddCodeLine ("clc");
1620 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1621 AddCodeLine ("adc (sp),y");
1622 AddCodeLine ("sta (sp),y");
1624 AddCodeLine ("clc");
1625 AddCodeLine ("adc (sp),y");
1626 AddCodeLine ("sta (sp),y");
1628 if ((flags & CF_UNSIGNED) == 0) {
1629 unsigned L = GetLocalLabel();
1630 AddCodeLine ("bpl %s", LocalLabelName (L));
1631 AddCodeLine ("dex");
1640 if (flags & CF_CONST) {
1641 if (CodeSizeFactor >= 400) {
1642 AddCodeLine ("clc");
1643 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1644 AddCodeLine ("adc (sp),y");
1645 AddCodeLine ("sta (sp),y");
1646 AddCodeLine ("iny");
1647 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1648 AddCodeLine ("adc (sp),y");
1649 AddCodeLine ("sta (sp),y");
1650 AddCodeLine ("tax");
1651 AddCodeLine ("dey");
1652 AddCodeLine ("lda (sp),y");
1654 g_getimmed (flags, val, 0);
1655 AddCodeLine ("jsr addeqysp");
1658 AddCodeLine ("jsr addeqysp");
1663 if (flags & CF_CONST) {
1664 g_getimmed (flags, val, 0);
1667 AddCodeLine ("jsr laddeqysp");
1677 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1678 /* Emit += for the location with address in ax */
1680 /* If the offset is too large for a byte register, add the high byte
1681 * of the offset to the primary. Beware: We need a special correction
1682 * if the offset in the low byte will overflow in the operation.
1684 offs = MakeByteOffs (flags, offs);
1686 /* Check the size and determine operation */
1687 switch (flags & CF_TYPE) {
1690 AddCodeLine ("sta ptr1");
1691 AddCodeLine ("stx ptr1+1");
1692 AddCodeLine ("ldy #$%02X", offs);
1693 AddCodeLine ("ldx #$00");
1694 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1695 AddCodeLine ("clc");
1696 AddCodeLine ("adc (ptr1),y");
1697 AddCodeLine ("sta (ptr1),y");
1701 if (CodeSizeFactor >= 200) {
1702 /* Lots of code, use only if size is not important */
1703 AddCodeLine ("sta ptr1");
1704 AddCodeLine ("stx ptr1+1");
1705 AddCodeLine ("ldy #$%02X", offs);
1706 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1707 AddCodeLine ("clc");
1708 AddCodeLine ("adc (ptr1),y");
1709 AddCodeLine ("sta (ptr1),y");
1710 AddCodeLine ("pha");
1711 AddCodeLine ("iny");
1712 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1713 AddCodeLine ("adc (ptr1),y");
1714 AddCodeLine ("sta (ptr1),y");
1715 AddCodeLine ("tax");
1716 AddCodeLine ("pla");
1722 AddCodeLine ("jsr pushax"); /* Push the address */
1723 push (CF_PTR); /* Correct the internal sp */
1724 g_getind (flags, offs); /* Fetch the value */
1725 g_inc (flags, val); /* Increment value in primary */
1726 g_putind (flags, offs); /* Store the value back */
1736 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1738 /* Emit -= for a static variable */
1740 /* Create the correct label name */
1741 const char* lbuf = GetLabelName (flags, label, offs);
1743 /* Check the size and determine operation */
1744 switch (flags & CF_TYPE) {
1747 if (flags & CF_FORCECHAR) {
1748 AddCodeLine ("ldx #$00");
1749 if (flags & CF_CONST) {
1751 AddCodeLine ("dec %s", lbuf);
1752 AddCodeLine ("lda %s", lbuf);
1754 AddCodeLine ("sec");
1755 AddCodeLine ("lda %s", lbuf);
1756 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1757 AddCodeLine ("sta %s", lbuf);
1760 AddCodeLine ("eor #$FF");
1761 AddCodeLine ("sec");
1762 AddCodeLine ("adc %s", lbuf);
1763 AddCodeLine ("sta %s", lbuf);
1765 if ((flags & CF_UNSIGNED) == 0) {
1766 unsigned L = GetLocalLabel();
1767 AddCodeLine ("bpl %s", LocalLabelName (L));
1768 AddCodeLine ("dex");
1776 AddCodeLine ("sec");
1777 if (flags & CF_CONST) {
1778 AddCodeLine ("lda %s", lbuf);
1779 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1780 AddCodeLine ("sta %s", lbuf);
1782 unsigned L = GetLocalLabel ();
1783 AddCodeLine ("bcs %s", LocalLabelName (L));
1784 AddCodeLine ("dec %s+1", lbuf);
1786 AddCodeLine ("ldx %s+1", lbuf);
1788 AddCodeLine ("lda %s+1", lbuf);
1789 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1790 AddCodeLine ("sta %s+1", lbuf);
1791 AddCodeLine ("tax");
1792 AddCodeLine ("lda %s", lbuf);
1795 AddCodeLine ("eor #$FF");
1796 AddCodeLine ("adc %s", lbuf);
1797 AddCodeLine ("sta %s", lbuf);
1798 AddCodeLine ("txa");
1799 AddCodeLine ("eor #$FF");
1800 AddCodeLine ("adc %s+1", lbuf);
1801 AddCodeLine ("sta %s+1", lbuf);
1802 AddCodeLine ("tax");
1803 AddCodeLine ("lda %s", lbuf);
1808 if (flags & CF_CONST) {
1810 AddCodeLine ("ldy #<(%s)", lbuf);
1811 AddCodeLine ("sty ptr1");
1812 AddCodeLine ("ldy #>(%s+1)", lbuf);
1813 AddCodeLine ("lda #$%02X", (unsigned char)val);
1814 AddCodeLine ("jsr lsubeqa");
1816 g_getstatic (flags, label, offs);
1818 g_putstatic (flags, label, offs);
1821 AddCodeLine ("ldy #<(%s)", lbuf);
1822 AddCodeLine ("sty ptr1");
1823 AddCodeLine ("ldy #>(%s+1)", lbuf);
1824 AddCodeLine ("jsr lsubeq");
1835 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1836 /* Emit -= for a local variable */
1838 /* Calculate the true offset, check it, load it into Y */
1840 CheckLocalOffs (offs);
1842 /* Check the size and determine operation */
1843 switch (flags & CF_TYPE) {
1846 if (flags & CF_FORCECHAR) {
1848 AddCodeLine ("ldx #$00");
1849 AddCodeLine ("sec");
1850 if (flags & CF_CONST) {
1851 AddCodeLine ("lda (sp),y");
1852 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1854 AddCodeLine ("eor #$FF");
1855 AddCodeLine ("adc (sp),y");
1857 AddCodeLine ("sta (sp),y");
1858 if ((flags & CF_UNSIGNED) == 0) {
1859 unsigned L = GetLocalLabel();
1860 AddCodeLine ("bpl %s", LocalLabelName (L));
1861 AddCodeLine ("dex");
1869 if (flags & CF_CONST) {
1870 g_getimmed (flags, val, 0);
1873 AddCodeLine ("jsr subeqysp");
1877 if (flags & CF_CONST) {
1878 g_getimmed (flags, val, 0);
1881 AddCodeLine ("jsr lsubeqysp");
1891 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
1892 /* Emit -= for the location with address in ax */
1894 /* If the offset is too large for a byte register, add the high byte
1895 * of the offset to the primary. Beware: We need a special correction
1896 * if the offset in the low byte will overflow in the operation.
1898 offs = MakeByteOffs (flags, offs);
1900 /* Check the size and determine operation */
1901 switch (flags & CF_TYPE) {
1904 AddCodeLine ("sta ptr1");
1905 AddCodeLine ("stx ptr1+1");
1906 AddCodeLine ("ldy #$%02X", offs);
1907 AddCodeLine ("ldx #$00");
1908 AddCodeLine ("lda (ptr1),y");
1909 AddCodeLine ("sec");
1910 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1911 AddCodeLine ("sta (ptr1),y");
1915 if (CodeSizeFactor >= 200) {
1916 /* Lots of code, use only if size is not important */
1917 AddCodeLine ("sta ptr1");
1918 AddCodeLine ("stx ptr1+1");
1919 AddCodeLine ("ldy #$%02X", offs);
1920 AddCodeLine ("lda (ptr1),y");
1921 AddCodeLine ("sec");
1922 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1923 AddCodeLine ("sta (ptr1),y");
1924 AddCodeLine ("pha");
1925 AddCodeLine ("iny");
1926 AddCodeLine ("lda (ptr1),y");
1927 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1928 AddCodeLine ("sta (ptr1),y");
1929 AddCodeLine ("tax");
1930 AddCodeLine ("pla");
1936 AddCodeLine ("jsr pushax"); /* Push the address */
1937 push (CF_PTR); /* Correct the internal sp */
1938 g_getind (flags, offs); /* Fetch the value */
1939 g_dec (flags, val); /* Increment value in primary */
1940 g_putind (flags, offs); /* Store the value back */
1950 /*****************************************************************************/
1951 /* Add a variable address to the value in ax */
1952 /*****************************************************************************/
1956 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
1957 /* Add the address of a local variable to ax */
1961 /* Add the offset */
1964 /* We cannot address more then 256 bytes of locals anyway */
1965 L = GetLocalLabel();
1966 CheckLocalOffs (offs);
1967 AddCodeLine ("clc");
1968 AddCodeLine ("adc #$%02X", offs & 0xFF);
1969 /* Do also skip the CLC insn below */
1970 AddCodeLine ("bcc %s", LocalLabelName (L));
1971 AddCodeLine ("inx");
1974 /* Add the current stackpointer value */
1975 AddCodeLine ("clc");
1977 /* Label was used above */
1980 AddCodeLine ("adc sp");
1981 AddCodeLine ("tay");
1982 AddCodeLine ("txa");
1983 AddCodeLine ("adc sp+1");
1984 AddCodeLine ("tax");
1985 AddCodeLine ("tya");
1990 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
1991 /* Add the address of a static variable to ax */
1993 /* Create the correct label name */
1994 const char* lbuf = GetLabelName (flags, label, offs);
1996 /* Add the address to the current ax value */
1997 AddCodeLine ("clc");
1998 AddCodeLine ("adc #<(%s)", lbuf);
1999 AddCodeLine ("tay");
2000 AddCodeLine ("txa");
2001 AddCodeLine ("adc #>(%s)", lbuf);
2002 AddCodeLine ("tax");
2003 AddCodeLine ("tya");
2008 /*****************************************************************************/
2010 /*****************************************************************************/
2014 void g_save (unsigned flags)
2015 /* Copy primary register to hold register. */
2017 /* Check the size and determine operation */
2018 switch (flags & CF_TYPE) {
2021 if (flags & CF_FORCECHAR) {
2022 AddCodeLine ("pha");
2028 AddCodeLine ("sta regsave");
2029 AddCodeLine ("stx regsave+1");
2033 AddCodeLine ("jsr saveeax");
2043 void g_restore (unsigned flags)
2044 /* Copy hold register to primary. */
2046 /* Check the size and determine operation */
2047 switch (flags & CF_TYPE) {
2050 if (flags & CF_FORCECHAR) {
2051 AddCodeLine ("pla");
2057 AddCodeLine ("lda regsave");
2058 AddCodeLine ("ldx regsave+1");
2062 AddCodeLine ("jsr resteax");
2072 void g_cmp (unsigned flags, unsigned long val)
2073 /* Immidiate compare. The primary register will not be changed, Z flag
2079 /* Check the size and determine operation */
2080 switch (flags & CF_TYPE) {
2083 if (flags & CF_FORCECHAR) {
2084 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2090 L = GetLocalLabel();
2091 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2092 AddCodeLine ("bne %s", LocalLabelName (L));
2093 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2098 Internal ("g_cmp: Long compares not implemented");
2108 static void oper (unsigned flags, unsigned long val, char** subs)
2109 /* Encode a binary operation. subs is a pointer to four groups of three
2111 * 0-2 --> Operate on ints
2112 * 3-5 --> Operate on unsigneds
2113 * 6-8 --> Operate on longs
2114 * 9-11 --> Operate on unsigned longs
2116 * The first subroutine names in each string group is used to encode an
2117 * operation with a zero constant, the second to encode an operation with
2118 * a 8 bit constant, and the third is used in all other cases.
2123 /* Determine the offset into the array */
2124 offs = (flags & CF_UNSIGNED)? 3 : 0;
2125 switch (flags & CF_TYPE) {
2138 /* Encode the operation */
2139 if (flags & CF_CONST) {
2140 /* Constant value given */
2141 if (val == 0 && subs [offs+0]) {
2142 /* Special case: constant with value zero */
2143 AddCodeLine ("jsr %s", subs [offs+0]);
2144 } else if (val < 0x100 && subs [offs+1]) {
2145 /* Special case: constant with high byte zero */
2146 ldaconst (val); /* Load low byte */
2147 AddCodeLine ("jsr %s", subs [offs+1]);
2149 /* Others: arbitrary constant value */
2150 g_getimmed (flags, val, 0); /* Load value */
2151 AddCodeLine ("jsr %s", subs [offs+2]);
2154 /* Value not constant (is already in (e)ax) */
2155 AddCodeLine ("jsr %s", subs [offs+2]);
2158 /* The operation will pop it's argument */
2164 void g_test (unsigned flags)
2165 /* Test the value in the primary and set the condition codes */
2167 switch (flags & CF_TYPE) {
2170 if (flags & CF_FORCECHAR) {
2171 AddCodeLine ("tax");
2177 AddCodeLine ("stx tmp1");
2178 AddCodeLine ("ora tmp1");
2182 if (flags & CF_UNSIGNED) {
2183 AddCodeLine ("jsr utsteax");
2185 AddCodeLine ("jsr tsteax");
2197 void g_push (unsigned flags, unsigned long val)
2198 /* Push the primary register or a constant value onto the stack */
2200 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2202 /* We have a constant 8 or 16 bit value */
2203 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2205 /* Handle as 8 bit value */
2207 AddCodeLine ("jsr pusha");
2211 /* Handle as 16 bit value */
2212 g_getimmed (flags, val, 0);
2213 AddCodeLine ("jsr pushax");
2218 /* Value is not 16 bit or not constant */
2219 if (flags & CF_CONST) {
2220 /* Constant 32 bit value, load into eax */
2221 g_getimmed (flags, val, 0);
2224 /* Push the primary register */
2225 switch (flags & CF_TYPE) {
2228 if (flags & CF_FORCECHAR) {
2229 /* Handle as char */
2230 AddCodeLine ("jsr pusha");
2235 AddCodeLine ("jsr pushax");
2239 AddCodeLine ("jsr pusheax");
2249 /* Adjust the stack offset */
2255 void g_swap (unsigned flags)
2256 /* Swap the primary register and the top of the stack. flags give the type
2257 * of *both* values (must have same size).
2260 switch (flags & CF_TYPE) {
2264 AddCodeLine ("jsr swapstk");
2268 AddCodeLine ("jsr swapestk");
2279 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2280 /* Call the specified subroutine name */
2282 if ((Flags & CF_FIXARGC) == 0) {
2283 /* Pass the argument count */
2286 AddCodeLine ("jsr _%s", Label);
2287 oursp += ArgSize; /* callee pops args */
2292 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2293 /* Call subroutine indirect */
2295 if ((Flags & CF_LOCAL) == 0) {
2296 /* Address is in a/x */
2297 if ((Flags & CF_FIXARGC) == 0) {
2298 /* Pass arg count */
2301 AddCodeLine ("jsr callax");
2303 /* The address is on stack, offset is on Val */
2305 CheckLocalOffs (Offs);
2306 AddCodeLine ("pha");
2307 AddCodeLine ("ldy #$%02X", Offs);
2308 AddCodeLine ("lda (sp),y");
2309 AddCodeLine ("sta jmpvec+1");
2310 AddCodeLine ("iny");
2311 AddCodeLine ("lda (sp),y");
2312 AddCodeLine ("sta jmpvec+2");
2313 AddCodeLine ("pla");
2314 AddCodeLine ("jsr jmpvec");
2317 /* Callee pops args */
2323 void g_jump (unsigned Label)
2324 /* Jump to specified internal label number */
2326 AddCodeLine ("jmp %s", LocalLabelName (Label));
2331 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2332 /* Jump to label if zero flag clear */
2334 AddCodeLine ("jne %s", LocalLabelName (label));
2339 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2340 /* Jump to label if zero flag set */
2342 AddCodeLine ("jeq %s", LocalLabelName (label));
2347 static void mod_internal (int k, char* verb1, char* verb2)
2350 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2354 AddCodeLine ("jsr %ssp", verb2);
2360 void g_space (int space)
2361 /* Create or drop space on the stack */
2364 mod_internal (-space, "inc", "addy");
2365 } else if (space > 0) {
2366 mod_internal (space, "dec", "suby");
2372 void g_cstackcheck (void)
2373 /* Check for a C stack overflow */
2375 AddCodeLine ("jsr cstkchk");
2380 void g_stackcheck (void)
2381 /* Check for a stack overflow */
2383 AddCodeLine ("jsr stkchk");
2388 void g_add (unsigned flags, unsigned long val)
2389 /* Primary = TOS + Primary */
2391 static char* ops [12] = {
2392 0, "tosadda0", "tosaddax",
2393 0, "tosadda0", "tosaddax",
2398 if (flags & CF_CONST) {
2399 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2400 g_push (flags & ~CF_CONST, 0);
2402 oper (flags, val, ops);
2407 void g_sub (unsigned flags, unsigned long val)
2408 /* Primary = TOS - Primary */
2410 static char* ops [12] = {
2411 0, "tossuba0", "tossubax",
2412 0, "tossuba0", "tossubax",
2417 if (flags & CF_CONST) {
2418 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2419 g_push (flags & ~CF_CONST, 0);
2421 oper (flags, val, ops);
2426 void g_rsub (unsigned flags, unsigned long val)
2427 /* Primary = Primary - TOS */
2429 static char* ops [12] = {
2430 0, "tosrsuba0", "tosrsubax",
2431 0, "tosrsuba0", "tosrsubax",
2435 oper (flags, val, ops);
2440 void g_mul (unsigned flags, unsigned long val)
2441 /* Primary = TOS * Primary */
2443 static char* ops [12] = {
2444 0, "tosmula0", "tosmulax",
2445 0, "tosumula0", "tosumulax",
2452 /* Do strength reduction if the value is constant and a power of two */
2453 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2454 /* Generate a shift instead */
2459 /* If the right hand side is const, the lhs is not on stack but still
2460 * in the primary register.
2462 if (flags & CF_CONST) {
2464 switch (flags & CF_TYPE) {
2467 if (flags & CF_FORCECHAR) {
2468 /* Handle some special cases */
2472 AddCodeLine ("sta tmp1");
2473 AddCodeLine ("asl a");
2474 AddCodeLine ("clc");
2475 AddCodeLine ("adc tmp1");
2479 AddCodeLine ("sta tmp1");
2480 AddCodeLine ("asl a");
2481 AddCodeLine ("asl a");
2482 AddCodeLine ("clc");
2483 AddCodeLine ("adc tmp1");
2487 AddCodeLine ("sta tmp1");
2488 AddCodeLine ("asl a");
2489 AddCodeLine ("clc");
2490 AddCodeLine ("adc tmp1");
2491 AddCodeLine ("asl a");
2495 AddCodeLine ("sta tmp1");
2496 AddCodeLine ("asl a");
2497 AddCodeLine ("asl a");
2498 AddCodeLine ("clc");
2499 AddCodeLine ("adc tmp1");
2500 AddCodeLine ("asl a");
2509 AddCodeLine ("jsr mulax3");
2512 AddCodeLine ("jsr mulax5");
2515 AddCodeLine ("jsr mulax6");
2518 AddCodeLine ("jsr mulax7");
2521 AddCodeLine ("jsr mulax9");
2524 AddCodeLine ("jsr mulax10");
2536 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2537 * into the normal, non-optimized stuff.
2539 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2540 g_push (flags & ~CF_CONST, 0);
2544 /* Use long way over the stack */
2545 oper (flags, val, ops);
2550 void g_div (unsigned flags, unsigned long val)
2551 /* Primary = TOS / Primary */
2553 static char* ops [12] = {
2554 0, "tosdiva0", "tosdivax",
2555 0, "tosudiva0", "tosudivax",
2560 /* Do strength reduction if the value is constant and a power of two */
2562 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2563 /* Generate a shift instead */
2566 /* Generate a division */
2567 if (flags & CF_CONST) {
2568 /* lhs is not on stack */
2569 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2570 g_push (flags & ~CF_CONST, 0);
2572 oper (flags, val, ops);
2578 void g_mod (unsigned flags, unsigned long val)
2579 /* Primary = TOS % Primary */
2581 static char* ops [12] = {
2582 0, "tosmoda0", "tosmodax",
2583 0, "tosumoda0", "tosumodax",
2589 /* Check if we can do some cost reduction */
2590 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2591 /* We can do that with an AND operation */
2592 g_and (flags, val - 1);
2594 /* Do it the hard way... */
2595 if (flags & CF_CONST) {
2596 /* lhs is not on stack */
2597 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2598 g_push (flags & ~CF_CONST, 0);
2600 oper (flags, val, ops);
2606 void g_or (unsigned flags, unsigned long val)
2607 /* Primary = TOS | Primary */
2609 static char* ops [12] = {
2610 0, "tosora0", "tosorax",
2611 0, "tosora0", "tosorax",
2616 /* If the right hand side is const, the lhs is not on stack but still
2617 * in the primary register.
2619 if (flags & CF_CONST) {
2621 switch (flags & CF_TYPE) {
2624 if (flags & CF_FORCECHAR) {
2625 if ((val & 0xFF) != 0xFF) {
2626 AddCodeLine ("ora #$%02X", (unsigned char)val);
2634 AddCodeLine ("ora #$%02X", (unsigned char)val);
2641 AddCodeLine ("ora #$%02X", (unsigned char)val);
2650 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2651 * into the normal, non-optimized stuff.
2653 g_push (flags & ~CF_CONST, 0);
2657 /* Use long way over the stack */
2658 oper (flags, val, ops);
2663 void g_xor (unsigned flags, unsigned long val)
2664 /* Primary = TOS ^ Primary */
2666 static char* ops [12] = {
2667 0, "tosxora0", "tosxorax",
2668 0, "tosxora0", "tosxorax",
2674 /* If the right hand side is const, the lhs is not on stack but still
2675 * in the primary register.
2677 if (flags & CF_CONST) {
2679 switch (flags & CF_TYPE) {
2682 if (flags & CF_FORCECHAR) {
2683 if ((val & 0xFF) != 0) {
2684 AddCodeLine ("eor #$%02X", (unsigned char)val);
2693 AddCodeLine ("eor #$%02X", (unsigned char)val);
2696 } else if ((val & 0xFF) == 0) {
2697 AddCodeLine ("pha");
2698 AddCodeLine ("txa");
2699 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2700 AddCodeLine ("tax");
2701 AddCodeLine ("pla");
2709 AddCodeLine ("eor #$%02X", (unsigned char)val);
2719 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2720 * into the normal, non-optimized stuff.
2722 g_push (flags & ~CF_CONST, 0);
2726 /* Use long way over the stack */
2727 oper (flags, val, ops);
2732 void g_and (unsigned flags, unsigned long val)
2733 /* Primary = TOS & Primary */
2735 static char* ops [12] = {
2736 0, "tosanda0", "tosandax",
2737 0, "tosanda0", "tosandax",
2742 /* If the right hand side is const, the lhs is not on stack but still
2743 * in the primary register.
2745 if (flags & CF_CONST) {
2747 switch (flags & CF_TYPE) {
2750 if (flags & CF_FORCECHAR) {
2751 AddCodeLine ("and #$%02X", (unsigned char)val);
2756 if ((val & 0xFFFF) != 0xFFFF) {
2761 } else if (val != 0xFF) {
2762 AddCodeLine ("and #$%02X", (unsigned char)val);
2764 } else if ((val & 0xFF00) == 0xFF00) {
2765 AddCodeLine ("and #$%02X", (unsigned char)val);
2766 } else if ((val & 0x00FF) == 0x0000) {
2767 AddCodeLine ("txa");
2768 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2769 AddCodeLine ("tax");
2772 AddCodeLine ("tay");
2773 AddCodeLine ("txa");
2774 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2775 AddCodeLine ("tax");
2776 AddCodeLine ("tya");
2777 if ((val & 0x00FF) != 0x00FF) {
2778 AddCodeLine ("and #$%02X", (unsigned char)val);
2787 AddCodeLine ("stx sreg+1");
2788 AddCodeLine ("stx sreg");
2789 if ((val & 0xFF) != 0xFF) {
2790 AddCodeLine ("and #$%02X", (unsigned char)val);
2793 } else if (val == 0xFF00) {
2795 AddCodeLine ("sta sreg+1");
2796 AddCodeLine ("sta sreg");
2805 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2806 * into the normal, non-optimized stuff.
2808 g_push (flags & ~CF_CONST, 0);
2812 /* Use long way over the stack */
2813 oper (flags, val, ops);
2818 void g_asr (unsigned flags, unsigned long val)
2819 /* Primary = TOS >> Primary */
2821 static char* ops [12] = {
2822 0, "tosasra0", "tosasrax",
2823 0, "tosshra0", "tosshrax",
2828 /* If the right hand side is const, the lhs is not on stack but still
2829 * in the primary register.
2831 if (flags & CF_CONST) {
2833 switch (flags & CF_TYPE) {
2837 if (val >= 8 && (flags & CF_UNSIGNED)) {
2838 AddCodeLine ("txa");
2845 } else if (val >= 1 && val <= 4) {
2846 if (flags & CF_UNSIGNED) {
2847 AddCodeLine ("jsr shrax%ld", val);
2849 AddCodeLine ("jsr asrax%ld", val);
2859 } else if (val >= 1 && val <= 4) {
2860 if (flags & CF_UNSIGNED) {
2861 AddCodeLine ("jsr shreax%ld", val);
2863 AddCodeLine ("jsr asreax%ld", val);
2866 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2867 AddCodeLine ("txa");
2868 AddCodeLine ("ldx sreg");
2869 AddCodeLine ("ldy sreg+1");
2870 AddCodeLine ("sty sreg");
2871 AddCodeLine ("ldy #$00");
2872 AddCodeLine ("sty sreg+1");
2874 } else if (val == 16) {
2875 AddCodeLine ("ldy #$00");
2876 AddCodeLine ("ldx sreg+1");
2877 if ((flags & CF_UNSIGNED) == 0) {
2878 unsigned L = GetLocalLabel();
2879 AddCodeLine ("bpl %s", LocalLabelName (L));
2880 AddCodeLine ("dey");
2883 AddCodeLine ("lda sreg");
2884 AddCodeLine ("sty sreg+1");
2885 AddCodeLine ("sty sreg");
2894 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2895 * into the normal, non-optimized stuff.
2897 g_push (flags & ~CF_CONST, 0);
2901 /* Use long way over the stack */
2902 oper (flags, val, ops);
2907 void g_asl (unsigned flags, unsigned long val)
2908 /* Primary = TOS << Primary */
2910 static char* ops [12] = {
2911 0, "tosasla0", "tosaslax",
2912 0, "tosshla0", "tosshlax",
2918 /* If the right hand side is const, the lhs is not on stack but still
2919 * in the primary register.
2921 if (flags & CF_CONST) {
2923 switch (flags & CF_TYPE) {
2928 AddCodeLine ("tax");
2929 AddCodeLine ("lda #$00");
2935 } else if (val >= 1 && val <= 4) {
2936 if (flags & CF_UNSIGNED) {
2937 AddCodeLine ("jsr shlax%ld", val);
2939 AddCodeLine ("jsr aslax%ld", val);
2949 } else if (val >= 1 && val <= 4) {
2950 if (flags & CF_UNSIGNED) {
2951 AddCodeLine ("jsr shleax%ld", val);
2953 AddCodeLine ("jsr asleax%ld", val);
2956 } else if (val == 8) {
2957 AddCodeLine ("ldy sreg");
2958 AddCodeLine ("sty sreg+1");
2959 AddCodeLine ("stx sreg");
2960 AddCodeLine ("tax");
2961 AddCodeLine ("lda #$00");
2963 } else if (val == 16) {
2964 AddCodeLine ("stx sreg+1");
2965 AddCodeLine ("sta sreg");
2966 AddCodeLine ("lda #$00");
2967 AddCodeLine ("tax");
2976 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2977 * into the normal, non-optimized stuff.
2979 g_push (flags & ~CF_CONST, 0);
2983 /* Use long way over the stack */
2984 oper (flags, val, ops);
2989 void g_neg (unsigned flags)
2990 /* Primary = -Primary */
2992 switch (flags & CF_TYPE) {
2996 AddCodeLine ("jsr negax");
3000 AddCodeLine ("jsr negeax");
3010 void g_bneg (unsigned flags)
3011 /* Primary = !Primary */
3013 switch (flags & CF_TYPE) {
3016 AddCodeLine ("jsr bnega");
3020 AddCodeLine ("jsr bnegax");
3024 AddCodeLine ("jsr bnegeax");
3034 void g_com (unsigned flags)
3035 /* Primary = ~Primary */
3037 switch (flags & CF_TYPE) {
3041 AddCodeLine ("jsr complax");
3045 AddCodeLine ("jsr compleax");
3055 void g_inc (unsigned flags, unsigned long val)
3056 /* Increment the primary register by a given number */
3058 /* Don't inc by zero */
3063 /* Generate code for the supported types */
3065 switch (flags & CF_TYPE) {
3068 if (flags & CF_FORCECHAR) {
3069 if (CPU == CPU_65C02 && val <= 2) {
3071 AddCodeLine ("ina");
3074 AddCodeLine ("clc");
3075 AddCodeLine ("adc #$%02X", (unsigned char)val);
3082 if (CPU == CPU_65C02 && val == 1) {
3083 unsigned L = GetLocalLabel();
3084 AddCodeLine ("ina");
3085 AddCodeLine ("bne %s", LocalLabelName (L));
3086 AddCodeLine ("inx");
3088 } else if (CodeSizeFactor < 200) {
3091 AddCodeLine ("jsr incax%lu", val);
3092 } else if (val <= 255) {
3094 AddCodeLine ("jsr incaxy");
3096 g_add (flags | CF_CONST, val);
3099 /* Inline the code */
3101 if ((val & 0xFF) != 0) {
3102 unsigned L = GetLocalLabel();
3103 AddCodeLine ("clc");
3104 AddCodeLine ("adc #$%02X", (unsigned char) val);
3105 AddCodeLine ("bcc %s", LocalLabelName (L));
3106 AddCodeLine ("inx");
3110 AddCodeLine ("inx");
3113 AddCodeLine ("inx");
3116 AddCodeLine ("inx");
3119 AddCodeLine ("clc");
3120 if ((val & 0xFF) != 0) {
3121 AddCodeLine ("adc #$%02X", (unsigned char) val);
3123 AddCodeLine ("pha");
3124 AddCodeLine ("txa");
3125 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3126 AddCodeLine ("tax");
3127 AddCodeLine ("pla");
3135 AddCodeLine ("jsr inceaxy");
3137 g_add (flags | CF_CONST, val);
3149 void g_dec (unsigned flags, unsigned long val)
3150 /* Decrement the primary register by a given number */
3152 /* Don't dec by zero */
3157 /* Generate code for the supported types */
3159 switch (flags & CF_TYPE) {
3162 if (flags & CF_FORCECHAR) {
3163 if (CPU == CPU_65C02 && val <= 2) {
3165 AddCodeLine ("dea");
3168 AddCodeLine ("sec");
3169 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3176 if (CodeSizeFactor < 200) {
3177 /* Use subroutines */
3179 AddCodeLine ("jsr decax%d", (int) val);
3180 } else if (val <= 255) {
3182 AddCodeLine ("jsr decaxy");
3184 g_sub (flags | CF_CONST, val);
3187 /* Inline the code */
3189 if ((val & 0xFF) != 0) {
3190 unsigned L = GetLocalLabel();
3191 AddCodeLine ("sec");
3192 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3193 AddCodeLine ("bcs %s", LocalLabelName (L));
3194 AddCodeLine ("dex");
3198 AddCodeLine ("dex");
3201 AddCodeLine ("dex");
3204 AddCodeLine ("sec");
3205 if ((val & 0xFF) != 0) {
3206 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3208 AddCodeLine ("pha");
3209 AddCodeLine ("txa");
3210 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3211 AddCodeLine ("tax");
3212 AddCodeLine ("pla");
3220 AddCodeLine ("jsr deceaxy");
3222 g_sub (flags | CF_CONST, val);
3235 * Following are the conditional operators. They compare the TOS against
3236 * the primary and put a literal 1 in the primary if the condition is
3237 * true, otherwise they clear the primary register
3242 void g_eq (unsigned flags, unsigned long val)
3243 /* Test for equal */
3245 static char* ops [12] = {
3246 "toseq00", "toseqa0", "toseqax",
3247 "toseq00", "toseqa0", "toseqax",
3254 /* If the right hand side is const, the lhs is not on stack but still
3255 * in the primary register.
3257 if (flags & CF_CONST) {
3259 switch (flags & CF_TYPE) {
3262 if (flags & CF_FORCECHAR) {
3263 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3264 AddCodeLine ("jsr booleq");
3270 L = GetLocalLabel();
3271 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3272 AddCodeLine ("bne %s", LocalLabelName (L));
3273 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3275 AddCodeLine ("jsr booleq");
3285 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3286 * into the normal, non-optimized stuff.
3288 g_push (flags & ~CF_CONST, 0);
3292 /* Use long way over the stack */
3293 oper (flags, val, ops);
3298 void g_ne (unsigned flags, unsigned long val)
3299 /* Test for not equal */
3301 static char* ops [12] = {
3302 "tosne00", "tosnea0", "tosneax",
3303 "tosne00", "tosnea0", "tosneax",
3310 /* If the right hand side is const, the lhs is not on stack but still
3311 * in the primary register.
3313 if (flags & CF_CONST) {
3315 switch (flags & CF_TYPE) {
3318 if (flags & CF_FORCECHAR) {
3319 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3320 AddCodeLine ("jsr boolne");
3326 L = GetLocalLabel();
3327 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3328 AddCodeLine ("bne %s", LocalLabelName (L));
3329 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3331 AddCodeLine ("jsr boolne");
3341 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3342 * into the normal, non-optimized stuff.
3344 g_push (flags & ~CF_CONST, 0);
3348 /* Use long way over the stack */
3349 oper (flags, val, ops);
3354 void g_lt (unsigned flags, unsigned long val)
3355 /* Test for less than */
3357 static char* ops [12] = {
3358 "toslt00", "toslta0", "tosltax",
3359 "tosult00", "tosulta0", "tosultax",
3364 /* If the right hand side is const, the lhs is not on stack but still
3365 * in the primary register.
3367 if (flags & CF_CONST) {
3369 /* Give a warning in some special cases */
3370 if ((flags & CF_UNSIGNED) && val == 0) {
3371 Warning ("Condition is never true");
3372 AddCodeLine ("jsr return0");
3376 /* Look at the type */
3377 switch (flags & CF_TYPE) {
3380 if (flags & CF_FORCECHAR) {
3381 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3382 if (flags & CF_UNSIGNED) {
3383 AddCodeLine ("jsr boolult");
3385 AddCodeLine ("jsr boollt");
3392 if (flags & CF_UNSIGNED) {
3393 /* Unsigned compare */
3394 /* If the low byte is zero, we must only test the high byte */
3395 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3396 if ((val & 0xFF) != 0) {
3397 unsigned L = GetLocalLabel();
3398 AddCodeLine ("bne %s", LocalLabelName (L));
3399 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3402 AddCodeLine ("jsr boolult");
3404 /* Signed compare */
3405 if ((val & 0xFF) == 0) {
3406 /* Low byte is zero, just look at the high byte */
3407 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3409 /* Subtract the two values */
3410 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3411 AddCodeLine ("txa");
3412 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3414 AddCodeLine ("jsr boollt");
3419 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3420 /* If we have a signed compare against zero, we only need to
3421 * test the high byte.
3423 AddCodeLine ("lda sreg+1");
3425 /* Do a subtraction */
3426 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3427 AddCodeLine ("txa");
3428 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3429 AddCodeLine ("lda sreg");
3430 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3431 AddCodeLine ("lda sreg+1");
3432 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3434 /* Emit the proper makebool routine */
3435 if (flags & CF_UNSIGNED) {
3436 /* Unsigned compare */
3437 AddCodeLine ("jsr boolult");
3439 /* Signed compare */
3440 AddCodeLine ("jsr boollt");
3448 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3449 * into the normal, non-optimized stuff.
3451 g_push (flags & ~CF_CONST, 0);
3455 /* Use long way over the stack */
3456 oper (flags, val, ops);
3461 void g_le (unsigned flags, unsigned long val)
3462 /* Test for less than or equal to */
3464 static char* ops [12] = {
3465 "tosle00", "toslea0", "tosleax",
3466 "tosule00", "tosulea0", "tosuleax",
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 /* Look at the type */
3478 switch (flags & CF_TYPE) {
3481 if (flags & CF_FORCECHAR) {
3482 if (flags & CF_UNSIGNED) {
3483 /* Unsigned compare */
3485 /* Use < instead of <= because the former gives
3486 * better code on the 6502 than the latter.
3488 g_lt (flags, val+1);
3491 Warning ("Condition is always true");
3492 AddCodeLine ("jsr return1");
3495 /* Signed compare */
3496 if ((long) val < 0x7F) {
3497 /* Use < instead of <= because the former gives
3498 * better code on the 6502 than the latter.
3500 g_lt (flags, val+1);
3503 Warning ("Condition is always true");
3504 AddCodeLine ("jsr return1");
3512 if (flags & CF_UNSIGNED) {
3513 /* Unsigned compare */
3515 /* Use < instead of <= because the former gives
3516 * better code on the 6502 than the latter.
3518 g_lt (flags, val+1);
3521 Warning ("Condition is always true");
3522 AddCodeLine ("jsr return1");
3525 /* Signed compare */
3526 if ((long) val < 0x7FFF) {
3527 g_lt (flags, val+1);
3530 Warning ("Condition is always true");
3531 AddCodeLine ("jsr return1");
3537 if (flags & CF_UNSIGNED) {
3538 /* Unsigned compare */
3539 if (val < 0xFFFFFFFF) {
3540 /* Use < instead of <= because the former gives
3541 * better code on the 6502 than the latter.
3543 g_lt (flags, val+1);
3546 Warning ("Condition is always true");
3547 AddCodeLine ("jsr return1");
3550 /* Signed compare */
3551 if ((long) val < 0x7FFFFFFF) {
3552 g_lt (flags, val+1);
3555 Warning ("Condition is always true");
3556 AddCodeLine ("jsr return1");
3565 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3566 * into the normal, non-optimized stuff.
3568 g_push (flags & ~CF_CONST, 0);
3572 /* Use long way over the stack */
3573 oper (flags, val, ops);
3578 void g_gt (unsigned flags, unsigned long val)
3579 /* Test for greater than */
3581 static char* ops [12] = {
3582 "tosgt00", "tosgta0", "tosgtax",
3583 "tosugt00", "tosugta0", "tosugtax",
3589 /* If the right hand side is const, the lhs is not on stack but still
3590 * in the primary register.
3592 if (flags & CF_CONST) {
3594 /* Look at the type */
3595 switch (flags & CF_TYPE) {
3598 if (flags & CF_FORCECHAR) {
3599 if (flags & CF_UNSIGNED) {
3601 /* If we have a compare > 0, we will replace it by
3602 * != 0 here, since both are identical but the
3603 * latter is easier to optimize.
3606 } else if (val < 0xFF) {
3607 /* Use >= instead of > because the former gives
3608 * better code on the 6502 than the latter.
3610 g_ge (flags, val+1);
3613 Warning ("Condition is never true");
3614 AddCodeLine ("jsr return0");
3617 if ((long) val < 0x7F) {
3618 /* Use >= instead of > because the former gives
3619 * better code on the 6502 than the latter.
3621 g_ge (flags, val+1);
3624 Warning ("Condition is never true");
3625 AddCodeLine ("jsr return0");
3633 if (flags & CF_UNSIGNED) {
3634 /* Unsigned compare */
3636 /* If we have a compare > 0, we will replace it by
3637 * != 0 here, since both are identical but the latter
3638 * is easier to optimize.
3641 } else if (val < 0xFFFF) {
3642 /* Use >= instead of > because the former gives better
3643 * code on the 6502 than the latter.
3645 g_ge (flags, val+1);
3648 Warning ("Condition is never true");
3649 AddCodeLine ("jsr return0");
3652 /* Signed compare */
3653 if ((long) val < 0x7FFF) {
3654 g_ge (flags, val+1);
3657 Warning ("Condition is never true");
3658 AddCodeLine ("jsr return0");
3664 if (flags & CF_UNSIGNED) {
3665 /* Unsigned compare */
3667 /* If we have a compare > 0, we will replace it by
3668 * != 0 here, since both are identical but the latter
3669 * is easier to optimize.
3672 } else if (val < 0xFFFFFFFF) {
3673 /* Use >= instead of > because the former gives better
3674 * code on the 6502 than the latter.
3676 g_ge (flags, val+1);
3679 Warning ("Condition is never true");
3680 AddCodeLine ("jsr return0");
3683 /* Signed compare */
3684 if ((long) val < 0x7FFFFFFF) {
3685 g_ge (flags, val+1);
3688 Warning ("Condition is never true");
3689 AddCodeLine ("jsr return0");
3698 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3699 * into the normal, non-optimized stuff.
3701 g_push (flags & ~CF_CONST, 0);
3705 /* Use long way over the stack */
3706 oper (flags, val, ops);
3711 void g_ge (unsigned flags, unsigned long val)
3712 /* Test for greater than or equal to */
3714 static char* ops [12] = {
3715 "tosge00", "tosgea0", "tosgeax",
3716 "tosuge00", "tosugea0", "tosugeax",
3722 /* If the right hand side is const, the lhs is not on stack but still
3723 * in the primary register.
3725 if (flags & CF_CONST) {
3727 /* Give a warning in some special cases */
3728 if ((flags & CF_UNSIGNED) && val == 0) {
3729 Warning ("Condition is always true");
3730 AddCodeLine ("jsr return1");
3734 /* Look at the type */
3735 switch (flags & CF_TYPE) {
3738 if (flags & CF_FORCECHAR) {
3739 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3740 if (flags & CF_UNSIGNED) {
3741 AddCodeLine ("jsr booluge");
3743 AddCodeLine ("jsr boolge");
3750 if (flags & CF_UNSIGNED) {
3751 /* Unsigned compare */
3752 /* If the low byte is zero, we must only test the high byte */
3753 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3754 if ((val & 0xFF) != 0) {
3755 unsigned L = GetLocalLabel();
3756 AddCodeLine ("bne %s", LocalLabelName (L));
3757 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3760 AddCodeLine ("jsr booluge");
3762 /* Signed compare */
3763 if ((val & 0xFF) == 0) {
3764 /* Low byte is zero, just look at the high byte */
3765 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3767 /* Subtract the two values */
3768 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3769 AddCodeLine ("txa");
3770 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3772 AddCodeLine ("jsr boolge");
3777 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3778 /* If we have a signed compare against zero, we only need to
3779 * test the high byte.
3781 AddCodeLine ("lda sreg+1");
3783 /* Do a subtraction */
3784 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3785 AddCodeLine ("txa");
3786 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3787 AddCodeLine ("lda sreg");
3788 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3789 AddCodeLine ("lda sreg+1");
3790 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3792 /* Emit the proper makebool routine */
3793 if (flags & CF_UNSIGNED) {
3794 /* Unsigned compare */
3795 AddCodeLine ("jsr booluge");
3797 /* Signed compare */
3798 AddCodeLine ("jsr boolge");
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 /*****************************************************************************/
3820 /* Allocating static storage */
3821 /*****************************************************************************/
3825 void g_res (unsigned n)
3826 /* Reserve static storage, n bytes */
3828 AddDataLine ("\t.res\t%u,$00", n);
3833 void g_defdata (unsigned flags, unsigned long val, long offs)
3834 /* Define data with the size given in flags */
3836 if (flags & CF_CONST) {
3838 /* Numeric constant */
3839 switch (flags & CF_TYPE) {
3842 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
3846 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
3850 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3861 /* Create the correct label name */
3862 const char* Label = GetLabelName (flags, val, offs);
3864 /* Labels are always 16 bit */
3865 AddDataLine ("\t.addr\t%s", Label);
3872 void g_defbytes (const void* Bytes, unsigned Count)
3873 /* Output a row of bytes as a constant */
3879 /* Cast the buffer pointer */
3880 const unsigned char* Data = (const unsigned char*) Bytes;
3882 /* Output the stuff */
3885 /* How many go into this line? */
3886 if ((Chunk = Count) > 16) {
3891 /* Output one line */
3892 strcpy (Buf, "\t.byte\t");
3895 B += sprintf (B, "$%02X", *Data++);
3901 /* Output the line */
3908 void g_zerobytes (unsigned n)
3909 /* Output n bytes of data initialized with zero */
3911 AddDataLine ("\t.res\t%u,$00", n);
3916 /*****************************************************************************/
3917 /* Switch statement */
3918 /*****************************************************************************/
3922 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
3923 /* Generate code for a switch statement */
3925 unsigned NextLabel = 0;
3928 /* Setup registers and determine which compare insn to use */
3929 const char* Compare;
3932 Compare = "cmp #$%02X";
3935 Compare = "cpx #$%02X";
3938 AddCodeLine ("ldy sreg");
3939 Compare = "cpy #$%02X";
3942 AddCodeLine ("ldy sreg+1");
3943 Compare = "cpy #$%02X";
3946 Internal ("Invalid depth in g_switch: %u", Depth);
3949 /* Walk over all nodes */
3950 for (I = 0; I < CollCount (Nodes); ++I) {
3952 /* Get the next case node */
3953 CaseNode* N = CollAtUnchecked (Nodes, I);
3955 /* If we have a next label, define it */
3957 g_defcodelabel (NextLabel);
3961 /* Do the compare */
3962 AddCodeLine (Compare, CN_GetValue (N));
3964 /* If this is the last level, jump directly to the case code if found */
3967 /* Branch if equal */
3968 g_falsejump (0, CN_GetLabel (N));
3972 /* Determine the next label */
3973 if (I == CollCount (Nodes) - 1) {
3974 /* Last node means not found */
3975 g_truejump (0, DefaultLabel);
3977 /* Jump to the next check */
3978 NextLabel = GetLocalLabel ();
3979 g_truejump (0, NextLabel);
3982 /* Check the next level */
3983 g_switch (N->Nodes, DefaultLabel, Depth-1);
3988 /* If we go here, we haven't found the label */
3989 g_jump (DefaultLabel);
3994 /*****************************************************************************/
3995 /* User supplied assembler code */
3996 /*****************************************************************************/
4000 void g_asmcode (struct StrBuf* B)
4001 /* Output one line of assembler code. */
4003 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));
4008 /*****************************************************************************/
4009 /* Inlined known functions */
4010 /*****************************************************************************/
4014 void g_strlen (unsigned flags, unsigned long val, long offs)
4015 /* Inline the strlen() function */
4017 /* We need a label in both cases */
4018 unsigned label = GetLocalLabel ();
4020 /* Two different encodings */
4021 if (flags & CF_CONST) {
4023 /* The address of the string is constant. Create the correct label name */
4024 const char* lbuf = GetLabelName (flags, val, offs);
4026 /* Generate the strlen code */
4027 AddCodeLine ("ldy #$FF");
4028 g_defcodelabel (label);
4029 AddCodeLine ("iny");
4030 AddCodeLine ("lda %s,y", lbuf);
4031 AddCodeLine ("bne %s", LocalLabelName (label));
4032 AddCodeLine ("tax");
4033 AddCodeLine ("tya");
4037 /* Address not constant but in primary */
4038 if (CodeSizeFactor < 400) {
4039 /* This is too much code, so call strlen instead of inlining */
4040 AddCodeLine ("jsr _strlen");
4042 /* Inline the function */
4043 AddCodeLine ("sta ptr1");
4044 AddCodeLine ("stx ptr1+1");
4045 AddCodeLine ("ldy #$FF");
4046 g_defcodelabel (label);
4047 AddCodeLine ("iny");
4048 AddCodeLine ("lda (ptr1),y");
4049 AddCodeLine ("bne %s", LocalLabelName (label));
4050 AddCodeLine ("tax");
4051 AddCodeLine ("tya");