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 /*****************************************************************************/
61 /*****************************************************************************/
63 /*****************************************************************************/
67 /* Compiler relative stack pointer */
72 /*****************************************************************************/
74 /*****************************************************************************/
78 static void typeerror (unsigned type)
79 /* Print an error message about an invalid operand type */
81 Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
86 static void CheckLocalOffs (unsigned Offs)
87 /* Check the offset into the stack for 8bit range */
90 /* Too many local vars */
91 Error ("Too many local variables");
97 static char* GetLabelName (unsigned flags, unsigned long label, unsigned offs)
99 static char lbuf [128]; /* Label name */
101 /* Create the correct label name */
102 switch (flags & CF_ADDRMASK) {
105 /* Static memory cell */
106 sprintf (lbuf, "%s+%u", LocalLabelName (label), offs);
111 sprintf (lbuf, "_%s+%u", (char*) label, offs);
115 /* Absolute address */
116 sprintf (lbuf, "$%04X", (unsigned)((label+offs) & 0xFFFF));
120 /* Variable in register bank */
121 sprintf (lbuf, "regbank+%u", (unsigned)((label+offs) & 0xFFFF));
125 Internal ("Invalid address flags");
128 /* Return a pointer to the static buffer */
134 /*****************************************************************************/
135 /* Pre- and postamble */
136 /*****************************************************************************/
140 void g_preamble (void)
141 /* Generate the assembler code preamble */
143 /* Create a new (global) segment list and remember it */
147 /* Identify the compiler version */
149 AddTextLine ("; File generated by cc65 v %u.%u.%u",
150 VER_MAJOR, VER_MINOR, VER_PATCH);
153 /* Insert some object file options */
154 AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
155 VER_MAJOR, VER_MINOR, VER_PATCH);
157 /* If we're producing code for some other CPU, switch the command set */
158 if (CPU == CPU_65C02) {
159 AddTextLine ("\t.pc02");
162 /* Allow auto import for runtime library routines */
163 AddTextLine ("\t.autoimport\ton");
165 /* Switch the assembler into case sensitive mode */
166 AddTextLine ("\t.case\t\ton");
168 /* Tell the assembler if we want to generate debug info */
169 AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
171 /* Import the stack pointer for direct auto variable access */
172 AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1");
174 /* Define long branch macros */
175 AddTextLine ("\t.macpack\tlongbranch");
180 void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
181 /* If debug info is enabled, place a file info into the source */
184 /* We have to place this into the global text segment, so it will
185 * appear before all .dbg line statements.
187 TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
193 /*****************************************************************************/
194 /* Segment support */
195 /*****************************************************************************/
199 void g_userodata (void)
200 /* Switch to the read only data segment */
202 UseDataSeg (SEG_RODATA);
207 void g_usedata (void)
208 /* Switch to the data segment */
210 UseDataSeg (SEG_DATA);
216 /* Switch to the bss segment */
218 UseDataSeg (SEG_BSS);
223 void g_segname (segment_t Seg, const char* Name)
224 /* Set the name of a segment */
228 /* Remember the new name */
229 NewSegName (Seg, Name);
231 /* Emit a segment directive for the data style segments */
233 case SEG_RODATA: S = CS->ROData; break;
234 case SEG_DATA: S = CS->Data; break;
235 case SEG_BSS: S = CS->BSS; break;
236 default: S = 0; break;
239 DS_AddLine (S, ".segment\t\"%s\"", Name);
245 /*****************************************************************************/
247 /*****************************************************************************/
251 unsigned sizeofarg (unsigned flags)
252 /* Return the size of a function argument type that is encoded in flags */
254 switch (flags & CF_TYPE) {
257 return (flags & CF_FORCECHAR)? 1 : 2;
274 int pop (unsigned flags)
275 /* Pop an argument of the given size */
277 return oursp += sizeofarg (flags);
282 int push (unsigned flags)
283 /* Push an argument of the given size */
285 return oursp -= sizeofarg (flags);
290 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
291 /* The value in Offs is an offset to an address in a/x. Make sure, an object
292 * of the type given in Flags can be loaded or stored into this address by
293 * adding part of the offset to the address in ax, so that the remaining
294 * offset fits into an index register. Return the remaining offset.
297 /* If the offset is too large for a byte register, add the high byte
298 * of the offset to the primary. Beware: We need a special correction
299 * if the offset in the low byte will overflow in the operation.
301 unsigned O = Offs & ~0xFFU;
302 if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
303 /* We need to add the low byte also */
307 /* Do the correction if we need one */
309 g_inc (CF_INT | CF_CONST, O);
313 /* Return the new offset */
319 /*****************************************************************************/
320 /* Functions handling local labels */
321 /*****************************************************************************/
325 void g_defcodelabel (unsigned label)
326 /* Define a local code label */
328 CS_AddLabel (CS->Code, LocalLabelName (label));
333 void g_defdatalabel (unsigned label)
334 /* Define a local data label */
336 AddDataLine ("%s:", LocalLabelName (label));
341 /*****************************************************************************/
342 /* Functions handling global labels */
343 /*****************************************************************************/
347 void g_defgloblabel (const char* Name)
348 /* Define a global label with the given name */
350 /* Global labels are always data labels */
351 AddDataLine ("_%s:", Name);
356 void g_defexport (const char* Name, int ZP)
357 /* Export the given label */
360 AddTextLine ("\t.exportzp\t_%s", Name);
362 AddTextLine ("\t.export\t\t_%s", Name);
368 void g_defimport (const char* Name, int ZP)
369 /* Import the given label */
372 AddTextLine ("\t.importzp\t_%s", Name);
374 AddTextLine ("\t.import\t\t_%s", Name);
380 /*****************************************************************************/
381 /* Load functions for various registers */
382 /*****************************************************************************/
386 static void ldaconst (unsigned val)
387 /* Load a with a constant */
389 AddCodeLine ("lda #$%02X", val & 0xFF);
394 static void ldxconst (unsigned val)
395 /* Load x with a constant */
397 AddCodeLine ("ldx #$%02X", val & 0xFF);
402 static void ldyconst (unsigned val)
403 /* Load y with a constant */
405 AddCodeLine ("ldy #$%02X", val & 0xFF);
410 /*****************************************************************************/
411 /* Function entry and exit */
412 /*****************************************************************************/
416 /* Remember the argument size of a function. The variable is set by g_enter
417 * and used by g_leave. If the functions gets its argument size by the caller
418 * (variable param list or function without prototype), g_enter will set the
424 void g_enter (unsigned flags, unsigned argsize)
425 /* Function prologue */
427 if ((flags & CF_FIXARGC) != 0) {
428 /* Just remember the argument size for the leave */
432 AddCodeLine ("jsr enter");
439 /* Function epilogue */
441 /* How many bytes of locals do we have to drop? */
444 /* If we didn't have a variable argument list, don't call leave */
447 /* Drop stackframe if needed */
451 AddCodeLine ("jsr incsp%d", k);
455 AddCodeLine ("jsr addysp");
462 /* Nothing to drop */
463 AddCodeLine ("jsr leave");
465 /* We've a stack frame to drop */
467 AddCodeLine ("jsr leavey");
471 /* Add the final rts */
477 /*****************************************************************************/
478 /* Register variables */
479 /*****************************************************************************/
483 void g_save_regvars (int RegOffs, unsigned Bytes)
484 /* Save register variables */
486 /* Don't loop for up to two bytes */
489 AddCodeLine ("lda regbank%+d", RegOffs);
490 AddCodeLine ("jsr pusha");
492 } else if (Bytes == 2) {
494 AddCodeLine ("lda regbank%+d", RegOffs);
495 AddCodeLine ("ldx regbank%+d", RegOffs+1);
496 AddCodeLine ("jsr pushax");
500 /* More than two bytes - loop */
501 unsigned Label = GetLocalLabel ();
503 ldyconst (Bytes - 1);
505 g_defcodelabel (Label);
506 AddCodeLine ("lda regbank%+d,x", RegOffs-1);
507 AddCodeLine ("sta (sp),y");
510 AddCodeLine ("bne %s", LocalLabelName (Label));
514 /* We pushed stuff, correct the stack pointer */
520 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
521 /* Restore register variables */
523 /* Calculate the actual stack offset and check it */
525 CheckLocalOffs (StackOffs);
527 /* Don't loop for up to two bytes */
530 ldyconst (StackOffs);
531 AddCodeLine ("lda (sp),y");
532 AddCodeLine ("sta regbank%+d", RegOffs);
534 } else if (Bytes == 2) {
536 ldyconst (StackOffs);
537 AddCodeLine ("lda (sp),y");
538 AddCodeLine ("sta regbank%+d", RegOffs);
540 AddCodeLine ("lda (sp),y");
541 AddCodeLine ("sta regbank%+d", RegOffs+1);
545 /* More than two bytes - loop */
546 unsigned Label = GetLocalLabel ();
547 ldyconst (StackOffs+Bytes-1);
549 g_defcodelabel (Label);
550 AddCodeLine ("lda (sp),y");
551 AddCodeLine ("sta regbank%+d,x", RegOffs-1);
554 AddCodeLine ("bne %s", LocalLabelName (Label));
561 /*****************************************************************************/
562 /* Fetching memory cells */
563 /*****************************************************************************/
567 void g_getimmed (unsigned Flags, unsigned long Val, unsigned Offs)
568 /* Load a constant into the primary register */
570 unsigned char B1, B2, B3, B4;
574 if ((Flags & CF_CONST) != 0) {
576 /* Numeric constant */
577 switch (Flags & CF_TYPE) {
580 if ((Flags & CF_FORCECHAR) != 0) {
586 ldxconst ((Val >> 8) & 0xFF);
587 ldaconst (Val & 0xFF);
591 /* Split the value into 4 bytes */
592 B1 = (unsigned char) (Val >> 0);
593 B2 = (unsigned char) (Val >> 8);
594 B3 = (unsigned char) (Val >> 16);
595 B4 = (unsigned char) (Val >> 24);
597 /* Remember which bytes are done */
601 AddCodeLine ("ldx #$%02X", B2);
604 AddCodeLine ("stx sreg");
608 AddCodeLine ("stx sreg+1");
611 if ((Done & 0x04) == 0 && B1 != B3) {
612 AddCodeLine ("lda #$%02X", B3);
613 AddCodeLine ("sta sreg");
616 if ((Done & 0x08) == 0 && B1 != B4) {
617 AddCodeLine ("lda #$%02X", B4);
618 AddCodeLine ("sta sreg+1");
621 AddCodeLine ("lda #$%02X", B1);
623 if ((Done & 0x04) == 0) {
625 AddCodeLine ("sta sreg");
627 if ((Done & 0x08) == 0) {
629 AddCodeLine ("sta sreg+1");
641 /* Some sort of label */
642 const char* Label = GetLabelName (Flags, Val, Offs);
644 /* Load the address into the primary */
645 AddCodeLine ("lda #<(%s)", Label);
646 AddCodeLine ("ldx #>(%s)", Label);
653 void g_getstatic (unsigned flags, unsigned long label, unsigned offs)
654 /* Fetch an static memory cell into the primary register */
656 /* Create the correct label name */
657 char* lbuf = GetLabelName (flags, label, offs);
659 /* Check the size and generate the correct load operation */
660 switch (flags & CF_TYPE) {
663 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
664 AddCodeLine ("lda %s", lbuf); /* load A from the label */
667 AddCodeLine ("lda %s", lbuf); /* load A from the label */
668 if (!(flags & CF_UNSIGNED)) {
669 /* Must sign extend */
670 unsigned L = GetLocalLabel ();
671 AddCodeLine ("bpl %s", LocalLabelName (L));
679 AddCodeLine ("lda %s", lbuf);
680 if (flags & CF_TEST) {
681 AddCodeLine ("ora %s+1", lbuf);
683 AddCodeLine ("ldx %s+1", lbuf);
688 if (flags & CF_TEST) {
689 AddCodeLine ("lda %s+3", lbuf);
690 AddCodeLine ("ora %s+2", lbuf);
691 AddCodeLine ("ora %s+1", lbuf);
692 AddCodeLine ("ora %s+0", lbuf);
694 AddCodeLine ("lda %s+3", lbuf);
695 AddCodeLine ("sta sreg+1");
696 AddCodeLine ("lda %s+2", lbuf);
697 AddCodeLine ("sta sreg");
698 AddCodeLine ("ldx %s+1", lbuf);
699 AddCodeLine ("lda %s", lbuf);
711 void g_getlocal (unsigned flags, int offs)
712 /* Fetch specified local object (local var). */
715 CheckLocalOffs (offs);
716 switch (flags & CF_TYPE) {
719 if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
720 if (CPU == CPU_65C02 && offs == 0) {
721 AddCodeLine ("lda (sp)");
724 AddCodeLine ("lda (sp),y");
728 AddCodeLine ("ldx #$00");
729 AddCodeLine ("lda (sp),y");
730 if ((flags & CF_UNSIGNED) == 0) {
731 unsigned L = GetLocalLabel();
732 AddCodeLine ("bpl %s", LocalLabelName (L));
740 CheckLocalOffs (offs + 1);
741 if (flags & CF_TEST) {
743 AddCodeLine ("lda (sp),y");
745 AddCodeLine ("ora (sp),y");
747 if (CodeSizeFactor > 180) {
749 AddCodeLine ("lda (sp),y");
752 AddCodeLine ("lda (sp),y");
756 AddCodeLine ("jsr ldaxysp");
758 AddCodeLine ("jsr ldax0sp");
767 AddCodeLine ("jsr ldeaxysp");
769 AddCodeLine ("jsr ldeax0sp");
780 void g_getind (unsigned flags, unsigned offs)
781 /* Fetch the specified object type indirect through the primary register
782 * into the primary register
785 /* If the offset is greater than 255, add the part that is > 255 to
786 * the primary. This way we get an easy addition and use the low byte
789 offs = MakeByteOffs (flags, offs);
791 /* Handle the indirect fetch */
792 switch (flags & CF_TYPE) {
795 /* Character sized */
796 if (flags & CF_UNSIGNED) {
798 AddCodeLine ("jsr ldauidx");
801 AddCodeLine ("jsr ldaidx");
806 if (flags & CF_TEST) {
808 AddCodeLine ("sta ptr1");
809 AddCodeLine ("stx ptr1+1");
810 AddCodeLine ("lda (ptr1),y");
812 AddCodeLine ("ora (ptr1),y");
815 AddCodeLine ("jsr ldaxi");
818 AddCodeLine ("jsr ldaxidx");
825 AddCodeLine ("jsr ldeaxi");
828 AddCodeLine ("jsr ldeaxidx");
830 if (flags & CF_TEST) {
831 AddCodeLine ("jsr tsteax");
843 void g_leasp (int offs)
844 /* Fetch the address of the specified symbol into the primary register */
846 /* Calculate the offset relative to sp */
849 /* For value 0 we do direct code */
851 AddCodeLine ("lda sp");
852 AddCodeLine ("ldx sp+1");
854 if (CodeSizeFactor < 300) {
855 ldaconst (offs); /* Load A with offset value */
856 AddCodeLine ("jsr leaasp"); /* Load effective address */
858 unsigned L = GetLocalLabel ();
859 if (CPU == CPU_65C02 && offs == 1) {
860 AddCodeLine ("lda sp");
861 AddCodeLine ("ldx sp+1");
863 AddCodeLine ("bne %s", LocalLabelName (L));
868 AddCodeLine ("ldx sp+1");
869 AddCodeLine ("adc sp");
870 AddCodeLine ("bcc %s", LocalLabelName (L));
880 void g_leavariadic (int Offs)
881 /* Fetch the address of a parameter in a variadic function into the primary
885 unsigned ArgSizeOffs;
887 /* Calculate the offset relative to sp */
890 /* Get the offset of the parameter which is stored at sp+0 on function
891 * entry and check if this offset is reachable with a byte offset.
894 ArgSizeOffs = -oursp;
895 CheckLocalOffs (ArgSizeOffs);
897 /* Get the size of all parameters. */
898 if (ArgSizeOffs == 0 && CPU == CPU_65C02) {
899 AddCodeLine ("lda (sp)");
901 ldyconst (ArgSizeOffs);
902 AddCodeLine ("lda (sp),y");
905 /* Add the value of the stackpointer */
906 if (CodeSizeFactor > 250) {
907 unsigned L = GetLocalLabel();
908 AddCodeLine ("ldx sp+1");
910 AddCodeLine ("adc sp");
911 AddCodeLine ("bcc %s", LocalLabelName (L));
915 AddCodeLine ("jsr leaasp");
918 /* Add the offset to the primary */
920 g_inc (CF_INT | CF_CONST, Offs);
921 } else if (Offs < 0) {
922 g_dec (CF_INT | CF_CONST, -Offs);
928 /*****************************************************************************/
929 /* Store into memory */
930 /*****************************************************************************/
934 void g_putstatic (unsigned flags, unsigned long label, unsigned offs)
935 /* Store the primary register into the specified static memory cell */
937 /* Create the correct label name */
938 char* lbuf = GetLabelName (flags, label, offs);
940 /* Check the size and generate the correct store operation */
941 switch (flags & CF_TYPE) {
944 AddCodeLine ("sta %s", lbuf);
948 AddCodeLine ("sta %s", lbuf);
949 AddCodeLine ("stx %s+1", lbuf);
953 AddCodeLine ("sta %s", lbuf);
954 AddCodeLine ("stx %s+1", lbuf);
955 AddCodeLine ("ldy sreg");
956 AddCodeLine ("sty %s+2", lbuf);
957 AddCodeLine ("ldy sreg+1");
958 AddCodeLine ("sty %s+3", lbuf);
969 void g_putlocal (unsigned Flags, int Offs, long Val)
970 /* Put data into local object. */
973 CheckLocalOffs (Offs);
974 switch (Flags & CF_TYPE) {
977 if (Flags & CF_CONST) {
978 AddCodeLine ("lda #$%02X", (unsigned char) Val);
980 if (CPU == CPU_65C02 && Offs == 0) {
981 AddCodeLine ("sta (sp)");
984 AddCodeLine ("sta (sp),y");
989 if (Flags & CF_CONST) {
991 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
992 AddCodeLine ("sta (sp),y");
993 if ((Flags & CF_NOKEEP) == 0) {
994 /* Place high byte into X */
997 if (CPU == CPU_65C02 && Offs == 0) {
998 AddCodeLine ("lda #$%02X", (unsigned char) Val);
999 AddCodeLine ("sta (sp)");
1001 if ((Val & 0xFF) == Offs+1) {
1002 /* The value we need is already in Y */
1003 AddCodeLine ("tya");
1004 AddCodeLine ("dey");
1006 AddCodeLine ("dey");
1007 AddCodeLine ("lda #$%02X", (unsigned char) Val);
1009 AddCodeLine ("sta (sp),y");
1012 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
1015 AddCodeLine ("jsr staxysp");
1017 AddCodeLine ("jsr stax0sp");
1020 if (CPU == CPU_65C02 && Offs == 0) {
1021 AddCodeLine ("sta (sp)");
1023 AddCodeLine ("txa");
1024 AddCodeLine ("sta (sp),y");
1027 AddCodeLine ("sta (sp),y");
1028 AddCodeLine ("iny");
1029 AddCodeLine ("txa");
1030 AddCodeLine ("sta (sp),y");
1037 if (Flags & CF_CONST) {
1038 g_getimmed (Flags, Val, 0);
1042 AddCodeLine ("jsr steaxysp");
1044 AddCodeLine ("jsr steax0sp");
1056 void g_putind (unsigned Flags, unsigned Offs)
1057 /* Store the specified object type in the primary register at the address
1058 * on the top of the stack
1061 /* We can handle offsets below $100 directly, larger offsets must be added
1062 * to the address. Since a/x is in use, best code is achieved by adding
1063 * just the high byte. Be sure to check if the low byte will overflow while
1066 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1068 /* Overflow - we need to add the low byte also */
1069 AddCodeLine ("ldy #$00");
1070 AddCodeLine ("clc");
1071 AddCodeLine ("pha");
1072 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1073 AddCodeLine ("adc (sp),y");
1074 AddCodeLine ("sta (sp),y");
1075 AddCodeLine ("iny");
1076 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1077 AddCodeLine ("adc (sp),y");
1078 AddCodeLine ("sta (sp),y");
1079 AddCodeLine ("pla");
1081 /* Complete address is on stack, new offset is zero */
1084 } else if ((Offs & 0xFF00) != 0) {
1086 /* We can just add the high byte */
1087 AddCodeLine ("ldy #$01");
1088 AddCodeLine ("clc");
1089 AddCodeLine ("pha");
1090 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1091 AddCodeLine ("adc (sp),y");
1092 AddCodeLine ("sta (sp),y");
1093 AddCodeLine ("pla");
1095 /* Offset is now just the low byte */
1099 /* Check the size and determine operation */
1100 switch (Flags & CF_TYPE) {
1104 AddCodeLine ("jsr staspidx");
1109 AddCodeLine ("jsr staxspidx");
1114 AddCodeLine ("jsr steaxspidx");
1122 /* Pop the argument which is always a pointer */
1128 /*****************************************************************************/
1129 /* type conversion and similiar stuff */
1130 /*****************************************************************************/
1134 void g_toslong (unsigned flags)
1135 /* Make sure, the value on TOS is a long. Convert if necessary */
1137 switch (flags & CF_TYPE) {
1141 if (flags & CF_UNSIGNED) {
1142 AddCodeLine ("jsr tosulong");
1144 AddCodeLine ("jsr toslong");
1159 void g_tosint (unsigned flags)
1160 /* Make sure, the value on TOS is an int. Convert if necessary */
1162 switch (flags & CF_TYPE) {
1169 AddCodeLine ("jsr tosint");
1180 void g_reglong (unsigned flags)
1181 /* Make sure, the value in the primary register a long. Convert if necessary */
1183 switch (flags & CF_TYPE) {
1187 if (flags & CF_UNSIGNED) {
1188 if (CodeSizeFactor >= 200) {
1190 AddCodeLine ("sty sreg");
1191 AddCodeLine ("sty sreg+1");
1193 AddCodeLine ("jsr axulong");
1196 AddCodeLine ("jsr axlong");
1210 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1211 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1212 * value, that corresponds to the value on TOS, rhs corresponds to the value
1213 * in (e)ax. The return value is the the flags value for the resulting type.
1216 unsigned ltype, rtype;
1219 /* Get the type spec from the flags */
1220 ltype = lhs & CF_TYPE;
1221 rtype = rhs & CF_TYPE;
1223 /* Check if a conversion is needed */
1224 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1225 /* We must promote the primary register to long */
1227 /* Get the new rhs type */
1228 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1230 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1231 /* We must promote the lhs to long */
1237 /* Get the new rhs type */
1238 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1242 /* Determine the result type for the operation:
1243 * - The result is const if both operands are const.
1244 * - The result is unsigned if one of the operands is unsigned.
1245 * - The result is long if one of the operands is long.
1246 * - Otherwise the result is int sized.
1248 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1249 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1250 if (rtype == CF_LONG || ltype == CF_LONG) {
1260 unsigned g_typecast (unsigned lhs, unsigned rhs)
1261 /* Cast the value in the primary register to the operand size that is flagged
1262 * by the lhs value. Return the result value.
1265 unsigned ltype, rtype;
1267 /* Get the type spec from the flags */
1268 ltype = lhs & CF_TYPE;
1269 rtype = rhs & CF_TYPE;
1271 /* Check if a conversion is needed */
1272 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1273 /* We must promote the primary register to long */
1277 /* Do not need any other action. If the left type is int, and the primary
1278 * register is long, it will be automagically truncated. If the right hand
1279 * side is const, it is not located in the primary register and handled by
1280 * the expression parser code.
1283 /* Result is const if the right hand side was const */
1284 lhs |= (rhs & CF_CONST);
1286 /* The resulting type is that of the left hand side (that's why you called
1294 void g_scale (unsigned flags, long val)
1295 /* Scale the value in the primary register by the given value. If val is positive,
1296 * scale up, is val is negative, scale down. This function is used to scale
1297 * the operands or results of pointer arithmetic by the size of the type, the
1298 * pointer points to.
1303 /* Value may not be zero */
1305 Internal ("Data type has no size");
1306 } else if (val > 0) {
1309 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1311 /* Factor is 2, 4, 8 and 16, use special function */
1312 switch (flags & CF_TYPE) {
1315 if (flags & CF_FORCECHAR) {
1317 AddCodeLine ("asl a");
1324 if (CodeSizeFactor >= (p2+1)*130U) {
1325 AddCodeLine ("stx tmp1");
1327 AddCodeLine ("asl a");
1328 AddCodeLine ("rol tmp1");
1330 AddCodeLine ("ldx tmp1");
1332 if (flags & CF_UNSIGNED) {
1333 AddCodeLine ("jsr shlax%d", p2);
1335 AddCodeLine ("jsr aslax%d", p2);
1341 if (flags & CF_UNSIGNED) {
1342 AddCodeLine ("jsr shleax%d", p2);
1344 AddCodeLine ("jsr asleax%d", p2);
1353 } else if (val != 1) {
1355 /* Use a multiplication instead */
1356 g_mul (flags | CF_CONST, val);
1364 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1366 /* Factor is 2, 4, 8 and 16 use special function */
1367 switch (flags & CF_TYPE) {
1370 if (flags & CF_FORCECHAR) {
1371 if (flags & CF_UNSIGNED) {
1373 AddCodeLine ("lsr a");
1376 } else if (p2 <= 2) {
1377 AddCodeLine ("cmp #$80");
1378 AddCodeLine ("ror a");
1385 if (flags & CF_UNSIGNED) {
1386 if (CodeSizeFactor >= (p2+1)*130U) {
1387 AddCodeLine ("stx tmp1");
1389 AddCodeLine ("lsr tmp1");
1390 AddCodeLine ("ror a");
1392 AddCodeLine ("ldx tmp1");
1394 AddCodeLine ("jsr lsrax%d", p2);
1397 if (CodeSizeFactor >= (p2+1)*150U) {
1398 AddCodeLine ("stx tmp1");
1400 AddCodeLine ("cpx #$80");
1401 AddCodeLine ("ror tmp1");
1402 AddCodeLine ("ror a");
1404 AddCodeLine ("ldx tmp1");
1406 AddCodeLine ("jsr asrax%d", p2);
1412 if (flags & CF_UNSIGNED) {
1413 AddCodeLine ("jsr lsreax%d", p2);
1415 AddCodeLine ("jsr asreax%d", p2);
1424 } else if (val != 1) {
1426 /* Use a division instead */
1427 g_div (flags | CF_CONST, val);
1435 /*****************************************************************************/
1436 /* Adds and subs of variables fix a fixed address */
1437 /*****************************************************************************/
1441 void g_addlocal (unsigned flags, int offs)
1442 /* Add a local variable to ax */
1446 /* Correct the offset and check it */
1448 CheckLocalOffs (offs);
1450 switch (flags & CF_TYPE) {
1453 L = GetLocalLabel();
1454 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1455 AddCodeLine ("clc");
1456 AddCodeLine ("adc (sp),y");
1457 AddCodeLine ("bcc %s", LocalLabelName (L));
1458 AddCodeLine ("inx");
1463 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1464 AddCodeLine ("clc");
1465 AddCodeLine ("adc (sp),y");
1466 AddCodeLine ("pha");
1467 AddCodeLine ("txa");
1468 AddCodeLine ("iny");
1469 AddCodeLine ("adc (sp),y");
1470 AddCodeLine ("tax");
1471 AddCodeLine ("pla");
1475 /* Do it the old way */
1477 g_getlocal (flags, offs);
1489 void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
1490 /* Add a static variable to ax */
1494 /* Create the correct label name */
1495 char* lbuf = GetLabelName (flags, label, offs);
1497 switch (flags & CF_TYPE) {
1500 L = GetLocalLabel();
1501 AddCodeLine ("clc");
1502 AddCodeLine ("adc %s", lbuf);
1503 AddCodeLine ("bcc %s", LocalLabelName (L));
1504 AddCodeLine ("inx");
1509 AddCodeLine ("clc");
1510 AddCodeLine ("adc %s", lbuf);
1511 AddCodeLine ("tay");
1512 AddCodeLine ("txa");
1513 AddCodeLine ("adc %s+1", lbuf);
1514 AddCodeLine ("tax");
1515 AddCodeLine ("tya");
1519 /* Do it the old way */
1521 g_getstatic (flags, label, offs);
1533 /*****************************************************************************/
1534 /* Compares of ax with a variable with fixed address */
1535 /*****************************************************************************/
1539 void g_cmplocal (unsigned flags, int offs)
1540 /* Compare a local variable to ax */
1542 Internal ("g_cmplocal not implemented");
1547 void g_cmpstatic (unsigned flags, unsigned label, unsigned offs)
1548 /* Compare a static variable to ax */
1550 Internal ("g_cmpstatic not implemented");
1555 /*****************************************************************************/
1556 /* Special op= functions */
1557 /*****************************************************************************/
1561 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
1563 /* Emit += for a static variable */
1565 /* Create the correct label name */
1566 char* lbuf = GetLabelName (flags, label, offs);
1568 /* Check the size and determine operation */
1569 switch (flags & CF_TYPE) {
1572 if (flags & CF_FORCECHAR) {
1573 AddCodeLine ("ldx #$00");
1574 if (flags & CF_CONST) {
1576 AddCodeLine ("inc %s", lbuf);
1577 AddCodeLine ("lda %s", lbuf);
1579 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1580 AddCodeLine ("clc");
1581 AddCodeLine ("adc %s", lbuf);
1582 AddCodeLine ("sta %s", lbuf);
1585 AddCodeLine ("clc");
1586 AddCodeLine ("adc %s", lbuf);
1587 AddCodeLine ("sta %s", lbuf);
1589 if ((flags & CF_UNSIGNED) == 0) {
1590 unsigned L = GetLocalLabel();
1591 AddCodeLine ("bpl %s", LocalLabelName (L));
1592 AddCodeLine ("dex");
1600 if (flags & CF_CONST) {
1602 unsigned L = GetLocalLabel ();
1603 AddCodeLine ("inc %s", lbuf);
1604 AddCodeLine ("bne %s", LocalLabelName (L));
1605 AddCodeLine ("inc %s+1", lbuf);
1607 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1608 AddCodeLine ("ldx %s+1", lbuf);
1610 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1611 AddCodeLine ("clc");
1612 AddCodeLine ("adc %s", lbuf);
1613 AddCodeLine ("sta %s", lbuf);
1615 unsigned L = GetLocalLabel ();
1616 AddCodeLine ("bcc %s", LocalLabelName (L));
1617 AddCodeLine ("inc %s+1", lbuf);
1619 AddCodeLine ("ldx %s+1", lbuf);
1621 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1622 AddCodeLine ("adc %s+1", lbuf);
1623 AddCodeLine ("sta %s+1", lbuf);
1624 AddCodeLine ("tax");
1625 AddCodeLine ("lda %s", lbuf);
1629 AddCodeLine ("clc");
1630 AddCodeLine ("adc %s", lbuf);
1631 AddCodeLine ("sta %s", lbuf);
1632 AddCodeLine ("txa");
1633 AddCodeLine ("adc %s+1", lbuf);
1634 AddCodeLine ("sta %s+1", lbuf);
1635 AddCodeLine ("tax");
1636 AddCodeLine ("lda %s", lbuf);
1641 if (flags & CF_CONST) {
1643 AddCodeLine ("ldy #<(%s)", lbuf);
1644 AddCodeLine ("sty ptr1");
1645 AddCodeLine ("ldy #>(%s+1)", lbuf);
1647 AddCodeLine ("jsr laddeq1");
1649 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1650 AddCodeLine ("jsr laddeqa");
1653 g_getstatic (flags, label, offs);
1655 g_putstatic (flags, label, offs);
1658 AddCodeLine ("ldy #<(%s)", lbuf);
1659 AddCodeLine ("sty ptr1");
1660 AddCodeLine ("ldy #>(%s+1)", lbuf);
1661 AddCodeLine ("jsr laddeq");
1672 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1673 /* Emit += for a local variable */
1675 /* Calculate the true offset, check it, load it into Y */
1677 CheckLocalOffs (offs);
1679 /* Check the size and determine operation */
1680 switch (flags & CF_TYPE) {
1683 if (flags & CF_FORCECHAR) {
1685 AddCodeLine ("ldx #$00");
1686 if (flags & CF_CONST) {
1687 AddCodeLine ("clc");
1688 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1689 AddCodeLine ("adc (sp),y");
1690 AddCodeLine ("sta (sp),y");
1692 AddCodeLine ("clc");
1693 AddCodeLine ("adc (sp),y");
1694 AddCodeLine ("sta (sp),y");
1696 if ((flags & CF_UNSIGNED) == 0) {
1697 unsigned L = GetLocalLabel();
1698 AddCodeLine ("bpl %s", LocalLabelName (L));
1699 AddCodeLine ("dex");
1707 if (flags & CF_CONST) {
1708 g_getimmed (flags, val, 0);
1711 AddCodeLine ("jsr addeqysp");
1715 if (flags & CF_CONST) {
1716 g_getimmed (flags, val, 0);
1719 AddCodeLine ("jsr laddeq0sp");
1722 AddCodeLine ("jsr laddeqysp");
1733 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1734 /* Emit += for the location with address in ax */
1736 /* If the offset is too large for a byte register, add the high byte
1737 * of the offset to the primary. Beware: We need a special correction
1738 * if the offset in the low byte will overflow in the operation.
1740 offs = MakeByteOffs (flags, offs);
1742 /* Check the size and determine operation */
1743 switch (flags & CF_TYPE) {
1746 AddCodeLine ("sta ptr1");
1747 AddCodeLine ("stx ptr1+1");
1748 AddCodeLine ("ldy #$%02X", offs);
1749 AddCodeLine ("ldx #$00");
1750 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1751 AddCodeLine ("clc");
1752 AddCodeLine ("adc (ptr1),y");
1753 AddCodeLine ("sta (ptr1),y");
1757 if (CodeSizeFactor >= 200) {
1758 /* Lots of code, use only if size is not important */
1759 AddCodeLine ("sta ptr1");
1760 AddCodeLine ("stx ptr1+1");
1761 AddCodeLine ("ldy #$%02X", offs);
1762 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1763 AddCodeLine ("clc");
1764 AddCodeLine ("adc (ptr1),y");
1765 AddCodeLine ("sta (ptr1),y");
1766 AddCodeLine ("pha");
1767 AddCodeLine ("iny");
1768 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1769 AddCodeLine ("adc (ptr1),y");
1770 AddCodeLine ("sta (ptr1),y");
1771 AddCodeLine ("tax");
1772 AddCodeLine ("pla");
1778 AddCodeLine ("jsr pushax"); /* Push the address */
1779 push (CF_PTR); /* Correct the internal sp */
1780 g_getind (flags, offs); /* Fetch the value */
1781 g_inc (flags, val); /* Increment value in primary */
1782 g_putind (flags, offs); /* Store the value back */
1792 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1794 /* Emit -= for a static variable */
1796 /* Create the correct label name */
1797 char* lbuf = GetLabelName (flags, label, offs);
1799 /* Check the size and determine operation */
1800 switch (flags & CF_TYPE) {
1803 if (flags & CF_FORCECHAR) {
1804 AddCodeLine ("ldx #$00");
1805 if (flags & CF_CONST) {
1807 AddCodeLine ("dec %s", lbuf);
1808 AddCodeLine ("lda %s", lbuf);
1810 AddCodeLine ("sec");
1811 AddCodeLine ("lda %s", lbuf);
1812 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1813 AddCodeLine ("sta %s", lbuf);
1816 AddCodeLine ("sec");
1817 AddCodeLine ("sta tmp1");
1818 AddCodeLine ("lda %s", lbuf);
1819 AddCodeLine ("sbc tmp1");
1820 AddCodeLine ("sta %s", lbuf);
1822 if ((flags & CF_UNSIGNED) == 0) {
1823 unsigned L = GetLocalLabel();
1824 AddCodeLine ("bpl %s", LocalLabelName (L));
1825 AddCodeLine ("dex");
1833 AddCodeLine ("sec");
1834 if (flags & CF_CONST) {
1835 AddCodeLine ("lda %s", lbuf);
1836 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1837 AddCodeLine ("sta %s", lbuf);
1839 unsigned L = GetLocalLabel ();
1840 AddCodeLine ("bcs %s", LocalLabelName (L));
1841 AddCodeLine ("dec %s+1", lbuf);
1843 AddCodeLine ("ldx %s+1", lbuf);
1845 AddCodeLine ("lda %s+1", lbuf);
1846 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1847 AddCodeLine ("sta %s+1", lbuf);
1848 AddCodeLine ("tax");
1849 AddCodeLine ("lda %s", lbuf);
1852 AddCodeLine ("sta tmp1");
1853 AddCodeLine ("lda %s", lbuf);
1854 AddCodeLine ("sbc tmp1");
1855 AddCodeLine ("sta %s", lbuf);
1856 AddCodeLine ("stx tmp1");
1857 AddCodeLine ("lda %s+1", lbuf);
1858 AddCodeLine ("sbc tmp1");
1859 AddCodeLine ("sta %s+1", lbuf);
1860 AddCodeLine ("tax");
1861 AddCodeLine ("lda %s", lbuf);
1866 if (flags & CF_CONST) {
1868 AddCodeLine ("ldy #<(%s)", lbuf);
1869 AddCodeLine ("sty ptr1");
1870 AddCodeLine ("ldy #>(%s+1)", lbuf);
1872 AddCodeLine ("jsr lsubeq1");
1874 AddCodeLine ("lda #$%02X", (unsigned char)val);
1875 AddCodeLine ("jsr lsubeqa");
1878 g_getstatic (flags, label, offs);
1880 g_putstatic (flags, label, offs);
1883 AddCodeLine ("ldy #<(%s)", lbuf);
1884 AddCodeLine ("sty ptr1");
1885 AddCodeLine ("ldy #>(%s+1)", lbuf);
1886 AddCodeLine ("jsr lsubeq");
1897 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1898 /* Emit -= for a local variable */
1900 /* Calculate the true offset, check it, load it into Y */
1902 CheckLocalOffs (offs);
1904 /* Check the size and determine operation */
1905 switch (flags & CF_TYPE) {
1908 if (flags & CF_FORCECHAR) {
1910 AddCodeLine ("ldx #$00");
1911 AddCodeLine ("sec");
1912 if (flags & CF_CONST) {
1913 AddCodeLine ("lda (sp),y");
1914 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1916 AddCodeLine ("sta tmp1");
1917 AddCodeLine ("lda (sp),y");
1918 AddCodeLine ("sbc tmp1");
1920 AddCodeLine ("sta (sp),y");
1921 if ((flags & CF_UNSIGNED) == 0) {
1922 unsigned L = GetLocalLabel();
1923 AddCodeLine ("bpl %s", LocalLabelName (L));
1924 AddCodeLine ("dex");
1932 if (flags & CF_CONST) {
1933 g_getimmed (flags, val, 0);
1936 AddCodeLine ("jsr subeq0sp");
1939 AddCodeLine ("jsr subeqysp");
1944 if (flags & CF_CONST) {
1945 g_getimmed (flags, val, 0);
1948 AddCodeLine ("jsr lsubeq0sp");
1951 AddCodeLine ("jsr lsubeqysp");
1962 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
1963 /* Emit -= for the location with address in ax */
1965 /* If the offset is too large for a byte register, add the high byte
1966 * of the offset to the primary. Beware: We need a special correction
1967 * if the offset in the low byte will overflow in the operation.
1969 offs = MakeByteOffs (flags, offs);
1971 /* Check the size and determine operation */
1972 switch (flags & CF_TYPE) {
1975 AddCodeLine ("sta ptr1");
1976 AddCodeLine ("stx ptr1+1");
1977 AddCodeLine ("ldy #$%02X", offs);
1978 AddCodeLine ("ldx #$00");
1979 AddCodeLine ("lda (ptr1),y");
1980 AddCodeLine ("sec");
1981 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1982 AddCodeLine ("sta (ptr1),y");
1986 if (CodeSizeFactor >= 200) {
1987 /* Lots of code, use only if size is not important */
1988 AddCodeLine ("sta ptr1");
1989 AddCodeLine ("stx ptr1+1");
1990 AddCodeLine ("ldy #$%02X", offs);
1991 AddCodeLine ("lda (ptr1),y");
1992 AddCodeLine ("sec");
1993 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1994 AddCodeLine ("sta (ptr1),y");
1995 AddCodeLine ("pha");
1996 AddCodeLine ("iny");
1997 AddCodeLine ("lda (ptr1),y");
1998 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1999 AddCodeLine ("sta (ptr1),y");
2000 AddCodeLine ("tax");
2001 AddCodeLine ("pla");
2007 AddCodeLine ("jsr pushax"); /* Push the address */
2008 push (CF_PTR); /* Correct the internal sp */
2009 g_getind (flags, offs); /* Fetch the value */
2010 g_dec (flags, val); /* Increment value in primary */
2011 g_putind (flags, offs); /* Store the value back */
2021 /*****************************************************************************/
2022 /* Add a variable address to the value in ax */
2023 /*****************************************************************************/
2027 void g_addaddr_local (unsigned flags, int offs)
2028 /* Add the address of a local variable to ax */
2032 /* Add the offset */
2035 /* We cannot address more then 256 bytes of locals anyway */
2036 L = GetLocalLabel();
2037 CheckLocalOffs (offs);
2038 AddCodeLine ("clc");
2039 AddCodeLine ("adc #$%02X", offs & 0xFF);
2040 /* Do also skip the CLC insn below */
2041 AddCodeLine ("bcc %s", LocalLabelName (L));
2042 AddCodeLine ("inx");
2045 /* Add the current stackpointer value */
2046 AddCodeLine ("clc");
2048 /* Label was used above */
2051 AddCodeLine ("adc sp");
2052 AddCodeLine ("tay");
2053 AddCodeLine ("txa");
2054 AddCodeLine ("adc sp+1");
2055 AddCodeLine ("tax");
2056 AddCodeLine ("tya");
2061 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2062 /* Add the address of a static variable to ax */
2064 /* Create the correct label name */
2065 char* lbuf = GetLabelName (flags, label, offs);
2067 /* Add the address to the current ax value */
2068 AddCodeLine ("clc");
2069 AddCodeLine ("adc #<(%s)", lbuf);
2070 AddCodeLine ("tay");
2071 AddCodeLine ("txa");
2072 AddCodeLine ("adc #>(%s)", lbuf);
2073 AddCodeLine ("tax");
2074 AddCodeLine ("tya");
2079 /*****************************************************************************/
2081 /*****************************************************************************/
2085 void g_save (unsigned flags)
2086 /* Copy primary register to hold register. */
2088 /* Check the size and determine operation */
2089 switch (flags & CF_TYPE) {
2092 if (flags & CF_FORCECHAR) {
2093 AddCodeLine ("pha");
2099 AddCodeLine ("sta regsave");
2100 AddCodeLine ("stx regsave+1");
2104 AddCodeLine ("jsr saveeax");
2114 void g_restore (unsigned flags)
2115 /* Copy hold register to primary. */
2117 /* Check the size and determine operation */
2118 switch (flags & CF_TYPE) {
2121 if (flags & CF_FORCECHAR) {
2122 AddCodeLine ("pla");
2128 AddCodeLine ("lda regsave");
2129 AddCodeLine ("ldx regsave+1");
2133 AddCodeLine ("jsr resteax");
2143 void g_cmp (unsigned flags, unsigned long val)
2144 /* Immidiate compare. The primary register will not be changed, Z flag
2150 /* Check the size and determine operation */
2151 switch (flags & CF_TYPE) {
2154 if (flags & CF_FORCECHAR) {
2155 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2161 L = GetLocalLabel();
2162 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2163 AddCodeLine ("bne %s", LocalLabelName (L));
2164 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2169 Internal ("g_cmp: Long compares not implemented");
2179 static void oper (unsigned flags, unsigned long val, char** subs)
2180 /* Encode a binary operation. subs is a pointer to four groups of three
2182 * 0-2 --> Operate on ints
2183 * 3-5 --> Operate on unsigneds
2184 * 6-8 --> Operate on longs
2185 * 9-11 --> Operate on unsigned longs
2187 * The first subroutine names in each string group is used to encode an
2188 * operation with a zero constant, the second to encode an operation with
2189 * a 8 bit constant, and the third is used in all other cases.
2194 /* Determine the offset into the array */
2195 offs = (flags & CF_UNSIGNED)? 3 : 0;
2196 switch (flags & CF_TYPE) {
2209 /* Encode the operation */
2210 if (flags & CF_CONST) {
2211 /* Constant value given */
2212 if (val == 0 && subs [offs+0]) {
2213 /* Special case: constant with value zero */
2214 AddCodeLine ("jsr %s", subs [offs+0]);
2215 } else if (val < 0x100 && subs [offs+1]) {
2216 /* Special case: constant with high byte zero */
2217 ldaconst (val); /* Load low byte */
2218 AddCodeLine ("jsr %s", subs [offs+1]);
2220 /* Others: arbitrary constant value */
2221 g_getimmed (flags, val, 0); /* Load value */
2222 AddCodeLine ("jsr %s", subs [offs+2]);
2225 /* Value not constant (is already in (e)ax) */
2226 AddCodeLine ("jsr %s", subs [offs+2]);
2229 /* The operation will pop it's argument */
2235 void g_test (unsigned flags)
2236 /* Test the value in the primary and set the condition codes */
2238 switch (flags & CF_TYPE) {
2241 if (flags & CF_FORCECHAR) {
2242 AddCodeLine ("tax");
2248 AddCodeLine ("stx tmp1");
2249 AddCodeLine ("ora tmp1");
2253 if (flags & CF_UNSIGNED) {
2254 AddCodeLine ("jsr utsteax");
2256 AddCodeLine ("jsr tsteax");
2268 void g_push (unsigned flags, unsigned long val)
2269 /* Push the primary register or a constant value onto the stack */
2273 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2275 /* We have a constant 8 or 16 bit value */
2276 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2278 /* Handle as 8 bit value */
2279 if (CodeSizeFactor >= 165 || val > 2) {
2281 AddCodeLine ("jsr pusha");
2283 AddCodeLine ("jsr pushc%d", (int) val);
2288 /* Handle as 16 bit value */
2289 hi = (unsigned char) (val >> 8);
2291 AddCodeLine ("jsr push%u", (unsigned) val);
2292 } else if (hi == 0 || hi == 0xFF) {
2293 /* Use special function */
2295 AddCodeLine ("jsr %s", (hi == 0)? "pusha0" : "pushaFF");
2298 g_getimmed (flags, val, 0);
2299 AddCodeLine ("jsr pushax");
2305 /* Value is not 16 bit or not constant */
2306 if (flags & CF_CONST) {
2307 /* Constant 32 bit value, load into eax */
2308 g_getimmed (flags, val, 0);
2311 /* Push the primary register */
2312 switch (flags & CF_TYPE) {
2315 if (flags & CF_FORCECHAR) {
2316 /* Handle as char */
2317 AddCodeLine ("jsr pusha");
2322 AddCodeLine ("jsr pushax");
2326 AddCodeLine ("jsr pusheax");
2336 /* Adjust the stack offset */
2342 void g_swap (unsigned flags)
2343 /* Swap the primary register and the top of the stack. flags give the type
2344 * of *both* values (must have same size).
2347 switch (flags & CF_TYPE) {
2351 AddCodeLine ("jsr swapstk");
2355 AddCodeLine ("jsr swapestk");
2366 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2367 /* Call the specified subroutine name */
2369 if ((Flags & CF_FIXARGC) == 0) {
2370 /* Pass the argument count */
2373 AddCodeLine ("jsr _%s", Label);
2374 oursp += ArgSize; /* callee pops args */
2379 void g_callind (unsigned Flags, unsigned ArgSize)
2380 /* Call subroutine with address in AX */
2382 if ((Flags & CF_FIXARGC) == 0) {
2383 /* Pass arg count */
2386 AddCodeLine ("jsr callax"); /* do the call */
2387 oursp += ArgSize; /* callee pops args */
2392 void g_jump (unsigned Label)
2393 /* Jump to specified internal label number */
2395 AddCodeLine ("jmp %s", LocalLabelName (Label));
2400 void g_switch (unsigned Flags)
2401 /* Output switch statement preamble */
2403 switch (Flags & CF_TYPE) {
2407 AddCodeLine ("jsr switch");
2411 AddCodeLine ("jsr lswitch");
2422 void g_case (unsigned flags, unsigned label, unsigned long val)
2423 /* Create table code for one case selector */
2425 switch (flags & CF_TYPE) {
2429 AddCodeLine (".word $%04X, %s",
2430 (unsigned)(val & 0xFFFF),
2431 LocalLabelName (label));
2435 AddCodeLine (".dword $%08lX", val);
2436 AddCodeLine (".word %s", LocalLabelName (label));
2447 void g_truejump (unsigned flags, unsigned label)
2448 /* Jump to label if zero flag clear */
2450 AddCodeLine ("jne %s", LocalLabelName (label));
2455 void g_falsejump (unsigned flags, unsigned label)
2456 /* Jump to label if zero flag set */
2458 AddCodeLine ("jeq %s", LocalLabelName (label));
2463 static void mod_internal (int k, char* verb1, char* verb2)
2466 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2470 AddCodeLine ("jsr %ssp", verb2);
2476 void g_space (int space)
2477 /* Create or drop space on the stack */
2480 mod_internal (-space, "inc", "addy");
2481 } else if (space > 0) {
2482 mod_internal (space, "dec", "suby");
2488 void g_cstackcheck (void)
2489 /* Check for a C stack overflow */
2491 AddCodeLine ("jsr cstkchk");
2496 void g_stackcheck (void)
2497 /* Check for a stack overflow */
2499 AddCodeLine ("jsr stkchk");
2504 void g_add (unsigned flags, unsigned long val)
2505 /* Primary = TOS + Primary */
2507 static char* ops [12] = {
2508 0, "tosadda0", "tosaddax",
2509 0, "tosadda0", "tosaddax",
2514 if (flags & CF_CONST) {
2515 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2516 g_push (flags & ~CF_CONST, 0);
2518 oper (flags, val, ops);
2523 void g_sub (unsigned flags, unsigned long val)
2524 /* Primary = TOS - Primary */
2526 static char* ops [12] = {
2527 0, "tossuba0", "tossubax",
2528 0, "tossuba0", "tossubax",
2533 if (flags & CF_CONST) {
2534 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2535 g_push (flags & ~CF_CONST, 0);
2537 oper (flags, val, ops);
2542 void g_rsub (unsigned flags, unsigned long val)
2543 /* Primary = Primary - TOS */
2545 static char* ops [12] = {
2546 0, "tosrsuba0", "tosrsubax",
2547 0, "tosrsuba0", "tosrsubax",
2551 oper (flags, val, ops);
2556 void g_mul (unsigned flags, unsigned long val)
2557 /* Primary = TOS * Primary */
2559 static char* ops [12] = {
2560 0, "tosmula0", "tosmulax",
2561 0, "tosumula0", "tosumulax",
2568 /* Do strength reduction if the value is constant and a power of two */
2569 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2570 /* Generate a shift instead */
2575 /* If the right hand side is const, the lhs is not on stack but still
2576 * in the primary register.
2578 if (flags & CF_CONST) {
2580 switch (flags & CF_TYPE) {
2583 if (flags & CF_FORCECHAR) {
2584 /* Handle some special cases */
2588 AddCodeLine ("sta tmp1");
2589 AddCodeLine ("asl a");
2590 AddCodeLine ("clc");
2591 AddCodeLine ("adc tmp1");
2595 AddCodeLine ("sta tmp1");
2596 AddCodeLine ("asl a");
2597 AddCodeLine ("asl a");
2598 AddCodeLine ("clc");
2599 AddCodeLine ("adc tmp1");
2603 AddCodeLine ("sta tmp1");
2604 AddCodeLine ("asl a");
2605 AddCodeLine ("asl a");
2606 AddCodeLine ("clc");
2607 AddCodeLine ("adc tmp1");
2608 AddCodeLine ("asl a");
2624 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2625 * into the normal, non-optimized stuff.
2627 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2628 g_push (flags & ~CF_CONST, 0);
2632 /* Use long way over the stack */
2633 oper (flags, val, ops);
2638 void g_div (unsigned flags, unsigned long val)
2639 /* Primary = TOS / Primary */
2641 static char* ops [12] = {
2642 0, "tosdiva0", "tosdivax",
2643 0, "tosudiva0", "tosudivax",
2648 /* Do strength reduction if the value is constant and a power of two */
2650 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2651 /* Generate a shift instead */
2654 /* Generate a division */
2655 if (flags & CF_CONST) {
2656 /* lhs is not on stack */
2657 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2658 g_push (flags & ~CF_CONST, 0);
2660 oper (flags, val, ops);
2666 void g_mod (unsigned flags, unsigned long val)
2667 /* Primary = TOS % Primary */
2669 static char* ops [12] = {
2670 0, "tosmoda0", "tosmodax",
2671 0, "tosumoda0", "tosumodax",
2677 /* Check if we can do some cost reduction */
2678 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2679 /* We can do that with an AND operation */
2680 g_and (flags, val - 1);
2682 /* Do it the hard way... */
2683 if (flags & CF_CONST) {
2684 /* lhs is not on stack */
2685 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2686 g_push (flags & ~CF_CONST, 0);
2688 oper (flags, val, ops);
2694 void g_or (unsigned flags, unsigned long val)
2695 /* Primary = TOS | Primary */
2697 static char* ops [12] = {
2698 0, "tosora0", "tosorax",
2699 0, "tosora0", "tosorax",
2704 /* If the right hand side is const, the lhs is not on stack but still
2705 * in the primary register.
2707 if (flags & CF_CONST) {
2709 switch (flags & CF_TYPE) {
2712 if (flags & CF_FORCECHAR) {
2713 if ((val & 0xFF) != 0xFF) {
2714 AddCodeLine ("ora #$%02X", (unsigned char)val);
2722 AddCodeLine ("ora #$%02X", (unsigned char)val);
2729 AddCodeLine ("ora #$%02X", (unsigned char)val);
2738 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2739 * into the normal, non-optimized stuff.
2741 g_push (flags & ~CF_CONST, 0);
2745 /* Use long way over the stack */
2746 oper (flags, val, ops);
2751 void g_xor (unsigned flags, unsigned long val)
2752 /* Primary = TOS ^ Primary */
2754 static char* ops [12] = {
2755 0, "tosxora0", "tosxorax",
2756 0, "tosxora0", "tosxorax",
2762 /* If the right hand side is const, the lhs is not on stack but still
2763 * in the primary register.
2765 if (flags & CF_CONST) {
2767 switch (flags & CF_TYPE) {
2770 if (flags & CF_FORCECHAR) {
2771 if ((val & 0xFF) != 0) {
2772 AddCodeLine ("eor #$%02X", (unsigned char)val);
2781 AddCodeLine ("eor #$%02X", (unsigned char)val);
2784 } else if ((val & 0xFF) == 0) {
2785 AddCodeLine ("pha");
2786 AddCodeLine ("txa");
2787 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2788 AddCodeLine ("tax");
2789 AddCodeLine ("pla");
2797 AddCodeLine ("eor #$%02X", (unsigned char)val);
2807 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2808 * into the normal, non-optimized stuff.
2810 g_push (flags & ~CF_CONST, 0);
2814 /* Use long way over the stack */
2815 oper (flags, val, ops);
2820 void g_and (unsigned flags, unsigned long val)
2821 /* Primary = TOS & Primary */
2823 static char* ops [12] = {
2824 0, "tosanda0", "tosandax",
2825 0, "tosanda0", "tosandax",
2830 /* If the right hand side is const, the lhs is not on stack but still
2831 * in the primary register.
2833 if (flags & CF_CONST) {
2835 switch (flags & CF_TYPE) {
2838 if (flags & CF_FORCECHAR) {
2839 AddCodeLine ("and #$%02X", (unsigned char)val);
2844 if ((val & 0xFFFF) != 0xFFFF) {
2849 } else if (val != 0xFF) {
2850 AddCodeLine ("and #$%02X", (unsigned char)val);
2852 } else if ((val & 0xFF00) == 0xFF00) {
2853 AddCodeLine ("and #$%02X", (unsigned char)val);
2854 } else if ((val & 0x00FF) == 0x0000) {
2855 AddCodeLine ("txa");
2856 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2857 AddCodeLine ("tax");
2860 AddCodeLine ("tay");
2861 AddCodeLine ("txa");
2862 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2863 AddCodeLine ("tax");
2864 AddCodeLine ("tya");
2865 if ((val & 0x00FF) != 0x00FF) {
2866 AddCodeLine ("and #$%02X", (unsigned char)val);
2875 AddCodeLine ("stx sreg+1");
2876 AddCodeLine ("stx sreg");
2877 if ((val & 0xFF) != 0xFF) {
2878 AddCodeLine ("and #$%02X", (unsigned char)val);
2881 } else if (val == 0xFF00) {
2883 AddCodeLine ("sta sreg+1");
2884 AddCodeLine ("sta sreg");
2893 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2894 * into the normal, non-optimized stuff.
2896 g_push (flags & ~CF_CONST, 0);
2900 /* Use long way over the stack */
2901 oper (flags, val, ops);
2906 void g_asr (unsigned flags, unsigned long val)
2907 /* Primary = TOS >> Primary */
2909 static char* ops [12] = {
2910 0, "tosasra0", "tosasrax",
2911 0, "tosshra0", "tosshrax",
2916 /* If the right hand side is const, the lhs is not on stack but still
2917 * in the primary register.
2919 if (flags & CF_CONST) {
2921 switch (flags & CF_TYPE) {
2925 if (val >= 1 && val <= 4) {
2926 if (flags & CF_UNSIGNED) {
2927 AddCodeLine ("jsr shrax%ld", val);
2929 AddCodeLine ("jsr asrax%ld", val);
2932 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2933 AddCodeLine ("txa");
2940 if (val >= 1 && val <= 4) {
2941 if (flags & CF_UNSIGNED) {
2942 AddCodeLine ("jsr shreax%ld", val);
2944 AddCodeLine ("jsr asreax%ld", val);
2947 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2948 AddCodeLine ("txa");
2949 AddCodeLine ("ldx sreg");
2950 AddCodeLine ("ldy sreg+1");
2951 AddCodeLine ("sty sreg");
2952 AddCodeLine ("ldy #$00");
2953 AddCodeLine ("sty sreg+1");
2955 } else if (val == 16) {
2956 AddCodeLine ("ldy #$00");
2957 AddCodeLine ("ldx sreg+1");
2958 if ((flags & CF_UNSIGNED) == 0) {
2959 unsigned L = GetLocalLabel();
2960 AddCodeLine ("bpl %s", LocalLabelName (L));
2961 AddCodeLine ("dey");
2964 AddCodeLine ("lda sreg");
2965 AddCodeLine ("sty sreg+1");
2966 AddCodeLine ("sty sreg");
2975 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2976 * into the normal, non-optimized stuff.
2978 g_push (flags & ~CF_CONST, 0);
2982 /* Use long way over the stack */
2983 oper (flags, val, ops);
2988 void g_asl (unsigned flags, unsigned long val)
2989 /* Primary = TOS << Primary */
2991 static char* ops [12] = {
2992 0, "tosasla0", "tosaslax",
2993 0, "tosshla0", "tosshlax",
2999 /* If the right hand side is const, the lhs is not on stack but still
3000 * in the primary register.
3002 if (flags & CF_CONST) {
3004 switch (flags & CF_TYPE) {
3008 if (val >= 1 && val <= 4) {
3009 if (flags & CF_UNSIGNED) {
3010 AddCodeLine ("jsr shlax%ld", val);
3012 AddCodeLine ("jsr aslax%ld", val);
3015 } else if (val == 8) {
3016 AddCodeLine ("tax");
3017 AddCodeLine ("lda #$00");
3023 if (val >= 1 && val <= 4) {
3024 if (flags & CF_UNSIGNED) {
3025 AddCodeLine ("jsr shleax%ld", val);
3027 AddCodeLine ("jsr asleax%ld", val);
3030 } else if (val == 8) {
3031 AddCodeLine ("ldy sreg");
3032 AddCodeLine ("sty sreg+1");
3033 AddCodeLine ("stx sreg");
3034 AddCodeLine ("tax");
3035 AddCodeLine ("lda #$00");
3037 } else if (val == 16) {
3038 AddCodeLine ("stx sreg+1");
3039 AddCodeLine ("sta sreg");
3040 AddCodeLine ("lda #$00");
3041 AddCodeLine ("tax");
3050 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3051 * into the normal, non-optimized stuff.
3053 g_push (flags & ~CF_CONST, 0);
3057 /* Use long way over the stack */
3058 oper (flags, val, ops);
3063 void g_neg (unsigned flags)
3064 /* Primary = -Primary */
3066 switch (flags & CF_TYPE) {
3070 AddCodeLine ("jsr negax");
3074 AddCodeLine ("jsr negeax");
3084 void g_bneg (unsigned flags)
3085 /* Primary = !Primary */
3087 switch (flags & CF_TYPE) {
3090 AddCodeLine ("jsr bnega");
3094 AddCodeLine ("jsr bnegax");
3098 AddCodeLine ("jsr bnegeax");
3108 void g_com (unsigned flags)
3109 /* Primary = ~Primary */
3111 switch (flags & CF_TYPE) {
3115 AddCodeLine ("jsr complax");
3119 AddCodeLine ("jsr compleax");
3129 void g_inc (unsigned flags, unsigned long val)
3130 /* Increment the primary register by a given number */
3132 /* Don't inc by zero */
3137 /* Generate code for the supported types */
3139 switch (flags & CF_TYPE) {
3142 if (flags & CF_FORCECHAR) {
3143 if (CPU == CPU_65C02 && val <= 2) {
3145 AddCodeLine ("ina");
3148 AddCodeLine ("clc");
3149 AddCodeLine ("adc #$%02X", (unsigned char)val);
3156 if (CPU == CPU_65C02 && val == 1) {
3157 unsigned L = GetLocalLabel();
3158 AddCodeLine ("ina");
3159 AddCodeLine ("bne %s", LocalLabelName (L));
3160 AddCodeLine ("inx");
3162 } else if (CodeSizeFactor < 200) {
3165 AddCodeLine ("jsr incax%lu", val);
3166 } else if (val <= 255) {
3168 AddCodeLine ("jsr incaxy");
3170 g_add (flags | CF_CONST, val);
3173 /* Inline the code */
3175 if ((val & 0xFF) != 0) {
3176 unsigned L = GetLocalLabel();
3177 AddCodeLine ("clc");
3178 AddCodeLine ("adc #$%02X", (unsigned char) val);
3179 AddCodeLine ("bcc %s", LocalLabelName (L));
3180 AddCodeLine ("inx");
3184 AddCodeLine ("inx");
3187 AddCodeLine ("inx");
3190 AddCodeLine ("clc");
3191 if ((val & 0xFF) != 0) {
3192 AddCodeLine ("adc #$%02X", (unsigned char) val);
3194 AddCodeLine ("pha");
3195 AddCodeLine ("txa");
3196 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3197 AddCodeLine ("tax");
3198 AddCodeLine ("pla");
3206 AddCodeLine ("jsr inceaxy");
3208 g_add (flags | CF_CONST, val);
3220 void g_dec (unsigned flags, unsigned long val)
3221 /* Decrement the primary register by a given number */
3223 /* Don't dec by zero */
3228 /* Generate code for the supported types */
3230 switch (flags & CF_TYPE) {
3233 if (flags & CF_FORCECHAR) {
3234 if (CPU == CPU_65C02 && val <= 2) {
3236 AddCodeLine ("dea");
3239 AddCodeLine ("sec");
3240 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3247 if (CodeSizeFactor < 200) {
3248 /* Use subroutines */
3250 AddCodeLine ("jsr decax%d", (int) val);
3251 } else if (val <= 255) {
3253 AddCodeLine ("jsr decaxy");
3255 g_sub (flags | CF_CONST, val);
3258 /* Inline the code */
3260 if ((val & 0xFF) != 0) {
3261 unsigned L = GetLocalLabel();
3262 AddCodeLine ("sec");
3263 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3264 AddCodeLine ("bcs %s", LocalLabelName (L));
3265 AddCodeLine ("dex");
3269 AddCodeLine ("dex");
3272 AddCodeLine ("dex");
3275 AddCodeLine ("sec");
3276 if ((val & 0xFF) != 0) {
3277 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3279 AddCodeLine ("pha");
3280 AddCodeLine ("txa");
3281 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3282 AddCodeLine ("tax");
3283 AddCodeLine ("pla");
3291 AddCodeLine ("jsr deceaxy");
3293 g_sub (flags | CF_CONST, val);
3306 * Following are the conditional operators. They compare the TOS against
3307 * the primary and put a literal 1 in the primary if the condition is
3308 * true, otherwise they clear the primary register
3313 void g_eq (unsigned flags, unsigned long val)
3314 /* Test for equal */
3316 static char* ops [12] = {
3317 "toseq00", "toseqa0", "toseqax",
3318 "toseq00", "toseqa0", "toseqax",
3325 /* If the right hand side is const, the lhs is not on stack but still
3326 * in the primary register.
3328 if (flags & CF_CONST) {
3330 switch (flags & CF_TYPE) {
3333 if (flags & CF_FORCECHAR) {
3334 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3335 AddCodeLine ("jsr booleq");
3341 L = GetLocalLabel();
3342 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3343 AddCodeLine ("bne %s", LocalLabelName (L));
3344 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3346 AddCodeLine ("jsr booleq");
3356 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3357 * into the normal, non-optimized stuff.
3359 g_push (flags & ~CF_CONST, 0);
3363 /* Use long way over the stack */
3364 oper (flags, val, ops);
3369 void g_ne (unsigned flags, unsigned long val)
3370 /* Test for not equal */
3372 static char* ops [12] = {
3373 "tosne00", "tosnea0", "tosneax",
3374 "tosne00", "tosnea0", "tosneax",
3381 /* If the right hand side is const, the lhs is not on stack but still
3382 * in the primary register.
3384 if (flags & CF_CONST) {
3386 switch (flags & CF_TYPE) {
3389 if (flags & CF_FORCECHAR) {
3390 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3391 AddCodeLine ("jsr boolne");
3397 L = GetLocalLabel();
3398 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3399 AddCodeLine ("bne %s", LocalLabelName (L));
3400 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3402 AddCodeLine ("jsr boolne");
3412 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3413 * into the normal, non-optimized stuff.
3415 g_push (flags & ~CF_CONST, 0);
3419 /* Use long way over the stack */
3420 oper (flags, val, ops);
3425 void g_lt (unsigned flags, unsigned long val)
3426 /* Test for less than */
3428 static char* ops [12] = {
3429 "toslt00", "toslta0", "tosltax",
3430 "tosult00", "tosulta0", "tosultax",
3435 /* If the right hand side is const, the lhs is not on stack but still
3436 * in the primary register.
3438 if (flags & CF_CONST) {
3440 /* Give a warning in some special cases */
3441 if ((flags & CF_UNSIGNED) && val == 0) {
3442 Warning ("Condition is never true");
3445 /* Look at the type */
3446 switch (flags & CF_TYPE) {
3449 if (flags & CF_FORCECHAR) {
3450 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3451 if (flags & CF_UNSIGNED) {
3452 AddCodeLine ("jsr boolult");
3454 AddCodeLine ("jsr boollt");
3461 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3462 /* If we have a signed compare against zero, we only need to
3463 * test the high byte.
3465 AddCodeLine ("txa");
3466 AddCodeLine ("jsr boollt");
3469 /* Direct code only for unsigned data types */
3470 if (flags & CF_UNSIGNED) {
3471 unsigned L = GetLocalLabel();
3472 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3473 AddCodeLine ("bne %s", LocalLabelName (L));
3474 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3476 AddCodeLine ("jsr boolult");
3482 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3483 /* If we have a signed compare against zero, we only need to
3484 * test the high byte.
3486 AddCodeLine ("lda sreg+1");
3487 AddCodeLine ("jsr boollt");
3496 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3497 * into the normal, non-optimized stuff.
3499 g_push (flags & ~CF_CONST, 0);
3503 /* Use long way over the stack */
3504 oper (flags, val, ops);
3509 void g_le (unsigned flags, unsigned long val)
3510 /* Test for less than or equal to */
3512 static char* ops [12] = {
3513 "tosle00", "toslea0", "tosleax",
3514 "tosule00", "tosulea0", "tosuleax",
3520 /* If the right hand side is const, the lhs is not on stack but still
3521 * in the primary register.
3523 if (flags & CF_CONST) {
3525 /* <= is not very effective on the 6502, so try to convert
3526 * it into < if the value is in a valid range.
3529 /* Look at the type */
3530 switch (flags & CF_TYPE) {
3533 if (flags & CF_FORCECHAR) {
3534 if (flags & CF_UNSIGNED) {
3536 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3537 AddCodeLine ("jsr boolult");
3539 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3540 AddCodeLine ("jsr boolule");
3544 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3545 AddCodeLine ("jsr boollt");
3547 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3548 AddCodeLine ("jsr boolle");
3556 if (flags & CF_UNSIGNED) {
3557 unsigned L = GetLocalLabel();
3558 const char* Name = "boolule";
3563 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3564 AddCodeLine ("bne %s", LocalLabelName (L));
3565 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3567 AddCodeLine ("jsr %s", Name);
3579 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3580 * into the normal, non-optimized stuff.
3582 g_push (flags & ~CF_CONST, 0);
3586 /* Use long way over the stack */
3587 oper (flags, val, ops);
3592 void g_gt (unsigned flags, unsigned long val)
3593 /* Test for greater than */
3595 static char* ops [12] = {
3596 "tosgt00", "tosgta0", "tosgtax",
3597 "tosugt00", "tosugta0", "tosugtax",
3603 /* If the right hand side is const, the lhs is not on stack but still
3604 * in the primary register.
3606 if (flags & CF_CONST) {
3608 /* > is not very effective on the 6502, so try to convert
3609 * it into >= if the value is in a valid range.
3612 /* Look at the type */
3613 switch (flags & CF_TYPE) {
3616 if (flags & CF_FORCECHAR) {
3617 if (flags & CF_UNSIGNED) {
3618 /* If we have a compare > 0, we will replace it by
3619 * != 0 here, since both are identical but the latter
3620 * is easier to optimize.
3623 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3624 AddCodeLine ("jsr boolne");
3625 } else if (val < 255) {
3626 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3627 AddCodeLine ("jsr booluge");
3629 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3630 AddCodeLine ("jsr boolugt");
3634 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3635 AddCodeLine ("jsr boolge");
3637 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3638 AddCodeLine ("jsr boolgt");
3646 if (flags & CF_UNSIGNED) {
3647 /* If we have a compare > 0, we will replace it by
3648 * != 0 here, since both are identical but the latter
3649 * is easier to optimize.
3652 AddCodeLine ("stx tmp1");
3653 AddCodeLine ("ora tmp1");
3654 AddCodeLine ("jsr boolne");
3656 unsigned L = GetLocalLabel();
3657 const char* Name = "boolugt";
3662 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3663 AddCodeLine ("bne %s", LocalLabelName (L));
3664 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3666 AddCodeLine ("jsr %s", Name);
3679 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3680 * into the normal, non-optimized stuff.
3682 g_push (flags & ~CF_CONST, 0);
3686 /* Use long way over the stack */
3687 oper (flags, val, ops);
3692 void g_ge (unsigned flags, unsigned long val)
3693 /* Test for greater than or equal to */
3695 static char* ops [12] = {
3696 "tosge00", "tosgea0", "tosgeax",
3697 "tosuge00", "tosugea0", "tosugeax",
3703 /* If the right hand side is const, the lhs is not on stack but still
3704 * in the primary register.
3706 if (flags & CF_CONST) {
3708 /* Give a warning in some special cases */
3709 if ((flags & CF_UNSIGNED) && val == 0) {
3710 Warning ("Condition is always true");
3713 /* Look at the type */
3714 switch (flags & CF_TYPE) {
3717 if (flags & CF_FORCECHAR) {
3718 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3719 if (flags & CF_UNSIGNED) {
3720 AddCodeLine ("jsr booluge");
3722 AddCodeLine ("jsr boolge");
3729 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3730 /* If we have a signed compare against zero, we only need to
3731 * test the high byte.
3733 AddCodeLine ("txa");
3734 AddCodeLine ("jsr boolge");
3737 /* Direct code only for unsigned data types */
3738 if (flags & CF_UNSIGNED) {
3739 unsigned L = GetLocalLabel();
3740 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3741 AddCodeLine ("bne %s", LocalLabelName (L));
3742 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3744 AddCodeLine ("jsr booluge");
3750 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3751 /* If we have a signed compare against zero, we only need to
3752 * test the high byte.
3754 AddCodeLine ("lda sreg+1");
3755 AddCodeLine ("jsr boolge");
3764 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3765 * into the normal, non-optimized stuff.
3767 g_push (flags & ~CF_CONST, 0);
3771 /* Use long way over the stack */
3772 oper (flags, val, ops);
3777 /*****************************************************************************/
3778 /* Allocating static storage */
3779 /*****************************************************************************/
3783 void g_res (unsigned n)
3784 /* Reserve static storage, n bytes */
3786 AddDataLine ("\t.res\t%u,$00", n);
3791 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3792 /* Define data with the size given in flags */
3794 if (flags & CF_CONST) {
3796 /* Numeric constant */
3797 switch (flags & CF_TYPE) {
3800 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
3804 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
3808 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3819 /* Create the correct label name */
3820 const char* Label = GetLabelName (flags, val, offs);
3822 /* Labels are always 16 bit */
3823 AddDataLine ("\t.word\t%s", Label);
3830 void g_defbytes (const void* Bytes, unsigned Count)
3831 /* Output a row of bytes as a constant */
3837 /* Cast the buffer pointer */
3838 const unsigned char* Data = (const unsigned char*) Bytes;
3840 /* Output the stuff */
3843 /* How many go into this line? */
3844 if ((Chunk = Count) > 16) {
3849 /* Output one line */
3850 strcpy (Buf, "\t.byte\t");
3853 B += sprintf (B, "$%02X", *Data++);
3859 /* Output the line */
3866 void g_zerobytes (unsigned n)
3867 /* Output n bytes of data initialized with zero */
3869 AddDataLine ("\t.res\t%u,$00", n);
3874 /*****************************************************************************/
3875 /* User supplied assembler code */
3876 /*****************************************************************************/
3880 void g_asmcode (const char* Line, int Len)
3881 /* Output one line of assembler code. If Len is greater than zero, it is used
3882 * as the maximum number of characters to use from Line.
3886 AddCodeLine ("%.*s", Len, Line);
3888 AddCodeLine ("%s", Line);
3894 /*****************************************************************************/
3895 /* Inlined known functions */
3896 /*****************************************************************************/
3900 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3901 /* Inline the strlen() function */
3903 /* We need a label in both cases */
3904 unsigned label = GetLocalLabel ();
3906 /* Two different encodings */
3907 if (flags & CF_CONST) {
3909 /* The address of the string is constant. Create the correct label name */
3910 char* lbuf = GetLabelName (flags, val, offs);
3912 /* Generate the strlen code */
3913 AddCodeLine ("ldy #$FF");
3914 g_defcodelabel (label);
3915 AddCodeLine ("iny");
3916 AddCodeLine ("lda %s,y", lbuf);
3917 AddCodeLine ("bne %s", LocalLabelName (label));
3918 AddCodeLine ("tax");
3919 AddCodeLine ("tya");
3923 /* Address not constant but in primary */
3924 if (CodeSizeFactor < 400) {
3925 /* This is too much code, so call strlen instead of inlining */
3926 AddCodeLine ("jsr _strlen");
3928 /* Inline the function */
3929 AddCodeLine ("sta ptr1");
3930 AddCodeLine ("stx ptr1+1");
3931 AddCodeLine ("ldy #$FF");
3932 g_defcodelabel (label);
3933 AddCodeLine ("iny");
3934 AddCodeLine ("lda (ptr1),y");
3935 AddCodeLine ("bne %s", LocalLabelName (label));
3936 AddCodeLine ("tax");
3937 AddCodeLine ("tya");