system.inc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. {
  2. $Id: system.inc,v 1.78 2005/03/25 22:53:39 jonas Exp $
  3. This file is part of the Free Pascal Run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. For details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {****************************************************************************
  12. Local types
  13. ****************************************************************************}
  14. {
  15. TextRec and FileRec are put in a separate file to make it available to other
  16. units without putting it explicitly in systemh.
  17. This way we keep TP compatibility, and the TextRec definition is available
  18. for everyone who needs it.
  19. }
  20. {$i filerec.inc}
  21. {$i textrec.inc}
  22. Procedure HandleError (Errno : Longint); forward;
  23. Procedure HandleErrorFrame (Errno : longint;frame : Pointer); forward;
  24. type
  25. FileFunc = Procedure(var t : TextRec);
  26. const
  27. STACK_MARGIN = 16384; { Stack size margin for stack checking }
  28. { Random / Randomize constants }
  29. OldRandSeed : Cardinal = 0;
  30. { For Error Handling.}
  31. ErrorBase : Pointer = nil;
  32. { Used by the ansistrings and maybe also other things in the future }
  33. var
  34. emptychar : char;public name 'FPC_EMPTYCHAR';
  35. initialstklen : longint;external name '__stklen';
  36. {****************************************************************************
  37. Routines which have compiler magic
  38. ****************************************************************************}
  39. {$ifndef INTERNCONSTINTF}
  40. Function lo(i : Integer) : byte; [INTERNPROC: fpc_in_lo_Word];
  41. Function lo(w : Word) : byte; [INTERNPROC: fpc_in_lo_Word];
  42. Function lo(l : Longint) : Word; [INTERNPROC: fpc_in_lo_long];
  43. Function lo(l : DWord) : Word; [INTERNPROC: fpc_in_lo_long];
  44. Function hi(i : Integer) : byte; [INTERNPROC: fpc_in_hi_Word];
  45. Function hi(w : Word) : byte; [INTERNPROC: fpc_in_hi_Word];
  46. Function hi(l : Longint) : Word; [INTERNPROC: fpc_in_hi_long];
  47. Function hi(l : DWord) : Word; [INTERNPROC: fpc_in_hi_long];
  48. Function lo(q : QWord) : DWord; [INTERNPROC: fpc_in_lo_qword];
  49. Function lo(i : Int64) : DWord; [INTERNPROC: fpc_in_lo_qword];
  50. Function hi(q : QWord) : DWord; [INTERNPROC: fpc_in_hi_qword];
  51. Function hi(i : Int64) : DWord; [INTERNPROC: fpc_in_hi_qword];
  52. Function chr(b : byte) : Char; [INTERNPROC: fpc_in_chr_byte];
  53. {$ifndef INTERNLENGTH}
  54. Function Length(s : string) : byte; [INTERNPROC: fpc_in_Length_string];
  55. Function Length(c : char) : byte; [INTERNPROC: fpc_in_Length_string];
  56. {$endif INTERNLENGTH}
  57. Procedure Reset(var f : TypedFile); [INTERNPROC: fpc_in_Reset_TypedFile];
  58. Procedure Rewrite(var f : TypedFile); [INTERNPROC: fpc_in_Rewrite_TypedFile];
  59. {$endif INTERNCONSTINTF}
  60. {****************************************************************************
  61. Include processor specific routines
  62. ****************************************************************************}
  63. {$ifdef FPC_USE_LIBC}
  64. { prefer libc implementations over our own, as they're most likely faster }
  65. {$i cgeneric.inc}
  66. { is now declared as external reference to another routine in the interface }
  67. {$i cgenstr.inc}
  68. {$endif FPC_USE_LIBC}
  69. {$ifdef cpui386}
  70. {$ifdef SYSPROCDEFINED}
  71. {$Error Can't determine processor type !}
  72. {$endif}
  73. {$i i386.inc} { Case dependent, don't change }
  74. {$endif cpui386}
  75. {$ifdef cpum68k}
  76. {$ifdef SYSPROCDEFINED}
  77. {$Error Can't determine processor type !}
  78. {$endif}
  79. {$i m68k.inc} { Case dependent, don't change }
  80. {$define SYSPROCDEFINED}
  81. {$endif cpum68k}
  82. {$ifdef cpux86_64}
  83. {$ifdef SYSPROCDEFINED}
  84. {$Error Can't determine processor type !}
  85. {$endif}
  86. {$i x86_64.inc} { Case dependent, don't change }
  87. {$define SYSPROCDEFINED}
  88. {$endif cpux86_64}
  89. {$ifdef cpupowerpc}
  90. {$ifdef SYSPROCDEFINED}
  91. {$Error Can't determine processor type !}
  92. {$endif}
  93. {$i powerpc.inc} { Case dependent, don't change }
  94. {$define SYSPROCDEFINED}
  95. {$endif cpupowerpc}
  96. {$ifdef cpualpha}
  97. {$ifdef SYSPROCDEFINED}
  98. {$Error Can't determine processor type !}
  99. {$endif}
  100. {$i alpha.inc} { Case dependent, don't change }
  101. {$define SYSPROCDEFINED}
  102. {$endif cpualpha}
  103. {$ifdef cpuiA64}
  104. {$ifdef SYSPROCDEFINED}
  105. {$Error Can't determine processor type !}
  106. {$endif}
  107. {$i ia64.inc} { Case dependent, don't change }
  108. {$define SYSPROCDEFINED}
  109. {$endif cpuiA64}
  110. {$ifdef cpusparc}
  111. {$ifdef SYSPROCDEFINED}
  112. {$Error Can't determine processor type !}
  113. {$endif}
  114. {$i sparc.inc} { Case dependent, don't change }
  115. {$define SYSPROCDEFINED}
  116. {$endif cpusparc}
  117. {$ifdef cpuarm}
  118. {$ifdef SYSPROCDEFINED}
  119. {$Error Can't determine processor type !}
  120. {$endif}
  121. {$i arm.inc} { Case dependent, don't change }
  122. {$define SYSPROCDEFINED}
  123. {$endif cpuarm}
  124. procedure fillchar(var x;count : SizeInt;value : boolean);{$ifdef SYSTEMINLINE}inline;{$endif}
  125. begin
  126. fillchar(x,count,byte(value));
  127. end;
  128. procedure fillchar(var x;count : SizeInt;value : char);{$ifdef SYSTEMINLINE}inline;{$endif}
  129. begin
  130. fillchar(x,count,byte(value));
  131. end;
  132. { Include generic pascal only routines which are not defined in the processor
  133. specific include file }
  134. {$I generic.inc}
  135. {****************************************************************************
  136. Set Handling
  137. ****************************************************************************}
  138. { Include set support which is processor specific}
  139. {$i set.inc}
  140. { Include generic pascal routines for sets if the processor }
  141. { specific routines are not available. }
  142. {$i genset.inc}
  143. {****************************************************************************
  144. Math Routines
  145. ****************************************************************************}
  146. function Hi(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  147. begin
  148. Hi := b shr 4
  149. end;
  150. function Lo(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  151. begin
  152. Lo := b and $0f
  153. end;
  154. Function swap (X : Word) : Word;{$ifdef SYSTEMINLINE}inline;{$endif}{$ifndef INTERNCONSTINTF}[internconst:fpc_in_const_swap_word];{$endif}
  155. Begin
  156. swap:=(X and $ff) shl 8 + (X shr 8)
  157. End;
  158. Function Swap (X : Integer) : Integer;{$ifdef SYSTEMINLINE}inline;{$endif}{$ifndef INTERNCONSTINTF}[internconst:fpc_in_const_swap_word];{$endif}
  159. Begin
  160. swap:=(X and $ff) shl 8 + (X shr 8)
  161. End;
  162. Function swap (X : Longint) : Longint;{$ifdef SYSTEMINLINE}inline;{$endif}{$ifndef INTERNCONSTINTF}[internconst:fpc_in_const_swap_long];{$endif}
  163. Begin
  164. Swap:=(X and $ffff) shl 16 + (X shr 16)
  165. End;
  166. Function Swap (X : Cardinal) : Cardinal;{$ifdef SYSTEMINLINE}inline;{$endif}{$ifndef INTERNCONSTINTF}[internconst:fpc_in_const_swap_long];{$endif}
  167. Begin
  168. Swap:=(X and $ffff) shl 16 + (X shr 16)
  169. End;
  170. Function Swap (X : QWord) : QWord;{$ifdef SYSTEMINLINE}inline;{$endif}{$ifndef INTERNCONSTINTF}[internconst:fpc_in_const_swap_qword];{$endif}
  171. Begin
  172. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  173. End;
  174. Function swap (X : Int64) : Int64;{$ifdef SYSTEMINLINE}inline;{$endif}{$ifndef INTERNCONSTINTF}[internconst:fpc_in_const_swap_qword];{$endif}
  175. Begin
  176. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  177. End;
  178. {$ifdef SUPPORT_DOUBLE}
  179. operator := (b:real48) d:double;
  180. begin
  181. D:=real2double(b);
  182. end;
  183. {$endif SUPPORT_DOUBLE}
  184. {$ifdef SUPPORT_EXTENDED}
  185. operator := (b:real48) e:extended;
  186. begin
  187. e:=real2double(b);
  188. end;
  189. {$endif SUPPORT_EXTENDED}
  190. {$ifdef FPC_USE_LIBC}
  191. { Include libc versions }
  192. {$i cgenmath.inc}
  193. {$endif FPC_USE_LIBC}
  194. { Include processor specific routines }
  195. {$I math.inc}
  196. { Include generic version }
  197. {$I genmath.inc}
  198. {****************************************************************************
  199. Subroutines for String handling
  200. ****************************************************************************}
  201. { Needs to be before RTTI handling }
  202. {$i sstrings.inc}
  203. { requires sstrings.inc for initval }
  204. {$I int64p.inc}
  205. {$I int64.inc}
  206. {Requires int64.inc, since that contains the VAL functions for int64 and qword}
  207. {$i astrings.inc}
  208. {$ifdef HASWIDESTRING}
  209. {$i wstrings.inc}
  210. {$endif HASWIDESTRING}
  211. {$i aliases.inc}
  212. {*****************************************************************************
  213. Dynamic Array support
  214. *****************************************************************************}
  215. {$i dynarr.inc}
  216. {*****************************************************************************
  217. Object Pascal support
  218. *****************************************************************************}
  219. {$i objpas.inc}
  220. {*****************************************************************************
  221. Variant support
  222. *****************************************************************************}
  223. {$ifdef HASVARIANT}
  224. {$i variant.inc}
  225. {$endif HASVARIANT}
  226. {****************************************************************************
  227. Run-Time Type Information (RTTI)
  228. ****************************************************************************}
  229. {$i rtti.inc}
  230. {----------------------------------------------------------------------
  231. Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  232. Pseudo-Random Number Generator.
  233. What is Mersenne Twister?
  234. Mersenne Twister(MT) is a pseudorandom number generator developped by
  235. Makoto Matsumoto and Takuji Nishimura (alphabetical order) during
  236. 1996-1997. MT has the following merits:
  237. It is designed with consideration on the flaws of various existing
  238. generators.
  239. Far longer period and far higher order of equidistribution than any
  240. other implemented generators. (It is proved that the period is 2^19937-1,
  241. and 623-dimensional equidistribution property is assured.)
  242. Fast generation. (Although it depends on the system, it is reported that
  243. MT is sometimes faster than the standard ANSI-C library in a system
  244. with pipeline and cache memory.)
  245. Efficient use of the memory. (The implemented C-code mt19937.c
  246. consumes only 624 words of working area.)
  247. home page
  248. http://www.math.keio.ac.jp/~matumoto/emt.html
  249. original c source
  250. http://www.math.keio.ac.jp/~nisimura/random/int/mt19937int.c
  251. Coded by Takuji Nishimura, considering the suggestions by
  252. Topher Cooper and Marc Rieffel in July-Aug. 1997.
  253. This library is free software; you can redistribute it and/or
  254. modify it under the terms of the GNU Library General Public
  255. License as published by the Free Software Foundation; either
  256. version 2 of the License, or (at your option) any later
  257. version.
  258. This library is distributed in the hope that it will be useful,
  259. but WITHOUT ANY WARRANTY; without even the implied warranty of
  260. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  261. See the GNU Library General Public License for more details.
  262. You should have received a copy of the GNU Library General
  263. Public License along with this library; if not, write to the
  264. Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  265. 02111-1307 USA
  266. Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura.
  267. When you use this, send an email to: [email protected]
  268. with an appropriate reference to your work.
  269. REFERENCE
  270. M. Matsumoto and T. Nishimura,
  271. "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  272. Pseudo-Random Number Generator",
  273. ACM Transactions on Modeling and Computer Simulation,
  274. Vol. 8, No. 1, January 1998, pp 3--30.
  275. Translated to OP and Delphi interface added by Roman Krejci (6.12.1999)
  276. http://www.rksolution.cz/delphi/tips.htm
  277. Revised 21.6.2000: Bug in the function RandInt_MT19937 fixed
  278. 2003/10/26: adapted to use the improved intialisation mentioned at
  279. <http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html> and
  280. removed the assembler code
  281. ----------------------------------------------------------------------}
  282. {$R-} {range checking off}
  283. {$Q-} {overflow checking off}
  284. { Period parameter }
  285. Const
  286. MT19937N=624;
  287. Type
  288. tMT19937StateArray = array [0..MT19937N-1] of longint; // the array for the state vector
  289. { Period parameters }
  290. const
  291. MT19937M=397;
  292. MT19937MATRIX_A =$9908b0df; // constant vector a
  293. MT19937UPPER_MASK=$80000000; // most significant w-r bits
  294. MT19937LOWER_MASK=$7fffffff; // least significant r bits
  295. { Tempering parameters }
  296. TEMPERING_MASK_B=$9d2c5680;
  297. TEMPERING_MASK_C=$efc60000;
  298. VAR
  299. mt : tMT19937StateArray;
  300. const
  301. mti: longint=MT19937N+1; // mti=MT19937N+1 means mt[] is not initialized
  302. { Initializing the array with a seed }
  303. procedure sgenrand_MT19937(seed: longint);
  304. var
  305. i: longint;
  306. begin
  307. mt[0] := seed;
  308. for i := 1 to MT19937N-1 do
  309. begin
  310. mt[i] := 1812433253 * (mt[i-1] xor (mt[i-1] shr 30)) + i;
  311. { See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. }
  312. { In the previous versions, MSBs of the seed affect }
  313. { only MSBs of the array mt[]. }
  314. { 2002/01/09 modified by Makoto Matsumoto }
  315. end;
  316. mti := MT19937N;
  317. end;
  318. function genrand_MT19937: longint;
  319. const
  320. mag01 : array [0..1] of longint =(0, longint(MT19937MATRIX_A));
  321. var
  322. y: longint;
  323. kk: longint;
  324. begin
  325. if (mti >= MT19937N) or
  326. (randseed <> oldrandseed) { generate MT19937N longints at one time }
  327. then begin
  328. if mti = (MT19937N+1) then // if sgenrand_MT19937() has not been called,
  329. begin
  330. sgenrand_MT19937(randseed); // default initial seed is used
  331. { hack: randseed is not used more than once in this algorithm. Most }
  332. { user changes are re-initialising reandseed with the value it had }
  333. { at the start -> with the "not", we will detect this change. }
  334. { Detecting other changes is not useful, since the generated }
  335. { numbers will be different anyway. }
  336. randseed := not(randseed);
  337. oldrandseed := randseed;
  338. end;
  339. for kk:=0 to MT19937N-MT19937M-1 do begin
  340. y := (mt[kk] and MT19937UPPER_MASK) or (mt[kk+1] and MT19937LOWER_MASK);
  341. mt[kk] := mt[kk+MT19937M] xor (y shr 1) xor mag01[y and $00000001];
  342. end;
  343. for kk:= MT19937N-MT19937M to MT19937N-2 do begin
  344. y := (mt[kk] and MT19937UPPER_MASK) or (mt[kk+1] and MT19937LOWER_MASK);
  345. mt[kk] := mt[kk+(MT19937M-MT19937N)] xor (y shr 1) xor mag01[y and $00000001];
  346. end;
  347. y := (mt[MT19937N-1] and MT19937UPPER_MASK) or (mt[0] and MT19937LOWER_MASK);
  348. mt[MT19937N-1] := mt[MT19937M-1] xor (y shr 1) xor mag01[y and $00000001];
  349. mti := 0;
  350. end;
  351. y := mt[mti]; inc(mti);
  352. y := y xor (y shr 11);
  353. y := y xor (y shl 7) and TEMPERING_MASK_B;
  354. y := y xor (y shl 15) and TEMPERING_MASK_C;
  355. y := y xor (y shr 18);
  356. Result := y;
  357. end;
  358. function random(l:longint): longint;
  359. begin
  360. random := longint((int64(cardinal(genrand_MT19937))*l) shr 32);
  361. end;
  362. function random(l:int64): int64;
  363. begin
  364. random := int64((qword(cardinal(genrand_MT19937)) or ((qword(cardinal(genrand_MT19937)) shl 32))) and $7fffffffffffffff) mod l;
  365. end;
  366. function random: extended;
  367. begin
  368. random := cardinal(genrand_MT19937) * (1.0/(int64(1) shl 32));
  369. end;
  370. {****************************************************************************
  371. Memory Management
  372. ****************************************************************************}
  373. Function Ptr(sel,off : Longint) : farpointer;{$ifdef SYSTEMINLINE}inline;{$endif}{$ifndef INTERNCONSTINTF}[internconst:fpc_in_const_ptr];{$endif}
  374. Begin
  375. ptr:=farpointer((sel shl 4)+off);
  376. End;
  377. Function CSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  378. Begin
  379. Cseg:=0;
  380. End;
  381. Function DSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  382. Begin
  383. Dseg:=0;
  384. End;
  385. Function SSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  386. Begin
  387. Sseg:=0;
  388. End;
  389. {*****************************************************************************
  390. Directory support.
  391. *****************************************************************************}
  392. Procedure getdir(drivenr:byte;Var dir:ansistring);
  393. { this is needed to also allow ansistrings, the shortstring version is
  394. OS dependent }
  395. var
  396. s : shortstring;
  397. begin
  398. getdir(drivenr,s);
  399. dir:=s;
  400. end;
  401. {$ifopt R+}
  402. {$define RangeCheckWasOn}
  403. {$R-}
  404. {$endif opt R+}
  405. {$ifopt I+}
  406. {$define IOCheckWasOn}
  407. {$I-}
  408. {$endif opt I+}
  409. {$ifopt Q+}
  410. {$define OverflowCheckWasOn}
  411. {$Q-}
  412. {$endif opt Q+}
  413. {*****************************************************************************
  414. Miscellaneous
  415. *****************************************************************************}
  416. procedure fpc_rangeerror;[public,alias:'FPC_RANGEERROR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  417. begin
  418. HandleErrorFrame(201,get_frame);
  419. end;
  420. procedure fpc_divbyzero;[public,alias:'FPC_DIVBYZERO']; {$ifdef hascompilerproc} compilerproc; {$endif}
  421. begin
  422. HandleErrorFrame(200,get_frame);
  423. end;
  424. procedure fpc_overflow;[public,alias:'FPC_OVERFLOW']; {$ifdef hascompilerproc} compilerproc; {$endif}
  425. begin
  426. HandleErrorFrame(215,get_frame);
  427. end;
  428. procedure fpc_iocheck;{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[public,alias:'FPC_IOCHECK']; {$ifdef hascompilerproc} compilerproc; {$endif}
  429. var
  430. l : longint;
  431. begin
  432. if InOutRes<>0 then
  433. begin
  434. l:=InOutRes;
  435. InOutRes:=0;
  436. HandleErrorFrame(l,get_frame);
  437. end;
  438. end;
  439. Function IOResult:Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  440. Begin
  441. IOResult:=InOutRes;
  442. InOutRes:=0;
  443. End;
  444. Function GetThreadID:SizeUInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  445. begin
  446. (* ThreadID is stored in a threadvar and made available in interface *)
  447. (* to allow setup of this value during thread initialization. *)
  448. GetThreadID := ThreadID;
  449. end;
  450. {*****************************************************************************
  451. Stack check code
  452. *****************************************************************************}
  453. {$IFNDEF NO_GENERIC_STACK_CHECK}
  454. {$IFOPT S+}
  455. {$DEFINE STACKCHECK}
  456. {$ENDIF}
  457. {$S-}
  458. procedure fpc_stackcheck(stack_size:Cardinal);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[public,alias:'FPC_STACKCHECK'];
  459. var
  460. c : Pointer;
  461. begin
  462. { Avoid recursive calls when called from the exit routines }
  463. if StackError then
  464. exit;
  465. c := Sptr - (stack_size + STACK_MARGIN);
  466. if (c <= StackBottom) then
  467. begin
  468. StackError:=true;
  469. HandleError(202);
  470. end;
  471. end;
  472. {$IFDEF STACKCHECK}
  473. {$S+}
  474. {$ENDIF}
  475. {$UNDEF STACKCHECK}
  476. {$ENDIF NO_GENERIC_STACK_CHECK}
  477. {*****************************************************************************
  478. Initialization / Finalization
  479. *****************************************************************************}
  480. const
  481. maxunits=1024; { See also files.pas of the compiler source }
  482. type
  483. TInitFinalRec=record
  484. InitProc,
  485. FinalProc : TProcedure;
  486. end;
  487. TInitFinalTable=record
  488. TableCount,
  489. InitCount : longint;
  490. Procs : array[1..maxunits] of TInitFinalRec;
  491. end;
  492. var
  493. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  494. procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; {$ifdef hascompilerproc} compilerproc; {$endif}
  495. var
  496. i : longint;
  497. begin
  498. { call cpu/fpu initialisation routine }
  499. fpc_cpuinit;
  500. with InitFinalTable do
  501. begin
  502. for i:=1 to TableCount do
  503. begin
  504. if assigned(Procs[i].InitProc) then
  505. Procs[i].InitProc();
  506. InitCount:=i;
  507. end;
  508. end;
  509. if assigned(InitProc) then
  510. TProcedure(InitProc)();
  511. end;
  512. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  513. begin
  514. with InitFinalTable do
  515. begin
  516. while (InitCount>0) do
  517. begin
  518. // we've to decrement the cound before calling the final. code
  519. // else a halt in the final. code leads to a endless loop
  520. dec(InitCount);
  521. if assigned(Procs[InitCount+1].FinalProc) then
  522. Procs[InitCount+1].FinalProc();
  523. end;
  524. end;
  525. end;
  526. {*****************************************************************************
  527. Error / Exit / ExitProc
  528. *****************************************************************************}
  529. Procedure system_exit;forward;
  530. Procedure InternalExit;
  531. var
  532. current_exit : Procedure;
  533. Begin
  534. while exitProc<>nil Do
  535. Begin
  536. InOutRes:=0;
  537. current_exit:=tProcedure(exitProc);
  538. exitProc:=nil;
  539. current_exit();
  540. End;
  541. { Finalize units }
  542. FinalizeUnits;
  543. { Show runtime error and exit }
  544. If erroraddr<>nil Then
  545. Begin
  546. Writeln(stdout,'Runtime error ',Errorcode,' at $',hexstr(PtrInt(Erroraddr),sizeof(PtrInt)*2));
  547. { to get a nice symify }
  548. Writeln(stdout,BackTraceStrFunc(Erroraddr));
  549. dump_stack(stdout,ErrorBase);
  550. Writeln(stdout,'');
  551. End;
  552. End;
  553. Procedure do_exit;[Public,Alias:'FPC_DO_EXIT'];
  554. begin
  555. InternalExit;
  556. System_exit;
  557. end;
  558. Procedure lib_exit;{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[Public,Alias:'FPC_LIB_EXIT'];
  559. begin
  560. InternalExit;
  561. end;
  562. Procedure Halt(ErrNum: Byte);
  563. Begin
  564. ExitCode:=Errnum;
  565. Do_Exit;
  566. end;
  567. function SysBackTraceStr (Addr: Pointer): ShortString;
  568. begin
  569. SysBackTraceStr:=' $'+HexStr(Ptrint(addr),sizeof(PtrInt)*2);
  570. end;
  571. Procedure HandleErrorAddrFrame (Errno : longint;addr,frame : Pointer);[public,alias:'FPC_BREAK_ERROR'];
  572. begin
  573. If pointer(ErrorProc)<>Nil then
  574. ErrorProc(Errno,addr,frame);
  575. errorcode:=word(Errno);
  576. erroraddr:=addr;
  577. errorbase:=frame;
  578. if errorcode <= maxExitCode then
  579. halt(errorcode)
  580. else
  581. halt(255)
  582. end;
  583. Procedure HandleErrorFrame (Errno : longint;frame : Pointer);
  584. {
  585. Procedure to handle internal errors, i.e. not user-invoked errors
  586. Internal function should ALWAYS call HandleError instead of RunError.
  587. Can be used for exception handlers to specify the frame
  588. }
  589. begin
  590. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  591. end;
  592. Procedure HandleError (Errno : longint);[public,alias : 'FPC_HANDLEERROR'];
  593. {
  594. Procedure to handle internal errors, i.e. not user-invoked errors
  595. Internal function should ALWAYS call HandleError instead of RunError.
  596. }
  597. begin
  598. HandleErrorFrame(Errno,get_frame);
  599. end;
  600. procedure RunError(w : word);[alias: 'FPC_RUNERROR'];
  601. begin
  602. errorcode:=w;
  603. erroraddr:=get_caller_addr(get_frame);
  604. errorbase:=get_caller_frame(get_frame);
  605. if errorcode <= maxExitCode then
  606. halt(errorcode)
  607. else
  608. halt(255)
  609. end;
  610. Procedure RunError;{$ifdef SYSTEMINLINE}inline;{$endif}
  611. Begin
  612. RunError (0);
  613. End;
  614. Procedure Halt;{$ifdef SYSTEMINLINE}inline;{$endif}
  615. Begin
  616. Halt(0);
  617. End;
  618. function do_isdevice(handle:thandle):boolean;forward;
  619. Procedure dump_stack(var f : text;bp : Pointer);
  620. var
  621. i : Longint;
  622. prevbp : Pointer;
  623. is_dev : boolean;
  624. caller_frame,
  625. caller_addr : Pointer;
  626. Begin
  627. prevbp:=bp-1;
  628. i:=0;
  629. is_dev:=do_isdevice(textrec(f).Handle);
  630. while bp > prevbp Do
  631. Begin
  632. caller_addr := get_caller_addr(bp);
  633. caller_frame := get_caller_frame(bp);
  634. if (caller_addr=nil) or
  635. (caller_frame=nil) then
  636. break;
  637. Writeln(f,BackTraceStrFunc(caller_addr));
  638. Inc(i);
  639. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  640. break;
  641. prevbp:=bp;
  642. bp:=caller_frame;
  643. End;
  644. End;
  645. Type
  646. PExitProcInfo = ^TExitProcInfo;
  647. TExitProcInfo = Record
  648. Next : PExitProcInfo;
  649. SaveExit : Pointer;
  650. Proc : TProcedure;
  651. End;
  652. const
  653. ExitProcList: PExitProcInfo = nil;
  654. Procedure DoExitProc;
  655. var
  656. P : PExitProcInfo;
  657. Proc : TProcedure;
  658. Begin
  659. P:=ExitProcList;
  660. ExitProcList:=P^.Next;
  661. ExitProc:=P^.SaveExit;
  662. Proc:=P^.Proc;
  663. DisPose(P);
  664. Proc();
  665. End;
  666. Procedure AddExitProc(Proc: TProcedure);
  667. var
  668. P : PExitProcInfo;
  669. Begin
  670. New(P);
  671. P^.Next:=ExitProcList;
  672. P^.SaveExit:=ExitProc;
  673. P^.Proc:=Proc;
  674. ExitProcList:=P;
  675. ExitProc:=@DoExitProc;
  676. End;
  677. function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):ppchar; // const ?
  678. // Extra allocate reserveentries pchar's at the beginning (default param=0 after 1.0.x ?)
  679. // Note: for internal use by skilled programmers only
  680. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  681. var p : ppchar;
  682. i : LongInt;
  683. begin
  684. if High(s)<Low(s) Then Exit(NIL);
  685. Getmem(p,sizeof(pchar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  686. // for cmd
  687. if p=nil then
  688. begin
  689. {$ifdef xunix}
  690. fpseterrno(ESysEnomem);
  691. {$endif}
  692. exit(NIL);
  693. end;
  694. for i:=low(s) to high(s) do
  695. p[i+Reserveentries]:=pchar(s[i]);
  696. p[high(s)+1+Reserveentries]:=nil;
  697. ArrayStringToPPchar:=p;
  698. end;
  699. Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):ppchar;
  700. {
  701. Create a PPChar to structure of pchars which are the arguments specified
  702. in the string S. Especially usefull for creating an ArgV for Exec-calls
  703. }
  704. begin
  705. StringToPPChar:=StringToPPChar(PChar(S),ReserveEntries);
  706. end;
  707. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  708. var
  709. i,nr : longint;
  710. Buf : ^char;
  711. p : ppchar;
  712. begin
  713. buf:=s;
  714. nr:=1;
  715. while (buf^<>#0) do // count nr of args
  716. begin
  717. while (buf^ in [' ',#9,#10]) do // Kill separators.
  718. inc(buf);
  719. inc(nr);
  720. if buf^='"' Then // quotes argument?
  721. begin
  722. inc(buf);
  723. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  724. inc(buf);
  725. if buf^='"' then // skip closing quote.
  726. inc(buf);
  727. end
  728. else
  729. begin // else std
  730. while not (buf^ in [' ',#0,#9,#10]) do
  731. inc(buf);
  732. end;
  733. end;
  734. getmem(p,(ReserveEntries+nr)*sizeof(pchar));
  735. StringToPPChar:=p;
  736. if p=nil then
  737. begin
  738. {$ifdef xunix}
  739. fpseterrno(ESysEnomem);
  740. {$endif}
  741. exit;
  742. end;
  743. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  744. buf:=s;
  745. while (buf^<>#0) do
  746. begin
  747. while (buf^ in [' ',#9,#10]) do // Kill separators.
  748. begin
  749. buf^:=#0;
  750. inc(buf);
  751. end;
  752. if buf^='"' Then // quotes argument?
  753. begin
  754. inc(buf);
  755. p^:=buf;
  756. inc(p);
  757. p^:=nil;
  758. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  759. inc(buf);
  760. if buf^='"' then // skip closing quote.
  761. begin
  762. buf^:=#0;
  763. inc(buf);
  764. end;
  765. end
  766. else
  767. begin
  768. p^:=buf;
  769. inc(p);
  770. p^:=nil;
  771. while not (buf^ in [' ',#0,#9,#10]) do
  772. inc(buf);
  773. end;
  774. end;
  775. end;
  776. {*****************************************************************************
  777. Abstract/Assert support.
  778. *****************************************************************************}
  779. procedure fpc_AbstractErrorIntern;{$ifdef hascompilerproc}compilerproc;{$endif}[public,alias : 'FPC_ABSTRACTERROR'];
  780. begin
  781. If pointer(AbstractErrorProc)<>nil then
  782. AbstractErrorProc();
  783. HandleErrorFrame(211,get_frame);
  784. end;
  785. Procedure fpc_assert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif} [Public,Alias : 'FPC_ASSERT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  786. begin
  787. if pointer(AssertErrorProc)<>nil then
  788. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  789. else
  790. HandleErrorFrame(227,get_frame);
  791. end;
  792. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
  793. begin
  794. If msg='' then
  795. write(stderr,'Assertion failed')
  796. else
  797. write(stderr,msg);
  798. Writeln(stderr,' (',FName,', line ',LineNo,').');
  799. Writeln(stderr,'');
  800. Halt(227);
  801. end;
  802. {*****************************************************************************
  803. SetJmp/LongJmp support.
  804. *****************************************************************************}
  805. {$i setjump.inc}
  806. {$ifdef IOCheckWasOn}
  807. {$I+}
  808. {$endif}
  809. {$ifdef RangeCheckWasOn}
  810. {$R+}
  811. {$endif}
  812. {$ifdef OverflowCheckWasOn}
  813. {$Q+}
  814. {$endif}
  815. {*****************************************************************************
  816. OS dependent Helpers/Syscalls
  817. *****************************************************************************}
  818. {$i sysos.inc}
  819. {*****************************************************************************
  820. Heap
  821. *****************************************************************************}
  822. {$i sysheap.inc}
  823. {$i heap.inc}
  824. {*****************************************************************************
  825. Thread support
  826. *****************************************************************************}
  827. { Generic threadmanager }
  828. {$i thread.inc}
  829. { Generic threadvar support }
  830. {$i threadvr.inc}
  831. { OS Dependent implementation }
  832. {$i systhrd.inc}
  833. {*****************************************************************************
  834. File Handling
  835. *****************************************************************************}
  836. { OS dependent low level file functions }
  837. {$i sysfile.inc}
  838. { Text file }
  839. {$i text.inc}
  840. { Untyped file }
  841. {$i file.inc}
  842. { Typed file }
  843. {$i typefile.inc}
  844. {*****************************************************************************
  845. Directory Handling
  846. *****************************************************************************}
  847. { OS dependent dir functions }
  848. {$i sysdir.inc}
  849. {
  850. $Log: system.inc,v $
  851. Revision 1.78 2005/03/25 22:53:39 jonas
  852. * fixed several warnings and notes about unused variables (mainly) or
  853. uninitialised use of variables/function results (a few)
  854. Revision 1.77 2005/02/14 17:13:29 peter
  855. * truncate log
  856. Revision 1.76 2005/02/08 20:25:28 florian
  857. - killed power from system unit
  858. * move operator ** to math unit
  859. Revision 1.75 2005/02/06 20:35:54 florian
  860. + InitProc
  861. Revision 1.74 2005/02/06 16:57:18 peter
  862. * threads for go32v2,os,emx,netware
  863. Revision 1.73 2005/02/06 13:06:20 peter
  864. * moved file and dir functions to sysfile/sysdir
  865. * win32 thread in systemunit
  866. Revision 1.72 2005/02/06 11:20:52 peter
  867. * threading in system unit
  868. * removed systhrds unit
  869. }