system.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. { PLEASE DO NOT OPTIMIZE BECAUSE THEY ACTUALLY WORK CORRECTLY - unless }
  217. { you want me to go violent :) (CEC) }
  218. Procedure UseSeed(seed : Longint);Forward;
  219. Function Random : Real;
  220. var
  221. ReturnValue : Real;
  222. begin
  223. if (InitialSeed) OR ((RandSeed <> OldRandSeed) AND NOT InitialSeed) then
  224. Begin
  225. OldRandSeed:=RandSeed;
  226. { This is a pretty complicated affair }
  227. { Initially we must call UseSeed when RandSeed is initalized }
  228. { We must also call UseSeed each time RandSeed is reinitialized }
  229. { DO NOT CHANGE THE ORDER OF DECLARATIONS IN THIS BLOCK }
  230. { UNLESS YOU WANT RANDON TO CRASH OF COURSE (CEC) }
  231. InitialSeed:=FALSE;
  232. UseSeed(Randseed);
  233. end;
  234. Inc(Seed1);
  235. Seed1 := (Seed1 * 706) mod 500009;
  236. INC(Seed2);
  237. Seed2 := (Seed2 * 774) MOD 600011;
  238. INC(Seed3);
  239. Seed3 := (Seed3 * 871) MOD 765241;
  240. ReturnValue := Seed1/500009.0 +
  241. Seed2/600011.0 +
  242. Seed3/765241.0;
  243. Random := frac(ReturnValue);
  244. end;
  245. Function Random(l : Longint) : Longint;
  246. begin
  247. if (InitialSeed) OR ((RandSeed <> OldRandSeed) AND NOT InitialSeed) then
  248. Begin
  249. OldRandSeed:=RandSeed;
  250. { This is a pretty complicated affair }
  251. { Initially we must call UseSeed when RandSeed is initalized }
  252. { We must also call UseSeed each time RandSeed is reinitialized }
  253. { DO NOT CHANGE THE ORDER OF DECLARATIONS IN THIS BLOCK }
  254. { UNLESS YOU WANT RANDON TO CRASH OF COURSE (CEC) }
  255. InitialSeed:=FALSE;
  256. UseSeed(Randseed);
  257. end;
  258. Inc(Seed1);
  259. Seed1 := (Seed1 * 998) mod 1000003;
  260. Random := Seed1 mod Succ(l);
  261. end;
  262. Procedure UseSeed(seed : Longint);
  263. begin
  264. Seed1 := seed mod 1000003;
  265. Seed2 := (Random(65000) * Random(65000)) mod 600011;
  266. Seed3 := (Random(65000) * Random(65000)) mod 765241;
  267. end;
  268. { Include processor specific routines }
  269. {$I math.inc}
  270. {****************************************************************************
  271. Memory Management
  272. ****************************************************************************}
  273. {$ifndef RTLLITE}
  274. Function Ptr(sel,off : Longint) : pointer;
  275. Begin
  276. sel:=0;
  277. ptr:=pointer(off);
  278. End;
  279. Function Addr (Var X) : Pointer;
  280. Begin
  281. Addr:=@(X);
  282. End;
  283. Function CSeg : Word;
  284. Begin
  285. Cseg:=0;
  286. End;
  287. Function DSeg : Word;
  288. Begin
  289. Dseg:=0;
  290. End;
  291. Function SSeg : Word;
  292. Begin
  293. Sseg:=0;
  294. End;
  295. {$endif RTLLITE}
  296. {*****************************************************************************
  297. Miscellaneous
  298. *****************************************************************************}
  299. Function IOResult:Word;
  300. Begin
  301. IOResult:=InOutRes;
  302. InOutRes:=0;
  303. End;
  304. procedure fillchar(var x;count : longint;value : char);
  305. begin
  306. fillchar(x,count,byte(value));
  307. end;
  308. {*****************************************************************************
  309. Init / Exit / ExitProc
  310. *****************************************************************************}
  311. Procedure RunError;
  312. Begin
  313. RunError (0);
  314. End;
  315. Procedure Halt;
  316. Begin
  317. Halt(0);
  318. End;
  319. Procedure dump_stack(bp : Longint);
  320. Procedure dump_frame(addr : Longint);
  321. Begin
  322. {To be used by symify}
  323. Writeln(stderr,' 0x',HexStr(addr,8));
  324. {$ifdef VER0_99_5}
  325. Flush(stderr);
  326. {$endif VER0_99_5}
  327. End;
  328. var
  329. i, prevbp : Longint;
  330. Begin
  331. prevbp:=bp-1;
  332. i:=0;
  333. while bp > prevbp Do
  334. Begin
  335. dump_frame(get_addr(bp));
  336. Inc(i);
  337. If i>max_frame_dump Then
  338. exit;
  339. prevbp:=bp;
  340. bp:=get_next_frame(bp);
  341. End;
  342. End;
  343. Procedure Do_exit;[Public,Alias: '__EXIT'];
  344. {
  345. Don't call this direct, the call is generated by the compiler
  346. }
  347. var
  348. current_exit : Procedure;
  349. Begin
  350. while exitProc<>nil Do
  351. Begin
  352. InOutRes:=0;
  353. current_exit:=tProcedure(exitProc);
  354. exitProc:=nil;
  355. current_exit();
  356. End;
  357. If DoError Then
  358. Begin
  359. Writeln('Run time error ',Errorcode,' at 0x',hexstr(Longint(Erroraddr),8));
  360. dump_stack(ErrorBase);
  361. End;
  362. {$ifdef VER0_99_5}
  363. Flush(stderr);
  364. {$endif VER0_99_5}
  365. End;
  366. Type
  367. PExitProcInfo = ^TExitProcInfo;
  368. TExitProcInfo = Record
  369. Next : PExitProcInfo;
  370. SaveExit : Pointer;
  371. Proc : TProcedure;
  372. End;
  373. const
  374. ExitProcList: PExitProcInfo = nil;
  375. Procedure DoExitProc;
  376. var
  377. P : PExitProcInfo;
  378. Proc : TProcedure;
  379. Begin
  380. P:=ExitProcList;
  381. ExitProcList:=P^.Next;
  382. ExitProc:=P^.SaveExit;
  383. Proc:=P^.Proc;
  384. DisPose(P);
  385. Proc();
  386. End;
  387. Procedure AddExitProc(Proc: TProcedure);
  388. var
  389. P : PExitProcInfo;
  390. Begin
  391. New(P);
  392. P^.Next:=ExitProcList;
  393. P^.SaveExit:=ExitProc;
  394. P^.Proc:=Proc;
  395. ExitProcList:=P;
  396. ExitProc:=@DoExitProc;
  397. End;
  398. {*****************************************************************************
  399. Assert() support.
  400. *****************************************************************************}
  401. Procedure do_assert (Const Name,Msg : string; LineNo : Longint); [Public,Alias : 'FPC_DO_ASSERT'];
  402. begin
  403. If msg='' then
  404. write (stderr,'Assertion failed. ')
  405. else
  406. write (stderr,msg);
  407. writeln (stderr,'(File : ',name,', line ',LineNo,'.');
  408. flush (stderr);
  409. HandleError (227);
  410. end;
  411. {*****************************************************************************
  412. SetJmp/LongJmp support.
  413. *****************************************************************************}
  414. {$i setjump.inc}
  415. {*****************************************************************************
  416. Exception support.
  417. *****************************************************************************}
  418. { No go, because objpas needed :( (MVC) }
  419. { $i except.inc}
  420. {
  421. $Log$
  422. Revision 1.26 1998-08-11 00:05:26 peter
  423. * $ifdef ver0_99_5 updates
  424. Revision 1.25 1998/07/30 13:26:18 michael
  425. + Added support for ErrorProc variable. All internal functions are required
  426. to call HandleError instead of runerror from now on.
  427. This is necessary for exception support.
  428. Revision 1.24 1998/07/28 20:37:45 michael
  429. + added setjmp/longjmp and exception support
  430. Revision 1.23 1998/07/23 19:53:20 michael
  431. + Adapted assert to Delphi format
  432. Revision 1.22 1998/07/23 13:08:41 michael
  433. + Implemented DO_ASSERT function.
  434. Revision 1.21 1998/07/15 12:09:35 carl
  435. * would not compile under FPC v0.99.5
  436. Revision 1.20 1998/07/13 21:19:12 florian
  437. * some problems with ansi string support fixed
  438. Revision 1.19 1998/07/08 11:56:55 carl
  439. * randon and Random(l) now work correctly - don't touch it works!
  440. Revision 1.18 1998/07/02 13:01:55 carl
  441. * hmmm... it is luck (BSS zeroed with GAS) that DoError and ErrorBase work.
  442. Now they are initilized instead.
  443. Revision 1.17 1998/07/02 12:53:09 carl
  444. * DOERROR RESOTRED! DON'T TOUCH :)
  445. Revision 1.16 1998/07/02 12:11:50 carl
  446. * no SINGLE in m68k and other processors!
  447. Revision 1.15 1998/07/02 09:25:05 peter
  448. * fixed do_error in runtimeerror
  449. Revision 1.14 1998/07/01 15:29:59 peter
  450. * better readln/writeln
  451. Revision 1.13 1998/06/26 08:21:09 daniel
  452. - Doerror removed.
  453. Revision 1.12 1998/06/25 14:04:25 peter
  454. + internal inc/dec
  455. Revision 1.11 1998/06/25 09:44:20 daniel
  456. + RTLLITE directive to compile minimal RTL.
  457. Revision 1.10 1998/06/15 15:16:26 daniel
  458. * RTLLITE conditional added to produce smaller RTL
  459. Revision 1.9 1998/06/10 07:46:45 michael
  460. + Forgot to commit some changes
  461. Revision 1.8 1998/06/08 12:38:24 michael
  462. Implemented rtti, inserted ansistrings again
  463. Revision 1.7 1998/06/04 23:46:01 peter
  464. * comp,extended are only i386 added support_comp,support_extended
  465. Revision 1.6 1998/05/20 11:23:09 cvs
  466. * test commit. Shouldn't be allowed.
  467. Revision 1.5 1998/05/12 10:42:45 peter
  468. * moved getopts to inc/, all supported OS's need argc,argv exported
  469. + strpas, strlen are now exported in the systemunit
  470. * removed logs
  471. * removed $ifdef ver_above
  472. Revision 1.4 1998/04/16 12:30:47 peter
  473. + inc(pchar), dec(pchar), incc(pchar,a),dec(pchar,a)
  474. Revision 1.3 1998/04/08 07:53:32 michael
  475. + Changed Random() function. Moved from system to processor dependent files (from Pedro Gimeno)
  476. }