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