system.inc 17 KB

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