system.inc 11 KB

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