system.inc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. {
  2. $Id$
  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. {$I innr.inc}
  40. Function lo(i : Integer) : byte; [INTERNPROC: In_lo_Word];
  41. Function lo(w : Word) : byte; [INTERNPROC: In_lo_Word];
  42. Function lo(l : Longint) : Word; [INTERNPROC: In_lo_long];
  43. Function lo(l : DWord) : Word; [INTERNPROC: In_lo_long];
  44. Function hi(i : Integer) : byte; [INTERNPROC: In_hi_Word];
  45. Function hi(w : Word) : byte; [INTERNPROC: In_hi_Word];
  46. Function hi(l : Longint) : Word; [INTERNPROC: In_hi_long];
  47. Function hi(l : DWord) : Word; [INTERNPROC: In_hi_long];
  48. Function lo(q : QWord) : DWord; [INTERNPROC: In_lo_qword];
  49. Function lo(i : Int64) : DWord; [INTERNPROC: In_lo_qword];
  50. Function hi(q : QWord) : DWord; [INTERNPROC: In_hi_qword];
  51. Function hi(i : Int64) : DWord; [INTERNPROC: In_hi_qword];
  52. Function chr(b : byte) : Char; [INTERNPROC: In_chr_byte];
  53. {$ifndef INTERNLENGTH}
  54. Function Length(s : string) : byte; [INTERNPROC: In_Length_string];
  55. Function Length(c : char) : byte; [INTERNPROC: In_Length_string];
  56. {$endif INTERNLENGTH}
  57. Procedure Reset(var f : TypedFile); [INTERNPROC: In_Reset_TypedFile];
  58. Procedure Rewrite(var f : TypedFile); [INTERNPROC: In_Rewrite_TypedFile];
  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. {$i cgenstr.inc}
  66. {$endif FPC_USE_LIBC}
  67. {$ifdef cpui386}
  68. {$ifdef SYSPROCDEFINED}
  69. {$Error Can't determine processor type !}
  70. {$endif}
  71. {$i i386.inc} { Case dependent, don't change }
  72. {$endif cpui386}
  73. {$ifdef cpum68k}
  74. {$ifdef SYSPROCDEFINED}
  75. {$Error Can't determine processor type !}
  76. {$endif}
  77. {$i m68k.inc} { Case dependent, don't change }
  78. {$define SYSPROCDEFINED}
  79. {$endif cpum68k}
  80. {$ifdef cpux86_64}
  81. {$ifdef SYSPROCDEFINED}
  82. {$Error Can't determine processor type !}
  83. {$endif}
  84. {$i x86_64.inc} { Case dependent, don't change }
  85. {$define SYSPROCDEFINED}
  86. {$endif cpux86_64}
  87. {$ifdef cpupowerpc}
  88. {$ifdef SYSPROCDEFINED}
  89. {$Error Can't determine processor type !}
  90. {$endif}
  91. {$i powerpc.inc} { Case dependent, don't change }
  92. {$define SYSPROCDEFINED}
  93. {$endif cpupowerpc}
  94. {$ifdef cpualpha}
  95. {$ifdef SYSPROCDEFINED}
  96. {$Error Can't determine processor type !}
  97. {$endif}
  98. {$i alpha.inc} { Case dependent, don't change }
  99. {$define SYSPROCDEFINED}
  100. {$endif cpualpha}
  101. {$ifdef cpuiA64}
  102. {$ifdef SYSPROCDEFINED}
  103. {$Error Can't determine processor type !}
  104. {$endif}
  105. {$i ia64.inc} { Case dependent, don't change }
  106. {$define SYSPROCDEFINED}
  107. {$endif cpuiA64}
  108. {$ifdef cpusparc}
  109. {$ifdef SYSPROCDEFINED}
  110. {$Error Can't determine processor type !}
  111. {$endif}
  112. {$i sparc.inc} { Case dependent, don't change }
  113. {$define SYSPROCDEFINED}
  114. {$endif cpusparc}
  115. {$ifdef cpuarm}
  116. {$ifdef SYSPROCDEFINED}
  117. {$Error Can't determine processor type !}
  118. {$endif}
  119. {$i arm.inc} { Case dependent, don't change }
  120. {$define SYSPROCDEFINED}
  121. {$endif cpuarm}
  122. procedure fillchar(var x;count : SizeInt;value : boolean);{$ifdef SYSTEMINLINE}inline;{$endif}
  123. begin
  124. fillchar(x,count,byte(value));
  125. end;
  126. procedure fillchar(var x;count : SizeInt;value : char);{$ifdef SYSTEMINLINE}inline;{$endif}
  127. begin
  128. fillchar(x,count,byte(value));
  129. end;
  130. { Include generic pascal only routines which are not defined in the processor
  131. specific include file }
  132. {$I generic.inc}
  133. {****************************************************************************
  134. Set Handling
  135. ****************************************************************************}
  136. { Include set support which is processor specific}
  137. {$i set.inc}
  138. { Include generic pascal routines for sets if the processor }
  139. { specific routines are not available. }
  140. {$i genset.inc}
  141. {****************************************************************************
  142. Math Routines
  143. ****************************************************************************}
  144. function Hi(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  145. begin
  146. Hi := b shr 4
  147. end;
  148. function Lo(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  149. begin
  150. Lo := b and $0f
  151. end;
  152. Function swap (X : Word) : Word;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_swap_word];
  153. Begin
  154. swap:=(X and $ff) shl 8 + (X shr 8)
  155. End;
  156. Function Swap (X : Integer) : Integer;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_swap_word];
  157. Begin
  158. swap:=(X and $ff) shl 8 + (X shr 8)
  159. End;
  160. Function swap (X : Longint) : Longint;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_swap_long];
  161. Begin
  162. Swap:=(X and $ffff) shl 16 + (X shr 16)
  163. End;
  164. Function Swap (X : Cardinal) : Cardinal;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_swap_long];
  165. Begin
  166. Swap:=(X and $ffff) shl 16 + (X shr 16)
  167. End;
  168. Function Swap (X : QWord) : QWord;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_swap_qword];
  169. Begin
  170. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  171. End;
  172. Function swap (X : Int64) : Int64;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_swap_qword];
  173. Begin
  174. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  175. End;
  176. operator := (b:real48) d:double;
  177. begin
  178. D:=real2double(b);
  179. end;
  180. {$ifdef SUPPORT_EXTENDED}
  181. operator := (b:real48) e:extended;
  182. begin
  183. e:=real2double(b);
  184. end;
  185. {$endif SUPPORT_EXTENDED}
  186. { Include processor specific routines }
  187. {$I math.inc}
  188. { Include generic version }
  189. {$I genmath.inc}
  190. operator ** (bas,expo : real) e: real;
  191. begin
  192. e:=power(bas,expo);
  193. end;
  194. operator ** (bas,expo : int64) i: int64;
  195. begin
  196. i:=power(bas,expo);
  197. end;
  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. {$ifndef VER1_0}
  365. random := int64((qword(cardinal(genrand_MT19937)) or ((qword(cardinal(genrand_MT19937)) shl 32))) and $7fffffffffffffff) mod l;
  366. {$endif VER1_0}
  367. end;
  368. function random: extended;
  369. begin
  370. random := cardinal(genrand_MT19937) * (1.0/(int64(1) shl 32));
  371. end;
  372. {****************************************************************************
  373. Memory Management
  374. ****************************************************************************}
  375. Function Ptr(sel,off : Longint) : farpointer;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_ptr];
  376. Begin
  377. ptr:=farpointer((sel shl 4)+off);
  378. End;
  379. Function CSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  380. Begin
  381. Cseg:=0;
  382. End;
  383. Function DSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  384. Begin
  385. Dseg:=0;
  386. End;
  387. Function SSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  388. Begin
  389. Sseg:=0;
  390. End;
  391. {*****************************************************************************
  392. Directory support.
  393. *****************************************************************************}
  394. Procedure getdir(drivenr:byte;Var dir:ansistring);
  395. { this is needed to also allow ansistrings, the shortstring version is
  396. OS dependent }
  397. var
  398. s : shortstring;
  399. begin
  400. getdir(drivenr,s);
  401. dir:=s;
  402. end;
  403. {$ifopt R+}
  404. {$define RangeCheckWasOn}
  405. {$R-}
  406. {$endif opt R+}
  407. {$ifopt I+}
  408. {$define IOCheckWasOn}
  409. {$I-}
  410. {$endif opt I+}
  411. {$ifopt Q+}
  412. {$define OverflowCheckWasOn}
  413. {$Q-}
  414. {$endif opt Q+}
  415. {*****************************************************************************
  416. Miscellaneous
  417. *****************************************************************************}
  418. procedure fpc_rangeerror;[public,alias:'FPC_RANGEERROR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  419. begin
  420. HandleErrorFrame(201,get_frame);
  421. end;
  422. procedure fpc_divbyzero;[public,alias:'FPC_DIVBYZERO']; {$ifdef hascompilerproc} compilerproc; {$endif}
  423. begin
  424. HandleErrorFrame(200,get_frame);
  425. end;
  426. procedure fpc_overflow;[public,alias:'FPC_OVERFLOW']; {$ifdef hascompilerproc} compilerproc; {$endif}
  427. begin
  428. HandleErrorFrame(215,get_frame);
  429. end;
  430. procedure fpc_iocheck;[saveregisters,public,alias:'FPC_IOCHECK']; {$ifdef hascompilerproc} compilerproc; {$endif}
  431. var
  432. l : longint;
  433. begin
  434. if InOutRes<>0 then
  435. begin
  436. l:=InOutRes;
  437. InOutRes:=0;
  438. HandleErrorFrame(l,get_frame);
  439. end;
  440. end;
  441. Function IOResult:Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  442. Begin
  443. IOResult:=InOutRes;
  444. InOutRes:=0;
  445. End;
  446. {*****************************************************************************
  447. Stack check code
  448. *****************************************************************************}
  449. {$IFNDEF NO_GENERIC_STACK_CHECK}
  450. {$IFOPT S+}
  451. {$DEFINE STACKCHECK}
  452. {$ENDIF}
  453. {$S-}
  454. procedure fpc_stackcheck(stack_size:Cardinal);[saveregisters,public,alias:'FPC_STACKCHECK'];
  455. var
  456. c : Pointer;
  457. begin
  458. { Avoid recursive calls when called from the exit routines }
  459. if StackError then
  460. exit;
  461. c := Sptr - (stack_size + STACK_MARGIN);
  462. if (c <= StackBottom) then
  463. begin
  464. StackError:=true;
  465. HandleError(202);
  466. end;
  467. end;
  468. {$IFDEF STACKCHECK}
  469. {$S+}
  470. {$ENDIF}
  471. {$UNDEF STACKCHECK}
  472. {$ENDIF NO_GENERIC_STACK_CHECK}
  473. {*****************************************************************************
  474. Initialization / Finalization
  475. *****************************************************************************}
  476. const
  477. maxunits=1024; { See also files.pas of the compiler source }
  478. type
  479. TInitFinalRec=record
  480. InitProc,
  481. FinalProc : TProcedure;
  482. end;
  483. TInitFinalTable=record
  484. TableCount,
  485. InitCount : longint;
  486. Procs : array[1..maxunits] of TInitFinalRec;
  487. end;
  488. var
  489. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  490. procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; {$ifdef hascompilerproc} compilerproc; {$endif}
  491. var
  492. i : longint;
  493. begin
  494. { call cpu/fpu initialisation routine }
  495. fpc_cpuinit;
  496. with InitFinalTable do
  497. begin
  498. for i:=1 to TableCount do
  499. begin
  500. if assigned(Procs[i].InitProc) then
  501. Procs[i].InitProc();
  502. InitCount:=i;
  503. end;
  504. end;
  505. end;
  506. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  507. begin
  508. with InitFinalTable do
  509. begin
  510. while (InitCount>0) do
  511. begin
  512. // we've to decrement the cound before calling the final. code
  513. // else a halt in the final. code leads to a endless loop
  514. dec(InitCount);
  515. if assigned(Procs[InitCount+1].FinalProc) then
  516. Procs[InitCount+1].FinalProc();
  517. end;
  518. end;
  519. end;
  520. {*****************************************************************************
  521. Error / Exit / ExitProc
  522. *****************************************************************************}
  523. Procedure system_exit;forward;
  524. Procedure InternalExit;
  525. var
  526. current_exit : Procedure;
  527. Begin
  528. while exitProc<>nil Do
  529. Begin
  530. InOutRes:=0;
  531. current_exit:=tProcedure(exitProc);
  532. exitProc:=nil;
  533. current_exit();
  534. End;
  535. { Finalize units }
  536. FinalizeUnits;
  537. { Show runtime error and exit }
  538. If erroraddr<>nil Then
  539. Begin
  540. Writeln(stdout,'Runtime error ',Errorcode,' at $',hexstr(PtrInt(Erroraddr),sizeof(PtrInt)*2));
  541. { to get a nice symify }
  542. Writeln(stdout,BackTraceStrFunc(Erroraddr));
  543. dump_stack(stdout,ErrorBase);
  544. Writeln(stdout,'');
  545. End;
  546. End;
  547. Procedure do_exit;[Public,Alias:'FPC_DO_EXIT'];
  548. begin
  549. InternalExit;
  550. System_exit;
  551. end;
  552. Procedure lib_exit;saveregisters;[Public,Alias:'FPC_LIB_EXIT'];
  553. begin
  554. InternalExit;
  555. end;
  556. Procedure Halt(ErrNum: Byte);
  557. Begin
  558. ExitCode:=Errnum;
  559. Do_Exit;
  560. end;
  561. function SysBackTraceStr (Addr: Pointer): ShortString;
  562. begin
  563. SysBackTraceStr:=' $'+HexStr(Ptrint(addr),sizeof(PtrInt)*2);
  564. end;
  565. Procedure HandleErrorAddrFrame (Errno : longint;addr,frame : Pointer);[public,alias:'FPC_BREAK_ERROR'];
  566. begin
  567. If pointer(ErrorProc)<>Nil then
  568. ErrorProc(Errno,addr,frame);
  569. errorcode:=word(Errno);
  570. erroraddr:=addr;
  571. errorbase:=frame;
  572. if errorcode <= maxExitCode then
  573. halt(errorcode)
  574. else
  575. halt(255)
  576. end;
  577. Procedure HandleErrorFrame (Errno : longint;frame : Pointer);
  578. {
  579. Procedure to handle internal errors, i.e. not user-invoked errors
  580. Internal function should ALWAYS call HandleError instead of RunError.
  581. Can be used for exception handlers to specify the frame
  582. }
  583. begin
  584. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  585. end;
  586. Procedure HandleError (Errno : longint);[public,alias : 'FPC_HANDLEERROR'];
  587. {
  588. Procedure to handle internal errors, i.e. not user-invoked errors
  589. Internal function should ALWAYS call HandleError instead of RunError.
  590. }
  591. begin
  592. HandleErrorFrame(Errno,get_frame);
  593. end;
  594. procedure RunError(w : word);[alias: 'FPC_RUNERROR'];
  595. begin
  596. errorcode:=w;
  597. erroraddr:=get_caller_addr(get_frame);
  598. errorbase:=get_caller_frame(get_frame);
  599. if errorcode <= maxExitCode then
  600. halt(errorcode)
  601. else
  602. halt(255)
  603. end;
  604. Procedure RunError;{$ifdef SYSTEMINLINE}inline;{$endif}
  605. Begin
  606. RunError (0);
  607. End;
  608. Procedure Halt;{$ifdef SYSTEMINLINE}inline;{$endif}
  609. Begin
  610. Halt(0);
  611. End;
  612. function do_isdevice(handle:thandle):boolean;forward;
  613. Procedure dump_stack(var f : text;bp : Pointer);
  614. var
  615. i : Longint;
  616. prevbp : Pointer;
  617. is_dev : boolean;
  618. caller_frame,
  619. caller_addr : Pointer;
  620. Begin
  621. prevbp:=bp-1;
  622. i:=0;
  623. is_dev:=do_isdevice(textrec(f).Handle);
  624. while bp > prevbp Do
  625. Begin
  626. caller_addr := get_caller_addr(bp);
  627. caller_frame := get_caller_frame(bp);
  628. if (caller_addr=nil) or
  629. (caller_frame=nil) then
  630. break;
  631. Writeln(f,BackTraceStrFunc(caller_addr));
  632. Inc(i);
  633. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  634. break;
  635. prevbp:=bp;
  636. bp:=caller_frame;
  637. End;
  638. End;
  639. Type
  640. PExitProcInfo = ^TExitProcInfo;
  641. TExitProcInfo = Record
  642. Next : PExitProcInfo;
  643. SaveExit : Pointer;
  644. Proc : TProcedure;
  645. End;
  646. const
  647. ExitProcList: PExitProcInfo = nil;
  648. Procedure DoExitProc;
  649. var
  650. P : PExitProcInfo;
  651. Proc : TProcedure;
  652. Begin
  653. P:=ExitProcList;
  654. ExitProcList:=P^.Next;
  655. ExitProc:=P^.SaveExit;
  656. Proc:=P^.Proc;
  657. DisPose(P);
  658. Proc();
  659. End;
  660. Procedure AddExitProc(Proc: TProcedure);
  661. var
  662. P : PExitProcInfo;
  663. Begin
  664. New(P);
  665. P^.Next:=ExitProcList;
  666. P^.SaveExit:=ExitProc;
  667. P^.Proc:=Proc;
  668. ExitProcList:=P;
  669. ExitProc:=@DoExitProc;
  670. End;
  671. {*****************************************************************************
  672. Abstract/Assert support.
  673. *****************************************************************************}
  674. procedure AbstractError;[public,alias : 'FPC_ABSTRACTERROR'];
  675. begin
  676. If pointer(AbstractErrorProc)<>nil then
  677. AbstractErrorProc();
  678. HandleErrorFrame(211,get_frame);
  679. end;
  680. {$ifdef hascompilerproc}
  681. { alias for internal usage in the compiler }
  682. procedure fpc_AbstractErrorIntern; compilerproc; external name 'FPC_ABSTRACTERROR';
  683. {$endif hascompilerproc}
  684. Procedure fpc_assert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer); [SaveRegisters,Public,Alias : 'FPC_ASSERT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  685. begin
  686. if pointer(AssertErrorProc)<>nil then
  687. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  688. else
  689. HandleErrorFrame(227,get_frame);
  690. end;
  691. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
  692. begin
  693. If msg='' then
  694. write(stderr,'Assertion failed')
  695. else
  696. write(stderr,msg);
  697. Writeln(stderr,' (',FName,', line ',LineNo,').');
  698. Writeln(stderr,'');
  699. Halt(227);
  700. end;
  701. {*****************************************************************************
  702. SetJmp/LongJmp support.
  703. *****************************************************************************}
  704. {$i setjump.inc}
  705. {$ifdef IOCheckWasOn}
  706. {$I+}
  707. {$endif}
  708. {$ifdef RangeCheckWasOn}
  709. {$R+}
  710. {$endif}
  711. {$ifdef OverflowCheckWasOn}
  712. {$Q+}
  713. {$endif}
  714. {
  715. $Log$
  716. Revision 1.64 2004-09-03 19:26:01 olle
  717. + added maxExitCode to all System.pp
  718. * constrained error code to be below maxExitCode in RunError et. al.
  719. Revision 1.63 2004/08/31 12:23:53 jonas
  720. * fixed compilation with 1.0.x
  721. Revision 1.62 2004/08/30 17:36:18 jonas
  722. * fixed random(int64)
  723. Revision 1.61 2004/08/29 10:23:12 peter
  724. no message
  725. Revision 1.60 2004/07/18 20:21:44 florian
  726. + several unicode (to/from utf-8 conversion) stuff added
  727. * some longint -> SizeInt changes
  728. Revision 1.59 2004/05/27 23:34:23 peter
  729. * stop backtrace also when caller frame is nil
  730. Revision 1.58 2004/05/16 18:51:20 peter
  731. * use thandle in do_*
  732. Revision 1.57 2004/05/01 15:26:33 jonas
  733. * use some more string routines from libc if FPC_USE_LIBC is used
  734. Revision 1.56 2004/04/22 19:43:43 peter
  735. * fix 64bit address printing
  736. Revision 1.55 2004/04/22 17:10:38 peter
  737. * random(int64) added
  738. Revision 1.54 2004/02/20 11:01:20 daniel
  739. * Applied
  740. Revision 1.53 2004/02/06 20:17:13 daniel
  741. * Use $ for hex numbers instead of alien 0x
  742. Revision 1.52 2004/01/22 20:12:37 florian
  743. * fixed syscall number
  744. Revision 1.51 2004/01/11 12:21:06 jonas
  745. * fixed wrong include filename
  746. Revision 1.50 2004/01/11 11:10:07 jonas
  747. + cgeneric.inc: implementations of rtl routines based on libc
  748. * system.inc: include cgeneric.inc before powerpc.inc/i386.inc/... if
  749. FPC_USE_LIBC is defined
  750. * powerpc.inc, i386.inc: check whether the routines they implement aren't
  751. implemented yet in another include file (cgeneric.inc)
  752. Revision 1.49 2004/01/02 17:21:50 jonas
  753. + fpc_cpuinit procedure to allow cpu/fpu initialisation before any unit
  754. initialises
  755. + fpu exceptions for invalid operations and division by zero enabled for
  756. ppc
  757. Revision 1.48 2004/01/01 17:58:16 jonas
  758. + integer division-by-zero detection support for ppc
  759. + compilerproc FPC_DIVBYZERO
  760. Revision 1.47 2003/11/03 09:42:28 marco
  761. * Peter's Cardinal<->Longint fixes patch
  762. Revision 1.46 2003/10/29 18:23:45 jonas
  763. * hack to allow repeatable random sequences using the new random number
  764. generator
  765. * fixed range warning/error
  766. Revision 1.45 2003/10/26 21:15:43 hajny
  767. * minor fix for new Random
  768. Revision 1.44 2003/10/26 18:46:02 jonas
  769. * replaced random generator with the Mersenne twister, which is about
  770. 3.5 times faster
  771. Revision 1.43 2003/09/14 11:34:13 peter
  772. * moved int64 asm code to int64p.inc
  773. * save ebx,esi
  774. Revision 1.42 2003/09/03 14:09:37 florian
  775. * arm fixes to the common rtl code
  776. * some generic math code fixed
  777. * ...
  778. Revision 1.41 2003/08/21 22:10:55 olle
  779. - removed parameter from fpc_iocheck
  780. * changed processor compiler directive * to cpu*
  781. Revision 1.40 2003/03/17 14:30:11 peter
  782. * changed address parameter/return values to pointer instead
  783. of longint
  784. Revision 1.39 2003/02/05 21:48:34 mazen
  785. * fixing run time errors related to unimplemented abstract methods in CG
  786. + giving empty emplementations for some RTL functions
  787. Revision 1.38 2002/12/07 14:36:33 carl
  788. - avoid warnings (add typecast)
  789. Revision 1.37 2002/11/18 18:33:51 peter
  790. * Swap(QWord) constant support
  791. Revision 1.36 2002/10/14 19:39:17 peter
  792. * threads unit added for thread support
  793. Revision 1.35 2002/09/18 18:32:01 carl
  794. * assert now halts with exitcode 227 (as Delphi does)
  795. Revision 1.34 2002/09/07 15:07:46 peter
  796. * old logs removed and tabs fixed
  797. Revision 1.33 2002/08/19 19:34:02 peter
  798. * SYSTEMINLINE define that will add inline directives for small
  799. functions and wrappers. This will be defined automaticly when
  800. the compiler defines the HASINLINE directive
  801. Revision 1.32 2002/07/28 20:43:48 florian
  802. * several fixes for linux/powerpc
  803. * several fixes to MT
  804. Revision 1.31 2002/07/26 22:46:06 florian
  805. * interface of system unit for Linux/PowerPC compiles
  806. Revision 1.30 2002/07/26 16:42:00 florian
  807. * endian directive for PowerPC fixed
  808. Revision 1.29 2002/07/04 20:40:09 florian
  809. + some x86-64 support added
  810. Revision 1.28 2002/04/21 15:51:50 carl
  811. * StackError is now a typed constant
  812. + $S can be used under unix
  813. Revision 1.27 2002/04/15 19:38:40 peter
  814. * stackcheck protected against infinite recursive after stack error
  815. * stackcheck requires saveregisters, because it can be called from
  816. iocheck and then will destroy the result of the original function
  817. Revision 1.26 2002/04/15 18:51:20 carl
  818. + generic stack checking can be overriden
  819. Revision 1.25 2002/04/12 17:37:36 carl
  820. + generic stack checking
  821. }