system.inc 38 KB

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