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", (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 void CE_FreeRegInfo (CodeEntry* E)
380 /* Free an existing register info struct */
390 void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
391 /* Generate register info for this instruction. If an old info exists, it is
395 /* Pointers to the register contents */
399 /* Function register usage */
400 unsigned short Use, Chg;
402 /* If we don't have a register info struct, allocate one. */
404 E->RI = NewRegInfo (InputRegs);
407 E->RI->In = *InputRegs;
409 RC_Invalidate (&E->RI->In);
411 E->RI->Out2 = E->RI->Out = E->RI->In;
414 /* Get pointers to the register contents */
418 /* Handle the different instructions */
422 /* We don't know the value of the carry, so the result is
430 if (CE_KnownImm (E)) {
431 Out->RegA = In->RegA & (short) E->Num;
432 } else if (E->AM == AM65_ZP) {
433 switch (GetKnownReg (E->Use, In)) {
435 Out->RegA = In->RegA & In->Tmp1;
438 Out->RegA = In->RegA & In->SRegLo;
441 Out->RegA = In->RegA & In->SRegHi;
454 if (E->AM == AM65_ACC && In->RegA >= 0) {
455 Out->RegA = (In->RegA << 1) & 0xFF;
456 } else if (E->AM == AM65_ZP) {
457 switch (GetKnownReg (E->Chg, In)) {
459 Out->Tmp1 = (In->Tmp1 << 1) & 0xFF;
462 Out->SRegLo = (In->SRegLo << 1) & 0xFF;
465 Out->SRegHi = (In->SRegHi << 1) & 0xFF;
468 } else if (E->AM == AM65_ZPX) {
469 /* Invalidates all ZP registers */
470 RC_InvalidateZP (Out);
530 Out->RegA = (In->RegA - 1) & 0xFF;
535 if (E->AM == AM65_ACC && In->RegA >= 0) {
536 Out->RegA = (In->RegA - 1) & 0xFF;
537 } else if (E->AM == AM65_ZP) {
538 switch (GetKnownReg (E->Chg, In)) {
540 Out->Tmp1 = (In->Tmp1 - 1) & 0xFF;
543 Out->SRegLo = (In->SRegLo - 1) & 0xFF;
546 Out->SRegHi = (In->SRegHi - 1) & 0xFF;
549 } else if (E->AM == AM65_ZPX) {
550 /* Invalidates all ZP registers */
551 RC_InvalidateZP (Out);
557 Out->RegX = (In->RegX - 1) & 0xFF;
563 Out->RegY = (In->RegY - 1) & 0xFF;
569 if (CE_KnownImm (E)) {
570 Out->RegA = In->RegA ^ (short) E->Num;
571 } else if (E->AM == AM65_ZP) {
572 switch (GetKnownReg (E->Use, In)) {
574 Out->RegA = In->RegA ^ In->Tmp1;
577 Out->RegA = In->RegA ^ In->SRegLo;
580 Out->RegA = In->RegA ^ In->SRegHi;
594 Out->RegA = (In->RegA + 1) & 0xFF;
599 if (E->AM == AM65_ACC && In->RegA >= 0) {
600 Out->RegA = (In->RegA + 1) & 0xFF;
601 } else if (E->AM == AM65_ZP) {
602 switch (GetKnownReg (E->Chg, In)) {
604 Out->Tmp1 = (In->Tmp1 + 1) & 0xFF;
607 Out->SRegLo = (In->SRegLo + 1) & 0xFF;
610 Out->SRegHi = (In->SRegHi + 1) & 0xFF;
613 } else if (E->AM == AM65_ZPX) {
614 /* Invalidates all ZP registers */
615 RC_InvalidateZP (Out);
621 Out->RegX = (In->RegX + 1) & 0xFF;
627 Out->RegY = (In->RegY + 1) & 0xFF;
653 /* Get the code info for the function */
654 GetFuncInfo (E->Arg, &Use, &Chg);
664 if (Chg & REG_TMP1) {
667 if (Chg & REG_SREG_LO) {
670 if (Chg & REG_SREG_HI) {
673 /* Quick hack for some known functions: */
674 if (strcmp (E->Arg, "tosandax") == 0) {
681 } else if (strcmp (E->Arg, "tosorax") == 0) {
682 if (In->RegA == 0xFF) {
685 if (In->RegX == 0xFF) {
698 if (CE_KnownImm (E)) {
699 Out->RegA = (unsigned char) E->Num;
700 } else if (E->AM == AM65_ZP) {
701 switch (GetKnownReg (E->Use, In)) {
703 Out->RegA = In->Tmp1;
706 Out->RegA = In->SRegLo;
709 Out->RegA = In->SRegHi;
716 /* A is now unknown */
722 if (CE_KnownImm (E)) {
723 Out->RegX = (unsigned char) E->Num;
724 } else if (E->AM == AM65_ZP) {
725 switch (GetKnownReg (E->Use, In)) {
727 Out->RegX = In->Tmp1;
730 Out->RegX = In->SRegLo;
733 Out->RegX = In->SRegHi;
740 /* X is now unknown */
746 if (CE_KnownImm (E)) {
747 Out->RegY = (unsigned char) E->Num;
748 } else if (E->AM == AM65_ZP) {
749 switch (GetKnownReg (E->Use, In)) {
751 Out->RegY = In->Tmp1;
754 Out->RegY = In->SRegLo;
757 Out->RegY = In->SRegHi;
764 /* Y is now unknown */
770 if (E->AM == AM65_ACC && In->RegA >= 0) {
771 Out->RegA = (In->RegA >> 1) & 0xFF;
772 } else if (E->AM == AM65_ZP) {
773 switch (GetKnownReg (E->Chg, In)) {
775 Out->Tmp1 = (In->Tmp1 >> 1) & 0xFF;
778 Out->SRegLo = (In->SRegLo >> 1) & 0xFF;
781 Out->SRegHi = (In->SRegHi >> 1) & 0xFF;
784 } else if (E->AM == AM65_ZPX) {
785 /* Invalidates all ZP registers */
786 RC_InvalidateZP (Out);
795 if (CE_KnownImm (E)) {
796 Out->RegA = In->RegA | (short) E->Num;
797 } else if (E->AM == AM65_ZP) {
798 switch (GetKnownReg (E->Use, In)) {
800 Out->RegA = In->RegA | In->Tmp1;
803 Out->RegA = In->RegA | In->SRegLo;
806 Out->RegA = In->RegA | In->SRegHi;
813 /* A is now unknown */
847 /* We don't know the value of the carry bit */
848 if (E->AM == AM65_ACC) {
850 } else if (E->AM == AM65_ZP) {
851 switch (GetKnownReg (E->Chg, In)) {
862 } else if (E->AM == AM65_ZPX) {
863 /* Invalidates all ZP registers */
864 RC_InvalidateZP (Out);
869 /* We don't know the value of the carry bit */
870 if (E->AM == AM65_ACC) {
872 } else if (E->AM == AM65_ZP) {
873 switch (GetKnownReg (E->Chg, In)) {
884 } else if (E->AM == AM65_ZPX) {
885 /* Invalidates all ZP registers */
886 RC_InvalidateZP (Out);
897 /* We don't know the value of the carry bit */
911 if (E->AM == AM65_ZP) {
912 switch (GetKnownReg (E->Chg, 0)) {
914 Out->Tmp1 = In->RegA;
917 Out->SRegLo = In->RegA;
920 Out->SRegHi = In->RegA;
923 } else if (E->AM == AM65_ZPX) {
924 /* Invalidates all ZP registers */
925 RC_InvalidateZP (Out);
930 if (E->AM == AM65_ZP) {
931 switch (GetKnownReg (E->Chg, 0)) {
933 Out->Tmp1 = In->RegX;
936 Out->SRegLo = In->RegX;
939 Out->SRegHi = In->RegX;
942 } else if (E->AM == AM65_ZPX) {
943 /* Invalidates all ZP registers */
944 RC_InvalidateZP (Out);
949 if (E->AM == AM65_ZP) {
950 switch (GetKnownReg (E->Chg, 0)) {
952 Out->Tmp1 = In->RegY;
955 Out->SRegLo = In->RegY;
958 Out->SRegHi = In->RegY;
961 } else if (E->AM == AM65_ZPX) {
962 /* Invalidates all ZP registers */
963 RC_InvalidateZP (Out);
968 if (E->AM == AM65_ZP) {
969 switch (GetKnownReg (E->Chg, 0)) {
980 } else if (E->AM == AM65_ZPX) {
981 /* Invalidates all ZP registers */
982 RC_InvalidateZP (Out);
987 Out->RegX = In->RegA;
991 Out->RegY = In->RegA;
995 if (E->AM == AM65_ZPX) {
996 /* Invalidates all ZP registers */
997 RC_InvalidateZP (Out);
998 } else if (E->AM == AM65_ZP) {
1000 switch (GetKnownReg (E->Chg, In)) {
1002 Out->Tmp1 &= ~In->RegA;
1005 Out->SRegLo &= ~In->RegA;
1008 Out->SRegHi &= ~In->RegA;
1012 switch (GetKnownReg (E->Chg, In)) {
1028 if (E->AM == AM65_ZPX) {
1029 /* Invalidates all ZP registers */
1030 RC_InvalidateZP (Out);
1031 } else if (E->AM == AM65_ZP) {
1032 if (In->RegA >= 0) {
1033 switch (GetKnownReg (E->Chg, In)) {
1035 Out->Tmp1 |= In->RegA;
1038 Out->SRegLo |= In->RegA;
1041 Out->SRegHi |= In->RegA;
1045 switch (GetKnownReg (E->Chg, In)) {
1065 Out->RegA = In->RegX;
1072 Out->RegA = In->RegY;
1083 static char* RegInfoDesc (unsigned U, char* Buf)
1084 /* Return a string containing register info */
1088 strcat (Buf, U & REG_SREG_HI? "H" : "_");
1089 strcat (Buf, U & REG_SREG_LO? "L" : "_");
1090 strcat (Buf, U & REG_A? "A" : "_");
1091 strcat (Buf, U & REG_X? "X" : "_");
1092 strcat (Buf, U & REG_Y? "Y" : "_");
1093 strcat (Buf, U & REG_TMP1? "T1" : "__");
1094 strcat (Buf, U & REG_PTR1? "1" : "_");
1095 strcat (Buf, U & REG_PTR2? "2" : "_");
1096 strcat (Buf, U & REG_SAVE? "V" : "_");
1103 void CE_Output (const CodeEntry* E, FILE* F)
1104 /* Output the code entry to a file */
1110 /* If we have a label, print that */
1111 unsigned LabelCount = CollCount (&E->Labels);
1113 for (I = 0; I < LabelCount; ++I) {
1114 CL_Output (CollConstAt (&E->Labels, I), F);
1117 /* Get the opcode description */
1118 D = GetOPCDesc (E->OPC);
1120 /* Print the mnemonic */
1121 Chars = fprintf (F, "\t%s", D->Mnemo);
1123 /* Print the operand */
1133 Chars += fprintf (F, "%*sa", 9-Chars, "");
1139 Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
1145 /* zeropage and absolute */
1146 Chars += fprintf (F, "%*s%s", 9-Chars, "", E->Arg);
1151 /* zeropage,X and absolute,X */
1152 Chars += fprintf (F, "%*s%s,x", 9-Chars, "", E->Arg);
1157 Chars += fprintf (F, "%*s%s,y", 9-Chars, "", E->Arg);
1162 Chars += fprintf (F, "%*s(%s,x)", 9-Chars, "", E->Arg);
1167 Chars += fprintf (F, "%*s(%s),y", 9-Chars, "", E->Arg);
1172 Chars += fprintf (F, "%*s(%s)", 9-Chars, "", E->Arg);
1177 Target = E->JumpTo? E->JumpTo->Name : E->Arg;
1178 Chars += fprintf (F, "%*s%s", 9-Chars, "", Target);
1182 Internal ("Invalid addressing mode");
1186 /* Print usage info if requested by the debugging flag */
1191 "%*s; USE: %-20s CHG: %-20s SIZE: %u\n",
1193 RegInfoDesc (E->Use, Use),
1194 RegInfoDesc (E->Chg, Chg),
1197 /* Terminate the line */