2
0

system.inc 41 KB

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