globals.pas 37 KB

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