2
0

system.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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. 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. {****************************************************************************
  35. Routines which have compiler magic
  36. ****************************************************************************}
  37. {$I innr.inc}
  38. Function lo(i : Integer) : byte; [INTERNPROC: In_lo_Word];
  39. Function lo(w : Word) : byte; [INTERNPROC: In_lo_Word];
  40. Function lo(l : Longint) : Word; [INTERNPROC: In_lo_long];
  41. Function hi(i : Integer) : byte; [INTERNPROC: In_hi_Word];
  42. Function hi(w : Word) : byte; [INTERNPROC: In_hi_Word];
  43. Function hi(l : Longint) : Word; [INTERNPROC: In_hi_long];
  44. Function chr(b : byte) : Char; [INTERNPROC: In_chr_byte];
  45. Function Length(s : string) : byte; [INTERNPROC: In_Length_string];
  46. Function Length(c : char) : byte; [INTERNPROC: In_Length_string];
  47. Procedure Reset(var f : TypedFile); [INTERNPROC: In_Reset_TypedFile];
  48. Procedure Rewrite(var f : TypedFile); [INTERNPROC: In_Rewrite_TypedFile];
  49. {****************************************************************************
  50. Include processor specific routines
  51. ****************************************************************************}
  52. {$IFDEF I386}
  53. {$IFDEF M68K}
  54. {$Error Can't determine processor type !}
  55. {$ENDIF}
  56. {$I i386.inc} { Case dependent, don't change }
  57. {$ELSE}
  58. {$IFDEF M68K}
  59. {$I m68k.inc} { Case dependent, don't change }
  60. {$ELSE}
  61. {$Error Can't determine processor type !}
  62. {$ENDIF}
  63. {$ENDIF}
  64. {****************************************************************************
  65. Set Handling
  66. ****************************************************************************}
  67. { Include set support which is processor specific}
  68. {$I set.inc}
  69. {****************************************************************************
  70. Subroutines for String handling
  71. ****************************************************************************}
  72. { Needs to be before RTTI handling }
  73. {$i sstrings.inc}
  74. Type
  75. PLongint = ^Longint;
  76. PByte = ^Byte;
  77. {$i astrings.inc}
  78. {****************************************************************************
  79. Run-Time Type Information (RTTI)
  80. ****************************************************************************}
  81. {$i rtti.inc}
  82. {****************************************************************************
  83. Math Routines
  84. ****************************************************************************}
  85. {$ifndef RTLLITE}
  86. function Hi(b : byte): byte;
  87. begin
  88. Hi := b shr 4
  89. end;
  90. function Lo(b : byte): byte;
  91. begin
  92. Lo := b and $0f
  93. end;
  94. Function swap (X : Word) : Word;[internconst:in_const_swap_word];
  95. Begin
  96. swap:=(X and $ff) shl 8 + (X shr 8)
  97. End;
  98. Function Swap (X : Integer) : Integer;[internconst:in_const_swap_word];
  99. Begin
  100. Swap:=Integer(Swap(Word(X)));
  101. End;
  102. Function swap (X : Longint) : Longint;[internconst:in_const_swap_long];
  103. Begin
  104. Swap:=(X and $ffff) shl 16 + (X shr 16)
  105. End;
  106. Function Swap (X : Cardinal) : Cardinal;[internconst:in_const_swap_long];
  107. Begin
  108. Swap:=Swap(Longint(X));
  109. End;
  110. {$endif RTLLITE}
  111. {****************************************************************************
  112. Random function routines
  113. This implements a very long cycle random number generator by combining
  114. three independant generators. The technique was described in the March
  115. 1987 issue of Byte.
  116. Taken and modified with permission from the PCQ Pascal rtl code.
  117. ****************************************************************************}
  118. {$R-}
  119. {$Q-}
  120. Procedure UseSeed(seed : Longint);Forward;
  121. Function Random : Real;
  122. var
  123. ReturnValue : Real;
  124. begin
  125. if (InitialSeed) OR ((RandSeed <> OldRandSeed) AND (NOT InitialSeed)) then
  126. Begin
  127. { This is a pretty complicated affair }
  128. { Initially we must call UseSeed when RandSeed is initalized }
  129. { We must also call UseSeed each time RandSeed is reinitialized }
  130. { DO NOT CHANGE THE ORDER OF DECLARATIONS IN THIS BLOCK }
  131. { UNLESS YOU WANT RANDON TO CRASH OF COURSE (CEC) }
  132. InitialSeed:=FALSE;
  133. OldRandSeed:=RandSeed;
  134. UseSeed(RandSeed);
  135. end;
  136. Inc(RandSeed);
  137. RandSeed := (RandSeed * 706) mod 500009;
  138. OldRandSeed:=RandSeed;
  139. INC(Seed2);
  140. Seed2 := (Seed2 * 774) MOD 600011;
  141. INC(Seed3);
  142. Seed3 := (Seed3 * 871) MOD 765241;
  143. ReturnValue := RandSeed/500009.0 +
  144. Seed2/600011.0 +
  145. Seed3/765241.0;
  146. Random := frac(ReturnValue);
  147. end;
  148. Function Random(l : Longint) : Longint;
  149. begin
  150. if (InitialSeed) OR ((RandSeed <> OldRandSeed) AND (NOT InitialSeed)) then
  151. Begin
  152. { This is a pretty complicated affair }
  153. { Initially we must call UseSeed when RandSeed is initalized }
  154. { We must also call UseSeed each time RandSeed is reinitialized }
  155. { DO NOT CHANGE THE ORDER OF DECLARATIONS IN THIS BLOCK }
  156. { UNLESS YOU WANT RANDON TO CRASH OF COURSE (CEC) }
  157. InitialSeed:=FALSE;
  158. OldRandSeed:=RandSeed;
  159. UseSeed(Randseed);
  160. end;
  161. Inc(RandSeed);
  162. RandSeed := (RandSeed * 998) mod 1000003;
  163. OldRandSeed:=RandSeed;
  164. Random := RandSeed mod l;
  165. end;
  166. Procedure UseSeed(seed : Longint);
  167. begin
  168. randseed := seed mod 1000003;
  169. Seed2 := (Random(65000) * Random(65000)) mod 600011;
  170. Seed3 := (Random(65000) * Random(65000)) mod 765241;
  171. end;
  172. { Include processor specific routines }
  173. {$I math.inc}
  174. {****************************************************************************
  175. Memory Management
  176. ****************************************************************************}
  177. {$ifndef RTLLITE}
  178. Function Ptr(sel,off : Longint) : pointer;[internconst:in_const_ptr];
  179. Begin
  180. sel:=0;
  181. ptr:=pointer(off);
  182. End;
  183. Function Addr (Var X) : Pointer;
  184. Begin
  185. Addr:=@(X);
  186. End;
  187. Function CSeg : Word;
  188. Begin
  189. Cseg:=0;
  190. End;
  191. Function DSeg : Word;
  192. Begin
  193. Dseg:=0;
  194. End;
  195. Function SSeg : Word;
  196. Begin
  197. Sseg:=0;
  198. End;
  199. {$endif RTLLITE}
  200. {*****************************************************************************
  201. Directory support.
  202. *****************************************************************************}
  203. Procedure getdir(drivenr:byte;Var dir:ansistring);
  204. { this is needed to also allow ansistrings, the shortstring version is
  205. OS dependent }
  206. var
  207. s : shortstring;
  208. begin
  209. getdir(drivenr,s);
  210. dir:=s;
  211. end;
  212. {*****************************************************************************
  213. Miscellaneous
  214. *****************************************************************************}
  215. procedure int_overflow;[public,alias:'FPC_OVERFLOW'];
  216. begin
  217. HandleErrorFrame(215,get_frame);
  218. end;
  219. Function IOResult:Word;
  220. Begin
  221. IOResult:=InOutRes;
  222. InOutRes:=0;
  223. End;
  224. procedure fillchar(var x;count : longint;value : boolean);
  225. begin
  226. fillchar(x,count,byte(value));
  227. end;
  228. procedure fillchar(var x;count : longint;value : char);
  229. begin
  230. fillchar(x,count,byte(value));
  231. end;
  232. {*****************************************************************************
  233. Init / Exit / ExitProc
  234. *****************************************************************************}
  235. Procedure HandleErrorFrame (Errno : longint;frame : longint);
  236. {
  237. Procedure to handle internal errors, i.e. not user-invoked errors
  238. Internal function should ALWAYS call HandleError instead of RunError.
  239. Can be used for exception handlers to specify the frame
  240. }
  241. var
  242. addr : longint;
  243. begin
  244. addr:=get_caller_addr(frame);
  245. If ErrorProc<>Nil then
  246. TErrorProc (ErrorProc)(Errno,pointer(addr));
  247. errorcode:=Errno;
  248. exitcode:=Errno;
  249. erroraddr:=pointer(addr);
  250. errorbase:=get_caller_frame(frame);
  251. halt(errorcode);
  252. end;
  253. Procedure HandleError (Errno : longint);[public,alias : 'FPC_HANDLEERROR'];
  254. {
  255. Procedure to handle internal errors, i.e. not user-invoked errors
  256. Internal function should ALWAYS call HandleError instead of RunError.
  257. }
  258. begin
  259. HandleErrorFrame(Errno,get_frame);
  260. end;
  261. procedure runerror(w : word);[alias: 'FPC_RUNERROR'];
  262. begin
  263. errorcode:=w;
  264. exitcode:=w;
  265. erroraddr:=pointer(get_caller_addr(get_frame));
  266. errorbase:=get_caller_frame(get_frame);
  267. halt(errorcode);
  268. end;
  269. Procedure RunError;
  270. Begin
  271. RunError (0);
  272. End;
  273. Procedure Halt;
  274. Begin
  275. Halt(0);
  276. End;
  277. Procedure dump_stack(var f : text;bp : Longint);
  278. var
  279. i, prevbp : Longint;
  280. Begin
  281. prevbp:=bp-1;
  282. i:=0;
  283. while bp > prevbp Do
  284. Begin
  285. Writeln(stderr,' 0x',HexStr(get_caller_addr(bp),8));
  286. Inc(i);
  287. If i>max_frame_dump Then
  288. exit;
  289. prevbp:=bp;
  290. bp:=get_caller_frame(bp);
  291. End;
  292. End;
  293. Procedure system_exit;forward;
  294. Procedure do_exit;[Public,Alias:'FPC_DO_EXIT'];
  295. var
  296. current_exit : Procedure;
  297. Begin
  298. while exitProc<>nil Do
  299. Begin
  300. InOutRes:=0;
  301. current_exit:=tProcedure(exitProc);
  302. exitProc:=nil;
  303. current_exit();
  304. End;
  305. If erroraddr<>nil Then
  306. Begin
  307. Writeln(stdout,'Run time error ',Errorcode,' at 0x',hexstr(Longint(Erroraddr),8));
  308. dump_stack(stdout,ErrorBase);
  309. End;
  310. { call system dependent exit code }
  311. System_exit;
  312. End;
  313. Type
  314. PExitProcInfo = ^TExitProcInfo;
  315. TExitProcInfo = Record
  316. Next : PExitProcInfo;
  317. SaveExit : Pointer;
  318. Proc : TProcedure;
  319. End;
  320. const
  321. ExitProcList: PExitProcInfo = nil;
  322. Procedure DoExitProc;
  323. var
  324. P : PExitProcInfo;
  325. Proc : TProcedure;
  326. Begin
  327. P:=ExitProcList;
  328. ExitProcList:=P^.Next;
  329. ExitProc:=P^.SaveExit;
  330. Proc:=P^.Proc;
  331. DisPose(P);
  332. Proc();
  333. End;
  334. Procedure AddExitProc(Proc: TProcedure);
  335. var
  336. P : PExitProcInfo;
  337. Begin
  338. New(P);
  339. P^.Next:=ExitProcList;
  340. P^.SaveExit:=ExitProc;
  341. P^.Proc:=Proc;
  342. ExitProcList:=P;
  343. ExitProc:=@DoExitProc;
  344. End;
  345. {*****************************************************************************
  346. Abstract/Assert support.
  347. *****************************************************************************}
  348. procedure AbstractError;[public,alias : 'FPC_ABSTRACTERROR'];
  349. Type
  350. TAbstractErrorProc=Procedure;
  351. begin
  352. If AbstractErrorProc<>nil then
  353. TAbstractErrorProc(AbstractErrorProc);
  354. RunError(211);
  355. end;
  356. Procedure int_assert(Const Msg,FName:string;LineNo,ErrorAddr:Longint); [Public,Alias : 'FPC_ASSERT'];
  357. type
  358. TAssertErrorProc=procedure(const msg,fname:string;lineno,erroraddr:longint);
  359. begin
  360. if AssertErrorProc<>nil then
  361. TAssertErrorProc(AssertErrorProc)(Msg,FName,LineNo,ErrorAddr)
  362. else
  363. HandleError(227);
  364. end;
  365. Procedure SysAssert(Const Msg,FName:string;LineNo,ErrorAddr:Longint);
  366. begin
  367. If msg='' then
  368. write(stderr,'Assertion failed')
  369. else
  370. write(stderr,msg);
  371. writeln(stderr,' (',FName,', line ',LineNo,').');
  372. end;
  373. {*****************************************************************************
  374. SetJmp/LongJmp support.
  375. *****************************************************************************}
  376. {$i setjump.inc}
  377. {
  378. $Log$
  379. Revision 1.53 1999-01-29 09:23:09 pierre
  380. * Fillchar(..,..,boolean) added
  381. Revision 1.52 1999/01/22 12:39:23 pierre
  382. + added text arg for dump_stack
  383. Revision 1.51 1999/01/18 10:05:52 pierre
  384. + system_exit procedure added
  385. Revision 1.50 1998/12/28 15:50:46 peter
  386. + stdout, which is needed when you write something in the system unit
  387. to the screen. Like the runtime error
  388. Revision 1.49 1998/12/21 14:28:21 pierre
  389. * HandleError -> HandleErrorFrame to avoid problem in
  390. assembler code in i386.inc
  391. (call to overloaded function in assembler block !)
  392. Revision 1.48 1998/12/18 17:21:33 peter
  393. * fixed io-error handling
  394. Revision 1.47 1998/12/15 22:43:03 peter
  395. * removed temp symbols
  396. Revision 1.46 1998/12/10 23:59:56 peter
  397. * removed warnign
  398. Revision 1.45 1998/12/01 14:00:10 pierre
  399. + added conversion from exceptions into run time error
  400. (only if syswin32 compiled with -ddebug for now !)
  401. * added HandleError(errno,frame)
  402. where you specify the frame
  403. needed for win32 exception handling
  404. Revision 1.44 1998/11/26 23:16:15 jonas
  405. * changed RandSeed and OldRandSeed to Cardinal to avoid negative random numbers
  406. Revision 1.43 1998/11/17 10:36:07 michael
  407. + renamed astrings.pp to astrings.inc
  408. Revision 1.42 1998/11/16 10:21:25 peter
  409. * fixes for H+
  410. Revision 1.41 1998/11/05 10:29:36 pierre
  411. * fix for length(char) in const expressions
  412. Revision 1.40 1998/11/04 20:34:02 michael
  413. + Removed ifdef useansistrings
  414. Revision 1.39 1998/10/12 22:11:28 jonas
  415. * fixed RandSeed bug
  416. Revision 1.38 1998/10/12 12:43:37 florian
  417. * made FPC_HANDLEERROR public
  418. Revision 1.37 1998/10/07 11:40:08 jonas
  419. * changed seed2 and seed3 to cardinal to prevent overflow
  420. Revision 1.36 1998/10/05 12:32:51 peter
  421. + assert() support
  422. Revision 1.35 1998/10/02 09:25:11 peter
  423. * more constant expression evals
  424. Revision 1.34 1998/09/22 15:30:54 peter
  425. * shortstring=string type added
  426. Revision 1.33 1998/09/16 13:08:03 michael
  427. Added AbstractErrorHandler
  428. Revision 1.32 1998/09/16 12:37:07 michael
  429. Added FPC_ prefix to abstracterror
  430. Revision 1.31 1998/09/15 17:12:32 michael
  431. + Merged changes from fixes branch
  432. Revision 1.30 1998/09/14 10:48:20 peter
  433. * FPC_ names
  434. * Heap manager is now system independent
  435. Revision 1.29.2.1 1998/09/15 17:08:43 michael
  436. + Added abstracterror call
  437. Revision 1.29 1998/09/01 17:36:21 peter
  438. + internconst
  439. Revision 1.28 1998/08/17 12:24:16 carl
  440. + important comment added
  441. Revision 1.27 1998/08/13 16:22:11 jonas
  442. * random now returns a value between 0 and max-1 instead of between 0 and max
  443. Revision 1.26 1998/08/11 00:05:26 peter
  444. * $ifdef ver0_99_5 updates
  445. Revision 1.25 1998/07/30 13:26:18 michael
  446. + Added support for ErrorProc variable. All internal functions are required
  447. to call HandleError instead of runerror from now on.
  448. This is necessary for exception support.
  449. Revision 1.24 1998/07/28 20:37:45 michael
  450. + added setjmp/longjmp and exception support
  451. Revision 1.23 1998/07/23 19:53:20 michael
  452. + Adapted assert to Delphi format
  453. Revision 1.22 1998/07/23 13:08:41 michael
  454. + Implemented DO_ASSERT function.
  455. Revision 1.21 1998/07/15 12:09:35 carl
  456. * would not compile under FPC v0.99.5
  457. Revision 1.20 1998/07/13 21:19:12 florian
  458. * some problems with ansi string support fixed
  459. Revision 1.19 1998/07/08 11:56:55 carl
  460. * randon and Random(l) now work correctly - don't touch it works!
  461. Revision 1.18 1998/07/02 13:01:55 carl
  462. * hmmm... it is luck (BSS zeroed with GAS) that DoError and ErrorBase work.
  463. Now they are initilized instead.
  464. Revision 1.17 1998/07/02 12:53:09 carl
  465. * DOERROR RESOTRED! DON'T TOUCH :)
  466. Revision 1.16 1998/07/02 12:11:50 carl
  467. * no SINGLE in m68k and other processors!
  468. Revision 1.15 1998/07/02 09:25:05 peter
  469. * fixed do_error in runtimeerror
  470. Revision 1.14 1998/07/01 15:29:59 peter
  471. * better readln/writeln
  472. Revision 1.13 1998/06/26 08:21:09 daniel
  473. - Doerror removed.
  474. Revision 1.12 1998/06/25 14:04:25 peter
  475. + internal inc/dec
  476. Revision 1.11 1998/06/25 09:44:20 daniel
  477. + RTLLITE directive to compile minimal RTL.
  478. Revision 1.10 1998/06/15 15:16:26 daniel
  479. * RTLLITE conditional added to produce smaller RTL
  480. Revision 1.9 1998/06/10 07:46:45 michael
  481. + Forgot to commit some changes
  482. Revision 1.8 1998/06/08 12:38:24 michael
  483. Implemented rtti, inserted ansistrings again
  484. Revision 1.7 1998/06/04 23:46:01 peter
  485. * comp,extended are only i386 added support_comp,support_extended
  486. Revision 1.6 1998/05/20 11:23:09 cvs
  487. * test commit. Shouldn't be allowed.
  488. Revision 1.5 1998/05/12 10:42:45 peter
  489. * moved getopts to inc/, all supported OS's need argc,argv exported
  490. + strpas, strlen are now exported in the systemunit
  491. * removed logs
  492. * removed $ifdef ver_above
  493. Revision 1.4 1998/04/16 12:30:47 peter
  494. + inc(pchar), dec(pchar), incc(pchar,a),dec(pchar,a)
  495. Revision 1.3 1998/04/08 07:53:32 michael
  496. + Changed Random() function. Moved from system to processor dependent files (from Pedro Gimeno)
  497. }