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)) {
723 AddCodeLine ("lda (sp),y");
726 AddCodeLine ("ldx #$00");
727 AddCodeLine ("lda (sp),y");
728 if ((flags & CF_UNSIGNED) == 0) {
729 unsigned L = GetLocalLabel();
730 AddCodeLine ("bpl %s", LocalLabelName (L));
738 CheckLocalOffs (offs + 1);
739 if (flags & CF_TEST) {
741 AddCodeLine ("lda (sp),y");
743 AddCodeLine ("ora (sp),y");
745 if (CodeSizeFactor > 180) {
747 AddCodeLine ("lda (sp),y");
750 AddCodeLine ("lda (sp),y");
753 AddCodeLine ("jsr ldaxysp");
760 AddCodeLine ("jsr ldeaxysp");
770 void g_getind (unsigned flags, unsigned offs)
771 /* Fetch the specified object type indirect through the primary register
772 * into the primary register
775 /* If the offset is greater than 255, add the part that is > 255 to
776 * the primary. This way we get an easy addition and use the low byte
779 offs = MakeByteOffs (flags, offs);
781 /* Handle the indirect fetch */
782 switch (flags & CF_TYPE) {
785 /* Character sized */
786 if (flags & CF_UNSIGNED) {
788 AddCodeLine ("jsr ldauidx");
791 AddCodeLine ("jsr ldaidx");
796 if (flags & CF_TEST) {
798 AddCodeLine ("sta ptr1");
799 AddCodeLine ("stx ptr1+1");
800 AddCodeLine ("lda (ptr1),y");
802 AddCodeLine ("ora (ptr1),y");
805 AddCodeLine ("jsr ldaxidx");
811 AddCodeLine ("jsr ldeaxidx");
812 if (flags & CF_TEST) {
813 AddCodeLine ("jsr tsteax");
825 void g_leasp (int offs)
826 /* Fetch the address of the specified symbol into the primary register */
828 /* Calculate the offset relative to sp */
831 /* For value 0 we do direct code */
833 AddCodeLine ("lda sp");
834 AddCodeLine ("ldx sp+1");
836 if (CodeSizeFactor < 300) {
837 ldaconst (offs); /* Load A with offset value */
838 AddCodeLine ("jsr leaasp"); /* Load effective address */
840 unsigned L = GetLocalLabel ();
841 if (CPU == CPU_65C02 && offs == 1) {
842 AddCodeLine ("lda sp");
843 AddCodeLine ("ldx sp+1");
845 AddCodeLine ("bne %s", LocalLabelName (L));
850 AddCodeLine ("ldx sp+1");
851 AddCodeLine ("adc sp");
852 AddCodeLine ("bcc %s", LocalLabelName (L));
862 void g_leavariadic (int Offs)
863 /* Fetch the address of a parameter in a variadic function into the primary
867 unsigned ArgSizeOffs;
869 /* Calculate the offset relative to sp */
872 /* Get the offset of the parameter which is stored at sp+0 on function
873 * entry and check if this offset is reachable with a byte offset.
876 ArgSizeOffs = -oursp;
877 CheckLocalOffs (ArgSizeOffs);
879 /* Get the size of all parameters. */
880 ldyconst (ArgSizeOffs);
881 AddCodeLine ("lda (sp),y");
883 /* Add the value of the stackpointer */
884 if (CodeSizeFactor > 250) {
885 unsigned L = GetLocalLabel();
886 AddCodeLine ("ldx sp+1");
888 AddCodeLine ("adc sp");
889 AddCodeLine ("bcc %s", LocalLabelName (L));
893 AddCodeLine ("jsr leaasp");
896 /* Add the offset to the primary */
898 g_inc (CF_INT | CF_CONST, Offs);
899 } else if (Offs < 0) {
900 g_dec (CF_INT | CF_CONST, -Offs);
906 /*****************************************************************************/
907 /* Store into memory */
908 /*****************************************************************************/
912 void g_putstatic (unsigned flags, unsigned long label, unsigned offs)
913 /* Store the primary register into the specified static memory cell */
915 /* Create the correct label name */
916 char* lbuf = GetLabelName (flags, label, offs);
918 /* Check the size and generate the correct store operation */
919 switch (flags & CF_TYPE) {
922 AddCodeLine ("sta %s", lbuf);
926 AddCodeLine ("sta %s", lbuf);
927 AddCodeLine ("stx %s+1", lbuf);
931 AddCodeLine ("sta %s", lbuf);
932 AddCodeLine ("stx %s+1", lbuf);
933 AddCodeLine ("ldy sreg");
934 AddCodeLine ("sty %s+2", lbuf);
935 AddCodeLine ("ldy sreg+1");
936 AddCodeLine ("sty %s+3", lbuf);
947 void g_putlocal (unsigned Flags, int Offs, long Val)
948 /* Put data into local object. */
951 CheckLocalOffs (Offs);
952 switch (Flags & CF_TYPE) {
955 if (Flags & CF_CONST) {
956 AddCodeLine ("lda #$%02X", (unsigned char) Val);
959 AddCodeLine ("sta (sp),y");
963 if (Flags & CF_CONST) {
965 AddCodeLine ("lda #$%02X", (unsigned char) (Val >> 8));
966 AddCodeLine ("sta (sp),y");
967 if ((Flags & CF_NOKEEP) == 0) {
968 /* Place high byte into X */
971 if ((Val & 0xFF) == Offs+1) {
972 /* The value we need is already in Y */
977 AddCodeLine ("lda #$%02X", (unsigned char) Val);
979 AddCodeLine ("sta (sp),y");
981 if ((Flags & CF_NOKEEP) == 0 || CodeSizeFactor < 160) {
983 AddCodeLine ("jsr staxysp");
986 AddCodeLine ("sta (sp),y");
989 AddCodeLine ("sta (sp),y");
995 if (Flags & CF_CONST) {
996 g_getimmed (Flags, Val, 0);
999 AddCodeLine ("jsr steaxysp");
1010 void g_putind (unsigned Flags, unsigned Offs)
1011 /* Store the specified object type in the primary register at the address
1012 * on the top of the stack
1015 /* We can handle offsets below $100 directly, larger offsets must be added
1016 * to the address. Since a/x is in use, best code is achieved by adding
1017 * just the high byte. Be sure to check if the low byte will overflow while
1020 if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1022 /* Overflow - we need to add the low byte also */
1023 AddCodeLine ("ldy #$00");
1024 AddCodeLine ("clc");
1025 AddCodeLine ("pha");
1026 AddCodeLine ("lda #$%02X", Offs & 0xFF);
1027 AddCodeLine ("adc (sp),y");
1028 AddCodeLine ("sta (sp),y");
1029 AddCodeLine ("iny");
1030 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1031 AddCodeLine ("adc (sp),y");
1032 AddCodeLine ("sta (sp),y");
1033 AddCodeLine ("pla");
1035 /* Complete address is on stack, new offset is zero */
1038 } else if ((Offs & 0xFF00) != 0) {
1040 /* We can just add the high byte */
1041 AddCodeLine ("ldy #$01");
1042 AddCodeLine ("clc");
1043 AddCodeLine ("pha");
1044 AddCodeLine ("lda #$%02X", (Offs >> 8) & 0xFF);
1045 AddCodeLine ("adc (sp),y");
1046 AddCodeLine ("sta (sp),y");
1047 AddCodeLine ("pla");
1049 /* Offset is now just the low byte */
1053 /* Check the size and determine operation */
1054 switch (Flags & CF_TYPE) {
1058 AddCodeLine ("jsr staspidx");
1063 AddCodeLine ("jsr staxspidx");
1068 AddCodeLine ("jsr steaxspidx");
1076 /* Pop the argument which is always a pointer */
1082 /*****************************************************************************/
1083 /* type conversion and similiar stuff */
1084 /*****************************************************************************/
1088 void g_toslong (unsigned flags)
1089 /* Make sure, the value on TOS is a long. Convert if necessary */
1091 switch (flags & CF_TYPE) {
1095 if (flags & CF_UNSIGNED) {
1096 AddCodeLine ("jsr tosulong");
1098 AddCodeLine ("jsr toslong");
1113 void g_tosint (unsigned flags)
1114 /* Make sure, the value on TOS is an int. Convert if necessary */
1116 switch (flags & CF_TYPE) {
1123 AddCodeLine ("jsr tosint");
1134 void g_reglong (unsigned flags)
1135 /* Make sure, the value in the primary register a long. Convert if necessary */
1137 switch (flags & CF_TYPE) {
1141 if (flags & CF_UNSIGNED) {
1142 if (CodeSizeFactor >= 200) {
1144 AddCodeLine ("sty sreg");
1145 AddCodeLine ("sty sreg+1");
1147 AddCodeLine ("jsr axulong");
1150 AddCodeLine ("jsr axlong");
1164 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1165 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1166 * value, that corresponds to the value on TOS, rhs corresponds to the value
1167 * in (e)ax. The return value is the the flags value for the resulting type.
1170 unsigned ltype, rtype;
1173 /* Get the type spec from the flags */
1174 ltype = lhs & CF_TYPE;
1175 rtype = rhs & CF_TYPE;
1177 /* Check if a conversion is needed */
1178 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1179 /* We must promote the primary register to long */
1181 /* Get the new rhs type */
1182 rhs = (rhs & ~CF_TYPE) | CF_LONG;
1184 } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1185 /* We must promote the lhs to long */
1191 /* Get the new rhs type */
1192 lhs = (lhs & ~CF_TYPE) | CF_LONG;
1196 /* Determine the result type for the operation:
1197 * - The result is const if both operands are const.
1198 * - The result is unsigned if one of the operands is unsigned.
1199 * - The result is long if one of the operands is long.
1200 * - Otherwise the result is int sized.
1202 result = (lhs & CF_CONST) & (rhs & CF_CONST);
1203 result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1204 if (rtype == CF_LONG || ltype == CF_LONG) {
1214 unsigned g_typecast (unsigned lhs, unsigned rhs)
1215 /* Cast the value in the primary register to the operand size that is flagged
1216 * by the lhs value. Return the result value.
1219 unsigned ltype, rtype;
1221 /* Get the type spec from the flags */
1222 ltype = lhs & CF_TYPE;
1223 rtype = rhs & CF_TYPE;
1225 /* Check if a conversion is needed */
1226 if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1227 /* We must promote the primary register to long */
1231 /* Do not need any other action. If the left type is int, and the primary
1232 * register is long, it will be automagically truncated. If the right hand
1233 * side is const, it is not located in the primary register and handled by
1234 * the expression parser code.
1237 /* Result is const if the right hand side was const */
1238 lhs |= (rhs & CF_CONST);
1240 /* The resulting type is that of the left hand side (that's why you called
1248 void g_scale (unsigned flags, long val)
1249 /* Scale the value in the primary register by the given value. If val is positive,
1250 * scale up, is val is negative, scale down. This function is used to scale
1251 * the operands or results of pointer arithmetic by the size of the type, the
1252 * pointer points to.
1257 /* Value may not be zero */
1259 Internal ("Data type has no size");
1260 } else if (val > 0) {
1263 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1265 /* Factor is 2, 4, 8 and 16, use special function */
1266 switch (flags & CF_TYPE) {
1269 if (flags & CF_FORCECHAR) {
1271 AddCodeLine ("asl a");
1278 if (CodeSizeFactor >= (p2+1)*130U) {
1279 AddCodeLine ("stx tmp1");
1281 AddCodeLine ("asl a");
1282 AddCodeLine ("rol tmp1");
1284 AddCodeLine ("ldx tmp1");
1286 if (flags & CF_UNSIGNED) {
1287 AddCodeLine ("jsr shlax%d", p2);
1289 AddCodeLine ("jsr aslax%d", p2);
1295 if (flags & CF_UNSIGNED) {
1296 AddCodeLine ("jsr shleax%d", p2);
1298 AddCodeLine ("jsr asleax%d", p2);
1307 } else if (val != 1) {
1309 /* Use a multiplication instead */
1310 g_mul (flags | CF_CONST, val);
1318 if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1320 /* Factor is 2, 4, 8 and 16 use special function */
1321 switch (flags & CF_TYPE) {
1324 if (flags & CF_FORCECHAR) {
1325 if (flags & CF_UNSIGNED) {
1327 AddCodeLine ("lsr a");
1330 } else if (p2 <= 2) {
1331 AddCodeLine ("cmp #$80");
1332 AddCodeLine ("ror a");
1339 if (flags & CF_UNSIGNED) {
1340 if (CodeSizeFactor >= (p2+1)*130U) {
1341 AddCodeLine ("stx tmp1");
1343 AddCodeLine ("lsr tmp1");
1344 AddCodeLine ("ror a");
1346 AddCodeLine ("ldx tmp1");
1348 AddCodeLine ("jsr lsrax%d", p2);
1351 if (CodeSizeFactor >= (p2+1)*150U) {
1352 AddCodeLine ("stx tmp1");
1354 AddCodeLine ("cpx #$80");
1355 AddCodeLine ("ror tmp1");
1356 AddCodeLine ("ror a");
1358 AddCodeLine ("ldx tmp1");
1360 AddCodeLine ("jsr asrax%d", p2);
1366 if (flags & CF_UNSIGNED) {
1367 AddCodeLine ("jsr lsreax%d", p2);
1369 AddCodeLine ("jsr asreax%d", p2);
1378 } else if (val != 1) {
1380 /* Use a division instead */
1381 g_div (flags | CF_CONST, val);
1389 /*****************************************************************************/
1390 /* Adds and subs of variables fix a fixed address */
1391 /*****************************************************************************/
1395 void g_addlocal (unsigned flags, int offs)
1396 /* Add a local variable to ax */
1400 /* Correct the offset and check it */
1402 CheckLocalOffs (offs);
1404 switch (flags & CF_TYPE) {
1407 L = GetLocalLabel();
1408 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1409 AddCodeLine ("clc");
1410 AddCodeLine ("adc (sp),y");
1411 AddCodeLine ("bcc %s", LocalLabelName (L));
1412 AddCodeLine ("inx");
1417 AddCodeLine ("ldy #$%02X", offs & 0xFF);
1418 AddCodeLine ("clc");
1419 AddCodeLine ("adc (sp),y");
1420 AddCodeLine ("pha");
1421 AddCodeLine ("txa");
1422 AddCodeLine ("iny");
1423 AddCodeLine ("adc (sp),y");
1424 AddCodeLine ("tax");
1425 AddCodeLine ("pla");
1429 /* Do it the old way */
1431 g_getlocal (flags, offs);
1443 void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
1444 /* Add a static variable to ax */
1448 /* Create the correct label name */
1449 char* lbuf = GetLabelName (flags, label, offs);
1451 switch (flags & CF_TYPE) {
1454 L = GetLocalLabel();
1455 AddCodeLine ("clc");
1456 AddCodeLine ("adc %s", lbuf);
1457 AddCodeLine ("bcc %s", LocalLabelName (L));
1458 AddCodeLine ("inx");
1463 AddCodeLine ("clc");
1464 AddCodeLine ("adc %s", lbuf);
1465 AddCodeLine ("tay");
1466 AddCodeLine ("txa");
1467 AddCodeLine ("adc %s+1", lbuf);
1468 AddCodeLine ("tax");
1469 AddCodeLine ("tya");
1473 /* Do it the old way */
1475 g_getstatic (flags, label, offs);
1487 /*****************************************************************************/
1488 /* Special op= functions */
1489 /*****************************************************************************/
1493 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
1495 /* Emit += for a static variable */
1497 /* Create the correct label name */
1498 char* lbuf = GetLabelName (flags, label, offs);
1500 /* Check the size and determine operation */
1501 switch (flags & CF_TYPE) {
1504 if (flags & CF_FORCECHAR) {
1505 AddCodeLine ("ldx #$00");
1506 if (flags & CF_CONST) {
1508 AddCodeLine ("inc %s", lbuf);
1509 AddCodeLine ("lda %s", lbuf);
1511 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1512 AddCodeLine ("clc");
1513 AddCodeLine ("adc %s", lbuf);
1514 AddCodeLine ("sta %s", lbuf);
1517 AddCodeLine ("clc");
1518 AddCodeLine ("adc %s", lbuf);
1519 AddCodeLine ("sta %s", lbuf);
1521 if ((flags & CF_UNSIGNED) == 0) {
1522 unsigned L = GetLocalLabel();
1523 AddCodeLine ("bpl %s", LocalLabelName (L));
1524 AddCodeLine ("dex");
1532 if (flags & CF_CONST) {
1534 unsigned L = GetLocalLabel ();
1535 AddCodeLine ("inc %s", lbuf);
1536 AddCodeLine ("bne %s", LocalLabelName (L));
1537 AddCodeLine ("inc %s+1", lbuf);
1539 AddCodeLine ("lda %s", lbuf); /* Hmmm... */
1540 AddCodeLine ("ldx %s+1", lbuf);
1542 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1543 AddCodeLine ("clc");
1544 AddCodeLine ("adc %s", lbuf);
1545 AddCodeLine ("sta %s", lbuf);
1547 unsigned L = GetLocalLabel ();
1548 AddCodeLine ("bcc %s", LocalLabelName (L));
1549 AddCodeLine ("inc %s+1", lbuf);
1551 AddCodeLine ("ldx %s+1", lbuf);
1553 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1554 AddCodeLine ("adc %s+1", lbuf);
1555 AddCodeLine ("sta %s+1", lbuf);
1556 AddCodeLine ("tax");
1557 AddCodeLine ("lda %s", lbuf);
1561 AddCodeLine ("clc");
1562 AddCodeLine ("adc %s", lbuf);
1563 AddCodeLine ("sta %s", lbuf);
1564 AddCodeLine ("txa");
1565 AddCodeLine ("adc %s+1", lbuf);
1566 AddCodeLine ("sta %s+1", lbuf);
1567 AddCodeLine ("tax");
1568 AddCodeLine ("lda %s", lbuf);
1573 if (flags & CF_CONST) {
1575 AddCodeLine ("ldy #<(%s)", lbuf);
1576 AddCodeLine ("sty ptr1");
1577 AddCodeLine ("ldy #>(%s+1)", lbuf);
1579 AddCodeLine ("jsr laddeq1");
1581 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1582 AddCodeLine ("jsr laddeqa");
1585 g_getstatic (flags, label, offs);
1587 g_putstatic (flags, label, offs);
1590 AddCodeLine ("ldy #<(%s)", lbuf);
1591 AddCodeLine ("sty ptr1");
1592 AddCodeLine ("ldy #>(%s+1)", lbuf);
1593 AddCodeLine ("jsr laddeq");
1604 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1605 /* Emit += for a local variable */
1607 /* Calculate the true offset, check it, load it into Y */
1609 CheckLocalOffs (offs);
1611 /* Check the size and determine operation */
1612 switch (flags & CF_TYPE) {
1615 if (flags & CF_FORCECHAR) {
1617 AddCodeLine ("ldx #$00");
1618 if (flags & CF_CONST) {
1619 AddCodeLine ("clc");
1620 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1621 AddCodeLine ("adc (sp),y");
1622 AddCodeLine ("sta (sp),y");
1624 AddCodeLine ("clc");
1625 AddCodeLine ("adc (sp),y");
1626 AddCodeLine ("sta (sp),y");
1628 if ((flags & CF_UNSIGNED) == 0) {
1629 unsigned L = GetLocalLabel();
1630 AddCodeLine ("bpl %s", LocalLabelName (L));
1631 AddCodeLine ("dex");
1640 if (flags & CF_CONST) {
1641 if (CodeSizeFactor >= 400) {
1642 AddCodeLine ("clc");
1643 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1644 AddCodeLine ("adc (sp),y");
1645 AddCodeLine ("sta (sp),y");
1646 AddCodeLine ("iny");
1647 AddCodeLine ("lda #$%02X", (int) ((val >> 8) & 0xFF));
1648 AddCodeLine ("adc (sp),y");
1649 AddCodeLine ("sta (sp),y");
1650 AddCodeLine ("tax");
1651 AddCodeLine ("dey");
1652 AddCodeLine ("lda (sp),y");
1654 g_getimmed (flags, val, 0);
1655 AddCodeLine ("jsr addeqysp");
1658 AddCodeLine ("jsr addeqysp");
1663 if (flags & CF_CONST) {
1664 g_getimmed (flags, val, 0);
1667 AddCodeLine ("jsr laddeqysp");
1677 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1678 /* Emit += for the location with address in ax */
1680 /* If the offset is too large for a byte register, add the high byte
1681 * of the offset to the primary. Beware: We need a special correction
1682 * if the offset in the low byte will overflow in the operation.
1684 offs = MakeByteOffs (flags, offs);
1686 /* Check the size and determine operation */
1687 switch (flags & CF_TYPE) {
1690 AddCodeLine ("sta ptr1");
1691 AddCodeLine ("stx ptr1+1");
1692 AddCodeLine ("ldy #$%02X", offs);
1693 AddCodeLine ("ldx #$00");
1694 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1695 AddCodeLine ("clc");
1696 AddCodeLine ("adc (ptr1),y");
1697 AddCodeLine ("sta (ptr1),y");
1701 if (CodeSizeFactor >= 200) {
1702 /* Lots of code, use only if size is not important */
1703 AddCodeLine ("sta ptr1");
1704 AddCodeLine ("stx ptr1+1");
1705 AddCodeLine ("ldy #$%02X", offs);
1706 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1707 AddCodeLine ("clc");
1708 AddCodeLine ("adc (ptr1),y");
1709 AddCodeLine ("sta (ptr1),y");
1710 AddCodeLine ("pha");
1711 AddCodeLine ("iny");
1712 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1713 AddCodeLine ("adc (ptr1),y");
1714 AddCodeLine ("sta (ptr1),y");
1715 AddCodeLine ("tax");
1716 AddCodeLine ("pla");
1722 AddCodeLine ("jsr pushax"); /* Push the address */
1723 push (CF_PTR); /* Correct the internal sp */
1724 g_getind (flags, offs); /* Fetch the value */
1725 g_inc (flags, val); /* Increment value in primary */
1726 g_putind (flags, offs); /* Store the value back */
1736 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1738 /* Emit -= for a static variable */
1740 /* Create the correct label name */
1741 char* lbuf = GetLabelName (flags, label, offs);
1743 /* Check the size and determine operation */
1744 switch (flags & CF_TYPE) {
1747 if (flags & CF_FORCECHAR) {
1748 AddCodeLine ("ldx #$00");
1749 if (flags & CF_CONST) {
1751 AddCodeLine ("dec %s", lbuf);
1752 AddCodeLine ("lda %s", lbuf);
1754 AddCodeLine ("sec");
1755 AddCodeLine ("lda %s", lbuf);
1756 AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1757 AddCodeLine ("sta %s", lbuf);
1760 AddCodeLine ("sec");
1761 AddCodeLine ("sta tmp1");
1762 AddCodeLine ("lda %s", lbuf);
1763 AddCodeLine ("sbc tmp1");
1764 AddCodeLine ("sta %s", lbuf);
1766 if ((flags & CF_UNSIGNED) == 0) {
1767 unsigned L = GetLocalLabel();
1768 AddCodeLine ("bpl %s", LocalLabelName (L));
1769 AddCodeLine ("dex");
1777 AddCodeLine ("sec");
1778 if (flags & CF_CONST) {
1779 AddCodeLine ("lda %s", lbuf);
1780 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1781 AddCodeLine ("sta %s", lbuf);
1783 unsigned L = GetLocalLabel ();
1784 AddCodeLine ("bcs %s", LocalLabelName (L));
1785 AddCodeLine ("dec %s+1", lbuf);
1787 AddCodeLine ("ldx %s+1", lbuf);
1789 AddCodeLine ("lda %s+1", lbuf);
1790 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1791 AddCodeLine ("sta %s+1", lbuf);
1792 AddCodeLine ("tax");
1793 AddCodeLine ("lda %s", lbuf);
1796 AddCodeLine ("sta tmp1");
1797 AddCodeLine ("lda %s", lbuf);
1798 AddCodeLine ("sbc tmp1");
1799 AddCodeLine ("sta %s", lbuf);
1800 AddCodeLine ("stx tmp1");
1801 AddCodeLine ("lda %s+1", lbuf);
1802 AddCodeLine ("sbc tmp1");
1803 AddCodeLine ("sta %s+1", lbuf);
1804 AddCodeLine ("tax");
1805 AddCodeLine ("lda %s", lbuf);
1810 if (flags & CF_CONST) {
1812 AddCodeLine ("ldy #<(%s)", lbuf);
1813 AddCodeLine ("sty ptr1");
1814 AddCodeLine ("ldy #>(%s+1)", lbuf);
1815 AddCodeLine ("lda #$%02X", (unsigned char)val);
1816 AddCodeLine ("jsr lsubeqa");
1818 g_getstatic (flags, label, offs);
1820 g_putstatic (flags, label, offs);
1823 AddCodeLine ("ldy #<(%s)", lbuf);
1824 AddCodeLine ("sty ptr1");
1825 AddCodeLine ("ldy #>(%s+1)", lbuf);
1826 AddCodeLine ("jsr lsubeq");
1837 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1838 /* Emit -= for a local variable */
1840 /* Calculate the true offset, check it, load it into Y */
1842 CheckLocalOffs (offs);
1844 /* Check the size and determine operation */
1845 switch (flags & CF_TYPE) {
1848 if (flags & CF_FORCECHAR) {
1850 AddCodeLine ("ldx #$00");
1851 AddCodeLine ("sec");
1852 if (flags & CF_CONST) {
1853 AddCodeLine ("lda (sp),y");
1854 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1856 AddCodeLine ("sta tmp1");
1857 AddCodeLine ("lda (sp),y");
1858 AddCodeLine ("sbc tmp1");
1860 AddCodeLine ("sta (sp),y");
1861 if ((flags & CF_UNSIGNED) == 0) {
1862 unsigned L = GetLocalLabel();
1863 AddCodeLine ("bpl %s", LocalLabelName (L));
1864 AddCodeLine ("dex");
1872 if (flags & CF_CONST) {
1873 g_getimmed (flags, val, 0);
1876 AddCodeLine ("jsr subeqysp");
1880 if (flags & CF_CONST) {
1881 g_getimmed (flags, val, 0);
1884 AddCodeLine ("jsr lsubeqysp");
1894 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
1895 /* Emit -= for the location with address in ax */
1897 /* If the offset is too large for a byte register, add the high byte
1898 * of the offset to the primary. Beware: We need a special correction
1899 * if the offset in the low byte will overflow in the operation.
1901 offs = MakeByteOffs (flags, offs);
1903 /* Check the size and determine operation */
1904 switch (flags & CF_TYPE) {
1907 AddCodeLine ("sta ptr1");
1908 AddCodeLine ("stx ptr1+1");
1909 AddCodeLine ("ldy #$%02X", offs);
1910 AddCodeLine ("ldx #$00");
1911 AddCodeLine ("lda (ptr1),y");
1912 AddCodeLine ("sec");
1913 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1914 AddCodeLine ("sta (ptr1),y");
1918 if (CodeSizeFactor >= 200) {
1919 /* Lots of code, use only if size is not important */
1920 AddCodeLine ("sta ptr1");
1921 AddCodeLine ("stx ptr1+1");
1922 AddCodeLine ("ldy #$%02X", offs);
1923 AddCodeLine ("lda (ptr1),y");
1924 AddCodeLine ("sec");
1925 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1926 AddCodeLine ("sta (ptr1),y");
1927 AddCodeLine ("pha");
1928 AddCodeLine ("iny");
1929 AddCodeLine ("lda (ptr1),y");
1930 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1931 AddCodeLine ("sta (ptr1),y");
1932 AddCodeLine ("tax");
1933 AddCodeLine ("pla");
1939 AddCodeLine ("jsr pushax"); /* Push the address */
1940 push (CF_PTR); /* Correct the internal sp */
1941 g_getind (flags, offs); /* Fetch the value */
1942 g_dec (flags, val); /* Increment value in primary */
1943 g_putind (flags, offs); /* Store the value back */
1953 /*****************************************************************************/
1954 /* Add a variable address to the value in ax */
1955 /*****************************************************************************/
1959 void g_addaddr_local (unsigned flags attribute ((unused)), int offs)
1960 /* Add the address of a local variable to ax */
1964 /* Add the offset */
1967 /* We cannot address more then 256 bytes of locals anyway */
1968 L = GetLocalLabel();
1969 CheckLocalOffs (offs);
1970 AddCodeLine ("clc");
1971 AddCodeLine ("adc #$%02X", offs & 0xFF);
1972 /* Do also skip the CLC insn below */
1973 AddCodeLine ("bcc %s", LocalLabelName (L));
1974 AddCodeLine ("inx");
1977 /* Add the current stackpointer value */
1978 AddCodeLine ("clc");
1980 /* Label was used above */
1983 AddCodeLine ("adc sp");
1984 AddCodeLine ("tay");
1985 AddCodeLine ("txa");
1986 AddCodeLine ("adc sp+1");
1987 AddCodeLine ("tax");
1988 AddCodeLine ("tya");
1993 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
1994 /* Add the address of a static variable to ax */
1996 /* Create the correct label name */
1997 char* lbuf = GetLabelName (flags, label, offs);
1999 /* Add the address to the current ax value */
2000 AddCodeLine ("clc");
2001 AddCodeLine ("adc #<(%s)", lbuf);
2002 AddCodeLine ("tay");
2003 AddCodeLine ("txa");
2004 AddCodeLine ("adc #>(%s)", lbuf);
2005 AddCodeLine ("tax");
2006 AddCodeLine ("tya");
2011 /*****************************************************************************/
2013 /*****************************************************************************/
2017 void g_save (unsigned flags)
2018 /* Copy primary register to hold register. */
2020 /* Check the size and determine operation */
2021 switch (flags & CF_TYPE) {
2024 if (flags & CF_FORCECHAR) {
2025 AddCodeLine ("pha");
2031 AddCodeLine ("sta regsave");
2032 AddCodeLine ("stx regsave+1");
2036 AddCodeLine ("jsr saveeax");
2046 void g_restore (unsigned flags)
2047 /* Copy hold register to primary. */
2049 /* Check the size and determine operation */
2050 switch (flags & CF_TYPE) {
2053 if (flags & CF_FORCECHAR) {
2054 AddCodeLine ("pla");
2060 AddCodeLine ("lda regsave");
2061 AddCodeLine ("ldx regsave+1");
2065 AddCodeLine ("jsr resteax");
2075 void g_cmp (unsigned flags, unsigned long val)
2076 /* Immidiate compare. The primary register will not be changed, Z flag
2082 /* Check the size and determine operation */
2083 switch (flags & CF_TYPE) {
2086 if (flags & CF_FORCECHAR) {
2087 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2093 L = GetLocalLabel();
2094 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2095 AddCodeLine ("bne %s", LocalLabelName (L));
2096 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2101 Internal ("g_cmp: Long compares not implemented");
2111 static void oper (unsigned flags, unsigned long val, char** subs)
2112 /* Encode a binary operation. subs is a pointer to four groups of three
2114 * 0-2 --> Operate on ints
2115 * 3-5 --> Operate on unsigneds
2116 * 6-8 --> Operate on longs
2117 * 9-11 --> Operate on unsigned longs
2119 * The first subroutine names in each string group is used to encode an
2120 * operation with a zero constant, the second to encode an operation with
2121 * a 8 bit constant, and the third is used in all other cases.
2126 /* Determine the offset into the array */
2127 offs = (flags & CF_UNSIGNED)? 3 : 0;
2128 switch (flags & CF_TYPE) {
2141 /* Encode the operation */
2142 if (flags & CF_CONST) {
2143 /* Constant value given */
2144 if (val == 0 && subs [offs+0]) {
2145 /* Special case: constant with value zero */
2146 AddCodeLine ("jsr %s", subs [offs+0]);
2147 } else if (val < 0x100 && subs [offs+1]) {
2148 /* Special case: constant with high byte zero */
2149 ldaconst (val); /* Load low byte */
2150 AddCodeLine ("jsr %s", subs [offs+1]);
2152 /* Others: arbitrary constant value */
2153 g_getimmed (flags, val, 0); /* Load value */
2154 AddCodeLine ("jsr %s", subs [offs+2]);
2157 /* Value not constant (is already in (e)ax) */
2158 AddCodeLine ("jsr %s", subs [offs+2]);
2161 /* The operation will pop it's argument */
2167 void g_test (unsigned flags)
2168 /* Test the value in the primary and set the condition codes */
2170 switch (flags & CF_TYPE) {
2173 if (flags & CF_FORCECHAR) {
2174 AddCodeLine ("tax");
2180 AddCodeLine ("stx tmp1");
2181 AddCodeLine ("ora tmp1");
2185 if (flags & CF_UNSIGNED) {
2186 AddCodeLine ("jsr utsteax");
2188 AddCodeLine ("jsr tsteax");
2200 void g_push (unsigned flags, unsigned long val)
2201 /* Push the primary register or a constant value onto the stack */
2203 if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2205 /* We have a constant 8 or 16 bit value */
2206 if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2208 /* Handle as 8 bit value */
2210 AddCodeLine ("jsr pusha");
2214 /* Handle as 16 bit value */
2215 g_getimmed (flags, val, 0);
2216 AddCodeLine ("jsr pushax");
2221 /* Value is not 16 bit or not constant */
2222 if (flags & CF_CONST) {
2223 /* Constant 32 bit value, load into eax */
2224 g_getimmed (flags, val, 0);
2227 /* Push the primary register */
2228 switch (flags & CF_TYPE) {
2231 if (flags & CF_FORCECHAR) {
2232 /* Handle as char */
2233 AddCodeLine ("jsr pusha");
2238 AddCodeLine ("jsr pushax");
2242 AddCodeLine ("jsr pusheax");
2252 /* Adjust the stack offset */
2258 void g_swap (unsigned flags)
2259 /* Swap the primary register and the top of the stack. flags give the type
2260 * of *both* values (must have same size).
2263 switch (flags & CF_TYPE) {
2267 AddCodeLine ("jsr swapstk");
2271 AddCodeLine ("jsr swapestk");
2282 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2283 /* Call the specified subroutine name */
2285 if ((Flags & CF_FIXARGC) == 0) {
2286 /* Pass the argument count */
2289 AddCodeLine ("jsr _%s", Label);
2290 oursp += ArgSize; /* callee pops args */
2295 void g_callind (unsigned Flags, unsigned ArgSize, int Offs)
2296 /* Call subroutine indirect */
2298 if ((Flags & CF_LOCAL) == 0) {
2299 /* Address is in a/x */
2300 if ((Flags & CF_FIXARGC) == 0) {
2301 /* Pass arg count */
2304 AddCodeLine ("jsr callax");
2306 /* The address is on stack, offset is on Val */
2308 CheckLocalOffs (Offs);
2309 AddCodeLine ("pha");
2310 AddCodeLine ("ldy #$%02X", Offs);
2311 AddCodeLine ("lda (sp),y");
2312 AddCodeLine ("sta jmpvec+1");
2313 AddCodeLine ("iny");
2314 AddCodeLine ("lda (sp),y");
2315 AddCodeLine ("sta jmpvec+2");
2316 AddCodeLine ("pla");
2317 AddCodeLine ("jsr jmpvec");
2320 /* Callee pops args */
2326 void g_jump (unsigned Label)
2327 /* Jump to specified internal label number */
2329 AddCodeLine ("jmp %s", LocalLabelName (Label));
2334 void g_truejump (unsigned flags attribute ((unused)), unsigned label)
2335 /* Jump to label if zero flag clear */
2337 AddCodeLine ("jne %s", LocalLabelName (label));
2342 void g_falsejump (unsigned flags attribute ((unused)), unsigned label)
2343 /* Jump to label if zero flag set */
2345 AddCodeLine ("jeq %s", LocalLabelName (label));
2350 static void mod_internal (int k, char* verb1, char* verb2)
2353 AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2357 AddCodeLine ("jsr %ssp", verb2);
2363 void g_space (int space)
2364 /* Create or drop space on the stack */
2367 mod_internal (-space, "inc", "addy");
2368 } else if (space > 0) {
2369 mod_internal (space, "dec", "suby");
2375 void g_cstackcheck (void)
2376 /* Check for a C stack overflow */
2378 AddCodeLine ("jsr cstkchk");
2383 void g_stackcheck (void)
2384 /* Check for a stack overflow */
2386 AddCodeLine ("jsr stkchk");
2391 void g_add (unsigned flags, unsigned long val)
2392 /* Primary = TOS + Primary */
2394 static char* ops [12] = {
2395 0, "tosadda0", "tosaddax",
2396 0, "tosadda0", "tosaddax",
2401 if (flags & CF_CONST) {
2402 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2403 g_push (flags & ~CF_CONST, 0);
2405 oper (flags, val, ops);
2410 void g_sub (unsigned flags, unsigned long val)
2411 /* Primary = TOS - Primary */
2413 static char* ops [12] = {
2414 0, "tossuba0", "tossubax",
2415 0, "tossuba0", "tossubax",
2420 if (flags & CF_CONST) {
2421 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2422 g_push (flags & ~CF_CONST, 0);
2424 oper (flags, val, ops);
2429 void g_rsub (unsigned flags, unsigned long val)
2430 /* Primary = Primary - TOS */
2432 static char* ops [12] = {
2433 0, "tosrsuba0", "tosrsubax",
2434 0, "tosrsuba0", "tosrsubax",
2438 oper (flags, val, ops);
2443 void g_mul (unsigned flags, unsigned long val)
2444 /* Primary = TOS * Primary */
2446 static char* ops [12] = {
2447 0, "tosmula0", "tosmulax",
2448 0, "tosumula0", "tosumulax",
2455 /* Do strength reduction if the value is constant and a power of two */
2456 if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2457 /* Generate a shift instead */
2462 /* If the right hand side is const, the lhs is not on stack but still
2463 * in the primary register.
2465 if (flags & CF_CONST) {
2467 switch (flags & CF_TYPE) {
2470 if (flags & CF_FORCECHAR) {
2471 /* Handle some special cases */
2475 AddCodeLine ("sta tmp1");
2476 AddCodeLine ("asl a");
2477 AddCodeLine ("clc");
2478 AddCodeLine ("adc tmp1");
2482 AddCodeLine ("sta tmp1");
2483 AddCodeLine ("asl a");
2484 AddCodeLine ("asl a");
2485 AddCodeLine ("clc");
2486 AddCodeLine ("adc tmp1");
2490 AddCodeLine ("sta tmp1");
2491 AddCodeLine ("asl a");
2492 AddCodeLine ("asl a");
2493 AddCodeLine ("clc");
2494 AddCodeLine ("adc tmp1");
2495 AddCodeLine ("asl a");
2504 AddCodeLine ("jsr mulax3");
2507 AddCodeLine ("jsr mulax5");
2510 AddCodeLine ("jsr mulax10");
2522 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2523 * into the normal, non-optimized stuff.
2525 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2526 g_push (flags & ~CF_CONST, 0);
2530 /* Use long way over the stack */
2531 oper (flags, val, ops);
2536 void g_div (unsigned flags, unsigned long val)
2537 /* Primary = TOS / Primary */
2539 static char* ops [12] = {
2540 0, "tosdiva0", "tosdivax",
2541 0, "tosudiva0", "tosudivax",
2546 /* Do strength reduction if the value is constant and a power of two */
2548 if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2549 /* Generate a shift instead */
2552 /* Generate a division */
2553 if (flags & CF_CONST) {
2554 /* lhs is not on stack */
2555 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2556 g_push (flags & ~CF_CONST, 0);
2558 oper (flags, val, ops);
2564 void g_mod (unsigned flags, unsigned long val)
2565 /* Primary = TOS % Primary */
2567 static char* ops [12] = {
2568 0, "tosmoda0", "tosmodax",
2569 0, "tosumoda0", "tosumodax",
2575 /* Check if we can do some cost reduction */
2576 if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2577 /* We can do that with an AND operation */
2578 g_and (flags, val - 1);
2580 /* Do it the hard way... */
2581 if (flags & CF_CONST) {
2582 /* lhs is not on stack */
2583 flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2584 g_push (flags & ~CF_CONST, 0);
2586 oper (flags, val, ops);
2592 void g_or (unsigned flags, unsigned long val)
2593 /* Primary = TOS | Primary */
2595 static char* ops [12] = {
2596 0, "tosora0", "tosorax",
2597 0, "tosora0", "tosorax",
2602 /* If the right hand side is const, the lhs is not on stack but still
2603 * in the primary register.
2605 if (flags & CF_CONST) {
2607 switch (flags & CF_TYPE) {
2610 if (flags & CF_FORCECHAR) {
2611 if ((val & 0xFF) != 0xFF) {
2612 AddCodeLine ("ora #$%02X", (unsigned char)val);
2620 AddCodeLine ("ora #$%02X", (unsigned char)val);
2627 AddCodeLine ("ora #$%02X", (unsigned char)val);
2636 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2637 * into the normal, non-optimized stuff.
2639 g_push (flags & ~CF_CONST, 0);
2643 /* Use long way over the stack */
2644 oper (flags, val, ops);
2649 void g_xor (unsigned flags, unsigned long val)
2650 /* Primary = TOS ^ Primary */
2652 static char* ops [12] = {
2653 0, "tosxora0", "tosxorax",
2654 0, "tosxora0", "tosxorax",
2660 /* If the right hand side is const, the lhs is not on stack but still
2661 * in the primary register.
2663 if (flags & CF_CONST) {
2665 switch (flags & CF_TYPE) {
2668 if (flags & CF_FORCECHAR) {
2669 if ((val & 0xFF) != 0) {
2670 AddCodeLine ("eor #$%02X", (unsigned char)val);
2679 AddCodeLine ("eor #$%02X", (unsigned char)val);
2682 } else if ((val & 0xFF) == 0) {
2683 AddCodeLine ("pha");
2684 AddCodeLine ("txa");
2685 AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2686 AddCodeLine ("tax");
2687 AddCodeLine ("pla");
2695 AddCodeLine ("eor #$%02X", (unsigned char)val);
2705 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2706 * into the normal, non-optimized stuff.
2708 g_push (flags & ~CF_CONST, 0);
2712 /* Use long way over the stack */
2713 oper (flags, val, ops);
2718 void g_and (unsigned flags, unsigned long val)
2719 /* Primary = TOS & Primary */
2721 static char* ops [12] = {
2722 0, "tosanda0", "tosandax",
2723 0, "tosanda0", "tosandax",
2728 /* If the right hand side is const, the lhs is not on stack but still
2729 * in the primary register.
2731 if (flags & CF_CONST) {
2733 switch (flags & CF_TYPE) {
2736 if (flags & CF_FORCECHAR) {
2737 AddCodeLine ("and #$%02X", (unsigned char)val);
2742 if ((val & 0xFFFF) != 0xFFFF) {
2747 } else if (val != 0xFF) {
2748 AddCodeLine ("and #$%02X", (unsigned char)val);
2750 } else if ((val & 0xFF00) == 0xFF00) {
2751 AddCodeLine ("and #$%02X", (unsigned char)val);
2752 } else if ((val & 0x00FF) == 0x0000) {
2753 AddCodeLine ("txa");
2754 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2755 AddCodeLine ("tax");
2758 AddCodeLine ("tay");
2759 AddCodeLine ("txa");
2760 AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2761 AddCodeLine ("tax");
2762 AddCodeLine ("tya");
2763 if ((val & 0x00FF) != 0x00FF) {
2764 AddCodeLine ("and #$%02X", (unsigned char)val);
2773 AddCodeLine ("stx sreg+1");
2774 AddCodeLine ("stx sreg");
2775 if ((val & 0xFF) != 0xFF) {
2776 AddCodeLine ("and #$%02X", (unsigned char)val);
2779 } else if (val == 0xFF00) {
2781 AddCodeLine ("sta sreg+1");
2782 AddCodeLine ("sta sreg");
2791 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2792 * into the normal, non-optimized stuff.
2794 g_push (flags & ~CF_CONST, 0);
2798 /* Use long way over the stack */
2799 oper (flags, val, ops);
2804 void g_asr (unsigned flags, unsigned long val)
2805 /* Primary = TOS >> Primary */
2807 static char* ops [12] = {
2808 0, "tosasra0", "tosasrax",
2809 0, "tosshra0", "tosshrax",
2814 /* If the right hand side is const, the lhs is not on stack but still
2815 * in the primary register.
2817 if (flags & CF_CONST) {
2819 switch (flags & CF_TYPE) {
2823 if (val >= 8 && (flags & CF_UNSIGNED)) {
2824 AddCodeLine ("txa");
2831 } else if (val >= 1 && val <= 4) {
2832 if (flags & CF_UNSIGNED) {
2833 AddCodeLine ("jsr shrax%ld", val);
2835 AddCodeLine ("jsr asrax%ld", val);
2845 } else if (val >= 1 && val <= 4) {
2846 if (flags & CF_UNSIGNED) {
2847 AddCodeLine ("jsr shreax%ld", val);
2849 AddCodeLine ("jsr asreax%ld", val);
2852 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2853 AddCodeLine ("txa");
2854 AddCodeLine ("ldx sreg");
2855 AddCodeLine ("ldy sreg+1");
2856 AddCodeLine ("sty sreg");
2857 AddCodeLine ("ldy #$00");
2858 AddCodeLine ("sty sreg+1");
2860 } else if (val == 16) {
2861 AddCodeLine ("ldy #$00");
2862 AddCodeLine ("ldx sreg+1");
2863 if ((flags & CF_UNSIGNED) == 0) {
2864 unsigned L = GetLocalLabel();
2865 AddCodeLine ("bpl %s", LocalLabelName (L));
2866 AddCodeLine ("dey");
2869 AddCodeLine ("lda sreg");
2870 AddCodeLine ("sty sreg+1");
2871 AddCodeLine ("sty sreg");
2880 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2881 * into the normal, non-optimized stuff.
2883 g_push (flags & ~CF_CONST, 0);
2887 /* Use long way over the stack */
2888 oper (flags, val, ops);
2893 void g_asl (unsigned flags, unsigned long val)
2894 /* Primary = TOS << Primary */
2896 static char* ops [12] = {
2897 0, "tosasla0", "tosaslax",
2898 0, "tosshla0", "tosshlax",
2904 /* If the right hand side is const, the lhs is not on stack but still
2905 * in the primary register.
2907 if (flags & CF_CONST) {
2909 switch (flags & CF_TYPE) {
2914 AddCodeLine ("tax");
2915 AddCodeLine ("lda #$00");
2921 } else if (val >= 1 && val <= 4) {
2922 if (flags & CF_UNSIGNED) {
2923 AddCodeLine ("jsr shlax%ld", val);
2925 AddCodeLine ("jsr aslax%ld", val);
2935 } else if (val >= 1 && val <= 4) {
2936 if (flags & CF_UNSIGNED) {
2937 AddCodeLine ("jsr shleax%ld", val);
2939 AddCodeLine ("jsr asleax%ld", val);
2942 } else if (val == 8) {
2943 AddCodeLine ("ldy sreg");
2944 AddCodeLine ("sty sreg+1");
2945 AddCodeLine ("stx sreg");
2946 AddCodeLine ("tax");
2947 AddCodeLine ("lda #$00");
2949 } else if (val == 16) {
2950 AddCodeLine ("stx sreg+1");
2951 AddCodeLine ("sta sreg");
2952 AddCodeLine ("lda #$00");
2953 AddCodeLine ("tax");
2962 /* If we go here, we didn't emit code. Push the lhs on stack and fall
2963 * into the normal, non-optimized stuff.
2965 g_push (flags & ~CF_CONST, 0);
2969 /* Use long way over the stack */
2970 oper (flags, val, ops);
2975 void g_neg (unsigned flags)
2976 /* Primary = -Primary */
2978 switch (flags & CF_TYPE) {
2982 AddCodeLine ("jsr negax");
2986 AddCodeLine ("jsr negeax");
2996 void g_bneg (unsigned flags)
2997 /* Primary = !Primary */
2999 switch (flags & CF_TYPE) {
3002 AddCodeLine ("jsr bnega");
3006 AddCodeLine ("jsr bnegax");
3010 AddCodeLine ("jsr bnegeax");
3020 void g_com (unsigned flags)
3021 /* Primary = ~Primary */
3023 switch (flags & CF_TYPE) {
3027 AddCodeLine ("jsr complax");
3031 AddCodeLine ("jsr compleax");
3041 void g_inc (unsigned flags, unsigned long val)
3042 /* Increment the primary register by a given number */
3044 /* Don't inc by zero */
3049 /* Generate code for the supported types */
3051 switch (flags & CF_TYPE) {
3054 if (flags & CF_FORCECHAR) {
3055 if (CPU == CPU_65C02 && val <= 2) {
3057 AddCodeLine ("ina");
3060 AddCodeLine ("clc");
3061 AddCodeLine ("adc #$%02X", (unsigned char)val);
3068 if (CPU == CPU_65C02 && val == 1) {
3069 unsigned L = GetLocalLabel();
3070 AddCodeLine ("ina");
3071 AddCodeLine ("bne %s", LocalLabelName (L));
3072 AddCodeLine ("inx");
3074 } else if (CodeSizeFactor < 200) {
3077 AddCodeLine ("jsr incax%lu", val);
3078 } else if (val <= 255) {
3080 AddCodeLine ("jsr incaxy");
3082 g_add (flags | CF_CONST, val);
3085 /* Inline the code */
3087 if ((val & 0xFF) != 0) {
3088 unsigned L = GetLocalLabel();
3089 AddCodeLine ("clc");
3090 AddCodeLine ("adc #$%02X", (unsigned char) val);
3091 AddCodeLine ("bcc %s", LocalLabelName (L));
3092 AddCodeLine ("inx");
3096 AddCodeLine ("inx");
3099 AddCodeLine ("inx");
3102 AddCodeLine ("inx");
3105 AddCodeLine ("clc");
3106 if ((val & 0xFF) != 0) {
3107 AddCodeLine ("adc #$%02X", (unsigned char) val);
3109 AddCodeLine ("pha");
3110 AddCodeLine ("txa");
3111 AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3112 AddCodeLine ("tax");
3113 AddCodeLine ("pla");
3121 AddCodeLine ("jsr inceaxy");
3123 g_add (flags | CF_CONST, val);
3135 void g_dec (unsigned flags, unsigned long val)
3136 /* Decrement the primary register by a given number */
3138 /* Don't dec by zero */
3143 /* Generate code for the supported types */
3145 switch (flags & CF_TYPE) {
3148 if (flags & CF_FORCECHAR) {
3149 if (CPU == CPU_65C02 && val <= 2) {
3151 AddCodeLine ("dea");
3154 AddCodeLine ("sec");
3155 AddCodeLine ("sbc #$%02X", (unsigned char)val);
3162 if (CodeSizeFactor < 200) {
3163 /* Use subroutines */
3165 AddCodeLine ("jsr decax%d", (int) val);
3166 } else if (val <= 255) {
3168 AddCodeLine ("jsr decaxy");
3170 g_sub (flags | CF_CONST, val);
3173 /* Inline the code */
3175 if ((val & 0xFF) != 0) {
3176 unsigned L = GetLocalLabel();
3177 AddCodeLine ("sec");
3178 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3179 AddCodeLine ("bcs %s", LocalLabelName (L));
3180 AddCodeLine ("dex");
3184 AddCodeLine ("dex");
3187 AddCodeLine ("dex");
3190 AddCodeLine ("sec");
3191 if ((val & 0xFF) != 0) {
3192 AddCodeLine ("sbc #$%02X", (unsigned char) val);
3194 AddCodeLine ("pha");
3195 AddCodeLine ("txa");
3196 AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3197 AddCodeLine ("tax");
3198 AddCodeLine ("pla");
3206 AddCodeLine ("jsr deceaxy");
3208 g_sub (flags | CF_CONST, val);
3221 * Following are the conditional operators. They compare the TOS against
3222 * the primary and put a literal 1 in the primary if the condition is
3223 * true, otherwise they clear the primary register
3228 void g_eq (unsigned flags, unsigned long val)
3229 /* Test for equal */
3231 static char* ops [12] = {
3232 "toseq00", "toseqa0", "toseqax",
3233 "toseq00", "toseqa0", "toseqax",
3240 /* If the right hand side is const, the lhs is not on stack but still
3241 * in the primary register.
3243 if (flags & CF_CONST) {
3245 switch (flags & CF_TYPE) {
3248 if (flags & CF_FORCECHAR) {
3249 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3250 AddCodeLine ("jsr booleq");
3256 L = GetLocalLabel();
3257 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3258 AddCodeLine ("bne %s", LocalLabelName (L));
3259 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3261 AddCodeLine ("jsr booleq");
3271 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3272 * into the normal, non-optimized stuff.
3274 g_push (flags & ~CF_CONST, 0);
3278 /* Use long way over the stack */
3279 oper (flags, val, ops);
3284 void g_ne (unsigned flags, unsigned long val)
3285 /* Test for not equal */
3287 static char* ops [12] = {
3288 "tosne00", "tosnea0", "tosneax",
3289 "tosne00", "tosnea0", "tosneax",
3296 /* If the right hand side is const, the lhs is not on stack but still
3297 * in the primary register.
3299 if (flags & CF_CONST) {
3301 switch (flags & CF_TYPE) {
3304 if (flags & CF_FORCECHAR) {
3305 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3306 AddCodeLine ("jsr boolne");
3312 L = GetLocalLabel();
3313 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3314 AddCodeLine ("bne %s", LocalLabelName (L));
3315 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3317 AddCodeLine ("jsr boolne");
3327 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3328 * into the normal, non-optimized stuff.
3330 g_push (flags & ~CF_CONST, 0);
3334 /* Use long way over the stack */
3335 oper (flags, val, ops);
3340 void g_lt (unsigned flags, unsigned long val)
3341 /* Test for less than */
3343 static char* ops [12] = {
3344 "toslt00", "toslta0", "tosltax",
3345 "tosult00", "tosulta0", "tosultax",
3350 /* If the right hand side is const, the lhs is not on stack but still
3351 * in the primary register.
3353 if (flags & CF_CONST) {
3355 /* Give a warning in some special cases */
3356 if ((flags & CF_UNSIGNED) && val == 0) {
3357 Warning ("Condition is never true");
3360 /* Look at the type */
3361 switch (flags & CF_TYPE) {
3364 if (flags & CF_FORCECHAR) {
3365 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3366 if (flags & CF_UNSIGNED) {
3367 AddCodeLine ("jsr boolult");
3369 AddCodeLine ("jsr boollt");
3376 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3377 /* If we have a signed compare against zero, we only need to
3378 * test the high byte.
3380 AddCodeLine ("txa");
3381 AddCodeLine ("jsr boollt");
3384 /* Direct code only for unsigned data types */
3385 if (flags & CF_UNSIGNED) {
3386 unsigned L = GetLocalLabel();
3387 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3388 AddCodeLine ("bne %s", LocalLabelName (L));
3389 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3391 AddCodeLine ("jsr boolult");
3397 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3398 /* If we have a signed compare against zero, we only need to
3399 * test the high byte.
3401 AddCodeLine ("lda sreg+1");
3402 AddCodeLine ("jsr boollt");
3411 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3412 * into the normal, non-optimized stuff.
3414 g_push (flags & ~CF_CONST, 0);
3418 /* Use long way over the stack */
3419 oper (flags, val, ops);
3424 void g_le (unsigned flags, unsigned long val)
3425 /* Test for less than or equal to */
3427 static char* ops [12] = {
3428 "tosle00", "toslea0", "tosleax",
3429 "tosule00", "tosulea0", "tosuleax",
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 /* <= is not very effective on the 6502, so try to convert
3441 * it into < if the value is in a valid range.
3444 /* Look at the type */
3445 switch (flags & CF_TYPE) {
3448 if (flags & CF_FORCECHAR) {
3449 if (flags & CF_UNSIGNED) {
3451 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3452 AddCodeLine ("jsr boolult");
3454 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3455 AddCodeLine ("jsr boolule");
3459 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3460 AddCodeLine ("jsr boollt");
3462 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3463 AddCodeLine ("jsr boolle");
3471 if (flags & CF_UNSIGNED) {
3472 unsigned L = GetLocalLabel();
3473 const char* Name = "boolule";
3478 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3479 AddCodeLine ("bne %s", LocalLabelName (L));
3480 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3482 AddCodeLine ("jsr %s", Name);
3494 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3495 * into the normal, non-optimized stuff.
3497 g_push (flags & ~CF_CONST, 0);
3501 /* Use long way over the stack */
3502 oper (flags, val, ops);
3507 void g_gt (unsigned flags, unsigned long val)
3508 /* Test for greater than */
3510 static char* ops [12] = {
3511 "tosgt00", "tosgta0", "tosgtax",
3512 "tosugt00", "tosugta0", "tosugtax",
3518 /* If the right hand side is const, the lhs is not on stack but still
3519 * in the primary register.
3521 if (flags & CF_CONST) {
3523 /* > is not very effective on the 6502, so try to convert
3524 * it into >= if the value is in a valid range.
3527 /* Look at the type */
3528 switch (flags & CF_TYPE) {
3531 if (flags & CF_FORCECHAR) {
3532 if (flags & CF_UNSIGNED) {
3533 /* If we have a compare > 0, we will replace it by
3534 * != 0 here, since both are identical but the latter
3535 * is easier to optimize.
3538 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3539 AddCodeLine ("jsr boolne");
3540 } else if (val < 255) {
3541 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3542 AddCodeLine ("jsr booluge");
3544 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3545 AddCodeLine ("jsr boolugt");
3549 AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3550 AddCodeLine ("jsr boolge");
3552 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3553 AddCodeLine ("jsr boolgt");
3561 if (flags & CF_UNSIGNED) {
3562 /* If we have a compare > 0, we will replace it by
3563 * != 0 here, since both are identical but the latter
3564 * is easier to optimize.
3567 AddCodeLine ("stx tmp1");
3568 AddCodeLine ("ora tmp1");
3569 AddCodeLine ("jsr boolne");
3571 unsigned L = GetLocalLabel();
3572 const char* Name = "boolugt";
3577 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3578 AddCodeLine ("bne %s", LocalLabelName (L));
3579 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3581 AddCodeLine ("jsr %s", Name);
3594 /* If we go here, we didn't emit code. Push the lhs on stack and fall
3595 * into the normal, non-optimized stuff.
3597 g_push (flags & ~CF_CONST, 0);
3601 /* Use long way over the stack */
3602 oper (flags, val, ops);
3607 void g_ge (unsigned flags, unsigned long val)
3608 /* Test for greater than or equal to */
3610 static char* ops [12] = {
3611 "tosge00", "tosgea0", "tosgeax",
3612 "tosuge00", "tosugea0", "tosugeax",
3618 /* If the right hand side is const, the lhs is not on stack but still
3619 * in the primary register.
3621 if (flags & CF_CONST) {
3623 /* Give a warning in some special cases */
3624 if ((flags & CF_UNSIGNED) && val == 0) {
3625 Warning ("Condition is always true");
3628 /* Look at the type */
3629 switch (flags & CF_TYPE) {
3632 if (flags & CF_FORCECHAR) {
3633 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3634 if (flags & CF_UNSIGNED) {
3635 AddCodeLine ("jsr booluge");
3637 AddCodeLine ("jsr boolge");
3644 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3645 /* If we have a signed compare against zero, we only need to
3646 * test the high byte.
3648 AddCodeLine ("txa");
3649 AddCodeLine ("jsr boolge");
3652 /* Direct code only for unsigned data types */
3653 if (flags & CF_UNSIGNED) {
3654 unsigned L = GetLocalLabel();
3655 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3656 AddCodeLine ("bne %s", LocalLabelName (L));
3657 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3659 AddCodeLine ("jsr booluge");
3665 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3666 /* If we have a signed compare against zero, we only need to
3667 * test the high byte.
3669 AddCodeLine ("lda sreg+1");
3670 AddCodeLine ("jsr boolge");
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 /*****************************************************************************/
3693 /* Allocating static storage */
3694 /*****************************************************************************/
3698 void g_res (unsigned n)
3699 /* Reserve static storage, n bytes */
3701 AddDataLine ("\t.res\t%u,$00", n);
3706 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3707 /* Define data with the size given in flags */
3709 if (flags & CF_CONST) {
3711 /* Numeric constant */
3712 switch (flags & CF_TYPE) {
3715 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
3719 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
3723 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3734 /* Create the correct label name */
3735 const char* Label = GetLabelName (flags, val, offs);
3737 /* Labels are always 16 bit */
3738 AddDataLine ("\t.addr\t%s", Label);
3745 void g_defbytes (const void* Bytes, unsigned Count)
3746 /* Output a row of bytes as a constant */
3752 /* Cast the buffer pointer */
3753 const unsigned char* Data = (const unsigned char*) Bytes;
3755 /* Output the stuff */
3758 /* How many go into this line? */
3759 if ((Chunk = Count) > 16) {
3764 /* Output one line */
3765 strcpy (Buf, "\t.byte\t");
3768 B += sprintf (B, "$%02X", *Data++);
3774 /* Output the line */
3781 void g_zerobytes (unsigned n)
3782 /* Output n bytes of data initialized with zero */
3784 AddDataLine ("\t.res\t%u,$00", n);
3789 /*****************************************************************************/
3790 /* Switch statement */
3791 /*****************************************************************************/
3795 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
3796 /* Generate code for a switch statement */
3798 unsigned NextLabel = 0;
3801 /* Setup registers and determine which compare insn to use */
3802 const char* Compare;
3805 Compare = "cmp #$%02X";
3808 Compare = "cpx #$%02X";
3811 AddCodeLine ("ldy sreg");
3812 Compare = "cpy #$%02X";
3815 AddCodeLine ("ldy sreg+1");
3816 Compare = "cpy #$%02X";
3819 Internal ("Invalid depth in g_switch: %u", Depth);
3822 /* Walk over all nodes */
3823 for (I = 0; I < CollCount (Nodes); ++I) {
3825 /* Get the next case node */
3826 CaseNode* N = CollAtUnchecked (Nodes, I);
3828 /* If we have a next label, define it */
3830 g_defcodelabel (NextLabel);
3834 /* Do the compare */
3835 AddCodeLine (Compare, CN_GetValue (N));
3837 /* If this is the last level, jump directly to the case code if found */
3840 /* Branch if equal */
3841 g_falsejump (0, CN_GetLabel (N));
3845 /* Determine the next label */
3846 if (I == CollCount (Nodes) - 1) {
3847 /* Last node means not found */
3848 g_truejump (0, DefaultLabel);
3850 /* Jump to the next check */
3851 NextLabel = GetLocalLabel ();
3852 g_truejump (0, NextLabel);
3855 /* Check the next level */
3856 g_switch (N->Nodes, DefaultLabel, Depth-1);
3861 /* If we go here, we haven't found the label */
3862 g_jump (DefaultLabel);
3867 /*****************************************************************************/
3868 /* User supplied assembler code */
3869 /*****************************************************************************/
3873 void g_asmcode (struct StrBuf* B)
3874 /* Output one line of assembler code. */
3876 AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));
3881 /*****************************************************************************/
3882 /* Inlined known functions */
3883 /*****************************************************************************/
3887 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3888 /* Inline the strlen() function */
3890 /* We need a label in both cases */
3891 unsigned label = GetLocalLabel ();
3893 /* Two different encodings */
3894 if (flags & CF_CONST) {
3896 /* The address of the string is constant. Create the correct label name */
3897 char* lbuf = GetLabelName (flags, val, offs);
3899 /* Generate the strlen code */
3900 AddCodeLine ("ldy #$FF");
3901 g_defcodelabel (label);
3902 AddCodeLine ("iny");
3903 AddCodeLine ("lda %s,y", lbuf);
3904 AddCodeLine ("bne %s", LocalLabelName (label));
3905 AddCodeLine ("tax");
3906 AddCodeLine ("tya");
3910 /* Address not constant but in primary */
3911 if (CodeSizeFactor < 400) {
3912 /* This is too much code, so call strlen instead of inlining */
3913 AddCodeLine ("jsr _strlen");
3915 /* Inline the function */
3916 AddCodeLine ("sta ptr1");
3917 AddCodeLine ("stx ptr1+1");
3918 AddCodeLine ("ldy #$FF");
3919 g_defcodelabel (label);
3920 AddCodeLine ("iny");
3921 AddCodeLine ("lda (ptr1),y");
3922 AddCodeLine ("bne %s", LocalLabelName (label));
3923 AddCodeLine ("tax");
3924 AddCodeLine ("tya");