jsystem.inc 39 KB

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