]> git.sur5r.net Git - cc65/blob - src/cc65/codeent.c
f2661e1e5e9379b93ca48a52f6653d60d1ceaa7d
[cc65] / src / cc65 / codeent.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 codeent.c                                 */
4 /*                                                                           */
5 /*                            Code segment entry                             */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37
38 /* common */
39 #include "check.h"
40 #include "xmalloc.h"
41
42 /* cc65 */
43 #include "codeinfo.h"
44 #include "error.h"
45 #include "global.h"
46 #include "codelab.h"
47 #include "opcodes.h"
48 #include "codeent.h"
49
50
51
52 /*****************************************************************************/
53 /*                                   Data                                    */
54 /*****************************************************************************/
55
56
57
58 /* Empty argument */
59 static char EmptyArg[] = "";
60
61
62
63 /*****************************************************************************/
64 /*                             Helper functions                              */
65 /*****************************************************************************/
66
67
68
69 static void FreeArg (char* Arg)
70 /* Free a code entry argument */
71 {
72     if (Arg != EmptyArg) {
73         xfree (Arg);
74     }
75 }
76
77
78
79 static char* GetArgCopy (const char* Arg)
80 /* Create an argument copy for assignment */
81 {
82     if (Arg && Arg[0] != '\0') {
83         /* Create a copy */
84         return xstrdup (Arg);
85     } else {
86         /* Use the empty argument string */
87         return EmptyArg;
88     }
89 }
90
91
92
93 /*****************************************************************************/
94 /*                                   Code                                    */
95 /*****************************************************************************/
96
97
98
99 CodeEntry* NewCodeEntry (const OPCDesc* D, am_t AM, const char* Arg, CodeLabel* JumpTo)
100 /* Create a new code entry, initialize and return it */
101 {
102     /* Allocate memory */
103     CodeEntry* E = xmalloc (sizeof (CodeEntry));
104
105     /* Initialize the fields */
106     E->OPC      = D->OPC;
107     E->AM       = AM;
108     E->Size     = GetInsnSize (E->OPC, E->AM);
109     E->Hints    = 0;
110     E->Arg      = GetArgCopy (Arg);
111     E->Num      = 0;
112     E->Flags    = 0;
113     E->Info     = D->Info;
114     E->Use      = D->Use;
115     E->Chg      = D->Chg;
116     if (E->OPC == OPC_JSR) {
117         /* A subroutine call */
118         GetFuncInfo (E->Arg, &E->Use, &E->Chg);
119     } else {
120         /* Some other instruction */
121         E->Use |= GetAMUseInfo (AM);
122     }
123     E->JumpTo   = JumpTo;
124     InitCollection (&E->Labels);
125
126     /* If we have a label given, add this entry to the label */
127     if (JumpTo) {
128         CollAppend (&JumpTo->JumpFrom, E);
129     }
130
131     /* Return the initialized struct */
132     return E;
133 }
134
135
136
137 void FreeCodeEntry (CodeEntry* E)
138 /* Free the given code entry */
139 {
140     /* Free the string argument if we have one */
141     FreeArg (E->Arg);
142
143     /* Cleanup the collection */
144     DoneCollection (&E->Labels);
145
146     /* Free the entry */
147     xfree (E);
148 }
149
150
151
152 int CodeEntriesAreEqual (const CodeEntry* E1, const CodeEntry* E2)
153 /* Check if both code entries are equal */
154 {
155     return E1->OPC == E2->OPC && E1->AM == E2->AM && strcmp (E1->Arg, E2->Arg) == 0;
156 }
157
158
159
160 void AttachCodeLabel (CodeEntry* E, CodeLabel* L)
161 /* Attach the label to the entry */
162 {
163     /* Mark the label as defined */
164     L->Flags |= LF_DEF;
165
166     /* Add it to the entries label list */
167     CollAppend (&E->Labels, L);
168
169     /* Tell the label about it's owner */
170     L->Owner = E;
171 }
172
173
174
175 int CodeEntryHasLabel (const CodeEntry* E)
176 /* Check if the given code entry has labels attached */
177 {
178     return (CollCount (&E->Labels) > 0);
179 }
180
181
182
183 unsigned GetCodeLabelCount (const CodeEntry* E)
184 /* Get the number of labels attached to this entry */
185 {
186     return CollCount (&E->Labels);
187 }
188
189
190
191 CodeLabel* GetCodeLabel (CodeEntry* E, unsigned Index)
192 /* Get a label from this code entry */
193 {
194     return CollAt (&E->Labels, Index);
195 }
196
197
198
199 void MoveCodeLabel (CodeLabel* L, CodeEntry* E)
200 /* Move the code label L from it's former owner to the code entry E. */
201 {
202     /* Delete the label from the owner */
203     CollDeleteItem (&L->Owner->Labels, L);
204
205     /* Set the new owner */
206     CollAppend (&E->Labels, L);
207     L->Owner = E;
208 }
209
210
211
212 int CodeEntryHasMark (const CodeEntry* E)
213 /* Return true if the given code entry has the CEF_USERMARK flag set */
214 {
215     return (E->Flags & CEF_USERMARK) != 0;
216 }
217
218
219
220 void CodeEntrySetMark (CodeEntry* E)
221 /* Set the CEF_USERMARK flag for the given entry */
222 {
223     E->Flags |= CEF_USERMARK;
224 }
225
226
227
228 void CodeEntryResetMark (CodeEntry* E)
229 /* Reset the CEF_USERMARK flag for the given entry */
230 {
231     E->Flags &= ~CEF_USERMARK;
232 }
233
234
235
236 void CodeEntrySetArg (CodeEntry* E, const char* Arg)
237 /* Set a new argument for the given code entry. An old string is deleted. */
238 {
239     /* Free the old argument */
240     FreeArg (E->Arg);
241
242     /* Assign the new one */
243     E->Arg = GetArgCopy (Arg);
244 }
245
246
247
248 void OutputCodeEntry (const CodeEntry* E, FILE* F)
249 /* Output the code entry to a file */
250 {
251     const OPCDesc* D;
252     unsigned Chars;
253     const char* Target;
254
255     /* If we have a label, print that */
256     unsigned LabelCount = CollCount (&E->Labels);
257     unsigned I;
258     for (I = 0; I < LabelCount; ++I) {
259         OutputCodeLabel (CollConstAt (&E->Labels, I), F);
260     }
261
262     /* Get the opcode description */
263     D = GetOPCDesc (E->OPC);
264
265     /* Print the mnemonic */
266     Chars = fprintf (F, "\t%s", D->Mnemo);
267
268     /* Print the operand */
269     switch (E->AM) {
270
271         case AM_IMP:
272             /* implicit */
273             break;
274
275         case AM_ACC:
276             /* accumulator */
277             Chars += fprintf (F, "%*sa", 9-Chars, "");
278             break;
279
280         case AM_IMM:
281             /* immidiate */
282             Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
283             break;
284
285         case AM_ZP:
286         case AM_ABS:
287             /* zeropage and absolute */
288             Chars += fprintf (F, "%*s%s", 9-Chars, "", E->Arg);
289             break;
290
291         case AM_ZPX:
292         case AM_ABSX:
293             /* zeropage,X and absolute,X */
294             Chars += fprintf (F, "%*s%s,x", 9-Chars, "", E->Arg);
295             break;
296
297         case AM_ABSY:
298             /* absolute,Y */
299             Chars += fprintf (F, "%*s%s,y", 9-Chars, "", E->Arg);
300             break;
301
302         case AM_ZPX_IND:
303             /* (zeropage,x) */
304             Chars += fprintf (F, "%*s(%s,x)", 9-Chars, "", E->Arg);
305             break;
306
307         case AM_ZP_INDY:
308             /* (zeropage),y */
309             Chars += fprintf (F, "%*s(%s),y", 9-Chars, "", E->Arg);
310             break;
311
312         case AM_ZP_IND:
313             /* (zeropage) */
314             Chars += fprintf (F, "%*s(%s)", 9-Chars, "", E->Arg);
315             break;
316
317         case AM_BRA:
318             /* branch */
319             Target = E->JumpTo? E->JumpTo->Name : E->Arg;
320             Chars += fprintf (F, "%*s%s", 9-Chars, "", Target);
321             break;
322
323         default:
324             Internal ("Invalid addressing mode");
325
326     }
327
328     /* Print usage info if requested by the debugging flag */
329 //    if (Debug) {
330         Chars += fprintf (F,
331                           "%*s; USE: %c%c%c CHG: %c%c%c",
332                           30-Chars, "",
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' : '_');
339 //    }
340
341     /* Terminate the line */
342     fprintf (F, "\n");
343 }
344
345
346
347