system.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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 : longint); forward;
  24. type
  25. FileFunc = Procedure(var t : TextRec);
  26. PByte = ^Byte;
  27. PWord = ^word;
  28. PDWord = ^DWord;
  29. PLongint = ^Longint;
  30. const
  31. { Random / Randomize constants }
  32. OldRandSeed : Cardinal = 0;
  33. InitialSeed : Boolean = TRUE;
  34. Seed2 : Cardinal = 0;
  35. Seed3 : Cardinal = 0;
  36. { For Error Handling.}
  37. ErrorBase : Longint = 0;
  38. { Used by the ansistrings and maybe also other things in the future }
  39. var
  40. emptychar : char;public name 'FPC_EMPTYCHAR';
  41. {****************************************************************************
  42. Routines which have compiler magic
  43. ****************************************************************************}
  44. {$I innr.inc}
  45. Function lo(i : Integer) : byte; [INTERNPROC: In_lo_Word];
  46. Function lo(w : Word) : byte; [INTERNPROC: In_lo_Word];
  47. Function lo(l : Longint) : Word; [INTERNPROC: In_lo_long];
  48. Function lo(l : DWord) : Word; [INTERNPROC: In_lo_long];
  49. Function hi(i : Integer) : byte; [INTERNPROC: In_hi_Word];
  50. Function hi(w : Word) : byte; [INTERNPROC: In_hi_Word];
  51. Function hi(l : Longint) : Word; [INTERNPROC: In_hi_long];
  52. Function hi(l : DWord) : Word; [INTERNPROC: In_hi_long];
  53. {$ifdef INT64}
  54. Function lo(q : QWord) : DWord; [INTERNPROC: In_lo_qword];
  55. Function lo(i : Int64) : DWord; [INTERNPROC: In_lo_qword];
  56. Function hi(q : QWord) : DWord; [INTERNPROC: In_hi_qword];
  57. Function hi(i : Int64) : DWord; [INTERNPROC: In_hi_qword];
  58. {$endif}
  59. Function chr(b : byte) : Char; [INTERNPROC: In_chr_byte];
  60. Function Length(s : string) : byte; [INTERNPROC: In_Length_string];
  61. Function Length(c : char) : byte; [INTERNPROC: In_Length_string];
  62. Procedure Reset(var f : TypedFile); [INTERNPROC: In_Reset_TypedFile];
  63. Procedure Rewrite(var f : TypedFile); [INTERNPROC: In_Rewrite_TypedFile];
  64. {****************************************************************************
  65. Include processor specific routines
  66. ****************************************************************************}
  67. {$IFDEF I386}
  68. {$IFDEF M68K}
  69. {$Error Can't determine processor type !}
  70. {$ENDIF}
  71. {$I i386.inc} { Case dependent, don't change }
  72. {$ELSE}
  73. {$IFDEF M68K}
  74. {$I m68k.inc} { Case dependent, don't change }
  75. {$ELSE}
  76. {$Error Can't determine processor type !}
  77. {$ENDIF}
  78. {$ENDIF}
  79. { Include generic pascal only routines which are not defined in the processor
  80. specific include file }
  81. {$I generic.inc}
  82. {****************************************************************************
  83. Set Handling
  84. ****************************************************************************}
  85. { Include set support which is processor specific}
  86. {$I set.inc}
  87. {****************************************************************************
  88. Math Routines
  89. ****************************************************************************}
  90. {$ifndef RTLLITE}
  91. function Hi(b : byte): byte;
  92. begin
  93. Hi := b shr 4
  94. end;
  95. function Lo(b : byte): byte;
  96. begin
  97. Lo := b and $0f
  98. end;
  99. Function swap (X : Word) : Word;[internconst:in_const_swap_word];
  100. Begin
  101. swap:=(X and $ff) shl 8 + (X shr 8)
  102. End;
  103. Function Swap (X : Integer) : Integer;[internconst:in_const_swap_word];
  104. Begin
  105. swap:=(X and $ff) shl 8 + (X shr 8)
  106. End;
  107. Function swap (X : Longint) : Longint;[internconst:in_const_swap_long];
  108. Begin
  109. Swap:=(X and $ffff) shl 16 + (X shr 16)
  110. End;
  111. Function Swap (X : Cardinal) : Cardinal;[internconst:in_const_swap_long];
  112. Begin
  113. Swap:=(X and $ffff) shl 16 + (X shr 16)
  114. End;
  115. {$ifdef INT64}
  116. Function Swap (X : QWord) : QWord;
  117. Begin
  118. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  119. End;
  120. Function swap (X : Int64) : Int64;
  121. Begin
  122. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  123. End;
  124. {$endif}
  125. {$endif RTLLITE}
  126. { Include processor specific routines }
  127. {$I math.inc}
  128. {****************************************************************************
  129. Subroutines for String handling
  130. ****************************************************************************}
  131. { Needs to be before RTTI handling }
  132. {$i sstrings.inc}
  133. {$i astrings.inc}
  134. {$ifdef haswidechar}
  135. {$i wstrings.inc}
  136. {$endif haswidechar}
  137. {****************************************************************************
  138. Run-Time Type Information (RTTI)
  139. ****************************************************************************}
  140. {$i rtti.inc}
  141. { requires sstrings.inc for initval }
  142. {$ifdef INT64}
  143. {$I int64.inc}
  144. {$endif INT64}
  145. {****************************************************************************
  146. Random function routines
  147. This implements a very long cycle random number generator by combining
  148. three independant generators. The technique was described in the March
  149. 1987 issue of Byte.
  150. Taken and modified with permission from the PCQ Pascal rtl code.
  151. ****************************************************************************}
  152. {$R-}
  153. {$Q-}
  154. Procedure NewSeed;Forward;
  155. Function Random : Extended;
  156. begin
  157. if (InitialSeed) OR (RandSeed <> OldRandSeed) then
  158. Begin
  159. { This is a pretty complicated affair }
  160. { Initially we must call NewSeed when RandSeed is initalized }
  161. { We must also call NewSeed each time RandSeed is reinitialized }
  162. { DO NOT CHANGE THE ORDER OF DECLARATIONS IN THIS BLOCK }
  163. { UNLESS YOU WANT RANDON TO CRASH OF COURSE (CEC) }
  164. InitialSeed:=FALSE;
  165. OldRandSeed:=RandSeed;
  166. NewSeed;
  167. end;
  168. Inc(RandSeed);
  169. RandSeed := (RandSeed * 706) mod 500009;
  170. OldRandSeed:=RandSeed;
  171. INC(Seed2);
  172. Seed2 := (Seed2 * 774) MOD 600011;
  173. INC(Seed3);
  174. Seed3 := (Seed3 * 871) MOD 765241;
  175. Random :=
  176. frac(RandSeed/500009.0 +
  177. Seed2/600011.0 +
  178. Seed3/765241.0);
  179. end;
  180. Function internRandom(l : Cardinal) : Cardinal;
  181. begin
  182. if (InitialSeed) OR (RandSeed <> OldRandSeed) then
  183. Begin
  184. { This is a pretty complicated affair }
  185. { Initially we must call NewSeed when RandSeed is initalized }
  186. { We must also call NewSeed each time RandSeed is reinitialized }
  187. { DO NOT CHANGE THE ORDER OF DECLARATIONS IN THIS BLOCK }
  188. { UNLESS YOU WANT RANDOM TO CRASH OF COURSE (CEC) }
  189. InitialSeed:=FALSE;
  190. OldRandSeed:=RandSeed;
  191. NewSeed;
  192. end;
  193. Inc(RandSeed);
  194. RandSeed := (RandSeed * 998) mod 1000003;
  195. OldRandSeed:=RandSeed;
  196. if l<>0 then
  197. begin
  198. internRandom := RandSeed mod l;
  199. end
  200. else internRandom:=0;
  201. end;
  202. function random(l:cardinal): cardinal;
  203. begin
  204. random := trunc(random()*l);
  205. end;
  206. {$ifndef cardinalmulfixed}
  207. function random(l:longint): longint;
  208. begin
  209. random := trunc(random()*l);
  210. end;
  211. {$endif cardinalmulfixed}
  212. Procedure NewSeed;
  213. begin
  214. randseed := randseed mod 1000003;
  215. Seed2 := (internRandom(65000) * internRandom(65000)) mod 600011;
  216. Seed3 := (internRandom(65000) * internRandom(65000)) mod 765241;
  217. end;
  218. {****************************************************************************
  219. Memory Management
  220. ****************************************************************************}
  221. {$ifndef RTLLITE}
  222. Function Ptr(sel,off : Longint) : pointer;[internconst:in_const_ptr];
  223. Begin
  224. sel:=0;
  225. ptr:=pointer(off);
  226. End;
  227. Function CSeg : Word;
  228. Begin
  229. Cseg:=0;
  230. End;
  231. Function DSeg : Word;
  232. Begin
  233. Dseg:=0;
  234. End;
  235. Function SSeg : Word;
  236. Begin
  237. Sseg:=0;
  238. End;
  239. {$endif RTLLITE}
  240. {*****************************************************************************
  241. Directory support.
  242. *****************************************************************************}
  243. Procedure getdir(drivenr:byte;Var dir:ansistring);
  244. { this is needed to also allow ansistrings, the shortstring version is
  245. OS dependent }
  246. var
  247. s : shortstring;
  248. begin
  249. getdir(drivenr,s);
  250. dir:=s;
  251. end;
  252. {*****************************************************************************
  253. Miscellaneous
  254. *****************************************************************************}
  255. procedure int_overflow;[public,alias:'FPC_OVERFLOW'];
  256. begin
  257. HandleErrorFrame(215,get_frame);
  258. end;
  259. {$ifdef HASSAVEREGISTERS}
  260. procedure int_iocheck(addr : longint);[saveregisters,public,alias:'FPC_IOCHECK'];
  261. var
  262. l : longint;
  263. begin
  264. if InOutRes<>0 then
  265. begin
  266. l:=InOutRes;
  267. InOutRes:=0;
  268. HandleErrorFrame(l,get_frame);
  269. end;
  270. end;
  271. {$endif}
  272. Function IOResult:Word;
  273. Begin
  274. IOResult:=InOutRes;
  275. InOutRes:=0;
  276. End;
  277. procedure fillchar(var x;count : longint;value : boolean);
  278. begin
  279. fillchar(x,count,byte(value));
  280. end;
  281. procedure fillchar(var x;count : longint;value : char);
  282. begin
  283. fillchar(x,count,byte(value));
  284. end;
  285. {*****************************************************************************
  286. Initialization / Finalization
  287. *****************************************************************************}
  288. const
  289. maxunits=1024; { See also files.pas of the compiler source }
  290. type
  291. TInitFinalRec=record
  292. InitProc,
  293. FinalProc : TProcedure;
  294. end;
  295. TInitFinalTable=record
  296. TableCount,
  297. InitCount : longint;
  298. Procs : array[1..maxunits] of TInitFinalRec;
  299. end;
  300. var
  301. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  302. procedure InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS'];
  303. var
  304. i : longint;
  305. begin
  306. with InitFinalTable do
  307. begin
  308. for i:=1to TableCount do
  309. begin
  310. if assigned(Procs[i].InitProc) then
  311. Procs[i].InitProc();
  312. InitCount:=i;
  313. end;
  314. end;
  315. end;
  316. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  317. begin
  318. with InitFinalTable do
  319. begin
  320. while (InitCount>0) do
  321. begin
  322. // we've to decrement the cound before calling the final. code
  323. // else a halt in the final. code leads to a endless loop
  324. dec(InitCount);
  325. if assigned(Procs[InitCount+1].FinalProc) then
  326. Procs[InitCount+1].FinalProc();
  327. end;
  328. end;
  329. end;
  330. {*****************************************************************************
  331. Error / Exit / ExitProc
  332. *****************************************************************************}
  333. Procedure system_exit;forward;
  334. Procedure do_exit;[Public,Alias:'FPC_DO_EXIT'];
  335. var
  336. current_exit : Procedure;
  337. Begin
  338. while exitProc<>nil Do
  339. Begin
  340. InOutRes:=0;
  341. current_exit:=tProcedure(exitProc);
  342. exitProc:=nil;
  343. current_exit();
  344. End;
  345. { Finalize units }
  346. FinalizeUnits;
  347. { Show runtime error }
  348. If erroraddr<>nil Then
  349. Begin
  350. Writeln(stdout,'Runtime error ',Errorcode,' at 0x',hexstr(Longint(Erroraddr),8));
  351. { to get a nice symify }
  352. Writeln(stdout,BackTraceStrFunc(Longint(Erroraddr)));
  353. dump_stack(stdout,ErrorBase);
  354. Writeln(stdout,'');
  355. End;
  356. { call system dependent exit code }
  357. System_exit;
  358. End;
  359. Procedure Halt(ErrNum: Byte);
  360. Begin
  361. ExitCode:=Errnum;
  362. Do_Exit;
  363. end;
  364. function SysBackTraceStr (Addr: longint): ShortString;
  365. begin
  366. SysBackTraceStr:=' 0x'+HexStr(addr,8);
  367. end;
  368. Procedure HandleErrorAddrFrame (Errno : longint;addr,frame : longint);
  369. begin
  370. If pointer(ErrorProc)<>Nil then
  371. ErrorProc(Errno,pointer(addr));
  372. errorcode:=Errno;
  373. exitcode:=Errno;
  374. erroraddr:=pointer(addr);
  375. errorbase:=frame;
  376. halt(errorcode);
  377. end;
  378. Procedure HandleErrorFrame (Errno : longint;frame : longint);
  379. {
  380. Procedure to handle internal errors, i.e. not user-invoked errors
  381. Internal function should ALWAYS call HandleError instead of RunError.
  382. Can be used for exception handlers to specify the frame
  383. }
  384. begin
  385. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  386. end;
  387. Procedure HandleError (Errno : longint);[public,alias : 'FPC_HANDLEERROR'];
  388. {
  389. Procedure to handle internal errors, i.e. not user-invoked errors
  390. Internal function should ALWAYS call HandleError instead of RunError.
  391. }
  392. begin
  393. HandleErrorFrame(Errno,get_frame);
  394. end;
  395. procedure runerror(w : word);[alias: 'FPC_RUNERROR'];
  396. begin
  397. errorcode:=w;
  398. exitcode:=w;
  399. erroraddr:=pointer(get_caller_addr(get_frame));
  400. errorbase:=get_caller_frame(get_frame);
  401. halt(errorcode);
  402. end;
  403. Procedure RunError;
  404. Begin
  405. RunError (0);
  406. End;
  407. Procedure Halt;
  408. Begin
  409. Halt(0);
  410. End;
  411. Procedure dump_stack(var f : text;bp : Longint);
  412. var
  413. i, prevbp : Longint;
  414. Begin
  415. prevbp:=bp-1;
  416. i:=0;
  417. while bp > prevbp Do
  418. Begin
  419. Writeln(f,BackTraceStrFunc(get_caller_addr(bp)));
  420. Inc(i);
  421. If i>max_frame_dump Then
  422. exit;
  423. prevbp:=bp;
  424. bp:=get_caller_frame(bp);
  425. End;
  426. End;
  427. Type
  428. PExitProcInfo = ^TExitProcInfo;
  429. TExitProcInfo = Record
  430. Next : PExitProcInfo;
  431. SaveExit : Pointer;
  432. Proc : TProcedure;
  433. End;
  434. const
  435. ExitProcList: PExitProcInfo = nil;
  436. Procedure DoExitProc;
  437. var
  438. P : PExitProcInfo;
  439. Proc : TProcedure;
  440. Begin
  441. P:=ExitProcList;
  442. ExitProcList:=P^.Next;
  443. ExitProc:=P^.SaveExit;
  444. Proc:=P^.Proc;
  445. DisPose(P);
  446. Proc();
  447. End;
  448. Procedure AddExitProc(Proc: TProcedure);
  449. var
  450. P : PExitProcInfo;
  451. Begin
  452. New(P);
  453. P^.Next:=ExitProcList;
  454. P^.SaveExit:=ExitProc;
  455. P^.Proc:=Proc;
  456. ExitProcList:=P;
  457. ExitProc:=@DoExitProc;
  458. End;
  459. {*****************************************************************************
  460. Abstract/Assert support.
  461. *****************************************************************************}
  462. procedure AbstractError;[public,alias : 'FPC_ABSTRACTERROR'];
  463. begin
  464. If pointer(AbstractErrorProc)<>nil then
  465. AbstractErrorProc();
  466. HandleErrorFrame(211,get_frame);
  467. end;
  468. Procedure int_assert(Const Msg,FName:Shortstring;LineNo,ErrorAddr:Longint); [{$ifdef HASSAVEREGISTERS}SaveRegisters,{$endif}Public,Alias : 'FPC_ASSERT'];
  469. begin
  470. if pointer(AssertErrorProc)<>nil then
  471. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  472. else
  473. HandleErrorFrame(227,get_frame);
  474. end;
  475. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo,ErrorAddr:Longint);
  476. begin
  477. If msg='' then
  478. write(stderr,'Assertion failed')
  479. else
  480. write(stderr,msg);
  481. Writeln(stderr,' (',FName,', line ',LineNo,').');
  482. Writeln(stderr,'');
  483. end;
  484. {*****************************************************************************
  485. SetJmp/LongJmp support.
  486. *****************************************************************************}
  487. {$i setjump.inc}
  488. {*****************************************************************************
  489. Object Pascal support
  490. *****************************************************************************}
  491. {$i objpas.inc}
  492. {
  493. $Log$
  494. Revision 1.86 2000-04-02 09:39:25 florian
  495. * halt in the finalization statement of a unit lead to an endless loop; fixed
  496. Revision 1.85 2000/03/14 07:31:57 pierre
  497. + HandleErrorAddrFrame
  498. Revision 1.84 2000/02/26 15:49:40 jonas
  499. + new str_real which is completely TP compatible regarding output
  500. format and which should have no rounding errors anymore
  501. Revision 1.83 2000/02/09 16:59:31 peter
  502. * truncated log
  503. Revision 1.82 2000/02/09 12:17:51 peter
  504. * moved halt to system.inc
  505. * syslinux doesn't use direct asm anymore
  506. Revision 1.81 2000/02/06 17:19:22 peter
  507. * lineinfo unit added which uses stabs to get lineinfo for backtraces
  508. Revision 1.80 2000/01/10 09:54:30 peter
  509. * primitives added
  510. Revision 1.79 2000/01/07 16:41:36 daniel
  511. * copyright 2000
  512. Revision 1.78 2000/01/07 16:32:25 daniel
  513. * copyright 2000 added
  514. Revision 1.77 1999/12/21 11:10:22 pierre
  515. * allow v09912 to compile system
  516. Revision 1.76 1999/12/18 14:54:34 florian
  517. * very basic widestring support
  518. Revision 1.75 1999/12/12 13:29:34 jonas
  519. * remove "random(longint): longint" if cardinalmulfixed is defined
  520. Revision 1.74 1999/12/01 12:37:13 jonas
  521. + function random(longint): longint
  522. Revision 1.73 1999/11/20 12:48:09 jonas
  523. * reinstated old random generator, but modified it so the integer
  524. one now has a much longer period
  525. Revision 1.72 1999/11/15 21:49:47 peter
  526. * exception address fixes
  527. Revision 1.71 1999/11/09 22:40:12 pierre
  528. + get also first BackTrace address with ' 0x' prefix
  529. Revision 1.70 1999/11/09 20:14:12 daniel
  530. * Committed new random generator.
  531. Revision 1.69 1999/11/06 14:35:39 peter
  532. * truncated log
  533. Revision 1.68 1999/10/26 12:31:00 peter
  534. * *errorproc are not procvars instead of pointers which allows better
  535. error checking for the parameters (shortstring<->ansistring)
  536. Revision 1.67 1999/09/18 16:05:12 jonas
  537. * dump_stack now actually dumps its info to f (was still hardcoded
  538. to stderr)
  539. Revision 1.66 1999/08/05 23:45:14 peter
  540. * saveregister is now working and used for assert and iocheck (which has
  541. been moved to system.inc because it's now system independent)
  542. Revision 1.65 1999/07/28 12:58:22 peter
  543. * fixed assert() to push/pop registers
  544. }