]> git.sur5r.net Git - cc65/blob - src/cc65/codeent.h
99c003b741ee04bc327b301cf02e78824d1b33a3
[cc65] / src / cc65 / codeent.h
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 codeent.h                                 */
4 /*                                                                           */
5 /*                            Code segment entry                             */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001-2002 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 #ifndef CODEENT_H
37 #define CODEENT_H
38
39
40
41 #include <stdio.h>
42 #include <string.h>
43
44 /* common */
45 #include "coll.h"
46 #include "inline.h"
47
48 /* cc65 */
49 #include "codelab.h"
50 #include "lineinfo.h"
51 #include "opcodes.h"
52 #include "reginfo.h"
53
54
55
56 /*****************************************************************************/
57 /*                                   Data                                    */
58 /*****************************************************************************/
59
60
61
62 /* Flags used */
63 #define CEF_USERMARK    0x0001U         /* Generic mark by user functions */
64 #define CEF_NUMARG      0x0002U         /* Insn has numerical argument */
65
66 /* Code entry structure */
67 typedef struct CodeEntry CodeEntry;
68 struct CodeEntry {
69     unsigned char       OPC;            /* Opcode */
70     unsigned char       AM;             /* Adressing mode */
71     unsigned char       Size;           /* Estimated size */
72     unsigned char       Flags;          /* Flags */
73     char*               Arg;            /* Argument as string */
74     unsigned long       Num;            /* Numeric argument */
75     unsigned short      Info;           /* Additional code info */
76     unsigned short      Use;            /* Registers used */
77     unsigned short      Chg;            /* Registers changed/destroyed */
78     CodeLabel*          JumpTo;         /* Jump label */
79     Collection          Labels;         /* Labels for this instruction */
80     LineInfo*           LI;             /* Source line info for this insn */
81     RegInfo*            RI;             /* Register info for this insn */
82 };
83
84
85
86 /*****************************************************************************/
87 /*                                   Code                                    */
88 /*****************************************************************************/
89
90
91
92 const char* MakeHexArg (unsigned Num);
93 /* Convert Num into a string in the form $XY, suitable for passing it as an
94  * argument to NewCodeEntry, and return a pointer to the string.
95  * BEWARE: The function returns a pointer to a static buffer, so the value is
96  * gone if you call it twice (and apart from that it's not thread and signal
97  * safe).
98  */
99
100 CodeEntry* NewCodeEntry (opc_t OPC, am_t AM, const char* Arg,
101                          CodeLabel* JumpTo, LineInfo* LI);
102 /* Create a new code entry, initialize and return it */
103
104 void FreeCodeEntry (CodeEntry* E);
105 /* Free the given code entry */
106
107 void CE_ReplaceOPC (CodeEntry* E, opc_t OPC);
108 /* Replace the opcode of the instruction. This will also replace related info,
109  * Size, Use and Chg, but it will NOT update any arguments or labels.
110  */
111
112 int CodeEntriesAreEqual (const CodeEntry* E1, const CodeEntry* E2);
113 /* Check if both code entries are equal */
114
115 void CE_AttachLabel (CodeEntry* E, CodeLabel* L);
116 /* Attach the label to the entry */
117
118 void CE_ClearJumpTo (CodeEntry* E);
119 /* Clear the JumpTo entry and the argument (which contained the name of the
120  * label). Note: The function will not clear the backpointer from the label,
121  * so use it with care.
122  */
123
124 #if defined(HAVE_INLINE)
125 INLINE int CE_HasLabel (const CodeEntry* E)
126 /* Check if the given code entry has labels attached */
127 {
128     return (CollCount (&E->Labels) > 0);
129 }
130 #else
131 #  define CE_HasLabel(E)        (CollCount (&(E)->Labels) > 0)
132 #endif
133
134 #if defined(HAVE_INLINE)
135 INLINE unsigned CE_GetLabelCount (const CodeEntry* E)
136 /* Get the number of labels attached to this entry */
137 {
138     return CollCount (&E->Labels);
139 }
140 #else
141 #  define CE_GetLabelCount(E)   CollCount (&(E)->Labels)
142 #endif
143
144 #if defined(HAVE_INLINE)
145 INLINE CodeLabel* CE_GetLabel (CodeEntry* E, unsigned Index)
146 /* Get a label from this code entry */
147 {
148     return CollAt (&E->Labels, Index);
149 }
150 #else
151 #  define CE_GetLabel(E, Index) CollAt (&(E)->Labels, (Index))
152 #endif
153
154 void CE_MoveLabel (CodeLabel* L, CodeEntry* E);
155 /* Move the code label L from it's former owner to the code entry E. */
156
157 #if defined(HAVE_INLINE)
158 INLINE int CE_HasMark (const CodeEntry* E)
159 /* Return true if the given code entry has the CEF_USERMARK flag set */
160 {
161     return (E->Flags & CEF_USERMARK) != 0;
162 }
163 #else
164 #  define CE_HasMark(E) (((E)->Flags & CEF_USERMARK) != 0)
165 #endif
166
167 #if defined(HAVE_INLINE)
168 INLINE void CE_SetMark (CodeEntry* E)
169 /* Set the CEF_USERMARK flag for the given entry */
170 {
171     E->Flags |= CEF_USERMARK;
172 }
173 #else
174 #  define CE_SetMark(E) ((E)->Flags |= CEF_USERMARK)
175 #endif
176
177 #if defined(HAVE_INLINE)
178 INLINE void CE_ResetMark (CodeEntry* E)
179 /* Reset the CEF_USERMARK flag for the given entry */
180 {
181     E->Flags &= ~CEF_USERMARK;
182 }
183 #else
184 #  define CE_ResetMark(E)       ((E)->Flags &= ~CEF_USERMARK)
185 #endif
186
187 void CE_SetArg (CodeEntry* E, const char* Arg);
188 /* Replace the argument by the new one. */
189
190 void CE_SetNumArg (CodeEntry* E, long Num);
191 /* Set a new numeric argument for the given code entry that must already
192  * have a numeric argument.
193  */
194
195 int CE_KnownImm (const CodeEntry* E);
196 /* Return true if the argument of E is a known immediate value */
197
198 #if defined(HAVE_INLINE)
199 INLINE int CE_IsCallTo (const CodeEntry* E, const char* Name)
200 /* Check if this is a call to the given function */
201 {
202     return (E->OPC == OP65_JSR && strcmp (E->Arg, Name) == 0);
203 }
204 #else
205 #  define CE_IsCallTo(E, Name) ((E)->OPC == OP65_JSR && strcmp ((E)->Arg, (Name)) == 0)
206 #endif
207
208 int CE_UseLoadFlags (const CodeEntry* E);
209 /* Return true if the instruction uses any flags that are set by a load of
210  * a register (N and Z).
211  */
212
213 void CE_FreeRegInfo (CodeEntry* E);
214 /* Free an existing register info struct */
215
216 void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs);
217 /* Generate register info for this instruction. If an old info exists, it is
218  * overwritten.
219  */
220
221 void CE_Output (const CodeEntry* E, FILE* F);
222 /* Output the code entry to a file */
223
224
225
226 /* End of codeent.h */
227 #endif
228
229
230
231