system.inc 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931
  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. { contains the definition of the TExceptAddr type, which is required
  11. by the compiler to generate code for any routine containing
  12. implicit or explicit exceptions }
  13. {$i excepth.inc}
  14. { ObjpasInt is the integer type, equivalent to Objpas.Integer (the Integer
  15. type in ObjFpc and Delphi modes). It is defined here for use in the
  16. implementation part of the System unit. }
  17. {$ifdef CPU16}
  18. type
  19. ObjpasInt = SmallInt;
  20. {$else CPU16}
  21. type
  22. ObjpasInt = LongInt;
  23. {$endif CPU16}
  24. { The RTTI is implemented through a series of constants : }
  25. Const
  26. // please update tkManagedTypes below if you add new
  27. // values
  28. tkUnknown = 0;
  29. tkInteger = 1;
  30. tkChar = 2;
  31. tkEnumeration = 3;
  32. tkFloat = 4;
  33. tkSet = 5;
  34. tkMethod = 6;
  35. tkSString = 7;
  36. tkString = tkSString;
  37. tkLString = 8;
  38. tkAString = 9;
  39. tkWString = 10;
  40. tkVariant = 11;
  41. tkArray = 12;
  42. tkRecord = 13;
  43. tkInterface = 14;
  44. tkClass = 15;
  45. tkObject = 16;
  46. tkWChar = 17;
  47. tkBool = 18;
  48. tkInt64 = 19;
  49. tkQWord = 20;
  50. tkDynArray = 21;
  51. tkInterfaceCorba = 22;
  52. tkProcVar = 23;
  53. tkUString = 24;
  54. tkHelper = 26;
  55. tkFile = 27;
  56. tkClassRef = 28;
  57. tkPointer = 29;
  58. // all potentially managed types
  59. tkManagedTypes = [tkAstring,tkWstring,tkUstring,tkArray,
  60. tkObject,tkRecord,tkDynArray,tkInterface,tkVariant];
  61. {****************************************************************************
  62. Local types
  63. ****************************************************************************}
  64. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  65. {$ifdef FPC_OBJFPC_EXTENDED_IF}
  66. {$if High(errorcode)<>maxExitCode}
  67. {$define FPC_LIMITED_EXITCODE}
  68. {$endif}
  69. {$else}
  70. {$define FPC_LIMITED_EXITCODE}
  71. {$endif FPC_OBJFPC_EXTENDED_IF}
  72. {$endif FPC_HAS_FEATURE_EXITCODE}
  73. Procedure HandleError (Errno : Longint); external name 'FPC_HANDLEERROR';
  74. Procedure HandleErrorFrame (Errno : longint;frame : Pointer); forward;
  75. Procedure HandleErrorAddrFrame (Errno : longint;addr : CodePointer; frame : Pointer); forward;
  76. Procedure HandleErrorAddrFrameInd (Errno : longint;addr : CodePointer; frame : Pointer); forward;
  77. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  78. type
  79. FileFunc = Procedure(var t : TextRec);
  80. {$endif FPC_HAS_FEATURE_TEXTIO}
  81. const
  82. STACK_MARGIN = 16384; { Stack size margin for stack checking }
  83. { Random / Randomize constants }
  84. OldRandSeed : Cardinal = 0;
  85. { For Error Handling.}
  86. ErrorBase : Pointer = nil;public name 'FPC_ERRORBASE';
  87. { Used by the ansi/widestrings and maybe also other things in the future }
  88. var
  89. { widechar, because also used by widestring -> pwidechar conversions }
  90. emptychar : widechar;public name 'FPC_EMPTYCHAR';
  91. {$ifndef FPC_NO_GENERIC_STACK_CHECK}
  92. { if the OS does the stack checking, we don't need any stklen from the
  93. main program }
  94. initialstklen : SizeUint;external name '__stklen';
  95. {$endif FPC_NO_GENERIC_STACK_CHECK}
  96. { checks whether the given suggested size for the stack of the current
  97. thread is acceptable. If this is the case, returns it unaltered.
  98. Otherwise it should return an acceptable value.
  99. Operating systems that automatically expand their stack on demand, should
  100. simply return a very large value.
  101. Operating systems which do not have a possibility to retrieve stack size
  102. information, should simply return the given stklen value (This is the default
  103. implementation).
  104. }
  105. {$ifdef FPC_HAS_FEATURE_STACKCHECK}
  106. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt; forward;
  107. {$endif FPC_HAS_FEATURE_STACKCHECK}
  108. {*****************************************************************************
  109. OS dependent Helpers/Syscalls
  110. *****************************************************************************}
  111. { for some OSes do_isdevice is defined in sysos.inc, but for others (win32)
  112. it isn't, and is used before the actual definition is encountered }
  113. {$ifdef FPC_HAS_FEATURE_FILEIO}
  114. function do_isdevice(handle:thandle):boolean;forward;
  115. {$endif FPC_HAS_FEATURE_FILEIO}
  116. {$i sysos.inc}
  117. {****************************************************************************
  118. Include processor specific routines
  119. ****************************************************************************}
  120. {$ifdef FPC_USE_LIBC}
  121. { Under Haiku, bcopy cause a problem when searching for include file
  122. in the compiler. So, we use the internal implementation for now
  123. under BeOS and Haiku. }
  124. {$ifndef BEOS}
  125. { prefer libc implementations over our own, as they're most likely faster }
  126. {$i cgeneric.inc}
  127. { is now declared as external reference to another routine in the interface }
  128. {$i cgenstr.inc}
  129. {$endif}
  130. {$endif FPC_USE_LIBC}
  131. {$ifdef cpui386}
  132. {$ifdef SYSPROCDEFINED}
  133. {$Error Can't determine processor type !}
  134. {$endif}
  135. {$i i386.inc} { Case dependent, don't change }
  136. {$define SYSPROCDEFINED}
  137. {$endif cpui386}
  138. {$ifdef cpui8086}
  139. {$ifdef SYSPROCDEFINED}
  140. {$Error Can't determine processor type !}
  141. {$endif}
  142. {$i i8086.inc} { Case dependent, don't change }
  143. {$define SYSPROCDEFINED}
  144. {$endif cpui8086}
  145. {$ifdef cpum68k}
  146. {$ifdef SYSPROCDEFINED}
  147. {$Error Can't determine processor type !}
  148. {$endif}
  149. {$i m68k.inc} { Case dependent, don't change }
  150. {$define SYSPROCDEFINED}
  151. {$endif cpum68k}
  152. {$ifdef cpux86_64}
  153. {$ifdef SYSPROCDEFINED}
  154. {$Error Can't determine processor type !}
  155. {$endif}
  156. {$i x86_64.inc} { Case dependent, don't change }
  157. {$define SYSPROCDEFINED}
  158. {$endif cpux86_64}
  159. {$ifdef cpupowerpc32}
  160. {$ifdef SYSPROCDEFINED}
  161. {$Error Can't determine processor type !}
  162. {$endif}
  163. {$i powerpc.inc} { Case dependent, don't change }
  164. {$define SYSPROCDEFINED}
  165. {$endif cpupowerpc32}
  166. {$ifdef cpupowerpc64}
  167. {$ifdef SYSPROCDEFINED}
  168. {$Error Can't determine processor type !}
  169. {$endif}
  170. {$i powerpc64.inc} { Case dependent, don't change }
  171. {$define SYSPROCDEFINED}
  172. {$endif cpupowerpc64}
  173. {$ifdef cpualpha}
  174. {$ifdef SYSPROCDEFINED}
  175. {$Error Can't determine processor type !}
  176. {$endif}
  177. {$i alpha.inc} { Case dependent, don't change }
  178. {$define SYSPROCDEFINED}
  179. {$endif cpualpha}
  180. {$ifdef cpuiA64}
  181. {$ifdef SYSPROCDEFINED}
  182. {$Error Can't determine processor type !}
  183. {$endif}
  184. {$i ia64.inc} { Case dependent, don't change }
  185. {$define SYSPROCDEFINED}
  186. {$endif cpuiA64}
  187. {$ifdef cpusparc}
  188. {$ifdef SYSPROCDEFINED}
  189. {$Error Can't determine processor type !}
  190. {$endif}
  191. {$i sparc.inc} { Case dependent, don't change }
  192. {$define SYSPROCDEFINED}
  193. {$endif cpusparc}
  194. {$ifdef cpuarm}
  195. {$ifdef SYSPROCDEFINED}
  196. {$Error Can't determine processor type !}
  197. {$endif}
  198. {$i armdefines.inc}
  199. {$if defined(CPUTHUMB2)}
  200. {$i thumb2.inc} { Case dependent, don't change }
  201. {$else}
  202. {$if defined(CPUTHUMB)}
  203. {$i thumb.inc} { Case dependent, don't change }
  204. {$else}
  205. {$i arm.inc} { Case dependent, don't change }
  206. {$endif}
  207. {$endif}
  208. {$define SYSPROCDEFINED}
  209. {$endif cpuarm}
  210. {$ifdef cpuavr}
  211. {$ifdef SYSPROCDEFINED}
  212. {$Error Can't determine processor type !}
  213. {$endif}
  214. {$i avr.inc} { Case dependent, don't change }
  215. {$define SYSPROCDEFINED}
  216. {$endif cpuavr}
  217. {$ifdef cpumipsel}
  218. {$ifdef SYSPROCDEFINED}
  219. {$Error Can't determine processor type !}
  220. {$endif}
  221. { there is no mipsel.inc, we use mips.inc instead }
  222. {$i mips.inc} { Case dependent, don't change }
  223. {$define SYSPROCDEFINED}
  224. {$else not cpumipsel}
  225. {$ifdef cpumips}
  226. {$ifdef SYSPROCDEFINED}
  227. {$Error Can't determine processor type !}
  228. {$endif}
  229. {$i mips.inc} { Case dependent, don't change }
  230. {$define SYSPROCDEFINED}
  231. {$endif cpumips}
  232. {$endif not cpumipsel}
  233. {$ifdef cpuaarch64}
  234. {$ifdef SYSPROCDEFINED}
  235. {$Error Can't determine processor type !}
  236. {$endif}
  237. {$i aarch64.inc} { Case dependent, don't change }
  238. {$define SYSPROCDEFINED}
  239. {$endif cpuaarch64}
  240. {$ifndef SYSPROCDEFINED}
  241. {$Error Can't determine processor type !}
  242. {$endif}
  243. procedure fillchar(var x;count : SizeInt;value : boolean);
  244. begin
  245. fillchar(x,count,byte(value));
  246. end;
  247. procedure fillchar(var x;count : SizeInt;value : char);
  248. begin
  249. fillchar(x,count,byte(value));
  250. end;
  251. procedure FillByte (var x;count : SizeInt;value : byte );
  252. begin
  253. FillChar (X,Count,VALUE);
  254. end;
  255. function IndexChar(Const buf;len:SizeInt;b:char):SizeInt;
  256. begin
  257. IndexChar:=IndexByte(Buf,Len,byte(B));
  258. end;
  259. function CompareChar(Const buf1,buf2;len:SizeInt):SizeInt;
  260. begin
  261. CompareChar:=CompareByte(buf1,buf2,len);
  262. end;
  263. procedure fpc_zeromem(p:pointer;len:ptruint);
  264. begin
  265. FillChar(p^,len,0);
  266. end;
  267. procedure fpc_fillmem(out data;len:ptruint;b : byte);
  268. begin
  269. FillByte(data,len,b);
  270. end;
  271. { Include generic pascal only routines which are not defined in the processor
  272. specific include file }
  273. {$I generic.inc}
  274. {****************************************************************************
  275. Set Handling
  276. ****************************************************************************}
  277. { Include set support which is processor specific}
  278. {$i set.inc}
  279. { Include generic pascal routines for sets if the processor }
  280. { specific routines are not available. }
  281. {$i genset.inc}
  282. {****************************************************************************
  283. Math Routines
  284. ****************************************************************************}
  285. function Hi(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  286. begin
  287. Hi := b shr 4
  288. end;
  289. function Lo(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  290. begin
  291. Lo := b and $0f
  292. end;
  293. Function Swap (X : Word) : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  294. Begin
  295. Swap := SwapEndian(X);
  296. End;
  297. Function Swap (X : Integer) : Integer;{$ifdef SYSTEMINLINE}inline;{$endif}
  298. Begin
  299. Swap := SwapEndian(X);
  300. End;
  301. Function Swap (X : Longint) : Longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  302. Begin
  303. Swap:=(X shl 16) + (X shr 16);
  304. End;
  305. Function Swap (X : Cardinal) : Cardinal;{$ifdef SYSTEMINLINE}inline;{$endif}
  306. Begin
  307. Swap:=(X shl 16) + (X shr 16);
  308. End;
  309. Function Swap (X : QWord) : QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  310. Begin
  311. Swap:=(X shl 32) + (X shr 32);
  312. End;
  313. Function Swap (X : Int64) : Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  314. Begin
  315. Swap:=(X shl 32) + (X shr 32);
  316. End;
  317. {$ifdef SUPPORT_DOUBLE}
  318. operator := (b:real48) d:double;{$ifdef SYSTEMINLINE}inline;{$endif}
  319. begin
  320. D:=real2double(b);
  321. end;
  322. {$endif SUPPORT_DOUBLE}
  323. {$ifdef SUPPORT_EXTENDED}
  324. operator := (b:real48) e:extended;{$ifdef SYSTEMINLINE}inline;{$endif}
  325. begin
  326. e:=real2double(b);
  327. end;
  328. {$endif SUPPORT_EXTENDED}
  329. {$ifndef FPUNONE}
  330. {$ifdef FPC_USE_LIBC}
  331. { Include libc versions }
  332. {$i cgenmath.inc}
  333. {$endif FPC_USE_LIBC}
  334. { Include processor specific routines }
  335. {$I math.inc}
  336. { Include generic version }
  337. {$I genmath.inc}
  338. {$endif}
  339. {$i gencurr.inc}
  340. function aligntoptr(p : pointer) : pointer;inline;
  341. begin
  342. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  343. result:=align(p,sizeof(p));
  344. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  345. result:=p;
  346. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  347. end;
  348. {****************************************************************************
  349. Subroutines for String handling
  350. ****************************************************************************}
  351. { Needs to be before RTTI handling }
  352. {$i sstrings.inc}
  353. { requires sstrings.inc for initval }
  354. {$I int64p.inc}
  355. {$I int64.inc}
  356. {Requires int64.inc, since that contains the VAL functions for int64 and qword}
  357. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  358. {$i astrings.inc}
  359. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  360. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  361. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  362. {$i wstrings.inc}
  363. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  364. {$i ustrings.inc}
  365. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  366. {$i aliases.inc}
  367. {*****************************************************************************
  368. Dynamic Array support
  369. *****************************************************************************}
  370. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  371. {$i dynarr.inc}
  372. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  373. {*****************************************************************************
  374. Object Pascal support
  375. *****************************************************************************}
  376. {$ifdef FPC_HAS_FEATURE_CLASSES}
  377. {$i objpas.inc}
  378. {$endif FPC_HAS_FEATURE_CLASSES}
  379. {*****************************************************************************
  380. Variant support
  381. *****************************************************************************}
  382. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  383. {$i variant.inc}
  384. {$endif FPC_HAS_FEATURE_VARIANTS}
  385. {****************************************************************************
  386. Run-Time Type Information (RTTI)
  387. ****************************************************************************}
  388. {$ifdef FPC_HAS_FEATURE_RTTI}
  389. {$i rtti.inc}
  390. {$endif FPC_HAS_FEATURE_RTTI}
  391. {$if defined(FPC_HAS_FEATURE_RANDOM)}
  392. {----------------------------------------------------------------------
  393. Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  394. Pseudo-Random Number Generator.
  395. What is Mersenne Twister?
  396. Mersenne Twister(MT) is a pseudorandom number generator developped by
  397. Makoto Matsumoto and Takuji Nishimura (alphabetical order) during
  398. 1996-1997. MT has the following merits:
  399. It is designed with consideration on the flaws of various existing
  400. generators.
  401. Far longer period and far higher order of equidistribution than any
  402. other implemented generators. (It is proved that the period is 2^19937-1,
  403. and 623-dimensional equidistribution property is assured.)
  404. Fast generation. (Although it depends on the system, it is reported that
  405. MT is sometimes faster than the standard ANSI-C library in a system
  406. with pipeline and cache memory.)
  407. Efficient use of the memory. (The implemented C-code mt19937.c
  408. consumes only 624 words of working area.)
  409. home page
  410. http://www.math.keio.ac.jp/~matumoto/emt.html
  411. original c source
  412. http://www.math.keio.ac.jp/~nisimura/random/int/mt19937int.c
  413. Coded by Takuji Nishimura, considering the suggestions by
  414. Topher Cooper and Marc Rieffel in July-Aug. 1997.
  415. This library is free software; you can redistribute it and/or
  416. modify it under the terms of the GNU Library General Public
  417. License as published by the Free Software Foundation; either
  418. version 2 of the License, or (at your option) any later
  419. version.
  420. This library is distributed in the hope that it will be useful,
  421. but WITHOUT ANY WARRANTY; without even the implied warranty of
  422. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  423. See the GNU Library General Public License for more details.
  424. You should have received a copy of the GNU Library General
  425. Public License along with this library; if not, write to the
  426. Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  427. 02111-1307 USA
  428. Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura.
  429. When you use this, send an email to: [email protected]
  430. with an appropriate reference to your work.
  431. REFERENCE
  432. M. Matsumoto and T. Nishimura,
  433. "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  434. Pseudo-Random Number Generator",
  435. ACM Transactions on Modeling and Computer Simulation,
  436. Vol. 8, No. 1, January 1998, pp 3--30.
  437. Translated to OP and Delphi interface added by Roman Krejci (6.12.1999)
  438. http://www.rksolution.cz/delphi/tips.htm
  439. Revised 21.6.2000: Bug in the function RandInt_MT19937 fixed
  440. 2003/10/26: adapted to use the improved intialisation mentioned at
  441. <http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html> and
  442. removed the assembler code
  443. ----------------------------------------------------------------------}
  444. {$R-} {range checking off}
  445. {$Q-} {overflow checking off}
  446. { Period parameter }
  447. Const
  448. MT19937N=624;
  449. Type
  450. tMT19937StateArray = array [0..MT19937N-1] of longint; // the array for the state vector
  451. { Period parameters }
  452. const
  453. MT19937M=397;
  454. MT19937MATRIX_A =$9908b0df; // constant vector a
  455. MT19937UPPER_MASK=longint($80000000); // most significant w-r bits
  456. MT19937LOWER_MASK=longint($7fffffff); // least significant r bits
  457. { Tempering parameters }
  458. TEMPERING_MASK_B=longint($9d2c5680);
  459. TEMPERING_MASK_C=longint($efc60000);
  460. VAR
  461. mt : tMT19937StateArray;
  462. mti: longint=MT19937N+1; // mti=MT19937N+1 means mt[] is not initialized
  463. { Initializing the array with a seed }
  464. procedure sgenrand_MT19937(seed: longint);
  465. var
  466. i: longint;
  467. begin
  468. mt[0] := seed;
  469. for i := 1 to MT19937N-1 do
  470. begin
  471. mt[i] := 1812433253 * (mt[i-1] xor (mt[i-1] shr 30)) + i;
  472. { See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. }
  473. { In the previous versions, MSBs of the seed affect }
  474. { only MSBs of the array mt[]. }
  475. { 2002/01/09 modified by Makoto Matsumoto }
  476. end;
  477. mti := MT19937N;
  478. end;
  479. function genrand_MT19937: longint;
  480. const
  481. mag01 : array [0..1] of longint =(0, longint(MT19937MATRIX_A));
  482. var
  483. y: longint;
  484. kk: longint;
  485. begin
  486. if RandSeed<>OldRandSeed then
  487. mti:=MT19937N+1;
  488. if (mti >= MT19937N) { generate MT19937N longints at one time }
  489. then begin
  490. if mti = (MT19937N+1) then // if sgenrand_MT19937() has not been called,
  491. begin
  492. sgenrand_MT19937(randseed); // default initial seed is used
  493. { hack: randseed is not used more than once in this algorithm. Most }
  494. { user changes are re-initialising reandseed with the value it had }
  495. { at the start -> with the "not", we will detect this change. }
  496. { Detecting other changes is not useful, since the generated }
  497. { numbers will be different anyway. }
  498. randseed := not(randseed);
  499. oldrandseed := randseed;
  500. end;
  501. for kk:=0 to MT19937N-MT19937M-1 do begin
  502. y := (mt[kk] and MT19937UPPER_MASK) or (mt[kk+1] and MT19937LOWER_MASK);
  503. mt[kk] := mt[kk+MT19937M] xor (y shr 1) xor mag01[y and $00000001];
  504. end;
  505. for kk:= MT19937N-MT19937M to MT19937N-2 do begin
  506. y := (mt[kk] and MT19937UPPER_MASK) or (mt[kk+1] and MT19937LOWER_MASK);
  507. mt[kk] := mt[kk+(MT19937M-MT19937N)] xor (y shr 1) xor mag01[y and $00000001];
  508. end;
  509. y := (mt[MT19937N-1] and MT19937UPPER_MASK) or (mt[0] and MT19937LOWER_MASK);
  510. mt[MT19937N-1] := mt[MT19937M-1] xor (y shr 1) xor mag01[y and $00000001];
  511. mti := 0;
  512. end;
  513. y := mt[mti]; inc(mti);
  514. y := y xor (y shr 11);
  515. y := y xor (y shl 7) and TEMPERING_MASK_B;
  516. y := y xor (y shl 15) and TEMPERING_MASK_C;
  517. y := y xor (y shr 18);
  518. Result := y;
  519. end;
  520. function random(l:longint): longint;
  521. begin
  522. { otherwise we can return values = l (JM) }
  523. if (l < 0) then
  524. inc(l);
  525. random := longint((int64(cardinal(genrand_MT19937))*l) shr 32);
  526. end;
  527. function random(l:int64): int64;
  528. begin
  529. { always call random, so the random generator cycles (TP-compatible) (JM) }
  530. random := int64((qword(cardinal(genrand_MT19937)) or ((qword(cardinal(genrand_MT19937)) shl 32))) and $7fffffffffffffff);
  531. if (l<>0) then
  532. random := random mod l
  533. else
  534. random := 0;
  535. end;
  536. {$ifndef FPUNONE}
  537. function random: extended;
  538. begin
  539. random := cardinal(genrand_MT19937) * (extended(1.0)/(int64(1) shl 32));
  540. end;
  541. {$endif}
  542. {$endif FPC_HAS_FEATURE_RANDOM}
  543. {****************************************************************************
  544. Memory Management
  545. ****************************************************************************}
  546. {$ifndef FPC_SYSTEM_HAS_PTR}
  547. Function Ptr(sel,off : {$ifdef CPU16}Word{$else}Longint{$endif}) : farpointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  548. Begin
  549. ptr:=farpointer((sel shl 4)+off);
  550. End;
  551. {$endif not FPC_SYSTEM_HAS_PTR}
  552. {$ifndef FPC_SYSTEM_HAS_CSEG}
  553. Function CSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  554. Begin
  555. Cseg:=0;
  556. End;
  557. {$endif not FPC_SYSTEM_HAS_CSEG}
  558. {$ifndef FPC_SYSTEM_HAS_DSEG}
  559. Function DSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  560. Begin
  561. Dseg:=0;
  562. End;
  563. {$endif not FPC_SYSTEM_HAS_DSEG}
  564. {$ifndef FPC_SYSTEM_HAS_SSEG}
  565. Function SSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  566. Begin
  567. Sseg:=0;
  568. End;
  569. {$endif not FPC_SYSTEM_HAS_SSEG}
  570. {$push}
  571. {$R-}
  572. {$I-}
  573. {$Q-}
  574. {*****************************************************************************
  575. Miscellaneous
  576. *****************************************************************************}
  577. {$ifndef FPC_SYSTEM_HAS_STACKTOP}
  578. function StackTop: pointer;
  579. begin
  580. result:=StackBottom+StackLength;
  581. end;
  582. {$endif FPC_SYSTEM_HAS_STACKTOP}
  583. {$ifndef FPC_SYSTEM_HAS_GET_PC_ADDR}
  584. { This provides a dummy implementation
  585. of get_pc_addr function, for CPU's that don't need
  586. the instruction address to walk the stack. }
  587. function get_pc_addr : codepointer;
  588. begin
  589. get_pc_addr:=nil;
  590. end;
  591. {$endif ndef FPC_SYSTEM_HAS_GET_PC_ADDR}
  592. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_STACKINFO}
  593. { This provides a simple implementation
  594. of get_caller_stackinfo procedure,
  595. using get_caller_addr and get_caller_frame
  596. functions. }
  597. procedure get_caller_stackinfo(var framebp : pointer; var addr : codepointer);
  598. var
  599. nextbp : pointer;
  600. nextaddr : codepointer;
  601. begin
  602. nextbp:=get_caller_frame(framebp,addr);
  603. nextaddr:=get_caller_addr(framebp,addr);
  604. framebp:=nextbp;
  605. addr:=nextaddr;
  606. end;
  607. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_STACKINFO}
  608. procedure fpc_rangeerror;[public,alias:'FPC_RANGEERROR']; compilerproc;
  609. begin
  610. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  611. end;
  612. procedure fpc_divbyzero;[public,alias:'FPC_DIVBYZERO']; compilerproc;
  613. begin
  614. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  615. end;
  616. procedure fpc_overflow;[public,alias:'FPC_OVERFLOW']; compilerproc;
  617. begin
  618. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  619. end;
  620. procedure fpc_threaderror; [public,alias:'FPC_THREADERROR'];
  621. begin
  622. HandleErrorAddrFrameInd(6,get_pc_addr,get_frame);
  623. end;
  624. procedure fpc_invalidpointer; [public,alias:'FPC_INVALIDPOINTER'];
  625. begin
  626. HandleErrorAddrFrameInd(216,get_pc_addr,get_frame);
  627. end;
  628. procedure fpc_iocheck;[public,alias:'FPC_IOCHECK']; compilerproc;
  629. var
  630. l : longint;
  631. HInoutRes : PWord;
  632. begin
  633. HInOutRes:=@InoutRes;
  634. if HInOutRes^<>0 then
  635. begin
  636. l:=HInOutRes^;
  637. HInOutRes^:=0;
  638. HandleErrorAddrFrameInd(l,get_pc_addr,get_frame);
  639. end;
  640. end;
  641. Function IOResult:Word;
  642. var
  643. HInoutRes : PWord;
  644. Begin
  645. HInoutRes:=@InoutRes;
  646. IOResult:=HInOutRes^;
  647. HInOutRes^:=0;
  648. End;
  649. Function GetThreadID:TThreadID;{$ifdef SYSTEMINLINE}inline;{$endif}
  650. begin
  651. (* ThreadID is stored in a threadvar and made available in interface *)
  652. (* to allow setup of this value during thread initialization. *)
  653. GetThreadID := ThreadID;
  654. end;
  655. function fpc_safecallcheck(res : hresult) : hresult;[public,alias:'FPC_SAFECALLCHECK']; compilerproc; {$ifdef CPU86} register; {$endif}
  656. begin
  657. if res<0 then
  658. begin
  659. if assigned(SafeCallErrorProc) then
  660. SafeCallErrorProc(res,get_frame);
  661. HandleErrorAddrFrameInd(229,get_pc_addr,get_frame);
  662. end;
  663. result:=res;
  664. end;
  665. {*****************************************************************************
  666. Stack check code
  667. *****************************************************************************}
  668. { be compatible with old code }
  669. {$ifdef FPC_NO_GENERIC_STACK_CHECK}
  670. {$define NO_GENERIC_STACK_CHECK}
  671. {$endif FPC_NO_GENERIC_STACK_CHECK}
  672. {$IFNDEF NO_GENERIC_STACK_CHECK}
  673. {$PUSH}
  674. {$S-}
  675. procedure fpc_stackcheck(stack_size:SizeUInt);[public,alias:'FPC_STACKCHECK']; compilerproc;
  676. var
  677. c : Pointer;
  678. begin
  679. { Avoid recursive calls when called from the exit routines }
  680. if StackError then
  681. exit;
  682. { don't use sack_size, since the stack pointer has already been
  683. decreased when this routine is called
  684. }
  685. c := Sptr - STACK_MARGIN;
  686. if (c <= StackBottom) then
  687. begin
  688. StackError:=true;
  689. HandleError(202);
  690. end;
  691. end;
  692. {$POP}
  693. {$ENDIF NO_GENERIC_STACK_CHECK}
  694. {*****************************************************************************
  695. Initialization / Finalization
  696. *****************************************************************************}
  697. const
  698. maxunits=1024; { See also files.pas of the compiler source }
  699. type
  700. TInitFinalRec=record
  701. InitProc,
  702. FinalProc : TProcedure;
  703. end;
  704. TInitFinalTable = record
  705. TableCount,
  706. InitCount : {$if defined(VER2)}longint
  707. {$elseif defined(VER3_0)}sizeint
  708. {$else}ALUUInt{$endif}
  709. ;
  710. Procs : array[1..maxunits] of TInitFinalRec;
  711. end;
  712. PInitFinalTable = ^TInitFinalTable;
  713. {$ifndef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  714. var
  715. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  716. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  717. procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; compilerproc;
  718. var
  719. i : ALUUInt;
  720. {$ifdef DEBUG}
  721. pt : PInitFinalTable;
  722. {$endif}
  723. begin
  724. { call cpu/fpu initialisation routine }
  725. fpc_cpuinit;
  726. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  727. {$ifdef DEBUG}
  728. pt := PInitFinalTable(EntryInformation.InitFinalTable);
  729. {$endif}
  730. with PInitFinalTable(EntryInformation.InitFinalTable)^ do
  731. {$else FPC_HAS_INDIRECT_MAIN_INFORMATION}
  732. {$ifdef DEBUG}
  733. pt := @InitFinalTable;
  734. {$endif}
  735. with InitFinalTable do
  736. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  737. begin
  738. for i:=1 to ALUUInt(TableCount) do
  739. begin
  740. if assigned(Procs[i].InitProc) then
  741. Procs[i].InitProc();
  742. InitCount:=i;
  743. end;
  744. end;
  745. if assigned(InitProc) then
  746. TProcedure(InitProc)();
  747. end;
  748. procedure internal_initializeunits; external name 'FPC_INITIALIZEUNITS';
  749. procedure fpc_LibInitializeUnits;[public,alias:'FPC_LIBINITIALIZEUNITS'];compilerproc;
  750. begin
  751. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  752. IsLibrary:=true;
  753. { must also be set to true for packages when implemented }
  754. ModuleIsLib:=true;
  755. internal_initializeunits;
  756. {$endif FPC_HAS_FEATURE_DYNLIBS}
  757. end;
  758. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  759. begin
  760. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  761. with PInitFinalTable(EntryInformation.InitFinalTable)^ do
  762. {$else FPC_HAS_INDIRECT_MAIN_INFORMATION}
  763. with InitFinalTable do
  764. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  765. begin
  766. while (InitCount>0) do
  767. begin
  768. // we've to decrement the cound before calling the final. code
  769. // else a halt in the final. code leads to a endless loop
  770. dec(InitCount);
  771. if assigned(Procs[InitCount+1].FinalProc) then
  772. Procs[InitCount+1].FinalProc();
  773. end;
  774. end;
  775. end;
  776. {*****************************************************************************
  777. Error / Exit / ExitProc
  778. *****************************************************************************}
  779. Procedure system_exit;forward;
  780. {$ifdef FPC_HAS_FEATURE_HEAP}
  781. {$ifndef HAS_MEMORYMANAGER}
  782. //not needed if independant memory manager
  783. Procedure FinalizeHeap;forward;
  784. {$endif HAS_MEMORYMANAGER}
  785. {$endif FPC_HAS_FEATURE_HEAP}
  786. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  787. procedure SysFlushStdIO;
  788. begin
  789. { Make sure that all output is written to the redirected file }
  790. if Textrec(Output).Mode=fmOutput then
  791. Flush(Output);
  792. if Textrec(ErrOutput).Mode=fmOutput then
  793. Flush(ErrOutput);
  794. if Textrec(stdout).Mode=fmOutput then
  795. Flush(stdout);
  796. if Textrec(StdErr).Mode=fmOutput then
  797. Flush(StdErr);
  798. end;
  799. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  800. Procedure InternalExit;
  801. var
  802. current_exit : Procedure;
  803. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  804. pstdout : ^Text;
  805. {$endif}
  806. {$if defined(MSWINDOWS) or defined(OS2)}
  807. i : longint;
  808. {$endif}
  809. Begin
  810. {$ifdef SYSTEMDEBUG}
  811. writeln('InternalExit');
  812. {$endif SYSTEMDEBUG}
  813. while exitProc<>nil Do
  814. Begin
  815. InOutRes:=0;
  816. current_exit:=tProcedure(exitProc);
  817. exitProc:=nil;
  818. current_exit();
  819. End;
  820. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  821. { the embedded system unit itself contains no routines for console i/o
  822. console i/o is done by the Consoleio unit which can do things like
  823. redirection to seriell i/o }
  824. {$ifndef EMBEDDED}
  825. { Show runtime error and exit }
  826. pstdout:=@stdout;
  827. If erroraddr<>nil Then
  828. Begin
  829. Writeln(pstdout^,'Runtime error ',Errorcode,' at $',hexstr(erroraddr));
  830. { to get a nice symify }
  831. Writeln(pstdout^,BackTraceStrFunc(Erroraddr));
  832. dump_stack(pstdout^,ErrorBase,ErrorAddr);
  833. Writeln(pstdout^,'');
  834. End;
  835. SysFlushStdIO;
  836. {$endif EMBEDDED}
  837. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  838. { Finalize units }
  839. FinalizeUnits;
  840. {$if defined(MSWINDOWS) or defined(OS2)}
  841. { finally release the heap if possible, especially
  842. important for DLLs.
  843. Reset the array to nil, and finally also argv itself to
  844. avoid double freeing problem in case this function gets called twice. }
  845. if assigned(argv) then
  846. begin
  847. for i:=0 to argc-1 do
  848. if assigned(argv[i]) then
  849. begin
  850. sysfreemem(argv[i]);
  851. argv[i]:=nil;
  852. end;
  853. sysfreemem(argv);
  854. argv:=nil;
  855. end;
  856. {$endif}
  857. {$ifdef LINUX}
  858. {sysfreemem already checks for nil}
  859. { Do not try to do anything if the heap manager already reported an error }
  860. if (errorcode<>203) and (errorcode<>204) then
  861. sysfreemem(calculated_cmdline);
  862. {$endif}
  863. {$ifdef BSD}
  864. { Do not try to do anything if the heap manager already reported an error }
  865. if (errorcode<>203) and (errorcode<>204) then
  866. sysfreemem(cmdline);
  867. {$endif}
  868. {$ifdef FPC_HAS_FEATURE_HEAP}
  869. {$ifndef HAS_MEMORYMANAGER}
  870. {$ifndef FPC_NO_DEFAULT_HEAP}
  871. FinalizeHeap;
  872. {$endif not FPC_NO_DEFAULT_HEAP}
  873. {$endif not HAS_MEMORYMANAGER}
  874. {$endif FPC_HAS_FEATURE_HEAP}
  875. End;
  876. Procedure fpc_do_exit;[Public,Alias:'FPC_DO_EXIT']; compilerproc;
  877. begin
  878. InternalExit;
  879. System_exit;
  880. end;
  881. procedure internal_do_exit; external name 'FPC_DO_EXIT';
  882. Procedure lib_exit;[Public,Alias:'FPC_LIB_EXIT'];
  883. begin
  884. InternalExit;
  885. end;
  886. Procedure Halt(ErrNum: Longint);
  887. Begin
  888. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  889. {$ifdef FPC_LIMITED_EXITCODE}
  890. if ErrNum > maxExitCode then
  891. ExitCode:=255
  892. else
  893. {$endif FPC_LIMITED_EXITCODE}
  894. ExitCode:=ErrNum;
  895. {$endif FPC_HAS_FEATURE_EXITCODE}
  896. internal_do_exit;
  897. end;
  898. function SysBackTraceStr (Addr: CodePointer): ShortString;
  899. begin
  900. SysBackTraceStr:=' $'+hexstr(addr);
  901. end;
  902. {$ifndef FPC_SYSTEM_HAS_CAPTUREBACKTRACE}
  903. function CaptureBacktrace(skipframes,count:sizeint;frames:PCodePointer):sizeint;
  904. var
  905. curr_frame,prev_frame: pointer;
  906. curr_addr: codepointer;
  907. i: sizeint;
  908. begin
  909. curr_frame:=get_frame;
  910. curr_addr:=get_pc_addr;
  911. prev_frame:=curr_frame;
  912. get_caller_stackinfo(curr_frame,curr_addr);
  913. i:=-skipframes;
  914. while (i<count) and (curr_frame>prev_frame) and
  915. (curr_frame<StackTop) do
  916. begin
  917. if (curr_addr=nil) or
  918. (curr_frame=nil) then
  919. break;
  920. if (i>=0) then
  921. frames[i]:=curr_addr;
  922. inc(i);
  923. prev_frame:=curr_frame;
  924. get_caller_stackinfo(curr_frame,curr_addr);
  925. end;
  926. if i<0 then
  927. result:=0
  928. else
  929. result:=i;
  930. end;
  931. {$endif FPC_SYSTEM_HAS_CAPTUREBACKTRACE}
  932. Procedure HandleErrorAddrFrame (Errno : longint;addr : CodePointer; frame : Pointer);[public,alias:'FPC_BREAK_ERROR']; {$ifdef CPUI386} register; {$endif}
  933. begin
  934. If pointer(ErrorProc)<>Nil then
  935. ErrorProc(Errno,addr,frame);
  936. errorcode:=word(Errno);
  937. erroraddr:=addr;
  938. errorbase:=frame;
  939. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  940. if ExceptAddrStack <> nil then
  941. raise TObject(nil) at addr,frame;
  942. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  943. Halt(errorcode);
  944. end;
  945. { This is used internally by system skip first level,
  946. and generated the same output as before, when
  947. HandleErrorFrame function was used internally. }
  948. Procedure HandleErrorAddrFrameInd (Errno : longint;addr : CodePointer; frame : Pointer);
  949. begin
  950. get_caller_stackinfo (frame, addr);
  951. HandleErrorAddrFrame (Errno,addr,frame);
  952. end;
  953. Procedure HandleErrorFrame (Errno : longint;frame : Pointer);
  954. {
  955. Procedure to handle internal errors, i.e. not user-invoked errors
  956. Internal function should ALWAYS call HandleError instead of RunError.
  957. Can be used for exception handlers to specify the frame
  958. }
  959. begin
  960. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  961. end;
  962. procedure fpc_handleerror (Errno : longint); compilerproc; [public,alias : 'FPC_HANDLEERROR'];
  963. {
  964. Procedure to handle internal errors, i.e. not user-invoked errors
  965. Internal function should ALWAYS call HandleError instead of RunError.
  966. }
  967. begin
  968. HandleErrorAddrFrameInd(Errno,get_pc_addr,get_frame);
  969. end;
  970. procedure RunError(w : word);[alias: 'FPC_RUNERROR'];
  971. var
  972. bp : pointer;
  973. pcaddr : codepointer;
  974. begin
  975. errorcode:=w;
  976. pcaddr:=get_pc_addr;
  977. bp:=get_frame;
  978. get_caller_stackinfo(bp,pcaddr);
  979. erroraddr:=pcaddr;
  980. errorbase:=bp;
  981. Halt(errorcode);
  982. end;
  983. Procedure RunError;{$ifdef SYSTEMINLINE}inline;{$endif}
  984. Begin
  985. RunError (0);
  986. End;
  987. Procedure Halt;{$ifdef SYSTEMINLINE}inline;{$endif}
  988. Begin
  989. Halt(0);
  990. End;
  991. Procedure Error(RunTimeError : TRunTimeError);
  992. begin
  993. RunError(RuntimeErrorExitCodes[RunTimeError]);
  994. end;
  995. Procedure dump_stack(var f : text;fp : Pointer; addr : CodePointer);
  996. var
  997. i : Longint;
  998. prevfp : Pointer;
  999. is_dev : boolean;
  1000. Begin
  1001. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1002. try
  1003. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1004. { Frame of this procedure acts as StackBottom, fp values below that are invalid. }
  1005. prevfp:=get_frame;
  1006. i:=0;
  1007. is_dev:=do_isdevice(textrec(f).Handle);
  1008. { sanity checks, new frame pointer must be always greater than the old one, further
  1009. it must point into the stack area, else something went wrong }
  1010. while (fp>prevfp) and (fp<StackTop) do
  1011. Begin
  1012. prevfp:=fp;
  1013. get_caller_stackinfo(fp,addr);
  1014. if (addr=nil) then
  1015. break;
  1016. Writeln(f,BackTraceStrFunc(addr));
  1017. if (fp=nil) then
  1018. break;
  1019. Inc(i);
  1020. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  1021. break;
  1022. End;
  1023. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1024. except
  1025. { prevent endless dump if an exception occured }
  1026. end;
  1027. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1028. End;
  1029. procedure dump_stack(var f: text; skipframes: longint);
  1030. var
  1031. i,count: longint;
  1032. frames: array [0..255] of codepointer;
  1033. begin
  1034. if do_isdevice(textrec(f).handle) then
  1035. count:=max_frame_dump
  1036. else
  1037. count:=255;
  1038. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1039. try
  1040. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1041. count:=CaptureBacktrace(skipframes+1,count,@frames[0]);
  1042. for i:=0 to count-1 do
  1043. writeln(f,BackTraceStrFunc(frames[i]));
  1044. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1045. except
  1046. end;
  1047. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1048. end;
  1049. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1050. procedure DumpExceptionBackTrace(var f:text);
  1051. var
  1052. FrameNumber,
  1053. FrameCount : longint;
  1054. Frames : PCodePointer;
  1055. begin
  1056. if RaiseList=nil then
  1057. exit;
  1058. WriteLn(f,BackTraceStrFunc(RaiseList^.Addr));
  1059. FrameCount:=RaiseList^.Framecount;
  1060. Frames:=RaiseList^.Frames;
  1061. for FrameNumber := 0 to FrameCount-1 do
  1062. WriteLn(f,BackTraceStrFunc(Frames[FrameNumber]));
  1063. end;
  1064. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1065. {$ifdef FPC_HAS_FEATURE_HEAP}
  1066. Type
  1067. PExitProcInfo = ^TExitProcInfo;
  1068. TExitProcInfo = Record
  1069. Next : PExitProcInfo;
  1070. SaveExit : CodePointer;
  1071. Proc : TProcedure;
  1072. End;
  1073. const
  1074. ExitProcList: PExitProcInfo = nil;
  1075. Procedure DoExitProc;
  1076. var
  1077. P : PExitProcInfo;
  1078. Proc : TProcedure;
  1079. Begin
  1080. P:=ExitProcList;
  1081. ExitProcList:=P^.Next;
  1082. ExitProc:=P^.SaveExit;
  1083. Proc:=P^.Proc;
  1084. DisPose(P);
  1085. Proc();
  1086. End;
  1087. Procedure AddExitProc(Proc: TProcedure);
  1088. var
  1089. P : PExitProcInfo;
  1090. Begin
  1091. New(P);
  1092. P^.Next:=ExitProcList;
  1093. P^.SaveExit:=ExitProc;
  1094. P^.Proc:=Proc;
  1095. ExitProcList:=P;
  1096. ExitProc:=@DoExitProc;
  1097. End;
  1098. {$endif FPC_HAS_FEATURE_HEAP}
  1099. {$ifdef FPC_HAS_FEATURE_HEAP}
  1100. function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):ppchar; // const ?
  1101. // Extra allocate reserveentries pchar's at the beginning (default param=0 after 1.0.x ?)
  1102. // Note: for internal use by skilled programmers only
  1103. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  1104. var p : ppchar;
  1105. i : LongInt;
  1106. begin
  1107. if High(s)<Low(s) Then Exit(NIL);
  1108. Getmem(p,sizeof(pchar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  1109. // for cmd
  1110. if p=nil then
  1111. begin
  1112. {$ifdef xunix}
  1113. fpseterrno(ESysEnomem);
  1114. {$endif}
  1115. exit(NIL);
  1116. end;
  1117. for i:=low(s) to high(s) do
  1118. p[i+Reserveentries]:=pchar(s[i]);
  1119. p[high(s)+1+Reserveentries]:=nil;
  1120. ArrayStringToPPchar:=p;
  1121. end;
  1122. Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):ppchar;
  1123. {
  1124. Create a PPChar to structure of pchars which are the arguments specified
  1125. in the string S. Especially useful for creating an ArgV for Exec-calls
  1126. }
  1127. begin
  1128. StringToPPChar:=StringToPPChar(PChar(S),ReserveEntries);
  1129. end;
  1130. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  1131. var
  1132. i,nr : longint;
  1133. Buf : ^char;
  1134. p : ppchar;
  1135. begin
  1136. buf:=s;
  1137. nr:=1;
  1138. while (buf^<>#0) do // count nr of args
  1139. begin
  1140. while (buf^ in [' ',#9,#10]) do // Kill separators.
  1141. inc(buf);
  1142. inc(nr);
  1143. if buf^='"' Then // quotes argument?
  1144. begin
  1145. inc(buf);
  1146. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1147. inc(buf);
  1148. if buf^='"' then // skip closing quote.
  1149. inc(buf);
  1150. end
  1151. else
  1152. begin // else std
  1153. while not (buf^ in [' ',#0,#9,#10]) do
  1154. inc(buf);
  1155. end;
  1156. end;
  1157. getmem(p,(ReserveEntries+nr)*sizeof(pchar));
  1158. StringToPPChar:=p;
  1159. if p=nil then
  1160. begin
  1161. {$ifdef xunix}
  1162. fpseterrno(ESysEnomem);
  1163. {$endif}
  1164. exit;
  1165. end;
  1166. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  1167. buf:=s;
  1168. while (buf^<>#0) do
  1169. begin
  1170. while (buf^ in [' ',#9,#10]) do // Kill separators.
  1171. begin
  1172. buf^:=#0;
  1173. inc(buf);
  1174. end;
  1175. if buf^='"' Then // quotes argument?
  1176. begin
  1177. inc(buf);
  1178. p^:=buf;
  1179. inc(p);
  1180. p^:=nil;
  1181. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1182. inc(buf);
  1183. if buf^='"' then // skip closing quote.
  1184. begin
  1185. buf^:=#0;
  1186. inc(buf);
  1187. end;
  1188. end
  1189. else
  1190. begin
  1191. p^:=buf;
  1192. inc(p);
  1193. p^:=nil;
  1194. while not (buf^ in [' ',#0,#9,#10]) do
  1195. inc(buf);
  1196. end;
  1197. end;
  1198. end;
  1199. {$endif FPC_HAS_FEATURE_HEAP}
  1200. {*****************************************************************************
  1201. Abstract/Assert support.
  1202. *****************************************************************************}
  1203. procedure fpc_emptymethod;[public,alias : 'FPC_EMPTYMETHOD'];
  1204. begin
  1205. end;
  1206. procedure fpc_AbstractErrorIntern;compilerproc;[public,alias : 'FPC_ABSTRACTERROR'];
  1207. begin
  1208. If pointer(AbstractErrorProc)<>nil then
  1209. AbstractErrorProc();
  1210. HandleErrorAddrFrameInd(211,get_pc_addr,get_frame);
  1211. end;
  1212. Procedure fpc_assert(Const Msg,FName:Shortstring;LineNo:Longint;
  1213. ErrorAddr:Pointer); [Public,Alias : 'FPC_ASSERT']; compilerproc;
  1214. begin
  1215. if pointer(AssertErrorProc)<>nil then
  1216. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  1217. else
  1218. HandleErrorAddrFrameInd(227,get_pc_addr,get_frame);
  1219. end;
  1220. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
  1221. begin
  1222. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1223. If msg='' then
  1224. write(stderr,'Assertion failed')
  1225. else
  1226. write(stderr,msg);
  1227. Writeln(stderr,' (',FName,', line ',LineNo,').');
  1228. Writeln(stderr,'');
  1229. Halt(227);
  1230. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1231. end;
  1232. {*****************************************************************************
  1233. SetJmp/LongJmp support.
  1234. *****************************************************************************}
  1235. {$i setjump.inc}
  1236. {$pop} //{$I-,R-,Q-} before 'procedure fpc_rangeerror'
  1237. {*****************************************************************************
  1238. Heap
  1239. *****************************************************************************}
  1240. {$i sysheap.inc}
  1241. {$i heap.inc}
  1242. {*****************************************************************************
  1243. Thread support
  1244. *****************************************************************************}
  1245. {$ifdef FPC_HAS_FEATURE_THREADING}
  1246. { Generic threadmanager }
  1247. {$i thread.inc}
  1248. {$ifndef FPC_SECTION_THREADVARS}
  1249. { Generic threadvar support }
  1250. {$i threadvr.inc}
  1251. {$endif FPC_SECTION_THREADVARS}
  1252. {$ifdef DISABLE_NO_THREAD_MANAGER}
  1253. { OS Dependent implementation }
  1254. {$i systhrd.inc}
  1255. {$endif DISABLE_NO_THREAD_MANAGER}
  1256. {$endif FPC_HAS_FEATURE_THREADING}
  1257. {*****************************************************************************
  1258. Dynamic library support
  1259. *****************************************************************************}
  1260. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  1261. {$i dynlib.inc}
  1262. {$ifdef DISABLE_NO_DYNLIBS_MANAGER}
  1263. { OS Dependant implementation }
  1264. {$i sysdl.inc}
  1265. {$endif DISABLE_NO_DYNLIBS_MANAGER}
  1266. {$endif FPC_HAS_FEATURE_DYNLIBS}
  1267. {*****************************************************************************
  1268. File Handling
  1269. *****************************************************************************}
  1270. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1271. { Allow slash and backslash as separators }
  1272. procedure DoDirSeparators(var p: pchar; inplace: boolean = true);
  1273. var
  1274. i : longint;
  1275. len : sizeint;
  1276. newp : pchar;
  1277. begin
  1278. len:=length(p);
  1279. newp:=nil;
  1280. for i:=0 to len do
  1281. if p[i] in AllowDirectorySeparators then
  1282. begin
  1283. if not inplace and
  1284. not assigned(newp) then
  1285. begin
  1286. getmem(newp,len+1);
  1287. move(p^,newp^,len+1);
  1288. p:=newp;
  1289. end;
  1290. p[i]:=DirectorySeparator;
  1291. end;
  1292. end;
  1293. procedure DoDirSeparators(var p: pwidechar; inplace: boolean = true);
  1294. var
  1295. i : longint;
  1296. len : sizeint;
  1297. newp : pwidechar;
  1298. begin
  1299. len:=length(p);
  1300. newp:=nil;
  1301. for i:=0 to len do
  1302. if (ord(p[i])<255) and
  1303. (ansichar(ord(p[i])) in AllowDirectorySeparators) then
  1304. begin
  1305. if not inplace and
  1306. not assigned(newp) then
  1307. begin
  1308. getmem(newp,(len+1)*2);
  1309. move(p^,newp^,(len+1)*2);
  1310. p:=newp;
  1311. end;
  1312. p[i]:=DirectorySeparator;
  1313. end;
  1314. end;
  1315. procedure DoDirSeparators(var p:shortstring);
  1316. var
  1317. i : longint;
  1318. begin
  1319. for i:=1 to length(p) do
  1320. if p[i] in AllowDirectorySeparators then
  1321. p[i]:=DirectorySeparator;
  1322. end;
  1323. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1324. procedure DoDirSeparators(var ps:RawByteString);
  1325. var
  1326. i : longint;
  1327. p : pchar;
  1328. unique : boolean;
  1329. begin
  1330. unique:=false;
  1331. for i:=1 to length(ps) do
  1332. if ps[i] in AllowDirectorySeparators then
  1333. begin
  1334. if not unique then
  1335. begin
  1336. uniquestring(ps);
  1337. p:=pchar(ps);
  1338. unique:=true;
  1339. end;
  1340. p[i-1]:=DirectorySeparator;
  1341. end;
  1342. end;
  1343. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1344. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  1345. procedure DoDirSeparators(var ps:UnicodeString);
  1346. var
  1347. i : longint;
  1348. p : pwidechar;
  1349. unique : boolean;
  1350. begin
  1351. unique:=false;
  1352. for i:=1 to length(ps) do
  1353. if ps[i] in AllowDirectorySeparators then
  1354. begin
  1355. if not unique then
  1356. begin
  1357. uniquestring(ps);
  1358. p:=pwidechar(ps);
  1359. unique:=true;
  1360. end;
  1361. p[i-1]:=DirectorySeparator;
  1362. end;
  1363. end;
  1364. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  1365. {$endif FPC_HAS_FEATURE_FILEIO}
  1366. { OS dependent low level file functions }
  1367. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1368. {$i sysfile.inc}
  1369. {$ifndef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1370. {$ifdef FPC_ANSI_TEXTFILEREC}
  1371. procedure do_open(var f; p: pansichar; flags: longint; pchangeable: boolean);
  1372. var
  1373. u: UnicodeString;
  1374. begin
  1375. widestringmanager.Ansi2UnicodeMoveProc(p,DefaultFileSystemCodePage,u,length(p));
  1376. do_open(f,pwidechar(u),flags,true);
  1377. end;
  1378. procedure do_erase(p: pansichar; pchangeable: boolean);
  1379. var
  1380. u: UnicodeString;
  1381. begin
  1382. widestringmanager.Ansi2UnicodeMoveProc(p,DefaultFileSystemCodePage,u,length(p));
  1383. do_erase(pwidechar(u),true);
  1384. end;
  1385. procedure do_rename(src, dst: pansichar; srcchangeable, dstchangeable: boolean);
  1386. var
  1387. usrc, udst: UnicodeString;
  1388. begin
  1389. widestringmanager.Ansi2UnicodeMoveProc(src,DefaultFileSystemCodePage,usrc,length(src));
  1390. widestringmanager.Ansi2UnicodeMoveProc(dst,DefaultFileSystemCodePage,udst,length(dst));
  1391. do_rename(pwidechar(usrc),pwidechar(udst),true,true);
  1392. end;
  1393. procedure do_rename(src: pansichar; dst: pwidechar; srcchangeable, dstchangeable: boolean);
  1394. var
  1395. usrc: UnicodeString;
  1396. begin
  1397. widestringmanager.Ansi2UnicodeMoveProc(src,DefaultFileSystemCodePage,usrc,length(src));
  1398. do_rename(pwidechar(usrc),dst,true,dstchangeable);
  1399. end;
  1400. {$endif FPC_ANSI_TEXTFILEREC}
  1401. {$endif not FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1402. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1403. {$ifndef FPC_ANSI_TEXTFILEREC}
  1404. procedure do_open(var f; p: pwidechar; flags: longint; pchangeable: boolean);
  1405. var
  1406. s: RawByteString;
  1407. begin
  1408. widestringmanager.Unicode2AnsiMoveProc(p,s,DefaultFileSystemCodePage,length(p));
  1409. do_open(f,pansichar(s),flags,true);
  1410. end;
  1411. procedure do_erase(p: pwidechar; pchangeable: boolean);
  1412. var
  1413. s: RawByteString;
  1414. begin
  1415. widestringmanager.Unicode2AnsiMoveProc(p,s,DefaultFileSystemCodePage,length(p));
  1416. do_erase(pansichar(s),true);
  1417. end;
  1418. procedure do_rename(src, dst: pwidechar; srcchangeable, dstchangeable: boolean);
  1419. var
  1420. rsrc, rdst: RawByteString;
  1421. begin
  1422. widestringmanager.Unicode2AnsiMoveProc(src,rsrc,DefaultFileSystemCodePage,length(src));
  1423. widestringmanager.Unicode2AnsiMoveProc(dst,rdst,DefaultFileSystemCodePage,length(dst));
  1424. do_rename(pansichar(rsrc),pansichar(rdst),true,true);
  1425. end;
  1426. procedure do_rename(src: pwidechar; dst: pansichar; srcchangeable, dstchangeable: boolean);
  1427. var
  1428. rsrc: RawByteString;
  1429. begin
  1430. widestringmanager.Unicode2AnsiMoveProc(src,rsrc,DefaultFileSystemCodePage,length(src));
  1431. do_rename(pansichar(rsrc),dst,true,dstchangeable);
  1432. end;
  1433. {$endif not FPC_ANSI_TEXTFILEREC}
  1434. {$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1435. {$endif FPC_HAS_FEATURE_FILEIO}
  1436. { helper for targets supporting no ansistrings, it is used
  1437. by non-ansistring code }
  1438. function min(v1,v2 : SizeInt) : SizeInt;
  1439. begin
  1440. if v1<v2 then
  1441. result:=v1
  1442. else
  1443. result:=v2;
  1444. end;
  1445. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  1446. { Text file }
  1447. {$i text.inc}
  1448. {$endif FPC_HAS_FEATURE_TEXTIO}
  1449. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1450. { Untyped file }
  1451. {$i file.inc}
  1452. { Typed file }
  1453. {$i typefile.inc}
  1454. {$endif FPC_HAS_FEATURE_FILEIO}
  1455. {*****************************************************************************
  1456. Directory Handling
  1457. *****************************************************************************}
  1458. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1459. { OS dependent dir functions }
  1460. {$i sysdir.inc}
  1461. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1462. {$ifndef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1463. procedure do_getdir(drivenr : byte;var dir : rawbytestring);
  1464. var
  1465. u: unicodestring;
  1466. begin
  1467. Do_getdir(drivenr,u);
  1468. widestringmanager.Unicode2AnsiMoveProc(pwidechar(u),dir,DefaultRTLFileSystemCodePage,length(u));
  1469. end;
  1470. {$endif FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1471. Procedure MkDir(Const s: RawByteString);[IOCheck];
  1472. Begin
  1473. If (s='') or (InOutRes <> 0) then
  1474. exit;
  1475. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1476. Do_mkdir(ToSingleByteFileSystemEncodedFileName(S));
  1477. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1478. Do_mkdir(S);
  1479. {$endif}
  1480. end;
  1481. Procedure RmDir(Const s: RawByteString);[IOCheck];
  1482. Begin
  1483. If (s='') or (InOutRes <> 0) then
  1484. exit;
  1485. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1486. Do_rmdir(ToSingleByteFileSystemEncodedFileName(S));
  1487. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1488. Do_rmdir(S);
  1489. {$endif}
  1490. End;
  1491. Procedure ChDir(Const s: RawByteString);[IOCheck];
  1492. Begin
  1493. If (s='') or (InOutRes <> 0) then
  1494. exit;
  1495. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1496. Do_chdir(ToSingleByteFileSystemEncodedFileName(S));
  1497. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1498. Do_chdir(S);
  1499. {$endif}
  1500. End;
  1501. Procedure getdir(drivenr:byte;Var dir:rawbytestring);
  1502. begin
  1503. Do_getdir(drivenr,dir);
  1504. { we should return results in the DefaultRTLFileSystemCodePage -> convert if
  1505. necessary }
  1506. setcodepage(dir,DefaultRTLFileSystemCodePage,true);
  1507. end;
  1508. { the generic shortstring ones are only implemented elsewhere for systems *not*
  1509. supporting ansi/unicodestrings; for now assume there are no systems that
  1510. support unicodestrings but not ansistrings }
  1511. { avoid double string conversions }
  1512. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1513. function GetDirStrFromShortstring(const s: shortstring): RawByteString;
  1514. begin
  1515. GetDirStrFromShortstring:=ToSingleByteFileSystemEncodedFileName(ansistring(s));
  1516. end;
  1517. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1518. function GetDirStrFromShortstring(const s: shortstring): UnicodeString;
  1519. begin
  1520. GetDirStrFromShortstring:=s;
  1521. end;
  1522. {$endif FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1523. Procedure MkDir(Const s: shortstring);[IOCheck];
  1524. Begin
  1525. If (s='') or (InOutRes <> 0) then
  1526. exit;
  1527. Do_mkdir(GetDirStrFromShortstring(S));
  1528. End;
  1529. Procedure RmDir(Const s: shortstring);[IOCheck];
  1530. Begin
  1531. If (s='') or (InOutRes <> 0) then
  1532. exit;
  1533. Do_rmdir(GetDirStrFromShortstring(S));
  1534. End;
  1535. Procedure ChDir(Const s: shortstring);[IOCheck];
  1536. Begin
  1537. If (s='') or (InOutRes <> 0) then
  1538. exit;
  1539. Do_chdir(GetDirStrFromShortstring(S));
  1540. End;
  1541. Procedure getdir(drivenr:byte;Var dir:shortstring);
  1542. var
  1543. s: rawbytestring;
  1544. begin
  1545. Do_getdir(drivenr,s);
  1546. if length(s)<=high(dir) then
  1547. dir:=s
  1548. else
  1549. inoutres:=3;
  1550. end;
  1551. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1552. {$if defined(FPC_HAS_FEATURE_WIDESTRINGS)}
  1553. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1554. { overloads required for mkdir/rmdir/chdir to ensure that the string is
  1555. converted to the right code page }
  1556. procedure do_mkdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1557. begin
  1558. do_mkdir(ToSingleByteFileSystemEncodedFileName(s));
  1559. end;
  1560. procedure do_rmdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1561. begin
  1562. do_rmdir(ToSingleByteFileSystemEncodedFileName(s));
  1563. end;
  1564. procedure do_chdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1565. begin
  1566. do_chdir(ToSingleByteFileSystemEncodedFileName(s));
  1567. end;
  1568. procedure do_getdir(drivenr : byte;var dir : unicodestring);
  1569. var
  1570. s: rawbytestring;
  1571. begin
  1572. Do_getdir(drivenr,s);
  1573. dir:=s;
  1574. end;
  1575. {$endif FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1576. Procedure MkDir(Const s: UnicodeString);[IOCheck];
  1577. Begin
  1578. if (s='') or (InOutRes <> 0) then
  1579. exit;
  1580. Do_mkdir(S);
  1581. End;
  1582. Procedure RmDir(Const s: UnicodeString);[IOCheck];
  1583. Begin
  1584. if (s='') or (InOutRes <> 0) then
  1585. exit;
  1586. Do_rmdir(S);
  1587. End;
  1588. Procedure ChDir(Const s: UnicodeString);[IOCheck];
  1589. Begin
  1590. if (s='') or (InOutRes <> 0) then
  1591. exit;
  1592. Do_chdir(S);
  1593. End;
  1594. Procedure getdir(drivenr:byte;Var dir:unicodestring);
  1595. begin
  1596. Do_getdir(drivenr,dir);
  1597. end;
  1598. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1599. {$endif FPC_HAS_FEATURE_FILEIO}
  1600. {*****************************************************************************
  1601. Resources support
  1602. *****************************************************************************}
  1603. {$i sysres.inc}
  1604. const
  1605. CtrlBreakHandler: TCtrlBreakHandler = nil;
  1606. {$IFNDEF FPC_HAS_SETCTRLBREAKHANDLER}
  1607. (* It is possible to provide platform specific implementation performing *)
  1608. (* special initialization; default implementation just sets the procedural *)
  1609. (* variable to make it available for use from the exception handler. *)
  1610. function SysSetCtrlBreakHandler (Handler: TCtrlBreakHandler): TCtrlBreakHandler;
  1611. begin
  1612. (* Return either nil or previous handler *)
  1613. SysSetCtrlBreakHandler := CtrlBreakHandler;
  1614. CtrlBreakHandler := Handler;
  1615. end;
  1616. {$ENDIF FPC_HAS_SETCTRLBREAKHANDLER}