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 /*****************************************************************************/
51 /*****************************************************************************/
53 /*****************************************************************************/
57 /*****************************************************************************/
59 /*****************************************************************************/
63 CodeEntry* NewCodeEntry (const OPCDesc* D, am_t AM, const char* Arg, CodeLabel* JumpTo)
64 /* Create a new code entry, initialize and return it */
67 CodeEntry* E = xmalloc (sizeof (CodeEntry));
69 /* Initialize the fields */
72 E->Size = GetInsnSize (E->OPC, E->AM);
74 E->Arg = (Arg && Arg[0] != '\0')? xstrdup (Arg) : 0;
78 if (E->OPC == OPC_JSR && E->Arg) {
79 /* A subroutine call */
80 E->Info |= GetFuncInfo (E->Arg);
82 /* Some other instruction */
83 E->Info |= GetAMUseInfo (AM);
86 InitCollection (&E->Labels);
88 /* If we have a label given, add this entry to the label */
90 CollAppend (&JumpTo->JumpFrom, E);
93 /* Return the initialized struct */
99 void FreeCodeEntry (CodeEntry* E)
100 /* Free the given code entry */
102 /* Free the string argument if we have one */
105 /* Cleanup the collection */
106 DoneCollection (&E->Labels);
114 int CodeEntryHasLabel (const CodeEntry* E)
115 /* Check if the given code entry has labels attached */
117 return (CollCount (&E->Labels) > 0);
122 void OutputCodeEntry (const CodeEntry* E, FILE* F)
123 /* Output the code entry to a file */
128 /* If we have a label, print that */
129 unsigned LabelCount = CollCount (&E->Labels);
131 for (I = 0; I < LabelCount; ++I) {
132 OutputCodeLabel (CollConstAt (&E->Labels, I), F);
135 /* Get the opcode description */
136 D = GetOPCDesc (E->OPC);
138 /* Print the mnemonic */
139 Chars = fprintf (F, "\t%s", D->Mnemo);
141 /* Print the operand */
150 Chars += fprintf (F, "%*sa", 9-Chars, "");
155 Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
160 /* zeropage and absolute */
161 Chars += fprintf (F, "%*s%s", 9-Chars, "", E->Arg);
166 /* zeropage,X and absolute,X */
167 Chars += fprintf (F, "%*s%s,x", 9-Chars, "", E->Arg);
172 Chars += fprintf (F, "%*s%s,y", 9-Chars, "", E->Arg);
177 Chars += fprintf (F, "%*s(%s,x)", 9-Chars, "", E->Arg);
182 Chars += fprintf (F, "%*s(%s),y", 9-Chars, "", E->Arg);
187 Chars += fprintf (F, "%*s(%s)", 9-Chars, "", E->Arg);
192 CHECK (E->JumpTo != 0);
193 Chars += fprintf (F, "%*s%s", 9-Chars, "", E->JumpTo->Name);
197 Internal ("Invalid addressing mode");
201 /* Print usage info if requested by the debugging flag */
204 "%*s; USE: %c%c%c CHG: %c%c%c",
206 (E->Info & CI_USE_A)? 'A' : '_',
207 (E->Info & CI_USE_X)? 'X' : '_',
208 (E->Info & CI_USE_Y)? 'Y' : '_',
209 (E->Info & CI_CHG_A)? 'A' : '_',
210 (E->Info & CI_CHG_X)? 'X' : '_',
211 (E->Info & CI_CHG_Y)? 'Y' : '_');
214 /* Terminate the line */