system.inc 55 KB

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