1 /*****************************************************************************/
5 /* Code segment entry */
9 /* (C) 2001-2009, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
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 /*****************************************************************************/
41 #include "debugflag.h"
56 /*****************************************************************************/
58 /*****************************************************************************/
63 static char EmptyArg[] = "";
67 /*****************************************************************************/
68 /* Helper functions */
69 /*****************************************************************************/
73 static void FreeArg (char* Arg)
74 /* Free a code entry argument */
76 if (Arg != EmptyArg) {
83 static char* GetArgCopy (const char* Arg)
84 /* Create an argument copy for assignment */
86 if (Arg && Arg[0] != '\0') {
90 /* Use the empty argument string */
97 static int NumArg (const char* Arg, unsigned long* Num)
98 /* If the given argument is numerical, convert it and return true. Otherwise
99 ** set Num to zero and return false.
105 /* Determine the base */
110 } else if (*Arg == '%') {
115 /* Convert the value. strtol is not exactly what we want here, but it's
116 ** cheap and may be replaced by something fancier later.
118 Val = strtoul (Arg, &End, Base);
120 /* Check if the conversion was successful */
123 /* Could not convert */
138 static void SetUseChgInfo (CodeEntry* E, const OPCDesc* D)
139 /* Set the Use and Chg in E */
143 /* If this is a subroutine call, or a jump to an external function,
144 ** lookup the information about this function and use it. The jump itself
145 ** does not change any registers, so we don't need to use the data from D.
147 if ((E->Info & (OF_UBRA | OF_CALL)) != 0 && E->JumpTo == 0) {
148 /* A subroutine call or jump to external symbol (function exit) */
149 GetFuncInfo (E->Arg, &E->Use, &E->Chg);
151 /* Some other instruction. Use the values from the opcode description
152 ** plus addressing mode info.
154 E->Use = D->Use | GetAMUseInfo (E->AM);
157 /* Check for special zero page registers used */
161 if (E->OPC == OP65_ASL || E->OPC == OP65_DEC ||
162 E->OPC == OP65_INC || E->OPC == OP65_LSR ||
163 E->OPC == OP65_ROL || E->OPC == OP65_ROR) {
164 /* A is changed by these insns */
171 /* Be conservative: */
175 Info = GetZPInfo (E->Arg);
176 if (Info && Info->ByteUse != REG_NONE) {
177 if (E->OPC == OP65_ASL || E->OPC == OP65_DEC ||
178 E->OPC == OP65_INC || E->OPC == OP65_LSR ||
179 E->OPC == OP65_ROL || E->OPC == OP65_ROR ||
180 E->OPC == OP65_TRB || E->OPC == OP65_TSB) {
181 /* The zp loc is both, input and output */
182 E->Chg |= Info->ByteUse;
183 E->Use |= Info->ByteUse;
184 } else if ((E->Info & OF_STORE) != 0) {
186 E->Chg |= Info->ByteUse;
189 E->Use |= Info->ByteUse;
197 Info = GetZPInfo (E->Arg);
198 if (Info && Info->ByteUse != REG_NONE) {
199 /* These addressing modes will never change the zp loc */
200 E->Use |= Info->WordUse;
205 /* Keep gcc silent */
213 /*****************************************************************************/
215 /*****************************************************************************/
219 const char* MakeHexArg (unsigned Num)
220 /* Convert Num into a string in the form $XY, suitable for passing it as an
221 ** argument to NewCodeEntry, and return a pointer to the string.
222 ** BEWARE: The function returns a pointer to a static buffer, so the value is
223 ** gone if you call it twice (and apart from that it's not thread and signal
228 xsprintf (Buf, sizeof (Buf), "$%02X", (unsigned char) Num);
234 CodeEntry* NewCodeEntry (opc_t OPC, am_t AM, const char* Arg,
235 CodeLabel* JumpTo, LineInfo* LI)
236 /* Create a new code entry, initialize and return it */
238 /* Get the opcode description */
239 const OPCDesc* D = GetOPCDesc (OPC);
241 /* Allocate memory */
242 CodeEntry* E = xmalloc (sizeof (CodeEntry));
244 /* Initialize the fields */
247 E->Size = GetInsnSize (E->OPC, E->AM);
248 E->Arg = GetArgCopy (Arg);
249 E->Flags = NumArg (E->Arg, &E->Num)? CEF_NUMARG : 0; /* Needs E->Arg */
252 E->LI = UseLineInfo (LI);
254 SetUseChgInfo (E, D);
255 InitCollection (&E->Labels);
257 /* If we have a label given, add this entry to the label */
259 CollAppend (&JumpTo->JumpFrom, E);
262 /* Return the initialized struct */
268 void FreeCodeEntry (CodeEntry* E)
269 /* Free the given code entry */
271 /* Free the string argument if we have one */
274 /* Cleanup the collection */
275 DoneCollection (&E->Labels);
277 /* Release the line info */
278 ReleaseLineInfo (E->LI);
280 /* Delete the register info */
289 void CE_ReplaceOPC (CodeEntry* E, opc_t OPC)
290 /* Replace the opcode of the instruction. This will also replace related info,
291 ** Size, Use and Chg, but it will NOT update any arguments or labels.
294 /* Get the opcode descriptor */
295 const OPCDesc* D = GetOPCDesc (OPC);
297 /* Replace the opcode */
300 E->Size = GetInsnSize (E->OPC, E->AM);
301 SetUseChgInfo (E, D);
306 int CodeEntriesAreEqual (const CodeEntry* E1, const CodeEntry* E2)
307 /* Check if both code entries are equal */
309 return (E1->OPC == E2->OPC && E1->AM == E2->AM && strcmp (E1->Arg, E2->Arg) == 0);
314 void CE_AttachLabel (CodeEntry* E, CodeLabel* L)
315 /* Attach the label to the entry */
317 /* Add it to the entries label list */
318 CollAppend (&E->Labels, L);
320 /* Tell the label about it's owner */
326 void CE_ClearJumpTo (CodeEntry* E)
327 /* Clear the JumpTo entry and the argument (which contained the name of the
328 ** label). Note: The function will not clear the backpointer from the label,
329 ** so use it with care.
332 /* Clear the JumpTo entry */
335 /* Clear the argument and assign the empty one */
342 void CE_MoveLabel (CodeLabel* L, CodeEntry* E)
343 /* Move the code label L from it's former owner to the code entry E. */
345 /* Delete the label from the owner */
346 CollDeleteItem (&L->Owner->Labels, L);
348 /* Set the new owner */
349 CollAppend (&E->Labels, L);
355 void CE_SetArg (CodeEntry* E, const char* Arg)
356 /* Replace the argument by the new one. */
358 /* Free the old argument */
361 /* Assign the new one */
362 E->Arg = GetArgCopy (Arg);
367 void CE_SetNumArg (CodeEntry* E, long Num)
368 /* Set a new numeric argument for the given code entry that must already
369 ** have a numeric argument.
374 /* Check that the entry has a numerical argument */
375 CHECK (E->Flags & CEF_NUMARG);
377 /* Make the new argument string */
380 xsprintf (Buf, sizeof (Buf), "$%02X", (unsigned) Num);
381 } else if (E->Size == 3) {
383 xsprintf (Buf, sizeof (Buf), "$%04X", (unsigned) Num);
385 Internal ("Invalid instruction size in CE_SetNumArg");
388 /* Replace the argument by the new one */
391 /* Use the new numerical value */
397 int CE_IsConstImm (const CodeEntry* E)
398 /* Return true if the argument of E is a constant immediate value */
400 return (E->AM == AM65_IMM && CE_HasNumArg (E));
405 int CE_IsKnownImm (const CodeEntry* E, unsigned long Num)
406 /* Return true if the argument of E is a constant immediate value that is
410 return (E->AM == AM65_IMM && CE_HasNumArg (E) && E->Num == Num);
415 int CE_UseLoadFlags (CodeEntry* E)
416 /* Return true if the instruction uses any flags that are set by a load of
417 ** a register (N and Z).
420 /* Follow unconditional branches, but beware of endless loops. After this,
421 ** E will point to the first entry that is not a branch.
423 if (E->Info & OF_UBRA) {
424 Collection C = AUTO_COLLECTION_INITIALIZER;
426 /* Follow the chain */
427 while (E->Info & OF_UBRA) {
429 /* Remember the entry so we can detect loops */
432 /* Check the target */
433 if (E->JumpTo == 0 || CollIndex (&C, E->JumpTo->Owner) >= 0) {
434 /* Unconditional jump to external symbol, or endless loop. */
436 return 0; /* Flags not used */
439 /* Follow the chain */
440 E = E->JumpTo->Owner;
443 /* Delete the collection */
447 /* A branch will use the flags */
448 if (E->Info & OF_FBRA) {
452 /* Call of a boolean transformer routine will also use the flags */
453 if (E->OPC == OP65_JSR) {
454 /* Get the condition that is evaluated and check it */
455 switch (FindBoolCmpCond (E->Arg)) {
464 /* Will use the N or Z flags */
468 case CMP_UGE: /* Uses only carry */
469 case CMP_ULT: /* Dito */
470 default: /* No bool transformer subroutine */
481 void CE_FreeRegInfo (CodeEntry* E)
482 /* Free an existing register info struct */
492 void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
493 /* Generate register info for this instruction. If an old info exists, it is
497 /* Pointers to the register contents */
501 /* Function register usage */
502 unsigned short Use, Chg;
504 /* If we don't have a register info struct, allocate one. */
506 E->RI = NewRegInfo (InputRegs);
509 E->RI->In = *InputRegs;
511 RC_Invalidate (&E->RI->In);
513 E->RI->Out2 = E->RI->Out = E->RI->In;
516 /* Get pointers to the register contents */
520 /* Handle the different instructions */
524 /* We don't know the value of the carry, so the result is
527 Out->RegA = UNKNOWN_REGVAL;
531 if (RegValIsKnown (In->RegA)) {
532 if (CE_IsConstImm (E)) {
533 Out->RegA = In->RegA & (short) E->Num;
534 } else if (E->AM == AM65_ZP) {
535 switch (GetKnownReg (E->Use & REG_ZP, In)) {
537 Out->RegA = In->RegA & In->Tmp1;
540 Out->RegA = In->RegA & In->Ptr1Lo;
543 Out->RegA = In->RegA & In->Ptr1Hi;
546 Out->RegA = In->RegA & In->SRegLo;
549 Out->RegA = In->RegA & In->SRegHi;
552 Out->RegA = UNKNOWN_REGVAL;
556 Out->RegA = UNKNOWN_REGVAL;
558 } else if (CE_IsKnownImm (E, 0)) {
559 /* A and $00 does always give zero */
565 if (E->AM == AM65_ACC && RegValIsKnown (In->RegA)) {
566 Out->RegA = (In->RegA << 1) & 0xFF;
567 } else if (E->AM == AM65_ZP) {
568 switch (GetKnownReg (E->Chg & REG_ZP, In)) {
570 Out->Tmp1 = (In->Tmp1 << 1) & 0xFF;
573 Out->Ptr1Lo = (In->Ptr1Lo << 1) & 0xFF;
576 Out->Ptr1Hi = (In->Ptr1Hi << 1) & 0xFF;
579 Out->SRegLo = (In->SRegLo << 1) & 0xFF;
582 Out->SRegHi = (In->SRegHi << 1) & 0xFF;
585 } else if (E->AM == AM65_ZPX) {
586 /* Invalidates all ZP registers */
587 RC_InvalidateZP (Out);
646 if (RegValIsKnown (In->RegA)) {
647 Out->RegA = (In->RegA - 1) & 0xFF;
652 if (E->AM == AM65_ACC && RegValIsKnown (In->RegA)) {
653 Out->RegA = (In->RegA - 1) & 0xFF;
654 } else if (E->AM == AM65_ZP) {
655 switch (GetKnownReg (E->Chg & REG_ZP, In)) {
657 Out->Tmp1 = (In->Tmp1 - 1) & 0xFF;
660 Out->Ptr1Lo = (In->Ptr1Lo - 1) & 0xFF;
663 Out->Ptr1Hi = (In->Ptr1Hi - 1) & 0xFF;
666 Out->SRegLo = (In->SRegLo - 1) & 0xFF;
669 Out->SRegHi = (In->SRegHi - 1) & 0xFF;
672 } else if (E->AM == AM65_ZPX) {
673 /* Invalidates all ZP registers */
674 RC_InvalidateZP (Out);
679 if (RegValIsKnown (In->RegX)) {
680 Out->RegX = (In->RegX - 1) & 0xFF;
685 if (RegValIsKnown (In->RegY)) {
686 Out->RegY = (In->RegY - 1) & 0xFF;
691 if (RegValIsKnown (In->RegA)) {
692 if (CE_IsConstImm (E)) {
693 Out->RegA = In->RegA ^ (short) E->Num;
694 } else if (E->AM == AM65_ZP) {
695 switch (GetKnownReg (E->Use & REG_ZP, In)) {
697 Out->RegA = In->RegA ^ In->Tmp1;
700 Out->RegA = In->RegA ^ In->Ptr1Lo;
703 Out->RegA = In->RegA ^ In->Ptr1Hi;
706 Out->RegA = In->RegA ^ In->SRegLo;
709 Out->RegA = In->RegA ^ In->SRegHi;
712 Out->RegA = UNKNOWN_REGVAL;
716 Out->RegA = UNKNOWN_REGVAL;
722 if (RegValIsKnown (In->RegA)) {
723 Out->RegA = (In->RegA + 1) & 0xFF;
728 if (E->AM == AM65_ACC && RegValIsKnown (In->RegA)) {
729 Out->RegA = (In->RegA + 1) & 0xFF;
730 } else if (E->AM == AM65_ZP) {
731 switch (GetKnownReg (E->Chg & REG_ZP, In)) {
733 Out->Tmp1 = (In->Tmp1 + 1) & 0xFF;
736 Out->Ptr1Lo = (In->Ptr1Lo + 1) & 0xFF;
739 Out->Ptr1Hi = (In->Ptr1Hi + 1) & 0xFF;
742 Out->SRegLo = (In->SRegLo + 1) & 0xFF;
745 Out->SRegHi = (In->SRegHi + 1) & 0xFF;
748 } else if (E->AM == AM65_ZPX) {
749 /* Invalidates all ZP registers */
750 RC_InvalidateZP (Out);
755 if (RegValIsKnown (In->RegX)) {
756 Out->RegX = (In->RegX + 1) & 0xFF;
761 if (RegValIsKnown (In->RegY)) {
762 Out->RegY = (In->RegY + 1) & 0xFF;
788 /* Get the code info for the function */
789 GetFuncInfo (E->Arg, &Use, &Chg);
791 Out->RegA = UNKNOWN_REGVAL;
794 Out->RegX = UNKNOWN_REGVAL;
797 Out->RegY = UNKNOWN_REGVAL;
799 if (Chg & REG_TMP1) {
800 Out->Tmp1 = UNKNOWN_REGVAL;
802 if (Chg & REG_PTR1_LO) {
803 Out->Ptr1Lo = UNKNOWN_REGVAL;
805 if (Chg & REG_PTR1_HI) {
806 Out->Ptr1Hi = UNKNOWN_REGVAL;
808 if (Chg & REG_SREG_LO) {
809 Out->SRegLo = UNKNOWN_REGVAL;
811 if (Chg & REG_SREG_HI) {
812 Out->SRegHi = UNKNOWN_REGVAL;
814 /* ## FIXME: Quick hack for some known functions: */
815 if (strcmp (E->Arg, "complax") == 0) {
816 if (RegValIsKnown (In->RegA)) {
817 Out->RegA = (In->RegA ^ 0xFF);
819 if (RegValIsKnown (In->RegX)) {
820 Out->RegX = (In->RegX ^ 0xFF);
822 } else if (strcmp (E->Arg, "tosandax") == 0) {
829 } else if (strcmp (E->Arg, "tosaslax") == 0) {
830 if (RegValIsKnown (In->RegA) && (In->RegA & 0x0F) >= 8) {
834 } else if (strcmp (E->Arg, "tosorax") == 0) {
835 if (In->RegA == 0xFF) {
838 if (In->RegX == 0xFF) {
841 } else if (strcmp (E->Arg, "tosshlax") == 0) {
842 if ((In->RegA & 0x0F) >= 8) {
845 } else if (FindBoolCmpCond (E->Arg) != CMP_INV ||
846 FindTosCmpCond (E->Arg) != CMP_INV) {
847 /* Result is boolean value, so X is zero on output */
859 if (CE_IsConstImm (E)) {
860 Out->RegA = (unsigned char) E->Num;
861 } else if (E->AM == AM65_ZP) {
862 switch (GetKnownReg (E->Use & REG_ZP, In)) {
864 Out->RegA = In->Tmp1;
867 Out->RegA = In->Ptr1Lo;
870 Out->RegA = In->Ptr1Hi;
873 Out->RegA = In->SRegLo;
876 Out->RegA = In->SRegHi;
879 Out->RegA = UNKNOWN_REGVAL;
883 /* A is now unknown */
884 Out->RegA = UNKNOWN_REGVAL;
889 if (CE_IsConstImm (E)) {
890 Out->RegX = (unsigned char) E->Num;
891 } else if (E->AM == AM65_ZP) {
892 switch (GetKnownReg (E->Use & REG_ZP, In)) {
894 Out->RegX = In->Tmp1;
897 Out->RegX = In->Ptr1Lo;
900 Out->RegX = In->Ptr1Hi;
903 Out->RegX = In->SRegLo;
906 Out->RegX = In->SRegHi;
909 Out->RegX = UNKNOWN_REGVAL;
913 /* X is now unknown */
914 Out->RegX = UNKNOWN_REGVAL;
919 if (CE_IsConstImm (E)) {
920 Out->RegY = (unsigned char) E->Num;
921 } else if (E->AM == AM65_ZP) {
922 switch (GetKnownReg (E->Use & REG_ZP, In)) {
924 Out->RegY = In->Tmp1;
927 Out->RegY = In->Ptr1Lo;
930 Out->RegY = In->Ptr1Hi;
933 Out->RegY = In->SRegLo;
936 Out->RegY = In->SRegHi;
939 Out->RegY = UNKNOWN_REGVAL;
943 /* Y is now unknown */
944 Out->RegY = UNKNOWN_REGVAL;
949 if (E->AM == AM65_ACC && RegValIsKnown (In->RegA)) {
950 Out->RegA = (In->RegA >> 1) & 0xFF;
951 } else if (E->AM == AM65_ZP) {
952 switch (GetKnownReg (E->Chg & REG_ZP, In)) {
954 Out->Tmp1 = (In->Tmp1 >> 1) & 0xFF;
957 Out->Ptr1Lo = (In->Ptr1Lo >> 1) & 0xFF;
960 Out->Ptr1Hi = (In->Ptr1Hi >> 1) & 0xFF;
963 Out->SRegLo = (In->SRegLo >> 1) & 0xFF;
966 Out->SRegHi = (In->SRegHi >> 1) & 0xFF;
969 } else if (E->AM == AM65_ZPX) {
970 /* Invalidates all ZP registers */
971 RC_InvalidateZP (Out);
979 if (RegValIsKnown (In->RegA)) {
980 if (CE_IsConstImm (E)) {
981 Out->RegA = In->RegA | (short) E->Num;
982 } else if (E->AM == AM65_ZP) {
983 switch (GetKnownReg (E->Use & REG_ZP, In)) {
985 Out->RegA = In->RegA | In->Tmp1;
988 Out->RegA = In->RegA | In->Ptr1Lo;
991 Out->RegA = In->RegA | In->Ptr1Hi;
994 Out->RegA = In->RegA | In->SRegLo;
997 Out->RegA = In->RegA | In->SRegHi;
1000 Out->RegA = UNKNOWN_REGVAL;
1004 /* A is now unknown */
1005 Out->RegA = UNKNOWN_REGVAL;
1007 } else if (CE_IsKnownImm (E, 0xFF)) {
1008 /* ORA with 0xFF does always give 0xFF */
1026 Out->RegA = UNKNOWN_REGVAL;
1033 Out->RegX = UNKNOWN_REGVAL;
1037 Out->RegY = UNKNOWN_REGVAL;
1041 /* We don't know the value of the carry bit */
1042 if (E->AM == AM65_ACC) {
1043 Out->RegA = UNKNOWN_REGVAL;
1044 } else if (E->AM == AM65_ZP) {
1045 switch (GetKnownReg (E->Chg & REG_ZP, In)) {
1047 Out->Tmp1 = UNKNOWN_REGVAL;
1050 Out->Ptr1Lo = UNKNOWN_REGVAL;
1053 Out->Ptr1Hi = UNKNOWN_REGVAL;
1056 Out->SRegLo = UNKNOWN_REGVAL;
1059 Out->SRegHi = UNKNOWN_REGVAL;
1062 } else if (E->AM == AM65_ZPX) {
1063 /* Invalidates all ZP registers */
1064 RC_InvalidateZP (Out);
1069 /* We don't know the value of the carry bit */
1070 if (E->AM == AM65_ACC) {
1071 Out->RegA = UNKNOWN_REGVAL;
1072 } else if (E->AM == AM65_ZP) {
1073 switch (GetKnownReg (E->Chg & REG_ZP, In)) {
1075 Out->Tmp1 = UNKNOWN_REGVAL;
1078 Out->Ptr1Lo = UNKNOWN_REGVAL;
1081 Out->Ptr1Hi = UNKNOWN_REGVAL;
1084 Out->SRegLo = UNKNOWN_REGVAL;
1087 Out->SRegHi = UNKNOWN_REGVAL;
1090 } else if (E->AM == AM65_ZPX) {
1091 /* Invalidates all ZP registers */
1092 RC_InvalidateZP (Out);
1103 /* We don't know the value of the carry bit */
1104 Out->RegA = UNKNOWN_REGVAL;
1117 if (E->AM == AM65_ZP) {
1118 switch (GetKnownReg (E->Chg & REG_ZP, 0)) {
1120 Out->Tmp1 = In->RegA;
1123 Out->Ptr1Lo = In->RegA;
1126 Out->Ptr1Hi = In->RegA;
1129 Out->SRegLo = In->RegA;
1132 Out->SRegHi = In->RegA;
1135 } else if (E->AM == AM65_ZPX) {
1136 /* Invalidates all ZP registers */
1137 RC_InvalidateZP (Out);
1142 if (E->AM == AM65_ZP) {
1143 switch (GetKnownReg (E->Chg & REG_ZP, 0)) {
1145 Out->Tmp1 = In->RegX;
1148 Out->Ptr1Lo = In->RegX;
1151 Out->Ptr1Hi = In->RegX;
1154 Out->SRegLo = In->RegX;
1157 Out->SRegHi = In->RegX;
1160 } else if (E->AM == AM65_ZPX) {
1161 /* Invalidates all ZP registers */
1162 RC_InvalidateZP (Out);
1167 if (E->AM == AM65_ZP) {
1168 switch (GetKnownReg (E->Chg & REG_ZP, 0)) {
1170 Out->Tmp1 = In->RegY;
1173 Out->Ptr1Lo = In->RegY;
1176 Out->Ptr1Hi = In->RegY;
1179 Out->SRegLo = In->RegY;
1182 Out->SRegHi = In->RegY;
1185 } else if (E->AM == AM65_ZPX) {
1186 /* Invalidates all ZP registers */
1187 RC_InvalidateZP (Out);
1192 if (E->AM == AM65_ZP) {
1193 switch (GetKnownReg (E->Chg & REG_ZP, 0)) {
1210 } else if (E->AM == AM65_ZPX) {
1211 /* Invalidates all ZP registers */
1212 RC_InvalidateZP (Out);
1217 Out->RegX = In->RegA;
1221 Out->RegY = In->RegA;
1225 if (E->AM == AM65_ZPX) {
1226 /* Invalidates all ZP registers */
1227 RC_InvalidateZP (Out);
1228 } else if (E->AM == AM65_ZP) {
1229 if (RegValIsKnown (In->RegA)) {
1230 switch (GetKnownReg (E->Chg & REG_ZP, In)) {
1232 Out->Tmp1 &= ~In->RegA;
1235 Out->Ptr1Lo &= ~In->RegA;
1238 Out->Ptr1Hi &= ~In->RegA;
1241 Out->SRegLo &= ~In->RegA;
1244 Out->SRegHi &= ~In->RegA;
1248 switch (GetKnownReg (E->Chg & REG_ZP, In)) {
1250 Out->Tmp1 = UNKNOWN_REGVAL;
1253 Out->Ptr1Lo = UNKNOWN_REGVAL;
1256 Out->Ptr1Hi = UNKNOWN_REGVAL;
1259 Out->SRegLo = UNKNOWN_REGVAL;
1262 Out->SRegHi = UNKNOWN_REGVAL;
1270 if (E->AM == AM65_ZPX) {
1271 /* Invalidates all ZP registers */
1272 RC_InvalidateZP (Out);
1273 } else if (E->AM == AM65_ZP) {
1274 if (RegValIsKnown (In->RegA)) {
1275 switch (GetKnownReg (E->Chg & REG_ZP, In)) {
1277 Out->Tmp1 |= In->RegA;
1280 Out->Ptr1Lo |= In->RegA;
1283 Out->Ptr1Hi |= In->RegA;
1286 Out->SRegLo |= In->RegA;
1289 Out->SRegHi |= In->RegA;
1293 switch (GetKnownReg (E->Chg & REG_ZP, In)) {
1295 Out->Tmp1 = UNKNOWN_REGVAL;
1298 Out->Ptr1Lo = UNKNOWN_REGVAL;
1301 Out->Ptr1Hi = UNKNOWN_REGVAL;
1304 Out->SRegLo = UNKNOWN_REGVAL;
1307 Out->SRegHi = UNKNOWN_REGVAL;
1315 Out->RegX = UNKNOWN_REGVAL;
1319 Out->RegA = In->RegX;
1326 Out->RegA = In->RegY;
1337 static char* RegInfoDesc (unsigned U, char* Buf)
1338 /* Return a string containing register info */
1342 strcat (Buf, U & REG_SREG_HI? "H" : "_");
1343 strcat (Buf, U & REG_SREG_LO? "L" : "_");
1344 strcat (Buf, U & REG_A? "A" : "_");
1345 strcat (Buf, U & REG_X? "X" : "_");
1346 strcat (Buf, U & REG_Y? "Y" : "_");
1347 strcat (Buf, U & REG_TMP1? "T1" : "__");
1348 strcat (Buf, U & REG_PTR1? "1" : "_");
1349 strcat (Buf, U & REG_PTR2? "2" : "_");
1350 strcat (Buf, U & REG_SAVE? "V" : "_");
1351 strcat (Buf, U & REG_SP? "S" : "_");
1358 static char* RegContentDesc (const RegContents* RC, char* Buf)
1359 /* Return a string containing register contents */
1363 if (RegValIsUnknown (RC->RegA)) {
1364 strcpy (B, "A:XX ");
1366 sprintf (B, "A:%02X ", RC->RegA);
1369 if (RegValIsUnknown (RC->RegX)) {
1370 strcpy (B, "X:XX ");
1372 sprintf (B, "X:%02X ", RC->RegX);
1375 if (RegValIsUnknown (RC->RegY)) {
1378 sprintf (B, "Y:%02X", RC->RegY);
1387 void CE_Output (const CodeEntry* E)
1388 /* Output the code entry to the output file */
1395 /* If we have a label, print that */
1396 unsigned LabelCount = CollCount (&E->Labels);
1398 for (I = 0; I < LabelCount; ++I) {
1399 CL_Output (CollConstAt (&E->Labels, I));
1402 /* Get the opcode description */
1403 D = GetOPCDesc (E->OPC);
1405 /* Print the mnemonic */
1406 Chars = WriteOutput ("\t%s", D->Mnemo);
1408 /* Space to leave before the operand */
1411 /* Print the operand */
1420 Chars += WriteOutput ("%*sa", Space, "");
1425 Chars += WriteOutput ("%*s#%s", Space, "", E->Arg);
1430 /* zeropage and absolute */
1431 Chars += WriteOutput ("%*s%s", Space, "", E->Arg);
1436 /* zeropage,X and absolute,X */
1437 Chars += WriteOutput ("%*s%s,x", Space, "", E->Arg);
1442 Chars += WriteOutput ("%*s%s,y", Space, "", E->Arg);
1447 Chars += WriteOutput ("%*s(%s,x)", Space, "", E->Arg);
1452 Chars += WriteOutput ("%*s(%s),y", Space, "", E->Arg);
1457 Chars += WriteOutput ("%*s(%s)", Space, "", E->Arg);
1462 Target = E->JumpTo? E->JumpTo->Name : E->Arg;
1463 Chars += WriteOutput ("%*s%s", Space, "", Target);
1467 Internal ("Invalid addressing mode");
1471 /* Print usage info if requested by the debugging flag */
1475 WriteOutput ("%*s; USE: %-12s CHG: %-12s SIZE: %u",
1476 (int)(30-Chars), "",
1477 RegInfoDesc (E->Use, Use),
1478 RegInfoDesc (E->Chg, Chg),
1484 WriteOutput (" In %s Out %s",
1485 RegContentDesc (&E->RI->In, RegIn),
1486 RegContentDesc (&E->RI->Out, RegOut));
1490 /* Terminate the line */