system.inc 38 KB

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