system.inc 52 KB

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