system.inc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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 ansistrings and maybe also other things in the future }
  32. var
  33. emptychar : char;public name 'FPC_EMPTYCHAR';
  34. initialstklen : longint;external name '__stklen';
  35. {****************************************************************************
  36. Routines which have compiler magic
  37. ****************************************************************************}
  38. {$ifndef INTERNCONSTINTF}
  39. Function lo(i : Integer) : byte; [INTERNPROC: fpc_in_lo_Word];
  40. Function lo(w : Word) : byte; [INTERNPROC: fpc_in_lo_Word];
  41. Function lo(l : Longint) : Word; [INTERNPROC: fpc_in_lo_long];
  42. Function lo(l : DWord) : Word; [INTERNPROC: fpc_in_lo_long];
  43. Function hi(i : Integer) : byte; [INTERNPROC: fpc_in_hi_Word];
  44. Function hi(w : Word) : byte; [INTERNPROC: fpc_in_hi_Word];
  45. Function hi(l : Longint) : Word; [INTERNPROC: fpc_in_hi_long];
  46. Function hi(l : DWord) : Word; [INTERNPROC: fpc_in_hi_long];
  47. Function lo(q : QWord) : DWord; [INTERNPROC: fpc_in_lo_qword];
  48. Function lo(i : Int64) : DWord; [INTERNPROC: fpc_in_lo_qword];
  49. Function hi(q : QWord) : DWord; [INTERNPROC: fpc_in_hi_qword];
  50. Function hi(i : Int64) : DWord; [INTERNPROC: fpc_in_hi_qword];
  51. Function chr(b : byte) : Char; [INTERNPROC: fpc_in_chr_byte];
  52. {$ifndef INTERNLENGTH}
  53. Function Length(s : string) : byte; [INTERNPROC: fpc_in_Length_string];
  54. Function Length(c : char) : byte; [INTERNPROC: fpc_in_Length_string];
  55. {$endif INTERNLENGTH}
  56. Procedure Reset(var f : TypedFile); [INTERNPROC: fpc_in_Reset_TypedFile];
  57. Procedure Rewrite(var f : TypedFile); [INTERNPROC: fpc_in_Rewrite_TypedFile];
  58. {$endif INTERNCONSTINTF}
  59. {****************************************************************************
  60. Include processor specific routines
  61. ****************************************************************************}
  62. {$ifdef FPC_USE_LIBC}
  63. { prefer libc implementations over our own, as they're most likely faster }
  64. {$i cgeneric.inc}
  65. { is now declared as external reference to another routine in the interface }
  66. {$i cgenstr.inc}
  67. {$endif FPC_USE_LIBC}
  68. {$ifdef cpui386}
  69. {$ifdef SYSPROCDEFINED}
  70. {$Error Can't determine processor type !}
  71. {$endif}
  72. {$i i386.inc} { Case dependent, don't change }
  73. {$endif cpui386}
  74. {$ifdef cpum68k}
  75. {$ifdef SYSPROCDEFINED}
  76. {$Error Can't determine processor type !}
  77. {$endif}
  78. {$i m68k.inc} { Case dependent, don't change }
  79. {$define SYSPROCDEFINED}
  80. {$endif cpum68k}
  81. {$ifdef cpux86_64}
  82. {$ifdef SYSPROCDEFINED}
  83. {$Error Can't determine processor type !}
  84. {$endif}
  85. {$i x86_64.inc} { Case dependent, don't change }
  86. {$define SYSPROCDEFINED}
  87. {$endif cpux86_64}
  88. {$ifdef cpupowerpc}
  89. {$ifdef SYSPROCDEFINED}
  90. {$Error Can't determine processor type !}
  91. {$endif}
  92. {$i powerpc.inc} { Case dependent, don't change }
  93. {$define SYSPROCDEFINED}
  94. {$endif cpupowerpc}
  95. {$ifdef cpualpha}
  96. {$ifdef SYSPROCDEFINED}
  97. {$Error Can't determine processor type !}
  98. {$endif}
  99. {$i alpha.inc} { Case dependent, don't change }
  100. {$define SYSPROCDEFINED}
  101. {$endif cpualpha}
  102. {$ifdef cpuiA64}
  103. {$ifdef SYSPROCDEFINED}
  104. {$Error Can't determine processor type !}
  105. {$endif}
  106. {$i ia64.inc} { Case dependent, don't change }
  107. {$define SYSPROCDEFINED}
  108. {$endif cpuiA64}
  109. {$ifdef cpusparc}
  110. {$ifdef SYSPROCDEFINED}
  111. {$Error Can't determine processor type !}
  112. {$endif}
  113. {$i sparc.inc} { Case dependent, don't change }
  114. {$define SYSPROCDEFINED}
  115. {$endif cpusparc}
  116. {$ifdef cpuarm}
  117. {$ifdef SYSPROCDEFINED}
  118. {$Error Can't determine processor type !}
  119. {$endif}
  120. {$i arm.inc} { Case dependent, don't change }
  121. {$define SYSPROCDEFINED}
  122. {$endif cpuarm}
  123. procedure fillchar(var x;count : SizeInt;value : boolean);{$ifdef SYSTEMINLINE}inline;{$endif}
  124. begin
  125. fillchar(x,count,byte(value));
  126. end;
  127. procedure fillchar(var x;count : SizeInt;value : char);{$ifdef SYSTEMINLINE}inline;{$endif}
  128. begin
  129. fillchar(x,count,byte(value));
  130. end;
  131. { Include generic pascal only routines which are not defined in the processor
  132. specific include file }
  133. {$I generic.inc}
  134. {****************************************************************************
  135. Set Handling
  136. ****************************************************************************}
  137. { Include set support which is processor specific}
  138. {$i set.inc}
  139. { Include generic pascal routines for sets if the processor }
  140. { specific routines are not available. }
  141. {$i genset.inc}
  142. {****************************************************************************
  143. Math Routines
  144. ****************************************************************************}
  145. function Hi(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  146. begin
  147. Hi := b shr 4
  148. end;
  149. function Lo(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  150. begin
  151. Lo := b and $0f
  152. end;
  153. Function swap (X : Word) : Word;{$ifdef SYSTEMINLINE}inline;{$endif}{$ifndef INTERNCONSTINTF}[internconst:fpc_in_const_swap_word];{$endif}
  154. Begin
  155. swap:=(X and $ff) shl 8 + (X shr 8)
  156. End;
  157. Function Swap (X : Integer) : Integer;{$ifdef SYSTEMINLINE}inline;{$endif}{$ifndef INTERNCONSTINTF}[internconst:fpc_in_const_swap_word];{$endif}
  158. Begin
  159. swap:=(X and $ff) shl 8 + (X shr 8)
  160. End;
  161. Function swap (X : Longint) : Longint;{$ifdef SYSTEMINLINE}inline;{$endif}{$ifndef INTERNCONSTINTF}[internconst:fpc_in_const_swap_long];{$endif}
  162. Begin
  163. Swap:=(X and $ffff) shl 16 + (X shr 16)
  164. End;
  165. Function Swap (X : Cardinal) : Cardinal;{$ifdef SYSTEMINLINE}inline;{$endif}{$ifndef INTERNCONSTINTF}[internconst:fpc_in_const_swap_long];{$endif}
  166. Begin
  167. Swap:=(X and $ffff) shl 16 + (X shr 16)
  168. End;
  169. Function Swap (X : QWord) : QWord;{$ifdef SYSTEMINLINE}inline;{$endif}{$ifndef INTERNCONSTINTF}[internconst:fpc_in_const_swap_qword];{$endif}
  170. Begin
  171. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  172. End;
  173. Function swap (X : Int64) : Int64;{$ifdef SYSTEMINLINE}inline;{$endif}{$ifndef INTERNCONSTINTF}[internconst:fpc_in_const_swap_qword];{$endif}
  174. Begin
  175. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  176. End;
  177. {$ifdef SUPPORT_DOUBLE}
  178. operator := (b:real48) d:double;
  179. begin
  180. D:=real2double(b);
  181. end;
  182. {$endif SUPPORT_DOUBLE}
  183. {$ifdef SUPPORT_EXTENDED}
  184. operator := (b:real48) e:extended;
  185. begin
  186. e:=real2double(b);
  187. end;
  188. {$endif SUPPORT_EXTENDED}
  189. {$ifdef FPC_USE_LIBC}
  190. { Include libc versions }
  191. {$i cgenmath.inc}
  192. {$endif FPC_USE_LIBC}
  193. { Include processor specific routines }
  194. {$I math.inc}
  195. { Include generic version }
  196. {$I genmath.inc}
  197. {****************************************************************************
  198. Subroutines for String handling
  199. ****************************************************************************}
  200. { Needs to be before RTTI handling }
  201. {$i sstrings.inc}
  202. { requires sstrings.inc for initval }
  203. {$I int64p.inc}
  204. {$I int64.inc}
  205. {Requires int64.inc, since that contains the VAL functions for int64 and qword}
  206. {$i astrings.inc}
  207. {$ifdef HASWIDESTRING}
  208. {$i wstrings.inc}
  209. {$endif HASWIDESTRING}
  210. {$i aliases.inc}
  211. {*****************************************************************************
  212. Dynamic Array support
  213. *****************************************************************************}
  214. {$i dynarr.inc}
  215. {*****************************************************************************
  216. Object Pascal support
  217. *****************************************************************************}
  218. {$i objpas.inc}
  219. {*****************************************************************************
  220. Variant support
  221. *****************************************************************************}
  222. {$ifdef HASVARIANT}
  223. {$i variant.inc}
  224. {$endif HASVARIANT}
  225. {****************************************************************************
  226. Run-Time Type Information (RTTI)
  227. ****************************************************************************}
  228. {$i rtti.inc}
  229. {----------------------------------------------------------------------
  230. Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  231. Pseudo-Random Number Generator.
  232. What is Mersenne Twister?
  233. Mersenne Twister(MT) is a pseudorandom number generator developped by
  234. Makoto Matsumoto and Takuji Nishimura (alphabetical order) during
  235. 1996-1997. MT has the following merits:
  236. It is designed with consideration on the flaws of various existing
  237. generators.
  238. Far longer period and far higher order of equidistribution than any
  239. other implemented generators. (It is proved that the period is 2^19937-1,
  240. and 623-dimensional equidistribution property is assured.)
  241. Fast generation. (Although it depends on the system, it is reported that
  242. MT is sometimes faster than the standard ANSI-C library in a system
  243. with pipeline and cache memory.)
  244. Efficient use of the memory. (The implemented C-code mt19937.c
  245. consumes only 624 words of working area.)
  246. home page
  247. http://www.math.keio.ac.jp/~matumoto/emt.html
  248. original c source
  249. http://www.math.keio.ac.jp/~nisimura/random/int/mt19937int.c
  250. Coded by Takuji Nishimura, considering the suggestions by
  251. Topher Cooper and Marc Rieffel in July-Aug. 1997.
  252. This library is free software; you can redistribute it and/or
  253. modify it under the terms of the GNU Library General Public
  254. License as published by the Free Software Foundation; either
  255. version 2 of the License, or (at your option) any later
  256. version.
  257. This library is distributed in the hope that it will be useful,
  258. but WITHOUT ANY WARRANTY; without even the implied warranty of
  259. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  260. See the GNU Library General Public License for more details.
  261. You should have received a copy of the GNU Library General
  262. Public License along with this library; if not, write to the
  263. Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  264. 02111-1307 USA
  265. Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura.
  266. When you use this, send an email to: [email protected]
  267. with an appropriate reference to your work.
  268. REFERENCE
  269. M. Matsumoto and T. Nishimura,
  270. "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  271. Pseudo-Random Number Generator",
  272. ACM Transactions on Modeling and Computer Simulation,
  273. Vol. 8, No. 1, January 1998, pp 3--30.
  274. Translated to OP and Delphi interface added by Roman Krejci (6.12.1999)
  275. http://www.rksolution.cz/delphi/tips.htm
  276. Revised 21.6.2000: Bug in the function RandInt_MT19937 fixed
  277. 2003/10/26: adapted to use the improved intialisation mentioned at
  278. <http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html> and
  279. removed the assembler code
  280. ----------------------------------------------------------------------}
  281. {$R-} {range checking off}
  282. {$Q-} {overflow checking off}
  283. { Period parameter }
  284. Const
  285. MT19937N=624;
  286. Type
  287. tMT19937StateArray = array [0..MT19937N-1] of longint; // the array for the state vector
  288. { Period parameters }
  289. const
  290. MT19937M=397;
  291. MT19937MATRIX_A =$9908b0df; // constant vector a
  292. MT19937UPPER_MASK=$80000000; // most significant w-r bits
  293. MT19937LOWER_MASK=$7fffffff; // least significant r bits
  294. { Tempering parameters }
  295. TEMPERING_MASK_B=$9d2c5680;
  296. TEMPERING_MASK_C=$efc60000;
  297. VAR
  298. mt : tMT19937StateArray;
  299. const
  300. mti: longint=MT19937N+1; // mti=MT19937N+1 means mt[] is not initialized
  301. { Initializing the array with a seed }
  302. procedure sgenrand_MT19937(seed: longint);
  303. var
  304. i: longint;
  305. begin
  306. mt[0] := seed;
  307. for i := 1 to MT19937N-1 do
  308. begin
  309. mt[i] := 1812433253 * (mt[i-1] xor (mt[i-1] shr 30)) + i;
  310. { See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. }
  311. { In the previous versions, MSBs of the seed affect }
  312. { only MSBs of the array mt[]. }
  313. { 2002/01/09 modified by Makoto Matsumoto }
  314. end;
  315. mti := MT19937N;
  316. end;
  317. function genrand_MT19937: longint;
  318. const
  319. mag01 : array [0..1] of longint =(0, longint(MT19937MATRIX_A));
  320. var
  321. y: longint;
  322. kk: longint;
  323. begin
  324. if RandSeed<>OldRandSeed then
  325. mti:=MT19937N+1;
  326. if (mti >= MT19937N) { 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. }