1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
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 char* GetLabelName (unsigned flags, unsigned long label, long offs)
101 static char lbuf [256]; /* Label name */
103 /* Create the correct label name */
104 switch (flags & CF_ADDRMASK) {
107 /* Static memory cell */
108 xsprintf (lbuf, sizeof (lbuf), "%s%+ld", LocalLabelName (label), offs);
113 xsprintf (lbuf, sizeof (lbuf), "_%s%+ld", (char*) label, offs);
117 /* Absolute address */
118 xsprintf (lbuf, sizeof (lbuf), "$%04X", (unsigned)((label+offs) & 0xFFFF));
122 /* Variable in register bank */
123 xsprintf (lbuf, sizeof (lbuf), "regbank+%u", (unsigned)((label+offs) & 0xFFFF));
127 Internal ("Invalid address flags");
130 /* Return a pointer to the static buffer */
136 /*****************************************************************************/
137 /* Pre- and postamble */
138 /*****************************************************************************/
142 void g_preamble (void)
143 /* Generate the assembler code preamble */
145 /* Create a new (global) segment list and remember it */
149 /* Identify the compiler version */
151 AddTextLine ("; File generated by cc65 v %u.%u.%u",
152 VER_MAJOR, VER_MINOR, VER_PATCH);
155 /* Insert some object file options */
156 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
157 VER_MAJOR, VER_MINOR, VER_PATCH);
159 /* If we're producing code for some other CPU, switch the command set */
160 if (CPU == CPU_65C02) {
161 AddTextLine ("\t.pc02");
164 /* Allow auto import for runtime library routines */
165 AddTextLine ("\t.autoimport\ton");
167 /* Switch the assembler into case sensitive mode */
168 AddTextLine ("\t.case\t\ton");
170 /* Tell the assembler if we want to generate debug info */
171 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
173 /* Import the stack pointer for direct auto variable access */
174 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
176 /* Define long branch macros */
177 AddTextLine ("\t.macpack\tlongbranch");
182 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
183 /* If debug info is enabled, place a file info into the source */
186 /* We have to place this into the global text segment, so it will
187 * appear before all .dbg line statements.
189 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
195 /*****************************************************************************/
196 /* Segment support */
197 /*****************************************************************************/
201 void g_userodata (void)
202 /* Switch to the read only data segment */
204 UseDataSeg (SEG_RODATA);
209 void g_usedata (void)
210 /* Switch to the data segment */
212 UseDataSeg (SEG_DATA);
218 /* Switch to the bss segment */
220 UseDataSeg (SEG_BSS);
225 void g_segname (segment_t Seg, const char* Name)
226 /* Set the name of a segment */
230 /* Remember the new name */
231 NewSegName (Seg, Name);
233 /* Emit a segment directive for the data style segments */
235 case SEG_RODATA: S = CS->ROData; break;
236 case SEG_DATA: S = CS->Data; break;
237 case SEG_BSS: S = CS->BSS; break;
238 default: S = 0; break;
241 DS_AddLine (S, ".segment\t\"%s\"", Name);
247 /*****************************************************************************/
249 /*****************************************************************************/
253 unsigned sizeofarg (unsigned flags)
254 /* Return the size of a function argument type that is encoded in flags */
256 switch (flags & CF_TYPE) {
259 return (flags & CF_FORCECHAR)? 1 : 2;
276 int pop (unsigned flags)
277 /* Pop an argument of the given size */
279 return oursp += sizeofarg (flags);
284 int push (unsigned flags)
285 /* Push an argument of the given size */
287 return oursp -= sizeofarg (flags);
292 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
293 /* The value in Offs is an offset to an address in a/x. Make sure, an object
294 * of the type given in Flags can be loaded or stored into this address by
295 * adding part of the offset to the address in ax, so that the remaining
296 * offset fits into an index register. Return the remaining offset.
299 /* If the offset is too large for a byte register, add the high byte
300 * of the offset to the primary. Beware: We need a special correction
301 * if the offset in the low byte will overflow in the operation.
303 unsigned O = Offs & ~0xFFU;
304 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
305 /* We need to add the low byte also */
309 /* Do the correction if we need one */
311 g_inc (CF_INT | CF_CONST, O);
315 /* Return the new offset */
321 /*****************************************************************************/
322 /* Functions handling local labels */
323 /*****************************************************************************/
327 void g_defcodelabel (unsigned label)
328 /* Define a local code label */
330 CS_AddLabel (CS->Code, LocalLabelName (label));
335 void g_defdatalabel (unsigned label)
336 /* Define a local data label */
338 AddDataLine ("%s:", LocalLabelName (label));
343 /*****************************************************************************/
344 /* Functions handling global labels */
345 /*****************************************************************************/
349 void g_defgloblabel (const char* Name)
350 /* Define a global label with the given name */
352 /* Global labels are always data labels */
353 AddDataLine ("_%s:", Name);
358 void g_defexport (const char* Name, int ZP)
359 /* Export the given label */
362 AddTextLine ("\t.exportzp\t_%s", Name);
364 AddTextLine ("\t.export\t\t_%s", Name);
370 void g_defimport (const char* Name, int ZP)
371 /* Import the given label */
374 AddTextLine ("\t.importzp\t_%s", Name);
376 AddTextLine ("\t.import\t\t_%s", Name);
382 /*****************************************************************************/
383 /* Load functions for various registers */
384 /*****************************************************************************/
388 static void ldaconst (unsigned val)
389 /* Load a with a constant */
391 AddCodeLine ("lda #$%02X", val & 0xFF);
396 static void ldxconst (unsigned val)
397 /* Load x with a constant */
399 AddCodeLine ("ldx #$%02X", val & 0xFF);
404 static void ldyconst (unsigned val)
405 /* Load y with a constant */
407 AddCodeLine ("ldy #$%02X", val & 0xFF);
412 /*****************************************************************************/
413 /* Function entry and exit */
414 /*****************************************************************************/
418 /* Remember the argument size of a function. The variable is set by g_enter
419 * and used by g_leave. If the functions gets its argument size by the caller
420 * (variable param list or function without prototype), g_enter will set the
426 void g_enter (unsigned flags, unsigned argsize)
427 /* Function prologue */
429 if ((flags & CF_FIXARGC) != 0) {
430 /* Just remember the argument size for the leave */
434 AddCodeLine ("jsr enter");
441 /* Function epilogue */
443 /* How many bytes of locals do we have to drop? */
446 /* If we didn't have a variable argument list, don't call leave */
449 /* Drop stackframe if needed */
453 AddCodeLine ("jsr incsp%d", k);
457 AddCodeLine ("jsr addysp");
464 /* Nothing to drop */
465 AddCodeLine ("jsr leave");
467 /* We've a stack frame to drop */
469 AddCodeLine ("jsr leavey");
473 /* Add the final rts */
479 /*****************************************************************************/
480 /* Register variables */
481 /*****************************************************************************/
485 void g_save_regvars (int RegOffs, unsigned Bytes)
486 /* Save register variables */
488 /* Don't loop for up to two bytes */
491 AddCodeLine ("lda regbank%+d", RegOffs);
492 AddCodeLine ("jsr pusha");
494 } else if (Bytes == 2) {
496 AddCodeLine ("lda regbank%+d", RegOffs);
497 AddCodeLine ("ldx regbank%+d", RegOffs+1);
498 AddCodeLine ("jsr pushax");
502 /* More than two bytes - loop */
503 unsigned Label = GetLocalLabel ();
505 ldyconst (Bytes - 1);
507 g_defcodelabel (Label);
508 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
509 AddCodeLine ("sta (sp),y");
512 AddCodeLine ("bne %s", LocalLabelName (Label));
516 /* We pushed stuff, correct the stack pointer */
522 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
523 /* Restore register variables */
525 /* Calculate the actual stack offset and check it */
527 CheckLocalOffs (StackOffs);
529 /* Don't loop for up to two bytes */
532 ldyconst (StackOffs);
533 AddCodeLine ("lda (sp),y");
534 AddCodeLine ("sta regbank%+d", RegOffs);
536 } else if (Bytes == 2) {
538 ldyconst (StackOffs);
539 AddCodeLine ("lda (sp),y");
540 AddCodeLine ("sta regbank%+d", RegOffs);
542 AddCodeLine ("lda (sp),y");
543 AddCodeLine ("sta regbank%+d", RegOffs+1);
547 /* More than two bytes - loop */
548 unsigned Label = GetLocalLabel ();
549 ldyconst (StackOffs+Bytes-1);
551 g_defcodelabel (Label);
552 AddCodeLine ("lda (sp),y");
553 AddCodeLine ("sta regbank%+d,x", RegOffs-1);
556 AddCodeLine ("bne %s", LocalLabelName (Label));
563 /*****************************************************************************/
564 /* Fetching memory cells */
565 /*****************************************************************************/
569 void g_getimmed (unsigned Flags, unsigned long Val, long Offs)
570 /* Load a constant into the primary register */
572 unsigned char B1, B2, B3, B4;
576 if ((Flags & CF_CONST) != 0) {
578 /* Numeric constant */
579 switch (Flags & CF_TYPE) {
582 if ((Flags & CF_FORCECHAR) != 0) {
588 ldxconst ((Val >> 8) & 0xFF);
589 ldaconst (Val & 0xFF);
593 /* Split the value into 4 bytes */
594 B1 = (unsigned char) (Val >> 0);
595 B2 = (unsigned char) (Val >> 8);
596 B3 = (unsigned char) (Val >> 16);
597 B4 = (unsigned char) (Val >> 24);
599 /* Remember which bytes are done */
603 AddCodeLine ("ldx #$%02X", B2);
606 AddCodeLine ("stx sreg");
610 AddCodeLine ("stx sreg+1");
613 if ((Done & 0x04) == 0 && B1 != B3) {
614 AddCodeLine ("lda #$%02X", B3);
615 AddCodeLine ("sta sreg");
618 if ((Done & 0x08) == 0 && B1 != B4) {
619 AddCodeLine ("lda #$%02X", B4);
620 AddCodeLine ("sta sreg+1");
623 AddCodeLine ("lda #$%02X", B1);
625 if ((Done & 0x04) == 0) {
627 AddCodeLine ("sta sreg");
629 if ((Done & 0x08) == 0) {
631 AddCodeLine ("sta sreg+1");
643 /* Some sort of label */
644 const char* Label = GetLabelName (Flags, Val, Offs);
646 /* Load the address into the primary */
647 AddCodeLine ("lda #<(%s)", Label);
648 AddCodeLine ("ldx #>(%s)", Label);
655 void g_getstatic (unsigned flags, unsigned long label, long offs)
656 /* Fetch an static memory cell into the primary register */
658 /* Create the correct label name */
659 char* lbuf = GetLabelName (flags, label, offs);
661 /* Check the size and generate the correct load operation */
662 switch (flags & CF_TYPE) {
665 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
666 AddCodeLine ("lda %s", lbuf); /* load A from the label */
669 AddCodeLine ("lda %s", lbuf); /* load A from the label */
670 if (!(flags & CF_UNSIGNED)) {
671 /* Must sign extend */
672 unsigned L = GetLocalLabel ();
673 AddCodeLine ("bpl %s", LocalLabelName (L));
681 AddCodeLine ("lda %s", lbuf);
682 if (flags & CF_TEST) {
683 AddCodeLine ("ora %s+1", lbuf);
685 AddCodeLine ("ldx %s+1", lbuf);
690 if (flags & CF_TEST) {
691 AddCodeLine ("lda %s+3", lbuf);
692 AddCodeLine ("ora %s+2", lbuf);
693 AddCodeLine ("ora %s+1", lbuf);
694 AddCodeLine ("ora %s+0", lbuf);
696 AddCodeLine ("lda %s+3", lbuf);
697 AddCodeLine ("sta sreg+1");
698 AddCodeLine ("lda %s+2", lbuf);
699 AddCodeLine ("sta sreg");
700 AddCodeLine ("ldx %s+1", lbuf);
701 AddCodeLine ("lda %s", lbuf);
713 void g_getlocal (unsigned flags, int offs)
714 /* Fetch specified local object (local var). */
717 CheckLocalOffs (offs);
718 switch (flags & CF_TYPE) {
721 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
723 AddCodeLine ("lda (sp),y");
726 AddCodeLine ("ldx #$00");
727 AddCodeLine ("lda (sp),y");
728 if ((flags & CF_UNSIGNED) == 0) {
729 unsigned L = GetLocalLabel();
730 AddCodeLine ("bpl %s", LocalLabelName (L));
738 CheckLocalOffs (offs + 1);
739 if (flags & CF_TEST) {
741 AddCodeLine ("lda (sp),y");
743 AddCodeLine ("ora (sp),y");
746 AddCodeLine ("jsr ldaxysp");
752 AddCodeLine ("jsr ldeaxysp");
762 void g_getind (unsigned flags, unsigned offs)
763 /* Fetch the specified object type indirect through the primary register
764 * into the primary register
767 /* If the offset is greater than 255, add the part that is > 255 to
768 * the primary. This way we get an easy addition and use the low byte
771 offs = MakeByteOffs (flags, offs);
773 /* Handle the indirect fetch */
774 switch (flags & CF_TYPE) {
777 /* Character sized */
778 if (flags & CF_UNSIGNED) {
780 AddCodeLine ("jsr ldauidx");
783 AddCodeLine ("jsr ldaidx");
788 if (flags & CF_TEST) {
790 AddCodeLine ("sta ptr1");
791 AddCodeLine ("stx ptr1+1");
792 AddCodeLine ("lda (ptr1),y");
794 AddCodeLine ("ora (ptr1),y");
797 AddCodeLine ("jsr ldaxidx");
803 AddCodeLine ("jsr ldeaxidx");
804 if (flags & CF_TEST) {
805 AddCodeLine ("jsr tsteax");
817 void g_leasp (int offs)
818 /* Fetch the address of the specified symbol into the primary register */
820 /* Calculate the offset relative to sp */
823 /* For value 0 we do direct code */
825 AddCodeLine ("lda sp");
826 AddCodeLine ("ldx sp+1");
828 if (CodeSizeFactor < 300) {
829 ldaconst (offs); /* Load A with offset value */
830 AddCodeLine ("jsr leaasp"); /* Load effective address */
832 unsigned L = GetLocalLabel ();
833 if (CPU == CPU_65C02 && offs == 1) {
834 AddCodeLine ("lda sp");
835 AddCodeLine ("ldx sp+1");
837 AddCodeLine ("bne %s", LocalLabelName (L));
842 AddCodeLine ("ldx sp+1");
843 AddCodeLine ("adc sp");
844 AddCodeLine ("bcc %s", LocalLabelName (L));
854 void g_leavariadic (int Offs)
855 /* Fetch the address of a parameter in a variadic function into the primary
859 unsigned ArgSizeOffs;
861 /* Calculate the offset relative to sp */
864 /* Get the offset of the parameter which is stored at sp+0 on function
865 * entry and check if this offset is reachable with a byte offset.
868 ArgSizeOffs = -oursp;
869 CheckLocalOffs (ArgSizeOffs);
871 /* Get the size of all parameters. */
872 ldyconst (ArgSizeOffs);
873 AddCodeLine ("lda (sp),y");
875 /* Add the value of the stackpointer */
876 if (CodeSizeFactor > 250) {
877 unsigned L = GetLocalLabel();
878 AddCodeLine ("ldx sp+1");
880 AddCodeLine ("adc sp");
881 AddCodeLine ("bcc %s", LocalLabelName (L));
885 AddCodeLine ("jsr leaasp");
888 /* Add the offset to the primary */
890 g_inc (CF_INT | CF_CONST, Offs);
891 } else if (Offs < 0) {
892 g_dec (CF_INT | CF_CONST, -Offs);
898 /*****************************************************************************/
899 /* Store into memory */
900 /*****************************************************************************/
904 void g_putstatic (unsigned flags, unsigned long label, long offs)
905 /* Store the primary register into the specified static memory cell */
907 /* Create the correct label name */
908 char* lbuf = GetLabelName (flags, label, offs);
910 /* Check the size and generate the correct store operation */
911 switch (flags & CF_TYPE) {
914 AddCodeLine ("sta %s", lbuf);
918 AddCodeLine ("sta %s", lbuf);
919 AddCodeLine ("stx %s+1", lbuf);
923 AddCodeLine ("sta %s", lbuf);
924 AddCodeLine ("stx %s+1", lbuf);
925 AddCodeLine ("ldy sreg");
926 AddCodeLine ("sty %s+2", lbuf);
927 AddCodeLine ("ldy sreg+1");
928 AddCodeLine ("sty %s+3", lbuf);
939 void g_putlocal (unsigned Flags, int Offs, long Val)
940 /* Put data into local object. */
943 CheckLocalOffs (Offs);
944 switch (Flags & CF_TYPE) {
947 if (Flags & CF_CONST) {
948 AddCodeLine ("lda #$%02X", (unsigned char) Val);
951 AddCodeLine ("sta (sp),y");
955 if (Flags & CF_CONST) {
957 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
958 AddCodeLine ("sta (sp),y");
959 if ((Flags & CF_NOKEEP) == 0) {
960 /* Place high byte into X */
963 if ((Val & 0xFF) == Offs+1) {
964 /* The value we need is already in Y */
969 AddCodeLine ("lda #$%02X", (unsigned char) Val);
971 AddCodeLine ("sta (sp),y");
973 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
975 AddCodeLine ("jsr staxysp");
978 AddCodeLine ("sta (sp),y");
981 AddCodeLine ("sta (sp),y");
987 if (Flags & CF_CONST) {
988 g_getimmed (Flags, Val, 0);
991 AddCodeLine ("jsr steaxysp");
1002 void g_putind (unsigned Flags, unsigned Offs)
1003 /* Store the specified object type in the primary register at the address
1004 * on the top of the stack
1007 /* We can handle offsets below $100 directly, larger offsets must be added
1008 * to the address. Since a/x is in use, best code is achieved by adding
1009 * just the high byte. Be sure to check if the low byte will overflow while
1012 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1014 /* Overflow - we need to add the low byte also */
1015 AddCodeLine ("ldy #$00");
1016 AddCodeLine ("clc");
1017 AddCodeLine ("pha");
1018 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1019 AddCodeLine ("adc (sp),y");
1020 AddCodeLine ("sta (sp),y");
1021 AddCodeLine ("iny");
1022 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1023 AddCodeLine ("adc (sp),y");
1024 AddCodeLine ("sta (sp),y");
1025 AddCodeLine ("pla");
1027 /* Complete address is on stack, new offset is zero */
1030 } else if ((Offs & 0xFF00) != 0) {
1032 /* We can just add the high byte */
1033 AddCodeLine ("ldy #$01");
1034 AddCodeLine ("clc");
1035 AddCodeLine ("pha");
1036 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1037 AddCodeLine ("adc (sp),y");
1038 AddCodeLine ("sta (sp),y");
1039 AddCodeLine ("pla");
1041 /* Offset is now just the low byte */
1045 /* Check the size and determine operation */
1046 switch (Flags & CF_TYPE) {
1050 AddCodeLine ("jsr staspidx");
1055 AddCodeLine ("jsr staxspidx");
1060 AddCodeLine ("jsr steaxspidx");
1068 /* Pop the argument which is always a pointer */
1074 /*****************************************************************************/
1075 /* type conversion and similiar stuff */
1076 /*****************************************************************************/
1080 void g_toslong (unsigned flags)
1081 /* Make sure, the value on TOS is a long. Convert if necessary */
1083 switch (flags & CF_TYPE) {
1087 if (flags & CF_UNSIGNED) {
1088 AddCodeLine ("jsr tosulong");
1090 AddCodeLine ("jsr toslong");
1105 void g_tosint (unsigned flags)
1106 /* Make sure, the value on TOS is an int. Convert if necessary */
1108 switch (flags & CF_TYPE) {
1115 AddCodeLine ("jsr tosint");
1126 void g_reglong (unsigned flags)
1127 /* Make sure, the value in the primary register a long. Convert if necessary */
1129 switch (flags & CF_TYPE) {
1133 if (flags & CF_UNSIGNED) {
1134 if (CodeSizeFactor >= 200) {
1136 AddCodeLine ("sty sreg");
1137 AddCodeLine ("sty sreg+1");
1139 AddCodeLine ("jsr axulong");
1142 AddCodeLine ("jsr axlong");
1156 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1157 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1158 * value, that corresponds to the value on TOS, rhs corresponds to the value
1159 * in (e)ax. The return value is the the flags value for the resulting type.
1162 unsigned ltype, rtype;
1165 /* Get the type spec from the flags */
1166 ltype = lhs & CF_TYPE;
1167 rtype = rhs & CF_TYPE;
1169 /* Check if a conversion is needed */
1170 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1171 /* We must promote the primary register to long */
1173 /* Get the new rhs type */
1174 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1176 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1177 /* We must promote the lhs to long */
1183 /* Get the new rhs type */
1184 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1188 /* Determine the result type for the operation:
1189 * - The result is const if both operands are const.
1190 * - The result is unsigned if one of the operands is unsigned.
1191 * - The result is long if one of the operands is long.
1192 * - Otherwise the result is int sized.
1194 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1195 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1196 if (rtype == CF_LONG || ltype == CF_LONG) {
1206 unsigned g_typecast (unsigned lhs, unsigned rhs)
1207 /* Cast the value in the primary register to the operand size that is flagged
1208 * by the lhs value. Return the result value.
1211 unsigned ltype, rtype;
1213 /* Get the type spec from the flags */
1214 ltype = lhs & CF_TYPE;
1215 rtype = rhs & CF_TYPE;
1217 /* Check if a conversion is needed */
1218 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1219 /* We must promote the primary register to long */
1223 /* Do not need any other action. If the left type is int, and the primary
1224 * register is long, it will be automagically truncated. If the right hand
1225 * side is const, it is not located in the primary register and handled by
1226 * the expression parser code.
1229 /* Result is const if the right hand side was const */
1230 lhs |= (rhs & CF_CONST);
1232 /* The resulting type is that of the left hand side (that's why you called
1240 void g_scale (unsigned flags, long val)
1241 /* Scale the value in the primary register by the given value. If val is positive,
1242 * scale up, is val is negative, scale down. This function is used to scale
1243 * the operands or results of pointer arithmetic by the size of the type, the
1244 * pointer points to.
1249 /* Value may not be zero */
1251 Internal ("Data type has no size");
1252 } else if (val > 0) {
1255 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1257 /* Factor is 2, 4, 8 and 16, use special function */
1258 switch (flags & CF_TYPE) {
1261 if (flags & CF_FORCECHAR) {
1263 AddCodeLine ("asl a");
1270 if (CodeSizeFactor >= (p2+1)*130U) {
1271 AddCodeLine ("stx tmp1");
1273 AddCodeLine ("asl a");
1274 AddCodeLine ("rol tmp1");
1276 AddCodeLine ("ldx tmp1");
1278 if (flags & CF_UNSIGNED) {
1279 AddCodeLine ("jsr shlax%d", p2);
1281 AddCodeLine ("jsr aslax%d", p2);
1287 if (flags & CF_UNSIGNED) {
1288 AddCodeLine ("jsr shleax%d", p2);
1290 AddCodeLine ("jsr asleax%d", p2);
1299 } else if (val != 1) {
1301 /* Use a multiplication instead */
1302 g_mul (flags | CF_CONST, val);
1310 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1312 /* Factor is 2, 4, 8 and 16 use special function */
1313 switch (flags & CF_TYPE) {
1316 if (flags & CF_FORCECHAR) {
1317 if (flags & CF_UNSIGNED) {
1319 AddCodeLine ("lsr a");
1322 } else if (p2 <= 2) {
1323 AddCodeLine ("cmp #$80");
1324 AddCodeLine ("ror a");
1331 if (flags & CF_UNSIGNED) {
1332 if (CodeSizeFactor >= (p2+1)*130U) {
1333 AddCodeLine ("stx tmp1");
1335 AddCodeLine ("lsr tmp1");
1336 AddCodeLine ("ror a");
1338 AddCodeLine ("ldx tmp1");
1340 AddCodeLine ("jsr lsrax%d", p2);
1343 if (CodeSizeFactor >= (p2+1)*150U) {
1344 AddCodeLine ("stx tmp1");
1346 AddCodeLine ("cpx #$80");
1347 AddCodeLine ("ror tmp1");
1348 AddCodeLine ("ror a");
1350 AddCodeLine ("ldx tmp1");
1352 AddCodeLine ("jsr asrax%d", p2);
1358 if (flags & CF_UNSIGNED) {
1359 AddCodeLine ("jsr lsreax%d", p2);
1361 AddCodeLine ("jsr asreax%d", p2);
1370 } else if (val != 1) {
1372 /* Use a division instead */
1373 g_div (flags | CF_CONST, val);
1381 /*****************************************************************************/
1382 /* Adds and subs of variables fix a fixed address */
1383 /*****************************************************************************/
1387 void g_addlocal (unsigned flags, int offs)
1388 /* Add a local variable to ax */
1392 /* Correct the offset and check it */
1394 CheckLocalOffs (offs);
1396 switch (flags & CF_TYPE) {
1399 L = GetLocalLabel();
1400 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1401 AddCodeLine ("clc");
1402 AddCodeLine ("adc (sp),y");
1403 AddCodeLine ("bcc %s", LocalLabelName (L));
1404 AddCodeLine ("inx");
1409 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1410 AddCodeLine ("clc");
1411 AddCodeLine ("adc (sp),y");
1412 AddCodeLine ("pha");
1413 AddCodeLine ("txa");
1414 AddCodeLine ("iny");
1415 AddCodeLine ("adc (sp),y");
1416 AddCodeLine ("tax");
1417 AddCodeLine ("pla");
1421 /* Do it the old way */
1423 g_getlocal (flags, offs);
1435 void g_addstatic (unsigned flags, unsigned long label, long offs)
1436 /* Add a static variable to ax */
1440 /* Create the correct label name */
1441 char* lbuf = GetLabelName (flags, label, offs);
1443 switch (flags & CF_TYPE) {
1446 L = GetLocalLabel();
1447 AddCodeLine ("clc");
1448 AddCodeLine ("adc %s", lbuf);
1449 AddCodeLine ("bcc %s", LocalLabelName (L));
1450 AddCodeLine ("inx");
1455 AddCodeLine ("clc");
1456 AddCodeLine ("adc %s", lbuf);
1457 AddCodeLine ("tay");
1458 AddCodeLine ("txa");
1459 AddCodeLine ("adc %s+1", lbuf);
1460 AddCodeLine ("tax");
1461 AddCodeLine ("tya");
1465 /* Do it the old way */
1467 g_getstatic (flags, label, offs);
1479 /*****************************************************************************/
1480 /* Special op= functions */
1481 /*****************************************************************************/
1485 void g_addeqstatic (unsigned flags, unsigned long label, long offs,
1487 /* Emit += for a static variable */
1489 /* Create the correct label name */
1490 char* lbuf = GetLabelName (flags, label, offs);
1492 /* Check the size and determine operation */
1493 switch (flags & CF_TYPE) {
1496 if (flags & CF_FORCECHAR) {
1497 AddCodeLine ("ldx #$00");
1498 if (flags & CF_CONST) {
1500 AddCodeLine ("inc %s", lbuf);
1501 AddCodeLine ("lda %s", lbuf);
1503 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1504 AddCodeLine ("clc");
1505 AddCodeLine ("adc %s", lbuf);
1506 AddCodeLine ("sta %s", lbuf);
1509 AddCodeLine ("clc");
1510 AddCodeLine ("adc %s", lbuf);
1511 AddCodeLine ("sta %s", lbuf);
1513 if ((flags & CF_UNSIGNED) == 0) {
1514 unsigned L = GetLocalLabel();
1515 AddCodeLine ("bpl %s", LocalLabelName (L));
1516 AddCodeLine ("dex");
1524 if (flags & CF_CONST) {
1526 unsigned L = GetLocalLabel ();
1527 AddCodeLine ("inc %s", lbuf);
1528 AddCodeLine ("bne %s", LocalLabelName (L));
1529 AddCodeLine ("inc %s+1", lbuf);
1531 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1532 AddCodeLine ("ldx %s+1", lbuf);
1534 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1535 AddCodeLine ("clc");
1536 AddCodeLine ("adc %s", lbuf);
1537 AddCodeLine ("sta %s", lbuf);
1539 unsigned L = GetLocalLabel ();
1540 AddCodeLine ("bcc %s", LocalLabelName (L));
1541 AddCodeLine ("inc %s+1", lbuf);
1543 AddCodeLine ("ldx %s+1", lbuf);
1545 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1546 AddCodeLine ("adc %s+1", lbuf);
1547 AddCodeLine ("sta %s+1", lbuf);
1548 AddCodeLine ("tax");
1549 AddCodeLine ("lda %s", lbuf);
1553 AddCodeLine ("clc");
1554 AddCodeLine ("adc %s", lbuf);
1555 AddCodeLine ("sta %s", lbuf);
1556 AddCodeLine ("txa");
1557 AddCodeLine ("adc %s+1", lbuf);
1558 AddCodeLine ("sta %s+1", lbuf);
1559 AddCodeLine ("tax");
1560 AddCodeLine ("lda %s", lbuf);
1565 if (flags & CF_CONST) {
1567 AddCodeLine ("ldy #<(%s)", lbuf);
1568 AddCodeLine ("sty ptr1");
1569 AddCodeLine ("ldy #>(%s+1)", lbuf);
1571 AddCodeLine ("jsr laddeq1");
1573 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1574 AddCodeLine ("jsr laddeqa");
1577 g_getstatic (flags, label, offs);
1579 g_putstatic (flags, label, offs);
1582 AddCodeLine ("ldy #<(%s)", lbuf);
1583 AddCodeLine ("sty ptr1");
1584 AddCodeLine ("ldy #>(%s+1)", lbuf);
1585 AddCodeLine ("jsr laddeq");
1596 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1597 /* Emit += for a local variable */
1599 /* Calculate the true offset, check it, load it into Y */
1601 CheckLocalOffs (offs);
1603 /* Check the size and determine operation */
1604 switch (flags & CF_TYPE) {
1607 if (flags & CF_FORCECHAR) {
1609 AddCodeLine ("ldx #$00");
1610 if (flags & CF_CONST) {
1611 AddCodeLine ("clc");
1612 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1613 AddCodeLine ("adc (sp),y");
1614 AddCodeLine ("sta (sp),y");
1616 AddCodeLine ("clc");
1617 AddCodeLine ("adc (sp),y");
1618 AddCodeLine ("sta (sp),y");
1620 if ((flags & CF_UNSIGNED) == 0) {
1621 unsigned L = GetLocalLabel();
1622 AddCodeLine ("bpl %s", LocalLabelName (L));
1623 AddCodeLine ("dex");
1632 if (flags & CF_CONST) {
1633 if (CodeSizeFactor >= 400) {
1634 AddCodeLine ("clc");
1635 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1636 AddCodeLine ("adc (sp),y");
1637 AddCodeLine ("sta (sp),y");
1638 AddCodeLine ("iny");
1639 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1640 AddCodeLine ("adc (sp),y");
1641 AddCodeLine ("sta (sp),y");
1642 AddCodeLine ("tax");
1643 AddCodeLine ("dey");
1644 AddCodeLine ("lda (sp),y");
1646 g_getimmed (flags, val, 0);
1647 AddCodeLine ("jsr addeqysp");
1650 AddCodeLine ("jsr addeqysp");
1655 if (flags & CF_CONST) {
1656 g_getimmed (flags, val, 0);
1659 AddCodeLine ("jsr laddeqysp");
1669 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1670 /* Emit += for the location with address in ax */
1672 /* If the offset is too large for a byte register, add the high byte
1673 * of the offset to the primary. Beware: We need a special correction
1674 * if the offset in the low byte will overflow in the operation.
1676 offs = MakeByteOffs (flags, offs);
1678 /* Check the size and determine operation */
1679 switch (flags & CF_TYPE) {
1682 AddCodeLine ("sta ptr1");
1683 AddCodeLine ("stx ptr1+1");
1684 AddCodeLine ("ldy #$%02X", offs);
1685 AddCodeLine ("ldx #$00");
1686 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1687 AddCodeLine ("clc");
1688 AddCodeLine ("adc (ptr1),y");
1689 AddCodeLine ("sta (ptr1),y");
1693 if (CodeSizeFactor >= 200) {
1694 /* Lots of code, use only if size is not important */
1695 AddCodeLine ("sta ptr1");
1696 AddCodeLine ("stx ptr1+1");
1697 AddCodeLine ("ldy #$%02X", offs);
1698 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1699 AddCodeLine ("clc");
1700 AddCodeLine ("adc (ptr1),y");
1701 AddCodeLine ("sta (ptr1),y");
1702 AddCodeLine ("pha");
1703 AddCodeLine ("iny");
1704 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1705 AddCodeLine ("adc (ptr1),y");
1706 AddCodeLine ("sta (ptr1),y");
1707 AddCodeLine ("tax");
1708 AddCodeLine ("pla");
1714 AddCodeLine ("jsr pushax"); /* Push the address */
1715 push (CF_PTR); /* Correct the internal sp */
1716 g_getind (flags, offs); /* Fetch the value */
1717 g_inc (flags, val); /* Increment value in primary */
1718 g_putind (flags, offs); /* Store the value back */
1728 void g_subeqstatic (unsigned flags, unsigned long label, long offs,
1730 /* Emit -= for a static variable */
1732 /* Create the correct label name */
1733 char* lbuf = GetLabelName (flags, label, offs);
1735 /* Check the size and determine operation */
1736 switch (flags & CF_TYPE) {
1739 if (flags & CF_FORCECHAR) {
1740 AddCodeLine ("ldx #$00");
1741 if (flags & CF_CONST) {
1743 AddCodeLine ("dec %s", lbuf);
1744 AddCodeLine ("lda %s", lbuf);
1746 AddCodeLine ("sec");
1747 AddCodeLine ("lda %s", lbuf);
1748 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1749 AddCodeLine ("sta %s", lbuf);
1752 AddCodeLine ("eor #$FF");
1753 AddCodeLine ("sec");
1754 AddCodeLine ("adc %s", lbuf);
1755 AddCodeLine ("sta %s", lbuf);
1757 if ((flags & CF_UNSIGNED) == 0) {
1758 unsigned L = GetLocalLabel();
1759 AddCodeLine ("bpl %s", LocalLabelName (L));
1760 AddCodeLine ("dex");
1768 AddCodeLine ("sec");
1769 if (flags & CF_CONST) {
1770 AddCodeLine ("lda %s", lbuf);
1771 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1772 AddCodeLine ("sta %s", lbuf);
1774 unsigned L = GetLocalLabel ();
1775 AddCodeLine ("bcs %s", LocalLabelName (L));
1776 AddCodeLine ("dec %s+1", lbuf);
1778 AddCodeLine ("ldx %s+1", lbuf);
1780 AddCodeLine ("lda %s+1", lbuf);
1781 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1782 AddCodeLine ("sta %s+1", lbuf);
1783 AddCodeLine ("tax");
1784 AddCodeLine ("lda %s", lbuf);
1787 AddCodeLine ("eor #$FF");
1788 AddCodeLine ("adc %s", lbuf);
1789 AddCodeLine ("sta %s", lbuf);
1790 AddCodeLine ("txa");
1791 AddCodeLine ("eor #$FF");
1792 AddCodeLine ("adc %s+1", lbuf);
1793 AddCodeLine ("sta %s+1", lbuf);
1794 AddCodeLine ("tax");
1795 AddCodeLine ("lda %s", lbuf);
1800 if (flags & CF_CONST) {
1802 AddCodeLine ("ldy #<(%s)", lbuf);
1803 AddCodeLine ("sty ptr1");
1804 AddCodeLine ("ldy #>(%s+1)", lbuf);
1805 AddCodeLine ("lda #$%02X", (unsigned char)val);
1806 AddCodeLine ("jsr lsubeqa");
1808 g_getstatic (flags, label, offs);
1810 g_putstatic (flags, label, offs);
1813 AddCodeLine ("ldy #<(%s)", lbuf);
1814 AddCodeLine ("sty ptr1");
1815 AddCodeLine ("ldy #>(%s+1)", lbuf);
1816 AddCodeLine ("jsr lsubeq");
1827 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1828 /* Emit -= for a local variable */
1830 /* Calculate the true offset, check it, load it into Y */
1832 CheckLocalOffs (offs);
1834 /* Check the size and determine operation */
1835 switch (flags & CF_TYPE) {
1838 if (flags & CF_FORCECHAR) {
1840 AddCodeLine ("ldx #$00");
1841 AddCodeLine ("sec");
1842 if (flags & CF_CONST) {
1843 AddCodeLine ("lda (sp),y");
1844 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1846 AddCodeLine ("eor #$FF");
1847 AddCodeLine ("adc (sp),y");
1849 AddCodeLine ("sta (sp),y");
1850 if ((flags & CF_UNSIGNED) == 0) {
1851 unsigned L = GetLocalLabel();
1852 AddCodeLine ("bpl %s", LocalLabelName (L));
1853 AddCodeLine ("dex");
1861 if (flags & CF_CONST) {
1862 g_getimmed (flags, val, 0);
1865 AddCodeLine ("jsr subeqysp");
1869 if (flags & CF_CONST) {
1870 g_getimmed (flags, val, 0);
1873 AddCodeLine ("jsr lsubeqysp");
1883 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
1884 /* Emit -= for the location with address in ax */
1886 /* If the offset is too large for a byte register, add the high byte
1887 * of the offset to the primary. Beware: We need a special correction
1888 * if the offset in the low byte will overflow in the operation.
1890 offs = MakeByteOffs (flags, offs);
1892 /* Check the size and determine operation */
1893 switch (flags & CF_TYPE) {
1896 AddCodeLine ("sta ptr1");
1897 AddCodeLine ("stx ptr1+1");
1898 AddCodeLine ("ldy #$%02X", offs);
1899 AddCodeLine ("ldx #$00");
1900 AddCodeLine ("lda (ptr1),y");
1901 AddCodeLine ("sec");
1902 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1903 AddCodeLine ("sta (ptr1),y");
1907 if (CodeSizeFactor >= 200) {
1908 /* Lots of code, use only if size is not important */
1909 AddCodeLine ("sta ptr1");
1910 AddCodeLine ("stx ptr1+1");
1911 AddCodeLine ("ldy #$%02X", offs);
1912 AddCodeLine ("lda (ptr1),y");
1913 AddCodeLine ("sec");
1914 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1915 AddCodeLine ("sta (ptr1),y");
1916 AddCodeLine ("pha");
1917 AddCodeLine ("iny");
1918 AddCodeLine ("lda (ptr1),y");
1919 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1920 AddCodeLine ("sta (ptr1),y");
1921 AddCodeLine ("tax");
1922 AddCodeLine ("pla");
1928 AddCodeLine ("jsr pushax"); /* Push the address */
1929 push (CF_PTR); /* Correct the internal sp */
1930 g_getind (flags, offs); /* Fetch the value */
1931 g_dec (flags, val); /* Increment value in primary */
1932 g_putind (flags, offs); /* Store the value back */
1942 /*****************************************************************************/
1943 /* Add a variable address to the value in ax */
1944 /*****************************************************************************/
1948 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
1949 /* Add the address of a local variable to ax */
1953 /* Add the offset */
1956 /* We cannot address more then 256 bytes of locals anyway */
1957 L = GetLocalLabel();
1958 CheckLocalOffs (offs);
1959 AddCodeLine ("clc");
1960 AddCodeLine ("adc #$%02X", offs & 0xFF);
1961 /* Do also skip the CLC insn below */
1962 AddCodeLine ("bcc %s", LocalLabelName (L));
1963 AddCodeLine ("inx");
1966 /* Add the current stackpointer value */
1967 AddCodeLine ("clc");
1969 /* Label was used above */
1972 AddCodeLine ("adc sp");
1973 AddCodeLine ("tay");
1974 AddCodeLine ("txa");
1975 AddCodeLine ("adc sp+1");
1976 AddCodeLine ("tax");
1977 AddCodeLine ("tya");
1982 void g_addaddr_static (unsigned flags, unsigned long label, long offs)
1983 /* Add the address of a static variable to ax */
1985 /* Create the correct label name */
1986 char* lbuf = GetLabelName (flags, label, offs);
1988 /* Add the address to the current ax value */
1989 AddCodeLine ("clc");
1990 AddCodeLine ("adc #<(%s)", lbuf);
1991 AddCodeLine ("tay");
1992 AddCodeLine ("txa");
1993 AddCodeLine ("adc #>(%s)", lbuf);
1994 AddCodeLine ("tax");
1995 AddCodeLine ("tya");
2000 /*****************************************************************************/
2002 /*****************************************************************************/
2006 void g_save (unsigned flags)
2007 /* Copy primary register to hold register. */
2009 /* Check the size and determine operation */
2010 switch (flags & CF_TYPE) {
2013 if (flags & CF_FORCECHAR) {
2014 AddCodeLine ("pha");
2020 AddCodeLine ("sta regsave");
2021 AddCodeLine ("stx regsave+1");
2025 AddCodeLine ("jsr saveeax");
2035 void g_restore (unsigned flags)
2036 /* Copy hold register to primary. */
2038 /* Check the size and determine operation */
2039 switch (flags & CF_TYPE) {
2042 if (flags & CF_FORCECHAR) {
2043 AddCodeLine ("pla");
2049 AddCodeLine ("lda regsave");
2050 AddCodeLine ("ldx regsave+1");
2054 AddCodeLine ("jsr resteax");
2064 void g_cmp (unsigned flags, unsigned long val)
2065 /* Immidiate compare. The primary register will not be changed, Z flag
2071 /* Check the size and determine operation */
2072 switch (flags & CF_TYPE) {
2075 if (flags & CF_FORCECHAR) {
2076 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2082 L = GetLocalLabel();
2083 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2084 AddCodeLine ("bne %s", LocalLabelName (L));
2085 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2090 Internal ("g_cmp: Long compares not implemented");
2100 static void oper (unsigned flags, unsigned long val, char** subs)
2101 /* Encode a binary operation. subs is a pointer to four groups of three
2103 * 0-2 --> Operate on ints
2104 * 3-5 --> Operate on unsigneds
2105 * 6-8 --> Operate on longs
2106 * 9-11 --> Operate on unsigned longs
2108 * The first subroutine names in each string group is used to encode an
2109 * operation with a zero constant, the second to encode an operation with
2110 * a 8 bit constant, and the third is used in all other cases.
2115 /* Determine the offset into the array */
2116 offs = (flags & CF_UNSIGNED)? 3 : 0;
2117 switch (flags & CF_TYPE) {
2130 /* Encode the operation */
2131 if (flags & CF_CONST) {
2132 /* Constant value given */
2133 if (val == 0 && subs [offs+0]) {
2134 /* Special case: constant with value zero */
2135 AddCodeLine ("jsr %s", subs [offs+0]);
2136 } else if (val < 0x100 && subs [offs+1]) {
2137 /* Special case: constant with high byte zero */
2138 ldaconst (val); /* Load low byte */
2139 AddCodeLine ("jsr %s", subs [offs+1]);
2141 /* Others: arbitrary constant value */
2142 g_getimmed (flags, val, 0); /* Load value */
2143 AddCodeLine ("jsr %s", subs [offs+2]);
2146 /* Value not constant (is already in (e)ax) */
2147 AddCodeLine ("jsr %s", subs [offs+2]);
2150 /* The operation will pop it's argument */
2156 void g_test (unsigned flags)
2157 /* Test the value in the primary and set the condition codes */
2159 switch (flags & CF_TYPE) {
2162 if (flags & CF_FORCECHAR) {
2163 AddCodeLine ("tax");
2169 AddCodeLine ("stx tmp1");
2170 AddCodeLine ("ora tmp1");
2174 if (flags & CF_UNSIGNED) {
2175 AddCodeLine ("jsr utsteax");
2177 AddCodeLine ("jsr tsteax");
2189 void g_push (unsigned flags, unsigned long val)
2190 /* Push the primary register or a constant value onto the stack */
2192 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2194 /* We have a constant 8 or 16 bit value */
2195 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2197 /* Handle as 8 bit value */
2199 AddCodeLine ("jsr pusha");
2203 /* Handle as 16 bit value */
2204 g_getimmed (flags, val, 0);
2205 AddCodeLine ("jsr pushax");
2210 /* Value is not 16 bit or not constant */
2211 if (flags & CF_CONST) {
2212 /* Constant 32 bit value, load into eax */
2213 g_getimmed (flags, val, 0);
2216 /* Push the primary register */
2217 switch (flags & CF_TYPE) {
2220 if (flags & CF_FORCECHAR) {
2221 /* Handle as char */
2222 AddCodeLine ("jsr pusha");
2227 AddCodeLine ("jsr pushax");
2231 AddCodeLine ("jsr pusheax");
2241 /* Adjust the stack offset */
2247 void g_swap (unsigned flags)
2248 /* Swap the primary register and the top of the stack. flags give the type
2249 * of *both* values (must have same size).
2252 switch (flags & CF_TYPE) {
2256 AddCodeLine ("jsr swapstk");
2260 AddCodeLine ("jsr swapestk");
2271 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2272 /* Call the specified subroutine name */
2274 if ((Flags & CF_FIXARGC) == 0) {
2275 /* Pass the argument count */
2278 AddCodeLine ("jsr _%s", Label);
2279 oursp += ArgSize; /* callee pops args */
2284 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2285 /* Call subroutine indirect */
2287 if ((Flags & CF_LOCAL) == 0) {
2288 /* Address is in a/x */
2289 if ((Flags & CF_FIXARGC) == 0) {
2290 /* Pass arg count */
2293 AddCodeLine ("jsr callax");
2295 /* The address is on stack, offset is on Val */
2297 CheckLocalOffs (Offs);
2298 AddCodeLine ("pha");
2299 AddCodeLine ("ldy #$%02X", Offs);
2300 AddCodeLine ("lda (sp),y");
2301 AddCodeLine ("sta jmpvec+1");
2302 AddCodeLine ("iny");
2303 AddCodeLine ("lda (sp),y");
2304 AddCodeLine ("sta jmpvec+2");
2305 AddCodeLine ("pla");
2306 AddCodeLine ("jsr jmpvec");
2309 /* Callee pops args */
2315 void g_jump (unsigned Label)
2316 /* Jump to specified internal label number */
2318 AddCodeLine ("jmp %s", LocalLabelName (Label));
2323 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2324 /* Jump to label if zero flag clear */
2326 AddCodeLine ("jne %s", LocalLabelName (label));
2331 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2332 /* Jump to label if zero flag set */
2334 AddCodeLine ("jeq %s", LocalLabelName (label));
2339 static void mod_internal (int k, char* verb1, char* verb2)
2342 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2346 AddCodeLine ("jsr %ssp", verb2);
2352 void g_space (int space)
2353 /* Create or drop space on the stack */
2356 mod_internal (-space, "inc", "addy");
2357 } else if (space > 0) {
2358 mod_internal (space, "dec", "suby");
2364 void g_cstackcheck (void)
2365 /* Check for a C stack overflow */
2367 AddCodeLine ("jsr cstkchk");
2372 void g_stackcheck (void)
2373 /* Check for a stack overflow */
2375 AddCodeLine ("jsr stkchk");
2380 void g_add (unsigned flags, unsigned long val)
2381 /* Primary = TOS + Primary */
2383 static char* ops [12] = {
2384 0, "tosadda0", "tosaddax",
2385 0, "tosadda0", "tosaddax",
2390 if (flags & CF_CONST) {
2391 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2392 g_push (flags & ~CF_CONST, 0);
2394 oper (flags, val, ops);
2399 void g_sub (unsigned flags, unsigned long val)
2400 /* Primary = TOS - Primary */
2402 static char* ops [12] = {
2403 0, "tossuba0", "tossubax",
2404 0, "tossuba0", "tossubax",
2409 if (flags & CF_CONST) {
2410 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2411 g_push (flags & ~CF_CONST, 0);
2413 oper (flags, val, ops);
2418 void g_rsub (unsigned flags, unsigned long val)
2419 /* Primary = Primary - TOS */
2421 static char* ops [12] = {
2422 0, "tosrsuba0", "tosrsubax",
2423 0, "tosrsuba0", "tosrsubax",
2427 oper (flags, val, ops);
2432 void g_mul (unsigned flags, unsigned long val)
2433 /* Primary = TOS * Primary */
2435 static char* ops [12] = {
2436 0, "tosmula0", "tosmulax",
2437 0, "tosumula0", "tosumulax",
2444 /* Do strength reduction if the value is constant and a power of two */
2445 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2446 /* Generate a shift instead */
2451 /* If the right hand side is const, the lhs is not on stack but still
2452 * in the primary register.
2454 if (flags & CF_CONST) {
2456 switch (flags & CF_TYPE) {
2459 if (flags & CF_FORCECHAR) {
2460 /* Handle some special cases */
2464 AddCodeLine ("sta tmp1");
2465 AddCodeLine ("asl a");
2466 AddCodeLine ("clc");
2467 AddCodeLine ("adc tmp1");
2471 AddCodeLine ("sta tmp1");
2472 AddCodeLine ("asl a");
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");
2484 AddCodeLine ("asl a");
2493 AddCodeLine ("jsr mulax3");
2496 AddCodeLine ("jsr mulax5");
2499 AddCodeLine ("jsr mulax10");
2511 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2512 * into the normal, non-optimized stuff.
2514 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2515 g_push (flags & ~CF_CONST, 0);
2519 /* Use long way over the stack */
2520 oper (flags, val, ops);
2525 void g_div (unsigned flags, unsigned long val)
2526 /* Primary = TOS / Primary */
2528 static char* ops [12] = {
2529 0, "tosdiva0", "tosdivax",
2530 0, "tosudiva0", "tosudivax",
2535 /* Do strength reduction if the value is constant and a power of two */
2537 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2538 /* Generate a shift instead */
2541 /* Generate a division */
2542 if (flags & CF_CONST) {
2543 /* lhs is not on stack */
2544 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2545 g_push (flags & ~CF_CONST, 0);
2547 oper (flags, val, ops);
2553 void g_mod (unsigned flags, unsigned long val)
2554 /* Primary = TOS % Primary */
2556 static char* ops [12] = {
2557 0, "tosmoda0", "tosmodax",
2558 0, "tosumoda0", "tosumodax",
2564 /* Check if we can do some cost reduction */
2565 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2566 /* We can do that with an AND operation */
2567 g_and (flags, val - 1);
2569 /* Do it the hard way... */
2570 if (flags & CF_CONST) {
2571 /* lhs is not on stack */
2572 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2573 g_push (flags & ~CF_CONST, 0);
2575 oper (flags, val, ops);
2581 void g_or (unsigned flags, unsigned long val)
2582 /* Primary = TOS | Primary */
2584 static char* ops [12] = {
2585 0, "tosora0", "tosorax",
2586 0, "tosora0", "tosorax",
2591 /* If the right hand side is const, the lhs is not on stack but still
2592 * in the primary register.
2594 if (flags & CF_CONST) {
2596 switch (flags & CF_TYPE) {
2599 if (flags & CF_FORCECHAR) {
2600 if ((val & 0xFF) != 0xFF) {
2601 AddCodeLine ("ora #$%02X", (unsigned char)val);
2609 AddCodeLine ("ora #$%02X", (unsigned char)val);
2616 AddCodeLine ("ora #$%02X", (unsigned char)val);
2625 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2626 * into the normal, non-optimized stuff.
2628 g_push (flags & ~CF_CONST, 0);
2632 /* Use long way over the stack */
2633 oper (flags, val, ops);
2638 void g_xor (unsigned flags, unsigned long val)
2639 /* Primary = TOS ^ Primary */
2641 static char* ops [12] = {
2642 0, "tosxora0", "tosxorax",
2643 0, "tosxora0", "tosxorax",
2649 /* If the right hand side is const, the lhs is not on stack but still
2650 * in the primary register.
2652 if (flags & CF_CONST) {
2654 switch (flags & CF_TYPE) {
2657 if (flags & CF_FORCECHAR) {
2658 if ((val & 0xFF) != 0) {
2659 AddCodeLine ("eor #$%02X", (unsigned char)val);
2668 AddCodeLine ("eor #$%02X", (unsigned char)val);
2671 } else if ((val & 0xFF) == 0) {
2672 AddCodeLine ("pha");
2673 AddCodeLine ("txa");
2674 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2675 AddCodeLine ("tax");
2676 AddCodeLine ("pla");
2684 AddCodeLine ("eor #$%02X", (unsigned char)val);
2694 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2695 * into the normal, non-optimized stuff.
2697 g_push (flags & ~CF_CONST, 0);
2701 /* Use long way over the stack */
2702 oper (flags, val, ops);
2707 void g_and (unsigned flags, unsigned long val)
2708 /* Primary = TOS & Primary */
2710 static char* ops [12] = {
2711 0, "tosanda0", "tosandax",
2712 0, "tosanda0", "tosandax",
2717 /* If the right hand side is const, the lhs is not on stack but still
2718 * in the primary register.
2720 if (flags & CF_CONST) {
2722 switch (flags & CF_TYPE) {
2725 if (flags & CF_FORCECHAR) {
2726 AddCodeLine ("and #$%02X", (unsigned char)val);
2731 if ((val & 0xFFFF) != 0xFFFF) {
2736 } else if (val != 0xFF) {
2737 AddCodeLine ("and #$%02X", (unsigned char)val);
2739 } else if ((val & 0xFF00) == 0xFF00) {
2740 AddCodeLine ("and #$%02X", (unsigned char)val);
2741 } else if ((val & 0x00FF) == 0x0000) {
2742 AddCodeLine ("txa");
2743 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2744 AddCodeLine ("tax");
2747 AddCodeLine ("tay");
2748 AddCodeLine ("txa");
2749 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2750 AddCodeLine ("tax");
2751 AddCodeLine ("tya");
2752 if ((val & 0x00FF) != 0x00FF) {
2753 AddCodeLine ("and #$%02X", (unsigned char)val);
2762 AddCodeLine ("stx sreg+1");
2763 AddCodeLine ("stx sreg");
2764 if ((val & 0xFF) != 0xFF) {
2765 AddCodeLine ("and #$%02X", (unsigned char)val);
2768 } else if (val == 0xFF00) {
2770 AddCodeLine ("sta sreg+1");
2771 AddCodeLine ("sta sreg");
2780 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2781 * into the normal, non-optimized stuff.
2783 g_push (flags & ~CF_CONST, 0);
2787 /* Use long way over the stack */
2788 oper (flags, val, ops);
2793 void g_asr (unsigned flags, unsigned long val)
2794 /* Primary = TOS >> Primary */
2796 static char* ops [12] = {
2797 0, "tosasra0", "tosasrax",
2798 0, "tosshra0", "tosshrax",
2803 /* If the right hand side is const, the lhs is not on stack but still
2804 * in the primary register.
2806 if (flags & CF_CONST) {
2808 switch (flags & CF_TYPE) {
2812 if (val >= 8 && (flags & CF_UNSIGNED)) {
2813 AddCodeLine ("txa");
2820 } else if (val >= 1 && val <= 4) {
2821 if (flags & CF_UNSIGNED) {
2822 AddCodeLine ("jsr shrax%ld", val);
2824 AddCodeLine ("jsr asrax%ld", val);
2834 } else if (val >= 1 && val <= 4) {
2835 if (flags & CF_UNSIGNED) {
2836 AddCodeLine ("jsr shreax%ld", val);
2838 AddCodeLine ("jsr asreax%ld", val);
2841 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2842 AddCodeLine ("txa");
2843 AddCodeLine ("ldx sreg");
2844 AddCodeLine ("ldy sreg+1");
2845 AddCodeLine ("sty sreg");
2846 AddCodeLine ("ldy #$00");
2847 AddCodeLine ("sty sreg+1");
2849 } else if (val == 16) {
2850 AddCodeLine ("ldy #$00");
2851 AddCodeLine ("ldx sreg+1");
2852 if ((flags & CF_UNSIGNED) == 0) {
2853 unsigned L = GetLocalLabel();
2854 AddCodeLine ("bpl %s", LocalLabelName (L));
2855 AddCodeLine ("dey");
2858 AddCodeLine ("lda sreg");
2859 AddCodeLine ("sty sreg+1");
2860 AddCodeLine ("sty sreg");
2869 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2870 * into the normal, non-optimized stuff.
2872 g_push (flags & ~CF_CONST, 0);
2876 /* Use long way over the stack */
2877 oper (flags, val, ops);
2882 void g_asl (unsigned flags, unsigned long val)
2883 /* Primary = TOS << Primary */
2885 static char* ops [12] = {
2886 0, "tosasla0", "tosaslax",
2887 0, "tosshla0", "tosshlax",
2893 /* If the right hand side is const, the lhs is not on stack but still
2894 * in the primary register.
2896 if (flags & CF_CONST) {
2898 switch (flags & CF_TYPE) {
2903 AddCodeLine ("tax");
2904 AddCodeLine ("lda #$00");
2910 } else if (val >= 1 && val <= 4) {
2911 if (flags & CF_UNSIGNED) {
2912 AddCodeLine ("jsr shlax%ld", val);
2914 AddCodeLine ("jsr aslax%ld", val);
2924 } else if (val >= 1 && val <= 4) {
2925 if (flags & CF_UNSIGNED) {
2926 AddCodeLine ("jsr shleax%ld", val);
2928 AddCodeLine ("jsr asleax%ld", val);
2931 } else if (val == 8) {
2932 AddCodeLine ("ldy sreg");
2933 AddCodeLine ("sty sreg+1");
2934 AddCodeLine ("stx sreg");
2935 AddCodeLine ("tax");
2936 AddCodeLine ("lda #$00");
2938 } else if (val == 16) {
2939 AddCodeLine ("stx sreg+1");
2940 AddCodeLine ("sta sreg");
2941 AddCodeLine ("lda #$00");
2942 AddCodeLine ("tax");
2951 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2952 * into the normal, non-optimized stuff.
2954 g_push (flags & ~CF_CONST, 0);
2958 /* Use long way over the stack */
2959 oper (flags, val, ops);
2964 void g_neg (unsigned flags)
2965 /* Primary = -Primary */
2967 switch (flags & CF_TYPE) {
2971 AddCodeLine ("jsr negax");
2975 AddCodeLine ("jsr negeax");
2985 void g_bneg (unsigned flags)
2986 /* Primary = !Primary */
2988 switch (flags & CF_TYPE) {
2991 AddCodeLine ("jsr bnega");
2995 AddCodeLine ("jsr bnegax");
2999 AddCodeLine ("jsr bnegeax");
3009 void g_com (unsigned flags)
3010 /* Primary = ~Primary */
3012 switch (flags & CF_TYPE) {
3016 AddCodeLine ("jsr complax");
3020 AddCodeLine ("jsr compleax");
3030 void g_inc (unsigned flags, unsigned long val)
3031 /* Increment the primary register by a given number */
3033 /* Don't inc by zero */
3038 /* Generate code for the supported types */
3040 switch (flags & CF_TYPE) {
3043 if (flags & CF_FORCECHAR) {
3044 if (CPU == CPU_65C02 && val <= 2) {
3046 AddCodeLine ("ina");
3049 AddCodeLine ("clc");
3050 AddCodeLine ("adc #$%02X", (unsigned char)val);
3057 if (CPU == CPU_65C02 && val == 1) {
3058 unsigned L = GetLocalLabel();
3059 AddCodeLine ("ina");
3060 AddCodeLine ("bne %s", LocalLabelName (L));
3061 AddCodeLine ("inx");
3063 } else if (CodeSizeFactor < 200) {
3066 AddCodeLine ("jsr incax%lu", val);
3067 } else if (val <= 255) {
3069 AddCodeLine ("jsr incaxy");
3071 g_add (flags | CF_CONST, val);
3074 /* Inline the code */
3076 if ((val & 0xFF) != 0) {
3077 unsigned L = GetLocalLabel();
3078 AddCodeLine ("clc");
3079 AddCodeLine ("adc #$%02X", (unsigned char) val);
3080 AddCodeLine ("bcc %s", LocalLabelName (L));
3081 AddCodeLine ("inx");
3085 AddCodeLine ("inx");
3088 AddCodeLine ("inx");
3091 AddCodeLine ("inx");
3094 AddCodeLine ("clc");
3095 if ((val & 0xFF) != 0) {
3096 AddCodeLine ("adc #$%02X", (unsigned char) val);
3098 AddCodeLine ("pha");
3099 AddCodeLine ("txa");
3100 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3101 AddCodeLine ("tax");
3102 AddCodeLine ("pla");
3110 AddCodeLine ("jsr inceaxy");
3112 g_add (flags | CF_CONST, val);
3124 void g_dec (unsigned flags, unsigned long val)
3125 /* Decrement the primary register by a given number */
3127 /* Don't dec by zero */
3132 /* Generate code for the supported types */
3134 switch (flags & CF_TYPE) {
3137 if (flags & CF_FORCECHAR) {
3138 if (CPU == CPU_65C02 && val <= 2) {
3140 AddCodeLine ("dea");
3143 AddCodeLine ("sec");
3144 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3151 if (CodeSizeFactor < 200) {
3152 /* Use subroutines */
3154 AddCodeLine ("jsr decax%d", (int) val);
3155 } else if (val <= 255) {
3157 AddCodeLine ("jsr decaxy");
3159 g_sub (flags | CF_CONST, val);
3162 /* Inline the code */
3164 if ((val & 0xFF) != 0) {
3165 unsigned L = GetLocalLabel();
3166 AddCodeLine ("sec");
3167 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3168 AddCodeLine ("bcs %s", LocalLabelName (L));
3169 AddCodeLine ("dex");
3173 AddCodeLine ("dex");
3176 AddCodeLine ("dex");
3179 AddCodeLine ("sec");
3180 if ((val & 0xFF) != 0) {
3181 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3183 AddCodeLine ("pha");
3184 AddCodeLine ("txa");
3185 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3186 AddCodeLine ("tax");
3187 AddCodeLine ("pla");
3195 AddCodeLine ("jsr deceaxy");
3197 g_sub (flags | CF_CONST, val);
3210 * Following are the conditional operators. They compare the TOS against
3211 * the primary and put a literal 1 in the primary if the condition is
3212 * true, otherwise they clear the primary register
3217 void g_eq (unsigned flags, unsigned long val)
3218 /* Test for equal */
3220 static char* ops [12] = {
3221 "toseq00", "toseqa0", "toseqax",
3222 "toseq00", "toseqa0", "toseqax",
3229 /* If the right hand side is const, the lhs is not on stack but still
3230 * in the primary register.
3232 if (flags & CF_CONST) {
3234 switch (flags & CF_TYPE) {
3237 if (flags & CF_FORCECHAR) {
3238 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3239 AddCodeLine ("jsr booleq");
3245 L = GetLocalLabel();
3246 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3247 AddCodeLine ("bne %s", LocalLabelName (L));
3248 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3250 AddCodeLine ("jsr booleq");
3260 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3261 * into the normal, non-optimized stuff.
3263 g_push (flags & ~CF_CONST, 0);
3267 /* Use long way over the stack */
3268 oper (flags, val, ops);
3273 void g_ne (unsigned flags, unsigned long val)
3274 /* Test for not equal */
3276 static char* ops [12] = {
3277 "tosne00", "tosnea0", "tosneax",
3278 "tosne00", "tosnea0", "tosneax",
3285 /* If the right hand side is const, the lhs is not on stack but still
3286 * in the primary register.
3288 if (flags & CF_CONST) {
3290 switch (flags & CF_TYPE) {
3293 if (flags & CF_FORCECHAR) {
3294 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3295 AddCodeLine ("jsr boolne");
3301 L = GetLocalLabel();
3302 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3303 AddCodeLine ("bne %s", LocalLabelName (L));
3304 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3306 AddCodeLine ("jsr boolne");
3316 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3317 * into the normal, non-optimized stuff.
3319 g_push (flags & ~CF_CONST, 0);
3323 /* Use long way over the stack */
3324 oper (flags, val, ops);
3329 void g_lt (unsigned flags, unsigned long val)
3330 /* Test for less than */
3332 static char* ops [12] = {
3333 "toslt00", "toslta0", "tosltax",
3334 "tosult00", "tosulta0", "tosultax",
3339 /* If the right hand side is const, the lhs is not on stack but still
3340 * in the primary register.
3342 if (flags & CF_CONST) {
3344 /* Give a warning in some special cases */
3345 if ((flags & CF_UNSIGNED) && val == 0) {
3346 Warning ("Condition is never true");
3347 AddCodeLine ("jsr return0");
3351 /* Look at the type */
3352 switch (flags & CF_TYPE) {
3355 if (flags & CF_FORCECHAR) {
3356 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3357 if (flags & CF_UNSIGNED) {
3358 AddCodeLine ("jsr boolult");
3360 AddCodeLine ("jsr boollt");
3367 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3368 /* If we have a signed compare against zero, we only need to
3369 * test the high byte.
3371 AddCodeLine ("txa");
3372 AddCodeLine ("jsr boollt");
3375 if (flags & CF_UNSIGNED) {
3376 /* Unsigned compare */
3377 /* If the low byte is zero, we must only test the high
3378 * byte. Or, citing Piotr Fusik:
3379 * Another interpretation: the code is effectively:
3380 * if (ax >= 0xAB00) goto L2;
3381 * It's equivalent to:
3382 * if ((ax >> 8) >= 0xAB) goto L2;
3384 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3385 if ((val & 0xFF) != 0) {
3386 unsigned L = GetLocalLabel();
3387 AddCodeLine ("bne %s", LocalLabelName (L));
3388 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3391 AddCodeLine ("jsr boolult");
3394 /* Signed compare */
3395 if ((val & 0xFF) == 0) {
3396 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3397 AddCodeLine ("jsr boollt");
3404 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3405 /* If we have a signed compare against zero, we only need to
3406 * test the high byte.
3408 AddCodeLine ("lda sreg+1");
3409 AddCodeLine ("jsr boollt");
3418 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3419 * into the normal, non-optimized stuff.
3421 g_push (flags & ~CF_CONST, 0);
3425 /* Use long way over the stack */
3426 oper (flags, val, ops);
3431 void g_le (unsigned flags, unsigned long val)
3432 /* Test for less than or equal to */
3434 static char* ops [12] = {
3435 "tosle00", "toslea0", "tosleax",
3436 "tosule00", "tosulea0", "tosuleax",
3442 /* If the right hand side is const, the lhs is not on stack but still
3443 * in the primary register.
3445 if (flags & CF_CONST) {
3447 /* Look at the type */
3448 switch (flags & CF_TYPE) {
3451 if (flags & CF_FORCECHAR) {
3452 if (flags & CF_UNSIGNED) {
3453 /* Unsigned compare */
3455 /* Use < instead of <= because the former gives
3456 * better code on the 6502 than the latter.
3458 AddCodeLine ("cmp #$%02X", (unsigned char)(val+1));
3459 AddCodeLine ("jsr boolult");
3461 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3462 AddCodeLine ("jsr boolule");
3465 /* Signed compare */
3467 /* Use < instead of <= because the former gives
3468 * better code on the 6502 than the latter.
3470 AddCodeLine ("cmp #$%02X", (unsigned char)(val+1));
3471 AddCodeLine ("jsr boollt");
3473 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3474 AddCodeLine ("jsr boolle");
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);
3490 unsigned L = GetLocalLabel();
3491 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3492 AddCodeLine ("bne %s", LocalLabelName (L));
3493 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3495 AddCodeLine ("jsr %s", "boolule");
3499 /* Signed compare */
3501 g_lt (flags, val+1);
3514 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3515 * into the normal, non-optimized stuff.
3517 g_push (flags & ~CF_CONST, 0);
3521 /* Use long way over the stack */
3522 oper (flags, val, ops);
3527 void g_gt (unsigned flags, unsigned long val)
3528 /* Test for greater than */
3530 static char* ops [12] = {
3531 "tosgt00", "tosgta0", "tosgtax",
3532 "tosugt00", "tosugta0", "tosugtax",
3538 /* If the right hand side is const, the lhs is not on stack but still
3539 * in the primary register.
3541 if (flags & CF_CONST) {
3543 /* Look at the type */
3544 switch (flags & CF_TYPE) {
3547 if (flags & CF_FORCECHAR) {
3548 if (flags & CF_UNSIGNED) {
3550 /* If we have a compare > 0, we will replace it by
3551 * != 0 here, since both are identical but the
3552 * latter is easier to optimize.
3554 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3555 AddCodeLine ("jsr boolne");
3556 } else if (val < 255) {
3557 /* Use >= instead of > because the former gives
3558 * better code on the 6502 than the latter.
3560 AddCodeLine ("cmp #$%02X", (unsigned char)(val+1));
3561 AddCodeLine ("jsr booluge");
3563 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3564 AddCodeLine ("jsr boolugt");
3568 /* Use >= instead of > because the former gives
3569 * better code on the 6502 than the latter.
3571 AddCodeLine ("cmp #$%02X", (unsigned char)(val+1));
3572 AddCodeLine ("jsr boolge");
3574 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3575 AddCodeLine ("jsr boolgt");
3583 if (flags & CF_UNSIGNED) {
3584 /* Unsigned compare */
3586 /* If we have a compare > 0, we will replace it by
3587 * != 0 here, since both are identical but the latter
3588 * is easier to optimize.
3590 AddCodeLine ("stx tmp1");
3591 AddCodeLine ("ora tmp1");
3592 AddCodeLine ("jsr boolne");
3593 } else if (val < 65535U) {
3594 /* Use >= instead of > because the former gives better
3595 * code on the 6502 than the latter.
3597 g_ge (flags, val+1);
3599 unsigned L = GetLocalLabel();
3600 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3601 AddCodeLine ("bne %s", LocalLabelName (L));
3602 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3604 AddCodeLine ("jsr %s", "boolugt");
3608 /* Signed compare */
3610 g_ge (flags, val+1);
3623 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3624 * into the normal, non-optimized stuff.
3626 g_push (flags & ~CF_CONST, 0);
3630 /* Use long way over the stack */
3631 oper (flags, val, ops);
3636 void g_ge (unsigned flags, unsigned long val)
3637 /* Test for greater than or equal to */
3639 static char* ops [12] = {
3640 "tosge00", "tosgea0", "tosgeax",
3641 "tosuge00", "tosugea0", "tosugeax",
3647 /* If the right hand side is const, the lhs is not on stack but still
3648 * in the primary register.
3650 if (flags & CF_CONST) {
3652 /* Give a warning in some special cases */
3653 if ((flags & CF_UNSIGNED) && val == 0) {
3654 Warning ("Condition is always true");
3655 AddCodeLine ("jsr return1");
3659 /* Look at the type */
3660 switch (flags & CF_TYPE) {
3663 if (flags & CF_FORCECHAR) {
3664 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3665 if (flags & CF_UNSIGNED) {
3666 AddCodeLine ("jsr booluge");
3668 AddCodeLine ("jsr boolge");
3675 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3676 /* If we have a signed compare against zero, we only need to
3677 * test the high byte.
3679 AddCodeLine ("txa");
3680 AddCodeLine ("jsr boolge");
3683 /* Direct code only for unsigned data types */
3684 if (flags & CF_UNSIGNED) {
3685 /* If the low byte is zero, we must only test the high
3686 * byte. Or, citing Piotr Fusik:
3687 * Another interpretation: the code is effectively:
3688 * if (ax >= 0xAB00) goto L2;
3689 * It's equivalent to:
3690 * if ((ax >> 8) >= 0xAB) goto L2;
3692 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3693 if ((val & 0xFF) != 0) {
3694 unsigned L = GetLocalLabel();
3695 AddCodeLine ("bne %s", LocalLabelName (L));
3696 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3699 AddCodeLine ("jsr booluge");
3702 /* Signed compare */
3703 if ((val & 0xFF) == 0) {
3704 AddCodeLine ("cpx #$%02X", (unsigned char) (val >> 8));
3705 AddCodeLine ("jsr boolge");
3712 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3713 /* If we have a signed compare against zero, we only need to
3714 * test the high byte.
3716 AddCodeLine ("lda sreg+1");
3717 AddCodeLine ("jsr boolge");
3726 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3727 * into the normal, non-optimized stuff.
3729 g_push (flags & ~CF_CONST, 0);
3733 /* Use long way over the stack */
3734 oper (flags, val, ops);
3739 /*****************************************************************************/
3740 /* Allocating static storage */
3741 /*****************************************************************************/
3745 void g_res (unsigned n)
3746 /* Reserve static storage, n bytes */
3748 AddDataLine ("\t.res\t%u,$00", n);
3753 void g_defdata (unsigned flags, unsigned long val, long offs)
3754 /* Define data with the size given in flags */
3756 if (flags & CF_CONST) {
3758 /* Numeric constant */
3759 switch (flags & CF_TYPE) {
3762 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
3766 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
3770 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3781 /* Create the correct label name */
3782 const char* Label = GetLabelName (flags, val, offs);
3784 /* Labels are always 16 bit */
3785 AddDataLine ("\t.addr\t%s", Label);
3792 void g_defbytes (const void* Bytes, unsigned Count)
3793 /* Output a row of bytes as a constant */
3799 /* Cast the buffer pointer */
3800 const unsigned char* Data = (const unsigned char*) Bytes;
3802 /* Output the stuff */
3805 /* How many go into this line? */
3806 if ((Chunk = Count) > 16) {
3811 /* Output one line */
3812 strcpy (Buf, "\t.byte\t");
3815 B += sprintf (B, "$%02X", *Data++);
3821 /* Output the line */
3828 void g_zerobytes (unsigned n)
3829 /* Output n bytes of data initialized with zero */
3831 AddDataLine ("\t.res\t%u,$00", n);
3836 /*****************************************************************************/
3837 /* Switch statement */
3838 /*****************************************************************************/
3842 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
3843 /* Generate code for a switch statement */
3845 unsigned NextLabel = 0;
3848 /* Setup registers and determine which compare insn to use */
3849 const char* Compare;
3852 Compare = "cmp #$%02X";
3855 Compare = "cpx #$%02X";
3858 AddCodeLine ("ldy sreg");
3859 Compare = "cpy #$%02X";
3862 AddCodeLine ("ldy sreg+1");
3863 Compare = "cpy #$%02X";
3866 Internal ("Invalid depth in g_switch: %u", Depth);
3869 /* Walk over all nodes */
3870 for (I = 0; I < CollCount (Nodes); ++I) {
3872 /* Get the next case node */
3873 CaseNode* N = CollAtUnchecked (Nodes, I);
3875 /* If we have a next label, define it */
3877 g_defcodelabel (NextLabel);
3881 /* Do the compare */
3882 AddCodeLine (Compare, CN_GetValue (N));
3884 /* If this is the last level, jump directly to the case code if found */
3887 /* Branch if equal */
3888 g_falsejump (0, CN_GetLabel (N));
3892 /* Determine the next label */
3893 if (I == CollCount (Nodes) - 1) {
3894 /* Last node means not found */
3895 g_truejump (0, DefaultLabel);
3897 /* Jump to the next check */
3898 NextLabel = GetLocalLabel ();
3899 g_truejump (0, NextLabel);
3902 /* Check the next level */
3903 g_switch (N->Nodes, DefaultLabel, Depth-1);
3908 /* If we go here, we haven't found the label */
3909 g_jump (DefaultLabel);
3914 /*****************************************************************************/
3915 /* User supplied assembler code */
3916 /*****************************************************************************/
3920 void g_asmcode (struct StrBuf* B)
3921 /* Output one line of assembler code. */
3923 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));
3928 /*****************************************************************************/
3929 /* Inlined known functions */
3930 /*****************************************************************************/
3934 void g_strlen (unsigned flags, unsigned long val, long offs)
3935 /* Inline the strlen() function */
3937 /* We need a label in both cases */
3938 unsigned label = GetLocalLabel ();
3940 /* Two different encodings */
3941 if (flags & CF_CONST) {
3943 /* The address of the string is constant. Create the correct label name */
3944 char* lbuf = GetLabelName (flags, val, offs);
3946 /* Generate the strlen code */
3947 AddCodeLine ("ldy #$FF");
3948 g_defcodelabel (label);
3949 AddCodeLine ("iny");
3950 AddCodeLine ("lda %s,y", lbuf);
3951 AddCodeLine ("bne %s", LocalLabelName (label));
3952 AddCodeLine ("tax");
3953 AddCodeLine ("tya");
3957 /* Address not constant but in primary */
3958 if (CodeSizeFactor < 400) {
3959 /* This is too much code, so call strlen instead of inlining */
3960 AddCodeLine ("jsr _strlen");
3962 /* Inline the function */
3963 AddCodeLine ("sta ptr1");
3964 AddCodeLine ("stx ptr1+1");
3965 AddCodeLine ("ldy #$FF");
3966 g_defcodelabel (label);
3967 AddCodeLine ("iny");
3968 AddCodeLine ("lda (ptr1),y");
3969 AddCodeLine ("bne %s", LocalLabelName (label));
3970 AddCodeLine ("tax");
3971 AddCodeLine ("tya");