system.inc 55 KB

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