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->OPC == OPC_JSR || ((E->Info & OF_BRA) != 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, CodeLabel* JumpTo)
164 /* Create a new code entry, initialize and return it */
166 /* Get the opcode description */
167 const OPCDesc* D = GetOPCDesc (OPC);
169 /* Allocate memory */
170 CodeEntry* E = xmalloc (sizeof (CodeEntry));
172 /* Initialize the fields */
175 E->Size = GetInsnSize (E->OPC, E->AM);
177 E->Arg = GetArgCopy (Arg);
178 if (NumArg (E->Arg, &E->Num)) {
179 E-> Flags = CEF_NUMARG;
185 SetUseChgInfo (E, D);
186 InitCollection (&E->Labels);
188 /* If we have a label given, add this entry to the label */
190 CollAppend (&JumpTo->JumpFrom, E);
193 /* Return the initialized struct */
199 void FreeCodeEntry (CodeEntry* E)
200 /* Free the given code entry */
202 /* Free the string argument if we have one */
205 /* Cleanup the collection */
206 DoneCollection (&E->Labels);
214 void ReplaceOPC (CodeEntry* E, opc_t OPC)
215 /* Replace the opcode of the instruction. This will also replace related info,
216 * Size, Use and Chg, but it will NOT update any arguments or labels.
219 /* Get the opcode descriptor */
220 const OPCDesc* D = GetOPCDesc (OPC);
222 /* Replace the opcode */
224 E->Size = GetInsnSize (E->OPC, E->AM);
226 SetUseChgInfo (E, D);
231 int CodeEntriesAreEqual (const CodeEntry* E1, const CodeEntry* E2)
232 /* Check if both code entries are equal */
234 return E1->OPC == E2->OPC && E1->AM == E2->AM && strcmp (E1->Arg, E2->Arg) == 0;
239 void AttachCodeLabel (CodeEntry* E, CodeLabel* L)
240 /* Attach the label to the entry */
242 /* Mark the label as defined */
245 /* Add it to the entries label list */
246 CollAppend (&E->Labels, L);
248 /* Tell the label about it's owner */
254 void MoveCodeLabel (CodeLabel* L, CodeEntry* E)
255 /* Move the code label L from it's former owner to the code entry E. */
257 /* Delete the label from the owner */
258 CollDeleteItem (&L->Owner->Labels, L);
260 /* Set the new owner */
261 CollAppend (&E->Labels, L);
267 void CodeEntrySetArg (CodeEntry* E, const char* Arg)
268 /* Set a new argument for the given code entry. An old string is deleted. */
270 /* Free the old argument */
273 /* Assign the new one */
274 E->Arg = GetArgCopy (Arg);
279 void OutputCodeEntry (const CodeEntry* E, FILE* F)
280 /* Output the code entry to a file */
286 /* If we have a label, print that */
287 unsigned LabelCount = CollCount (&E->Labels);
289 for (I = 0; I < LabelCount; ++I) {
290 OutputCodeLabel (CollConstAt (&E->Labels, I), F);
293 /* Get the opcode description */
294 D = GetOPCDesc (E->OPC);
296 /* Print the mnemonic */
297 Chars = fprintf (F, "\t%s", D->Mnemo);
299 /* Print the operand */
308 Chars += fprintf (F, "%*sa", 9-Chars, "");
313 Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
318 /* zeropage and absolute */
319 Chars += fprintf (F, "%*s%s", 9-Chars, "", E->Arg);
324 /* zeropage,X and absolute,X */
325 Chars += fprintf (F, "%*s%s,x", 9-Chars, "", E->Arg);
330 Chars += fprintf (F, "%*s%s,y", 9-Chars, "", E->Arg);
335 Chars += fprintf (F, "%*s(%s,x)", 9-Chars, "", E->Arg);
340 Chars += fprintf (F, "%*s(%s),y", 9-Chars, "", E->Arg);
345 Chars += fprintf (F, "%*s(%s)", 9-Chars, "", E->Arg);
350 Target = E->JumpTo? E->JumpTo->Name : E->Arg;
351 Chars += fprintf (F, "%*s%s", 9-Chars, "", Target);
355 Internal ("Invalid addressing mode");
359 /* Print usage info if requested by the debugging flag */
362 "%*s; USE: %c%c%c CHG: %c%c%c",
364 (E->Use & REG_A)? 'A' : '_',
365 (E->Use & REG_X)? 'X' : '_',
366 (E->Use & REG_Y)? 'Y' : '_',
367 (E->Chg & REG_A)? 'A' : '_',
368 (E->Chg & REG_X)? 'X' : '_',
369 (E->Chg & REG_Y)? 'Y' : '_');