system.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. var
  25. { For Error Handling.}
  26. DoError : Boolean;
  27. ErrorBase : Longint;
  28. {****************************************************************************
  29. Include processor specific routines
  30. ****************************************************************************}
  31. {$IFDEF I386}
  32. {$IFDEF M68K}
  33. {$Error Can't determine processor type !}
  34. {$ENDIF}
  35. {$I i386.inc} { Case dependent, don't change }
  36. {$ELSE}
  37. {$IFDEF M68K}
  38. {$I m68k.inc} { Case dependent, don't change }
  39. {$ELSE}
  40. {$Error Can't determine processor type !}
  41. {$ENDIF}
  42. {$ENDIF}
  43. {****************************************************************************
  44. Routines which have compiler magic
  45. ****************************************************************************}
  46. {$I innr.inc}
  47. Function lo(i : Integer) : byte; [INTERNPROC: In_lo_Word];
  48. Function lo(w : Word) : byte; [INTERNPROC: In_lo_Word];
  49. Function lo(l : Longint) : Word; [INTERNPROC: In_lo_long];
  50. Function hi(i : Integer) : byte; [INTERNPROC: In_hi_Word];
  51. Function hi(w : Word) : byte; [INTERNPROC: In_hi_Word];
  52. Function hi(l : Longint) : Word; [INTERNPROC: In_hi_long];
  53. {$ifndef INTERN_INC}
  54. Procedure Inc(var i : Cardinal); [INTERNPROC: In_Inc_DWord];
  55. Procedure Inc(var i : Longint); [INTERNPROC: In_Inc_DWord];
  56. Procedure Inc(var i : Integer); [INTERNPROC: In_Inc_Word];
  57. Procedure Inc(var i : Word); [INTERNPROC: In_Inc_Word];
  58. Procedure Inc(var i : shortint); [INTERNPROC: In_Inc_byte];
  59. Procedure Inc(var i : byte); [INTERNPROC: In_Inc_byte];
  60. Procedure Inc(var c : Char); [INTERNPROC: In_Inc_byte];
  61. Procedure Inc(var p : PChar); [INTERNPROC: In_Inc_DWord];
  62. Procedure Dec(var i : Cardinal); [INTERNPROC: In_Dec_DWord];
  63. Procedure Dec(var i : Longint); [INTERNPROC: In_Dec_DWord];
  64. Procedure Dec(var i : Integer); [INTERNPROC: In_Dec_Word];
  65. Procedure Dec(var i : Word); [INTERNPROC: In_Dec_Word];
  66. Procedure Dec(var i : shortint); [INTERNPROC: In_Dec_byte];
  67. Procedure Dec(var i : byte); [INTERNPROC: In_Dec_byte];
  68. Procedure Dec(var c : Char); [INTERNPROC: In_Dec_byte];
  69. Procedure Dec(var p : PChar); [INTERNPROC: In_Dec_DWord];
  70. {$endif INTERN_INC}
  71. Function chr(b : byte) : Char; [INTERNPROC: In_chr_byte];
  72. Function Length(s : string) : byte; [INTERNPROC: In_Length_string];
  73. Procedure Reset(var f : TypedFile); [INTERNPROC: In_Reset_TypedFile];
  74. Procedure Rewrite(var f : TypedFile); [INTERNPROC: In_Rewrite_TypedFile];
  75. {****************************************************************************
  76. Set Handling
  77. ****************************************************************************}
  78. { Include set support which is processor specific}
  79. {$I set.inc}
  80. {****************************************************************************
  81. Subroutines for String handling
  82. ****************************************************************************}
  83. { Needs to be before RTTI handling }
  84. {$i sstrings.inc}
  85. {$ifdef UseAnsiStrings}
  86. {$i astrings.pp}
  87. {$else}
  88. { Provide dummy procedures needed for rtti}
  89. Procedure decr_ansi_ref (P : pointer);[Alias : 'DECR_ANSI_REF'];
  90. begin
  91. end;
  92. Procedure incr_ansi_ref (P : pointer);[Alias : 'INCR_ANSI_REF'];
  93. begin
  94. end;
  95. {$endif}
  96. {****************************************************************************
  97. Run-Time Type Information (RTTI)
  98. ****************************************************************************}
  99. {$i rtti.inc}
  100. {****************************************************************************
  101. Math Routines
  102. ****************************************************************************}
  103. {$ifndef RTLLITE}
  104. function Hi(b : byte): byte;
  105. begin
  106. Hi := b shr 4
  107. end;
  108. function Lo(b : byte): byte;
  109. begin
  110. Lo := b and $0f
  111. end;
  112. {$ifndef INTERN_INC}
  113. Procedure Inc(var i : Cardinal;a: Longint);
  114. Begin
  115. I:=I+A;
  116. End;
  117. Procedure Dec(var i : Cardinal;a: Longint);
  118. Begin
  119. I:=I-A;
  120. End;
  121. Procedure Inc(var i : Longint;a : Longint);
  122. Begin
  123. i:=i+a;
  124. End;
  125. Procedure Dec(var i : Longint;a : Longint);
  126. Begin
  127. i:=i-a;
  128. End;
  129. Procedure Dec(var i : Word;a : Longint);
  130. Begin
  131. i:=i-a;
  132. End;
  133. Procedure Inc(var i : Word;a : Longint);
  134. Begin
  135. i:=i+a;
  136. End;
  137. Procedure Dec(var i : Integer;a : Longint);
  138. Begin
  139. i:=i-a;
  140. End;
  141. Procedure Inc(var i : Integer;a : Longint);
  142. Begin
  143. i:=i+a;
  144. End;
  145. Procedure Dec(var i : byte;a : Longint);
  146. Begin
  147. i:=i-a;
  148. End;
  149. Procedure Inc(var i : byte;a : Longint);
  150. Begin
  151. i:=i+a;
  152. End;
  153. Procedure Dec(var i : shortint;a : Longint);
  154. Begin
  155. i:=i-a;
  156. End;
  157. Procedure Inc(var i : shortint;a : Longint);
  158. Begin
  159. i:=i+a;
  160. End;
  161. Procedure Dec(var c : Char;a : Longint);
  162. Begin
  163. byte(c):=byte(c)-a;
  164. End;
  165. Procedure Inc(var c : Char;a : Longint);
  166. Begin
  167. Byte(c):=byte(c)+a;
  168. End;
  169. Procedure Dec(var p : PChar;a : Longint);
  170. Begin
  171. longint(p):=longint(p)-a;
  172. End;
  173. Procedure Inc(var p : PChar;a : Longint);
  174. Begin
  175. longint(p):=longint(p)+a;
  176. End;
  177. {$endif INTERN_INC}
  178. Function swap (X : Word) : Word;
  179. Begin
  180. swap:=(X and $ff) shl 8 + (X shr 8)
  181. End;
  182. Function Swap (X : Integer) : Integer;
  183. Begin
  184. Swap:=Integer(Swap(Word(X)));
  185. End;
  186. Function swap (X : Longint) : Longint;
  187. Begin
  188. Swap:=(X and $ffff) shl 16 + (X shr 16)
  189. End;
  190. Function Swap (X : Cardinal) : Cardinal;
  191. Begin
  192. Swap:=Swap(Longint(X));
  193. End;
  194. {$endif RTLLITE}
  195. {$R-}
  196. Function Random(l : Longint) : Longint;
  197. {
  198. the problem Wwth this Function is if l is maxLongint*3/4 then the
  199. probability to obtain a number in the range maxlongint*1/4 to maxlongint*1/2
  200. is two times smaller than the probability for other numbers !
  201. }
  202. Begin
  203. Randseed:=Randseed*134775813+1;
  204. Random:=abs(Randseed mod l);
  205. End;
  206. Function Random : real;
  207. {
  208. I am not sure about the accuracy of such a value (PM)
  209. }
  210. Begin
  211. Random:=abs(Randseed);
  212. Random:=Random/(maxLongint+1.0);
  213. Randseed:=Randseed*134775813+1;
  214. Random:=(abs(Randseed)+Random)/(maxLongint+2.0);
  215. End;
  216. { Include processor specific routines }
  217. {$I math.inc}
  218. {****************************************************************************
  219. Memory Management
  220. ****************************************************************************}
  221. {$ifndef RTLLITE}
  222. Function Ptr(sel,off : Longint) : pointer;
  223. Begin
  224. sel:=0;
  225. ptr:=pointer(off);
  226. End;
  227. Function Addr (Var X) : Pointer;
  228. Begin
  229. Addr:=@(X);
  230. End;
  231. Function CSeg : Word;
  232. Begin
  233. Cseg:=0;
  234. End;
  235. Function DSeg : Word;
  236. Begin
  237. Dseg:=0;
  238. End;
  239. Function SSeg : Word;
  240. Begin
  241. Sseg:=0;
  242. End;
  243. {$endif RTLLITE}
  244. {*****************************************************************************
  245. Miscellaneous
  246. *****************************************************************************}
  247. Function IOResult:Word;
  248. Begin
  249. IOResult:=InOutRes;
  250. InOutRes:=0;
  251. End;
  252. procedure fillchar(var x;count : longint;value : char);
  253. begin
  254. fillchar(x,count,byte(value));
  255. end;
  256. {*****************************************************************************
  257. Init / Exit / ExitProc
  258. *****************************************************************************}
  259. Procedure RunError;
  260. Begin
  261. RunError (0);
  262. End;
  263. Procedure Halt;
  264. Begin
  265. Halt(0);
  266. End;
  267. Procedure dump_stack(bp : Longint);
  268. Procedure dump_frame(addr : Longint);
  269. Begin
  270. {To be used by symify}
  271. Writeln(stderr,' 0x',HexStr(addr,8));
  272. {$IFNDEF NEW_READWRITE}
  273. Flush(stderr);
  274. {$ENDIF NEW_READWRITE}
  275. End;
  276. var
  277. i, prevbp : Longint;
  278. Begin
  279. prevbp:=bp-1;
  280. i:=0;
  281. while bp > prevbp Do
  282. Begin
  283. dump_frame(get_addr(bp));
  284. Inc(i);
  285. If i>max_frame_dump Then
  286. exit;
  287. prevbp:=bp;
  288. bp:=get_next_frame(bp);
  289. End;
  290. End;
  291. Procedure Do_exit;[Public,Alias: '__EXIT'];
  292. {
  293. Don't call this direct, the call is generated by the compiler
  294. }
  295. var
  296. current_exit : Procedure;
  297. Begin
  298. while exitProc<>nil Do
  299. Begin
  300. InOutRes:=0;
  301. current_exit:=tProcedure(exitProc);
  302. exitProc:=nil;
  303. current_exit();
  304. End;
  305. If DoError Then
  306. Begin
  307. Writeln('Run time error ',Errorcode,' at 0x',hexstr(Longint(Erroraddr),8));
  308. dump_stack(ErrorBase);
  309. End;
  310. {$IFNDEF NEW_READWRITE}
  311. Flush(stderr);
  312. {$ENDIF NEW_READWRITE}
  313. End;
  314. Type
  315. PExitProcInfo = ^TExitProcInfo;
  316. TExitProcInfo = Record
  317. Next : PExitProcInfo;
  318. SaveExit : Pointer;
  319. Proc : TProcedure;
  320. End;
  321. const
  322. ExitProcList: PExitProcInfo = nil;
  323. Procedure DoExitProc;
  324. var
  325. P : PExitProcInfo;
  326. Proc : TProcedure;
  327. Begin
  328. P:=ExitProcList;
  329. ExitProcList:=P^.Next;
  330. ExitProc:=P^.SaveExit;
  331. Proc:=P^.Proc;
  332. DisPose(P);
  333. Proc();
  334. End;
  335. Procedure AddExitProc(Proc: TProcedure);
  336. var
  337. P : PExitProcInfo;
  338. Begin
  339. New(P);
  340. P^.Next:=ExitProcList;
  341. P^.SaveExit:=ExitProc;
  342. P^.Proc:=Proc;
  343. ExitProcList:=P;
  344. ExitProc:=@DoExitProc;
  345. End;
  346. {
  347. $Log$
  348. Revision 1.17 1998-07-02 12:53:09 carl
  349. * DOERROR RESOTRED! DON'T TOUCH :)
  350. Revision 1.16 1998/07/02 12:11:50 carl
  351. * no SINGLE in m68k and other processors!
  352. Revision 1.15 1998/07/02 09:25:05 peter
  353. * fixed do_error in runtimeerror
  354. Revision 1.14 1998/07/01 15:29:59 peter
  355. * better readln/writeln
  356. Revision 1.13 1998/06/26 08:21:09 daniel
  357. - Doerror removed.
  358. Revision 1.12 1998/06/25 14:04:25 peter
  359. + internal inc/dec
  360. Revision 1.11 1998/06/25 09:44:20 daniel
  361. + RTLLITE directive to compile minimal RTL.
  362. Revision 1.10 1998/06/15 15:16:26 daniel
  363. * RTLLITE conditional added to produce smaller RTL
  364. Revision 1.9 1998/06/10 07:46:45 michael
  365. + Forgot to commit some changes
  366. Revision 1.8 1998/06/08 12:38:24 michael
  367. Implemented rtti, inserted ansistrings again
  368. Revision 1.7 1998/06/04 23:46:01 peter
  369. * comp,extended are only i386 added support_comp,support_extended
  370. Revision 1.6 1998/05/20 11:23:09 cvs
  371. * test commit. Shouldn't be allowed.
  372. Revision 1.5 1998/05/12 10:42:45 peter
  373. * moved getopts to inc/, all supported OS's need argc,argv exported
  374. + strpas, strlen are now exported in the systemunit
  375. * removed logs
  376. * removed $ifdef ver_above
  377. Revision 1.4 1998/04/16 12:30:47 peter
  378. + inc(pchar), dec(pchar), incc(pchar,a),dec(pchar,a)
  379. Revision 1.3 1998/04/08 07:53:32 michael
  380. + Changed Random() function. Moved from system to processor dependent files (from Pedro Gimeno)
  381. }