system.inc 38 KB

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