system.inc 28 KB

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