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