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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
|
%{
/*******************************************************************************
*
* MODULE: pragma.y
*
********************************************************************************
*
* DESCRIPTION: Pragma parser
*
********************************************************************************
*
* $Project: /Convert-Binary-C $
* $Author: mhx $
* $Date: 2009/03/15 04:10:48 +0100 $
* $Revision: 20 $
* $Source: /ctlib/pragma.y $
*
********************************************************************************
*
* Copyright (c) 2002-2009 Marcus Holland-Moritz. All rights reserved.
* This program is free software; you can redistribute it and/or modify
* it under the same terms as Perl itself.
*
*******************************************************************************/
/*===== GLOBAL INCLUDES ======================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*===== LOCAL INCLUDES =======================================================*/
#include "ctdebug.h"
#include "cterror.h"
#include "pragma.h"
#include "util/ccattr.h"
#include "util/memalloc.h"
#include "util/list.h"
#include "ucpp/cpp.h"
/*===== DEFINES ==============================================================*/
/* ADDITIONAL BISON CONFIGURATION */
#define YYPARSE_PARAM pState
#define YYLEX_PARAM pState
#define YYERROR_VERBOSE
/*
* Bison version >= 1.31 is needed for YYFPRINTF
*/
#if YYDEBUG && defined CTLIB_DEBUGGING
#define YYFPRINTF BisonDebugFunc
#endif
#define pragma_error( msg ) \
CT_DEBUG( PRAGMA, ("pragma_error(): %s", msg) )
#define pragma_parse CTlib_pragma_parse
/* MACROS */
#define PSTATE ((PragmaState *) pState)
#define VALID_PACK( value ) \
( (value) == 0 \
|| (value) == 1 \
|| (value) == 2 \
|| (value) == 4 \
|| (value) == 8 \
)
/*===== TYPEDEFS =============================================================*/
struct _pragmaState {
CParseInfo *pCPI;
const char *file;
long int line;
const char *code;
struct {
LinkedList stack;
unsigned current;
} pack;
};
typedef struct {
unsigned size;
} PackElement;
/*===== EXTERNAL VARIABLES ===================================================*/
/*===== GLOBAL VARIABLES =====================================================*/
/*===== STATIC VARIABLES =====================================================*/
/* TOKEN MAPPING TABLE */
static const int tokentab[] = {
0, /* NONE, */ /* whitespace */
0, /* NEWLINE, */ /* newline */
0, /* COMMENT, */ /* comment */
0, /* NUMBER, */ /* number constant */
0, /* NAME, */ /* identifier */
0, /* BUNCH, */ /* non-C characters */
0, /* PRAGMA, */ /* a #pragma directive */
0, /* CONTEXT, */ /* new file or #line */
0, /* STRING, */ /* constant "xxx" */
0, /* CHAR, */ /* constant 'xxx' */
'/', /* SLASH, */ /* / */
0, /* ASSLASH, */ /* /= */
'-', /* MINUS, */ /* - */
0, /* MMINUS, */ /* -- */
0, /* ASMINUS, */ /* -= */
0, /* ARROW, */ /* -> */
'+', /* PLUS, */ /* + */
0, /* PPLUS, */ /* ++ */
0, /* ASPLUS, */ /* += */
'<', /* LT, */ /* < */
0, /* LEQ, */ /* <= */
0, /* LSH, */ /* << */
0, /* ASLSH, */ /* <<= */
'>', /* GT, */ /* > */
0, /* GEQ, */ /* >= */
0, /* RSH, */ /* >> */
0, /* ASRSH, */ /* >>= */
'=', /* ASGN, */ /* = */
0, /* SAME, */ /* == */
#ifdef CAST_OP
0, /* CAST, */ /* => */
#endif
'~', /* NOT, */ /* ~ */
0, /* NEQ, */ /* != */
'&', /* AND, */ /* & */
0, /* LAND, */ /* && */
0, /* ASAND, */ /* &= */
'|', /* OR, */ /* | */
0, /* LOR, */ /* || */
0, /* ASOR, */ /* |= */
'%', /* PCT, */ /* % */
0, /* ASPCT, */ /* %= */
'*', /* STAR, */ /* * */
0, /* ASSTAR, */ /* *= */
'^', /* CIRC, */ /* ^ */
0, /* ASCIRC, */ /* ^= */
'!', /* LNOT, */ /* ! */
'{', /* LBRA, */ /* { */
'}', /* RBRA, */ /* } */
'[', /* LBRK, */ /* [ */
']', /* RBRK, */ /* ] */
'(', /* LPAR, */ /* ( */
')', /* RPAR, */ /* ) */
',', /* COMMA, */ /* , */
'?', /* QUEST, */ /* ? */
';', /* SEMIC, */ /* ; */
':', /* COLON, */ /* : */
'.', /* DOT, */ /* . */
0, /* MDOTS, */ /* ... */
0, /* SHARP, */ /* # */
0, /* DSHARP, */ /* ## */
0, /* OPT_NONE, */ /* optional space to separate tokens in text output */
0, /* DIGRAPH_TOKENS, */ /* there begin digraph tokens */
/* for DIG_*, do not change order, unless checking undig() in cpp.c */
'[', /* DIG_LBRK, */ /* <: */
']', /* DIG_RBRK, */ /* :> */
'{', /* DIG_LBRA, */ /* <% */
'}', /* DIG_RBRA, */ /* %> */
0, /* DIG_SHARP, */ /* %: */
0, /* DIG_DSHARP, */ /* %:%: */
0, /* DIGRAPH_TOKENS_END, */ /* digraph tokens end here */
0, /* LAST_MEANINGFUL_TOKEN, */ /* reserved words will go there */
0, /* MACROARG, */ /* special token for representing macro arguments */
0, /* UPLUS = CPPERR, */ /* unary + */
0, /* UMINUS */ /* unary - */
};
%}
/*===== YACC PARSER DEFINITION ================================================*/
%union {
int ival;
}
%{
/*===== STATIC FUNCTION PROTOTYPES ===========================================*/
static int is_valid_pack_arg(PragmaState *pState, int arg);
static inline int pragma_lex(YYSTYPE *plval, PragmaState *pState);
static PackElement *packelem_new(unsigned size);
static void packelem_delete(PackElement *pPack);
%}
%token <ival> CONSTANT
%token PACK_TOK
%token PUSH_TOK POP_TOK
%pure_parser
%start pragma
%%
pragma
: pragma_pack
;
pragma_pack
: PACK_TOK
{ PSTATE->pack.current = 0; }
| PACK_TOK '(' ')'
{ PSTATE->pack.current = 0; }
| PACK_TOK '(' pragma_pack_args ')'
;
pragma_pack_args
: CONSTANT
{
if (is_valid_pack_arg(PSTATE, $1))
{
PSTATE->pack.current = $1;
}
}
| PUSH_TOK ',' CONSTANT
{
if (is_valid_pack_arg(PSTATE, $3))
{
LL_push(PSTATE->pack.stack, packelem_new(PSTATE->pack.current));
PSTATE->pack.current = $3;
}
}
| POP_TOK
{
PackElement *pPack = LL_pop(PSTATE->pack.stack);
if (pPack)
{
PSTATE->pack.current = pPack->size;
packelem_delete(pPack);
}
else
PSTATE->pack.current = 0;
}
;
%%
/*===== STATIC FUNCTIONS =====================================================*/
/*******************************************************************************
*
* ROUTINE: is_valid_pack_arg
*
* WRITTEN BY: Marcus Holland-Moritz ON: Jun 2007
* CHANGED BY: ON:
*
********************************************************************************
*
* DESCRIPTION: Pack element constructor.
*
* ARGUMENTS:
*
* RETURNS:
*
*******************************************************************************/
static int is_valid_pack_arg(PragmaState *pState, int arg)
{
if (VALID_PACK(arg))
return 1;
push_error(pState->pCPI, "%s, line %ld: invalid argument %d to #pragma pack",
pState->file, pState->line, arg);
return 0;
}
/*******************************************************************************
*
* ROUTINE: packelem_new
*
* WRITTEN BY: Marcus Holland-Moritz ON: Jan 2002
* CHANGED BY: ON:
*
********************************************************************************
*
* DESCRIPTION: Pack element constructor.
*
* ARGUMENTS:
*
* RETURNS:
*
*******************************************************************************/
static PackElement *packelem_new(unsigned size)
{
PackElement *pPack;
AllocF(PackElement *, pPack, sizeof(PackElement));
pPack->size = size;
return pPack;
}
/*******************************************************************************
*
* ROUTINE: packelem_delete
*
* WRITTEN BY: Marcus Holland-Moritz ON: Jan 2002
* CHANGED BY: ON:
*
********************************************************************************
*
* DESCRIPTION: Pack element destructor.
*
* ARGUMENTS:
*
* RETURNS:
*
*******************************************************************************/
static void packelem_delete(PackElement *pPack)
{
if (pPack)
Free(pPack);
}
/*******************************************************************************
*
* ROUTINE: pragma_lex
*
* WRITTEN BY: Marcus Holland-Moritz ON: Jan 2002
* CHANGED BY: ON:
*
********************************************************************************
*
* DESCRIPTION: Pragma lexer.
*
* ARGUMENTS:
*
* RETURNS:
*
*******************************************************************************/
static inline int pragma_lex(YYSTYPE *plval, PragmaState *pState)
{
int token, rval;
CT_DEBUG( PRAGMA, ("pragma_lex()"));
while ((token = (int) *pState->code++) != 0)
{
switch (token)
{
case NUMBER:
{
const char *num = pState->code;
pState->code = strchr(num, PRAGMA_TOKEN_END) + 1;
plval->ival = strtol(num, NULL, 0);
CT_DEBUG(PRAGMA, ("pragma - constant: %d", plval->ival));
return CONSTANT;
}
case NAME:
{
const char *tokstr = pState->code;
int toklen, tokval;
#include "token/t_pragma.c"
success:
pState->code += toklen + 1;
return tokval;
unknown:
break;
}
default:
if ((rval = tokentab[token]) != 0)
return rval;
break;
}
}
return 0;
}
/*===== FUNCTIONS ============================================================*/
/*******************************************************************************
*
* ROUTINE: pragma_parser_parse
*
* WRITTEN BY: Marcus Holland-Moritz ON: Jun 2007
* CHANGED BY: ON:
*
********************************************************************************
*
* DESCRIPTION:
*
* ARGUMENTS:
*
* RETURNS:
*
*******************************************************************************/
int pragma_parser_parse(PragmaState *pPragma)
{
return pragma_parse(pPragma);
}
/*******************************************************************************
*
* ROUTINE: pragma_parser_new
*
* WRITTEN BY: Marcus Holland-Moritz ON: Jan 2002
* CHANGED BY: ON:
*
********************************************************************************
*
* DESCRIPTION:
*
* ARGUMENTS:
*
* RETURNS:
*
*******************************************************************************/
PragmaState *pragma_parser_new(CParseInfo *pCPI)
{
PragmaState *pState;
CT_DEBUG(PRAGMA, ("pragma_parser_new"));
AllocF(PragmaState *, pState, sizeof(PragmaState));
pState->pCPI = pCPI;
pState->file = 0;
pState->line = 0;
pState->code = 0;
pState->pack.stack = LL_new();
pState->pack.current = 0;
return pState;
}
/*******************************************************************************
*
* ROUTINE: pragma_parser_delete
*
* WRITTEN BY: Marcus Holland-Moritz ON: Jan 2002
* CHANGED BY: ON:
*
********************************************************************************
*
* DESCRIPTION:
*
* ARGUMENTS:
*
* RETURNS:
*
*******************************************************************************/
void pragma_parser_delete(PragmaState *pPragma)
{
if (pPragma)
{
CT_DEBUG(PRAGMA, ("pragma_parser_delete"));
LL_destroy(pPragma->pack.stack, (LLDestroyFunc) packelem_delete);
Free(pPragma);
}
}
/*******************************************************************************
*
* ROUTINE: pragma_parser_set_context
*
* WRITTEN BY: Marcus Holland-Moritz ON: Jun 2007
* CHANGED BY: ON:
*
********************************************************************************
*
* DESCRIPTION:
*
* ARGUMENTS:
*
* RETURNS:
*
*******************************************************************************/
void pragma_parser_set_context(PragmaState *pPragma, const char *file, long int line, const char *code)
{
pPragma->file = file;
pPragma->line = line;
pPragma->code = code;
}
/*******************************************************************************
*
* ROUTINE: pragma_parser_get_pack
*
* WRITTEN BY: Marcus Holland-Moritz ON: Jun 2007
* CHANGED BY: ON:
*
********************************************************************************
*
* DESCRIPTION:
*
* ARGUMENTS:
*
* RETURNS:
*
*******************************************************************************/
unsigned pragma_parser_get_pack(PragmaState *pPragma)
{
return pPragma->pack.current;
}
|