globals.pas 37 KB

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