2
0

system.inc 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463
  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)}
  447. result := aligntoqword(p);
  448. {$else CPUM68K}
  449. result := aligntoptr(p);
  450. {$endif CPUM68K}
  451. end;
  452. {$if 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_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 : ALUUInt;
  937. Procs : array[1..maxunits] of TInitFinalRec;
  938. end;
  939. PInitFinalTable = ^TInitFinalTable;
  940. {$ifndef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  941. var
  942. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  943. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  944. procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; compilerproc;
  945. var
  946. i : ALUUInt;
  947. pt : PInitFinalTable;
  948. begin
  949. { call cpu/fpu initialisation routine }
  950. fpc_cpuinit;
  951. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  952. pt := PInitFinalTable(EntryInformation.InitFinalTable);
  953. {$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  954. pt := @InitFinalTable;
  955. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  956. with pt^ do
  957. begin
  958. for i:=1 to ALUUInt(TableCount) do
  959. begin
  960. if assigned(Procs[i].InitProc) then
  961. Procs[i].InitProc();
  962. InitCount:=i;
  963. end;
  964. end;
  965. if assigned(InitProc) then
  966. TProcedure(InitProc)();
  967. end;
  968. procedure internal_initializeunits; external name 'FPC_INITIALIZEUNITS';
  969. procedure fpc_LibInitializeUnits;[public,alias:'FPC_LIBINITIALIZEUNITS'];compilerproc;
  970. begin
  971. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  972. IsLibrary:=true;
  973. { must also be set to true for packages when implemented }
  974. ModuleIsLib:=true;
  975. internal_initializeunits;
  976. {$endif FPC_HAS_FEATURE_DYNLIBS}
  977. end;
  978. {$ifdef FPC_INIT_FINAL_UNITS_BY_CALLS}
  979. procedure FinalizeUnits; external name 'FPC_FINALIZE_FUNC_TABLE';
  980. {$else FPC_INIT_FINAL_UNITS_BY_CALLS}
  981. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  982. begin
  983. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  984. with PInitFinalTable(EntryInformation.InitFinalTable)^ do
  985. {$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  986. with InitFinalTable do
  987. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  988. begin
  989. while (InitCount>0) do
  990. begin
  991. // we've to decrement the cound before calling the final. code
  992. // else a halt in the final. code leads to a endless loop
  993. dec(InitCount);
  994. if assigned(Procs[InitCount+1].FinalProc) then
  995. Procs[InitCount+1].FinalProc();
  996. end;
  997. end;
  998. end;
  999. {$endif FPC_INIT_FINAL_UNITS_BY_CALLS}
  1000. {*****************************************************************************
  1001. FPU Exceptions state
  1002. *****************************************************************************}
  1003. {$if defined(FPC_SYSTEM_FPUCW_IMMUTABLE) or defined(FPU_NONE)}
  1004. function GetNativeFPUControlWord: TNativeFPUControlWord; {$if defined(SYSTEMINLINE)}inline;{$endif}
  1005. begin
  1006. result:=default(TNativeFPUControlWord);
  1007. end;
  1008. procedure SetNativeFPUControlWord(const cw: TNativeFPUControlWord); {$if defined(SYSTEMINLINE)}inline;{$endif}
  1009. begin
  1010. end;
  1011. {$endif}
  1012. {*****************************************************************************
  1013. Error / Exit / ExitProc
  1014. *****************************************************************************}
  1015. Procedure system_exit;forward;{$ifdef FPC_SYSTEM_EXIT_NO_RETURN}noreturn;{$endif}
  1016. {$ifdef FPC_HAS_FEATURE_HEAP}
  1017. {$ifndef HAS_MEMORYMANAGER}
  1018. //not needed if independant memory manager
  1019. Procedure FinalizeHeap;forward;
  1020. {$endif HAS_MEMORYMANAGER}
  1021. {$endif FPC_HAS_FEATURE_HEAP}
  1022. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1023. procedure SysFlushStdIO;
  1024. begin
  1025. { Make sure that all output is written to the redirected file }
  1026. if Textrec(Output).Mode=fmOutput then
  1027. Flush(Output);
  1028. if Textrec(ErrOutput).Mode=fmOutput then
  1029. Flush(ErrOutput);
  1030. {$ifndef FPC_STDOUT_TRUE_ALIAS}
  1031. if Textrec(stdout).Mode=fmOutput then
  1032. Flush(stdout);
  1033. if Textrec(StdErr).Mode=fmOutput then
  1034. Flush(StdErr);
  1035. {$endif FPC_STDOUT_TRUE_ALIAS}
  1036. end;
  1037. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1038. Procedure InternalExit;
  1039. var
  1040. current_exit : Procedure;
  1041. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1042. pstdout : ^Text;
  1043. {$endif}
  1044. {$if defined(MSWINDOWS) or defined(OS2)}
  1045. i : longint;
  1046. {$endif}
  1047. Begin
  1048. {$ifdef SYSTEMDEBUG}
  1049. writeln('InternalExit');
  1050. {$endif SYSTEMDEBUG}
  1051. {$ifndef CPUAVR}
  1052. while exitProc<>nil Do
  1053. Begin
  1054. InOutRes:=0;
  1055. current_exit:=tProcedure(exitProc);
  1056. exitProc:=nil;
  1057. current_exit();
  1058. End;
  1059. {$endif CPUAVR}
  1060. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1061. { the embedded system unit itself contains no routines for console i/o
  1062. console i/o is done by the Consoleio unit which can do things like
  1063. redirection to seriell i/o }
  1064. {$ifndef EMBEDDED}
  1065. { Show runtime error and exit }
  1066. if WriteErrorsToStdErr then
  1067. pstdout:=@stderr
  1068. else
  1069. pstdout:=@stdout;
  1070. If erroraddr<>nil Then
  1071. Begin
  1072. Writeln(pstdout^,'Runtime error ',Errorcode,' at $',hexstr(erroraddr));
  1073. { to get a nice symify }
  1074. Writeln(pstdout^,BackTraceStrFunc(Erroraddr));
  1075. dump_stack(pstdout^,ErrorBase,ErrorAddr);
  1076. Writeln(pstdout^,'');
  1077. End;
  1078. SysFlushStdIO;
  1079. {$endif EMBEDDED}
  1080. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1081. { Finalize units }
  1082. FinalizeUnits;
  1083. {$if defined(OS2)}
  1084. { finally release the heap if possible, especially
  1085. important for DLLs.
  1086. Reset the array to nil, and finally also argv itself to
  1087. avoid double freeing problem in case this function gets called twice. }
  1088. if assigned(argv) then
  1089. begin
  1090. for i:=0 to argc-1 do
  1091. if assigned(argv[i]) then
  1092. begin
  1093. sysfreemem(argv[i]);
  1094. argv[i]:=nil;
  1095. end;
  1096. sysfreemem(argv);
  1097. argv:=nil;
  1098. end;
  1099. {$endif}
  1100. {$ifdef LINUX}
  1101. {sysfreemem already checks for nil}
  1102. { Do not try to do anything if the heap manager already reported an error }
  1103. if (errorcode<>203) and (errorcode<>204) then
  1104. sysfreemem(calculated_cmdline);
  1105. {$endif}
  1106. {$ifdef BSD}
  1107. { Do not try to do anything if the heap manager already reported an error }
  1108. if (errorcode<>203) and (errorcode<>204) then
  1109. sysfreemem(cmdline);
  1110. {$endif}
  1111. {$ifdef FPC_HAS_FEATURE_HEAP}
  1112. {$ifndef HAS_MEMORYMANAGER}
  1113. {$ifndef FPC_NO_DEFAULT_HEAP}
  1114. FinalizeHeap;
  1115. {$endif not FPC_NO_DEFAULT_HEAP}
  1116. {$endif not HAS_MEMORYMANAGER}
  1117. {$endif FPC_HAS_FEATURE_HEAP}
  1118. End;
  1119. Procedure fpc_do_exit;[Public,Alias:'FPC_DO_EXIT']; compilerproc;
  1120. begin
  1121. InternalExit;
  1122. System_exit;
  1123. end;
  1124. procedure internal_do_exit; external name 'FPC_DO_EXIT';
  1125. Procedure fpc_lib_exit;[Public,Alias:'FPC_LIB_EXIT'];
  1126. begin
  1127. InternalExit;
  1128. end;
  1129. Procedure Halt(ErrNum: TExitCode);noreturn;
  1130. Begin
  1131. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  1132. {$ifdef FPC_LIMITED_EXITCODE}
  1133. if ErrNum > maxExitCode then
  1134. ExitCode:=255
  1135. else
  1136. {$endif FPC_LIMITED_EXITCODE}
  1137. ExitCode:=ErrNum;
  1138. {$endif FPC_HAS_FEATURE_EXITCODE}
  1139. internal_do_exit;
  1140. end;
  1141. {$ifndef FPC_SYSTEM_HAS_BACKTRACESTR}
  1142. function SysBackTraceStr (Addr: CodePointer): ShortString;
  1143. begin
  1144. SysBackTraceStr:=' $'+hexstr(addr);
  1145. end;
  1146. {$endif FPC_SYSTEM_HAS_BACKTRACESTR}
  1147. {$ifndef FPC_SYSTEM_HAS_CAPTUREBACKTRACE}
  1148. function CaptureBacktrace(skipframes,count:sizeint;frames:PCodePointer):sizeint;
  1149. var
  1150. curr_frame,prev_frame: pointer;
  1151. curr_addr: codepointer;
  1152. i: sizeint;
  1153. begin
  1154. curr_frame:=get_frame;
  1155. curr_addr:=get_pc_addr;
  1156. prev_frame:=curr_frame;
  1157. get_caller_stackinfo(curr_frame,curr_addr);
  1158. i:=-skipframes;
  1159. while (i<count) and (curr_frame>prev_frame) and
  1160. (curr_frame<StackTop) do
  1161. begin
  1162. if (curr_addr=nil) or
  1163. (curr_frame=nil) then
  1164. break;
  1165. if (i>=0) then
  1166. frames[i]:=curr_addr;
  1167. inc(i);
  1168. prev_frame:=curr_frame;
  1169. get_caller_stackinfo(curr_frame,curr_addr);
  1170. end;
  1171. if i<0 then
  1172. result:=0
  1173. else
  1174. result:=i;
  1175. end;
  1176. {$endif FPC_SYSTEM_HAS_CAPTUREBACKTRACE}
  1177. Procedure HandleErrorAddrFrame (Errno : TExitCode;addr : CodePointer; frame : Pointer);[public,alias:'FPC_BREAK_ERROR']; {$ifdef CPUI386} register; {$endif}
  1178. begin
  1179. If codepointer(ErrorProc)<>Nil then
  1180. ErrorProc(Errno,addr,frame);
  1181. errorcode:=word(Errno);
  1182. erroraddr:=addr;
  1183. errorbase:=frame;
  1184. Halt(errorcode);
  1185. end;
  1186. { This is used internally by system skip first level,
  1187. and generated the same output as before, when
  1188. HandleErrorFrame function was used internally. }
  1189. Procedure HandleErrorAddrFrameInd (Errno : TExitCode;addr : CodePointer; frame : Pointer);
  1190. begin
  1191. get_caller_stackinfo (frame, addr);
  1192. HandleErrorAddrFrame (Errno,addr,frame);
  1193. end;
  1194. Procedure HandleErrorFrame (Errno : TExitCode;frame : Pointer);
  1195. {
  1196. Procedure to handle internal errors, i.e. not user-invoked errors
  1197. Internal function should ALWAYS call HandleError instead of RunError.
  1198. Can be used for exception handlers to specify the frame
  1199. }
  1200. begin
  1201. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  1202. end;
  1203. procedure fpc_handleerror (Errno : TExitCode); compilerproc; [public,alias : 'FPC_HANDLEERROR'];
  1204. {
  1205. Procedure to handle internal errors, i.e. not user-invoked errors
  1206. Internal function should ALWAYS call HandleError instead of RunError.
  1207. }
  1208. begin
  1209. HandleErrorAddrFrameInd(Errno,get_pc_addr,get_frame);
  1210. end;
  1211. procedure RunError(w : word);[alias: 'FPC_RUNERROR'];noreturn;
  1212. var
  1213. bp : pointer;
  1214. pcaddr : codepointer;
  1215. begin
  1216. errorcode:=w;
  1217. pcaddr:=get_pc_addr;
  1218. bp:=get_frame;
  1219. get_caller_stackinfo(bp,pcaddr);
  1220. erroraddr:=pcaddr;
  1221. errorbase:=bp;
  1222. Halt(errorcode);
  1223. end;
  1224. Procedure RunError;{$ifdef SYSTEMINLINE}inline;{$endif}noreturn;
  1225. Begin
  1226. RunError (0);
  1227. End;
  1228. Procedure Halt;{$ifdef SYSTEMINLINE}inline;{$endif}noreturn;
  1229. Begin
  1230. Halt(0);
  1231. End;
  1232. Procedure Error(RunTimeError : TRunTimeError);
  1233. begin
  1234. RunError(RuntimeErrorExitCodes[RunTimeError]);
  1235. end;
  1236. Procedure dump_stack(var f : text;fp : Pointer; addr : CodePointer);
  1237. var
  1238. i : ObjpasInt;
  1239. prevfp : Pointer;
  1240. is_dev : boolean;
  1241. Begin
  1242. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1243. try
  1244. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1245. { Frame of this procedure acts as StackBottom, fp values below that are invalid. }
  1246. prevfp:=get_frame;
  1247. i:=0;
  1248. is_dev:=do_isdevice(textrec(f).Handle);
  1249. { sanity checks, new frame pointer must be always greater than the old one, further
  1250. it must point into the stack area, else something went wrong }
  1251. while (fp>prevfp) and (fp<StackTop) do
  1252. Begin
  1253. prevfp:=fp;
  1254. get_caller_stackinfo(fp,addr);
  1255. if (addr=nil) then
  1256. break;
  1257. Writeln(f,BackTraceStrFunc(addr));
  1258. if (fp=nil) then
  1259. break;
  1260. Inc(i);
  1261. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  1262. break;
  1263. End;
  1264. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1265. except
  1266. { prevent endless dump if an exception occurred }
  1267. end;
  1268. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1269. End;
  1270. procedure dump_stack(var f: text; skipframes: longint);
  1271. var
  1272. i,count: ObjpasInt;
  1273. frames: array [0..255] of codepointer;
  1274. begin
  1275. if do_isdevice(textrec(f).handle) then
  1276. count:=max_frame_dump
  1277. else
  1278. count:=255;
  1279. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1280. try
  1281. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1282. count:=CaptureBacktrace(skipframes+1,count,@frames[0]);
  1283. for i:=0 to count-1 do
  1284. writeln(f,BackTraceStrFunc(frames[i]));
  1285. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1286. except
  1287. end;
  1288. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1289. end;
  1290. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1291. procedure DumpExceptionBackTrace(var f:text);
  1292. var
  1293. FrameNumber,
  1294. FrameCount : ObjpasInt;
  1295. Frames : PCodePointer;
  1296. begin
  1297. if RaiseList=nil then
  1298. exit;
  1299. WriteLn(f,BackTraceStrFunc(RaiseList^.Addr));
  1300. FrameCount:=RaiseList^.Framecount;
  1301. Frames:=RaiseList^.Frames;
  1302. for FrameNumber := 0 to FrameCount-1 do
  1303. WriteLn(f,BackTraceStrFunc(Frames[FrameNumber]));
  1304. end;
  1305. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1306. {$ifdef FPC_HAS_FEATURE_HEAP}
  1307. Type
  1308. PExitProcInfo = ^TExitProcInfo;
  1309. TExitProcInfo = Record
  1310. Next : PExitProcInfo;
  1311. SaveExit : CodePointer;
  1312. Proc : TProcedure;
  1313. End;
  1314. const
  1315. ExitProcList: PExitProcInfo = nil;
  1316. Procedure DoExitProc;
  1317. var
  1318. P : PExitProcInfo;
  1319. Proc : TProcedure;
  1320. Begin
  1321. P:=ExitProcList;
  1322. ExitProcList:=P^.Next;
  1323. ExitProc:=P^.SaveExit;
  1324. Proc:=P^.Proc;
  1325. DisPose(P);
  1326. Proc();
  1327. End;
  1328. Procedure AddExitProc(Proc: TProcedure);
  1329. var
  1330. P : PExitProcInfo;
  1331. Begin
  1332. New(P);
  1333. P^.Next:=ExitProcList;
  1334. P^.SaveExit:=ExitProc;
  1335. P^.Proc:=Proc;
  1336. ExitProcList:=P;
  1337. ExitProc:=@DoExitProc;
  1338. End;
  1339. {$endif FPC_HAS_FEATURE_HEAP}
  1340. {$ifdef FPC_HAS_FEATURE_HEAP}
  1341. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1342. function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):ppansichar; // const ?
  1343. {$ifdef EXCLUDE_COMPLEX_PROCS}
  1344. begin
  1345. runerror(217);
  1346. end;
  1347. {$else EXCLUDE_COMPLEX_PROCS}
  1348. // Extra allocate reserveentries pansichar's at the beginning (default param=0 after 1.0.x ?)
  1349. // Note: for internal use by skilled programmers only
  1350. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  1351. var p : ppansichar;
  1352. i : ObjpasInt;
  1353. begin
  1354. if High(s)<Low(s) Then Exit(NIL);
  1355. Getmem(p,sizeof(pansichar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  1356. // for cmd
  1357. if p=nil then
  1358. begin
  1359. {$ifdef xunix}
  1360. fpseterrno(ESysEnomem);
  1361. {$endif}
  1362. exit(NIL);
  1363. end;
  1364. for i:=low(s) to high(s) do
  1365. p[i+Reserveentries]:=pansichar(s[i]);
  1366. p[high(s)+1+Reserveentries]:=nil;
  1367. ArrayStringToPPchar:=p;
  1368. end;
  1369. {$endif EXCLUDE_COMPLEX_PROCS}
  1370. Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):ppansichar;
  1371. {
  1372. Create a PPAnsiChar to structure of pchars which are the arguments specified
  1373. in the string S. Especially useful for creating an ArgV for Exec-calls
  1374. }
  1375. begin
  1376. StringToPPChar:=StringToPPChar(PAnsiChar(S),ReserveEntries);
  1377. end;
  1378. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1379. Function StringToPPChar(S: PAnsiChar;ReserveEntries:integer):ppansichar;
  1380. var
  1381. i,nr : ObjpasInt;
  1382. Buf : ^ansichar;
  1383. p : ppansichar;
  1384. begin
  1385. buf:=s;
  1386. nr:=1;
  1387. while (buf^<>#0) do // count nr of args
  1388. begin
  1389. while (buf^ in [' ',#9,#10]) do // Kill separators.
  1390. inc(buf);
  1391. inc(nr);
  1392. if buf^='"' Then // quotes argument?
  1393. begin
  1394. inc(buf);
  1395. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1396. inc(buf);
  1397. if buf^='"' then // skip closing quote.
  1398. inc(buf);
  1399. end
  1400. else
  1401. begin // else std
  1402. while not (buf^ in [' ',#0,#9,#10]) do
  1403. inc(buf);
  1404. end;
  1405. end;
  1406. getmem(p,(ReserveEntries+nr)*sizeof(pansichar));
  1407. StringToPPChar:=p;
  1408. if p=nil then
  1409. begin
  1410. {$ifdef xunix}
  1411. fpseterrno(ESysEnomem);
  1412. {$endif}
  1413. exit;
  1414. end;
  1415. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  1416. buf:=s;
  1417. while (buf^<>#0) do
  1418. begin
  1419. while (buf^ in [' ',#9,#10]) do // Kill separators.
  1420. begin
  1421. buf^:=#0;
  1422. inc(buf);
  1423. end;
  1424. if buf^='"' Then // quotes argument?
  1425. begin
  1426. inc(buf);
  1427. p^:=buf;
  1428. inc(p);
  1429. p^:=nil;
  1430. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1431. inc(buf);
  1432. if buf^='"' then // skip closing quote.
  1433. begin
  1434. buf^:=#0;
  1435. inc(buf);
  1436. end;
  1437. end
  1438. else
  1439. begin
  1440. p^:=buf;
  1441. inc(p);
  1442. p^:=nil;
  1443. while not (buf^ in [' ',#0,#9,#10]) do
  1444. inc(buf);
  1445. end;
  1446. end;
  1447. end;
  1448. {$endif FPC_HAS_FEATURE_HEAP}
  1449. {*****************************************************************************
  1450. Abstract/Assert support.
  1451. *****************************************************************************}
  1452. procedure fpc_emptymethod;[public,alias : 'FPC_EMPTYMETHOD'];
  1453. begin
  1454. end;
  1455. procedure fpc_AbstractErrorIntern;compilerproc;[public,alias : 'FPC_ABSTRACTERROR'];
  1456. begin
  1457. If codepointer(AbstractErrorProc)<>nil then
  1458. AbstractErrorProc();
  1459. HandleErrorAddrFrameInd(211,get_pc_addr,get_frame);
  1460. end;
  1461. Procedure fpc_assert(Const Msg,FName:Shortstring;LineNo:Longint;
  1462. ErrorAddr:Pointer); [Public,Alias : 'FPC_ASSERT']; compilerproc;
  1463. begin
  1464. if codepointer(AssertErrorProc)<>nil then
  1465. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  1466. else
  1467. HandleErrorAddrFrameInd(227,get_pc_addr,get_frame);
  1468. end;
  1469. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
  1470. begin
  1471. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1472. If Length(msg)=0 then
  1473. write(stderr,'Assertion failed')
  1474. else
  1475. write(stderr,msg);
  1476. Writeln(stderr,' (',FName,', line ',LineNo,').');
  1477. Writeln(stderr,'');
  1478. Halt(227);
  1479. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1480. end;
  1481. {*****************************************************************************
  1482. SetJmp/LongJmp support.
  1483. *****************************************************************************}
  1484. {$i setjump.inc}
  1485. {$pop} //{$I-,R-,Q-} before 'procedure fpc_rangeerror'
  1486. {*****************************************************************************
  1487. Heap
  1488. *****************************************************************************}
  1489. {$i sysheap.inc}
  1490. {$i heap.inc}
  1491. {*****************************************************************************
  1492. Thread support
  1493. *****************************************************************************}
  1494. {$ifdef FPC_HAS_FEATURE_THREADING}
  1495. { Generic threadmanager }
  1496. {$i thread.inc}
  1497. { Generic threadvar support }
  1498. {$i threadvr.inc}
  1499. {$ifdef DISABLE_NO_THREAD_MANAGER}
  1500. { OS Dependent implementation }
  1501. {$i systhrd.inc}
  1502. {$endif DISABLE_NO_THREAD_MANAGER}
  1503. {$endif FPC_HAS_FEATURE_THREADING}
  1504. {*****************************************************************************
  1505. Dynamic library support
  1506. *****************************************************************************}
  1507. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  1508. {$i dynlib.inc}
  1509. {$ifdef DISABLE_NO_DYNLIBS_MANAGER}
  1510. { OS Dependant implementation }
  1511. {$i sysdl.inc}
  1512. {$endif DISABLE_NO_DYNLIBS_MANAGER}
  1513. {$endif FPC_HAS_FEATURE_DYNLIBS}
  1514. {*****************************************************************************
  1515. File Handling
  1516. *****************************************************************************}
  1517. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1518. { Allow slash and backslash as separators }
  1519. procedure DoDirSeparators(var p: pansichar; inplace: boolean = true);
  1520. var
  1521. i : ObjpasInt;
  1522. len : sizeint;
  1523. newp : pansichar;
  1524. begin
  1525. len:=length(p);
  1526. newp:=nil;
  1527. for i:=0 to len do
  1528. if p[i] in AllowDirectorySeparators then
  1529. begin
  1530. if not inplace and
  1531. not assigned(newp) then
  1532. begin
  1533. getmem(newp,len+1);
  1534. move(p^,newp^,len+1);
  1535. p:=newp;
  1536. end;
  1537. p[i]:=DirectorySeparator;
  1538. end;
  1539. end;
  1540. procedure DoDirSeparators(var p: pwidechar; inplace: boolean = true);
  1541. var
  1542. i : ObjpasInt;
  1543. len : sizeint;
  1544. newp : pwidechar;
  1545. begin
  1546. len:=length(p);
  1547. newp:=nil;
  1548. for i:=0 to len do
  1549. if (ord(p[i])<255) and
  1550. (ansichar(ord(p[i])) in AllowDirectorySeparators) then
  1551. begin
  1552. if not inplace and
  1553. not assigned(newp) then
  1554. begin
  1555. getmem(newp,(len+1)*2);
  1556. move(p^,newp^,(len+1)*2);
  1557. p:=newp;
  1558. end;
  1559. p[i]:=DirectorySeparator;
  1560. end;
  1561. end;
  1562. procedure DoDirSeparators(var p:shortstring);
  1563. var
  1564. i : ObjpasInt;
  1565. begin
  1566. for i:=1 to length(p) do
  1567. if p[i] in AllowDirectorySeparators then
  1568. p[i]:=DirectorySeparator;
  1569. end;
  1570. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1571. procedure DoDirSeparators(var ps:RawByteString);
  1572. var
  1573. i : ObjpasInt;
  1574. p : pansichar;
  1575. unique : boolean;
  1576. begin
  1577. unique:=false;
  1578. for i:=1 to length(ps) do
  1579. if ps[i] in AllowDirectorySeparators then
  1580. begin
  1581. if not unique then
  1582. begin
  1583. uniquestring(ps);
  1584. p:=pansichar(ps);
  1585. unique:=true;
  1586. end;
  1587. p[i-1]:=DirectorySeparator;
  1588. end;
  1589. end;
  1590. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1591. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  1592. procedure DoDirSeparators(var ps:UnicodeString);
  1593. var
  1594. i : ObjpasInt;
  1595. p : pwidechar;
  1596. unique : boolean;
  1597. begin
  1598. unique:=false;
  1599. for i:=1 to length(ps) do
  1600. if ps[i] in AllowDirectorySeparators then
  1601. begin
  1602. if not unique then
  1603. begin
  1604. uniquestring(ps);
  1605. p:=pwidechar(ps);
  1606. unique:=true;
  1607. end;
  1608. p[i-1]:=DirectorySeparator;
  1609. end;
  1610. end;
  1611. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  1612. {$endif FPC_HAS_FEATURE_FILEIO}
  1613. { OS dependent low level file functions }
  1614. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1615. {$i sysfile.inc}
  1616. {$ifndef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1617. {$ifdef FPC_ANSI_TEXTFILEREC}
  1618. procedure do_open(var f; p: pansichar; flags: longint; pchangeable: boolean);
  1619. var
  1620. u: UnicodeString;
  1621. begin
  1622. widestringmanager.Ansi2UnicodeMoveProc(p,DefaultFileSystemCodePage,u,length(p));
  1623. do_open(f,pwidechar(u),flags,true);
  1624. end;
  1625. procedure do_erase(p: pansichar; pchangeable: boolean);
  1626. var
  1627. u: UnicodeString;
  1628. begin
  1629. widestringmanager.Ansi2UnicodeMoveProc(p,DefaultFileSystemCodePage,u,length(p));
  1630. do_erase(pwidechar(u),true);
  1631. end;
  1632. procedure do_rename(src, dst: pansichar; srcchangeable, dstchangeable: boolean);
  1633. var
  1634. usrc, udst: UnicodeString;
  1635. begin
  1636. widestringmanager.Ansi2UnicodeMoveProc(src,DefaultFileSystemCodePage,usrc,length(src));
  1637. widestringmanager.Ansi2UnicodeMoveProc(dst,DefaultFileSystemCodePage,udst,length(dst));
  1638. do_rename(pwidechar(usrc),pwidechar(udst),true,true);
  1639. end;
  1640. procedure do_rename(src: pansichar; dst: pwidechar; srcchangeable, dstchangeable: boolean);
  1641. var
  1642. usrc: UnicodeString;
  1643. begin
  1644. widestringmanager.Ansi2UnicodeMoveProc(src,DefaultFileSystemCodePage,usrc,length(src));
  1645. do_rename(pwidechar(usrc),dst,true,dstchangeable);
  1646. end;
  1647. {$endif FPC_ANSI_TEXTFILEREC}
  1648. {$endif not FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1649. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1650. {$ifndef FPC_ANSI_TEXTFILEREC}
  1651. procedure do_open(var f; p: pwidechar; flags: longint; pchangeable: boolean);
  1652. var
  1653. s: RawByteString;
  1654. begin
  1655. widestringmanager.Unicode2AnsiMoveProc(p,s,DefaultFileSystemCodePage,length(p));
  1656. do_open(f,pansichar(s),flags,true);
  1657. end;
  1658. procedure do_erase(p: pwidechar; pchangeable: boolean);
  1659. var
  1660. s: RawByteString;
  1661. begin
  1662. widestringmanager.Unicode2AnsiMoveProc(p,s,DefaultFileSystemCodePage,length(p));
  1663. do_erase(pansichar(s),true);
  1664. end;
  1665. procedure do_rename(src, dst: pwidechar; srcchangeable, dstchangeable: boolean);
  1666. var
  1667. rsrc, rdst: RawByteString;
  1668. begin
  1669. widestringmanager.Unicode2AnsiMoveProc(src,rsrc,DefaultFileSystemCodePage,length(src));
  1670. widestringmanager.Unicode2AnsiMoveProc(dst,rdst,DefaultFileSystemCodePage,length(dst));
  1671. do_rename(pansichar(rsrc),pansichar(rdst),true,true);
  1672. end;
  1673. procedure do_rename(src: pwidechar; dst: pansichar; srcchangeable, dstchangeable: boolean);
  1674. var
  1675. rsrc: RawByteString;
  1676. begin
  1677. widestringmanager.Unicode2AnsiMoveProc(src,rsrc,DefaultFileSystemCodePage,length(src));
  1678. do_rename(pansichar(rsrc),dst,true,dstchangeable);
  1679. end;
  1680. {$endif not FPC_ANSI_TEXTFILEREC}
  1681. {$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1682. {$endif FPC_HAS_FEATURE_FILEIO}
  1683. { helper for targets supporting no ansistrings, it is used
  1684. by non-ansistring code }
  1685. function min(v1,v2 : SizeInt) : SizeInt;
  1686. begin
  1687. if v1<v2 then
  1688. result:=v1
  1689. else
  1690. result:=v2;
  1691. end;
  1692. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  1693. { Text file }
  1694. {$i text.inc}
  1695. {$endif FPC_HAS_FEATURE_TEXTIO}
  1696. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1697. { Untyped file }
  1698. {$i file.inc}
  1699. { Typed file }
  1700. {$i typefile.inc}
  1701. {$endif FPC_HAS_FEATURE_FILEIO}
  1702. {*****************************************************************************
  1703. Directory Handling
  1704. *****************************************************************************}
  1705. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1706. { OS dependent dir functions }
  1707. {$i sysdir.inc}
  1708. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1709. {$ifndef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1710. procedure do_getdir(drivenr : byte;var dir : rawbytestring);
  1711. var
  1712. u: unicodestring;
  1713. begin
  1714. Do_getdir(drivenr,u);
  1715. widestringmanager.Unicode2AnsiMoveProc(pwidechar(u),dir,DefaultRTLFileSystemCodePage,length(u));
  1716. end;
  1717. {$endif FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1718. Procedure MkDir(Const s: RawByteString);[IOCheck];
  1719. Begin
  1720. If (Length(s)=0) or (InOutRes <> 0) then
  1721. exit;
  1722. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1723. Do_mkdir(ToSingleByteFileSystemEncodedFileName(S));
  1724. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1725. Do_mkdir(S);
  1726. {$endif}
  1727. end;
  1728. Procedure RmDir(Const s: RawByteString);[IOCheck];
  1729. Begin
  1730. If (Length(s)=0) or (InOutRes <> 0) then
  1731. exit;
  1732. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1733. Do_rmdir(ToSingleByteFileSystemEncodedFileName(S));
  1734. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1735. Do_rmdir(S);
  1736. {$endif}
  1737. End;
  1738. Procedure ChDir(Const s: RawByteString);[IOCheck];
  1739. Begin
  1740. If (Length(s)=0) or (InOutRes <> 0) then
  1741. exit;
  1742. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1743. Do_chdir(ToSingleByteFileSystemEncodedFileName(S));
  1744. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1745. Do_chdir(S);
  1746. {$endif}
  1747. End;
  1748. Procedure getdir(drivenr:byte;Var dir:rawbytestring);
  1749. begin
  1750. Do_getdir(drivenr,dir);
  1751. { we should return results in the DefaultRTLFileSystemCodePage -> convert if
  1752. necessary }
  1753. setcodepage(dir,DefaultRTLFileSystemCodePage,true);
  1754. end;
  1755. { the generic shortstring ones are only implemented elsewhere for systems *not*
  1756. supporting ansi/unicodestrings; for now assume there are no systems that
  1757. support unicodestrings but not ansistrings }
  1758. { avoid double string conversions }
  1759. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1760. function GetDirStrFromShortstring(const s: shortstring): RawByteString;
  1761. begin
  1762. GetDirStrFromShortstring:=ToSingleByteFileSystemEncodedFileName(ansistring(s));
  1763. end;
  1764. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1765. function GetDirStrFromShortstring(const s: shortstring): UnicodeString;
  1766. begin
  1767. GetDirStrFromShortstring:=s;
  1768. end;
  1769. {$endif FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1770. Procedure MkDir(Const s: shortstring);[IOCheck];
  1771. Begin
  1772. If (Length(s)=0) or (InOutRes <> 0) then
  1773. exit;
  1774. Do_mkdir(GetDirStrFromShortstring(S));
  1775. End;
  1776. Procedure RmDir(Const s: shortstring);[IOCheck];
  1777. Begin
  1778. If (Length(s)=0) or (InOutRes <> 0) then
  1779. exit;
  1780. Do_rmdir(GetDirStrFromShortstring(S));
  1781. End;
  1782. Procedure ChDir(Const s: shortstring);[IOCheck];
  1783. Begin
  1784. If (Length(S)=0) or (InOutRes <> 0) then
  1785. exit;
  1786. Do_chdir(GetDirStrFromShortstring(S));
  1787. End;
  1788. Procedure getdir(drivenr:byte;Var dir:shortstring);
  1789. var
  1790. s: rawbytestring;
  1791. begin
  1792. Do_getdir(drivenr,s);
  1793. if length(s)<=high(dir) then
  1794. dir:=s
  1795. else
  1796. inoutres:=3;
  1797. end;
  1798. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1799. {$if defined(FPC_HAS_FEATURE_WIDESTRINGS)}
  1800. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1801. { overloads required for mkdir/rmdir/chdir to ensure that the string is
  1802. converted to the right code page }
  1803. procedure do_mkdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1804. begin
  1805. do_mkdir(ToSingleByteFileSystemEncodedFileName(s));
  1806. end;
  1807. procedure do_rmdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1808. begin
  1809. do_rmdir(ToSingleByteFileSystemEncodedFileName(s));
  1810. end;
  1811. procedure do_chdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1812. begin
  1813. do_chdir(ToSingleByteFileSystemEncodedFileName(s));
  1814. end;
  1815. procedure do_getdir(drivenr : byte;var dir : unicodestring);
  1816. var
  1817. s: rawbytestring;
  1818. begin
  1819. Do_getdir(drivenr,s);
  1820. dir:=unicodestring(s);
  1821. end;
  1822. {$endif FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1823. Procedure MkDir(Const s: UnicodeString);[IOCheck];
  1824. Begin
  1825. if (Length(s)=0) or (InOutRes <> 0) then
  1826. exit;
  1827. Do_mkdir(S);
  1828. End;
  1829. Procedure RmDir(Const s: UnicodeString);[IOCheck];
  1830. Begin
  1831. if (Length(s)=0) or (InOutRes <> 0) then
  1832. exit;
  1833. Do_rmdir(S);
  1834. End;
  1835. Procedure ChDir(Const s: UnicodeString);[IOCheck];
  1836. Begin
  1837. if (Length(s)=0) or (InOutRes <> 0) then
  1838. exit;
  1839. Do_chdir(S);
  1840. End;
  1841. Procedure getdir(drivenr:byte;Var dir:unicodestring);
  1842. begin
  1843. Do_getdir(drivenr,dir);
  1844. end;
  1845. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1846. {$endif FPC_HAS_FEATURE_FILEIO}
  1847. {*****************************************************************************
  1848. Resources support
  1849. *****************************************************************************}
  1850. {$i sysres.inc}
  1851. const
  1852. CtrlBreakHandler: TCtrlBreakHandler = nil;
  1853. {$IFNDEF FPC_HAS_SETCTRLBREAKHANDLER}
  1854. (* It is possible to provide platform specific implementation performing *)
  1855. (* special initialization; default implementation just sets the procedural *)
  1856. (* variable to make it available for use from the exception handler. *)
  1857. function SysSetCtrlBreakHandler (Handler: TCtrlBreakHandler): TCtrlBreakHandler;
  1858. begin
  1859. (* Return either nil or previous handler *)
  1860. SysSetCtrlBreakHandler := CtrlBreakHandler;
  1861. CtrlBreakHandler := Handler;
  1862. end;
  1863. {$ENDIF FPC_HAS_SETCTRLBREAKHANDLER}
  1864. {$ifdef cpu16}
  1865. function AtomicIncrement (var Target: smallint) : smallint; {$ifdef SYSTEMINLINE}inline;{$endif}
  1866. begin
  1867. Result:=InterlockedIncrement(Target);
  1868. end;
  1869. function AtomicDecrement (var Target: smallint) : smallint; {$ifdef SYSTEMINLINE}inline;{$endif}
  1870. begin
  1871. Result:=InterlockedDecrement(Target);
  1872. end;
  1873. function AtomicCmpExchange(var Target: smallint; NewValue, Comperand: smallint): smallint; {$ifdef SYSTEMINLINE}inline;{$endif}
  1874. begin
  1875. Result:=InterlockedCompareExchange(Target,NewValue,Comperand);
  1876. end;
  1877. function AtomicExchange (var Target: smallint;Source : smallint) : smallint; {$ifdef SYSTEMINLINE}inline;{$endif}
  1878. begin
  1879. Result:=InterlockedExchange(Target,Source);
  1880. end;
  1881. function AtomicIncrement (var Target: word) : word; {$ifdef SYSTEMINLINE}inline;{$endif}
  1882. begin
  1883. Result:=InterlockedIncrement(Target);
  1884. end;
  1885. function AtomicDecrement (var Target: word) : word; {$ifdef SYSTEMINLINE}inline;{$endif}
  1886. begin
  1887. Result:=InterlockedDecrement(Target);
  1888. end;
  1889. function AtomicCmpExchange(var Target: word; NewValue, Comperand: word): word; {$ifdef SYSTEMINLINE}inline;{$endif}
  1890. begin
  1891. Result:=InterlockedCompareExchange(Target, NewValue, Comperand);
  1892. end;
  1893. function AtomicExchange (var Target: word;Source : word) : word; {$ifdef SYSTEMINLINE}inline;{$endif}
  1894. begin
  1895. Result:=InterlockedExchange(Target,Source);
  1896. end;
  1897. {$endif cpu16}
  1898. function AtomicIncrement (var Target: longint) : longint; {$ifdef SYSTEMINLINE}inline;{$endif}
  1899. begin
  1900. Result:=InterlockedIncrement(Target);
  1901. end;
  1902. function AtomicDecrement (var Target: longint) : longint; {$ifdef SYSTEMINLINE}inline;{$endif}
  1903. begin
  1904. Result:=InterlockedDecrement(Target);
  1905. end;
  1906. function AtomicCmpExchange(var Target: longint; NewValue, Comperand: longint): longint; {$ifdef SYSTEMINLINE}inline;{$endif}
  1907. begin
  1908. Result:=InterlockedCompareExchange(Target,NewValue, Comperand);
  1909. end;
  1910. function AtomicExchange (var Target: longint;Source : longint) : longint; {$ifdef SYSTEMINLINE}inline;{$endif}
  1911. begin
  1912. Result:=InterlockedExchange(Target,Source);
  1913. end;
  1914. {$ifdef cpu64}
  1915. function AtomicIncrement (var Target: int64) : int64; {$ifdef SYSTEMINLINE}inline;{$endif}
  1916. begin
  1917. Result:=InterlockedIncrement64(Target);
  1918. end;
  1919. function AtomicDecrement (var Target: int64) : int64; {$ifdef SYSTEMINLINE}inline;{$endif}
  1920. begin
  1921. Result:=InterlockedDecrement64(Target);
  1922. end;
  1923. function AtomicCmpExchange(var Target: int64; NewValue, Comperand: int64): int64; {$ifdef SYSTEMINLINE}inline;{$endif}
  1924. begin
  1925. Result:=InterlockedCompareExchange64(Target,NewValue, Comperand);
  1926. end;
  1927. function AtomicExchange (var Target: int64;Source : int64) : int64; {$ifdef SYSTEMINLINE}inline;{$endif}
  1928. begin
  1929. Result:=InterlockedExchange64(Target,Source);
  1930. end;
  1931. {$endif cpu64}
  1932. { Pointer overloads }
  1933. {$ifndef FPC_SYSTEM_DISABLE_INTERLOCK_POINTER_OVERLOAD}
  1934. function AtomicIncrement (var Target: pointer) : pointer; {$ifdef SYSTEMINLINE}inline;{$endif}
  1935. begin
  1936. {$IFDEF CPU64}
  1937. Result:=Pointer(InterlockedIncrement64(int64(Target)));
  1938. {$ELSE}
  1939. {$IFDEF CPU16}
  1940. Result:=Pointer(InterlockedIncrement(smallint(Target)));
  1941. {$ELSE}
  1942. Result:=Pointer(InterlockedIncrement(Longint(Target)));
  1943. {$ENDIF}
  1944. {$ENDIF}
  1945. end;
  1946. function AtomicDecrement (var Target: pointer) : pointer; {$ifdef SYSTEMINLINE}inline;{$endif}
  1947. begin
  1948. {$IFDEF CPU64}
  1949. Result:=Pointer(InterlockedDecrement64(Int64(Target)));
  1950. {$ELSE}
  1951. {$IFDEF CPU16}
  1952. Result:=Pointer(InterlockedDecrement(smallint(Target)));
  1953. {$ELSE}
  1954. Result:=Pointer(InterlockedDecrement(Longint(Target)));
  1955. {$ENDIF}
  1956. {$ENDIF}
  1957. end;
  1958. function AtomicCmpExchange(var Target: pointer; NewValue, Comperand: pointer): pointer; {$ifdef SYSTEMINLINE}inline;{$endif}
  1959. begin
  1960. {$IFDEF CPU64}
  1961. Result:=Pointer(InterlockedCompareExchange64(Int64(Target),Int64(NewValue), Int64(Comperand)));
  1962. {$ELSE}
  1963. {$IFDEF CPU16}
  1964. Result:=Pointer(InterlockedCompareExchange(smallint(Target),smallint(NewValue),smallint(Comperand)));
  1965. {$ELSE}
  1966. Result:=Pointer(InterlockedCompareExchange(LongInt(Target),LongInt(NewValue), LongInt(Comperand)));
  1967. {$ENDIF}
  1968. {$ENDIF}
  1969. end;
  1970. function AtomicExchange(var Target: pointer;Source : pointer) : pointer; {$ifdef SYSTEMINLINE}inline;{$endif}
  1971. begin
  1972. {$IFDEF CPU64}
  1973. Result:=Pointer(InterlockedExchange64(Int64(Target),Int64(Source)));
  1974. {$ELSE}
  1975. {$IFDEF CPU16}
  1976. Result:=Pointer(InterlockedExchange(smallint(Target),smallint(Source)));
  1977. {$ELSE}
  1978. Result:=Pointer(InterlockedExchange(LongInt(Target),LongInt(Source)));
  1979. {$ENDIF}
  1980. {$ENDIF}
  1981. end;
  1982. {$endif FPC_SYSTEM_DISABLE_INTERLOCK_POINTER_OVERLOAD}
  1983. function AtomicIncrement (var Target: Cardinal) : Cardinal; {$ifdef SYSTEMINLINE}inline;{$endif}
  1984. begin
  1985. Result:=Cardinal(InterlockedIncrement(Longint(Target)));
  1986. end;
  1987. function AtomicDecrement (var Target: Cardinal) : Cardinal; {$ifdef SYSTEMINLINE}inline;{$endif}
  1988. begin
  1989. Result:=Cardinal(InterlockedIncrement(Longint(Target)));
  1990. end;
  1991. function AtomicCmpExchange(var Target: Cardinal; NewValue, Comperand: Cardinal): Cardinal; {$ifdef SYSTEMINLINE}inline;{$endif}
  1992. begin
  1993. Result:=Cardinal(InterlockedCompareExchange(Longint(Target),Longint(NewValue), Longint(Comperand)));
  1994. end;
  1995. function AtomicExchange (var Target: Cardinal;Source : Cardinal) : Cardinal; {$ifdef SYSTEMINLINE}inline;{$endif}
  1996. begin
  1997. Result:=Cardinal(InterlockedExchange(Longint(Target),Longint(Source)));
  1998. end;
  1999. {$ifdef cpu64}
  2000. function AtomicIncrement (var Target: qword) : qword; {$ifdef SYSTEMINLINE}inline;{$endif}
  2001. begin
  2002. Result:=QWord(InterlockedIncrement64(Int64(Target)));
  2003. end;
  2004. function AtomicDecrement (var Target: qword) : qword; {$ifdef SYSTEMINLINE}inline;{$endif}
  2005. begin
  2006. Result:=QWord(InterlockedDecrement64(int64(Target)));
  2007. end;
  2008. function AtomicCmpExchange(var Target: qword; NewValue, Comperand: qword): qword; {$ifdef SYSTEMINLINE}inline;{$endif}
  2009. begin
  2010. Result:=QWord(InterlockedCompareExchange64(Int64(Target),Int64(NewValue), Int64(Comperand)));
  2011. end;
  2012. function AtomicExchange (var Target: qword;Source : qword) : qword; {$ifdef SYSTEMINLINE}inline;{$endif}
  2013. begin
  2014. Result:=QWord(InterlockedExchange64(Int64(Target),Int64(Source)));
  2015. end;
  2016. {$endif}