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
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
|
{
This file is part of the Free Pascal Run time library.
Copyright (c) 1999-2008 by the Free Pascal development team
See the file COPYING.FPC, included in this distribution,
For details about the copyright.
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.
**********************************************************************}
{ ObjpasInt is the integer type, equivalent to Objpas.Integer (the Integer
type in ObjFpc and Delphi modes). It is defined here for use in the
implementation part of the System unit. }
{$ifdef CPU16}
type
ObjpasInt = SmallInt;
{$else CPU16}
type
ObjpasInt = LongInt;
{$endif CPU16}
{****************************************************************************
Local types
****************************************************************************}
{$ifdef FPC_HAS_FEATURE_EXITCODE}
{$if High(errorcode)<>maxExitCode}
{$define FPC_LIMITED_EXITCODE}
{$endif}
{$endif FPC_HAS_FEATURE_EXITCODE}
Procedure HandleError (Errno : TExitCode); external name 'FPC_HANDLEERROR';
Procedure HandleErrorFrame (Errno : TExitCode;frame : Pointer); forward;
Procedure HandleErrorAddrFrame (Errno : TExitCode;addr : CodePointer; frame : Pointer); forward;
Procedure HandleErrorAddrFrameInd (Errno : TExitCode;addr : CodePointer; frame : Pointer); forward;
{$ifdef FPC_HAS_FEATURE_TEXTIO}
type
FileFunc = Procedure(var t : TextRec);
{$endif FPC_HAS_FEATURE_TEXTIO}
const
{$if defined(CPUAVR)}
STACK_MARGIN = 64; { Stack size margin for stack checking }
{$elseif defined(CPUZ80)}
STACK_MARGIN = 64; { Stack size margin for stack checking }
{$else}
STACK_MARGIN = 16384; { Stack size margin for stack checking }
{$endif}
{ Random / Randomize constants }
OldRandSeed : Cardinal = 0;
{ For Error Handling.}
ErrorBase : Pointer = nil;public name 'FPC_ERRORBASE';
{ Used by the ansi/widestrings and maybe also other things in the future }
var
{ widechar, because also used by widestring -> pwidechar conversions }
emptychar : widechar;public name 'FPC_EMPTYCHAR';
{$ifndef FPC_NO_GENERIC_STACK_CHECK}
{ if the OS does the stack checking, we don't need any stklen from the
main program }
initialstklen : SizeUint{$ifndef FPC_HAS_INDIRECT_ENTRY_INFORMATION}; external name '__stklen';{$else} = 0;{$endif}
{$endif FPC_NO_GENERIC_STACK_CHECK}
{$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
EntryInformation: TEntryInformation;
{$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
var
{$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
{$ifdef FPC_HAS_RESSTRINITS}
FPCResStrInitTables : Pointer;public name '_FPC_ResStrInitTables';
{$endif FPC_HAS_RESSTRINITS}
FPCResourceStringTables : Pointer;public name '_FPC_ResourceStringTables';
FPCResLocation : Pointer;public name'_FPC_ResLocation';
{$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
{$ifdef FPC_HAS_RESSTRINITS}
FPCResStrInitTablesVar : record end; external name 'FPC_RESSTRINITTABLES';
FPCResStrInitTables : Pointer = @FPCResStrInitTablesVar;public name '_FPC_ResStrInitTables';
{$endif FPC_HAS_RESSTRINITS}
FPCResourceStringTablesVar : record end; External Name 'FPC_RESOURCESTRINGTABLES';
FPCResourceStringTables : Pointer = @FPCResourceStringTablesVar;public name '_FPC_ResourceStringTables';
{$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
{$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
procedure SetupEntryInformation(constref info: TEntryInformation);[public,alias:'_FPC_SetupEntryInformation'];
begin
EntryInformation := info;
FPCResStrInitTables := info.ResStrInitTables;
FPCResourceStringTables := info.ResourceStringTables;
FPCResLocation := info.ResLocation;
{$ifdef FPC_SYSTEM_HAS_OSSETUPENTRYINFORMATION}
OSSetupEntryInformation(info);
{$endif FPC_SYSTEM_HAS_OSSETUPENTRYINFORMATION}
end;
{$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
{ checks whether the given suggested size for the stack of the current
thread is acceptable. If this is the case, returns it unaltered.
Otherwise it should return an acceptable value.
Operating systems that automatically expand their stack on demand, should
simply return a very large value.
Operating systems which do not have a possibility to retrieve stack size
information, should simply return the given stklen value (This is the default
implementation).
}
{$ifdef FPC_HAS_FEATURE_STACKCHECK}
function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt; forward;
{$endif FPC_HAS_FEATURE_STACKCHECK}
{*****************************************************************************
OS dependent Helpers/Syscalls
*****************************************************************************}
{ for some OSes do_isdevice is defined in sysos.inc, but for others (win32)
it isn't, and is used before the actual definition is encountered }
{$ifdef FPC_HAS_FEATURE_FILEIO}
function do_isdevice(handle:thandle):boolean;forward;
{$endif FPC_HAS_FEATURE_FILEIO}
{$i sysos.inc}
{****************************************************************************
Include processor specific routines
****************************************************************************}
{$ifdef FPC_USE_LIBC}
{$ifdef USE_CPU_MOVE}
{ Avoid use of generic C code for move procedure }
{$define FPC_SYSTEM_HAS_MOVE}
{$endif}
{ Under Haiku, bcopy cause a problem when searching for include file
in the compiler. So, we use the internal implementation for now
under BeOS and Haiku. }
{$ifndef BEOS}
{ prefer libc implementations over our own, as they're most likely faster }
{$i cgeneric.inc}
{ is now declared as external reference to another routine in the interface }
{$i cgenstr.inc}
{$endif}
{$ifdef USE_CPU_MOVE}
{ Avoid use of generic C code for move procedure }
{$undef FPC_SYSTEM_HAS_MOVE}
{$endif}
{$endif FPC_USE_LIBC}
{$ifdef cpui386}
{$ifdef SYSPROCDEFINED}
{$Error Can't determine processor type !}
{$endif}
{$i i386.inc} { Case dependent, don't change }
{$define SYSPROCDEFINED}
{$endif cpui386}
{$ifdef cpui8086}
{$ifdef SYSPROCDEFINED}
{$Error Can't determine processor type !}
{$endif}
{$i i8086.inc} { Case dependent, don't change }
{$define SYSPROCDEFINED}
{$endif cpui8086}
{$ifdef cpum68k}
{$ifdef SYSPROCDEFINED}
{$Error Can't determine processor type !}
{$endif}
{$i m68k.inc} { Case dependent, don't change }
{$define SYSPROCDEFINED}
{$endif cpum68k}
{$ifdef cpux86_64}
{$ifdef SYSPROCDEFINED}
{$Error Can't determine processor type !}
{$endif}
{$i x86_64.inc} { Case dependent, don't change }
{$define SYSPROCDEFINED}
{$endif cpux86_64}
{$ifdef cpupowerpc32}
{$ifdef SYSPROCDEFINED}
{$Error Can't determine processor type !}
{$endif}
{$i powerpc.inc} { Case dependent, don't change }
{$define SYSPROCDEFINED}
{$endif cpupowerpc32}
{$ifdef cpupowerpc64}
{$ifdef SYSPROCDEFINED}
{$Error Can't determine processor type !}
{$endif}
{$i powerpc64.inc} { Case dependent, don't change }
{$define SYSPROCDEFINED}
{$endif cpupowerpc64}
{$ifdef cpualpha}
{$ifdef SYSPROCDEFINED}
{$Error Can't determine processor type !}
{$endif}
{$i alpha.inc} { Case dependent, don't change }
{$define SYSPROCDEFINED}
{$endif cpualpha}
{$ifdef cpuiA64}
{$ifdef SYSPROCDEFINED}
{$Error Can't determine processor type !}
{$endif}
{$i ia64.inc} { Case dependent, don't change }
{$define SYSPROCDEFINED}
{$endif cpuiA64}
{$ifdef cpusparc}
{$ifdef SYSPROCDEFINED}
{$Error Can't determine processor type !}
{$endif}
{$i sparc.inc} { Case dependent, don't change }
{$define SYSPROCDEFINED}
{$endif cpusparc}
{$ifdef cpusparc64}
{$ifdef SYSPROCDEFINED}
{$Error Can't determine processor type !}
{$endif}
{$i sparc64.inc} { Case dependent, don't change }
{$define SYSPROCDEFINED}
{$endif cpusparc64}
{$ifdef cpuarm}
{$ifdef SYSPROCDEFINED}
{$Error Can't determine processor type !}
{$endif}
{$i armdefines.inc}
{$if defined(CPUTHUMB2)}
{$i thumb2.inc} { Case dependent, don't change }
{$else}
{$if defined(CPUTHUMB)}
{$i thumb.inc} { Case dependent, don't change }
{$else}
{$i arm.inc} { Case dependent, don't change }
{$endif}
{$endif}
{$define SYSPROCDEFINED}
{$endif cpuarm}
{$ifdef cpuavr}
{$ifdef SYSPROCDEFINED}
{$Error Can't determine processor type !}
{$endif}
{$i avr.inc} { Case dependent, don't change }
{$define SYSPROCDEFINED}
{$endif cpuavr}
{$ifdef cpumipsel}
{$ifdef SYSPROCDEFINED}
{$Error Can't determine processor type !}
{$endif}
{ there is no mipsel.inc, we use mips.inc instead }
{$i mips.inc} { Case dependent, don't change }
{$define SYSPROCDEFINED}
{$else not cpumipsel}
{$ifdef cpumips}
{$ifdef SYSPROCDEFINED}
{$Error Can't determine processor type !}
{$endif}
{$i mips.inc} { Case dependent, don't change }
{$define SYSPROCDEFINED}
{$endif cpumips}
{$endif not cpumipsel}
{$ifdef cpuaarch64}
{$ifdef SYSPROCDEFINED}
{$Error Can't determine processor type !}
{$endif}
{$i aarch64.inc} { Case dependent, don't change }
{$define SYSPROCDEFINED}
{$endif cpuaarch64}
{$ifdef cpuriscv32}
{$ifdef SYSPROCDEFINED}
{$Error Can't determine processor type !}
{$endif}
{$i riscv32.inc} { Case dependent, don't change }
{$define SYSPROCDEFINED}
{$endif cpuriscv32}
{$ifdef cpuriscv64}
{$ifdef SYSPROCDEFINED}
{$Error Can't determine processor type !}
{$endif}
{$i riscv64.inc} { Case dependent, don't change }
{$define SYSPROCDEFINED}
{$endif cpuriscv64}
{$ifdef cpuxtensa}
{$ifdef SYSPROCDEFINED}
{$Error Can't determine processor type !}
{$endif}
{$i xtensa.inc} { Case dependent, don't change }
{$define SYSPROCDEFINED}
{$endif cpuxtensa}
{$ifdef cpuz80}
{$ifdef SYSPROCDEFINED}
{$Error Can't determine processor type !}
{$endif}
{$i z80.inc} { Case dependent, don't change }
{$define SYSPROCDEFINED}
{$endif cpuz80}
{$ifndef SYSPROCDEFINED}
{$Error Can't determine processor type !}
{$endif}
procedure fillchar(var x;count : {$ifdef FILLCHAR_HAS_SIZEUINT_COUNT}SizeUInt{$else}SizeInt{$endif};value : boolean);
begin
fillchar(x,count,byte(value));
end;
procedure fillchar(var x;count : {$ifdef FILLCHAR_HAS_SIZEUINT_COUNT}SizeUInt{$else}SizeInt{$endif};value : char);
begin
fillchar(x,count,byte(value));
end;
procedure FillByte (var x;count : {$ifdef FILLCHAR_HAS_SIZEUINT_COUNT}SizeUInt{$else}SizeInt{$endif};value : byte );
begin
FillChar (X,Count,VALUE);
end;
function IndexChar(Const buf;len:SizeInt;b:char):SizeInt;
begin
IndexChar:=IndexByte(Buf,Len,byte(B));
end;
function CompareChar(Const buf1,buf2;len:SizeInt):SizeInt;
begin
CompareChar:=CompareByte(buf1,buf2,len);
end;
procedure fpc_zeromem(p:pointer;len:sizeuint);
begin
FillChar(p^,len,0);
end;
procedure fpc_fillmem(out data;len:sizeuint;b : byte);
begin
FillByte(data,len,b);
end;
{ Include generic pascal only routines which are not defined in the processor
specific include file }
{$I generic.inc}
{****************************************************************************
Set Handling
****************************************************************************}
{ Include set support which is processor specific}
{$i set.inc}
{ Include generic pascal routines for sets if the processor }
{ specific routines are not available. }
{$i genset.inc}
{****************************************************************************
Math Routines
****************************************************************************}
function Hi(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
begin
Hi := b shr 4
end;
function Lo(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
begin
Lo := b and $0f
end;
Function Swap (X : Word) : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
Begin
Swap := SwapEndian(X);
End;
Function Swap (X : Integer) : Integer;{$ifdef SYSTEMINLINE}inline;{$endif}
Begin
Swap := SwapEndian(X);
End;
Function Swap (X : Longint) : Longint;{$ifdef SYSTEMINLINE}inline;{$endif}
Begin
Swap:=(X shl 16) + (X shr 16);
End;
Function Swap (X : Cardinal) : Cardinal;{$ifdef SYSTEMINLINE}inline;{$endif}
Begin
Swap:=(X shl 16) + (X shr 16);
End;
Function Swap (X : QWord) : QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
Begin
Swap:=(X shl 32) + (X shr 32);
End;
Function Swap (X : Int64) : Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
Begin
Swap:=(X shl 32) + (X shr 32);
End;
{$ifdef SUPPORT_DOUBLE}
operator := (b:real48) d:double;{$ifdef SYSTEMINLINE}inline;{$endif}
begin
D:=real2double(b);
end;
{$endif SUPPORT_DOUBLE}
{$ifdef SUPPORT_EXTENDED}
operator := (b:real48) e:extended;{$ifdef SYSTEMINLINE}inline;{$endif}
begin
e:=real2double(b);
end;
{$endif SUPPORT_EXTENDED}
{$ifndef FPUNONE}
{$ifdef FPC_USE_LIBC}
{ Include libc versions }
{$i cgenmath.inc}
{$endif FPC_USE_LIBC}
{ Include processor specific routines }
{$I math.inc}
{ Include generic version }
{$I genmath.inc}
{$endif}
{$i gencurr.inc}
function aligntoptr(p : pointer) : pointer;inline;
begin
{$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
result:=align(p,sizeof(p));
{$else FPC_REQUIRES_PROPER_ALIGNMENT}
result:=p;
{$endif FPC_REQUIRES_PROPER_ALIGNMENT}
end;
function aligntoqword(p : pointer) : pointer;inline;
{$push}
{$packrecords c}
type
TAlignCheck = record
b : byte;
q : qword;
end;
{$pop}
begin
{$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
result:=align(p,PtrInt(@TAlignCheck(nil^).q))
{$else FPC_REQUIRES_PROPER_ALIGNMENT}
result:=p;
{$endif FPC_REQUIRES_PROPER_ALIGNMENT}
end;
function AlignTypeData(p : pointer) : pointer; inline;
begin
{$if defined(CPUM68K) and not defined(VER3_0)}
result := aligntoqword(p);
{$else CPUM68K and not VER3_0}
result := aligntoptr(p);
{$endif CPUM68K and not VER3_0}
end;
{$if not defined(VER3_0) and not defined(VER3_2)}
type
TRttiDataCommon =
{$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
packed
{$endif}
record
Attrs: Pointer;
end;
{$endif not VER3_0 and not VER3_2}
{****************************************************************************
Subroutines for String handling
****************************************************************************}
{ Needs to be before RTTI handling }
{$i sstrings.inc}
{ requires sstrings.inc for initval }
{$I int64p.inc}
{$I int64.inc}
{Requires int64.inc, since that contains the VAL functions for int64 and qword}
{$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
{$i astrings.inc}
{$endif FPC_HAS_FEATURE_ANSISTRINGS}
{$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
{$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
{$i wstrings.inc}
{$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
{$i ustrings.inc}
{$endif FPC_HAS_FEATURE_WIDESTRINGS}
{$i aliases.inc}
{****************************************************************************
Run-Time Type Information (RTTI) declarations
****************************************************************************}
{$ifdef FPC_HAS_FEATURE_RTTI}
{$i rttidecl.inc}
{$endif FPC_HAS_FEATURE_RTTI}
{*****************************************************************************
Dynamic Array support
*****************************************************************************}
{$ifdef FPC_HAS_FEATURE_DYNARRAYS}
{$i dynarr.inc}
{$endif FPC_HAS_FEATURE_DYNARRAYS}
{*****************************************************************************
Object Pascal support
*****************************************************************************}
{$ifdef FPC_HAS_FEATURE_CLASSES}
{$i objpas.inc}
{$endif FPC_HAS_FEATURE_CLASSES}
{*****************************************************************************
Variant support
*****************************************************************************}
{$ifdef FPC_HAS_FEATURE_VARIANTS}
{$i variant.inc}
{$endif FPC_HAS_FEATURE_VARIANTS}
{****************************************************************************
Run-Time Type Information (RTTI)
****************************************************************************}
{$ifdef FPC_HAS_FEATURE_RTTI}
{$i rtti.inc}
{$endif FPC_HAS_FEATURE_RTTI}
{$if defined(FPC_HAS_FEATURE_RANDOM)}
{$ifndef FPC_USE_SIMPLE_RANDOM}
{ Pascal translation of https://github.com/dajobe/libmtwist }
{* -*- Mode: c; c-basic-offset: 2 -*-
*
* mt.c - Mersenne Twister functions
*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org/>
*
*}
{$R-} {range checking off}
{$Q-} {overflow checking off}
const
MTWIST_N = 624;
MTWIST_M = 397;
MT_STATIC_SEED = 5489;
MTWIST_UPPER_MASK = cardinal($80000000);
MTWIST_LOWER_MASK = cardinal($7FFFFFFF);
MTWIST_MATRIX_A = cardinal($9908B0DF);
var
mt_state: array[0..MTWIST_N-1] of cardinal;
const
mt_index: cardinal = MTWIST_N+1;
function MTWIST_MIXBITS(u, v: cardinal): cardinal; inline;
begin
result:=(u and MTWIST_UPPER_MASK) or (v and MTWIST_LOWER_MASK);
end;
function MTWIST_TWIST(u, v: cardinal): cardinal; inline;
begin
{ the construct at the end is equivalent to
if odd(v) then
MTWIST_MATRIX_A
else
0
}
result:=(MTWIST_MIXBITS(u,v) shr 1) xor (cardinal(-(v and 1)) and MTWIST_MATRIX_A);
end;
procedure mtwist_init(seed: cardinal);
var
i: longint;
begin
mt_state[0]:=seed;
for i:=1 to MTWIST_N-1 do
mt_state[i]:=cardinal(1812433253) * (mt_state[i-1] xor (mt_state[i-1] shr 30)) + i;
{ still need to update the state }
mt_index:=MTWIST_N;
end;
procedure mtwist_update_state;
var
count: longint;
begin
{ The original C code uses pointers, doesn't play nice with JVM backend;
it counts from N-M+1 downto 0 (0 not included) for the first loop, which
should initialise the first N-M+1 elements -- doing so gives the wrong
results though (different from the old generator, and it also doesn't
match the algorithm description), so we use only N-M iterations. They don't
seem to test this one element and its value does not impact subsequent
numbers, so it's probably a bug in their implementation.
}
for count:=0 to MTWIST_N-MTWIST_M-1 do
mt_state[count]:=mt_state[count+MTWIST_M] xor MTWIST_TWIST(mt_state[count],mt_state[count+1]);
for count:=MTWIST_N-MTWIST_M to MTWIST_N-2 do
mt_state[count]:=mt_state[count+(MTWIST_M-MTWIST_N)] xor MTWIST_TWIST(mt_state[count],mt_state[count+1]);
mt_state[MTWIST_N-1]:=mt_state[MTWIST_M-1] xor MTWIST_TWIST(mt_state[MTWIST_N-1],mt_state[0]);
mt_index:=0;
end;
function mtwist_u32rand: cardinal;
var
l_index :cardinal;
begin
l_index:=mt_index;
inc(mt_index);
if (RandSeed<>OldRandSeed) or
(l_index>=MTWIST_N+1) then
begin
mtwist_init(RandSeed);
{ Detect resets of randseed
This will break if someone coincidentally uses not(randseed) as the
next randseed, but it's much more common that you will reset randseed
to the same value as before to regenerate the same sequence of numbers
}
RandSeed:=not(RandSeed);
OldRandSeed:=RandSeed;
l_index:=MTWIST_N;
end;
if l_index=MTWIST_N then
begin
mtwist_update_state;
l_index:=0;
mt_index:=1;
end;
result:=mt_state[l_index];
result:=result xor (result shr 11);
result:=result xor ((result shl 7) and cardinal($9D2C5680));
result:=result xor ((result shl 15) and cardinal($EFC60000));
result:=result xor (result shr 18);
end;
function random(l:longint): longint;
begin
{ otherwise we can return values = l (JM) }
if (l < 0) then
inc(l);
random := longint((int64(mtwist_u32rand)*l) shr 32);
end;
function random(l:int64): int64;
var
a, b, c, d: cardinal;
q, bd, ad, bc, ac: qword;
carry: qword;
begin
if (l < 0) then
begin
inc(l);
q:=qword(-l)
end
else
q:=qword(l);
a:=mtwist_u32rand;
b:=mtwist_u32rand;
c:=q shr 32;
d:=cardinal(q);
bd:=qword(b)*d;
ad:=qword(a)*d;
bc:=qword(b)*c;
ac:=qword(a)*c;
// We only need the carry bit
carry:=((bd shr 32)+cardinal(ad)+cardinal(bc)) shr 32;
// Calculate the final result
result:=int64(carry+(ad shr 32)+(bc shr 32)+ac);
if l<0 then
result:=-result;
end;
{$ifndef FPUNONE}
function random: extended;
begin
random := mtwist_u32rand * (extended(1.0)/(int64(1) shl 32));
end;
{$endif}
{$else FPC_USE_SIMPLE_RANDOM}
{ A simple implementation of random. TP/Delphi compatible. }
const
QRAN_A = 134775813;
QRAN_C = 1;
function rand_next: cardinal;
var
s: cardinal;
begin
s:=RandSeed*QRAN_A+QRAN_C;
RandSeed:=s;
rand_next:=s;
end;
function random(l: word): word;
var
s,ss: cardinal;
begin
s:=rand_next;
{ use 32-bit multiplications here }
ss:=(s shr 16)*l;
s:=(s and $FFFF)*l shr 16;
random:=(ss + s) shr 16;
end;
function random(l: longint): longint;
begin
random:=int64(rand_next)*l shr 32;
end;
function random(l:int64):int64;
begin
random:=random(longint(l));
end;
{$ifndef FPUNONE}
function random: extended;
const
c = 1.0/$10000/$10000;
begin
random:=rand_next*c;
end;
{$endif}
{$endif FPC_USE_SIMPLE_RANDOM}
{$endif FPC_HAS_FEATURE_RANDOM}
{****************************************************************************
Memory Management
****************************************************************************}
{$ifndef FPC_SYSTEM_HAS_PTR}
Function Ptr(sel,off : {$ifdef CPU16}Word{$else}Longint{$endif}) : farpointer;{$ifdef SYSTEMINLINE}inline;{$endif}
Begin
ptr:=farpointer((sel shl 4)+off);
End;
{$endif not FPC_SYSTEM_HAS_PTR}
{$ifndef FPC_SYSTEM_HAS_CSEG}
Function CSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
Begin
Cseg:=0;
End;
{$endif not FPC_SYSTEM_HAS_CSEG}
{$ifndef FPC_SYSTEM_HAS_DSEG}
Function DSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
Begin
Dseg:=0;
End;
{$endif not FPC_SYSTEM_HAS_DSEG}
{$ifndef FPC_SYSTEM_HAS_SSEG}
Function SSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
Begin
Sseg:=0;
End;
{$endif not FPC_SYSTEM_HAS_SSEG}
{$push}
{$R-}
{$I-}
{$Q-}
{*****************************************************************************
Miscellaneous
*****************************************************************************}
{$ifndef FPC_SYSTEM_HAS_STACKTOP}
function StackTop: pointer;
begin
{ Avoid wrap to zero on 32-bit }
if ptruint(StackBottom) > high(ptruint) - StackLength then
result:=pointer(ptruint(high(ptruint)))
else
result:=StackBottom + StackLength;
end;
{$endif FPC_SYSTEM_HAS_STACKTOP}
{$ifndef FPC_SYSTEM_HAS_GET_PC_ADDR}
{ This provides a dummy implementation
of get_pc_addr function, for CPU's that don't need
the instruction address to walk the stack. }
function get_pc_addr : codepointer;inline;
begin
get_pc_addr:=nil;
end;
{$endif ndef FPC_SYSTEM_HAS_GET_PC_ADDR}
{$ifndef FPC_SYSTEM_HAS_GET_CALLER_STACKINFO}
{ This provides a simple implementation
of get_caller_stackinfo procedure,
using get_caller_addr and get_caller_frame
functions. }
procedure get_caller_stackinfo(var framebp : pointer; var addr : codepointer);
var
nextbp : pointer;
nextaddr : codepointer;
begin
nextbp:=get_caller_frame(framebp,addr);
nextaddr:=get_caller_addr(framebp,addr);
framebp:=nextbp;
addr:=nextaddr;
end;
{$endif ndef FPC_SYSTEM_HAS_GET_CALLER_STACKINFO}
{$ifdef FPC_HAS_EXPLICIT_INTERLOCKED_POINTER}
{$ifndef FPC_SYSTEM_HAS_EXPLICIT_INTERLOCKED_POINTER}
function InterLockedDecrement (var Target: pointer) : pointer;
begin
Result := Pointer(InterLockedDecrement(PtrInt(Target)));
end;
function InterLockedIncrement (var Target: pointer) : pointer;
begin
Result := Pointer(InterLockedIncrement(PtrInt(Target)));
end;
function InterLockedExchange (var Target: pointer; Source : pointer) : pointer;
begin
Result := Pointer(InterLockedExchange(PtrInt(Target), PtrInt(Source)));
end;
function InterLockedExchangeAdd (var Target: pointer; Source : pointer) : pointer;
begin
Result := Pointer(InterLockedExchangeAdd(PtrInt(Target), PtrInt(Source)));
end;
function InterLockedCompareExchange (var Target: pointer; NewValue: pointer; Comperand: pointer): pointer;
begin
Result := Pointer(InterLockedCompareExchange(PtrInt(Target), PtrInt(NewValue), PtrInt(Comperand)));
end;
function InterLockedCompareExchangePointer (var Target: pointer; NewValue: pointer; Comperand: pointer): pointer;
begin
Result := Pointer(InterLockedCompareExchange(PtrInt(Target), PtrInt(NewValue), PtrInt(Comperand)));
end;
{$endif FPC_SYSTEM_HAS_EXPLICIT_INTERLOCKED_POINTER}
{$endif FPC_HAS_EXPLICIT_INTERLOCKED_POINTER}
procedure fpc_objecterror; compilerproc;
begin
HandleErrorAddrFrameInd(210,get_pc_addr,get_frame);
end;
procedure fpc_rangeerror;[public,alias:'FPC_RANGEERROR']; compilerproc;
begin
HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
end;
procedure fpc_divbyzero;[public,alias:'FPC_DIVBYZERO']; compilerproc;
begin
HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
end;
procedure fpc_overflow;[public,alias:'FPC_OVERFLOW']; compilerproc;
begin
HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
end;
procedure fpc_threaderror; [public,alias:'FPC_THREADERROR'];
begin
HandleErrorAddrFrameInd(236{RuntimeErrorExitCodes[reThreadError]},get_pc_addr,get_frame);
end;
procedure fpc_invalidpointer; [public,alias:'FPC_INVALIDPOINTER'];
begin
HandleErrorAddrFrameInd(216,get_pc_addr,get_frame);
end;
procedure fpc_iocheck;[public,alias:'FPC_IOCHECK']; compilerproc;
var
l : longint;
HInoutRes : PWord;
begin
HInOutRes:=@InoutRes;
if HInOutRes^<>0 then
begin
l:=HInOutRes^;
HInOutRes^:=0;
HandleErrorAddrFrameInd(l,get_pc_addr,get_frame);
end;
end;
Function IOResult:Word;
var
HInoutRes : PWord;
Begin
HInoutRes:=@InoutRes;
IOResult:=HInOutRes^;
HInOutRes^:=0;
End;
Function GetThreadID:TThreadID;{$ifdef SYSTEMINLINE}inline;{$endif}
begin
(* ThreadID is stored in a threadvar and made available in interface *)
(* to allow setup of this value during thread initialization. *)
GetThreadID := ThreadID;
end;
function fpc_safecallcheck(res : hresult) : hresult;[public,alias:'FPC_SAFECALLCHECK']; compilerproc; {$ifdef CPU86} register; {$endif}
begin
if res<0 then
begin
if assigned(SafeCallErrorProc) then
SafeCallErrorProc(res,get_frame);
HandleErrorAddrFrameInd(229,get_pc_addr,get_frame);
end;
result:=res;
end;
{*****************************************************************************
Stack check code
*****************************************************************************}
{ be compatible with old code }
{$ifdef FPC_NO_GENERIC_STACK_CHECK}
{$define NO_GENERIC_STACK_CHECK}
{$endif FPC_NO_GENERIC_STACK_CHECK}
{$IFNDEF NO_GENERIC_STACK_CHECK}
{$PUSH}
{$S-}
procedure fpc_stackcheck(stack_size:SizeUInt);[public,alias:'FPC_STACKCHECK']; compilerproc;
var
c : Pointer;
begin
{ Avoid recursive calls when called from the exit routines }
if StackError then
exit;
{ check stack alignment }
{$ifdef CPUI386}
{$ifdef FPC_STACKALIGNMENT}
{$if FPC_STACKALIGNMENT=16}
if ((PtrUInt(Sptr)+4) mod 16)<>0 then
begin
StackError:=true;
HandleError(202);
end;
{$endif FPC_STACKALIGNMENT=16}
{$endif FPC_STACKALIGNMENT}
{$endif CPUI386}
{ don't use stack_size, since the stack pointer has already been
decreased when this routine is called
}
c := Sptr - STACK_MARGIN;
if (c <= StackBottom) then
begin
StackError:=true;
HandleError(202);
end;
end;
{$POP}
{$ENDIF NO_GENERIC_STACK_CHECK}
{*****************************************************************************
Initialization / Finalization
*****************************************************************************}
const
maxunits=1024; { See also files.pas of the compiler source }
type
TInitFinalRec=record
InitProc,
FinalProc : TProcedure;
end;
TInitFinalTable = record
TableCount,
InitCount : {$if defined(VER2)}longint
{$elseif defined(VER3_0)}sizeint
{$else}ALUUInt{$endif}
;
Procs : array[1..maxunits] of TInitFinalRec;
end;
PInitFinalTable = ^TInitFinalTable;
{$ifndef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
var
InitFinalTable : TInitFinalTable;external name 'INITFINAL';
{$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; compilerproc;
var
i : ALUUInt;
pt : PInitFinalTable;
begin
{ call cpu/fpu initialisation routine }
fpc_cpuinit;
{$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
pt := PInitFinalTable(EntryInformation.InitFinalTable);
{$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
pt := @InitFinalTable;
{$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
with pt^ do
begin
for i:=1 to ALUUInt(TableCount) do
begin
if assigned(Procs[i].InitProc) then
Procs[i].InitProc();
InitCount:=i;
end;
end;
if assigned(InitProc) then
TProcedure(InitProc)();
end;
procedure internal_initializeunits; external name 'FPC_INITIALIZEUNITS';
procedure fpc_LibInitializeUnits;[public,alias:'FPC_LIBINITIALIZEUNITS'];compilerproc;
begin
{$ifdef FPC_HAS_FEATURE_DYNLIBS}
IsLibrary:=true;
{ must also be set to true for packages when implemented }
ModuleIsLib:=true;
internal_initializeunits;
{$endif FPC_HAS_FEATURE_DYNLIBS}
end;
{$ifdef FPC_INIT_FINAL_UNITS_BY_CALLS}
procedure FinalizeUnits; external name 'FPC_FINALIZE_FUNC_TABLE';
{$else FPC_INIT_FINAL_UNITS_BY_CALLS}
procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
begin
{$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
with PInitFinalTable(EntryInformation.InitFinalTable)^ do
{$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
with InitFinalTable do
{$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
begin
while (InitCount>0) do
begin
// we've to decrement the cound before calling the final. code
// else a halt in the final. code leads to a endless loop
dec(InitCount);
if assigned(Procs[InitCount+1].FinalProc) then
Procs[InitCount+1].FinalProc();
end;
end;
end;
{$endif FPC_INIT_FINAL_UNITS_BY_CALLS}
{*****************************************************************************
Error / Exit / ExitProc
*****************************************************************************}
Procedure system_exit;forward;{$ifdef FPC_SYSTEM_EXIT_NO_RETURN}noreturn;{$endif}
{$ifdef FPC_HAS_FEATURE_HEAP}
{$ifndef HAS_MEMORYMANAGER}
//not needed if independant memory manager
Procedure FinalizeHeap;forward;
{$endif HAS_MEMORYMANAGER}
{$endif FPC_HAS_FEATURE_HEAP}
{$ifdef FPC_HAS_FEATURE_CONSOLEIO}
procedure SysFlushStdIO;
begin
{ Make sure that all output is written to the redirected file }
if Textrec(Output).Mode=fmOutput then
Flush(Output);
if Textrec(ErrOutput).Mode=fmOutput then
Flush(ErrOutput);
{$ifndef FPC_STDOUT_TRUE_ALIAS}
if Textrec(stdout).Mode=fmOutput then
Flush(stdout);
if Textrec(StdErr).Mode=fmOutput then
Flush(StdErr);
{$endif FPC_STDOUT_TRUE_ALIAS}
end;
{$endif FPC_HAS_FEATURE_CONSOLEIO}
Procedure InternalExit;
var
current_exit : Procedure;
{$ifdef FPC_HAS_FEATURE_CONSOLEIO}
pstdout : ^Text;
{$endif}
{$if defined(MSWINDOWS) or defined(OS2)}
i : longint;
{$endif}
Begin
{$ifdef SYSTEMDEBUG}
writeln('InternalExit');
{$endif SYSTEMDEBUG}
{$ifndef CPUAVR}
while exitProc<>nil Do
Begin
InOutRes:=0;
current_exit:=tProcedure(exitProc);
exitProc:=nil;
current_exit();
End;
{$endif CPUAVR}
{$ifdef FPC_HAS_FEATURE_CONSOLEIO}
{ the embedded system unit itself contains no routines for console i/o
console i/o is done by the Consoleio unit which can do things like
redirection to seriell i/o }
{$ifndef EMBEDDED}
{ Show runtime error and exit }
if WriteErrorsToStdErr then
pstdout:=@stderr
else
pstdout:=@stdout;
If erroraddr<>nil Then
Begin
Writeln(pstdout^,'Runtime error ',Errorcode,' at $',hexstr(erroraddr));
{ to get a nice symify }
Writeln(pstdout^,BackTraceStrFunc(Erroraddr));
dump_stack(pstdout^,ErrorBase,ErrorAddr);
Writeln(pstdout^,'');
End;
SysFlushStdIO;
{$endif EMBEDDED}
{$endif FPC_HAS_FEATURE_CONSOLEIO}
{ Finalize units }
FinalizeUnits;
{$if defined(OS2)}
{ finally release the heap if possible, especially
important for DLLs.
Reset the array to nil, and finally also argv itself to
avoid double freeing problem in case this function gets called twice. }
if assigned(argv) then
begin
for i:=0 to argc-1 do
if assigned(argv[i]) then
begin
sysfreemem(argv[i]);
argv[i]:=nil;
end;
sysfreemem(argv);
argv:=nil;
end;
{$endif}
{$ifdef LINUX}
{sysfreemem already checks for nil}
{ Do not try to do anything if the heap manager already reported an error }
if (errorcode<>203) and (errorcode<>204) then
sysfreemem(calculated_cmdline);
{$endif}
{$ifdef BSD}
{ Do not try to do anything if the heap manager already reported an error }
if (errorcode<>203) and (errorcode<>204) then
sysfreemem(cmdline);
{$endif}
{$ifdef FPC_HAS_FEATURE_HEAP}
{$ifndef HAS_MEMORYMANAGER}
{$ifndef FPC_NO_DEFAULT_HEAP}
FinalizeHeap;
{$endif not FPC_NO_DEFAULT_HEAP}
{$endif not HAS_MEMORYMANAGER}
{$endif FPC_HAS_FEATURE_HEAP}
End;
Procedure fpc_do_exit;[Public,Alias:'FPC_DO_EXIT']; compilerproc;
begin
InternalExit;
System_exit;
end;
procedure internal_do_exit; external name 'FPC_DO_EXIT';
Procedure fpc_lib_exit;[Public,Alias:'FPC_LIB_EXIT'];
begin
InternalExit;
end;
Procedure Halt(ErrNum: TExitCode);noreturn;
Begin
{$ifdef FPC_HAS_FEATURE_EXITCODE}
{$ifdef FPC_LIMITED_EXITCODE}
if ErrNum > maxExitCode then
ExitCode:=255
else
{$endif FPC_LIMITED_EXITCODE}
ExitCode:=ErrNum;
{$endif FPC_HAS_FEATURE_EXITCODE}
internal_do_exit;
end;
function SysBackTraceStr (Addr: CodePointer): ShortString;
begin
SysBackTraceStr:=' $'+hexstr(addr);
end;
{$ifndef FPC_SYSTEM_HAS_CAPTUREBACKTRACE}
function CaptureBacktrace(skipframes,count:sizeint;frames:PCodePointer):sizeint;
var
curr_frame,prev_frame: pointer;
curr_addr: codepointer;
i: sizeint;
begin
curr_frame:=get_frame;
curr_addr:=get_pc_addr;
prev_frame:=curr_frame;
get_caller_stackinfo(curr_frame,curr_addr);
i:=-skipframes;
while (i<count) and (curr_frame>prev_frame) and
(curr_frame<StackTop) do
begin
if (curr_addr=nil) or
(curr_frame=nil) then
break;
if (i>=0) then
frames[i]:=curr_addr;
inc(i);
prev_frame:=curr_frame;
get_caller_stackinfo(curr_frame,curr_addr);
end;
if i<0 then
result:=0
else
result:=i;
end;
{$endif FPC_SYSTEM_HAS_CAPTUREBACKTRACE}
Procedure HandleErrorAddrFrame (Errno : TExitCode;addr : CodePointer; frame : Pointer);[public,alias:'FPC_BREAK_ERROR']; {$ifdef CPUI386} register; {$endif}
begin
If codepointer(ErrorProc)<>Nil then
ErrorProc(Errno,addr,frame);
errorcode:=word(Errno);
erroraddr:=addr;
errorbase:=frame;
{$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
if ExceptAddrStack <> nil then
raise TObject(nil) at addr,frame;
{$endif FPC_HAS_FEATURE_EXCEPTIONS}
Halt(errorcode);
end;
{ This is used internally by system skip first level,
and generated the same output as before, when
HandleErrorFrame function was used internally. }
Procedure HandleErrorAddrFrameInd (Errno : TExitCode;addr : CodePointer; frame : Pointer);
begin
get_caller_stackinfo (frame, addr);
HandleErrorAddrFrame (Errno,addr,frame);
end;
Procedure HandleErrorFrame (Errno : TExitCode;frame : Pointer);
{
Procedure to handle internal errors, i.e. not user-invoked errors
Internal function should ALWAYS call HandleError instead of RunError.
Can be used for exception handlers to specify the frame
}
begin
HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
end;
procedure fpc_handleerror (Errno : TExitCode); compilerproc; [public,alias : 'FPC_HANDLEERROR'];
{
Procedure to handle internal errors, i.e. not user-invoked errors
Internal function should ALWAYS call HandleError instead of RunError.
}
begin
HandleErrorAddrFrameInd(Errno,get_pc_addr,get_frame);
end;
procedure RunError(w : word);[alias: 'FPC_RUNERROR'];noreturn;
var
bp : pointer;
pcaddr : codepointer;
begin
errorcode:=w;
pcaddr:=get_pc_addr;
bp:=get_frame;
get_caller_stackinfo(bp,pcaddr);
erroraddr:=pcaddr;
errorbase:=bp;
Halt(errorcode);
end;
Procedure RunError;{$ifdef SYSTEMINLINE}inline;{$endif}noreturn;
Begin
RunError (0);
End;
Procedure Halt;{$ifdef SYSTEMINLINE}inline;{$endif}noreturn;
Begin
Halt(0);
End;
Procedure Error(RunTimeError : TRunTimeError);
begin
RunError(RuntimeErrorExitCodes[RunTimeError]);
end;
Procedure dump_stack(var f : text;fp : Pointer; addr : CodePointer);
var
i : ObjpasInt;
prevfp : Pointer;
is_dev : boolean;
Begin
{$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
try
{$endif FPC_HAS_FEATURE_EXCEPTIONS}
{ Frame of this procedure acts as StackBottom, fp values below that are invalid. }
prevfp:=get_frame;
i:=0;
is_dev:=do_isdevice(textrec(f).Handle);
{ sanity checks, new frame pointer must be always greater than the old one, further
it must point into the stack area, else something went wrong }
while (fp>prevfp) and (fp<StackTop) do
Begin
prevfp:=fp;
get_caller_stackinfo(fp,addr);
if (addr=nil) then
break;
Writeln(f,BackTraceStrFunc(addr));
if (fp=nil) then
break;
Inc(i);
If ((i>max_frame_dump) and is_dev) or (i>256) Then
break;
End;
{$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
except
{ prevent endless dump if an exception occurred }
end;
{$endif FPC_HAS_FEATURE_EXCEPTIONS}
End;
procedure dump_stack(var f: text; skipframes: longint);
var
i,count: ObjpasInt;
frames: array [0..255] of codepointer;
begin
if do_isdevice(textrec(f).handle) then
count:=max_frame_dump
else
count:=255;
{$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
try
{$endif FPC_HAS_FEATURE_EXCEPTIONS}
count:=CaptureBacktrace(skipframes+1,count,@frames[0]);
for i:=0 to count-1 do
writeln(f,BackTraceStrFunc(frames[i]));
{$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
except
end;
{$endif FPC_HAS_FEATURE_EXCEPTIONS}
end;
{$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
procedure DumpExceptionBackTrace(var f:text);
var
FrameNumber,
FrameCount : ObjpasInt;
Frames : PCodePointer;
begin
if RaiseList=nil then
exit;
WriteLn(f,BackTraceStrFunc(RaiseList^.Addr));
FrameCount:=RaiseList^.Framecount;
Frames:=RaiseList^.Frames;
for FrameNumber := 0 to FrameCount-1 do
WriteLn(f,BackTraceStrFunc(Frames[FrameNumber]));
end;
{$endif FPC_HAS_FEATURE_EXCEPTIONS}
{$ifdef FPC_HAS_FEATURE_HEAP}
Type
PExitProcInfo = ^TExitProcInfo;
TExitProcInfo = Record
Next : PExitProcInfo;
SaveExit : CodePointer;
Proc : TProcedure;
End;
const
ExitProcList: PExitProcInfo = nil;
Procedure DoExitProc;
var
P : PExitProcInfo;
Proc : TProcedure;
Begin
P:=ExitProcList;
ExitProcList:=P^.Next;
ExitProc:=P^.SaveExit;
Proc:=P^.Proc;
DisPose(P);
Proc();
End;
Procedure AddExitProc(Proc: TProcedure);
var
P : PExitProcInfo;
Begin
New(P);
P^.Next:=ExitProcList;
P^.SaveExit:=ExitProc;
P^.Proc:=Proc;
ExitProcList:=P;
ExitProc:=@DoExitProc;
End;
{$endif FPC_HAS_FEATURE_HEAP}
{$ifdef FPC_HAS_FEATURE_HEAP}
{$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):ppchar; // const ?
{$ifdef EXCLUDE_COMPLEX_PROCS}
begin
runerror(217);
end;
{$else EXCLUDE_COMPLEX_PROCS}
// Extra allocate reserveentries pchar's at the beginning (default param=0 after 1.0.x ?)
// Note: for internal use by skilled programmers only
// if "s" goes out of scope in the parent procedure, the pointer is dangling.
var p : ppchar;
i : ObjpasInt;
begin
if High(s)<Low(s) Then Exit(NIL);
Getmem(p,sizeof(pchar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
// for cmd
if p=nil then
begin
{$ifdef xunix}
fpseterrno(ESysEnomem);
{$endif}
exit(NIL);
end;
for i:=low(s) to high(s) do
p[i+Reserveentries]:=pchar(s[i]);
p[high(s)+1+Reserveentries]:=nil;
ArrayStringToPPchar:=p;
end;
{$endif EXCLUDE_COMPLEX_PROCS}
Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):ppchar;
{
Create a PPChar to structure of pchars which are the arguments specified
in the string S. Especially useful for creating an ArgV for Exec-calls
}
begin
StringToPPChar:=StringToPPChar(PChar(S),ReserveEntries);
end;
{$endif FPC_HAS_FEATURE_ANSISTRINGS}
Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
var
i,nr : ObjpasInt;
Buf : ^char;
p : ppchar;
begin
buf:=s;
nr:=1;
while (buf^<>#0) do // count nr of args
begin
while (buf^ in [' ',#9,#10]) do // Kill separators.
inc(buf);
inc(nr);
if buf^='"' Then // quotes argument?
begin
inc(buf);
while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
inc(buf);
if buf^='"' then // skip closing quote.
inc(buf);
end
else
begin // else std
while not (buf^ in [' ',#0,#9,#10]) do
inc(buf);
end;
end;
getmem(p,(ReserveEntries+nr)*sizeof(pchar));
StringToPPChar:=p;
if p=nil then
begin
{$ifdef xunix}
fpseterrno(ESysEnomem);
{$endif}
exit;
end;
for i:=1 to ReserveEntries do inc(p); // skip empty slots
buf:=s;
while (buf^<>#0) do
begin
while (buf^ in [' ',#9,#10]) do // Kill separators.
begin
buf^:=#0;
inc(buf);
end;
if buf^='"' Then // quotes argument?
begin
inc(buf);
p^:=buf;
inc(p);
p^:=nil;
while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
inc(buf);
if buf^='"' then // skip closing quote.
begin
buf^:=#0;
inc(buf);
end;
end
else
begin
p^:=buf;
inc(p);
p^:=nil;
while not (buf^ in [' ',#0,#9,#10]) do
inc(buf);
end;
end;
end;
{$endif FPC_HAS_FEATURE_HEAP}
{*****************************************************************************
Abstract/Assert support.
*****************************************************************************}
procedure fpc_emptymethod;[public,alias : 'FPC_EMPTYMETHOD'];
begin
end;
procedure fpc_AbstractErrorIntern;compilerproc;[public,alias : 'FPC_ABSTRACTERROR'];
begin
If codepointer(AbstractErrorProc)<>nil then
AbstractErrorProc();
HandleErrorAddrFrameInd(211,get_pc_addr,get_frame);
end;
Procedure fpc_assert(Const Msg,FName:Shortstring;LineNo:Longint;
ErrorAddr:Pointer); [Public,Alias : 'FPC_ASSERT']; compilerproc;
begin
if codepointer(AssertErrorProc)<>nil then
AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
else
HandleErrorAddrFrameInd(227,get_pc_addr,get_frame);
end;
Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
begin
{$ifdef FPC_HAS_FEATURE_CONSOLEIO}
If msg='' then
write(stderr,'Assertion failed')
else
write(stderr,msg);
Writeln(stderr,' (',FName,', line ',LineNo,').');
Writeln(stderr,'');
Halt(227);
{$endif FPC_HAS_FEATURE_CONSOLEIO}
end;
{*****************************************************************************
SetJmp/LongJmp support.
*****************************************************************************}
{$i setjump.inc}
{$pop} //{$I-,R-,Q-} before 'procedure fpc_rangeerror'
{*****************************************************************************
Heap
*****************************************************************************}
{$i sysheap.inc}
{$i heap.inc}
{*****************************************************************************
Thread support
*****************************************************************************}
{$ifdef FPC_HAS_FEATURE_THREADING}
{ Generic threadmanager }
{$i thread.inc}
{ Generic threadvar support }
{$i threadvr.inc}
{$ifdef DISABLE_NO_THREAD_MANAGER}
{ OS Dependent implementation }
{$i systhrd.inc}
{$endif DISABLE_NO_THREAD_MANAGER}
{$endif FPC_HAS_FEATURE_THREADING}
{*****************************************************************************
Dynamic library support
*****************************************************************************}
{$ifdef FPC_HAS_FEATURE_DYNLIBS}
{$i dynlib.inc}
{$ifdef DISABLE_NO_DYNLIBS_MANAGER}
{ OS Dependant implementation }
{$i sysdl.inc}
{$endif DISABLE_NO_DYNLIBS_MANAGER}
{$endif FPC_HAS_FEATURE_DYNLIBS}
{*****************************************************************************
File Handling
*****************************************************************************}
{$ifdef FPC_HAS_FEATURE_FILEIO}
{ Allow slash and backslash as separators }
procedure DoDirSeparators(var p: pchar; inplace: boolean = true);
var
i : ObjpasInt;
len : sizeint;
newp : pchar;
begin
len:=length(p);
newp:=nil;
for i:=0 to len do
if p[i] in AllowDirectorySeparators then
begin
if not inplace and
not assigned(newp) then
begin
getmem(newp,len+1);
move(p^,newp^,len+1);
p:=newp;
end;
p[i]:=DirectorySeparator;
end;
end;
procedure DoDirSeparators(var p: pwidechar; inplace: boolean = true);
var
i : ObjpasInt;
len : sizeint;
newp : pwidechar;
begin
len:=length(p);
newp:=nil;
for i:=0 to len do
if (ord(p[i])<255) and
(ansichar(ord(p[i])) in AllowDirectorySeparators) then
begin
if not inplace and
not assigned(newp) then
begin
getmem(newp,(len+1)*2);
move(p^,newp^,(len+1)*2);
p:=newp;
end;
p[i]:=DirectorySeparator;
end;
end;
procedure DoDirSeparators(var p:shortstring);
var
i : ObjpasInt;
begin
for i:=1 to length(p) do
if p[i] in AllowDirectorySeparators then
p[i]:=DirectorySeparator;
end;
{$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
procedure DoDirSeparators(var ps:RawByteString);
var
i : ObjpasInt;
p : pchar;
unique : boolean;
begin
unique:=false;
for i:=1 to length(ps) do
if ps[i] in AllowDirectorySeparators then
begin
if not unique then
begin
uniquestring(ps);
p:=pchar(ps);
unique:=true;
end;
p[i-1]:=DirectorySeparator;
end;
end;
{$endif FPC_HAS_FEATURE_ANSISTRINGS}
{$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
procedure DoDirSeparators(var ps:UnicodeString);
var
i : ObjpasInt;
p : pwidechar;
unique : boolean;
begin
unique:=false;
for i:=1 to length(ps) do
if ps[i] in AllowDirectorySeparators then
begin
if not unique then
begin
uniquestring(ps);
p:=pwidechar(ps);
unique:=true;
end;
p[i-1]:=DirectorySeparator;
end;
end;
{$endif FPC_HAS_FEATURE_UNICODESTRINGS}
{$endif FPC_HAS_FEATURE_FILEIO}
{ OS dependent low level file functions }
{$ifdef FPC_HAS_FEATURE_FILEIO}
{$i sysfile.inc}
{$ifndef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
{$ifdef FPC_ANSI_TEXTFILEREC}
procedure do_open(var f; p: pansichar; flags: longint; pchangeable: boolean);
var
u: UnicodeString;
begin
widestringmanager.Ansi2UnicodeMoveProc(p,DefaultFileSystemCodePage,u,length(p));
do_open(f,pwidechar(u),flags,true);
end;
procedure do_erase(p: pansichar; pchangeable: boolean);
var
u: UnicodeString;
begin
widestringmanager.Ansi2UnicodeMoveProc(p,DefaultFileSystemCodePage,u,length(p));
do_erase(pwidechar(u),true);
end;
procedure do_rename(src, dst: pansichar; srcchangeable, dstchangeable: boolean);
var
usrc, udst: UnicodeString;
begin
widestringmanager.Ansi2UnicodeMoveProc(src,DefaultFileSystemCodePage,usrc,length(src));
widestringmanager.Ansi2UnicodeMoveProc(dst,DefaultFileSystemCodePage,udst,length(dst));
do_rename(pwidechar(usrc),pwidechar(udst),true,true);
end;
procedure do_rename(src: pansichar; dst: pwidechar; srcchangeable, dstchangeable: boolean);
var
usrc: UnicodeString;
begin
widestringmanager.Ansi2UnicodeMoveProc(src,DefaultFileSystemCodePage,usrc,length(src));
do_rename(pwidechar(usrc),dst,true,dstchangeable);
end;
{$endif FPC_ANSI_TEXTFILEREC}
{$endif not FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
{$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
{$ifndef FPC_ANSI_TEXTFILEREC}
procedure do_open(var f; p: pwidechar; flags: longint; pchangeable: boolean);
var
s: RawByteString;
begin
widestringmanager.Unicode2AnsiMoveProc(p,s,DefaultFileSystemCodePage,length(p));
do_open(f,pansichar(s),flags,true);
end;
procedure do_erase(p: pwidechar; pchangeable: boolean);
var
s: RawByteString;
begin
widestringmanager.Unicode2AnsiMoveProc(p,s,DefaultFileSystemCodePage,length(p));
do_erase(pansichar(s),true);
end;
procedure do_rename(src, dst: pwidechar; srcchangeable, dstchangeable: boolean);
var
rsrc, rdst: RawByteString;
begin
widestringmanager.Unicode2AnsiMoveProc(src,rsrc,DefaultFileSystemCodePage,length(src));
widestringmanager.Unicode2AnsiMoveProc(dst,rdst,DefaultFileSystemCodePage,length(dst));
do_rename(pansichar(rsrc),pansichar(rdst),true,true);
end;
procedure do_rename(src: pwidechar; dst: pansichar; srcchangeable, dstchangeable: boolean);
var
rsrc: RawByteString;
begin
widestringmanager.Unicode2AnsiMoveProc(src,rsrc,DefaultFileSystemCodePage,length(src));
do_rename(pansichar(rsrc),dst,true,dstchangeable);
end;
{$endif not FPC_ANSI_TEXTFILEREC}
{$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}
{$endif FPC_HAS_FEATURE_FILEIO}
{ helper for targets supporting no ansistrings, it is used
by non-ansistring code }
function min(v1,v2 : SizeInt) : SizeInt;
begin
if v1<v2 then
result:=v1
else
result:=v2;
end;
{$ifdef FPC_HAS_FEATURE_TEXTIO}
{ Text file }
{$i text.inc}
{$endif FPC_HAS_FEATURE_TEXTIO}
{$ifdef FPC_HAS_FEATURE_FILEIO}
{ Untyped file }
{$i file.inc}
{ Typed file }
{$i typefile.inc}
{$endif FPC_HAS_FEATURE_FILEIO}
{*****************************************************************************
Directory Handling
*****************************************************************************}
{$ifdef FPC_HAS_FEATURE_FILEIO}
{ OS dependent dir functions }
{$i sysdir.inc}
{$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
{$ifndef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
procedure do_getdir(drivenr : byte;var dir : rawbytestring);
var
u: unicodestring;
begin
Do_getdir(drivenr,u);
widestringmanager.Unicode2AnsiMoveProc(pwidechar(u),dir,DefaultRTLFileSystemCodePage,length(u));
end;
{$endif FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
Procedure MkDir(Const s: RawByteString);[IOCheck];
Begin
If (s='') or (InOutRes <> 0) then
exit;
{$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
Do_mkdir(ToSingleByteFileSystemEncodedFileName(S));
{$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
Do_mkdir(S);
{$endif}
end;
Procedure RmDir(Const s: RawByteString);[IOCheck];
Begin
If (s='') or (InOutRes <> 0) then
exit;
{$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
Do_rmdir(ToSingleByteFileSystemEncodedFileName(S));
{$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
Do_rmdir(S);
{$endif}
End;
Procedure ChDir(Const s: RawByteString);[IOCheck];
Begin
If (s='') or (InOutRes <> 0) then
exit;
{$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
Do_chdir(ToSingleByteFileSystemEncodedFileName(S));
{$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
Do_chdir(S);
{$endif}
End;
Procedure getdir(drivenr:byte;Var dir:rawbytestring);
begin
Do_getdir(drivenr,dir);
{ we should return results in the DefaultRTLFileSystemCodePage -> convert if
necessary }
setcodepage(dir,DefaultRTLFileSystemCodePage,true);
end;
{ the generic shortstring ones are only implemented elsewhere for systems *not*
supporting ansi/unicodestrings; for now assume there are no systems that
support unicodestrings but not ansistrings }
{ avoid double string conversions }
{$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
function GetDirStrFromShortstring(const s: shortstring): RawByteString;
begin
GetDirStrFromShortstring:=ToSingleByteFileSystemEncodedFileName(ansistring(s));
end;
{$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
function GetDirStrFromShortstring(const s: shortstring): UnicodeString;
begin
GetDirStrFromShortstring:=s;
end;
{$endif FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
Procedure MkDir(Const s: shortstring);[IOCheck];
Begin
If (s='') or (InOutRes <> 0) then
exit;
Do_mkdir(GetDirStrFromShortstring(S));
End;
Procedure RmDir(Const s: shortstring);[IOCheck];
Begin
If (s='') or (InOutRes <> 0) then
exit;
Do_rmdir(GetDirStrFromShortstring(S));
End;
Procedure ChDir(Const s: shortstring);[IOCheck];
Begin
If (s='') or (InOutRes <> 0) then
exit;
Do_chdir(GetDirStrFromShortstring(S));
End;
Procedure getdir(drivenr:byte;Var dir:shortstring);
var
s: rawbytestring;
begin
Do_getdir(drivenr,s);
if length(s)<=high(dir) then
dir:=s
else
inoutres:=3;
end;
{$endif FPC_HAS_FEATURE_ANSISTRINGS}
{$if defined(FPC_HAS_FEATURE_WIDESTRINGS)}
{$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
{ overloads required for mkdir/rmdir/chdir to ensure that the string is
converted to the right code page }
procedure do_mkdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
begin
do_mkdir(ToSingleByteFileSystemEncodedFileName(s));
end;
procedure do_rmdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
begin
do_rmdir(ToSingleByteFileSystemEncodedFileName(s));
end;
procedure do_chdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
begin
do_chdir(ToSingleByteFileSystemEncodedFileName(s));
end;
procedure do_getdir(drivenr : byte;var dir : unicodestring);
var
s: rawbytestring;
begin
Do_getdir(drivenr,s);
dir:=unicodestring(s);
end;
{$endif FPCRTL_FILESYSTEM_TWO_BYTE_API}
Procedure MkDir(Const s: UnicodeString);[IOCheck];
Begin
if (s='') or (InOutRes <> 0) then
exit;
Do_mkdir(S);
End;
Procedure RmDir(Const s: UnicodeString);[IOCheck];
Begin
if (s='') or (InOutRes <> 0) then
exit;
Do_rmdir(S);
End;
Procedure ChDir(Const s: UnicodeString);[IOCheck];
Begin
if (s='') or (InOutRes <> 0) then
exit;
Do_chdir(S);
End;
Procedure getdir(drivenr:byte;Var dir:unicodestring);
begin
Do_getdir(drivenr,dir);
end;
{$endif FPC_HAS_FEATURE_WIDESTRINGS}
{$endif FPC_HAS_FEATURE_FILEIO}
{*****************************************************************************
Resources support
*****************************************************************************}
{$i sysres.inc}
const
CtrlBreakHandler: TCtrlBreakHandler = nil;
{$IFNDEF FPC_HAS_SETCTRLBREAKHANDLER}
(* It is possible to provide platform specific implementation performing *)
(* special initialization; default implementation just sets the procedural *)
(* variable to make it available for use from the exception handler. *)
function SysSetCtrlBreakHandler (Handler: TCtrlBreakHandler): TCtrlBreakHandler;
begin
(* Return either nil or previous handler *)
SysSetCtrlBreakHandler := CtrlBreakHandler;
CtrlBreakHandler := Handler;
end;
{$ENDIF FPC_HAS_SETCTRLBREAKHANDLER}
|