system.inc 13 KB

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