system.inc 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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 VER_1_0}
  365. random := int64((qword(cardinal(genrand_MT19937)) or ((qword(cardinal(genrand_MT19937)) shl 32))) and $7fffffffffffffff) mod l;
  366. {$endif VER_1_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. exitcode:=word(Errno);
  571. erroraddr:=addr;
  572. errorbase:=frame;
  573. halt(errorcode);
  574. end;
  575. Procedure HandleErrorFrame (Errno : longint;frame : Pointer);
  576. {
  577. Procedure to handle internal errors, i.e. not user-invoked errors
  578. Internal function should ALWAYS call HandleError instead of RunError.
  579. Can be used for exception handlers to specify the frame
  580. }
  581. begin
  582. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  583. end;
  584. Procedure HandleError (Errno : longint);[public,alias : 'FPC_HANDLEERROR'];
  585. {
  586. Procedure to handle internal errors, i.e. not user-invoked errors
  587. Internal function should ALWAYS call HandleError instead of RunError.
  588. }
  589. begin
  590. HandleErrorFrame(Errno,get_frame);
  591. end;
  592. procedure runerror(w : word);[alias: 'FPC_RUNERROR'];
  593. begin
  594. errorcode:=w;
  595. exitcode:=w;
  596. erroraddr:=get_caller_addr(get_frame);
  597. errorbase:=get_caller_frame(get_frame);
  598. halt(errorcode);
  599. end;
  600. Procedure RunError;{$ifdef SYSTEMINLINE}inline;{$endif}
  601. Begin
  602. RunError (0);
  603. End;
  604. Procedure Halt;{$ifdef SYSTEMINLINE}inline;{$endif}
  605. Begin
  606. Halt(0);
  607. End;
  608. function do_isdevice(handle:thandle):boolean;forward;
  609. Procedure dump_stack(var f : text;bp : Pointer);
  610. var
  611. i : Longint;
  612. prevbp : Pointer;
  613. is_dev : boolean;
  614. caller_frame,
  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. caller_frame := get_caller_frame(bp);
  624. if (caller_addr=nil) or
  625. (caller_frame=nil) then
  626. break;
  627. Writeln(f,BackTraceStrFunc(caller_addr));
  628. Inc(i);
  629. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  630. break;
  631. prevbp:=bp;
  632. bp:=caller_frame;
  633. End;
  634. End;
  635. Type
  636. PExitProcInfo = ^TExitProcInfo;
  637. TExitProcInfo = Record
  638. Next : PExitProcInfo;
  639. SaveExit : Pointer;
  640. Proc : TProcedure;
  641. End;
  642. const
  643. ExitProcList: PExitProcInfo = nil;
  644. Procedure DoExitProc;
  645. var
  646. P : PExitProcInfo;
  647. Proc : TProcedure;
  648. Begin
  649. P:=ExitProcList;
  650. ExitProcList:=P^.Next;
  651. ExitProc:=P^.SaveExit;
  652. Proc:=P^.Proc;
  653. DisPose(P);
  654. Proc();
  655. End;
  656. Procedure AddExitProc(Proc: TProcedure);
  657. var
  658. P : PExitProcInfo;
  659. Begin
  660. New(P);
  661. P^.Next:=ExitProcList;
  662. P^.SaveExit:=ExitProc;
  663. P^.Proc:=Proc;
  664. ExitProcList:=P;
  665. ExitProc:=@DoExitProc;
  666. End;
  667. {*****************************************************************************
  668. Abstract/Assert support.
  669. *****************************************************************************}
  670. procedure AbstractError;[public,alias : 'FPC_ABSTRACTERROR'];
  671. begin
  672. If pointer(AbstractErrorProc)<>nil then
  673. AbstractErrorProc();
  674. HandleErrorFrame(211,get_frame);
  675. end;
  676. {$ifdef hascompilerproc}
  677. { alias for internal usage in the compiler }
  678. procedure fpc_AbstractErrorIntern; compilerproc; external name 'FPC_ABSTRACTERROR';
  679. {$endif hascompilerproc}
  680. Procedure fpc_assert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer); [SaveRegisters,Public,Alias : 'FPC_ASSERT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  681. begin
  682. if pointer(AssertErrorProc)<>nil then
  683. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  684. else
  685. HandleErrorFrame(227,get_frame);
  686. end;
  687. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
  688. begin
  689. If msg='' then
  690. write(stderr,'Assertion failed')
  691. else
  692. write(stderr,msg);
  693. Writeln(stderr,' (',FName,', line ',LineNo,').');
  694. Writeln(stderr,'');
  695. Halt(227);
  696. end;
  697. {*****************************************************************************
  698. SetJmp/LongJmp support.
  699. *****************************************************************************}
  700. {$i setjump.inc}
  701. {$ifdef IOCheckWasOn}
  702. {$I+}
  703. {$endif}
  704. {$ifdef RangeCheckWasOn}
  705. {$R+}
  706. {$endif}
  707. {$ifdef OverflowCheckWasOn}
  708. {$Q+}
  709. {$endif}
  710. {
  711. $Log$
  712. Revision 1.62 2004-08-30 17:36:18 jonas
  713. * fixed random(int64)
  714. Revision 1.61 2004/08/29 10:23:12 peter
  715. no message
  716. Revision 1.60 2004/07/18 20:21:44 florian
  717. + several unicode (to/from utf-8 conversion) stuff added
  718. * some longint -> SizeInt changes
  719. Revision 1.59 2004/05/27 23:34:23 peter
  720. * stop backtrace also when caller frame is nil
  721. Revision 1.58 2004/05/16 18:51:20 peter
  722. * use thandle in do_*
  723. Revision 1.57 2004/05/01 15:26:33 jonas
  724. * use some more string routines from libc if FPC_USE_LIBC is used
  725. Revision 1.56 2004/04/22 19:43:43 peter
  726. * fix 64bit address printing
  727. Revision 1.55 2004/04/22 17:10:38 peter
  728. * random(int64) added
  729. Revision 1.54 2004/02/20 11:01:20 daniel
  730. * Applied
  731. Revision 1.53 2004/02/06 20:17:13 daniel
  732. * Use $ for hex numbers instead of alien 0x
  733. Revision 1.52 2004/01/22 20:12:37 florian
  734. * fixed syscall number
  735. Revision 1.51 2004/01/11 12:21:06 jonas
  736. * fixed wrong include filename
  737. Revision 1.50 2004/01/11 11:10:07 jonas
  738. + cgeneric.inc: implementations of rtl routines based on libc
  739. * system.inc: include cgeneric.inc before powerpc.inc/i386.inc/... if
  740. FPC_USE_LIBC is defined
  741. * powerpc.inc, i386.inc: check whether the routines they implement aren't
  742. implemented yet in another include file (cgeneric.inc)
  743. Revision 1.49 2004/01/02 17:21:50 jonas
  744. + fpc_cpuinit procedure to allow cpu/fpu initialisation before any unit
  745. initialises
  746. + fpu exceptions for invalid operations and division by zero enabled for
  747. ppc
  748. Revision 1.48 2004/01/01 17:58:16 jonas
  749. + integer division-by-zero detection support for ppc
  750. + compilerproc FPC_DIVBYZERO
  751. Revision 1.47 2003/11/03 09:42:28 marco
  752. * Peter's Cardinal<->Longint fixes patch
  753. Revision 1.46 2003/10/29 18:23:45 jonas
  754. * hack to allow repeatable random sequences using the new random number
  755. generator
  756. * fixed range warning/error
  757. Revision 1.45 2003/10/26 21:15:43 hajny
  758. * minor fix for new Random
  759. Revision 1.44 2003/10/26 18:46:02 jonas
  760. * replaced random generator with the Mersenne twister, which is about
  761. 3.5 times faster
  762. Revision 1.43 2003/09/14 11:34:13 peter
  763. * moved int64 asm code to int64p.inc
  764. * save ebx,esi
  765. Revision 1.42 2003/09/03 14:09:37 florian
  766. * arm fixes to the common rtl code
  767. * some generic math code fixed
  768. * ...
  769. Revision 1.41 2003/08/21 22:10:55 olle
  770. - removed parameter from fpc_iocheck
  771. * changed processor compiler directive * to cpu*
  772. Revision 1.40 2003/03/17 14:30:11 peter
  773. * changed address parameter/return values to pointer instead
  774. of longint
  775. Revision 1.39 2003/02/05 21:48:34 mazen
  776. * fixing run time errors related to unimplemented abstract methods in CG
  777. + giving empty emplementations for some RTL functions
  778. Revision 1.38 2002/12/07 14:36:33 carl
  779. - avoid warnings (add typecast)
  780. Revision 1.37 2002/11/18 18:33:51 peter
  781. * Swap(QWord) constant support
  782. Revision 1.36 2002/10/14 19:39:17 peter
  783. * threads unit added for thread support
  784. Revision 1.35 2002/09/18 18:32:01 carl
  785. * assert now halts with exitcode 227 (as Delphi does)
  786. Revision 1.34 2002/09/07 15:07:46 peter
  787. * old logs removed and tabs fixed
  788. Revision 1.33 2002/08/19 19:34:02 peter
  789. * SYSTEMINLINE define that will add inline directives for small
  790. functions and wrappers. This will be defined automaticly when
  791. the compiler defines the HASINLINE directive
  792. Revision 1.32 2002/07/28 20:43:48 florian
  793. * several fixes for linux/powerpc
  794. * several fixes to MT
  795. Revision 1.31 2002/07/26 22:46:06 florian
  796. * interface of system unit for Linux/PowerPC compiles
  797. Revision 1.30 2002/07/26 16:42:00 florian
  798. * endian directive for PowerPC fixed
  799. Revision 1.29 2002/07/04 20:40:09 florian
  800. + some x86-64 support added
  801. Revision 1.28 2002/04/21 15:51:50 carl
  802. * StackError is now a typed constant
  803. + $S can be used under unix
  804. Revision 1.27 2002/04/15 19:38:40 peter
  805. * stackcheck protected against infinite recursive after stack error
  806. * stackcheck requires saveregisters, because it can be called from
  807. iocheck and then will destroy the result of the original function
  808. Revision 1.26 2002/04/15 18:51:20 carl
  809. + generic stack checking can be overriden
  810. Revision 1.25 2002/04/12 17:37:36 carl
  811. + generic stack checking
  812. }