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_INC || E->OPC == OP65_DEC) {
169 } else if ((E->Info & OF_STORE) != 0) {
180 if (IsZPName (E->Arg, &Use) && Use != REG_NONE) {
181 /* These addressing modes will never change the zp loc */
187 /* Keep gcc silent */
195 /*****************************************************************************/
197 /*****************************************************************************/
201 CodeEntry* NewCodeEntry (opc_t OPC, am_t AM, const char* Arg,
202 CodeLabel* JumpTo, LineInfo* LI)
203 /* Create a new code entry, initialize and return it */
205 /* Get the opcode description */
206 const OPCDesc* D = GetOPCDesc (OPC);
208 /* Allocate memory */
209 CodeEntry* E = xmalloc (sizeof (CodeEntry));
211 /* Initialize the fields */
214 E->Arg = GetArgCopy (Arg);
215 E->Flags = NumArg (E->Arg, &E->Num)? CEF_NUMARG : 0;
217 E->Size = GetInsnSize (E->OPC, E->AM);
219 E->LI = UseLineInfo (LI);
221 SetUseChgInfo (E, D);
222 InitCollection (&E->Labels);
224 /* If we have a label given, add this entry to the label */
226 CollAppend (&JumpTo->JumpFrom, E);
229 /* Return the initialized struct */
235 void FreeCodeEntry (CodeEntry* E)
236 /* Free the given code entry */
238 /* Free the string argument if we have one */
241 /* Cleanup the collection */
242 DoneCollection (&E->Labels);
244 /* Release the line info */
245 ReleaseLineInfo (E->LI);
247 /* Delete the register info */
256 void CE_ReplaceOPC (CodeEntry* E, opc_t OPC)
257 /* Replace the opcode of the instruction. This will also replace related info,
258 * Size, Use and Chg, but it will NOT update any arguments or labels.
261 /* Get the opcode descriptor */
262 const OPCDesc* D = GetOPCDesc (OPC);
264 /* Replace the opcode */
267 E->Size = GetInsnSize (E->OPC, E->AM);
268 SetUseChgInfo (E, D);
273 int CodeEntriesAreEqual (const CodeEntry* E1, const CodeEntry* E2)
274 /* Check if both code entries are equal */
276 return E1->OPC == E2->OPC && E1->AM == E2->AM && strcmp (E1->Arg, E2->Arg) == 0;
281 void CE_AttachLabel (CodeEntry* E, CodeLabel* L)
282 /* Attach the label to the entry */
284 /* Add it to the entries label list */
285 CollAppend (&E->Labels, L);
287 /* Tell the label about it's owner */
293 void CE_MoveLabel (CodeLabel* L, CodeEntry* E)
294 /* Move the code label L from it's former owner to the code entry E. */
296 /* Delete the label from the owner */
297 CollDeleteItem (&L->Owner->Labels, L);
299 /* Set the new owner */
300 CollAppend (&E->Labels, L);
306 void CE_SetNumArg (CodeEntry* E, long Num)
307 /* Set a new numeric argument for the given code entry that must already
308 * have a numeric argument.
313 /* Check that the entry has a numerical argument */
314 CHECK (E->Flags & CEF_NUMARG);
316 /* Make the new argument string */
319 xsprintf (Buf, sizeof (Buf), "$%02X", (unsigned) Num);
320 } else if (E->Size == 3) {
322 xsprintf (Buf, sizeof (Buf), "$%04X", (unsigned) Num);
324 Internal ("Invalid instruction size in CE_SetNumArg");
327 /* Free the old argument */
330 /* Assign the new one */
331 E->Arg = GetArgCopy (Buf);
333 /* Use the new numerical value */
339 int CE_KnownImm (const CodeEntry* E)
340 /* Return true if the argument of E is a known immediate value */
342 return (E->AM == AM65_IMM && (E->Flags & CEF_NUMARG) != 0);
347 void CE_FreeRegInfo (CodeEntry* E)
348 /* Free an existing register info struct */
358 void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
359 /* Generate register info for this instruction. If an old info exists, it is
363 /* Pointers to the register contents */
367 /* Function register usage */
368 unsigned short Use, Chg;
370 /* If we don't have a register info struct, allocate one. */
372 E->RI = NewRegInfo (InputRegs);
375 E->RI->In = *InputRegs;
377 RC_Invalidate (&E->RI->In);
379 E->RI->Out2 = E->RI->Out = E->RI->In;
382 /* Get pointers to the register contents */
386 /* Handle the different instructions */
390 /* We don't know the value of the carry, so the result is
398 if (CE_KnownImm (E)) {
399 Out->RegA = In->RegA & (short) E->Num;
407 if (E->AM == AM65_ACC && In->RegA >= 0) {
408 Out->RegA = (In->RegA << 1) & 0xFF;
468 Out->RegA = In->RegA - 1;
473 if (E->AM == AM65_ACC && In->RegA >= 0) {
474 Out->RegA = In->RegA - 1;
480 Out->RegX = In->RegX - 1;
486 Out->RegY = In->RegY - 1;
492 if (CE_KnownImm (E)) {
493 Out->RegA = In->RegA ^ (short) E->Num;
502 Out->RegA = In->RegA + 1;
507 if (E->AM == AM65_ACC && In->RegA >= 0) {
508 Out->RegA = In->RegA + 1;
514 Out->RegX = In->RegX + 1;
520 Out->RegY = In->RegY + 1;
546 /* Get the code info for the function */
547 GetFuncInfo (E->Arg, &Use, &Chg);
566 if (CE_KnownImm (E)) {
567 Out->RegA = (unsigned char) E->Num;
569 /* A is now unknown */
575 if (CE_KnownImm (E)) {
576 Out->RegX = (unsigned char) E->Num;
578 /* X is now unknown */
584 if (CE_KnownImm (E)) {
585 Out->RegY = (unsigned char) E->Num;
587 /* Y is now unknown */
593 if (E->AM == AM65_ACC && In->RegA >= 0) {
594 Out->RegA = (In->RegA >> 1) & 0xFF;
603 if (CE_KnownImm (E)) {
604 Out->RegA = In->RegA | (short) E->Num;
606 /* A is now unknown */
654 /* We don't know the value of the carry bit */
677 Out->RegX = In->RegA;
681 Out->RegY = In->RegA;
699 Out->RegA = In->RegX;
706 Out->RegA = In->RegY;
717 static char* RegInfoDesc (unsigned U, char* Buf)
718 /* Return a string containing register info */
759 void CE_Output (const CodeEntry* E, FILE* F)
760 /* Output the code entry to a file */
766 /* If we have a label, print that */
767 unsigned LabelCount = CollCount (&E->Labels);
769 for (I = 0; I < LabelCount; ++I) {
770 CL_Output (CollConstAt (&E->Labels, I), F);
773 /* Get the opcode description */
774 D = GetOPCDesc (E->OPC);
776 /* Print the mnemonic */
777 Chars = fprintf (F, "\t%s", D->Mnemo);
779 /* Print the operand */
789 Chars += fprintf (F, "%*sa", 9-Chars, "");
795 Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
801 /* zeropage and absolute */
802 Chars += fprintf (F, "%*s%s", 9-Chars, "", E->Arg);
807 /* zeropage,X and absolute,X */
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,x)", 9-Chars, "", E->Arg);
823 Chars += fprintf (F, "%*s(%s),y", 9-Chars, "", E->Arg);
828 Chars += fprintf (F, "%*s(%s)", 9-Chars, "", E->Arg);
833 Target = E->JumpTo? E->JumpTo->Name : E->Arg;
834 Chars += fprintf (F, "%*s%s", 9-Chars, "", Target);
838 Internal ("Invalid addressing mode");
842 /* Print usage info if requested by the debugging flag */
847 "%*s; USE: %-18s CHG: %-18s SIZE: %u\n",
849 RegInfoDesc (E->Use, Use),
850 RegInfoDesc (E->Chg, Chg),
853 /* Terminate the line */