xref: /openbsd-src/gnu/usr.bin/binutils/gas/config/m68k-parse.y (revision 007c2a4539b8b8aaa95c5e73e77620090abe113b)
1 /* m68k.y -- bison grammar for m68k operand parsing
2    Copyright 1995, 1996, 1997, 1998, 2001 Free Software Foundation, Inc.
3    Written by Ken Raeburn and Ian Lance Taylor, Cygnus Support
4 
5    This file is part of GAS, the GNU Assembler.
6 
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11 
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21 
22 /* This file holds a bison grammar to parse m68k operands.  The m68k
23    has a complicated operand syntax, and gas supports two main
24    variations of it.  Using a grammar is probably overkill, but at
25    least it makes clear exactly what we do support.  */
26 
27 %{
28 
29 #include "as.h"
30 #include "tc-m68k.h"
31 #include "m68k-parse.h"
32 #include "safe-ctype.h"
33 
34 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
35    etc), as well as gratuitously global symbol names If other parser
36    generators (bison, byacc, etc) produce additional global names that
37    conflict at link time, then those parser generators need to be
38    fixed instead of adding those names to this list. */
39 
40 #define	yymaxdepth m68k_maxdepth
41 #define	yyparse	m68k_parse
42 #define	yylex	m68k_lex
43 #define	yyerror	m68k_error
44 #define	yylval	m68k_lval
45 #define	yychar	m68k_char
46 #define	yydebug	m68k_debug
47 #define	yypact	m68k_pact
48 #define	yyr1	m68k_r1
49 #define	yyr2	m68k_r2
50 #define	yydef	m68k_def
51 #define	yychk	m68k_chk
52 #define	yypgo	m68k_pgo
53 #define	yyact	m68k_act
54 #define	yyexca	m68k_exca
55 #define yyerrflag m68k_errflag
56 #define yynerrs	m68k_nerrs
57 #define	yyps	m68k_ps
58 #define	yypv	m68k_pv
59 #define	yys	m68k_s
60 #define	yy_yys	m68k_yys
61 #define	yystate	m68k_state
62 #define	yytmp	m68k_tmp
63 #define	yyv	m68k_v
64 #define	yy_yyv	m68k_yyv
65 #define	yyval	m68k_val
66 #define	yylloc	m68k_lloc
67 #define yyreds	m68k_reds		/* With YYDEBUG defined */
68 #define yytoks	m68k_toks		/* With YYDEBUG defined */
69 #define yylhs	m68k_yylhs
70 #define yylen	m68k_yylen
71 #define yydefred m68k_yydefred
72 #define yydgoto	m68k_yydgoto
73 #define yysindex m68k_yysindex
74 #define yyrindex m68k_yyrindex
75 #define yygindex m68k_yygindex
76 #define yytable	 m68k_yytable
77 #define yycheck	 m68k_yycheck
78 
79 #ifndef YYDEBUG
80 #define YYDEBUG 1
81 #endif
82 
83 /* Internal functions.  */
84 
85 static enum m68k_register m68k_reg_parse PARAMS ((char **));
86 static int yylex PARAMS ((void));
87 static void yyerror PARAMS ((const char *));
88 
89 /* The parser sets fields pointed to by this global variable.  */
90 static struct m68k_op *op;
91 
92 %}
93 
94 %union
95 {
96   struct m68k_indexreg indexreg;
97   enum m68k_register reg;
98   struct m68k_exp exp;
99   unsigned long mask;
100   int onereg;
101 }
102 
103 %token <reg> DR AR FPR FPCR LPC ZAR ZDR LZPC CREG
104 %token <indexreg> INDEXREG
105 %token <exp> EXPR
106 
107 %type <indexreg> zireg zdireg
108 %type <reg> zadr zdr apc zapc zpc optzapc optczapc
109 %type <exp> optcexpr optexprc
110 %type <mask> reglist ireglist reglistpair
111 %type <onereg> reglistreg
112 
113 %%
114 
115 /* An operand.  */
116 
117 operand:
118 	  generic_operand
119 	| motorola_operand
120 	| mit_operand
121 	;
122 
123 /* A generic operand.  */
124 
125 generic_operand:
126 	  DR
127 		{
128 		  op->mode = DREG;
129 		  op->reg = $1;
130 		}
131 	| AR
132 		{
133 		  op->mode = AREG;
134 		  op->reg = $1;
135 		}
136 	| FPR
137 		{
138 		  op->mode = FPREG;
139 		  op->reg = $1;
140 		}
141 	| FPCR
142 		{
143 		  op->mode = CONTROL;
144 		  op->reg = $1;
145 		}
146 	| CREG
147 		{
148 		  op->mode = CONTROL;
149 		  op->reg = $1;
150 		}
151 	| EXPR
152 		{
153 		  op->mode = ABSL;
154 		  op->disp = $1;
155 		}
156 	| '#' EXPR
157 		{
158 		  op->mode = IMMED;
159 		  op->disp = $2;
160 		}
161 	| '&' EXPR
162 		{
163 		  op->mode = IMMED;
164 		  op->disp = $2;
165 		}
166 	| reglist
167 		{
168 		  op->mode = REGLST;
169 		  op->mask = $1;
170 		}
171 	;
172 
173 /* An operand in Motorola syntax.  This includes MRI syntax as well,
174    which may or may not be different in that it permits commutativity
175    of index and base registers, and permits an offset expression to
176    appear inside or outside of the parentheses.  */
177 
178 motorola_operand:
179 	  '(' AR ')'
180 		{
181 		  op->mode = AINDR;
182 		  op->reg = $2;
183 		}
184 	| '(' AR ')' '+'
185 		{
186 		  op->mode = AINC;
187 		  op->reg = $2;
188 		}
189 	| '-' '(' AR ')'
190 		{
191 		  op->mode = ADEC;
192 		  op->reg = $3;
193 		}
194 	| '(' EXPR ',' zapc ')'
195 		{
196 		  op->reg = $4;
197 		  op->disp = $2;
198 		  if (($4 >= ZADDR0 && $4 <= ZADDR7)
199 		      || $4 == ZPC)
200 		    op->mode = BASE;
201 		  else
202 		    op->mode = DISP;
203 		}
204 	| '(' zapc ',' EXPR ')'
205 		{
206 		  op->reg = $2;
207 		  op->disp = $4;
208 		  if (($2 >= ZADDR0 && $2 <= ZADDR7)
209 		      || $2 == ZPC)
210 		    op->mode = BASE;
211 		  else
212 		    op->mode = DISP;
213 		}
214 	| EXPR '(' zapc ')'
215 		{
216 		  op->reg = $3;
217 		  op->disp = $1;
218 		  if (($3 >= ZADDR0 && $3 <= ZADDR7)
219 		      || $3 == ZPC)
220 		    op->mode = BASE;
221 		  else
222 		    op->mode = DISP;
223 		}
224 	| '(' LPC ')'
225 		{
226 		  op->mode = DISP;
227 		  op->reg = $2;
228 		}
229 	| '(' ZAR ')'
230 		{
231 		  op->mode = BASE;
232 		  op->reg = $2;
233 		}
234 	| '(' LZPC ')'
235 		{
236 		  op->mode = BASE;
237 		  op->reg = $2;
238 		}
239 	| '(' EXPR ',' zapc ',' zireg ')'
240 		{
241 		  op->mode = BASE;
242 		  op->reg = $4;
243 		  op->disp = $2;
244 		  op->index = $6;
245 		}
246 	| '(' EXPR ',' zapc ',' zpc ')'
247 		{
248 		  if ($4 == PC || $4 == ZPC)
249 		    yyerror (_("syntax error"));
250 		  op->mode = BASE;
251 		  op->reg = $6;
252 		  op->disp = $2;
253 		  op->index.reg = $4;
254 		  op->index.size = SIZE_UNSPEC;
255 		  op->index.scale = 1;
256 		}
257 	| '(' EXPR ',' zdireg optczapc ')'
258 		{
259 		  op->mode = BASE;
260 		  op->reg = $5;
261 		  op->disp = $2;
262 		  op->index = $4;
263 		}
264 	| '(' zdireg ',' EXPR ')'
265 		{
266 		  op->mode = BASE;
267 		  op->disp = $4;
268 		  op->index = $2;
269 		}
270 	| EXPR '(' zapc ',' zireg ')'
271 		{
272 		  op->mode = BASE;
273 		  op->reg = $3;
274 		  op->disp = $1;
275 		  op->index = $5;
276 		}
277 	| '(' zapc ',' zireg ')'
278 		{
279 		  op->mode = BASE;
280 		  op->reg = $2;
281 		  op->index = $4;
282 		}
283 	| EXPR '(' zapc ',' zpc ')'
284 		{
285 		  if ($3 == PC || $3 == ZPC)
286 		    yyerror (_("syntax error"));
287 		  op->mode = BASE;
288 		  op->reg = $5;
289 		  op->disp = $1;
290 		  op->index.reg = $3;
291 		  op->index.size = SIZE_UNSPEC;
292 		  op->index.scale = 1;
293 		}
294 	| '(' zapc ',' zpc ')'
295 		{
296 		  if ($2 == PC || $2 == ZPC)
297 		    yyerror (_("syntax error"));
298 		  op->mode = BASE;
299 		  op->reg = $4;
300 		  op->index.reg = $2;
301 		  op->index.size = SIZE_UNSPEC;
302 		  op->index.scale = 1;
303 		}
304 	| EXPR '(' zdireg optczapc ')'
305 		{
306 		  op->mode = BASE;
307 		  op->reg = $4;
308 		  op->disp = $1;
309 		  op->index = $3;
310 		}
311 	| '(' zdireg optczapc ')'
312 		{
313 		  op->mode = BASE;
314 		  op->reg = $3;
315 		  op->index = $2;
316 		}
317 	| '(' '[' EXPR optczapc ']' ',' zireg optcexpr ')'
318 		{
319 		  op->mode = POST;
320 		  op->reg = $4;
321 		  op->disp = $3;
322 		  op->index = $7;
323 		  op->odisp = $8;
324 		}
325 	| '(' '[' EXPR optczapc ']' optcexpr ')'
326 		{
327 		  op->mode = POST;
328 		  op->reg = $4;
329 		  op->disp = $3;
330 		  op->odisp = $6;
331 		}
332 	| '(' '[' zapc ']' ',' zireg optcexpr ')'
333 		{
334 		  op->mode = POST;
335 		  op->reg = $3;
336 		  op->index = $6;
337 		  op->odisp = $7;
338 		}
339 	| '(' '[' zapc ']' optcexpr ')'
340 		{
341 		  op->mode = POST;
342 		  op->reg = $3;
343 		  op->odisp = $5;
344 		}
345 	| '(' '[' EXPR ',' zapc ',' zireg ']' optcexpr ')'
346 		{
347 		  op->mode = PRE;
348 		  op->reg = $5;
349 		  op->disp = $3;
350 		  op->index = $7;
351 		  op->odisp = $9;
352 		}
353 	| '(' '[' zapc ',' zireg ']' optcexpr ')'
354 		{
355 		  op->mode = PRE;
356 		  op->reg = $3;
357 		  op->index = $5;
358 		  op->odisp = $7;
359 		}
360 	| '(' '[' EXPR ',' zapc ',' zpc ']' optcexpr ')'
361 		{
362 		  if ($5 == PC || $5 == ZPC)
363 		    yyerror (_("syntax error"));
364 		  op->mode = PRE;
365 		  op->reg = $7;
366 		  op->disp = $3;
367 		  op->index.reg = $5;
368 		  op->index.size = SIZE_UNSPEC;
369 		  op->index.scale = 1;
370 		  op->odisp = $9;
371 		}
372 	| '(' '[' zapc ',' zpc ']' optcexpr ')'
373 		{
374 		  if ($3 == PC || $3 == ZPC)
375 		    yyerror (_("syntax error"));
376 		  op->mode = PRE;
377 		  op->reg = $5;
378 		  op->index.reg = $3;
379 		  op->index.size = SIZE_UNSPEC;
380 		  op->index.scale = 1;
381 		  op->odisp = $7;
382 		}
383 	| '(' '[' optexprc zdireg optczapc ']' optcexpr ')'
384 		{
385 		  op->mode = PRE;
386 		  op->reg = $5;
387 		  op->disp = $3;
388 		  op->index = $4;
389 		  op->odisp = $7;
390 		}
391 	;
392 
393 /* An operand in MIT syntax.  */
394 
395 mit_operand:
396 	  optzapc '@'
397 		{
398 		  /* We use optzapc to avoid a shift/reduce conflict.  */
399 		  if ($1 < ADDR0 || $1 > ADDR7)
400 		    yyerror (_("syntax error"));
401 		  op->mode = AINDR;
402 		  op->reg = $1;
403 		}
404 	| optzapc '@' '+'
405 		{
406 		  /* We use optzapc to avoid a shift/reduce conflict.  */
407 		  if ($1 < ADDR0 || $1 > ADDR7)
408 		    yyerror (_("syntax error"));
409 		  op->mode = AINC;
410 		  op->reg = $1;
411 		}
412 	| optzapc '@' '-'
413 		{
414 		  /* We use optzapc to avoid a shift/reduce conflict.  */
415 		  if ($1 < ADDR0 || $1 > ADDR7)
416 		    yyerror (_("syntax error"));
417 		  op->mode = ADEC;
418 		  op->reg = $1;
419 		}
420 	| optzapc '@' '(' EXPR ')'
421 		{
422 		  op->reg = $1;
423 		  op->disp = $4;
424 		  if (($1 >= ZADDR0 && $1 <= ZADDR7)
425 		      || $1 == ZPC)
426 		    op->mode = BASE;
427 		  else
428 		    op->mode = DISP;
429 		}
430 	| optzapc '@' '(' optexprc zireg ')'
431 		{
432 		  op->mode = BASE;
433 		  op->reg = $1;
434 		  op->disp = $4;
435 		  op->index = $5;
436 		}
437 	| optzapc '@' '(' EXPR ')' '@' '(' optexprc zireg ')'
438 		{
439 		  op->mode = POST;
440 		  op->reg = $1;
441 		  op->disp = $4;
442 		  op->index = $9;
443 		  op->odisp = $8;
444 		}
445 	| optzapc '@' '(' EXPR ')' '@' '(' EXPR ')'
446 		{
447 		  op->mode = POST;
448 		  op->reg = $1;
449 		  op->disp = $4;
450 		  op->odisp = $8;
451 		}
452 	| optzapc '@' '(' optexprc zireg ')' '@' '(' EXPR ')'
453 		{
454 		  op->mode = PRE;
455 		  op->reg = $1;
456 		  op->disp = $4;
457 		  op->index = $5;
458 		  op->odisp = $9;
459 		}
460 	;
461 
462 /* An index register, possibly suppressed, which need not have a size
463    or scale.  */
464 
465 zireg:
466 	  INDEXREG
467 	| zadr
468 		{
469 		  $$.reg = $1;
470 		  $$.size = SIZE_UNSPEC;
471 		  $$.scale = 1;
472 		}
473 	;
474 
475 /* A register which may be an index register, but which may not be an
476    address register.  This nonterminal is used to avoid ambiguity when
477    trying to parse something like (0,d5,a6) as compared to (0,a6,d5).  */
478 
479 zdireg:
480 	  INDEXREG
481 	| zdr
482 		{
483 		  $$.reg = $1;
484 		  $$.size = SIZE_UNSPEC;
485 		  $$.scale = 1;
486 		}
487 	;
488 
489 /* An address or data register, or a suppressed address or data
490    register.  */
491 
492 zadr:
493 	  zdr
494 	| AR
495 	| ZAR
496 	;
497 
498 /* A data register which may be suppressed.  */
499 
500 zdr:
501 	  DR
502 	| ZDR
503 	;
504 
505 /* Either an address register or the PC.  */
506 
507 apc:
508 	  AR
509 	| LPC
510 	;
511 
512 /* Either an address register, or the PC, or a suppressed address
513    register, or a suppressed PC.  */
514 
515 zapc:
516 	  apc
517 	| LZPC
518 	| ZAR
519 	;
520 
521 /* An optional zapc.  */
522 
523 optzapc:
524 	  /* empty */
525 		{
526 		  $$ = ZADDR0;
527 		}
528 	| zapc
529 	;
530 
531 /* The PC, optionally suppressed.  */
532 
533 zpc:
534 	  LPC
535 	| LZPC
536 	;
537 
538 /* ',' zapc when it may be omitted.  */
539 
540 optczapc:
541 	  /* empty */
542 		{
543 		  $$ = ZADDR0;
544 		}
545 	| ',' zapc
546 		{
547 		  $$ = $2;
548 		}
549 	;
550 
551 /* ',' EXPR when it may be omitted.  */
552 
553 optcexpr:
554 	  /* empty */
555 		{
556 		  $$.exp.X_op = O_absent;
557 		  $$.size = SIZE_UNSPEC;
558 		}
559 	| ',' EXPR
560 		{
561 		  $$ = $2;
562 		}
563 	;
564 
565 /* EXPR ',' when it may be omitted.  */
566 
567 optexprc:
568 	  /* empty */
569 		{
570 		  $$.exp.X_op = O_absent;
571 		  $$.size = SIZE_UNSPEC;
572 		}
573 	| EXPR ','
574 		{
575 		  $$ = $1;
576 		}
577 	;
578 
579 /* A register list for the movem instruction.  */
580 
581 reglist:
582 	  reglistpair
583 	| reglistpair '/' ireglist
584 		{
585 		  $$ = $1 | $3;
586 		}
587 	| reglistreg '/' ireglist
588 		{
589 		  $$ = (1 << $1) | $3;
590 		}
591 	;
592 
593 /* We use ireglist when we know we are looking at a reglist, and we
594    can safely reduce a simple register to reglistreg.  If we permitted
595    reglist to reduce to reglistreg, it would be ambiguous whether a
596    plain register were a DREG/AREG/FPREG or a REGLST.  */
597 
598 ireglist:
599 	  reglistreg
600 		{
601 		  $$ = 1 << $1;
602 		}
603 	| reglistpair
604 	| reglistpair '/' ireglist
605 		{
606 		  $$ = $1 | $3;
607 		}
608 	| reglistreg '/' ireglist
609 		{
610 		  $$ = (1 << $1) | $3;
611 		}
612 	;
613 
614 reglistpair:
615 	  reglistreg '-' reglistreg
616 		{
617 		  if ($1 <= $3)
618 		    $$ = (1 << ($3 + 1)) - 1 - ((1 << $1) - 1);
619 		  else
620 		    $$ = (1 << ($1 + 1)) - 1 - ((1 << $3) - 1);
621 		}
622 	;
623 
624 reglistreg:
625 	  DR
626 		{
627 		  $$ = $1 - DATA0;
628 		}
629 	| AR
630 		{
631 		  $$ = $1 - ADDR0 + 8;
632 		}
633 	| FPR
634 		{
635 		  $$ = $1 - FP0 + 16;
636 		}
637 	| FPCR
638 		{
639 		  if ($1 == FPI)
640 		    $$ = 24;
641 		  else if ($1 == FPS)
642 		    $$ = 25;
643 		  else
644 		    $$ = 26;
645 		}
646 	;
647 
648 %%
649 
650 /* The string to parse is stored here, and modified by yylex.  */
651 
652 static char *str;
653 
654 /* The original string pointer.  */
655 
656 static char *strorig;
657 
658 /* If *CCP could be a register, return the register number and advance
659    *CCP.  Otherwise don't change *CCP, and return 0.  */
660 
661 static enum m68k_register
m68k_reg_parse(ccp)662 m68k_reg_parse (ccp)
663      register char **ccp;
664 {
665   char *start = *ccp;
666   char c;
667   char *p;
668   symbolS *symbolp;
669 
670   if (flag_reg_prefix_optional)
671     {
672       if (*start == REGISTER_PREFIX)
673 	start++;
674       p = start;
675     }
676   else
677     {
678       if (*start != REGISTER_PREFIX)
679 	return 0;
680       p = start + 1;
681     }
682 
683   if (! is_name_beginner (*p))
684     return 0;
685 
686   p++;
687   while (is_part_of_name (*p) && *p != '.' && *p != ':' && *p != '*')
688     p++;
689 
690   c = *p;
691   *p = 0;
692   symbolp = symbol_find (start);
693   *p = c;
694 
695   if (symbolp != NULL && S_GET_SEGMENT (symbolp) == reg_section)
696     {
697       *ccp = p;
698       return S_GET_VALUE (symbolp);
699     }
700 
701   /* In MRI mode, something like foo.bar can be equated to a register
702      name.  */
703   while (flag_mri && c == '.')
704     {
705       ++p;
706       while (is_part_of_name (*p) && *p != '.' && *p != ':' && *p != '*')
707 	p++;
708       c = *p;
709       *p = '\0';
710       symbolp = symbol_find (start);
711       *p = c;
712       if (symbolp != NULL && S_GET_SEGMENT (symbolp) == reg_section)
713 	{
714 	  *ccp = p;
715 	  return S_GET_VALUE (symbolp);
716 	}
717     }
718 
719   return 0;
720 }
721 
722 /* The lexer.  */
723 
724 static int
yylex()725 yylex ()
726 {
727   enum m68k_register reg;
728   char *s;
729   int parens;
730   int c = 0;
731   int tail = 0;
732   char *hold;
733 
734   if (*str == ' ')
735     ++str;
736 
737   if (*str == '\0')
738     return 0;
739 
740   /* Various special characters are just returned directly.  */
741   switch (*str)
742     {
743     case '@':
744       /* In MRI mode, this can be the start of an octal number.  */
745       if (flag_mri)
746 	{
747 	  if (ISDIGIT (str[1])
748 	      || ((str[1] == '+' || str[1] == '-')
749 		  && ISDIGIT (str[2])))
750 	    break;
751 	}
752       /* Fall through.  */
753     case '#':
754     case '&':
755     case ',':
756     case ')':
757     case '/':
758     case '[':
759     case ']':
760       return *str++;
761     case '+':
762       /* It so happens that a '+' can only appear at the end of an
763          operand.  If it appears anywhere else, it must be a unary
764          plus on an expression.  */
765       if (str[1] == '\0')
766 	return *str++;
767       break;
768     case '-':
769       /* A '-' can only appear in -(ar), rn-rn, or ar@-.  If it
770          appears anywhere else, it must be a unary minus on an
771          expression.  */
772       if (str[1] == '\0')
773 	return *str++;
774       s = str + 1;
775       if (*s == '(')
776 	++s;
777       if (m68k_reg_parse (&s) != 0)
778 	return *str++;
779       break;
780     case '(':
781       /* A '(' can only appear in `(reg)', `(expr,...', `([', `@(', or
782          `)('.  If it appears anywhere else, it must be starting an
783          expression.  */
784       if (str[1] == '['
785 	  || (str > strorig
786 	      && (str[-1] == '@'
787 		  || str[-1] == ')')))
788 	return *str++;
789       s = str + 1;
790       if (m68k_reg_parse (&s) != 0)
791 	return *str++;
792       /* Check for the case of '(expr,...' by scanning ahead.  If we
793          find a comma outside of balanced parentheses, we return '('.
794          If we find an unbalanced right parenthesis, then presumably
795          the '(' really starts an expression.  */
796       parens = 0;
797       for (s = str + 1; *s != '\0'; s++)
798 	{
799 	  if (*s == '(')
800 	    ++parens;
801 	  else if (*s == ')')
802 	    {
803 	      if (parens == 0)
804 		break;
805 	      --parens;
806 	    }
807 	  else if (*s == ',' && parens == 0)
808 	    {
809 	      /* A comma can not normally appear in an expression, so
810 		 this is a case of '(expr,...'.  */
811 	      return *str++;
812 	    }
813 	}
814     }
815 
816   /* See if it's a register.  */
817 
818   reg = m68k_reg_parse (&str);
819   if (reg != 0)
820     {
821       int ret;
822 
823       yylval.reg = reg;
824 
825       if (reg >= DATA0 && reg <= DATA7)
826 	ret = DR;
827       else if (reg >= ADDR0 && reg <= ADDR7)
828 	ret = AR;
829       else if (reg >= FP0 && reg <= FP7)
830 	return FPR;
831       else if (reg == FPI
832 	       || reg == FPS
833 	       || reg == FPC)
834 	return FPCR;
835       else if (reg == PC)
836 	return LPC;
837       else if (reg >= ZDATA0 && reg <= ZDATA7)
838 	ret = ZDR;
839       else if (reg >= ZADDR0 && reg <= ZADDR7)
840 	ret = ZAR;
841       else if (reg == ZPC)
842 	return LZPC;
843       else
844 	return CREG;
845 
846       /* If we get here, we have a data or address register.  We
847 	 must check for a size or scale; if we find one, we must
848 	 return INDEXREG.  */
849 
850       s = str;
851 
852       if (*s != '.' && *s != ':' && *s != '*')
853 	return ret;
854 
855       yylval.indexreg.reg = reg;
856 
857       if (*s != '.' && *s != ':')
858 	yylval.indexreg.size = SIZE_UNSPEC;
859       else
860 	{
861 	  ++s;
862 	  switch (*s)
863 	    {
864 	    case 'w':
865 	    case 'W':
866 	      yylval.indexreg.size = SIZE_WORD;
867 	      ++s;
868 	      break;
869 	    case 'l':
870 	    case 'L':
871 	      yylval.indexreg.size = SIZE_LONG;
872 	      ++s;
873 	      break;
874 	    default:
875 	      yyerror (_("illegal size specification"));
876 	      yylval.indexreg.size = SIZE_UNSPEC;
877 	      break;
878 	    }
879 	}
880 
881       yylval.indexreg.scale = 1;
882 
883       if (*s == '*' || *s == ':')
884 	{
885 	  expressionS scale;
886 
887 	  ++s;
888 
889 	  hold = input_line_pointer;
890 	  input_line_pointer = s;
891 	  expression (&scale);
892 	  s = input_line_pointer;
893 	  input_line_pointer = hold;
894 
895 	  if (scale.X_op != O_constant)
896 	    yyerror (_("scale specification must resolve to a number"));
897 	  else
898 	    {
899 	      switch (scale.X_add_number)
900 		{
901 		case 1:
902 		case 2:
903 		case 4:
904 		case 8:
905 		  yylval.indexreg.scale = scale.X_add_number;
906 		  break;
907 		default:
908 		  yyerror (_("invalid scale value"));
909 		  break;
910 		}
911 	    }
912 	}
913 
914       str = s;
915 
916       return INDEXREG;
917     }
918 
919   /* It must be an expression.  Before we call expression, we need to
920      look ahead to see if there is a size specification.  We must do
921      that first, because otherwise foo.l will be treated as the symbol
922      foo.l, rather than as the symbol foo with a long size
923      specification.  The grammar requires that all expressions end at
924      the end of the operand, or with ',', '(', ']', ')'.  */
925 
926   parens = 0;
927   for (s = str; *s != '\0'; s++)
928     {
929       if (*s == '(')
930 	{
931 	  if (parens == 0
932 	      && s > str
933 	      && (s[-1] == ')' || ISALNUM (s[-1])))
934 	    break;
935 	  ++parens;
936 	}
937       else if (*s == ')')
938 	{
939 	  if (parens == 0)
940 	    break;
941 	  --parens;
942 	}
943       else if (parens == 0
944 	       && (*s == ',' || *s == ']'))
945 	break;
946     }
947 
948   yylval.exp.size = SIZE_UNSPEC;
949   if (s <= str + 2
950       || (s[-2] != '.' && s[-2] != ':'))
951     tail = 0;
952   else
953     {
954       switch (s[-1])
955 	{
956 	case 's':
957 	case 'S':
958 	case 'b':
959 	case 'B':
960 	  yylval.exp.size = SIZE_BYTE;
961 	  break;
962 	case 'w':
963 	case 'W':
964 	  yylval.exp.size = SIZE_WORD;
965 	  break;
966 	case 'l':
967 	case 'L':
968 	  yylval.exp.size = SIZE_LONG;
969 	  break;
970 	default:
971 	  break;
972 	}
973       if (yylval.exp.size != SIZE_UNSPEC)
974 	tail = 2;
975     }
976 
977 #ifdef OBJ_ELF
978   {
979     /* Look for @PLTPC, etc.  */
980     char *cp;
981 
982     yylval.exp.pic_reloc = pic_none;
983     cp = s - tail;
984     if (cp - 6 > str && cp[-6] == '@')
985       {
986 	if (strncmp (cp - 6, "@PLTPC", 6) == 0)
987 	  {
988 	    yylval.exp.pic_reloc = pic_plt_pcrel;
989 	    tail += 6;
990 	  }
991 	else if (strncmp (cp - 6, "@GOTPC", 6) == 0)
992 	  {
993 	    yylval.exp.pic_reloc = pic_got_pcrel;
994 	    tail += 6;
995 	  }
996       }
997     else if (cp - 4 > str && cp[-4] == '@')
998       {
999 	if (strncmp (cp - 4, "@PLT", 4) == 0)
1000 	  {
1001 	    yylval.exp.pic_reloc = pic_plt_off;
1002 	    tail += 4;
1003 	  }
1004 	else if (strncmp (cp - 4, "@GOT", 4) == 0)
1005 	  {
1006 	    yylval.exp.pic_reloc = pic_got_off;
1007 	    tail += 4;
1008 	  }
1009       }
1010   }
1011 #endif
1012 
1013   if (tail != 0)
1014     {
1015       c = s[-tail];
1016       s[-tail] = 0;
1017     }
1018 
1019   hold = input_line_pointer;
1020   input_line_pointer = str;
1021   expression (&yylval.exp.exp);
1022   str = input_line_pointer;
1023   input_line_pointer = hold;
1024 
1025   if (tail != 0)
1026     {
1027       s[-tail] = c;
1028       str = s;
1029     }
1030 
1031   return EXPR;
1032 }
1033 
1034 /* Parse an m68k operand.  This is the only function which is called
1035    from outside this file.  */
1036 
1037 int
m68k_ip_op(s,oparg)1038 m68k_ip_op (s, oparg)
1039      char *s;
1040      struct m68k_op *oparg;
1041 {
1042   memset (oparg, 0, sizeof *oparg);
1043   oparg->error = NULL;
1044   oparg->index.reg = ZDATA0;
1045   oparg->index.scale = 1;
1046   oparg->disp.exp.X_op = O_absent;
1047   oparg->odisp.exp.X_op = O_absent;
1048 
1049   str = strorig = s;
1050   op = oparg;
1051 
1052   return yyparse ();
1053 }
1054 
1055 /* The error handler.  */
1056 
1057 static void
yyerror(s)1058 yyerror (s)
1059      const char *s;
1060 {
1061   op->error = s;
1062 }
1063