globals.pas 37 KB

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