globals.pas 36 KB

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