globals.pas 37 KB

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