system.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. {$IFNDEF ENDIAN_LITTLE}
  63. {$DEFINE ENDIAN_LITTLE}
  64. {$ENDIF}
  65. {$IFDEF M68K}
  66. {$Error Can't determine processor type !}
  67. {$ENDIF}
  68. {$I i386.inc} { Case dependent, don't change }
  69. {$ELSE}
  70. {$IFDEF M68K}
  71. {$IFNDEF ENDIAN_BIG}
  72. {$DEFINE ENDIAN_BIG}
  73. {$ENDIF}
  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. { Include generic pascal routines for sets if the processor }
  88. { specific routines are not available. }
  89. {$I genset.inc}
  90. {****************************************************************************
  91. Math Routines
  92. ****************************************************************************}
  93. function Hi(b : byte): byte;
  94. begin
  95. Hi := b shr 4
  96. end;
  97. function Lo(b : byte): byte;
  98. begin
  99. Lo := b and $0f
  100. end;
  101. Function swap (X : Word) : Word;[internconst:in_const_swap_word];
  102. Begin
  103. swap:=(X and $ff) shl 8 + (X shr 8)
  104. End;
  105. Function Swap (X : Integer) : Integer;[internconst:in_const_swap_word];
  106. Begin
  107. swap:=(X and $ff) shl 8 + (X shr 8)
  108. End;
  109. Function swap (X : Longint) : Longint;[internconst:in_const_swap_long];
  110. Begin
  111. Swap:=(X and $ffff) shl 16 + (X shr 16)
  112. End;
  113. Function Swap (X : Cardinal) : Cardinal;[internconst:in_const_swap_long];
  114. Begin
  115. Swap:=(X and $ffff) shl 16 + (X shr 16)
  116. End;
  117. Function Swap (X : QWord) : QWord;
  118. Begin
  119. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  120. End;
  121. Function swap (X : Int64) : Int64;
  122. Begin
  123. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  124. End;
  125. { Include processor specific routines }
  126. {$I math.inc}
  127. {****************************************************************************
  128. Subroutines for String handling
  129. ****************************************************************************}
  130. { Needs to be before RTTI handling }
  131. {$i sstrings.inc}
  132. { requires sstrings.inc for initval }
  133. {$I int64.inc}
  134. {Requires int64.inc, since that contains the VAL functions for int64 and qword}
  135. {$i astrings.inc}
  136. {$ifdef HASWIDESTRING}
  137. {$i wstrings.inc}
  138. {$endif HASWIDESTRING}
  139. {*****************************************************************************
  140. Dynamic Array support
  141. *****************************************************************************}
  142. {$i dynarr.inc}
  143. {*****************************************************************************
  144. Object Pascal support
  145. *****************************************************************************}
  146. {$i objpas.inc}
  147. {****************************************************************************
  148. Run-Time Type Information (RTTI)
  149. ****************************************************************************}
  150. {$i rtti.inc}
  151. {****************************************************************************
  152. Random function routines
  153. This implements a very long cycle random number generator by combining
  154. three independant generators. The technique was described in the March
  155. 1987 issue of Byte.
  156. Taken and modified with permission from the PCQ Pascal rtl code.
  157. ****************************************************************************}
  158. {$R-}
  159. {$Q-}
  160. Procedure NewSeed;Forward;
  161. Function Random : Extended;
  162. begin
  163. if (InitialSeed) OR (RandSeed <> OldRandSeed) then
  164. Begin
  165. { This is a pretty complicated affair }
  166. { Initially we must call NewSeed when RandSeed is initalized }
  167. { We must also call NewSeed each time RandSeed is reinitialized }
  168. { DO NOT CHANGE THE ORDER OF DECLARATIONS IN THIS BLOCK }
  169. { UNLESS YOU WANT RANDON TO CRASH OF COURSE (CEC) }
  170. InitialSeed:=FALSE;
  171. OldRandSeed:=RandSeed;
  172. NewSeed;
  173. end;
  174. Inc(RandSeed);
  175. RandSeed := (RandSeed * 706) mod 500009;
  176. OldRandSeed:=RandSeed;
  177. INC(Seed2);
  178. Seed2 := (Seed2 * 774) MOD 600011;
  179. INC(Seed3);
  180. Seed3 := (Seed3 * 871) MOD 765241;
  181. Random :=
  182. frac(RandSeed/500009.0 +
  183. Seed2/600011.0 +
  184. Seed3/765241.0);
  185. end;
  186. Function internRandom(l : Cardinal) : Cardinal;
  187. begin
  188. if (InitialSeed) OR (RandSeed <> OldRandSeed) then
  189. Begin
  190. { This is a pretty complicated affair }
  191. { Initially we must call NewSeed when RandSeed is initalized }
  192. { We must also call NewSeed each time RandSeed is reinitialized }
  193. { DO NOT CHANGE THE ORDER OF DECLARATIONS IN THIS BLOCK }
  194. { UNLESS YOU WANT RANDOM TO CRASH OF COURSE (CEC) }
  195. InitialSeed:=FALSE;
  196. OldRandSeed:=RandSeed;
  197. NewSeed;
  198. end;
  199. Inc(RandSeed);
  200. RandSeed := (RandSeed * 998) mod 1000003;
  201. OldRandSeed:=RandSeed;
  202. if l<>0 then
  203. begin
  204. internRandom := RandSeed mod l;
  205. end
  206. else internRandom:=0;
  207. end;
  208. function random(l:cardinal): cardinal;
  209. begin
  210. random := trunc(random()*l);
  211. end;
  212. function random(l:longint): longint;
  213. begin
  214. random := trunc(random()*l);
  215. end;
  216. Procedure NewSeed;
  217. begin
  218. randseed := randseed mod 1000003;
  219. Seed2 := (internRandom(65000) * internRandom(65000)) mod 600011;
  220. Seed3 := (internRandom(65000) * internRandom(65000)) mod 765241;
  221. end;
  222. {****************************************************************************
  223. Memory Management
  224. ****************************************************************************}
  225. Function Ptr(sel,off : Longint) : farpointer;[internconst:in_const_ptr];
  226. Begin
  227. ptr:=farpointer((sel shl 4)+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. {*****************************************************************************
  242. Directory support.
  243. *****************************************************************************}
  244. Procedure getdir(drivenr:byte;Var dir:ansistring);
  245. { this is needed to also allow ansistrings, the shortstring version is
  246. OS dependent }
  247. var
  248. s : shortstring;
  249. begin
  250. getdir(drivenr,s);
  251. dir:=s;
  252. end;
  253. {$ifopt R+}
  254. {$define RangeCheckWasOn}
  255. {$R-}
  256. {$endif opt R+}
  257. {$ifopt I+}
  258. {$define IOCheckWasOn}
  259. {$I-}
  260. {$endif opt I+}
  261. {$ifopt Q+}
  262. {$define OverflowCheckWasOn}
  263. {$Q-}
  264. {$endif opt Q+}
  265. {*****************************************************************************
  266. Miscellaneous
  267. *****************************************************************************}
  268. procedure int_rangeerror;[public,alias:'FPC_RANGEERROR'];
  269. begin
  270. HandleErrorFrame(201,get_frame);
  271. end;
  272. procedure int_overflow;[public,alias:'FPC_OVERFLOW'];
  273. begin
  274. HandleErrorFrame(215,get_frame);
  275. end;
  276. procedure int_iocheck(addr : longint);[saveregisters,public,alias:'FPC_IOCHECK'];
  277. var
  278. l : longint;
  279. begin
  280. if InOutRes<>0 then
  281. begin
  282. l:=InOutRes;
  283. InOutRes:=0;
  284. HandleErrorFrame(l,get_frame);
  285. end;
  286. end;
  287. Function IOResult:Word;
  288. Begin
  289. IOResult:=InOutRes;
  290. InOutRes:=0;
  291. End;
  292. procedure fillchar(var x;count : longint;value : boolean);
  293. begin
  294. fillchar(x,count,byte(value));
  295. end;
  296. procedure fillchar(var x;count : longint;value : char);
  297. begin
  298. fillchar(x,count,byte(value));
  299. end;
  300. {*****************************************************************************
  301. Initialization / Finalization
  302. *****************************************************************************}
  303. const
  304. maxunits=1024; { See also files.pas of the compiler source }
  305. type
  306. TInitFinalRec=record
  307. InitProc,
  308. FinalProc : TProcedure;
  309. end;
  310. TInitFinalTable=record
  311. TableCount,
  312. InitCount : longint;
  313. Procs : array[1..maxunits] of TInitFinalRec;
  314. end;
  315. var
  316. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  317. procedure InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS'];
  318. var
  319. i : longint;
  320. begin
  321. with InitFinalTable do
  322. begin
  323. for i:=1to TableCount do
  324. begin
  325. if assigned(Procs[i].InitProc) then
  326. Procs[i].InitProc();
  327. InitCount:=i;
  328. end;
  329. end;
  330. end;
  331. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  332. begin
  333. with InitFinalTable do
  334. begin
  335. while (InitCount>0) do
  336. begin
  337. // we've to decrement the cound before calling the final. code
  338. // else a halt in the final. code leads to a endless loop
  339. dec(InitCount);
  340. if assigned(Procs[InitCount+1].FinalProc) then
  341. Procs[InitCount+1].FinalProc();
  342. end;
  343. end;
  344. end;
  345. {*****************************************************************************
  346. Error / Exit / ExitProc
  347. *****************************************************************************}
  348. Procedure system_exit;forward;
  349. Procedure InternalExit;
  350. var
  351. current_exit : Procedure;
  352. Begin
  353. while exitProc<>nil Do
  354. Begin
  355. InOutRes:=0;
  356. current_exit:=tProcedure(exitProc);
  357. exitProc:=nil;
  358. current_exit();
  359. End;
  360. { Finalize units }
  361. FinalizeUnits;
  362. { Show runtime error and exit }
  363. If erroraddr<>nil Then
  364. Begin
  365. Writeln(stdout,'Runtime error ',Errorcode,' at 0x',hexstr(Longint(Erroraddr),8));
  366. { to get a nice symify }
  367. Writeln(stdout,BackTraceStrFunc(Longint(Erroraddr)));
  368. dump_stack(stdout,ErrorBase);
  369. Writeln(stdout,'');
  370. End;
  371. End;
  372. Procedure do_exit;[Public,Alias:'FPC_DO_EXIT'];
  373. begin
  374. InternalExit;
  375. System_exit;
  376. end;
  377. Procedure lib_exit;saveregisters;[Public,Alias:'FPC_LIB_EXIT'];
  378. begin
  379. InternalExit;
  380. end;
  381. Procedure Halt(ErrNum: Byte);
  382. Begin
  383. ExitCode:=Errnum;
  384. Do_Exit;
  385. end;
  386. function SysBackTraceStr (Addr: longint): ShortString;
  387. begin
  388. SysBackTraceStr:=' 0x'+HexStr(addr,8);
  389. end;
  390. Procedure HandleErrorAddrFrame (Errno : longint;addr,frame : longint);[public,alias:'FPC_BREAK_ERROR'];
  391. begin
  392. If pointer(ErrorProc)<>Nil then
  393. ErrorProc(Errno,pointer(addr),pointer(frame));
  394. errorcode:=Errno;
  395. exitcode:=Errno;
  396. erroraddr:=pointer(addr);
  397. errorbase:=frame;
  398. halt(errorcode);
  399. end;
  400. Procedure HandleErrorFrame (Errno : longint;frame : longint);
  401. {
  402. Procedure to handle internal errors, i.e. not user-invoked errors
  403. Internal function should ALWAYS call HandleError instead of RunError.
  404. Can be used for exception handlers to specify the frame
  405. }
  406. begin
  407. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  408. end;
  409. Procedure HandleError (Errno : longint);[public,alias : 'FPC_HANDLEERROR'];
  410. {
  411. Procedure to handle internal errors, i.e. not user-invoked errors
  412. Internal function should ALWAYS call HandleError instead of RunError.
  413. }
  414. begin
  415. HandleErrorFrame(Errno,get_frame);
  416. end;
  417. procedure runerror(w : word);[alias: 'FPC_RUNERROR'];
  418. begin
  419. errorcode:=w;
  420. exitcode:=w;
  421. erroraddr:=pointer(get_caller_addr(get_frame));
  422. errorbase:=get_caller_frame(get_frame);
  423. halt(errorcode);
  424. end;
  425. Procedure RunError;
  426. Begin
  427. RunError (0);
  428. End;
  429. Procedure Halt;
  430. Begin
  431. Halt(0);
  432. End;
  433. function do_isdevice(handle:longint):boolean;forward;
  434. Procedure dump_stack(var f : text;bp : Longint);
  435. var
  436. i, prevbp : Longint;
  437. is_dev : boolean;
  438. Begin
  439. prevbp:=bp-1;
  440. i:=0;
  441. is_dev:=do_isdevice(textrec(f).Handle);
  442. while bp > prevbp Do
  443. Begin
  444. Writeln(f,BackTraceStrFunc(get_caller_addr(bp)));
  445. Inc(i);
  446. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  447. exit;
  448. prevbp:=bp;
  449. bp:=get_caller_frame(bp);
  450. End;
  451. End;
  452. Type
  453. PExitProcInfo = ^TExitProcInfo;
  454. TExitProcInfo = Record
  455. Next : PExitProcInfo;
  456. SaveExit : Pointer;
  457. Proc : TProcedure;
  458. End;
  459. const
  460. ExitProcList: PExitProcInfo = nil;
  461. Procedure DoExitProc;
  462. var
  463. P : PExitProcInfo;
  464. Proc : TProcedure;
  465. Begin
  466. P:=ExitProcList;
  467. ExitProcList:=P^.Next;
  468. ExitProc:=P^.SaveExit;
  469. Proc:=P^.Proc;
  470. DisPose(P);
  471. Proc();
  472. End;
  473. Procedure AddExitProc(Proc: TProcedure);
  474. var
  475. P : PExitProcInfo;
  476. Begin
  477. New(P);
  478. P^.Next:=ExitProcList;
  479. P^.SaveExit:=ExitProc;
  480. P^.Proc:=Proc;
  481. ExitProcList:=P;
  482. ExitProc:=@DoExitProc;
  483. End;
  484. {*****************************************************************************
  485. Abstract/Assert support.
  486. *****************************************************************************}
  487. procedure AbstractError;[public,alias : 'FPC_ABSTRACTERROR'];
  488. begin
  489. If pointer(AbstractErrorProc)<>nil then
  490. AbstractErrorProc();
  491. HandleErrorFrame(211,get_frame);
  492. end;
  493. Procedure int_assert(Const Msg,FName:Shortstring;LineNo,ErrorAddr:Longint); [SaveRegisters,Public,Alias : 'FPC_ASSERT'];
  494. begin
  495. if pointer(AssertErrorProc)<>nil then
  496. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  497. else
  498. HandleErrorFrame(227,get_frame);
  499. end;
  500. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo,ErrorAddr:Longint);
  501. begin
  502. If msg='' then
  503. write(stderr,'Assertion failed')
  504. else
  505. write(stderr,msg);
  506. Writeln(stderr,' (',FName,', line ',LineNo,').');
  507. Writeln(stderr,'');
  508. end;
  509. {*****************************************************************************
  510. SetJmp/LongJmp support.
  511. *****************************************************************************}
  512. {$i setjump.inc}
  513. {$ifdef IOCheckWasOn}
  514. {$I+}
  515. {$endif}
  516. {$ifdef RangeCheckWasOn}
  517. {$R+}
  518. {$endif}
  519. {$ifdef OverflowCheckWasOn}
  520. {$Q+}
  521. {$endif}
  522. {
  523. $Log$
  524. Revision 1.16 2001-07-08 21:00:18 peter
  525. * various widestring updates, it works now mostly without charset
  526. mapping supported
  527. Revision 1.15 2001/06/13 18:32:05 peter
  528. * big endian updates (merged)
  529. Revision 1.14 2001/06/03 15:15:58 peter
  530. * lib_exit added
  531. Revision 1.13 2001/05/09 19:57:07 peter
  532. *** empty log message ***
  533. Revision 1.12 2001/04/13 18:06:28 peter
  534. * removed rtllite define
  535. Revision 1.11 2000/12/16 15:56:19 jonas
  536. - removed all ifdef cardinalmulfix code
  537. Revision 1.10 2000/11/13 14:47:46 jonas
  538. * support for range checking when converting from 64bit to something
  539. smaller (32bit, 16bit, 8bit)
  540. * fixed range checking between longint/cardinal and for array indexing
  541. with cardinal (values > $7fffffff were considered negative)
  542. Revision 1.9 2000/11/11 16:12:01 peter
  543. * ptr returns farpointer
  544. Revision 1.8 2000/11/06 21:35:59 peter
  545. * removed some warnings
  546. Revision 1.7 2000/11/04 17:52:46 florian
  547. * fixed linker errors
  548. Revision 1.6 2000/10/13 12:04:03 peter
  549. * FPC_BREAK_ERROR added
  550. Revision 1.5 2000/08/13 17:55:14 michael
  551. + Added some delphi compatibility types
  552. Revision 1.4 2000/08/09 19:31:18 marco
  553. * fixes for val(int64 or qword) to ansistring
  554. Revision 1.3 2000/07/14 10:33:10 michael
  555. + Conditionals fixed
  556. Revision 1.2 2000/07/13 11:33:45 michael
  557. + removed logs
  558. }