jsystem.inc 41 KB

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