jsystem.inc 41 KB

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