jsystem.inc 50 KB

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