system.inc 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956
  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{$ifndef FPC_HAS_INDIRECT_ENTRY_INFORMATION}; external name '__stklen';{$else} = 0;{$endif}
  87. {$endif FPC_NO_GENERIC_STACK_CHECK}
  88. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  89. EntryInformation: TEntryInformation;
  90. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  91. var
  92. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  93. {$ifdef FPC_HAS_RESSTRINITS}
  94. FPCResStrInitTables : Pointer;public name '_FPC_ResStrInitTables';
  95. {$endif FPC_HAS_RESSTRINITS}
  96. FPCResourceStringTables : Pointer;public name '_FPC_ResourceStringTables';
  97. FPCResLocation : Pointer;public name'_FPC_ResLocation';
  98. {$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  99. {$ifdef FPC_HAS_RESSTRINITS}
  100. FPCResStrInitTablesVar : record end; external name 'FPC_RESSTRINITTABLES';
  101. FPCResStrInitTables : Pointer = @FPCResStrInitTablesVar;public name '_FPC_ResStrInitTables';
  102. {$endif FPC_HAS_RESSTRINITS}
  103. FPCResourceStringTablesVar : record end; External Name 'FPC_RESOURCESTRINGTABLES';
  104. FPCResourceStringTables : Pointer = @FPCResourceStringTablesVar;public name '_FPC_ResourceStringTables';
  105. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  106. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  107. procedure SetupEntryInformation(constref info: TEntryInformation);[public,alias:'_FPC_SetupEntryInformation'];
  108. begin
  109. EntryInformation := info;
  110. FPCResStrInitTables := info.ResStrInitTables;
  111. FPCResourceStringTables := info.ResourceStringTables;
  112. FPCResLocation := info.ResLocation;
  113. {$ifdef FPC_SYSTEM_HAS_OSSETUPENTRYINFORMATION}
  114. OSSetupEntryInformation(info);
  115. {$endif FPC_SYSTEM_HAS_OSSETUPENTRYINFORMATION}
  116. end;
  117. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  118. { checks whether the given suggested size for the stack of the current
  119. thread is acceptable. If this is the case, returns it unaltered.
  120. Otherwise it should return an acceptable value.
  121. Operating systems that automatically expand their stack on demand, should
  122. simply return a very large value.
  123. Operating systems which do not have a possibility to retrieve stack size
  124. information, should simply return the given stklen value (This is the default
  125. implementation).
  126. }
  127. {$ifdef FPC_HAS_FEATURE_STACKCHECK}
  128. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt; forward;
  129. {$endif FPC_HAS_FEATURE_STACKCHECK}
  130. {*****************************************************************************
  131. OS dependent Helpers/Syscalls
  132. *****************************************************************************}
  133. { for some OSes do_isdevice is defined in sysos.inc, but for others (win32)
  134. it isn't, and is used before the actual definition is encountered }
  135. {$ifdef FPC_HAS_FEATURE_FILEIO}
  136. function do_isdevice(handle:thandle):boolean;forward;
  137. {$endif FPC_HAS_FEATURE_FILEIO}
  138. {$i sysos.inc}
  139. {****************************************************************************
  140. Include processor specific routines
  141. ****************************************************************************}
  142. {$ifdef FPC_USE_LIBC}
  143. { Under Haiku, bcopy cause a problem when searching for include file
  144. in the compiler. So, we use the internal implementation for now
  145. under BeOS and Haiku. }
  146. {$ifndef BEOS}
  147. { prefer libc implementations over our own, as they're most likely faster }
  148. {$i cgeneric.inc}
  149. { is now declared as external reference to another routine in the interface }
  150. {$i cgenstr.inc}
  151. {$endif}
  152. {$endif FPC_USE_LIBC}
  153. {$ifdef cpui386}
  154. {$ifdef SYSPROCDEFINED}
  155. {$Error Can't determine processor type !}
  156. {$endif}
  157. {$i i386.inc} { Case dependent, don't change }
  158. {$define SYSPROCDEFINED}
  159. {$endif cpui386}
  160. {$ifdef cpui8086}
  161. {$ifdef SYSPROCDEFINED}
  162. {$Error Can't determine processor type !}
  163. {$endif}
  164. {$i i8086.inc} { Case dependent, don't change }
  165. {$define SYSPROCDEFINED}
  166. {$endif cpui8086}
  167. {$ifdef cpum68k}
  168. {$ifdef SYSPROCDEFINED}
  169. {$Error Can't determine processor type !}
  170. {$endif}
  171. {$i m68k.inc} { Case dependent, don't change }
  172. {$define SYSPROCDEFINED}
  173. {$endif cpum68k}
  174. {$ifdef cpux86_64}
  175. {$ifdef SYSPROCDEFINED}
  176. {$Error Can't determine processor type !}
  177. {$endif}
  178. {$i x86_64.inc} { Case dependent, don't change }
  179. {$define SYSPROCDEFINED}
  180. {$endif cpux86_64}
  181. {$ifdef cpupowerpc32}
  182. {$ifdef SYSPROCDEFINED}
  183. {$Error Can't determine processor type !}
  184. {$endif}
  185. {$i powerpc.inc} { Case dependent, don't change }
  186. {$define SYSPROCDEFINED}
  187. {$endif cpupowerpc32}
  188. {$ifdef cpupowerpc64}
  189. {$ifdef SYSPROCDEFINED}
  190. {$Error Can't determine processor type !}
  191. {$endif}
  192. {$i powerpc64.inc} { Case dependent, don't change }
  193. {$define SYSPROCDEFINED}
  194. {$endif cpupowerpc64}
  195. {$ifdef cpualpha}
  196. {$ifdef SYSPROCDEFINED}
  197. {$Error Can't determine processor type !}
  198. {$endif}
  199. {$i alpha.inc} { Case dependent, don't change }
  200. {$define SYSPROCDEFINED}
  201. {$endif cpualpha}
  202. {$ifdef cpuiA64}
  203. {$ifdef SYSPROCDEFINED}
  204. {$Error Can't determine processor type !}
  205. {$endif}
  206. {$i ia64.inc} { Case dependent, don't change }
  207. {$define SYSPROCDEFINED}
  208. {$endif cpuiA64}
  209. {$ifdef cpusparc}
  210. {$ifdef SYSPROCDEFINED}
  211. {$Error Can't determine processor type !}
  212. {$endif}
  213. {$i sparc.inc} { Case dependent, don't change }
  214. {$define SYSPROCDEFINED}
  215. {$endif cpusparc}
  216. {$ifdef cpuarm}
  217. {$ifdef SYSPROCDEFINED}
  218. {$Error Can't determine processor type !}
  219. {$endif}
  220. {$i armdefines.inc}
  221. {$if defined(CPUTHUMB2)}
  222. {$i thumb2.inc} { Case dependent, don't change }
  223. {$else}
  224. {$if defined(CPUTHUMB)}
  225. {$i thumb.inc} { Case dependent, don't change }
  226. {$else}
  227. {$i arm.inc} { Case dependent, don't change }
  228. {$endif}
  229. {$endif}
  230. {$define SYSPROCDEFINED}
  231. {$endif cpuarm}
  232. {$ifdef cpuavr}
  233. {$ifdef SYSPROCDEFINED}
  234. {$Error Can't determine processor type !}
  235. {$endif}
  236. {$i avr.inc} { Case dependent, don't change }
  237. {$define SYSPROCDEFINED}
  238. {$endif cpuavr}
  239. {$ifdef cpumipsel}
  240. {$ifdef SYSPROCDEFINED}
  241. {$Error Can't determine processor type !}
  242. {$endif}
  243. { there is no mipsel.inc, we use mips.inc instead }
  244. {$i mips.inc} { Case dependent, don't change }
  245. {$define SYSPROCDEFINED}
  246. {$else not cpumipsel}
  247. {$ifdef cpumips}
  248. {$ifdef SYSPROCDEFINED}
  249. {$Error Can't determine processor type !}
  250. {$endif}
  251. {$i mips.inc} { Case dependent, don't change }
  252. {$define SYSPROCDEFINED}
  253. {$endif cpumips}
  254. {$endif not cpumipsel}
  255. {$ifdef cpuaarch64}
  256. {$ifdef SYSPROCDEFINED}
  257. {$Error Can't determine processor type !}
  258. {$endif}
  259. {$i aarch64.inc} { Case dependent, don't change }
  260. {$define SYSPROCDEFINED}
  261. {$endif cpuaarch64}
  262. {$ifndef SYSPROCDEFINED}
  263. {$Error Can't determine processor type !}
  264. {$endif}
  265. procedure fillchar(var x;count : {$ifdef FILLCHAR_HAS_SIZEUINT_COUNT}SizeUInt{$else}SizeInt{$endif};value : boolean);
  266. begin
  267. fillchar(x,count,byte(value));
  268. end;
  269. procedure fillchar(var x;count : {$ifdef FILLCHAR_HAS_SIZEUINT_COUNT}SizeUInt{$else}SizeInt{$endif};value : char);
  270. begin
  271. fillchar(x,count,byte(value));
  272. end;
  273. procedure FillByte (var x;count : {$ifdef FILLCHAR_HAS_SIZEUINT_COUNT}SizeUInt{$else}SizeInt{$endif};value : byte );
  274. begin
  275. FillChar (X,Count,VALUE);
  276. end;
  277. function IndexChar(Const buf;len:SizeInt;b:char):SizeInt;
  278. begin
  279. IndexChar:=IndexByte(Buf,Len,byte(B));
  280. end;
  281. function CompareChar(Const buf1,buf2;len:SizeInt):SizeInt;
  282. begin
  283. CompareChar:=CompareByte(buf1,buf2,len);
  284. end;
  285. procedure fpc_zeromem(p:pointer;len:sizeuint);
  286. begin
  287. FillChar(p^,len,0);
  288. end;
  289. procedure fpc_fillmem(out data;len:sizeuint;b : byte);
  290. begin
  291. FillByte(data,len,b);
  292. end;
  293. { Include generic pascal only routines which are not defined in the processor
  294. specific include file }
  295. {$I generic.inc}
  296. {****************************************************************************
  297. Set Handling
  298. ****************************************************************************}
  299. { Include set support which is processor specific}
  300. {$i set.inc}
  301. { Include generic pascal routines for sets if the processor }
  302. { specific routines are not available. }
  303. {$i genset.inc}
  304. {****************************************************************************
  305. Math Routines
  306. ****************************************************************************}
  307. function Hi(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  308. begin
  309. Hi := b shr 4
  310. end;
  311. function Lo(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  312. begin
  313. Lo := b and $0f
  314. end;
  315. Function Swap (X : Word) : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  316. Begin
  317. Swap := SwapEndian(X);
  318. End;
  319. Function Swap (X : Integer) : Integer;{$ifdef SYSTEMINLINE}inline;{$endif}
  320. Begin
  321. Swap := SwapEndian(X);
  322. End;
  323. Function Swap (X : Longint) : Longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  324. Begin
  325. Swap:=(X shl 16) + (X shr 16);
  326. End;
  327. Function Swap (X : Cardinal) : Cardinal;{$ifdef SYSTEMINLINE}inline;{$endif}
  328. Begin
  329. Swap:=(X shl 16) + (X shr 16);
  330. End;
  331. Function Swap (X : QWord) : QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  332. Begin
  333. Swap:=(X shl 32) + (X shr 32);
  334. End;
  335. Function Swap (X : Int64) : Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  336. Begin
  337. Swap:=(X shl 32) + (X shr 32);
  338. End;
  339. {$ifdef SUPPORT_DOUBLE}
  340. operator := (b:real48) d:double;{$ifdef SYSTEMINLINE}inline;{$endif}
  341. begin
  342. D:=real2double(b);
  343. end;
  344. {$endif SUPPORT_DOUBLE}
  345. {$ifdef SUPPORT_EXTENDED}
  346. operator := (b:real48) e:extended;{$ifdef SYSTEMINLINE}inline;{$endif}
  347. begin
  348. e:=real2double(b);
  349. end;
  350. {$endif SUPPORT_EXTENDED}
  351. {$ifndef FPUNONE}
  352. {$ifdef FPC_USE_LIBC}
  353. { Include libc versions }
  354. {$i cgenmath.inc}
  355. {$endif FPC_USE_LIBC}
  356. { Include processor specific routines }
  357. {$I math.inc}
  358. { Include generic version }
  359. {$I genmath.inc}
  360. {$endif}
  361. {$i gencurr.inc}
  362. function aligntoptr(p : pointer) : pointer;inline;
  363. begin
  364. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  365. result:=align(p,sizeof(p));
  366. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  367. result:=p;
  368. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  369. end;
  370. {****************************************************************************
  371. Subroutines for String handling
  372. ****************************************************************************}
  373. { Needs to be before RTTI handling }
  374. {$i sstrings.inc}
  375. { requires sstrings.inc for initval }
  376. {$I int64p.inc}
  377. {$I int64.inc}
  378. {Requires int64.inc, since that contains the VAL functions for int64 and qword}
  379. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  380. {$i astrings.inc}
  381. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  382. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  383. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  384. {$i wstrings.inc}
  385. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  386. {$i ustrings.inc}
  387. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  388. {$i aliases.inc}
  389. {*****************************************************************************
  390. Dynamic Array support
  391. *****************************************************************************}
  392. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  393. {$i dynarr.inc}
  394. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  395. {*****************************************************************************
  396. Object Pascal support
  397. *****************************************************************************}
  398. {$ifdef FPC_HAS_FEATURE_CLASSES}
  399. {$i objpas.inc}
  400. {$endif FPC_HAS_FEATURE_CLASSES}
  401. {*****************************************************************************
  402. Variant support
  403. *****************************************************************************}
  404. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  405. {$i variant.inc}
  406. {$endif FPC_HAS_FEATURE_VARIANTS}
  407. {****************************************************************************
  408. Run-Time Type Information (RTTI)
  409. ****************************************************************************}
  410. {$ifdef FPC_HAS_FEATURE_RTTI}
  411. {$i rtti.inc}
  412. {$endif FPC_HAS_FEATURE_RTTI}
  413. {$if defined(FPC_HAS_FEATURE_RANDOM)}
  414. { Pascal translation of https://github.com/dajobe/libmtwist }
  415. {* -*- Mode: c; c-basic-offset: 2 -*-
  416. *
  417. * mt.c - Mersenne Twister functions
  418. *
  419. * This is free and unencumbered software released into the public domain.
  420. *
  421. * Anyone is free to copy, modify, publish, use, compile, sell, or
  422. * distribute this software, either in source code form or as a compiled
  423. * binary, for any purpose, commercial or non-commercial, and by any
  424. * means.
  425. *
  426. * In jurisdictions that recognize copyright laws, the author or authors
  427. * of this software dedicate any and all copyright interest in the
  428. * software to the public domain. We make this dedication for the benefit
  429. * of the public at large and to the detriment of our heirs and
  430. * successors. We intend this dedication to be an overt act of
  431. * relinquishment in perpetuity of all present and future rights to this
  432. * software under copyright law.
  433. *
  434. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  435. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  436. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  437. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  438. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  439. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  440. * OTHER DEALINGS IN THE SOFTWARE.
  441. *
  442. * For more information, please refer to <http://unlicense.org/>
  443. *
  444. *}
  445. {$R-} {range checking off}
  446. {$Q-} {overflow checking off}
  447. const
  448. MTWIST_N = 624;
  449. MTWIST_M = 397;
  450. MT_STATIC_SEED = 5489;
  451. MTWIST_UPPER_MASK = cardinal($80000000);
  452. MTWIST_LOWER_MASK = cardinal($7FFFFFFF);
  453. MTWIST_MATRIX_A = cardinal($9908B0DF);
  454. var
  455. mt_state: array[0..MTWIST_N-1] of cardinal;
  456. const
  457. mt_index: cardinal = MTWIST_N+1;
  458. function MTWIST_MIXBITS(u, v: cardinal): cardinal; inline;
  459. begin
  460. result:=(u and MTWIST_UPPER_MASK) or (v and MTWIST_LOWER_MASK);
  461. end;
  462. function MTWIST_TWIST(u, v: cardinal): cardinal; inline;
  463. begin
  464. { the construct at the end is equivalent to
  465. if odd(v) then
  466. MTWIST_MATRIX_A
  467. else
  468. 0
  469. }
  470. result:=(MTWIST_MIXBITS(u,v) shr 1) xor (cardinal(-(v and 1)) and MTWIST_MATRIX_A);
  471. end;
  472. procedure mtwist_init(seed: cardinal);
  473. var
  474. i: longint;
  475. begin
  476. mt_state[0]:=seed;
  477. for i:=1 to MTWIST_N-1 do
  478. mt_state[i]:=cardinal(1812433253) * (mt_state[i-1] xor (mt_state[i-1] shr 30)) + i;
  479. { still need to update the state }
  480. mt_index:=MTWIST_N;
  481. end;
  482. procedure mtwist_update_state;
  483. var
  484. count: longint;
  485. begin
  486. { The original C code uses pointers, doesn't play nice with JVM backend;
  487. it counts from N-M+1 downto 0 (0 not included) for the first loop, which
  488. should initialise the first N-M+1 elements -- doing so gives the wrong
  489. results though (different from the old generator, and it also doesn't
  490. match the algorithm description), so we use only N-M iterations. They don't
  491. seem to test this one element and its value does not impact subsequent
  492. numbers, so it's probably a bug in their implementation.
  493. }
  494. for count:=0 to MTWIST_N-MTWIST_M-1 do
  495. mt_state[count]:=mt_state[count+MTWIST_M] xor MTWIST_TWIST(mt_state[count],mt_state[count+1]);
  496. for count:=MTWIST_N-MTWIST_M to MTWIST_N-2 do
  497. mt_state[count]:=mt_state[count+(MTWIST_M-MTWIST_N)] xor MTWIST_TWIST(mt_state[count],mt_state[count+1]);
  498. mt_state[MTWIST_N-1]:=mt_state[MTWIST_M-1] xor MTWIST_TWIST(mt_state[MTWIST_N-1],mt_state[0]);
  499. mt_index:=0;
  500. end;
  501. function mtwist_u32rand: cardinal;
  502. var
  503. l_index :cardinal;
  504. begin
  505. l_index:=mt_index;
  506. inc(mt_index);
  507. if (RandSeed<>OldRandSeed) or
  508. (l_index>=MTWIST_N+1) then
  509. begin
  510. mtwist_init(RandSeed);
  511. { Detect resets of randseed
  512. This will break if someone coincidentally uses not(randseed) as the
  513. next randseed, but it's much more common that you will reset randseed
  514. to the same value as before to regenerate the same sequence of numbers
  515. }
  516. RandSeed:=not(RandSeed);
  517. OldRandSeed:=RandSeed;
  518. l_index:=MTWIST_N;
  519. end;
  520. if mt_index=MTWIST_N then
  521. begin
  522. mtwist_update_state;
  523. l_index:=0;
  524. end;
  525. result:=mt_state[l_index];
  526. result:=result xor (result shr 11);
  527. result:=result xor ((result shl 7) and cardinal($9D2C5680));
  528. result:=result xor ((result shl 15) and cardinal($EFC60000));
  529. result:=result xor (result shr 18);
  530. end;
  531. function random(l:longint): longint;
  532. begin
  533. { otherwise we can return values = l (JM) }
  534. if (l < 0) then
  535. inc(l);
  536. random := longint((int64(mtwist_u32rand)*l) shr 32);
  537. end;
  538. function random(l:int64): int64;
  539. begin
  540. { always call random, so the random generator cycles (TP-compatible) (JM) }
  541. { also do it in two separate steps, so the order in which the two calls
  542. are performed is predictable (JM) }
  543. random:=mtwist_u32rand;
  544. random:=random or ((qword(mtwist_u32rand) shl 32) and high(int64));
  545. if (l<>0) then
  546. random := random mod l
  547. else
  548. random := 0;
  549. end;
  550. {$ifndef FPUNONE}
  551. function random: extended;
  552. begin
  553. random := mtwist_u32rand * (extended(1.0)/(int64(1) shl 32));
  554. end;
  555. {$endif}
  556. {$endif FPC_HAS_FEATURE_RANDOM}
  557. {****************************************************************************
  558. Memory Management
  559. ****************************************************************************}
  560. {$ifndef FPC_SYSTEM_HAS_PTR}
  561. Function Ptr(sel,off : {$ifdef CPU16}Word{$else}Longint{$endif}) : farpointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  562. Begin
  563. ptr:=farpointer((sel shl 4)+off);
  564. End;
  565. {$endif not FPC_SYSTEM_HAS_PTR}
  566. {$ifndef FPC_SYSTEM_HAS_CSEG}
  567. Function CSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  568. Begin
  569. Cseg:=0;
  570. End;
  571. {$endif not FPC_SYSTEM_HAS_CSEG}
  572. {$ifndef FPC_SYSTEM_HAS_DSEG}
  573. Function DSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  574. Begin
  575. Dseg:=0;
  576. End;
  577. {$endif not FPC_SYSTEM_HAS_DSEG}
  578. {$ifndef FPC_SYSTEM_HAS_SSEG}
  579. Function SSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  580. Begin
  581. Sseg:=0;
  582. End;
  583. {$endif not FPC_SYSTEM_HAS_SSEG}
  584. {$push}
  585. {$R-}
  586. {$I-}
  587. {$Q-}
  588. {*****************************************************************************
  589. Miscellaneous
  590. *****************************************************************************}
  591. {$ifndef FPC_SYSTEM_HAS_STACKTOP}
  592. function StackTop: pointer;
  593. begin
  594. result:=StackBottom+StackLength;
  595. end;
  596. {$endif FPC_SYSTEM_HAS_STACKTOP}
  597. {$ifndef FPC_SYSTEM_HAS_GET_PC_ADDR}
  598. { This provides a dummy implementation
  599. of get_pc_addr function, for CPU's that don't need
  600. the instruction address to walk the stack. }
  601. function get_pc_addr : codepointer;
  602. begin
  603. get_pc_addr:=nil;
  604. end;
  605. {$endif ndef FPC_SYSTEM_HAS_GET_PC_ADDR}
  606. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_STACKINFO}
  607. { This provides a simple implementation
  608. of get_caller_stackinfo procedure,
  609. using get_caller_addr and get_caller_frame
  610. functions. }
  611. procedure get_caller_stackinfo(var framebp : pointer; var addr : codepointer);
  612. var
  613. nextbp : pointer;
  614. nextaddr : codepointer;
  615. begin
  616. nextbp:=get_caller_frame(framebp,addr);
  617. nextaddr:=get_caller_addr(framebp,addr);
  618. framebp:=nextbp;
  619. addr:=nextaddr;
  620. end;
  621. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_STACKINFO}
  622. procedure fpc_objecterror; compilerproc;
  623. begin
  624. HandleErrorAddrFrameInd(210,get_pc_addr,get_frame);
  625. end;
  626. procedure fpc_rangeerror;[public,alias:'FPC_RANGEERROR']; compilerproc;
  627. begin
  628. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  629. end;
  630. procedure fpc_divbyzero;[public,alias:'FPC_DIVBYZERO']; compilerproc;
  631. begin
  632. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  633. end;
  634. procedure fpc_overflow;[public,alias:'FPC_OVERFLOW']; compilerproc;
  635. begin
  636. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  637. end;
  638. procedure fpc_threaderror; [public,alias:'FPC_THREADERROR'];
  639. begin
  640. HandleErrorAddrFrameInd(6,get_pc_addr,get_frame);
  641. end;
  642. procedure fpc_invalidpointer; [public,alias:'FPC_INVALIDPOINTER'];
  643. begin
  644. HandleErrorAddrFrameInd(216,get_pc_addr,get_frame);
  645. end;
  646. procedure fpc_iocheck;[public,alias:'FPC_IOCHECK']; compilerproc;
  647. var
  648. l : longint;
  649. HInoutRes : PWord;
  650. begin
  651. HInOutRes:=@InoutRes;
  652. if HInOutRes^<>0 then
  653. begin
  654. l:=HInOutRes^;
  655. HInOutRes^:=0;
  656. HandleErrorAddrFrameInd(l,get_pc_addr,get_frame);
  657. end;
  658. end;
  659. Function IOResult:Word;
  660. var
  661. HInoutRes : PWord;
  662. Begin
  663. HInoutRes:=@InoutRes;
  664. IOResult:=HInOutRes^;
  665. HInOutRes^:=0;
  666. End;
  667. Function GetThreadID:TThreadID;{$ifdef SYSTEMINLINE}inline;{$endif}
  668. begin
  669. (* ThreadID is stored in a threadvar and made available in interface *)
  670. (* to allow setup of this value during thread initialization. *)
  671. GetThreadID := ThreadID;
  672. end;
  673. function fpc_safecallcheck(res : hresult) : hresult;[public,alias:'FPC_SAFECALLCHECK']; compilerproc; {$ifdef CPU86} register; {$endif}
  674. begin
  675. if res<0 then
  676. begin
  677. if assigned(SafeCallErrorProc) then
  678. SafeCallErrorProc(res,get_frame);
  679. HandleErrorAddrFrameInd(229,get_pc_addr,get_frame);
  680. end;
  681. result:=res;
  682. end;
  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. const
  716. maxunits=1024; { See also files.pas of the compiler source }
  717. type
  718. TInitFinalRec=record
  719. InitProc,
  720. FinalProc : TProcedure;
  721. end;
  722. TInitFinalTable = record
  723. TableCount,
  724. InitCount : {$if defined(VER2)}longint
  725. {$elseif defined(VER3_0)}sizeint
  726. {$else}ALUUInt{$endif}
  727. ;
  728. Procs : array[1..maxunits] of TInitFinalRec;
  729. end;
  730. PInitFinalTable = ^TInitFinalTable;
  731. {$ifndef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  732. var
  733. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  734. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  735. procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; compilerproc;
  736. var
  737. i : ALUUInt;
  738. {$ifdef DEBUG}
  739. pt : PInitFinalTable;
  740. {$endif}
  741. begin
  742. { call cpu/fpu initialisation routine }
  743. fpc_cpuinit;
  744. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  745. {$ifdef DEBUG}
  746. pt := PInitFinalTable(EntryInformation.InitFinalTable);
  747. {$endif}
  748. with PInitFinalTable(EntryInformation.InitFinalTable)^ do
  749. {$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  750. {$ifdef DEBUG}
  751. pt := @InitFinalTable;
  752. {$endif}
  753. with InitFinalTable do
  754. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  755. begin
  756. for i:=1 to ALUUInt(TableCount) do
  757. begin
  758. if assigned(Procs[i].InitProc) then
  759. Procs[i].InitProc();
  760. InitCount:=i;
  761. end;
  762. end;
  763. if assigned(InitProc) then
  764. TProcedure(InitProc)();
  765. end;
  766. procedure internal_initializeunits; external name 'FPC_INITIALIZEUNITS';
  767. procedure fpc_LibInitializeUnits;[public,alias:'FPC_LIBINITIALIZEUNITS'];compilerproc;
  768. begin
  769. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  770. IsLibrary:=true;
  771. { must also be set to true for packages when implemented }
  772. ModuleIsLib:=true;
  773. internal_initializeunits;
  774. {$endif FPC_HAS_FEATURE_DYNLIBS}
  775. end;
  776. {$ifdef CPUAVR}
  777. procedure FinalizeUnits; external name 'FPC_FINALIZE_FUNC_TABLE';
  778. {$else CPUAVR}
  779. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  780. begin
  781. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  782. with PInitFinalTable(EntryInformation.InitFinalTable)^ do
  783. {$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  784. with InitFinalTable do
  785. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  786. begin
  787. while (InitCount>0) do
  788. begin
  789. // we've to decrement the cound before calling the final. code
  790. // else a halt in the final. code leads to a endless loop
  791. dec(InitCount);
  792. if assigned(Procs[InitCount+1].FinalProc) then
  793. Procs[InitCount+1].FinalProc();
  794. end;
  795. end;
  796. end;
  797. {$endif CPUAVR}
  798. {*****************************************************************************
  799. Error / Exit / ExitProc
  800. *****************************************************************************}
  801. Procedure system_exit;forward;
  802. {$ifdef FPC_HAS_FEATURE_HEAP}
  803. {$ifndef HAS_MEMORYMANAGER}
  804. //not needed if independant memory manager
  805. Procedure FinalizeHeap;forward;
  806. {$endif HAS_MEMORYMANAGER}
  807. {$endif FPC_HAS_FEATURE_HEAP}
  808. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  809. procedure SysFlushStdIO;
  810. begin
  811. { Make sure that all output is written to the redirected file }
  812. if Textrec(Output).Mode=fmOutput then
  813. Flush(Output);
  814. if Textrec(ErrOutput).Mode=fmOutput then
  815. Flush(ErrOutput);
  816. if Textrec(stdout).Mode=fmOutput then
  817. Flush(stdout);
  818. if Textrec(StdErr).Mode=fmOutput then
  819. Flush(StdErr);
  820. end;
  821. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  822. Procedure InternalExit;
  823. var
  824. current_exit : Procedure;
  825. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  826. pstdout : ^Text;
  827. {$endif}
  828. {$if defined(MSWINDOWS) or defined(OS2)}
  829. i : longint;
  830. {$endif}
  831. Begin
  832. {$ifdef SYSTEMDEBUG}
  833. writeln('InternalExit');
  834. {$endif SYSTEMDEBUG}
  835. {$ifndef CPUAVR}
  836. while exitProc<>nil Do
  837. Begin
  838. InOutRes:=0;
  839. current_exit:=tProcedure(exitProc);
  840. exitProc:=nil;
  841. current_exit();
  842. End;
  843. {$endif CPUAVR}
  844. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  845. { the embedded system unit itself contains no routines for console i/o
  846. console i/o is done by the Consoleio unit which can do things like
  847. redirection to seriell i/o }
  848. {$ifndef EMBEDDED}
  849. { Show runtime error and exit }
  850. if WriteErrorsToStdErr then
  851. pstdout:=@stderr
  852. else
  853. pstdout:=@stdout;
  854. If erroraddr<>nil Then
  855. Begin
  856. Writeln(pstdout^,'Runtime error ',Errorcode,' at $',hexstr(erroraddr));
  857. { to get a nice symify }
  858. Writeln(pstdout^,BackTraceStrFunc(Erroraddr));
  859. dump_stack(pstdout^,ErrorBase,ErrorAddr);
  860. Writeln(pstdout^,'');
  861. End;
  862. SysFlushStdIO;
  863. {$endif EMBEDDED}
  864. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  865. { Finalize units }
  866. FinalizeUnits;
  867. {$if (defined(MSWINDOWS) and not defined(win16)) or defined(OS2)}
  868. { finally release the heap if possible, especially
  869. important for DLLs.
  870. Reset the array to nil, and finally also argv itself to
  871. avoid double freeing problem in case this function gets called twice. }
  872. if assigned(argv) then
  873. begin
  874. for i:=0 to argc-1 do
  875. if assigned(argv[i]) then
  876. begin
  877. sysfreemem(argv[i]);
  878. argv[i]:=nil;
  879. end;
  880. sysfreemem(argv);
  881. argv:=nil;
  882. end;
  883. {$endif}
  884. {$ifdef LINUX}
  885. {sysfreemem already checks for nil}
  886. { Do not try to do anything if the heap manager already reported an error }
  887. if (errorcode<>203) and (errorcode<>204) then
  888. sysfreemem(calculated_cmdline);
  889. {$endif}
  890. {$ifdef BSD}
  891. { Do not try to do anything if the heap manager already reported an error }
  892. if (errorcode<>203) and (errorcode<>204) then
  893. sysfreemem(cmdline);
  894. {$endif}
  895. {$ifdef FPC_HAS_FEATURE_HEAP}
  896. {$ifndef HAS_MEMORYMANAGER}
  897. {$ifndef FPC_NO_DEFAULT_HEAP}
  898. FinalizeHeap;
  899. {$endif not FPC_NO_DEFAULT_HEAP}
  900. {$endif not HAS_MEMORYMANAGER}
  901. {$endif FPC_HAS_FEATURE_HEAP}
  902. End;
  903. Procedure fpc_do_exit;[Public,Alias:'FPC_DO_EXIT']; compilerproc;
  904. begin
  905. InternalExit;
  906. System_exit;
  907. end;
  908. procedure internal_do_exit; external name 'FPC_DO_EXIT';
  909. Procedure lib_exit;[Public,Alias:'FPC_LIB_EXIT'];
  910. begin
  911. InternalExit;
  912. end;
  913. Procedure Halt(ErrNum: Longint);
  914. Begin
  915. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  916. {$ifdef FPC_LIMITED_EXITCODE}
  917. if ErrNum > maxExitCode then
  918. ExitCode:=255
  919. else
  920. {$endif FPC_LIMITED_EXITCODE}
  921. ExitCode:=ErrNum;
  922. {$endif FPC_HAS_FEATURE_EXITCODE}
  923. internal_do_exit;
  924. end;
  925. function SysBackTraceStr (Addr: CodePointer): ShortString;
  926. begin
  927. SysBackTraceStr:=' $'+hexstr(addr);
  928. end;
  929. {$ifndef FPC_SYSTEM_HAS_CAPTUREBACKTRACE}
  930. function CaptureBacktrace(skipframes,count:sizeint;frames:PCodePointer):sizeint;
  931. var
  932. curr_frame,prev_frame: pointer;
  933. curr_addr: codepointer;
  934. i: sizeint;
  935. begin
  936. curr_frame:=get_frame;
  937. curr_addr:=get_pc_addr;
  938. prev_frame:=curr_frame;
  939. get_caller_stackinfo(curr_frame,curr_addr);
  940. i:=-skipframes;
  941. while (i<count) and (curr_frame>prev_frame) and
  942. (curr_frame<StackTop) do
  943. begin
  944. if (curr_addr=nil) or
  945. (curr_frame=nil) then
  946. break;
  947. if (i>=0) then
  948. frames[i]:=curr_addr;
  949. inc(i);
  950. prev_frame:=curr_frame;
  951. get_caller_stackinfo(curr_frame,curr_addr);
  952. end;
  953. if i<0 then
  954. result:=0
  955. else
  956. result:=i;
  957. end;
  958. {$endif FPC_SYSTEM_HAS_CAPTUREBACKTRACE}
  959. Procedure HandleErrorAddrFrame (Errno : longint;addr : CodePointer; frame : Pointer);[public,alias:'FPC_BREAK_ERROR']; {$ifdef CPUI386} register; {$endif}
  960. begin
  961. If codepointer(ErrorProc)<>Nil then
  962. ErrorProc(Errno,addr,frame);
  963. errorcode:=word(Errno);
  964. erroraddr:=addr;
  965. errorbase:=frame;
  966. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  967. if ExceptAddrStack <> nil then
  968. raise TObject(nil) at addr,frame;
  969. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  970. Halt(errorcode);
  971. end;
  972. { This is used internally by system skip first level,
  973. and generated the same output as before, when
  974. HandleErrorFrame function was used internally. }
  975. Procedure HandleErrorAddrFrameInd (Errno : longint;addr : CodePointer; frame : Pointer);
  976. begin
  977. get_caller_stackinfo (frame, addr);
  978. HandleErrorAddrFrame (Errno,addr,frame);
  979. end;
  980. Procedure HandleErrorFrame (Errno : longint;frame : Pointer);
  981. {
  982. Procedure to handle internal errors, i.e. not user-invoked errors
  983. Internal function should ALWAYS call HandleError instead of RunError.
  984. Can be used for exception handlers to specify the frame
  985. }
  986. begin
  987. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  988. end;
  989. procedure fpc_handleerror (Errno : longint); compilerproc; [public,alias : 'FPC_HANDLEERROR'];
  990. {
  991. Procedure to handle internal errors, i.e. not user-invoked errors
  992. Internal function should ALWAYS call HandleError instead of RunError.
  993. }
  994. begin
  995. HandleErrorAddrFrameInd(Errno,get_pc_addr,get_frame);
  996. end;
  997. procedure RunError(w : word);[alias: 'FPC_RUNERROR'];
  998. var
  999. bp : pointer;
  1000. pcaddr : codepointer;
  1001. begin
  1002. errorcode:=w;
  1003. pcaddr:=get_pc_addr;
  1004. bp:=get_frame;
  1005. get_caller_stackinfo(bp,pcaddr);
  1006. erroraddr:=pcaddr;
  1007. errorbase:=bp;
  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. Procedure dump_stack(var f : text;fp : Pointer; addr : CodePointer);
  1023. var
  1024. i : Longint;
  1025. prevfp : Pointer;
  1026. is_dev : boolean;
  1027. Begin
  1028. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1029. try
  1030. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1031. { Frame of this procedure acts as StackBottom, fp values below that are invalid. }
  1032. prevfp:=get_frame;
  1033. i:=0;
  1034. is_dev:=do_isdevice(textrec(f).Handle);
  1035. { sanity checks, new frame pointer must be always greater than the old one, further
  1036. it must point into the stack area, else something went wrong }
  1037. while (fp>prevfp) and (fp<StackTop) do
  1038. Begin
  1039. prevfp:=fp;
  1040. get_caller_stackinfo(fp,addr);
  1041. if (addr=nil) then
  1042. break;
  1043. Writeln(f,BackTraceStrFunc(addr));
  1044. if (fp=nil) then
  1045. break;
  1046. Inc(i);
  1047. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  1048. break;
  1049. End;
  1050. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1051. except
  1052. { prevent endless dump if an exception occurred }
  1053. end;
  1054. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1055. End;
  1056. procedure dump_stack(var f: text; skipframes: longint);
  1057. var
  1058. i,count: longint;
  1059. frames: array [0..255] of codepointer;
  1060. begin
  1061. if do_isdevice(textrec(f).handle) then
  1062. count:=max_frame_dump
  1063. else
  1064. count:=255;
  1065. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1066. try
  1067. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1068. count:=CaptureBacktrace(skipframes+1,count,@frames[0]);
  1069. for i:=0 to count-1 do
  1070. writeln(f,BackTraceStrFunc(frames[i]));
  1071. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1072. except
  1073. end;
  1074. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1075. end;
  1076. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1077. procedure DumpExceptionBackTrace(var f:text);
  1078. var
  1079. FrameNumber,
  1080. FrameCount : longint;
  1081. Frames : PCodePointer;
  1082. begin
  1083. if RaiseList=nil then
  1084. exit;
  1085. WriteLn(f,BackTraceStrFunc(RaiseList^.Addr));
  1086. FrameCount:=RaiseList^.Framecount;
  1087. Frames:=RaiseList^.Frames;
  1088. for FrameNumber := 0 to FrameCount-1 do
  1089. WriteLn(f,BackTraceStrFunc(Frames[FrameNumber]));
  1090. end;
  1091. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1092. {$ifdef FPC_HAS_FEATURE_HEAP}
  1093. Type
  1094. PExitProcInfo = ^TExitProcInfo;
  1095. TExitProcInfo = Record
  1096. Next : PExitProcInfo;
  1097. SaveExit : CodePointer;
  1098. Proc : TProcedure;
  1099. End;
  1100. const
  1101. ExitProcList: PExitProcInfo = nil;
  1102. Procedure DoExitProc;
  1103. var
  1104. P : PExitProcInfo;
  1105. Proc : TProcedure;
  1106. Begin
  1107. P:=ExitProcList;
  1108. ExitProcList:=P^.Next;
  1109. ExitProc:=P^.SaveExit;
  1110. Proc:=P^.Proc;
  1111. DisPose(P);
  1112. Proc();
  1113. End;
  1114. Procedure AddExitProc(Proc: TProcedure);
  1115. var
  1116. P : PExitProcInfo;
  1117. Begin
  1118. New(P);
  1119. P^.Next:=ExitProcList;
  1120. P^.SaveExit:=ExitProc;
  1121. P^.Proc:=Proc;
  1122. ExitProcList:=P;
  1123. ExitProc:=@DoExitProc;
  1124. End;
  1125. {$endif FPC_HAS_FEATURE_HEAP}
  1126. {$ifdef FPC_HAS_FEATURE_HEAP}
  1127. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1128. function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):ppchar; // const ?
  1129. // Extra allocate reserveentries pchar's at the beginning (default param=0 after 1.0.x ?)
  1130. // Note: for internal use by skilled programmers only
  1131. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  1132. var p : ppchar;
  1133. i : LongInt;
  1134. begin
  1135. if High(s)<Low(s) Then Exit(NIL);
  1136. Getmem(p,sizeof(pchar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  1137. // for cmd
  1138. if p=nil then
  1139. begin
  1140. {$ifdef xunix}
  1141. fpseterrno(ESysEnomem);
  1142. {$endif}
  1143. exit(NIL);
  1144. end;
  1145. for i:=low(s) to high(s) do
  1146. p[i+Reserveentries]:=pchar(s[i]);
  1147. p[high(s)+1+Reserveentries]:=nil;
  1148. ArrayStringToPPchar:=p;
  1149. end;
  1150. Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):ppchar;
  1151. {
  1152. Create a PPChar to structure of pchars which are the arguments specified
  1153. in the string S. Especially useful for creating an ArgV for Exec-calls
  1154. }
  1155. begin
  1156. StringToPPChar:=StringToPPChar(PChar(S),ReserveEntries);
  1157. end;
  1158. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1159. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  1160. var
  1161. i,nr : longint;
  1162. Buf : ^char;
  1163. p : ppchar;
  1164. begin
  1165. buf:=s;
  1166. nr:=1;
  1167. while (buf^<>#0) do // count nr of args
  1168. begin
  1169. while (buf^ in [' ',#9,#10]) do // Kill separators.
  1170. inc(buf);
  1171. inc(nr);
  1172. if buf^='"' Then // quotes argument?
  1173. begin
  1174. inc(buf);
  1175. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1176. inc(buf);
  1177. if buf^='"' then // skip closing quote.
  1178. inc(buf);
  1179. end
  1180. else
  1181. begin // else std
  1182. while not (buf^ in [' ',#0,#9,#10]) do
  1183. inc(buf);
  1184. end;
  1185. end;
  1186. getmem(p,(ReserveEntries+nr)*sizeof(pchar));
  1187. StringToPPChar:=p;
  1188. if p=nil then
  1189. begin
  1190. {$ifdef xunix}
  1191. fpseterrno(ESysEnomem);
  1192. {$endif}
  1193. exit;
  1194. end;
  1195. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  1196. buf:=s;
  1197. while (buf^<>#0) do
  1198. begin
  1199. while (buf^ in [' ',#9,#10]) do // Kill separators.
  1200. begin
  1201. buf^:=#0;
  1202. inc(buf);
  1203. end;
  1204. if buf^='"' Then // quotes argument?
  1205. begin
  1206. inc(buf);
  1207. p^:=buf;
  1208. inc(p);
  1209. p^:=nil;
  1210. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1211. inc(buf);
  1212. if buf^='"' then // skip closing quote.
  1213. begin
  1214. buf^:=#0;
  1215. inc(buf);
  1216. end;
  1217. end
  1218. else
  1219. begin
  1220. p^:=buf;
  1221. inc(p);
  1222. p^:=nil;
  1223. while not (buf^ in [' ',#0,#9,#10]) do
  1224. inc(buf);
  1225. end;
  1226. end;
  1227. end;
  1228. {$endif FPC_HAS_FEATURE_HEAP}
  1229. {*****************************************************************************
  1230. Abstract/Assert support.
  1231. *****************************************************************************}
  1232. procedure fpc_emptymethod;[public,alias : 'FPC_EMPTYMETHOD'];
  1233. begin
  1234. end;
  1235. procedure fpc_AbstractErrorIntern;compilerproc;[public,alias : 'FPC_ABSTRACTERROR'];
  1236. begin
  1237. If codepointer(AbstractErrorProc)<>nil then
  1238. AbstractErrorProc();
  1239. HandleErrorAddrFrameInd(211,get_pc_addr,get_frame);
  1240. end;
  1241. Procedure fpc_assert(Const Msg,FName:Shortstring;LineNo:Longint;
  1242. ErrorAddr:Pointer); [Public,Alias : 'FPC_ASSERT']; compilerproc;
  1243. begin
  1244. if codepointer(AssertErrorProc)<>nil then
  1245. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  1246. else
  1247. HandleErrorAddrFrameInd(227,get_pc_addr,get_frame);
  1248. end;
  1249. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
  1250. begin
  1251. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1252. If msg='' then
  1253. write(stderr,'Assertion failed')
  1254. else
  1255. write(stderr,msg);
  1256. Writeln(stderr,' (',FName,', line ',LineNo,').');
  1257. Writeln(stderr,'');
  1258. Halt(227);
  1259. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1260. end;
  1261. {*****************************************************************************
  1262. SetJmp/LongJmp support.
  1263. *****************************************************************************}
  1264. {$i setjump.inc}
  1265. {$pop} //{$I-,R-,Q-} before 'procedure fpc_rangeerror'
  1266. {*****************************************************************************
  1267. Heap
  1268. *****************************************************************************}
  1269. {$i sysheap.inc}
  1270. {$i heap.inc}
  1271. {*****************************************************************************
  1272. Thread support
  1273. *****************************************************************************}
  1274. {$ifdef FPC_HAS_FEATURE_THREADING}
  1275. { Generic threadmanager }
  1276. {$i thread.inc}
  1277. {$ifndef FPC_SECTION_THREADVARS}
  1278. { Generic threadvar support }
  1279. {$i threadvr.inc}
  1280. {$endif FPC_SECTION_THREADVARS}
  1281. {$ifdef DISABLE_NO_THREAD_MANAGER}
  1282. { OS Dependent implementation }
  1283. {$i systhrd.inc}
  1284. {$endif DISABLE_NO_THREAD_MANAGER}
  1285. {$endif FPC_HAS_FEATURE_THREADING}
  1286. {*****************************************************************************
  1287. Dynamic library support
  1288. *****************************************************************************}
  1289. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  1290. {$i dynlib.inc}
  1291. {$ifdef DISABLE_NO_DYNLIBS_MANAGER}
  1292. { OS Dependant implementation }
  1293. {$i sysdl.inc}
  1294. {$endif DISABLE_NO_DYNLIBS_MANAGER}
  1295. {$endif FPC_HAS_FEATURE_DYNLIBS}
  1296. {*****************************************************************************
  1297. File Handling
  1298. *****************************************************************************}
  1299. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1300. { Allow slash and backslash as separators }
  1301. procedure DoDirSeparators(var p: pchar; inplace: boolean = true);
  1302. var
  1303. i : longint;
  1304. len : sizeint;
  1305. newp : pchar;
  1306. begin
  1307. len:=length(p);
  1308. newp:=nil;
  1309. for i:=0 to len do
  1310. if p[i] in AllowDirectorySeparators then
  1311. begin
  1312. if not inplace and
  1313. not assigned(newp) then
  1314. begin
  1315. getmem(newp,len+1);
  1316. move(p^,newp^,len+1);
  1317. p:=newp;
  1318. end;
  1319. p[i]:=DirectorySeparator;
  1320. end;
  1321. end;
  1322. procedure DoDirSeparators(var p: pwidechar; inplace: boolean = true);
  1323. var
  1324. i : longint;
  1325. len : sizeint;
  1326. newp : pwidechar;
  1327. begin
  1328. len:=length(p);
  1329. newp:=nil;
  1330. for i:=0 to len do
  1331. if (ord(p[i])<255) and
  1332. (ansichar(ord(p[i])) in AllowDirectorySeparators) then
  1333. begin
  1334. if not inplace and
  1335. not assigned(newp) then
  1336. begin
  1337. getmem(newp,(len+1)*2);
  1338. move(p^,newp^,(len+1)*2);
  1339. p:=newp;
  1340. end;
  1341. p[i]:=DirectorySeparator;
  1342. end;
  1343. end;
  1344. procedure DoDirSeparators(var p:shortstring);
  1345. var
  1346. i : longint;
  1347. begin
  1348. for i:=1 to length(p) do
  1349. if p[i] in AllowDirectorySeparators then
  1350. p[i]:=DirectorySeparator;
  1351. end;
  1352. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1353. procedure DoDirSeparators(var ps:RawByteString);
  1354. var
  1355. i : longint;
  1356. p : pchar;
  1357. unique : boolean;
  1358. begin
  1359. unique:=false;
  1360. for i:=1 to length(ps) do
  1361. if ps[i] in AllowDirectorySeparators then
  1362. begin
  1363. if not unique then
  1364. begin
  1365. uniquestring(ps);
  1366. p:=pchar(ps);
  1367. unique:=true;
  1368. end;
  1369. p[i-1]:=DirectorySeparator;
  1370. end;
  1371. end;
  1372. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1373. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  1374. procedure DoDirSeparators(var ps:UnicodeString);
  1375. var
  1376. i : longint;
  1377. p : pwidechar;
  1378. unique : boolean;
  1379. begin
  1380. unique:=false;
  1381. for i:=1 to length(ps) do
  1382. if ps[i] in AllowDirectorySeparators then
  1383. begin
  1384. if not unique then
  1385. begin
  1386. uniquestring(ps);
  1387. p:=pwidechar(ps);
  1388. unique:=true;
  1389. end;
  1390. p[i-1]:=DirectorySeparator;
  1391. end;
  1392. end;
  1393. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  1394. {$endif FPC_HAS_FEATURE_FILEIO}
  1395. { OS dependent low level file functions }
  1396. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1397. {$i sysfile.inc}
  1398. {$ifndef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1399. {$ifdef FPC_ANSI_TEXTFILEREC}
  1400. procedure do_open(var f; p: pansichar; flags: longint; pchangeable: boolean);
  1401. var
  1402. u: UnicodeString;
  1403. begin
  1404. widestringmanager.Ansi2UnicodeMoveProc(p,DefaultFileSystemCodePage,u,length(p));
  1405. do_open(f,pwidechar(u),flags,true);
  1406. end;
  1407. procedure do_erase(p: pansichar; pchangeable: boolean);
  1408. var
  1409. u: UnicodeString;
  1410. begin
  1411. widestringmanager.Ansi2UnicodeMoveProc(p,DefaultFileSystemCodePage,u,length(p));
  1412. do_erase(pwidechar(u),true);
  1413. end;
  1414. procedure do_rename(src, dst: pansichar; srcchangeable, dstchangeable: boolean);
  1415. var
  1416. usrc, udst: UnicodeString;
  1417. begin
  1418. widestringmanager.Ansi2UnicodeMoveProc(src,DefaultFileSystemCodePage,usrc,length(src));
  1419. widestringmanager.Ansi2UnicodeMoveProc(dst,DefaultFileSystemCodePage,udst,length(dst));
  1420. do_rename(pwidechar(usrc),pwidechar(udst),true,true);
  1421. end;
  1422. procedure do_rename(src: pansichar; dst: pwidechar; srcchangeable, dstchangeable: boolean);
  1423. var
  1424. usrc: UnicodeString;
  1425. begin
  1426. widestringmanager.Ansi2UnicodeMoveProc(src,DefaultFileSystemCodePage,usrc,length(src));
  1427. do_rename(pwidechar(usrc),dst,true,dstchangeable);
  1428. end;
  1429. {$endif FPC_ANSI_TEXTFILEREC}
  1430. {$endif not FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1431. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1432. {$ifndef FPC_ANSI_TEXTFILEREC}
  1433. procedure do_open(var f; p: pwidechar; flags: longint; pchangeable: boolean);
  1434. var
  1435. s: RawByteString;
  1436. begin
  1437. widestringmanager.Unicode2AnsiMoveProc(p,s,DefaultFileSystemCodePage,length(p));
  1438. do_open(f,pansichar(s),flags,true);
  1439. end;
  1440. procedure do_erase(p: pwidechar; pchangeable: boolean);
  1441. var
  1442. s: RawByteString;
  1443. begin
  1444. widestringmanager.Unicode2AnsiMoveProc(p,s,DefaultFileSystemCodePage,length(p));
  1445. do_erase(pansichar(s),true);
  1446. end;
  1447. procedure do_rename(src, dst: pwidechar; srcchangeable, dstchangeable: boolean);
  1448. var
  1449. rsrc, rdst: RawByteString;
  1450. begin
  1451. widestringmanager.Unicode2AnsiMoveProc(src,rsrc,DefaultFileSystemCodePage,length(src));
  1452. widestringmanager.Unicode2AnsiMoveProc(dst,rdst,DefaultFileSystemCodePage,length(dst));
  1453. do_rename(pansichar(rsrc),pansichar(rdst),true,true);
  1454. end;
  1455. procedure do_rename(src: pwidechar; dst: pansichar; srcchangeable, dstchangeable: boolean);
  1456. var
  1457. rsrc: RawByteString;
  1458. begin
  1459. widestringmanager.Unicode2AnsiMoveProc(src,rsrc,DefaultFileSystemCodePage,length(src));
  1460. do_rename(pansichar(rsrc),dst,true,dstchangeable);
  1461. end;
  1462. {$endif not FPC_ANSI_TEXTFILEREC}
  1463. {$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1464. {$endif FPC_HAS_FEATURE_FILEIO}
  1465. { helper for targets supporting no ansistrings, it is used
  1466. by non-ansistring code }
  1467. function min(v1,v2 : SizeInt) : SizeInt;
  1468. begin
  1469. if v1<v2 then
  1470. result:=v1
  1471. else
  1472. result:=v2;
  1473. end;
  1474. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  1475. { Text file }
  1476. {$i text.inc}
  1477. {$endif FPC_HAS_FEATURE_TEXTIO}
  1478. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1479. { Untyped file }
  1480. {$i file.inc}
  1481. { Typed file }
  1482. {$i typefile.inc}
  1483. {$endif FPC_HAS_FEATURE_FILEIO}
  1484. {*****************************************************************************
  1485. Directory Handling
  1486. *****************************************************************************}
  1487. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1488. { OS dependent dir functions }
  1489. {$i sysdir.inc}
  1490. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1491. {$ifndef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1492. procedure do_getdir(drivenr : byte;var dir : rawbytestring);
  1493. var
  1494. u: unicodestring;
  1495. begin
  1496. Do_getdir(drivenr,u);
  1497. widestringmanager.Unicode2AnsiMoveProc(pwidechar(u),dir,DefaultRTLFileSystemCodePage,length(u));
  1498. end;
  1499. {$endif FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1500. Procedure MkDir(Const s: RawByteString);[IOCheck];
  1501. Begin
  1502. If (s='') or (InOutRes <> 0) then
  1503. exit;
  1504. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1505. Do_mkdir(ToSingleByteFileSystemEncodedFileName(S));
  1506. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1507. Do_mkdir(S);
  1508. {$endif}
  1509. end;
  1510. Procedure RmDir(Const s: RawByteString);[IOCheck];
  1511. Begin
  1512. If (s='') or (InOutRes <> 0) then
  1513. exit;
  1514. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1515. Do_rmdir(ToSingleByteFileSystemEncodedFileName(S));
  1516. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1517. Do_rmdir(S);
  1518. {$endif}
  1519. End;
  1520. Procedure ChDir(Const s: RawByteString);[IOCheck];
  1521. Begin
  1522. If (s='') or (InOutRes <> 0) then
  1523. exit;
  1524. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1525. Do_chdir(ToSingleByteFileSystemEncodedFileName(S));
  1526. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1527. Do_chdir(S);
  1528. {$endif}
  1529. End;
  1530. Procedure getdir(drivenr:byte;Var dir:rawbytestring);
  1531. begin
  1532. Do_getdir(drivenr,dir);
  1533. { we should return results in the DefaultRTLFileSystemCodePage -> convert if
  1534. necessary }
  1535. setcodepage(dir,DefaultRTLFileSystemCodePage,true);
  1536. end;
  1537. { the generic shortstring ones are only implemented elsewhere for systems *not*
  1538. supporting ansi/unicodestrings; for now assume there are no systems that
  1539. support unicodestrings but not ansistrings }
  1540. { avoid double string conversions }
  1541. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1542. function GetDirStrFromShortstring(const s: shortstring): RawByteString;
  1543. begin
  1544. GetDirStrFromShortstring:=ToSingleByteFileSystemEncodedFileName(ansistring(s));
  1545. end;
  1546. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1547. function GetDirStrFromShortstring(const s: shortstring): UnicodeString;
  1548. begin
  1549. GetDirStrFromShortstring:=s;
  1550. end;
  1551. {$endif FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1552. Procedure MkDir(Const s: shortstring);[IOCheck];
  1553. Begin
  1554. If (s='') or (InOutRes <> 0) then
  1555. exit;
  1556. Do_mkdir(GetDirStrFromShortstring(S));
  1557. End;
  1558. Procedure RmDir(Const s: shortstring);[IOCheck];
  1559. Begin
  1560. If (s='') or (InOutRes <> 0) then
  1561. exit;
  1562. Do_rmdir(GetDirStrFromShortstring(S));
  1563. End;
  1564. Procedure ChDir(Const s: shortstring);[IOCheck];
  1565. Begin
  1566. If (s='') or (InOutRes <> 0) then
  1567. exit;
  1568. Do_chdir(GetDirStrFromShortstring(S));
  1569. End;
  1570. Procedure getdir(drivenr:byte;Var dir:shortstring);
  1571. var
  1572. s: rawbytestring;
  1573. begin
  1574. Do_getdir(drivenr,s);
  1575. if length(s)<=high(dir) then
  1576. dir:=s
  1577. else
  1578. inoutres:=3;
  1579. end;
  1580. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1581. {$if defined(FPC_HAS_FEATURE_WIDESTRINGS)}
  1582. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1583. { overloads required for mkdir/rmdir/chdir to ensure that the string is
  1584. converted to the right code page }
  1585. procedure do_mkdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1586. begin
  1587. do_mkdir(ToSingleByteFileSystemEncodedFileName(s));
  1588. end;
  1589. procedure do_rmdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1590. begin
  1591. do_rmdir(ToSingleByteFileSystemEncodedFileName(s));
  1592. end;
  1593. procedure do_chdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1594. begin
  1595. do_chdir(ToSingleByteFileSystemEncodedFileName(s));
  1596. end;
  1597. procedure do_getdir(drivenr : byte;var dir : unicodestring);
  1598. var
  1599. s: rawbytestring;
  1600. begin
  1601. Do_getdir(drivenr,s);
  1602. dir:=unicodestring(s);
  1603. end;
  1604. {$endif FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1605. Procedure MkDir(Const s: UnicodeString);[IOCheck];
  1606. Begin
  1607. if (s='') or (InOutRes <> 0) then
  1608. exit;
  1609. Do_mkdir(S);
  1610. End;
  1611. Procedure RmDir(Const s: UnicodeString);[IOCheck];
  1612. Begin
  1613. if (s='') or (InOutRes <> 0) then
  1614. exit;
  1615. Do_rmdir(S);
  1616. End;
  1617. Procedure ChDir(Const s: UnicodeString);[IOCheck];
  1618. Begin
  1619. if (s='') or (InOutRes <> 0) then
  1620. exit;
  1621. Do_chdir(S);
  1622. End;
  1623. Procedure getdir(drivenr:byte;Var dir:unicodestring);
  1624. begin
  1625. Do_getdir(drivenr,dir);
  1626. end;
  1627. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1628. {$endif FPC_HAS_FEATURE_FILEIO}
  1629. {*****************************************************************************
  1630. Resources support
  1631. *****************************************************************************}
  1632. {$i sysres.inc}
  1633. const
  1634. CtrlBreakHandler: TCtrlBreakHandler = nil;
  1635. {$IFNDEF FPC_HAS_SETCTRLBREAKHANDLER}
  1636. (* It is possible to provide platform specific implementation performing *)
  1637. (* special initialization; default implementation just sets the procedural *)
  1638. (* variable to make it available for use from the exception handler. *)
  1639. function SysSetCtrlBreakHandler (Handler: TCtrlBreakHandler): TCtrlBreakHandler;
  1640. begin
  1641. (* Return either nil or previous handler *)
  1642. SysSetCtrlBreakHandler := CtrlBreakHandler;
  1643. CtrlBreakHandler := Handler;
  1644. end;
  1645. {$ENDIF FPC_HAS_SETCTRLBREAKHANDLER}