system.inc 27 KB

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