1 /* m68k.y -- bison grammar for m68k operand parsing
2 Copyright 1995, 1996, 1997, 1998, 2001, 2003, 2004
3 Free Software Foundation, Inc.
4 Written by Ken Raeburn and Ian Lance Taylor, Cygnus Support
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* This file holds a bison grammar to parse m68k operands. The m68k
24 has a complicated operand syntax, and gas supports two main
25 variations of it. Using a grammar is probably overkill, but at
26 least it makes clear exactly what we do support. */
32 #include "m68k-parse.h"
33 #include "safe-ctype.h"
35 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
36 etc), as well as gratuitously global symbol names If other parser
37 generators (bison, byacc, etc) produce additional global names that
38 conflict at link time, then those parser generators need to be
39 fixed instead of adding those names to this list. */
41 #define yymaxdepth m68k_maxdepth
42 #define yyparse m68k_parse
43 #define yylex m68k_lex
44 #define yyerror m68k_error
45 #define yylval m68k_lval
46 #define yychar m68k_char
47 #define yydebug m68k_debug
48 #define yypact m68k_pact
51 #define yydef m68k_def
52 #define yychk m68k_chk
53 #define yypgo m68k_pgo
54 #define yyact m68k_act
55 #define yyexca m68k_exca
56 #define yyerrflag m68k_errflag
57 #define yynerrs m68k_nerrs
61 #define yy_yys m68k_yys
62 #define yystate m68k_state
63 #define yytmp m68k_tmp
65 #define yy_yyv m68k_yyv
66 #define yyval m68k_val
67 #define yylloc m68k_lloc
68 #define yyreds m68k_reds /* With YYDEBUG defined */
69 #define yytoks m68k_toks /* With YYDEBUG defined */
70 #define yylhs m68k_yylhs
71 #define yylen m68k_yylen
72 #define yydefred m68k_yydefred
73 #define yydgoto m68k_yydgoto
74 #define yysindex m68k_yysindex
75 #define yyrindex m68k_yyrindex
76 #define yygindex m68k_yygindex
77 #define yytable m68k_yytable
78 #define yycheck m68k_yycheck
84 /* Internal functions. */
86 static enum m68k_register m68k_reg_parse PARAMS
((char **));
87 static int yylex PARAMS
((void));
88 static void yyerror PARAMS
((const char *));
90 /* The parser sets fields pointed to by this global variable. */
91 static struct m68k_op
*op
;
97 struct m68k_indexreg indexreg
;
98 enum m68k_register reg
;
102 int trailing_ampersand
;
105 %token
<reg
> DR AR FPR FPCR LPC ZAR ZDR LZPC CREG
106 %token
<indexreg
> INDEXREG
109 %type
<indexreg
> zireg zdireg
110 %type
<reg
> zadr zdr apc zapc zpc optzapc optczapc
111 %type
<exp
> optcexpr optexprc
112 %type
<mask
> reglist ireglist reglistpair
113 %type
<onereg
> reglistreg
114 %type
<trailing_ampersand
> optional_ampersand
122 | motorola_operand optional_ampersand
124 op
->trailing_ampersand
= $2;
126 | mit_operand optional_ampersand
128 op
->trailing_ampersand
= $2;
132 /* A trailing ampersand(for MAC/EMAC mask addressing). */
140 /* A generic operand. */
200 /* An operand in Motorola syntax. This includes MRI syntax as well,
201 which may or may not be different in that it permits commutativity
202 of index and base registers, and permits an offset expression to
203 appear inside or outside of the parentheses. */
221 |
'(' EXPR
',' zapc
')'
225 if
(($4 >= ZADDR0
&& $4 <= ZADDR7
)
231 |
'(' zapc
',' EXPR
')'
235 if
(($2 >= ZADDR0
&& $2 <= ZADDR7
)
245 if
(($3 >= ZADDR0
&& $3 <= ZADDR7
)
266 |
'(' EXPR
',' zapc
',' zireg
')'
273 |
'(' EXPR
',' zapc
',' zpc
')'
275 if
($4 == PC ||
$4 == ZPC
)
276 yyerror (_
("syntax error"));
281 op
->index.size
= SIZE_UNSPEC
;
284 |
'(' EXPR
',' zdireg optczapc
')'
291 |
'(' zdireg
',' EXPR
')'
297 | EXPR
'(' zapc
',' zireg
')'
304 |
'(' zapc
',' zireg
')'
310 | EXPR
'(' zapc
',' zpc
')'
312 if
($3 == PC ||
$3 == ZPC
)
313 yyerror (_
("syntax error"));
318 op
->index.size
= SIZE_UNSPEC
;
321 |
'(' zapc
',' zpc
')'
323 if
($2 == PC ||
$2 == ZPC
)
324 yyerror (_
("syntax error"));
328 op
->index.size
= SIZE_UNSPEC
;
331 | EXPR
'(' zdireg optczapc
')'
338 |
'(' zdireg optczapc
')'
344 |
'(' '[' EXPR optczapc
']' ',' zireg optcexpr
')'
352 |
'(' '[' EXPR optczapc
']' optcexpr
')'
359 |
'(' '[' zapc
']' ',' zireg optcexpr
')'
366 |
'(' '[' zapc
']' optcexpr
')'
372 |
'(' '[' EXPR
',' zapc
',' zireg
']' optcexpr
')'
380 |
'(' '[' zapc
',' zireg
']' optcexpr
')'
387 |
'(' '[' EXPR
',' zapc
',' zpc
']' optcexpr
')'
389 if
($5 == PC ||
$5 == ZPC
)
390 yyerror (_
("syntax error"));
395 op
->index.size
= SIZE_UNSPEC
;
399 |
'(' '[' zapc
',' zpc
']' optcexpr
')'
401 if
($3 == PC ||
$3 == ZPC
)
402 yyerror (_
("syntax error"));
406 op
->index.size
= SIZE_UNSPEC
;
410 |
'(' '[' optexprc zdireg optczapc
']' optcexpr
')'
420 /* An operand in MIT syntax. */
425 /* We use optzapc to avoid a shift/reduce conflict. */
426 if
($1 < ADDR0 ||
$1 > ADDR7
)
427 yyerror (_
("syntax error"));
433 /* We use optzapc to avoid a shift/reduce conflict. */
434 if
($1 < ADDR0 ||
$1 > ADDR7
)
435 yyerror (_
("syntax error"));
441 /* We use optzapc to avoid a shift/reduce conflict. */
442 if
($1 < ADDR0 ||
$1 > ADDR7
)
443 yyerror (_
("syntax error"));
447 | optzapc
'@' '(' EXPR
')'
451 if
(($1 >= ZADDR0
&& $1 <= ZADDR7
)
457 | optzapc
'@' '(' optexprc zireg
')'
464 | optzapc
'@' '(' EXPR
')' '@' '(' optexprc zireg
')'
472 | optzapc
'@' '(' EXPR
')' '@' '(' EXPR
')'
479 | optzapc
'@' '(' optexprc zireg
')' '@' '(' EXPR
')'
489 /* An index register, possibly suppressed, which need not have a size
497 $$.size
= SIZE_UNSPEC
;
502 /* A register which may be an index register, but which may not be an
503 address register. This nonterminal is used to avoid ambiguity when
504 trying to parse something like (0,d5,a6) as compared to (0,a6,d5). */
511 $$.size
= SIZE_UNSPEC
;
516 /* An address or data register, or a suppressed address or data
525 /* A data register which may be suppressed. */
532 /* Either an address register or the PC. */
539 /* Either an address register, or the PC, or a suppressed address
540 register, or a suppressed PC. */
548 /* An optional zapc. */
558 /* The PC, optionally suppressed. */
565 /* ',' zapc when it may be omitted. */
578 /* ',' EXPR when it may be omitted. */
583 $$.exp.X_op
= O_absent
;
584 $$.size
= SIZE_UNSPEC
;
592 /* EXPR ',' when it may be omitted. */
597 $$.exp.X_op
= O_absent
;
598 $$.size
= SIZE_UNSPEC
;
606 /* A register list for the movem instruction. */
610 | reglistpair
'/' ireglist
614 | reglistreg
'/' ireglist
620 /* We use ireglist when we know we are looking at a reglist, and we
621 can safely reduce a simple register to reglistreg. If we permitted
622 reglist to reduce to reglistreg, it would be ambiguous whether a
623 plain register were a DREG/AREG/FPREG or a REGLST. */
631 | reglistpair
'/' ireglist
635 | reglistreg
'/' ireglist
642 reglistreg
'-' reglistreg
645 $$
= (1 << ($3 + 1)) - 1 - ((1 << $1) - 1);
647 $$
= (1 << ($1 + 1)) - 1 - ((1 << $3) - 1);
677 /* The string to parse is stored here, and modified by yylex. */
681 /* The original string pointer. */
683 static char *strorig
;
685 /* If *CCP could be a register, return the register number and advance
686 *CCP. Otherwise don't change *CCP, and return 0. */
688 static enum m68k_register
697 if
(flag_reg_prefix_optional
)
699 if
(*start
== REGISTER_PREFIX
)
705 if
(*start
!= REGISTER_PREFIX
)
710 if
(! is_name_beginner
(*p
))
714 while
(is_part_of_name
(*p
) && *p
!= '.' && *p
!= ':' && *p
!= '*')
719 symbolp
= symbol_find
(start
);
722 if
(symbolp
!= NULL
&& S_GET_SEGMENT
(symbolp
) == reg_section
)
725 return S_GET_VALUE
(symbolp
);
728 /* In MRI mode, something like foo.bar can be equated to a register
730 while
(flag_mri
&& c
== '.')
733 while
(is_part_of_name
(*p
) && *p
!= '.' && *p
!= ':' && *p
!= '*')
737 symbolp
= symbol_find
(start
);
739 if
(symbolp
!= NULL
&& S_GET_SEGMENT
(symbolp
) == reg_section
)
742 return S_GET_VALUE
(symbolp
);
754 enum m68k_register reg
;
767 /* Various special characters are just returned directly. */
771 /* In MRI mode, this can be the start of an octal number. */
775 ||
((str
[1] == '+' || str
[1] == '-')
776 && ISDIGIT
(str
[2])))
791 /* It so happens that a '+' can only appear at the end of an
792 operand, or if it is trailed by an '&'(see mac load insn).
793 If it appears anywhere else, it must be a unary. */
794 if
(str
[1] == '\0' ||
(str
[1] == '&' && str
[2] == '\0'))
798 /* A '-' can only appear in -(ar), rn-rn, or ar@-. If it
799 appears anywhere else, it must be a unary minus on an
800 expression, unless it it trailed by a '&'(see mac load insn). */
801 if
(str
[1] == '\0' ||
(str
[1] == '&' && str
[2] == '\0'))
806 if
(m68k_reg_parse
(&s
) != 0)
810 /* A '(' can only appear in `(reg)', `(expr,...', `([', `@(', or
811 `)('. If it appears anywhere else, it must be starting an
819 if
(m68k_reg_parse
(&s
) != 0)
821 /* Check for the case of '(expr,...' by scanning ahead. If we
822 find a comma outside of balanced parentheses, we return '('.
823 If we find an unbalanced right parenthesis, then presumably
824 the '(' really starts an expression. */
826 for
(s
= str
+ 1; *s
!= '\0'; s
++)
836 else if
(*s
== ',' && parens
== 0)
838 /* A comma can not normally appear in an expression, so
839 this is a case of '(expr,...'. */
845 /* See if it's a register. */
847 reg
= m68k_reg_parse
(&str
);
854 if
(reg
>= DATA0
&& reg
<= DATA7
)
856 else if
(reg
>= ADDR0
&& reg
<= ADDR7
)
858 else if
(reg
>= FP0
&& reg
<= FP7
)
866 else if
(reg
>= ZDATA0
&& reg
<= ZDATA7
)
868 else if
(reg
>= ZADDR0
&& reg
<= ZADDR7
)
875 /* If we get here, we have a data or address register. We
876 must check for a size or scale; if we find one, we must
881 if
(*s
!= '.' && *s
!= ':' && *s
!= '*')
884 yylval.indexreg.reg
= reg
;
886 if
(*s
!= '.' && *s
!= ':')
887 yylval.indexreg.size
= SIZE_UNSPEC
;
895 yylval.indexreg.size
= SIZE_WORD
;
900 yylval.indexreg.size
= SIZE_LONG
;
904 yyerror (_
("illegal size specification"));
905 yylval.indexreg.size
= SIZE_UNSPEC
;
910 yylval.indexreg.scale
= 1;
912 if
(*s
== '*' ||
*s
== ':')
918 hold
= input_line_pointer
;
919 input_line_pointer
= s
;
921 s
= input_line_pointer
;
922 input_line_pointer
= hold
;
924 if
(scale.X_op
!= O_constant
)
925 yyerror (_
("scale specification must resolve to a number"));
928 switch
(scale.X_add_number
)
934 yylval.indexreg.scale
= scale.X_add_number
;
937 yyerror (_
("invalid scale value"));
948 /* It must be an expression. Before we call expression, we need to
949 look ahead to see if there is a size specification. We must do
950 that first, because otherwise foo.l will be treated as the symbol
951 foo.l, rather than as the symbol foo with a long size
952 specification. The grammar requires that all expressions end at
953 the end of the operand, or with ',', '(', ']', ')'. */
956 for
(s
= str
; *s
!= '\0'; s
++)
962 && (s
[-1] == ')' || ISALNUM
(s
[-1])))
973 && (*s
== ',' ||
*s
== ']'))
977 yylval.exp.size
= SIZE_UNSPEC
;
979 ||
(s
[-2] != '.' && s
[-2] != ':'))
989 yylval.exp.size
= SIZE_BYTE
;
993 yylval.exp.size
= SIZE_WORD
;
997 yylval.exp.size
= SIZE_LONG
;
1002 if
(yylval.exp.size
!= SIZE_UNSPEC
)
1008 /* Look for @PLTPC, etc. */
1011 yylval.exp.pic_reloc
= pic_none
;
1013 if
(cp
- 6 > str
&& cp
[-6] == '@')
1015 if
(strncmp
(cp
- 6, "@PLTPC", 6) == 0)
1017 yylval.exp.pic_reloc
= pic_plt_pcrel
;
1020 else if
(strncmp
(cp
- 6, "@GOTPC", 6) == 0)
1022 yylval.exp.pic_reloc
= pic_got_pcrel
;
1026 else if
(cp
- 4 > str
&& cp
[-4] == '@')
1028 if
(strncmp
(cp
- 4, "@PLT", 4) == 0)
1030 yylval.exp.pic_reloc
= pic_plt_off
;
1033 else if
(strncmp
(cp
- 4, "@GOT", 4) == 0)
1035 yylval.exp.pic_reloc
= pic_got_off
;
1048 hold
= input_line_pointer
;
1049 input_line_pointer
= str
;
1050 expression
(&yylval.exp.exp
);
1051 str
= input_line_pointer
;
1052 input_line_pointer
= hold
;
1063 /* Parse an m68k operand. This is the only function which is called
1064 from outside this file. */
1067 m68k_ip_op
(s
, oparg
)
1069 struct m68k_op
*oparg
;
1071 memset
(oparg
, 0, sizeof
*oparg
);
1072 oparg
->error = NULL
;
1073 oparg
->index.reg
= ZDATA0
;
1074 oparg
->index.scale
= 1;
1075 oparg
->disp.exp.X_op
= O_absent
;
1076 oparg
->odisp.exp.X_op
= O_absent
;
1084 /* The error handler. */