system.inc 18 KB

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