system.inc 15 KB

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