File: v2cc-const-fold.cc

package info (click to toggle)
freehdl 0.0.8-2.2
  • links: PTS
  • area: main
  • in suites: buster, sid, stretch
  • size: 8,632 kB
  • ctags: 10,443
  • sloc: cpp: 45,275; sh: 11,405; yacc: 4,206; ansic: 2,026; lex: 486; perl: 430; makefile: 390; tcl: 100
file content (1522 lines) | stat: -rw-r--r-- 49,497 bytes parent folder | download | duplicates (5)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
#include <freehdl/vaul.h>
#include "v2cc-chunk.h"
#include <algorithm>
#include <stdlib.h>
#include <ctype.h>
#if HAVE_MALLOC_H
#include <malloc.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <math.h>
#include <limits.h>

#include "mapping.h"
#include "v2cc-util.h"



// Used to generate error messages
extern vaul_error_printer codegen_error;



// ******************************************************************************************
// Name: m_constant_fold, generic function
//
// Description: performs constant folding optimizations on nodes
//
// Return value: returns number of errors found during folding
//
// ******************************************************************************************


int
m_constant_fold (pIIR_Type st, RegionStack &rstack)
{
  if (done(st) & CONST_FOLD) return 0; else  done(st) |= CONST_FOLD;

  int error_count = 0;
  if (st->declaration != NULL)
    error_count += constant_fold(st->declaration->type, rstack);

  return error_count;
}


int
m_constant_fold (pIIR_PhysicalType st, RegionStack &rstack)
{
  if (done(st) & CONST_FOLD) return 0; else  done(st) |= CONST_FOLD;
  
  int error_count = 0;
  if (st->base != NULL)
    error_count += constant_fold(st->base, rstack);
  
  for (pIIR_UnitList ul = st->units; ul; ul = ul->rest)
    if (ul->first->multiplier)
      error_count += constant_fold(ul->first->multiplier, rstack);

  return error_count;
}


int
m_constant_fold (pIIR_ScalarSubtype st, RegionStack &rstack)
{
  if (done(st) & CONST_FOLD) return 0; else  done(st) |= CONST_FOLD;
  
  int error_count = 0;
  error_count += constant_fold(st->immediate_base, rstack);

  if (st->range != NULL)
    error_count += constant_fold(st->range, rstack);
  
  return error_count;
}


int
m_constant_fold (pIIR_ArrayType st, RegionStack &rstack)
{
  if (done(st) & CONST_FOLD) return 0; else  done(st) |= CONST_FOLD;
  
  int error_count = 0;
  for (pIIR_TypeList tl = st->index_types; tl; tl = tl->rest)
    error_count += constant_fold(tl->first, rstack);
  
  error_count += constant_fold(st->element_type, rstack);

  return error_count;
}


int
m_constant_fold (pIIR_ArraySubtype st, RegionStack &rstack)
{
  if (done(st) & CONST_FOLD) return 0; else  done(st) |= CONST_FOLD;
  
  int error_count = 0;
  error_count += constant_fold(st->immediate_base, rstack);
  for (pIIR_TypeList tl = st->constraint; tl; tl = tl->rest)
    error_count += constant_fold(tl->first, rstack);
  
  return error_count;
}


int
m_constant_fold (pIIR_QualifiedExpression e, RegionStack &rstack)
{
  if (done(e) & CONST_FOLD) return 0; else  done(e) |= CONST_FOLD;
  
  int error_count = 0;
  error_count += constant_fold(e->type_mark, rstack);
  error_count += constant_fold(e->expression, rstack);
  
  return error_count;
}


int
m_constant_fold(pIIR_AttrSigFunc ase, RegionStack &rstack)
{
  if (done(ase) & CONST_FOLD) return 0; else  done(ase) |= CONST_FOLD;

  int error_count = constant_fold(ase->signal, rstack);

  return error_count;
}


int
m_constant_fold(pIIR_ArrayLiteralExpression le, RegionStack &rstack)
{
  if (done(le) & CONST_FOLD) return 0; else  done(le) |= CONST_FOLD;

  int error_count = 0;
  // Get the valid enumeration items
  pIIR_EnumerationLiteralList enum_item_list = 
    pIIR_EnumerationType(get_base_type(pIIR_ArrayType(get_base_type(le->subtype))->element_type))->enumeration_literals;

  char *str = le->value->text.to_chars() + 1; // Skip leading "
  int length = strlen(str) - 1; // Get length of array
  vector<int> literal_value(length);

  // Get enumeration pos for each item
  int i = 0;
  string item = "'"; 
  while (true) {
    if (*str == '"' && *(str + 1) == '\0') break; // Skip trailing "
    item = string("'") + *str + string("'");
    literal_value[i] = literal_to_pos(enum_item_list, item);
    i++;
    str++;
  }

  // Store folded value
  folded_value(le).array_literal_value() = literal_value;
  valid_folded_value(le) = true;

  // Currently array literals are not folded!
  return error_count;
}


int
m_constant_fold(pIIR_EnumLiteralReference le, RegionStack &rstack)
{
  if (done(le) & CONST_FOLD) return 0; else  done(le) |= CONST_FOLD;

  int error_count = constant_fold(le->value, rstack);
  folded_value(le).long_value() = le->value->enum_pos;
  valid_folded_value(le) = true;

  return error_count;
}


int
m_constant_fold(pIIR_CharacterLiteral le, RegionStack &rstack)
{
  if (done(le) & CONST_FOLD) return 0; else  done(le) |= CONST_FOLD;

  int error_count = 0;
  folded_value(le).long_value() = (long long int)(le->text.to_chars())[1];
  valid_folded_value(le) = true;

  return error_count;
}


int
m_constant_fold(pIIR_StringLiteral le, RegionStack &rstack)
{
  if (done(le) & CONST_FOLD) return 0; else  done(le) |= CONST_FOLD;

  int error_count = 0;
  // String literals are currently not folded
  return error_count;
}



int
m_constant_fold(pIIR_EnumerationLiteral el, RegionStack &rstack)
{
  if (done(el) & CONST_FOLD) return 0; else  done(el) |= CONST_FOLD;

  folded_value(el).long_value() = lint(el->enum_pos);
  valid_folded_value(el) = true;

  return 0;
}


int
m_constant_fold(pIIR_IntegerLiteral le, RegionStack &rstack)
{
  if (done(le) & CONST_FOLD) return 0; else  done(le) |= CONST_FOLD;

  pair<bool, StaticLint> result = string_to_lint (le->text.to_chars());
  folded_value(le) = result.second;
  valid_folded_value(le) = result.first;

  // Retrun an error of the constant could not be folded!
  return  result.first? 0 : 1;
}


int
m_constant_fold(pIIR_FloatingPointLiteral le, RegionStack &rstack)
{
  if (done(le) & CONST_FOLD) return 0; else  done(le) |= CONST_FOLD;

  pair<bool, StaticDouble> result = string_to_double (le->text.to_chars());
  folded_value(le) = result.second;
  valid_folded_value(le) = result.first;

  // Retrun an error of the constant could not be folded!
  return  result.first? 0 : 1;
}


int
m_constant_fold(pIIR_AbstractLiteralExpression ale, RegionStack &rstack)
{
  if (done(ale) & CONST_FOLD) return 0; else  done(ale) |= CONST_FOLD;

  int error_count = constant_fold(ale->value, rstack);
  if (valid_folded_value(ale->value))
    {
      folded_value(ale) =  folded_value(ale->value);
      valid_folded_value(ale) = valid_folded_value(ale->value);
    }

  return error_count;
}

// function is used to fold a scalar value
template<class T, class A>
T perform_operation(pIIR_FunctionCall fc, A a)
{
  string op_str = fc->function->declarator->text.to_chars();

  if (op_str == "\"not\"")
    return T(int(a)? 0 : 1);
  else if (op_str == "\"+\"")
    return T(a);
  else if (op_str == "\"-\"")
    return T(A(-1) * a);
  else if (op_str == "\"abs\"")
    return T((a < 0)? -a : a);
  else
    assert(false);
}

// Returns 0 if parameter is equal to 0, -1 if the parameter is less
// than 0, 1 otherwise
template<class X>
int sign(const X a)
{
  if (a == X(0))
    return 0;
  else if (a < X(0))
    return -1;
  else 
    return 1;
}


// Overloaded functions returning the min/max values of the
// corresponding type. Note that the parameter dummy is actually only
// used to select the corresponding function.
double max_huge_value(double dummy) { return HUGE_VAL; }
double min_huge_value(double dummy) { return -HUGE_VAL; }
lint max_huge_value(lint dummy) { return PHYSICAL_MAX; }
lint min_huge_value(lint dummy) { return PHYSICAL_MIN; }

//*******************************************************
// Add operations
//*******************************************************
template<class X, class Y>
X calculate_plus(const X a, const Y b)
{
  X result = a + b;

  // Check whether the sign of the result is ok
  if (abs(sign(a) + sign(b)) == 2 &&
      sign(a) != sign(result))
    // If the result is wrong then return either the max or the min
    // lint value
    return sign(a)==1? max_huge_value(a) : min_huge_value(a);
  else
    // Return result of the operation
    return result;
}

double
calculate_plus(const double a, const double b)
{
  return a + b; // If doubles are added we don't have to check the
  // result as this is done by the floating point operator
}

//*******************************************************
// Subtract operations
//*******************************************************
template<class X, class Y>
X calculate_minus(const X a, const Y b)
{
  X result = a - b;

  // Check whether the sign of the result is ok
  if (abs(sign(a) - sign(b)) == 2 &&
      sign(a) != sign(result))
    // If the result is wrong then return either the max or the min
    // lint value
    return sign(a)==1? max_huge_value(a) : min_huge_value(a);
  else
    // Return result of the operation
    return result;
}

double
calculate_minus(const double a, const double b)
{
  return a - b; // If doubles are subtracted we don't have to check
  // the result as this is done by the floating point operator
}

//*******************************************************
// Multiply operations
//*******************************************************
template<class X, class Y>
X calculate_mult(const X a, const Y b)
{
  X result = a * b;

  // Check whether at least one parameter is equal to 0 or the result
  // is reasonable. Note that we will compare result / a == b as well
  // as result / b == a as X and Y may be different types (e.g.,
  // double and long long int).
  if (a == X(0) || b == Y(0) ||
      (Y(result / a) == b) || 
      (X(result / b) == a))
    return result;
  else
    return sign(a) * sign(b) == -1? min_huge_value(a) : max_huge_value(a);
}

double
calculate_mult(const double a, const double b)
{
  return a * b; // If doubles are multiplied we don't have to check
  // the result as this is done by the floating point operator
}

//*******************************************************
// Divide operations
//*******************************************************
template<class X, class Y>
X calculate_div(const X a, const Y b)
{
  // Check second parameter
  if (b == Y(0))
    // If the second parameter is 0 then return either the maximum
    // lint or the min lint value depending on the sign of the first
    // parameter
    return sign(a) == -1? min_huge_value(a) : max_huge_value(a);
  else
    return X(a / b);
}

double
calculate_div(const double a, const double b)
{
  return a / b; // If doubles are divided we don't have to check
  // the result as this is done by the floating point operator
}


// function is used to fold two scalar values
template<class T, class A, class B>
T perform_operation(pIIR_FunctionCall fc, A a, B b)
{
  string op_str = fc->function->declarator->text.to_chars();

  if (op_str == "\"and\"")
    return T(int(a) & int(b)? 1 : 0);
  else if (op_str == "\"or\"")
    return T(int(a) | int(b)? 1 : 0);
  else if (op_str == "\"xor\"")
    return T(int(a) ^ int(b) ? 1 : 0);
  else if (op_str == "\"xnor\"")
    return T(1 ^ (int(a) ^ int(b)) ? 1 : 0);
  else if (op_str == "\"nand\"")
    return T(1 ^ (int(a) & int(b)) ? 1 : 0);
  else if (op_str == "\"nor\"")
    return T(1 ^ (int(a) | int(b)) ? 1 : 0);
  else if (op_str == "\"=\"")
    return T((a == b)? 1 : 0);
  else if (op_str == "\">\"")
    return T((a > b)? 1 : 0);
  else if (op_str == "\">=\"")
    return T((a > b)? 1 : 0);
  else if (op_str == "\"<=\"")
    return T((a <= b)? 1 : 0);
  else if (op_str == "\"<\"")
    return T((a < b)? 1 : 0);
  else if (op_str == "\"/=\"")
    return T((a != b)? 1 : 0);
  else if (op_str == "\"=\"")
    return T((a == b)? 1 : 0);
  else if (op_str == "\"+\"")
    return T(calculate_plus(a, b));
  else if (op_str == "\"-\"")
    return T(calculate_minus(a, b));
  else if (op_str == "\"*\"")
    return T(calculate_mult(a, b));
  else if (op_str == "\"/\"")
    return T(calculate_div(a, b));
  else if (op_str == "\"mod\"")
    assert(false);
  else if (op_str == "\"rem\"")
    assert(false);
  else if (op_str == "\"**\"") {
    return T(calculate_pow(a, b));
  } else
    assert(false);
}


int
m_constant_fold(pIIR_FunctionCall fc, RegionStack &rstack)
{
  if (done(fc) & CONST_FOLD) return 0; else  done(fc) |= CONST_FOLD;

  int error_count = 0;
  int n_args = 0;
  int try_function_folding = true;

  // Try to fold function parameters
  for (pIIR_AssociationList al = fc->parameter_association_list; al; al = al->rest) {
    error_count += constant_fold(al->first->actual, rstack);
    if (!valid_folded_value(al->first->actual))
      try_function_folding = false;
    n_args++;
  }

  // Return if not all prameters are locally static!
  if (!try_function_folding)
    return error_count;

  // Now, try to fold function call
  switch (get_operator_type(fc->function)) { // Analyze function call type

  case STD_OP: // The function call is a standard operator call

    if (n_args == 1) {
      //**********************************************************************
      // Function has one parameter
      //**********************************************************************
      pIIR_Type base_type = get_base_type(fc->parameter_association_list->first->actual->subtype);
      if (base_type->is(IR_INTEGER_TYPE) ||
	  base_type->is(IR_ENUMERATION_TYPE) ||
	  base_type->is(IR_PHYSICAL_TYPE)) {
	lint arg = folded_value(fc->parameter_association_list->first->actual).long_value();
	folded_value(fc).long_value() = perform_operation<lint, lint>(fc, arg);

      } else if (base_type->is(IR_FLOATING_TYPE)) {
	double arg = folded_value(fc->parameter_association_list->first->actual).double_value();
	folded_value(fc).double_value() = perform_operation<double, double>(fc, arg);

      } else
	assert(false);

    } else if (n_args == 2) {
      //**********************************************************************
      // Function has two parameters
      //**********************************************************************
      pIIR_Type base_type1 = get_base_type(fc->parameter_association_list->first->actual->subtype);
      pIIR_Type base_type2 = get_base_type(fc->parameter_association_list->rest->first->actual->subtype);
      pIIR_Type result_type = get_base_type(fc->subtype);
      
      if (is_array_type(result_type)) {
	//**********************************************************************
	// Two arrays (or an array and an array element, or two array elements) are 
	// concatenated
	string op_str = fc->function->declarator->text.to_chars();
	// Only the concatenation operator is predefined for arrays
	assert(op_str == "\"&\"");
	
	// First, determine length of arguments
	int length1 = is_array_type(base_type1)? 
	  folded_value(fc->parameter_association_list->first->actual).array_literal_value().size() : 1;
	int length2 = is_array_type(base_type2)? 
	  folded_value(fc->parameter_association_list->rest->first->actual).array_literal_value().size() : 1;

	// Next, concatenate both arguments
	vector<int> result(length1 + length2);
	if (is_array_type(base_type1)) 
	  // The argument is an array
	  copy(folded_value(fc->parameter_association_list->first->actual).array_literal_value().begin(),
	       folded_value(fc->parameter_association_list->first->actual).array_literal_value().end(),
	       result.begin());
	else
	  // The argument is an array element
	  result[0] = folded_value(fc->parameter_association_list->first->actual).long_value();

	if (is_array_type(base_type2))
	  // The argument is an array
	  copy(folded_value(fc->parameter_association_list->rest->first->actual).array_literal_value().begin(),
	       folded_value(fc->parameter_association_list->rest->first->actual).array_literal_value().end(),
	       &result[length1]);
	else
	  // The argument is an array element
	  result[length1] = folded_value(fc->parameter_association_list->rest->first->actual).long_value();

	// Check whether length of target array matches length of
	// concatenation result.
	if (is_constrained_array_type(fc->subtype)) {
	  // First, try to determine array bounds
	  vector<RangeDescriptor> range_desc = get_discrete_range(fc->subtype, rstack, IR_NOT_STATIC);
	  range_desc[0].constant_fold_rangedes(rstack);
	  StaticRangeDescriptor<lint, IR_Direction> range = 
	    range_desc[0].rangedes_to_lint(rstack);
	  // If array bounds are locally static then determine length
	  // of array
	  if (and_reduce(range.valid)) {
	    lint length = range_to_length(range.left, range.dir, range.right);
	    if (length != (length1 + length2)) {
	      codegen_error.error("%:error: length (=%i) of concatenation result does not match length (=%i) of target array.", 
				  fc, length1 + length2, length);
	      error_count++;
	    }
	  }
	}
	
	folded_value(fc).array_literal_value() = result;

      } else if (base_type1->is(IR_PHYSICAL_TYPE) && base_type2->is(IR_PHYSICAL_TYPE) ||
		 base_type1->is(IR_PHYSICAL_TYPE) && base_type2->is(IR_INTEGER_TYPE) ||
		 base_type1->is(IR_INTEGER_TYPE) && base_type2->is(IR_PHYSICAL_TYPE)) {
	//**********************************************************************
	lint arg1 = folded_value(fc->parameter_association_list->first->actual).long_value();
	lint arg2 = folded_value(fc->parameter_association_list->rest->first->actual).long_value();
	lint result = perform_operation<lint, lint, lint>(fc, arg1, arg2);
	// Check whether result is ok
	if (result > PHYSICAL_MAX || result < PHYSICAL_MIN) {
	  result = result > 0? PHYSICAL_MAX : PHYSICAL_MIN;
	  codegen_error.error("%:error: overflow/underflow in %n.", fc, fc);
	  error_count++;
	}
	// Store result
	folded_value(fc).long_value() = result;

      } else if (base_type1->is(IR_PHYSICAL_TYPE) && base_type2->is(IR_FLOATING_TYPE)) {
	//**********************************************************************
	lint arg1 = folded_value(fc->parameter_association_list->first->actual).long_value();
	double arg2 = folded_value(fc->parameter_association_list->rest->first->actual).double_value();
	lint result = perform_operation<lint, double, double>(fc, double(arg1), arg2);
	// Check whether result is ok
	if (result > PHYSICAL_MAX || result < PHYSICAL_MIN) {
	  result = result > 0? PHYSICAL_MAX : PHYSICAL_MIN;
	  codegen_error.error("%:error: overflow/underflow in %n.", fc, fc);
	  error_count++;
	}
	// Store result
	folded_value(fc).long_value() = result;
	
      } else if (base_type1->is(IR_FLOATING_TYPE) && base_type2->is(IR_PHYSICAL_TYPE)) {
	//**********************************************************************
	double arg1 = folded_value(fc->parameter_association_list->first->actual).double_value();
	lint arg2 = folded_value(fc->parameter_association_list->rest->first->actual).long_value();
	lint result = perform_operation<lint, double, double>(fc, arg1, double(arg2));
	// Check whether result is ok
	if (result > PHYSICAL_MAX || result < PHYSICAL_MIN) {
	  result = result > 0? PHYSICAL_MAX : PHYSICAL_MIN;
	  codegen_error.error("%:error: overflow/underflow in %n.", fc, fc);
	  error_count++;
	}
	// Store result
	folded_value(fc).long_value() = result;
	
      } else if ((base_type1->is(IR_ENUMERATION_TYPE) || base_type1->is(IR_INTEGER_TYPE)) &&
		 (base_type2->is(IR_ENUMERATION_TYPE) || base_type2->is(IR_INTEGER_TYPE))) {
	//**********************************************************************
	lint arg1 = folded_value(fc->parameter_association_list->first->actual).long_value();
	lint arg2 = folded_value(fc->parameter_association_list->rest->first->actual).long_value();
	lint result = perform_operation<lint, lint, lint>(fc, arg1, arg2);
	// Check whether result is ok
	if (result > INTEGER_MAX || result < INTEGER_MIN) {
	  result = result > 0? INTEGER_MAX : INTEGER_MIN;
	  codegen_error.error("%:error: overflow/underflow in %n.", fc, fc);
	  error_count++;
	}
	// Store result
	folded_value(fc).long_value() = result;
	
      } else if (base_type1->is(IR_FLOATING_TYPE) && base_type2->is(IR_FLOATING_TYPE) && 
		 result_type->is(IR_ENUMERATION_TYPE)) {
	//**********************************************************************
	// Compare operation
	double arg1 = folded_value(fc->parameter_association_list->first->actual).double_value();
	double arg2 = folded_value(fc->parameter_association_list->rest->first->actual).double_value();
	lint result = perform_operation<lint, double, double>(fc, arg1, arg2);
	// Check whether result is ok
	if (result > INTEGER_MAX || result < INTEGER_MIN) {
	  result = result > 0? INTEGER_MAX : INTEGER_MIN;
	  codegen_error.error("%:error: overflow/underflow in %n.", fc, fc);
	  error_count++;
	}
	// Store result
	folded_value(fc).long_value() = result;
	
      } else if (base_type1->is(IR_FLOATING_TYPE) && base_type2->is(IR_INTEGER_TYPE)) {
	//**********************************************************************
	// Power
	double arg1 = folded_value(fc->parameter_association_list->first->actual).double_value();
	lint arg2 = folded_value(fc->parameter_association_list->rest->first->actual).long_value();
	double result = perform_operation<double, double, lint>(fc, arg1, arg2);
	// Check whether result is ok
	if (result == HUGE_VAL || result == -HUGE_VAL) {
	    codegen_error.error("%:error: overflow/underflow in %n.", fc, fc);
	    error_count++;
	  }
	// Store result
	folded_value(fc).double_value() = result;

      } else if (base_type1->is(IR_FLOATING_TYPE) && base_type2->is(IR_FLOATING_TYPE) &&
		 result_type->is(IR_FLOATING_TYPE)) {
	//**********************************************************************
	// Misc operations
	double arg1 = folded_value(fc->parameter_association_list->first->actual).double_value();
	double arg2 = folded_value(fc->parameter_association_list->rest->first->actual).double_value();
	double result = perform_operation<double, double, double>(fc, arg1, arg2);
	// Check whether result is ok
	if (result == HUGE_VAL || result == -HUGE_VAL) {
	  codegen_error.error("%:error: overflow/underflow in %n.", fc, fc);
	  error_count++;
	}
	// Store result
	folded_value(fc).double_value() = result;
	
      } else
	assert(false);

    }

    if (error_count == 0)
      valid_folded_value(fc) = true; // The node has been optimized
      
    break;

  case BASIC_OP: // The function is a operator defined in an IEEE
    // library. Currently no constant folding is performed for this class
    // of operators
  case USER_OP: // The function is an user defined operator call
  case NO_OP: // A ordinary function call (no operator)
    break;
  }

  return error_count;
}


int
m_constant_fold(pIIR_PhysicalLiteral le, RegionStack &rstack)
{
  if (done(le) & CONST_FOLD) return 0; else  done(le) |= CONST_FOLD;

  // First, try to fold the unit. I.e., evaluate the number of basic
  // unit the current unit consists of
  int error_count = constant_fold(le->unit, rstack);
  
  // Next, fold the value expression
  if (le->value != NULL)
    // If value pointer is not NULL then fold value
    error_count += constant_fold(le->value, rstack);

  if (!valid_folded_value(le->unit) || 
      !(le->value == NULL || valid_folded_value(le->value)))
    return error_count;

  // Calculate the resulting physical value. The physical value is
  // actually converted to a long long int number which represents the
  // number of basic units the physical value consists of.
  valid_folded_value(le) = true;
  if (le->value == NULL) 
    // If the value expression is NULL then only a time unit was
    // given. Hence, implicitly set value to 1.
    folded_value(le).long_value() = folded_value(le->unit).long_value();
  else if (le->value->is(IR_INTEGER_LITERAL))
    folded_value(le).long_value() = 
      calculate_mult(folded_value(le->value).long_value(), folded_value(le->unit).long_value());
  else if (le->value->is(IR_FLOATING_POINT_LITERAL))
    {
      // XXX - Allow inaccuracies here.  `calculate_mult (double,
      // long)' does extensive checking to ensure that only
      // multiplications are performed where the result can be
      // represented exactly.  This does not make much sense for
      // floating point and for example fails for 1.0e-3 * 1000000.

      double res = (folded_value(le->value).double_value()
		    * folded_value(le->unit).long_value());
      if (res > PHYSICAL_MAX)
	folded_value(le).long_value() = PHYSICAL_MAX;
      else if (res < PHYSICAL_MIN)
	folded_value(le).long_value() = PHYSICAL_MIN;
      else
	folded_value(le).long_value() = lint(res);
    }
  else
    assert(false);
  
  // Check whether no overflow occurred during constant folding
  if (folded_value(le).long_value() > PHYSICAL_MAX ||
      folded_value(le).long_value() < PHYSICAL_MIN) {
    codegen_error.error("%:error: overflow occured during constant folding.", le);
    error_count++;
  }    

  return error_count;
}


int
m_constant_fold(pIIR_PhysicalUnit pu, RegionStack &rstack)
{
  if (done(pu) & CONST_FOLD) return 0; else  done(pu) |= CONST_FOLD;

  int error_count = 0;

  // Fold multiplier value
  if (pu->multiplier != NULL)
    error_count = constant_fold(pu->multiplier, rstack);
  // Fold unit name, i.e. determine number of basic units this unit
  // includes
  if (pu->unit_name != NULL)
    error_count += constant_fold(pu->unit_name, rstack);

  // Flag an error if no valid data could be folded
  if (pu->multiplier != NULL &&
      !valid_folded_value(pu->multiplier)) {
    codegen_error.error("%:error: cannot evaluate expression.", pu, pu->multiplier);
    error_count++;
  }
  // Flag an error if no valid data could be folded
  if (pu->unit_name != NULL &&
      !valid_folded_value(pu->unit_name)) {
    codegen_error.error("%:error: cannot evaluate expression.", pu, pu->unit_name);
    error_count++;
  }

  // Determine multiplier value an unit value (unit value = value
  // related to the basic unit)
  lint mult_value = pu->multiplier == NULL? lint(1) : folded_value(pu->multiplier).long_value();
  lint unit_value = pu->unit_name == NULL? lint(1) : folded_value(pu->unit_name).long_value();

  // Multiply the multiplier value with the number of basic units and
  // store result into node
  folded_value(pu).long_value() = calculate_mult(mult_value, unit_value);
  valid_folded_value(pu) = true;

  // Check whether no overflow occured during constant folding
  if (folded_value(pu).long_value() > PHYSICAL_MAX ||
      folded_value(pu).long_value() < PHYSICAL_MIN) {
    codegen_error.error("%:error: overflow occured during constant folding.", pu);
    error_count++;
  }    
  
  return error_count;
}



int
m_constant_fold(pIIR_SimpleReference sr, RegionStack &rstack)
{
  if (done(sr) & CONST_FOLD) return 0; else  done(sr) |= CONST_FOLD;
  // Return if the expression is not locally static or does not
  // reference a constant
  if (!sr->object->is(IR_CONSTANT_DECLARATION))
    return 0;

  pIIR_ConstantDeclaration cdecl = pIIR_ConstantDeclaration(sr->object);
  int error_count = 0;
  // Return if constant does not have an initial value
  if (cdecl->initial_value != NULL)
    error_count = constant_fold(cdecl->initial_value, rstack);
  else 
    return 0;
  // Return if no folded constant value could be determined.
  if (error_count != 0 ||
      !valid_folded_value(cdecl->initial_value))
    return error_count;

  pIIR_Type base_type = get_base_type(sr->subtype); // Get base type
  
  if (base_type->is(IR_INTEGER_TYPE) ||
      base_type->is(IR_ENUMERATION_TYPE) ||
      base_type->is(IR_PHYSICAL_TYPE)) {
    folded_value(sr).long_value() =
      folded_value(cdecl->initial_value).long_value();
    valid_folded_value(sr) = true;
  } if (base_type->is(IR_FLOATING_TYPE)) {
    folded_value(sr).double_value() =
      folded_value(cdecl->initial_value).double_value();
    valid_folded_value(sr) = true;
  } else if (folded_value(cdecl->initial_value).is_array_literal_value ()) {
    folded_value(sr).array_literal_value() =
      folded_value(cdecl->initial_value).array_literal_value ();
    valid_folded_value(sr) = true;
  }

  return 0;
}



int
m_constant_fold(pIIR_ArrayReference ar, RegionStack &rstack)
{
  if (done(ar) & CONST_FOLD) return 0; else  done(ar) |= CONST_FOLD;

  int error_count = constant_fold(ar->array, rstack);
  for (pIIR_ExpressionList el = ar->indices; el; el = el->rest)
    error_count += constant_fold(el, rstack);

  return error_count;
}



int
m_constant_fold(pIIR_ArrayRange ar, RegionStack &rstack)
{
  if (done(ar) & CONST_FOLD) return 0; else  done(ar) |= CONST_FOLD;

  int error_count = 0;

  if (ar->array != NULL)
    error_count += constant_fold(ar->array, rstack);

  if (ar->index != NULL)
    error_count += constant_fold(ar->index, rstack);

  return error_count;
}


int
m_constant_fold(pIIR_ExplicitRange er, RegionStack &rstack)
{
  if (done(er) & CONST_FOLD) return 0; else  done(er) |= CONST_FOLD;

  int error_count = constant_fold(er->left, rstack);
  error_count += constant_fold(er->right, rstack);

  return error_count;
}


int
m_constant_fold(pIIR_OthersIndexedAssociation ia, RegionStack &rstack)
{
  int error_count = constant_fold(ia->value, rstack);
  return error_count;
}


int
m_constant_fold(pIIR_RangeIndexedAssociation ia, RegionStack &rstack)
{
  if (done(ia) & CONST_FOLD) return 0; else  done(ia) |= CONST_FOLD;

  int error_count = constant_fold(ia->value, rstack);
  error_count += constant_fold(ia->index_range, rstack);

  return error_count;
}


int
m_constant_fold(pIIR_SingleIndexedAssociation ia, RegionStack &rstack)
{
  if (done(ia) & CONST_FOLD) return 0; else  done(ia) |= CONST_FOLD;

  int error_count = 0;
  if (ia->index != NULL)
    error_count += constant_fold(ia->index, rstack);

  error_count += constant_fold(ia->value, rstack);

  return error_count;
}


int
m_constant_fold (pIIR_SliceIndexedAssociation sia, RegionStack &rstack)
{
  if (done(sia) & CONST_FOLD) return 0; else  done(sia) |= CONST_FOLD;

  int error_count = constant_fold(sia->value, rstack);
  error_count += constant_fold(sia->index_range, rstack);

  return error_count;
}


int
m_constant_fold(pIIR_SliceReference sr, RegionStack &rstack)
{
  if (done(sr) & CONST_FOLD) return 0; else  done(sr) |= CONST_FOLD;

  int error_count = 0;
  error_count += constant_fold(sr->array, rstack);
  error_count += constant_fold(sr->range, rstack);

  return error_count;
}



int
m_constant_fold(pIIR_RecordReference rr, RegionStack &rstack)
{
  if (done(rr) & CONST_FOLD) return 0; else  done(rr) |= CONST_FOLD;

  int error_count = 0;
  error_count += constant_fold(rr->record, rstack);

  return error_count;
}



int
m_constant_fold(pIIR_Expression e, RegionStack &rstack)
{
  return 0;
}


int
m_constant_fold(pIIR_Allocator a, RegionStack &rstack)
{
  if (done(a) & CONST_FOLD) return 0; else  done(a) |= CONST_FOLD;

  int error_count = constant_fold(a->type_mark, rstack);
  if (a->value != NULL)
    error_count += constant_fold(a->value, rstack);

  return error_count;
}


int
m_constant_fold(pIIR_ExpressionList el, RegionStack &rstack)
{
  if (done(el) & CONST_FOLD) return 0; else  done(el) |= CONST_FOLD;

  int error_count = 0;
  for ( ; el; el = el->rest)
    error_count += constant_fold(el->first, rstack);

  return error_count;
}



int
m_constant_fold (pIIR_ArrayAggregate aa, RegionStack &rstack)
{
  if (done(aa) & CONST_FOLD) return 0; else  done(aa) |= CONST_FOLD;

  int error_count = 0;
  // First, determine context of asscociations
  for (pIIR_IndexedAssociationList al = aa->indexed_association_list; al; al = al->rest)
    error_count += constant_fold(al->first, rstack);

  return error_count;
}


int
m_constant_fold (pIIR_RecordAggregate ra, RegionStack &rstack)
{
  if (done(ra) & CONST_FOLD) return 0; else  done(ra) |= CONST_FOLD;

  int error_count = 0;
  // First, determine context of asscociations
  for (pIIR_ElementAssociationList al = ra->element_association_list; al; al = al->rest)
    error_count += constant_fold(al->first->value, rstack);

  return error_count;
}


int
m_constant_fold (pIIR_AttrArrayFunc aa, RegionStack &rstack)
{
  if (done(aa) & CONST_FOLD) return 0; else  done(aa) |= CONST_FOLD;

  int error_count = 0;
  vector<RangeDescriptor> range_des_vec;

  if (aa->array != NULL) {
    if (!is_constrained_array_type(aa->array->subtype)) return error_count;
    // Get range descriptors from array object
    range_des_vec = get_discrete_range(aa->array->subtype, rstack, IR_NOT_STATIC);

  } else if (aa->array_type != NULL) {
    if (!is_constrained_array_type(aa->array_type)) return error_count;
    // Get range descriptors from array type
    range_des_vec = get_discrete_range(aa->array_type, rstack, IR_NOT_STATIC);

  } else
    assert(false);
  
  // Constant fold corresponding range descriptor
  RangeDescriptor &range_des = range_des_vec[aa->index - 1];
  range_des.constant_fold_rangedes(rstack);
  StaticRangeDescriptor<lint, IR_Direction> range = 
    range_des.rangedes_to_lint(rstack);
  
  // Initialize flag
  valid_folded_value(aa) = false;

  // Check attribute
  if (aa->is(IR_ATTR_ARRAY_LENGTH)) {
    if (and_reduce(range.valid)) {
      folded_value(aa).long_value() = 
	(range.left <= range.right && range.dir == IR_DIRECTION_UP) || 
	(range.left >= range.right && range.dir == IR_DIRECTION_DOWN) ?
	lint_abs(range.right - range.left) + 1 : 0;
      valid_folded_value(aa) = true;
    }

  } else if (aa->is(IR_ATTR_ARRAY_ASCENDING)) {
    if (range.valid[1]) {
      folded_value(aa).long_value() = range.dir == IR_DIRECTION_UP? 1 : 0;
      valid_folded_value(aa) = true;
    }

  } else if (aa->is(IR_ATTR_ARRAY_LOW)) {
    if (and_reduce(range.valid)) {
      folded_value(aa).long_value() = min (range.left, range.right);
      valid_folded_value(aa) = true;
    }

  } else if (aa->is(IR_ATTR_ARRAY_HIGH)) {
    if (and_reduce(range.valid)) {
      folded_value(aa).long_value() = max (range.left, range.right);
      valid_folded_value(aa) = true;
    }

  } else if (aa->is(IR_ATTR_ARRAY_RIGHT)) {
    if (range.valid[2]) {
      folded_value(aa).long_value() = range.right;
      valid_folded_value(aa) = true;
    }

  } else if (aa->is(IR_ATTR_ARRAY_LEFT)) {
    if (range.valid[0]) {
      folded_value(aa).long_value() = range.left;
      valid_folded_value(aa) = true;
    }

  } else 
    assert(false);

  return error_count;
}



int
m_constant_fold (pIIR_AttrTypeFunc a, RegionStack &rstack)
{
  int error_count = 0;
  vector<RangeDescriptor> type_range_des;
  StaticRangeDescriptor<lint, IR_Direction> static_lint_range;
  StaticRangeDescriptor<double, IR_Direction> static_double_range;
  
  pIIR_Root left_bound_node, right_bound_node;

  if (a->prefix != NULL) {
    // Determine range of prefix type
    type_range_des = get_discrete_range(a->prefix, rstack, IR_NOT_STATIC); 
    // type_range_des should contain exactly one single
    // RangeDescriptor instance
    assert(type_range_des.size() == 1);
    left_bound_node = type_range_des[0].left;
    right_bound_node = type_range_des[0].right;
    // Try to fold range descriptor
    type_range_des[0].constant_fold_rangedes(rstack);
    if (get_base_type (a->prefix)-> is (IR_FLOATING_TYPE))
      {
	// Determine left, right and direction of range. is_static stores
	// whether the corresponding value is static.
	static_double_range = type_range_des[0].rangedes_to_double (rstack);
	// if neither left or right bound nor the direction are static
	// then do not perform any constant folding because no range
	// checking can be done at compile time.
	if (!or_reduce(static_double_range.valid))
	  return error_count;
      }
    else
      {
	// Determine left, right and direction of range. is_static stores
	// whether the corresponding value is static.
	static_lint_range = type_range_des[0].rangedes_to_lint (rstack);
	// if neither left or right bound nor the direction are static
	// then do not perform any constant folding because no range
	// checking can be done at compile time.
	if (!or_reduce(static_lint_range.valid))
	  return error_count;
      }
  }

  // Fold argument
  if (a->argument != NULL)
    constant_fold(a->argument, rstack);
  // Return if the argument is not locally static
  if (a->argument != NULL && !valid_folded_value(a->argument))
    return error_count;

  // Determine argument value and store whether the argument is an
  // integer/enumeration/physical or floating point type
  lint lint_argument;
  double double_argument;
  bool has_long_argument;
  bool has_no_argument = true;
  if (a->argument != NULL) {
    pIIR_Type type = get_base_type(a->argument->subtype);
    has_no_argument = false;
    if (type->is(IR_INTEGER_TYPE) ||
	type->is(IR_ENUMERATION_TYPE) ||
	type->is(IR_PHYSICAL_TYPE)) {
      lint_argument = folded_value(a->argument).long_value();
      has_long_argument = true;

    } else if (type->is(IR_FLOATING_TYPE)) {
      double_argument = folded_value(a->argument).double_value();
      has_long_argument = false;
      
    } else {
      cerr << "unsupported argtype: " << type->base->kind_name () << "\n";
      assert(false);
    }
  }


  // Bail out if the attribute is used on an illegal type
  if (!has_long_argument && 
      (a->is(IR_ATTR_RIGHTOF) ||
       a->is(IR_ATTR_LEFTOF) ||
       a->is(IR_ATTR_VAL) ||
       a->is(IR_ATTR_POS) ||
       a->is(IR_ATTR_SUCC) ||
       a->is(IR_ATTR_PRED) ||
       a->is(IR_ATTR_LEFTOF) ||
       a->is(IR_ATTR_RIGHTOF)
       )) {
    codegen_error.error("%:error: attribute %n is not defined for floating point types.", a, a);
    return ++error_count;
  }
      
  
  // Finally, handle the various attributes
  // ***** RIGHTOF *******************************************
  // Note that this attribute is not defined for floatingpoint types
  if (a->is(IR_ATTR_RIGHTOF)) {
    folded_value(a).long_value() = 
      lint_argument + (static_lint_range.dir == IR_DIRECTION_UP? +1 : -1);
    // Check result
    if (static_lint_range.dir == IR_DIRECTION_DOWN)
      if (!static_lint_range.valid[0])
	// Return if we cannot range check the result
	return error_count;
      else if (folded_value(a).long_value() < static_lint_range.left) {
	codegen_error.error("%:error: result of attribute %n is out of bounds. Left bound of type %n is %n.", 
			    a, a, a->prefix, left_bound_node);
	return ++error_count;
      }

    if (static_lint_range.dir == IR_DIRECTION_UP)
      if (!static_lint_range.valid[2])
	// Return if we cannot range check the result
	return error_count;
      else if (folded_value(a).long_value() > static_lint_range.right) {
	codegen_error.error("%:error: result of attribute %n is out of bounds. Right bound of type %n is %n.", 
			    a, a, a->prefix, right_bound_node);
	return ++error_count;
      }

    valid_folded_value(a) = true;

  // ***** LEFTOF *******************************************
  // Note that this attribute is not defined for floatingpoint types
  } else if (a->is(IR_ATTR_LEFTOF)) {
    folded_value(a).long_value() = lint_argument - (static_lint_range.dir == IR_DIRECTION_UP? +1 : -1);
    // Check result
    if (static_lint_range.dir == IR_DIRECTION_UP)
      if (!static_lint_range.valid[2])
	// Return if we cannot range check the result
	return error_count;
      else if (folded_value(a).long_value() < static_lint_range.left) {
	codegen_error.error("%:error: result of attribute %n is out of bounds. Left bound of type %n is %n.", 
			    a, a, a->prefix, left_bound_node);
	return ++error_count;
      }

    if (static_lint_range.dir == IR_DIRECTION_DOWN)
      if (!static_lint_range.valid[0])
	// Return if we cannot range check the result
	return error_count;
      else if (folded_value(a).long_value() > static_lint_range.right) {
	  codegen_error.error("%:error: result of attribute %n is out of bounds. Right bound of type %n is %n.", 
			      a, a, a->prefix, right_bound_node);
	  return ++error_count;
      }

    valid_folded_value(a) = true;

  // ***** PRED *******************************************
  // Note that this attribute is not defined for floatingpoint types
  } else if (a->is(IR_ATTR_PRED)) {
    folded_value(a).long_value() = lint_argument - 1;
    // Check result
    if (!static_lint_range.valid[0])
      // Return if we cannot range check the result
      return error_count;
    
    if (folded_value(a).long_value() < static_lint_range.left) {
      codegen_error.error("%:error: result of attribute %n is out of bound. Left bound of type %n is %n.", 
			  a, a, a->prefix, right_bound_node);
      return ++error_count;
    }

    valid_folded_value(a) = true;

  // ***** SUCC *******************************************
  // Note that this attribute is not defined for floatingpoint types
  } else if (a->is(IR_ATTR_SUCC)) {
    folded_value(a).long_value() = lint_argument + 1;
    // Check result
    if (!static_lint_range.valid[2])
      // Return if we cannot range check the result
      return error_count;

    if (folded_value(a).long_value() > static_lint_range.right) {
      codegen_error.error("%:error: result of attribute %n is out of bound. Right bound of type %n is %n.", 
			  a, a, a->prefix, right_bound_node);
      return ++error_count;
    }

    valid_folded_value(a) = true;

  // ***** VAL *******************************************
  // Note that this attribute is not defined for floatingpoint types
  } else if (a->is(IR_ATTR_VAL)) {
    folded_value(a).long_value() = lint_argument;
    // Check result
    if (!static_lint_range.valid[2] ||
	!static_lint_range.valid[0])
      // Return if we cannot range check the result
      return error_count;
    else if (folded_value(a).long_value() < static_lint_range.left) {
      codegen_error.error("%:error: result of attribute %n is out of bounds. Left bound of type %n is %n.", 
			  a, a, a->prefix, left_bound_node);
      return ++error_count;
    } else if (folded_value(a).long_value() > static_lint_range.right) {
      codegen_error.error("%:error: result of attribute %n is out of bounds. Right bound of type %n is %n.", 
			  a, a, a->prefix, right_bound_node);
      return ++error_count;
    }
    valid_folded_value(a) = true;

  // ***** POS *******************************************
  // Note that this attribute is not defined for floatingpoint types
  } else if (a->is(IR_ATTR_POS)) {
    folded_value(a).long_value() = lint_argument;
    valid_folded_value(a) = true;

  // ***** VALUE *******************************************
  } else if (a->is(IR_ATTR_VALUE)) {
    // The VALUE attribute is not folded
    return error_count;

  // ***** IMAGE *******************************************
  } else if (a->is(IR_ATTR_IMAGE)) {
    // The IMAGE attribute is not folded
    return error_count;

  } else 
    assert(false);

  return error_count;
}



int
m_constant_fold (pIIR_AttrTypeValue a, RegionStack &rstack)
{
  int error_count = 0;
  vector<RangeDescriptor> type_range_des;
  StaticRangeDescriptor<lint, IR_Direction> static_lint_range;
  StaticRangeDescriptor<double, IR_Direction> static_double_range;
  bool is_floatingpoint_type = false;

  if (a->prefix != NULL) {
    // Determine range of prefix type
    type_range_des = get_discrete_range(a->prefix, rstack, IR_NOT_STATIC); 
    // type_range_des should contain exactly one single
    // RangeDescriptor instance
    assert(type_range_des.size() == 1);
    // Try to fold range descriptor
    type_range_des[0].constant_fold_rangedes(rstack);
    // Determine left, right and direction of range. is_static stores
    // whether the corresponding value is static.
    if (get_base_type (a->prefix)->is (IR_FLOATING_TYPE))
      {
	static_double_range = type_range_des[0].rangedes_to_double (rstack);
	// if neither left or right bound nor the direction are static
	// then do not perform any constant folding because no range
	// checking can be done at compile time.
	is_floatingpoint_type = true;
	if (!or_reduce(static_double_range.valid))
	  return error_count;
      }
    else 
      {
	static_lint_range = type_range_des[0].rangedes_to_lint (rstack);
	// if neither left or right bound nor the direction are static
	// then do not perform any constant folding because no range
	// checking can be done at compile time.
	is_floatingpoint_type = false;
	if (!or_reduce(static_lint_range.valid))
	  return error_count;
      }
  }

  // Fold argument
  if (a->argument != NULL)
    constant_fold(a->argument, rstack);
  // Return if the argument is not locally static
  if (a->argument != NULL && !valid_folded_value(a->argument))
    return error_count;

  // Determine argument value and store whether the argument is an
  // integer/enumeration/physical or floating point type
  lint lint_argument;
  double double_argument;
  bool has_long_argument;
  bool has_no_argument = true;
  if (a->argument != NULL) {
    pIIR_Type type = get_base_type(a->argument->subtype);
    has_no_argument = false;
    if (type->is(IR_INTEGER_TYPE) ||
	type->is(IR_ENUMERATION_TYPE) ||
	type->is(IR_PHYSICAL_TYPE)) {
      lint_argument = folded_value(a->argument).long_value();
      has_long_argument = true;
      
    } else if (type->is(IR_FLOATING_TYPE)) {
      double_argument = folded_value(a->argument).double_value();
      has_long_argument = false;
      
    } else
      assert(false);
  }

  // Create shortcut to access valid array.
  vector<bool> &valid = is_floatingpoint_type?
    static_double_range.valid : static_lint_range.valid;

  // Finally, handle the various attributes
  // ***** LENGTH *******************************************
  if (a->is(IR_ATTR_LENGTH)) {
    // Actually, this attribute is defined for arrays only!
    assert(false);

  // ***** ASCENDING *******************************************
  } else if (a->is(IR_ATTR_ASCENDING)) {
    if (!valid[1])
      // Return if direction is not static
      return error_count;

    if (is_floatingpoint_type)
      folded_value(a).double_value() = static_double_range.dir == IR_DIRECTION_UP? 1 : 0;
    else
      folded_value(a).long_value() = static_lint_range.dir == IR_DIRECTION_UP? 1 : 0;
    valid_folded_value(a) = true;

  // ***** HIGH *******************************************
  } else if (a->is(IR_ATTR_HIGH)) {
    if (!valid[2] ||
	!valid[0])
      // Return if bound is not static
      return error_count;

    if (is_floatingpoint_type)
      folded_value(a).double_value() = max(static_double_range.right, static_double_range.left);
    else
      folded_value(a).long_value() = max(static_lint_range.right, static_lint_range.left);
    valid_folded_value(a) = true;

  // ***** LOW *******************************************
  } else if (a->is(IR_ATTR_LOW)) { 
    if (!valid[2] ||
	!valid[0])
      // Return if bound is not static
      return error_count;

    if (is_floatingpoint_type)
      folded_value(a).double_value() = min(static_double_range.right, static_double_range.left);
    else
      folded_value(a).long_value() = min(static_lint_range.right, static_lint_range.left);
    valid_folded_value(a) = true;

  // ***** RIGHT *******************************************
  } else if (a->is(IR_ATTR_RIGHT)) {
    if (!valid[2])
      // Return if bound is not static
      return error_count;

    if (is_floatingpoint_type)
      folded_value(a).double_value() = static_double_range.right;
    else
      folded_value(a).long_value() = static_lint_range.right;
    valid_folded_value(a) = true;

  // ***** LEFT *******************************************
  } else if (a->is(IR_ATTR_LEFT)) {
    if (!valid[0])
      // Return if bound is not static
      return error_count;

    if (is_floatingpoint_type)
      folded_value(a).double_value() = static_double_range.left;
    else
      folded_value(a).long_value() = static_lint_range.left;
    valid_folded_value(a) = true;

  } else 
    assert(false);

  return error_count;
}


int
m_constant_fold (pIIR_TypeConversion tc, RegionStack &rstack)
{
  int error_count = 0;
  
  error_count += constant_fold(tc->expression, rstack);
  // Return if an error occured during constant folding of the operand
  // or no valid folded value could be determined for the operand or
  // the operand is not scalar.
  if (error_count != 0 ||
      !valid_folded_value(tc->expression) ||
      !is_scalar_type(tc->expression->subtype)) 
    return error_count;
  // Determine converted value
  pIIR_Type target_base_type = get_base_type(tc->type_mark);
  pIIR_Type source_base_type = get_base_type(tc->expression->subtype);

  if (target_base_type->is(IR_FLOATING_TYPE) &&
      source_base_type->is(IR_INTEGER_TYPE)) {
    folded_value(tc).double_value() = (double)folded_value(tc->expression).long_value();
    // Check whether 
    error_count += check_scalar_against_bounds(tc, folded_value(tc).double_value(), tc->type_mark, 
					       rstack, runtime_checks(tc), true, " illegal type conversion:" );

  } else if (source_base_type->is(IR_FLOATING_TYPE) &&
	     target_base_type->is(IR_INTEGER_TYPE)) {
    folded_value(tc).long_value() = (lint)rint(folded_value(tc->expression).double_value());
    error_count += check_scalar_against_bounds(tc, folded_value(tc).long_value(), tc->type_mark,
					       rstack, runtime_checks(tc), true, " illegal type conversion:" );

  } else if (target_base_type->is(IR_FLOATING_TYPE) &&
	     source_base_type->is(IR_FLOATING_TYPE)) {
    folded_value(tc).double_value() = folded_value(tc->expression).double_value();
    error_count += check_scalar_against_bounds(tc, folded_value(tc).double_value(), tc->type_mark, 
					       rstack, runtime_checks(tc), true, " illegal type conversion:" );

  } else {
    folded_value(tc).long_value() = folded_value(tc->expression).long_value();
    error_count += check_scalar_against_bounds(tc, folded_value(tc).long_value(), tc->type_mark, 
					       rstack, runtime_checks(tc), true, " illegal type conversion:" );

  }
  
  valid_folded_value(tc) = (error_count == 0);

  return error_count;
}