]> git.sur5r.net Git - cc65/blob - src/ca65/ea.c
Free expression trees when they're no longer needed
[cc65] / src / ca65 / ea.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                   ea.c                                    */
4 /*                                                                           */
5 /*           Effective address parsing for the ca65 macroassembler           */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 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 /* ld65 */
37 #include "error.h"
38 #include "expr.h"
39 #include "instr.h"
40 #include "nexttok.h"
41 #include "ea.h"
42
43
44
45 /*****************************************************************************/
46 /*                                   Code                                    */
47 /*****************************************************************************/
48
49
50
51 void GetEA (EffAddr* A)
52 /* Parse an effective address, return the result in A */
53 {
54     unsigned long Restrictions;
55
56     /* Clear the output struct */
57     A->AddrModeSet = 0;
58     A->Bank = 0;
59     A->Expr = 0;
60
61     /* Handle an addressing size override */
62     switch (Tok) {
63         case TOK_OVERRIDE_ZP:
64             Restrictions = AM_DIR | AM_DIR_X | AM_DIR_Y;
65             NextTok ();
66             break;
67
68         case TOK_OVERRIDE_ABS:
69             Restrictions = AM_ABS | AM_ABS_X | AM_ABS_Y;
70             NextTok ();
71             break;
72
73         case TOK_OVERRIDE_FAR:
74             Restrictions = AM_ABS_LONG | AM_ABS_LONG_X;
75             NextTok ();
76             break;
77
78         default:
79             Restrictions = ~0UL;        /* None */
80             break;
81     }
82
83     /* Parse the effective address */
84     if (TokIsSep (Tok)) {
85
86         A->AddrModeSet = AM_IMPLICIT;
87
88     } else if (Tok == TOK_HASH) {
89
90         /* #val */
91         NextTok ();
92         A->Expr  = Expression ();
93         A->AddrModeSet = AM_IMM;
94
95     } else if (Tok == TOK_A) {
96
97         NextTok ();
98         A->AddrModeSet = AM_ACCU;
99
100     } else if (Tok == TOK_LBRACK) {
101
102         /* [dir] or [dir],y */
103         NextTok ();
104         A->Expr = Expression ();
105         Consume (TOK_RBRACK, ERR_RBRACK_EXPECTED);
106         if (Tok == TOK_COMMA) {
107             /* [dir],y */
108             NextTok ();
109             Consume (TOK_Y, ERR_Y_EXPECTED);
110             A->AddrModeSet = AM_DIR_IND_LONG_Y;
111         } else {
112             /* [dir] */
113             A->AddrModeSet = AM_DIR_IND_LONG;
114         }
115
116     } else if (Tok == TOK_LPAREN) {
117
118         /* One of the indirect modes */
119         NextTok ();
120         A->Expr = Expression ();
121
122         if (Tok == TOK_COMMA) {
123
124             /* (expr,X) or (rel,S),y */
125             NextTok ();
126             if (Tok == TOK_X) {
127                 /* (adr,x) */
128                 NextTok ();
129                 A->AddrModeSet = AM_ABS_X_IND | AM_DIR_X_IND;
130                 ConsumeRParen ();
131             } else if (Tok == TOK_S) {
132                 /* (rel,s),y */
133                 NextTok ();
134                 A->AddrModeSet = AM_STACK_REL_IND_Y;
135                 ConsumeRParen ();
136                 ConsumeComma ();
137                 Consume (TOK_Y, ERR_Y_EXPECTED);
138             } else {
139                 Error (ERR_SYNTAX);
140             }
141
142         } else {
143
144             /* (adr) or (adr),y */
145             ConsumeRParen ();
146             if (Tok == TOK_COMMA) {
147                 /* (adr),y */
148                 NextTok ();
149                 Consume (TOK_Y, ERR_Y_EXPECTED);
150                 A->AddrModeSet = AM_DIR_IND_Y;
151             } else {
152                 /* (adr) */
153                 A->AddrModeSet = AM_ABS_IND | AM_DIR_IND;
154             }
155         }
156
157     } else {
158
159         /* Remaining stuff:
160          *
161          * adr
162          * bank.adr
163          * adr,x
164          * bank.adr,x
165          * adr,y
166          * adr,s
167          */
168         A->Expr = Expression ();
169
170         if (Tok == TOK_DOT) {
171
172             /* Expr was a bank specification: bank.adr or bank.adr,x */
173             A->Bank = A->Expr;
174             NextTok ();
175             A->Expr = Expression ();
176             if (Tok == TOK_COMMA) {
177                 /* bank.adr,x */
178                 NextTok ();
179                 Consume (TOK_X, ERR_X_EXPECTED);
180                 A->AddrModeSet = AM_ABS_LONG_X;
181             } else {
182                 /* bank.adr */
183                 A->AddrModeSet = AM_ABS_LONG;
184             }
185
186         } else {
187
188             if (Tok == TOK_COMMA) {
189
190                 NextTok ();
191                 switch (Tok) {
192
193                     case TOK_X:
194                         A->AddrModeSet = AM_ABS_LONG_X | AM_ABS_X | AM_DIR_X;
195                         NextTok ();
196                         break;
197
198                     case TOK_Y:
199                         A->AddrModeSet = AM_ABS_Y | AM_DIR_Y;
200                         NextTok ();
201                         break;
202
203                     case TOK_S:
204                         A->AddrModeSet = AM_STACK_REL;
205                         NextTok ();
206                         break;
207
208                     default:
209                         Error (ERR_SYNTAX);
210
211                 }
212
213             } else {
214
215                 A->AddrModeSet = AM_ABS_LONG | AM_ABS | AM_DIR;
216
217             }
218         }
219     }
220
221     /* Apply addressing mode overrides */
222     A->AddrModeSet &= Restrictions;
223 }
224
225
226