system.inc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. {$IFDEF DoMapping}
  189. {$IFDEF DoS}
  190. ptr:=pointer($e0000000+sel shl 4+off);
  191. {$ELSE}
  192. ptr:=pointer(sel shl 4+off);
  193. {$ENDIF}
  194. {$ELSE}
  195. ptr:=pointer(off);
  196. {$ENDIF}
  197. End;
  198. Function Addr (Var X) : Pointer;
  199. Begin
  200. Addr:=@(X);
  201. End;
  202. Function CSeg : Word;
  203. Begin
  204. Cseg:=0;
  205. End;
  206. Function DSeg : Word;
  207. Begin
  208. Dseg:=0;
  209. End;
  210. Function SSeg : Word;
  211. Begin
  212. Sseg:=0;
  213. End;
  214. {****************************************************************************
  215. Subroutines for short strings are in sstrings.inc
  216. ****************************************************************************}
  217. {$i sstrings.inc}
  218. {*****************************************************************************
  219. Miscellaneous
  220. *****************************************************************************}
  221. Function IOResult:Word;
  222. Begin
  223. IOResult:=InOutRes;
  224. InOutRes:=0;
  225. End;
  226. procedure fillchar(var x;count : longint;value : char);
  227. begin
  228. fillchar(x,count,byte(value));
  229. end;
  230. {*****************************************************************************
  231. Init / Exit / ExitProc
  232. *****************************************************************************}
  233. Procedure RunError;
  234. Begin
  235. RunError (0);
  236. End;
  237. Procedure Halt;
  238. Begin
  239. Halt(0);
  240. End;
  241. Procedure Initexception;[Public,Alias: 'INITEXCEPTION'];
  242. Begin
  243. Writeln('Exception occurred during program initialization.');
  244. halt(216);
  245. End;
  246. Procedure dump_stack(bp : Longint);
  247. Procedure dump_frame(addr : Longint);
  248. Begin
  249. {To be used by symify}
  250. Writeln(stderr,' 0x',HexStr(addr,8));
  251. Flush(stderr);
  252. End;
  253. var
  254. i, prevbp : Longint;
  255. Begin
  256. prevbp:=bp-1;
  257. i:=0;
  258. while bp > prevbp Do
  259. Begin
  260. dump_frame(get_addr(bp));
  261. Inc(i);
  262. If i>max_frame_dump Then
  263. exit;
  264. prevbp:=bp;
  265. bp:=get_next_frame(bp);
  266. End;
  267. End;
  268. Procedure Do_exit;[Public,Alias: '__EXIT'];
  269. {
  270. Don't call this direct, the call is generated by the compiler
  271. }
  272. var
  273. current_exit : Procedure;
  274. Begin
  275. while exitProc<>nil Do
  276. Begin
  277. InOutRes:=0;
  278. current_exit:=tProcedure(exitProc);
  279. exitProc:=nil;
  280. current_exit();
  281. End;
  282. If DoError Then
  283. Begin
  284. Writeln('Run time error ',Errorcode,' at 0x',hexstr(Longint(Erroraddr),8));
  285. dump_stack(ErrorBase);
  286. End;
  287. { this is wrong at least for dos !!!
  288. in dos input output and stderr must be left open !! }
  289. {$ifndef DOS}
  290. {$ifndef GO32V2}
  291. Close(Output);
  292. Close(StdErr);
  293. {$endif GO32V2}
  294. {$endif DOS}
  295. End;
  296. Type
  297. PExitProcInfo = ^TExitProcInfo;
  298. TExitProcInfo = Record
  299. Next : PExitProcInfo;
  300. SaveExit : Pointer;
  301. Proc : TProcedure;
  302. End;
  303. const
  304. ExitProcList: PExitProcInfo = nil;
  305. Procedure DoExitProc;
  306. var
  307. P : PExitProcInfo;
  308. Proc : TProcedure;
  309. Begin
  310. P:=ExitProcList;
  311. ExitProcList:=P^.Next;
  312. ExitProc:=P^.SaveExit;
  313. Proc:=P^.Proc;
  314. DisPose(P);
  315. Proc();
  316. End;
  317. Procedure AddExitProc(Proc: TProcedure);
  318. var
  319. P : PExitProcInfo;
  320. Begin
  321. New(P);
  322. P^.Next:=ExitProcList;
  323. P^.SaveExit:=ExitProc;
  324. P^.Proc:=Proc;
  325. ExitProcList:=P;
  326. ExitProc:=@DoExitProc;
  327. End;
  328. {
  329. $Log$
  330. Revision 1.5 1998-05-12 10:42:45 peter
  331. * moved getopts to inc/, all supported OS's need argc,argv exported
  332. + strpas, strlen are now exported in the systemunit
  333. * removed logs
  334. * removed $ifdef ver_above
  335. Revision 1.4 1998/04/16 12:30:47 peter
  336. + inc(pchar), dec(pchar), incc(pchar,a),dec(pchar,a)
  337. Revision 1.3 1998/04/08 07:53:32 michael
  338. + Changed Random() function. Moved from system to processor dependent files (from Pedro Gimeno)
  339. }