system.inc 16 KB

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