2
0

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