]> git.sur5r.net Git - cc65/blob - src/cc65/codeent.c
d8f852bc9507c6429d8f22f724d263f4f27079c9
[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 /* common */
37 #include "check.h"
38 #include "xmalloc.h"
39
40 /* cc65 */
41 #include "codeinfo.h"
42 #include "error.h"
43 #include "global.h"
44 #include "codelab.h"
45 #include "opcodes.h"
46 #include "codeent.h"
47
48
49
50 /*****************************************************************************/
51 /*                                   Data                                    */
52 /*****************************************************************************/
53
54
55
56 /*****************************************************************************/
57 /*                                   Code                                    */
58 /*****************************************************************************/
59
60
61
62 CodeEntry* NewCodeEntry (const OPCDesc* D, am_t AM, const char* Arg, CodeLabel* JumpTo)
63 /* Create a new code entry, initialize and return it */
64 {
65     /* Allocate memory */
66     CodeEntry* E = xmalloc (sizeof (CodeEntry));
67
68     /* Initialize the fields */
69     E->OPC      = D->OPC;
70     E->AM       = AM;
71     E->Size     = GetInsnSize (E->OPC, E->AM);
72     E->Hints    = 0;
73     E->Arg      = (Arg && Arg[0] != '\0')? xstrdup (Arg) : 0;
74     E->Num      = 0;
75     E->Flags    = 0;
76     E->Info     = D->Info;
77     E->Use      = D->Use;
78     E->Chg      = D->Chg;
79     if (E->OPC == OPC_JSR && E->Arg) {
80         /* A subroutine call */
81         GetFuncInfo (E->Arg, &E->Use, &E->Chg);
82     } else {
83         /* Some other instruction */
84         E->Use |= GetAMUseInfo (AM);
85     }
86     E->JumpTo   = JumpTo;
87     InitCollection (&E->Labels);
88
89     /* If we have a label given, add this entry to the label */
90     if (JumpTo) {
91         CollAppend (&JumpTo->JumpFrom, E);
92     }
93
94     /* Return the initialized struct */
95     return E;
96 }
97
98
99
100 void FreeCodeEntry (CodeEntry* E)
101 /* Free the given code entry */
102 {
103     /* Free the string argument if we have one */
104     xfree (E->Arg);
105
106     /* Cleanup the collection */
107     DoneCollection (&E->Labels);
108
109     /* Free the entry */
110     xfree (E);
111 }
112
113
114
115 int CodeEntryHasLabel (const CodeEntry* E)
116 /* Check if the given code entry has labels attached */
117 {
118     return (CollCount (&E->Labels) > 0);
119 }
120
121
122
123 int CodeEntryHasMark (const CodeEntry* E)
124 /* Return true if the given code entry has the CEF_USERMARK flag set */
125 {
126     return (E->Flags & CEF_USERMARK) != 0;
127 }
128
129
130
131 void CodeEntrySetMark (CodeEntry* E)
132 /* Set the CEF_USERMARK flag for the given entry */
133 {
134     E->Flags |= CEF_USERMARK;
135 }
136
137
138
139 void CodeEntryResetMark (CodeEntry* E)
140 /* Reset the CEF_USERMARK flag for the given entry */
141 {
142     E->Flags &= ~CEF_USERMARK;
143 }
144
145
146
147 CodeLabel* GetCodeLabel (CodeEntry* E, unsigned Index)
148 /* Get a label from this code entry */
149 {
150     return CollAt (&E->Labels, Index);
151 }
152
153
154
155 void MoveCodeLabel (CodeLabel* L, CodeEntry* E)
156 /* Move the code label L from it's former owner to the code entry E. */
157 {
158     /* Delete the label from the owner */
159     CollDeleteItem (&L->Owner->Labels, L);
160
161     /* Set the new owner */
162     CollAppend (&E->Labels, L);
163     L->Owner = E;
164 }
165
166
167
168 void OutputCodeEntry (const CodeEntry* E, FILE* F)
169 /* Output the code entry to a file */
170 {
171     const OPCDesc* D;
172     unsigned Chars;
173
174     /* If we have a label, print that */
175     unsigned LabelCount = CollCount (&E->Labels);
176     unsigned I;
177     for (I = 0; I < LabelCount; ++I) {
178         OutputCodeLabel (CollConstAt (&E->Labels, I), F);
179     }
180
181     /* Get the opcode description */
182     D = GetOPCDesc (E->OPC);
183
184     /* Print the mnemonic */
185     Chars = fprintf (F, "\t%s", D->Mnemo);
186
187     /* Print the operand */
188     switch (E->AM) {
189
190         case AM_IMP:
191             /* implicit */
192             break;
193
194         case AM_ACC:
195             /* accumulator */
196             Chars += fprintf (F, "%*sa", 9-Chars, "");
197             break;
198
199         case AM_IMM:
200             /* immidiate */
201             Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
202             break;
203
204         case AM_ZP:
205         case AM_ABS:
206             /* zeropage and absolute */
207             Chars += fprintf (F, "%*s%s", 9-Chars, "", E->Arg);
208             break;
209
210         case AM_ZPX:
211         case AM_ABSX:
212             /* zeropage,X and absolute,X */
213             Chars += fprintf (F, "%*s%s,x", 9-Chars, "", E->Arg);
214             break;
215
216         case AM_ABSY:
217             /* absolute,Y */
218             Chars += fprintf (F, "%*s%s,y", 9-Chars, "", E->Arg);
219             break;
220
221         case AM_ZPX_IND:
222             /* (zeropage,x) */
223             Chars += fprintf (F, "%*s(%s,x)", 9-Chars, "", E->Arg);
224             break;
225
226         case AM_ZP_INDY:
227             /* (zeropage),y */
228             Chars += fprintf (F, "%*s(%s),y", 9-Chars, "", E->Arg);
229             break;
230
231         case AM_ZP_IND:
232             /* (zeropage) */
233             Chars += fprintf (F, "%*s(%s)", 9-Chars, "", E->Arg);
234             break;
235
236         case AM_BRA:
237             /* branch */
238             CHECK (E->JumpTo != 0);
239             Chars += fprintf (F, "%*s%s", 9-Chars, "", E->JumpTo->Name);
240             break;
241
242         default:
243             Internal ("Invalid addressing mode");
244
245     }
246
247     /* Print usage info if requested by the debugging flag */
248 //    if (Debug) {
249         Chars += fprintf (F,
250                           "%*s; USE: %c%c%c CHG: %c%c%c",
251                           30-Chars, "",
252                           (E->Use & REG_A)? 'A' : '_',
253                           (E->Use & REG_X)? 'X' : '_',
254                           (E->Use & REG_Y)? 'Y' : '_',
255                           (E->Chg & REG_A)? 'A' : '_',
256                           (E->Chg & REG_X)? 'X' : '_',
257                           (E->Chg & REG_Y)? 'Y' : '_');
258 //    }
259
260     /* Terminate the line */
261     fprintf (F, "\n");
262 }
263
264
265
266