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, unsigned offs)
101 static char lbuf [128]; /* Label name */
103 /* Create the correct label name */
104 switch (flags & CF_ADDRMASK) {
107 /* Static memory cell */
108 sprintf (lbuf, "%s+%u", LocalLabelName (label), offs);
113 sprintf (lbuf, "_%s+%u", (char*) label, offs);
117 /* Absolute address */
118 sprintf (lbuf, "$%04X", (unsigned)((label+offs) & 0xFFFF));
122 /* Variable in register bank */
123 sprintf (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, unsigned 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, unsigned 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)) {
722 if (CPU == CPU_65C02 && offs == 0) {
723 AddCodeLine ("lda (sp)");
726 AddCodeLine ("lda (sp),y");
730 AddCodeLine ("ldx #$00");
731 AddCodeLine ("lda (sp),y");
732 if ((flags & CF_UNSIGNED) == 0) {
733 unsigned L = GetLocalLabel();
734 AddCodeLine ("bpl %s", LocalLabelName (L));
742 CheckLocalOffs (offs + 1);
743 if (flags & CF_TEST) {
745 AddCodeLine ("lda (sp),y");
747 AddCodeLine ("ora (sp),y");
749 if (CodeSizeFactor > 180) {
751 AddCodeLine ("lda (sp),y");
754 AddCodeLine ("lda (sp),y");
757 AddCodeLine ("jsr ldaxysp");
764 AddCodeLine ("jsr ldeaxysp");
774 void g_getind (unsigned flags, unsigned offs)
775 /* Fetch the specified object type indirect through the primary register
776 * into the primary register
779 /* If the offset is greater than 255, add the part that is > 255 to
780 * the primary. This way we get an easy addition and use the low byte
783 offs = MakeByteOffs (flags, offs);
785 /* Handle the indirect fetch */
786 switch (flags & CF_TYPE) {
789 /* Character sized */
790 if (flags & CF_UNSIGNED) {
792 AddCodeLine ("jsr ldauidx");
795 AddCodeLine ("jsr ldaidx");
800 if (flags & CF_TEST) {
802 AddCodeLine ("sta ptr1");
803 AddCodeLine ("stx ptr1+1");
804 AddCodeLine ("lda (ptr1),y");
806 AddCodeLine ("ora (ptr1),y");
809 AddCodeLine ("jsr ldaxidx");
815 AddCodeLine ("jsr ldeaxidx");
816 if (flags & CF_TEST) {
817 AddCodeLine ("jsr tsteax");
829 void g_leasp (int offs)
830 /* Fetch the address of the specified symbol into the primary register */
832 /* Calculate the offset relative to sp */
835 /* For value 0 we do direct code */
837 AddCodeLine ("lda sp");
838 AddCodeLine ("ldx sp+1");
840 if (CodeSizeFactor < 300) {
841 ldaconst (offs); /* Load A with offset value */
842 AddCodeLine ("jsr leaasp"); /* Load effective address */
844 unsigned L = GetLocalLabel ();
845 if (CPU == CPU_65C02 && offs == 1) {
846 AddCodeLine ("lda sp");
847 AddCodeLine ("ldx sp+1");
849 AddCodeLine ("bne %s", LocalLabelName (L));
854 AddCodeLine ("ldx sp+1");
855 AddCodeLine ("adc sp");
856 AddCodeLine ("bcc %s", LocalLabelName (L));
866 void g_leavariadic (int Offs)
867 /* Fetch the address of a parameter in a variadic function into the primary
871 unsigned ArgSizeOffs;
873 /* Calculate the offset relative to sp */
876 /* Get the offset of the parameter which is stored at sp+0 on function
877 * entry and check if this offset is reachable with a byte offset.
880 ArgSizeOffs = -oursp;
881 CheckLocalOffs (ArgSizeOffs);
883 /* Get the size of all parameters. */
884 if (ArgSizeOffs == 0 && CPU == CPU_65C02) {
885 AddCodeLine ("lda (sp)");
887 ldyconst (ArgSizeOffs);
888 AddCodeLine ("lda (sp),y");
891 /* Add the value of the stackpointer */
892 if (CodeSizeFactor > 250) {
893 unsigned L = GetLocalLabel();
894 AddCodeLine ("ldx sp+1");
896 AddCodeLine ("adc sp");
897 AddCodeLine ("bcc %s", LocalLabelName (L));
901 AddCodeLine ("jsr leaasp");
904 /* Add the offset to the primary */
906 g_inc (CF_INT | CF_CONST, Offs);
907 } else if (Offs < 0) {
908 g_dec (CF_INT | CF_CONST, -Offs);
914 /*****************************************************************************/
915 /* Store into memory */
916 /*****************************************************************************/
920 void g_putstatic (unsigned flags, unsigned long label, unsigned offs)
921 /* Store the primary register into the specified static memory cell */
923 /* Create the correct label name */
924 char* lbuf = GetLabelName (flags, label, offs);
926 /* Check the size and generate the correct store operation */
927 switch (flags & CF_TYPE) {
930 AddCodeLine ("sta %s", lbuf);
934 AddCodeLine ("sta %s", lbuf);
935 AddCodeLine ("stx %s+1", lbuf);
939 AddCodeLine ("sta %s", lbuf);
940 AddCodeLine ("stx %s+1", lbuf);
941 AddCodeLine ("ldy sreg");
942 AddCodeLine ("sty %s+2", lbuf);
943 AddCodeLine ("ldy sreg+1");
944 AddCodeLine ("sty %s+3", lbuf);
955 void g_putlocal (unsigned Flags, int Offs, long Val)
956 /* Put data into local object. */
959 CheckLocalOffs (Offs);
960 switch (Flags & CF_TYPE) {
963 if (Flags & CF_CONST) {
964 AddCodeLine ("lda #$%02X", (unsigned char) Val);
966 if (CPU == CPU_65C02 && Offs == 0) {
967 AddCodeLine ("sta (sp)");
970 AddCodeLine ("sta (sp),y");
975 if (Flags & CF_CONST) {
977 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
978 AddCodeLine ("sta (sp),y");
979 if ((Flags & CF_NOKEEP) == 0) {
980 /* Place high byte into X */
983 if (CPU == CPU_65C02 && Offs == 0) {
984 AddCodeLine ("lda #$%02X", (unsigned char) Val);
985 AddCodeLine ("sta (sp)");
987 if ((Val & 0xFF) == Offs+1) {
988 /* The value we need is already in Y */
993 AddCodeLine ("lda #$%02X", (unsigned char) Val);
995 AddCodeLine ("sta (sp),y");
998 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
1000 AddCodeLine ("jsr staxysp");
1002 if (CPU == CPU_65C02 && Offs == 0) {
1003 AddCodeLine ("sta (sp)");
1005 AddCodeLine ("txa");
1006 AddCodeLine ("sta (sp),y");
1009 AddCodeLine ("sta (sp),y");
1010 AddCodeLine ("iny");
1011 AddCodeLine ("txa");
1012 AddCodeLine ("sta (sp),y");
1019 if (Flags & CF_CONST) {
1020 g_getimmed (Flags, Val, 0);
1023 AddCodeLine ("jsr steaxysp");
1034 void g_putind (unsigned Flags, unsigned Offs)
1035 /* Store the specified object type in the primary register at the address
1036 * on the top of the stack
1039 /* We can handle offsets below $100 directly, larger offsets must be added
1040 * to the address. Since a/x is in use, best code is achieved by adding
1041 * just the high byte. Be sure to check if the low byte will overflow while
1044 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1046 /* Overflow - we need to add the low byte also */
1047 AddCodeLine ("ldy #$00");
1048 AddCodeLine ("clc");
1049 AddCodeLine ("pha");
1050 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1051 AddCodeLine ("adc (sp),y");
1052 AddCodeLine ("sta (sp),y");
1053 AddCodeLine ("iny");
1054 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1055 AddCodeLine ("adc (sp),y");
1056 AddCodeLine ("sta (sp),y");
1057 AddCodeLine ("pla");
1059 /* Complete address is on stack, new offset is zero */
1062 } else if ((Offs & 0xFF00) != 0) {
1064 /* We can just add the high byte */
1065 AddCodeLine ("ldy #$01");
1066 AddCodeLine ("clc");
1067 AddCodeLine ("pha");
1068 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1069 AddCodeLine ("adc (sp),y");
1070 AddCodeLine ("sta (sp),y");
1071 AddCodeLine ("pla");
1073 /* Offset is now just the low byte */
1077 /* Check the size and determine operation */
1078 switch (Flags & CF_TYPE) {
1082 AddCodeLine ("jsr staspidx");
1087 AddCodeLine ("jsr staxspidx");
1092 AddCodeLine ("jsr steaxspidx");
1100 /* Pop the argument which is always a pointer */
1106 /*****************************************************************************/
1107 /* type conversion and similiar stuff */
1108 /*****************************************************************************/
1112 void g_toslong (unsigned flags)
1113 /* Make sure, the value on TOS is a long. Convert if necessary */
1115 switch (flags & CF_TYPE) {
1119 if (flags & CF_UNSIGNED) {
1120 AddCodeLine ("jsr tosulong");
1122 AddCodeLine ("jsr toslong");
1137 void g_tosint (unsigned flags)
1138 /* Make sure, the value on TOS is an int. Convert if necessary */
1140 switch (flags & CF_TYPE) {
1147 AddCodeLine ("jsr tosint");
1158 void g_reglong (unsigned flags)
1159 /* Make sure, the value in the primary register a long. Convert if necessary */
1161 switch (flags & CF_TYPE) {
1165 if (flags & CF_UNSIGNED) {
1166 if (CodeSizeFactor >= 200) {
1168 AddCodeLine ("sty sreg");
1169 AddCodeLine ("sty sreg+1");
1171 AddCodeLine ("jsr axulong");
1174 AddCodeLine ("jsr axlong");
1188 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1189 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1190 * value, that corresponds to the value on TOS, rhs corresponds to the value
1191 * in (e)ax. The return value is the the flags value for the resulting type.
1194 unsigned ltype, rtype;
1197 /* Get the type spec from the flags */
1198 ltype = lhs & CF_TYPE;
1199 rtype = rhs & CF_TYPE;
1201 /* Check if a conversion is needed */
1202 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1203 /* We must promote the primary register to long */
1205 /* Get the new rhs type */
1206 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1208 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1209 /* We must promote the lhs to long */
1215 /* Get the new rhs type */
1216 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1220 /* Determine the result type for the operation:
1221 * - The result is const if both operands are const.
1222 * - The result is unsigned if one of the operands is unsigned.
1223 * - The result is long if one of the operands is long.
1224 * - Otherwise the result is int sized.
1226 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1227 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1228 if (rtype == CF_LONG || ltype == CF_LONG) {
1238 unsigned g_typecast (unsigned lhs, unsigned rhs)
1239 /* Cast the value in the primary register to the operand size that is flagged
1240 * by the lhs value. Return the result value.
1243 unsigned ltype, rtype;
1245 /* Get the type spec from the flags */
1246 ltype = lhs & CF_TYPE;
1247 rtype = rhs & CF_TYPE;
1249 /* Check if a conversion is needed */
1250 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1251 /* We must promote the primary register to long */
1255 /* Do not need any other action. If the left type is int, and the primary
1256 * register is long, it will be automagically truncated. If the right hand
1257 * side is const, it is not located in the primary register and handled by
1258 * the expression parser code.
1261 /* Result is const if the right hand side was const */
1262 lhs |= (rhs & CF_CONST);
1264 /* The resulting type is that of the left hand side (that's why you called
1272 void g_scale (unsigned flags, long val)
1273 /* Scale the value in the primary register by the given value. If val is positive,
1274 * scale up, is val is negative, scale down. This function is used to scale
1275 * the operands or results of pointer arithmetic by the size of the type, the
1276 * pointer points to.
1281 /* Value may not be zero */
1283 Internal ("Data type has no size");
1284 } else if (val > 0) {
1287 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1289 /* Factor is 2, 4, 8 and 16, use special function */
1290 switch (flags & CF_TYPE) {
1293 if (flags & CF_FORCECHAR) {
1295 AddCodeLine ("asl a");
1302 if (CodeSizeFactor >= (p2+1)*130U) {
1303 AddCodeLine ("stx tmp1");
1305 AddCodeLine ("asl a");
1306 AddCodeLine ("rol tmp1");
1308 AddCodeLine ("ldx tmp1");
1310 if (flags & CF_UNSIGNED) {
1311 AddCodeLine ("jsr shlax%d", p2);
1313 AddCodeLine ("jsr aslax%d", p2);
1319 if (flags & CF_UNSIGNED) {
1320 AddCodeLine ("jsr shleax%d", p2);
1322 AddCodeLine ("jsr asleax%d", p2);
1331 } else if (val != 1) {
1333 /* Use a multiplication instead */
1334 g_mul (flags | CF_CONST, val);
1342 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1344 /* Factor is 2, 4, 8 and 16 use special function */
1345 switch (flags & CF_TYPE) {
1348 if (flags & CF_FORCECHAR) {
1349 if (flags & CF_UNSIGNED) {
1351 AddCodeLine ("lsr a");
1354 } else if (p2 <= 2) {
1355 AddCodeLine ("cmp #$80");
1356 AddCodeLine ("ror a");
1363 if (flags & CF_UNSIGNED) {
1364 if (CodeSizeFactor >= (p2+1)*130U) {
1365 AddCodeLine ("stx tmp1");
1367 AddCodeLine ("lsr tmp1");
1368 AddCodeLine ("ror a");
1370 AddCodeLine ("ldx tmp1");
1372 AddCodeLine ("jsr lsrax%d", p2);
1375 if (CodeSizeFactor >= (p2+1)*150U) {
1376 AddCodeLine ("stx tmp1");
1378 AddCodeLine ("cpx #$80");
1379 AddCodeLine ("ror tmp1");
1380 AddCodeLine ("ror a");
1382 AddCodeLine ("ldx tmp1");
1384 AddCodeLine ("jsr asrax%d", p2);
1390 if (flags & CF_UNSIGNED) {
1391 AddCodeLine ("jsr lsreax%d", p2);
1393 AddCodeLine ("jsr asreax%d", p2);
1402 } else if (val != 1) {
1404 /* Use a division instead */
1405 g_div (flags | CF_CONST, val);
1413 /*****************************************************************************/
1414 /* Adds and subs of variables fix a fixed address */
1415 /*****************************************************************************/
1419 void g_addlocal (unsigned flags, int offs)
1420 /* Add a local variable to ax */
1424 /* Correct the offset and check it */
1426 CheckLocalOffs (offs);
1428 switch (flags & CF_TYPE) {
1431 L = GetLocalLabel();
1432 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1433 AddCodeLine ("clc");
1434 AddCodeLine ("adc (sp),y");
1435 AddCodeLine ("bcc %s", LocalLabelName (L));
1436 AddCodeLine ("inx");
1441 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1442 AddCodeLine ("clc");
1443 AddCodeLine ("adc (sp),y");
1444 AddCodeLine ("pha");
1445 AddCodeLine ("txa");
1446 AddCodeLine ("iny");
1447 AddCodeLine ("adc (sp),y");
1448 AddCodeLine ("tax");
1449 AddCodeLine ("pla");
1453 /* Do it the old way */
1455 g_getlocal (flags, offs);
1467 void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
1468 /* Add a static variable to ax */
1472 /* Create the correct label name */
1473 char* lbuf = GetLabelName (flags, label, offs);
1475 switch (flags & CF_TYPE) {
1478 L = GetLocalLabel();
1479 AddCodeLine ("clc");
1480 AddCodeLine ("adc %s", lbuf);
1481 AddCodeLine ("bcc %s", LocalLabelName (L));
1482 AddCodeLine ("inx");
1487 AddCodeLine ("clc");
1488 AddCodeLine ("adc %s", lbuf);
1489 AddCodeLine ("tay");
1490 AddCodeLine ("txa");
1491 AddCodeLine ("adc %s+1", lbuf);
1492 AddCodeLine ("tax");
1493 AddCodeLine ("tya");
1497 /* Do it the old way */
1499 g_getstatic (flags, label, offs);
1511 /*****************************************************************************/
1512 /* Special op= functions */
1513 /*****************************************************************************/
1517 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
1519 /* Emit += for a static variable */
1521 /* Create the correct label name */
1522 char* lbuf = GetLabelName (flags, label, offs);
1524 /* Check the size and determine operation */
1525 switch (flags & CF_TYPE) {
1528 if (flags & CF_FORCECHAR) {
1529 AddCodeLine ("ldx #$00");
1530 if (flags & CF_CONST) {
1532 AddCodeLine ("inc %s", lbuf);
1533 AddCodeLine ("lda %s", lbuf);
1535 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1536 AddCodeLine ("clc");
1537 AddCodeLine ("adc %s", lbuf);
1538 AddCodeLine ("sta %s", lbuf);
1541 AddCodeLine ("clc");
1542 AddCodeLine ("adc %s", lbuf);
1543 AddCodeLine ("sta %s", lbuf);
1545 if ((flags & CF_UNSIGNED) == 0) {
1546 unsigned L = GetLocalLabel();
1547 AddCodeLine ("bpl %s", LocalLabelName (L));
1548 AddCodeLine ("dex");
1556 if (flags & CF_CONST) {
1558 unsigned L = GetLocalLabel ();
1559 AddCodeLine ("inc %s", lbuf);
1560 AddCodeLine ("bne %s", LocalLabelName (L));
1561 AddCodeLine ("inc %s+1", lbuf);
1563 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1564 AddCodeLine ("ldx %s+1", lbuf);
1566 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1567 AddCodeLine ("clc");
1568 AddCodeLine ("adc %s", lbuf);
1569 AddCodeLine ("sta %s", lbuf);
1571 unsigned L = GetLocalLabel ();
1572 AddCodeLine ("bcc %s", LocalLabelName (L));
1573 AddCodeLine ("inc %s+1", lbuf);
1575 AddCodeLine ("ldx %s+1", lbuf);
1577 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1578 AddCodeLine ("adc %s+1", lbuf);
1579 AddCodeLine ("sta %s+1", lbuf);
1580 AddCodeLine ("tax");
1581 AddCodeLine ("lda %s", lbuf);
1585 AddCodeLine ("clc");
1586 AddCodeLine ("adc %s", lbuf);
1587 AddCodeLine ("sta %s", lbuf);
1588 AddCodeLine ("txa");
1589 AddCodeLine ("adc %s+1", lbuf);
1590 AddCodeLine ("sta %s+1", lbuf);
1591 AddCodeLine ("tax");
1592 AddCodeLine ("lda %s", lbuf);
1597 if (flags & CF_CONST) {
1599 AddCodeLine ("ldy #<(%s)", lbuf);
1600 AddCodeLine ("sty ptr1");
1601 AddCodeLine ("ldy #>(%s+1)", lbuf);
1603 AddCodeLine ("jsr laddeq1");
1605 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1606 AddCodeLine ("jsr laddeqa");
1609 g_getstatic (flags, label, offs);
1611 g_putstatic (flags, label, offs);
1614 AddCodeLine ("ldy #<(%s)", lbuf);
1615 AddCodeLine ("sty ptr1");
1616 AddCodeLine ("ldy #>(%s+1)", lbuf);
1617 AddCodeLine ("jsr laddeq");
1628 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1629 /* Emit += for a local variable */
1631 /* Calculate the true offset, check it, load it into Y */
1633 CheckLocalOffs (offs);
1635 /* Check the size and determine operation */
1636 switch (flags & CF_TYPE) {
1639 if (flags & CF_FORCECHAR) {
1641 AddCodeLine ("ldx #$00");
1642 if (flags & CF_CONST) {
1643 AddCodeLine ("clc");
1644 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1645 AddCodeLine ("adc (sp),y");
1646 AddCodeLine ("sta (sp),y");
1648 AddCodeLine ("clc");
1649 AddCodeLine ("adc (sp),y");
1650 AddCodeLine ("sta (sp),y");
1652 if ((flags & CF_UNSIGNED) == 0) {
1653 unsigned L = GetLocalLabel();
1654 AddCodeLine ("bpl %s", LocalLabelName (L));
1655 AddCodeLine ("dex");
1664 if (flags & CF_CONST) {
1665 if (CodeSizeFactor >= 400) {
1666 AddCodeLine ("clc");
1667 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1668 AddCodeLine ("adc (sp),y");
1669 AddCodeLine ("sta (sp),y");
1670 AddCodeLine ("iny");
1671 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1672 AddCodeLine ("adc (sp),y");
1673 AddCodeLine ("sta (sp),y");
1674 AddCodeLine ("tax");
1675 AddCodeLine ("dey");
1676 AddCodeLine ("lda (sp),y");
1678 g_getimmed (flags, val, 0);
1679 AddCodeLine ("jsr addeqysp");
1682 AddCodeLine ("jsr addeqysp");
1687 if (flags & CF_CONST) {
1688 g_getimmed (flags, val, 0);
1691 AddCodeLine ("jsr laddeqysp");
1701 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1702 /* Emit += for the location with address in ax */
1704 /* If the offset is too large for a byte register, add the high byte
1705 * of the offset to the primary. Beware: We need a special correction
1706 * if the offset in the low byte will overflow in the operation.
1708 offs = MakeByteOffs (flags, offs);
1710 /* Check the size and determine operation */
1711 switch (flags & CF_TYPE) {
1714 AddCodeLine ("sta ptr1");
1715 AddCodeLine ("stx ptr1+1");
1716 AddCodeLine ("ldy #$%02X", offs);
1717 AddCodeLine ("ldx #$00");
1718 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1719 AddCodeLine ("clc");
1720 AddCodeLine ("adc (ptr1),y");
1721 AddCodeLine ("sta (ptr1),y");
1725 if (CodeSizeFactor >= 200) {
1726 /* Lots of code, use only if size is not important */
1727 AddCodeLine ("sta ptr1");
1728 AddCodeLine ("stx ptr1+1");
1729 AddCodeLine ("ldy #$%02X", offs);
1730 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1731 AddCodeLine ("clc");
1732 AddCodeLine ("adc (ptr1),y");
1733 AddCodeLine ("sta (ptr1),y");
1734 AddCodeLine ("pha");
1735 AddCodeLine ("iny");
1736 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1737 AddCodeLine ("adc (ptr1),y");
1738 AddCodeLine ("sta (ptr1),y");
1739 AddCodeLine ("tax");
1740 AddCodeLine ("pla");
1746 AddCodeLine ("jsr pushax"); /* Push the address */
1747 push (CF_PTR); /* Correct the internal sp */
1748 g_getind (flags, offs); /* Fetch the value */
1749 g_inc (flags, val); /* Increment value in primary */
1750 g_putind (flags, offs); /* Store the value back */
1760 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1762 /* Emit -= for a static variable */
1764 /* Create the correct label name */
1765 char* lbuf = GetLabelName (flags, label, offs);
1767 /* Check the size and determine operation */
1768 switch (flags & CF_TYPE) {
1771 if (flags & CF_FORCECHAR) {
1772 AddCodeLine ("ldx #$00");
1773 if (flags & CF_CONST) {
1775 AddCodeLine ("dec %s", lbuf);
1776 AddCodeLine ("lda %s", lbuf);
1778 AddCodeLine ("sec");
1779 AddCodeLine ("lda %s", lbuf);
1780 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1781 AddCodeLine ("sta %s", lbuf);
1784 AddCodeLine ("sec");
1785 AddCodeLine ("sta tmp1");
1786 AddCodeLine ("lda %s", lbuf);
1787 AddCodeLine ("sbc tmp1");
1788 AddCodeLine ("sta %s", lbuf);
1790 if ((flags & CF_UNSIGNED) == 0) {
1791 unsigned L = GetLocalLabel();
1792 AddCodeLine ("bpl %s", LocalLabelName (L));
1793 AddCodeLine ("dex");
1801 AddCodeLine ("sec");
1802 if (flags & CF_CONST) {
1803 AddCodeLine ("lda %s", lbuf);
1804 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1805 AddCodeLine ("sta %s", lbuf);
1807 unsigned L = GetLocalLabel ();
1808 AddCodeLine ("bcs %s", LocalLabelName (L));
1809 AddCodeLine ("dec %s+1", lbuf);
1811 AddCodeLine ("ldx %s+1", lbuf);
1813 AddCodeLine ("lda %s+1", lbuf);
1814 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1815 AddCodeLine ("sta %s+1", lbuf);
1816 AddCodeLine ("tax");
1817 AddCodeLine ("lda %s", lbuf);
1820 AddCodeLine ("sta tmp1");
1821 AddCodeLine ("lda %s", lbuf);
1822 AddCodeLine ("sbc tmp1");
1823 AddCodeLine ("sta %s", lbuf);
1824 AddCodeLine ("stx tmp1");
1825 AddCodeLine ("lda %s+1", lbuf);
1826 AddCodeLine ("sbc tmp1");
1827 AddCodeLine ("sta %s+1", lbuf);
1828 AddCodeLine ("tax");
1829 AddCodeLine ("lda %s", lbuf);
1834 if (flags & CF_CONST) {
1836 AddCodeLine ("ldy #<(%s)", lbuf);
1837 AddCodeLine ("sty ptr1");
1838 AddCodeLine ("ldy #>(%s+1)", lbuf);
1839 AddCodeLine ("lda #$%02X", (unsigned char)val);
1840 AddCodeLine ("jsr lsubeqa");
1842 g_getstatic (flags, label, offs);
1844 g_putstatic (flags, label, offs);
1847 AddCodeLine ("ldy #<(%s)", lbuf);
1848 AddCodeLine ("sty ptr1");
1849 AddCodeLine ("ldy #>(%s+1)", lbuf);
1850 AddCodeLine ("jsr lsubeq");
1861 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1862 /* Emit -= for a local variable */
1864 /* Calculate the true offset, check it, load it into Y */
1866 CheckLocalOffs (offs);
1868 /* Check the size and determine operation */
1869 switch (flags & CF_TYPE) {
1872 if (flags & CF_FORCECHAR) {
1874 AddCodeLine ("ldx #$00");
1875 AddCodeLine ("sec");
1876 if (flags & CF_CONST) {
1877 AddCodeLine ("lda (sp),y");
1878 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1880 AddCodeLine ("sta tmp1");
1881 AddCodeLine ("lda (sp),y");
1882 AddCodeLine ("sbc tmp1");
1884 AddCodeLine ("sta (sp),y");
1885 if ((flags & CF_UNSIGNED) == 0) {
1886 unsigned L = GetLocalLabel();
1887 AddCodeLine ("bpl %s", LocalLabelName (L));
1888 AddCodeLine ("dex");
1896 if (flags & CF_CONST) {
1897 g_getimmed (flags, val, 0);
1900 AddCodeLine ("jsr subeqysp");
1904 if (flags & CF_CONST) {
1905 g_getimmed (flags, val, 0);
1908 AddCodeLine ("jsr lsubeqysp");
1918 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
1919 /* Emit -= for the location with address in ax */
1921 /* If the offset is too large for a byte register, add the high byte
1922 * of the offset to the primary. Beware: We need a special correction
1923 * if the offset in the low byte will overflow in the operation.
1925 offs = MakeByteOffs (flags, offs);
1927 /* Check the size and determine operation */
1928 switch (flags & CF_TYPE) {
1931 AddCodeLine ("sta ptr1");
1932 AddCodeLine ("stx ptr1+1");
1933 AddCodeLine ("ldy #$%02X", offs);
1934 AddCodeLine ("ldx #$00");
1935 AddCodeLine ("lda (ptr1),y");
1936 AddCodeLine ("sec");
1937 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1938 AddCodeLine ("sta (ptr1),y");
1942 if (CodeSizeFactor >= 200) {
1943 /* Lots of code, use only if size is not important */
1944 AddCodeLine ("sta ptr1");
1945 AddCodeLine ("stx ptr1+1");
1946 AddCodeLine ("ldy #$%02X", offs);
1947 AddCodeLine ("lda (ptr1),y");
1948 AddCodeLine ("sec");
1949 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1950 AddCodeLine ("sta (ptr1),y");
1951 AddCodeLine ("pha");
1952 AddCodeLine ("iny");
1953 AddCodeLine ("lda (ptr1),y");
1954 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1955 AddCodeLine ("sta (ptr1),y");
1956 AddCodeLine ("tax");
1957 AddCodeLine ("pla");
1963 AddCodeLine ("jsr pushax"); /* Push the address */
1964 push (CF_PTR); /* Correct the internal sp */
1965 g_getind (flags, offs); /* Fetch the value */
1966 g_dec (flags, val); /* Increment value in primary */
1967 g_putind (flags, offs); /* Store the value back */
1977 /*****************************************************************************/
1978 /* Add a variable address to the value in ax */
1979 /*****************************************************************************/
1983 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
1984 /* Add the address of a local variable to ax */
1988 /* Add the offset */
1991 /* We cannot address more then 256 bytes of locals anyway */
1992 L = GetLocalLabel();
1993 CheckLocalOffs (offs);
1994 AddCodeLine ("clc");
1995 AddCodeLine ("adc #$%02X", offs & 0xFF);
1996 /* Do also skip the CLC insn below */
1997 AddCodeLine ("bcc %s", LocalLabelName (L));
1998 AddCodeLine ("inx");
2001 /* Add the current stackpointer value */
2002 AddCodeLine ("clc");
2004 /* Label was used above */
2007 AddCodeLine ("adc sp");
2008 AddCodeLine ("tay");
2009 AddCodeLine ("txa");
2010 AddCodeLine ("adc sp+1");
2011 AddCodeLine ("tax");
2012 AddCodeLine ("tya");
2017 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2018 /* Add the address of a static variable to ax */
2020 /* Create the correct label name */
2021 char* lbuf = GetLabelName (flags, label, offs);
2023 /* Add the address to the current ax value */
2024 AddCodeLine ("clc");
2025 AddCodeLine ("adc #<(%s)", lbuf);
2026 AddCodeLine ("tay");
2027 AddCodeLine ("txa");
2028 AddCodeLine ("adc #>(%s)", lbuf);
2029 AddCodeLine ("tax");
2030 AddCodeLine ("tya");
2035 /*****************************************************************************/
2037 /*****************************************************************************/
2041 void g_save (unsigned flags)
2042 /* Copy primary register to hold register. */
2044 /* Check the size and determine operation */
2045 switch (flags & CF_TYPE) {
2048 if (flags & CF_FORCECHAR) {
2049 AddCodeLine ("pha");
2055 AddCodeLine ("sta regsave");
2056 AddCodeLine ("stx regsave+1");
2060 AddCodeLine ("jsr saveeax");
2070 void g_restore (unsigned flags)
2071 /* Copy hold register to primary. */
2073 /* Check the size and determine operation */
2074 switch (flags & CF_TYPE) {
2077 if (flags & CF_FORCECHAR) {
2078 AddCodeLine ("pla");
2084 AddCodeLine ("lda regsave");
2085 AddCodeLine ("ldx regsave+1");
2089 AddCodeLine ("jsr resteax");
2099 void g_cmp (unsigned flags, unsigned long val)
2100 /* Immidiate compare. The primary register will not be changed, Z flag
2106 /* Check the size and determine operation */
2107 switch (flags & CF_TYPE) {
2110 if (flags & CF_FORCECHAR) {
2111 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2117 L = GetLocalLabel();
2118 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2119 AddCodeLine ("bne %s", LocalLabelName (L));
2120 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2125 Internal ("g_cmp: Long compares not implemented");
2135 static void oper (unsigned flags, unsigned long val, char** subs)
2136 /* Encode a binary operation. subs is a pointer to four groups of three
2138 * 0-2 --> Operate on ints
2139 * 3-5 --> Operate on unsigneds
2140 * 6-8 --> Operate on longs
2141 * 9-11 --> Operate on unsigned longs
2143 * The first subroutine names in each string group is used to encode an
2144 * operation with a zero constant, the second to encode an operation with
2145 * a 8 bit constant, and the third is used in all other cases.
2150 /* Determine the offset into the array */
2151 offs = (flags & CF_UNSIGNED)? 3 : 0;
2152 switch (flags & CF_TYPE) {
2165 /* Encode the operation */
2166 if (flags & CF_CONST) {
2167 /* Constant value given */
2168 if (val == 0 && subs [offs+0]) {
2169 /* Special case: constant with value zero */
2170 AddCodeLine ("jsr %s", subs [offs+0]);
2171 } else if (val < 0x100 && subs [offs+1]) {
2172 /* Special case: constant with high byte zero */
2173 ldaconst (val); /* Load low byte */
2174 AddCodeLine ("jsr %s", subs [offs+1]);
2176 /* Others: arbitrary constant value */
2177 g_getimmed (flags, val, 0); /* Load value */
2178 AddCodeLine ("jsr %s", subs [offs+2]);
2181 /* Value not constant (is already in (e)ax) */
2182 AddCodeLine ("jsr %s", subs [offs+2]);
2185 /* The operation will pop it's argument */
2191 void g_test (unsigned flags)
2192 /* Test the value in the primary and set the condition codes */
2194 switch (flags & CF_TYPE) {
2197 if (flags & CF_FORCECHAR) {
2198 AddCodeLine ("tax");
2204 AddCodeLine ("stx tmp1");
2205 AddCodeLine ("ora tmp1");
2209 if (flags & CF_UNSIGNED) {
2210 AddCodeLine ("jsr utsteax");
2212 AddCodeLine ("jsr tsteax");
2224 void g_push (unsigned flags, unsigned long val)
2225 /* Push the primary register or a constant value onto the stack */
2227 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2229 /* We have a constant 8 or 16 bit value */
2230 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2232 /* Handle as 8 bit value */
2234 AddCodeLine ("jsr pusha");
2238 /* Handle as 16 bit value */
2239 g_getimmed (flags, val, 0);
2240 AddCodeLine ("jsr pushax");
2245 /* Value is not 16 bit or not constant */
2246 if (flags & CF_CONST) {
2247 /* Constant 32 bit value, load into eax */
2248 g_getimmed (flags, val, 0);
2251 /* Push the primary register */
2252 switch (flags & CF_TYPE) {
2255 if (flags & CF_FORCECHAR) {
2256 /* Handle as char */
2257 AddCodeLine ("jsr pusha");
2262 AddCodeLine ("jsr pushax");
2266 AddCodeLine ("jsr pusheax");
2276 /* Adjust the stack offset */
2282 void g_swap (unsigned flags)
2283 /* Swap the primary register and the top of the stack. flags give the type
2284 * of *both* values (must have same size).
2287 switch (flags & CF_TYPE) {
2291 AddCodeLine ("jsr swapstk");
2295 AddCodeLine ("jsr swapestk");
2306 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2307 /* Call the specified subroutine name */
2309 if ((Flags & CF_FIXARGC) == 0) {
2310 /* Pass the argument count */
2313 AddCodeLine ("jsr _%s", Label);
2314 oursp += ArgSize; /* callee pops args */
2319 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2320 /* Call subroutine indirect */
2322 if ((Flags & CF_LOCAL) == 0) {
2323 /* Address is in a/x */
2324 if ((Flags & CF_FIXARGC) == 0) {
2325 /* Pass arg count */
2328 AddCodeLine ("jsr callax");
2330 /* The address is on stack, offset is on Val */
2332 CheckLocalOffs (Offs);
2333 AddCodeLine ("pha");
2334 AddCodeLine ("ldy #$%02X", Offs);
2335 AddCodeLine ("lda (sp),y");
2336 AddCodeLine ("sta jmpvec+1");
2337 AddCodeLine ("iny");
2338 AddCodeLine ("lda (sp),y");
2339 AddCodeLine ("sta jmpvec+2");
2340 AddCodeLine ("pla");
2341 AddCodeLine ("jsr jmpvec");
2344 /* Callee pops args */
2350 void g_jump (unsigned Label)
2351 /* Jump to specified internal label number */
2353 AddCodeLine ("jmp %s", LocalLabelName (Label));
2358 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2359 /* Jump to label if zero flag clear */
2361 AddCodeLine ("jne %s", LocalLabelName (label));
2366 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2367 /* Jump to label if zero flag set */
2369 AddCodeLine ("jeq %s", LocalLabelName (label));
2374 static void mod_internal (int k, char* verb1, char* verb2)
2377 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2381 AddCodeLine ("jsr %ssp", verb2);
2387 void g_space (int space)
2388 /* Create or drop space on the stack */
2391 mod_internal (-space, "inc", "addy");
2392 } else if (space > 0) {
2393 mod_internal (space, "dec", "suby");
2399 void g_cstackcheck (void)
2400 /* Check for a C stack overflow */
2402 AddCodeLine ("jsr cstkchk");
2407 void g_stackcheck (void)
2408 /* Check for a stack overflow */
2410 AddCodeLine ("jsr stkchk");
2415 void g_add (unsigned flags, unsigned long val)
2416 /* Primary = TOS + Primary */
2418 static char* ops [12] = {
2419 0, "tosadda0", "tosaddax",
2420 0, "tosadda0", "tosaddax",
2425 if (flags & CF_CONST) {
2426 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2427 g_push (flags & ~CF_CONST, 0);
2429 oper (flags, val, ops);
2434 void g_sub (unsigned flags, unsigned long val)
2435 /* Primary = TOS - Primary */
2437 static char* ops [12] = {
2438 0, "tossuba0", "tossubax",
2439 0, "tossuba0", "tossubax",
2444 if (flags & CF_CONST) {
2445 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2446 g_push (flags & ~CF_CONST, 0);
2448 oper (flags, val, ops);
2453 void g_rsub (unsigned flags, unsigned long val)
2454 /* Primary = Primary - TOS */
2456 static char* ops [12] = {
2457 0, "tosrsuba0", "tosrsubax",
2458 0, "tosrsuba0", "tosrsubax",
2462 oper (flags, val, ops);
2467 void g_mul (unsigned flags, unsigned long val)
2468 /* Primary = TOS * Primary */
2470 static char* ops [12] = {
2471 0, "tosmula0", "tosmulax",
2472 0, "tosumula0", "tosumulax",
2479 /* Do strength reduction if the value is constant and a power of two */
2480 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2481 /* Generate a shift instead */
2486 /* If the right hand side is const, the lhs is not on stack but still
2487 * in the primary register.
2489 if (flags & CF_CONST) {
2491 switch (flags & CF_TYPE) {
2494 if (flags & CF_FORCECHAR) {
2495 /* Handle some special cases */
2499 AddCodeLine ("sta tmp1");
2500 AddCodeLine ("asl a");
2501 AddCodeLine ("clc");
2502 AddCodeLine ("adc tmp1");
2506 AddCodeLine ("sta tmp1");
2507 AddCodeLine ("asl a");
2508 AddCodeLine ("asl a");
2509 AddCodeLine ("clc");
2510 AddCodeLine ("adc tmp1");
2514 AddCodeLine ("sta tmp1");
2515 AddCodeLine ("asl a");
2516 AddCodeLine ("asl a");
2517 AddCodeLine ("clc");
2518 AddCodeLine ("adc tmp1");
2519 AddCodeLine ("asl a");
2528 AddCodeLine ("jsr mulax3");
2531 AddCodeLine ("jsr mulax5");
2534 AddCodeLine ("jsr mulax10");
2546 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2547 * into the normal, non-optimized stuff.
2549 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2550 g_push (flags & ~CF_CONST, 0);
2554 /* Use long way over the stack */
2555 oper (flags, val, ops);
2560 void g_div (unsigned flags, unsigned long val)
2561 /* Primary = TOS / Primary */
2563 static char* ops [12] = {
2564 0, "tosdiva0", "tosdivax",
2565 0, "tosudiva0", "tosudivax",
2570 /* Do strength reduction if the value is constant and a power of two */
2572 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2573 /* Generate a shift instead */
2576 /* Generate a division */
2577 if (flags & CF_CONST) {
2578 /* lhs is not on stack */
2579 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2580 g_push (flags & ~CF_CONST, 0);
2582 oper (flags, val, ops);
2588 void g_mod (unsigned flags, unsigned long val)
2589 /* Primary = TOS % Primary */
2591 static char* ops [12] = {
2592 0, "tosmoda0", "tosmodax",
2593 0, "tosumoda0", "tosumodax",
2599 /* Check if we can do some cost reduction */
2600 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2601 /* We can do that with an AND operation */
2602 g_and (flags, val - 1);
2604 /* Do it the hard way... */
2605 if (flags & CF_CONST) {
2606 /* lhs is not on stack */
2607 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2608 g_push (flags & ~CF_CONST, 0);
2610 oper (flags, val, ops);
2616 void g_or (unsigned flags, unsigned long val)
2617 /* Primary = TOS | Primary */
2619 static char* ops [12] = {
2620 0, "tosora0", "tosorax",
2621 0, "tosora0", "tosorax",
2626 /* If the right hand side is const, the lhs is not on stack but still
2627 * in the primary register.
2629 if (flags & CF_CONST) {
2631 switch (flags & CF_TYPE) {
2634 if (flags & CF_FORCECHAR) {
2635 if ((val & 0xFF) != 0xFF) {
2636 AddCodeLine ("ora #$%02X", (unsigned char)val);
2644 AddCodeLine ("ora #$%02X", (unsigned char)val);
2651 AddCodeLine ("ora #$%02X", (unsigned char)val);
2660 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2661 * into the normal, non-optimized stuff.
2663 g_push (flags & ~CF_CONST, 0);
2667 /* Use long way over the stack */
2668 oper (flags, val, ops);
2673 void g_xor (unsigned flags, unsigned long val)
2674 /* Primary = TOS ^ Primary */
2676 static char* ops [12] = {
2677 0, "tosxora0", "tosxorax",
2678 0, "tosxora0", "tosxorax",
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) != 0) {
2694 AddCodeLine ("eor #$%02X", (unsigned char)val);
2703 AddCodeLine ("eor #$%02X", (unsigned char)val);
2706 } else if ((val & 0xFF) == 0) {
2707 AddCodeLine ("pha");
2708 AddCodeLine ("txa");
2709 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2710 AddCodeLine ("tax");
2711 AddCodeLine ("pla");
2719 AddCodeLine ("eor #$%02X", (unsigned char)val);
2729 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2730 * into the normal, non-optimized stuff.
2732 g_push (flags & ~CF_CONST, 0);
2736 /* Use long way over the stack */
2737 oper (flags, val, ops);
2742 void g_and (unsigned flags, unsigned long val)
2743 /* Primary = TOS & Primary */
2745 static char* ops [12] = {
2746 0, "tosanda0", "tosandax",
2747 0, "tosanda0", "tosandax",
2752 /* If the right hand side is const, the lhs is not on stack but still
2753 * in the primary register.
2755 if (flags & CF_CONST) {
2757 switch (flags & CF_TYPE) {
2760 if (flags & CF_FORCECHAR) {
2761 AddCodeLine ("and #$%02X", (unsigned char)val);
2766 if ((val & 0xFFFF) != 0xFFFF) {
2771 } else if (val != 0xFF) {
2772 AddCodeLine ("and #$%02X", (unsigned char)val);
2774 } else if ((val & 0xFF00) == 0xFF00) {
2775 AddCodeLine ("and #$%02X", (unsigned char)val);
2776 } else if ((val & 0x00FF) == 0x0000) {
2777 AddCodeLine ("txa");
2778 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2779 AddCodeLine ("tax");
2782 AddCodeLine ("tay");
2783 AddCodeLine ("txa");
2784 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2785 AddCodeLine ("tax");
2786 AddCodeLine ("tya");
2787 if ((val & 0x00FF) != 0x00FF) {
2788 AddCodeLine ("and #$%02X", (unsigned char)val);
2797 AddCodeLine ("stx sreg+1");
2798 AddCodeLine ("stx sreg");
2799 if ((val & 0xFF) != 0xFF) {
2800 AddCodeLine ("and #$%02X", (unsigned char)val);
2803 } else if (val == 0xFF00) {
2805 AddCodeLine ("sta sreg+1");
2806 AddCodeLine ("sta sreg");
2815 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2816 * into the normal, non-optimized stuff.
2818 g_push (flags & ~CF_CONST, 0);
2822 /* Use long way over the stack */
2823 oper (flags, val, ops);
2828 void g_asr (unsigned flags, unsigned long val)
2829 /* Primary = TOS >> Primary */
2831 static char* ops [12] = {
2832 0, "tosasra0", "tosasrax",
2833 0, "tosshra0", "tosshrax",
2838 /* If the right hand side is const, the lhs is not on stack but still
2839 * in the primary register.
2841 if (flags & CF_CONST) {
2843 switch (flags & CF_TYPE) {
2847 if (val >= 8 && (flags & CF_UNSIGNED)) {
2848 AddCodeLine ("txa");
2856 if (val >= 1 && val <= 4) {
2857 if (flags & CF_UNSIGNED) {
2858 AddCodeLine ("jsr shrax%ld", val);
2860 AddCodeLine ("jsr asrax%ld", val);
2867 if (val >= 1 && val <= 4) {
2868 if (flags & CF_UNSIGNED) {
2869 AddCodeLine ("jsr shreax%ld", val);
2871 AddCodeLine ("jsr asreax%ld", val);
2874 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2875 AddCodeLine ("txa");
2876 AddCodeLine ("ldx sreg");
2877 AddCodeLine ("ldy sreg+1");
2878 AddCodeLine ("sty sreg");
2879 AddCodeLine ("ldy #$00");
2880 AddCodeLine ("sty sreg+1");
2882 } else if (val == 16) {
2883 AddCodeLine ("ldy #$00");
2884 AddCodeLine ("ldx sreg+1");
2885 if ((flags & CF_UNSIGNED) == 0) {
2886 unsigned L = GetLocalLabel();
2887 AddCodeLine ("bpl %s", LocalLabelName (L));
2888 AddCodeLine ("dey");
2891 AddCodeLine ("lda sreg");
2892 AddCodeLine ("sty sreg+1");
2893 AddCodeLine ("sty sreg");
2902 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2903 * into the normal, non-optimized stuff.
2905 g_push (flags & ~CF_CONST, 0);
2909 /* Use long way over the stack */
2910 oper (flags, val, ops);
2915 void g_asl (unsigned flags, unsigned long val)
2916 /* Primary = TOS << Primary */
2918 static char* ops [12] = {
2919 0, "tosasla0", "tosaslax",
2920 0, "tosshla0", "tosshlax",
2926 /* If the right hand side is const, the lhs is not on stack but still
2927 * in the primary register.
2929 if (flags & CF_CONST) {
2931 switch (flags & CF_TYPE) {
2936 AddCodeLine ("tax");
2937 AddCodeLine ("lda #$00");
2944 if (val >= 1 && val <= 4) {
2945 if (flags & CF_UNSIGNED) {
2946 AddCodeLine ("jsr shlax%ld", val);
2948 AddCodeLine ("jsr aslax%ld", val);
2955 if (val >= 1 && val <= 4) {
2956 if (flags & CF_UNSIGNED) {
2957 AddCodeLine ("jsr shleax%ld", val);
2959 AddCodeLine ("jsr asleax%ld", val);
2962 } else if (val == 8) {
2963 AddCodeLine ("ldy sreg");
2964 AddCodeLine ("sty sreg+1");
2965 AddCodeLine ("stx sreg");
2966 AddCodeLine ("tax");
2967 AddCodeLine ("lda #$00");
2969 } else if (val == 16) {
2970 AddCodeLine ("stx sreg+1");
2971 AddCodeLine ("sta sreg");
2972 AddCodeLine ("lda #$00");
2973 AddCodeLine ("tax");
2982 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2983 * into the normal, non-optimized stuff.
2985 g_push (flags & ~CF_CONST, 0);
2989 /* Use long way over the stack */
2990 oper (flags, val, ops);
2995 void g_neg (unsigned flags)
2996 /* Primary = -Primary */
2998 switch (flags & CF_TYPE) {
3002 AddCodeLine ("jsr negax");
3006 AddCodeLine ("jsr negeax");
3016 void g_bneg (unsigned flags)
3017 /* Primary = !Primary */
3019 switch (flags & CF_TYPE) {
3022 AddCodeLine ("jsr bnega");
3026 AddCodeLine ("jsr bnegax");
3030 AddCodeLine ("jsr bnegeax");
3040 void g_com (unsigned flags)
3041 /* Primary = ~Primary */
3043 switch (flags & CF_TYPE) {
3047 AddCodeLine ("jsr complax");
3051 AddCodeLine ("jsr compleax");
3061 void g_inc (unsigned flags, unsigned long val)
3062 /* Increment the primary register by a given number */
3064 /* Don't inc by zero */
3069 /* Generate code for the supported types */
3071 switch (flags & CF_TYPE) {
3074 if (flags & CF_FORCECHAR) {
3075 if (CPU == CPU_65C02 && val <= 2) {
3077 AddCodeLine ("ina");
3080 AddCodeLine ("clc");
3081 AddCodeLine ("adc #$%02X", (unsigned char)val);
3088 if (CPU == CPU_65C02 && val == 1) {
3089 unsigned L = GetLocalLabel();
3090 AddCodeLine ("ina");
3091 AddCodeLine ("bne %s", LocalLabelName (L));
3092 AddCodeLine ("inx");
3094 } else if (CodeSizeFactor < 200) {
3097 AddCodeLine ("jsr incax%lu", val);
3098 } else if (val <= 255) {
3100 AddCodeLine ("jsr incaxy");
3102 g_add (flags | CF_CONST, val);
3105 /* Inline the code */
3107 if ((val & 0xFF) != 0) {
3108 unsigned L = GetLocalLabel();
3109 AddCodeLine ("clc");
3110 AddCodeLine ("adc #$%02X", (unsigned char) val);
3111 AddCodeLine ("bcc %s", LocalLabelName (L));
3112 AddCodeLine ("inx");
3116 AddCodeLine ("inx");
3119 AddCodeLine ("inx");
3122 AddCodeLine ("clc");
3123 if ((val & 0xFF) != 0) {
3124 AddCodeLine ("adc #$%02X", (unsigned char) val);
3126 AddCodeLine ("pha");
3127 AddCodeLine ("txa");
3128 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3129 AddCodeLine ("tax");
3130 AddCodeLine ("pla");
3138 AddCodeLine ("jsr inceaxy");
3140 g_add (flags | CF_CONST, val);
3152 void g_dec (unsigned flags, unsigned long val)
3153 /* Decrement the primary register by a given number */
3155 /* Don't dec by zero */
3160 /* Generate code for the supported types */
3162 switch (flags & CF_TYPE) {
3165 if (flags & CF_FORCECHAR) {
3166 if (CPU == CPU_65C02 && val <= 2) {
3168 AddCodeLine ("dea");
3171 AddCodeLine ("sec");
3172 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3179 if (CodeSizeFactor < 200) {
3180 /* Use subroutines */
3182 AddCodeLine ("jsr decax%d", (int) val);
3183 } else if (val <= 255) {
3185 AddCodeLine ("jsr decaxy");
3187 g_sub (flags | CF_CONST, val);
3190 /* Inline the code */
3192 if ((val & 0xFF) != 0) {
3193 unsigned L = GetLocalLabel();
3194 AddCodeLine ("sec");
3195 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3196 AddCodeLine ("bcs %s", LocalLabelName (L));
3197 AddCodeLine ("dex");
3201 AddCodeLine ("dex");
3204 AddCodeLine ("dex");
3207 AddCodeLine ("sec");
3208 if ((val & 0xFF) != 0) {
3209 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3211 AddCodeLine ("pha");
3212 AddCodeLine ("txa");
3213 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3214 AddCodeLine ("tax");
3215 AddCodeLine ("pla");
3223 AddCodeLine ("jsr deceaxy");
3225 g_sub (flags | CF_CONST, val);
3238 * Following are the conditional operators. They compare the TOS against
3239 * the primary and put a literal 1 in the primary if the condition is
3240 * true, otherwise they clear the primary register
3245 void g_eq (unsigned flags, unsigned long val)
3246 /* Test for equal */
3248 static char* ops [12] = {
3249 "toseq00", "toseqa0", "toseqax",
3250 "toseq00", "toseqa0", "toseqax",
3257 /* If the right hand side is const, the lhs is not on stack but still
3258 * in the primary register.
3260 if (flags & CF_CONST) {
3262 switch (flags & CF_TYPE) {
3265 if (flags & CF_FORCECHAR) {
3266 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3267 AddCodeLine ("jsr booleq");
3273 L = GetLocalLabel();
3274 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3275 AddCodeLine ("bne %s", LocalLabelName (L));
3276 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3278 AddCodeLine ("jsr booleq");
3288 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3289 * into the normal, non-optimized stuff.
3291 g_push (flags & ~CF_CONST, 0);
3295 /* Use long way over the stack */
3296 oper (flags, val, ops);
3301 void g_ne (unsigned flags, unsigned long val)
3302 /* Test for not equal */
3304 static char* ops [12] = {
3305 "tosne00", "tosnea0", "tosneax",
3306 "tosne00", "tosnea0", "tosneax",
3313 /* If the right hand side is const, the lhs is not on stack but still
3314 * in the primary register.
3316 if (flags & CF_CONST) {
3318 switch (flags & CF_TYPE) {
3321 if (flags & CF_FORCECHAR) {
3322 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3323 AddCodeLine ("jsr boolne");
3329 L = GetLocalLabel();
3330 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3331 AddCodeLine ("bne %s", LocalLabelName (L));
3332 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3334 AddCodeLine ("jsr boolne");
3344 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3345 * into the normal, non-optimized stuff.
3347 g_push (flags & ~CF_CONST, 0);
3351 /* Use long way over the stack */
3352 oper (flags, val, ops);
3357 void g_lt (unsigned flags, unsigned long val)
3358 /* Test for less than */
3360 static char* ops [12] = {
3361 "toslt00", "toslta0", "tosltax",
3362 "tosult00", "tosulta0", "tosultax",
3367 /* If the right hand side is const, the lhs is not on stack but still
3368 * in the primary register.
3370 if (flags & CF_CONST) {
3372 /* Give a warning in some special cases */
3373 if ((flags & CF_UNSIGNED) && val == 0) {
3374 Warning ("Condition is never true");
3377 /* Look at the type */
3378 switch (flags & CF_TYPE) {
3381 if (flags & CF_FORCECHAR) {
3382 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3383 if (flags & CF_UNSIGNED) {
3384 AddCodeLine ("jsr boolult");
3386 AddCodeLine ("jsr boollt");
3393 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3394 /* If we have a signed compare against zero, we only need to
3395 * test the high byte.
3397 AddCodeLine ("txa");
3398 AddCodeLine ("jsr boollt");
3401 /* Direct code only for unsigned data types */
3402 if (flags & CF_UNSIGNED) {
3403 unsigned L = GetLocalLabel();
3404 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3405 AddCodeLine ("bne %s", LocalLabelName (L));
3406 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3408 AddCodeLine ("jsr boolult");
3414 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3415 /* If we have a signed compare against zero, we only need to
3416 * test the high byte.
3418 AddCodeLine ("lda sreg+1");
3419 AddCodeLine ("jsr boollt");
3428 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3429 * into the normal, non-optimized stuff.
3431 g_push (flags & ~CF_CONST, 0);
3435 /* Use long way over the stack */
3436 oper (flags, val, ops);
3441 void g_le (unsigned flags, unsigned long val)
3442 /* Test for less than or equal to */
3444 static char* ops [12] = {
3445 "tosle00", "toslea0", "tosleax",
3446 "tosule00", "tosulea0", "tosuleax",
3452 /* If the right hand side is const, the lhs is not on stack but still
3453 * in the primary register.
3455 if (flags & CF_CONST) {
3457 /* <= is not very effective on the 6502, so try to convert
3458 * it into < if the value is in a valid range.
3461 /* Look at the type */
3462 switch (flags & CF_TYPE) {
3465 if (flags & CF_FORCECHAR) {
3466 if (flags & CF_UNSIGNED) {
3468 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3469 AddCodeLine ("jsr boolult");
3471 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3472 AddCodeLine ("jsr boolule");
3476 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3477 AddCodeLine ("jsr boollt");
3479 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3480 AddCodeLine ("jsr boolle");
3488 if (flags & CF_UNSIGNED) {
3489 unsigned L = GetLocalLabel();
3490 const char* Name = "boolule";
3495 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3496 AddCodeLine ("bne %s", LocalLabelName (L));
3497 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3499 AddCodeLine ("jsr %s", Name);
3511 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3512 * into the normal, non-optimized stuff.
3514 g_push (flags & ~CF_CONST, 0);
3518 /* Use long way over the stack */
3519 oper (flags, val, ops);
3524 void g_gt (unsigned flags, unsigned long val)
3525 /* Test for greater than */
3527 static char* ops [12] = {
3528 "tosgt00", "tosgta0", "tosgtax",
3529 "tosugt00", "tosugta0", "tosugtax",
3535 /* If the right hand side is const, the lhs is not on stack but still
3536 * in the primary register.
3538 if (flags & CF_CONST) {
3540 /* > is not very effective on the 6502, so try to convert
3541 * it into >= if the value is in a valid range.
3544 /* Look at the type */
3545 switch (flags & CF_TYPE) {
3548 if (flags & CF_FORCECHAR) {
3549 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 latter
3552 * is easier to optimize.
3555 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3556 AddCodeLine ("jsr boolne");
3557 } else if (val < 255) {
3558 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3559 AddCodeLine ("jsr booluge");
3561 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3562 AddCodeLine ("jsr boolugt");
3566 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3567 AddCodeLine ("jsr boolge");
3569 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3570 AddCodeLine ("jsr boolgt");
3578 if (flags & CF_UNSIGNED) {
3579 /* If we have a compare > 0, we will replace it by
3580 * != 0 here, since both are identical but the latter
3581 * is easier to optimize.
3584 AddCodeLine ("stx tmp1");
3585 AddCodeLine ("ora tmp1");
3586 AddCodeLine ("jsr boolne");
3588 unsigned L = GetLocalLabel();
3589 const char* Name = "boolugt";
3594 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3595 AddCodeLine ("bne %s", LocalLabelName (L));
3596 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3598 AddCodeLine ("jsr %s", Name);
3611 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3612 * into the normal, non-optimized stuff.
3614 g_push (flags & ~CF_CONST, 0);
3618 /* Use long way over the stack */
3619 oper (flags, val, ops);
3624 void g_ge (unsigned flags, unsigned long val)
3625 /* Test for greater than or equal to */
3627 static char* ops [12] = {
3628 "tosge00", "tosgea0", "tosgeax",
3629 "tosuge00", "tosugea0", "tosugeax",
3635 /* If the right hand side is const, the lhs is not on stack but still
3636 * in the primary register.
3638 if (flags & CF_CONST) {
3640 /* Give a warning in some special cases */
3641 if ((flags & CF_UNSIGNED) && val == 0) {
3642 Warning ("Condition is always true");
3645 /* Look at the type */
3646 switch (flags & CF_TYPE) {
3649 if (flags & CF_FORCECHAR) {
3650 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3651 if (flags & CF_UNSIGNED) {
3652 AddCodeLine ("jsr booluge");
3654 AddCodeLine ("jsr boolge");
3661 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3662 /* If we have a signed compare against zero, we only need to
3663 * test the high byte.
3665 AddCodeLine ("txa");
3666 AddCodeLine ("jsr boolge");
3669 /* Direct code only for unsigned data types */
3670 if (flags & CF_UNSIGNED) {
3671 unsigned L = GetLocalLabel();
3672 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3673 AddCodeLine ("bne %s", LocalLabelName (L));
3674 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3676 AddCodeLine ("jsr booluge");
3682 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3683 /* If we have a signed compare against zero, we only need to
3684 * test the high byte.
3686 AddCodeLine ("lda sreg+1");
3687 AddCodeLine ("jsr boolge");
3696 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3697 * into the normal, non-optimized stuff.
3699 g_push (flags & ~CF_CONST, 0);
3703 /* Use long way over the stack */
3704 oper (flags, val, ops);
3709 /*****************************************************************************/
3710 /* Allocating static storage */
3711 /*****************************************************************************/
3715 void g_res (unsigned n)
3716 /* Reserve static storage, n bytes */
3718 AddDataLine ("\t.res\t%u,$00", n);
3723 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3724 /* Define data with the size given in flags */
3726 if (flags & CF_CONST) {
3728 /* Numeric constant */
3729 switch (flags & CF_TYPE) {
3732 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
3736 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
3740 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3751 /* Create the correct label name */
3752 const char* Label = GetLabelName (flags, val, offs);
3754 /* Labels are always 16 bit */
3755 AddDataLine ("\t.addr\t%s", Label);
3762 void g_defbytes (const void* Bytes, unsigned Count)
3763 /* Output a row of bytes as a constant */
3769 /* Cast the buffer pointer */
3770 const unsigned char* Data = (const unsigned char*) Bytes;
3772 /* Output the stuff */
3775 /* How many go into this line? */
3776 if ((Chunk = Count) > 16) {
3781 /* Output one line */
3782 strcpy (Buf, "\t.byte\t");
3785 B += sprintf (B, "$%02X", *Data++);
3791 /* Output the line */
3798 void g_zerobytes (unsigned n)
3799 /* Output n bytes of data initialized with zero */
3801 AddDataLine ("\t.res\t%u,$00", n);
3806 /*****************************************************************************/
3807 /* Switch statement */
3808 /*****************************************************************************/
3812 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
3813 /* Generate code for a switch statement */
3815 unsigned NextLabel = 0;
3818 /* Setup registers and determine which compare insn to use */
3819 const char* Compare;
3822 Compare = "cmp #$%02X";
3825 Compare = "cpx #$%02X";
3828 AddCodeLine ("ldy sreg");
3829 Compare = "cpy #$%02X";
3832 AddCodeLine ("ldy sreg+1");
3833 Compare = "cpy #$%02X";
3836 Internal ("Invalid depth in g_switch: %u", Depth);
3839 /* Walk over all nodes */
3840 for (I = 0; I < CollCount (Nodes); ++I) {
3842 /* Get the next case node */
3843 CaseNode* N = CollAtUnchecked (Nodes, I);
3845 /* If we have a next label, define it */
3847 g_defcodelabel (NextLabel);
3851 /* Do the compare */
3852 AddCodeLine (Compare, CN_GetValue (N));
3854 /* If this is the last level, jump directly to the case code if found */
3857 /* Branch if equal */
3858 g_falsejump (0, CN_GetLabel (N));
3862 /* Determine the next label */
3863 if (I == CollCount (Nodes) - 1) {
3864 /* Last node means not found */
3865 g_truejump (0, DefaultLabel);
3867 /* Jump to the next check */
3868 NextLabel = GetLocalLabel ();
3869 g_truejump (0, NextLabel);
3872 /* Check the next level */
3873 g_switch (N->Nodes, DefaultLabel, Depth-1);
3878 /* If we go here, we haven't found the label */
3879 g_jump (DefaultLabel);
3884 /*****************************************************************************/
3885 /* User supplied assembler code */
3886 /*****************************************************************************/
3890 void g_asmcode (struct StrBuf* B)
3891 /* Output one line of assembler code. */
3893 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));
3898 /*****************************************************************************/
3899 /* Inlined known functions */
3900 /*****************************************************************************/
3904 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3905 /* Inline the strlen() function */
3907 /* We need a label in both cases */
3908 unsigned label = GetLocalLabel ();
3910 /* Two different encodings */
3911 if (flags & CF_CONST) {
3913 /* The address of the string is constant. Create the correct label name */
3914 char* lbuf = GetLabelName (flags, val, offs);
3916 /* Generate the strlen code */
3917 AddCodeLine ("ldy #$FF");
3918 g_defcodelabel (label);
3919 AddCodeLine ("iny");
3920 AddCodeLine ("lda %s,y", lbuf);
3921 AddCodeLine ("bne %s", LocalLabelName (label));
3922 AddCodeLine ("tax");
3923 AddCodeLine ("tya");
3927 /* Address not constant but in primary */
3928 if (CodeSizeFactor < 400) {
3929 /* This is too much code, so call strlen instead of inlining */
3930 AddCodeLine ("jsr _strlen");
3932 /* Inline the function */
3933 AddCodeLine ("sta ptr1");
3934 AddCodeLine ("stx ptr1+1");
3935 AddCodeLine ("ldy #$FF");
3936 g_defcodelabel (label);
3937 AddCodeLine ("iny");
3938 AddCodeLine ("lda (ptr1),y");
3939 AddCodeLine ("bne %s", LocalLabelName (label));
3940 AddCodeLine ("tax");
3941 AddCodeLine ("tya");