jsystem.inc 50 KB

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