globals.pas 37 KB

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