system.inc 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434
  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. begin
  654. random := xsr128_32_u32rand * (extended(1.0)/(int64(1) shl 32));
  655. end;
  656. {$endif}
  657. {$pop} // random
  658. {$else FPC_USE_SIMPLE_RANDOM}
  659. { A simple implementation of random. TP/Delphi compatible. }
  660. const
  661. QRAN_A = 134775813;
  662. QRAN_C = 1;
  663. function rand_next: cardinal;
  664. var
  665. s: cardinal;
  666. begin
  667. s:=RandSeed*QRAN_A+QRAN_C;
  668. RandSeed:=s;
  669. rand_next:=s;
  670. end;
  671. function random(l: word): word;
  672. var
  673. s,ss: cardinal;
  674. begin
  675. s:=rand_next;
  676. { use 32-bit multiplications here }
  677. ss:=(s shr 16)*l;
  678. s:=(s and $FFFF)*l shr 16;
  679. random:=(ss + s) shr 16;
  680. end;
  681. function random(l: longint): longint;
  682. begin
  683. random:=int64(rand_next)*l shr 32;
  684. end;
  685. function random(l:int64):int64;
  686. begin
  687. random:=random(longint(l));
  688. end;
  689. {$ifndef FPUNONE}
  690. function random: extended;
  691. const
  692. c = 1.0/$10000/$10000;
  693. begin
  694. random:=rand_next*c;
  695. end;
  696. {$endif}
  697. {$endif FPC_USE_SIMPLE_RANDOM}
  698. {$endif FPC_HAS_FEATURE_RANDOM}
  699. {****************************************************************************
  700. Memory Management
  701. ****************************************************************************}
  702. {$ifndef FPC_SYSTEM_HAS_PTR}
  703. Function Ptr(sel,off : {$ifdef CPU16}Word{$else}Longint{$endif}) : farpointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  704. Begin
  705. ptr:=farpointer((sel shl 4)+off);
  706. End;
  707. {$endif not FPC_SYSTEM_HAS_PTR}
  708. {$ifndef FPC_SYSTEM_HAS_CSEG}
  709. Function CSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  710. Begin
  711. Cseg:=0;
  712. End;
  713. {$endif not FPC_SYSTEM_HAS_CSEG}
  714. {$ifndef FPC_SYSTEM_HAS_DSEG}
  715. Function DSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  716. Begin
  717. Dseg:=0;
  718. End;
  719. {$endif not FPC_SYSTEM_HAS_DSEG}
  720. {$ifndef FPC_SYSTEM_HAS_SSEG}
  721. Function SSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  722. Begin
  723. Sseg:=0;
  724. End;
  725. {$endif not FPC_SYSTEM_HAS_SSEG}
  726. {$push}
  727. {$R-}
  728. {$I-}
  729. {$Q-}
  730. {*****************************************************************************
  731. Miscellaneous
  732. *****************************************************************************}
  733. {$ifndef FPC_SYSTEM_HAS_STACKTOP}
  734. function StackTop: pointer;
  735. begin
  736. { Avoid wrap to zero on 32-bit }
  737. if ptruint(StackBottom) > high(ptruint) - StackLength then
  738. result:=pointer(ptruint(high(ptruint)))
  739. else
  740. result:=StackBottom + StackLength;
  741. end;
  742. {$endif FPC_SYSTEM_HAS_STACKTOP}
  743. {$ifndef FPC_SYSTEM_HAS_GET_PC_ADDR}
  744. { This provides a dummy implementation
  745. of get_pc_addr function, for CPU's that don't need
  746. the instruction address to walk the stack. }
  747. function get_pc_addr : codepointer;inline;
  748. begin
  749. get_pc_addr:=nil;
  750. end;
  751. {$endif ndef FPC_SYSTEM_HAS_GET_PC_ADDR}
  752. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_STACKINFO}
  753. { This provides a simple implementation
  754. of get_caller_stackinfo procedure,
  755. using get_caller_addr and get_caller_frame
  756. functions. }
  757. procedure get_caller_stackinfo(var framebp : pointer; var addr : codepointer);
  758. var
  759. nextbp : pointer;
  760. nextaddr : codepointer;
  761. begin
  762. nextbp:=get_caller_frame(framebp,addr);
  763. nextaddr:=get_caller_addr(framebp,addr);
  764. framebp:=nextbp;
  765. addr:=nextaddr;
  766. end;
  767. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_STACKINFO}
  768. {$ifdef FPC_HAS_EXPLICIT_INTERLOCKED_POINTER}
  769. {$ifndef FPC_SYSTEM_HAS_EXPLICIT_INTERLOCKED_POINTER}
  770. function InterLockedDecrement (var Target: pointer) : pointer;
  771. begin
  772. Result := Pointer(InterLockedDecrement(PtrInt(Target)));
  773. end;
  774. function InterLockedIncrement (var Target: pointer) : pointer;
  775. begin
  776. Result := Pointer(InterLockedIncrement(PtrInt(Target)));
  777. end;
  778. function InterLockedExchange (var Target: pointer; Source : pointer) : pointer;
  779. begin
  780. Result := Pointer(InterLockedExchange(PtrInt(Target), PtrInt(Source)));
  781. end;
  782. function InterLockedExchangeAdd (var Target: pointer; Source : pointer) : pointer;
  783. begin
  784. Result := Pointer(InterLockedExchangeAdd(PtrInt(Target), PtrInt(Source)));
  785. end;
  786. function InterLockedCompareExchange (var Target: pointer; NewValue: pointer; Comperand: pointer): pointer;
  787. begin
  788. Result := Pointer(InterLockedCompareExchange(PtrInt(Target), PtrInt(NewValue), PtrInt(Comperand)));
  789. end;
  790. function InterLockedCompareExchangePointer (var Target: pointer; NewValue: pointer; Comperand: pointer): pointer;
  791. begin
  792. Result := Pointer(InterLockedCompareExchange(PtrInt(Target), PtrInt(NewValue), PtrInt(Comperand)));
  793. end;
  794. {$endif FPC_SYSTEM_HAS_EXPLICIT_INTERLOCKED_POINTER}
  795. {$endif FPC_HAS_EXPLICIT_INTERLOCKED_POINTER}
  796. procedure fpc_objecterror; compilerproc;
  797. begin
  798. HandleErrorAddrFrameInd(210,get_pc_addr,get_frame);
  799. end;
  800. procedure fpc_rangeerror;[public,alias:'FPC_RANGEERROR']; compilerproc;
  801. begin
  802. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  803. end;
  804. procedure fpc_divbyzero;[public,alias:'FPC_DIVBYZERO']; compilerproc;
  805. begin
  806. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  807. end;
  808. procedure fpc_overflow;[public,alias:'FPC_OVERFLOW']; compilerproc;
  809. begin
  810. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  811. end;
  812. procedure fpc_threaderror; [public,alias:'FPC_THREADERROR'];
  813. begin
  814. HandleErrorAddrFrameInd(236{RuntimeErrorExitCodes[reThreadError]},get_pc_addr,get_frame);
  815. end;
  816. procedure fpc_invalidpointer; [public,alias:'FPC_INVALIDPOINTER'];
  817. begin
  818. HandleErrorAddrFrameInd(216,get_pc_addr,get_frame);
  819. end;
  820. procedure fpc_iocheck;[public,alias:'FPC_IOCHECK']; compilerproc;
  821. var
  822. l : longint;
  823. HInoutRes : PWord;
  824. begin
  825. HInOutRes:=@InoutRes;
  826. if HInOutRes^<>0 then
  827. begin
  828. l:=HInOutRes^;
  829. HInOutRes^:=0;
  830. HandleErrorAddrFrameInd(l,get_pc_addr,get_frame);
  831. end;
  832. end;
  833. Function IOResult:Word;
  834. var
  835. HInoutRes : PWord;
  836. Begin
  837. HInoutRes:=@InoutRes;
  838. IOResult:=HInOutRes^;
  839. HInOutRes^:=0;
  840. End;
  841. Function GetThreadID:TThreadID;{$ifdef SYSTEMINLINE}inline;{$endif}
  842. begin
  843. (* ThreadID is stored in a threadvar and made available in interface *)
  844. (* to allow setup of this value during thread initialization. *)
  845. GetThreadID := ThreadID;
  846. end;
  847. function fpc_safecallcheck(res : hresult) : hresult;[public,alias:'FPC_SAFECALLCHECK']; compilerproc; {$ifdef CPU86} register; {$endif}
  848. begin
  849. if res<0 then
  850. begin
  851. if assigned(SafeCallErrorProc) then
  852. SafeCallErrorProc(res,get_frame);
  853. HandleErrorAddrFrameInd(229,get_pc_addr,get_frame);
  854. end;
  855. result:=res;
  856. end;
  857. {*****************************************************************************
  858. Stack check code
  859. *****************************************************************************}
  860. { be compatible with old code }
  861. {$ifdef FPC_NO_GENERIC_STACK_CHECK}
  862. {$define NO_GENERIC_STACK_CHECK}
  863. {$endif FPC_NO_GENERIC_STACK_CHECK}
  864. {$IFNDEF NO_GENERIC_STACK_CHECK}
  865. {$PUSH}
  866. {$S-}
  867. procedure fpc_stackcheck(stack_size:SizeUInt);[public,alias:'FPC_STACKCHECK']; compilerproc;
  868. var
  869. c : Pointer;
  870. begin
  871. { Avoid recursive calls when called from the exit routines }
  872. if StackError then
  873. exit;
  874. { check stack alignment }
  875. {$ifdef CPUI386}
  876. {$ifdef FPC_STACKALIGNMENT}
  877. {$if FPC_STACKALIGNMENT=16}
  878. if ((PtrUInt(Sptr)+4) mod 16)<>0 then
  879. begin
  880. StackError:=true;
  881. HandleError(202);
  882. end;
  883. {$endif FPC_STACKALIGNMENT=16}
  884. {$endif FPC_STACKALIGNMENT}
  885. {$endif CPUI386}
  886. { don't use stack_size, since the stack pointer has already been
  887. decreased when this routine is called
  888. }
  889. c := Sptr - StackMargin;
  890. if (c <= StackBottom) then
  891. begin
  892. StackError:=true;
  893. HandleError(202);
  894. end;
  895. end;
  896. {$POP}
  897. {$ENDIF NO_GENERIC_STACK_CHECK}
  898. {*****************************************************************************
  899. Initialization / Finalization
  900. *****************************************************************************}
  901. const
  902. maxunits=1024; { See also files.pas of the compiler source }
  903. type
  904. TInitFinalRec=record
  905. InitProc,
  906. FinalProc : TProcedure;
  907. end;
  908. TInitFinalTable = record
  909. TableCount,
  910. InitCount : {$if defined(VER3_0)}sizeint
  911. {$else}ALUUInt{$endif}
  912. ;
  913. Procs : array[1..maxunits] of TInitFinalRec;
  914. end;
  915. PInitFinalTable = ^TInitFinalTable;
  916. {$ifndef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  917. var
  918. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  919. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  920. procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; compilerproc;
  921. var
  922. i : ALUUInt;
  923. pt : PInitFinalTable;
  924. begin
  925. { call cpu/fpu initialisation routine }
  926. fpc_cpuinit;
  927. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  928. pt := PInitFinalTable(EntryInformation.InitFinalTable);
  929. {$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  930. pt := @InitFinalTable;
  931. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  932. with pt^ do
  933. begin
  934. for i:=1 to ALUUInt(TableCount) do
  935. begin
  936. if assigned(Procs[i].InitProc) then
  937. Procs[i].InitProc();
  938. InitCount:=i;
  939. end;
  940. end;
  941. if assigned(InitProc) then
  942. TProcedure(InitProc)();
  943. end;
  944. procedure internal_initializeunits; external name 'FPC_INITIALIZEUNITS';
  945. procedure fpc_LibInitializeUnits;[public,alias:'FPC_LIBINITIALIZEUNITS'];compilerproc;
  946. begin
  947. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  948. IsLibrary:=true;
  949. { must also be set to true for packages when implemented }
  950. ModuleIsLib:=true;
  951. internal_initializeunits;
  952. {$endif FPC_HAS_FEATURE_DYNLIBS}
  953. end;
  954. {$ifdef FPC_INIT_FINAL_UNITS_BY_CALLS}
  955. procedure FinalizeUnits; external name 'FPC_FINALIZE_FUNC_TABLE';
  956. {$else FPC_INIT_FINAL_UNITS_BY_CALLS}
  957. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  958. begin
  959. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  960. with PInitFinalTable(EntryInformation.InitFinalTable)^ do
  961. {$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  962. with InitFinalTable do
  963. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  964. begin
  965. while (InitCount>0) do
  966. begin
  967. // we've to decrement the cound before calling the final. code
  968. // else a halt in the final. code leads to a endless loop
  969. dec(InitCount);
  970. if assigned(Procs[InitCount+1].FinalProc) then
  971. Procs[InitCount+1].FinalProc();
  972. end;
  973. end;
  974. end;
  975. {$endif FPC_INIT_FINAL_UNITS_BY_CALLS}
  976. {*****************************************************************************
  977. FPU Exceptions state
  978. *****************************************************************************}
  979. {$if defined(FPC_SYSTEM_FPUCW_IMMUTABLE) or defined(FPU_NONE)}
  980. function GetNativeFPUControlWord: TNativeFPUControlWord; {$if defined(SYSTEMINLINE)}inline;{$endif}
  981. begin
  982. result:=default(TNativeFPUControlWord);
  983. end;
  984. procedure SetNativeFPUControlWord(const cw: TNativeFPUControlWord); {$if defined(SYSTEMINLINE)}inline;{$endif}
  985. begin
  986. end;
  987. {$endif}
  988. {*****************************************************************************
  989. Error / Exit / ExitProc
  990. *****************************************************************************}
  991. Procedure system_exit;forward;{$ifdef FPC_SYSTEM_EXIT_NO_RETURN}noreturn;{$endif}
  992. {$ifdef FPC_HAS_FEATURE_HEAP}
  993. {$ifndef HAS_MEMORYMANAGER}
  994. //not needed if independant memory manager
  995. Procedure FinalizeHeap;forward;
  996. {$endif HAS_MEMORYMANAGER}
  997. {$endif FPC_HAS_FEATURE_HEAP}
  998. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  999. procedure SysFlushStdIO;
  1000. begin
  1001. { Make sure that all output is written to the redirected file }
  1002. if Textrec(Output).Mode=fmOutput then
  1003. Flush(Output);
  1004. if Textrec(ErrOutput).Mode=fmOutput then
  1005. Flush(ErrOutput);
  1006. {$ifndef FPC_STDOUT_TRUE_ALIAS}
  1007. if Textrec(stdout).Mode=fmOutput then
  1008. Flush(stdout);
  1009. if Textrec(StdErr).Mode=fmOutput then
  1010. Flush(StdErr);
  1011. {$endif FPC_STDOUT_TRUE_ALIAS}
  1012. end;
  1013. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1014. Procedure InternalExit;
  1015. var
  1016. current_exit : Procedure;
  1017. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1018. pstdout : ^Text;
  1019. {$endif}
  1020. {$if defined(MSWINDOWS) or defined(OS2)}
  1021. i : longint;
  1022. {$endif}
  1023. Begin
  1024. {$ifdef SYSTEMDEBUG}
  1025. writeln('InternalExit');
  1026. {$endif SYSTEMDEBUG}
  1027. {$ifndef CPUAVR}
  1028. while exitProc<>nil Do
  1029. Begin
  1030. InOutRes:=0;
  1031. current_exit:=tProcedure(exitProc);
  1032. exitProc:=nil;
  1033. current_exit();
  1034. End;
  1035. {$endif CPUAVR}
  1036. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1037. { the embedded system unit itself contains no routines for console i/o
  1038. console i/o is done by the Consoleio unit which can do things like
  1039. redirection to seriell i/o }
  1040. {$ifndef EMBEDDED}
  1041. { Show runtime error and exit }
  1042. if WriteErrorsToStdErr then
  1043. pstdout:=@stderr
  1044. else
  1045. pstdout:=@stdout;
  1046. If erroraddr<>nil Then
  1047. Begin
  1048. Writeln(pstdout^,'Runtime error ',Errorcode,' at $',hexstr(erroraddr));
  1049. { to get a nice symify }
  1050. Writeln(pstdout^,BackTraceStrFunc(Erroraddr));
  1051. dump_stack(pstdout^,ErrorBase,ErrorAddr);
  1052. Writeln(pstdout^,'');
  1053. End;
  1054. SysFlushStdIO;
  1055. {$endif EMBEDDED}
  1056. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1057. { Finalize units }
  1058. FinalizeUnits;
  1059. {$if defined(OS2)}
  1060. { finally release the heap if possible, especially
  1061. important for DLLs.
  1062. Reset the array to nil, and finally also argv itself to
  1063. avoid double freeing problem in case this function gets called twice. }
  1064. if assigned(argv) then
  1065. begin
  1066. for i:=0 to argc-1 do
  1067. if assigned(argv[i]) then
  1068. begin
  1069. sysfreemem(argv[i]);
  1070. argv[i]:=nil;
  1071. end;
  1072. sysfreemem(argv);
  1073. argv:=nil;
  1074. end;
  1075. {$endif}
  1076. {$ifdef LINUX}
  1077. {sysfreemem already checks for nil}
  1078. { Do not try to do anything if the heap manager already reported an error }
  1079. if (errorcode<>203) and (errorcode<>204) then
  1080. sysfreemem(calculated_cmdline);
  1081. {$endif}
  1082. {$ifdef BSD}
  1083. { Do not try to do anything if the heap manager already reported an error }
  1084. if (errorcode<>203) and (errorcode<>204) then
  1085. sysfreemem(cmdline);
  1086. {$endif}
  1087. {$ifdef FPC_HAS_FEATURE_HEAP}
  1088. {$ifndef HAS_MEMORYMANAGER}
  1089. {$ifndef FPC_NO_DEFAULT_HEAP}
  1090. FinalizeHeap;
  1091. {$endif not FPC_NO_DEFAULT_HEAP}
  1092. {$endif not HAS_MEMORYMANAGER}
  1093. {$endif FPC_HAS_FEATURE_HEAP}
  1094. End;
  1095. Procedure fpc_do_exit;[Public,Alias:'FPC_DO_EXIT']; compilerproc;
  1096. begin
  1097. InternalExit;
  1098. System_exit;
  1099. end;
  1100. procedure internal_do_exit; external name 'FPC_DO_EXIT';
  1101. Procedure fpc_lib_exit;[Public,Alias:'FPC_LIB_EXIT'];
  1102. begin
  1103. InternalExit;
  1104. end;
  1105. Procedure Halt(ErrNum: TExitCode);noreturn;
  1106. Begin
  1107. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  1108. {$ifdef FPC_LIMITED_EXITCODE}
  1109. if ErrNum > maxExitCode then
  1110. ExitCode:=255
  1111. else
  1112. {$endif FPC_LIMITED_EXITCODE}
  1113. ExitCode:=ErrNum;
  1114. {$endif FPC_HAS_FEATURE_EXITCODE}
  1115. internal_do_exit;
  1116. end;
  1117. {$ifndef FPC_SYSTEM_HAS_BACKTRACESTR}
  1118. function SysBackTraceStr (Addr: CodePointer): ShortString;
  1119. begin
  1120. SysBackTraceStr:=' $'+hexstr(addr);
  1121. end;
  1122. {$endif FPC_SYSTEM_HAS_BACKTRACESTR}
  1123. {$ifndef FPC_SYSTEM_HAS_CAPTUREBACKTRACE}
  1124. function CaptureBacktrace(skipframes,count:sizeint;frames:PCodePointer):sizeint;
  1125. var
  1126. curr_frame,prev_frame: pointer;
  1127. curr_addr: codepointer;
  1128. i: sizeint;
  1129. begin
  1130. curr_frame:=get_frame;
  1131. curr_addr:=get_pc_addr;
  1132. prev_frame:=curr_frame;
  1133. get_caller_stackinfo(curr_frame,curr_addr);
  1134. i:=-skipframes;
  1135. while (i<count) and (curr_frame>prev_frame) and
  1136. (curr_frame<StackTop) do
  1137. begin
  1138. if (curr_addr=nil) or
  1139. (curr_frame=nil) then
  1140. break;
  1141. if (i>=0) then
  1142. frames[i]:=curr_addr;
  1143. inc(i);
  1144. prev_frame:=curr_frame;
  1145. get_caller_stackinfo(curr_frame,curr_addr);
  1146. end;
  1147. if i<0 then
  1148. result:=0
  1149. else
  1150. result:=i;
  1151. end;
  1152. {$endif FPC_SYSTEM_HAS_CAPTUREBACKTRACE}
  1153. Procedure HandleErrorAddrFrame (Errno : TExitCode;addr : CodePointer; frame : Pointer);[public,alias:'FPC_BREAK_ERROR']; {$ifdef CPUI386} register; {$endif}
  1154. begin
  1155. If codepointer(ErrorProc)<>Nil then
  1156. ErrorProc(Errno,addr,frame);
  1157. errorcode:=word(Errno);
  1158. erroraddr:=addr;
  1159. errorbase:=frame;
  1160. Halt(errorcode);
  1161. end;
  1162. { This is used internally by system skip first level,
  1163. and generated the same output as before, when
  1164. HandleErrorFrame function was used internally. }
  1165. Procedure HandleErrorAddrFrameInd (Errno : TExitCode;addr : CodePointer; frame : Pointer);
  1166. begin
  1167. get_caller_stackinfo (frame, addr);
  1168. HandleErrorAddrFrame (Errno,addr,frame);
  1169. end;
  1170. Procedure HandleErrorFrame (Errno : TExitCode;frame : Pointer);
  1171. {
  1172. Procedure to handle internal errors, i.e. not user-invoked errors
  1173. Internal function should ALWAYS call HandleError instead of RunError.
  1174. Can be used for exception handlers to specify the frame
  1175. }
  1176. begin
  1177. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  1178. end;
  1179. procedure fpc_handleerror (Errno : TExitCode); compilerproc; [public,alias : 'FPC_HANDLEERROR'];
  1180. {
  1181. Procedure to handle internal errors, i.e. not user-invoked errors
  1182. Internal function should ALWAYS call HandleError instead of RunError.
  1183. }
  1184. begin
  1185. HandleErrorAddrFrameInd(Errno,get_pc_addr,get_frame);
  1186. end;
  1187. procedure RunError(w : word);[alias: 'FPC_RUNERROR'];noreturn;
  1188. var
  1189. bp : pointer;
  1190. pcaddr : codepointer;
  1191. begin
  1192. errorcode:=w;
  1193. pcaddr:=get_pc_addr;
  1194. bp:=get_frame;
  1195. get_caller_stackinfo(bp,pcaddr);
  1196. erroraddr:=pcaddr;
  1197. errorbase:=bp;
  1198. Halt(errorcode);
  1199. end;
  1200. Procedure RunError;{$ifdef SYSTEMINLINE}inline;{$endif}noreturn;
  1201. Begin
  1202. RunError (0);
  1203. End;
  1204. Procedure Halt;{$ifdef SYSTEMINLINE}inline;{$endif}noreturn;
  1205. Begin
  1206. Halt(0);
  1207. End;
  1208. Procedure Error(RunTimeError : TRunTimeError);
  1209. begin
  1210. RunError(RuntimeErrorExitCodes[RunTimeError]);
  1211. end;
  1212. Procedure dump_stack(var f : text;fp : Pointer; addr : CodePointer);
  1213. var
  1214. i : ObjpasInt;
  1215. prevfp : Pointer;
  1216. is_dev : boolean;
  1217. Begin
  1218. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1219. try
  1220. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1221. { Frame of this procedure acts as StackBottom, fp values below that are invalid. }
  1222. prevfp:=get_frame;
  1223. i:=0;
  1224. is_dev:=do_isdevice(textrec(f).Handle);
  1225. { sanity checks, new frame pointer must be always greater than the old one, further
  1226. it must point into the stack area, else something went wrong }
  1227. while (fp>prevfp) and (fp<StackTop) do
  1228. Begin
  1229. prevfp:=fp;
  1230. get_caller_stackinfo(fp,addr);
  1231. if (addr=nil) then
  1232. break;
  1233. Writeln(f,BackTraceStrFunc(addr));
  1234. if (fp=nil) then
  1235. break;
  1236. Inc(i);
  1237. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  1238. break;
  1239. End;
  1240. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1241. except
  1242. { prevent endless dump if an exception occurred }
  1243. end;
  1244. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1245. End;
  1246. procedure dump_stack(var f: text; skipframes: longint);
  1247. var
  1248. i,count: ObjpasInt;
  1249. frames: array [0..255] of codepointer;
  1250. begin
  1251. if do_isdevice(textrec(f).handle) then
  1252. count:=max_frame_dump
  1253. else
  1254. count:=255;
  1255. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1256. try
  1257. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1258. count:=CaptureBacktrace(skipframes+1,count,@frames[0]);
  1259. for i:=0 to count-1 do
  1260. writeln(f,BackTraceStrFunc(frames[i]));
  1261. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1262. except
  1263. end;
  1264. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1265. end;
  1266. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1267. procedure DumpExceptionBackTrace(var f:text);
  1268. var
  1269. FrameNumber,
  1270. FrameCount : ObjpasInt;
  1271. Frames : PCodePointer;
  1272. begin
  1273. if RaiseList=nil then
  1274. exit;
  1275. WriteLn(f,BackTraceStrFunc(RaiseList^.Addr));
  1276. FrameCount:=RaiseList^.Framecount;
  1277. Frames:=RaiseList^.Frames;
  1278. for FrameNumber := 0 to FrameCount-1 do
  1279. WriteLn(f,BackTraceStrFunc(Frames[FrameNumber]));
  1280. end;
  1281. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1282. {$ifdef FPC_HAS_FEATURE_HEAP}
  1283. Type
  1284. PExitProcInfo = ^TExitProcInfo;
  1285. TExitProcInfo = Record
  1286. Next : PExitProcInfo;
  1287. SaveExit : CodePointer;
  1288. Proc : TProcedure;
  1289. End;
  1290. const
  1291. ExitProcList: PExitProcInfo = nil;
  1292. Procedure DoExitProc;
  1293. var
  1294. P : PExitProcInfo;
  1295. Proc : TProcedure;
  1296. Begin
  1297. P:=ExitProcList;
  1298. ExitProcList:=P^.Next;
  1299. ExitProc:=P^.SaveExit;
  1300. Proc:=P^.Proc;
  1301. DisPose(P);
  1302. Proc();
  1303. End;
  1304. Procedure AddExitProc(Proc: TProcedure);
  1305. var
  1306. P : PExitProcInfo;
  1307. Begin
  1308. New(P);
  1309. P^.Next:=ExitProcList;
  1310. P^.SaveExit:=ExitProc;
  1311. P^.Proc:=Proc;
  1312. ExitProcList:=P;
  1313. ExitProc:=@DoExitProc;
  1314. End;
  1315. {$endif FPC_HAS_FEATURE_HEAP}
  1316. {$ifdef FPC_HAS_FEATURE_HEAP}
  1317. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1318. function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):ppansichar; // const ?
  1319. {$ifdef EXCLUDE_COMPLEX_PROCS}
  1320. begin
  1321. runerror(217);
  1322. end;
  1323. {$else EXCLUDE_COMPLEX_PROCS}
  1324. // Extra allocate reserveentries pansichar's at the beginning (default param=0 after 1.0.x ?)
  1325. // Note: for internal use by skilled programmers only
  1326. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  1327. var p : ppansichar;
  1328. i : ObjpasInt;
  1329. begin
  1330. if High(s)<Low(s) Then Exit(NIL);
  1331. Getmem(p,sizeof(pansichar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  1332. // for cmd
  1333. if p=nil then
  1334. begin
  1335. {$ifdef xunix}
  1336. fpseterrno(ESysEnomem);
  1337. {$endif}
  1338. exit(NIL);
  1339. end;
  1340. for i:=low(s) to high(s) do
  1341. p[i+Reserveentries]:=pansichar(s[i]);
  1342. p[high(s)+1+Reserveentries]:=nil;
  1343. ArrayStringToPPchar:=p;
  1344. end;
  1345. {$endif EXCLUDE_COMPLEX_PROCS}
  1346. Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):ppansichar;
  1347. {
  1348. Create a PPAnsiChar to structure of pchars which are the arguments specified
  1349. in the string S. Especially useful for creating an ArgV for Exec-calls
  1350. }
  1351. begin
  1352. StringToPPChar:=StringToPPChar(PAnsiChar(S),ReserveEntries);
  1353. end;
  1354. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1355. Function StringToPPChar(S: PAnsiChar;ReserveEntries:integer):ppansichar;
  1356. var
  1357. i,nr : ObjpasInt;
  1358. Buf : ^ansichar;
  1359. p : ppansichar;
  1360. begin
  1361. buf:=s;
  1362. nr:=1;
  1363. while (buf^<>#0) do // count nr of args
  1364. begin
  1365. while (buf^ in [' ',#9,#10]) do // Kill separators.
  1366. inc(buf);
  1367. inc(nr);
  1368. if buf^='"' Then // quotes argument?
  1369. begin
  1370. inc(buf);
  1371. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1372. inc(buf);
  1373. if buf^='"' then // skip closing quote.
  1374. inc(buf);
  1375. end
  1376. else
  1377. begin // else std
  1378. while not (buf^ in [' ',#0,#9,#10]) do
  1379. inc(buf);
  1380. end;
  1381. end;
  1382. getmem(p,(ReserveEntries+nr)*sizeof(pansichar));
  1383. StringToPPChar:=p;
  1384. if p=nil then
  1385. begin
  1386. {$ifdef xunix}
  1387. fpseterrno(ESysEnomem);
  1388. {$endif}
  1389. exit;
  1390. end;
  1391. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  1392. buf:=s;
  1393. while (buf^<>#0) do
  1394. begin
  1395. while (buf^ in [' ',#9,#10]) do // Kill separators.
  1396. begin
  1397. buf^:=#0;
  1398. inc(buf);
  1399. end;
  1400. if buf^='"' Then // quotes argument?
  1401. begin
  1402. inc(buf);
  1403. p^:=buf;
  1404. inc(p);
  1405. p^:=nil;
  1406. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1407. inc(buf);
  1408. if buf^='"' then // skip closing quote.
  1409. begin
  1410. buf^:=#0;
  1411. inc(buf);
  1412. end;
  1413. end
  1414. else
  1415. begin
  1416. p^:=buf;
  1417. inc(p);
  1418. p^:=nil;
  1419. while not (buf^ in [' ',#0,#9,#10]) do
  1420. inc(buf);
  1421. end;
  1422. end;
  1423. end;
  1424. {$endif FPC_HAS_FEATURE_HEAP}
  1425. {*****************************************************************************
  1426. Abstract/Assert support.
  1427. *****************************************************************************}
  1428. procedure fpc_emptymethod;[public,alias : 'FPC_EMPTYMETHOD'];
  1429. begin
  1430. end;
  1431. procedure fpc_AbstractErrorIntern;compilerproc;[public,alias : 'FPC_ABSTRACTERROR'];
  1432. begin
  1433. If codepointer(AbstractErrorProc)<>nil then
  1434. AbstractErrorProc();
  1435. HandleErrorAddrFrameInd(211,get_pc_addr,get_frame);
  1436. end;
  1437. Procedure fpc_assert(Const Msg,FName:Shortstring;LineNo:Longint;
  1438. ErrorAddr:Pointer); [Public,Alias : 'FPC_ASSERT']; compilerproc;
  1439. begin
  1440. if codepointer(AssertErrorProc)<>nil then
  1441. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  1442. else
  1443. HandleErrorAddrFrameInd(227,get_pc_addr,get_frame);
  1444. end;
  1445. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
  1446. begin
  1447. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1448. If Length(msg)=0 then
  1449. write(stderr,'Assertion failed')
  1450. else
  1451. write(stderr,msg);
  1452. Writeln(stderr,' (',FName,', line ',LineNo,').');
  1453. Writeln(stderr,'');
  1454. Halt(227);
  1455. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1456. end;
  1457. {*****************************************************************************
  1458. SetJmp/LongJmp support.
  1459. *****************************************************************************}
  1460. {$i setjump.inc}
  1461. {$pop} //{$I-,R-,Q-} before 'procedure fpc_rangeerror'
  1462. {*****************************************************************************
  1463. Heap
  1464. *****************************************************************************}
  1465. {$i sysheap.inc}
  1466. {$i heap.inc}
  1467. {*****************************************************************************
  1468. Thread support
  1469. *****************************************************************************}
  1470. {$ifdef FPC_HAS_FEATURE_THREADING}
  1471. { Generic threadmanager }
  1472. {$i thread.inc}
  1473. { Generic threadvar support }
  1474. {$i threadvr.inc}
  1475. {$ifdef DISABLE_NO_THREAD_MANAGER}
  1476. { OS Dependent implementation }
  1477. {$i systhrd.inc}
  1478. {$endif DISABLE_NO_THREAD_MANAGER}
  1479. {$endif FPC_HAS_FEATURE_THREADING}
  1480. {*****************************************************************************
  1481. Dynamic library support
  1482. *****************************************************************************}
  1483. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  1484. {$i dynlib.inc}
  1485. {$ifdef DISABLE_NO_DYNLIBS_MANAGER}
  1486. { OS Dependant implementation }
  1487. {$i sysdl.inc}
  1488. {$endif DISABLE_NO_DYNLIBS_MANAGER}
  1489. {$endif FPC_HAS_FEATURE_DYNLIBS}
  1490. {*****************************************************************************
  1491. File Handling
  1492. *****************************************************************************}
  1493. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1494. { Allow slash and backslash as separators }
  1495. procedure DoDirSeparators(var p: pansichar; inplace: boolean = true);
  1496. var
  1497. i : ObjpasInt;
  1498. len : sizeint;
  1499. newp : pansichar;
  1500. begin
  1501. len:=length(p);
  1502. newp:=nil;
  1503. for i:=0 to len do
  1504. if p[i] in AllowDirectorySeparators then
  1505. begin
  1506. if not inplace and
  1507. not assigned(newp) then
  1508. begin
  1509. getmem(newp,len+1);
  1510. move(p^,newp^,len+1);
  1511. p:=newp;
  1512. end;
  1513. p[i]:=DirectorySeparator;
  1514. end;
  1515. end;
  1516. procedure DoDirSeparators(var p: pwidechar; inplace: boolean = true);
  1517. var
  1518. i : ObjpasInt;
  1519. len : sizeint;
  1520. newp : pwidechar;
  1521. begin
  1522. len:=length(p);
  1523. newp:=nil;
  1524. for i:=0 to len do
  1525. if (ord(p[i])<255) and
  1526. (ansichar(ord(p[i])) in AllowDirectorySeparators) then
  1527. begin
  1528. if not inplace and
  1529. not assigned(newp) then
  1530. begin
  1531. getmem(newp,(len+1)*2);
  1532. move(p^,newp^,(len+1)*2);
  1533. p:=newp;
  1534. end;
  1535. p[i]:=DirectorySeparator;
  1536. end;
  1537. end;
  1538. procedure DoDirSeparators(var p:shortstring);
  1539. var
  1540. i : ObjpasInt;
  1541. begin
  1542. for i:=1 to length(p) do
  1543. if p[i] in AllowDirectorySeparators then
  1544. p[i]:=DirectorySeparator;
  1545. end;
  1546. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1547. procedure DoDirSeparators(var ps:RawByteString);
  1548. var
  1549. i : ObjpasInt;
  1550. p : pansichar;
  1551. unique : boolean;
  1552. begin
  1553. unique:=false;
  1554. for i:=1 to length(ps) do
  1555. if ps[i] in AllowDirectorySeparators then
  1556. begin
  1557. if not unique then
  1558. begin
  1559. uniquestring(ps);
  1560. p:=pansichar(ps);
  1561. unique:=true;
  1562. end;
  1563. p[i-1]:=DirectorySeparator;
  1564. end;
  1565. end;
  1566. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1567. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  1568. procedure DoDirSeparators(var ps:UnicodeString);
  1569. var
  1570. i : ObjpasInt;
  1571. p : pwidechar;
  1572. unique : boolean;
  1573. begin
  1574. unique:=false;
  1575. for i:=1 to length(ps) do
  1576. if ps[i] in AllowDirectorySeparators then
  1577. begin
  1578. if not unique then
  1579. begin
  1580. uniquestring(ps);
  1581. p:=pwidechar(ps);
  1582. unique:=true;
  1583. end;
  1584. p[i-1]:=DirectorySeparator;
  1585. end;
  1586. end;
  1587. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  1588. {$endif FPC_HAS_FEATURE_FILEIO}
  1589. { OS dependent low level file functions }
  1590. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1591. {$i sysfile.inc}
  1592. {$ifndef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1593. {$ifdef FPC_ANSI_TEXTFILEREC}
  1594. procedure do_open(var f; p: pansichar; flags: longint; pchangeable: boolean);
  1595. var
  1596. u: UnicodeString;
  1597. begin
  1598. widestringmanager.Ansi2UnicodeMoveProc(p,DefaultFileSystemCodePage,u,length(p));
  1599. do_open(f,pwidechar(u),flags,true);
  1600. end;
  1601. procedure do_erase(p: pansichar; pchangeable: boolean);
  1602. var
  1603. u: UnicodeString;
  1604. begin
  1605. widestringmanager.Ansi2UnicodeMoveProc(p,DefaultFileSystemCodePage,u,length(p));
  1606. do_erase(pwidechar(u),true);
  1607. end;
  1608. procedure do_rename(src, dst: pansichar; srcchangeable, dstchangeable: boolean);
  1609. var
  1610. usrc, udst: UnicodeString;
  1611. begin
  1612. widestringmanager.Ansi2UnicodeMoveProc(src,DefaultFileSystemCodePage,usrc,length(src));
  1613. widestringmanager.Ansi2UnicodeMoveProc(dst,DefaultFileSystemCodePage,udst,length(dst));
  1614. do_rename(pwidechar(usrc),pwidechar(udst),true,true);
  1615. end;
  1616. procedure do_rename(src: pansichar; dst: pwidechar; srcchangeable, dstchangeable: boolean);
  1617. var
  1618. usrc: UnicodeString;
  1619. begin
  1620. widestringmanager.Ansi2UnicodeMoveProc(src,DefaultFileSystemCodePage,usrc,length(src));
  1621. do_rename(pwidechar(usrc),dst,true,dstchangeable);
  1622. end;
  1623. {$endif FPC_ANSI_TEXTFILEREC}
  1624. {$endif not FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1625. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1626. {$ifndef FPC_ANSI_TEXTFILEREC}
  1627. procedure do_open(var f; p: pwidechar; flags: longint; pchangeable: boolean);
  1628. var
  1629. s: RawByteString;
  1630. begin
  1631. widestringmanager.Unicode2AnsiMoveProc(p,s,DefaultFileSystemCodePage,length(p));
  1632. do_open(f,pansichar(s),flags,true);
  1633. end;
  1634. procedure do_erase(p: pwidechar; pchangeable: boolean);
  1635. var
  1636. s: RawByteString;
  1637. begin
  1638. widestringmanager.Unicode2AnsiMoveProc(p,s,DefaultFileSystemCodePage,length(p));
  1639. do_erase(pansichar(s),true);
  1640. end;
  1641. procedure do_rename(src, dst: pwidechar; srcchangeable, dstchangeable: boolean);
  1642. var
  1643. rsrc, rdst: RawByteString;
  1644. begin
  1645. widestringmanager.Unicode2AnsiMoveProc(src,rsrc,DefaultFileSystemCodePage,length(src));
  1646. widestringmanager.Unicode2AnsiMoveProc(dst,rdst,DefaultFileSystemCodePage,length(dst));
  1647. do_rename(pansichar(rsrc),pansichar(rdst),true,true);
  1648. end;
  1649. procedure do_rename(src: pwidechar; dst: pansichar; srcchangeable, dstchangeable: boolean);
  1650. var
  1651. rsrc: RawByteString;
  1652. begin
  1653. widestringmanager.Unicode2AnsiMoveProc(src,rsrc,DefaultFileSystemCodePage,length(src));
  1654. do_rename(pansichar(rsrc),dst,true,dstchangeable);
  1655. end;
  1656. {$endif not FPC_ANSI_TEXTFILEREC}
  1657. {$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1658. {$endif FPC_HAS_FEATURE_FILEIO}
  1659. { helper for targets supporting no ansistrings, it is used
  1660. by non-ansistring code }
  1661. function min(v1,v2 : SizeInt) : SizeInt;
  1662. begin
  1663. if v1<v2 then
  1664. result:=v1
  1665. else
  1666. result:=v2;
  1667. end;
  1668. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  1669. { Text file }
  1670. {$i text.inc}
  1671. {$endif FPC_HAS_FEATURE_TEXTIO}
  1672. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1673. { Untyped file }
  1674. {$i file.inc}
  1675. { Typed file }
  1676. {$i typefile.inc}
  1677. {$endif FPC_HAS_FEATURE_FILEIO}
  1678. {*****************************************************************************
  1679. Directory Handling
  1680. *****************************************************************************}
  1681. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1682. { OS dependent dir functions }
  1683. {$i sysdir.inc}
  1684. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1685. {$ifndef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1686. procedure do_getdir(drivenr : byte;var dir : rawbytestring);
  1687. var
  1688. u: unicodestring;
  1689. begin
  1690. Do_getdir(drivenr,u);
  1691. widestringmanager.Unicode2AnsiMoveProc(pwidechar(u),dir,DefaultRTLFileSystemCodePage,length(u));
  1692. end;
  1693. {$endif FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1694. Procedure MkDir(Const s: RawByteString);[IOCheck];
  1695. Begin
  1696. If (Length(s)=0) or (InOutRes <> 0) then
  1697. exit;
  1698. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1699. Do_mkdir(ToSingleByteFileSystemEncodedFileName(S));
  1700. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1701. Do_mkdir(S);
  1702. {$endif}
  1703. end;
  1704. Procedure RmDir(Const s: RawByteString);[IOCheck];
  1705. Begin
  1706. If (Length(s)=0) or (InOutRes <> 0) then
  1707. exit;
  1708. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1709. Do_rmdir(ToSingleByteFileSystemEncodedFileName(S));
  1710. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1711. Do_rmdir(S);
  1712. {$endif}
  1713. End;
  1714. Procedure ChDir(Const s: RawByteString);[IOCheck];
  1715. Begin
  1716. If (Length(s)=0) or (InOutRes <> 0) then
  1717. exit;
  1718. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1719. Do_chdir(ToSingleByteFileSystemEncodedFileName(S));
  1720. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1721. Do_chdir(S);
  1722. {$endif}
  1723. End;
  1724. Procedure getdir(drivenr:byte;Var dir:rawbytestring);
  1725. begin
  1726. Do_getdir(drivenr,dir);
  1727. { we should return results in the DefaultRTLFileSystemCodePage -> convert if
  1728. necessary }
  1729. setcodepage(dir,DefaultRTLFileSystemCodePage,true);
  1730. end;
  1731. { the generic shortstring ones are only implemented elsewhere for systems *not*
  1732. supporting ansi/unicodestrings; for now assume there are no systems that
  1733. support unicodestrings but not ansistrings }
  1734. { avoid double string conversions }
  1735. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1736. function GetDirStrFromShortstring(const s: shortstring): RawByteString;
  1737. begin
  1738. GetDirStrFromShortstring:=ToSingleByteFileSystemEncodedFileName(ansistring(s));
  1739. end;
  1740. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1741. function GetDirStrFromShortstring(const s: shortstring): UnicodeString;
  1742. begin
  1743. GetDirStrFromShortstring:=s;
  1744. end;
  1745. {$endif FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1746. Procedure MkDir(Const s: shortstring);[IOCheck];
  1747. Begin
  1748. If (Length(s)=0) or (InOutRes <> 0) then
  1749. exit;
  1750. Do_mkdir(GetDirStrFromShortstring(S));
  1751. End;
  1752. Procedure RmDir(Const s: shortstring);[IOCheck];
  1753. Begin
  1754. If (Length(s)=0) or (InOutRes <> 0) then
  1755. exit;
  1756. Do_rmdir(GetDirStrFromShortstring(S));
  1757. End;
  1758. Procedure ChDir(Const s: shortstring);[IOCheck];
  1759. Begin
  1760. If (Length(S)=0) or (InOutRes <> 0) then
  1761. exit;
  1762. Do_chdir(GetDirStrFromShortstring(S));
  1763. End;
  1764. Procedure getdir(drivenr:byte;Var dir:shortstring);
  1765. var
  1766. s: rawbytestring;
  1767. begin
  1768. Do_getdir(drivenr,s);
  1769. if length(s)<=high(dir) then
  1770. dir:=s
  1771. else
  1772. inoutres:=3;
  1773. end;
  1774. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1775. {$if defined(FPC_HAS_FEATURE_WIDESTRINGS)}
  1776. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1777. { overloads required for mkdir/rmdir/chdir to ensure that the string is
  1778. converted to the right code page }
  1779. procedure do_mkdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1780. begin
  1781. do_mkdir(ToSingleByteFileSystemEncodedFileName(s));
  1782. end;
  1783. procedure do_rmdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1784. begin
  1785. do_rmdir(ToSingleByteFileSystemEncodedFileName(s));
  1786. end;
  1787. procedure do_chdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1788. begin
  1789. do_chdir(ToSingleByteFileSystemEncodedFileName(s));
  1790. end;
  1791. procedure do_getdir(drivenr : byte;var dir : unicodestring);
  1792. var
  1793. s: rawbytestring;
  1794. begin
  1795. Do_getdir(drivenr,s);
  1796. dir:=unicodestring(s);
  1797. end;
  1798. {$endif FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1799. Procedure MkDir(Const s: UnicodeString);[IOCheck];
  1800. Begin
  1801. if (Length(s)=0) or (InOutRes <> 0) then
  1802. exit;
  1803. Do_mkdir(S);
  1804. End;
  1805. Procedure RmDir(Const s: UnicodeString);[IOCheck];
  1806. Begin
  1807. if (Length(s)=0) or (InOutRes <> 0) then
  1808. exit;
  1809. Do_rmdir(S);
  1810. End;
  1811. Procedure ChDir(Const s: UnicodeString);[IOCheck];
  1812. Begin
  1813. if (Length(s)=0) or (InOutRes <> 0) then
  1814. exit;
  1815. Do_chdir(S);
  1816. End;
  1817. Procedure getdir(drivenr:byte;Var dir:unicodestring);
  1818. begin
  1819. Do_getdir(drivenr,dir);
  1820. end;
  1821. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1822. {$endif FPC_HAS_FEATURE_FILEIO}
  1823. {*****************************************************************************
  1824. Resources support
  1825. *****************************************************************************}
  1826. {$i sysres.inc}
  1827. const
  1828. CtrlBreakHandler: TCtrlBreakHandler = nil;
  1829. {$IFNDEF FPC_HAS_SETCTRLBREAKHANDLER}
  1830. (* It is possible to provide platform specific implementation performing *)
  1831. (* special initialization; default implementation just sets the procedural *)
  1832. (* variable to make it available for use from the exception handler. *)
  1833. function SysSetCtrlBreakHandler (Handler: TCtrlBreakHandler): TCtrlBreakHandler;
  1834. begin
  1835. (* Return either nil or previous handler *)
  1836. SysSetCtrlBreakHandler := CtrlBreakHandler;
  1837. CtrlBreakHandler := Handler;
  1838. end;
  1839. {$ENDIF FPC_HAS_SETCTRLBREAKHANDLER}
  1840. {$ifdef cpu16}
  1841. function AtomicIncrement (var Target: smallint) : smallint; {$ifdef SYSTEMINLINE}inline{$endif};
  1842. begin
  1843. Result:=InterlockedIncrement(Target);
  1844. end;
  1845. function AtomicDecrement (var Target: smallint) : smallint; {$ifdef SYSTEMINLINE}inline{$endif};
  1846. begin
  1847. Result:=InterlockedDecrement(Target);
  1848. end;
  1849. function AtomicCmpExchange(var Target: smallint; NewValue, Comperand: smallint): smallint; {$ifdef SYSTEMINLINE}inline{$endif};
  1850. begin
  1851. Result:=InterlockedCompareExchange(Target,NewValue,Comperand);
  1852. end;
  1853. function AtomicExchange (var Target: smallint;Source : smallint) : smallint; {$ifdef SYSTEMINLINE}inline{$endif};
  1854. begin
  1855. Result:=InterlockedExchange(Target,Source);
  1856. end;
  1857. function AtomicIncrement (var Target: word) : word; {$ifdef SYSTEMINLINE}inline{$endif};
  1858. begin
  1859. Result:=InterlockedIncrement(Target);
  1860. end;
  1861. function AtomicDecrement (var Target: word) : word; {$ifdef SYSTEMINLINE}inline{$endif};
  1862. begin
  1863. Result:=InterlockedDecrement(Target);
  1864. end;
  1865. function AtomicCmpExchange(var Target: word; NewValue, Comperand: word): word; {$ifdef SYSTEMINLINE}inline{$endif};
  1866. begin
  1867. Result:=InterlockedCompareExchange(Target, NewValue, Comperand);
  1868. end;
  1869. function AtomicExchange (var Target: word;Source : word) : word; {$ifdef SYSTEMINLINE}inline{$endif};
  1870. begin
  1871. Result:=InterlockedExchange(Target,Source);
  1872. end;
  1873. {$endif cpu16}
  1874. function AtomicIncrement (var Target: longint) : longint; {$ifdef SYSTEMINLINE}inline{$endif};
  1875. begin
  1876. Result:=InterlockedIncrement(Target);
  1877. end;
  1878. function AtomicDecrement (var Target: longint) : longint; {$ifdef SYSTEMINLINE}inline{$endif};
  1879. begin
  1880. Result:=InterlockedDecrement(Target);
  1881. end;
  1882. function AtomicCmpExchange(var Target: longint; NewValue, Comperand: longint): longint; {$ifdef SYSTEMINLINE}inline{$endif};
  1883. begin
  1884. Result:=InterlockedCompareExchange(Target,NewValue, Comperand);
  1885. end;
  1886. function AtomicExchange (var Target: longint;Source : longint) : longint; {$ifdef SYSTEMINLINE}inline{$endif};
  1887. begin
  1888. Result:=InterlockedExchange(Target,Source);
  1889. end;
  1890. {$ifdef cpu64}
  1891. function AtomicIncrement (var Target: int64) : int64; {$ifdef SYSTEMINLINE}inline{$endif};
  1892. begin
  1893. Result:=InterlockedIncrement64(Target);
  1894. end;
  1895. function AtomicDecrement (var Target: int64) : int64; {$ifdef SYSTEMINLINE}inline{$endif};
  1896. begin
  1897. Result:=InterlockedDecrement64(Target);
  1898. end;
  1899. function AtomicCmpExchange(var Target: int64; NewValue, Comperand: int64): int64; {$ifdef SYSTEMINLINE}inline{$endif};
  1900. begin
  1901. Result:=InterlockedCompareExchange64(Target,NewValue, Comperand);
  1902. end;
  1903. function AtomicExchange (var Target: int64;Source : int64) : int64; {$ifdef SYSTEMINLINE}inline{$endif};
  1904. begin
  1905. Result:=InterlockedExchange64(Target,Source);
  1906. end;
  1907. {$endif cpu64}
  1908. { Pointer overloads }
  1909. {$ifndef FPC_SYSTEM_DISABLE_INTERLOCK_POINTER_OVERLOAD}
  1910. function AtomicIncrement (var Target: pointer) : pointer; {$ifdef SYSTEMINLINE}inline{$endif};
  1911. begin
  1912. {$IFDEF CPU64}
  1913. Result:=Pointer(InterlockedIncrement64(int64(Target)));
  1914. {$ELSE}
  1915. {$IFDEF CPU16}
  1916. Result:=Pointer(InterlockedIncrement(smallint(Target)));
  1917. {$ELSE}
  1918. Result:=Pointer(InterlockedIncrement(Longint(Target)));
  1919. {$ENDIF}
  1920. {$ENDIF}
  1921. end;
  1922. function AtomicDecrement (var Target: pointer) : pointer; {$ifdef SYSTEMINLINE}inline{$endif};
  1923. begin
  1924. {$IFDEF CPU64}
  1925. Result:=Pointer(InterlockedDecrement64(Int64(Target)));
  1926. {$ELSE}
  1927. {$IFDEF CPU16}
  1928. Result:=Pointer(InterlockedDecrement(smallint(Target)));
  1929. {$ELSE}
  1930. Result:=Pointer(InterlockedDecrement(Longint(Target)));
  1931. {$ENDIF}
  1932. {$ENDIF}
  1933. end;
  1934. function AtomicCmpExchange(var Target: pointer; NewValue, Comperand: pointer): pointer; {$ifdef SYSTEMINLINE}inline{$endif};
  1935. begin
  1936. {$IFDEF CPU64}
  1937. Result:=Pointer(InterlockedCompareExchange64(Int64(Target),Int64(NewValue), Int64(Comperand)));
  1938. {$ELSE}
  1939. {$IFDEF CPU16}
  1940. Result:=Pointer(InterlockedCompareExchange(smallint(Target),smallint(NewValue),smallint(Comperand)));
  1941. {$ELSE}
  1942. Result:=Pointer(InterlockedCompareExchange(LongInt(Target),LongInt(NewValue), LongInt(Comperand)));
  1943. {$ENDIF}
  1944. {$ENDIF}
  1945. end;
  1946. function AtomicExchange(var Target: pointer;Source : pointer) : pointer; {$ifdef SYSTEMINLINE}inline{$endif};
  1947. begin
  1948. {$IFDEF CPU64}
  1949. Result:=Pointer(InterlockedExchange64(Int64(Target),Int64(Source)));
  1950. {$ELSE}
  1951. {$IFDEF CPU16}
  1952. Result:=Pointer(InterlockedExchange(smallint(Target),smallint(Source)));
  1953. {$ELSE}
  1954. Result:=Pointer(InterlockedExchange(LongInt(Target),LongInt(Source)));
  1955. {$ENDIF}
  1956. {$ENDIF}
  1957. end;
  1958. {$endif FPC_SYSTEM_DISABLE_INTERLOCK_POINTER_OVERLOAD}
  1959. function AtomicIncrement (var Target: Cardinal) : Cardinal; {$ifdef SYSTEMINLINE}inline{$endif};
  1960. begin
  1961. Result:=Cardinal(InterlockedIncrement(Longint(Target)));
  1962. end;
  1963. function AtomicDecrement (var Target: Cardinal) : Cardinal; {$ifdef SYSTEMINLINE}inline{$endif};
  1964. begin
  1965. Result:=Cardinal(InterlockedIncrement(Longint(Target)));
  1966. end;
  1967. function AtomicCmpExchange(var Target: Cardinal; NewValue, Comperand: Cardinal): Cardinal; {$ifdef SYSTEMINLINE}inline{$endif};
  1968. begin
  1969. Result:=Cardinal(InterlockedCompareExchange(Longint(Target),Longint(NewValue), Longint(Comperand)));
  1970. end;
  1971. function AtomicExchange (var Target: Cardinal;Source : Cardinal) : Cardinal; {$ifdef SYSTEMINLINE}inline{$endif};
  1972. begin
  1973. Result:=Cardinal(InterlockedExchange(Longint(Target),Longint(Source)));
  1974. end;
  1975. {$ifdef cpu64}
  1976. function AtomicIncrement (var Target: qword) : qword; {$ifdef SYSTEMINLINE}inline{$endif};
  1977. begin
  1978. Result:=QWord(InterlockedIncrement64(Int64(Target)));
  1979. end;
  1980. function AtomicDecrement (var Target: qword) : qword; {$ifdef SYSTEMINLINE}inline{$endif};
  1981. begin
  1982. Result:=QWord(InterlockedDecrement64(int64(Target)));
  1983. end;
  1984. function AtomicCmpExchange(var Target: qword; NewValue, Comperand: qword): qword; {$ifdef SYSTEMINLINE}inline{$endif};
  1985. begin
  1986. Result:=QWord(InterlockedCompareExchange64(Int64(Target),Int64(NewValue), Int64(Comperand)));
  1987. end;
  1988. function AtomicExchange (var Target: qword;Source : qword) : qword; {$ifdef SYSTEMINLINE}inline{$endif};
  1989. begin
  1990. Result:=QWord(InterlockedExchange64(Int64(Target),Int64(Source)));
  1991. end;
  1992. {$endif}