summaryrefslogtreecommitdiff
path: root/compiler/defutil.pas
blob: 6ac905156bc204cf0b0bb6649454ee9c4f8e1836 (plain)
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
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
{
    Copyright (c) 1998-2006 by Florian Klaempfl

    This unit provides some help routines for type handling

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 ****************************************************************************
}
unit defutil;

{$i fpcdefs.inc}

interface

    uses
       globtype,globals,constexp,
       symconst,symtype,symdef,
       cgbase,cpubase;

    type
       tmmxtype = (mmxno,mmxu8bit,mmxs8bit,mmxu16bit,mmxs16bit,
                   mmxu32bit,mmxs32bit,mmxfixed16,mmxsingle,mmxs64bit,mmxu64bit);


{*****************************************************************************
                          Basic type functions
 *****************************************************************************}

    {# Returns true, if definition defines an ordinal type }
    function is_ordinal(def : tdef) : boolean;

    {# Returns true, if definition defines a string type }
    function is_string(def : tdef): boolean;

    {# Returns True, if definition defines a type that behaves like a string,
       namely that can be joined and compared with another string-like type }
    function is_stringlike(def : tdef) : boolean;

    {# Returns True, if definition defines an enumeration type }
    function is_enum(def : tdef) : boolean;

    {# Returns True, if definition defines a set type }
    function is_set(def : tdef) : boolean;

    {# Returns the minimal integer value of the type }
    function get_min_value(def : tdef) : TConstExprInt;

    {# Returns the maximal integer value of the type }
    function get_max_value(def : tdef) : TConstExprInt;

    {# Returns basetype of the specified integer range }
    function range_to_basetype(const l,h:TConstExprInt):tordtype;

    procedure range_to_type(const l,h:TConstExprInt;var def:tdef);

    procedure int_to_type(const v:TConstExprInt;var def:tdef);

    {# Returns true, if definition defines an integer type }
    function is_integer(def : tdef) : boolean;

    {# Returns true if definition is a boolean }
    function is_boolean(def : tdef) : boolean;

    {# Returns true if definition is a Pascal-style boolean (1 = true, zero = false) }
    function is_pasbool(def : tdef) : boolean;

    {# Returns true if definition is a C-style boolean (non-zero value = true, zero = false) }
    function is_cbool(def : tdef) : boolean;

    {# Returns true if definition is a char

       This excludes the unicode char.
    }
    function is_char(def : tdef) : boolean;

    {# Returns true if definition is a widechar }
    function is_widechar(def : tdef) : boolean;

    {# Returns true if definition is either an AnsiChar or a WideChar }
    function is_anychar(def : tdef) : boolean;

    {# Returns true if definition is a void}
    function is_void(def : tdef) : boolean;

    {# Returns true if definition is a smallset}
    function is_smallset(p : tdef) : boolean;

    {# Returns true, if def defines a signed data type
       (only for ordinal types)
    }
    function is_signed(def : tdef) : boolean;

    {# Returns an unsigned integer type of the same size as def; def must be
       an ordinal or enum }
    function get_unsigned_inttype(def: tdef): torddef;

    {# Returns whether def_from's range is comprised in def_to's if both are
      orddefs, false otherwise                                              }
    function is_in_limit(def_from,def_to : tdef) : boolean;

    {# Returns whether def is reference counted }
    function is_managed_type(def: tdef) : boolean;{$ifdef USEINLINE}inline;{$endif}

    { # Returns whether def is needs to load RTTI for reference counting }
    function is_rtti_managed_type(def: tdef) : boolean;

{    function is_in_limit_value(val_from:TConstExprInt;def_from,def_to : tdef) : boolean;}

{*****************************************************************************
                              Array helper functions
 *****************************************************************************}

    {# Returns true, if p points to a zero based (non special like open or
      dynamic array def).

      This is mainly used to see if the array
      is convertable to a pointer
    }
    function is_zero_based_array(p : tdef) : boolean;

    {# Returns true if p points to an open array definition }
    function is_open_array(p : tdef) : boolean;

    {# Returns true if p points to a dynamic array definition }
    function is_dynamic_array(p : tdef) : boolean;

    {# Returns true, if p points to an array of const definition }
    function is_array_constructor(p : tdef) : boolean;

    {# Returns true, if p points to a variant array }
    function is_variant_array(p : tdef) : boolean;

    {# Returns true, if p points to an array of const }
    function is_array_of_const(p : tdef) : boolean;

    {# Returns true, if p points any kind of special array

       That is if the array is an open array, a variant
       array, an array constants constructor, or an
       array of const.

       Bitpacked arrays aren't special in this regard though.
    }
    function is_special_array(p : tdef) : boolean;

    {# Returns true if p is a bitpacked array }
    function is_packed_array(p: tdef) : boolean;

    {# Returns true if p is a bitpacked record }
    function is_packed_record_or_object(p: tdef) : boolean;

    {# Returns true if p is a char array def }
    function is_chararray(p : tdef) : boolean;

    {# Returns true if p is a wide char array def }
    function is_widechararray(p : tdef) : boolean;

    {# Returns true if p is a open char array def }
    function is_open_chararray(p : tdef) : boolean;

    {# Returns true if p is a open wide char array def }
    function is_open_widechararray(p : tdef) : boolean;

{*****************************************************************************
                          String helper functions
 *****************************************************************************}

    {# Returns true if p points to an open string type }
    function is_open_string(p : tdef) : boolean;

    {# Returns true if p is an ansi string type }
    function is_ansistring(p : tdef) : boolean;

    {# Returns true if p is an ansi string type with codepage 0 }
    function is_rawbytestring(p : tdef) : boolean;

    {# Returns true if p is a long string type }
    function is_longstring(p : tdef) : boolean;

    {# returns true if p is a wide string type }
    function is_widestring(p : tdef) : boolean;

    {# true if p is an unicode string def }
    function is_unicodestring(p : tdef) : boolean;

    {# true if p is an unicode/wide/ansistring string def }
    function is_dynamicstring(p : tdef) : boolean;

    {# returns true if p is a wide or unicode string type }
    function is_wide_or_unicode_string(p : tdef) : boolean;

    {# Returns true if p is a short string type }
    function is_shortstring(p : tdef) : boolean;

    {# Returns true if p is any pointer def }
    function is_pointer(p : tdef) : boolean;

    {# Returns true if p is a pchar def }
    function is_pchar(p : tdef) : boolean;

    {# Returns true if p is a pwidechar def }
    function is_pwidechar(p : tdef) : boolean;

    {# Returns true if p is a voidpointer def }
    function is_voidpointer(p : tdef) : boolean;

    {# Returns true, if definition is a float }
    function is_fpu(def : tdef) : boolean;

    {# Returns true, if def is a currency type }
    function is_currency(def : tdef) : boolean;

    {# Returns true, if def is a single type }
    function is_single(def : tdef) : boolean;

    {# Returns true, if def is a double type }
    function is_double(def : tdef) : boolean;

    {# Returns true, if def is an extended type }
    function is_extended(def : tdef) : boolean;

    {# Returns true, if definition is a "real" real (i.e. single/double/extended) }
    function is_real(def : tdef) : boolean;

    {# Returns true for single,double,extended and cextended }
    function is_real_or_cextended(def : tdef) : boolean;

    { true, if def is a 8 bit int type }
    function is_8bitint(def : tdef) : boolean;

    { true, if def is a 8 bit ordinal type }
    function is_8bit(def : tdef) : boolean;

    { true, if def is a 16 bit int type }
    function is_16bitint(def : tdef) : boolean;

    { true, if def is a 16 bit ordinal type }
    function is_16bit(def : tdef) : boolean;

    {# Returns true, if def is a 32 bit integer type }
    function is_32bitint(def : tdef) : boolean;

    {# Returns true, if def is a 32 bit ordinal type }
    function is_32bit(def : tdef) : boolean;

    {# Returns true, if def is a 64 bit integer type }
    function is_64bitint(def : tdef) : boolean;

    {# Returns true, if def is a 64 bit type }
    function is_64bit(def : tdef) : boolean;

    { true, if def1 and def2 are both integers of the same bit size and sign }
    function are_equal_ints(def1, def2: tdef): boolean;

    { true, if def is an int type, larger than the processor's native int size }
    function is_oversizedint(def : tdef) : boolean;

    { true, if def is an ordinal type, larger than the processor's native int size }
    function is_oversizedord(def : tdef) : boolean;

    { true, if def is an int type, equal in size to the processor's native int size }
    function is_nativeint(def : tdef) : boolean;

    { true, if def is an ordinal type, equal in size to the processor's native int size }
    function is_nativeord(def : tdef) : boolean;

    { true, if def is an unsigned int type, equal in size to the processor's native int size }
    function is_nativeuint(def : tdef) : boolean;

    { true, if def is a signed int type, equal in size to the processor's native int size }
    function is_nativesint(def : tdef) : boolean;

    {# If @var(l) isn't in the range of todef a range check error (if not explicit) is generated and
      the value is placed within the range
    }
    procedure testrange(todef : tdef;var l : tconstexprint;explicit,forcerangecheck:boolean);

    {# Returns the range of def, where @var(l) is the low-range and @var(h) is
      the high-range.
    }
    procedure getrange(def : tdef;out l, h : TConstExprInt);

    { Returns the range type of an ordinal type in the sense of ISO-10206 }
    function get_iso_range_type(def: tdef): tdef;

    { type being a vector? }
    function is_vector(p : tdef) : boolean;

    { some type helper routines for MMX support }
    function is_mmx_able_array(p : tdef) : boolean;

    {# returns the mmx type }
    function mmx_type(p : tdef) : tmmxtype;

    { returns if the passed type (array) fits into an mm register }
    function fits_in_mm_register(p : tdef) : boolean;

    {# From a definition return the abstract code generator size enum. It is
       to note that the value returned can be @var(OS_NO) }
    function def_cgsize(def: tdef): tcgsize;

    { #Return an orddef (integer) correspondig to a tcgsize }
    function cgsize_orddef(size: tcgsize): torddef;

    {# Same as def_cgsize, except that it will interpret certain arrays as
       vectors and return OS_M* sizes for them }
    function def_cgmmsize(def: tdef): tcgsize;

    {# returns true, if the type passed is can be used with windows automation }
    function is_automatable(p : tdef) : boolean;

    { # returns true if the procdef has no parameters and no specified return type }
    function is_bareprocdef(pd : tprocdef): boolean;

    { returns true if the procdef is a C-style variadic function }
    function is_c_variadic(pd: tabstractprocdef): boolean; {$ifdef USEINLINE}inline;{$endif}

    { # returns the smallest base integer type whose range encompasses that of
        both ld and rd; if keep_sign_if_equal, then if ld and rd have the same
        signdness, the result will also get that signdness }
    function get_common_intdef(ld, rd: torddef; keep_sign_if_equal: boolean): torddef;

    { # returns whether the type is potentially a valid type of/for an "univ" parameter
        (basically: it must have a compile-time size) }
    function is_valid_univ_para_type(def: tdef): boolean;

    { # returns whether the procdef/procvardef represents a nested procedure
        or not }
    function is_nested_pd(def: tabstractprocdef): boolean;{$ifdef USEINLINE}inline;{$endif}

    { # returns whether def is a type parameter of a generic }
    function is_typeparam(def : tdef) : boolean;{$ifdef USEINLINE}inline;{$endif}

    { returns true of def is a methodpointer }
    function is_methodpointer(def : tdef) : boolean;

    { returns true if def is a C "block" }
    function is_block(def: tdef): boolean;

    { returns the TTypeKind value of the def }
    function get_typekind(def: tdef): byte;

implementation

    uses
       verbose,cutils;

    { returns true, if def uses FPU }
    function is_fpu(def : tdef) : boolean;
      begin
         is_fpu:=(def.typ=floatdef);
      end;


    { returns true, if def is a currency type }
    function is_currency(def : tdef) : boolean;
      begin
         case s64currencytype.typ of
           orddef :
             result:=(def.typ=orddef) and
                     (torddef(s64currencytype).ordtype=torddef(def).ordtype);
           floatdef :
             result:=(def.typ=floatdef) and
                     (tfloatdef(s64currencytype).floattype=tfloatdef(def).floattype);
           else
             internalerror(200304222);
         end;
      end;


    { returns true, if def is a single type }
    function is_single(def : tdef) : boolean;
      begin
        result:=(def.typ=floatdef) and
          (tfloatdef(def).floattype=s32real);
      end;


    { returns true, if def is a double type }
    function is_double(def : tdef) : boolean;
      begin
        result:=(def.typ=floatdef) and
          (tfloatdef(def).floattype=s64real);
      end;


    function is_extended(def : tdef) : boolean;
      begin
        result:=(def.typ=floatdef) and
          (tfloatdef(def).floattype in [s80real,sc80real]);
      end;


    { returns true, if definition is a "real" real (i.e. single/double/extended) }
    function is_real(def : tdef) : boolean;
      begin
        result:=(def.typ=floatdef) and
          (tfloatdef(def).floattype in [s32real,s64real,s80real]);
      end;


    function is_real_or_cextended(def: tdef): boolean;
      begin
        result:=(def.typ=floatdef) and
          (tfloatdef(def).floattype in [s32real,s64real,s80real,sc80real]);
      end;


    function range_to_basetype(const l,h:TConstExprInt):tordtype;
      begin
        { prefer signed over unsigned }
        if (l>=int64(-128)) and (h<=127) then
         range_to_basetype:=s8bit
        else if (l>=0) and (h<=255) then
         range_to_basetype:=u8bit
        else if (l>=int64(-32768)) and (h<=32767) then
         range_to_basetype:=s16bit
        else if (l>=0) and (h<=65535) then
         range_to_basetype:=u16bit
        else if (l>=int64(low(longint))) and (h<=high(longint)) then
         range_to_basetype:=s32bit
        else if (l>=low(cardinal)) and (h<=high(cardinal)) then
         range_to_basetype:=u32bit
        else if (l>=low(int64)) and (h<=high(int64)) then
         range_to_basetype:=s64bit
        else
         range_to_basetype:=u64bit;
      end;


    procedure range_to_type(const l,h:TConstExprInt;var def:tdef);
      begin
        { prefer signed over unsigned }
        if (l>=int64(-128)) and (h<=127) then
         def:=s8inttype
        else if (l>=0) and (h<=255) then
         def:=u8inttype
        else if (l>=int64(-32768)) and (h<=32767) then
         def:=s16inttype
        else if (l>=0) and (h<=65535) then
         def:=u16inttype
        else if (l>=int64(low(longint))) and (h<=high(longint)) then
         def:=s32inttype
        else if (l>=low(cardinal)) and (h<=high(cardinal)) then
         def:=u32inttype
        else if (l>=low(int64)) and (h<=high(int64)) then
         def:=s64inttype
        else
         def:=u64inttype;
      end;


    procedure int_to_type(const v:TConstExprInt;var def:tdef);
      begin
        range_to_type(v,v,def);
      end;


    { true if p is an ordinal }
    function is_ordinal(def : tdef) : boolean;
      var
         dt : tordtype;
      begin
         case def.typ of
           orddef :
             begin
               dt:=torddef(def).ordtype;
               is_ordinal:=dt in [uchar,uwidechar,
                                  u8bit,u16bit,u32bit,u64bit,
                                  s8bit,s16bit,s32bit,s64bit,
                                  pasbool1,pasbool8,pasbool16,pasbool32,pasbool64,
                                  bool8bit,bool16bit,bool32bit,bool64bit,customint];
             end;
           enumdef :
             is_ordinal:=true;
           else
             is_ordinal:=false;
         end;
      end;

    { true if p is a string }
    function is_string(def : tdef) : boolean;
      begin
        is_string := (assigned(def) and (def.typ = stringdef));
      end;

    function is_stringlike(def : tdef) : boolean;
      begin
        result := is_string(def) or
                  is_anychar(def) or
                  is_pchar(def) or
                  is_pwidechar(def) or
                  is_chararray(def) or
                  is_widechararray(def) or
                  is_open_chararray(def) or
                  is_open_widechararray(def) or
                  (def=java_jlstring);
      end;

    function is_enum(def : tdef) : boolean;
      begin
        result:=def.typ=enumdef;
      end;

    function is_set(def : tdef) : boolean;
      begin
        result:=def.typ=setdef;
      end;

    { returns the min. value of the type }
    function get_min_value(def : tdef) : TConstExprInt;
      begin
         case def.typ of
           orddef:
             result:=torddef(def).low;
           enumdef:
             result:=int64(tenumdef(def).min);
           else
             result:=0;
         end;
      end;


    { returns the max. value of the type }
    function get_max_value(def : tdef) : TConstExprInt;
      begin
         case def.typ of
           orddef:
             result:=torddef(def).high;
           enumdef:
             result:=tenumdef(def).max;
           else
             result:=0;
         end;
      end;


    { true if p is an integer }
    function is_integer(def : tdef) : boolean;
      begin
        result:=(def.typ=orddef) and
                    (torddef(def).ordtype in [u8bit,u16bit,u32bit,u64bit,
                                          s8bit,s16bit,s32bit,s64bit,
                                          customint]);
      end;


    { true if p is a boolean }
    function is_boolean(def : tdef) : boolean;
      begin
        result:=(def.typ=orddef) and
                    (torddef(def).ordtype in [pasbool1,pasbool8,pasbool16,pasbool32,pasbool64,bool8bit,bool16bit,bool32bit,bool64bit]);
      end;


    function is_pasbool(def : tdef) : boolean;
      begin
        result:=(def.typ=orddef) and
                    (torddef(def).ordtype in [pasbool1,pasbool8,pasbool16,pasbool32,pasbool64]);
      end;

    { true if def is a C-style boolean (non-zero value = true, zero = false) }
    function is_cbool(def : tdef) : boolean;
      begin
        result:=(def.typ=orddef) and
                    (torddef(def).ordtype in [bool8bit,bool16bit,bool32bit,bool64bit]);
      end;


    { true if p is a void }
    function is_void(def : tdef) : boolean;
      begin
        result:=(def.typ=orddef) and
                 (torddef(def).ordtype=uvoid);
      end;


    { true if p is a char }
    function is_char(def : tdef) : boolean;
      begin
        result:=(def.typ=orddef) and
                 (torddef(def).ordtype=uchar);
      end;


    { true if p is a wchar }
    function is_widechar(def : tdef) : boolean;
      begin
        result:=(def.typ=orddef) and
                 (torddef(def).ordtype=uwidechar);
      end;


    { true if p is a char or wchar }
    function is_anychar(def : tdef) : boolean;
      begin
        result:=(def.typ=orddef) and
                 (torddef(def).ordtype in [uchar,uwidechar])
      end;


    { true if p is signed (integer) }
    function is_signed(def : tdef) : boolean;
      begin
         case def.typ of
           orddef :
             result:=torddef(def).low < 0;
           enumdef :
             result:=tenumdef(def).min < 0;
           arraydef :
             result:=is_signed(tarraydef(def).rangedef);
           else
             result:=false;
         end;
      end;


    function get_unsigned_inttype(def: tdef): torddef;
      begin
        case def.typ of
          orddef,
          enumdef:
            result:=cgsize_orddef(tcgsize2unsigned[def_cgsize(def)]);
          else
            internalerror(2016062001);
        end;
      end;


    function is_in_limit(def_from,def_to : tdef) : boolean;

      begin
         if (def_from.typ<>def_to.typ) or
            not(def_from.typ in [orddef,enumdef,setdef]) then
           begin
             is_in_limit := false;
             exit;
           end;
         case def_from.typ of
           orddef:
             is_in_limit:=(torddef(def_from).low>=torddef(def_to).low) and
                          (torddef(def_from).high<=torddef(def_to).high);
           enumdef:
             is_in_limit:=(tenumdef(def_from).min>=tenumdef(def_to).min) and
                          (tenumdef(def_from).max<=tenumdef(def_to).max);
           setdef:
             is_in_limit:=(tsetdef(def_from).setbase>=tsetdef(def_to).setbase) and
                          (tsetdef(def_from).setmax<=tsetdef(def_to).setmax);
         else
           is_in_limit:=false;
         end;
      end;


    function is_managed_type(def: tdef): boolean;{$ifdef USEINLINE}inline;{$endif}
      begin
        result:=def.needs_inittable;
      end;


    function is_rtti_managed_type(def: tdef): boolean;
      begin
        result:=def.needs_inittable and not (
          is_interfacecom_or_dispinterface(def) or
          (def.typ=variantdef) or
          (
            (def.typ=stringdef) and
            (tstringdef(def).stringtype in [st_ansistring,st_widestring,st_unicodestring])
          )
        );
      end;


    { true, if p points to an open array def }
    function is_open_string(p : tdef) : boolean;
      begin
         is_open_string:=(p.typ=stringdef) and
                         (tstringdef(p).stringtype=st_shortstring) and
                         (tstringdef(p).len=0);
      end;


    { true, if p points to a zero based array def }
    function is_zero_based_array(p : tdef) : boolean;
      begin
         result:=(p.typ=arraydef) and
                 (tarraydef(p).lowrange=0) and
                 not(is_special_array(p));
      end;

    { true if p points to a dynamic array def }
    function is_dynamic_array(p : tdef) : boolean;
      begin
         result:=(p.typ=arraydef) and
                 (ado_IsDynamicArray in tarraydef(p).arrayoptions);
      end;


    { true, if p points to an open array def }
    function is_open_array(p : tdef) : boolean;
      begin
         { check for sizesinttype is needed, because for unsigned the high
           range is also -1 ! (PFV) }
         result:=(p.typ=arraydef) and
                 (tarraydef(p).rangedef=sizesinttype) and
                 (tarraydef(p).lowrange=0) and
                 (tarraydef(p).highrange=-1) and
                 ((tarraydef(p).arrayoptions * [ado_IsVariant,ado_IsArrayOfConst,ado_IsConstructor,ado_IsDynamicArray])=[]);
      end;

    { true, if p points to an array of const def }
    function is_array_constructor(p : tdef) : boolean;
      begin
         result:=(p.typ=arraydef) and
                 (ado_IsConstructor in tarraydef(p).arrayoptions);
      end;

    { true, if p points to a variant array }
    function is_variant_array(p : tdef) : boolean;
      begin
         result:=(p.typ=arraydef) and
                 (ado_IsVariant in tarraydef(p).arrayoptions);
      end;

    { true, if p points to an array of const }
    function is_array_of_const(p : tdef) : boolean;
      begin
         result:=(p.typ=arraydef) and
                 (ado_IsArrayOfConst in tarraydef(p).arrayoptions);
      end;

    { true, if p points to a special array, bitpacked arrays aren't special in this regard though }
    function is_special_array(p : tdef) : boolean;
      begin
         result:=(p.typ=arraydef) and
                 (
                  ((tarraydef(p).arrayoptions * [ado_IsVariant,ado_IsArrayOfConst,ado_IsConstructor,ado_IsDynamicArray])<>[]) or
                  is_open_array(p)
                 );
      end;

    { true if p is an ansi string def }
    function is_ansistring(p : tdef) : boolean;
      begin
         is_ansistring:=(p.typ=stringdef) and
                        (tstringdef(p).stringtype=st_ansistring);
      end;

    { true if p is an ansi string def with codepage CP_NONE }
    function is_rawbytestring(p : tdef) : boolean;
      begin
        is_rawbytestring:=(p.typ=stringdef) and
                       (tstringdef(p).stringtype=st_ansistring) and
                       (tstringdef(p).encoding=globals.CP_NONE);
      end;

    { true if p is an long string def }
    function is_longstring(p : tdef) : boolean;
      begin
         is_longstring:=(p.typ=stringdef) and
                        (tstringdef(p).stringtype=st_longstring);
      end;


    { true if p is an wide string def }
    function is_widestring(p : tdef) : boolean;
      begin
         is_widestring:=(p.typ=stringdef) and
                        (tstringdef(p).stringtype=st_widestring);
      end;


    function is_dynamicstring(p: tdef): boolean;
      begin
         is_dynamicstring:=(p.typ=stringdef) and
                        (tstringdef(p).stringtype in [st_ansistring,st_widestring,st_unicodestring]);
      end;


    { true if p is an wide string def }
    function is_wide_or_unicode_string(p : tdef) : boolean;
      begin
         is_wide_or_unicode_string:=(p.typ=stringdef) and
                        (tstringdef(p).stringtype in [st_widestring,st_unicodestring]);
      end;


    { true if p is an unicode string def }
    function is_unicodestring(p : tdef) : boolean;
      begin
         is_unicodestring:=(p.typ=stringdef) and
                        (tstringdef(p).stringtype=st_unicodestring);
      end;


    { true if p is an short string def }
    function is_shortstring(p : tdef) : boolean;
      begin
         is_shortstring:=(p.typ=stringdef) and
                         (tstringdef(p).stringtype=st_shortstring);
      end;


    { true if p is bit packed array def }
    function is_packed_array(p: tdef) : boolean;
      begin
        is_packed_array :=
           (p.typ = arraydef) and
           (ado_IsBitPacked in tarraydef(p).arrayoptions);
      end;


    { true if p is bit packed record def }
    function is_packed_record_or_object(p: tdef) : boolean;
      begin
        is_packed_record_or_object :=
           (p.typ in [recorddef,objectdef]) and
           (tabstractrecorddef(p).is_packed);
      end;


    { true if p is a char array def }
    function is_chararray(p : tdef) : boolean;
      begin
        is_chararray:=(p.typ=arraydef) and
                      is_char(tarraydef(p).elementdef) and
                      not(is_special_array(p));
      end;

    { true if p is a widechar array def }
    function is_widechararray(p : tdef) : boolean;
      begin
        is_widechararray:=(p.typ=arraydef) and
                          is_widechar(tarraydef(p).elementdef) and
                          not(is_special_array(p));
      end;


    { true if p is a open char array def }
    function is_open_chararray(p : tdef) : boolean;
      begin
        is_open_chararray:= is_open_array(p) and
                            is_char(tarraydef(p).elementdef);
      end;

    { true if p is a open wide char array def }
    function is_open_widechararray(p : tdef) : boolean;
      begin
        is_open_widechararray:= is_open_array(p) and
                                is_widechar(tarraydef(p).elementdef);
      end;

    { true if p is any pointer def }
    function is_pointer(p : tdef) : boolean;
      begin
        is_pointer:=(p.typ=pointerdef);
      end;

    { true if p is a pchar def }
    function is_pchar(p : tdef) : boolean;
      begin
        is_pchar:=(p.typ=pointerdef) and
                  (is_char(tpointerdef(p).pointeddef) or
                   (is_zero_based_array(tpointerdef(p).pointeddef) and
                    is_chararray(tpointerdef(p).pointeddef)));
      end;

    { true if p is a pchar def }
    function is_pwidechar(p : tdef) : boolean;
      begin
        is_pwidechar:=(p.typ=pointerdef) and
                      (is_widechar(tpointerdef(p).pointeddef) or
                       (is_zero_based_array(tpointerdef(p).pointeddef) and
                        is_widechararray(tpointerdef(p).pointeddef)));
      end;


    { true if p is a voidpointer def }
    function is_voidpointer(p : tdef) : boolean;
      begin
        is_voidpointer:=(p.typ=pointerdef) and
                        (tpointerdef(p).pointeddef.typ=orddef) and
                        (torddef(tpointerdef(p).pointeddef).ordtype=uvoid);
      end;


    { true, if def is a 8 bit int type }
    function is_8bitint(def : tdef) : boolean;
      begin
         result:=(def.typ=orddef) and (torddef(def).ordtype in [u8bit,s8bit])
      end;

    { true, if def is a 8 bit ordinal type }
    function is_8bit(def : tdef) : boolean;
      begin
         result:=(def.typ=orddef) and (torddef(def).ordtype in [u8bit,s8bit,pasbool1,pasbool8,bool8bit,uchar])
      end;

    { true, if def is a 16 bit int type }
    function is_16bitint(def : tdef) : boolean;
      begin
         result:=(def.typ=orddef) and (torddef(def).ordtype in [u16bit,s16bit])
      end;

    { true, if def is a 16 bit ordinal type }
    function is_16bit(def : tdef) : boolean;
      begin
         result:=(def.typ=orddef) and (torddef(def).ordtype in [u16bit,s16bit,pasbool16,bool16bit,uwidechar])
      end;

    { true, if def is a 32 bit int type }
    function is_32bitint(def : tdef) : boolean;
      begin
         result:=(def.typ=orddef) and (torddef(def).ordtype in [u32bit,s32bit])
      end;

    { true, if def is a 32 bit ordinal type }
    function is_32bit(def: tdef): boolean;
      begin
         result:=(def.typ=orddef) and (torddef(def).ordtype in [u32bit,s32bit,pasbool32,bool32bit])
      end;

    { true, if def is a 64 bit int type }
    function is_64bitint(def : tdef) : boolean;
      begin
         is_64bitint:=(def.typ=orddef) and (torddef(def).ordtype in [u64bit,s64bit])
      end;


    { true, if def is a 64 bit type }
    function is_64bit(def : tdef) : boolean;
      begin
         is_64bit:=(def.typ=orddef) and (torddef(def).ordtype in [u64bit,s64bit,scurrency,pasbool64,bool64bit])
      end;


    { true, if def1 and def2 are both integers of the same bit size and sign }
    function are_equal_ints(def1, def2: tdef): boolean;
      begin
        result:=(def1.typ=orddef) and (def2.typ=orddef) and
          (torddef(def1).ordtype in [u8bit,u16bit,u32bit,u64bit,
                                     s8bit,s16bit,s32bit,s64bit,customint]) and
          (torddef(def1).ordtype=torddef(def2).ordtype) and
          ((torddef(def1).ordtype<>customint) or
           ((torddef(def1).low=torddef(def2).low) and
            (torddef(def1).high=torddef(def2).high)));
      end;


    { true, if def is an int type, larger than the processor's native int size }
    function is_oversizedint(def : tdef) : boolean;
      begin
{$if defined(cpu8bitalu)}
         result:=is_64bitint(def) or is_32bitint(def) or is_16bitint(def);
{$elseif defined(cpu16bitalu)}
         result:=is_64bitint(def) or is_32bitint(def);
{$elseif defined(cpu32bitaddr)}
         result:=is_64bitint(def);
{$elseif defined(cpu64bitaddr)}
         result:=false;
{$endif}
      end;

    { true, if def is an ordinal type, larger than the processor's native int size }
    function is_oversizedord(def : tdef) : boolean;
      begin
{$if defined(cpu8bitalu)}
         result:=is_64bit(def) or is_32bit(def) or is_16bit(def);
{$elseif defined(cpu16bitalu)}
         result:=is_64bit(def) or is_32bit(def);
{$elseif defined(cpu32bitaddr)}
         result:=is_64bit(def);
{$elseif defined(cpu64bitaddr)}
         result:=false;
{$endif}
      end;


    { true, if def is an int type, equal in size to the processor's native int size }
    function is_nativeint(def: tdef): boolean;
      begin
{$if defined(cpu8bitalu)}
         result:=is_8bitint(def);
{$elseif defined(cpu16bitalu)}
         result:=is_16bitint(def);
{$elseif defined(cpu32bitaddr)}
         result:=is_32bitint(def);
{$elseif defined(cpu64bitaddr)}
         result:=is_64bitint(def);
{$endif}
      end;

    { true, if def is an ordinal type, equal in size to the processor's native int size }
    function is_nativeord(def: tdef): boolean;
      begin
{$if defined(cpu8bitalu)}
         result:=is_8bit(def);
{$elseif defined(cpu16bitalu)}
         result:=is_16bit(def);
{$elseif defined(cpu32bitaddr)}
         result:=is_32bit(def);
{$elseif defined(cpu64bitaddr)}
         result:=is_64bit(def);
{$endif}
      end;

    { true, if def is an unsigned int type, equal in size to the processor's native int size }
    function is_nativeuint(def: tdef): boolean;
      begin
         result:=is_nativeint(def) and (def.typ=orddef) and (torddef(def).ordtype in [u64bit,u32bit,u16bit,u8bit]);
      end;

    { true, if def is a signed int type, equal in size to the processor's native int size }
    function is_nativesint(def: tdef): boolean;
      begin
         result:=is_nativeint(def) and (def.typ=orddef) and (torddef(def).ordtype in [s64bit,s32bit,s16bit,s8bit]);
      end;

    { if l isn't in the range of todef a range check error (if not explicit) is generated and
      the value is placed within the range }
    procedure testrange(todef : tdef;var l : tconstexprint;explicit,forcerangecheck:boolean);
      var
         lv,hv: TConstExprInt;
      begin
         { for 64 bit types we need only to check if it is less than }
         { zero, if def is a qword node                              }
         getrange(todef,lv,hv);
         if (l<lv) or (l>hv) then
           begin
             if not explicit then
               begin
                 if ((todef.typ=enumdef) and
                     { delphi allows range check errors in
                      enumeration type casts FK }
                     not(m_delphi in current_settings.modeswitches)) or
                    (cs_check_range in current_settings.localswitches) or
                    forcerangecheck then
                   Message3(type_e_range_check_error_bounds,tostr(l),tostr(lv),tostr(hv))
                 else
                   Message3(type_w_range_check_error_bounds,tostr(l),tostr(lv),tostr(hv));
               end;
             { Fix the value to fit in the allocated space for this type of variable }
             case longint(todef.size) of
               1: l := l and $ff;
               2: l := l and $ffff;
               4: l := l and $ffffffff;
               else
                 ;
             end;
             {reset sign, i.e. converting -1 to qword changes the value to high(qword)}
             l.signed:=false;
             { do sign extension if necessary (JM) }
             if is_signed(todef) then
              begin
                case longint(todef.size) of
                  1: l.svalue := shortint(l.svalue);
                  2: l.svalue := smallint(l.svalue);
                  4: l.svalue := longint(l.svalue);
                  else
                    ;
                end;
                l.signed:=true;
              end;
           end;
      end;


    { return the range from def in l and h }
    procedure getrange(def : tdef;out l, h : TConstExprInt);
      begin
        case def.typ of
          orddef :
            begin
              l:=torddef(def).low;
              h:=torddef(def).high;
            end;
          enumdef :
            begin
              l:=int64(tenumdef(def).min);
              h:=int64(tenumdef(def).max);
            end;
          arraydef :
            begin
              l:=int64(tarraydef(def).lowrange);
              h:=int64(tarraydef(def).highrange);
            end;
          undefineddef:
            begin
              l:=torddef(sizesinttype).low;
              h:=torddef(sizesinttype).high;
            end;
          else
            internalerror(200611054);
        end;
      end;


    function mmx_type(p : tdef) : tmmxtype;
      begin
         mmx_type:=mmxno;
         if is_mmx_able_array(p) then
           begin
              if tarraydef(p).elementdef.typ=floatdef then
                case tfloatdef(tarraydef(p).elementdef).floattype of
                  s32real:
                    mmx_type:=mmxsingle;
                  else
                    ;
                end
              else
                case torddef(tarraydef(p).elementdef).ordtype of
                   u8bit:
                     mmx_type:=mmxu8bit;
                   s8bit:
                     mmx_type:=mmxs8bit;
                   u16bit:
                     mmx_type:=mmxu16bit;
                   s16bit:
                     mmx_type:=mmxs16bit;
                   u32bit:
                     mmx_type:=mmxu32bit;
                   s32bit:
                     mmx_type:=mmxs32bit;
                   else
                     ;
                end;
           end;
      end;


    { The range-type of an ordinal-type that is a subrange-type shall be the host-type (see 6.4.2.4) of the subrange-type.
      The range-type of an ordinal-type that is not a subrange-type shall be the ordinal-type.

      The subrange-bounds shall be of compatible ordinal-types, and the range-type (see 6.4.2.1) of the ordinal-types shall
      be designated the host-type of the subrange-type. }
    function get_iso_range_type(def: tdef): tdef;
      begin
        result:=nil;
        case def.typ of
           orddef:
             begin
               if is_integer(def) then
                 begin
                   if (torddef(def).low>=torddef(sinttype).low) and
                      (torddef(def).high<=torddef(sinttype).high) then
                     result:=sinttype
                   else
                     range_to_type(torddef(def).low,torddef(def).high,result);
                 end
               else case torddef(def).ordtype of
                 pasbool1:
                   result:=pasbool1type;
                 pasbool8:
                   result:=pasbool8type;
                 pasbool16:
                   result:=pasbool16type;
                 pasbool32:
                   result:=pasbool32type;
                 pasbool64:
                   result:=pasbool64type;
                 bool8bit:
                   result:=bool8type;
                 bool16bit:
                   result:=bool16type;
                 bool32bit:
                   result:=bool32type;
                 bool64bit:
                   result:=bool64type;
                 uchar:
                   result:=cansichartype;
                 uwidechar:
                   result:=cwidechartype;
                 scurrency:
                   result:=s64currencytype;
                 else
                   internalerror(2018010901);
               end;
             end;
           enumdef:
             begin
               while assigned(tenumdef(def).basedef) do
                 def:=tenumdef(def).basedef;
               result:=def;
             end
           else
             internalerror(2018010701);
        end;
      end;


    function is_vector(p : tdef) : boolean;
      begin
        result:=(p.typ=arraydef) and
                not(is_special_array(p)) and
                (tarraydef(p).elementdef.typ=floatdef) and
                (tfloatdef(tarraydef(p).elementdef).floattype in [s32real,s64real]);
      end;


    { returns if the passed type (array) fits into an mm register }
    function fits_in_mm_register(p : tdef) : boolean;
      begin
{$ifdef x86}
        result:= is_vector(p) and
                 (
                  (tarraydef(p).elementdef.typ=floatdef) and
                  (
                   (tarraydef(p).lowrange=0) and
                   (tarraydef(p).highrange=3) and
                   (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
                  )
                 ) or

                 (
                  (tarraydef(p).elementdef.typ=floatdef) and
                  (
                   (tarraydef(p).lowrange=0) and
                   (tarraydef(p).highrange=1) and
                   (tfloatdef(tarraydef(p).elementdef).floattype=s64real)
                  )
                 );
{$else x86}
        result:=false;
{$endif x86}
      end;


    function is_mmx_able_array(p : tdef) : boolean;
      begin
{$ifdef SUPPORT_MMX}
         if (cs_mmx_saturation in current_settings.localswitches) then
           begin
              is_mmx_able_array:=(p.typ=arraydef) and
                not(is_special_array(p)) and
                (
                 (
                  (tarraydef(p).elementdef.typ=orddef) and
                  (
                   (
                    (tarraydef(p).lowrange=0) and
                    (tarraydef(p).highrange=1) and
                    (torddef(tarraydef(p).elementdef).ordtype in [u32bit,s32bit])
                   )
                   or
                   (
                    (tarraydef(p).lowrange=0) and
                    (tarraydef(p).highrange=3) and
                    (torddef(tarraydef(p).elementdef).ordtype in [u16bit,s16bit])
                   )
                  )
                 )
                 or
                (
                 (
                  (tarraydef(p).elementdef.typ=floatdef) and
                  (
                   (tarraydef(p).lowrange=0) and
                   (tarraydef(p).highrange=1) and
                   (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
                  )
                 )
                )
              );
           end
         else
           begin
              is_mmx_able_array:=(p.typ=arraydef) and
                (
                 (
                  (tarraydef(p).elementdef.typ=orddef) and
                  (
                   (
                    (tarraydef(p).lowrange=0) and
                    (tarraydef(p).highrange=1) and
                    (torddef(tarraydef(p).elementdef).ordtype in [u32bit,s32bit])
                   )
                   or
                   (
                    (tarraydef(p).lowrange=0) and
                    (tarraydef(p).highrange=3) and
                    (torddef(tarraydef(p).elementdef).ordtype in [u16bit,s16bit])
                   )
                   or
                   (
                    (tarraydef(p).lowrange=0) and
                    (tarraydef(p).highrange=7) and
                    (torddef(tarraydef(p).elementdef).ordtype in [u8bit,s8bit])
                   )
                  )
                 )
                 or
                 (
                  (tarraydef(p).elementdef.typ=floatdef) and
                  (
                   (tarraydef(p).lowrange=0) and
                   (tarraydef(p).highrange=1) and
                   (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
                  )
                 )
                );
           end;
{$else SUPPORT_MMX}
         is_mmx_able_array:=false;
{$endif SUPPORT_MMX}
      end;


    function def_cgsize(def: tdef): tcgsize;
      begin
        case def.typ of
          orddef,
          enumdef,
          setdef:
            begin
              result:=int_cgsize(def.size);
              if is_signed(def) then
                result:=tcgsize(ord(result)+(ord(OS_S8)-ord(OS_8)));
            end;
          classrefdef,
          pointerdef:
            begin
              result:=int_cgsize(def.size);
              { can happen for far/huge pointers on non-i8086 }
              if result=OS_NO then
                internalerror(2013052201);
            end;
          formaldef:
            result := int_cgsize(voidpointertype.size);
          procvardef:
            result:=int_cgsize(def.size);
          stringdef :
            result:=int_cgsize(def.size);
          objectdef :
            result:=int_cgsize(def.size);
          floatdef:
            if cs_fp_emulation in current_settings.moduleswitches then
              result:=int_cgsize(def.size)
            else
              result:=tfloat2tcgsize[tfloatdef(def).floattype];
          recorddef :
            result:=int_cgsize(def.size);
          arraydef :
            begin
              if is_dynamic_array(def) or not is_special_array(def) then
                begin
                  if (cs_support_vectors in current_settings.globalswitches) and is_vector(def) and ((TArrayDef(def).elementdef.typ = floatdef) and not (cs_fp_emulation in current_settings.moduleswitches)) then
                    begin
                      { Determine if, based on the floating-point type and the size
                        of the array, if it can be made into a vector }
                      case TFloatDef(def).floattype of
                        s32real:
                          result := float_array_cgsize(def.size);
                        s64real:
                          result := double_array_cgsize(def.size);
                        else
                          { If not, fall back }
                          result := int_cgsize(def.size);
                      end;
                    end
                  else
                    result := int_cgsize(def.size);
                end
              else
                result := OS_NO;
            end;
          else
            begin
              { undefined size }
              result:=OS_NO;
            end;
        end;
      end;

    function cgsize_orddef(size: tcgsize): torddef;
      begin
        case size of
          OS_8:
            result:=torddef(u8inttype);
          OS_S8:
            result:=torddef(s8inttype);
          OS_16:
            result:=torddef(u16inttype);
          OS_S16:
            result:=torddef(s16inttype);
          OS_32:
            result:=torddef(u32inttype);
          OS_S32:
            result:=torddef(s32inttype);
          OS_64:
            result:=torddef(u64inttype);
          OS_S64:
            result:=torddef(s64inttype);
          else
            internalerror(2012050401);
        end;
      end;

    function def_cgmmsize(def: tdef): tcgsize;
      begin
        case def.typ of
          arraydef:
            begin
              case tarraydef(def).elementdef.typ of
                orddef:
                  begin
                    { this is not correct, OS_MX normally mean that the vector
                      contains elements of size X. However, vectors themselves
                      can also have different sizes (e.g. a vector of 2 singles on
                      SSE) and the total size is currently more important }
                    case def.size of
                      1: result:=OS_M8;
                      2: result:=OS_M16;
                      4: result:=OS_M32;
                      8: result:=OS_M64;
                      16: result:=OS_M128;
                      32: result:=OS_M256;
                      64: result:=OS_M512;
                      else
                        internalerror(2013060103);
                    end;
                  end;
                floatdef:
                  begin
                    case TFloatDef(tarraydef(def).elementdef).floattype of
                      s32real:
                        case def.size of
                          4:  result:=OS_MF32;
                          16: result:=OS_MF128;
                          32: result:=OS_MF256;
                          64: result:=OS_MF512;
                          else
                            internalerror(2017121400);
                        end;
                      s64real:
                        case def.size of
                          8:  result:=OS_MD64;
                          16: result:=OS_MD128;
                          32: result:=OS_MD256;
                          64: result:=OS_MD512;
                          else
                            internalerror(2017121401);
                        end;
                      else
                        internalerror(2017121402);
                    end;
                  end;
                else
                  result:=def_cgsize(def);
              end;
            end
          else
            result:=def_cgsize(def);
        end;
      end;

    { In Windows 95 era, ordinals were restricted to [u8bit,s32bit,s16bit,bool16bit]
      As of today, both signed and unsigned types from 8 to 64 bits are supported. }
    function is_automatable(p : tdef) : boolean;
      begin
        case p.typ of
          orddef:
            result:=torddef(p).ordtype in [u8bit,s8bit,u16bit,s16bit,u32bit,s32bit,
              u64bit,s64bit,bool16bit,scurrency];
          floatdef:
            result:=tfloatdef(p).floattype in [s64currency,s64real,s32real];
          stringdef:
            result:=tstringdef(p).stringtype in [st_ansistring,st_widestring,st_unicodestring];
          variantdef:
            result:=true;
          objectdef:
            result:=tobjectdef(p).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba];
          else
            result:=false;
        end;
      end;


    {# returns true, if the type passed is a varset }
    function is_smallset(p : tdef) : boolean;
      begin
        {$if defined(cpu8bitalu)}
          result:=(p.typ=setdef) and (p.size = 1)
        {$elseif defined(cpu16bitalu)}
          result:=(p.typ=setdef) and (p.size in [1,2])
        {$else}
          result:=(p.typ=setdef) and (p.size in [1,2,4])
        {$endif}
      end;


    function is_bareprocdef(pd : tprocdef): boolean;
      begin
        result:=(pd.maxparacount=0) and
                (is_void(pd.returndef) or
                 (pd.proctypeoption = potype_constructor));
      end;

    function is_c_variadic(pd: tabstractprocdef): boolean;
      begin
        result:=
          (po_varargs in pd.procoptions) or
          (po_variadic in pd.procoptions);
      end;

    function get_common_intdef(ld, rd: torddef; keep_sign_if_equal: boolean): torddef;
      var
        llow, lhigh: tconstexprint;
      begin
        llow:=min(ld.low,rd.low);
        lhigh:=max(ld.high,rd.high);
        case range_to_basetype(llow,lhigh) of
          s8bit:
            result:=torddef(s8inttype);
          u8bit:
            result:=torddef(u8inttype);
          s16bit:
            result:=torddef(s16inttype);
          u16bit:
            result:=torddef(u16inttype);
          s32bit:
            result:=torddef(s32inttype);
          u32bit:
            result:=torddef(u32inttype);
          s64bit:
            result:=torddef(s64inttype);
          u64bit:
            result:=torddef(u64inttype);
          else
            begin
              { avoid warning }
              result:=nil;
              internalerror(200802291);
            end;
        end;
        if keep_sign_if_equal and
           (is_signed(ld)=is_signed(rd)) and
           (is_signed(result)<>is_signed(ld)) then
          case result.ordtype of
            s8bit:
              result:=torddef(u8inttype);
            u8bit:
              result:=torddef(s16inttype);
            s16bit:
              result:=torddef(u16inttype);
            u16bit:
              result:=torddef(s32inttype);
            s32bit:
              result:=torddef(u32inttype);
            u32bit:
              result:=torddef(s64inttype);
            s64bit:
              result:=torddef(u64inttype);
            else
              ;
          end;
      end;


    function is_valid_univ_para_type(def: tdef): boolean;
      begin
        result:=
          not is_open_array(def) and
          not is_void(def) and
          (def.typ<>formaldef);
      end;


    function is_nested_pd(def: tabstractprocdef): boolean;{$ifdef USEINLINE}inline;{$endif}
      begin
        result:=def.parast.symtablelevel>normal_function_level;
      end;


    function is_typeparam(def : tdef) : boolean;{$ifdef USEINLINE}inline;{$endif}
      begin
        result:=(def.typ=undefineddef);
      end;


    function is_methodpointer(def: tdef): boolean;
      begin
        result:=(def.typ=procvardef) and (po_methodpointer in tprocvardef(def).procoptions);
      end;


    function is_block(def: tdef): boolean;
      begin
        result:=(def.typ=procvardef) and (po_is_block in tprocvardef(def).procoptions)
      end;


    function get_typekind(def:tdef):byte;
      begin
        case def.typ of
          arraydef:
            if ado_IsDynamicArray in tarraydef(def).arrayoptions then
              result:=tkDynArray
            else
              result:=tkArray;
          recorddef:
            result:=tkRecord;
          pointerdef:
            result:=tkPointer;
          orddef:
            case torddef(def).ordtype of
              u8bit,
              u16bit,
              u32bit,
              s8bit,
              s16bit,
              s32bit:
                result:=tkInteger;
              u64bit:
                result:=tkQWord;
              s64bit:
                result:=tkInt64;
              pasbool1,
              pasbool8,
              pasbool16,
              pasbool32,
              pasbool64,
              bool8bit,
              bool16bit,
              bool32bit,
              bool64bit:
                result:=tkBool;
              uchar:
                result:=tkChar;
              uwidechar:
                result:=tkWChar;
              scurrency:
                result:=tkFloat;
              else
                result:=tkUnknown;
            end;
          stringdef:
            case tstringdef(def).stringtype of
              st_shortstring:
                result:=tkSString;
              st_longstring:
                result:=tkLString;
              st_ansistring:
                result:=tkAString;
              st_widestring:
                result:=tkWString;
              st_unicodestring:
                result:=tkUString;
            end;
          enumdef:
            result:=tkEnumeration;
          objectdef:
            case tobjectdef(def).objecttype of
              odt_class,
              odt_javaclass:
                result:=tkClass;
              odt_object:
                result:=tkObject;
              odt_interfacecom,
              odt_dispinterface,
              odt_interfacejava:
                result:=tkInterface;
              odt_interfacecorba:
                result:=tkInterfaceCorba;
              odt_helper:
                result:=tkHelper;
              else
                result:=tkUnknown;
            end;
          { currently tkFile is not used }
          {filedef:
            result:=tkFile;}
          setdef:
            result:=tkSet;
          procvardef:
            if tprocvardef(def).is_methodpointer then
              result:=tkMethod
            else
              result:=tkProcVar;
          floatdef:
            result:=tkFloat;
          classrefdef:
            result:=tkClassRef;
          variantdef:
            result:=tkVariant;
          else
            result:=tkUnknown;
        end;
      end;

end.