globals.pas 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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. {$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. pfileposinfo = ^tfileposinfo;
  90. tfileposinfo = record
  91. line : longint;
  92. column : word;
  93. fileindex : word;
  94. moduleindex : word;
  95. end;
  96. tcodepagestring = string[20];
  97. tsettings = record
  98. globalswitches : tglobalswitches;
  99. moduleswitches : tmoduleswitches;
  100. localswitches : tlocalswitches;
  101. modeswitches : tmodeswitches;
  102. optimizerswitches : toptimizerswitches;
  103. debugswitches : tdebugswitches;
  104. { 0: old behaviour for sets <=256 elements
  105. >0: round to this size }
  106. setalloc,
  107. packenum : shortint;
  108. alignment : talignmentinfo;
  109. cputype,
  110. optimizecputype : tcputype;
  111. fputype : tfputype;
  112. asmmode : tasmmode;
  113. interfacetype : tinterfacetypes;
  114. defproccall : tproccalloption;
  115. sourcecodepage : tcodepagestring;
  116. packrecords : shortint;
  117. maxfpuregisters : shortint;
  118. minfpconstprec : tfloattype;
  119. end;
  120. const
  121. LinkMapWeightDefault = 1000;
  122. type
  123. TLinkRec = record
  124. Key : AnsiString;
  125. Value : AnsiString; // key expands to valuelist "value"
  126. Weight: longint;
  127. end;
  128. TLinkStrMap = class
  129. private
  130. itemcnt : longint;
  131. fmap : Array Of TLinkRec;
  132. function Lookup(key:Ansistring):longint;
  133. function getlinkrec(i:longint):TLinkRec;
  134. public
  135. procedure Add(key:ansistring;value:AnsiString='';weight:longint=LinkMapWeightDefault);
  136. procedure addseries(keys:AnsiString;weight:longint=LinkMapWeightDefault);
  137. function AddDep(keyvalue:String):boolean;
  138. function AddWeight(keyvalue:String):boolean;
  139. procedure SetValue(key:AnsiString;Weight:Integer);
  140. procedure SortonWeight;
  141. function Find(key:AnsiString):AnsiString;
  142. procedure Expand(src:TCmdStrList;dest: TLinkStrMap);
  143. procedure UpdateWeights(Weightmap:TLinkStrMap);
  144. constructor Create;
  145. property count : longint read itemcnt;
  146. property items[I:longint]:TLinkRec read getlinkrec; default;
  147. end;
  148. var
  149. { specified inputfile }
  150. inputfilepath : string;
  151. inputfilename : string;
  152. { specified outputfile with -o parameter }
  153. outputfilename : string;
  154. outputprefix : pshortstring;
  155. outputsuffix : pshortstring;
  156. { specified with -FE or -FU }
  157. outputexedir : TPathStr;
  158. outputunitdir : TPathStr;
  159. { things specified with parameters }
  160. paratarget : tsystem;
  161. paratargetdbg : tdbg;
  162. paratargetasm : tasm;
  163. paralinkoptions : TCmdStr;
  164. paradynamiclinker : string;
  165. paraprintnodetree : byte;
  166. parapreprocess : boolean;
  167. printnodefile : text;
  168. { typical cross compiling params}
  169. { directory where the utils can be found (options -FD) }
  170. utilsdirectory : TPathStr;
  171. { targetname specific prefix used by these utils (options -XP<path>) }
  172. utilsprefix : TCmdStr;
  173. cshared : boolean; { pass --shared to ld to link C libs shared}
  174. Dontlinkstdlibpath: Boolean; { Don't add std paths to linkpath}
  175. rlinkpath : TCmdStr; { rpath-link linkdir override}
  176. sysrootpath : TCmdStr; { target system root to search dyn linker }
  177. { some flags for global compiler switches }
  178. do_build,
  179. do_release,
  180. do_make : boolean;
  181. { path for searching units, different paths can be seperated by ; }
  182. exepath : TPathStr; { Path to ppc }
  183. librarysearchpath,
  184. unitsearchpath,
  185. objectsearchpath,
  186. includesearchpath,
  187. frameworksearchpath : TSearchPathList;
  188. autoloadunits : string;
  189. { linking }
  190. usewindowapi : boolean;
  191. description : string;
  192. SetPEFlagsSetExplicity,
  193. ImageBaseSetExplicity,
  194. MinStackSizeSetExplicity,
  195. MaxStackSizeSetExplicity,
  196. DescriptionSetExplicity : boolean;
  197. dllversion : string;
  198. dllmajor,
  199. dllminor,
  200. dllrevision : word; { revision only for netware }
  201. { win pe }
  202. peflags : longint;
  203. minstacksize,
  204. maxstacksize,
  205. imagebase : aword;
  206. UseDeffileForExports : boolean;
  207. UseDeffileForExportsSetExplicitly : boolean;
  208. GenerateImportSection,
  209. GenerateImportSectionSetExplicitly,
  210. RelocSection : boolean;
  211. RelocSectionSetExplicitly : boolean;
  212. LinkTypeSetExplicitly : boolean;
  213. current_tokenpos, { position of the last token }
  214. current_filepos : tfileposinfo; { current position }
  215. nwscreenname : string;
  216. nwthreadname : string;
  217. nwcopyright : string;
  218. codegenerror : boolean; { true if there is an error reported }
  219. block_type : tblock_type; { type of currently parsed block }
  220. parsing_para_level : integer; { parameter level, used to convert
  221. proc calls to proc loads in firstcalln }
  222. compile_level : word;
  223. resolving_forward : boolean; { used to add forward reference as second ref }
  224. exceptblockcounter : integer; { each except block gets a unique number check gotos }
  225. aktexceptblock : integer; { the exceptblock number of the current block (0 if none) }
  226. LinkLibraryAliases : TLinkStrMap;
  227. LinkLibraryOrder : TLinkStrMap;
  228. init_settings,
  229. current_settings : tsettings;
  230. nextlocalswitches : tlocalswitches;
  231. localswitcheschanged : boolean;
  232. { Memory sizes }
  233. heapsize,
  234. stacksize,
  235. jmp_buf_size : longint;
  236. {$Ifdef EXTDEBUG}
  237. { parameter switches }
  238. debugstop : boolean;
  239. {$EndIf EXTDEBUG}
  240. { windows / OS/2 application type }
  241. apptype : tapptype;
  242. features : tfeatures;
  243. const
  244. DLLsource : boolean = false;
  245. DLLImageBase : pshortstring = nil;
  246. { used to set all registers used for each global function
  247. this should dramatically decrease the number of
  248. recompilations needed PM }
  249. simplify_ppu : boolean = true;
  250. Inside_asm_statement : boolean = false;
  251. global_unit_count : word = 0;
  252. { for error info in pp.pas }
  253. parser_current_file : string = '';
  254. {$if defined(m68k) or defined(arm)}
  255. { PalmOS resources }
  256. palmos_applicationname : string = 'FPC Application';
  257. palmos_applicationid : string[4] = 'FPCA';
  258. {$endif defined(m68k) or defined(arm)}
  259. {$ifdef powerpc}
  260. { default calling convention used on MorphOS }
  261. syscall_convention : string = 'LEGACY';
  262. {$endif powerpc}
  263. { default name of the C-style "main" procedure of the library/program }
  264. { (this will be prefixed with the target_info.cprefix) }
  265. defaultmainaliasname = 'main';
  266. mainaliasname : string = defaultmainaliasname;
  267. { by default no local variable trashing }
  268. localvartrashing: longint = -1;
  269. { actual values are defined in ncgutil.pas }
  270. nroftrashvalues = 4;
  271. var
  272. starttime : real;
  273. function getdatestr:string;
  274. function gettimestr:string;
  275. function filetimestring( t : longint) : string;
  276. function getrealtime : real;
  277. procedure DefaultReplacements(var s:ansistring);
  278. function Shell(const command:ansistring): longint;
  279. function GetEnvPChar(const envname:string):pchar;
  280. procedure FreeEnvPChar(p:pchar);
  281. function is_number_float(d : double) : boolean;
  282. { discern +0.0 and -0.0 }
  283. function get_real_sign(r: bestreal): longint;
  284. procedure InitGlobals;
  285. procedure DoneGlobals;
  286. function string2guid(const s: string; var GUID: TGUID): boolean;
  287. function guid2string(const GUID: TGUID): string;
  288. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  289. function Setcputype(const s:string;var a:tcputype):boolean;
  290. function SetFpuType(const s:string;var a:tfputype):boolean;
  291. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  292. function UpdateOptimizerStr(s:string;var a:toptimizerswitches):boolean;
  293. function UpdateDebugStr(s:string;var a:tdebugswitches):boolean;
  294. function IncludeFeature(const s : string) : boolean;
  295. function SetMinFPConstPrec(const s: string; var a: tfloattype) : boolean;
  296. {# Routine to get the required alignment for size of data, which will
  297. be placed in bss segment, according to the current alignment requirements }
  298. function var_align(siz: longint): shortint;
  299. {# Routine to get the required alignment for size of data, which will
  300. be placed in data/const segment, according to the current alignment requirements }
  301. function const_align(siz: longint): shortint;
  302. {$ifdef ARM}
  303. function is_double_hilo_swapped: boolean;{$ifdef USEINLINE}inline;{$endif}
  304. {$endif ARM}
  305. implementation
  306. uses
  307. {$ifdef macos}
  308. macutils,
  309. {$endif}
  310. comphook;
  311. {****************************************************************************
  312. TLinkStrMap
  313. ****************************************************************************}
  314. Constructor TLinkStrMap.create;
  315. begin
  316. inherited;
  317. itemcnt:=0;
  318. end;
  319. procedure TLinkStrMap.Add(key:ansistring;value:AnsiString='';weight:longint=LinkMapWeightDefault);
  320. begin
  321. if lookup(key)<>-1 Then
  322. exit;
  323. if itemcnt<=length(fmap) Then
  324. setlength(fmap,itemcnt+10);
  325. fmap[itemcnt].key:=key;
  326. fmap[itemcnt].value:=value;
  327. fmap[itemcnt].weight:=weight;
  328. inc(itemcnt);
  329. end;
  330. function TLinkStrMap.AddDep(keyvalue:String):boolean;
  331. var
  332. i : Longint;
  333. begin
  334. AddDep:=false;
  335. i:=pos('=',keyvalue);
  336. if i=0 then
  337. exit;
  338. Add(Copy(KeyValue,1,i-1),Copy(KeyValue,i+1,length(KeyValue)-i));
  339. AddDep:=True;
  340. end;
  341. function TLinkStrMap.AddWeight(keyvalue:String):boolean;
  342. var
  343. i,j : Longint;
  344. Code : Word;
  345. s : AnsiString;
  346. begin
  347. AddWeight:=false;
  348. i:=pos('=',keyvalue);
  349. if i=0 then
  350. exit;
  351. s:=Copy(KeyValue,i+1,length(KeyValue)-i);
  352. val(s,j,code);
  353. if code=0 Then
  354. begin
  355. Add(Copy(KeyValue,1,i-1),'',j);
  356. AddWeight:=True;
  357. end;
  358. end;
  359. procedure TLinkStrMap.addseries(keys:AnsiString;weight:longint);
  360. var
  361. i,j,k : longint;
  362. begin
  363. k:=length(keys);
  364. i:=1;
  365. while i<=k do
  366. begin
  367. j:=i;
  368. while (i<=k) and (keys[i]<>',') do
  369. inc(i);
  370. add(copy(keys,j,i-j),'',weight);
  371. inc(i);
  372. end;
  373. end;
  374. procedure TLinkStrMap.SetValue(Key:Ansistring;weight:Integer);
  375. var
  376. j : longint;
  377. begin
  378. j:=lookup(key);
  379. if j<>-1 then
  380. fmap[j].weight:=weight;
  381. end;
  382. function TLinkStrMap.find(key:Ansistring):Ansistring;
  383. var
  384. j : longint;
  385. begin
  386. find:='';
  387. j:=lookup(key);
  388. if j<>-1 then
  389. find:=fmap[j].value;
  390. end;
  391. function TLinkStrMap.lookup(key:Ansistring):longint;
  392. var
  393. i : longint;
  394. begin
  395. lookup:=-1;
  396. i:=0;
  397. while (i<itemcnt) and (fmap[i].key<>key) do
  398. inc(i);
  399. if i<>itemcnt then
  400. lookup:=i;
  401. end;
  402. procedure TLinkStrMap.SortOnWeight;
  403. var
  404. i, j : longint;
  405. m : TLinkRec;
  406. begin
  407. if itemcnt <2 then exit;
  408. for i:=0 to itemcnt-1 do
  409. for j:=i+1 to itemcnt-1 do
  410. begin
  411. if fmap[i].weight>fmap[j].weight Then
  412. begin
  413. m:=fmap[i];
  414. fmap[i]:=fmap[j];
  415. fmap[j]:=m;
  416. end;
  417. end;
  418. end;
  419. function TLinkStrMap.getlinkrec(i:longint):TLinkRec;
  420. begin
  421. result:=fmap[i];
  422. end;
  423. procedure TLinkStrMap.Expand(Src:TCmdStrList;Dest:TLinkStrMap);
  424. // expands every thing in Src to Dest for linkorder purposes.
  425. var
  426. r : longint;
  427. LibN : TCmdStr;
  428. begin
  429. while not src.empty do
  430. begin
  431. LibN:=src.getfirst;
  432. r:=lookup (LibN);
  433. if r=-1 then
  434. dest.add(LibN)
  435. else
  436. dest.addseries(fmap[r].value);
  437. end;
  438. end;
  439. procedure TLinkStrMap.UpdateWeights(Weightmap:TLinkStrMap);
  440. var
  441. l,r : longint;
  442. begin
  443. for l := 0 to itemcnt-1 do
  444. begin
  445. r:=weightmap.lookup (fmap[l].key);
  446. if r<>-1 then
  447. fmap[l].weight:=weightmap[r].weight;
  448. end;
  449. end;
  450. {****************************************************************************
  451. Time Handling
  452. ****************************************************************************}
  453. Function L0(l:longint):string;
  454. {
  455. return the string of value l, if l<10 then insert a zero, so
  456. the string is always at least 2 chars '01','02',etc
  457. }
  458. var
  459. s : string;
  460. begin
  461. Str(l,s);
  462. if l<10 then
  463. s:='0'+s;
  464. L0:=s;
  465. end;
  466. function gettimestr:string;
  467. {
  468. get the current time in a string HH:MM:SS
  469. }
  470. var
  471. hour,min,sec,hsec : word;
  472. begin
  473. DecodeTime(Time,hour,min,sec,hsec);
  474. gettimestr:=L0(Hour)+':'+L0(min)+':'+L0(sec);
  475. end;
  476. function getdatestr:string;
  477. {
  478. get the current date in a string YY/MM/DD
  479. }
  480. var
  481. Year,Month,Day: Word;
  482. begin
  483. DecodeDate(Date,year,month,day);
  484. getdatestr:=L0(Year)+'/'+L0(Month)+'/'+L0(Day);
  485. end;
  486. function filetimestring( t : longint) : string;
  487. {
  488. convert dos datetime t to a string YY/MM/DD HH:MM:SS
  489. }
  490. var
  491. DT : TDateTime;
  492. hsec : word;
  493. Year,Month,Day: Word;
  494. hour,min,sec : word;
  495. begin
  496. if t=-1 then
  497. begin
  498. Result := 'Not Found';
  499. exit;
  500. end;
  501. DT := FileDateToDateTime(t);
  502. DecodeTime(DT,hour,min,sec,hsec);
  503. DecodeDate(DT,year,month,day);
  504. Result := L0(Year)+'/'+L0(Month)+'/'+L0(Day)+' '+L0(Hour)+':'+L0(min)+':'+L0(sec);
  505. end;
  506. function getrealtime : real;
  507. var
  508. h,m,s,s1000 : word;
  509. begin
  510. DecodeTime(Time,h,m,s,s1000);
  511. result:=h*3600.0+m*60.0+s+s1000/1000.0;
  512. end;
  513. {****************************************************************************
  514. Default Macro Handling
  515. ****************************************************************************}
  516. procedure DefaultReplacements(var s:ansistring);
  517. begin
  518. { Replace some macros }
  519. Replace(s,'$FPCVERSION',version_string);
  520. Replace(s,'$FPCFULLVERSION',full_version_string);
  521. Replace(s,'$FPCDATE',date_string);
  522. Replace(s,'$FPCCPU',target_cpu_string);
  523. Replace(s,'$FPCOS',target_os_string);
  524. if tf_use_8_3 in Source_Info.Flags then
  525. Replace(s,'$FPCTARGET',target_os_string)
  526. else
  527. Replace(s,'$FPCTARGET',target_full_string);
  528. end;
  529. {****************************************************************************
  530. OS Dependent things
  531. ****************************************************************************}
  532. function GetEnvPChar(const envname:string):pchar;
  533. {$ifdef win32}
  534. var
  535. s : string;
  536. i,len : longint;
  537. hp,p,p2 : pchar;
  538. {$endif}
  539. begin
  540. {$ifdef hasunix}
  541. GetEnvPchar:=BaseUnix.fpGetEnv(envname);
  542. {$define GETENVOK}
  543. {$endif}
  544. {$ifdef win32}
  545. GetEnvPchar:=nil;
  546. p:=GetEnvironmentStrings;
  547. hp:=p;
  548. while hp^<>#0 do
  549. begin
  550. s:=strpas(hp);
  551. i:=pos('=',s);
  552. len:=strlen(hp);
  553. if upper(copy(s,1,i-1))=upper(envname) then
  554. begin
  555. GetMem(p2,len-length(envname));
  556. Move(hp[i],p2^,len-length(envname));
  557. GetEnvPchar:=p2;
  558. break;
  559. end;
  560. { next string entry}
  561. hp:=hp+len+1;
  562. end;
  563. FreeEnvironmentStrings(p);
  564. {$define GETENVOK}
  565. {$endif}
  566. {$ifdef os2}
  567. GetEnvPChar := Dos.GetEnvPChar (EnvName);
  568. {$define GETENVOK}
  569. {$endif}
  570. {$ifdef GETENVOK}
  571. {$undef GETENVOK}
  572. {$else}
  573. GetEnvPchar:=StrPNew(GetEnvironmentVariable(envname));
  574. {$endif}
  575. end;
  576. procedure FreeEnvPChar(p:pchar);
  577. begin
  578. {$ifndef hasunix}
  579. {$ifndef os2}
  580. freemem(p);
  581. {$endif}
  582. {$endif}
  583. end;
  584. {$if defined(MORPHOS) or defined(AMIGA)}
  585. {$define AMIGASHELL}
  586. {$endif}
  587. function Shell(const command:ansistring): longint;
  588. { This is already defined in the linux.ppu for linux, need for the *
  589. expansion under linux }
  590. {$ifdef hasunix}
  591. begin
  592. result := Unix.fpsystem(command);
  593. end;
  594. {$else hasunix}
  595. {$ifdef amigashell}
  596. begin
  597. result := ExecuteProcess('',command);
  598. end;
  599. {$else amigashell}
  600. var
  601. comspec : string;
  602. begin
  603. comspec:=GetEnvironmentVariable('COMSPEC');
  604. result := ExecuteProcess(comspec,' /C '+command);
  605. end;
  606. {$endif amigashell}
  607. {$endif hasunix}
  608. {$UNDEF AMIGASHELL}
  609. function is_number_float(d : double) : boolean;
  610. var
  611. bytearray : array[0..7] of byte;
  612. begin
  613. move(d,bytearray,8);
  614. { only 1.1 save, 1.0.x will use always little endian }
  615. {$ifdef FPC_BIG_ENDIAN}
  616. result:=((bytearray[0] and $7f)<>$7f) or ((bytearray[1] and $f0)<>$f0);
  617. {$else FPC_BIG_ENDIAN}
  618. result:=((bytearray[7] and $7f)<>$7f) or ((bytearray[6] and $f0)<>$f0);
  619. {$endif FPC_BIG_ENDIAN}
  620. end;
  621. function get_real_sign(r: bestreal): longint;
  622. var
  623. p: pbyte;
  624. begin
  625. p := pbyte(@r);
  626. {$ifdef CPU_ARM}
  627. inc(p,4);
  628. {$else}
  629. {$ifdef FPC_LITTLE_ENDIAN}
  630. inc(p,sizeof(r)-1);
  631. {$endif}
  632. {$endif}
  633. if (p^ and $80) = 0 then
  634. result := 1
  635. else
  636. result := -1;
  637. end;
  638. function convertdoublerec(d : tdoublerec) : tdoublerec;{$ifdef USEINLINE}inline;{$endif}
  639. {$ifdef CPUARM}
  640. var
  641. i : longint;
  642. begin
  643. for i:=0 to 3 do
  644. begin
  645. result.bytes[i+4]:=d.bytes[i];
  646. result.bytes[i]:=d.bytes[i+4];
  647. end;
  648. {$else CPUARM}
  649. begin
  650. result:=d;
  651. {$endif CPUARM}
  652. end;
  653. { '('D1:'00000000-'D2:'0000-'D3:'0000-'D4:'0000-000000000000)' }
  654. function string2guid(const s: string; var GUID: TGUID): boolean;
  655. function ishexstr(const hs: string): boolean;
  656. var
  657. i: integer;
  658. begin
  659. ishexstr:=false;
  660. for i:=1 to Length(hs) do begin
  661. if not (hs[i] in ['0'..'9','A'..'F','a'..'f']) then
  662. exit;
  663. end;
  664. ishexstr:=true;
  665. end;
  666. function hexstr2longint(const hexs: string): longint;
  667. var
  668. i: integer;
  669. rl: longint;
  670. begin
  671. rl:=0;
  672. for i:=1 to length(hexs) do begin
  673. rl:=rl shl 4;
  674. case hexs[i] of
  675. '0'..'9' : inc(rl,ord(hexs[i])-ord('0'));
  676. 'A'..'F' : inc(rl,ord(hexs[i])-ord('A')+10);
  677. 'a'..'f' : inc(rl,ord(hexs[i])-ord('a')+10);
  678. end
  679. end;
  680. hexstr2longint:=rl;
  681. end;
  682. var
  683. i: integer;
  684. begin
  685. if (Length(s)=38) and (s[1]='{') and (s[38]='}') and
  686. (s[10]='-') and (s[15]='-') and (s[20]='-') and (s[25]='-') and
  687. ishexstr(copy(s,2,8)) and ishexstr(copy(s,11,4)) and
  688. ishexstr(copy(s,16,4)) and ishexstr(copy(s,21,4)) and
  689. ishexstr(copy(s,26,12)) then begin
  690. GUID.D1:=dword(hexstr2longint(copy(s,2,8)));
  691. { these values are arealdy in the correct range (4 chars = word) }
  692. GUID.D2:=word(hexstr2longint(copy(s,11,4)));
  693. GUID.D3:=word(hexstr2longint(copy(s,16,4)));
  694. for i:=0 to 1 do
  695. GUID.D4[i]:=byte(hexstr2longint(copy(s,21+i*2,2)));
  696. for i:=2 to 7 do
  697. GUID.D4[i]:=byte(hexstr2longint(copy(s,22+i*2,2)));
  698. string2guid:=true;
  699. end
  700. else if (length(s)=0) then
  701. begin
  702. FillChar(GUID,SizeOf(GUID),0);
  703. string2guid:=true;
  704. end
  705. else
  706. string2guid:=false;
  707. end;
  708. function guid2string(const GUID: TGUID): string;
  709. begin
  710. guid2string:=
  711. '{'+hexstr(GUID.D1,8)+
  712. '-'+hexstr(GUID.D2,4)+
  713. '-'+hexstr(GUID.D3,4)+
  714. '-'+hexstr(GUID.D4[0],2)+hexstr(GUID.D4[1],2)+
  715. '-'+hexstr(GUID.D4[2],2)+hexstr(GUID.D4[3],2)+
  716. hexstr(GUID.D4[4],2)+hexstr(GUID.D4[5],2)+
  717. hexstr(GUID.D4[6],2)+hexstr(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 UpdateDebugStr(s:string;var a:tdebugswitches):boolean;
  871. var
  872. tok : string;
  873. doset,
  874. found : boolean;
  875. opt : tdebugswitch;
  876. begin
  877. result:=true;
  878. uppervar(s);
  879. repeat
  880. tok:=GetToken(s,',');
  881. if tok='' then
  882. break;
  883. if Copy(tok,1,2)='NO' then
  884. begin
  885. delete(tok,1,2);
  886. doset:=false;
  887. end
  888. else
  889. doset:=true;
  890. found:=false;
  891. for opt:=low(tdebugswitch) to high(tdebugswitch) do
  892. begin
  893. if DebugSwitchStr[opt]=tok then
  894. begin
  895. found:=true;
  896. break;
  897. end;
  898. end;
  899. if found then
  900. begin
  901. if doset then
  902. include(a,opt)
  903. else
  904. exclude(a,opt);
  905. end
  906. else
  907. result:=false;
  908. until false;
  909. end;
  910. function IncludeFeature(const s : string) : boolean;
  911. var
  912. i : tfeature;
  913. begin
  914. result:=true;
  915. for i:=low(tfeature) to high(tfeature) do
  916. if s=featurestr[i] then
  917. begin
  918. include(features,i);
  919. exit;
  920. end;
  921. result:=false;
  922. end;
  923. function SetMinFPConstPrec(const s: string; var a: tfloattype) : boolean;
  924. var
  925. value, error: longint;
  926. begin
  927. if (upper(s)='DEFAULT') then
  928. begin
  929. a:=s32real;
  930. result:=true;
  931. exit;
  932. end;
  933. result:=false;
  934. val(s,value,error);
  935. if (error<>0) then
  936. exit;
  937. case value of
  938. 32: a:=s32real;
  939. 64: a:=s64real;
  940. { adding support for 80 bit here is tricky, since we can't really }
  941. { check whether the target cpu+OS actually supports it }
  942. else
  943. exit;
  944. end;
  945. result:=true;
  946. end;
  947. function var_align(siz: longint): shortint;
  948. begin
  949. siz := size_2_align(siz);
  950. var_align := used_align(siz,current_settings.alignment.varalignmin,current_settings.alignment.varalignmax);
  951. end;
  952. function const_align(siz: longint): shortint;
  953. begin
  954. siz := size_2_align(siz);
  955. const_align := used_align(siz,current_settings.alignment.constalignmin,current_settings.alignment.constalignmax);
  956. end;
  957. {$ifdef ARM}
  958. function is_double_hilo_swapped: boolean;{$ifdef USEINLINE}inline;{$endif}
  959. begin
  960. result := (current_settings.fputype in [fpu_fpa,fpu_fpa10,fpu_fpa11]) and
  961. not(cs_fp_emulation in current_settings.moduleswitches);
  962. {$ifdef FPC_DOUBLE_HILO_SWAPPED}
  963. { inverse result if compiler was compiled with swapped hilo already }
  964. result := not result;
  965. {$endif FPC_DOUBLE_HILO_SWAPPED}
  966. end;
  967. {$endif ARM}
  968. {****************************************************************************
  969. Init
  970. ****************************************************************************}
  971. {$ifdef unix}
  972. {$define need_path_search}
  973. {$endif unix}
  974. {$ifdef os2}
  975. {$define need_path_search}
  976. {$endif os2}
  977. {$ifdef macos}
  978. {$define need_path_search}
  979. {$endif macos}
  980. procedure get_exepath;
  981. var
  982. localExepath : TCmdStr;
  983. exeName:TCmdStr;
  984. {$ifdef need_path_search}
  985. hs1 : TPathStr;
  986. {$endif need_path_search}
  987. begin
  988. localexepath:=GetEnvironmentVariable('PPC_EXEC_PATH');
  989. if localexepath='' then
  990. begin
  991. exeName := FixFileName(system.paramstr(0));
  992. localexepath := ExtractFilePath(exeName);
  993. end;
  994. {$ifdef need_path_search}
  995. if localexepath='' then
  996. begin
  997. hs1 := ExtractFileName(exeName);
  998. ChangeFileExt(hs1,source_info.exeext);
  999. {$ifdef macos}
  1000. FindFile(hs1,GetEnvironmentVariable('Commands'),false,localExepath);
  1001. {$else macos}
  1002. FindFile(hs1,GetEnvironmentVariable('PATH'),false,localExepath);
  1003. {$endif macos}
  1004. localExepath:=ExtractFilePath(localExepath);
  1005. end;
  1006. {$endif need_path_search}
  1007. exepath:=FixPath(localExepath,false);
  1008. end;
  1009. procedure DoneGlobals;
  1010. begin
  1011. if assigned(DLLImageBase) then
  1012. StringDispose(DLLImageBase);
  1013. librarysearchpath.Free;
  1014. unitsearchpath.Free;
  1015. objectsearchpath.Free;
  1016. includesearchpath.Free;
  1017. frameworksearchpath.Free;
  1018. LinkLibraryAliases.Free;
  1019. LinkLibraryOrder.Free;
  1020. end;
  1021. procedure InitGlobals;
  1022. begin
  1023. get_exepath;
  1024. { reset globals }
  1025. do_build:=false;
  1026. do_release:=false;
  1027. do_make:=true;
  1028. compile_level:=0;
  1029. DLLsource:=false;
  1030. resolving_forward:=false;
  1031. paratarget:=system_none;
  1032. paratargetasm:=as_none;
  1033. paratargetdbg:=dbg_none;
  1034. { Output }
  1035. OutputFileName:='';
  1036. OutputPrefix:=Nil;
  1037. OutputSuffix:=Nil;
  1038. OutputExeDir:='';
  1039. OutputUnitDir:='';
  1040. { Utils directory }
  1041. utilsdirectory:='';
  1042. utilsprefix:='';
  1043. cshared:=false;
  1044. rlinkpath:='';
  1045. sysrootpath:='';
  1046. { Search Paths }
  1047. librarysearchpath:=TSearchPathList.Create;
  1048. unitsearchpath:=TSearchPathList.Create;
  1049. includesearchpath:=TSearchPathList.Create;
  1050. objectsearchpath:=TSearchPathList.Create;
  1051. frameworksearchpath:=TSearchPathList.Create;
  1052. { Def file }
  1053. usewindowapi:=false;
  1054. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  1055. DescriptionSetExplicity:=false;
  1056. SetPEFlagsSetExplicity:=false;
  1057. ImageBaseSetExplicity:=false;
  1058. MinStackSizeSetExplicity:=false;
  1059. MaxStackSizeSetExplicity:=false;
  1060. dllversion:='';
  1061. dllmajor:=1;
  1062. dllminor:=0;
  1063. dllrevision:=0;
  1064. nwscreenname := '';
  1065. nwthreadname := '';
  1066. nwcopyright := '';
  1067. UseDeffileForExports:=false;
  1068. UseDeffileForExportsSetExplicitly:=false;
  1069. GenerateImportSection:=false;
  1070. RelocSection:=false;
  1071. RelocSectionSetExplicitly:=false;
  1072. LinkTypeSetExplicitly:=false;
  1073. { memory sizes, will be overriden by parameter or default for target
  1074. in options or init_parser }
  1075. stacksize:=0;
  1076. { not initialized yet }
  1077. jmp_buf_size:=-1;
  1078. apptype:=app_cui;
  1079. { Init values }
  1080. init_settings.modeswitches:=fpcmodeswitches;
  1081. init_settings.localswitches:=[cs_check_io,cs_typed_const_writable];
  1082. init_settings.moduleswitches:=[cs_extsyntax,cs_implicit_exceptions];
  1083. init_settings.globalswitches:=[cs_check_unit_name,cs_link_static];
  1084. init_settings.optimizerswitches:=[];
  1085. init_settings.debugswitches:=[];
  1086. init_settings.sourcecodepage:='8859-1';
  1087. init_settings.packenum:=4;
  1088. init_settings.setalloc:=0;
  1089. fillchar(init_settings.alignment,sizeof(talignmentinfo),0);
  1090. init_settings.minfpconstprec:=s32real;
  1091. { might be overridden later }
  1092. init_settings.asmmode:=asmmode_standard;
  1093. init_settings.cputype:=cpu_none;
  1094. init_settings.optimizecputype:=cpu_none;
  1095. init_settings.fputype:=fpu_none;
  1096. init_settings.interfacetype:=it_interfacecom;
  1097. init_settings.defproccall:=pocall_default;
  1098. { Target specific defaults, these can override previous default options }
  1099. {$ifdef i386}
  1100. init_settings.cputype:=cpu_Pentium;
  1101. init_settings.optimizecputype:=cpu_Pentium3;
  1102. init_settings.fputype:=fpu_x87;
  1103. {$endif i386}
  1104. {$ifdef m68k}
  1105. init_settings.cputype:=cpu_MC68020;
  1106. init_settings.fputype:=fpu_soft;
  1107. {$endif m68k}
  1108. {$ifdef powerpc}
  1109. init_settings.cputype:=cpu_PPC604;
  1110. init_settings.optimizecputype:=cpu_ppc7400;
  1111. init_settings.fputype:=fpu_standard;
  1112. {$endif powerpc}
  1113. {$ifdef POWERPC64}
  1114. init_settings.cputype:=cpu_PPC970;
  1115. init_settings.optimizecputype:=cpu_ppc970;
  1116. init_settings.fputype:=fpu_standard;
  1117. {$endif POWERPC64}
  1118. {$ifdef sparc}
  1119. init_settings.cputype:=cpu_SPARC_V8;
  1120. init_settings.fputype:=fpu_hard;
  1121. {$endif sparc}
  1122. {$ifdef arm}
  1123. init_settings.cputype:=cpu_armv3;
  1124. init_settings.fputype:=fpu_fpa;
  1125. {$endif arm}
  1126. {$ifdef x86_64}
  1127. init_settings.cputype:=cpu_athlon64;
  1128. init_settings.fputype:=fpu_sse64;
  1129. {$endif x86_64}
  1130. if init_settings.optimizecputype=cpu_none then
  1131. init_settings.optimizecputype:=init_settings.cputype;
  1132. LinkLibraryAliases :=TLinkStrMap.Create;
  1133. LinkLibraryOrder :=TLinkStrMap.Create;
  1134. { enable all features by default }
  1135. features:=[low(Tfeature)..high(Tfeature)];
  1136. end;
  1137. end.