system.inc 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  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. Function CSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  538. Begin
  539. Cseg:=0;
  540. End;
  541. Function DSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  542. Begin
  543. Dseg:=0;
  544. End;
  545. Function SSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  546. Begin
  547. Sseg:=0;
  548. End;
  549. {$push}
  550. {$R-}
  551. {$I-}
  552. {$Q-}
  553. {*****************************************************************************
  554. Miscellaneous
  555. *****************************************************************************}
  556. {$ifndef FPC_SYSTEM_HAS_GET_PC_ADDR}
  557. { This provides a dummy implementation
  558. of get_pc_addr function, for CPU's that don't need
  559. the instruction address to walk the stack. }
  560. function get_pc_addr : pointer;
  561. begin
  562. get_pc_addr:=nil;
  563. end;
  564. {$endif ndef FPC_SYSTEM_HAS_GET_PC_ADDR}
  565. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_STACKINFO}
  566. { This provides a simple implementation
  567. of get_caller_stackinfo procedure,
  568. using get_caller_addr and get_caller_frame
  569. functions. }
  570. procedure get_caller_stackinfo(var framebp,addr : pointer);
  571. var
  572. nextbp,nextaddr : pointer;
  573. begin
  574. nextbp:=get_caller_frame(framebp,addr);
  575. nextaddr:=get_caller_addr(framebp,addr);
  576. framebp:=nextbp;
  577. addr:=nextaddr;
  578. end;
  579. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_STACKINFO}
  580. procedure fpc_rangeerror;[public,alias:'FPC_RANGEERROR']; compilerproc;
  581. begin
  582. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  583. end;
  584. procedure fpc_divbyzero;[public,alias:'FPC_DIVBYZERO']; compilerproc;
  585. begin
  586. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  587. end;
  588. procedure fpc_overflow;[public,alias:'FPC_OVERFLOW']; compilerproc;
  589. begin
  590. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  591. end;
  592. procedure fpc_threaderror; [public,alias:'FPC_THREADERROR'];
  593. begin
  594. HandleErrorAddrFrameInd(6,get_pc_addr,get_frame);
  595. end;
  596. procedure fpc_invalidpointer; [public,alias:'FPC_INVALIDPOINTER'];
  597. begin
  598. HandleErrorAddrFrameInd(216,get_pc_addr,get_frame);
  599. end;
  600. procedure fpc_iocheck;[public,alias:'FPC_IOCHECK']; compilerproc;
  601. var
  602. l : longint;
  603. HInoutRes : PWord;
  604. begin
  605. HInOutRes:=@InoutRes;
  606. if HInOutRes^<>0 then
  607. begin
  608. l:=HInOutRes^;
  609. HInOutRes^:=0;
  610. HandleErrorAddrFrameInd(l,get_pc_addr,get_frame);
  611. end;
  612. end;
  613. Function IOResult:Word;
  614. var
  615. HInoutRes : PWord;
  616. Begin
  617. HInoutRes:=@InoutRes;
  618. IOResult:=HInOutRes^;
  619. HInOutRes^:=0;
  620. End;
  621. Function GetThreadID:TThreadID;{$ifdef SYSTEMINLINE}inline;{$endif}
  622. begin
  623. (* ThreadID is stored in a threadvar and made available in interface *)
  624. (* to allow setup of this value during thread initialization. *)
  625. GetThreadID := ThreadID;
  626. end;
  627. function fpc_safecallcheck(res : hresult) : hresult;[public,alias:'FPC_SAFECALLCHECK']; compilerproc; {$ifdef CPU86} register; {$endif}
  628. begin
  629. if res<0 then
  630. begin
  631. if assigned(SafeCallErrorProc) then
  632. SafeCallErrorProc(res,get_frame);
  633. HandleErrorAddrFrameInd(229,get_pc_addr,get_frame);
  634. end;
  635. result:=res;
  636. end;
  637. {*****************************************************************************
  638. Stack check code
  639. *****************************************************************************}
  640. { be compatible with old code }
  641. {$ifdef FPC_NO_GENERIC_STACK_CHECK}
  642. {$define NO_GENERIC_STACK_CHECK}
  643. {$endif FPC_NO_GENERIC_STACK_CHECK}
  644. {$IFNDEF NO_GENERIC_STACK_CHECK}
  645. {$PUSH}
  646. {$S-}
  647. procedure fpc_stackcheck(stack_size:SizeUInt);[public,alias:'FPC_STACKCHECK']; compilerproc;
  648. var
  649. c : Pointer;
  650. begin
  651. { Avoid recursive calls when called from the exit routines }
  652. if StackError then
  653. exit;
  654. { don't use sack_size, since the stack pointer has already been
  655. decreased when this routine is called
  656. }
  657. c := Sptr - STACK_MARGIN;
  658. if (c <= StackBottom) then
  659. begin
  660. StackError:=true;
  661. HandleError(202);
  662. end;
  663. end;
  664. {$POP}
  665. {$ENDIF NO_GENERIC_STACK_CHECK}
  666. {*****************************************************************************
  667. Initialization / Finalization
  668. *****************************************************************************}
  669. const
  670. maxunits=1024; { See also files.pas of the compiler source }
  671. type
  672. TInitFinalRec=record
  673. InitProc,
  674. FinalProc : TProcedure;
  675. end;
  676. TInitFinalTable = record
  677. TableCount,
  678. InitCount : longint;
  679. Procs : array[1..maxunits] of TInitFinalRec;
  680. end;
  681. PInitFinalTable = ^TInitFinalTable;
  682. {$ifndef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  683. var
  684. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  685. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  686. procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; compilerproc;
  687. var
  688. i : longint;
  689. {$ifdef DEBUG}
  690. pt : PInitFinalTable;
  691. {$endif}
  692. begin
  693. { call cpu/fpu initialisation routine }
  694. fpc_cpuinit;
  695. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  696. {$ifdef DEBUG}
  697. pt := PInitFinalTable(EntryInformation.InitFinalTable);
  698. {$endif}
  699. with PInitFinalTable(EntryInformation.InitFinalTable)^ do
  700. {$else FPC_HAS_INDIRECT_MAIN_INFORMATION}
  701. {$ifdef DEBUG}
  702. pt := @InitFinalTable;
  703. {$endif}
  704. with InitFinalTable do
  705. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  706. begin
  707. for i:=1 to TableCount do
  708. begin
  709. if assigned(Procs[i].InitProc) then
  710. Procs[i].InitProc();
  711. InitCount:=i;
  712. end;
  713. end;
  714. if assigned(InitProc) then
  715. TProcedure(InitProc)();
  716. end;
  717. procedure internal_initializeunits; external name 'FPC_INITIALIZEUNITS';
  718. procedure fpc_LibInitializeUnits;[public,alias:'FPC_LIBINITIALIZEUNITS'];
  719. begin
  720. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  721. IsLibrary:=true;
  722. { must also be set to true for packages when implemented }
  723. ModuleIsLib:=true;
  724. internal_initializeunits;
  725. {$endif FPC_HAS_FEATURE_DYNLIBS}
  726. end;
  727. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  728. begin
  729. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  730. with PInitFinalTable(EntryInformation.InitFinalTable)^ do
  731. {$else FPC_HAS_INDIRECT_MAIN_INFORMATION}
  732. with InitFinalTable do
  733. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  734. begin
  735. while (InitCount>0) do
  736. begin
  737. // we've to decrement the cound before calling the final. code
  738. // else a halt in the final. code leads to a endless loop
  739. dec(InitCount);
  740. if assigned(Procs[InitCount+1].FinalProc) then
  741. Procs[InitCount+1].FinalProc();
  742. end;
  743. end;
  744. end;
  745. {*****************************************************************************
  746. Error / Exit / ExitProc
  747. *****************************************************************************}
  748. Procedure system_exit;forward;
  749. {$ifdef FPC_HAS_FEATURE_HEAP}
  750. {$ifndef HAS_MEMORYMANAGER}
  751. //not needed if independant memory manager
  752. Procedure FinalizeHeap;forward;
  753. {$endif HAS_MEMORYMANAGER}
  754. {$endif FPC_HAS_FEATURE_HEAP}
  755. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  756. procedure SysFlushStdIO;
  757. begin
  758. { Make sure that all output is written to the redirected file }
  759. if Textrec(Output).Mode=fmOutput then
  760. Flush(Output);
  761. if Textrec(ErrOutput).Mode=fmOutput then
  762. Flush(ErrOutput);
  763. if Textrec(stdout).Mode=fmOutput then
  764. Flush(stdout);
  765. if Textrec(StdErr).Mode=fmOutput then
  766. Flush(StdErr);
  767. end;
  768. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  769. Procedure InternalExit;
  770. var
  771. current_exit : Procedure;
  772. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  773. pstdout : ^Text;
  774. {$endif}
  775. {$if defined(MSWINDOWS) or defined(OS2)}
  776. i : longint;
  777. {$endif}
  778. Begin
  779. {$ifdef SYSTEMDEBUG}
  780. writeln('InternalExit');
  781. {$endif SYSTEMDEBUG}
  782. while exitProc<>nil Do
  783. Begin
  784. InOutRes:=0;
  785. current_exit:=tProcedure(exitProc);
  786. exitProc:=nil;
  787. current_exit();
  788. End;
  789. { Finalize units }
  790. FinalizeUnits;
  791. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  792. { the embedded system unit itself contains no routines for console i/o
  793. console i/o is done by the Consoleio unit which can do things like
  794. redirection to seriell i/o }
  795. {$ifndef EMBEDDED}
  796. { Show runtime error and exit }
  797. pstdout:=@stdout;
  798. If erroraddr<>nil Then
  799. Begin
  800. Writeln(pstdout^,'Runtime error ',Errorcode,' at $',hexstr(erroraddr));
  801. { to get a nice symify }
  802. Writeln(pstdout^,BackTraceStrFunc(Erroraddr));
  803. dump_stack(pstdout^,ErrorBase,ErrorAddr);
  804. Writeln(pstdout^,'');
  805. End;
  806. SysFlushStdIO;
  807. {$endif EMBEDDED}
  808. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  809. {$if defined(MSWINDOWS) or defined(OS2)}
  810. { finally release the heap if possible, especially
  811. important for DLLs.
  812. Reset the array to nil, and finally also argv itself to
  813. avoid double freeing problem in case this function gets called twice. }
  814. if assigned(argv) then
  815. begin
  816. for i:=0 to argc-1 do
  817. if assigned(argv[i]) then
  818. begin
  819. sysfreemem(argv[i]);
  820. argv[i]:=nil;
  821. end;
  822. sysfreemem(argv);
  823. argv:=nil;
  824. end;
  825. {$endif}
  826. {$ifdef LINUX}
  827. {sysfreemem already checks for nil}
  828. sysfreemem(calculated_cmdline);
  829. {$endif}
  830. {$ifdef BSD}
  831. sysfreemem(cmdline);
  832. {$endif}
  833. {$ifdef FPC_HAS_FEATURE_HEAP}
  834. {$ifndef HAS_MEMORYMANAGER}
  835. FinalizeHeap;
  836. {$endif HAS_MEMORYMANAGER}
  837. {$endif FPC_HAS_FEATURE_HEAP}
  838. End;
  839. Procedure do_exit;[Public,Alias:'FPC_DO_EXIT'];
  840. begin
  841. InternalExit;
  842. System_exit;
  843. end;
  844. Procedure lib_exit;[Public,Alias:'FPC_LIB_EXIT'];
  845. begin
  846. InternalExit;
  847. end;
  848. Procedure Halt(ErrNum: Longint);
  849. Begin
  850. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  851. {$ifdef FPC_LIMITED_EXITCODE}
  852. if ErrNum > maxExitCode then
  853. ExitCode:=255
  854. else
  855. {$endif FPC_LIMITED_EXITCODE}
  856. ExitCode:=ErrNum;
  857. {$endif FPC_HAS_FEATURE_EXITCODE}
  858. Do_Exit;
  859. end;
  860. function SysBackTraceStr (Addr: Pointer): ShortString;
  861. begin
  862. SysBackTraceStr:=' $'+hexstr(addr);
  863. end;
  864. Procedure HandleErrorAddrFrame (Errno : longint;addr,frame : Pointer);[public,alias:'FPC_BREAK_ERROR']; {$ifdef CPU86} register; {$endif}
  865. begin
  866. If pointer(ErrorProc)<>Nil then
  867. ErrorProc(Errno,addr,frame);
  868. errorcode:=word(Errno);
  869. erroraddr:=addr;
  870. errorbase:=frame;
  871. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  872. if ExceptAddrStack <> nil then
  873. raise TObject(nil) at addr,frame;
  874. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  875. Halt(errorcode);
  876. end;
  877. { This is used internally by system skip first level,
  878. and generated the same output as before, when
  879. HandleErrorFrame function was used internally. }
  880. Procedure HandleErrorAddrFrameInd (Errno : longint;addr,frame : Pointer);
  881. begin
  882. get_caller_stackinfo (frame, addr);
  883. HandleErrorAddrFrame (Errno,addr,frame);
  884. end;
  885. Procedure HandleErrorFrame (Errno : longint;frame : Pointer);
  886. {
  887. Procedure to handle internal errors, i.e. not user-invoked errors
  888. Internal function should ALWAYS call HandleError instead of RunError.
  889. Can be used for exception handlers to specify the frame
  890. }
  891. begin
  892. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  893. end;
  894. procedure fpc_handleerror (Errno : longint); compilerproc; [public,alias : 'FPC_HANDLEERROR'];
  895. {
  896. Procedure to handle internal errors, i.e. not user-invoked errors
  897. Internal function should ALWAYS call HandleError instead of RunError.
  898. }
  899. begin
  900. HandleErrorAddrFrameInd(Errno,get_pc_addr,get_frame);
  901. end;
  902. procedure RunError(w : word);[alias: 'FPC_RUNERROR'];
  903. var
  904. bp,pcaddr : pointer;
  905. begin
  906. errorcode:=w;
  907. pcaddr:=get_pc_addr;
  908. bp:=get_frame;
  909. get_caller_stackinfo(bp,pcaddr);
  910. erroraddr:=pcaddr;
  911. errorbase:=bp;
  912. Halt(errorcode);
  913. end;
  914. Procedure RunError;{$ifdef SYSTEMINLINE}inline;{$endif}
  915. Begin
  916. RunError (0);
  917. End;
  918. Procedure Halt;{$ifdef SYSTEMINLINE}inline;{$endif}
  919. Begin
  920. Halt(0);
  921. End;
  922. Procedure Error(RunTimeError : TRunTimeError);
  923. begin
  924. RunError(RuntimeErrorExitCodes[RunTimeError]);
  925. end;
  926. Procedure dump_stack(var f : text;bp,addr : Pointer);
  927. var
  928. i : Longint;
  929. prevbp : Pointer;
  930. prevaddr : pointer;
  931. is_dev : boolean;
  932. caller_frame,
  933. caller_addr : Pointer;
  934. Begin
  935. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  936. try
  937. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  938. prevbp:=bp-1;
  939. prevaddr:=nil;
  940. i:=0;
  941. is_dev:=do_isdevice(textrec(f).Handle);
  942. while bp > prevbp Do
  943. Begin
  944. caller_addr := get_caller_addr(bp,addr);
  945. caller_frame := get_caller_frame(bp,addr);
  946. if (caller_addr=nil) then
  947. break;
  948. Writeln(f,BackTraceStrFunc(caller_addr));
  949. if (caller_frame=nil) then
  950. break;
  951. Inc(i);
  952. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  953. break;
  954. prevbp:=bp;
  955. prevaddr:=addr;
  956. bp:=caller_frame;
  957. addr:=caller_addr;
  958. End;
  959. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  960. except
  961. { prevent endless dump if an exception occured }
  962. end;
  963. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  964. End;
  965. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  966. procedure DumpExceptionBackTrace(var f:text);
  967. var
  968. FrameNumber,
  969. FrameCount : longint;
  970. Frames : PPointer;
  971. begin
  972. if RaiseList=nil then
  973. exit;
  974. WriteLn(f,BackTraceStrFunc(RaiseList^.Addr));
  975. FrameCount:=RaiseList^.Framecount;
  976. Frames:=RaiseList^.Frames;
  977. for FrameNumber := 0 to FrameCount-1 do
  978. WriteLn(f,BackTraceStrFunc(Frames[FrameNumber]));
  979. end;
  980. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  981. {$ifdef FPC_HAS_FEATURE_HEAP}
  982. Type
  983. PExitProcInfo = ^TExitProcInfo;
  984. TExitProcInfo = Record
  985. Next : PExitProcInfo;
  986. SaveExit : Pointer;
  987. Proc : TProcedure;
  988. End;
  989. const
  990. ExitProcList: PExitProcInfo = nil;
  991. Procedure DoExitProc;
  992. var
  993. P : PExitProcInfo;
  994. Proc : TProcedure;
  995. Begin
  996. P:=ExitProcList;
  997. ExitProcList:=P^.Next;
  998. ExitProc:=P^.SaveExit;
  999. Proc:=P^.Proc;
  1000. DisPose(P);
  1001. Proc();
  1002. End;
  1003. Procedure AddExitProc(Proc: TProcedure);
  1004. var
  1005. P : PExitProcInfo;
  1006. Begin
  1007. New(P);
  1008. P^.Next:=ExitProcList;
  1009. P^.SaveExit:=ExitProc;
  1010. P^.Proc:=Proc;
  1011. ExitProcList:=P;
  1012. ExitProc:=@DoExitProc;
  1013. End;
  1014. {$endif FPC_HAS_FEATURE_HEAP}
  1015. {$ifdef FPC_HAS_FEATURE_HEAP}
  1016. function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):ppchar; // const ?
  1017. // Extra allocate reserveentries pchar's at the beginning (default param=0 after 1.0.x ?)
  1018. // Note: for internal use by skilled programmers only
  1019. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  1020. var p : ppchar;
  1021. i : LongInt;
  1022. begin
  1023. if High(s)<Low(s) Then Exit(NIL);
  1024. Getmem(p,sizeof(pchar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  1025. // for cmd
  1026. if p=nil then
  1027. begin
  1028. {$ifdef xunix}
  1029. fpseterrno(ESysEnomem);
  1030. {$endif}
  1031. exit(NIL);
  1032. end;
  1033. for i:=low(s) to high(s) do
  1034. p[i+Reserveentries]:=pchar(s[i]);
  1035. p[high(s)+1+Reserveentries]:=nil;
  1036. ArrayStringToPPchar:=p;
  1037. end;
  1038. Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):ppchar;
  1039. {
  1040. Create a PPChar to structure of pchars which are the arguments specified
  1041. in the string S. Especially useful for creating an ArgV for Exec-calls
  1042. }
  1043. begin
  1044. StringToPPChar:=StringToPPChar(PChar(S),ReserveEntries);
  1045. end;
  1046. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  1047. var
  1048. i,nr : longint;
  1049. Buf : ^char;
  1050. p : ppchar;
  1051. begin
  1052. buf:=s;
  1053. nr:=1;
  1054. while (buf^<>#0) do // count nr of args
  1055. begin
  1056. while (buf^ in [' ',#9,#10]) do // Kill separators.
  1057. inc(buf);
  1058. inc(nr);
  1059. if buf^='"' Then // quotes argument?
  1060. begin
  1061. inc(buf);
  1062. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1063. inc(buf);
  1064. if buf^='"' then // skip closing quote.
  1065. inc(buf);
  1066. end
  1067. else
  1068. begin // else std
  1069. while not (buf^ in [' ',#0,#9,#10]) do
  1070. inc(buf);
  1071. end;
  1072. end;
  1073. getmem(p,(ReserveEntries+nr)*sizeof(pchar));
  1074. StringToPPChar:=p;
  1075. if p=nil then
  1076. begin
  1077. {$ifdef xunix}
  1078. fpseterrno(ESysEnomem);
  1079. {$endif}
  1080. exit;
  1081. end;
  1082. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  1083. buf:=s;
  1084. while (buf^<>#0) do
  1085. begin
  1086. while (buf^ in [' ',#9,#10]) do // Kill separators.
  1087. begin
  1088. buf^:=#0;
  1089. inc(buf);
  1090. end;
  1091. if buf^='"' Then // quotes argument?
  1092. begin
  1093. inc(buf);
  1094. p^:=buf;
  1095. inc(p);
  1096. p^:=nil;
  1097. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1098. inc(buf);
  1099. if buf^='"' then // skip closing quote.
  1100. begin
  1101. buf^:=#0;
  1102. inc(buf);
  1103. end;
  1104. end
  1105. else
  1106. begin
  1107. p^:=buf;
  1108. inc(p);
  1109. p^:=nil;
  1110. while not (buf^ in [' ',#0,#9,#10]) do
  1111. inc(buf);
  1112. end;
  1113. end;
  1114. end;
  1115. {$endif FPC_HAS_FEATURE_HEAP}
  1116. {*****************************************************************************
  1117. Abstract/Assert support.
  1118. *****************************************************************************}
  1119. procedure fpc_emptymethod;[public,alias : 'FPC_EMPTYMETHOD'];
  1120. begin
  1121. end;
  1122. procedure fpc_AbstractErrorIntern;compilerproc;[public,alias : 'FPC_ABSTRACTERROR'];
  1123. begin
  1124. If pointer(AbstractErrorProc)<>nil then
  1125. AbstractErrorProc();
  1126. HandleErrorAddrFrameInd(211,get_pc_addr,get_frame);
  1127. end;
  1128. Procedure fpc_assert(Const Msg,FName:Shortstring;LineNo:Longint;
  1129. ErrorAddr:Pointer); [Public,Alias : 'FPC_ASSERT']; compilerproc;
  1130. begin
  1131. if pointer(AssertErrorProc)<>nil then
  1132. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  1133. else
  1134. HandleErrorAddrFrameInd(227,get_pc_addr,get_frame);
  1135. end;
  1136. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
  1137. begin
  1138. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1139. If msg='' then
  1140. write(stderr,'Assertion failed')
  1141. else
  1142. write(stderr,msg);
  1143. Writeln(stderr,' (',FName,', line ',LineNo,').');
  1144. Writeln(stderr,'');
  1145. Halt(227);
  1146. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1147. end;
  1148. {*****************************************************************************
  1149. SetJmp/LongJmp support.
  1150. *****************************************************************************}
  1151. {$i setjump.inc}
  1152. {$pop} //{$I-,R-,Q-} before 'procedure fpc_rangeerror'
  1153. {*****************************************************************************
  1154. Heap
  1155. *****************************************************************************}
  1156. {$i sysheap.inc}
  1157. {$i heap.inc}
  1158. {*****************************************************************************
  1159. Thread support
  1160. *****************************************************************************}
  1161. {$ifdef FPC_HAS_FEATURE_THREADING}
  1162. { Generic threadmanager }
  1163. {$i thread.inc}
  1164. { Generic threadvar support }
  1165. {$i threadvr.inc}
  1166. {$ifdef DISABLE_NO_THREAD_MANAGER}
  1167. { OS Dependent implementation }
  1168. {$i systhrd.inc}
  1169. {$endif DISABLE_NO_THREAD_MANAGER}
  1170. {$endif FPC_HAS_FEATURE_THREADING}
  1171. {*****************************************************************************
  1172. File Handling
  1173. *****************************************************************************}
  1174. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1175. { Allow slash and backslash as separators }
  1176. procedure DoDirSeparators(p:Pchar);
  1177. var
  1178. i : longint;
  1179. begin
  1180. for i:=0 to strlen(p) do
  1181. if p[i] in AllowDirectorySeparators then
  1182. p[i]:=DirectorySeparator;
  1183. end;
  1184. procedure DoDirSeparators(var p:shortstring);
  1185. var
  1186. i : longint;
  1187. begin
  1188. for i:=1 to length(p) do
  1189. if p[i] in AllowDirectorySeparators then
  1190. p[i]:=DirectorySeparator;
  1191. end;
  1192. {$endif FPC_HAS_FEATURE_FILEIO}
  1193. { OS dependent low level file functions }
  1194. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1195. {$i sysfile.inc}
  1196. {$endif FPC_HAS_FEATURE_FILEIO}
  1197. { Text file }
  1198. {$i text.inc}
  1199. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1200. { Untyped file }
  1201. {$i file.inc}
  1202. { Typed file }
  1203. {$i typefile.inc}
  1204. {$endif FPC_HAS_FEATURE_FILEIO}
  1205. {*****************************************************************************
  1206. Directory Handling
  1207. *****************************************************************************}
  1208. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1209. { OS dependent dir functions }
  1210. {$i sysdir.inc}
  1211. {$endif FPC_HAS_FEATURE_FILEIO}
  1212. {$if defined(FPC_HAS_FEATURE_FILEIO) and defined(FPC_HAS_FEATURE_ANSISTRINGS)}
  1213. Procedure getdir(drivenr:byte;Var dir:ansistring);
  1214. { this is needed to also allow ansistrings, the shortstring version is
  1215. OS dependent }
  1216. var
  1217. s : shortstring;
  1218. begin
  1219. getdir(drivenr,s);
  1220. dir:=s;
  1221. end;
  1222. {$endif}
  1223. {$if defined(FPC_HAS_FEATURE_FILEIO)}
  1224. Procedure MkDir(Const s: String);
  1225. Var
  1226. Buffer: Array[0..255] of Char;
  1227. Begin
  1228. If (s='') or (InOutRes <> 0) then
  1229. exit;
  1230. Move(s[1], Buffer, Length(s));
  1231. Buffer[Length(s)] := #0;
  1232. MkDir(@buffer[0],length(s));
  1233. End;
  1234. Procedure RmDir(Const s: String);
  1235. Var
  1236. Buffer: Array[0..255] of Char;
  1237. Begin
  1238. If (s='') or (InOutRes <> 0) then
  1239. exit;
  1240. Move(s[1], Buffer, Length(s));
  1241. Buffer[Length(s)] := #0;
  1242. RmDir(@buffer[0],length(s));
  1243. End;
  1244. Procedure ChDir(Const s: String);
  1245. Var
  1246. Buffer: Array[0..255] of Char;
  1247. Begin
  1248. If (s='') or (InOutRes <> 0) then
  1249. exit;
  1250. Move(s[1], Buffer, Length(s));
  1251. Buffer[Length(s)] := #0;
  1252. ChDir(@buffer[0],length(s));
  1253. End;
  1254. {$endif}
  1255. {*****************************************************************************
  1256. Resources support
  1257. *****************************************************************************}
  1258. {$i sysres.inc}
  1259. const
  1260. CtrlBreakHandler: TCtrlBreakHandler = nil;
  1261. {$IFNDEF FPC_HAS_SETCTRLBREAKHANDLER}
  1262. (* It is possible to provide platform specific implementation performing *)
  1263. (* special initialization; default implementation just sets the procedural *)
  1264. (* variable to make it available for use from the exception handler. *)
  1265. function SysSetCtrlBreakHandler (Handler: TCtrlBreakHandler): TCtrlBreakHandler;
  1266. begin
  1267. (* Return either nil or previous handler *)
  1268. SysSetCtrlBreakHandler := CtrlBreakHandler;
  1269. CtrlBreakHandler := Handler;
  1270. end;
  1271. {$ENDIF FPC_HAS_SETCTRLBREAKHANDLER}