system.inc 14 KB

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