globals.pas 34 KB

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