system.inc 40 KB

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