system.inc 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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. {$ifndef INTERNCONSTINTF}
  40. Function lo(i : Integer) : byte; [INTERNPROC: fpc_in_lo_Word];
  41. Function lo(w : Word) : byte; [INTERNPROC: fpc_in_lo_Word];
  42. Function lo(l : Longint) : Word; [INTERNPROC: fpc_in_lo_long];
  43. Function lo(l : DWord) : Word; [INTERNPROC: fpc_in_lo_long];
  44. Function hi(i : Integer) : byte; [INTERNPROC: fpc_in_hi_Word];
  45. Function hi(w : Word) : byte; [INTERNPROC: fpc_in_hi_Word];
  46. Function hi(l : Longint) : Word; [INTERNPROC: fpc_in_hi_long];
  47. Function hi(l : DWord) : Word; [INTERNPROC: fpc_in_hi_long];
  48. Function lo(q : QWord) : DWord; [INTERNPROC: fpc_in_lo_qword];
  49. Function lo(i : Int64) : DWord; [INTERNPROC: fpc_in_lo_qword];
  50. Function hi(q : QWord) : DWord; [INTERNPROC: fpc_in_hi_qword];
  51. Function hi(i : Int64) : DWord; [INTERNPROC: fpc_in_hi_qword];
  52. Function chr(b : byte) : Char; [INTERNPROC: fpc_in_chr_byte];
  53. {$ifndef INTERNLENGTH}
  54. Function Length(s : string) : byte; [INTERNPROC: fpc_in_Length_string];
  55. Function Length(c : char) : byte; [INTERNPROC: fpc_in_Length_string];
  56. {$endif INTERNLENGTH}
  57. Procedure Reset(var f : TypedFile); [INTERNPROC: fpc_in_Reset_TypedFile];
  58. Procedure Rewrite(var f : TypedFile); [INTERNPROC: fpc_in_Rewrite_TypedFile];
  59. {$endif INTERNCONSTINTF}
  60. {****************************************************************************
  61. Include processor specific routines
  62. ****************************************************************************}
  63. {$ifdef FPC_USE_LIBC}
  64. { prefer libc implementations over our own, as they're most likely faster }
  65. {$i cgeneric.inc}
  66. { is now declared as external reference to another routine in the interface }
  67. {$i cgenstr.inc}
  68. {$endif FPC_USE_LIBC}
  69. {$ifdef cpui386}
  70. {$ifdef SYSPROCDEFINED}
  71. {$Error Can't determine processor type !}
  72. {$endif}
  73. {$i i386.inc} { Case dependent, don't change }
  74. {$endif cpui386}
  75. {$ifdef cpum68k}
  76. {$ifdef SYSPROCDEFINED}
  77. {$Error Can't determine processor type !}
  78. {$endif}
  79. {$i m68k.inc} { Case dependent, don't change }
  80. {$define SYSPROCDEFINED}
  81. {$endif cpum68k}
  82. {$ifdef cpux86_64}
  83. {$ifdef SYSPROCDEFINED}
  84. {$Error Can't determine processor type !}
  85. {$endif}
  86. {$i x86_64.inc} { Case dependent, don't change }
  87. {$define SYSPROCDEFINED}
  88. {$endif cpux86_64}
  89. {$ifdef cpupowerpc}
  90. {$ifdef SYSPROCDEFINED}
  91. {$Error Can't determine processor type !}
  92. {$endif}
  93. {$i powerpc.inc} { Case dependent, don't change }
  94. {$define SYSPROCDEFINED}
  95. {$endif cpupowerpc}
  96. {$ifdef cpualpha}
  97. {$ifdef SYSPROCDEFINED}
  98. {$Error Can't determine processor type !}
  99. {$endif}
  100. {$i alpha.inc} { Case dependent, don't change }
  101. {$define SYSPROCDEFINED}
  102. {$endif cpualpha}
  103. {$ifdef cpuiA64}
  104. {$ifdef SYSPROCDEFINED}
  105. {$Error Can't determine processor type !}
  106. {$endif}
  107. {$i ia64.inc} { Case dependent, don't change }
  108. {$define SYSPROCDEFINED}
  109. {$endif cpuiA64}
  110. {$ifdef cpusparc}
  111. {$ifdef SYSPROCDEFINED}
  112. {$Error Can't determine processor type !}
  113. {$endif}
  114. {$i sparc.inc} { Case dependent, don't change }
  115. {$define SYSPROCDEFINED}
  116. {$endif cpusparc}
  117. {$ifdef cpuarm}
  118. {$ifdef SYSPROCDEFINED}
  119. {$Error Can't determine processor type !}
  120. {$endif}
  121. {$i arm.inc} { Case dependent, don't change }
  122. {$define SYSPROCDEFINED}
  123. {$endif cpuarm}
  124. procedure fillchar(var x;count : SizeInt;value : boolean);{$ifdef SYSTEMINLINE}inline;{$endif}
  125. begin
  126. fillchar(x,count,byte(value));
  127. end;
  128. procedure fillchar(var x;count : SizeInt;value : char);{$ifdef SYSTEMINLINE}inline;{$endif}
  129. begin
  130. fillchar(x,count,byte(value));
  131. end;
  132. { Include generic pascal only routines which are not defined in the processor
  133. specific include file }
  134. {$I generic.inc}
  135. {****************************************************************************
  136. Set Handling
  137. ****************************************************************************}
  138. { Include set support which is processor specific}
  139. {$i set.inc}
  140. { Include generic pascal routines for sets if the processor }
  141. { specific routines are not available. }
  142. {$i genset.inc}
  143. {****************************************************************************
  144. Math Routines
  145. ****************************************************************************}
  146. function Hi(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  147. begin
  148. Hi := b shr 4
  149. end;
  150. function Lo(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  151. begin
  152. Lo := b and $0f
  153. end;
  154. Function swap (X : Word) : Word;{$ifdef SYSTEMINLINE}inline;{$endif}{$ifndef INTERNCONSTINTF}[internconst:fpc_in_const_swap_word];{$endif}
  155. Begin
  156. swap:=(X and $ff) shl 8 + (X shr 8)
  157. End;
  158. Function Swap (X : Integer) : Integer;{$ifdef SYSTEMINLINE}inline;{$endif}{$ifndef INTERNCONSTINTF}[internconst:fpc_in_const_swap_word];{$endif}
  159. Begin
  160. swap:=(X and $ff) shl 8 + (X shr 8)
  161. End;
  162. Function swap (X : Longint) : Longint;{$ifdef SYSTEMINLINE}inline;{$endif}{$ifndef INTERNCONSTINTF}[internconst:fpc_in_const_swap_long];{$endif}
  163. Begin
  164. Swap:=(X and $ffff) shl 16 + (X shr 16)
  165. End;
  166. Function Swap (X : Cardinal) : Cardinal;{$ifdef SYSTEMINLINE}inline;{$endif}{$ifndef INTERNCONSTINTF}[internconst:fpc_in_const_swap_long];{$endif}
  167. Begin
  168. Swap:=(X and $ffff) shl 16 + (X shr 16)
  169. End;
  170. Function Swap (X : QWord) : QWord;{$ifdef SYSTEMINLINE}inline;{$endif}{$ifndef INTERNCONSTINTF}[internconst:fpc_in_const_swap_qword];{$endif}
  171. Begin
  172. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  173. End;
  174. Function swap (X : Int64) : Int64;{$ifdef SYSTEMINLINE}inline;{$endif}{$ifndef INTERNCONSTINTF}[internconst:fpc_in_const_swap_qword];{$endif}
  175. Begin
  176. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  177. End;
  178. {$ifdef SUPPORT_DOUBLE}
  179. operator := (b:real48) d:double;
  180. begin
  181. D:=real2double(b);
  182. end;
  183. {$endif SUPPORT_DOUBLE}
  184. {$ifdef SUPPORT_EXTENDED}
  185. operator := (b:real48) e:extended;
  186. begin
  187. e:=real2double(b);
  188. end;
  189. {$endif SUPPORT_EXTENDED}
  190. {$ifdef FPC_USE_LIBC}
  191. { Include libc versions }
  192. {$i cgenmath.inc}
  193. {$endif FPC_USE_LIBC}
  194. { Include processor specific routines }
  195. {$I math.inc}
  196. { Include generic version }
  197. {$I genmath.inc}
  198. {****************************************************************************
  199. Subroutines for String handling
  200. ****************************************************************************}
  201. { Needs to be before RTTI handling }
  202. {$i sstrings.inc}
  203. { requires sstrings.inc for initval }
  204. {$I int64p.inc}
  205. {$I int64.inc}
  206. {Requires int64.inc, since that contains the VAL functions for int64 and qword}
  207. {$i astrings.inc}
  208. {$ifdef HASWIDESTRING}
  209. {$i wstrings.inc}
  210. {$endif HASWIDESTRING}
  211. {$i aliases.inc}
  212. {*****************************************************************************
  213. Dynamic Array support
  214. *****************************************************************************}
  215. {$i dynarr.inc}
  216. {*****************************************************************************
  217. Object Pascal support
  218. *****************************************************************************}
  219. {$i objpas.inc}
  220. {*****************************************************************************
  221. Variant support
  222. *****************************************************************************}
  223. {$ifdef HASVARIANT}
  224. {$i variant.inc}
  225. {$endif HASVARIANT}
  226. {****************************************************************************
  227. Run-Time Type Information (RTTI)
  228. ****************************************************************************}
  229. {$i rtti.inc}
  230. {----------------------------------------------------------------------
  231. Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  232. Pseudo-Random Number Generator.
  233. What is Mersenne Twister?
  234. Mersenne Twister(MT) is a pseudorandom number generator developped by
  235. Makoto Matsumoto and Takuji Nishimura (alphabetical order) during
  236. 1996-1997. MT has the following merits:
  237. It is designed with consideration on the flaws of various existing
  238. generators.
  239. Far longer period and far higher order of equidistribution than any
  240. other implemented generators. (It is proved that the period is 2^19937-1,
  241. and 623-dimensional equidistribution property is assured.)
  242. Fast generation. (Although it depends on the system, it is reported that
  243. MT is sometimes faster than the standard ANSI-C library in a system
  244. with pipeline and cache memory.)
  245. Efficient use of the memory. (The implemented C-code mt19937.c
  246. consumes only 624 words of working area.)
  247. home page
  248. http://www.math.keio.ac.jp/~matumoto/emt.html
  249. original c source
  250. http://www.math.keio.ac.jp/~nisimura/random/int/mt19937int.c
  251. Coded by Takuji Nishimura, considering the suggestions by
  252. Topher Cooper and Marc Rieffel in July-Aug. 1997.
  253. This library is free software; you can redistribute it and/or
  254. modify it under the terms of the GNU Library General Public
  255. License as published by the Free Software Foundation; either
  256. version 2 of the License, or (at your option) any later
  257. version.
  258. This library is distributed in the hope that it will be useful,
  259. but WITHOUT ANY WARRANTY; without even the implied warranty of
  260. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  261. See the GNU Library General Public License for more details.
  262. You should have received a copy of the GNU Library General
  263. Public License along with this library; if not, write to the
  264. Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  265. 02111-1307 USA
  266. Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura.
  267. When you use this, send an email to: [email protected]
  268. with an appropriate reference to your work.
  269. REFERENCE
  270. M. Matsumoto and T. Nishimura,
  271. "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  272. Pseudo-Random Number Generator",
  273. ACM Transactions on Modeling and Computer Simulation,
  274. Vol. 8, No. 1, January 1998, pp 3--30.
  275. Translated to OP and Delphi interface added by Roman Krejci (6.12.1999)
  276. http://www.rksolution.cz/delphi/tips.htm
  277. Revised 21.6.2000: Bug in the function RandInt_MT19937 fixed
  278. 2003/10/26: adapted to use the improved intialisation mentioned at
  279. <http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html> and
  280. removed the assembler code
  281. ----------------------------------------------------------------------}
  282. {$R-} {range checking off}
  283. {$Q-} {overflow checking off}
  284. { Period parameter }
  285. Const
  286. MT19937N=624;
  287. Type
  288. tMT19937StateArray = array [0..MT19937N-1] of longint; // the array for the state vector
  289. { Period parameters }
  290. const
  291. MT19937M=397;
  292. MT19937MATRIX_A =$9908b0df; // constant vector a
  293. MT19937UPPER_MASK=$80000000; // most significant w-r bits
  294. MT19937LOWER_MASK=$7fffffff; // least significant r bits
  295. { Tempering parameters }
  296. TEMPERING_MASK_B=$9d2c5680;
  297. TEMPERING_MASK_C=$efc60000;
  298. VAR
  299. mt : tMT19937StateArray;
  300. const
  301. mti: longint=MT19937N+1; // mti=MT19937N+1 means mt[] is not initialized
  302. { Initializing the array with a seed }
  303. procedure sgenrand_MT19937(seed: longint);
  304. var
  305. i: longint;
  306. begin
  307. mt[0] := seed;
  308. for i := 1 to MT19937N-1 do
  309. begin
  310. mt[i] := 1812433253 * (mt[i-1] xor (mt[i-1] shr 30)) + i;
  311. { See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. }
  312. { In the previous versions, MSBs of the seed affect }
  313. { only MSBs of the array mt[]. }
  314. { 2002/01/09 modified by Makoto Matsumoto }
  315. end;
  316. mti := MT19937N;
  317. end;
  318. function genrand_MT19937: longint;
  319. const
  320. mag01 : array [0..1] of longint =(0, longint(MT19937MATRIX_A));
  321. var
  322. y: longint;
  323. kk: longint;
  324. begin
  325. if (mti >= MT19937N) or
  326. (randseed <> oldrandseed) { generate MT19937N longints at one time }
  327. then begin
  328. if mti = (MT19937N+1) then // if sgenrand_MT19937() has not been called,
  329. begin
  330. sgenrand_MT19937(randseed); // default initial seed is used
  331. { hack: randseed is not used more than once in this algorithm. Most }
  332. { user changes are re-initialising reandseed with the value it had }
  333. { at the start -> with the "not", we will detect this change. }
  334. { Detecting other changes is not useful, since the generated }
  335. { numbers will be different anyway. }
  336. randseed := not(randseed);
  337. oldrandseed := randseed;
  338. end;
  339. for kk:=0 to MT19937N-MT19937M-1 do begin
  340. y := (mt[kk] and MT19937UPPER_MASK) or (mt[kk+1] and MT19937LOWER_MASK);
  341. mt[kk] := mt[kk+MT19937M] xor (y shr 1) xor mag01[y and $00000001];
  342. end;
  343. for kk:= MT19937N-MT19937M to MT19937N-2 do begin
  344. y := (mt[kk] and MT19937UPPER_MASK) or (mt[kk+1] and MT19937LOWER_MASK);
  345. mt[kk] := mt[kk+(MT19937M-MT19937N)] xor (y shr 1) xor mag01[y and $00000001];
  346. end;
  347. y := (mt[MT19937N-1] and MT19937UPPER_MASK) or (mt[0] and MT19937LOWER_MASK);
  348. mt[MT19937N-1] := mt[MT19937M-1] xor (y shr 1) xor mag01[y and $00000001];
  349. mti := 0;
  350. end;
  351. y := mt[mti]; inc(mti);
  352. y := y xor (y shr 11);
  353. y := y xor (y shl 7) and TEMPERING_MASK_B;
  354. y := y xor (y shl 15) and TEMPERING_MASK_C;
  355. y := y xor (y shr 18);
  356. Result := y;
  357. end;
  358. function random(l:longint): longint;
  359. begin
  360. random := longint((int64(cardinal(genrand_MT19937))*l) shr 32);
  361. end;
  362. function random(l:int64): int64;
  363. begin
  364. {$ifndef VER1_0}
  365. random := int64((qword(cardinal(genrand_MT19937)) or ((qword(cardinal(genrand_MT19937)) shl 32))) and $7fffffffffffffff) mod l;
  366. {$endif VER1_0}
  367. end;
  368. function random: extended;
  369. begin
  370. random := cardinal(genrand_MT19937) * (1.0/(int64(1) shl 32));
  371. end;
  372. {****************************************************************************
  373. Memory Management
  374. ****************************************************************************}
  375. Function Ptr(sel,off : Longint) : farpointer;{$ifdef SYSTEMINLINE}inline;{$endif}{$ifndef INTERNCONSTINTF}[internconst:fpc_in_const_ptr];{$endif}
  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;{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[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. Function GetThreadID:SizeUInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  447. begin
  448. (* ThreadID is stored in a threadvar and made available in interface *)
  449. (* to allow setup of this value during thread initialization. *)
  450. GetThreadID := ThreadID;
  451. end;
  452. {*****************************************************************************
  453. Stack check code
  454. *****************************************************************************}
  455. {$IFNDEF NO_GENERIC_STACK_CHECK}
  456. {$IFOPT S+}
  457. {$DEFINE STACKCHECK}
  458. {$ENDIF}
  459. {$S-}
  460. procedure fpc_stackcheck(stack_size:Cardinal);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[public,alias:'FPC_STACKCHECK'];
  461. var
  462. c : Pointer;
  463. begin
  464. { Avoid recursive calls when called from the exit routines }
  465. if StackError then
  466. exit;
  467. c := Sptr - (stack_size + STACK_MARGIN);
  468. if (c <= StackBottom) then
  469. begin
  470. StackError:=true;
  471. HandleError(202);
  472. end;
  473. end;
  474. {$IFDEF STACKCHECK}
  475. {$S+}
  476. {$ENDIF}
  477. {$UNDEF STACKCHECK}
  478. {$ENDIF NO_GENERIC_STACK_CHECK}
  479. {*****************************************************************************
  480. Initialization / Finalization
  481. *****************************************************************************}
  482. const
  483. maxunits=1024; { See also files.pas of the compiler source }
  484. type
  485. TInitFinalRec=record
  486. InitProc,
  487. FinalProc : TProcedure;
  488. end;
  489. TInitFinalTable=record
  490. TableCount,
  491. InitCount : longint;
  492. Procs : array[1..maxunits] of TInitFinalRec;
  493. end;
  494. var
  495. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  496. procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; {$ifdef hascompilerproc} compilerproc; {$endif}
  497. var
  498. i : longint;
  499. begin
  500. { call cpu/fpu initialisation routine }
  501. fpc_cpuinit;
  502. with InitFinalTable do
  503. begin
  504. for i:=1 to TableCount do
  505. begin
  506. if assigned(Procs[i].InitProc) then
  507. Procs[i].InitProc();
  508. InitCount:=i;
  509. end;
  510. end;
  511. if assigned(InitProc) then
  512. TProcedure(InitProc)();
  513. end;
  514. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  515. begin
  516. with InitFinalTable do
  517. begin
  518. while (InitCount>0) do
  519. begin
  520. // we've to decrement the cound before calling the final. code
  521. // else a halt in the final. code leads to a endless loop
  522. dec(InitCount);
  523. if assigned(Procs[InitCount+1].FinalProc) then
  524. Procs[InitCount+1].FinalProc();
  525. end;
  526. end;
  527. end;
  528. {*****************************************************************************
  529. Error / Exit / ExitProc
  530. *****************************************************************************}
  531. Procedure system_exit;forward;
  532. Procedure InternalExit;
  533. var
  534. current_exit : Procedure;
  535. Begin
  536. while exitProc<>nil Do
  537. Begin
  538. InOutRes:=0;
  539. current_exit:=tProcedure(exitProc);
  540. exitProc:=nil;
  541. current_exit();
  542. End;
  543. { Finalize units }
  544. FinalizeUnits;
  545. { Show runtime error and exit }
  546. If erroraddr<>nil Then
  547. Begin
  548. Writeln(stdout,'Runtime error ',Errorcode,' at $',hexstr(PtrInt(Erroraddr),sizeof(PtrInt)*2));
  549. { to get a nice symify }
  550. Writeln(stdout,BackTraceStrFunc(Erroraddr));
  551. dump_stack(stdout,ErrorBase);
  552. Writeln(stdout,'');
  553. End;
  554. End;
  555. Procedure do_exit;[Public,Alias:'FPC_DO_EXIT'];
  556. begin
  557. InternalExit;
  558. System_exit;
  559. end;
  560. Procedure lib_exit;{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[Public,Alias:'FPC_LIB_EXIT'];
  561. begin
  562. InternalExit;
  563. end;
  564. Procedure Halt(ErrNum: Byte);
  565. Begin
  566. ExitCode:=Errnum;
  567. Do_Exit;
  568. end;
  569. function SysBackTraceStr (Addr: Pointer): ShortString;
  570. begin
  571. SysBackTraceStr:=' $'+HexStr(Ptrint(addr),sizeof(PtrInt)*2);
  572. end;
  573. Procedure HandleErrorAddrFrame (Errno : longint;addr,frame : Pointer);[public,alias:'FPC_BREAK_ERROR'];
  574. begin
  575. If pointer(ErrorProc)<>Nil then
  576. ErrorProc(Errno,addr,frame);
  577. errorcode:=word(Errno);
  578. erroraddr:=addr;
  579. errorbase:=frame;
  580. if errorcode <= maxExitCode then
  581. halt(errorcode)
  582. else
  583. halt(255)
  584. end;
  585. Procedure HandleErrorFrame (Errno : longint;frame : Pointer);
  586. {
  587. Procedure to handle internal errors, i.e. not user-invoked errors
  588. Internal function should ALWAYS call HandleError instead of RunError.
  589. Can be used for exception handlers to specify the frame
  590. }
  591. begin
  592. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  593. end;
  594. Procedure HandleError (Errno : longint);[public,alias : 'FPC_HANDLEERROR'];
  595. {
  596. Procedure to handle internal errors, i.e. not user-invoked errors
  597. Internal function should ALWAYS call HandleError instead of RunError.
  598. }
  599. begin
  600. HandleErrorFrame(Errno,get_frame);
  601. end;
  602. procedure RunError(w : word);[alias: 'FPC_RUNERROR'];
  603. begin
  604. errorcode:=w;
  605. erroraddr:=get_caller_addr(get_frame);
  606. errorbase:=get_caller_frame(get_frame);
  607. if errorcode <= maxExitCode then
  608. halt(errorcode)
  609. else
  610. halt(255)
  611. end;
  612. Procedure RunError;{$ifdef SYSTEMINLINE}inline;{$endif}
  613. Begin
  614. RunError (0);
  615. End;
  616. Procedure Halt;{$ifdef SYSTEMINLINE}inline;{$endif}
  617. Begin
  618. Halt(0);
  619. End;
  620. function do_isdevice(handle:thandle):boolean;forward;
  621. Procedure dump_stack(var f : text;bp : Pointer);
  622. var
  623. i : Longint;
  624. prevbp : Pointer;
  625. is_dev : boolean;
  626. caller_frame,
  627. caller_addr : Pointer;
  628. Begin
  629. prevbp:=bp-1;
  630. i:=0;
  631. is_dev:=do_isdevice(textrec(f).Handle);
  632. while bp > prevbp Do
  633. Begin
  634. caller_addr := get_caller_addr(bp);
  635. caller_frame := get_caller_frame(bp);
  636. if (caller_addr=nil) or
  637. (caller_frame=nil) then
  638. break;
  639. Writeln(f,BackTraceStrFunc(caller_addr));
  640. Inc(i);
  641. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  642. break;
  643. prevbp:=bp;
  644. bp:=caller_frame;
  645. End;
  646. End;
  647. Type
  648. PExitProcInfo = ^TExitProcInfo;
  649. TExitProcInfo = Record
  650. Next : PExitProcInfo;
  651. SaveExit : Pointer;
  652. Proc : TProcedure;
  653. End;
  654. const
  655. ExitProcList: PExitProcInfo = nil;
  656. Procedure DoExitProc;
  657. var
  658. P : PExitProcInfo;
  659. Proc : TProcedure;
  660. Begin
  661. P:=ExitProcList;
  662. ExitProcList:=P^.Next;
  663. ExitProc:=P^.SaveExit;
  664. Proc:=P^.Proc;
  665. DisPose(P);
  666. Proc();
  667. End;
  668. Procedure AddExitProc(Proc: TProcedure);
  669. var
  670. P : PExitProcInfo;
  671. Begin
  672. New(P);
  673. P^.Next:=ExitProcList;
  674. P^.SaveExit:=ExitProc;
  675. P^.Proc:=Proc;
  676. ExitProcList:=P;
  677. ExitProc:=@DoExitProc;
  678. End;
  679. function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):ppchar; // const ?
  680. // Extra allocate reserveentries pchar's at the beginning (default param=0 after 1.0.x ?)
  681. // Note: for internal use by skilled programmers only
  682. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  683. var p : ppchar;
  684. i : LongInt;
  685. begin
  686. if High(s)<Low(s) Then Exit(NIL);
  687. Getmem(p,sizeof(pchar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  688. // for cmd
  689. if p=nil then
  690. begin
  691. {$ifdef xunix}
  692. fpseterrno(ESysEnomem);
  693. {$endif}
  694. exit(NIL);
  695. end;
  696. for i:=low(s) to high(s) do
  697. p[i+Reserveentries]:=pchar(s[i]);
  698. p[high(s)+1+Reserveentries]:=nil;
  699. ArrayStringToPPchar:=p;
  700. end;
  701. Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):ppchar;
  702. {
  703. Create a PPChar to structure of pchars which are the arguments specified
  704. in the string S. Especially usefull for creating an ArgV for Exec-calls
  705. }
  706. begin
  707. StringToPPChar:=StringToPPChar(PChar(S),ReserveEntries);
  708. end;
  709. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  710. var
  711. i,nr : longint;
  712. Buf : ^char;
  713. p : ppchar;
  714. begin
  715. buf:=s;
  716. nr:=1;
  717. while (buf^<>#0) do // count nr of args
  718. begin
  719. while (buf^ in [' ',#9,#10]) do // Kill separators.
  720. inc(buf);
  721. inc(nr);
  722. if buf^='"' Then // quotes argument?
  723. begin
  724. inc(buf);
  725. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  726. inc(buf);
  727. if buf^='"' then // skip closing quote.
  728. inc(buf);
  729. end
  730. else
  731. begin // else std
  732. while not (buf^ in [' ',#0,#9,#10]) do
  733. inc(buf);
  734. end;
  735. end;
  736. getmem(p,(ReserveEntries+nr)*sizeof(pchar));
  737. StringToPPChar:=p;
  738. if p=nil then
  739. begin
  740. {$ifdef xunix}
  741. fpseterrno(ESysEnomem);
  742. {$endif}
  743. exit;
  744. end;
  745. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  746. buf:=s;
  747. while (buf^<>#0) do
  748. begin
  749. while (buf^ in [' ',#9,#10]) do // Kill separators.
  750. begin
  751. buf^:=#0;
  752. inc(buf);
  753. end;
  754. if buf^='"' Then // quotes argument?
  755. begin
  756. inc(buf);
  757. p^:=buf;
  758. inc(p);
  759. p^:=nil;
  760. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  761. inc(buf);
  762. if buf^='"' then // skip closing quote.
  763. begin
  764. buf^:=#0;
  765. inc(buf);
  766. end;
  767. end
  768. else
  769. begin
  770. p^:=buf;
  771. inc(p);
  772. p^:=nil;
  773. while not (buf^ in [' ',#0,#9,#10]) do
  774. inc(buf);
  775. end;
  776. end;
  777. end;
  778. {*****************************************************************************
  779. Abstract/Assert support.
  780. *****************************************************************************}
  781. procedure fpc_AbstractErrorIntern;{$ifdef hascompilerproc}compilerproc;{$endif}[public,alias : 'FPC_ABSTRACTERROR'];
  782. begin
  783. If pointer(AbstractErrorProc)<>nil then
  784. AbstractErrorProc();
  785. HandleErrorFrame(211,get_frame);
  786. end;
  787. Procedure fpc_assert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif} [Public,Alias : 'FPC_ASSERT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  788. begin
  789. if pointer(AssertErrorProc)<>nil then
  790. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  791. else
  792. HandleErrorFrame(227,get_frame);
  793. end;
  794. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
  795. begin
  796. If msg='' then
  797. write(stderr,'Assertion failed')
  798. else
  799. write(stderr,msg);
  800. Writeln(stderr,' (',FName,', line ',LineNo,').');
  801. Writeln(stderr,'');
  802. Halt(227);
  803. end;
  804. {*****************************************************************************
  805. SetJmp/LongJmp support.
  806. *****************************************************************************}
  807. {$i setjump.inc}
  808. {$ifdef IOCheckWasOn}
  809. {$I+}
  810. {$endif}
  811. {$ifdef RangeCheckWasOn}
  812. {$R+}
  813. {$endif}
  814. {$ifdef OverflowCheckWasOn}
  815. {$Q+}
  816. {$endif}
  817. {*****************************************************************************
  818. OS dependent Helpers/Syscalls
  819. *****************************************************************************}
  820. {$i sysos.inc}
  821. {*****************************************************************************
  822. Heap
  823. *****************************************************************************}
  824. {$i sysheap.inc}
  825. {$i heap.inc}
  826. {*****************************************************************************
  827. Thread support
  828. *****************************************************************************}
  829. { Generic threadmanager }
  830. {$i thread.inc}
  831. { Generic threadvar support }
  832. {$i threadvr.inc}
  833. { OS Dependent implementation }
  834. {$i systhrd.inc}
  835. {*****************************************************************************
  836. File Handling
  837. *****************************************************************************}
  838. { OS dependent low level file functions }
  839. {$i sysfile.inc}
  840. { Text file }
  841. {$i text.inc}
  842. { Untyped file }
  843. {$i file.inc}
  844. { Typed file }
  845. {$i typefile.inc}
  846. {*****************************************************************************
  847. Directory Handling
  848. *****************************************************************************}
  849. { OS dependent dir functions }
  850. {$i sysdir.inc}
  851. {
  852. $Log$
  853. Revision 1.78 2005-03-25 22:53:39 jonas
  854. * fixed several warnings and notes about unused variables (mainly) or
  855. uninitialised use of variables/function results (a few)
  856. Revision 1.77 2005/02/14 17:13:29 peter
  857. * truncate log
  858. Revision 1.76 2005/02/08 20:25:28 florian
  859. - killed power from system unit
  860. * move operator ** to math unit
  861. Revision 1.75 2005/02/06 20:35:54 florian
  862. + InitProc
  863. Revision 1.74 2005/02/06 16:57:18 peter
  864. * threads for go32v2,os,emx,netware
  865. Revision 1.73 2005/02/06 13:06:20 peter
  866. * moved file and dir functions to sysfile/sysdir
  867. * win32 thread in systemunit
  868. Revision 1.72 2005/02/06 11:20:52 peter
  869. * threading in system unit
  870. * removed systhrds unit
  871. }