system.inc 38 KB

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