globals.pas 36 KB

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