globals.pas 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements some support functions and global variables
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  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. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit globals;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. {$ifdef win32}
  22. windows,
  23. {$endif}
  24. {$ifdef os2}
  25. dos,
  26. {$endif os2}
  27. {$ifdef hasunix}
  28. Baseunix,unix,
  29. {$endif}
  30. {$IFNDEF USE_FAKE_SYSUTILS}
  31. sysutils,
  32. {$ELSE}
  33. fksysutl,
  34. {$ENDIF}
  35. { comphook pulls in sysutils anyways }
  36. cutils,cclasses,cfileutl,
  37. cpuinfo,
  38. globtype,version,systems;
  39. const
  40. delphimodeswitches =
  41. [m_delphi,m_all,m_class,m_objpas,m_result,m_string_pchar,
  42. m_pointer_2_procedure,m_autoderef,m_tp_procvar,m_initfinal,m_default_ansistring,
  43. m_out,m_default_para,m_duplicate_names,m_hintdirective,m_add_pointer,
  44. m_property,m_default_inline,m_except];
  45. fpcmodeswitches =
  46. [m_fpc,m_all,m_string_pchar,m_nested_comment,m_repeat_forward,
  47. m_cvar_support,m_initfinal,m_add_pointer,m_hintdirective,
  48. m_property,m_default_inline];
  49. objfpcmodeswitches =
  50. [m_objfpc,m_fpc,m_all,m_class,m_objpas,m_result,m_string_pchar,m_nested_comment,
  51. m_repeat_forward,m_cvar_support,m_initfinal,m_add_pointer,m_out,m_default_para,m_hintdirective,
  52. m_property,m_default_inline,m_except];
  53. tpmodeswitches =
  54. [m_tp7,m_all,m_tp_procvar,m_duplicate_names];
  55. {$ifdef gpc_mode}
  56. gpcmodeswitches =
  57. [m_gpc,m_all,m_tp_procvar];
  58. {$endif}
  59. macmodeswitches =
  60. [m_mac,m_all,m_result,m_cvar_support,m_mac_procvar];
  61. { maximum nesting of routines }
  62. maxnesting = 32;
  63. { Filenames and extensions }
  64. sourceext = '.pp';
  65. pasext = '.pas';
  66. pext = '.p';
  67. treelogfilename = 'tree.log';
  68. {$if defined(CPUARM) and defined(FPUFPA)}
  69. MathQNaN : tdoublerec = (bytes : (0,0,252,255,0,0,0,0));
  70. MathInf : tdoublerec = (bytes : (0,0,240,127,0,0,0,0));
  71. MathNegInf : tdoublerec = (bytes : (0,0,240,255,0,0,0,0));
  72. MathPi : tdoublerec = (bytes : (251,33,9,64,24,45,68,84));
  73. {$else}
  74. {$ifdef FPC_LITTLE_ENDIAN}
  75. MathQNaN : tdoublerec = (bytes : (0,0,0,0,0,0,252,255));
  76. MathInf : tdoublerec = (bytes : (0,0,0,0,0,0,240,127));
  77. MathNegInf : tdoublerec = (bytes : (0,0,0,0,0,0,240,255));
  78. MathPi : tdoublerec = (bytes : (24,45,68,84,251,33,9,64));
  79. MathPiExtended : textendedrec = (bytes : (53,194,104,33,162,218,15,201,0,64));
  80. {$else FPC_LITTLE_ENDIAN}
  81. MathQNaN : tdoublerec = (bytes : (255,252,0,0,0,0,0,0));
  82. MathInf : tdoublerec = (bytes : (127,240,0,0,0,0,0,0));
  83. MathNegInf : tdoublerec = (bytes : (255,240,0,0,0,0,0,0));
  84. MathPi : tdoublerec = (bytes : (64,9,33,251,84,68,45,24));
  85. MathPiExtended : textendedrec = (bytes : (64,0,201,15,218,162,33,104,194,53));
  86. {$endif FPC_LITTLE_ENDIAN}
  87. {$endif}
  88. type
  89. tcodepagestring = string[20];
  90. tsettings = record
  91. globalswitches : tglobalswitches;
  92. moduleswitches : tmoduleswitches;
  93. localswitches : tlocalswitches;
  94. modeswitches : tmodeswitches;
  95. optimizerswitches : toptimizerswitches;
  96. debugswitches : tdebugswitches;
  97. { 0: old behaviour for sets <=256 elements
  98. >0: round to this size }
  99. setalloc,
  100. packenum : shortint;
  101. alignment : talignmentinfo;
  102. cputype,
  103. optimizecputype : tcputype;
  104. fputype : tfputype;
  105. asmmode : tasmmode;
  106. interfacetype : tinterfacetypes;
  107. defproccall : tproccalloption;
  108. sourcecodepage : tcodepagestring;
  109. packrecords : shortint;
  110. maxfpuregisters : shortint;
  111. minfpconstprec : tfloattype;
  112. end;
  113. const
  114. LinkMapWeightDefault = 1000;
  115. type
  116. TLinkRec = record
  117. Key : AnsiString;
  118. Value : AnsiString; // key expands to valuelist "value"
  119. Weight: longint;
  120. end;
  121. TLinkStrMap = class
  122. private
  123. itemcnt : longint;
  124. fmap : Array Of TLinkRec;
  125. function Lookup(key:Ansistring):longint;
  126. function getlinkrec(i:longint):TLinkRec;
  127. public
  128. procedure Add(key:ansistring;value:AnsiString='';weight:longint=LinkMapWeightDefault);
  129. procedure addseries(keys:AnsiString;weight:longint=LinkMapWeightDefault);
  130. function AddDep(keyvalue:String):boolean;
  131. function AddWeight(keyvalue:String):boolean;
  132. procedure SetValue(key:AnsiString;Weight:Integer);
  133. procedure SortonWeight;
  134. function Find(key:AnsiString):AnsiString;
  135. procedure Expand(src:TCmdStrList;dest: TLinkStrMap);
  136. procedure UpdateWeights(Weightmap:TLinkStrMap);
  137. constructor Create;
  138. property count : longint read itemcnt;
  139. property items[I:longint]:TLinkRec read getlinkrec; default;
  140. end;
  141. var
  142. { specified inputfile }
  143. inputfilepath : string;
  144. inputfilename : string;
  145. { specified outputfile with -o parameter }
  146. outputfilename : string;
  147. outputprefix : pshortstring;
  148. outputsuffix : pshortstring;
  149. { specified with -FE or -FU }
  150. outputexedir : TPathStr;
  151. outputunitdir : TPathStr;
  152. { things specified with parameters }
  153. paratarget : tsystem;
  154. paratargetdbg : tdbg;
  155. paratargetasm : tasm;
  156. paralinkoptions : TCmdStr;
  157. paradynamiclinker : string;
  158. paraprintnodetree : byte;
  159. parapreprocess : boolean;
  160. printnodefile : text;
  161. { typical cross compiling params}
  162. { directory where the utils can be found (options -FD) }
  163. utilsdirectory : TPathStr;
  164. { targetname specific prefix used by these utils (options -XP<path>) }
  165. utilsprefix : TCmdStr;
  166. cshared : boolean; { pass --shared to ld to link C libs shared}
  167. Dontlinkstdlibpath: Boolean; { Don't add std paths to linkpath}
  168. rlinkpath : TCmdStr; { rpath-link linkdir override}
  169. sysrootpath : TCmdStr; { target system root to search dyn linker }
  170. { some flags for global compiler switches }
  171. do_build,
  172. do_release,
  173. do_make : boolean;
  174. { path for searching units, different paths can be seperated by ; }
  175. exepath : TPathStr; { Path to ppc }
  176. librarysearchpath,
  177. unitsearchpath,
  178. objectsearchpath,
  179. includesearchpath,
  180. frameworksearchpath : TSearchPathList;
  181. autoloadunits : string;
  182. { linking }
  183. usewindowapi : boolean;
  184. description : string;
  185. SetPEFlagsSetExplicity,
  186. ImageBaseSetExplicity,
  187. MinStackSizeSetExplicity,
  188. MaxStackSizeSetExplicity,
  189. DescriptionSetExplicity : boolean;
  190. dllversion : string;
  191. dllmajor,
  192. dllminor,
  193. dllrevision : word; { revision only for netware }
  194. { win pe }
  195. peflags : longint;
  196. minstacksize,
  197. maxstacksize,
  198. imagebase : aword;
  199. UseDeffileForExports : boolean;
  200. UseDeffileForExportsSetExplicitly : boolean;
  201. GenerateImportSection,
  202. GenerateImportSectionSetExplicitly,
  203. RelocSection : boolean;
  204. RelocSectionSetExplicitly : boolean;
  205. LinkTypeSetExplicitly : boolean;
  206. current_tokenpos, { position of the last token }
  207. current_filepos : tfileposinfo; { current position }
  208. nwscreenname : string;
  209. nwthreadname : string;
  210. nwcopyright : string;
  211. codegenerror : boolean; { true if there is an error reported }
  212. block_type : tblock_type; { type of currently parsed block }
  213. compile_level : word;
  214. resolving_forward : boolean; { used to add forward reference as second ref }
  215. exceptblockcounter : integer; { each except block gets a unique number check gotos }
  216. aktexceptblock : integer; { the exceptblock number of the current block (0 if none) }
  217. LinkLibraryAliases : TLinkStrMap;
  218. LinkLibraryOrder : TLinkStrMap;
  219. init_settings,
  220. current_settings : tsettings;
  221. nextlocalswitches : tlocalswitches;
  222. localswitcheschanged : boolean;
  223. { Memory sizes }
  224. heapsize,
  225. stacksize,
  226. jmp_buf_size : longint;
  227. {$Ifdef EXTDEBUG}
  228. { parameter switches }
  229. debugstop : boolean;
  230. {$EndIf EXTDEBUG}
  231. { windows / OS/2 application type }
  232. apptype : tapptype;
  233. features : tfeatures;
  234. const
  235. DLLsource : boolean = false;
  236. DLLImageBase : pshortstring = nil;
  237. { used to set all registers used for each global function
  238. this should dramatically decrease the number of
  239. recompilations needed PM }
  240. simplify_ppu : boolean = true;
  241. Inside_asm_statement : boolean = false;
  242. global_unit_count : word = 0;
  243. { for error info in pp.pas }
  244. parser_current_file : string = '';
  245. {$if defined(m68k) or defined(arm)}
  246. { PalmOS resources }
  247. palmos_applicationname : string = 'FPC Application';
  248. palmos_applicationid : string[4] = 'FPCA';
  249. {$endif defined(m68k) or defined(arm)}
  250. {$ifdef powerpc}
  251. { default calling convention used on MorphOS }
  252. syscall_convention : string = 'LEGACY';
  253. {$endif powerpc}
  254. { default name of the C-style "main" procedure of the library/program }
  255. { (this will be prefixed with the target_info.cprefix) }
  256. defaultmainaliasname = 'main';
  257. mainaliasname : string = defaultmainaliasname;
  258. { by default no local variable trashing }
  259. localvartrashing: longint = -1;
  260. { actual values are defined in ncgutil.pas }
  261. nroftrashvalues = 4;
  262. const
  263. default_settings : TSettings = (
  264. globalswitches : [cs_check_unit_name,cs_link_static];
  265. moduleswitches : [cs_extsyntax,cs_implicit_exceptions];
  266. localswitches : [cs_check_io,cs_typed_const_writable];
  267. modeswitches : fpcmodeswitches;
  268. optimizerswitches : [];
  269. debugswitches : [];
  270. setalloc : 0;
  271. packenum : 4;
  272. alignment : (
  273. procalign : 0;
  274. loopalign : 0;
  275. jumpalign : 0;
  276. constalignmin : 0;
  277. constalignmax : 0;
  278. varalignmin : 0;
  279. varalignmax : 0;
  280. localalignmin : 0;
  281. localalignmax : 0;
  282. recordalignmin : 0;
  283. recordalignmax : 0;
  284. maxCrecordalign : 0;
  285. );
  286. {$ifdef i386}
  287. cputype : cpu_Pentium;
  288. optimizecputype : cpu_Pentium3;
  289. fputype : fpu_x87;
  290. {$endif i386}
  291. {$ifdef m68k}
  292. cputype : cpu_MC68020;
  293. fputype : fpu_soft;
  294. {$endif m68k}
  295. {$ifdef powerpc}
  296. cputype : cpu_PPC604;
  297. optimizecputype : cpu_ppc7400;
  298. fputype : fpu_standard;
  299. {$endif powerpc}
  300. {$ifdef POWERPC64}
  301. cputype : cpu_PPC970;
  302. optimizecputype : cpu_ppc970;
  303. fputype : fpu_standard;
  304. {$endif POWERPC64}
  305. {$ifdef sparc}
  306. cputype : cpu_SPARC_V8;
  307. fputype : fpu_hard;
  308. {$endif sparc}
  309. {$ifdef arm}
  310. cputype : cpu_armv3;
  311. fputype : fpu_fpa;
  312. {$endif arm}
  313. {$ifdef x86_64}
  314. cputype : cpu_athlon64;
  315. fputype : fpu_sse64;
  316. {$endif x86_64}
  317. {$ifdef avr}
  318. cputype : cpuinfo.cpu_avr;
  319. fputype : fpu_none;
  320. {$endif avr}
  321. asmmode : asmmode_standard;
  322. interfacetype : it_interfacecom;
  323. defproccall : pocall_default;
  324. sourcecodepage : '8859-1';
  325. packrecords : 0;
  326. maxfpuregisters : 0;
  327. minfpconstprec : s32real;
  328. );
  329. var
  330. starttime : real;
  331. function getdatestr:string;
  332. function gettimestr:string;
  333. function filetimestring( t : longint) : string;
  334. function getrealtime : real;
  335. procedure DefaultReplacements(var s:ansistring);
  336. function Shell(const command:ansistring): longint;
  337. function GetEnvPChar(const envname:string):pchar;
  338. procedure FreeEnvPChar(p:pchar);
  339. function is_number_float(d : double) : boolean;
  340. { discern +0.0 and -0.0 }
  341. function get_real_sign(r: bestreal): longint;
  342. procedure InitGlobals;
  343. procedure DoneGlobals;
  344. function string2guid(const s: string; var GUID: TGUID): boolean;
  345. function guid2string(const GUID: TGUID): string;
  346. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  347. function Setabitype(const s:string;var a:tabi):boolean;
  348. function Setcputype(const s:string;var a:tcputype):boolean;
  349. function SetFpuType(const s:string;var a:tfputype):boolean;
  350. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  351. function UpdateOptimizerStr(s:string;var a:toptimizerswitches):boolean;
  352. function UpdateDebugStr(s:string;var a:tdebugswitches):boolean;
  353. function IncludeFeature(const s : string) : boolean;
  354. function SetMinFPConstPrec(const s: string; var a: tfloattype) : boolean;
  355. {# Routine to get the required alignment for size of data, which will
  356. be placed in bss segment, according to the current alignment requirements }
  357. function var_align(siz: longint): shortint;
  358. {# Routine to get the required alignment for size of data, which will
  359. be placed in data/const segment, according to the current alignment requirements }
  360. function const_align(siz: longint): shortint;
  361. {$ifdef ARM}
  362. function is_double_hilo_swapped: boolean;{$ifdef USEINLINE}inline;{$endif}
  363. {$endif ARM}
  364. function floating_point_range_check_error : boolean;
  365. implementation
  366. uses
  367. {$ifdef macos}
  368. macutils,
  369. {$endif}
  370. comphook;
  371. {****************************************************************************
  372. TLinkStrMap
  373. ****************************************************************************}
  374. Constructor TLinkStrMap.create;
  375. begin
  376. inherited;
  377. itemcnt:=0;
  378. end;
  379. procedure TLinkStrMap.Add(key:ansistring;value:AnsiString='';weight:longint=LinkMapWeightDefault);
  380. begin
  381. if lookup(key)<>-1 Then
  382. exit;
  383. if itemcnt<=length(fmap) Then
  384. setlength(fmap,itemcnt+10);
  385. fmap[itemcnt].key:=key;
  386. fmap[itemcnt].value:=value;
  387. fmap[itemcnt].weight:=weight;
  388. inc(itemcnt);
  389. end;
  390. function TLinkStrMap.AddDep(keyvalue:String):boolean;
  391. var
  392. i : Longint;
  393. begin
  394. AddDep:=false;
  395. i:=pos('=',keyvalue);
  396. if i=0 then
  397. exit;
  398. Add(Copy(KeyValue,1,i-1),Copy(KeyValue,i+1,length(KeyValue)-i));
  399. AddDep:=True;
  400. end;
  401. function TLinkStrMap.AddWeight(keyvalue:String):boolean;
  402. var
  403. i,j : Longint;
  404. Code : Word;
  405. s : AnsiString;
  406. begin
  407. AddWeight:=false;
  408. i:=pos('=',keyvalue);
  409. if i=0 then
  410. exit;
  411. s:=Copy(KeyValue,i+1,length(KeyValue)-i);
  412. val(s,j,code);
  413. if code=0 Then
  414. begin
  415. Add(Copy(KeyValue,1,i-1),'',j);
  416. AddWeight:=True;
  417. end;
  418. end;
  419. procedure TLinkStrMap.addseries(keys:AnsiString;weight:longint);
  420. var
  421. i,j,k : longint;
  422. begin
  423. k:=length(keys);
  424. i:=1;
  425. while i<=k do
  426. begin
  427. j:=i;
  428. while (i<=k) and (keys[i]<>',') do
  429. inc(i);
  430. add(copy(keys,j,i-j),'',weight);
  431. inc(i);
  432. end;
  433. end;
  434. procedure TLinkStrMap.SetValue(Key:Ansistring;weight:Integer);
  435. var
  436. j : longint;
  437. begin
  438. j:=lookup(key);
  439. if j<>-1 then
  440. fmap[j].weight:=weight;
  441. end;
  442. function TLinkStrMap.find(key:Ansistring):Ansistring;
  443. var
  444. j : longint;
  445. begin
  446. find:='';
  447. j:=lookup(key);
  448. if j<>-1 then
  449. find:=fmap[j].value;
  450. end;
  451. function TLinkStrMap.lookup(key:Ansistring):longint;
  452. var
  453. i : longint;
  454. begin
  455. lookup:=-1;
  456. i:=0;
  457. while (i<itemcnt) and (fmap[i].key<>key) do
  458. inc(i);
  459. if i<>itemcnt then
  460. lookup:=i;
  461. end;
  462. procedure TLinkStrMap.SortOnWeight;
  463. var
  464. i, j : longint;
  465. m : TLinkRec;
  466. begin
  467. if itemcnt <2 then exit;
  468. for i:=0 to itemcnt-1 do
  469. for j:=i+1 to itemcnt-1 do
  470. begin
  471. if fmap[i].weight>fmap[j].weight Then
  472. begin
  473. m:=fmap[i];
  474. fmap[i]:=fmap[j];
  475. fmap[j]:=m;
  476. end;
  477. end;
  478. end;
  479. function TLinkStrMap.getlinkrec(i:longint):TLinkRec;
  480. begin
  481. result:=fmap[i];
  482. end;
  483. procedure TLinkStrMap.Expand(Src:TCmdStrList;Dest:TLinkStrMap);
  484. // expands every thing in Src to Dest for linkorder purposes.
  485. var
  486. r : longint;
  487. LibN : TCmdStr;
  488. begin
  489. while not src.empty do
  490. begin
  491. LibN:=src.getfirst;
  492. r:=lookup (LibN);
  493. if r=-1 then
  494. dest.add(LibN)
  495. else
  496. dest.addseries(fmap[r].value);
  497. end;
  498. end;
  499. procedure TLinkStrMap.UpdateWeights(Weightmap:TLinkStrMap);
  500. var
  501. l,r : longint;
  502. begin
  503. for l := 0 to itemcnt-1 do
  504. begin
  505. r:=weightmap.lookup (fmap[l].key);
  506. if r<>-1 then
  507. fmap[l].weight:=weightmap[r].weight;
  508. end;
  509. end;
  510. {****************************************************************************
  511. Time Handling
  512. ****************************************************************************}
  513. Function L0(l:longint):string;
  514. {
  515. return the string of value l, if l<10 then insert a zero, so
  516. the string is always at least 2 chars '01','02',etc
  517. }
  518. var
  519. s : string;
  520. begin
  521. Str(l,s);
  522. if l<10 then
  523. s:='0'+s;
  524. L0:=s;
  525. end;
  526. function gettimestr:string;
  527. {
  528. get the current time in a string HH:MM:SS
  529. }
  530. var
  531. hour,min,sec,hsec : word;
  532. begin
  533. DecodeTime(Time,hour,min,sec,hsec);
  534. gettimestr:=L0(Hour)+':'+L0(min)+':'+L0(sec);
  535. end;
  536. function getdatestr:string;
  537. {
  538. get the current date in a string YY/MM/DD
  539. }
  540. var
  541. Year,Month,Day: Word;
  542. begin
  543. DecodeDate(Date,year,month,day);
  544. getdatestr:=L0(Year)+'/'+L0(Month)+'/'+L0(Day);
  545. end;
  546. function filetimestring( t : longint) : string;
  547. {
  548. convert dos datetime t to a string YY/MM/DD HH:MM:SS
  549. }
  550. var
  551. DT : TDateTime;
  552. hsec : word;
  553. Year,Month,Day: Word;
  554. hour,min,sec : word;
  555. begin
  556. if t=-1 then
  557. begin
  558. Result := 'Not Found';
  559. exit;
  560. end;
  561. DT := FileDateToDateTime(t);
  562. DecodeTime(DT,hour,min,sec,hsec);
  563. DecodeDate(DT,year,month,day);
  564. Result := L0(Year)+'/'+L0(Month)+'/'+L0(Day)+' '+L0(Hour)+':'+L0(min)+':'+L0(sec);
  565. end;
  566. function getrealtime : real;
  567. var
  568. h,m,s,s1000 : word;
  569. begin
  570. DecodeTime(Time,h,m,s,s1000);
  571. result:=h*3600.0+m*60.0+s+s1000/1000.0;
  572. end;
  573. {****************************************************************************
  574. Default Macro Handling
  575. ****************************************************************************}
  576. procedure DefaultReplacements(var s:ansistring);
  577. begin
  578. { Replace some macros }
  579. Replace(s,'$FPCVERSION',version_string);
  580. Replace(s,'$FPCFULLVERSION',full_version_string);
  581. Replace(s,'$FPCDATE',date_string);
  582. Replace(s,'$FPCCPU',target_cpu_string);
  583. Replace(s,'$FPCOS',target_os_string);
  584. if tf_use_8_3 in Source_Info.Flags then
  585. Replace(s,'$FPCTARGET',target_os_string)
  586. else
  587. Replace(s,'$FPCTARGET',target_full_string);
  588. end;
  589. {****************************************************************************
  590. OS Dependent things
  591. ****************************************************************************}
  592. function GetEnvPChar(const envname:string):pchar;
  593. {$ifdef win32}
  594. var
  595. s : string;
  596. i,len : longint;
  597. hp,p,p2 : pchar;
  598. {$endif}
  599. begin
  600. {$ifdef hasunix}
  601. GetEnvPchar:=BaseUnix.fpGetEnv(envname);
  602. {$define GETENVOK}
  603. {$endif}
  604. {$ifdef win32}
  605. GetEnvPchar:=nil;
  606. p:=GetEnvironmentStrings;
  607. hp:=p;
  608. while hp^<>#0 do
  609. begin
  610. s:=strpas(hp);
  611. i:=pos('=',s);
  612. len:=strlen(hp);
  613. if upper(copy(s,1,i-1))=upper(envname) then
  614. begin
  615. GetMem(p2,len-length(envname));
  616. Move(hp[i],p2^,len-length(envname));
  617. GetEnvPchar:=p2;
  618. break;
  619. end;
  620. { next string entry}
  621. hp:=hp+len+1;
  622. end;
  623. FreeEnvironmentStrings(p);
  624. {$define GETENVOK}
  625. {$endif}
  626. {$ifdef os2}
  627. GetEnvPChar := Dos.GetEnvPChar (EnvName);
  628. {$define GETENVOK}
  629. {$endif}
  630. {$ifdef GETENVOK}
  631. {$undef GETENVOK}
  632. {$else}
  633. GetEnvPchar:=StrPNew(GetEnvironmentVariable(envname));
  634. {$endif}
  635. end;
  636. procedure FreeEnvPChar(p:pchar);
  637. begin
  638. {$ifndef hasunix}
  639. {$ifndef os2}
  640. freemem(p);
  641. {$endif}
  642. {$endif}
  643. end;
  644. {$if defined(MORPHOS) or defined(AMIGA)}
  645. {$define AMIGASHELL}
  646. {$endif}
  647. function Shell(const command:ansistring): longint;
  648. { This is already defined in the linux.ppu for linux, need for the *
  649. expansion under linux }
  650. {$ifdef hasunix}
  651. begin
  652. result := Unix.fpsystem(command);
  653. end;
  654. {$else hasunix}
  655. {$ifdef amigashell}
  656. begin
  657. result := ExecuteProcess('',command);
  658. end;
  659. {$else amigashell}
  660. var
  661. comspec : string;
  662. begin
  663. comspec:=GetEnvironmentVariable('COMSPEC');
  664. result := ExecuteProcess(comspec,' /C '+command);
  665. end;
  666. {$endif amigashell}
  667. {$endif hasunix}
  668. {$UNDEF AMIGASHELL}
  669. function is_number_float(d : double) : boolean;
  670. var
  671. bytearray : array[0..7] of byte;
  672. begin
  673. move(d,bytearray,8);
  674. { only 1.1 save, 1.0.x will use always little endian }
  675. {$ifdef FPC_BIG_ENDIAN}
  676. result:=((bytearray[0] and $7f)<>$7f) or ((bytearray[1] and $f0)<>$f0);
  677. {$else FPC_BIG_ENDIAN}
  678. result:=((bytearray[7] and $7f)<>$7f) or ((bytearray[6] and $f0)<>$f0);
  679. {$endif FPC_BIG_ENDIAN}
  680. end;
  681. function get_real_sign(r: bestreal): longint;
  682. var
  683. p: pbyte;
  684. begin
  685. p := pbyte(@r);
  686. {$ifdef CPU_ARM}
  687. inc(p,4);
  688. {$else}
  689. {$ifdef FPC_LITTLE_ENDIAN}
  690. inc(p,sizeof(r)-1);
  691. {$endif}
  692. {$endif}
  693. if (p^ and $80) = 0 then
  694. result := 1
  695. else
  696. result := -1;
  697. end;
  698. function convertdoublerec(d : tdoublerec) : tdoublerec;{$ifdef USEINLINE}inline;{$endif}
  699. {$ifdef CPUARM}
  700. var
  701. i : longint;
  702. begin
  703. for i:=0 to 3 do
  704. begin
  705. result.bytes[i+4]:=d.bytes[i];
  706. result.bytes[i]:=d.bytes[i+4];
  707. end;
  708. {$else CPUARM}
  709. begin
  710. result:=d;
  711. {$endif CPUARM}
  712. end;
  713. { '('D1:'00000000-'D2:'0000-'D3:'0000-'D4:'0000-000000000000)' }
  714. function string2guid(const s: string; var GUID: TGUID): boolean;
  715. function ishexstr(const hs: string): boolean;
  716. var
  717. i: integer;
  718. begin
  719. ishexstr:=false;
  720. for i:=1 to Length(hs) do begin
  721. if not (hs[i] in ['0'..'9','A'..'F','a'..'f']) then
  722. exit;
  723. end;
  724. ishexstr:=true;
  725. end;
  726. function hexstr2longint(const hexs: string): longint;
  727. var
  728. i: integer;
  729. rl: longint;
  730. begin
  731. rl:=0;
  732. for i:=1 to length(hexs) do begin
  733. rl:=rl shl 4;
  734. case hexs[i] of
  735. '0'..'9' : inc(rl,ord(hexs[i])-ord('0'));
  736. 'A'..'F' : inc(rl,ord(hexs[i])-ord('A')+10);
  737. 'a'..'f' : inc(rl,ord(hexs[i])-ord('a')+10);
  738. end
  739. end;
  740. hexstr2longint:=rl;
  741. end;
  742. var
  743. i: integer;
  744. begin
  745. if (Length(s)=38) and (s[1]='{') and (s[38]='}') and
  746. (s[10]='-') and (s[15]='-') and (s[20]='-') and (s[25]='-') and
  747. ishexstr(copy(s,2,8)) and ishexstr(copy(s,11,4)) and
  748. ishexstr(copy(s,16,4)) and ishexstr(copy(s,21,4)) and
  749. ishexstr(copy(s,26,12)) then begin
  750. GUID.D1:=dword(hexstr2longint(copy(s,2,8)));
  751. { these values are arealdy in the correct range (4 chars = word) }
  752. GUID.D2:=word(hexstr2longint(copy(s,11,4)));
  753. GUID.D3:=word(hexstr2longint(copy(s,16,4)));
  754. for i:=0 to 1 do
  755. GUID.D4[i]:=byte(hexstr2longint(copy(s,21+i*2,2)));
  756. for i:=2 to 7 do
  757. GUID.D4[i]:=byte(hexstr2longint(copy(s,22+i*2,2)));
  758. string2guid:=true;
  759. end
  760. else if (length(s)=0) then
  761. begin
  762. FillChar(GUID,SizeOf(GUID),0);
  763. string2guid:=true;
  764. end
  765. else
  766. string2guid:=false;
  767. end;
  768. function guid2string(const GUID: TGUID): string;
  769. begin
  770. guid2string:=
  771. '{'+hexstr(GUID.D1,8)+
  772. '-'+hexstr(GUID.D2,4)+
  773. '-'+hexstr(GUID.D3,4)+
  774. '-'+hexstr(GUID.D4[0],2)+hexstr(GUID.D4[1],2)+
  775. '-'+hexstr(GUID.D4[2],2)+hexstr(GUID.D4[3],2)+
  776. hexstr(GUID.D4[4],2)+hexstr(GUID.D4[5],2)+
  777. hexstr(GUID.D4[6],2)+hexstr(GUID.D4[7],2)+
  778. '}';
  779. end;
  780. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  781. const
  782. DefProcCallName : array[tproccalloption] of string[12] = ('',
  783. 'CDECL',
  784. 'CPPDECL',
  785. 'FAR16',
  786. 'OLDFPCCALL',
  787. '', { internproc }
  788. '', { syscall }
  789. 'PASCAL',
  790. 'REGISTER',
  791. 'SAFECALL',
  792. 'STDCALL',
  793. 'SOFTFLOAT',
  794. 'MWPASCAL'
  795. );
  796. var
  797. t : tproccalloption;
  798. hs : string;
  799. begin
  800. result:=false;
  801. if (s = '') then
  802. exit;
  803. hs:=upper(s);
  804. if (hs = 'DEFAULT') then
  805. begin
  806. a := pocall_default;
  807. result := true;
  808. exit;
  809. end;
  810. for t:=low(tproccalloption) to high(tproccalloption) do
  811. if DefProcCallName[t]=hs then
  812. begin
  813. a:=t;
  814. result:=true;
  815. break;
  816. end;
  817. end;
  818. function Setabitype(const s:string;var a:tabi):boolean;
  819. var
  820. t : tabi;
  821. hs : string;
  822. begin
  823. result:=false;
  824. hs:=Upper(s);
  825. for t:=low(t) to high(t) do
  826. if abi2str[t]=hs then
  827. begin
  828. a:=t;
  829. result:=true;
  830. break;
  831. end;
  832. end;
  833. function Setcputype(const s:string;var a:tcputype):boolean;
  834. var
  835. t : tcputype;
  836. hs : string;
  837. begin
  838. result:=false;
  839. hs:=Upper(s);
  840. for t:=low(tcputype) to high(tcputype) do
  841. if cputypestr[t]=hs then
  842. begin
  843. a:=t;
  844. result:=true;
  845. break;
  846. end;
  847. end;
  848. function SetFpuType(const s:string;var a:tfputype):boolean;
  849. var
  850. t : tfputype;
  851. begin
  852. result:=false;
  853. for t:=low(tfputype) to high(tfputype) do
  854. if fputypestr[t]=s then
  855. begin
  856. a:=t;
  857. result:=true;
  858. break;
  859. end;
  860. end;
  861. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  862. var
  863. tok : string;
  864. vstr : string;
  865. l : longint;
  866. code : integer;
  867. b : talignmentinfo;
  868. begin
  869. UpdateAlignmentStr:=true;
  870. uppervar(s);
  871. fillchar(b,sizeof(b),0);
  872. repeat
  873. tok:=GetToken(s,'=');
  874. if tok='' then
  875. break;
  876. vstr:=GetToken(s,',');
  877. val(vstr,l,code);
  878. if tok='PROC' then
  879. b.procalign:=l
  880. else if tok='JUMP' then
  881. b.jumpalign:=l
  882. else if tok='LOOP' then
  883. b.loopalign:=l
  884. else if tok='CONSTMIN' then
  885. b.constalignmin:=l
  886. else if tok='CONSTMAX' then
  887. b.constalignmax:=l
  888. else if tok='VARMIN' then
  889. b.varalignmin:=l
  890. else if tok='VARMAX' then
  891. b.varalignmax:=l
  892. else if tok='LOCALMIN' then
  893. b.localalignmin:=l
  894. else if tok='LOCALMAX' then
  895. b.localalignmax:=l
  896. else if tok='RECORDMIN' then
  897. b.recordalignmin:=l
  898. else if tok='RECORDMAX' then
  899. b.recordalignmax:=l
  900. else { Error }
  901. UpdateAlignmentStr:=false;
  902. until false;
  903. UpdateAlignment(a,b);
  904. end;
  905. function UpdateOptimizerStr(s:string;var a:toptimizerswitches):boolean;
  906. var
  907. tok : string;
  908. doset,
  909. found : boolean;
  910. opt : toptimizerswitch;
  911. begin
  912. result:=true;
  913. uppervar(s);
  914. repeat
  915. tok:=GetToken(s,',');
  916. if tok='' then
  917. break;
  918. if Copy(tok,1,2)='NO' then
  919. begin
  920. delete(tok,1,2);
  921. doset:=false;
  922. end
  923. else
  924. doset:=true;
  925. found:=false;
  926. for opt:=low(toptimizerswitch) to high(toptimizerswitch) do
  927. begin
  928. if OptimizerSwitchStr[opt]=tok then
  929. begin
  930. found:=true;
  931. break;
  932. end;
  933. end;
  934. if found then
  935. begin
  936. if doset then
  937. include(a,opt)
  938. else
  939. exclude(a,opt);
  940. end
  941. else
  942. result:=false;
  943. until false;
  944. end;
  945. function UpdateDebugStr(s:string;var a:tdebugswitches):boolean;
  946. var
  947. tok : string;
  948. doset,
  949. found : boolean;
  950. opt : tdebugswitch;
  951. begin
  952. result:=true;
  953. uppervar(s);
  954. repeat
  955. tok:=GetToken(s,',');
  956. if tok='' then
  957. break;
  958. if Copy(tok,1,2)='NO' then
  959. begin
  960. delete(tok,1,2);
  961. doset:=false;
  962. end
  963. else
  964. doset:=true;
  965. found:=false;
  966. for opt:=low(tdebugswitch) to high(tdebugswitch) do
  967. begin
  968. if DebugSwitchStr[opt]=tok then
  969. begin
  970. found:=true;
  971. break;
  972. end;
  973. end;
  974. if found then
  975. begin
  976. if doset then
  977. include(a,opt)
  978. else
  979. exclude(a,opt);
  980. end
  981. else
  982. result:=false;
  983. until false;
  984. end;
  985. function IncludeFeature(const s : string) : boolean;
  986. var
  987. i : tfeature;
  988. begin
  989. result:=true;
  990. for i:=low(tfeature) to high(tfeature) do
  991. if s=featurestr[i] then
  992. begin
  993. include(features,i);
  994. exit;
  995. end;
  996. result:=false;
  997. end;
  998. function SetMinFPConstPrec(const s: string; var a: tfloattype) : boolean;
  999. var
  1000. value, error: longint;
  1001. begin
  1002. if (upper(s)='DEFAULT') then
  1003. begin
  1004. a:=s32real;
  1005. result:=true;
  1006. exit;
  1007. end;
  1008. result:=false;
  1009. val(s,value,error);
  1010. if (error<>0) then
  1011. exit;
  1012. case value of
  1013. 32: a:=s32real;
  1014. 64: a:=s64real;
  1015. { adding support for 80 bit here is tricky, since we can't really }
  1016. { check whether the target cpu+OS actually supports it }
  1017. else
  1018. exit;
  1019. end;
  1020. result:=true;
  1021. end;
  1022. function var_align(siz: longint): shortint;
  1023. begin
  1024. siz := size_2_align(siz);
  1025. var_align := used_align(siz,current_settings.alignment.varalignmin,current_settings.alignment.varalignmax);
  1026. end;
  1027. function const_align(siz: longint): shortint;
  1028. begin
  1029. siz := size_2_align(siz);
  1030. const_align := used_align(siz,current_settings.alignment.constalignmin,current_settings.alignment.constalignmax);
  1031. end;
  1032. {$ifdef ARM}
  1033. function is_double_hilo_swapped: boolean;{$ifdef USEINLINE}inline;{$endif}
  1034. begin
  1035. result := (current_settings.fputype in [fpu_fpa,fpu_fpa10,fpu_fpa11]) and
  1036. not(cs_fp_emulation in current_settings.moduleswitches);
  1037. {$ifdef FPC_DOUBLE_HILO_SWAPPED}
  1038. { inverse result if compiler was compiled with swapped hilo already }
  1039. result := not result;
  1040. {$endif FPC_DOUBLE_HILO_SWAPPED}
  1041. end;
  1042. {$endif ARM}
  1043. function floating_point_range_check_error : boolean;
  1044. begin
  1045. result:=((([cs_check_range,cs_check_overflow]*current_settings.localswitches)<>[]) and not
  1046. (m_delphi in current_settings.modeswitches)
  1047. ); // or (cs_ieee_errors in current_settings.localswitches);
  1048. end;
  1049. {****************************************************************************
  1050. Init
  1051. ****************************************************************************}
  1052. {$ifdef unix}
  1053. {$define need_path_search}
  1054. {$endif unix}
  1055. {$ifdef os2}
  1056. {$define need_path_search}
  1057. {$endif os2}
  1058. {$ifdef macos}
  1059. {$define need_path_search}
  1060. {$endif macos}
  1061. procedure get_exepath;
  1062. var
  1063. localExepath : TCmdStr;
  1064. exeName:TCmdStr;
  1065. {$ifdef need_path_search}
  1066. hs1 : TPathStr;
  1067. {$endif need_path_search}
  1068. begin
  1069. localexepath:=GetEnvironmentVariable('PPC_EXEC_PATH');
  1070. if localexepath='' then
  1071. begin
  1072. exeName := FixFileName(system.paramstr(0));
  1073. localexepath := ExtractFilePath(exeName);
  1074. end;
  1075. {$ifdef need_path_search}
  1076. if localexepath='' then
  1077. begin
  1078. hs1 := ExtractFileName(exeName);
  1079. ChangeFileExt(hs1,source_info.exeext);
  1080. {$ifdef macos}
  1081. FindFile(hs1,GetEnvironmentVariable('Commands'),false,localExepath);
  1082. {$else macos}
  1083. FindFile(hs1,GetEnvironmentVariable('PATH'),false,localExepath);
  1084. {$endif macos}
  1085. localExepath:=ExtractFilePath(localExepath);
  1086. end;
  1087. {$endif need_path_search}
  1088. exepath:=FixPath(localExepath,false);
  1089. end;
  1090. procedure DoneGlobals;
  1091. begin
  1092. if assigned(DLLImageBase) then
  1093. StringDispose(DLLImageBase);
  1094. librarysearchpath.Free;
  1095. unitsearchpath.Free;
  1096. objectsearchpath.Free;
  1097. includesearchpath.Free;
  1098. frameworksearchpath.Free;
  1099. LinkLibraryAliases.Free;
  1100. LinkLibraryOrder.Free;
  1101. end;
  1102. procedure InitGlobals;
  1103. begin
  1104. get_exepath;
  1105. { reset globals }
  1106. do_build:=false;
  1107. do_release:=false;
  1108. do_make:=true;
  1109. compile_level:=0;
  1110. DLLsource:=false;
  1111. resolving_forward:=false;
  1112. paratarget:=system_none;
  1113. paratargetasm:=as_none;
  1114. paratargetdbg:=dbg_none;
  1115. { Output }
  1116. OutputFileName:='';
  1117. OutputPrefix:=Nil;
  1118. OutputSuffix:=Nil;
  1119. OutputExeDir:='';
  1120. OutputUnitDir:='';
  1121. { Utils directory }
  1122. utilsdirectory:='';
  1123. utilsprefix:='';
  1124. cshared:=false;
  1125. rlinkpath:='';
  1126. sysrootpath:='';
  1127. { Search Paths }
  1128. librarysearchpath:=TSearchPathList.Create;
  1129. unitsearchpath:=TSearchPathList.Create;
  1130. includesearchpath:=TSearchPathList.Create;
  1131. objectsearchpath:=TSearchPathList.Create;
  1132. frameworksearchpath:=TSearchPathList.Create;
  1133. { Def file }
  1134. usewindowapi:=false;
  1135. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  1136. DescriptionSetExplicity:=false;
  1137. SetPEFlagsSetExplicity:=false;
  1138. ImageBaseSetExplicity:=false;
  1139. MinStackSizeSetExplicity:=false;
  1140. MaxStackSizeSetExplicity:=false;
  1141. dllversion:='';
  1142. dllmajor:=1;
  1143. dllminor:=0;
  1144. dllrevision:=0;
  1145. nwscreenname := '';
  1146. nwthreadname := '';
  1147. nwcopyright := '';
  1148. UseDeffileForExports:=false;
  1149. UseDeffileForExportsSetExplicitly:=false;
  1150. GenerateImportSection:=false;
  1151. RelocSection:=false;
  1152. RelocSectionSetExplicitly:=false;
  1153. LinkTypeSetExplicitly:=false;
  1154. { memory sizes, will be overriden by parameter or default for target
  1155. in options or init_parser }
  1156. stacksize:=0;
  1157. { not initialized yet }
  1158. jmp_buf_size:=-1;
  1159. apptype:=app_cui;
  1160. { Init values }
  1161. init_settings:=default_settings;
  1162. if init_settings.optimizecputype=cpu_none then
  1163. init_settings.optimizecputype:=init_settings.cputype;
  1164. LinkLibraryAliases :=TLinkStrMap.Create;
  1165. LinkLibraryOrder :=TLinkStrMap.Create;
  1166. { enable all features by default }
  1167. features:=[low(Tfeature)..high(Tfeature)];
  1168. end;
  1169. end.