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_regint (unsigned Flags)
1135 /* Make sure, the value in the primary register an int. Convert if necessary */
1139 switch (Flags & CF_TYPE) {
1142 if (Flags & CF_FORCECHAR) {
1143 /* Conversion is from char */
1144 if (Flags & CF_UNSIGNED) {
1145 AddCodeLine ("ldx #$00");
1147 L = GetLocalLabel();
1148 AddCodeLine ("ldx #$00");
1149 AddCodeLine ("cmp #$80");
1150 AddCodeLine ("bcc %s", LocalLabelName (L));
1151 AddCodeLine ("dex");
1168 void g_reglong (unsigned Flags)
1169 /* Make sure, the value in the primary register a long. Convert if necessary */
1173 switch (Flags & CF_TYPE) {
1176 if (Flags & CF_FORCECHAR) {
1177 /* Conversion is from char */
1178 if (Flags & CF_UNSIGNED) {
1179 if (CodeSizeFactor >= 200) {
1180 AddCodeLine ("ldx #$00");
1181 AddCodeLine ("stx sreg");
1182 AddCodeLine ("stx sreg+1");
1184 AddCodeLine ("jsr aulong");
1187 if (CodeSizeFactor >= 366) {
1188 L = GetLocalLabel();
1189 AddCodeLine ("ldx #$00");
1190 AddCodeLine ("cmp #$80");
1191 AddCodeLine ("bcc %s", LocalLabelName (L));
1192 AddCodeLine ("dex");
1194 AddCodeLine ("stx sreg");
1195 AddCodeLine ("stx sreg+1");
1197 AddCodeLine ("jsr along");
1204 if (Flags & CF_UNSIGNED) {
1205 if (CodeSizeFactor >= 200) {
1207 AddCodeLine ("sty sreg");
1208 AddCodeLine ("sty sreg+1");
1210 AddCodeLine ("jsr axulong");
1213 AddCodeLine ("jsr axlong");
1227 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1228 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1229 * value, that corresponds to the value on TOS, rhs corresponds to the value
1230 * in (e)ax. The return value is the the flags value for the resulting type.
1233 unsigned ltype, rtype;
1236 /* Get the type spec from the flags */
1237 ltype = lhs & CF_TYPE;
1238 rtype = rhs & CF_TYPE;
1240 /* Check if a conversion is needed */
1241 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1242 /* We must promote the primary register to long */
1244 /* Get the new rhs type */
1245 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1247 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1248 /* We must promote the lhs to long */
1254 /* Get the new rhs type */
1255 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1259 /* Determine the result type for the operation:
1260 * - The result is const if both operands are const.
1261 * - The result is unsigned if one of the operands is unsigned.
1262 * - The result is long if one of the operands is long.
1263 * - Otherwise the result is int sized.
1265 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1266 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1267 if (rtype == CF_LONG || ltype == CF_LONG) {
1277 unsigned g_typecast (unsigned lhs, unsigned rhs)
1278 /* Cast the value in the primary register to the operand size that is flagged
1279 * by the lhs value. Return the result value.
1282 unsigned ltype, rtype;
1284 /* Get the type spec from the flags */
1285 ltype = lhs & CF_TYPE;
1286 rtype = rhs & CF_TYPE;
1288 /* Check if a conversion is needed */
1289 if ((rhs & CF_CONST) == 0) {
1290 if (ltype == CF_LONG && rtype != CF_LONG) {
1291 /* We must promote the primary register to long */
1293 } else if (ltype == CF_INT && rtype != CF_INT) {
1294 /* We must promote the primary register to int */
1299 /* Do not need any other action. If the left type is int, and the primary
1300 * register is long, it will be automagically truncated. If the right hand
1301 * side is const, it is not located in the primary register and handled by
1302 * the expression parser code.
1305 /* Result is const if the right hand side was const */
1306 lhs |= (rhs & CF_CONST);
1308 /* The resulting type is that of the left hand side (that's why you called
1316 void g_scale (unsigned flags, long val)
1317 /* Scale the value in the primary register by the given value. If val is positive,
1318 * scale up, is val is negative, scale down. This function is used to scale
1319 * the operands or results of pointer arithmetic by the size of the type, the
1320 * pointer points to.
1325 /* Value may not be zero */
1327 Internal ("Data type has no size");
1328 } else if (val > 0) {
1331 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1333 /* Factor is 2, 4, 8 and 16, use special function */
1334 switch (flags & CF_TYPE) {
1337 if (flags & CF_FORCECHAR) {
1339 AddCodeLine ("asl a");
1346 if (CodeSizeFactor >= (p2+1)*130U) {
1347 AddCodeLine ("stx tmp1");
1349 AddCodeLine ("asl a");
1350 AddCodeLine ("rol tmp1");
1352 AddCodeLine ("ldx tmp1");
1354 if (flags & CF_UNSIGNED) {
1355 AddCodeLine ("jsr shlax%d", p2);
1357 AddCodeLine ("jsr aslax%d", p2);
1363 if (flags & CF_UNSIGNED) {
1364 AddCodeLine ("jsr shleax%d", p2);
1366 AddCodeLine ("jsr asleax%d", p2);
1375 } else if (val != 1) {
1377 /* Use a multiplication instead */
1378 g_mul (flags | CF_CONST, val);
1386 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1388 /* Factor is 2, 4, 8 and 16 use special function */
1389 switch (flags & CF_TYPE) {
1392 if (flags & CF_FORCECHAR) {
1393 if (flags & CF_UNSIGNED) {
1395 AddCodeLine ("lsr a");
1398 } else if (p2 <= 2) {
1399 AddCodeLine ("cmp #$80");
1400 AddCodeLine ("ror a");
1407 if (flags & CF_UNSIGNED) {
1408 if (CodeSizeFactor >= (p2+1)*130U) {
1409 AddCodeLine ("stx tmp1");
1411 AddCodeLine ("lsr tmp1");
1412 AddCodeLine ("ror a");
1414 AddCodeLine ("ldx tmp1");
1416 AddCodeLine ("jsr lsrax%d", p2);
1419 if (CodeSizeFactor >= (p2+1)*150U) {
1420 AddCodeLine ("stx tmp1");
1422 AddCodeLine ("cpx #$80");
1423 AddCodeLine ("ror tmp1");
1424 AddCodeLine ("ror a");
1426 AddCodeLine ("ldx tmp1");
1428 AddCodeLine ("jsr asrax%d", p2);
1434 if (flags & CF_UNSIGNED) {
1435 AddCodeLine ("jsr lsreax%d", p2);
1437 AddCodeLine ("jsr asreax%d", p2);
1446 } else if (val != 1) {
1448 /* Use a division instead */
1449 g_div (flags | CF_CONST, val);
1457 /*****************************************************************************/
1458 /* Adds and subs of variables fix a fixed address */
1459 /*****************************************************************************/
1463 void g_addlocal (unsigned flags, int offs)
1464 /* Add a local variable to ax */
1468 /* Correct the offset and check it */
1470 CheckLocalOffs (offs);
1472 switch (flags & CF_TYPE) {
1475 L = GetLocalLabel();
1476 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1477 AddCodeLine ("clc");
1478 AddCodeLine ("adc (sp),y");
1479 AddCodeLine ("bcc %s", LocalLabelName (L));
1480 AddCodeLine ("inx");
1485 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1486 AddCodeLine ("clc");
1487 AddCodeLine ("adc (sp),y");
1488 AddCodeLine ("pha");
1489 AddCodeLine ("txa");
1490 AddCodeLine ("iny");
1491 AddCodeLine ("adc (sp),y");
1492 AddCodeLine ("tax");
1493 AddCodeLine ("pla");
1497 /* Do it the old way */
1499 g_getlocal (flags, offs);
1511 void g_addstatic (unsigned flags, unsigned long label, long offs)
1512 /* Add a static variable to ax */
1516 /* Create the correct label name */
1517 const char* lbuf = GetLabelName (flags, label, offs);
1519 switch (flags & CF_TYPE) {
1522 L = GetLocalLabel();
1523 AddCodeLine ("clc");
1524 AddCodeLine ("adc %s", lbuf);
1525 AddCodeLine ("bcc %s", LocalLabelName (L));
1526 AddCodeLine ("inx");
1531 AddCodeLine ("clc");
1532 AddCodeLine ("adc %s", lbuf);
1533 AddCodeLine ("tay");
1534 AddCodeLine ("txa");
1535 AddCodeLine ("adc %s+1", lbuf);
1536 AddCodeLine ("tax");
1537 AddCodeLine ("tya");
1541 /* Do it the old way */
1543 g_getstatic (flags, label, offs);
1555 /*****************************************************************************/
1556 /* Special op= functions */
1557 /*****************************************************************************/
1561 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1563 /* Emit += for a static variable */
1565 /* Create the correct label name */
1566 const char* lbuf = GetLabelName (flags, label, offs);
1568 /* Check the size and determine operation */
1569 switch (flags & CF_TYPE) {
1572 if (flags & CF_FORCECHAR) {
1573 AddCodeLine ("ldx #$00");
1574 if (flags & CF_CONST) {
1576 AddCodeLine ("inc %s", lbuf);
1577 AddCodeLine ("lda %s", lbuf);
1579 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1580 AddCodeLine ("clc");
1581 AddCodeLine ("adc %s", lbuf);
1582 AddCodeLine ("sta %s", lbuf);
1585 AddCodeLine ("clc");
1586 AddCodeLine ("adc %s", lbuf);
1587 AddCodeLine ("sta %s", lbuf);
1589 if ((flags & CF_UNSIGNED) == 0) {
1590 unsigned L = GetLocalLabel();
1591 AddCodeLine ("bpl %s", LocalLabelName (L));
1592 AddCodeLine ("dex");
1600 if (flags & CF_CONST) {
1602 unsigned L = GetLocalLabel ();
1603 AddCodeLine ("inc %s", lbuf);
1604 AddCodeLine ("bne %s", LocalLabelName (L));
1605 AddCodeLine ("inc %s+1", lbuf);
1607 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1608 AddCodeLine ("ldx %s+1", lbuf);
1610 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1611 AddCodeLine ("clc");
1612 AddCodeLine ("adc %s", lbuf);
1613 AddCodeLine ("sta %s", lbuf);
1615 unsigned L = GetLocalLabel ();
1616 AddCodeLine ("bcc %s", LocalLabelName (L));
1617 AddCodeLine ("inc %s+1", lbuf);
1619 AddCodeLine ("ldx %s+1", lbuf);
1621 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1622 AddCodeLine ("adc %s+1", lbuf);
1623 AddCodeLine ("sta %s+1", lbuf);
1624 AddCodeLine ("tax");
1625 AddCodeLine ("lda %s", lbuf);
1629 AddCodeLine ("clc");
1630 AddCodeLine ("adc %s", lbuf);
1631 AddCodeLine ("sta %s", lbuf);
1632 AddCodeLine ("txa");
1633 AddCodeLine ("adc %s+1", lbuf);
1634 AddCodeLine ("sta %s+1", lbuf);
1635 AddCodeLine ("tax");
1636 AddCodeLine ("lda %s", lbuf);
1641 if (flags & CF_CONST) {
1643 AddCodeLine ("ldy #<(%s)", lbuf);
1644 AddCodeLine ("sty ptr1");
1645 AddCodeLine ("ldy #>(%s)", lbuf);
1647 AddCodeLine ("jsr laddeq1");
1649 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1650 AddCodeLine ("jsr laddeqa");
1653 g_getstatic (flags, label, offs);
1655 g_putstatic (flags, label, offs);
1658 AddCodeLine ("ldy #<(%s)", lbuf);
1659 AddCodeLine ("sty ptr1");
1660 AddCodeLine ("ldy #>(%s)", lbuf);
1661 AddCodeLine ("jsr laddeq");
1672 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1673 /* Emit += for a local variable */
1675 /* Calculate the true offset, check it, load it into Y */
1677 CheckLocalOffs (offs);
1679 /* Check the size and determine operation */
1680 switch (flags & CF_TYPE) {
1683 if (flags & CF_FORCECHAR) {
1685 AddCodeLine ("ldx #$00");
1686 if (flags & CF_CONST) {
1687 AddCodeLine ("clc");
1688 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1689 AddCodeLine ("adc (sp),y");
1690 AddCodeLine ("sta (sp),y");
1692 AddCodeLine ("clc");
1693 AddCodeLine ("adc (sp),y");
1694 AddCodeLine ("sta (sp),y");
1696 if ((flags & CF_UNSIGNED) == 0) {
1697 unsigned L = GetLocalLabel();
1698 AddCodeLine ("bpl %s", LocalLabelName (L));
1699 AddCodeLine ("dex");
1708 if (flags & CF_CONST) {
1709 if (CodeSizeFactor >= 400) {
1710 AddCodeLine ("clc");
1711 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1712 AddCodeLine ("adc (sp),y");
1713 AddCodeLine ("sta (sp),y");
1714 AddCodeLine ("iny");
1715 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1716 AddCodeLine ("adc (sp),y");
1717 AddCodeLine ("sta (sp),y");
1718 AddCodeLine ("tax");
1719 AddCodeLine ("dey");
1720 AddCodeLine ("lda (sp),y");
1722 g_getimmed (flags, val, 0);
1723 AddCodeLine ("jsr addeqysp");
1726 AddCodeLine ("jsr addeqysp");
1731 if (flags & CF_CONST) {
1732 g_getimmed (flags, val, 0);
1735 AddCodeLine ("jsr laddeqysp");
1745 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1746 /* Emit += for the location with address in ax */
1748 /* If the offset is too large for a byte register, add the high byte
1749 * of the offset to the primary. Beware: We need a special correction
1750 * if the offset in the low byte will overflow in the operation.
1752 offs = MakeByteOffs (flags, offs);
1754 /* Check the size and determine operation */
1755 switch (flags & CF_TYPE) {
1758 AddCodeLine ("sta ptr1");
1759 AddCodeLine ("stx ptr1+1");
1760 AddCodeLine ("ldy #$%02X", offs);
1761 AddCodeLine ("ldx #$00");
1762 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1763 AddCodeLine ("clc");
1764 AddCodeLine ("adc (ptr1),y");
1765 AddCodeLine ("sta (ptr1),y");
1769 if (CodeSizeFactor >= 200) {
1770 /* Lots of code, use only if size is not important */
1771 AddCodeLine ("sta ptr1");
1772 AddCodeLine ("stx ptr1+1");
1773 AddCodeLine ("ldy #$%02X", offs);
1774 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1775 AddCodeLine ("clc");
1776 AddCodeLine ("adc (ptr1),y");
1777 AddCodeLine ("sta (ptr1),y");
1778 AddCodeLine ("pha");
1779 AddCodeLine ("iny");
1780 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1781 AddCodeLine ("adc (ptr1),y");
1782 AddCodeLine ("sta (ptr1),y");
1783 AddCodeLine ("tax");
1784 AddCodeLine ("pla");
1790 AddCodeLine ("jsr pushax"); /* Push the address */
1791 push (CF_PTR); /* Correct the internal sp */
1792 g_getind (flags, offs); /* Fetch the value */
1793 g_inc (flags, val); /* Increment value in primary */
1794 g_putind (flags, offs); /* Store the value back */
1804 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1806 /* Emit -= for a static variable */
1808 /* Create the correct label name */
1809 const char* lbuf = GetLabelName (flags, label, offs);
1811 /* Check the size and determine operation */
1812 switch (flags & CF_TYPE) {
1815 if (flags & CF_FORCECHAR) {
1816 AddCodeLine ("ldx #$00");
1817 if (flags & CF_CONST) {
1819 AddCodeLine ("dec %s", lbuf);
1820 AddCodeLine ("lda %s", lbuf);
1822 AddCodeLine ("sec");
1823 AddCodeLine ("lda %s", lbuf);
1824 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1825 AddCodeLine ("sta %s", lbuf);
1828 AddCodeLine ("eor #$FF");
1829 AddCodeLine ("sec");
1830 AddCodeLine ("adc %s", lbuf);
1831 AddCodeLine ("sta %s", lbuf);
1833 if ((flags & CF_UNSIGNED) == 0) {
1834 unsigned L = GetLocalLabel();
1835 AddCodeLine ("bpl %s", LocalLabelName (L));
1836 AddCodeLine ("dex");
1844 AddCodeLine ("sec");
1845 if (flags & CF_CONST) {
1846 AddCodeLine ("lda %s", lbuf);
1847 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1848 AddCodeLine ("sta %s", lbuf);
1850 unsigned L = GetLocalLabel ();
1851 AddCodeLine ("bcs %s", LocalLabelName (L));
1852 AddCodeLine ("dec %s+1", lbuf);
1854 AddCodeLine ("ldx %s+1", lbuf);
1856 AddCodeLine ("lda %s+1", lbuf);
1857 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1858 AddCodeLine ("sta %s+1", lbuf);
1859 AddCodeLine ("tax");
1860 AddCodeLine ("lda %s", lbuf);
1863 AddCodeLine ("eor #$FF");
1864 AddCodeLine ("adc %s", lbuf);
1865 AddCodeLine ("sta %s", lbuf);
1866 AddCodeLine ("txa");
1867 AddCodeLine ("eor #$FF");
1868 AddCodeLine ("adc %s+1", lbuf);
1869 AddCodeLine ("sta %s+1", lbuf);
1870 AddCodeLine ("tax");
1871 AddCodeLine ("lda %s", lbuf);
1876 if (flags & CF_CONST) {
1878 AddCodeLine ("ldy #<(%s)", lbuf);
1879 AddCodeLine ("sty ptr1");
1880 AddCodeLine ("ldy #>(%s)", lbuf);
1881 AddCodeLine ("lda #$%02X", (unsigned char)val);
1882 AddCodeLine ("jsr lsubeqa");
1884 g_getstatic (flags, label, offs);
1886 g_putstatic (flags, label, offs);
1889 AddCodeLine ("ldy #<(%s)", lbuf);
1890 AddCodeLine ("sty ptr1");
1891 AddCodeLine ("ldy #>(%s)", lbuf);
1892 AddCodeLine ("jsr lsubeq");
1903 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1904 /* Emit -= for a local variable */
1906 /* Calculate the true offset, check it, load it into Y */
1908 CheckLocalOffs (offs);
1910 /* Check the size and determine operation */
1911 switch (flags & CF_TYPE) {
1914 if (flags & CF_FORCECHAR) {
1916 AddCodeLine ("ldx #$00");
1917 AddCodeLine ("sec");
1918 if (flags & CF_CONST) {
1919 AddCodeLine ("lda (sp),y");
1920 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1922 AddCodeLine ("eor #$FF");
1923 AddCodeLine ("adc (sp),y");
1925 AddCodeLine ("sta (sp),y");
1926 if ((flags & CF_UNSIGNED) == 0) {
1927 unsigned L = GetLocalLabel();
1928 AddCodeLine ("bpl %s", LocalLabelName (L));
1929 AddCodeLine ("dex");
1937 if (flags & CF_CONST) {
1938 g_getimmed (flags, val, 0);
1941 AddCodeLine ("jsr subeqysp");
1945 if (flags & CF_CONST) {
1946 g_getimmed (flags, val, 0);
1949 AddCodeLine ("jsr lsubeqysp");
1959 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
1960 /* Emit -= for the location with address in ax */
1962 /* If the offset is too large for a byte register, add the high byte
1963 * of the offset to the primary. Beware: We need a special correction
1964 * if the offset in the low byte will overflow in the operation.
1966 offs = MakeByteOffs (flags, offs);
1968 /* Check the size and determine operation */
1969 switch (flags & CF_TYPE) {
1972 AddCodeLine ("sta ptr1");
1973 AddCodeLine ("stx ptr1+1");
1974 AddCodeLine ("ldy #$%02X", offs);
1975 AddCodeLine ("ldx #$00");
1976 AddCodeLine ("lda (ptr1),y");
1977 AddCodeLine ("sec");
1978 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1979 AddCodeLine ("sta (ptr1),y");
1983 if (CodeSizeFactor >= 200) {
1984 /* Lots of code, use only if size is not important */
1985 AddCodeLine ("sta ptr1");
1986 AddCodeLine ("stx ptr1+1");
1987 AddCodeLine ("ldy #$%02X", offs);
1988 AddCodeLine ("lda (ptr1),y");
1989 AddCodeLine ("sec");
1990 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1991 AddCodeLine ("sta (ptr1),y");
1992 AddCodeLine ("pha");
1993 AddCodeLine ("iny");
1994 AddCodeLine ("lda (ptr1),y");
1995 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1996 AddCodeLine ("sta (ptr1),y");
1997 AddCodeLine ("tax");
1998 AddCodeLine ("pla");
2004 AddCodeLine ("jsr pushax"); /* Push the address */
2005 push (CF_PTR); /* Correct the internal sp */
2006 g_getind (flags, offs); /* Fetch the value */
2007 g_dec (flags, val); /* Increment value in primary */
2008 g_putind (flags, offs); /* Store the value back */
2018 /*****************************************************************************/
2019 /* Add a variable address to the value in ax */
2020 /*****************************************************************************/
2024 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
2025 /* Add the address of a local variable to ax */
2029 /* Add the offset */
2032 /* We cannot address more then 256 bytes of locals anyway */
2033 L = GetLocalLabel();
2034 CheckLocalOffs (offs);
2035 AddCodeLine ("clc");
2036 AddCodeLine ("adc #$%02X", offs & 0xFF);
2037 /* Do also skip the CLC insn below */
2038 AddCodeLine ("bcc %s", LocalLabelName (L));
2039 AddCodeLine ("inx");
2042 /* Add the current stackpointer value */
2043 AddCodeLine ("clc");
2045 /* Label was used above */
2048 AddCodeLine ("adc sp");
2049 AddCodeLine ("tay");
2050 AddCodeLine ("txa");
2051 AddCodeLine ("adc sp+1");
2052 AddCodeLine ("tax");
2053 AddCodeLine ("tya");
2058 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
2059 /* Add the address of a static variable to ax */
2061 /* Create the correct label name */
2062 const char* lbuf = GetLabelName (flags, label, offs);
2064 /* Add the address to the current ax value */
2065 AddCodeLine ("clc");
2066 AddCodeLine ("adc #<(%s)", lbuf);
2067 AddCodeLine ("tay");
2068 AddCodeLine ("txa");
2069 AddCodeLine ("adc #>(%s)", lbuf);
2070 AddCodeLine ("tax");
2071 AddCodeLine ("tya");
2076 /*****************************************************************************/
2078 /*****************************************************************************/
2082 void g_save (unsigned flags)
2083 /* Copy primary register to hold register. */
2085 /* Check the size and determine operation */
2086 switch (flags & CF_TYPE) {
2089 if (flags & CF_FORCECHAR) {
2090 AddCodeLine ("pha");
2096 AddCodeLine ("sta regsave");
2097 AddCodeLine ("stx regsave+1");
2101 AddCodeLine ("jsr saveeax");
2111 void g_restore (unsigned flags)
2112 /* Copy hold register to primary. */
2114 /* Check the size and determine operation */
2115 switch (flags & CF_TYPE) {
2118 if (flags & CF_FORCECHAR) {
2119 AddCodeLine ("pla");
2125 AddCodeLine ("lda regsave");
2126 AddCodeLine ("ldx regsave+1");
2130 AddCodeLine ("jsr resteax");
2140 void g_cmp (unsigned flags, unsigned long val)
2141 /* Immidiate compare. The primary register will not be changed, Z flag
2147 /* Check the size and determine operation */
2148 switch (flags & CF_TYPE) {
2151 if (flags & CF_FORCECHAR) {
2152 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2158 L = GetLocalLabel();
2159 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2160 AddCodeLine ("bne %s", LocalLabelName (L));
2161 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2166 Internal ("g_cmp: Long compares not implemented");
2176 static void oper (unsigned flags, unsigned long val, char** subs)
2177 /* Encode a binary operation. subs is a pointer to four groups of three
2179 * 0-2 --> Operate on ints
2180 * 3-5 --> Operate on unsigneds
2181 * 6-8 --> Operate on longs
2182 * 9-11 --> Operate on unsigned longs
2184 * The first subroutine names in each string group is used to encode an
2185 * operation with a zero constant, the second to encode an operation with
2186 * a 8 bit constant, and the third is used in all other cases.
2191 /* Determine the offset into the array */
2192 offs = (flags & CF_UNSIGNED)? 3 : 0;
2193 switch (flags & CF_TYPE) {
2206 /* Encode the operation */
2207 if (flags & CF_CONST) {
2208 /* Constant value given */
2209 if (val == 0 && subs [offs+0]) {
2210 /* Special case: constant with value zero */
2211 AddCodeLine ("jsr %s", subs [offs+0]);
2212 } else if (val < 0x100 && subs [offs+1]) {
2213 /* Special case: constant with high byte zero */
2214 ldaconst (val); /* Load low byte */
2215 AddCodeLine ("jsr %s", subs [offs+1]);
2217 /* Others: arbitrary constant value */
2218 g_getimmed (flags, val, 0); /* Load value */
2219 AddCodeLine ("jsr %s", subs [offs+2]);
2222 /* Value not constant (is already in (e)ax) */
2223 AddCodeLine ("jsr %s", subs [offs+2]);
2226 /* The operation will pop it's argument */
2232 void g_test (unsigned flags)
2233 /* Test the value in the primary and set the condition codes */
2235 switch (flags & CF_TYPE) {
2238 if (flags & CF_FORCECHAR) {
2239 AddCodeLine ("tax");
2245 AddCodeLine ("stx tmp1");
2246 AddCodeLine ("ora tmp1");
2250 if (flags & CF_UNSIGNED) {
2251 AddCodeLine ("jsr utsteax");
2253 AddCodeLine ("jsr tsteax");
2265 void g_push (unsigned flags, unsigned long val)
2266 /* Push the primary register or a constant value onto the stack */
2268 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2270 /* We have a constant 8 or 16 bit value */
2271 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2273 /* Handle as 8 bit value */
2275 AddCodeLine ("jsr pusha");
2279 /* Handle as 16 bit value */
2280 g_getimmed (flags, val, 0);
2281 AddCodeLine ("jsr pushax");
2286 /* Value is not 16 bit or not constant */
2287 if (flags & CF_CONST) {
2288 /* Constant 32 bit value, load into eax */
2289 g_getimmed (flags, val, 0);
2292 /* Push the primary register */
2293 switch (flags & CF_TYPE) {
2296 if (flags & CF_FORCECHAR) {
2297 /* Handle as char */
2298 AddCodeLine ("jsr pusha");
2303 AddCodeLine ("jsr pushax");
2307 AddCodeLine ("jsr pusheax");
2317 /* Adjust the stack offset */
2323 void g_swap (unsigned flags)
2324 /* Swap the primary register and the top of the stack. flags give the type
2325 * of *both* values (must have same size).
2328 switch (flags & CF_TYPE) {
2332 AddCodeLine ("jsr swapstk");
2336 AddCodeLine ("jsr swapestk");
2347 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2348 /* Call the specified subroutine name */
2350 if ((Flags & CF_FIXARGC) == 0) {
2351 /* Pass the argument count */
2354 AddCodeLine ("jsr _%s", Label);
2355 oursp += ArgSize; /* callee pops args */
2360 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2361 /* Call subroutine indirect */
2363 if ((Flags & CF_LOCAL) == 0) {
2364 /* Address is in a/x */
2365 if ((Flags & CF_FIXARGC) == 0) {
2366 /* Pass arg count */
2369 AddCodeLine ("jsr callax");
2371 /* The address is on stack, offset is on Val */
2373 CheckLocalOffs (Offs);
2374 AddCodeLine ("pha");
2375 AddCodeLine ("ldy #$%02X", Offs);
2376 AddCodeLine ("lda (sp),y");
2377 AddCodeLine ("sta jmpvec+1");
2378 AddCodeLine ("iny");
2379 AddCodeLine ("lda (sp),y");
2380 AddCodeLine ("sta jmpvec+2");
2381 AddCodeLine ("pla");
2382 AddCodeLine ("jsr jmpvec");
2385 /* Callee pops args */
2391 void g_jump (unsigned Label)
2392 /* Jump to specified internal label number */
2394 AddCodeLine ("jmp %s", LocalLabelName (Label));
2399 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2400 /* Jump to label if zero flag clear */
2402 AddCodeLine ("jne %s", LocalLabelName (label));
2407 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2408 /* Jump to label if zero flag set */
2410 AddCodeLine ("jeq %s", LocalLabelName (label));
2415 static void mod_internal (int k, char* verb1, char* verb2)
2418 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2422 AddCodeLine ("jsr %ssp", verb2);
2428 void g_space (int space)
2429 /* Create or drop space on the stack */
2432 mod_internal (-space, "inc", "addy");
2433 } else if (space > 0) {
2434 mod_internal (space, "dec", "suby");
2440 void g_cstackcheck (void)
2441 /* Check for a C stack overflow */
2443 AddCodeLine ("jsr cstkchk");
2448 void g_stackcheck (void)
2449 /* Check for a stack overflow */
2451 AddCodeLine ("jsr stkchk");
2456 void g_add (unsigned flags, unsigned long val)
2457 /* Primary = TOS + Primary */
2459 static char* ops [12] = {
2460 0, "tosadda0", "tosaddax",
2461 0, "tosadda0", "tosaddax",
2466 if (flags & CF_CONST) {
2467 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2468 g_push (flags & ~CF_CONST, 0);
2470 oper (flags, val, ops);
2475 void g_sub (unsigned flags, unsigned long val)
2476 /* Primary = TOS - Primary */
2478 static char* ops [12] = {
2479 0, "tossuba0", "tossubax",
2480 0, "tossuba0", "tossubax",
2485 if (flags & CF_CONST) {
2486 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2487 g_push (flags & ~CF_CONST, 0);
2489 oper (flags, val, ops);
2494 void g_rsub (unsigned flags, unsigned long val)
2495 /* Primary = Primary - TOS */
2497 static char* ops [12] = {
2498 0, "tosrsuba0", "tosrsubax",
2499 0, "tosrsuba0", "tosrsubax",
2503 oper (flags, val, ops);
2508 void g_mul (unsigned flags, unsigned long val)
2509 /* Primary = TOS * Primary */
2511 static char* ops [12] = {
2512 0, "tosmula0", "tosmulax",
2513 0, "tosumula0", "tosumulax",
2520 /* Do strength reduction if the value is constant and a power of two */
2521 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2522 /* Generate a shift instead */
2527 /* If the right hand side is const, the lhs is not on stack but still
2528 * in the primary register.
2530 if (flags & CF_CONST) {
2532 switch (flags & CF_TYPE) {
2535 if (flags & CF_FORCECHAR) {
2536 /* Handle some special cases */
2540 AddCodeLine ("sta tmp1");
2541 AddCodeLine ("asl a");
2542 AddCodeLine ("clc");
2543 AddCodeLine ("adc tmp1");
2547 AddCodeLine ("sta tmp1");
2548 AddCodeLine ("asl a");
2549 AddCodeLine ("asl a");
2550 AddCodeLine ("clc");
2551 AddCodeLine ("adc tmp1");
2555 AddCodeLine ("sta tmp1");
2556 AddCodeLine ("asl a");
2557 AddCodeLine ("clc");
2558 AddCodeLine ("adc tmp1");
2559 AddCodeLine ("asl a");
2563 AddCodeLine ("sta tmp1");
2564 AddCodeLine ("asl a");
2565 AddCodeLine ("asl a");
2566 AddCodeLine ("clc");
2567 AddCodeLine ("adc tmp1");
2568 AddCodeLine ("asl a");
2577 AddCodeLine ("jsr mulax3");
2580 AddCodeLine ("jsr mulax5");
2583 AddCodeLine ("jsr mulax6");
2586 AddCodeLine ("jsr mulax7");
2589 AddCodeLine ("jsr mulax9");
2592 AddCodeLine ("jsr mulax10");
2604 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2605 * into the normal, non-optimized stuff.
2607 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2608 g_push (flags & ~CF_CONST, 0);
2612 /* Use long way over the stack */
2613 oper (flags, val, ops);
2618 void g_div (unsigned flags, unsigned long val)
2619 /* Primary = TOS / Primary */
2621 static char* ops [12] = {
2622 0, "tosdiva0", "tosdivax",
2623 0, "tosudiva0", "tosudivax",
2628 /* Do strength reduction if the value is constant and a power of two */
2630 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2631 /* Generate a shift instead */
2634 /* Generate a division */
2635 if (flags & CF_CONST) {
2636 /* lhs is not on stack */
2637 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2638 g_push (flags & ~CF_CONST, 0);
2640 oper (flags, val, ops);
2646 void g_mod (unsigned flags, unsigned long val)
2647 /* Primary = TOS % Primary */
2649 static char* ops [12] = {
2650 0, "tosmoda0", "tosmodax",
2651 0, "tosumoda0", "tosumodax",
2657 /* Check if we can do some cost reduction */
2658 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2659 /* We can do that with an AND operation */
2660 g_and (flags, val - 1);
2662 /* Do it the hard way... */
2663 if (flags & CF_CONST) {
2664 /* lhs is not on stack */
2665 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2666 g_push (flags & ~CF_CONST, 0);
2668 oper (flags, val, ops);
2674 void g_or (unsigned flags, unsigned long val)
2675 /* Primary = TOS | Primary */
2677 static char* ops [12] = {
2678 0, "tosora0", "tosorax",
2679 0, "tosora0", "tosorax",
2684 /* If the right hand side is const, the lhs is not on stack but still
2685 * in the primary register.
2687 if (flags & CF_CONST) {
2689 switch (flags & CF_TYPE) {
2692 if (flags & CF_FORCECHAR) {
2693 if ((val & 0xFF) != 0xFF) {
2694 AddCodeLine ("ora #$%02X", (unsigned char)val);
2702 AddCodeLine ("ora #$%02X", (unsigned char)val);
2709 AddCodeLine ("ora #$%02X", (unsigned char)val);
2718 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2719 * into the normal, non-optimized stuff.
2721 g_push (flags & ~CF_CONST, 0);
2725 /* Use long way over the stack */
2726 oper (flags, val, ops);
2731 void g_xor (unsigned flags, unsigned long val)
2732 /* Primary = TOS ^ Primary */
2734 static char* ops [12] = {
2735 0, "tosxora0", "tosxorax",
2736 0, "tosxora0", "tosxorax",
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 if ((val & 0xFF) != 0) {
2752 AddCodeLine ("eor #$%02X", (unsigned char)val);
2761 AddCodeLine ("eor #$%02X", (unsigned char)val);
2764 } else if ((val & 0xFF) == 0) {
2765 AddCodeLine ("pha");
2766 AddCodeLine ("txa");
2767 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2768 AddCodeLine ("tax");
2769 AddCodeLine ("pla");
2777 AddCodeLine ("eor #$%02X", (unsigned char)val);
2787 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2788 * into the normal, non-optimized stuff.
2790 g_push (flags & ~CF_CONST, 0);
2794 /* Use long way over the stack */
2795 oper (flags, val, ops);
2800 void g_and (unsigned flags, unsigned long val)
2801 /* Primary = TOS & Primary */
2803 static char* ops [12] = {
2804 0, "tosanda0", "tosandax",
2805 0, "tosanda0", "tosandax",
2810 /* If the right hand side is const, the lhs is not on stack but still
2811 * in the primary register.
2813 if (flags & CF_CONST) {
2815 switch (flags & CF_TYPE) {
2818 if (flags & CF_FORCECHAR) {
2819 AddCodeLine ("and #$%02X", (unsigned char)val);
2824 if ((val & 0xFFFF) != 0xFFFF) {
2829 } else if (val != 0xFF) {
2830 AddCodeLine ("and #$%02X", (unsigned char)val);
2832 } else if ((val & 0xFF00) == 0xFF00) {
2833 AddCodeLine ("and #$%02X", (unsigned char)val);
2834 } else if ((val & 0x00FF) == 0x0000) {
2835 AddCodeLine ("txa");
2836 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2837 AddCodeLine ("tax");
2840 AddCodeLine ("tay");
2841 AddCodeLine ("txa");
2842 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2843 AddCodeLine ("tax");
2844 AddCodeLine ("tya");
2845 if ((val & 0x00FF) != 0x00FF) {
2846 AddCodeLine ("and #$%02X", (unsigned char)val);
2855 AddCodeLine ("stx sreg+1");
2856 AddCodeLine ("stx sreg");
2857 if ((val & 0xFF) != 0xFF) {
2858 AddCodeLine ("and #$%02X", (unsigned char)val);
2861 } else if (val == 0xFF00) {
2863 AddCodeLine ("sta sreg+1");
2864 AddCodeLine ("sta sreg");
2873 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2874 * into the normal, non-optimized stuff.
2876 g_push (flags & ~CF_CONST, 0);
2880 /* Use long way over the stack */
2881 oper (flags, val, ops);
2886 void g_asr (unsigned flags, unsigned long val)
2887 /* Primary = TOS >> Primary */
2889 static char* ops [12] = {
2890 0, "tosasra0", "tosasrax",
2891 0, "tosshra0", "tosshrax",
2896 /* If the right hand side is const, the lhs is not on stack but still
2897 * in the primary register.
2899 if (flags & CF_CONST) {
2901 switch (flags & CF_TYPE) {
2905 if (val >= 8 && (flags & CF_UNSIGNED)) {
2906 AddCodeLine ("txa");
2913 } else if (val >= 1 && val <= 4) {
2914 if (flags & CF_UNSIGNED) {
2915 AddCodeLine ("jsr shrax%ld", val);
2917 AddCodeLine ("jsr asrax%ld", val);
2927 } else if (val >= 1 && val <= 4) {
2928 if (flags & CF_UNSIGNED) {
2929 AddCodeLine ("jsr shreax%ld", val);
2931 AddCodeLine ("jsr asreax%ld", val);
2934 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2935 AddCodeLine ("txa");
2936 AddCodeLine ("ldx sreg");
2937 AddCodeLine ("ldy sreg+1");
2938 AddCodeLine ("sty sreg");
2939 AddCodeLine ("ldy #$00");
2940 AddCodeLine ("sty sreg+1");
2942 } else if (val == 16) {
2943 AddCodeLine ("ldy #$00");
2944 AddCodeLine ("ldx sreg+1");
2945 if ((flags & CF_UNSIGNED) == 0) {
2946 unsigned L = GetLocalLabel();
2947 AddCodeLine ("bpl %s", LocalLabelName (L));
2948 AddCodeLine ("dey");
2951 AddCodeLine ("lda sreg");
2952 AddCodeLine ("sty sreg+1");
2953 AddCodeLine ("sty sreg");
2962 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2963 * into the normal, non-optimized stuff.
2965 g_push (flags & ~CF_CONST, 0);
2969 /* Use long way over the stack */
2970 oper (flags, val, ops);
2975 void g_asl (unsigned flags, unsigned long val)
2976 /* Primary = TOS << Primary */
2978 static char* ops [12] = {
2979 0, "tosasla0", "tosaslax",
2980 0, "tosshla0", "tosshlax",
2986 /* If the right hand side is const, the lhs is not on stack but still
2987 * in the primary register.
2989 if (flags & CF_CONST) {
2991 switch (flags & CF_TYPE) {
2996 AddCodeLine ("tax");
2997 AddCodeLine ("lda #$00");
3003 } else if (val >= 1 && val <= 4) {
3004 if (flags & CF_UNSIGNED) {
3005 AddCodeLine ("jsr shlax%ld", val);
3007 AddCodeLine ("jsr aslax%ld", val);
3017 } else if (val >= 1 && val <= 4) {
3018 if (flags & CF_UNSIGNED) {
3019 AddCodeLine ("jsr shleax%ld", val);
3021 AddCodeLine ("jsr asleax%ld", val);
3024 } else if (val == 8) {
3025 AddCodeLine ("ldy sreg");
3026 AddCodeLine ("sty sreg+1");
3027 AddCodeLine ("stx sreg");
3028 AddCodeLine ("tax");
3029 AddCodeLine ("lda #$00");
3031 } else if (val == 16) {
3032 AddCodeLine ("stx sreg+1");
3033 AddCodeLine ("sta sreg");
3034 AddCodeLine ("lda #$00");
3035 AddCodeLine ("tax");
3044 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3045 * into the normal, non-optimized stuff.
3047 g_push (flags & ~CF_CONST, 0);
3051 /* Use long way over the stack */
3052 oper (flags, val, ops);
3057 void g_neg (unsigned flags)
3058 /* Primary = -Primary */
3060 switch (flags & CF_TYPE) {
3064 AddCodeLine ("jsr negax");
3068 AddCodeLine ("jsr negeax");
3078 void g_bneg (unsigned flags)
3079 /* Primary = !Primary */
3081 switch (flags & CF_TYPE) {
3084 AddCodeLine ("jsr bnega");
3088 AddCodeLine ("jsr bnegax");
3092 AddCodeLine ("jsr bnegeax");
3102 void g_com (unsigned flags)
3103 /* Primary = ~Primary */
3105 switch (flags & CF_TYPE) {
3109 AddCodeLine ("jsr complax");
3113 AddCodeLine ("jsr compleax");
3123 void g_inc (unsigned flags, unsigned long val)
3124 /* Increment the primary register by a given number */
3126 /* Don't inc by zero */
3131 /* Generate code for the supported types */
3133 switch (flags & CF_TYPE) {
3136 if (flags & CF_FORCECHAR) {
3137 if (CPU == CPU_65C02 && val <= 2) {
3139 AddCodeLine ("ina");
3142 AddCodeLine ("clc");
3143 AddCodeLine ("adc #$%02X", (unsigned char)val);
3150 if (CPU == CPU_65C02 && val == 1) {
3151 unsigned L = GetLocalLabel();
3152 AddCodeLine ("ina");
3153 AddCodeLine ("bne %s", LocalLabelName (L));
3154 AddCodeLine ("inx");
3156 } else if (CodeSizeFactor < 200) {
3159 AddCodeLine ("jsr incax%lu", val);
3160 } else if (val <= 255) {
3162 AddCodeLine ("jsr incaxy");
3164 g_add (flags | CF_CONST, val);
3167 /* Inline the code */
3169 if ((val & 0xFF) != 0) {
3170 unsigned L = GetLocalLabel();
3171 AddCodeLine ("clc");
3172 AddCodeLine ("adc #$%02X", (unsigned char) val);
3173 AddCodeLine ("bcc %s", LocalLabelName (L));
3174 AddCodeLine ("inx");
3178 AddCodeLine ("inx");
3181 AddCodeLine ("inx");
3184 AddCodeLine ("inx");
3187 AddCodeLine ("clc");
3188 if ((val & 0xFF) != 0) {
3189 AddCodeLine ("adc #$%02X", (unsigned char) val);
3191 AddCodeLine ("pha");
3192 AddCodeLine ("txa");
3193 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3194 AddCodeLine ("tax");
3195 AddCodeLine ("pla");
3203 AddCodeLine ("jsr inceaxy");
3205 g_add (flags | CF_CONST, val);
3217 void g_dec (unsigned flags, unsigned long val)
3218 /* Decrement the primary register by a given number */
3220 /* Don't dec by zero */
3225 /* Generate code for the supported types */
3227 switch (flags & CF_TYPE) {
3230 if (flags & CF_FORCECHAR) {
3231 if (CPU == CPU_65C02 && val <= 2) {
3233 AddCodeLine ("dea");
3236 AddCodeLine ("sec");
3237 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3244 if (CodeSizeFactor < 200) {
3245 /* Use subroutines */
3247 AddCodeLine ("jsr decax%d", (int) val);
3248 } else if (val <= 255) {
3250 AddCodeLine ("jsr decaxy");
3252 g_sub (flags | CF_CONST, val);
3255 /* Inline the code */
3257 if ((val & 0xFF) != 0) {
3258 unsigned L = GetLocalLabel();
3259 AddCodeLine ("sec");
3260 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3261 AddCodeLine ("bcs %s", LocalLabelName (L));
3262 AddCodeLine ("dex");
3266 AddCodeLine ("dex");
3269 AddCodeLine ("dex");
3272 AddCodeLine ("sec");
3273 if ((val & 0xFF) != 0) {
3274 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3276 AddCodeLine ("pha");
3277 AddCodeLine ("txa");
3278 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3279 AddCodeLine ("tax");
3280 AddCodeLine ("pla");
3288 AddCodeLine ("jsr deceaxy");
3290 g_sub (flags | CF_CONST, val);
3303 * Following are the conditional operators. They compare the TOS against
3304 * the primary and put a literal 1 in the primary if the condition is
3305 * true, otherwise they clear the primary register
3310 void g_eq (unsigned flags, unsigned long val)
3311 /* Test for equal */
3313 static char* ops [12] = {
3314 "toseq00", "toseqa0", "toseqax",
3315 "toseq00", "toseqa0", "toseqax",
3322 /* If the right hand side is const, the lhs is not on stack but still
3323 * in the primary register.
3325 if (flags & CF_CONST) {
3327 switch (flags & CF_TYPE) {
3330 if (flags & CF_FORCECHAR) {
3331 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3332 AddCodeLine ("jsr booleq");
3338 L = GetLocalLabel();
3339 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3340 AddCodeLine ("bne %s", LocalLabelName (L));
3341 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3343 AddCodeLine ("jsr booleq");
3353 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3354 * into the normal, non-optimized stuff.
3356 g_push (flags & ~CF_CONST, 0);
3360 /* Use long way over the stack */
3361 oper (flags, val, ops);
3366 void g_ne (unsigned flags, unsigned long val)
3367 /* Test for not equal */
3369 static char* ops [12] = {
3370 "tosne00", "tosnea0", "tosneax",
3371 "tosne00", "tosnea0", "tosneax",
3378 /* If the right hand side is const, the lhs is not on stack but still
3379 * in the primary register.
3381 if (flags & CF_CONST) {
3383 switch (flags & CF_TYPE) {
3386 if (flags & CF_FORCECHAR) {
3387 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3388 AddCodeLine ("jsr boolne");
3394 L = GetLocalLabel();
3395 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3396 AddCodeLine ("bne %s", LocalLabelName (L));
3397 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3399 AddCodeLine ("jsr boolne");
3409 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3410 * into the normal, non-optimized stuff.
3412 g_push (flags & ~CF_CONST, 0);
3416 /* Use long way over the stack */
3417 oper (flags, val, ops);
3422 void g_lt (unsigned flags, unsigned long val)
3423 /* Test for less than */
3425 static char* ops [12] = {
3426 "toslt00", "toslta0", "tosltax",
3427 "tosult00", "tosulta0", "tosultax",
3432 /* If the right hand side is const, the lhs is not on stack but still
3433 * in the primary register.
3435 if (flags & CF_CONST) {
3437 /* Give a warning in some special cases */
3438 if ((flags & CF_UNSIGNED) && val == 0) {
3439 Warning ("Condition is never true");
3440 AddCodeLine ("jsr return0");
3444 /* Look at the type */
3445 switch (flags & CF_TYPE) {
3448 if (flags & CF_FORCECHAR) {
3449 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3450 if (flags & CF_UNSIGNED) {
3451 AddCodeLine ("jsr boolult");
3453 AddCodeLine ("jsr boollt");
3460 if (flags & CF_UNSIGNED) {
3461 /* Unsigned compare */
3462 /* If the low byte is zero, we must only test the high byte */
3463 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3464 if ((val & 0xFF) != 0) {
3465 unsigned L = GetLocalLabel();
3466 AddCodeLine ("bne %s", LocalLabelName (L));
3467 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3470 AddCodeLine ("jsr boolult");
3472 /* Signed compare */
3473 if ((val & 0xFF) == 0) {
3474 /* Low byte is zero, just look at the high byte */
3475 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3477 /* Subtract the two values */
3478 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3479 AddCodeLine ("txa");
3480 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3482 AddCodeLine ("jsr boollt");
3487 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3488 /* If we have a signed compare against zero, we only need to
3489 * test the high byte.
3491 AddCodeLine ("lda sreg+1");
3493 /* Do a subtraction */
3494 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3495 AddCodeLine ("txa");
3496 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3497 AddCodeLine ("lda sreg");
3498 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3499 AddCodeLine ("lda sreg+1");
3500 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3502 /* Emit the proper makebool routine */
3503 if (flags & CF_UNSIGNED) {
3504 /* Unsigned compare */
3505 AddCodeLine ("jsr boolult");
3507 /* Signed compare */
3508 AddCodeLine ("jsr boollt");
3516 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3517 * into the normal, non-optimized stuff.
3519 g_push (flags & ~CF_CONST, 0);
3523 /* Use long way over the stack */
3524 oper (flags, val, ops);
3529 void g_le (unsigned flags, unsigned long val)
3530 /* Test for less than or equal to */
3532 static char* ops [12] = {
3533 "tosle00", "toslea0", "tosleax",
3534 "tosule00", "tosulea0", "tosuleax",
3540 /* If the right hand side is const, the lhs is not on stack but still
3541 * in the primary register.
3543 if (flags & CF_CONST) {
3545 /* Look at the type */
3546 switch (flags & CF_TYPE) {
3549 if (flags & CF_FORCECHAR) {
3550 if (flags & CF_UNSIGNED) {
3551 /* Unsigned compare */
3553 /* Use < instead of <= because the former gives
3554 * better code on the 6502 than the latter.
3556 g_lt (flags, val+1);
3559 Warning ("Condition is always true");
3560 AddCodeLine ("jsr return1");
3563 /* Signed compare */
3564 if ((long) val < 0x7F) {
3565 /* Use < instead of <= because the former gives
3566 * better code on the 6502 than the latter.
3568 g_lt (flags, val+1);
3571 Warning ("Condition is always true");
3572 AddCodeLine ("jsr return1");
3580 if (flags & CF_UNSIGNED) {
3581 /* Unsigned compare */
3583 /* Use < instead of <= because the former gives
3584 * better code on the 6502 than the latter.
3586 g_lt (flags, val+1);
3589 Warning ("Condition is always true");
3590 AddCodeLine ("jsr return1");
3593 /* Signed compare */
3594 if ((long) val < 0x7FFF) {
3595 g_lt (flags, val+1);
3598 Warning ("Condition is always true");
3599 AddCodeLine ("jsr return1");
3605 if (flags & CF_UNSIGNED) {
3606 /* Unsigned compare */
3607 if (val < 0xFFFFFFFF) {
3608 /* Use < instead of <= because the former gives
3609 * better code on the 6502 than the latter.
3611 g_lt (flags, val+1);
3614 Warning ("Condition is always true");
3615 AddCodeLine ("jsr return1");
3618 /* Signed compare */
3619 if ((long) val < 0x7FFFFFFF) {
3620 g_lt (flags, val+1);
3623 Warning ("Condition is always true");
3624 AddCodeLine ("jsr return1");
3633 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3634 * into the normal, non-optimized stuff.
3636 g_push (flags & ~CF_CONST, 0);
3640 /* Use long way over the stack */
3641 oper (flags, val, ops);
3646 void g_gt (unsigned flags, unsigned long val)
3647 /* Test for greater than */
3649 static char* ops [12] = {
3650 "tosgt00", "tosgta0", "tosgtax",
3651 "tosugt00", "tosugta0", "tosugtax",
3657 /* If the right hand side is const, the lhs is not on stack but still
3658 * in the primary register.
3660 if (flags & CF_CONST) {
3662 /* Look at the type */
3663 switch (flags & CF_TYPE) {
3666 if (flags & CF_FORCECHAR) {
3667 if (flags & CF_UNSIGNED) {
3669 /* If we have a compare > 0, we will replace it by
3670 * != 0 here, since both are identical but the
3671 * latter is easier to optimize.
3674 } else if (val < 0xFF) {
3675 /* Use >= instead of > because the former gives
3676 * better code on the 6502 than the latter.
3678 g_ge (flags, val+1);
3681 Warning ("Condition is never true");
3682 AddCodeLine ("jsr return0");
3685 if ((long) val < 0x7F) {
3686 /* Use >= instead of > because the former gives
3687 * better code on the 6502 than the latter.
3689 g_ge (flags, val+1);
3692 Warning ("Condition is never true");
3693 AddCodeLine ("jsr return0");
3701 if (flags & CF_UNSIGNED) {
3702 /* Unsigned compare */
3704 /* If we have a compare > 0, we will replace it by
3705 * != 0 here, since both are identical but the latter
3706 * is easier to optimize.
3709 } else if (val < 0xFFFF) {
3710 /* Use >= instead of > because the former gives better
3711 * code on the 6502 than the latter.
3713 g_ge (flags, val+1);
3716 Warning ("Condition is never true");
3717 AddCodeLine ("jsr return0");
3720 /* Signed compare */
3721 if ((long) val < 0x7FFF) {
3722 g_ge (flags, val+1);
3725 Warning ("Condition is never true");
3726 AddCodeLine ("jsr return0");
3732 if (flags & CF_UNSIGNED) {
3733 /* Unsigned compare */
3735 /* If we have a compare > 0, we will replace it by
3736 * != 0 here, since both are identical but the latter
3737 * is easier to optimize.
3740 } else if (val < 0xFFFFFFFF) {
3741 /* Use >= instead of > because the former gives better
3742 * code on the 6502 than the latter.
3744 g_ge (flags, val+1);
3747 Warning ("Condition is never true");
3748 AddCodeLine ("jsr return0");
3751 /* Signed compare */
3752 if ((long) val < 0x7FFFFFFF) {
3753 g_ge (flags, val+1);
3756 Warning ("Condition is never true");
3757 AddCodeLine ("jsr return0");
3766 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3767 * into the normal, non-optimized stuff.
3769 g_push (flags & ~CF_CONST, 0);
3773 /* Use long way over the stack */
3774 oper (flags, val, ops);
3779 void g_ge (unsigned flags, unsigned long val)
3780 /* Test for greater than or equal to */
3782 static char* ops [12] = {
3783 "tosge00", "tosgea0", "tosgeax",
3784 "tosuge00", "tosugea0", "tosugeax",
3790 /* If the right hand side is const, the lhs is not on stack but still
3791 * in the primary register.
3793 if (flags & CF_CONST) {
3795 /* Give a warning in some special cases */
3796 if ((flags & CF_UNSIGNED) && val == 0) {
3797 Warning ("Condition is always true");
3798 AddCodeLine ("jsr return1");
3802 /* Look at the type */
3803 switch (flags & CF_TYPE) {
3806 if (flags & CF_FORCECHAR) {
3807 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3808 if (flags & CF_UNSIGNED) {
3809 AddCodeLine ("jsr booluge");
3811 AddCodeLine ("jsr boolge");
3818 if (flags & CF_UNSIGNED) {
3819 /* Unsigned compare */
3820 /* If the low byte is zero, we must only test the high byte */
3821 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3822 if ((val & 0xFF) != 0) {
3823 unsigned L = GetLocalLabel();
3824 AddCodeLine ("bne %s", LocalLabelName (L));
3825 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3828 AddCodeLine ("jsr booluge");
3830 /* Signed compare */
3831 if ((val & 0xFF) == 0) {
3832 /* Low byte is zero, just look at the high byte */
3833 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3835 /* Subtract the two values */
3836 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3837 AddCodeLine ("txa");
3838 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3840 AddCodeLine ("jsr boolge");
3845 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3846 /* If we have a signed compare against zero, we only need to
3847 * test the high byte.
3849 AddCodeLine ("lda sreg+1");
3851 /* Do a subtraction */
3852 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3853 AddCodeLine ("txa");
3854 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3855 AddCodeLine ("lda sreg");
3856 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3857 AddCodeLine ("lda sreg+1");
3858 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3860 /* Emit the proper makebool routine */
3861 if (flags & CF_UNSIGNED) {
3862 /* Unsigned compare */
3863 AddCodeLine ("jsr booluge");
3865 /* Signed compare */
3866 AddCodeLine ("jsr boolge");
3874 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3875 * into the normal, non-optimized stuff.
3877 g_push (flags & ~CF_CONST, 0);
3881 /* Use long way over the stack */
3882 oper (flags, val, ops);
3887 /*****************************************************************************/
3888 /* Allocating static storage */
3889 /*****************************************************************************/
3893 void g_res (unsigned n)
3894 /* Reserve static storage, n bytes */
3896 AddDataLine ("\t.res\t%u,$00", n);
3901 void g_defdata (unsigned flags, unsigned long val, long offs)
3902 /* Define data with the size given in flags */
3904 if (flags & CF_CONST) {
3906 /* Numeric constant */
3907 switch (flags & CF_TYPE) {
3910 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
3914 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
3918 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3929 /* Create the correct label name */
3930 const char* Label = GetLabelName (flags, val, offs);
3932 /* Labels are always 16 bit */
3933 AddDataLine ("\t.addr\t%s", Label);
3940 void g_defbytes (const void* Bytes, unsigned Count)
3941 /* Output a row of bytes as a constant */
3947 /* Cast the buffer pointer */
3948 const unsigned char* Data = (const unsigned char*) Bytes;
3950 /* Output the stuff */
3953 /* How many go into this line? */
3954 if ((Chunk = Count) > 16) {
3959 /* Output one line */
3960 strcpy (Buf, "\t.byte\t");
3963 B += sprintf (B, "$%02X", *Data++);
3969 /* Output the line */
3976 void g_zerobytes (unsigned n)
3977 /* Output n bytes of data initialized with zero */
3979 AddDataLine ("\t.res\t%u,$00", n);
3984 void g_initauto (unsigned Label, unsigned Size)
3985 /* Initialize a local variable at stack offset zero from static data */
3987 unsigned CodeLabel = GetLocalLabel ();
3989 CheckLocalOffs (Size);
3992 g_defcodelabel (CodeLabel);
3993 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
3994 AddCodeLine ("sta (sp),y");
3995 AddCodeLine ("dey");
3996 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
3997 } else if (Size <= 256) {
3999 g_defcodelabel (CodeLabel);
4000 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4001 AddCodeLine ("sta (sp),y");
4002 AddCodeLine ("iny");
4003 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4004 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4010 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4011 /* Initialize a static local variable from static initialization data */
4014 unsigned CodeLabel = GetLocalLabel ();
4016 g_defcodelabel (CodeLabel);
4017 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4018 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4019 AddCodeLine ("dey");
4020 AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4021 } else if (Size <= 256) {
4022 unsigned CodeLabel = GetLocalLabel ();
4024 g_defcodelabel (CodeLabel);
4025 AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4026 AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4027 AddCodeLine ("iny");
4028 AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4029 AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4031 /* Use the easy way here: memcpy */
4032 g_getimmed (CF_STATIC, VarLabel, 0);
4033 AddCodeLine ("jsr pushax");
4034 g_getimmed (CF_STATIC, InitLabel, 0);
4035 AddCodeLine ("jsr pushax");
4036 g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4037 AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4043 /*****************************************************************************/
4044 /* Switch statement */
4045 /*****************************************************************************/
4049 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4050 /* Generate code for a switch statement */
4052 unsigned NextLabel = 0;
4055 /* Setup registers and determine which compare insn to use */
4056 const char* Compare;
4059 Compare = "cmp #$%02X";
4062 Compare = "cpx #$%02X";
4065 AddCodeLine ("ldy sreg");
4066 Compare = "cpy #$%02X";
4069 AddCodeLine ("ldy sreg+1");
4070 Compare = "cpy #$%02X";
4073 Internal ("Invalid depth in g_switch: %u", Depth);
4076 /* Walk over all nodes */
4077 for (I = 0; I < CollCount (Nodes); ++I) {
4079 /* Get the next case node */
4080 CaseNode* N = CollAtUnchecked (Nodes, I);
4082 /* If we have a next label, define it */
4084 g_defcodelabel (NextLabel);
4088 /* Do the compare */
4089 AddCodeLine (Compare, CN_GetValue (N));
4091 /* If this is the last level, jump directly to the case code if found */
4094 /* Branch if equal */
4095 g_falsejump (0, CN_GetLabel (N));
4099 /* Determine the next label */
4100 if (I == CollCount (Nodes) - 1) {
4101 /* Last node means not found */
4102 g_truejump (0, DefaultLabel);
4104 /* Jump to the next check */
4105 NextLabel = GetLocalLabel ();
4106 g_truejump (0, NextLabel);
4109 /* Check the next level */
4110 g_switch (N->Nodes, DefaultLabel, Depth-1);
4115 /* If we go here, we haven't found the label */
4116 g_jump (DefaultLabel);
4121 /*****************************************************************************/
4122 /* User supplied assembler code */
4123 /*****************************************************************************/
4127 void g_asmcode (struct StrBuf* B)
4128 /* Output one line of assembler code. */
4130 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));
4135 /*****************************************************************************/
4136 /* Inlined known functions */
4137 /*****************************************************************************/
4141 void g_strlen (unsigned flags, unsigned long val, long offs)
4142 /* Inline the strlen() function */
4144 /* We need a label in both cases */
4145 unsigned label = GetLocalLabel ();
4147 /* Two different encodings */
4148 if (flags & CF_CONST) {
4150 /* The address of the string is constant. Create the correct label name */
4151 const char* lbuf = GetLabelName (flags, val, offs);
4153 /* Generate the strlen code */
4154 AddCodeLine ("ldy #$FF");
4155 g_defcodelabel (label);
4156 AddCodeLine ("iny");
4157 AddCodeLine ("lda %s,y", lbuf);
4158 AddCodeLine ("bne %s", LocalLabelName (label));
4159 AddCodeLine ("tax");
4160 AddCodeLine ("tya");
4164 /* Address not constant but in primary */
4165 if (CodeSizeFactor < 400) {
4166 /* This is too much code, so call strlen instead of inlining */
4167 AddCodeLine ("jsr _strlen");
4169 /* Inline the function */
4170 AddCodeLine ("sta ptr1");
4171 AddCodeLine ("stx ptr1+1");
4172 AddCodeLine ("ldy #$FF");
4173 g_defcodelabel (label);
4174 AddCodeLine ("iny");
4175 AddCodeLine ("lda (ptr1),y");
4176 AddCodeLine ("bne %s", LocalLabelName (label));
4177 AddCodeLine ("tax");
4178 AddCodeLine ("tya");