]> git.sur5r.net Git - cc65/blob - src/cc65/codeent.c
61b41a479a556c255125340f40a15ccb8356c120
[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 static void SetUseChgInfo (CodeEntry* E, const OPCDesc* D)
137 /* Set the Use and Chg in E */
138 {
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.
142      */
143     if ((E->Info & (OF_BRA | OF_CALL)) != 0 && E->JumpTo == 0) {
144         /* A subroutine call or jump to external symbol (function exit) */
145         GetFuncInfo (E->Arg, &E->Use, &E->Chg);
146     } else {
147         /* Some other instruction. Use the values from the opcode description
148          * plus addressing mode info
149          */
150         E->Use = D->Use | GetAMUseInfo (E->AM);
151         E->Chg = D->Chg;
152     }
153 }
154
155
156
157 /*****************************************************************************/
158 /*                                   Code                                    */
159 /*****************************************************************************/
160
161
162
163 CodeEntry* NewCodeEntry (opc_t OPC, am_t AM, const char* Arg,
164                          CodeLabel* JumpTo, LineInfo* LI)
165 /* Create a new code entry, initialize and return it */
166 {
167     /* Get the opcode description */
168     const OPCDesc* D = GetOPCDesc (OPC);
169
170     /* Allocate memory */
171     CodeEntry* E = xmalloc (sizeof (CodeEntry));
172
173     /* Initialize the fields */
174     E->OPC    = D->OPC;
175     E->AM     = AM;
176     E->Arg    = GetArgCopy (Arg);
177     E->Flags  = NumArg (E->Arg, &E->Num)? CEF_NUMARG : 0;
178     E->Info   = D->Info;
179     E->Size   = GetInsnSize (E->OPC, E->AM);
180     E->JumpTo = JumpTo;
181     E->LI     = UseLineInfo (LI);
182     SetUseChgInfo (E, D);
183     InitCollection (&E->Labels);
184
185     /* If we have a label given, add this entry to the label */
186     if (JumpTo) {
187         CollAppend (&JumpTo->JumpFrom, E);
188     }
189
190     /* Return the initialized struct */
191     return E;
192 }
193
194
195
196 void FreeCodeEntry (CodeEntry* E)
197 /* Free the given code entry */
198 {
199     /* Free the string argument if we have one */
200     FreeArg (E->Arg);
201
202     /* Cleanup the collection */
203     DoneCollection (&E->Labels);
204
205     /* Release the line info */
206     ReleaseLineInfo (E->LI);
207
208     /* Free the entry */
209     xfree (E);
210 }
211
212
213
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.
217  */
218 {
219     /* Get the opcode descriptor */
220     const OPCDesc* D = GetOPCDesc (OPC);
221
222     /* Replace the opcode */
223     E->OPC  = OPC;
224     E->Info = D->Info;
225     E->Size = GetInsnSize (E->OPC, E->AM);
226     SetUseChgInfo (E, D);
227 }
228
229
230
231 int CodeEntriesAreEqual (const CodeEntry* E1, const CodeEntry* E2)
232 /* Check if both code entries are equal */
233 {
234     return E1->OPC == E2->OPC && E1->AM == E2->AM && strcmp (E1->Arg, E2->Arg) == 0;
235 }
236
237
238
239 void AttachCodeLabel (CodeEntry* E, CodeLabel* L)
240 /* Attach the label to the entry */
241 {
242     /* Add it to the entries label list */
243     CollAppend (&E->Labels, L);
244
245     /* Tell the label about it's owner */
246     L->Owner = E;
247 }
248
249
250
251 void MoveCodeLabel (CodeLabel* L, CodeEntry* E)
252 /* Move the code label L from it's former owner to the code entry E. */
253 {
254     /* Delete the label from the owner */
255     CollDeleteItem (&L->Owner->Labels, L);
256
257     /* Set the new owner */
258     CollAppend (&E->Labels, L);
259     L->Owner = E;
260 }
261
262
263
264 void CodeEntrySetArg (CodeEntry* E, const char* Arg)
265 /* Set a new argument for the given code entry. An old string is deleted. */
266 {
267     /* Free the old argument */
268     FreeArg (E->Arg);
269
270     /* Assign the new one */
271     E->Arg = GetArgCopy (Arg);
272 }
273
274
275
276 void OutputCodeEntry (const CodeEntry* E, FILE* F)
277 /* Output the code entry to a file */
278 {
279     const OPCDesc* D;
280     unsigned Chars;
281     const char* Target;
282
283     /* If we have a label, print that */
284     unsigned LabelCount = CollCount (&E->Labels);
285     unsigned I;
286     for (I = 0; I < LabelCount; ++I) {
287         OutputCodeLabel (CollConstAt (&E->Labels, I), F);
288     }
289
290     /* Get the opcode description */
291     D = GetOPCDesc (E->OPC);
292
293     /* Print the mnemonic */
294     Chars = fprintf (F, "\t%s", D->Mnemo);
295
296     /* Print the operand */
297     switch (E->AM) {
298
299         case AM65_IMP:
300             /* implicit */
301             break;
302
303         case AM65_ACC:
304             /* accumulator */
305             Chars += fprintf (F, "%*sa", 9-Chars, "");
306             break;
307
308         case AM65_IMM:
309             /* immidiate */
310             Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
311             break;
312
313         case AM65_ZP:
314         case AM65_ABS:
315             /* zeropage and absolute */
316             Chars += fprintf (F, "%*s%s", 9-Chars, "", E->Arg);
317             break;
318
319         case AM65_ZPX:
320         case AM65_ABSX:
321             /* zeropage,X and absolute,X */
322             Chars += fprintf (F, "%*s%s,x", 9-Chars, "", E->Arg);
323             break;
324
325         case AM65_ABSY:
326             /* absolute,Y */
327             Chars += fprintf (F, "%*s%s,y", 9-Chars, "", E->Arg);
328             break;
329
330         case AM65_ZPX_IND:
331             /* (zeropage,x) */
332             Chars += fprintf (F, "%*s(%s,x)", 9-Chars, "", E->Arg);
333             break;
334
335         case AM65_ZP_INDY:
336             /* (zeropage),y */
337             Chars += fprintf (F, "%*s(%s),y", 9-Chars, "", E->Arg);
338             break;
339
340         case AM65_ZP_IND:
341             /* (zeropage) */
342             Chars += fprintf (F, "%*s(%s)", 9-Chars, "", E->Arg);
343             break;
344
345         case AM65_BRA:
346             /* branch */
347             Target = E->JumpTo? E->JumpTo->Name : E->Arg;
348             Chars += fprintf (F, "%*s%s", 9-Chars, "", Target);
349             break;
350
351         default:
352             Internal ("Invalid addressing mode");
353
354     }
355
356     /* Print usage info if requested by the debugging flag */
357     if (Debug) {
358         fprintf (F,
359                  "%*s; USE: %c%c%c CHG: %c%c%c SIZE: %u\n",
360                  30-Chars, "",
361                  (E->Use & REG_A)? 'A' : '_',
362                  (E->Use & REG_X)? 'X' : '_',
363                  (E->Use & REG_Y)? 'Y' : '_',
364                  (E->Chg & REG_A)? 'A' : '_',
365                  (E->Chg & REG_X)? 'X' : '_',
366                  (E->Chg & REG_Y)? 'Y' : '_',
367                  E->Size);
368     } else {
369         /* Terminate the line */
370         fprintf (F, "\n");
371     }
372 }
373
374
375
376
377