system.inc 28 KB

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