system.inc 41 KB

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