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