jsystem.inc 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  1. {
  2. This file is part of the Free Pascal Run time library.
  3. Copyright (c) 1999-2008 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. For details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. { The RTTI is implemented through a series of constants : }
  11. Const
  12. // please update tkManagedTypes below if you add new
  13. // values
  14. tkUnknown = 0;
  15. tkInteger = 1;
  16. tkChar = 2;
  17. tkEnumeration = 3;
  18. tkFloat = 4;
  19. tkSet = 5;
  20. tkMethod = 6;
  21. tkSString = 7;
  22. tkString = tkSString;
  23. tkLString = 8;
  24. tkAString = 9;
  25. tkWString = 10;
  26. tkVariant = 11;
  27. tkArray = 12;
  28. tkRecord = 13;
  29. tkInterface = 14;
  30. tkClass = 15;
  31. tkObject = 16;
  32. tkWChar = 17;
  33. tkBool = 18;
  34. tkInt64 = 19;
  35. tkQWord = 20;
  36. tkDynArray = 21;
  37. tkInterfaceCorba = 22;
  38. tkProcVar = 23;
  39. tkUString = 24;
  40. tkHelper = 26;
  41. // all potentially managed types
  42. tkManagedTypes = [tkAstring,tkWstring,tkUstring,tkArray,
  43. tkObject,tkRecord,tkDynArray,tkInterface,tkVariant];
  44. {****************************************************************************
  45. Local types
  46. ****************************************************************************}
  47. {
  48. TextRec and FileRec are put in a separate file to make it available to other
  49. units without putting it explicitly in systemh.
  50. This way we keep TP compatibility, and the TextRec definition is available
  51. for everyone who needs it.
  52. }
  53. {$ifdef FPC_HAS_FEATURE_FILEIO}
  54. {$i filerec.inc}
  55. {$endif FPC_HAS_FEATURE_FILEIO}
  56. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  57. {$i textrec.inc}
  58. {$endif FPC_HAS_FEATURE_TEXTIO}
  59. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  60. {$ifdef FPC_OBJFPC_EXTENDED_IF}
  61. {$if High(errorcode)<>maxExitCode}
  62. {$define FPC_LIMITED_EXITCODE}
  63. {$endif}
  64. {$else}
  65. {$define FPC_LIMITED_EXITCODE}
  66. {$endif FPC_OBJFPC_EXTENDED_IF}
  67. {$endif FPC_HAS_FEATURE_EXITCODE}
  68. Procedure HandleError (Errno : Longint); forward;
  69. Procedure HandleErrorFrame (Errno : longint;frame : Pointer); forward;
  70. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  71. type
  72. FileFunc = Procedure(var t : TextRec);
  73. {$endif FPC_HAS_FEATURE_TEXTIO}
  74. const
  75. STACK_MARGIN = 16384; { Stack size margin for stack checking }
  76. { Random / Randomize constants }
  77. OldRandSeed : Cardinal = 0;
  78. (*
  79. { For Error Handling.}
  80. ErrorBase : Pointer = nil;
  81. *)
  82. { Used by the ansi/widestrings and maybe also other things in the future }
  83. var
  84. { widechar, because also used by widestring -> pwidechar conversions }
  85. emptychar : widechar;public name 'FPC_EMPTYCHAR';
  86. {$ifndef FPC_NO_GENERIC_STACK_CHECK}
  87. { if the OS does the stack checking, we don't need any stklen from the
  88. main program }
  89. initialstklen : SizeUint;external name '__stklen';
  90. {$endif FPC_NO_GENERIC_STACK_CHECK}
  91. { checks whether the given suggested size for the stack of the current
  92. thread is acceptable. If this is the case, returns it unaltered.
  93. Otherwise it should return an acceptable value.
  94. Operating systems that automatically expand their stack on demand, should
  95. simply return a very large value.
  96. Operating systems which do not have a possibility to retrieve stack size
  97. information, should simply return the given stklen value (This is the default
  98. implementation).
  99. }
  100. {$ifdef FPC_HAS_FEATURE_STACKCHECK}
  101. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt; forward;
  102. {$endif FPC_HAS_FEATURE_STACKCHECK}
  103. {*****************************************************************************
  104. OS dependent Helpers/Syscalls
  105. *****************************************************************************}
  106. { for some OSes do_isdevice is defined in sysos.inc, but for others (win32)
  107. it isn't, and is used before the actual definition is encountered }
  108. {$ifdef FPC_HAS_FEATURE_FILEIO}
  109. function do_isdevice(handle:thandle):boolean;forward;
  110. {$endif FPC_HAS_FEATURE_FILEIO}
  111. {$i sysos.inc}
  112. {****************************************************************************
  113. Include processor specific routines
  114. ****************************************************************************}
  115. {$ifdef FPC_USE_LIBC}
  116. { Under Haiku, bcopy cause a problem when searching for include file
  117. in the compiler. So, we use the internal implementation for now
  118. under BeOS and Haiku. }
  119. {$ifndef BEOS}
  120. { prefer libc implementations over our own, as they're most likely faster }
  121. {$i cgeneric.inc}
  122. { is now declared as external reference to another routine in the interface }
  123. {$i cgenstr.inc}
  124. {$endif}
  125. {$endif FPC_USE_LIBC}
  126. {$ifdef cpui386}
  127. {$ifdef SYSPROCDEFINED}
  128. {$Error Can't determine processor type !}
  129. {$endif}
  130. {$i i386.inc} { Case dependent, don't change }
  131. {$endif cpui386}
  132. {$ifdef cpum68k}
  133. {$ifdef SYSPROCDEFINED}
  134. {$Error Can't determine processor type !}
  135. {$endif}
  136. {$i m68k.inc} { Case dependent, don't change }
  137. {$define SYSPROCDEFINED}
  138. {$endif cpum68k}
  139. {$ifdef cpux86_64}
  140. {$ifdef SYSPROCDEFINED}
  141. {$Error Can't determine processor type !}
  142. {$endif}
  143. {$i x86_64.inc} { Case dependent, don't change }
  144. {$define SYSPROCDEFINED}
  145. {$endif cpux86_64}
  146. {$ifdef cpupowerpc32}
  147. {$ifdef SYSPROCDEFINED}
  148. {$Error Can't determine processor type !}
  149. {$endif}
  150. {$i powerpc.inc} { Case dependent, don't change }
  151. {$define SYSPROCDEFINED}
  152. {$endif cpupowerpc32}
  153. {$ifdef cpupowerpc64}
  154. {$ifdef SYSPROCDEFINED}
  155. {$Error Can't determine processor type !}
  156. {$endif}
  157. {$i powerpc64.inc} { Case dependent, don't change }
  158. {$define SYSPROCDEFINED}
  159. {$endif cpupowerpc64}
  160. {$ifdef cpualpha}
  161. {$ifdef SYSPROCDEFINED}
  162. {$Error Can't determine processor type !}
  163. {$endif}
  164. {$i alpha.inc} { Case dependent, don't change }
  165. {$define SYSPROCDEFINED}
  166. {$endif cpualpha}
  167. {$ifdef cpuiA64}
  168. {$ifdef SYSPROCDEFINED}
  169. {$Error Can't determine processor type !}
  170. {$endif}
  171. {$i ia64.inc} { Case dependent, don't change }
  172. {$define SYSPROCDEFINED}
  173. {$endif cpuiA64}
  174. {$ifdef cpusparc}
  175. {$ifdef SYSPROCDEFINED}
  176. {$Error Can't determine processor type !}
  177. {$endif}
  178. {$i sparc.inc} { Case dependent, don't change }
  179. {$define SYSPROCDEFINED}
  180. {$endif cpusparc}
  181. {$ifdef cpuarm}
  182. {$ifdef SYSPROCDEFINED}
  183. {$Error Can't determine processor type !}
  184. {$endif}
  185. {$if defined(CPUCORTEXM3) or defined(CPUARMV7M)}
  186. {$i thumb2.inc} { Case dependent, don't change }
  187. {$else}
  188. {$i arm.inc} { Case dependent, don't change }
  189. {$endif}
  190. {$define SYSPROCDEFINED}
  191. {$endif cpuarm}
  192. {$ifdef cpuavr}
  193. {$ifdef SYSPROCDEFINED}
  194. {$Error Can't determine processor type !}
  195. {$endif}
  196. {$i avr.inc} { Case dependent, don't change }
  197. {$define SYSPROCDEFINED}
  198. {$endif cpuavr}
  199. {$ifdef cpujvm}
  200. {$ifdef SYSPROCDEFINED}
  201. {$Error Can't determine processor type !}
  202. {$endif}
  203. {$i jvm.inc}
  204. {$define SYSPROCDEFINED}
  205. {$endif cpuavr}
  206. {$ifndef jvm}
  207. procedure fillchar(var x;count : SizeInt;value : boolean);
  208. begin
  209. fillchar(x,count,byte(value));
  210. end;
  211. procedure fillchar(var x;count : SizeInt;value : char);
  212. begin
  213. fillchar(x,count,byte(value));
  214. end;
  215. procedure FillByte (var x;count : SizeInt;value : byte );
  216. begin
  217. FillChar (X,Count,VALUE);
  218. end;
  219. function IndexChar(Const buf;len:SizeInt;b:char):SizeInt;
  220. begin
  221. IndexChar:=IndexByte(Buf,Len,byte(B));
  222. end;
  223. function CompareChar(Const buf1,buf2;len:SizeInt):SizeInt;
  224. begin
  225. CompareChar:=CompareByte(buf1,buf2,len);
  226. end;
  227. {$endif jvm}
  228. { Include generic pascal only routines which are not defined in the processor
  229. specific include file }
  230. {$I generic.inc}
  231. {****************************************************************************
  232. Set Handling
  233. ****************************************************************************}
  234. {$ifndef jvm}
  235. { Include set support which is processor specific}
  236. {$i set.inc}
  237. { Include generic pascal routines for sets if the processor }
  238. { specific routines are not available. }
  239. {$i genset.inc}
  240. {$endif}
  241. {****************************************************************************
  242. Math Routines
  243. ****************************************************************************}
  244. function Hi(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  245. begin
  246. Hi := b shr 4
  247. end;
  248. function Lo(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  249. begin
  250. Lo := b and $0f
  251. end;
  252. Function Swap (X : Word) : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  253. Begin
  254. Swap := SwapEndian(X);
  255. End;
  256. //Function Swap (X : Integer) : Integer;{$ifdef SYSTEMINLINE}inline;{$endif}
  257. //Begin
  258. // Swap := SwapEndian(X);
  259. //End;
  260. Function Swap (X : Longint) : Longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  261. Begin
  262. Swap:=(X and $ffff) shl 16 + (X shr 16)
  263. End;
  264. //Function Swap (X : Cardinal) : Cardinal;{$ifdef SYSTEMINLINE}inline;{$endif}
  265. //Begin
  266. // Swap:=(X and $ffff) shl 16 + (X shr 16)
  267. //End;
  268. //Function Swap (X : QWord) : QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  269. //Begin
  270. // Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  271. //End;
  272. Function swap (X : Int64) : Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  273. Begin
  274. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  275. End;
  276. {$ifdef SUPPORT_DOUBLE}
  277. operator := (b:real48) d:double;{$ifdef SYSTEMINLINE}inline;{$endif}
  278. begin
  279. D:=real2double(b);
  280. end;
  281. {$endif SUPPORT_DOUBLE}
  282. {$ifdef SUPPORT_EXTENDED}
  283. operator := (b:real48) e:extended;{$ifdef SYSTEMINLINE}inline;{$endif}
  284. begin
  285. e:=real2double(b);
  286. end;
  287. {$endif SUPPORT_EXTENDED}
  288. {$ifndef FPUNONE}
  289. {$ifdef FPC_USE_LIBC}
  290. { Include libc versions }
  291. {$i cgenmath.inc}
  292. {$endif FPC_USE_LIBC}
  293. { Include processor specific routines }
  294. {$I math.inc}
  295. { Include generic version }
  296. {$I genmath.inc}
  297. {$endif}
  298. {$i gencurr.inc}
  299. function aligntoptr(p : pointer) : pointer;inline;
  300. begin
  301. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  302. result:=align(p,sizeof(p));
  303. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  304. result:=p;
  305. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  306. end;
  307. {****************************************************************************
  308. Subroutines for String handling
  309. ****************************************************************************}
  310. { Needs to be before RTTI handling }
  311. {$i sstrings.inc}
  312. { requires sstrings.inc for initval }
  313. {$I int64p.inc}
  314. { contains invalid typecasts for the JVM}
  315. {$ifndef jvm}
  316. {$I int64.inc}
  317. {$endif not jvm}
  318. {Requires int64.inc, since that contains the VAL functions for int64 and qword}
  319. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  320. {$i astrings.inc}
  321. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  322. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  323. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  324. {$i wstrings.inc}
  325. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  326. {$i ustrings.inc}
  327. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  328. {$i aliases.inc}
  329. {*****************************************************************************
  330. Dynamic Array support
  331. *****************************************************************************}
  332. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  333. (*
  334. {$i dynarr.inc}
  335. *)
  336. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  337. {*****************************************************************************
  338. Object Pascal support
  339. *****************************************************************************}
  340. {$ifdef FPC_HAS_FEATURE_CLASSES}
  341. {$i objpas.inc}
  342. {$endif FPC_HAS_FEATURE_CLASSES}
  343. {*****************************************************************************
  344. Variant support
  345. *****************************************************************************}
  346. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  347. {$i variant.inc}
  348. {$endif FPC_HAS_FEATURE_VARIANTS}
  349. {****************************************************************************
  350. Run-Time Type Information (RTTI)
  351. ****************************************************************************}
  352. {$ifdef FPC_HAS_FEATURE_RTTI}
  353. {$i rtti.inc}
  354. {$endif FPC_HAS_FEATURE_RTTI}
  355. {$if defined(FPC_HAS_FEATURE_RANDOM)}
  356. {----------------------------------------------------------------------
  357. Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  358. Pseudo-Random Number Generator.
  359. What is Mersenne Twister?
  360. Mersenne Twister(MT) is a pseudorandom number generator developped by
  361. Makoto Matsumoto and Takuji Nishimura (alphabetical order) during
  362. 1996-1997. MT has the following merits:
  363. It is designed with consideration on the flaws of various existing
  364. generators.
  365. Far longer period and far higher order of equidistribution than any
  366. other implemented generators. (It is proved that the period is 2^19937-1,
  367. and 623-dimensional equidistribution property is assured.)
  368. Fast generation. (Although it depends on the system, it is reported that
  369. MT is sometimes faster than the standard ANSI-C library in a system
  370. with pipeline and cache memory.)
  371. Efficient use of the memory. (The implemented C-code mt19937.c
  372. consumes only 624 words of working area.)
  373. home page
  374. http://www.math.keio.ac.jp/~matumoto/emt.html
  375. original c source
  376. http://www.math.keio.ac.jp/~nisimura/random/int/mt19937int.c
  377. Coded by Takuji Nishimura, considering the suggestions by
  378. Topher Cooper and Marc Rieffel in July-Aug. 1997.
  379. This library is free software; you can redistribute it and/or
  380. modify it under the terms of the GNU Library General Public
  381. License as published by the Free Software Foundation; either
  382. version 2 of the License, or (at your option) any later
  383. version.
  384. This library is distributed in the hope that it will be useful,
  385. but WITHOUT ANY WARRANTY; without even the implied warranty of
  386. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  387. See the GNU Library General Public License for more details.
  388. You should have received a copy of the GNU Library General
  389. Public License along with this library; if not, write to the
  390. Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  391. 02111-1307 USA
  392. Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura.
  393. When you use this, send an email to: [email protected]
  394. with an appropriate reference to your work.
  395. REFERENCE
  396. M. Matsumoto and T. Nishimura,
  397. "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  398. Pseudo-Random Number Generator",
  399. ACM Transactions on Modeling and Computer Simulation,
  400. Vol. 8, No. 1, January 1998, pp 3--30.
  401. Translated to OP and Delphi interface added by Roman Krejci (6.12.1999)
  402. http://www.rksolution.cz/delphi/tips.htm
  403. Revised 21.6.2000: Bug in the function RandInt_MT19937 fixed
  404. 2003/10/26: adapted to use the improved intialisation mentioned at
  405. <http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html> and
  406. removed the assembler code
  407. ----------------------------------------------------------------------}
  408. {$R-} {range checking off}
  409. {$Q-} {overflow checking off}
  410. { Period parameter }
  411. Const
  412. MT19937N=624;
  413. Type
  414. tMT19937StateArray = array [0..MT19937N-1] of longint; // the array for the state vector
  415. { Period parameters }
  416. const
  417. MT19937M=397;
  418. MT19937MATRIX_A =$9908b0df; // constant vector a
  419. MT19937UPPER_MASK=longint($80000000); // most significant w-r bits
  420. MT19937LOWER_MASK=longint($7fffffff); // least significant r bits
  421. { Tempering parameters }
  422. TEMPERING_MASK_B=longint($9d2c5680);
  423. TEMPERING_MASK_C=longint($efc60000);
  424. VAR
  425. mt : tMT19937StateArray;
  426. const
  427. mti: longint=MT19937N+1; // mti=MT19937N+1 means mt[] is not initialized
  428. { Initializing the array with a seed }
  429. procedure sgenrand_MT19937(seed: longint);
  430. var
  431. i: longint;
  432. begin
  433. mt[0] := seed;
  434. for i := 1 to MT19937N-1 do
  435. begin
  436. mt[i] := 1812433253 * (mt[i-1] xor (mt[i-1] shr 30)) + i;
  437. { See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. }
  438. { In the previous versions, MSBs of the seed affect }
  439. { only MSBs of the array mt[]. }
  440. { 2002/01/09 modified by Makoto Matsumoto }
  441. end;
  442. mti := MT19937N;
  443. end;
  444. function genrand_MT19937: longint;
  445. const
  446. mag01 : array [0..1] of longint =(0, longint(MT19937MATRIX_A));
  447. var
  448. y: longint;
  449. kk: longint;
  450. begin
  451. if RandSeed<>OldRandSeed then
  452. mti:=MT19937N+1;
  453. if (mti >= MT19937N) { generate MT19937N longints at one time }
  454. then begin
  455. if mti = (MT19937N+1) then // if sgenrand_MT19937() has not been called,
  456. begin
  457. sgenrand_MT19937(randseed); // default initial seed is used
  458. { hack: randseed is not used more than once in this algorithm. Most }
  459. { user changes are re-initialising reandseed with the value it had }
  460. { at the start -> with the "not", we will detect this change. }
  461. { Detecting other changes is not useful, since the generated }
  462. { numbers will be different anyway. }
  463. randseed := not(randseed);
  464. oldrandseed := randseed;
  465. end;
  466. for kk:=0 to MT19937N-MT19937M-1 do begin
  467. y := (mt[kk] and MT19937UPPER_MASK) or (mt[kk+1] and MT19937LOWER_MASK);
  468. mt[kk] := mt[kk+MT19937M] xor (y shr 1) xor mag01[y and $00000001];
  469. end;
  470. for kk:= MT19937N-MT19937M to MT19937N-2 do begin
  471. y := (mt[kk] and MT19937UPPER_MASK) or (mt[kk+1] and MT19937LOWER_MASK);
  472. mt[kk] := mt[kk+(MT19937M-MT19937N)] xor (y shr 1) xor mag01[y and $00000001];
  473. end;
  474. y := (mt[MT19937N-1] and MT19937UPPER_MASK) or (mt[0] and MT19937LOWER_MASK);
  475. mt[MT19937N-1] := mt[MT19937M-1] xor (y shr 1) xor mag01[y and $00000001];
  476. mti := 0;
  477. end;
  478. y := mt[mti]; inc(mti);
  479. y := y xor (y shr 11);
  480. y := y xor (y shl 7) and TEMPERING_MASK_B;
  481. y := y xor (y shl 15) and TEMPERING_MASK_C;
  482. y := y xor (y shr 18);
  483. Result := y;
  484. end;
  485. function random(l:longint): longint;
  486. begin
  487. { otherwise we can return values = l (JM) }
  488. if (l < 0) then
  489. inc(l);
  490. random := longint((int64(cardinal(genrand_MT19937))*l) shr 32);
  491. end;
  492. function random(l:int64): int64;
  493. begin
  494. { always call random, so the random generator cycles (TP-compatible) (JM) }
  495. random := int64((qword(cardinal(genrand_MT19937)) or ((qword(cardinal(genrand_MT19937)) shl 32))) and $7fffffffffffffff);
  496. if (l<>0) then
  497. random := random mod l
  498. else
  499. random := 0;
  500. end;
  501. {$ifndef FPUNONE}
  502. function random: extended;
  503. begin
  504. random := cardinal(genrand_MT19937) * (extended(1.0)/(int64(1) shl 32));
  505. end;
  506. {$endif}
  507. {$endif FPC_HAS_FEATURE_RANDOM}
  508. {****************************************************************************
  509. Memory Management
  510. ****************************************************************************}
  511. (*
  512. Function Ptr(sel,off : Longint) : farpointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  513. Begin
  514. ptr:=farpointer((sel shl 4)+off);
  515. End;
  516. Function CSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  517. Begin
  518. Cseg:=0;
  519. End;
  520. Function DSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  521. Begin
  522. Dseg:=0;
  523. End;
  524. Function SSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  525. Begin
  526. Sseg:=0;
  527. End;
  528. *)
  529. {$ifopt R+}
  530. {$define RangeCheckWasOn}
  531. {$R-}
  532. {$endif opt R+}
  533. {$ifopt I+}
  534. {$define IOCheckWasOn}
  535. {$I-}
  536. {$endif opt I+}
  537. {$ifopt Q+}
  538. {$define OverflowCheckWasOn}
  539. {$Q-}
  540. {$endif opt Q+}
  541. {*****************************************************************************
  542. Miscellaneous
  543. *****************************************************************************}
  544. procedure fpc_rangeerror;[public,alias:'FPC_RANGEERROR']; compilerproc;
  545. begin
  546. HandleErrorFrame(201,get_frame);
  547. end;
  548. procedure fpc_divbyzero;[public,alias:'FPC_DIVBYZERO']; compilerproc;
  549. begin
  550. HandleErrorFrame(200,get_frame);
  551. end;
  552. procedure fpc_overflow;[public,alias:'FPC_OVERFLOW']; compilerproc;
  553. begin
  554. HandleErrorFrame(215,get_frame);
  555. end;
  556. procedure fpc_threaderror; [public,alias:'FPC_THREADERROR'];
  557. begin
  558. HandleErrorFrame(6,get_frame);
  559. end;
  560. (*
  561. procedure fpc_iocheck;[public,alias:'FPC_IOCHECK']; compilerproc;
  562. var
  563. l : longint;
  564. HInoutRes : PWord;
  565. begin
  566. HInOutRes:=@InoutRes;
  567. if HInOutRes^<>0 then
  568. begin
  569. l:=HInOutRes^;
  570. HInOutRes^:=0;
  571. HandleErrorFrame(l,get_frame);
  572. end;
  573. end;
  574. Function IOResult:Word;
  575. var
  576. HInoutRes : PWord;
  577. Begin
  578. HInoutRes:=@InoutRes;
  579. IOResult:=HInOutRes^;
  580. HInOutRes^:=0;
  581. End;
  582. Function GetThreadID:TThreadID;{$ifdef SYSTEMINLINE}inline;{$endif}
  583. begin
  584. (* ThreadID is stored in a threadvar and made available in interface *)
  585. (* to allow setup of this value during thread initialization. *)
  586. GetThreadID := ThreadID;
  587. end;
  588. function fpc_safecallcheck(res : hresult) : hresult;[public,alias:'FPC_SAFECALLCHECK']; compilerproc; {$ifdef CPU86} register; {$endif}
  589. begin
  590. if res<0 then
  591. begin
  592. if assigned(SafeCallErrorProc) then
  593. SafeCallErrorProc(res,get_frame);
  594. HandleErrorFrame(229,get_frame);
  595. end;
  596. result:=res;
  597. end;
  598. *)
  599. {*****************************************************************************
  600. Stack check code
  601. *****************************************************************************}
  602. { be compatible with old code }
  603. {$ifdef FPC_NO_GENERIC_STACK_CHECK}
  604. {$define NO_GENERIC_STACK_CHECK}
  605. {$endif FPC_NO_GENERIC_STACK_CHECK}
  606. {$IFNDEF NO_GENERIC_STACK_CHECK}
  607. {$IFOPT S+}
  608. {$DEFINE STACKCHECK}
  609. {$ENDIF}
  610. {$S-}
  611. procedure fpc_stackcheck(stack_size:SizeUInt);[public,alias:'FPC_STACKCHECK'];
  612. var
  613. c : Pointer;
  614. begin
  615. { Avoid recursive calls when called from the exit routines }
  616. if StackError then
  617. exit;
  618. { don't use sack_size, since the stack pointer has already been
  619. decreased when this routine is called
  620. }
  621. c := Sptr - STACK_MARGIN;
  622. if (c <= StackBottom) then
  623. begin
  624. StackError:=true;
  625. HandleError(202);
  626. end;
  627. end;
  628. {$IFDEF STACKCHECK}
  629. {$S+}
  630. {$ENDIF}
  631. {$UNDEF STACKCHECK}
  632. {$ENDIF NO_GENERIC_STACK_CHECK}
  633. {*****************************************************************************
  634. Initialization / Finalization
  635. *****************************************************************************}
  636. (*
  637. const
  638. maxunits=1024; { See also files.pas of the compiler source }
  639. type
  640. TInitFinalRec=record
  641. InitProc,
  642. FinalProc : TProcedure;
  643. end;
  644. TInitFinalTable = record
  645. TableCount,
  646. InitCount : longint;
  647. Procs : array[1..maxunits] of TInitFinalRec;
  648. end;
  649. PInitFinalTable = ^TInitFinalTable;
  650. {$ifndef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  651. var
  652. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  653. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  654. procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; compilerproc;
  655. var
  656. i : longint;
  657. begin
  658. { call cpu/fpu initialisation routine }
  659. fpc_cpuinit;
  660. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  661. with PInitFinalTable(EntryInformation.InitFinalTable)^ do
  662. {$else FPC_HAS_INDIRECT_MAIN_INFORMATION}
  663. with InitFinalTable do
  664. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  665. begin
  666. for i:=1 to TableCount do
  667. begin
  668. if assigned(Procs[i].InitProc) then
  669. Procs[i].InitProc();
  670. InitCount:=i;
  671. end;
  672. end;
  673. if assigned(InitProc) then
  674. TProcedure(InitProc)();
  675. end;
  676. procedure internal_initializeunits; external name 'FPC_INITIALIZEUNITS';
  677. procedure fpc_LibInitializeUnits;[public,alias:'FPC_LIBINITIALIZEUNITS'];
  678. begin
  679. IsLibrary:=true;
  680. { must also be set to true for packages when implemented }
  681. ModuleIsLib:=true;
  682. internal_initializeunits;
  683. end;
  684. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  685. begin
  686. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  687. with PInitFinalTable(EntryInformation.InitFinalTable)^ do
  688. {$else FPC_HAS_INDIRECT_MAIN_INFORMATION}
  689. with InitFinalTable do
  690. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  691. begin
  692. while (InitCount>0) do
  693. begin
  694. // we've to decrement the cound before calling the final. code
  695. // else a halt in the final. code leads to a endless loop
  696. dec(InitCount);
  697. if assigned(Procs[InitCount+1].FinalProc) then
  698. Procs[InitCount+1].FinalProc();
  699. end;
  700. end;
  701. end;
  702. *)
  703. {*****************************************************************************
  704. Error / Exit / ExitProc
  705. *****************************************************************************}
  706. Procedure system_exit;forward;
  707. {$ifdef FPC_HAS_FEATURE_HEAP}
  708. {$ifndef HAS_MEMORYMANAGER}
  709. //not needed if independant memory manager
  710. Procedure FinalizeHeap;forward;
  711. {$endif HAS_MEMORYMANAGER}
  712. {$endif FPC_HAS_FEATURE_HEAP}
  713. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  714. procedure SysFlushStdIO;
  715. var
  716. pstdout : ^Text;
  717. begin
  718. { Show runtime error and exit }
  719. pstdout:=@stdout;
  720. If erroraddr<>nil Then
  721. Begin
  722. Writeln(pstdout^,'Runtime error ',Errorcode,' at $',hexstr(erroraddr));
  723. { to get a nice symify }
  724. Writeln(pstdout^,BackTraceStrFunc(Erroraddr));
  725. dump_stack(pstdout^,ErrorBase);
  726. Writeln(pstdout^,'');
  727. End;
  728. { Make sure that all output is written to the redirected file }
  729. if Textrec(Output).Mode=fmOutput then
  730. Flush(Output);
  731. if Textrec(ErrOutput).Mode=fmOutput then
  732. Flush(ErrOutput);
  733. if Textrec(pstdout^).Mode=fmOutput then
  734. Flush(pstdout^);
  735. if Textrec(StdErr).Mode=fmOutput then
  736. Flush(StdErr);
  737. end;
  738. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  739. Procedure InternalExit;
  740. (*
  741. var
  742. current_exit : Procedure;
  743. {$if defined(MSWINDOWS) or defined(OS2)}
  744. i : longint;
  745. {$endif}
  746. *)
  747. Begin
  748. (*
  749. {$ifdef SYSTEMDEBUG}
  750. writeln('InternalExit');
  751. {$endif SYSTEMDEBUG}
  752. while exitProc<>nil Do
  753. Begin
  754. InOutRes:=0;
  755. current_exit:=tProcedure(exitProc);
  756. exitProc:=nil;
  757. current_exit();
  758. End;
  759. { Finalize units }
  760. FinalizeUnits;
  761. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  762. SysFlushStdIO;
  763. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  764. {$if defined(MSWINDOWS) or defined(OS2)}
  765. { finally release the heap if possible, especially
  766. important for DLLs.
  767. Reset the array to nil, and finally also argv itself to
  768. avoid double freeing problem in case this function gets called twice. }
  769. if assigned(argv) then
  770. begin
  771. for i:=0 to argc-1 do
  772. if assigned(argv[i]) then
  773. begin
  774. sysfreemem(argv[i]);
  775. argv[i]:=nil;
  776. end;
  777. sysfreemem(argv);
  778. argv:=nil;
  779. end;
  780. {$endif}
  781. {$ifdef LINUX}
  782. {sysfreemem already checks for nil}
  783. sysfreemem(calculated_cmdline);
  784. {$endif}
  785. {$ifdef BSD}
  786. sysfreemem(cmdline);
  787. {$endif}
  788. {$ifdef FPC_HAS_FEATURE_HEAP}
  789. {$ifndef HAS_MEMORYMANAGER}
  790. FinalizeHeap;
  791. {$endif HAS_MEMORYMANAGER}
  792. {$endif FPC_HAS_FEATURE_HEAP}
  793. *)
  794. End;
  795. Procedure do_exit;[Public,Alias:'FPC_DO_EXIT'];
  796. begin
  797. InternalExit;
  798. System_exit;
  799. end;
  800. Procedure lib_exit;[Public,Alias:'FPC_LIB_EXIT'];
  801. begin
  802. InternalExit;
  803. end;
  804. Procedure Halt(ErrNum: Longint);
  805. Begin
  806. ExitCode:=Errnum;
  807. Do_Exit;
  808. end;
  809. (*
  810. function SysBackTraceStr (Addr: Pointer): ShortString;
  811. begin
  812. SysBackTraceStr:=' $'+hexstr(addr);
  813. end;
  814. *)
  815. Procedure HandleErrorAddrFrame (Errno : longint;addr,frame : Pointer);[public,alias:'FPC_BREAK_ERROR']; {$ifdef CPU86} register; {$endif}
  816. begin
  817. If pointer(ErrorProc)<>Nil then
  818. ErrorProc(Errno,addr,frame);
  819. raise FpcRunTimeError.Create(Errno);
  820. (*
  821. errorcode:=word(Errno);
  822. erroraddr:=addr;
  823. errorbase:=frame;
  824. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  825. if ExceptAddrStack <> nil then
  826. raise TObject(nil) at addr,frame;
  827. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  828. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  829. {$ifdef FPC_LIMITED_EXITCODE}
  830. if errorcode > maxExitCode then
  831. halt(255)
  832. else
  833. {$endif FPC_LIMITED_EXITCODE}
  834. halt(errorcode);
  835. {$else FPC_HAS_FEATURE_EXITCODE}
  836. halt;
  837. {$endif FPC_HAS_FEATURE_EXITCODE}
  838. *)
  839. end;
  840. Procedure HandleErrorFrame (Errno : longint;frame : Pointer);
  841. {
  842. Procedure to handle internal errors, i.e. not user-invoked errors
  843. Internal function should ALWAYS call HandleError instead of RunError.
  844. Can be used for exception handlers to specify the frame
  845. }
  846. begin
  847. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  848. end;
  849. Procedure HandleError (Errno : longint);[public,alias : 'FPC_HANDLEERROR'];
  850. {
  851. Procedure to handle internal errors, i.e. not user-invoked errors
  852. Internal function should ALWAYS call HandleError instead of RunError.
  853. }
  854. begin
  855. HandleErrorFrame(Errno,get_frame);
  856. end;
  857. procedure RunError(w : word);[alias: 'FPC_RUNERROR'];
  858. begin
  859. errorcode:=w;
  860. (*
  861. erroraddr:=get_caller_addr(get_frame);
  862. errorbase:=get_caller_frame(get_frame);
  863. *)
  864. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  865. {$ifdef FPC_LIMITED_EXITCODE}
  866. if errorcode > maxExitCode then
  867. halt(255)
  868. else
  869. {$endif FPC_LIMITED_EXITCODE}
  870. halt(errorcode);
  871. {$else FPC_HAS_FEATURE_EXITCODE}
  872. halt;
  873. {$endif FPC_HAS_FEATURE_EXITCODE}
  874. end;
  875. Procedure RunError;{$ifdef SYSTEMINLINE}inline;{$endif}
  876. Begin
  877. RunError (0);
  878. End;
  879. Procedure Halt;{$ifdef SYSTEMINLINE}inline;{$endif}
  880. Begin
  881. Halt(0);
  882. End;
  883. Procedure Error(RunTimeError : TRunTimeError);
  884. begin
  885. RunError(RuntimeErrorExitCodes[RunTimeError]);
  886. end;
  887. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  888. Procedure dump_stack(var f : text;bp : Pointer);
  889. var
  890. i : Longint;
  891. prevbp : Pointer;
  892. is_dev : boolean;
  893. caller_frame,
  894. caller_addr : Pointer;
  895. Begin
  896. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  897. try
  898. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  899. prevbp:=bp-1;
  900. i:=0;
  901. is_dev:=do_isdevice(textrec(f).Handle);
  902. while bp > prevbp Do
  903. Begin
  904. caller_addr := get_caller_addr(bp);
  905. caller_frame := get_caller_frame(bp);
  906. if (caller_addr=nil) then
  907. break;
  908. Writeln(f,BackTraceStrFunc(caller_addr));
  909. if (caller_frame=nil) then
  910. break;
  911. Inc(i);
  912. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  913. break;
  914. prevbp:=bp;
  915. bp:=caller_frame;
  916. End;
  917. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  918. except
  919. { prevent endless dump if an exception occured }
  920. end;
  921. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  922. End;
  923. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  924. procedure DumpExceptionBackTrace(var f:text);
  925. var
  926. FrameNumber,
  927. FrameCount : longint;
  928. Frames : PPointer;
  929. begin
  930. if RaiseList=nil then
  931. exit;
  932. WriteLn(f,BackTraceStrFunc(RaiseList^.Addr));
  933. FrameCount:=RaiseList^.Framecount;
  934. Frames:=RaiseList^.Frames;
  935. for FrameNumber := 0 to FrameCount-1 do
  936. WriteLn(f,BackTraceStrFunc(Frames[FrameNumber]));
  937. end;
  938. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  939. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  940. {$ifdef FPC_HAS_FEATURE_HEAP}
  941. Type
  942. PExitProcInfo = ^TExitProcInfo;
  943. TExitProcInfo = Record
  944. Next : PExitProcInfo;
  945. SaveExit : Pointer;
  946. Proc : TProcedure;
  947. End;
  948. const
  949. ExitProcList: PExitProcInfo = nil;
  950. Procedure DoExitProc;
  951. var
  952. P : PExitProcInfo;
  953. Proc : TProcedure;
  954. Begin
  955. P:=ExitProcList;
  956. ExitProcList:=P^.Next;
  957. ExitProc:=P^.SaveExit;
  958. Proc:=P^.Proc;
  959. DisPose(P);
  960. Proc();
  961. End;
  962. Procedure AddExitProc(Proc: TProcedure);
  963. var
  964. P : PExitProcInfo;
  965. Begin
  966. New(P);
  967. P^.Next:=ExitProcList;
  968. P^.SaveExit:=ExitProc;
  969. P^.Proc:=Proc;
  970. ExitProcList:=P;
  971. ExitProc:=@DoExitProc;
  972. End;
  973. {$endif FPC_HAS_FEATURE_HEAP}
  974. {$ifdef FPC_HAS_FEATURE_HEAP}
  975. function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):ppchar; // const ?
  976. // Extra allocate reserveentries pchar's at the beginning (default param=0 after 1.0.x ?)
  977. // Note: for internal use by skilled programmers only
  978. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  979. var p : ppchar;
  980. i : LongInt;
  981. begin
  982. if High(s)<Low(s) Then Exit(NIL);
  983. Getmem(p,sizeof(pchar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  984. // for cmd
  985. if p=nil then
  986. begin
  987. {$ifdef xunix}
  988. fpseterrno(ESysEnomem);
  989. {$endif}
  990. exit(NIL);
  991. end;
  992. for i:=low(s) to high(s) do
  993. p[i+Reserveentries]:=pchar(s[i]);
  994. p[high(s)+1+Reserveentries]:=nil;
  995. ArrayStringToPPchar:=p;
  996. end;
  997. Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):ppchar;
  998. {
  999. Create a PPChar to structure of pchars which are the arguments specified
  1000. in the string S. Especially useful for creating an ArgV for Exec-calls
  1001. }
  1002. begin
  1003. StringToPPChar:=StringToPPChar(PChar(S),ReserveEntries);
  1004. end;
  1005. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  1006. var
  1007. i,nr : longint;
  1008. Buf : ^char;
  1009. p : ppchar;
  1010. begin
  1011. buf:=s;
  1012. nr:=1;
  1013. while (buf^<>#0) do // count nr of args
  1014. begin
  1015. while (buf^ in [' ',#9,#10]) do // Kill separators.
  1016. inc(buf);
  1017. inc(nr);
  1018. if buf^='"' Then // quotes argument?
  1019. begin
  1020. inc(buf);
  1021. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1022. inc(buf);
  1023. if buf^='"' then // skip closing quote.
  1024. inc(buf);
  1025. end
  1026. else
  1027. begin // else std
  1028. while not (buf^ in [' ',#0,#9,#10]) do
  1029. inc(buf);
  1030. end;
  1031. end;
  1032. getmem(p,(ReserveEntries+nr)*sizeof(pchar));
  1033. StringToPPChar:=p;
  1034. if p=nil then
  1035. begin
  1036. {$ifdef xunix}
  1037. fpseterrno(ESysEnomem);
  1038. {$endif}
  1039. exit;
  1040. end;
  1041. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  1042. buf:=s;
  1043. while (buf^<>#0) do
  1044. begin
  1045. while (buf^ in [' ',#9,#10]) do // Kill separators.
  1046. begin
  1047. buf^:=#0;
  1048. inc(buf);
  1049. end;
  1050. if buf^='"' Then // quotes argument?
  1051. begin
  1052. inc(buf);
  1053. p^:=buf;
  1054. inc(p);
  1055. p^:=nil;
  1056. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1057. inc(buf);
  1058. if buf^='"' then // skip closing quote.
  1059. begin
  1060. buf^:=#0;
  1061. inc(buf);
  1062. end;
  1063. end
  1064. else
  1065. begin
  1066. p^:=buf;
  1067. inc(p);
  1068. p^:=nil;
  1069. while not (buf^ in [' ',#0,#9,#10]) do
  1070. inc(buf);
  1071. end;
  1072. end;
  1073. end;
  1074. {$endif FPC_HAS_FEATURE_HEAP}
  1075. {*****************************************************************************
  1076. Abstract/Assert support.
  1077. *****************************************************************************}
  1078. procedure fpc_AbstractErrorIntern;compilerproc;[public,alias : 'FPC_ABSTRACTERROR'];
  1079. begin
  1080. (*
  1081. If pointer(AbstractErrorProc)<>nil then
  1082. AbstractErrorProc();
  1083. *)
  1084. HandleErrorFrame(211,get_frame);
  1085. end;
  1086. Procedure fpc_assert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer); [Public,Alias : 'FPC_ASSERT']; compilerproc;
  1087. begin
  1088. (*
  1089. if pointer(AssertErrorProc)<>nil then
  1090. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  1091. else
  1092. *)
  1093. HandleErrorFrame(227,get_frame);
  1094. end;
  1095. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
  1096. begin
  1097. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1098. If msg='' then
  1099. write(stderr,'Assertion failed')
  1100. else
  1101. write(stderr,msg);
  1102. Writeln(stderr,' (',FName,', line ',LineNo,').');
  1103. Writeln(stderr,'');
  1104. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  1105. Halt(227);
  1106. {$else FPC_HAS_FEATURE_EXITCODE}
  1107. halt;
  1108. {$endif FPC_HAS_FEATURE_EXITCODE}
  1109. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1110. end;
  1111. {*****************************************************************************
  1112. SetJmp/LongJmp support.
  1113. *****************************************************************************}
  1114. {$i setjump.inc}
  1115. {$ifdef IOCheckWasOn}
  1116. {$I+}
  1117. {$endif}
  1118. {$ifdef RangeCheckWasOn}
  1119. {$R+}
  1120. {$endif}
  1121. {$ifdef OverflowCheckWasOn}
  1122. {$Q+}
  1123. {$endif}
  1124. {*****************************************************************************
  1125. Heap
  1126. *****************************************************************************}
  1127. {$ifdef FPC_HAS_FEATURE_HEAP}
  1128. {$i sysheap.inc}
  1129. {$i heap.inc}
  1130. {$endif FPC_HAS_FEATURE_HEAP}
  1131. {*****************************************************************************
  1132. Thread support
  1133. *****************************************************************************}
  1134. {$ifdef FPC_HAS_FEATURE_THREADING}
  1135. { Generic threadmanager }
  1136. {$i thread.inc}
  1137. { Generic threadvar support }
  1138. {$i threadvr.inc}
  1139. {$ifdef DISABLE_NO_THREAD_MANAGER}
  1140. { OS Dependent implementation }
  1141. {$i systhrd.inc}
  1142. {$endif DISABLE_NO_THREAD_MANAGER}
  1143. {$endif FPC_HAS_FEATURE_THREADING}
  1144. {*****************************************************************************
  1145. File Handling
  1146. *****************************************************************************}
  1147. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1148. { Allow slash and backslash as separators }
  1149. procedure DoDirSeparators(p:Pchar);
  1150. var
  1151. i : longint;
  1152. begin
  1153. for i:=0 to strlen(p) do
  1154. if p[i] in AllowDirectorySeparators then
  1155. p[i]:=DirectorySeparator;
  1156. end;
  1157. procedure DoDirSeparators(var p:shortstring);
  1158. var
  1159. i : longint;
  1160. begin
  1161. for i:=1 to length(p) do
  1162. if p[i] in AllowDirectorySeparators then
  1163. p[i]:=DirectorySeparator;
  1164. end;
  1165. {$endif FPC_HAS_FEATURE_FILEIO}
  1166. { OS dependent low level file functions }
  1167. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1168. {$i sysfile.inc}
  1169. {$endif FPC_HAS_FEATURE_FILEIO}
  1170. { Text file }
  1171. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  1172. {$i text.inc}
  1173. {$endif FPC_HAS_FEATURE_TEXTIO}
  1174. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1175. { Untyped file }
  1176. {$i file.inc}
  1177. { Typed file }
  1178. {$i typefile.inc}
  1179. {$endif FPC_HAS_FEATURE_FILEIO}
  1180. {*****************************************************************************
  1181. Directory Handling
  1182. *****************************************************************************}
  1183. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1184. { OS dependent dir functions }
  1185. {$i sysdir.inc}
  1186. {$endif FPC_HAS_FEATURE_FILEIO}
  1187. {$if defined(FPC_HAS_FEATURE_FILEIO) and defined(FPC_HAS_FEATURE_ANSISTRINGS)}
  1188. Procedure getdir(drivenr:byte;Var dir:ansistring);
  1189. { this is needed to also allow ansistrings, the shortstring version is
  1190. OS dependent }
  1191. var
  1192. s : shortstring;
  1193. begin
  1194. getdir(drivenr,s);
  1195. dir:=s;
  1196. end;
  1197. {$endif}
  1198. {$if defined(FPC_HAS_FEATURE_FILEIO)}
  1199. Procedure MkDir(Const s: String);
  1200. Var
  1201. Buffer: Array[0..255] of Char;
  1202. Begin
  1203. If (s='') or (InOutRes <> 0) then
  1204. exit;
  1205. Move(s[1], Buffer, Length(s));
  1206. Buffer[Length(s)] := #0;
  1207. MkDir(@buffer[0],length(s));
  1208. End;
  1209. Procedure RmDir(Const s: String);
  1210. Var
  1211. Buffer: Array[0..255] of Char;
  1212. Begin
  1213. If (s='') or (InOutRes <> 0) then
  1214. exit;
  1215. Move(s[1], Buffer, Length(s));
  1216. Buffer[Length(s)] := #0;
  1217. RmDir(@buffer[0],length(s));
  1218. End;
  1219. Procedure ChDir(Const s: String);
  1220. Var
  1221. Buffer: Array[0..255] of Char;
  1222. Begin
  1223. If (s='') or (InOutRes <> 0) then
  1224. exit;
  1225. Move(s[1], Buffer, Length(s));
  1226. Buffer[Length(s)] := #0;
  1227. ChDir(@buffer[0],length(s));
  1228. End;
  1229. {$endif}
  1230. {*****************************************************************************
  1231. Resources support
  1232. *****************************************************************************}
  1233. {$i sysres.inc}
  1234. (*
  1235. const
  1236. CtrlBreakHandler: TCtrlBreakHandler = nil;
  1237. {$IFNDEF FPC_HAS_SETCTRLBREAKHANDLER}
  1238. (* It is possible to provide platform specific implementation performing *)
  1239. (* special initialization; default implementation just sets the procedural *)
  1240. (* variable to make it available for use from the exception handler. *)
  1241. function SysSetCtrlBreakHandler (Handler: TCtrlBreakHandler): TCtrlBreakHandler;
  1242. begin
  1243. (* Return either nil or previous handler *)
  1244. SysSetCtrlBreakHandler := CtrlBreakHandler;
  1245. CtrlBreakHandler := Handler;
  1246. end;
  1247. {$ENDIF FPC_HAS_SETCTRLBREAKHANDLER}
  1248. *)