system.inc 15 KB

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