/
eval.c
1739 lines (1643 loc) · 46.7 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
/*
* Copyright (C) 1985-1992 New York University
*
* This file is part of the Ada/Ed-C system. See the Ada/Ed README file for
* warranty (none) and distribution info and also the GNU General Public
* License for more details.
*/
#include "hdr.h"
#include "vars.h"
#include "attr.h"
#include "arithprots.h"
#include "setprots.h"
#include "errmsgprots.h"
#include "nodesprots.h"
#include "machineprots.h"
#include "sspansprots.h"
#include "chapprots.h"
#include "miscprots.h"
#include "smiscprots.h"
#include "evalprots.h"
/* Define DETAIL to break up some complicated expresssions into
* several statements to assist debugging using interactive debugger
*/
#define DETAIL
static Const const_val(Symbol);
static Const eval_lit_map(Symbol);
static Const const_fold(Node);
static Const fold_unop(Node);
static Const fold_op(Node);
static Const fold_attr(Node);
static Const fold_convert(Node);
static Const eval_qual_range(Node, Symbol);
static Const eval_real_type_attribute(Node);
static Const check_overflow(Node, Const);
static int *fl_mantissa(int);
static int *fl_emax(int);
static void insert_and_prune(Node, Const);
static Rational fx_max (Rational, Rational);
static Const test_expr(int);
extern Const int_const(), real_const(), rat_const();
extern ADA_MIN_INTEGER;
/* TBSL:provide proper link to ADA_SMALL_REAL*/
#define ADA_SMALL_REAL 0.1
static Const const_val(Symbol obj) /*;const_val*/
{
/* Return the constant value of the object if it has one;
* else return om.
* The constant value of a user-defined constant is derived from
* its SIGNATURE, when this is a constant value.
* The constant value of a literal is obtained from the literal map
* of its type.
*/
Tuple sig;
if (cdebug2 > 3) TO_ERRFILE("const_val");
if (is_literal(obj)) return eval_lit_map(obj);
sig = SIGNATURE(obj);
if( is_constant(obj) && is_scalar_type(TYPE_OF(obj))
&& N_KIND((Node)sig) == as_ivalue) {
return (Const) N_VAL((Node)sig);
/* TBSL: could be static but not constant folded yet. */
}
else return const_new(CONST_OM);
}
static Const eval_lit_map(Symbol obj) /*;eval_lit_map*/
{
Symbol typ;
Tuple tup;
int i;
typ = TYPE_OF(obj);
tup = (Tuple) literal_map(typ);
for (i = 1; i <= tup_size(tup); i += 2) {
if (ORIG_NAME(obj) == (char *)0) continue;
if (streq(tup[i], ORIG_NAME(obj)))
return int_const((int)tup[i+1]);
}
return const_new(CONST_OM);
/*(return literal_map(TYPE_OF(obj))(original_name(obj));*/
}
void eval_static(Node node) /*;eval_static*/
{
/* Top level evaluation of static expressions and constant folding. The
* recursive procedure const_fold is invoked, and a top-level range
* check on numeric results is performed.
*/
/* If the node type is set to as_ivalue, the the N_VAL field will
* be a Const.
*/
Const result;
result = const_fold(node);
if (result->const_kind != CONST_OM)
check_overflow(node, result);
}
static Const const_fold(Node node) /*;const_fold*/
{
/* This recursive procedure evaluates expressions, when static.
* If node is static, its actual value is returned, and the node is
* modified to be an ivalue. Otherwise const_fold returns om, and node
* is untouched. If the static evaluation shows that the expression
* would raise an exception, a ['raise' exception] value is produced
* and placed on the tree.
*/
Fortup ft1;
Node expn, index_list, index, discr_range;
Const result;
Node opn;
Node n2, op_range;
Symbol sym, op_type;
/* */
#define is_simple_value(t) ((t)->const_kind == CONST_INT \
|| (t)->const_kind == CONST_UINT || (t)->const_kind == CONST_REAL)
if (cdebug2 > 3) { }
switch (N_KIND(node)) {
case(as_simple_name):
result = const_val(N_UNQ(node));
break;
case(as_ivalue):
result = (Const) N_VAL(node);
break;
case(as_int_literal):
/* TBSL: assuming int literal already converted check this Const*/
result = (Const) N_VAL(node);
break;
case(as_real_literal):
/*TBSL: assuming real literal already converted */
result = (Const) N_VAL(node);
break;
case(as_string_ivalue):
/* Will be static if required type has static low bound.*/
/* indx := index_type(N_TYPE(node));
* [-, lo_exp, -] := signature(indx);
* * Move this test to the expander, once format of aggregates is known.
* if is_static_expr(lo_exp) then
* lob := N_VAL(lo_exp);
* av := [v : [-, v] in comp_list];
* result := check_null_aggregate(av, lob, indices, node);
* result := ['array_ivalue', [v: [-, v] in comp_list],
* lob, lob + #comp_list - 1];
* else
*/
result = const_new(CONST_OM);
/* end if; */
break;
case(as_character_literal):
result = const_new(CONST_STR);
break;
case(as_un_op):
result = fold_unop(node);
break;
case(as_in):
opn = N_AST1(node);
op_range = N_AST2(node);
result = eval_qual_range(opn, N_TYPE(op_range));
if (is_const_constraint_error(result))
result = test_expr(FALSE);
else if (!is_const_om(result))
result = test_expr(TRUE);
break;
case(as_notin):
opn = N_AST1(node);
n2 = N_AST2(node);
result = eval_qual_range(opn, N_TYPE(n2));
if (is_const_constraint_error(result))
result = test_expr(TRUE);
else if (!is_const_constraint_error(result))
result = test_expr(FALSE);
break;
case(as_op):
result = fold_op(node);
break;
case(as_call):
{
int i;
Tuple arg_list;
Const arg;
opn = N_AST1(node);
result = const_new(CONST_OM); /* in general not static */
arg_list = N_LIST(N_AST2(node)); /* but can fold actuals. */
for (i = 1; i <= tup_size(arg_list); i++)
arg = const_fold((Node)arg_list[i]);
if (N_KIND(opn) == as_simple_name) {
sym = ALIAS(N_UNQ(opn));
if (sym != (Symbol)0 && is_literal(sym))
/* replace call by actual value of literal */
result = eval_lit_map(sym);
}
}
break;
case(as_parenthesis):
/* If the parenthesised expression is evaluable, return
* its value. Otherwise leave it parenthesised.
*/
opn = N_AST1(node);
result = const_fold(opn);
break;
case(as_qual_range):
opn = N_AST1(node);
op_type = N_TYPE(node);
result = eval_qual_range(opn, op_type);
if (is_const_constraint_error(result)) {
create_raise(node, symbol_constraint_error);
result = const_new(CONST_OM);
}
break;
case(as_qual_index):
eval_static(N_AST1(node));
result = const_new(CONST_OM);
break;
case(as_attribute):
case(as_range_attribute):
/* use separate procedure for C */
result = fold_attr(node);
break;
case(as_qualify):
if (fold_context)
result = const_fold(N_AST2(node));
else
/* in the context of a conformance check, keep qualification.*/
result = const_new(CONST_OM);
break;
/* Type conversion:
* /TBSL/ These conversions are not properly checked!
*/
case(as_convert):
/* use separate procedure for C */
result = fold_convert(node);
break;
case(as_array_aggregate):
/* This is treated in the expander.*/
result = const_new(CONST_OM);
break;
case(as_record_aggregate):
result = const_new(CONST_OM);
break;
case(as_selector): /*TBSL Case for discriminants needed */
expn = N_AST1(node);
eval_static(expn);
return const_new(CONST_OM);
case(as_slice):
expn = N_AST1(node);
discr_range = N_AST2(node);
eval_static(expn);
eval_static(discr_range);
return const_new(CONST_OM);
case(as_row): /* Not folded for now.*/
/* p1 := check_const_val(op1);
* if is_value(op1) then
* result := ['array_ivalue', [op1(2)], 1, 1];
* else
*/
return const_new(CONST_OM);
case(as_index):
expn = N_AST1(node);
index_list = N_AST2(node);
eval_static(expn);
FORTUP(index = (Node), N_LIST(index_list), ft1)
eval_static(index);
ENDFORTUP(ft1);
return const_new(CONST_OM);
default:
result = const_new(CONST_OM);
}
if (result->const_kind != CONST_OM)
insert_and_prune(node, result);
return result;
}
static Const fold_unop(Node node) /*;fold_unop*/
{
Node opn, oplist;
Const result, op1;
int op1_kind;
Symbol sym;
opn = N_AST1(node);
oplist = N_AST2(node);
op1 = const_fold((Node) (N_LIST(oplist))[1]);
if (is_const_om(op1)) return op1;
op1_kind = op1->const_kind;
sym = N_UNQ(opn);
if (sym == symbol_addui) {
/* the "+" can be ignored if it is used as a unary op */
result = op1;
}
else if (sym == symbol_addufl) {
result = op1;
}
else if (sym == symbol_addufx) {
result = op1;
}
else if (sym == symbol_subui ||
sym == symbol_subufl || sym == symbol_subufx) {
if (is_simple_value(op1)) {
if (sym == symbol_subui) {
if (is_const_int(op1)) {
if (INTV(op1) == ADA_MIN_INTEGER) {
create_raise(node, symbol_constraint_error);
result = const_new(CONST_OM);
}
else {
result = int_const(-INTV(op1));
}
}
else if (is_const_uint(op1))
result = uint_const(int_umin(UINTV(op1)));
else chaos("eval:subui bad type");
}
else if (sym == symbol_subufl) {
const_check(op1, CONST_REAL);
result = real_const(-REALV(op1));
}
}
else {
const_check(op1, CONST_RAT);
result= rat_const(rat_umin(RATV(op1)));
}
}
else if ( sym == symbol_not) {
if (is_simple_value (op1)) {
if (op1_kind == CONST_INT)
result = int_const(1-INTV(op1)); /*bnot in setl */
else chaos("fold_unop: bad kind");
}
else { /*TBSL*/
result = const_new(CONST_OM);
}
}
else if ( sym == symbol_absi ||
sym == symbol_absfl || sym == symbol_absfx) {
if (is_simple_value(op1)) {
if (sym == symbol_absi) {
if (op1_kind == CONST_INT) result = int_const(abs(INTV(op1)));
else if (op1_kind == CONST_UINT)chaos("fold_unit absi in uint");
else chaos("fold_unop: bad kind");
}
else if (sym == symbol_absfl) {
result = real_const(fabs(REALV(op1)));
}
}
else {
result= rat_const(rat_abs(RATV(op1)));
}
}
return result;
}
static Const fold_op(Node node) /*;fold_op*/
{
Node opn, arg1, arg2, oplist;
Const result, op1, op2, tryc;
Symbol sym, op_name;
int *uint;
int rm;
Tuple tup;
int res, overflow;
opn = N_AST1(node);
oplist = N_AST2(node);
tup = N_LIST(oplist);
arg1 = (Node) tup[1];
arg2 = (Node) tup[2];
op1 = const_fold(arg1);
op2 = const_fold(arg2);
op_name = N_UNQ(opn);
/* If either operand raises and exception, so does the operation */
if (N_KIND(arg1) == as_raise) {
copy_attributes(arg1, node);
return const_new(CONST_OM);
}
if (N_KIND(arg2) == as_raise
&& op_name != symbol_andthen && op_name != symbol_orelse) {
copy_attributes(arg2, node);
return const_new(CONST_OM);
}
if (is_const_om(op1) || (is_const_om(op2)
&& (op_name != symbol_in || op_name != symbol_notin))) {
return const_new(CONST_OM);
}
sym = op_name;
if ( sym == symbol_addi || sym == symbol_addfl) {
if (sym == symbol_addi) {
res = word_add(INTV(op1), INTV(op2), &overflow);
if (overflow) {
create_raise(node, symbol_constraint_error);
result = const_new(CONST_OM);
}
else result = int_const(res);
}
else
result = real_const(REALV(op1) + REALV(op2));
}
else if ( sym == symbol_addfx) {
const_check(op1, CONST_RAT);
const_check(op2, CONST_RAT);
result= rat_const(rat_add(RATV(op1), RATV(op2)));
}
else if ( sym == symbol_subi) {
if (is_const_int(op1)) {
if (is_const_int(op2)) {
res = word_sub(INTV(op1), INTV(op2), &overflow);
if (overflow) {
create_raise(node, symbol_constraint_error);
result = const_new(CONST_OM);
}
else result = int_const(res);
}
else {
chaos("fold_op: subi operand types");
}
}
}
else if (sym == symbol_subfl) {
result = real_const(REALV(op1) - REALV(op2));
}
else if ( sym == symbol_subfx) {
const_check(op1, CONST_RAT);
const_check(op2, CONST_RAT);
result= rat_const(rat_sub(RATV(op1), RATV(op2)));
}
else if ( sym == symbol_muli) {
#ifdef TBSL
-- need to check for overflow and convert result back to int if not
-- note that low-level setl is missing calls to check_overflow that
-- are present in high-level and should be in low-level as well
result = int_mul(int_fri(op1), int_fri(op2));
#endif
/* until overflow check in */
const_check(op1, CONST_INT);
const_check(op2, CONST_INT);
res = word_mul(INTV(op1), INTV(op2), &overflow);
if (overflow) {
create_raise(node, symbol_constraint_error);
result = const_new(CONST_OM);
}
else result = int_const(res);
}
else if ( sym == symbol_mulfl) {
const_check(op1, CONST_REAL);
const_check(op2, CONST_REAL);
if ((fabs(REALV(op1)) < ADA_SMALL_REAL)
|| (fabs(REALV(op2)) < ADA_SMALL_REAL)) {
result = real_const(0.0);
}
else if (log(fabs(REALV(op1))) +
log(fabs(REALV(op2))) > ADA_MAX_REAL) {
create_raise(node, symbol_constraint_error);
return const_new(CONST_OM);
}
else
result = real_const(REALV(op1) * REALV(op2));
}
else if ( sym == symbol_mulfx) {
const_check(op1, CONST_RAT);
const_check(op2, CONST_RAT);
result = rat_const(rat_mul(RATV(op1), RATV(op2)));
}
else if (sym == symbol_mulfxi || sym == symbol_mulfli) {
const_check(op1, CONST_RAT);
const_check(op2, CONST_RAT);
result = rat_const(rat_fri(int_mul(num(RATV(op1)), UINTV(op2)),
den(RATV(op1))));
}
else if (sym == symbol_mulifx) {
const_check(op1, CONST_UINT);
const_check(op2, CONST_RAT);
result = rat_const(rat_fri(int_mul(UINTV(op1), num(RATV(op2))),
den(RATV(op2))));
}
else if (sym == symbol_divi) {
if (INTV(op2)== 0) {
create_raise(node, symbol_constraint_error);
return const_new(CONST_OM);
}
result = int_const(INTV(op1) / INTV(op2));
}
else if (sym == symbol_divfl) {
const_check(op2, CONST_REAL);
if (fabs(REALV(op2)) < ADA_SMALL_REAL) {
create_raise(node, symbol_constraint_error);
return const_new(CONST_OM);
}
else if (fabs(REALV(op1)) < ADA_SMALL_REAL) {
const_check(op1, CONST_REAL);
result = real_const(0.0);
}
else if (log(fabs(REALV(op1))) -
log(fabs(REALV(op2))) >log(ADA_MAX_REAL)) {
create_raise(node, symbol_constraint_error);
return const_new(CONST_OM);
}
else {
result = real_const(REALV(op1)
/ REALV(op2));
}
}
else if (sym == symbol_divfx) {
/* TBSL: note that rnum(rat2) is in long integer format */
if (int_eqz(num(RATV(op2)))) {
create_raise(node, symbol_constraint_error);
return const_new(CONST_OM);
}
result = rat_const(rat_div(RATV(op1), RATV(op2)));
}
else if (sym == symbol_divfxi || sym == symbol_divfli) {
const_check(op1, CONST_RAT);
if (is_const_int(op2)) {
if (!INTV(op2)) {
create_raise(node, symbol_constraint_error);
return const_new(CONST_OM); }
result = rat_const(rat_fri(num(RATV(op1)), int_mul(den(RATV(op1)),
int_fri(INTV(op2))))); }
/* Shouldn't be a rational
else if (is_const_rat(op2)) {
if (int_eqz(num(RATV(op2)))) {
create_raise(node, symbol_constraint_error);
return const_new(CONST_OM); }
result = rat_const(rat_div(RATV(op1), RATV(op2))); }
*/
else {
const_check(op2, CONST_UINT);
if (int_eqz(UINTV(op2))) {
create_raise(node, symbol_constraint_error);
return const_new(CONST_OM); }
result = rat_const(rat_fri(num(RATV(op1)), int_mul(den(RATV(op1)),
UINTV(op2))));
}
}
else if (sym == symbol_remi) {
if (INTV(op2) == 0) {
create_raise(node, symbol_constraint_error);
return const_new(CONST_OM);
}
result = int_const(INTV(op1) - (INTV(op1) / INTV(op2)) * INTV(op2));
}
else if (sym == symbol_modi) {
if (INTV(op2) == 0) {
create_raise(node, symbol_constraint_error);
return const_new(CONST_OM);
}
rm = INTV(op1) % INTV(op2);
if ((rm == 0) || (INTV(op2) > 0))
result = int_const(rm);
else
result = int_const(rm + INTV(op2));
}
else if (sym == symbol_expi) {
if (INTV(op2) < 0) {
create_raise(node, symbol_constraint_error);
return const_new(CONST_OM);
}
else {
if (is_const_int(op1))
uint = int_fri(INTV(op1));
else
chaos("expi: bad kind");
const_check(op2, CONST_INT);
result = int_const(int_toi(int_exp(uint, int_fri(INTV(op2)))));
}
}
else if (sym == symbol_expfl) {
const_check(op1, CONST_REAL);
const_check(op2, CONST_INT);
if ((fabs(REALV(op1)) < ADA_SMALL_REAL)
|| ((abs(INTV(op2)) * log (fabs( REALV(op1)))) > log(ADA_MAX_REAL))) {
create_raise(node, symbol_constraint_error);
return const_new(CONST_OM);
}
return const_new(CONST_OM);
#ifdef TBSL
-- need to find C form for **
pow(x, y) is x**y with x an y both double.
result = op1 ** op2;
#endif
}
else if ((sym == symbol_cat) || (sym == symbol_cat_ca)
|| (sym == symbol_cat_ac) || (sym == symbol_cat_cc)) {
/* /TBSL/ Bounds may not be correct!*/
/* [-, agg1, lb1, ub1] := op1;
* [-, agg2, lb2, ub2] := op2;
* agg := agg1 + agg2;
* lb := lb1 min lb2;
*/
result = const_new(CONST_OM);
}
else if (sym == symbol_and || sym == symbol_or || sym == symbol_xor) {
if (is_simple_value(op1)) {
if (N_UNQ(opn) == symbol_and) {
if (is_const_int(op1))
result = int_const(INTV(op1)&&INTV(op2));
else
chaos("fold_unop: bad kind");
}
else if (N_UNQ(opn) == symbol_or) {
if (is_const_int(op1))
result = int_const(INTV(op1)||INTV(op2));
else
chaos("fold_unop: or bad kind");
}
else if (N_UNQ(opn) == symbol_xor) {
result = test_expr((INTV(op1))!= (INTV(op2)));
}
else {
chaos("ERROR IN ES99");
}
}
}
else if (sym == symbol_andthen || sym == symbol_orelse) {
/* not static */
result = const_new(CONST_OM);
}
else if (sym == symbol_eq) {
#ifdef TBSN
if (is_universal_real(op1) && is_universal_real(op2))
result = test_expr(rat_eql(op1, op2));
else
result = test_expr(op1 == op2);
#endif
if (const_same_kind(op1, op2))
return test_expr(const_eq(op1, op2));
else return int_const(FALSE);
}
else if (sym == symbol_ne) {
#ifdef TBSN
if (is_universal_real(op1) && is_universal_real(op2)) {
result = test_expr(rat_neq(op1, op2));
}
else {
/*TBSL: do we need two cases here */
if (is_const_int(op1))
result = int_const(INTV(op1) != INTV(op2));
else if (is_const_real(op1))
result = test_expr((REALV(op1) != REALV(op2)));
else
chaos("error in ne case in const_fold");
}
#endif
if (const_same_kind(op1, op2))
return test_expr(const_ne(op1, op2));
else return int_const(FALSE);
}
else if (sym == symbol_lt) {
if (is_simple_value(op1)) {
#ifdef TBSN
if (is_const_int(op1)) {
result = int_const(INTV(op1) < INTV(op2));
}
else {
if (is_const_real(op1)) {
result = real_const(REALV(op1)
< REALV(op2));
}
else {
chaos("fold_unop: lt bad kind ");
}
}
#endif
if (const_same_kind(op1, op2))
return test_expr(const_lt(op1, op2));
else return int_const(FALSE);
}
/*TBSL need array types */
else if (is_const_rat (op1) && is_const_rat (op2)) {
result = test_expr(rat_lss (RATV (op1), RATV (op2)));
}
else {
result = const_new(CONST_OM);
}
}
else if (sym == symbol_le) {
if (is_simple_value(op1)) {
#ifdef TBSN
if (is_const_int(op1)) {
result = int_const(INTV(op1) <= INTV(op2));
}
else if (is_const_real(op1)) {
result = real_const(REALV(op1) <= REALV(op2));
}
else {
chaos("fold_op: le bad kind");
}
#endif
if (const_same_kind(op1, op2))
return test_expr(const_le(op1, op2));
else return int_const(FALSE);
}
else { /*TBSL need array types */
if (is_const_rat (op1) && is_const_rat (op2))
result = test_expr(rat_leq (RATV (op1), RATV (op2)));
else
result = const_new(CONST_OM);
}
}
else if (sym == symbol_gt) {
if (is_simple_value(op1)) {
#ifdef TBSN
if (is_const_int(op1)) {
result = int_const(INTV(op1) > INTV(op2));
}
else if (is_const_real(op1)) {
result = real_const(REALV(op1)
> REALV(op2));
}
else {
chaos("fold_op: gt bad kind");
}
#endif
if (const_same_kind(op1, op2))
return test_expr(const_gt(op1, op2));
else return int_const(FALSE);
}
else { /*TBSL need array types */
if (is_const_rat (op1) && is_const_rat (op2))
result = test_expr(rat_gtr (RATV (op1), RATV (op2)));
result = const_new(CONST_OM);
}
}
else if (sym == symbol_ge) {
if (is_simple_value(op1)) {
#ifdef TBSN
if (is_const_int(op1))
result = int_const(INTV(op1) >= INTV(op2));
else if (is_const_real(op1))
result = real_const(REALV(op1) >= REALV(op2));
else
chaos("fold op ge bad kind");
#endif
if (const_same_kind(op1, op2))
return test_expr(const_ge(op1, op2));
else
return int_const(FALSE);
}
else { /*TBSL need array types */
if (is_const_rat (op1) && is_const_rat (op2))
result = test_expr(rat_geq (RATV (op1), RATV (op2)));
result = const_new(CONST_OM);
}
}
else if (sym == symbol_in || sym == symbol_notin) {
specialize(arg1, N_TYPE(arg2)); /* ?? */
/* check whether this is correct, SETL is TYPE_OF, which is WRONG!!*/
if (N_KIND(arg2) != as_simple_name) {
result = const_new(CONST_OM); /* Could do better. */
}
else {
tryc = eval_qual_range(opn, N_UNQ(arg2));
if (is_const_constraint_error(tryc))
result = test_expr(op_name == symbol_notin);
else if (!is_const_om(tryc))
result= test_expr(op_name == symbol_in);
}
}
else {
printf("bad operator symbol: %s\n", nature_str(NATURE(sym)));
chaos("fold_op: bad operator");
}
return result;
}
static Const fold_attr(Node node) /*;fold_attr*/
{
Node attr_node, typ_node, arg_node, f_node, l_node, l_n, h_n;
Symbol typ1;
int attrkind, is_t_n, rv, i, len, max;
Const first, last, op1, result, lo, hi;
Tuple tsig, sig, l;
Span save_span;
attr_node = N_AST1(node);
typ_node = N_AST2(node);
arg_node = N_AST3(node);
/* Try to fold the prefix of the attribute*/
eval_static(typ_node);
/*attr = N_VAL(attr_node); -- should be dead 3-13-86 ds */
attrkind = (int) attribute_kind(node);
if (N_KIND(typ_node) != as_simple_name) {
/*Not for attribute COUNT. beware!*/
typ1 = N_TYPE(typ_node);
}
else {
typ1 = N_UNQ(typ_node);
}
is_t_n = is_type_node(typ_node);
/* For array attributes, we establish whether it is being
* applied to an object or to a type. The two operations
* are distinguished in the interpreter by prefix O_ or T_
*/
if ((attrkind == ATTR_T_FIRST || attrkind == ATTR_T_LAST
|| attrkind == ATTR_T_RANGE || attrkind == ATTR_T_LENGTH )
&& can_constrain(typ1) ) {
errmsg( "attribute cannot be applied to unconstrained array type",
"3.6.2", attr_node);
}
else if (attrkind == ATTR_T_SIZE || attrkind == ATTR_O_SIZE) {
node = size_attribute(node);
if (N_KIND(node) == as_ivalue) {
return (Const) N_VAL(node);
}
else {
return const_new(CONST_OM);
}
}
else if (attrkind == ATTR_BASE) {
save_span = get_left_span(node);
N_KIND(node) = as_simple_name;
/* clear attribute code so won't be taken as string*/
N_VAL(node) = (char *)0;
N_UNQ(node) = base_type(typ1);
set_span(node, save_span);
return const_new(CONST_OM);
}
if (!is_t_n)return const_new(CONST_OM);
/* This was needed in the high level, to prevent extra
* folding in non-static cases. It may be superfluous here
*/
/* Attributes that are functions take the base type */
if (attrkind == ATTR_BASE || attrkind == ATTR_POS || attrkind == ATTR_PRED
||attrkind == ATTR_SUCC || attrkind == ATTR_VAL
|| attrkind == ATTR_VALUE) {
N_UNQ(typ_node) = base_type(typ1);
}
if (arg_node != OPT_NODE) {
op1 = const_fold(arg_node);
if (is_const_om(op1))return const_new(CONST_OM);
}
/* They are evaluable statically only if the subtype typ1
* itself is static.
*/
if (is_type(typ1) && is_static_subtype(typ1)
|| is_task_type(TYPE_OF(typ1))
|| attrkind == ATTR_T_CONSTRAINED || attrkind == ATTR_O_CONSTRAINED) {
; /* try to evaluate */
}
else {
return const_new(CONST_OM); /* not static (RM 4.9 (8)*/
}
if (is_generic_type(typ1)) return const_new(CONST_OM);
if (is_static_subtype(typ1)) {
tsig = SIGNATURE(typ1);
f_node = (Node) tsig[2];
l_node = (Node) tsig[3];
first = const_fold(f_node);
last = const_fold(l_node);
}
/* Attributes of SCALAR types or ARRAY types: */
if (attrkind == ATTR_O_FIRST || attrkind == ATTR_T_FIRST)
result = first;
else if (attrkind == ATTR_O_LAST || attrkind == ATTR_T_LAST)
result = last;
else if ( attrkind == ATTR_O_LENGTH || attrkind == ATTR_T_LENGTH
|| attrkind == ATTR_O_RANGE || attrkind == ATTR_T_RANGE)
result = const_new(CONST_OM);
/* Attributes of DISCRETE types: */
else if (attrkind == ATTR_IMAGE) {
Symbol btyp1;
char *image;
Tuple tup;
int tsize;
btyp1 = root_type(typ1);
image = emalloct(10, "fold-attr");
if (btyp1 == symbol_integer) {
const_check(op1, CONST_INT);
if (INTV(op1) >= 0) sprintf(image, " %d", INTV(op1));
else sprintf(image, "%d", INTV(op1));
}
else {
/* image :=
* if exists [nam, v] in literal_map(btyp1) | op1 = v
* then nam else '' end;
*/
image = "";
tup = (Tuple) literal_map(btyp1);
tsize = tup_size(tup);
for (i = 1; i <= tsize; i += 2) {
const_check(op1, CONST_INT);
if ((int)tup[i+1] == INTV(op1)) {
image = strjoin(tup[i], "");
break;
}
}
}
N_KIND(node) = as_string_ivalue;
/* N_VAL(node) = [abs c : c in image]; */
tsize = strlen(image);
tup = tup_new(tsize);
for (i = 1; i <= tsize; i++)
tup[i] = (char *) image[i-1];
if (N_AST1_DEFINED(N_KIND(node))) N_AST1(node) = (Node) 0;
if (N_AST2_DEFINED(N_KIND(node))) N_AST2(node) = (Node) 0;
if (N_AST3_DEFINED(N_KIND(node))) N_AST3(node) = (Node) 0;
if (N_AST4_DEFINED(N_KIND(node))) N_AST4(node) = (Node) 0;
N_VAL(node) = (char *) tup;
result = const_new(CONST_OM);
}
else if (attrkind == ATTR_VALUE) {
chaos("value attrobute (eval.c)");
}
else if (attrkind == ATTR_POS) {
const_check(op1, CONST_INT);
result = uint_const(int_fri(INTV(op1))); /*$ES10*/
/* result = int_const(int_fri(op1)); */ /*$ES10*/
}
else if (attrkind == ATTR_VAL || attrkind == ATTR_PRED
|| attrkind == ATTR_SUCC) {
const_check(op1, CONST_INT);
rv = INTV(op1);
sig = SIGNATURE(base_type(typ1));
if (sig != (Tuple)0) {
l_n = (Node) sig[2];
h_n = (Node) sig[3];
}
else {
l_n = (Node) 0;
h_n = (Node) 0;
}
lo = const_fold(l_n);
hi = const_fold(h_n);
if (is_const_om(lo) || is_const_om(hi)) {
return const_new(CONST_OM);
}
if (attrkind == ATTR_PRED) {
const_check(lo, CONST_INT);
if (rv > INTV(lo)) rv -= 1;
else {
create_raise(node, symbol_constraint_error);
return const_new(CONST_OM);
}
}
else if (attrkind == ATTR_SUCC) {
const_check(hi, CONST_INT);
if (rv < INTV(hi)) rv += 1;
else {
create_raise(node, symbol_constraint_error);
return const_new(CONST_OM);
}
}
else if (attrkind == ATTR_VAL) {
const_check(lo, CONST_INT);
const_check(hi, CONST_INT);
if (rv < INTV(lo) || rv > INTV(hi)) {
create_raise(node, symbol_constraint_error);
return const_new(CONST_OM);
}
}
result = int_const(rv);
}
else if (attrkind == ATTR_WIDTH) {
int first_val, last_val, max_val;
if (root_type(typ1) == symbol_integer) {
if (is_const_om(first) || is_const_om(last))
chaos("eval WIDTH: unexpected const_kind");
const_check(first, CONST_INT);
const_check(last, CONST_INT);
/* In the case of a null range the Width is defined as 0.
* Otherwise it is defined as the maximum IMAGE length for
* all values of the subtype.
*/
if (INTV(first) > INTV(last))
result = uint_const(int_fri(0));
else {