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 gratuitiously 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
;
103 %token
<reg
> DR AR FPR FPCR LPC ZAR ZDR LZPC CREG
104 %token
<indexreg
> INDEXREG
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
123 /* A generic operand. */
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. */
194 |
'(' EXPR
',' zapc
')'
198 if
(($4 >= ZADDR0
&& $4 <= ZADDR7
)
204 |
'(' zapc
',' EXPR
')'
208 if
(($2 >= ZADDR0
&& $2 <= ZADDR7
)
218 if
(($3 >= ZADDR0
&& $3 <= ZADDR7
)
239 |
'(' EXPR
',' zapc
',' zireg
')'
246 |
'(' EXPR
',' zapc
',' zpc
')'
248 if
($4 == PC ||
$4 == ZPC
)
249 yyerror (_
("syntax error"));
254 op
->index.size
= SIZE_UNSPEC
;
257 |
'(' EXPR
',' zdireg optczapc
')'
264 |
'(' zdireg
',' EXPR
')'
270 | EXPR
'(' zapc
',' zireg
')'
277 |
'(' zapc
',' zireg
')'
283 | EXPR
'(' zapc
',' zpc
')'
285 if
($3 == PC ||
$3 == ZPC
)
286 yyerror (_
("syntax error"));
291 op
->index.size
= SIZE_UNSPEC
;
294 |
'(' zapc
',' zpc
')'
296 if
($2 == PC ||
$2 == ZPC
)
297 yyerror (_
("syntax error"));
301 op
->index.size
= SIZE_UNSPEC
;
304 | EXPR
'(' zdireg optczapc
')'
311 |
'(' zdireg optczapc
')'
317 |
'(' '[' EXPR optczapc
']' ',' zireg optcexpr
')'
325 |
'(' '[' EXPR optczapc
']' optcexpr
')'
332 |
'(' '[' zapc
']' ',' zireg optcexpr
')'
339 |
'(' '[' zapc
']' optcexpr
')'
345 |
'(' '[' EXPR
',' zapc
',' zireg
']' optcexpr
')'
353 |
'(' '[' zapc
',' zireg
']' optcexpr
')'
360 |
'(' '[' EXPR
',' zapc
',' zpc
']' optcexpr
')'
362 if
($5 == PC ||
$5 == ZPC
)
363 yyerror (_
("syntax error"));
368 op
->index.size
= SIZE_UNSPEC
;
372 |
'(' '[' zapc
',' zpc
']' optcexpr
')'
374 if
($3 == PC ||
$3 == ZPC
)
375 yyerror (_
("syntax error"));
379 op
->index.size
= SIZE_UNSPEC
;
383 |
'(' '[' optexprc zdireg optczapc
']' optcexpr
')'
393 /* An operand in MIT syntax. */
398 /* We use optzapc to avoid a shift/reduce conflict. */
399 if
($1 < ADDR0 ||
$1 > ADDR7
)
400 yyerror (_
("syntax error"));
406 /* We use optzapc to avoid a shift/reduce conflict. */
407 if
($1 < ADDR0 ||
$1 > ADDR7
)
408 yyerror (_
("syntax error"));
414 /* We use optzapc to avoid a shift/reduce conflict. */
415 if
($1 < ADDR0 ||
$1 > ADDR7
)
416 yyerror (_
("syntax error"));
420 | optzapc
'@' '(' EXPR
')'
424 if
(($1 >= ZADDR0
&& $1 <= ZADDR7
)
430 | optzapc
'@' '(' optexprc zireg
')'
437 | optzapc
'@' '(' EXPR
')' '@' '(' optexprc zireg
')'
445 | optzapc
'@' '(' EXPR
')' '@' '(' EXPR
')'
452 | optzapc
'@' '(' optexprc zireg
')' '@' '(' EXPR
')'
462 /* An index register, possibly suppressed, which need not have a size
470 $$.size
= SIZE_UNSPEC
;
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). */
484 $$.size
= SIZE_UNSPEC
;
489 /* An address or data register, or a suppressed address or data
498 /* A data register which may be suppressed. */
505 /* Either an address register or the PC. */
512 /* Either an address register, or the PC, or a suppressed address
513 register, or a suppressed PC. */
521 /* An optional zapc. */
531 /* The PC, optionally suppressed. */
538 /* ',' zapc when it may be omitted. */
551 /* ',' EXPR when it may be omitted. */
556 $$.exp.X_op
= O_absent
;
557 $$.size
= SIZE_UNSPEC
;
565 /* EXPR ',' when it may be omitted. */
570 $$.exp.X_op
= O_absent
;
571 $$.size
= SIZE_UNSPEC
;
579 /* A register list for the movem instruction. */
583 | reglistpair
'/' ireglist
587 | reglistreg
'/' ireglist
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. */
604 | reglistpair
'/' ireglist
608 | reglistreg
'/' ireglist
615 reglistreg
'-' reglistreg
618 $$
= (1 << ($3 + 1)) - 1 - ((1 << $1) - 1);
620 $$
= (1 << ($1 + 1)) - 1 - ((1 << $3) - 1);
650 /* The string to parse is stored here, and modified by yylex. */
654 /* The original string pointer. */
656 static char *strorig
;
658 /* If *CCP could be a register, return the register number and advance
659 *CCP. Otherwise don't change *CCP, and return 0. */
661 static enum m68k_register
670 if
(flag_reg_prefix_optional
)
672 if
(*start
== REGISTER_PREFIX
)
678 if
(*start
!= REGISTER_PREFIX
)
683 if
(! is_name_beginner
(*p
))
687 while
(is_part_of_name
(*p
) && *p
!= '.' && *p
!= ':' && *p
!= '*')
692 symbolp
= symbol_find
(start
);
695 if
(symbolp
!= NULL
&& S_GET_SEGMENT
(symbolp
) == reg_section
)
698 return S_GET_VALUE
(symbolp
);
701 /* In MRI mode, something like foo.bar can be equated to a register
703 while
(flag_mri
&& c
== '.')
706 while
(is_part_of_name
(*p
) && *p
!= '.' && *p
!= ':' && *p
!= '*')
710 symbolp
= symbol_find
(start
);
712 if
(symbolp
!= NULL
&& S_GET_SEGMENT
(symbolp
) == reg_section
)
715 return S_GET_VALUE
(symbolp
);
727 enum m68k_register reg
;
740 /* Various special characters are just returned directly. */
744 /* In MRI mode, this can be the start of an octal number. */
748 ||
((str
[1] == '+' || str
[1] == '-')
749 && ISDIGIT
(str
[2])))
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. */
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
777 if
(m68k_reg_parse
(&s
) != 0)
781 /* A '(' can only appear in `(reg)', `(expr,...', `([', `@(', or
782 `)('. If it appears anywhere else, it must be starting an
790 if
(m68k_reg_parse
(&s
) != 0)
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. */
797 for
(s
= str
+ 1; *s
!= '\0'; s
++)
807 else if
(*s
== ',' && parens
== 0)
809 /* A comma can not normally appear in an expression, so
810 this is a case of '(expr,...'. */
816 /* See if it's a register. */
818 reg
= m68k_reg_parse
(&str
);
825 if
(reg
>= DATA0
&& reg
<= DATA7
)
827 else if
(reg
>= ADDR0
&& reg
<= ADDR7
)
829 else if
(reg
>= FP0
&& reg
<= FP7
)
837 else if
(reg
>= ZDATA0
&& reg
<= ZDATA7
)
839 else if
(reg
>= ZADDR0
&& reg
<= ZADDR7
)
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
852 if
(*s
!= '.' && *s
!= ':' && *s
!= '*')
855 yylval.indexreg.reg
= reg
;
857 if
(*s
!= '.' && *s
!= ':')
858 yylval.indexreg.size
= SIZE_UNSPEC
;
866 yylval.indexreg.size
= SIZE_WORD
;
871 yylval.indexreg.size
= SIZE_LONG
;
875 yyerror (_
("illegal size specification"));
876 yylval.indexreg.size
= SIZE_UNSPEC
;
881 yylval.indexreg.scale
= 1;
883 if
(*s
== '*' ||
*s
== ':')
889 hold
= input_line_pointer
;
890 input_line_pointer
= s
;
892 s
= input_line_pointer
;
893 input_line_pointer
= hold
;
895 if
(scale.X_op
!= O_constant
)
896 yyerror (_
("scale specification must resolve to a number"));
899 switch
(scale.X_add_number
)
905 yylval.indexreg.scale
= scale.X_add_number
;
908 yyerror (_
("invalid scale value"));
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 ',', '(', ']', ')'. */
927 for
(s
= str
; *s
!= '\0'; s
++)
933 && (s
[-1] == ')' || ISALNUM
(s
[-1])))
944 && (*s
== ',' ||
*s
== ']'))
948 yylval.exp.size
= SIZE_UNSPEC
;
950 ||
(s
[-2] != '.' && s
[-2] != ':'))
960 yylval.exp.size
= SIZE_BYTE
;
964 yylval.exp.size
= SIZE_WORD
;
968 yylval.exp.size
= SIZE_LONG
;
973 if
(yylval.exp.size
!= SIZE_UNSPEC
)
979 /* Look for @PLTPC, etc. */
982 yylval.exp.pic_reloc
= pic_none
;
984 if
(cp
- 6 > str
&& cp
[-6] == '@')
986 if
(strncmp
(cp
- 6, "@PLTPC", 6) == 0)
988 yylval.exp.pic_reloc
= pic_plt_pcrel
;
991 else if
(strncmp
(cp
- 6, "@GOTPC", 6) == 0)
993 yylval.exp.pic_reloc
= pic_got_pcrel
;
997 else if
(cp
- 4 > str
&& cp
[-4] == '@')
999 if
(strncmp
(cp
- 4, "@PLT", 4) == 0)
1001 yylval.exp.pic_reloc
= pic_plt_off
;
1004 else if
(strncmp
(cp
- 4, "@GOT", 4) == 0)
1006 yylval.exp.pic_reloc
= pic_got_off
;
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
;
1034 /* Parse an m68k operand. This is the only function which is called
1035 from outside this file. */
1038 m68k_ip_op
(s
, oparg
)
1040 struct m68k_op
*oparg
;
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
;
1055 /* The error handler. */