system.inc 40 KB

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