1 /*****************************************************************************/
5 /* Code segment entry */
9 /* (C) 2001 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 if (IsZPName (E->Arg, &Use) && Use != REG_NONE) {
166 if (E->OPC == OP65_ASL || E->OPC == OP65_DEC ||
167 E->OPC == OP65_INC || E->OPC == OP65_LSR ||
168 E->OPC == OP65_ROL || E->OPC == OP65_ROR ||
169 E->OPC == OP65_TRB || E->OPC == OP65_TSB) {
170 /* The zp loc is both, input and output */
173 } else if ((E->Info & OF_STORE) != 0) {
186 if (IsZPName (E->Arg, &Use) && Use != REG_NONE) {
187 /* These addressing modes will never change the zp loc */
193 /* Keep gcc silent */
201 /*****************************************************************************/
203 /*****************************************************************************/
207 CodeEntry* NewCodeEntry (opc_t OPC, am_t AM, const char* Arg,
208 CodeLabel* JumpTo, LineInfo* LI)
209 /* Create a new code entry, initialize and return it */
211 /* Get the opcode description */
212 const OPCDesc* D = GetOPCDesc (OPC);
214 /* Allocate memory */
215 CodeEntry* E = xmalloc (sizeof (CodeEntry));
217 /* Initialize the fields */
220 E->Arg = GetArgCopy (Arg);
221 E->Flags = NumArg (E->Arg, &E->Num)? CEF_NUMARG : 0;
223 E->Size = GetInsnSize (E->OPC, E->AM);
225 E->LI = UseLineInfo (LI);
227 SetUseChgInfo (E, D);
228 InitCollection (&E->Labels);
230 /* If we have a label given, add this entry to the label */
232 CollAppend (&JumpTo->JumpFrom, E);
235 /* Return the initialized struct */
241 void FreeCodeEntry (CodeEntry* E)
242 /* Free the given code entry */
244 /* Free the string argument if we have one */
247 /* Cleanup the collection */
248 DoneCollection (&E->Labels);
250 /* Release the line info */
251 ReleaseLineInfo (E->LI);
253 /* Delete the register info */
262 void CE_ReplaceOPC (CodeEntry* E, opc_t OPC)
263 /* Replace the opcode of the instruction. This will also replace related info,
264 * Size, Use and Chg, but it will NOT update any arguments or labels.
267 /* Get the opcode descriptor */
268 const OPCDesc* D = GetOPCDesc (OPC);
270 /* Replace the opcode */
273 E->Size = GetInsnSize (E->OPC, E->AM);
274 SetUseChgInfo (E, D);
279 int CodeEntriesAreEqual (const CodeEntry* E1, const CodeEntry* E2)
280 /* Check if both code entries are equal */
282 return E1->OPC == E2->OPC && E1->AM == E2->AM && strcmp (E1->Arg, E2->Arg) == 0;
287 void CE_AttachLabel (CodeEntry* E, CodeLabel* L)
288 /* Attach the label to the entry */
290 /* Add it to the entries label list */
291 CollAppend (&E->Labels, L);
293 /* Tell the label about it's owner */
299 void CE_MoveLabel (CodeLabel* L, CodeEntry* E)
300 /* Move the code label L from it's former owner to the code entry E. */
302 /* Delete the label from the owner */
303 CollDeleteItem (&L->Owner->Labels, L);
305 /* Set the new owner */
306 CollAppend (&E->Labels, L);
312 void CE_SetNumArg (CodeEntry* E, long Num)
313 /* Set a new numeric argument for the given code entry that must already
314 * have a numeric argument.
319 /* Check that the entry has a numerical argument */
320 CHECK (E->Flags & CEF_NUMARG);
322 /* Make the new argument string */
325 xsprintf (Buf, sizeof (Buf), "$%02X", (unsigned) Num);
326 } else if (E->Size == 3) {
328 xsprintf (Buf, sizeof (Buf), "$%04X", (unsigned) Num);
330 Internal ("Invalid instruction size in CE_SetNumArg");
333 /* Free the old argument */
336 /* Assign the new one */
337 E->Arg = GetArgCopy (Buf);
339 /* Use the new numerical value */
345 int CE_KnownImm (const CodeEntry* E)
346 /* Return true if the argument of E is a known immediate value */
348 return (E->AM == AM65_IMM && (E->Flags & CEF_NUMARG) != 0);
353 void CE_FreeRegInfo (CodeEntry* E)
354 /* Free an existing register info struct */
364 void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
365 /* Generate register info for this instruction. If an old info exists, it is
369 /* Pointers to the register contents */
373 /* Function register usage */
374 unsigned short Use, Chg;
376 /* If we don't have a register info struct, allocate one. */
378 E->RI = NewRegInfo (InputRegs);
381 E->RI->In = *InputRegs;
383 RC_Invalidate (&E->RI->In);
385 E->RI->Out2 = E->RI->Out = E->RI->In;
388 /* Get pointers to the register contents */
392 /* Handle the different instructions */
396 /* We don't know the value of the carry, so the result is
404 if (CE_KnownImm (E)) {
405 Out->RegA = In->RegA & (short) E->Num;
413 if (E->AM == AM65_ACC && In->RegA >= 0) {
414 Out->RegA = (In->RegA << 1) & 0xFF;
415 } else if (E->AM == AM65_ZP) {
416 if ((E->Chg & REG_SREG_LO) != 0 && In->SRegLo >= 0) {
417 Out->SRegLo = (In->SRegLo << 1) & 0xFF;
418 } else if ((E->Chg & REG_SREG_HI) != 0 && In->SRegHi >= 0) {
419 Out->SRegHi = (In->SRegHi << 1) & 0xFF;
480 Out->RegA = (In->RegA - 1) & 0xFF;
485 if (E->AM == AM65_ACC && In->RegA >= 0) {
486 Out->RegA = (In->RegA - 1) & 0xFF;
487 } else if (E->AM == AM65_ZP) {
488 if ((E->Chg & REG_SREG_LO) != 0 && In->SRegLo >= 0) {
489 Out->SRegLo = (In->SRegLo - 1) & 0xFF;
490 } else if ((E->Chg & REG_SREG_HI) != 0 && In->SRegHi >= 0) {
491 Out->SRegHi = (In->SRegHi - 1) & 0xFF;
498 Out->RegX = (In->RegX - 1) & 0xFF;
504 Out->RegY = (In->RegY - 1) & 0xFF;
510 if (CE_KnownImm (E)) {
511 Out->RegA = In->RegA ^ (short) E->Num;
520 Out->RegA = (In->RegA + 1) & 0xFF;
525 if (E->AM == AM65_ACC && In->RegA >= 0) {
526 Out->RegA = (In->RegA + 1) & 0xFF;
527 } else if (E->AM == AM65_ZP) {
528 if ((E->Chg & REG_SREG_LO) != 0 && In->SRegLo >= 0) {
529 Out->SRegLo = (In->SRegLo + 1) & 0xFF;
530 } else if ((E->Chg & REG_SREG_HI) != 0 && In->SRegHi >= 0) {
531 Out->SRegHi = (In->SRegHi + 1) & 0xFF;
538 Out->RegX = (In->RegX + 1) & 0xFF;
544 Out->RegY = (In->RegY + 1) & 0xFF;
570 /* Get the code info for the function */
571 GetFuncInfo (E->Arg, &Use, &Chg);
581 if (Chg & REG_SREG_LO) {
584 if (Chg & REG_SREG_HI) {
596 if (CE_KnownImm (E)) {
597 Out->RegA = (unsigned char) E->Num;
598 } else if (E->AM == AM65_ZP) {
599 if (E->Use & REG_SREG_LO) {
600 Out->RegA = In->SRegLo;
601 } else if (E->Use & REG_SREG_HI) {
602 Out->RegA = In->SRegHi;
607 /* A is now unknown */
613 if (CE_KnownImm (E)) {
614 Out->RegX = (unsigned char) E->Num;
615 } else if (E->AM == AM65_ZP) {
616 if (E->Use & REG_SREG_LO) {
617 Out->RegX = In->SRegLo;
618 } else if (E->Use & REG_SREG_HI) {
619 Out->RegX = In->SRegHi;
624 /* X is now unknown */
630 if (CE_KnownImm (E)) {
631 Out->RegY = (unsigned char) E->Num;
632 } else if (E->AM == AM65_ZP) {
633 if (E->Use & REG_SREG_LO) {
634 Out->RegY = In->SRegLo;
635 } else if (E->Use & REG_SREG_HI) {
636 Out->RegY = In->SRegHi;
641 /* Y is now unknown */
647 if (E->AM == AM65_ACC && In->RegA >= 0) {
648 Out->RegA = (In->RegA >> 1) & 0xFF;
649 } else if (E->AM == AM65_ZP) {
650 if ((E->Chg & REG_SREG_LO) != 0 && In->SRegLo >= 0) {
651 Out->SRegLo = (In->SRegLo >> 1) & 0xFF;
652 } else if (E->Chg & REG_SREG_HI) {
653 Out->SRegHi = (In->SRegHi >> 1) & 0xFF;
663 if (CE_KnownImm (E)) {
664 Out->RegA = In->RegA | (short) E->Num;
666 /* A is now unknown */
700 if (E->AM == AM65_ACC) {
702 } else if (E->AM == AM65_ZP) {
703 if (E->Chg & REG_SREG_LO) {
705 } else if (E->Chg & REG_SREG_HI) {
712 if (E->AM == AM65_ACC) {
714 } else if (E->AM == AM65_ZP) {
715 if (E->Chg & REG_SREG_LO) {
717 } else if (E->Chg & REG_SREG_HI) {
730 /* We don't know the value of the carry bit */
744 if (E->AM == AM65_ZP) {
745 if (E->Chg & REG_SREG_LO) {
746 Out->SRegLo = In->RegA;
747 } else if (E->Chg & REG_SREG_HI) {
748 Out->SRegHi = In->RegA;
754 if (E->AM == AM65_ZP) {
755 if (E->Chg & REG_SREG_LO) {
756 Out->SRegLo = In->RegX;
757 } else if (E->Chg & REG_SREG_HI) {
758 Out->SRegHi = In->RegX;
764 if (E->AM == AM65_ZP) {
765 if (E->Chg & REG_SREG_LO) {
766 Out->SRegLo = In->RegY;
767 } else if (E->Chg & REG_SREG_HI) {
768 Out->SRegHi = In->RegY;
774 Out->RegX = In->RegA;
778 Out->RegY = In->RegA;
796 Out->RegA = In->RegX;
803 Out->RegA = In->RegY;
814 static char* RegInfoDesc (unsigned U, char* Buf)
815 /* Return a string containing register info */
819 strcat (Buf, U & REG_SREG_HI? "H" : "_");
820 strcat (Buf, U & REG_SREG_LO? "L" : "_");
821 strcat (Buf, U & REG_A? "A" : "_");
822 strcat (Buf, U & REG_X? "X" : "_");
823 strcat (Buf, U & REG_Y? "Y" : "_");
824 strcat (Buf, U & REG_SP? "S" : "_");
825 strcat (Buf, U & REG_TMP1? "T1" : "__");
826 strcat (Buf, U & REG_TMP2? "T2" : "__");
827 strcat (Buf, U & REG_TMP3? "T3" : "__");
828 strcat (Buf, U & REG_TMP4? "T4" : "__");
829 strcat (Buf, U & REG_PTR1? "1" : "_");
830 strcat (Buf, U & REG_PTR2? "2" : "_");
831 strcat (Buf, U & REG_PTR3? "3" : "_");
832 strcat (Buf, U & REG_PTR4? "4" : "_");
833 strcat (Buf, U & REG_SAVE? "V" : "_");
834 strcat (Buf, U & REG_BANK? "B" : "_");
841 void CE_Output (const CodeEntry* E, FILE* F)
842 /* Output the code entry to a file */
848 /* If we have a label, print that */
849 unsigned LabelCount = CollCount (&E->Labels);
851 for (I = 0; I < LabelCount; ++I) {
852 CL_Output (CollConstAt (&E->Labels, I), F);
855 /* Get the opcode description */
856 D = GetOPCDesc (E->OPC);
858 /* Print the mnemonic */
859 Chars = fprintf (F, "\t%s", D->Mnemo);
861 /* Print the operand */
871 Chars += fprintf (F, "%*sa", 9-Chars, "");
877 Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
883 /* zeropage and absolute */
884 Chars += fprintf (F, "%*s%s", 9-Chars, "", E->Arg);
889 /* zeropage,X and absolute,X */
890 Chars += fprintf (F, "%*s%s,x", 9-Chars, "", E->Arg);
895 Chars += fprintf (F, "%*s%s,y", 9-Chars, "", E->Arg);
900 Chars += fprintf (F, "%*s(%s,x)", 9-Chars, "", E->Arg);
905 Chars += fprintf (F, "%*s(%s),y", 9-Chars, "", E->Arg);
910 Chars += fprintf (F, "%*s(%s)", 9-Chars, "", E->Arg);
915 Target = E->JumpTo? E->JumpTo->Name : E->Arg;
916 Chars += fprintf (F, "%*s%s", 9-Chars, "", Target);
920 Internal ("Invalid addressing mode");
924 /* Print usage info if requested by the debugging flag */
929 "%*s; USE: %-20s CHG: %-20s SIZE: %u\n",
931 RegInfoDesc (E->Use, Use),
932 RegInfoDesc (E->Chg, Chg),
935 /* Terminate the line */