globals.pas 36 KB

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