jsystem.inc 41 KB

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