system.inc 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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:cardinal): cardinal;
  359. begin
  360. random := cardinal((int64(cardinal(genrand_MT19937))*l) shr 32);
  361. end;
  362. function random(l:longint): longint;
  363. begin
  364. random := longint((int64(cardinal(genrand_MT19937))*l) shr 32);
  365. end;
  366. function random(l:int64): int64;
  367. begin
  368. random := longint((int64(cardinal(genrand_MT19937))*l) shr 32);
  369. end;
  370. function random: extended;
  371. begin
  372. random := cardinal(genrand_MT19937) * (1.0/(int64(1) shl 32));
  373. end;
  374. {****************************************************************************
  375. Memory Management
  376. ****************************************************************************}
  377. Function Ptr(sel,off : Longint) : farpointer;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_ptr];
  378. Begin
  379. ptr:=farpointer((sel shl 4)+off);
  380. End;
  381. Function CSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  382. Begin
  383. Cseg:=0;
  384. End;
  385. Function DSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  386. Begin
  387. Dseg:=0;
  388. End;
  389. Function SSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  390. Begin
  391. Sseg:=0;
  392. End;
  393. {*****************************************************************************
  394. Directory support.
  395. *****************************************************************************}
  396. Procedure getdir(drivenr:byte;Var dir:ansistring);
  397. { this is needed to also allow ansistrings, the shortstring version is
  398. OS dependent }
  399. var
  400. s : shortstring;
  401. begin
  402. getdir(drivenr,s);
  403. dir:=s;
  404. end;
  405. {$ifopt R+}
  406. {$define RangeCheckWasOn}
  407. {$R-}
  408. {$endif opt R+}
  409. {$ifopt I+}
  410. {$define IOCheckWasOn}
  411. {$I-}
  412. {$endif opt I+}
  413. {$ifopt Q+}
  414. {$define OverflowCheckWasOn}
  415. {$Q-}
  416. {$endif opt Q+}
  417. {*****************************************************************************
  418. Miscellaneous
  419. *****************************************************************************}
  420. procedure fpc_rangeerror;[public,alias:'FPC_RANGEERROR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  421. begin
  422. HandleErrorFrame(201,get_frame);
  423. end;
  424. procedure fpc_divbyzero;[public,alias:'FPC_DIVBYZERO']; {$ifdef hascompilerproc} compilerproc; {$endif}
  425. begin
  426. HandleErrorFrame(200,get_frame);
  427. end;
  428. procedure fpc_overflow;[public,alias:'FPC_OVERFLOW']; {$ifdef hascompilerproc} compilerproc; {$endif}
  429. begin
  430. HandleErrorFrame(215,get_frame);
  431. end;
  432. procedure fpc_iocheck;[saveregisters,public,alias:'FPC_IOCHECK']; {$ifdef hascompilerproc} compilerproc; {$endif}
  433. var
  434. l : longint;
  435. begin
  436. if InOutRes<>0 then
  437. begin
  438. l:=InOutRes;
  439. InOutRes:=0;
  440. HandleErrorFrame(l,get_frame);
  441. end;
  442. end;
  443. Function IOResult:Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  444. Begin
  445. IOResult:=InOutRes;
  446. InOutRes:=0;
  447. End;
  448. {*****************************************************************************
  449. Stack check code
  450. *****************************************************************************}
  451. {$IFNDEF NO_GENERIC_STACK_CHECK}
  452. {$IFOPT S+}
  453. {$DEFINE STACKCHECK}
  454. {$ENDIF}
  455. {$S-}
  456. procedure fpc_stackcheck(stack_size:Cardinal);[saveregisters,public,alias:'FPC_STACKCHECK'];
  457. var
  458. c : Pointer;
  459. begin
  460. { Avoid recursive calls when called from the exit routines }
  461. if StackError then
  462. exit;
  463. c := Sptr - (stack_size + STACK_MARGIN);
  464. if (c <= StackBottom) then
  465. begin
  466. StackError:=true;
  467. HandleError(202);
  468. end;
  469. end;
  470. {$IFDEF STACKCHECK}
  471. {$S+}
  472. {$ENDIF}
  473. {$UNDEF STACKCHECK}
  474. {$ENDIF NO_GENERIC_STACK_CHECK}
  475. {*****************************************************************************
  476. Initialization / Finalization
  477. *****************************************************************************}
  478. const
  479. maxunits=1024; { See also files.pas of the compiler source }
  480. type
  481. TInitFinalRec=record
  482. InitProc,
  483. FinalProc : TProcedure;
  484. end;
  485. TInitFinalTable=record
  486. TableCount,
  487. InitCount : longint;
  488. Procs : array[1..maxunits] of TInitFinalRec;
  489. end;
  490. var
  491. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  492. procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; {$ifdef hascompilerproc} compilerproc; {$endif}
  493. var
  494. i : longint;
  495. begin
  496. { call cpu/fpu initialisation routine }
  497. fpc_cpuinit;
  498. with InitFinalTable do
  499. begin
  500. for i:=1 to TableCount do
  501. begin
  502. if assigned(Procs[i].InitProc) then
  503. Procs[i].InitProc();
  504. InitCount:=i;
  505. end;
  506. end;
  507. end;
  508. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  509. begin
  510. with InitFinalTable do
  511. begin
  512. while (InitCount>0) do
  513. begin
  514. // we've to decrement the cound before calling the final. code
  515. // else a halt in the final. code leads to a endless loop
  516. dec(InitCount);
  517. if assigned(Procs[InitCount+1].FinalProc) then
  518. Procs[InitCount+1].FinalProc();
  519. end;
  520. end;
  521. end;
  522. {*****************************************************************************
  523. Error / Exit / ExitProc
  524. *****************************************************************************}
  525. Procedure system_exit;forward;
  526. Procedure InternalExit;
  527. var
  528. current_exit : Procedure;
  529. Begin
  530. while exitProc<>nil Do
  531. Begin
  532. InOutRes:=0;
  533. current_exit:=tProcedure(exitProc);
  534. exitProc:=nil;
  535. current_exit();
  536. End;
  537. { Finalize units }
  538. FinalizeUnits;
  539. { Show runtime error and exit }
  540. If erroraddr<>nil Then
  541. Begin
  542. Writeln(stdout,'Runtime error ',Errorcode,' at $',hexstr(PtrInt(Erroraddr),sizeof(PtrInt)*2));
  543. { to get a nice symify }
  544. Writeln(stdout,BackTraceStrFunc(Erroraddr));
  545. dump_stack(stdout,ErrorBase);
  546. Writeln(stdout,'');
  547. End;
  548. End;
  549. Procedure do_exit;[Public,Alias:'FPC_DO_EXIT'];
  550. begin
  551. InternalExit;
  552. System_exit;
  553. end;
  554. Procedure lib_exit;saveregisters;[Public,Alias:'FPC_LIB_EXIT'];
  555. begin
  556. InternalExit;
  557. end;
  558. Procedure Halt(ErrNum: Byte);
  559. Begin
  560. ExitCode:=Errnum;
  561. Do_Exit;
  562. end;
  563. function SysBackTraceStr (Addr: Pointer): ShortString;
  564. begin
  565. SysBackTraceStr:=' $'+HexStr(Ptrint(addr),sizeof(PtrInt)*2);
  566. end;
  567. Procedure HandleErrorAddrFrame (Errno : longint;addr,frame : Pointer);[public,alias:'FPC_BREAK_ERROR'];
  568. begin
  569. If pointer(ErrorProc)<>Nil then
  570. ErrorProc(Errno,addr,frame);
  571. errorcode:=word(Errno);
  572. exitcode:=word(Errno);
  573. erroraddr:=addr;
  574. errorbase:=frame;
  575. halt(errorcode);
  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. exitcode:=w;
  598. erroraddr:=get_caller_addr(get_frame);
  599. errorbase:=get_caller_frame(get_frame);
  600. halt(errorcode);
  601. end;
  602. Procedure RunError;{$ifdef SYSTEMINLINE}inline;{$endif}
  603. Begin
  604. RunError (0);
  605. End;
  606. Procedure Halt;{$ifdef SYSTEMINLINE}inline;{$endif}
  607. Begin
  608. Halt(0);
  609. End;
  610. function do_isdevice(handle:thandle):boolean;forward;
  611. Procedure dump_stack(var f : text;bp : Pointer);
  612. var
  613. i : Longint;
  614. prevbp : Pointer;
  615. is_dev : boolean;
  616. caller_frame,
  617. caller_addr : Pointer;
  618. Begin
  619. prevbp:=bp-1;
  620. i:=0;
  621. is_dev:=do_isdevice(textrec(f).Handle);
  622. while bp > prevbp Do
  623. Begin
  624. caller_addr := get_caller_addr(bp);
  625. caller_frame := get_caller_frame(bp);
  626. if (caller_addr=nil) or
  627. (caller_frame=nil) then
  628. break;
  629. Writeln(f,BackTraceStrFunc(caller_addr));
  630. Inc(i);
  631. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  632. break;
  633. prevbp:=bp;
  634. bp:=caller_frame;
  635. End;
  636. End;
  637. Type
  638. PExitProcInfo = ^TExitProcInfo;
  639. TExitProcInfo = Record
  640. Next : PExitProcInfo;
  641. SaveExit : Pointer;
  642. Proc : TProcedure;
  643. End;
  644. const
  645. ExitProcList: PExitProcInfo = nil;
  646. Procedure DoExitProc;
  647. var
  648. P : PExitProcInfo;
  649. Proc : TProcedure;
  650. Begin
  651. P:=ExitProcList;
  652. ExitProcList:=P^.Next;
  653. ExitProc:=P^.SaveExit;
  654. Proc:=P^.Proc;
  655. DisPose(P);
  656. Proc();
  657. End;
  658. Procedure AddExitProc(Proc: TProcedure);
  659. var
  660. P : PExitProcInfo;
  661. Begin
  662. New(P);
  663. P^.Next:=ExitProcList;
  664. P^.SaveExit:=ExitProc;
  665. P^.Proc:=Proc;
  666. ExitProcList:=P;
  667. ExitProc:=@DoExitProc;
  668. End;
  669. {*****************************************************************************
  670. Abstract/Assert support.
  671. *****************************************************************************}
  672. procedure AbstractError;[public,alias : 'FPC_ABSTRACTERROR'];
  673. begin
  674. If pointer(AbstractErrorProc)<>nil then
  675. AbstractErrorProc();
  676. HandleErrorFrame(211,get_frame);
  677. end;
  678. {$ifdef hascompilerproc}
  679. { alias for internal usage in the compiler }
  680. procedure fpc_AbstractErrorIntern; compilerproc; external name 'FPC_ABSTRACTERROR';
  681. {$endif hascompilerproc}
  682. Procedure fpc_assert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer); [SaveRegisters,Public,Alias : 'FPC_ASSERT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  683. begin
  684. if pointer(AssertErrorProc)<>nil then
  685. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  686. else
  687. HandleErrorFrame(227,get_frame);
  688. end;
  689. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
  690. begin
  691. If msg='' then
  692. write(stderr,'Assertion failed')
  693. else
  694. write(stderr,msg);
  695. Writeln(stderr,' (',FName,', line ',LineNo,').');
  696. Writeln(stderr,'');
  697. Halt(227);
  698. end;
  699. {*****************************************************************************
  700. SetJmp/LongJmp support.
  701. *****************************************************************************}
  702. {$i setjump.inc}
  703. {$ifdef IOCheckWasOn}
  704. {$I+}
  705. {$endif}
  706. {$ifdef RangeCheckWasOn}
  707. {$R+}
  708. {$endif}
  709. {$ifdef OverflowCheckWasOn}
  710. {$Q+}
  711. {$endif}
  712. {
  713. $Log$
  714. Revision 1.60 2004-07-18 20:21:44 florian
  715. + several unicode (to/from utf-8 conversion) stuff added
  716. * some longint -> SizeInt changes
  717. Revision 1.59 2004/05/27 23:34:23 peter
  718. * stop backtrace also when caller frame is nil
  719. Revision 1.58 2004/05/16 18:51:20 peter
  720. * use thandle in do_*
  721. Revision 1.57 2004/05/01 15:26:33 jonas
  722. * use some more string routines from libc if FPC_USE_LIBC is used
  723. Revision 1.56 2004/04/22 19:43:43 peter
  724. * fix 64bit address printing
  725. Revision 1.55 2004/04/22 17:10:38 peter
  726. * random(int64) added
  727. Revision 1.54 2004/02/20 11:01:20 daniel
  728. * Applied
  729. Revision 1.53 2004/02/06 20:17:13 daniel
  730. * Use $ for hex numbers instead of alien 0x
  731. Revision 1.52 2004/01/22 20:12:37 florian
  732. * fixed syscall number
  733. Revision 1.51 2004/01/11 12:21:06 jonas
  734. * fixed wrong include filename
  735. Revision 1.50 2004/01/11 11:10:07 jonas
  736. + cgeneric.inc: implementations of rtl routines based on libc
  737. * system.inc: include cgeneric.inc before powerpc.inc/i386.inc/... if
  738. FPC_USE_LIBC is defined
  739. * powerpc.inc, i386.inc: check whether the routines they implement aren't
  740. implemented yet in another include file (cgeneric.inc)
  741. Revision 1.49 2004/01/02 17:21:50 jonas
  742. + fpc_cpuinit procedure to allow cpu/fpu initialisation before any unit
  743. initialises
  744. + fpu exceptions for invalid operations and division by zero enabled for
  745. ppc
  746. Revision 1.48 2004/01/01 17:58:16 jonas
  747. + integer division-by-zero detection support for ppc
  748. + compilerproc FPC_DIVBYZERO
  749. Revision 1.47 2003/11/03 09:42:28 marco
  750. * Peter's Cardinal<->Longint fixes patch
  751. Revision 1.46 2003/10/29 18:23:45 jonas
  752. * hack to allow repeatable random sequences using the new random number
  753. generator
  754. * fixed range warning/error
  755. Revision 1.45 2003/10/26 21:15:43 hajny
  756. * minor fix for new Random
  757. Revision 1.44 2003/10/26 18:46:02 jonas
  758. * replaced random generator with the Mersenne twister, which is about
  759. 3.5 times faster
  760. Revision 1.43 2003/09/14 11:34:13 peter
  761. * moved int64 asm code to int64p.inc
  762. * save ebx,esi
  763. Revision 1.42 2003/09/03 14:09:37 florian
  764. * arm fixes to the common rtl code
  765. * some generic math code fixed
  766. * ...
  767. Revision 1.41 2003/08/21 22:10:55 olle
  768. - removed parameter from fpc_iocheck
  769. * changed processor compiler directive * to cpu*
  770. Revision 1.40 2003/03/17 14:30:11 peter
  771. * changed address parameter/return values to pointer instead
  772. of longint
  773. Revision 1.39 2003/02/05 21:48:34 mazen
  774. * fixing run time errors related to unimplemented abstract methods in CG
  775. + giving empty emplementations for some RTL functions
  776. Revision 1.38 2002/12/07 14:36:33 carl
  777. - avoid warnings (add typecast)
  778. Revision 1.37 2002/11/18 18:33:51 peter
  779. * Swap(QWord) constant support
  780. Revision 1.36 2002/10/14 19:39:17 peter
  781. * threads unit added for thread support
  782. Revision 1.35 2002/09/18 18:32:01 carl
  783. * assert now halts with exitcode 227 (as Delphi does)
  784. Revision 1.34 2002/09/07 15:07:46 peter
  785. * old logs removed and tabs fixed
  786. Revision 1.33 2002/08/19 19:34:02 peter
  787. * SYSTEMINLINE define that will add inline directives for small
  788. functions and wrappers. This will be defined automaticly when
  789. the compiler defines the HASINLINE directive
  790. Revision 1.32 2002/07/28 20:43:48 florian
  791. * several fixes for linux/powerpc
  792. * several fixes to MT
  793. Revision 1.31 2002/07/26 22:46:06 florian
  794. * interface of system unit for Linux/PowerPC compiles
  795. Revision 1.30 2002/07/26 16:42:00 florian
  796. * endian directive for PowerPC fixed
  797. Revision 1.29 2002/07/04 20:40:09 florian
  798. + some x86-64 support added
  799. Revision 1.28 2002/04/21 15:51:50 carl
  800. * StackError is now a typed constant
  801. + $S can be used under unix
  802. Revision 1.27 2002/04/15 19:38:40 peter
  803. * stackcheck protected against infinite recursive after stack error
  804. * stackcheck requires saveregisters, because it can be called from
  805. iocheck and then will destroy the result of the original function
  806. Revision 1.26 2002/04/15 18:51:20 carl
  807. + generic stack checking can be overriden
  808. Revision 1.25 2002/04/12 17:37:36 carl
  809. + generic stack checking
  810. }