jsystem.inc 39 KB

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