system.inc 10.0 KB

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