globals.pas 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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. exceptblockcounter : integer; { each except block gets a unique number check gotos }
  215. current_exceptblock : integer; { the exceptblock number of the current block (0 if none) }
  216. LinkLibraryAliases : TLinkStrMap;
  217. LinkLibraryOrder : TLinkStrMap;
  218. init_settings,
  219. current_settings : tsettings;
  220. nextlocalswitches : tlocalswitches;
  221. localswitcheschanged : boolean;
  222. { Memory sizes }
  223. heapsize,
  224. stacksize,
  225. jmp_buf_size : longint;
  226. {$Ifdef EXTDEBUG}
  227. { parameter switches }
  228. debugstop : boolean;
  229. {$EndIf EXTDEBUG}
  230. { windows / OS/2 application type }
  231. apptype : tapptype;
  232. features : tfeatures;
  233. const
  234. DLLsource : boolean = false;
  235. DLLImageBase : pshortstring = nil;
  236. { used to set all registers used for each global function
  237. this should dramatically decrease the number of
  238. recompilations needed PM }
  239. simplify_ppu : boolean = true;
  240. Inside_asm_statement : boolean = false;
  241. global_unit_count : word = 0;
  242. { for error info in pp.pas }
  243. parser_current_file : string = '';
  244. {$if defined(m68k) or defined(arm)}
  245. { PalmOS resources }
  246. palmos_applicationname : string = 'FPC Application';
  247. palmos_applicationid : string[4] = 'FPCA';
  248. {$endif defined(m68k) or defined(arm)}
  249. {$ifdef powerpc}
  250. { default calling convention used on MorphOS }
  251. syscall_convention : string = 'LEGACY';
  252. {$endif powerpc}
  253. { default name of the C-style "main" procedure of the library/program }
  254. { (this will be prefixed with the target_info.cprefix) }
  255. defaultmainaliasname = 'main';
  256. mainaliasname : string = defaultmainaliasname;
  257. { by default no local variable trashing }
  258. localvartrashing: longint = -1;
  259. { actual values are defined in ncgutil.pas }
  260. nroftrashvalues = 4;
  261. const
  262. default_settings : TSettings = (
  263. globalswitches : [cs_check_unit_name,cs_link_static];
  264. moduleswitches : [cs_extsyntax,cs_implicit_exceptions];
  265. localswitches : [cs_check_io,cs_typed_const_writable];
  266. modeswitches : fpcmodeswitches;
  267. optimizerswitches : [];
  268. debugswitches : [];
  269. setalloc : 0;
  270. packenum : 4;
  271. alignment : (
  272. procalign : 0;
  273. loopalign : 0;
  274. jumpalign : 0;
  275. constalignmin : 0;
  276. constalignmax : 0;
  277. varalignmin : 0;
  278. varalignmax : 0;
  279. localalignmin : 0;
  280. localalignmax : 0;
  281. recordalignmin : 0;
  282. recordalignmax : 0;
  283. maxCrecordalign : 0;
  284. );
  285. {$ifdef i386}
  286. cputype : cpu_Pentium;
  287. optimizecputype : cpu_Pentium3;
  288. fputype : fpu_x87;
  289. {$endif i386}
  290. {$ifdef m68k}
  291. cputype : cpu_MC68020;
  292. optimizecputype : 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. optimizecputype : cpu_SPARC_V8;
  308. fputype : fpu_hard;
  309. {$endif sparc}
  310. {$ifdef arm}
  311. cputype : cpu_armv3;
  312. optimizecputype : cpu_armv3;
  313. fputype : fpu_fpa;
  314. {$endif arm}
  315. {$ifdef x86_64}
  316. cputype : cpu_athlon64;
  317. optimizecputype : cpu_athlon64;
  318. fputype : fpu_sse64;
  319. {$endif x86_64}
  320. {$ifdef avr}
  321. cputype : cpuinfo.cpu_avr;
  322. optimizecputype : cpuinfo.cpu_avr;
  323. fputype : fpu_none;
  324. {$endif avr}
  325. asmmode : asmmode_standard;
  326. interfacetype : it_interfacecom;
  327. defproccall : pocall_default;
  328. sourcecodepage : '8859-1';
  329. packrecords : 0;
  330. maxfpuregisters : 0;
  331. minfpconstprec : s32real;
  332. );
  333. var
  334. starttime : real;
  335. function getdatestr:string;
  336. function gettimestr:string;
  337. function filetimestring( t : longint) : string;
  338. function getrealtime : real;
  339. procedure DefaultReplacements(var s:ansistring);
  340. function Shell(const command:ansistring): longint;
  341. function GetEnvPChar(const envname:string):pchar;
  342. procedure FreeEnvPChar(p:pchar);
  343. function is_number_float(d : double) : boolean;
  344. { discern +0.0 and -0.0 }
  345. function get_real_sign(r: bestreal): longint;
  346. procedure InitGlobals;
  347. procedure DoneGlobals;
  348. function string2guid(const s: string; var GUID: TGUID): boolean;
  349. function guid2string(const GUID: TGUID): string;
  350. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  351. function Setabitype(const s:string;var a:tabi):boolean;
  352. function Setcputype(const s:string;var a:tcputype):boolean;
  353. function SetFpuType(const s:string;var a:tfputype):boolean;
  354. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  355. function UpdateOptimizerStr(s:string;var a:toptimizerswitches):boolean;
  356. function UpdateDebugStr(s:string;var a:tdebugswitches):boolean;
  357. function IncludeFeature(const s : string) : boolean;
  358. function SetMinFPConstPrec(const s: string; var a: tfloattype) : boolean;
  359. {# Routine to get the required alignment for size of data, which will
  360. be placed in bss segment, according to the current alignment requirements }
  361. function var_align(siz: longint): shortint;
  362. {# Routine to get the required alignment for size of data, which will
  363. be placed in data/const segment, according to the current alignment requirements }
  364. function const_align(siz: longint): shortint;
  365. {$ifdef ARM}
  366. function is_double_hilo_swapped: boolean;{$ifdef USEINLINE}inline;{$endif}
  367. {$endif ARM}
  368. function floating_point_range_check_error : boolean;
  369. implementation
  370. uses
  371. {$ifdef macos}
  372. macutils,
  373. {$endif}
  374. comphook;
  375. {****************************************************************************
  376. TLinkStrMap
  377. ****************************************************************************}
  378. Constructor TLinkStrMap.create;
  379. begin
  380. inherited;
  381. itemcnt:=0;
  382. end;
  383. procedure TLinkStrMap.Add(key:ansistring;value:AnsiString='';weight:longint=LinkMapWeightDefault);
  384. begin
  385. if lookup(key)<>-1 Then
  386. exit;
  387. if itemcnt<=length(fmap) Then
  388. setlength(fmap,itemcnt+10);
  389. fmap[itemcnt].key:=key;
  390. fmap[itemcnt].value:=value;
  391. fmap[itemcnt].weight:=weight;
  392. inc(itemcnt);
  393. end;
  394. function TLinkStrMap.AddDep(keyvalue:String):boolean;
  395. var
  396. i : Longint;
  397. begin
  398. AddDep:=false;
  399. i:=pos('=',keyvalue);
  400. if i=0 then
  401. exit;
  402. Add(Copy(KeyValue,1,i-1),Copy(KeyValue,i+1,length(KeyValue)-i));
  403. AddDep:=True;
  404. end;
  405. function TLinkStrMap.AddWeight(keyvalue:String):boolean;
  406. var
  407. i,j : Longint;
  408. Code : Word;
  409. s : AnsiString;
  410. begin
  411. AddWeight:=false;
  412. i:=pos('=',keyvalue);
  413. if i=0 then
  414. exit;
  415. s:=Copy(KeyValue,i+1,length(KeyValue)-i);
  416. val(s,j,code);
  417. if code=0 Then
  418. begin
  419. Add(Copy(KeyValue,1,i-1),'',j);
  420. AddWeight:=True;
  421. end;
  422. end;
  423. procedure TLinkStrMap.addseries(keys:AnsiString;weight:longint);
  424. var
  425. i,j,k : longint;
  426. begin
  427. k:=length(keys);
  428. i:=1;
  429. while i<=k do
  430. begin
  431. j:=i;
  432. while (i<=k) and (keys[i]<>',') do
  433. inc(i);
  434. add(copy(keys,j,i-j),'',weight);
  435. inc(i);
  436. end;
  437. end;
  438. procedure TLinkStrMap.SetValue(Key:Ansistring;weight:Integer);
  439. var
  440. j : longint;
  441. begin
  442. j:=lookup(key);
  443. if j<>-1 then
  444. fmap[j].weight:=weight;
  445. end;
  446. function TLinkStrMap.find(key:Ansistring):Ansistring;
  447. var
  448. j : longint;
  449. begin
  450. find:='';
  451. j:=lookup(key);
  452. if j<>-1 then
  453. find:=fmap[j].value;
  454. end;
  455. function TLinkStrMap.lookup(key:Ansistring):longint;
  456. var
  457. i : longint;
  458. begin
  459. lookup:=-1;
  460. i:=0;
  461. while (i<itemcnt) and (fmap[i].key<>key) do
  462. inc(i);
  463. if i<>itemcnt then
  464. lookup:=i;
  465. end;
  466. procedure TLinkStrMap.SortOnWeight;
  467. var
  468. i, j : longint;
  469. m : TLinkRec;
  470. begin
  471. if itemcnt <2 then exit;
  472. for i:=0 to itemcnt-1 do
  473. for j:=i+1 to itemcnt-1 do
  474. begin
  475. if fmap[i].weight>fmap[j].weight Then
  476. begin
  477. m:=fmap[i];
  478. fmap[i]:=fmap[j];
  479. fmap[j]:=m;
  480. end;
  481. end;
  482. end;
  483. function TLinkStrMap.getlinkrec(i:longint):TLinkRec;
  484. begin
  485. result:=fmap[i];
  486. end;
  487. procedure TLinkStrMap.Expand(Src:TCmdStrList;Dest:TLinkStrMap);
  488. // expands every thing in Src to Dest for linkorder purposes.
  489. var
  490. r : longint;
  491. LibN : TCmdStr;
  492. begin
  493. while not src.empty do
  494. begin
  495. LibN:=src.getfirst;
  496. r:=lookup (LibN);
  497. if r=-1 then
  498. dest.add(LibN)
  499. else
  500. dest.addseries(fmap[r].value);
  501. end;
  502. end;
  503. procedure TLinkStrMap.UpdateWeights(Weightmap:TLinkStrMap);
  504. var
  505. l,r : longint;
  506. begin
  507. for l := 0 to itemcnt-1 do
  508. begin
  509. r:=weightmap.lookup (fmap[l].key);
  510. if r<>-1 then
  511. fmap[l].weight:=weightmap[r].weight;
  512. end;
  513. end;
  514. {****************************************************************************
  515. Time Handling
  516. ****************************************************************************}
  517. Function L0(l:longint):string;
  518. {
  519. return the string of value l, if l<10 then insert a zero, so
  520. the string is always at least 2 chars '01','02',etc
  521. }
  522. var
  523. s : string;
  524. begin
  525. Str(l,s);
  526. if l<10 then
  527. s:='0'+s;
  528. L0:=s;
  529. end;
  530. function gettimestr:string;
  531. {
  532. get the current time in a string HH:MM:SS
  533. }
  534. var
  535. hour,min,sec,hsec : word;
  536. begin
  537. DecodeTime(Time,hour,min,sec,hsec);
  538. gettimestr:=L0(Hour)+':'+L0(min)+':'+L0(sec);
  539. end;
  540. function getdatestr:string;
  541. {
  542. get the current date in a string YY/MM/DD
  543. }
  544. var
  545. Year,Month,Day: Word;
  546. begin
  547. DecodeDate(Date,year,month,day);
  548. getdatestr:=L0(Year)+'/'+L0(Month)+'/'+L0(Day);
  549. end;
  550. function filetimestring( t : longint) : string;
  551. {
  552. convert dos datetime t to a string YY/MM/DD HH:MM:SS
  553. }
  554. var
  555. DT : TDateTime;
  556. hsec : word;
  557. Year,Month,Day: Word;
  558. hour,min,sec : word;
  559. begin
  560. if t=-1 then
  561. begin
  562. Result := 'Not Found';
  563. exit;
  564. end;
  565. DT := FileDateToDateTime(t);
  566. DecodeTime(DT,hour,min,sec,hsec);
  567. DecodeDate(DT,year,month,day);
  568. Result := L0(Year)+'/'+L0(Month)+'/'+L0(Day)+' '+L0(Hour)+':'+L0(min)+':'+L0(sec);
  569. end;
  570. function getrealtime : real;
  571. var
  572. h,m,s,s1000 : word;
  573. begin
  574. DecodeTime(Time,h,m,s,s1000);
  575. result:=h*3600.0+m*60.0+s+s1000/1000.0;
  576. end;
  577. {****************************************************************************
  578. Default Macro Handling
  579. ****************************************************************************}
  580. procedure DefaultReplacements(var s:ansistring);
  581. begin
  582. { Replace some macros }
  583. Replace(s,'$FPCVERSION',version_string);
  584. Replace(s,'$FPCFULLVERSION',full_version_string);
  585. Replace(s,'$FPCDATE',date_string);
  586. Replace(s,'$FPCCPU',target_cpu_string);
  587. Replace(s,'$FPCOS',target_os_string);
  588. if tf_use_8_3 in Source_Info.Flags then
  589. Replace(s,'$FPCTARGET',target_os_string)
  590. else
  591. Replace(s,'$FPCTARGET',target_full_string);
  592. end;
  593. {****************************************************************************
  594. OS Dependent things
  595. ****************************************************************************}
  596. function GetEnvPChar(const envname:string):pchar;
  597. {$ifdef win32}
  598. var
  599. s : string;
  600. i,len : longint;
  601. hp,p,p2 : pchar;
  602. {$endif}
  603. begin
  604. {$ifdef hasunix}
  605. GetEnvPchar:=BaseUnix.fpGetEnv(envname);
  606. {$define GETENVOK}
  607. {$endif}
  608. {$ifdef win32}
  609. GetEnvPchar:=nil;
  610. p:=GetEnvironmentStrings;
  611. hp:=p;
  612. while hp^<>#0 do
  613. begin
  614. s:=strpas(hp);
  615. i:=pos('=',s);
  616. len:=strlen(hp);
  617. if upper(copy(s,1,i-1))=upper(envname) then
  618. begin
  619. GetMem(p2,len-length(envname));
  620. Move(hp[i],p2^,len-length(envname));
  621. GetEnvPchar:=p2;
  622. break;
  623. end;
  624. { next string entry}
  625. hp:=hp+len+1;
  626. end;
  627. FreeEnvironmentStrings(p);
  628. {$define GETENVOK}
  629. {$endif}
  630. {$ifdef os2}
  631. GetEnvPChar := Dos.GetEnvPChar (EnvName);
  632. {$define GETENVOK}
  633. {$endif}
  634. {$ifdef GETENVOK}
  635. {$undef GETENVOK}
  636. {$else}
  637. GetEnvPchar:=StrPNew(GetEnvironmentVariable(envname));
  638. {$endif}
  639. end;
  640. procedure FreeEnvPChar(p:pchar);
  641. begin
  642. {$ifndef hasunix}
  643. {$ifndef os2}
  644. freemem(p);
  645. {$endif}
  646. {$endif}
  647. end;
  648. {$if defined(MORPHOS) or defined(AMIGA)}
  649. {$define AMIGASHELL}
  650. {$endif}
  651. function Shell(const command:ansistring): longint;
  652. { This is already defined in the linux.ppu for linux, need for the *
  653. expansion under linux }
  654. {$ifdef hasunix}
  655. begin
  656. result := Unix.fpsystem(command);
  657. end;
  658. {$else hasunix}
  659. {$ifdef amigashell}
  660. begin
  661. result := ExecuteProcess('',command);
  662. end;
  663. {$else amigashell}
  664. var
  665. comspec : string;
  666. begin
  667. comspec:=GetEnvironmentVariable('COMSPEC');
  668. result := ExecuteProcess(comspec,' /C '+command);
  669. end;
  670. {$endif amigashell}
  671. {$endif hasunix}
  672. {$UNDEF AMIGASHELL}
  673. function is_number_float(d : double) : boolean;
  674. var
  675. bytearray : array[0..7] of byte;
  676. begin
  677. move(d,bytearray,8);
  678. { only 1.1 save, 1.0.x will use always little endian }
  679. {$ifdef FPC_BIG_ENDIAN}
  680. result:=((bytearray[0] and $7f)<>$7f) or ((bytearray[1] and $f0)<>$f0);
  681. {$else FPC_BIG_ENDIAN}
  682. result:=((bytearray[7] and $7f)<>$7f) or ((bytearray[6] and $f0)<>$f0);
  683. {$endif FPC_BIG_ENDIAN}
  684. end;
  685. function get_real_sign(r: bestreal): longint;
  686. var
  687. p: pbyte;
  688. begin
  689. p := pbyte(@r);
  690. {$ifdef CPU_ARM}
  691. inc(p,4);
  692. {$else}
  693. {$ifdef FPC_LITTLE_ENDIAN}
  694. inc(p,sizeof(r)-1);
  695. {$endif}
  696. {$endif}
  697. if (p^ and $80) = 0 then
  698. result := 1
  699. else
  700. result := -1;
  701. end;
  702. function convertdoublerec(d : tdoublerec) : tdoublerec;{$ifdef USEINLINE}inline;{$endif}
  703. {$ifdef CPUARM}
  704. var
  705. i : longint;
  706. begin
  707. for i:=0 to 3 do
  708. begin
  709. result.bytes[i+4]:=d.bytes[i];
  710. result.bytes[i]:=d.bytes[i+4];
  711. end;
  712. {$else CPUARM}
  713. begin
  714. result:=d;
  715. {$endif CPUARM}
  716. end;
  717. { '('D1:'00000000-'D2:'0000-'D3:'0000-'D4:'0000-000000000000)' }
  718. function string2guid(const s: string; var GUID: TGUID): boolean;
  719. function ishexstr(const hs: string): boolean;
  720. var
  721. i: integer;
  722. begin
  723. ishexstr:=false;
  724. for i:=1 to Length(hs) do begin
  725. if not (hs[i] in ['0'..'9','A'..'F','a'..'f']) then
  726. exit;
  727. end;
  728. ishexstr:=true;
  729. end;
  730. function hexstr2longint(const hexs: string): longint;
  731. var
  732. i: integer;
  733. rl: longint;
  734. begin
  735. rl:=0;
  736. for i:=1 to length(hexs) do begin
  737. rl:=rl shl 4;
  738. case hexs[i] of
  739. '0'..'9' : inc(rl,ord(hexs[i])-ord('0'));
  740. 'A'..'F' : inc(rl,ord(hexs[i])-ord('A')+10);
  741. 'a'..'f' : inc(rl,ord(hexs[i])-ord('a')+10);
  742. end
  743. end;
  744. hexstr2longint:=rl;
  745. end;
  746. var
  747. i: integer;
  748. begin
  749. if (Length(s)=38) and (s[1]='{') and (s[38]='}') and
  750. (s[10]='-') and (s[15]='-') and (s[20]='-') and (s[25]='-') and
  751. ishexstr(copy(s,2,8)) and ishexstr(copy(s,11,4)) and
  752. ishexstr(copy(s,16,4)) and ishexstr(copy(s,21,4)) and
  753. ishexstr(copy(s,26,12)) then begin
  754. GUID.D1:=dword(hexstr2longint(copy(s,2,8)));
  755. { these values are arealdy in the correct range (4 chars = word) }
  756. GUID.D2:=word(hexstr2longint(copy(s,11,4)));
  757. GUID.D3:=word(hexstr2longint(copy(s,16,4)));
  758. for i:=0 to 1 do
  759. GUID.D4[i]:=byte(hexstr2longint(copy(s,21+i*2,2)));
  760. for i:=2 to 7 do
  761. GUID.D4[i]:=byte(hexstr2longint(copy(s,22+i*2,2)));
  762. string2guid:=true;
  763. end
  764. else if (length(s)=0) then
  765. begin
  766. FillChar(GUID,SizeOf(GUID),0);
  767. string2guid:=true;
  768. end
  769. else
  770. string2guid:=false;
  771. end;
  772. function guid2string(const GUID: TGUID): string;
  773. begin
  774. guid2string:=
  775. '{'+hexstr(GUID.D1,8)+
  776. '-'+hexstr(GUID.D2,4)+
  777. '-'+hexstr(GUID.D3,4)+
  778. '-'+hexstr(GUID.D4[0],2)+hexstr(GUID.D4[1],2)+
  779. '-'+hexstr(GUID.D4[2],2)+hexstr(GUID.D4[3],2)+
  780. hexstr(GUID.D4[4],2)+hexstr(GUID.D4[5],2)+
  781. hexstr(GUID.D4[6],2)+hexstr(GUID.D4[7],2)+
  782. '}';
  783. end;
  784. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  785. const
  786. DefProcCallName : array[tproccalloption] of string[12] = ('',
  787. 'CDECL',
  788. 'CPPDECL',
  789. 'FAR16',
  790. 'OLDFPCCALL',
  791. '', { internproc }
  792. '', { syscall }
  793. 'PASCAL',
  794. 'REGISTER',
  795. 'SAFECALL',
  796. 'STDCALL',
  797. 'SOFTFLOAT',
  798. 'MWPASCAL'
  799. );
  800. var
  801. t : tproccalloption;
  802. hs : string;
  803. begin
  804. result:=false;
  805. if (s = '') then
  806. exit;
  807. hs:=upper(s);
  808. if (hs = 'DEFAULT') then
  809. begin
  810. a := pocall_default;
  811. result := true;
  812. exit;
  813. end;
  814. for t:=low(tproccalloption) to high(tproccalloption) do
  815. if DefProcCallName[t]=hs then
  816. begin
  817. a:=t;
  818. result:=true;
  819. break;
  820. end;
  821. end;
  822. function Setabitype(const s:string;var a:tabi):boolean;
  823. var
  824. t : tabi;
  825. hs : string;
  826. begin
  827. result:=false;
  828. hs:=Upper(s);
  829. for t:=low(t) to high(t) do
  830. if abi2str[t]=hs then
  831. begin
  832. a:=t;
  833. result:=true;
  834. break;
  835. end;
  836. end;
  837. function Setcputype(const s:string;var a:tcputype):boolean;
  838. var
  839. t : tcputype;
  840. hs : string;
  841. begin
  842. result:=false;
  843. hs:=Upper(s);
  844. for t:=low(tcputype) to high(tcputype) do
  845. if cputypestr[t]=hs then
  846. begin
  847. a:=t;
  848. result:=true;
  849. break;
  850. end;
  851. end;
  852. function SetFpuType(const s:string;var a:tfputype):boolean;
  853. var
  854. t : tfputype;
  855. begin
  856. result:=false;
  857. for t:=low(tfputype) to high(tfputype) do
  858. if fputypestr[t]=s then
  859. begin
  860. a:=t;
  861. result:=true;
  862. break;
  863. end;
  864. end;
  865. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  866. var
  867. tok : string;
  868. vstr : string;
  869. l : longint;
  870. code : integer;
  871. b : talignmentinfo;
  872. begin
  873. UpdateAlignmentStr:=true;
  874. uppervar(s);
  875. fillchar(b,sizeof(b),0);
  876. repeat
  877. tok:=GetToken(s,'=');
  878. if tok='' then
  879. break;
  880. vstr:=GetToken(s,',');
  881. val(vstr,l,code);
  882. if tok='PROC' then
  883. b.procalign:=l
  884. else if tok='JUMP' then
  885. b.jumpalign:=l
  886. else if tok='LOOP' then
  887. b.loopalign:=l
  888. else if tok='CONSTMIN' then
  889. b.constalignmin:=l
  890. else if tok='CONSTMAX' then
  891. b.constalignmax:=l
  892. else if tok='VARMIN' then
  893. b.varalignmin:=l
  894. else if tok='VARMAX' then
  895. b.varalignmax:=l
  896. else if tok='LOCALMIN' then
  897. b.localalignmin:=l
  898. else if tok='LOCALMAX' then
  899. b.localalignmax:=l
  900. else if tok='RECORDMIN' then
  901. b.recordalignmin:=l
  902. else if tok='RECORDMAX' then
  903. b.recordalignmax:=l
  904. else { Error }
  905. UpdateAlignmentStr:=false;
  906. until false;
  907. UpdateAlignment(a,b);
  908. end;
  909. function UpdateOptimizerStr(s:string;var a:toptimizerswitches):boolean;
  910. var
  911. tok : string;
  912. doset,
  913. found : boolean;
  914. opt : toptimizerswitch;
  915. begin
  916. result:=true;
  917. uppervar(s);
  918. repeat
  919. tok:=GetToken(s,',');
  920. if tok='' then
  921. break;
  922. if Copy(tok,1,2)='NO' then
  923. begin
  924. delete(tok,1,2);
  925. doset:=false;
  926. end
  927. else
  928. doset:=true;
  929. found:=false;
  930. for opt:=low(toptimizerswitch) to high(toptimizerswitch) do
  931. begin
  932. if OptimizerSwitchStr[opt]=tok then
  933. begin
  934. found:=true;
  935. break;
  936. end;
  937. end;
  938. if found then
  939. begin
  940. if doset then
  941. include(a,opt)
  942. else
  943. exclude(a,opt);
  944. end
  945. else
  946. result:=false;
  947. until false;
  948. end;
  949. function UpdateDebugStr(s:string;var a:tdebugswitches):boolean;
  950. var
  951. tok : string;
  952. doset,
  953. found : boolean;
  954. opt : tdebugswitch;
  955. begin
  956. result:=true;
  957. uppervar(s);
  958. repeat
  959. tok:=GetToken(s,',');
  960. if tok='' then
  961. break;
  962. if Copy(tok,1,2)='NO' then
  963. begin
  964. delete(tok,1,2);
  965. doset:=false;
  966. end
  967. else
  968. doset:=true;
  969. found:=false;
  970. for opt:=low(tdebugswitch) to high(tdebugswitch) do
  971. begin
  972. if DebugSwitchStr[opt]=tok then
  973. begin
  974. found:=true;
  975. break;
  976. end;
  977. end;
  978. if found then
  979. begin
  980. if doset then
  981. include(a,opt)
  982. else
  983. exclude(a,opt);
  984. end
  985. else
  986. result:=false;
  987. until false;
  988. end;
  989. function IncludeFeature(const s : string) : boolean;
  990. var
  991. i : tfeature;
  992. begin
  993. result:=true;
  994. for i:=low(tfeature) to high(tfeature) do
  995. if s=featurestr[i] then
  996. begin
  997. include(features,i);
  998. exit;
  999. end;
  1000. result:=false;
  1001. end;
  1002. function SetMinFPConstPrec(const s: string; var a: tfloattype) : boolean;
  1003. var
  1004. value, error: longint;
  1005. begin
  1006. if (upper(s)='DEFAULT') then
  1007. begin
  1008. a:=s32real;
  1009. result:=true;
  1010. exit;
  1011. end;
  1012. result:=false;
  1013. val(s,value,error);
  1014. if (error<>0) then
  1015. exit;
  1016. case value of
  1017. 32: a:=s32real;
  1018. 64: a:=s64real;
  1019. { adding support for 80 bit here is tricky, since we can't really }
  1020. { check whether the target cpu+OS actually supports it }
  1021. else
  1022. exit;
  1023. end;
  1024. result:=true;
  1025. end;
  1026. function var_align(siz: longint): shortint;
  1027. begin
  1028. siz := size_2_align(siz);
  1029. var_align := used_align(siz,current_settings.alignment.varalignmin,current_settings.alignment.varalignmax);
  1030. end;
  1031. function const_align(siz: longint): shortint;
  1032. begin
  1033. siz := size_2_align(siz);
  1034. const_align := used_align(siz,current_settings.alignment.constalignmin,current_settings.alignment.constalignmax);
  1035. end;
  1036. {$ifdef ARM}
  1037. function is_double_hilo_swapped: boolean;{$ifdef USEINLINE}inline;{$endif}
  1038. begin
  1039. result := (current_settings.fputype in [fpu_fpa,fpu_fpa10,fpu_fpa11]) and
  1040. not(cs_fp_emulation in current_settings.moduleswitches);
  1041. {$ifdef FPC_DOUBLE_HILO_SWAPPED}
  1042. { inverse result if compiler was compiled with swapped hilo already }
  1043. result := not result;
  1044. {$endif FPC_DOUBLE_HILO_SWAPPED}
  1045. end;
  1046. {$endif ARM}
  1047. function floating_point_range_check_error : boolean;
  1048. begin
  1049. result:=((([cs_check_range,cs_check_overflow]*current_settings.localswitches)<>[]) and not
  1050. (m_delphi in current_settings.modeswitches)
  1051. ); // or (cs_ieee_errors in current_settings.localswitches);
  1052. end;
  1053. {****************************************************************************
  1054. Init
  1055. ****************************************************************************}
  1056. {$ifdef unix}
  1057. {$define need_path_search}
  1058. {$endif unix}
  1059. {$ifdef os2}
  1060. {$define need_path_search}
  1061. {$endif os2}
  1062. {$ifdef macos}
  1063. {$define need_path_search}
  1064. {$endif macos}
  1065. procedure get_exepath;
  1066. var
  1067. localExepath : TCmdStr;
  1068. exeName:TCmdStr;
  1069. {$ifdef need_path_search}
  1070. hs1 : TPathStr;
  1071. {$endif need_path_search}
  1072. begin
  1073. localexepath:=GetEnvironmentVariable('PPC_EXEC_PATH');
  1074. if localexepath='' then
  1075. begin
  1076. exeName := FixFileName(system.paramstr(0));
  1077. localexepath := ExtractFilePath(exeName);
  1078. end;
  1079. {$ifdef need_path_search}
  1080. if localexepath='' then
  1081. begin
  1082. hs1 := ExtractFileName(exeName);
  1083. ChangeFileExt(hs1,source_info.exeext);
  1084. {$ifdef macos}
  1085. FindFile(hs1,GetEnvironmentVariable('Commands'),false,localExepath);
  1086. {$else macos}
  1087. FindFile(hs1,GetEnvironmentVariable('PATH'),false,localExepath);
  1088. {$endif macos}
  1089. localExepath:=ExtractFilePath(localExepath);
  1090. end;
  1091. {$endif need_path_search}
  1092. exepath:=FixPath(localExepath,false);
  1093. end;
  1094. procedure DoneGlobals;
  1095. begin
  1096. if assigned(DLLImageBase) then
  1097. StringDispose(DLLImageBase);
  1098. librarysearchpath.Free;
  1099. unitsearchpath.Free;
  1100. objectsearchpath.Free;
  1101. includesearchpath.Free;
  1102. frameworksearchpath.Free;
  1103. LinkLibraryAliases.Free;
  1104. LinkLibraryOrder.Free;
  1105. end;
  1106. procedure InitGlobals;
  1107. begin
  1108. get_exepath;
  1109. { reset globals }
  1110. do_build:=false;
  1111. do_release:=false;
  1112. do_make:=true;
  1113. compile_level:=0;
  1114. DLLsource:=false;
  1115. paratarget:=system_none;
  1116. paratargetasm:=as_none;
  1117. paratargetdbg:=dbg_none;
  1118. { Output }
  1119. OutputFileName:='';
  1120. OutputPrefix:=Nil;
  1121. OutputSuffix:=Nil;
  1122. OutputExeDir:='';
  1123. OutputUnitDir:='';
  1124. { Utils directory }
  1125. utilsdirectory:='';
  1126. utilsprefix:='';
  1127. cshared:=false;
  1128. rlinkpath:='';
  1129. sysrootpath:='';
  1130. { Search Paths }
  1131. librarysearchpath:=TSearchPathList.Create;
  1132. unitsearchpath:=TSearchPathList.Create;
  1133. includesearchpath:=TSearchPathList.Create;
  1134. objectsearchpath:=TSearchPathList.Create;
  1135. frameworksearchpath:=TSearchPathList.Create;
  1136. { Def file }
  1137. usewindowapi:=false;
  1138. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  1139. DescriptionSetExplicity:=false;
  1140. SetPEFlagsSetExplicity:=false;
  1141. ImageBaseSetExplicity:=false;
  1142. MinStackSizeSetExplicity:=false;
  1143. MaxStackSizeSetExplicity:=false;
  1144. dllversion:='';
  1145. dllmajor:=1;
  1146. dllminor:=0;
  1147. dllrevision:=0;
  1148. nwscreenname := '';
  1149. nwthreadname := '';
  1150. nwcopyright := '';
  1151. UseDeffileForExports:=false;
  1152. UseDeffileForExportsSetExplicitly:=false;
  1153. GenerateImportSection:=false;
  1154. RelocSection:=false;
  1155. RelocSectionSetExplicitly:=false;
  1156. LinkTypeSetExplicitly:=false;
  1157. { memory sizes, will be overriden by parameter or default for target
  1158. in options or init_parser }
  1159. stacksize:=0;
  1160. { not initialized yet }
  1161. jmp_buf_size:=-1;
  1162. apptype:=app_cui;
  1163. { Init values }
  1164. init_settings:=default_settings;
  1165. if init_settings.optimizecputype=cpu_none then
  1166. init_settings.optimizecputype:=init_settings.cputype;
  1167. LinkLibraryAliases :=TLinkStrMap.Create;
  1168. LinkLibraryOrder :=TLinkStrMap.Create;
  1169. { enable all features by default }
  1170. features:=[low(Tfeature)..high(Tfeature)];
  1171. end;
  1172. end.