]> Shamusworld >> Repos - rmac/blob - amode.c
divu.l/divs.l/mulu.l/muls.l debugged and condensed into one function
[rmac] / amode.c
1 //
2 // RMAC - Reboot's Macro Assembler for all Atari computers
3 // AMODE.C - Addressing Modes
4 // Copyright (C) 199x Landon Dyer, 2011-2017 Reboot and Friends
5 // RMAC derived from MADMAC v1.07 Written by Landon Dyer, 1986
6 // Source utilised with the kind permission of Landon Dyer
7 //
8
9 #include "amode.h"
10 #include "error.h"
11 #include "expr.h"
12 #include "mach.h"
13 #include "procln.h"
14 #include "rmac.h"
15 #include "sect.h"
16 #include "token.h"
17
18 #define DEF_KW
19 #include "kwtab.h"
20 #define DEF_MN
21 #include "mntab.h"
22
23 extern char unsupport[];
24
25 // Address-mode information
26 int nmodes;                                     // Number of addr'ing modes found
27 int am0;                                        // Addressing mode
28 int a0reg;                                      // Register
29 TOKEN a0expr[EXPRSIZE];         // Expression
30 uint64_t a0exval;                       // Expression's value
31 WORD a0exattr;                          // Expression's attribute
32 int a0ixreg;                            // Index register
33 int a0ixsiz;                            // Index register size (and scale)
34 TOKEN a0oexpr[EXPRSIZE];        // Outer displacement expression
35 uint64_t a0oexval;                      // Outer displacement value
36 WORD a0oexattr;                         // Outer displacement attribute
37 SYM * a0esym;                           // External symbol involved in expr
38 TOKEN a0bexpr[EXPRSIZE];        // Base displacement expression
39 uint64_t a0bexval;                      // Base displacement value
40 WORD a0bexattr;                         // Base displacement attribute
41 WORD a0bsize;                           // Base displacement size
42 WORD a0extension;                       // 020+ extension address word
43 WORD am0_030;                           // ea bits for 020+ addressing modes
44
45 int am1;                                        // Addressing mode
46 int a1reg;                                      // Register
47 TOKEN a1expr[EXPRSIZE];         // Expression
48 uint64_t a1exval;                       // Expression's value
49 WORD a1exattr;                          // Expression's attribute
50 int a1ixreg;                            // Index register
51 int a1ixsiz;                            // Index register size (and scale)
52 TOKEN a1oexpr[EXPRSIZE];        // Outer displacement expression
53 uint64_t a1oexval;                      // Outer displacement value
54 WORD a1oexattr;                         // Outer displacement attribute
55 SYM * a1esym;                           // External symbol involved in expr
56 TOKEN a1bexpr[EXPRSIZE];        // Base displacement expression
57 uint64_t a1bexval;                      // Base displacement value
58 WORD a1bexattr;                         // Base displacement attribute
59 WORD a1bsize;                           // Base displacement size
60 WORD a1extension;                       // 020+ extension address word
61 WORD am1_030;                           // ea bits for 020+ addressing modes
62
63 int a2reg;                                      // Register for div.l (68020+)
64
65 int bfparam1;                           // bfxxx / fmove instruction parameter 1
66 int bfparam2;                           // bfxxx / fmove instruction parameter 2
67 int bfval1;                                     // bfxxx / fmove value 1
68 int bfval2;                                     // bfxxx / fmove value 2
69 TOKEN bf0expr[EXPRSIZE];        // Expression
70 uint64_t bf0exval;                      // Expression's value
71 WORD bf0exattr;                         // Expression's attribute
72 SYM * bf0esym;                          // External symbol involved in expr
73
74 // Function prototypes
75 int check030bf(void);
76
77
78 //
79 // Parse addressing mode
80 //
81 int amode(int acount)
82 {
83         // Initialize global return values
84         nmodes = a0reg = a1reg = 0;
85         am0 = am1 = AM_NONE;
86         a0expr[0] = a0oexpr[0] = a1expr[0] = a1oexpr[0] = ENDEXPR;
87         a0exattr = a0oexattr = a1exattr = a1oexattr = 0;
88         a0esym = a1esym = NULL;
89         a0bexpr[0] = a1bexpr[0] = ENDEXPR;
90         a0bexval = a1bexval = 0;
91         a0bsize = a0extension = a1bsize = a1extension = 0;
92         am0_030 = am1_030 = 0;
93         bfparam1 = bfparam2 = 0;
94         bf0expr[0] = ENDEXPR;
95         bf0exattr = 0;
96         bf0esym = NULL;
97
98         // If at EOL, then no addr modes at all
99         if (*tok == EOL)
100                 return 0;
101
102         // Parse first addressing mode
103         #define AnOK      a0ok
104         #define AMn       am0
105         #define AnREG     a0reg
106         #define AnIXREG   a0ixreg
107         #define AnIXSIZ   a0ixsiz
108         #define AnEXPR    a0expr
109         #define AnEXVAL   a0exval
110         #define AnEXATTR  a0exattr
111         #define AnOEXPR   a0oexpr
112         #define AnOEXVAL  a0oexval
113         #define AnOEXATTR a0oexattr
114         #define AnESYM    a0esym
115         #define AMn_IX0   am0_ix0
116         #define AMn_IXN   am0_ixn
117         #define CHK_FOR_DISPn CheckForDisp0
118         #define AnBEXPR   a0bexpr
119         #define AnBEXVAL  a0bexval
120         #define AnBEXATTR a0bexattr
121         #define AnBZISE   a0bsize
122         #define AnEXTEN   a0extension
123         #define AMn_030   am0_030
124         #define IS_SUPPRESSEDn IS_SUPPRESSED0
125         #define CHECKODn CHECKOD0
126         #include "parmode.h"
127
128         // If caller wants only one mode, return just one (ignore comma);. If there
129         // is no second addressing mode (no comma), then return just one anyway.
130         nmodes = 1;
131
132         // it's a bitfield instruction--check the parameters inside the {} block
133         // for validity
134         if (*tok == '{')
135                 if (check030bf() == ERROR)
136                         return ERROR;
137
138         if ((acount == 0) || (*tok != ','))
139                 return 1;
140
141         // Eat the comma
142         tok++;
143
144         // Parse second addressing mode
145         #define AnOK      a1ok
146         #define AMn       am1
147         #define AnREG     a1reg
148         #define AnIXREG   a1ixreg
149         #define AnIXSIZ   a1ixsiz
150         #define AnEXPR    a1expr
151         #define AnEXVAL   a1exval
152         #define AnEXATTR  a1exattr
153         #define AnOEXPR   a1oexpr
154         #define AnOEXVAL  a1oexval
155         #define AnOEXATTR a1oexattr
156         #define AnESYM    a1esym
157         #define AMn_IX0   am1_ix0
158         #define AMn_IXN   am1_ixn
159         #define CHK_FOR_DISPn CheckForDisp1
160         #define AnBEXPR   a1bexpr
161         #define AnBEXVAL  a1bexval
162         #define AnBEXATTR a1bexattr
163         #define AnBZISE   a1bsize
164         #define AnEXTEN   a1extension
165         #define AMn_030   am1_030
166         #define IS_SUPPRESSEDn IS_SUPPRESSED1
167         #define CHECKODn CHECKOD1
168         #include "parmode.h"
169
170         // It's a bitfield instruction--check the parameters inside the {} block
171         // for validity
172         if (*tok == '{')
173         if (check030bf() == ERROR)
174                 return ERROR;
175
176         // At this point, it is legal for 020+ to have a ':'. For example divu.l
177         // d0,d2:d3
178         if (*tok == ':')
179         {
180                 if ((activecpu & (CPU_68020 | CPU_68030 | CPU_68040)) == 0)
181                         return error(unsupport);
182
183                 // TODO: protect this from combinations like Dx:FPx etc :)
184                 tok++;  //eat the colon
185
186                 if ((*tok >= KW_D0) && (*tok <= KW_D7))
187                 {
188                         a2reg = (*tok++) & 7;
189                 }
190                 else if ((*tok >= KW_FP0) && (*tok <= KW_FP7))
191                 {
192                         a2reg = (*tok++) & 7;
193                 }
194                 else
195                         return error("a data or FPU register must follow a :");
196         }
197         else
198         {
199                 // If no ':' is present then maybe we have something like divs.l d0,d1
200                 // which sould translate to divs.l d0,d1:d1
201                 a2reg = a1reg;
202         }
203
204         nmodes = 2;
205         return 2;
206
207         // Error messages:
208 badmode:
209         return error("addressing mode syntax");
210
211         //unmode:
212         //return error("unimplemented addressing mode");
213 }
214
215
216 //
217 // Parse register list
218 //
219 int reglist(WORD * a_rmask)
220 {
221         static WORD msktab[] = {
222                 0x0001, 0x0002, 0x0004, 0x0008,
223                 0x0010, 0x0020, 0x0040, 0x0080,
224                 0x0100, 0x0200, 0x0400, 0x0800,
225                 0x1000, 0x2000, 0x4000, 0x8000
226         };
227
228         WORD rmask = 0;
229         int r, cnt;
230
231         for(;;)
232         {
233                 if ((*tok >= KW_D0) && (*tok <= KW_A7))
234                         r = *tok++ & 0x0F;
235                 else
236                         break;
237
238                 if (*tok == '-')
239                 {
240                         tok++;
241
242                         if ((*tok >= KW_D0) && (*tok <= KW_A7))
243                                 cnt = *tok++ & 0x0F;
244                         else
245                                 return error("register list syntax");
246
247                         if (cnt < r)
248                                 return error("register list order");
249
250                         cnt -= r;
251                 }
252                 else
253                         cnt = 0;
254
255                 while (cnt-- >= 0)
256                         rmask |= msktab[r++];
257
258                 if (*tok != '/')
259                         break;
260
261                 tok++;
262         }
263
264         *a_rmask = rmask;
265
266         return OK;
267 }
268
269
270 //
271 // Parse FPU register list
272 //
273 int fpu_reglist_left(WORD * a_rmask)
274 {
275         static WORD msktab_minus[] = {
276                 0x0080, 0x0040, 0x0020, 0x0010,
277                 0x0008, 0x0004, 0x0002, 0x0001
278         };
279
280         WORD rmask = 0;
281         int r, cnt;
282
283         for(;;)
284         {
285                 if ((*tok >= KW_FP0) && (*tok <= KW_FP7))
286                         r = *tok++ & 0x07;
287                 else
288                         break;
289
290                 if (*tok == '-')
291                 {
292                         tok++;
293
294                         if ((*tok >= KW_FP0) && (*tok <= KW_FP7))
295                                 cnt = *tok++ & 0x07;
296                         else
297                                 return error("register list syntax");
298
299                         if (cnt < r)
300                                 return error("register list order");
301
302                         cnt -= r;
303                 }
304                 else
305                         cnt = 0;
306
307                 r = 0;
308
309                 while (cnt-- >= 0)
310                         rmask |= msktab_minus[r++];
311
312                 if (*tok != '/')
313                         break;
314
315                 tok++;
316         }
317
318         *a_rmask = rmask;
319
320         return OK;
321 }
322
323
324 int fpu_reglist_right(WORD * a_rmask)
325 {
326         static WORD msktab_plus[] = {
327                 0x0001, 0x0002, 0x0004, 0x0008,
328                 0x0010, 0x0020, 0x0040, 0x0080
329         };
330
331         WORD rmask = 0;
332         int r, cnt;
333
334         for(;;)
335         {
336                 if ((*tok >= KW_FP0) && (*tok <= KW_FP7))
337                         r = *tok++ & 0x07;
338                 else
339                         break;
340
341                 if (*tok == '-')
342                 {
343                         tok++;
344
345                         if ((*tok >= KW_FP0) && (*tok <= KW_FP7))
346                                 cnt = *tok++ & 0x07;
347                         else
348                                 return error("register list syntax");
349
350                         if (cnt < r)
351                                 return error("register list order");
352
353                         cnt -= r;
354                 }
355                 else
356                         cnt = 0;
357
358                 while (cnt-- >= 0)
359                         rmask |= msktab_plus[r++];
360
361                 if (*tok != '/')
362                         break;
363
364                 tok++;
365         }
366
367         *a_rmask = rmask;
368
369         return OK;
370 }
371
372
373 //
374 // Check for bitfield instructions extra params
375 // These are 020+ instructions and have the following syntax:
376 // bfxxx <ea>{param1,param2}
377 // param1/2 are either data registers or immediate values
378 //
379 int check030bf(void)
380 {
381         PTR tp;
382         CHECK00;
383         tok++;
384
385         if (*tok == CONST)
386         {
387                 tp.u32 = tok + 1;
388                 bfval1 = (int)*tp.u64++;
389                 tok = tp.u32;
390
391                 // Do=0, offset=immediate - shift it to place
392                 bfparam1 = (0 << 11);
393         }
394         else if (*tok == SYMBOL)
395         {
396                 if (expr(bf0expr, &bf0exval, &bf0exattr, &bf0esym) != OK)
397                         return ERROR;
398
399                 if (!(bf0exattr & DEFINED))
400                         return error("bfxxx offset: immediate value must evaluate");
401
402                 bfval1 = (int)bf0exval;
403
404                 // Do=0, offset=immediate - shift it to place
405                 bfparam1 = (0 << 11);
406         }
407         else if ((*tok >= KW_D0) && (*tok <= KW_D7))
408         {
409                 // Do=1, offset=data register - shift it to place
410                 bfparam1 = (1 << 11);
411                 bfval1 = (*(int *)tok - 128);
412                 tok++;
413         }
414         else
415                 return ERROR;
416
417         // Eat the ':', if any
418         if (*tok == ':')
419                 tok++;
420
421         if (*tok == '}' && tok[1] == EOL)
422         {
423                 // It is ok to have }, EOL here - it might be "fmove fpn,<ea> {dx}"
424                 tok++;
425                 return OK;
426         }
427
428         if (*tok == CONST)
429         {
430                 tp.u32 = tok + 1;
431                 bfval2 = (int)*tp.u64++;
432                 tok = tp.u32;
433
434                 // Do=0, offset=immediate - shift it to place
435                 bfparam2 = (0 << 5);
436         }
437         else if (*tok == SYMBOL)
438         {
439                 if (expr(bf0expr, &bf0exval, &bf0exattr, &bf0esym) != OK)
440                         return ERROR;
441
442                 bfval2 = (int)bf0exval;
443
444                 if (!(bf0exattr & DEFINED))
445                         return error("bfxxx width: immediate value must evaluate");
446
447                 // Do=0, offset=immediate - shift it to place
448                 bfparam2 = (0 << 5);
449         }
450         else if ((*tok >= KW_D0) && (*tok <= KW_D7))
451         {
452                 // Do=1, offset=data register - shift it to place
453                 bfval2 = (*(int *)tok - 128);
454                 bfparam2 = (1 << 5);
455                 tok++;
456         }
457         else
458                 return ERROR;
459
460         tok++;  // Eat the '}'
461
462         return OK;
463 }
464