system.inc 51 KB

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