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;
474 Out->RegA = In->RegA - 1;
479 if (E->AM == AM65_ACC && In->RegA >= 0) {
480 Out->RegA = In->RegA - 1;
486 Out->RegX = In->RegX - 1;
492 Out->RegY = In->RegY - 1;
498 if (CE_KnownImm (E)) {
499 Out->RegA = In->RegA ^ (short) E->Num;
508 Out->RegA = In->RegA + 1;
513 if (E->AM == AM65_ACC && In->RegA >= 0) {
514 Out->RegA = In->RegA + 1;
520 Out->RegX = In->RegX + 1;
526 Out->RegY = In->RegY + 1;
552 /* Get the code info for the function */
553 GetFuncInfo (E->Arg, &Use, &Chg);
572 if (CE_KnownImm (E)) {
573 Out->RegA = (unsigned char) E->Num;
575 /* A is now unknown */
581 if (CE_KnownImm (E)) {
582 Out->RegX = (unsigned char) E->Num;
584 /* X is now unknown */
590 if (CE_KnownImm (E)) {
591 Out->RegY = (unsigned char) E->Num;
593 /* Y is now unknown */
599 if (E->AM == AM65_ACC && In->RegA >= 0) {
600 Out->RegA = (In->RegA >> 1) & 0xFF;
609 if (CE_KnownImm (E)) {
610 Out->RegA = In->RegA | (short) E->Num;
612 /* A is now unknown */
660 /* We don't know the value of the carry bit */
683 Out->RegX = In->RegA;
687 Out->RegY = In->RegA;
705 Out->RegA = In->RegX;
712 Out->RegA = In->RegY;
723 static char* RegInfoDesc (unsigned U, char* Buf)
724 /* Return a string containing register info */
728 strcat (Buf, U & REG_SREG? "E" : "_");
729 strcat (Buf, U & REG_A? "A" : "_");
730 strcat (Buf, U & REG_X? "X" : "_");
731 strcat (Buf, U & REG_Y? "Y" : "_");
732 strcat (Buf, U & REG_SP? "S" : "_");
733 strcat (Buf, U & REG_TMP1? "T1" : "__");
734 strcat (Buf, U & REG_TMP2? "T2" : "__");
735 strcat (Buf, U & REG_TMP3? "T3" : "__");
736 strcat (Buf, U & REG_TMP4? "T4" : "__");
737 strcat (Buf, U & REG_PTR1? "1" : "_");
738 strcat (Buf, U & REG_PTR2? "2" : "_");
739 strcat (Buf, U & REG_PTR3? "3" : "_");
740 strcat (Buf, U & REG_PTR4? "4" : "_");
741 strcat (Buf, U & REG_SAVE? "V" : "_");
742 strcat (Buf, U & REG_BANK? "B" : "_");
749 void CE_Output (const CodeEntry* E, FILE* F)
750 /* Output the code entry to a file */
756 /* If we have a label, print that */
757 unsigned LabelCount = CollCount (&E->Labels);
759 for (I = 0; I < LabelCount; ++I) {
760 CL_Output (CollConstAt (&E->Labels, I), F);
763 /* Get the opcode description */
764 D = GetOPCDesc (E->OPC);
766 /* Print the mnemonic */
767 Chars = fprintf (F, "\t%s", D->Mnemo);
769 /* Print the operand */
779 Chars += fprintf (F, "%*sa", 9-Chars, "");
785 Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
791 /* zeropage and absolute */
792 Chars += fprintf (F, "%*s%s", 9-Chars, "", E->Arg);
797 /* zeropage,X and absolute,X */
798 Chars += fprintf (F, "%*s%s,x", 9-Chars, "", E->Arg);
803 Chars += fprintf (F, "%*s%s,y", 9-Chars, "", E->Arg);
808 Chars += fprintf (F, "%*s(%s,x)", 9-Chars, "", E->Arg);
813 Chars += fprintf (F, "%*s(%s),y", 9-Chars, "", E->Arg);
818 Chars += fprintf (F, "%*s(%s)", 9-Chars, "", E->Arg);
823 Target = E->JumpTo? E->JumpTo->Name : E->Arg;
824 Chars += fprintf (F, "%*s%s", 9-Chars, "", Target);
828 Internal ("Invalid addressing mode");
832 /* Print usage info if requested by the debugging flag */
837 "%*s; USE: %-19s CHG: %-19s SIZE: %u\n",
839 RegInfoDesc (E->Use, Use),
840 RegInfoDesc (E->Chg, Chg),
843 /* Terminate the line */