system.inc 51 KB

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