globals.pas 37 KB

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