jsystem.inc 50 KB

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