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
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"
32 #include "safe-ctype.h"
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. */
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
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
60 #define yy_yys m68k_yys
61 #define yystate m68k_state
62 #define yytmp m68k_tmp
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
83 /* Internal functions. */
85 static enum m68k_register m68k_reg_parse PARAMS
((char **));
86 static int yylex PARAMS
((void));
87 static void yyerror PARAMS
((const char *));
89 /* The parser sets fields pointed to by this global variable. */
90 static struct m68k_op
*op
;
96 struct m68k_indexreg indexreg
;
97 enum m68k_register reg
;
101 int trailing_ampersand
;
104 %token
<reg
> DR AR FPR FPCR LPC ZAR ZDR LZPC CREG
105 %token
<indexreg
> INDEXREG
108 %type
<indexreg
> zireg zdireg
109 %type
<reg
> zadr zdr apc zapc zpc optzapc optczapc
110 %type
<exp
> optcexpr optexprc
111 %type
<mask
> reglist ireglist reglistpair
112 %type
<onereg
> reglistreg
113 %type
<trailing_ampersand
> optional_ampersand
121 | motorola_operand optional_ampersand
123 op
->trailing_ampersand
= $2;
128 /* A trailing ampersand(for MAC/EMAC mask addressing). */
136 /* A generic operand. */
196 /* An operand in Motorola syntax. This includes MRI syntax as well,
197 which may or may not be different in that it permits commutativity
198 of index and base registers, and permits an offset expression to
199 appear inside or outside of the parentheses. */
217 |
'(' EXPR
',' zapc
')'
221 if
(($4 >= ZADDR0
&& $4 <= ZADDR7
)
227 |
'(' zapc
',' EXPR
')'
231 if
(($2 >= ZADDR0
&& $2 <= ZADDR7
)
241 if
(($3 >= ZADDR0
&& $3 <= ZADDR7
)
262 |
'(' EXPR
',' zapc
',' zireg
')'
269 |
'(' EXPR
',' zapc
',' zpc
')'
271 if
($4 == PC ||
$4 == ZPC
)
272 yyerror (_
("syntax error"));
277 op
->index.size
= SIZE_UNSPEC
;
280 |
'(' EXPR
',' zdireg optczapc
')'
287 |
'(' zdireg
',' EXPR
')'
293 | EXPR
'(' zapc
',' zireg
')'
300 |
'(' zapc
',' zireg
')'
306 | EXPR
'(' zapc
',' zpc
')'
308 if
($3 == PC ||
$3 == ZPC
)
309 yyerror (_
("syntax error"));
314 op
->index.size
= SIZE_UNSPEC
;
317 |
'(' zapc
',' zpc
')'
319 if
($2 == PC ||
$2 == ZPC
)
320 yyerror (_
("syntax error"));
324 op
->index.size
= SIZE_UNSPEC
;
327 | EXPR
'(' zdireg optczapc
')'
334 |
'(' zdireg optczapc
')'
340 |
'(' '[' EXPR optczapc
']' ',' zireg optcexpr
')'
348 |
'(' '[' EXPR optczapc
']' optcexpr
')'
355 |
'(' '[' zapc
']' ',' zireg optcexpr
')'
362 |
'(' '[' zapc
']' optcexpr
')'
368 |
'(' '[' EXPR
',' zapc
',' zireg
']' optcexpr
')'
376 |
'(' '[' zapc
',' zireg
']' optcexpr
')'
383 |
'(' '[' EXPR
',' zapc
',' zpc
']' optcexpr
')'
385 if
($5 == PC ||
$5 == ZPC
)
386 yyerror (_
("syntax error"));
391 op
->index.size
= SIZE_UNSPEC
;
395 |
'(' '[' zapc
',' zpc
']' optcexpr
')'
397 if
($3 == PC ||
$3 == ZPC
)
398 yyerror (_
("syntax error"));
402 op
->index.size
= SIZE_UNSPEC
;
406 |
'(' '[' optexprc zdireg optczapc
']' optcexpr
')'
416 /* An operand in MIT syntax. */
421 /* We use optzapc to avoid a shift/reduce conflict. */
422 if
($1 < ADDR0 ||
$1 > ADDR7
)
423 yyerror (_
("syntax error"));
429 /* We use optzapc to avoid a shift/reduce conflict. */
430 if
($1 < ADDR0 ||
$1 > ADDR7
)
431 yyerror (_
("syntax error"));
437 /* We use optzapc to avoid a shift/reduce conflict. */
438 if
($1 < ADDR0 ||
$1 > ADDR7
)
439 yyerror (_
("syntax error"));
443 | optzapc
'@' '(' EXPR
')'
447 if
(($1 >= ZADDR0
&& $1 <= ZADDR7
)
453 | optzapc
'@' '(' optexprc zireg
')'
460 | optzapc
'@' '(' EXPR
')' '@' '(' optexprc zireg
')'
468 | optzapc
'@' '(' EXPR
')' '@' '(' EXPR
')'
475 | optzapc
'@' '(' optexprc zireg
')' '@' '(' EXPR
')'
485 /* An index register, possibly suppressed, which need not have a size
493 $$.size
= SIZE_UNSPEC
;
498 /* A register which may be an index register, but which may not be an
499 address register. This nonterminal is used to avoid ambiguity when
500 trying to parse something like (0,d5,a6) as compared to (0,a6,d5). */
507 $$.size
= SIZE_UNSPEC
;
512 /* An address or data register, or a suppressed address or data
521 /* A data register which may be suppressed. */
528 /* Either an address register or the PC. */
535 /* Either an address register, or the PC, or a suppressed address
536 register, or a suppressed PC. */
544 /* An optional zapc. */
554 /* The PC, optionally suppressed. */
561 /* ',' zapc when it may be omitted. */
574 /* ',' EXPR when it may be omitted. */
579 $$.exp.X_op
= O_absent
;
580 $$.size
= SIZE_UNSPEC
;
588 /* EXPR ',' when it may be omitted. */
593 $$.exp.X_op
= O_absent
;
594 $$.size
= SIZE_UNSPEC
;
602 /* A register list for the movem instruction. */
606 | reglistpair
'/' ireglist
610 | reglistreg
'/' ireglist
616 /* We use ireglist when we know we are looking at a reglist, and we
617 can safely reduce a simple register to reglistreg. If we permitted
618 reglist to reduce to reglistreg, it would be ambiguous whether a
619 plain register were a DREG/AREG/FPREG or a REGLST. */
627 | reglistpair
'/' ireglist
631 | reglistreg
'/' ireglist
638 reglistreg
'-' reglistreg
641 $$
= (1 << ($3 + 1)) - 1 - ((1 << $1) - 1);
643 $$
= (1 << ($1 + 1)) - 1 - ((1 << $3) - 1);
673 /* The string to parse is stored here, and modified by yylex. */
677 /* The original string pointer. */
679 static char *strorig
;
681 /* If *CCP could be a register, return the register number and advance
682 *CCP. Otherwise don't change *CCP, and return 0. */
684 static enum m68k_register
693 if
(flag_reg_prefix_optional
)
695 if
(*start
== REGISTER_PREFIX
)
701 if
(*start
!= REGISTER_PREFIX
)
706 if
(! is_name_beginner
(*p
))
710 while
(is_part_of_name
(*p
) && *p
!= '.' && *p
!= ':' && *p
!= '*')
715 symbolp
= symbol_find
(start
);
718 if
(symbolp
!= NULL
&& S_GET_SEGMENT
(symbolp
) == reg_section
)
721 return S_GET_VALUE
(symbolp
);
724 /* In MRI mode, something like foo.bar can be equated to a register
726 while
(flag_mri
&& c
== '.')
729 while
(is_part_of_name
(*p
) && *p
!= '.' && *p
!= ':' && *p
!= '*')
733 symbolp
= symbol_find
(start
);
735 if
(symbolp
!= NULL
&& S_GET_SEGMENT
(symbolp
) == reg_section
)
738 return S_GET_VALUE
(symbolp
);
750 enum m68k_register reg
;
763 /* Various special characters are just returned directly. */
767 /* In MRI mode, this can be the start of an octal number. */
771 ||
((str
[1] == '+' || str
[1] == '-')
772 && ISDIGIT
(str
[2])))
787 /* It so happens that a '+' can only appear at the end of an
788 operand, or if it is trailed by an '&'(see mac load insn).
789 If it appears anywhere else, it must be a unary. */
790 if
(str
[1] == '\0' ||
(str
[1] == '&' && str
[2] == '\0'))
794 /* A '-' can only appear in -(ar), rn-rn, or ar@-. If it
795 appears anywhere else, it must be a unary minus on an
802 if
(m68k_reg_parse
(&s
) != 0)
806 /* A '(' can only appear in `(reg)', `(expr,...', `([', `@(', or
807 `)('. If it appears anywhere else, it must be starting an
815 if
(m68k_reg_parse
(&s
) != 0)
817 /* Check for the case of '(expr,...' by scanning ahead. If we
818 find a comma outside of balanced parentheses, we return '('.
819 If we find an unbalanced right parenthesis, then presumably
820 the '(' really starts an expression. */
822 for
(s
= str
+ 1; *s
!= '\0'; s
++)
832 else if
(*s
== ',' && parens
== 0)
834 /* A comma can not normally appear in an expression, so
835 this is a case of '(expr,...'. */
841 /* See if it's a register. */
843 reg
= m68k_reg_parse
(&str
);
850 if
(reg
>= DATA0
&& reg
<= DATA7
)
852 else if
(reg
>= ADDR0
&& reg
<= ADDR7
)
854 else if
(reg
>= FP0
&& reg
<= FP7
)
862 else if
(reg
>= ZDATA0
&& reg
<= ZDATA7
)
864 else if
(reg
>= ZADDR0
&& reg
<= ZADDR7
)
871 /* If we get here, we have a data or address register. We
872 must check for a size or scale; if we find one, we must
877 if
(*s
!= '.' && *s
!= ':' && *s
!= '*')
880 yylval.indexreg.reg
= reg
;
882 if
(*s
!= '.' && *s
!= ':')
883 yylval.indexreg.size
= SIZE_UNSPEC
;
891 yylval.indexreg.size
= SIZE_WORD
;
896 yylval.indexreg.size
= SIZE_LONG
;
900 yyerror (_
("illegal size specification"));
901 yylval.indexreg.size
= SIZE_UNSPEC
;
906 yylval.indexreg.scale
= 1;
908 if
(*s
== '*' ||
*s
== ':')
914 hold
= input_line_pointer
;
915 input_line_pointer
= s
;
917 s
= input_line_pointer
;
918 input_line_pointer
= hold
;
920 if
(scale.X_op
!= O_constant
)
921 yyerror (_
("scale specification must resolve to a number"));
924 switch
(scale.X_add_number
)
930 yylval.indexreg.scale
= scale.X_add_number
;
933 yyerror (_
("invalid scale value"));
944 /* It must be an expression. Before we call expression, we need to
945 look ahead to see if there is a size specification. We must do
946 that first, because otherwise foo.l will be treated as the symbol
947 foo.l, rather than as the symbol foo with a long size
948 specification. The grammar requires that all expressions end at
949 the end of the operand, or with ',', '(', ']', ')'. */
952 for
(s
= str
; *s
!= '\0'; s
++)
958 && (s
[-1] == ')' || ISALNUM
(s
[-1])))
969 && (*s
== ',' ||
*s
== ']'))
973 yylval.exp.size
= SIZE_UNSPEC
;
975 ||
(s
[-2] != '.' && s
[-2] != ':'))
985 yylval.exp.size
= SIZE_BYTE
;
989 yylval.exp.size
= SIZE_WORD
;
993 yylval.exp.size
= SIZE_LONG
;
998 if
(yylval.exp.size
!= SIZE_UNSPEC
)
1004 /* Look for @PLTPC, etc. */
1007 yylval.exp.pic_reloc
= pic_none
;
1009 if
(cp
- 6 > str
&& cp
[-6] == '@')
1011 if
(strncmp
(cp
- 6, "@PLTPC", 6) == 0)
1013 yylval.exp.pic_reloc
= pic_plt_pcrel
;
1016 else if
(strncmp
(cp
- 6, "@GOTPC", 6) == 0)
1018 yylval.exp.pic_reloc
= pic_got_pcrel
;
1022 else if
(cp
- 4 > str
&& cp
[-4] == '@')
1024 if
(strncmp
(cp
- 4, "@PLT", 4) == 0)
1026 yylval.exp.pic_reloc
= pic_plt_off
;
1029 else if
(strncmp
(cp
- 4, "@GOT", 4) == 0)
1031 yylval.exp.pic_reloc
= pic_got_off
;
1044 hold
= input_line_pointer
;
1045 input_line_pointer
= str
;
1046 expression
(&yylval.exp.exp
);
1047 str
= input_line_pointer
;
1048 input_line_pointer
= hold
;
1059 /* Parse an m68k operand. This is the only function which is called
1060 from outside this file. */
1063 m68k_ip_op
(s
, oparg
)
1065 struct m68k_op
*oparg
;
1067 memset
(oparg
, 0, sizeof
*oparg
);
1068 oparg
->error = NULL
;
1069 oparg
->index.reg
= ZDATA0
;
1070 oparg
->index.scale
= 1;
1071 oparg
->disp.exp.X_op
= O_absent
;
1072 oparg
->odisp.exp.X_op
= O_absent
;
1080 /* The error handler. */