system.inc 10 KB

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