system.inc 65 KB

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