1 /*****************************************************************************/
5 /* Code segment entry */
9 /* (C) 2001-2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
55 /*****************************************************************************/
57 /*****************************************************************************/
62 static char EmptyArg[] = "";
66 /*****************************************************************************/
67 /* Helper functions */
68 /*****************************************************************************/
72 static void FreeArg (char* Arg)
73 /* Free a code entry argument */
75 if (Arg != EmptyArg) {
82 static char* GetArgCopy (const char* Arg)
83 /* Create an argument copy for assignment */
85 if (Arg && Arg[0] != '\0') {
89 /* Use the empty argument string */
96 static int NumArg (const char* Arg, unsigned long* Num)
97 /* If the given argument is numerical, convert it and return true. Otherwise
98 * set Num to zero and return false.
104 /* Determine the base */
109 } else if (*Arg == '%') {
114 /* Convert the value. strtol is not exactly what we want here, but it's
115 * cheap and may be replaced by something fancier later.
117 Val = strtoul (Arg, &End, Base);
119 /* Check if the conversion was successful */
122 /* Could not convert */
137 static void SetUseChgInfo (CodeEntry* E, const OPCDesc* D)
138 /* Set the Use and Chg in E */
142 /* If this is a subroutine call, or a jump to an external function,
143 * lookup the information about this function and use it. The jump itself
144 * does not change any registers, so we don't need to use the data from D.
146 if ((E->Info & (OF_BRA | OF_CALL)) != 0 && E->JumpTo == 0) {
147 /* A subroutine call or jump to external symbol (function exit) */
148 GetFuncInfo (E->Arg, &E->Use, &E->Chg);
150 /* Some other instruction. Use the values from the opcode description
151 * plus addressing mode info.
153 E->Use = D->Use | GetAMUseInfo (E->AM);
156 /* Check for special zero page registers used */
160 if (E->OPC == OP65_ASL || E->OPC == OP65_DEC ||
161 E->OPC == OP65_INC || E->OPC == OP65_LSR ||
162 E->OPC == OP65_ROL || E->OPC == OP65_ROR) {
163 /* A is changed by these insns */
170 /* Be conservative: */
174 Info = GetZPInfo (E->Arg);
175 if (Info && Info->ByteUse != REG_NONE) {
176 if (E->OPC == OP65_ASL || E->OPC == OP65_DEC ||
177 E->OPC == OP65_INC || E->OPC == OP65_LSR ||
178 E->OPC == OP65_ROL || E->OPC == OP65_ROR ||
179 E->OPC == OP65_TRB || E->OPC == OP65_TSB) {
180 /* The zp loc is both, input and output */
181 E->Chg |= Info->ByteUse;
182 E->Use |= Info->ByteUse;
183 } else if ((E->Info & OF_STORE) != 0) {
185 E->Chg |= Info->ByteUse;
188 E->Use |= Info->ByteUse;
196 Info = GetZPInfo (E->Arg);
197 if (Info && Info->ByteUse != REG_NONE) {
198 /* These addressing modes will never change the zp loc */
199 E->Use |= Info->WordUse;
204 /* Keep gcc silent */
212 /*****************************************************************************/
214 /*****************************************************************************/
218 const char* MakeHexArg (unsigned Num)
219 /* Convert Num into a string in the form $XY, suitable for passing it as an
220 * argument to NewCodeEntry, and return a pointer to the string.
221 * BEWARE: The function returns a pointer to a static buffer, so the value is
222 * gone if you call it twice (and apart from that it's not thread and signal
227 xsprintf (Buf, sizeof (Buf), "$%02X", (unsigned char) Num);
233 CodeEntry* NewCodeEntry (opc_t OPC, am_t AM, const char* Arg,
234 CodeLabel* JumpTo, LineInfo* LI)
235 /* Create a new code entry, initialize and return it */
237 /* Get the opcode description */
238 const OPCDesc* D = GetOPCDesc (OPC);
240 /* Allocate memory */
241 CodeEntry* E = xmalloc (sizeof (CodeEntry));
243 /* Initialize the fields */
246 E->Arg = GetArgCopy (Arg);
247 E->Flags = NumArg (E->Arg, &E->Num)? CEF_NUMARG : 0;
249 E->Size = GetInsnSize (E->OPC, E->AM);
251 E->LI = UseLineInfo (LI);
253 SetUseChgInfo (E, D);
254 InitCollection (&E->Labels);
256 /* If we have a label given, add this entry to the label */
258 CollAppend (&JumpTo->JumpFrom, E);
261 /* Return the initialized struct */
267 void FreeCodeEntry (CodeEntry* E)
268 /* Free the given code entry */
270 /* Free the string argument if we have one */
273 /* Cleanup the collection */
274 DoneCollection (&E->Labels);
276 /* Release the line info */
277 ReleaseLineInfo (E->LI);
279 /* Delete the register info */
288 void CE_ReplaceOPC (CodeEntry* E, opc_t OPC)
289 /* Replace the opcode of the instruction. This will also replace related info,
290 * Size, Use and Chg, but it will NOT update any arguments or labels.
293 /* Get the opcode descriptor */
294 const OPCDesc* D = GetOPCDesc (OPC);
296 /* Replace the opcode */
299 E->Size = GetInsnSize (E->OPC, E->AM);
300 SetUseChgInfo (E, D);
305 int CodeEntriesAreEqual (const CodeEntry* E1, const CodeEntry* E2)
306 /* Check if both code entries are equal */
308 return E1->OPC == E2->OPC && E1->AM == E2->AM && strcmp (E1->Arg, E2->Arg) == 0;
313 void CE_AttachLabel (CodeEntry* E, CodeLabel* L)
314 /* Attach the label to the entry */
316 /* Add it to the entries label list */
317 CollAppend (&E->Labels, L);
319 /* Tell the label about it's owner */
325 void CE_MoveLabel (CodeLabel* L, CodeEntry* E)
326 /* Move the code label L from it's former owner to the code entry E. */
328 /* Delete the label from the owner */
329 CollDeleteItem (&L->Owner->Labels, L);
331 /* Set the new owner */
332 CollAppend (&E->Labels, L);
338 void CE_SetNumArg (CodeEntry* E, long Num)
339 /* Set a new numeric argument for the given code entry that must already
340 * have a numeric argument.
345 /* Check that the entry has a numerical argument */
346 CHECK (E->Flags & CEF_NUMARG);
348 /* Make the new argument string */
351 xsprintf (Buf, sizeof (Buf), "$%02X", (unsigned) Num);
352 } else if (E->Size == 3) {
354 xsprintf (Buf, sizeof (Buf), "$%04X", (unsigned) Num);
356 Internal ("Invalid instruction size in CE_SetNumArg");
359 /* Free the old argument */
362 /* Assign the new one */
363 E->Arg = GetArgCopy (Buf);
365 /* Use the new numerical value */
371 int CE_KnownImm (const CodeEntry* E)
372 /* Return true if the argument of E is a known immediate value */
374 return (E->AM == AM65_IMM && (E->Flags & CEF_NUMARG) != 0);
379 int CE_UseLoadFlags (const CodeEntry* E)
380 /* Return true if the instruction uses any flags that are set by a load of
381 * a register (N and Z).
384 /* A branch will use the flags */
385 if (E->Info & OF_FBRA) {
389 /* Call of a boolean transformer routine will also use the flags */
390 if (E->OPC == OP65_JSR) {
391 /* Get the condition that is evaluated and check it */
392 switch (FindBoolCmpCond (E->Arg)) {
401 /* Will use the N or Z flags */
405 case CMP_UGE: /* Uses only carry */
406 case CMP_ULT: /* Dito */
407 default: /* No bool transformer subroutine */
418 void CE_FreeRegInfo (CodeEntry* E)
419 /* Free an existing register info struct */
429 void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
430 /* Generate register info for this instruction. If an old info exists, it is
434 /* Pointers to the register contents */
438 /* Function register usage */
439 unsigned short Use, Chg;
441 /* If we don't have a register info struct, allocate one. */
443 E->RI = NewRegInfo (InputRegs);
446 E->RI->In = *InputRegs;
448 RC_Invalidate (&E->RI->In);
450 E->RI->Out2 = E->RI->Out = E->RI->In;
453 /* Get pointers to the register contents */
457 /* Handle the different instructions */
461 /* We don't know the value of the carry, so the result is
469 if (CE_KnownImm (E)) {
470 Out->RegA = In->RegA & (short) E->Num;
471 } else if (E->AM == AM65_ZP) {
472 switch (GetKnownReg (E->Use, In)) {
474 Out->RegA = In->RegA & In->Tmp1;
477 Out->RegA = In->RegA & In->SRegLo;
480 Out->RegA = In->RegA & In->SRegHi;
493 if (E->AM == AM65_ACC && In->RegA >= 0) {
494 Out->RegA = (In->RegA << 1) & 0xFF;
495 } else if (E->AM == AM65_ZP) {
496 switch (GetKnownReg (E->Chg, In)) {
498 Out->Tmp1 = (In->Tmp1 << 1) & 0xFF;
501 Out->SRegLo = (In->SRegLo << 1) & 0xFF;
504 Out->SRegHi = (In->SRegHi << 1) & 0xFF;
507 } else if (E->AM == AM65_ZPX) {
508 /* Invalidates all ZP registers */
509 RC_InvalidateZP (Out);
569 Out->RegA = (In->RegA - 1) & 0xFF;
574 if (E->AM == AM65_ACC && In->RegA >= 0) {
575 Out->RegA = (In->RegA - 1) & 0xFF;
576 } else if (E->AM == AM65_ZP) {
577 switch (GetKnownReg (E->Chg, In)) {
579 Out->Tmp1 = (In->Tmp1 - 1) & 0xFF;
582 Out->SRegLo = (In->SRegLo - 1) & 0xFF;
585 Out->SRegHi = (In->SRegHi - 1) & 0xFF;
588 } else if (E->AM == AM65_ZPX) {
589 /* Invalidates all ZP registers */
590 RC_InvalidateZP (Out);
596 Out->RegX = (In->RegX - 1) & 0xFF;
602 Out->RegY = (In->RegY - 1) & 0xFF;
608 if (CE_KnownImm (E)) {
609 Out->RegA = In->RegA ^ (short) E->Num;
610 } else if (E->AM == AM65_ZP) {
611 switch (GetKnownReg (E->Use, In)) {
613 Out->RegA = In->RegA ^ In->Tmp1;
616 Out->RegA = In->RegA ^ In->SRegLo;
619 Out->RegA = In->RegA ^ In->SRegHi;
633 Out->RegA = (In->RegA + 1) & 0xFF;
638 if (E->AM == AM65_ACC && In->RegA >= 0) {
639 Out->RegA = (In->RegA + 1) & 0xFF;
640 } else if (E->AM == AM65_ZP) {
641 switch (GetKnownReg (E->Chg, In)) {
643 Out->Tmp1 = (In->Tmp1 + 1) & 0xFF;
646 Out->SRegLo = (In->SRegLo + 1) & 0xFF;
649 Out->SRegHi = (In->SRegHi + 1) & 0xFF;
652 } else if (E->AM == AM65_ZPX) {
653 /* Invalidates all ZP registers */
654 RC_InvalidateZP (Out);
660 Out->RegX = (In->RegX + 1) & 0xFF;
666 Out->RegY = (In->RegY + 1) & 0xFF;
692 /* Get the code info for the function */
693 GetFuncInfo (E->Arg, &Use, &Chg);
703 if (Chg & REG_TMP1) {
706 if (Chg & REG_SREG_LO) {
709 if (Chg & REG_SREG_HI) {
712 /* ## FIXME: Quick hack for some known functions: */
713 if (strcmp (E->Arg, "tosandax") == 0) {
720 } else if (strcmp (E->Arg, "tosorax") == 0) {
721 if (In->RegA == 0xFF) {
724 if (In->RegX == 0xFF) {
727 } else if (FindBoolCmpCond (E->Arg) != CMP_INV) {
739 if (CE_KnownImm (E)) {
740 Out->RegA = (unsigned char) E->Num;
741 } else if (E->AM == AM65_ZP) {
742 switch (GetKnownReg (E->Use, In)) {
744 Out->RegA = In->Tmp1;
747 Out->RegA = In->SRegLo;
750 Out->RegA = In->SRegHi;
757 /* A is now unknown */
763 if (CE_KnownImm (E)) {
764 Out->RegX = (unsigned char) E->Num;
765 } else if (E->AM == AM65_ZP) {
766 switch (GetKnownReg (E->Use, In)) {
768 Out->RegX = In->Tmp1;
771 Out->RegX = In->SRegLo;
774 Out->RegX = In->SRegHi;
781 /* X is now unknown */
787 if (CE_KnownImm (E)) {
788 Out->RegY = (unsigned char) E->Num;
789 } else if (E->AM == AM65_ZP) {
790 switch (GetKnownReg (E->Use, In)) {
792 Out->RegY = In->Tmp1;
795 Out->RegY = In->SRegLo;
798 Out->RegY = In->SRegHi;
805 /* Y is now unknown */
811 if (E->AM == AM65_ACC && In->RegA >= 0) {
812 Out->RegA = (In->RegA >> 1) & 0xFF;
813 } else if (E->AM == AM65_ZP) {
814 switch (GetKnownReg (E->Chg, In)) {
816 Out->Tmp1 = (In->Tmp1 >> 1) & 0xFF;
819 Out->SRegLo = (In->SRegLo >> 1) & 0xFF;
822 Out->SRegHi = (In->SRegHi >> 1) & 0xFF;
825 } else if (E->AM == AM65_ZPX) {
826 /* Invalidates all ZP registers */
827 RC_InvalidateZP (Out);
836 if (CE_KnownImm (E)) {
837 Out->RegA = In->RegA | (short) E->Num;
838 } else if (E->AM == AM65_ZP) {
839 switch (GetKnownReg (E->Use, In)) {
841 Out->RegA = In->RegA | In->Tmp1;
844 Out->RegA = In->RegA | In->SRegLo;
847 Out->RegA = In->RegA | In->SRegHi;
854 /* A is now unknown */
888 /* We don't know the value of the carry bit */
889 if (E->AM == AM65_ACC) {
891 } else if (E->AM == AM65_ZP) {
892 switch (GetKnownReg (E->Chg, In)) {
903 } else if (E->AM == AM65_ZPX) {
904 /* Invalidates all ZP registers */
905 RC_InvalidateZP (Out);
910 /* We don't know the value of the carry bit */
911 if (E->AM == AM65_ACC) {
913 } else if (E->AM == AM65_ZP) {
914 switch (GetKnownReg (E->Chg, In)) {
925 } else if (E->AM == AM65_ZPX) {
926 /* Invalidates all ZP registers */
927 RC_InvalidateZP (Out);
938 /* We don't know the value of the carry bit */
952 if (E->AM == AM65_ZP) {
953 switch (GetKnownReg (E->Chg, 0)) {
955 Out->Tmp1 = In->RegA;
958 Out->SRegLo = In->RegA;
961 Out->SRegHi = In->RegA;
964 } else if (E->AM == AM65_ZPX) {
965 /* Invalidates all ZP registers */
966 RC_InvalidateZP (Out);
971 if (E->AM == AM65_ZP) {
972 switch (GetKnownReg (E->Chg, 0)) {
974 Out->Tmp1 = In->RegX;
977 Out->SRegLo = In->RegX;
980 Out->SRegHi = In->RegX;
983 } else if (E->AM == AM65_ZPX) {
984 /* Invalidates all ZP registers */
985 RC_InvalidateZP (Out);
990 if (E->AM == AM65_ZP) {
991 switch (GetKnownReg (E->Chg, 0)) {
993 Out->Tmp1 = In->RegY;
996 Out->SRegLo = In->RegY;
999 Out->SRegHi = In->RegY;
1002 } else if (E->AM == AM65_ZPX) {
1003 /* Invalidates all ZP registers */
1004 RC_InvalidateZP (Out);
1009 if (E->AM == AM65_ZP) {
1010 switch (GetKnownReg (E->Chg, 0)) {
1021 } else if (E->AM == AM65_ZPX) {
1022 /* Invalidates all ZP registers */
1023 RC_InvalidateZP (Out);
1028 Out->RegX = In->RegA;
1032 Out->RegY = In->RegA;
1036 if (E->AM == AM65_ZPX) {
1037 /* Invalidates all ZP registers */
1038 RC_InvalidateZP (Out);
1039 } else if (E->AM == AM65_ZP) {
1040 if (In->RegA >= 0) {
1041 switch (GetKnownReg (E->Chg, In)) {
1043 Out->Tmp1 &= ~In->RegA;
1046 Out->SRegLo &= ~In->RegA;
1049 Out->SRegHi &= ~In->RegA;
1053 switch (GetKnownReg (E->Chg, In)) {
1069 if (E->AM == AM65_ZPX) {
1070 /* Invalidates all ZP registers */
1071 RC_InvalidateZP (Out);
1072 } else if (E->AM == AM65_ZP) {
1073 if (In->RegA >= 0) {
1074 switch (GetKnownReg (E->Chg, In)) {
1076 Out->Tmp1 |= In->RegA;
1079 Out->SRegLo |= In->RegA;
1082 Out->SRegHi |= In->RegA;
1086 switch (GetKnownReg (E->Chg, In)) {
1106 Out->RegA = In->RegX;
1113 Out->RegA = In->RegY;
1124 static char* RegInfoDesc (unsigned U, char* Buf)
1125 /* Return a string containing register info */
1129 strcat (Buf, U & REG_SREG_HI? "H" : "_");
1130 strcat (Buf, U & REG_SREG_LO? "L" : "_");
1131 strcat (Buf, U & REG_A? "A" : "_");
1132 strcat (Buf, U & REG_X? "X" : "_");
1133 strcat (Buf, U & REG_Y? "Y" : "_");
1134 strcat (Buf, U & REG_TMP1? "T1" : "__");
1135 strcat (Buf, U & REG_PTR1? "1" : "_");
1136 strcat (Buf, U & REG_PTR2? "2" : "_");
1137 strcat (Buf, U & REG_SAVE? "V" : "_");
1144 void CE_Output (const CodeEntry* E, FILE* F)
1145 /* Output the code entry to a file */
1151 /* If we have a label, print that */
1152 unsigned LabelCount = CollCount (&E->Labels);
1154 for (I = 0; I < LabelCount; ++I) {
1155 CL_Output (CollConstAt (&E->Labels, I), F);
1158 /* Get the opcode description */
1159 D = GetOPCDesc (E->OPC);
1161 /* Print the mnemonic */
1162 Chars = fprintf (F, "\t%s", D->Mnemo);
1164 /* Print the operand */
1174 Chars += fprintf (F, "%*sa", 9-Chars, "");
1180 Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
1186 /* zeropage and absolute */
1187 Chars += fprintf (F, "%*s%s", 9-Chars, "", E->Arg);
1192 /* zeropage,X and absolute,X */
1193 Chars += fprintf (F, "%*s%s,x", 9-Chars, "", E->Arg);
1198 Chars += fprintf (F, "%*s%s,y", 9-Chars, "", E->Arg);
1203 Chars += fprintf (F, "%*s(%s,x)", 9-Chars, "", E->Arg);
1208 Chars += fprintf (F, "%*s(%s),y", 9-Chars, "", E->Arg);
1213 Chars += fprintf (F, "%*s(%s)", 9-Chars, "", E->Arg);
1218 Target = E->JumpTo? E->JumpTo->Name : E->Arg;
1219 Chars += fprintf (F, "%*s%s", 9-Chars, "", Target);
1223 Internal ("Invalid addressing mode");
1227 /* Print usage info if requested by the debugging flag */
1232 "%*s; USE: %-20s CHG: %-20s SIZE: %u\n",
1234 RegInfoDesc (E->Use, Use),
1235 RegInfoDesc (E->Chg, Chg),
1238 /* Terminate the line */