globals.pas 34 KB

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