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 /*****************************************************************************/
54 /*****************************************************************************/
56 /*****************************************************************************/
61 static char EmptyArg[] = "";
65 /*****************************************************************************/
66 /* Helper functions */
67 /*****************************************************************************/
71 static void FreeArg (char* Arg)
72 /* Free a code entry argument */
74 if (Arg != EmptyArg) {
81 static char* GetArgCopy (const char* Arg)
82 /* Create an argument copy for assignment */
84 if (Arg && Arg[0] != '\0') {
88 /* Use the empty argument string */
95 static int NumArg (const char* Arg, unsigned long* Num)
96 /* If the given argument is numerical, convert it and return true. Otherwise
97 * set Num to zero and return false.
103 /* Determine the base */
108 } else if (*Arg == '%') {
113 /* Convert the value. strtol is not exactly what we want here, but it's
114 * cheap and may be replaced by something fancier later.
116 Val = strtoul (Arg, &End, Base);
118 /* Check if the conversion was successful */
121 /* Could not convert */
136 static void SetUseChgInfo (CodeEntry* E, const OPCDesc* D)
137 /* Set the Use and Chg in E */
139 /* If this is a subroutine call, or a jump to an external function,
140 * lookup the information about this function and use it. The jump itself
141 * does not change any registers, so we don't need to use the data from D.
143 if ((E->Info & (OF_BRA | OF_CALL)) != 0 && E->JumpTo == 0) {
144 /* A subroutine call or jump to external symbol (function exit) */
145 GetFuncInfo (E->Arg, &E->Use, &E->Chg);
147 /* Some other instruction. Use the values from the opcode description
148 * plus addressing mode info
150 E->Use = D->Use | GetAMUseInfo (E->AM);
157 /*****************************************************************************/
159 /*****************************************************************************/
163 CodeEntry* NewCodeEntry (opc_t OPC, am_t AM, const char* Arg,
164 CodeLabel* JumpTo, LineInfo* LI)
165 /* Create a new code entry, initialize and return it */
167 /* Get the opcode description */
168 const OPCDesc* D = GetOPCDesc (OPC);
170 /* Allocate memory */
171 CodeEntry* E = xmalloc (sizeof (CodeEntry));
173 /* Initialize the fields */
176 E->Arg = GetArgCopy (Arg);
177 E->Flags = NumArg (E->Arg, &E->Num)? CEF_NUMARG : 0;
179 E->Size = GetInsnSize (E->OPC, E->AM);
181 E->LI = UseLineInfo (LI);
183 SetUseChgInfo (E, D);
184 InitCollection (&E->Labels);
186 /* If we have a label given, add this entry to the label */
188 CollAppend (&JumpTo->JumpFrom, E);
191 /* Return the initialized struct */
197 void FreeCodeEntry (CodeEntry* E)
198 /* Free the given code entry */
200 /* Free the string argument if we have one */
203 /* Cleanup the collection */
204 DoneCollection (&E->Labels);
206 /* Release the line info */
207 ReleaseLineInfo (E->LI);
209 /* Delete the register info */
218 void CE_ReplaceOPC (CodeEntry* E, opc_t OPC)
219 /* Replace the opcode of the instruction. This will also replace related info,
220 * Size, Use and Chg, but it will NOT update any arguments or labels.
223 /* Get the opcode descriptor */
224 const OPCDesc* D = GetOPCDesc (OPC);
226 /* Replace the opcode */
229 E->Size = GetInsnSize (E->OPC, E->AM);
230 SetUseChgInfo (E, D);
235 int CodeEntriesAreEqual (const CodeEntry* E1, const CodeEntry* E2)
236 /* Check if both code entries are equal */
238 return E1->OPC == E2->OPC && E1->AM == E2->AM && strcmp (E1->Arg, E2->Arg) == 0;
243 void CE_AttachLabel (CodeEntry* E, CodeLabel* L)
244 /* Attach the label to the entry */
246 /* Add it to the entries label list */
247 CollAppend (&E->Labels, L);
249 /* Tell the label about it's owner */
255 void CE_MoveLabel (CodeLabel* L, CodeEntry* E)
256 /* Move the code label L from it's former owner to the code entry E. */
258 /* Delete the label from the owner */
259 CollDeleteItem (&L->Owner->Labels, L);
261 /* Set the new owner */
262 CollAppend (&E->Labels, L);
268 void CE_SetArg (CodeEntry* E, const char* Arg)
269 /* Set a new argument for the given code entry. An old string is deleted. */
271 /* Free the old argument */
274 /* Assign the new one */
275 E->Arg = GetArgCopy (Arg);
280 int CE_KnownImm (const CodeEntry* E)
281 /* Return true if the argument of E is a known immediate value */
283 return (E->AM == AM65_IMM && (E->Flags & CEF_NUMARG) != 0);
288 void CE_FreeRegInfo (CodeEntry* E)
289 /* Free an existing register info struct */
299 void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
300 /* Generate register info for this instruction. If an old info exists, it is
304 /* Pointers to the register contents */
308 /* Function register usage */
309 unsigned short Use, Chg;
311 /* If we don't have a register info struct, allocate one. */
313 E->RI = NewRegInfo (InputRegs);
316 E->RI->In = *InputRegs;
318 RC_Invalidate (&E->RI->In);
320 E->RI->Out2 = E->RI->Out = E->RI->In;
323 /* Get pointers to the register contents */
327 /* Handle the different instructions */
331 /* We don't know the value of the carry, so the result is
339 if (CE_KnownImm (E)) {
340 Out->RegA = In->RegA & (short) E->Num;
348 if (E->AM == AM65_ACC && In->RegA >= 0) {
349 Out->RegA = (In->RegA << 1) & 0xFF;
409 Out->RegA = In->RegA - 1;
414 if (E->AM == AM65_ACC && In->RegA >= 0) {
415 Out->RegA = In->RegA - 1;
421 Out->RegX = In->RegX - 1;
427 Out->RegY = In->RegY - 1;
433 if (CE_KnownImm (E)) {
434 Out->RegA = In->RegA ^ (short) E->Num;
443 Out->RegA = In->RegA + 1;
448 if (E->AM == AM65_ACC && In->RegA >= 0) {
449 Out->RegA = In->RegA + 1;
455 Out->RegX = In->RegX + 1;
461 Out->RegY = In->RegY + 1;
487 /* Get the code info for the function */
488 GetFuncInfo (E->Arg, &Use, &Chg);
507 if (CE_KnownImm (E)) {
508 Out->RegA = (unsigned char) E->Num;
510 /* A is now unknown */
516 if (CE_KnownImm (E)) {
517 Out->RegX = (unsigned char) E->Num;
519 /* X is now unknown */
525 if (CE_KnownImm (E)) {
526 Out->RegY = (unsigned char) E->Num;
528 /* Y is now unknown */
534 if (E->AM == AM65_ACC && In->RegA >= 0) {
535 Out->RegA = (In->RegA >> 1) & 0xFF;
544 if (CE_KnownImm (E)) {
545 Out->RegA = In->RegA | (short) E->Num;
547 /* A is now unknown */
595 /* We don't know the value of the carry bit */
618 Out->RegX = In->RegA;
622 Out->RegY = In->RegA;
640 Out->RegA = In->RegX;
647 Out->RegA = In->RegY;
658 void CE_Output (const CodeEntry* E, FILE* F)
659 /* Output the code entry to a file */
665 /* If we have a label, print that */
666 unsigned LabelCount = CollCount (&E->Labels);
668 for (I = 0; I < LabelCount; ++I) {
669 CL_Output (CollConstAt (&E->Labels, I), F);
672 /* Get the opcode description */
673 D = GetOPCDesc (E->OPC);
675 /* Print the mnemonic */
676 Chars = fprintf (F, "\t%s", D->Mnemo);
678 /* Print the operand */
688 Chars += fprintf (F, "%*sa", 9-Chars, "");
694 Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
700 /* zeropage and absolute */
701 Chars += fprintf (F, "%*s%s", 9-Chars, "", E->Arg);
706 /* zeropage,X and absolute,X */
707 Chars += fprintf (F, "%*s%s,x", 9-Chars, "", E->Arg);
712 Chars += fprintf (F, "%*s%s,y", 9-Chars, "", E->Arg);
717 Chars += fprintf (F, "%*s(%s,x)", 9-Chars, "", E->Arg);
722 Chars += fprintf (F, "%*s(%s),y", 9-Chars, "", E->Arg);
727 Chars += fprintf (F, "%*s(%s)", 9-Chars, "", E->Arg);
732 Target = E->JumpTo? E->JumpTo->Name : E->Arg;
733 Chars += fprintf (F, "%*s%s", 9-Chars, "", Target);
737 Internal ("Invalid addressing mode");
741 /* Print usage info if requested by the debugging flag */
744 "%*s; USE: %c%c%c CHG: %c%c%c SIZE: %u\n",
746 (E->Use & REG_A)? 'A' : '_',
747 (E->Use & REG_X)? 'X' : '_',
748 (E->Use & REG_Y)? 'Y' : '_',
749 (E->Chg & REG_A)? 'A' : '_',
750 (E->Chg & REG_X)? 'X' : '_',
751 (E->Chg & REG_Y)? 'Y' : '_',
754 /* Terminate the line */