2
0

jsystem.inc 50 KB

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