system.inc 35 KB

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