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 /*****************************************************************************/
138 /*****************************************************************************/
142 CodeEntry* NewCodeEntry (const OPCDesc* D, am_t AM, const char* Arg, CodeLabel* JumpTo)
143 /* Create a new code entry, initialize and return it */
145 /* Allocate memory */
146 CodeEntry* E = xmalloc (sizeof (CodeEntry));
148 /* Initialize the fields */
151 E->Size = GetInsnSize (E->OPC, E->AM);
153 E->Arg = GetArgCopy (Arg);
154 if (NumArg (E->Arg, &E->Num)) {
155 E-> Flags = CEF_NUMARG;
162 if (E->OPC == OPC_JSR) {
163 /* A subroutine call */
164 GetFuncInfo (E->Arg, &E->Use, &E->Chg);
165 } else if ((E->Info & OF_BRA) != 0 && JumpTo == 0) {
166 /* Jump to external symbol (function exit) */
167 GetFuncInfo (E->Arg, &E->Use, &E->Chg);
169 /* Some other instruction */
170 E->Use |= GetAMUseInfo (E->AM);
173 InitCollection (&E->Labels);
175 /* If we have a label given, add this entry to the label */
177 CollAppend (&JumpTo->JumpFrom, E);
180 /* Return the initialized struct */
186 void FreeCodeEntry (CodeEntry* E)
187 /* Free the given code entry */
189 /* Free the string argument if we have one */
192 /* Cleanup the collection */
193 DoneCollection (&E->Labels);
201 void ReplaceOPC (CodeEntry* E, opc_t OPC)
202 /* Replace the opcode of the instruction. This will also replace related info,
203 * Size, Use and Chg, but it will NOT update any arguments or labels.
206 /* Get the opcode descriptor */
207 const OPCDesc* D = GetOPCDesc (OPC);
209 /* Replace the opcode */
211 E->Size = GetInsnSize (E->OPC, E->AM);
215 if (E->OPC == OPC_JSR) {
216 /* A subroutine call */
217 GetFuncInfo (E->Arg, &E->Use, &E->Chg);
219 /* Some other instruction */
220 E->Use |= GetAMUseInfo (E->AM);
226 int CodeEntriesAreEqual (const CodeEntry* E1, const CodeEntry* E2)
227 /* Check if both code entries are equal */
229 return E1->OPC == E2->OPC && E1->AM == E2->AM && strcmp (E1->Arg, E2->Arg) == 0;
234 void AttachCodeLabel (CodeEntry* E, CodeLabel* L)
235 /* Attach the label to the entry */
237 /* Mark the label as defined */
240 /* Add it to the entries label list */
241 CollAppend (&E->Labels, L);
243 /* Tell the label about it's owner */
249 int CodeEntryHasLabel (const CodeEntry* E)
250 /* Check if the given code entry has labels attached */
252 return (CollCount (&E->Labels) > 0);
257 unsigned GetCodeLabelCount (const CodeEntry* E)
258 /* Get the number of labels attached to this entry */
260 return CollCount (&E->Labels);
265 CodeLabel* GetCodeLabel (CodeEntry* E, unsigned Index)
266 /* Get a label from this code entry */
268 return CollAt (&E->Labels, Index);
273 void MoveCodeLabel (CodeLabel* L, CodeEntry* E)
274 /* Move the code label L from it's former owner to the code entry E. */
276 /* Delete the label from the owner */
277 CollDeleteItem (&L->Owner->Labels, L);
279 /* Set the new owner */
280 CollAppend (&E->Labels, L);
286 int CodeEntryHasMark (const CodeEntry* E)
287 /* Return true if the given code entry has the CEF_USERMARK flag set */
289 return (E->Flags & CEF_USERMARK) != 0;
294 void CodeEntrySetMark (CodeEntry* E)
295 /* Set the CEF_USERMARK flag for the given entry */
297 E->Flags |= CEF_USERMARK;
302 void CodeEntryResetMark (CodeEntry* E)
303 /* Reset the CEF_USERMARK flag for the given entry */
305 E->Flags &= ~CEF_USERMARK;
310 void CodeEntrySetArg (CodeEntry* E, const char* Arg)
311 /* Set a new argument for the given code entry. An old string is deleted. */
313 /* Free the old argument */
316 /* Assign the new one */
317 E->Arg = GetArgCopy (Arg);
322 void OutputCodeEntry (const CodeEntry* E, FILE* F)
323 /* Output the code entry to a file */
329 /* If we have a label, print that */
330 unsigned LabelCount = CollCount (&E->Labels);
332 for (I = 0; I < LabelCount; ++I) {
333 OutputCodeLabel (CollConstAt (&E->Labels, I), F);
336 /* Get the opcode description */
337 D = GetOPCDesc (E->OPC);
339 /* Print the mnemonic */
340 Chars = fprintf (F, "\t%s", D->Mnemo);
342 /* Print the operand */
351 Chars += fprintf (F, "%*sa", 9-Chars, "");
356 Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
361 /* zeropage and absolute */
362 Chars += fprintf (F, "%*s%s", 9-Chars, "", E->Arg);
367 /* zeropage,X and absolute,X */
368 Chars += fprintf (F, "%*s%s,x", 9-Chars, "", E->Arg);
373 Chars += fprintf (F, "%*s%s,y", 9-Chars, "", E->Arg);
378 Chars += fprintf (F, "%*s(%s,x)", 9-Chars, "", E->Arg);
383 Chars += fprintf (F, "%*s(%s),y", 9-Chars, "", E->Arg);
388 Chars += fprintf (F, "%*s(%s)", 9-Chars, "", E->Arg);
393 Target = E->JumpTo? E->JumpTo->Name : E->Arg;
394 Chars += fprintf (F, "%*s%s", 9-Chars, "", Target);
398 Internal ("Invalid addressing mode");
402 /* Print usage info if requested by the debugging flag */
405 "%*s; USE: %c%c%c CHG: %c%c%c",
407 (E->Use & REG_A)? 'A' : '_',
408 (E->Use & REG_X)? 'X' : '_',
409 (E->Use & REG_Y)? 'Y' : '_',
410 (E->Chg & REG_A)? 'A' : '_',
411 (E->Chg & REG_X)? 'X' : '_',
412 (E->Chg & REG_Y)? 'Y' : '_');