jsystem.inc 39 KB

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