system.inc 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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. {$ifndef BEOS}
  118. { prefer libc implementations over our own, as they're most likely faster }
  119. {$i cgeneric.inc}
  120. { is now declared as external reference to another routine in the interface }
  121. {$i cgenstr.inc}
  122. {$endif}
  123. {$endif FPC_USE_LIBC}
  124. {$ifdef cpui386}
  125. {$ifdef SYSPROCDEFINED}
  126. {$Error Can't determine processor type !}
  127. {$endif}
  128. {$i i386.inc} { Case dependent, don't change }
  129. {$endif cpui386}
  130. {$ifdef cpum68k}
  131. {$ifdef SYSPROCDEFINED}
  132. {$Error Can't determine processor type !}
  133. {$endif}
  134. {$i m68k.inc} { Case dependent, don't change }
  135. {$define SYSPROCDEFINED}
  136. {$endif cpum68k}
  137. {$ifdef cpux86_64}
  138. {$ifdef SYSPROCDEFINED}
  139. {$Error Can't determine processor type !}
  140. {$endif}
  141. {$i x86_64.inc} { Case dependent, don't change }
  142. {$define SYSPROCDEFINED}
  143. {$endif cpux86_64}
  144. {$ifdef cpupowerpc32}
  145. {$ifdef SYSPROCDEFINED}
  146. {$Error Can't determine processor type !}
  147. {$endif}
  148. {$i powerpc.inc} { Case dependent, don't change }
  149. {$define SYSPROCDEFINED}
  150. {$endif cpupowerpc32}
  151. {$ifdef cpupowerpc64}
  152. {$ifdef SYSPROCDEFINED}
  153. {$Error Can't determine processor type !}
  154. {$endif}
  155. {$i powerpc64.inc} { Case dependent, don't change }
  156. {$define SYSPROCDEFINED}
  157. {$endif cpupowerpc64}
  158. {$ifdef cpualpha}
  159. {$ifdef SYSPROCDEFINED}
  160. {$Error Can't determine processor type !}
  161. {$endif}
  162. {$i alpha.inc} { Case dependent, don't change }
  163. {$define SYSPROCDEFINED}
  164. {$endif cpualpha}
  165. {$ifdef cpuiA64}
  166. {$ifdef SYSPROCDEFINED}
  167. {$Error Can't determine processor type !}
  168. {$endif}
  169. {$i ia64.inc} { Case dependent, don't change }
  170. {$define SYSPROCDEFINED}
  171. {$endif cpuiA64}
  172. {$ifdef cpusparc}
  173. {$ifdef SYSPROCDEFINED}
  174. {$Error Can't determine processor type !}
  175. {$endif}
  176. {$i sparc.inc} { Case dependent, don't change }
  177. {$define SYSPROCDEFINED}
  178. {$endif cpusparc}
  179. {$ifdef cpuarm}
  180. {$ifdef SYSPROCDEFINED}
  181. {$Error Can't determine processor type !}
  182. {$endif}
  183. {$if defined(CPUCORTEXM3) or defined(CPUARMV7M)}
  184. {$i thumb2.inc} { Case dependent, don't change }
  185. {$else}
  186. {$i arm.inc} { Case dependent, don't change }
  187. {$endif}
  188. {$define SYSPROCDEFINED}
  189. {$endif cpuarm}
  190. {$ifdef cpuavr}
  191. {$ifdef SYSPROCDEFINED}
  192. {$Error Can't determine processor type !}
  193. {$endif}
  194. {$i avr.inc} { Case dependent, don't change }
  195. {$define SYSPROCDEFINED}
  196. {$endif cpuavr}
  197. procedure fillchar(var x;count : SizeInt;value : boolean);
  198. begin
  199. fillchar(x,count,byte(value));
  200. end;
  201. procedure fillchar(var x;count : SizeInt;value : char);
  202. begin
  203. fillchar(x,count,byte(value));
  204. end;
  205. procedure FillByte (var x;count : SizeInt;value : byte );
  206. begin
  207. FillChar (X,Count,VALUE);
  208. end;
  209. function IndexChar(Const buf;len:SizeInt;b:char):SizeInt;
  210. begin
  211. IndexChar:=IndexByte(Buf,Len,byte(B));
  212. end;
  213. function CompareChar(Const buf1,buf2;len:SizeInt):SizeInt;
  214. begin
  215. CompareChar:=CompareByte(buf1,buf2,len);
  216. end;
  217. { Include generic pascal only routines which are not defined in the processor
  218. specific include file }
  219. {$I generic.inc}
  220. {****************************************************************************
  221. Set Handling
  222. ****************************************************************************}
  223. { Include set support which is processor specific}
  224. {$i set.inc}
  225. { Include generic pascal routines for sets if the processor }
  226. { specific routines are not available. }
  227. {$i genset.inc}
  228. {****************************************************************************
  229. Math Routines
  230. ****************************************************************************}
  231. function Hi(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  232. begin
  233. Hi := b shr 4
  234. end;
  235. function Lo(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  236. begin
  237. Lo := b and $0f
  238. end;
  239. Function Swap (X : Word) : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  240. Begin
  241. Swap := SwapEndian(X);
  242. End;
  243. Function Swap (X : Integer) : Integer;{$ifdef SYSTEMINLINE}inline;{$endif}
  244. Begin
  245. Swap := SwapEndian(X);
  246. End;
  247. Function Swap (X : Longint) : Longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  248. Begin
  249. Swap:=(X and $ffff) shl 16 + (X shr 16)
  250. End;
  251. Function Swap (X : Cardinal) : Cardinal;{$ifdef SYSTEMINLINE}inline;{$endif}
  252. Begin
  253. Swap:=(X and $ffff) shl 16 + (X shr 16)
  254. End;
  255. Function Swap (X : QWord) : QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  256. Begin
  257. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  258. End;
  259. Function swap (X : Int64) : Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  260. Begin
  261. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  262. End;
  263. {$ifdef SUPPORT_DOUBLE}
  264. operator := (b:real48) d:double;{$ifdef SYSTEMINLINE}inline;{$endif}
  265. begin
  266. D:=real2double(b);
  267. end;
  268. {$endif SUPPORT_DOUBLE}
  269. {$ifdef SUPPORT_EXTENDED}
  270. operator := (b:real48) e:extended;{$ifdef SYSTEMINLINE}inline;{$endif}
  271. begin
  272. e:=real2double(b);
  273. end;
  274. {$endif SUPPORT_EXTENDED}
  275. {$ifndef FPUNONE}
  276. {$ifdef FPC_USE_LIBC}
  277. { Include libc versions }
  278. {$i cgenmath.inc}
  279. {$endif FPC_USE_LIBC}
  280. { Include processor specific routines }
  281. {$I math.inc}
  282. { Include generic version }
  283. {$I genmath.inc}
  284. {$endif}
  285. {$i gencurr.inc}
  286. function aligntoptr(p : pointer) : pointer;inline;
  287. begin
  288. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  289. result:=align(p,sizeof(p));
  290. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  291. result:=p;
  292. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  293. end;
  294. {****************************************************************************
  295. Subroutines for String handling
  296. ****************************************************************************}
  297. { Needs to be before RTTI handling }
  298. {$i sstrings.inc}
  299. { requires sstrings.inc for initval }
  300. {$I int64p.inc}
  301. {$I int64.inc}
  302. {Requires int64.inc, since that contains the VAL functions for int64 and qword}
  303. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  304. {$i astrings.inc}
  305. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  306. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  307. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  308. {$i wstrings.inc}
  309. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  310. {$i ustrings.inc}
  311. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  312. {$i aliases.inc}
  313. {*****************************************************************************
  314. Dynamic Array support
  315. *****************************************************************************}
  316. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  317. {$i dynarr.inc}
  318. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  319. {*****************************************************************************
  320. Object Pascal support
  321. *****************************************************************************}
  322. {$ifdef FPC_HAS_FEATURE_CLASSES}
  323. {$i objpas.inc}
  324. {$endif FPC_HAS_FEATURE_CLASSES}
  325. {*****************************************************************************
  326. Variant support
  327. *****************************************************************************}
  328. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  329. {$i variant.inc}
  330. {$endif FPC_HAS_FEATURE_VARIANTS}
  331. {****************************************************************************
  332. Run-Time Type Information (RTTI)
  333. ****************************************************************************}
  334. {$ifdef FPC_HAS_FEATURE_RTTI}
  335. {$i rtti.inc}
  336. {$endif FPC_HAS_FEATURE_RTTI}
  337. {$if defined(FPC_HAS_FEATURE_RANDOM)}
  338. {----------------------------------------------------------------------
  339. Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  340. Pseudo-Random Number Generator.
  341. What is Mersenne Twister?
  342. Mersenne Twister(MT) is a pseudorandom number generator developped by
  343. Makoto Matsumoto and Takuji Nishimura (alphabetical order) during
  344. 1996-1997. MT has the following merits:
  345. It is designed with consideration on the flaws of various existing
  346. generators.
  347. Far longer period and far higher order of equidistribution than any
  348. other implemented generators. (It is proved that the period is 2^19937-1,
  349. and 623-dimensional equidistribution property is assured.)
  350. Fast generation. (Although it depends on the system, it is reported that
  351. MT is sometimes faster than the standard ANSI-C library in a system
  352. with pipeline and cache memory.)
  353. Efficient use of the memory. (The implemented C-code mt19937.c
  354. consumes only 624 words of working area.)
  355. home page
  356. http://www.math.keio.ac.jp/~matumoto/emt.html
  357. original c source
  358. http://www.math.keio.ac.jp/~nisimura/random/int/mt19937int.c
  359. Coded by Takuji Nishimura, considering the suggestions by
  360. Topher Cooper and Marc Rieffel in July-Aug. 1997.
  361. This library is free software; you can redistribute it and/or
  362. modify it under the terms of the GNU Library General Public
  363. License as published by the Free Software Foundation; either
  364. version 2 of the License, or (at your option) any later
  365. version.
  366. This library is distributed in the hope that it will be useful,
  367. but WITHOUT ANY WARRANTY; without even the implied warranty of
  368. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  369. See the GNU Library General Public License for more details.
  370. You should have received a copy of the GNU Library General
  371. Public License along with this library; if not, write to the
  372. Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  373. 02111-1307 USA
  374. Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura.
  375. When you use this, send an email to: [email protected]
  376. with an appropriate reference to your work.
  377. REFERENCE
  378. M. Matsumoto and T. Nishimura,
  379. "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  380. Pseudo-Random Number Generator",
  381. ACM Transactions on Modeling and Computer Simulation,
  382. Vol. 8, No. 1, January 1998, pp 3--30.
  383. Translated to OP and Delphi interface added by Roman Krejci (6.12.1999)
  384. http://www.rksolution.cz/delphi/tips.htm
  385. Revised 21.6.2000: Bug in the function RandInt_MT19937 fixed
  386. 2003/10/26: adapted to use the improved intialisation mentioned at
  387. <http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html> and
  388. removed the assembler code
  389. ----------------------------------------------------------------------}
  390. {$R-} {range checking off}
  391. {$Q-} {overflow checking off}
  392. { Period parameter }
  393. Const
  394. MT19937N=624;
  395. Type
  396. tMT19937StateArray = array [0..MT19937N-1] of longint; // the array for the state vector
  397. { Period parameters }
  398. const
  399. MT19937M=397;
  400. MT19937MATRIX_A =$9908b0df; // constant vector a
  401. MT19937UPPER_MASK=longint($80000000); // most significant w-r bits
  402. MT19937LOWER_MASK=longint($7fffffff); // least significant r bits
  403. { Tempering parameters }
  404. TEMPERING_MASK_B=longint($9d2c5680);
  405. TEMPERING_MASK_C=longint($efc60000);
  406. VAR
  407. mt : tMT19937StateArray;
  408. const
  409. mti: longint=MT19937N+1; // mti=MT19937N+1 means mt[] is not initialized
  410. { Initializing the array with a seed }
  411. procedure sgenrand_MT19937(seed: longint);
  412. var
  413. i: longint;
  414. begin
  415. mt[0] := seed;
  416. for i := 1 to MT19937N-1 do
  417. begin
  418. mt[i] := 1812433253 * (mt[i-1] xor (mt[i-1] shr 30)) + i;
  419. { See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. }
  420. { In the previous versions, MSBs of the seed affect }
  421. { only MSBs of the array mt[]. }
  422. { 2002/01/09 modified by Makoto Matsumoto }
  423. end;
  424. mti := MT19937N;
  425. end;
  426. function genrand_MT19937: longint;
  427. const
  428. mag01 : array [0..1] of longint =(0, longint(MT19937MATRIX_A));
  429. var
  430. y: longint;
  431. kk: longint;
  432. begin
  433. if RandSeed<>OldRandSeed then
  434. mti:=MT19937N+1;
  435. if (mti >= MT19937N) { generate MT19937N longints at one time }
  436. then begin
  437. if mti = (MT19937N+1) then // if sgenrand_MT19937() has not been called,
  438. begin
  439. sgenrand_MT19937(randseed); // default initial seed is used
  440. { hack: randseed is not used more than once in this algorithm. Most }
  441. { user changes are re-initialising reandseed with the value it had }
  442. { at the start -> with the "not", we will detect this change. }
  443. { Detecting other changes is not useful, since the generated }
  444. { numbers will be different anyway. }
  445. randseed := not(randseed);
  446. oldrandseed := randseed;
  447. end;
  448. for kk:=0 to MT19937N-MT19937M-1 do begin
  449. y := (mt[kk] and MT19937UPPER_MASK) or (mt[kk+1] and MT19937LOWER_MASK);
  450. mt[kk] := mt[kk+MT19937M] xor (y shr 1) xor mag01[y and $00000001];
  451. end;
  452. for kk:= MT19937N-MT19937M to MT19937N-2 do begin
  453. y := (mt[kk] and MT19937UPPER_MASK) or (mt[kk+1] and MT19937LOWER_MASK);
  454. mt[kk] := mt[kk+(MT19937M-MT19937N)] xor (y shr 1) xor mag01[y and $00000001];
  455. end;
  456. y := (mt[MT19937N-1] and MT19937UPPER_MASK) or (mt[0] and MT19937LOWER_MASK);
  457. mt[MT19937N-1] := mt[MT19937M-1] xor (y shr 1) xor mag01[y and $00000001];
  458. mti := 0;
  459. end;
  460. y := mt[mti]; inc(mti);
  461. y := y xor (y shr 11);
  462. y := y xor (y shl 7) and TEMPERING_MASK_B;
  463. y := y xor (y shl 15) and TEMPERING_MASK_C;
  464. y := y xor (y shr 18);
  465. Result := y;
  466. end;
  467. function random(l:longint): longint;
  468. begin
  469. { otherwise we can return values = l (JM) }
  470. if (l < 0) then
  471. inc(l);
  472. random := longint((int64(cardinal(genrand_MT19937))*l) shr 32);
  473. end;
  474. function random(l:int64): int64;
  475. begin
  476. { always call random, so the random generator cycles (TP-compatible) (JM) }
  477. random := int64((qword(cardinal(genrand_MT19937)) or ((qword(cardinal(genrand_MT19937)) shl 32))) and $7fffffffffffffff);
  478. if (l<>0) then
  479. random := random mod l
  480. else
  481. random := 0;
  482. end;
  483. {$ifndef FPUNONE}
  484. function random: extended;
  485. begin
  486. random := cardinal(genrand_MT19937) * (extended(1.0)/(int64(1) shl 32));
  487. end;
  488. {$endif}
  489. {$endif FPC_HAS_FEATURE_RANDOM}
  490. {****************************************************************************
  491. Memory Management
  492. ****************************************************************************}
  493. Function Ptr(sel,off : Longint) : farpointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  494. Begin
  495. ptr:=farpointer((sel shl 4)+off);
  496. End;
  497. Function CSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  498. Begin
  499. Cseg:=0;
  500. End;
  501. Function DSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  502. Begin
  503. Dseg:=0;
  504. End;
  505. Function SSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  506. Begin
  507. Sseg:=0;
  508. End;
  509. {$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. var
  692. pstdout : ^Text;
  693. begin
  694. { Show runtime error and exit }
  695. pstdout:=@stdout;
  696. If erroraddr<>nil Then
  697. Begin
  698. Writeln(pstdout^,'Runtime error ',Errorcode,' at $',hexstr(erroraddr));
  699. { to get a nice symify }
  700. Writeln(pstdout^,BackTraceStrFunc(Erroraddr));
  701. dump_stack(pstdout^,ErrorBase);
  702. Writeln(pstdout^,'');
  703. End;
  704. { Make sure that all output is written to the redirected file }
  705. if Textrec(Output).Mode=fmOutput then
  706. Flush(Output);
  707. if Textrec(ErrOutput).Mode=fmOutput then
  708. Flush(ErrOutput);
  709. if Textrec(pstdout^).Mode=fmOutput then
  710. Flush(pstdout^);
  711. if Textrec(StdErr).Mode=fmOutput then
  712. Flush(StdErr);
  713. end;
  714. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  715. Procedure InternalExit;
  716. var
  717. current_exit : Procedure;
  718. {$if defined(MSWINDOWS) or defined(OS2)}
  719. i : longint;
  720. {$endif}
  721. Begin
  722. {$ifdef SYSTEMDEBUG}
  723. writeln('InternalExit');
  724. {$endif SYSTEMDEBUG}
  725. while exitProc<>nil Do
  726. Begin
  727. InOutRes:=0;
  728. current_exit:=tProcedure(exitProc);
  729. exitProc:=nil;
  730. current_exit();
  731. End;
  732. { Finalize units }
  733. FinalizeUnits;
  734. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  735. SysFlushStdIO;
  736. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  737. {$if defined(MSWINDOWS) or defined(OS2)}
  738. { finally release the heap if possible, especially
  739. important for DLLs.
  740. Reset the array to nil, and finally also argv itself to
  741. avoid double freeing problem in case this function gets called twice. }
  742. if assigned(argv) then
  743. begin
  744. for i:=0 to argc-1 do
  745. if assigned(argv[i]) then
  746. begin
  747. sysfreemem(argv[i]);
  748. argv[i]:=nil;
  749. end;
  750. sysfreemem(argv);
  751. argv:=nil;
  752. end;
  753. {$endif}
  754. {$ifdef LINUX}
  755. {sysfreemem already checks for nil}
  756. sysfreemem(calculated_cmdline);
  757. {$endif}
  758. {$ifdef BSD}
  759. sysfreemem(cmdline);
  760. {$endif}
  761. {$ifdef FPC_HAS_FEATURE_HEAP}
  762. {$ifndef HAS_MEMORYMANAGER}
  763. FinalizeHeap;
  764. {$endif HAS_MEMORYMANAGER}
  765. {$endif FPC_HAS_FEATURE_HEAP}
  766. End;
  767. Procedure do_exit;[Public,Alias:'FPC_DO_EXIT'];
  768. begin
  769. InternalExit;
  770. System_exit;
  771. end;
  772. Procedure lib_exit;[Public,Alias:'FPC_LIB_EXIT'];
  773. begin
  774. InternalExit;
  775. end;
  776. Procedure Halt(ErrNum: Longint);
  777. Begin
  778. ExitCode:=Errnum;
  779. Do_Exit;
  780. end;
  781. function SysBackTraceStr (Addr: Pointer): ShortString;
  782. begin
  783. SysBackTraceStr:=' $'+hexstr(addr);
  784. end;
  785. Procedure HandleErrorAddrFrame (Errno : longint;addr,frame : Pointer);[public,alias:'FPC_BREAK_ERROR']; {$ifdef CPU86} register; {$endif}
  786. begin
  787. If pointer(ErrorProc)<>Nil then
  788. ErrorProc(Errno,addr,frame);
  789. errorcode:=word(Errno);
  790. erroraddr:=addr;
  791. errorbase:=frame;
  792. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  793. if ExceptAddrStack <> nil then
  794. raise TObject(nil) at addr,frame;
  795. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  796. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  797. {$ifdef FPC_LIMITED_EXITCODE}
  798. if errorcode > maxExitCode then
  799. halt(255)
  800. else
  801. {$endif FPC_LIMITED_EXITCODE}
  802. halt(errorcode);
  803. {$else FPC_HAS_FEATURE_EXITCODE}
  804. halt;
  805. {$endif FPC_HAS_FEATURE_EXITCODE}
  806. end;
  807. Procedure HandleErrorFrame (Errno : longint;frame : Pointer);
  808. {
  809. Procedure to handle internal errors, i.e. not user-invoked errors
  810. Internal function should ALWAYS call HandleError instead of RunError.
  811. Can be used for exception handlers to specify the frame
  812. }
  813. begin
  814. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  815. end;
  816. Procedure HandleError (Errno : longint);[public,alias : 'FPC_HANDLEERROR'];
  817. {
  818. Procedure to handle internal errors, i.e. not user-invoked errors
  819. Internal function should ALWAYS call HandleError instead of RunError.
  820. }
  821. begin
  822. HandleErrorFrame(Errno,get_frame);
  823. end;
  824. procedure RunError(w : word);[alias: 'FPC_RUNERROR'];
  825. begin
  826. errorcode:=w;
  827. erroraddr:=get_caller_addr(get_frame);
  828. errorbase:=get_caller_frame(get_frame);
  829. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  830. {$ifdef FPC_LIMITED_EXITCODE}
  831. if errorcode > maxExitCode then
  832. halt(255)
  833. else
  834. {$endif FPC_LIMITED_EXITCODE}
  835. halt(errorcode);
  836. {$else FPC_HAS_FEATURE_EXITCODE}
  837. halt;
  838. {$endif FPC_HAS_FEATURE_EXITCODE}
  839. end;
  840. Procedure RunError;{$ifdef SYSTEMINLINE}inline;{$endif}
  841. Begin
  842. RunError (0);
  843. End;
  844. Procedure Halt;{$ifdef SYSTEMINLINE}inline;{$endif}
  845. Begin
  846. Halt(0);
  847. End;
  848. Procedure Error(RunTimeError : TRunTimeError);
  849. begin
  850. RunError(RuntimeErrorExitCodes[RunTimeError]);
  851. end;
  852. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  853. Procedure dump_stack(var f : text;bp : Pointer);
  854. var
  855. i : Longint;
  856. prevbp : Pointer;
  857. is_dev : boolean;
  858. caller_frame,
  859. caller_addr : Pointer;
  860. Begin
  861. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  862. try
  863. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  864. prevbp:=bp-1;
  865. i:=0;
  866. is_dev:=do_isdevice(textrec(f).Handle);
  867. while bp > prevbp Do
  868. Begin
  869. caller_addr := get_caller_addr(bp);
  870. caller_frame := get_caller_frame(bp);
  871. if (caller_addr=nil) then
  872. break;
  873. Writeln(f,BackTraceStrFunc(caller_addr));
  874. if (caller_frame=nil) then
  875. break;
  876. Inc(i);
  877. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  878. break;
  879. prevbp:=bp;
  880. bp:=caller_frame;
  881. End;
  882. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  883. except
  884. { prevent endless dump if an exception occured }
  885. end;
  886. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  887. End;
  888. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  889. procedure DumpExceptionBackTrace(var f:text);
  890. var
  891. FrameNumber,
  892. FrameCount : longint;
  893. Frames : PPointer;
  894. begin
  895. if RaiseList=nil then
  896. exit;
  897. WriteLn(f,BackTraceStrFunc(RaiseList^.Addr));
  898. FrameCount:=RaiseList^.Framecount;
  899. Frames:=RaiseList^.Frames;
  900. for FrameNumber := 0 to FrameCount-1 do
  901. WriteLn(f,BackTraceStrFunc(Frames[FrameNumber]));
  902. end;
  903. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  904. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  905. {$ifdef FPC_HAS_FEATURE_HEAP}
  906. Type
  907. PExitProcInfo = ^TExitProcInfo;
  908. TExitProcInfo = Record
  909. Next : PExitProcInfo;
  910. SaveExit : Pointer;
  911. Proc : TProcedure;
  912. End;
  913. const
  914. ExitProcList: PExitProcInfo = nil;
  915. Procedure DoExitProc;
  916. var
  917. P : PExitProcInfo;
  918. Proc : TProcedure;
  919. Begin
  920. P:=ExitProcList;
  921. ExitProcList:=P^.Next;
  922. ExitProc:=P^.SaveExit;
  923. Proc:=P^.Proc;
  924. DisPose(P);
  925. Proc();
  926. End;
  927. Procedure AddExitProc(Proc: TProcedure);
  928. var
  929. P : PExitProcInfo;
  930. Begin
  931. New(P);
  932. P^.Next:=ExitProcList;
  933. P^.SaveExit:=ExitProc;
  934. P^.Proc:=Proc;
  935. ExitProcList:=P;
  936. ExitProc:=@DoExitProc;
  937. End;
  938. {$endif FPC_HAS_FEATURE_HEAP}
  939. {$ifdef FPC_HAS_FEATURE_HEAP}
  940. function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):ppchar; // const ?
  941. // Extra allocate reserveentries pchar's at the beginning (default param=0 after 1.0.x ?)
  942. // Note: for internal use by skilled programmers only
  943. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  944. var p : ppchar;
  945. i : LongInt;
  946. begin
  947. if High(s)<Low(s) Then Exit(NIL);
  948. Getmem(p,sizeof(pchar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  949. // for cmd
  950. if p=nil then
  951. begin
  952. {$ifdef xunix}
  953. fpseterrno(ESysEnomem);
  954. {$endif}
  955. exit(NIL);
  956. end;
  957. for i:=low(s) to high(s) do
  958. p[i+Reserveentries]:=pchar(s[i]);
  959. p[high(s)+1+Reserveentries]:=nil;
  960. ArrayStringToPPchar:=p;
  961. end;
  962. Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):ppchar;
  963. {
  964. Create a PPChar to structure of pchars which are the arguments specified
  965. in the string S. Especially useful for creating an ArgV for Exec-calls
  966. }
  967. begin
  968. StringToPPChar:=StringToPPChar(PChar(S),ReserveEntries);
  969. end;
  970. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  971. var
  972. i,nr : longint;
  973. Buf : ^char;
  974. p : ppchar;
  975. begin
  976. buf:=s;
  977. nr:=1;
  978. while (buf^<>#0) do // count nr of args
  979. begin
  980. while (buf^ in [' ',#9,#10]) do // Kill separators.
  981. inc(buf);
  982. inc(nr);
  983. if buf^='"' Then // quotes argument?
  984. begin
  985. inc(buf);
  986. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  987. inc(buf);
  988. if buf^='"' then // skip closing quote.
  989. inc(buf);
  990. end
  991. else
  992. begin // else std
  993. while not (buf^ in [' ',#0,#9,#10]) do
  994. inc(buf);
  995. end;
  996. end;
  997. getmem(p,(ReserveEntries+nr)*sizeof(pchar));
  998. StringToPPChar:=p;
  999. if p=nil then
  1000. begin
  1001. {$ifdef xunix}
  1002. fpseterrno(ESysEnomem);
  1003. {$endif}
  1004. exit;
  1005. end;
  1006. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  1007. buf:=s;
  1008. while (buf^<>#0) do
  1009. begin
  1010. while (buf^ in [' ',#9,#10]) do // Kill separators.
  1011. begin
  1012. buf^:=#0;
  1013. inc(buf);
  1014. end;
  1015. if buf^='"' Then // quotes argument?
  1016. begin
  1017. inc(buf);
  1018. p^:=buf;
  1019. inc(p);
  1020. p^:=nil;
  1021. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1022. inc(buf);
  1023. if buf^='"' then // skip closing quote.
  1024. begin
  1025. buf^:=#0;
  1026. inc(buf);
  1027. end;
  1028. end
  1029. else
  1030. begin
  1031. p^:=buf;
  1032. inc(p);
  1033. p^:=nil;
  1034. while not (buf^ in [' ',#0,#9,#10]) do
  1035. inc(buf);
  1036. end;
  1037. end;
  1038. end;
  1039. {$endif FPC_HAS_FEATURE_HEAP}
  1040. {*****************************************************************************
  1041. Abstract/Assert support.
  1042. *****************************************************************************}
  1043. procedure fpc_AbstractErrorIntern;compilerproc;[public,alias : 'FPC_ABSTRACTERROR'];
  1044. begin
  1045. If pointer(AbstractErrorProc)<>nil then
  1046. AbstractErrorProc();
  1047. HandleErrorFrame(211,get_frame);
  1048. end;
  1049. Procedure fpc_assert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer); [Public,Alias : 'FPC_ASSERT']; compilerproc;
  1050. begin
  1051. if pointer(AssertErrorProc)<>nil then
  1052. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  1053. else
  1054. HandleErrorFrame(227,get_frame);
  1055. end;
  1056. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
  1057. begin
  1058. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1059. If msg='' then
  1060. write(stderr,'Assertion failed')
  1061. else
  1062. write(stderr,msg);
  1063. Writeln(stderr,' (',FName,', line ',LineNo,').');
  1064. Writeln(stderr,'');
  1065. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  1066. Halt(227);
  1067. {$else FPC_HAS_FEATURE_EXITCODE}
  1068. halt;
  1069. {$endif FPC_HAS_FEATURE_EXITCODE}
  1070. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1071. end;
  1072. {*****************************************************************************
  1073. SetJmp/LongJmp support.
  1074. *****************************************************************************}
  1075. {$i setjump.inc}
  1076. {$ifdef IOCheckWasOn}
  1077. {$I+}
  1078. {$endif}
  1079. {$ifdef RangeCheckWasOn}
  1080. {$R+}
  1081. {$endif}
  1082. {$ifdef OverflowCheckWasOn}
  1083. {$Q+}
  1084. {$endif}
  1085. {*****************************************************************************
  1086. Heap
  1087. *****************************************************************************}
  1088. {$ifdef FPC_HAS_FEATURE_HEAP}
  1089. {$i sysheap.inc}
  1090. {$i heap.inc}
  1091. {$endif FPC_HAS_FEATURE_HEAP}
  1092. {*****************************************************************************
  1093. Thread support
  1094. *****************************************************************************}
  1095. {$ifdef FPC_HAS_FEATURE_THREADING}
  1096. { Generic threadmanager }
  1097. {$i thread.inc}
  1098. { Generic threadvar support }
  1099. {$i threadvr.inc}
  1100. {$ifdef DISABLE_NO_THREAD_MANAGER}
  1101. { OS Dependent implementation }
  1102. {$i systhrd.inc}
  1103. {$endif DISABLE_NO_THREAD_MANAGER}
  1104. {$endif FPC_HAS_FEATURE_THREADING}
  1105. {*****************************************************************************
  1106. File Handling
  1107. *****************************************************************************}
  1108. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1109. { Allow slash and backslash as separators }
  1110. procedure DoDirSeparators(p:Pchar);
  1111. var
  1112. i : longint;
  1113. begin
  1114. for i:=0 to strlen(p) do
  1115. if p[i] in AllowDirectorySeparators then
  1116. p[i]:=DirectorySeparator;
  1117. end;
  1118. procedure DoDirSeparators(var p:shortstring);
  1119. var
  1120. i : longint;
  1121. begin
  1122. for i:=1 to length(p) do
  1123. if p[i] in AllowDirectorySeparators then
  1124. p[i]:=DirectorySeparator;
  1125. end;
  1126. {$endif FPC_HAS_FEATURE_FILEIO}
  1127. { OS dependent low level file functions }
  1128. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1129. {$i sysfile.inc}
  1130. {$endif FPC_HAS_FEATURE_FILEIO}
  1131. { Text file }
  1132. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  1133. {$i text.inc}
  1134. {$endif FPC_HAS_FEATURE_TEXTIO}
  1135. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1136. { Untyped file }
  1137. {$i file.inc}
  1138. { Typed file }
  1139. {$i typefile.inc}
  1140. {$endif FPC_HAS_FEATURE_FILEIO}
  1141. {*****************************************************************************
  1142. Directory Handling
  1143. *****************************************************************************}
  1144. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1145. { OS dependent dir functions }
  1146. {$i sysdir.inc}
  1147. {$endif FPC_HAS_FEATURE_FILEIO}
  1148. {$if defined(FPC_HAS_FEATURE_FILEIO) and defined(FPC_HAS_FEATURE_ANSISTRINGS)}
  1149. Procedure getdir(drivenr:byte;Var dir:ansistring);
  1150. { this is needed to also allow ansistrings, the shortstring version is
  1151. OS dependent }
  1152. var
  1153. s : shortstring;
  1154. begin
  1155. getdir(drivenr,s);
  1156. dir:=s;
  1157. end;
  1158. {$endif}
  1159. {$if defined(FPC_HAS_FEATURE_FILEIO)}
  1160. Procedure MkDir(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. MkDir(@buffer[0],length(s));
  1169. End;
  1170. Procedure RmDir(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. RmDir(@buffer[0],length(s));
  1179. End;
  1180. Procedure ChDir(Const s: String);
  1181. Var
  1182. Buffer: Array[0..255] of Char;
  1183. Begin
  1184. If (s='') or (InOutRes <> 0) then
  1185. exit;
  1186. Move(s[1], Buffer, Length(s));
  1187. Buffer[Length(s)] := #0;
  1188. ChDir(@buffer[0],length(s));
  1189. End;
  1190. {$endif}
  1191. {*****************************************************************************
  1192. Resources support
  1193. *****************************************************************************}
  1194. {$i sysres.inc}
  1195. const
  1196. CtrlBreakHandler: TCtrlBreakHandler = nil;
  1197. {$IFNDEF FPC_HAS_SETCTRLBREAKHANDLER}
  1198. (* It is possible to provide platform specific implementation performing *)
  1199. (* special initialization; default implementation just sets the procedural *)
  1200. (* variable to make it available for use from the exception handler. *)
  1201. function SysSetCtrlBreakHandler (Handler: TCtrlBreakHandler): TCtrlBreakHandler;
  1202. begin
  1203. (* Return either nil or previous handler *)
  1204. SysSetCtrlBreakHandler := CtrlBreakHandler;
  1205. CtrlBreakHandler := Handler;
  1206. end;
  1207. {$ENDIF FPC_HAS_SETCTRLBREAKHANDLER}