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 */
161 /* Be conservative: */
165 Info = GetZPInfo (E->Arg);
166 if (Info && Info->ByteUse != REG_NONE) {
167 if (E->OPC == OP65_ASL || E->OPC == OP65_DEC ||
168 E->OPC == OP65_INC || E->OPC == OP65_LSR ||
169 E->OPC == OP65_ROL || E->OPC == OP65_ROR ||
170 E->OPC == OP65_TRB || E->OPC == OP65_TSB) {
171 /* The zp loc is both, input and output */
172 E->Chg |= Info->ByteUse;
173 E->Use |= Info->ByteUse;
174 } else if ((E->Info & OF_STORE) != 0) {
176 E->Chg |= Info->ByteUse;
179 E->Use |= Info->ByteUse;
187 Info = GetZPInfo (E->Arg);
188 if (Info && Info->ByteUse != REG_NONE) {
189 /* These addressing modes will never change the zp loc */
190 E->Use |= Info->WordUse;
195 /* Keep gcc silent */
203 /*****************************************************************************/
205 /*****************************************************************************/
209 const char* MakeHexArg (unsigned Num)
210 /* Convert Num into a string in the form $XY, suitable for passing it as an
211 * argument to NewCodeEntry, and return a pointer to the string.
212 * BEWARE: The function returns a pointer to a static buffer, so the value is
213 * gone if you call it twice (and apart from that it's not thread and signal
218 xsprintf (Buf, sizeof (Buf), "$%02X", (char) Num);
224 CodeEntry* NewCodeEntry (opc_t OPC, am_t AM, const char* Arg,
225 CodeLabel* JumpTo, LineInfo* LI)
226 /* Create a new code entry, initialize and return it */
228 /* Get the opcode description */
229 const OPCDesc* D = GetOPCDesc (OPC);
231 /* Allocate memory */
232 CodeEntry* E = xmalloc (sizeof (CodeEntry));
234 /* Initialize the fields */
237 E->Arg = GetArgCopy (Arg);
238 E->Flags = NumArg (E->Arg, &E->Num)? CEF_NUMARG : 0;
240 E->Size = GetInsnSize (E->OPC, E->AM);
242 E->LI = UseLineInfo (LI);
244 SetUseChgInfo (E, D);
245 InitCollection (&E->Labels);
247 /* If we have a label given, add this entry to the label */
249 CollAppend (&JumpTo->JumpFrom, E);
252 /* Return the initialized struct */
258 void FreeCodeEntry (CodeEntry* E)
259 /* Free the given code entry */
261 /* Free the string argument if we have one */
264 /* Cleanup the collection */
265 DoneCollection (&E->Labels);
267 /* Release the line info */
268 ReleaseLineInfo (E->LI);
270 /* Delete the register info */
279 void CE_ReplaceOPC (CodeEntry* E, opc_t OPC)
280 /* Replace the opcode of the instruction. This will also replace related info,
281 * Size, Use and Chg, but it will NOT update any arguments or labels.
284 /* Get the opcode descriptor */
285 const OPCDesc* D = GetOPCDesc (OPC);
287 /* Replace the opcode */
290 E->Size = GetInsnSize (E->OPC, E->AM);
291 SetUseChgInfo (E, D);
296 int CodeEntriesAreEqual (const CodeEntry* E1, const CodeEntry* E2)
297 /* Check if both code entries are equal */
299 return E1->OPC == E2->OPC && E1->AM == E2->AM && strcmp (E1->Arg, E2->Arg) == 0;
304 void CE_AttachLabel (CodeEntry* E, CodeLabel* L)
305 /* Attach the label to the entry */
307 /* Add it to the entries label list */
308 CollAppend (&E->Labels, L);
310 /* Tell the label about it's owner */
316 void CE_MoveLabel (CodeLabel* L, CodeEntry* E)
317 /* Move the code label L from it's former owner to the code entry E. */
319 /* Delete the label from the owner */
320 CollDeleteItem (&L->Owner->Labels, L);
322 /* Set the new owner */
323 CollAppend (&E->Labels, L);
329 void CE_SetNumArg (CodeEntry* E, long Num)
330 /* Set a new numeric argument for the given code entry that must already
331 * have a numeric argument.
336 /* Check that the entry has a numerical argument */
337 CHECK (E->Flags & CEF_NUMARG);
339 /* Make the new argument string */
342 xsprintf (Buf, sizeof (Buf), "$%02X", (unsigned) Num);
343 } else if (E->Size == 3) {
345 xsprintf (Buf, sizeof (Buf), "$%04X", (unsigned) Num);
347 Internal ("Invalid instruction size in CE_SetNumArg");
350 /* Free the old argument */
353 /* Assign the new one */
354 E->Arg = GetArgCopy (Buf);
356 /* Use the new numerical value */
362 int CE_KnownImm (const CodeEntry* E)
363 /* Return true if the argument of E is a known immediate value */
365 return (E->AM == AM65_IMM && (E->Flags & CEF_NUMARG) != 0);
370 void CE_FreeRegInfo (CodeEntry* E)
371 /* Free an existing register info struct */
381 void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
382 /* Generate register info for this instruction. If an old info exists, it is
386 /* Pointers to the register contents */
390 /* Function register usage */
391 unsigned short Use, Chg;
393 /* If we don't have a register info struct, allocate one. */
395 E->RI = NewRegInfo (InputRegs);
398 E->RI->In = *InputRegs;
400 RC_Invalidate (&E->RI->In);
402 E->RI->Out2 = E->RI->Out = E->RI->In;
405 /* Get pointers to the register contents */
409 /* Handle the different instructions */
413 /* We don't know the value of the carry, so the result is
421 if (CE_KnownImm (E)) {
422 Out->RegA = In->RegA & (short) E->Num;
423 } else if (E->AM == AM65_ZP) {
424 switch (GetKnownReg (E->Use, In)) {
426 Out->RegA = In->RegA & In->Tmp1;
429 Out->RegA = In->RegA & In->SRegLo;
432 Out->RegA = In->RegA & In->SRegHi;
445 if (E->AM == AM65_ACC && In->RegA >= 0) {
446 Out->RegA = (In->RegA << 1) & 0xFF;
447 } else if (E->AM == AM65_ZP) {
448 switch (GetKnownReg (E->Chg, In)) {
450 Out->Tmp1 = (In->Tmp1 << 1) & 0xFF;
453 Out->SRegLo = (In->SRegLo << 1) & 0xFF;
456 Out->SRegHi = (In->SRegHi << 1) & 0xFF;
459 } else if (E->AM == AM65_ZPX) {
460 /* Invalidates all ZP registers */
461 RC_InvalidateZP (Out);
521 Out->RegA = (In->RegA - 1) & 0xFF;
526 if (E->AM == AM65_ACC && In->RegA >= 0) {
527 Out->RegA = (In->RegA - 1) & 0xFF;
528 } else if (E->AM == AM65_ZP) {
529 switch (GetKnownReg (E->Chg, In)) {
531 Out->Tmp1 = (In->Tmp1 - 1) & 0xFF;
534 Out->SRegLo = (In->SRegLo - 1) & 0xFF;
537 Out->SRegHi = (In->SRegHi - 1) & 0xFF;
540 } else if (E->AM == AM65_ZPX) {
541 /* Invalidates all ZP registers */
542 RC_InvalidateZP (Out);
548 Out->RegX = (In->RegX - 1) & 0xFF;
554 Out->RegY = (In->RegY - 1) & 0xFF;
560 if (CE_KnownImm (E)) {
561 Out->RegA = In->RegA ^ (short) E->Num;
562 } else if (E->AM == AM65_ZP) {
563 switch (GetKnownReg (E->Use, In)) {
565 Out->RegA = In->RegA ^ In->Tmp1;
568 Out->RegA = In->RegA ^ In->SRegLo;
571 Out->RegA = In->RegA ^ In->SRegHi;
585 Out->RegA = (In->RegA + 1) & 0xFF;
590 if (E->AM == AM65_ACC && In->RegA >= 0) {
591 Out->RegA = (In->RegA + 1) & 0xFF;
592 } else if (E->AM == AM65_ZP) {
593 switch (GetKnownReg (E->Chg, In)) {
595 Out->Tmp1 = (In->Tmp1 + 1) & 0xFF;
598 Out->SRegLo = (In->SRegLo + 1) & 0xFF;
601 Out->SRegHi = (In->SRegHi + 1) & 0xFF;
604 } else if (E->AM == AM65_ZPX) {
605 /* Invalidates all ZP registers */
606 RC_InvalidateZP (Out);
612 Out->RegX = (In->RegX + 1) & 0xFF;
618 Out->RegY = (In->RegY + 1) & 0xFF;
644 /* Get the code info for the function */
645 GetFuncInfo (E->Arg, &Use, &Chg);
655 if (Chg & REG_TMP1) {
658 if (Chg & REG_SREG_LO) {
661 if (Chg & REG_SREG_HI) {
673 if (CE_KnownImm (E)) {
674 Out->RegA = (unsigned char) E->Num;
675 } else if (E->AM == AM65_ZP) {
676 switch (GetKnownReg (E->Use, In)) {
678 Out->RegA = In->Tmp1;
681 Out->RegA = In->SRegLo;
684 Out->RegA = In->SRegHi;
691 /* A is now unknown */
697 if (CE_KnownImm (E)) {
698 Out->RegX = (unsigned char) E->Num;
699 } else if (E->AM == AM65_ZP) {
700 switch (GetKnownReg (E->Use, In)) {
702 Out->RegX = In->Tmp1;
705 Out->RegX = In->SRegLo;
708 Out->RegX = In->SRegHi;
715 /* X is now unknown */
721 if (CE_KnownImm (E)) {
722 Out->RegY = (unsigned char) E->Num;
723 } else if (E->AM == AM65_ZP) {
724 switch (GetKnownReg (E->Use, In)) {
726 Out->RegY = In->Tmp1;
729 Out->RegY = In->SRegLo;
732 Out->RegY = In->SRegHi;
739 /* Y is now unknown */
745 if (E->AM == AM65_ACC && In->RegA >= 0) {
746 Out->RegA = (In->RegA >> 1) & 0xFF;
747 } else if (E->AM == AM65_ZP) {
748 switch (GetKnownReg (E->Chg, In)) {
750 Out->Tmp1 = (In->Tmp1 >> 1) & 0xFF;
753 Out->SRegLo = (In->SRegLo >> 1) & 0xFF;
756 Out->SRegHi = (In->SRegHi >> 1) & 0xFF;
759 } else if (E->AM == AM65_ZPX) {
760 /* Invalidates all ZP registers */
761 RC_InvalidateZP (Out);
770 if (CE_KnownImm (E)) {
771 Out->RegA = In->RegA | (short) E->Num;
772 } else if (E->AM == AM65_ZP) {
773 switch (GetKnownReg (E->Use, In)) {
775 Out->RegA = In->RegA | In->Tmp1;
778 Out->RegA = In->RegA | In->SRegLo;
781 Out->RegA = In->RegA | In->SRegHi;
788 /* A is now unknown */
822 /* We don't know the value of the carry bit */
823 if (E->AM == AM65_ACC) {
825 } else if (E->AM == AM65_ZP) {
826 switch (GetKnownReg (E->Chg, In)) {
837 } else if (E->AM == AM65_ZPX) {
838 /* Invalidates all ZP registers */
839 RC_InvalidateZP (Out);
844 /* We don't know the value of the carry bit */
845 if (E->AM == AM65_ACC) {
847 } else if (E->AM == AM65_ZP) {
848 switch (GetKnownReg (E->Chg, In)) {
859 } else if (E->AM == AM65_ZPX) {
860 /* Invalidates all ZP registers */
861 RC_InvalidateZP (Out);
872 /* We don't know the value of the carry bit */
886 if (E->AM == AM65_ZP) {
887 switch (GetKnownReg (E->Chg, 0)) {
889 Out->Tmp1 = In->RegA;
892 Out->SRegLo = In->RegA;
895 Out->SRegHi = In->RegA;
898 } else if (E->AM == AM65_ZPX) {
899 /* Invalidates all ZP registers */
900 RC_InvalidateZP (Out);
905 if (E->AM == AM65_ZP) {
906 switch (GetKnownReg (E->Chg, 0)) {
908 Out->Tmp1 = In->RegX;
911 Out->SRegLo = In->RegX;
914 Out->SRegHi = In->RegX;
917 } else if (E->AM == AM65_ZPX) {
918 /* Invalidates all ZP registers */
919 RC_InvalidateZP (Out);
924 if (E->AM == AM65_ZP) {
925 switch (GetKnownReg (E->Chg, 0)) {
927 Out->Tmp1 = In->RegY;
930 Out->SRegLo = In->RegY;
933 Out->SRegHi = In->RegY;
936 } else if (E->AM == AM65_ZPX) {
937 /* Invalidates all ZP registers */
938 RC_InvalidateZP (Out);
943 if (E->AM == AM65_ZP) {
944 switch (GetKnownReg (E->Chg, 0)) {
955 } else if (E->AM == AM65_ZPX) {
956 /* Invalidates all ZP registers */
957 RC_InvalidateZP (Out);
962 Out->RegX = In->RegA;
966 Out->RegY = In->RegA;
970 if (E->AM == AM65_ZPX) {
971 /* Invalidates all ZP registers */
972 RC_InvalidateZP (Out);
973 } else if (E->AM == AM65_ZP) {
975 switch (GetKnownReg (E->Chg, In)) {
977 Out->Tmp1 &= ~In->RegA;
980 Out->SRegLo &= ~In->RegA;
983 Out->SRegHi &= ~In->RegA;
987 switch (GetKnownReg (E->Chg, In)) {
1003 if (E->AM == AM65_ZPX) {
1004 /* Invalidates all ZP registers */
1005 RC_InvalidateZP (Out);
1006 } else if (E->AM == AM65_ZP) {
1007 if (In->RegA >= 0) {
1008 switch (GetKnownReg (E->Chg, In)) {
1010 Out->Tmp1 |= In->RegA;
1013 Out->SRegLo |= In->RegA;
1016 Out->SRegHi |= In->RegA;
1020 switch (GetKnownReg (E->Chg, In)) {
1040 Out->RegA = In->RegX;
1047 Out->RegA = In->RegY;
1058 static char* RegInfoDesc (unsigned U, char* Buf)
1059 /* Return a string containing register info */
1063 strcat (Buf, U & REG_SREG_HI? "H" : "_");
1064 strcat (Buf, U & REG_SREG_LO? "L" : "_");
1065 strcat (Buf, U & REG_A? "A" : "_");
1066 strcat (Buf, U & REG_X? "X" : "_");
1067 strcat (Buf, U & REG_Y? "Y" : "_");
1068 strcat (Buf, U & REG_TMP1? "T1" : "__");
1069 strcat (Buf, U & REG_PTR1? "1" : "_");
1070 strcat (Buf, U & REG_PTR2? "2" : "_");
1071 strcat (Buf, U & REG_SAVE? "V" : "_");
1078 void CE_Output (const CodeEntry* E, FILE* F)
1079 /* Output the code entry to a file */
1085 /* If we have a label, print that */
1086 unsigned LabelCount = CollCount (&E->Labels);
1088 for (I = 0; I < LabelCount; ++I) {
1089 CL_Output (CollConstAt (&E->Labels, I), F);
1092 /* Get the opcode description */
1093 D = GetOPCDesc (E->OPC);
1095 /* Print the mnemonic */
1096 Chars = fprintf (F, "\t%s", D->Mnemo);
1098 /* Print the operand */
1108 Chars += fprintf (F, "%*sa", 9-Chars, "");
1114 Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
1120 /* zeropage and absolute */
1121 Chars += fprintf (F, "%*s%s", 9-Chars, "", E->Arg);
1126 /* zeropage,X and absolute,X */
1127 Chars += fprintf (F, "%*s%s,x", 9-Chars, "", E->Arg);
1132 Chars += fprintf (F, "%*s%s,y", 9-Chars, "", E->Arg);
1137 Chars += fprintf (F, "%*s(%s,x)", 9-Chars, "", E->Arg);
1142 Chars += fprintf (F, "%*s(%s),y", 9-Chars, "", E->Arg);
1147 Chars += fprintf (F, "%*s(%s)", 9-Chars, "", E->Arg);
1152 Target = E->JumpTo? E->JumpTo->Name : E->Arg;
1153 Chars += fprintf (F, "%*s%s", 9-Chars, "", Target);
1157 Internal ("Invalid addressing mode");
1161 /* Print usage info if requested by the debugging flag */
1166 "%*s; USE: %-20s CHG: %-20s SIZE: %u\n",
1168 RegInfoDesc (E->Use, Use),
1169 RegInfoDesc (E->Chg, Chg),
1172 /* Terminate the line */