1 /* m68k.y -- bison grammar for m68k operand parsing
2 Copyright (C) 1995, 96, 1997, 1998 Free Software Foundation, Inc.
3 Written by Ken Raeburn and Ian Lance Taylor, Cygnus Support
5 This file is part of GAS, the GNU Assembler.
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)
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.
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
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. */
31 #include "m68k-parse.h"
33 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
34 etc), as well as gratuitiously global symbol names If other parser
35 generators (bison, byacc, etc) produce additional global names that
36 conflict at link time, then those parser generators need to be
37 fixed instead of adding those names to this list. */
39 #define yymaxdepth m68k_maxdepth
40 #define yyparse m68k_parse
41 #define yylex m68k_lex
42 #define yyerror m68k_error
43 #define yylval m68k_lval
44 #define yychar m68k_char
45 #define yydebug m68k_debug
46 #define yypact m68k_pact
49 #define yydef m68k_def
50 #define yychk m68k_chk
51 #define yypgo m68k_pgo
52 #define yyact m68k_act
53 #define yyexca m68k_exca
54 #define yyerrflag m68k_errflag
55 #define yynerrs m68k_nerrs
59 #define yy_yys m68k_yys
60 #define yystate m68k_state
61 #define yytmp m68k_tmp
63 #define yy_yyv m68k_yyv
64 #define yyval m68k_val
65 #define yylloc m68k_lloc
66 #define yyreds m68k_reds /* With YYDEBUG defined */
67 #define yytoks m68k_toks /* With YYDEBUG defined */
68 #define yylhs m68k_yylhs
69 #define yylen m68k_yylen
70 #define yydefred m68k_yydefred
71 #define yydgoto m68k_yydgoto
72 #define yysindex m68k_yysindex
73 #define yyrindex m68k_yyrindex
74 #define yygindex m68k_yygindex
75 #define yytable m68k_yytable
76 #define yycheck m68k_yycheck
82 /* Internal functions. */
84 static enum m68k_register m68k_reg_parse PARAMS
((char **));
85 static int yylex PARAMS
((void));
86 static void yyerror PARAMS
((const char *));
88 /* The parser sets fields pointed to by this global variable. */
89 static struct m68k_op
*op
;
95 struct m68k_indexreg indexreg
;
96 enum m68k_register reg
;
102 %token
<reg
> DR AR FPR FPCR LPC ZAR ZDR LZPC CREG
103 %token
<indexreg
> INDEXREG
106 %type
<indexreg
> zireg zdireg
107 %type
<reg
> zadr zdr apc zapc zpc optzapc optczapc
108 %type
<exp
> optcexpr optexprc
109 %type
<mask
> reglist ireglist reglistpair
110 %type
<onereg
> reglistreg
122 /* A generic operand. */
172 /* An operand in Motorola syntax. This includes MRI syntax as well,
173 which may or may not be different in that it permits commutativity
174 of index and base registers, and permits an offset expression to
175 appear inside or outside of the parentheses. */
193 |
'(' EXPR
',' zapc
')'
197 if
(($4 >= ZADDR0
&& $4 <= ZADDR7
)
203 |
'(' zapc
',' EXPR
')'
207 if
(($2 >= ZADDR0
&& $2 <= ZADDR7
)
217 if
(($3 >= ZADDR0
&& $3 <= ZADDR7
)
238 |
'(' EXPR
',' zapc
',' zireg
')'
245 |
'(' EXPR
',' zapc
',' zpc
')'
247 if
($4 == PC ||
$4 == ZPC
)
248 yyerror (_
("syntax error"));
253 op
->index.size
= SIZE_UNSPEC
;
256 |
'(' EXPR
',' zdireg optczapc
')'
263 |
'(' zdireg
',' EXPR
')'
269 | EXPR
'(' zapc
',' zireg
')'
276 |
'(' zapc
',' zireg
')'
282 | EXPR
'(' zapc
',' zpc
')'
284 if
($3 == PC ||
$3 == ZPC
)
285 yyerror (_
("syntax error"));
290 op
->index.size
= SIZE_UNSPEC
;
293 |
'(' zapc
',' zpc
')'
295 if
($2 == PC ||
$2 == ZPC
)
296 yyerror (_
("syntax error"));
300 op
->index.size
= SIZE_UNSPEC
;
303 | EXPR
'(' zdireg optczapc
')'
310 |
'(' zdireg optczapc
')'
316 |
'(' '[' EXPR optczapc
']' ',' zireg optcexpr
')'
324 |
'(' '[' EXPR optczapc
']' optcexpr
')'
331 |
'(' '[' zapc
']' ',' zireg optcexpr
')'
338 |
'(' '[' zapc
']' optcexpr
')'
344 |
'(' '[' EXPR
',' zapc
',' zireg
']' optcexpr
')'
352 |
'(' '[' zapc
',' zireg
']' optcexpr
')'
359 |
'(' '[' EXPR
',' zapc
',' zpc
']' optcexpr
')'
361 if
($5 == PC ||
$5 == ZPC
)
362 yyerror (_
("syntax error"));
367 op
->index.size
= SIZE_UNSPEC
;
371 |
'(' '[' zapc
',' zpc
']' optcexpr
')'
373 if
($3 == PC ||
$3 == ZPC
)
374 yyerror (_
("syntax error"));
378 op
->index.size
= SIZE_UNSPEC
;
382 |
'(' '[' optexprc zdireg optczapc
']' optcexpr
')'
392 /* An operand in MIT syntax. */
397 /* We use optzapc to avoid a shift/reduce conflict. */
398 if
($1 < ADDR0 ||
$1 > ADDR7
)
399 yyerror (_
("syntax error"));
405 /* We use optzapc to avoid a shift/reduce conflict. */
406 if
($1 < ADDR0 ||
$1 > ADDR7
)
407 yyerror (_
("syntax error"));
413 /* We use optzapc to avoid a shift/reduce conflict. */
414 if
($1 < ADDR0 ||
$1 > ADDR7
)
415 yyerror (_
("syntax error"));
419 | optzapc
'@' '(' EXPR
')'
423 if
(($1 >= ZADDR0
&& $1 <= ZADDR7
)
429 | optzapc
'@' '(' optexprc zireg
')'
436 | optzapc
'@' '(' EXPR
')' '@' '(' optexprc zireg
')'
444 | optzapc
'@' '(' EXPR
')' '@' '(' EXPR
')'
451 | optzapc
'@' '(' optexprc zireg
')' '@' '(' EXPR
')'
461 /* An index register, possibly suppressed, which need not have a size
469 $$.size
= SIZE_UNSPEC
;
474 /* A register which may be an index register, but which may not be an
475 address register. This nonterminal is used to avoid ambiguity when
476 trying to parse something like (0,d5,a6) as compared to (0,a6,d5). */
483 $$.size
= SIZE_UNSPEC
;
488 /* An address or data register, or a suppressed address or data
497 /* A data register which may be suppressed. */
504 /* Either an address register or the PC. */
511 /* Either an address register, or the PC, or a suppressed address
512 register, or a suppressed PC. */
520 /* An optional zapc. */
530 /* The PC, optionally suppressed. */
537 /* ',' zapc when it may be omitted. */
550 /* ',' EXPR when it may be omitted. */
555 $$.exp.X_op
= O_absent
;
556 $$.size
= SIZE_UNSPEC
;
564 /* EXPR ',' when it may be omitted. */
569 $$.exp.X_op
= O_absent
;
570 $$.size
= SIZE_UNSPEC
;
578 /* A register list for the movem instruction. */
582 | reglistpair
'/' ireglist
586 | reglistreg
'/' ireglist
592 /* We use ireglist when we know we are looking at a reglist, and we
593 can safely reduce a simple register to reglistreg. If we permitted
594 reglist to reduce to reglistreg, it would be ambiguous whether a
595 plain register were a DREG/AREG/FPREG or a REGLST. */
603 | reglistpair
'/' ireglist
607 | reglistreg
'/' ireglist
614 reglistreg
'-' reglistreg
617 $$
= (1 << ($3 + 1)) - 1 - ((1 << $1) - 1);
619 $$
= (1 << ($1 + 1)) - 1 - ((1 << $3) - 1);
649 /* The string to parse is stored here, and modified by yylex. */
653 /* The original string pointer. */
655 static char *strorig
;
657 /* If *CCP could be a register, return the register number and advance
658 *CCP. Otherwise don't change *CCP, and return 0. */
660 static enum m68k_register
669 if
(flag_reg_prefix_optional
)
671 if
(*start
== REGISTER_PREFIX
)
677 if
(*start
!= REGISTER_PREFIX
)
682 if
(! is_name_beginner
(*p
))
686 while
(is_part_of_name
(*p
) && *p
!= '.' && *p
!= ':' && *p
!= '*')
691 symbolp
= symbol_find
(start
);
694 if
(symbolp
!= NULL
&& S_GET_SEGMENT
(symbolp
) == reg_section
)
697 return S_GET_VALUE
(symbolp
);
700 /* In MRI mode, something like foo.bar can be equated to a register
702 while
(flag_mri
&& c
== '.')
705 while
(is_part_of_name
(*p
) && *p
!= '.' && *p
!= ':' && *p
!= '*')
709 symbolp
= symbol_find
(start
);
711 if
(symbolp
!= NULL
&& S_GET_SEGMENT
(symbolp
) == reg_section
)
714 return S_GET_VALUE
(symbolp
);
726 enum m68k_register reg
;
739 /* Various special characters are just returned directly. */
743 /* In MRI mode, this can be the start of an octal number. */
747 ||
((str
[1] == '+' || str
[1] == '-')
748 && isdigit
(str
[2])))
761 /* It so happens that a '+' can only appear at the end of an
762 operand. If it appears anywhere else, it must be a unary
763 plus on an expression. */
768 /* A '-' can only appear in -(ar), rn-rn, or ar@-. If it
769 appears anywhere else, it must be a unary minus on an
776 if
(m68k_reg_parse
(&s
) != 0)
780 /* A '(' can only appear in `(reg)', `(expr,...', `([', `@(', or
781 `)('. If it appears anywhere else, it must be starting an
789 if
(m68k_reg_parse
(&s
) != 0)
791 /* Check for the case of '(expr,...' by scanning ahead. If we
792 find a comma outside of balanced parentheses, we return '('.
793 If we find an unbalanced right parenthesis, then presumably
794 the '(' really starts an expression. */
796 for
(s
= str
+ 1; *s
!= '\0'; s
++)
806 else if
(*s
== ',' && parens
== 0)
808 /* A comma can not normally appear in an expression, so
809 this is a case of '(expr,...'. */
815 /* See if it's a register. */
817 reg
= m68k_reg_parse
(&str
);
824 if
(reg
>= DATA0
&& reg
<= DATA7
)
826 else if
(reg
>= ADDR0
&& reg
<= ADDR7
)
828 else if
(reg
>= FP0
&& reg
<= FP7
)
836 else if
(reg
>= ZDATA0
&& reg
<= ZDATA7
)
838 else if
(reg
>= ZADDR0
&& reg
<= ZADDR7
)
845 /* If we get here, we have a data or address register. We
846 must check for a size or scale; if we find one, we must
851 if
(*s
!= '.' && *s
!= ':' && *s
!= '*')
854 yylval.indexreg.reg
= reg
;
856 if
(*s
!= '.' && *s
!= ':')
857 yylval.indexreg.size
= SIZE_UNSPEC
;
865 yylval.indexreg.size
= SIZE_WORD
;
870 yylval.indexreg.size
= SIZE_LONG
;
874 yyerror (_
("illegal size specification"));
875 yylval.indexreg.size
= SIZE_UNSPEC
;
880 yylval.indexreg.scale
= 1;
882 if
(*s
== '*' ||
*s
== ':')
888 hold
= input_line_pointer
;
889 input_line_pointer
= s
;
891 s
= input_line_pointer
;
892 input_line_pointer
= hold
;
894 if
(scale.X_op
!= O_constant
)
895 yyerror (_
("scale specification must resolve to a number"));
898 switch
(scale.X_add_number
)
904 yylval.indexreg.scale
= scale.X_add_number
;
907 yyerror (_
("invalid scale value"));
918 /* It must be an expression. Before we call expression, we need to
919 look ahead to see if there is a size specification. We must do
920 that first, because otherwise foo.l will be treated as the symbol
921 foo.l, rather than as the symbol foo with a long size
922 specification. The grammar requires that all expressions end at
923 the end of the operand, or with ',', '(', ']', ')'. */
926 for
(s
= str
; *s
!= '\0'; s
++)
932 && (s
[-1] == ')' || isalnum
((unsigned char) s
[-1])))
943 && (*s
== ',' ||
*s
== ']'))
947 yylval.exp.size
= SIZE_UNSPEC
;
949 ||
(s
[-2] != '.' && s
[-2] != ':'))
959 yylval.exp.size
= SIZE_BYTE
;
963 yylval.exp.size
= SIZE_WORD
;
967 yylval.exp.size
= SIZE_LONG
;
972 if
(yylval.exp.size
!= SIZE_UNSPEC
)
978 /* Look for @PLTPC, etc. */
981 yylval.exp.pic_reloc
= pic_none
;
983 if
(cp
- 6 > str
&& cp
[-6] == '@')
985 if
(strncmp
(cp
- 6, "@PLTPC", 6) == 0)
987 yylval.exp.pic_reloc
= pic_plt_pcrel
;
990 else if
(strncmp
(cp
- 6, "@GOTPC", 6) == 0)
992 yylval.exp.pic_reloc
= pic_got_pcrel
;
996 else if
(cp
- 4 > str
&& cp
[-4] == '@')
998 if
(strncmp
(cp
- 4, "@PLT", 4) == 0)
1000 yylval.exp.pic_reloc
= pic_plt_off
;
1003 else if
(strncmp
(cp
- 4, "@GOT", 4) == 0)
1005 yylval.exp.pic_reloc
= pic_got_off
;
1018 hold
= input_line_pointer
;
1019 input_line_pointer
= str
;
1020 expression
(&yylval.exp.exp
);
1021 str
= input_line_pointer
;
1022 input_line_pointer
= hold
;
1033 /* Parse an m68k operand. This is the only function which is called
1034 from outside this file. */
1037 m68k_ip_op
(s
, oparg
)
1039 struct m68k_op
*oparg
;
1041 memset
(oparg
, 0, sizeof
*oparg
);
1042 oparg
->error = NULL
;
1043 oparg
->index.reg
= ZDATA0
;
1044 oparg
->index.scale
= 1;
1045 oparg
->disp.exp.X_op
= O_absent
;
1046 oparg
->odisp.exp.X_op
= O_absent
;
1054 /* The error handler. */