system.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 UseSeed(seed : Longint);Forward;
  141. Function Random : Real;
  142. var
  143. ReturnValue : Real;
  144. begin
  145. if (InitialSeed) OR ((RandSeed <> OldRandSeed) AND (NOT InitialSeed)) then
  146. Begin
  147. { This is a pretty complicated affair }
  148. { Initially we must call UseSeed when RandSeed is initalized }
  149. { We must also call UseSeed each time RandSeed is reinitialized }
  150. { DO NOT CHANGE THE ORDER OF DECLARATIONS IN THIS BLOCK }
  151. { UNLESS YOU WANT RANDON TO CRASH OF COURSE (CEC) }
  152. InitialSeed:=FALSE;
  153. OldRandSeed:=RandSeed;
  154. UseSeed(RandSeed);
  155. end;
  156. Inc(RandSeed);
  157. RandSeed := (RandSeed * 706) mod 500009;
  158. OldRandSeed:=RandSeed;
  159. INC(Seed2);
  160. Seed2 := (Seed2 * 774) MOD 600011;
  161. INC(Seed3);
  162. Seed3 := (Seed3 * 871) MOD 765241;
  163. ReturnValue := RandSeed/500009.0 +
  164. Seed2/600011.0 +
  165. Seed3/765241.0;
  166. Random := frac(ReturnValue);
  167. end;
  168. Function Random(l : Longint) : Longint;
  169. begin
  170. if (InitialSeed) OR ((RandSeed <> OldRandSeed) AND (NOT InitialSeed)) then
  171. Begin
  172. { This is a pretty complicated affair }
  173. { Initially we must call UseSeed when RandSeed is initalized }
  174. { We must also call UseSeed each time RandSeed is reinitialized }
  175. { DO NOT CHANGE THE ORDER OF DECLARATIONS IN THIS BLOCK }
  176. { UNLESS YOU WANT RANDON TO CRASH OF COURSE (CEC) }
  177. InitialSeed:=FALSE;
  178. OldRandSeed:=RandSeed;
  179. UseSeed(Randseed);
  180. end;
  181. Inc(RandSeed);
  182. RandSeed := (RandSeed * 998) mod 1000003;
  183. OldRandSeed:=RandSeed;
  184. if l=0 then
  185. Random:=0
  186. else
  187. Random := RandSeed mod l;
  188. end;
  189. Procedure UseSeed(seed : Longint);
  190. begin
  191. randseed := seed mod 1000003;
  192. Seed2 := (Random(65000) * Random(65000)) mod 600011;
  193. Seed3 := (Random(65000) * Random(65000)) mod 765241;
  194. end;
  195. { Include processor specific routines }
  196. {$I math.inc}
  197. {$ifdef INT64}
  198. {$I int64.inc}
  199. {$endif INT64}
  200. {****************************************************************************
  201. Memory Management
  202. ****************************************************************************}
  203. {$ifndef RTLLITE}
  204. Function Ptr(sel,off : Longint) : pointer;[internconst:in_const_ptr];
  205. Begin
  206. sel:=0;
  207. ptr:=pointer(off);
  208. End;
  209. Function CSeg : Word;
  210. Begin
  211. Cseg:=0;
  212. End;
  213. Function DSeg : Word;
  214. Begin
  215. Dseg:=0;
  216. End;
  217. Function SSeg : Word;
  218. Begin
  219. Sseg:=0;
  220. End;
  221. {$endif RTLLITE}
  222. {*****************************************************************************
  223. Directory support.
  224. *****************************************************************************}
  225. Procedure getdir(drivenr:byte;Var dir:ansistring);
  226. { this is needed to also allow ansistrings, the shortstring version is
  227. OS dependent }
  228. var
  229. s : shortstring;
  230. begin
  231. getdir(drivenr,s);
  232. dir:=s;
  233. end;
  234. {*****************************************************************************
  235. Miscellaneous
  236. *****************************************************************************}
  237. procedure int_overflow;[public,alias:'FPC_OVERFLOW'];
  238. begin
  239. HandleErrorFrame(215,get_frame);
  240. end;
  241. {$ifdef HASSAVEREGISTERS}
  242. procedure int_iocheck(addr : longint);[saveregisters,public,alias:'FPC_IOCHECK'];
  243. var
  244. l : longint;
  245. begin
  246. if InOutRes<>0 then
  247. begin
  248. l:=InOutRes;
  249. InOutRes:=0;
  250. HandleErrorFrame(l,get_frame);
  251. end;
  252. end;
  253. {$endif}
  254. Function IOResult:Word;
  255. Begin
  256. IOResult:=InOutRes;
  257. InOutRes:=0;
  258. End;
  259. procedure fillchar(var x;count : longint;value : boolean);
  260. begin
  261. fillchar(x,count,byte(value));
  262. end;
  263. procedure fillchar(var x;count : longint;value : char);
  264. begin
  265. fillchar(x,count,byte(value));
  266. end;
  267. {*****************************************************************************
  268. Initialization / Finalization
  269. *****************************************************************************}
  270. const
  271. maxunits=1024; { See also files.pas of the compiler source }
  272. type
  273. TInitFinalRec=record
  274. InitProc,
  275. FinalProc : TProcedure;
  276. end;
  277. TInitFinalTable=record
  278. TableCount,
  279. InitCount : longint;
  280. Procs : array[1..maxunits] of TInitFinalRec;
  281. end;
  282. var
  283. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  284. procedure InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS'];
  285. var
  286. i : longint;
  287. begin
  288. with InitFinalTable do
  289. begin
  290. for i:=1to TableCount do
  291. begin
  292. if assigned(Procs[i].InitProc) then
  293. Procs[i].InitProc();
  294. InitCount:=i;
  295. end;
  296. end;
  297. end;
  298. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  299. begin
  300. with InitFinalTable do
  301. begin
  302. while (InitCount>0) do
  303. begin
  304. if assigned(Procs[InitCount].FinalProc) then
  305. Procs[InitCount].FinalProc();
  306. dec(InitCount);
  307. end;
  308. end;
  309. end;
  310. {*****************************************************************************
  311. Error / Exit / ExitProc
  312. *****************************************************************************}
  313. Procedure HandleErrorFrame (Errno : longint;frame : longint);
  314. {
  315. Procedure to handle internal errors, i.e. not user-invoked errors
  316. Internal function should ALWAYS call HandleError instead of RunError.
  317. Can be used for exception handlers to specify the frame
  318. }
  319. var
  320. addr : longint;
  321. begin
  322. addr:=get_caller_addr(frame);
  323. If ErrorProc<>Nil then
  324. ErrorProc(Errno,pointer(addr));
  325. errorcode:=Errno;
  326. exitcode:=Errno;
  327. erroraddr:=pointer(addr);
  328. errorbase:=get_caller_frame(frame);
  329. halt(errorcode);
  330. end;
  331. Procedure HandleError (Errno : longint);[public,alias : 'FPC_HANDLEERROR'];
  332. {
  333. Procedure to handle internal errors, i.e. not user-invoked errors
  334. Internal function should ALWAYS call HandleError instead of RunError.
  335. }
  336. begin
  337. HandleErrorFrame(Errno,get_frame);
  338. end;
  339. procedure runerror(w : word);[alias: 'FPC_RUNERROR'];
  340. begin
  341. errorcode:=w;
  342. exitcode:=w;
  343. erroraddr:=pointer(get_caller_addr(get_frame));
  344. errorbase:=get_caller_frame(get_frame);
  345. halt(errorcode);
  346. end;
  347. Procedure RunError;
  348. Begin
  349. RunError (0);
  350. End;
  351. Procedure Halt;
  352. Begin
  353. Halt(0);
  354. End;
  355. Procedure dump_stack(var f : text;bp : Longint);
  356. var
  357. i, prevbp : Longint;
  358. Begin
  359. prevbp:=bp-1;
  360. i:=0;
  361. while bp > prevbp Do
  362. Begin
  363. Writeln(f,' 0x',HexStr(get_caller_addr(bp),8));
  364. Inc(i);
  365. If i>max_frame_dump Then
  366. exit;
  367. prevbp:=bp;
  368. bp:=get_caller_frame(bp);
  369. End;
  370. End;
  371. Procedure system_exit;forward;
  372. Procedure do_exit;[Public,Alias:'FPC_DO_EXIT'];
  373. var
  374. current_exit : Procedure;
  375. Begin
  376. while exitProc<>nil Do
  377. Begin
  378. InOutRes:=0;
  379. current_exit:=tProcedure(exitProc);
  380. exitProc:=nil;
  381. current_exit();
  382. End;
  383. { Finalize units }
  384. FinalizeUnits;
  385. { Show runtime error }
  386. If erroraddr<>nil Then
  387. Begin
  388. Writeln(stdout,'Runtime error ',Errorcode,' at 0x',hexstr(Longint(Erroraddr),8));
  389. dump_stack(stdout,ErrorBase);
  390. Writeln(stdout,'');
  391. End;
  392. { call system dependent exit code }
  393. System_exit;
  394. End;
  395. Type
  396. PExitProcInfo = ^TExitProcInfo;
  397. TExitProcInfo = Record
  398. Next : PExitProcInfo;
  399. SaveExit : Pointer;
  400. Proc : TProcedure;
  401. End;
  402. const
  403. ExitProcList: PExitProcInfo = nil;
  404. Procedure DoExitProc;
  405. var
  406. P : PExitProcInfo;
  407. Proc : TProcedure;
  408. Begin
  409. P:=ExitProcList;
  410. ExitProcList:=P^.Next;
  411. ExitProc:=P^.SaveExit;
  412. Proc:=P^.Proc;
  413. DisPose(P);
  414. Proc();
  415. End;
  416. Procedure AddExitProc(Proc: TProcedure);
  417. var
  418. P : PExitProcInfo;
  419. Begin
  420. New(P);
  421. P^.Next:=ExitProcList;
  422. P^.SaveExit:=ExitProc;
  423. P^.Proc:=Proc;
  424. ExitProcList:=P;
  425. ExitProc:=@DoExitProc;
  426. End;
  427. {*****************************************************************************
  428. Abstract/Assert support.
  429. *****************************************************************************}
  430. procedure AbstractError;[public,alias : 'FPC_ABSTRACTERROR'];
  431. begin
  432. If AbstractErrorProc<>nil then
  433. AbstractErrorProc();
  434. HandleError(211);
  435. end;
  436. Procedure int_assert(Const Msg,FName:Shortstring;LineNo,ErrorAddr:Longint); [{$ifdef HASSAVEREGISTERS}SaveRegisters,{$endif}Public,Alias : 'FPC_ASSERT'];
  437. begin
  438. if AssertErrorProc<>nil then
  439. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  440. else
  441. HandleError(227);
  442. end;
  443. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo,ErrorAddr:Longint);
  444. begin
  445. If msg='' then
  446. write(stderr,'Assertion failed')
  447. else
  448. write(stderr,msg);
  449. Writeln(stderr,' (',FName,', line ',LineNo,').');
  450. Writeln(stderr,'');
  451. end;
  452. {*****************************************************************************
  453. SetJmp/LongJmp support.
  454. *****************************************************************************}
  455. {$i setjump.inc}
  456. {*****************************************************************************
  457. Object Pascal support
  458. *****************************************************************************}
  459. {$i objpas.inc}
  460. {
  461. $Log$
  462. Revision 1.69 1999-11-06 14:35:39 peter
  463. * truncated log
  464. Revision 1.68 1999/10/26 12:31:00 peter
  465. * *errorproc are not procvars instead of pointers which allows better
  466. error checking for the parameters (shortstring<->ansistring)
  467. Revision 1.67 1999/09/18 16:05:12 jonas
  468. * dump_stack now actually dumps its info to f (was still hardcoded
  469. to stderr)
  470. Revision 1.66 1999/08/05 23:45:14 peter
  471. * saveregister is now working and used for assert and iocheck (which has
  472. been moved to system.inc because it's now system independent)
  473. Revision 1.65 1999/07/28 12:58:22 peter
  474. * fixed assert() to push/pop registers
  475. Revision 1.64 1999/07/05 20:04:27 peter
  476. * removed temp defines
  477. Revision 1.63 1999/07/03 01:24:19 peter
  478. * $ifdef int64
  479. Revision 1.62 1999/07/02 18:06:42 florian
  480. + qword/int64: lo/hi/swap
  481. Revision 1.61 1999/07/01 15:39:51 florian
  482. + qword/int64 type released
  483. Revision 1.60 1999/06/11 11:47:00 peter
  484. * random doesn't rte 200 with random(0)
  485. Revision 1.59 1999/06/05 20:45:12 michael
  486. + AbstractErro should call HandleError, not runerror.
  487. Revision 1.58 1999/05/17 21:52:39 florian
  488. * most of the Object Pascal stuff moved to the system unit
  489. Revision 1.57 1999/04/17 13:10:25 peter
  490. * addr() internal
  491. Revision 1.56 1999/04/15 12:20:01 peter
  492. + finalization support
  493. Revision 1.55 1999/03/01 15:41:03 peter
  494. * use external names
  495. * removed all direct assembler modes
  496. Revision 1.54 1999/02/01 00:05:14 florian
  497. + functions lo/hi for DWord type implemented
  498. Revision 1.53 1999/01/29 09:23:09 pierre
  499. * Fillchar(..,..,boolean) added
  500. Revision 1.52 1999/01/22 12:39:23 pierre
  501. + added text arg for dump_stack
  502. Revision 1.51 1999/01/18 10:05:52 pierre
  503. + system_exit procedure added
  504. Revision 1.50 1998/12/28 15:50:46 peter
  505. + stdout, which is needed when you write something in the system unit
  506. to the screen. Like the runtime error
  507. Revision 1.49 1998/12/21 14:28:21 pierre
  508. * HandleError -> HandleErrorFrame to avoid problem in
  509. assembler code in i386.inc
  510. (call to overloaded function in assembler block !)
  511. }