system.inc 28 KB

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