system.inc 9.3 KB

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