system.inc 28 KB

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