system.inc 27 KB

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