system.inc 41 KB

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