system.inc 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. {
  2. This file is part of the Free Pascal Run time library.
  3. Copyright (c) 1999-2008 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. For details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. { The RTTI is implemented through a series of constants : }
  11. Const
  12. // please update tkManagedTypes below if you add new
  13. // values
  14. tkUnknown = 0;
  15. tkInteger = 1;
  16. tkChar = 2;
  17. tkEnumeration = 3;
  18. tkFloat = 4;
  19. tkSet = 5;
  20. tkMethod = 6;
  21. tkSString = 7;
  22. tkString = tkSString;
  23. tkLString = 8;
  24. tkAString = 9;
  25. tkWString = 10;
  26. tkVariant = 11;
  27. tkArray = 12;
  28. tkRecord = 13;
  29. tkInterface = 14;
  30. tkClass = 15;
  31. tkObject = 16;
  32. tkWChar = 17;
  33. tkBool = 18;
  34. tkInt64 = 19;
  35. tkQWord = 20;
  36. tkDynArray = 21;
  37. tkInterfaceCorba = 22;
  38. tkProcVar = 23;
  39. tkUString = 24;
  40. // all potentially managed types
  41. tkManagedTypes = [tkAstring,tkWstring,tkUstring,tkArray,
  42. tkObject,tkRecord,tkDynArray,tkInterface,tkVariant];
  43. {****************************************************************************
  44. Local types
  45. ****************************************************************************}
  46. {
  47. TextRec and FileRec are put in a separate file to make it available to other
  48. units without putting it explicitly in systemh.
  49. This way we keep TP compatibility, and the TextRec definition is available
  50. for everyone who needs it.
  51. }
  52. {$ifdef FPC_HAS_FEATURE_FILEIO}
  53. {$i filerec.inc}
  54. {$endif FPC_HAS_FEATURE_FILEIO}
  55. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  56. {$i textrec.inc}
  57. {$endif FPC_HAS_FEATURE_TEXTIO}
  58. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  59. {$ifdef FPC_OBJFPC_EXTENDED_IF}
  60. {$if High(errorcode)<>maxExitCode}
  61. {$define FPC_LIMITED_EXITCODE}
  62. {$endif}
  63. {$else}
  64. {$define FPC_LIMITED_EXITCODE}
  65. {$endif FPC_OBJFPC_EXTENDED_IF}
  66. {$endif FPC_HAS_FEATURE_EXITCODE}
  67. Procedure HandleError (Errno : Longint); forward;
  68. Procedure HandleErrorFrame (Errno : longint;frame : Pointer); forward;
  69. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  70. type
  71. FileFunc = Procedure(var t : TextRec);
  72. {$endif FPC_HAS_FEATURE_TEXTIO}
  73. const
  74. STACK_MARGIN = 16384; { Stack size margin for stack checking }
  75. { Random / Randomize constants }
  76. OldRandSeed : Cardinal = 0;
  77. { For Error Handling.}
  78. ErrorBase : Pointer = nil;
  79. { Used by the ansi/widestrings and maybe also other things in the future }
  80. var
  81. { widechar, because also used by widestring -> pwidechar conversions }
  82. emptychar : widechar;public name 'FPC_EMPTYCHAR';
  83. {$ifndef FPC_NO_GENERIC_STACK_CHECK}
  84. { if the OS does the stack checking, we don't need any stklen from the
  85. main program }
  86. initialstklen : SizeUint;external name '__stklen';
  87. {$endif FPC_NO_GENERIC_STACK_CHECK}
  88. { checks whether the given suggested size for the stack of the current
  89. thread is acceptable. If this is the case, returns it unaltered.
  90. Otherwise it should return an acceptable value.
  91. Operating systems that automatically expand their stack on demand, should
  92. simply return a very large value.
  93. Operating systems which do not have a possibility to retrieve stack size
  94. information, should simply return the given stklen value (This is the default
  95. implementation).
  96. }
  97. {$ifdef FPC_HAS_FEATURE_STACKCHECK}
  98. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt; forward;
  99. {$endif FPC_HAS_FEATURE_STACKCHECK}
  100. {*****************************************************************************
  101. OS dependent Helpers/Syscalls
  102. *****************************************************************************}
  103. { for some OSes do_isdevice is defined in sysos.inc, but for others (win32)
  104. it isn't, and is used before the actual definition is encountered }
  105. {$ifdef FPC_HAS_FEATURE_FILEIO}
  106. function do_isdevice(handle:thandle):boolean;forward;
  107. {$endif FPC_HAS_FEATURE_FILEIO}
  108. {$i sysos.inc}
  109. {****************************************************************************
  110. Include processor specific routines
  111. ****************************************************************************}
  112. {$ifdef FPC_USE_LIBC}
  113. { Under Haiku, bcopy cause a problem when searching for include file
  114. in the compiler. So, we use the internal implementation for now
  115. under BeOS and Haiku. }
  116. {$ifndef BEOS}
  117. { prefer libc implementations over our own, as they're most likely faster }
  118. {$i cgeneric.inc}
  119. { is now declared as external reference to another routine in the interface }
  120. {$i cgenstr.inc}
  121. {$endif}
  122. {$endif FPC_USE_LIBC}
  123. {$ifdef cpui386}
  124. {$ifdef SYSPROCDEFINED}
  125. {$Error Can't determine processor type !}
  126. {$endif}
  127. {$i i386.inc} { Case dependent, don't change }
  128. {$endif cpui386}
  129. {$ifdef cpum68k}
  130. {$ifdef SYSPROCDEFINED}
  131. {$Error Can't determine processor type !}
  132. {$endif}
  133. {$i m68k.inc} { Case dependent, don't change }
  134. {$define SYSPROCDEFINED}
  135. {$endif cpum68k}
  136. {$ifdef cpux86_64}
  137. {$ifdef SYSPROCDEFINED}
  138. {$Error Can't determine processor type !}
  139. {$endif}
  140. {$i x86_64.inc} { Case dependent, don't change }
  141. {$define SYSPROCDEFINED}
  142. {$endif cpux86_64}
  143. {$ifdef cpupowerpc32}
  144. {$ifdef SYSPROCDEFINED}
  145. {$Error Can't determine processor type !}
  146. {$endif}
  147. {$i powerpc.inc} { Case dependent, don't change }
  148. {$define SYSPROCDEFINED}
  149. {$endif cpupowerpc32}
  150. {$ifdef cpupowerpc64}
  151. {$ifdef SYSPROCDEFINED}
  152. {$Error Can't determine processor type !}
  153. {$endif}
  154. {$i powerpc64.inc} { Case dependent, don't change }
  155. {$define SYSPROCDEFINED}
  156. {$endif cpupowerpc64}
  157. {$ifdef cpualpha}
  158. {$ifdef SYSPROCDEFINED}
  159. {$Error Can't determine processor type !}
  160. {$endif}
  161. {$i alpha.inc} { Case dependent, don't change }
  162. {$define SYSPROCDEFINED}
  163. {$endif cpualpha}
  164. {$ifdef cpuiA64}
  165. {$ifdef SYSPROCDEFINED}
  166. {$Error Can't determine processor type !}
  167. {$endif}
  168. {$i ia64.inc} { Case dependent, don't change }
  169. {$define SYSPROCDEFINED}
  170. {$endif cpuiA64}
  171. {$ifdef cpusparc}
  172. {$ifdef SYSPROCDEFINED}
  173. {$Error Can't determine processor type !}
  174. {$endif}
  175. {$i sparc.inc} { Case dependent, don't change }
  176. {$define SYSPROCDEFINED}
  177. {$endif cpusparc}
  178. {$ifdef cpuarm}
  179. {$ifdef SYSPROCDEFINED}
  180. {$Error Can't determine processor type !}
  181. {$endif}
  182. {$if defined(CPUCORTEXM3) or defined(CPUARMV7M)}
  183. {$i thumb2.inc} { Case dependent, don't change }
  184. {$else}
  185. {$i arm.inc} { Case dependent, don't change }
  186. {$endif}
  187. {$define SYSPROCDEFINED}
  188. {$endif cpuarm}
  189. {$ifdef cpuavr}
  190. {$ifdef SYSPROCDEFINED}
  191. {$Error Can't determine processor type !}
  192. {$endif}
  193. {$i avr.inc} { Case dependent, don't change }
  194. {$define SYSPROCDEFINED}
  195. {$endif cpuavr}
  196. procedure fillchar(var x;count : SizeInt;value : boolean);
  197. begin
  198. fillchar(x,count,byte(value));
  199. end;
  200. procedure fillchar(var x;count : SizeInt;value : char);
  201. begin
  202. fillchar(x,count,byte(value));
  203. end;
  204. procedure FillByte (var x;count : SizeInt;value : byte );
  205. begin
  206. FillChar (X,Count,VALUE);
  207. end;
  208. function IndexChar(Const buf;len:SizeInt;b:char):SizeInt;
  209. begin
  210. IndexChar:=IndexByte(Buf,Len,byte(B));
  211. end;
  212. function CompareChar(Const buf1,buf2;len:SizeInt):SizeInt;
  213. begin
  214. CompareChar:=CompareByte(buf1,buf2,len);
  215. end;
  216. { Include generic pascal only routines which are not defined in the processor
  217. specific include file }
  218. {$I generic.inc}
  219. {****************************************************************************
  220. Set Handling
  221. ****************************************************************************}
  222. { Include set support which is processor specific}
  223. {$i set.inc}
  224. { Include generic pascal routines for sets if the processor }
  225. { specific routines are not available. }
  226. {$i genset.inc}
  227. {****************************************************************************
  228. Math Routines
  229. ****************************************************************************}
  230. function Hi(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  231. begin
  232. Hi := b shr 4
  233. end;
  234. function Lo(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  235. begin
  236. Lo := b and $0f
  237. end;
  238. Function Swap (X : Word) : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  239. Begin
  240. Swap := SwapEndian(X);
  241. End;
  242. Function Swap (X : Integer) : Integer;{$ifdef SYSTEMINLINE}inline;{$endif}
  243. Begin
  244. Swap := SwapEndian(X);
  245. End;
  246. Function Swap (X : Longint) : Longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  247. Begin
  248. Swap:=(X and $ffff) shl 16 + (X shr 16)
  249. End;
  250. Function Swap (X : Cardinal) : Cardinal;{$ifdef SYSTEMINLINE}inline;{$endif}
  251. Begin
  252. Swap:=(X and $ffff) shl 16 + (X shr 16)
  253. End;
  254. Function Swap (X : QWord) : QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  255. Begin
  256. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  257. End;
  258. Function swap (X : Int64) : Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  259. Begin
  260. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  261. End;
  262. {$ifdef SUPPORT_DOUBLE}
  263. operator := (b:real48) d:double;{$ifdef SYSTEMINLINE}inline;{$endif}
  264. begin
  265. D:=real2double(b);
  266. end;
  267. {$endif SUPPORT_DOUBLE}
  268. {$ifdef SUPPORT_EXTENDED}
  269. operator := (b:real48) e:extended;{$ifdef SYSTEMINLINE}inline;{$endif}
  270. begin
  271. e:=real2double(b);
  272. end;
  273. {$endif SUPPORT_EXTENDED}
  274. {$ifndef FPUNONE}
  275. {$ifdef FPC_USE_LIBC}
  276. { Include libc versions }
  277. {$i cgenmath.inc}
  278. {$endif FPC_USE_LIBC}
  279. { Include processor specific routines }
  280. {$I math.inc}
  281. { Include generic version }
  282. {$I genmath.inc}
  283. {$endif}
  284. {$i gencurr.inc}
  285. function aligntoptr(p : pointer) : pointer;inline;
  286. begin
  287. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  288. result:=align(p,sizeof(p));
  289. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  290. result:=p;
  291. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  292. end;
  293. {****************************************************************************
  294. Subroutines for String handling
  295. ****************************************************************************}
  296. { Needs to be before RTTI handling }
  297. {$i sstrings.inc}
  298. { requires sstrings.inc for initval }
  299. {$I int64p.inc}
  300. {$I int64.inc}
  301. {Requires int64.inc, since that contains the VAL functions for int64 and qword}
  302. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  303. {$i astrings.inc}
  304. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  305. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  306. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  307. {$i wstrings.inc}
  308. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  309. {$i ustrings.inc}
  310. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  311. {$i aliases.inc}
  312. {*****************************************************************************
  313. Dynamic Array support
  314. *****************************************************************************}
  315. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  316. {$i dynarr.inc}
  317. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  318. {*****************************************************************************
  319. Object Pascal support
  320. *****************************************************************************}
  321. {$ifdef FPC_HAS_FEATURE_CLASSES}
  322. {$i objpas.inc}
  323. {$endif FPC_HAS_FEATURE_CLASSES}
  324. {*****************************************************************************
  325. Variant support
  326. *****************************************************************************}
  327. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  328. {$i variant.inc}
  329. {$endif FPC_HAS_FEATURE_VARIANTS}
  330. {****************************************************************************
  331. Run-Time Type Information (RTTI)
  332. ****************************************************************************}
  333. {$ifdef FPC_HAS_FEATURE_RTTI}
  334. {$i rtti.inc}
  335. {$endif FPC_HAS_FEATURE_RTTI}
  336. {$if defined(FPC_HAS_FEATURE_RANDOM)}
  337. {----------------------------------------------------------------------
  338. Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  339. Pseudo-Random Number Generator.
  340. What is Mersenne Twister?
  341. Mersenne Twister(MT) is a pseudorandom number generator developped by
  342. Makoto Matsumoto and Takuji Nishimura (alphabetical order) during
  343. 1996-1997. MT has the following merits:
  344. It is designed with consideration on the flaws of various existing
  345. generators.
  346. Far longer period and far higher order of equidistribution than any
  347. other implemented generators. (It is proved that the period is 2^19937-1,
  348. and 623-dimensional equidistribution property is assured.)
  349. Fast generation. (Although it depends on the system, it is reported that
  350. MT is sometimes faster than the standard ANSI-C library in a system
  351. with pipeline and cache memory.)
  352. Efficient use of the memory. (The implemented C-code mt19937.c
  353. consumes only 624 words of working area.)
  354. home page
  355. http://www.math.keio.ac.jp/~matumoto/emt.html
  356. original c source
  357. http://www.math.keio.ac.jp/~nisimura/random/int/mt19937int.c
  358. Coded by Takuji Nishimura, considering the suggestions by
  359. Topher Cooper and Marc Rieffel in July-Aug. 1997.
  360. This library is free software; you can redistribute it and/or
  361. modify it under the terms of the GNU Library General Public
  362. License as published by the Free Software Foundation; either
  363. version 2 of the License, or (at your option) any later
  364. version.
  365. This library is distributed in the hope that it will be useful,
  366. but WITHOUT ANY WARRANTY; without even the implied warranty of
  367. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  368. See the GNU Library General Public License for more details.
  369. You should have received a copy of the GNU Library General
  370. Public License along with this library; if not, write to the
  371. Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  372. 02111-1307 USA
  373. Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura.
  374. When you use this, send an email to: [email protected]
  375. with an appropriate reference to your work.
  376. REFERENCE
  377. M. Matsumoto and T. Nishimura,
  378. "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  379. Pseudo-Random Number Generator",
  380. ACM Transactions on Modeling and Computer Simulation,
  381. Vol. 8, No. 1, January 1998, pp 3--30.
  382. Translated to OP and Delphi interface added by Roman Krejci (6.12.1999)
  383. http://www.rksolution.cz/delphi/tips.htm
  384. Revised 21.6.2000: Bug in the function RandInt_MT19937 fixed
  385. 2003/10/26: adapted to use the improved intialisation mentioned at
  386. <http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html> and
  387. removed the assembler code
  388. ----------------------------------------------------------------------}
  389. {$R-} {range checking off}
  390. {$Q-} {overflow checking off}
  391. { Period parameter }
  392. Const
  393. MT19937N=624;
  394. Type
  395. tMT19937StateArray = array [0..MT19937N-1] of longint; // the array for the state vector
  396. { Period parameters }
  397. const
  398. MT19937M=397;
  399. MT19937MATRIX_A =$9908b0df; // constant vector a
  400. MT19937UPPER_MASK=longint($80000000); // most significant w-r bits
  401. MT19937LOWER_MASK=longint($7fffffff); // least significant r bits
  402. { Tempering parameters }
  403. TEMPERING_MASK_B=longint($9d2c5680);
  404. TEMPERING_MASK_C=longint($efc60000);
  405. VAR
  406. mt : tMT19937StateArray;
  407. const
  408. mti: longint=MT19937N+1; // mti=MT19937N+1 means mt[] is not initialized
  409. { Initializing the array with a seed }
  410. procedure sgenrand_MT19937(seed: longint);
  411. var
  412. i: longint;
  413. begin
  414. mt[0] := seed;
  415. for i := 1 to MT19937N-1 do
  416. begin
  417. mt[i] := 1812433253 * (mt[i-1] xor (mt[i-1] shr 30)) + i;
  418. { See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. }
  419. { In the previous versions, MSBs of the seed affect }
  420. { only MSBs of the array mt[]. }
  421. { 2002/01/09 modified by Makoto Matsumoto }
  422. end;
  423. mti := MT19937N;
  424. end;
  425. function genrand_MT19937: longint;
  426. const
  427. mag01 : array [0..1] of longint =(0, longint(MT19937MATRIX_A));
  428. var
  429. y: longint;
  430. kk: longint;
  431. begin
  432. if RandSeed<>OldRandSeed then
  433. mti:=MT19937N+1;
  434. if (mti >= MT19937N) { generate MT19937N longints at one time }
  435. then begin
  436. if mti = (MT19937N+1) then // if sgenrand_MT19937() has not been called,
  437. begin
  438. sgenrand_MT19937(randseed); // default initial seed is used
  439. { hack: randseed is not used more than once in this algorithm. Most }
  440. { user changes are re-initialising reandseed with the value it had }
  441. { at the start -> with the "not", we will detect this change. }
  442. { Detecting other changes is not useful, since the generated }
  443. { numbers will be different anyway. }
  444. randseed := not(randseed);
  445. oldrandseed := randseed;
  446. end;
  447. for kk:=0 to MT19937N-MT19937M-1 do begin
  448. y := (mt[kk] and MT19937UPPER_MASK) or (mt[kk+1] and MT19937LOWER_MASK);
  449. mt[kk] := mt[kk+MT19937M] xor (y shr 1) xor mag01[y and $00000001];
  450. end;
  451. for kk:= MT19937N-MT19937M to MT19937N-2 do begin
  452. y := (mt[kk] and MT19937UPPER_MASK) or (mt[kk+1] and MT19937LOWER_MASK);
  453. mt[kk] := mt[kk+(MT19937M-MT19937N)] xor (y shr 1) xor mag01[y and $00000001];
  454. end;
  455. y := (mt[MT19937N-1] and MT19937UPPER_MASK) or (mt[0] and MT19937LOWER_MASK);
  456. mt[MT19937N-1] := mt[MT19937M-1] xor (y shr 1) xor mag01[y and $00000001];
  457. mti := 0;
  458. end;
  459. y := mt[mti]; inc(mti);
  460. y := y xor (y shr 11);
  461. y := y xor (y shl 7) and TEMPERING_MASK_B;
  462. y := y xor (y shl 15) and TEMPERING_MASK_C;
  463. y := y xor (y shr 18);
  464. Result := y;
  465. end;
  466. function random(l:longint): longint;
  467. begin
  468. { otherwise we can return values = l (JM) }
  469. if (l < 0) then
  470. inc(l);
  471. random := longint((int64(cardinal(genrand_MT19937))*l) shr 32);
  472. end;
  473. function random(l:int64): int64;
  474. begin
  475. { always call random, so the random generator cycles (TP-compatible) (JM) }
  476. random := int64((qword(cardinal(genrand_MT19937)) or ((qword(cardinal(genrand_MT19937)) shl 32))) and $7fffffffffffffff);
  477. if (l<>0) then
  478. random := random mod l
  479. else
  480. random := 0;
  481. end;
  482. {$ifndef FPUNONE}
  483. function random: extended;
  484. begin
  485. random := cardinal(genrand_MT19937) * (extended(1.0)/(int64(1) shl 32));
  486. end;
  487. {$endif}
  488. {$endif FPC_HAS_FEATURE_RANDOM}
  489. {****************************************************************************
  490. Memory Management
  491. ****************************************************************************}
  492. Function Ptr(sel,off : Longint) : farpointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  493. Begin
  494. ptr:=farpointer((sel shl 4)+off);
  495. End;
  496. Function CSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  497. Begin
  498. Cseg:=0;
  499. End;
  500. Function DSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  501. Begin
  502. Dseg:=0;
  503. End;
  504. Function SSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  505. Begin
  506. Sseg:=0;
  507. End;
  508. {$ifopt R+}
  509. {$define RangeCheckWasOn}
  510. {$R-}
  511. {$endif opt R+}
  512. {$ifopt I+}
  513. {$define IOCheckWasOn}
  514. {$I-}
  515. {$endif opt I+}
  516. {$ifopt Q+}
  517. {$define OverflowCheckWasOn}
  518. {$Q-}
  519. {$endif opt Q+}
  520. {*****************************************************************************
  521. Miscellaneous
  522. *****************************************************************************}
  523. procedure fpc_rangeerror;[public,alias:'FPC_RANGEERROR']; compilerproc;
  524. begin
  525. HandleErrorFrame(201,get_frame);
  526. end;
  527. procedure fpc_divbyzero;[public,alias:'FPC_DIVBYZERO']; compilerproc;
  528. begin
  529. HandleErrorFrame(200,get_frame);
  530. end;
  531. procedure fpc_overflow;[public,alias:'FPC_OVERFLOW']; compilerproc;
  532. begin
  533. HandleErrorFrame(215,get_frame);
  534. end;
  535. procedure fpc_threaderror; [public,alias:'FPC_THREADERROR'];
  536. begin
  537. HandleErrorFrame(6,get_frame);
  538. end;
  539. procedure fpc_iocheck;[public,alias:'FPC_IOCHECK']; compilerproc;
  540. var
  541. l : longint;
  542. HInoutRes : PWord;
  543. begin
  544. HInOutRes:=@InoutRes;
  545. if HInOutRes^<>0 then
  546. begin
  547. l:=HInOutRes^;
  548. HInOutRes^:=0;
  549. HandleErrorFrame(l,get_frame);
  550. end;
  551. end;
  552. Function IOResult:Word;
  553. var
  554. HInoutRes : PWord;
  555. Begin
  556. HInoutRes:=@InoutRes;
  557. IOResult:=HInOutRes^;
  558. HInOutRes^:=0;
  559. End;
  560. Function GetThreadID:TThreadID;{$ifdef SYSTEMINLINE}inline;{$endif}
  561. begin
  562. (* ThreadID is stored in a threadvar and made available in interface *)
  563. (* to allow setup of this value during thread initialization. *)
  564. GetThreadID := ThreadID;
  565. end;
  566. function fpc_safecallcheck(res : hresult) : hresult;[public,alias:'FPC_SAFECALLCHECK']; compilerproc; {$ifdef CPU86} register; {$endif}
  567. begin
  568. if res<0 then
  569. begin
  570. if assigned(SafeCallErrorProc) then
  571. SafeCallErrorProc(res,get_frame);
  572. HandleErrorFrame(229,get_frame);
  573. end;
  574. result:=res;
  575. end;
  576. {*****************************************************************************
  577. Stack check code
  578. *****************************************************************************}
  579. { be compatible with old code }
  580. {$ifdef FPC_NO_GENERIC_STACK_CHECK}
  581. {$define NO_GENERIC_STACK_CHECK}
  582. {$endif FPC_NO_GENERIC_STACK_CHECK}
  583. {$IFNDEF NO_GENERIC_STACK_CHECK}
  584. {$IFOPT S+}
  585. {$DEFINE STACKCHECK}
  586. {$ENDIF}
  587. {$S-}
  588. procedure fpc_stackcheck(stack_size:SizeUInt);[public,alias:'FPC_STACKCHECK'];
  589. var
  590. c : Pointer;
  591. begin
  592. { Avoid recursive calls when called from the exit routines }
  593. if StackError then
  594. exit;
  595. { don't use sack_size, since the stack pointer has already been
  596. decreased when this routine is called
  597. }
  598. c := Sptr - STACK_MARGIN;
  599. if (c <= StackBottom) then
  600. begin
  601. StackError:=true;
  602. HandleError(202);
  603. end;
  604. end;
  605. {$IFDEF STACKCHECK}
  606. {$S+}
  607. {$ENDIF}
  608. {$UNDEF STACKCHECK}
  609. {$ENDIF NO_GENERIC_STACK_CHECK}
  610. {*****************************************************************************
  611. Initialization / Finalization
  612. *****************************************************************************}
  613. const
  614. maxunits=1024; { See also files.pas of the compiler source }
  615. type
  616. TInitFinalRec=record
  617. InitProc,
  618. FinalProc : TProcedure;
  619. end;
  620. TInitFinalTable = record
  621. TableCount,
  622. InitCount : longint;
  623. Procs : array[1..maxunits] of TInitFinalRec;
  624. end;
  625. PInitFinalTable = ^TInitFinalTable;
  626. {$ifndef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  627. var
  628. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  629. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  630. procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; compilerproc;
  631. var
  632. i : longint;
  633. begin
  634. { call cpu/fpu initialisation routine }
  635. fpc_cpuinit;
  636. with {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}PInitFinalTable(EntryInformation.{$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  637. InitFinalTable
  638. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION})^{$endif FPC_HAS_INDIRECT_MAIN_INFORMATION} do
  639. begin
  640. for i:=1 to TableCount do
  641. begin
  642. if assigned(Procs[i].InitProc) then
  643. Procs[i].InitProc();
  644. InitCount:=i;
  645. end;
  646. end;
  647. if assigned(InitProc) then
  648. TProcedure(InitProc)();
  649. end;
  650. procedure internal_initializeunits; external name 'FPC_INITIALIZEUNITS';
  651. procedure fpc_LibInitializeUnits;[public,alias:'FPC_LIBINITIALIZEUNITS'];
  652. begin
  653. IsLibrary:=true;
  654. { must also be set to true for packages when implemented }
  655. ModuleIsLib:=true;
  656. internal_initializeunits;
  657. end;
  658. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  659. begin
  660. with {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}PInitFinalTable(EntryInformation.{$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  661. InitFinalTable
  662. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION})^{$endif FPC_HAS_INDIRECT_MAIN_INFORMATION} do
  663. begin
  664. while (InitCount>0) do
  665. begin
  666. // we've to decrement the cound before calling the final. code
  667. // else a halt in the final. code leads to a endless loop
  668. dec(InitCount);
  669. if assigned(Procs[InitCount+1].FinalProc) then
  670. Procs[InitCount+1].FinalProc();
  671. end;
  672. end;
  673. end;
  674. {*****************************************************************************
  675. Error / Exit / ExitProc
  676. *****************************************************************************}
  677. Procedure system_exit;forward;
  678. {$ifdef FPC_HAS_FEATURE_HEAP}
  679. {$ifndef HAS_MEMORYMANAGER}
  680. //not needed if independant memory manager
  681. Procedure FinalizeHeap;forward;
  682. {$endif HAS_MEMORYMANAGER}
  683. {$endif FPC_HAS_FEATURE_HEAP}
  684. Procedure InternalExit;
  685. var
  686. current_exit : Procedure;
  687. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  688. pstdout : ^Text;
  689. {$endif}
  690. {$if defined(MSWINDOWS) or defined(OS2)}
  691. i : longint;
  692. {$endif}
  693. Begin
  694. {$ifdef SYSTEMDEBUG}
  695. writeln('InternalExit');
  696. {$endif SYSTEMDEBUG}
  697. while exitProc<>nil Do
  698. Begin
  699. InOutRes:=0;
  700. current_exit:=tProcedure(exitProc);
  701. exitProc:=nil;
  702. current_exit();
  703. End;
  704. { Finalize units }
  705. FinalizeUnits;
  706. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  707. { Show runtime error and exit }
  708. pstdout:=@stdout;
  709. If erroraddr<>nil Then
  710. Begin
  711. Writeln(pstdout^,'Runtime error ',Errorcode,' at $',hexstr(erroraddr));
  712. { to get a nice symify }
  713. Writeln(pstdout^,BackTraceStrFunc(Erroraddr));
  714. dump_stack(pstdout^,ErrorBase);
  715. Writeln(pstdout^,'');
  716. End;
  717. { Make sure that all output is written to the redirected file }
  718. if Textrec(Output).Mode=fmOutput then
  719. Flush(Output);
  720. if Textrec(ErrOutput).Mode=fmOutput then
  721. Flush(ErrOutput);
  722. if Textrec(pstdout^).Mode=fmOutput then
  723. Flush(pstdout^);
  724. if Textrec(StdErr).Mode=fmOutput then
  725. Flush(StdErr);
  726. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  727. {$if defined(MSWINDOWS) or defined(OS2)}
  728. { finally release the heap if possible, especially
  729. important for DLLs }
  730. for i:=0 to argc do
  731. sysfreemem(argv[i]);
  732. sysfreemem(argv);
  733. {$endif}
  734. {$ifdef LINUX}
  735. {sysfreemem already checks for nil}
  736. sysfreemem(calculated_cmdline);
  737. {$endif}
  738. {$ifdef BSD}
  739. sysfreemem(cmdline);
  740. {$endif}
  741. {$ifdef FPC_HAS_FEATURE_HEAP}
  742. {$ifndef HAS_MEMORYMANAGER}
  743. FinalizeHeap;
  744. {$endif HAS_MEMORYMANAGER}
  745. {$endif FPC_HAS_FEATURE_HEAP}
  746. End;
  747. Procedure do_exit;[Public,Alias:'FPC_DO_EXIT'];
  748. begin
  749. InternalExit;
  750. System_exit;
  751. end;
  752. Procedure lib_exit;[Public,Alias:'FPC_LIB_EXIT'];
  753. begin
  754. InternalExit;
  755. end;
  756. Procedure Halt(ErrNum: Longint);
  757. Begin
  758. ExitCode:=Errnum;
  759. Do_Exit;
  760. end;
  761. function SysBackTraceStr (Addr: Pointer): ShortString;
  762. begin
  763. SysBackTraceStr:=' $'+hexstr(addr);
  764. end;
  765. Procedure HandleErrorAddrFrame (Errno : longint;addr,frame : Pointer);[public,alias:'FPC_BREAK_ERROR']; {$ifdef CPU86} register; {$endif}
  766. begin
  767. If pointer(ErrorProc)<>Nil then
  768. ErrorProc(Errno,addr,frame);
  769. errorcode:=word(Errno);
  770. erroraddr:=addr;
  771. errorbase:=frame;
  772. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  773. if ExceptAddrStack <> nil then
  774. raise TObject(nil) at addr,frame;
  775. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  776. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  777. {$ifdef FPC_LIMITED_EXITCODE}
  778. if errorcode > maxExitCode then
  779. halt(255)
  780. else
  781. {$endif FPC_LIMITED_EXITCODE}
  782. halt(errorcode);
  783. {$else FPC_HAS_FEATURE_EXITCODE}
  784. halt;
  785. {$endif FPC_HAS_FEATURE_EXITCODE}
  786. end;
  787. Procedure HandleErrorFrame (Errno : longint;frame : Pointer);
  788. {
  789. Procedure to handle internal errors, i.e. not user-invoked errors
  790. Internal function should ALWAYS call HandleError instead of RunError.
  791. Can be used for exception handlers to specify the frame
  792. }
  793. begin
  794. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  795. end;
  796. Procedure HandleError (Errno : longint);[public,alias : 'FPC_HANDLEERROR'];
  797. {
  798. Procedure to handle internal errors, i.e. not user-invoked errors
  799. Internal function should ALWAYS call HandleError instead of RunError.
  800. }
  801. begin
  802. HandleErrorFrame(Errno,get_frame);
  803. end;
  804. procedure RunError(w : word);[alias: 'FPC_RUNERROR'];
  805. begin
  806. errorcode:=w;
  807. erroraddr:=get_caller_addr(get_frame);
  808. errorbase:=get_caller_frame(get_frame);
  809. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  810. {$ifdef FPC_LIMITED_EXITCODE}
  811. if errorcode > maxExitCode then
  812. halt(255)
  813. else
  814. {$endif FPC_LIMITED_EXITCODE}
  815. halt(errorcode);
  816. {$else FPC_HAS_FEATURE_EXITCODE}
  817. halt;
  818. {$endif FPC_HAS_FEATURE_EXITCODE}
  819. end;
  820. Procedure RunError;{$ifdef SYSTEMINLINE}inline;{$endif}
  821. Begin
  822. RunError (0);
  823. End;
  824. Procedure Halt;{$ifdef SYSTEMINLINE}inline;{$endif}
  825. Begin
  826. Halt(0);
  827. End;
  828. Procedure Error(RunTimeError : TRunTimeError);
  829. begin
  830. RunError(RuntimeErrorExitCodes[RunTimeError]);
  831. end;
  832. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  833. Procedure dump_stack(var f : text;bp : Pointer);
  834. var
  835. i : Longint;
  836. prevbp : Pointer;
  837. is_dev : boolean;
  838. caller_frame,
  839. caller_addr : Pointer;
  840. Begin
  841. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  842. try
  843. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  844. prevbp:=bp-1;
  845. i:=0;
  846. is_dev:=do_isdevice(textrec(f).Handle);
  847. while bp > prevbp Do
  848. Begin
  849. caller_addr := get_caller_addr(bp);
  850. caller_frame := get_caller_frame(bp);
  851. if (caller_addr=nil) then
  852. break;
  853. Writeln(f,BackTraceStrFunc(caller_addr));
  854. if (caller_frame=nil) then
  855. break;
  856. Inc(i);
  857. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  858. break;
  859. prevbp:=bp;
  860. bp:=caller_frame;
  861. End;
  862. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  863. except
  864. { prevent endless dump if an exception occured }
  865. end;
  866. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  867. End;
  868. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  869. procedure DumpExceptionBackTrace(var f:text);
  870. var
  871. FrameNumber,
  872. FrameCount : longint;
  873. Frames : PPointer;
  874. begin
  875. if RaiseList=nil then
  876. exit;
  877. WriteLn(f,BackTraceStrFunc(RaiseList^.Addr));
  878. FrameCount:=RaiseList^.Framecount;
  879. Frames:=RaiseList^.Frames;
  880. for FrameNumber := 0 to FrameCount-1 do
  881. WriteLn(f,BackTraceStrFunc(Frames[FrameNumber]));
  882. end;
  883. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  884. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  885. {$ifdef FPC_HAS_FEATURE_HEAP}
  886. Type
  887. PExitProcInfo = ^TExitProcInfo;
  888. TExitProcInfo = Record
  889. Next : PExitProcInfo;
  890. SaveExit : Pointer;
  891. Proc : TProcedure;
  892. End;
  893. const
  894. ExitProcList: PExitProcInfo = nil;
  895. Procedure DoExitProc;
  896. var
  897. P : PExitProcInfo;
  898. Proc : TProcedure;
  899. Begin
  900. P:=ExitProcList;
  901. ExitProcList:=P^.Next;
  902. ExitProc:=P^.SaveExit;
  903. Proc:=P^.Proc;
  904. DisPose(P);
  905. Proc();
  906. End;
  907. Procedure AddExitProc(Proc: TProcedure);
  908. var
  909. P : PExitProcInfo;
  910. Begin
  911. New(P);
  912. P^.Next:=ExitProcList;
  913. P^.SaveExit:=ExitProc;
  914. P^.Proc:=Proc;
  915. ExitProcList:=P;
  916. ExitProc:=@DoExitProc;
  917. End;
  918. {$endif FPC_HAS_FEATURE_HEAP}
  919. {$ifdef FPC_HAS_FEATURE_HEAP}
  920. function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):ppchar; // const ?
  921. // Extra allocate reserveentries pchar's at the beginning (default param=0 after 1.0.x ?)
  922. // Note: for internal use by skilled programmers only
  923. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  924. var p : ppchar;
  925. i : LongInt;
  926. begin
  927. if High(s)<Low(s) Then Exit(NIL);
  928. Getmem(p,sizeof(pchar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  929. // for cmd
  930. if p=nil then
  931. begin
  932. {$ifdef xunix}
  933. fpseterrno(ESysEnomem);
  934. {$endif}
  935. exit(NIL);
  936. end;
  937. for i:=low(s) to high(s) do
  938. p[i+Reserveentries]:=pchar(s[i]);
  939. p[high(s)+1+Reserveentries]:=nil;
  940. ArrayStringToPPchar:=p;
  941. end;
  942. Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):ppchar;
  943. {
  944. Create a PPChar to structure of pchars which are the arguments specified
  945. in the string S. Especially useful for creating an ArgV for Exec-calls
  946. }
  947. begin
  948. StringToPPChar:=StringToPPChar(PChar(S),ReserveEntries);
  949. end;
  950. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  951. var
  952. i,nr : longint;
  953. Buf : ^char;
  954. p : ppchar;
  955. begin
  956. buf:=s;
  957. nr:=1;
  958. while (buf^<>#0) do // count nr of args
  959. begin
  960. while (buf^ in [' ',#9,#10]) do // Kill separators.
  961. inc(buf);
  962. inc(nr);
  963. if buf^='"' Then // quotes argument?
  964. begin
  965. inc(buf);
  966. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  967. inc(buf);
  968. if buf^='"' then // skip closing quote.
  969. inc(buf);
  970. end
  971. else
  972. begin // else std
  973. while not (buf^ in [' ',#0,#9,#10]) do
  974. inc(buf);
  975. end;
  976. end;
  977. getmem(p,(ReserveEntries+nr)*sizeof(pchar));
  978. StringToPPChar:=p;
  979. if p=nil then
  980. begin
  981. {$ifdef xunix}
  982. fpseterrno(ESysEnomem);
  983. {$endif}
  984. exit;
  985. end;
  986. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  987. buf:=s;
  988. while (buf^<>#0) do
  989. begin
  990. while (buf^ in [' ',#9,#10]) do // Kill separators.
  991. begin
  992. buf^:=#0;
  993. inc(buf);
  994. end;
  995. if buf^='"' Then // quotes argument?
  996. begin
  997. inc(buf);
  998. p^:=buf;
  999. inc(p);
  1000. p^:=nil;
  1001. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1002. inc(buf);
  1003. if buf^='"' then // skip closing quote.
  1004. begin
  1005. buf^:=#0;
  1006. inc(buf);
  1007. end;
  1008. end
  1009. else
  1010. begin
  1011. p^:=buf;
  1012. inc(p);
  1013. p^:=nil;
  1014. while not (buf^ in [' ',#0,#9,#10]) do
  1015. inc(buf);
  1016. end;
  1017. end;
  1018. end;
  1019. {$endif FPC_HAS_FEATURE_HEAP}
  1020. {*****************************************************************************
  1021. Abstract/Assert support.
  1022. *****************************************************************************}
  1023. procedure fpc_AbstractErrorIntern;compilerproc;[public,alias : 'FPC_ABSTRACTERROR'];
  1024. begin
  1025. If pointer(AbstractErrorProc)<>nil then
  1026. AbstractErrorProc();
  1027. HandleErrorFrame(211,get_frame);
  1028. end;
  1029. Procedure fpc_assert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer); [Public,Alias : 'FPC_ASSERT']; compilerproc;
  1030. begin
  1031. if pointer(AssertErrorProc)<>nil then
  1032. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  1033. else
  1034. HandleErrorFrame(227,get_frame);
  1035. end;
  1036. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
  1037. begin
  1038. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1039. If msg='' then
  1040. write(stderr,'Assertion failed')
  1041. else
  1042. write(stderr,msg);
  1043. Writeln(stderr,' (',FName,', line ',LineNo,').');
  1044. Writeln(stderr,'');
  1045. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  1046. Halt(227);
  1047. {$else FPC_HAS_FEATURE_EXITCODE}
  1048. halt;
  1049. {$endif FPC_HAS_FEATURE_EXITCODE}
  1050. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1051. end;
  1052. {*****************************************************************************
  1053. SetJmp/LongJmp support.
  1054. *****************************************************************************}
  1055. {$i setjump.inc}
  1056. {$ifdef IOCheckWasOn}
  1057. {$I+}
  1058. {$endif}
  1059. {$ifdef RangeCheckWasOn}
  1060. {$R+}
  1061. {$endif}
  1062. {$ifdef OverflowCheckWasOn}
  1063. {$Q+}
  1064. {$endif}
  1065. {*****************************************************************************
  1066. Heap
  1067. *****************************************************************************}
  1068. {$ifdef FPC_HAS_FEATURE_HEAP}
  1069. {$i sysheap.inc}
  1070. {$i heap.inc}
  1071. {$endif FPC_HAS_FEATURE_HEAP}
  1072. {*****************************************************************************
  1073. Thread support
  1074. *****************************************************************************}
  1075. {$ifdef FPC_HAS_FEATURE_THREADING}
  1076. { Generic threadmanager }
  1077. {$i thread.inc}
  1078. { Generic threadvar support }
  1079. {$i threadvr.inc}
  1080. {$ifdef DISABLE_NO_THREAD_MANAGER}
  1081. { OS Dependent implementation }
  1082. {$i systhrd.inc}
  1083. {$endif DISABLE_NO_THREAD_MANAGER}
  1084. {$endif FPC_HAS_FEATURE_THREADING}
  1085. {*****************************************************************************
  1086. File Handling
  1087. *****************************************************************************}
  1088. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1089. { Allow slash and backslash as separators }
  1090. procedure DoDirSeparators(p:Pchar);
  1091. var
  1092. i : longint;
  1093. begin
  1094. for i:=0 to strlen(p) do
  1095. if p[i] in AllowDirectorySeparators then
  1096. p[i]:=DirectorySeparator;
  1097. end;
  1098. procedure DoDirSeparators(var p:shortstring);
  1099. var
  1100. i : longint;
  1101. begin
  1102. for i:=1 to length(p) do
  1103. if p[i] in AllowDirectorySeparators then
  1104. p[i]:=DirectorySeparator;
  1105. end;
  1106. {$endif FPC_HAS_FEATURE_FILEIO}
  1107. { OS dependent low level file functions }
  1108. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1109. {$i sysfile.inc}
  1110. {$endif FPC_HAS_FEATURE_FILEIO}
  1111. { Text file }
  1112. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  1113. {$i text.inc}
  1114. {$endif FPC_HAS_FEATURE_TEXTIO}
  1115. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1116. { Untyped file }
  1117. {$i file.inc}
  1118. { Typed file }
  1119. {$i typefile.inc}
  1120. {$endif FPC_HAS_FEATURE_FILEIO}
  1121. {*****************************************************************************
  1122. Directory Handling
  1123. *****************************************************************************}
  1124. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1125. { OS dependent dir functions }
  1126. {$i sysdir.inc}
  1127. {$endif FPC_HAS_FEATURE_FILEIO}
  1128. {$if defined(FPC_HAS_FEATURE_FILEIO) and defined(FPC_HAS_FEATURE_ANSISTRINGS)}
  1129. Procedure getdir(drivenr:byte;Var dir:ansistring);
  1130. { this is needed to also allow ansistrings, the shortstring version is
  1131. OS dependent }
  1132. var
  1133. s : shortstring;
  1134. begin
  1135. getdir(drivenr,s);
  1136. dir:=s;
  1137. end;
  1138. {$endif}
  1139. {$if defined(FPC_HAS_FEATURE_FILEIO)}
  1140. Procedure MkDir(Const s: String);
  1141. Var
  1142. Buffer: Array[0..255] of Char;
  1143. Begin
  1144. If (s='') or (InOutRes <> 0) then
  1145. exit;
  1146. Move(s[1], Buffer, Length(s));
  1147. Buffer[Length(s)] := #0;
  1148. MkDir(@buffer[0],length(s));
  1149. End;
  1150. Procedure RmDir(Const s: String);
  1151. Var
  1152. Buffer: Array[0..255] of Char;
  1153. Begin
  1154. If (s='') or (InOutRes <> 0) then
  1155. exit;
  1156. Move(s[1], Buffer, Length(s));
  1157. Buffer[Length(s)] := #0;
  1158. RmDir(@buffer[0],length(s));
  1159. End;
  1160. Procedure ChDir(Const s: String);
  1161. Var
  1162. Buffer: Array[0..255] of Char;
  1163. Begin
  1164. If (s='') or (InOutRes <> 0) then
  1165. exit;
  1166. Move(s[1], Buffer, Length(s));
  1167. Buffer[Length(s)] := #0;
  1168. ChDir(@buffer[0],length(s));
  1169. End;
  1170. {$endif}
  1171. {*****************************************************************************
  1172. Resources support
  1173. *****************************************************************************}
  1174. {$i sysres.inc}
  1175. const
  1176. CtrlBreakHandler: TCtrlBreakHandler = nil;
  1177. {$IFNDEF FPC_HAS_SETCTRLBREAKHANDLER}
  1178. (* It is possible to provide platform specific implementation performing *)
  1179. (* special initialization; default implementation just sets the procedural *)
  1180. (* variable to make it available for use from the exception handler. *)
  1181. function SysSetCtrlBreakHandler (Handler: TCtrlBreakHandler): TCtrlBreakHandler;
  1182. begin
  1183. (* Return either nil or previous handler *)
  1184. SysSetCtrlBreakHandler := CtrlBreakHandler;
  1185. CtrlBreakHandler := Handler;
  1186. end;
  1187. {$ENDIF FPC_HAS_SETCTRLBREAKHANDLER}