system.inc 55 KB

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