system.inc 17 KB

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