system.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. const
  27. { Random / Randomize constants }
  28. OldRandSeed : Cardinal = 0;
  29. InitialSeed : Boolean = TRUE;
  30. Seed2 : Cardinal = 0;
  31. Seed3 : Cardinal = 0;
  32. { For Error Handling.}
  33. ErrorBase : Longint = 0;
  34. { Used by the ansistrings and maybe also other things in the future }
  35. var
  36. emptychar : char;public name 'FPC_EMPTYCHAR';
  37. {****************************************************************************
  38. Routines which have compiler magic
  39. ****************************************************************************}
  40. {$I innr.inc}
  41. Function lo(i : Integer) : byte; [INTERNPROC: In_lo_Word];
  42. Function lo(w : Word) : byte; [INTERNPROC: In_lo_Word];
  43. Function lo(l : Longint) : Word; [INTERNPROC: In_lo_long];
  44. Function lo(l : DWord) : Word; [INTERNPROC: In_lo_long];
  45. Function hi(i : Integer) : byte; [INTERNPROC: In_hi_Word];
  46. Function hi(w : Word) : byte; [INTERNPROC: In_hi_Word];
  47. Function hi(l : Longint) : Word; [INTERNPROC: In_hi_long];
  48. Function hi(l : DWord) : Word; [INTERNPROC: In_hi_long];
  49. Function lo(q : QWord) : DWord; [INTERNPROC: In_lo_qword];
  50. Function lo(i : Int64) : DWord; [INTERNPROC: In_lo_qword];
  51. Function hi(q : QWord) : DWord; [INTERNPROC: In_hi_qword];
  52. Function hi(i : Int64) : DWord; [INTERNPROC: In_hi_qword];
  53. Function chr(b : byte) : Char; [INTERNPROC: In_chr_byte];
  54. Function Length(s : string) : byte; [INTERNPROC: In_Length_string];
  55. Function Length(c : char) : byte; [INTERNPROC: In_Length_string];
  56. Procedure Reset(var f : TypedFile); [INTERNPROC: In_Reset_TypedFile];
  57. Procedure Rewrite(var f : TypedFile); [INTERNPROC: In_Rewrite_TypedFile];
  58. {****************************************************************************
  59. Include processor specific routines
  60. ****************************************************************************}
  61. {$IFDEF I386}
  62. {$IFDEF M68K}
  63. {$Error Can't determine processor type !}
  64. {$ENDIF}
  65. {$I i386.inc} { Case dependent, don't change }
  66. {$ELSE}
  67. {$IFDEF M68K}
  68. {$I m68k.inc} { Case dependent, don't change }
  69. {$ELSE}
  70. {$Error Can't determine processor type !}
  71. {$ENDIF}
  72. {$ENDIF}
  73. { Include generic pascal only routines which are not defined in the processor
  74. specific include file }
  75. {$I generic.inc}
  76. {****************************************************************************
  77. Set Handling
  78. ****************************************************************************}
  79. { Include set support which is processor specific}
  80. {$I set.inc}
  81. {****************************************************************************
  82. Math Routines
  83. ****************************************************************************}
  84. {$ifndef RTLLITE}
  85. function Hi(b : byte): byte;
  86. begin
  87. Hi := b shr 4
  88. end;
  89. function Lo(b : byte): byte;
  90. begin
  91. Lo := b and $0f
  92. end;
  93. Function swap (X : Word) : Word;[internconst:in_const_swap_word];
  94. Begin
  95. swap:=(X and $ff) shl 8 + (X shr 8)
  96. End;
  97. Function Swap (X : Integer) : Integer;[internconst:in_const_swap_word];
  98. Begin
  99. swap:=(X and $ff) shl 8 + (X shr 8)
  100. End;
  101. Function swap (X : Longint) : Longint;[internconst:in_const_swap_long];
  102. Begin
  103. Swap:=(X and $ffff) shl 16 + (X shr 16)
  104. End;
  105. Function Swap (X : Cardinal) : Cardinal;[internconst:in_const_swap_long];
  106. Begin
  107. Swap:=(X and $ffff) shl 16 + (X shr 16)
  108. End;
  109. Function Swap (X : QWord) : QWord;
  110. Begin
  111. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  112. End;
  113. Function swap (X : Int64) : Int64;
  114. Begin
  115. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  116. End;
  117. {$endif RTLLITE}
  118. { Include processor specific routines }
  119. {$I math.inc}
  120. {****************************************************************************
  121. Subroutines for String handling
  122. ****************************************************************************}
  123. { Needs to be before RTTI handling }
  124. {$i sstrings.inc}
  125. { requires sstrings.inc for initval }
  126. {$I int64.inc}
  127. {Requires int64.inc, since that contains the VAL functions for int64 and qword}
  128. {$i astrings.inc}
  129. {$ifdef haswidechar}
  130. {$i wstrings.inc}
  131. {$endif haswidechar}
  132. {****************************************************************************
  133. Run-Time Type Information (RTTI)
  134. ****************************************************************************}
  135. {$i rtti.inc}
  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 NewSeed;Forward;
  146. Function Random : Extended;
  147. begin
  148. if (InitialSeed) OR (RandSeed <> OldRandSeed) then
  149. Begin
  150. { This is a pretty complicated affair }
  151. { Initially we must call NewSeed when RandSeed is initalized }
  152. { We must also call NewSeed each time RandSeed is reinitialized }
  153. { DO NOT CHANGE THE ORDER OF DECLARATIONS IN THIS BLOCK }
  154. { UNLESS YOU WANT RANDON TO CRASH OF COURSE (CEC) }
  155. InitialSeed:=FALSE;
  156. OldRandSeed:=RandSeed;
  157. NewSeed;
  158. end;
  159. Inc(RandSeed);
  160. RandSeed := (RandSeed * 706) mod 500009;
  161. OldRandSeed:=RandSeed;
  162. INC(Seed2);
  163. Seed2 := (Seed2 * 774) MOD 600011;
  164. INC(Seed3);
  165. Seed3 := (Seed3 * 871) MOD 765241;
  166. Random :=
  167. frac(RandSeed/500009.0 +
  168. Seed2/600011.0 +
  169. Seed3/765241.0);
  170. end;
  171. Function internRandom(l : Cardinal) : Cardinal;
  172. begin
  173. if (InitialSeed) OR (RandSeed <> OldRandSeed) then
  174. Begin
  175. { This is a pretty complicated affair }
  176. { Initially we must call NewSeed when RandSeed is initalized }
  177. { We must also call NewSeed each time RandSeed is reinitialized }
  178. { DO NOT CHANGE THE ORDER OF DECLARATIONS IN THIS BLOCK }
  179. { UNLESS YOU WANT RANDOM TO CRASH OF COURSE (CEC) }
  180. InitialSeed:=FALSE;
  181. OldRandSeed:=RandSeed;
  182. NewSeed;
  183. end;
  184. Inc(RandSeed);
  185. RandSeed := (RandSeed * 998) mod 1000003;
  186. OldRandSeed:=RandSeed;
  187. if l<>0 then
  188. begin
  189. internRandom := RandSeed mod l;
  190. end
  191. else internRandom:=0;
  192. end;
  193. function random(l:cardinal): cardinal;
  194. begin
  195. random := trunc(random()*l);
  196. end;
  197. {$ifndef cardinalmulfixed}
  198. function random(l:longint): longint;
  199. begin
  200. random := trunc(random()*l);
  201. end;
  202. {$endif cardinalmulfixed}
  203. Procedure NewSeed;
  204. begin
  205. randseed := randseed mod 1000003;
  206. Seed2 := (internRandom(65000) * internRandom(65000)) mod 600011;
  207. Seed3 := (internRandom(65000) * internRandom(65000)) mod 765241;
  208. end;
  209. {****************************************************************************
  210. Memory Management
  211. ****************************************************************************}
  212. {$ifndef RTLLITE}
  213. Function Ptr(sel,off : Longint) : pointer;[internconst:in_const_ptr];
  214. Begin
  215. sel:=0;
  216. ptr:=pointer(off);
  217. End;
  218. Function CSeg : Word;
  219. Begin
  220. Cseg:=0;
  221. End;
  222. Function DSeg : Word;
  223. Begin
  224. Dseg:=0;
  225. End;
  226. Function SSeg : Word;
  227. Begin
  228. Sseg:=0;
  229. End;
  230. {$endif RTLLITE}
  231. {*****************************************************************************
  232. Directory support.
  233. *****************************************************************************}
  234. Procedure getdir(drivenr:byte;Var dir:ansistring);
  235. { this is needed to also allow ansistrings, the shortstring version is
  236. OS dependent }
  237. var
  238. s : shortstring;
  239. begin
  240. getdir(drivenr,s);
  241. dir:=s;
  242. end;
  243. {$ifopt R+}
  244. {$define RangeCheckWasOn}
  245. {$R-}
  246. {$endif opt R+}
  247. {$ifopt I+}
  248. {$define IOCheckWasOn}
  249. {$I-}
  250. {$endif opt I+}
  251. {$ifopt Q+}
  252. {$define OverflowCheckWasOn}
  253. {$Q-}
  254. {$endif opt Q+}
  255. {*****************************************************************************
  256. Miscellaneous
  257. *****************************************************************************}
  258. procedure int_overflow;[public,alias:'FPC_OVERFLOW'];
  259. begin
  260. HandleErrorFrame(215,get_frame);
  261. end;
  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. Function IOResult:Word;
  274. Begin
  275. IOResult:=InOutRes;
  276. InOutRes:=0;
  277. End;
  278. procedure fillchar(var x;count : longint;value : boolean);
  279. begin
  280. fillchar(x,count,byte(value));
  281. end;
  282. procedure fillchar(var x;count : longint;value : char);
  283. begin
  284. fillchar(x,count,byte(value));
  285. end;
  286. {*****************************************************************************
  287. Initialization / Finalization
  288. *****************************************************************************}
  289. const
  290. maxunits=1024; { See also files.pas of the compiler source }
  291. type
  292. TInitFinalRec=record
  293. InitProc,
  294. FinalProc : TProcedure;
  295. end;
  296. TInitFinalTable=record
  297. TableCount,
  298. InitCount : longint;
  299. Procs : array[1..maxunits] of TInitFinalRec;
  300. end;
  301. var
  302. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  303. procedure InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS'];
  304. var
  305. i : longint;
  306. begin
  307. with InitFinalTable do
  308. begin
  309. for i:=1to TableCount do
  310. begin
  311. if assigned(Procs[i].InitProc) then
  312. Procs[i].InitProc();
  313. InitCount:=i;
  314. end;
  315. end;
  316. end;
  317. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  318. begin
  319. with InitFinalTable do
  320. begin
  321. while (InitCount>0) do
  322. begin
  323. // we've to decrement the cound before calling the final. code
  324. // else a halt in the final. code leads to a endless loop
  325. dec(InitCount);
  326. if assigned(Procs[InitCount+1].FinalProc) then
  327. Procs[InitCount+1].FinalProc();
  328. end;
  329. end;
  330. end;
  331. {*****************************************************************************
  332. Error / Exit / ExitProc
  333. *****************************************************************************}
  334. Procedure system_exit;forward;
  335. Procedure do_exit;[Public,Alias:'FPC_DO_EXIT'];
  336. var
  337. current_exit : Procedure;
  338. Begin
  339. while exitProc<>nil Do
  340. Begin
  341. InOutRes:=0;
  342. current_exit:=tProcedure(exitProc);
  343. exitProc:=nil;
  344. current_exit();
  345. End;
  346. { Finalize units }
  347. FinalizeUnits;
  348. { Show runtime error }
  349. If erroraddr<>nil Then
  350. Begin
  351. Writeln(stdout,'Runtime error ',Errorcode,' at 0x',hexstr(Longint(Erroraddr),8));
  352. { to get a nice symify }
  353. Writeln(stdout,BackTraceStrFunc(Longint(Erroraddr)));
  354. dump_stack(stdout,ErrorBase);
  355. Writeln(stdout,'');
  356. End;
  357. { call system dependent exit code }
  358. System_exit;
  359. End;
  360. Procedure Halt(ErrNum: Byte);
  361. Begin
  362. ExitCode:=Errnum;
  363. Do_Exit;
  364. end;
  365. function SysBackTraceStr (Addr: longint): ShortString;
  366. begin
  367. SysBackTraceStr:=' 0x'+HexStr(addr,8);
  368. end;
  369. Procedure HandleErrorAddrFrame (Errno : longint;addr,frame : longint);
  370. begin
  371. If pointer(ErrorProc)<>Nil then
  372. ErrorProc(Errno,pointer(addr),pointer(frame));
  373. errorcode:=Errno;
  374. exitcode:=Errno;
  375. erroraddr:=pointer(addr);
  376. errorbase:=frame;
  377. halt(errorcode);
  378. end;
  379. Procedure HandleErrorFrame (Errno : longint;frame : longint);
  380. {
  381. Procedure to handle internal errors, i.e. not user-invoked errors
  382. Internal function should ALWAYS call HandleError instead of RunError.
  383. Can be used for exception handlers to specify the frame
  384. }
  385. begin
  386. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  387. end;
  388. Procedure HandleError (Errno : longint);[public,alias : 'FPC_HANDLEERROR'];
  389. {
  390. Procedure to handle internal errors, i.e. not user-invoked errors
  391. Internal function should ALWAYS call HandleError instead of RunError.
  392. }
  393. begin
  394. HandleErrorFrame(Errno,get_frame);
  395. end;
  396. procedure runerror(w : word);[alias: 'FPC_RUNERROR'];
  397. begin
  398. errorcode:=w;
  399. exitcode:=w;
  400. erroraddr:=pointer(get_caller_addr(get_frame));
  401. errorbase:=get_caller_frame(get_frame);
  402. halt(errorcode);
  403. end;
  404. Procedure RunError;
  405. Begin
  406. RunError (0);
  407. End;
  408. Procedure Halt;
  409. Begin
  410. Halt(0);
  411. End;
  412. function do_isdevice(handle:longint):boolean;forward;
  413. Procedure dump_stack(var f : text;bp : Longint);
  414. var
  415. i, prevbp : Longint;
  416. is_dev : boolean;
  417. Begin
  418. prevbp:=bp-1;
  419. i:=0;
  420. is_dev:=do_isdevice(textrec(f).Handle);
  421. while bp > prevbp Do
  422. Begin
  423. Writeln(f,BackTraceStrFunc(get_caller_addr(bp)));
  424. Inc(i);
  425. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  426. exit;
  427. prevbp:=bp;
  428. bp:=get_caller_frame(bp);
  429. End;
  430. End;
  431. Type
  432. PExitProcInfo = ^TExitProcInfo;
  433. TExitProcInfo = Record
  434. Next : PExitProcInfo;
  435. SaveExit : Pointer;
  436. Proc : TProcedure;
  437. End;
  438. const
  439. ExitProcList: PExitProcInfo = nil;
  440. Procedure DoExitProc;
  441. var
  442. P : PExitProcInfo;
  443. Proc : TProcedure;
  444. Begin
  445. P:=ExitProcList;
  446. ExitProcList:=P^.Next;
  447. ExitProc:=P^.SaveExit;
  448. Proc:=P^.Proc;
  449. DisPose(P);
  450. Proc();
  451. End;
  452. Procedure AddExitProc(Proc: TProcedure);
  453. var
  454. P : PExitProcInfo;
  455. Begin
  456. New(P);
  457. P^.Next:=ExitProcList;
  458. P^.SaveExit:=ExitProc;
  459. P^.Proc:=Proc;
  460. ExitProcList:=P;
  461. ExitProc:=@DoExitProc;
  462. End;
  463. {*****************************************************************************
  464. Abstract/Assert support.
  465. *****************************************************************************}
  466. procedure AbstractError;[public,alias : 'FPC_ABSTRACTERROR'];
  467. begin
  468. If pointer(AbstractErrorProc)<>nil then
  469. AbstractErrorProc();
  470. HandleErrorFrame(211,get_frame);
  471. end;
  472. Procedure int_assert(Const Msg,FName:Shortstring;LineNo,ErrorAddr:Longint); [SaveRegisters,Public,Alias : 'FPC_ASSERT'];
  473. begin
  474. if pointer(AssertErrorProc)<>nil then
  475. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  476. else
  477. HandleErrorFrame(227,get_frame);
  478. end;
  479. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo,ErrorAddr:Longint);
  480. begin
  481. If msg='' then
  482. write(stderr,'Assertion failed')
  483. else
  484. write(stderr,msg);
  485. Writeln(stderr,' (',FName,', line ',LineNo,').');
  486. Writeln(stderr,'');
  487. end;
  488. {*****************************************************************************
  489. SetJmp/LongJmp support.
  490. *****************************************************************************}
  491. {$i setjump.inc}
  492. {$ifdef IOCheckWasOn}
  493. {$I+}
  494. {$endif}
  495. {$ifdef RangeCheckWasOn}
  496. {$R+}
  497. {$endif}
  498. {$ifdef OverflowCheckWasOn}
  499. {$Q+}
  500. {$endif}
  501. {*****************************************************************************
  502. Object Pascal support
  503. *****************************************************************************}
  504. {$i objpas.inc}
  505. {
  506. $Log$
  507. Revision 1.5 2000-08-13 17:55:14 michael
  508. + Added some delphi compatibility types
  509. Revision 1.4 2000/08/09 19:31:18 marco
  510. * fixes for val(int64 or qword) to ansistring
  511. Revision 1.3 2000/07/14 10:33:10 michael
  512. + Conditionals fixed
  513. Revision 1.2 2000/07/13 11:33:45 michael
  514. + removed logs
  515. }