system.inc 54 KB

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