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 /*****************************************************************************/
52 /*****************************************************************************/
54 /*****************************************************************************/
59 static char EmptyArg[] = "";
63 /*****************************************************************************/
64 /* Helper functions */
65 /*****************************************************************************/
69 static void FreeArg (char* Arg)
70 /* Free a code entry argument */
72 if (Arg != EmptyArg) {
79 static char* GetArgCopy (const char* Arg)
80 /* Create an argument copy for assignment */
82 if (Arg && Arg[0] != '\0') {
86 /* Use the empty argument string */
93 /*****************************************************************************/
95 /*****************************************************************************/
99 CodeEntry* NewCodeEntry (const OPCDesc* D, am_t AM, const char* Arg, CodeLabel* JumpTo)
100 /* Create a new code entry, initialize and return it */
102 /* Allocate memory */
103 CodeEntry* E = xmalloc (sizeof (CodeEntry));
105 /* Initialize the fields */
108 E->Size = GetInsnSize (E->OPC, E->AM);
110 E->Arg = GetArgCopy (Arg);
116 if (E->OPC == OPC_JSR) {
117 /* A subroutine call */
118 GetFuncInfo (E->Arg, &E->Use, &E->Chg);
120 /* Some other instruction */
121 E->Use |= GetAMUseInfo (AM);
124 InitCollection (&E->Labels);
126 /* If we have a label given, add this entry to the label */
128 CollAppend (&JumpTo->JumpFrom, E);
131 /* Return the initialized struct */
137 void FreeCodeEntry (CodeEntry* E)
138 /* Free the given code entry */
140 /* Free the string argument if we have one */
143 /* Cleanup the collection */
144 DoneCollection (&E->Labels);
152 int CodeEntriesAreEqual (const CodeEntry* E1, const CodeEntry* E2)
153 /* Check if both code entries are equal */
155 return E1->OPC == E2->OPC && E1->AM == E2->AM && strcmp (E1->Arg, E2->Arg) == 0;
160 void AttachCodeLabel (CodeEntry* E, CodeLabel* L)
161 /* Attach the label to the entry */
163 /* Mark the label as defined */
166 /* Add it to the entries label list */
167 CollAppend (&E->Labels, L);
169 /* Tell the label about it's owner */
175 int CodeEntryHasLabel (const CodeEntry* E)
176 /* Check if the given code entry has labels attached */
178 return (CollCount (&E->Labels) > 0);
183 unsigned GetCodeLabelCount (const CodeEntry* E)
184 /* Get the number of labels attached to this entry */
186 return CollCount (&E->Labels);
191 CodeLabel* GetCodeLabel (CodeEntry* E, unsigned Index)
192 /* Get a label from this code entry */
194 return CollAt (&E->Labels, Index);
199 void MoveCodeLabel (CodeLabel* L, CodeEntry* E)
200 /* Move the code label L from it's former owner to the code entry E. */
202 /* Delete the label from the owner */
203 CollDeleteItem (&L->Owner->Labels, L);
205 /* Set the new owner */
206 CollAppend (&E->Labels, L);
212 int CodeEntryHasMark (const CodeEntry* E)
213 /* Return true if the given code entry has the CEF_USERMARK flag set */
215 return (E->Flags & CEF_USERMARK) != 0;
220 void CodeEntrySetMark (CodeEntry* E)
221 /* Set the CEF_USERMARK flag for the given entry */
223 E->Flags |= CEF_USERMARK;
228 void CodeEntryResetMark (CodeEntry* E)
229 /* Reset the CEF_USERMARK flag for the given entry */
231 E->Flags &= ~CEF_USERMARK;
236 void CodeEntrySetArg (CodeEntry* E, const char* Arg)
237 /* Set a new argument for the given code entry. An old string is deleted. */
239 /* Free the old argument */
242 /* Assign the new one */
243 E->Arg = GetArgCopy (Arg);
248 void OutputCodeEntry (const CodeEntry* E, FILE* F)
249 /* Output the code entry to a file */
255 /* If we have a label, print that */
256 unsigned LabelCount = CollCount (&E->Labels);
258 for (I = 0; I < LabelCount; ++I) {
259 OutputCodeLabel (CollConstAt (&E->Labels, I), F);
262 /* Get the opcode description */
263 D = GetOPCDesc (E->OPC);
265 /* Print the mnemonic */
266 Chars = fprintf (F, "\t%s", D->Mnemo);
268 /* Print the operand */
277 Chars += fprintf (F, "%*sa", 9-Chars, "");
282 Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
287 /* zeropage and absolute */
288 Chars += fprintf (F, "%*s%s", 9-Chars, "", E->Arg);
293 /* zeropage,X and absolute,X */
294 Chars += fprintf (F, "%*s%s,x", 9-Chars, "", E->Arg);
299 Chars += fprintf (F, "%*s%s,y", 9-Chars, "", E->Arg);
304 Chars += fprintf (F, "%*s(%s,x)", 9-Chars, "", E->Arg);
309 Chars += fprintf (F, "%*s(%s),y", 9-Chars, "", E->Arg);
314 Chars += fprintf (F, "%*s(%s)", 9-Chars, "", E->Arg);
319 Target = E->JumpTo? E->JumpTo->Name : E->Arg;
320 Chars += fprintf (F, "%*s%s", 9-Chars, "", Target);
324 Internal ("Invalid addressing mode");
328 /* Print usage info if requested by the debugging flag */
331 "%*s; USE: %c%c%c CHG: %c%c%c",
333 (E->Use & REG_A)? 'A' : '_',
334 (E->Use & REG_X)? 'X' : '_',
335 (E->Use & REG_Y)? 'Y' : '_',
336 (E->Chg & REG_A)? 'A' : '_',
337 (E->Chg & REG_X)? 'X' : '_',
338 (E->Chg & REG_Y)? 'Y' : '_');
341 /* Terminate the line */