system.inc 41 KB

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