system.inc 15 KB

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