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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
|
/* GNU m4 -- A simple macro processor
Copyright (C) 1989-1994, 2006-2007, 2009-2014, 2016 Free Software
Foundation, Inc.
This file is part of GNU M4.
GNU M4 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.
GNU M4 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* This file contains the functions to evaluate integer expressions for
the "eval" macro. It is a little, fairly self-contained module, with
its own scanner, and a recursive descent parser. The only entry point
is evaluate (). */
#include "m4.h"
/* Evaluates token types. */
typedef enum eval_token
{
ERROR, BADOP,
PLUS, MINUS,
EXPONENT,
TIMES, DIVIDE, MODULO,
ASSIGN, EQ, NOTEQ, GT, GTEQ, LS, LSEQ,
LSHIFT, RSHIFT,
LNOT, LAND, LOR,
NOT, AND, OR, XOR,
LEFTP, RIGHTP,
NUMBER, EOTEXT
}
eval_token;
/* Error types. */
typedef enum eval_error
{
NO_ERROR,
DIVIDE_ZERO,
MODULO_ZERO,
NEGATIVE_EXPONENT,
/* All errors prior to SYNTAX_ERROR can be ignored in a dead
branch of && and ||. All errors after are just more details
about a syntax error. */
SYNTAX_ERROR,
MISSING_RIGHT,
UNKNOWN_INPUT,
EXCESS_INPUT,
INVALID_OPERATOR
}
eval_error;
static eval_error logical_or_term (eval_token, int32_t *);
static eval_error logical_and_term (eval_token, int32_t *);
static eval_error or_term (eval_token, int32_t *);
static eval_error xor_term (eval_token, int32_t *);
static eval_error and_term (eval_token, int32_t *);
static eval_error equality_term (eval_token, int32_t *);
static eval_error cmp_term (eval_token, int32_t *);
static eval_error shift_term (eval_token, int32_t *);
static eval_error add_term (eval_token, int32_t *);
static eval_error mult_term (eval_token, int32_t *);
static eval_error exp_term (eval_token, int32_t *);
static eval_error unary_term (eval_token, int32_t *);
static eval_error simple_term (eval_token, int32_t *);
/*--------------------.
| Lexical functions. |
`--------------------*/
/* Pointer to next character of input text. */
static const char *eval_text;
/* Value of eval_text, from before last call of eval_lex (). This is so we
can back up, if we have read too much. */
static const char *last_text;
static void
eval_init_lex (const char *text)
{
eval_text = text;
last_text = NULL;
}
static void
eval_undo (void)
{
eval_text = last_text;
}
/* VAL is numerical value, if any. */
static eval_token
eval_lex (int32_t *val)
{
while (isspace (to_uchar (*eval_text)))
eval_text++;
last_text = eval_text;
if (*eval_text == '\0')
return EOTEXT;
if (isdigit (to_uchar (*eval_text)))
{
int base, digit;
if (*eval_text == '0')
{
eval_text++;
switch (*eval_text)
{
case 'x':
case 'X':
base = 16;
eval_text++;
break;
case 'b':
case 'B':
base = 2;
eval_text++;
break;
case 'r':
case 'R':
base = 0;
eval_text++;
while (isdigit (to_uchar (*eval_text)) && base <= 36)
base = 10 * base + *eval_text++ - '0';
if (base == 0 || base > 36 || *eval_text != ':')
return ERROR;
eval_text++;
break;
default:
base = 8;
}
}
else
base = 10;
/* FIXME - this calculation can overflow. Consider xstrtol. */
*val = 0;
for (; *eval_text; eval_text++)
{
if (isdigit (to_uchar (*eval_text)))
digit = *eval_text - '0';
else if (islower (to_uchar (*eval_text)))
digit = *eval_text - 'a' + 10;
else if (isupper (to_uchar (*eval_text)))
digit = *eval_text - 'A' + 10;
else
break;
if (base == 1)
{
if (digit == 1)
(*val)++;
else if (digit == 0 && !*val)
continue;
else
break;
}
else if (digit >= base)
break;
else
*val = *val * base + digit;
}
return NUMBER;
}
switch (*eval_text++)
{
case '+':
if (*eval_text == '+' || *eval_text == '=')
return BADOP;
return PLUS;
case '-':
if (*eval_text == '-' || *eval_text == '=')
return BADOP;
return MINUS;
case '*':
if (*eval_text == '*')
{
eval_text++;
return EXPONENT;
}
else if (*eval_text == '=')
return BADOP;
return TIMES;
case '/':
if (*eval_text == '=')
return BADOP;
return DIVIDE;
case '%':
if (*eval_text == '=')
return BADOP;
return MODULO;
case '=':
if (*eval_text == '=')
{
eval_text++;
return EQ;
}
return ASSIGN;
case '!':
if (*eval_text == '=')
{
eval_text++;
return NOTEQ;
}
return LNOT;
case '>':
if (*eval_text == '=')
{
eval_text++;
return GTEQ;
}
else if (*eval_text == '>')
{
if (*++eval_text == '=')
return BADOP;
return RSHIFT;
}
return GT;
case '<':
if (*eval_text == '=')
{
eval_text++;
return LSEQ;
}
else if (*eval_text == '<')
{
if (*++eval_text == '=')
return BADOP;
return LSHIFT;
}
return LS;
case '^':
if (*eval_text == '=')
return BADOP;
return XOR;
case '~':
return NOT;
case '&':
if (*eval_text == '&')
{
eval_text++;
return LAND;
}
else if (*eval_text == '=')
return BADOP;
return AND;
case '|':
if (*eval_text == '|')
{
eval_text++;
return LOR;
}
else if (*eval_text == '=')
return BADOP;
return OR;
case '(':
return LEFTP;
case ')':
return RIGHTP;
default:
return ERROR;
}
}
/*---------------------------------------.
| Main entry point, called from "eval". |
`---------------------------------------*/
bool
evaluate (const char *expr, int32_t *val)
{
eval_token et;
eval_error err;
eval_init_lex (expr);
et = eval_lex (val);
err = logical_or_term (et, val);
if (err == NO_ERROR && *eval_text != '\0')
{
if (eval_lex (val) == BADOP)
err = INVALID_OPERATOR;
else
err = EXCESS_INPUT;
}
switch (err)
{
case NO_ERROR:
break;
case MISSING_RIGHT:
M4ERROR ((warning_status, 0,
"bad expression in eval (missing right parenthesis): %s",
expr));
break;
case SYNTAX_ERROR:
M4ERROR ((warning_status, 0,
"bad expression in eval: %s", expr));
break;
case UNKNOWN_INPUT:
M4ERROR ((warning_status, 0,
"bad expression in eval (bad input): %s", expr));
break;
case EXCESS_INPUT:
M4ERROR ((warning_status, 0,
"bad expression in eval (excess input): %s", expr));
break;
case INVALID_OPERATOR:
M4ERROR ((warning_status, 0,
"invalid operator in eval: %s", expr));
retcode = EXIT_FAILURE;
break;
case DIVIDE_ZERO:
M4ERROR ((warning_status, 0,
"divide by zero in eval: %s", expr));
break;
case MODULO_ZERO:
M4ERROR ((warning_status, 0,
"modulo by zero in eval: %s", expr));
break;
case NEGATIVE_EXPONENT:
M4ERROR ((warning_status, 0,
"negative exponent in eval: %s", expr));
break;
default:
M4ERROR ((warning_status, 0,
"INTERNAL ERROR: bad error code in evaluate ()"));
abort ();
}
return err != NO_ERROR;
}
/*---------------------------.
| Recursive descent parser. |
`---------------------------*/
static eval_error
logical_or_term (eval_token et, int32_t *v1)
{
int32_t v2;
eval_error er;
if ((er = logical_and_term (et, v1)) != NO_ERROR)
return er;
while ((et = eval_lex (&v2)) == LOR)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
/* Implement short-circuiting of valid syntax. */
er = logical_and_term (et, &v2);
if (er == NO_ERROR)
*v1 = *v1 || v2;
else if (*v1 != 0 && er < SYNTAX_ERROR)
*v1 = 1;
else
return er;
}
if (et == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
logical_and_term (eval_token et, int32_t *v1)
{
int32_t v2;
eval_error er;
if ((er = or_term (et, v1)) != NO_ERROR)
return er;
while ((et = eval_lex (&v2)) == LAND)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
/* Implement short-circuiting of valid syntax. */
er = or_term (et, &v2);
if (er == NO_ERROR)
*v1 = *v1 && v2;
else if (*v1 == 0 && er < SYNTAX_ERROR)
; /* v1 is already 0 */
else
return er;
}
if (et == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
or_term (eval_token et, int32_t *v1)
{
int32_t v2;
eval_error er;
if ((er = xor_term (et, v1)) != NO_ERROR)
return er;
while ((et = eval_lex (&v2)) == OR)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = xor_term (et, &v2)) != NO_ERROR)
return er;
*v1 |= v2;
}
if (et == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
xor_term (eval_token et, int32_t *v1)
{
int32_t v2;
eval_error er;
if ((er = and_term (et, v1)) != NO_ERROR)
return er;
while ((et = eval_lex (&v2)) == XOR)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = and_term (et, &v2)) != NO_ERROR)
return er;
*v1 ^= v2;
}
if (et == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
and_term (eval_token et, int32_t *v1)
{
int32_t v2;
eval_error er;
if ((er = equality_term (et, v1)) != NO_ERROR)
return er;
while ((et = eval_lex (&v2)) == AND)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = equality_term (et, &v2)) != NO_ERROR)
return er;
*v1 &= v2;
}
if (et == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
equality_term (eval_token et, int32_t *v1)
{
eval_token op;
int32_t v2;
eval_error er;
if ((er = cmp_term (et, v1)) != NO_ERROR)
return er;
/* In the 1.4.x series, we maintain the traditional behavior that
'=' is a synonym for '=='; however, this is contrary to POSIX and
we hope to convert '=' to mean assignment in 2.0. */
while ((op = eval_lex (&v2)) == EQ || op == NOTEQ || op == ASSIGN)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = cmp_term (et, &v2)) != NO_ERROR)
return er;
if (op == ASSIGN)
{
M4ERROR ((warning_status, 0, "\
Warning: recommend ==, not =, for equality operator"));
op = EQ;
}
*v1 = (op == EQ) == (*v1 == v2);
}
if (op == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
cmp_term (eval_token et, int32_t *v1)
{
eval_token op;
int32_t v2;
eval_error er;
if ((er = shift_term (et, v1)) != NO_ERROR)
return er;
while ((op = eval_lex (&v2)) == GT || op == GTEQ
|| op == LS || op == LSEQ)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = shift_term (et, &v2)) != NO_ERROR)
return er;
switch (op)
{
case GT:
*v1 = *v1 > v2;
break;
case GTEQ:
*v1 = *v1 >= v2;
break;
case LS:
*v1 = *v1 < v2;
break;
case LSEQ:
*v1 = *v1 <= v2;
break;
default:
M4ERROR ((warning_status, 0,
"INTERNAL ERROR: bad comparison operator in cmp_term ()"));
abort ();
}
}
if (op == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
shift_term (eval_token et, int32_t *v1)
{
eval_token op;
int32_t v2;
uint32_t u1;
eval_error er;
if ((er = add_term (et, v1)) != NO_ERROR)
return er;
while ((op = eval_lex (&v2)) == LSHIFT || op == RSHIFT)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = add_term (et, &v2)) != NO_ERROR)
return er;
/* Minimize undefined C behavior (shifting by a negative number,
shifting by the width or greater, left shift overflow, or
right shift of a negative number). Implement Java 32-bit
wrap-around semantics. This code assumes that the
implementation-defined overflow when casting unsigned to
signed is a silent twos-complement wrap-around. */
switch (op)
{
case LSHIFT:
u1 = *v1;
u1 <<= (uint32_t) (v2 & 0x1f);
*v1 = u1;
break;
case RSHIFT:
u1 = *v1 < 0 ? ~*v1 : *v1;
u1 >>= (uint32_t) (v2 & 0x1f);
*v1 = *v1 < 0 ? ~u1 : u1;
break;
default:
M4ERROR ((warning_status, 0,
"INTERNAL ERROR: bad shift operator in shift_term ()"));
abort ();
}
}
if (op == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
add_term (eval_token et, int32_t *v1)
{
eval_token op;
int32_t v2;
eval_error er;
if ((er = mult_term (et, v1)) != NO_ERROR)
return er;
while ((op = eval_lex (&v2)) == PLUS || op == MINUS)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = mult_term (et, &v2)) != NO_ERROR)
return er;
/* Minimize undefined C behavior on overflow. This code assumes
that the implementation-defined overflow when casting
unsigned to signed is a silent twos-complement
wrap-around. */
if (op == PLUS)
*v1 = (int32_t) ((uint32_t) *v1 + (uint32_t) v2);
else
*v1 = (int32_t) ((uint32_t) *v1 - (uint32_t) v2);
}
if (op == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
mult_term (eval_token et, int32_t *v1)
{
eval_token op;
int32_t v2;
eval_error er;
if ((er = exp_term (et, v1)) != NO_ERROR)
return er;
while ((op = eval_lex (&v2)) == TIMES || op == DIVIDE || op == MODULO)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = exp_term (et, &v2)) != NO_ERROR)
return er;
/* Minimize undefined C behavior on overflow. This code assumes
that the implementation-defined overflow when casting
unsigned to signed is a silent twos-complement
wrap-around. */
switch (op)
{
case TIMES:
*v1 = (int32_t) ((uint32_t) *v1 * (uint32_t) v2);
break;
case DIVIDE:
if (v2 == 0)
return DIVIDE_ZERO;
else if (v2 == -1)
/* Avoid overflow, and the x86 SIGFPE on INT_MIN / -1. */
*v1 = (int32_t) -(uint32_t) *v1;
else
*v1 /= v2;
break;
case MODULO:
if (v2 == 0)
return MODULO_ZERO;
else if (v2 == -1)
/* Avoid the x86 SIGFPE on INT_MIN % -1. */
*v1 = 0;
else
*v1 %= v2;
break;
default:
M4ERROR ((warning_status, 0,
"INTERNAL ERROR: bad operator in mult_term ()"));
abort ();
}
}
if (op == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
exp_term (eval_token et, int32_t *v1)
{
uint32_t result;
int32_t v2;
eval_error er;
if ((er = unary_term (et, v1)) != NO_ERROR)
return er;
while ((et = eval_lex (&v2)) == EXPONENT)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = exp_term (et, &v2)) != NO_ERROR)
return er;
/* Minimize undefined C behavior on overflow. This code assumes
that the implementation-defined overflow when casting
unsigned to signed is a silent twos-complement
wrap-around. */
result = 1;
if (v2 < 0)
return NEGATIVE_EXPONENT;
if (*v1 == 0 && v2 == 0)
return DIVIDE_ZERO;
while (v2-- > 0)
result *= (uint32_t) *v1;
*v1 = result;
}
if (et == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
unary_term (eval_token et, int32_t *v1)
{
eval_error er;
if (et == PLUS || et == MINUS || et == NOT || et == LNOT)
{
eval_token et2 = eval_lex (v1);
if (et2 == ERROR)
return UNKNOWN_INPUT;
if ((er = unary_term (et2, v1)) != NO_ERROR)
return er;
/* Minimize undefined C behavior on overflow. This code assumes
that the implementation-defined overflow when casting
unsigned to signed is a silent twos-complement
wrap-around. */
if (et == MINUS)
*v1 = (int32_t) -(uint32_t) *v1;
else if (et == NOT)
*v1 = ~*v1;
else if (et == LNOT)
*v1 = *v1 == 0 ? 1 : 0;
}
else if ((er = simple_term (et, v1)) != NO_ERROR)
return er;
return NO_ERROR;
}
static eval_error
simple_term (eval_token et, int32_t *v1)
{
int32_t v2;
eval_error er;
switch (et)
{
case LEFTP:
et = eval_lex (v1);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = logical_or_term (et, v1)) != NO_ERROR)
return er;
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if (et != RIGHTP)
return MISSING_RIGHT;
break;
case NUMBER:
break;
case BADOP:
return INVALID_OPERATOR;
default:
return SYNTAX_ERROR;
}
return NO_ERROR;
}
|