/*-
* Copyright (c) 2010 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by David A. Holland.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
fprintf(stderr, "tokens:");
num = tokenarray_num(&tokens);
for (i=0; i<num; i++) {
t = tokenarray_get(&tokens, i);
switch (t->tok) {
case T_EOF: fprintf(stderr, " <eof>"); break;
case T_VAL: fprintf(stderr, " %d", t->val); break;
case T_LPAREN: fprintf(stderr, " ("); break;
case T_RPAREN: fprintf(stderr, " )"); break;
case T_PIPEPIPE: fprintf(stderr, " ||"); break;
case T_AMPAMP: fprintf(stderr, " &&"); break;
case T_EQEQ: fprintf(stderr, " =="); break;
case T_BANGEQ: fprintf(stderr, " !="); break;
case T_LTEQ: fprintf(stderr, " <="); break;
case T_GTEQ: fprintf(stderr, " >="); break;
case T_LTLT: fprintf(stderr, " <<"); break;
case T_GTGT: fprintf(stderr, " >>"); break;
case T_QUES: fprintf(stderr, " ?"); break;
case T_COLON: fprintf(stderr, " :"); break;
case T_PIPE: fprintf(stderr, " |"); break;
case T_CARET: fprintf(stderr, " ^"); break;
case T_AMP: fprintf(stderr, " &"); break;
case T_LT: fprintf(stderr, " <"); break;
case T_GT: fprintf(stderr, " >"); break;
case T_PLUS: fprintf(stderr, " +"); break;
case T_MINUS: fprintf(stderr, " -"); break;
case T_STAR: fprintf(stderr, " *"); break;
case T_SLASH: fprintf(stderr, " /"); break;
case T_PCT: fprintf(stderr, " %%"); break;
case T_BANG: fprintf(stderr, " !"); break;
case T_TILDE: fprintf(stderr, " ~"); break;
}
}
fprintf(stderr, "\n");
}
#endif
static
bool
isuop(enum tokens tok)
{
switch (tok) {
case T_BANG:
case T_TILDE:
case T_MINUS:
case T_PLUS:
return true;
default:
break;
}
return false;
}
static
bool
isbop(enum tokens tok)
{
switch (tok) {
case T_EOF:
case T_VAL:
case T_LPAREN:
case T_RPAREN:
case T_COLON:
case T_QUES:
case T_BANG:
case T_TILDE:
return false;
default:
break;
}
return true;
}
static
bool
isop(enum tokens tok)
{
switch (tok) {
case T_EOF:
case T_VAL:
case T_LPAREN:
case T_RPAREN:
return false;
default:
break;
}
return true;
}
static
int
getprec(enum tokens tok)
{
switch (tok) {
case T_BANG: case T_TILDE: return -1;
case T_STAR: case T_SLASH: case T_PCT: return 0;
case T_PLUS: case T_MINUS: return 1;
case T_LTLT: case T_GTGT: return 2;
case T_LT: case T_LTEQ: case T_GT: case T_GTEQ: return 3;
case T_EQEQ: case T_BANGEQ: return 4;
case T_AMP: return 5;
case T_CARET: return 6;
case T_PIPE: return 7;
case T_AMPAMP: return 8;
case T_PIPEPIPE: return 9;
default: break;
}
return 10;
}
static
int
eval_uop(enum tokens op, int val)
{
switch (op) {
case T_BANG: val = !val; break;
case T_TILDE: val = (int)~(unsigned)val; break;
case T_MINUS: val = -val; break;
case T_PLUS: break;
default: assert(0); break;
}
return val;
}
static
int
eval_bop(struct place *p, int lv, enum tokens op, int rv)
{
unsigned mask;
switch (op) {
case T_PIPEPIPE: return lv || rv;
case T_AMPAMP: return lv && rv;
case T_PIPE: return (int)((unsigned)lv | (unsigned)rv);
case T_CARET: return (int)((unsigned)lv ^ (unsigned)rv);
case T_AMP: return (int)((unsigned)lv & (unsigned)rv);
case T_EQEQ: return lv == rv;
case T_BANGEQ: return lv != rv;
case T_LT: return lv < rv;
case T_GT: return lv > rv;
case T_LTEQ: return lv <= rv;
case T_GTEQ: return lv >= rv;
case T_LTLT:
case T_GTGT:
if (rv < 0) {
complain(p, "Negative bit-shift");
complain_fail();
rv = 0;
}
if ((unsigned)rv >= CHAR_BIT * sizeof(unsigned)) {
complain(p, "Bit-shift farther than type width");
complain_fail();
rv = 0;
}
if (op == T_LTLT) {
return (int)((unsigned)lv << (unsigned)rv);
}
mask = ((unsigned)-1) << (CHAR_BIT * sizeof(unsigned) - rv);
lv = (int)(((unsigned)lv >> (unsigned)rv) | mask);
return lv;
if (num == 2 &&
t2->tok == T_VAL &&
t1->tok == T_EOF) {
/* accepting state */
break;
}
if (num >= 1 &&
t1->tok == T_EOF) {
/* any other configuration at eof is an error */
complain(&t1->place, "Parse error");
complain_fail();
break;
}
/* otherwise, wait for more input */
break;
}
}
static
void
token(struct place *p, enum tokens tok, int val)
{
struct token *t;
t = token_create(p, tok, val);
tokenarray_add(&tokens, t, NULL);
tryreduce();
}
static
int
wordval(struct place *p, char *word)
{
unsigned long val;
char *t;
if (word[0] >= '0' && word[0] <= '9') {
errno = 0;
val = strtoul(word, &t, 0);
if (errno) {
complain(p, "Invalid integer constant");
complain_fail();
return 0;
}
while (*t == 'U' || *t == 'L') {
t++;
}
if (*t != '\0') {
complain(p, "Trailing garbage after integer constant");
complain_fail();
return 0;
}
if (val > INT_MAX) {
complain(p, "Integer constant too large");
complain_fail();
return INT_MAX;
}
return val;
}
/* if it's a symbol, warn and substitute 0. */
if (warns.undef) {
complain(p, "Warning: value of undefined symbol %s is 0",
word);
if (mode.werror) {
complain_fail();
}
}
debuglog(p, "Undefined symbol %s; substituting 0", word);
return 0;
}
static
bool
check_word(struct place *p, char *expr, size_t pos, size_t *len_ret)
{
size_t len;
int val;
char tmp;