1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
/* GCC-StarPU
Copyright (C) 2011, 2012 Institut National de Recherche en Informatique et Automatique
GCC-StarPU is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GCC-StarPU is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC-StarPU. If not, see <http://www.gnu.org/licenses/>. */
/* Parser for simple C expressions in pragmas. */
%define api.pure
%parse-param { location_t loc }
%parse-param { const char *pragma }
%parse-param { tree *seq }
%debug
%{
#include <starpu-gcc-config.h>
#include <gcc-plugin.h>
#include <plugin.h>
#include <tree.h>
#include <cpplib.h>
#ifdef HAVE_C_FAMILY_C_COMMON_H
# include <c-family/c-common.h>
#elif HAVE_C_COMMON_H
# include <c-common.h>
#endif
#ifdef HAVE_C_FAMILY_C_PRAGMA_H
# include <c-family/c-pragma.h>
#elif HAVE_C_PRAGMA_H
# include <c-pragma.h>
#endif
#if !HAVE_DECL_BUILD_ARRAY_REF
/* This declaration is missing in GCC 4.6.1. */
extern tree build_array_ref (location_t loc, tree array, tree index);
#endif
#define YYSTYPE tree
#define YYLTYPE location_t
static void
yyerror (location_t loc, const char *pragma, tree *seq,
char const *message)
{
error_at (loc, "parse error in pragma %qs: %s", pragma, message);
}
/* Return SOMETHING if it's a VAR_DECL, an identifier bound to a VAR_DECL,
or another object; raise an error otherwise. */
static tree
ensure_bound (location_t loc, tree something)
{
gcc_assert (something != NULL_TREE);
if (DECL_P (something))
return something;
else if (TREE_CODE (something) == IDENTIFIER_NODE)
{
tree var = lookup_name (something);
if (var == NULL_TREE)
{
error_at (loc, "unbound variable %qE", something);
return error_mark_node;
}
else
return var;
}
return something;
}
static tree
build_component_ref (location_t loc, tree what, tree field)
{
sorry ("struct field access not implemented yet"); /* XXX */
return error_mark_node;
}
%}
%code {
/* Mapping of libcpp token names to Bison-generated token names. This is
not ideal but Bison cannot be told to use the `enum cpp_ttype'
values. */
#define STARPU_CPP_TOKENS \
TK (CPP_NAME) \
TK (CPP_NUMBER) \
TK (CPP_AND) \
TK (CPP_OPEN_SQUARE) \
TK (CPP_CLOSE_SQUARE) \
TK (CPP_OPEN_PAREN) \
TK (CPP_CLOSE_PAREN) \
TK (CPP_PLUS) \
TK (CPP_MINUS) \
TK (CPP_MULT) \
TK (CPP_DIV) \
TK (CPP_DOT) \
TK (CPP_DEREF)
#ifndef __cplusplus
static const int cpplib_bison_token_map[] =
{
# define TK(x) [x] = Y ## x,
STARPU_CPP_TOKENS
# undef TK
};
#else /* __cplusplus */
/* No designated initializers in C++. */
static int cpplib_bison_token_map[CPP_PADDING];
#endif /* __cplusplus */
static int
yylex (YYSTYPE *lvalp)
{
int ret;
#ifdef __cplusplus
if (cpplib_bison_token_map[CPP_NAME] != YCPP_NAME)
{
/* Initialize the table. */
# define TK(x) cpplib_bison_token_map[x] = Y ## x;
STARPU_CPP_TOKENS
# undef TK
}
#endif
ret = pragma_lex (lvalp);
if (ret < sizeof cpplib_bison_token_map / sizeof cpplib_bison_token_map[0])
ret = cpplib_bison_token_map[ret];
else
ret = -1;
return ret;
}
}
%token YCPP_NAME "identifier"
%token YCPP_NUMBER "integer"
%token YCPP_AND "&"
%token YCPP_OPEN_SQUARE "["
%token YCPP_CLOSE_SQUARE "]"
%token YCPP_OPEN_PAREN "("
%token YCPP_CLOSE_PAREN ")"
%token YCPP_PLUS "+"
%token YCPP_MINUS "-"
%token YCPP_MULT "*"
%token YCPP_DIV "/"
%token YCPP_DOT "."
%token YCPP_DEREF "->"
%% /* Grammar rules. */
/* Always return a TREE_LIST rather than a raw chain, because the elements
of that list may be already chained for other purposes---e.g., PARM_DECLs
of a function are chained together. */
sequence: expression {
gcc_assert (*seq == NULL_TREE);
*seq = tree_cons (NULL_TREE, $1, NULL_TREE);
$$ = *seq;
}
| expression sequence {
gcc_assert ($2 == *seq);
*seq = tree_cons (NULL_TREE, $1, $2);
$$ = *seq;
}
;
expression: binary_expression
;
/* XXX: `ensure_bound' below leads to errors raised even for non-significant
arguments---e.g., junk after pragma. */
identifier: YCPP_NAME { $$ = ensure_bound (loc, $1); }
;
binary_expression: additive_expression
;
multiplicative_expression: multiplicative_expression YCPP_MULT cast_expression {
$$ = build_binary_op (UNKNOWN_LOCATION, MULT_EXPR, $1, $3, 0);
}
| multiplicative_expression YCPP_DIV cast_expression {
$$ = build_binary_op (UNKNOWN_LOCATION, TRUNC_DIV_EXPR, $1, $3, 0);
}
| cast_expression
;
additive_expression: multiplicative_expression
| additive_expression YCPP_PLUS multiplicative_expression {
$$ = build_binary_op (UNKNOWN_LOCATION, PLUS_EXPR, $1, $3, 0);
}
| additive_expression YCPP_MINUS multiplicative_expression {
$$ = build_binary_op (UNKNOWN_LOCATION, MINUS_EXPR, $1, $3, 0);
}
;
cast_expression: unary_expression
/* XXX: No support for '(' TYPE-NAME ')' UNARY-EXPRESSION. */
;
unary_expression: postfix_expression
| YCPP_AND cast_expression {
$$ = build_addr (ensure_bound (loc, $2), current_function_decl);
}
;
postfix_expression:
primary_expression
| postfix_expression YCPP_OPEN_SQUARE expression YCPP_CLOSE_SQUARE {
#if 1
/* Build the array ref with proper error checking. */
$$ = build_array_ref (loc, ensure_bound (loc, $1),
ensure_bound (loc, $3));
#else /* TIMTOWTDI */
$$ = build_indirect_ref (loc,
build_binary_op (loc, PLUS_EXPR, ensure_bound (loc, $1), ensure_bound (loc, $3), 0),
RO_ARRAY_INDEXING);
#endif
}
| postfix_expression YCPP_DOT identifier {
$$ = build_component_ref (loc, ensure_bound (loc, $1), $2);
}
| postfix_expression YCPP_DEREF identifier {
$$ = build_component_ref (loc,
build_indirect_ref (loc, ensure_bound (loc, $1), RO_ARRAY_INDEXING),
$2);
}
;
primary_expression: identifier
| constant
| YCPP_OPEN_PAREN expression YCPP_CLOSE_PAREN { $$ = $2; }
;
constant: YCPP_NUMBER { $$ = $1; }
;
%%
|