system.inc 38 KB

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