]> git.sur5r.net Git - cc65/blob - src/cc65/opcodes.h
Added a peephole optimization step for global array accesses
[cc65] / src / cc65 / opcodes.h
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 opcodes.h                                 */
4 /*                                                                           */
5 /*                  Opcode and addressing mode definitions                   */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001      Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
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 OPCODES_H
37 #define OPCODES_H
38
39
40
41 /* common */
42 #include "inline.h"
43
44
45
46 /*****************************************************************************/
47 /*                                   Data                                    */
48 /*****************************************************************************/
49
50
51
52 /* Definitions for the possible opcodes */
53 typedef enum {
54
55     /* 65XX opcodes */
56     OP65_ADC,
57     OP65_AND,
58     OP65_ASL,
59     OP65_BCC,
60     OP65_BCS,
61     OP65_BEQ,
62     OP65_BIT,
63     OP65_BMI,
64     OP65_BNE,
65     OP65_BPL,
66     OP65_BRA,
67     OP65_BRK,
68     OP65_BVC,
69     OP65_BVS,
70     OP65_CLC,
71     OP65_CLD,
72     OP65_CLI,
73     OP65_CLV,
74     OP65_CMP,
75     OP65_CPX,
76     OP65_CPY,
77     OP65_DEA,
78     OP65_DEC,
79     OP65_DEX,
80     OP65_DEY,
81     OP65_EOR,
82     OP65_INA,
83     OP65_INC,
84     OP65_INX,
85     OP65_INY,
86     OP65_JCC,
87     OP65_JCS,
88     OP65_JEQ,
89     OP65_JMI,
90     OP65_JMP,
91     OP65_JNE,
92     OP65_JPL,
93     OP65_JSR,
94     OP65_JVC,
95     OP65_JVS,
96     OP65_LDA,
97     OP65_LDX,
98     OP65_LDY,
99     OP65_LSR,
100     OP65_NOP,
101     OP65_ORA,
102     OP65_PHA,
103     OP65_PHP,
104     OP65_PHX,
105     OP65_PHY,
106     OP65_PLA,
107     OP65_PLP,
108     OP65_PLX,
109     OP65_PLY,
110     OP65_ROL,
111     OP65_ROR,
112     OP65_RTI,
113     OP65_RTS,
114     OP65_SBC,
115     OP65_SEC,
116     OP65_SED,
117     OP65_SEI,
118     OP65_STA,
119     OP65_STX,
120     OP65_STY,
121     OP65_TAX,
122     OP65_TAY,
123     OP65_TRB,
124     OP65_TSB,
125     OP65_TSX,
126     OP65_TXA,
127     OP65_TXS,
128     OP65_TYA,
129
130     /* Number of opcodes available */
131     OPCODE_COUNT,
132
133     /* Several other opcode information constants */
134     OP65_FIRST = OP65_ADC,
135     OP65_LAST  = OP65_TYA,
136     OP65_COUNT = OP65_LAST - OP65_FIRST + 1
137 } opc_t;
138
139 /* Addressing modes */
140 typedef enum {
141
142     /* Addressing modes of the virtual stack machine */
143     AM_IMP,
144     AM_IMM,
145     AM_STACK,
146     AM_ABS,
147
148     /* 65XX addressing modes */
149     AM65_IMP,                   /* implicit */
150     AM65_ACC,                   /* accumulator */
151     AM65_IMM,                   /* immidiate */
152     AM65_ZP,                    /* zeropage */
153     AM65_ZPX,                   /* zeropage,X */
154     AM65_ZPY,                   /* zeropage,Y */
155     AM65_ABS,                   /* absolute */
156     AM65_ABSX,                  /* absolute,X */
157     AM65_ABSY,                  /* absolute,Y */
158     AM65_ZPX_IND,               /* (zeropage,x) */
159     AM65_ZP_INDY,               /* (zeropage),y */
160     AM65_ZP_IND,                /* (zeropage) */
161     AM65_BRA                    /* branch */
162 } am_t;
163
164 /* Branch conditions */
165 typedef enum {
166     BC_CC,
167     BC_CS,
168     BC_EQ,
169     BC_MI,
170     BC_NE,
171     BC_PL,
172     BC_VC,
173     BC_VS
174 } bc_t;
175
176 /* Opcode info */
177 #define OF_NONE         0x0000U /* No additional information */
178 #define OF_UBRA         0x0001U /* Unconditional branch */
179 #define OF_CBRA         0x0002U /* Conditional branch */
180 #define OF_ZBRA         0x0004U /* Branch on zero flag condition */
181 #define OF_FBRA         0x0008U /* Branch on cond set by a load */
182 #define OF_LBRA         0x0010U /* Jump/branch is long */
183 #define OF_RET          0x0020U /* Return from function */
184 #define OF_LOAD         0x0040U /* Register load */
185 #define OF_STORE        0x0080U /* Register store */
186 #define OF_XFR          0x0100U /* Transfer instruction */
187 #define OF_CALL         0x0200U /* A subroutine call */
188 #define OF_REG_INCDEC   0x0400U /* A register increment or decrement */
189 #define OF_SETF         0x0800U /* Insn will set all load flags (not carry) */
190
191 /* Combined infos */
192 #define OF_BRA  (OF_UBRA | OF_CBRA)     /* Operation is a jump/branch */
193 #define OF_DEAD (OF_UBRA | OF_RET)      /* Dead end - no exec behind this point */
194
195 /* Opcode description */
196 typedef struct {
197     opc_t           OPC;                /* Opcode */
198     char            Mnemo[9];           /* Mnemonic */
199     unsigned char   Size;               /* Size, 0 = check addressing mode */
200     unsigned char   Use;                /* Registers used by this insn */
201     unsigned char   Chg;                /* Registers changed by this insn */
202     unsigned short  Info;               /* Additional information */
203 } OPCDesc;
204
205 /* Opcode description table */
206 extern const OPCDesc OPCTable[OPCODE_COUNT];
207
208
209
210 /*****************************************************************************/
211 /*                                   Code                                    */
212 /*****************************************************************************/
213
214
215
216 const OPCDesc* FindOP65 (const char* OPC);
217 /* Find the given opcode and return the opcode description. If the opcode was
218  * not found, NULL is returned.
219  */
220
221 unsigned GetInsnSize (opc_t OPC, am_t AM);
222 /* Return the size of the given instruction */
223
224 #if defined(HAVE_INLINE)
225 INLINE const OPCDesc* GetOPCDesc (opc_t OPC)
226 /* Get an opcode description */
227 {
228     /* Return the description */
229     return &OPCTable [OPC];
230 }
231 #else
232 #  define GetOPCDesc(OPC)       (&OPCTable [(OPC)])
233 #endif
234
235 #if defined(HAVE_INLINE)
236 INLINE unsigned GetOPCInfo (opc_t OPC)
237 /* Get opcode information */
238 {
239     /* Return the info */
240     return OPCTable[OPC].Info;
241 }
242 #else
243 #  define GetOPCInfo(OPC)       (OPCTable[(OPC)].Info)
244 #endif
245
246 unsigned char GetAMUseInfo (am_t AM);
247 /* Get usage info for the given addressing mode (addressing modes that use
248  * index registers return REG_r info for these registers).
249  */
250
251 opc_t GetInverseBranch (opc_t OPC);
252 /* Return a branch that reverse the condition of the branch given in OPC */
253
254 opc_t MakeShortBranch (opc_t OPC);
255 /* Return the short version of the given branch. If the branch is already
256  * a short branch, return the opcode unchanged.
257  */
258
259 opc_t MakeLongBranch (opc_t OPC);
260 /* Return the long version of the given branch. If the branch is already
261  * a long branch, return the opcode unchanged.
262  */
263
264 bc_t GetBranchCond (opc_t OPC);
265 /* Get the condition for the conditional branch in OPC */
266
267 bc_t GetInverseCond (bc_t BC);
268 /* Return the inverse condition of the given one */
269
270
271
272 /* End of opcodes.h */
273 #endif
274
275
276