2
0

system.inc 57 KB

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