system.inc 41 KB

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