globals.pas 36 KB

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