forked from arjunjm/SimpleScalar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.c
1243 lines (1123 loc) · 31.4 KB
/
eval.c
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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* expr.c - expression evaluator routines */
/* SimpleScalar(TM) Tool Suite
* Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.
* All Rights Reserved.
*
* THIS IS A LEGAL DOCUMENT, BY USING SIMPLESCALAR,
* YOU ARE AGREEING TO THESE TERMS AND CONDITIONS.
*
* No portion of this work may be used by any commercial entity, or for any
* commercial purpose, without the prior, written permission of SimpleScalar,
* LLC (info@simplescalar.com). Nonprofit and noncommercial use is permitted
* as described below.
*
* 1. SimpleScalar is provided AS IS, with no warranty of any kind, express
* or implied. The user of the program accepts full responsibility for the
* application of the program and the use of any results.
*
* 2. Nonprofit and noncommercial use is encouraged. SimpleScalar may be
* downloaded, compiled, executed, copied, and modified solely for nonprofit,
* educational, noncommercial research, and noncommercial scholarship
* purposes provided that this notice in its entirety accompanies all copies.
* Copies of the modified software can be delivered to persons who use it
* solely for nonprofit, educational, noncommercial research, and
* noncommercial scholarship purposes provided that this notice in its
* entirety accompanies all copies.
*
* 3. ALL COMMERCIAL USE, AND ALL USE BY FOR PROFIT ENTITIES, IS EXPRESSLY
* PROHIBITED WITHOUT A LICENSE FROM SIMPLESCALAR, LLC (info@simplescalar.com).
*
* 4. No nonprofit user may place any restrictions on the use of this software,
* including as modified by the user, by any other authorized user.
*
* 5. Noncommercial and nonprofit users may distribute copies of SimpleScalar
* in compiled or executable form as set forth in Section 2, provided that
* either: (A) it is accompanied by the corresponding machine-readable source
* code, or (B) it is accompanied by a written offer, with no time limit, to
* give anyone a machine-readable copy of the corresponding source code in
* return for reimbursement of the cost of distribution. This written offer
* must permit verbatim duplication by anyone, or (C) it is distributed by
* someone who received only the executable form, and is accompanied by a
* copy of the written offer of source code.
*
* 6. SimpleScalar was developed by Todd M. Austin, Ph.D. The tool suite is
* currently maintained by SimpleScalar LLC (info@simplescalar.com). US Mail:
* 2395 Timbercrest Court, Ann Arbor, MI 48105.
*
* Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include "host.h"
#include "misc.h"
#include "eval.h"
#if defined(sparc) && !defined(__svr4__)
#define strtoul strtol
#endif /* sparc */
/* expression evaluation error, this must be a global */
enum eval_err_t eval_error = ERR_NOERR;
/* enum eval_err_t -> error description string map */
char *eval_err_str[ERR_NUM] = {
/* ERR_NOERR */ "!! no error!!",
/* ERR_UPAREN */ "unmatched parenthesis",
/* ERR_NOTERM */ "expression term is missing",
/* ERR_DIV0 */ "divide by zero",
/* ERR_BADCONST */ "badly formed constant",
/* ERR_BADEXPR */ "badly formed expression",
/* ERR_UNDEFVAR */ "variable is undefined",
/* ERR_EXTRA */ "extra characters at end of expression"
};
/* *first* token character -> enum eval_token_t map */
static enum eval_token_t tok_map[256];
static int tok_map_initialized = FALSE;
/* builds the first token map */
static void
init_tok_map(void)
{
int i;
for (i=0; i<256; i++)
tok_map[i] = tok_invalid;
/* identifier characters */
for (i='a'; i<='z'; i++)
tok_map[i] = tok_ident;
for (i='A'; i<='Z'; i++)
tok_map[i] = tok_ident;
tok_map[(int)'_'] = tok_ident;
tok_map[(int)'$'] = tok_ident;
/* numeric characters */
for (i='0'; i<='9'; i++)
tok_map[i] = tok_const;
tok_map[(int)'.'] = tok_const;
/* operator characters */
tok_map[(int)'+'] = tok_plus;
tok_map[(int)'-'] = tok_minus;
tok_map[(int)'*'] = tok_mult;
tok_map[(int)'/'] = tok_div;
tok_map[(int)'('] = tok_oparen;
tok_map[(int)')'] = tok_cparen;
/* whitespace characers */
tok_map[(int)' '] = tok_whitespace;
tok_map[(int)'\t'] = tok_whitespace;
}
/* get next token from the expression string */
static enum eval_token_t /* token parsed */
get_next_token(struct eval_state_t *es) /* expression evaluator */
{
int allow_hex;
enum eval_token_t tok;
char *ptok_buf, last_char;
/* initialize the token map, if needed */
if (!tok_map_initialized)
{
init_tok_map();
tok_map_initialized = TRUE;
}
/* use the peek'ed token, if available, tok_buf should still be valid */
if (es->peek_tok != tok_invalid)
{
tok = es->peek_tok;
es->peek_tok = tok_invalid;
return tok;
}
/* set up the token string space */
ptok_buf = es->tok_buf;
*ptok_buf = '\0';
/* skip whitespace */
while (*es->p && tok_map[(int)*es->p] == tok_whitespace)
es->p++;
/* end of token stream? */
if (*es->p == '\0')
return tok_eof;
*ptok_buf++ = *es->p;
tok = tok_map[(int)*es->p++];
switch (tok)
{
case tok_ident:
/* parse off next identifier */
while (*es->p
&& (tok_map[(int)*es->p] == tok_ident
|| tok_map[(int)*es->p] == tok_const))
{
*ptok_buf++ = *es->p++;
}
break;
case tok_const:
/* parse off next numeric literal */
last_char = '\0';
allow_hex = FALSE;
while (*es->p &&
(tok_map[(int)*es->p] == tok_const
|| (*es->p == '-' && last_char == 'e')
|| (*es->p == '+' && last_char == 'e')
|| tolower(*es->p) == 'e'
|| tolower(*es->p) == 'x'
|| (tolower(*es->p) == 'a' && allow_hex)
|| (tolower(*es->p) == 'b' && allow_hex)
|| (tolower(*es->p) == 'c' && allow_hex)
|| (tolower(*es->p) == 'd' && allow_hex)
|| (tolower(*es->p) == 'e' && allow_hex)
|| (tolower(*es->p) == 'f' && allow_hex)))
{
last_char = tolower(*es->p);
if (*es->p == 'x' || *es->p == 'X')
allow_hex = TRUE;
*ptok_buf++ = *es->p++;
}
break;
case tok_plus:
case tok_minus:
case tok_mult:
case tok_div:
case tok_oparen:
case tok_cparen:
/* just pass on the token */
break;
default:
tok = tok_invalid;
break;
}
/* terminate the token string buffer */
*ptok_buf = '\0';
return tok;
}
/* peek ahead at the next token from the expression stream, currently
only the next token can be peek'ed at */
static enum eval_token_t /* next token in expression */
peek_next_token(struct eval_state_t *es) /* expression evalutor */
{
/* if there is no peek ahead token, get one */
if (es->peek_tok == tok_invalid)
{
es->lastp = es->p;
es->peek_tok = get_next_token(es);
}
/* return peek ahead token */
return es->peek_tok;
}
/* forward declaration */
static struct eval_value_t expr(struct eval_state_t *es);
/* default expression error value, eval_err is also set */
static struct eval_value_t err_value = { et_int, { 0 } };
/* expression type strings */
char *eval_type_str[et_NUM] = {
/* et_int */ "int",
/* et_uint */ "unsigned int",
/* et_addr */ "md_addr_t",
#ifdef HOST_HAS_QWORD
/* et_qword */ "qword_t",
/* et_sqword */ "sqword_t",
#endif /* HOST_HAS_QWORD */
/* et_float */ "float",
/* et_double */ "double",
/* et_symbol */ "symbol"
};
/* determine necessary arithmetic conversion on T1 <op> T2 */
static enum eval_type_t /* type of expression result */
result_type(enum eval_type_t t1, /* left operand type */
enum eval_type_t t2) /* right operand type */
{
/* sanity check, symbols should not show up in arithmetic exprs */
if (t1 == et_symbol || t2 == et_symbol)
panic("symbol used in expression");
/* using C rules, i.e., A6.5 */
if (t1 == et_double || t2 == et_double)
return et_double;
else if (t1 == et_float || t2 == et_float)
return et_float;
#ifdef HOST_HAS_QWORD
else if (t1 == et_qword || t2 == et_qword)
return et_qword;
else if (t1 == et_sqword || t2 == et_sqword)
return et_sqword;
#endif /* HOST_HAS_QWORD */
else if (t1 == et_addr || t2 == et_addr)
return et_addr;
else if (t1 == et_uint || t2 == et_uint)
return et_uint;
else
return et_int;
}
/*
* expression value arithmetic conversions
*/
/* eval_value_t (any numeric type) -> double */
double
eval_as_double(struct eval_value_t val)
{
switch (val.type)
{
case et_double:
return val.value.as_double;
case et_float:
return (double)val.value.as_float;
#ifdef HOST_HAS_QWORD
case et_qword:
#ifdef _MSC_VER /* FIXME: MSC does not implement qword_t to dbl conversion */
return (double)(sqword_t)val.value.as_qword;
#else /* !_MSC_VER */
return (double)val.value.as_qword;
#endif /* _MSC_VER */
case et_sqword:
return (double)val.value.as_sqword;
#endif /* HOST_HAS_QWORD */
case et_addr:
#if defined(_MSC_VER) && defined(TARGET_ALPHA)
/* FIXME: MSC does not implement qword_t to double conversion */
return (double)(sqword_t)val.value.as_addr;
#else
return (double)val.value.as_addr;
#endif
case et_uint:
return (double)val.value.as_uint;
case et_int:
return (double)val.value.as_int;
case et_symbol:
panic("symbol used in expression");
default:
panic("illegal arithmetic expression conversion");
}
}
/* eval_value_t (any numeric type) -> float */
float
eval_as_float(struct eval_value_t val)
{
switch (val.type)
{
case et_double:
return (float)val.value.as_double;
case et_float:
return val.value.as_float;
#ifdef HOST_HAS_QWORD
case et_qword:
#ifdef _MSC_VER /* FIXME: MSC does not implement qword_t to dbl conversion */
return (float)(sqword_t)val.value.as_qword;
#else /* !_MSC_VER */
return (float)val.value.as_qword;
#endif /* _MSC_VER */
case et_sqword:
return (float)val.value.as_sqword;
#endif /* HOST_HAS_QWORD */
case et_addr:
#if defined(_MSC_VER) && defined(TARGET_ALPHA)
/* FIXME: MSC does not implement qword_t to double conversion */
return (float)(sqword_t)val.value.as_addr;
#else
return (float)val.value.as_addr;
#endif
case et_uint:
return (float)val.value.as_uint;
case et_int:
return (float)val.value.as_int;
case et_symbol:
panic("symbol used in expression");
default:
panic("illegal arithmetic expression conversion");
}
}
#ifdef HOST_HAS_QWORD
/* eval_value_t (any numeric type) -> qword_t */
qword_t
eval_as_qword(struct eval_value_t val)
{
switch (val.type)
{
case et_double:
return (qword_t)val.value.as_double;
case et_float:
return (qword_t)val.value.as_float;
case et_qword:
return val.value.as_qword;
case et_sqword:
return (qword_t)val.value.as_sqword;
case et_addr:
return (qword_t)val.value.as_addr;
case et_uint:
return (qword_t)val.value.as_uint;
case et_int:
return (qword_t)val.value.as_int;
case et_symbol:
panic("symbol used in expression");
default:
panic("illegal arithmetic expression conversion");
}
}
/* eval_value_t (any numeric type) -> sqword_t */
sqword_t
eval_as_sqword(struct eval_value_t val)
{
switch (val.type)
{
case et_double:
return (sqword_t)val.value.as_double;
case et_float:
return (sqword_t)val.value.as_float;
case et_qword:
return (sqword_t)val.value.as_qword;
case et_sqword:
return val.value.as_sqword;
case et_addr:
return (sqword_t)val.value.as_addr;
case et_uint:
return (sqword_t)val.value.as_uint;
case et_int:
return (sqword_t)val.value.as_int;
case et_symbol:
panic("symbol used in expression");
default:
panic("illegal arithmetic expression conversion");
}
}
#endif /* HOST_HAS_QWORD */
/* eval_value_t (any numeric type) -> unsigned int */
md_addr_t
eval_as_addr(struct eval_value_t val)
{
switch (val.type)
{
case et_double:
return (md_addr_t)val.value.as_double;
case et_float:
return (md_addr_t)val.value.as_float;
#ifdef HOST_HAS_QWORD
case et_qword:
return (md_addr_t)val.value.as_qword;
case et_sqword:
return (md_addr_t)val.value.as_sqword;
#endif /* HOST_HAS_QWORD */
case et_addr:
return val.value.as_addr;
case et_uint:
return (md_addr_t)val.value.as_uint;
case et_int:
return (md_addr_t)val.value.as_int;
case et_symbol:
panic("symbol used in expression");
default:
panic("illegal arithmetic expression conversion");
}
}
/* eval_value_t (any numeric type) -> unsigned int */
unsigned int
eval_as_uint(struct eval_value_t val)
{
switch (val.type)
{
case et_double:
return (unsigned int)val.value.as_double;
case et_float:
return (unsigned int)val.value.as_float;
#ifdef HOST_HAS_QWORD
case et_qword:
return (unsigned int)val.value.as_qword;
case et_sqword:
return (unsigned int)val.value.as_sqword;
#endif /* HOST_HAS_QWORD */
case et_addr:
return (unsigned int)val.value.as_addr;
case et_uint:
return val.value.as_uint;
case et_int:
return (unsigned int)val.value.as_int;
case et_symbol:
panic("symbol used in expression");
default:
panic("illegal arithmetic expression conversion");
}
}
/* eval_value_t (any numeric type) -> int */
int
eval_as_int(struct eval_value_t val)
{
switch (val.type)
{
case et_double:
return (int)val.value.as_double;
case et_float:
return (int)val.value.as_float;
#ifdef HOST_HAS_QWORD
case et_qword:
return (int)val.value.as_qword;
case et_sqword:
return (int)val.value.as_sqword;
#endif /* HOST_HAS_QWORD */
case et_addr:
return (int)val.value.as_addr;
case et_uint:
return (int)val.value.as_uint;
case et_int:
return val.value.as_int;
case et_symbol:
panic("symbol used in expression");
default:
panic("illegal arithmetic expression conversion");
}
}
/*
* arithmetic intrinsics operations, used during expression evaluation
*/
/* compute <val1> + <val2> */
static struct eval_value_t
f_add(struct eval_value_t val1, struct eval_value_t val2)
{
enum eval_type_t et;
struct eval_value_t val;
/* symbols are not allowed in arithmetic expressions */
if (val1.type == et_symbol || val2.type == et_symbol)
{
eval_error = ERR_BADEXPR;
return err_value;
}
/* get result type, and perform operation in that type */
et = result_type(val1.type, val2.type);
switch (et)
{
case et_double:
val.type = et_double;
val.value.as_double = eval_as_double(val1) + eval_as_double(val2);
break;
case et_float:
val.type = et_float;
val.value.as_float = eval_as_float(val1) + eval_as_float(val2);
break;
#ifdef HOST_HAS_QWORD
case et_qword:
val.type = et_qword;
val.value.as_qword = eval_as_qword(val1) + eval_as_qword(val2);
break;
case et_sqword:
val.type = et_sqword;
val.value.as_sqword = eval_as_sqword(val1) + eval_as_sqword(val2);
break;
#endif /* HOST_HAS_QWORD */
case et_addr:
val.type = et_addr;
val.value.as_addr = eval_as_addr(val1) + eval_as_addr(val2);
break;
case et_uint:
val.type = et_uint;
val.value.as_uint = eval_as_uint(val1) + eval_as_uint(val2);
break;
case et_int:
val.type = et_int;
val.value.as_int = eval_as_int(val1) + eval_as_int(val2);
break;
default:
panic("bogus expression type");
}
return val;
}
/* compute <val1> - <val2> */
static struct eval_value_t
f_sub(struct eval_value_t val1, struct eval_value_t val2)
{
enum eval_type_t et;
struct eval_value_t val;
/* symbols are not allowed in arithmetic expressions */
if (val1.type == et_symbol || val2.type == et_symbol)
{
eval_error = ERR_BADEXPR;
return err_value;
}
/* get result type, and perform operation in that type */
et = result_type(val1.type, val2.type);
switch (et)
{
case et_double:
val.type = et_double;
val.value.as_double = eval_as_double(val1) - eval_as_double(val2);
break;
case et_float:
val.type = et_float;
val.value.as_float = eval_as_float(val1) - eval_as_float(val2);
break;
#ifdef HOST_HAS_QWORD
case et_qword:
val.type = et_qword;
val.value.as_qword = eval_as_qword(val1) - eval_as_qword(val2);
break;
case et_sqword:
val.type = et_sqword;
val.value.as_sqword = eval_as_sqword(val1) - eval_as_sqword(val2);
break;
#endif /* HOST_HAS_QWORD */
case et_addr:
val.type = et_addr;
val.value.as_addr = eval_as_addr(val1) - eval_as_addr(val2);
break;
case et_uint:
val.type = et_uint;
val.value.as_uint = eval_as_uint(val1) - eval_as_uint(val2);
break;
case et_int:
val.type = et_int;
val.value.as_int = eval_as_int(val1) - eval_as_int(val2);
break;
default:
panic("bogus expression type");
}
return val;
}
/* compute <val1> * <val2> */
static struct eval_value_t
f_mult(struct eval_value_t val1, struct eval_value_t val2)
{
enum eval_type_t et;
struct eval_value_t val;
/* symbols are not allowed in arithmetic expressions */
if (val1.type == et_symbol || val2.type == et_symbol)
{
eval_error = ERR_BADEXPR;
return err_value;
}
/* get result type, and perform operation in that type */
et = result_type(val1.type, val2.type);
switch (et)
{
case et_double:
val.type = et_double;
val.value.as_double = eval_as_double(val1) * eval_as_double(val2);
break;
case et_float:
val.type = et_float;
val.value.as_float = eval_as_float(val1) * eval_as_float(val2);
break;
#ifdef HOST_HAS_QWORD
case et_qword:
val.type = et_qword;
val.value.as_qword = eval_as_qword(val1) * eval_as_qword(val2);
break;
case et_sqword:
val.type = et_sqword;
val.value.as_sqword = eval_as_sqword(val1) * eval_as_sqword(val2);
break;
#endif /* HOST_HAS_QWORD */
case et_addr:
val.type = et_addr;
val.value.as_addr = eval_as_addr(val1) * eval_as_addr(val2);
break;
case et_uint:
val.type = et_uint;
val.value.as_uint = eval_as_uint(val1) * eval_as_uint(val2);
break;
case et_int:
val.type = et_int;
val.value.as_int = eval_as_int(val1) * eval_as_int(val2);
break;
default:
panic("bogus expression type");
}
return val;
}
/* compute <val1> / <val2> */
static struct eval_value_t
f_div(struct eval_value_t val1, struct eval_value_t val2)
{
enum eval_type_t et;
struct eval_value_t val;
/* symbols are not allowed in arithmetic expressions */
if (val1.type == et_symbol || val2.type == et_symbol)
{
eval_error = ERR_BADEXPR;
return err_value;
}
/* get result type, and perform operation in that type */
et = result_type(val1.type, val2.type);
switch (et)
{
case et_double:
val.type = et_double;
val.value.as_double = eval_as_double(val1) / eval_as_double(val2);
break;
case et_float:
val.type = et_float;
val.value.as_float = eval_as_float(val1) / eval_as_float(val2);
break;
#ifdef HOST_HAS_QWORD
case et_qword:
val.type = et_qword;
val.value.as_qword = eval_as_qword(val1) / eval_as_qword(val2);
break;
case et_sqword:
val.type = et_sqword;
val.value.as_sqword = eval_as_sqword(val1) / eval_as_sqword(val2);
break;
#endif /* HOST_HAS_QWORD */
case et_addr:
val.type = et_addr;
val.value.as_addr = eval_as_addr(val1) / eval_as_addr(val2);
break;
case et_uint:
val.type = et_uint;
val.value.as_uint = eval_as_uint(val1) / eval_as_uint(val2);
break;
case et_int:
val.type = et_int;
val.value.as_int = eval_as_int(val1) / eval_as_int(val2);
break;
default:
panic("bogus expression type");
}
return val;
}
/* compute - <val1> */
static struct eval_value_t
f_neg(struct eval_value_t val1)
{
struct eval_value_t val;
/* symbols are not allowed in arithmetic expressions */
if (val1.type == et_symbol)
{
eval_error = ERR_BADEXPR;
return err_value;
}
/* result type is the same as the operand type */
switch (val1.type)
{
case et_double:
val.type = et_double;
val.value.as_double = - val1.value.as_double;
break;
case et_float:
val.type = et_float;
val.value.as_float = - val1.value.as_float;
break;
#ifdef HOST_HAS_QWORD
case et_qword:
val.type = et_sqword;
val.value.as_qword = - (sqword_t)val1.value.as_qword;
break;
case et_sqword:
val.type = et_sqword;
val.value.as_sqword = - val1.value.as_sqword;
break;
#endif /* HOST_HAS_QWORD */
case et_addr:
val.type = et_addr;
val.value.as_addr = - val1.value.as_addr;
break;
case et_uint:
if ((unsigned int)val1.value.as_uint > 2147483648U)
{
/* promote type */
#ifdef HOST_HAS_QWORD
val.type = et_sqword;
val.value.as_sqword = - ((sqword_t)val1.value.as_uint);
#else /* !HOST_HAS_QWORD */
val.type = et_double;
val.value.as_double = - ((double)val1.value.as_uint);
#endif /* HOST_HAS_QWORD */
}
else
{
/* don't promote type */
val.type = et_int;
val.value.as_int = - ((int)val1.value.as_uint);
}
break;
case et_int:
if ((unsigned int)val1.value.as_int == 0x80000000U)
{
/* promote type */
val.type = et_uint;
val.value.as_uint = 2147483648U;
}
else
{
/* don't promote type */
val.type = et_int;
val.value.as_int = - val1.value.as_int;
}
break;
default:
panic("bogus expression type");
}
return val;
}
/* compute val1 == 0 */
static int
f_eq_zero(struct eval_value_t val1)
{
int val;
/* symbols are not allowed in arithmetic expressions */
if (val1.type == et_symbol)
{
eval_error = ERR_BADEXPR;
return FALSE;
}
switch (val1.type)
{
case et_double:
val = val1.value.as_double == 0.0;
break;
case et_float:
val = val1.value.as_float == 0.0;
break;
#ifdef HOST_HAS_QWORD
case et_qword:
val = val1.value.as_qword == 0;
break;
case et_sqword:
val = val1.value.as_sqword == 0;
break;
#endif /* HOST_HAS_QWORD */
case et_addr:
val = val1.value.as_addr == 0;
break;
case et_uint:
val = val1.value.as_uint == 0;
break;
case et_int:
val = val1.value.as_int == 0;
break;
default:
panic("bogus expression type");
}
return val;
}
/* evaluate the value of the numeric literal constant in ES->TOK_BUF,
eval_err is set to a value other than ERR_NOERR if the constant cannot
be parsed and converted to an expression value */
static struct eval_value_t /* value of the literal constant */
constant(struct eval_state_t *es) /* expression evaluator */
{
struct eval_value_t val;
int int_val;
unsigned int uint_val;
double double_val;
char *endp;
#ifdef HOST_HAS_QWORD
sqword_t sqword_val;
qword_t qword_val;
#endif /* HOST_HAS_QWORD */
#if 0 /* no longer needed... */
#if defined(sparc) && !defined(__svr4__)
extern long strtol(char *, char **, int);
extern double strtod(char *, char **);
#endif /* sparc */
#endif
/*
* attempt multiple conversions, from least to most precise, using
* the value returned when the conversion is successful
*/
/* attempt integer conversion */
errno = 0;
int_val = strtol(es->tok_buf, &endp, /* parse base */0);
if (!errno && !*endp)
{
/* good conversion */
val.type = et_int;
val.value.as_int = int_val;
return val;
}
/* else, not an integer, attempt unsigned int conversion */
errno = 0;
uint_val = strtoul(es->tok_buf, &endp, /* parse base */0);
if (!errno && !*endp)
{
/* good conversion */
val.type = et_uint;
val.value.as_uint = uint_val;
return val;
}
#ifdef HOST_HAS_QWORD
/* else, not an int/uint, attempt sqword_t conversion */
errno = 0;
sqword_val = myatosq(es->tok_buf, &endp, /* parse base */0);
if (!errno && !*endp)
{
/* good conversion */
val.type = et_sqword;
val.value.as_sqword = sqword_val;
return val;
}
/* else, not an sqword_t, attempt qword_t conversion */
errno = 0;
qword_val = myatoq(es->tok_buf, &endp, /* parse base */0);
if (!errno && !*endp)
{
/* good conversion */
val.type = et_qword;
val.value.as_qword = qword_val;
return val;
}
#endif /* HOST_HAS_QWORD */
/* else, not any type of integer, attempt double conversion (NOTE: no
reliable float conversion is available on all machines) */
errno = 0;
double_val = strtod(es->tok_buf, &endp);
if (!errno && !*endp)
{
/* good conversion */
val.type = et_double;
val.value.as_double = double_val;
return val;
}
/* else, not a double value, therefore, could not convert constant,
declare an error */
eval_error = ERR_BADCONST;
return err_value;
}
/* evaluate an expression factor, eval_err will indicate it any
expression evaluation occurs */
static struct eval_value_t /* value of factor */
factor(struct eval_state_t *es) /* expression evaluator */
{
enum eval_token_t tok;
struct eval_value_t val;
tok = peek_next_token(es);
switch (tok)
{
case tok_oparen:
(void)get_next_token(es);
val = expr(es);
if (eval_error)
return err_value;
tok = peek_next_token(es);
if (tok != tok_cparen)
{
eval_error = ERR_UPAREN;
return err_value;
}
(void)get_next_token(es);
break;
case tok_minus:
/* negation operator */
(void)get_next_token(es);
val = factor(es);
if (eval_error)
return err_value;
val = f_neg(val);
break;
case tok_ident:
(void)get_next_token(es);
/* evaluate the identifier in TOK_BUF */
val = es->f_eval_ident(es);
if (eval_error)
return err_value;
break;
case tok_const:
(void)get_next_token(es);
val = constant(es);
if (eval_error)
return err_value;
break;
default:
eval_error = ERR_NOTERM;
return err_value;
}
return val;
}
/* evaluate an expression term, eval_err will indicate it any
expression evaluation occurs */
static struct eval_value_t /* value to expression term */
term(struct eval_state_t *es) /* expression evaluator */
{
enum eval_token_t tok;
struct eval_value_t val, val1;