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 CodeEntry* NewCodeEntry (opc_t OPC, am_t AM, const char* Arg,
210 CodeLabel* JumpTo, LineInfo* LI)
211 /* Create a new code entry, initialize and return it */
213 /* Get the opcode description */
214 const OPCDesc* D = GetOPCDesc (OPC);
216 /* Allocate memory */
217 CodeEntry* E = xmalloc (sizeof (CodeEntry));
219 /* Initialize the fields */
222 E->Arg = GetArgCopy (Arg);
223 E->Flags = NumArg (E->Arg, &E->Num)? CEF_NUMARG : 0;
225 E->Size = GetInsnSize (E->OPC, E->AM);
227 E->LI = UseLineInfo (LI);
229 SetUseChgInfo (E, D);
230 InitCollection (&E->Labels);
232 /* If we have a label given, add this entry to the label */
234 CollAppend (&JumpTo->JumpFrom, E);
237 /* Return the initialized struct */
243 void FreeCodeEntry (CodeEntry* E)
244 /* Free the given code entry */
246 /* Free the string argument if we have one */
249 /* Cleanup the collection */
250 DoneCollection (&E->Labels);
252 /* Release the line info */
253 ReleaseLineInfo (E->LI);
255 /* Delete the register info */
264 void CE_ReplaceOPC (CodeEntry* E, opc_t OPC)
265 /* Replace the opcode of the instruction. This will also replace related info,
266 * Size, Use and Chg, but it will NOT update any arguments or labels.
269 /* Get the opcode descriptor */
270 const OPCDesc* D = GetOPCDesc (OPC);
272 /* Replace the opcode */
275 E->Size = GetInsnSize (E->OPC, E->AM);
276 SetUseChgInfo (E, D);
281 int CodeEntriesAreEqual (const CodeEntry* E1, const CodeEntry* E2)
282 /* Check if both code entries are equal */
284 return E1->OPC == E2->OPC && E1->AM == E2->AM && strcmp (E1->Arg, E2->Arg) == 0;
289 void CE_AttachLabel (CodeEntry* E, CodeLabel* L)
290 /* Attach the label to the entry */
292 /* Add it to the entries label list */
293 CollAppend (&E->Labels, L);
295 /* Tell the label about it's owner */
301 void CE_MoveLabel (CodeLabel* L, CodeEntry* E)
302 /* Move the code label L from it's former owner to the code entry E. */
304 /* Delete the label from the owner */
305 CollDeleteItem (&L->Owner->Labels, L);
307 /* Set the new owner */
308 CollAppend (&E->Labels, L);
314 void CE_SetNumArg (CodeEntry* E, long Num)
315 /* Set a new numeric argument for the given code entry that must already
316 * have a numeric argument.
321 /* Check that the entry has a numerical argument */
322 CHECK (E->Flags & CEF_NUMARG);
324 /* Make the new argument string */
327 xsprintf (Buf, sizeof (Buf), "$%02X", (unsigned) Num);
328 } else if (E->Size == 3) {
330 xsprintf (Buf, sizeof (Buf), "$%04X", (unsigned) Num);
332 Internal ("Invalid instruction size in CE_SetNumArg");
335 /* Free the old argument */
338 /* Assign the new one */
339 E->Arg = GetArgCopy (Buf);
341 /* Use the new numerical value */
347 int CE_KnownImm (const CodeEntry* E)
348 /* Return true if the argument of E is a known immediate value */
350 return (E->AM == AM65_IMM && (E->Flags & CEF_NUMARG) != 0);
355 void CE_FreeRegInfo (CodeEntry* E)
356 /* Free an existing register info struct */
366 void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
367 /* Generate register info for this instruction. If an old info exists, it is
371 /* Pointers to the register contents */
375 /* Function register usage */
376 unsigned short Use, Chg;
378 /* If we don't have a register info struct, allocate one. */
380 E->RI = NewRegInfo (InputRegs);
383 E->RI->In = *InputRegs;
385 RC_Invalidate (&E->RI->In);
387 E->RI->Out2 = E->RI->Out = E->RI->In;
390 /* Get pointers to the register contents */
394 /* Handle the different instructions */
398 /* We don't know the value of the carry, so the result is
406 if (CE_KnownImm (E)) {
407 Out->RegA = In->RegA & (short) E->Num;
415 if (E->AM == AM65_ACC && In->RegA >= 0) {
416 Out->RegA = (In->RegA << 1) & 0xFF;
417 } else if (E->AM == AM65_ZP) {
418 if ((E->Chg & REG_SREG_LO) != 0 && In->SRegLo >= 0) {
419 Out->SRegLo = (In->SRegLo << 1) & 0xFF;
420 } else if ((E->Chg & REG_SREG_HI) != 0 && In->SRegHi >= 0) {
421 Out->SRegHi = (In->SRegHi << 1) & 0xFF;
423 } else if (E->AM == AM65_ZPX) {
424 /* Invalidates all ZP registers */
425 RC_InvalidateZP (Out);
485 Out->RegA = (In->RegA - 1) & 0xFF;
490 if (E->AM == AM65_ACC && In->RegA >= 0) {
491 Out->RegA = (In->RegA - 1) & 0xFF;
492 } else if (E->AM == AM65_ZP) {
493 if ((E->Chg & REG_SREG_LO) != 0 && In->SRegLo >= 0) {
494 Out->SRegLo = (In->SRegLo - 1) & 0xFF;
495 } else if ((E->Chg & REG_SREG_HI) != 0 && In->SRegHi >= 0) {
496 Out->SRegHi = (In->SRegHi - 1) & 0xFF;
498 } else if (E->AM == AM65_ZPX) {
499 /* Invalidates all ZP registers */
500 RC_InvalidateZP (Out);
506 Out->RegX = (In->RegX - 1) & 0xFF;
512 Out->RegY = (In->RegY - 1) & 0xFF;
518 if (CE_KnownImm (E)) {
519 Out->RegA = In->RegA ^ (short) E->Num;
528 Out->RegA = (In->RegA + 1) & 0xFF;
533 if (E->AM == AM65_ACC && In->RegA >= 0) {
534 Out->RegA = (In->RegA + 1) & 0xFF;
535 } else if (E->AM == AM65_ZP) {
536 if ((E->Chg & REG_SREG_LO) != 0 && In->SRegLo >= 0) {
537 Out->SRegLo = (In->SRegLo + 1) & 0xFF;
538 } else if ((E->Chg & REG_SREG_HI) != 0 && In->SRegHi >= 0) {
539 Out->SRegHi = (In->SRegHi + 1) & 0xFF;
541 } else if (E->AM == AM65_ZPX) {
542 /* Invalidates all ZP registers */
543 RC_InvalidateZP (Out);
549 Out->RegX = (In->RegX + 1) & 0xFF;
555 Out->RegY = (In->RegY + 1) & 0xFF;
581 /* Get the code info for the function */
582 GetFuncInfo (E->Arg, &Use, &Chg);
592 if (Chg & REG_SREG_LO) {
595 if (Chg & REG_SREG_HI) {
607 if (CE_KnownImm (E)) {
608 Out->RegA = (unsigned char) E->Num;
609 } else if (E->AM == AM65_ZP) {
610 if (E->Use & REG_SREG_LO) {
611 Out->RegA = In->SRegLo;
612 } else if (E->Use & REG_SREG_HI) {
613 Out->RegA = In->SRegHi;
618 /* A is now unknown */
624 if (CE_KnownImm (E)) {
625 Out->RegX = (unsigned char) E->Num;
626 } else if (E->AM == AM65_ZP) {
627 if (E->Use & REG_SREG_LO) {
628 Out->RegX = In->SRegLo;
629 } else if (E->Use & REG_SREG_HI) {
630 Out->RegX = In->SRegHi;
635 /* X is now unknown */
641 if (CE_KnownImm (E)) {
642 Out->RegY = (unsigned char) E->Num;
643 } else if (E->AM == AM65_ZP) {
644 if (E->Use & REG_SREG_LO) {
645 Out->RegY = In->SRegLo;
646 } else if (E->Use & REG_SREG_HI) {
647 Out->RegY = In->SRegHi;
652 /* Y is now unknown */
658 if (E->AM == AM65_ACC && In->RegA >= 0) {
659 Out->RegA = (In->RegA >> 1) & 0xFF;
660 } else if (E->AM == AM65_ZP) {
661 if ((E->Chg & REG_SREG_LO) != 0 && In->SRegLo >= 0) {
662 Out->SRegLo = (In->SRegLo >> 1) & 0xFF;
663 } else if (E->Chg & REG_SREG_HI) {
664 Out->SRegHi = (In->SRegHi >> 1) & 0xFF;
666 } else if (E->AM == AM65_ZPX) {
667 /* Invalidates all ZP registers */
668 RC_InvalidateZP (Out);
677 if (CE_KnownImm (E)) {
678 Out->RegA = In->RegA | (short) E->Num;
680 /* A is now unknown */
714 if (E->AM == AM65_ACC) {
716 } else if (E->AM == AM65_ZP) {
717 if (E->Chg & REG_SREG_LO) {
719 } else if (E->Chg & REG_SREG_HI) {
722 } else if (E->AM == AM65_ZPX) {
723 /* Invalidates all ZP registers */
724 RC_InvalidateZP (Out);
729 if (E->AM == AM65_ACC) {
731 } else if (E->AM == AM65_ZP) {
732 if (E->Chg & REG_SREG_LO) {
734 } else if (E->Chg & REG_SREG_HI) {
737 } else if (E->AM == AM65_ZPX) {
738 /* Invalidates all ZP registers */
739 RC_InvalidateZP (Out);
750 /* We don't know the value of the carry bit */
764 if (E->AM == AM65_ZP) {
765 if (E->Chg & REG_SREG_LO) {
766 Out->SRegLo = In->RegA;
767 } else if (E->Chg & REG_SREG_HI) {
768 Out->SRegHi = In->RegA;
770 } else if (E->AM == AM65_ZPX) {
771 /* Invalidates all ZP registers */
772 RC_InvalidateZP (Out);
777 if (E->AM == AM65_ZP) {
778 if (E->Chg & REG_SREG_LO) {
779 Out->SRegLo = In->RegX;
780 } else if (E->Chg & REG_SREG_HI) {
781 Out->SRegHi = In->RegX;
783 } else if (E->AM == AM65_ZPX) {
784 /* Invalidates all ZP registers */
785 RC_InvalidateZP (Out);
790 if (E->AM == AM65_ZP) {
791 if (E->Chg & REG_SREG_LO) {
792 Out->SRegLo = In->RegY;
793 } else if (E->Chg & REG_SREG_HI) {
794 Out->SRegHi = In->RegY;
796 } else if (E->AM == AM65_ZPX) {
797 /* Invalidates all ZP registers */
798 RC_InvalidateZP (Out);
803 if (E->AM == AM65_ZP) {
804 if (E->Chg & REG_SREG_LO) {
806 } else if (E->Chg & REG_SREG_HI) {
809 } else if (E->AM == AM65_ZPX) {
810 /* Invalidates all ZP registers */
811 RC_InvalidateZP (Out);
816 Out->RegX = In->RegA;
820 Out->RegY = In->RegA;
838 Out->RegA = In->RegX;
845 Out->RegA = In->RegY;
856 static char* RegInfoDesc (unsigned U, char* Buf)
857 /* Return a string containing register info */
861 strcat (Buf, U & REG_SREG_HI? "H" : "_");
862 strcat (Buf, U & REG_SREG_LO? "L" : "_");
863 strcat (Buf, U & REG_A? "A" : "_");
864 strcat (Buf, U & REG_X? "X" : "_");
865 strcat (Buf, U & REG_Y? "Y" : "_");
866 strcat (Buf, U & REG_TMP1? "T1" : "__");
867 strcat (Buf, U & REG_PTR1? "1" : "_");
868 strcat (Buf, U & REG_PTR2? "2" : "_");
869 strcat (Buf, U & REG_SAVE? "V" : "_");
876 void CE_Output (const CodeEntry* E, FILE* F)
877 /* Output the code entry to a file */
883 /* If we have a label, print that */
884 unsigned LabelCount = CollCount (&E->Labels);
886 for (I = 0; I < LabelCount; ++I) {
887 CL_Output (CollConstAt (&E->Labels, I), F);
890 /* Get the opcode description */
891 D = GetOPCDesc (E->OPC);
893 /* Print the mnemonic */
894 Chars = fprintf (F, "\t%s", D->Mnemo);
896 /* Print the operand */
906 Chars += fprintf (F, "%*sa", 9-Chars, "");
912 Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
918 /* zeropage and absolute */
919 Chars += fprintf (F, "%*s%s", 9-Chars, "", E->Arg);
924 /* zeropage,X and absolute,X */
925 Chars += fprintf (F, "%*s%s,x", 9-Chars, "", E->Arg);
930 Chars += fprintf (F, "%*s%s,y", 9-Chars, "", E->Arg);
935 Chars += fprintf (F, "%*s(%s,x)", 9-Chars, "", E->Arg);
940 Chars += fprintf (F, "%*s(%s),y", 9-Chars, "", E->Arg);
945 Chars += fprintf (F, "%*s(%s)", 9-Chars, "", E->Arg);
950 Target = E->JumpTo? E->JumpTo->Name : E->Arg;
951 Chars += fprintf (F, "%*s%s", 9-Chars, "", Target);
955 Internal ("Invalid addressing mode");
959 /* Print usage info if requested by the debugging flag */
964 "%*s; USE: %-20s CHG: %-20s SIZE: %u\n",
966 RegInfoDesc (E->Use, Use),
967 RegInfoDesc (E->Chg, Chg),
970 /* Terminate the line */