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 /*****************************************************************************/
50 /*****************************************************************************/
52 /*****************************************************************************/
56 /*****************************************************************************/
58 /*****************************************************************************/
62 CodeEntry* NewCodeEntry (const OPCDesc* D, am_t AM, const char* Arg, CodeLabel* JumpTo)
63 /* Create a new code entry, initialize and return it */
66 CodeEntry* E = xmalloc (sizeof (CodeEntry));
68 /* Initialize the fields */
71 E->Size = GetInsnSize (E->OPC, E->AM);
73 E->Arg = (Arg && Arg[0] != '\0')? xstrdup (Arg) : 0;
79 if (E->OPC == OPC_JSR && E->Arg) {
80 /* A subroutine call */
81 GetFuncInfo (E->Arg, &E->Use, &E->Chg);
83 /* Some other instruction */
84 E->Use |= GetAMUseInfo (AM);
87 InitCollection (&E->Labels);
89 /* If we have a label given, add this entry to the label */
91 CollAppend (&JumpTo->JumpFrom, E);
94 /* Return the initialized struct */
100 void FreeCodeEntry (CodeEntry* E)
101 /* Free the given code entry */
103 /* Free the string argument if we have one */
106 /* Cleanup the collection */
107 DoneCollection (&E->Labels);
115 int CodeEntryHasLabel (const CodeEntry* E)
116 /* Check if the given code entry has labels attached */
118 return (CollCount (&E->Labels) > 0);
123 int CodeEntryHasMark (const CodeEntry* E)
124 /* Return true if the given code entry has the CEF_USERMARK flag set */
126 return (E->Flags & CEF_USERMARK) != 0;
131 void CodeEntrySetMark (CodeEntry* E)
132 /* Set the CEF_USERMARK flag for the given entry */
134 E->Flags |= CEF_USERMARK;
139 void CodeEntryResetMark (CodeEntry* E)
140 /* Reset the CEF_USERMARK flag for the given entry */
142 E->Flags &= ~CEF_USERMARK;
147 CodeLabel* GetCodeLabel (CodeEntry* E, unsigned Index)
148 /* Get a label from this code entry */
150 return CollAt (&E->Labels, Index);
155 void MoveCodeLabel (CodeLabel* L, CodeEntry* E)
156 /* Move the code label L from it's former owner to the code entry E. */
158 /* Delete the label from the owner */
159 CollDeleteItem (&L->Owner->Labels, L);
161 /* Set the new owner */
162 CollAppend (&E->Labels, L);
168 void OutputCodeEntry (const CodeEntry* E, FILE* F)
169 /* Output the code entry to a file */
174 /* If we have a label, print that */
175 unsigned LabelCount = CollCount (&E->Labels);
177 for (I = 0; I < LabelCount; ++I) {
178 OutputCodeLabel (CollConstAt (&E->Labels, I), F);
181 /* Get the opcode description */
182 D = GetOPCDesc (E->OPC);
184 /* Print the mnemonic */
185 Chars = fprintf (F, "\t%s", D->Mnemo);
187 /* Print the operand */
196 Chars += fprintf (F, "%*sa", 9-Chars, "");
201 Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
206 /* zeropage and absolute */
207 Chars += fprintf (F, "%*s%s", 9-Chars, "", E->Arg);
212 /* zeropage,X and absolute,X */
213 Chars += fprintf (F, "%*s%s,x", 9-Chars, "", E->Arg);
218 Chars += fprintf (F, "%*s%s,y", 9-Chars, "", E->Arg);
223 Chars += fprintf (F, "%*s(%s,x)", 9-Chars, "", E->Arg);
228 Chars += fprintf (F, "%*s(%s),y", 9-Chars, "", E->Arg);
233 Chars += fprintf (F, "%*s(%s)", 9-Chars, "", E->Arg);
238 CHECK (E->JumpTo != 0);
239 Chars += fprintf (F, "%*s%s", 9-Chars, "", E->JumpTo->Name);
243 Internal ("Invalid addressing mode");
247 /* Print usage info if requested by the debugging flag */
250 "%*s; USE: %c%c%c CHG: %c%c%c",
252 (E->Use & REG_A)? 'A' : '_',
253 (E->Use & REG_X)? 'X' : '_',
254 (E->Use & REG_Y)? 'Y' : '_',
255 (E->Chg & REG_A)? 'A' : '_',
256 (E->Chg & REG_X)? 'X' : '_',
257 (E->Chg & REG_Y)? 'Y' : '_');
260 /* Terminate the line */