system.inc 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  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;
  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. {$if not defined(BEOS) and not defined(SYMOBI)}
  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. {$ifopt R+}
  510. {$define RangeCheckWasOn}
  511. {$R-}
  512. {$endif opt R+}
  513. {$ifopt I+}
  514. {$define IOCheckWasOn}
  515. {$I-}
  516. {$endif opt I+}
  517. {$ifopt Q+}
  518. {$define OverflowCheckWasOn}
  519. {$Q-}
  520. {$endif opt Q+}
  521. {*****************************************************************************
  522. Miscellaneous
  523. *****************************************************************************}
  524. procedure fpc_rangeerror;[public,alias:'FPC_RANGEERROR']; compilerproc;
  525. begin
  526. HandleErrorFrame(201,get_frame);
  527. end;
  528. procedure fpc_divbyzero;[public,alias:'FPC_DIVBYZERO']; compilerproc;
  529. begin
  530. HandleErrorFrame(200,get_frame);
  531. end;
  532. procedure fpc_overflow;[public,alias:'FPC_OVERFLOW']; compilerproc;
  533. begin
  534. HandleErrorFrame(215,get_frame);
  535. end;
  536. procedure fpc_threaderror; [public,alias:'FPC_THREADERROR'];
  537. begin
  538. HandleErrorFrame(6,get_frame);
  539. end;
  540. procedure fpc_iocheck;[public,alias:'FPC_IOCHECK']; compilerproc;
  541. var
  542. l : longint;
  543. HInoutRes : PWord;
  544. begin
  545. HInOutRes:=@InoutRes;
  546. if HInOutRes^<>0 then
  547. begin
  548. l:=HInOutRes^;
  549. HInOutRes^:=0;
  550. HandleErrorFrame(l,get_frame);
  551. end;
  552. end;
  553. Function IOResult:Word;
  554. var
  555. HInoutRes : PWord;
  556. Begin
  557. HInoutRes:=@InoutRes;
  558. IOResult:=HInOutRes^;
  559. HInOutRes^:=0;
  560. End;
  561. Function GetThreadID:TThreadID;{$ifdef SYSTEMINLINE}inline;{$endif}
  562. begin
  563. (* ThreadID is stored in a threadvar and made available in interface *)
  564. (* to allow setup of this value during thread initialization. *)
  565. GetThreadID := ThreadID;
  566. end;
  567. function fpc_safecallcheck(res : hresult) : hresult;[public,alias:'FPC_SAFECALLCHECK']; compilerproc; {$ifdef CPU86} register; {$endif}
  568. begin
  569. if res<0 then
  570. begin
  571. if assigned(SafeCallErrorProc) then
  572. SafeCallErrorProc(res,get_frame);
  573. HandleErrorFrame(229,get_frame);
  574. end;
  575. result:=res;
  576. end;
  577. {*****************************************************************************
  578. Stack check code
  579. *****************************************************************************}
  580. { be compatible with old code }
  581. {$ifdef FPC_NO_GENERIC_STACK_CHECK}
  582. {$define NO_GENERIC_STACK_CHECK}
  583. {$endif FPC_NO_GENERIC_STACK_CHECK}
  584. {$IFNDEF NO_GENERIC_STACK_CHECK}
  585. {$IFOPT S+}
  586. {$DEFINE STACKCHECK}
  587. {$ENDIF}
  588. {$S-}
  589. procedure fpc_stackcheck(stack_size:SizeUInt);[public,alias:'FPC_STACKCHECK'];
  590. var
  591. c : Pointer;
  592. begin
  593. { Avoid recursive calls when called from the exit routines }
  594. if StackError then
  595. exit;
  596. { don't use sack_size, since the stack pointer has already been
  597. decreased when this routine is called
  598. }
  599. c := Sptr - STACK_MARGIN;
  600. if (c <= StackBottom) then
  601. begin
  602. StackError:=true;
  603. HandleError(202);
  604. end;
  605. end;
  606. {$IFDEF STACKCHECK}
  607. {$S+}
  608. {$ENDIF}
  609. {$UNDEF STACKCHECK}
  610. {$ENDIF NO_GENERIC_STACK_CHECK}
  611. {*****************************************************************************
  612. Initialization / Finalization
  613. *****************************************************************************}
  614. const
  615. maxunits=1024; { See also files.pas of the compiler source }
  616. type
  617. TInitFinalRec=record
  618. InitProc,
  619. FinalProc : TProcedure;
  620. end;
  621. TInitFinalTable = record
  622. TableCount,
  623. InitCount : longint;
  624. Procs : array[1..maxunits] of TInitFinalRec;
  625. end;
  626. PInitFinalTable = ^TInitFinalTable;
  627. {$ifndef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  628. var
  629. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  630. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  631. procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; compilerproc;
  632. var
  633. i : longint;
  634. begin
  635. { call cpu/fpu initialisation routine }
  636. fpc_cpuinit;
  637. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  638. with PInitFinalTable(EntryInformation.InitFinalTable)^ do
  639. {$else FPC_HAS_INDIRECT_MAIN_INFORMATION}
  640. with InitFinalTable do
  641. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  642. begin
  643. for i:=1 to TableCount do
  644. begin
  645. if assigned(Procs[i].InitProc) then
  646. Procs[i].InitProc();
  647. InitCount:=i;
  648. end;
  649. end;
  650. if assigned(InitProc) then
  651. TProcedure(InitProc)();
  652. end;
  653. procedure internal_initializeunits; external name 'FPC_INITIALIZEUNITS';
  654. procedure fpc_LibInitializeUnits;[public,alias:'FPC_LIBINITIALIZEUNITS'];
  655. begin
  656. IsLibrary:=true;
  657. { must also be set to true for packages when implemented }
  658. ModuleIsLib:=true;
  659. internal_initializeunits;
  660. end;
  661. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  662. begin
  663. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  664. with PInitFinalTable(EntryInformation.InitFinalTable)^ do
  665. {$else FPC_HAS_INDIRECT_MAIN_INFORMATION}
  666. with InitFinalTable do
  667. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  668. begin
  669. while (InitCount>0) do
  670. begin
  671. // we've to decrement the cound before calling the final. code
  672. // else a halt in the final. code leads to a endless loop
  673. dec(InitCount);
  674. if assigned(Procs[InitCount+1].FinalProc) then
  675. Procs[InitCount+1].FinalProc();
  676. end;
  677. end;
  678. end;
  679. {*****************************************************************************
  680. Error / Exit / ExitProc
  681. *****************************************************************************}
  682. Procedure system_exit;forward;
  683. {$ifdef FPC_HAS_FEATURE_HEAP}
  684. {$ifndef HAS_MEMORYMANAGER}
  685. //not needed if independant memory manager
  686. Procedure FinalizeHeap;forward;
  687. {$endif HAS_MEMORYMANAGER}
  688. {$endif FPC_HAS_FEATURE_HEAP}
  689. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  690. procedure SysFlushStdIO;
  691. begin
  692. { Make sure that all output is written to the redirected file }
  693. if Textrec(Output).Mode=fmOutput then
  694. Flush(Output);
  695. if Textrec(ErrOutput).Mode=fmOutput then
  696. Flush(ErrOutput);
  697. if Textrec(stdout).Mode=fmOutput then
  698. Flush(stdout);
  699. if Textrec(StdErr).Mode=fmOutput then
  700. Flush(StdErr);
  701. end;
  702. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  703. Procedure InternalExit;
  704. var
  705. current_exit : Procedure;
  706. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  707. pstdout : ^Text;
  708. {$endif}
  709. {$if defined(MSWINDOWS) or defined(OS2)}
  710. i : longint;
  711. {$endif}
  712. Begin
  713. {$ifdef SYSTEMDEBUG}
  714. writeln('InternalExit');
  715. {$endif SYSTEMDEBUG}
  716. while exitProc<>nil Do
  717. Begin
  718. InOutRes:=0;
  719. current_exit:=tProcedure(exitProc);
  720. exitProc:=nil;
  721. current_exit();
  722. End;
  723. { Finalize units }
  724. FinalizeUnits;
  725. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  726. { Show runtime error and exit }
  727. pstdout:=@stdout;
  728. If erroraddr<>nil Then
  729. Begin
  730. Writeln(pstdout^,'Runtime error ',Errorcode,' at $',hexstr(erroraddr));
  731. { to get a nice symify }
  732. Writeln(pstdout^,BackTraceStrFunc(Erroraddr));
  733. dump_stack(pstdout^,ErrorBase);
  734. Writeln(pstdout^,'');
  735. End;
  736. SysFlushStdIO;
  737. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  738. {$if defined(MSWINDOWS) or defined(OS2)}
  739. { finally release the heap if possible, especially
  740. important for DLLs.
  741. Reset the array to nil, and finally also argv itself to
  742. avoid double freeing problem in case this function gets called twice. }
  743. if assigned(argv) then
  744. begin
  745. for i:=0 to argc-1 do
  746. if assigned(argv[i]) then
  747. begin
  748. sysfreemem(argv[i]);
  749. argv[i]:=nil;
  750. end;
  751. sysfreemem(argv);
  752. argv:=nil;
  753. end;
  754. {$endif}
  755. {$ifdef LINUX}
  756. {sysfreemem already checks for nil}
  757. sysfreemem(calculated_cmdline);
  758. {$endif}
  759. {$ifdef BSD}
  760. sysfreemem(cmdline);
  761. {$endif}
  762. {$ifdef FPC_HAS_FEATURE_HEAP}
  763. {$ifndef HAS_MEMORYMANAGER}
  764. FinalizeHeap;
  765. {$endif HAS_MEMORYMANAGER}
  766. {$endif FPC_HAS_FEATURE_HEAP}
  767. End;
  768. Procedure do_exit;[Public,Alias:'FPC_DO_EXIT'];
  769. begin
  770. InternalExit;
  771. System_exit;
  772. end;
  773. Procedure lib_exit;[Public,Alias:'FPC_LIB_EXIT'];
  774. begin
  775. InternalExit;
  776. end;
  777. Procedure Halt(ErrNum: Longint);
  778. Begin
  779. ExitCode:=Errnum;
  780. Do_Exit;
  781. end;
  782. function SysBackTraceStr (Addr: Pointer): ShortString;
  783. begin
  784. SysBackTraceStr:=' $'+hexstr(addr);
  785. end;
  786. Procedure HandleErrorAddrFrame (Errno : longint;addr,frame : Pointer);[public,alias:'FPC_BREAK_ERROR']; {$ifdef CPU86} register; {$endif}
  787. begin
  788. If pointer(ErrorProc)<>Nil then
  789. ErrorProc(Errno,addr,frame);
  790. errorcode:=word(Errno);
  791. erroraddr:=addr;
  792. errorbase:=frame;
  793. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  794. if ExceptAddrStack <> nil then
  795. raise TObject(nil) at addr,frame;
  796. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  797. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  798. {$ifdef FPC_LIMITED_EXITCODE}
  799. if errorcode > maxExitCode then
  800. halt(255)
  801. else
  802. {$endif FPC_LIMITED_EXITCODE}
  803. halt(errorcode);
  804. {$else FPC_HAS_FEATURE_EXITCODE}
  805. halt;
  806. {$endif FPC_HAS_FEATURE_EXITCODE}
  807. end;
  808. Procedure HandleErrorFrame (Errno : longint;frame : Pointer);
  809. {
  810. Procedure to handle internal errors, i.e. not user-invoked errors
  811. Internal function should ALWAYS call HandleError instead of RunError.
  812. Can be used for exception handlers to specify the frame
  813. }
  814. begin
  815. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  816. end;
  817. Procedure HandleError (Errno : longint);[public,alias : 'FPC_HANDLEERROR'];
  818. {
  819. Procedure to handle internal errors, i.e. not user-invoked errors
  820. Internal function should ALWAYS call HandleError instead of RunError.
  821. }
  822. begin
  823. HandleErrorFrame(Errno,get_frame);
  824. end;
  825. procedure RunError(w : word);[alias: 'FPC_RUNERROR'];
  826. begin
  827. errorcode:=w;
  828. erroraddr:=get_caller_addr(get_frame);
  829. errorbase:=get_caller_frame(get_frame);
  830. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  831. {$ifdef FPC_LIMITED_EXITCODE}
  832. if errorcode > maxExitCode then
  833. halt(255)
  834. else
  835. {$endif FPC_LIMITED_EXITCODE}
  836. halt(errorcode);
  837. {$else FPC_HAS_FEATURE_EXITCODE}
  838. halt;
  839. {$endif FPC_HAS_FEATURE_EXITCODE}
  840. end;
  841. Procedure RunError;{$ifdef SYSTEMINLINE}inline;{$endif}
  842. Begin
  843. RunError (0);
  844. End;
  845. Procedure Halt;{$ifdef SYSTEMINLINE}inline;{$endif}
  846. Begin
  847. Halt(0);
  848. End;
  849. Procedure Error(RunTimeError : TRunTimeError);
  850. begin
  851. RunError(RuntimeErrorExitCodes[RunTimeError]);
  852. end;
  853. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  854. Procedure dump_stack(var f : text;bp : Pointer);
  855. var
  856. i : Longint;
  857. prevbp : Pointer;
  858. is_dev : boolean;
  859. caller_frame,
  860. caller_addr : Pointer;
  861. Begin
  862. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  863. try
  864. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  865. prevbp:=bp-1;
  866. i:=0;
  867. is_dev:=do_isdevice(textrec(f).Handle);
  868. while bp > prevbp Do
  869. Begin
  870. caller_addr := get_caller_addr(bp);
  871. caller_frame := get_caller_frame(bp);
  872. if (caller_addr=nil) then
  873. break;
  874. Writeln(f,BackTraceStrFunc(caller_addr));
  875. if (caller_frame=nil) then
  876. break;
  877. Inc(i);
  878. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  879. break;
  880. prevbp:=bp;
  881. bp:=caller_frame;
  882. End;
  883. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  884. except
  885. { prevent endless dump if an exception occured }
  886. end;
  887. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  888. End;
  889. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  890. procedure DumpExceptionBackTrace(var f:text);
  891. var
  892. FrameNumber,
  893. FrameCount : longint;
  894. Frames : PPointer;
  895. begin
  896. if RaiseList=nil then
  897. exit;
  898. WriteLn(f,BackTraceStrFunc(RaiseList^.Addr));
  899. FrameCount:=RaiseList^.Framecount;
  900. Frames:=RaiseList^.Frames;
  901. for FrameNumber := 0 to FrameCount-1 do
  902. WriteLn(f,BackTraceStrFunc(Frames[FrameNumber]));
  903. end;
  904. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  905. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  906. {$ifdef FPC_HAS_FEATURE_HEAP}
  907. Type
  908. PExitProcInfo = ^TExitProcInfo;
  909. TExitProcInfo = Record
  910. Next : PExitProcInfo;
  911. SaveExit : Pointer;
  912. Proc : TProcedure;
  913. End;
  914. const
  915. ExitProcList: PExitProcInfo = nil;
  916. Procedure DoExitProc;
  917. var
  918. P : PExitProcInfo;
  919. Proc : TProcedure;
  920. Begin
  921. P:=ExitProcList;
  922. ExitProcList:=P^.Next;
  923. ExitProc:=P^.SaveExit;
  924. Proc:=P^.Proc;
  925. DisPose(P);
  926. Proc();
  927. End;
  928. Procedure AddExitProc(Proc: TProcedure);
  929. var
  930. P : PExitProcInfo;
  931. Begin
  932. New(P);
  933. P^.Next:=ExitProcList;
  934. P^.SaveExit:=ExitProc;
  935. P^.Proc:=Proc;
  936. ExitProcList:=P;
  937. ExitProc:=@DoExitProc;
  938. End;
  939. {$endif FPC_HAS_FEATURE_HEAP}
  940. {$ifdef FPC_HAS_FEATURE_HEAP}
  941. function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):ppchar; // const ?
  942. // Extra allocate reserveentries pchar's at the beginning (default param=0 after 1.0.x ?)
  943. // Note: for internal use by skilled programmers only
  944. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  945. var p : ppchar;
  946. i : LongInt;
  947. begin
  948. if High(s)<Low(s) Then Exit(NIL);
  949. Getmem(p,sizeof(pchar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  950. // for cmd
  951. if p=nil then
  952. begin
  953. {$ifdef xunix}
  954. fpseterrno(ESysEnomem);
  955. {$endif}
  956. exit(NIL);
  957. end;
  958. for i:=low(s) to high(s) do
  959. p[i+Reserveentries]:=pchar(s[i]);
  960. p[high(s)+1+Reserveentries]:=nil;
  961. ArrayStringToPPchar:=p;
  962. end;
  963. Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):ppchar;
  964. {
  965. Create a PPChar to structure of pchars which are the arguments specified
  966. in the string S. Especially useful for creating an ArgV for Exec-calls
  967. }
  968. begin
  969. StringToPPChar:=StringToPPChar(PChar(S),ReserveEntries);
  970. end;
  971. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  972. var
  973. i,nr : longint;
  974. Buf : ^char;
  975. p : ppchar;
  976. begin
  977. buf:=s;
  978. nr:=1;
  979. while (buf^<>#0) do // count nr of args
  980. begin
  981. while (buf^ in [' ',#9,#10]) do // Kill separators.
  982. inc(buf);
  983. inc(nr);
  984. if buf^='"' Then // quotes argument?
  985. begin
  986. inc(buf);
  987. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  988. inc(buf);
  989. if buf^='"' then // skip closing quote.
  990. inc(buf);
  991. end
  992. else
  993. begin // else std
  994. while not (buf^ in [' ',#0,#9,#10]) do
  995. inc(buf);
  996. end;
  997. end;
  998. getmem(p,(ReserveEntries+nr)*sizeof(pchar));
  999. StringToPPChar:=p;
  1000. if p=nil then
  1001. begin
  1002. {$ifdef xunix}
  1003. fpseterrno(ESysEnomem);
  1004. {$endif}
  1005. exit;
  1006. end;
  1007. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  1008. buf:=s;
  1009. while (buf^<>#0) do
  1010. begin
  1011. while (buf^ in [' ',#9,#10]) do // Kill separators.
  1012. begin
  1013. buf^:=#0;
  1014. inc(buf);
  1015. end;
  1016. if buf^='"' Then // quotes argument?
  1017. begin
  1018. inc(buf);
  1019. p^:=buf;
  1020. inc(p);
  1021. p^:=nil;
  1022. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1023. inc(buf);
  1024. if buf^='"' then // skip closing quote.
  1025. begin
  1026. buf^:=#0;
  1027. inc(buf);
  1028. end;
  1029. end
  1030. else
  1031. begin
  1032. p^:=buf;
  1033. inc(p);
  1034. p^:=nil;
  1035. while not (buf^ in [' ',#0,#9,#10]) do
  1036. inc(buf);
  1037. end;
  1038. end;
  1039. end;
  1040. {$endif FPC_HAS_FEATURE_HEAP}
  1041. {*****************************************************************************
  1042. Abstract/Assert support.
  1043. *****************************************************************************}
  1044. procedure fpc_AbstractErrorIntern;compilerproc;[public,alias : 'FPC_ABSTRACTERROR'];
  1045. begin
  1046. If pointer(AbstractErrorProc)<>nil then
  1047. AbstractErrorProc();
  1048. HandleErrorFrame(211,get_frame);
  1049. end;
  1050. Procedure fpc_assert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer); [Public,Alias : 'FPC_ASSERT']; compilerproc;
  1051. begin
  1052. if pointer(AssertErrorProc)<>nil then
  1053. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  1054. else
  1055. HandleErrorFrame(227,get_frame);
  1056. end;
  1057. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
  1058. begin
  1059. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1060. If msg='' then
  1061. write(stderr,'Assertion failed')
  1062. else
  1063. write(stderr,msg);
  1064. Writeln(stderr,' (',FName,', line ',LineNo,').');
  1065. Writeln(stderr,'');
  1066. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  1067. Halt(227);
  1068. {$else FPC_HAS_FEATURE_EXITCODE}
  1069. halt;
  1070. {$endif FPC_HAS_FEATURE_EXITCODE}
  1071. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1072. end;
  1073. {*****************************************************************************
  1074. SetJmp/LongJmp support.
  1075. *****************************************************************************}
  1076. {$i setjump.inc}
  1077. {$ifdef IOCheckWasOn}
  1078. {$I+}
  1079. {$endif}
  1080. {$ifdef RangeCheckWasOn}
  1081. {$R+}
  1082. {$endif}
  1083. {$ifdef OverflowCheckWasOn}
  1084. {$Q+}
  1085. {$endif}
  1086. {*****************************************************************************
  1087. Heap
  1088. *****************************************************************************}
  1089. {$ifdef FPC_HAS_FEATURE_HEAP}
  1090. {$i sysheap.inc}
  1091. {$i heap.inc}
  1092. {$endif FPC_HAS_FEATURE_HEAP}
  1093. {*****************************************************************************
  1094. Thread support
  1095. *****************************************************************************}
  1096. {$ifdef FPC_HAS_FEATURE_THREADING}
  1097. { Generic threadmanager }
  1098. {$i thread.inc}
  1099. { Generic threadvar support }
  1100. {$i threadvr.inc}
  1101. {$ifdef DISABLE_NO_THREAD_MANAGER}
  1102. { OS Dependent implementation }
  1103. {$i systhrd.inc}
  1104. {$endif DISABLE_NO_THREAD_MANAGER}
  1105. {$endif FPC_HAS_FEATURE_THREADING}
  1106. {*****************************************************************************
  1107. File Handling
  1108. *****************************************************************************}
  1109. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1110. { Allow slash and backslash as separators }
  1111. procedure DoDirSeparators(p:Pchar);
  1112. var
  1113. i : longint;
  1114. begin
  1115. for i:=0 to strlen(p) do
  1116. if p[i] in AllowDirectorySeparators then
  1117. p[i]:=DirectorySeparator;
  1118. end;
  1119. procedure DoDirSeparators(var p:shortstring);
  1120. var
  1121. i : longint;
  1122. begin
  1123. for i:=1 to length(p) do
  1124. if p[i] in AllowDirectorySeparators then
  1125. p[i]:=DirectorySeparator;
  1126. end;
  1127. {$endif FPC_HAS_FEATURE_FILEIO}
  1128. { OS dependent low level file functions }
  1129. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1130. {$i sysfile.inc}
  1131. {$endif FPC_HAS_FEATURE_FILEIO}
  1132. { Text file }
  1133. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  1134. {$i text.inc}
  1135. {$endif FPC_HAS_FEATURE_TEXTIO}
  1136. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1137. { Untyped file }
  1138. {$i file.inc}
  1139. { Typed file }
  1140. {$i typefile.inc}
  1141. {$endif FPC_HAS_FEATURE_FILEIO}
  1142. {*****************************************************************************
  1143. Directory Handling
  1144. *****************************************************************************}
  1145. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1146. { OS dependent dir functions }
  1147. {$i sysdir.inc}
  1148. {$endif FPC_HAS_FEATURE_FILEIO}
  1149. {$if defined(FPC_HAS_FEATURE_FILEIO) and defined(FPC_HAS_FEATURE_ANSISTRINGS)}
  1150. Procedure getdir(drivenr:byte;Var dir:ansistring);
  1151. { this is needed to also allow ansistrings, the shortstring version is
  1152. OS dependent }
  1153. var
  1154. s : shortstring;
  1155. begin
  1156. getdir(drivenr,s);
  1157. dir:=s;
  1158. end;
  1159. {$endif}
  1160. {$if defined(FPC_HAS_FEATURE_FILEIO)}
  1161. Procedure MkDir(Const s: String);
  1162. Var
  1163. Buffer: Array[0..255] of Char;
  1164. Begin
  1165. If (s='') or (InOutRes <> 0) then
  1166. exit;
  1167. Move(s[1], Buffer, Length(s));
  1168. Buffer[Length(s)] := #0;
  1169. MkDir(@buffer[0],length(s));
  1170. End;
  1171. Procedure RmDir(Const s: String);
  1172. Var
  1173. Buffer: Array[0..255] of Char;
  1174. Begin
  1175. If (s='') or (InOutRes <> 0) then
  1176. exit;
  1177. Move(s[1], Buffer, Length(s));
  1178. Buffer[Length(s)] := #0;
  1179. RmDir(@buffer[0],length(s));
  1180. End;
  1181. Procedure ChDir(Const s: String);
  1182. Var
  1183. Buffer: Array[0..255] of Char;
  1184. Begin
  1185. If (s='') or (InOutRes <> 0) then
  1186. exit;
  1187. Move(s[1], Buffer, Length(s));
  1188. Buffer[Length(s)] := #0;
  1189. ChDir(@buffer[0],length(s));
  1190. End;
  1191. {$endif}
  1192. {*****************************************************************************
  1193. Resources support
  1194. *****************************************************************************}
  1195. {$i sysres.inc}
  1196. const
  1197. CtrlBreakHandler: TCtrlBreakHandler = nil;
  1198. {$IFNDEF FPC_HAS_SETCTRLBREAKHANDLER}
  1199. (* It is possible to provide platform specific implementation performing *)
  1200. (* special initialization; default implementation just sets the procedural *)
  1201. (* variable to make it available for use from the exception handler. *)
  1202. function SysSetCtrlBreakHandler (Handler: TCtrlBreakHandler): TCtrlBreakHandler;
  1203. begin
  1204. (* Return either nil or previous handler *)
  1205. SysSetCtrlBreakHandler := CtrlBreakHandler;
  1206. CtrlBreakHandler := Handler;
  1207. end;
  1208. {$ENDIF FPC_HAS_SETCTRLBREAKHANDLER}