]> git.sur5r.net Git - cc65/blob - src/ca65/ea65.c
Merge remote-tracking branch 'upstream/master' into a5200
[cc65] / src / ca65 / ea65.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  ea65.c                                   */
4 /*                                                                           */
5 /*        65XX effective address parsing for the ca65 macroassembler         */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2011, 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 /* ca65 */
37 #include "ea.h"
38 #include "ea65.h"
39 #include "error.h"
40 #include "expr.h"
41 #include "instr.h"
42 #include "nexttok.h"
43
44
45
46 /*****************************************************************************/
47 /*                                   Code                                    */
48 /*****************************************************************************/
49
50
51
52 void GetEA (EffAddr* A)
53 /* Parse an effective address, return the result in A */
54 {
55     unsigned long Restrictions;
56
57     /* Clear the output struct */
58     A->AddrModeSet = 0;
59     A->Expr = 0;
60
61     /* Handle an addressing size override */
62     switch (CurTok.Tok) {
63         case TOK_OVERRIDE_ZP:
64             Restrictions = AM65_DIR | AM65_DIR_X | AM65_DIR_Y;
65             NextTok ();
66             break;
67
68         case TOK_OVERRIDE_ABS:
69             Restrictions = AM65_ABS | AM65_ABS_X | AM65_ABS_Y;
70             NextTok ();
71             break;
72
73         case TOK_OVERRIDE_FAR:
74             Restrictions = AM65_ABS_LONG | AM65_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 (CurTok.Tok)) {
85
86         A->AddrModeSet = AM65_IMPLICIT;
87
88     } else if (CurTok.Tok == TOK_HASH) {
89
90         /* #val */
91         NextTok ();
92         A->Expr  = Expression ();
93         A->AddrModeSet = AM65_ALL_IMM;
94
95     } else if (CurTok.Tok == TOK_A) {
96
97         NextTok ();
98         A->AddrModeSet = AM65_ACCU;
99
100     } else if (CurTok.Tok == TOK_LBRACK) {
101
102         /* [dir] or [dir],y */
103         NextTok ();
104         A->Expr = Expression ();
105         Consume (TOK_RBRACK, "']' expected");
106         if (CurTok.Tok == TOK_COMMA) {
107             /* [dir],y */
108             NextTok ();
109             Consume (TOK_Y, "`Y' expected");
110             A->AddrModeSet = AM65_DIR_IND_LONG_Y;
111         } else {
112             /* [dir] */
113             A->AddrModeSet = AM65_DIR_IND_LONG | AM65_ABS_IND_LONG;
114         }
115
116     } else if (CurTok.Tok == TOK_LPAREN) {
117
118         /* One of the indirect modes */
119         NextTok ();
120         A->Expr = Expression ();
121
122         if (CurTok.Tok == TOK_COMMA) {
123
124             /* (expr,X) or (rel,S),y */
125             NextTok ();
126             if (CurTok.Tok == TOK_X) {
127                 /* (adr,x) */
128                 NextTok ();
129                 A->AddrModeSet = AM65_ABS_X_IND | AM65_DIR_X_IND;
130                 ConsumeRParen ();
131             } else if (CurTok.Tok == TOK_S) {
132                 /* (rel,s),y */
133                 NextTok ();
134                 A->AddrModeSet = AM65_STACK_REL_IND_Y;
135                 ConsumeRParen ();
136                 ConsumeComma ();
137                 Consume (TOK_Y, "`Y' expected");
138             } else {
139                 Error ("Syntax error");
140             }
141
142         } else {
143
144             /* (adr) or (adr),y */
145             ConsumeRParen ();
146             if (CurTok.Tok == TOK_COMMA) {
147                 /* (adr),y */
148                 NextTok ();
149                 Consume (TOK_Y, "`Y' expected");
150                 A->AddrModeSet = AM65_DIR_IND_Y;
151             } else {
152                 /* (adr) */
153                 A->AddrModeSet = AM65_ABS_IND | AM65_ABS_IND_LONG | AM65_DIR_IND;
154             }
155         }
156
157     } else {
158
159         /* Remaining stuff:
160          *
161          * adr
162          * adr,x
163          * adr,y
164          * adr,s
165          */
166         A->Expr = Expression ();
167
168         if (CurTok.Tok == TOK_COMMA) {
169
170             NextTok ();
171             switch (CurTok.Tok) {
172
173                 case TOK_X:
174                     A->AddrModeSet = AM65_ABS_LONG_X | AM65_ABS_X | AM65_DIR_X;
175                     NextTok ();
176                     break;
177
178                 case TOK_Y:
179                     A->AddrModeSet = AM65_ABS_Y | AM65_DIR_Y;
180                     NextTok ();
181                     break;
182
183                 case TOK_S:
184                     A->AddrModeSet = AM65_STACK_REL;
185                     NextTok ();
186                     break;
187
188                 default:
189                     Error ("Syntax error");
190
191             }
192
193         } else {
194
195             A->AddrModeSet = AM65_ABS_LONG | AM65_ABS | AM65_DIR;
196
197         }
198     }
199
200     /* Apply addressing mode overrides */
201     A->AddrModeSet &= Restrictions;
202 }