system.inc 57 KB

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