system.inc 38 KB

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