globals.pas 34 KB

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