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