2
0

jsystem.inc 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  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 : AnsiChar);
  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:AnsiChar):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. FPU Exceptions state
  765. *****************************************************************************}
  766. {$if defined(FPC_SYSTEM_FPUCW_IMMUTABLE) or defined(FPU_NONE)}
  767. function GetNativeFPUControlWord: TNativeFPUControlWord; {$if defined(SYSTEMINLINE)}inline;{$endif}
  768. begin
  769. result:=default(TNativeFPUControlWord);
  770. end;
  771. procedure SetNativeFPUControlWord(const cw: TNativeFPUControlWord); {$if defined(SYSTEMINLINE)}inline;{$endif}
  772. begin
  773. end;
  774. {$endif}
  775. {*****************************************************************************
  776. Error / Exit / ExitProc
  777. *****************************************************************************}
  778. Procedure system_exit;forward;
  779. {$ifdef FPC_HAS_FEATURE_HEAP}
  780. {$ifndef HAS_MEMORYMANAGER}
  781. //not needed if independant memory manager
  782. Procedure FinalizeHeap;forward;
  783. {$endif HAS_MEMORYMANAGER}
  784. {$endif FPC_HAS_FEATURE_HEAP}
  785. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  786. procedure SysFlushStdIO;
  787. begin
  788. { Make sure that all output is written to the redirected file }
  789. if Textrec(Output).Mode=fmOutput then
  790. Flush(Output);
  791. if Textrec(ErrOutput).Mode=fmOutput then
  792. Flush(ErrOutput);
  793. if Textrec(stdout).Mode=fmOutput then
  794. Flush(stdout);
  795. if Textrec(StdErr).Mode=fmOutput then
  796. Flush(StdErr);
  797. end;
  798. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  799. Procedure InternalExit;
  800. (*
  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. *)
  810. Begin
  811. (*
  812. {$ifdef SYSTEMDEBUG}
  813. writeln('InternalExit');
  814. {$endif SYSTEMDEBUG}
  815. while exitProc<>nil Do
  816. Begin
  817. InOutRes:=0;
  818. current_exit:=tProcedure(exitProc);
  819. exitProc:=nil;
  820. current_exit();
  821. End;
  822. { Finalize units }
  823. FinalizeUnits;
  824. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  825. { the embedded system unit itself contains no routines for console i/o
  826. console i/o is done by the Consoleio unit which can do things like
  827. redirection to seriell i/o }
  828. {$ifndef EMBEDDED}
  829. { Show runtime error and exit }
  830. pstdout:=@stdout;
  831. If erroraddr<>nil Then
  832. Begin
  833. Writeln(pstdout^,'Runtime error ',Errorcode,' at $',hexstr(erroraddr));
  834. { to get a nice symify }
  835. Writeln(pstdout^,BackTraceStrFunc(Erroraddr));
  836. dump_stack(pstdout^,ErrorBase,ErrorAddr);
  837. Writeln(pstdout^,'');
  838. End;
  839. SysFlushStdIO;
  840. {$endif EMBEDDED}
  841. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  842. {$if defined(MSWINDOWS) or defined(OS2)}
  843. { finally release the heap if possible, especially
  844. important for DLLs.
  845. Reset the array to nil, and finally also argv itself to
  846. avoid double freeing problem in case this function gets called twice. }
  847. if assigned(argv) then
  848. begin
  849. for i:=0 to argc-1 do
  850. if assigned(argv[i]) then
  851. begin
  852. sysfreemem(argv[i]);
  853. argv[i]:=nil;
  854. end;
  855. sysfreemem(argv);
  856. argv:=nil;
  857. end;
  858. {$endif}
  859. {$ifdef LINUX}
  860. {sysfreemem already checks for nil}
  861. { Do not try to do anything if the heap manager already reported an error }
  862. if (errorcode<>203) and (errorcode<>204) then
  863. sysfreemem(calculated_cmdline);
  864. {$endif}
  865. {$ifdef BSD}
  866. { Do not try to do anything if the heap manager already reported an error }
  867. if (errorcode<>203) and (errorcode<>204) then
  868. sysfreemem(cmdline);
  869. {$endif}
  870. {$ifdef FPC_HAS_FEATURE_HEAP}
  871. {$ifndef HAS_MEMORYMANAGER}
  872. {$ifndef FPC_NO_DEFAULT_HEAP}
  873. FinalizeHeap;
  874. {$endif not FPC_NO_DEFAULT_HEAP}
  875. {$endif not HAS_MEMORYMANAGER}
  876. {$endif FPC_HAS_FEATURE_HEAP}
  877. *)
  878. End;
  879. Procedure fpc_do_exit;[Public,Alias:'FPC_DO_EXIT']; compilerproc;
  880. begin
  881. InternalExit;
  882. System_exit;
  883. end;
  884. procedure internal_do_exit; external name 'fpc_do_exit';
  885. Procedure fpc_lib_exit;[Public,Alias:'FPC_LIB_EXIT'];
  886. begin
  887. InternalExit;
  888. end;
  889. Procedure Halt(ErrNum: Longint);
  890. Begin
  891. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  892. {$ifdef FPC_LIMITED_EXITCODE}
  893. if ErrNum > maxExitCode then
  894. ExitCode:=255
  895. else
  896. {$endif FPC_LIMITED_EXITCODE}
  897. ExitCode:=ErrNum;
  898. {$endif FPC_HAS_FEATURE_EXITCODE}
  899. internal_do_exit;
  900. end;
  901. (*
  902. function SysBackTraceStr (Addr: CodePointer): ShortString;
  903. begin
  904. SysBackTraceStr:=' $'+hexstr(addr);
  905. end;
  906. *)
  907. (*
  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. prev_frame:=curr_frame;
  923. get_caller_stackinfo(curr_frame,curr_addr);
  924. if (curr_addr=nil) or
  925. (curr_frame=nil) then
  926. break;
  927. if (i>=0) then
  928. frames[i]:=curr_addr;
  929. inc(i);
  930. end;
  931. if i<0 then
  932. result:=0
  933. else
  934. result:=i;
  935. end;
  936. *)
  937. Procedure HandleErrorAddrFrame (Errno : longint;addr : CodePointer; frame : Pointer);[public,alias:'FPC_BREAK_ERROR']; {$ifdef CPUI386} register; {$endif}
  938. begin
  939. If pointer(ErrorProc)<>Nil then
  940. ErrorProc(Errno,addr,frame);
  941. raise FpcRunTimeError.Create(Errno);
  942. (*
  943. errorcode:=word(Errno);
  944. erroraddr:=addr;
  945. errorbase:=frame;
  946. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  947. if ExceptAddrStack <> nil then
  948. raise TObject(nil) at addr,frame;
  949. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  950. Halt(errorcode);
  951. *)
  952. end;
  953. { This is used internally by system skip first level,
  954. and generated the same output as before, when
  955. HandleErrorFrame function was used internally. }
  956. Procedure HandleErrorAddrFrameInd (Errno : longint;addr : CodePointer; frame : Pointer);
  957. begin
  958. get_caller_stackinfo (frame, addr);
  959. HandleErrorAddrFrame (Errno,addr,frame);
  960. end;
  961. Procedure HandleErrorFrame (Errno : longint;frame : Pointer);
  962. {
  963. Procedure to handle internal errors, i.e. not user-invoked errors
  964. Internal function should ALWAYS call HandleError instead of RunError.
  965. Can be used for exception handlers to specify the frame
  966. }
  967. begin
  968. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  969. end;
  970. procedure fpc_handleerror (Errno : longint); compilerproc; [public,alias : 'FPC_HANDLEERROR'];
  971. {
  972. Procedure to handle internal errors, i.e. not user-invoked errors
  973. Internal function should ALWAYS call HandleError instead of RunError.
  974. }
  975. begin
  976. HandleErrorAddrFrameInd(Errno,get_pc_addr,get_frame);
  977. end;
  978. procedure RunError(w : word);[alias: 'FPC_RUNERROR'];
  979. var
  980. bp : pointer;
  981. pcaddr : codepointer;
  982. begin
  983. errorcode:=w;
  984. (*
  985. pcaddr:=get_pc_addr;
  986. bp:=get_frame;
  987. get_caller_stackinfo(bp,pcaddr);
  988. erroraddr:=pcaddr;
  989. errorbase:=bp;
  990. *)
  991. Halt(errorcode);
  992. end;
  993. Procedure RunError;{$ifdef SYSTEMINLINE}inline;{$endif}
  994. Begin
  995. RunError (0);
  996. End;
  997. Procedure Halt;{$ifdef SYSTEMINLINE}inline;{$endif}
  998. Begin
  999. Halt(0);
  1000. End;
  1001. Procedure Error(RunTimeError : TRunTimeError);
  1002. begin
  1003. RunError(RuntimeErrorExitCodes[RunTimeError]);
  1004. end;
  1005. {$ifndef CPUJVM}
  1006. Procedure dump_stack(var f : text;fp : Pointer; addr : CodePointer);
  1007. var
  1008. i : Longint;
  1009. prevfp : Pointer;
  1010. is_dev : boolean;
  1011. Begin
  1012. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1013. try
  1014. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1015. { Frame of this procedure acts as StackBottom, fp values below that are invalid. }
  1016. prevfp:=get_frame;
  1017. i:=0;
  1018. is_dev:=do_isdevice(textrec(f).Handle);
  1019. { sanity checks, new frame pointer must be always greater than the old one, further
  1020. it must point into the stack area, else something went wrong }
  1021. while (fp>prevfp) and (fp<StackTop) do
  1022. Begin
  1023. prevfp:=fp;
  1024. get_caller_stackinfo(fp,addr);
  1025. if (addr=nil) then
  1026. break;
  1027. Writeln(f,BackTraceStrFunc(addr));
  1028. if (fp=nil) then
  1029. break;
  1030. Inc(i);
  1031. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  1032. break;
  1033. End;
  1034. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1035. except
  1036. { prevent endless dump if an exception occurred }
  1037. end;
  1038. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1039. End;
  1040. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1041. procedure DumpExceptionBackTrace(var f:text);
  1042. var
  1043. FrameNumber,
  1044. FrameCount : longint;
  1045. Frames : PCodePointer;
  1046. begin
  1047. if RaiseList=nil then
  1048. exit;
  1049. WriteLn(f,BackTraceStrFunc(RaiseList^.Addr));
  1050. FrameCount:=RaiseList^.Framecount;
  1051. Frames:=RaiseList^.Frames;
  1052. for FrameNumber := 0 to FrameCount-1 do
  1053. WriteLn(f,BackTraceStrFunc(Frames[FrameNumber]));
  1054. end;
  1055. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1056. {$endif CPUJVM}
  1057. {$ifdef FPC_HAS_FEATURE_HEAP}
  1058. Type
  1059. PExitProcInfo = ^TExitProcInfo;
  1060. TExitProcInfo = Record
  1061. Next : PExitProcInfo;
  1062. SaveExit : CodePointer;
  1063. Proc : TProcedure;
  1064. End;
  1065. const
  1066. ExitProcList: PExitProcInfo = nil;
  1067. Procedure DoExitProc;
  1068. var
  1069. P : PExitProcInfo;
  1070. Proc : TProcedure;
  1071. Begin
  1072. P:=ExitProcList;
  1073. ExitProcList:=P^.Next;
  1074. ExitProc:=P^.SaveExit;
  1075. Proc:=P^.Proc;
  1076. DisPose(P);
  1077. Proc();
  1078. End;
  1079. Procedure AddExitProc(Proc: TProcedure);
  1080. var
  1081. P : PExitProcInfo;
  1082. Begin
  1083. New(P);
  1084. P^.Next:=ExitProcList;
  1085. P^.SaveExit:=ExitProc;
  1086. P^.Proc:=Proc;
  1087. ExitProcList:=P;
  1088. ExitProc:=@DoExitProc;
  1089. End;
  1090. {$endif FPC_HAS_FEATURE_HEAP}
  1091. {$ifdef FPC_HAS_FEATURE_HEAP}
  1092. function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):PPAnsiChar; // const ?
  1093. // Extra allocate reserveentries PAnsiChar's at the beginning (default param=0 after 1.0.x ?)
  1094. // Note: for internal use by skilled programmers only
  1095. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  1096. var p : PPAnsiChar;
  1097. i : LongInt;
  1098. begin
  1099. if High(s)<Low(s) Then Exit(NIL);
  1100. Getmem(p,sizeof(PAnsiChar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  1101. // for cmd
  1102. if p=nil then
  1103. begin
  1104. {$ifdef xunix}
  1105. fpseterrno(ESysEnomem);
  1106. {$endif}
  1107. exit(NIL);
  1108. end;
  1109. for i:=low(s) to high(s) do
  1110. p[i+Reserveentries]:=PAnsiChar(s[i]);
  1111. p[high(s)+1+Reserveentries]:=nil;
  1112. ArrayStringToPPchar:=p;
  1113. end;
  1114. Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):PPAnsiChar;
  1115. {
  1116. Create a PPAnsiChar to structure of pchars which are the arguments specified
  1117. in the string S. Especially useful for creating an ArgV for Exec-calls
  1118. }
  1119. begin
  1120. StringToPPChar:=StringToPPChar(PAnsiChar(S),ReserveEntries);
  1121. end;
  1122. Function StringToPPChar(S: PAnsiChar;ReserveEntries:integer):PPAnsiChar;
  1123. var
  1124. i,nr : longint;
  1125. Buf : ^AnsiChar;
  1126. p : PPAnsiChar;
  1127. begin
  1128. buf:=s;
  1129. nr:=1;
  1130. while (buf^<>#0) do // count nr of args
  1131. begin
  1132. while (buf^ in [' ',#9,#10]) do // Kill separators.
  1133. inc(buf);
  1134. inc(nr);
  1135. if buf^='"' Then // quotes argument?
  1136. begin
  1137. inc(buf);
  1138. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1139. inc(buf);
  1140. if buf^='"' then // skip closing quote.
  1141. inc(buf);
  1142. end
  1143. else
  1144. begin // else std
  1145. while not (buf^ in [' ',#0,#9,#10]) do
  1146. inc(buf);
  1147. end;
  1148. end;
  1149. getmem(p,(ReserveEntries+nr)*sizeof(PAnsiChar));
  1150. StringToPPChar:=p;
  1151. if p=nil then
  1152. begin
  1153. {$ifdef xunix}
  1154. fpseterrno(ESysEnomem);
  1155. {$endif}
  1156. exit;
  1157. end;
  1158. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  1159. buf:=s;
  1160. while (buf^<>#0) do
  1161. begin
  1162. while (buf^ in [' ',#9,#10]) do // Kill separators.
  1163. begin
  1164. buf^:=#0;
  1165. inc(buf);
  1166. end;
  1167. if buf^='"' Then // quotes argument?
  1168. begin
  1169. inc(buf);
  1170. p^:=buf;
  1171. inc(p);
  1172. p^:=nil;
  1173. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1174. inc(buf);
  1175. if buf^='"' then // skip closing quote.
  1176. begin
  1177. buf^:=#0;
  1178. inc(buf);
  1179. end;
  1180. end
  1181. else
  1182. begin
  1183. p^:=buf;
  1184. inc(p);
  1185. p^:=nil;
  1186. while not (buf^ in [' ',#0,#9,#10]) do
  1187. inc(buf);
  1188. end;
  1189. end;
  1190. end;
  1191. {$endif FPC_HAS_FEATURE_HEAP}
  1192. {*****************************************************************************
  1193. Abstract/Assert support.
  1194. *****************************************************************************}
  1195. procedure fpc_emptymethod;[public,alias : 'FPC_EMPTYMETHOD'];
  1196. begin
  1197. end;
  1198. procedure fpc_AbstractErrorIntern;compilerproc;[public,alias : 'FPC_ABSTRACTERROR'];
  1199. begin
  1200. (*
  1201. If pointer(AbstractErrorProc)<>nil then
  1202. AbstractErrorProc();
  1203. *)
  1204. HandleErrorAddrFrameInd(211,get_pc_addr,get_frame);
  1205. end;
  1206. Procedure fpc_assert(Const Msg,FName:unicodestring;LineNo:Longint;ErrorAddr:Pointer); [Public,Alias : 'FPC_ASSERT']; compilerproc;
  1207. begin
  1208. (*
  1209. if pointer(AssertErrorProc)<>nil then
  1210. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  1211. else
  1212. HandleErrorAddrFrameInd(227,get_pc_addr,get_frame);
  1213. *)
  1214. raise JLAssertionError.Create(JLObject(Fname+'('+unicodestring(JLInteger.valueOf(LineNo).toString)+'): '+Msg));
  1215. end;
  1216. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
  1217. begin
  1218. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1219. If msg='' then
  1220. write(stderr,'Assertion failed')
  1221. else
  1222. write(stderr,msg);
  1223. Writeln(stderr,' (',FName,', line ',LineNo,').');
  1224. Writeln(stderr,'');
  1225. Halt(227);
  1226. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1227. end;
  1228. {*****************************************************************************
  1229. SetJmp/LongJmp support.
  1230. *****************************************************************************}
  1231. {$i setjump.inc}
  1232. {$pop} //{$I-,R-,Q-} before 'procedure fpc_rangeerror'
  1233. {*****************************************************************************
  1234. Heap
  1235. *****************************************************************************}
  1236. {$ifndef CPUJVM}
  1237. {$i sysheap.inc}
  1238. {$i heap.inc}
  1239. {$endif CPUJVM}
  1240. {*****************************************************************************
  1241. Thread support
  1242. *****************************************************************************}
  1243. {$if defined(FPC_HAS_FEATURE_THREADING) and not defined(cpujvm)}
  1244. { Generic threadmanager }
  1245. {$i thread.inc}
  1246. {$ifndef FPC_SECTION_THREADVARS}
  1247. { Generic threadvar support }
  1248. {$i threadvr.inc}
  1249. {$endif FPC_SECTION_THREADVARS}
  1250. {$ifdef DISABLE_NO_THREAD_MANAGER}
  1251. { OS Dependent implementation }
  1252. {$i systhrd.inc}
  1253. {$endif DISABLE_NO_THREAD_MANAGER}
  1254. {$endif FPC_HAS_FEATURE_THREADING and not cpujvm}
  1255. {*****************************************************************************
  1256. File Handling
  1257. *****************************************************************************}
  1258. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1259. { Allow slash and backslash as separators }
  1260. procedure DoDirSeparators(var p: PAnsiChar; inplace: boolean = true);
  1261. var
  1262. i : longint;
  1263. len : sizeint;
  1264. newp : PAnsiChar;
  1265. begin
  1266. len:=length(p);
  1267. newp:=nil;
  1268. for i:=0 to len do
  1269. if p[i] in AllowDirectorySeparators then
  1270. begin
  1271. if not inplace and
  1272. not assigned(newp) then
  1273. begin
  1274. getmem(newp,len+1);
  1275. move(p^,newp^,len+1);
  1276. p:=newp;
  1277. end;
  1278. p[i]:=DirectorySeparator;
  1279. end;
  1280. end;
  1281. procedure DoDirSeparators(var p: pwidechar; inplace: boolean = true);
  1282. var
  1283. i : longint;
  1284. len : sizeint;
  1285. newp : pwidechar;
  1286. begin
  1287. len:=length(p);
  1288. newp:=nil;
  1289. for i:=0 to len do
  1290. if (ord(p[i])<255) and
  1291. (ansichar(ord(p[i])) in AllowDirectorySeparators) then
  1292. begin
  1293. if not inplace and
  1294. not assigned(newp) then
  1295. begin
  1296. getmem(newp,(len+1)*2);
  1297. move(p^,newp^,(len+1)*2);
  1298. p:=newp;
  1299. end;
  1300. p[i]:=DirectorySeparator;
  1301. end;
  1302. end;
  1303. procedure DoDirSeparators(var p:shortstring);
  1304. var
  1305. i : longint;
  1306. begin
  1307. for i:=1 to length(p) do
  1308. if p[i] in AllowDirectorySeparators then
  1309. p[i]:=DirectorySeparator;
  1310. end;
  1311. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1312. procedure DoDirSeparators(var ps:RawByteString);
  1313. var
  1314. i : longint;
  1315. p : PAnsiChar;
  1316. unique : boolean;
  1317. begin
  1318. unique:=false;
  1319. for i:=1 to length(ps) do
  1320. if ps[i] in AllowDirectorySeparators then
  1321. begin
  1322. if not unique then
  1323. begin
  1324. uniquestring(ps);
  1325. p:=PAnsiChar(ps);
  1326. unique:=true;
  1327. end;
  1328. p[i-1]:=DirectorySeparator;
  1329. end;
  1330. end;
  1331. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1332. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  1333. procedure DoDirSeparators(var ps:UnicodeString);
  1334. var
  1335. i : longint;
  1336. p : pwidechar;
  1337. unique : boolean;
  1338. begin
  1339. unique:=false;
  1340. for i:=1 to length(ps) do
  1341. if ps[i] in AllowDirectorySeparators then
  1342. begin
  1343. if not unique then
  1344. begin
  1345. uniquestring(ps);
  1346. p:=pwidechar(ps);
  1347. unique:=true;
  1348. end;
  1349. p[i-1]:=DirectorySeparator;
  1350. end;
  1351. end;
  1352. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  1353. {$endif FPC_HAS_FEATURE_FILEIO}
  1354. { OS dependent low level file functions }
  1355. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1356. {$i sysfile.inc}
  1357. {$ifndef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1358. {$ifdef FPC_ANSI_TEXTFILEREC}
  1359. procedure do_open(var f; p: pansichar; flags: longint; pchangeable: boolean);
  1360. var
  1361. u: UnicodeString;
  1362. begin
  1363. widestringmanager.Ansi2UnicodeMoveProc(p,DefaultFileSystemCodePage,u,length(p));
  1364. do_open(f,pwidechar(u),flags,true);
  1365. end;
  1366. procedure do_erase(p: pansichar; pchangeable: boolean);
  1367. var
  1368. u: UnicodeString;
  1369. begin
  1370. widestringmanager.Ansi2UnicodeMoveProc(p,DefaultFileSystemCodePage,u,length(p));
  1371. do_erase(pwidechar(u),true);
  1372. end;
  1373. procedure do_rename(src, dst: pansichar; srcchangeable, dstchangeable: boolean);
  1374. var
  1375. usrc, udst: UnicodeString;
  1376. begin
  1377. widestringmanager.Ansi2UnicodeMoveProc(src,DefaultFileSystemCodePage,usrc,length(src));
  1378. widestringmanager.Ansi2UnicodeMoveProc(dst,DefaultFileSystemCodePage,udst,length(dst));
  1379. do_rename(pwidechar(usrc),pwidechar(udst),true,true);
  1380. end;
  1381. procedure do_rename(src: pansichar; dst: pwidechar; srcchangeable, dstchangeable: boolean);
  1382. var
  1383. usrc: UnicodeString;
  1384. begin
  1385. widestringmanager.Ansi2UnicodeMoveProc(src,DefaultFileSystemCodePage,usrc,length(src));
  1386. do_rename(pwidechar(usrc),dst,true,dstchangeable);
  1387. end;
  1388. {$endif FPC_ANSI_TEXTFILEREC}
  1389. {$endif not FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1390. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1391. {$ifndef FPC_ANSI_TEXTFILEREC}
  1392. procedure do_open(var f; p: pwidechar; flags: longint; pchangeable: boolean);
  1393. var
  1394. s: RawByteString;
  1395. begin
  1396. widestringmanager.Unicode2AnsiMoveProc(p,s,DefaultFileSystemCodePage,length(p));
  1397. do_open(f,pansichar(s),flags,true);
  1398. end;
  1399. procedure do_erase(p: pwidechar; pchangeable: boolean);
  1400. var
  1401. s: RawByteString;
  1402. begin
  1403. widestringmanager.Unicode2AnsiMoveProc(p,s,DefaultFileSystemCodePage,length(p));
  1404. do_erase(pansichar(s),true);
  1405. end;
  1406. procedure do_rename(src, dst: pwidechar; srcchangeable, dstchangeable: boolean);
  1407. var
  1408. rsrc, rdst: RawByteString;
  1409. begin
  1410. widestringmanager.Unicode2AnsiMoveProc(src,rsrc,DefaultFileSystemCodePage,length(src));
  1411. widestringmanager.Unicode2AnsiMoveProc(dst,rdst,DefaultFileSystemCodePage,length(dst));
  1412. do_rename(pansichar(rsrc),pansichar(rdst),true,true);
  1413. end;
  1414. procedure do_rename(src: pwidechar; dst: pansichar; srcchangeable, dstchangeable: boolean);
  1415. var
  1416. rsrc: RawByteString;
  1417. begin
  1418. widestringmanager.Unicode2AnsiMoveProc(src,rsrc,DefaultFileSystemCodePage,length(src));
  1419. do_rename(pansichar(rsrc),dst,true,dstchangeable);
  1420. end;
  1421. {$endif not FPC_ANSI_TEXTFILEREC}
  1422. {$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1423. {$endif FPC_HAS_FEATURE_FILEIO}
  1424. (* already declared earlier in system.pp for java
  1425. { helper for targets supporting no ansistrings, it is used
  1426. by non-ansistring code }
  1427. function min(v1,v2 : SizeInt) : SizeInt;
  1428. begin
  1429. if v1<v2 then
  1430. result:=v1
  1431. else
  1432. result:=v2;
  1433. end;
  1434. *)
  1435. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  1436. { Text file }
  1437. {$i text.inc}
  1438. {$endif FPC_HAS_FEATURE_TEXTIO}
  1439. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1440. { Untyped file }
  1441. {$i file.inc}
  1442. { Typed file }
  1443. {$i typefile.inc}
  1444. {$endif FPC_HAS_FEATURE_FILEIO}
  1445. {*****************************************************************************
  1446. Directory Handling
  1447. *****************************************************************************}
  1448. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1449. { OS dependent dir functions }
  1450. {$i sysdir.inc}
  1451. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1452. {$ifndef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1453. procedure do_getdir(drivenr : byte;var dir : rawbytestring);
  1454. var
  1455. u: unicodestring;
  1456. begin
  1457. Do_getdir(drivenr,u);
  1458. widestringmanager.Unicode2AnsiMoveProc(pwidechar(u),dir,DefaultRTLFileSystemCodePage,length(u));
  1459. end;
  1460. {$endif FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1461. Procedure MkDir(Const s: RawByteString);[IOCheck];
  1462. Begin
  1463. If (s='') or (InOutRes <> 0) then
  1464. exit;
  1465. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1466. Do_mkdir(ToSingleByteFileSystemEncodedFileName(S));
  1467. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1468. Do_mkdir(S);
  1469. {$endif}
  1470. end;
  1471. Procedure RmDir(Const s: RawByteString);[IOCheck];
  1472. Begin
  1473. If (s='') or (InOutRes <> 0) then
  1474. exit;
  1475. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1476. Do_rmdir(ToSingleByteFileSystemEncodedFileName(S));
  1477. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1478. Do_rmdir(S);
  1479. {$endif}
  1480. End;
  1481. Procedure ChDir(Const s: RawByteString);[IOCheck];
  1482. Begin
  1483. If (s='') or (InOutRes <> 0) then
  1484. exit;
  1485. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1486. Do_chdir(ToSingleByteFileSystemEncodedFileName(S));
  1487. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1488. Do_chdir(S);
  1489. {$endif}
  1490. End;
  1491. Procedure getdir(drivenr:byte;Var dir:rawbytestring);
  1492. begin
  1493. Do_getdir(drivenr,dir);
  1494. { we should return results in the DefaultRTLFileSystemCodePage -> convert if
  1495. necessary }
  1496. setcodepage(dir,DefaultRTLFileSystemCodePage,true);
  1497. end;
  1498. { the generic shortstring ones are only implemented elsewhere for systems *not*
  1499. supporting ansi/unicodestrings; for now assume there are no systems that
  1500. support unicodestrings but not ansistrings }
  1501. { avoid double string conversions }
  1502. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1503. function GetDirStrFromShortstring(const s: shortstring): RawByteString;
  1504. begin
  1505. GetDirStrFromShortstring:=ToSingleByteFileSystemEncodedFileName(ansistring(s));
  1506. end;
  1507. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1508. function GetDirStrFromShortstring(const s: shortstring): UnicodeString;
  1509. begin
  1510. GetDirStrFromShortstring:=s;
  1511. end;
  1512. {$endif FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1513. Procedure MkDir(Const s: shortstring);[IOCheck];
  1514. Begin
  1515. If (s='') or (InOutRes <> 0) then
  1516. exit;
  1517. Do_mkdir(GetDirStrFromShortstring(S));
  1518. End;
  1519. Procedure RmDir(Const s: shortstring);[IOCheck];
  1520. Begin
  1521. If (s='') or (InOutRes <> 0) then
  1522. exit;
  1523. Do_rmdir(GetDirStrFromShortstring(S));
  1524. End;
  1525. Procedure ChDir(Const s: shortstring);[IOCheck];
  1526. Begin
  1527. If (s='') or (InOutRes <> 0) then
  1528. exit;
  1529. Do_chdir(GetDirStrFromShortstring(S));
  1530. End;
  1531. Procedure getdir(drivenr:byte;Var dir:shortstring);
  1532. var
  1533. s: rawbytestring;
  1534. begin
  1535. Do_getdir(drivenr,s);
  1536. if length(s)<=high(dir) then
  1537. dir:=s
  1538. else
  1539. inoutres:=3;
  1540. end;
  1541. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1542. {$if defined(FPC_HAS_FEATURE_WIDESTRINGS)}
  1543. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1544. { overloads required for mkdir/rmdir/chdir to ensure that the string is
  1545. converted to the right code page }
  1546. procedure do_mkdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1547. begin
  1548. do_mkdir(ToSingleByteFileSystemEncodedFileName(s));
  1549. end;
  1550. procedure do_rmdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1551. begin
  1552. do_rmdir(ToSingleByteFileSystemEncodedFileName(s));
  1553. end;
  1554. procedure do_chdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1555. begin
  1556. do_chdir(ToSingleByteFileSystemEncodedFileName(s));
  1557. end;
  1558. procedure do_getdir(drivenr : byte;var dir : unicodestring);
  1559. var
  1560. s: rawbytestring;
  1561. begin
  1562. Do_getdir(drivenr,s);
  1563. dir:=s;
  1564. end;
  1565. {$endif FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1566. Procedure MkDir(Const s: UnicodeString);[IOCheck];
  1567. Begin
  1568. if (s='') or (InOutRes <> 0) then
  1569. exit;
  1570. Do_mkdir(S);
  1571. End;
  1572. Procedure RmDir(Const s: UnicodeString);[IOCheck];
  1573. Begin
  1574. if (s='') or (InOutRes <> 0) then
  1575. exit;
  1576. Do_rmdir(S);
  1577. End;
  1578. Procedure ChDir(Const s: UnicodeString);[IOCheck];
  1579. Begin
  1580. if (s='') or (InOutRes <> 0) then
  1581. exit;
  1582. Do_chdir(S);
  1583. End;
  1584. Procedure getdir(drivenr:byte;Var dir:unicodestring);
  1585. begin
  1586. Do_getdir(drivenr,dir);
  1587. end;
  1588. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1589. {$endif FPC_HAS_FEATURE_FILEIO}
  1590. {*****************************************************************************
  1591. Resources support
  1592. *****************************************************************************}
  1593. {$i sysres.inc}
  1594. (*
  1595. const
  1596. CtrlBreakHandler: TCtrlBreakHandler = nil;
  1597. {$IFNDEF FPC_HAS_SETCTRLBREAKHANDLER}
  1598. (* It is possible to provide platform specific implementation performing *)
  1599. (* special initialization; default implementation just sets the procedural *)
  1600. (* variable to make it available for use from the exception handler. *)
  1601. function SysSetCtrlBreakHandler (Handler: TCtrlBreakHandler): TCtrlBreakHandler;
  1602. begin
  1603. (* Return either nil or previous handler *)
  1604. SysSetCtrlBreakHandler := CtrlBreakHandler;
  1605. CtrlBreakHandler := Handler;
  1606. end;
  1607. {$ENDIF FPC_HAS_SETCTRLBREAKHANDLER}
  1608. *)