globals.pas 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements some support functions and global variables
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit globals;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. {$ifdef win32}
  22. windows,
  23. {$endif}
  24. {$ifdef os2}
  25. dos,
  26. {$endif os2}
  27. {$ifdef hasunix}
  28. Baseunix,unix,
  29. {$endif}
  30. {$IFNDEF USE_FAKE_SYSUTILS}
  31. sysutils,
  32. {$ELSE}
  33. fksysutl,
  34. {$ENDIF}
  35. { comphook pulls in sysutils anyways }
  36. cutils,cclasses,cfileutl,
  37. cpuinfo,
  38. globtype,version,systems;
  39. const
  40. delphimodeswitches : tmodeswitches=
  41. [m_delphi,m_all,m_class,m_objpas,m_result,m_string_pchar,
  42. m_pointer_2_procedure,m_autoderef,m_tp_procvar,m_initfinal,m_default_ansistring,
  43. m_out,m_default_para,m_duplicate_names,m_hintdirective,m_add_pointer,
  44. m_property,m_default_inline,m_except];
  45. fpcmodeswitches : tmodeswitches=
  46. [m_fpc,m_all,m_string_pchar,m_nested_comment,m_repeat_forward,
  47. m_cvar_support,m_initfinal,m_add_pointer,m_hintdirective,
  48. m_property,m_default_inline];
  49. objfpcmodeswitches : tmodeswitches=
  50. [m_objfpc,m_fpc,m_all,m_class,m_objpas,m_result,m_string_pchar,m_nested_comment,
  51. m_repeat_forward,m_cvar_support,m_initfinal,m_add_pointer,m_out,m_default_para,m_hintdirective,
  52. m_property,m_default_inline,m_except];
  53. tpmodeswitches : tmodeswitches=
  54. [m_tp7,m_all,m_tp_procvar,m_duplicate_names];
  55. {$ifdef gpc_mode}
  56. gpcmodeswitches : tmodeswitches=
  57. [m_gpc,m_all,m_tp_procvar];
  58. {$endif}
  59. macmodeswitches : tmodeswitches=
  60. [m_mac,m_all,m_result,m_cvar_support,m_mac_procvar];
  61. { maximum nesting of routines }
  62. maxnesting = 32;
  63. { Filenames and extensions }
  64. sourceext = '.pp';
  65. pasext = '.pas';
  66. pext = '.p';
  67. treelogfilename = 'tree.log';
  68. {$if defined(CPUARM) and defined(FPUFPA)}
  69. MathQNaN : tdoublerec = (bytes : (0,0,252,255,0,0,0,0));
  70. MathInf : tdoublerec = (bytes : (0,0,240,127,0,0,0,0));
  71. MathNegInf : tdoublerec = (bytes : (0,0,240,255,0,0,0,0));
  72. MathPi : tdoublerec = (bytes : (251,33,9,64,24,45,68,84));
  73. {$else}
  74. {$ifdef FPC_LITTLE_ENDIAN}
  75. MathQNaN : tdoublerec = (bytes : (0,0,0,0,0,0,252,255));
  76. MathInf : tdoublerec = (bytes : (0,0,0,0,0,0,240,127));
  77. MathNegInf : tdoublerec = (bytes : (0,0,0,0,0,0,240,255));
  78. MathPi : tdoublerec = (bytes : (24,45,68,84,251,33,9,64));
  79. MathPiExtended : textendedrec = (bytes : (53,194,104,33,162,218,15,201,0,64));
  80. {$else FPC_LITTLE_ENDIAN}
  81. MathQNaN : tdoublerec = (bytes : (255,252,0,0,0,0,0,0));
  82. MathInf : tdoublerec = (bytes : (127,240,0,0,0,0,0,0));
  83. MathNegInf : tdoublerec = (bytes : (255,240,0,0,0,0,0,0));
  84. MathPi : tdoublerec = (bytes : (64,9,33,251,84,68,45,24));
  85. MathPiExtended : textendedrec = (bytes : (64,0,201,15,218,162,33,104,194,53));
  86. {$endif FPC_LITTLE_ENDIAN}
  87. {$endif}
  88. type
  89. tcodepagestring = string[20];
  90. tsettings = record
  91. globalswitches : tglobalswitches;
  92. moduleswitches : tmoduleswitches;
  93. localswitches : tlocalswitches;
  94. modeswitches : tmodeswitches;
  95. optimizerswitches : toptimizerswitches;
  96. debugswitches : tdebugswitches;
  97. { 0: old behaviour for sets <=256 elements
  98. >0: round to this size }
  99. setalloc,
  100. packenum : shortint;
  101. alignment : talignmentinfo;
  102. cputype,
  103. optimizecputype : tcputype;
  104. fputype : tfputype;
  105. asmmode : tasmmode;
  106. interfacetype : tinterfacetypes;
  107. defproccall : tproccalloption;
  108. sourcecodepage : tcodepagestring;
  109. packrecords : shortint;
  110. maxfpuregisters : shortint;
  111. minfpconstprec : tfloattype;
  112. end;
  113. const
  114. LinkMapWeightDefault = 1000;
  115. type
  116. TLinkRec = record
  117. Key : AnsiString;
  118. Value : AnsiString; // key expands to valuelist "value"
  119. Weight: longint;
  120. end;
  121. TLinkStrMap = class
  122. private
  123. itemcnt : longint;
  124. fmap : Array Of TLinkRec;
  125. function Lookup(key:Ansistring):longint;
  126. function getlinkrec(i:longint):TLinkRec;
  127. public
  128. procedure Add(key:ansistring;value:AnsiString='';weight:longint=LinkMapWeightDefault);
  129. procedure addseries(keys:AnsiString;weight:longint=LinkMapWeightDefault);
  130. function AddDep(keyvalue:String):boolean;
  131. function AddWeight(keyvalue:String):boolean;
  132. procedure SetValue(key:AnsiString;Weight:Integer);
  133. procedure SortonWeight;
  134. function Find(key:AnsiString):AnsiString;
  135. procedure Expand(src:TCmdStrList;dest: TLinkStrMap);
  136. procedure UpdateWeights(Weightmap:TLinkStrMap);
  137. constructor Create;
  138. property count : longint read itemcnt;
  139. property items[I:longint]:TLinkRec read getlinkrec; default;
  140. end;
  141. var
  142. { specified inputfile }
  143. inputfilepath : string;
  144. inputfilename : string;
  145. { specified outputfile with -o parameter }
  146. outputfilename : string;
  147. outputprefix : pshortstring;
  148. outputsuffix : pshortstring;
  149. { specified with -FE or -FU }
  150. outputexedir : TPathStr;
  151. outputunitdir : TPathStr;
  152. { things specified with parameters }
  153. paratarget : tsystem;
  154. paratargetdbg : tdbg;
  155. paratargetasm : tasm;
  156. paralinkoptions : TCmdStr;
  157. paradynamiclinker : string;
  158. paraprintnodetree : byte;
  159. parapreprocess : boolean;
  160. printnodefile : text;
  161. { typical cross compiling params}
  162. { directory where the utils can be found (options -FD) }
  163. utilsdirectory : TPathStr;
  164. { targetname specific prefix used by these utils (options -XP<path>) }
  165. utilsprefix : TCmdStr;
  166. cshared : boolean; { pass --shared to ld to link C libs shared}
  167. Dontlinkstdlibpath: Boolean; { Don't add std paths to linkpath}
  168. rlinkpath : TCmdStr; { rpath-link linkdir override}
  169. { some flags for global compiler switches }
  170. do_build,
  171. do_release,
  172. do_make : boolean;
  173. { path for searching units, different paths can be seperated by ; }
  174. exepath : TPathStr; { Path to ppc }
  175. librarysearchpath,
  176. unitsearchpath,
  177. objectsearchpath,
  178. includesearchpath,
  179. frameworksearchpath : TSearchPathList;
  180. autoloadunits : string;
  181. { linking }
  182. usewindowapi : boolean;
  183. description : string;
  184. SetPEFlagsSetExplicity,
  185. ImageBaseSetExplicity,
  186. MinStackSizeSetExplicity,
  187. MaxStackSizeSetExplicity,
  188. DescriptionSetExplicity : boolean;
  189. dllversion : string;
  190. dllmajor,
  191. dllminor,
  192. dllrevision : word; { revision only for netware }
  193. { win pe }
  194. peflags : longint;
  195. minstacksize,
  196. maxstacksize,
  197. imagebase : aword;
  198. UseDeffileForExports : boolean;
  199. UseDeffileForExportsSetExplicitly : boolean;
  200. GenerateImportSection,
  201. GenerateImportSectionSetExplicitly,
  202. RelocSection : boolean;
  203. RelocSectionSetExplicitly : boolean;
  204. LinkTypeSetExplicitly : boolean;
  205. current_tokenpos, { position of the last token }
  206. current_filepos : tfileposinfo; { current position }
  207. nwscreenname : string;
  208. nwthreadname : string;
  209. nwcopyright : string;
  210. codegenerror : boolean; { true if there is an error reported }
  211. block_type : tblock_type; { type of currently parsed block }
  212. compile_level : word;
  213. resolving_forward : boolean; { used to add forward reference as second ref }
  214. exceptblockcounter : integer; { each except block gets a unique number check gotos }
  215. aktexceptblock : integer; { the exceptblock number of the current block (0 if none) }
  216. LinkLibraryAliases : TLinkStrMap;
  217. LinkLibraryOrder : TLinkStrMap;
  218. init_settings,
  219. current_settings : tsettings;
  220. nextlocalswitches : tlocalswitches;
  221. localswitcheschanged : boolean;
  222. { Memory sizes }
  223. heapsize,
  224. stacksize,
  225. jmp_buf_size : longint;
  226. {$Ifdef EXTDEBUG}
  227. { parameter switches }
  228. debugstop : boolean;
  229. {$EndIf EXTDEBUG}
  230. { windows / OS/2 application type }
  231. apptype : tapptype;
  232. features : tfeatures;
  233. const
  234. DLLsource : boolean = false;
  235. DLLImageBase : pshortstring = nil;
  236. { used to set all registers used for each global function
  237. this should dramatically decrease the number of
  238. recompilations needed PM }
  239. simplify_ppu : boolean = true;
  240. Inside_asm_statement : boolean = false;
  241. global_unit_count : word = 0;
  242. { for error info in pp.pas }
  243. parser_current_file : string = '';
  244. {$if defined(m68k) or defined(arm)}
  245. { PalmOS resources }
  246. palmos_applicationname : string = 'FPC Application';
  247. palmos_applicationid : string[4] = 'FPCA';
  248. {$endif defined(m68k) or defined(arm)}
  249. {$ifdef powerpc}
  250. { default calling convention used on MorphOS }
  251. syscall_convention : string = 'LEGACY';
  252. {$endif powerpc}
  253. { default name of the C-style "main" procedure of the library/program }
  254. { (this will be prefixed with the target_info.cprefix) }
  255. mainaliasname : string = 'main';
  256. { by default no local variable trashing }
  257. localvartrashing: longint = -1;
  258. { actual values are defined in ncgutil.pas }
  259. nroftrashvalues = 4;
  260. var
  261. starttime : real;
  262. function getdatestr:string;
  263. function gettimestr:string;
  264. function filetimestring( t : longint) : string;
  265. function getrealtime : real;
  266. procedure DefaultReplacements(var s:ansistring);
  267. function Shell(const command:ansistring): longint;
  268. function GetEnvPChar(const envname:string):pchar;
  269. procedure FreeEnvPChar(p:pchar);
  270. function is_number_float(d : double) : boolean;
  271. { discern +0.0 and -0.0 }
  272. function get_real_sign(r: bestreal): longint;
  273. procedure InitGlobals;
  274. procedure DoneGlobals;
  275. function string2guid(const s: string; var GUID: TGUID): boolean;
  276. function guid2string(const GUID: TGUID): string;
  277. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  278. function Setcputype(const s:string;var a:tcputype):boolean;
  279. function SetFpuType(const s:string;var a:tfputype):boolean;
  280. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  281. function UpdateOptimizerStr(s:string;var a:toptimizerswitches):boolean;
  282. function UpdateDebugStr(s:string;var a:tdebugswitches):boolean;
  283. function IncludeFeature(const s : string) : boolean;
  284. function SetMinFPConstPrec(const s: string; var a: tfloattype) : boolean;
  285. {# Routine to get the required alignment for size of data, which will
  286. be placed in bss segment, according to the current alignment requirements }
  287. function var_align(siz: longint): shortint;
  288. {# Routine to get the required alignment for size of data, which will
  289. be placed in data/const segment, according to the current alignment requirements }
  290. function const_align(siz: longint): shortint;
  291. {$ifdef ARM}
  292. function is_double_hilo_swapped: boolean;{$ifdef USEINLINE}inline;{$endif}
  293. {$endif ARM}
  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. 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.fpsystem(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. begin
  694. guid2string:=
  695. '{'+hexstr(GUID.D1,8)+
  696. '-'+hexstr(GUID.D2,4)+
  697. '-'+hexstr(GUID.D3,4)+
  698. '-'+hexstr(GUID.D4[0],2)+hexstr(GUID.D4[1],2)+
  699. '-'+hexstr(GUID.D4[2],2)+hexstr(GUID.D4[3],2)+
  700. hexstr(GUID.D4[4],2)+hexstr(GUID.D4[5],2)+
  701. hexstr(GUID.D4[6],2)+hexstr(GUID.D4[7],2)+
  702. '}';
  703. end;
  704. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  705. const
  706. DefProcCallName : array[tproccalloption] of string[12] = ('',
  707. 'CDECL',
  708. 'CPPDECL',
  709. 'FAR16',
  710. 'OLDFPCCALL',
  711. '', { internproc }
  712. '', { syscall }
  713. 'PASCAL',
  714. 'REGISTER',
  715. 'SAFECALL',
  716. 'STDCALL',
  717. 'SOFTFLOAT',
  718. 'MWPASCAL'
  719. );
  720. var
  721. t : tproccalloption;
  722. hs : string;
  723. begin
  724. result:=false;
  725. if (s = '') then
  726. exit;
  727. hs:=upper(s);
  728. if (hs = 'DEFAULT') then
  729. begin
  730. a := pocall_default;
  731. result := true;
  732. exit;
  733. end;
  734. for t:=low(tproccalloption) to high(tproccalloption) do
  735. if DefProcCallName[t]=hs then
  736. begin
  737. a:=t;
  738. result:=true;
  739. break;
  740. end;
  741. end;
  742. function Setcputype(const s:string;var a:tcputype):boolean;
  743. var
  744. t : tcputype;
  745. hs : string;
  746. begin
  747. result:=false;
  748. hs:=Upper(s);
  749. for t:=low(tcputype) to high(tcputype) do
  750. if cputypestr[t]=hs then
  751. begin
  752. a:=t;
  753. result:=true;
  754. break;
  755. end;
  756. end;
  757. function SetFpuType(const s:string;var a:tfputype):boolean;
  758. var
  759. t : tfputype;
  760. begin
  761. result:=false;
  762. for t:=low(tfputype) to high(tfputype) do
  763. if fputypestr[t]=s then
  764. begin
  765. a:=t;
  766. result:=true;
  767. break;
  768. end;
  769. end;
  770. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  771. var
  772. tok : string;
  773. vstr : string;
  774. l : longint;
  775. code : integer;
  776. b : talignmentinfo;
  777. begin
  778. UpdateAlignmentStr:=true;
  779. uppervar(s);
  780. fillchar(b,sizeof(b),0);
  781. repeat
  782. tok:=GetToken(s,'=');
  783. if tok='' then
  784. break;
  785. vstr:=GetToken(s,',');
  786. val(vstr,l,code);
  787. if tok='PROC' then
  788. b.procalign:=l
  789. else if tok='JUMP' then
  790. b.jumpalign:=l
  791. else if tok='LOOP' then
  792. b.loopalign:=l
  793. else if tok='CONSTMIN' then
  794. b.constalignmin:=l
  795. else if tok='CONSTMAX' then
  796. b.constalignmax:=l
  797. else if tok='VARMIN' then
  798. b.varalignmin:=l
  799. else if tok='VARMAX' then
  800. b.varalignmax:=l
  801. else if tok='LOCALMIN' then
  802. b.localalignmin:=l
  803. else if tok='LOCALMAX' then
  804. b.localalignmax:=l
  805. else if tok='RECORDMIN' then
  806. b.recordalignmin:=l
  807. else if tok='RECORDMAX' then
  808. b.recordalignmax:=l
  809. else { Error }
  810. UpdateAlignmentStr:=false;
  811. until false;
  812. UpdateAlignment(a,b);
  813. end;
  814. function UpdateOptimizerStr(s:string;var a:toptimizerswitches):boolean;
  815. var
  816. tok : string;
  817. doset,
  818. found : boolean;
  819. opt : toptimizerswitch;
  820. begin
  821. result:=true;
  822. uppervar(s);
  823. repeat
  824. tok:=GetToken(s,',');
  825. if tok='' then
  826. break;
  827. if Copy(tok,1,2)='NO' then
  828. begin
  829. delete(tok,1,2);
  830. doset:=false;
  831. end
  832. else
  833. doset:=true;
  834. found:=false;
  835. for opt:=low(toptimizerswitch) to high(toptimizerswitch) do
  836. begin
  837. if OptimizerSwitchStr[opt]=tok then
  838. begin
  839. found:=true;
  840. break;
  841. end;
  842. end;
  843. if found then
  844. begin
  845. if doset then
  846. include(a,opt)
  847. else
  848. exclude(a,opt);
  849. end
  850. else
  851. result:=false;
  852. until false;
  853. end;
  854. function UpdateDebugStr(s:string;var a:tdebugswitches):boolean;
  855. var
  856. tok : string;
  857. doset,
  858. found : boolean;
  859. opt : tdebugswitch;
  860. begin
  861. result:=true;
  862. uppervar(s);
  863. repeat
  864. tok:=GetToken(s,',');
  865. if tok='' then
  866. break;
  867. if Copy(tok,1,2)='NO' then
  868. begin
  869. delete(tok,1,2);
  870. doset:=false;
  871. end
  872. else
  873. doset:=true;
  874. found:=false;
  875. for opt:=low(tdebugswitch) to high(tdebugswitch) do
  876. begin
  877. if DebugSwitchStr[opt]=tok then
  878. begin
  879. found:=true;
  880. break;
  881. end;
  882. end;
  883. if found then
  884. begin
  885. if doset then
  886. include(a,opt)
  887. else
  888. exclude(a,opt);
  889. end
  890. else
  891. result:=false;
  892. until false;
  893. end;
  894. function IncludeFeature(const s : string) : boolean;
  895. var
  896. i : tfeature;
  897. begin
  898. result:=true;
  899. for i:=low(tfeature) to high(tfeature) do
  900. if s=featurestr[i] then
  901. begin
  902. include(features,i);
  903. exit;
  904. end;
  905. result:=false;
  906. end;
  907. function SetMinFPConstPrec(const s: string; var a: tfloattype) : boolean;
  908. var
  909. value, error: longint;
  910. begin
  911. if (upper(s)='DEFAULT') then
  912. begin
  913. a:=s32real;
  914. result:=true;
  915. exit;
  916. end;
  917. result:=false;
  918. val(s,value,error);
  919. if (error<>0) then
  920. exit;
  921. case value of
  922. 32: a:=s32real;
  923. 64: a:=s64real;
  924. { adding support for 80 bit here is tricky, since we can't really }
  925. { check whether the target cpu+OS actually supports it }
  926. else
  927. exit;
  928. end;
  929. result:=true;
  930. end;
  931. function var_align(siz: longint): shortint;
  932. begin
  933. siz := size_2_align(siz);
  934. var_align := used_align(siz,current_settings.alignment.varalignmin,current_settings.alignment.varalignmax);
  935. end;
  936. function const_align(siz: longint): shortint;
  937. begin
  938. siz := size_2_align(siz);
  939. const_align := used_align(siz,current_settings.alignment.constalignmin,current_settings.alignment.constalignmax);
  940. end;
  941. {$ifdef ARM}
  942. function is_double_hilo_swapped: boolean;{$ifdef USEINLINE}inline;{$endif}
  943. begin
  944. result := (current_settings.fputype in [fpu_fpa,fpu_fpa10,fpu_fpa11]) and
  945. not(cs_fp_emulation in current_settings.moduleswitches);
  946. {$ifdef FPC_DOUBLE_HILO_SWAPPED}
  947. { inverse result if compiler was compiled with swapped hilo already }
  948. result := not result;
  949. {$endif FPC_DOUBLE_HILO_SWAPPED}
  950. end;
  951. {$endif ARM}
  952. {****************************************************************************
  953. Init
  954. ****************************************************************************}
  955. {$ifdef unix}
  956. {$define need_path_search}
  957. {$endif unix}
  958. {$ifdef os2}
  959. {$define need_path_search}
  960. {$endif os2}
  961. {$ifdef macos}
  962. {$define need_path_search}
  963. {$endif macos}
  964. procedure get_exepath;
  965. var
  966. localExepath : TCmdStr;
  967. exeName:TCmdStr;
  968. {$ifdef need_path_search}
  969. hs1 : TPathStr;
  970. {$endif need_path_search}
  971. begin
  972. localexepath:=GetEnvironmentVariable('PPC_EXEC_PATH');
  973. if localexepath='' then
  974. begin
  975. exeName := FixFileName(system.paramstr(0));
  976. localexepath := ExtractFilePath(exeName);
  977. end;
  978. {$ifdef need_path_search}
  979. if localexepath='' then
  980. begin
  981. hs1 := ExtractFileName(exeName);
  982. ChangeFileExt(hs1,source_info.exeext);
  983. {$ifdef macos}
  984. FindFile(hs1,GetEnvironmentVariable('Commands'),false,localExepath);
  985. {$else macos}
  986. FindFile(hs1,GetEnvironmentVariable('PATH'),false,localExepath);
  987. {$endif macos}
  988. localExepath:=ExtractFilePath(localExepath);
  989. end;
  990. {$endif need_path_search}
  991. exepath:=FixPath(localExepath,false);
  992. end;
  993. procedure DoneGlobals;
  994. begin
  995. if assigned(DLLImageBase) then
  996. StringDispose(DLLImageBase);
  997. librarysearchpath.Free;
  998. unitsearchpath.Free;
  999. objectsearchpath.Free;
  1000. includesearchpath.Free;
  1001. frameworksearchpath.Free;
  1002. LinkLibraryAliases.Free;
  1003. LinkLibraryOrder.Free;
  1004. end;
  1005. procedure InitGlobals;
  1006. begin
  1007. get_exepath;
  1008. { reset globals }
  1009. do_build:=false;
  1010. do_release:=false;
  1011. do_make:=true;
  1012. compile_level:=0;
  1013. DLLsource:=false;
  1014. resolving_forward:=false;
  1015. paratarget:=system_none;
  1016. paratargetasm:=as_none;
  1017. paratargetdbg:=dbg_none;
  1018. { Output }
  1019. OutputFileName:='';
  1020. OutputPrefix:=Nil;
  1021. OutputSuffix:=Nil;
  1022. OutputExeDir:='';
  1023. OutputUnitDir:='';
  1024. { Utils directory }
  1025. utilsdirectory:='';
  1026. utilsprefix:='';
  1027. cshared:=false;
  1028. rlinkpath:='';
  1029. { Search Paths }
  1030. librarysearchpath:=TSearchPathList.Create;
  1031. unitsearchpath:=TSearchPathList.Create;
  1032. includesearchpath:=TSearchPathList.Create;
  1033. objectsearchpath:=TSearchPathList.Create;
  1034. frameworksearchpath:=TSearchPathList.Create;
  1035. { Def file }
  1036. usewindowapi:=false;
  1037. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  1038. DescriptionSetExplicity:=false;
  1039. SetPEFlagsSetExplicity:=false;
  1040. ImageBaseSetExplicity:=false;
  1041. MinStackSizeSetExplicity:=false;
  1042. MaxStackSizeSetExplicity:=false;
  1043. dllversion:='';
  1044. dllmajor:=1;
  1045. dllminor:=0;
  1046. dllrevision:=0;
  1047. nwscreenname := '';
  1048. nwthreadname := '';
  1049. nwcopyright := '';
  1050. UseDeffileForExports:=false;
  1051. UseDeffileForExportsSetExplicitly:=false;
  1052. GenerateImportSection:=false;
  1053. RelocSection:=false;
  1054. RelocSectionSetExplicitly:=false;
  1055. LinkTypeSetExplicitly:=false;
  1056. { memory sizes, will be overriden by parameter or default for target
  1057. in options or init_parser }
  1058. stacksize:=0;
  1059. { not initialized yet }
  1060. jmp_buf_size:=-1;
  1061. apptype:=app_cui;
  1062. { Init values }
  1063. init_settings.modeswitches:=fpcmodeswitches;
  1064. init_settings.localswitches:=[cs_check_io,cs_typed_const_writable];
  1065. init_settings.moduleswitches:=[cs_extsyntax,cs_implicit_exceptions];
  1066. init_settings.globalswitches:=[cs_check_unit_name,cs_link_static];
  1067. init_settings.optimizerswitches:=[];
  1068. init_settings.debugswitches:=[];
  1069. init_settings.sourcecodepage:='8859-1';
  1070. init_settings.packenum:=4;
  1071. init_settings.setalloc:=0;
  1072. fillchar(init_settings.alignment,sizeof(talignmentinfo),0);
  1073. init_settings.minfpconstprec:=s32real;
  1074. { might be overridden later }
  1075. init_settings.asmmode:=asmmode_standard;
  1076. init_settings.cputype:=cpu_none;
  1077. init_settings.optimizecputype:=cpu_none;
  1078. init_settings.fputype:=fpu_none;
  1079. init_settings.interfacetype:=it_interfacecom;
  1080. init_settings.defproccall:=pocall_default;
  1081. { Target specific defaults, these can override previous default options }
  1082. {$ifdef i386}
  1083. init_settings.cputype:=cpu_Pentium;
  1084. init_settings.optimizecputype:=cpu_Pentium3;
  1085. init_settings.fputype:=fpu_x87;
  1086. {$endif i386}
  1087. {$ifdef m68k}
  1088. init_settings.cputype:=cpu_MC68020;
  1089. init_settings.fputype:=fpu_soft;
  1090. {$endif m68k}
  1091. {$ifdef powerpc}
  1092. init_settings.cputype:=cpu_PPC604;
  1093. init_settings.optimizecputype:=cpu_ppc7400;
  1094. init_settings.fputype:=fpu_standard;
  1095. {$endif powerpc}
  1096. {$ifdef POWERPC64}
  1097. init_settings.cputype:=cpu_PPC970;
  1098. init_settings.optimizecputype:=cpu_ppc970;
  1099. init_settings.fputype:=fpu_standard;
  1100. {$endif POWERPC64}
  1101. {$ifdef sparc}
  1102. init_settings.cputype:=cpu_SPARC_V8;
  1103. init_settings.fputype:=fpu_hard;
  1104. {$endif sparc}
  1105. {$ifdef arm}
  1106. init_settings.cputype:=cpu_armv3;
  1107. init_settings.fputype:=fpu_fpa;
  1108. {$endif arm}
  1109. {$ifdef x86_64}
  1110. init_settings.cputype:=cpu_athlon64;
  1111. init_settings.fputype:=fpu_sse64;
  1112. {$endif x86_64}
  1113. if init_settings.optimizecputype=cpu_none then
  1114. init_settings.optimizecputype:=init_settings.cputype;
  1115. LinkLibraryAliases :=TLinkStrMap.Create;
  1116. LinkLibraryOrder :=TLinkStrMap.Create;
  1117. { enable all features by default }
  1118. features:=[low(Tfeature)..high(Tfeature)];
  1119. end;
  1120. end.