system.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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 system_exit;forward;
  331. Procedure do_exit;[Public,Alias:'FPC_DO_EXIT'];
  332. var
  333. current_exit : Procedure;
  334. Begin
  335. while exitProc<>nil Do
  336. Begin
  337. InOutRes:=0;
  338. current_exit:=tProcedure(exitProc);
  339. exitProc:=nil;
  340. current_exit();
  341. End;
  342. { Finalize units }
  343. FinalizeUnits;
  344. { Show runtime error }
  345. If erroraddr<>nil Then
  346. Begin
  347. Writeln(stdout,'Runtime error ',Errorcode,' at 0x',hexstr(Longint(Erroraddr),8));
  348. { to get a nice symify }
  349. Writeln(stdout,BackTraceStrFunc(Longint(Erroraddr)));
  350. dump_stack(stdout,ErrorBase);
  351. Writeln(stdout,'');
  352. End;
  353. { call system dependent exit code }
  354. System_exit;
  355. End;
  356. Procedure Halt(ErrNum: Byte);
  357. Begin
  358. ExitCode:=Errnum;
  359. Do_Exit;
  360. end;
  361. function SysBackTraceStr (Addr: longint): ShortString;
  362. begin
  363. SysBackTraceStr:=' 0x'+HexStr(addr,8);
  364. end;
  365. Procedure HandleErrorFrame (Errno : longint;frame : longint);
  366. {
  367. Procedure to handle internal errors, i.e. not user-invoked errors
  368. Internal function should ALWAYS call HandleError instead of RunError.
  369. Can be used for exception handlers to specify the frame
  370. }
  371. var
  372. addr : longint;
  373. begin
  374. addr:=get_caller_addr(frame);
  375. If pointer(ErrorProc)<>Nil then
  376. ErrorProc(Errno,pointer(addr));
  377. errorcode:=Errno;
  378. exitcode:=Errno;
  379. erroraddr:=pointer(addr);
  380. errorbase:=get_caller_frame(frame);
  381. halt(errorcode);
  382. end;
  383. Procedure HandleError (Errno : longint);[public,alias : 'FPC_HANDLEERROR'];
  384. {
  385. Procedure to handle internal errors, i.e. not user-invoked errors
  386. Internal function should ALWAYS call HandleError instead of RunError.
  387. }
  388. begin
  389. HandleErrorFrame(Errno,get_frame);
  390. end;
  391. procedure runerror(w : word);[alias: 'FPC_RUNERROR'];
  392. begin
  393. errorcode:=w;
  394. exitcode:=w;
  395. erroraddr:=pointer(get_caller_addr(get_frame));
  396. errorbase:=get_caller_frame(get_frame);
  397. halt(errorcode);
  398. end;
  399. Procedure RunError;
  400. Begin
  401. RunError (0);
  402. End;
  403. Procedure Halt;
  404. Begin
  405. Halt(0);
  406. End;
  407. Procedure dump_stack(var f : text;bp : Longint);
  408. var
  409. i, prevbp : Longint;
  410. Begin
  411. prevbp:=bp-1;
  412. i:=0;
  413. while bp > prevbp Do
  414. Begin
  415. Writeln(f,BackTraceStrFunc(get_caller_addr(bp)));
  416. Inc(i);
  417. If i>max_frame_dump Then
  418. exit;
  419. prevbp:=bp;
  420. bp:=get_caller_frame(bp);
  421. End;
  422. End;
  423. Type
  424. PExitProcInfo = ^TExitProcInfo;
  425. TExitProcInfo = Record
  426. Next : PExitProcInfo;
  427. SaveExit : Pointer;
  428. Proc : TProcedure;
  429. End;
  430. const
  431. ExitProcList: PExitProcInfo = nil;
  432. Procedure DoExitProc;
  433. var
  434. P : PExitProcInfo;
  435. Proc : TProcedure;
  436. Begin
  437. P:=ExitProcList;
  438. ExitProcList:=P^.Next;
  439. ExitProc:=P^.SaveExit;
  440. Proc:=P^.Proc;
  441. DisPose(P);
  442. Proc();
  443. End;
  444. Procedure AddExitProc(Proc: TProcedure);
  445. var
  446. P : PExitProcInfo;
  447. Begin
  448. New(P);
  449. P^.Next:=ExitProcList;
  450. P^.SaveExit:=ExitProc;
  451. P^.Proc:=Proc;
  452. ExitProcList:=P;
  453. ExitProc:=@DoExitProc;
  454. End;
  455. {*****************************************************************************
  456. Abstract/Assert support.
  457. *****************************************************************************}
  458. procedure AbstractError;[public,alias : 'FPC_ABSTRACTERROR'];
  459. begin
  460. If pointer(AbstractErrorProc)<>nil then
  461. AbstractErrorProc();
  462. HandleErrorFrame(211,get_frame);
  463. end;
  464. Procedure int_assert(Const Msg,FName:Shortstring;LineNo,ErrorAddr:Longint); [{$ifdef HASSAVEREGISTERS}SaveRegisters,{$endif}Public,Alias : 'FPC_ASSERT'];
  465. begin
  466. if pointer(AssertErrorProc)<>nil then
  467. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  468. else
  469. HandleErrorFrame(227,get_frame);
  470. end;
  471. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo,ErrorAddr:Longint);
  472. begin
  473. If msg='' then
  474. write(stderr,'Assertion failed')
  475. else
  476. write(stderr,msg);
  477. Writeln(stderr,' (',FName,', line ',LineNo,').');
  478. Writeln(stderr,'');
  479. end;
  480. {*****************************************************************************
  481. SetJmp/LongJmp support.
  482. *****************************************************************************}
  483. {$i setjump.inc}
  484. {*****************************************************************************
  485. Object Pascal support
  486. *****************************************************************************}
  487. {$i objpas.inc}
  488. {
  489. $Log$
  490. Revision 1.83 2000-02-09 16:59:31 peter
  491. * truncated log
  492. Revision 1.82 2000/02/09 12:17:51 peter
  493. * moved halt to system.inc
  494. * syslinux doesn't use direct asm anymore
  495. Revision 1.81 2000/02/06 17:19:22 peter
  496. * lineinfo unit added which uses stabs to get lineinfo for backtraces
  497. Revision 1.80 2000/01/10 09:54:30 peter
  498. * primitives added
  499. Revision 1.79 2000/01/07 16:41:36 daniel
  500. * copyright 2000
  501. Revision 1.78 2000/01/07 16:32:25 daniel
  502. * copyright 2000 added
  503. Revision 1.77 1999/12/21 11:10:22 pierre
  504. * allow v09912 to compile system
  505. Revision 1.76 1999/12/18 14:54:34 florian
  506. * very basic widestring support
  507. Revision 1.75 1999/12/12 13:29:34 jonas
  508. * remove "random(longint): longint" if cardinalmulfixed is defined
  509. Revision 1.74 1999/12/01 12:37:13 jonas
  510. + function random(longint): longint
  511. Revision 1.73 1999/11/20 12:48:09 jonas
  512. * reinstated old random generator, but modified it so the integer
  513. one now has a much longer period
  514. Revision 1.72 1999/11/15 21:49:47 peter
  515. * exception address fixes
  516. Revision 1.71 1999/11/09 22:40:12 pierre
  517. + get also first BackTrace address with ' 0x' prefix
  518. Revision 1.70 1999/11/09 20:14:12 daniel
  519. * Committed new random generator.
  520. Revision 1.69 1999/11/06 14:35:39 peter
  521. * truncated log
  522. Revision 1.68 1999/10/26 12:31:00 peter
  523. * *errorproc are not procvars instead of pointers which allows better
  524. error checking for the parameters (shortstring<->ansistring)
  525. Revision 1.67 1999/09/18 16:05:12 jonas
  526. * dump_stack now actually dumps its info to f (was still hardcoded
  527. to stderr)
  528. Revision 1.66 1999/08/05 23:45:14 peter
  529. * saveregister is now working and used for assert and iocheck (which has
  530. been moved to system.inc because it's now system independent)
  531. Revision 1.65 1999/07/28 12:58:22 peter
  532. * fixed assert() to push/pop registers
  533. }