globals.pas 34 KB

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