globals.pas 37 KB

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