globals.pas 37 KB

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