system.pp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2005 by Florian Klaempfl and Pavel Ozerski
  4. and Yury Sidorov member of the Free Pascal development team.
  5. FPC Pascal system unit for the WinCE.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit System;
  13. interface
  14. {$ifdef SYSTEMDEBUG}
  15. {$define SYSTEMEXCEPTIONDEBUG}
  16. {$endif SYSTEMDEBUG}
  17. {$define WINCE_EXCEPTION_HANDLING}
  18. {$define DISABLE_NO_THREAD_MANAGER}
  19. {$define HAS_CMDLINE}
  20. {$define HAS_MEMORYMANAGER} // comment this line to switch from wincemm to fpcmm
  21. {$define HAS_WIDESTRINGMANAGER}
  22. { include system-independent routine headers }
  23. {$I systemh.inc}
  24. const
  25. LineEnding = #13#10;
  26. LFNSupport = true;
  27. DirectorySeparator = '\';
  28. DriveSeparator = ':';
  29. ExtensionSeparator = '.';
  30. PathSeparator = ';';
  31. AllowDirectorySeparators : set of char = ['\','/'];
  32. AllowDriveSeparators : set of char = [':'];
  33. { FileNameCaseSensitive is defined separately below!!! }
  34. maxExitCode = 65535;
  35. MaxPathLen = 260;
  36. AllFilesMask = '*';
  37. const
  38. { Default filehandles }
  39. UnusedHandle : THandle = THandle(-1);
  40. StdInputHandle : THandle = 0;
  41. StdOutputHandle : THandle = 0;
  42. StdErrorHandle : THandle = 0;
  43. FileNameCaseSensitive : boolean = true;
  44. CtrlZMarksEOF: boolean = true; (* #26 is considered as end of file *)
  45. sLineBreak = LineEnding;
  46. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  47. var
  48. { WinCE Info }
  49. hprevinst,
  50. MainInstance,
  51. DLLreason,DLLparam:DWord;
  52. type
  53. TDLL_Entry_Hook = procedure (dllparam : longint);
  54. const
  55. Dll_Process_Detach_Hook : TDLL_Entry_Hook = nil;
  56. Dll_Thread_Attach_Hook : TDLL_Entry_Hook = nil;
  57. Dll_Thread_Detach_Hook : TDLL_Entry_Hook = nil;
  58. { ANSI <-> Wide }
  59. function AnsiToWideBuf(AnsiBuf: PChar; AnsiBufLen: longint; WideBuf: PWideChar; WideBufLen: longint): longint;
  60. function WideToAnsiBuf(WideBuf: PWideChar; WideCharsLen: longint; AnsiBuf: PChar; AnsiBufLen: longint): longint;
  61. function PCharToPWideChar(str: PChar; strlen: longint = -1; outlen: PLongInt = nil): PWideChar;
  62. function StringToPWideChar(const s: AnsiString; outlen: PLongInt = nil): PWideChar;
  63. { Wrappers for some WinAPI calls }
  64. function CreateEvent(lpEventAttributes:pointer;bManualReset:longbool;bInitialState:longbool;lpName:pchar): THandle;
  65. function ResetEvent(h: THandle): LONGBOOL;
  66. function SetEvent(h: THandle): LONGBOOL;
  67. function GetCurrentProcessId:DWORD;
  68. function Win32GetCurrentThreadId:DWORD;
  69. function TlsAlloc : DWord;
  70. function TlsFree(dwTlsIndex : DWord) : LongBool;
  71. function GetFileAttributes(p : pchar) : dword;
  72. function DeleteFile(p : pchar) : longint;
  73. function MoveFile(old,_new : pchar) : longint;
  74. function CreateFile(lpFileName:pchar; dwDesiredAccess:DWORD; dwShareMode:DWORD;
  75. lpSecurityAttributes:pointer; dwCreationDisposition:DWORD;
  76. dwFlagsAndAttributes:DWORD; hTemplateFile:DWORD):longint;
  77. function CreateDirectory(name : pointer;sec : pointer) : longbool;
  78. function RemoveDirectory(name:pointer):longbool;
  79. {$ifdef CPUARM}
  80. { the external directive isn't really necessary here because it is overridden by external (FK) }
  81. function addd(d1,d2 : double) : double; compilerproc;
  82. cdecl;external 'coredll' name '__addd';
  83. function subd(d1,d2 : double) : double; compilerproc;
  84. cdecl;external 'coredll' name '__subd';
  85. function muld(d1,d2 : double) : double; compilerproc;
  86. cdecl;external 'coredll' name '__muld';
  87. function divd(d1,d2 : double) : double; compilerproc;
  88. cdecl;external 'coredll' name '__divd';
  89. function eqd(d1,d2 : double) : boolean; compilerproc;
  90. cdecl;external 'coredll' name '__eqd';
  91. function ned(d1,d2 : double) : boolean; compilerproc;
  92. cdecl;external 'coredll' name '__ned';
  93. function ltd(d1,d2 : double) : boolean; compilerproc;
  94. cdecl;external 'coredll' name '__ltd';
  95. function gtd(d1,d2 : double) : boolean; compilerproc;
  96. cdecl;external 'coredll' name '__gtd';
  97. function ged(d1,d2 : double) : boolean; compilerproc;
  98. cdecl;external 'coredll' name '__ged';
  99. function led(d1,d2 : double) : boolean; compilerproc;
  100. cdecl;external 'coredll' name '__led';
  101. { ***************** single ******************** }
  102. function eqs(d1,d2 : single) : boolean; compilerproc;
  103. cdecl;external 'coredll' name '__eqs';
  104. function nes(d1,d2 : single) : boolean; compilerproc;
  105. cdecl;external 'coredll' name '__nes';
  106. function lts(d1,d2 : single) : boolean; compilerproc;
  107. cdecl;external 'coredll' name '__lts';
  108. function gts(d1,d2 : single) : boolean; compilerproc;
  109. cdecl;external 'coredll' name '__gts';
  110. function ges(d1,d2 : single) : boolean; compilerproc;
  111. cdecl;external 'coredll' name '__ges';
  112. function les(d1,d2 : single) : boolean; compilerproc;
  113. cdecl;external 'coredll' name '__les';
  114. function dtos(d : double) : single; compilerproc;
  115. cdecl;external 'coredll' name '__dtos';
  116. function stod(d : single) : double; compilerproc;
  117. cdecl;external 'coredll' name '__stod';
  118. function negs(d : single) : single; compilerproc;
  119. cdecl;external 'coredll' name '__negs';
  120. function negd(d : double) : double; compilerproc;
  121. cdecl;external 'coredll' name '__negd';
  122. function utod(i : dword) : double; compilerproc;
  123. cdecl;external 'coredll' name '__utod';
  124. function itod(i : longint) : double; compilerproc;
  125. cdecl;external 'coredll' name '__itod';
  126. function ui64tod(i : qword) : double; compilerproc;
  127. cdecl;external 'coredll' name '__u64tod';
  128. function i64tod(i : int64) : double; compilerproc;
  129. cdecl;external 'coredll' name '__i64tod';
  130. function utos(i : dword) : single; compilerproc;
  131. cdecl;external 'coredll' name '__utos';
  132. function itos(i : longint) : single; compilerproc;
  133. cdecl;external 'coredll' name '__itos';
  134. function ui64tos(i : qword) : single; compilerproc;
  135. cdecl;external 'coredll' name '__u64tos';
  136. function i64tos(i : int64) : single; compilerproc;
  137. cdecl;external 'coredll' name '__i64tos';
  138. function adds(s1,s2 : single) : single; compilerproc;
  139. function subs(s1,s2 : single) : single; compilerproc;
  140. function muls(s1,s2 : single) : single; compilerproc;
  141. function divs(s1,s2 : single) : single; compilerproc;
  142. {$endif CPUARM}
  143. function CmdLine: PChar;
  144. { C compatible arguments }
  145. function argc: longint;
  146. function argv: ppchar;
  147. implementation
  148. var
  149. SysInstance : Longint;public name '_FPC_SysInstance';
  150. function MessageBox(w1:longint;l1,l2:PWideChar;w2:longint):longint;
  151. cdecl; external 'coredll' name 'MessageBoxW';
  152. {*****************************************************************************}
  153. {$define FPC_SYSTEM_HAS_MOVE}
  154. procedure memmove(dest, src: pointer; count: longint);
  155. cdecl; external 'coredll' name 'memmove';
  156. procedure Move(const source;var dest;count:SizeInt);[public, alias: 'FPC_MOVE'];
  157. begin
  158. if count > 0 then
  159. memmove(@dest, @source, count);
  160. end;
  161. {$define FPC_SYSTEM_HAS_COMPAREBYTE}
  162. function memcmp(buf1, buf2: pointer; count: longint): longint;
  163. cdecl; external 'coredll' name 'memcmp';
  164. function CompareByte(Const buf1,buf2;len:SizeInt):SizeInt;
  165. begin
  166. CompareByte := memcmp(@buf1, @buf2, len);
  167. end;
  168. {$ifdef CPUARM}
  169. {$define FPC_SYSTEM_HAS_INT}
  170. function floor(d : double) : double;
  171. cdecl;external 'coredll' name 'floor';
  172. function ceil(d : double) : double;
  173. cdecl;external 'coredll' name 'ceil';
  174. function fpc_int_real(d: ValReal): ValReal;compilerproc;
  175. begin
  176. if d > 0 then
  177. fpc_int_real:=floor(d)
  178. else
  179. fpc_int_real:=ceil(d);
  180. end;
  181. {$define FPC_SYSTEM_HAS_TRUNC}
  182. function __dtoi64(d: double) : int64; cdecl; external 'coredll';
  183. function fpc_trunc_real(d : ValReal) : int64; assembler; nostackframe; compilerproc;
  184. asm
  185. b __dtoi64
  186. end;
  187. {$define FPC_SYSTEM_HAS_ABS}
  188. function fabs(d: double): double; cdecl; external 'coredll';
  189. function fpc_abs_real(d : ValReal) : ValReal; assembler; nostackframe; compilerproc;
  190. asm
  191. b fabs
  192. end;
  193. {$define FPC_SYSTEM_HAS_SQRT}
  194. function coresqrt(d: double): double; cdecl; external 'coredll' name 'sqrt';
  195. function fpc_sqrt_real(d : ValReal) : ValReal; assembler; nostackframe; compilerproc;
  196. asm
  197. b coresqrt
  198. end;
  199. function adds(s1,s2 : single) : single;
  200. begin
  201. adds := addd(s1, s2);
  202. end;
  203. function subs(s1,s2 : single) : single;
  204. begin
  205. subs := subd(s1, s2);
  206. end;
  207. function muls(s1,s2 : single) : single;
  208. begin
  209. muls := muld(s1, s2);
  210. end;
  211. function divs(s1,s2 : single) : single;
  212. begin
  213. divs := divd(s1, s2);
  214. end;
  215. {$endif CPUARM}
  216. {*****************************************************************************}
  217. { include system independent routines }
  218. {$I system.inc}
  219. {*****************************************************************************
  220. ANSI <-> Wide
  221. *****************************************************************************}
  222. const
  223. { MultiByteToWideChar }
  224. MB_PRECOMPOSED = 1;
  225. MB_COMPOSITE = 2;
  226. MB_ERR_INVALID_CHARS = 8;
  227. MB_USEGLYPHCHARS = 4;
  228. CP_OEMCP = 1;
  229. function MultiByteToWideChar(CodePage:UINT; dwFlags:DWORD; lpMultiByteStr:PChar; cchMultiByte:longint; lpWideCharStr:PWideChar;cchWideChar:longint):longint;
  230. cdecl; external 'coredll' name 'MultiByteToWideChar';
  231. function WideCharToMultiByte(CodePage:UINT; dwFlags:DWORD; lpWideCharStr:PWideChar; cchWideChar:longint; lpMultiByteStr:PChar;cchMultiByte:longint; lpDefaultChar:PChar; lpUsedDefaultChar:pointer):longint;
  232. cdecl; external 'coredll' name 'WideCharToMultiByte';
  233. function GetACP:UINT; cdecl; external 'coredll' name 'GetACP';
  234. function GetConsoleCP:UINT; cdecl; external 'coredll' name 'GetConsoleCP';
  235. function GetConsoleOutputCP:UINT; cdecl; external 'coredll' name 'GetConsoleOutputCP';
  236. { Returns number of characters stored to WideBuf, including null-terminator. }
  237. function AnsiToWideBuf(AnsiBuf: PChar; AnsiBufLen: longint; WideBuf: PWideChar; WideBufLen: longint): longint;
  238. begin
  239. Result := MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, AnsiBuf, AnsiBufLen, WideBuf, WideBufLen div SizeOf(WideChar));
  240. if ((AnsiBufLen <> -1) or (Result = 0)) and (WideBuf <> nil) then
  241. begin
  242. if (Result + 1)*SizeOf(WideChar) > WideBufLen then
  243. begin
  244. Result := 0;
  245. if WideBufLen < SizeOf(WideChar) then
  246. exit;
  247. end;
  248. WideBuf[Result] := #0;
  249. end;
  250. if (AnsiBufLen <> -1) and ((Result <> 0) or (AnsiBufLen = 0)) then
  251. Inc(Result);
  252. end;
  253. { Returns number of characters stored to AnsiBuf, including null-terminator. }
  254. function WideToAnsiBuf(WideBuf: PWideChar; WideCharsLen: longint; AnsiBuf: PChar; AnsiBufLen: longint): longint;
  255. begin
  256. Result := WideCharToMultiByte(CP_ACP, 0, WideBuf, WideCharsLen, AnsiBuf, AnsiBufLen, nil, nil);
  257. if ((WideCharsLen <> -1) or (Result = 0)) and (AnsiBuf <> nil) then
  258. begin
  259. if Result + 1 > AnsiBufLen then
  260. begin
  261. Result := 0;
  262. if AnsiBufLen < 1 then
  263. exit;
  264. end;
  265. AnsiBuf[Result] := #0;
  266. end;
  267. if (WideCharsLen <> -1) and ((Result <> 0) or (WideCharsLen = 0)) then
  268. Inc(Result);
  269. end;
  270. { Returns dynamic memory block, which contains wide string. This memory should be freed using FreeMem. }
  271. { outlen will contain number of wide characters stored to result buffer, including null-terminator. }
  272. function PCharToPWideChar(str: PChar; strlen: longint = -1; outlen: PLongInt = nil): PWideChar;
  273. var
  274. len: longint;
  275. begin
  276. while True do begin
  277. if strlen <> -1 then
  278. len:=strlen + 1
  279. else
  280. len:=AnsiToWideBuf(str, -1, nil, 0);
  281. if len > 0 then
  282. begin
  283. len:=len*SizeOf(WideChar);
  284. GetMem(Result, len);
  285. len:=AnsiToWideBuf(str, strlen, Result, len);
  286. if (len = 0) and (strlen <> -1) then
  287. begin
  288. FreeMem(Result);
  289. strlen:=-1;
  290. continue;
  291. end;
  292. end
  293. else begin
  294. GetMem(Result, SizeOf(WideChar));
  295. len:=1;
  296. Result^:=#0;
  297. end;
  298. break;
  299. end;
  300. if outlen <> nil then
  301. outlen^:=len;
  302. end;
  303. { Returns dynamic memory block, which contains wide string. This memory should be freed using FreeMem. }
  304. { outlen will contain number of wide characters stored to result buffer, including null-terminator. }
  305. function StringToPWideChar(const s: AnsiString; outlen: PLongInt = nil): PWideChar;
  306. var
  307. len, wlen: longint;
  308. begin
  309. len:=Length(s);
  310. wlen:=(len + 1)*SizeOf(WideChar);
  311. GetMem(Result, wlen);
  312. wlen:=AnsiToWideBuf(PChar(s), len, Result, wlen)*SizeOf(WideChar);
  313. if wlen = 0 then
  314. begin
  315. wlen:=AnsiToWideBuf(PChar(s), len, nil, 0)*SizeOf(WideChar);
  316. if wlen > 0 then
  317. begin
  318. ReAllocMem(Result, wlen);
  319. wlen:=AnsiToWideBuf(PChar(s), len, Result, wlen)*SizeOf(WideChar);
  320. end
  321. else
  322. begin
  323. Result^:=#0;
  324. wlen:=SizeOf(WideChar);
  325. end;
  326. end;
  327. if outlen <> nil then
  328. outlen^:=wlen div SizeOf(WideChar);
  329. end;
  330. {*****************************************************************************
  331. WinAPI wrappers implementation
  332. *****************************************************************************}
  333. function GetFileAttributesW(p : pwidechar) : dword;
  334. cdecl; external KernelDLL name 'GetFileAttributesW';
  335. function DeleteFileW(p : pwidechar) : longint;
  336. cdecl; external KernelDLL name 'DeleteFileW';
  337. function MoveFileW(old,_new : pwidechar) : longint;
  338. cdecl; external KernelDLL name 'MoveFileW';
  339. function CreateFileW(lpFileName:pwidechar; dwDesiredAccess:DWORD; dwShareMode:DWORD;
  340. lpSecurityAttributes:pointer; dwCreationDisposition:DWORD;
  341. dwFlagsAndAttributes:DWORD; hTemplateFile:DWORD):longint;
  342. cdecl; external KernelDLL name 'CreateFileW';
  343. function CreateDirectoryW(name : pwidechar;sec : pointer) : longbool;
  344. cdecl; external KernelDLL name 'CreateDirectoryW';
  345. function RemoveDirectoryW(name:pwidechar):longbool;
  346. cdecl; external KernelDLL name 'RemoveDirectoryW';
  347. function GetFileAttributes(p : pchar) : dword;
  348. var
  349. buf: array[0..MaxPathLen] of WideChar;
  350. begin
  351. AnsiToWideBuf(p, -1, buf, SizeOf(buf));
  352. GetFileAttributes := GetFileAttributesW(buf);
  353. end;
  354. function DeleteFile(p : pchar) : longint;
  355. var
  356. buf: array[0..MaxPathLen] of WideChar;
  357. begin
  358. AnsiToWideBuf(p, -1, buf, SizeOf(buf));
  359. DeleteFile := DeleteFileW(buf);
  360. end;
  361. function MoveFile(old,_new : pchar) : longint;
  362. var
  363. buf_old, buf_new: array[0..MaxPathLen] of WideChar;
  364. begin
  365. AnsiToWideBuf(old, -1, buf_old, SizeOf(buf_old));
  366. AnsiToWideBuf(_new, -1, buf_new, SizeOf(buf_new));
  367. MoveFile := MoveFileW(buf_old, buf_new);
  368. end;
  369. function CreateFile(lpFileName:pchar; dwDesiredAccess:DWORD; dwShareMode:DWORD;
  370. lpSecurityAttributes:pointer; dwCreationDisposition:DWORD;
  371. dwFlagsAndAttributes:DWORD; hTemplateFile:DWORD):longint;
  372. var
  373. buf: array[0..MaxPathLen] of WideChar;
  374. begin
  375. AnsiToWideBuf(lpFileName, -1, buf, SizeOf(buf));
  376. CreateFile := CreateFileW(buf, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
  377. dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
  378. end;
  379. function CreateDirectory(name : pointer;sec : pointer) : longbool;
  380. var
  381. buf: array[0..MaxPathLen] of WideChar;
  382. begin
  383. AnsiToWideBuf(name, -1, buf, SizeOf(buf));
  384. CreateDirectory := CreateDirectoryW(buf, sec);
  385. end;
  386. function RemoveDirectory(name:pointer):longbool;
  387. var
  388. buf: array[0..MaxPathLen] of WideChar;
  389. begin
  390. AnsiToWideBuf(name, -1, buf, SizeOf(buf));
  391. RemoveDirectory := RemoveDirectoryW(buf);
  392. end;
  393. const
  394. {$ifdef CPUARM}
  395. UserKData = $FFFFC800;
  396. {$else CPUARM}
  397. UserKData = $00005800;
  398. {$endif CPUARM}
  399. SYSHANDLE_OFFSET = $004;
  400. SYS_HANDLE_BASE = 64;
  401. SH_CURTHREAD = 1;
  402. SH_CURPROC = 2;
  403. type
  404. PHandle = ^THandle;
  405. const
  406. EVENT_PULSE = 1;
  407. EVENT_RESET = 2;
  408. EVENT_SET = 3;
  409. function CreateEventW(lpEventAttributes:pointer;bManualReset:longbool;bInitialState:longbool;lpName:PWideChar): THandle;
  410. cdecl; external KernelDLL name 'CreateEventW';
  411. function CreateEvent(lpEventAttributes:pointer;bManualReset:longbool;bInitialState:longbool;lpName:pchar): THandle;
  412. var
  413. buf: array[0..MaxPathLen] of WideChar;
  414. begin
  415. if lpName=nil then
  416. CreateEvent := CreateEventW(lpEventAttributes, bManualReset, bInitialState, nil)
  417. else begin
  418. AnsiToWideBuf(lpName, -1, buf, SizeOf(buf));
  419. CreateEvent := CreateEventW(lpEventAttributes, bManualReset, bInitialState, buf);
  420. end;
  421. end;
  422. function EventModify(h: THandle; func: DWORD): LONGBOOL;
  423. cdecl; external KernelDLL name 'EventModify';
  424. function TlsCall(p1, p2: DWORD): DWORD;
  425. cdecl; external KernelDLL name 'TlsCall';
  426. function ResetEvent(h: THandle): LONGBOOL;
  427. begin
  428. ResetEvent := EventModify(h,EVENT_RESET);
  429. end;
  430. function SetEvent(h: THandle): LONGBOOL;
  431. begin
  432. SetEvent := EventModify(h,EVENT_SET);
  433. end;
  434. function GetCurrentProcessId:DWORD;
  435. var
  436. p: PHandle;
  437. begin
  438. p:=PHandle(UserKData+SYSHANDLE_OFFSET + SH_CURPROC*SizeOf(THandle));
  439. GetCurrentProcessId := p^;
  440. end;
  441. function Win32GetCurrentThreadId:DWORD;
  442. var
  443. p: PHandle;
  444. begin
  445. p:=PHandle(UserKData+SYSHANDLE_OFFSET + SH_CURTHREAD*SizeOf(THandle));
  446. Win32GetCurrentThreadId := p^;
  447. end;
  448. const
  449. TLS_FUNCALLOC = 0;
  450. TLS_FUNCFREE = 1;
  451. function TlsAlloc : DWord;
  452. begin
  453. TlsAlloc := TlsCall(TLS_FUNCALLOC, 0);
  454. end;
  455. function TlsFree(dwTlsIndex : DWord) : LongBool;
  456. begin
  457. TlsFree := LongBool(TlsCall(TLS_FUNCFREE, dwTlsIndex));
  458. end;
  459. {*****************************************************************************
  460. Parameter Handling
  461. *****************************************************************************}
  462. function GetCommandLine : pwidechar;
  463. cdecl; external KernelDLL name 'GetCommandLineW';
  464. var
  465. ModuleName : array[0..255] of char;
  466. function GetCommandFile:pchar;
  467. var
  468. buf: array[0..MaxPathLen] of WideChar;
  469. begin
  470. if ModuleName[0] = #0 then begin
  471. GetModuleFileName(0, @buf, SizeOf(buf));
  472. WideToAnsiBuf(buf, -1, @ModuleName, SizeOf(ModuleName));
  473. end;
  474. GetCommandFile:=@ModuleName;
  475. end;
  476. var
  477. Fargc: longint;
  478. Fargv: ppchar;
  479. FCmdLine: PChar;
  480. procedure setup_arguments;
  481. var
  482. arglen,
  483. count : longint;
  484. argstart,
  485. pc,arg : pchar;
  486. quote : char;
  487. argvlen : longint;
  488. procedure allocarg(idx,len:longint);
  489. var
  490. oldargvlen : longint;
  491. begin
  492. if idx>=argvlen then
  493. begin
  494. oldargvlen:=argvlen;
  495. argvlen:=(idx+8) and (not 7);
  496. sysreallocmem(Fargv,argvlen*sizeof(pointer));
  497. fillchar(Fargv[oldargvlen],(argvlen-oldargvlen)*sizeof(pointer),0);
  498. end;
  499. { use realloc to reuse already existing memory }
  500. { always allocate, even if length is zero, since }
  501. { the arg. is still present! }
  502. sysreallocmem(Fargv[idx],len+1);
  503. end;
  504. begin
  505. { create commandline, it starts with the executed filename which is argv[0] }
  506. { WinCE passes the command NOT via the args, but via getmodulefilename}
  507. if FCmdLine <> nil then exit;
  508. argvlen:=0;
  509. pc:=getcommandfile;
  510. Arglen:=0;
  511. while pc[Arglen] <> #0 do
  512. Inc(Arglen);
  513. allocarg(0,arglen);
  514. move(pc^,Fargv[0]^,arglen+1);
  515. { Setup FCmdLine variable }
  516. arg:=PChar(GetCommandLine);
  517. count:=WideToAnsiBuf(PWideChar(arg), -1, nil, 0);
  518. FCmdLine:=SysGetMem(arglen + count + 3);
  519. FCmdLine^:='"';
  520. move(pc^, (FCmdLine + 1)^, arglen);
  521. (FCmdLine + arglen + 1)^:='"';
  522. (FCmdLine + arglen + 2)^:=' ';
  523. WideToAnsiBuf(PWideChar(arg), -1, FCmdLine + arglen + 3, count);
  524. { process arguments }
  525. count:=0;
  526. pc:=FCmdLine;
  527. {$IfDef SYSTEM_DEBUG_STARTUP}
  528. Writeln(stderr,'WinCE GetCommandLine is #',pc,'#');
  529. {$EndIf }
  530. while pc^<>#0 do
  531. begin
  532. { skip leading spaces }
  533. while pc^ in [#1..#32] do
  534. inc(pc);
  535. if pc^=#0 then
  536. break;
  537. { calc argument length }
  538. quote:=' ';
  539. argstart:=pc;
  540. arglen:=0;
  541. while (pc^<>#0) do
  542. begin
  543. case pc^ of
  544. #1..#32 :
  545. begin
  546. if quote<>' ' then
  547. inc(arglen)
  548. else
  549. break;
  550. end;
  551. '"' :
  552. begin
  553. if quote<>'''' then
  554. begin
  555. if pchar(pc+1)^<>'"' then
  556. begin
  557. if quote='"' then
  558. quote:=' '
  559. else
  560. quote:='"';
  561. end
  562. else
  563. inc(pc);
  564. end
  565. else
  566. inc(arglen);
  567. end;
  568. '''' :
  569. begin
  570. if quote<>'"' then
  571. begin
  572. if pchar(pc+1)^<>'''' then
  573. begin
  574. if quote='''' then
  575. quote:=' '
  576. else
  577. quote:='''';
  578. end
  579. else
  580. inc(pc);
  581. end
  582. else
  583. inc(arglen);
  584. end;
  585. else
  586. inc(arglen);
  587. end;
  588. inc(pc);
  589. end;
  590. { copy argument }
  591. { Don't copy the first one, it is already there.}
  592. If Count<>0 then
  593. begin
  594. allocarg(count,arglen);
  595. quote:=' ';
  596. pc:=argstart;
  597. arg:=Fargv[count];
  598. while (pc^<>#0) do
  599. begin
  600. case pc^ of
  601. #1..#32 :
  602. begin
  603. if quote<>' ' then
  604. begin
  605. arg^:=pc^;
  606. inc(arg);
  607. end
  608. else
  609. break;
  610. end;
  611. '"' :
  612. begin
  613. if quote<>'''' then
  614. begin
  615. if pchar(pc+1)^<>'"' then
  616. begin
  617. if quote='"' then
  618. quote:=' '
  619. else
  620. quote:='"';
  621. end
  622. else
  623. inc(pc);
  624. end
  625. else
  626. begin
  627. arg^:=pc^;
  628. inc(arg);
  629. end;
  630. end;
  631. '''' :
  632. begin
  633. if quote<>'"' then
  634. begin
  635. if pchar(pc+1)^<>'''' then
  636. begin
  637. if quote='''' then
  638. quote:=' '
  639. else
  640. quote:='''';
  641. end
  642. else
  643. inc(pc);
  644. end
  645. else
  646. begin
  647. arg^:=pc^;
  648. inc(arg);
  649. end;
  650. end;
  651. else
  652. begin
  653. arg^:=pc^;
  654. inc(arg);
  655. end;
  656. end;
  657. inc(pc);
  658. end;
  659. arg^:=#0;
  660. end;
  661. {$IfDef SYSTEM_DEBUG_STARTUP}
  662. Writeln(stderr,'dos arg ',count,' #',arglen,'#',Fargv[count],'#');
  663. {$EndIf SYSTEM_DEBUG_STARTUP}
  664. inc(count);
  665. end;
  666. { get argc and create an nil entry }
  667. Fargc:=count;
  668. allocarg(argc,0);
  669. { free unused memory }
  670. sysreallocmem(Fargv,(argc+1)*sizeof(pointer));
  671. end;
  672. function CmdLine: PChar;
  673. begin
  674. setup_arguments;
  675. Result:=FCmdLine;
  676. end;
  677. function argc: longint;
  678. begin
  679. setup_arguments;
  680. Result:=Fargc;
  681. end;
  682. function argv: ppchar;
  683. begin
  684. setup_arguments;
  685. Result:=Fargv;
  686. end;
  687. function paramcount : longint;
  688. begin
  689. paramcount := argc - 1;
  690. end;
  691. function paramstr(l : longint) : string;
  692. begin
  693. setup_arguments;
  694. if (l>=0) and (l<Fargc) then
  695. paramstr:=strpas(Fargv[l])
  696. else
  697. paramstr:='';
  698. end;
  699. procedure randomize;
  700. begin
  701. randseed:=GetTickCount;
  702. end;
  703. {*****************************************************************************
  704. System Dependent Exit code
  705. *****************************************************************************}
  706. procedure PascalMain;external name 'PASCALMAIN';
  707. procedure ExitThread(Exitcode : longint); cdecl; external 'coredll';
  708. Procedure system_exit;
  709. begin
  710. if IsLibrary then
  711. exit;
  712. if not IsConsole then
  713. begin
  714. Close(stderr);
  715. Close(stdout);
  716. Close(erroutput);
  717. Close(Input);
  718. Close(Output);
  719. end;
  720. ExitThread(exitcode);
  721. end;
  722. {$ifdef cpu386}
  723. var
  724. { value of the stack segment
  725. to check if the call stack can be written on exceptions }
  726. _SS : Cardinal;
  727. {$endif cpu386}
  728. function Dll_entry : longbool;[public, alias : '_FPC_DLL_Entry'];
  729. begin
  730. IsLibrary:=true;
  731. Dll_entry:=false;
  732. case DLLreason of
  733. DLL_PROCESS_ATTACH :
  734. begin
  735. PASCALMAIN;
  736. Dll_entry:=true;
  737. end;
  738. DLL_THREAD_ATTACH :
  739. begin
  740. { Allocate Threadvars ?!}
  741. if assigned(Dll_Thread_Attach_Hook) then
  742. Dll_Thread_Attach_Hook(DllParam);
  743. end;
  744. DLL_THREAD_DETACH :
  745. begin
  746. if assigned(Dll_Thread_Detach_Hook) then
  747. Dll_Thread_Detach_Hook(DllParam);
  748. { Release Threadvars ?!}
  749. end;
  750. DLL_PROCESS_DETACH :
  751. begin
  752. Lib_Exit;
  753. if assigned(Dll_Process_Detach_Hook) then
  754. Dll_Process_Detach_Hook(DllParam);
  755. end;
  756. end;
  757. end;
  758. {$ifdef WINCE_EXCEPTION_HANDLING}
  759. //
  760. // Hardware exception handling
  761. //
  762. {
  763. Error code definitions for the WinCE API functions
  764. Values are 32 bit values layed out as follows:
  765. 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
  766. 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  767. +---+-+-+-----------------------+-------------------------------+
  768. |Sev|C|R| Facility | Code |
  769. +---+-+-+-----------------------+-------------------------------+
  770. where
  771. Sev - is the severity code
  772. 00 - Success
  773. 01 - Informational
  774. 10 - Warning
  775. 11 - Error
  776. C - is the Customer code flag
  777. R - is a reserved bit
  778. Facility - is the facility code
  779. Code - is the facility's status code
  780. }
  781. const
  782. SEVERITY_SUCCESS = $00000000;
  783. SEVERITY_INFORMATIONAL = $40000000;
  784. SEVERITY_WARNING = $80000000;
  785. SEVERITY_ERROR = $C0000000;
  786. const
  787. STATUS_SEGMENT_NOTIFICATION = $40000005;
  788. DBG_TERMINATE_THREAD = $40010003;
  789. DBG_TERMINATE_PROCESS = $40010004;
  790. DBG_CONTROL_C = $40010005;
  791. DBG_CONTROL_BREAK = $40010008;
  792. STATUS_GUARD_PAGE_VIOLATION = $80000001;
  793. STATUS_DATATYPE_MISALIGNMENT = $80000002;
  794. STATUS_BREAKPOINT = $80000003;
  795. STATUS_SINGLE_STEP = $80000004;
  796. DBG_EXCEPTION_NOT_HANDLED = $80010001;
  797. STATUS_ACCESS_VIOLATION = $C0000005;
  798. STATUS_IN_PAGE_ERROR = $C0000006;
  799. STATUS_INVALID_HANDLE = $C0000008;
  800. STATUS_NO_MEMORY = $C0000017;
  801. STATUS_ILLEGAL_INSTRUCTION = $C000001D;
  802. STATUS_NONCONTINUABLE_EXCEPTION = $C0000025;
  803. STATUS_INVALID_DISPOSITION = $C0000026;
  804. STATUS_ARRAY_BOUNDS_EXCEEDED = $C000008C;
  805. STATUS_FLOAT_DENORMAL_OPERAND = $C000008D;
  806. STATUS_FLOAT_DIVIDE_BY_ZERO = $C000008E;
  807. STATUS_FLOAT_INEXACT_RESULT = $C000008F;
  808. STATUS_FLOAT_INVALID_OPERATION = $C0000090;
  809. STATUS_FLOAT_OVERFLOW = $C0000091;
  810. STATUS_FLOAT_STACK_CHECK = $C0000092;
  811. STATUS_FLOAT_UNDERFLOW = $C0000093;
  812. STATUS_INTEGER_DIVIDE_BY_ZERO = $C0000094;
  813. STATUS_INTEGER_OVERFLOW = $C0000095;
  814. STATUS_PRIVILEGED_INSTRUCTION = $C0000096;
  815. STATUS_STACK_OVERFLOW = $C00000FD;
  816. STATUS_CONTROL_C_EXIT = $C000013A;
  817. STATUS_FLOAT_MULTIPLE_FAULTS = $C00002B4;
  818. STATUS_FLOAT_MULTIPLE_TRAPS = $C00002B5;
  819. STATUS_REG_NAT_CONSUMPTION = $C00002C9;
  820. const
  821. ExceptionContinueExecution = 0;
  822. ExceptionContinueSearch = 1;
  823. ExceptionNestedException = 2;
  824. ExceptionCollidedUnwind = 3;
  825. ExceptionExecuteHandler = 4;
  826. MaxExceptionLevel = 16;
  827. exceptLevel : Byte = 0;
  828. {$ifdef CPUARM}
  829. const
  830. CONTEXT_ARM = $0000040;
  831. CONTEXT_CONTROL = CONTEXT_ARM or $00000001;
  832. CONTEXT_INTEGER = CONTEXT_ARM or $00000002;
  833. CONTEXT_SEGMENTS = CONTEXT_ARM or $00000004;
  834. CONTEXT_FLOATING_POINT = CONTEXT_ARM or $00000008;
  835. CONTEXT_DEBUG_REGISTERS = CONTEXT_ARM or $00000010;
  836. CONTEXT_EXTENDED_REGISTERS = CONTEXT_ARM or $00000020;
  837. CONTEXT_FULL = CONTEXT_CONTROL or CONTEXT_INTEGER or CONTEXT_SEGMENTS;
  838. NUM_VFP_REGS = 32;
  839. NUM_EXTRA_CONTROL_REGS = 8;
  840. type
  841. PContext = ^TContext;
  842. TContext = record
  843. ContextFlags : LongWord;
  844. // This section is specified/returned if the ContextFlags word contains
  845. // the flag CONTEXT_INTEGER.
  846. R0 : LongWord;
  847. R1 : LongWord;
  848. R2 : LongWord;
  849. R3 : LongWord;
  850. R4 : LongWord;
  851. R5 : LongWord;
  852. R6 : LongWord;
  853. R7 : LongWord;
  854. R8 : LongWord;
  855. R9 : LongWord;
  856. R10 : LongWord;
  857. R11 : LongWord;
  858. R12 : LongWord;
  859. // This section is specified/returned if the ContextFlags word contains
  860. // the flag CONTEXT_CONTROL.
  861. Sp : LongWord;
  862. Lr : LongWord;
  863. Pc : LongWord;
  864. Psr : LongWord;
  865. Fpscr : LongWord;
  866. FpExc : LongWord;
  867. // Floating point registers
  868. S : array[0..(NUM_VFP_REGS + 1)-1] of LongWord;
  869. FpExtra : array[0..(NUM_EXTRA_CONTROL_REGS)-1] of LongWord;
  870. end;
  871. {$endif CPUARM}
  872. {$ifdef CPUI386}
  873. const
  874. CONTEXT_X86 = $00010000;
  875. CONTEXT_CONTROL = CONTEXT_X86 or $00000001;
  876. CONTEXT_INTEGER = CONTEXT_X86 or $00000002;
  877. CONTEXT_SEGMENTS = CONTEXT_X86 or $00000004;
  878. CONTEXT_FLOATING_POINT = CONTEXT_X86 or $00000008;
  879. CONTEXT_DEBUG_REGISTERS = CONTEXT_X86 or $00000010;
  880. CONTEXT_EXTENDED_REGISTERS = CONTEXT_X86 or $00000020;
  881. MAXIMUM_SUPPORTED_EXTENSION = 512;
  882. type
  883. PFloatingSaveArea = ^TFloatingSaveArea;
  884. TFloatingSaveArea = packed record
  885. ControlWord : Cardinal;
  886. StatusWord : Cardinal;
  887. TagWord : Cardinal;
  888. ErrorOffset : Cardinal;
  889. ErrorSelector : Cardinal;
  890. DataOffset : Cardinal;
  891. DataSelector : Cardinal;
  892. RegisterArea : array[0..79] of Byte;
  893. Cr0NpxState : Cardinal;
  894. end;
  895. PContext = ^TContext;
  896. TContext = packed record
  897. //
  898. // The flags values within this flag control the contents of
  899. // a CONTEXT record.
  900. //
  901. ContextFlags : Cardinal;
  902. //
  903. // This section is specified/returned if CONTEXT_DEBUG_REGISTERS is
  904. // set in ContextFlags. Note that CONTEXT_DEBUG_REGISTERS is NOT
  905. // included in CONTEXT_FULL.
  906. //
  907. Dr0, Dr1, Dr2,
  908. Dr3, Dr6, Dr7 : Cardinal;
  909. //
  910. // This section is specified/returned if the
  911. // ContextFlags word contains the flag CONTEXT_FLOATING_POINT.
  912. //
  913. FloatSave : TFloatingSaveArea;
  914. //
  915. // This section is specified/returned if the
  916. // ContextFlags word contains the flag CONTEXT_SEGMENTS.
  917. //
  918. SegGs, SegFs,
  919. SegEs, SegDs : Cardinal;
  920. //
  921. // This section is specified/returned if the
  922. // ContextFlags word contains the flag CONTEXT_INTEGER.
  923. //
  924. Edi, Esi, Ebx,
  925. Edx, Ecx, Eax : Cardinal;
  926. //
  927. // This section is specified/returned if the
  928. // ContextFlags word contains the flag CONTEXT_CONTROL.
  929. //
  930. Ebp : Cardinal;
  931. Eip : Cardinal;
  932. SegCs : Cardinal;
  933. EFlags, Esp, SegSs : Cardinal;
  934. //
  935. // This section is specified/returned if the ContextFlags word
  936. // contains the flag CONTEXT_EXTENDED_REGISTERS.
  937. // The format and contexts are processor specific
  938. //
  939. ExtendedRegisters : array[0..MAXIMUM_SUPPORTED_EXTENSION-1] of Byte;
  940. end;
  941. {$endif CPUI386}
  942. type
  943. PExceptionPointers = ^TExceptionPointers;
  944. TExceptionPointers = packed record
  945. ExceptionRecord : PExceptionRecord;
  946. ContextRecord : PContext;
  947. end;
  948. {$ifdef CPUI386}
  949. {**************************** i386 Exception handling *****************************************}
  950. function GetCurrentProcess:DWORD;
  951. begin
  952. GetCurrentProcess := SH_CURPROC+SYS_HANDLE_BASE;
  953. end;
  954. function ReadProcessMemory(process : dword;address : pointer;dest : pointer;size : dword;bytesread : pdword) : longbool;
  955. cdecl; external 'coredll' name 'ReadProcessMemory';
  956. function is_prefetch(p : pointer) : boolean;
  957. var
  958. a : array[0..15] of byte;
  959. doagain : boolean;
  960. instrlo,instrhi,opcode : byte;
  961. i : longint;
  962. begin
  963. result:=false;
  964. { read memory savely without causing another exeception }
  965. if not(ReadProcessMemory(GetCurrentProcess,p,@a,sizeof(a),nil)) then
  966. exit;
  967. i:=0;
  968. doagain:=true;
  969. while doagain and (i<15) do
  970. begin
  971. opcode:=a[i];
  972. instrlo:=opcode and $f;
  973. instrhi:=opcode and $f0;
  974. case instrhi of
  975. { prefix? }
  976. $20,$30:
  977. doagain:=(instrlo and 7)=6;
  978. $60:
  979. doagain:=(instrlo and $c)=4;
  980. $f0:
  981. doagain:=instrlo in [0,2,3];
  982. $0:
  983. begin
  984. result:=(instrlo=$f) and (a[i+1] in [$d,$18]);
  985. exit;
  986. end;
  987. else
  988. doagain:=false;
  989. end;
  990. inc(i);
  991. end;
  992. end;
  993. var
  994. exceptEip : array[0..MaxExceptionLevel-1] of Longint;
  995. exceptError : array[0..MaxExceptionLevel-1] of Byte;
  996. resetFPU : array[0..MaxExceptionLevel-1] of Boolean;
  997. {$ifdef SYSTEMEXCEPTIONDEBUG}
  998. procedure DebugHandleErrorAddrFrame(error, addr, frame : longint);
  999. begin
  1000. if IsConsole then
  1001. begin
  1002. write(stderr,'HandleErrorAddrFrame(error=',error);
  1003. write(stderr,',addr=',hexstr(addr,8));
  1004. writeln(stderr,',frame=',hexstr(frame,8),')');
  1005. end;
  1006. HandleErrorAddrFrame(error,addr,frame);
  1007. end;
  1008. {$endif SYSTEMEXCEPTIONDEBUG}
  1009. procedure JumpToHandleErrorFrame;
  1010. var
  1011. eip, ebp, error : Longint;
  1012. begin
  1013. // save ebp
  1014. asm
  1015. movl (%ebp),%eax
  1016. movl %eax,ebp
  1017. end;
  1018. if (exceptLevel > 0) then
  1019. dec(exceptLevel);
  1020. eip:=exceptEip[exceptLevel];
  1021. error:=exceptError[exceptLevel];
  1022. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1023. if IsConsole then
  1024. writeln(stderr,'In JumpToHandleErrorFrame error=',error);
  1025. {$endif SYSTEMEXCEPTIONDEBUG}
  1026. if resetFPU[exceptLevel] then
  1027. SysResetFPU;
  1028. { build a fake stack }
  1029. asm
  1030. {$ifdef REGCALL}
  1031. movl ebp,%ecx
  1032. movl eip,%edx
  1033. movl error,%eax
  1034. pushl eip
  1035. movl ebp,%ebp // Change frame pointer
  1036. {$else}
  1037. movl ebp,%eax
  1038. pushl %eax
  1039. movl eip,%eax
  1040. pushl %eax
  1041. movl error,%eax
  1042. pushl %eax
  1043. movl eip,%eax
  1044. pushl %eax
  1045. movl ebp,%ebp // Change frame pointer
  1046. {$endif}
  1047. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1048. jmpl DebugHandleErrorAddrFrame
  1049. {$else not SYSTEMEXCEPTIONDEBUG}
  1050. jmpl HandleErrorAddrFrame
  1051. {$endif SYSTEMEXCEPTIONDEBUG}
  1052. end;
  1053. end;
  1054. function i386_exception_handler(ExceptionRecord: PExceptionRecord;
  1055. EstablisherFrame: pointer; ContextRecord: PContext;
  1056. DispatcherContext: pointer): longint; cdecl;
  1057. var
  1058. res: longint;
  1059. must_reset_fpu: boolean;
  1060. begin
  1061. res := ExceptionContinueSearch;
  1062. if ContextRecord^.SegSs=_SS then begin
  1063. must_reset_fpu := true;
  1064. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1065. if IsConsole then Writeln(stderr,'Exception ',
  1066. hexstr(excep^.ExceptionRecord^.ExceptionCode, 8));
  1067. {$endif SYSTEMEXCEPTIONDEBUG}
  1068. case cardinal(ExceptionRecord^.ExceptionCode) of
  1069. STATUS_INTEGER_DIVIDE_BY_ZERO,
  1070. STATUS_FLOAT_DIVIDE_BY_ZERO :
  1071. res := 200;
  1072. STATUS_ARRAY_BOUNDS_EXCEEDED :
  1073. begin
  1074. res := 201;
  1075. must_reset_fpu := false;
  1076. end;
  1077. STATUS_STACK_OVERFLOW :
  1078. begin
  1079. res := 202;
  1080. must_reset_fpu := false;
  1081. end;
  1082. STATUS_FLOAT_OVERFLOW :
  1083. res := 205;
  1084. STATUS_FLOAT_DENORMAL_OPERAND,
  1085. STATUS_FLOAT_UNDERFLOW :
  1086. res := 206;
  1087. {excep^.ContextRecord^.FloatSave.StatusWord := excep^.ContextRecord^.FloatSave.StatusWord and $ffffff00;}
  1088. STATUS_FLOAT_INEXACT_RESULT,
  1089. STATUS_FLOAT_INVALID_OPERATION,
  1090. STATUS_FLOAT_STACK_CHECK :
  1091. res := 207;
  1092. STATUS_INTEGER_OVERFLOW :
  1093. begin
  1094. res := 215;
  1095. must_reset_fpu := false;
  1096. end;
  1097. STATUS_ILLEGAL_INSTRUCTION:
  1098. res := 216;
  1099. STATUS_ACCESS_VIOLATION:
  1100. { Athlon prefetch bug? }
  1101. if is_prefetch(pointer(ContextRecord^.Eip)) then
  1102. begin
  1103. { if yes, then retry }
  1104. ExceptionRecord^.ExceptionCode := 0;
  1105. res:=ExceptionContinueExecution;
  1106. end
  1107. else
  1108. res := 216;
  1109. STATUS_CONTROL_C_EXIT:
  1110. res := 217;
  1111. STATUS_PRIVILEGED_INSTRUCTION:
  1112. begin
  1113. res := 218;
  1114. must_reset_fpu := false;
  1115. end;
  1116. else
  1117. begin
  1118. if ((ExceptionRecord^.ExceptionCode and SEVERITY_ERROR) = SEVERITY_ERROR) then
  1119. res := 217
  1120. else
  1121. res := 255;
  1122. end;
  1123. end;
  1124. if (res >= 200) and (exceptLevel < MaxExceptionLevel) then begin
  1125. exceptEip[exceptLevel] := ContextRecord^.Eip;
  1126. exceptError[exceptLevel] := res;
  1127. resetFPU[exceptLevel] := must_reset_fpu;
  1128. inc(exceptLevel);
  1129. ContextRecord^.Eip := Longint(@JumpToHandleErrorFrame);
  1130. ExceptionRecord^.ExceptionCode := 0;
  1131. res := ExceptionContinueExecution;
  1132. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1133. if IsConsole then begin
  1134. writeln(stderr,'Exception Continue Exception set at ',
  1135. hexstr(exceptEip[exceptLevel],8));
  1136. writeln(stderr,'Eip changed to ',
  1137. hexstr(longint(@JumpToHandleErrorFrame),8), ' error=', error);
  1138. end;
  1139. {$endif SYSTEMEXCEPTIONDEBUG}
  1140. end;
  1141. end;
  1142. i386_exception_handler := res;
  1143. end;
  1144. {$endif CPUI386}
  1145. {$ifdef CPUARM}
  1146. {**************************** ARM Exception handling *****************************************}
  1147. var
  1148. exceptPC : array[0..MaxExceptionLevel-1] of Longint;
  1149. exceptError : array[0..MaxExceptionLevel-1] of Byte;
  1150. procedure JumpToHandleErrorFrame;
  1151. var
  1152. _pc, _fp, _error : Longint;
  1153. begin
  1154. // get original fp
  1155. asm
  1156. ldr r0,[r11,#-12]
  1157. str r0,_fp
  1158. end;
  1159. if (exceptLevel > 0) then
  1160. dec(exceptLevel);
  1161. _pc:=exceptPC[exceptLevel];
  1162. _error:=exceptError[exceptLevel];
  1163. asm
  1164. ldr r0,_error
  1165. ldr r1,_pc
  1166. ldr r2,_fp
  1167. mov r11,r2 // Change frame pointer
  1168. b HandleErrorAddrFrame
  1169. end;
  1170. end;
  1171. function ARM_ExceptionHandler(ExceptionRecord: PExceptionRecord;
  1172. EstablisherFrame: pointer; ContextRecord: PContext;
  1173. DispatcherContext: pointer): longint; [public, alias : '_ARM_ExceptionHandler'];
  1174. var
  1175. res: longint;
  1176. begin
  1177. res := ExceptionContinueSearch;
  1178. case cardinal(ExceptionRecord^.ExceptionCode) of
  1179. STATUS_INTEGER_DIVIDE_BY_ZERO,
  1180. STATUS_FLOAT_DIVIDE_BY_ZERO :
  1181. res := 200;
  1182. STATUS_ARRAY_BOUNDS_EXCEEDED :
  1183. res := 201;
  1184. STATUS_STACK_OVERFLOW :
  1185. res := 202;
  1186. STATUS_FLOAT_OVERFLOW :
  1187. res := 205;
  1188. STATUS_FLOAT_DENORMAL_OPERAND,
  1189. STATUS_FLOAT_UNDERFLOW :
  1190. res := 206;
  1191. STATUS_FLOAT_INEXACT_RESULT,
  1192. STATUS_FLOAT_INVALID_OPERATION,
  1193. STATUS_FLOAT_STACK_CHECK :
  1194. res := 207;
  1195. STATUS_INTEGER_OVERFLOW :
  1196. res := 215;
  1197. STATUS_ILLEGAL_INSTRUCTION:
  1198. res := 216;
  1199. STATUS_ACCESS_VIOLATION:
  1200. res := 216;
  1201. STATUS_DATATYPE_MISALIGNMENT:
  1202. res := 214;
  1203. STATUS_CONTROL_C_EXIT:
  1204. res := 217;
  1205. STATUS_PRIVILEGED_INSTRUCTION:
  1206. res := 218;
  1207. else
  1208. begin
  1209. if ((cardinal(ExceptionRecord^.ExceptionCode) and SEVERITY_ERROR) = SEVERITY_ERROR) then
  1210. res := 217
  1211. else
  1212. res := 255;
  1213. end;
  1214. end;
  1215. if (res <> ExceptionContinueSearch) and (exceptLevel < MaxExceptionLevel) then begin
  1216. exceptPC[exceptLevel] := ContextRecord^.PC;
  1217. exceptError[exceptLevel] := res;
  1218. inc(exceptLevel);
  1219. ContextRecord^.PC := Longint(@JumpToHandleErrorFrame);
  1220. ExceptionRecord^.ExceptionCode := 0;
  1221. res := ExceptionContinueExecution;
  1222. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1223. if IsConsole then begin
  1224. writeln(stderr,'Exception Continue Exception set at ',
  1225. hexstr(exceptEip[exceptLevel],8));
  1226. writeln(stderr,'Eip changed to ',
  1227. hexstr(longint(@JumpToHandleErrorFrame),8), ' error=', error);
  1228. end;
  1229. {$endif SYSTEMEXCEPTIONDEBUG}
  1230. end;
  1231. ARM_ExceptionHandler := res;
  1232. end;
  1233. {$endif CPUARM}
  1234. {$endif WINCE_EXCEPTION_HANDLING}
  1235. procedure Exe_entry;[public, alias : '_FPC_EXE_Entry'];
  1236. var
  1237. st: pointer;
  1238. begin
  1239. IsLibrary:=false;
  1240. {$ifdef CPUARM}
  1241. asm
  1242. str sp,st
  1243. end;
  1244. StackTop:=st;
  1245. asm
  1246. mov fp,#0
  1247. bl PASCALMAIN;
  1248. end;
  1249. {$endif CPUARM}
  1250. {$ifdef CPUI386}
  1251. asm
  1252. {$ifdef WINCE_EXCEPTION_HANDLING}
  1253. pushl i386_exception_handler
  1254. pushl %fs:(0)
  1255. mov %esp,%fs:(0)
  1256. {$endif WINCE_EXCEPTION_HANDLING}
  1257. pushl %ebp
  1258. movl %esp,%eax
  1259. movl %eax,st
  1260. end;
  1261. StackTop:=st;
  1262. asm
  1263. xorl %eax,%eax
  1264. movw %ss,%ax
  1265. movl %eax,_SS
  1266. xorl %ebp,%ebp
  1267. call PASCALMAIN
  1268. popl %ebp
  1269. {$ifdef WINCE_EXCEPTION_HANDLING}
  1270. popl %fs:(0)
  1271. addl $4, %esp
  1272. {$endif WINCE_EXCEPTION_HANDLING}
  1273. end;
  1274. {$endif CPUI386}
  1275. end;
  1276. procedure _FPC_mainCRTStartup;public name '_mainCRTStartup';
  1277. begin
  1278. IsConsole:=True;
  1279. Exe_entry;
  1280. end;
  1281. procedure _FPC_WinMainCRTStartup;public name '_WinMainCRTStartup';
  1282. begin
  1283. IsConsole:=False;
  1284. Exe_entry;
  1285. end;
  1286. procedure _FPC_DLLMainCRTStartup(_hinstance,_dllreason,_dllparam:longint);public name '_DLLMainCRTStartup';
  1287. begin
  1288. IsConsole:=true;
  1289. sysinstance:=_hinstance;
  1290. dllreason:=_dllreason;
  1291. dllparam:=_dllparam;
  1292. DLL_Entry;
  1293. end;
  1294. procedure _FPC_DLLWinMainCRTStartup(_hinstance,_dllreason,_dllparam:longint);public name '_DLLWinMainCRTStartup';
  1295. begin
  1296. IsConsole:=false;
  1297. sysinstance:=_hinstance;
  1298. dllreason:=_dllreason;
  1299. dllparam:=_dllparam;
  1300. DLL_Entry;
  1301. end;
  1302. {****************************************************************************
  1303. OS dependend widestrings
  1304. ****************************************************************************}
  1305. function CharUpperBuff(lpsz:LPWSTR; cchLength:DWORD):DWORD; cdecl; external KernelDLL name 'CharUpperBuffW';
  1306. function CharLowerBuff(lpsz:LPWSTR; cchLength:DWORD):DWORD; cdecl; external KernelDLL name 'CharLowerBuffW';
  1307. procedure WinCEWide2AnsiMove(source:pwidechar;var dest:RawByteString;cp:TSystemCodePage;len:SizeInt);
  1308. var
  1309. destlen: SizeInt;
  1310. begin
  1311. // retrieve length including trailing #0
  1312. // not anymore, because this must also be usable for single characters
  1313. destlen:=WideCharToMultiByte(cp, 0, source, len, nil, 0, nil, nil);
  1314. // this will null-terminate
  1315. setlength(dest, destlen);
  1316. if destlen>0 then
  1317. begin
  1318. WideCharToMultiByte(cp, 0, source, len, @dest[1], destlen, nil, nil);
  1319. PAnsiRec(pointer(dest)-AnsiFirstOff)^.CodePage:=cp;
  1320. end;
  1321. end;
  1322. procedure WinCEAnsi2WideMove(source:pchar;cp:TSystemCodePage;var dest:widestring;len:SizeInt);
  1323. var
  1324. destlen: SizeInt;
  1325. dwFlags: DWORD;
  1326. begin
  1327. if cp=CP_UTF8 then
  1328. dwFlags:=0
  1329. else
  1330. dwFlags:=MB_PRECOMPOSED;
  1331. destlen:=MultiByteToWideChar(cp, dwFlags, source, len, nil, 0);
  1332. // this will null-terminate
  1333. setlength(dest, destlen);
  1334. if destlen>0 then
  1335. MultiByteToWideChar(cp, dwFlags, source, len, @dest[1], destlen);
  1336. end;
  1337. function WinCEWideUpper(const s : WideString) : WideString;
  1338. begin
  1339. result:=s;
  1340. UniqueString(result);
  1341. if length(result)>0 then
  1342. CharUpperBuff(LPWSTR(result),length(result));
  1343. end;
  1344. function WinCEWideLower(const s : WideString) : WideString;
  1345. begin
  1346. result:=s;
  1347. UniqueString(result);
  1348. if length(result)>0 then
  1349. CharLowerBuff(LPWSTR(result),length(result));
  1350. end;
  1351. procedure WinCEUnicode2AnsiMove(source:punicodechar;var dest:RawByteString;cp:TSystemCodePage;len:SizeInt);
  1352. var
  1353. destlen: SizeInt;
  1354. begin
  1355. // retrieve length including trailing #0
  1356. // not anymore, because this must also be usable for single characters
  1357. destlen:=WideCharToMultiByte(cp, 0, source, len, nil, 0, nil, nil);
  1358. // this will null-terminate
  1359. setlength(dest, destlen);
  1360. if destlen>0 then
  1361. begin
  1362. WideCharToMultiByte(cp, 0, source, len, @dest[1], destlen, nil, nil);
  1363. PAnsiRec(pointer(dest)-AnsiFirstOff)^.CodePage:=cp;
  1364. end;
  1365. end;
  1366. procedure WinCEAnsi2UnicodeMove(source:pchar;cp : TSystemCodePage;var dest:UnicodeString;len:SizeInt);
  1367. var
  1368. destlen: SizeInt;
  1369. dwflags: DWORD;
  1370. begin
  1371. if cp=CP_UTF8 then
  1372. dwFlags:=0
  1373. else
  1374. dwFlags:=MB_PRECOMPOSED;
  1375. destlen:=MultiByteToWideChar(cp, dwFlags, source, len, nil, 0);
  1376. // this will null-terminate
  1377. setlength(dest, destlen);
  1378. if destlen>0 then
  1379. begin
  1380. MultiByteToWideChar(cp, dwFlags, source, len, @dest[1], destlen);
  1381. PUnicodeRec(pointer(dest)-UnicodeFirstOff)^.CodePage:=CP_UTF16;
  1382. end;
  1383. end;
  1384. function WinCEUnicodeUpper(const s : UnicodeString) : UnicodeString;
  1385. begin
  1386. Result:=WinCEWideUpper(s);
  1387. end;
  1388. function WinCEUnicodeLower(const s : UnicodeString) : UnicodeString;
  1389. begin
  1390. Result:=WinCEWideLower(s);
  1391. end;
  1392. function WinCEGetStandardCodePage(const stdcp: TStandardCodePageEnum): TSystemCodePage;
  1393. begin
  1394. case stdcp of
  1395. scpAnsi: Result := GetACP;
  1396. scpConsoleInput: Result := GetConsoleCP;
  1397. scpConsoleOutput: Result := GetConsoleOutputCP;
  1398. end;
  1399. end;
  1400. { there is a similiar procedure in sysutils which inits the fields which
  1401. are only relevant for the sysutils units }
  1402. procedure InitWinCEWidestrings;
  1403. begin
  1404. widestringmanager.Wide2AnsiMoveProc:=@WinCEWide2AnsiMove;
  1405. widestringmanager.Ansi2WideMoveProc:=@WinCEAnsi2WideMove;
  1406. widestringmanager.UpperWideStringProc:=@WinCEWideUpper;
  1407. widestringmanager.LowerWideStringProc:=@WinCEWideLower;
  1408. { Unicode }
  1409. widestringmanager.Unicode2AnsiMoveProc:=@WinCEUnicode2AnsiMove;
  1410. widestringmanager.Ansi2UnicodeMoveProc:=@WinCEAnsi2UnicodeMove;
  1411. widestringmanager.UpperUnicodeStringProc:=@WinCEUnicodeUpper;
  1412. widestringmanager.LowerUnicodeStringProc:=@WinCEUnicodeLower;
  1413. { Codepage }
  1414. widestringmanager.GetStandardCodePageProc:=@WinCEGetStandardCodePage;
  1415. DefaultSystemCodePage:=GetACP;
  1416. DefaultUnicodeCodePage:=CP_UTF16;
  1417. end;
  1418. {$IFDEF HAS_MEMORYMANAGER}
  1419. {****************************************************************************
  1420. Memory manager
  1421. ****************************************************************************}
  1422. function malloc(Size : ptruint) : Pointer; cdecl; external 'coredll';
  1423. procedure free(P : pointer); cdecl; external 'coredll';
  1424. function realloc(P : Pointer; Size : ptruint) : pointer; cdecl; external 'coredll';
  1425. function _msize(P : pointer): ptruint; cdecl; external 'coredll';
  1426. function SysGetMem (Size : ptruint) : Pointer;
  1427. begin
  1428. Result:=malloc(Size);
  1429. end;
  1430. Function SysFreeMem (P : pointer) : ptruint;
  1431. begin
  1432. free(P);
  1433. Result:=0;
  1434. end;
  1435. Function SysFreeMemSize(p:pointer;Size:ptruint):ptruint;
  1436. begin
  1437. Result:=0;
  1438. if (size > 0) and (p <> nil) then
  1439. Result:=SysFreeMem(P);
  1440. end;
  1441. Function SysAllocMem(Size : ptruint) : Pointer;
  1442. begin
  1443. Result:=SysGetMem(Size);
  1444. if Result <> nil then
  1445. FillChar(Result^, Size, 0);
  1446. end;
  1447. Function SysReAllocMem (var p:pointer;Size:ptruint):Pointer;
  1448. begin
  1449. Result:=realloc(p, Size);
  1450. p:=Result;
  1451. end;
  1452. function SysTryResizeMem(var p:pointer;size : ptruint):boolean;
  1453. var
  1454. res: pointer;
  1455. begin
  1456. res:=realloc(p, Size);
  1457. Result:=(res <> nil) or (Size = 0);
  1458. if Result then
  1459. p:=res;
  1460. end;
  1461. function SysMemSize(P : pointer): ptruint;
  1462. begin
  1463. Result:=_msize(P);
  1464. end;
  1465. function SysGetHeapStatus:THeapStatus;
  1466. begin
  1467. fillchar(Result,sizeof(Result),0);
  1468. end;
  1469. function SysGetFPCHeapStatus:TFPCHeapStatus;
  1470. begin
  1471. fillchar(Result,sizeof(Result),0);
  1472. end;
  1473. {$ENDIF HAS_MEMORYMANAGER}
  1474. {****************************************************************************
  1475. Error Message writing using messageboxes
  1476. ****************************************************************************}
  1477. const
  1478. ErrorBufferLength = 1024;
  1479. var
  1480. ErrorBuf : array[0..ErrorBufferLength] of char;
  1481. ErrorBufW : array[0..ErrorBufferLength] of widechar;
  1482. ErrorLen : longint;
  1483. Function ErrorWrite(Var F: TextRec): Integer;
  1484. {
  1485. An error message should always end with #13#10#13#10
  1486. }
  1487. var
  1488. i : longint;
  1489. Begin
  1490. while F.BufPos>0 do
  1491. begin
  1492. begin
  1493. if F.BufPos+ErrorLen>ErrorBufferLength then
  1494. i:=ErrorBufferLength-ErrorLen
  1495. else
  1496. i:=F.BufPos;
  1497. Move(F.BufPtr^,ErrorBuf[ErrorLen],i);
  1498. inc(ErrorLen,i);
  1499. ErrorBuf[ErrorLen]:=#0;
  1500. end;
  1501. if ErrorLen=ErrorBufferLength then
  1502. begin
  1503. AnsiToWideBuf(@ErrorBuf, -1, @ErrorBufW, SizeOf(ErrorBufW));
  1504. MessageBox(0,@ErrorBufW,'Error',$10010); { MB_SETFOREGROUND or ICON_ERROR }
  1505. ErrorLen:=0;
  1506. end;
  1507. Dec(F.BufPos,i);
  1508. end;
  1509. ErrorWrite:=0;
  1510. End;
  1511. Function ErrorClose(Var F: TextRec): Integer;
  1512. begin
  1513. if ErrorLen>0 then
  1514. begin
  1515. AnsiToWideBuf(@ErrorBuf, -1, @ErrorBufW, SizeOf(ErrorBufW));
  1516. MessageBox(0,@ErrorBufW,'Error',$10010); { MB_SETFOREGROUND or ICON_ERROR }
  1517. ErrorLen:=0;
  1518. end;
  1519. ErrorLen:=0;
  1520. ErrorClose:=0;
  1521. end;
  1522. Function ErrorOpen(Var F: TextRec): Integer;
  1523. Begin
  1524. TextRec(F).InOutFunc:=@ErrorWrite;
  1525. TextRec(F).FlushFunc:=@ErrorWrite;
  1526. TextRec(F).CloseFunc:=@ErrorClose;
  1527. ErrorLen:=0;
  1528. ErrorOpen:=0;
  1529. End;
  1530. procedure AssignError(Var T: Text);
  1531. begin
  1532. Assign(T,'');
  1533. TextRec(T).OpenFunc:=@ErrorOpen;
  1534. Rewrite(T);
  1535. end;
  1536. function _getstdfilex(fd: integer): pointer; cdecl; external 'coredll';
  1537. function _fileno(fd: pointer): THandle; cdecl; external 'coredll';
  1538. procedure SysInitStdIO;
  1539. begin
  1540. { Setup stdin, stdout and stderr, for GUI apps redirect stderr,stdout to be
  1541. displayed in and messagebox }
  1542. if not IsConsole then
  1543. begin
  1544. AssignError(stderr);
  1545. AssignError(stdout);
  1546. Assign(Output,'');
  1547. Assign(Input,'');
  1548. Assign(ErrOutput,'');
  1549. end
  1550. else
  1551. begin
  1552. StdInputHandle:=_fileno(_getstdfilex(0));
  1553. StdOutputHandle:=_fileno(_getstdfilex(1));
  1554. StdErrorHandle:=_fileno(_getstdfilex(2));
  1555. OpenStdIO(Input,fmInput,StdInputHandle);
  1556. OpenStdIO(Output,fmOutput,StdOutputHandle);
  1557. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  1558. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  1559. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  1560. end;
  1561. end;
  1562. (* ProcessID cached to avoid repeated calls to GetCurrentProcess. *)
  1563. var
  1564. ProcessID: SizeUInt;
  1565. function GetProcessID: SizeUInt;
  1566. begin
  1567. GetProcessID := ProcessID;
  1568. end;
  1569. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  1570. begin
  1571. result := stklen;
  1572. end;
  1573. procedure SysCleanup;
  1574. var
  1575. i: integer;
  1576. begin
  1577. if FCmdLine = nil then
  1578. exit;
  1579. SysFreeMem(FCmdLine);
  1580. for i:=0 to Fargc do
  1581. sysfreemem(Fargv[i]);
  1582. sysfreemem(Fargv);
  1583. end;
  1584. initialization
  1585. SysResetFPU;
  1586. if not(IsLibrary) then
  1587. SysInitFPU;
  1588. StackLength := CheckInitialStkLen(InitialStkLen);
  1589. StackBottom := StackTop - StackLength;
  1590. { some misc stuff }
  1591. hprevinst:=0;
  1592. if not IsLibrary then
  1593. SysInstance:=GetModuleHandle(nil);
  1594. MainInstance:=SysInstance;
  1595. {$IFNDEF HAS_MEMORYMANAGER}
  1596. { Setup Heap }
  1597. InitHeap;
  1598. {$ENDIF HAS_MEMORYMANAGER}
  1599. SysInitExceptions;
  1600. initunicodestringmanager;
  1601. InitWinCEWidestrings;
  1602. if not IsLibrary then
  1603. begin
  1604. SysInitStdIO;
  1605. end;
  1606. { Reset IO Error }
  1607. InOutRes:=0;
  1608. ProcessID := GetCurrentProcessID;
  1609. { threading }
  1610. InitSystemThreads;
  1611. { Reset internal error variable }
  1612. errno:=0;
  1613. initvariantmanager;
  1614. DispCallByIDProc:=@DoDispCallByIDError;
  1615. finalization
  1616. SysCleanup;
  1617. end.