system.inc 51 KB

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