globals.pas 36 KB

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