jsystem.inc 39 KB

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