system.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 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. {$IFNDEF ORDINTERN}
  68. Function ord(c : Char) : byte; [INTERNPROC: In_ord_Char];
  69. {$ENDIF ORDINTERN}
  70. Function chr(b : byte) : Char; [INTERNPROC: In_chr_byte];
  71. Function Length(s : string) : byte; [INTERNPROC: In_Length_string];
  72. {$IFDEF VER_ABOVE0_9_5}
  73. Procedure Reset(var f : TypedFile); [INTERNPROC: In_Reset_TypedFile];
  74. Procedure Rewrite(var f : TypedFile); [INTERNPROC: In_Rewrite_TypedFile];
  75. {$ENDIF}
  76. {****************************************************************************
  77. Math Routines
  78. ****************************************************************************}
  79. function Hi(b : byte): byte;
  80. begin
  81. Hi := b shr 4
  82. end;
  83. function Lo(b : byte): byte;
  84. begin
  85. Lo := b and $0f
  86. end;
  87. Procedure Inc(var i : Cardinal;a: Longint);
  88. Begin
  89. I:=I+A;
  90. End;
  91. Procedure Dec(var i : Cardinal;a: Longint);
  92. Begin
  93. I:=I-A;
  94. End;
  95. Procedure Inc(var i : Longint;a : Longint);
  96. Begin
  97. i:=i+a;
  98. End;
  99. Procedure Dec(var i : Longint;a : Longint);
  100. Begin
  101. i:=i-a;
  102. End;
  103. Procedure Dec(var i : Word;a : Longint);
  104. Begin
  105. i:=i-a;
  106. End;
  107. Procedure Inc(var i : Word;a : Longint);
  108. Begin
  109. i:=i+a;
  110. End;
  111. Procedure Dec(var i : Integer;a : Longint);
  112. Begin
  113. i:=i-a;
  114. End;
  115. Procedure Inc(var i : Integer;a : Longint);
  116. Begin
  117. i:=i+a;
  118. End;
  119. Procedure Dec(var i : byte;a : Longint);
  120. Begin
  121. i:=i-a;
  122. End;
  123. Procedure Inc(var i : byte;a : Longint);
  124. Begin
  125. i:=i+a;
  126. End;
  127. Procedure Dec(var i : shortint;a : Longint);
  128. Begin
  129. i:=i-a;
  130. End;
  131. Procedure Inc(var i : shortint;a : Longint);
  132. Begin
  133. i:=i+a;
  134. End;
  135. Procedure Dec(var c : Char;a : Longint);
  136. Begin
  137. byte(c):=byte(c)-a;
  138. End;
  139. Procedure Inc(var c : Char;a : Longint);
  140. Begin
  141. Byte(c):=byte(c)+a;
  142. End;
  143. Function swap (X : Word) : Word;
  144. Begin
  145. swap:=(X and $ff) shl 8 + (X shr 8)
  146. End;
  147. Function Swap (X : Integer) : Integer;
  148. Begin
  149. Swap:=Integer(Swap(Word(X)));
  150. End;
  151. Function swap (X : Longint) : Longint;
  152. Begin
  153. Swap:=(X and $ffff) shl 16 + (X shr 16)
  154. End;
  155. Function Swap (X : Cardinal) : Cardinal;
  156. Begin
  157. Swap:=Swap(Longint(X));
  158. End;
  159. {$R-}
  160. Function Random(l : Longint) : Longint;
  161. {
  162. the problem Wwth this Function is if l is maxLongint*3/4 then the
  163. probability to obtain a number in the range maxlongint*1/4 to maxlongint*1/2
  164. is two times smaller than the probability for other numbers !
  165. }
  166. Begin
  167. Randseed:=Randseed*134775813+1;
  168. Random:=abs(Randseed mod l);
  169. End;
  170. Function Random : real;
  171. {
  172. I am not sure about the accuracy of such a value (PM)
  173. }
  174. Begin
  175. Random:=abs(Randseed);
  176. Random:=Random/(maxLongint+1.0);
  177. Randseed:=Randseed*134775813+1;
  178. Random:=(abs(Randseed)+Random)/(maxLongint+2.0);
  179. End;
  180. { Include processor specific routines }
  181. {$I math.inc}
  182. {****************************************************************************
  183. Set Handling
  184. ****************************************************************************}
  185. { Include set support which is processor specific}
  186. {$I set.inc}
  187. {****************************************************************************
  188. Memory Management
  189. ****************************************************************************}
  190. Function Ptr(sel,off : Longint) : pointer;
  191. Begin
  192. sel:=0;
  193. {$IFDEF DoMapping}
  194. {$IFDEF DoS}
  195. ptr:=pointer($e0000000+sel shl 4+off);
  196. {$ELSE}
  197. ptr:=pointer(sel shl 4+off);
  198. {$ENDIF}
  199. {$ELSE}
  200. ptr:=pointer(off);
  201. {$ENDIF}
  202. End;
  203. Function Addr (Var X) : Pointer;
  204. Begin
  205. Addr:=@(X);
  206. End;
  207. Function CSeg : Word;
  208. Begin
  209. Cseg:=0;
  210. End;
  211. Function DSeg : Word;
  212. Begin
  213. Dseg:=0;
  214. End;
  215. Function SSeg : Word;
  216. Begin
  217. Sseg:=0;
  218. End;
  219. {****************************************************************************
  220. Subroutines for short strings are in sstrings.inc
  221. ****************************************************************************}
  222. {$i sstrings.inc}
  223. {*****************************************************************************
  224. Miscellaneous
  225. *****************************************************************************}
  226. Function IOResult:Word;
  227. Begin
  228. IOResult:=InOutRes;
  229. InOutRes:=0;
  230. End;
  231. procedure fillchar(var x;count : longint;value : char);
  232. begin
  233. fillchar(x,count,byte(value));
  234. end;
  235. {*****************************************************************************
  236. Init / Exit / ExitProc
  237. *****************************************************************************}
  238. Procedure RunError;
  239. Begin
  240. RunError (0);
  241. End;
  242. Procedure Halt;
  243. Begin
  244. Halt(0);
  245. End;
  246. Procedure Initexception;[Public,Alias: 'INITEXCEPTION'];
  247. Begin
  248. Writeln('Exception occurred during program initialization.');
  249. halt(216);
  250. End;
  251. Procedure dump_stack(bp : Longint);
  252. Procedure dump_frame(addr : Longint);
  253. Begin
  254. {To be used by symify}
  255. Writeln(stderr,' 0x',HexStr(addr,8));
  256. Flush(stderr);
  257. End;
  258. var
  259. i, prevbp : Longint;
  260. Begin
  261. prevbp:=bp-1;
  262. i:=0;
  263. while bp > prevbp Do
  264. Begin
  265. dump_frame(get_addr(bp));
  266. Inc(i);
  267. If i>max_frame_dump Then
  268. exit;
  269. prevbp:=bp;
  270. bp:=get_next_frame(bp);
  271. End;
  272. End;
  273. Procedure Do_exit;[Public,Alias: '__EXIT'];
  274. {
  275. Don't call this direct, the call is generated by the compiler
  276. }
  277. var
  278. current_exit : Procedure;
  279. Begin
  280. while exitProc<>nil Do
  281. Begin
  282. InOutRes:=0;
  283. current_exit:=tProcedure(exitProc);
  284. exitProc:=nil;
  285. current_exit();
  286. End;
  287. If DoError Then
  288. Begin
  289. Writeln('Run time error ',Errorcode,' at 0x',hexstr(Longint(Erroraddr),8));
  290. dump_stack(ErrorBase);
  291. End;
  292. { this is wrong at least for dos !!!
  293. in dos input output and stderr must be left open !! }
  294. {$ifndef DOS}
  295. {$ifndef GO32V2}
  296. Close(Output);
  297. Close(StdErr);
  298. {$endif GO32V2}
  299. {$endif DOS}
  300. End;
  301. Type
  302. PExitProcInfo = ^TExitProcInfo;
  303. TExitProcInfo = Record
  304. Next : PExitProcInfo;
  305. SaveExit : Pointer;
  306. Proc : TProcedure;
  307. End;
  308. const
  309. ExitProcList: PExitProcInfo = nil;
  310. Procedure DoExitProc;
  311. var
  312. P : PExitProcInfo;
  313. Proc : TProcedure;
  314. Begin
  315. P:=ExitProcList;
  316. ExitProcList:=P^.Next;
  317. ExitProc:=P^.SaveExit;
  318. Proc:=P^.Proc;
  319. DisPose(P);
  320. Proc();
  321. End;
  322. Procedure AddExitProc(Proc: TProcedure);
  323. var
  324. P : PExitProcInfo;
  325. Begin
  326. New(P);
  327. P^.Next:=ExitProcList;
  328. P^.SaveExit:=ExitProc;
  329. P^.Proc:=Proc;
  330. ExitProcList:=P;
  331. ExitProc:=@DoExitProc;
  332. End;
  333. {
  334. $Log$
  335. Revision 1.2 1998-03-25 23:39:17 florian
  336. * complete Object Pascal support moved to objpas unit
  337. Revision 1.1.1.1 1998/03/25 11:18:43 root
  338. * Restored version
  339. Revision 1.29 1998/02/07 05:31:22 carl
  340. * bugfix of Swap with cardinal (no return value was set)
  341. Revision 1.28 1998/01/30 17:19:18 pierre
  342. * output and stderr must not be closed at exit under dos
  343. Revision 1.27 1998/01/26 11:59:53 michael
  344. + Added log at the end
  345. Working file: rtl/inc/system.inc
  346. description:
  347. ----------------------------
  348. revision 1.26
  349. date: 1998/01/23 22:21:49; author: michael; state: Exp; lines: +18 -6
  350. + Set up things for Delphi Object model under DELPHI_EXTENSIONS
  351. ----------------------------
  352. revision 1.25
  353. date: 1998/01/20 09:14:35; author: michael; state: Exp; lines: +13 -1
  354. + implemented Hi and Lo for bytes. SHould be made an internalproc, though.
  355. ----------------------------
  356. revision 1.24
  357. date: 1998/01/19 10:21:32; author: michael; state: Exp; lines: +10 -1
  358. * moved Fillchar t(..,char) to system.inc
  359. ----------------------------
  360. revision 1.23
  361. date: 1998/01/16 17:58:51; author: florian; state: Exp; lines: +8 -2
  362. * basic tobject added (create, destroy and free)
  363. ----------------------------
  364. revision 1.22
  365. date: 1998/01/11 02:45:43; author: michael; state: Exp; lines: +322 -1380
  366. + Moved file operations to
  367. - file.inc : Untyped file handling
  368. - text.inc text file handling
  369. - typefile.inc typed file handling
  370. - version stuff to version.inc
  371. By Peter Vreman.
  372. ----------------------------
  373. revision 1.21
  374. date: 1998/01/06 00:29:33; author: michael; state: Exp; lines: +45 -81
  375. Implemented a system independent sequence of reset/rewrite/append fileopenfunc etc system \n (from Peter Vreman)
  376. ----------------------------
  377. revision 1.20
  378. date: 1998/01/05 12:32:14; author: michael; state: Exp; lines: +143 -748
  379. * Undid changes by Carl, it rendered the system unit unusable !
  380. ----------------------------
  381. revision 1.19
  382. date: 1998/01/05 00:47:10; author: carl; state: Exp; lines: +748 -143
  383. + Compatible with m68k floating point types
  384. + Test68000/Test68882 variables added when in m68k mode
  385. ----------------------------
  386. revision 1.18
  387. date: 1998/01/03 00:44:19; author: michael; state: Exp; lines: +6 -25
  388. * Shorter WRITE_TEXT_BOOLEAN (From Peter Vreman)
  389. - Removed unused vars in READ_TEXT_CHAR (From Peter Vreman)
  390. - Removed 'far' from DoExitProc (From Peter Vreman)
  391. ----------------------------
  392. revision 1.17
  393. date: 1998/01/01 16:56:08; author: michael; state: Exp; lines: +60 -1
  394. + Implemented Addr() function.
  395. + Implemented Inc/Dec for cardinal
  396. + Implemented Inc/Dec (C : Char; A : longint);
  397. ----------------------------
  398. revision 1.16
  399. date: 1997/12/24 14:08:38; author: michael; state: Exp; lines: +7 -2
  400. + Added SSEG function, (From Peter Vreman)
  401. * fixed bug in append, default filehandle should be 1, not 0 (From Peter
  402. Vreman)
  403. ----------------------------
  404. revision 1.15
  405. date: 1997/12/23 16:32:21; author: michael; state: Exp; lines: +24 -26
  406. * More efficient treating of append, reset and rewrite (From Peter Vreman)
  407. ----------------------------
  408. revision 1.14
  409. date: 1997/12/22 18:53:18; author: michael; state: Exp; lines: +6 -752
  410. + All 255-length string handling routines have been moved to sstrings.inc.
  411. ----------------------------
  412. revision 1.13
  413. date: 1997/12/22 15:35:30; author: michael; state: Exp; lines: +10 -9
  414. * Fixed bug introduced by previous commit.
  415. ----------------------------
  416. revision 1.12
  417. date: 1997/12/22 11:11:54; author: michael; state: Exp; lines: +138 -86
  418. * Implemented better (faster) string handling routines from Peter Vreman.
  419. ----------------------------
  420. revision 1.11
  421. date: 1997/12/22 10:44:26; author: pierre; state: Exp; lines: +5 -5
  422. * tipping error in READ_TEXT_INTEGER and so on
  423. ----------------------------
  424. revision 1.10
  425. date: 1997/12/19 11:37:55; author: pierre; state: Exp; lines: +53 -1
  426. * added read_text for integer word byte and shortint
  427. ----------------------------
  428. revision 1.9
  429. date: 1997/12/11 11:45:32; author: michael; state: Exp; lines: +45 -45
  430. + undid changes to pos/delete/copy/insert. Version 1.8 was UNUSABLE.
  431. ----------------------------
  432. revision 1.8
  433. date: 1997/12/10 12:13:20; author: michael; state: Exp; lines: +89 -89
  434. * changed DateiFunc to FileFunc
  435. ----------------------------
  436. revision 1.7
  437. date: 1997/12/02 16:08:53; author: pierre; state: Exp; lines: +7 -1
  438. * bug fix in val(string,longint,word) for '-2147483648'
  439. ----------------------------
  440. revision 1.6
  441. date: 1997/12/01 12:08:05; author: michael; state: Exp; lines: +10 -4
  442. + added copyright reference header.
  443. ----------------------------
  444. revision 1.5
  445. date: 1997/11/28 19:45:22; author: pierre; state: Exp; lines: +14 -12
  446. * one more bug fix with namelength
  447. + fixed math in fixed_math define (does not compile yet)
  448. ----------------------------
  449. revision 1.4
  450. date: 1997/11/28 12:21:51; author: michael; state: Exp; lines: +2 -2
  451. Removed the WRITE_TEXT_CARDINAL for version 0.9.1 and less.
  452. ----------------------------
  453. revision 1.3
  454. date: 1997/11/27 22:49:05; author: florian; state: Exp; lines: +5 -0
  455. - CPU.PP added
  456. - some bugs in DOS fixed (espsecially for go32v1)
  457. - the win32 system unit is now compilable
  458. ----------------------------
  459. revision 1.2
  460. date: 1997/11/27 16:29:37; author: michael; state: Exp; lines: +3 -3
  461. Change submitted by Pierre Muller:
  462. Added check : version must be above 0.9.7 for extended type
  463. handling functions.
  464. ----------------------------
  465. revision 1.1
  466. date: 1997/11/27 08:33:47; author: michael; state: Exp;
  467. Initial revision
  468. ----------------------------
  469. revision 1.1.1.1
  470. date: 1997/11/27 08:33:47; author: michael; state: Exp; lines: +0 -0
  471. FPC RTL CVS start
  472. =============================================================================
  473. }