system.inc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. {
  2. $Id$
  3. This file is part of the Free Pascal Run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. For details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {****************************************************************************
  12. Local types
  13. ****************************************************************************}
  14. {
  15. TextRec and FileRec are put in a separate file to make it available to other
  16. units without putting it explicitly in systemh.
  17. This way we keep TP compatibility, and the TextRec definition is available
  18. for everyone who needs it.
  19. }
  20. {$i filerec.inc}
  21. {$i textrec.inc}
  22. Procedure HandleError (Errno : Longint); forward;
  23. Procedure HandleErrorFrame (Errno : longint;frame : Pointer); forward;
  24. type
  25. FileFunc = Procedure(var t : TextRec);
  26. const
  27. STACK_MARGIN = 16384; { Stack size margin for stack checking }
  28. { Random / Randomize constants }
  29. OldRandSeed : Cardinal = 0;
  30. InitialSeed : Boolean = TRUE;
  31. Seed2 : Cardinal = 0;
  32. Seed3 : Cardinal = 0;
  33. { For Error Handling.}
  34. ErrorBase : Pointer = nil;
  35. { Used by the ansistrings and maybe also other things in the future }
  36. var
  37. emptychar : char;public name 'FPC_EMPTYCHAR';
  38. initialstklen : longint;external name '__stklen';
  39. {****************************************************************************
  40. Routines which have compiler magic
  41. ****************************************************************************}
  42. {$I innr.inc}
  43. Function lo(i : Integer) : byte; [INTERNPROC: In_lo_Word];
  44. Function lo(w : Word) : byte; [INTERNPROC: In_lo_Word];
  45. Function lo(l : Longint) : Word; [INTERNPROC: In_lo_long];
  46. Function lo(l : DWord) : Word; [INTERNPROC: In_lo_long];
  47. Function hi(i : Integer) : byte; [INTERNPROC: In_hi_Word];
  48. Function hi(w : Word) : byte; [INTERNPROC: In_hi_Word];
  49. Function hi(l : Longint) : Word; [INTERNPROC: In_hi_long];
  50. Function hi(l : DWord) : Word; [INTERNPROC: In_hi_long];
  51. Function lo(q : QWord) : DWord; [INTERNPROC: In_lo_qword];
  52. Function lo(i : Int64) : DWord; [INTERNPROC: In_lo_qword];
  53. Function hi(q : QWord) : DWord; [INTERNPROC: In_hi_qword];
  54. Function hi(i : Int64) : DWord; [INTERNPROC: In_hi_qword];
  55. Function chr(b : byte) : Char; [INTERNPROC: In_chr_byte];
  56. {$ifndef INTERNLENGTH}
  57. Function Length(s : string) : byte; [INTERNPROC: In_Length_string];
  58. Function Length(c : char) : byte; [INTERNPROC: In_Length_string];
  59. {$endif INTERNLENGTH}
  60. Procedure Reset(var f : TypedFile); [INTERNPROC: In_Reset_TypedFile];
  61. Procedure Rewrite(var f : TypedFile); [INTERNPROC: In_Rewrite_TypedFile];
  62. {****************************************************************************
  63. Include processor specific routines
  64. ****************************************************************************}
  65. {$ifdef cpui386}
  66. {$ifdef SYSPROCDEFINED}
  67. {$Error Can't determine processor type !}
  68. {$endif}
  69. {$define ENDIAN_LITTLE}
  70. {$i i386.inc} { Case dependent, don't change }
  71. {$endif cpui386}
  72. {$ifdef cpum68k}
  73. {$ifdef SYSPROCDEFINED}
  74. {$Error Can't determine processor type !}
  75. {$endif}
  76. {$define ENDIAN_BIG}
  77. {$i m68k.inc} { Case dependent, don't change }
  78. {$define SYSPROCDEFINED}
  79. {$endif cpum68k}
  80. {$ifdef cpux86_64}
  81. {$ifdef SYSPROCDEFINED}
  82. {$Error Can't determine processor type !}
  83. {$endif}
  84. {$define ENDIAN_LITTLE}
  85. {$i x86_64.inc} { Case dependent, don't change }
  86. {$define SYSPROCDEFINED}
  87. {$endif cpux86_64}
  88. {$ifdef cpupowerpc}
  89. {$ifdef SYSPROCDEFINED}
  90. {$Error Can't determine processor type !}
  91. {$endif}
  92. {$define ENDIAN_BIG}
  93. {$i powerpc.inc} { Case dependent, don't change }
  94. {$define SYSPROCDEFINED}
  95. {$endif cpupowerpc}
  96. {$ifdef cpualpha}
  97. {$ifdef SYSPROCDEFINED}
  98. {$Error Can't determine processor type !}
  99. {$endif}
  100. {$define ENDIAN_BIG}
  101. {$i alpha.inc} { Case dependent, don't change }
  102. {$define SYSPROCDEFINED}
  103. {$endif cpualpha}
  104. {$ifdef cpuiA64}
  105. {$ifdef SYSPROCDEFINED}
  106. {$Error Can't determine processor type !}
  107. {$endif}
  108. {$define ENDIAN_LITTLE}
  109. {$i ia64.inc} { Case dependent, don't change }
  110. {$define SYSPROCDEFINED}
  111. {$endif cpuiA64}
  112. {$ifdef cpusparc}
  113. {$ifdef SYSPROCDEFINED}
  114. {$Error Can't determine processor type !}
  115. {$endif}
  116. {$define ENDIAN_BIG}
  117. {$i sparc.inc} { Case dependent, don't change }
  118. {$define SYSPROCDEFINED}
  119. {$endif cpusparc}
  120. procedure fillchar(var x;count : longint;value : boolean);{$ifdef SYSTEMINLINE}inline;{$endif}
  121. begin
  122. fillchar(x,count,byte(value));
  123. end;
  124. procedure fillchar(var x;count : longint;value : char);{$ifdef SYSTEMINLINE}inline;{$endif}
  125. begin
  126. fillchar(x,count,byte(value));
  127. end;
  128. { Include generic pascal only routines which are not defined in the processor
  129. specific include file }
  130. {$I generic.inc}
  131. {****************************************************************************
  132. Set Handling
  133. ****************************************************************************}
  134. { Include set support which is processor specific}
  135. {$i set.inc}
  136. { Include generic pascal routines for sets if the processor }
  137. { specific routines are not available. }
  138. {$i genset.inc}
  139. {****************************************************************************
  140. Math Routines
  141. ****************************************************************************}
  142. function Hi(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  143. begin
  144. Hi := b shr 4
  145. end;
  146. function Lo(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  147. begin
  148. Lo := b and $0f
  149. end;
  150. Function swap (X : Word) : Word;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_swap_word];
  151. Begin
  152. swap:=(X and $ff) shl 8 + (X shr 8)
  153. End;
  154. Function Swap (X : Integer) : Integer;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_swap_word];
  155. Begin
  156. swap:=(X and $ff) shl 8 + (X shr 8)
  157. End;
  158. Function swap (X : Longint) : Longint;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_swap_long];
  159. Begin
  160. Swap:=(X and $ffff) shl 16 + (X shr 16)
  161. End;
  162. Function Swap (X : Cardinal) : Cardinal;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_swap_long];
  163. Begin
  164. Swap:=(X and $ffff) shl 16 + (X shr 16)
  165. End;
  166. Function Swap (X : QWord) : QWord;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_swap_qword];
  167. Begin
  168. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  169. End;
  170. Function swap (X : Int64) : Int64;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_swap_qword];
  171. Begin
  172. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  173. End;
  174. operator := (b:real48) d:double;
  175. begin
  176. D:=real2double(b);
  177. end;
  178. {$ifdef SUPPORT_EXTENDED}
  179. operator := (b:real48) e:extended;
  180. begin
  181. e:=real2double(b);
  182. end;
  183. {$endif SUPPORT_EXTENDED}
  184. { Include processor specific routines }
  185. {$I math.inc}
  186. { Include generic version }
  187. {$I genmath.inc}
  188. {****************************************************************************
  189. Subroutines for String handling
  190. ****************************************************************************}
  191. { Needs to be before RTTI handling }
  192. {$i sstrings.inc}
  193. { requires sstrings.inc for initval }
  194. {$I int64.inc}
  195. {Requires int64.inc, since that contains the VAL functions for int64 and qword}
  196. {$i astrings.inc}
  197. {$ifdef HASWIDESTRING}
  198. {$i wstrings.inc}
  199. {$endif HASWIDESTRING}
  200. {$i aliases.inc}
  201. {*****************************************************************************
  202. Dynamic Array support
  203. *****************************************************************************}
  204. {$i dynarr.inc}
  205. {*****************************************************************************
  206. Object Pascal support
  207. *****************************************************************************}
  208. {$i objpas.inc}
  209. {*****************************************************************************
  210. Variant support
  211. *****************************************************************************}
  212. {$ifdef HASVARIANT}
  213. {$i variant.inc}
  214. {$endif HASVARIANT}
  215. {****************************************************************************
  216. Run-Time Type Information (RTTI)
  217. ****************************************************************************}
  218. {$i rtti.inc}
  219. {****************************************************************************
  220. Random function routines
  221. This implements a very long cycle random number generator by combining
  222. three independant generators. The technique was described in the March
  223. 1987 issue of Byte.
  224. Taken and modified with permission from the PCQ Pascal rtl code.
  225. ****************************************************************************}
  226. {$R-}
  227. {$Q-}
  228. Procedure NewSeed;Forward;
  229. Function Random : Extended;
  230. begin
  231. if (InitialSeed) OR (RandSeed <> OldRandSeed) then
  232. Begin
  233. { This is a pretty complicated affair }
  234. { Initially we must call NewSeed when RandSeed is initalized }
  235. { We must also call NewSeed each time RandSeed is reinitialized }
  236. { DO NOT CHANGE THE ORDER OF DECLARATIONS IN THIS BLOCK }
  237. { UNLESS YOU WANT RANDON TO CRASH OF COURSE (CEC) }
  238. InitialSeed:=FALSE;
  239. OldRandSeed:=RandSeed;
  240. NewSeed;
  241. end;
  242. Inc(RandSeed);
  243. RandSeed := (RandSeed * 706) mod 500009;
  244. OldRandSeed:=RandSeed;
  245. INC(Seed2);
  246. Seed2 := (Seed2 * 774) MOD 600011;
  247. INC(Seed3);
  248. Seed3 := (Seed3 * 871) MOD 765241;
  249. Random :=
  250. frac(RandSeed/500009.0 +
  251. Seed2/600011.0 +
  252. Seed3/765241.0);
  253. end;
  254. Function internRandom(l : Cardinal) : Cardinal;
  255. begin
  256. if (InitialSeed) OR (RandSeed <> OldRandSeed) then
  257. Begin
  258. { This is a pretty complicated affair }
  259. { Initially we must call NewSeed when RandSeed is initalized }
  260. { We must also call NewSeed each time RandSeed is reinitialized }
  261. { DO NOT CHANGE THE ORDER OF DECLARATIONS IN THIS BLOCK }
  262. { UNLESS YOU WANT RANDOM TO CRASH OF COURSE (CEC) }
  263. InitialSeed:=FALSE;
  264. OldRandSeed:=RandSeed;
  265. NewSeed;
  266. end;
  267. Inc(RandSeed);
  268. RandSeed := (RandSeed * 998) mod 1000003;
  269. OldRandSeed:=RandSeed;
  270. if l<>0 then
  271. begin
  272. internRandom := RandSeed mod l;
  273. end
  274. else internRandom:=0;
  275. end;
  276. function random(l:cardinal): cardinal;
  277. begin
  278. random := trunc(random()*l);
  279. end;
  280. function random(l:longint): longint;
  281. begin
  282. random := trunc(random()*l);
  283. end;
  284. Procedure NewSeed;
  285. begin
  286. randseed := randseed mod 1000003;
  287. Seed2 := (internRandom(65000) * internRandom(65000)) mod 600011;
  288. Seed3 := (internRandom(65000) * internRandom(65000)) mod 765241;
  289. end;
  290. {****************************************************************************
  291. Memory Management
  292. ****************************************************************************}
  293. Function Ptr(sel,off : Longint) : farpointer;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_ptr];
  294. Begin
  295. ptr:=farpointer((sel shl 4)+off);
  296. End;
  297. Function CSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  298. Begin
  299. Cseg:=0;
  300. End;
  301. Function DSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  302. Begin
  303. Dseg:=0;
  304. End;
  305. Function SSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  306. Begin
  307. Sseg:=0;
  308. End;
  309. {*****************************************************************************
  310. Directory support.
  311. *****************************************************************************}
  312. Procedure getdir(drivenr:byte;Var dir:ansistring);
  313. { this is needed to also allow ansistrings, the shortstring version is
  314. OS dependent }
  315. var
  316. s : shortstring;
  317. begin
  318. getdir(drivenr,s);
  319. dir:=s;
  320. end;
  321. {$ifopt R+}
  322. {$define RangeCheckWasOn}
  323. {$R-}
  324. {$endif opt R+}
  325. {$ifopt I+}
  326. {$define IOCheckWasOn}
  327. {$I-}
  328. {$endif opt I+}
  329. {$ifopt Q+}
  330. {$define OverflowCheckWasOn}
  331. {$Q-}
  332. {$endif opt Q+}
  333. {*****************************************************************************
  334. Miscellaneous
  335. *****************************************************************************}
  336. procedure fpc_rangeerror;[public,alias:'FPC_RANGEERROR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  337. begin
  338. HandleErrorFrame(201,get_frame);
  339. end;
  340. procedure fpc_overflow;[public,alias:'FPC_OVERFLOW']; {$ifdef hascompilerproc} compilerproc; {$endif}
  341. begin
  342. HandleErrorFrame(215,get_frame);
  343. end;
  344. procedure fpc_iocheck;[saveregisters,public,alias:'FPC_IOCHECK']; {$ifdef hascompilerproc} compilerproc; {$endif}
  345. var
  346. l : longint;
  347. begin
  348. if InOutRes<>0 then
  349. begin
  350. l:=InOutRes;
  351. InOutRes:=0;
  352. HandleErrorFrame(l,get_frame);
  353. end;
  354. end;
  355. Function IOResult:Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  356. Begin
  357. IOResult:=InOutRes;
  358. InOutRes:=0;
  359. End;
  360. {*****************************************************************************
  361. Stack check code
  362. *****************************************************************************}
  363. {$IFNDEF NO_GENERIC_STACK_CHECK}
  364. {$IFOPT S+}
  365. {$DEFINE STACKCHECK}
  366. {$ENDIF}
  367. {$S-}
  368. procedure fpc_stackcheck(stack_size:Cardinal);[saveregisters,public,alias:'FPC_STACKCHECK'];
  369. var
  370. c : cardinal;
  371. begin
  372. { Avoid recursive calls when called from the exit routines }
  373. if StackError then
  374. exit;
  375. c := cardinal(Sptr) - stack_size - STACK_MARGIN;
  376. if (c <= StackBottom) then
  377. begin
  378. StackError:=true;
  379. HandleError(202);
  380. end;
  381. end;
  382. {$IFDEF STACKCHECK}
  383. {$S+}
  384. {$ENDIF}
  385. {$UNDEF STACKCHECK}
  386. {$ENDIF NO_GENERIC_STACK_CHECK}
  387. {*****************************************************************************
  388. Initialization / Finalization
  389. *****************************************************************************}
  390. const
  391. maxunits=1024; { See also files.pas of the compiler source }
  392. type
  393. TInitFinalRec=record
  394. InitProc,
  395. FinalProc : TProcedure;
  396. end;
  397. TInitFinalTable=record
  398. TableCount,
  399. InitCount : longint;
  400. Procs : array[1..maxunits] of TInitFinalRec;
  401. end;
  402. var
  403. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  404. procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; {$ifdef hascompilerproc} compilerproc; {$endif}
  405. var
  406. i : longint;
  407. begin
  408. with InitFinalTable do
  409. begin
  410. for i:=1to TableCount do
  411. begin
  412. if assigned(Procs[i].InitProc) then
  413. Procs[i].InitProc();
  414. InitCount:=i;
  415. end;
  416. end;
  417. end;
  418. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  419. begin
  420. with InitFinalTable do
  421. begin
  422. while (InitCount>0) do
  423. begin
  424. // we've to decrement the cound before calling the final. code
  425. // else a halt in the final. code leads to a endless loop
  426. dec(InitCount);
  427. if assigned(Procs[InitCount+1].FinalProc) then
  428. Procs[InitCount+1].FinalProc();
  429. end;
  430. end;
  431. end;
  432. {*****************************************************************************
  433. Error / Exit / ExitProc
  434. *****************************************************************************}
  435. Procedure system_exit;forward;
  436. Procedure InternalExit;
  437. var
  438. current_exit : Procedure;
  439. Begin
  440. while exitProc<>nil Do
  441. Begin
  442. InOutRes:=0;
  443. current_exit:=tProcedure(exitProc);
  444. exitProc:=nil;
  445. current_exit();
  446. End;
  447. { Finalize units }
  448. FinalizeUnits;
  449. { Show runtime error and exit }
  450. If erroraddr<>nil Then
  451. Begin
  452. Writeln(stdout,'Runtime error ',Errorcode,' at 0x',hexstr(Longint(Erroraddr),8));
  453. { to get a nice symify }
  454. Writeln(stdout,BackTraceStrFunc(Erroraddr));
  455. dump_stack(stdout,ErrorBase);
  456. Writeln(stdout,'');
  457. End;
  458. End;
  459. Procedure do_exit;[Public,Alias:'FPC_DO_EXIT'];
  460. begin
  461. InternalExit;
  462. System_exit;
  463. end;
  464. Procedure lib_exit;saveregisters;[Public,Alias:'FPC_LIB_EXIT'];
  465. begin
  466. InternalExit;
  467. end;
  468. Procedure Halt(ErrNum: Byte);
  469. Begin
  470. ExitCode:=Errnum;
  471. Do_Exit;
  472. end;
  473. function SysBackTraceStr (Addr: Pointer): ShortString;
  474. begin
  475. SysBackTraceStr:=' 0x'+HexStr(Longint(addr),8);
  476. end;
  477. Procedure HandleErrorAddrFrame (Errno : longint;addr,frame : Pointer);[public,alias:'FPC_BREAK_ERROR'];
  478. begin
  479. If pointer(ErrorProc)<>Nil then
  480. ErrorProc(Errno,addr,frame);
  481. errorcode:=word(Errno);
  482. exitcode:=word(Errno);
  483. erroraddr:=addr;
  484. errorbase:=frame;
  485. halt(errorcode);
  486. end;
  487. Procedure HandleErrorFrame (Errno : longint;frame : Pointer);
  488. {
  489. Procedure to handle internal errors, i.e. not user-invoked errors
  490. Internal function should ALWAYS call HandleError instead of RunError.
  491. Can be used for exception handlers to specify the frame
  492. }
  493. begin
  494. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  495. end;
  496. Procedure HandleError (Errno : longint);[public,alias : 'FPC_HANDLEERROR'];
  497. {
  498. Procedure to handle internal errors, i.e. not user-invoked errors
  499. Internal function should ALWAYS call HandleError instead of RunError.
  500. }
  501. begin
  502. HandleErrorFrame(Errno,get_frame);
  503. end;
  504. procedure runerror(w : word);[alias: 'FPC_RUNERROR'];
  505. begin
  506. errorcode:=w;
  507. exitcode:=w;
  508. erroraddr:=get_caller_addr(get_frame);
  509. errorbase:=get_caller_frame(get_frame);
  510. halt(errorcode);
  511. end;
  512. Procedure RunError;{$ifdef SYSTEMINLINE}inline;{$endif}
  513. Begin
  514. RunError (0);
  515. End;
  516. Procedure Halt;{$ifdef SYSTEMINLINE}inline;{$endif}
  517. Begin
  518. Halt(0);
  519. End;
  520. function do_isdevice(handle:longint):boolean;forward;
  521. Procedure dump_stack(var f : text;bp : Pointer);
  522. var
  523. i : Longint;
  524. prevbp : Pointer;
  525. is_dev : boolean;
  526. caller_addr : Pointer;
  527. Begin
  528. prevbp:=bp-1;
  529. i:=0;
  530. is_dev:=do_isdevice(textrec(f).Handle);
  531. while bp > prevbp Do
  532. Begin
  533. caller_addr := get_caller_addr(bp);
  534. if caller_addr <> nil then
  535. Writeln(f,BackTraceStrFunc(caller_addr));
  536. Inc(i);
  537. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  538. exit;
  539. prevbp:=bp;
  540. bp:=get_caller_frame(bp);
  541. End;
  542. End;
  543. Type
  544. PExitProcInfo = ^TExitProcInfo;
  545. TExitProcInfo = Record
  546. Next : PExitProcInfo;
  547. SaveExit : Pointer;
  548. Proc : TProcedure;
  549. End;
  550. const
  551. ExitProcList: PExitProcInfo = nil;
  552. Procedure DoExitProc;
  553. var
  554. P : PExitProcInfo;
  555. Proc : TProcedure;
  556. Begin
  557. P:=ExitProcList;
  558. ExitProcList:=P^.Next;
  559. ExitProc:=P^.SaveExit;
  560. Proc:=P^.Proc;
  561. DisPose(P);
  562. Proc();
  563. End;
  564. Procedure AddExitProc(Proc: TProcedure);
  565. var
  566. P : PExitProcInfo;
  567. Begin
  568. New(P);
  569. P^.Next:=ExitProcList;
  570. P^.SaveExit:=ExitProc;
  571. P^.Proc:=Proc;
  572. ExitProcList:=P;
  573. ExitProc:=@DoExitProc;
  574. End;
  575. {*****************************************************************************
  576. Abstract/Assert support.
  577. *****************************************************************************}
  578. procedure AbstractError;[public,alias : 'FPC_ABSTRACTERROR'];
  579. begin
  580. If pointer(AbstractErrorProc)<>nil then
  581. AbstractErrorProc();
  582. HandleErrorFrame(211,get_frame);
  583. end;
  584. {$ifdef hascompilerproc}
  585. { alias for internal usage in the compiler }
  586. procedure fpc_AbstractErrorIntern; compilerproc; external name 'FPC_ABSTRACTERROR';
  587. {$endif hascompilerproc}
  588. Procedure fpc_assert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer); [SaveRegisters,Public,Alias : 'FPC_ASSERT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  589. begin
  590. if pointer(AssertErrorProc)<>nil then
  591. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  592. else
  593. HandleErrorFrame(227,get_frame);
  594. end;
  595. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
  596. begin
  597. If msg='' then
  598. write(stderr,'Assertion failed')
  599. else
  600. write(stderr,msg);
  601. Writeln(stderr,' (',FName,', line ',LineNo,').');
  602. Writeln(stderr,'');
  603. Halt(227);
  604. end;
  605. {*****************************************************************************
  606. SetJmp/LongJmp support.
  607. *****************************************************************************}
  608. {$i setjump.inc}
  609. {$ifdef IOCheckWasOn}
  610. {$I+}
  611. {$endif}
  612. {$ifdef RangeCheckWasOn}
  613. {$R+}
  614. {$endif}
  615. {$ifdef OverflowCheckWasOn}
  616. {$Q+}
  617. {$endif}
  618. {
  619. $Log$
  620. Revision 1.41 2003-08-21 22:10:55 olle
  621. - removed parameter from fpc_iocheck
  622. * changed processor compiler directive * to cpu*
  623. Revision 1.40 2003/03/17 14:30:11 peter
  624. * changed address parameter/return values to pointer instead
  625. of longint
  626. Revision 1.39 2003/02/05 21:48:34 mazen
  627. * fixing run time errors related to unimplemented abstract methods in CG
  628. + giving empty emplementations for some RTL functions
  629. Revision 1.38 2002/12/07 14:36:33 carl
  630. - avoid warnings (add typecast)
  631. Revision 1.37 2002/11/18 18:33:51 peter
  632. * Swap(QWord) constant support
  633. Revision 1.36 2002/10/14 19:39:17 peter
  634. * threads unit added for thread support
  635. Revision 1.35 2002/09/18 18:32:01 carl
  636. * assert now halts with exitcode 227 (as Delphi does)
  637. Revision 1.34 2002/09/07 15:07:46 peter
  638. * old logs removed and tabs fixed
  639. Revision 1.33 2002/08/19 19:34:02 peter
  640. * SYSTEMINLINE define that will add inline directives for small
  641. functions and wrappers. This will be defined automaticly when
  642. the compiler defines the HASINLINE directive
  643. Revision 1.32 2002/07/28 20:43:48 florian
  644. * several fixes for linux/powerpc
  645. * several fixes to MT
  646. Revision 1.31 2002/07/26 22:46:06 florian
  647. * interface of system unit for Linux/PowerPC compiles
  648. Revision 1.30 2002/07/26 16:42:00 florian
  649. * endian directive for PowerPC fixed
  650. Revision 1.29 2002/07/04 20:40:09 florian
  651. + some x86-64 support added
  652. Revision 1.28 2002/04/21 15:51:50 carl
  653. * StackError is now a typed constant
  654. + $S can be used under unix
  655. Revision 1.27 2002/04/15 19:38:40 peter
  656. * stackcheck protected against infinite recursive after stack error
  657. * stackcheck requires saveregisters, because it can be called from
  658. iocheck and then will destroy the result of the original function
  659. Revision 1.26 2002/04/15 18:51:20 carl
  660. + generic stack checking can be overriden
  661. Revision 1.25 2002/04/12 17:37:36 carl
  662. + generic stack checking
  663. }