2
0

jsystem.inc 50 KB

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